feat(string_replace): apply the rules to tool-call arguments

JE
21h ago
+729-117

Proposal 3 of #1675 (independent of proposal 1, #1676).

Problem. A client runs tool-call arguments, so a rule that holds for the assistant text holds for what the model passes to a tool. string_replace saw only text parts in every phase: a placeholder inside an argument (grep …/<PERSON_53>, def <PERSON_60>(url):) passed a block rule that would have caught the same text in the reply. We measured 37 invented placeholders in one argument reaching the client while both guards reported allow; only Presidio's restore touched arguments, and only for IDs mapped in that request.

Change. In the response phase each string value of the decoded arguments is a text of its own: keys, numbers and the JSON around the values stay as they are, and a < the provider escaped as \u003c is still seen. Arguments that are not a JSON object or array are matched as raw text but never rewritten, so they stay valid JSON. Every on_match mode counts a match in the arguments; replace writes the call back through SetToolArguments. In a stream each call's arguments are a window of their own under stream_lookbehind, like a choice's text, so a match split across two deltas is found when the window closes — block/respond buffer and decide on the assembled completion, as before.

Behaviour change. Tool-call argument deltas used to pass through in every stream mode. They are now subject to the rules. The existing stream test's pass-through expectation is updated to the new behaviour.

Tests. Unit: replace, block and warn on arguments; values only (keys untouched); JSON-escaped placeholder; non-JSON arguments counted, never rewritten; a choice with a match in text and in a call counted once; streamed argument deltas split across chunks, replace and warn. e2e: block, replace and a clean call, streamed and not — a blocked streamed call reaches the client as the in-stream error with none of the arguments. Docs en/de. The argument decoding Presidio already had moves to internal/plugins/jsonargs so there is one copy.

Not in this PR. Proposal 2 — an unmapped decision on the restoring Presidio instance, which knows mapped from unmapped without a regex. Follows separately.

Summary by CodeRabbit

  • New Features
    • String-replacement guardrails now inspect tool-call arguments in responses and streams. In responses, JSON string values can be replaced, blocked, or flagged while keys, numbers, and structure remain unchanged.
    • Stream matching works across split chunks with separate context for each tool call. Replacements are escaped to preserve valid JSON; streamed matching can also rename keys.
    • Non-object and non-array arguments are checked as text but not rewritten. Buffered block and respond decisions use decoded arguments.
  • Documentation
    • Updated guardrail guidance to describe tool-argument matching, stream behavior, and replacement escaping.