Files
pipelines/internal/server/session_export_gate.go
T
alan f061675fc1 Claude stages survive server restarts; non-repo folders don't falsely overlap; busy scheduled runs wait
- Claude stage CLIs now run in their own session (setsid) with the prompt from
  a file and stream-json written straight to cli-<n>.out in the evidence
  directory; owner.json records pid/pgid, session, invocation and output. A
  server shutdown leaves the CLI running and owned (ErrDetached). Start-up
  recovery re-adopts it (stage.ClaudeStageReattachable): the next process
  re-reads the output, follows the live CLI until it exits, or reads the
  result it left while the server was down; one that ended without a result
  marks the run interrupted with a clear reason. The outcome repair follow-up
  is re-adopted too (or asked again if its CLI died without a reply).
- Workspace overlap: two folders outside any repository overlap only when
  they are the same folder or the inner one lies within the outer run's
  declared write paths; nested-repository rules are unchanged.
- Scheduled admissions refused for a busy workspace are recorded as
  "delayed" and retried every 5 min for up to 2 h (env-configurable), then
  started or skipped with the reason; the operator gets a low-priority app
  notice (and ntfy when enabled) when a scheduled run is delayed or skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:55:12 +00:00

116 lines
3.8 KiB
Go

package server
import (
"context"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
func completeSessionExport(e *protocol.SessionExport) bool {
return e != nil && e.Status == "complete" && e.SessionID != "" && e.Messages > 0 && e.HTML != "" && e.HTMLBytes > 0 && !e.Truncated && e.Diagnostic == ""
}
// resumeExportGates is also run when no job is due: a manual export repair or
// a crash after persisting repair evidence may have made a handoff eligible.
func (r *runner) resumeExportGates(ctx context.Context) error {
ready, err := r.store.ReadySessionExportGates(ctx)
if err != nil {
return err
}
for id, sequence := range ready {
r.mu.Lock()
if _, active := r.active[id]; active {
r.mu.Unlock()
continue
}
d, err := r.store.GetRun(ctx, id)
if err != nil {
r.mu.Unlock()
return err
}
if err := r.checkWorkspaceFreeExcept(d.Workspace, id, d.WritePaths...); err != nil {
r.mu.Unlock()
continue
}
job, err := r.store.SessionExportJob(ctx, id, sequence)
if err != nil || job == nil {
r.mu.Unlock()
continue
}
var saved *protocol.RunStep
for i := range d.Steps {
if d.Steps[i].Sequence == sequence {
saved = &d.Steps[i]
break
}
}
// An exhausted recovery job is terminal: the visit continues with its
// recorded partial/unavailable evidence (still repairable later)
// rather than holding the run and its workspace forever.
exhausted := job.Status == "exhausted"
verified := exhausted && saved != nil
if !exhausted {
owner, err := r.store.RepairSessionOwner(ctx, id, sequence)
if err != nil || job.SessionID != owner.ID || job.Backend != owner.Backend || job.Directory != owner.Directory || job.Reference != owner.Reference {
r.mu.Unlock()
continue
}
verified = saved != nil && r.verifiedExportRepair(id, sequence, saved, owner)
}
if !verified {
r.mu.Unlock()
continue
}
if err := r.store.ResumeSessionExportGate(ctx, id, sequence); err != nil {
r.mu.Unlock()
continue
}
if exhausted {
log.Printf("run %d visit %d: session export recovery exhausted (%s); continuing with incomplete session evidence", id, sequence, job.Diagnostic)
}
step := *saved
r.emitRun(protocol.RunEventFrame{RunID: id, Workspace: d.Workspace, Status: protocol.RunStatusRunning, CurrentStage: step.Stage, CompletedSteps: completedSteps(d.Steps)}, true)
r.launch(id, d.Workspace, func(runCtx context.Context) { r.resume(runCtx, id, d, d.Workspace, step) })
r.mu.Unlock()
}
return nil
}
// verifiedExportRepair reports whether a complete repair of the exact owner is
// present on disk with its recorded size.
func (r *runner) verifiedExportRepair(id int64, sequence int, saved *protocol.RunStep, owner pipeline.SessionOwner) bool {
for _, e := range saved.SessionExportRepairs {
if !completeSessionExport(e) || e.SessionID != owner.ID || e.Backend != owner.Backend || e.Directory != owner.Directory {
continue
}
// New repairs stay private in the original visit. Historical
// public repairs remain valid, but are never rewritten there.
directory := r.turnPaths(id, sequence, saved.Stage).TurnDir
if strings.HasPrefix(e.HTML, "public/") {
directory = filepath.Join(r.artifactsRoot, strconv.FormatInt(id, 10))
} else if filepath.Base(e.HTML) != e.HTML || !strings.HasPrefix(e.HTML, "session-repair-") {
continue
}
if filepath.Clean(e.HTML) != e.HTML {
continue
}
root, openErr := os.OpenRoot(directory)
if openErr != nil {
continue
}
info, statErr := root.Lstat(e.HTML)
root.Close()
if statErr == nil && info.Mode().IsRegular() && info.Size() == e.HTMLBytes && info.Size() > 0 && info.Size() <= stage.SessionExportFileLimit {
return true
}
}
return false
}