diff --git a/docs/notes/session-export-inline-attachments.md b/docs/notes/session-export-inline-attachments.md new file mode 100644 index 00000000..4c03d10e --- /dev/null +++ b/docs/notes/session-export-inline-attachments.md @@ -0,0 +1,26 @@ +# Complete visual audit exports without duplicate inline payloads + +Run34 visit3 retained a complete, untruncated native session (71 messages, +4,196,297 decoded native bytes), but the HTML audit exceeded the8MiB render cap. +The readable tool projection printed47 inline `data:` attachment URIs as text, +duplicating3,752,634 bytes already stored in complete native message records. + +The renderer now summarizes an inline attachment in the readable projection +ONLY when its exact URI bytes also occur in that message's retained native +record. The complete native JSON remains unchanged and HTML-escaped. If the +native record is absent or does not contain the payload, the original full +projection is kept; there is no evidence loss or fabricated completeness. + +No source/render/artifact bound is raised. All remain at their existing limits, +and genuinely oversized exports still report partial/truncated. No HTML is +executed from the attachment; MIME labels and native data stay escaped. + +Read-only scratch export of the exact original visual-review session now +produces4,676,544 bytes,71 messages, `truncated:false`. Production originals and +checkpoints were not overwritten by this proof. + +Regressions cover a4MiB inline payload retained exactly once, fallback when no +native copy exists, and escaped/unescaped hostile native JSON/MIME values. The +full Go suite passes with the unrelated notification-test URL env isolated. +Installation remains deferred until a quiescent service window; no active run +may be interrupted just to activate this correction. diff --git a/internal/stage/session_export.go b/internal/stage/session_export.go index 2c752c5c..f7562b2d 100644 --- a/internal/stage/session_export.go +++ b/internal/stage/session_export.go @@ -262,7 +262,11 @@ func renderHTMLMessage(out *bytes.Buffer, index int, message opencode.ExportedMe fmt.Fprintf(out, "
\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, "%s\n", html.EscapeString(content.Text)) } else if content.URI != "" { - fmt.Fprintf(out, "
%s (%s)
\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, "\n", html.EscapeString(content.Mime)) + } else { + fmt.Fprintf(out, "%s (%s)
\n", html.EscapeString(content.URI), html.EscapeString(content.Mime)) + } } } if part.State.Error != nil { diff --git a/internal/stage/session_export_inline_test.go b/internal/stage/session_export_inline_test.go new file mode 100644 index 00000000..87249d51 --- /dev/null +++ b/internal/stage/session_export_inline_test.go @@ -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,` + 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">` + doc, _ := renderSessionHTML(session, SessionExportRequest{}) + if strings.Contains(doc, "