@telora/daemon 0.14.2 → 0.14.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/build-info.json +2 -2
  2. package/dist/queries/schemas.d.ts +4 -4
  3. package/dist/queries/schemas.js +1 -1
  4. package/dist/queries/schemas.js.map +1 -1
  5. package/dist/spawner-lifecycle.d.ts +43 -0
  6. package/dist/spawner-lifecycle.d.ts.map +1 -0
  7. package/dist/spawner-lifecycle.js +152 -0
  8. package/dist/spawner-lifecycle.js.map +1 -0
  9. package/dist/spawner-stream-handlers.d.ts +67 -0
  10. package/dist/spawner-stream-handlers.d.ts.map +1 -0
  11. package/dist/spawner-stream-handlers.js +193 -0
  12. package/dist/spawner-stream-handlers.js.map +1 -0
  13. package/dist/spawner.d.ts +15 -36
  14. package/dist/spawner.d.ts.map +1 -1
  15. package/dist/spawner.js +29 -327
  16. package/dist/spawner.js.map +1 -1
  17. package/dist/strategy-completion-event.d.ts +29 -0
  18. package/dist/strategy-completion-event.d.ts.map +1 -0
  19. package/dist/strategy-completion-event.js +69 -0
  20. package/dist/strategy-completion-event.js.map +1 -0
  21. package/dist/strategy-executor.d.ts +8 -45
  22. package/dist/strategy-executor.d.ts.map +1 -1
  23. package/dist/strategy-executor.js +22 -423
  24. package/dist/strategy-executor.js.map +1 -1
  25. package/dist/strategy-spawn-helpers.d.ts +67 -0
  26. package/dist/strategy-spawn-helpers.d.ts.map +1 -0
  27. package/dist/strategy-spawn-helpers.js +160 -0
  28. package/dist/strategy-spawn-helpers.js.map +1 -0
  29. package/dist/strategy-team-lifecycle.d.ts +50 -0
  30. package/dist/strategy-team-lifecycle.d.ts.map +1 -0
  31. package/dist/strategy-team-lifecycle.js +218 -0
  32. package/dist/strategy-team-lifecycle.js.map +1 -0
  33. package/dist/unified-engine-lifecycle.d.ts +36 -0
  34. package/dist/unified-engine-lifecycle.d.ts.map +1 -0
  35. package/dist/unified-engine-lifecycle.js +250 -0
  36. package/dist/unified-engine-lifecycle.js.map +1 -0
  37. package/dist/unified-shell-config.d.ts +25 -0
  38. package/dist/unified-shell-config.d.ts.map +1 -1
  39. package/dist/unified-shell-config.js +45 -0
  40. package/dist/unified-shell-config.js.map +1 -1
  41. package/dist/unified-shell-status.d.ts.map +1 -1
  42. package/dist/unified-shell-status.js +16 -2
  43. package/dist/unified-shell-status.js.map +1 -1
  44. package/dist/unified-shell.d.ts +2 -15
  45. package/dist/unified-shell.d.ts.map +1 -1
  46. package/dist/unified-shell.js +18 -619
  47. package/dist/unified-shell.js.map +1 -1
  48. package/package.json +1 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategy-completion-event.js","sourceRoot":"","sources":["../src/strategy-completion-event.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAClF,OAAO,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACnF,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAa7D,2EAA2E;AAE3E;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,GAA2B;IACrE,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,SAAS,EAAE,kBAAkB,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC;IAE9E,MAAM,CAAC,iBAAiB,EAAE,aAAa,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QAC3D,qBAAqB,CAAC,UAAU,CAAC;QACjC,iBAAiB,CAAC,UAAU,CAAC;KAC9B,CAAC,CAAC;IAEH,6CAA6C;IAC7C,MAAM,WAAW,GAAG,i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AO,CAAC,GAAG,CACT,iDAAiD,YAAY,uBAAuB,CACrF,CAAC;IACF,MAAM,OAAO,GAAG,gBAAgB,CAAC,iBAAiB,EAAE,aAAa,EAAE,SAAS,CAAC,gBAAgB,CAAC,CAAC;IAE/F,wBAAwB;IACxB,KAAK,MAAM,CAAC,IAAI,iBAAiB,EAAE,CAAC;QAClC,SAAS,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACrC,SAAS,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,sBAAsB,CAAC,CAAC;IACjE,CAAC;IAED,kBAAkB,CAAC,KAAK,EAAE,CAAC;IAC3B,WAAW,CAAC,IAAI,CAAC,KAAM,EAAE,OAAO,CAAC,CAAC;AACpC,CAAC"}
@@ -13,9 +13,12 @@
13
13
  * 5. Terminates when all work is done; fresh team spawned for new work
14
14
  *
15
15
  * This module is the orchestration entry point. Pure logic is extracted to:
16
- * - strategy-team-state.ts -- active teams map, config derivation
17
- * - strategy-merge.ts -- branch merge logic
18
- * - strategy-completion.ts -- post-execution completion handling
16
+ * - strategy-team-state.ts -- active teams map, config derivation
17
+ * - strategy-merge.ts -- branch merge logic
18
+ * - strategy-completion.ts -- post-execution completion handling
19
+ * - strategy-spawn-helpers.ts -- log streams, stream handlers, process close
20
+ * - strategy-team-lifecycle.ts -- terminate, wait, detect deactivated, merge checks
21
+ * - strategy-completion-event.ts -- completion event handler
19
22
  */
20
23
  import type { DaemonConfig, AgentRole, PipelineConfig } from './types.js';
21
24
  import { type ResourceGovernor } from '@telora/daemon-core';
@@ -24,6 +27,8 @@ export { getActiveTeams, hasActiveTeam, getActiveTeamCount, } from './strategy-t
24
27
  export { mergeStrategyBranch, } from './strategy-merge.js';
25
28
  export { handleTeamCompletion, advanceDeliveryStatuses, isStatusTerminal, isStatusAgentActionable, } from './strategy-completion.js';
26
29
  export type { TeamCompletionParams } from './strategy-completion.js';
30
+ export { terminateTeam, waitForTeamExit, terminateAllTeams, detectDeactivatedStrategies, checkAndMergeCompletedDeliveries, } from './strategy-team-lifecycle.js';
31
+ export type { CompletionEventContext } from './strategy-completion-event.js';
27
32
  /** Inject the resource governor for global concurrency limiting. */
28
33
  export declare function initGovernor(gov: ResourceGovernor): void;
29
34
  /**
@@ -47,46 +52,4 @@ export interface SpawnStrategyTeamParams {
47
52
  * and issues, builds a task DAG, and coordinates worker execution.
48
53
  */
49
54
  export declare function spawnStrategyTeam(params: SpawnStrategyTeamParams): Promise<void>;
50
- /**
51
- * Terminate a strategy team.
52
- *
53
- * Sends a deactivation message via stdin (if open) before SIGTERM
54
- * to allow the team lead to process the shutdown gracefully.
55
- */
56
- export declare function terminateTeam(strategyId: string): boolean;
57
- /**
58
- * Wait for a team's process to exit (leave activeTeams).
59
- *
60
- * Polls activeTeams until the strategy is no longer present,
61
- * which happens when handleTeamCompletion runs on the 'close' event.
62
- * If the timeout expires, sends SIGKILL and waits briefly.
63
- *
64
- * @param strategyId - Strategy to wait for
65
- * @param timeoutMs - Max wait time in ms (default 30s)
66
- * @returns true if team exited within timeout, false if forced
67
- */
68
- export declare function waitForTeamExit(strategyId: string, timeoutMs?: number): Promise<boolean>;
69
- /**
70
- * Terminate all active teams.
71
- */
72
- export declare function terminateAllTeams(): void;
73
- /**
74
- * Detect strategies that have been deactivated (agent role removed)
75
- * and shut down their active teams.
76
- */
77
- export declare function detectDeactivatedStrategies(config: DaemonConfig): Promise<void>;
78
- /**
79
- * Check active teams for newly completed deliveries and merge
80
- * the strategy branch to integration incrementally.
81
- *
82
- * Called from the poll loop. For each active team, queries delivery
83
- * statuses and triggers a merge when any delivery reaches verify/done
84
- * that hasn't already been merged mid-flight.
85
- *
86
- * After processing merges, checks if ALL active deliveries are terminal
87
- * and merged. If so, terminates the team proactively since no more work
88
- * remains. The strategy stays active in the DB -- if new deliveries
89
- * arrive later, the next poll cycle spawns a fresh team.
90
- */
91
- export declare function checkAndMergeCompletedDeliveries(config: DaemonConfig): Promise<void>;
92
55
  //# sourceMappingURL=strategy-executor.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"strategy-executor.d.ts","sourceRoot":"","sources":["../src/strategy-executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAKH,OAAO,KAAK,EACV,YAAY,EACZ,SAAS,EAET,cAAc,EACf,MAAM,YAAY,CAAC;AAUpB,OAAO,EAA0D,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAcpH,OAAO,yBAAyB,CAAC;AAKjC,OAAO,EACL,cAAc,EACd,aAAa,EACb,kBAAkB,GACnB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACL,mBAAmB,GACpB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,oBAAoB,EACpB,uBAAuB,EACvB,gBAAgB,EAChB,uBAAuB,GACxB,MAAM,0BAA0B,CAAC;AAElC,YAAY,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAkBrE,oEAAoE;AACpE,wBAAgB,YAAY,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAExD;AA8UD;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAK5G;AA+BD,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,YAAY,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,SAAS,CAAC;IAChB,cAAc,EAAE,cAAc,GAAG,IAAI,CAAC;IACtC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,4DAA4D;IAC5D,mBAAmB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACrC;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,uBAAuB,GAC9B,OAAO,CAAC,IAAI,CAAC,CAmZ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BAAgB,GAAG,IAAI,CAExD;AAwCD;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAK5G;AA+BD,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,YAAY,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,SAAS,CAAC;IAChB,cAAc,EAAE,cAAc,GAAG,IAAI,CAAC;IACtC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,4DAA4D;IAC5D,mBAAmB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACrC;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,uBAAuB,GAC9B,OAAO,CAAC,IAAI,CAAC,CAmZf"}
@@ -13,104 +13,48 @@
13
13
  * 5. Terminates when all work is done; fresh team spawned for new work
14
14
  *
15
15
  * This module is the orchestration entry point. Pure logic is extracted to:
16
- * - strategy-team-state.ts -- active teams map, config derivation
17
- * - strategy-merge.ts -- branch merge logic
18
- * - strategy-completion.ts -- post-execution completion handling
16
+ * - strategy-team-state.ts -- active teams map, config derivation
17
+ * - strategy-merge.ts -- branch merge logic
18
+ * - strategy-completion.ts -- post-execution completion handling
19
+ * - strategy-spawn-helpers.ts -- log streams, stream handlers, process close
20
+ * - strategy-team-lifecycle.ts -- terminate, wait, detect deactivated, merge checks
21
+ * - strategy-completion-event.ts -- completion event handler
19
22
  */
20
23
  import { spawn } from 'node:child_process';
21
- import { mkdirSync, existsSync, createWriteStream } from 'node:fs';
22
- import { join } from 'node:path';
23
- import { createSession, updateSession, getReadyStrategies, getActiveStrategies, } from './supabase.js';
24
+ import { mkdirSync, existsSync } from 'node:fs';
25
+ import { createSession, updateSession, getActiveStrategies, } from './supabase.js';
24
26
  import { createWorktree, runGitSync, repoHasCommits } from './git.js';
25
27
  import { installAuditPreCommitHook } from './audit-hooks.js';
26
28
  import { getStrategyWorktree, setStrategyWorktree } from './strategy-worktree-state.js';
27
- import { withRetry, StreamJsonParser, sendMessage, productLabel } from '@telora/daemon-core';
28
- import { recordActivity, setNarration, clearNarration } from './heartbeat.js';
29
- import { CompletionDetector } from './completion-detector.js';
30
- import { formatEventForLog } from '@telora/daemon-core';
31
- import { ActivityTracker } from './activity-tracker.js';
32
- import { buildStrategyTeamPrompt, buildWakeMessage } from './strategy-prompt-builder.js';
29
+ import { withRetry, sendMessage, productLabel } from '@telora/daemon-core';
30
+ import { recordActivity } from './heartbeat.js';
31
+ import { buildStrategyTeamPrompt } from './strategy-prompt-builder.js';
33
32
  import { buildRoleFrameworkPrompt } from './team-prompt-base.js';
34
- import { getStrategyDeliveries, getStrategyIssues, getProductContextForStrategy, getProductDeploymentProfileSnapshot, updateStrategyClaudeSessionId, fetchStrategyWorkflow } from './queries/strategies.js';
33
+ import { getStrategyDeliveries, getStrategyIssues, getProductContextForStrategy, getProductDeploymentProfileSnapshot, fetchStrategyWorkflow } from './queries/strategies.js';
35
34
  import { buildSpawnEnvironment } from './spawn-environment.js';
36
35
  import { sanitizeGitSegment } from './git-utils.js';
37
36
  import { recordStrategyTeardown } from './spawn-cooldown.js';
38
37
  import { consumePendingSpawnDirective, assembleDirectiveContent } from './directive-executor.js';
39
- import { OPEN_ISSUE_STATUSES } from './constants.js';
40
38
  import { resolveAssemblyRecipe } from './assembly-engine.js';
41
39
  import './assembly-resolvers.js';
42
- import { configForProduct, findProduct } from './config.js';
43
- // ── Re-exports from extracted modules (backward compatibility) ───────
40
+ import { isStatusAgentActionable, isStatusBlocking } from './stage-classifier.js';
41
+ // ── Imports from extracted modules ──────────────────────────────────────
42
+ import { getActiveTeams, deriveExecutionConfig, } from './strategy-team-state.js';
43
+ import { setupTeamLogStreams, attachStreamHandlers, handleTeamProcessClose, } from './strategy-spawn-helpers.js';
44
+ import { terminateTeam, } from './strategy-team-lifecycle.js';
45
+ import { handleCompletionEvent, } from './strategy-completion-event.js';
46
+ // ── Re-exports for backward compatibility ───────────────────────────────
44
47
  export { getActiveTeams, hasActiveTeam, getActiveTeamCount, } from './strategy-team-state.js';
45
48
  export { mergeStrategyBranch, } from './strategy-merge.js';
46
49
  export { handleTeamCompletion, advanceDeliveryStatuses, isStatusTerminal, isStatusAgentActionable, } from './strategy-completion.js';
47
- // ── Imports from extracted modules (used internally) ─────────────────
48
- import { getActiveTeams, deriveExecutionConfig, } from './strategy-team-state.js';
49
- import { mergeStrategyBranch, escalateMergeConflict } from './strategy-merge.js';
50
- import { handleTeamCompletion, } from './strategy-completion.js';
51
- import { isStatusTerminal, isStatusAgentActionable, isStatusBlocking } from './stage-classifier.js';
50
+ export { terminateTeam, waitForTeamExit, terminateAllTeams, detectDeactivatedStrategies, checkAndMergeCompletedDeliveries, } from './strategy-team-lifecycle.js';
52
51
  // ── Resource governor (optional, injected by StrategyEngine) ─────────
53
52
  let governor = null;
54
53
  /** Inject the resource governor for global concurrency limiting. */
55
54
  export function initGovernor(gov) {
56
55
  governor = gov;
57
56
  }
58
- /**
59
- * Handle a 'complete' event from the CompletionDetector.
60
- *
61
- * Queries current delivery and issue state to decide whether to:
62
- * - Terminate the team (all deliveries terminal or no open issues remain), or
63
- * - Send a mid-strategy handoff message with remaining work.
64
- *
65
- * Extracted from the inline event callback in spawnStrategyTeam to eliminate
66
- * deep nesting inside an async Promise chain inside an event listener.
67
- */
68
- async function handleCompletionEvent(ctx) {
69
- const { strategyId, strategyName, teamState, completionDetector, proc } = ctx;
70
- const [currentDeliveries, currentIssues] = await Promise.all([
71
- getStrategyDeliveries(strategyId),
72
- getStrategyIssues(strategyId),
73
- ]);
74
- // If all deliveries are terminal, shut down.
75
- const allTerminal = currentDeliveries.every(d => isStatusTerminal(d.executionStatus ?? ''));
76
- if (allTerminal) {
77
- console.log(`[strategy-executor] All deliveries terminal for "${strategyName}" -- terminating team`);
78
- teamState.shutdownReason = 'work_complete';
79
- terminateTeam(strategyId);
80
- return;
81
- }
82
- // Check if there are actionable deliveries with open issues.
83
- const actionableDeliveryIds = new Set(currentDeliveries
84
- .filter(d => isStatusAgentActionable(d.executionStatus ?? ''))
85
- .map(d => d.id));
86
- if (actionableDeliveryIds.size === 0) {
87
- console.log(`[strategy-executor] No actionable deliveries for "${strategyName}" -- terminating team`);
88
- teamState.shutdownReason = 'work_complete';
89
- terminateTeam(strategyId);
90
- return;
91
- }
92
- // Check for open issues across actionable deliveries.
93
- const hasOpenIssues = currentIssues.some(i => actionableDeliveryIds.has(i.deliveryId) && OPEN_ISSUE_STATUSES.has(i.status));
94
- if (!hasOpenIssues) {
95
- console.log(`[strategy-executor] Actionable deliveries have no open issues for "${strategyName}" -- ` +
96
- `terminating team. Auto-advance will handle verify transition.`);
97
- teamState.shutdownReason = 'work_complete';
98
- terminateTeam(strategyId);
99
- return;
100
- }
101
- // Mid-strategy handoff: actionable work with open issues remains.
102
- // Send work immediately and reset the completion detector for the next cycle.
103
- console.log(`[strategy-executor] Mid-strategy handoff for "${strategyName}" -- sending new work`);
104
- const message = buildWakeMessage(currentDeliveries, currentIssues, teamState.knownDeliveryIds);
105
- // Update tracking state
106
- for (const d of currentDeliveries) {
107
- teamState.knownDeliveryIds.add(d.id);
108
- teamState.deliveryStageIds.set(d.id, d.currentWorkflowStageId);
109
- }
110
- completionDetector.reset();
111
- sendMessage(proc.stdin, message);
112
- }
113
- // ── Private helpers for spawnStrategyTeam ─────────────────────────────
57
+ // ── Private helpers ─────────────────────────────────────────────────────
114
58
  /**
115
59
  * Pre-spawn guard: enforce rank-ordered execution.
116
60
  *
@@ -140,148 +84,6 @@ function findActionableDeliveries(deliveries, strategyName) {
140
84
  }
141
85
  return deliveries.filter(d => isStatusAgentActionable(d.executionStatus ?? ''));
142
86
  }
143
- /**
144
- * Create log file paths and writable streams for a team's output.
145
- */
146
- function setupTeamLogStreams(logDir, branchName, strategyName) {
147
- const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
148
- const logPrefix = `strategy-${branchName.replace(/\//g, '-')}-${timestamp}`;
149
- const stdoutPath = join(logDir, `${logPrefix}.stdout.log`);
150
- const stderrPath = join(logDir, `${logPrefix}.stderr.log`);
151
- const jsonlPath = join(logDir, `${logPrefix}.stream.jsonl`);
152
- const stdoutLogStream = createWriteStream(stdoutPath, { mode: 0o600 });
153
- const stderrStream = createWriteStream(stderrPath, { mode: 0o600 });
154
- const jsonlStream = createWriteStream(jsonlPath, { mode: 0o600 });
155
- // Attach error handlers to prevent unhandled 'error' events from crashing the daemon
156
- stdoutLogStream.on('error', (err) => {
157
- console.warn(`[strategy-executor] stdout log stream error for "${strategyName}": ${err.message}`);
158
- });
159
- stderrStream.on('error', (err) => {
160
- console.warn(`[strategy-executor] stderr log stream error for "${strategyName}": ${err.message}`);
161
- });
162
- jsonlStream.on('error', (err) => {
163
- console.warn(`[strategy-executor] jsonl log stream error for "${strategyName}": ${err.message}`);
164
- });
165
- return { stdoutPath, stderrPath, jsonlPath, stdoutLogStream, stderrStream, jsonlStream };
166
- }
167
- /**
168
- * Attach stream parser, completion detector, activity tracker, and event
169
- * handlers to the spawned team lead process.
170
- */
171
- function attachStreamHandlers(params) {
172
- const { proc, teamState, strategyId, strategyName, sessionId, logs } = params;
173
- const streamParser = new StreamJsonParser();
174
- const completionDetector = new CompletionDetector();
175
- streamParser.attach(proc.stdout);
176
- completionDetector.attach(streamParser);
177
- teamState.completionDetector = completionDetector;
178
- // Attach activity tracker for live activity snapshots
179
- const activityTracker = new ActivityTracker(sessionId);
180
- activityTracker.onNarration((text) => setNarration(strategyId, text));
181
- activityTracker.attach(streamParser);
182
- // Write raw NDJSON lines to jsonl log file
183
- streamParser.on('event', (event) => {
184
- logs.jsonlStream.write(JSON.stringify(event) + '\n');
185
- });
186
- // Write human-readable lines to stdout log
187
- streamParser.on('event', (event) => {
188
- const line = formatEventForLog(event);
189
- if (line) {
190
- logs.stdoutLogStream.write(line + '\n');
191
- }
192
- });
193
- // Track session state from stream events
194
- streamParser.on('init', (event) => {
195
- console.log(` [strategy-team] Session initialized (model: ${event.model}, tools: ${event.tools.length})`);
196
- teamState.claudeSessionId = event.session_id;
197
- // Persist session ID for --resume support on re-spawn
198
- updateStrategyClaudeSessionId(strategyId, event.session_id).catch((err) => {
199
- console.warn(`[strategy-executor] Failed to persist Claude session ID for "${strategyName}":`, err.message);
200
- });
201
- });
202
- streamParser.on('teammate', (event) => {
203
- if (event.subtype === 'teammate_spawned') {
204
- console.log(` [strategy-team] Worker spawned: ${event.agent_name}`);
205
- }
206
- else if (event.subtype === 'teammate_completed') {
207
- const status = event.is_error ? 'FAILED' : 'completed';
208
- console.log(` [strategy-team] Worker ${status}: ${event.agent_name}`);
209
- }
210
- });
211
- streamParser.on('result', (result) => {
212
- const cost = result.total_cost_usd?.toFixed(4) ?? '?';
213
- console.log(` [strategy-team] Result: ${result.is_error ? 'ERROR' : 'Success'}: ${result.num_turns} turns, $${cost}`);
214
- });
215
- // Stderr pipes to log
216
- proc.stderr?.pipe(logs.stderrStream);
217
- return { completionDetector, activityTracker };
218
- }
219
- /**
220
- * Handle the team lead process 'close' event.
221
- *
222
- * Detects --resume failures and retries without resume, otherwise
223
- * runs normal cleanup (governor release, stream close, completion handling).
224
- */
225
- async function handleTeamProcessClose(ctx) {
226
- const { code, signal, strategyName, strategyId, resumeId, spawnedAt, teamState, sessionId, config: cfg, completionDetector, activityTracker, logs, params: spawnParams, } = ctx;
227
- const activeTeams = getActiveTeams();
228
- console.log(`[strategy-executor] Team lead for "${strategyName}" exited (code: ${code}, signal: ${signal})`);
229
- // Detect --resume failure: if the process exits quickly with a non-zero
230
- // code and --resume was used, retry without resume.
231
- const RESUME_FAILURE_THRESHOLD_MS = 15_000;
232
- const elapsedMs = Date.now() - spawnedAt;
233
- if (resumeId && code !== 0 && code !== null && elapsedMs < RESUME_FAILURE_THRESHOLD_MS) {
234
- console.warn(`[strategy-executor] Resume failed for "${strategyName}" (exited in ${elapsedMs}ms), retrying without resume`);
235
- // Release governor slot and clean up minimal state
236
- governor?.releaseSlot('strategy');
237
- completionDetector.destroy();
238
- logs.stdoutLogStream.end();
239
- logs.stderrStream.end();
240
- logs.jsonlStream.end();
241
- // Clean up session and team state
242
- try {
243
- await updateSession(sessionId, {
244
- status: 'failed',
245
- exit_reason: `Resume failed (code ${code}), retrying without resume`,
246
- exit_category: 'internal',
247
- ended_at: new Date().toISOString(),
248
- });
249
- }
250
- catch (updateErr) {
251
- console.warn('[strategy-executor] Failed to update session after resume failure:', updateErr.message);
252
- }
253
- teamState.phase = 'terminated';
254
- activeTeams.delete(strategyId);
255
- // Re-spawn without --resume
256
- await spawnStrategyTeam({ ...spawnParams, lastClaudeSessionId: null });
257
- return;
258
- }
259
- // Release governor slot (if governor is configured)
260
- governor?.releaseSlot('strategy');
261
- // Null out stdin so post-exit merge logic falls through to the
262
- // fallback resolution agent instead of sending messages to a dead process.
263
- teamState.leadStdin = null;
264
- // Final activity flush before cleanup
265
- try {
266
- await activityTracker.finalFlush();
267
- }
268
- catch (err) {
269
- console.warn('[strategy-executor] activityTracker.finalFlush failed:', err.message);
270
- }
271
- // Clean up streams
272
- completionDetector.destroy();
273
- logs.stdoutLogStream.end();
274
- logs.stderrStream.end();
275
- logs.jsonlStream.end();
276
- recordActivity();
277
- await handleTeamCompletion({
278
- config: cfg,
279
- teamState,
280
- sessionId,
281
- code,
282
- signal,
283
- });
284
- }
285
87
  // ── Team spawning ────────────────────────────────────────────────────
286
88
  /**
287
89
  * Generate branch name for strategy-level work.
@@ -653,7 +455,7 @@ export async function spawnStrategyTeam(params) {
653
455
  handleTeamProcessClose({
654
456
  code, signal, strategyName, strategyId, resumeId, spawnedAt,
655
457
  teamState, sessionId: session.id, config, completionDetector,
656
- activityTracker, logs, params,
458
+ activityTracker, logs, params, governor, spawnStrategyTeam,
657
459
  }).catch(err => {
658
460
  console.error(`[strategy-executor] handleTeamProcessClose failed for "${strategyName}":`, err.message);
659
461
  });
@@ -682,207 +484,4 @@ export async function spawnStrategyTeam(params) {
682
484
  activeTeams.delete(strategyId);
683
485
  });
684
486
  }
685
- // ── Team lifecycle management ────────────────────────────────────────
686
- /**
687
- * Terminate a strategy team.
688
- *
689
- * Sends a deactivation message via stdin (if open) before SIGTERM
690
- * to allow the team lead to process the shutdown gracefully.
691
- */
692
- export function terminateTeam(strategyId) {
693
- const activeTeams = getActiveTeams();
694
- const team = activeTeams.get(strategyId);
695
- if (!team || !team.leadPid)
696
- return false;
697
- // Warn if terminating while conflict resolution is in progress
698
- if (team.resolvingMergeConflict) {
699
- console.warn(`[strategy-executor] Terminating team "${team.strategyName}" while merge conflict resolution is in progress`);
700
- }
701
- console.log(`[strategy-executor] Terminating team for strategy "${team.strategyName}" (phase: ${team.phase})`);
702
- team.phase = 'shutting_down';
703
- clearNarration(strategyId);
704
- try {
705
- // Send a deactivation message first to let the team lead process it
706
- if (team.leadStdin) {
707
- sendMessage(team.leadStdin, 'Pipeline deactivated. Exit now.');
708
- // Close stdin after a short delay to let the message be processed
709
- setTimeout(() => {
710
- try {
711
- team.leadStdin?.end();
712
- }
713
- catch (e) {
714
- console.debug('[strategy-executor] stdin.end() failed (may already be closed):', e.message);
715
- }
716
- }, 5000);
717
- }
718
- // SIGTERM to the lead process -- it should clean up workers
719
- process.kill(team.leadPid, 'SIGTERM');
720
- // Escalate to SIGKILL after timeout
721
- setTimeout(() => {
722
- if (activeTeams.has(strategyId) && team.leadPid) {
723
- try {
724
- process.kill(team.leadPid, 'SIGKILL');
725
- }
726
- catch (e) {
727
- console.debug('[strategy-executor] SIGKILL failed (process may have exited):', e.message);
728
- }
729
- }
730
- }, 30000);
731
- return true;
732
- }
733
- catch (e) {
734
- console.debug(`[strategy-executor] terminateTeam: process may have already exited:`, e.message);
735
- return false;
736
- }
737
- }
738
- /**
739
- * Wait for a team's process to exit (leave activeTeams).
740
- *
741
- * Polls activeTeams until the strategy is no longer present,
742
- * which happens when handleTeamCompletion runs on the 'close' event.
743
- * If the timeout expires, sends SIGKILL and waits briefly.
744
- *
745
- * @param strategyId - Strategy to wait for
746
- * @param timeoutMs - Max wait time in ms (default 30s)
747
- * @returns true if team exited within timeout, false if forced
748
- */
749
- export async function waitForTeamExit(strategyId, timeoutMs = 30000) {
750
- const activeTeams = getActiveTeams();
751
- const team = activeTeams.get(strategyId);
752
- if (!team)
753
- return true; // Already gone
754
- const deadline = Date.now() + timeoutMs;
755
- const pollMs = 500;
756
- while (Date.now() < deadline) {
757
- if (!activeTeams.has(strategyId))
758
- return true;
759
- await new Promise(resolve => setTimeout(resolve, pollMs));
760
- }
761
- // Timeout expired — escalate to SIGKILL
762
- if (team.leadPid) {
763
- console.warn(`[strategy-executor] Team "${team.strategyName}" did not exit within ${timeoutMs}ms, sending SIGKILL`);
764
- try {
765
- process.kill(team.leadPid, 'SIGKILL');
766
- }
767
- catch { /* process may already be gone */ }
768
- // Brief wait for SIGKILL to take effect
769
- await new Promise(resolve => setTimeout(resolve, 2000));
770
- }
771
- return !activeTeams.has(strategyId);
772
- }
773
- /**
774
- * Terminate all active teams.
775
- */
776
- export function terminateAllTeams() {
777
- const activeTeams = getActiveTeams();
778
- for (const strategyId of activeTeams.keys()) {
779
- terminateTeam(strategyId);
780
- }
781
- }
782
- /**
783
- * Detect strategies that have been deactivated (agent role removed)
784
- * and shut down their active teams.
785
- */
786
- export async function detectDeactivatedStrategies(config) {
787
- const activeTeams = getActiveTeams();
788
- if (activeTeams.size === 0)
789
- return;
790
- try {
791
- // Aggregate ready strategies across all configured products
792
- const allReadyStrategies = [];
793
- for (const product of config.products) {
794
- const strategies = await getReadyStrategies(config.organizationId, product.id);
795
- allReadyStrategies.push(...strategies);
796
- }
797
- const activeStrategyIds = new Set(allReadyStrategies.map(s => s.strategy_id));
798
- for (const [strategyId, team] of activeTeams) {
799
- if (!activeStrategyIds.has(strategyId) && team.phase !== 'shutting_down' && team.phase !== 'terminated') {
800
- console.log(`[strategy-executor] Strategy "${team.strategyName}" deactivated -- shutting down team`);
801
- terminateTeam(strategyId);
802
- }
803
- }
804
- }
805
- catch (err) {
806
- console.warn(`[strategy-executor] Failed to check for deactivated strategies:`, err.message);
807
- }
808
- }
809
- /**
810
- * Check active teams for newly completed deliveries and merge
811
- * the strategy branch to integration incrementally.
812
- *
813
- * Called from the poll loop. For each active team, queries delivery
814
- * statuses and triggers a merge when any delivery reaches verify/done
815
- * that hasn't already been merged mid-flight.
816
- *
817
- * After processing merges, checks if ALL active deliveries are terminal
818
- * and merged. If so, terminates the team proactively since no more work
819
- * remains. The strategy stays active in the DB -- if new deliveries
820
- * arrive later, the next poll cycle spawns a fresh team.
821
- */
822
- export async function checkAndMergeCompletedDeliveries(config) {
823
- const activeTeams = getActiveTeams();
824
- if (activeTeams.size === 0)
825
- return;
826
- for (const [strategyId, team] of activeTeams) {
827
- // Only check teams that are actively executing
828
- if (team.phase !== 'executing')
829
- continue;
830
- // Skip merge attempts while team lead is resolving a merge conflict
831
- if (team.resolvingMergeConflict) {
832
- console.log(`[strategy-executor] Skipping merge check for "${team.strategyName}" -- conflict resolution in progress`);
833
- continue;
834
- }
835
- // Use product-scoped config for this team's merge operations
836
- const teamProduct = findProduct(config, team.productId);
837
- const teamConfig = teamProduct ? configForProduct(config, teamProduct) : config;
838
- try {
839
- const deliveries = await getStrategyDeliveries(strategyId);
840
- // Find deliveries in verify/done that we haven't merged for yet
841
- const newlyCompleted = deliveries.filter(d => !isStatusAgentActionable(d.executionStatus ?? '') && !isStatusBlocking(d.executionStatus ?? '')
842
- && !team.mergedDeliveryIds.has(d.id));
843
- if (newlyCompleted.length > 0) {
844
- const completedNames = newlyCompleted.map(d => d.name).join(', ');
845
- console.log(`[strategy-executor] ${newlyCompleted.length} delivery(ies) completed mid-flight for "${team.strategyName}": ${completedNames} -- merging to integration`);
846
- // One merge covers all completed deliveries (same branch)
847
- const mergeResult = await mergeStrategyBranch(teamConfig, team, team.leadSessionId ?? '', team.branchName);
848
- if (mergeResult.mergeSucceeded) {
849
- // Track merged deliveries (git state already reported by mergeStrategyBranch)
850
- for (const d of newlyCompleted) {
851
- team.mergedDeliveryIds.add(d.id);
852
- }
853
- }
854
- else {
855
- console.warn(`[strategy-executor] Mid-flight merge failed for "${team.strategyName}": ${mergeResult.exitReason}`);
856
- // Escalate merge conflict for each unmerged delivery
857
- for (const d of newlyCompleted) {
858
- escalateMergeConflict({
859
- organizationId: team.organizationId,
860
- sessionId: team.leadSessionId ?? '',
861
- deliveryId: d.id,
862
- deliveryName: d.name,
863
- branchName: team.branchName,
864
- integrationBranch: config.integrationBranch,
865
- mergeError: mergeResult.exitReason,
866
- }).catch(err => console.warn(`[strategy-executor] escalateMergeConflict failed for ${d.id}:`, err.message));
867
- }
868
- // Don't mark as merged -- will retry next poll. Team continues working.
869
- }
870
- }
871
- // ── All-done detection ──────────────────────────────────────
872
- // If any delivery is queued or running, the team still has work.
873
- // Otherwise the team is idle, waiting for the daemon to push new work.
874
- const teamWork = deliveries.filter(d => isStatusAgentActionable(d.executionStatus ?? ''));
875
- if (teamWork.length > 0) {
876
- console.log(`[strategy-executor] ${teamWork.length} delivery(ies) still queued/running for "${team.strategyName}" -- team continues`);
877
- continue;
878
- }
879
- // No queued/running deliveries -- team is idle, completion detector
880
- // handles the actual idle transition. Log for observability.
881
- console.log(`[strategy-executor] All deliveries complete for "${team.strategyName}" -- team is idle`);
882
- }
883
- catch (err) {
884
- console.warn(`[strategy-executor] Failed to check completed deliveries for "${team.strategyName}":`, err.message);
885
- }
886
- }
887
- }
888
487
  //# sourceMappingURL=strategy-executor.js.map