0f5a188c6a
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.
153 lines
4.7 KiB
Go
153 lines
4.7 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"github.com/charmbracelet/lipgloss"
|
|
"github.com/charmbracelet/x/ansi"
|
|
)
|
|
|
|
func fileChangesSummary(step protocol.RunStep) string {
|
|
if step.FileChanges == nil {
|
|
if step.Status == "running" {
|
|
return "File changes · capture pending"
|
|
}
|
|
return "File changes · not recorded for this visit"
|
|
}
|
|
c := step.FileChanges
|
|
if c.Status == "unavailable" {
|
|
return "File changes · capture unavailable"
|
|
}
|
|
label := fmt.Sprintf("File changes · %d observed files", len(c.Files))
|
|
if len(c.Files) == 0 {
|
|
label = "File changes · no changes observed"
|
|
}
|
|
if c.Status == "partial" {
|
|
label += " · partial capture"
|
|
}
|
|
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", "␍")
|
|
line = strings.Map(func(r rune) rune {
|
|
if r < 32 || r == 127 {
|
|
return '�'
|
|
}
|
|
return r
|
|
}, line)
|
|
style := lipgloss.NewStyle()
|
|
switch {
|
|
case strings.HasPrefix(line, "--- "), strings.HasPrefix(line, "+++ "):
|
|
style = style.Foreground(themeMuted).Bold(true)
|
|
case strings.HasPrefix(line, "@@"):
|
|
style = style.Foreground(themeAccent).Bold(true)
|
|
case strings.HasPrefix(line, "+"):
|
|
style = style.Foreground(lipgloss.Color("2"))
|
|
case strings.HasPrefix(line, "-"):
|
|
style = style.Foreground(lipgloss.Color("1"))
|
|
case strings.HasPrefix(line, "\\ "):
|
|
style = style.Foreground(themeMuted)
|
|
}
|
|
return style.Render(line)
|
|
}
|
|
|
|
// HighlightDiff also sanitizes untrusted source before terminal rendering.
|
|
func HighlightDiff(patch string) string {
|
|
lines := strings.Split(strings.TrimSuffix(patch, "\n"), "\n")
|
|
for i, line := range lines {
|
|
lines[i] = diffHighlight(line)
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func stageDiffLines(step protocol.RunStep) []string {
|
|
c := step.FileChanges
|
|
if c == nil {
|
|
return []string{fileChangesSummary(step)}
|
|
}
|
|
lines := []string{fileChangesSummary(step), "Observed workspace changes during this visit; not proof of exclusive authorship."}
|
|
if c.Scope != "" {
|
|
lines = append(lines, c.Scope)
|
|
}
|
|
if c.Diagnostic != "" {
|
|
lines = append(lines, lipgloss.NewStyle().Foreground(themeError).Render(ansi.Strip(c.Diagnostic)))
|
|
}
|
|
if c.Skipped > 0 {
|
|
lines = append(lines, fmt.Sprintf("%d files excluded from capture.", c.Skipped))
|
|
}
|
|
if c.Omitted > 0 {
|
|
lines = append(lines, fmt.Sprintf("%d additional changed files omitted by capture limits.", c.Omitted))
|
|
}
|
|
for _, file := range c.Files {
|
|
lines = append(lines, "", lipgloss.NewStyle().Bold(true).Render(fmt.Sprintf("%s · %q", file.Kind, file.Path)))
|
|
if file.BeforeMode != file.AfterMode {
|
|
lines = append(lines, fmt.Sprintf("Mode: %s → %s", file.BeforeMode, file.AfterMode))
|
|
}
|
|
if file.Binary {
|
|
lines = append(lines, "Binary file changed · contents not displayed.")
|
|
}
|
|
if file.Patch != "" {
|
|
for _, line := range strings.Split(strings.TrimSuffix(file.Patch, "\n"), "\n") {
|
|
lines = append(lines, diffHighlight(line))
|
|
}
|
|
}
|
|
if file.Truncated {
|
|
lines = append(lines, lipgloss.NewStyle().Foreground(themeMuted).Render("Patch omitted or truncated by capture limits."))
|
|
}
|
|
if !file.Binary && file.Patch == "" && !file.Truncated {
|
|
lines = append(lines, "No text hunk · empty file or file-mode change.")
|
|
}
|
|
}
|
|
return lines
|
|
}
|