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.
This commit is contained in:
@@ -43,6 +43,7 @@ type RunStep struct {
|
||||
Control *ControlEvidence `json:"control,omitempty"`
|
||||
FileChanges *StageFileChanges `json:"file_changes,omitempty"`
|
||||
Notifications []NotificationReceipt `json:"notifications,omitempty"`
|
||||
Artifacts []StageArtifact `json:"artifacts,omitempty"`
|
||||
Sequence int `json:"sequence"`
|
||||
Stage string `json:"stage"`
|
||||
Status string `json:"status"`
|
||||
@@ -54,8 +55,22 @@ type RunStep struct {
|
||||
SessionID string `json:"session_id"`
|
||||
AgentActive bool `json:"agent_active"`
|
||||
Transcript string `json:"transcript"`
|
||||
StartedAt string `json:"started_at"`
|
||||
FinishedAt string `json:"finished_at"`
|
||||
// ArtifactsSkipped and ArtifactsTruncated count files excluded from the
|
||||
// bounded per-step scan (over the file/size cap) and by the total budget.
|
||||
ArtifactsSkipped int `json:"artifacts_skipped,omitempty"`
|
||||
ArtifactsTruncated int `json:"artifacts_truncated,omitempty"`
|
||||
StartedAt string `json:"started_at"`
|
||||
FinishedAt string `json:"finished_at"`
|
||||
}
|
||||
|
||||
// StageArtifact is one file a stage left in its artifacts directory. It is
|
||||
// observed server-side evidence, not a model's claim. An empty Artifacts slice
|
||||
// means no files were recorded (including historical steps).
|
||||
type StageArtifact struct {
|
||||
Name string `json:"name"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
ContentType string `json:"content_type"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
type MemoryRead struct {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user