Files
kandev/apps/backend/internal/agent/runtime/lifecycle/manager.go
T
João Salavisa de92557f46 test(backend): wire goleak goroutine-leak detection across 8 packages (#1139)
* chore(backend): add go.uber.org/goleak test dependency

Promotes the previously-indirect goleak v1.3.0 to a direct require so
per-package TestMain calls can use it for goroutine leak detection.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(backend/lifecycle): drain workspace-stream backoff on Manager.Stop

StreamManager.connectWorkspaceStream slept the full retry backoff in
time.Sleep between failed attempts. SessionTraceContext is deliberately
uncancellable (it carries a long-lived span), so if the Manager began
shutting down mid-backoff the goroutine outlived Stop() and stranded
until the timer fired.

Thread Manager.stopCh through the StreamManager constructor and add a
sleepOrStop helper that selects between the timer and the shutdown
signal. ReconnectAll's startup sleep gets the same treatment.

TestConnectWorkspaceStream_BackoffDrainsOnStop closes stopCh while a
failing-port retry is in flight and asserts the goroutine exits within
500ms — fails without the fix, passes with it.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(backend/github): close sqlite DB in newTestStore to drain connectionOpener

newTestStore opened *sqlx.DB via db.OpenSQLite but never registered a
Close on cleanup, leaving database/sql.(*DB).connectionOpener goroutines
parked across test boundaries. Add t.Cleanup(sqlxDB.Close); goleak's
per-package TestMain (added in a follow-up commit) is the regression
check.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(backend): wire goleak.VerifyTestMain across leak-prone packages

Per-package TestMain in:
- internal/integrations/healthpoll (auth-health ticker loop)
- internal/jira, internal/linear (auth + issue-watch loops)
- internal/github (PR / review / issue poller loops)
- internal/gateway/websocket (per-conn pumps + terminal bridge)
- internal/orchestrator (detached event-handler goroutines)
- internal/agent/runtime/lifecycle (remote-status loop, StreamManager
  helpers; ignores OTel batch-span processor and OTLP retry helper —
  initialised early in the process and only exit on shutdown)
- internal/agentctl/server/process (extends the existing fixture-binary
  TestMain so subprocess + PTY pump leaks surface there)

Catches regressions where a Start/Stop forgets to drain on WaitGroup,
or a detached goroutine forgets to honour ctx/stopCh cancellation.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* docs(backend): document goroutine ownership and leak-testing convention

New "Goroutine ownership and leak testing" subsection under Conventions
in apps/backend/AGENTS.md (CLAUDE.md symlinks to it). Covers:

- Lifecycle: Start/Stop + WaitGroup, both idempotent.
- Cancellation: every long wait selects on ctx.Done() or stopCh; never
  time.Sleep in retry loops (reference lifecycle.StreamManager.sleepOrStop).
- Detached helpers: must respect the owning type's shutdown signal.
- Leak testing: new packages spawning goroutines add goleak.VerifyTestMain;
  IgnoreTopFunction is reserved for genuine third-party background
  goroutines and must carry a comment explaining the suppression.

Lists the packages currently instrumented so future contributors can
mirror the pattern.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* chore(backend/lifecycle): drop dead OTel IgnoreTopFunction from goleak setup

Review follow-up. agentctl/tracing.initTracing only spawns the OTel SDK
provider when OTEL_EXPORTER_OTLP_ENDPOINT is set; in tests it isn't, so
the noop provider is used and no batchSpanProcessor / OTLP retry
goroutines ever exist. The IgnoreTopFunction calls were dead
suppressions and the comment ("called by NewManager via the agent
execution trace path") misdescribed when the SDK init runs. Drop the
ignores and correct AGENTS.md to stop citing OTel as the canonical
suppression example.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(backend/lifecycle): drop unneeded 50ms sleep from backoff-drain test

QA follow-up. The 50ms time.Sleep before close(stopCh) in
TestConnectWorkspaceStream_BackoffDrainsOnStop was meant to ensure the
goroutine had entered the backoff sleep before shutdown — but that
ordering doesn't affect the test outcome:

  - With the fix: sleepOrStop sees stopCh already closed and returns
    false on the first call, so the goroutine drains within one attempt
    regardless of when stopCh closes.
  - Without the fix: time.Sleep ignores stopCh, the goroutine burns
    its full 5-retry backoff (~31s), and the 500ms timeout fires.

Removing the sleep makes the test ~12x faster (15ms vs 200ms total) and
removes the only time.Sleep-based wait from the new tests, aligning
with the codebase's "channel-based sync, no sleep waits" convention.
Verified by 50x -race and 5x revert-and-fail.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(backend/github): drain HTTP idle conns before goleak inspects

CI surfaced a leaked goroutine on the Linux runner that local runs
missed: a single net/http.(*http2ClientConn).readLoop parked on a
TLS read, created by net/http.(*http2Transport).newClientConn. The
package's anonymousHTTPClient and PATClient's rewriteTransport both
route through http.DefaultTransport's connection pool, so an idle
HTTP/2 connection in that pool keeps the reader goroutine alive past
m.Run() — invisible on sandboxed local runs where outbound network
is blocked.

Replace github/goleak_test.go's plain VerifyTestMain with an
expanded TestMain that drains http.DefaultTransport's idle
connections before goleak.Find runs. This is the stdlib's
documented teardown for the connection pool, keeps the assertion
strict (no IgnoreTopFunction), and doesn't leak into production
code. Other in-scope packages don't need this — they don't use
http.DefaultTransport for HTTPS calls.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(backend): drain ws.Done on stopCh; always run goleak in github tests

- lifecycle/streams.go: replace bare <-ws.Done() with a select that also
  watches sm.stopCh so a connected goroutine drains when Manager.Stop()
  fires before the remote WS closes. A nil stopCh (isolated tests) blocks
  the nil channel case forever, degrading to plain <-ws.Done() semantics.
- github/goleak_test.go: remove the exitCode==0 guard around goleak.Find
  so leaks are always surfaced even when a test has already failed; original
  exit code is preserved (leaks only promote it to 1).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(backend/lifecycle): close ws on stopCh so agentctl WS loops drain

cubic flagged that the previous shutdown path observed stopCh but never
called ws.Close(), so the underlying WebSocket and its agentctl-side
read/write goroutines (workspace_stream.go:67-68) kept running past
Manager.Stop(). ClearWorkspaceStream only removes the WS from the
execution's tracking — it doesn't close the connection.

Call ws.Close() on the stopCh path. ws.Close is idempotent via
closeOnce, so it's safe even if the remote end already closed.

TestConnectWorkspaceStream_ClosesWSOnStop connects to a mock workspace
server that stays open until the client closes its side; after stopCh
fires, the test asserts ws.Done() closes within 500ms — which is only
true if connectWorkspaceStream invoked ws.Close(). Verified to fail
without the fix and pass with it.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(backend/lifecycle): use channel sync instead of sleep polling in ws-close test

cubic flagged that TestConnectWorkspaceStream_ClosesWSOnStop polled
execution.GetWorkspaceStream() in a 500ms sleep loop, which is brittle
under CI load. connectWorkspaceStream already accepts a ready
channel that closes after SetWorkspaceStream — pass it in and wait on
the channel instead.

synctest isn't viable here (real network I/O), so channel-based sync is
the testing convention per AGENTS.md.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-29 10:23:18 +01:00

345 lines
14 KiB
Go

// Package lifecycle manages agent instance lifecycles including tracking,
// state transitions, and cleanup.
package lifecycle
import (
"sync"
"sync/atomic"
"time"
"go.uber.org/zap"
"golang.org/x/sync/singleflight"
"github.com/kandev/kandev/internal/agent/docker"
"github.com/kandev/kandev/internal/agent/executor"
"github.com/kandev/kandev/internal/agent/registry"
agentctl "github.com/kandev/kandev/internal/agent/runtime/agentctl"
"github.com/kandev/kandev/internal/agent/runtime/routingerr"
"github.com/kandev/kandev/internal/common/logger"
"github.com/kandev/kandev/internal/events/bus"
"github.com/kandev/kandev/internal/secrets"
"github.com/kandev/kandev/internal/task/models"
"github.com/kandev/kandev/internal/worktree"
)
// ExecutorFallbackPolicy controls behavior when a requested runtime is unavailable.
type ExecutorFallbackPolicy string
const (
// ExecutorFallbackAllow silently falls back to the default runtime (current behavior).
ExecutorFallbackAllow ExecutorFallbackPolicy = "allow"
// ExecutorFallbackWarn falls back but logs a warning (current behavior, explicit).
ExecutorFallbackWarn ExecutorFallbackPolicy = "warn"
// ExecutorFallbackDeny returns an error if the requested runtime is unavailable.
ExecutorFallbackDeny ExecutorFallbackPolicy = "deny"
)
// Manager manages agent instance lifecycles
type Manager struct {
registry *registry.Registry
eventBus bus.EventBus
credsMgr CredentialsManager
profileResolver ProfileResolver
worktreeMgr *worktree.Manager
mcpProvider McpConfigProvider
logger *logger.Logger
// dataDir is the kandev root directory. Misnamed for historical reasons:
// cmd/kandev/agents.go passes cfg.ResolvedHomeDir() (the kandev root —
// typically ~/.kandev) here, not ResolvedDataDir(). Used for:
// - Session history storage (SessionHistoryManager)
// - Ephemeral workspace creation (quick chat) at <root>/quick-chat/<sessionID>
// - Scratch workspaces for repo-less tasks at <root>/tasks/<workspaceID>/<taskID>
dataDir string
// ExecutorRegistry manages multiple runtimes (Docker, Standalone, etc.)
// Each task can select its runtime based on executor type.
executorRegistry *ExecutorRegistry
// executorFallbackPolicy controls behavior when a requested runtime is unavailable.
executorFallbackPolicy ExecutorFallbackPolicy
// Refactored components for separation of concerns
executionStore *ExecutionStore // Thread-safe execution tracking
commandBuilder *CommandBuilder // Builds agent commands from registry config
sessionManager *SessionManager // Handles ACP session initialization
streamManager *StreamManager // Manages WebSocket streams
eventPublisher *EventPublisher // Publishes lifecycle events
historyManager *SessionHistoryManager // Stores session history for context injection (fork_session pattern)
// Workspace info provider for on-demand instance creation
workspaceInfoProvider WorkspaceInfoProvider
// bootMessageService creates boot messages displayed in chat during agent startup.
bootMessageService BootMessageService
// preparerRegistry maps executor types to environment preparers.
preparerRegistry *PreparerRegistry
// mcpHandler is the MCP request dispatcher for handling MCP requests
// from agentctl instances through the agent stream.
mcpHandler agentctl.MCPHandler
// singleflight deduplicates concurrent GetOrEnsureExecution calls for the same session
ensureExecutionGroup singleflight.Group
// Background remote status polling
remoteStatusPollInterval time.Duration
remoteStatusMu sync.RWMutex
remoteStatusBySession map[string]*RemoteStatus
stopCh chan struct{}
wg sync.WaitGroup
// shuttingDown is flipped true when graceful shutdown begins (see
// StopAllAgents) so handlers running in detached goroutines can
// short-circuit work that would otherwise race the teardown and log
// confusing errors against children that already died from the same
// terminal-wide SIGINT.
shuttingDown atomic.Bool
// pollAggregator routes hub session-mode events to agentctl. See
// manager_subscription.go.
pollAggregator *workspacePollAggregator
// secretStore encrypts/decrypts runtime auth tokens (e.g., agentctl handshake tokens).
// Used to persist tokens across backend restarts for remote executor recovery.
secretStore secrets.SecretStore
// runningWriter persists the executors_running row in lockstep with executionStore.
// See SetExecutorRunningWriter and persistence.go. The lifecycle manager is the
// only component allowed to write the lifecycle-owned columns of this table.
runningWriter ExecutorRunningWriter
// agentProfileReader resolves the full agent_profiles row (including the
// office-enrichment fields added in ADR 0005 Wave A) for the launch-prep
// SkillDeployer hook. Nil → skill deploy is skipped.
agentProfileReader AgentProfileReader
// skillDeployer materialises per-profile skills + custom prompt before
// the agent process starts. Defaults to a no-op deployer; office wires
// its concrete implementation via SetSkillDeployer.
skillDeployer SkillDeployer
// remediateNpxCache is the hook fired when the routing classifier
// returns CodeNpxCacheCorrupted. NewManager wires routingerr.RemediateNpxCache;
// tests override it to avoid touching the real filesystem.
remediateNpxCache func(path string, log *zap.Logger) error
}
// NewManager creates a new lifecycle manager.
// The executorRegistry manages multiple runtimes (Docker, Standalone, etc.) for task-specific execution.
// The fallbackPolicy controls behavior when a requested runtime is unavailable.
func NewManager(
reg *registry.Registry,
eventBus bus.EventBus,
executorRegistry *ExecutorRegistry,
credsMgr CredentialsManager,
profileResolver ProfileResolver,
mcpProvider McpConfigProvider,
fallbackPolicy ExecutorFallbackPolicy,
dataDir string,
log *logger.Logger,
) *Manager {
componentLogger := log.WithFields(zap.String("component", "lifecycle-manager"))
// Initialize command builder
commandBuilder := NewCommandBuilder()
// Create stop channel for graceful shutdown
stopCh := make(chan struct{})
// Initialize session manager
sessionManager := NewSessionManager(log, stopCh)
// Initialize event publisher
eventPublisher := NewEventPublisher(eventBus, log)
// Initialize execution store
executionStore := NewExecutionStore()
// Initialize session history manager for fork_session pattern (context injection)
historyManager, err := NewSessionHistoryManager("", dataDir, log)
if err != nil {
log.Warn("failed to create session history manager, context injection disabled", zap.Error(err))
}
mgr := &Manager{
registry: reg,
eventBus: eventBus,
executorRegistry: executorRegistry,
executorFallbackPolicy: fallbackPolicy,
credsMgr: credsMgr,
profileResolver: profileResolver,
mcpProvider: mcpProvider,
logger: componentLogger,
dataDir: dataDir,
executionStore: executionStore,
commandBuilder: commandBuilder,
sessionManager: sessionManager,
eventPublisher: eventPublisher,
historyManager: historyManager,
remoteStatusPollInterval: 60 * time.Second,
remoteStatusBySession: make(map[string]*RemoteStatus),
stopCh: stopCh,
skillDeployer: NoopSkillDeployer(),
remediateNpxCache: routingerr.RemediateNpxCache,
}
// Initialize stream manager with callbacks that delegate to manager methods
// mcpHandler will be set later via SetMCPHandler.
// stopCh is shared with the manager so workspace-stream backoff drains on Stop.
mgr.streamManager = NewStreamManager(log, StreamCallbacks{
OnAgentEvent: mgr.handleAgentEvent,
OnStreamDisconnect: mgr.handleStreamDisconnect,
OnGitStatus: mgr.handleGitStatusUpdate,
OnGitCommit: mgr.handleGitCommitCreated,
OnGitReset: mgr.handleGitResetDetected,
OnBranchSwitch: mgr.handleBranchSwitch,
OnFileChange: mgr.handleFileChangeNotification,
OnShellOutput: mgr.handleShellOutput,
OnShellExit: mgr.handleShellExit,
OnProcessOutput: mgr.handleProcessOutput,
OnProcessStatus: mgr.handleProcessStatus,
}, nil, stopCh)
// Set session manager dependencies for full orchestration
sessionManager.SetDependencies(eventPublisher, mgr.streamManager, executionStore, historyManager)
mgr.pollAggregator = newWorkspacePollAggregator(mgr)
if executorRegistry != nil {
mgr.logger.Info("initialized with runtimes", zap.Int("count", len(executorRegistry.List())))
}
return mgr
}
// HandleSessionMode routes a session-level mode transition (from the gateway
// hub) into the per-workspace aggregator, which pushes the resulting
// workspace-effective mode to agentctl. See manager_subscription.go.
func (m *Manager) HandleSessionMode(sessionID string, mode WorkspacePollMode) {
if m.pollAggregator == nil {
return
}
m.pollAggregator.HandleSessionMode(sessionID, mode)
}
// flushCachedPollMode pushes any session mode the gateway cached before this
// execution was ready. Closes the pre-execution-focus race where the frontend
// sent session.focus during execution startup and the cached mode never
// reached agentctl. No-op when nothing was cached.
func (m *Manager) flushCachedPollMode(sessionID string) {
if m.pollAggregator == nil {
return
}
m.pollAggregator.FlushSessionMode(sessionID)
}
// SetWorktreeManager sets the worktree manager for Git worktree isolation.
//
// This must be called before launching agents if Git worktree support is enabled in the runtime.
// The worktree manager creates isolated Git working directories for each agent execution,
// allowing multiple agents to work on the same repository without conflicts.
//
// Call this during initialization, typically when setting up the orchestrator service.
// If not set, agents will work directly in the repository's main working directory.
func (m *Manager) SetWorktreeManager(worktreeMgr *worktree.Manager) {
m.worktreeMgr = worktreeMgr
// Register the worktree preparer so that executor type "worktree" gets
// worktree-specific preparation (create git worktree, checkout PR branch)
// instead of the generic local preparer.
if m.preparerRegistry != nil {
m.preparerRegistry.Register(models.ExecutorTypeWorktree, NewWorktreePreparer(worktreeMgr, m.logger))
}
}
// WorktreeManager returns the configured worktree manager. Returns nil
// when worktree support has not been wired (legacy / tests). Used by
// the office task-handoffs cleaner to translate worktree IDs into
// disk operations.
func (m *Manager) WorktreeManager() *worktree.Manager {
return m.worktreeMgr
}
// SetMCPHandler sets the MCP request handler for dispatching MCP tool calls.
//
// MCP requests from agents flow through the agent stream (WebSocket) to the backend,
// where they are dispatched to this handler. This enables agents to use MCP tools
// like listing workspaces, boards, tasks, and asking user questions.
//
// This must be called before agents start making MCP calls. Typically set during
// initialization after the MCP handlers are created.
func (m *Manager) SetMCPHandler(handler agentctl.MCPHandler) {
m.mcpHandler = handler
// Update the stream manager with the handler
m.streamManager.mcpHandler = handler
}
// SetWorkspaceInfoProvider sets the provider for workspace information.
//
// The WorkspaceInfoProvider interface allows the lifecycle manager to dynamically create
// agent executions on-demand when the frontend connects to a session that doesn't have
// an active execution yet. This enables session resume after server restart or when
// accessing a session via URL (/task/[id]/[sessionId]).
//
// The provider must implement:
// - GetWorkspaceInfoBySessionID(ctx, sessionID) - Returns workspace path, worktree info,
// and MCP servers configured for the session
//
// This is typically called during initialization, with the task service as the provider.
// Without this, EnsureWorkspaceExecutionForSession will fail.
func (m *Manager) SetWorkspaceInfoProvider(provider WorkspaceInfoProvider) {
m.workspaceInfoProvider = provider
}
// SetBootMessageService sets the service used to create boot messages in chat
// during agent startup. If not set, no boot messages will be created.
func (m *Manager) SetBootMessageService(svc BootMessageService) {
m.bootMessageService = svc
}
// SetPreparerRegistry sets the registry of environment preparers.
func (m *Manager) SetPreparerRegistry(registry *PreparerRegistry) {
m.preparerRegistry = registry
}
// SetSecretStore sets the secret store for encrypting runtime auth tokens.
func (m *Manager) SetSecretStore(store secrets.SecretStore) {
m.secretStore = store
}
// SetAgentProfileReader wires the reader the launch-prep SkillDeployer uses
// to resolve full agent_profiles rows by id. Without it, the skill deploy
// step is silently skipped.
func (m *Manager) SetAgentProfileReader(reader AgentProfileReader) {
m.agentProfileReader = reader
}
// SetSkillDeployer plugs in a concrete deployer that materialises per-profile
// skills + custom prompt into the workspace before launch. Default is a
// no-op deployer; office wires its real implementation here.
func (m *Manager) SetSkillDeployer(deployer SkillDeployer) {
if deployer == nil {
m.skillDeployer = NoopSkillDeployer()
return
}
m.skillDeployer = deployer
}
// DockerClientProvider returns a function that lazily resolves the Docker client
// from the Docker executor in the registry. Returns nil if Docker is unavailable.
func (m *Manager) DockerClientProvider() func() *docker.Client {
return func() *docker.Client {
if m.executorRegistry == nil {
return nil
}
backend, err := m.executorRegistry.GetBackend(executor.NameDocker)
if err != nil {
return nil
}
dockerExec, ok := backend.(*DockerExecutor)
if !ok {
return nil
}
return dockerExec.Client()
}
}