Skip to content

feat(control-plane): refuse a retire or reject while an active Moca Pi session is pinned to the build - #819

Merged
webchang merged 3 commits into
rossoctl:mainfrom
webchang:pr/i817-retire-pinned
Nov 16, 2026
Merged

webchang merged 3 commits into
rossoctl:mainfrom
webchang:pr/i817-retire-pinned

Conversation

@webchang

Copy link
Copy Markdown
Member

Closes #817

What changed and why

  • A retire (POST /v1/templates/{id}/retire, DELETE /v1/templates/{id}/builds/{build}) or an admin's reject (POST /v1/templates/{id}/builds/{build}/reject) is now refused 409 build_in_use ("is pinned by an active session") while an active Moca Pi session is pinned to the build. Before, only live sandboxes counted, so a retire could stop the worker and remove the snapshot that a pinned session's next turn needs.
  • A per-build session index: a session created with a sandbox template is added to a sorted set keyed by its build (the build id hashed into the key, as the owner index does); the delete cascade removes it. buildsInUse asks the index whether an active session is still pinned to the build. A deployment whose index has no such check behaves as before.
  • Pruning: a member whose record is gone, is not active (for example left deleting by a crash mid-cascade) or names another build does not count, and the check removes it from the set. So a crash between the cascade's steps cannot block a retire for good, and the set does not keep growing.
  • Docs: the lifecycle spec's §5.1 and §5.2 count an active pinned session as use, with the index's limits. The retire, retire-build and reject descriptions and their 409 answers in openapi.yaml say so. API version 1.22.0 (changelog and change policy updated), because an existing refusal gains a cause.
  • User guide: a retire is refused while a live sandbox runs on the build or an active Moca Pi session is pinned to it. The old wording, "a sandbox or session", was not exact. The guide also gave retire --wait's default as 300 s; it now gives the CLI's 900 s, which outlasts the 600 s a host keeps shipping an ended sandbox's egress before it removes the build.

Tests

activation.test.ts, with a template store, a host inventory and the session index:

  • "never retires a build an active session is pinned to; deleting the session frees it": a session pinned to a registered, held, active build; retire and retire-build are refused 409 build_in_use, and the build stays registered; after the session is deleted, the retire answers 200.
  • "never rejects a build an active session is pinned to (409 build_in_use)": an admin's reject is refused while the session is active, and goes through once it is deleted.
  • "prunes a pinned session that ended without the cascade's removal; an active one still blocks": ended sessions alone do not block and are removed from the set; beside an active one the retire is refused and only the active one stays; deleting it empties the set and the retire answers 200.

All three fail with the session check reverted; the pruning case also fails with only the pruning reverted. pnpm -r typecheck, the control-plane suite (with Redis), the openapi contract test and pre-commit pass.

Every commit is signed off (DCO).

Notes for review

  • The PR for Template create reuses a registered build even when no host holds its snapshot any more #816 also claims 1.22.0, as a sibling from the same main; whichever of the two lands second renumbers to 1.23.0.
  • The issue said no API version change. This PR bumps to 1.22.0 anyway, following the change policy, because an existing refusal (409 build_in_use) gains a cause.
  • buildsInUse keeps a typeof deps.index?.hasActiveSessionOnBuild === 'function' guard, so an index without the method behaves as before. Every index has it today, so the guard can go if you prefer.

Assisted-By: Claude Code

… is pinned to the build

A Moca Pi session created with a template keeps the build it resolved
to for its life, and each turn needs that build's worker. The retire
and reject guard counted live sandboxes only, so a retire (or an
admin's reject) under such a session went through: the build's leases
went, its worker was stopped and its snapshot removed, and the
session's next turn could not be served.

Sessions are now indexed by the build they are pinned to (a sorted set
per build, the build id hashed into the key as the owner index does):
added at session create, removed by the delete cascade. buildsInUse
asks the index whether an active session is still pinned to the build,
and the retire or reject is refused 409 build_in_use ("is pinned by an
active session"), as for a live sandbox.

A member whose record is gone, is not active (left deleting by a crash
mid-cascade) or names another build does not count, and the check
removes it from the set as it scans, so such a crash never blocks a
retire for good and the set does not keep growing.

Refs rossoctl#817

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

The lifecycle spec's retire (§5.1) and reject (§5.2) sections now count
an active pinned session as use, with the index's limits, in place of
the note that sessions did not yet name a build. The retire,
retire-build and reject descriptions and their 409 answers say so. API
1.22.0: an existing refusal gains a cause, so the changelog and the
change policy name the new version.

Refs rossoctl#817

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

The user guide said a retire is refused while "a sandbox or session"
uses the build. With this branch's guard that is exact only as: a live
sandbox runs on the build, or an active Moca Pi session is pinned to
it. It also gave retire --wait's default as 300 s; the CLI's default
(RETIRE_TIMEOUT_S) is 900 s, past the 600 s a host keeps shipping an
ended sandbox's egress before it removes the build. No other doc gives
300 s for it.

Refs rossoctl#817

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang requested a review from pdettori as a code owner November 16, 2026 05:48
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 16, 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.

Thanks — this closes the gap the lifecycle spec called out, and the design is sound: the per-build index mirrors the owner index (hashed key, same zset pattern), the check prunes crash-orphaned members so a stale entry can never wedge a retire permanently, and the known races (create-vs-retire ordering, pre-index sessions) are honestly documented in the spec rather than papered over. Tests cover the three behaviors that matter: block, unblock on delete, and prune-with-active-survivor.

A couple of small notes below; the rest is fine as is.

Summary

Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript (control-plane), docs (OpenAPI, spec, changelog, user guide)
Agent/IDE config (.claude/.vscode): none
Commits: 3 commits, all signed-off: yes
CI status: all checks passing

Note: this and #818 both claim 1.22.0; per your note, whichever lands second needs the renumber to 1.23.0 (changelog, openapi.yaml, change-policy) — easy to miss on merge.

Verified: SessionRecord.state is only 'active' | 'deleting' on head (ownership.ts:75), so the rec.state === 'active' && !rec.tombstone membership check can't evict a session in an intermediate state; and RETIRE_TIMEOUT_S = 900 in packages/mocactl/src/sbx/templates.ts:40 matches the user-guide correction.

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

Comment thread docs/api/openapi.yaml
`409 build_in_use` while a live sandbox runs on it. Idempotent: an already rejected build
is answered with `record: null`. Not reversible: a new build is the way back. Needs
`409 build_in_use` while a live sandbox runs on it or an active session is pinned to it.
Idempotent: an already rejected build is answered with `record: null`. Not reversible: a new build is the way back. Needs

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: this added line runs ~115 characters where the file's description blocks wrap at 100 — the sentence rewrap merged two lines. Suggested fix: break before "Not reversible:" to restore the wrap the surrounding descriptions use. Cosmetic only; the contract test and docs CI pass either way.

@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

This PR counts an active Moca Pi session pinned to a build as use, so retire, retire-build and an admin's reject are refused 409 build_in_use while one exists (#817). It does this through a per-build session zset, which the delete cascade maintains and the check prunes of ended or crashed members. The implementation, tests and docs are solid, and the acknowledged limits (create/retire race, pre-index sessions) are reasonable. One design point: an idle session can block an admin's reject indefinitely. Plus one nit.

Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript (control-plane, Redis index), tests, API docs/spec, user guide
Agent/IDE config (.claude/.vscode): none
Commits: 3 commits, all signed-off: yes
CI status: passing (all 17 checks)

Note: #818 also claims API 1.22.0, so whichever lands second renumbers to 1.23.0.

Assisted-By: Claude Code

if (typeof deps.index?.hasActiveSessionOnBuild === 'function') {
for (const x of builds) {
if (out.has(x.buildId) || !x.buildId) continue;
if (await deps.index.hasActiveSessionOnBuild(x.buildId))

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 (design): This now blocks an admin's reject too, and nothing can clear the block except the session's owner. Sessions don't idle out (the spec says: "blocks … until it is deleted, however long ago its last turn was"), and deleteSession is owner-only (assertOwner). So on a deployment-visible template, any user's forgotten session pins the build indefinitely. That stops the owner's retire, and also the admin's reject after the fact, which is the lever for a build that "later turns out wrong" (§5.2). Consider either letting reject count live sandboxes only (a wrong build should cut off sessions on it), or adding an admin override. At minimum, the spec's limits list should say an admin cannot get past it.

for (const x of builds) {
if (busyBuilds.has(x.buildId) || (x.worker !== '' && legacyWorkers.has(x.worker)))
out.set(x.buildId, 'still serves a live sandbox');
if (typeof deps.index?.hasActiveSessionOnBuild === 'function') {

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: As the PR body offers: every OwnershipIndex has hasActiveSessionOnBuild now, so the typeof … === 'function' guard and the "a deployment whose index has no such check" wording in the doc comment can go.

@webchang
webchang merged commit a7d568d into rossoctl:main Nov 16, 2026
17 checks passed
webchang added a commit to webchang/moca that referenced this pull request Nov 16, 2026
Upstream main at 032c8f6 (merge of rossoctl#818). Brings, since ccf393b:
- rossoctl#819: the retire/reject guard for an active session pinned to a build,
  with ownership.ts's index of the sessions pinned to each build;
- rossoctl#804: packages/knative-server renamed to packages/server;
- rossoctl#818: a registered build is reused only while a fresh host holds its
  snapshot (no fresh host at all: reused as before), and a supersede at
  registration narrowed to rebuildOf-flagged rebuilds (excludes the
  reporting host, records claim.held, moves the leases); buildById and
  buildByRef.

The fork carried earlier ports of rossoctl#818 and rossoctl#819 (d43a7a8 stillHeld;
144314a and e674b3c the pinned-session retire guard and its pruning).
Upstream's versions win. Conflicts, each resolved to upstream's side:
- docs/api/openapi.yaml: the create's reuse paragraph (fresh hosts,
  supersede) and retire's build_in_use wording.
- packages/control-plane/src/ownership.ts: buildSessionsKey's comment
  (retire or reject), indexSession and cascadeDelete's key local, and
  hasActiveSessionOnBuild's comment.
- packages/control-plane/src/sandbox-handlers.ts: stillHeld (fresh hosts,
  heldFresh, no fresh host means reuse) and buildsInUse's comment.
- packages/control-plane/test/activation.test.ts: the pinned-session
  retire test and the pruning test, as upstream has them.
- packages/control-plane/test/template-create.test.ts: the reuse test,
  plus upstream's stale-host and no-fresh-host cases.

After the resolution these five files are byte-identical to upstream's,
so no fork duplicate of the ports remains. CHANGELOG.md is upstream's.
No fork-only file references packages/knative-server.

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.

Retire and reject should be refused while an active Moca Pi session is pinned to the build

4 participants