@agentuity/opencode 0.1.40 → 0.1.42

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 (165) hide show
  1. package/README.md +324 -19
  2. package/dist/agents/architect.d.ts +4 -0
  3. package/dist/agents/architect.d.ts.map +1 -0
  4. package/dist/agents/architect.js +259 -0
  5. package/dist/agents/architect.js.map +1 -0
  6. package/dist/agents/builder.d.ts +1 -1
  7. package/dist/agents/builder.d.ts.map +1 -1
  8. package/dist/agents/builder.js +44 -1
  9. package/dist/agents/builder.js.map +1 -1
  10. package/dist/agents/index.d.ts.map +1 -1
  11. package/dist/agents/index.js +6 -0
  12. package/dist/agents/index.js.map +1 -1
  13. package/dist/agents/lead.d.ts +1 -1
  14. package/dist/agents/lead.d.ts.map +1 -1
  15. package/dist/agents/lead.js +185 -22
  16. package/dist/agents/lead.js.map +1 -1
  17. package/dist/agents/planner.d.ts +4 -0
  18. package/dist/agents/planner.d.ts.map +1 -0
  19. package/dist/agents/planner.js +158 -0
  20. package/dist/agents/planner.js.map +1 -0
  21. package/dist/agents/runner.d.ts +4 -0
  22. package/dist/agents/runner.d.ts.map +1 -0
  23. package/dist/agents/runner.js +364 -0
  24. package/dist/agents/runner.js.map +1 -0
  25. package/dist/agents/types.d.ts +5 -1
  26. package/dist/agents/types.d.ts.map +1 -1
  27. package/dist/background/concurrency.d.ts +36 -0
  28. package/dist/background/concurrency.d.ts.map +1 -0
  29. package/dist/background/concurrency.js +92 -0
  30. package/dist/background/concurrency.js.map +1 -0
  31. package/dist/background/index.d.ts +5 -0
  32. package/dist/background/index.d.ts.map +1 -0
  33. package/dist/background/index.js +4 -0
  34. package/dist/background/index.js.map +1 -0
  35. package/dist/background/manager.d.ts +54 -0
  36. package/dist/background/manager.d.ts.map +1 -0
  37. package/dist/background/manager.js +409 -0
  38. package/dist/background/manager.js.map +1 -0
  39. package/dist/background/types.d.ts +47 -0
  40. package/dist/background/types.d.ts.map +1 -0
  41. package/dist/background/types.js +2 -0
  42. package/dist/background/types.js.map +1 -0
  43. package/dist/config/index.d.ts +2 -0
  44. package/dist/config/index.d.ts.map +1 -1
  45. package/dist/config/index.js +2 -0
  46. package/dist/config/index.js.map +1 -1
  47. package/dist/config/loader.d.ts +24 -0
  48. package/dist/config/loader.d.ts.map +1 -1
  49. package/dist/config/loader.js +102 -23
  50. package/dist/config/loader.js.map +1 -1
  51. package/dist/config/presets.d.ts +16 -0
  52. package/dist/config/presets.d.ts.map +1 -0
  53. package/dist/config/presets.js +20 -0
  54. package/dist/config/presets.js.map +1 -0
  55. package/dist/config/validation.d.ts +26 -0
  56. package/dist/config/validation.d.ts.map +1 -0
  57. package/dist/config/validation.js +48 -0
  58. package/dist/config/validation.js.map +1 -0
  59. package/dist/index.d.ts +1 -1
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js.map +1 -1
  62. package/dist/plugin/hooks/cadence.d.ts.map +1 -1
  63. package/dist/plugin/hooks/cadence.js +3 -1
  64. package/dist/plugin/hooks/cadence.js.map +1 -1
  65. package/dist/plugin/hooks/keyword.d.ts.map +1 -1
  66. package/dist/plugin/hooks/keyword.js +3 -0
  67. package/dist/plugin/hooks/keyword.js.map +1 -1
  68. package/dist/plugin/plugin.d.ts.map +1 -1
  69. package/dist/plugin/plugin.js +335 -36
  70. package/dist/plugin/plugin.js.map +1 -1
  71. package/dist/skills/frontmatter.d.ts +7 -0
  72. package/dist/skills/frontmatter.d.ts.map +1 -0
  73. package/dist/skills/frontmatter.js +17 -0
  74. package/dist/skills/frontmatter.js.map +1 -0
  75. package/dist/skills/index.d.ts +4 -0
  76. package/dist/skills/index.d.ts.map +1 -0
  77. package/dist/skills/index.js +4 -0
  78. package/dist/skills/index.js.map +1 -0
  79. package/dist/skills/loader.d.ts +20 -0
  80. package/dist/skills/loader.d.ts.map +1 -0
  81. package/dist/skills/loader.js +152 -0
  82. package/dist/skills/loader.js.map +1 -0
  83. package/dist/skills/types.d.ts +41 -0
  84. package/dist/skills/types.d.ts.map +1 -0
  85. package/dist/skills/types.js +2 -0
  86. package/dist/skills/types.js.map +1 -0
  87. package/dist/tmux/decision-engine.d.ts +24 -0
  88. package/dist/tmux/decision-engine.d.ts.map +1 -0
  89. package/dist/tmux/decision-engine.js +193 -0
  90. package/dist/tmux/decision-engine.js.map +1 -0
  91. package/dist/tmux/executor.d.ts +84 -0
  92. package/dist/tmux/executor.d.ts.map +1 -0
  93. package/dist/tmux/executor.js +546 -0
  94. package/dist/tmux/executor.js.map +1 -0
  95. package/dist/tmux/index.d.ts +7 -0
  96. package/dist/tmux/index.d.ts.map +1 -0
  97. package/dist/tmux/index.js +7 -0
  98. package/dist/tmux/index.js.map +1 -0
  99. package/dist/tmux/manager.d.ts +116 -0
  100. package/dist/tmux/manager.d.ts.map +1 -0
  101. package/dist/tmux/manager.js +488 -0
  102. package/dist/tmux/manager.js.map +1 -0
  103. package/dist/tmux/state-query.d.ts +7 -0
  104. package/dist/tmux/state-query.d.ts.map +1 -0
  105. package/dist/tmux/state-query.js +70 -0
  106. package/dist/tmux/state-query.js.map +1 -0
  107. package/dist/tmux/types.d.ts +97 -0
  108. package/dist/tmux/types.d.ts.map +1 -0
  109. package/dist/tmux/types.js +8 -0
  110. package/dist/tmux/types.js.map +1 -0
  111. package/dist/tmux/utils.d.ts +32 -0
  112. package/dist/tmux/utils.d.ts.map +1 -0
  113. package/dist/tmux/utils.js +80 -0
  114. package/dist/tmux/utils.js.map +1 -0
  115. package/dist/tools/background.d.ts +61 -0
  116. package/dist/tools/background.d.ts.map +1 -0
  117. package/dist/tools/background.js +78 -0
  118. package/dist/tools/background.js.map +1 -0
  119. package/dist/tools/delegate.d.ts +6 -0
  120. package/dist/tools/delegate.d.ts.map +1 -1
  121. package/dist/tools/delegate.js +8 -2
  122. package/dist/tools/delegate.js.map +1 -1
  123. package/dist/tools/index.d.ts +1 -0
  124. package/dist/tools/index.d.ts.map +1 -1
  125. package/dist/tools/index.js +1 -0
  126. package/dist/tools/index.js.map +1 -1
  127. package/dist/types.d.ts +118 -18
  128. package/dist/types.d.ts.map +1 -1
  129. package/dist/types.js +49 -7
  130. package/dist/types.js.map +1 -1
  131. package/package.json +4 -3
  132. package/src/agents/architect.ts +262 -0
  133. package/src/agents/builder.ts +44 -1
  134. package/src/agents/index.ts +6 -0
  135. package/src/agents/lead.ts +185 -22
  136. package/src/agents/planner.ts +161 -0
  137. package/src/agents/runner.ts +367 -0
  138. package/src/agents/types.ts +5 -1
  139. package/src/background/concurrency.ts +116 -0
  140. package/src/background/index.ts +4 -0
  141. package/src/background/manager.ts +478 -0
  142. package/src/background/types.ts +52 -0
  143. package/src/config/index.ts +2 -0
  144. package/src/config/loader.ts +128 -31
  145. package/src/config/presets.ts +21 -0
  146. package/src/config/validation.ts +70 -0
  147. package/src/index.ts +1 -0
  148. package/src/plugin/hooks/cadence.ts +2 -1
  149. package/src/plugin/hooks/keyword.ts +3 -0
  150. package/src/plugin/plugin.ts +374 -42
  151. package/src/skills/frontmatter.ts +25 -0
  152. package/src/skills/index.ts +3 -0
  153. package/src/skills/loader.ts +185 -0
  154. package/src/skills/types.ts +43 -0
  155. package/src/tmux/decision-engine.ts +246 -0
  156. package/src/tmux/executor.ts +618 -0
  157. package/src/tmux/index.ts +14 -0
  158. package/src/tmux/manager.ts +577 -0
  159. package/src/tmux/state-query.ts +77 -0
  160. package/src/tmux/types.ts +107 -0
  161. package/src/tmux/utils.ts +85 -0
  162. package/src/tools/background.ts +145 -0
  163. package/src/tools/delegate.ts +8 -2
  164. package/src/tools/index.ts +9 -0
  165. package/src/types.ts +88 -15
@@ -0,0 +1,618 @@
1
+ import type { PaneAction, WindowState, TmuxConfig } from './types';
2
+ import { runTmuxCommand, runTmuxCommandSync } from './utils';
3
+ import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
4
+ import { homedir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import { spawn, spawnSync } from 'bun';
7
+
8
+ /**
9
+ * Path to persist the agents window ID for crash recovery.
10
+ * Uses ~/.config/agentuity/coder/cache/ which is consistent with other Agentuity paths
11
+ * and likely exists for any Agentuity user.
12
+ */
13
+ const CACHE_DIR = join(homedir(), '.config', 'agentuity', 'coder', 'cache');
14
+ const AGENTS_WINDOW_FILE = join(CACHE_DIR, 'agents-window-id');
15
+
16
+ /**
17
+ * Escape a string for safe use in shell commands.
18
+ * Wraps in single quotes and escapes any internal single quotes.
19
+ */
20
+ function shellEscape(str: string): string {
21
+ // Replace single quotes with '\'' (end quote, escaped quote, start quote)
22
+ return `'${str.replace(/'/g, "'\\''")}'`;
23
+ }
24
+
25
+ /** Maximum retries for recursive spawn attempts to prevent infinite loops */
26
+ const MAX_SPAWN_RETRIES = 3;
27
+
28
+ export interface ActionResult {
29
+ success: boolean;
30
+ paneId?: string;
31
+ windowId?: string;
32
+ pid?: number;
33
+ error?: string;
34
+ }
35
+
36
+ const PROCESS_TERM_WAIT_MS = 1000;
37
+
38
+ function isProcessAlive(pid: number): boolean {
39
+ try {
40
+ process.kill(pid, 0);
41
+ return true;
42
+ } catch (error) {
43
+ const code = (error as NodeJS.ErrnoException).code;
44
+ return code !== 'ESRCH';
45
+ }
46
+ }
47
+
48
+ async function getPanePid(paneId: string): Promise<number | undefined> {
49
+ if (!paneId) return undefined;
50
+ const result = await runTmuxCommand(['display', '-p', '-t', paneId, '#{pane_pid}']);
51
+ if (!result.success) return undefined;
52
+ const pid = Number(result.output.trim());
53
+ if (!Number.isFinite(pid) || pid <= 0) return undefined;
54
+ return pid;
55
+ }
56
+
57
+ /**
58
+ * Kill a process and all its children (the entire process tree).
59
+ *
60
+ * This is necessary because we spawn `bash -c "opencode attach ...; tmux kill-pane"`
61
+ * and #{pane_pid} returns the bash PID, not the opencode attach PID.
62
+ * We need to kill the children (opencode attach) not just the parent (bash).
63
+ */
64
+ export async function killProcessByPid(pid: number): Promise<boolean> {
65
+ if (!Number.isFinite(pid) || pid <= 0) return false;
66
+
67
+ // First, kill all child processes
68
+ try {
69
+ const proc = spawn(['pkill', '-TERM', '-P', String(pid)], {
70
+ stdout: 'pipe',
71
+ stderr: 'pipe',
72
+ });
73
+ await proc.exited;
74
+ } catch {
75
+ // Ignore errors - children may not exist
76
+ }
77
+
78
+ // Then kill the parent
79
+ try {
80
+ process.kill(pid, 'SIGTERM');
81
+ } catch (error) {
82
+ const code = (error as NodeJS.ErrnoException).code;
83
+ if (code === 'ESRCH') return true;
84
+ return false;
85
+ }
86
+
87
+ await new Promise((resolve) => setTimeout(resolve, PROCESS_TERM_WAIT_MS));
88
+
89
+ // Check if parent and children are dead
90
+ if (!isProcessAlive(pid)) return true;
91
+
92
+ // Force kill children
93
+ try {
94
+ const proc = spawn(['pkill', '-KILL', '-P', String(pid)], {
95
+ stdout: 'pipe',
96
+ stderr: 'pipe',
97
+ });
98
+ await proc.exited;
99
+ } catch {
100
+ // Ignore errors
101
+ }
102
+
103
+ // Force kill parent
104
+ try {
105
+ process.kill(pid, 'SIGKILL');
106
+ } catch (error) {
107
+ const code = (error as NodeJS.ErrnoException).code;
108
+ if (code === 'ESRCH') return true;
109
+ return false;
110
+ }
111
+
112
+ return !isProcessAlive(pid);
113
+ }
114
+
115
+ /**
116
+ * State for separate-window mode - tracks the dedicated "Agents" window
117
+ */
118
+ let agentsWindowId: string | undefined;
119
+
120
+ /**
121
+ * Ensure the cache directory exists
122
+ */
123
+ function ensureCacheDir(): void {
124
+ if (!existsSync(CACHE_DIR)) {
125
+ mkdirSync(CACHE_DIR, { recursive: true });
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Persist the agents window ID to disk for crash recovery
131
+ */
132
+ function persistAgentsWindowId(windowId: string): void {
133
+ try {
134
+ ensureCacheDir();
135
+ writeFileSync(AGENTS_WINDOW_FILE, windowId, 'utf-8');
136
+ } catch {
137
+ // Ignore write errors - persistence is best-effort
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Load the agents window ID from disk (for crash recovery)
143
+ */
144
+ function loadPersistedAgentsWindowId(): string | undefined {
145
+ try {
146
+ if (!existsSync(AGENTS_WINDOW_FILE)) return undefined;
147
+ const windowId = readFileSync(AGENTS_WINDOW_FILE, 'utf-8').trim();
148
+ return windowId || undefined;
149
+ } catch {
150
+ return undefined;
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Clear the persisted agents window ID
156
+ */
157
+ function clearPersistedAgentsWindowId(): void {
158
+ try {
159
+ if (existsSync(AGENTS_WINDOW_FILE)) {
160
+ unlinkSync(AGENTS_WINDOW_FILE);
161
+ }
162
+ } catch {
163
+ // Ignore delete errors
164
+ }
165
+ }
166
+
167
+ /**
168
+ * Execute a single pane action
169
+ *
170
+ * All agents spawn in a dedicated "Agents" window with tiled grid layout.
171
+ */
172
+ export async function executeAction(
173
+ action: PaneAction,
174
+ ctx: { config: TmuxConfig; serverUrl: string; windowState: WindowState }
175
+ ): Promise<ActionResult> {
176
+ switch (action.type) {
177
+ case 'spawn':
178
+ return spawnInAgentsWindow(action, { serverUrl: ctx.serverUrl });
179
+ case 'close':
180
+ return closePane(action);
181
+ case 'replace':
182
+ return replacePane(action, ctx);
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Execute multiple actions in sequence
188
+ */
189
+ export async function executeActions(
190
+ actions: PaneAction[],
191
+ ctx: { config: TmuxConfig; serverUrl: string; windowState: WindowState }
192
+ ): Promise<{
193
+ success: boolean;
194
+ spawnedPaneId?: string;
195
+ results: Array<{ action: PaneAction; result: ActionResult }>;
196
+ }> {
197
+ const results: Array<{ action: PaneAction; result: ActionResult }> = [];
198
+ let spawnedPaneId: string | undefined;
199
+
200
+ for (const action of actions) {
201
+ const result = await executeAction(action, ctx);
202
+ results.push({ action, result });
203
+ if (!result.success) {
204
+ return { success: false, spawnedPaneId, results };
205
+ }
206
+ if (action.type === 'spawn' && result.paneId) {
207
+ spawnedPaneId = result.paneId;
208
+ }
209
+ }
210
+
211
+ return { success: true, spawnedPaneId, results };
212
+ }
213
+
214
+ /**
215
+ * Close an existing pane
216
+ * Uses: tmux kill-pane -t <paneId>
217
+ */
218
+ async function closePane(action: Extract<PaneAction, { type: 'close' }>): Promise<ActionResult> {
219
+ return closePaneById(action.paneId);
220
+ }
221
+
222
+ /**
223
+ * Close a pane by its ID
224
+ * Exported for use by TmuxSessionManager when sessions complete
225
+ */
226
+ export async function closePaneById(paneId: string, pid?: number): Promise<ActionResult> {
227
+ let resolvedPid = pid;
228
+ if (!resolvedPid) {
229
+ resolvedPid = await getPanePid(paneId);
230
+ }
231
+
232
+ if (resolvedPid) {
233
+ await killProcessByPid(resolvedPid);
234
+ }
235
+
236
+ const result = await runTmuxCommand(['kill-pane', '-t', paneId]);
237
+ if (!result.success) {
238
+ return { success: false, error: result.output };
239
+ }
240
+ return { success: true };
241
+ }
242
+
243
+ /**
244
+ * Replace an existing pane with a new session
245
+ * Pane self-destructs when command exits (session complete, server died, etc.)
246
+ */
247
+ async function replacePane(
248
+ action: Extract<PaneAction, { type: 'replace' }>,
249
+ ctx: { serverUrl: string }
250
+ ): Promise<ActionResult> {
251
+ // Pane kills itself when opencode attach exits (for any reason)
252
+ // Use shellEscape to prevent shell injection via session IDs
253
+ const escapedServerUrl = shellEscape(ctx.serverUrl);
254
+ const escapedSessionId = shellEscape(action.newSessionId);
255
+ const command = `opencode attach ${escapedServerUrl} --session ${escapedSessionId}; tmux kill-pane`;
256
+ const result = await runTmuxCommand(['respawn-pane', '-k', '-t', action.paneId, command]);
257
+ if (!result.success) {
258
+ return { success: false, error: result.output };
259
+ }
260
+ const pid = await getPanePid(action.paneId);
261
+ return { success: true, paneId: action.paneId, pid };
262
+ }
263
+
264
+ /**
265
+ * Spawn agent in a dedicated "Agents" window with tiled grid layout
266
+ *
267
+ * On first spawn: Creates a new window named "Agents"
268
+ * Subsequent spawns: Splits within that window
269
+ * After each spawn: Applies tiled layout for a clean grid
270
+ *
271
+ * This keeps the main pane untouched while grouping all agent panes together.
272
+ * Tip: Click a pane to select it, then press Ctrl-b z to zoom/unzoom.
273
+ *
274
+ * @param retryCount - Internal counter to prevent infinite recursion (default 0)
275
+ */
276
+ async function spawnInAgentsWindow(
277
+ action: Extract<PaneAction, { type: 'spawn' }>,
278
+ ctx: { serverUrl: string },
279
+ retryCount = 0
280
+ ): Promise<ActionResult> {
281
+ // Prevent infinite recursion if tmux keeps failing
282
+ if (retryCount >= MAX_SPAWN_RETRIES) {
283
+ return {
284
+ success: false,
285
+ error: `Failed to spawn agent pane after ${MAX_SPAWN_RETRIES} attempts`,
286
+ };
287
+ }
288
+
289
+ // Pane kills itself when opencode attach exits (session complete, server died, etc.)
290
+ // Use shellEscape to prevent shell injection via session IDs
291
+ const escapedServerUrl = shellEscape(ctx.serverUrl);
292
+ const escapedSessionId = shellEscape(action.sessionId);
293
+ const command = `opencode attach ${escapedServerUrl} --session ${escapedSessionId}; tmux kill-pane`;
294
+ const layout = 'tiled'; // Always use tiled layout for grid arrangement
295
+
296
+ // Check if we have a cached agents window ID and if it still exists
297
+ if (agentsWindowId) {
298
+ const checkResult = await runTmuxCommand([
299
+ 'list-panes',
300
+ '-t',
301
+ agentsWindowId,
302
+ '-F',
303
+ '#{pane_id}',
304
+ ]);
305
+
306
+ if (!checkResult.success) {
307
+ // Window no longer exists, clear the cache
308
+ agentsWindowId = undefined;
309
+ }
310
+ }
311
+
312
+ // If no agents window exists, create one
313
+ if (!agentsWindowId) {
314
+ const createResult = await runTmuxCommand([
315
+ 'new-window',
316
+ '-d', // Don't switch to new window
317
+ '-P',
318
+ '-F',
319
+ '#{window_id}:#{pane_id}',
320
+ '-n',
321
+ 'Agents',
322
+ command,
323
+ ]);
324
+
325
+ if (!createResult.success) {
326
+ return { success: false, error: createResult.output };
327
+ }
328
+
329
+ // Parse window_id:pane_id from output
330
+ const output = createResult.output?.trim() || '';
331
+ const [windowId, paneId] = output.split(':');
332
+ agentsWindowId = windowId;
333
+
334
+ // Persist for crash recovery
335
+ if (agentsWindowId) {
336
+ persistAgentsWindowId(agentsWindowId);
337
+ }
338
+
339
+ // Apply initial layout (useful when more panes are added later)
340
+ if (agentsWindowId && layout) {
341
+ await runTmuxCommand(['select-layout', '-t', agentsWindowId, layout]);
342
+ }
343
+
344
+ const pid = paneId ? await getPanePid(paneId) : undefined;
345
+ return { success: true, paneId, windowId, pid };
346
+ }
347
+
348
+ // Agents window exists - split within it
349
+ // First, get the first pane in the agents window to use as split target
350
+ const listResult = await runTmuxCommand([
351
+ 'list-panes',
352
+ '-t',
353
+ agentsWindowId,
354
+ '-F',
355
+ '#{pane_id}',
356
+ ]);
357
+
358
+ if (!listResult.success || !listResult.output) {
359
+ // Fallback: create new window (with retry counter)
360
+ agentsWindowId = undefined;
361
+ return spawnInAgentsWindow(action, ctx, retryCount + 1);
362
+ }
363
+
364
+ const targetPaneId = listResult.output.split('\n')[0]?.trim();
365
+ if (!targetPaneId) {
366
+ // Fallback: create new window (with retry counter)
367
+ agentsWindowId = undefined;
368
+ return spawnInAgentsWindow(action, ctx, retryCount + 1);
369
+ }
370
+
371
+ // Split within the agents window
372
+ const splitResult = await runTmuxCommand([
373
+ 'split-window',
374
+ action.splitDirection,
375
+ '-t',
376
+ targetPaneId,
377
+ '-P',
378
+ '-F',
379
+ '#{pane_id}',
380
+ command,
381
+ ]);
382
+
383
+ if (!splitResult.success) {
384
+ return { success: false, error: splitResult.output };
385
+ }
386
+
387
+ const paneId = splitResult.output?.trim();
388
+
389
+ // Apply the configured layout to the agents window (e.g., tiled for grid)
390
+ if (agentsWindowId && layout) {
391
+ await runTmuxCommand(['select-layout', '-t', agentsWindowId, layout]);
392
+ }
393
+
394
+ const pid = paneId ? await getPanePid(paneId) : undefined;
395
+ return {
396
+ success: true,
397
+ paneId: paneId || undefined,
398
+ windowId: agentsWindowId,
399
+ pid,
400
+ };
401
+ }
402
+
403
+ /**
404
+ * Reset the agents window state (for cleanup)
405
+ */
406
+ export function resetAgentsWindow(): void {
407
+ agentsWindowId = undefined;
408
+ clearPersistedAgentsWindowId();
409
+ }
410
+
411
+ /**
412
+ * Close the agents window if it exists
413
+ * This kills the entire window, which closes all panes within it
414
+ */
415
+ export async function closeAgentsWindow(): Promise<void> {
416
+ // Try to recover window ID from disk if not in memory
417
+ const windowId = agentsWindowId ?? loadPersistedAgentsWindowId();
418
+ if (!windowId) return;
419
+
420
+ // Kill the entire window (closes all panes within it)
421
+ await runTmuxCommand(['kill-window', '-t', windowId]);
422
+ agentsWindowId = undefined;
423
+ clearPersistedAgentsWindowId();
424
+ }
425
+
426
+ /**
427
+ * Synchronously close the agents window (for shutdown)
428
+ * Uses spawnSync to ensure it completes before process exit
429
+ */
430
+ export function closeAgentsWindowSync(): void {
431
+ // Try to recover window ID from disk if not in memory
432
+ const windowId = agentsWindowId ?? loadPersistedAgentsWindowId();
433
+ if (!windowId) return;
434
+
435
+ // Kill the entire window synchronously
436
+ runTmuxCommandSync(['kill-window', '-t', windowId]);
437
+ agentsWindowId = undefined;
438
+ clearPersistedAgentsWindowId();
439
+ }
440
+
441
+ /**
442
+ * Get the current agents window ID (for testing/debugging)
443
+ * Also checks persisted file for crash recovery
444
+ */
445
+ export function getAgentsWindowId(): string | undefined {
446
+ return agentsWindowId ?? loadPersistedAgentsWindowId();
447
+ }
448
+
449
+ /**
450
+ * Kill all orphaned opencode attach processes for a given server URL.
451
+ * This is a fallback cleanup method when PID-based cleanup fails.
452
+ *
453
+ * @param serverUrl - The server URL to match (optional, kills all if not provided)
454
+ * @param logger - Optional logging function for debug output
455
+ * @returns Number of processes killed
456
+ */
457
+ export async function killOrphanedAttachProcesses(
458
+ serverUrl?: string,
459
+ logger?: (msg: string) => void
460
+ ): Promise<number> {
461
+ const log = logger ?? (() => {});
462
+
463
+ try {
464
+ // Use pkill with pattern matching for opencode attach
465
+ const { spawn } = await import('bun');
466
+
467
+ // First, find matching processes to log what we're killing
468
+ const psProc = spawn(['ps', 'aux'], { stdout: 'pipe', stderr: 'pipe' });
469
+ await psProc.exited;
470
+ const psOutput = await new Response(psProc.stdout).text();
471
+ const lines = psOutput.split('\n');
472
+
473
+ const matchingPids: number[] = [];
474
+ for (const line of lines) {
475
+ if (!line.includes('opencode attach')) continue;
476
+ if (serverUrl && !line.includes(serverUrl)) continue;
477
+ // Don't kill ourselves
478
+ if (line.includes(String(process.pid))) continue;
479
+
480
+ const parts = line.trim().split(/\s+/);
481
+ const pid = Number(parts[1]);
482
+ if (Number.isFinite(pid) && pid > 0) {
483
+ matchingPids.push(pid);
484
+ }
485
+ }
486
+
487
+ if (matchingPids.length === 0) {
488
+ log('No orphaned opencode attach processes found');
489
+ return 0;
490
+ }
491
+
492
+ log(`Found ${matchingPids.length} orphaned processes: ${matchingPids.join(', ')}`);
493
+
494
+ // Kill each process individually for better control
495
+ let killed = 0;
496
+ for (const pid of matchingPids) {
497
+ try {
498
+ // Try SIGTERM first
499
+ process.kill(pid, 'SIGTERM');
500
+ log(`Sent SIGTERM to PID ${pid}`);
501
+ killed++;
502
+ } catch (error) {
503
+ const code = (error as NodeJS.ErrnoException).code;
504
+ if (code !== 'ESRCH') {
505
+ log(`Failed to kill PID ${pid}: ${code}`);
506
+ }
507
+ }
508
+ }
509
+
510
+ // Wait a bit then SIGKILL any survivors
511
+ await new Promise((resolve) => setTimeout(resolve, PROCESS_TERM_WAIT_MS));
512
+
513
+ for (const pid of matchingPids) {
514
+ if (!isProcessAlive(pid)) continue;
515
+ try {
516
+ process.kill(pid, 'SIGKILL');
517
+ log(`Sent SIGKILL to PID ${pid}`);
518
+ } catch {
519
+ // Ignore errors on SIGKILL
520
+ }
521
+ }
522
+
523
+ log(`Cleanup complete: killed ${killed} processes`);
524
+ return killed;
525
+ } catch (error) {
526
+ log(`Fallback cleanup failed: ${error}`);
527
+ return 0;
528
+ }
529
+ }
530
+
531
+ /**
532
+ * Synchronous version of killOrphanedAttachProcesses for shutdown handlers.
533
+ * Uses spawnSync to ensure completion before process exit.
534
+ *
535
+ * @param serverUrl - The server URL to match (optional, kills all if not provided)
536
+ * @param logger - Optional logging function for debug output
537
+ * @returns Number of processes killed
538
+ */
539
+ export function killOrphanedAttachProcessesSync(
540
+ serverUrl?: string,
541
+ logger?: (msg: string) => void
542
+ ): number {
543
+ const log = logger ?? (() => {});
544
+
545
+ try {
546
+ // Find matching processes
547
+ const psResult = spawnSync(['ps', 'aux'], { timeout: 2000 });
548
+ if (psResult.exitCode !== 0) {
549
+ log('Failed to list processes');
550
+ return 0;
551
+ }
552
+
553
+ const psOutput = psResult.stdout?.toString() ?? '';
554
+ const lines = psOutput.split('\n');
555
+
556
+ const matchingPids: number[] = [];
557
+ for (const line of lines) {
558
+ if (!line.includes('opencode attach')) continue;
559
+ if (serverUrl && !line.includes(serverUrl)) continue;
560
+ // Don't kill ourselves
561
+ if (line.includes(String(process.pid))) continue;
562
+
563
+ const parts = line.trim().split(/\s+/);
564
+ const pid = Number(parts[1]);
565
+ if (Number.isFinite(pid) && pid > 0) {
566
+ matchingPids.push(pid);
567
+ }
568
+ }
569
+
570
+ if (matchingPids.length === 0) {
571
+ log('No orphaned opencode attach processes found');
572
+ return 0;
573
+ }
574
+
575
+ log(`Found ${matchingPids.length} orphaned processes: ${matchingPids.join(', ')}`);
576
+
577
+ // Kill each process
578
+ let killed = 0;
579
+ for (const pid of matchingPids) {
580
+ try {
581
+ process.kill(pid, 'SIGTERM');
582
+ log(`Sent SIGTERM to PID ${pid}`);
583
+ killed++;
584
+ } catch (error) {
585
+ const code = (error as NodeJS.ErrnoException).code;
586
+ if (code !== 'ESRCH') {
587
+ log(`Failed to kill PID ${pid}: ${code}`);
588
+ }
589
+ }
590
+ }
591
+
592
+ // Brief wait using SharedArrayBuffer (sync sleep)
593
+ try {
594
+ const buffer = new SharedArrayBuffer(4);
595
+ const view = new Int32Array(buffer);
596
+ Atomics.wait(view, 0, 0, PROCESS_TERM_WAIT_MS);
597
+ } catch {
598
+ // Ignore sleep errors
599
+ }
600
+
601
+ // SIGKILL survivors
602
+ for (const pid of matchingPids) {
603
+ if (!isProcessAlive(pid)) continue;
604
+ try {
605
+ process.kill(pid, 'SIGKILL');
606
+ log(`Sent SIGKILL to PID ${pid}`);
607
+ } catch {
608
+ // Ignore errors
609
+ }
610
+ }
611
+
612
+ log(`Cleanup complete: killed ${killed} processes`);
613
+ return killed;
614
+ } catch (error) {
615
+ log(`Fallback cleanup failed: ${error}`);
616
+ return 0;
617
+ }
618
+ }
@@ -0,0 +1,14 @@
1
+ export * from './types';
2
+ export * from './utils';
3
+ export { queryWindowState } from './state-query';
4
+ export { decideSpawnActions, calculateCapacity } from './decision-engine';
5
+ export {
6
+ executeAction,
7
+ executeActions,
8
+ closeAgentsWindow,
9
+ closeAgentsWindowSync,
10
+ getAgentsWindowId,
11
+ killOrphanedAttachProcesses,
12
+ killOrphanedAttachProcessesSync,
13
+ } from './executor';
14
+ export { TmuxSessionManager } from './manager';