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>
This commit is contained in:
2026-12-03 12:55:12 +00:00
parent c4de1de27b
commit f061675fc1
20 changed files with 1344 additions and 180 deletions
+83 -22
View File
@@ -319,7 +319,7 @@ func (r *runner) startWithSchedule(request protocol.Message, origin *pipeline.Sc
return nil, err
}
if existing == 0 {
if err := r.checkWorkspaceFree(workspace); err != nil {
if err := r.checkWorkspaceFree(workspace, request.WritePaths...); err != nil {
r.mu.Unlock()
return nil, err
}
@@ -343,7 +343,7 @@ func (r *runner) startWithSchedule(request protocol.Message, origin *pipeline.Sc
// Another run may have reserved this workspace while the title was
// generated, so re-check before reserving it; a race on the request key is
// handled by the store's idempotent insert.
if err := r.checkAdmissionWorkspace(workspace, existing); err != nil {
if err := r.checkAdmissionWorkspace(workspace, existing, request.WritePaths...); err != nil {
r.mu.Unlock()
return nil, err
}
@@ -380,7 +380,7 @@ func (r *runner) startWithSchedule(request protocol.Message, origin *pipeline.Sc
request.Input["pwd"] = workspace
recheck := r.ctx.Err()
if recheck == nil {
recheck = r.checkAdmissionWorkspace(workspace, existing)
recheck = r.checkAdmissionWorkspace(workspace, existing, request.WritePaths...)
}
if recheck != nil {
r.mu.Unlock()
@@ -543,9 +543,9 @@ var runSettleRead = func(r *runner, ctx context.Context, id int64) (*protocol.Ru
// storage maintenance may have removed a clean inactive tree while an
// admission was generating its title, so it is checked under the same lock
// removal takes. Callers must hold r.mu.
func (r *runner) checkAdmissionWorkspace(workspace string, existing int64) error {
func (r *runner) checkAdmissionWorkspace(workspace string, existing int64, writePaths ...string) error {
if existing == 0 {
if err := r.checkWorkspaceFree(workspace); err != nil {
if err := r.checkWorkspaceFree(workspace, writePaths...); err != nil {
return err
}
}
@@ -557,14 +557,30 @@ func (r *runner) checkAdmissionWorkspace(workspace string, existing int64) error
// checkWorkspaceFree refuses a run when an active or cleanup-unconfirmed run
// reserves an overlapping resolved workspace. Callers must hold r.mu.
func (r *runner) checkWorkspaceFree(workspace string) error {
return r.checkWorkspaceFreeExcept(workspace, 0)
// writePaths are the new run's declared write paths (they decide an overlap
// only between nested folders outside any repository).
func (r *runner) checkWorkspaceFree(workspace string, writePaths ...string) error {
return r.checkWorkspaceFreeExcept(workspace, 0, writePaths...)
}
func (r *runner) checkWorkspaceFreeExcept(workspace string, ownGate int64) error {
func (r *runner) checkWorkspaceFreeExcept(workspace string, ownGate int64, writePaths ...string) error {
own := func() []string { return writePaths }
// pathsOf reads a reserving run's declared write paths only when needed.
pathsOf := func(id int64) func() []string {
return func() []string {
read, cancel := context.WithTimeout(context.WithoutCancel(r.ctx), 5*time.Second)
defer cancel()
paths, err := r.store.RunWritePaths(read, id)
if err != nil {
// Unknown scope: keep the whole folder reserved (fail closed).
return []string{"."}
}
return paths
}
}
for id, entry := range r.active {
if workspacesOverlap(entry.workspace, workspace) {
return fmt.Errorf("run %d is already active in %s, which overlaps this run's workspace %s; wait for run %d to finish or use a different workspace (for example another checkout of the repository)", id, entry.workspace, workspace, id)
if workspaceScopesOverlap(entry.workspace, workspace, pathsOf(id), own) {
return &workspaceBusyError{runID: id, message: fmt.Sprintf("run %d is already active in %s, which overlaps this run's workspace %s; wait for run %d to finish or use a different workspace (for example another checkout of the repository)", id, entry.workspace, workspace, id)}
}
}
check, cancel := context.WithTimeout(context.WithoutCancel(r.ctx), 5*time.Second)
@@ -574,35 +590,76 @@ func (r *runner) checkWorkspaceFreeExcept(workspace string, ownGate int64) error
return fmt.Errorf("read pending export workspace reservations: %w", err)
}
for id, reserved := range reservations {
if id != ownGate && workspacesOverlap(reserved, workspace) {
return fmt.Errorf("run %d is awaiting its session export; workspace %s remains reserved", id, reserved)
if id != ownGate && workspaceScopesOverlap(reserved, workspace, pathsOf(id), own) {
return &workspaceBusyError{runID: id, message: fmt.Sprintf("run %d is awaiting its session export; workspace %s remains reserved", id, reserved)}
}
}
return nil
}
// workspaceBusyError refuses an admission because another run (runID)
// reserves an overlapping workspace; waiting can clear it.
type workspaceBusyError struct {
runID int64
message string
}
func (e *workspaceBusyError) Error() string { return e.message }
// workspacesOverlap reports whether two resolved workspaces are the same
// tree or nested in one another within the same repository. A folder that
// merely holds separate repositories (a hub such as ~/project with clones
// under repos/) does not overlap a run inside one of those repositories, and
// neither do two different repositories nested in one another: each run
// works in its own repository.
// works in its own repository. Two folders outside any repository are not one
// shared tree either: nested, they overlap only where the outer run declared
// write paths reaching the inner folder (see workspaceScopesOverlap).
func workspacesOverlap(a, b string) bool {
within := func(parent, child string) bool {
rel, err := filepath.Rel(parent, child)
return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator))
}
return workspaceScopesOverlap(a, b, nil, nil)
}
// workspaceScopesOverlap is workspacesOverlap with each workspace's declared
// write paths (relative to it). They matter only for two nested folders
// outside any repository; a nil lookup means "declared none".
func workspaceScopesOverlap(a, b string, aPaths, bPaths func() []string) bool {
switch {
case filepath.Clean(a) == filepath.Clean(b):
return true
case within(a, b):
return repositoryRoot(b) == repositoryRoot(a)
case within(b, a):
return repositoryRoot(a) == repositoryRoot(b)
case pathAtOrBelow(a, b):
return nestedWorkspacesOverlap(a, b, aPaths)
case pathAtOrBelow(b, a):
return nestedWorkspacesOverlap(b, a, bPaths)
}
return false
}
// nestedWorkspacesOverlap decides for an inner workspace below outer.
func nestedWorkspacesOverlap(outer, inner string, outerPaths func() []string) bool {
root := repositoryRoot(outer)
if repositoryRoot(inner) != root {
return false // Different repositories: each run works in its own.
}
if root != "" {
return true // The same repository is one shared tree.
}
if outerPaths == nil {
return false
}
for _, path := range outerPaths() {
target := filepath.Join(outer, path)
if pathAtOrBelow(target, inner) || pathAtOrBelow(inner, target) {
return true
}
}
return false
}
// pathAtOrBelow reports whether child is parent or below it.
func pathAtOrBelow(parent, child string) bool {
rel, err := filepath.Rel(parent, child)
return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator))
}
// repositoryRoot is the nearest directory at or above dir holding a .git
// entry (a repository or a linked worktree), or "" outside any repository.
func repositoryRoot(dir string) string {
@@ -1055,7 +1112,7 @@ func (r *runner) decide(request protocol.Message) (*protocol.RunDetail, error) {
return nil, fmt.Errorf("run %d is awaiting session export, not a human decision", detail.ID)
}
if detail.Status == protocol.RunStatusWaiting {
if err := r.checkWorkspaceFree(detail.Workspace); err != nil {
if err := r.checkWorkspaceFree(detail.Workspace, detail.WritePaths...); err != nil {
r.mu.Unlock()
return nil, err
}
@@ -2237,6 +2294,10 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
client.Transcript = piClient.Transcript
} else if modelConfig.Harness == stage.ClaudeBackend {
claudeClient := &stage.ClaudeStageClient{Directory: workspace, Tools: tools, WritePaths: request.WritePaths, ArtifactsDir: artifactsDir, Skills: def.Skills}
if adopted != nil {
// Follow the CLI the previous process left (see run_reattach.go).
claudeClient.Reference = adopted.owner.Reference
}
claudeClient.OnOwnership = func(session, reference string, active bool) error {
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()