ApplyAgentDefaults appended the auto-injected redact_secrets
tool_response_transform entry after the agent's configured transforms, so
a configured hook ran on unredacted input. Command hooks receive the whole
hook input as JSON on stdin (pkg/hooks/handler.go), so a hook that
archives its input recorded the raw tool output, result metadata, and -
once the hook protocol started carrying them - the inline document
attachments MCP servers return as text resources, even with redaction
enabled.
When the agent-level redact_secrets flag is on (the default) and the
agent configures tool_response_transform hooks, inject a named redaction
pass ahead of them, and keep the auto-injected trailing entry as the
final scrub so a rewrite that reintroduces a secret is still caught
before emission, persistence, and the next LLM call. The leading pass
carries its own Name because the executor deduplicates identical hooks:
without it the trailing pass would collapse into the leading one. With no
configured transforms no leading pass is injected, so the effective
config and the per-tool-result cost of the common case are unchanged.
Tag HookSpecificOutput.updated_tool_documents with omitzero instead of
omitempty, so an empty but non-nil slice - the explicit "remove every
attachment" rewrite - keeps its [] wire form when a Go hook marshals its
output instead of being dropped back to "leave them unchanged".
Tests:
- TestConfiguredCommandHookReceivesRedactedToolDocuments configures a
real command hook that captures its stdin, lets ApplyAgentDefaults
inject the auto redactor, dispatches tool_response_transform with an
inline-text document carrying a PAT, and asserts the captured stdin
has no raw secret (it is the redacted form).
- TestApplyAgentDefaultsOrdersTransformsAroundRedaction pins the
leading/trailing placement and their distinct identity.
- TestToolDocumentsJSONRoundTripDistinguishesNilFromEmpty round-trips
nil (field omitted) vs an empty list (present [], kept as a rewrite).
Refs: docker#5017
----------------------------------------------------------------------
Validation (on this commit):
go build ./... EXIT=0
go vet ./... EXIT=0
gofmt -l <changed files> clean (empty)
go test ./pkg/hooks/... ./pkg/runtime/toolexec/... ok
go test ./... -count=1 EXIT=0, 266 ok packages
golangci-lint run --allow-parallel-runners (2.14.0) 0 issues
go run ./lint . no offenses (3004 files)
go mod tidy --diff EXIT=0 (clean)
Red -> green: on pristine 265b5ff the new command-hook test fails with
the raw PAT in the captured stdin (and the ordering / JSON round-trip
tests fail too); all three pass with this change.
Signed-off-by: PerryLink <255665900+PerryLink@users.noreply.github.com>
--------
DRAFT PR BODY — #5011 (do not post without root sign-off)
Title:
fix(redact_secrets): scrub attachment documents in tool_response_transformFixes #5011
Problem
redact_secretsscrubs tool output on thetool_response_transformleg, but thehook input only carries the tool's textual output (
tool_response) andtool_result_meta. Tool attachments are not part of it.MCP servers return inline text resources as
tools.DocumentContent{Text: ...}(for example
text/markdown). Those documents are emitted to event subscribers andpersisted in the session by
recordToolResponse(chat.BuildToolResultMultiContent),so the secret survives in the session file and in the side consumers (SSE / ACP / TUI).
The next LLM call is re-scrubbed by the
before_llm_callleg, which is why this gapis easy to miss.
Scope decision (from the issue)
#5011 asks for a scope decision: scrub
DocumentContent.Textin the transform leg,or extend the hook protocol to carry attachments. This PR implements the second,
additively:
hooks.Inputgainstool_documents(the result'stools.DocumentContentlist) fortool_response_transformandpost_tool_use.HookSpecificOutputgainsupdated_tool_documents: nil means no rewrite, a presentlist (even an empty one) replaces the attachments; the last non-nil rewrite wins in
the sequential pipeline, matching
updated_tool_response/updated_tool_result_meta.aggregate, and strict-output validation understand the newfield, so existing hooks are unaffected.
recordToolResponse, sothe event stream, the persisted session, the
post_tool_useinput, and the next modelcall all see the same redacted attachments.
redact_secretsnow scrubs each attachment'sname,uri,mime_type, andtextwith portcullis, mirroring the
before_llm_callleg (which already scrubs documentname / MIME type / inline text). Binary
datais opaque base64 and is not scanned;clean documents and binary payloads are preserved byte-for-byte. When nothing matches,
the builtin returns no rewrite, so the runtime's fast path is unchanged.
Tests
New end-to-end regression test
TestDispatcherRedactsInlineTextToolDocuments(
pkg/runtime/toolexec): a tool returns an inline-text document carrying a GitHub PAT;the test asserts the emitted
ToolCallResult, the persisted session message, and thepost_tool_useinput are all redacted, while a clean text document and a binary PDFremain unchanged. Unit tests cover text and non-text attachment fields, the clean no-op
path, and sequential pipeline composition; the strict-output capability table gains an
updated_tool_documentsrow.Docs
Updated
docs/configuration/hooks/index.md(new input/output fields and compositionrules),
docs/configuration/agents/index.mdanddocs/guides/secrets/index.md(redaction coverage).
Validation
Ran locally on
main @ c0062b8awith Go 1.27.4:go build ./...,go vet ./...,gofmt— cleango test ./... -count=1— pass (266 packages ok)go test ./pkg/hooks/... ./pkg/runtime/toolexec— passgolangci-lint run --allow-parallel-runners(v2.14.0) andgo run ./lint .— 0 issuesc0062b8aand passes after the change (red → green)git commit -S -s)Notes / risk
hooks ignore them.
channel that already carries unbounded tool output). Binary
datais included becausea replacement list has to stay complete.
attachments explicitly out of scope for redaction).
Local draft produced by the hub recon run
task-38; not posted.