Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ The codebase follows a **layered architecture with dependency injection** and **
- `breakpoints/anchor-resolution.ts` (`reresolveAnchors`) and `breakpoints/launch-warnings.ts` (pure warning builders)
- `execution/execution-controller.ts` (`ExecutionController`): stepping (table-driven over `STEP_KINDS`), continue, pause, thread list, `waitForStop`; `execution/session-state-wait.ts` (`waitForSessionState`): the one level-triggered wait on a session's state — it re-reads the state in a microtask after each change (`OperationsContext.onStateChange`, fed by `_updateSessionState`), so an entry stop the core auto-continues within one call stack is never reported (issue #849)
- `inspection/stop-anchor.ts` (`selectStopAnchor`: the one verdict on which thread a stop is anchored on, shared by the attach controller and the frame-anchor resolver — a `pause` reported on a thread with no program frame moves to the first sibling whose stack reaches the program and says so; breakpoint/step/exception/entry stops never move; issues #904/#898), `inspection/stop-location.ts` (`describeStopLocation`: the one landing read behind the pause, step, wait_for_stop, attach and launch answers — a `start_debugging`/`restart_debugging` that is paused when it answers reads it too, issue #951 — `location`, and `threadId`/`note` when the answer is anchored off the reported thread or no frame is program code; the landing also carries its resolver read internally as `StopLanding.stackResult`, which `inspection/stop-snapshot.ts` (`composeStopSnapshot`, issue #982) turns into the bounded `snapshot` — exception from the redacted stop record, top ≤3 frames, locals via `extractLocalsFromResolved` under `snapshotLocalsLimits` and the `snapshotLocalsTimeoutMs` tunable — that every PAUSED arrival answer carries (entry stops, dry runs, pending answers and the pause "Already paused" fast path carry none; `DEBUG_MCP_NO_SNAPSHOT` opts out, explicit variable-access mode withholds locals); the handlers hoist the landing fields and the snapshot through `handlers/shared.ts` `hoistStopLanding` — attach included (issue #982 parity) — which also reads the line context and the stale-build sentence and dedupes the snapshot out of the embedded `data`), `inspection/expression-evaluator.ts` (`ExpressionEvaluator`), `jvm/redefine-classes-controller.ts` (`RedefineClassesController`), `mirror/mirror-controller.ts` (`MirrorController`)
- Collaborators see the facade through `OperationsContext` (`operations-context.ts`), whose members are late-bound arrows and getters — never captured references — so reassigning a facade method or writing a tunable on a live instance is visible to them; each takes the narrowest `Pick<>` slice it uses. Collaborator-to-collaborator dependencies are constructor arguments (`DebugLauncher(ctx, proxyLauncher, breakpoints, inFlight)`, `AttachController(ctx, proxyLauncher, breakpoints, pauseCoordinator, inFlight)`) held as captured instance references: their methods resolve at call time, so `vi.spyOn(internals(ops).proxyLauncher, 'start')` intercepts, but reassigning a collaborator field after construction is not observed. The facade owns one shared `InFlightGuard` (`in-flight-guard.ts`, issue #711) that the launcher and the attach controller both take: `startDebugging`, `restartDebugging`, `attachToProcess` and `detachFromProcess` each claim the session before their first await and release it in `finally`, so one launch-shaped call per session at a time; `restartDebugging` replays through the launcher's private `launch()` under its own claim, never through the facade's `startDebugging`. The guard also holds the session's ending in flight (issue #1325): `close_debug_session` goes through the facade's `closeDebugSession` → `AttachController.closeSession`, and a close or a plain detach that arrives while another close or detach of the session is in flight joins it (`InFlightGuard.runEnding`/`ending`), and no second `disconnect` goes out after a confirmed release, also after a detach whose cleanup failed once its release was confirmed (the detach's record keeps the release; the joined close passes a confirmed one, `released` or `program-ended`, to `closeSessionReporting` as `releaseSent` and stops the worker itself; after a detach whose release was not confirmed, which may never have reached the adapter, or that failed before its release, the close sends its own, as one that joined nothing does); a terminating detach is refused instead. While an ending is in flight `InFlightGuard.tryAcquire` refuses every launch, restart or attach, whoever holds the claim ("This session is being closed …"), so an attach that was in flight when the close came and answers meanwhile lets nothing in under it. The close also claims the session when nothing holds it, and is itself never refused: with a launch-shaped call in flight it goes ahead unclaimed. The server's exit and the idle policy call `closeSessionReporting` directly, outside the join. Shared per-request DAP helpers live in `dap-request-helpers.ts`
Each stop has a record (`stop-record.ts`, issue #1362): an id minted when the core applies it, its run, thread, reason, visibility and operation cause. An answer describes one stop, as that stop was when the answer's reads completed. It may be out of date by the time it is serialized or read. The pinned paths check identity independently of state and sample state in the session layer after their reads; no handler refreshes an answer. First reads keep their existing bounds. A re-read gets what remains of 35 s measured from the first landing read; wait_for_stop's timeout bounds waiting only, so reading an arriving stop can take the answer past that timeout. A changed answer omits the landing and snapshot and says why; if the window expired before a retry, it says the stop changed and was not read again. Snapshot diff bases and step hints are staged during composition and committed synchronously when the session layer selects its returned answer; bases never move to an older stop. Launch pins after breakpoint re-send, before dependent reads. This change does not pin get_local_variables, evaluate_expression, get_scopes or get_variables, or change which thread a step resumes; those are issue #1374.
- Session state has one gate, `SessionManagerCore._updateSessionState`, and every call of it names its site and event (`{ site, event, facts? }`, paired by the compiler through `EVENT_OF_SITE`). `src/session/machine/` holds the transition table (`transition-table.ts`) and its shadow check (`shadow.ts`), which judges each write against the table and records a verdict without refusing anything (`MCP_STATE_SHADOW`: `record` by default, `strict` in the unit project, `off`). See `docs/architecture/session-state-machine.md` for the verdicts, the `legacy`/`future` marks, and how to add a site, an event or a row
- Coordinates ProxyManager instances (one per session)
- Handles breakpoint management and queuing
Expand Down
1 change: 1 addition & 0 deletions changelog.d/1348.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**An answer that describes a stop describes one stop** — `wait_for_stop`, the three step tools, `pause_execution`, a launch, restart or attach that answers paused, `hot_reload` when it stops the program, and `get_stack_trace` each read a stop in several requests to the adapter, and a stop that was replaced by another while those ran could leave the answer with one stop's reason beside the other's location, frames or snapshot. The server now keeps a record of each stop, and an answer is pinned to one record and checked against it as it is read (the record is internal: answers gain no field). When the stop changes during the reads, the answer reads the new stop once, if the program is paused at one and time remains (35 s from the first read); when it changes again, or the time is used up, the answer carries no location, frames or snapshot and says which, in a note that begins "The stop changed while this answer was being read" and ends "Call get_stack_trace again to read the current stop." If the program has resumed or ended by then, a wait, step, pause, launch, attach or hot-reload answer says that instead, and `get_stack_trace` keeps the note (and names no last stop while the program is running again). An answer is the stop as it was when its reads completed, and the program can still move before the answer is read. `wait_for_stop`'s timeout bounds the waiting only: a stop that arrives just inside it is read in full, so the answer can come after the timeout by the time that read takes. `get_local_variables`, `evaluate_expression`, `get_scopes` and `get_variables` are not pinned by this change, and which thread a step resumes is unchanged; both are #1374. (#1348, #1362)
69 changes: 68 additions & 1 deletion docs/architecture/session-state-machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,73 @@ which holds the test-only cast and runs no gate, shadow, timestamp update or
listeners; a fake with a type of its own is a plain object and is written
directly.

## The stop record and the pins

The first part of the `stop-record` stage (issue #1362; it closes #1348). A stop gets a record
when the core applies it ([`stop-record.ts`](../../src/session/stop-record.ts)): an id that is
never reused in the process, the run it arrived on (the session's proxy generation), its session,
thread, reason, whether it is shown (`false` for the entry stop the core resumes unseen), and the
operation in flight that it answers (`cause`: a pause armed on this run's proxy, else the step
sent on its thread; the step in flight and the pause intent carry ids for this). The record's
`info` is the session's `lastStop`, the object every answer projects; the record itself is never
emitted. The field for the worker's receipt sequence number (`seq`) is in the type and empty: the
second part stamps it in the worker and applies no observation older than one already applied.

What a session has recorded, the stop it is at and the last stop it showed are readings of these
records: `stopRecordOf` (the record behind `lastStop`, with today's lifetime: a hidden stop is
recorded while its resume is in flight and erased after it), `currentStopOf` (that record while
the session is paused; a reading of the state, so nothing is cleared on a resume or restored by a
refused one), and `lastShownStop` (kept across resumes and hidden stops, reset per run; nothing
reads it yet). Every write of `lastStop` in shipped code is one of the module's three functions,
and every comparison of two stops is by record id, which
[`stop-record-writes.test.ts`](../../tests/core/unit/session/stop-record-writes.test.ts) checks
by name across `src/` and the package sources (its header says what a scan of names cannot see).
Tests give a session a stop through
[`setStopForTest`](../../tests/test-utils/helpers/stop-record.ts); a `lastStop` written around
the module has no record. The shadow is handed the record ids as its stop identities.

**An answer describes one stop, as that stop was when the answer's reads completed.**
It may be out of date by the time it is serialized or read. This is consistency, not
freshness; nothing rewrites an answer after the session layer returns it.

The pinned paths are wait_for_stop (including descendant selection), the three steps,
pause_execution, paused launch/restart and attach answers, pausedChild, a running program
stopped by hot_reload, and get_stack_trace. Each pins before its dependent reads; launch
pins after breakpoint re-send. The landing reads the current inspection anchor, which may
already differ from the reported thread, and discloses that choice. It does not promise
that every request targets the reported thread. Identity checks are independent of state:
the landing checks after its settle and stack read, and the resolver uses the caller's
pin to gate adoption. The session layer samples the root's and selected child's own state
after their reads and selects a paused, running or ended answer there.

First reads retain their existing bounds: the ordinary DAP timeout and proxy margin,
readiness probes, the locals timer, the step-origin bound and the step's stop-wait grace.
There is no new overall deadline on a first read. Only a replacement read takes a Deadline,
with what remains of 35,000 ms measured from the first landing read. The outer retry race
ends at that boundary and disposes its requests. If there is no time left, no retry starts.
wait_for_stop's timeout bounds waiting and tree re-selection, not the landing/snapshot of
a stop that arrived inside it; the answer can therefore arrive after that timeout by the
time those reads take. Cancellation is not reported as expiry of a stop-read budget.

A broken pin is re-read once if another stop is paused and time remains. If that read
changes too, the changed answer omits the landing/frames/snapshot and says the stop changed;
it asks the caller to call get_stack_trace again. If the retry window expires, the note
says either that the stop was not read again or that the re-read did not complete, as
applicable. It does not promise a reason field on attach or same-stop unpinned locals.

Source/build enrichment stays in the handlers, using the accepted location. Those file
reads do not pin a stop or renew its transaction. Snapshot diff bases and step hints are
staged during composition and committed synchronously when the session layer selects the
answer it returns. An older snapshot cannot move the base backwards to an older stop.
A later handler error, serialization failure or cancellation does not undo that selection.
There is no per-call publication scope or serialization-time state refresh.

This change does not pin get_local_variables, evaluate_expression, get_scopes or get_variables, or change which thread a step resumes; those are issue #1374.

Regression tests include stop-pin-regressions, stop-pin-consistency, stop-pin-boundaries,
stop-pin-ordinary, the landing/attach tests and stop-pins-through-tools. The ordinary
fixture also matches 51d5886c and d511ae79, excluding generated ids and timestamps.

## The table

A **phase** is a public state one to one (`created`, `starting`, `running`, `paused`,
Expand All @@ -108,7 +175,7 @@ The stages a mark can name:

| Stage | What it changes |
|---|---|
| `stop-record` | One record per stop, stamped with a sequence number where the proxy receives it, which every answer reads; an event older than one already applied changes no phase. |
| `stop-record` | One record per stop, stamped with a sequence number where the proxy receives it, which every answer reads; an event older than one already applied changes no phase. The record and the answers' pins are in (above); the sequence number and the rule for an older event are the second part. |
| `hidden-stop` | A stop the core resumes without showing it (an entry stop when `stopOnEntry` is off) never enters `paused`. |
| `endings` | Every ending of a run is one record, which collects the evidence of every terminal event that reaches it (the program's exit, `terminated`, the worker's exit). |
| `admission` | Which calls may run while another call or an ending is in flight on the session, and what a refused or unanswered request leaves behind. |
Expand Down
36 changes: 36 additions & 0 deletions docs/tool-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This document provides a complete reference for all tools available in mcp-debug
- [clear_breakpoints](#clear_breakpoints)
3. [Execution Control](#execution-control)
- [The stop snapshot](#the-stop-snapshot)
- [One stop per answer](#one-stop-per-answer)
- [start_debugging](#start_debugging)
- [restart_debugging](#restart_debugging)
- [step_over](#step_over)
Expand Down Expand Up @@ -600,6 +601,41 @@ crashing state costs zero extra tool calls:
landing read failed. Under `DEBUG_MCP_VARIABLE_ACCESS=explicit` the `locals` half is withheld
(exception and frames stay). `DEBUG_MCP_NO_SNAPSHOT=1` turns the snapshot off entirely.

### One stop per answer

**An answer describes one stop, as that stop was when the answer's reads completed.**
It may be out of date by the time it is serialized or read. The pinned paths are
wait_for_stop, the three step tools, pause_execution, paused launch/restart and attach
answers, pausedChild, hot_reload when it stops a running program, and get_stack_trace.
Their identity checks work even after resume or exit. Each session-layer caller samples
state after the read and builds the answer there; handlers do not refresh its state later.

If the stop changes, the answer re-reads it once while another stop is paused and time
remains. A second change omits the landing, frames and snapshot. The note says: "The stop
changed while this answer was being read; its stack and snapshot were omitted. Call
get_stack_trace again to read the current stop." Only shapes that carry a reason/lastStop
retain those fields; attach has no reason field. If the program has resumed or ended by the
time the answer is built, a wait, step, pause, launch, attach or hot_reload answer says that
(running, or the end) and carries no such note; get_stack_trace keeps the note, returns an
empty `stackFrames` list, and names a last stop only when the session is paused or has ended.

First reads keep their existing request/readiness/locals bounds. Only a re-read is bounded
by what remains of 35,000 ms from the first landing read. If none remains, the changed
answer says the stop changed and was not read again; if the retry began but could not
finish, it says so. A first stack-trace timeout remains a failure, not an empty success.
The step's wait for a stop keeps its original grace. wait_for_stop's timeout bounds waiting
only: a stop arriving inside it is read normally, so the answer can arrive after the timeout
by the time it takes to read that stop. Cancellation is not a stop-read-budget expiry.

Source context and stale-build enrichment stay in handlers and describe the answer's own
location, whatever the session does meanwhile. get_stack_trace checks its captured top
frame; hot_reload adds neither source context nor a stale-build note. Snapshot diff bases
and hint delivery are committed when the session layer selects its returned answer, not
when composing an attempt. An older stop cannot replace a newer diff base. Later handler
failure or cancellation is accepted and does not roll back that commit.

This change does not pin get_local_variables, evaluate_expression, get_scopes or get_variables, or change which thread a step resumes; those are issue #1374.

### start_debugging

Starts debugging a script.
Expand Down
5 changes: 2 additions & 3 deletions packages/shared/src/utils/secret-redaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,8 @@ function redactDeep(value: unknown, ancestors: WeakSet<object>, totals: Map<stri

/**
* Deep redacted copy of a stop record for emission in tool answers
* (issue #982). The stored record is never mutated: stop-identity guards
* (the exceptionInfo merge, `lastStop !== before` checks) depend on the
* session's own object staying stable.
* (issue #982). The stored record is never mutated: the server's stop record
* holds the session's own object, and the exceptionInfo merge writes into it.
*/
export function redactStopInfo(stop: SessionStopInfo): SessionStopInfo {
return redactSecretsDeep(stop).value;
Expand Down
Loading
Loading