feat(coding-agent): move_session revoke seals an imported copy (#4003)
Fixes #4003. Part of desktop app item 3137 (tracked internally); desktop#4008 gates its take-back on the move_session_revoke capability.
What
move_session {op:"revoke", sessionPath, moveId} seals the copy a receiving host IMPORTED for a move, after the sender took the session back:
- runs under the move lock (
withSessionMoveLock, the same lock as export/finish):session_heldwhen another process holds the copy,session_open_herewhen this host has it open; - appends
session_movedwith{ move_id, session_id, target: <import's from>, reason: "revoked", moved_at }, so the existing ownership fold makes the file read-only and the write guard refuses later writes withsession_moved_away; - only a file whose latest ownership entry is this move's
session_moved_inis revoked; a repeat for the same move is a no-op that answers the same; - anything else (another move id, the sender's released copy, a never-imported writable session) is refused with
move_mismatch. - capability
move_session_revokeis advertised by multi-session hosts next tomove_session.
Wire: request { type: "move_session", op: "revoke", sessionPath, moveId } -> success data: { ownership: "moved" }; refusals error/errorCode: move_mismatch, session_held, session_open_here, invalid_move_request (missing sessionPath or bad moveId), with errorData.detail.
Import and finish behavior are unchanged (finish's return path now shares one ownershipOf helper with revoke; same value).
Step table and crash fold (head 675f380fcd)
Every durable step on one device, in order. The same table is in src/core/session-move/move-steps.ts (MOVE_STEPS); the code calls moveStep(step) right after each step, and one test faults every row. Rules after every row: at most one writable copy, the record agrees with the files, and a revoke mark is never lost.
| Step | On disk | Receiver record | A crash or throw there must leave |
|---|---|---|---|
import:received | bundle verified; nothing landed | receiving, target recorded | nothing landed: status receiving; a same-folder import lands it, other folders are fenced |
import:stamped | nothing landed; staged copy ready | receiving, target, committing | every import fenced (lands once); status finds no file at the target, so failed and the sender takes it back |
import:renamed | copy landed at the target, writable | still receiving + committing | status or a same-file import confirms it (accepted); a failure here never records failed; other folders fenced |
import:accepted | copy landed | accepted, target (final) | done |
revoke:sealed | receiver copy sealed (session_moved reason revoked) | accepted, mark not yet written | status reads the sealed file and answers revoked, writing the mark; a repeated revoke writes it too |
revoke:marked | receiver copy sealed | accepted + revoked | done |
finish:recorded | sender file records moved / reclaimed; bundle kept | (sender side) | a repeated finish re-queues the dropped input once and removes the bundle |
finish:requeued | input re-queued; bundle still there | (sender side) | a repeated finish finds the input queued and removes the bundle |
- Stamp before the rename:
commitstampscommittingunder the ledger lock after its prepare steps (replacePredecessor, extension state, which land nothing, so a refusal there still recordsfailed) and before the rename. A stamped record never lands again. - Never
failedbeside a landed copy:fail()/settlerefuse to overwrite a stamped record (the probed MEDIUM).statussettles the stamped record from the file at the recorded target. - Revoke mark kept:
confirmLandedkeeps a revoke mark (the LOW), andstatusrestores a mark a crash lost after the seal. - One sweep test (
rpc-move-session-steps.test.ts) faults every row of the table two ways: a crash (the files are restored to that moment, so no catch handler's write survives) and a throw (catch handlers run). For import and revoke it also runs a variant with the receiver's copy deleted. It then replays the client's follow-ups (repeat the failed call, status with and without the path, finish, an import here and in another folder) and asserts the three rules after each. - The suite can fail: removing the settle refusal fails 2 sweep/probe tests, removing the revoke reconcile fails 4, and removing the commit stamp fails 2.
- Residual, in the CHANGELOG: a move an older version started receiving and that never landed cannot be finished; the user copies the session to a new thread.
- Layout: the landing rules moved to
move-landing.tsand the ledger lock tomove-ledger-lock.ts, keepingmove-ledger.tsunder the 250-line ceiling (244).
Lands-once fold (head 48d1f6144c)
- A move lands once (probed M1, conservative fix: fewer states, fewer split-brain cases): an accepted record never lands again, revoked or not, at any target. A deleted copy is gone; the user copies the session to a new thread. This removes the re-land added in 6969bac671, which a lost revoke mark plus a deleted copy could turn into a second writable copy.
- An old-build
receivingrecord with no landing target (probed L1) refuses every new receive (move_fenced). The file it already landed is still accepted through the same-file check (confirmLanded), which runs beforebeginReceive. - L2 (the exact-string target compare fails closed) and L3 (concurrent same-folder imports share one staging directory; pre-existing) are filed as #4016 and not folded in here.
Landing target fold (head 6969bac671)
- The ledger records where a move lands (probed HIGH + MEDIUM, one root cause):
beginReceive(moveId, target)stores the session file the first import lands as, under the ledger lock, and the first claimant wins. While the move is receiving or accepted, any import for another target is refused withmove_fencedbefore anything is written. That covers two imports at once (H1) and an import after a lostacceptedwrite (M1).commit(moveId, target, rename)renames only into the stored target: fromreceiving, or for a re-land, fromacceptedwhen not revoked. Any other case throwsMoveFencedError.- The same-file early return now calls
confirmLanded(moveId, target). A landed file at the stored path proves the commit, so #3998's lost-write path still endsaccepted; a landed file at a different path is fenced. - The stored target never appears in an answer, because records are parsed field by field.
- Re-land of a deleted copy: removed again in 48d1f6144c (a move lands once).
- Sweep: only
import.tslands a session file for a move id (rebindSessionFileruns only insidecommit's callback, into the same target path), and all three of its ledger calls now carry the target.export.tswrites the sender's bundle, not a session file. - An old-build
receivingrecord claims its target on the next import: replaced in 48d1f6144c (refused, except its already landed file).
Import fence fold (head e772ca4768)
- An accepted move lands once (probed MEDIUM; this fixes the whole class, not only the revoked case):
beginReceiverefuses any accepted record, revoked or not, withmove_fenced. An import of the same bundle into another folder is refused and writes nothing, so a second writable copy can't appear on the receiver. This also closes the earlier bug where a plain accepted move could land twice.- Re-importing the move's own file still returns early in
import.ts, beforebeginReceive. - #3998's lost-write path (the record left
receiving) is unchanged.
- Re-importing the move's own file still returns early in
- Seal before flag, on purpose (L1):
revokeseals the copy first and marks the record afterwards. Flag-first would let a sender hearrevokedand reclaim while the receiver's copy is still writable: two writers. Seal-first can only crash into "sealed, record plainaccepted", where nobody is wrongly writable. rpc.md now says a client whoserevokedid not succeed repeats it before acting on any status. - A ledger test (L2) shows a repeat revoke restores
revoked: trueafter the record is deleted or reset to plainaccepted.
Delta review folds (head 243a890ddc)
- the ledger records the revoke (MEDIUM):
revokeaddsrevoked: trueto the move's accepted ledger record under the same move lock, after the seal. The state staysaccepted, so the ledger-final rule is unchanged. A repeat writes it again if a crash lost it, and a repeated import of the same bundle keeps it.statusreports{accepted, revoked: true}with or withoutsessionPath, andcancel/preparereturn the same record. - a revoked answer for a sender already marked moved (LOW-1) is refused with
move_already_moved(new wire code, documented), and nothing changes. statuswith a wrongsessionPath(LOW-2): a file that exists but is not this move's import is refused withmove_mismatch. The file is read in this host's queue for that session file. The cross-process holder lock would refuse while the receiver has the session open, so instead docs/rpc.md says a cross-process race is settled by asking again. A path with no file answers from the ledger.finish.tsJSDoc reflowed (LOW-3).
Review folds (head de1bbf8dd7)
- status after a revoke (MEDIUM 1, lead ruling: additive): the ledger's
acceptedstays final and unchanged.statuswith the copy'ssessionPathanswers{ state: "accepted", revoked: true }when the latest record for that move id on the file is thesession_movedwith reasonrevoked. Old readers seeacceptedas before.revoked: truewins for writability: the write guard refuses every write to that copy (session_moved_away). WithoutsessionPath, the answer comes from the ledger alone and stays plainaccepted, as documented. - finish honors the revoke (lead condition:
revoked: truewins for every reader that decides writability):parseMoveStatuskeeps the flag, andfinishwith{ accepted, revoked: true }is a take-back, never a move. A sender still sealed for the move reclaims it on the same path asdeclined(session_reclaimedwithreason: "revoked", queued input back, under the move lock). A sender that already took the session back is left as it is, and a repeat changes nothing. Plainacceptedstill marks the sender moved. - a used copy is still revoked (MEDIUM 2): turns written after the import are kept, and the copy becomes read-only for good; the client offers "Copy to a new thread". Refusing would leave two writable copies. Documented and tested.
- take-back order (LOW 1): docs/rpc.md says the client sends
cancelfirst (it fences a pending move), thenrevokeif the import already landed; acancelthat answersreceivingwaits onstatus. - restatement test removed (LOW 2); re-imported copy refuses a revoke for the earlier move id with
move_mismatch(LOW 3, new test). session-move-commands.tsis unchanged (248 code lines); the repo has no LOC gate.
Evidence
Step table / crash fold (head 675f380fcd):
- RED (tests-only commit 721e50dda6, run at 48d1f6144c):
Tests 3 failed (3).- The interrupted commit plus a deleted copy re-landed (
success: true). - A failure after the rename recorded
failed(import_failed) beside the landed writable copy. - A same-file re-import dropped the revoke mark.
- The interrupted commit plus a deleted copy re-landed (
- GREEN (675f380fcd): 24 move/protocol-info files ->
Test Files 24 passed (24) / Tests 146 passed (146), including the 32-case step sweep. Root tsc is clean,bun run checkexit 0, biome clean. The mutation checks are listed above. - Static receipt on a fresh clone at 675f380fcd: label
mds-4004-static8,GATES_DONE fail=0: the 24 move/protocol-info files passed, plus tsc,bun run check, build,check-pr-changelog --base 4dd6f3c135PASS and the marker grep;senpi-staticreceiptFILED GREEN ... #4004-675f380fcd.json failures: [] refusals=0/7.
Lands-once fold (head 48d1f6144c):
- RED (tests-only commit 2745354452, run at 6969bac671):
Tests 5 failed | 10 passed (15).- A retry after the landed copy was deleted re-landed, with the move not revoked and with its revoke mark lost (
success: true). - An old-build
receivingrecord with no target let an import into another folder land. - The ledger let an accepted move re-land at its own target and let a target-less
receivingrecord start a receive.
- A retry after the landed copy was deleted re-landed, with the move not revoked and with its revoke mark lost (
- GREEN (48d1f6144c): 22 move/protocol-info files ->
Test Files 22 passed (22) / Tests 111 passed (111);bun run checkexit 0; biome clean. - Static receipt on a fresh clone at 48d1f6144c: label
mds-4004-static7,GATES_DONE fail=0: the 22 move/protocol-info files passed, plus tsc,bun run check, build,check-pr-changelog --base 4dd6f3c135PASS and the marker grep;senpi-staticreceiptFILED GREEN ... #4004-48d1f6144c.json failures: [] refusals=0/7.
Landing target fold (head 6969bac671):
- RED (tests-only commit e58113aeb4, run at e772ca4768):
Tests 3 failed | 12 passed (15).- Two concurrent imports into different folders landed 2 files (
expected length 1, got 2). The test forces the race with a ledger subclass that holds each commit until both imports reach it or one settles: no sleeps. - After a lost accepted write, an import into another folder succeeded.
- A not-revoked retry after the landed copy was deleted was refused.
- Two concurrent imports into different folders landed 2 files (
- GREEN (6969bac671): 22 move/protocol-info files ->
Test Files 22 passed (22) / Tests 108 passed (108). The landing suite passed 5 of 5 repeated runs. Root tsc is clean,bun run checkexit 0, biome clean. The ledger unit tests pass the newtargetargument, and the earlier "accepted is fenced" ledger test now checks the lead's rule: another target is always fenced, the own target only once revoked. - Static receipt on a fresh clone at 6969bac671: label
mds-4004-static6,GATES_DONE fail=0: the 22 move/protocol-info files passed, plus tsc,bun run check, build,check-pr-changelog --base 4dd6f3c135PASS and the marker grep;senpi-staticreceiptFILED GREEN ... #4004-6969bac671.json failures: [] refusals=0/7.
Import fence fold (head e772ca4768):
- RED (tests-only commit c8be48d993, run at 243a890ddc):
Tests 3 failed | 6 passed (9). Both re-import cases (revoked and plain) gotsuccess: trueinstead ofmove_fenced, andbeginReceiveresolved on an accepted record. The L2 repeat-revoke test passes on the current code. - GREEN (e772ca4768): 21 move/protocol-info files ->
Test Files 21 passed (21) / Tests 103 passed (103);bun run checkexit 0; biome clean. - Static receipt on a fresh clone at e772ca4768: label
mds-4004-static5,GATES_DONE fail=0: the 21 move/protocol-info files passed, plus tsc,bun run check, build,check-pr-changelog --base 4dd6f3c135PASS and the marker grep;senpi-staticreceiptFILED GREEN ... #4004-e772ca4768.json failures: [] refusals=0/7.
Delta review folds (head 243a890ddc):
- RED (tests-only commit ff24a3f28e, run at de1bbf8dd7):
Tests 4 failed | 10 passed (14). Status without a path lacked"revoked": true(2 tests), a wrong path got success instead ofmove_mismatch, and a moved sender'sfinishgot success instead ofmove_already_moved. - GREEN (243a890ddc): 20 move/protocol-info files ->
Test Files 20 passed (20) / Tests 99 passed (99); root tsc clean;bun run checkexit 0; biome clean. - Static receipt on a fresh clone at 243a890ddc: label
mds-4004-static4,GATES_DONE fail=0: the 20 move/protocol-info files passed, plus tsc,bun run check, build,check-pr-changelog --base 4dd6f3c135PASS and the marker grep;senpi-staticreceiptFILED GREEN ... #4004-243a890ddc.json failures: [] refusals=0/7.
Finish fold (head de1bbf8dd7):
- RED (tests-only commit 9d0f7c8d3d, run at 81d45f80c6):
Tests 1 failed | 10 passed (11);finishwith{accepted, revoked:true}answered- "ownership": "writable" / + "ownership": "moved". - GREEN (de1bbf8dd7): 19 move/protocol-info files ->
Test Files 19 passed (19) / Tests 96 passed (96);bun run checkexit 0; biome clean. (The GREEN commit also points the RED assertion at thesession_reclaimedentry rather than the requeued input that follows it.) - Static receipt on a fresh clone at de1bbf8dd7: label
mds-4004-static3,GATES_DONE fail=0: the 19 move/protocol-info files passed, plus tsc,bun run check, build,check-pr-changelog --base 4dd6f3c135PASS and the marker grep;senpi-staticreceiptFILED GREEN ... #4004-de1bbf8dd7.json failures: [] refusals=0/7.
Review folds (head 81d45f80c6):
- RED (tests-only commit 8a926ea3e8,
rpc-move-session-revoke.test.ts):Tests 1 failed | 8 passed (9); the failure is the status test (- "revoked": truemissing from the answer). The used-copy and re-import tests pass on the old code: they pin semantics the code already had. - GREEN (81d45f80c6): the 17
session-move*/rpc-move*files plusrpc-multi-session-protocol-info->Test Files 18 passed (18) / Tests 94 passed (94);bun run checkexit 0; biome clean on the touched files. - Static receipt on a fresh clone at 81d45f80c6: label
mds-4004-static2,GATES_DONE fail=0: the 18 move/protocol-info files passed, plus tsc,bun run check, build,check-pr-changelog --base 4dd6f3c135PASS and the marker grep;senpi-staticreceiptFILED GREEN ... #4004-81d45f80c6.json failures: [] refusals=0/7.
Earlier (8e6d9900e3): RED efc2b8bf6f Tests 8 failed (8); GREEN 2 files / 8 tests plus the move set; bun run check 0; check-pr-changelog PASS.
Summary by cubic
Fixes #4003: adds a revoke op to move_session so a receiving host can seal the copy it imported for a move once the sender takes the session back, preventing two writable copies of the same session.
revokeruns under the move lock, refuses held or open sessions, seals the copy with asession_movedentry (reasonrevoked), and marks the ledger recordrevoked: true; a repeat is a no-op and anything else is refused withmove_mismatch.statusanswers{accepted, revoked: true}(with or without the copy's path), andfinishwith a revoked answer is a take-back, never a move; a sender already marked moved refuses withmove_already_moved.- The ledger records the target the first import lands at, so an accepted move never lands a second copy and an import into another folder is fenced with
move_fenced. - Every durable step is settled from the files after a crash or throw (
move-steps.tsstep table, swept fault-by-fault), keeping at most one writable copy and never losing the revoke mark. - Multi-session hosts advertise the
move_session_revokecapability; a record write outside its lock is refused withmove_record_unlocked. - A move an older version started receiving, that never landed, cannot be finished; the user copies the session to a new thread (documented in the CHANGELOG).