Skip to content

fix(mocactl): render another device's newer turn as its own on resume - #692

Merged
cwiklik merged 2 commits into
rossoctl:mainfrom
cwiklik:fix/483-resume-foreign-turn-split
Nov 5, 2026
Merged

cwiklik merged 2 commits into
rossoctl:mainfrom
cwiklik:fix/483-resume-foreign-turn-split

Conversation

@cwiklik

@cwiklik cwiklik commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Problem

A resume's attach replays the session's latest turn (spec §4.2), which need not be the turn
this device left open — another device may have run a newer one while this device was away. Those
frames were appended into the still-open turn, so the other device's reply rendered as the
continuation of our half-written one, sitting under our prompt.

Fixes #483 (follow-up to #471).

Fix

The transcript already records the open turn's id, so compare it against the replayed turn
frame's id. When they differ:

  • settle our turn where it stopped — reply final, unfinished tools marked interrupted
  • show a notice (another device's turn) before the replay, so what follows is not read as the
    answer to the prompt above it

Settling is also what keeps the live area honest: an open assistant block would swallow the other
turn's text, and an unsettled tool would pin every later block in the live area for the rest of the
session. For the same reason a terminal frame now settles the open detachable turn, which the
resume-time interrupted marking skips — restarting, owner_lost or a cancel mid-tool could
otherwise leave a result-less tool open forever.

Display only: nothing new is asked of the control plane or the harness.

Changes

File What
render/blocks.ts openTurnId(transcript), startsForeignTurn(state), OTHER_DEVICE_TURN_NOTICE
views/useSession.ts tracks the open turn id; splits on a mismatching turn frame; settles on a terminal
app.tsx passes the transcript's open turn id through the resume's attach

Testing

Written test-first; each test was watched failing for the right reason before the implementation
(reverting the app.tsx wiring reproduced the bug as OUR HALFTHEIR REPLY in the rendered frame).

  • blocks.test.ts — openTurnId across prompt+turn / bare turn / finished / pre-detachable
    transcripts; startsForeignTurn settles and notices; the other turn renders as its own
  • use-session.test.tsx — the split on a newer turn; a regression guard that the catch-up on our
    own
    turn still continues in place (feat: detachable turns — re-attach to a running turn after mocactl quits (#471) #481 I1); a still-open tool is interrupted on owner_lost,
    restarting and cancelled
  • app.test.tsx — the whole resume path, transcript id through to rendered blocks

vitest run in packages/mocactl: 1669 passed. Three failures (LeaveTurnOverlay,
CredentialsOverlay, trailing-slash onboarding) are waitFor/timeout flakes under load that also
reproduce on a clean main tree. tsc --noEmit clean; prettier clean.

Assisted-By: Claude Code

@cwiklik
cwiklik requested a review from pdettori as a code owner November 5, 2026 14:08
@cwiklik cwiklik added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 5, 2026

@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 approach is right: comparing the replayed turn frame's id with the transcript's open turn, settling our turn on a mismatch, and settling on every terminal. The tests cover both the two-device split and the same-turn catch-up. One must-fix, inline: the open-turn ref outlives the turn it describes when that turn ends without a terminal frame, so this device's next turn is labeled as another device's.

Areas: mocactl render/useSession/app + tests. No .claude/.vscode changes. 1 commit, DCO signed. CI 17/17 green.

Assisted-By: Claude Code

// own, rather than appending it into our still-open reply.
if (f.type === 'turn') {
const ours = openTurn.current;
openTurn.current = f.turnId;

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: openTurn.current is only cleared by a terminal frame (L146). These paths leave it set:

  • attach-none: the resumed turn is turn_not_found, or an ended turn frame was caught up on
  • turn-end without a terminal: a dropped stream, an abort-cancel, or "the harness ended the turn without a result"

In each case the next prompt's own turn frame (t2) is compared against the stale t1, and startsForeignTurn puts "another device's turn" between the user's prompt and their own reply. The most direct way to hit it: resume an open-detachable turn the server no longer has. attach-none with missed: true shows INTERRUPTED_TURN_NOTICE, which tells the user to send "go on". That "go on" reply is then labeled as another device's.

Suggest clearing the ref in attach-none, turn-end and turn-start, so only an attach's first turn frame is ever compared. Also add a use-session test for attach-none → submit → no notice.

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.

Fixed in 8f73d83 — you were right, and it was worse than one path.

The ref is now cleared on all three ways out of a turn that aren't a terminal frame:

  • attach-none — turn_not_found, or catching up on an already-ended turn
  • turn-end with no terminal — a dropped stream, or an abort reported through the end alone
  • turn-start — this device starting its own turn, which no catch-up of the previous one can continue

So only an attach's first turn frame is ever compared against it, as you suggested.

Your repro is now a test: resume an open detachable turn the server no longer has, get INTERRUPTED_TURN_NOTICE, take its advice and send "go on" — the reply came back with another device's turn wedged between the prompt and its answer. It failed with exactly that extra notice block before the fix. A second test covers the turn-end-without-terminal path the same way.

The existing #483 tests still hold unchanged: an attach emits attach-start, never turn-start, so clearing on turn-start leaves the two-device split alone. 17/17 in use-session.test.tsx, 181/181 across the four core suites, tsc --noEmit clean.

A resume's attach replays the session's LATEST turn (spec §4.2), which need
not be the one this device left open: another device may have run a newer
one while this device was away. Its frames were appended into the still-open
turn, so the other device's reply read as the continuation of our half-written
one, under our prompt.

The transcript already records the open turn's id, so compare it against the
replayed `turn` frame's id. When they differ, settle our turn where it stopped
— reply final, unfinished tools marked interrupted — and show a notice before
the replay so the turn that follows is not read as the answer to the prompt
above it.

Settling is also what keeps the live area honest: an open assistant block
would swallow the other turn's text, and an unsettled tool would pin every
later block in the live area for the rest of the session. For the same reason
a terminal frame now settles the open detachable turn, which the resume-time
marking skips: `restarting`, `owner_lost` or a cancel mid-tool could otherwise
leave a result-less tool open forever.

Display only: nothing new is asked of the control plane or the harness.

Fixes rossoctl#483

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
Review follow-up on rossoctl#483: `openTurn` was only cleared by a terminal frame, so
it outlived the turn it described on every other path out of that turn. Three
of them:

- `attach-none` — the resumed turn is `turn_not_found`, or an already-`ended`
  turn frame was caught up on. The blocks are settled, the ref is not.
- `turn-end` with no terminal — a dropped stream, or an abort the harness
  reports through the end alone.
- `turn-start` — this device starting a turn of its own, which no catch-up of
  the previous turn can continue.

In each case the next turn's own `turn` frame was compared against the stale
id and `startsForeignTurn` put "another device's turn" between the user's
prompt and their own reply.

The most direct repro: resume an open detachable turn the server no longer
has. The `attach-none` shows INTERRUPTED_TURN_NOTICE, which tells the user to
send "go on" — and that reply came back labelled as another device's.

Only an attach's first `turn` frame should ever be compared against the ref,
which is what clearing it on these three paths leaves.

Two use-session tests, one per leak path. The existing rossoctl#483 tests still hold:
an attach emits `attach-start`, never `turn-start`, so the two-device split is
unaffected.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik
cwiklik force-pushed the fix/483-resume-foreign-turn-split branch from 8f73d83 to 1efb388 Compare November 5, 2026 16:26
@cwiklik

cwiklik commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

@ingpaolodettori-dev ready for another look — the must-fix is addressed in 1efb3888, and the branch is now rebased on current main (0695305c).

The ref is cleared on all three paths out of a turn that aren't a terminal frame, as you suggested:

path how the turn ended
attach-none turn_not_found, or catching up on an already-ended turn
turn-end without a terminal a dropped stream, an abort-cancel, or the harness ending the turn with no result
turn-start this device starting its own turn, which no catch-up of the previous one can continue

So only an attach's first turn frame is ever compared against it.

Your repro is the first of two new use-session tests, verbatim: resume an open-detachable turn the server no longer has, get INTERRUPTED_TURN_NOTICE, take its advice and send "go on". It failed RED with exactly the block you predicted:

    "notice",     <- the interrupted-turn notice
    "user",       <- "go on"
+   "notice",     <- "another device's turn"   (the bug)
    "assistant",

The second covers the turn-end-without-terminal path the same way, using a dropped stream.

Clearing on turn-start does not affect the two-device split: an attach emits attach-start, never turn-start, so the existing #483 tests pass unchanged.

Verification on the rebased head: 198/198 across use-session, app, blocks, session-manager and turn-reattach-seams; tsc --noEmit clean; prettier clean; CI 17/17 green on the pre-rebase head.

@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 review at 1efb388 (a rebase of 8f73d83 onto main 0695305).

git range-diff shows both commits are = against d9c5e7c/8f73d83, and main changed none of the six files this PR touches between the old base 38aa214 and 0695305. So the rebase changes no code.

The earlier must-fix is resolved: openTurn.current is now also cleared on turn-start, attach-none and a turn-end that has no terminal (useSession.ts), so only the first turn frame of a resume attach is compared against a non-undefined ref. There are regression tests for both leak paths (the attach-none → "go on" repro, and turn-end with no terminal from a dropped stream). The existing #483 split tests are unaffected because an attach emits attach-start, never turn-start.

No new issues. Signed-off: yes (2/2).

cwiklik added a commit to cwiklik/moca that referenced this pull request Nov 5, 2026
A detachable turn POST that meets another device's turn gets
`409 turn_in_progress`, and the client queues a conflict attach plus a
resend of the prompt. Quitting or switching sessions at that moment runs
`retire()`, whose `clearQueue()` drops every queued prompt -- and for a
409 the queue is the only place that prompt lives, since `runTurn` skips
recording it (`if (!conflicted) record()`). It was lost without a word.

Note the issue's "Current state" is stale on one point: it says the
prompt "has already been written to the local transcript" and so "looks
answered". The rossoctl#471 final review changed that. The prompt leaves no
trace at all, so the bug is durability, not disambiguation -- the
session's auto-title went with it.

Now the leave paths record it as a new `unsent` transcript entry before
the queue is cleared, the leave overlay says the prompt will not be
sent while the user can still choose to stay, and the resume shows it
under a warning notice and offers it back in the input, ready on Enter.
Never auto-sent: the other device's answer may have changed the
question.

Esc keeps today's behavior exactly. A withdrawal is the user taking
their own prompt back, so it owes a later resume nothing -- hence a
dedicated `unsentRecorded` flag rather than clearing `conflict`, which
also decides whether a cancel withdraws or cancels remotely.

Block placement needed widening for this: an unsent prompt and its
notice sit below the turn above them, like a queued prompt, so the
replay of the other device's reply keeps filling its own block instead
of opening a second one underneath. `TranscriptStore.load` already
skips record kinds it does not know, so older clients read these
transcripts unchanged.

Item 4 of the issue (a notice line before the other device's turn)
ships in rossoctl#692 and is deliberately not repeated here.

Closes rossoctl#479

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik
cwiklik merged commit 0729604 into rossoctl:main Nov 5, 2026
23 of 24 checks passed
cwiklik added a commit to cwiklik/moca that referenced this pull request Nov 5, 2026
A detachable turn POST that meets another device's turn gets
`409 turn_in_progress`, and the client queues a conflict attach plus a
resend of the prompt. Quitting or switching sessions at that moment runs
`retire()`, whose `clearQueue()` drops every queued prompt -- and for a
409 the queue is the only place that prompt lives, since `runTurn` skips
recording it (`if (!conflicted) record()`). It was lost without a word.

Note the issue's "Current state" is stale on one point: it says the
prompt "has already been written to the local transcript" and so "looks
answered". The rossoctl#471 final review changed that. The prompt leaves no
trace at all, so the bug is durability, not disambiguation -- the
session's auto-title went with it.

Now the leave paths record it as a new `unsent` transcript entry before
the queue is cleared, the leave overlay says the prompt will not be
sent while the user can still choose to stay, and the resume shows it
under a warning notice and offers it back in the input, ready on Enter.
Never auto-sent: the other device's answer may have changed the
question.

Esc keeps today's behavior exactly. A withdrawal is the user taking
their own prompt back, so it owes a later resume nothing -- hence a
dedicated `unsentRecorded` flag rather than clearing `conflict`, which
also decides whether a cancel withdraws or cancels remotely.

Block placement needed widening for this: an unsent prompt and its
notice sit below the turn above them, like a queued prompt, so the
replay of the other device's reply keeps filling its own block instead
of opening a second one underneath. `TranscriptStore.load` already
skips record kinds it does not know, so older clients read these
transcripts unchanged.

Item 4 of the issue (a notice line before the other device's turn)
ships in rossoctl#692 and is deliberately not repeated here.

Closes rossoctl#479

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
webchang added a commit to webchang/moca that referenced this pull request Nov 6, 2026
Upstream since v0.10.0: mocactl fixes for unsent prompts and resumed turns
(rossoctl#692, rossoctl#694) and the MOCA -> moca rename as a name (rossoctl#695). One conflict,
docs/index.md: upstream's renamed table, with our template onboarding row.

The rename touches prose only where a deployment depends on it: no Secret,
ConfigMap, label, selector, env var, image or config path, and no
provenance record or stream name changes; ocp-sbx and ocp-combined render
byte-identically to b6b15a6.

mocactl's sandbox-service contract test now covers the template lifecycle
methods SandboxApiClient added (retire, tags, edit, owner, quota), each
checked against docs/api/openapi.yaml.

Typecheck, lint, Go, harness 817, control-plane 1184, relay 228,
knative-server 443, mocactl 1718 (with --testTimeout=30000), the sbx and
onboarding shell suites pass.

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.

mocactl: a resume that catches up on another device's newer turn appends it into the open turn (follow-up to #471)

3 participants