fix(audit): expose verified visit exports and export-only recovery
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user