pi-harness-runtime 0.10.11 → 0.10.12

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 (584) hide show
  1. package/cli.ts +111 -0
  2. package/footer-status.ts +204 -0
  3. package/harness/agent-handoff.ts +189 -0
  4. package/harness/auto-compact.ts +309 -0
  5. package/harness/auto-quota-resume.ts +132 -0
  6. package/harness/autonomous-review.ts +88 -0
  7. package/harness/blackboard.ts +291 -0
  8. package/harness/context-compact-orchestrator.ts +551 -0
  9. package/harness/context-window-manager.ts +464 -0
  10. package/harness/continue-prompt.ts +242 -0
  11. package/harness/e2e/index.ts +44 -0
  12. package/harness/e2e/minimax-quota-parser.ts +88 -0
  13. package/harness/e2e/minimax-quota-scraper.ts +609 -0
  14. package/harness/e2e/openai-quota-scraper.ts +447 -0
  15. package/harness/e2e/playwright-runner.ts +204 -0
  16. package/harness/e2e/quota-status.ts +194 -0
  17. package/harness/e2e/test-engine.ts +398 -0
  18. package/harness/forked-summarizer.ts +304 -0
  19. package/harness/herdr-agents.ts +91 -0
  20. package/harness/index.ts +232 -0
  21. package/harness/job-state-machine.ts +363 -0
  22. package/harness/loop-code-agent.ts +284 -0
  23. package/harness/loop-controller.ts +201 -0
  24. package/harness/loop-review-agent.ts +295 -0
  25. package/harness/loop-runtime.ts +760 -0
  26. package/harness/loop-types.ts +118 -0
  27. package/harness/master-planner.ts +327 -0
  28. package/harness/notification-events.ts +299 -0
  29. package/harness/output-limit-handler.ts +316 -0
  30. package/harness/partial-recovery.ts +531 -0
  31. package/harness/project-detector/detector.ts +328 -0
  32. package/harness/repair-engine.ts +340 -0
  33. package/harness/session-memory.ts +401 -0
  34. package/harness/task-graph.d.ts +51 -0
  35. package/harness/task-graph.ts +143 -0
  36. package/mirror.ts +278 -0
  37. package/package.json +8 -12
  38. package/packages/a2a-adapter/src/agent.ts +139 -0
  39. package/packages/a2a-adapter/src/client.ts +107 -0
  40. package/packages/a2a-adapter/src/discovery.ts +106 -0
  41. package/packages/a2a-adapter/src/index.ts +38 -0
  42. package/packages/a2a-adapter/src/protocol.ts +46 -0
  43. package/packages/a2a-adapter/src/transport.ts +142 -0
  44. package/packages/a2a-adapter/src/types.ts +117 -0
  45. package/packages/auth/src/minimax-browser-auth.ts +1109 -0
  46. package/packages/auth/src/run-minimax-auth.ts +150 -0
  47. package/packages/autonomous-refactor/src/detector.ts +219 -0
  48. package/packages/autonomous-refactor/src/index.ts +16 -0
  49. package/packages/autonomous-refactor/src/planner.ts +99 -0
  50. package/packages/autonomous-refactor/src/types.ts +57 -0
  51. package/packages/autonomous-runtime/src/inbox.ts +266 -0
  52. package/packages/autonomous-runtime/src/index.ts +22 -0
  53. package/packages/autonomous-runtime/src/lease.ts +318 -0
  54. package/packages/autonomous-runtime/src/types.ts +257 -0
  55. package/packages/cache-strategy/src/cache.ts +213 -0
  56. package/packages/cache-strategy/src/index.ts +6 -0
  57. package/packages/cache-strategy/src/types.ts +40 -0
  58. package/packages/capability-registry/src/defaults.ts +247 -0
  59. package/packages/capability-registry/src/index.ts +43 -0
  60. package/packages/capability-registry/src/query.ts +99 -0
  61. package/packages/capability-registry/src/registry.ts +222 -0
  62. package/packages/capability-registry/src/types.d.ts +47 -0
  63. package/packages/capability-registry/src/types.js +10 -0
  64. package/packages/capability-registry/src/types.ts +83 -0
  65. package/packages/checkpoint/src/checkpoint-manager.ts +33 -0
  66. package/packages/checkpoint/src/differ.ts +281 -0
  67. package/packages/checkpoint/src/engine.ts +532 -0
  68. package/packages/checkpoint/src/index.ts +48 -0
  69. package/packages/checkpoint/src/storage.ts +439 -0
  70. package/packages/checkpoint/src/types.ts +348 -0
  71. package/packages/cli-plugin-sdk/src/index.ts +169 -0
  72. package/packages/cli-plugin-sdk/src/plugin-fs.ts +91 -0
  73. package/packages/cli-plugin-sdk/src/registry.ts +55 -0
  74. package/packages/cli-plugin-sdk/src/types.ts +32 -0
  75. package/packages/clipboard/dist/src/clipboard.d.ts +11 -0
  76. package/packages/clipboard/dist/src/clipboard.js +51 -0
  77. package/packages/clipboard/dist/src/copy-sync.d.ts +27 -0
  78. package/packages/clipboard/dist/src/copy-sync.js +170 -0
  79. package/packages/clipboard/dist/src/gist-relay.d.ts +48 -0
  80. package/packages/clipboard/dist/src/gist-relay.js +186 -0
  81. package/packages/clipboard/dist/src/github-login.d.ts +16 -0
  82. package/packages/clipboard/dist/src/github-login.js +69 -0
  83. package/packages/clipboard/dist/src/index.d.ts +4 -0
  84. package/packages/clipboard/dist/src/index.js +3 -0
  85. package/packages/clipboard/dist/src/types.d.ts +13 -0
  86. package/packages/clipboard/dist/src/types.js +5 -0
  87. package/packages/clipboard/src/clipboard.ts +59 -0
  88. package/packages/clipboard/src/copy-sync.ts +201 -0
  89. package/packages/clipboard/src/gist-relay.ts +240 -0
  90. package/packages/clipboard/src/github-login.ts +107 -0
  91. package/packages/clipboard/src/index.ts +3 -0
  92. package/packages/clipboard/src/types.ts +14 -0
  93. package/packages/code-generation/src/faker.ts +461 -0
  94. package/packages/code-generation/src/generator.ts +421 -0
  95. package/packages/code-generation/src/index.ts +56 -0
  96. package/packages/code-generation/src/templates/ejs-renderer.ts +142 -0
  97. package/packages/code-generation/src/templates/registry.ts +174 -0
  98. package/packages/code-generation/src/types.ts +373 -0
  99. package/packages/code-review/src/engine.ts +408 -0
  100. package/packages/code-review/src/index.ts +46 -0
  101. package/packages/code-review/src/reports/html.ts +143 -0
  102. package/packages/code-review/src/reports/markdown.ts +197 -0
  103. package/packages/code-review/src/reports/text.ts +171 -0
  104. package/packages/code-review/src/types.ts +324 -0
  105. package/packages/codex-adapter/src/executor.ts +83 -0
  106. package/packages/codex-adapter/src/index.ts +16 -0
  107. package/packages/codex-adapter/src/provider.ts +213 -0
  108. package/packages/codex-adapter/src/types.ts +118 -0
  109. package/packages/config-capture/src/index.ts +357 -0
  110. package/packages/context-compiler/src/budget.ts +122 -0
  111. package/packages/context-compiler/src/cache.ts +134 -0
  112. package/packages/context-compiler/src/compiler.ts +314 -0
  113. package/packages/context-compiler/src/deduplicate.ts +171 -0
  114. package/packages/context-compiler/src/filter.ts +135 -0
  115. package/packages/context-compiler/src/index.ts +61 -0
  116. package/packages/context-compiler/src/okf-loader.ts +86 -0
  117. package/packages/context-compiler/src/score.ts +130 -0
  118. package/packages/context-compiler/src/types.ts +246 -0
  119. package/packages/context-discovery/dist/src/discover.d.ts +3 -0
  120. package/packages/context-discovery/dist/src/discover.js +42 -0
  121. package/packages/context-discovery/dist/src/discovery.d.ts +28 -0
  122. package/packages/context-discovery/dist/src/discovery.js +21 -0
  123. package/packages/context-discovery/dist/src/index.d.ts +3 -0
  124. package/packages/context-discovery/dist/src/index.js +2 -0
  125. package/packages/context-discovery/dist/src/types.d.ts +16 -0
  126. package/packages/context-discovery/dist/src/types.js +2 -0
  127. package/packages/context-discovery/src/discover.ts +42 -0
  128. package/packages/context-discovery/src/discovery.ts +44 -0
  129. package/packages/context-discovery/src/index.ts +2 -0
  130. package/packages/context-discovery/src/types.ts +15 -0
  131. package/packages/context-manager/src/compaction.ts +139 -0
  132. package/packages/context-manager/src/core.ts +85 -0
  133. package/packages/context-manager/src/index.ts +22 -0
  134. package/packages/context-manager/src/types.ts +44 -0
  135. package/packages/cookie-sanitizer/src/atomic-write.ts +79 -0
  136. package/packages/cookie-sanitizer/src/detect-format.ts +102 -0
  137. package/packages/cookie-sanitizer/src/index.ts +89 -0
  138. package/packages/cookie-sanitizer/src/infer-provider.ts +103 -0
  139. package/packages/cookie-sanitizer/src/normalize.ts +92 -0
  140. package/packages/cookie-sanitizer/src/parse-json.ts +122 -0
  141. package/packages/cookie-sanitizer/src/parse-netscape.ts +118 -0
  142. package/packages/cookie-sanitizer/src/sync.ts +312 -0
  143. package/packages/cookie-sanitizer/src/types.ts +101 -0
  144. package/packages/cookie-sanitizer/src/watcher.ts +180 -0
  145. package/packages/cost-optimizer/src/budget.ts +138 -0
  146. package/packages/cost-optimizer/src/config.ts +34 -0
  147. package/packages/cost-optimizer/src/forecast.ts +59 -0
  148. package/packages/cost-optimizer/src/index.ts +74 -0
  149. package/packages/cost-optimizer/src/optimizer.ts +277 -0
  150. package/packages/cost-optimizer/src/selection.ts +264 -0
  151. package/packages/cost-optimizer/src/tracker.ts +122 -0
  152. package/packages/cost-optimizer/src/types.ts +134 -0
  153. package/packages/dependency-analyzer/dist/src/analyzer.d.ts +17 -0
  154. package/packages/dependency-analyzer/dist/src/analyzer.js +119 -0
  155. package/packages/dependency-analyzer/dist/src/imports.d.ts +12 -0
  156. package/packages/dependency-analyzer/dist/src/imports.js +73 -0
  157. package/packages/dependency-analyzer/dist/src/index.d.ts +4 -0
  158. package/packages/dependency-analyzer/dist/src/index.js +3 -0
  159. package/packages/dependency-analyzer/dist/src/types.d.ts +28 -0
  160. package/packages/dependency-analyzer/dist/src/types.js +5 -0
  161. package/packages/dependency-analyzer/src/analyzer.ts +140 -0
  162. package/packages/dependency-analyzer/src/imports.ts +93 -0
  163. package/packages/dependency-analyzer/src/index.ts +9 -0
  164. package/packages/dependency-analyzer/src/types.ts +38 -0
  165. package/packages/django-plugin/src/analyzer.ts +284 -0
  166. package/packages/django-plugin/src/index.ts +39 -0
  167. package/packages/django-plugin/src/types.ts +35 -0
  168. package/packages/doc-generator/src/detector.ts +113 -0
  169. package/packages/doc-generator/src/docs.ts +158 -0
  170. package/packages/doc-generator/src/index.ts +7 -0
  171. package/packages/doc-generator/src/types.ts +58 -0
  172. package/packages/evaluation-engine/src/engine.ts +488 -0
  173. package/packages/evaluation-engine/src/index.ts +23 -0
  174. package/packages/evaluation-engine/src/types.ts +136 -0
  175. package/packages/evaluation-runner/src/index.ts +6 -0
  176. package/packages/evaluation-runner/src/runner.ts +182 -0
  177. package/packages/evaluation-runner/src/types.ts +51 -0
  178. package/packages/event-bus/dist/src/bus.d.ts +15 -0
  179. package/packages/event-bus/dist/src/bus.js +98 -0
  180. package/packages/event-bus/dist/src/herdr-bus.d.ts +121 -0
  181. package/packages/event-bus/dist/src/herdr-bus.js +322 -0
  182. package/packages/event-bus/dist/src/index.d.ts +6 -0
  183. package/packages/event-bus/dist/src/index.js +4 -0
  184. package/packages/event-bus/dist/src/types.d.ts +27 -0
  185. package/packages/event-bus/dist/src/types.js +10 -0
  186. package/packages/event-bus/src/bus.ts +111 -0
  187. package/packages/event-bus/src/herdr-bus.ts +525 -0
  188. package/packages/event-bus/src/index.ts +38 -0
  189. package/packages/event-bus/src/types.ts +59 -0
  190. package/packages/event-store/dist/src/index.d.ts +3 -0
  191. package/packages/event-store/dist/src/index.js +2 -0
  192. package/packages/event-store/dist/src/store.d.ts +15 -0
  193. package/packages/event-store/dist/src/store.js +139 -0
  194. package/packages/event-store/dist/src/types.d.ts +48 -0
  195. package/packages/event-store/dist/src/types.js +5 -0
  196. package/packages/event-store/src/index.ts +11 -0
  197. package/packages/event-store/src/store.ts +168 -0
  198. package/packages/event-store/src/types.d.ts +48 -0
  199. package/packages/event-store/src/types.js +5 -0
  200. package/packages/event-store/src/types.ts +65 -0
  201. package/packages/experience-replay/src/index.ts +19 -0
  202. package/packages/experience-replay/src/replay.ts +348 -0
  203. package/packages/experience-replay/src/types.ts +128 -0
  204. package/packages/feedback-collector/src/collector.ts +160 -0
  205. package/packages/feedback-collector/src/index.ts +6 -0
  206. package/packages/feedback-collector/src/types.ts +51 -0
  207. package/packages/framework-detector/src/detector.ts +586 -0
  208. package/packages/framework-detector/src/index.ts +46 -0
  209. package/packages/framework-detector/src/signatures/registry.ts +648 -0
  210. package/packages/framework-detector/src/types.ts +299 -0
  211. package/packages/framework-plugin-sdk/src/index.ts +45 -0
  212. package/packages/framework-plugin-sdk/src/manager.ts +525 -0
  213. package/packages/framework-plugin-sdk/src/types.ts +449 -0
  214. package/packages/frappe-plugin/src/analyzer.ts +454 -0
  215. package/packages/frappe-plugin/src/index.ts +72 -0
  216. package/packages/frappe-plugin/src/types.ts +135 -0
  217. package/packages/generic-web-plugin/src/analyzer.ts +347 -0
  218. package/packages/generic-web-plugin/src/index.ts +61 -0
  219. package/packages/generic-web-plugin/src/types.ts +41 -0
  220. package/packages/health-monitor/src/index.ts +16 -0
  221. package/packages/health-monitor/src/monitor.ts +216 -0
  222. package/packages/health-monitor/src/types.ts +45 -0
  223. package/packages/intent-analyzer/dist/src/analyzer.d.ts +13 -0
  224. package/packages/intent-analyzer/dist/src/analyzer.js +239 -0
  225. package/packages/intent-analyzer/dist/src/index.d.ts +3 -0
  226. package/packages/intent-analyzer/dist/src/index.js +2 -0
  227. package/packages/intent-analyzer/dist/src/types.d.ts +26 -0
  228. package/packages/intent-analyzer/dist/src/types.js +7 -0
  229. package/packages/intent-analyzer/src/analyzer.ts +259 -0
  230. package/packages/intent-analyzer/src/index.ts +8 -0
  231. package/packages/intent-analyzer/src/types.ts +44 -0
  232. package/packages/knowledge-retrieval/src/index.ts +18 -0
  233. package/packages/knowledge-retrieval/src/mcp-client.ts +212 -0
  234. package/packages/knowledge-retrieval/src/types.ts +112 -0
  235. package/packages/laravel-plugin/src/analyzer.ts +323 -0
  236. package/packages/laravel-plugin/src/index.ts +40 -0
  237. package/packages/laravel-plugin/src/types.ts +24 -0
  238. package/packages/learning-engine/src/engine.ts +391 -0
  239. package/packages/learning-engine/src/index.ts +26 -0
  240. package/packages/learning-engine/src/types.ts +201 -0
  241. package/packages/mcp-adapter/src/index.ts +20 -0
  242. package/packages/mcp-adapter/src/server.ts +273 -0
  243. package/packages/mcp-adapter/src/tools.ts +220 -0
  244. package/packages/mcp-adapter/src/types.ts +79 -0
  245. package/packages/memory-engine/src/engine.ts +615 -0
  246. package/packages/memory-engine/src/index.ts +20 -0
  247. package/packages/memory-engine/src/types.ts +159 -0
  248. package/packages/model-registry/src/cost.ts +80 -0
  249. package/packages/model-registry/src/defaults.ts +178 -0
  250. package/packages/model-registry/src/index.ts +57 -0
  251. package/packages/model-registry/src/query.ts +85 -0
  252. package/packages/model-registry/src/registry.ts +188 -0
  253. package/packages/model-registry/src/types.d.ts +63 -0
  254. package/packages/model-registry/src/types.js +5 -0
  255. package/packages/model-registry/src/types.ts +69 -0
  256. package/packages/nextjs-plugin/src/analyzer.ts +247 -0
  257. package/packages/nextjs-plugin/src/index.ts +41 -0
  258. package/packages/nextjs-plugin/src/types.ts +36 -0
  259. package/packages/notification/src/adapters/email-adapter.ts +126 -0
  260. package/packages/notification/src/adapters/ntfy-adapter.ts +100 -0
  261. package/packages/notification/src/adapters/telegram-adapter.ts +107 -0
  262. package/packages/notification/src/adapters/webhook-adapter.ts +75 -0
  263. package/packages/notification/src/base-adapter.ts +75 -0
  264. package/packages/notification/src/index.ts +9 -0
  265. package/packages/notification/src/notification-center.ts +286 -0
  266. package/packages/notification/src/types.ts +89 -0
  267. package/packages/observability/src/alerts.ts +355 -0
  268. package/packages/observability/src/health.ts +325 -0
  269. package/packages/observability/src/index.ts +79 -0
  270. package/packages/observability/src/logger.ts +373 -0
  271. package/packages/observability/src/metrics.ts +475 -0
  272. package/packages/observability/src/tracer.ts +336 -0
  273. package/packages/observability/src/types.ts +391 -0
  274. package/packages/okf-indexer/src/cli.ts +25 -0
  275. package/packages/okf-indexer/src/indexer.ts +186 -0
  276. package/packages/performance-optimizer/src/index.ts +20 -0
  277. package/packages/performance-optimizer/src/optimizer.ts +428 -0
  278. package/packages/performance-optimizer/src/types.ts +112 -0
  279. package/packages/privilege-broker/src/approval.ts +70 -0
  280. package/packages/privilege-broker/src/audit.ts +142 -0
  281. package/packages/privilege-broker/src/executor.ts +110 -0
  282. package/packages/privilege-broker/src/index.ts +46 -0
  283. package/packages/privilege-broker/src/registry.ts +98 -0
  284. package/packages/privilege-broker/src/types.ts +105 -0
  285. package/packages/project-analyzer/src/analyzer.ts +683 -0
  286. package/packages/project-analyzer/src/cache.ts +329 -0
  287. package/packages/project-analyzer/src/command-discovery.ts +239 -0
  288. package/packages/project-analyzer/src/index.ts +123 -0
  289. package/packages/project-analyzer/src/plugins/index.ts +16 -0
  290. package/packages/project-analyzer/src/rule-discovery.ts +282 -0
  291. package/packages/project-analyzer/src/signals.ts +355 -0
  292. package/packages/project-analyzer/src/types.ts +412 -0
  293. package/packages/project-analyzer/src/walker.ts +366 -0
  294. package/packages/projection-engine/dist/src/engine.d.ts +61 -0
  295. package/packages/projection-engine/dist/src/engine.js +87 -0
  296. package/packages/projection-engine/dist/src/index.d.ts +2 -0
  297. package/packages/projection-engine/dist/src/index.js +2 -0
  298. package/packages/projection-engine/dist/src/project.d.ts +3 -0
  299. package/packages/projection-engine/dist/src/project.js +24 -0
  300. package/packages/projection-engine/dist/src/types.d.ts +55 -0
  301. package/packages/projection-engine/dist/src/types.js +5 -0
  302. package/packages/projection-engine/src/engine.ts +134 -0
  303. package/packages/projection-engine/src/index.ts +8 -0
  304. package/packages/projection-engine/src/project.ts +24 -0
  305. package/packages/projection-engine/src/types.d.ts +55 -0
  306. package/packages/projection-engine/src/types.js +5 -0
  307. package/packages/projection-engine/src/types.ts +60 -0
  308. package/packages/prompt-compiler/src/budget.ts +170 -0
  309. package/packages/prompt-compiler/src/compiler.ts +188 -0
  310. package/packages/prompt-compiler/src/deduplicate.ts +84 -0
  311. package/packages/prompt-compiler/src/index.ts +50 -0
  312. package/packages/prompt-compiler/src/normalize.ts +140 -0
  313. package/packages/prompt-compiler/src/render.ts +105 -0
  314. package/packages/prompt-compiler/src/section-builder.ts +279 -0
  315. package/packages/prompt-compiler/src/types.ts +263 -0
  316. package/packages/prompt-compiler/src/validate.ts +157 -0
  317. package/packages/prompt-versioning/src/index.ts +6 -0
  318. package/packages/prompt-versioning/src/types.ts +41 -0
  319. package/packages/prompt-versioning/src/versioning.ts +149 -0
  320. package/packages/provider-adapter-sdk/dist/src/builder.d.ts +140 -0
  321. package/packages/provider-adapter-sdk/dist/src/builder.js +332 -0
  322. package/packages/provider-adapter-sdk/dist/src/errors.d.ts +71 -0
  323. package/packages/provider-adapter-sdk/dist/src/errors.js +111 -0
  324. package/packages/provider-adapter-sdk/dist/src/index.d.ts +15 -0
  325. package/packages/provider-adapter-sdk/dist/src/index.js +19 -0
  326. package/packages/provider-adapter-sdk/dist/src/registry.d.ts +108 -0
  327. package/packages/provider-adapter-sdk/dist/src/registry.js +301 -0
  328. package/packages/provider-adapter-sdk/dist/src/tester.d.ts +71 -0
  329. package/packages/provider-adapter-sdk/dist/src/tester.js +392 -0
  330. package/packages/provider-adapter-sdk/dist/src/types.d.ts +182 -0
  331. package/packages/provider-adapter-sdk/dist/src/types.js +11 -0
  332. package/packages/provider-adapter-sdk/src/builder.ts +442 -0
  333. package/packages/provider-adapter-sdk/src/errors.ts +153 -0
  334. package/packages/provider-adapter-sdk/src/index.ts +84 -0
  335. package/packages/provider-adapter-sdk/src/registry.ts +486 -0
  336. package/packages/provider-adapter-sdk/src/tester.ts +483 -0
  337. package/packages/provider-adapter-sdk/src/types.ts +293 -0
  338. package/packages/provider-router/dist/capability-registry/src/types.d.ts +47 -0
  339. package/packages/provider-router/dist/capability-registry/src/types.js +10 -0
  340. package/packages/provider-router/dist/provider-router/src/index.d.ts +23 -0
  341. package/packages/provider-router/dist/provider-router/src/index.js +25 -0
  342. package/packages/provider-router/dist/provider-router/src/policy.d.ts +10 -0
  343. package/packages/provider-router/dist/provider-router/src/policy.js +45 -0
  344. package/packages/provider-router/dist/provider-router/src/provider-router.d.ts +56 -0
  345. package/packages/provider-router/dist/provider-router/src/provider-router.js +222 -0
  346. package/packages/provider-router/dist/provider-router/src/strategies.d.ts +19 -0
  347. package/packages/provider-router/dist/provider-router/src/strategies.js +110 -0
  348. package/packages/provider-router/dist/provider-router/src/types.d.ts +91 -0
  349. package/packages/provider-router/dist/provider-router/src/types.js +10 -0
  350. package/packages/provider-router/dist/src/index.d.ts +23 -0
  351. package/packages/provider-router/dist/src/index.js +25 -0
  352. package/packages/provider-router/dist/src/policy.d.ts +10 -0
  353. package/packages/provider-router/dist/src/policy.js +45 -0
  354. package/packages/provider-router/dist/src/provider-router.d.ts +56 -0
  355. package/packages/provider-router/dist/src/provider-router.js +222 -0
  356. package/packages/provider-router/dist/src/strategies.d.ts +19 -0
  357. package/packages/provider-router/dist/src/strategies.js +110 -0
  358. package/packages/provider-router/dist/src/types.d.ts +91 -0
  359. package/packages/provider-router/dist/src/types.js +10 -0
  360. package/packages/provider-router/dist/types/src/runtime-types.d.ts +412 -0
  361. package/packages/provider-router/dist/types/src/runtime-types.js +2 -0
  362. package/packages/provider-router/src/index.ts +49 -0
  363. package/packages/provider-router/src/policy.ts +50 -0
  364. package/packages/provider-router/src/provider-router.ts +315 -0
  365. package/packages/provider-router/src/strategies.ts +158 -0
  366. package/packages/provider-router/src/types.ts +130 -0
  367. package/packages/provider-selector/src/index.ts +13 -0
  368. package/packages/provider-selector/src/selector.ts +120 -0
  369. package/packages/provider-selector/src/types.ts +33 -0
  370. package/packages/providers/dist/src/adapters.d.ts +133 -0
  371. package/packages/providers/dist/src/adapters.js +236 -0
  372. package/packages/providers/dist/src/deepagents-adapter.d.ts +78 -0
  373. package/packages/providers/dist/src/deepagents-adapter.js +323 -0
  374. package/packages/providers/dist/src/glm-usage.d.ts +102 -0
  375. package/packages/providers/dist/src/glm-usage.js +197 -0
  376. package/packages/providers/dist/src/index.d.ts +11 -0
  377. package/packages/providers/dist/src/index.js +11 -0
  378. package/packages/providers/dist/src/openai-usage.d.ts +103 -0
  379. package/packages/providers/dist/src/openai-usage.js +208 -0
  380. package/packages/providers/dist/src/provider-id.d.ts +46 -0
  381. package/packages/providers/dist/src/provider-id.js +113 -0
  382. package/packages/providers/src/adapters.ts +362 -0
  383. package/packages/providers/src/deepagents-adapter.ts +458 -0
  384. package/packages/providers/src/glm-usage.ts +248 -0
  385. package/packages/providers/src/index.ts +11 -0
  386. package/packages/providers/src/openai-usage.ts +263 -0
  387. package/packages/providers/src/provider-id.ts +129 -0
  388. package/packages/quota-manager/dist/src/examples/tui-usage-integration.d.ts +35 -0
  389. package/packages/quota-manager/dist/src/examples/tui-usage-integration.js +73 -0
  390. package/packages/quota-manager/dist/src/index.d.ts +8 -0
  391. package/packages/quota-manager/dist/src/index.js +8 -0
  392. package/packages/quota-manager/dist/src/quota-manager.d.ts +70 -0
  393. package/packages/quota-manager/dist/src/quota-manager.js +260 -0
  394. package/packages/quota-manager/dist/src/tui-usage-monitor.d.ts +96 -0
  395. package/packages/quota-manager/dist/src/tui-usage-monitor.js +268 -0
  396. package/packages/quota-manager/src/examples/tui-usage-integration.ts +72 -0
  397. package/packages/quota-manager/src/index.ts +8 -0
  398. package/packages/quota-manager/src/quota-manager.ts +336 -0
  399. package/packages/quota-manager/src/tui-usage-monitor.ts +355 -0
  400. package/packages/rate-limiter/src/index.ts +6 -0
  401. package/packages/rate-limiter/src/limiter.ts +122 -0
  402. package/packages/rate-limiter/src/types.ts +41 -0
  403. package/packages/react-vite-plugin/src/analyzer.ts +226 -0
  404. package/packages/react-vite-plugin/src/index.ts +39 -0
  405. package/packages/react-vite-plugin/src/types.ts +28 -0
  406. package/packages/release-manager/src/changelog.ts +148 -0
  407. package/packages/release-manager/src/index.ts +21 -0
  408. package/packages/release-manager/src/types.ts +50 -0
  409. package/packages/release-manager/src/version.ts +52 -0
  410. package/packages/requirement-compiler/src/ambiguity-detector.ts +302 -0
  411. package/packages/requirement-compiler/src/classifier.ts +145 -0
  412. package/packages/requirement-compiler/src/compiler.ts +328 -0
  413. package/packages/requirement-compiler/src/extractor.ts +155 -0
  414. package/packages/requirement-compiler/src/index.ts +36 -0
  415. package/packages/requirement-compiler/src/normalizer.ts +161 -0
  416. package/packages/requirement-compiler/src/risk-detector.ts +58 -0
  417. package/packages/requirement-compiler/src/types.ts +596 -0
  418. package/packages/runtime/dist/src/command-executor.d.ts +129 -0
  419. package/packages/runtime/dist/src/command-executor.js +383 -0
  420. package/packages/runtime/dist/src/index.d.ts +11 -0
  421. package/packages/runtime/dist/src/index.js +11 -0
  422. package/packages/runtime/dist/src/local-runtime-agent.d.ts +111 -0
  423. package/packages/runtime/dist/src/local-runtime-agent.js +356 -0
  424. package/packages/runtime/dist/src/policy-engine.d.ts +203 -0
  425. package/packages/runtime/dist/src/policy-engine.js +592 -0
  426. package/packages/runtime/dist/src/runtime-api.d.ts +137 -0
  427. package/packages/runtime/dist/src/runtime-api.js +454 -0
  428. package/packages/runtime/dist/src/workspace-manager.d.ts +162 -0
  429. package/packages/runtime/dist/src/workspace-manager.js +451 -0
  430. package/packages/runtime/src/command-executor.ts +523 -0
  431. package/packages/runtime/src/index.ts +11 -0
  432. package/packages/runtime/src/local-runtime-agent.ts +480 -0
  433. package/packages/runtime/src/policy-engine.ts +813 -0
  434. package/packages/runtime/src/runtime-api.ts +665 -0
  435. package/packages/runtime/src/workspace-manager.ts +591 -0
  436. package/packages/scheduler/src/scheduler.ts +50 -0
  437. package/packages/scheduler-adapter/src/cron.ts +123 -0
  438. package/packages/scheduler-adapter/src/index.ts +9 -0
  439. package/packages/scheduler-adapter/src/interface.ts +61 -0
  440. package/packages/scheduler-adapter/src/internal.ts +99 -0
  441. package/packages/scheduler-adapter/src/systemd.ts +221 -0
  442. package/packages/session/src/history.ts +353 -0
  443. package/packages/session/src/index.ts +53 -0
  444. package/packages/session/src/manager.ts +481 -0
  445. package/packages/session/src/policy.ts +363 -0
  446. package/packages/session/src/store.ts +348 -0
  447. package/packages/session/src/types.ts +275 -0
  448. package/packages/session-api/dist/src/api.d.ts +83 -0
  449. package/packages/session-api/dist/src/api.js +49 -0
  450. package/packages/session-api/dist/src/index.d.ts +2 -0
  451. package/packages/session-api/dist/src/index.js +2 -0
  452. package/packages/session-api/dist/src/types.d.ts +22 -0
  453. package/packages/session-api/dist/src/types.js +5 -0
  454. package/packages/session-api/src/api.ts +117 -0
  455. package/packages/session-api/src/index.ts +1 -0
  456. package/packages/session-api/src/types.ts +23 -0
  457. package/packages/session-export/dist/src/exporter.d.ts +28 -0
  458. package/packages/session-export/dist/src/exporter.js +42 -0
  459. package/packages/session-export/dist/src/index.d.ts +2 -0
  460. package/packages/session-export/dist/src/index.js +2 -0
  461. package/packages/session-export/dist/src/types.d.ts +20 -0
  462. package/packages/session-export/dist/src/types.js +5 -0
  463. package/packages/session-export/src/exporter.ts +81 -0
  464. package/packages/session-export/src/index.ts +1 -0
  465. package/packages/session-export/src/types.ts +22 -0
  466. package/packages/shared-context/src/index.ts +7 -0
  467. package/packages/shared-context/src/shared-context.ts +42 -0
  468. package/packages/skill-registry/src/defaults.ts +146 -0
  469. package/packages/skill-registry/src/index.ts +48 -0
  470. package/packages/skill-registry/src/matching.ts +118 -0
  471. package/packages/skill-registry/src/registry.ts +211 -0
  472. package/packages/skill-registry/src/types.ts +97 -0
  473. package/packages/sprint-planner/src/decomposer.ts +57 -0
  474. package/packages/sprint-planner/src/index.ts +16 -0
  475. package/packages/sprint-planner/src/planner.ts +150 -0
  476. package/packages/sprint-planner/src/types.ts +59 -0
  477. package/packages/subscription-engine/dist/src/engine.d.ts +13 -0
  478. package/packages/subscription-engine/dist/src/engine.js +40 -0
  479. package/packages/subscription-engine/dist/src/index.d.ts +2 -0
  480. package/packages/subscription-engine/dist/src/index.js +2 -0
  481. package/packages/subscription-engine/dist/src/subscriptions.d.ts +19 -0
  482. package/packages/subscription-engine/dist/src/subscriptions.js +39 -0
  483. package/packages/subscription-engine/dist/src/types.d.ts +24 -0
  484. package/packages/subscription-engine/dist/src/types.js +5 -0
  485. package/packages/subscription-engine/src/engine.ts +55 -0
  486. package/packages/subscription-engine/src/index.ts +6 -0
  487. package/packages/subscription-engine/src/subscriptions.ts +55 -0
  488. package/packages/subscription-engine/src/types.ts +27 -0
  489. package/packages/task-compiler/src/command-policy.ts +193 -0
  490. package/packages/task-compiler/src/compiler.ts +114 -0
  491. package/packages/task-compiler/src/decompose.ts +265 -0
  492. package/packages/task-compiler/src/file-scope.ts +240 -0
  493. package/packages/task-compiler/src/graph.ts +144 -0
  494. package/packages/task-compiler/src/index.ts +38 -0
  495. package/packages/task-compiler/src/types.ts +225 -0
  496. package/packages/task-compiler/src/validate.ts +218 -0
  497. package/packages/task-compiler/src/verification.ts +248 -0
  498. package/packages/tencentdb-memory/src/client.ts +130 -0
  499. package/packages/tencentdb-memory/src/index.ts +32 -0
  500. package/packages/tencentdb-memory/src/loader.ts +150 -0
  501. package/packages/tencentdb-memory/src/mcp-tools.ts +213 -0
  502. package/packages/tencentdb-memory/src/types.ts +101 -0
  503. package/packages/tencentdb-sync/src/cli.ts +363 -0
  504. package/packages/tencentdb-sync/src/client.ts +268 -0
  505. package/packages/tencentdb-sync/src/config.ts +139 -0
  506. package/packages/tencentdb-sync/src/index.ts +15 -0
  507. package/packages/test-data-generator/src/faker-adapter.ts +657 -0
  508. package/packages/test-data-generator/src/generator.ts +461 -0
  509. package/packages/test-data-generator/src/index.ts +48 -0
  510. package/packages/test-data-generator/src/types.ts +421 -0
  511. package/packages/test-generator/src/generator.ts +144 -0
  512. package/packages/test-generator/src/index.ts +11 -0
  513. package/packages/test-generator/src/types.ts +39 -0
  514. package/packages/todo-bd-sync/src/detector.ts +171 -0
  515. package/packages/todo-bd-sync/src/extension.ts +129 -0
  516. package/packages/todo-bd-sync/src/index.ts +401 -0
  517. package/packages/todo-bd-sync/src/sync.ts +322 -0
  518. package/packages/todo-bd-sync/src/task-analyzer.ts +314 -0
  519. package/packages/todo-bd-sync/src/todo-reminder.ts +418 -0
  520. package/packages/todo-bd-sync/src/types.ts +137 -0
  521. package/packages/token-estimation/src/index.ts +192 -0
  522. package/packages/token-optimizer/src/index.ts +15 -0
  523. package/packages/token-optimizer/src/optimizer.ts +150 -0
  524. package/packages/token-optimizer/src/types.ts +47 -0
  525. package/packages/types/src/agent-worker-types.d.ts +206 -0
  526. package/packages/types/src/agent-worker-types.js +41 -0
  527. package/packages/types/src/agent-worker-types.ts +264 -0
  528. package/packages/types/src/ai-providers.d.ts +36 -0
  529. package/packages/types/src/ai-providers.js +66 -0
  530. package/packages/types/src/ai-providers.ts +73 -0
  531. package/packages/types/src/runtime-types.d.ts +418 -0
  532. package/packages/types/src/runtime-types.js +3 -0
  533. package/packages/types/src/runtime-types.ts +588 -0
  534. package/packages/workflow-events/dist/src/events.d.ts +38 -0
  535. package/packages/workflow-events/dist/src/events.js +55 -0
  536. package/packages/workflow-events/dist/src/index.d.ts +2 -0
  537. package/packages/workflow-events/dist/src/index.js +2 -0
  538. package/packages/workflow-events/dist/src/pipeline.d.ts +21 -0
  539. package/packages/workflow-events/dist/src/pipeline.js +36 -0
  540. package/packages/workflow-events/dist/src/types.d.ts +110 -0
  541. package/packages/workflow-events/dist/src/types.js +5 -0
  542. package/packages/workflow-events/src/events.ts +86 -0
  543. package/packages/workflow-events/src/index.ts +19 -0
  544. package/packages/workflow-events/src/pipeline.ts +41 -0
  545. package/packages/workflow-events/src/types.d.ts +110 -0
  546. package/packages/workflow-events/src/types.js +5 -0
  547. package/packages/workflow-events/src/types.ts +139 -0
  548. package/packages/workspace-scanner/dist/src/detect.d.ts +3 -0
  549. package/packages/workspace-scanner/dist/src/detect.js +60 -0
  550. package/packages/workspace-scanner/dist/src/git.d.ts +7 -0
  551. package/packages/workspace-scanner/dist/src/git.js +75 -0
  552. package/packages/workspace-scanner/dist/src/index.d.ts +5 -0
  553. package/packages/workspace-scanner/dist/src/index.js +4 -0
  554. package/packages/workspace-scanner/dist/src/scanner.d.ts +11 -0
  555. package/packages/workspace-scanner/dist/src/scanner.js +59 -0
  556. package/packages/workspace-scanner/dist/src/types.d.ts +35 -0
  557. package/packages/workspace-scanner/dist/src/types.js +5 -0
  558. package/packages/workspace-scanner/src/detect.ts +77 -0
  559. package/packages/workspace-scanner/src/git.ts +83 -0
  560. package/packages/workspace-scanner/src/index.ts +9 -0
  561. package/packages/workspace-scanner/src/scanner.ts +73 -0
  562. package/packages/workspace-scanner/src/types.ts +48 -0
  563. package/packages/worktree/dist/src/index.d.ts +7 -0
  564. package/packages/worktree/dist/src/index.js +7 -0
  565. package/packages/worktree/dist/src/worktree.d.ts +88 -0
  566. package/packages/worktree/dist/src/worktree.js +239 -0
  567. package/packages/worktree/src/index.ts +7 -0
  568. package/packages/worktree/src/worktree.ts +290 -0
  569. package/packages/write-review/src/blackboard.ts +249 -0
  570. package/packages/write-review/src/gate.ts +169 -0
  571. package/packages/write-review/src/index.ts +18 -0
  572. package/packages/write-review/src/injection.ts +170 -0
  573. package/packages/write-review/src/review.ts +206 -0
  574. package/packages/write-review/src/trigger.ts +133 -0
  575. package/packages/write-review/src/types.ts +90 -0
  576. package/proactive-compact.ts +90 -0
  577. package/prompts/tencentdb-connect.md +35 -0
  578. package/prompts/tencentdb-setup.md +101 -0
  579. package/prompts/tencentdb.md +88 -0
  580. package/renderer.ts +205 -0
  581. package/skills/harness-runtime/SKILL.md +237 -0
  582. package/status-parsers.ts +84 -0
  583. package/tracker.ts +72 -0
  584. package/windows.ts +121 -0
@@ -0,0 +1,551 @@
1
+ /**
2
+ * Context Compact Orchestrator — Integration Layer
3
+ *
4
+ * Connects token estimation + microcompact + full compact into the harness loop.
5
+ * Enhanced with proper proactive/reactive compact and circuit breaker.
6
+ *
7
+ * Architecture:
8
+ * LoopRuntime.executeTask()
9
+ * +-> CompactOrchestrator.invokeWithCompact()
10
+ * +-> beforeInvoke():
11
+ * | 1. Estimate token count with buffer awareness
12
+ * | 2. Check circuit breaker status
13
+ * | 3. Microcompact: prune old tool results if time-based
14
+ * | 4. If over autoCompactThreshold → Full compact via forked summarization
15
+ * +-> afterInvoke():
16
+ * 1. Update token stats
17
+ * 2. Handle reactive 413 (context too long)
18
+ * 3. Generate continue prompt
19
+ */
20
+
21
+ import type {
22
+ CompactableMessage,
23
+ InvokeWithCompactOptions,
24
+ InvokeResult,
25
+ CompactResult,
26
+ CompactTriggerReason,
27
+ } from "../packages/types/src/runtime-types.js";
28
+ import {
29
+ roughTokenCount,
30
+ roughMessagesTokens,
31
+ } from "../packages/token-estimation/src/index.js";
32
+ import {
33
+ ContextWindowManager,
34
+ microcompactToolResults,
35
+ parseTokenGapFromError,
36
+ AUTOCOMPACT_BUFFER_TOKENS,
37
+ MAX_CONSECUTIVE_COMPACT_FAILURES,
38
+ } from "./context-window-manager.js";
39
+ import { continuePromptGenerator } from "./continue-prompt.js";
40
+
41
+ // --- Re-exports ----------------------------------------------------------------
42
+
43
+ /** Re-export CompactTriggerReason so harness/index.ts can re-export from here */
44
+ export type { CompactTriggerReason } from "../packages/types/src/runtime-types.js";
45
+
46
+ // --- Callbacks ----------------------------------------------------------------
47
+
48
+ /** Callbacks provided by the harness to handle compact side-effects */
49
+ export interface CompactOrchestratorCallbacks {
50
+ /**
51
+ * The actual LLM invocation.
52
+ * Required unless opts.invokeAgent is passed to invokeWithCompact.
53
+ */
54
+ invokeAgent?: (opts: InvokeWithCompactOptions) => Promise<InvokeResult>;
55
+ /** Called after every successful compaction with a summary */
56
+ onCheckpoint?: (result: CompactResult) => void;
57
+ /** Called when quota events occur during compact */
58
+ onQuotaEvent?: (
59
+ event: "paused" | "resumed" | "exhausted",
60
+ provider?: string,
61
+ ) => void;
62
+ /** Called before compact starts */
63
+ onPreCompact?: (reason: CompactTriggerReason) => void;
64
+ /** Called after compact completes */
65
+ onPostCompact?: (result: CompactResult) => void;
66
+ /**
67
+ * Forked summarization: summarize old messages via a separate LLM call.
68
+ * Returns { summary, droppedCount }.
69
+ * If not provided, falls back to heuristic truncation.
70
+ */
71
+ summarizeViaForkedAgent?: (
72
+ messages: CompactableMessage[],
73
+ reason: CompactTriggerReason,
74
+ ) => Promise<{ summary: string; droppedCount: number }>;
75
+ }
76
+
77
+ // --- Config -------------------------------------------------------------------
78
+
79
+ export interface CompactOrchestratorConfig {
80
+ jobId: string;
81
+ /** Provider name for ContextWindowManager (default: "openai") */
82
+ provider?: string;
83
+ /** Model for token estimation (default: "gpt-4") */
84
+ model?: string;
85
+ /** Trigger auto-compact at this usage pct (0-1). Default: 0.85 */
86
+ autoCompactThreshold?: number;
87
+ /** Stop API calls at this pct (0-1). Default: 0.97 */
88
+ blockingThreshold?: number;
89
+ /** Max compact attempts before circuit breaker. Default: 3 */
90
+ maxCompactAttempts?: number;
91
+ /** Time gap (ms) after which to microcompact old tool results. Default: 30 min */
92
+ microcompactTimeGapMs?: number;
93
+ /** Keep this many recent tool results during microcompact. Default: 2 */
94
+ microcompactKeepRecent?: number;
95
+ /** Context window size for the model */
96
+ contextWindowSize?: number;
97
+ }
98
+
99
+ // --- Orchestrator -------------------------------------------------------------
100
+
101
+ /**
102
+ * Compact Orchestrator
103
+ *
104
+ * Wraps LLM invocations with compact checks.
105
+ * Call invokeWithCompact() instead of calling the LLM directly.
106
+ *
107
+ * Usage:
108
+ * const orchestrator = new CompactOrchestrator({ jobId: "job-1" });
109
+ * const result = await orchestrator.invokeWithCompact(
110
+ * { messages, model, maxOutputTokens, tools },
111
+ * { onCheckpoint, onQuotaEvent, summarizeViaForkedAgent }
112
+ * );
113
+ */
114
+ export class CompactOrchestrator {
115
+ private readonly provider: string;
116
+ private readonly model: string;
117
+ private readonly autoCompactThreshold: number;
118
+ private readonly blockingThreshold: number;
119
+ private readonly maxCompactAttempts: number;
120
+ private readonly microcompactTimeGapMs: number;
121
+ private readonly microcompactKeepRecent: number;
122
+ private readonly contextWindow: ContextWindowManager;
123
+
124
+ private compactAttempts = 0;
125
+ private lastAssistantTimestamp = 0;
126
+
127
+ // Tool result tracking for microcompact
128
+ private toolResultsById = new Map<
129
+ string,
130
+ { timestamp: number; tokens: number }
131
+ >();
132
+
133
+ constructor(config: CompactOrchestratorConfig) {
134
+ this.provider = config.provider ?? "openai";
135
+ this.model = config.model ?? "gpt-4";
136
+ this.autoCompactThreshold = config.autoCompactThreshold ?? 0.85;
137
+ this.blockingThreshold = config.blockingThreshold ?? 0.97;
138
+ this.maxCompactAttempts =
139
+ config.maxCompactAttempts ?? MAX_CONSECUTIVE_COMPACT_FAILURES;
140
+ this.microcompactTimeGapMs = config.microcompactTimeGapMs ?? 30 * 60 * 1000;
141
+ this.microcompactKeepRecent = config.microcompactKeepRecent ?? 2;
142
+ this.contextWindow = new ContextWindowManager();
143
+
144
+ // Set context window size if provided
145
+ if (config.contextWindowSize) {
146
+ this.contextWindow.setContextWindow(
147
+ this.provider,
148
+ this.model,
149
+ config.contextWindowSize,
150
+ );
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Main entry point: invoke LLM with compact checks.
156
+ *
157
+ * @param opts - InvokeWithCompactOptions (messages array is modified in-place on compact)
158
+ * @param callbacks - CompactOrchestratorCallbacks (for side-effects)
159
+ */
160
+ async invokeWithCompact(
161
+ opts: InvokeWithCompactOptions,
162
+ callbacks: CompactOrchestratorCallbacks,
163
+ ): Promise<
164
+ InvokeResult & { compactResult?: CompactResult; continueMessage?: string }
165
+ > {
166
+ const { messages, model, maxOutputTokens } = opts;
167
+ this.compactAttempts++;
168
+
169
+ // -- Estimate tokens with buffer awareness -----------------------------
170
+ const estimate = this.contextWindow.estimateTokensWithBuffer({
171
+ messages,
172
+ tools: opts.tools,
173
+ systemPrompt: opts.systemPrompt,
174
+ provider: this.provider,
175
+ model: model ?? this.model,
176
+ });
177
+
178
+ // -- Circuit breaker check ---------------------------------------------
179
+ if (this.contextWindow.shouldCircuitBreak()) {
180
+ return {
181
+ success: false,
182
+ error: `Compact circuit breaker engaged after ${this.contextWindow.getConsecutiveFailures()} consecutive failures.`,
183
+ };
184
+ }
185
+
186
+ // -- Blocking: must compact before API call ---------------------------
187
+ if (this.contextWindow.shouldBlockApiCall(estimate)) {
188
+ callbacks.onPreCompact?.("token_threshold");
189
+ const compact = await this.runFullCompact(
190
+ messages,
191
+ model ?? this.model,
192
+ maxOutputTokens,
193
+ "token_threshold",
194
+ callbacks,
195
+ );
196
+ if (!compact.success) {
197
+ return {
198
+ success: false,
199
+ error: `Context too large and compact failed: ${compact.error}`,
200
+ };
201
+ }
202
+ if (compact.result) {
203
+ callbacks.onPostCompact?.(compact.result);
204
+ callbacks.onCheckpoint?.(compact.result);
205
+ }
206
+ this.contextWindow.recordCompactSuccess();
207
+ }
208
+
209
+ // -- Time-based microcompact -----------------------------------------
210
+ this.tryMicrocompact(messages);
211
+
212
+ // -- Proactive auto-compact at 85% ----------------------------------
213
+ if (this.contextWindow.shouldProactiveCompact(estimate)) {
214
+ callbacks.onPreCompact?.("token_threshold");
215
+ const compact = await this.runFullCompact(
216
+ messages,
217
+ model ?? this.model,
218
+ maxOutputTokens,
219
+ "token_threshold",
220
+ callbacks,
221
+ );
222
+ if (compact.success) {
223
+ this.contextWindow.recordCompactSuccess();
224
+ if (compact.result) {
225
+ callbacks.onPostCompact?.(compact.result);
226
+ callbacks.onCheckpoint?.(compact.result);
227
+ }
228
+ }
229
+ }
230
+
231
+ // -- Make the LLM call -----------------------------------------------
232
+ const invoke = opts.invokeAgent ?? callbacks.invokeAgent;
233
+ if (!invoke) {
234
+ return {
235
+ success: false,
236
+ error:
237
+ "No invokeAgent provided — must pass invokeAgent in opts or callbacks",
238
+ };
239
+ }
240
+ const result = await invoke!(opts);
241
+
242
+ // Track tool results
243
+ this.trackToolResults(messages);
244
+
245
+ // -- Update stats -----------------------------------------------------
246
+ if (result.usage) {
247
+ this.contextWindow.updateStats({
248
+ provider: this.provider,
249
+ model: model ?? this.model,
250
+ maxTokens: estimate.total + (result.usage.outputTokens ?? 0),
251
+ usedTokens: result.usage.inputTokens ?? estimate.total,
252
+ });
253
+ }
254
+
255
+ // -- Reactive compact on context-too-long error ---------------------
256
+ const isContextError = this.isContextTooLongError(result.error);
257
+
258
+ if (isContextError) {
259
+ const failures = this.contextWindow.recordCompactFailure();
260
+ if (this.contextWindow.shouldCircuitBreak()) {
261
+ return {
262
+ success: false,
263
+ error: `Context too long, compact circuit breaker engaged after ${failures} failures.`,
264
+ };
265
+ }
266
+
267
+ callbacks.onPreCompact?.("output_limit");
268
+ const compact = await this.runFullCompact(
269
+ messages,
270
+ model ?? this.model,
271
+ maxOutputTokens,
272
+ "output_limit",
273
+ callbacks,
274
+ );
275
+
276
+ if (compact.success) {
277
+ this.contextWindow.recordCompactSuccess();
278
+ if (compact.result) {
279
+ callbacks.onPostCompact?.(compact.result);
280
+ callbacks.onCheckpoint?.(compact.result);
281
+ }
282
+
283
+ // Retry once after compact
284
+ const retryResult = await invoke!(opts);
285
+ if (retryResult.success) {
286
+ this.lastAssistantTimestamp = Date.now();
287
+ return retryResult;
288
+ }
289
+ }
290
+ }
291
+
292
+ if (result.success) {
293
+ this.lastAssistantTimestamp = Date.now();
294
+ }
295
+
296
+ return result;
297
+ }
298
+
299
+ /**
300
+ * Run full compact: summarize old messages.
301
+ */
302
+ private async runFullCompact(
303
+ messages: CompactableMessage[],
304
+ model: string,
305
+ maxOutputTokens: number,
306
+ reason: CompactTriggerReason,
307
+ callbacks: CompactOrchestratorCallbacks,
308
+ ): Promise<{
309
+ success: boolean;
310
+ result?: CompactResult;
311
+ error?: string;
312
+ continueMessage?: string;
313
+ }> {
314
+ const beforeTokens = this.estimateTokens({
315
+ messages,
316
+ model,
317
+ maxOutputTokens,
318
+ });
319
+
320
+ // Keep last 5-10 messages; compact everything before that
321
+ const keepRecent = Math.min(10, Math.floor(messages.length * 0.2));
322
+ const recentMessages = messages.slice(-keepRecent);
323
+ const oldMessages = messages.slice(0, -keepRecent);
324
+
325
+ if (oldMessages.length === 0) {
326
+ return { success: false, error: "Nothing to compact" };
327
+ }
328
+
329
+ // -- Generate summary ------------------------------------------------
330
+ let summary = "";
331
+ let droppedCount = oldMessages.length;
332
+
333
+ if (callbacks.summarizeViaForkedAgent) {
334
+ try {
335
+ const result = await callbacks.summarizeViaForkedAgent(
336
+ oldMessages,
337
+ reason,
338
+ );
339
+ summary = result.summary;
340
+ droppedCount = result.droppedCount;
341
+ } catch {
342
+ summary = this.heuristicSummary(oldMessages, reason);
343
+ }
344
+ } else {
345
+ summary = this.heuristicSummary(oldMessages, reason);
346
+ }
347
+
348
+ // -- Build compact boundary message -----------------------------------
349
+ const boundaryMsg: CompactableMessage = {
350
+ role: "system",
351
+ content: continuePromptGenerator.generateBoundary({
352
+ summary,
353
+ reason,
354
+ messagesCompacted: droppedCount,
355
+ }),
356
+ timestamp: Date.now(),
357
+ metadata: {
358
+ compactBoundary: true,
359
+ reason,
360
+ compactedMessages: droppedCount,
361
+ },
362
+ };
363
+
364
+ // -- Preserve tool results from recent messages -----------------------
365
+ const preservedRecent = recentMessages.map((msg) => {
366
+ if (msg.role !== "assistant" || !msg.toolResults) return msg;
367
+ return {
368
+ ...msg,
369
+ toolResults: this.preserveRecentToolResults(
370
+ msg,
371
+ this.microcompactKeepRecent,
372
+ ),
373
+ };
374
+ });
375
+
376
+ // -- Rebuild message array -------------------------------------------
377
+ messages.length = 0;
378
+ messages.push(boundaryMsg, ...preservedRecent);
379
+
380
+ const afterTokens = this.estimateTokens({
381
+ messages,
382
+ model,
383
+ maxOutputTokens,
384
+ });
385
+
386
+ const result: CompactResult = {
387
+ trigger: reason,
388
+ beforeTokens,
389
+ afterTokens,
390
+ messagesCompacted: droppedCount,
391
+ summary,
392
+ };
393
+
394
+ const continueMessage = continuePromptGenerator.generateMinimal({
395
+ summary,
396
+ recentMessages: preservedRecent,
397
+ });
398
+
399
+ return { success: true, result, continueMessage };
400
+ }
401
+
402
+ /**
403
+ * Preserve only recent N tool results per message
404
+ */
405
+ private preserveRecentToolResults(
406
+ msg: CompactableMessage,
407
+ keepCount: number,
408
+ ): CompactableMessage["toolResults"] {
409
+ if (!msg.toolResults) return undefined;
410
+
411
+ const sorted = [...msg.toolResults]
412
+ .sort((a, b) => b.timestamp - a.timestamp)
413
+ .slice(0, keepCount);
414
+
415
+ return sorted.map((tr) => ({
416
+ ...tr,
417
+ content: tr.content.substring(0, 1000),
418
+ }));
419
+ }
420
+
421
+ /**
422
+ * Heuristic summary when LLM summarization unavailable
423
+ */
424
+ private heuristicSummary(
425
+ messages: CompactableMessage[],
426
+ reason: CompactTriggerReason,
427
+ ): string {
428
+ const summaries: string[] = [];
429
+
430
+ // Extract key information from last 20 messages
431
+ for (const msg of messages.slice(-20)) {
432
+ if (msg.role === "user" && msg.content.length > 50) {
433
+ summaries.push(`- User: ${msg.content.substring(0, 200)}`);
434
+ }
435
+ if (msg.metadata?.action) {
436
+ summaries.push(`- Action: ${msg.metadata.action}`);
437
+ }
438
+ }
439
+
440
+ return [
441
+ `[Context compacted due to: ${reason}]`,
442
+ "",
443
+ "The conversation history was too long to fit in the context window.",
444
+ "Key context from earlier conversation:",
445
+ "",
446
+ summaries.join("\n") || "- Conversation was in progress",
447
+ "",
448
+ "Please continue from the recent messages provided.",
449
+ ].join("\n");
450
+ }
451
+
452
+ /**
453
+ * Microcompact: prune old tool results based on time gap.
454
+ * Uses the microcompactToolResults helper from context-window-manager.
455
+ */
456
+ private tryMicrocompact(messages: CompactableMessage[]): void {
457
+ if (Date.now() - this.lastAssistantTimestamp < this.microcompactTimeGapMs) {
458
+ return;
459
+ }
460
+
461
+ const freed = microcompactToolResults(messages, {
462
+ maxAgeMs: this.microcompactTimeGapMs,
463
+ keepRecent: this.microcompactKeepRecent,
464
+ });
465
+
466
+ if (freed > 0) {
467
+ this.lastAssistantTimestamp = Date.now();
468
+ }
469
+ }
470
+
471
+ /**
472
+ * Check if error is a context-too-long error
473
+ */
474
+ private isContextTooLongError(error: string | undefined): boolean {
475
+ if (!error) return false;
476
+ return (
477
+ /context.*(length|window).*exceed/i.test(error) ||
478
+ /too many (tokens|input tokens)/i.test(error) ||
479
+ /413/i.test(error) ||
480
+ /prompt is too long/i.test(error)
481
+ );
482
+ }
483
+
484
+ /**
485
+ * Estimate token count for an invoke call.
486
+ */
487
+ estimateTokens(opts: InvokeWithCompactOptions): number {
488
+ const messageTokens = roughMessagesTokens(
489
+ opts.messages.map((m) => ({ role: m.role, content: m.content })),
490
+ );
491
+ const toolTokens = (opts.tools ?? []).reduce(
492
+ (sum, tool) =>
493
+ sum +
494
+ roughTokenCount(tool.name) +
495
+ roughTokenCount(tool.description ?? "") +
496
+ roughTokenCount(JSON.stringify(tool.input_schema ?? {})),
497
+ 0,
498
+ );
499
+ return messageTokens + toolTokens;
500
+ }
501
+
502
+ /**
503
+ * Track tool results for microcompact decisions.
504
+ */
505
+ private trackToolResults(messages: CompactableMessage[]): void {
506
+ for (const msg of messages) {
507
+ if (msg.role !== "assistant" || !msg.toolResults) continue;
508
+ for (const tr of msg.toolResults) {
509
+ this.toolResultsById.set(tr.id, {
510
+ timestamp: tr.timestamp,
511
+ tokens: tr.tokens,
512
+ });
513
+ }
514
+ }
515
+ }
516
+
517
+ /** Get current compact attempt count */
518
+ getCompactAttempts(): number {
519
+ return this.compactAttempts;
520
+ }
521
+
522
+ /** Get consecutive failure count */
523
+ getConsecutiveFailures(): number {
524
+ return this.contextWindow.getConsecutiveFailures();
525
+ }
526
+
527
+ /** Check if circuit breaker is engaged */
528
+ isCircuitBroken(): boolean {
529
+ return this.contextWindow.shouldCircuitBreak();
530
+ }
531
+
532
+ /** Reset compact state for a new task */
533
+ reset(): void {
534
+ this.compactAttempts = 0;
535
+ this.toolResultsById.clear();
536
+ this.contextWindow.resetCircuitBreaker();
537
+ }
538
+
539
+ /** Get utilization status for a model */
540
+ getUtilizationStatus(
541
+ provider: string,
542
+ model: string,
543
+ ): "ok" | "warning" | "critical" | "unknown" {
544
+ return this.contextWindow.getUtilizationStatus(provider, model);
545
+ }
546
+
547
+ /** Generate utilization report */
548
+ generateUtilizationReport(): string {
549
+ return this.contextWindow.generateReport();
550
+ }
551
+ }