Skip to content

CONTRACT: fix(ui-server,ui-sdk): wait for a turn's own maintenance before refusing its first reference call (#4079) - #4080

Merged
schlessera merged 7 commits into
mainfrom
fix/4079-reference-maintenance-admission
Nov 16, 2026
Merged

schlessera merged 7 commits into
mainfrom
fix/4079-reference-maintenance-admission

Conversation

@schlessera

@schlessera schlessera commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Closes #4079

What changed

A turn's first reference_source call no longer fails because the turn's own maintenance commands hold the shared maintenance budget. Each reference Git command now waits up to 30 s (REFERENCE_ADMISSION_WAIT_MS) for the budget to free, then refuses as busy with "nothing ran, retry" guidance. That covers resolution as well as fetch and checkout. A raw maintenance-busy error that still reaches the tool is classified busy, never the generic unavailable. An awaited scratch check now waits up to 3 s (SCRATCH_ADMISSION_WAIT_MS, one deadline across its Git queries), so a short reference command no longer leaves .brain/scratch/ read-only for the turn. MaintenanceTempBusyError gains freedWithin(ms, signal), a bounded freed().

Why this shape

The issue offered three options. I took the bounded admission wait:

  • Ordering the sandbox checks before tools are offered does not cover what was observed. Governed turns already resolve their sandbox before referenceTools.begin. The hosted refusal named a command-kind holder, which was the app's boot-time brain scratch prune (its own log says "scratch prune failed"), not the turn's scratch check.
  • A separate allowance for reference Git would split the budget. The scratch check still needs a whole allowance, so it would still be refused.

The wait reuses the existing in-process release wake-up (freed()) and the bounded cross-process recheck, so no new mechanism is involved. Each bound is per command, so no wait is unbounded. Neither wait reserves anything extra.

These keep the old refuse-at-once behaviour on purpose:

  • The synchronous scratch check (prepareWorkerSandbox) cannot wait. Its step refuses as before.
  • Bind-time revalidation of lapsed sources runs during turn admission. It passes admissionWaitMs: 0 and withholds the source, as before, rather than stalling the turn's start behind an index.
  • The scratch wait is short (3 s, not 30 s), so a long holder such as an index leaves scratch read-only after the wait rather than holding every turn's start.

Proof

All of these run against the real maintenance admission ledger, with production limits (one 256 MiB allowance in a 256 MiB budget) on a private domain. In the ui-server file, only the worker launch is scripted. The scratch test uses the real prepareWorkerSandboxAsync and maintenance worker.

Failing first. I reverted only packages/*/src to origin/main (eb3c53a) and ran the new tests:

  • packages/ui-server/tests/reference-maintenance-admission.test.ts: every test fails on the reason the issue quotes. For example, REFERENCE_CALL_SUCCEEDS_ONCE_THE_SCRATCH_CHECK_FINISHES receives {"ok":false,"code":"unavailable","message":"The reference service could not complete this request."}. REFERENCE_CALL_PAST_ITS_BOUND_IS_BUSY_NOT_UNAVAILABLE and RAW_MAINTENANCE_BUSY_IS_A_BUSY_REFUSAL receive code: "unavailable".
  • packages/ui-sdk/tests/maintenance-temp-holders.test.ts: TURN_SCRATCH_CHECK_IS_NOT_REFUSED_BY_A_SHORT_REFERENCE_COMMAND receives ".brain/scratch/ could not be checked (Maintenance temporary storage is busy: ... (held by: git). ...)", the line from the hosted log.

With the fix, the ui-server file passes 4 of 4. Its timing assertion proves the call is admitted within the stated 30 s bound.

Mutations, each restored afterwards:

Mutation Named failing assertion
M1: the transport never waits REFERENCE_CALL_SUCCEEDS_ONCE_THE_SCRATCH_CHECK_FINISHES, REFERENCE_CALL_WAITS_ITS_BOUND_BEFORE_REFUSING
M2: the source drops busy on resolution REFERENCE_CALL_PAST_ITS_BOUND_IS_BUSY_NOT_UNAVAILABLE
M3: the tools drop the raw-busy fallback RAW_MAINTENANCE_BUSY_IS_A_BUSY_REFUSAL
M4: the scratch check never waits TURN_SCRATCH_CHECK_IS_NOT_REFUSED_BY_A_SHORT_REFERENCE_COMMAND
M5: the synchronous scratch check waits SYNC_SCRATCH_CHECK_NEVER_WAITS (5007 ms)
M6: acquire drops busy on fetch REFERENCE_FETCH_PAST_ITS_BOUND_IS_BUSY_NOT_UNAVAILABLE

Environment limit. This host has no unprivileged user namespaces (unshare: write failed /proc/self/uid_map), so no real worker starts here. Locally, the scratch test therefore gets past admission (the busy assertion passes) and then fails TURN_SCRATCH_STAYS_WRITABLE_AFTER_A_SHORT_REFERENCE_COMMAND with "git could not run to confirm .brain/scratch/ is gitignored". Uncontended origin/main gives the same environment-only result. Hosted CI proves the writable outcome.

Neighbouring suites. I ran the reference-source, tools, worktrees, GitHub access, transport, maintenance-temp, worker-sandbox, worker-steps, index-requeue and probe-admission files. 182 pass. All 8 failures also fail on origin/main in this sandbox (real worker boundary).

Build. bun run build is clean, because exports changed. tsc --noEmit is clean.

Hosted integrated cell (AC4): passed. A temporary probe commit (9ac8999, copied from #4072's) made hosted runtime_host also run scripts/prove-reference-pool.ts --integrated on both backends. Heavy feedback run https://github.com/schlessera/brain-kit/actions/runs/43854394276 tested merge commit e19300f, and its runtime_host job (https://github.com/schlessera/brain-kit/actions/runs/43854394276/job/133255130366) logged Integrated reference proof passed (claude, pi)., beside Reference pool proof passed. Its log has no reference tool failed, no Maintenance temporary storage is busy and no read-only this turn. The proof step succeeded. Only the job's receipt-upload step failed, with EACCES on the integrated cell's app-owned transcript-archive directory. That directory comes from the probe, which is reverted in 2407cb4; #4072's branch carries the upload fix separately.

Two test faults that run found, now fixed in f762439:

  • unit (3): the writable-scratch test built its brain under the OS /tmp, which a maintenance worker replaces with its own. The wait worked (TURN_SCRATCH_CHECK_IS_NOT_REFUSED_BY_A_SHORT_REFERENCE_COMMAND passed on hosted), but the worker's git query could not see the brain. The brain now lives in the repository's gitignored tmp/, where the worker sandbox writable test keeps its brain; that test passed in the same run (unit (2)). The assertion now prints the mount's reason.
  • verify: the reference admission test imported MaintenanceTempReservation, which @schlessera/brain-ui-sdk/internal does not export (TS2305). It now uses ReturnType<typeof reserveMaintenanceTemp>. Root tsc --noEmit is clean at 2407cb4.

Third fix, in 62934ee: candidate confirmation at ef20465 still failed unit (3), now with the reason visible: "git could not run to confirm .brain/scratch/ is gitignored". The wait had worked, but the maintenance worker never ran git, because it had no prepared startup view. Every real-worker writable test supplies one through createFixtureMaintenanceHost. The test now uses that host, with its ledger state beside the brain in the repository's gitignored tmp/ and masked from the worker, the same way maintenance-temp-helpers.test.ts does.

With that input lease the worker runs on this host too, so the writable outcome is now proven locally:

  • With the fix: maintenance-temp-holders.test.ts passes 8 of 8, with scratch { writable: true }.
  • On main (packages/ui-sdk/src reverted): it fails TURN_SCRATCH_CHECK_IS_NOT_REFUSED_BY_A_SHORT_REFERENCE_COMMAND with the busy refusal (held by: git).
  • Mutation M4 (the scratch check never waits): it fails on that same assertion.
bun run check:pr --base origin/main
{"version":1,"kind":"brain-check-pr-receipt","head":"62934ee91fa214c81dbfa1aa152be7ad2de2d5bd","base":"origin/main","mode":"focused","startedAt":"2026-11-16T18:54:26.756Z","finishedAt":"2026-11-16T18:55:35.010Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":12516.151867},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":103.92609699999957},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":68.8981010000025},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":21.901950999999826},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":55527.568512}]}

Contract

The busy code is already in the documented reference_source/reference_worktree refusal set. This changes which code one case returns (unavailable becomes busy) and states the new 30 s bound in docs/integration-contract/mcp.md in the same CONTRACT: commit. That makes it additive: no field, shape or tool name moved. ReferenceTransport.resolve gains an optional admissionWaitMs input. It is ui-server internal, not a package export.

Changed guard (proportionate containment)

  • Trigger and coverage. The maintenance-temp admission refuses when another maintenance reservation holds the budget. Now a reference Git command, and an awaited scratch check, wait a bounded time before that refusal. It still does not cover cross-process ordering, only liveness, which the existing recheck covers.
  • What it restricts. One command, as before. A reference request past its bound refuses only that request, as busy. A scratch check past its bound leaves only that turn's scratch area read-only.
  • Restricted mode before refusing. Waiting is now that mode. The scratch side keeps its existing degraded mode (read-only scratch for one turn) once the wait passes.
  • How it clears. Automatically: a release in this process wakes the waiters at once, and a release in another process is found by the bounded recheck. Nothing needs a human.
  • Where the cause is recorded. The busy refusal names its holder kinds in the host log (reference tool refusal (busy): ..., worker scratch area is read-only this turn: ...). The agent gets a retry message.
  • Scope test. a fetch refused past its bound after a successful resolution is busy too shows one request refused without anything published. The other tests show the next request succeeds once the holder finishes.

Collisions

bun scripts/collisions.ts --json --pr 4080 at fix head 2616ed6 (the later commits change only this PR's own two test files and revert the probe):

None is a lock.

Checklist

  • Focused local preflight/behavioral receipts and complete selected hosted proof passed; actual head/base/job checkout evidence is in Proof. (Local receipts at 62934ee; ready-candidate proof runs at confirm.)
  • Acceptance criteria from the issue are all met: AC1–AC3 by the tests above, AC4 by heavy run 43854394276.
  • The PR and completed/advanced issues have verified release milestones under docs/process/github.md.
  • A changeset is included (.changeset/4079-reference-maintenance-admission.md).
  • No personal data anywhere in the diff.
  • The integration contract component is updated in this same commit.
  • Docs under docs/ reflect the change. Citations my line shifts moved are repaired.
  • The changed guard answers the proportionate-containment checklist above.
  • Provider history stays append-only: no provider-facing text changed beyond the refusal's code and message.

Anything left open

  • The temporary probe is reverted (2407cb4); scripts/prove-reference-pool.ts matches main.
  • Not changed, on purpose: the hosted scratch-mask query (scratch-mask-query.ts) and the bridge mask's scratch query still refuse at once when busy. They do not run at a turn's start.

🤖 Generated with Claude Code

…fore refusing its first reference call (#4079)

Every maintenance command reserves the whole default 256 MiB maintenance
budget, and admission refuses rather than waits. A reference Git command
that overlapped the turn's scratch check, or the boot-time scratch prune,
was refused at once, and the tool answered the generic `unavailable` with
no retry guidance. The scratch check refused the other way round left
`.brain/scratch/` read-only for the turn.

- The reference transport waits up to REFERENCE_ADMISSION_WAIT_MS (30 s)
  per Git command for the budget to free, then refuses as `busy`. The
  reference source maps that to its existing `busy` code on resolution and
  on fetch or checkout. The tool classifies any raw maintenance-busy error
  that reaches it as `busy` too, never `unavailable`.
- An awaited scratch check waits up to SCRATCH_ADMISSION_WAIT_MS (3 s) on
  one deadline across its Git queries. The synchronous check and bind-time
  revalidation of lapsed sources still refuse at once.
- MaintenanceTempBusyError gains freedWithin(ms, signal), a bounded freed().

Neither wait adds an allowance; both stay inside the existing budget.
The MCP contract component states the new bound for `busy`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera schlessera added this to the 0.45.0 milestone Nov 16, 2026
@schlessera schlessera added type: fix Something is broken and must behave the way it is documented. area: ui-sdk Wire protocol, runtime schemas, the server and client SDK surfaces. 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 16, 2026
…ce cell (#4079)

Copied from #4072's probe so hosted runtime_host runs the integrated cell. Reverted before the PR is ready.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
schlessera and others added 2 commits November 16, 2026 12:05
…ntenance worker sees it; import an exported type (#4079)

Hosted heavy feedback showed two test faults in this PR, not in the fix:

- The writable-scratch test built its brain under the OS /tmp, which a
  maintenance worker replaces with its own, so the worker's git query could
  not see it and scratch stayed read-only after the wait had worked. The
  brain now lives in the repository's gitignored tmp/, as the worker
  sandbox tests place theirs, and the assertion prints the mount's reason.
- The reference admission test imported MaintenanceTempReservation, which
  the SDK's internal entry does not export (TS2305 in strict typecheck).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… reference cell (#4079)"

The probe ran the integrated cell on hosted runtime_host in heavy feedback run 43854394276: Integrated reference proof passed (claude, pi).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera
schlessera marked this pull request as ready for review November 16, 2026 18:23
…fixture input lease (#4079)

Candidate confirmation showed the writable-scratch test still read-only:
"git could not run to confirm .brain/scratch/ is gitignored". The wait
had worked, but the worker never ran git. It had no prepared startup
view, which every real-worker writable test supplies through
createFixtureMaintenanceHost. The test now uses that host, with its
ledger state beside the brain in the repository's gitignored tmp/ and
masked from the worker, as maintenance-temp-helpers.test.ts does. It
passes locally, where the worker now runs, and still fails on main with
the "(held by: git)" busy refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera
schlessera marked this pull request as draft November 16, 2026 20:04
@schlessera
schlessera marked this pull request as ready for review November 16, 2026 20:04
@schlessera
schlessera merged commit c67d588 into main Nov 16, 2026
49 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ui-sdk Wire protocol, runtime schemas, the server and client SDK surfaces. 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 turn's first reference_source call is refused by the turn's own maintenance admission

1 participant