package stage import ( "context" "encoding/json" "fmt" "git.a-wan.net/alan/pipelines/internal/opencode" "strings" ) // evidenceBound labels a payload whose redacted text hit the 16 KiB // RedactEvidence cap. RedactEvidence itself keeps the content bounded; this // threshold only marks the preview for callers. const evidenceBound = 16 << 10 // evidencePayload is one labelled, bounded, credential-redacted evidence source. // Missing records a read that could not be retrieved (or was not attempted) so // an absent payload is never mistaken for empty but complete evidence. type evidencePayload struct { Scope string `json:"scope"` Format string `json:"format"` Bounded bool `json:"bounded,omitempty"` OriginalBytes int `json:"original_bytes"` Content string `json:"content,omitempty"` Missing string `json:"missing,omitempty"` Diagnostic string `json:"diagnostic,omitempty"` } // boundEvidence redacts and bounds a single retained payload. It reports a // bounded preview when the source exceeded the evidence bound, and a redacted // missing diagnostic instead of fabricating content when the read failed. func boundEvidence(data []byte, format, scope string, readErr error) evidencePayload { if readErr != nil { return evidencePayload{Scope: scope, Format: format, Missing: RedactEvidence(readErr.Error())} } redacted := RedactEvidence(string(data)) payload := evidencePayload{Scope: scope, Format: format, OriginalBytes: len(data), Content: redacted} if len(data) > evidenceBound || strings.Contains(redacted, "[truncated]") { payload.Bounded = true payload.Diagnostic = "bounded redacted preview only; full retained evidence remains on the execution computer" } return payload } // SessionEvidence only reads retained data on the execution computer. Never // generate, resume, wait, interrupt or delete while investigating a session. // For OpenCode V2 the messages payload uses the same bounded final-assistant // query as the final result readback (finalAssistantReadback), so a small final // assistant result is selected even when large prior tool content exists; the // recent-20 all-message history is not fetched. Every payload is returned as a // labelled bounded/redacted envelope. Native V1 keeps its session/messages // readback and MessageReadbackDiagnostic. Pi's JSONL evidence is handled by // PiEvidence and is never parsed as one JSON object. func (c OpenCodeClient) SessionEvidence(ctx context.Context, id string) (string, error) { if !sessionIDPattern.MatchString(id) { return "", fmt.Errorf("invalid session ID") } connection, err := opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend) if err != nil { return "", err } defer connection.Close() c.connection = connection c.Backend = connection.Backend var session, messages, log []byte var sessionErr, messagesErr, logErr error logDiagnostic := "" readbackDiagnostic := "" unavailable := fmt.Errorf("not attempted: session aggregate unavailable") if c.Backend == "v1" { session, sessionErr = connection.Native(ctx, "get", "/session/"+id, nil) if sessionErr == nil { messages, messagesErr = connection.Messages(ctx, id) readbackDiagnostic = connection.MessageReadbackDiagnostic } else { messagesErr = unavailable } } else { session, sessionErr = c.call(ctx, "get", "/api/session/"+id, nil) if sessionErr == nil { messages, messagesErr = c.call(ctx, "get", "/api/session/"+id+finalAssistantReadback, nil) } else { messagesErr = unavailable } if sessionErr == nil { log, logErr = c.call(ctx, "get", "/api/experimental/session/"+id+"/log?follow=false", nil) if logErr != nil { logDiagnostic = RedactEvidence(logErr.Error()) } } else { logErr = unavailable } } messagesQuery := "native_messages" if c.Backend != "v1" { messagesQuery = finalAssistantReadback } data, err := json.Marshal(map[string]any{ "scope": "latest_bounded_evidence", "backend": c.Backend, "messages_query": messagesQuery, "session": boundEvidence(session, "json", "session_aggregate", sessionErr), "messages": boundEvidence(messages, "json", "latest_assistant_message", messagesErr), "log": boundEvidence(log, "text", "experimental_log", logErr), "log_diagnostic": logDiagnostic, "readback_diagnostic": readbackDiagnostic, }) if err != nil { return "", err } return string(data), nil }