fix(stage): retain inline audit payloads once without losing native evidence

This commit is contained in:
2026-10-21 16:59:07 +01:00
parent d874a11ad6
commit ceffd5d0d1
3 changed files with 113 additions and 2 deletions
+19 -2
View File
@@ -262,7 +262,11 @@ func renderHTMLMessage(out *bytes.Buffer, index int, message opencode.ExportedMe
fmt.Fprintf(out, "<p class=\"meta\">id: %s | model: %s | agent: %s | created_at: %d</p>\n",
html.EscapeString(message.ID), html.EscapeString(message.Model), html.EscapeString(message.Agent), message.CreatedAt)
for _, part := range message.Parts {
renderHTMLPart(out, part)
if part.Type == protocol.ChatPartTool {
renderHTMLToolWithNativeRecord(out, part, message.Raw)
} else {
renderHTMLPart(out, part)
}
}
// The readable projection is not the complete native record: tools can
// carry inline images and provider-specific metadata absent from ChatPart.
@@ -297,6 +301,10 @@ func renderHTMLPart(out *bytes.Buffer, part opencode.ExportedPart) {
// renderHTMLTool renders a tool call with its name, status, input, result and
// error, escaping every value.
func renderHTMLTool(out *bytes.Buffer, part opencode.ExportedPart) {
renderHTMLToolWithNativeRecord(out, part, nil)
}
func renderHTMLToolWithNativeRecord(out *bytes.Buffer, part opencode.ExportedPart, native []byte) {
name := part.Name
if name == "" {
name = part.ID
@@ -316,7 +324,16 @@ func renderHTMLTool(out *bytes.Buffer, part opencode.ExportedPart) {
if content.Text != "" {
fmt.Fprintf(out, "<pre class=\"output\">%s</pre>\n", html.EscapeString(content.Text))
} else if content.URI != "" {
fmt.Fprintf(out, "<p>%s (%s)</p>\n", html.EscapeString(content.URI), html.EscapeString(content.Mime))
// Inline screenshot/file data is already retained verbatim in the
// complete native record. Printing megabytes of base64 again as a
// paragraph adds no readable evidence and can truncate the whole
// audit. Summarize only when that exact payload is demonstrably in
// this message's native bytes; otherwise preserve the old full output.
if len(content.URI) >= 5 && strings.EqualFold(content.URI[:5], "data:") && bytes.Contains(native, []byte(content.URI)) {
fmt.Fprintf(out, "<p class=\"inline-attachment\">Inline attachment (%s): complete payload retained in the native message below.</p>\n", html.EscapeString(content.Mime))
} else {
fmt.Fprintf(out, "<p>%s (%s)</p>\n", html.EscapeString(content.URI), html.EscapeString(content.Mime))
}
}
}
if part.State.Error != nil {
@@ -0,0 +1,68 @@
package stage
import (
"encoding/json"
"html"
"strconv"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func inlineSession(uri string, native json.RawMessage) opencode.ExportedSession {
return opencode.ExportedSession{ID: "ses_inline", Backend: "v2", Messages: []opencode.ExportedMessage{{
ID: "message_inline", Role: "assistant", Raw: native,
Parts: []opencode.ExportedPart{{Type: protocol.ChatPartTool, Name: "screenshot", State: &protocol.ChatToolState{
Content: []protocol.ChatToolContent{{URI: uri, Mime: "image/png"}},
}}},
}}}
}
func TestSessionExportRetainsLargeInlinePayloadOnceWithoutRaisingBounds(t *testing.T) {
uri := "data:image/png;base64," + strings.Repeat("A", 4<<20)
native, err := json.Marshal(map[string]any{"id": "message_inline", "image": uri, "future_field": "retained"})
if err != nil {
t.Fatal(err)
}
doc, truncated := renderSessionHTML(inlineSession(uri, native), SessionExportRequest{})
if truncated || len(doc) > SessionExportFileLimit {
t.Fatalf("complete native screenshot was truncated by duplicate projection: bytes=%d", len(doc))
}
if strings.Count(doc, uri) != 1 || !strings.Contains(html.UnescapeString(doc), string(native)) {
t.Fatal("exact native payload was lost or duplicated")
}
if !strings.Contains(doc, "complete payload retained in the native message below") || strings.Contains(doc, "TRUNCATED") {
t.Fatal("inline projection did not accurately describe retained complete evidence")
}
}
func TestSessionExportNeverSummarizesInlinePayloadAbsentFromNativeRecord(t *testing.T) {
uri := "data:image/png;base64,AAAA"
for _, native := range []json.RawMessage{nil, json.RawMessage(`{"unrelated":"record"}`)} {
doc, truncated := renderSessionHTML(inlineSession(uri, native), SessionExportRequest{})
if truncated || !strings.Contains(doc, uri) || strings.Contains(doc, "class=\"inline-attachment\"") {
t.Fatal("inline payload without a complete native copy was omitted")
}
}
}
func TestSessionExportInlineProjectionDoesNotCreateExecutableMarkup(t *testing.T) {
uri := `data:text/html,<script>alert(1)</script>`
native, err := json.Marshal(map[string]string{"attachment": uri})
if err != nil {
t.Fatal(err)
}
// Cover both escaped and unescaped native JSON encodings: only the latter
// contains the literal URI and therefore takes the new summary path.
plain := json.RawMessage(`{"attachment":` + strconv.Quote(uri) + `}`)
for _, raw := range []json.RawMessage{native, plain} {
session := inlineSession(uri, raw)
session.Messages[0].Parts[0].State.Content[0].Mime = `image/png"><script>bad</script>`
doc, _ := renderSessionHTML(session, SessionExportRequest{})
if strings.Contains(doc, "<script>") || !strings.Contains(html.UnescapeString(doc), uri) {
t.Fatal("inline attachment became markup or was lost")
}
}
}