Skip to content

feat: read the history of a stored session after a cursor with _session/history_tail - #816

Open
ignatov wants to merge 3 commits into
mainfrom
air/history-tail
Open

ignatov wants to merge 3 commits into
mainfrom
air/history-tail

Conversation

@ignatov

@ignatov ignatov commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Summary

This change adds the _session/history_tail request. A client can read the history of a stored session after a cursor. The read takes no writer lock and opens no session.

Why

An AIR client shows sessions that another Codex process writes, such as the Codex CLI in a terminal. A session/load of such a thread fails, because thread/resume takes the writer lock. The client must follow that session read only. The new request reads the thread with thread/read, thread/items/list and thread/turns/list. It never calls thread/resume.

What it adds

  • _session/history_tail with an opaque cursor. The client sends back the cursor as it is.
  • Three answers:
    • ok: the updates after the cursor, in the order of a load replay, and a new cursor.
    • unchanged: no updates. This read costs only a few small reads of the newest item and turn.
    • rewritten: the cursor names an item that the history no longer holds, for example after thread/revert. The client reads the whole history again.
  • turnOpen tells the client that the newest turn of the root thread can still run.
  • Paging: with limit, an answer can have more: true. The client asks again with the new cursor. The pages together give the same updates as one answer.
  • historyCursor on the load replay end update, for a client that declares historyTail. The first tail after a load starts from it.
  • The thread_active_writer refusal of session/load now carries data.held as an empty object. Codex does not name the holder, so the object has no fields.

Failure rule

A failed read of the root thread, of a subagent thread, or of model/list fails the whole tail. The error is -32603 with data.reason history_read_failed and retryable: true. The error has no updates and no cursor. The client keeps its cursor and asks again later. A root thread that Codex does not have answers -32002.

Known limits

  • A subagent thread with no rollout yet counts as empty. Codex tells this only in the text of an invalid_request error, so the agent matches these error texts.
  • A page continues only in the kept replay that its cursor names. When the agent no longer keeps that replay, the page answers rewritten, and the client reads the whole history again.
  • Another process sees the running turn of the holder as interrupted with no end time. So turnOpen stays true while that turn runs.
  • Codex persists an item only when it completes. A running command of the holder is not in the tail until it ends.

Contract

See docs/air-extensions.md, sections "History tail" and "Load replay end".

Tests

  • npm run typecheck: passes.
  • npm test: 171 files passed, 7 skipped. 2851 tests passed, 39 skipped.
  • npm run test:integration: 12 files and 29 tests passed.

…ion/history_tail`

AIR follows sessions that another Codex client writes, such as the Codex CLI.
A `session/load` of such a thread fails: `thread/resume` takes the writer lock.
The new request reads the thread with `thread/read`, `thread/items/list` and
`thread/turns/list` only. It takes no lock and opens no session.

- `CodexAcpClient.readSessionHistory` gives the stored history without
  `thread/resume`, close to the change of PR #815. A legacy thread uses one
  `thread/read`.
- `streamThreadHistory` replays into a target: a state, a connection and a
  sink. A load uses the open session. The tail uses a detached state and its
  own delivery, so the `_meta` filter is the filter of a load.
- The tail replays the whole history into a recorder and cuts it after the
  cursor item of each session, as the Claude adapter does. `unchanged` reads
  only the newest item and turn of the root thread and the newest item of
  each open subagent. `rewritten` comes when the cursor item is gone, as
  after `thread/revert`. `turnOpen` tells that the newest turn has no end.
- A v1 client with `loadReplayEnd` gets one `session_info_update` after the
  load replay. With `historyTail` it carries `historyCursor`.
- The `thread_active_writer` refusal carries an empty `data.held` object, the
  form of the Claude adapter. Codex does not name the holder, and the tail
  cannot tell a held thread without `thread/resume`, so its answer has no
  `held`.

A live test with two app-servers on one temporary Codex home shows that the
second one reads the held thread, and that it sees the running turn of the
first one as interrupted with no `completedAt`. So `turnOpen` is true while
that turn runs.
Copilot AI balanced review requested due to automatic review settings November 29, 2026 06:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved history-tail defects can leave child history stale, cause repeated full-history replays, and prevent clients from recognizing retryable read failures.

3 open issues
What changed in this PR

Adds read-only session history polling for AIR clients, reusing the adapter’s load replay logic without opening a session or taking its writer lock.

Changes:

  • Adds _session/history_tail with opaque cursors, paging, cached replays, and change detection.
  • Adds load replay cursors and empty held metadata to active-writer errors.
  • Documents the contract and updates integration tests and capability snapshots.
File Description
src/​HistoryTail.ts Implements cursors, replay recording, paging, and caching.
src/​CodexThreadErrors.ts Recognizes missing rollouts and adds holder metadata.
src/​CodexAppServerClient.ts Exports the history timeout helper.
src/​CodexAcpServer.ts Integrates detached replay and history-tail requests.
src/​CodexAcpClient.ts Adds read-only history, head, and model reads.
src/​AirExtension.ts Defines history-tail and replay-end metadata keys.
src/​AcpAgentRouter.ts Routes tail requests and disposes cached replays.
src/​__tests__/​scenarios/​scenario-harness.ts Adds the expected history-tail capability.
src/​__tests__/​scenarios/​data/​air/​web-search.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​subagent-activity.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​read-search-list.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​plan-stream.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​plan-review-permission.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​plan-deltas-and-turn-plan.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​network-and-sandbox-approval.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​nested-subagent-session.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​native-subagent-session.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​mcp-url-elicitation.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​mcp-tool.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​mcp-tool-approval.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​mcp-startup-failure.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​mcp-elicitation.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​late-subagent-update.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​image-view.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​image-generation.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​history-replay.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​guardian-review.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​goal-update.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​fuzzy-file-search.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​file-changes.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​file-change-approval.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​dynamic-tool.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​context-compaction.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​command-without-streamed-output.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​command-output-stdin.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​command-failed.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​command-approval.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​collab-controls.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​collab-agent.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​background-terminal.jsonl Updates advertised capability snapshot.
src/​__tests__/​scenarios/​data/​air/​agent-message-and-reasoning.jsonl Updates advertised capability snapshot.
src/​__tests__/​integration/​history-tail.test.ts Tests following a session held by another process.
src/​__tests__/​CodexACPAgent/​thread-active-writer.test.ts Expects empty holder metadata in refusal errors.
src/​__tests__/​CodexACPAgent/​initialize.test.ts Expects history-tail capability advertisement.
src/​__tests__/​CodexACPAgent/​data/​session-index-capabilities.json Updates capability expectations.
README.md Links to the read-only history-tail feature.
docs/​air-extensions.md Documents requests, responses, cursors, and limitations.

Review effort: Balanced

Comment thread src/CodexAcpClient.ts Outdated
Comment on lines +934 to +935
const last = turn?.items.at(-1);
item = last ? itemIdentity(last, true) : "";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b3b2c9a. When the newest turn of a legacy thread has no item yet, the head now pages back through earlier turns to the newest item. turn and turnOpen still come from the newest turn. A new test checks that such a thread answers unchanged. It fails without the fix.

Comment thread src/CodexAcpServer.ts Outdated
} catch (error) {
const refused = storedSessionError(sessionId, error, this.sessionIndex.enabled);
// A thread that Codex does not have is -32002. Each other failed read can pass on a later try.
if (refused instanceof RequestError) throw refused;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b3b2c9a. The tail wrapper now turns the process-exit error (code 1001) into the retryable history_read_failed error. Not-found (-32002), cancellation and tail errors that are already normalized pass through unchanged. A new test covers an exit during a root read. It fails without the fix.

Comment thread src/HistoryTail.ts
Comment on lines +570 to +571
for (const [threadId, item] of Object.entries(signature.children)) {
if ((await head(threadId)).item !== item) return false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b3b2c9a. For a legacy subagent, c now names the last item plus a rolling hash of all the items of its newest turn. The head computes the same value from that turn. The subagent session is exactly that turn. So a rollback and a new run that end with the same item no longer match, and the tail answers rewritten. A new test covers this case. It fails without the fix.

…cess exits

- A legacy thread whose newest turn has no item yet now takes the newest
  item of an earlier turn for its signature. Before, the signature never
  matched, and each tail replayed the whole history.
- The signature of a legacy subagent now names its last item and a rolling
  hash of all the items of its newest turn. A rollback and a new run that
  end with the same item now answer `rewritten`, not `unchanged`.
- An exit of the Codex process during a read now fails the tail with the
  retryable `history_read_failed` error, as other failed reads do.
…use of a process exit

The paging back to the newest item of a legacy thread had no guard for a
repeated cursor, so a bad cursor from Codex made the read loop forever.
It now fails the read, which gives the retryable history error.

When an exit of the Codex process fails a history tail read, the
history_read_failed error now keeps the code, the message and the data of
the exit error in data.cause. The client can see restartable and
retryAfterMs and stop when a later try does not help.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants