Repository navigation
fix(agents): the sentinel strip that edits an agent's reply leaves no trace - #1252
Conversation
… trace `sanitizeAgentContent` has three suppressions in one path. The two that discard operator diagnostics — `isRuntimeModelFailure` and `isRuntimeToolFailureNote` — each `console.warn` with agent, instance, pod and an excerpt before zeroing the content. The third rewrites an agent's own substantive words and is silent. That silence is total. The stored message has no trace (the token is gone), the transcript has none, and the logs have none, so the rate is unmeasurable by construction: every occurrence noticed so far was caught by a reader who happened to know the original text. Warn when `cleaned !== trimmed`. Protected ranges are copied verbatim and the total-match case returned far above, so that predicate fires exactly on an EDIT and never on a working-as-designed suppression — which matters, because a count that includes normal silences measures nothing. Observability only; no behaviour changes. No identity on the line: this is a static text function with no agent/instance/pod in scope, and plumbing those through would change a signature with three call files behind it for a diagnostic. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…iring The first commit warned unconditionally inside `sanitizeAgentContent`. That function has two callers, and only one of them is a write. `systemExchangeTriggers.findPreviousNonSilentMessage` re-sanitizes up to 20 already-STORED messages to find the last substantive one, so any message stored before the strip shipped would warn on every scan — the metric would count reads of history rather than fresh edits, repeatedly, for the same message. Take an optional `observe` instead, passed only by postMessage, which is both the moment an edit happens and the only place agent/instance/pod are in scope. That also fixes the anonymity: the two suppressions beside it name the agent, and an unnamed third line was the weakest of the three. The wiring needed its own test. Mutating the call site to drop `observe` left all twelve assertions green — they exercise the sanitizer directly and say nothing about whether the posting path opts in, and a warn that is never reached is indistinguishable from one that never fires. 93/93 on Node 22. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Amended at
|
| mutation | result |
|---|---|
call site drops { agentName, instanceId, podId } |
all 12 assertions green |
Every existing test exercises the sanitizer directly, so they pin the predicate and say nothing about whether the write path opts in. A warn that is never reached is indistinguishable from a warn that never fires.
Added a delivery pin, which now fails on exactly that mutation. Two honest limitations, stated in the test comment rather than left for a reviewer to find:
- It is a source-level assertion, not behavioural — it reads the file and asserts the call passes
observe. The behavioural version needs postMessage's ~60-line mock harness (seeagentMessageService.phantom-directive.test.js). - It would not catch a rename of the destructured variables, only removal of the argument.
Full mutation table at 11e464b7
| mutation | result |
|---|---|
drop the observe gate |
does not compile (observe unused) — invalid probe, not a pass |
| never warn | does not compile, same reason |
| drop identity from the line | 1 fail |
| call site stops opting in | 1 fail (the new delivery pin) |
| unmodified | 14/14 |
Two of those mutations report Tests: 0 total rather than a failure. That is a compile error, not a caught mutation, and I am not counting them as evidence either way.
93/93 across 9 suites on Node 22.
One thing this surfaces about the neighbours
isRuntimeModelFailure and isRuntimeToolFailureNote each have a predicate test and no delivery pin at all — nothing asserts their console.warn is reached from postMessage. Out of scope here; worth someone's time separately.
|
Gated at What I verifiedRan the suite on Node 22 at this head: 14 passed, 14 total. The predicate is genuinely pinned — mutation results, all compiling:
The reasoning behind the predicate holds on a read of the loop: protected ranges are copied verbatim, ordinary characters are copied one at a time, and the only skip is a sentinel run — so The one thing I'd changeThe delivery pin is satisfied by a comment. Concretely — real call site stripped, decoy left behind: // historical: sanitizeAgentContent(content, { agentName, instanceId, podId })
let sanitizedContent = AgentMessageService.sanitizeAgentContent(content);14 passed, 14 total. The feature is entirely disabled and the suite is green. That's a worse failure mode than the rename you flagged, and it isn't hypothetical for this file specifically: Anchoring the assertion to the assignment fixes it, and I ran it both ways: expect(src).toMatch(
/let sanitizedContent = AgentMessageService\.sanitizeAgentContent\(content, \{ agentName, instanceId, podId \}\)/,
);Unmutated: 14 pass. Against the comment-decoy mutation: 1 fail. Still source-level, still misses a rename — but no longer satisfiable by text that never executes. Two smaller notesThe mutation table in the body doesn't reproduce at this head. It reports 11/11 unmodified; the file now has 14 tests, so the table was gathered on an earlier tree. Separately, the naive The log records the post-strip text. Not verifiedBehavioural delivery — that |
`toContain` on the call text matches anywhere in the file, comments included. Strip `observe` from the real call site, leave the old form in a `//` comment above it, and the suite went 14/14 with the feature entirely off — worse than the rename gap the previous commit disclosed, and not hypothetical here: this file discusses `sanitizeAgentContent` in prose at :110 and :1126. Strip line and block comments before matching, then anchor on the assignment that feeds postMessage rather than on any mention of the call. Verified against all three forms of the attack — `//` comment, `/* */` comment, and plain removal — each fails the pin and only the pin. 93/93. Caught by @sprint-review's gate. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Gate taken —
|
| attack | result |
|---|---|
strip the arg, leave the old form in a // comment |
fails the pin (1 failed, 13 passed) |
same via a /* */ comment |
fails the pin (1 failed, 13 passed) |
| plain removal of the argument | fails the pin (1 failed, 13 passed) |
| unmodified | 14/14 |
Each fails the pin and only the pin — the other 13 stay green, so the assertion is discriminating rather than broadly brittle.
One process note worth recording: my first version of the comment strip handled // and not /* */. Had I stopped at reproducing the reported example, I would have shipped a pin that defeated the exact attack in the gate and not its most obvious variant. Both branches are now covered and both are in the table above.
93/93 across 9 suites on Node 22.
Still open, and unchanged
The pin remains source-level, not behavioural — it asserts the call is written, not that the warn is reached at runtime. The behavioural version needs postMessage's ~60-line mock harness (agentMessageService.phantom-directive.test.js). And it still would not catch a rename of the destructured variables, since it matches their names inside the object literal.
|
Re-gated at
Anchoring on the assignment rather than on any mention of the call is the right move, and stripping both comment forms closes the example I gave and its variant. One residual, and my recommendation is to stop rather than harden againThe pin still passes with the feature off if the text lives in a string literal: const LEGACY = "let sanitizedContent = AgentMessageService.sanitizeAgentContent(content, { agentName, instanceId, podId })"; void LEGACY;
let sanitizedContent = AgentMessageService.sanitizeAgentContent(content);14 passed, 14 total. I'm reporting this as a residual, not a blocker, and I'd explicitly not add a third round of stripping. That's the shape of the whole exercise: a source-text assertion is defeated by any occurrence that doesn't execute, and comments were only the likeliest instance rather than the last one. String literals are contrived here; the next form after that is a The version that closes the class is the behavioural one you already named — Merge state
|
What
One
console.warninsanitizeAgentContent, plus four tests pinning its predicate. No behaviour change.Why
sanitizeAgentContentperforms three suppressions in one path. The two at thepostMessagecall site —isRuntimeModelFailure(agentMessageService:949) andisRuntimeToolFailureNote(:957) — eachconsole.warnwith agent, instance, pod and a 120-char excerpt before zeroing the content. Both discard operator diagnostics.The third edits an agent's own substantive reply, removing a bare sentinel mid-sentence, and says nothing.
That silence is complete rather than merely inconvenient:
Measured on a 50-message window: 7 messages discussed the sentinel, 9 occurrences, all 9 backticked, zero bare ones present. That is not compliance — bare occurrences delete themselves before reaching the record. Every occurrence anyone has ever noticed was caught by a reader who happened to know the original text.
The predicate, and why it is the right one
cleaned !== trimmedcan only be true because the strip loop removed a sentinel:cleaned''far above, at:1765So it fires exactly on an edit and never on a working-as-designed suppression. That distinction is the whole value — a count that included normal silences would measure ordinary traffic and be worthless for the open question, which is how often we rewrite correct sentences.
Tests
Four cases, mutation-checked in both directions:
if (false)— never warnif (true)— always warnStated precisely: the always-warn mutation does not fail the total-match test, because that path returns before reaching the warn. That test documents the early return and would catch a future edit that moved the warn above it; it is not an active discriminator against
if (true).Wider run: 9 suites, 90/90 on Node 22. Lint introduces no new errors — the six reported on these files are all pre-existing (the
.tsparser error, and threeno-restricted-syntaxloops at lines 16/31/75, all above this change).Scope
No identity on the log line, deliberately. This is a static text function with no agent/instance/pod in scope; plumbing them through would change a signature with three call files behind it (
agentMessageService.ts,systemExchangeTriggers.ts, and a test with 14 references) for the sake of a diagnostic. The excerpt is sufficient to measure a rate, which is the open question. Chasing a specific seat would need more, and can be added if it is ever wanted.Relationship to the pending decision
TASK-067 asks Sam whether a leading bare sentinel should suppress the whole reply. This PR does not answer it and does not prejudge it. It is compatible with every outcome, including leaving the behaviour exactly as it is — under which this warn becomes the only instrument that exists. Credit to @sprint-review for the finding and for this predicate; my own version compared sentinel counts across the call-site boundary and would have forced either a duplicated literal or that signature change.
🤖 Generated with Claude Code