Skip to content

test(mcp): each loop-tool call drains and identity-checks its own audit row — the #2807 case no longer reads its neighbour's (#2952) - #2978

Merged
vybe merged 5 commits into
devfrom
vybe/issue-2952
Sep 23, 2026
Merged

vybe merged 5 commits into
devfrom
vybe/issue-2952

Conversation

@trinity-ability

Copy link
Copy Markdown
Contributor

Fixes #2952

Problem

src/mcp-server/src/access-wiring.test.ts looked up its audit row as "the first run_agent_loop row at an index ≥ auditRows.length", sampling that length at the top of the test. The MCP server's audit POST is fire-and-forget (audit.ts postAudit(entry).catch(() => {})), so the client can hold its callTool result before the stub has recorded the row. On a busy host, the previous test's late row (a successful self loop) lands after the sample and matches first. CI run 35730033664 attempt 1 failed #2807: a refused loop start is audited as a refusal with the refusal was audited as a success: {"tool":"run_agent_loop","duration_ms":1,"success":true} on a PR that touched nothing near the loop tools.

Mechanism (test-only)

Every call made through asAgent().call now drains its own audit row before returning (call() → { out, row }), and that row is checked two ways:

  • Position: the row's index is a call counter captured inside call. A precondition asserts auditRows.length === calls before each call, so the recorder has caught up at every call boundary.
  • Session identity: each session presents its own bearer (${AGENT_KEY}-${name}), and the stub echoes that bearer back as key_name. auditRowAt then asserts row.mcp_key_name === key and row.details.tool === tool.

The drain runs before JSON.parse. If an isError text made the parse throw first, the drain would be skipped and the row would stay in flight for the next call to misread. Both #2807 cases, including the deny-then-allow pair on one session, get their rows from their own calls. Test names and every assertion on the row's contents are unchanged.

audit.ts and all other production code are untouched. The diff is one test file, one learnings fragment and one tests/registry.json row.

Evidence

  • Deterministic red on the original file. With the stub recording its audit row 50 ms late (a temporary perturbation, never committed):
    • Planning run: 5 tests / 3 pass / 2 fail. Subtest 4 failed with CI's exact message, the refusal was audited as a success: {"tool":"run_agent_loop",...,"success":true}. Subtest 5 failed with a stale refusal, the permitted call inherited a stale refusal: {...,"denied":true}.
    • Later runs: subtest 5 red on first (assert.equal(first.details.denied, true)), deterministically ×3 at 50 ms and at 200 ms.
  • Green on the fixed file under the same perturbation: 5/5 ×3 at 50 ms, 5/5 at 200 ms. The guarantee is not tuned to one delay.
  • Mutations, to show the fix can't hide a production regression:
    • M1, refusal label dropped in the audit path → 2 red.
    • M2, two rows per call → 4 red.
    • A constant stub key_name makes every call fail with row N is not this session's, so the identity check is live.
  • Suite: cd src/mcp-server && npm run build && npm test → build clean, # tests 504 / # pass 504 / # fail 0. Verified ×4 in the verify stage and re-run at ship.
  • Access-wiring file: 10/10 consecutive runs green (# pass 5 # fail 0).
  • The flake did not reproduce unperturbed on the build host (12/12 full-suite runs green). The failure is confirmed from CI run 35730033664 attempt 1.

The MCP Server Tests workflow is advisory, not a required check on dev. The local counts above are the proof.

Docs

No requirements / architecture / feature-flow delta — test-only, behaviour-preserving. The audit row's contents and labelling are unchanged; only how the test finds the row changed.

Learnings fragment: docs/memory/learnings/2026-09-22-await-is-not-identity-for-a-fire-and-forget-recorder.md. Rule: a recorder fed by a fire-and-forget side channel needs position + identity, never position alone.

Follow-ups — listed, not filed

🤖 Generated with Claude Code

trinity-ability and others added 5 commits September 23, 2026 07:59
…it row (#2952)

The #2807 transport cases awaited the first `run_agent_loop` row past an
index sampled at test start. That is a position, not an identity: the
previous test never awaited its own fire-and-forget row, so on a busy
host its late `success: true` row was the first match and the refusal
read as audited-as-success (CI run 35730033664 on PR #2936).

Every call through `asAgent().call` now drains the row at its own call
index before parsing the envelope, and checks it for tool and session
(a per-session bearer the stub echoes back as `key_name`). A loud
precondition (rows == calls) makes a change to withAudit's one row per
call break this file on purpose. audit.ts is untouched (#2926).

Evidence: the stub recording its row 50 ms late reds the dev file
(5 tests / 4 pass / 1 fail, 3 of 3 runs; also at 200 ms) and leaves the
fixed file 5/5 (3 of 3 at 50 ms, 1 of 1 at 200 ms). A constant key_name
reds 5/5 with "row N is not this session's". Unperturbed: npm test
504/504, file alone 10/10.

Also registers access-wiring.test.ts in tests/registry.json (it had no row).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… case; the sibling's red is timing-dependent (#2952)

CI run 35730033664 attempt 1 failed only the refused-loop case (464/465);
the fragment claimed the sibling case chained into 'stale refusal' there.
Under the 50 ms stub perturbation the original sibling case goes red on
either 'first' (a late success row) or 'second' (a refusal row) depending
on arrival order — say that instead.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ategory and records its #2952 update (#2952)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…cal, per the merge-train note on the PR

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@vybe
vybe marked this pull request as ready for review September 23, 2026 09:56
@vybe

vybe commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

merge-train: pushed one mechanical commit to this branch — merged dev and rebuilt tests/registry.json from the git stages (dev's catalog + this PR's one mcp-server/src/access-wiring.test.ts row; JSON validated, no duplicates, diff vs dev purely additive). No code changes.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

merge-train: validated individually (3-member queue, below train size); all checks green on the rebuilt head incl. MCP Server Tests.

@vybe
vybe merged commit 6b0ddd9 into dev Sep 23, 2026
25 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

Development

Successfully merging this pull request may close these issues.

2 participants