# `Codex.Sessions`
[🔗](https://github.com/nshkrdotcom/codex_sdk/blob/v0.18.1/lib/codex/sessions.ex#L1)

Helpers for inspecting Codex CLI session files and replaying recorded changes.

Patch application uses the shared non-PTY command lane. Only raw interactive
or provider-native control families remain on local subprocess ownership.

# `apply_result`

```elixir
@type apply_result() :: %{
  stdout: String.t(),
  stderr: String.t(),
  exit_code: integer(),
  success: boolean()
}
```

# `session_entry`

```elixir
@type session_entry() :: %{
  id: String.t(),
  path: String.t(),
  started_at: String.t() | nil,
  updated_at: String.t() | nil,
  cwd: String.t() | nil,
  originator: String.t() | nil,
  cli_version: String.t() | nil,
  metadata: map()
}
```

# `apply`

```elixir
@spec apply(
  String.t() | list(),
  keyword()
) :: {:ok, apply_result()} | {:error, term()}
```

Applies a unified diff or file_change items to the local working tree.

Accepts either a diff string or a list of `%Codex.Items.FileChange{}` structs
(or raw change maps with `path`, `kind`, and `diff` fields).

# `list_sessions`

```elixir
@spec list_sessions(keyword()) :: {:ok, [session_entry()]} | {:error, term()}
```

Lists known sessions by scanning the sessions directory.

## Options

- `:sessions_dir` - Override the default session directory.

# `undo`

```elixir
@spec undo(
  term(),
  keyword()
) :: {:ok, map()} | {:error, term()}
```

Restores the working tree using a ghost snapshot item.

Accepts a `%Codex.Items.GhostSnapshot{}` struct, a raw response item map, or a
ghost commit map with `id` and preexisting untracked fields.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
