server: export a finished stage's real session as session.md and session.html
Every OpenCode-harness model stage now writes its real session into its own step artifacts directory as session.md and session.html, so the existing bounded artifact listing, the /artifacts/<run>/<step>/<name> serving path and the artifact scan find and serve them with no special casing, while the stage keeps its bounded preview in the run record unchanged. - internal/opencode/export.go adds the one shared native export invocation and the exact session-ID/messages validation used by both Connection.Messages and the new ExportSession, so the readback path does not diverge. The backend's published subcommand is used (v1 `export <id>`, v2 `session export <id>`), the source read is capped at 8 MiB, and a larger session is read as a prefix and reported truncated instead of failing. boundedOutput.ReadFrom keeps the cap effective so overflow and the truncation path are real. - internal/stage/session_export.go renders markdown and html with a metadata header, every role and every part (unknown part types as raw JSON, every session text escaped in html), atomic temp+rename writes that never overwrite an existing file, a visible truncation banner at both ends when the source or the 2 MiB rendered cap cuts it, and an honest 0-message header for an empty session rather than fabricated text. - protocol.SessionExport and RunStep.SessionExport are stored in a new step_session_exports table (CREATE TABLE IF NOT EXISTS, so an old database gains an empty table with no migration) loaded through the existing main SELECT alongside the other per-step evidence and deleted by run retention. - internal/server/runner.go exports after the post-stage filediff capture and before scanArtifacts, with context.WithTimeout(context.WithoutCancel(ctx), 10s); it never waits for session idleness and a failure or a Pi stage (no native export path) records a sanitized diagnostic and continues, never changing the stage status, outcome, target, timing or result validation. Reflected where it belongs: the packaged skill references (internal/author/skill/references/operations.md and delivery.md) carry the new Session export rules and the run-artifacts bullet, and docs/stage-artifacts.md documents the names, the <run>/<step> paths, the served URL, the caps, the truncation labels, failure isolation and the Pi limitation. The packaged templates (examples/*.md) only tell a stage to save its own artifacts and say nothing about the server-written session export, so none needed updating; no new reference file was added, so install.go's managed inventory is unchanged. Shared documentation: README.md's per-step artifacts sentence is now stale - it should name the exported session alongside images, screenshots, reports and logs. It is deliberately not committed here; the shared documents are folded by the single documentation writer. Checks: gofmt -l on the touched packages produced no output; go vet ./... passed; go test -race -count=1 ./... passed.
This commit is contained in:
@@ -3,9 +3,9 @@ package opencode
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -26,6 +26,30 @@ func (b *boundedOutput) Write(p []byte) (int, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// ReadFrom reads through the bounded Write so the cap applies even when
|
||||
// os/exec's io.Copy selects a ReadFrom on the destination: without this the
|
||||
// embedded bytes.Buffer's promoted ReadFrom would bypass Write and buffer the
|
||||
// whole output without bound, making overflow (and the truncation path) dead.
|
||||
func (b *boundedOutput) ReadFrom(r io.Reader) (int64, error) {
|
||||
var total int64
|
||||
buffer := make([]byte, 32<<10)
|
||||
for {
|
||||
n, err := r.Read(buffer)
|
||||
if n > 0 {
|
||||
if _, writeErr := b.Write(buffer[:n]); writeErr != nil {
|
||||
return total, writeErr
|
||||
}
|
||||
total += int64(n)
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return total, nil
|
||||
}
|
||||
return total, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Messages preserves old sessions with native OutputFormat readback failures:
|
||||
// the CLI export reads the local stored data without that HTTP encoder. It does
|
||||
// not regenerate, repair the database, delete history or change conversations.
|
||||
@@ -48,12 +72,8 @@ func (c *Connection) Messages(ctx context.Context, id string) ([]byte, error) {
|
||||
if executable == "" {
|
||||
executable = Executable()
|
||||
}
|
||||
cmd := CommandContext(ctx, executable, "export", id)
|
||||
cmd.Dir = c.Directory
|
||||
stdout, stderr := &boundedOutput{limit: MaxResponse}, &boundedOutput{limit: 8 << 10}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
if exportErr := cmd.Run(); exportErr != nil {
|
||||
res, exportErr := exportNative(ctx, executable, c.Directory, c.Backend, id, MaxResponse)
|
||||
if exportErr != nil {
|
||||
if ctx.Err() != nil {
|
||||
return data, ctx.Err()
|
||||
}
|
||||
@@ -61,18 +81,13 @@ func (c *Connection) Messages(ctx context.Context, id string) ([]byte, error) {
|
||||
// preserving exec.ErrWaitDelay from a descendant-held output pipe.
|
||||
return data, errors.Join(err, fmt.Errorf("read-only native export fallback failed: %w", exportErr))
|
||||
}
|
||||
if stdout.overflow {
|
||||
if res.truncated {
|
||||
return data, fmt.Errorf("%w; native export exceeds 1 MiB evidence limit", err)
|
||||
}
|
||||
var exported struct {
|
||||
Info struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"info"`
|
||||
Messages json.RawMessage `json:"messages"`
|
||||
}
|
||||
if json.Unmarshal(stdout.Bytes(), &exported) != nil || exported.Info.ID != id || len(exported.Messages) == 0 || exported.Messages[0] != '[' {
|
||||
messages, ok := validNativeExport(res.envelope, id)
|
||||
if !ok {
|
||||
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."
|
||||
return exported.Messages, nil
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user