Skip to content

Waits: a unit wait on a unit systemd does not know is refused when asked (#852) - #1377

Merged
scuttletoby merged 2 commits into
mainfrom
fork/unit-wait-never-started
Nov 29, 2026
Merged

scuttletoby merged 2 commits into
mainfrom
fork/unit-wait-never-started

Conversation

@scuttletoby

Copy link
Copy Markdown
Owner

Part of #852 (the second point of the 11-22 unit wait comment).

What

A unit wait on a unit systemd does not know (LoadState=not-found) counted as ended. So a parent that asked for its child's unit before the child started it was woken at once, every time it asked: each wake a paid turn that re-reads a large context, and no real wait.

  • At registration such a unit is now refused with invalid and the way out: "systemd does not know the unit X: it has not started yet, or it ended and was forgotten. Ask again once it runs, or with unit.missing "ended" if it is known to have run". Nothing is stored.
  • unit.missing: "ended" (new, optional; default refuse) keeps the old answer for a unit known to have run and been forgotten: met at once.
  • A stored wait (one asked while systemd knew the unit) still ends when systemd forgets it, as a transient unit's end. Unchanged.
  • The judge tells registration from a stored read by context.waitId === null, which only the registration path passes. A permanent judge failure may now carry its refusal code; without one the old mapping stays (not_supported for a unit wait without systemd, invalid otherwise).
  • wait_for's description and docs/fork/README.md (server.waitFor) say so.

State question

  • Old records: a wait stored before this change has no missing field and is read as a stored wait, so it ends when its unit is forgotten, as before. The default refuse is never written into the stored spec, so asking the same wait again after the upgrade gives the same key and finds the stored one (tested: missing: "refuse" and no missing validate to the same target).
  • Restart: nothing new is stored; the rule depends only on whether the read is the registration read.
  • A registration read that answers late (after the wait was stored and the run adopted): its refusal ends the stored wait as check_error with the same text, never as ended. It fails closed.
  • A unit that appears only after registration: refused at registration; the caller asks again once it runs. A "wait until it appears, then until it ends" mode is not in this PR.

Left in #852

  • A signal wait (child sends a named signal, the parent's wait is met only then).
  • A parent hears when a run that a wait started in its child ends.
  • Optional: a unit.missing: "wait" mode (wait for the unit to appear, then end), if wanted.
  • Retiring the consumer project's wake service.

Tests

Red on main, green here:

  • apps/server/src/waits/WaitJudges.test.ts: at registration an unknown unit is a permanent failure with code invalid; with missing: "ended" it is met; known units at registration are judged as before.
  • apps/server/src/waits/WaitService.checks.test.ts: the real unit judge over a fake systemctl that answers not-found: register is refused with invalid and stores nothing; with missing: "ended" it is met and stores nothing; a stored wait on the same unit ends ended.
  • packages/shared/src/waitFor.test.ts: missing: "ended" is kept in the spec; missing: "refuse" gives the same target as no missing.

Run locally: those three files plus apps/server/src/waits/, apps/server/src/mcp/toolkits/waits/ and the contracts wait tests (17 files, 339 passed, 1 skipped). vp run check:changed (fmt, lint, typecheck) is clean, and so are the fork schema, upstream-files and seam checks.

🤖 Generated with Claude Code

scuttletoby and others added 2 commits November 29, 2026 17:51
…ked (#852)

A unit systemd does not know counted as ended, so a wait asked before
the unit started (a parent waiting on its child's unit) was met at once,
every time it was asked: each one a paid turn and no real wait.

At registration such a unit is now refused (`invalid`, with the way
out). `unit.missing: "ended"` keeps the old answer for a unit known to
have run and been forgotten. A stored wait still ends when systemd
forgets its unit, and the default is left out of the stored spec, so
older waits keep their key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The wait_for description now says a unit systemd does not know is
refused, and its input gains unit.missing, so the fork's MCP tool list
grows from 4873 to 5025 bytes. Set forkAgentContextBudget.json to the
measured value, as ForkAgentContextBudget.test.ts asks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@scuttletoby

Copy link
Copy Markdown
Owner Author

lens: code · reviewer: claude (code lens, round 1) · read at: a18fd33

blocker: none
major: none
minor: a unit wait whose registration read never runs is stored without being judged, and the first sweep ends it as ended at once, which is the behaviour this PR removes. startJudgeWhenFree returns null when the unit pool (2 judges) stays busy past REGISTER_JUDGE_LIMIT_MS or is stuck. The wait is then stored, and the sweep judges it with waitId set, so the new refusal is skipped. Reproduced (a test I did not commit, shown below). This needs two slow systemctl show reads at once, so it is rare, and it gives the pre-PR result, so nothing gets worse than main. Fix: refuse a unit wait with busy or try-again when no registration read could start, or mark such a record so its first stored read counts as the registration read. The tool text and docs say "is refused", which is not true on this path.
minor: missing: "ended" is part of the key, so asking the same unit with and without it gives two separate waits. That is fine as designed and only worth a word in the docs if anyone ends up relying on dedupe across the two forms.

Reproduction (unit judge that sleeps 10 s for slow-* names and is otherwise the real judge over a fake systemctl answering not-found):

register slow-a.service, slow-b.service (both pool slots taken, stored after 3 s)
register child-job.service  -> {"state":"waiting","waitId":"w_7sqpllly5h",...}
judgeDue                    -> {"judged":3,"ended":1}
child-job.service stored    -> "delivering", outcome "ended", "systemd does not know this unit (never started, or ended and forgotten)."

Checked and fine:

  • Key compatibility: the default refuse is never written. validateTarget spreads missing only when it is "ended", and canonicalJson drops undefined values, so a new request with no missing or with missing: "refuse" hashes to the same key as an old stored record. findOrRefuse runs before the judge, so asking again finds the stored wait even when systemd has since forgotten the unit. The asker gets deduped and no refusal.
  • Old records decode: missing is optionalKey in WaitUnitSpec, and an old record has no missing, so it reads as a stored wait and still ends when its unit is forgotten. The PR's checks test covers this through judgeDue.
  • The refusal stores nothing: it returns before the store lock, and ensuring stops the run that was not adopted. The test asserts that the store file is absent.
  • A registration read that answers late, after the wait was adopted, carries the registration context, so its permanent failure ends the stored wait as check_error and not as ended. It fails closed.
  • judgement.code ?? old mapping: only the unit judge sets code, so every other kind keeps its old refusal codes.
  • The tool description, the docs/fork/README.md paragraph and the forkAgentContextBudget.json bump (152 bytes, matching the longer description) agree with each other, apart from the minor above.

State question: run again, so the same request after an upgrade has the same key and finds its stored wait. Stops half-way, so a refusal stores nothing, and a late answer fails closed as check_error. Old records have no missing and end as before when their unit is forgotten. Empty state means a fresh store, with the refusal as tested. The exception is the busy-pool path in the minor above, which stores an unjudged wait that then ends at once.

🤖 Generated with Claude Code

@scuttletoby scuttletoby added the fork:merge-ready Merge conveyor: lenses done, merge when green (PRs) label Nov 29, 2026
@scuttletoby
scuttletoby merged commit 2f21620 into main Nov 29, 2026
35 checks passed
@scuttletoby
scuttletoby deleted the fork/unit-wait-never-started branch November 29, 2026 20:12
scuttletoby added a commit that referenced this pull request Nov 29, 2026
…s busy (#1407)

When both unit check places stayed busy past the registration limit, a
unit wait was stored without systemd being asked, and its first sweep
ended an unknown unit as `ended` at once: the answer #1377 removed. Such
a wait is now refused (`too_many`, try again) and nothing is stored.
With `unit.missing: "ended"` an unknown unit ends anyway, so that wait
is still stored for the sweep.

`missing` stays in the key only as "ended", so older waits keep their
key; the tool text and docs now say the two forms are separate waits.
The fork tool list grows from 5025 to 5063 bytes.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork:merge-ready Merge conveyor: lenses done, merge when green (PRs)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant