aicodeman 0.2.8 → 0.3.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 (348) hide show
  1. package/README.md +91 -0
  2. package/dist/ai-idle-checker.d.ts.map +1 -1
  3. package/dist/ai-idle-checker.js +3 -2
  4. package/dist/ai-idle-checker.js.map +1 -1
  5. package/dist/ai-plan-checker.d.ts.map +1 -1
  6. package/dist/ai-plan-checker.js +3 -2
  7. package/dist/ai-plan-checker.js.map +1 -1
  8. package/dist/bash-tool-parser.d.ts +2 -3
  9. package/dist/bash-tool-parser.d.ts.map +1 -1
  10. package/dist/bash-tool-parser.js +14 -31
  11. package/dist/bash-tool-parser.js.map +1 -1
  12. package/dist/config/ai-defaults.d.ts +16 -0
  13. package/dist/config/ai-defaults.d.ts.map +1 -0
  14. package/dist/config/ai-defaults.js +16 -0
  15. package/dist/config/ai-defaults.js.map +1 -0
  16. package/dist/config/auth-config.d.ts +19 -0
  17. package/dist/config/auth-config.d.ts.map +1 -0
  18. package/dist/config/auth-config.js +28 -0
  19. package/dist/config/auth-config.js.map +1 -0
  20. package/dist/config/exec-timeout.d.ts +10 -0
  21. package/dist/config/exec-timeout.d.ts.map +1 -0
  22. package/dist/config/exec-timeout.js +10 -0
  23. package/dist/config/exec-timeout.js.map +1 -0
  24. package/dist/config/map-limits.d.ts +4 -0
  25. package/dist/config/map-limits.d.ts.map +1 -1
  26. package/dist/config/map-limits.js +7 -0
  27. package/dist/config/map-limits.js.map +1 -1
  28. package/dist/config/server-timing.d.ts +36 -0
  29. package/dist/config/server-timing.d.ts.map +1 -0
  30. package/dist/config/server-timing.js +51 -0
  31. package/dist/config/server-timing.js.map +1 -0
  32. package/dist/config/team-config.d.ts +16 -0
  33. package/dist/config/team-config.d.ts.map +1 -0
  34. package/dist/config/team-config.js +16 -0
  35. package/dist/config/team-config.js.map +1 -0
  36. package/dist/config/terminal-limits.d.ts +18 -0
  37. package/dist/config/terminal-limits.d.ts.map +1 -0
  38. package/dist/config/terminal-limits.js +18 -0
  39. package/dist/config/terminal-limits.js.map +1 -0
  40. package/dist/config/tunnel-config.d.ts +27 -0
  41. package/dist/config/tunnel-config.d.ts.map +1 -0
  42. package/dist/config/tunnel-config.js +36 -0
  43. package/dist/config/tunnel-config.js.map +1 -0
  44. package/dist/hooks-config.d.ts.map +1 -1
  45. package/dist/hooks-config.js +7 -6
  46. package/dist/hooks-config.js.map +1 -1
  47. package/dist/image-watcher.d.ts +4 -4
  48. package/dist/image-watcher.d.ts.map +1 -1
  49. package/dist/image-watcher.js +17 -30
  50. package/dist/image-watcher.js.map +1 -1
  51. package/dist/index.js +1 -2
  52. package/dist/index.js.map +1 -1
  53. package/dist/plan-orchestrator.d.ts +2 -24
  54. package/dist/plan-orchestrator.d.ts.map +1 -1
  55. package/dist/plan-orchestrator.js.map +1 -1
  56. package/dist/push-store.d.ts +1 -1
  57. package/dist/push-store.d.ts.map +1 -1
  58. package/dist/push-store.js +4 -12
  59. package/dist/push-store.js.map +1 -1
  60. package/dist/ralph-fix-plan-watcher.d.ts +91 -0
  61. package/dist/ralph-fix-plan-watcher.d.ts.map +1 -0
  62. package/dist/ralph-fix-plan-watcher.js +326 -0
  63. package/dist/ralph-fix-plan-watcher.js.map +1 -0
  64. package/dist/ralph-plan-tracker.d.ts +201 -0
  65. package/dist/ralph-plan-tracker.d.ts.map +1 -0
  66. package/dist/ralph-plan-tracker.js +325 -0
  67. package/dist/ralph-plan-tracker.js.map +1 -0
  68. package/dist/ralph-stall-detector.d.ts +84 -0
  69. package/dist/ralph-stall-detector.d.ts.map +1 -0
  70. package/dist/ralph-stall-detector.js +139 -0
  71. package/dist/ralph-stall-detector.js.map +1 -0
  72. package/dist/ralph-status-parser.d.ts +141 -0
  73. package/dist/ralph-status-parser.d.ts.map +1 -0
  74. package/dist/ralph-status-parser.js +478 -0
  75. package/dist/ralph-status-parser.js.map +1 -0
  76. package/dist/ralph-tracker.d.ts +194 -685
  77. package/dist/ralph-tracker.d.ts.map +1 -1
  78. package/dist/ralph-tracker.js +349 -1713
  79. package/dist/ralph-tracker.js.map +1 -1
  80. package/dist/respawn-adaptive-timing.d.ts +61 -0
  81. package/dist/respawn-adaptive-timing.d.ts.map +1 -0
  82. package/dist/respawn-adaptive-timing.js +105 -0
  83. package/dist/respawn-adaptive-timing.js.map +1 -0
  84. package/dist/respawn-controller.d.ts +14 -101
  85. package/dist/respawn-controller.d.ts.map +1 -1
  86. package/dist/respawn-controller.js +155 -594
  87. package/dist/respawn-controller.js.map +1 -1
  88. package/dist/respawn-health.d.ts +54 -0
  89. package/dist/respawn-health.d.ts.map +1 -0
  90. package/dist/respawn-health.js +183 -0
  91. package/dist/respawn-health.js.map +1 -0
  92. package/dist/respawn-metrics.d.ts +81 -0
  93. package/dist/respawn-metrics.d.ts.map +1 -0
  94. package/dist/respawn-metrics.js +198 -0
  95. package/dist/respawn-metrics.js.map +1 -0
  96. package/dist/respawn-patterns.d.ts +45 -0
  97. package/dist/respawn-patterns.d.ts.map +1 -0
  98. package/dist/respawn-patterns.js +125 -0
  99. package/dist/respawn-patterns.js.map +1 -0
  100. package/dist/session-auto-ops.d.ts +89 -0
  101. package/dist/session-auto-ops.d.ts.map +1 -0
  102. package/dist/session-auto-ops.js +224 -0
  103. package/dist/session-auto-ops.js.map +1 -0
  104. package/dist/session-cli-builder.d.ts +62 -0
  105. package/dist/session-cli-builder.d.ts.map +1 -0
  106. package/dist/session-cli-builder.js +121 -0
  107. package/dist/session-cli-builder.js.map +1 -0
  108. package/dist/session-task-cache.d.ts +52 -0
  109. package/dist/session-task-cache.d.ts.map +1 -0
  110. package/dist/session-task-cache.js +90 -0
  111. package/dist/session-task-cache.js.map +1 -0
  112. package/dist/session.d.ts +2 -33
  113. package/dist/session.d.ts.map +1 -1
  114. package/dist/session.js +58 -309
  115. package/dist/session.js.map +1 -1
  116. package/dist/state-store.d.ts +9 -2
  117. package/dist/state-store.d.ts.map +1 -1
  118. package/dist/state-store.js +112 -39
  119. package/dist/state-store.js.map +1 -1
  120. package/dist/subagent-watcher.d.ts +16 -9
  121. package/dist/subagent-watcher.d.ts.map +1 -1
  122. package/dist/subagent-watcher.js +126 -147
  123. package/dist/subagent-watcher.js.map +1 -1
  124. package/dist/team-watcher.d.ts +3 -0
  125. package/dist/team-watcher.d.ts.map +1 -1
  126. package/dist/team-watcher.js +54 -5
  127. package/dist/team-watcher.js.map +1 -1
  128. package/dist/tmux-manager.d.ts.map +1 -1
  129. package/dist/tmux-manager.js +1 -2
  130. package/dist/tmux-manager.js.map +1 -1
  131. package/dist/tunnel-manager.d.ts +26 -0
  132. package/dist/tunnel-manager.d.ts.map +1 -1
  133. package/dist/tunnel-manager.js +127 -7
  134. package/dist/tunnel-manager.js.map +1 -1
  135. package/dist/types/api.d.ts +93 -0
  136. package/dist/types/api.d.ts.map +1 -0
  137. package/dist/types/api.js +83 -0
  138. package/dist/types/api.js.map +1 -0
  139. package/dist/types/app-state.d.ts +100 -0
  140. package/dist/types/app-state.d.ts.map +1 -0
  141. package/dist/types/app-state.js +59 -0
  142. package/dist/types/app-state.js.map +1 -0
  143. package/dist/types/common.d.ts +70 -0
  144. package/dist/types/common.d.ts.map +1 -0
  145. package/dist/types/common.js +8 -0
  146. package/dist/types/common.js.map +1 -0
  147. package/dist/types/index.d.ts +18 -0
  148. package/dist/types/index.d.ts.map +1 -0
  149. package/dist/types/index.js +18 -0
  150. package/dist/types/index.js.map +1 -0
  151. package/dist/types/lifecycle.d.ts +17 -0
  152. package/dist/types/lifecycle.d.ts.map +1 -0
  153. package/dist/types/lifecycle.js +5 -0
  154. package/dist/types/lifecycle.js.map +1 -0
  155. package/dist/types/plan.d.ts +32 -0
  156. package/dist/types/plan.d.ts.map +1 -0
  157. package/dist/types/plan.js +5 -0
  158. package/dist/types/plan.js.map +1 -0
  159. package/dist/types/push.d.ts +23 -0
  160. package/dist/types/push.d.ts.map +1 -0
  161. package/dist/types/push.js +5 -0
  162. package/dist/types/push.js.map +1 -0
  163. package/dist/types/ralph.d.ts +241 -0
  164. package/dist/types/ralph.d.ts.map +1 -0
  165. package/dist/types/ralph.js +49 -0
  166. package/dist/types/ralph.js.map +1 -0
  167. package/dist/types/respawn.d.ts +250 -0
  168. package/dist/types/respawn.d.ts.map +1 -0
  169. package/dist/types/respawn.js +5 -0
  170. package/dist/types/respawn.js.map +1 -0
  171. package/dist/types/run-summary.d.ts +81 -0
  172. package/dist/types/run-summary.d.ts.map +1 -0
  173. package/dist/types/run-summary.js +22 -0
  174. package/dist/types/run-summary.js.map +1 -0
  175. package/dist/types/session.d.ts +130 -0
  176. package/dist/types/session.d.ts.map +1 -0
  177. package/dist/types/session.js +5 -0
  178. package/dist/types/session.js.map +1 -0
  179. package/dist/types/task.d.ts +58 -0
  180. package/dist/types/task.d.ts.map +1 -0
  181. package/dist/types/task.js +5 -0
  182. package/dist/types/task.js.map +1 -0
  183. package/dist/types/teams.d.ts +55 -0
  184. package/dist/types/teams.d.ts.map +1 -0
  185. package/dist/types/teams.js +5 -0
  186. package/dist/types/teams.js.map +1 -0
  187. package/dist/types/tools.d.ts +46 -0
  188. package/dist/types/tools.d.ts.map +1 -0
  189. package/dist/types/tools.js +5 -0
  190. package/dist/types/tools.js.map +1 -0
  191. package/dist/types.d.ts +1 -1138
  192. package/dist/types.d.ts.map +1 -1
  193. package/dist/types.js +1 -214
  194. package/dist/types.js.map +1 -1
  195. package/dist/utils/claude-cli-resolver.d.ts.map +1 -1
  196. package/dist/utils/claude-cli-resolver.js +1 -2
  197. package/dist/utils/claude-cli-resolver.js.map +1 -1
  198. package/dist/utils/debouncer.d.ts +111 -0
  199. package/dist/utils/debouncer.d.ts.map +1 -0
  200. package/dist/utils/debouncer.js +162 -0
  201. package/dist/utils/debouncer.js.map +1 -0
  202. package/dist/utils/index.d.ts +3 -2
  203. package/dist/utils/index.d.ts.map +1 -1
  204. package/dist/utils/index.js +3 -2
  205. package/dist/utils/index.js.map +1 -1
  206. package/dist/utils/opencode-cli-resolver.d.ts.map +1 -1
  207. package/dist/utils/opencode-cli-resolver.js +1 -2
  208. package/dist/utils/opencode-cli-resolver.js.map +1 -1
  209. package/dist/utils/string-similarity.d.ts +0 -57
  210. package/dist/utils/string-similarity.d.ts.map +1 -1
  211. package/dist/utils/string-similarity.js +3 -18
  212. package/dist/utils/string-similarity.js.map +1 -1
  213. package/dist/web/middleware/auth.d.ts +31 -0
  214. package/dist/web/middleware/auth.d.ts.map +1 -0
  215. package/dist/web/middleware/auth.js +154 -0
  216. package/dist/web/middleware/auth.js.map +1 -0
  217. package/dist/web/ports/auth-port.d.ts +18 -0
  218. package/dist/web/ports/auth-port.d.ts.map +1 -0
  219. package/dist/web/ports/auth-port.js +6 -0
  220. package/dist/web/ports/auth-port.js.map +1 -0
  221. package/dist/web/ports/config-port.d.ts +28 -0
  222. package/dist/web/ports/config-port.d.ts.map +1 -0
  223. package/dist/web/ports/config-port.js +6 -0
  224. package/dist/web/ports/config-port.js.map +1 -0
  225. package/dist/web/ports/event-port.d.ts +13 -0
  226. package/dist/web/ports/event-port.d.ts.map +1 -0
  227. package/dist/web/ports/event-port.js +6 -0
  228. package/dist/web/ports/event-port.js.map +1 -0
  229. package/dist/web/ports/index.d.ts +14 -0
  230. package/dist/web/ports/index.d.ts.map +1 -0
  231. package/dist/web/ports/index.js +9 -0
  232. package/dist/web/ports/index.js.map +1 -0
  233. package/dist/web/ports/infra-port.d.ts +36 -0
  234. package/dist/web/ports/infra-port.d.ts.map +1 -0
  235. package/dist/web/ports/infra-port.js +6 -0
  236. package/dist/web/ports/infra-port.js.map +1 -0
  237. package/dist/web/ports/respawn-port.d.ts +20 -0
  238. package/dist/web/ports/respawn-port.d.ts.map +1 -0
  239. package/dist/web/ports/respawn-port.js +6 -0
  240. package/dist/web/ports/respawn-port.js.map +1 -0
  241. package/dist/web/ports/session-port.d.ts +15 -0
  242. package/dist/web/ports/session-port.d.ts.map +1 -0
  243. package/dist/web/ports/session-port.js +6 -0
  244. package/dist/web/ports/session-port.js.map +1 -0
  245. package/dist/web/public/api-client.js +70 -0
  246. package/dist/web/public/api-client.js.br +0 -0
  247. package/dist/web/public/api-client.js.gz +0 -0
  248. package/dist/web/public/app.js +152 -236
  249. package/dist/web/public/app.js.br +0 -0
  250. package/dist/web/public/app.js.gz +0 -0
  251. package/dist/web/public/constants.js +238 -0
  252. package/dist/web/public/constants.js.br +0 -0
  253. package/dist/web/public/constants.js.gz +0 -0
  254. package/dist/web/public/index.html +11 -3
  255. package/dist/web/public/index.html.br +0 -0
  256. package/dist/web/public/index.html.gz +0 -0
  257. package/dist/web/public/keyboard-accessory.js +279 -0
  258. package/dist/web/public/keyboard-accessory.js.br +0 -0
  259. package/dist/web/public/keyboard-accessory.js.gz +0 -0
  260. package/dist/web/public/mobile-handlers.js +467 -0
  261. package/dist/web/public/mobile-handlers.js.br +0 -0
  262. package/dist/web/public/mobile-handlers.js.gz +0 -0
  263. package/dist/web/public/mobile.css.gz +0 -0
  264. package/dist/web/public/notification-manager.js +445 -0
  265. package/dist/web/public/notification-manager.js.br +0 -0
  266. package/dist/web/public/notification-manager.js.gz +0 -0
  267. package/dist/web/public/ralph-wizard.js +3 -3
  268. package/dist/web/public/ralph-wizard.js.br +0 -0
  269. package/dist/web/public/ralph-wizard.js.gz +0 -0
  270. package/dist/web/public/styles.css.gz +0 -0
  271. package/dist/web/public/subagent-windows.js +1115 -0
  272. package/dist/web/public/subagent-windows.js.br +0 -0
  273. package/dist/web/public/subagent-windows.js.gz +0 -0
  274. package/dist/web/public/sw.js.gz +0 -0
  275. package/dist/web/public/upload.html.gz +0 -0
  276. package/dist/web/public/vendor/xterm-addon-fit.min.js.gz +0 -0
  277. package/dist/web/public/vendor/xterm-addon-unicode11.min.js.gz +0 -0
  278. package/dist/web/public/vendor/xterm-addon-webgl.min.js.gz +0 -0
  279. package/dist/web/public/vendor/xterm.css.gz +0 -0
  280. package/dist/web/public/vendor/xterm.min.js.gz +0 -0
  281. package/dist/web/public/voice-input.js +858 -0
  282. package/dist/web/public/voice-input.js.br +0 -0
  283. package/dist/web/public/voice-input.js.gz +0 -0
  284. package/dist/web/route-helpers.d.ts +38 -0
  285. package/dist/web/route-helpers.d.ts.map +1 -0
  286. package/dist/web/route-helpers.js +143 -0
  287. package/dist/web/route-helpers.js.map +1 -0
  288. package/dist/web/routes/case-routes.d.ts +9 -0
  289. package/dist/web/routes/case-routes.d.ts.map +1 -0
  290. package/dist/web/routes/case-routes.js +419 -0
  291. package/dist/web/routes/case-routes.js.map +1 -0
  292. package/dist/web/routes/file-routes.d.ts +8 -0
  293. package/dist/web/routes/file-routes.d.ts.map +1 -0
  294. package/dist/web/routes/file-routes.js +337 -0
  295. package/dist/web/routes/file-routes.js.map +1 -0
  296. package/dist/web/routes/hook-event-routes.d.ts +9 -0
  297. package/dist/web/routes/hook-event-routes.d.ts.map +1 -0
  298. package/dist/web/routes/hook-event-routes.js +57 -0
  299. package/dist/web/routes/hook-event-routes.js.map +1 -0
  300. package/dist/web/routes/index.d.ts +16 -0
  301. package/dist/web/routes/index.d.ts.map +1 -0
  302. package/dist/web/routes/index.js +16 -0
  303. package/dist/web/routes/index.js.map +1 -0
  304. package/dist/web/routes/mux-routes.d.ts +8 -0
  305. package/dist/web/routes/mux-routes.d.ts.map +1 -0
  306. package/dist/web/routes/mux-routes.js +32 -0
  307. package/dist/web/routes/mux-routes.js.map +1 -0
  308. package/dist/web/routes/plan-routes.d.ts +9 -0
  309. package/dist/web/routes/plan-routes.d.ts.map +1 -0
  310. package/dist/web/routes/plan-routes.js +381 -0
  311. package/dist/web/routes/plan-routes.js.map +1 -0
  312. package/dist/web/routes/push-routes.d.ts +8 -0
  313. package/dist/web/routes/push-routes.d.ts.map +1 -0
  314. package/dist/web/routes/push-routes.js +49 -0
  315. package/dist/web/routes/push-routes.js.map +1 -0
  316. package/dist/web/routes/ralph-routes.d.ts +9 -0
  317. package/dist/web/routes/ralph-routes.d.ts.map +1 -0
  318. package/dist/web/routes/ralph-routes.js +475 -0
  319. package/dist/web/routes/ralph-routes.js.map +1 -0
  320. package/dist/web/routes/respawn-routes.d.ts +8 -0
  321. package/dist/web/routes/respawn-routes.d.ts.map +1 -0
  322. package/dist/web/routes/respawn-routes.js +260 -0
  323. package/dist/web/routes/respawn-routes.js.map +1 -0
  324. package/dist/web/routes/scheduled-routes.d.ts +8 -0
  325. package/dist/web/routes/scheduled-routes.d.ts.map +1 -0
  326. package/dist/web/routes/scheduled-routes.js +51 -0
  327. package/dist/web/routes/scheduled-routes.js.map +1 -0
  328. package/dist/web/routes/session-routes.d.ts +9 -0
  329. package/dist/web/routes/session-routes.d.ts.map +1 -0
  330. package/dist/web/routes/session-routes.js +729 -0
  331. package/dist/web/routes/session-routes.js.map +1 -0
  332. package/dist/web/routes/system-routes.d.ts +9 -0
  333. package/dist/web/routes/system-routes.d.ts.map +1 -0
  334. package/dist/web/routes/system-routes.js +678 -0
  335. package/dist/web/routes/system-routes.js.map +1 -0
  336. package/dist/web/routes/team-routes.d.ts +8 -0
  337. package/dist/web/routes/team-routes.d.ts.map +1 -0
  338. package/dist/web/routes/team-routes.js +14 -0
  339. package/dist/web/routes/team-routes.js.map +1 -0
  340. package/dist/web/schemas.d.ts +43 -3
  341. package/dist/web/schemas.d.ts.map +1 -1
  342. package/dist/web/schemas.js +6 -2
  343. package/dist/web/schemas.js.map +1 -1
  344. package/dist/web/server.d.ts +10 -9
  345. package/dist/web/server.d.ts.map +1 -1
  346. package/dist/web/server.js +342 -3829
  347. package/dist/web/server.js.map +1 -1
  348. package/package.json +1 -1
@@ -0,0 +1,1115 @@
1
+ // Codeman — Subagent window management for CodemanApp
2
+ // Loaded after app.js (needs CodemanApp class defined)
3
+
4
+ Object.assign(CodemanApp.prototype, {
5
+ // Render subagent badge with dropdown for minimized agents on a tab
6
+ renderSubagentTabBadge(sessionId, minimizedAgents) {
7
+ if (!minimizedAgents || minimizedAgents.size === 0) return '';
8
+
9
+ const agentItems = [];
10
+ for (const agentId of minimizedAgents) {
11
+ const agent = this.subagents.get(agentId);
12
+ const displayName = agent?.description || agentId.substring(0, 12);
13
+ const truncatedName = displayName.length > 25 ? displayName.substring(0, 25) + '…' : displayName;
14
+ const statusClass = agent?.status || 'idle';
15
+ agentItems.push(`
16
+ <div class="subagent-dropdown-item" onclick="event.stopPropagation(); app.restoreMinimizedSubagent('${escapeHtml(agentId)}', '${escapeHtml(sessionId)}')" title="Click to restore">
17
+ <span class="subagent-dropdown-status ${statusClass}"></span>
18
+ <span class="subagent-dropdown-name">${escapeHtml(truncatedName)}</span>
19
+ <span class="subagent-dropdown-close" onclick="event.stopPropagation(); app.permanentlyCloseMinimizedSubagent('${escapeHtml(agentId)}', '${escapeHtml(sessionId)}')" title="Dismiss">&times;</span>
20
+ </div>
21
+ `);
22
+ }
23
+
24
+ // Compact badge - shows on hover, click to pin open
25
+ const count = minimizedAgents.size;
26
+ const label = count === 1 ? 'AGENT' : `AGENTS (${count})`;
27
+ return `
28
+ <span class="tab-subagent-badge"
29
+ onmouseenter="app.showSubagentDropdown(this)"
30
+ onmouseleave="app.scheduleHideSubagentDropdown(this)"
31
+ onclick="event.stopPropagation(); app.pinSubagentDropdown(this);">
32
+ <span class="subagent-label">${label}</span>
33
+ <div class="subagent-dropdown" onmouseenter="app.cancelHideSubagentDropdown()" onmouseleave="app.scheduleHideSubagentDropdown(this.parentElement)">
34
+ ${agentItems.join('')}
35
+ </div>
36
+ </span>
37
+ `;
38
+ },
39
+
40
+ // Restore a minimized subagent window
41
+ restoreMinimizedSubagent(agentId, sessionId) {
42
+ // Remove from minimized set
43
+ const minimizedAgents = this.minimizedSubagents.get(sessionId);
44
+ if (minimizedAgents) {
45
+ minimizedAgents.delete(agentId);
46
+ if (minimizedAgents.size === 0) {
47
+ this.minimizedSubagents.delete(sessionId);
48
+ }
49
+ }
50
+
51
+ // Restore the window
52
+ this.restoreSubagentWindow(agentId);
53
+
54
+ // Re-render tabs to update badge
55
+ this.renderSessionTabs();
56
+
57
+ // Persist the state change
58
+ this.saveSubagentWindowStates();
59
+ },
60
+
61
+ // Permanently close a minimized subagent (remove from DOM and minimized set)
62
+ permanentlyCloseMinimizedSubagent(agentId, sessionId) {
63
+ // Remove from minimized set
64
+ const minimizedAgents = this.minimizedSubagents.get(sessionId);
65
+ if (minimizedAgents) {
66
+ minimizedAgents.delete(agentId);
67
+ if (minimizedAgents.size === 0) {
68
+ this.minimizedSubagents.delete(sessionId);
69
+ }
70
+ }
71
+
72
+ // Force close the window (removes from DOM)
73
+ this.forceCloseSubagentWindow(agentId);
74
+
75
+ // Re-render tabs to update badge
76
+ this.renderSessionTabs();
77
+ this.updateConnectionLines();
78
+
79
+ // Persist the state change
80
+ this.saveSubagentWindowStates();
81
+ },
82
+
83
+ // ========== Subagent Window State Persistence ==========
84
+
85
+ /**
86
+ * Save subagent window states (minimized/open) to server for cross-browser persistence.
87
+ * Called when a window is minimized, restored, or auto-minimized on completion.
88
+ */
89
+ async saveSubagentWindowStates() {
90
+ // Build state object: which agents are minimized per session
91
+ const minimizedState = {};
92
+ for (const [sessionId, agentIds] of this.minimizedSubagents) {
93
+ minimizedState[sessionId] = Array.from(agentIds);
94
+ }
95
+
96
+ // Also track which windows are open (not minimized)
97
+ const openWindows = [];
98
+ for (const [agentId, windowData] of this.subagentWindows) {
99
+ if (!windowData.minimized) {
100
+ openWindows.push({
101
+ agentId,
102
+ position: windowData.position || null
103
+ });
104
+ }
105
+ }
106
+
107
+ const windowStates = { minimized: minimizedState, open: openWindows };
108
+
109
+ // Save to localStorage for quick restore
110
+ localStorage.setItem('codeman-subagent-window-states', JSON.stringify(windowStates));
111
+
112
+ // Save to server for cross-browser persistence
113
+ try {
114
+ await this._apiPut('/api/subagent-window-states', windowStates);
115
+ } catch (err) {
116
+ console.error('Failed to save subagent window states to server:', err);
117
+ }
118
+ },
119
+
120
+ /**
121
+ * Restore subagent window states after loading subagents.
122
+ * Opens windows that were open before, keeps minimized ones minimized.
123
+ * IMPORTANT: Parent associations are loaded from subagentParentMap BEFORE this is called.
124
+ */
125
+ async restoreSubagentWindowStates() {
126
+ const states = await this.loadSubagentWindowStates();
127
+
128
+ // Restore minimized state using the PERSISTENT parent map
129
+ // Skip old agents from previous runs to avoid confusion
130
+ const cutoffTime = Date.now() - 10 * 60 * 1000; // 10 minutes
131
+ for (const [savedSessionId, agentIds] of Object.entries(states.minimized || {})) {
132
+ if (Array.isArray(agentIds) && agentIds.length > 0) {
133
+ for (const agentId of agentIds) {
134
+ const agent = this.subagents.get(agentId);
135
+ if (!agent) continue; // Agent no longer exists
136
+
137
+ // Skip completed or old agents
138
+ const agentStartTime = agent.startedAt || 0;
139
+ if (agent.status === 'completed' || agentStartTime < cutoffTime) continue;
140
+
141
+ // Use the PERSISTENT parent map (THE source of truth)
142
+ // Fall back to saved sessionId only if it exists in current sessions
143
+ const parentFromMap = this.subagentParentMap.get(agentId);
144
+ const correctSessionId = parentFromMap ||
145
+ (this.sessions.has(savedSessionId) ? savedSessionId : null);
146
+
147
+ if (correctSessionId) {
148
+ // Ensure the parent map has this association
149
+ if (!parentFromMap && this.sessions.has(savedSessionId)) {
150
+ this.setAgentParentSessionId(agentId, savedSessionId);
151
+ }
152
+
153
+ if (!this.minimizedSubagents.has(correctSessionId)) {
154
+ this.minimizedSubagents.set(correctSessionId, new Set());
155
+ }
156
+ this.minimizedSubagents.get(correctSessionId).add(agentId);
157
+ }
158
+ }
159
+ }
160
+ }
161
+
162
+ // Restore open windows (for recent, non-completed agents only)
163
+ const now = Date.now();
164
+ const maxAgeMs = 10 * 60 * 1000; // 10 minutes - don't restore windows for old agents
165
+ for (const { agentId, position } of (states.open || [])) {
166
+ const agent = this.subagents.get(agentId);
167
+ // Only restore window if agent exists, is recent, and is still active/idle
168
+ const agentAge = agent?.startedAt ? now - agent.startedAt : Infinity;
169
+ if (agent && agent.status !== 'completed' && agentAge < maxAgeMs) {
170
+ this.openSubagentWindow(agentId);
171
+ // Restore position if saved (with viewport bounds check)
172
+ if (position) {
173
+ const windowData = this.subagentWindows.get(agentId);
174
+ if (windowData && windowData.element) {
175
+ // Parse position values and clamp to viewport
176
+ let left = parseInt(position.left, 10) || 50;
177
+ let top = parseInt(position.top, 10) || WINDOW_INITIAL_TOP_PX;
178
+ const viewportWidth = window.innerWidth;
179
+ const viewportHeight = window.innerHeight;
180
+ const windowWidth = 420;
181
+ const windowHeight = 350;
182
+ left = Math.max(10, Math.min(left, viewportWidth - windowWidth - 10));
183
+ top = Math.max(10, Math.min(top, viewportHeight - windowHeight - 10));
184
+ windowData.element.style.left = `${left}px`;
185
+ windowData.element.style.top = `${top}px`;
186
+ windowData.position = { left: `${left}px`, top: `${top}px` };
187
+ }
188
+ }
189
+ }
190
+ }
191
+
192
+ this.renderSessionTabs(); // Update tab badges
193
+ this.saveSubagentWindowStates(); // Persist corrected mappings
194
+
195
+ // Update connection lines after all windows are restored (use rAF to ensure DOM is ready)
196
+ requestAnimationFrame(() => {
197
+ this.updateConnectionLines();
198
+ });
199
+ },
200
+
201
+ // ========== Subagent Connection Lines ==========
202
+ //
203
+ // Connection lines are drawn from agent windows to their parent TABs.
204
+ // The parent TAB is determined by the PERSISTENT subagentParentMap.
205
+ // This map stores agentId -> sessionId, where sessionId is the tab's data-id.
206
+
207
+ updateConnectionLines() {
208
+ // Coalesce multiple calls — uses background scheduler priority to avoid
209
+ // competing with terminal writes for main thread time
210
+ if (!this._connectionLinesScheduled) {
211
+ this._connectionLinesScheduled = true;
212
+ scheduleBackground(() => {
213
+ this._connectionLinesScheduled = false;
214
+ this._updateConnectionLinesImmediate();
215
+ });
216
+ }
217
+ },
218
+
219
+ _updateConnectionLinesImmediate() {
220
+ const svg = document.getElementById('connectionLines');
221
+ if (!svg) return;
222
+
223
+ // Check if Ralph wizard modal is open
224
+ const wizardModal = document.getElementById('ralphWizardModal');
225
+ const wizardOpen = wizardModal?.classList.contains('active');
226
+ const wizardContent = wizardOpen ? wizardModal.querySelector('.modal-content') : null;
227
+
228
+ // Collect visible regular subagent windows
229
+ const visibleSubagentWindows = [];
230
+ for (const [agentId, windowInfo] of this.subagentWindows) {
231
+ if (windowInfo.minimized || windowInfo.hidden) continue;
232
+ const win = windowInfo.element;
233
+ if (!win) continue;
234
+ visibleSubagentWindows.push({ agentId, windowInfo, win });
235
+ }
236
+
237
+ // Get plan subagent windows as array for distribution
238
+ const planSubagentArray = Array.from(this.planSubagents.entries())
239
+ .filter(([, data]) => data.element)
240
+ .map(([id, data]) => ({ id, ...data }));
241
+
242
+ // === PHASE 1: Batch all layout reads (getBoundingClientRect) ===
243
+ // Reading layout properties forces the browser to calculate layout.
244
+ // By batching all reads before any writes, we avoid repeated forced reflows.
245
+ const rects = new Map();
246
+
247
+ // Read all subagent window rects
248
+ for (const { agentId, win } of visibleSubagentWindows) {
249
+ rects.set('sub:' + agentId, win.getBoundingClientRect());
250
+ }
251
+
252
+ // Read all plan subagent rects
253
+ for (const planAgent of planSubagentArray) {
254
+ rects.set('plan:' + planAgent.id, planAgent.element.getBoundingClientRect());
255
+ }
256
+
257
+ // Read wizard rect (if open)
258
+ let wizardRect = null;
259
+ if (wizardOpen && wizardContent) {
260
+ wizardRect = wizardContent.getBoundingClientRect();
261
+ }
262
+
263
+ // Read tab rects for normal mode (only tabs that are actually needed)
264
+ if (!wizardOpen) {
265
+ for (const { agentId } of visibleSubagentWindows) {
266
+ const parentSessionId = this.subagentParentMap.get(agentId);
267
+ if (!parentSessionId || rects.has('tab:' + parentSessionId)) continue;
268
+ const tab = document.querySelector(`.session-tab[data-id="${parentSessionId}"]`);
269
+ if (tab) rects.set('tab:' + parentSessionId, tab.getBoundingClientRect());
270
+ }
271
+ }
272
+
273
+ // Read plan window rects for wizard-to-plan lines
274
+ if (wizardOpen && wizardContent && this.planSubagents.size > 0 && !this.planAgentsMinimized) {
275
+ for (const [agentId, windowData] of this.planSubagents) {
276
+ if (!windowData.element) continue;
277
+ const key = 'planwin:' + agentId;
278
+ if (!rects.has(key)) rects.set(key, windowData.element.getBoundingClientRect());
279
+ }
280
+ }
281
+
282
+ // === PHASE 2: DOM writes using cached rects (no more layout reads) ===
283
+ svg.innerHTML = '';
284
+
285
+ for (const { agentId } of visibleSubagentWindows) {
286
+ const winRect = rects.get('sub:' + agentId);
287
+
288
+ // If wizard is open with plan subagents, connect regular subagents to plan subagent windows
289
+ if (wizardOpen && wizardContent && planSubagentArray.length > 0) {
290
+ // Find the nearest plan subagent window to connect to
291
+ let nearestPlanAgent = null;
292
+ let nearestDistance = Infinity;
293
+
294
+ for (const planAgent of planSubagentArray) {
295
+ const planRect = rects.get('plan:' + planAgent.id);
296
+ const planCenterX = planRect.left + planRect.width / 2;
297
+ const planCenterY = planRect.top + planRect.height / 2;
298
+ const winCenterX = winRect.left + winRect.width / 2;
299
+ const winCenterY = winRect.top + winRect.height / 2;
300
+ const distance = Math.hypot(planCenterX - winCenterX, planCenterY - winCenterY);
301
+
302
+ if (distance < nearestDistance) {
303
+ nearestDistance = distance;
304
+ nearestPlanAgent = planAgent;
305
+ }
306
+ }
307
+
308
+ if (nearestPlanAgent) {
309
+ const planRect = rects.get('plan:' + nearestPlanAgent.id);
310
+
311
+ // Draw line from plan subagent window to regular subagent window
312
+ let x1, y1, x2, y2;
313
+ const planCenterX = planRect.left + planRect.width / 2;
314
+ const winCenterX = winRect.left + winRect.width / 2;
315
+
316
+ if (winCenterX < planCenterX) {
317
+ x1 = planRect.left;
318
+ y1 = planRect.top + planRect.height / 2;
319
+ x2 = winRect.right;
320
+ y2 = winRect.top + winRect.height / 2;
321
+ } else {
322
+ x1 = planRect.right;
323
+ y1 = planRect.top + planRect.height / 2;
324
+ x2 = winRect.left;
325
+ y2 = winRect.top + winRect.height / 2;
326
+ }
327
+
328
+ const midX = (x1 + x2) / 2;
329
+ const path = `M ${x1} ${y1} C ${midX} ${y1}, ${midX} ${y2}, ${x2} ${y2}`;
330
+
331
+ const line = document.createElementNS('http://www.w3.org/2000/svg', 'path');
332
+ line.setAttribute('d', path);
333
+ line.setAttribute('class', 'connection-line plan-to-subagent-line');
334
+ line.setAttribute('data-agent-id', agentId);
335
+ line.setAttribute('data-plan-agent-id', nearestPlanAgent.id);
336
+ svg.appendChild(line);
337
+ }
338
+ } else if (wizardOpen && wizardContent) {
339
+ // Wizard open but no plan subagents - connect directly to wizard
340
+ const winCenterX = winRect.left + winRect.width / 2;
341
+ const wizardCenterX = wizardRect.left + wizardRect.width / 2;
342
+
343
+ let x1, y1, x2, y2;
344
+
345
+ if (winCenterX < wizardCenterX) {
346
+ x1 = wizardRect.left;
347
+ y1 = wizardRect.top + wizardRect.height / 2;
348
+ x2 = winRect.right;
349
+ y2 = winRect.top + winRect.height / 2;
350
+ } else {
351
+ x1 = wizardRect.right;
352
+ y1 = wizardRect.top + wizardRect.height / 2;
353
+ x2 = winRect.left;
354
+ y2 = winRect.top + winRect.height / 2;
355
+ }
356
+
357
+ const midX = (x1 + x2) / 2;
358
+ const path = `M ${x1} ${y1} C ${midX} ${y1}, ${midX} ${y2}, ${x2} ${y2}`;
359
+
360
+ const line = document.createElementNS('http://www.w3.org/2000/svg', 'path');
361
+ line.setAttribute('d', path);
362
+ line.setAttribute('class', 'connection-line wizard-connection');
363
+ line.setAttribute('data-agent-id', agentId);
364
+ svg.appendChild(line);
365
+ } else {
366
+ // NORMAL MODE: Connect agent window to its parent TAB
367
+ // Use the PERSISTENT subagentParentMap as the ONLY source of truth
368
+ const parentSessionId = this.subagentParentMap.get(agentId);
369
+
370
+ if (!parentSessionId) {
371
+ // No parent known yet - skip this agent
372
+ continue;
373
+ }
374
+
375
+ const tabRect = rects.get('tab:' + parentSessionId);
376
+ if (!tabRect) {
377
+ // Tab not in DOM (might be scrolled out or session closed)
378
+ continue;
379
+ }
380
+
381
+ // Draw curved line from TAB bottom-center to window top-center
382
+ const x1 = tabRect.left + tabRect.width / 2;
383
+ const y1 = tabRect.bottom;
384
+ const x2 = winRect.left + winRect.width / 2;
385
+ const y2 = winRect.top;
386
+
387
+ // Bezier curve control points for smooth curve
388
+ const midY = (y1 + y2) / 2;
389
+ const path = `M ${x1} ${y1} C ${x1} ${midY}, ${x2} ${midY}, ${x2} ${y2}`;
390
+
391
+ const line = document.createElementNS('http://www.w3.org/2000/svg', 'path');
392
+ line.setAttribute('d', path);
393
+ line.setAttribute('class', 'connection-line');
394
+ line.setAttribute('data-agent-id', agentId);
395
+ line.setAttribute('data-parent-tab', parentSessionId);
396
+ svg.appendChild(line);
397
+ }
398
+ }
399
+
400
+ // Draw lines from wizard to plan subagent windows (Opus agents during plan generation)
401
+ // Skip if agents are minimized to tab
402
+ if (wizardOpen && wizardContent && this.planSubagents.size > 0 && !this.planAgentsMinimized) {
403
+ for (const [agentId] of this.planSubagents) {
404
+ const winRect = rects.get('planwin:' + agentId);
405
+ if (!winRect) continue;
406
+
407
+ // Determine which side of wizard the window is on
408
+ const winCenterX = winRect.left + winRect.width / 2;
409
+ const wizardCenterX = wizardRect.left + wizardRect.width / 2;
410
+
411
+ let x1, y1, x2, y2;
412
+
413
+ if (winCenterX < wizardCenterX) {
414
+ x1 = wizardRect.left;
415
+ y1 = wizardRect.top + wizardRect.height / 3 + (this.planSubagents.size > 3 ? 0 : 50);
416
+ x2 = winRect.right;
417
+ y2 = winRect.top + winRect.height / 2;
418
+ } else {
419
+ x1 = wizardRect.right;
420
+ y1 = wizardRect.top + wizardRect.height / 3 + (this.planSubagents.size > 3 ? 0 : 50);
421
+ x2 = winRect.left;
422
+ y2 = winRect.top + winRect.height / 2;
423
+ }
424
+
425
+ const midX = (x1 + x2) / 2;
426
+ const path = `M ${x1} ${y1} C ${midX} ${y1}, ${midX} ${y2}, ${x2} ${y2}`;
427
+
428
+ const line = document.createElementNS('http://www.w3.org/2000/svg', 'path');
429
+ line.setAttribute('d', path);
430
+ line.setAttribute('class', 'connection-line wizard-connection plan-subagent-line');
431
+ line.setAttribute('data-plan-agent-id', agentId);
432
+ svg.appendChild(line);
433
+ }
434
+ }
435
+ },
436
+
437
+ // ========== Subagent Floating Windows ==========
438
+
439
+ openSubagentWindow(agentId) {
440
+ // If window already exists, focus it
441
+ if (this.subagentWindows.has(agentId)) {
442
+ const existing = this.subagentWindows.get(agentId);
443
+ const agent = this.subagents.get(agentId);
444
+ const settings = this.loadAppSettingsFromStorage();
445
+ const activeTabOnly = settings.subagentActiveTabOnly ?? true;
446
+
447
+ // If window is hidden (different tab) and activeTabOnly is enabled, switch to parent tab
448
+ if (existing.hidden && agent?.parentSessionId && activeTabOnly) {
449
+ this.selectSession(agent.parentSessionId);
450
+ return;
451
+ }
452
+
453
+ // If not activeTabOnly mode, just show the window
454
+ if (existing.hidden && !activeTabOnly) {
455
+ existing.element.style.display = 'flex';
456
+ existing.hidden = false;
457
+ }
458
+
459
+ existing.element.style.zIndex = ++this.subagentWindowZIndex;
460
+ if (existing.minimized) {
461
+ this.restoreSubagentWindow(agentId);
462
+ }
463
+ return;
464
+ }
465
+
466
+ const agent = this.subagents.get(agentId);
467
+ if (!agent) return;
468
+
469
+ // Only open windows for agents that belong to a Codeman-managed session tab.
470
+ // Agents from external Claude sessions (not tracked by Codeman) should not pop up.
471
+ if (agent.sessionId) {
472
+ const hasMatchingTab = Array.from(this.sessions.values()).some(s => s.claudeSessionId === agent.sessionId);
473
+ if (!hasMatchingTab) return;
474
+ }
475
+
476
+ // Calculate final position - grid layout to avoid overlaps
477
+ const windowCount = this.subagentWindows.size;
478
+ const isMobile = MobileDetection.getDeviceType() === 'mobile';
479
+ const mobileCardHeight = 110;
480
+ const mobileCardGap = 4;
481
+ const windowWidth = isMobile ? window.innerWidth : 420;
482
+ const windowHeight = isMobile ? mobileCardHeight : 350;
483
+ const gap = 20;
484
+ const viewportWidth = window.innerWidth;
485
+ const viewportHeight = window.innerHeight;
486
+
487
+ let finalX = 0;
488
+ let finalY = 0;
489
+
490
+ if (isMobile) {
491
+ // Mobile: stack compact cards. Count visible (non-minimized) windows.
492
+ let visibleCount = 0;
493
+ for (const [, data] of this.subagentWindows) {
494
+ if (!data.minimized && !data.hidden) visibleCount++;
495
+ }
496
+ finalX = 4;
497
+ const keyboardUp = typeof KeyboardHandler !== 'undefined' && KeyboardHandler.keyboardVisible;
498
+ if (keyboardUp) {
499
+ // Keyboard visible: stack from bottom above toolbar
500
+ const toolbarHeight = 40;
501
+ const bottomOffset = toolbarHeight + visibleCount * (mobileCardHeight + mobileCardGap);
502
+ finalY = viewportHeight - bottomOffset - mobileCardHeight;
503
+ } else {
504
+ // Keyboard hidden: stack from top below header with spacing
505
+ const headerHeight = document.querySelector('.header')?.offsetHeight || 36;
506
+ const topStart = headerHeight + 8;
507
+ finalY = topStart + visibleCount * (mobileCardHeight + mobileCardGap);
508
+ }
509
+ } else {
510
+ // Check if Ralph wizard modal is open - if so, position windows on the sides
511
+ const wizardModal = document.getElementById('ralphWizardModal');
512
+ const wizardOpen = wizardModal?.classList.contains('active');
513
+
514
+ let startX, startY, maxCols;
515
+
516
+ if (wizardOpen) {
517
+ // Wizard is ~720px wide, centered. Position windows on left/right sides
518
+ const wizardWidth = 720;
519
+ const centerX = viewportWidth / 2;
520
+ const wizardLeft = centerX - wizardWidth / 2;
521
+ const wizardRight = centerX + wizardWidth / 2;
522
+
523
+ // Alternate between left and right sides of the wizard
524
+ const leftSideSpace = wizardLeft - 20;
525
+ const rightSideSpace = viewportWidth - wizardRight - 20;
526
+
527
+ if (windowCount % 2 === 0 && rightSideSpace >= windowWidth) {
528
+ // Even windows go to the right
529
+ startX = wizardRight + 20;
530
+ maxCols = Math.floor(rightSideSpace / (windowWidth + gap)) || 1;
531
+ } else if (leftSideSpace >= windowWidth) {
532
+ // Odd windows go to the left
533
+ startX = Math.max(10, wizardLeft - windowWidth - 20);
534
+ maxCols = 1; // Usually only room for 1 column on left
535
+ } else {
536
+ // Not enough side space, use right side
537
+ startX = wizardRight + 20;
538
+ maxCols = 1;
539
+ }
540
+ startY = 80; // Start higher when wizard is open
541
+ } else {
542
+ // Normal positioning
543
+ startX = 50;
544
+ startY = WINDOW_INITIAL_TOP_PX;
545
+ maxCols = Math.floor((viewportWidth - startX - 50) / (windowWidth + gap)) || 1;
546
+ }
547
+
548
+ const maxRows = Math.floor((viewportHeight - startY - 50) / (windowHeight + gap)) || 1;
549
+ const col = windowCount % maxCols;
550
+ const row = Math.floor(windowCount / maxCols) % maxRows; // Wrap rows to stay in viewport
551
+ finalX = startX + col * (windowWidth + gap);
552
+ finalY = startY + row * (windowHeight + gap);
553
+
554
+ // Ensure window stays within viewport bounds
555
+ finalX = Math.max(10, Math.min(finalX, viewportWidth - windowWidth - 10));
556
+ finalY = Math.max(10, Math.min(finalY, viewportHeight - windowHeight - 10));
557
+ }
558
+
559
+ // Get parent session from PERSISTENT map (THE source of truth for tab connections)
560
+ const parentSessionId = this.subagentParentMap.get(agentId) || agent.parentSessionId;
561
+ let parentSessionName = null;
562
+
563
+ if (parentSessionId) {
564
+ const parentSession = this.sessions.get(parentSessionId);
565
+ if (parentSession) {
566
+ parentSessionName = this.getSessionName(parentSession);
567
+ // Ensure the agent object is also updated for consistency
568
+ if (!agent.parentSessionId) {
569
+ agent.parentSessionId = parentSessionId;
570
+ agent.parentSessionName = parentSessionName;
571
+ this.subagents.set(agentId, agent);
572
+ }
573
+ }
574
+ }
575
+
576
+ // Get parent TAB element for spawn animation
577
+ const parentTab = parentSessionId
578
+ ? document.querySelector(`.session-tab[data-id="${parentSessionId}"]`)
579
+ : null;
580
+
581
+ // Create window element
582
+ const win = document.createElement('div');
583
+ win.className = 'subagent-window';
584
+ win.id = `subagent-window-${agentId}`;
585
+ win.style.zIndex = ++this.subagentWindowZIndex;
586
+
587
+ // Build parent header if we have parent info
588
+ const parentHeader = parentSessionId && parentSessionName
589
+ ? `<div class="subagent-window-parent" data-parent-session="${parentSessionId}">
590
+ <span class="parent-label">from</span>
591
+ <span class="parent-name" onclick="app.selectSession('${escapeHtml(parentSessionId)}')">${escapeHtml(parentSessionName)}</span>
592
+ </div>`
593
+ : '';
594
+
595
+ const teammateInfo = this.getTeammateInfo(agent);
596
+ const windowTitle = teammateInfo ? teammateInfo.name : (agent.description || agentId.substring(0, 7));
597
+ const maxTitleLen = isMobile ? 30 : 50;
598
+ const truncatedTitle = windowTitle.length > maxTitleLen ? windowTitle.substring(0, maxTitleLen) + '...' : windowTitle;
599
+ const modelBadge = agent.modelShort
600
+ ? `<span class="subagent-model-badge ${agent.modelShort}">${agent.modelShort}</span>`
601
+ : '';
602
+ win.innerHTML = `
603
+ <div class="subagent-window-header">
604
+ <div class="subagent-window-title" title="${escapeHtml(agent.description || agentId)}">
605
+ <span class="icon">🤖</span>
606
+ <span class="id">${escapeHtml(truncatedTitle)}</span>
607
+ ${modelBadge}
608
+ <span class="status ${agent.status}">${agent.status}</span>
609
+ </div>
610
+ <div class="subagent-window-actions">
611
+ <button onclick="app.closeSubagentWindow('${escapeHtml(agentId)}')" title="Minimize to tab">─</button>
612
+ </div>
613
+ </div>
614
+ ${parentHeader}
615
+ <div class="subagent-window-body" id="subagent-window-body-${agentId}">
616
+ <div class="subagent-empty">Loading activity...</div>
617
+ </div>
618
+ `;
619
+
620
+ // If we have a parent tab, start window at tab position for spawn animation
621
+ if (isMobile) {
622
+ // Mobile: position using top (keyboard-aware positioning calculated above)
623
+ win.style.top = `${finalY}px`;
624
+ win.style.bottom = 'auto';
625
+ } else if (parentTab) {
626
+ const tabRect = parentTab.getBoundingClientRect();
627
+ win.style.left = `${tabRect.left}px`;
628
+ win.style.top = `${tabRect.bottom}px`;
629
+ win.style.transform = 'scale(0.3)';
630
+ win.style.opacity = '0';
631
+ win.classList.add('spawning');
632
+ } else {
633
+ // No parent tab, just position normally (desktop/tablet)
634
+ win.style.left = `${finalX}px`;
635
+ win.style.top = `${finalY}px`;
636
+ }
637
+
638
+ document.body.appendChild(win);
639
+
640
+ // Make draggable (returns listener refs for cleanup)
641
+ const dragListeners = this.makeWindowDraggable(win, win.querySelector('.subagent-window-header'));
642
+
643
+ // Check if this window should be visible based on settings
644
+ // Use the PERSISTENT parent map for accurate tab-based visibility
645
+ const settings = this.loadAppSettingsFromStorage();
646
+ const activeTabOnly = settings.subagentActiveTabOnly ?? true;
647
+ let shouldHide = false;
648
+ if (activeTabOnly) {
649
+ const storedParent = this.subagentParentMap.get(agentId);
650
+ const hasKnownParent = storedParent || agent.parentSessionId;
651
+ const parentId = storedParent || agent.parentSessionId;
652
+ const isForActiveSession = !hasKnownParent || parentId === this.activeSessionId;
653
+ shouldHide = !isForActiveSession;
654
+ }
655
+
656
+ // Store reference (including drag listeners for cleanup)
657
+ this.subagentWindows.set(agentId, {
658
+ element: win,
659
+ minimized: false,
660
+ hidden: shouldHide,
661
+ dragListeners, // Store for cleanup to prevent memory leaks
662
+ });
663
+
664
+ // Hide window if not for active session
665
+ if (shouldHide) {
666
+ win.style.display = 'none';
667
+ }
668
+
669
+ // Render content — check if this teammate has a tmux pane
670
+ const paneInfo = teammateInfo ? this.teammatePanesByName.get(teammateInfo.name) : null;
671
+ if (paneInfo) {
672
+ this.initTeammateTerminal(agentId, paneInfo, win);
673
+ } else {
674
+ this.renderSubagentWindowContent(agentId);
675
+ }
676
+
677
+ // Focus on click
678
+ win.addEventListener('mousedown', () => {
679
+ win.style.zIndex = ++this.subagentWindowZIndex;
680
+ });
681
+
682
+ // Update connection lines when window is resized
683
+ const resizeObserver = new ResizeObserver(() => {
684
+ this.updateConnectionLines();
685
+ });
686
+ resizeObserver.observe(win);
687
+
688
+ // Store observer for cleanup
689
+ this.subagentWindows.get(agentId).resizeObserver = resizeObserver;
690
+
691
+ // Animate to final position if spawning from tab (desktop only)
692
+ if (parentTab && !isMobile) {
693
+ requestAnimationFrame(() => {
694
+ win.style.transition = 'all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1)';
695
+ win.style.left = `${finalX}px`;
696
+ win.style.top = `${finalY}px`;
697
+ win.style.transform = 'scale(1)';
698
+ win.style.opacity = '1';
699
+
700
+ // Clean up after animation
701
+ setTimeout(() => {
702
+ win.style.transition = '';
703
+ win.classList.remove('spawning');
704
+ this.updateConnectionLines();
705
+ }, 400);
706
+ });
707
+ } else {
708
+ // No animation (mobile uses CSS positioning), just update connection lines
709
+ this.updateConnectionLines();
710
+ }
711
+
712
+ // Persist the state change (new window opened)
713
+ this.saveSubagentWindowStates();
714
+ },
715
+
716
+ closeSubagentWindow(agentId) {
717
+ const windowData = this.subagentWindows.get(agentId);
718
+ if (!windowData) return;
719
+
720
+ const agent = this.subagents.get(agentId);
721
+
722
+ // Get parent from PERSISTENT map (THE source of truth)
723
+ // Fall back to agent's parentSessionId, then to active session
724
+ const storedParent = this.subagentParentMap.get(agentId);
725
+ let parentSessionId = storedParent || agent?.parentSessionId || this.activeSessionId;
726
+
727
+ // If we don't have a stored parent yet, store it now
728
+ if (!storedParent && parentSessionId && this.sessions.has(parentSessionId)) {
729
+ this.setAgentParentSessionId(agentId, parentSessionId);
730
+ }
731
+
732
+ // Always minimize to tab
733
+ windowData.element.style.display = 'none';
734
+ windowData.minimized = true;
735
+
736
+ // Track minimized agent for the session (use the TAB's session ID)
737
+ if (parentSessionId) {
738
+ if (!this.minimizedSubagents.has(parentSessionId)) {
739
+ this.minimizedSubagents.set(parentSessionId, new Set());
740
+ }
741
+ this.minimizedSubagents.get(parentSessionId).add(agentId);
742
+
743
+ // Update tab badge to show minimized agents
744
+ this.renderSessionTabs();
745
+ }
746
+
747
+ // Persist the state change
748
+ this.saveSubagentWindowStates();
749
+ this.updateConnectionLines();
750
+ // Restack remaining visible mobile windows to fill the gap
751
+ this.relayoutMobileSubagentWindows();
752
+ },
753
+
754
+ /** Reposition all visible mobile subagent windows (called on keyboard show/hide). */
755
+ relayoutMobileSubagentWindows() {
756
+ if (MobileDetection.getDeviceType() !== 'mobile') return;
757
+ const mobileCardHeight = 110;
758
+ const mobileCardGap = 4;
759
+ const keyboardUp = typeof KeyboardHandler !== 'undefined' && KeyboardHandler.keyboardVisible;
760
+ let idx = 0;
761
+ for (const [, data] of this.subagentWindows) {
762
+ if (data.minimized || data.hidden) continue;
763
+ const el = data.element;
764
+ // Reset left to proper position (drag may have set an arbitrary value)
765
+ el.style.left = '4px';
766
+ if (keyboardUp) {
767
+ // Stack from bottom above toolbar
768
+ const bottomPx = 40 + idx * (mobileCardHeight + mobileCardGap);
769
+ el.style.bottom = `${bottomPx}px`;
770
+ el.style.top = 'auto';
771
+ } else {
772
+ // Stack from top below header
773
+ const headerHeight = document.querySelector('.header')?.offsetHeight || 36;
774
+ const topPx = headerHeight + 8 + idx * (mobileCardHeight + mobileCardGap);
775
+ el.style.top = `${topPx}px`;
776
+ el.style.bottom = 'auto';
777
+ }
778
+ idx++;
779
+ }
780
+ },
781
+
782
+ // Clean up ALL floating windows (called during handleInit to prevent memory leaks on reconnect)
783
+ cleanupAllFloatingWindows() {
784
+ // Clean up all subagent windows with their ResizeObservers and drag listeners
785
+ for (const [agentId, windowData] of this.subagentWindows) {
786
+ if (windowData.resizeObserver) {
787
+ windowData.resizeObserver.disconnect();
788
+ }
789
+ if (windowData.dragListeners) {
790
+ document.removeEventListener('mousemove', windowData.dragListeners.move);
791
+ document.removeEventListener('mouseup', windowData.dragListeners.up);
792
+ if (windowData.dragListeners.touchMove) {
793
+ document.removeEventListener('touchmove', windowData.dragListeners.touchMove);
794
+ document.removeEventListener('touchend', windowData.dragListeners.up);
795
+ document.removeEventListener('touchcancel', windowData.dragListeners.up);
796
+ }
797
+ }
798
+ windowData.element.remove();
799
+ }
800
+ this.subagentWindows.clear();
801
+
802
+ // Clean up all teammate terminals
803
+ for (const [, termData] of this.teammateTerminals) {
804
+ if (termData.resizeObserver) termData.resizeObserver.disconnect();
805
+ if (termData.terminal) {
806
+ try { termData.terminal.dispose(); } catch {}
807
+ }
808
+ }
809
+ this.teammateTerminals.clear();
810
+ this.teammatePanesByName.clear();
811
+
812
+ // Clean up all log viewer windows with their EventSources and drag listeners
813
+ for (const [windowId, data] of this.logViewerWindows) {
814
+ if (data.eventSource) {
815
+ data.eventSource.close();
816
+ }
817
+ if (data.dragListeners) {
818
+ document.removeEventListener('mousemove', data.dragListeners.move);
819
+ document.removeEventListener('mouseup', data.dragListeners.up);
820
+ if (data.dragListeners.touchMove) {
821
+ document.removeEventListener('touchmove', data.dragListeners.touchMove);
822
+ document.removeEventListener('touchend', data.dragListeners.up);
823
+ document.removeEventListener('touchcancel', data.dragListeners.up);
824
+ }
825
+ }
826
+ data.element.remove();
827
+ }
828
+ this.logViewerWindows.clear();
829
+
830
+ // Clean up plan subagent windows (wizard agents)
831
+ if (this.planSubagents) {
832
+ for (const [agentId, windowData] of this.planSubagents) {
833
+ if (windowData.dragListeners) {
834
+ document.removeEventListener('mousemove', windowData.dragListeners.move);
835
+ document.removeEventListener('mouseup', windowData.dragListeners.up);
836
+ }
837
+ if (windowData.element) {
838
+ windowData.element.remove();
839
+ }
840
+ }
841
+ this.planSubagents.clear();
842
+ }
843
+
844
+ // Clean up all image popup windows with their drag listeners
845
+ for (const [imageId, popupData] of this.imagePopups) {
846
+ if (popupData.dragListeners) {
847
+ document.removeEventListener('mousemove', popupData.dragListeners.move);
848
+ document.removeEventListener('mouseup', popupData.dragListeners.up);
849
+ if (popupData.dragListeners.touchMove) {
850
+ document.removeEventListener('touchmove', popupData.dragListeners.touchMove);
851
+ document.removeEventListener('touchend', popupData.dragListeners.up);
852
+ document.removeEventListener('touchcancel', popupData.dragListeners.up);
853
+ }
854
+ }
855
+ popupData.element.remove();
856
+ }
857
+ this.imagePopups.clear();
858
+
859
+ // Clear orphaned plan generation state
860
+ this.activePlanOrchestratorId = null;
861
+ this._planProgressHandler = null;
862
+ this.planGenerationStopped = true;
863
+ if (this.planGenerationAbortController) {
864
+ this.planGenerationAbortController.abort();
865
+ this.planGenerationAbortController = null;
866
+ }
867
+
868
+ // Clean up wizard-specific timers (leak fix: not cleared on SSE reconnect)
869
+ if (this.wizardMinimizedTimer) {
870
+ clearInterval(this.wizardMinimizedTimer);
871
+ this.wizardMinimizedTimer = null;
872
+ }
873
+
874
+ // Clean up wizard drag listeners (leak fix: document-level handlers)
875
+ this.cleanupWizardDragging();
876
+
877
+ // Deactivate focus trap if wizard was open (leak fix: keydown listener)
878
+ if (this.activeFocusTrap) {
879
+ this.activeFocusTrap.deactivate();
880
+ this.activeFocusTrap = null;
881
+ }
882
+
883
+ // Clean up team tasks panel drag listeners
884
+ if (this.teamTasksDragListeners) {
885
+ document.removeEventListener('mousemove', this.teamTasksDragListeners.move);
886
+ document.removeEventListener('mouseup', this.teamTasksDragListeners.up);
887
+ if (this.teamTasksDragListeners.touchMove) {
888
+ document.removeEventListener('touchmove', this.teamTasksDragListeners.touchMove);
889
+ document.removeEventListener('touchend', this.teamTasksDragListeners.up);
890
+ document.removeEventListener('touchcancel', this.teamTasksDragListeners.up);
891
+ }
892
+ this.teamTasksDragListeners = null;
893
+ }
894
+
895
+ // Clear minimized agents tracking
896
+ this.minimizedSubagents.clear();
897
+
898
+ // Update monitor panel
899
+ this.renderMonitorPlanAgents();
900
+
901
+ // Update connection lines (should be empty now)
902
+ this.updateConnectionLines();
903
+ },
904
+
905
+ restoreSubagentWindow(agentId) {
906
+ const windowData = this.subagentWindows.get(agentId);
907
+ const agent = this.subagents.get(agentId);
908
+
909
+ // If window doesn't exist but agent does, recreate it
910
+ if (!windowData && agent) {
911
+ this.openSubagentWindow(agentId);
912
+ return;
913
+ }
914
+
915
+ if (windowData) {
916
+ const settings = this.loadAppSettingsFromStorage();
917
+ const activeTabOnly = settings.subagentActiveTabOnly ?? true;
918
+
919
+ // Get parent from PERSISTENT map (THE source of truth)
920
+ const storedParent = this.subagentParentMap.get(agentId);
921
+ const parentSessionId = storedParent || agent?.parentSessionId;
922
+
923
+ // Determine if we should show the window
924
+ let shouldShow = true;
925
+ if (activeTabOnly) {
926
+ // Only restore if the window belongs to the active session (or has no parent)
927
+ shouldShow = !parentSessionId || parentSessionId === this.activeSessionId;
928
+ }
929
+
930
+ if (shouldShow) {
931
+ windowData.element.style.display = 'flex';
932
+ windowData.element.style.zIndex = ++this.subagentWindowZIndex;
933
+ windowData.hidden = false;
934
+ }
935
+ windowData.minimized = false;
936
+ this.updateConnectionLines();
937
+ // Restack all visible mobile windows so restored ones don't overlap
938
+ this.relayoutMobileSubagentWindows();
939
+ }
940
+ },
941
+
942
+ // Returns drag listener references for cleanup (prevents memory leaks)
943
+ makeWindowDraggable(win, handle) {
944
+ let isDragging = false;
945
+ let startX, startY, startLeft, startTop;
946
+ let dragUpdateScheduled = false;
947
+
948
+ const startDrag = (clientX, clientY) => {
949
+ isDragging = true;
950
+ startX = clientX;
951
+ startY = clientY;
952
+ startLeft = parseInt(win.style.left) || win.getBoundingClientRect().left;
953
+ startTop = parseInt(win.style.top) || win.getBoundingClientRect().top;
954
+ // On drag start, switch from bottom-positioned to top-positioned so left/top work
955
+ win.style.bottom = 'auto';
956
+ };
957
+
958
+ const moveDrag = (clientX, clientY) => {
959
+ if (!isDragging) return;
960
+ const dx = clientX - startX;
961
+ const dy = clientY - startY;
962
+ // Constrain to viewport bounds
963
+ const winWidth = win.offsetWidth || 420;
964
+ const winHeight = win.offsetHeight || 350;
965
+ const maxX = window.innerWidth - winWidth - 4;
966
+ const maxY = window.innerHeight - winHeight - 4;
967
+ const newLeft = Math.max(4, Math.min(startLeft + dx, maxX));
968
+ const newTop = Math.max(4, Math.min(startTop + dy, maxY));
969
+ win.style.left = `${newLeft}px`;
970
+ win.style.top = `${newTop}px`;
971
+ // Throttle connection line updates during drag
972
+ if (!dragUpdateScheduled) {
973
+ dragUpdateScheduled = true;
974
+ requestAnimationFrame(() => {
975
+ this.updateConnectionLines();
976
+ dragUpdateScheduled = false;
977
+ });
978
+ }
979
+ };
980
+
981
+ const endDrag = () => {
982
+ if (isDragging) {
983
+ isDragging = false;
984
+ // Save position after drag ends
985
+ this.saveSubagentWindowStates();
986
+ }
987
+ };
988
+
989
+ // Mouse events
990
+ handle.addEventListener('mousedown', (e) => {
991
+ if (e.target.tagName === 'BUTTON') return;
992
+ startDrag(e.clientX, e.clientY);
993
+ e.preventDefault();
994
+ });
995
+
996
+ // Touch events
997
+ handle.addEventListener('touchstart', (e) => {
998
+ if (e.target.tagName === 'BUTTON') return;
999
+ const touch = e.touches[0];
1000
+ startDrag(touch.clientX, touch.clientY);
1001
+ }, { passive: true });
1002
+
1003
+ // Store references to document-level listeners so they can be removed on window close
1004
+ const moveListener = (e) => {
1005
+ moveDrag(e.clientX, e.clientY);
1006
+ };
1007
+
1008
+ const touchMoveListener = (e) => {
1009
+ if (!isDragging) return;
1010
+ e.preventDefault(); // Prevent page scroll while dragging
1011
+ const touch = e.touches[0];
1012
+ moveDrag(touch.clientX, touch.clientY);
1013
+ };
1014
+
1015
+ const upListener = () => {
1016
+ endDrag();
1017
+ };
1018
+
1019
+ document.addEventListener('mousemove', moveListener);
1020
+ document.addEventListener('mouseup', upListener);
1021
+ document.addEventListener('touchmove', touchMoveListener, { passive: false });
1022
+ document.addEventListener('touchend', upListener);
1023
+ document.addEventListener('touchcancel', upListener);
1024
+
1025
+ // Return listener references for cleanup
1026
+ return { move: moveListener, up: upListener, touchMove: touchMoveListener };
1027
+ },
1028
+
1029
+ // Show subagent dropdown on hover
1030
+ showSubagentDropdown(badgeEl) {
1031
+ this.cancelHideSubagentDropdown();
1032
+ const dropdown = badgeEl.querySelector('.subagent-dropdown');
1033
+ if (!dropdown || dropdown.classList.contains('open')) return;
1034
+
1035
+ // Close other dropdowns first
1036
+ document.querySelectorAll('.subagent-dropdown.open').forEach(d => {
1037
+ d.classList.remove('open', 'pinned');
1038
+ if (d.parentElement === document.body && d._originalParent) {
1039
+ d._originalParent.appendChild(d);
1040
+ }
1041
+ });
1042
+
1043
+ // Move to body to escape clipping
1044
+ dropdown._originalParent = badgeEl;
1045
+ document.body.appendChild(dropdown);
1046
+
1047
+ // Position below badge
1048
+ const rect = badgeEl.getBoundingClientRect();
1049
+ dropdown.style.top = `${rect.bottom + 2}px`;
1050
+ dropdown.style.left = `${rect.left + rect.width / 2}px`;
1051
+ dropdown.style.transform = 'translateX(-50%)';
1052
+ dropdown.classList.add('open');
1053
+ },
1054
+
1055
+ // Schedule hide after delay (allows moving mouse to dropdown)
1056
+ scheduleHideSubagentDropdown(badgeEl) {
1057
+ this._subagentHideTimeout = setTimeout(() => {
1058
+ const dropdown = badgeEl?.querySelector?.('.subagent-dropdown') ||
1059
+ document.querySelector('.subagent-dropdown.open');
1060
+ if (dropdown && !dropdown.classList.contains('pinned')) {
1061
+ dropdown.classList.remove('open');
1062
+ if (dropdown._originalParent) {
1063
+ dropdown._originalParent.appendChild(dropdown);
1064
+ }
1065
+ }
1066
+ }, 150);
1067
+ },
1068
+
1069
+ // Cancel scheduled hide
1070
+ cancelHideSubagentDropdown() {
1071
+ if (this._subagentHideTimeout) {
1072
+ clearTimeout(this._subagentHideTimeout);
1073
+ this._subagentHideTimeout = null;
1074
+ }
1075
+ },
1076
+
1077
+ // Pin dropdown open on click (stays until clicking outside)
1078
+ pinSubagentDropdown(badgeEl) {
1079
+ const dropdown = document.querySelector('.subagent-dropdown.open');
1080
+ if (!dropdown) {
1081
+ this.showSubagentDropdown(badgeEl);
1082
+ // On mobile/touch, pin immediately so onmouseleave doesn't close it
1083
+ const openedDropdown = document.querySelector('.subagent-dropdown.open');
1084
+ if (openedDropdown) {
1085
+ openedDropdown.classList.add('pinned');
1086
+ const closeHandler = (e) => {
1087
+ if (!badgeEl.contains(e.target) && !openedDropdown.contains(e.target)) {
1088
+ openedDropdown.classList.remove('open', 'pinned');
1089
+ if (openedDropdown._originalParent) {
1090
+ openedDropdown._originalParent.appendChild(openedDropdown);
1091
+ }
1092
+ document.removeEventListener('click', closeHandler);
1093
+ }
1094
+ };
1095
+ setTimeout(() => document.addEventListener('click', closeHandler), 0);
1096
+ }
1097
+ return;
1098
+ }
1099
+ dropdown.classList.toggle('pinned');
1100
+
1101
+ if (dropdown.classList.contains('pinned')) {
1102
+ // Close on outside click
1103
+ const closeHandler = (e) => {
1104
+ if (!badgeEl.contains(e.target) && !dropdown.contains(e.target)) {
1105
+ dropdown.classList.remove('open', 'pinned');
1106
+ if (dropdown._originalParent) {
1107
+ dropdown._originalParent.appendChild(dropdown);
1108
+ }
1109
+ document.removeEventListener('click', closeHandler);
1110
+ }
1111
+ };
1112
+ setTimeout(() => document.addEventListener('click', closeHandler), 0);
1113
+ }
1114
+ },
1115
+ });