Skip to content

fix(mcp): audit a returned operational failure as a failure, not a success (#4026) - #4246

Merged
vybe merged 4 commits into
devfrom
AndriiPasternak31/4026-returned-failure-audit
Nov 5, 2026
Merged

vybe merged 4 commits into
devfrom
AndriiPasternak31/4026-returned-failure-audit

Conversation

@AndriiPasternak31

@AndriiPasternak31 AndriiPasternak31 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4026

Problem

The MCP audit wrapper (audit.ts::withAudit) labels a tool call by throw / no-throw plus one out-of-band stamp, and the only stamp was denied (#2807). A tool that returns a failure instead of throwing it was therefore audited success: true. That covered apply_system's 409 refusal report, its 503 / 500 reports and not_available, and every teardown_system refusal, including its 403.

Change

  • types.ts: ToolOutcome.kind is now "denied" | "failed". denied keeps its meaning exactly: authorization refusals only.

  • audit.ts::toolFailed(context, envelope, auditReason?) is the non-denial twin of accessDenied. It stamps {kind: "failed", reason} on the per-call context and returns JSON.stringify(envelope, null, 2), so the caller's JSON is unchanged. The row reads success: false with a short error and no denied key. Rules:

    • A failed stamp never overwrites a denied one.
    • The reason is never empty, because logToolCall drops an empty error.
    • The fallback reason is "Tool call failed", never "Access denied".

    withAudit itself needs no logic change; only its comments changed.

  • apply_system: every non-403 failure is stamped failed:

    • 404/405 not_available
    • JSON object bodies: 400 / 401 / 409 / 422 / 503 / 500
    • non-object bodies
    • a missing key, or a lost connection

    Audit reasons are short, for example HTTP 409: status 'refused'. A backend detail is capped at 200 characters on the row; the caller still receives it in full. The 403 stays denied, byte-identical.

  • teardown_system: the 403 is stamped denied through accessDenied. The backend sends it when teardown is unlicensed, when the caller is not a person, or when every member was not_authorized. Every other returned failure, the 500 failed report included, is stamped failed. Each teardown row names the backend's own cause, for example HTTP 403: status 'failed' for an all-members-refused report or HTTP 404: Not Found (not_available) for an absent route.

  • A 2xx stays success: true whatever the report's own status says (a dry-run invalid, an apply or teardown partial): the call worked, and the report carries the outcome.

  • Docs: architecture/mcp-server.md, feature-flows/system-manifest.md (the apply audit-row table said success: true), feature-flows/audit-trail.md.

Test plan

  • src/tools/systems.apply.test.ts drives both tools through the registered, audit-wrapped server: real createServer, the streamable-HTTP client, a stub backend recording audit rows.
    • Every failure branch is asserted to produce success: false, the exact audit error, and the right denied value.
    • For each branch, the caller's text is pinned byte-for-byte as dev returns it today.
    • New teardown_system cases: 404, 503, 400, the 500 failed report, a 502, a 500 that is not a report, a malformed 500, a dropped connection, a 403 in both forms, and a 200 preview and a 200 partial.
    • Same-session sequences prove no stale marker carries over (403 then 409, 409 then 200).
  • src/audit-denial.test.ts covers toolFailed:
    • row shape {tool, duration_ms, success, error}, with the denied row shape pinned unchanged
    • denied wins, in both orders, and through the catch path (a stamp then a thrown 403 is still a denial)
    • reason precedence, including empty strings
    • no leak onto the shared session

Validation: npx tsc --noEmit exits 0, and npm test in src/mcp-server passes 1182 / 1182. Red first: on dev, 31 of the 50 new assertions fail. In systems.apply.test.ts each red is the row reading success: true. audit-denial.test.ts is red on dev for a different reason: toolFailed does not exist there, so the file fails on its import, not on behaviour. The byte-for-byte text assertions already pass on dev.
Other shape: teardown_system against a system that does not exist, so the 404 comes from the serving route (System 'acme' not found), not from a build without the route. The caller's text is unchanged, and the row reads success: false with no denied and error: "HTTP 404: System 'acme' not found (not_available)", distinct from the absent route's HTTP 404: Not Found (not_available). Case: "404 from a serving route (no such system)" in src/tools/systems.apply.test.ts; it passes.
Mutation: each of these turns the suite red:

  • stamp denied instead of failed
  • drop the denied-wins guard
  • drop the non-empty reason guard
  • unstamp the apply object body
  • stamp teardown's 403 as failed
  • unstamp the teardown failed report
  • unstamp the apply transport path
  • remove the 200-character cap
  • drop the 403's backend-cause reason
  • drop the backend detail from teardown's other reasons

Unverified: not run against a live stack. The audit rows were read from the stub backend that the registered server posts to, not from a running backend's audit table.

Journey Impact: none: an MCP audit-row label change only; the caller-facing results of both tools are byte-identical.

Out of scope

  • Other never-throw MCP tools still return their failures unstamped. Moving them onto toolFailed is a per-tool decision about which branches are denials, so it belongs in its own change: bug(mcp): the other never-throw tools still audit a returned failure as a success #4248.
  • Two findings about teardown_system's caller-facing JSON are left untouched here, because this change keeps that JSON byte-identical:
    • A 403 carrying a failed report loses the per-member report (abilityai/trinity-enterprise#1099).
    • A 404 for an empty membership reads as "not available on this platform" (abilityai/trinity-enterprise#1100).

…ccess (#4026)

The MCP audit wrapper labels a tool call by throw / no-throw plus one
out-of-band stamp, and the only stamp was `denied` (#2807). A tool that
RETURNS a failure instead of throwing it was therefore audited
`success: true`: apply_system's 409 refusal report, its 503/500 reports and
not_available, and every teardown_system refusal.

- ToolOutcome gains a `failed` kind. audit.ts::toolFailed stamps it on the
  per-call context and serialises the envelope exactly as before, so the
  caller's JSON is unchanged. The row reads `success: false` with a short
  `error` and no `denied` key. A `denied` stamp is never overwritten, and the
  reason is never empty (an empty `error` is dropped from the row).
- apply_system: every non-403 failure (404/405 not_available, JSON object
  bodies 400/401/409/422/503/500, non-object bodies, missing key, lost
  connection) is stamped `failed`. The 403 stays `denied`, byte-identical.
- teardown_system: the 403 (unlicensed, human-only, or every member
  not_authorized) is stamped `denied` through accessDenied; every other
  returned failure, the 500 `failed` report included, `failed`.
- 2xx results (a dry-run `invalid`, a `partial` apply or teardown) stay
  `success: true`.

Tests drive both tools through the registered, audit-wrapped server and pin
the caller's text byte-for-byte as it was before this change.
system-manifest.md's apply_system table said a non-403 failure is audited
success: true; audit-trail.md described only the denied stamp. Both now name
the failed kind and toolFailed.
…#4026)

Review follow-ups on the returned-failure stamp:

- An apply failure's reason copied the backend `detail` unbounded (a 5,000-
  character manifest error became a 5,000-character row reason). Reasons now
  come from one helper, httpAuditReason, which caps a detail at 200
  characters. The caller still receives the full detail.
- A teardown 403 recorded the caller's licensing / human-only paragraph as its
  reason, including when the real cause was every member being refused
  (`skipped: not_authorized`). The row now names the backend's own cause.
- Teardown's other failure reasons carry the backend detail before the flags,
  so an absent route ("Not Found") and a serving route that found no such
  system read differently on the row, even though the caller's message does
  not yet tell them apart (trinity-enterprise#1100).
- types.ts no longer claims a denial's reason is never empty; that guarantee
  is toolFailed's only.
- Tests pin three cases that were right but unguarded: an array-valued
  error, an empty report status, and a write to any other session key.

The caller-facing JSON of both tools is unchanged; the byte-for-byte pins
still pass.
@AndriiPasternak31 AndriiPasternak31 changed the title fix(mcp): audit a returned operational failure as a failure, not a success fix(mcp): audit a returned operational failure as a failure, not a success (#4026) Nov 4, 2026
@AndriiPasternak31
AndriiPasternak31 marked this pull request as ready for review November 4, 2026 22:47
@AndriiPasternak31 AndriiPasternak31 self-assigned this Nov 4, 2026
@vybe

vybe commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

merge-train (2026-11-04): validated READY at a5cf703d4. Two notes ride with it, tracked in #4265 rather than blocking here:

  • The string-detail arm of httpAuditReason can put up to 200 characters of manifest text on the append-only audit row when a YAML parse fails. deploy_system's throw path already writes the same error uncapped, so this extends an existing exposure.
  • Only detail is capped. status and the non-ApiError branches are not.

@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: batch validated on train/20261105-0424 (#4266), all gates green.

@vybe
vybe merged commit 5d16d91 into dev Nov 5, 2026
27 of 28 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