fix(opencode): read V2 session exports directly from authenticated API

This commit is contained in:
2026-10-20 20:40:57 +01:00
parent 074b5bb087
commit 1b8d0f2a75
7 changed files with 339 additions and 37 deletions
+1
View File
@@ -1411,6 +1411,7 @@ func stepIndex(data chatEvent) int {
// "role", model as "modelID", timing as "created") with the parts under
// "parts".
type nativeChatMessage struct {
Raw json.RawMessage `json:"-"`
ID string `json:"id"`
Type string `json:"type"`
Agent string `json:"agent"`
+70 -21
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"time"
@@ -21,29 +22,53 @@ var ErrEmptySessionExport = errors.New("session recorded but native export conta
var errTransientExport = errors.New("native session export temporarily unavailable")
var errSessionIdentity = errors.New("native export session identity mismatch")
// nativeExportResult is one bounded `opencode export` readback. envelope is the
// stdout prefix the CLI wrote, capped at the caller's limit; truncated is true
// when the CLI wrote more than the cap, so the prefix is not valid whole JSON.
// nativeExportResult is one bounded native export readback. envelope is capped
// at the caller's limit; truncated explicitly distinguishes a size-limited
// prefix from complete JSON.
type nativeExportResult struct {
envelope []byte
truncated bool
}
// exportNative runs the read-only session export CLI once with stdout capped at
// limit bytes. It is the single export invocation shared by Connection.Messages
// and ExportSession; the returned envelope is validated by the one
// validNativeExport check both call. The subcommand follows the backend's
// published interface: the native v1 CLI exposes top-level `export <id>`, while
// the installed v2 CLI exposes it as `session export <id>`. It never writes to,
// resumes or mutates the session, and it does not wait for session idleness.
func exportNative(ctx context.Context, executable, directory, backend, id string, limit int) (nativeExportResult, error) {
// exportNative reads the existing V2 endpoint directly (CLI stdout silently
// cuts large exports at 256 KiB), retaining the top-level export CLI for V1.
// It never starts a backend, resumes a session, pairs, or mutates stored data.
func exportNative(ctx context.Context, executable, directory, backend, id string, limit int, connections ...*Connection) (nativeExportResult, error) {
if !sessionID.MatchString(id) {
return nativeExportResult{}, fmt.Errorf("invalid session ID")
}
args := []string{"export", id}
if backend == "v2" {
args = []string{"session", "export", id}
var c *Connection
if len(connections) > 0 {
c = connections[0]
} else {
var err error
c, err = sessionExportConnection(directory)
if err != nil {
return nativeExportResult{}, err
}
}
if c == nil || c.URL == "" || c.Backend != "v2" {
return nativeExportResult{}, fmt.Errorf("session export requires the existing V2 endpoint")
}
body, err := c.HistoryNative(ctx, "/api/experimental/session/"+id+"/export?sanitize=false")
if err != nil {
if c.LastHTTPStatus == 429 || c.LastHTTPStatus == 503 {
err = errors.Join(errTransientExport, err)
}
return nativeExportResult{}, err
}
defer body.Close()
data, err := io.ReadAll(io.LimitReader(body, int64(limit)+1))
if err != nil {
return nativeExportResult{}, fmt.Errorf("session export response interrupted")
}
if len(data) > limit {
return nativeExportResult{envelope: data[:limit], truncated: true}, nil
}
return nativeExportResult{envelope: data}, nil
}
args := []string{"export", id}
cmd := CommandContext(ctx, executable, args...)
cmd.Dir = directory
stdout := &boundedOutput{limit: limit}
@@ -126,6 +151,7 @@ type ExportedPart struct {
// ExportedMessage is one decoded turn. Every role is kept, not only
// user/assistant, so the export is the session the harness stored.
type ExportedMessage struct {
Raw json.RawMessage
ID string
Role string
Model string
@@ -153,12 +179,15 @@ type ExportedSession struct {
// writes, no resumption and no idleness wait. Empty readbacks and explicitly
// temporary failures get at most three attempts within the caller's deadline;
// wrong identity and permanent failures never retry.
func ExportSession(ctx context.Context, executable, directory, backend, id string) (ExportedSession, error) {
// An optional existing connection pins V2 readback to the caller's exact
// endpoint/auth context. Otherwise only configured endpoint/private local
// service state is read; there is no CLI/service-start fallback.
func ExportSession(ctx context.Context, executable, directory, backend, id string, connections ...*Connection) (ExportedSession, error) {
if !sessionID.MatchString(id) {
return ExportedSession{}, fmt.Errorf("invalid session ID")
}
for attempt := 0; attempt < 3; attempt++ {
session, err := exportSessionOnce(ctx, executable, directory, backend, id)
session, err := exportSessionOnce(ctx, executable, directory, backend, id, connections...)
session.Attempts = attempt + 1
if err == nil {
return session, nil
@@ -181,9 +210,9 @@ func ExportSession(ctx context.Context, executable, directory, backend, id strin
panic("unreachable")
}
func exportSessionOnce(ctx context.Context, executable, directory, backend, id string) (ExportedSession, error) {
func exportSessionOnce(ctx context.Context, executable, directory, backend, id string, connections ...*Connection) (ExportedSession, error) {
session := ExportedSession{ID: id, Backend: backend}
exported, err := exportNative(ctx, executable, directory, backend, id, MaxSessionExport)
exported, err := exportNative(ctx, executable, directory, backend, id, MaxSessionExport, connections...)
if err != nil {
return session, fmt.Errorf("native session export failed: %w", err)
}
@@ -193,9 +222,7 @@ func exportSessionOnce(ctx context.Context, executable, directory, backend, id s
return session, err
}
session.Messages, session.Truncated = exportedMessages(records), true
if len(session.Messages) == 0 {
return session, ErrEmptySessionExport
}
// Even a cut inside the first turn is partial evidence, never settling.
return session, nil
}
messages, ok := validNativeExport(exported.envelope, id)
@@ -214,7 +241,7 @@ func exportSessionOnce(ctx context.Context, executable, directory, backend, id s
if identity != "" && identity != id {
return session, errSessionIdentity
}
if identity == id && errors.As(err, &syntax) && strings.Contains(syntax.Error(), "unexpected end") {
if backend != "v2" && len(exported.envelope) != 262144 && identity == id && errors.As(err, &syntax) && strings.Contains(syntax.Error(), "unexpected end") {
return session, errTransientExport
}
return session, fmt.Errorf("native export returned invalid session evidence")
@@ -273,6 +300,7 @@ func exportedMessages(records []nativeChatMessage) []ExportedMessage {
messages := make([]ExportedMessage, 0, len(records))
for _, record := range records {
message := ExportedMessage{
Raw: record.Raw,
ID: record.messageID(),
Role: record.role(),
Model: record.modelID(),
@@ -290,6 +318,18 @@ func exportedMessages(records []nativeChatMessage) []ExportedMessage {
if len(message.Parts) == 0 && message.Role == protocol.ChatRoleUser && record.Text != "" {
message.Parts = append(message.Parts, ExportedPart{Type: protocol.ChatPartText, Text: record.Text})
}
var fields map[string]json.RawMessage
_ = json.Unmarshal(record.Raw, &fields)
var files []json.RawMessage
_ = json.Unmarshal(fields["files"], &files)
for _, file := range files {
message.Parts = append(message.Parts, ExportedPart{Type: "file", Raw: file})
}
// Non-chat events (system, selection, compaction, etc.) must retain
// their real payload even when they have no text/content array.
if len(message.Parts) == 0 {
message.Parts = append(message.Parts, ExportedPart{Type: record.role(), Raw: record.Raw})
}
messages = append(messages, message)
}
return messages
@@ -300,6 +340,10 @@ func exportedMessages(records []nativeChatMessage) []ExportedMessage {
// info.id is an error so an unrelated session is never exported.
func decodeExportPrefix(data []byte, wantID string) ([]nativeChatMessage, bool, error) {
decoder := json.NewDecoder(bytes.NewReader(data))
return decodeExportPrefixObject(decoder, wantID)
}
func decodeExportPrefixObject(decoder *json.Decoder, wantID string) ([]nativeChatMessage, bool, error) {
open, err := decoder.Token()
if err != nil {
return nil, false, fmt.Errorf("native export returned invalid session evidence")
@@ -323,6 +367,11 @@ func decodeExportPrefix(data []byte, wantID string) ([]nativeChatMessage, bool,
}
key, _ := keyToken.(string)
switch key {
case "data":
if sawIdentity || sawMessages {
return nil, false, fmt.Errorf("ambiguous native export envelope")
}
return decodeExportPrefixObject(decoder, wantID)
case "info":
var info struct {
ID string `json:"id"`
+132
View File
@@ -0,0 +1,132 @@
package opencode
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func exportAPI(t *testing.T, body string, status int) (*Connection, *int) {
t.Helper()
calls := new(int)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
*calls++
user, pass, ok := r.BasicAuth()
if !ok || user != "opencode" || pass != "test-export-password" {
t.Error("missing endpoint authentication")
w.WriteHeader(401)
return
}
if r.Method != "GET" || r.URL.Path != "/api/experimental/session/ses_ok/export" || r.URL.Query().Get("sanitize") != "false" || r.URL.Query().Get("directory") != "/workspace" {
t.Errorf("unexpected export operation: %s %s", r.Method, r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
fmt.Fprint(w, body)
}))
t.Cleanup(srv.Close)
return &Connection{Backend: "v2", URL: srv.URL, Directory: "/workspace", password: "test-export-password"}, calls
}
func TestV2ExportHTTPPreservesLargeRealParts(t *testing.T) {
image := strings.Repeat("a", 400<<10)
body := `{"data":{"info":{"id":"ses_ok"},"messages":[` +
`{"id":"m1","type":"user","text":"inspect","files":[{"mime":"image/png","data":"` + image + `"}]},` +
`{"id":"m2","type":"assistant","agent":"survey","model":{"id":"luna","providerID":"local"},"content":[` +
`{"type":"reasoning","text":"observed"},{"type":"tool","name":"screenshot","state":{"status":"completed","input":{"path":"screen"},"content":[{"type":"file","mime":"image/png","data":"` + image + `"}]}},` +
`{"type":"text","text":"after the old CLI cap"}]}]}}`
c, calls := exportAPI(t, body, 200)
session, err := ExportSession(context.Background(), "/must-not-run-cli", "/workspace", "v2", "ses_ok", c)
if err != nil || session.Truncated || session.Attempts != 1 || *calls != 1 || len(session.Messages) != 2 {
t.Fatalf("large HTTP read failed: messages=%d truncated=%v attempts=%d calls=%d err=%v", len(session.Messages), session.Truncated, session.Attempts, *calls, err)
}
if string(session.Messages[0].Parts[1].Raw) != `{"mime":"image/png","data":"`+image+`"}` {
t.Fatal("inline user image not preserved")
}
m := session.Messages[1]
if m.ID != "m2" || m.Agent != "survey" || m.Model != "luna" || len(m.Parts) != 3 || m.Parts[0].Text != "observed" || m.Parts[2].Text != "after the old CLI cap" || !strings.Contains(string(m.Parts[1].Raw), image) || !json.Valid(m.Raw) {
t.Fatal("real tool/reasoning/identity lost")
}
}
func TestV2ExportHTTPFailuresDoNotSettleForever(t *testing.T) {
cutPrefix := `{"info":{"id":"ses_ok"},"messages":[{"type":"user","text":"`
cutAtCLICap := cutPrefix + strings.Repeat("x", 262144-len(cutPrefix))
for _, tc := range []struct {
name, body string
status int
}{
{"wrong-session", `{"data":{"info":{"id":"ses_other"},"messages":[]}}`, 200},
{"missing-identity", `{"data":{"messages":[]}}`, 200},
{"auth", `{"error":"private response"}`, 401},
{"not-found", `{}`, 404},
{"old-cli-cut", cutAtCLICap, 200},
} {
t.Run(tc.name, func(t *testing.T) {
c, calls := exportAPI(t, tc.body, tc.status)
s, err := ExportSession(context.Background(), "/must-not-run", "/workspace", "v2", "ses_ok", c)
if err == nil || s.Attempts != 1 || *calls != 1 || strings.Contains(err.Error(), "private response") {
t.Fatalf("permanent failure not bounded/sanitized: attempts=%d calls=%d err=%v", s.Attempts, *calls, err)
}
})
}
}
func TestV2ExportHTTPBoundedWrappedPrefix(t *testing.T) {
for _, initial := range []string{"", `{"id":"m1","type":"assistant","content":[{"type":"text","text":"first"}]},`} {
body := `{"data":{"info":{"id":"ses_ok"},"messages":[` + initial + `{"type":"user","text":"` + strings.Repeat("x", MaxSessionExport) + `"}]}}`
c, calls := exportAPI(t, body, 200)
s, err := ExportSession(context.Background(), "unused", "/workspace", "v2", "ses_ok", c)
if err != nil || !s.Truncated || *calls != 1 || len(s.Messages) != map[bool]int{true: 1, false: 0}[initial != ""] {
t.Fatalf("bounded wrapped prefix: messages=%d truncated=%v calls=%d err=%v", len(s.Messages), s.Truncated, *calls, err)
}
}
}
func TestV2ExportHTTPRecoveryIsBounded(t *testing.T) {
for _, status := range []int{200, 503} {
c, calls := exportAPI(t, `{"data":{"info":{"id":"ses_ok"},"messages":[]}}`, status)
s, err := ExportSession(context.Background(), "unused", "/workspace", "v2", "ses_ok", c)
if err == nil || *calls != 3 || s.Attempts != 3 {
t.Fatalf("HTTP %d recovery is not bounded: attempts=%d calls=%d err=%v", status, s.Attempts, *calls, err)
}
}
}
func TestV2ExportExistingServiceDiscoveryAndEndpointIsolation(t *testing.T) {
c, calls := exportAPI(t, `{"data":{"info":{"id":"ses_ok"},"messages":[{"type":"user","text":"real"}]}}`, 200)
stateDir := t.TempDir()
t.Setenv(OpenCodeServiceStateEnvKey, stateDir)
t.Setenv(EndpointEnvKey, "")
t.Setenv(EndpointPasswordFileEnvKey, "")
t.Setenv(EndpointUsernameEnvKey, "")
if err := os.Mkdir(filepath.Join(stateDir, "opencode"), 0700); err != nil {
t.Fatal(err)
}
data, _ := json.Marshal(map[string]string{"url": c.URL, "password": c.password})
path := filepath.Join(stateDir, "opencode", "service.json")
if err := os.WriteFile(path, data, 0600); err != nil {
t.Fatal(err)
}
s, err := ExportSession(context.Background(), "/never-start-cli", "/workspace", "v2", "ses_ok")
if err != nil || len(s.Messages) != 1 || *calls != 1 {
t.Fatalf("existing service read: %v", err)
}
t.Setenv(EndpointEnvKey, "http://127.0.0.1:1")
if _, err := ExportSession(context.Background(), "/never-start-cli", "/workspace", "v2", "ses_ok"); err == nil || *calls != 1 {
t.Fatal("credential crossed endpoint boundary")
}
t.Setenv(EndpointEnvKey, "")
if err := os.Chmod(path, 0644); err != nil {
t.Fatal(err)
}
if _, err := ExportSession(context.Background(), "/never-start-cli", "/workspace", "v2", "ses_ok"); err == nil || *calls != 1 {
t.Fatal("public service credential accepted")
}
}
+73
View File
@@ -0,0 +1,73 @@
package opencode
import (
"encoding/json"
"fmt"
"os"
"strings"
)
// sessionExportConnection reads existing endpoint/auth configuration only. It
// never discovers by starting a CLI, serves a new backend, or pairs credentials.
// Callers already holding the exact connection should pass it to ExportSession.
func sessionExportConnection(directory string) (*Connection, error) {
endpoint := strings.TrimSpace(os.Getenv(EndpointEnvKey))
password, err := ReadEndpointCredentialFile(os.Getenv(EndpointPasswordFileEnvKey))
if err != nil {
return nil, err
}
if endpoint == "" || password == "" {
path, err := ServiceCredentialPath()
if err != nil {
return nil, err
}
credential, err := ReadServiceCredential(path)
if err != nil {
return nil, err
}
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot read the local OpenCode endpoint")
}
var state struct {
URL string `json:"url"`
}
if json.Unmarshal(data, &state) != nil {
return nil, fmt.Errorf("invalid local OpenCode endpoint")
}
base, err := NormalizeEndpointURL(state.URL)
if err != nil {
return nil, fmt.Errorf("invalid local OpenCode endpoint")
}
if endpoint == "" {
endpoint = base
} else {
configured, err := NormalizeEndpointURL(endpoint)
if err != nil || configured != base {
return nil, fmt.Errorf("session export endpoint requires its own credential")
}
}
if password == "" {
password = credential
}
}
base, err := NormalizeEndpointURL(endpoint)
if err != nil {
return nil, err
}
return &Connection{Backend: "v2", URL: base, Directory: directory, password: password,
username: strings.TrimSpace(os.Getenv(EndpointUsernameEnvKey))}, nil
}
// UnmarshalJSON retains the complete stored turn, including unknown metadata
// and inline image attachments, independently of the chat UI projection.
func (m *nativeChatMessage) UnmarshalJSON(data []byte) error {
type message nativeChatMessage
var decoded message
if err := json.Unmarshal(data, &decoded); err != nil {
return err
}
*m = nativeChatMessage(decoded)
m.Raw = append(json.RawMessage(nil), data...)
return nil
}
+11 -12
View File
@@ -33,11 +33,11 @@ func TestExportSessionDecodesRolesPartsAndUnknownTypes(t *testing.T) {
`{"type":"file","uri":"file:///tmp/x"}]},` +
`{"info":{"id":"m2","role":"user"},"text":"hi"}]}`
bin := exportFixture(t, id, body)
session, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", id)
session, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", id)
if err != nil {
t.Fatal(err)
}
if session.ID != id || session.Backend != "v2" || session.Truncated {
if session.ID != id || session.Backend != "v1" || session.Truncated {
t.Fatalf("unexpected session: %+v", session)
}
if len(session.Messages) != 2 {
@@ -114,7 +114,7 @@ func TestExportSessionRetriesOneInvalidReadbackWithoutCausalClaim(t *testing.T)
"fi\n" +
"cat <<'EOF'\n" + valid + "\nEOF\n"
bin := writeFixture(t, script)
session, err := ExportSession(context.Background(), bin, dir, "v2", id)
session, err := ExportSession(context.Background(), bin, dir, "v1", id)
if err != nil {
t.Fatal(err)
}
@@ -125,17 +125,17 @@ func TestExportSessionRetriesOneInvalidReadbackWithoutCausalClaim(t *testing.T)
func TestExportSessionRejectsMismatchedOrInvalidID(t *testing.T) {
bin := exportFixture(t, "ses_other", `{"info":{"id":"ses_other"},"messages":[{"info":{"role":"assistant"}}]}`)
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", "ses_want"); err == nil {
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", "ses_want"); err == nil {
t.Fatal("mismatched session export accepted")
}
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", "not-a-session"); err == nil {
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", "not-a-session"); err == nil {
t.Fatal("invalid session id accepted without running the CLI")
}
}
func TestExportSessionFailsSanitizedWhenCLIUnavailable(t *testing.T) {
bin := filepath.Join(t.TempDir(), "absent-opencode")
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", "ses_ok"); err == nil {
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", "ses_ok"); err == nil {
t.Fatal("missing CLI must fail the export")
}
}
@@ -144,7 +144,7 @@ func TestExportNativeReportsBoundedOverflow(t *testing.T) {
id := "ses_ok"
body := `{"info":{"id":"ses_ok"},"messages":[{"info":{"role":"assistant"},"parts":[{"type":"text","text":"` + strings.Repeat("x", 256) + `"}]}]}`
bin := exportFixture(t, id, body)
res, err := exportNative(context.Background(), bin, t.TempDir(), "v2", id, 32)
res, err := exportNative(context.Background(), bin, t.TempDir(), "v1", id, 32)
if err != nil {
t.Fatal(err)
}
@@ -160,9 +160,8 @@ func TestExportNativeUsesBackendSubcommand(t *testing.T) {
id := "ses_ok"
body := `{"info":{"id":"ses_ok"},"messages":[{"info":{"role":"assistant"},"parts":[{"type":"text","text":"hello"}]}]}`
bin := exportFixture(t, id, body)
// Both a v1 (top-level export) and a v2 (session export) CLI accept the one
// shared invocation.
for _, backend := range []string{"v1", "v2"} {
// V1 retains its top-level CLI export. V2 is tested with HTTP below.
for _, backend := range []string{"v1"} {
session, err := ExportSession(context.Background(), bin, t.TempDir(), backend, id)
if err != nil || len(session.Messages) != 1 {
t.Fatalf("backend %s export failed: %v %+v", backend, err, session)
@@ -177,7 +176,7 @@ func TestExportSessionKeepsCompleteTurnsFromOverCapSource(t *testing.T) {
`{"info":{"role":"assistant"},"parts":[{"type":"text","text":"first"}]},` +
`{"info":{"role":"user"},"parts":[{"type":"text","text":"` + huge + `"}]}]}`
bin := exportFixture(t, id, body)
session, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", id)
session, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", id)
if err != nil {
t.Fatal(err)
}
@@ -232,7 +231,7 @@ func TestExportRecoveryRetriesOnlyTransientOrEmptyReadback(t *testing.T) {
} else {
script += "if [ $n = 1 ]; then " + tc.first + "; else echo '{\"info\":{\"id\":\"ses_ok\"},\"messages\":[{\"info\":{\"role\":\"assistant\"},\"parts\":[{\"type\":\"text\",\"text\":\"ready\"}]}]}'; fi\n"
}
session, err := ExportSession(context.Background(), writeFixture(t, script), dir, "v2", "ses_ok")
session, err := ExportSession(context.Background(), writeFixture(t, script), dir, "v1", "ses_ok")
if (err != nil) != tc.wantError {
t.Fatalf("unexpected result %+v %v", session, err)
}
+4 -1
View File
@@ -85,7 +85,7 @@ func (c *Connection) Messages(ctx context.Context, id string) ([]byte, error) {
if executable == "" {
executable = Executable()
}
res, exportErr := exportNative(ctx, executable, c.Directory, c.Backend, id, MaxResponse)
res, exportErr := exportNative(ctx, executable, c.Directory, c.Backend, id, MaxResponse, c)
if exportErr != nil {
if ctx.Err() != nil {
return data, ctx.Err()
@@ -102,5 +102,8 @@ func (c *Connection) Messages(ctx context.Context, id string) ([]byte, error) {
return data, fmt.Errorf("%w; native export returned invalid or mismatched session evidence", err)
}
c.MessageReadbackDiagnostic = "Native HTTP message readback rejected stored OutputFormat (HTTP 400); read-only CLI export recovered the original messages without modifying the session."
if c.Backend == "v2" {
c.MessageReadbackDiagnostic = "Native HTTP message readback rejected stored OutputFormat (HTTP 400); read-only HTTP export recovered the original messages without modifying the session."
}
return messages, nil
}
+48 -3
View File
@@ -3,6 +3,8 @@ package stage
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
@@ -12,9 +14,8 @@ import (
"git.a-wan.net/alan/pipelines/internal/opencode"
)
// sessionCLI writes a fake `opencode` CLI that answers the session export
// invocation (v1 `export <id>` or v2 `session export <id>`) with body and points
// PIPELINES_OPENCODE at it, so ExportSession runs without a service.
// sessionCLI supplies an isolated authenticated V2 export API plus the legacy
// V1 CLI fixture. Neither transport can reach the operator's real service.
func sessionCLI(t *testing.T, id, body string) {
t.Helper()
path := filepath.Join(t.TempDir(), "opencode")
@@ -26,6 +27,28 @@ func sessionCLI(t *testing.T, id, body string) {
t.Fatal(err)
}
t.Setenv("PIPELINES_OPENCODE", path)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, password, ok := r.BasicAuth()
if !ok || user != "opencode" || password != "fixture-password" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if r.Method != "GET" || r.URL.Path != "/api/experimental/session/"+id+"/export" || r.URL.Query().Get("sanitize") != "false" {
t.Errorf("unexpected export operation: %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"data":`+body+`}`)
}))
t.Cleanup(srv.Close)
passwordPath := filepath.Join(t.TempDir(), "password")
if err := os.WriteFile(passwordPath, []byte("fixture-password"), 0600); err != nil {
t.Fatal(err)
}
t.Setenv(opencode.EndpointEnvKey, srv.URL)
t.Setenv(opencode.EndpointPasswordFileEnvKey, passwordPath)
t.Setenv(opencode.EndpointUsernameEnvKey, "")
}
func sessionBody() string {
@@ -124,6 +147,26 @@ func TestExportSessionRenderedTruncationBanner(t *testing.T) {
}
}
func TestExportSessionLargeHTTPExportWritesBoundedRealHTML(t *testing.T) {
dir := t.TempDir()
image := strings.Repeat("a", 300<<10)
body := `{"info":{"id":"ses_ok"},"messages":[{"id":"m1","type":"user","text":"image input","files":[{"mime":"image/png","data":"` + image + `"}]},{"id":"m2","type":"assistant","content":[{"type":"reasoning","text":"real reasoning"},{"type":"tool","name":"read","state":{"status":"completed","input":{},"content":[{"type":"text","text":"real tool output"}]}},{"type":"text","text":"after CLI cutoff"}]}]}`
sessionCLI(t, "ses_ok", body)
result, err := ExportSession(context.Background(), sessionRequest(dir))
if err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(filepath.Join(dir, result.HTML))
if err != nil || result.Truncated || result.Messages != 2 || len(data) > SessionExportFileLimit || result.HTMLBytes != int64(len(data)) {
t.Fatalf("large API HTML not complete/bounded: %+v err=%v", result, err)
}
for _, want := range []string{image, "real reasoning", "real tool output", "after CLI cutoff", "m1", "m2"} {
if !strings.Contains(string(data), want) {
t.Fatal("large API HTML lost real content")
}
}
}
func TestExportSessionSourceTruncationIsLabelledNotWhole(t *testing.T) {
session := opencode.ExportedSession{
ID: "ses_ok",
@@ -232,6 +275,8 @@ func TestExportSessionRefusesToOverwriteTheHTMLFile(t *testing.T) {
func TestExportSessionFailureWritesNoFiles(t *testing.T) {
dir := t.TempDir()
t.Setenv("PIPELINES_OPENCODE", filepath.Join(t.TempDir(), "absent-opencode"))
t.Setenv(opencode.EndpointEnvKey, "http://127.0.0.1:1")
t.Setenv(opencode.EndpointPasswordFileEnvKey, filepath.Join(t.TempDir(), "absent-credential"))
_, err := ExportSession(context.Background(), sessionRequest(dir))
if err == nil {
t.Fatal("export failure must return an error")