Skip to content

Stream Codex replies into Leaf conversations - #476

Merged
max-sixty merged 6 commits into
mainfrom
codex/stream-codex-replies
Sep 9, 2026
Merged

max-sixty merged 6 commits into
mainfrom
codex/stream-codex-replies

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 9, 2026 •

Copy link
Copy Markdown
Owner

Leaf comments currently reach Codex through a durable queue, but the reader cannot see the response until the agent explicitly writes back to Leaf. This adds an experimental App Server transport that starts an addressed Codex turn for single-conversation feedback, streams its public response into that Leaf thread, and commits the completed text as one durable reply.

The App Server client binds each Leaf delivery to an exact Codex task and turn, keeps activity separate from response text, handles reconnect and turn-start races, and falls back to the existing queue for mixed or unsupported deliveries. The browser projects the in-progress response as an ordinary thread message and preserves incomplete replies as labelled drafts rather than settling the reader's input.

leaf codex launch now owns a private App Server and its interactive CLI client, while leaf codex start <page> connects the page adapter to that server. The integration remains experimental. The operating contract and future directions are documented in skills/leaf/references/host-codex.md and notes/codex-integration.md.

The complete CLI-to-App-Server-to-Leaf-browser path was smoke-tested with two consecutive Leaf-started turns, incremental rendering, durable completion, browser reload, server restart, and page re-vendoring. The repository suite passes with 953 tests.

This was written by Codex on behalf of max-sixty

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The streaming path works end-to-end and the projection/browser split reads right. Two things I'd want settled before it lands; the first is inline.

A slow turn/start response re-sends the same feedback into the same turn. start_delivery waits START_TIMEOUT for the socket thread's answer, while _send restarts its own START_TIMEOUT on every intervening notification — so a turn/start whose response lags behind turn/started and the first deltas (the ordering notes/codex-integration.md says the buffering machinery exists for) times out the caller while the socket thread goes on to bind the turn normally. _recover_delivery then propagates that RuntimeError with the queue record still offering, so the adapter's retry loop re-offers it and calls start_delivery again: a second turn/start that App Server steers into the same active turn with an identical leaf_feedback output, _bind appending the delivery id twice, and no receipt for the batch in between. The queue fallback survives the same retry because host-codex.md tells the model to treat a repeated page-and-sequence pair as a retry — the direct path has no equivalent statement, and its duplicate arrives as a tool result rather than a pointer the model chooses to read. Either the caller's wait has to outlive the socket thread's, or the direct path needs that retry rule stated where the model reads it.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The addressable: false projection is the right shape for the first finding — one field, read by the shared reaction-surface renderer, so a consumer that needs an event id opts out at the point it reads rather than each surface learning about the draft. I ran the two suites it touches: tests/test_render_conversations.py -k codex (3 passed, including the new no-strip-while-streaming / strip-after-commit pair) and tests/test_interact_session.py -k "app_server or codex" (43 passed).

The turn/start fix in the same commit trades the duplicate for a hang I can reproduce; that one's inline.

Comment thread skills/leaf/scripts/leaf/codex.py Resolved

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

c16d0ae closes the in-flight half of the hang: I confirmed the new test fails at cd152740 with _queue.Empty and passes here, and a socket error now tears the connection down and reconnects rather than continuing the loop on a socket that just failed. The other half is still open, and it's inline — an exception type _run doesn't enumerate exits the socket thread with available still set, and every later start_delivery blocks forever.

Comment thread skills/leaf/scripts/leaf/codex.py Resolved
leaf-agent
leaf-agent previously approved these changes Sep 9, 2026
@leaf-agent
leaf-agent dismissed their stale review September 9, 2026 04:20

CI failed — test_leaf_started_codex_turn_streams_into_its_thread_and_commits is red on fb5b16b.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

test is red on fb5b16b5 — test_leaf_started_codex_turn_streams_into_its_thread_and_commits fails with assert 'codex-stream:leaf-turn' == '<reply id>'. I'm dismissing my approval on that; the fix is inline and I'm happy to push it.

The failure is the test reading the middle of _commit_stream_reply's transaction, not the observer change in fb5b16b5. That function appends the reply and clears the draft under one PageTransaction, and append_event writes the log line where clear_stream_reply writes status.json afterwards. The poll waits on events_model.read_events, which takes no lease, so it can return the instant the reply lands and before the draft is gone; page_state in tests/interact_support.py then builds full_state off an unlocked read and projects both, with the draft last. A reader never sees this — PageStateService.page_state keeps a PageTransaction across both files — so the window is the test's alone, and the poll needs to name the settled state rather than the one the page passes through.

Waiting on the draft's removal as well as the reply closes it, since the clear is the same transaction's last write. Delaying PageTransaction.clear_stream_reply by 300ms reproduces CI's assertion exactly on this head, and the inline version passes under that same delay; tests/test_interact_session.py -k "app_server or codex" stays at 45 passed.

The unlocked read in page_state is the more general version of this — every test that polls the log and then asserts on the projection can observe a tear the server cannot. That helper is outside this PR's diff, so it's a separate change if you want it.

Separately, fb5b16b5 itself does what the last thread asked: the new test fails at c16d0ae with the socket thread dead and available still set, and passes here.

Comment thread tests/test_interact_session.py Resolved
@max-sixty
max-sixty merged commit c3c1c25 into main Sep 9, 2026
10 checks passed
@max-sixty
max-sixty deleted the codex/stream-codex-replies branch September 9, 2026 04:45
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.

2 participants