Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Use [OpenAI Codex](https://github.com/openai/codex) from [Agent Client Protocol]
- [Native ACP subagent sessions](docs/subagent-sessions.md) with separate child histories and permissions. ACP v2 selects the standard route without capability negotiation.
- [Session cursors](docs/session-cursors.md): `cursor_update` positions and `session/fork` at a cursor.
- Editing a user message in place with [`_session/rewind`](docs/air-extensions.md#session-rewind) for AIR.
- A read-only [history tail](docs/air-extensions.md#history-tail) for AIR: the history of a stored session after a cursor, also of a session that another Codex client holds.
- Mid-turn input through the [_session/steering extension](docs/steering.md), with an opt-in host-owned fallback when no turn can accept it.
- [Async Codex questions](docs/async-questions.md) as non-blocking form elicitations, with answers steered into the turn that asked them.
- [Background terminal tasks](docs/background-work.md) with standard ACP v2 state and terminal Stop. ACP v1 retains the Air extension.
Expand Down
151 changes: 149 additions & 2 deletions docs/air-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ It describes only this adapter.
- [Context compaction](#context-compaction)
- [Session fork point](#session-fork-point)
- [Session rewind](#session-rewind)
- [Load replay end](#load-replay-end)
- [History tail](#history-tail)
- [Presentation hints](#presentation-hints)
- [Predicted follow-up prompt](#predicted-follow-up-prompt)
- [Removed keys](#removed-keys)
Expand Down Expand Up @@ -143,7 +145,8 @@ The `initialize` response carries the agent side of the extension only when the
"recommendedValue",
"rawInputRendering",
"planContentDelta",
"codexHooks"
"codexHooks",
"historyTail"
]
}
}
Expand Down Expand Up @@ -174,6 +177,8 @@ A client that is not AIR gets no `jetbrains` key and no `goal` key in the `initi
| `codexHooks` | Lets AIR review and trust startup hooks before it opens a session. | [Hook trust](#hook-trust) |
| `sessionIndex` | Serves `session/list` as the session index of AIR, as the ACP session list extensions RFD and RFD #2161 describe, adds rename and archive requests, and pushes the changed rows of a cwd to a subscription. | [Session index](#session-index) |
| `sessionRewind` | Accepts `_session/rewind`, and announces its session cursors with the `rewind` capability. ACP v1 only. | [Session rewind](#session-rewind) |
| `loadReplayEnd` | Sends one `session_info_update` after the whole history replay of a `session/load`. ACP v1 only. The agent does not advertise it. | [Load replay end](#load-replay-end) |
| `historyTail` | Adds the cursor of the replayed history to the load replay end. The agent advertises it and serves `_session/history_tail` to every v1 client. | [History tail](#history-tail) |

Five agent capabilities come with `sessionIndex` and are listed exactly when it is. The client does not declare them:

Expand Down Expand Up @@ -1240,7 +1245,8 @@ A thread that is not loaded here has no `state`. A change of `state` is a row ch
`_meta.jetbrains.air.archived`. For an archive it comes after the close, as the last update of the session.
- `session/load` and `session/resume` of an archived thread fail, and do not unarchive it.
- Errors:
- A thread that another Codex process holds fails with the `thread_active_writer` reason in `data.reason`.
- A thread that another Codex process holds fails with the `thread_active_writer` reason in `data.reason`
and an empty `data.held` object (see [History tail](#history-tail)).
- A thread that Codex does not have, or has deleted, fails with `-32002`, as does a thread that `thread/read`
shows without a rollout. V2 `session/delete` succeeds if the thread is already gone. Unarchive never brings back a deleted thread.
- Without `sessionIndex` the three `_session/*` methods answer method-not-found, and `session/delete` keeps
Expand Down Expand Up @@ -1506,6 +1512,147 @@ Every `thread/revert` call, even a failing one, reloads the thread: that interru
So the adapter decides `busy` and `backgroundWork` before it calls.
File changes are not reverted. The files stay as the removed turns left them.

## Load replay end

ACP v1 has no signal that the history replay of `session/load` is over, other than the response.
The client declares `loadReplayEnd` in its capabilities. The agent does not advertise it.
After the history replay of the loaded session, the agent then sends one more update for that session:

```json
{
"sessionId": "<loaded session>",
"update": {
"sessionUpdate": "session_info_update",
"_meta": { "jetbrains": { "air": { "version": 1, "loadReplayEnd": true } } }
}
}
```

- The update has no `title`. A `null` there would clear the title.
- It comes after every replayed update, the subagent history included, and before the `session/load` response.
- It comes also for an empty history.
- It does not come when the replay fails. A failed replay closes the session again.
- It does not come on ACP v2. The v2 `replay_complete` marks the same point there.
- For a client that also declares `historyTail`, it carries `historyCursor`, the cursor of the replayed history (see [History tail](#history-tail)).
A load that failed to read a subagent lacks its history, so its load replay end has no `historyCursor`. The next tail then reads the whole history.

## History tail

AIR shows sessions that another Codex client writes, such as the Codex CLI in a terminal.
A `session/load` of such a thread fails, because `thread/resume` takes the writer lock of the thread.
`_session/history_tail` gives the history of a stored thread after a cursor.
It reads the thread with `thread/read`, `thread/items/list` and `thread/turns/list`, and never calls `thread/resume`.
So it takes no writer lock, opens no session, and also reads a thread that another process holds.

The agent advertises `historyTail` to every AIR client on ACP v1, and serves the method there.
ACP v2 gets no `historyTail` and the method not found.
AIR on ACP v1 never uses standard subagent sessions. Another client with them gets `-32600`.

Request:

```json
{ "sessionId": "…", "cwd": "/Users/me/repo", "cursor": { "v": 1, "…": "…" }, "limit": 2000 }
```

- `sessionId` is required. A thread that Codex does not have is `-32002`.
- `cwd` is required and must be an absolute path. The tool calls of the history render against it, as in a load.
- `cursor` is optional. No cursor, or `null`, reads the whole history. The client sends back a cursor of an earlier answer or of a load replay end as it is.
- `limit` is optional and must be a positive integer. An answer holds about `limit` updates: it ends at the end of a thread item. Without `limit`, the agent uses 2000.

Answer:

```json
{
"status": "ok",
"updates": [{ "sessionId": "…", "update": { "…": "…" } }],
"cursor": { "…": "…" },
"turnOpen": false
}
```

- `ok`: `updates` holds the `session/update` notifications that the replay of a `session/load` sends after the cursor, with the same converter and the same `_meta` filter.
The updates come in replay order, the order of a load: the frames of a child session come where the load replays them, before the end state of the child.
The `disconnected` states of the subagents that have not ended come last, in each last answer, also when no item changed.
- With `more: true`, more updates follow: the client asks again with the new `cursor`. The pages keep the replay order, and together they give what one answer with no `limit` gives. Only the last answer has `turnOpen`.
- `turnOpen` tells that the newest turn of the root thread can still run: it has no end time (`completedAt`), and its status is not `completed` or `failed`.
An `interrupted` turn with no end time counts as open, see the known gaps.
- `unchanged`: the newest item and the newest turn of the root thread, and the newest item of each subagent in `c`, are the same as at the read of the whole history that gave the cursor.
The answer has the same `cursor`, no updates, and the `turnOpen` of the newest turn now, also in its `cursor`.
A turn can end with no end time and no new item, so `turnOpen` does not come from the cursor.
- A child session that the cursor does not name gets its `subagent_spawned` update before its first update, also when the spawn comes before the cursor of its parent.
This happens when the turn of a queued interaction with an ended subagent starts: the root thread gets no new item, and the next session of the subagent starts.
The spawn also comes before a state of such a session. The answer gives each state of it that comes before the cursor of its parent, such as the `disconnected` state that a subagent gives its own subagent.
The new cursor names each child session whose spawn the client has, so no answer gives a spawn twice.
- `rewritten`: the cursor names an item that the history no longer holds in its session, or it is not a cursor of this version.
This happens after `thread/revert`. The answer has no updates and no cursor. The client reads again with no cursor and replaces the transcript.
Only a replay that started after the request answers `rewritten`. When an older kept replay lacks the item of the cursor, the agent replays again and answers from the new replay.
- A failed read fails the whole tail with `-32603` and `data` `{"reason": "history_read_failed", "retryable": true, "sessionId": "…", "threadIds": ["…"]}`.
This covers each read of the root thread, of a subagent thread, and of `model/list`. `threadIds` names the subagent threads whose read failed.
A failed `model/list` adds `"models": true`: the model reports of the subagents need the models.
An exit of the Codex process adds `cause`: the code, the message and the data of the exit error, such as `restartable` and `retryAfterMs`.
A `cause` with `restartable` `false` tells the client that a later try does not help.
A root thread that Codex does not have is not such a failure: the tail answers `-32002`.
The error has no updates and no cursor. The client keeps its cursor and asks again later. The agent keeps no replay of a failed read.
A load does not fail: it leaves out the history of such a subagent and logs the failure.
- The answer has no `held`. Codex tells the holder of the writer lock only when `thread/resume` fails, and the tail does not call it.
The refusal of `session/load` for such a thread carries `data.held` as an empty object, next to `data.reason` `thread_active_writer`.
The holder is not known, so the object has no `entrypoint` and no `status`.
- The tail sends no `session/update` and no load replay end.

The cursor is opaque to the client:

```json
{ "v": 1, "s": { "<session id>": [12, "<id of the last item>"] }, "end": "<newest item id>", "turn": "<newest turn id>@<completedAt>", "c": { "<subagent thread id>": "<last item id>" }, "turnOpen": false }
```

- `s` names, for each session of the replay, the count of its updates and the last thread item of the session that the client has. `#0` names the updates before the first item, such as the title.
An update belongs to the current item of its own session, also when it comes while the replay of a child session runs.
An item that sends no update, such as a reasoning item with no text, also has its place in the session.
`""` names a child session whose `subagent_spawned` update the client has, but none of its updates. An answer and a load replay end give it.
A legacy thread names its items `item-<n>` and counts them again after a rollback.
So there `s` names an item by its id and a rolling hash of the items of the session up to it: `item-7:<hash>`.
A rollback that leaves an item with the same id and content then gives another name, and the answer is `rewritten`.
- `end`, `turn` and `c` are the signature of a read of the whole history. A load replay end has no signature, so the first tail after a load reads the whole history.
`c` holds each subagent that had not ended, and each ended subagent with a queued interaction that started no turn yet.
On a legacy thread, `end` names an item by its id and a hash of its content: Codex appends to the last reasoning item in place.
Such an append makes the next tail `rewritten`. A newest turn with no item yet takes the newest item of an earlier turn.
On a legacy thread, `c` names the last item of the subagent and a rolling hash of all the items of its newest turn.
So a rollback and a new run of the subagent that end with the same item still change `c`.
- `p` names the kept replay of a page with `more`. `q` holds where the page ended in that replay and the positions of the first page.
So the next page goes on in the same replay, and the pages together give what one answer with no `limit` gives.
A page goes on only in the replay that `p` names. When the agent no longer keeps that replay, the page answers `rewritten`, and the client reads the whole history again.
This happens when the replay is idle for 5 minutes, when four other replays push it out, or when another read of the same session and `cwd` replaces it.
The agent pushes out a replay in the middle of a paged read only after the other replays.
- The agent ignores the `h` field of a cursor of an older build.

A thread item keeps its id when the thread grows, so the cursor of a load stays valid.
`unchanged` costs one `thread/read`, one `thread/turns/list` and one `thread/items/list` with `limit: 1`, and one more `thread/items/list` for each subagent that had not ended.
A legacy thread has no `thread/items/list`: its newest item comes from `thread/turns/list` with the items of the newest turn, and its history from one `thread/read` with `includeTurns`.
Any other read replays the whole history again and cuts it after the cursor: the converter of a load has state across the items, such as the open tool calls.
The agent keeps the replay of each read, so a page does not replay the whole history again.
The pages of one read come from the same replay, also when the thread changes between two pages.
A read after a cursor uses the kept replay while its signature holds. A read with no cursor always replays.
The agent keeps one replay for each session and `cwd`, four at most, and drops a replay that no read uses for 5 minutes.
Reads of one session and `cwd` that overlap share one replay. A read joins only a replay that reads the newest turn after the start of the read.
At most two replays run at one time, and the others wait.
A cancelled read stops to wait, and a waiting replay that no read waits for does not start.
A replay that ends after its reads were cancelled is kept for the next read.
Each read of Codex in a tail fails after 60 seconds with no answer, so a hung read does not block the other tails.
A subagent thread that Codex does not have yet has no items, and it does not fail the tail.
Codex tells this only with the text of an `invalid_request` that names the thread: `thread <id> is not materialized yet`, `no rollout found for thread id <id>`, or `thread not found: <id>`.
The agent accepts this text only at the first read of the first run of a subagent, and at the first page of the root thread.
Any other failed read of a subagent thread fails the tail with the retryable error above.
A thread with no rollout has `""` as its newest item, as in `c`. A failed read of the newest item of a subagent in `c` matches no item, also not `""`.
So the tail replays, and the replay gives the error.
The agent drops the kept replays when the connection closes.

Known gaps:

- Another process sees the running turn of the holder as `interrupted` with no `completedAt`, because Codex marks an in-progress turn of a thread that it has not loaded as interrupted.
So `turnOpen` is `true` while the turn runs. It also stays `true` after the holder crashed in the turn, until a new turn starts.
- Codex persists an item only when it completes. A running command or message of the holder is not in the tail until it ends.
- The title comes only in a read with no cursor. A tail after a cursor does not carry a title change: the session list and `session_info_update` of a live session carry it.
- `unchanged` does not see a change of a subagent of a subagent.
## Presentation hints

The adapter sends these keys only to AIR:
Expand Down
3 changes: 3 additions & 0 deletions src/AcpAgentRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "./AcpExtensions";
import {ASYNC_TASK_STOP_METHOD} from "./async-tasks/AsyncTaskExtension";
import {SESSION_REWIND_METHOD} from "./SessionRewind";
import {HISTORY_TAIL_METHOD, historyTailParamsParser} from "./HistoryTail";
import {CODEX_HOOKS_LIST_METHOD, CODEX_HOOKS_TRUST_METHOD} from "./CodexHookTrust";
import {
SESSION_ARCHIVE_METHOD,
Expand Down Expand Up @@ -194,6 +195,7 @@ function createRouter(
setups.clear();
stopSetupPublication();
agent.sessionIndex.dispose();
agent.disposeHistoryTails();
if (codexAcpServer === agent) {
codexAcpServer = null;
}
Expand Down Expand Up @@ -236,6 +238,7 @@ function createRouter(
.onRequest(SESSION_STEERING_METHOD, sessionSteerParamsParser, (ctx) => getAgent(ctx.params).extMethod(SESSION_STEERING_METHOD, ctx.params))
.onRequest(ASYNC_TASK_STOP_METHOD, asyncTaskStopParamsParser, (ctx) => getAgent(ctx.params).extMethod(ASYNC_TASK_STOP_METHOD, ctx.params))
.onRequest(SESSION_REWIND_METHOD, sessionRewindParamsParser, (ctx) => getAgent(ctx.params).extMethod(SESSION_REWIND_METHOD, ctx.params))
.onRequest(HISTORY_TAIL_METHOD, historyTailParamsParser, (ctx) => getAgentForPaths(ctx.params).historyTail(ctx.params, ctx.signal))
.onRequest(CODEX_HOOKS_LIST_METHOD, hooksListParamsParser, (ctx) => getAgent().listHooks(ctx.params.cwd))
.onRequest(CODEX_HOOKS_TRUST_METHOD, hooksTrustParamsParser, (ctx) => getAgent().trustHooks(ctx.params.cwd, ctx.params.hooks))
.onRequest(GOAL_CONTROL_METHOD, goalControlParamsParser, (ctx) => getAgent(ctx.params).extMethod(GOAL_CONTROL_METHOD, ctx.params))
Expand Down
14 changes: 14 additions & 0 deletions src/AirExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ export const AIR_CODEX_HOOKS_KEY = "codexHooks";
export const AIR_CONTENT_DELTA_KEY = "contentDelta";
export const AIR_SUBAGENT_KEY = "subagent";
export const AIR_SESSION_REWIND_KEY = "sessionRewind";
/**
* The client wants to know when the history replay of a loaded session is whole. After the
* last replayed update, the agent sends one `session_info_update` with
* `_meta.jetbrains.air.loadReplayEnd: true`. The client declares it; the agent does not
* advertise it.
*/
export const AIR_LOAD_REPLAY_END_KEY = "loadReplayEnd";
/**
* The agent answers `_session/history_tail` and advertises it. A client that declares it gets
* the cursor of a load in the load replay end, under {@link AIR_HISTORY_CURSOR_KEY}.
*/
export const AIR_HISTORY_TAIL_KEY = "historyTail";
/** The `_meta.jetbrains.air` key of the load replay end that holds the cursor of the replayed history. */
export const AIR_HISTORY_CURSOR_KEY = "historyCursor";
export const AIR_CUSTOM_ANSWER_KEY = "customAnswer";
export const AIR_CUSTOM_ANSWER_FIELDS_KEY = "customAnswerFields";
/**
Expand Down
Loading