@supaku/agentfactory 0.1.0

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 (71) hide show
  1. package/LICENSE +21 -0
  2. package/dist/src/deployment/deployment-checker.d.ts +110 -0
  3. package/dist/src/deployment/deployment-checker.d.ts.map +1 -0
  4. package/dist/src/deployment/deployment-checker.js +242 -0
  5. package/dist/src/deployment/index.d.ts +3 -0
  6. package/dist/src/deployment/index.d.ts.map +1 -0
  7. package/dist/src/deployment/index.js +2 -0
  8. package/dist/src/index.d.ts +5 -0
  9. package/dist/src/index.d.ts.map +1 -0
  10. package/dist/src/index.js +4 -0
  11. package/dist/src/logger.d.ts +117 -0
  12. package/dist/src/logger.d.ts.map +1 -0
  13. package/dist/src/logger.js +430 -0
  14. package/dist/src/orchestrator/activity-emitter.d.ts +128 -0
  15. package/dist/src/orchestrator/activity-emitter.d.ts.map +1 -0
  16. package/dist/src/orchestrator/activity-emitter.js +406 -0
  17. package/dist/src/orchestrator/api-activity-emitter.d.ts +167 -0
  18. package/dist/src/orchestrator/api-activity-emitter.d.ts.map +1 -0
  19. package/dist/src/orchestrator/api-activity-emitter.js +469 -0
  20. package/dist/src/orchestrator/heartbeat-writer.d.ts +57 -0
  21. package/dist/src/orchestrator/heartbeat-writer.d.ts.map +1 -0
  22. package/dist/src/orchestrator/heartbeat-writer.js +137 -0
  23. package/dist/src/orchestrator/index.d.ts +20 -0
  24. package/dist/src/orchestrator/index.d.ts.map +1 -0
  25. package/dist/src/orchestrator/index.js +22 -0
  26. package/dist/src/orchestrator/log-analyzer.d.ts +160 -0
  27. package/dist/src/orchestrator/log-analyzer.d.ts.map +1 -0
  28. package/dist/src/orchestrator/log-analyzer.js +572 -0
  29. package/dist/src/orchestrator/log-config.d.ts +39 -0
  30. package/dist/src/orchestrator/log-config.d.ts.map +1 -0
  31. package/dist/src/orchestrator/log-config.js +45 -0
  32. package/dist/src/orchestrator/orchestrator.d.ts +246 -0
  33. package/dist/src/orchestrator/orchestrator.d.ts.map +1 -0
  34. package/dist/src/orchestrator/orchestrator.js +2525 -0
  35. package/dist/src/orchestrator/parse-work-result.d.ts +16 -0
  36. package/dist/src/orchestrator/parse-work-result.d.ts.map +1 -0
  37. package/dist/src/orchestrator/parse-work-result.js +73 -0
  38. package/dist/src/orchestrator/progress-logger.d.ts +72 -0
  39. package/dist/src/orchestrator/progress-logger.d.ts.map +1 -0
  40. package/dist/src/orchestrator/progress-logger.js +135 -0
  41. package/dist/src/orchestrator/session-logger.d.ts +159 -0
  42. package/dist/src/orchestrator/session-logger.d.ts.map +1 -0
  43. package/dist/src/orchestrator/session-logger.js +275 -0
  44. package/dist/src/orchestrator/state-recovery.d.ts +96 -0
  45. package/dist/src/orchestrator/state-recovery.d.ts.map +1 -0
  46. package/dist/src/orchestrator/state-recovery.js +301 -0
  47. package/dist/src/orchestrator/state-types.d.ts +165 -0
  48. package/dist/src/orchestrator/state-types.d.ts.map +1 -0
  49. package/dist/src/orchestrator/state-types.js +7 -0
  50. package/dist/src/orchestrator/stream-parser.d.ts +145 -0
  51. package/dist/src/orchestrator/stream-parser.d.ts.map +1 -0
  52. package/dist/src/orchestrator/stream-parser.js +131 -0
  53. package/dist/src/orchestrator/types.d.ts +205 -0
  54. package/dist/src/orchestrator/types.d.ts.map +1 -0
  55. package/dist/src/orchestrator/types.js +4 -0
  56. package/dist/src/providers/amp-provider.d.ts +20 -0
  57. package/dist/src/providers/amp-provider.d.ts.map +1 -0
  58. package/dist/src/providers/amp-provider.js +24 -0
  59. package/dist/src/providers/claude-provider.d.ts +18 -0
  60. package/dist/src/providers/claude-provider.d.ts.map +1 -0
  61. package/dist/src/providers/claude-provider.js +267 -0
  62. package/dist/src/providers/codex-provider.d.ts +21 -0
  63. package/dist/src/providers/codex-provider.d.ts.map +1 -0
  64. package/dist/src/providers/codex-provider.js +25 -0
  65. package/dist/src/providers/index.d.ts +42 -0
  66. package/dist/src/providers/index.d.ts.map +1 -0
  67. package/dist/src/providers/index.js +77 -0
  68. package/dist/src/providers/types.d.ts +147 -0
  69. package/dist/src/providers/types.d.ts.map +1 -0
  70. package/dist/src/providers/types.js +13 -0
  71. package/package.json +63 -0
@@ -0,0 +1,246 @@
1
+ /**
2
+ * Agent Orchestrator
3
+ * Spawns concurrent Claude agents to work on Linear backlog issues
4
+ * Uses the Claude Agent SDK for programmatic control
5
+ */
6
+ import { type AgentWorkType } from '@supaku/agentfactory-linear';
7
+ import type { OrchestratorConfig, OrchestratorIssue, AgentProcess, OrchestratorEvents, SpawnAgentOptions, OrchestratorResult, StopAgentResult, ForwardPromptResult, InjectMessageResult, SpawnAgentWithResumeOptions } from './types';
8
+ /**
9
+ * Generate a worktree identifier that includes the work type suffix
10
+ *
11
+ * @param issueIdentifier - Issue identifier (e.g., "SUP-294")
12
+ * @param workType - Type of work being performed
13
+ * @returns Worktree identifier with suffix (e.g., "SUP-294-QA")
14
+ */
15
+ export declare function getWorktreeIdentifier(issueIdentifier: string, workType: AgentWorkType): string;
16
+ export declare class AgentOrchestrator {
17
+ private readonly config;
18
+ private readonly client;
19
+ private readonly events;
20
+ private readonly activeAgents;
21
+ private readonly agentHandles;
22
+ private provider;
23
+ private readonly agentSessions;
24
+ private readonly activityEmitters;
25
+ private readonly sessionToIssue;
26
+ private readonly abortControllers;
27
+ private readonly agentLoggers;
28
+ private readonly heartbeatWriters;
29
+ private readonly progressLoggers;
30
+ private readonly sessionLoggers;
31
+ constructor(config?: OrchestratorConfig, events?: OrchestratorEvents);
32
+ /**
33
+ * Update the last activity timestamp for an agent (for inactivity timeout tracking)
34
+ * @param issueId - The issue ID of the agent
35
+ * @param activityType - Optional description of the activity type
36
+ */
37
+ private updateLastActivity;
38
+ /**
39
+ * Get timeout configuration for a specific work type
40
+ * @param workType - The work type to get timeout config for
41
+ * @returns Timeout configuration with inactivity and max session values
42
+ */
43
+ private getTimeoutConfig;
44
+ /**
45
+ * Get backlog issues for the configured project
46
+ */
47
+ getBacklogIssues(limit?: number): Promise<OrchestratorIssue[]>;
48
+ /**
49
+ * Validate that a path is a valid git worktree
50
+ */
51
+ private validateWorktree;
52
+ /**
53
+ * Extract the full error message from an execSync error.
54
+ *
55
+ * Node's execSync throws an Error where .message only contains
56
+ * "Command failed: <command>", but the actual git error output
57
+ * is in .stderr. This helper combines both for reliable pattern matching.
58
+ */
59
+ private getExecSyncErrorMessage;
60
+ /**
61
+ * Check if a git error indicates a branch/worktree conflict.
62
+ *
63
+ * Git uses different error messages depending on the situation:
64
+ * - "is already checked out at '/path'" - branch checked out in another worktree
65
+ * - "is already used by worktree at '/path'" - branch associated with another worktree
66
+ *
67
+ * Both mean the same thing: the branch is occupied by another worktree.
68
+ */
69
+ private isBranchConflictError;
70
+ /**
71
+ * Extract the conflicting worktree path from a git branch conflict error.
72
+ *
73
+ * Parses paths like:
74
+ * - "fatal: 'SUP-402' is already checked out at '/path/to/.worktrees/SUP-402-DEV'"
75
+ * - "fatal: 'SUP-402' is already used by worktree at '/path/to/.worktrees/SUP-402-DEV'"
76
+ */
77
+ private parseConflictingWorktreePath;
78
+ /**
79
+ * Attempt to clean up a stale worktree that is blocking branch creation.
80
+ *
81
+ * During dev\u2192qa\u2192acceptance handoffs, the prior work type's worktree may still
82
+ * exist after its agent has finished (the orchestrator cleans up externally,
83
+ * but there's a race window). This method checks if the blocking worktree's
84
+ * agent is still alive via heartbeat. If not, it removes the stale worktree
85
+ * so the new work type can proceed.
86
+ *
87
+ * @returns true if the conflicting worktree was cleaned up
88
+ */
89
+ private tryCleanupConflictingWorktree;
90
+ /**
91
+ * Handle a branch conflict error by attempting to clean up the stale worktree
92
+ * and retrying, or throwing a retriable error for the worker's retry loop.
93
+ */
94
+ private handleBranchConflict;
95
+ /**
96
+ * Create a git worktree for an issue with work type suffix
97
+ *
98
+ * @param issueIdentifier - Issue identifier (e.g., "SUP-294")
99
+ * @param workType - Type of work being performed
100
+ * @returns Object containing worktreePath and worktreeIdentifier
101
+ */
102
+ createWorktree(issueIdentifier: string, workType: AgentWorkType): {
103
+ worktreePath: string;
104
+ worktreeIdentifier: string;
105
+ };
106
+ /**
107
+ * Clean up a git worktree
108
+ *
109
+ * @param worktreeIdentifier - Worktree identifier with work type suffix (e.g., "SUP-294-QA")
110
+ */
111
+ removeWorktree(worktreeIdentifier: string): void;
112
+ /**
113
+ * Pre-install dependencies in a worktree before spawning an agent.
114
+ * This prevents agents from wasting time/tokens running pnpm install themselves,
115
+ * and avoids pathological polling loops when pnpm install is run as a background task.
116
+ *
117
+ * @param worktreePath - Absolute path to the worktree
118
+ * @param identifier - Issue identifier for logging (e.g., "SUP-123")
119
+ */
120
+ preInstallDependencies(worktreePath: string, identifier: string): void;
121
+ /**
122
+ * Spawn a Claude agent for a specific issue using the Agent SDK
123
+ */
124
+ spawnAgent(options: SpawnAgentOptions): AgentProcess;
125
+ /**
126
+ * Process the provider event stream and emit activities
127
+ */
128
+ private processEventStream;
129
+ /**
130
+ * Handle a single normalized agent event from any provider
131
+ */
132
+ private handleAgentEvent;
133
+ /**
134
+ * Extract GitHub PR URL from text (typically from gh pr create output)
135
+ */
136
+ private extractPullRequestUrl;
137
+ /**
138
+ * Update the Linear session with the PR URL
139
+ */
140
+ private updateSessionPullRequest;
141
+ /**
142
+ * Post completion comment with full result message
143
+ * Uses multi-comment splitting for long messages (up to 10 comments, 10k chars each)
144
+ */
145
+ private postCompletionComment;
146
+ /**
147
+ * Clean up agent resources
148
+ */
149
+ private cleanupAgent;
150
+ /**
151
+ * Finalize the session logger with final status
152
+ */
153
+ private finalizeSessionLogger;
154
+ /**
155
+ * Run the orchestrator - spawn agents for backlog issues
156
+ */
157
+ run(): Promise<OrchestratorResult>;
158
+ /**
159
+ * Spawn agent for a single issue (webhook-triggered or CLI)
160
+ * Generates a session ID if not provided to enable autonomous mode
161
+ *
162
+ * This method includes crash recovery support:
163
+ * - If a worktree exists with valid state and stale heartbeat, triggers recovery
164
+ * - If a worktree exists with fresh heartbeat (agent alive), throws error to prevent duplicates
165
+ *
166
+ * @param issueIdOrIdentifier - Issue ID or identifier (e.g., SUP-123)
167
+ * @param sessionId - Optional Linear session ID
168
+ * @param workType - Optional work type (auto-detected from issue status if not provided)
169
+ * @param prompt - Optional custom prompt override
170
+ */
171
+ spawnAgentForIssue(issueIdOrIdentifier: string, sessionId?: string, workType?: AgentWorkType, prompt?: string): Promise<AgentProcess>;
172
+ /**
173
+ * Get all active agents
174
+ */
175
+ getActiveAgents(): AgentProcess[];
176
+ /**
177
+ * Stop a running agent by issue ID
178
+ * @param issueId - The Linear issue ID
179
+ * @param cleanupWorktree - Whether to remove the git worktree
180
+ * @param stopReason - Why the agent is being stopped: 'user_request' or 'timeout'
181
+ */
182
+ stopAgent(issueId: string, cleanupWorktree?: boolean, stopReason?: 'user_request' | 'timeout'): Promise<StopAgentResult>;
183
+ /**
184
+ * Stop a running agent by session ID
185
+ */
186
+ stopAgentBySession(sessionId: string, cleanupWorktree?: boolean): Promise<StopAgentResult>;
187
+ /**
188
+ * Get agent by session ID
189
+ */
190
+ getAgentBySession(sessionId: string): AgentProcess | undefined;
191
+ /**
192
+ * Update the worker ID for all active activity emitters.
193
+ * Called after worker re-registration to ensure activities are attributed
194
+ * to the new worker ID and pass ownership checks.
195
+ *
196
+ * @param newWorkerId - The new worker ID after re-registration
197
+ */
198
+ updateWorkerId(newWorkerId: string): void;
199
+ /**
200
+ * Forward a follow-up prompt to an existing or new agent
201
+ *
202
+ * If the agent is running, attempts to inject the message into the running session
203
+ * without stopping it. If injection fails or agent isn't running, it will be
204
+ * stopped gracefully and resumed with the new prompt.
205
+ *
206
+ * @param workType - Optional work type. If not provided, inherits from existing agent or defaults to 'development'.
207
+ */
208
+ forwardPrompt(issueId: string, sessionId: string, prompt: string, claudeSessionId?: string, workType?: AgentWorkType): Promise<ForwardPromptResult>;
209
+ /**
210
+ * Inject a user message into a running agent session without stopping it.
211
+ *
212
+ * Uses the SDK's streamInput() method to send follow-up messages to a running session.
213
+ * This is the preferred method for user follow-ups as it doesn't interrupt agent work.
214
+ *
215
+ * @param issueId - The issue ID the agent is working on
216
+ * @param sessionId - The Linear session ID
217
+ * @param message - The user message to inject
218
+ * @returns Result indicating if injection was successful
219
+ */
220
+ injectMessage(issueId: string, sessionId: string, message: string): Promise<InjectMessageResult>;
221
+ /**
222
+ * Spawn an agent with resume capability for continuing a previous session
223
+ * If autoTransition is enabled, also transitions the issue status to the appropriate working state
224
+ */
225
+ spawnAgentWithResume(options: SpawnAgentWithResumeOptions): Promise<AgentProcess>;
226
+ /**
227
+ * Stop all running agents
228
+ */
229
+ stopAll(): void;
230
+ /**
231
+ * Wait for all agents to complete with inactivity-based timeout
232
+ *
233
+ * Unlike a simple session timeout, this method monitors each agent's activity
234
+ * and only stops agents that have been inactive for longer than the inactivity
235
+ * timeout. Active agents are allowed to run indefinitely (unless maxSessionTimeoutMs
236
+ * is set as a hard cap).
237
+ *
238
+ * @param inactivityTimeoutMsOverride - Override inactivity timeout from config (for backwards compatibility)
239
+ */
240
+ waitForAll(inactivityTimeoutMsOverride?: number): Promise<AgentProcess[]>;
241
+ }
242
+ /**
243
+ * Create an orchestrator instance
244
+ */
245
+ export declare function createOrchestrator(config?: OrchestratorConfig, events?: OrchestratorEvents): AgentOrchestrator;
246
+ //# sourceMappingURL=orchestrator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../../../src/orchestrator/orchestrator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA+BH,OAAO,EAML,KAAK,aAAa,EAMnB,MAAM,6BAA6B,CAAA;AAKpC,OAAO,KAAK,EACV,kBAAkB,EAClB,iBAAiB,EACjB,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAElB,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,2BAA2B,EAC5B,MAAM,SAAS,CAAA;AA0kBhB;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,eAAe,EAAE,MAAM,EACvB,QAAQ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