Skip to content

fix(control-plane): pin a queued turn's model and sandbox tier to its record (#388) - #788

Merged
cwiklik merged 2 commits into
rossoctl:mainfrom
cwiklik:feat/388-m2-turn-grants
Nov 12, 2026
Merged

cwiklik merged 2 commits into
rossoctl:mainfrom
cwiklik:feat/388-m2-turn-grants

Conversation

@cwiklik

@cwiklik cwiklik commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Closes nothing yet — this is #388's M2 item 4 only. See "What is still open" below.

The defect

POST /internal/turn-credentials read sandboxTier and model from the live session while
credentialName and cfg came from the turn record. So a PATCH /v1/sessions/{id} between
submission and dequeue moved an already-queued turn onto the new model.

updateSession's own rule — a turn already running keeps its model — cannot cover it, because the
turn has not started: an async turn's exchange can be an hour after its submission.

The change

The turn record now carries model, modelLimits and sandboxTier, copied at submission for the
same reason cfg and credentialName are, and the exchange reads all four from it. The session is
still read for the owner compare and the tombstone; nothing the turn spends comes from it.

  • model and modelLimits are pinned together. The limits describe that model, so an old id
    against new limits would send the wrong max_tokens upstream.
  • sandboxTier is pinned raw, not resolved. sessionTier still applies the deployment's
    current default to a turn that pinned none — the choice is the submitter's, the default is the
    deployment's (viewTier's rule) — so a resolved pin would keep naming a tier on a deployment that
    has since removed them. sessionTier's parameter narrows to Pick<SessionRecord, 'sandboxTier'>
    so a TurnRecord gets the identical reading with no second copy of the rule and no production
    call-site churn.
  • model / sandboxTier are required-with-'', exactly as cfg is, so the compiler named
    every construction site instead of letting one default silently.
  • modelLimits is re-sanitized on read, as OwnershipIndex.get does for the session's own copy:
    the field reaches the harness as a turn's max_tokens, and a half-written hash must not become
    one.

The dequeue response shape is unchanged, so turn-claim.ts needed no edit.

Tests

  • Two acceptance tests in turn-exchange.test.ts drive the real submitTurn rather than the
    queuedTurn fixture — what is under test is the copy onto the record, which a fixture that writes
    the fields itself would pass with the copy removed. One switches the session's model with a
    PATCH after submission; one covers the tier, including that a record pinning none still resolves
    the deployment default.
  • A round-trip test in the shared store contract, so the fake and real Redis must agree: a
    modelLimits that came back as a string from one of them would reach the harness as max_tokens.
  • Both negative-checked by reverting the fix they guard — they fail.

Docs

docs/api/openapi.yaml (both response field descriptions), docs/reference/http-api.md (replacing
#714's caveat), and spec §5.1 (three table rows plus an amendment blockquote in the file's existing
style).

What is still open on #388

The other three M2 items — both audiences' grants (MI1 §6.1), turn_exp and refresh, and lifting
the tenancy gate — all require MI1 S2, which does not exist in the repo: no moca-grant,
inferenceGrant, sandboxGrant, MOCA_GRANT_TTL_S, turn_exp or moca-egress outside spec
markdown. The async fan-out spec's own sequencing table names S2 as M2's prerequisite, and MI1's S2
table includes a new moca-egress Go service plus a Go grant verifier. #388 stays open.

Known limit

A turn already on the queue across this one rollout reads back model: '' / sandboxTier: '' and
runs on the deployment defaults rather than its session's choices. Flagged with a shortcut:
comment at turn-store.get, naming the upgrade (a live-session fallback keyed on
h.model === undefined) if a deployment ever needs a queue to survive an upgrade unchanged.

Verification

Repo-wide make typecheck, make fmt and make lint clean. control-plane 1220 passed / 3 skipped
with the real-Redis contract running; knative-server, supervisor, mocactl, sandbox-relay,
config-bundle, ibac-stub, k8s-sandbox, sandbox-client, work-queue, session-backend all green.

Assisted-By: Claude Code

`/internal/turn-credentials` read `sandboxTier` and `model` from the LIVE
session while `credentialName` and `cfg` came from the turn record, so a
`PATCH /v1/sessions/{id}` between submission and dequeue moved an
already-queued turn onto the new model. `updateSession`'s own rule -- a turn
already running keeps its model -- cannot cover it, because the turn has not
started: an async turn's exchange can be an hour after its submission.

The record now carries `model`, `modelLimits` and `sandboxTier`, copied at
submission for the reason `cfg` and `credentialName` are, and the exchange
reads all four from it. The session is still read for the owner compare and
the tombstone; nothing the turn SPENDS comes from it.

- `model` and `modelLimits` are pinned TOGETHER: the limits describe that
  model, so an old id against new limits would send the wrong `max_tokens`.
- `sandboxTier` is pinned RAW. `sessionTier` still applies the deployment's
  current default to a turn that pinned none -- the choice is the submitter's,
  the default is the deployment's (`viewTier`'s rule), so a resolved pin would
  keep naming a tier on a deployment that has since removed them. Its
  parameter narrows to `Pick<SessionRecord, 'sandboxTier'>` so a `TurnRecord`
  gets the identical reading with no second copy of the rule.
- `model`/`sandboxTier` are required-with-`''` like `cfg`, so every
  construction site is named by the compiler rather than defaulted silently.

The dequeue response shape is unchanged, so the worker needs no edit.

rossoctl#388 M2 item 4. Items 1-3 (the two MI1 §6.1 grants, the deployment-wide
`moca-egress` token, lifting the tenancy gate) remain blocked on MI1 S2,
which the async spec's own sequencing table names as M2's prerequisite and
which exists nowhere in the repo yet.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik
cwiklik requested a review from pdettori as a code owner November 12, 2026 20:59
@cwiklik cwiklik 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

The fix is correct and well scoped. submitTurn now copies model, modelLimits and sandboxTier onto the turn record, and /internal/turn-credentials reads all three from the record. A PATCH /v1/sessions/{id} therefore can no longer move a turn that is already queued. The acceptance tests go through the real submitTurn, and the store contract round-trips the new fields, so the copy itself is covered. Read-back limits are re-sanitized, and the pre-rollout '' fallback is called out with a shortcut: comment.

I have one suggestion and no must-fixes. The "tier is pinned raw" wording overstates what gets stored (see the inline comment): createSession already writes the resolved default into the session, so on a tiered deployment the turn pins the default that was current at session creation.

Author: cwiklik (CONTRIBUTOR — returning external)
Areas reviewed: TypeScript (control-plane src + tests), Docs (OpenAPI, http-api reference, spec §5.1)
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (17/17)

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

*/
modelLimits?: ModelLimits;
/**
* The session's OWN tier choice, copied at submission; '' when it named none.

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 record's sandboxTier is not really "the session's OWN tier choice, raw". createSession (handlers.ts:879) already resolves it when it stores the session: const sandboxTier = requestedTier ?? tiers?.default ?? '';. So on a deployment that declared tiers when the session was created, a session that requested no tier stores tiers.default, and submitTurn copies that resolved value here. If the operator later changes default (say container → microvm), the turn still runs on the old default and does not get the deployment's current one. The record only reads '' when the session was created on a deployment with no tiers, which is the case the t-untiered test hand-writes through queuedTurn.

The code behaves sensibly: it pins what the session holds, just as the model pin does. But the rationale here, the spec §5.1 row and blockquote (lines 314/326), and the comment in turn-exchange.ts:180 all promise "a turn that chose none still gets the deployment's current default at dequeue", and that only holds for untiered-at-creation sessions.

Suggested fix: either reword these to "the session's stored tier (resolved at session creation when tiers were declared; '' otherwise)", or, if the raw-choice semantics are the real intent, have createSession store requestedTier ?? '' and leave resolution to sessionTier/viewTier. The second option changes stored session data, so it probably belongs in its own PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Correct, and I took option 1 — aad594ed.

createSession resolving the tier at handlers.ts:879 makes "raw" wrong in four
places, so all four now say the record pins the session's stored tier: this doc,
the turn-exchange comment, spec §5.1 (table row and the amendment blockquote), and
the openapi.yaml description. Option 2 stays out of this PR, as you suggested.

The correction also closed a gap I had under-documented. If the record's tier is ''
the old line handed the worker the deployment default, and for a pre-#388 record —
no tier field at all — that is a different tier than the session's. select-sandbox.ts
treats that as reason: 'retiered' and starts the workspace empty, so the rollout
window could lose a session's work. The dequeue now resolves an unpinned tier against
the session:

const tier = sessionTier(turn.sandboxTier ? turn : rec, deps.config.sandboxTiers);

rec is already loaded two checks above for the owner compare, so it costs no extra
read, and for an untiered-at-creation session it is the same answer the old line gave
(the session stores '' too). turn-store.get's shortcut: comment is narrowed to the
model, which keeps that gap — a turn answered by the wrong model, not a lost workspace.

The tier test's first two assertions could not fail, since a session's tier is immutable
(updateSession refuses every field but model); it now also asserts the unpinned case,
which fails with the fallback removed (expected 'container' to be 'microvm').

Assisted-By: Claude Code

Review of rossoctl#788 found the record's `sandboxTier` is not the submitter's raw
choice: `createSession` already resolves it (`requestedTier ?? tiers?.default
?? ''`), so a turn pins the default that was current when its SESSION was
created. Four doc sites promised the opposite -- that a turn pinning no tier
still picks up the deployment's current default -- and they are reworded to
what is stored.

That correction is also what makes the dequeue's own gap fixable in one line.
A record written before rossoctl#388 carries no tier and reads back as '', and handing
the worker the deployment default instead of the session's own tier retiers the
session's sandbox -- `select-sandbox.ts` then starts its workspace empty. So the
exchange resolves an unpinned tier against the session (`rec`, already loaded
two checks above, so no extra read). For a session created while the deployment
declared no tiers the two are the same answer, since the session stores '' too.

The tier test now guards that read rather than only the copy: its first two
assertions cannot fail, because a session's tier is immutable (`updateSession`
refuses every field but `model`). Removing the fallback fails the new one.

`turn-store.get`'s shortcut comment is narrowed to the model, which keeps the
rollout-window gap -- a wrong answer, not a lost workspace.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>

@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

All checks pass. Ready for human review. The fix is correct and well scoped. /internal/turn-credentials now reads model, modelLimits and sandboxTier from the turn record, so a PATCH /v1/sessions/{id} made while a turn is still queued no longer moves that turn onto the new model. For a record that pinned no tier, the session-tier fallback stops a pre-#388 record from retiering the sandbox. Two minor suggestions are inline.

Author: cwiklik (CONTRIBUTOR — returning external)
Areas reviewed: TypeScript (control-plane), tests, Docs (OpenAPI, http-api reference, spec §5.1)
Agent/IDE config (.claude/.vscode): none
Commits: 2 commits, all signed-off: yes
CI status: passing (k8s-kind-e2e, deploy-scripts, deploy-scripts-bash32 and one check job still pending at review time)

Verified on head aad594e: tsc --noEmit clean, prettier --check clean on the changed files, and turn-exchange/exchange/turn-store suites 94/94 passing. The Redis-backed suites could not run here because no local Redis was available. Mutation checks: putting the rec.model read back fails "keeps the model it was submitted with…", and dropping the session fallback (sessionTier(turn, …)) fails "keeps the tier it was submitted with…". The probes were reverted afterwards.

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

// session's. Only turns already on the queue during that one rollout are affected, and the
// consequence is one turn answered by the wrong model. Upgrade to a live-session fallback keyed
// on `h.model === undefined` if a deployment ever needs a queue to survive an upgrade unchanged.
// The TIER has that fallback already (`turn-exchange.ts`), because there the same gap retiers

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 shortcut: explains why the model has no live-session fallback while the tier does. That is reasonable. But the asymmetry is visible to operators: after a rollout, a pre-#388 queued turn runs on the deployment's default model and its session's tier. The fallback would be small: have get keep model as undefined when the hash field is absent, and fall back to rec.model/rec.modelLimits only in that case at the dequeue. Both fields would then behave the same during an upgrade window. If you keep the shortcut, consider adding a one-line note to the rollout/upgrade docs so operators know a queue drain before upgrade avoids it.

Comment thread docs/api/openapi.yaml
the model the TURN RECORD pinned at submission, with the limits recorded when
it was chosen; absent when the submission named none, and the worker then uses
its deployment default. Pinned, so switching the session's model while this
turn is still queued does not move it onto the new one.

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: The response shape is unchanged, but what /internal/turn-credentials returns for model/sandboxTier has changed (record, not live session). A short line under the 1.3.0 entry in docs/api/CHANGELOG.md, or the next entry, would record that for worker implementers. Per change-policy decision 5, the changelog tracks contract changes.

@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.

The follow-up resolves the "raw tier" point from the previous head. turn-record.ts, turn-exchange.ts, spec §5.1 and openapi.yaml now all say the record pins the session's stored tier. That tier is already resolved by createSession.

The new dequeue line sessionTier(turn.sandboxTier ? turn : rec, …) is correct, and it is a real improvement:

  • rec is the session already loaded for the owner compare, so there is no extra read.
  • For a session created untiered, the session also stores '', so the answer matches the old line.
  • For a pre-#388 record with no tier field, it now resolves to the session's own tier. The old line used the deployment default, which select-sandbox.ts would treat as retiered and start the workspace empty. That would have been data loss during the rollout window.
  • The test now asserts the unpinned case ('microvm', not 'container'), and the comment is honest that the first two assertions only pin the copy, because a session's tier is immutable.

The narrowed shortcut: in turn-store.ts (model only: one wrong-model answer, no data loss) is a fair trade. I agree with clawgenti's two open notes (a rollout/CHANGELOG line, and whether the model should get the same fallback). Neither blocks.

Assisted-By: Claude Code

@cwiklik
cwiklik merged commit 86d6350 into rossoctl:main Nov 12, 2026
17 checks passed
webchang added a commit to webchang/moca that referenced this pull request Nov 13, 2026
Upstream main's rossoctl#788 (a queued turn pinned to its submission's model and
tier, via rossoctl#775 → rossoctl#789) meets this branch's templated queued turns.
Conflicts, both kept:
- turn-exchange.ts: the tier and the model are read from the turn
  record (rossoctl#788); the session's template, seed and build are still read
  from the session, which writes them once at creation.
- turn-exchange.test.ts: both describe blocks.
- openapi.yaml: rossoctl#788's record-pinned sandboxTier text, and this branch's
  sandboxTemplate, sandboxSeed, sandboxSeedSource and sandboxBuildId.

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.

4 participants