fix(audit): expose verified visit exports and export-only recovery

This commit is contained in:
2026-10-20 21:20:06 +01:00
parent f87e1ed488
commit ca78b24f47
14 changed files with 359 additions and 59 deletions
+43 -26
View File
@@ -54,8 +54,46 @@ func (r *runner) scanArtifacts(runID int64, sequence int, directory string, publ
return nil, 0, 0
}
files := []protocol.StageArtifact{}
seen := map[string]bool{}
skipped, truncated := 0, 0
used := int64(0)
add := func(full, name string, info fs.FileInfo) bool {
if seen[full] {
return true
}
if len(files) >= maxArtifactFiles {
skipped++
return false
}
if info.Size() > maxArtifactFileBytes {
skipped++
return true
}
if used+info.Size() > maxArtifactTotalBytes {
truncated++
return false
}
contentType := mime.TypeByExtension(filepath.Ext(name))
if contentType == "" {
contentType = sniffArtifact(root, full)
if contentType == "" {
skipped++
return true
}
}
used += info.Size()
seen[full] = true
files = append(files, protocol.StageArtifact{Name: name, SizeBytes: info.Size(), ContentType: contentType, URL: artifactURL(runID, sequence, name)})
return true
}
// Mandatory runtime evidence must not disappear behind hundreds of private
// captures/public deliverables in the bounded inventory.
for _, name := range []string{"input.md", "memory.md", "output.md", "session.html"} {
full := path.Join(rel, name)
if info, err := root.Lstat(full); err == nil && info.Mode().IsRegular() && info.Mode()&fs.ModeSymlink == 0 && info.Size() <= maxArtifactFileBytes {
add(full, name, info)
}
}
scan := func(relative, prefix string) {
_ = fs.WalkDir(root.FS(), relative, func(name string, entry fs.DirEntry, walkErr error) error {
if walkErr != nil {
@@ -65,6 +103,9 @@ func (r *runner) scanArtifacts(runID int64, sequence int, directory string, publ
if name == relative || entry.IsDir() {
return nil
}
if seen[name] {
return nil
}
if entry.Type()&fs.ModeSymlink != 0 || !entry.Type().IsRegular() {
skipped++
return nil
@@ -74,36 +115,12 @@ func (r *runner) scanArtifacts(runID int64, sequence int, directory string, publ
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, relative+"/")
artifactName = prefix + artifactName
full := path.Join(relative, strings.TrimPrefix(artifactName, prefix))
contentType := mime.TypeByExtension(filepath.Ext(artifactName))
if contentType == "" {
contentType = sniffArtifact(root, full)
if contentType == "" {
skipped++
return nil
}
if !add(full, artifactName, info) {
return fs.SkipAll
}
used += info.Size()
files = append(files, protocol.StageArtifact{
Name: artifactName,
SizeBytes: info.Size(),
ContentType: contentType,
URL: artifactURL(runID, sequence, artifactName),
})
return nil
})
}
+28
View File
@@ -29,6 +29,34 @@ func artifactNamed(files []protocol.StageArtifact, name string) (protocol.StageA
return protocol.StageArtifact{}, false
}
func TestSessionEvidenceIsInventoriedBeforeLargeCaptureDirectories(t *testing.T) {
root := t.TempDir()
dir := filepath.Join(root, "1", "stages", "01-test")
if err := os.MkdirAll(filepath.Join(dir, "private"), 0700); err != nil {
t.Fatal(err)
}
for i := 0; i < maxArtifactFiles+10; i++ {
if err := os.WriteFile(filepath.Join(dir, "private", fmt.Sprintf("capture-%03d.txt", i)), []byte("fixture"), 0600); err != nil {
t.Fatal(err)
}
}
for _, name := range []string{"input.md", "memory.md", "output.md", "session.html"} {
if err := os.WriteFile(filepath.Join(dir, name), []byte("runtime evidence"), 0600); err != nil {
t.Fatal(err)
}
}
r := &runner{artifactsRoot: root}
files, skipped, _ := r.scanArtifacts(1, 1, dir)
if len(files) > maxArtifactFiles || skipped == 0 {
t.Fatalf("inventory bound lost: %d %d", len(files), skipped)
}
for _, name := range []string{"input.md", "memory.md", "output.md", "session.html"} {
if _, ok := artifactNamed(files, name); !ok {
t.Fatalf("mandatory runtime file %s hidden by user captures", name)
}
}
}
func TestArtifactsScanRecordsSizesTypesAndSkips(t *testing.T) {
root := t.TempDir()
stepDir := filepath.Join(root, "4", "2")
+33 -13
View File
@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
"git.a-wan.net/alan/pipelines/internal/pipeline"
@@ -37,7 +38,11 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc
if err != nil {
return nil, err
}
if d.Status == "running" || d.Status == "queued" || d.Status == "waiting" {
pendingGate, gateErr := r.store.PendingSessionExportGate(ctx, id, sequence)
if gateErr != nil {
return nil, gateErr
}
if d.Status == "running" || d.Status == "queued" || (d.Status == "waiting" && !pendingGate) {
return nil, fmt.Errorf("session export repair requires a settled run")
}
var step *protocol.RunStep
@@ -80,22 +85,28 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc
if err := r.store.RecordSessionExportRepair(recordCtx, id, sequence, evidence); err != nil {
return nil, err
}
return r.store.GetRun(recordCtx, id)
return r.sessionExportUpdatedDetail(recordCtx, id)
}
if owner.Backend != "v1" && owner.Backend != "v2" {
if owner.Backend != "v1" && owner.Backend != "v2" && owner.Backend != "pi" {
return recordFailure("native export unavailable for original harness")
}
if len(step.Artifacts) >= maxArtifactFiles {
return recordFailure("artifact inventory full; repair not attempted")
}
root, err := os.OpenRoot(r.artifactsRoot)
if err != nil {
return recordFailure(err.Error())
}
defer root.Close()
run := strconv.FormatInt(id, 10)
public := filepath.Join(run, "public")
for _, dir := range []string{run, public} {
if step.Stage == "" || step.Stage == "." || step.Stage == ".." || strings.ContainsAny(step.Stage, "/\\") {
return recordFailure("invalid recorded stage key")
}
visit := filepath.Join(run, "stages", fmt.Sprintf("%02d-%s", sequence, step.Stage))
legacy := filepath.Join(run, strconv.Itoa(sequence))
if !containedDirectory(root, filepath.ToSlash(visit)) && containedDirectory(root, filepath.ToSlash(legacy)) {
visit = legacy
}
// Audit records belong to this visit, never shared public/: the next stage
// must not receive the previous agent's private tool/transcript contents.
for _, dir := range []string{run, filepath.Dir(visit), visit} {
info, err := root.Lstat(dir)
if os.IsNotExist(err) {
if err := root.Mkdir(dir, 0700); err != nil {
@@ -123,7 +134,7 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc
if export == nil {
export = stage.ExportSession
}
result, err := export(exportCtx, stage.SessionExportRequest{SessionID: owner.ID, Backend: owner.Backend, Directory: owner.Directory, ArtifactsDir: filepath.Join(r.artifactsRoot, public), FileName: name, RunID: id, Sequence: sequence, Stage: step.Stage, Provider: step.Provider, Model: step.Model})
result, err := export(exportCtx, stage.SessionExportRequest{SessionID: owner.ID, Backend: owner.Backend, Reference: owner.Reference, Directory: owner.Directory, ArtifactsDir: filepath.Join(r.artifactsRoot, visit), FileName: name, RunID: id, Sequence: sequence, Stage: step.Stage, Provider: step.Provider, Model: step.Model})
persistCtx, persistCancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer persistCancel()
evidence.Attempts = result.Attempts
@@ -131,6 +142,7 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc
return recordFailure(err.Error())
}
evidence.Status = result.Status
evidence.Source = result.Source
evidence.Messages = result.Messages
evidence.Truncated = result.Truncated
evidence.Diagnostic = result.Diagnostic
@@ -138,11 +150,11 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc
if result.HTML != name {
return recordFailure("repair exporter returned an unexpected filename")
}
info, err := root.Lstat(filepath.Join(public, name))
info, err := root.Lstat(filepath.Join(visit, name))
if err != nil {
return recordFailure(err.Error())
}
if !info.Mode().IsRegular() || info.Size() <= 0 || info.Size() > stage.SessionExportFileLimit || result.Messages <= 0 {
if !info.Mode().IsRegular() || info.Size() <= 0 || info.Size() > stage.SessionExportFileLimit || result.Messages <= 0 || info.Size() != result.HTMLBytes {
return recordFailure("repair export missing, empty, non-normal or oversized")
}
var used int64
@@ -152,7 +164,7 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc
if used+info.Size() > maxArtifactTotalBytes {
return recordFailure("repaired export exceeds artifact inventory byte bounds")
}
evidence.HTML = "public/" + name
evidence.HTML = name
evidence.HTMLBytes = info.Size()
if err := r.store.RecordDeliverable(persistCtx, id, sequence, protocol.StageArtifact{Name: evidence.HTML, SizeBytes: info.Size(), ContentType: "text/html; charset=utf-8", URL: artifactURL(id, sequence, evidence.HTML)}); err != nil {
return recordFailure(err.Error())
@@ -162,5 +174,13 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc
if err := r.store.RecordSessionExportRepair(persistCtx, id, sequence, evidence); err != nil {
return nil, err
}
return r.store.GetRun(persistCtx, id)
return r.sessionExportUpdatedDetail(persistCtx, id)
}
func (r *runner) sessionExportUpdatedDetail(ctx context.Context, id int64) (*protocol.RunDetail, error) {
detail, err := r.store.GetRun(ctx, id)
if err == nil {
r.emitRun(protocol.RunEventFrame{RunID: id, PipelineID: detail.PipelineID, Name: detail.Name, Title: detail.Title, Version: detail.Version, Workspace: detail.Workspace, Status: detail.Status, CurrentStage: detail.CurrentStage, CompletedSteps: detail.CompletedSteps, SessionExportUpdated: true}, true)
}
return detail, err
}
@@ -68,7 +68,7 @@ func TestRuntimeSessionExportRepairPreservesOriginalAndUsesRecordedOwner(t *test
t.Fatalf("repair changed run outcome: %+v", repaired)
}
latest := repaired.Steps[0].SessionExportRepairs[1]
if latest.Status != "complete" || latest.Messages != 2 || latest.SessionID != owner.ID || latest.Backend != owner.Backend || !strings.HasPrefix(latest.HTML, "public/session-repair-1-") {
if latest.Status != "complete" || latest.Messages != 2 || latest.SessionID != owner.ID || latest.Backend != owner.Backend || !strings.HasPrefix(latest.HTML, "session-repair-1-") {
t.Fatalf("repair not recorded: %+v", latest)
}
after, _ := json.Marshal(repaired.Steps[0].SessionExport)
@@ -79,7 +79,7 @@ func TestRuntimeSessionExportRepairPreservesOriginalAndUsesRecordedOwner(t *test
if string(body) != "historical evidence" {
t.Fatal("historical export overwritten")
}
body, err = os.ReadFile(filepath.Join(h.runner.artifactsRoot, "1", latest.HTML))
body, err = os.ReadFile(filepath.Join(h.runner.artifactsRoot, "1", "stages", "01-"+repaired.Steps[0].Stage, latest.HTML))
if err != nil || len(body) == 0 {
t.Fatalf("repair file missing: %v", err)
}
@@ -141,7 +141,7 @@ func TestCLIRepairSessionExportUsesRuntimeProtocol(t *testing.T) {
SessionID string `json:"session_id"`
HTML string `json:"html"`
}
if err := json.Unmarshal(out.Bytes(), &evidence); err != nil || evidence.Status != "complete" || evidence.SessionID != "ses_run" || !strings.HasPrefix(evidence.HTML, "public/session-repair-1-") {
if err := json.Unmarshal(out.Bytes(), &evidence); err != nil || evidence.Status != "complete" || evidence.SessionID != "ses_run" || !strings.HasPrefix(evidence.HTML, "session-repair-1-") {
t.Fatalf("wrong repair reply: %+v %v %s", evidence, err, out.String())
}
}