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:
2026-10-15 17:51:18 +01:00
parent 370eaaed1f
commit 0f5a188c6a
19 changed files with 905 additions and 49 deletions
@@ -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 (`<data>/artifacts/<run>/<step>/`); 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/<run>/<step>/<name>` 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