Skip to content

fix: close reachable invalid state transitions in sessions, refs, trace, mcp and waits - #602

Merged
lahfir merged 2 commits into
mainfrom
laf/state-machine-fixes
Nov 9, 2026
Merged

lahfir merged 2 commits into
mainfrom
laf/state-machine-fixes

Conversation

@lahfir

@lahfir lahfir commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Sessions, refs, trace, MCP and waits could reach invalid states through races, retries and crashes: an ended session reopened, a reaped session recreated, drilled refs deleted at session end, a trace ledger left empty by a kill, a reused MCP id cross-wired after cancel, and wait loops giving up early.
This PR serializes session manifest writes under refstore.lock, takes the lease before reading the manifest, compares refmap bytes before discarding, writes the ledger atomically, keeps cancelled MCP ids occupied, and fixes the preflight disposition and app-terminated matching.
Each regression test fails without its fix; not verified on macOS or Windows.

Fixes #601, fixes #597, fixes #600, fixes #599, fixes #598

…ce, mcp and waits

- hold refstore.lock across session manifest read-modify-writes and take the liveness lease before reading the manifest (#601)
- discard ref scaffolding only when the trace copy is byte-identical (#597)
- write the trace usage ledger atomically (#600)
- keep a cancelled MCP call id occupied until its call finishes (#599)
- mark dispatch-preflight refusals not_delivered and confirm every matching app-terminated event (#598)
- add docs/state-machines.md with states, invariants, transition tables and a findings register

Each regression test fails without its fix. Not verified on macOS or Windows.
@vercel

vercel Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
agent-desktop Ignored Ignored Preview, v0 Nov 7, 2026 4:30am UTC

@lahfir
lahfir marked this pull request as ready for review November 7, 2026 04:32
@lahfir
lahfir requested review from Railly and ctate November 7, 2026 04:33
gshost1 added a commit to gshost1/agent-desktop that referenced this pull request Nov 7, 2026
…ns, refs, trace, mcp and waits (#1)

* fix: close reachable invalid state transitions in sessions, refs, trace, mcp and waits

- hold refstore.lock across session manifest read-modify-writes and take the liveness lease before reading the manifest (vercel-labs#601)
- discard ref scaffolding only when the trace copy is byte-identical (vercel-labs#597)
- write the trace usage ledger atomically (vercel-labs#600)
- keep a cancelled MCP call id occupied until its call finishes (vercel-labs#599)
- mark dispatch-preflight refusals not_delivered and confirm every matching app-terminated event (vercel-labs#598)
- add docs/state-machines.md with states, invariants, transition tables and a findings register

Each regression test fails without its fix. Not verified on macOS or Windows.

* test: drop state machines doc and trim regression tests

---------

Co-authored-by: Lahfir <70215676+lahfir@users.noreply.github.com>
gshost1 added a commit to gshost1/agent-desktop that referenced this pull request Nov 8, 2026
…er (#51)

* fix(trace): pair command.end with its own command.start by seq

The trace reader paired command.start and command.end by
segment:writer_pid:command in one slot. FFI host threads can run the same
command on one session at overlapping times, so start A, start B, end A,
end B reported a false "Unpaired command.end", and start A, start B,
end B with A crashed hid A's lost end.

command.end now carries command_seq, the seq of the scope's own
command.start, and the reader pairs a sequenced end only with that start.
Ends from older traces have no command_seq; they close one open start of
the same segment, writer pid and command, so overlapping legacy scopes pair
by count instead of overwriting each other. The two modes are the explicit
CommandPairing state, held apart in OpenCommands so a legacy end never
closes a start that a sequenced end claims.

The HTML viewer grouped ends by a blind stack pop across writers; it now
closes the frame the same rules pick. The unpaired-start warning names the
start's seq. CommandScope moves to its own file to keep context/mod.rs
under the size cap. The field is additive and documented in the traces
guide. Upstream register item 16 in `vercel-labs#602`.

* fix(trace): make viewer pairing match the reader and stop guessing seqs

Review of the command_seq pairing found the viewer and reader disagreeing
and the reader naming a start it could not know.

The viewer's legacy fallback closed any open frame with the same name,
including a start that a later end claims by command_seq. Its grouping
now lives in viewer_group.js with the reader's rules: a claimed pre-pass,
claimed starts close only by seq, legacy ends close the oldest unclaimed
start of the same writer and command. viewer_group.test.js covers it
with node --test, in the same form as viewer_filter.test.js.

Legacy ends closed the newest start, and the unpaired warning then named
that bucket's leftover seq as if it were known. Legacy ends now close the
oldest start, and a bucket latches "ambiguous" once an end had to choose
among several. Its leftovers are reported as paired by count with no seq.
A seq is named only when every pairing was forced.

The claimed set stays a separate pass: a legacy end can come before the
sequenced end that claims a start of the same command, so one pass could
not leave that start alone. The mixed-format test covers that case.

CommandScope is re-exported from lib.rs, as AGENTS.md requires, instead
of from context. Its start goes through a CommandContext method like its
end. The scope tests share a temp-trace guard that removes the file on
drop.
gshost1 added a commit to gshost1/agent-desktop that referenced this pull request Nov 8, 2026
…gs at startup (#52)

* fix(mcp): forward only server-wide globals and refuse per-command ones

`mcp` forwarded every global flag given on its command line to each tool
call's child. Flags that describe one command broke the server:
`mcp --debug --screenshot x.html` failed every tool other than snapshot and
click from the first call (including `desktop_skills_get`, which the
server instructions say to read first), and every snapshot or click after
the first, because the debug file is create-once. `mcp --wait-for` failed
every tool without a post-action wait the same way.

Each global now has an explicit scope. Server-wide flags (--session,
--agent-id, --headed, --trace, --trace-strict, --verbose) are forwarded as
before. Per-command flags (--debug, --screenshot, --wait-for,
--wait-for-gone, --wait-timeout) and any global without a scope make `mcp`
print an INVALID_ARGS envelope with a suggestion and exit 1 before it
serves anything. A test fails when a new global flag has no scope.

The process test that used --debug to show globals reach every call now
uses an invalid --agent-id, and the server instructions no longer list
--wait-for as a server flag. Item 11 of the upstream register `vercel-labs#602`.

* fix(mcp): refuse bad startup flags on stderr with exit 2 and stop forwarding --verbose

Review of the first change found gaps in how `mcp` refuses its flags:

- The refusal envelope went to stdout, which an MCP client reads as
  JSON-RPC, so the user never saw why the server exited. It now goes to
  stderr through the same JSON writer the CLI uses, and an argument error
  exits 2 like every other argument error.
- An invalid --session or --agent-id was forwarded and failed every call.
  `mcp` now checks both once with the child's own validators.
- A non-UTF-8 value was forwarded after a lossy conversion, which could
  point a child at a different --trace file. It is now refused.
- The long-name check ran before the scope check, so a short-only global
  would have been dropped silently, and a global missing from the scope
  table got the per-command message. Both are now an internal error that
  says the flag is not classified for mcp.
- --verbose only produced child stderr that the server buffers and then
  discards. It is now server-only: it turns on the server's own debug log
  and is not forwarded.

The tests now check the refusal while stdin is still open, prove valid
values reach the child through the trace it writes, and clean up their
scratch directory. The module is renamed to global_forward and its entry
point to collect_forwarded_globals. main.rs gains a small write_response
helper so the refusal reuses the envelope writer instead of copying it.
@lahfir
lahfir merged commit ab7dfb2 into main Nov 9, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants