Skip to content

feat(cli): b4 threads tail for stream reattachment - #506

Merged
blove merged 32 commits into
mainfrom
blove/ap-threads-tail
Sep 10, 2026
Merged

blove merged 32 commits into
mainfrom
blove/ap-threads-tail

Conversation

@blove

@blove blove commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Adds b4 threads tail <thread-id> to reconnect a terminal to an existing B4.run thread. It prints the durable state and current turn snapshot, follows live output, and exits on completion or detachment. Attaching does not claim a run slot or cancel the producer.

--url selects a server, repeatable --header supplies middleware credentials, and --json emits newline-delimited frames. Plain text remains continuous across snapshot and live token boundaries. The incremental SSE parser handles LF, CRLF, CR, split delimiters, folded data, and numeric retry hints. Malformed frames produce diagnostics without crashing output. Terminal events release the response reader, including when output fails, without waiting for server EOF.

Header, URL, and transport error messages avoid exposing supplied credentials. Unknown or unverified checkpoint history receives a clear authorization diagnostic. Includes CLI documentation, a patch changeset, parser/render/lifecycle regressions, and real-server tests for durable and live attachment and middleware authorization.

Depends on #489. Build, typecheck, lint, docs, and focused CLI tests are checked locally; merge requires the existing CI validation and deployment lanes. No publishing or workflow changes.

blove and others added 19 commits August 24, 2026 09:15
The AG-UI handler ran runRegistry.begin before rechecking the concrete
thread row, so on the create-race path a caller the recheck ultimately
denies held the victim thread's run slot for the width of that recheck.
Because the thread id is client-chosen on this endpoint, a denied caller
could brick a concurrent authorized run on the same thread with a
transient run_in_flight 409.

Move the row authorization (and the implicit create, when the turn makes
one) ahead of the run-slot claim, mirroring the Agent Protocol run
handlers. Adds a deterministic concurrency regression test that blocks a
denied caller's recheck mid-flight and asserts a concurrent authorized
run still claims the slot. Also corrects a stale comment on the static
module manifest's thread-access channel (it is emitted and validated,
not inert).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…urrent main

The 2026-08-09 provisional plan predated PR1/PR B landing; its line anchors
and wire assertions were stale by construction. This re-draft pins every
anchor to the current tree, corrects two stale-spec items (the interrupt
value is already surfaced; the hub belongs beside runRegistry, not
threadRouteMap), and structures the work as durable-path-first so each task
is independently testable.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pull-based fan-out for AP reattachment: open()/publish()/close() on the
producer side, attach() handing each subscriber a point-in-time digest
snapshot plus a live tail via next(). Coalesces consecutive chunk frames
and per-callId subagent.message frames, drops the digest whole on byte
overflow, isolates per-subscriber queue overflow, and guards producers
against writing into a replaced/closed entry (mirrors the run-registry
identity guard).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Add the GET mirror of POST /threads/:id/runs/stream so a disconnected
client can reattach to a thread: one `event: state` snapshot frame
(status, values, parked interrupts) followed by an immediate
`done{output:null}` + jittered retry hint on the durable path (no live
turn in this process). Also wires the live-path branch against the
already-landed LiveTurnHub, though nothing calls hub.open yet, so every
attach in this PR is durable.

Gates identically to GET /pending_interrupts: thread lookup, thread-access
read gate (denial byte-identical to a genuine 404), route-identity
resolution, then middleware with method:"GET". Adds the ThreadOperation
member `thread.attach` to @dawn-ai/sdk so policies can distinguish it.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…oducers

Wires hub.open/publish/close into handleApStreamRequest, handleResumeRequest,
and the AG-UI handler, so GET /threads/:id/runs/stream actually tails a
running turn instead of always falling back to the durable path. Each
producer opens a live turn right after claiming the run slot (anchored on
the latest checkpoint before its own route stream runs), publishes every
non-terminal chunk, and closes with the same terminal chunk the primary
client saw, unconditionally, in its finally — regardless of done, error,
park, or cancel. The AG-UI handler taps the raw StreamChunk stream before
AG-UI translation so an attacher sees AP-vocabulary frames. handler.close()
now drains the hub too, fanning a terminal frame to any hanging viewer.

Also completes handleApAttachRequest's live-path `values` read (left null
by the prior PR): it now reads the checkpoint at the live turn's anchor,
which races nothing since old checkpoints are immutable.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The attach-endpoint documentation added to
apps/web/content/docs/dev-server/agent-protocol.mdx changed that page's
git-derived last-modified date, which the SEO manifest freshness gate
(added to main after this branch was cut) checks against the checked-in
manifest. Regenerated so the gate passes.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…-PR2 main

The 2026-08-09 provisional plan predated PR2, so its wire assertions were
unverified and its docs task duplicated what PR2 shipped. This re-draft pins
every frame the attach endpoint actually emits, drops the new-docs-page task
(PR2 documented the wire contract in agent-protocol.mdx; a new page costs a
hardcoded mirror list in check-docs.mjs), drops the examples/research digest
gate as redundant with unit coverage, and fixes the three blockers the
verification pass found: the attach-before-create race, the changeset gate
ordering, and the docs gate reading built dist.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
renderSnapshot renders values.messages, applies input only when the
frame is not a resume turn, and feeds turn[] through the same
renderFrame used for live tail frames so a snapshot followed by the
live tail concatenates cleanly by construction.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
consumeAttachStream reads the SSE body with getReader()/TextDecoder in
streaming mode, renders state/done/detached/live frames (or emits raw
JSON per frame in json mode), and classifies how the stream ended --
done, detached with reason, or truncated -- plus any retry hint, so
the command layer can map that onto an exit code.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Adds packages/cli/test/threads-tail-command.test.ts covering the
durable path, live tail with a real blocking-tool barrier, an unknown
thread id, and middleware header gating, all against a real bound
startRuntimeServer instance.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
`dawn threads tail` branched on a top-level `code`, but the runtime's error
envelope carries it at `error.details.code` (as DETAILS, which every thread
endpoint passes) or `error.code`. The lookup therefore never matched, so the
409 `thread_route_unknown` branch was unreachable and a thread that had never
run reported the generic failure with the raw JSON envelope pasted into the
message. Reads the code from both documented positions with own-property
lookups, and surfaces the server's own sentence instead of the envelope.

Covered by a new integration case that asserts the friendly wording and that
no raw envelope leaks into the message — it fails against the previous code.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Separate commit on purpose: the manifest records each docs page's git commit
time, so regenerating it and then amending the docs commit rewrites the very
timestamp the manifest just captured. Regenerate AFTER the docs commit is
final, and land the manifest on its own.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@blove
blove temporarily deployed to vercel-preview August 27, 2026 05:34 — with GitHub Actions Inactive
@vercel

vercel Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
b4-run Ready Ready Preview Sep 10, 2026 7:18am UTC
dawnai Ready Ready Preview Sep 10, 2026 7:18am UTC

Request Review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated approval: this PR received an intelligent (AI) code review. See the review comments on this PR.

Point-in-time snapshot of the two open PRs plus the durable parts: the CI
conditions this work hit (the SEO lastmod gate and its amend-order trap, the
repo-wide review outage, the sandbox fork-bomb flake), the decisions on
record that should not be relitigated, the three bugs that green tests
missed, and the open follow-ups.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Base automatically changed from blove/agent-protocol-thread-auth-94c180 to main September 10, 2026 07:15
@blove blove changed the title feat(cli): dawn threads tail — reattach to a running turn from the terminal feat(cli): b4 threads tail for stream reattachment Sep 10, 2026
@blove
blove enabled auto-merge (squash) September 10, 2026 07:34
@blove
blove merged commit 111d45c into main Sep 10, 2026
52 of 57 checks passed
@blove
blove deleted the blove/ap-threads-tail branch September 10, 2026 07:36

This branch was successfully deployed

1 active and 1 inactive deployments
Preview — 16d8a269 Deployed Sep 10, 2026 by vercel[bot]
vercel-preview — 16d8a269 Deployed Sep 10, 2026 by blove via vercel-native #1765
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.

1 participant