Skip to content

Prepend the untrusted-content preamble to every tool that returns message text - #298

Draft
MaxGhenis wants to merge 8 commits into
mainfrom
fix/mcp-preamble-all-message-tools
Draft

MaxGhenis wants to merge 8 commits into
mainfrom
fix/mcp-preamble-all-message-tools

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Four MCP tools returned text taken from message bodies without messagePreamble, the untrusted-content warning that the read tools prepend. This PR adds the preamble to them. It also adds a test that calls every registered tool, so a tool that returns message text without the warning fails CI. Review found three more ways message text reached the model unwarned, and the PR fixes those too:

  • A failed Signal Desktop import put the export in its error.
  • A failed Signal receive put the messages it had printed into the status error.
  • Structured payloads carried bodies with only a true/false flag. Claude Code gives the model the structured payload and drops the text block, so the model saw those bodies with no warning at all.

What changes

Tool Message text in its result Before After
conversation_stats top phrases (list and stats JSON) no preamble starts with it
person_stats top phrases (list and stats JSON) no preamble starts with it
generate_story quotes (truncate(m.Body, 200)), API narrative built from bodies no preamble starts with it
generate_person_story same as generate_story no preamble starts with it
get_conversation message lines preamble after the Conversation: header starts with it
get_person_messages_range message lines preamble after the Messages with … header starts with it
get_conversation, search_messages (structured content) message bodies and transcripts contains_untrusted_text: true only the payload also carries untrusted_content_warning as its first field
get_status (Signal, after receive failures) messages signal-cli printed before failing in last_error, no preamble left out of last_error
import_messages (Signal, on helper failure) the export helper's stdout, i.e. the export in the error, no preamble left out

get_conversation and get_person_messages_range already carried the warning. It now comes before the header too, so every warned result starts with it. That moves the conversation name and the person-match header below the warning. The preamble wording is unchanged everywhere, including the new structured field.

Structured content (internal/tools/tools.go). When a result has structuredContent, Claude Code hands the model that payload as JSON and drops the result's text blocks, where the preamble lives. A reviewer found this in Claude Code 2.1.318's MCP result converter, and I confirmed it there: the converter keeps the text blocks only behind an internal feature flag that defaults to off. So structuredResult now adds untrusted_content_warning to any payload that sets contains_untrusted_text, holding the preamble's text. Go marshals map keys in sorted order, which would put the flag first and the warning later, so the payload marshals with the warning as its first field. Other payloads are unchanged.

Signal receive (internal/signallive/client.go). A signal-cli receive call can print messages, as JSON envelope lines, before it exits non-zero. The loop stores those messages, which is correct. But after repeated failures it also copied the whole output into lastError and the poller's error, and get_status and /api/status show lastError. The failure text now leaves out every line that starts with {, which covers an envelope and one cut off mid-write. It keeps signal-cli's own diagnostics, and the logs get the same filtered text. The account-invalid and poison matchers still read the full output (see the follow-up issue).

Signal Desktop import (internal/importer/signal_desktop.go). The helper runner now returns stdout (the JSON export) separately from stderr instead of combining them.

  • When the helper fails, the error carries its exit status and stderr, and never stdout. Stderr is capped at 2,000 bytes, and an ellipsis marks a cut.
  • When the helper exits cleanly but stdout does not decode, the error carries only the JSON parser's message. That message can quote one character or one number from stdout, but no message text.
  • Side effect: a warning that node writes to stderr on a clean run no longer breaks decoding.

A reviewer reproduced the old leak with the real helper. It needs an abnormal failure after the export is written, such as db.close() throwing or the 30 s timeout firing mid-write.

Tools that need no preamble.

  • generate_viz and render_story report only the path, size, counts, names, date range and timezone. The HTML file they write holds quotes and phrases, but that goes to disk, not into the tool result.
  • The send tools echo only the caller's own message. The legacy send_message and send_to_conversation also return the stored outgoing copy in their structured message summary. No other sender wrote that text, and the preamble's wording ("messages from external senders") would be false on it. Exempting the caller's own text is my reading of the requirement; the test pins the premise (below).
  • React, contacts, routes, drafts, import, transcript and media return no stored message text. Status no longer does either. The test calls each of them.

Test

internal/tools/untrusted_preamble_test.go registers the MCP surface in seven shapes:

  • legacy
  • v2-primary
  • client (transportless; its daemon client refuses every request without dialling)
  • client v2-primary
  • client-live (transportless, with a running app scripted by an in-memory handler; no socket)
  • v2 outbox (Options.V2, the in-app shape with v2 sending on)
  • v2 outbox primary

The three v2-primary shapes read through v2read over a seeded v2 store, as production does. In both outbox shapes the v2 conversations that sends land in hold sender messages, so a send result that quoted its thread would carry the marker.

The test stubs the Google transport hooks. It seeds a marker that only senders wrote into:

  • legacy and v2 incoming bodies;
  • a voice-message transcript;
  • a WhatsApp export line;
  • attachment bytes (in the v2 media store and from the scripted app);
  • the latest-message preview the Google stubs return.

It never passes the marker as an argument. Then it calls every registered handler, and fails when:

  • a result carries the marker anywhere, in any case (text, structured content or metadata), and its first content block doesn't start with messagePreamble;
  • structured content carries the marker without contains_untrusted_text: true and without untrusted_content_warning as its first field, a check the test makes on the marshalled bytes;
  • a message-reading tool stops returning the marker, or, in a v2-primary shape, returns it from the legacy store instead of v2;
  • download_media returns attachment bytes instead of describing them;
  • a tool expected to succeed in that shape returns an error or lacks its success text. Each such tool has its own success text, so an early "no messages found", an empty import or an unsettled delivery cannot pass for a clean result;
  • a legacy send returns a body other than the caller's own message;
  • a registered tool has no entry in the argument table, or an entry names a tool that is no longer registered.

Which tools must succeed where:

  • Sends: legacy, v2-primary, client-live and both outbox shapes. The legacy group send calls the Google client directly, so only the outbox shapes reach it, and client mode refuses group sends by design.
  • Reactions: legacy, client-live and both outbox shapes.
  • Daemon-routed tools: get_status, transcript and media reach their success results in client-live.

TestStructuredContentOpensWithUntrustedWarning pins the field order for get_conversation and search_messages. It also checks that payloads without the flag stay plain maps. In internal/signallive, TestFailedReceiveErrorsLeaveOutPrintedEnvelopes covers the re-pair and reconnect branches, a line cut off mid-write and ApplyPollerFailure. The existing failed-receive tests now also assert that no printed body reaches LastError.

Fail-first. The first version of the test (commit 5c0a696, four shapes) failed against main (d863ba6) on exactly the first six tools in the table, in every shape where each runs: 16 subtests, all on the preamble assertion. No other tool failed.

Mutation checks. Eleven mutants, run one at a time at 5d9e0fc. Each re-adds a fixed leak or weakens a fixture, and each makes a named test fail:

  • Signal receive: no filter (M1); a filter that drops only lines that parse as JSON, which misses a cut-off envelope (M2).
  • Structured warning: missing (M3); present but not first (M4).
  • A send result returns the stored copy instead of the caller's text (M5).
  • A v2-primary read comes from the legacy store (M6).
  • An outbox send result quotes its thread's latest incoming message (M7).
  • download_media returns attachment bytes (M8).
  • The legacy send returns Google's latest-message preview (M9).
  • The Signal Desktop decode error quotes stdout (M10).
  • The generate_viz fixture names nobody, so the tool stops early (M11).

M7 first got past send_group_message, because the v2 group that send lands in held no sender message; the same held for every send in the non-primary outbox shape. Commit ea80249 seeds those conversations. Rerun against it, M7 fails all eight outbox send subtests. One existing randomized property test catches M1 only on some seeds; the two deterministic tests catch it on every run. Logs: ~/reviews/om-pr298-preamble-2026-11-19/runs/r2/mutation.md.

Runs. At the head, go test ./internal/tools/ ./internal/signallive/ ./internal/importer/ passes, and so does go vet ./internal/tools/.

Not covered.

  • The legacy group send's success result: it calls the Google client directly, which the test cannot stub without a production change.
  • In client-live the scripted app has v2 sending on, so the legacy /api/send and /api/send-media routes are not exercised; their results carry IDs only.
  • No test pins the preamble's wording; the test compares results against the constant.
  • Conversation names, contact names and attachment filenames are not message text and stay out of scope. One case breaks that premise: a forged WhatsApp export header can turn body text into a participant name. The follow-up issue has it.

Site

openmessage.ai (#283) lists warned and unwarned tools as of v0.2.9 (site/data/facts.js), and its test checks those lists against the release tag. The site needs no change until a release ships this fix. Releases are Max's call.

Status

Draft. Max decides on merging and releasing.

  • In-session review (commit 5c0a696): three lenses, with two skeptics per finding. It raised no P0 or P1. Commit 10c5fbe fixed its P2s. Records: ~/reviews/om-pr298-preamble-2026-11-19/runs/r1.
  • Independent review, round 1 (Subfleet 20261119-005912-om-pr298-review-ro, head 10c5fbe): REQUEST_CHANGES, with three P1s and four P2s. Each finding was checked against the code (runs/r2/verify-f*.md). All seven are answered:
    • 4a4b060: Signal receive.
    • e5deb16: structured warning.
    • 5d9e0fc: guard hardening, the send-body pin, and the v2, media, client-live and non-dialling daemon fixtures.
    • 4604609: Signal import tests.
    • bfe1dc8: wording corrections in CLAUDE.md.
    • ea80249: outbox send fixtures, from the mutation run.
  • Independent review, round 2: Subfleet 20261119-230102-om-pr298-review-r2, at head ea80249, is queued for the next free review lane.
  • Follow-up: Sender-written text can still reach tool results or transport state outside the message-text paths #299 covers three paths outside this PR's scope: a forged WhatsApp export header that becomes a participant name, Signal receive matchers that read message bodies, and a killed getAttachment call whose partial output can reach an error.

🤖 Generated with Claude Code

MaxGhenis and others added 8 commits November 19, 2026 00:02
…sage text

conversation_stats and person_stats print top phrases from message bodies,
and generate_story and generate_person_story return quotes taken from them
(and an API narrative built from them), all without messagePreamble. They now
open with it. get_conversation and get_person_messages_range wrote a header
before the preamble; it now comes first, so every warned result starts with it.

untrusted_preamble_test.go registers the MCP surface in legacy, v2-primary,
client and client-v2-primary mode, seeds a store with a marker only senders
wrote (bodies, a voice transcript, a WhatsApp export line), calls every
registered handler, and fails when a result carries the marker without
starting with the preamble, when a structured payload carries it without
contains_untrusted_text, when a message-reading tool stops returning the
marker, or when a registered tool has no entry in its argument table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…box paths

A failing Signal Desktop export helper put its combined output in the import
error, and import_messages returns errors to the model verbatim. That output
is the export, every message body in it. The runner now keeps stdout (the
export) apart, and the error carries only stderr, capped at 2,000 bytes.

The preamble test now also registers the v2 outbox shapes (with and without
v2-primary), stubs the Google transport hooks so the send and react tools
reach their success results, and transcribes an incoming message instead of
an outgoing one. Mutants that quote a reacted-to message, echo a transcribed
message's body, or attach thread bodies to a v2 send result now fail it.
Marker matching ignores case. CLAUDE.md names the six shapes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A failed signal-cli receive call can print envelopes before it exits, and
the receive loop copied its whole output into lastError and the poller's
error. get_status and /api/status show lastError without the
untrusted-content warning, so an incoming body could reach the model
unwarned. The failure text now leaves out every line that starts with
'{' (an envelope, or one cut off mid-write); the envelopes are still
stored, and the account-invalid and poison matchers still read the full
output.

Answers review finding 1 on PR 298.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests that a clean helper run whose stderr carries a warning still
decodes (stdout is read on its own), and that an export that does not
decode yields only the parser's message, never message text from it.
The existing helper-failure test now shares its setup.

Answers review finding 7 on PR 298.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
get_conversation and search_messages return message bodies twice: in a
text block that starts with messagePreamble, and in structured content
flagged only by contains_untrusted_text. Some MCP clients, Claude Code
among them, give the model the structured content and drop the text
block, so the model saw bodies with no warning. A payload that sets
contains_untrusted_text now also carries untrusted_content_warning (the
preamble's wording), marshalled as its first field so a reader meets it
before any message text.

Answers review finding 2 on PR 298.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Structured content that carries message text must open with
  untrusted_content_warning, and a new test pins that field order.
- Each tool that must succeed in a shape now has to show its success
  text, not just avoid an error, so an early "no messages" or an
  unsettled delivery no longer passes.
- The v2-primary shapes read through v2read over a seeded v2 store, as
  production does, and each read result must come from that store.
- The v2 outbox primary shape seeds v2 conversations so its native
  sends succeed, and has a media service whose attachment bytes carry
  the marker; download_media must describe them, never return them.
- A seventh shape, client-live, serves a scripted running app from an
  in-memory handler, so the daemon-routed send, react, transcript,
  media and status tools reach their success results.
- The client shapes use a daemon client that refuses without dialling.
- Google conversation stubs return a latest-message preview carrying
  the marker.
- Legacy send results must return only the caller's own message text,
  the premise of their exemption from the preamble.

Answers review findings 2 to 6 on PR 298.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CLAUDE.md now says that structured payloads carrying message text open
with untrusted_content_warning, that send results echoing the caller's
own message need no preamble, that Signal receive and import errors keep
message text out, and what the guard test seeds, which shapes it runs
and where each tool must succeed.

Answers review findings 3 and 4 on PR 298.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A mutation run showed a send result that quoted its thread's latest
incoming message passed the guard for send_group_message, and for every
send in the non-primary outbox shape: the v2 conversations those sends
land in held no sender messages. The v2 group now holds one, and the
non-primary outbox shape seeds its v2 store too. The fixture's remote
thread IDs now equal the conversation IDs, as MirrorConversation writes
them, so the mirror reuses these rows. With the mutant applied, all eight
outbox send subtests fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant