package claude import ( "context" "encoding/json" "fmt" "os" "time" "git.a-wan.net/alan/pipelines/internal/claudehost" ) // retainedSessionReadback may reattach an existing verified live host after a // transient startup failure. It never spawns/resumes a CLI or sends a prompt. // Unavailable live work is unknown, not an authoritative empty task/turn set. func (c *Chat) retainedSessionReadback(ctx context.Context, id string) (*session, error) { for { if err := ctx.Err(); err != nil { return nil, err } c.mu.Lock() closed := c.closed c.mu.Unlock() if closed { return nil, context.Canceled } s := c.session(id) if s != nil { s.mu.Lock() attached := s.proc != nil waiting := s.starting s.mu.Unlock() if attached { return s, s.awaitReadbackReady(ctx) } if waiting != nil { select { case <-waiting: continue case <-ctx.Done(): return nil, ctx.Err() } } } var retained *claudehost.Record if c.hostDir != "" { path := claudehost.PathsFor(c.hostDir, id).Record data, err := os.ReadFile(path) if err != nil && !os.IsNotExist(err) { return nil, fmt.Errorf("read Claude Code host reference: %w", err) } if err == nil { var record claudehost.Record if json.Unmarshal(data, &record) != nil || record.SessionID != id || record.Socket == "" { return nil, fmt.Errorf("invalid retained Claude Code host reference for %s", id) } if hostReferenceLive(record) { retained = &record } } if retained == nil { // A hosted session without a live retained owner has no work // to reattach; do not reuse stale cached turn/task liveness. return nil, nil } } else { // Direct-mode callers retain their existing in-memory behavior. return s, nil } if s == nil { s = c.sessionFor(id, retained.Directory) } s.mu.Lock() if s.proc != nil || s.starting != nil { s.mu.Unlock() continue } wait := make(chan struct{}) s.starting = wait s.mu.Unlock() p, conn, plan := c.connectHostContext(ctx, *retained) c.mu.Lock() closed = c.closed c.mu.Unlock() if p != nil && (ctx.Err() != nil || closed) { _ = conn.Close() p = nil } s.mu.Lock() if p != nil { s.proc = p s.adoptLocked(p) } s.starting = nil close(wait) s.mu.Unlock() if p == nil { if err := ctx.Err(); err != nil { return nil, err } if closed { return nil, context.Canceled } return nil, fmt.Errorf("Claude Code host for %s is unavailable; retained work was not stopped", id) } go p.readHost(conn, plan) return s, s.awaitReadbackReady(ctx) } } // A replay deadline must propagate as unknown to startup recovery. Otherwise an // empty cache can incorrectly settle/delete the durable live-session journal. func (s *session) awaitReadbackReady(ctx context.Context) error { if err := ctx.Err(); err != nil { return err } s.mu.Lock() p := s.proc s.mu.Unlock() if p == nil { return nil } timer := time.NewTimer(10 * time.Second) defer timer.Stop() select { case <-p.ready: return nil case <-p.done: return fmt.Errorf("Claude Code host disconnected before replay completed") case <-ctx.Done(): return ctx.Err() case <-timer.C: return fmt.Errorf("Claude Code host replay timeout: %w", context.DeadlineExceeded) } }