Files
pipelines/internal/protocol/runs_test.go
T
alan 0f5a188c6a Record and serve per-stage artifacts
Every agent stage now writes into a per-run/step artifacts directory under
the data dir (<data>/artifacts/<run>/<step>), passed to Pi as
PIPELINES_ARTIFACTS_DIR, exposed on the OpenCode client env seam, and stated
in the agent prompt boilerplate so agents save images, screenshots, reports
and logs there.

After each step the server scans that directory with a bounded, symlink- and
traversal-safe inventory (os.OpenRoot + Lstat + O_NOFOLLOW) recording name,
size_bytes, content type (extension then 512-byte sniff fallback) and URL,
enforcing max files, per-file bytes and total bytes while counting skipped
and truncated entries. A missing, unreadable or oversized directory yields
only a best-effort diagnostic and never changes the step outcome.

The list is carried on protocol.RunStep as Artifacts []StageArtifact plus
skipped/truncated counters (omitempty, backwards compatible), persisted in a
new step_artifacts table keyed (run_id, sequence) alongside the
notifications/file-changes tables and loaded by GetRun.

GET /artifacts/{run}/{step}/{name} on the existing handler serves the bytes
with http.ServeContent, so Content-Type, Content-Length, HEAD and Range
(206/416) come from the stdlib; unknown runs/steps, absolute paths,
traversal, symlink escapes and non-regular files are refused.

The TUI lists each selected step's artifacts (name + human size) in the
retained-evidence section and adds an artifacts summary in the style of the
file-changes summary. Adds docs/stage-artifacts.md linked from README and an
artifacts subsection to the packaged operations skill reference.

Tests cover the scan records and caps, missing/oversized directories,
HTTP HEAD/Range/traversal/symlink/unknown refusals and protocol round-trips.
2026-10-15 17:51:18 +01:00

54 lines
1.4 KiB
Go

package protocol
import (
"encoding/json"
"strings"
"testing"
)
func TestRunStepArtifactsRoundTrip(t *testing.T) {
step := RunStep{
Sequence: 2,
Stage: "review",
Artifacts: []StageArtifact{{
Name: "captures/shot.png",
SizeBytes: 1234,
ContentType: "image/png",
URL: "/artifacts/9/2/captures/shot.png",
}},
ArtifactsSkipped: 1,
ArtifactsTruncated: 3,
}
encoded, err := json.Marshal(step)
if err != nil {
t.Fatal(err)
}
var decoded RunStep
if err := json.Unmarshal(encoded, &decoded); err != nil {
t.Fatal(err)
}
if len(decoded.Artifacts) != 1 || decoded.Artifacts[0] != step.Artifacts[0] {
t.Fatalf("artifacts lost in round trip: %+v", decoded.Artifacts)
}
if decoded.ArtifactsSkipped != 1 || decoded.ArtifactsTruncated != 3 {
t.Fatalf("counters lost in round trip: %+v", decoded)
}
}
func TestRunStepOmitsAbsentArtifacts(t *testing.T) {
encoded, err := json.Marshal(RunStep{Sequence: 1, Stage: "one"})
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(encoded), "artifacts") {
t.Fatalf("empty artifacts must be omitted: %s", encoded)
}
var decoded RunStep
if err := json.Unmarshal([]byte(`{"sequence":1}`), &decoded); err != nil {
t.Fatal(err)
}
if decoded.Artifacts != nil || decoded.ArtifactsSkipped != 0 || decoded.ArtifactsTruncated != 0 {
t.Fatalf("absent field must decode empty: %+v", decoded)
}
}