Documentation
¶
Overview ¶
Package shutdown manages graceful daemon shutdown and orphan prevention.
On shutdown:
- Stop accepting new work
- Wait for active workers to finish (up to grace period)
- Kill remaining workers
- Clean up worktrees
- Update state.db
- Remove PID file
On startup:
- Detect stale workers from previous crash
- Kill orphaned claude processes
- Clean up abandoned worktrees
- Reset worker states in DB
Index ¶
- Constants
- type Manager
- func (m *Manager) CleanupOrphans() (cleaned int)
- func (m *Manager) CleanupWorktrees()
- func (m *Manager) GracefulShutdown() int
- func (m *Manager) RecoverOrphanedBeads() (recovered int)
- func (m *Manager) ResetBead(beadID, anvilPath string) error
- func (m *Manager) SetCrucibleActiveCheck(fn func(beadID, anvil string) bool)
- func (m *Manager) SetGracePeriod(d time.Duration)
Constants ¶
const ( // DefaultGracePeriod is how long to wait for workers to finish. DefaultGracePeriod = 60 * time.Second // KillTimeout is how long to wait after SIGTERM before SIGKILL. KillTimeout = 10 * time.Second )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// OnOrphanFound, when set, is called when an orphaned bead is detected
// before auto-recovery. If it returns true, the bead has been handled
// externally (e.g. deferred to the Hearth dialog) and should NOT be
// auto-recovered. If nil or returns false, auto-recovery proceeds as
// before. Set by the daemon after construction.
OnOrphanFound func(beadID, anvil, title, branch string) bool
// contains filtered or unexported fields
}
Manager handles graceful shutdown and orphan cleanup.
func NewManager ¶
func NewManager(db *state.DB, wm *worktree.Manager, logger *slog.Logger, anvils map[string]string) *Manager
NewManager creates a new shutdown manager.
func (*Manager) CleanupOrphans ¶
CleanupOrphans detects and cleans up orphaned resources from a previous crash. Call this on daemon startup.
func (*Manager) CleanupWorktrees ¶
func (m *Manager) CleanupWorktrees()
CleanupWorktrees removes all worktrees across all anvils (for full shutdown).
func (*Manager) GracefulShutdown ¶
GracefulShutdown performs an orderly shutdown of all active workers. Returns the number of workers that had to be forcefully killed.
func (*Manager) RecoverOrphanedBeads ¶
RecoverOrphanedBeads detects beads with status=in_progress in the beads DB that have no active worker and no open PR in Forge's state DB. These are beads that were claimed but orphaned (e.g., daemon crashed mid-session). Only beads belonging to this Forge's configured anvils are considered, and only beads that Forge has previously claimed (i.e., have a worker record in state.db) are eligible for recovery — beads set to in_progress by humans or external tools are left untouched. This runs both at startup and periodically during normal operation (every 10 poll cycles) so recovery is not limited to crash scenarios. Returns the number of beads recovered.
func (*Manager) ResetBead ¶ added in v0.4.0
ResetBead marks a bead as open via bd update and clears the assignee. This is exported so the daemon's IPC handler can reset a bead when the user chooses the "Recover" action in the orphan dialog.
func (*Manager) SetCrucibleActiveCheck ¶ added in v0.3.0
SetCrucibleActiveCheck registers a callback that orphan recovery uses to determine whether a bead ID in a given anvil has an active Crucible run. If the callback returns true the bead is skipped — it is not orphaned, just managed by the Crucible rather than a direct worker row. The anvil parameter scopes the check so that two anvils with the same bead ID are handled independently.
func (*Manager) SetGracePeriod ¶
SetGracePeriod configures the shutdown grace period.