gsd-pi 2.18.0 → 2.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (289) hide show
  1. package/README.md +5 -1
  2. package/dist/cli.js +3 -3
  3. package/dist/onboarding.d.ts +3 -1
  4. package/dist/onboarding.js +77 -3
  5. package/dist/remote-questions-config.d.ts +1 -1
  6. package/dist/resources/extensions/google-search/index.ts +164 -47
  7. package/dist/resources/extensions/gsd/auto-dashboard.ts +14 -2
  8. package/dist/resources/extensions/gsd/auto-prompts.ts +148 -39
  9. package/dist/resources/extensions/gsd/auto-worktree.ts +93 -9
  10. package/dist/resources/extensions/gsd/auto.ts +690 -39
  11. package/dist/resources/extensions/gsd/captures.ts +384 -0
  12. package/dist/resources/extensions/gsd/commands.ts +654 -36
  13. package/dist/resources/extensions/gsd/complexity-classifier.ts +322 -0
  14. package/dist/resources/extensions/gsd/context-budget.ts +243 -0
  15. package/dist/resources/extensions/gsd/context-store.ts +195 -0
  16. package/dist/resources/extensions/gsd/dashboard-overlay.ts +51 -3
  17. package/dist/resources/extensions/gsd/db-writer.ts +341 -0
  18. package/dist/resources/extensions/gsd/debug-logger.ts +178 -0
  19. package/dist/resources/extensions/gsd/dispatch-guard.ts +0 -1
  20. package/dist/resources/extensions/gsd/docs/preferences-reference.md +54 -0
  21. package/dist/resources/extensions/gsd/doctor-proactive.ts +286 -0
  22. package/dist/resources/extensions/gsd/doctor.ts +283 -2
  23. package/dist/resources/extensions/gsd/export.ts +81 -2
  24. package/dist/resources/extensions/gsd/files.ts +39 -9
  25. package/dist/resources/extensions/gsd/git-service.ts +6 -0
  26. package/dist/resources/extensions/gsd/gsd-db.ts +752 -0
  27. package/dist/resources/extensions/gsd/guided-flow.ts +26 -1
  28. package/dist/resources/extensions/gsd/history.ts +0 -1
  29. package/dist/resources/extensions/gsd/index.ts +277 -1
  30. package/dist/resources/extensions/gsd/md-importer.ts +526 -0
  31. package/dist/resources/extensions/gsd/metrics.ts +84 -0
  32. package/dist/resources/extensions/gsd/model-cost-table.ts +65 -0
  33. package/dist/resources/extensions/gsd/model-router.ts +256 -0
  34. package/dist/resources/extensions/gsd/notifications.ts +0 -1
  35. package/dist/resources/extensions/gsd/post-unit-hooks.ts +72 -2
  36. package/dist/resources/extensions/gsd/preferences.ts +198 -150
  37. package/dist/resources/extensions/gsd/prompt-loader.ts +45 -9
  38. package/dist/resources/extensions/gsd/prompts/execute-task.md +3 -5
  39. package/dist/resources/extensions/gsd/prompts/heal-skill.md +45 -0
  40. package/dist/resources/extensions/gsd/prompts/plan-slice.md +5 -1
  41. package/dist/resources/extensions/gsd/prompts/quick-task.md +48 -0
  42. package/dist/resources/extensions/gsd/prompts/reassess-roadmap.md +6 -0
  43. package/dist/resources/extensions/gsd/prompts/replan-slice.md +8 -0
  44. package/dist/resources/extensions/gsd/prompts/system.md +2 -1
  45. package/dist/resources/extensions/gsd/prompts/triage-captures.md +62 -0
  46. package/dist/resources/extensions/gsd/quick.ts +156 -0
  47. package/dist/resources/extensions/gsd/skill-discovery.ts +5 -3
  48. package/dist/resources/extensions/gsd/skill-health.ts +417 -0
  49. package/dist/resources/extensions/gsd/skill-telemetry.ts +127 -0
  50. package/dist/resources/extensions/gsd/state.ts +30 -0
  51. package/dist/resources/extensions/gsd/templates/preferences.md +1 -0
  52. package/dist/resources/extensions/gsd/tests/captures.test.ts +438 -0
  53. package/dist/resources/extensions/gsd/tests/complexity-classifier.test.ts +181 -0
  54. package/dist/resources/extensions/gsd/tests/context-budget.test.ts +283 -0
  55. package/dist/resources/extensions/gsd/tests/context-compression.test.ts +1 -1
  56. package/dist/resources/extensions/gsd/tests/context-store.test.ts +462 -0
  57. package/dist/resources/extensions/gsd/tests/continue-here.test.ts +204 -0
  58. package/dist/resources/extensions/gsd/tests/dashboard-budget.test.ts +346 -0
  59. package/dist/resources/extensions/gsd/tests/db-writer.test.ts +602 -0
  60. package/dist/resources/extensions/gsd/tests/debug-logger.test.ts +185 -0
  61. package/dist/resources/extensions/gsd/tests/derive-state-db.test.ts +406 -0
  62. package/dist/resources/extensions/gsd/tests/dispatch-guard.test.ts +0 -1
  63. package/dist/resources/extensions/gsd/tests/dist-redirect.mjs +22 -0
  64. package/dist/resources/extensions/gsd/tests/doctor-proactive.test.ts +244 -0
  65. package/dist/resources/extensions/gsd/tests/doctor-runtime.test.ts +303 -0
  66. package/dist/resources/extensions/gsd/tests/feature-branch-lifecycle-integration.test.ts +434 -0
  67. package/dist/resources/extensions/gsd/tests/gsd-db.test.ts +353 -0
  68. package/dist/resources/extensions/gsd/tests/gsd-inspect.test.ts +125 -0
  69. package/dist/resources/extensions/gsd/tests/gsd-tools.test.ts +326 -0
  70. package/dist/resources/extensions/gsd/tests/integration-edge.test.ts +228 -0
  71. package/dist/resources/extensions/gsd/tests/integration-lifecycle.test.ts +277 -0
  72. package/dist/resources/extensions/gsd/tests/md-importer.test.ts +411 -0
  73. package/dist/resources/extensions/gsd/tests/metrics.test.ts +197 -0
  74. package/dist/resources/extensions/gsd/tests/milestone-transition-worktree.test.ts +144 -0
  75. package/dist/resources/extensions/gsd/tests/model-cost-table.test.ts +69 -0
  76. package/dist/resources/extensions/gsd/tests/model-isolation.test.ts +99 -0
  77. package/dist/resources/extensions/gsd/tests/model-router.test.ts +167 -0
  78. package/dist/resources/extensions/gsd/tests/parsers.test.ts +40 -0
  79. package/dist/resources/extensions/gsd/tests/post-unit-hooks.test.ts +41 -1
  80. package/dist/resources/extensions/gsd/tests/preferences-git.test.ts +0 -1
  81. package/dist/resources/extensions/gsd/tests/preferences-hooks.test.ts +0 -1
  82. package/dist/resources/extensions/gsd/tests/preferences-mode.test.ts +110 -0
  83. package/dist/resources/extensions/gsd/tests/preferences-models.test.ts +0 -1
  84. package/dist/resources/extensions/gsd/tests/prompt-budget-enforcement.test.ts +464 -0
  85. package/dist/resources/extensions/gsd/tests/prompt-db.test.ts +385 -0
  86. package/dist/resources/extensions/gsd/tests/remote-questions.test.ts +488 -1
  87. package/dist/resources/extensions/gsd/tests/resolve-ts-hooks.mjs +17 -29
  88. package/dist/resources/extensions/gsd/tests/resolve-ts.mjs +2 -8
  89. package/dist/resources/extensions/gsd/tests/routing-history.test.ts +215 -62
  90. package/dist/resources/extensions/gsd/tests/skill-lifecycle.test.ts +126 -0
  91. package/dist/resources/extensions/gsd/tests/stop-auto-remote.test.ts +31 -8
  92. package/dist/resources/extensions/gsd/tests/token-savings.test.ts +366 -0
  93. package/dist/resources/extensions/gsd/tests/triage-dispatch.test.ts +224 -0
  94. package/dist/resources/extensions/gsd/tests/triage-resolution.test.ts +215 -0
  95. package/dist/resources/extensions/gsd/tests/unit-runtime.test.ts +25 -1
  96. package/dist/resources/extensions/gsd/tests/visualizer-critical-path.test.ts +145 -0
  97. package/dist/resources/extensions/gsd/tests/visualizer-data.test.ts +290 -0
  98. package/dist/resources/extensions/gsd/tests/visualizer-overlay.test.ts +120 -0
  99. package/dist/resources/extensions/gsd/tests/visualizer-views.test.ts +478 -0
  100. package/dist/resources/extensions/gsd/tests/worktree-db-integration.test.ts +205 -0
  101. package/dist/resources/extensions/gsd/tests/worktree-db.test.ts +442 -0
  102. package/dist/resources/extensions/gsd/tests/worktree-post-create-hook.test.ts +165 -0
  103. package/dist/resources/extensions/gsd/triage-resolution.ts +200 -0
  104. package/dist/resources/extensions/gsd/triage-ui.ts +175 -0
  105. package/dist/resources/extensions/gsd/types.ts +29 -0
  106. package/dist/resources/extensions/gsd/undo.ts +0 -1
  107. package/dist/resources/extensions/gsd/unit-runtime.ts +5 -1
  108. package/dist/resources/extensions/gsd/visualizer-data.ts +505 -0
  109. package/dist/resources/extensions/gsd/visualizer-overlay.ts +337 -0
  110. package/dist/resources/extensions/gsd/visualizer-views.ts +755 -0
  111. package/dist/resources/extensions/gsd/worktree-command.ts +18 -0
  112. package/dist/resources/extensions/gsd/worktree-manager.ts +11 -4
  113. package/dist/resources/extensions/remote-questions/config.ts +4 -2
  114. package/dist/resources/extensions/remote-questions/discord-adapter.ts +35 -4
  115. package/dist/resources/extensions/remote-questions/format.ts +166 -14
  116. package/dist/resources/extensions/remote-questions/manager.ts +14 -4
  117. package/dist/resources/extensions/remote-questions/remote-command.ts +100 -4
  118. package/dist/resources/extensions/remote-questions/slack-adapter.ts +58 -2
  119. package/dist/resources/extensions/remote-questions/telegram-adapter.ts +161 -0
  120. package/dist/resources/extensions/remote-questions/types.ts +2 -1
  121. package/dist/resources/extensions/ttsr/ttsr-manager.ts +26 -0
  122. package/dist/resources/extensions/voice/index.ts +4 -3
  123. package/package.json +1 -1
  124. package/packages/pi-coding-agent/dist/core/agent-session.d.ts.map +1 -1
  125. package/packages/pi-coding-agent/dist/core/agent-session.js +12 -1
  126. package/packages/pi-coding-agent/dist/core/agent-session.js.map +1 -1
  127. package/packages/pi-coding-agent/dist/core/extensions/loader.d.ts.map +1 -1
  128. package/packages/pi-coding-agent/dist/core/extensions/loader.js +5 -0
  129. package/packages/pi-coding-agent/dist/core/extensions/loader.js.map +1 -1
  130. package/packages/pi-coding-agent/dist/core/lsp/client.d.ts +6 -0
  131. package/packages/pi-coding-agent/dist/core/lsp/client.d.ts.map +1 -1
  132. package/packages/pi-coding-agent/dist/core/lsp/client.js +25 -0
  133. package/packages/pi-coding-agent/dist/core/lsp/client.js.map +1 -1
  134. package/packages/pi-coding-agent/dist/core/lsp/index.d.ts +2 -0
  135. package/packages/pi-coding-agent/dist/core/lsp/index.d.ts.map +1 -1
  136. package/packages/pi-coding-agent/dist/core/lsp/index.js +106 -3
  137. package/packages/pi-coding-agent/dist/core/lsp/index.js.map +1 -1
  138. package/packages/pi-coding-agent/dist/core/lsp/lsp.md +6 -0
  139. package/packages/pi-coding-agent/dist/core/lsp/types.d.ts +35 -0
  140. package/packages/pi-coding-agent/dist/core/lsp/types.d.ts.map +1 -1
  141. package/packages/pi-coding-agent/dist/core/lsp/types.js +6 -0
  142. package/packages/pi-coding-agent/dist/core/lsp/types.js.map +1 -1
  143. package/packages/pi-coding-agent/dist/core/lsp/utils.d.ts +3 -1
  144. package/packages/pi-coding-agent/dist/core/lsp/utils.d.ts.map +1 -1
  145. package/packages/pi-coding-agent/dist/core/lsp/utils.js +45 -0
  146. package/packages/pi-coding-agent/dist/core/lsp/utils.js.map +1 -1
  147. package/packages/pi-coding-agent/dist/core/settings-manager.d.ts +6 -0
  148. package/packages/pi-coding-agent/dist/core/settings-manager.d.ts.map +1 -1
  149. package/packages/pi-coding-agent/dist/core/settings-manager.js +43 -11
  150. package/packages/pi-coding-agent/dist/core/settings-manager.js.map +1 -1
  151. package/packages/pi-coding-agent/dist/core/system-prompt.d.ts.map +1 -1
  152. package/packages/pi-coding-agent/dist/core/system-prompt.js +7 -1
  153. package/packages/pi-coding-agent/dist/core/system-prompt.js.map +1 -1
  154. package/packages/pi-coding-agent/dist/core/tools/edit.d.ts.map +1 -1
  155. package/packages/pi-coding-agent/dist/core/tools/edit.js +5 -0
  156. package/packages/pi-coding-agent/dist/core/tools/edit.js.map +1 -1
  157. package/packages/pi-coding-agent/dist/core/tools/index.d.ts +2 -0
  158. package/packages/pi-coding-agent/dist/core/tools/index.d.ts.map +1 -1
  159. package/packages/pi-coding-agent/dist/core/tools/write.d.ts.map +1 -1
  160. package/packages/pi-coding-agent/dist/core/tools/write.js +5 -0
  161. package/packages/pi-coding-agent/dist/core/tools/write.js.map +1 -1
  162. package/packages/pi-coding-agent/src/core/agent-session.ts +13 -1
  163. package/packages/pi-coding-agent/src/core/extensions/loader.ts +6 -0
  164. package/packages/pi-coding-agent/src/core/lsp/client.ts +26 -0
  165. package/packages/pi-coding-agent/src/core/lsp/index.ts +157 -2
  166. package/packages/pi-coding-agent/src/core/lsp/lsp.md +6 -0
  167. package/packages/pi-coding-agent/src/core/lsp/types.ts +53 -0
  168. package/packages/pi-coding-agent/src/core/lsp/utils.ts +56 -0
  169. package/packages/pi-coding-agent/src/core/settings-manager.ts +41 -11
  170. package/packages/pi-coding-agent/src/core/system-prompt.ts +7 -1
  171. package/packages/pi-coding-agent/src/core/tools/edit.ts +3 -0
  172. package/packages/pi-coding-agent/src/core/tools/write.ts +3 -0
  173. package/src/resources/extensions/google-search/index.ts +164 -47
  174. package/src/resources/extensions/gsd/auto-dashboard.ts +14 -2
  175. package/src/resources/extensions/gsd/auto-prompts.ts +148 -39
  176. package/src/resources/extensions/gsd/auto-worktree.ts +93 -9
  177. package/src/resources/extensions/gsd/auto.ts +690 -39
  178. package/src/resources/extensions/gsd/captures.ts +384 -0
  179. package/src/resources/extensions/gsd/commands.ts +654 -36
  180. package/src/resources/extensions/gsd/complexity-classifier.ts +322 -0
  181. package/src/resources/extensions/gsd/context-budget.ts +243 -0
  182. package/src/resources/extensions/gsd/context-store.ts +195 -0
  183. package/src/resources/extensions/gsd/dashboard-overlay.ts +51 -3
  184. package/src/resources/extensions/gsd/db-writer.ts +341 -0
  185. package/src/resources/extensions/gsd/debug-logger.ts +178 -0
  186. package/src/resources/extensions/gsd/dispatch-guard.ts +0 -1
  187. package/src/resources/extensions/gsd/docs/preferences-reference.md +54 -0
  188. package/src/resources/extensions/gsd/doctor-proactive.ts +286 -0
  189. package/src/resources/extensions/gsd/doctor.ts +283 -2
  190. package/src/resources/extensions/gsd/export.ts +81 -2
  191. package/src/resources/extensions/gsd/files.ts +39 -9
  192. package/src/resources/extensions/gsd/git-service.ts +6 -0
  193. package/src/resources/extensions/gsd/gsd-db.ts +752 -0
  194. package/src/resources/extensions/gsd/guided-flow.ts +26 -1
  195. package/src/resources/extensions/gsd/history.ts +0 -1
  196. package/src/resources/extensions/gsd/index.ts +277 -1
  197. package/src/resources/extensions/gsd/md-importer.ts +526 -0
  198. package/src/resources/extensions/gsd/metrics.ts +84 -0
  199. package/src/resources/extensions/gsd/model-cost-table.ts +65 -0
  200. package/src/resources/extensions/gsd/model-router.ts +256 -0
  201. package/src/resources/extensions/gsd/notifications.ts +0 -1
  202. package/src/resources/extensions/gsd/post-unit-hooks.ts +72 -2
  203. package/src/resources/extensions/gsd/preferences.ts +198 -150
  204. package/src/resources/extensions/gsd/prompt-loader.ts +45 -9
  205. package/src/resources/extensions/gsd/prompts/execute-task.md +3 -5
  206. package/src/resources/extensions/gsd/prompts/heal-skill.md +45 -0
  207. package/src/resources/extensions/gsd/prompts/plan-slice.md +5 -1
  208. package/src/resources/extensions/gsd/prompts/quick-task.md +48 -0
  209. package/src/resources/extensions/gsd/prompts/reassess-roadmap.md +6 -0
  210. package/src/resources/extensions/gsd/prompts/replan-slice.md +8 -0
  211. package/src/resources/extensions/gsd/prompts/system.md +2 -1
  212. package/src/resources/extensions/gsd/prompts/triage-captures.md +62 -0
  213. package/src/resources/extensions/gsd/quick.ts +156 -0
  214. package/src/resources/extensions/gsd/skill-discovery.ts +5 -3
  215. package/src/resources/extensions/gsd/skill-health.ts +417 -0
  216. package/src/resources/extensions/gsd/skill-telemetry.ts +127 -0
  217. package/src/resources/extensions/gsd/state.ts +30 -0
  218. package/src/resources/extensions/gsd/templates/preferences.md +1 -0
  219. package/src/resources/extensions/gsd/tests/captures.test.ts +438 -0
  220. package/src/resources/extensions/gsd/tests/complexity-classifier.test.ts +181 -0
  221. package/src/resources/extensions/gsd/tests/context-budget.test.ts +283 -0
  222. package/src/resources/extensions/gsd/tests/context-compression.test.ts +1 -1
  223. package/src/resources/extensions/gsd/tests/context-store.test.ts +462 -0
  224. package/src/resources/extensions/gsd/tests/continue-here.test.ts +204 -0
  225. package/src/resources/extensions/gsd/tests/dashboard-budget.test.ts +346 -0
  226. package/src/resources/extensions/gsd/tests/db-writer.test.ts +602 -0
  227. package/src/resources/extensions/gsd/tests/debug-logger.test.ts +185 -0
  228. package/src/resources/extensions/gsd/tests/derive-state-db.test.ts +406 -0
  229. package/src/resources/extensions/gsd/tests/dispatch-guard.test.ts +0 -1
  230. package/src/resources/extensions/gsd/tests/dist-redirect.mjs +22 -0
  231. package/src/resources/extensions/gsd/tests/doctor-proactive.test.ts +244 -0
  232. package/src/resources/extensions/gsd/tests/doctor-runtime.test.ts +303 -0
  233. package/src/resources/extensions/gsd/tests/feature-branch-lifecycle-integration.test.ts +434 -0
  234. package/src/resources/extensions/gsd/tests/gsd-db.test.ts +353 -0
  235. package/src/resources/extensions/gsd/tests/gsd-inspect.test.ts +125 -0
  236. package/src/resources/extensions/gsd/tests/gsd-tools.test.ts +326 -0
  237. package/src/resources/extensions/gsd/tests/integration-edge.test.ts +228 -0
  238. package/src/resources/extensions/gsd/tests/integration-lifecycle.test.ts +277 -0
  239. package/src/resources/extensions/gsd/tests/md-importer.test.ts +411 -0
  240. package/src/resources/extensions/gsd/tests/metrics.test.ts +197 -0
  241. package/src/resources/extensions/gsd/tests/milestone-transition-worktree.test.ts +144 -0
  242. package/src/resources/extensions/gsd/tests/model-cost-table.test.ts +69 -0
  243. package/src/resources/extensions/gsd/tests/model-isolation.test.ts +99 -0
  244. package/src/resources/extensions/gsd/tests/model-router.test.ts +167 -0
  245. package/src/resources/extensions/gsd/tests/parsers.test.ts +40 -0
  246. package/src/resources/extensions/gsd/tests/post-unit-hooks.test.ts +41 -1
  247. package/src/resources/extensions/gsd/tests/preferences-git.test.ts +0 -1
  248. package/src/resources/extensions/gsd/tests/preferences-hooks.test.ts +0 -1
  249. package/src/resources/extensions/gsd/tests/preferences-mode.test.ts +110 -0
  250. package/src/resources/extensions/gsd/tests/preferences-models.test.ts +0 -1
  251. package/src/resources/extensions/gsd/tests/prompt-budget-enforcement.test.ts +464 -0
  252. package/src/resources/extensions/gsd/tests/prompt-db.test.ts +385 -0
  253. package/src/resources/extensions/gsd/tests/remote-questions.test.ts +488 -1
  254. package/src/resources/extensions/gsd/tests/resolve-ts-hooks.mjs +17 -29
  255. package/src/resources/extensions/gsd/tests/resolve-ts.mjs +2 -8
  256. package/src/resources/extensions/gsd/tests/routing-history.test.ts +215 -62
  257. package/src/resources/extensions/gsd/tests/skill-lifecycle.test.ts +126 -0
  258. package/src/resources/extensions/gsd/tests/stop-auto-remote.test.ts +31 -8
  259. package/src/resources/extensions/gsd/tests/token-savings.test.ts +366 -0
  260. package/src/resources/extensions/gsd/tests/triage-dispatch.test.ts +224 -0
  261. package/src/resources/extensions/gsd/tests/triage-resolution.test.ts +215 -0
  262. package/src/resources/extensions/gsd/tests/unit-runtime.test.ts +25 -1
  263. package/src/resources/extensions/gsd/tests/visualizer-critical-path.test.ts +145 -0
  264. package/src/resources/extensions/gsd/tests/visualizer-data.test.ts +290 -0
  265. package/src/resources/extensions/gsd/tests/visualizer-overlay.test.ts +120 -0
  266. package/src/resources/extensions/gsd/tests/visualizer-views.test.ts +478 -0
  267. package/src/resources/extensions/gsd/tests/worktree-db-integration.test.ts +205 -0
  268. package/src/resources/extensions/gsd/tests/worktree-db.test.ts +442 -0
  269. package/src/resources/extensions/gsd/tests/worktree-post-create-hook.test.ts +165 -0
  270. package/src/resources/extensions/gsd/triage-resolution.ts +200 -0
  271. package/src/resources/extensions/gsd/triage-ui.ts +175 -0
  272. package/src/resources/extensions/gsd/types.ts +29 -0
  273. package/src/resources/extensions/gsd/undo.ts +0 -1
  274. package/src/resources/extensions/gsd/unit-runtime.ts +5 -1
  275. package/src/resources/extensions/gsd/visualizer-data.ts +505 -0
  276. package/src/resources/extensions/gsd/visualizer-overlay.ts +337 -0
  277. package/src/resources/extensions/gsd/visualizer-views.ts +755 -0
  278. package/src/resources/extensions/gsd/worktree-command.ts +18 -0
  279. package/src/resources/extensions/gsd/worktree-manager.ts +11 -4
  280. package/src/resources/extensions/remote-questions/config.ts +4 -2
  281. package/src/resources/extensions/remote-questions/discord-adapter.ts +35 -4
  282. package/src/resources/extensions/remote-questions/format.ts +166 -14
  283. package/src/resources/extensions/remote-questions/manager.ts +14 -4
  284. package/src/resources/extensions/remote-questions/remote-command.ts +100 -4
  285. package/src/resources/extensions/remote-questions/slack-adapter.ts +58 -2
  286. package/src/resources/extensions/remote-questions/telegram-adapter.ts +161 -0
  287. package/src/resources/extensions/remote-questions/types.ts +2 -1
  288. package/src/resources/extensions/ttsr/ttsr-manager.ts +26 -0
  289. package/src/resources/extensions/voice/index.ts +4 -3
@@ -0,0 +1,322 @@
1
+ // GSD Extension — Complexity Classifier
2
+ // Classifies unit complexity for dynamic model routing.
3
+ // Pure heuristics + adaptive learning — no LLM calls. Sub-millisecond classification.
4
+
5
+ import { existsSync, readFileSync } from "node:fs";
6
+ import { join } from "node:path";
7
+ import { gsdRoot } from "./paths.js";
8
+ import { getAdaptiveTierAdjustment } from "./routing-history.js";
9
+
10
+ // ─── Types ───────────────────────────────────────────────────────────────────
11
+
12
+ export type ComplexityTier = "light" | "standard" | "heavy";
13
+
14
+ export interface ClassificationResult {
15
+ tier: ComplexityTier;
16
+ reason: string;
17
+ downgraded: boolean; // true if budget pressure lowered the tier
18
+ }
19
+
20
+ export interface TaskMetadata {
21
+ fileCount?: number;
22
+ dependencyCount?: number;
23
+ isNewFile?: boolean;
24
+ tags?: string[];
25
+ estimatedLines?: number;
26
+ codeBlockCount?: number; // number of fenced code blocks in plan
27
+ complexityKeywords?: string[]; // detected complexity signals
28
+ }
29
+
30
+ // ─── Unit Type → Default Tier Mapping ────────────────────────────────────────
31
+
32
+ const UNIT_TYPE_TIERS: Record<string, ComplexityTier> = {
33
+ // Tier 1 — Light: structured summaries, completion, UAT
34
+ "complete-slice": "light",
35
+ "run-uat": "light",
36
+
37
+ // Tier 2 — Standard: research, routine planning
38
+ "research-milestone": "standard",
39
+ "research-slice": "standard",
40
+ "plan-milestone": "standard",
41
+ "plan-slice": "standard",
42
+
43
+ // Tier 3 — Heavy: execution, replanning (requires deep reasoning)
44
+ "execute-task": "standard", // default standard, upgraded by metadata
45
+ "replan-slice": "heavy",
46
+ "reassess-roadmap": "heavy",
47
+ };
48
+
49
+ // ─── Public API ──────────────────────────────────────────────────────────────
50
+
51
+ /**
52
+ * Classify unit complexity to determine which model tier to use.
53
+ *
54
+ * @param unitType The type of unit being dispatched
55
+ * @param unitId The unit ID (e.g. "M001/S01/T01")
56
+ * @param basePath Project base path (for reading task plans)
57
+ * @param budgetPct Current budget usage as fraction (0.0-1.0+), or undefined if no budget
58
+ * @param metadata Optional pre-parsed task metadata
59
+ */
60
+ export function classifyUnitComplexity(
61
+ unitType: string,
62
+ unitId: string,
63
+ basePath: string,
64
+ budgetPct?: number,
65
+ metadata?: TaskMetadata,
66
+ ): ClassificationResult {
67
+ // Hook units default to light
68
+ if (unitType.startsWith("hook/")) {
69
+ const result: ClassificationResult = { tier: "light", reason: "hook unit", downgraded: false };
70
+ return applyBudgetPressure(result, budgetPct);
71
+ }
72
+
73
+ // Start with the default tier for this unit type
74
+ let tier = UNIT_TYPE_TIERS[unitType] ?? "standard";
75
+ let reason = `unit type: ${unitType}`;
76
+
77
+ // For execute-task, analyze task metadata for complexity signals
78
+ if (unitType === "execute-task") {
79
+ const taskAnalysis = analyzeTaskComplexity(unitId, basePath, metadata);
80
+ tier = taskAnalysis.tier;
81
+ reason = taskAnalysis.reason;
82
+ }
83
+
84
+ // For plan-slice, check if the slice has many tasks (complex planning)
85
+ if (unitType === "plan-slice" || unitType === "plan-milestone") {
86
+ const planAnalysis = analyzePlanComplexity(unitId, basePath);
87
+ if (planAnalysis) {
88
+ tier = planAnalysis.tier;
89
+ reason = planAnalysis.reason;
90
+ }
91
+ }
92
+
93
+ // Adaptive learning: check if history suggests bumping the tier
94
+ const tags = metadata?.tags ?? extractTaskMetadata(unitId, basePath).tags;
95
+ const adaptiveAdjustment = getAdaptiveTierAdjustment(unitType, tier, tags);
96
+ if (adaptiveAdjustment && tierOrdinal(adaptiveAdjustment) > tierOrdinal(tier)) {
97
+ reason = `${reason} (adaptive: high failure rate at ${tier})`;
98
+ tier = adaptiveAdjustment;
99
+ }
100
+
101
+ const result: ClassificationResult = { tier, reason, downgraded: false };
102
+ return applyBudgetPressure(result, budgetPct);
103
+ }
104
+
105
+ /**
106
+ * Get a short label for the tier (for dashboard display).
107
+ */
108
+ export function tierLabel(tier: ComplexityTier): string {
109
+ switch (tier) {
110
+ case "light": return "L";
111
+ case "standard": return "S";
112
+ case "heavy": return "H";
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Get the tier ordering value (for comparison).
118
+ */
119
+ export function tierOrdinal(tier: ComplexityTier): number {
120
+ switch (tier) {
121
+ case "light": return 0;
122
+ case "standard": return 1;
123
+ case "heavy": return 2;
124
+ }
125
+ }
126
+
127
+ // ─── Task Complexity Analysis ────────────────────────────────────────────────
128
+
129
+ interface TaskAnalysis {
130
+ tier: ComplexityTier;
131
+ reason: string;
132
+ }
133
+
134
+ function analyzeTaskComplexity(
135
+ unitId: string,
136
+ basePath: string,
137
+ metadata?: TaskMetadata,
138
+ ): TaskAnalysis {
139
+ // Try to read task plan for complexity signals
140
+ const meta = metadata ?? extractTaskMetadata(unitId, basePath);
141
+
142
+ // Heavy signals
143
+ if (meta.dependencyCount && meta.dependencyCount >= 3) {
144
+ return { tier: "heavy", reason: `${meta.dependencyCount} dependencies` };
145
+ }
146
+ if (meta.fileCount && meta.fileCount >= 6) {
147
+ return { tier: "heavy", reason: `${meta.fileCount} files to modify` };
148
+ }
149
+ if (meta.estimatedLines && meta.estimatedLines >= 500) {
150
+ return { tier: "heavy", reason: `~${meta.estimatedLines} lines estimated` };
151
+ }
152
+
153
+ // Heavy signals from complexity keywords (Phase 4)
154
+ if (meta.complexityKeywords && meta.complexityKeywords.length >= 2) {
155
+ return { tier: "heavy", reason: `complex: ${meta.complexityKeywords.join(", ")}` };
156
+ }
157
+ if (meta.codeBlockCount && meta.codeBlockCount >= 5) {
158
+ return { tier: "heavy", reason: `${meta.codeBlockCount} code blocks in plan` };
159
+ }
160
+
161
+ // Standard signals from single complexity keyword
162
+ if (meta.complexityKeywords && meta.complexityKeywords.length === 1) {
163
+ return { tier: "standard", reason: `${meta.complexityKeywords[0]} task` };
164
+ }
165
+
166
+ // Light signals (simple tasks)
167
+ if (meta.tags?.some(t => /^(docs?|readme|comment|config|typo|rename)$/i.test(t))) {
168
+ return { tier: "light", reason: `simple task: ${meta.tags.join(", ")}` };
169
+ }
170
+ if (meta.fileCount !== undefined && meta.fileCount <= 1 && !meta.isNewFile) {
171
+ return { tier: "light", reason: "single file modification" };
172
+ }
173
+
174
+ // Standard by default
175
+ return { tier: "standard", reason: "standard execution task" };
176
+ }
177
+
178
+ function analyzePlanComplexity(
179
+ unitId: string,
180
+ basePath: string,
181
+ ): TaskAnalysis | null {
182
+ // Check if this is a milestone-level plan (more complex) vs single slice
183
+ const parts = unitId.split("/");
184
+ if (parts.length === 1) {
185
+ // Milestone-level planning is always at least standard
186
+ return { tier: "standard", reason: "milestone-level planning" };
187
+ }
188
+
189
+ // For slice planning, try to read the context/research to gauge complexity
190
+ // If research exists and is large, bump to heavy
191
+ const [mid, sid] = parts;
192
+ const researchPath = join(gsdRoot(basePath), mid, "slices", sid, "RESEARCH.md");
193
+ try {
194
+ if (existsSync(researchPath)) {
195
+ const content = readFileSync(researchPath, "utf-8");
196
+ const lineCount = content.split("\n").length;
197
+ if (lineCount > 200) {
198
+ return { tier: "heavy", reason: `complex slice: ${lineCount}-line research` };
199
+ }
200
+ }
201
+ } catch {
202
+ // Non-fatal
203
+ }
204
+
205
+ return null; // Use default tier
206
+ }
207
+
208
+ /**
209
+ * Extract task metadata from the task plan file on disk.
210
+ */
211
+ function extractTaskMetadata(unitId: string, basePath: string): TaskMetadata {
212
+ const meta: TaskMetadata = {};
213
+ const parts = unitId.split("/");
214
+ if (parts.length !== 3) return meta;
215
+
216
+ const [mid, sid, tid] = parts;
217
+ const taskPlanPath = join(gsdRoot(basePath), mid, "slices", sid, "tasks", `${tid}-PLAN.md`);
218
+
219
+ try {
220
+ if (!existsSync(taskPlanPath)) return meta;
221
+ const content = readFileSync(taskPlanPath, "utf-8");
222
+ const lines = content.split("\n");
223
+
224
+ // Count files mentioned in "Files:" or "- Files:" lines
225
+ const fileLines = lines.filter(l => /^\s*-?\s*files?\s*:/i.test(l));
226
+ if (fileLines.length > 0) {
227
+ // Count comma-separated or bullet-pointed files
228
+ const allFiles = new Set<string>();
229
+ for (const line of fileLines) {
230
+ const filesStr = line.replace(/^\s*-?\s*files?\s*:\s*/i, "");
231
+ const files = filesStr.split(/[,;]/).map(f => f.trim()).filter(Boolean);
232
+ files.forEach(f => allFiles.add(f));
233
+ }
234
+ meta.fileCount = allFiles.size;
235
+ }
236
+
237
+ // Check for "new file" or "create" keywords
238
+ meta.isNewFile = lines.some(l => /\b(create|new file|scaffold|bootstrap)\b/i.test(l));
239
+
240
+ // Look for tags/labels in frontmatter or content
241
+ const tags: string[] = [];
242
+ if (content.match(/\b(refactor|migration|architect)/i)) tags.push("refactor");
243
+ if (content.match(/\b(test|spec|coverage)\b/i)) tags.push("test");
244
+ if (content.match(/\b(doc|readme|comment|jsdoc)\b/i)) tags.push("docs");
245
+ if (content.match(/\b(config|env|setting)\b/i)) tags.push("config");
246
+ if (content.match(/\b(rename|typo|spelling)\b/i)) tags.push("rename");
247
+ meta.tags = tags;
248
+
249
+ // Try to extract estimated lines from content
250
+ const estimateMatch = content.match(/~?\s*(\d+)\s*lines?\b/i);
251
+ if (estimateMatch) {
252
+ meta.estimatedLines = parseInt(estimateMatch[1], 10);
253
+ }
254
+
255
+ // Phase 4: Deeper introspection signals
256
+
257
+ // Count fenced code blocks (```) — more code blocks = more complex implementation
258
+ const codeBlockMatches = content.match(/^```/gm);
259
+ meta.codeBlockCount = codeBlockMatches ? Math.floor(codeBlockMatches.length / 2) : 0;
260
+
261
+ // Detect complexity keywords that suggest harder tasks
262
+ const complexityKeywords: string[] = [];
263
+ if (content.match(/\b(migration|migrate|schema change)\b/i)) complexityKeywords.push("migration");
264
+ if (content.match(/\b(architect|design pattern|system design)\b/i)) complexityKeywords.push("architecture");
265
+ if (content.match(/\b(security|auth|encrypt|credential|vulnerability)\b/i)) complexityKeywords.push("security");
266
+ if (content.match(/\b(performance|optimize|cache|index)\b/i)) complexityKeywords.push("performance");
267
+ if (content.match(/\b(concurrent|parallel|race condition|mutex|lock)\b/i)) complexityKeywords.push("concurrency");
268
+ if (content.match(/\b(backward.?compat|breaking change|deprecat)\b/i)) complexityKeywords.push("compatibility");
269
+ meta.complexityKeywords = complexityKeywords;
270
+ } catch {
271
+ // Non-fatal — metadata extraction is best-effort
272
+ }
273
+
274
+ return meta;
275
+ }
276
+
277
+ // ─── Budget Pressure ─────────────────────────────────────────────────────────
278
+
279
+ /**
280
+ * Apply budget pressure to a classification result.
281
+ * As budget usage increases, more aggressively downgrade tiers.
282
+ *
283
+ * - <50%: Normal classification (no change)
284
+ * - 50-75%: Tier 2 → Tier 1 where possible
285
+ * - 75-90%: Only heavy tasks keep configured model
286
+ * - >90%: Everything except replan-slice gets cheapest model
287
+ */
288
+ function applyBudgetPressure(
289
+ result: ClassificationResult,
290
+ budgetPct?: number,
291
+ ): ClassificationResult {
292
+ if (budgetPct === undefined || budgetPct < 0.5) return result;
293
+
294
+ const original = result.tier;
295
+
296
+ if (budgetPct >= 0.9) {
297
+ // >90%: almost everything goes to light
298
+ if (result.tier !== "heavy") {
299
+ result.tier = "light";
300
+ } else {
301
+ // Even heavy gets downgraded to standard
302
+ result.tier = "standard";
303
+ }
304
+ } else if (budgetPct >= 0.75) {
305
+ // 75-90%: only heavy stays, everything else goes to light
306
+ if (result.tier === "standard") {
307
+ result.tier = "light";
308
+ }
309
+ } else {
310
+ // 50-75%: standard → light
311
+ if (result.tier === "standard") {
312
+ result.tier = "light";
313
+ }
314
+ }
315
+
316
+ if (result.tier !== original) {
317
+ result.downgraded = true;
318
+ result.reason = `${result.reason} (budget pressure: ${Math.round(budgetPct * 100)}%)`;
319
+ }
320
+
321
+ return result;
322
+ }
@@ -0,0 +1,243 @@
1
+ /**
2
+ * Context budget engine — proportional allocation, section-boundary truncation,
3
+ * and executor context window resolution.
4
+ *
5
+ * All functions are pure or near-pure (dependency-injected). No global state, no I/O.
6
+ * Budget ratios are module-level constants for easy tuning.
7
+ *
8
+ * @see D001 (module location), D002 (200K fallback), D003 (section-boundary truncation)
9
+ */
10
+
11
+ // ─── Budget ratio constants ──────────────────────────────────────────────────
12
+ // Percentages of total context window allocated to each budget category.
13
+ // These are applied after tokens→chars conversion.
14
+
15
+ /** Proportion of context window for dependency/prior-task summaries */
16
+ const SUMMARY_RATIO = 0.15;
17
+
18
+ /** Proportion of context window for inline context (plans, decisions, code) */
19
+ const INLINE_CONTEXT_RATIO = 0.40;
20
+
21
+ /** Proportion of context window for verification sections in prompts */
22
+ const VERIFICATION_RATIO = 0.10;
23
+
24
+ /** Approximate chars-per-token conversion factor */
25
+ const CHARS_PER_TOKEN = 4;
26
+
27
+ /** Default context window when none can be resolved (D002) */
28
+ const DEFAULT_CONTEXT_WINDOW = 200_000;
29
+
30
+ /** Percentage of context consumed before suggesting a continue-here checkpoint */
31
+ const CONTINUE_THRESHOLD_PERCENT = 70;
32
+
33
+ // ─── Task count bounds ───────────────────────────────────────────────────────
34
+ // Task count range scales with context window. Smaller windows get fewer tasks
35
+ // to avoid overloading the executor.
36
+
37
+ const TASK_COUNT_MIN = 2;
38
+
39
+ /** Task count ceiling tiers: [contextWindowThreshold, maxTasks] */
40
+ const TASK_COUNT_TIERS: [number, number][] = [
41
+ [500_000, 8], // 500K+ tokens → up to 8 tasks
42
+ [200_000, 6], // 200K+ tokens → up to 6 tasks
43
+ [128_000, 5], // 128K+ tokens → up to 5 tasks
44
+ [0, 3], // anything smaller → up to 3 tasks
45
+ ];
46
+
47
+ // ─── Types ───────────────────────────────────────────────────────────────────
48
+
49
+ export interface TruncationResult {
50
+ /** The (possibly truncated) content string */
51
+ content: string;
52
+ /** Number of sections dropped during truncation; 0 when content fits */
53
+ droppedSections: number;
54
+ }
55
+
56
+ export interface BudgetAllocation {
57
+ /** Character budget for dependency/prior-task summaries */
58
+ summaryBudgetChars: number;
59
+ /** Character budget for inline context (plans, decisions, code snippets) */
60
+ inlineContextBudgetChars: number;
61
+ /** Recommended task count range for the executor at this context window */
62
+ taskCountRange: { min: number; max: number };
63
+ /** Percentage of context consumed before suggesting a continue-here checkpoint */
64
+ continueThresholdPercent: number;
65
+ /** Character budget for verification sections */
66
+ verificationBudgetChars: number;
67
+ }
68
+
69
+ // ─── Minimal interface slices for dependency injection ───────────────────────
70
+ // These avoid coupling to full ModelRegistry/GSDPreferences types in tests.
71
+
72
+ export interface MinimalModel {
73
+ id: string;
74
+ provider: string;
75
+ contextWindow: number;
76
+ }
77
+
78
+ export interface MinimalModelRegistry {
79
+ getAll(): MinimalModel[];
80
+ }
81
+
82
+ export interface MinimalPreferences {
83
+ models?: {
84
+ execution?: string | { model: string; fallbacks?: string[] };
85
+ };
86
+ }
87
+
88
+ // ─── Public API ──────────────────────────────────────────────────────────────
89
+
90
+ /**
91
+ * Compute proportional budget allocations from a context window size (in tokens).
92
+ *
93
+ * Returns deterministic output for any given input. Invalid inputs (≤ 0)
94
+ * silently default to 200K (D002).
95
+ */
96
+ export function computeBudgets(contextWindow: number): BudgetAllocation {
97
+ const effectiveWindow = contextWindow > 0 ? contextWindow : DEFAULT_CONTEXT_WINDOW;
98
+ const totalChars = effectiveWindow * CHARS_PER_TOKEN;
99
+
100
+ return {
101
+ summaryBudgetChars: Math.floor(totalChars * SUMMARY_RATIO),
102
+ inlineContextBudgetChars: Math.floor(totalChars * INLINE_CONTEXT_RATIO),
103
+ verificationBudgetChars: Math.floor(totalChars * VERIFICATION_RATIO),
104
+ continueThresholdPercent: CONTINUE_THRESHOLD_PERCENT,
105
+ taskCountRange: {
106
+ min: TASK_COUNT_MIN,
107
+ max: resolveTaskCountMax(effectiveWindow),
108
+ },
109
+ };
110
+ }
111
+
112
+ /**
113
+ * Truncate content at markdown section boundaries to fit within a character budget.
114
+ *
115
+ * Splits on `### ` headings and `---` dividers. Keeps whole sections that fit.
116
+ * Appends `[...truncated N sections]` when content is dropped.
117
+ * Returns content unchanged when it fits within budget.
118
+ *
119
+ * @see D003 — section-boundary truncation is mandatory; mid-section cuts are unacceptable.
120
+ */
121
+ export function truncateAtSectionBoundary(content: string, budgetChars: number): TruncationResult {
122
+ if (!content || content.length <= budgetChars) {
123
+ return { content, droppedSections: 0 };
124
+ }
125
+
126
+ // Split on section markers: ### headings or --- dividers (on their own line)
127
+ const sections = splitIntoSections(content);
128
+
129
+ if (sections.length <= 1) {
130
+ // No section markers — keep as much as fits from the start
131
+ const truncated = content.slice(0, budgetChars);
132
+ return { content: truncated + "\n\n[...truncated 1 sections]", droppedSections: 1 };
133
+ }
134
+
135
+ // Greedily keep sections that fit
136
+ let usedChars = 0;
137
+ let keptCount = 0;
138
+
139
+ for (const section of sections) {
140
+ const sectionLen = section.length;
141
+ if (usedChars + sectionLen > budgetChars && keptCount > 0) {
142
+ break;
143
+ }
144
+ // Always keep at least the first section (even if it exceeds budget)
145
+ usedChars += sectionLen;
146
+ keptCount++;
147
+ if (usedChars >= budgetChars) break;
148
+ }
149
+
150
+ const droppedCount = sections.length - keptCount;
151
+ if (droppedCount === 0) {
152
+ return { content, droppedSections: 0 };
153
+ }
154
+
155
+ const kept = sections.slice(0, keptCount).join("");
156
+ return {
157
+ content: kept.trimEnd() + `\n\n[...truncated ${droppedCount} sections]`,
158
+ droppedSections: droppedCount,
159
+ };
160
+ }
161
+
162
+ /**
163
+ * Resolve the executor model's context window size using a fallback chain:
164
+ *
165
+ * 1. Look up the configured executor model ID in preferences → find in registry → return contextWindow
166
+ * 2. Fall back to sessionContextWindow if provided
167
+ * 3. Fall back to 200K default (D002)
168
+ *
169
+ * Supports "provider/model" format in preferences for explicit provider targeting.
170
+ */
171
+ export function resolveExecutorContextWindow(
172
+ registry: MinimalModelRegistry | undefined,
173
+ preferences: MinimalPreferences | undefined,
174
+ sessionContextWindow?: number,
175
+ ): number {
176
+ // Step 1: Try configured executor model
177
+ if (preferences?.models?.execution && registry) {
178
+ const executionConfig = preferences.models.execution;
179
+ const modelId = typeof executionConfig === "string"
180
+ ? executionConfig
181
+ : executionConfig.model;
182
+
183
+ if (modelId) {
184
+ const model = findModelById(registry, modelId);
185
+ if (model && model.contextWindow > 0) {
186
+ return model.contextWindow;
187
+ }
188
+ }
189
+ }
190
+
191
+ // Step 2: Fall back to session context window
192
+ if (sessionContextWindow && sessionContextWindow > 0) {
193
+ return sessionContextWindow;
194
+ }
195
+
196
+ // Step 3: Fall back to default (D002)
197
+ return DEFAULT_CONTEXT_WINDOW;
198
+ }
199
+
200
+ // ─── Internal helpers ────────────────────────────────────────────────────────
201
+
202
+ /**
203
+ * Resolve task count ceiling from context window size.
204
+ * Larger windows support more tasks per slice.
205
+ */
206
+ function resolveTaskCountMax(contextWindow: number): number {
207
+ for (const [threshold, max] of TASK_COUNT_TIERS) {
208
+ if (contextWindow >= threshold) return max;
209
+ }
210
+ return 3; // fallback — unreachable given tiers include 0
211
+ }
212
+
213
+ /**
214
+ * Split content into sections at `### ` headings or `---` dividers.
215
+ * Each section includes its leading marker.
216
+ */
217
+ function splitIntoSections(content: string): string[] {
218
+ // Match section boundaries: ### heading or --- divider at start of line
219
+ const pattern = /^(?=### |\-{3,}\s*$)/m;
220
+ const parts = content.split(pattern).filter(p => p.length > 0);
221
+ return parts;
222
+ }
223
+
224
+ /**
225
+ * Find a model in the registry by ID string.
226
+ * Supports "provider/model" format for explicit provider targeting,
227
+ * or bare model ID (first match wins).
228
+ */
229
+ function findModelById(registry: MinimalModelRegistry, modelId: string): MinimalModel | undefined {
230
+ const allModels = registry.getAll();
231
+ const slashIdx = modelId.indexOf("/");
232
+
233
+ if (slashIdx !== -1) {
234
+ const provider = modelId.substring(0, slashIdx).toLowerCase();
235
+ const id = modelId.substring(slashIdx + 1).toLowerCase();
236
+ return allModels.find(
237
+ m => m.provider.toLowerCase() === provider && m.id.toLowerCase() === id,
238
+ );
239
+ }
240
+
241
+ // Bare ID — first match
242
+ return allModels.find(m => m.id === modelId);
243
+ }