Skip to content

CONTRACT: fix(ui-server): bound a UI sync's launch and record its outcome in Activity (#4257) - #4258

Merged
schlessera merged 10 commits into
mainfrom
fix/4257-bounded-sync-launch
Nov 20, 2026
Merged

schlessera merged 10 commits into
mainfrom
fix/4257-bounded-sync-launch

Conversation

@schlessera

@schlessera schlessera commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Closes #4257

What changes

POST /api/brain/sync (packages/ui-server/src/routes/brain.ts):

  • Bounded start. The CLI probe, the writer gate, boundary.launch and, on a hosted install, the mirror lock, startup view and origin probe share one 120 s bound and one abort signal. launch reports the real start through the existing onExecute hook. For a mirror sync (runMirrorSync) that happens after the origin probe, just before the first worker. Past the bound the stream ends with { type: "done", success: false, reason: "launch_timeout" } and the admission entry is cleared. A launch that returned but never started, or that settles later, has its process killed.
  • Output grace after exit. Once the process has exited, the brain is released when every unfinished output pipe has sat open with nothing to read for 10 s, counted from the exit at the earliest. Time spent delivering output to a slow reader never counts, and the cancel is never awaited, because the host boundary's own cancel drains to EOF. A descendant that holds stdout open no longer keeps the brain reserved. This is the other way the observed case could stick: the container showed no sync process.
  • 409 names the hold. syncingRoots is now a Map of root to admission time. The 409 body is { error, heldMs } and the message gives the hold in seconds.
  • Activity run and log line. Each admitted UI sync is an Activity root span (kind and origin cron, jobName: "ui-sync", attributed to the requesting principal). Its terminal outcome carries the exit status and the last 5 non-empty output lines, and its job_output event holds the combined output tail. A failure also logs ui sync failed on the brain logger. Recording fails open.

Contract: additive (heldMs, reason: "launch_timeout", the ui-sync run). It is documented in docs/http-api.md, and the commit is prefixed CONTRACT:. The changeset is a ui-server minor.

Out of scope, as the issue says: why the observed launch stalled, and the tier A refusal itself (#4256).

Validation

  • packages/ui-server/tests/sync-launch-bound.test.ts (new) covers a launch that never settles, a late launch that gets killed, a hosted launch that returns but never starts its first worker, a held output pipe, the 409 heldMs, and a reader-disconnected failure recorded in Activity and the log.
  • tests/sync-mirror.test.ts adds a native test: the real hosted sync launch reports its start once, after launch returned and before the worker ran.
  • tests/host-version-requirements.test.ts now also asserts, through createApp with the Odysseus fixture corpus as the brain, that a refused UI sync writes the ui-sync Activity run and the ui sync failed log line. This proves the app wiring.
  • Failing first at base bd751d434, with the new test file against the unchanged route, the four original cases failed on their named assertions: STALLED_STREAM_ENDS, the late-kill done frame missing, heldMs undefined, and UI_SYNC_RUN_RECORDED null.
  • Hosted mutation, app wiring. Commit 5867f40ae removed activity: activity.store from app.ts. Heavy feedback run 44736962153 failed exactly APP_UI_SYNC_RUN (unit (2) and determinism (1)), and nothing else in those categories failed. The next commit, 12829ec10, restored it.
  • Local mutations, each restored afterwards:
    • Removing the late kill fails LATE_PROCESS_KILLED.
    • Removing the output grace fails HELD_PIPE_STREAM_ENDS.
    • Not waiting for the reported start fails PRESTART_STREAM_ENDS.
    • Not killing an unstarted launch at the bound fails PRESTART_KILLED.
    • Awaiting the output cancel (a boundary stream whose cancel drains to EOF) fails HELD_PIPE_STREAM_ENDS.
    • Dropping clearTimeout once the sync has started fails STARTED_SYNC_NOT_KILLED.
    • A wall-clock grace after exit fails SLOW_READER_GETS_FINAL_LINE.
    • Idle time counted from the start of the read fails SILENT_SYNC_FINAL_LINE_KEPT.
    • Native: the start report must come at least 1.5 s before a 2 s worker exits (START_REPORTED_BEFORE_WORKER_RUNS).
  • bun run typecheck passes.
  • Local gap: the native tests (sync-reservation, sync-mirror, host-version-requirements) need user namespaces, which this host lacks. They fail here on unchanged main too, so hosted heavy feedback and CI are their proof.
  • Review: fresh-context reviews are recorded on this PR, with their findings answered.

Collisions

bun scripts/collisions.ts --json --pr 4258 at head a111ec0bddeb536b42ba27e50650cbfab6c1706c: closure-only overlaps with #3056, #3059, #3069, #3581, #4151, #4234 and #4262, through shared affected test closures. No path collisions.

Refreshed by merging origin/main (d88c227c6). Main touched none of this PR's files, so the check:pr receipt below is from the last real change, 0686c841f.

{"version":1,"kind":"brain-check-pr-receipt","head":"0686c841fce4fd0abfe23faf3e2063860f5d508c","base":"origin/main","mode":"focused","startedAt":"2026-11-20T14:10:53.922Z","finishedAt":"2026-11-20T14:11:39.303Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":13021.033517999998},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":137.09244000000035},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":78.80242899999939},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":19.794215000001714},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":32106.876057999998}]}

🤖 Generated with Claude Code

schlessera and others added 2 commits November 20, 2026 12:28
…come in Activity (#4257)

A probe or launch that never settled held the brain's admission entry until
restart, so every later Sync got 409 with nothing running. The start is now
bounded (refused as `launch_timeout`, a late launch is killed), output pipes
held open past the process's exit get a short grace, the 409 names how long
the hold has lasted (`heldMs`), and each UI sync is an Activity run with its
exit status and last error lines plus a `ui sync failed` log line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera schlessera added type: fix Something is broken and must behave the way it is documented. area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. contract Touches the integration contract: CONTRACT: commit, contract doc updated in the same commit. labels Nov 20, 2026

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head c7af79a against base bd751d4. Reviewer: agent. 1 finding: 0 fixed, 0 filed, 0 rejected, 1 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "c7af79a0068a296bb0332cc76cbd5d8f5cd9cff9",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/tests/host-version-requirements.test.ts:122-133: issue #4257 Verification wants an integration test through the fixture corpus, each new test shown failing first. This app-level ui-sync run/log assertion uses an empty temp brain, not packages/core/fixtures/corpus/, and per the PR body was never run locally (no native worker boundary): no failing-first receipt, never seen passing, so the app wiring is unproven."
    }
  ]
}

schlessera and others added 2 commits November 20, 2026 12:34
…ivity wiring to show the app test fails (#4257)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ord the contract entry (#4257)

Restores the wiring the previous commit removed as a mutation (its hosted run
failed APP_UI_SYNC_RUN as expected), and adds the authoritative contract
entry for the additive sync fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head c7af79a against base bd751d4. Reviewer: agent. 1 finding: 1 fixed, 0 filed, 0 rejected, 0 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "c7af79a0068a296bb0332cc76cbd5d8f5cd9cff9",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/tests/host-version-requirements.test.ts:122-133: issue #4257 Verification wants an integration test through the fixture corpus, each new test shown failing first. This app-level ui-sync run/log assertion uses an empty temp brain, not packages/core/fixtures/corpus/, and per the PR body was never run locally (no native worker boundary): no failing-first receipt, never seen passing, so the app wiring is unproven.",
      "disposition": "fixed",
      "commit": "5867f40ae3d34b59c3dce7eb4a0e101f478ec6e1"
    }
  ]
}

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 12829ec against base bd751d4. Reviewer: agent. 1 finding: 0 fixed, 0 filed, 0 rejected, 1 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "12829ec10820712ad72773913f99616e610ee290",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/src/routes/brain.ts:233-259: the 120 s bound only covers boundary.launch, but on a hosted install launch (host-boundary.ts:676-725) returns as soon as runMirrorSync starts. Its mirror lock, native startup view and origin probe (sync-mirror.ts:151-166) stay unbounded, so if one never settles proc.exited never resolves and the brain stays held with a 409 until restart, the #4257 case."
    }
  ]
}

…starts (#4257)

A hosted sync's launch returns before its mirror lock, native startup view
and origin probe, so the launch bound ended too early. The boundary now
reports the start through onExecute (after the origin probe for a mirror
sync), the route bounds until that start, and a returned launch that never
starts is killed at the bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 12829ec against base bd751d4. Reviewer: agent. 1 finding: 1 fixed, 0 filed, 0 rejected, 0 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "12829ec10820712ad72773913f99616e610ee290",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/src/routes/brain.ts:233-259: the 120 s bound only covers boundary.launch, but on a hosted install launch (host-boundary.ts:676-725) returns as soon as runMirrorSync starts. Its mirror lock, native startup view and origin probe (sync-mirror.ts:151-166) stay unbounded, so if one never settles proc.exited never resolves and the brain stays held with a 409 until restart, the #4257 case.",
      "disposition": "fixed",
      "commit": "01b630b4dac07fafb48f3038485b8b5b27920cda"
    }
  ]
}

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 01b630b against base bd751d4. Reviewer: agent. 1 finding: 0 fixed, 0 filed, 0 rejected, 1 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "01b630b4dac07fafb48f3038485b8b5b27920cda",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/src/routes/brain.ts:313: the 10 s output grace does not release a non-hosted UI sync. `await reader.cancel()` reaches credentialOutput's cancel() (host-boundary.ts:333-337), which reads on to EOF; sync's writer-gate hold wraps stdout that way (host-boundary.ts:342-347). A descendant holding stdout keeps the 409. The held-pipe test's fake stream cancels at once, so it cannot fail."
    }
  ]
}

…ts output cancel (#4257)

The host boundary's admission-retaining stream drains to EOF on cancel, so
awaiting it held the reservation while a descendant kept the pipe open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 01b630b against base bd751d4. Reviewer: agent. 1 finding: 1 fixed, 0 filed, 0 rejected, 0 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "01b630b4dac07fafb48f3038485b8b5b27920cda",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/src/routes/brain.ts:313: the 10 s output grace does not release a non-hosted UI sync. `await reader.cancel()` reaches credentialOutput's cancel() (host-boundary.ts:333-337), which reads on to EOF; sync's writer-gate hold wraps stdout that way (host-boundary.ts:342-347). A descendant holding stdout keeps the 409. The held-pipe test's fake stream cancels at once, so it cannot fail.",
      "disposition": "fixed",
      "commit": "d6cced97aacdb1906283ac11aa85c9d8cb008ef7"
    }
  ]
}

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head d6cced9 against base bd751d4. Reviewer: agent. No findings.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "d6cced97aacdb1906283ac11aa85c9d8cb008ef7",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "no-findings",
  "findings": []
}

…es nothing (#4257)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 7a8cc53 against base bd751d4. Reviewer: agent. 3 findings: 0 fixed, 0 filed, 0 rejected, 3 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "7a8cc533a7b8153a678ce83860f4f6eabacf1e0b",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/tests/sync-mirror.test.ts:406-422: START_REPORTED_ONCE_AFTER_LAUNCH only checks that onExecute fires after launch returns, not before the worker runs. Moving input.onExecute (sync-mirror.ts:168) to the end of executeMirrorSync still passes, yet then the route's 120 s bound covers the whole hosted sync and kills any sync longer than that. The placement the bound depends on is unpinned."
    },
    {
      "summary": "packages/ui-server/tests/sync-launch-bound.test.ts: no case has a sync that reports onExecute and then runs past syncLaunchTimeoutMs. routes/brain.ts:23 promises a started sync is never bounded, but dropping clearTimeout(timer) at routes/brain.ts:273, which would SIGTERM every sync running longer than 120 s, fails no assertion: each bounded case's process exits at once."
    },
    {
      "summary": "packages/ui-server/src/routes/brain.ts:309-316: the 10 s grace bounds draining, not a held pipe. A hosted sync enqueues all output only at exit and closes its stream (host-boundary.ts:728-729), so a connected reader that drains slower than 10 s has the rest discarded by reader.cancel(): lost progress lines, and the Activity reason and log line (built from send()) miss the final error lines #4257 wants recorded."
    }
  ]
}

…d idle; pin the start report (#4257)

A slow reader draining a hosted sync's buffered output lost its last lines
when the grace ran on wall-clock time after exit. The grace now counts only
time every unfinished pipe spends blocked on a read. Tests now pin that a
started sync outlives the launch bound and that the hosted start is reported
before the worker runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 7a8cc53 against base bd751d4. Reviewer: agent. 3 findings: 3 fixed, 0 filed, 0 rejected, 0 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "7a8cc533a7b8153a678ce83860f4f6eabacf1e0b",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/tests/sync-mirror.test.ts:406-422: START_REPORTED_ONCE_AFTER_LAUNCH only checks that onExecute fires after launch returns, not before the worker runs. Moving input.onExecute (sync-mirror.ts:168) to the end of executeMirrorSync still passes, yet then the route's 120 s bound covers the whole hosted sync and kills any sync longer than that. The placement the bound depends on is unpinned.",
      "disposition": "fixed",
      "commit": "f0bcdc5db569528cf6462f1935d896729d7830eb"
    },
    {
      "summary": "packages/ui-server/tests/sync-launch-bound.test.ts: no case has a sync that reports onExecute and then runs past syncLaunchTimeoutMs. routes/brain.ts:23 promises a started sync is never bounded, but dropping clearTimeout(timer) at routes/brain.ts:273, which would SIGTERM every sync running longer than 120 s, fails no assertion: each bounded case's process exits at once.",
      "disposition": "fixed",
      "commit": "f0bcdc5db569528cf6462f1935d896729d7830eb"
    },
    {
      "summary": "packages/ui-server/src/routes/brain.ts:309-316: the 10 s grace bounds draining, not a held pipe. A hosted sync enqueues all output only at exit and closes its stream (host-boundary.ts:728-729), so a connected reader that drains slower than 10 s has the rest discarded by reader.cancel(): lost progress lines, and the Activity reason and log line (built from send()) miss the final error lines #4257 wants recorded.",
      "disposition": "fixed",
      "commit": "f0bcdc5db569528cf6462f1935d896729d7830eb"
    }
  ]
}

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head f0bcdc5 against base bd751d4. Reviewer: agent. 1 finding: 0 fixed, 0 filed, 0 rejected, 1 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "f0bcdc5db569528cf6462f1935d896729d7830eb",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/src/routes/brain.ts:326-328: the output grace counts idle time from when the pending read began (drainLines sets waiting at :81), not from exit. A sync silent for 10 s or more before it writes its final lines and exits has a read idle since before exit, so if exit is seen before that last chunk is read, check() finishes at once and cancels the readers: the error lines are dropped from the stream, Activity reason and log line."
    }
  ]
}

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head f0bcdc5 against base bd751d4. Reviewer: agent. 1 finding: 1 fixed, 0 filed, 0 rejected, 0 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "f0bcdc5db569528cf6462f1935d896729d7830eb",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/src/routes/brain.ts:326-328: the output grace counts idle time from when the pending read began (drainLines sets waiting at :81), not from exit. A sync silent for 10 s or more before it writes its final lines and exits has a read idle since before exit, so if exit is seen before that last chunk is read, check() finishes at once and cancels the readers: the error lines are dropped from the stream, Activity reason and log line.",
      "disposition": "fixed",
      "commit": "0686c841fce4fd0abfe23faf3e2063860f5d508c"
    }
  ]
}

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 0686c84 against base bd751d4. Reviewer: agent. No findings.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4258,
  "head": "0686c841fce4fd0abfe23faf3e2063860f5d508c",
  "base": "bd751d43435bbffa919d987323d17098eb5f7166",
  "reviewer": "agent",
  "outcome": "no-findings",
  "findings": []
}

@schlessera schlessera added this to the 0.45.0 milestone Nov 20, 2026
@schlessera
schlessera marked this pull request as ready for review November 20, 2026 14:18
@schlessera
schlessera merged commit cb42362 into main Nov 20, 2026
61 of 63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. contract Touches the integration contract: CONTRACT: commit, contract doc updated in the same commit. type: fix Something is broken and must behave the way it is documented.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ui-server: a UI sync whose launch never settles holds "already running" until restart, and its failure is never recorded

1 participant