@claude-flow/cli 3.0.0-alpha.29 → 3.0.0-alpha.31

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.
@@ -0,0 +1,304 @@
1
+ /**
2
+ * Headless Worker Executor
3
+ * Enables workers to invoke Claude Code in headless mode with configurable sandbox profiles.
4
+ *
5
+ * ADR-020: Headless Worker Integration Architecture
6
+ * - Integrates with CLAUDE_CODE_HEADLESS and CLAUDE_CODE_SANDBOX_MODE environment variables
7
+ * - Provides process pool for concurrent execution
8
+ * - Builds context from file glob patterns
9
+ * - Supports prompt templates and output parsing
10
+ * - Implements timeout and graceful error handling
11
+ *
12
+ * Key Features:
13
+ * - Process pool with configurable maxConcurrent
14
+ * - Context building from file glob patterns with caching
15
+ * - Prompt template system with context injection
16
+ * - Output parsing (text, json, markdown)
17
+ * - Timeout handling with graceful termination
18
+ * - Execution logging for debugging
19
+ * - Event emission for monitoring
20
+ */
21
+ import { EventEmitter } from 'events';
22
+ import type { WorkerType } from './worker-daemon.js';
23
+ /**
24
+ * Headless worker types - workers that use Claude Code AI
25
+ */
26
+ export type HeadlessWorkerType = 'audit' | 'optimize' | 'testgaps' | 'document' | 'ultralearn' | 'refactor' | 'deepdive' | 'predict';
27
+ /**
28
+ * Local worker types - workers that run locally without AI
29
+ */
30
+ export type LocalWorkerType = 'map' | 'consolidate' | 'benchmark' | 'preload';
31
+ /**
32
+ * Sandbox mode for headless execution
33
+ */
34
+ export type SandboxMode = 'strict' | 'permissive' | 'disabled';
35
+ /**
36
+ * Model types for Claude Code
37
+ */
38
+ export type ModelType = 'sonnet' | 'opus' | 'haiku';
39
+ /**
40
+ * Output format for worker results
41
+ */
42
+ export type OutputFormat = 'text' | 'json' | 'markdown';
43
+ /**
44
+ * Execution mode for workers
45
+ */
46
+ export type ExecutionMode = 'local' | 'headless';
47
+ /**
48
+ * Worker priority levels
49
+ */
50
+ export type WorkerPriority = 'low' | 'normal' | 'high' | 'critical';
51
+ /**
52
+ * Base worker configuration (matching worker-daemon.ts)
53
+ */
54
+ export interface WorkerConfig {
55
+ type: WorkerType;
56
+ intervalMs: number;
57
+ priority: WorkerPriority;
58
+ description: string;
59
+ enabled: boolean;
60
+ }
61
+ /**
62
+ * Headless-specific options
63
+ */
64
+ export interface HeadlessOptions {
65
+ /** Prompt template for Claude Code */
66
+ promptTemplate: string;
67
+ /** Sandbox profile: strict, permissive, or disabled */
68
+ sandbox: SandboxMode;
69
+ /** Model to use: sonnet, opus, or haiku */
70
+ model?: ModelType;
71
+ /** Maximum tokens for output */
72
+ maxOutputTokens?: number;
73
+ /** Timeout in milliseconds (overrides default) */
74
+ timeoutMs?: number;
75
+ /** File glob patterns to include as context */
76
+ contextPatterns?: string[];
77
+ /** Output parsing format */
78
+ outputFormat?: OutputFormat;
79
+ }
80
+ /**
81
+ * Extended worker configuration with headless options
82
+ */
83
+ export interface HeadlessWorkerConfig extends WorkerConfig {
84
+ /** Execution mode: local or headless */
85
+ mode: ExecutionMode;
86
+ /** Headless-specific options (required when mode is 'headless') */
87
+ headless?: HeadlessOptions;
88
+ }
89
+ /**
90
+ * Executor configuration options
91
+ */
92
+ export interface HeadlessExecutorConfig {
93
+ /** Maximum concurrent headless processes */
94
+ maxConcurrent?: number;
95
+ /** Default timeout in milliseconds */
96
+ defaultTimeoutMs?: number;
97
+ /** Maximum files to include in context */
98
+ maxContextFiles?: number;
99
+ /** Maximum characters per file in context */
100
+ maxCharsPerFile?: number;
101
+ /** Log directory for execution logs */
102
+ logDir?: string;
103
+ /** Whether to cache context between runs */
104
+ cacheContext?: boolean;
105
+ /** Context cache TTL in milliseconds */
106
+ cacheTtlMs?: number;
107
+ }
108
+ /**
109
+ * Result from headless execution
110
+ */
111
+ export interface HeadlessExecutionResult {
112
+ /** Whether execution completed successfully */
113
+ success: boolean;
114
+ /** Raw output from Claude Code */
115
+ output: string;
116
+ /** Parsed output (if outputFormat is json or markdown) */
117
+ parsedOutput?: unknown;
118
+ /** Execution duration in milliseconds */
119
+ durationMs: number;
120
+ /** Estimated tokens used (if available) */
121
+ tokensUsed?: number;
122
+ /** Model used for execution */
123
+ model: string;
124
+ /** Sandbox mode used */
125
+ sandboxMode: SandboxMode;
126
+ /** Worker type that was executed */
127
+ workerType: HeadlessWorkerType;
128
+ /** Timestamp of execution */
129
+ timestamp: Date;
130
+ /** Error message if execution failed */
131
+ error?: string;
132
+ /** Execution ID for tracking */
133
+ executionId: string;
134
+ }
135
+ /**
136
+ * Pool status information
137
+ */
138
+ export interface PoolStatus {
139
+ activeCount: number;
140
+ queueLength: number;
141
+ maxConcurrent: number;
142
+ activeWorkers: Array<{
143
+ executionId: string;
144
+ workerType: HeadlessWorkerType;
145
+ startTime: Date;
146
+ elapsedMs: number;
147
+ }>;
148
+ queuedWorkers: Array<{
149
+ workerType: HeadlessWorkerType;
150
+ queuedAt: Date;
151
+ waitingMs: number;
152
+ }>;
153
+ }
154
+ /**
155
+ * Array of headless worker types for runtime checking
156
+ */
157
+ export declare const HEADLESS_WORKER_TYPES: HeadlessWorkerType[];
158
+ /**
159
+ * Array of local worker types
160
+ */
161
+ export declare const LOCAL_WORKER_TYPES: LocalWorkerType[];
162
+ /**
163
+ * Default headless worker configurations based on ADR-020
164
+ */
165
+ export declare const HEADLESS_WORKER_CONFIGS: Record<HeadlessWorkerType, HeadlessWorkerConfig>;
166
+ /**
167
+ * Local worker configurations
168
+ */
169
+ export declare const LOCAL_WORKER_CONFIGS: Record<LocalWorkerType, HeadlessWorkerConfig>;
170
+ /**
171
+ * Combined worker configurations
172
+ */
173
+ export declare const ALL_WORKER_CONFIGS: HeadlessWorkerConfig[];
174
+ /**
175
+ * Check if a worker type is a headless worker
176
+ */
177
+ export declare function isHeadlessWorker(type: WorkerType): type is HeadlessWorkerType;
178
+ /**
179
+ * Check if a worker type is a local worker
180
+ */
181
+ export declare function isLocalWorker(type: WorkerType): type is LocalWorkerType;
182
+ /**
183
+ * Get model ID from model type
184
+ */
185
+ export declare function getModelId(model: ModelType): string;
186
+ /**
187
+ * Get worker configuration by type
188
+ */
189
+ export declare function getWorkerConfig(type: WorkerType): HeadlessWorkerConfig | undefined;
190
+ /**
191
+ * HeadlessWorkerExecutor - Executes workers using Claude Code in headless mode
192
+ *
193
+ * Features:
194
+ * - Process pool with configurable concurrency limit
195
+ * - Pending queue for overflow requests
196
+ * - Context caching with configurable TTL
197
+ * - Execution logging for debugging
198
+ * - Event emission for monitoring
199
+ * - Graceful termination
200
+ */
201
+ export declare class HeadlessWorkerExecutor extends EventEmitter {
202
+ private projectRoot;
203
+ private config;
204
+ private processPool;
205
+ private pendingQueue;
206
+ private contextCache;
207
+ private claudeCodeAvailable;
208
+ private claudeCodeVersion;
209
+ constructor(projectRoot: string, options?: HeadlessExecutorConfig);
210
+ /**
211
+ * Check if Claude Code CLI is available
212
+ */
213
+ isAvailable(): Promise<boolean>;
214
+ /**
215
+ * Get Claude Code version
216
+ */
217
+ getVersion(): Promise<string | null>;
218
+ /**
219
+ * Execute a headless worker
220
+ */
221
+ execute(workerType: HeadlessWorkerType, configOverrides?: Partial<HeadlessOptions>): Promise<HeadlessExecutionResult>;
222
+ /**
223
+ * Get pool status
224
+ */
225
+ getPoolStatus(): PoolStatus;
226
+ /**
227
+ * Get number of active executions
228
+ */
229
+ getActiveCount(): number;
230
+ /**
231
+ * Cancel a running execution
232
+ */
233
+ cancel(executionId: string): boolean;
234
+ /**
235
+ * Cancel all running executions
236
+ */
237
+ cancelAll(): number;
238
+ /**
239
+ * Clear context cache
240
+ */
241
+ clearContextCache(): void;
242
+ /**
243
+ * Get worker configuration
244
+ */
245
+ getConfig(workerType: HeadlessWorkerType): HeadlessWorkerConfig | undefined;
246
+ /**
247
+ * Get all headless worker types
248
+ */
249
+ getHeadlessWorkerTypes(): HeadlessWorkerType[];
250
+ /**
251
+ * Get all local worker types
252
+ */
253
+ getLocalWorkerTypes(): LocalWorkerType[];
254
+ /**
255
+ * Ensure log directory exists
256
+ */
257
+ private ensureLogDir;
258
+ /**
259
+ * Internal execution logic
260
+ */
261
+ private executeInternal;
262
+ /**
263
+ * Process the pending queue
264
+ */
265
+ private processQueue;
266
+ /**
267
+ * Build context from file patterns
268
+ */
269
+ private buildContext;
270
+ /**
271
+ * Simple glob implementation for file matching
272
+ */
273
+ private simpleGlob;
274
+ /**
275
+ * Match filename against a simple pattern
276
+ */
277
+ private matchesPattern;
278
+ /**
279
+ * Build full prompt with context
280
+ */
281
+ private buildPrompt;
282
+ /**
283
+ * Execute Claude Code in headless mode
284
+ */
285
+ private executeClaudeCode;
286
+ /**
287
+ * Parse JSON output from Claude Code
288
+ */
289
+ private parseJsonOutput;
290
+ /**
291
+ * Parse markdown output into sections
292
+ */
293
+ private parseMarkdownOutput;
294
+ /**
295
+ * Create an error result
296
+ */
297
+ private createErrorResult;
298
+ /**
299
+ * Log execution details for debugging
300
+ */
301
+ private logExecution;
302
+ }
303
+ export default HeadlessWorkerExecutor;
304
+ //# sourceMappingURL=headless-worker-executor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"headless-worker-executor.d.ts","sourceRoot":"","sources":["../../../src/services/headless-worker-executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAMrD;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAC1B,OAAO,GACP,UAAU,GACV,UAAU,GACV,UAAU,GACV,YAAY,GACZ,UAAU,GACV,UAAU,GACV,SAAS,CAAC;AAEd;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,aAAa,GAAG,WAAW,GAAG,SAAS,CAAC;AAE9E;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,YAAY,GAAG,UAAU,CAAC;AAE/D;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,QAAQ,GAAG,MAAM,G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