Repository navigation
Close the hosted turn when its App Server stream goes silent or its follower faults - #729
Conversation
leaf-agent
left a comment
There was a problem hiding this comment.
The universal terminal guard is the right repair, and the silence bound routes a stalled subscription into the recovery a dropped one already takes. One consequence of the broadened guard is worth closing before it lands.
A fault outside the four previously named types used to propagate out of _follow_turn and reach the thread's excepthook, which printed a traceback to container stderr. It is now caught, and its only record is detail = str(error) or type(error).__name__ — which keeps the type only when the message is empty. So a KeyError in the projection loop, the exact fault the second half of this change exists to catch, is recorded as App Server turn stream failed: 'params': no type, no location. turn_stream_completed logs status and no error field at all, unlike the turn_delivery_unbound branch beside it, which already carries error=type(error).__name__.
That matters because it is the evidence this PR names as still outstanding: separating a stalled stream from a follower fault on the next occurrence needs turn_stream_completed and container stderr, and after this change neither names the fault. I am pushing the repair — name the type in detail, carry it on turn_stream_completed, and pin it in the follower-fault test.
Verified separately: all three new cases fail on the merge base ee83b181 and pass at this head, with the follower-fault case failing on base by KeyError escaping _follow_turn — the mechanism the change describes. uv run pytest tests/test_website_server.py is green here (72 passed).
The broadened guard stops a fault outside the four named types from reaching the thread excepthook, which took the traceback with it. Carry the exception type in the turn's failure detail and on turn_stream_completed, so separating a stalled stream from a follower fault stays readable from the log the next occurrence leaves.
publish-sitefailed onmainat 0309bd0 in run 34941155345. The deploy itself succeeded and the release pass verified leaf.page in Chrome; the job then failed ten and a half minutes later on the hosted-agent pass withagent published but did not reply; the page read working. This is the third run with that shape (#694, #705), and the commit under test touches onlyskills/leaf/scripts/leaf/render_gate/andexporting.py, neither of which can reach the Worker or the container.The fault
The container ran the agent to a publication at 8.7 s and then projected nothing for the rest of the gate's
TURN_LIMIT. The page kept readingworking: Starting— the detail_follow_turnsets before its first notification — while the container answered every one of the gate's two-second state reads. Agent alive, follower projecting nothing, claim open, no receipt._follow_turnconverts a stream that fails into a terminal turn: its handler synthesizes afailedstatus, clears the stream activity, commits the reply stream and closes the claim. #705 identified two exits that skipped it, both producing exactly that page state:socket.recv(timeout=1)raisesTimeoutErrorevery idle second, and that becamecontinuewith nothing bounding the repetition. Reconnection was reachable only from a socket error, so the recovery_resume_turn_streamalready implements was never tried.assert delivery_id is not Noneand theAppServerReplyStreamconstruction sat ahead of thetryentirely, and aKeyErrororAttributeErrorinside the projection loop propagated out through_run_follow_turn, whosefinallyonly discards the thread fromfollowing_threads.The change
Both halves restore the same contract —
worker/README.md's "a failed, interrupted, or completed-but-unanswered provider turn closes its active claim turn without inventing a reply" — and neither substitutes for the other: the guard cannot close a turn whose follower never returns, and the bound cannot close one whose follower raised.recv_notificationreturns nothing while the stream is merely quiet and lets theTimeoutErrorthrough once the silence outlastsSTREAM_SILENCE.TimeoutErroris anOSError, so that routes a stalled subscription into the recovery a dropped one already takes, unchanged:thread/resumereads the authoritative turn, which carries the terminal status the stream never sent. A resume the turn did not need costs one round trip and reconciles the same way a real drop does, so the bound does not have to be tight — 120 s is past any gap between items a running turn produces, and well inside the ten minutes the reader spent watching a dead page.failedterminal the stream-failure path produces: activity cleared, claim closed, no invented reply, reader obligation still open.The consequence is wider than this gate. Any leaf.page reader whose turn landed here saw a page that read working indefinitely, with neither an answer nor a failure.
Verification
Three cases in
tests/test_website_server.py, each failing onmainand passing here: the silence bound distinguishes a quiet stream from a stalled one; a socket that only ever times out recovers its completed turn and commits the reply through the resume path; and a follower fault outside the named types closes the claim, leaves the activity no longer reading working, and leaves the obligation unanswered.uv run pytest tests/test_website_server.pyis green (72 passed). The everyday suite is green apart from four failures this sandbox owns, which reproduce unchanged on the merge base — two on a read-only/tmp, one on the sandbox umask, one on the Node bundler.What no local run can prove is which of the two mechanisms produced the three observed stalls; separating them needs
turn_stream_completedand container stderr from Workers Observability, and this account has no Cloudflare credentials. That is why both are repaired: each is reachable, and each alone leaves the contract broken.Fixes #705