Skip to content

feat(control-plane,sbx,mocactl): hosts remove a retired build's worker and snapshot - #785

Merged
webchang merged 56 commits into
rossoctl:mainfrom
webchang:pr/i20-removal
Nov 14, 2026
Merged

webchang merged 56 commits into
rossoctl:mainfrom
webchang:pr/i20-removal

Conversation

@webchang

@webchang webchang commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Closes #743. Stacked on #781 (retire): this branch contains #781's commits, and its own change is the last 4 commits (review range d7d994ed..0ce477a5). Once #781 merges, only those remain.

What it does: after a build is retired, each host that holds it removes the build's worker and snapshot, and reports that it did. Four signed-off commits:

  1. The control plane:
    • the wanted state gains remove: retired builds that a host holds with no live sandbox. Each also stays in stop;
    • a host's removed report is accepted for retired builds only. It writes a retire record by host:<id> with event: removed and the buildRecord;
    • on removed, heldBy drops that host at once;
    • openapi 1.19.0.
  2. The host agent: removal matches the host's own template record by build id and worker. It skips a running worker, runs install-host.sh --uninstall, removes the snapshot directory and its record, and reports removed. SBX_INSTALL_HOST is set in both units.
  3. mocactl: retire --wait also waits for heldBy to empty.
  4. A fix: a snapshot name must start with an alphanumeric character, so no hidden entry, . or .. under snapshots/ can be named.

Limits:

  • --wait against a control plane older than 1.19.0 with host agents would wait until its timeout; the two ship together.
  • The snapshot is deleted before removed is reported. So a failed report leaves no host record, and heldBy empties at the host's next inventory.
  • The relay token is revoked by the operator's setup.sh run.
  • Workspace data stays for the janitor.
  • A host that's down removes at its next poll.
  • Two builds of one template from the same image share a snapshot directory. The older one's heldBy empties at the host's next inventory, without a removed record.

Coordination: #786 also takes openapi 1.19.0 (both stack on #781), so whichever merges later renumbers.

Tests on the tip:

  • typecheck passes, and so does the control-plane suite;
  • mocactl passes, run serially;
  • host-agent.test.sh passes on bash 5 and 3.2, and setup and overlay pass;
  • install-host.test.sh passes on bash 5. On bash 3.2 it stops at the janitor case, as on the parent branch;
  • shellcheck is clean, and prettier passes;
  • deploy-scripts runs close to its 5-minute limit with the new shell suites; a canceled run with every test passing clears on rerun.

14 files, +384 / -88, counted over the review range.

Assisted-By: Claude Code

…he API

Template lifecycle spec §5.1. Nothing withdrew a template or a build: every
build stayed usable for ever, and retiring meant editing the env list and
redeploying, which left no trace of what the template was.

- POST /v1/templates/{id}/retire {reason?} and DELETE
  /v1/templates/{id}/builds/{build} (the owner, or an admin): every build
  not yet retired, or the one named by build id or handle, becomes
  `retired` and its activation leases go. Each writes a `retire` record
  naming the caller, `from` (the status before), `buildRecord` (the digest
  of the build's `build` record, null for one never built) and the reason;
  earlier records stay. The answer lists each build with its id, readiness
  and the record's digest; an already retired build is listed with
  `record: null` and no second record.
- Never cuts off running work: refused `409 build_in_use` while a live
  sandbox runs on a build it names. The in-use check is one function
  (buildsInUse), where sessions pinned to a build join the live sandboxes
  once sessions name builds.
- Use is refused as gone: a sandbox create or an activation naming a
  retired build, a tag of one, or a template whose every build is retired,
  answers `410 template_retired`.
- Readiness reads `retired`; each build lists `heldBy`, the hosts whose
  last inventory holds its snapshot (null without host agents).
- The wanted state lists a retired build a host holds, with no live
  sandbox, in `stop`, and the worker report takes `detached` for it.
  Removing the worker and the snapshot from the host is not part of this
  change.
- openapi 1.8.0 (the two routes, RetiredBuild, the codes, `heldBy`, the
  readiness value), its changelog, the HTTP API reference and the
  lifecycle spec's §5.1.

Refs rossoctl#749

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
The client part of retire (template lifecycle spec §5.1).

- `mocactl sbx templates retire ID [--build B] [--reason TEXT] [--wait
  [--timeout S]]` retires the template (every build of it) or build B,
  and prints each build retired, saying when one was already retired.
  --reason is kept in the record of a whole template's retire; with
  --build (a DELETE, which takes no body) it is refused as a usage error.
- --wait then reads each build until it is `retired` and no host holds
  its snapshot (`heldBy` empty; null, or absent from an older control
  plane, means no host agents), saying once who still holds it. Past
  --timeout (default 300 s) it exits 5, as `activate --wait` does.
- Any sbx command refused `410 template_retired` says that a retired
  template or build is never served again, and where to look instead.

SandboxApiClient gains retireTemplate, SbxBuild `heldBy` and `handle`,
and the contract test checks both routes against the OpenAPI document.

Refs rossoctl#749

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
…'s removal

`templates retire --wait` waited for `heldBy` to empty, which only the
host's removal step does, so on a deployment with host agents it always
ran to its timeout. It now waits for what this change does deliver: the
build reads `retired` and its worker no longer runs.

- control-plane: a retired build whose worker still holds a presence
  record reports `capacity` {running, inUse}, as an active one does;
  null once its host has stopped it. openapi's TemplateBuild.capacity
  and the 1.8.0 changelog say so; a test covers both cases.
- mocactl: `retire --wait` is done when each build is `retired` with
  `capacity` null, or at once without host agents (`heldBy` null). While
  waiting it says once per change that the worker still runs, with its
  sandboxes in use; the timeout names the worker's stop. The usage text,
  the spec's §5.1 and the HTTP API reference say that waiting for the
  removal from every host comes with rossoctl#743.
- The spec's §5.1 known limits add that `build_in_use` is judged from a
  read before the catalog update, so a create racing the retire can land
  on the build; the wanted state never stops a worker with a live
  sandbox, so that sandbox runs. A follow-up note: retiring the build
  `default` points to could move `default` to the newest registered one.

Refs rossoctl#749

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
…opped worker

With the relay's presence unreadable, a retired build's capacity read as
null, which retire --wait takes as "its worker stopped". Such a build now
reports `capacity: {running: null, inUse}`, so a null capacity means only
"read, and not attached", and --wait keeps waiting on it, saying the
worker's state cannot be read.

Tests: the control plane reports running null with presence unreadable;
--wait waits it out and says so once.

Refs rossoctl#749

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
…report

Template lifecycle spec §6.1 step 7: a retired build is removed from the
hosts that hold it once no live sandbox runs on its worker.

- GET /v1/hosts/{host}/wanted gains `remove`: the retired builds the host
  holds with no live sandbox on their worker. Each stays in `stop` too, so
  the agent stops a running worker before it removes it, in the same run.
  A worker with a live sandbox is in no list.
- POST /v1/hosts/{host}/workers/report takes `removed` for a retired build
  the host holds: a `retire` record by `host:<id>` with `event: removed`
  and `buildRecord`, the digest of the build's `build` record. The host no
  longer holds the build, so its `heldBy` drops the host at once rather
  than at the next inventory. `removed` for a build that is not retired,
  or that the host does not hold, is 404 `not_found`.
- openapi.yaml 1.9.0 (`remove`, the `removed` event, `heldBy`); the
  changelog's 1.9.0 entry.

Refs rossoctl#743

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
host-agent.sh's removal(), after the wanted state's `stop` in the same run,
for each build the control plane lists in `remove` (template lifecycle spec
§6.1 step 7):

- finds the snapshot from this host's own template record whose manifest
  hash is the build id and whose worker is the one named, never from a
  path the control plane sends; a name no record matches, or a malformed
  one, deletes nothing and is an error line;
- skips a worker whose unit still runs (removed once it is stopped);
- runs `install-host.sh --uninstall <worker>` when the worker is installed
  (SBX_INSTALL_HOST; it keeps the workspace data for the janitor), then
  deletes the snapshot directory and its record, and reports `removed`.

A control plane that sends no `remove` (before API 1.9.0) is taken as
before. The summary line counts `removed`. Both agent units set
SBX_INSTALL_HOST to the checkout under /srv/sbx/src, as they set
SBX_TEMPLATE_BUILD: installed in /usr/local/libexec, the agent cannot find
install-host.sh from its own path. The RUNBOOK's host agent section says
what a retire now does on the host.

Refs rossoctl#743

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
`templates retire --wait` returned once each build's worker had stopped;
removing the build from its hosts was still to come. With the host
agent's removal step it now also waits for `heldBy` to empty: each
holding host has removed the worker and snapshot and reported it.

- While it waits it says once per change that the worker still runs,
  or that it has stopped and which hosts still hold the build. The done
  line says the worker stopped and was removed from every host; without
  host agents (`heldBy` null) it is done at once, as before. A timeout
  names both the stop and the removal.
- The usage text, the doc comment, the lifecycle spec (§5.1, §6.1 step 7
  and its removal paragraph, §6.3's wanted state, §7.1's `retire` row)
  and the HTTP API reference no longer say that removal is still to
  come. The spec's known limits say that a host whose agent does not
  poll removes the build at its next poll, and that the workspace data
  stays for the janitor.

Refs rossoctl#743

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
…ding dot

A template record's snapshot names one entry under snapshots/; it now
must start with a letter or digit, so a record can never name a hidden
entry there (nor . or ..).

Refs rossoctl#743

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 12, 2026

@clawgenti clawgenti left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

This PR is stacked on #781, and the review covers only its own 4 commits (d7d994e..0ce477a). The deletion path holds up: the snapshot name comes only from this host's own template record, matched on both build id and worker. Worker and build ids are checked against regexes, the name has to be one plain entry starting with a letter or digit, rm -rf uses ${SBX_SRV:?} and --, a running worker is skipped, and an unmatched or malformed request deletes nothing. The other three findings are about failure handling. The uninstall's failure reason is lost because its log sits in the trap-deleted $WORK. With errexit off inside removal(), a failed rm -rf still drops the record and reports removed, which leaves an orphan snapshot. And the last commit's leading-dot guard has no test. The control-plane remove/removed handling, the mocactl --wait change and the openapi/docs updates match the PR body and are covered by tests.

Author: webchang (MEMBER — maintainer)
Areas reviewed: commit conventions/DCO, PR format, shell (host-agent.sh removal path handling and deletion safety, systemd units), control-plane host handlers, mocactl retire --wait, tests, docs (RUNBOOK, openapi 1.9.0, http-api, spec), agent-config gate
Agent/IDE config (.claude/.vscode): none
Commits: 4 commits, all signed-off: yes
CI status: passing (all 17 checks green, including shellcheck, deploy-scripts and deploy-scripts-bash32)

0 must-fix, 3 suggestions, 1 nit.

Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

Comment thread deploy/sbx/host-agent.sh Outdated
continue
fi
if [[ -f "$SBX_ENV_DIR/workers/$w.env" ]]; then
"$SBX_INSTALL_HOST" --uninstall "$w" >"$WORK/uninstall-$w.log" 2>&1 || { error "install-host.sh --uninstall $w failed"; continue; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: The uninstall's output goes to $WORK/uninstall-$w.log, and main()'s EXIT trap deletes $WORK (line 350), so the journal only ever shows install-host.sh --uninstall <w> failed and never the reason. This matters most for a misconfigured path. SBX_INSTALL_HOST is not checked in check_settings (only SBX_TEMPLATE_BUILD is, line 94). On a host whose units predate this PR, the default resolves from /usr/local/libexec to /usr/deploy/sbx/install-host.sh, which does not exist. Every 15 s run would then log the same bare error, the build would never be removed, and retire --wait would time out. Suggested fix: check [[ -x "$SBX_INSTALL_HOST" ]] || { error "no install-host.sh at $SBX_INSTALL_HOST"; continue; } before the call, and on failure put the log's last lines in the error line (e.g. error "install-host.sh --uninstall $w failed: $(tail -n 3 "$WORK/uninstall-$w.log" | tr '\n' ' ')"), or send the log to stderr so it reaches the journal.

Comment thread deploy/sbx/host-agent.sh Outdated
if [[ -f "$SBX_ENV_DIR/workers/$w.env" ]]; then
"$SBX_INSTALL_HOST" --uninstall "$w" >"$WORK/uninstall-$w.log" 2>&1 || { error "install-host.sh --uninstall $w failed"; continue; }
fi
rm -rf -- "${SBX_SRV:?}/snapshots/$snap"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: removal is called as the right-hand side of || (line 298), so set -e is off for its whole body. If rm -rf -- "${SBX_SRV:?}/snapshots/$snap" fails (read-only or busy mount, immutable attribute, EIO), the loop still deletes the record (line 332) and reports removed. The snapshot directory then stays on disk with no record, so neither a later removal nor the inventory ever finds it again: disk leaks silently while provenance says it was removed. Suggested fix: rm -rf -- "${SBX_SRV:?}/snapshots/$snap" || { error "could not delete snapshot $snap"; continue; }, so the record (and with it the retry on the next poll) is kept until the snapshot is really gone.

Comment thread deploy/sbx/host-agent.sh
[[ -n "$rec" ]] || { error "no template record here for build $b of worker $w"; continue; }
snap="$(jq -r .snapshot "$rec")"
# One plain entry under snapshots/: no slash, and no leading dot (no ., .. or hidden entry).
[[ "$snap" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] || { error 'a template record names a bad snapshot'; continue; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: This guard is the last safety check before rm -rf (commit 0ce477a tightened it to refuse a leading dot), but test 13 in deploy/sbx/tests/host-agent.test.sh never reaches it. Its only malformed case is a bad build id, which line 313 refuses first. Please add a case: a record whose manifest hash and worker match, but whose snapshot is .., .hidden, a/b or empty. Assert that a template record names a bad snapshot is printed, that $SBX_SRV/snapshots and the record are still there, and that no workers/report is sent.

nit: jq -r .snapshot prints the string null for a record with no snapshot key, and null passes this regex, so such a record would rm -rf snapshots/null. Use jq -r '.snapshot // empty' (or jq -er '.snapshot | strings') so a missing or non-string field is refused.

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

The range adds remove to the host wanted state (retired builds a host holds with no live sandbox), a removed worker report that writes a host retire record and drops the host from heldBy, the host-agent removal step (uninstall worker, rm -rf the snapshot named by the host's own template record, delete the record, report), and mocactl retire --wait waiting for heldBy to empty. The host-side path handling holds up: the snapshot name comes only from a local record matched on manifest hash and worker, it is regex-checked (no slash, no leading dot), and the rm -rf has no symlink-follow risk. One correctness gap remains: the control plane asks for removal by buildId/worker without checking whether a live (non-retired) build shares the same snapshot, so the documented 'rebuild is the way back' path can have its snapshot and worker deleted.

Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript, Shell, Docs/OpenAPI
Agent/IDE config (.claude/.vscode): none
Commits: all signed-off: yes
CI status: 16 pass, deploy-scripts-bash32 pending

Inline: 1 must-fix, 1 suggestion, 1 nit. The head 523e218 is a merge-only cascade of #757 up the stack (empty remerge-diff, no new non-merge commits); the review covers this PR's own range d7d994ed..0ce477a5, as the body asks.

AI-assisted review, approved by a maintainer before posting.

// A retired build has no lease and is never placed: once its worker is idle it is stopped,
// then removed from the host with its snapshot. Never one with a live sandbox.
if (b.status === 'retired') {
if (b.worker && !busy.has(b.worker)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

must-fix: remove is keyed only on this build being retired. It does not check whether another build that is not retired shares the same buildId (manifest hash) or worker, and that case is reachable. REUSABLE leaves out retired, so after a retire, POST /v1/templates/{id}/builds with the same image and spec (the documented "a new build is the way back") queues a new build. template-build.sh names the snapshot/worker <id>-<12 hex of the image digest>, so it answers "already built" when the snapshot is still there, and the build registers with the same buildId and worker as the retired one. This happens whenever the host's --once poll claims the rebuild before removal runs, e.g. a host coming back up, since build_one runs before activation in the same run. The retired entry then stays in stop+remove. The agent stops the worker, uninstalls it and rm -rfs the snapshot the registered build needs, and the removed report deletes holds[buildId], so the live build loses heldBy too. (Before this PR the same overlap only made the worker flap between run and stop; now it destroys the snapshot.) Suggested fix: skip a retired build in both stop and remove when any non-retired build in the catalog has the same buildId or worker. Optionally also refuse removed for such a build in hostWorkerReport. Please add a test with a retired build and a registered one that share a hash.

Comment thread deploy/sbx/host-agent.sh
say "{\"agent\":\"skipped\",\"worker\":\"$w\",\"why\":\"still running: removed once it is stopped\"}"
continue
fi
if [[ -f "$SBX_ENV_DIR/workers/$w.env" ]]; then

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: install-host.sh --uninstall was an operator command until now. Calling it automatically has a host-wide side effect when the retired worker is the host's last SBX0 worker: uninstall_worker -> remove_shared_units -> remove_egress removes the shared units ([email protected], sbx.slice), moca-egress, and the egress credential drop-in/token (credential injection removed with the last worker; re-run install-host.sh --egress ...). So any template owner retiring their template can now remove a host's egress credential setup with no operator involved, and the next worker an operator installs comes up without injection until --egress is re-run. Either pass an option to uninstall only the worker's own files when called from the agent, or at least document this in the RUNBOOK's removal paragraph and surface it in the agent's output. Today it goes only to $WORK/uninstall-$w.log, which is deleted on exit.

Comment thread deploy/sbx/tests/host-agent.test.sh Outdated
printf '{"run":[],"stop":[]}' >"$TMP/state/wanted-answer"
wo || fail 'a wanted state without remove failed'
grep -q '"removed":0,"errors":0' "$TMP/out" || fail 'a wanted state without remove (an older control plane) is not taken'
pass 'removes a retired build only when named and matched by its own record, never a running worker, and reports it'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: Commit 4 tightens the snapshot-name check, but no case exercises it. Add a record whose snapshot is .., .hidden, a/b or empty, and assert that nothing under $SBX_SRV is deleted and nothing is reported. Add one case where the mocked install-host exits non-zero, and assert that the snapshot and record stay and no removed report is sent. Those two branches are the safety-critical ones in removal().

@clawgenti clawgenti left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

Re-review of the push since my last review (0ce477a..523e218, review 5883319041). This PR's own commits are unchanged: git range-diff shows every earlier commit as equal. The push only merges the parent branches again ("Merge pr/i26-retire into pr/i20-removal", and the merges below it), and so brings in #757's five new fixes (381d7c4 too-large command closes its connection, b1beba6 lease keeps its VM on that refusal, ace1c43 build-snapshot.sh no writes through a symlink, ab347c1 install-host.sh removes the old jail path, 5181204 http-api doc). Those belong to #757 and are reviewed there. I checked the two conflicted merges on the way down (3ee0acf upstream/main into i10, d4445a2 i10 into i17) against their recorded resolutions. errors.go, guestconn.go, build-snapshot.sh, microvm-worker main.go and snapshot_test.go keep both sides, as the commit messages say, and main.go's single LoadManifest now feeds both ManifestFeatures and VerifyWith. No new findings. The earlier 3 suggestions, 1 nit still apply: none of the files they cite changed in this push.

Author: webchang (MEMBER — maintainer)
Areas reviewed: range-diff of this PR's own commits, the incoming #757 commits (scope only), the conflict resolutions in the two upstream merges, commits/DCO, CI, agent-config gate
Agent/IDE config (.claude/.vscode): none
Commits: 55 commits, all signed-off: yes
CI status: passing (all 17 checks green)

0 new must-fix, 0 new suggestions, 0 new nits.

Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

Conflicts in docs only, both sides kept: openapi's getQuota description
(rossoctl#766's template and build counting, and this branch's activation
leases) and http-api.md's error-code list (rossoctl#766's template_exists,
policy_refused and quota_exceeded, and this branch's no_active_slot,
template_unavailable and template_not_active).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Conflict in docs/reference/http-api.md's error-code list, both sides
kept: rossoctl#766's template_exists, policy_refused and quota_exceeded, and
this branch's build_in_use and template_retired.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
rossoctl#775's merge was redone without a duplicate import; this takes it. The
error-code list keeps the resolution already made here.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
A retired build's worker is stopped only by a host agent that holds the
build: the wanted state's `stop` lists only the builds a host holds. With
host agents present but none holding the build (`heldBy` empty, such as an
imported template whose worker an operator runs), the server still reports
the attached worker's capacity, so --wait could only time out and exit 5.

--wait now treats `heldBy` empty like null: nothing to wait for. When the
worker still runs, it says that the worker is not host-managed and is to be
stopped where it runs, instead of saying it stopped. The spec, the HTTP API
reference and the usage text say so; a test covers `heldBy: []` with a
non-null capacity (running known and unknown).

Refs rossoctl#749
Assisted-By: Claude (Anthropic AI) <[email protected]>

Signed-off-by: Rong Chang <[email protected]>
…apacity

A build retired while queued, building or probing has no worker, so there
is nothing to stop. While presence could not be read it still reported
`capacity: {running: null, ...}`, which `sbx templates retire --wait`
waits out until its timeout. Only a retired build with a worker now
reports capacity; one without reports null, presence read or not. The
openapi description says so; an activation test covers it.

Refs rossoctl#749
Assisted-By: Claude (Anthropic AI) <[email protected]>

Signed-off-by: Rong Chang <[email protected]>
A retire's build_in_use check matched live sandboxes by worker id. Two
builds of one template from the same image with other specs share a
worker id (template-build.sh names the worker from the template id and
the image digest), so a live sandbox on the newer build blocked retiring
the older one with a misleading 409.

A sandbox record names the build it resolved to (`buildId`); it now
counts for that build only. A record without `buildId`, from before
builds were traced, still counts for every build on its worker. The
spec says so; activation tests cover both.

Refs rossoctl#749
Assisted-By: Claude (Anthropic AI) <[email protected]>

Signed-off-by: Rong Chang <[email protected]>
…cord

A retire record's `buildRecord` was found by matching the `build`
record's manifest_hash against the build's id. A build that was built and
then rejected keeps its handle as its id (only registration sets the id to
the manifest hash), so its retire record said `buildRecord: null`, though
an `outcome: built` record exists for it, against the documented "null
for one never built".

buildRecordOf now matches the build record by the build's handle when it
has one, and by the manifest hash only for a build without a handle. An
activation test retires a built-then-rejected build and checks the
record it cites.

Refs rossoctl#749
Assisted-By: Claude (Anthropic AI) <[email protected]>

Signed-off-by: Rong Chang <[email protected]>
…the retired build

A retire took no activation lock, and the lease-taking step did not
re-check the build's status. An activation that resolved its build as
registered just before a retire still wrote its lease onto the retired
build and answered 200; the `active` quota counts leases whatever the
build's status, so that stale lease counted against the caller until it
expired.

A retire now runs its in-use read and catalog update under the activation
lock, and the lease-taking step re-reads the build's status under that
lock: a build retired since is refused 410 template_retired, with no lease
and no activate record. The spec says so; two activation tests drive the
race both ways (the retire between resolve and lock, and the retire
arriving during the lease write).

Refs rossoctl#749
Assisted-By: Claude (Anthropic AI) <[email protected]>

Signed-off-by: Rong Chang <[email protected]>
Refs rossoctl#749
Assisted-By: Claude (Anthropic AI) <[email protected]>

Signed-off-by: Rong Chang <[email protected]>
Takes the activation branch's reviewed fixes (the default lease from the
holders' idle limit, imported templates never host-managed, stale hosts
left out of placement, sticky placement from the hosts' running workers,
Retry-After from the builds placed on the holders, no renewal on a
first-use retry, a repeated refusal recorded once, and the host agent's
fixes) beside this branch's retire. Conflicts and their resolutions:

- packages/control-plane/src/sandbox-handlers.ts: imports only. Both
  sides kept: buildRecordOf and the HostView type from this branch;
  hostManaged, busyWorkers and freshHosts from the activation branch.
  takeLeaseLocked merged cleanly and keeps both: the clamped default
  lease, busy-aware placement and the holders' Retry-After, and the
  status re-check under the activation lock (template_retired, or
  template_unavailable, before the quota check, placement and the
  activate record).
- packages/control-plane/src/host-handlers.ts: hostWanted keeps the
  retired branch (a held retired build's idle worker is in stop, never
  in run) and the activation branch's sticky stop (a leased build this
  host runs while placement keeps it elsewhere) and refusal clearing,
  also cleared for a retired build. A build that is not hostManaged (an
  imported one) is skipped whatever its status. hostWorkerReport keeps
  detached for a retired build and the running list and refusal dedupe.
  Both doc comments merged.
- docs/api/openapi.yaml: activate's description keeps the Retry-After
  wording and 410 template_retired; hostWanted's keeps sticky placement,
  the draining count and retired builds in stop, and says an imported
  build is in neither list; hostWorkerReport's keeps detached for a
  retired build and the refusal dedupe. info.version stays 1.8.0 (the
  activation branch's changes stay under 1.7.0, retire is 1.8.0).
- docs/specs/2026-11-03-template-lifecycle-and-provenance-design.md: the
  wanted-state bullet joins both texts: sticky stop, retired builds in
  stop, imported builds in neither list, stop-then-start order, and
  detached taken for a retired build.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
…slot

Placement counted a worker still draining a live sandbox against its
host's activeLimit only when its build was registered. A build retired
while a sandbox still ran on its worker freed the slot at once, though
the worker kept running until the sandbox ended, so placement could put
another build on that host over its limit. A retired build with a worker
now counts the same way while its worker is busy. The spec says so; a
placement test retires a build with a live sandbox on its worker and
sees the slot taken until the sandbox ends.

The in-use check's local set of workers with untraced sandboxes is
renamed, so it no longer shadows the imported busyWorkers.

Refs rossoctl#749
Assisted-By: Claude (Anthropic AI) <[email protected]>

Signed-off-by: Rong Chang <[email protected]>
Conflicts and their resolutions (both sides' behaviour kept):

- deploy/sbx/host-agent.sh: activation() stops each `stop` worker
  first, then starts each `run` worker (the retire side's order, so a
  freed slot is free before a start), then runs removal() for the
  wanted state's `remove` (this side). The header comment says all
  three. The test's summary lines for the empty-log, gate-mismatch and
  non-JSON-record cases gain `"removed":0`.
- packages/control-plane/src/host-handlers.ts: a retired build held by
  the host clears its recorded refusal (retire side) and, with no live
  sandbox, is listed in both `stop` and `remove` (this side); a build
  imported from SH_SANDBOX_TEMPLATES is in no list. The `removed`
  report's buildRecord is looked up with the build itself, as the
  retire side's buildRecordOf now takes it (handle first). The report's
  doc names both the repeated-refusal rule and `removed`. The
  activation test for a copy placed on another host expects
  `remove: []`.
- docs/api/openapi.yaml, docs/reference/http-api.md, the lifecycle
  spec: the wanted state's text keeps sticky placement, the
  other-host `stop`, the draining worker's slot and the imported-build
  exclusion, and adds `remove`; the report text keeps the
  refusal dedupe and adds `removed`; retire --wait is described as
  below.
- packages/mocactl/src/sbx/templates.ts, cli.ts,
  test/sbx-templates.test.ts: retire --wait's two rules made one. With
  host agents it waits for the worker's stop and for heldBy to empty;
  heldBy null (no host agents, or an older control plane) is done at
  once, and so is a heldBy empty from the first read (no host holds the
  build), saying a worker that still runs is not host-managed. A build
  that was held and whose heldBy empties is not done until its worker
  stops too. Two tests added: empty from the start with the worker
  stopped, and held then emptied while the worker still runs.

Refs rossoctl#743

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
@webchang

Copy link
Copy Markdown
Member Author

Merged #781's second round (15e6655) into this branch; new head aca19d8. The conflicts:

This PR's own change is unchanged; CI is the full check.

Assisted-By: Claude Code

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

Follow-up at aca19d8 (fd0093d..aca19d8). There are no new commits of this PR's own. The merge brings in #781's second round (668d831, 6688bf1, b7ec347, d5953e2, 15e6655). I already reviewed those on #781 @15e6655 (APPROVE draft). I checked the conflict resolutions with git show --remerge-diff aca19d8, and all are correct:

  • hostWanted keeps this branch's remove list and takes #781's run/stop filter (stop never lists a worker in run). remove is not filtered, and that's right: a retired build in remove is not shared, so it can't share a worker with a registered build in run.
  • liveSnapshots is now #781's exported copy. sandbox-handlers.ts buildBody uses it, so shared and the wanted state judge alike.
  • In cmdRetire --wait, the shared check comes first (it returns at once, since a shared build is never removed). Next comes this branch's heldBy-based "no host holds it" branch. It replaces #781's bare capacity === null branch. Then "stopped and removed from every host". The order is right.
  • #781's two hostWanted tests now expect remove: [] and remove: [two, one]. The CLI tests from both sides are kept. The docs, openapi, and spec text are merged and agree.

Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript (control-plane, mocactl), API docs (merge resolutions)
Agent/IDE config (.claude/.vscode): none
Commits: no new own commits (1 merge), signed off (DCO pass)
CI status: passing so far. check (ci), deploy-scripts, and k8s-kind-e2e are pending.

AI-assisted review, approved by a maintainer before posting.

Brings in upstream main (lease modes, API 1.6.0) through pr/i19-activation.

Conflicts:
- docs/api/CHANGELOG.md: this branch's retire section, renumbered 1.9.0, above
  pr/i19-activation's 1.8.0, pr/i18-host-agent's 1.7.0 and main's 1.6.0.
- docs/api/openapi.yaml: the Error code list keeps this branch's template_retired and
  build_in_use, then main's mode_not_allowed.
- docs/reference/http-api.md: the host agents (1.7.0) and activation (1.8.0) rows as
  pr/i19-activation has them, then this branch's retire row.
- packages/control-plane/src/errors.ts, test/errors.test.ts: both sides' new codes and statuses.
- packages/control-plane/src/sandbox-handlers.ts: this branch's imports (buildRecordOf, HostView,
  liveSnapshots) with main's MIN_IDLE_TIMEOUT_S.

Beyond the markers:
- packages/control-plane/test/openapi-contract.test.ts: the enrollment-checked route count is 24
  (pr/i19-activation's 22 plus this branch's retire and build delete; each side had raised 20 to 22).
- packages/control-plane/test/activation.test.ts: the untraced sandbox record carries main's
  mode, idleTimeoutS and modeEpoch, now required fields of SandboxRecord.

API version: this branch's retire moves from 1.8.0 to 1.9.0 (openapi info.version, the changelog,
and the two "API 1.8.0" mentions in docs/reference/http-api.md).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings in upstream main (lease modes, API 1.6.0) through pr/i26-retire.

Conflicts:
- docs/api/CHANGELOG.md: this branch's removal section, renumbered 1.10.0, above pr/i26-retire's
  1.9.0 and the lower sections.

API version: this branch's removal moves from 1.9.0 to 1.10.0: openapi info.version and its
"since 1.9.0" field notes, the changelog, and the "API 1.9.0" mentions in
docs/reference/http-api.md and deploy/sbx/host-agent.sh.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings in upstream main through f60db30 (turn grants, API 1.7.0) by pr/i19-activation.

Conflicts:
- docs/api/CHANGELOG.md: this branch's retire section, renumbered 1.10.0, above
  pr/i19-activation's 1.9.0, pr/i18-host-agent's 1.8.0 and main's 1.7.0 and 1.6.0.
- docs/reference/http-api.md: the host agents (1.8.0) and activation (1.9.0) rows as
  pr/i19-activation has them, then this branch's retire row.

API version: this branch's retire moves from 1.9.0 to 1.10.0 (openapi info.version, the
changelog, and the two "API 1.9.0" mentions in docs/reference/http-api.md).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings in upstream main through f60db30 (turn grants, API 1.7.0) by pr/i26-retire.

Conflicts:
- docs/api/CHANGELOG.md: this branch's removal section, renumbered 1.11.0, above pr/i26-retire's
  1.10.0 and the lower sections.

API version: this branch's removal moves from 1.10.0 to 1.11.0: openapi info.version and its
field notes, the changelog, and the mentions in docs/reference/http-api.md and
deploy/sbx/host-agent.sh.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
webchang added a commit to webchang/moca that referenced this pull request Nov 13, 2026
…a live build shares is neither stopped nor removed

Two builds can share a snapshot (the same manifest hash in two templates, or built again) or a
worker id (two builds of one template from one image). The wanted state's `remove` was keyed only
on the build being retired, so retiring one had the host agent uninstall a worker and delete a
snapshot that a build still registered needed.

- liveSnapshots(docs): the build ids and workers of every build whose use has not ended. A retired
  or admin-rejected build that shares either is left out of `stop` and `remove` (that build's own
  entries decide), a host's `removed` report of it is refused 409 build_in_use (so the live build
  keeps the host in heldBy), and its TemplateBuild reports `shared: true`. A rejected build is not
  live, so an admin's reject does not count as sharing with itself (the upstream branches' merge
  follow-up). Sharing is judged across the whole catalog, as the wanted state judges it.
- One worker id is never listed in `run` for one build and in `stop` for another that shares it.
- A retired or admin-rejected build's worker still draining a live sandbox counts against its
  host's activeLimit, as a registered one's does.
- heldBy lists every host as last reported, stale ones too: it is the snapshot inventory, not
  placement, and a silent host still holds what it last reported.
- A T.unique rejection naming a retired build says it is the same snapshot, and to change the spec
  or create again after the snapshot is removed.
- mocactl's `templates retire --wait` is done at once for a `shared` build, and says why.

openapi (TemplateBuild `shared`, heldBy) and the API changelog say so. (This tree's retire waits for
removal through heldBy and reports no capacity for a retired build, so the upstream capacity
changes for retired builds do not apply.)

Tests (each fails with the fix reverted): never stops or removes a retired build whose snapshot or
worker a build not retired shares; never lists one worker in run and stop for two registered
builds that share it; keeps a silent host in heldBy; counts a retired (and an admin-rejected)
build's worker draining a live sandbox against its host; says how to rebuild when the same
snapshot belongs to a retired build; mocactl: is done at once for a shared build.

(cherry picked from commit 52fd121)
(cherry picked from commit 668d831)
(cherry picked from commit 15e6655)
(cherry picked from commit 6688bf1)
(cherry picked from commit 508baff)
(cherry picked from commit 2d3df2e)
(cherry picked from commit 6e24cdb)
(cherry picked from commit e895379)

Ports rossoctl#781, rossoctl#785 and rossoctl#786 review fixes

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
webchang added a commit to webchang/moca that referenced this pull request Nov 13, 2026
… the host's shared setup

install-host.sh --uninstall was an operator command; the agent's removal of a retired build now
runs it automatically, and on the host's last SBX0 worker it also removed the shared units
([email protected], sbx.slice), moca-egress and its credential injection, so a client's retire
could undo an operator's --egress. install-host.sh gains --keep-shared, accepted only with
--uninstall: it removes the worker's own files and nothing shared, even for the last worker, and
says what it kept when no worker is left; a plain --uninstall of the same id removes the rest. The
agent's removal runs `--uninstall <worker> --keep-shared`. The RUNBOOK and the header say so.

Tests (fail with the fix reverted): install-host.test.sh, --uninstall --keep-shared removes the last
worker alone and keeps the shared units and egress setup (under credential injection), and
--keep-shared alone is refused; host-agent.test.sh checks the agent passes it.

Host-side (deploy/sbx/install-host.sh, deploy/sbx/host-agent.sh).

(cherry picked from commit 4297014)

Ports rossoctl#785 review fixes

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
webchang added a commit to webchang/moca that referenced this pull request Nov 13, 2026
…napshot is gone

removal() reads a record's `snapshot` as a string only (a missing or
non-string value is empty and refused, never the word null), deletes the
record only once the snapshot directory is gone (a failed delete is an
error line, not reported, and retried next run), carries the last lines
of a failed uninstall's log in its error line, and names an
SBX_INSTALL_HOST that is no executable, removing nothing.

host-agent.test.sh: bad snapshot names delete and report nothing; a
failing uninstall and a missing install-host.sh keep the snapshot and
record; an undeletable snapshot keeps its record and goes on the next
run (skipped as root). Host-side.

(cherry picked from commit 9fee088)

Ports rossoctl#785 review fixes

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
webchang added a commit to webchang/moca that referenced this pull request Nov 13, 2026
…host with no grace

The wanted answer listed a retired or admin-rejected build in `stop`
beside `remove` only on a host with an idle grace. The host agent skips
the removal of a running worker, so on a host without a grace a worker
that ran when its build was retired was never stopped, nor removed. It
is now listed in `stop` on every host, as upstream rossoctl#785's wanted answer
does (the agent stops it only if its unit runs, before the removal).

activation.test.ts: an activated (running) build retired on a host with
no grace is in `stop` and `remove`; the existing removal test expects
`stop` too. Both fail with the fix reverted.

Ports rossoctl#785 review fixes

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings in upstream main through ab8d10e (template tags, edits and owner transfer at API 1.8.0,
setup.sh's Redis image guard), the host agent at 1.9.0 and activation at 1.10.0, by
pr/i19-activation.

Conflicts:
- docs/api/CHANGELOG.md: this branch's retire section, renumbered 1.11.0, above activation's
  1.10.0, the host agent's 1.9.0 and main's 1.8.0, 1.7.0 and 1.6.0.
- docs/reference/change-policy.md: version 1.11.0; main's list, then the template host agents,
  activation leases and template retire.
- docs/reference/http-api.md: main's template admin row, the host agent and activation rows, then
  this branch's retire row (API 1.11.0).
- docs/specs/2026-11-03-template-lifecycle-and-provenance-design.md: the status line names
  §5.1-§5.2 with retire; §5 is main's, with this branch's retire block (its table without the
  cancel row, which main's table holds) after main's tag paragraph and before §5.2.
- packages/control-plane/src/routes.ts: main's four template admin routes, then retire and the
  build delete, then activation.
- packages/control-plane/src/sandbox-handlers.ts: both sides' imports (main's, with this branch's
  buildRecordOf, HostView and liveSnapshots).
- packages/control-plane/test/routes.test.ts, route-profile.test.ts: main's routes, then retire's.
- packages/control-plane/test/openapi-contract.test.ts: 28 enrollable routes (26 plus retire's
  two).
- packages/mocactl/src/sbx/cli.ts: main's NO_RECORD, TEMPLATE_ADMIN_SUBS and TEMPLATE_ONE_ID, with
  retire in TEMPLATE_SUBS, TEMPLATE_ONE_ID, TAKES_BUILD and TAKES_TIMEOUT.
- packages/mocactl/test/contract.test.ts: 25 replies.

Fitted to main: retireBuilds takes main's ownTemplate and makes the owner's check again under the
template's lock (ownerCheck), as main's other owner actions do, so a retire by an owner the
template was transferred away from meanwhile is refused.

API version: this branch's retire moves from 1.10.0 to 1.11.0 (openapi info.version, the
changelog, change-policy and the two "API 1.10.0" mentions in docs/reference/http-api.md).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings in upstream main through ab8d10e (template tags, edits and owner transfer at API 1.8.0,
the worker stop's VM reap, SBX_WORKER_AT_BOOT, setup.sh's Redis image guard) and retire at
1.11.0, by pr/i26-retire.

Conflicts:
- deploy/sbx/install-host.sh: main's SBX_WORKER_AT_BOOT check, then the usage line with this
  branch's --uninstall ID [--keep-shared]. --keep-shared keeps main's sbx-reap-vms.sh helper with
  the other shared units (it is a manifest `unit`).
- docs/api/CHANGELOG.md: this branch's removal section, renumbered 1.12.0, above retire's 1.11.0,
  activation's 1.10.0, the host agent's 1.9.0 and main's 1.8.0, 1.7.0 and 1.6.0.
- docs/reference/change-policy.md: version 1.12.0; main's list, then the template host agents,
  activation leases, template retire and retired build removal.

API version: this branch's removal moves from 1.11.0 to 1.12.0 (openapi info.version and its two
"1.11.0" notes, the changelog, change-policy, "Since API 1.11.0" in docs/reference/http-api.md and
"older than API 1.11.0" in deploy/sbx/host-agent.sh).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings in upstream main through 01528b6 (subject trust for gate records at API 1.9.0), the host
agent at 1.10.0 and activation at 1.11.0, by pr/i19-activation.

Conflicts:
- docs/api/CHANGELOG.md: this branch's retire section, renumbered 1.12.0, above activation's
  1.11.0, the host agent's 1.10.0 and main's 1.9.0 and below.
- docs/reference/change-policy.md: version 1.12.0; main's list (through subject trust for gate
  records), then the template host agents, activation leases and template retire.
- docs/reference/http-api.md: the host agent (1.10.0) and activation (1.11.0) rows, then this
  branch's retire row (API 1.12.0).
- packages/control-plane/src/sandbox-handlers.ts: ReadinessFacts carries both this branch's hosts
  and shared and main's trusted set; resolveForUse judges visibility once with main's
  canSee(d, p, trusted) and uses it for both the retired refusal and the build.

API version: this branch's retire moves from 1.11.0 to 1.12.0 (openapi info.version, the
changelog, change-policy and the two "API 1.11.0" mentions in docs/reference/http-api.md).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings in upstream main through ec0f840, whose tree is pr/i19-activation's, already merged here
(its merges of pr/i18-host-agent and pr/i19-activation). No conflicts and no file changes.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings in upstream main through ec0f840 (subject trust for gate records at API 1.9.0; the host
agent at 1.10.0 and activation at 1.11.0, merged) and retire at 1.12.0, by pr/i26-retire.

Conflicts:
- docs/api/CHANGELOG.md: this branch's removal section, renumbered 1.13.0, above retire's 1.12.0
  and below.
- docs/reference/change-policy.md: version 1.13.0; main's list (through subject trust for gate
  records), then the template host agents, activation leases, template retire and retired build
  removal.

API version: this branch's removal moves from 1.12.0 to 1.13.0 (openapi info.version and its two
notes, the changelog, change-policy, "Since API 1.12.0" in docs/reference/http-api.md and "older
than API 1.12.0" in deploy/sbx/host-agent.sh).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Merges upstream main c812554 (memory placement, start pacing, egress
records and the egress agent, the sbx lease clients). This branch's retire
section moves from API 1.12.0 to 1.15.0, the next number above main's 1.14.0.

Conflicts and their resolutions:
- docs/api/CHANGELOG.md: this branch's section, renamed 1.15.0, above main's
  1.14.0 to 1.12.0 sections; main's sections from 1.14.0 down are main's
  bytes.
- docs/api/openapi.yaml: info.version 1.15.0; Template.readiness keeps
  `retired` and main's readinessReason; TemplateBuild.readiness keeps main's
  memory wording plus `retired`, and main's readinessReason, queued,
  queuePosition, graced and gracedUntil; activateTemplate keeps main's
  memory refusals plus `410 template_retired`; hostWanted keeps main's
  wording (memory headroom, idle grace, the wanted order) plus the retired
  builds in `stop` and the imported-build rule.
- docs/reference/change-policy.md: main's list, then "template retire";
  both version numbers 1.15.0.
- docs/reference/http-api.md: main's egress-records row and paragraph, then
  this branch's retire row and paragraph, now API 1.15.0.
- docs/specs/2026-11-03-template-lifecycle-and-provenance-design.md: main's
  status line plus "and retire (rossoctl#749)"; §6.2 keeps main's memory text plus
  "retired ones (§5.1) included" for `used`.
- packages/control-plane/src/activation.ts: main's place() with its memory
  maps; workerOf also holds a retired build with a worker (this branch), and
  buildOfWorker names a retired build's worker too unless a registered build
  shares it, so a retired worker draining a live sandbox counts against its
  host whether the agent reports it in `running` or in the memory report's
  runningWorkers. The doc comment keeps both.
- packages/control-plane/src/host-handlers.ts: main's imports plus
  hostManaged; main's hostEgress and hostWanted (now wantedAnswer plus
  egressSids). This branch's retire logic moves into wantedAnswer: a held,
  host-managed retired build is in `stop` when its worker has no live
  sandbox, unless a build not retired shares its snapshot or worker
  (liveSnapshots); a retired build is never graced or displaced (graceUntil
  takes registered builds only). The run/stop worker filter runs after the
  graced builds join `run` and after the displaced re-check, so a worker in
  `run` (wanted or graced) is never in `stop`. `starting` and `graced` are
  main's, for the live builds.
- packages/control-plane/src/sandbox-handlers.ts: main's imports plus
  liveSnapshots. ReadinessFacts keeps main's `queue` and `hosts` (the fresh
  host views, for a start line's Retry-After) and this branch's every-host
  views, renamed `holders` (for `heldBy`), and `shared`. readinessFacts
  reads the catalog once for both sharing and placement. buildBody keeps
  `retired` readiness and main's readinessReason, start line and grace
  fields. The activation lock moves to main's activation-lock.ts, whose
  comment gains this branch's retire and sandbox-create sentences.
  retireBuilds keeps ownerCheck under the template lock.
- packages/control-plane/test/activation.test.ts: both sides' place() tests.
- packages/control-plane/test/openapi-contract.test.ts: 29 enrollable routes
  (26 + 2 retire + 1 egress).
- packages/mocactl/src/sbx/api.ts: main's SbxBuild fields, with this
  branch's capacity `running: number | null`.
- packages/mocactl/src/sbx/cli.ts: usage keeps main's activate, deactivate
  --stop-now, hosts and create --mode text, with retire after deactivate;
  both `reason` and `stop-now` options; both dispatches.
- packages/mocactl/src/sbx/templates.ts: header names both; both
  RETIRE_TIMEOUT_S and main's sized activate timeout; readinessText keeps
  `running ?` for a null running and main's reason, queue and grace text.
- packages/mocactl/test/contract.test.ts: 27 calls (main's reply shape).
- packages/mocactl/test/sbx-templates.test.ts: both imports, header and
  test blocks.

Beyond the markers:
- activation.test.ts: this branch's retire tests compare the whole wanted
  answer; they now include main's `starting` and `egressSids`.
- activation.test.ts: new tests for the merged behaviour: a graced build
  retired is stopped at once and never graced; a retired build whose worker
  a graced build shares stays out of `stop`, and the graced build's own
  entry stops the worker once its grace ends; a retired worker draining a
  live sandbox that only the memory report names counts against its host.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings rossoctl#789 (sandbox templates for sessions, the combined control plane;
API 1.15.0 and 1.16.0) and rossoctl#790 (a session's seed, patch and records;
1.17.0). This branch's retire section moves to 1.18.0.

Conflicts:
- docs/api/CHANGELOG.md: ## 1.18.0 retire, above main's ## 1.17.0 to
  1.15.0 and below, which are unchanged.
- docs/api/openapi.yaml: info.version 1.18.0. The wanted-state
  description is main's (with a pinned session's hold), plus the
  retired builds in stop and the imported-build rule.
- docs/reference/change-policy.md: main's list, then "template retire";
  control plane 1.18.0, harness 1.3.0 (main's).
- packages/control-plane/src/sandbox-handlers.ts readinessFacts: this
  branch's fields (holders, shared) on the no-sandbox return, under
  main's condition (no sandbox use on the harness profile only).
- packages/control-plane/test/activation.test.ts: registered() takes this
  branch's options (another, vcpus, sourceDigest, status) plus main's
  spec, as opts.spec; main's one caller passes { spec }.

Also moved to 1.18.0: http-api.md's Retire row and "Retire came with API"
line.

A worker kept by a pinned session's hold counts as busy, as a live
sandbox does, so a retired build's worker drains until the hold ends
before it is stopped.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Brings pr/i26-retire at upstream main d227d41 (through rossoctl#790, with rossoctl#789's
combined plane and pinned-session holds, rossoctl#791/rossoctl#792 egress, rossoctl#784 pacing,
rossoctl#776 memory placement and rossoctl#808), and i26's retire at API 1.18.0. This
branch's removal section moves to 1.19.0.

Conflicts:
- docs/api/CHANGELOG.md: ## 1.19.0 removal above i26's ## 1.18.0 and
  main's sections, which are unchanged. The `remove` bullet names a
  pinned session's hold beside a live sandbox.
- docs/api/openapi.yaml: info.version 1.19.0. The wanted-state
  description is main's (memory, idle grace, pinned-session hold, start
  order), with "in no list", `remove` (since 1.19.0; not under a live
  sandbox or a session's hold) and the shared-build rule; the response
  keeps main's `starting`, `graced` and `egressSids` and adds `remove`.
- docs/reference/change-policy.md: main's list, then "template retire",
  then "retired build removal"; control plane 1.19.0, harness 1.3.0.
- packages/control-plane/src/host-handlers.ts: main moved the wanted
  state into wantedAnswer; `remove` is built there, in the retired
  branch, under main's busyWorkers (a live sandbox or a pinned session's
  hold), and returned beside main's run, stop, starting and graced;
  hostWanted adds egressSids as main does. hostWorkerReport takes
  `removed` on main's version of it (st.running and refused kept); the
  host's holds drop the build on st. The header, liveSnapshots and
  T.unique texts are this branch's.
- packages/control-plane/test/activation.test.ts: the wanted answers
  carry both `remove` and main's `starting`/`egressSids`.
- packages/mocactl/src/sbx/cli.ts: main's usage (sbx hosts, create
  --mode/--idle-timeout) with this branch's retire --wait text.
- deploy/sbx/host-agent.sh: main's header and run loop (run.tsv, start
  limit, grace), then this branch's removal() before the lock is let go;
  REMOVED beside main's counters; summary keeps "removed" before
  "errors" and main's egress, memory, start and grace fields after it.
- deploy/sbx/tests/host-agent.test.sh: both mocks (install-host,
  journalctl) and both settings; main's sections 13-26, then this
  branch's removal section as 27, on a clean srv with well-formed
  snapshots (snapfiles), so main's memory report raises no error.
- deploy/sbx/RUNBOOK.md: main's summary text with "removed".
- docs/specs/2026-11-03-template-lifecycle-and-provenance-design.md:
  main's records table with this branch's `event: removed`; step 7 names
  a pinned session's hold.

Also moved to 1.19.0: http-api.md's "Since API 1.19.0" and
host-agent.sh's "older than API 1.19.0" comment. activation.test.ts's
live-sandbox removal case reads the answer with toMatchObject: the
wanted state now also carries egressSids, which list the sandbox.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
…s' egress window has passed

The wanted state's `remove` listed a retired build as soon as no live
sandbox ran on its worker. The agent then reports `removed`, which drops
the build from the host's holds, and with it the host's right to ship
egress events for that worker's sandboxes (hostSandboxes): the events of
a sandbox that ended in the last poll, which the agent sends after the
removal in the same run, were refused.

A retired build whose worker has a sandbox still in egressAccepts (ended
less than EGRESS_GRACE_S ago) is now in `stop` only; `remove` lists it
once that window has passed. The CHANGELOG's 1.19.0, openapi, the spec's
step 7 and the RUNBOOK say so. activation.test.ts checks both answers.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
…s egress window

A retired build whose worker had a sandbox end less than 600 s ago stays
in `stop` only until that window passes, and is listed in `remove` after
it, so the host can still ship that sandbox's last egress events. With
the old 300 s default, `retire --wait` on such a build timed out (exit 5)
before the removal. The default is now 900 s: the window plus a few host
polls. The usage text, the spec and the RUNBOOK say so.

Test: the default outlasts the 600 s window.

Refs rossoctl#743

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
…tion

The merge of pr/i26-retire took i26's 1.18.0; this branch's section, its
change-policy entry and the remove field's "since 1.19.0" are 1.19.0.

Refs rossoctl#743

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
…est section and change-policy

A merge that took a lower branch's info.version left the contract a
version behind its own changelog, and no test caught it. The contract test
now reads the CHANGELOG's first section heading and checks that
openapi.yaml and change-policy.md name the same version.

Refs rossoctl#743

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
@webchang

Copy link
Copy Markdown
Member Author

#781's newest head (219d83f, with main d227d41) merged in. Besides resolving the conflicts, this round adds:

  1. A retired build stays out of remove for 600 s after its last sandbox ended (e914f83), so the host can still ship that sandbox's last egress records.
  2. retire --wait now defaults to 900 s (b5f1660), to outlast that window.
  3. openapi info.version is 1.19.0 (10c26dc).
  4. A new contract test (fd26802) checks that openapi's version matches the CHANGELOG's newest section and change-policy.md.

#786 also claims 1.19.0 (both stack on #781), so whichever merges later renumbers.

Assisted-By: Claude Code

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Follow-up at fd26802 (aee58f8..fd26802). This push merges #781's tip (main d227d41, via pr/i26-retire) and adds four commits of this PR's own:

  • e914f83: wantedAnswer holds a retired build in stop without remove while its worker still has a sandbox in egressAccepts, meaning one that ended less than EGRESS_GRACE_S (600 s) ago. This is right. removed drops the build from holds, and hostWorkers → hostSandboxes → hostEgress reads holds. Before this fix, the agent removed the build in the same run and then had the last egress events of a just-ended sandbox refused. The new test checks both answers, before and after the grace period. Without an egress log (!deps.egress), hostSandboxes is empty and removal is immediate, which is fine.
  • b5f1660: retire --wait now defaults to 900 s, which covers the 600 s window plus host polls. The usage text, spec, and RUNBOOK all say so.
  • 10c26dc / fd26802: openapi info.version is 1.19.0. A new contract test ties info.version and change-policy to the CHANGELOG's top section, which closes the drift class the merge exposed.

I checked the hand resolutions in 2d7c8b3, d4722fa, and 1f1f9f4 with git show --remerge-diff and a dropped-lines check:

  • remove moved into main's wantedAnswer (beside starting/graced/egressSids).
  • The worker report uses st.holds for the removed hold drop.
  • host-agent.sh keeps main's start queue, egress counters, and summary fields, adds removed, and runs removal after the start loop. Its version guard now says 1.19.0.

No conflict markers are left.

Author: webchang (MEMBER — maintainer)
Agent/IDE config (.claude/.vscode): none
Commits: 4 new own commits plus merges, all signed off (DCO pass)
CI status: all checks green, k8s-kind-e2e included

AI-assisted review, approved by a maintainer before posting.

@webchang
webchang merged commit 906482c into rossoctl:main Nov 14, 2026
18 checks passed
webchang added a commit to webchang/moca that referenced this pull request Nov 14, 2026
Brings rossoctl#785 (retired build removal, API 1.19.0). This branch's
enrollment section takes the next number, 1.20.0.

Conflicts:
- docs/api/CHANGELOG.md: ## 1.20.0 above main's ## 1.19.0 and the
  sections below it, which are unchanged.
- docs/api/openapi.yaml: info.version 1.20.0.
- docs/reference/change-policy.md: main's list, then "enrollment and the
  admin role"; control plane 1.20.0.

Also moved to 1.20.0: http-api.md's Subjects row and its "Who is enrolled"
paragraph.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
@pdettori pdettori added this to the v0.11.0 milestone Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let the host agent remove a retired build's worker and snapshot

4 participants