package server import ( "context" "crypto/rand" "encoding/hex" "fmt" "os" "path/filepath" "strconv" "strings" "time" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" ) // repairSessionExport is runtime-owned and read-only to OpenCode. It never // resumes a session, runs a stage, changes a definition, or overwrites evidence. func (r *runner) repairSessionExport(ctx context.Context, id int64, sequence int) (*protocol.RunDetail, error) { return r.repairSessionExportOwned(ctx, id, sequence, nil) } func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequence int, expected *pipeline.SessionOwner) (*protocol.RunDetail, error) { r.exportRepairMu.Lock() defer r.exportRepairMu.Unlock() if id <= 0 || sequence <= 0 { return nil, fmt.Errorf("positive run ID and export_sequence are required") } r.mu.Lock() _, executing := r.active[id] r.mu.Unlock() if executing { return nil, fmt.Errorf("run execution cleanup is still active; repair deferred") } d, err := r.store.GetRun(ctx, id) if err != nil { return nil, err } 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 for i := range d.Steps { if d.Steps[i].Sequence == sequence { step = &d.Steps[i] break } } if step == nil || step.AgentActive { return nil, fmt.Errorf("no settled recorded session for this visit") } if len(step.SessionExportRepairs) >= 20 { return nil, fmt.Errorf("repair attempt limit reached for this visit") } owner, err := r.store.RepairSessionOwner(ctx, id, sequence) if err != nil { return nil, fmt.Errorf("exact original session ownership metadata unavailable: %w", err) } if expected != nil && (owner.ID != expected.ID || owner.Backend != expected.Backend || owner.Directory != expected.Directory || owner.Reference != expected.Reference) { return nil, fmt.Errorf("original session ownership differs from durable recovery snapshot; export refused") } active, err := r.store.ActiveSessionOwners(ctx) if err != nil { return nil, err } for _, current := range active { if current.ID == owner.ID { return nil, fmt.Errorf("original session is still active; export repair deferred") } } evidence := &protocol.SessionExport{SessionID: owner.ID, Backend: owner.Backend, Directory: owner.Directory, RecordedAt: time.Now().UTC().Format(time.RFC3339), Source: "native_export"} // Capture and retain failures too. The diagnostic is evidence, not a new // run outcome, and cannot change the original stage/run success status. recordFailure := func(message string) (*protocol.RunDetail, error) { recordCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second) defer cancel() evidence.Status = "failed" evidence.Diagnostic = stage.RedactEvidence(message) if err := r.store.RecordSessionExportRepair(recordCtx, id, sequence, evidence); err != nil { return nil, err } return r.sessionExportUpdatedDetail(recordCtx, id) } if owner.Backend != "v1" && owner.Backend != "v2" && owner.Backend != "pi" { return recordFailure("native export unavailable for original harness") } root, err := os.OpenRoot(r.artifactsRoot) if err != nil { return recordFailure(err.Error()) } defer root.Close() run := strconv.FormatInt(id, 10) 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 { return recordFailure(err.Error()) } continue } if err != nil { return recordFailure(err.Error()) } if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 { return recordFailure("repair directory is not a normal directory") } } var nonce [16]byte if _, err := rand.Read(nonce[:]); err != nil { return recordFailure(err.Error()) } name := fmt.Sprintf("session-repair-%d-%s.html", sequence, hex.EncodeToString(nonce[:])) // Stage.ExportSession is a runtime rendering helper, not a model stage. // No prompts/configs/definitions are involved. Export uses original owner. exportCtx, cancel := context.WithTimeout(ctx, stage.SessionExportTimeout) defer cancel() export := r.exportSession if export == nil { export = stage.ExportSession } 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 if err != nil { return recordFailure(err.Error()) } evidence.Status = result.Status evidence.Source = result.Source evidence.Messages = result.Messages evidence.Truncated = result.Truncated evidence.Diagnostic = result.Diagnostic if result.HTML != "" { if result.HTML != name { return recordFailure("repair exporter returned an unexpected filename") } 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 || info.Size() != result.HTMLBytes { return recordFailure("repair export missing, empty, non-normal or oversized") } // Mandatory audit repair has its own bounded allowance: at most twenty // attempts, each capped by SessionExportFileLimit. A full ordinary file // inventory (count OR bytes) must not prevent recovery of the handoff. 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()) } } classifySessionExport(evidence) if err := r.store.RecordSessionExportRepair(persistCtx, id, sequence, evidence); err != nil { return nil, err } 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, ExportPendingSequence: detail.ExportPendingSequence, CurrentStage: detail.CurrentStage, CompletedSteps: detail.CompletedSteps, SessionExportUpdated: true}, true) } return detail, err }