@gokulvenkatareddy/cortex 0.1.7

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 (299) hide show
  1. package/README.md +1295 -0
  2. package/apps/octogent/.github/workflows/ci.yml +40 -0
  3. package/apps/octogent/.shims/claude +4 -0
  4. package/apps/octogent/AGENTS.md +71 -0
  5. package/apps/octogent/CONTRIBUTING.md +72 -0
  6. package/apps/octogent/LICENSE +21 -0
  7. package/apps/octogent/README.md +184 -0
  8. package/apps/octogent/apps/api/AGENTS.md +32 -0
  9. package/apps/octogent/apps/api/package.json +19 -0
  10. package/apps/octogent/apps/api/src/agentStateDetection.ts +181 -0
  11. package/apps/octogent/apps/api/src/claudeSessionScanner.ts +235 -0
  12. package/apps/octogent/apps/api/src/claudeSkills.ts +182 -0
  13. package/apps/octogent/apps/api/src/claudeUsage.ts +922 -0
  14. package/apps/octogent/apps/api/src/cli.ts +595 -0
  15. package/apps/octogent/apps/api/src/codeIntelStore.ts +46 -0
  16. package/apps/octogent/apps/api/src/codexUsage.ts +278 -0
  17. package/apps/octogent/apps/api/src/createApiServer/codeIntelRoutes.ts +60 -0
  18. package/apps/octogent/apps/api/src/createApiServer/conversationRoutes.ts +128 -0
  19. package/apps/octogent/apps/api/src/createApiServer/deckRoutes.ts +873 -0
  20. package/apps/octogent/apps/api/src/createApiServer/gitParsers.ts +140 -0
  21. package/apps/octogent/apps/api/src/createApiServer/gitRoutes.ts +214 -0
  22. package/apps/octogent/apps/api/src/createApiServer/miscRoutes.ts +316 -0
  23. package/apps/octogent/apps/api/src/createApiServer/monitorParsers.ts +137 -0
  24. package/apps/octogent/apps/api/src/createApiServer/monitorRoutes.ts +95 -0
  25. package/apps/octogent/apps/api/src/createApiServer/requestHandler.ts +311 -0
  26. package/apps/octogent/apps/api/src/createApiServer/requestParsers.ts +25 -0
  27. package/apps/octogent/apps/api/src/createApiServer/routeHelpers.ts +97 -0
  28. package/apps/octogent/apps/api/src/createApiServer/security.ts +70 -0
  29. package/apps/octogent/apps/api/src/createApiServer/terminalParsers.ts +167 -0
  30. package/apps/octogent/apps/api/src/createApiServer/terminalRoutes.ts +315 -0
  31. package/apps/octogent/apps/api/src/createApiServer/types.ts +24 -0
  32. package/apps/octogent/apps/api/src/createApiServer/uiStateParsers.ts +255 -0
  33. package/apps/octogent/apps/api/src/createApiServer/upgradeHandler.ts +38 -0
  34. package/apps/octogent/apps/api/src/createApiServer/usageRoutes.ts +84 -0
  35. package/apps/octogent/apps/api/src/createApiServer.ts +176 -0
  36. package/apps/octogent/apps/api/src/deck/readDeckTentacles.ts +595 -0
  37. package/apps/octogent/apps/api/src/githubRepoSummary.ts +397 -0
  38. package/apps/octogent/apps/api/src/logging.ts +9 -0
  39. package/apps/octogent/apps/api/src/monitor/defaults.ts +3 -0
  40. package/apps/octogent/apps/api/src/monitor/index.ts +8 -0
  41. package/apps/octogent/apps/api/src/monitor/repository.ts +303 -0
  42. package/apps/octogent/apps/api/src/monitor/service.ts +349 -0
  43. package/apps/octogent/apps/api/src/monitor/types.ts +120 -0
  44. package/apps/octogent/apps/api/src/monitor/xProvider.ts +587 -0
  45. package/apps/octogent/apps/api/src/projectPersistence.ts +377 -0
  46. package/apps/octogent/apps/api/src/prompts/index.ts +10 -0
  47. package/apps/octogent/apps/api/src/prompts/promptResolver.ts +145 -0
  48. package/apps/octogent/apps/api/src/runtimeMetadata.ts +69 -0
  49. package/apps/octogent/apps/api/src/server.ts +80 -0
  50. package/apps/octogent/apps/api/src/setupState.ts +80 -0
  51. package/apps/octogent/apps/api/src/setupStatus.ts +174 -0
  52. package/apps/octogent/apps/api/src/startupPrerequisites.ts +146 -0
  53. package/apps/octogent/apps/api/src/terminalRuntime/channelMessaging.ts +87 -0
  54. package/apps/octogent/apps/api/src/terminalRuntime/claudeTranscript.ts +279 -0
  55. package/apps/octogent/apps/api/src/terminalRuntime/constants.ts +15 -0
  56. package/apps/octogent/apps/api/src/terminalRuntime/conversations.ts +492 -0
  57. package/apps/octogent/apps/api/src/terminalRuntime/gitOperations.ts +341 -0
  58. package/apps/octogent/apps/api/src/terminalRuntime/hookProcessor.ts +405 -0
  59. package/apps/octogent/apps/api/src/terminalRuntime/protocol.ts +46 -0
  60. package/apps/octogent/apps/api/src/terminalRuntime/ptyEnvironment.ts +50 -0
  61. package/apps/octogent/apps/api/src/terminalRuntime/registry.ts +423 -0
  62. package/apps/octogent/apps/api/src/terminalRuntime/sessionRuntime.ts +671 -0
  63. package/apps/octogent/apps/api/src/terminalRuntime/systemClients.ts +432 -0
  64. package/apps/octogent/apps/api/src/terminalRuntime/types.ts +157 -0
  65. package/apps/octogent/apps/api/src/terminalRuntime/worktreeManager.ts +135 -0
  66. package/apps/octogent/apps/api/src/terminalRuntime.ts +567 -0
  67. package/apps/octogent/apps/api/src/usageUtils.ts +16 -0
  68. package/apps/octogent/apps/api/src/ws-shim.d.ts +28 -0
  69. package/apps/octogent/apps/api/tests/agentStateDetection.test.ts +67 -0
  70. package/apps/octogent/apps/api/tests/claudeUsage.test.ts +583 -0
  71. package/apps/octogent/apps/api/tests/codexUsage.test.ts +107 -0
  72. package/apps/octogent/apps/api/tests/createApiServer.test.ts +3207 -0
  73. package/apps/octogent/apps/api/tests/githubRepoSummary.test.ts +100 -0
  74. package/apps/octogent/apps/api/tests/logging.test.ts +33 -0
  75. package/apps/octogent/apps/api/tests/monitorApi.test.ts +467 -0
  76. package/apps/octogent/apps/api/tests/monitorCore.test.ts +104 -0
  77. package/apps/octogent/apps/api/tests/promptResolver.test.ts +109 -0
  78. package/apps/octogent/apps/api/tests/protocol.test.ts +14 -0
  79. package/apps/octogent/apps/api/tests/sessionRuntime.test.ts +608 -0
  80. package/apps/octogent/apps/api/tests/startupPrerequisites.test.ts +70 -0
  81. package/apps/octogent/apps/api/tests/upgradeHandler.test.ts +40 -0
  82. package/apps/octogent/apps/api/tests/xMonitorProvider.test.ts +109 -0
  83. package/apps/octogent/apps/api/tsconfig.json +7 -0
  84. package/apps/octogent/apps/api/vitest.config.ts +7 -0
  85. package/apps/octogent/apps/web/AGENTS.md +38 -0
  86. package/apps/octogent/apps/web/index.html +13 -0
  87. package/apps/octogent/apps/web/package.json +32 -0
  88. package/apps/octogent/apps/web/public/octopus-favicon.svg +26 -0
  89. package/apps/octogent/apps/web/src/App.tsx +646 -0
  90. package/apps/octogent/apps/web/src/app/canvas/types.ts +34 -0
  91. package/apps/octogent/apps/web/src/app/codeIntelAggregation.ts +278 -0
  92. package/apps/octogent/apps/web/src/app/constants.ts +28 -0
  93. package/apps/octogent/apps/web/src/app/conversationNormalizers.ts +135 -0
  94. package/apps/octogent/apps/web/src/app/formatTimestamp.ts +18 -0
  95. package/apps/octogent/apps/web/src/app/githubMetrics.ts +76 -0
  96. package/apps/octogent/apps/web/src/app/githubNormalizers.ts +91 -0
  97. package/apps/octogent/apps/web/src/app/hooks/useAgentRuntimeStates.ts +18 -0
  98. package/apps/octogent/apps/web/src/app/hooks/useBackendLivenessPolling.ts +53 -0
  99. package/apps/octogent/apps/web/src/app/hooks/useCanvasGraphData.ts +449 -0
  100. package/apps/octogent/apps/web/src/app/hooks/useCanvasTransform.ts +260 -0
  101. package/apps/octogent/apps/web/src/app/hooks/useClaudeUsagePolling.ts +40 -0
  102. package/apps/octogent/apps/web/src/app/hooks/useClickOutside.ts +30 -0
  103. package/apps/octogent/apps/web/src/app/hooks/useCodeIntelRuntime.ts +83 -0
  104. package/apps/octogent/apps/web/src/app/hooks/useCodexUsagePolling.ts +35 -0
  105. package/apps/octogent/apps/web/src/app/hooks/useConsoleKeyboardShortcuts.ts +31 -0
  106. package/apps/octogent/apps/web/src/app/hooks/useConversationsRuntime.ts +377 -0
  107. package/apps/octogent/apps/web/src/app/hooks/useForceSimulation.ts +319 -0
  108. package/apps/octogent/apps/web/src/app/hooks/useGitHubPrimaryViewModel.ts +143 -0
  109. package/apps/octogent/apps/web/src/app/hooks/useGithubSummaryPolling.ts +28 -0
  110. package/apps/octogent/apps/web/src/app/hooks/useInitialColumnsHydration.ts +64 -0
  111. package/apps/octogent/apps/web/src/app/hooks/useMonitorRuntime.ts +220 -0
  112. package/apps/octogent/apps/web/src/app/hooks/usePersistedUiState.ts +536 -0
  113. package/apps/octogent/apps/web/src/app/hooks/usePollingData.ts +79 -0
  114. package/apps/octogent/apps/web/src/app/hooks/usePromptLibrary.ts +185 -0
  115. package/apps/octogent/apps/web/src/app/hooks/useTentacleGitLifecycle.ts +530 -0
  116. package/apps/octogent/apps/web/src/app/hooks/useTerminalCompletionNotification.ts +94 -0
  117. package/apps/octogent/apps/web/src/app/hooks/useTerminalMutations.ts +266 -0
  118. package/apps/octogent/apps/web/src/app/hooks/useTerminalStateReconciliation.ts +23 -0
  119. package/apps/octogent/apps/web/src/app/hooks/useUsageHeatmapPolling.ts +43 -0
  120. package/apps/octogent/apps/web/src/app/hooks/useWorkspaceSetup.ts +80 -0
  121. package/apps/octogent/apps/web/src/app/hotkeys.ts +31 -0
  122. package/apps/octogent/apps/web/src/app/monitorNormalizers.ts +145 -0
  123. package/apps/octogent/apps/web/src/app/notificationSounds.ts +164 -0
  124. package/apps/octogent/apps/web/src/app/terminalRuntimeStateStore.ts +261 -0
  125. package/apps/octogent/apps/web/src/app/terminalState.ts +21 -0
  126. package/apps/octogent/apps/web/src/app/types.ts +42 -0
  127. package/apps/octogent/apps/web/src/app/uiStateNormalizers.ts +113 -0
  128. package/apps/octogent/apps/web/src/app/usageNormalizers.ts +58 -0
  129. package/apps/octogent/apps/web/src/components/ActiveAgentsSidebar.tsx +60 -0
  130. package/apps/octogent/apps/web/src/components/ActivityPrimaryView.tsx +21 -0
  131. package/apps/octogent/apps/web/src/components/AgentStateBadge.tsx +47 -0
  132. package/apps/octogent/apps/web/src/components/CanvasPrimaryView.tsx +1532 -0
  133. package/apps/octogent/apps/web/src/components/ClearAllConversationsDialog.tsx +33 -0
  134. package/apps/octogent/apps/web/src/components/CodeIntelArcDiagram.tsx +245 -0
  135. package/apps/octogent/apps/web/src/components/CodeIntelPrimaryView.tsx +104 -0
  136. package/apps/octogent/apps/web/src/components/CodeIntelTreemap.tsx +138 -0
  137. package/apps/octogent/apps/web/src/components/ConsolePrimaryNav.tsx +31 -0
  138. package/apps/octogent/apps/web/src/components/ConversationsPrimaryView.tsx +243 -0
  139. package/apps/octogent/apps/web/src/components/DeckPrimaryView.tsx +613 -0
  140. package/apps/octogent/apps/web/src/components/DeleteTentacleDialog.tsx +91 -0
  141. package/apps/octogent/apps/web/src/components/EmptyOctopus.tsx +715 -0
  142. package/apps/octogent/apps/web/src/components/GitHubPrimaryView.tsx +494 -0
  143. package/apps/octogent/apps/web/src/components/MonitorPrimaryView.tsx +475 -0
  144. package/apps/octogent/apps/web/src/components/PrimaryViewRouter.tsx +99 -0
  145. package/apps/octogent/apps/web/src/components/PromptsPrimaryView.tsx +243 -0
  146. package/apps/octogent/apps/web/src/components/RuntimeStatusStrip.tsx +273 -0
  147. package/apps/octogent/apps/web/src/components/SettingsPrimaryView.tsx +92 -0
  148. package/apps/octogent/apps/web/src/components/SidebarActionPanel.tsx +124 -0
  149. package/apps/octogent/apps/web/src/components/SidebarConversationsList.tsx +279 -0
  150. package/apps/octogent/apps/web/src/components/SidebarPromptsList.tsx +116 -0
  151. package/apps/octogent/apps/web/src/components/TelemetryTape.tsx +106 -0
  152. package/apps/octogent/apps/web/src/components/TentacleGitActionsDialog.tsx +341 -0
  153. package/apps/octogent/apps/web/src/components/Terminal.tsx +524 -0
  154. package/apps/octogent/apps/web/src/components/TerminalPromptPicker.tsx +140 -0
  155. package/apps/octogent/apps/web/src/components/UsageHeatmap.tsx +702 -0
  156. package/apps/octogent/apps/web/src/components/canvas/CanvasTentaclePanel.tsx +485 -0
  157. package/apps/octogent/apps/web/src/components/canvas/CanvasTerminalColumn.tsx +89 -0
  158. package/apps/octogent/apps/web/src/components/canvas/DeleteAllTerminalsDialog.tsx +221 -0
  159. package/apps/octogent/apps/web/src/components/canvas/OctopusNode.tsx +307 -0
  160. package/apps/octogent/apps/web/src/components/canvas/SessionNode.tsx +185 -0
  161. package/apps/octogent/apps/web/src/components/deck/ActionCards.tsx +118 -0
  162. package/apps/octogent/apps/web/src/components/deck/AddTentacleForm.tsx +269 -0
  163. package/apps/octogent/apps/web/src/components/deck/DeckBottomActions.tsx +56 -0
  164. package/apps/octogent/apps/web/src/components/deck/TentaclePod.tsx +334 -0
  165. package/apps/octogent/apps/web/src/components/deck/WorkspaceSetupCard.tsx +105 -0
  166. package/apps/octogent/apps/web/src/components/deck/octopusVisuals.ts +72 -0
  167. package/apps/octogent/apps/web/src/components/terminalReplay.ts +62 -0
  168. package/apps/octogent/apps/web/src/components/terminalWheel.ts +54 -0
  169. package/apps/octogent/apps/web/src/components/ui/ActionButton.tsx +34 -0
  170. package/apps/octogent/apps/web/src/components/ui/ConfirmationDialog.tsx +86 -0
  171. package/apps/octogent/apps/web/src/components/ui/MarkdownContent.tsx +43 -0
  172. package/apps/octogent/apps/web/src/components/ui/SettingsToggle.tsx +34 -0
  173. package/apps/octogent/apps/web/src/components/ui/StatusBadge.tsx +24 -0
  174. package/apps/octogent/apps/web/src/main.tsx +17 -0
  175. package/apps/octogent/apps/web/src/runtime/HttpTerminalSnapshotReader.ts +87 -0
  176. package/apps/octogent/apps/web/src/runtime/runtimeEndpoints.ts +412 -0
  177. package/apps/octogent/apps/web/src/styles/chrome-and-buttons.css +272 -0
  178. package/apps/octogent/apps/web/src/styles/console-canvas-activity.css +358 -0
  179. package/apps/octogent/apps/web/src/styles/console-canvas-canvas.css +1843 -0
  180. package/apps/octogent/apps/web/src/styles/console-canvas-code-intel.css +227 -0
  181. package/apps/octogent/apps/web/src/styles/console-canvas-conversations.css +705 -0
  182. package/apps/octogent/apps/web/src/styles/console-canvas-deck.css +1524 -0
  183. package/apps/octogent/apps/web/src/styles/console-canvas-github.css +541 -0
  184. package/apps/octogent/apps/web/src/styles/console-canvas-monitor.css +595 -0
  185. package/apps/octogent/apps/web/src/styles/console-canvas-pixpack.css +81 -0
  186. package/apps/octogent/apps/web/src/styles/console-canvas-prompts.css +474 -0
  187. package/apps/octogent/apps/web/src/styles/console-canvas-settings.css +207 -0
  188. package/apps/octogent/apps/web/src/styles/console-chrome-status-nav.css +441 -0
  189. package/apps/octogent/apps/web/src/styles/console-overrides-telemetry.css +320 -0
  190. package/apps/octogent/apps/web/src/styles/console-theme-tokens.css +25 -0
  191. package/apps/octogent/apps/web/src/styles/cortex-theme.css +412 -0
  192. package/apps/octogent/apps/web/src/styles/foundation.css +100 -0
  193. package/apps/octogent/apps/web/src/styles/sidebar-and-scrollbars.css +447 -0
  194. package/apps/octogent/apps/web/src/styles/terminal-and-status.css +356 -0
  195. package/apps/octogent/apps/web/src/styles.css +25 -0
  196. package/apps/octogent/apps/web/src/types/ws.d.ts +23 -0
  197. package/apps/octogent/apps/web/tests/CanvasPrimaryView.test.tsx +347 -0
  198. package/apps/octogent/apps/web/tests/HttpTerminalSnapshotReader.test.tsx +54 -0
  199. package/apps/octogent/apps/web/tests/RuntimeStatusStrip.test.tsx +70 -0
  200. package/apps/octogent/apps/web/tests/Terminal.test.tsx +87 -0
  201. package/apps/octogent/apps/web/tests/add-tentacle-form.test.tsx +48 -0
  202. package/apps/octogent/apps/web/tests/app-github-runtime.test.tsx +162 -0
  203. package/apps/octogent/apps/web/tests/app-monitor-runtime.test.tsx +657 -0
  204. package/apps/octogent/apps/web/tests/app-shell-navigation.test.tsx +109 -0
  205. package/apps/octogent/apps/web/tests/app-swarm-refresh.test.tsx +268 -0
  206. package/apps/octogent/apps/web/tests/app-ui-state-persistence.test.tsx +116 -0
  207. package/apps/octogent/apps/web/tests/app-workspace-setup.test.tsx +217 -0
  208. package/apps/octogent/apps/web/tests/canvas-tentacle-panel.test.tsx +195 -0
  209. package/apps/octogent/apps/web/tests/delete-all-terminals-dialog.test.tsx +76 -0
  210. package/apps/octogent/apps/web/tests/githubMetrics.test.tsx +52 -0
  211. package/apps/octogent/apps/web/tests/hotkeys.test.tsx +44 -0
  212. package/apps/octogent/apps/web/tests/runtimeEndpoints.test.tsx +240 -0
  213. package/apps/octogent/apps/web/tests/setup.ts +39 -0
  214. package/apps/octogent/apps/web/tests/tentacle-pod.test.tsx +62 -0
  215. package/apps/octogent/apps/web/tests/terminalReplay.test.ts +71 -0
  216. package/apps/octogent/apps/web/tests/terminalState.test.tsx +49 -0
  217. package/apps/octogent/apps/web/tests/terminalWheel.test.tsx +51 -0
  218. package/apps/octogent/apps/web/tests/test-utils/appTestHarness.ts +48 -0
  219. package/apps/octogent/apps/web/tests/uiPrimitives.test.tsx +31 -0
  220. package/apps/octogent/apps/web/tests/useAgentRuntimeStates.test.tsx +47 -0
  221. package/apps/octogent/apps/web/tsconfig.json +8 -0
  222. package/apps/octogent/apps/web/vite.api.bundle.config.mts +32 -0
  223. package/apps/octogent/apps/web/vite.config.ts +22 -0
  224. package/apps/octogent/bin/octogent +3 -0
  225. package/apps/octogent/biome.json +21 -0
  226. package/apps/octogent/docs/concepts/mental-model.md +79 -0
  227. package/apps/octogent/docs/concepts/runtime-and-api.md +60 -0
  228. package/apps/octogent/docs/concepts/tentacles.md +85 -0
  229. package/apps/octogent/docs/getting-started/installation.md +54 -0
  230. package/apps/octogent/docs/getting-started/quickstart.md +79 -0
  231. package/apps/octogent/docs/guides/inter-agent-messaging.md +43 -0
  232. package/apps/octogent/docs/guides/orchestrating-child-agents.md +49 -0
  233. package/apps/octogent/docs/guides/working-with-todos.md +56 -0
  234. package/apps/octogent/docs/index.md +40 -0
  235. package/apps/octogent/docs/reference/api.md +103 -0
  236. package/apps/octogent/docs/reference/cli.md +71 -0
  237. package/apps/octogent/docs/reference/experimental-features.md +28 -0
  238. package/apps/octogent/docs/reference/filesystem-layout.md +62 -0
  239. package/apps/octogent/docs/reference/troubleshooting.md +49 -0
  240. package/apps/octogent/package.json +35 -0
  241. package/apps/octogent/packages/core/AGENTS.md +31 -0
  242. package/apps/octogent/packages/core/package.json +12 -0
  243. package/apps/octogent/packages/core/src/adapters/InMemoryTerminalSnapshotReader.ts +10 -0
  244. package/apps/octogent/packages/core/src/application/buildTerminalList.ts +13 -0
  245. package/apps/octogent/packages/core/src/domain/agentRuntime.ts +18 -0
  246. package/apps/octogent/packages/core/src/domain/channel.ts +8 -0
  247. package/apps/octogent/packages/core/src/domain/completionSound.ts +14 -0
  248. package/apps/octogent/packages/core/src/domain/conversation.ts +48 -0
  249. package/apps/octogent/packages/core/src/domain/deck.ts +33 -0
  250. package/apps/octogent/packages/core/src/domain/git.ts +32 -0
  251. package/apps/octogent/packages/core/src/domain/monitor.ts +62 -0
  252. package/apps/octogent/packages/core/src/domain/setup.ts +27 -0
  253. package/apps/octogent/packages/core/src/domain/terminal.ts +17 -0
  254. package/apps/octogent/packages/core/src/domain/uiState.ts +22 -0
  255. package/apps/octogent/packages/core/src/domain/usage.ts +60 -0
  256. package/apps/octogent/packages/core/src/index.ts +15 -0
  257. package/apps/octogent/packages/core/src/ports/TerminalSnapshotReader.ts +5 -0
  258. package/apps/octogent/packages/core/src/util/typeCoercion.ts +20 -0
  259. package/apps/octogent/packages/core/tests/buildTerminalList.test.ts +75 -0
  260. package/apps/octogent/packages/core/tsconfig.json +7 -0
  261. package/apps/octogent/packages/core/tsconfig.tsbuildinfo +1 -0
  262. package/apps/octogent/packages/core/vitest.config.ts +7 -0
  263. package/apps/octogent/pnpm-lock.yaml +3212 -0
  264. package/apps/octogent/pnpm-workspace.yaml +3 -0
  265. package/apps/octogent/prompts/meta-prompt-generator.md +223 -0
  266. package/apps/octogent/prompts/octoboss-clean-contexts.md +30 -0
  267. package/apps/octogent/prompts/octoboss-reorganize-tentacles.md +29 -0
  268. package/apps/octogent/prompts/octoboss-reorganize-todos.md +27 -0
  269. package/apps/octogent/prompts/sandbox-init.md +3 -0
  270. package/apps/octogent/prompts/swarm-parent.md +83 -0
  271. package/apps/octogent/prompts/swarm-worker.md +50 -0
  272. package/apps/octogent/prompts/tentacle-context-init.md +1 -0
  273. package/apps/octogent/prompts/tentacle-planner.md +110 -0
  274. package/apps/octogent/prompts/tentacle-reorganize-todos.md +20 -0
  275. package/apps/octogent/prompts/tentacle-update-tentacle.md +18 -0
  276. package/apps/octogent/scripts/build-package.mjs +23 -0
  277. package/apps/octogent/scripts/dev.mjs +158 -0
  278. package/apps/octogent/scripts/smoke-public-install.mjs +271 -0
  279. package/apps/octogent/static/images/octogent-header.png +0 -0
  280. package/apps/octogent/static/images/preview_1.jpg +0 -0
  281. package/apps/octogent/static/images/preview_2.jpg +0 -0
  282. package/apps/octogent/static/images/preview_3.jpg +0 -0
  283. package/apps/octogent/static/images/preview_4.jpg +0 -0
  284. package/apps/octogent/static/images/preview_5.jpg +0 -0
  285. package/apps/octogent/static/images/preview_6.jpg +0 -0
  286. package/apps/octogent/tsconfig.base.json +16 -0
  287. package/bin/AGI +3 -0
  288. package/bin/AGI-install-app +71 -0
  289. package/bin/AGI-ui +16 -0
  290. package/bin/AGI-voice +15 -0
  291. package/bin/AGI-web +16 -0
  292. package/bin/cortex +109 -0
  293. package/bin/cortex-octogent +99 -0
  294. package/bin/import-specifier.mjs +13 -0
  295. package/bin/import-specifier.test.mjs +13 -0
  296. package/bin/octo +150 -0
  297. package/dist/cli.mjs +555650 -0
  298. package/package.json +157 -0
  299. package/scripts/setup-wizard.ts +390 -0
@@ -0,0 +1,40 @@
1
+ name: CI
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches:
7
+ - main
8
+
9
+ jobs:
10
+ quality:
11
+ name: Lint, Test, Build
12
+ runs-on: ubuntu-latest
13
+ timeout-minutes: 20
14
+
15
+ steps:
16
+ - name: Checkout
17
+ uses: actions/checkout@v4
18
+
19
+ - name: Setup pnpm
20
+ uses: pnpm/action-setup@v4
21
+ with:
22
+ version: 10.4.1
23
+
24
+ - name: Setup Node.js
25
+ uses: actions/setup-node@v4
26
+ with:
27
+ node-version: 22
28
+ cache: pnpm
29
+
30
+ - name: Install dependencies
31
+ run: pnpm install --frozen-lockfile
32
+
33
+ - name: Lint
34
+ run: pnpm lint
35
+
36
+ - name: Test
37
+ run: pnpm test
38
+
39
+ - name: Build
40
+ run: pnpm build
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env bash
2
+ # Octogent expects `claude` as the agent CLI. We route it to CORTEX.
3
+ # CORTEX_REPO_ROOT is set by bin/cortex-octogent before launch.
4
+ exec node "${CORTEX_REPO_ROOT:-/Users/gokul/Documents/openclaude}/cortex.mjs" "$@"
@@ -0,0 +1,71 @@
1
+ # Repository Guidelines
2
+
3
+ ## Code Style First
4
+ - Preserve existing patterns before inventing new ones. Read nearby code and match the established module shape, naming, and data flow unless there is a clear reason to change direction.
5
+ - Start with tests. For bug fixes, reproduce first. For new features, add the test that defines the behavior before expanding the implementation.
6
+ - Implement incrementally. Prefer small, working steps over broad rewrites. Keep the tree passing as you go.
7
+ - Think in systems. Extract shared behavior into reusable components, hooks, utilities, or domain functions instead of cloning slightly different versions.
8
+ - Keep modules focused. Large React containers should orchestrate, not hold every constant, parser, and JSX block. Keep pure logic in `src/app/*`, UI in `src/components/*`, and CSS split into focused files under `src/styles/*`.
9
+ - Comments explain why, not what. Add comments only for constraints, tradeoffs, or non-obvious reasoning.
10
+ - Design defensively. Validate assumptions, handle edge cases, and treat security boundaries as part of the implementation, not a follow-up.
11
+
12
+ ## Project Structure
13
+ - Monorepo: `apps/*` and `packages/*` via `pnpm-workspace.yaml`.
14
+ - Runtime: Node.js 22+, TypeScript, `pnpm`.
15
+ - Core package: `packages/core`
16
+ - Framework-agnostic domain types, application logic, and ports.
17
+ - Must stay free of React, HTTP, PTY, and filesystem orchestration concerns.
18
+ - API app: `apps/api`
19
+ - Node HTTP/WebSocket server, PTY session runtime, worktree lifecycle, transcript persistence, monitor service.
20
+ - Web app: `apps/web`
21
+ - Vite + React operator UI, modular CSS, UI orchestration over API/runtime contracts.
22
+ - Runtime state: `.octogent/`
23
+ - `state/tentacles.json`
24
+ - `state/transcripts/*.jsonl`
25
+ - `worktrees/<tentacleId>`
26
+
27
+ ## Documentation Map
28
+ - Start at `README.md` for the product overview and command surface.
29
+ - Docs index: `docs/index.md`
30
+ - Core concepts:
31
+ - `docs/concepts/mental-model.md`
32
+ - `docs/concepts/tentacles.md`
33
+ - `docs/concepts/runtime-and-api.md`
34
+ - Workflow guides:
35
+ - `docs/guides/working-with-todos.md`
36
+ - `docs/guides/orchestrating-child-agents.md`
37
+ - `docs/guides/inter-agent-messaging.md`
38
+ - References:
39
+ - `docs/reference/cli.md`
40
+ - `docs/reference/api.md`
41
+ - `docs/reference/filesystem-layout.md`
42
+ - `docs/reference/troubleshooting.md`
43
+ - Read only the docs relevant to the surface you are touching. Do not do a full docs sweep unless the task is documentation maintenance.
44
+
45
+ ## Architecture Boundaries
46
+ - `packages/core` defines domain contracts and pure application logic. Both apps may depend on it; it must not depend on app code.
47
+ - `apps/api` owns infrastructure concerns: PTYs, WebSockets, filesystem persistence, process execution, and git worktree operations.
48
+ - `apps/web` owns presentation and client-side interaction state. Do not move server-only behavior into the web app to avoid adding hidden backend logic to the UI.
49
+ - If behavior is reusable across apps, move it into `packages/core` only when it can remain framework-agnostic.
50
+ - Keep orchestration thin. Entry points such as API server/bootstrap files and top-level React containers should wire dependencies, not accumulate business logic.
51
+
52
+ ## Workflow
53
+ - Read only the guides and code relevant to the surface you are changing. Do not sweep the whole repo before starting.
54
+ - Prefer small, isolated edits over broad cleanup unless the task explicitly asks for refactoring.
55
+ - Keep docs in sync with behavior changes when user-facing workflows, commands, persistence layout, or architecture assumptions change.
56
+ - Preserve the product vocabulary already documented in `CLAUDE.md`: agents, sessions, worktrees, logs, pipelines, tentacles, and terminal columns.
57
+
58
+ ## Verification
59
+ - Install: `pnpm install`
60
+ - Dev: `pnpm dev`
61
+ - Build: `pnpm build`
62
+ - Test: `pnpm test`
63
+ - Lint: `pnpm lint`
64
+ - Format: `pnpm format`
65
+ - For narrow changes, run the most direct test or package-scoped test first, then widen verification as needed.
66
+ - For changes that affect shared contracts, persistence, or cross-app behavior, run the relevant package tests and the root build before landing.
67
+
68
+ ## Scoped Guides
69
+ - `apps/api/AGENTS.md` expands server/runtime/worktree rules.
70
+ - `apps/web/AGENTS.md` expands UI/component/style rules.
71
+ - `packages/core/AGENTS.md` expands domain and ports-and-adapters rules.
@@ -0,0 +1,72 @@
1
+ # Contributing
2
+
3
+ Octogent is an experimental personal project. Contributions are welcome, but the bar is accuracy over volume. Keep changes small, test-backed, and easy to review.
4
+
5
+ ## Before you change anything
6
+
7
+ - read the relevant docs in `docs/`
8
+ - check whether the behavior already exists in the API or UI before adding more surface area
9
+ - keep the project Claude Code-first in docs and product framing
10
+ - do not document speculative features as if they already work
11
+
12
+ ## Prerequisites
13
+
14
+ - Node.js `22+`
15
+ - pnpm
16
+ - `claude` for the supported agent workflow
17
+ - `git` for worktree features
18
+
19
+ ## Setup
20
+
21
+ ```bash
22
+ pnpm install
23
+ ```
24
+
25
+ ## Development
26
+
27
+ ```bash
28
+ pnpm dev
29
+ ```
30
+
31
+ The dev runner starts the local API and web app together.
32
+
33
+ ## Required checks
34
+
35
+ Run these before opening a pull request:
36
+
37
+ ```bash
38
+ pnpm test
39
+ pnpm lint
40
+ pnpm build
41
+ ```
42
+
43
+ Use `pnpm format` if you need to rewrite formatting.
44
+
45
+ ## What good contributions look like
46
+
47
+ - incremental changes with clear scope
48
+ - tests for behavior changes
49
+ - docs updated in the same change when workflows or concepts change
50
+ - code and docs that reflect the current implementation, not a roadmap
51
+
52
+ ## Docs policy
53
+
54
+ - `docs/` is for contributor and future-agent understanding
55
+ - if you change tentacles, todos, terminals, orchestration, or messaging, update the matching docs page
56
+
57
+ ## Pull request expectations
58
+
59
+ - explain the problem in one short paragraph
60
+ - explain the behavior change in concrete terms
61
+ - mention any persistence, API, or workflow impact
62
+ - include screenshots for visible UI changes
63
+ - call out missing follow-up work explicitly instead of hiding it
64
+
65
+ ## Areas that matter most right now
66
+
67
+ - tentacle model and agent-facing context files
68
+ - todo parsing and delegation flow
69
+ - Claude Code terminal lifecycle
70
+ - child-agent orchestration
71
+ - inter-agent messaging
72
+ - fixing existing issues and optimize for reliability
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,184 @@
1
+ <div align="center">
2
+
3
+ <img width="1500" height="500" alt="Octogent header" src="./static/images/octogent-header.png" />
4
+ <br/>
5
+ <br/>
6
+
7
+ <strong>too many terminals, not enough tentacles</strong>
8
+ <br />
9
+ <br />
10
+
11
+ ![Last Update](https://img.shields.io/github/last-commit/hesamsheikh/octogent?label=Last%20Update&style=flat-square)
12
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.8-3178C6?style=flat-square&logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
13
+ [![Node.js](https://img.shields.io/badge/Node.js-22+-5FA04E?style=flat-square&logo=node.js&logoColor=white)](https://nodejs.org/)
14
+ [![Follow on X](https://img.shields.io/badge/Follow%20on-X-000000?style=flat-square&logo=x)](https://x.com/Hesamation)
15
+ [![Discord](https://img.shields.io/badge/Discord-Open%20Source%20AI%20Builders-5865F2?style=flat-square&logo=discord&logoColor=white)](https://discord.gg/vtJykN3t)
16
+
17
+ </div>
18
+
19
+ # Octogent
20
+
21
+ It's really not fun to have **ten Claude Code sessions open at once**, constantly switching between them and trying to remember what each one was supposed to do. *Things get blurry fast* when one agent is doing documentation, another is touching the database, another is changing the API, and another is somewhere in the frontend. **Octogent** tries to fix that by giving each job its own <u>scoped context, notes, and task list</u>, while also making it possible for Claude Code to **spawn other Claude Code agents**, assign them work, and communicate with them.
22
+
23
+ ## The Vision
24
+
25
+ This repo is a personal exploration of what an AI coding environment might look like when terminal coding agents are treated as parts of a bigger orchestration layer, not the final interface by themselves. The point is not to hide **Claude Code** behind abstractions. The point is to make *multi-agent work less chaotic for the developer* on a real codebase.
26
+
27
+ ## Screenshots
28
+
29
+ <div align="center">
30
+ <table>
31
+ <tr>
32
+ <td><img src="./static/images/preview_1.jpg" alt="Screenshot 1" width="100%"/></td>
33
+ <td><img src="./static/images/preview_2.jpg" alt="Screenshot 2" width="100%"/></td>
34
+ </tr>
35
+ <tr>
36
+ <td><img src="./static/images/preview_3.jpg" alt="Screenshot 3" width="100%"/></td>
37
+ <td><img src="./static/images/preview_4.jpg" alt="Screenshot 4" width="100%"/></td>
38
+ </tr>
39
+ <tr>
40
+ <td><img src="./static/images/preview_5.jpg" alt="Screenshot 5" width="100%"/></td>
41
+ <td><img src="./static/images/preview_6.jpg" alt="Screenshot 6" width="100%"/></td>
42
+ </tr>
43
+ </table>
44
+ </div>
45
+
46
+ ## What Octogent Does for You
47
+
48
+ - **Creates tentacles as context layers** so agents can work with scoped markdown files instead of broad, messy chat context
49
+ - **Uses `todo.md` as an execution surface** so tasks stay visible, trackable, and ready for delegation
50
+ - **Runs multiple Claude Code terminals** so one developer can coordinate several coding sessions at once
51
+ - **Spawns child agents from todo items** so parallel work has a concrete source of truth
52
+ - **Supports inter-agent messaging** so workers and coordinators can report completion, blockers, and handoff notes
53
+ - **Keeps agent-facing context in files** so the system is more durable than a single prompt thread
54
+ - **Provides a local API and UI** for terminal lifecycle, persistence, websocket transport, and orchestration
55
+
56
+ A **tentacle** is a folder under `.octogent/tentacles/<tentacle-id>/` that holds agent-readable markdown such as `CONTEXT.md`, `todo.md`, and any extra notes needed for that slice of the codebase.
57
+
58
+ The octopus metaphor is literal: *one octopus, many tentacles, different work happening at the same time*.
59
+
60
+ ## Tentacles
61
+
62
+ A **tentacle** is a scoped job container. It gives one slice of work its own files, notes, and `todo.md` so the agent is not forced to reconstruct the entire codebase context from chat history.
63
+
64
+ What it does:
65
+
66
+ - keeps context local to one area such as documentation, database work, API changes, or frontend work
67
+ - gives agents durable files they can read and update
68
+ - provides a natural source for delegation through todo items
69
+
70
+ For the full model, see [Tentacles](docs/concepts/tentacles.md) and [Working With Todos](docs/guides/working-with-todos.md).
71
+
72
+ ## Context, Notes, and Task Lists
73
+
74
+ In Octogent, a tentacle is not only a task bucket. It is also where the job keeps its local context. That can include notes about one part of the codebase, implementation details, handoff files, and a `todo.md` that tracks what still needs to happen. A Claude Code agent can read and update those files as the work moves forward.
75
+
76
+ That means you can:
77
+
78
+ - keep documentation, database, API, or frontend work separated into different job contexts
79
+ - store the notes that help an agent understand that part of the codebase
80
+ - spawn one agent for one specific item
81
+ - break a larger job into multiple items
82
+ - launch a swarm so several agents work through the list in parallel
83
+ - use the files inside the tentacle as the shared source of truth for what is done and what is left
84
+
85
+ For the full model, see [Tentacles](docs/concepts/tentacles.md) and [Working With Todos](docs/guides/working-with-todos.md).
86
+
87
+ ## Claude Code Managing Claude Code
88
+
89
+ One of the main ideas here is that **Claude Code** should not only be treated as a single terminal session waiting for a human prompt. In Octogent, one Claude Code agent can coordinate other Claude Code agents, assign them specific jobs, and exchange short messages with them while the human stays at the orchestration layer.
90
+
91
+ This is different from Claude Code's subagent spawning, since it allows you to directly see and control what each worker agent is doing.
92
+
93
+ That means Octogent is not just a dashboard for multiple terminals. It is also a way to structure parent-worker behavior around scoped tasks and shared context files.
94
+
95
+ For the current model, see [Orchestrating Child Agents](docs/guides/orchestrating-child-agents.md) and [Inter-Agent Messaging](docs/guides/inter-agent-messaging.md).
96
+
97
+ ## How It Works
98
+
99
+ 1. Create a tentacle for a scoped slice of work.
100
+ 2. Store context, notes, and `todo.md` inside that tentacle so the agent has durable, local guidance.
101
+ 3. Run one or more agent terminals against that tentacle, and delegate child work from the task list when needed.
102
+ 4. Use the local API and web UI to monitor sessions, messages, transcripts, and worktree state.
103
+
104
+ ## Quick start
105
+
106
+ <details>
107
+ <summary><strong>Local development</strong></summary>
108
+
109
+ ```bash
110
+ pnpm install
111
+ pnpm dev
112
+ ```
113
+
114
+ This starts the API and web app for local development.
115
+
116
+ </details>
117
+
118
+ <details open>
119
+ <summary><strong>Current install status</strong></summary>
120
+
121
+ ```bash
122
+ Octogent is not published to the npm registry yet.
123
+ ```
124
+
125
+ For local development:
126
+
127
+ ```bash
128
+ pnpm install
129
+ pnpm dev
130
+ ```
131
+
132
+ For a local global CLI install from a clone:
133
+
134
+ ```bash
135
+ pnpm install
136
+ pnpm build
137
+ npm install -g .
138
+ octogent
139
+ ```
140
+
141
+ The registry install flow `npm install -g octogent` will only work after the package is published.
142
+
143
+ </details>
144
+
145
+ On first run, **Octogent** creates the local `.octogent/` scaffold automatically, assigns a stable project ID, picks an available local API port starting at `8787`, and opens the UI unless `OCTOGENT_NO_OPEN=1` is set.
146
+
147
+ ## Requirements
148
+
149
+ - Node.js `22+`
150
+ - `claude` installed for the supported agent workflow
151
+ - `git` for worktree terminals
152
+ - `gh` for GitHub pull request features
153
+ - `curl` for the current Claude hook callback flow
154
+
155
+ Startup fails if neither `claude` nor another supported provider binary is installed. The current docs only cover **Claude Code**.
156
+
157
+ ## What persists
158
+
159
+ - `.octogent/` keeps project-local scaffold and worktrees
160
+ - `~/.octogent/projects/<project-id>/state/` keeps runtime state, transcripts, monitor cache, and metadata
161
+ - `.octogent/tentacles/<tentacle-id>/` keeps the context files and todos that agents read
162
+
163
+ PTY sessions survive browser reloads during the idle grace period, but they do **not** survive an API restart.
164
+
165
+ ## Docs
166
+
167
+ - [Docs Home](docs/index.md)
168
+ - [Installation](docs/getting-started/installation.md)
169
+ - [Quickstart](docs/getting-started/quickstart.md)
170
+ - [Mental Model](docs/concepts/mental-model.md)
171
+ - [Tentacles](docs/concepts/tentacles.md)
172
+ - [Runtime and API](docs/concepts/runtime-and-api.md)
173
+ - [Working With Todos](docs/guides/working-with-todos.md)
174
+ - [Orchestrating Child Agents](docs/guides/orchestrating-child-agents.md)
175
+ - [Inter-Agent Messaging](docs/guides/inter-agent-messaging.md)
176
+ - [CLI Reference](docs/reference/cli.md)
177
+ - [Filesystem Layout](docs/reference/filesystem-layout.md)
178
+ - [API Reference](docs/reference/api.md)
179
+ - [Experimental Features](docs/reference/experimental-features.md)
180
+ - [Troubleshooting](docs/reference/troubleshooting.md)
181
+ - [Contributing](CONTRIBUTING.md)
182
+
183
+ ## Contributor setup
184
+ Contributions are welcome 🤗. For contributor workflow and expectations, see [CONTRIBUTING.md](CONTRIBUTING.md).
@@ -0,0 +1,32 @@
1
+ # API Guidelines
2
+
3
+ ## Ownership
4
+ - `apps/api` owns HTTP/WebSocket routing, PTY session orchestration, worktree lifecycle, transcript persistence, monitor service, and runtime integrations.
5
+ - Keep infrastructure details here. Do not push PTY, filesystem, process, or git orchestration into `packages/core` or `apps/web`.
6
+
7
+ ## Relevant Docs
8
+ - `docs/concepts/runtime-and-api.md`
9
+ - `docs/reference/api.md`
10
+ - `docs/reference/filesystem-layout.md`
11
+ - `docs/reference/troubleshooting.md`
12
+ - Read these when changing API contracts, runtime lifecycle behavior, persistence layout, or operator-facing server workflows.
13
+
14
+ ## Boundaries
15
+ - Treat `packages/core` as the source of framework-agnostic types and application logic.
16
+ - API routes and request parsers may adapt input into core/application shapes, but avoid embedding large business rules directly in route handlers.
17
+ - Keep request parsing, route wiring, runtime orchestration, and persistence concerns in separate modules when the file structure already supports it.
18
+ - Do not make the web app depend on server-only implementation details. Expose stable API/runtime contracts instead.
19
+
20
+ ## State And Persistence
21
+ - Runtime state under `.octogent/` is a contract surface. Be careful with compatibility when changing file formats or paths.
22
+ - Transcript, tentacle registry, monitor config, and worktree data should remain predictable and inspectable on disk.
23
+ - Prefer explicit migration or normalization paths over silent shape drift.
24
+
25
+ ## PTY, Process, And Git Safety
26
+ - Treat PTY/session lifecycle code as stateful and failure-prone. Handle cleanup, disconnects, and partial failures explicitly.
27
+ - For worktree operations, prioritize correctness and recoverability over clever automation.
28
+ - Avoid destructive filesystem or git behavior unless the task explicitly requires it and the UI/API surface makes the action clear.
29
+
30
+ ## Testing
31
+ - Add targeted tests for request parsing, route behavior, persistence compatibility, and runtime edge cases when touching those surfaces.
32
+ - For bug fixes, reproduce with a test before changing runtime logic when feasible.
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@octogent/api",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "tsx watch src/server.ts",
8
+ "build": "tsc -p tsconfig.json --noEmit",
9
+ "test": "vitest run"
10
+ },
11
+ "dependencies": {
12
+ "@octogent/core": "workspace:*",
13
+ "node-pty": "^1.1.0",
14
+ "ws": "^8.19.0"
15
+ },
16
+ "devDependencies": {
17
+ "tsx": "^4.20.6"
18
+ }
19
+ }
@@ -0,0 +1,181 @@
1
+ import type { AgentRuntimeState } from "@octogent/core";
2
+
3
+ export type { AgentRuntimeState };
4
+
5
+ const PROCESSING_PATTERN = /esc to interrupt/i;
6
+
7
+ const DEFAULT_MAX_BUFFER_LENGTH = 256;
8
+ const DEFAULT_IDLE_AFTER_MS = 1_600;
9
+
10
+ const ESCAPE_CODE = 27;
11
+ const BEL_CODE = 7;
12
+ const CSI_MARKER = 91;
13
+ const OSC_MARKER = 93;
14
+ const ST_MARKER = 92;
15
+
16
+ const stripAnsiAndControlSequences = (value: string) => {
17
+ let cleaned = "";
18
+
19
+ for (let index = 0; index < value.length; index += 1) {
20
+ const code = value.charCodeAt(index);
21
+ if (code !== ESCAPE_CODE) {
22
+ cleaned += value[index] ?? "";
23
+ continue;
24
+ }
25
+
26
+ const marker = value.charCodeAt(index + 1);
27
+ if (marker === CSI_MARKER) {
28
+ index += 2;
29
+ while (index < value.length) {
30
+ const csiCode = value.charCodeAt(index);
31
+ if (csiCode >= 64 && csiCode <= 126) {
32
+ break;
33
+ }
34
+ index += 1;
35
+ }
36
+ continue;
37
+ }
38
+
39
+ if (marker === OSC_MARKER) {
40
+ index += 2;
41
+ while (index < value.length) {
42
+ const oscCode = value.charCodeAt(index);
43
+ if (oscCode === BEL_CODE) {
44
+ break;
45
+ }
46
+ if (oscCode === ESCAPE_CODE && value.charCodeAt(index + 1) === ST_MARKER) {
47
+ index += 1;
48
+ break;
49
+ }
50
+ index += 1;
51
+ }
52
+ continue;
53
+ }
54
+
55
+ // Consume one-byte escape sequences like ESC c.
56
+ index += 1;
57
+ }
58
+
59
+ return cleaned;
60
+ };
61
+
62
+ const normalizeOutput = (chunk: string) => {
63
+ return stripAnsiAndControlSequences(chunk).replace(/\r\n/g, "\n").replace(/\r/g, "\n");
64
+ };
65
+
66
+ const findLastRelevantMatchIndex = (text: string, pattern: RegExp, chunkStartIndex: number) => {
67
+ const flags = pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`;
68
+ const matcher = new RegExp(pattern.source, flags);
69
+ let latest = -1;
70
+
71
+ for (const match of text.matchAll(matcher)) {
72
+ if (typeof match.index !== "number") {
73
+ continue;
74
+ }
75
+
76
+ const matchStartIndex = match.index;
77
+ const matchEndIndex = matchStartIndex + match[0].length;
78
+ const touchesNewChunk = matchEndIndex > chunkStartIndex;
79
+ if (!touchesNewChunk) {
80
+ continue;
81
+ }
82
+
83
+ latest = matchStartIndex;
84
+ }
85
+
86
+ return latest;
87
+ };
88
+
89
+ const hasProcessingSignal = (text: string, chunkStartIndex: number): boolean =>
90
+ findLastRelevantMatchIndex(text, PROCESSING_PATTERN, chunkStartIndex) !== -1;
91
+
92
+ export class AgentStateTracker {
93
+ private readonly maxBufferLength: number;
94
+ private readonly idleAfterMs: number;
95
+ private carry = "";
96
+ private state: AgentRuntimeState;
97
+ private idleDeadlineAt: number | null = null;
98
+
99
+ constructor({
100
+ initialState = "idle",
101
+ maxBufferLength = DEFAULT_MAX_BUFFER_LENGTH,
102
+ idleAfterMs = DEFAULT_IDLE_AFTER_MS,
103
+ }: { initialState?: AgentRuntimeState; maxBufferLength?: number; idleAfterMs?: number } = {}) {
104
+ this.state = initialState;
105
+ this.maxBufferLength = Math.max(256, Math.floor(maxBufferLength));
106
+ this.idleAfterMs = Math.max(250, Math.floor(idleAfterMs));
107
+ if (this.state === "processing") {
108
+ this.idleDeadlineAt = Date.now() + this.idleAfterMs;
109
+ }
110
+ }
111
+
112
+ get currentState(): AgentRuntimeState {
113
+ return this.state;
114
+ }
115
+
116
+ private enterProcessing(now: number): AgentRuntimeState | null {
117
+ this.idleDeadlineAt = now + this.idleAfterMs;
118
+ if (this.state === "processing") {
119
+ return null;
120
+ }
121
+
122
+ this.state = "processing";
123
+ return "processing";
124
+ }
125
+
126
+ forceState(nextState: AgentRuntimeState, now = Date.now()): boolean {
127
+ if (nextState === this.state) {
128
+ if (nextState === "processing") {
129
+ this.idleDeadlineAt = now + this.idleAfterMs;
130
+ }
131
+ return false;
132
+ }
133
+
134
+ this.state = nextState;
135
+ this.idleDeadlineAt = nextState === "processing" ? now + this.idleAfterMs : null;
136
+ return true;
137
+ }
138
+
139
+ observeSubmit(now = Date.now()): AgentRuntimeState | null {
140
+ return this.enterProcessing(now);
141
+ }
142
+
143
+ observeChunk(chunk: string, now = Date.now()): AgentRuntimeState | null {
144
+ if (!chunk) {
145
+ return null;
146
+ }
147
+
148
+ const normalized = normalizeOutput(chunk);
149
+ if (!normalized) {
150
+ return null;
151
+ }
152
+
153
+ const combined = `${this.carry}${normalized}`;
154
+ const chunkStartIndex = this.carry.length;
155
+ this.carry = combined.slice(-this.maxBufferLength);
156
+
157
+ if (this.state === "processing" && normalized.trim().length > 0) {
158
+ this.idleDeadlineAt = now + this.idleAfterMs;
159
+ }
160
+
161
+ if (!hasProcessingSignal(combined, chunkStartIndex)) {
162
+ return null;
163
+ }
164
+
165
+ return this.enterProcessing(now);
166
+ }
167
+
168
+ poll(now = Date.now()): AgentRuntimeState | null {
169
+ if (this.state !== "processing" || this.idleDeadlineAt === null) {
170
+ return null;
171
+ }
172
+
173
+ if (now < this.idleDeadlineAt) {
174
+ return null;
175
+ }
176
+
177
+ this.state = "idle";
178
+ this.idleDeadlineAt = null;
179
+ return "idle";
180
+ }
181
+ }