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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user