diff --git a/README.md b/README.md index 2fdceb6d..daba9a0f 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,9 @@ disposable repository, review edits and command evidence, and expect rough edges See the [tester walkthrough and known limits](docs/first-version-testing.md). Stage results include [retained, highlighted workspace diffs](docs/stage-file-diffs.md) -for newly executed visits, alongside their reports and audit evidence. +for newly executed visits, alongside their reports and audit evidence. Stages can +also [record and serve their own artifacts](docs/stage-artifacts.md) - images, +screenshots, reports and logs - per step. Use `:export` / `:import` to [select and transfer pipeline definitions](docs/pipeline-transfer.md) between servers as portable ZIP bundles. diff --git a/cmd/pipelines-server/main.go b/cmd/pipelines-server/main.go index bb42d166..0ffa3cb0 100644 --- a/cmd/pipelines-server/main.go +++ b/cmd/pipelines-server/main.go @@ -35,7 +35,8 @@ func main() { defer store.Close() ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() - srv := &http.Server{Addr: *addr, Handler: server.Handler(ctx, store), ReadHeaderTimeout: 5 * time.Second} + artifactsRoot := filepath.Join(filepath.Dir(*db), "artifacts") + srv := &http.Server{Addr: *addr, Handler: server.HandlerWithOptions(ctx, server.Options{ArtifactsRoot: artifactsRoot}, store), ReadHeaderTimeout: 5 * time.Second} defer server.WaitRuns(srv.Handler) go func() { <-ctx.Done() diff --git a/docs/stage-artifacts.md b/docs/stage-artifacts.md new file mode 100644 index 00000000..67e96c43 --- /dev/null +++ b/docs/stage-artifacts.md @@ -0,0 +1,53 @@ +# Stage artifacts + +A stage that produces images, screenshots, reports or logs - typically written +into an ignored or untracked directory - would otherwise leave nothing retrievable +per step. Every agent visit therefore gets its own artifacts directory, and the +files it contains are recorded on the step and served over HTTP. + +## Where they live + +The server creates `///` before executing an agent +visit. The artifacts root defaults to the directory holding the database +(`/artifacts`) and can be pointed elsewhere with the server's +`ArtifactsRoot` option. Only model stages get a directory. + +The directory is passed to the stage: + +- as the `PIPELINES_ARTIFACTS_DIR` environment variable in a locally spawned Pi + process, and exported to the OpenCode CLI where the runtime controls it; +- in the agent prompt, next to `Run directory:`, for every agent harness. Because + OpenCode V2 tools execute inside the shared serve process, that prompt line is + the reliable carrier there. + +The prompt instruction is deliberately general: save any artifacts you produce - +images, screenshots, reports, logs - into the directory; they are attached to the +stage. There are no technology, language or file-type rules. + +## What is recorded + +After the stage finishes and before the step is stored, the server makes one +bounded inventory of the directory and records, per file, its `name`, +`size_bytes`, `content_type` (from the extension, with a content sniff of at most +the first 512 bytes as a fallback) and a server-relative `url` +(`/artifacts///`). The same fields appear under each step's +`artifacts` in `pipelines inspect RUN_ID --json` and in the TUI. + +- Limits: 200 files per step, 8 MiB per file and 64 MiB total. Over-cap files are + skipped and the skipped/truncated counts are recorded with the step. +- Symlinks in any component and non-regular files are excluded, and the walk is + bounded by the file cap so a large directory is never fully enumerated. +- Artifacts are evidence, not a gate. A missing, unreadable, symlinked, oversized + or empty directory records nothing and never fails the run or changes its + outcome. The database retains the recorded list, not file copies. + +## Fetching bytes + +`GET /artifacts///` on the server that serves `/healthz` and +`/connect` returns the file. Only files inside that run's and step's artifact root +are reachable: path traversal, absolute paths, symlink escapes and unknown +runs/steps are refused. Responses carry the correct `Content-Type` and +`Content-Length`; `HEAD` returns headers without a body; and `Range` requests +return `206 Partial Content` with the exact slice (or `416` when unsatisfiable), +so images and video can stream. `http.ServeContent` streams from the opened file, +so a request never loads the whole file into memory. diff --git a/internal/author/skill/references/operations.md b/internal/author/skill/references/operations.md index 52c9901e..04cdcf0b 100644 --- a/internal/author/skill/references/operations.md +++ b/internal/author/skill/references/operations.md @@ -149,6 +149,32 @@ The stage's File changes section highlights added/removed lines and hunk headers Do not add model output fields, new stages or extra model calls merely to obtain file diffs: current capture is server-owned. Do not promise full files/rollback. +## Retained stage artifacts + +Every agent visit gets a per-run/step artifacts directory under the data +directory (`/artifacts///`); the root is configurable and +defaults next to the database. The directory is passed as +`PIPELINES_ARTIFACTS_DIR` to a locally spawned Pi process and exported to the +OpenCode CLI where controllable, and it is stated in the agent prompt next to +`Run directory:` for every harness. OpenCode V2 tools run inside the shared serve +process, so there the prompt line, not the environment, is the reliable carrier. +The instruction is general: save images, screenshots, reports and logs there. + +After the visit the server makes one bounded inventory and records each file's +name, size, content type (extension, sniff fallback) and a relative url; these +appear under each step's `artifacts` in `inspect RUN_ID --json` and the TUI. +`GET /artifacts///` serves the bytes (correct Content-Type and +Content-Length, HEAD, and Range/206/416 via `http.ServeContent`), refusing +traversal, absolute paths, symlink escapes and unknown runs/steps. + +- Limits: 200 files, 8 MiB/file, 64 MiB total per visit; over-cap files are + skipped and counted; symlinks and non-regular files are excluded. +- Artifacts are read-only evidence, not a gate: absent, unreadable, symlinked or + oversized directories record nothing and never change the run verdict. The + database keeps the recorded list, not file copies. +- Do not invent artifact files or claim a stage produced one without the recorded + evidence; the server observes the directory after the stage. + ## Debug and retry Debug opens a new conversation with retained failure context, exact definition and diff --git a/internal/pipeline/runs.go b/internal/pipeline/runs.go index df08f7ac..5707b375 100644 --- a/internal/pipeline/runs.go +++ b/internal/pipeline/runs.go @@ -35,6 +35,9 @@ PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps( CREATE TABLE IF NOT EXISTS step_control ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, evidence_json TEXT NOT NULL, PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); +CREATE TABLE IF NOT EXISTS step_artifacts ( +run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, artifacts_json TEXT NOT NULL, +PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); CREATE TABLE IF NOT EXISTS run_session_owners ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, backend TEXT NOT NULL, reference TEXT NOT NULL DEFAULT '', PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); @@ -190,6 +193,7 @@ func (s *Store) GetRun(ctx context.Context, id int64) (*protocol.RunDetail, erro rows, err := tx.QueryContext(ctx, `SELECT sequence,stage_key,status,outcome,target,input_json,output_json,diagnostic,session_id,agent_active,transcript, COALESCE((SELECT receipts_json FROM step_notifications n WHERE n.run_id=run_steps.run_id AND n.sequence=run_steps.sequence),'[]'), COALESCE((SELECT changes_json FROM step_file_changes f WHERE f.run_id=run_steps.run_id AND f.sequence=run_steps.sequence),'null'), +COALESCE((SELECT artifacts_json FROM step_artifacts a WHERE a.run_id=run_steps.run_id AND a.sequence=run_steps.sequence),'null'), strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',finished_at),'') FROM run_steps WHERE run_id=? ORDER BY sequence`, id) if err != nil { return nil, err @@ -198,8 +202,8 @@ strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ' for rows.Next() { var step protocol.RunStep var notifications string - var fileChanges string - if err := rows.Scan(&step.Sequence, &step.Stage, &step.Status, &step.Outcome, &step.Target, &input, &output, &step.Diagnostic, &step.SessionID, &step.AgentActive, &step.Transcript, ¬ifications, &fileChanges, &step.StartedAt, &step.FinishedAt); err != nil { + var fileChanges, artifacts string + if err := rows.Scan(&step.Sequence, &step.Stage, &step.Status, &step.Outcome, &step.Target, &input, &output, &step.Diagnostic, &step.SessionID, &step.AgentActive, &step.Transcript, ¬ifications, &fileChanges, &artifacts, &step.StartedAt, &step.FinishedAt); err != nil { rows.Close() return nil, err } @@ -211,6 +215,16 @@ strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ' rows.Close() return nil, err } + if artifacts != "" && artifacts != "null" { + var evidence stageArtifactsEvidence + if err := json.Unmarshal([]byte(artifacts), &evidence); err != nil { + rows.Close() + return nil, err + } + step.Artifacts = evidence.Files + step.ArtifactsSkipped = evidence.Skipped + step.ArtifactsTruncated = evidence.Truncated + } if err := decodeValues(input, &step.Input); err != nil { rows.Close() return nil, err @@ -347,6 +361,15 @@ FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_own return owners, rows.Err() } +// stageArtifactsEvidence is the persisted per-step artifact list plus its +// bounded-scan counters; it is kept in a separate table like the other +// step-scoped evidence so run_steps needs no schema migration on old databases. +type stageArtifactsEvidence struct { + Files []protocol.StageArtifact `json:"files"` + Skipped int `json:"skipped,omitempty"` + Truncated int `json:"truncated,omitempty"` +} + func (s *Store) FinishStep(ctx context.Context, id int64, step protocol.RunStep) error { output, err := json.Marshal(step.Output) if err != nil { @@ -383,6 +406,15 @@ WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, return err } } + if len(step.Artifacts) > 0 || step.ArtifactsSkipped > 0 || step.ArtifactsTruncated > 0 { + data, err := json.Marshal(stageArtifactsEvidence{Files: step.Artifacts, Skipped: step.ArtifactsSkipped, Truncated: step.ArtifactsTruncated}) + if err != nil { + return err + } + if _, err := tx.ExecContext(ctx, "INSERT INTO step_artifacts(run_id,sequence,artifacts_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil { + return err + } + } return tx.Commit() } diff --git a/internal/pipeline/store.go b/internal/pipeline/store.go index d2c64639..afdde185 100644 --- a/internal/pipeline/store.go +++ b/internal/pipeline/store.go @@ -8,6 +8,7 @@ import ( "fmt" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" + "path/filepath" "strings" "sync" "time" @@ -16,8 +17,9 @@ import ( ) type Store struct { - db *sql.DB - mu sync.Mutex + db *sql.DB + dir string + mu sync.Mutex } func Open(path string) (*Store, error) { @@ -34,7 +36,7 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU db.Close() return nil, err } - s := &Store{db: db} + s := &Store{db: db, dir: filepath.Dir(path)} if err := s.initRuns(); err != nil { db.Close() return nil, err @@ -48,6 +50,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU func (s *Store) Close() error { return s.db.Close() } +// Directory is the resolved directory holding the database, used to place +// adjacent server state such as stage artifacts. +func (s *Store) Directory() string { return s.dir } + func (s *Store) List(ctx context.Context) ([]protocol.PipelineSummary, error) { rows, err := s.db.QueryContext(ctx, `SELECT p.id, p.name, v.version, json_array_length(v.graph_json, '$.stages'), diff --git a/internal/protocol/runs.go b/internal/protocol/runs.go index 7ea1f744..dc4c0880 100644 --- a/internal/protocol/runs.go +++ b/internal/protocol/runs.go @@ -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 { diff --git a/internal/protocol/runs_test.go b/internal/protocol/runs_test.go new file mode 100644 index 00000000..b5486e2c --- /dev/null +++ b/internal/protocol/runs_test.go @@ -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) + } +} diff --git a/internal/server/artifacts.go b/internal/server/artifacts.go new file mode 100644 index 00000000..9122f7fc --- /dev/null +++ b/internal/server/artifacts.go @@ -0,0 +1,225 @@ +package server + +import ( + "errors" + "io" + "io/fs" + "log" + "mime" + "net/http" + "net/url" + "os" + "path" + "path/filepath" + "strconv" + "strings" + "syscall" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// Artifact capture is deliberately small and best-effort. A stage's artifacts +// directory is evidence, never a gate: every scan error is swallowed and never +// changes the step's outcome. +const ( + maxArtifactFiles = 200 + maxArtifactFileBytes = 8 << 20 + maxArtifactTotalBytes = 64 << 20 + artifactSniffBytes = 512 +) + +// scanArtifacts returns a bounded inventory of one step's artifacts directory. +// The directory path is supplied by the caller because it was created next to +// the step; runID and sequence name the served URL. A missing, unreadable, +// symlinked or oversized directory yields no entries and a zero count. +func (r *runner) scanArtifacts(runID int64, sequence int, directory string) ([]protocol.StageArtifact, int, int) { + if directory == "" || r.artifactsRoot == "" { + return nil, 0, 0 + } + root, err := os.OpenRoot(r.artifactsRoot) + if err != nil { + log.Printf("stage artifacts root unavailable: %v", err) + return nil, 0, 0 + } + defer root.Close() + rel := path.Join(strconv.FormatInt(runID, 10), strconv.Itoa(sequence)) + if !containedDirectory(root, rel) { + return nil, 0, 0 + } + files := []protocol.StageArtifact{} + skipped, truncated := 0, 0 + used := int64(0) + walkErr := fs.WalkDir(root.FS(), rel, func(name string, entry fs.DirEntry, walkErr error) error { + if walkErr != nil { + // A single unreadable entry must not abort the whole inventory. + return nil + } + if name == rel || entry.IsDir() { + return nil + } + if entry.Type()&fs.ModeSymlink != 0 || !entry.Type().IsRegular() { + skipped++ + return nil + } + info, infoErr := entry.Info() + if infoErr != nil || !info.Mode().IsRegular() { + skipped++ + return nil + } + if len(files) >= maxArtifactFiles { + skipped++ + return fs.SkipAll + } + if info.Size() > maxArtifactFileBytes { + skipped++ + return nil + } + if used+info.Size() > maxArtifactTotalBytes { + truncated++ + return fs.SkipAll + } + artifactName := strings.TrimPrefix(name, rel+"/") + full := path.Join(rel, artifactName) + contentType := mime.TypeByExtension(filepath.Ext(artifactName)) + if contentType == "" { + contentType = sniffArtifact(root, full) + if contentType == "" { + skipped++ + return nil + } + } + used += info.Size() + files = append(files, protocol.StageArtifact{ + Name: artifactName, + SizeBytes: info.Size(), + ContentType: contentType, + URL: artifactURL(runID, sequence, artifactName), + }) + return nil + }) + if walkErr != nil { + log.Printf("stage artifacts inventory run=%d step=%d: %v", runID, sequence, walkErr) + return nil, 0, 0 + } + if len(files) == 0 { + files = nil + } + return files, skipped, truncated +} + +// containedDirectory reports whether rel names a real directory below root with +// no symlink in any component. +func containedDirectory(root *os.Root, rel string) bool { + if rel == "" || !fs.ValidPath(rel) || rel == "." { + return false + } + parts := strings.Split(rel, "/") + for i := range parts { + info, err := root.Lstat(strings.Join(parts[:i+1], "/")) + if err != nil || info.Mode()&os.ModeSymlink != 0 { + return false + } + if i == len(parts)-1 { + return info.IsDir() + } + } + return false +} + +// sniffArtifact opens one regular file without following a final symlink and +// returns the detected content type from at most the first 512 bytes. +func sniffArtifact(root *os.Root, rel string) string { + file, err := root.OpenFile(rel, os.O_RDONLY|syscall.O_NOFOLLOW, 0) + if err != nil { + return "" + } + defer file.Close() + buffer := make([]byte, artifactSniffBytes) + n, err := io.ReadFull(file, buffer) + if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) { + return "" + } + return http.DetectContentType(buffer[:n]) +} + +// artifactURL builds the server-relative URL for one artifact, escaping each +// path segment so a name cannot forge path structure. +func artifactURL(runID int64, sequence int, name string) string { + parts := strings.Split(name, "/") + for i, part := range parts { + parts[i] = url.PathEscape(part) + } + return "/artifacts/" + strconv.FormatInt(runID, 10) + "/" + strconv.Itoa(sequence) + "/" + strings.Join(parts, "/") +} + +// serveArtifact serves one recorded step artifact. It refuses unknown runs or +// steps and any name that is absolute, non-local, symlinked in any component or +// not a regular file, so nothing outside // is served. +// http.ServeContent supplies Content-Type (extension then sniff), Content-Length, +// HEAD and Range handling from the opened *os.File. +func (r *runner) serveArtifact(w http.ResponseWriter, req *http.Request) { + name := req.PathValue("name") + runID, runErr := strconv.ParseInt(req.PathValue("run"), 10, 64) + sequence, stepErr := strconv.Atoi(req.PathValue("step")) + if runErr != nil || stepErr != nil || runID <= 0 || sequence <= 0 { + http.NotFound(w, req) + return + } + if name == "" || name == "." || !fs.ValidPath(name) || path.IsAbs(name) { + http.NotFound(w, req) + return + } + detail, err := r.store.GetRun(req.Context(), runID) + if err != nil { + http.NotFound(w, req) + return + } + known := false + for _, step := range detail.Steps { + if step.Sequence == sequence { + known = true + break + } + } + if !known { + http.NotFound(w, req) + return + } + root, err := os.OpenRoot(r.artifactsRoot) + if err != nil { + http.NotFound(w, req) + return + } + defer root.Close() + rel := path.Join(strconv.FormatInt(runID, 10), strconv.Itoa(sequence)) + if !containedDirectory(root, rel) { + http.NotFound(w, req) + return + } + full := path.Join(rel, name) + parts := strings.Split(full, "/") + for i := range parts { + info, lerr := root.Lstat(strings.Join(parts[:i+1], "/")) + if lerr != nil || info.Mode()&os.ModeSymlink != 0 { + http.NotFound(w, req) + return + } + } + info, err := root.Lstat(full) + if err != nil || !info.Mode().IsRegular() { + http.NotFound(w, req) + return + } + file, err := root.OpenFile(full, os.O_RDONLY|syscall.O_NOFOLLOW, 0) + if err != nil { + http.NotFound(w, req) + return + } + defer file.Close() + opened, err := file.Stat() + if err != nil || !opened.Mode().IsRegular() { + http.NotFound(w, req) + return + } + http.ServeContent(w, req, path.Base(name), opened.ModTime(), file) +} diff --git a/internal/server/artifacts_test.go b/internal/server/artifacts_test.go new file mode 100644 index 00000000..573b851b --- /dev/null +++ b/internal/server/artifacts_test.go @@ -0,0 +1,278 @@ +package server + +import ( + "bytes" + "context" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" +) + +func artifactNamed(files []protocol.StageArtifact, name string) (protocol.StageArtifact, bool) { + for _, file := range files { + if file.Name == name { + return file, true + } + } + return protocol.StageArtifact{}, false +} + +func TestArtifactsScanRecordsSizesTypesAndSkips(t *testing.T) { + root := t.TempDir() + stepDir := filepath.Join(root, "4", "2") + if err := os.MkdirAll(filepath.Join(stepDir, "reports"), 0700); err != nil { + t.Fatal(err) + } + png := []byte("\x89PNG\r\n\x1a\n...png bytes...") + if err := os.WriteFile(filepath.Join(stepDir, "shot.png"), png, 0600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(stepDir, "notes"), []byte("hello artifacts"), 0600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(stepDir, "reports", "out.txt"), []byte("report"), 0600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(stepDir, "big.bin"), make([]byte, maxArtifactFileBytes+1), 0600); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(stepDir, "shot.png"), filepath.Join(stepDir, "alias.png")); err != nil { + t.Fatal(err) + } + r := &runner{artifactsRoot: root} + files, skipped, truncated := r.scanArtifacts(4, 2, stepDir) + if truncated != 0 { + t.Fatalf("unexpected truncation: %d", truncated) + } + if skipped < 2 { + t.Fatalf("oversized file and symlink must be skipped: %d", skipped) + } + shot, ok := artifactNamed(files, "shot.png") + if !ok || shot.SizeBytes != int64(len(png)) || shot.ContentType != "image/png" || shot.URL != "/artifacts/4/2/shot.png" { + t.Fatalf("png artifact wrong: %+v", shot) + } + notes, ok := artifactNamed(files, "notes") + if !ok || !strings.HasPrefix(notes.ContentType, "text/plain") { + t.Fatalf("extensionless artifact not sniffed: %+v", notes) + } + if nested, ok := artifactNamed(files, "reports/out.txt"); !ok || nested.SizeBytes != int64(len("report")) { + t.Fatalf("nested artifact missing: %+v", files) + } + if _, ok := artifactNamed(files, "alias.png"); ok { + t.Fatal("symlink must not be recorded") + } + if _, ok := artifactNamed(files, "big.bin"); ok { + t.Fatal("oversized artifact must not be recorded") + } +} + +func TestArtifactsScanBoundsFileCount(t *testing.T) { + root := t.TempDir() + stepDir := filepath.Join(root, "1", "1") + if err := os.MkdirAll(stepDir, 0700); err != nil { + t.Fatal(err) + } + for i := 0; i < maxArtifactFiles+5; i++ { + name := filepath.Join(stepDir, fmt.Sprintf("f%04d", i)) + if err := os.WriteFile(name, []byte("x"), 0600); err != nil { + t.Fatal(err) + } + } + r := &runner{artifactsRoot: root} + files, skipped, _ := r.scanArtifacts(1, 1, stepDir) + if len(files) != maxArtifactFiles { + t.Fatalf("file cap not enforced: %d", len(files)) + } + if skipped == 0 { + t.Fatal("over-cap files were not counted as skipped") + } +} + +func TestArtifactsScanMissingDirectoryIsEmpty(t *testing.T) { + r := &runner{artifactsRoot: t.TempDir()} + files, skipped, truncated := r.scanArtifacts(1, 1, filepath.Join(t.TempDir(), "absent")) + if len(files) != 0 || skipped != 0 || truncated != 0 { + t.Fatalf("missing directory must be empty: %+v %d %d", files, skipped, truncated) + } +} + +// runWithArtifact executes one real run whose stage writes a file into the +// runner-provided artifacts directory. It returns the live handler and the +// recorded step so tests can serve the artifact over the real HTTP endpoint. +func runWithArtifact(t *testing.T) (*runHandler, protocol.RunStep) { + t.Helper() + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + store, err := pipeline.Open(filepath.Join(t.TempDir(), "artifacts.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { store.Close() }) + if _, err := store.Submit(ctx, "artifacts", pipeline.Template([]string{"one"}, true)); err != nil { + t.Fatal(err) + } + items, err := store.List(ctx) + if err != nil { + t.Fatal(err) + } + handler := Handler(ctx, store).(*runHandler) + t.Cleanup(func() { cancel(); WaitRuns(handler) }) + handler.runner.artifactsRoot = t.TempDir() + handler.runner.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) { + if def.ArtifactsDir == "" { + t.Error("runner did not provide an artifacts directory") + return stage.Result{Outcome: "success"}, nil + } + if err := os.WriteFile(filepath.Join(def.ArtifactsDir, "report.txt"), []byte("artifact bytes"), 0600); err != nil { + return stage.Result{}, err + } + return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil + } + dir := t.TempDir() + run, err := handler.runner.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "artifact", Workspace: dir, Input: map[string]any{"prompt": "task"}}) + if err != nil { + t.Fatal(err) + } + handler.runner.wg.Wait() + detail, err := store.GetRun(ctx, run.ID) + if err != nil { + t.Fatal(err) + } + if detail.Status != "succeeded" || len(detail.Steps) != 1 { + t.Fatalf("unexpected run: %+v", detail) + } + step := detail.Steps[0] + if len(step.Artifacts) != 1 { + t.Fatalf("artifact not retained: %+v", step.Artifacts) + } + artifact := step.Artifacts[0] + if artifact.Name != "report.txt" || artifact.SizeBytes != int64(len("artifact bytes")) || + !strings.HasPrefix(artifact.ContentType, "text/plain") || artifact.URL == "" { + t.Fatalf("recorded artifact wrong: %+v", artifact) + } + return handler, step +} + +func TestArtifactHTTPGetHeadRangeAndRefusals(t *testing.T) { + handler, step := runWithArtifact(t) + srv := httptest.NewServer(handler) + defer srv.Close() + base := srv.URL + step.Artifacts[0].URL + payload := []byte("artifact bytes") + + resp, err := http.Get(base) + if err != nil { + t.Fatal(err) + } + body, _ := io.ReadAll(resp.Body) + resp.Body.Close() + if resp.StatusCode != http.StatusOK || !bytes.Equal(body, payload) { + t.Fatalf("GET wrong: %d %q", resp.StatusCode, body) + } + if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "text/plain") { + t.Fatalf("GET content type wrong: %q", ct) + } + if cl := resp.Header.Get("Content-Length"); cl != fmt.Sprint(len(payload)) { + t.Fatalf("GET content length wrong: %q", cl) + } + + head, err := http.Head(base) + if err != nil { + t.Fatal(err) + } + headBody, _ := io.ReadAll(head.Body) + head.Body.Close() + if head.StatusCode != http.StatusOK || len(headBody) != 0 { + t.Fatalf("HEAD wrong: %d %q", head.StatusCode, headBody) + } + if head.Header.Get("Content-Length") != fmt.Sprint(len(payload)) { + t.Fatalf("HEAD lost content length: %q", head.Header.Get("Content-Length")) + } + + rangeReq, _ := http.NewRequest(http.MethodGet, base, nil) + rangeReq.Header.Set("Range", "bytes=0-3") + rangeResp, err := http.DefaultClient.Do(rangeReq) + if err != nil { + t.Fatal(err) + } + rangeBody, _ := io.ReadAll(rangeResp.Body) + rangeResp.Body.Close() + if rangeResp.StatusCode != http.StatusPartialContent || !bytes.Equal(rangeBody, payload[:4]) { + t.Fatalf("Range wrong: %d %q", rangeResp.StatusCode, rangeBody) + } + if got := rangeResp.Header.Get("Content-Range"); got != fmt.Sprintf("bytes 0-3/%d", len(payload)) { + t.Fatalf("Content-Range wrong: %q", got) + } + + unsatReq, _ := http.NewRequest(http.MethodGet, base, nil) + unsatReq.Header.Set("Range", "bytes=9999-10000") + unsatResp, err := http.DefaultClient.Do(unsatReq) + if err != nil { + t.Fatal(err) + } + unsatResp.Body.Close() + if unsatResp.StatusCode != http.StatusRequestedRangeNotSatisfiable { + t.Fatalf("unsatisfiable Range wrong: %d", unsatResp.StatusCode) + } + + // Unknown run and unknown step must not serve bytes. + for _, path := range []string{"/artifacts/999999/1/report.txt", fmt.Sprintf("/artifacts/%d/99/report.txt", runIDFromURL(step.Artifacts[0].URL))} { + resp, err := http.Get(srv.URL + path) + if err != nil { + t.Fatal(err) + } + resp.Body.Close() + if resp.StatusCode != http.StatusNotFound { + t.Fatalf("%s expected 404, got %d", path, resp.StatusCode) + } + } +} + +func runIDFromURL(url string) int64 { + var id int64 + fmt.Sscanf(url, "/artifacts/%d/", &id) + return id +} + +func TestArtifactHTTPRefusesEscapes(t *testing.T) { + handler, step := runWithArtifact(t) + runID := runIDFromURL(step.Artifacts[0].URL) + root := handler.runner.artifactsRoot + + // A symlink inside the step directory must never be followed. + stepDir := filepath.Join(root, fmt.Sprint(runID), "1") + secret := filepath.Join(t.TempDir(), "secret.txt") + if err := os.WriteFile(secret, []byte("secret"), 0600); err != nil { + t.Fatal(err) + } + if err := os.Symlink(secret, filepath.Join(stepDir, "escape.txt")); err != nil { + t.Fatal(err) + } + + srv := httptest.NewServer(handler) + defer srv.Close() + for _, path := range []string{ + fmt.Sprintf("/artifacts/%d/1/escape.txt", runID), + fmt.Sprintf("/artifacts/%d/1/%%2e%%2e/%%2e%%2e/secret.txt", runID), + fmt.Sprintf("/artifacts/%d/1/%%2Fetc%%2Fpasswd", runID), + } { + resp, err := http.Get(srv.URL + path) + if err != nil { + t.Fatal(err) + } + body, _ := io.ReadAll(resp.Body) + resp.Body.Close() + if resp.StatusCode == http.StatusOK || bytes.Contains(body, []byte("secret")) { + t.Fatalf("%s served forbidden bytes: %d %q", path, resp.StatusCode, body) + } + } +} diff --git a/internal/server/runner.go b/internal/server/runner.go index 5b8058d7..14024761 100644 --- a/internal/server/runner.go +++ b/internal/server/runner.go @@ -8,6 +8,7 @@ import ( "maps" "os" "path/filepath" + "strconv" "strings" "sync" "time" @@ -28,13 +29,14 @@ type activeRun struct { } type runner struct { - ctx context.Context - store *pipeline.Store - mu sync.Mutex - active map[int64]activeRun - execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error) - wg sync.WaitGroup - schedulerWG sync.WaitGroup + ctx context.Context + store *pipeline.Store + artifactsRoot string + mu sync.Mutex + active map[int64]activeRun + execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error) + wg sync.WaitGroup + schedulerWG sync.WaitGroup } func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) { @@ -58,7 +60,8 @@ func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) { if err := s.RecoverRuns(ctx); err != nil { return nil, err } - return &runner{ctx: ctx, store: s, active: map[int64]activeRun{}}, nil + root := filepath.Join(s.Directory(), "artifacts") + return &runner{ctx: ctx, store: s, artifactsRoot: root, active: map[int64]activeRun{}}, nil } func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.RunDetail, error) { @@ -357,8 +360,21 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail, return } useAgent := stage.EffectiveType(def.Type) == stage.ModelType && (len(tools) > 0 || !explicitTools) + // Every agent visit gets a per-run/step directory for the files it + // produces. Creation is best-effort: an unusable directory must never + // fail the run, and it is only attempted for model stages. + artifactsDir := "" + if stage.EffectiveType(def.Type) == stage.ModelType { + candidate := filepath.Join(r.artifactsRoot, strconv.FormatInt(id, 10), strconv.Itoa(sequence)) + if mkErr := os.MkdirAll(candidate, 0700); mkErr != nil { + log.Printf("stage artifacts directory run=%d step=%d: %v", id, sequence, mkErr) + } else { + artifactsDir = candidate + def.ArtifactsDir = candidate + } + } operations := &stage.OperationLog{} - client := &stage.AgentClient{OpenCodeClient: stage.OpenCodeClient{Directory: workspace, Operations: operations}, WritePaths: request.WritePaths, Tools: tools} + client := &stage.AgentClient{OpenCodeClient: stage.OpenCodeClient{Directory: workspace, Operations: operations, ArtifactsDir: artifactsDir}, WritePaths: request.WritePaths, Tools: tools} client.OnSession = func(session string, active bool) error { c, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -409,7 +425,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail, err = fmt.Errorf("Pi is not configured on execution server") return } - piClient = &stage.PiClient{Executable: profile.Executable, Directory: workspace, Tools: tools, WritePaths: request.WritePaths} + piClient = &stage.PiClient{Executable: profile.Executable, Directory: workspace, Tools: tools, WritePaths: request.WritePaths, ArtifactsDir: artifactsDir} piClient.OnOwnership = func(session, reference string, active bool) error { c, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -422,7 +438,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail, result, err = (stage.ModelStage{Client: client}).Run(stageCtx, def, input) stageSession = client.SessionID } else if stage.EffectiveType(def.Type) == stage.ModelType { - plain := &stage.OpenCodeClient{Directory: workspace, Operations: operations} + plain := &stage.OpenCodeClient{Directory: workspace, Operations: operations, ArtifactsDir: artifactsDir} plain.OnGenerationSession = func(session, backend, reference string, active bool) error { c, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -446,7 +462,8 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail, changes.Status = "partial" changes.Diagnostic = "Agent cleanup was not confirmed; files may still be changing." } - step := protocol.RunStep{Sequence: sequence, Stage: key, Status: "completed", Outcome: result.Outcome, Output: result.Output, Transcript: operations.Transcript(client.Transcript), SessionID: stageSession, FileChanges: changes} + artifacts, artifactsSkipped, artifactsTruncated := r.scanArtifacts(id, sequence, artifactsDir) + step := protocol.RunStep{Sequence: sequence, Stage: key, Status: "completed", Outcome: result.Outcome, Output: result.Output, Transcript: operations.Transcript(client.Transcript), SessionID: stageSession, FileChanges: changes, Artifacts: artifacts, ArtifactsSkipped: artifactsSkipped, ArtifactsTruncated: artifactsTruncated} if budget := budgets[key]; budget != nil { step.Transcript = stage.BudgetTranscript(step.Transcript, budget, modelConfig.Timeout, modelConfig.Retries) } diff --git a/internal/server/websocket.go b/internal/server/websocket.go index d3991865..7d071be3 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -16,8 +16,22 @@ import ( "git.a-wan.net/alan/pipelines/internal/protocol" ) -// Handler serves the persistent connection. Canceling ctx stops active clients. +// Options configures the HTTP handler. +type Options struct { + // ArtifactsRoot overrides where stage artifacts live. Empty uses the store's + // directory (the database's parent), matching the data-directory default. + ArtifactsRoot string +} + +// Handler serves the persistent connection with default options. Canceling ctx +// stops active clients. func Handler(ctx context.Context, stores ...*pipeline.Store) http.Handler { + return HandlerWithOptions(ctx, Options{}, stores...) +} + +// HandlerWithOptions serves the persistent connection and the stage-artifact +// endpoint. Canceling ctx stops active clients. +func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipeline.Store) http.Handler { build := buildinfo.Current() started := time.Now() mux := http.NewServeMux() @@ -25,11 +39,21 @@ func Handler(ctx context.Context, stores ...*pipeline.Store) http.Handler { var runErr error if len(stores) > 0 { runs, runErr = newRunner(ctx, stores[0]) + if runErr == nil && options.ArtifactsRoot != "" { + runs.artifactsRoot = options.ArtifactsRoot + } if runErr == nil { runs.schedulerWG.Add(1) go runs.scheduleLoop() } } + mux.HandleFunc("GET /artifacts/{run}/{step}/{name...}", func(w http.ResponseWriter, r *http.Request) { + if runs == nil { + http.NotFound(w, r) + return + } + runs.serveArtifact(w, r) + }) mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if runErr != nil { diff --git a/internal/stage/agent.go b/internal/stage/agent.go index cdf559fe..bb05085d 100644 --- a/internal/stage/agent.go +++ b/internal/stage/agent.go @@ -124,6 +124,7 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s } } prompt += "\nRun directory: " + c.Directory + ". Edit scope: " + editPaths + ". There is no implicit language or extension restriction; explicit stage permissions and run scopes are authoritative. Select verification commands from the actual repository and permitted commands. Older generic language-specific boilerplate is not a runtime permission policy. Do not invent a missing file-list or manual-approval requirement. Do not infer that later stages lack editing because this stage is read-only. Respect this stage's role and available tools. Work in the run directory; do not modify unrelated locations or Pipelines history/configuration. Do not delegate. Commit/push only when the task explicitly requests it. Final response must be only the stage result JSON." + prompt += artifactsPrompt(c.ArtifactsDir) if c.Policy.Shell != nil { prompt += "\nPermitted shell commands: " + strings.Join(c.Policy.Shell, "; ") } @@ -158,6 +159,16 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s return text, err } +// artifactsPrompt is the general, technology-neutral boilerplate that tells an +// agent stage where to save the files it produces. It is empty when the runtime +// did not provide a directory, so existing prompts are unchanged without one. +func artifactsPrompt(directory string) string { + if directory == "" { + return "" + } + return "\nArtifacts directory: " + directory + ". Save any artifacts you produce - images, screenshots, reports, logs - into that directory; they are attached to this stage." +} + // settleV2Usage issues one bounded final session-aggregate read and records the // reported usage on the existing per-session ledger. It is best-effort: any read // or validation failure leaves the snapshot unknown rather than failing the run. diff --git a/internal/stage/artifacts_test.go b/internal/stage/artifacts_test.go new file mode 100644 index 00000000..a30ca152 --- /dev/null +++ b/internal/stage/artifacts_test.go @@ -0,0 +1,35 @@ +package stage + +import ( + "strings" + "testing" +) + +func TestPiEnvironmentCarriesArtifactsDirectory(t *testing.T) { + env := piEnvironment("/tmp/ref", "/data/artifacts/7/3") + want := ArtifactsEnv + "=/data/artifacts/7/3" + for _, entry := range env { + if entry == want { + return + } + } + t.Fatalf("Pi environment missing %q", want) +} + +func TestPiEnvironmentOmitsEmptyArtifactsDirectory(t *testing.T) { + for _, entry := range piEnvironment("/tmp/ref", "") { + if strings.HasPrefix(entry, ArtifactsEnv+"=") { + t.Fatalf("unexpected artifacts entry %q", entry) + } + } +} + +func TestArtifactsPromptStatesDirectory(t *testing.T) { + prompt := artifactsPrompt("/data/artifacts/1/2") + if !strings.Contains(prompt, "Artifacts directory: /data/artifacts/1/2") { + t.Fatalf("prompt missing artifacts directory: %q", prompt) + } + if artifactsPrompt("") != "" { + t.Fatal("empty directory must add no boilerplate") + } +} diff --git a/internal/stage/opencode.go b/internal/stage/opencode.go index 0c6c3c37..862d0b80 100644 --- a/internal/stage/opencode.go +++ b/internal/stage/opencode.go @@ -23,10 +23,16 @@ const maxModelResponse = 1 << 20 // A fresh location-scoped session avoids unrelated user conversation history and // is retained for debugging. Generation is text-only: no agent tools are run. type OpenCodeClient struct { - Directory string // empty uses the pipeline server's working directory - Backend string - Reference string - FormatAudit string + Directory string // empty uses the pipeline server's working directory + Backend string + Reference string + FormatAudit string + // ArtifactsDir is exported to the spawned OpenCode CLI as + // PIPELINES_ARTIFACTS_DIR where the runtime controls the process. V2 agent + // tools execute inside the shared `opencode serve` process, so its + // environment cannot be set per stage; the stage prompt is the guaranteed + // carrier of the artifacts directory there. + ArtifactsDir string Operations *OperationLog OnGenerationSession func(id, backend, reference string, active bool) error connection *opencode.Connection @@ -34,6 +40,10 @@ type OpenCodeClient struct { sessionPollInterval time.Duration // test seam; defaults to defaultSessionPollInterval } +// ArtifactsEnv names the environment variable carrying a stage's artifacts +// directory to a locally spawned harness. +const ArtifactsEnv = "PIPELINES_ARTIFACTS_DIR" + // defaultSessionPollInterval bounds the delay between V2 active-session checks. // The V2 /api/experimental/session/{id}/wait request blocks server-side until the // CLI's own request timeout (observed ~6 minutes, HTTP 499) expires, which a long @@ -118,6 +128,9 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any) } cmd := opencode.CommandContext(ctx, opencode.Executable(), args...) cmd.Dir = directory + if c.ArtifactsDir != "" { + cmd.Env = append(os.Environ(), ArtifactsEnv+"="+c.ArtifactsDir) + } stdout := &boundedBuffer{limit: maxModelResponse} stderr := &boundedBuffer{limit: 8 << 10} cmd.Stdout = stdout diff --git a/internal/stage/pi.go b/internal/stage/pi.go index c4e6e4b1..78f38c2e 100644 --- a/internal/stage/pi.go +++ b/internal/stage/pi.go @@ -25,23 +25,26 @@ type PiClient struct { Executable, Directory, SessionID, Reference, Transcript string Tools []string WritePaths []string - Policy ToolPolicy - OnSession func(string, bool) error - OnOwnership func(string, string, bool) error - WorkStarted bool - cmd *exec.Cmd - in io.WriteCloser - lines chan []byte - readErr chan error - next int - ready, settled bool - stopping bool - runErr error - budget *Budget - tokens int64 - cost float64 - usageSeen map[string]bool - completed int + // ArtifactsDir is exported to the spawned Pi process as + // PIPELINES_ARTIFACTS_DIR so its tools can write stage artifacts. + ArtifactsDir string + Policy ToolPolicy + OnSession func(string, bool) error + OnOwnership func(string, string, bool) error + WorkStarted bool + cmd *exec.Cmd + in io.WriteCloser + lines chan []byte + readErr chan error + next int + ready, settled bool + stopping bool + runErr error + budget *Budget + tokens int64 + cost float64 + usageSeen map[string]bool + completed int } func (c *PiClient) BudgetCapable() bool { return true } @@ -147,7 +150,7 @@ func (c *PiClient) Generate(ctx context.Context, config ModelConfig, prompt stri c.cmd = exec.Command(c.Executable, args...) c.cmd.Dir = c.Directory c.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} - c.cmd.Env = append(os.Environ(), "PI_OFFLINE=1", "PIPELINES_PI_POLICY="+filepath.Join(c.Reference, "policy.json"), "PIPELINES_PI_LEASE="+filepath.Join(c.Reference, "lease"), "PIPELINES_PI_STOPPED="+filepath.Join(c.Reference, "quiescent")) + c.cmd.Env = piEnvironment(c.Reference, c.ArtifactsDir) stderr, err := os.OpenFile(filepath.Join(c.Reference, "stderr.log"), os.O_CREATE|os.O_WRONLY, 0600) if err != nil { return "", err @@ -308,6 +311,19 @@ func (c *PiClient) Generate(ctx context.Context, config ModelConfig, prompt stri return piResult(last) } +// piEnvironment builds the spawned Pi process environment, including the +// per-stage artifacts directory when the runtime provided one. +func piEnvironment(reference, artifactsDir string) []string { + env := append(os.Environ(), "PI_OFFLINE=1", + "PIPELINES_PI_POLICY="+filepath.Join(reference, "policy.json"), + "PIPELINES_PI_LEASE="+filepath.Join(reference, "lease"), + "PIPELINES_PI_STOPPED="+filepath.Join(reference, "quiescent")) + if artifactsDir != "" { + env = append(env, ArtifactsEnv+"="+artifactsDir) + } + return env +} + func (c *PiClient) rpc(ctx context.Context, kind string, fields map[string]any) ([]byte, error) { c.next++ id := fmt.Sprint(c.next) diff --git a/internal/stage/stage.go b/internal/stage/stage.go index f35ccdac..ce875473 100644 --- a/internal/stage/stage.go +++ b/internal/stage/stage.go @@ -14,9 +14,12 @@ type Definition struct { ReportFileArtifacts bool Key string Type string - Blocks map[string]string - Configs map[string]string - Edges map[string]string + // ArtifactsDir is the per-run/step directory the runtime created for this + // visit; empty when the runner did not provide one (for example in tests). + ArtifactsDir string + Blocks map[string]string + Configs map[string]string + Edges map[string]string } // Result selects one declared outcome. The future graph runner owns resolving diff --git a/internal/tui/file_changes.go b/internal/tui/file_changes.go index 1cbb53f5..6528268f 100644 --- a/internal/tui/file_changes.go +++ b/internal/tui/file_changes.go @@ -30,6 +30,52 @@ func fileChangesSummary(step protocol.RunStep) string { return label + " · d view diff" } +func artifactsSummary(step protocol.RunStep) string { + if len(step.Artifacts) == 0 { + if step.Status == "running" { + return "Artifacts · capture pending" + } + return "Artifacts · not recorded for this visit" + } + label := fmt.Sprintf("Artifacts · %d recorded", len(step.Artifacts)) + if step.ArtifactsSkipped > 0 || step.ArtifactsTruncated > 0 { + label += " · partial capture" + } + return label +} + +func humanBytes(size int64) string { + const unit = 1024 + if size < unit { + return fmt.Sprintf("%d B", size) + } + div, exp := int64(unit), 0 + for value := size / unit; value >= unit; value /= unit { + div *= unit + exp++ + } + return fmt.Sprintf("%.1f %ciB", float64(size)/float64(div), "KMGTPE"[exp]) +} + +// artifactLines lists one step's recorded artifacts (name and size) for the +// retained-evidence view, matching the file-changes style. +func artifactLines(step protocol.RunStep) []string { + if len(step.Artifacts) == 0 { + return nil + } + lines := []string{artifactsSummary(step)} + for _, artifact := range step.Artifacts { + lines = append(lines, fmt.Sprintf("%s · %s · %s", ansi.Strip(artifact.Name), humanBytes(artifact.SizeBytes), ansi.Strip(artifact.ContentType))) + } + if step.ArtifactsSkipped > 0 { + lines = append(lines, fmt.Sprintf("%d files excluded from artifact capture.", step.ArtifactsSkipped)) + } + if step.ArtifactsTruncated > 0 { + lines = append(lines, fmt.Sprintf("%d files omitted by artifact capture limits.", step.ArtifactsTruncated)) + } + return lines +} + func diffHighlight(line string) string { line = ansi.Strip(line) line = strings.ReplaceAll(strings.ReplaceAll(line, "\t", " "), "\r", "␍") diff --git a/internal/tui/run_detail.go b/internal/tui/run_detail.go index bcbd8e19..103bf141 100644 --- a/internal/tui/run_detail.go +++ b/internal/tui/run_detail.go @@ -458,11 +458,11 @@ func (m Model) runSectionLines(width int) []string { } } if !m.runSectionClosed(2) { - content[2] = append(content[2], "", fileChangesSummary(step)) + content[2] = append(content[2], "", fileChangesSummary(step), artifactsSummary(step)) } var diff []string if !m.runSectionClosed(5) { - diff = stageDiffLines(step) + diff = append(stageDiffLines(step), artifactLines(step)...) } content = append(content, diff) for _, receipt := range step.Notifications {