Repository navigation
fix(mcp): audit a returned operational failure as a failure, not a success (#4026) - #4246
Merged
Merged
Conversation
…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.
This was referenced Nov 4, 2026
…#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
marked this pull request as ready for review
November 4, 2026 22:47
Contributor
|
merge-train (2026-11-04): validated READY at
|
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 wasdenied(#2807). A tool that returns a failure instead of throwing it was therefore auditedsuccess: true. That coveredapply_system's 409 refusal report, its 503 / 500 reports andnot_available, and everyteardown_systemrefusal, including its 403.Change
types.ts:ToolOutcome.kindis now"denied" | "failed".deniedkeeps its meaning exactly: authorization refusals only.audit.ts::toolFailed(context, envelope, auditReason?)is the non-denial twin ofaccessDenied. It stamps{kind: "failed", reason}on the per-call context and returnsJSON.stringify(envelope, null, 2), so the caller's JSON is unchanged. The row readssuccess: falsewith a shorterrorand nodeniedkey. Rules:failedstamp never overwrites adeniedone.logToolCalldrops an emptyerror."Tool call failed", never"Access denied".withAudititself needs no logic change; only its comments changed.apply_system: every non-403 failure is stampedfailed:not_availableAudit reasons are short, for example
HTTP 409: status 'refused'. A backenddetailis capped at 200 characters on the row; the caller still receives it in full. The 403 staysdenied, byte-identical.teardown_system: the 403 is stampeddeniedthroughaccessDenied. The backend sends it when teardown is unlicensed, when the caller is not a person, or when every member wasnot_authorized. Every other returned failure, the 500failedreport included, is stampedfailed. Each teardown row names the backend's own cause, for exampleHTTP 403: status 'failed'for an all-members-refused report orHTTP 404: Not Found (not_available)for an absent route.A 2xx stays
success: truewhatever the report's ownstatussays (a dry-runinvalid, an apply or teardownpartial): the call worked, and the report carries the outcome.Docs:
architecture/mcp-server.md,feature-flows/system-manifest.md(the apply audit-row table saidsuccess: true),feature-flows/audit-trail.md.Test plan
src/tools/systems.apply.test.tsdrives both tools through the registered, audit-wrapped server: realcreateServer, the streamable-HTTP client, a stub backend recording audit rows.success: false, the exact auditerror, and the rightdeniedvalue.devreturns it today.teardown_systemcases: 404, 503, 400, the 500failedreport, 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 200partial.src/audit-denial.test.tscoverstoolFailed:{tool, duration_ms, success, error}, with thedeniedrow shape pinned unchangedValidation:
npx tsc --noEmitexits 0, andnpm testinsrc/mcp-serverpasses 1182 / 1182. Red first: ondev, 31 of the 50 new assertions fail. Insystems.apply.test.tseach red is the row readingsuccess: true.audit-denial.test.tsis red ondevfor a different reason:toolFaileddoes not exist there, so the file fails on its import, not on behaviour. The byte-for-byte text assertions already pass ondev.Other shape:
teardown_systemagainst 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 readssuccess: falsewith nodeniedanderror: "HTTP 404: System 'acme' not found (not_available)", distinct from the absent route'sHTTP 404: Not Found (not_available). Case: "404 from a serving route (no such system)" insrc/tools/systems.apply.test.ts; it passes.Mutation: each of these turns the suite red:
deniedinstead offailedfailedfailedreportUnverified: 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
toolFailedis 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.teardown_system's caller-facing JSON are left untouched here, because this change keeps that JSON byte-identical:failedreport loses the per-member report (abilityai/trinity-enterprise#1099).