Skip to content

Stream an App Server turn's opening message as the start of its reply - #1057

Merged
max-sixty merged 13 commits into
mainfrom
message-flow
Sep 24, 2026
Merged

max-sixty merged 13 commits into
mainfrom
message-flow

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

On the website, a user's question showed nothing in its thread until the agent's work was done. The host published only the App Server turn's final_answer message as the reply, and dropped every commentary message so working narration stayed out of the conversation (#497). In a local scripts/verify_site.py local run, the reply first appeared at 20.8 s, after the page edit had already published at 16.1 s, although the model wrote a message to the user 2 s into the turn.

The turn's first agent message is now the reply's opening, when App Server marks it commentary and only the turn's input and reasoning precede it. It streams into the thread at once. Any other commentary stays private, including narration after a tool call from an agent that skipped the opening. The committed reply is the opening plus the final answer. Only the final answer's completion settles it, so the Stop guard still waits for that, and a turn with no final answer commits nothing. A reconnect mid-turn restores the opening already on screen, and a completed turn's message state is dropped, so an adopted next turn starts clean. The website host and the leaf codex adapter share this fold (AppServerEvents in codex.py).

Measured with the same local run afterwards, the reply first appeared at 6.1 s, the edit published at 13.6 s, and the reply committed at 43.1 s.

The agent learns the rule from three places:

  • the delivered answering.reply clause in registry.json;
  • host-codex-app-server.md "Replies", which the website agent reads in full;
  • ANSWER_ASK_INSTRUCTION in schema.py.

docs/how-it-works.html describes the rule and repeats the delivered clause.

Behavior that moved: test_app_server_events_report_semantic_codex_progress now opens its turn with a commentary message. That opening streams, and the commentary after it still does not. The test also checks:

  • what final_text commits, with and without a final answer;
  • what a reconnect restores;
  • that narration after a tool call is never the opening;
  • that a completed turn leaves no state behind.

The snapshots are regenerated for the changed clause: the handling table, interaction recovery, summary suggestion and hosted delivery.

Gaps:

  • There has been no run on the deployed leaf.page and none through the Cloudflare container path.
  • There has been no live run of the leaf codex terminal adapter.
  • The early reply depends on the model writing an opening before its first tool call. A model that skips it falls back to the old timing.

🤖 Generated with Claude Code

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 13 commits September 23, 2026 10:19
notes/guidance-duplication.md lists the rules Leaf's agent-facing guidance states
in more than one place: SKILL.md, references/, package guidance and registries,
$events handling and answering, agent-facing Python strings, protocol sidecars
and hand-copied docs. It gives every site and a proposed single home for each
rule. Seven rules already disagree between copies, and in nearly every case the
short copy (a CLI help string, a delivered clause, a docs paragraph) is the one
that leaves out a case.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Readers are all author "user" today, so a page cannot record who
moved, commented or voted, or who has it open. The item records that
gap next to how Claude Code Artifacts handle it: a viewer id on each
row, with names, faces and presence resolved from the host.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
On the website, a user's question showed nothing in its thread until the
agent's work was done: the host published only the turn's final answer as
the reply, and dropped every `commentary` message so working narration
would stay out of the conversation (#497). In a local `verify_site.py`
run the reply first appeared at 20.8 s, after the page edit had already
published at 16.1 s, although the model wrote a message to the user 2 s
into the turn.

The turn's first agent message, when App Server marks it commentary and
nothing but the turn's input and reasoning precede it, is now the
reply's opening. It streams into the thread immediately; commentary
after it, or after a tool call when the agent skipped the opening, stays
private. A reconnect mid-turn restores the opening already on screen,
and a completed turn's message state is dropped so an adopted next turn
starts clean. The final answer completes the reply, which commits as the opening and the final answer together. Only the
final answer's completion settles the reply, so the Stop guard still
waits for it, and a turn with no final answer commits nothing. The
website instructions, `host-codex-app-server.md` "Replies", the
delivered `answering.reply` clause and `conversation-loop.md` tell the
agent to write that opening before its first tool call. Both App Server
carriers (the website host and the `leaf codex` adapter) share the fold.

Measured with the same local run afterwards: the reply first appeared at
5.2 s, the edit published at 17.3 s, and the reply committed at 22.0 s.

Behavior that moved: `test_app_server_events_report_semantic_codex_progress`
now opens its turn with a commentary message, which streams, while the
commentary after it still does not, and it checks what `final_text`
commits, what a reconnect restores, narration after a tool call, and a
completed turn's clean state. Snapshots regenerated for the changed clause and website
instructions: the handling table, interaction recovery (reply, version,
receipt), summary suggestion, and hosted delivery (reply, version,
receipt).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	skills/leaf/references/host-codex-app-server.md
#	tests/_regtest_outputs/test_website_server.test_hosted_agent_receives_the_response_instructions_and_delivery[receipt]__0/snapshot.yaml
#	tests/_regtest_outputs/test_website_server.test_hosted_agent_receives_the_response_instructions_and_delivery[reply]__0/snapshot.yaml
#	tests/_regtest_outputs/test_website_server.test_hosted_agent_receives_the_response_instructions_and_delivery[version]__0/snapshot.yaml
#	worker/server.py
# Conflicts:
#	skills/leaf/references/conversation-loop.md
@max-sixty
max-sixty merged commit 57d776f into main Sep 24, 2026
5 checks passed
@max-sixty
max-sixty deleted the message-flow branch September 24, 2026 00:32
max-sixty added a commit that referenced this pull request Sep 24, 2026
The main CI run at `57d776f3` failed two interaction snapshots after the
App Server reply guidance changed. Regenerate the event-case and carrier
snapshots from their existing tests so they record the
opening-plus-final reply wording published by #1057.

Both exact failing tests pass without snapshot reset after regeneration.
The diff changes only the delivered reply clause and its YAML wrapping.

> _This was written by Codex on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Sep 24, 2026
…essage (#1069)

#1057 made an App Server turn's reply its opening and final messages.
`leaf reply`'s refusal for an event those messages answer still said the
event "is bound to this delivery's final message". That named only half
the rule and gave an agent mid-turn nothing to do next. It now says:

> event '<id>' is answered by this turn's messages; finish the reply in
your final message

`host-codex-app-server.md` "Replies" quotes the refusal the same way.
The three hosted-delivery snapshots that embed that reference are
regenerated, and the refusal tests in `test_interact_session.py` and
`test_website_server.py` match the new wording.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Sep 24, 2026
Each delivery now carries only the instructions for the route that
delivers it. Clauses used to state conditions Leaf already settles at
freeze time:

- "Acknowledge … by your host contract's route where it leaves
acknowledgement to you"
- "In a Codex task Leaf observes over App Server, your final message is
the reply instead"
- "If this event's conversation has a null `title`"

In paired `verify_site.py local` runs on 2026-09-23, the leaf.page agent
ignored those hedges. It invented `leaf delivery ack`, `$LEAF ack` and
`leaf acknowledge`, and it posted an early `leaf reply`.

**Clause selection.** A clause's `when` now reads the event's
`conversation`, which is its thread's digest, and the `carrier`
delivering it (`wait`, `queue` or `app-server`), alongside the record
and its `obligation`. Each clause now states one case:

| | acknowledgement | reply |
|---|---|---|
| `leaf wait` | `leaf wait --ack <delivery-id>` | `leaf reply` before
other work |
| Codex queue | none (the adapter acknowledges) | `leaf reply` before
other work |
| Codex App Server | none | the turn's first and final messages |

The title clause appears only when the thread has no title. The Markdown
line is now its own clause, and every carrier gets it. `summary_hint` is
now read from the digest.

**Handling is written at freeze time, not at capture.** It is added by
`freeze_delivery(carrier=…)` and `offer_delivery(…, carrier)`. A Codex
record collects batches before the adapter knows which transport will
offer them. Before this change, a record captured under one transport
and offered over the other after a restart told the agent the wrong
route. In the App Server → queue direction, nothing posted the "final
message" reply.

**Watcher route.** The preface in `codex-watcher.md` now tells the page
task to skip the `leaf wait --ack` clause that a forwarded batch
carries.

**Tests.** Behavior that moved:
- The per-case table
(`test_each_case_of_an_event_is_told_what_the_snapshot_shows`) adds
cases for queue, App Server, and titled and untitled threads.
- The carrier snapshot now freezes the queue route through the adapter's
own path. The three carriers now differ.
- Tests that rebuilt the `when` input now pass `carrier` and
`conversation`.
- The "collecting record without handling" drop test is gone, since
collecting records no longer hold handling.
- I re-recorded the recovery, summary and hosted-delivery snapshots, and
the sample delivery in `docs/how-it-works.html`.

**Evidence.**
- The delivery, Codex, website-server and docs suites pass (1273 tests).
- One local `verify_site.py` run passed before the merge with #1057,
whose opening-message rule the App Server clause now follows. The hosted
agent ran 2 commands and 1 file edit. It invented no acknowledgement
command and posted no early `leaf reply`. That is one run.

**Gaps.**
- There are no paired Claude Code runs yet to check that acknowledgement
stays prompt. That check is in TODO.md.
- An App Server delivery that was already offered keeps its frozen
handling if the adapter restarts on the queue. A delivery never changes
under its id.
- `ANSWER_ASK_INSTRUCTION`, used for Stop-hook and idle refusals, still
says "in a Codex task Leaf observes over App Server…".

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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