@pugi/cli 0.1.0-beta.9 → 0.1.0-beta.91

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 (411) hide show
  1. package/CHANGELOG.md +132 -0
  2. package/LICENSE +1 -1
  3. package/assets/pugi-prozr2-mascot.ansi +9 -0
  4. package/bin/run.js +33 -1
  5. package/dist/commands/deploy.js +40 -40
  6. package/dist/commands/flatten.js +191 -0
  7. package/dist/commands/jobs-watch.js +201 -0
  8. package/dist/commands/jobs.js +42 -27
  9. package/dist/commands/smoke.js +133 -0
  10. package/dist/core/agent-progress/cleanup.js +134 -0
  11. package/dist/core/agent-progress/schema.js +144 -0
  12. package/dist/core/agent-progress/writer.js +101 -0
  13. package/dist/core/agents/adaptive-router.js +330 -0
  14. package/dist/core/agents/query-decomposer.js +297 -0
  15. package/dist/core/agents/registry.js +3 -3
  16. package/dist/core/approvals/shortcut-resolver.js +98 -0
  17. package/dist/core/artifact-chain/dispatcher.js +148 -0
  18. package/dist/core/artifact-chain/exporter.js +164 -0
  19. package/dist/core/artifact-chain/state.js +243 -0
  20. package/dist/core/artifact-chain/steps.js +169 -0
  21. package/dist/core/ask-user/question.js +92 -0
  22. package/dist/core/audit/audit-trail.js +275 -0
  23. package/dist/core/auth/ensure-authenticated.js +129 -0
  24. package/dist/core/auth/env-provider.js +238 -0
  25. package/dist/core/auto-open-browser.js +4 -4
  26. package/dist/core/auto-update/channels.js +122 -0
  27. package/dist/core/auto-update/checker.js +241 -0
  28. package/dist/core/auto-update/state.js +235 -0
  29. package/dist/core/bare-mode/index.js +107 -0
  30. package/dist/core/bash/redirect.js +281 -0
  31. package/dist/core/bash-classifier.js +436 -40
  32. package/dist/core/checkpoint/resumer.js +149 -0
  33. package/dist/core/checkpoint/rewinder.js +291 -0
  34. package/dist/core/checkpoints/shadow-git.js +670 -0
  35. package/dist/core/citations/parser.js +109 -0
  36. package/dist/core/classifier/yolo-classifier.js +88 -0
  37. package/dist/core/codegraph/decision-store.js +248 -0
  38. package/dist/core/codegraph/detect-repo.js +459 -0
  39. package/dist/core/codegraph/install.js +134 -0
  40. package/dist/core/codegraph/offer-hook.js +220 -0
  41. package/dist/core/compact/auto-trigger.js +96 -0
  42. package/dist/core/compact/buffer-rewriter.js +115 -0
  43. package/dist/core/compact/summarizer.js +208 -0
  44. package/dist/core/compact/token-counter.js +108 -0
  45. package/dist/core/consensus/anvil-fanout.js +25 -25
  46. package/dist/core/consensus/diff-capture.js +121 -12
  47. package/dist/core/consensus/rubric.js +21 -21
  48. package/dist/core/context/builder.js +6 -6
  49. package/dist/core/context/compaction-events.js +8 -8
  50. package/dist/core/context/compaction.js +31 -31
  51. package/dist/core/context/index.js +15 -8
  52. package/dist/core/context/invariants.js +51 -51
  53. package/dist/core/context/markdown-loader.js +28 -10
  54. package/dist/core/context/markdown-traverse.js +255 -0
  55. package/dist/core/context/pugiignore.js +41 -41
  56. package/dist/core/context/repo-skeleton.js +37 -37
  57. package/dist/core/context/tool-eviction.js +55 -0
  58. package/dist/core/context/watcher.js +32 -32
  59. package/dist/core/context/working-set.js +23 -23
  60. package/dist/core/coordinator/agent-tools.js +77 -0
  61. package/dist/core/coordinator/agent-toolset.js +65 -0
  62. package/dist/core/coordinator/fsm.js +73 -0
  63. package/dist/core/coordinator/mode-fsm.js +70 -0
  64. package/dist/core/cost/rate-card.js +129 -0
  65. package/dist/core/cost/tracker.js +221 -0
  66. package/dist/core/credentials.js +13 -13
  67. package/dist/core/cron/scheduler.js +138 -0
  68. package/dist/core/denial-tracking/index.js +8 -0
  69. package/dist/core/denial-tracking/state.js +264 -0
  70. package/dist/core/diagnostics/probe-runner.js +93 -0
  71. package/dist/core/diagnostics/probes/api.js +46 -0
  72. package/dist/core/diagnostics/probes/auth.js +93 -0
  73. package/dist/core/diagnostics/probes/bare-mode.js +42 -0
  74. package/dist/core/diagnostics/probes/cli-version.js +127 -0
  75. package/dist/core/diagnostics/probes/config.js +72 -0
  76. package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
  77. package/dist/core/diagnostics/probes/disk.js +81 -0
  78. package/dist/core/diagnostics/probes/engine-live.js +46 -0
  79. package/dist/core/diagnostics/probes/git.js +65 -0
  80. package/dist/core/diagnostics/probes/hooks.js +118 -0
  81. package/dist/core/diagnostics/probes/mcp.js +75 -0
  82. package/dist/core/diagnostics/probes/node.js +59 -0
  83. package/dist/core/diagnostics/probes/pnpm.js +36 -0
  84. package/dist/core/diagnostics/probes/pugi-md.js +89 -0
  85. package/dist/core/diagnostics/probes/sandbox.js +40 -0
  86. package/dist/core/diagnostics/probes/session.js +74 -0
  87. package/dist/core/diagnostics/probes/status-snapshot.js +488 -0
  88. package/dist/core/diagnostics/probes/workspace.js +63 -0
  89. package/dist/core/diagnostics/types.js +70 -0
  90. package/dist/core/dispatch/cache-cleanup.js +197 -0
  91. package/dist/core/dispatch/cache-handoff.js +295 -0
  92. package/dist/core/edits/apply-patch-layer-e.js +189 -0
  93. package/dist/core/edits/dispatch.js +333 -7
  94. package/dist/core/edits/format-detector.js +260 -0
  95. package/dist/core/edits/format-matrix.js +26 -0
  96. package/dist/core/edits/fuzzy-ladder.js +650 -0
  97. package/dist/core/edits/index.js +5 -1
  98. package/dist/core/edits/journal.js +199 -0
  99. package/dist/core/edits/layer-a-apply.js +15 -15
  100. package/dist/core/edits/layer-a-fuzzy-apply.js +198 -0
  101. package/dist/core/edits/layer-b-apply.js +9 -9
  102. package/dist/core/edits/layer-c-apply.js +6 -6
  103. package/dist/core/edits/layer-d-ast.js +557 -14
  104. package/dist/core/edits/marker-parser.js +12 -12
  105. package/dist/core/edits/security-gate.js +27 -27
  106. package/dist/core/edits/verify-hook.js +273 -0
  107. package/dist/core/edits/worktree.js +29 -29
  108. package/dist/core/engine/anvil-client.js +214 -26
  109. package/dist/core/engine/auto-compact.js +179 -0
  110. package/dist/core/engine/budgets.js +186 -0
  111. package/dist/core/engine/context-prefix.js +155 -0
  112. package/dist/core/engine/index.js +1 -1
  113. package/dist/core/engine/intensity.js +158 -0
  114. package/dist/core/engine/intent.js +260 -0
  115. package/dist/core/engine/native-pugi.js +1295 -227
  116. package/dist/core/engine/prompts.js +129 -19
  117. package/dist/core/engine/strip-internal-fields.js +124 -0
  118. package/dist/core/engine/tool-bridge.js +1792 -59
  119. package/dist/core/evaluation/golden-dataset.js +293 -0
  120. package/dist/core/feedback/queue.js +177 -0
  121. package/dist/core/feedback/submitter.js +145 -0
  122. package/dist/core/file-cache.js +113 -1
  123. package/dist/core/flatten/flatten-repo.js +439 -0
  124. package/dist/core/format/osc8-link.js +28 -0
  125. package/dist/core/hook-chains.js +392 -0
  126. package/dist/core/hooks/citation-verify-hook.js +138 -0
  127. package/dist/core/hooks/citation-verify.js +112 -0
  128. package/dist/core/hooks/events.js +46 -0
  129. package/dist/core/hooks/index.js +15 -0
  130. package/dist/core/hooks/registry.js +216 -0
  131. package/dist/core/hooks/runner.js +236 -0
  132. package/dist/core/hooks/v2/event-emitter.js +115 -0
  133. package/dist/core/hooks/v2/executor.js +282 -0
  134. package/dist/core/hooks/v2/index.js +25 -0
  135. package/dist/core/hooks/v2/lifecycle.js +104 -0
  136. package/dist/core/hooks/v2/loader.js +216 -0
  137. package/dist/core/hooks/v2/matcher.js +125 -0
  138. package/dist/core/hooks/v2/trust.js +143 -0
  139. package/dist/core/hooks/v2/types.js +86 -0
  140. package/dist/core/hooks/worktree-events.js +158 -0
  141. package/dist/core/image/renderer.js +71 -0
  142. package/dist/core/init/detector.js +582 -0
  143. package/dist/core/init/template-renderer.js +242 -0
  144. package/dist/core/jobs/registry.js +18 -18
  145. package/dist/core/ledger/results-tsv.js +142 -0
  146. package/dist/core/log-discipline/stdout-redirect.js +51 -0
  147. package/dist/core/lsp/cache.js +105 -0
  148. package/dist/core/lsp/client.js +551 -41
  149. package/dist/core/lsp/language-detect.js +66 -0
  150. package/dist/core/lsp/post-edit-diagnostics.js +171 -0
  151. package/dist/core/lsp/server-detect.js +173 -0
  152. package/dist/core/lsp/symbol-cache.js +162 -0
  153. package/dist/core/lsp/symbol-tools.js +664 -0
  154. package/dist/core/mcp/client.js +97 -28
  155. package/dist/core/mcp/http-server.js +553 -0
  156. package/dist/core/mcp/orchestrator-tools.js +662 -0
  157. package/dist/core/mcp/permission.js +190 -0
  158. package/dist/core/mcp/registry.js +39 -17
  159. package/dist/core/mcp/server-tools.js +219 -0
  160. package/dist/core/mcp/server.js +397 -0
  161. package/dist/core/mcp/trust.js +10 -10
  162. package/dist/core/memory/dual-write.js +416 -0
  163. package/dist/core/memory/passive-extract.js +130 -0
  164. package/dist/core/memory/phase1-kinds.js +20 -0
  165. package/dist/core/memory/secret-scanner.js +304 -0
  166. package/dist/core/memory-sync/queue.js +170 -0
  167. package/dist/core/metrics/extract.js +113 -0
  168. package/dist/core/modes/roo-modes.js +68 -0
  169. package/dist/core/onboarding/ensure-initialized.js +133 -0
  170. package/dist/core/onboarding/marker.js +111 -0
  171. package/dist/core/onboarding/telemetry-state.js +108 -0
  172. package/dist/core/output-style/presets.js +176 -0
  173. package/dist/core/output-style/state.js +185 -0
  174. package/dist/core/path-security.js +287 -5
  175. package/dist/core/permission.js +82 -22
  176. package/dist/core/permissions/auto-classifier.js +124 -0
  177. package/dist/core/permissions/bash-parser.js +371 -0
  178. package/dist/core/permissions/circuit-breaker.js +83 -0
  179. package/dist/core/permissions/constrained-edit.js +91 -0
  180. package/dist/core/permissions/gate.js +278 -0
  181. package/dist/core/permissions/index.js +20 -0
  182. package/dist/core/permissions/mode.js +174 -0
  183. package/dist/core/permissions/network-egress.js +137 -0
  184. package/dist/core/permissions/state.js +241 -0
  185. package/dist/core/permissions/tool-class.js +93 -0
  186. package/dist/core/plan-mode/ui-state.js +51 -0
  187. package/dist/core/plans/plan-artifact.js +721 -0
  188. package/dist/core/policy-limits/etag-store.js +122 -0
  189. package/dist/core/prd-check/parser.js +215 -0
  190. package/dist/core/prd-check/reporter.js +127 -0
  191. package/dist/core/prd-check/session-review.js +557 -0
  192. package/dist/core/prd-check/verifiers.js +223 -0
  193. package/dist/core/prompt-cache/client-cache.js +99 -0
  194. package/dist/core/prompts/assembly.js +29 -0
  195. package/dist/core/prompts/registry.js +364 -0
  196. package/dist/core/pugi-md/cc-compat-rules.js +735 -0
  197. package/dist/core/pugi-md/context-injector.js +76 -0
  198. package/dist/core/pugi-md/walk-up.js +207 -0
  199. package/dist/core/python/uv-installer.js +270 -0
  200. package/dist/core/python/uv-resolver.js +83 -0
  201. package/dist/core/rate-limit/narrator.js +146 -0
  202. package/dist/core/recipes/cli-types.js +20 -0
  203. package/dist/core/recipes/loader.js +103 -0
  204. package/dist/core/recipes/runner.js +345 -0
  205. package/dist/core/recipes/schema.js +587 -0
  206. package/dist/core/release-notes/parser.js +241 -0
  207. package/dist/core/release-notes/state.js +116 -0
  208. package/dist/core/repl/ask.js +37 -37
  209. package/dist/core/repl/cancellation.js +26 -26
  210. package/dist/core/repl/cap-warning.js +4 -4
  211. package/dist/core/repl/clipboard-read.js +11 -11
  212. package/dist/core/repl/dispatch-fsm.js +12 -12
  213. package/dist/core/repl/history-search.js +15 -15
  214. package/dist/core/repl/history.js +28 -18
  215. package/dist/core/repl/kill-ring.js +5 -5
  216. package/dist/core/repl/model-pricing.js +135 -0
  217. package/dist/core/repl/privacy-banner.js +22 -22
  218. package/dist/core/repl/session.js +2148 -217
  219. package/dist/core/repl/slash-commands.js +501 -41
  220. package/dist/core/repl/store/index.js +1 -1
  221. package/dist/core/repl/store/jsonl-log.js +22 -22
  222. package/dist/core/repl/store/lockfile.js +10 -10
  223. package/dist/core/repl/store/session-store.js +136 -107
  224. package/dist/core/repl/store/types.js +15 -15
  225. package/dist/core/repl/store/uuid-v7.js +12 -12
  226. package/dist/core/repl/workspace-context.js +43 -21
  227. package/dist/core/repo-map/build.js +125 -0
  228. package/dist/core/repo-map/cache.js +185 -0
  229. package/dist/core/repo-map/extractor.js +254 -0
  230. package/dist/core/repo-map/formatter.js +145 -0
  231. package/dist/core/repo-map/page-rank.js +105 -0
  232. package/dist/core/repo-map/scanner.js +211 -0
  233. package/dist/core/retry-budget/budget.js +284 -0
  234. package/dist/core/retry-budget/index.js +5 -0
  235. package/dist/core/retry-budget/retry-cap.js +74 -0
  236. package/dist/core/routing/lead-worker.js +43 -0
  237. package/dist/core/routing/pre-flight-estimator.js +108 -0
  238. package/dist/core/runs/run-tree.js +103 -0
  239. package/dist/core/security/injection-scanner.js +367 -0
  240. package/dist/core/security/output-filter.js +418 -0
  241. package/dist/core/session/env-file.js +105 -0
  242. package/dist/core/session/section-budgets.js +140 -0
  243. package/dist/core/session.js +92 -0
  244. package/dist/core/settings.js +324 -5
  245. package/dist/core/share/formatter.js +271 -0
  246. package/dist/core/share/redactor.js +221 -0
  247. package/dist/core/share/uploader.js +267 -0
  248. package/dist/core/skills/defaults.js +30 -30
  249. package/dist/core/skills/loader.js +22 -22
  250. package/dist/core/skills/sources.js +27 -27
  251. package/dist/core/smoke/headless-driver.js +174 -0
  252. package/dist/core/smoke/orchestrator.js +194 -0
  253. package/dist/core/smoke/runner.js +238 -0
  254. package/dist/core/smoke/scenario-parser.js +316 -0
  255. package/dist/core/statusline.js +99 -0
  256. package/dist/core/subagents/dispatcher-real.js +600 -0
  257. package/dist/core/subagents/dispatcher.js +132 -43
  258. package/dist/core/subagents/index.js +19 -6
  259. package/dist/core/subagents/isolation-matrix.js +213 -0
  260. package/dist/core/subagents/spawn.js +19 -4
  261. package/dist/core/telemetry/emitter.js +229 -0
  262. package/dist/core/telemetry/queue.js +251 -0
  263. package/dist/core/theme/context.js +91 -0
  264. package/dist/core/theme/presets.js +228 -0
  265. package/dist/core/theme/state.js +181 -0
  266. package/dist/core/todos/invariant.js +10 -0
  267. package/dist/core/todos/state.js +177 -0
  268. package/dist/core/tool-schema/compressor.js +89 -0
  269. package/dist/core/transport/version-interceptor.js +166 -0
  270. package/dist/core/trust.js +2 -2
  271. package/dist/core/tui/thinking-block.js +64 -0
  272. package/dist/core/vim/keymap.js +288 -0
  273. package/dist/core/vim/state.js +92 -0
  274. package/dist/core/watch-markers/marker-watcher.js +133 -0
  275. package/dist/core/worktree/include-parser.js +249 -0
  276. package/dist/core/worktree-manager/cleanup.js +123 -0
  277. package/dist/core/worktree-manager/manager.js +303 -0
  278. package/dist/index.js +36 -0
  279. package/dist/runtime/bootstrap.js +190 -0
  280. package/dist/runtime/cli.js +4185 -549
  281. package/dist/runtime/commands/agents.js +31 -31
  282. package/dist/runtime/commands/budget.js +5 -5
  283. package/dist/runtime/commands/cancel.js +231 -0
  284. package/dist/runtime/commands/chain.js +489 -0
  285. package/dist/runtime/commands/codegraph-status.js +227 -0
  286. package/dist/runtime/commands/compact.js +297 -0
  287. package/dist/runtime/commands/config.js +73 -39
  288. package/dist/runtime/commands/cost.js +199 -0
  289. package/dist/runtime/commands/delegate.js +27 -4
  290. package/dist/runtime/commands/dispatch.js +126 -0
  291. package/dist/runtime/commands/doctor.js +579 -0
  292. package/dist/runtime/commands/feedback.js +184 -0
  293. package/dist/runtime/commands/hooks.js +187 -0
  294. package/dist/runtime/commands/init.js +254 -0
  295. package/dist/runtime/commands/lsp.js +200 -38
  296. package/dist/runtime/commands/mcp.js +879 -0
  297. package/dist/runtime/commands/memory.js +582 -0
  298. package/dist/runtime/commands/model.js +237 -0
  299. package/dist/runtime/commands/onboarding.js +275 -0
  300. package/dist/runtime/commands/patch.js +12 -12
  301. package/dist/runtime/commands/permissions.js +112 -0
  302. package/dist/runtime/commands/plan.js +143 -0
  303. package/dist/runtime/commands/prd-check.js +285 -0
  304. package/dist/runtime/commands/privacy.js +17 -17
  305. package/dist/runtime/commands/recipe.js +325 -0
  306. package/dist/runtime/commands/redo-blob-store.js +92 -0
  307. package/dist/runtime/commands/redo.js +361 -0
  308. package/dist/runtime/commands/release-notes.js +229 -0
  309. package/dist/runtime/commands/repo-map.js +95 -0
  310. package/dist/runtime/commands/report.js +299 -0
  311. package/dist/runtime/commands/resume.js +118 -0
  312. package/dist/runtime/commands/review-consensus.js +68 -53
  313. package/dist/runtime/commands/rewind.js +333 -0
  314. package/dist/runtime/commands/roster.js +14 -14
  315. package/dist/runtime/commands/sessions.js +163 -0
  316. package/dist/runtime/commands/share.js +316 -0
  317. package/dist/runtime/commands/skills.js +31 -31
  318. package/dist/runtime/commands/status.js +186 -0
  319. package/dist/runtime/commands/stickers.js +82 -0
  320. package/dist/runtime/commands/style.js +194 -0
  321. package/dist/runtime/commands/theme.js +196 -0
  322. package/dist/runtime/commands/undo.js +54 -22
  323. package/dist/runtime/commands/update.js +289 -0
  324. package/dist/runtime/commands/vim.js +140 -0
  325. package/dist/runtime/commands/worktree.js +8 -8
  326. package/dist/runtime/commands/worktrees.js +155 -0
  327. package/dist/runtime/headless-repl.js +195 -0
  328. package/dist/runtime/headless.js +543 -0
  329. package/dist/runtime/load-hooks-or-exit.js +71 -0
  330. package/dist/runtime/plan-decompose.js +22 -22
  331. package/dist/runtime/sigint-guard.js +272 -0
  332. package/dist/runtime/update-check.js +28 -28
  333. package/dist/runtime/version.js +65 -0
  334. package/dist/runtime/worktree-bootstrap.js +579 -0
  335. package/dist/skills/bundled/batch.js +617 -0
  336. package/dist/skills/bundled/index.js +45 -0
  337. package/dist/skills/bundled/loop.js +358 -0
  338. package/dist/skills/bundled/remember.js +383 -0
  339. package/dist/skills/bundled/simplify.js +289 -0
  340. package/dist/skills/bundled/skillify.js +373 -0
  341. package/dist/skills/bundled/stuck.js +558 -0
  342. package/dist/skills/bundled/verify.js +439 -0
  343. package/dist/testing/vcr.js +486 -0
  344. package/dist/tools/agent-tool.js +229 -0
  345. package/dist/tools/apply-patch.js +89 -28
  346. package/dist/tools/ask-user-question.js +337 -0
  347. package/dist/tools/ask-user.js +115 -0
  348. package/dist/tools/bash.js +624 -46
  349. package/dist/tools/brief.js +224 -0
  350. package/dist/tools/cron.js +433 -0
  351. package/dist/tools/enter-worktree.js +250 -0
  352. package/dist/tools/exit-worktree.js +147 -0
  353. package/dist/tools/file-tools.js +161 -44
  354. package/dist/tools/lsp-tools.js +377 -1
  355. package/dist/tools/mcp-tool.js +260 -0
  356. package/dist/tools/multi-edit.js +361 -0
  357. package/dist/tools/powershell.js +268 -0
  358. package/dist/tools/registry.js +99 -4
  359. package/dist/tools/skill-tool.js +96 -0
  360. package/dist/tools/sleep.js +99 -0
  361. package/dist/tools/synthetic-output.js +133 -0
  362. package/dist/tools/tasks.js +208 -0
  363. package/dist/tools/todo-write.js +184 -0
  364. package/dist/tools/verify-plan-execution.js +295 -0
  365. package/dist/tools/web-fetch-injection-scanner.js +207 -0
  366. package/dist/tools/web-fetch.js +195 -10
  367. package/dist/tools/web-search.js +458 -0
  368. package/dist/tui/agent-progress-card.js +111 -0
  369. package/dist/tui/agent-tree.js +11 -1
  370. package/dist/tui/ask-modal.js +14 -14
  371. package/dist/tui/ask-user-question-chips.js +315 -0
  372. package/dist/tui/ask-user-question-prompt.js +203 -0
  373. package/dist/tui/compact-banner.js +81 -0
  374. package/dist/tui/conversation-pane.js +85 -11
  375. package/dist/tui/cost-table.js +111 -0
  376. package/dist/tui/device-flow.js +2 -2
  377. package/dist/tui/doctor-table.js +46 -0
  378. package/dist/tui/feedback-prompt.js +156 -0
  379. package/dist/tui/input-box.js +247 -32
  380. package/dist/tui/login-picker.js +3 -3
  381. package/dist/tui/markdown-render.js +6 -6
  382. package/dist/tui/multi-file-diff-approval.js +375 -0
  383. package/dist/tui/onboarding-wizard.js +240 -0
  384. package/dist/tui/permissions-picker.js +86 -0
  385. package/dist/tui/render.js +36 -1
  386. package/dist/tui/repl-render.js +176 -25
  387. package/dist/tui/repl-splash-art.js +16 -16
  388. package/dist/tui/repl-splash-mascot.js +48 -24
  389. package/dist/tui/repl-splash.js +22 -22
  390. package/dist/tui/repl.js +125 -45
  391. package/dist/tui/slash-palette.js +6 -6
  392. package/dist/tui/splash.js +2 -2
  393. package/dist/tui/status-bar.js +109 -31
  394. package/dist/tui/status-table.js +7 -0
  395. package/dist/tui/stickers-art.js +136 -0
  396. package/dist/tui/style-table.js +28 -0
  397. package/dist/tui/theme-table.js +29 -0
  398. package/dist/tui/thinking-spinner.js +123 -0
  399. package/dist/tui/tool-stream-pane.js +53 -4
  400. package/dist/tui/update-banner.js +27 -2
  401. package/dist/tui/vim-input.js +267 -0
  402. package/dist/tui/welcome-banner.js +107 -0
  403. package/dist/tui/welcome-data.js +293 -0
  404. package/dist/tui/workspace-context.js +2 -2
  405. package/package.json +31 -16
  406. package/test/scenarios/codegen-create-file.scenario.txt +13 -0
  407. package/test/scenarios/compact-force.scenario.txt +12 -0
  408. package/test/scenarios/identity.scenario.txt +12 -0
  409. package/test/scenarios/persona-handoff.scenario.txt +12 -0
  410. package/test/scenarios/walkback.scenario.txt +12 -0
  411. package/dist/core/engine/compaction-hook.js +0 -154
@@ -1,18 +1,18 @@
1
1
  /**
2
- * REPL session lifecycle - Sprint α5.7 (ADR-0056 PR-PUGI-CLI-REPL-DEFAULT).
2
+ * REPL session lifecycle - Sprint .
3
3
  *
4
4
  * Owns the state machine that the REPL UI subscribes to:
5
5
  *
6
- * 1. Open a server-side Pugi session via POST /api/pugi/sessions.
7
- * The CLI keeps a sessionId; reconnect uses it.
8
- * 2. Subscribe to GET /api/pugi/sessions/:id/stream (SSE). Each event
9
- * pushes one of: agent.spawned, agent.step, agent.tokens,
10
- * agent.completed, agent.blocked, agent.failed.
11
- * 3. Dispatch a brief via POST /api/pugi/sessions/:id/brief.
12
- * 4. Track active dispatches so the cap-warning gate has a number.
13
- * 5. Reconnect with Last-Event-ID on transient failure (10 retries,
14
- * exponential backoff capped at 5s) so the operator sees a stable
15
- * stream even on flaky connections.
6
+ * 1. Open a server-side Pugi session via POST /api/pugi/sessions.
7
+ * The CLI keeps a sessionId; reconnect uses it.
8
+ * 2. Subscribe to GET /api/pugi/sessions/:id/stream (SSE). Each event
9
+ * pushes one of: agent.spawned, agent.step, agent.tokens,
10
+ * agent.completed, agent.blocked, agent.failed.
11
+ * 3. Dispatch a brief via POST /api/pugi/sessions/:id/brief.
12
+ * 4. Track active dispatches so the cap-warning gate has a number.
13
+ * 5. Reconnect with Last-Event-ID on transient failure (10 retries,
14
+ * exponential backoff capped at 5s) so the operator sees a stable
15
+ * stream even on flaky connections.
16
16
  *
17
17
  * The module is environment-agnostic: callers inject `fetch` (Node 22
18
18
  * native or a stub from a test) and `EventSource` (a polyfill or
@@ -21,12 +21,13 @@
21
21
  * surface is exercisable without a network.
22
22
  *
23
23
  * Brand voice: the conversation transcript is line-based, persona-
24
- * prefixed (Mira / Marcus / Hiroshi / Vera / Anika / Olivia / Diego /
24
+ * prefixed (Pugi / Marcus / Hiroshi / Vera / Anika / Olivia / Diego /
25
25
  * Sofia per @pugi/personas). Forbidden words gate applies to every
26
26
  * line we synthesize client-side; server-side events are passed through
27
27
  * verbatim - the brand gate on those happens at the controller.
28
28
  */
29
29
  import { randomUUID } from 'node:crypto';
30
+ import { homedir } from 'node:os';
30
31
  import { getPersona } from '@pugi/personas';
31
32
  import { listRoles, getPersonaForRole } from '../agents/registry.js';
32
33
  import { evaluateCap, describeVerdict } from './cap-warning.js';
@@ -34,18 +35,39 @@ import { parseSlashCommand } from './slash-commands.js';
34
35
  import { webFetchTool } from '../../tools/web-fetch.js';
35
36
  import { loadSettings } from '../settings.js';
36
37
  import { getJobRegistry } from '../jobs/registry.js';
38
+ import { applyCompactMask } from '../compact/buffer-rewriter.js';
39
+ import { applyRewindMask } from '../checkpoint/rewinder.js';
40
+ import { evaluateAutoCompact } from '../compact/auto-trigger.js';
41
+ import { estimateTokensInMany } from '../compact/token-counter.js';
37
42
  import { extractAskTags, extractPlanReviewTags, signatureForAsk, } from './ask.js';
38
43
  import { existsSync, readdirSync, statSync } from 'node:fs';
39
44
  import { resolve as resolvePath } from 'node:path';
40
45
  import { CancellationToken } from './cancellation.js';
41
46
  import { DispatchFSM } from './dispatch-fsm.js';
47
+ import { computeCostUsd, formatCostUsd, formatTokens } from './model-pricing.js';
42
48
  const MAX_TRANSCRIPT_ROWS = 500;
43
49
  const MAX_TOOL_CALLS = 200;
50
+ /**
51
+ * small-CC-parity batch : width cap for the inline
52
+ * `streamingDelta` tail rendered next to the args while the call is
53
+ * `running`. Keeps the tool-stream row single-line on an 80-col
54
+ * terminal even when Bash output blasts through stdout. Exported so the
55
+ * spec can pin the truncation behaviour.
56
+ */
57
+ export const STREAMING_DELTA_MAX_CHARS = 80;
58
+ /**
59
+ * small-CC-parity batch : character cap for the
60
+ * collapsed `resultPreview` on a completed row. The pane shows
61
+ * `✓ Read(file) OK (2ms) "first 50 chars…"` so the operator sees what
62
+ * the tool produced without expanding. Per CEO spec (50 chars).
63
+ * Exported so the spec + the pane share one source of truth.
64
+ */
65
+ export const RESULT_PREVIEW_MAX_CHARS = 50;
44
66
  const MAX_RECONNECT_ATTEMPTS = 10;
45
67
  const RECONNECT_BASE_MS = 250;
46
68
  const RECONNECT_MAX_MS = 5_000;
47
69
  /**
48
- * α6.5 filewatch throttle: minimum gap between two file-change
70
+ * filewatch throttle: minimum gap between two file-change
49
71
  * system lines surfaced in the conversation pane. Per the sprint
50
72
  * spec, a noisy save burst should not flood the transcript - we
51
73
  * coalesce all chokidar batches that arrive inside the window into
@@ -60,7 +82,7 @@ const FILEWATCH_SYSTEM_LINE_GAP_MS = 5_000;
60
82
  * would accumulate forever, holding refs to thousands of FilewatchBatch
61
83
  * objects (each carrying its own events array). On overflow we drop
62
84
  * the OLDEST batch and surface a one-shot system warning so the
63
- * operator knows the buffer is shedding. triple-review P1 (PR #380).
85
+ * operator knows the buffer is shedding. triple-review P1 (PR).
64
86
  */
65
87
  const PENDING_FILEWATCH_BATCH_CAP = 100;
66
88
  /**
@@ -70,7 +92,7 @@ const PENDING_FILEWATCH_BATCH_CAP = 100;
70
92
  * CLI mints a fresh server session, swaps the consumer over, and
71
93
  * keeps running - but we cap the recovery to 3 attempts inside 60s
72
94
  * so a truly down admin-api fails loud instead of spinning forever.
73
- * (α6.14.2 wave 5 - CEO dogfood fix.)
95
+ *
74
96
  */
75
97
  const MAX_SESSION_RECREATE_ATTEMPTS = 3;
76
98
  const SESSION_RECREATE_WINDOW_MS = 60_000;
@@ -90,13 +112,13 @@ export class ReplSession {
90
112
  * with "Stream interrupted (HTTP 404)" loops, we mint a fresh
91
113
  * session and swap the consumer. Capped at MAX_SESSION_RECREATE_*
92
114
  * inside SESSION_RECREATE_WINDOW_MS so a permanently down admin-api
93
- * fails loud instead of looping silently. (α6.14.2 wave 5.)
115
+ * fails loud instead of looping silently.
94
116
  */
95
117
  recentRecreateAtMs = [];
96
118
  /**
97
119
  * True while a session-recreate POST is in flight. Guards against
98
120
  * the SSE stream firing multiple `onError(404)` callbacks racing
99
- * the in-flight createSession promise. (α6.14.2 wave 5.)
121
+ * the in-flight createSession promise.
100
122
  */
101
123
  recreatingSession = false;
102
124
  /**
@@ -108,11 +130,11 @@ export class ReplSession {
108
130
  * `shipped.` - the actual reply text was lost. By caching the last
109
131
  * non-trivial detail here, we can flush it into the transcript when
110
132
  * the agent completes so the operator sees what the persona actually
111
- * said. CEO wave-2 fix 2026-05-25.
133
+ * said. CEO wave-2 fix.
112
134
  */
113
135
  lastStepDetail = new Map();
114
136
  /**
115
- * Optional local SessionStore - α6.4. When non-null, every
137
+ * Optional local SessionStore - . When non-null, every
116
138
  * appendRow() call mirrors the row into the JSONL log so the
117
139
  * conversation can be restored via `/resume`. Errors from the store
118
140
  * are swallowed to a single system line (degradation, not crash).
@@ -146,20 +168,20 @@ export class ReplSession {
146
168
  * `/privacy` slash falls back to the contract doc with an "unknown"
147
169
  * banner when null.
148
170
  *
149
- * Triple-review P1 fix (2026-05-25): the prior build defined
171
+ * Triple-review P1 fix : the prior build defined
150
172
  * `renderPrivacyBanner` but never called it, and `/privacy` always
151
173
  * rendered with `null` mode. The contract was advertised but the
152
174
  * operator had no mode visibility.
153
175
  */
154
176
  privacyMode = null;
155
177
  /**
156
- * α6.5 Tier 0 / Tier 1 / chokidar wiring. The bootstrap builds the
178
+ * Tier 0 / Tier 1 / chokidar wiring. The bootstrap builds the
157
179
  * skeleton + working set + watcher once and hands them to the
158
180
  * session. The session uses them to:
159
181
  *
160
- * - render `/context` (count + cap + total bytes + skeleton size).
161
- * - emit throttled "file changed" system lines on watcher batches.
162
- * - forget removed files from the working set on `unlink`.
182
+ * - render `/context` (count + cap + total bytes + skeleton size).
183
+ * - emit throttled "file changed" system lines on watcher batches.
184
+ * - forget removed files from the working set on `unlink`.
163
185
  *
164
186
  * All three are optional - tests and minimal callers pass null /
165
187
  * undefined and the session degrades to "no three-tier integration"
@@ -181,7 +203,7 @@ export class ReplSession {
181
203
  * a summary that mentions how many additional files were touched.
182
204
  * Capped at PENDING_FILEWATCH_BATCH_CAP to bound memory growth
183
205
  * under long-running noisy filewatch sources (tsc --watch on a
184
- * 200-file project hammering for hours). triple-review P1 (PR #380).
206
+ * 200-file project hammering for hours). triple-review P1 (PR).
185
207
  */
186
208
  pendingFilewatchBatches = [];
187
209
  /**
@@ -198,7 +220,7 @@ export class ReplSession {
198
220
  * session.close() and watcher.close() does not run handlers on a
199
221
  * dead session. Without detachment, recordFilewatchBatch would
200
222
  * touch this.workingSet / this.transcript on a closed session.
201
- * triple-review P1 (PR #380).
223
+ * triple-review P1 (PR).
202
224
  */
203
225
  filewatchBatchHandler = (batch) => {
204
226
  this.recordFilewatchBatch(batch);
@@ -211,7 +233,7 @@ export class ReplSession {
211
233
  * signatures. The persona may emit the same envelope twice on network
212
234
  * retry; we suppress the duplicate so the operator does not see two
213
235
  * stacked modals. Capped at 32 entries - generous for a real session,
214
- * defensive against a hostile flood. (α6.3.)
236
+ * defensive against a hostile flood.
215
237
  */
216
238
  seenTagSignatures = [];
217
239
  /**
@@ -219,11 +241,11 @@ export class ReplSession {
219
241
  * `<pugi-ask>` open and close tags may arrive in separate
220
242
  * `agent.step` events when the upstream LLM token-streams output
221
243
  * char-by-char. We accumulate the running detail per taskId until a
222
- * complete envelope lands OR the turn ends. (α6.3.)
244
+ * complete envelope lands OR the turn ends.
223
245
  */
224
246
  askBuffer = new Map();
225
247
  /**
226
- * α6.9 dispatch FSM. One instance owned by the session; transitions
248
+ * dispatch FSM. One instance owned by the session; transitions
227
249
  * are mirrored into `state.dispatchState` via an onEnter listener so
228
250
  * subscribers see every change. Resets to `idle` after a terminal
229
251
  * transition (`completed` / `failed` / `aborted`) so the next brief
@@ -236,7 +258,7 @@ export class ReplSession {
236
258
  // accessor - callers cannot reach into this private field.
237
259
  fsm = new DispatchFSM();
238
260
  /**
239
- * α6.9 cancellation token for the currently in-flight dispatch.
261
+ * cancellation token for the currently in-flight dispatch.
240
262
  * Minted on `dispatchBrief()` and released on terminal transitions.
241
263
  * When non-null, calling `cancel()` aborts the token, closes the SSE
242
264
  * stream, and transitions the FSM to `aborting` → `aborted`.
@@ -245,7 +267,7 @@ export class ReplSession {
245
267
  */
246
268
  currentDispatchToken = null;
247
269
  /**
248
- * R2 P1 fix (Codex triple-review 2026-05-25): monotonic dispatch
270
+ * R2 P1 fix (Codex triple-review): monotonic dispatch
249
271
  * sequence id. Incremented on every `dispatchBrief()`. The
250
272
  * agent.spawned handler stamps the current value into
251
273
  * `taskDispatchSeq[event.taskId]`. Terminal handlers
@@ -268,7 +290,7 @@ export class ReplSession {
268
290
  */
269
291
  taskDispatchSeq = new Map();
270
292
  /**
271
- * R3 P1 fix (Codex triple-review 2026-05-25): wall-clock guard used to
293
+ * R3 P1 fix (Codex triple-review): wall-clock guard used to
272
294
  * drop SSE events whose `event.timestamp` predates the current
273
295
  * dispatch. The R2 seq gate alone fails when a LATE `agent.spawned`
274
296
  * from brief #1 arrives AFTER brief #2 mints a new dispatch token:
@@ -289,7 +311,7 @@ export class ReplSession {
289
311
  * the turn ends with this flag still set, we emit a system-line
290
312
  * warning that the persona produced an incomplete tag - the partial
291
313
  * XML is silently dropped (the parser already withheld it from the
292
- * cleaned body). Codex triple-review P2 (PR #375).
314
+ * cleaned body). Codex triple-review P2 (PR).
293
315
  */
294
316
  askBufferPending = new Set();
295
317
  constructor(options) {
@@ -315,6 +337,19 @@ export class ReplSession {
315
337
  toolCalls: [],
316
338
  transcript: [],
317
339
  tokensDownstreamTotal: 0,
340
+ // cost-meter sprint — cost accumulators land at zero on boot.
341
+ // `sessionStartedAtEpochMs` is set at construction time (vs the
342
+ // server-side `agent.session.opened` event) so the elapsed slot
343
+ // on the status row starts ticking the moment the REPL mounts.
344
+ sessionTokensIn: 0,
345
+ sessionTokensOut: 0,
346
+ sessionCostUsd: 0,
347
+ sessionStartedAtEpochMs: this.now(),
348
+ recentTurns: [],
349
+ turnTokensIn: 0,
350
+ turnTokensOut: 0,
351
+ turnCostUsd: 0,
352
+ lastTurnDelta: null,
318
353
  briefStartedAtEpochMs: undefined,
319
354
  pendingAsk: null,
320
355
  pendingAskSource: null,
@@ -322,8 +357,9 @@ export class ReplSession {
322
357
  pendingPlanReviewSource: null,
323
358
  dispatchState: 'idle',
324
359
  dispatchToolLabel: null,
360
+ lastCompletedOutcome: null,
325
361
  };
326
- // α6.9: mirror every FSM transition into the public state so the
362
+ // : mirror every FSM transition into the public state so the
327
363
  // status-bar surface can rerender on the next frame. Local listener
328
364
  // is intentionally cheap — just a patch + clear the per-state tool
329
365
  // label when leaving `tool_running`.
@@ -359,6 +395,7 @@ export class ReplSession {
359
395
  apiUrl: this.options.apiUrl,
360
396
  apiKey: this.options.apiKey,
361
397
  workspace: this.options.workspace,
398
+ cyberZoo: this.options.cyberZoo,
362
399
  });
363
400
  this.patch({ sessionId, connection: 'connecting' });
364
401
  this.openStream();
@@ -371,6 +408,18 @@ export class ReplSession {
371
408
  // admin-api down) is silent - the operator can still type
372
409
  // `/privacy` to see the contract.
373
410
  void this.fetchAndAnnouncePrivacyMode().catch(() => undefined);
411
+ // silently drain any feedback envelopes
412
+ // that landed offline during a previous session. Best-effort —
413
+ // a failed flush leaves the queue intact for the next start.
414
+ // Never blocks bootstrap.
415
+ void this.flushFeedbackQueueOnBootstrap().catch(() => undefined);
416
+ // BT 9 Phase 2 : codegraph cold-start hook.
417
+ // Surfaces ONE of two nudges:
418
+ // - stale-index reminder ("Codegraph index is N days old…")
419
+ // - 30-day post-decline reminder ("Detected medium TS repo…")
420
+ // Skips silently in every other case. Best-effort — a failed
421
+ // detection NEVER blocks bootstrap (the helper itself catches).
422
+ void this.runCodegraphColdStart().catch(() => undefined);
374
423
  }
375
424
  catch (error) {
376
425
  this.appendSystemLine(`Could not open Pugi session: ${this.errorMessage(error)}`);
@@ -385,7 +434,7 @@ export class ReplSession {
385
434
  * banner is preferable to a noisy "could not fetch privacy mode"
386
435
  * line on every login.
387
436
  *
388
- * Triple-review P1 fix (2026-05-25): without this call,
437
+ * Triple-review P1 fix : without this call,
389
438
  * `renderPrivacyBanner` was defined but never reached the wire, and
390
439
  * `/privacy` always rendered with `null` mode.
391
440
  */
@@ -414,13 +463,69 @@ export class ReplSession {
414
463
  // Silent fail - offline / DNS / unauth all collapse to no banner.
415
464
  }
416
465
  }
466
+ /**
467
+ * on bootstrap, drain the local feedback
468
+ * queue silently. Operators who ran `pugi feedback` while offline
469
+ * see their envelopes flushed on the next online session without
470
+ * any extra command. The drain is best-effort and never blocks
471
+ * the REPL — a failed flush leaves the queue intact for the next
472
+ * bootstrap attempt.
473
+ */
474
+ async flushFeedbackQueueOnBootstrap() {
475
+ const { flushFeedbackQueueSilently } = await import('../../runtime/commands/feedback.js');
476
+ await flushFeedbackQueueSilently(process.cwd(), {
477
+ apiUrl: this.options.apiUrl,
478
+ apiKey: this.options.apiKey,
479
+ });
480
+ }
481
+ /**
482
+ * BT 9 Phase 2 : codegraph cold-start nudge.
483
+ *
484
+ * Surfaces ONE of two nudges on REPL boot when the gate trips:
485
+ * - 30-day post-decline reminder ("Detected medium TS repo…")
486
+ * - stale-index reminder ("Codegraph index is N days old…")
487
+ *
488
+ * The evaluator is pure; we stamp `lastReindexCheckAt` here so the
489
+ * stale-index nudge throttles к once-per-day. The init-flow first-
490
+ * run prompt is handled separately by `pugi init` to avoid double-
491
+ * prompting в the common "init + then code" boot sequence.
492
+ *
493
+ * Best-effort: any error inside the codegraph module is swallowed —
494
+ * a cold-start nudge that breaks the REPL would be worse than no
495
+ * nudge at all.
496
+ */
497
+ async runCodegraphColdStart() {
498
+ try {
499
+ const workspaceRoot = this.options.workspace?.workspaceCwd ?? process.cwd();
500
+ const { evaluateColdStart } = await import('../codegraph/offer-hook.js');
501
+ const verdict = evaluateColdStart({ workspaceRoot });
502
+ if (verdict.kind === 'silent')
503
+ return;
504
+ if (verdict.kind === 'stale-index') {
505
+ this.appendSystemLine(verdict.message);
506
+ const { markReindexChecked } = await import('../codegraph/decision-store.js');
507
+ markReindexChecked(workspaceRoot);
508
+ return;
509
+ }
510
+ // 'remind' — surface the offer copy as a system line. Operator
511
+ // accepts via `/codegraph-status --install` OR explicitly via
512
+ // `pugi mcp install codegraph codegraph serve --mcp`.
513
+ this.appendSystemLine('');
514
+ this.appendSystemLine(verdict.message);
515
+ this.appendSystemLine(' Accept: run `pugi mcp install codegraph codegraph serve --mcp && pugi mcp trust codegraph`');
516
+ this.appendSystemLine(' Skip: /codegraph-status to inspect the decision; the prompt re-appears in 30 days');
517
+ }
518
+ catch {
519
+ // Codegraph nudge is decoration — failure must NEVER surface.
520
+ }
521
+ }
417
522
  /**
418
523
  * Tear down the SSE stream and stop the reconnect timer. The session
419
524
  * id stays valid server-side; `pugi resume <id>` reopens later.
420
525
  */
421
526
  close() {
422
527
  this.closed = true;
423
- // α6.9: fire the cancellation token before tearing down the stream
528
+ // : fire the cancellation token before tearing down the stream
424
529
  // so any in-flight tool sees the abort signal AND any pending
425
530
  // PostBrief promise can short-circuit. Idempotent — token.abort()
426
531
  // is a no-op when already aborted.
@@ -446,32 +551,32 @@ export class ReplSession {
446
551
  // run a handler on a dead session. The handlers themselves also
447
552
  // hard-guard on `this.closed`, but detaching is the load-bearing
448
553
  // fix - it severs the strong reference the watcher held on the
449
- // session callback, which otherwise blocks GC. triple-review P1 (PR #380).
554
+ // session callback, which otherwise blocks GC. triple-review P1 (PR).
450
555
  if (this.watcher) {
451
556
  this.watcher.off('batch', this.filewatchBatchHandler);
452
557
  this.watcher.off('capExceeded', this.filewatchCapHandler);
453
558
  }
454
559
  }
455
- /* ------------- α6.9 cancellation surface -------------- */
560
+ /* ------------- cancellation surface -------------- */
456
561
  /**
457
562
  * Operator-driven abort for the in-flight dispatch. Idempotent — a
458
563
  * second call while already in `aborting` / `aborted` is a no-op.
459
564
  *
460
565
  * Steps (in order):
461
566
  *
462
- * 1. Snapshot the current state. If terminal or idle, no-op.
463
- * 2. Transition the FSM to `aborting` so the bottom-bar shows the
464
- * pending shutdown immediately (the operator gets feedback
465
- * before any IO completes).
466
- * 3. Abort the cancellation token. This fans out to every listener
467
- * that was attached during the dispatch — chiefly the SSE
468
- * stream wrapper (which calls `streamHandle.close()`) and any
469
- * mid-flight tool executor that polled `isAborted`.
470
- * 4. Append a system line so the conversation reads "Aborted." at
471
- * the operator's last input position.
472
- * 5. Transition to `aborted` (terminal). The next operator brief
473
- * mints a fresh token + transitions back to
474
- * `awaiting_response`.
567
+ * 1. Snapshot the current state. If terminal or idle, no-op.
568
+ * 2. Transition the FSM to `aborting` so the bottom-bar shows the
569
+ * pending shutdown immediately (the operator gets feedback
570
+ * before any IO completes).
571
+ * 3. Abort the cancellation token. This fans out to every listener
572
+ * that was attached during the dispatch — chiefly the SSE
573
+ * stream wrapper (which calls `streamHandle.close()`) and any
574
+ * mid-flight tool executor that polled `isAborted`.
575
+ * 4. Append a system line so the conversation reads "Aborted." at
576
+ * the operator's last input position.
577
+ * 5. Transition to `aborted` (terminal). The next operator brief
578
+ * mints a fresh token + transitions back to
579
+ * `awaiting_response`.
475
580
  *
476
581
  * Returns `true` when an abort was actually issued (state was
477
582
  * non-terminal + non-idle), `false` otherwise.
@@ -539,6 +644,73 @@ export class ReplSession {
539
644
  getDispatchState() {
540
645
  return this.fsm.current;
541
646
  }
647
+ /**
648
+ * BT 8 (the upstream tool parity): Esc-Esc walkback. Trim the last
649
+ * operator/persona turn pair from the in-memory transcript so the
650
+ * model's next call sees the conversation as if the most recent
651
+ * turn never happened. The local SessionStore still has the events
652
+ * on disk (append-only); the in-memory mask is advisory and the next
653
+ * `/compact` boundary will fold them naturally.
654
+ *
655
+ * Refusal modes:
656
+ * - `'no-turn'` - transcript has no operator/persona row to pop.
657
+ * - `'in-flight'` - dispatch is mid-flight; popping would race with
658
+ * the streaming persona row. The operator must
659
+ * cancel (Ctrl+C) before walking back.
660
+ *
661
+ * Success mode:
662
+ * - `'walked-back'` - the trailing persona row + the operator row
663
+ * that triggered it are gone from the transcript.
664
+ * A `↩ walked back 1 turn` status row is appended
665
+ * so the operator sees the state change without
666
+ * guessing.
667
+ *
668
+ * The mask is in-memory only on purpose. Disk-side rewind already has
669
+ * a separate first-class command (`/rewind`) with checkpoint
670
+ * semantics — the Esc-Esc shortcut is a one-tap "oops, undo that" for
671
+ * the live transcript, NOT a transactional rollback.
672
+ */
673
+ walkbackLastTurn() {
674
+ // Refuse while a dispatch is running. Popping the operator row that
675
+ // is currently driving the model's response would leave the persona
676
+ // line orphaned on the next streamed chunk; the FSM also lacks a
677
+ // clean teardown path here. The operator gets a one-line refusal
678
+ // and can Ctrl+C first if they really want to walk back.
679
+ const current = this.fsm.current;
680
+ if (current !== 'idle' && current !== 'completed'
681
+ && current !== 'aborted' && current !== 'failed') {
682
+ this.appendSystemLine('Walkback refused: dispatch in flight. Cancel with Ctrl+C, then Esc-Esc again.');
683
+ return 'in-flight';
684
+ }
685
+ // Find the trailing operator row. Walking backwards because the
686
+ // transcript is append-only and the most recent operator turn is
687
+ // by definition the last `source === 'operator'` row.
688
+ const transcript = this.state.transcript;
689
+ let operatorIdx = -1;
690
+ for (let i = transcript.length - 1; i >= 0; i -= 1) {
691
+ const row = transcript[i];
692
+ if (row.source === 'operator') {
693
+ operatorIdx = i;
694
+ break;
695
+ }
696
+ }
697
+ if (operatorIdx === -1) {
698
+ // No operator turn to pop. Quiet refusal — surfacing a "nothing
699
+ // to undo" line on every accidental double-Esc would be noisy.
700
+ return 'no-turn';
701
+ }
702
+ // Trim everything from the operator row onward (its echo + any
703
+ // persona/system rows that landed in response). The slice keeps
704
+ // every row BEFORE the operator turn, which is the conversation
705
+ // exactly as it stood right before the operator pressed Enter.
706
+ const trimmed = transcript.slice(0, operatorIdx);
707
+ this.patch({ transcript: trimmed });
708
+ // Status row so the operator sees the state change without
709
+ // guessing. Brand voice: single ASCII line, return-arrow glyph
710
+ // (U+21A9) which renders across every modern terminal.
711
+ this.appendSystemLine('↩ walked back 1 turn');
712
+ return 'walked-back';
713
+ }
542
714
  /**
543
715
  * Current cancellation token. Returned for the tool execution path
544
716
  * (file-tools.ts) so it can pass the token down into a ToolContext
@@ -564,7 +736,7 @@ export class ReplSession {
564
736
  // UI overlays - no transport interaction.
565
737
  return verdict;
566
738
  case 'quit':
567
- // UI Designer audit 2026-05-25: "Brief it. It ships." is reserved
739
+ // UI Designer audit: "Brief it. It ships." is reserved
568
740
  // for identity intro + landing per wave-4 prompt rule. Drop the
569
741
  // tagline drift here; tell the operator what happened and how to
570
742
  // resume.
@@ -578,7 +750,7 @@ export class ReplSession {
578
750
  return verdict;
579
751
  }
580
752
  case 'delegate': {
581
- // α7.5 Phase 1: surface the dispatch intent inline. The actual
753
+ // Phase 1: surface the dispatch intent inline. The actual
582
754
  // wire shape (POST /api/pugi/sessions/:id/delegate) requires the
583
755
  // SDK transport extension that ships alongside this PR; the
584
756
  // REPL session module wires the call when the matching transport
@@ -606,7 +778,43 @@ export class ReplSession {
606
778
  return verdict;
607
779
  }
608
780
  case 'jobs': {
609
- await this.dispatchJobs();
781
+ // cleanup : `/jobs --watch` mounts the
782
+ // live Ink TUI from inside the REPL. The dispatcher does NOT
783
+ // mount the watcher itself (that would unmount the REPL's
784
+ // own Ink tree) — instead it surfaces the shell command so
785
+ // the operator runs the watcher in a fresh terminal. Bare
786
+ // `/jobs` continues to render the one-shot snapshot.
787
+ if (verdict.watch) {
788
+ this.appendSystemLine('Run `pugi jobs --watch` from a fresh shell — the live TUI cannot share the REPL Ink tree.');
789
+ }
790
+ else {
791
+ await this.dispatchJobs();
792
+ }
793
+ return verdict;
794
+ }
795
+ case 'cancel': {
796
+ // small-CC-parity batch : forward the parsed
797
+ // mode + dispatchId to `runCancelCommand`. The dispatcher uses
798
+ // a dynamic import so the cancel module's filesystem helpers
799
+ // stay out of the REPL keystroke hot path; same separation as
800
+ // `/redo`, `/prd-check`, `/chain`. The runner writes its
801
+ // output lines through `appendSystemLine` so the verdict
802
+ // lands on the system pane alongside other slash results.
803
+ try {
804
+ const { runCancelCommand } = await import('../../runtime/commands/cancel.js');
805
+ const cancelMode = verdict.mode === 'list'
806
+ ? { kind: 'list' }
807
+ : verdict.mode === 'all'
808
+ ? { kind: 'all' }
809
+ : { kind: 'one', dispatchId: verdict.dispatchId };
810
+ await runCancelCommand(cancelMode, {
811
+ write: (line) => this.appendSystemLine(line),
812
+ });
813
+ }
814
+ catch (err) {
815
+ const message = err instanceof Error ? err.message : String(err);
816
+ this.appendSystemLine(`/cancel failed: ${message}`);
817
+ }
610
818
  return verdict;
611
819
  }
612
820
  case 'diff': {
@@ -614,11 +822,15 @@ export class ReplSession {
614
822
  return verdict;
615
823
  }
616
824
  case 'cost': {
617
- this.dispatchCost();
825
+ await this.dispatchCost();
826
+ return verdict;
827
+ }
828
+ case 'quota': {
829
+ await this.dispatchQuota();
618
830
  return verdict;
619
831
  }
620
832
  case 'status': {
621
- this.dispatchStatus();
833
+ await this.dispatchStatus();
622
834
  return verdict;
623
835
  }
624
836
  case 'consensus': {
@@ -640,7 +852,7 @@ export class ReplSession {
640
852
  return verdict;
641
853
  }
642
854
  case 'ask': {
643
- // α6.3: synthesise a local yes/no `<pugi-ask>` modal so the
855
+ // : synthesise a local yes/no `<pugi-ask>` modal so the
644
856
  // operator can exercise the question UI without a persona-side
645
857
  // round trip. The REPL UI mounts the modal from the resulting
646
858
  // `pendingAsk` state; on resolution the encoded verdict lands
@@ -663,12 +875,1007 @@ export class ReplSession {
663
875
  await this.dispatchPrivacy();
664
876
  return verdict;
665
877
  }
878
+ case 'init': {
879
+ // β1 Sl11 → β1a r1 (real inline scaffold): invoke
880
+ // `scaffoldPugiWorkspace` directly so the operator gets the
881
+ // same .pugi/ setup they would from `pugi init` on a fresh
882
+ // shell. Already-initialised workspaces (every artifact already
883
+ // present) get the "Already initialised" copy; partial / fresh
884
+ // workspaces get the full Created+Skipped breakdown. Default
885
+ // skills install is best-effort — any error from the bundled
886
+ // pack is surfaced as a system line and does not break the
887
+ // REPL session. The dynamic import keeps the slash dispatcher
888
+ // free of a runtime/cli.ts cycle on every keystroke.
889
+ try {
890
+ const { scaffoldPugiWorkspace } = await import('../../runtime/cli.js');
891
+ const lines = [];
892
+ const result = await scaffoldPugiWorkspace({
893
+ cwd: process.cwd(),
894
+ // Slash callers default to the full default-skills pack so
895
+ // the in-REPL experience matches `pugi init`. Operators who
896
+ // want a minimal scaffold still have the shell command.
897
+ noDefaults: false,
898
+ log: (line) => {
899
+ const trimmed = line.replace(/\n+$/u, '');
900
+ if (trimmed.length > 0)
901
+ lines.push(trimmed);
902
+ },
903
+ });
904
+ if (result.alreadyInitialized) {
905
+ this.appendSystemLine(`.pugi/ already initialised at ${result.root}. ${result.skipped.length} artefact(s) verified.`);
906
+ }
907
+ else {
908
+ this.appendSystemLine(`Pugi initialised at ${result.root}. Created ${result.created.length} artefact(s), skipped ${result.skipped.length}.`);
909
+ }
910
+ if (result.defaultSkills.length > 0) {
911
+ const installed = result.defaultSkills.filter((s) => s.status === 'installed').length;
912
+ const skippedSkills = result.defaultSkills.filter((s) => s.status === 'skipped-existing').length;
913
+ this.appendSystemLine(`Default skills: ${installed} installed, ${skippedSkills} already present.`);
914
+ }
915
+ for (const line of lines)
916
+ this.appendSystemLine(line);
917
+ }
918
+ catch (error) {
919
+ const message = error instanceof Error ? error.message : String(error);
920
+ this.appendSystemLine(`/init failed: ${message}`);
921
+ }
922
+ return verdict;
923
+ }
924
+ case 'mcp': {
925
+ // β4 Sl7 : /mcp [sub] [args...] forwards to the
926
+ // runtime command. We deliberately route through the same
927
+ // entry-point used by `pugi mcp` from a fresh shell so the
928
+ // surface stays single-sourced. `serve` is refused inline —
929
+ // booting an MCP server inside an active REPL would compete
930
+ // with the REPL itself for stdio, which is exactly the wrong
931
+ // thing to do.
932
+ if (verdict.args[0] === 'serve') {
933
+ this.appendSystemLine('/mcp serve is not safe inside the REPL (it competes for stdio). ' +
934
+ 'Run `pugi mcp serve` from a fresh shell instead.');
935
+ return verdict;
936
+ }
937
+ try {
938
+ const { runMcpCommand } = await import('../../runtime/commands/mcp.js');
939
+ const lines = [];
940
+ await runMcpCommand(verdict.args, {
941
+ workspaceRoot: process.cwd(),
942
+ writeOutput: (_payload, text) => {
943
+ const trimmed = text.replace(/\n+$/u, '');
944
+ if (trimmed.length > 0)
945
+ lines.push(trimmed);
946
+ },
947
+ });
948
+ for (const line of lines)
949
+ this.appendSystemLine(line);
950
+ if (lines.length === 0) {
951
+ this.appendSystemLine('/mcp: no output.');
952
+ }
953
+ }
954
+ catch (error) {
955
+ const message = error instanceof Error ? error.message : String(error);
956
+ this.appendSystemLine(`/mcp failed: ${message}`);
957
+ }
958
+ return verdict;
959
+ }
960
+ case 'theme': {
961
+ // /theme [name] [--persist|--reset|--list]
962
+ // forwards to the shared `runThemeCommand` runner. Same async
963
+ // buffer-then-flush pattern as `/style` so a future async
964
+ // write path inside the runner cannot drop a tail emission
965
+ // and so multi-line payloads (banner + preview table) land
966
+ // one row per visual line in the conversation pane.
967
+ try {
968
+ const { runThemeCommand } = await import('../../runtime/commands/theme.js');
969
+ const lines = [];
970
+ await runThemeCommand(verdict.args, {
971
+ workspaceRoot: process.cwd(),
972
+ writeOutput: (_payload, text) => {
973
+ for (const raw of text.split('\n')) {
974
+ const trimmed = raw.replace(/\s+$/u, '');
975
+ lines.push(trimmed);
976
+ }
977
+ },
978
+ });
979
+ if (lines.length === 0) {
980
+ this.appendSystemLine('/theme: no output.');
981
+ }
982
+ else {
983
+ for (const line of lines)
984
+ this.appendSystemLine(line);
985
+ }
986
+ }
987
+ catch (error) {
988
+ const message = error instanceof Error ? error.message : String(error);
989
+ this.appendSystemLine(`/theme failed: ${message}`);
990
+ }
991
+ return verdict;
992
+ }
993
+ case 'style': {
994
+ // /style [name] [--persist|--reset|--list]
995
+ // forwards to the shared `runStyleCommand` runner so the slash
996
+ // + top-level surfaces share one code path. Dynamic import
997
+ // keeps the dispatcher free of the output-style module graph
998
+ // until the operator first invokes the slash. The runner's
999
+ // exit code is captured but NOT propagated to process.exitCode
1000
+ // — REPL session should not die because a bad preset slug was
1001
+ // typed in the input box.
1002
+ try {
1003
+ const { runStyleCommand } = await import('../../runtime/commands/style.js');
1004
+ // L18 P1 fix : writeOutput is invoked SYNCHRONOUSLY
1005
+ // by `runStyleCommand` for each emitted block. We buffer every
1006
+ // emission into `lines` and flush after the await resolves so
1007
+ // that:
1008
+ // (1) any future async write path inside the runner cannot
1009
+ // drop a tail emission (callback never references the
1010
+ // Ink frame directly), and
1011
+ // (2) multi-line payloads (e.g. the active-style banner +
1012
+ // catalogue table) render one row per visual line in the
1013
+ // conversation pane, matching the `/stickers` surface.
1014
+ const lines = [];
1015
+ await runStyleCommand(verdict.args, {
1016
+ workspaceRoot: process.cwd(),
1017
+ writeOutput: (_payload, text) => {
1018
+ for (const raw of text.split('\n')) {
1019
+ const trimmed = raw.replace(/\s+$/u, '');
1020
+ lines.push(trimmed);
1021
+ }
1022
+ },
1023
+ });
1024
+ if (lines.length === 0) {
1025
+ this.appendSystemLine('/style: no output.');
1026
+ }
1027
+ else {
1028
+ for (const line of lines)
1029
+ this.appendSystemLine(line);
1030
+ }
1031
+ }
1032
+ catch (error) {
1033
+ const message = error instanceof Error ? error.message : String(error);
1034
+ this.appendSystemLine(`/style failed: ${message}`);
1035
+ }
1036
+ return verdict;
1037
+ }
1038
+ case 'onboarding': {
1039
+ // /onboarding forwards to the shared
1040
+ // `runOnboardingCommand` runner. From inside the REPL we ALWAYS
1041
+ // route through the non-interactive snapshot path — the REPL
1042
+ // already owns the Ink tree and mounting a second Ink wizard
1043
+ // on top would conflict over stdin raw mode. Operators who
1044
+ // want the interactive walk exit the REPL and run
1045
+ // `pugi onboarding` from a fresh shell; the slash surface
1046
+ // surfaces the recap card + hints inline so the operator
1047
+ // sees current values without leaving the session.
1048
+ try {
1049
+ const { runOnboardingCommand } = await import('../../runtime/commands/onboarding.js');
1050
+ const { resolveActiveCredential } = await import('../credentials.js');
1051
+ const credential = resolveActiveCredential();
1052
+ const lines = [];
1053
+ await runOnboardingCommand(verdict.args, {
1054
+ workspaceRoot: process.cwd(),
1055
+ env: process.env,
1056
+ authPresent: credential !== null,
1057
+ interactive: false,
1058
+ writeOutput: (_payload, text) => {
1059
+ const trimmed = text.replace(/\n+$/u, '');
1060
+ if (trimmed.length > 0)
1061
+ lines.push(trimmed);
1062
+ },
1063
+ });
1064
+ for (const line of lines)
1065
+ this.appendSystemLine(line);
1066
+ if (lines.length === 0) {
1067
+ this.appendSystemLine('/onboarding: no output.');
1068
+ }
1069
+ }
1070
+ catch (error) {
1071
+ const message = error instanceof Error ? error.message : String(error);
1072
+ this.appendSystemLine(`/onboarding failed: ${message}`);
1073
+ }
1074
+ return verdict;
1075
+ }
1076
+ case 'vim': {
1077
+ // /vim forwards to the shared
1078
+ // `runVimCommand` runner so the slash + top-level surfaces
1079
+ // stay single-sourced. Dynamic import mirrors /style so the
1080
+ // dispatcher does not drag the vim module graph into every
1081
+ // keystroke.
1082
+ //
1083
+ // The runner mutates `~/.pugi/config.json::vimMode`; the
1084
+ // active REPL session does NOT live-pick-up the flip (the
1085
+ // VimInput wrapper is mounted once at REPL boot). Operators
1086
+ // get a hint that the next session will reflect the change.
1087
+ // A follow-up sprint can plumb a state-store subscriber so
1088
+ // the flip takes effect mid-session.
1089
+ try {
1090
+ const { runVimCommand } = await import('../../runtime/commands/vim.js');
1091
+ const lines = [];
1092
+ await runVimCommand(verdict.args, {
1093
+ env: process.env,
1094
+ writeOutput: (_payload, text) => {
1095
+ for (const raw of text.split('\n')) {
1096
+ const trimmed = raw.replace(/\s+$/u, '');
1097
+ lines.push(trimmed);
1098
+ }
1099
+ },
1100
+ });
1101
+ if (lines.length === 0) {
1102
+ this.appendSystemLine('/vim: no output.');
1103
+ }
1104
+ else {
1105
+ for (const line of lines)
1106
+ this.appendSystemLine(line);
1107
+ }
1108
+ }
1109
+ catch (error) {
1110
+ const message = error instanceof Error ? error.message : String(error);
1111
+ this.appendSystemLine(`/vim failed: ${message}`);
1112
+ }
1113
+ return verdict;
1114
+ }
1115
+ case 'doctor': {
1116
+ // L17 : run the doctor probe sweep inline. We
1117
+ // dynamic-import the runtime/commands/doctor module so the
1118
+ // slash dispatcher does not pull the diagnostics graph
1119
+ // (execFileSync + fs probes) into every keystroke. The
1120
+ // module's output is captured into local lines so we can
1121
+ // render it as system entries in the conversation pane;
1122
+ // an Ink-rendered table inside the REPL frame is a follow-up.
1123
+ try {
1124
+ const { runDoctorCommand, defaultHome } = await import('../../runtime/commands/doctor.js');
1125
+ const lines = [];
1126
+ await runDoctorCommand({
1127
+ cwd: process.cwd(),
1128
+ home: defaultHome(),
1129
+ env: process.env,
1130
+ json: false,
1131
+ writeOutput: (_payload, text) => {
1132
+ const trimmed = text.replace(/\n+$/u, '');
1133
+ if (trimmed.length > 0)
1134
+ lines.push(trimmed);
1135
+ },
1136
+ });
1137
+ for (const line of lines)
1138
+ this.appendSystemLine(line);
1139
+ if (lines.length === 0) {
1140
+ this.appendSystemLine('/doctor: no output.');
1141
+ }
1142
+ }
1143
+ catch (error) {
1144
+ const message = error instanceof Error ? error.message : String(error);
1145
+ this.appendSystemLine(`/doctor failed: ${message}`);
1146
+ }
1147
+ return verdict;
1148
+ }
1149
+ case 'prd-check': {
1150
+ // : forward to the same handler the shell
1151
+ // surface uses so the verdict is identical between
1152
+ // `/prd-check` and `pugi prd-check`. Dynamic-import the
1153
+ // module to keep the parser + verifier graph out of the
1154
+ // REPL hot path.
1155
+ //
1156
+ // final : the runner now also honours
1157
+ // `--session` mode (orthogonal to the verifier graph — walks
1158
+ // up for PRD.md, reads NDJSON turns, dispatches a cross-
1159
+ // review subagent). We stream the runner's status lines
1160
+ // directly to the system pane so the operator sees
1161
+ // "Locating PRD..." / "Reviewing against PRD..." while the
1162
+ // dispatch is in flight, then the structured Satisfied /
1163
+ // Outstanding lists when it lands.
1164
+ try {
1165
+ const { parsePrdCheckArgs, runPrdCheckCommand } = await import('../../runtime/commands/prd-check.js');
1166
+ const parsed = parsePrdCheckArgs(verdict.args, { jsonDefault: false });
1167
+ if (!parsed.ok) {
1168
+ this.appendSystemLine(`/prd-check: ${parsed.error}`);
1169
+ return verdict;
1170
+ }
1171
+ let sawOutput = false;
1172
+ await runPrdCheckCommand({
1173
+ cwd: process.cwd(),
1174
+ ...(parsed.prdPath !== undefined ? { prdPath: parsed.prdPath } : {}),
1175
+ flags: parsed.flags,
1176
+ // The REPL slash does not have a snapshot of the CLI
1177
+ // command registry, so we pass an empty set; the
1178
+ // command:<name> verifier will report FAIL for now.
1179
+ // This is a deliberate trade-off — the slash surface
1180
+ // primarily exists for quick eyeball checks during a
1181
+ // session; the shell surface (which DOES inject the
1182
+ // full registry) is the canonical gate.
1183
+ knownCommands: new Set(),
1184
+ writeOutput: (_payload, text) => {
1185
+ const trimmed = text.replace(/\n+$/u, '');
1186
+ if (trimmed.length > 0) {
1187
+ this.appendSystemLine(trimmed);
1188
+ sawOutput = true;
1189
+ }
1190
+ },
1191
+ });
1192
+ if (!sawOutput) {
1193
+ this.appendSystemLine('/prd-check: no output.');
1194
+ }
1195
+ }
1196
+ catch (error) {
1197
+ const message = error instanceof Error ? error.message : String(error);
1198
+ this.appendSystemLine(`/prd-check failed: ${message}`);
1199
+ }
1200
+ return verdict;
1201
+ }
1202
+ case 'chain': {
1203
+ // : forward to the shell-surface runner so
1204
+ // the slash + top-level CLI share one parser + dispatcher.
1205
+ // Dynamic import keeps the chain module out of the REPL hot
1206
+ // path. The slash variant does NOT inject the live delegate
1207
+ // wire-up — operators wanting full dispatch run `pugi chain
1208
+ // next` from a fresh shell. The slash form is best-effort for
1209
+ // status / show / list which are read-only.
1210
+ try {
1211
+ const { runChainCommand } = await import('../../runtime/commands/chain.js');
1212
+ const lines = [];
1213
+ await runChainCommand(verdict.args, {
1214
+ cwd: process.cwd(),
1215
+ json: false,
1216
+ writeOutput: (_payload, text) => {
1217
+ const trimmed = text.replace(/\n+$/u, '');
1218
+ if (trimmed.length > 0)
1219
+ lines.push(trimmed);
1220
+ },
1221
+ });
1222
+ for (const line of lines)
1223
+ this.appendSystemLine(line);
1224
+ if (lines.length === 0) {
1225
+ this.appendSystemLine('/chain: no output.');
1226
+ }
1227
+ }
1228
+ catch (error) {
1229
+ const message = error instanceof Error ? error.message : String(error);
1230
+ this.appendSystemLine(`/chain failed: ${message}`);
1231
+ }
1232
+ return verdict;
1233
+ }
1234
+ case 'codegraph-status': {
1235
+ // BT 9 Phase 2 : forward to the runner. The
1236
+ // bare form renders the four-row status table; flags handle
1237
+ // install / reindex / offer. Dynamic import keeps the
1238
+ // codegraph module out of the REPL hot path until first use.
1239
+ try {
1240
+ const { runCodegraphStatusCommand } = await import('../../runtime/commands/codegraph-status.js');
1241
+ const lines = [];
1242
+ const workspaceRoot = this.options.workspace?.workspaceCwd ?? process.cwd();
1243
+ await runCodegraphStatusCommand(verdict.args, {
1244
+ workspaceRoot,
1245
+ writeOutput: (_payload, text) => {
1246
+ for (const raw of text.split('\n')) {
1247
+ const trimmed = raw.replace(/\s+$/u, '');
1248
+ lines.push(trimmed);
1249
+ }
1250
+ },
1251
+ });
1252
+ if (lines.length === 0) {
1253
+ this.appendSystemLine('/codegraph-status: no output.');
1254
+ }
1255
+ else {
1256
+ for (const line of lines)
1257
+ this.appendSystemLine(line);
1258
+ }
1259
+ }
1260
+ catch (error) {
1261
+ const message = error instanceof Error ? error.message : String(error);
1262
+ this.appendSystemLine(`/codegraph-status failed: ${message}`);
1263
+ }
1264
+ return verdict;
1265
+ }
1266
+ case 'permissions': {
1267
+ // handle the `/permissions [mode] [--persist]` flow.
1268
+ // The session module forwards to the runtime helper so the
1269
+ // workspace + global-config writes share one code path with
1270
+ // the CLI's top-level `--mode` resolution. The dynamic import
1271
+ // keeps the dispatcher free of a session.ts -> runtime/cli.ts
1272
+ // cycle.
1273
+ try {
1274
+ const { runPermissionsCommand } = await import('../../runtime/commands/permissions.js');
1275
+ const lines = [];
1276
+ await runPermissionsCommand(verdict, {
1277
+ workspaceRoot: process.cwd(),
1278
+ writeOutput: (line) => {
1279
+ const trimmed = line.replace(/\n+$/u, '');
1280
+ if (trimmed.length > 0)
1281
+ lines.push(trimmed);
1282
+ },
1283
+ });
1284
+ for (const line of lines)
1285
+ this.appendSystemLine(line);
1286
+ }
1287
+ catch (error) {
1288
+ const message = error instanceof Error ? error.message : String(error);
1289
+ this.appendSystemLine(`/permissions failed: ${message}`);
1290
+ }
1291
+ return verdict;
1292
+ }
1293
+ case 'compact': {
1294
+ // /compact summarises older turns and
1295
+ // appends a boundary marker. We forward to the same runner the
1296
+ // top-level `pugi compact` command uses so the surface stays
1297
+ // single-sourced. The session module owns the in-memory
1298
+ // transcript echo (system line + banner row) so the operator
1299
+ // sees the marker land without a fresh REPL bootstrap.
1300
+ //
1301
+ // BT 8 (the upstream tool parity): `--force` bypasses the
1302
+ // noop-empty guard so the operator can compact even short
1303
+ // sessions (useful before a manual checkpoint).
1304
+ await this.dispatchCompact('manual', { force: verdict.force });
1305
+ return verdict;
1306
+ }
1307
+ case 'model': {
1308
+ // BT 8 (the upstream tool parity): /model lists OR selects the
1309
+ // active model. Slash + top-level CLI share `runModelCommand`.
1310
+ // The session module forwards writeOutput → appendSystemLine so
1311
+ // the menu + the confirmation line land inline in the
1312
+ // transcript. Tier override is undefined at the slash surface;
1313
+ // the runner defaults to 'team' so unauthenticated operators
1314
+ // see every model. Server-side calls enforce the real tier cap.
1315
+ try {
1316
+ const { runModelCommand } = await import('../../runtime/commands/model.js');
1317
+ await runModelCommand({ slug: verdict.slug }, {
1318
+ workspaceRoot: process.cwd(),
1319
+ writeOutput: (line) => {
1320
+ const trimmed = line.replace(/\n+$/u, '');
1321
+ if (trimmed.length > 0)
1322
+ this.appendSystemLine(trimmed);
1323
+ else
1324
+ this.appendSystemLine('');
1325
+ },
1326
+ });
1327
+ }
1328
+ catch (error) {
1329
+ const message = error instanceof Error ? error.message : String(error);
1330
+ this.appendSystemLine(`/model failed: ${message}`);
1331
+ }
1332
+ return verdict;
1333
+ }
1334
+ case 'rewind': {
1335
+ // /rewind appends an append-only
1336
+ // tombstone marker that rolls the conversation back to a
1337
+ // checkpoint. The actual replay-mask is advisory — the on-disk
1338
+ // events stay durable so `pugi sessions undo-rewind` can
1339
+ // reverse the operation. We forward to the same runner the
1340
+ // top-level `pugi rewind` command uses to keep the surface
1341
+ // single-sourced. Dynamic import avoids pulling the checkpoint
1342
+ // graph into the dispatcher at module load.
1343
+ if (!this.store || !this.localSessionId) {
1344
+ this.appendSystemLine('Local session store is disabled — /rewind is unavailable.');
1345
+ return verdict;
1346
+ }
1347
+ try {
1348
+ const { runRewindCommand } = await import('../../runtime/commands/rewind.js');
1349
+ await runRewindCommand(verdict.args, {
1350
+ workspaceRoot: process.cwd(),
1351
+ sessionId: this.localSessionId,
1352
+ store: this.store,
1353
+ writeOutput: (_payload, text) => {
1354
+ if (text.length > 0)
1355
+ this.appendSystemLine(text);
1356
+ },
1357
+ });
1358
+ }
1359
+ catch (error) {
1360
+ const message = error instanceof Error ? error.message : String(error);
1361
+ this.appendSystemLine(`/rewind failed: ${message}`);
1362
+ }
1363
+ return verdict;
1364
+ }
1365
+ case 'share': {
1366
+ // /share forwards to the same runner the
1367
+ // top-level `pugi share` command uses. The session module
1368
+ // wires writeOutput to appendSystemLine so the upload result +
1369
+ // privacy gate banner land in the REPL transcript inline.
1370
+ // Confirmation prompt + readline still use stdio because the
1371
+ // Ink frame is held by the input box; operators wanting fully
1372
+ // scripted shares pass `--yes` so no prompt fires.
1373
+ try {
1374
+ const { runShareCommand } = await import('../../runtime/commands/share.js');
1375
+ const lines = [];
1376
+ await runShareCommand(verdict.args, {
1377
+ workspaceRoot: process.cwd(),
1378
+ cliVersion: this.options.cliVersion,
1379
+ sessionId: this.localSessionId ?? undefined,
1380
+ writeOutput: (_payload, text) => {
1381
+ const trimmed = text.replace(/\n+$/u, '');
1382
+ if (trimmed.length > 0)
1383
+ lines.push(trimmed);
1384
+ },
1385
+ });
1386
+ for (const line of lines)
1387
+ this.appendSystemLine(line);
1388
+ if (lines.length === 0) {
1389
+ this.appendSystemLine('/share: no output.');
1390
+ }
1391
+ }
1392
+ catch (error) {
1393
+ const message = error instanceof Error ? error.message : String(error);
1394
+ this.appendSystemLine(`/share failed: ${message}`);
1395
+ }
1396
+ return verdict;
1397
+ }
1398
+ case 'plan': {
1399
+ // handle `/plan [--back | --persist] [<prompt>]`.
1400
+ // The session module forwards the mode-switch portion to the
1401
+ // shared runtime helper so the workspace + global-config writes
1402
+ // share one code path with `pugi plan`. When the operator
1403
+ // typed a prompt alongside (`/plan write me X`), the prompt is
1404
+ // forwarded through the dispatch FSM exactly as if they had
1405
+ // typed it directly — the only difference is the gate now
1406
+ // refuses write/dispatch tools because the workspace mode flipped
1407
+ // to plan first. Same dynamic-import trick as /permissions to
1408
+ // avoid pulling the engine adapter graph into the dispatcher.
1409
+ try {
1410
+ const { runPlanCommand } = await import('../../runtime/commands/plan.js');
1411
+ const lines = [];
1412
+ await runPlanCommand({ back: verdict.back, persist: verdict.persist }, {
1413
+ workspaceRoot: process.cwd(),
1414
+ writeOutput: (line) => {
1415
+ const trimmed = line.replace(/\n+$/u, '');
1416
+ if (trimmed.length > 0)
1417
+ lines.push(trimmed);
1418
+ },
1419
+ });
1420
+ for (const line of lines)
1421
+ this.appendSystemLine(line);
1422
+ // Optional one-shot engine dispatch: when the operator typed
1423
+ // a prompt alongside the slash, route it through the existing
1424
+ // dispatch path. We rewrite the verdict into a synthetic
1425
+ // `dispatch` result so the engine sees the user's prompt with
1426
+ // the plan-mode gate already in place. `--auto-back` is NOT
1427
+ // honoured in the slash surface today — operators stay in
1428
+ // plan mode and revert manually with `/plan --back`. The CLI
1429
+ // top-level `pugi plan --auto-back` exists for scripted use.
1430
+ if (verdict.prompt.length > 0 && !verdict.back) {
1431
+ return { kind: 'dispatch', brief: verdict.prompt };
1432
+ }
1433
+ }
1434
+ catch (error) {
1435
+ const message = error instanceof Error ? error.message : String(error);
1436
+ this.appendSystemLine(`/plan failed: ${message}`);
1437
+ }
1438
+ return verdict;
1439
+ }
1440
+ case 'release-notes': {
1441
+ // changelog diff between the operator's
1442
+ // last-seen + installed CLI versions. Delegate к the shared
1443
+ // `runReleaseNotesCommand` runner so the slash + top-level
1444
+ // paths stay single-sourced. The renderer collects each line
1445
+ // into the system pane via `appendSystemLine` — no fresh Ink
1446
+ // mount, no boxed render. `--reset` is honoured via the
1447
+ // `verdict.reset` field parsed in slash-commands.ts.
1448
+ try {
1449
+ const { runReleaseNotesCommand, defaultReleaseNotesHome } = await import('../../runtime/commands/release-notes.js');
1450
+ const lines = [];
1451
+ runReleaseNotesCommand({
1452
+ home: defaultReleaseNotesHome(),
1453
+ json: false,
1454
+ reset: verdict.reset,
1455
+ writeOutput: (_payload, text) => {
1456
+ for (const line of text.split('\n')) {
1457
+ lines.push(line.replace(/\s+$/u, ''));
1458
+ }
1459
+ },
1460
+ });
1461
+ if (lines.length === 0) {
1462
+ this.appendSystemLine('/release-notes: no output.');
1463
+ }
1464
+ else {
1465
+ for (const line of lines)
1466
+ this.appendSystemLine(line);
1467
+ }
1468
+ }
1469
+ catch (error) {
1470
+ const message = error instanceof Error ? error.message : String(error);
1471
+ this.appendSystemLine(`/release-notes failed: ${message}`);
1472
+ }
1473
+ return verdict;
1474
+ }
1475
+ case 'stickers': {
1476
+ // brand-personality gimmick. Delegate to
1477
+ // the shared `runStickersCommand` so the slash + top-level
1478
+ // paths stay single-sourced. The renderer routes the text
1479
+ // through the system pane line-buffer (ascii-only — no fresh
1480
+ // Ink mount) so the gimmick lands as a single contiguous
1481
+ // block в the conversation transcript.
1482
+ try {
1483
+ const { runStickersCommand } = await import('../../runtime/commands/stickers.js');
1484
+ // L33 P1 fix : await the runner even though the
1485
+ // current implementation is synchronous. Two reasons:
1486
+ // (1) future-proofs the call site against the runner growing
1487
+ // an async path (e.g. remote stickerpack fetch) — without
1488
+ // this await, a returned promise would resolve AFTER we
1489
+ // flushed `lines` and the gimmick would render blank, and
1490
+ // (2) keeps the slash dispatcher uniform with the other
1491
+ // command runners (style, doctor, permissions, plan), all
1492
+ // of which are awaited.
1493
+ const lines = [];
1494
+ await runStickersCommand({
1495
+ json: false,
1496
+ asciiOnly: true,
1497
+ writeOutput: (_payload, text) => {
1498
+ for (const line of text.split('\n')) {
1499
+ const trimmed = line.replace(/\s+$/u, '');
1500
+ lines.push(trimmed);
1501
+ }
1502
+ },
1503
+ });
1504
+ if (lines.length === 0) {
1505
+ this.appendSystemLine('/stickers: no output.');
1506
+ }
1507
+ else {
1508
+ for (const line of lines)
1509
+ this.appendSystemLine(line);
1510
+ }
1511
+ }
1512
+ catch (error) {
1513
+ const message = error instanceof Error ? error.message : String(error);
1514
+ this.appendSystemLine(`/stickers failed: ${message}`);
1515
+ }
1516
+ return verdict;
1517
+ }
1518
+ case 'update': {
1519
+ // /update probes the npm registry for a
1520
+ // newer @pugi/cli version on the configured channel and prints
1521
+ // the install command. The slash form NEVER spawns `npm install
1522
+ // -g` — that would corrupt the binary we are currently running.
1523
+ // Operators see the install command + run it manually (or run
1524
+ // `pugi update --apply` from a fresh shell after the REPL
1525
+ // exits). The slash + top-level paths share the dispatcher so
1526
+ // channel resolution + last-check persistence stay single-
1527
+ // sourced.
1528
+ try {
1529
+ const { parseUpdateArgs, runUpdateCommand } = await import('../../runtime/commands/update.js');
1530
+ const parsed = parseUpdateArgs(verdict.args);
1531
+ if ('error' in parsed) {
1532
+ this.appendSystemLine(parsed.error);
1533
+ return verdict;
1534
+ }
1535
+ // Force `apply=false` on the slash path — see comment above.
1536
+ const slashFlags = { ...parsed, apply: false };
1537
+ const lines = [];
1538
+ await runUpdateCommand({
1539
+ cwd: process.cwd(),
1540
+ home: homedir(),
1541
+ env: process.env,
1542
+ flags: slashFlags,
1543
+ promptConfirm: async () => false,
1544
+ writeOutput: (_payload, text) => {
1545
+ for (const line of text.split('\n')) {
1546
+ const trimmed = line.replace(/\s+$/u, '');
1547
+ if (trimmed.length > 0)
1548
+ lines.push(trimmed);
1549
+ }
1550
+ },
1551
+ });
1552
+ if (lines.length === 0) {
1553
+ this.appendSystemLine('/update: no output.');
1554
+ }
1555
+ else {
1556
+ for (const line of lines)
1557
+ this.appendSystemLine(line);
1558
+ }
1559
+ }
1560
+ catch (error) {
1561
+ const message = error instanceof Error ? error.message : String(error);
1562
+ this.appendSystemLine(`/update failed: ${message}`);
1563
+ }
1564
+ return verdict;
1565
+ }
1566
+ case 'feedback': {
1567
+ // in-CLI feedback collector. The wizard
1568
+ // mounts a fresh Ink tree (renderFeedbackPrompt) outside the
1569
+ // live REPL input box so the operator can step through
1570
+ // category / rating / comment / context / confirm without
1571
+ // interleaving with persona output. The session module owns
1572
+ // the submit + queue wiring so the slash + top-level CLI
1573
+ // surfaces stay single-sourced through `runFeedbackCommand`.
1574
+ try {
1575
+ await this.runFeedbackSlash();
1576
+ }
1577
+ catch (error) {
1578
+ const message = error instanceof Error ? error.message : String(error);
1579
+ this.appendSystemLine(`/feedback failed: ${message}`);
1580
+ }
1581
+ return verdict;
1582
+ }
1583
+ case 'repo-map': {
1584
+ // AST-light workspace summary. Delegate
1585
+ // к the shared `runRepoMapCommand` so the slash + top-level
1586
+ // paths stay single-sourced. The rendered text lands on the
1587
+ // system pane via `appendSystemLine` (no fresh Ink mount) so
1588
+ // the listing flows into the conversation transcript like
1589
+ // any other command output.
1590
+ try {
1591
+ const { runRepoMapCommand } = await import('../../runtime/commands/repo-map.js');
1592
+ const lines = [];
1593
+ await runRepoMapCommand({
1594
+ cwd: process.cwd(),
1595
+ refresh: verdict.refresh,
1596
+ json: false,
1597
+ writeOutput: (_payload, text) => {
1598
+ for (const line of text.split('\n')) {
1599
+ const trimmed = line.replace(/\s+$/u, '');
1600
+ lines.push(trimmed);
1601
+ }
1602
+ },
1603
+ });
1604
+ if (lines.length === 0) {
1605
+ this.appendSystemLine('/repo-map: no output.');
1606
+ }
1607
+ else {
1608
+ for (const line of lines)
1609
+ this.appendSystemLine(line);
1610
+ }
1611
+ }
1612
+ catch (error) {
1613
+ const message = error instanceof Error ? error.message : String(error);
1614
+ this.appendSystemLine(`/repo-map failed: ${message}`);
1615
+ }
1616
+ return verdict;
1617
+ }
1618
+ case 'undo': {
1619
+ // final : graduated from stub. The runtime
1620
+ // command `runUndoCommand` already exists with full Aider walk-
1621
+ // back semantics — single-step revert of the most recent
1622
+ // successful `write` / `edit` / `multi_edit` tool result, with
1623
+ // an mtime+hash gate that refuses to overwrite uncommitted
1624
+ // operator work. We open a fresh PugiSession against the cwd
1625
+ // so the inverse-mutation audit lands on the same NDJSON
1626
+ // events stream the REPL writes to; dynamic-import keeps the
1627
+ // runner + git plumbing out of the REPL hot path.
1628
+ try {
1629
+ const [{ runUndoCommand }, { openSession }] = await Promise.all([
1630
+ import('../../runtime/commands/undo.js'),
1631
+ import('../session.js'),
1632
+ ]);
1633
+ const workspaceRoot = process.cwd();
1634
+ const session = openSession(workspaceRoot);
1635
+ this.appendSystemLine('Reverting last write...');
1636
+ await runUndoCommand([], {
1637
+ workspaceRoot,
1638
+ session,
1639
+ writeOutput: (_payload, text) => {
1640
+ const trimmed = text.replace(/\n+$/u, '');
1641
+ if (trimmed.length > 0)
1642
+ this.appendSystemLine(trimmed);
1643
+ },
1644
+ });
1645
+ }
1646
+ catch (error) {
1647
+ const message = error instanceof Error ? error.message : String(error);
1648
+ this.appendSystemLine(`/undo failed: ${message}`);
1649
+ }
1650
+ return verdict;
1651
+ }
1652
+ case 'redo': {
1653
+ // cleanup : counterpart к /undo. The runtime
1654
+ // command `runRedoCommand` consumes one entry from the LIFO
1655
+ // undo stack (most recent unconsumed `tool=undo` result), reads
1656
+ // the captured AFTER content from `.pugi/undo-blobs/`, and
1657
+ // re-applies the mutations under the same mtime+hash external-
1658
+ // modification gate the undo runner uses. Same dynamic-import
1659
+ // posture as /undo so the redo + blob-store + git plumbing
1660
+ // stays out of the REPL hot path.
1661
+ try {
1662
+ const [{ runRedoCommand }, { openSession }] = await Promise.all([
1663
+ import('../../runtime/commands/redo.js'),
1664
+ import('../session.js'),
1665
+ ]);
1666
+ const workspaceRoot = process.cwd();
1667
+ const session = openSession(workspaceRoot);
1668
+ this.appendSystemLine('Reapplying last undo...');
1669
+ await runRedoCommand([], {
1670
+ workspaceRoot,
1671
+ session,
1672
+ writeOutput: (_payload, text) => {
1673
+ const trimmed = text.replace(/\n+$/u, '');
1674
+ if (trimmed.length > 0)
1675
+ this.appendSystemLine(trimmed);
1676
+ },
1677
+ });
1678
+ }
1679
+ catch (error) {
1680
+ const message = error instanceof Error ? error.message : String(error);
1681
+ this.appendSystemLine(`/redo failed: ${message}`);
1682
+ }
1683
+ return verdict;
1684
+ }
1685
+ case 'plan-artifact': {
1686
+ // Pugi backlog : plan-as-FILE artifact surface.
1687
+ // Dynamic-import the core module so the REPL hot path stays free
1688
+ // of the artifact store + diff renderer until the operator
1689
+ // actually exercises a `/plan show|list|diff|prune` invocation.
1690
+ try {
1691
+ const { readPlan, listPlans, diffPlans, prunePlans, PlanNotFoundError, InvalidPlanIdError, } = await import('../plans/plan-artifact.js');
1692
+ const root = process.cwd();
1693
+ const sub = verdict.sub;
1694
+ if (sub.op === 'show') {
1695
+ try {
1696
+ const record = readPlan(sub.planId, { root });
1697
+ this.appendSystemLine(`plan ${record.frontmatter.planId} (task=${record.frontmatter.taskId}, created=${record.frontmatter.createdAt})`);
1698
+ if (record.frontmatter.supersededBy) {
1699
+ this.appendSystemLine(`superseded by ${record.frontmatter.supersededBy}`);
1700
+ }
1701
+ for (const line of record.body.split('\n')) {
1702
+ this.appendSystemLine(line);
1703
+ }
1704
+ }
1705
+ catch (error) {
1706
+ if (error instanceof PlanNotFoundError) {
1707
+ this.appendSystemLine(`/plan show: plan not found: ${sub.planId}`);
1708
+ }
1709
+ else if (error instanceof InvalidPlanIdError) {
1710
+ this.appendSystemLine(`/plan show: invalid plan-id: ${sub.planId}`);
1711
+ }
1712
+ else {
1713
+ throw error;
1714
+ }
1715
+ }
1716
+ }
1717
+ else if (sub.op === 'list') {
1718
+ const filter = sub.taskId ? { taskId: sub.taskId, root } : { root };
1719
+ const records = listPlans(filter);
1720
+ if (records.length === 0) {
1721
+ this.appendSystemLine('/plan list: no plans yet.');
1722
+ }
1723
+ else {
1724
+ this.appendSystemLine(`plan-id taskId createdAt supersededBy`);
1725
+ for (const rec of records) {
1726
+ const fm = rec.frontmatter;
1727
+ const supers = fm.supersededBy ?? '-';
1728
+ this.appendSystemLine(`${fm.planId} ${fm.taskId.padEnd(15)} ${fm.createdAt} ${supers}`);
1729
+ }
1730
+ }
1731
+ }
1732
+ else if (sub.op === 'diff') {
1733
+ try {
1734
+ const diff = diffPlans(sub.planId, sub.otherId, { root });
1735
+ for (const line of diff.split('\n')) {
1736
+ this.appendSystemLine(line);
1737
+ }
1738
+ }
1739
+ catch (error) {
1740
+ if (error instanceof PlanNotFoundError) {
1741
+ this.appendSystemLine(`/plan diff: plan not found`);
1742
+ }
1743
+ else if (error instanceof InvalidPlanIdError) {
1744
+ this.appendSystemLine(`/plan diff: invalid plan-id`);
1745
+ }
1746
+ else {
1747
+ throw error;
1748
+ }
1749
+ }
1750
+ }
1751
+ else {
1752
+ // prune
1753
+ const result = prunePlans(sub.maxAgeDays !== undefined
1754
+ ? { root, maxAgeDays: sub.maxAgeDays }
1755
+ : { root });
1756
+ this.appendSystemLine(`/plan prune: cleaned ${result.cleaned} plan${result.cleaned === 1 ? '' : 's'}.`);
1757
+ for (const id of result.removedIds) {
1758
+ this.appendSystemLine(` - ${id}`);
1759
+ }
1760
+ }
1761
+ }
1762
+ catch (error) {
1763
+ const message = error instanceof Error ? error.message : String(error);
1764
+ this.appendSystemLine(`/plan ${verdict.sub.op} failed: ${message}`);
1765
+ }
1766
+ return verdict;
1767
+ }
666
1768
  case 'stub': {
667
1769
  this.appendSystemLine(verdict.message);
668
1770
  return verdict;
669
1771
  }
670
1772
  }
671
1773
  }
1774
+ /**
1775
+ * drive the `/feedback` wizard from inside
1776
+ * the REPL. Mounts the Ink prompt, collects the draft, hands it to
1777
+ * `runFeedbackCommand` (which routes to submit-now or
1778
+ * queue-locally), then writes the operator-facing toast to the
1779
+ * conversation system pane.
1780
+ *
1781
+ * The session module owns the wiring (cwd, cliVersion, apiUrl,
1782
+ * apiKey, transcript provider) so the slash + top-level CLI paths
1783
+ * stay single-sourced through `runFeedbackCommand`.
1784
+ */
1785
+ async runFeedbackSlash() {
1786
+ const { renderFeedbackPrompt } = await import('../../tui/feedback-prompt.js');
1787
+ const { runFeedbackCommand, renderFeedbackToast } = await import('../../runtime/commands/feedback.js');
1788
+ const { submitFeedback, redactSessionContext } = await import('../feedback/submitter.js');
1789
+ const verdict = await renderFeedbackPrompt();
1790
+ if (verdict.cancelled || !verdict.draft) {
1791
+ this.appendSystemLine('Feedback cancelled. Nothing was sent.');
1792
+ return;
1793
+ }
1794
+ // Build a session-context provider that reads the LAST 5 turns
1795
+ // from the live transcript + applies the redactor. Only invoked
1796
+ // when the operator opted in on step 4.
1797
+ const sessionContextProvider = () => {
1798
+ const last5 = this.state.transcript
1799
+ .filter((row) => row.source !== 'system')
1800
+ .slice(-5)
1801
+ .map((row) => ({
1802
+ role: row.source === 'operator' ? 'user' : 'assistant',
1803
+ text: row.text,
1804
+ }));
1805
+ // The workspace context exposed to the session does not carry
1806
+ // a git branch field today, so we omit `gitBranch` here. When
1807
+ // `ReplWorkspaceContext` gains the field we can forward it via
1808
+ // an extra options entry without changing the redactor contract.
1809
+ return redactSessionContext(last5);
1810
+ };
1811
+ const result = await runFeedbackCommand({
1812
+ cwd: process.cwd(),
1813
+ cliVersion: this.options.cliVersion,
1814
+ submit: async (env) => submitFeedback(env, {
1815
+ apiUrl: this.options.apiUrl,
1816
+ apiKey: this.options.apiKey,
1817
+ }),
1818
+ draft: verdict.draft,
1819
+ sessionContext: sessionContextProvider,
1820
+ });
1821
+ this.appendSystemLine(renderFeedbackToast(result));
1822
+ }
1823
+ /**
1824
+ * drive the `/compact` flow from inside the
1825
+ * REPL. Reuses the standalone runner so the wire shape + reason
1826
+ * codes stay single-sourced. The result is echoed into the
1827
+ * transcript as a system line; on success the operator sees the
1828
+ * banner sentinel on next render.
1829
+ *
1830
+ * `trigger='manual'` for explicit `/compact` invocations;
1831
+ * `trigger='auto'` for the threshold gate. The runner records the
1832
+ * trigger in the marker payload so the banner can distinguish them.
1833
+ */
1834
+ async dispatchCompact(trigger, options = {}) {
1835
+ if (!this.store || !this.localSessionId) {
1836
+ this.appendSystemLine('Local session store is disabled — /compact is unavailable.');
1837
+ return;
1838
+ }
1839
+ try {
1840
+ const { runCompactCommand } = await import('../../runtime/commands/compact.js');
1841
+ const result = await runCompactCommand([], {
1842
+ workspaceRoot: process.cwd(),
1843
+ sessionId: this.localSessionId,
1844
+ store: this.store,
1845
+ trigger,
1846
+ force: options.force === true,
1847
+ writeOutput: (_payload, text) => {
1848
+ if (text.length > 0)
1849
+ this.appendSystemLine(text);
1850
+ },
1851
+ });
1852
+ if (result.status === 'compacted') {
1853
+ // L29 : emit a structured `compact-boundary` row so
1854
+ // the conversation pane routes the marker through the dedicated
1855
+ // `<CompactBanner />` Ink component (gray, terminal-width
1856
+ // separator) instead of leaking the raw text into a `system`
1857
+ // row. The plain-text body is kept as a deterministic fallback
1858
+ // for non-Ink consumers (snapshot tests, JSON-mode exports).
1859
+ const turnsBefore = result.turnsBefore ?? 0;
1860
+ this.appendRow({
1861
+ source: 'compact-boundary',
1862
+ text: `─── context compacted (${turnsBefore} turns → 1 summary, ${trigger}) ───`,
1863
+ compaction: {
1864
+ turnsBefore,
1865
+ trigger,
1866
+ summaryTokenCount: result.tokensSummarised,
1867
+ // Fresh in-REPL compaction lands at the head of the
1868
+ // transcript — no turns have followed it yet.
1869
+ turnsAgo: 0,
1870
+ },
1871
+ });
1872
+ }
1873
+ }
1874
+ catch (error) {
1875
+ const message = error instanceof Error ? error.message : String(error);
1876
+ this.appendSystemLine(`/compact failed: ${message}`);
1877
+ }
1878
+ }
672
1879
  /**
673
1880
  * In-REPL `/privacy` - alpha 6.13. Prints the full 3-mode contract
674
1881
  * doc + the current mode banner inline. The current mode is fetched
@@ -679,7 +1886,7 @@ export class ReplSession {
679
1886
  */
680
1887
  async dispatchPrivacy() {
681
1888
  const { renderPrivacyContractDoc } = await import('./privacy-banner.js');
682
- // Triple-review P1 fix (2026-05-25): use the bootstrap-cached mode
1889
+ // Triple-review P1 fix : use the bootstrap-cached mode
683
1890
  // so the operator sees the LIVE current mode in the banner header
684
1891
  // instead of "(unknown)". The fetch happens once on session start;
685
1892
  // if it failed (offline / unauth) the cache stays null and the
@@ -689,7 +1896,7 @@ export class ReplSession {
689
1896
  this.appendSystemLine(doc);
690
1897
  }
691
1898
  /**
692
- * In-REPL `/resume` - α6.4. Lists the 10 most recent sessions from
1899
+ * In-REPL `/resume` - . Lists the 10 most recent sessions from
693
1900
  * the local SessionStore and prints them as a numbered system menu.
694
1901
  * The Ink-side picker UI is deferred to the next sprint; today the
695
1902
  * operator gets a deterministic list + the exact command to relaunch
@@ -720,7 +1927,7 @@ export class ReplSession {
720
1927
  const title = (row.title ?? '(untitled)').slice(0, 64);
721
1928
  const idShort = row.id.slice(0, 13);
722
1929
  const branch = row.branch ?? 'no-branch';
723
- this.appendSystemLine(` ${(i + 1).toString().padStart(2)}. ${idShort} ${branch.padEnd(16)} ${title}`);
1930
+ this.appendSystemLine(` ${(i + 1).toString().padStart(2)}. ${idShort} ${branch.padEnd(16)} ${title}`);
724
1931
  }
725
1932
  this.appendSystemLine('Pick one with: pugi resume <id> (paste the 13-char id from above).');
726
1933
  }
@@ -732,7 +1939,7 @@ export class ReplSession {
732
1939
  clearTranscript() {
733
1940
  this.patch({ transcript: [] });
734
1941
  }
735
- /* ------------- α6.3 office-hours surface -------------- */
1942
+ /* ------------- office-hours surface -------------- */
736
1943
  /**
737
1944
  * Surface an `<pugi-ask>` modal manually. Returned promise resolves
738
1945
  * with the operator's verdict - used by the `pugi ask "<q>"` shell
@@ -781,7 +1988,7 @@ export class ReplSession {
781
1988
  * came from a persona stream, cancel ALSO dispatches a literal
782
1989
  * `[ASK-RESPONSE:cancelled]` to admin-api so the persona observes the
783
1990
  * cancellation rather than hanging indefinitely on the missing
784
- * follow-up. The matching documentation in the Mira system prompt
1991
+ * follow-up. The matching documentation in the Pugi system prompt
785
1992
  * teaches the persona to acknowledge cancellation and offer a
786
1993
  * different path. Local-origin modals (synthesised via `/ask`) skip
787
1994
  * the dispatch entirely - the persona never saw the question.
@@ -812,7 +2019,7 @@ export class ReplSession {
812
2019
  // Surface the operator's choice as a transcript row so the
813
2020
  // conversation reads linearly. The label of the chosen option
814
2021
  // (or the literal custom input) is more readable than the bare
815
- // value - Codex CLI's "you chose: Vercel" pattern.
2022
+ // value - peer CLI's "you chose: Vercel" pattern.
816
2023
  const humanLabel = humanLabelForVerdict(tag, sanitisedVerdict);
817
2024
  this.appendOperatorLine(humanLabel);
818
2025
  // Local-origin modals (operator typed `/ask`) never need an
@@ -904,22 +2111,87 @@ export class ReplSession {
904
2111
  try {
905
2112
  const registry = getJobRegistry();
906
2113
  const entries = await registry.list();
907
- if (entries.length === 0) {
2114
+ // cleanup : also scan `.pugi/agent-progress/*.json`
2115
+ // so long-running external agents (the JSON pattern from
2116
+ // `feedback_agent_progress_tracking_pattern.md`) show up next к
2117
+ // background-bash entries. The two surfaces are orthogonal — bash
2118
+ // jobs come from the in-process registry, agent-progress comes from
2119
+ // sidecar JSON written by any agent (Pugi-spawned or external) — so
2120
+ // we render both, sorted with running first.
2121
+ const agentProgressRows = await this.collectAgentProgressRows();
2122
+ if (entries.length === 0 && agentProgressRows.length === 0) {
908
2123
  this.appendSystemLine('No background jobs tracked.');
909
2124
  return;
910
2125
  }
911
- this.appendSystemLine(`Background jobs (${entries.length}):`);
912
- for (const entry of entries) {
913
- const id = entry.id.replace(/^pj-/, '').slice(0, 8);
914
- const status = entry.status;
915
- const cmd = entry.command.length > 48 ? `${entry.command.slice(0, 47)}…` : entry.command;
916
- this.appendSystemLine(` ${id} ${status.padEnd(10)} ${cmd}`);
2126
+ if (entries.length > 0) {
2127
+ this.appendSystemLine(`Background jobs (${entries.length}):`);
2128
+ for (const entry of entries) {
2129
+ const id = entry.id.replace(/^pj-/, '').slice(0, 8);
2130
+ const status = entry.status;
2131
+ const cmd = entry.command.length > 48 ? `${entry.command.slice(0, 47)}…` : entry.command;
2132
+ this.appendSystemLine(` ${id} ${status.padEnd(10)} ${cmd}`);
2133
+ }
2134
+ }
2135
+ if (agentProgressRows.length > 0) {
2136
+ this.appendSystemLine(`Agent progress (${agentProgressRows.length}):`);
2137
+ for (const row of agentProgressRows) {
2138
+ this.appendSystemLine(` ${row}`);
2139
+ }
2140
+ this.appendSystemLine('Tip: run `pugi jobs --watch` for the live Ink TUI.');
917
2141
  }
918
2142
  }
919
2143
  catch (error) {
920
2144
  this.appendSystemLine(`/jobs failed: ${this.errorMessage(error)}`);
921
2145
  }
922
2146
  }
2147
+ /**
2148
+ * cleanup : scan `.pugi/agent-progress/*.json`
2149
+ * for in-flight long-running agent tasks and emit a one-line per
2150
+ * agent for the `/jobs` snapshot. Sorting matches the live TUI's
2151
+ * `sortProgressEntries` (running first, then by lastUpdate desc).
2152
+ *
2153
+ * Best-effort: a missing dir, malformed JSON, or bad permissions
2154
+ * yields an empty list and a swallowed error — the in-process
2155
+ * registry view is the older well-tested surface and must never be
2156
+ * gated behind a sidecar dir's health.
2157
+ */
2158
+ async collectAgentProgressRows() {
2159
+ try {
2160
+ const [{ resolveProgressDir }, { readProgressFile, sortProgressEntries }, fs, path] = await Promise.all([
2161
+ import('../agent-progress/writer.js'),
2162
+ import('../../commands/jobs-watch.js'),
2163
+ import('node:fs'),
2164
+ import('node:path'),
2165
+ ]);
2166
+ const dir = resolveProgressDir();
2167
+ if (!fs.existsSync(dir))
2168
+ return [];
2169
+ const files = fs
2170
+ .readdirSync(dir)
2171
+ .filter((f) => f.endsWith('.json'))
2172
+ .map((f) => path.join(dir, f));
2173
+ const progress = files
2174
+ .map((p) => readProgressFile(p))
2175
+ .filter((p) => p !== undefined);
2176
+ const sorted = sortProgressEntries(progress);
2177
+ return sorted.map((p) => {
2178
+ const id = p.agentId.length > 24 ? `${p.agentId.slice(0, 23)}…` : p.agentId;
2179
+ const pct = `${String(Math.round(p.percentComplete)).padStart(3, ' ')}%`;
2180
+ const elapsedSec = Math.max(0, Math.floor(p.elapsedMs / 1000));
2181
+ const elapsed = elapsedSec >= 60
2182
+ ? `${Math.floor(elapsedSec / 60)}m${String(elapsedSec % 60).padStart(2, '0')}s`
2183
+ : `${elapsedSec}s`;
2184
+ const status = p.status.padEnd(9, ' ');
2185
+ const step = p.stepDescription.length > 36
2186
+ ? `${p.stepDescription.slice(0, 35)}…`
2187
+ : p.stepDescription;
2188
+ return `${id.padEnd(24, ' ')} ${status} ${pct} ${elapsed.padStart(6, ' ')} ${step}`;
2189
+ });
2190
+ }
2191
+ catch {
2192
+ return [];
2193
+ }
2194
+ }
923
2195
  dispatchDiff() {
924
2196
  try {
925
2197
  const artifactsRoot = resolvePath(process.cwd(), '.pugi', 'artifacts');
@@ -935,7 +2207,7 @@ export class ReplSession {
935
2207
  const candidate = resolvePath(artifactsRoot, name, 'diff.patch');
936
2208
  if (existsSync(candidate)) {
937
2209
  const size = statSync(candidate).size;
938
- diffs.push(` ${name}/diff.patch (${size} bytes)`);
2210
+ diffs.push(` ${name}/diff.patch (${size} bytes)`);
939
2211
  }
940
2212
  }
941
2213
  if (diffs.length === 0) {
@@ -950,38 +2222,233 @@ export class ReplSession {
950
2222
  this.appendSystemLine(`/diff failed: ${this.errorMessage(error)}`);
951
2223
  }
952
2224
  }
953
- dispatchCost() {
954
- const { tokensDownstreamTotal, agents } = this.state;
2225
+ async dispatchCost() {
2226
+ // cost-meter sprint full breakdown matching the TUI status row
2227
+ // footer. The session totals line mirrors the footer format
2228
+ // (`↑ <in> ↓ <out> · $X.XX · <elapsed>`) so the operator scans the
2229
+ // same numbers in two places. Per-turn list shows the last 5 turns
2230
+ // oldest → newest; an empty list renders one system line so the
2231
+ // operator knows the surface is wired (`No completed turns yet.`).
2232
+ //
2233
+ // L19 — after the in-memory recap, also render the
2234
+ // persisted per-model table from `.pugi/cost.json`. That surface
2235
+ // survives a REPL restart and answers the "what did I spend on
2236
+ // claude-opus vs qwen this week?" question the in-memory recap can
2237
+ // not. Errors loading the file collapse to a single warning line so
2238
+ // the in-memory recap (the older, well-tested surface) is never
2239
+ // gated behind a fresh dependency.
2240
+ const { sessionTokensIn, sessionTokensOut, sessionCostUsd, sessionStartedAtEpochMs, recentTurns, agents, } = this.state;
955
2241
  const active = agents.filter((a) => a.status === 'queued' || a.status === 'thinking').length;
956
- const lineTokens = `Tokens this session: ${tokensDownstreamTotal.toLocaleString()} (in+out).`;
957
- const lineAgents = `Active dispatches: ${active} of cap.`;
958
- this.appendSystemLine(lineTokens);
959
- this.appendSystemLine(lineAgents);
960
- this.appendSystemLine('Full per-persona budget breakdown lands in α6.5.');
961
- }
962
- dispatchStatus() {
963
- const sessionId = this.state.sessionId ?? '(unbound)';
964
- const reach = this.state.connection;
965
- this.appendSystemLine(`Backend: ${this.options.apiUrl} (${reach}).`);
966
- this.appendSystemLine(`Session: ${sessionId}.`);
967
- this.appendSystemLine(`Workspace: ${this.state.workspaceLabel}.`);
968
- this.appendSystemLine(`CLI: pugi ${this.state.cliVersion}.`);
969
- }
970
- /**
971
- * α6.5 `/context` slash handler. Surfaces the three-tier context
2242
+ const elapsedMs = Math.max(0, this.now() - sessionStartedAtEpochMs);
2243
+ const elapsedLabel = formatElapsedShort(elapsedMs);
2244
+ this.appendSystemLine(`Session: ↑ ${formatTokens(sessionTokensIn)} ↓ ${formatTokens(sessionTokensOut)} · ${formatCostUsd(sessionCostUsd)} · ${elapsedLabel}`);
2245
+ this.appendSystemLine(`Active dispatches: ${active} of cap.`);
2246
+ if (recentTurns.length === 0) {
2247
+ this.appendSystemLine('No completed turns yet — brief the workforce to charge the meter.');
2248
+ }
2249
+ else {
2250
+ this.appendSystemLine(`Recent turns (last ${recentTurns.length}):`);
2251
+ for (let i = 0; i < recentTurns.length; i += 1) {
2252
+ const turn = recentTurns[i];
2253
+ const idx = (i + 1).toString().padStart(2, ' ');
2254
+ this.appendSystemLine(` ${idx}. ${formatTokens(turn.tokensIn)} ↓ ${formatTokens(turn.tokensOut)} · ${formatCostUsd(turn.costUsd)}`);
2255
+ }
2256
+ }
2257
+ // L19: append the persisted per-model table from .pugi/cost.json.
2258
+ try {
2259
+ const [{ createCostTracker }, { renderCostForSlash }] = await Promise.all([
2260
+ import('../cost/tracker.js'),
2261
+ import('../../runtime/commands/cost.js'),
2262
+ ]);
2263
+ const workspaceRoot = this.options.workspace?.workspaceCwd ?? process.cwd();
2264
+ const sessionId = this.state.sessionId ?? 'no-session';
2265
+ const tracker = createCostTracker({
2266
+ workspaceRoot,
2267
+ sessionIdProvider: () => sessionId,
2268
+ now: () => this.now(),
2269
+ });
2270
+ const current = tracker.current();
2271
+ if (current && Object.keys(current.models).length > 0) {
2272
+ this.appendSystemLine('');
2273
+ const { lines } = renderCostForSlash({
2274
+ tracker,
2275
+ allSessions: false,
2276
+ windowDays: 30,
2277
+ now: () => this.now(),
2278
+ });
2279
+ for (const line of lines)
2280
+ this.appendSystemLine(line);
2281
+ }
2282
+ }
2283
+ catch {
2284
+ // best-effort — the persisted view is additive; failure never
2285
+ // breaks the in-memory recap above
2286
+ }
2287
+ }
2288
+ /**
2289
+ * cost-meter sprint — `/quota` slash handler. Fetches the live
2290
+ * `/api/pugi/usage` snapshot and renders three lines: plan tier,
2291
+ * monthly window, and per-counter `used/cap (pct%)`. Failure modes
2292
+ * (offline, unauth, older admin-api) collapse to a single one-line
2293
+ * `Could not fetch quota…` system message so the surface never throws
2294
+ * from a keystroke handler.
2295
+ *
2296
+ * The fetch is best-effort with a 4s timeout — mirrors the `whoami`
2297
+ * pattern in `runtime/cli.ts` so the operator gets the same UX on the
2298
+ * REPL slash and the CLI command.
2299
+ */
2300
+ async dispatchQuota() {
2301
+ const controller = new AbortController();
2302
+ const timer = setTimeout(() => controller.abort(), 4000);
2303
+ try {
2304
+ const url = `${this.options.apiUrl.replace(/\/+$/, '')}/api/pugi/usage`;
2305
+ const res = await fetch(url, {
2306
+ method: 'GET',
2307
+ headers: {
2308
+ authorization: `Bearer ${this.options.apiKey}`,
2309
+ accept: 'application/json',
2310
+ },
2311
+ signal: controller.signal,
2312
+ });
2313
+ if (!res.ok) {
2314
+ this.appendSystemLine(`Could not fetch quota: HTTP ${res.status}.`);
2315
+ return;
2316
+ }
2317
+ const body = (await res.json());
2318
+ const tier = typeof body.tier === 'string' ? body.tier : '(unknown)';
2319
+ const tierLabel = QUOTA_TIER_LABELS[tier] ?? tier;
2320
+ const month = typeof body.billingMonth === 'string' ? body.billingMonth : '(unknown month)';
2321
+ const resetAt = typeof body.resetAt === 'string' ? body.resetAt : null;
2322
+ const resetLine = resetAt ? ` · resets ${formatResetWindow(resetAt, this.now())}` : '';
2323
+ this.appendSystemLine(`Plan: ${tierLabel} · ${month}${resetLine}`);
2324
+ const used = body.used ?? {};
2325
+ const caps = body.quotas ?? {};
2326
+ const counters = [
2327
+ ['sync', used.sync, caps.sync],
2328
+ ['review', used.review, caps.review],
2329
+ ['engine', used.engine, caps.engine],
2330
+ ];
2331
+ // cleanup : color-code each counter row by
2332
+ // utilisation. The thresholds match the upstream tool's tier-meter
2333
+ // convention so operators trained on that surface read the same
2334
+ // signal here. ANSI codes wrap the WHOLE row (not just the
2335
+ // percent) so the line wraps as one visual unit; the cost-quota
2336
+ // spec regex still matches because anchors are inside the
2337
+ // wrapped substring.
2338
+ for (const [name, value, cap] of counters) {
2339
+ const v = typeof value === 'number' ? value : 0;
2340
+ if (cap === null || cap === undefined) {
2341
+ // Unlimited counters never trip the gauge — leave them
2342
+ // uncolored so the eye does not register an alarm signal
2343
+ // where there is no cap к exhaust.
2344
+ this.appendSystemLine(` ${name.padEnd(7, ' ')} ${v.toLocaleString()} / unlimited`);
2345
+ }
2346
+ else {
2347
+ const pct = cap > 0 ? Math.round((v / cap) * 100) : 0;
2348
+ const row = ` ${name.padEnd(7, ' ')} ${v.toLocaleString()} / ${cap.toLocaleString()} (${pct}%)`;
2349
+ this.appendSystemLine(colorizeQuotaRow(row, pct));
2350
+ }
2351
+ }
2352
+ }
2353
+ catch (error) {
2354
+ const msg = error instanceof Error ? error.message : String(error);
2355
+ this.appendSystemLine(`Could not fetch quota: ${msg}.`);
2356
+ }
2357
+ finally {
2358
+ clearTimeout(timer);
2359
+ }
2360
+ }
2361
+ /**
2362
+ * In-REPL `/status` — . Surfaces the full
2363
+ * session snapshot (id + age, cwd, permission mode, CLI version,
2364
+ * tokens, dispatches, last cmd, compact boundaries, auth identity,
2365
+ * connection) by delegating к the same `runStatusCommand` the
2366
+ * top-level `pugi status` shell uses. Live REPL state (session
2367
+ * id, token totals, last operator command) flows in through the
2368
+ * context so the slash variant shows MORE than the shell path.
2369
+ *
2370
+ * The renderer routes к the system pane via `appendSystemLine`
2371
+ * so the snapshot lands as a single contiguous block в the
2372
+ * conversation transcript. Migrating к the Ink `<StatusTable>`
2373
+ * mounted directly в the REPL frame is a follow-up sprint —
2374
+ * keeping the line-buffered path here avoids cycling the
2375
+ * conversation pane's render model mid-.
2376
+ */
2377
+ async dispatchStatus() {
2378
+ try {
2379
+ const { runStatusCommand, defaultStatusHome } = await import('../../runtime/commands/status.js');
2380
+ // Find the most-recent operator transcript row + its timestamp
2381
+ // so the snapshot's `Last cmd` field has real content в REPL
2382
+ // mode. Walking от newest end is O(transcript) worst case but
2383
+ // bounded by MAX_TRANSCRIPT_ROWS so this stays cheap.
2384
+ let lastCommand = null;
2385
+ let lastCommandAtEpochMs = null;
2386
+ for (let i = this.state.transcript.length - 1; i >= 0; i -= 1) {
2387
+ const row = this.state.transcript[i];
2388
+ if (row.source === 'operator') {
2389
+ lastCommand = row.text;
2390
+ lastCommandAtEpochMs = row.timestampEpochMs;
2391
+ break;
2392
+ }
2393
+ }
2394
+ const liveTokens = this.state.sessionTokensIn + this.state.sessionTokensOut;
2395
+ const lines = [];
2396
+ await runStatusCommand({
2397
+ cwd: process.cwd(),
2398
+ home: defaultStatusHome(),
2399
+ env: process.env,
2400
+ json: false,
2401
+ liveSessionId: this.state.sessionId ?? null,
2402
+ sessionStartedAtEpochMs: this.state.sessionStartedAtEpochMs,
2403
+ liveTokensUsed: liveTokens >= 0 ? liveTokens : 0,
2404
+ lastCommand,
2405
+ lastCommandAtEpochMs,
2406
+ // Repl-mode context: the session knows both the live
2407
+ // transport URL and the operator's workspace label, so we
2408
+ // forward them as authoritative inputs к the snapshot.
2409
+ // The status snapshot used к infer these from the
2410
+ // credentials file, which was wrong in two cases:
2411
+ // (a) the operator was inside a REPL talking к Anvil dev
2412
+ // (port 4100) but credentials still pointed к
2413
+ // api.pugi.io — the `Backend` row mis-reported;
2414
+ // (b) `workspaceLabel` was никогда rendered at all.
2415
+ liveApiUrl: this.options.apiUrl,
2416
+ workspaceLabel: this.options.workspaceLabel,
2417
+ writeOutput: (_payload, text) => {
2418
+ for (const line of text.split('\n')) {
2419
+ const trimmed = line.replace(/\s+$/u, '');
2420
+ if (trimmed.length > 0)
2421
+ lines.push(trimmed);
2422
+ }
2423
+ },
2424
+ });
2425
+ if (lines.length === 0) {
2426
+ this.appendSystemLine('/status: no output.');
2427
+ return;
2428
+ }
2429
+ for (const line of lines)
2430
+ this.appendSystemLine(line);
2431
+ }
2432
+ catch (error) {
2433
+ const message = error instanceof Error ? error.message : String(error);
2434
+ this.appendSystemLine(`/status failed: ${message}`);
2435
+ }
2436
+ }
2437
+ /**
2438
+ * `/context` slash handler. Surfaces the three-tier context
972
2439
  * summary as a stack of system lines. Sections (in order):
973
2440
  *
974
- * 1. Tier 0 (repo skeleton) - size in bytes, branch, package
975
- * manager, languages. Skipped when no skeleton was injected
976
- * (REPL launched outside a workspace or with --no-context).
2441
+ * 1. Tier 0 (repo skeleton) - size in bytes, branch, package
2442
+ * manager, languages. Skipped when no skeleton was injected
2443
+ * (REPL launched outside a workspace or with --no-context).
977
2444
  *
978
- * 2. Tier 1 (working set) - `count / capacity` plus the total
979
- * size in bytes plus the oldest entry's age in seconds.
980
- * Always emits even when empty so the operator can confirm
981
- * the tier is wired.
2445
+ * 2. Tier 1 (working set) - `count / capacity` plus the total
2446
+ * size in bytes plus the oldest entry's age in seconds.
2447
+ * Always emits even when empty so the operator can confirm
2448
+ * the tier is wired.
982
2449
  *
983
- * 3. Tier 2 (RAG) - one-line heads-up that the Anvil-side
984
- * workspace lands in α6.5b.
2450
+ * 3. Tier 2 (RAG) - one-line heads-up that the Anvil-side
2451
+ * workspace lands in .
985
2452
  *
986
2453
  * The renderer never mutates state.
987
2454
  */
@@ -1010,10 +2477,10 @@ export class ReplSession {
1010
2477
  else {
1011
2478
  this.appendSystemLine('Tier 1 working set: not wired.');
1012
2479
  }
1013
- this.appendSystemLine('Tier 2 RAG: deferred to α6.5b (Anvil-side per-tenant workspace).');
2480
+ this.appendSystemLine('Tier 2 RAG: deferred to (Anvil-side per-tenant workspace).');
1014
2481
  }
1015
2482
  /**
1016
- * α6.5 chokidar batch handler. Forwards each event to the working
2483
+ * chokidar batch handler. Forwards each event to the working
1017
2484
  * set tracker (so `unlink` evicts and `add`/`change` bump the
1018
2485
  * recency) and emits at most one throttled system line per
1019
2486
  * `FILEWATCH_SYSTEM_LINE_GAP_MS` window.
@@ -1021,7 +2488,7 @@ export class ReplSession {
1021
2488
  * The transcript surface intentionally shows ONE filename + the
1022
2489
  * count of additional changes (`file changed: src/foo.ts (+3 more)`).
1023
2490
  * The full event list is preserved in the buffer for future
1024
- * `/context --files` deep-dive (not in α6.5 Phase 1).
2491
+ * `/context --files` deep-dive (not in Phase 1).
1025
2492
  */
1026
2493
  recordFilewatchBatch(batch) {
1027
2494
  // Hard-guard against post-close invocation. close() detaches the
@@ -1030,7 +2497,7 @@ export class ReplSession {
1030
2497
  // listener captured at the start of emit(). If the session closes
1031
2498
  // mid-emit, the handler can still fire on a dead session. Returning
1032
2499
  // early keeps the working set + transcript untouched.
1033
- // triple-review P1 (PR #380).
2500
+ // triple-review P1 (PR).
1034
2501
  if (this.closed)
1035
2502
  return;
1036
2503
  if (this.workingSet) {
@@ -1050,7 +2517,7 @@ export class ReplSession {
1050
2517
  // do not emit a system line. Cap the buffer at
1051
2518
  // PENDING_FILEWATCH_BATCH_CAP and drop the oldest on overflow so
1052
2519
  // a noisy filewatch source cannot drive unbounded memory growth
1053
- // across a long REPL session. triple-review P1 (PR #380).
2520
+ // across a long REPL session. triple-review P1 (PR).
1054
2521
  if (this.pendingFilewatchBatches.length >= PENDING_FILEWATCH_BATCH_CAP) {
1055
2522
  this.pendingFilewatchBatches.shift();
1056
2523
  if (!this.pendingFilewatchOverflowWarned) {
@@ -1078,14 +2545,14 @@ export class ReplSession {
1078
2545
  this.pendingFilewatchBatches = [];
1079
2546
  }
1080
2547
  /**
1081
- * α6.5 chokidar cap-exceeded handler. The watcher closes itself
2548
+ * chokidar cap-exceeded handler. The watcher closes itself
1082
2549
  * when it crosses the watched-paths cap; the session surfaces a
1083
2550
  * single system line so the operator knows live updates are off.
1084
2551
  * The conversation stays usable - we just lose the file-changed
1085
2552
  * badge for the rest of the session.
1086
2553
  */
1087
2554
  recordFilewatchCapExceeded(info) {
1088
- // Same post-close guard as recordFilewatchBatch. triple-review P1 (PR #380).
2555
+ // Same post-close guard as recordFilewatchBatch. triple-review P1 (PR).
1089
2556
  if (this.closed)
1090
2557
  return;
1091
2558
  this.appendSystemLine(`Filewatch off: ${info.watchedCount} watched paths exceeded cap (${info.cap}). Falling back to manual stat-on-read.`);
@@ -1093,7 +2560,7 @@ export class ReplSession {
1093
2560
  /**
1094
2561
  * Fetch one URL via the web_fetch tool and inject the resulting
1095
2562
  * Markdown into the transcript as an operator-attributed brief. The
1096
- * `<untrusted-content>` sentinel travels with the body so the Mira
2563
+ * `<untrusted-content>` sentinel travels with the body so the Pugi
1097
2564
  * system prompt can refuse to follow instructions inside it.
1098
2565
  *
1099
2566
  * Gating: the dispatcher reads PugiSettings from disk on every
@@ -1148,33 +2615,36 @@ export class ReplSession {
1148
2615
  this.appendSystemLine(capLine);
1149
2616
  }
1150
2617
  this.appendOperatorLine(brief);
1151
- this.patch({ briefStartedAtEpochMs: this.now() });
1152
- // α6.9 + R3 P1 (Codex triple-review 2026-05-25): supersede the
2618
+ // Reset `lastCompletedOutcome` so a fresh dispatch does not
2619
+ // inherit the prior turn's status-bar label (e.g. a stale
2620
+ // "replied" sticking around while the next dispatch is in flight).
2621
+ this.patch({ briefStartedAtEpochMs: this.now(), lastCompletedOutcome: null });
2622
+ // + R3 P1 (Codex triple-review): supersede the
1153
2623
  // prior dispatch when one is in flight. Steps in order:
1154
2624
  //
1155
- // 1. Abort the old CancellationToken so any in-flight tool
1156
- // holding `ctx.cancellation` sees `isAborted = true` and bails
1157
- // (the R2 fix; preserves the file-tools cancellation gate).
1158
- // 2. Drive the OLD FSM through `aborting -> aborted` terminal.
1159
- // This is load-bearing for the R3 race: a LATE event arriving
1160
- // on the old FSM (`agent.spawned`, `agent.step`, terminal,
1161
- // etc.) before the timestamp gate trips would otherwise still
1162
- // attempt to transition the new FSM. Driving the old FSM to a
1163
- // terminal state means the FSM check in
1164
- // `advanceFsmOnDispatchEnd` (`isTerminal`) short-circuits as a
1165
- // defense-in-depth layer.
1166
- // 3. `resetFsmToIdle()` mints a fresh FSM so the new dispatch
1167
- // starts clean. The FSM legal-transition matrix forbids
1168
- // `aborted -> awaiting_response`, so the reset is required.
1169
- // 4. Record `currentDispatchStartTime` BEFORE bumping
1170
- // `dispatchSeq` + clearing `taskDispatchSeq`. The timestamp
1171
- // gate in `handleServerEvent` checks
1172
- // `event.timestamp < currentDispatchStartTime` to drop late
1173
- // events from any superseded dispatch (including the late
1174
- // `agent.spawned` that the R2 seq gate could not catch).
1175
- // 5. Clear `taskDispatchSeq` so any stamp left over from the old
1176
- // dispatch cannot influence seq comparisons for the new turn.
1177
- // 6. Bump `dispatchSeq` and mint a fresh `CancellationToken`.
2625
+ // 1. Abort the old CancellationToken so any in-flight tool
2626
+ // holding `ctx.cancellation` sees `isAborted = true` and bails
2627
+ // (the R2 fix; preserves the file-tools cancellation gate).
2628
+ // 2. Drive the OLD FSM through `aborting -> aborted` terminal.
2629
+ // This is load-bearing for the R3 race: a LATE event arriving
2630
+ // on the old FSM (`agent.spawned`, `agent.step`, terminal,
2631
+ // etc.) before the timestamp gate trips would otherwise still
2632
+ // attempt to transition the new FSM. Driving the old FSM to a
2633
+ // terminal state means the FSM check in
2634
+ // `advanceFsmOnDispatchEnd` (`isTerminal`) short-circuits as a
2635
+ // defense-in-depth layer.
2636
+ // 3. `resetFsmToIdle()` mints a fresh FSM so the new dispatch
2637
+ // starts clean. The FSM legal-transition matrix forbids
2638
+ // `aborted -> awaiting_response`, so the reset is required.
2639
+ // 4. Record `currentDispatchStartTime` BEFORE bumping
2640
+ // `dispatchSeq` + clearing `taskDispatchSeq`. The timestamp
2641
+ // gate in `handleServerEvent` checks
2642
+ // `event.timestamp < currentDispatchStartTime` to drop late
2643
+ // events from any superseded dispatch (including the late
2644
+ // `agent.spawned` that the R2 seq gate could not catch).
2645
+ // 5. Clear `taskDispatchSeq` so any stamp left over from the old
2646
+ // dispatch cannot influence seq comparisons for the new turn.
2647
+ // 6. Bump `dispatchSeq` and mint a fresh `CancellationToken`.
1178
2648
  //
1179
2649
  // If no prior dispatch is in flight (clean idle / terminal entry),
1180
2650
  // the supersede block is skipped; we only reset the FSM if it sits
@@ -1226,7 +2696,7 @@ export class ReplSession {
1226
2696
  if (this.fsm.current === 'idle') {
1227
2697
  this.fsm.transition('awaiting_response', 'brief_dispatched');
1228
2698
  }
1229
- // α6.9: re-open the SSE stream if a prior `cancel()` tore it
2699
+ // : re-open the SSE stream if a prior `cancel()` tore it
1230
2700
  // down. Without this, the new brief would dispatch on admin-api
1231
2701
  // but the client would never observe `agent.spawned` / `step` /
1232
2702
  // `completed` — the operator would see a stalled status bar
@@ -1245,14 +2715,14 @@ export class ReplSession {
1245
2715
  }
1246
2716
  catch (error) {
1247
2717
  this.appendSystemLine(`Brief dispatch refused: ${this.errorMessage(error)}`);
1248
- // α6.9: a failed brief POST never produced a turn, so we move
2718
+ // : a failed brief POST never produced a turn, so we move
1249
2719
  // the FSM straight to `failed` so the bottom-bar surfaces the
1250
2720
  // outcome and the next brief can mint a fresh token.
1251
2721
  this.markDispatchFailed('post_brief_failed');
1252
2722
  }
1253
2723
  }
1254
2724
  /**
1255
- * α6.9: reset the FSM to `idle` after a terminal transition so the
2725
+ * : reset the FSM to `idle` after a terminal transition so the
1256
2726
  * next brief can start. The FSM does not allow direct
1257
2727
  * `completed -> awaiting_response`, so we mint a fresh FSM by
1258
2728
  * overwriting the field. Listeners on the old FSM are dropped (they
@@ -1281,7 +2751,7 @@ export class ReplSession {
1281
2751
  this.patch({ dispatchState: 'idle', dispatchToolLabel: null });
1282
2752
  }
1283
2753
  /**
1284
- * α6.9: short-circuit the FSM to `failed` on a non-recoverable
2754
+ * : short-circuit the FSM to `failed` on a non-recoverable
1285
2755
  * dispatch error (network refusal, malformed event, etc). Idempotent
1286
2756
  * — a second call from a terminal state is a no-op.
1287
2757
  */
@@ -1297,7 +2767,7 @@ export class ReplSession {
1297
2767
  if (this.fsm.current === 'aborting')
1298
2768
  return;
1299
2769
  this.fsm.transition('failed', reason);
1300
- // α6.9 P1 fix (Claude triple-review): postBrief threw between
2770
+ // P1 fix (Claude triple-review): postBrief threw between
1301
2771
  // openStream() and dispatch registration server-side. The local
1302
2772
  // SSE handle is open but listening for events under a dispatchId
1303
2773
  // the admin-api never created. If we leave it open, any inbound
@@ -1306,7 +2776,7 @@ export class ReplSession {
1306
2776
  // IllegalDispatchTransitionError. Tear down so the next brief
1307
2777
  // re-opens cleanly via dispatchBrief's openStream() gate.
1308
2778
  //
1309
- // R2 P2 fix (Claude triple-review 2026-05-25): tear down the
2779
+ // R2 P2 fix (Claude triple-review): tear down the
1310
2780
  // stream BEFORE nulling the token. Same ordering contract as
1311
2781
  // `cancel()`: any onAbort listener fired during teardown should
1312
2782
  // observe the (now-aborted) token via getCurrentDispatchToken()
@@ -1364,7 +2834,7 @@ export class ReplSession {
1364
2834
  onError: (error) => {
1365
2835
  if (this.closed)
1366
2836
  return;
1367
- // α6.14.2 wave 5: when admin-api restarts it drops the in-memory
2837
+ // wave 5: when admin-api restarts it drops the in-memory
1368
2838
  // session store, so subscribe returns HTTP 404 forever on the
1369
2839
  // saved sessionId. Detect that case and mint a fresh server
1370
2840
  // session silently rather than spamming the operator with
@@ -1394,7 +2864,7 @@ export class ReplSession {
1394
2864
  void this.recreateSessionSilently();
1395
2865
  return;
1396
2866
  }
1397
- // α6.14.4 CEO dogfood 2026-05-25 (parity with Claude Code):
2867
+ // CEO dogfood (parity with the upstream tool):
1398
2868
  // collapse the repeated "Stream interrupted (fetch failed).
1399
2869
  // Reconnecting." spam. The status bar already shows
1400
2870
  // connection='reconnecting' AND the attempt counter; pushing
@@ -1419,7 +2889,7 @@ export class ReplSession {
1419
2889
  * `Error("HTTP 404 on SSE stream")`. We pattern-match on the status
1420
2890
  * 404 so a different transport (e.g. a test fake or a future polling
1421
2891
  * fallback) can surface the same intent with the same shape.
1422
- * (α6.14.2 wave 5.)
2892
+ *
1423
2893
  */
1424
2894
  isSessionNotFoundError(error) {
1425
2895
  const msg = this.errorMessage(error);
@@ -1432,7 +2902,7 @@ export class ReplSession {
1432
2902
  * a permanently down admin-api fails loud after a few seconds of
1433
2903
  * trying. Logged once per attempt at debug level (we surface a
1434
2904
  * single visible line on first auto-recreate so the operator knows
1435
- * what happened, then stay quiet). (α6.14.2 wave 5.)
2905
+ * what happened, then stay quiet).
1436
2906
  */
1437
2907
  async recreateSessionSilently() {
1438
2908
  if (this.closed)
@@ -1475,6 +2945,7 @@ export class ReplSession {
1475
2945
  apiUrl: this.options.apiUrl,
1476
2946
  apiKey: this.options.apiKey,
1477
2947
  workspace: this.options.workspace,
2948
+ cyberZoo: this.options.cyberZoo,
1478
2949
  });
1479
2950
  this.patch({ sessionId, connection: 'connecting' });
1480
2951
  this.openStream();
@@ -1506,7 +2977,7 @@ export class ReplSession {
1506
2977
  }
1507
2978
  /* ------------- event reducer -------------- */
1508
2979
  handleServerEvent(event) {
1509
- // R3 P1 fix (Codex triple-review 2026-05-25): wall-clock gate that
2980
+ // R3 P1 fix (Codex triple-review): wall-clock gate that
1510
2981
  // drops events from a SUPERSEDED dispatch. The R2 seq gate alone
1511
2982
  // could not catch a LATE `agent.spawned` for an old taskId arriving
1512
2983
  // AFTER `dispatchBrief` already bumped `dispatchSeq`. The late
@@ -1534,16 +3005,16 @@ export class ReplSession {
1534
3005
  switch (event.type) {
1535
3006
  case 'agent.spawned': {
1536
3007
  const persona = safePersonaName(event.role);
1537
- // Wave 4 fix 2026-05-25: the roster collapses to one row per
1538
- // persona slug. The α5.7 reducer pushed a fresh row on every
3008
+ // fix: the roster collapses to one row per
3009
+ // persona slug. The reducer pushed a fresh row on every
1539
3010
  // spawn, so after three turns the bottom panel stacked
1540
3011
  // "Pugi orchestrator shipped" three times. The new contract:
1541
- // - If a row already exists for this personaSlug, REUSE it.
1542
- // Replace its taskId, reset status to 'queued', clear the
1543
- // detail line, restart the duration clock, zero the token
1544
- // counters. The persona name + slug + role stay stable
1545
- // (they are the row identity).
1546
- // - If no row exists yet, push a new one.
3012
+ // - If a row already exists for this personaSlug, REUSE it.
3013
+ // Replace its taskId, reset status to 'queued', clear the
3014
+ // detail line, restart the duration clock, zero the token
3015
+ // counters. The persona name + slug + role stay stable
3016
+ // (they are the row identity).
3017
+ // - If no row exists yet, push a new one.
1547
3018
  // Per-task lifecycle (step/tokens/completed/blocked/failed) is
1548
3019
  // keyed off `taskId` everywhere, so the reused row still folds
1549
3020
  // the latest task's events correctly.
@@ -1567,7 +3038,7 @@ export class ReplSession {
1567
3038
  else {
1568
3039
  this.patch({ agents: [node, ...this.state.agents] });
1569
3040
  }
1570
- // R2 P1 fix (Codex triple-review 2026-05-25): stamp the live
3041
+ // R2 P1 fix (Codex triple-review): stamp the live
1571
3042
  // dispatch sequence onto this taskId so terminal handlers can
1572
3043
  // tell apart a "current dispatch" event from a "superseded
1573
3044
  // dispatch" event. See `dispatchSeq` + `taskDispatchSeq`
@@ -1579,7 +3050,7 @@ export class ReplSession {
1579
3050
  // double-print. `void persona` keeps the resolved name in scope
1580
3051
  // for the agent tree node above without leaking it into the
1581
3052
  // transcript body.
1582
- // α6.14.3 CEO dogfood 2026-05-25: drop the "dispatched (X)"
3053
+ // CEO dogfood: drop the "dispatched (X)"
1583
3054
  // transcript echo. The agent tree pane already shows the
1584
3055
  // spawned state; printing it as a persona row is pure noise
1585
3056
  // between the operator's brief and the persona's real reply.
@@ -1587,7 +3058,7 @@ export class ReplSession {
1587
3058
  return;
1588
3059
  }
1589
3060
  case 'agent.step': {
1590
- // α6.3 office-hours: scan the running buffer for `<pugi-ask>` /
3061
+ // office-hours: scan the running buffer for `<pugi-ask>` /
1591
3062
  // `<pugi-plan-review>` envelopes BEFORE we cache the detail.
1592
3063
  // The parser returns the cleaned remainder with the raw XML
1593
3064
  // stripped, so the operator never sees the envelope as prose.
@@ -1600,7 +3071,7 @@ export class ReplSession {
1600
3071
  if (sanitised && sanitised.trim().length > 0) {
1601
3072
  this.lastStepDetail.set(event.taskId, sanitised);
1602
3073
  }
1603
- // α6.12: synthesise a tool call entry when the step detail
3074
+ // : synthesise a tool call entry when the step detail
1604
3075
  // matches a tool-invocation grammar. The pattern is generous
1605
3076
  // (Read(path) / Edit(path:lines) / Bash(cmd) / Grep(pat) /
1606
3077
  // Glob(pat) / WebFetch(url)) so the pane has rows to render
@@ -1615,7 +3086,7 @@ export class ReplSession {
1615
3086
  });
1616
3087
  if (synthesised) {
1617
3088
  this.appendToolCall(synthesised);
1618
- // α6.9: a fresh tool call moves the FSM to `tool_running`
3089
+ // : a fresh tool call moves the FSM to `tool_running`
1619
3090
  // when the dispatch is still active. The status-bar surface
1620
3091
  // also gets a short label (`tool: read`, `tool: bash`, etc).
1621
3092
  // Aborting / terminal states are not allowed to transition
@@ -1631,8 +3102,22 @@ export class ReplSession {
1631
3102
  }
1632
3103
  case 'agent.tokens': {
1633
3104
  const delta = event.tokensIn + event.tokensOut;
3105
+ // cost-meter sprint — bind a client-side USD figure to this
3106
+ // frame. The model slug rides on the event (optional for back-
3107
+ // compat); the price ladder in `model-pricing.ts` falls back to
3108
+ // a Sonnet-tier rate when the slug is missing, so the meter is
3109
+ // always populated. Negative / NaN values are clamped to zero
3110
+ // inside `computeCostUsd` so a buggy upstream never credits the
3111
+ // meter.
3112
+ const deltaCostUsd = computeCostUsd(event.tokensIn, event.tokensOut, event.model);
1634
3113
  this.patch({
1635
3114
  tokensDownstreamTotal: this.state.tokensDownstreamTotal + delta,
3115
+ sessionTokensIn: this.state.sessionTokensIn + event.tokensIn,
3116
+ sessionTokensOut: this.state.sessionTokensOut + event.tokensOut,
3117
+ sessionCostUsd: this.state.sessionCostUsd + deltaCostUsd,
3118
+ turnTokensIn: this.state.turnTokensIn + event.tokensIn,
3119
+ turnTokensOut: this.state.turnTokensOut + event.tokensOut,
3120
+ turnCostUsd: this.state.turnCostUsd + deltaCostUsd,
1636
3121
  agents: this.state.agents.map((a) => a.taskId === event.taskId
1637
3122
  ? {
1638
3123
  ...a,
@@ -1652,17 +3137,49 @@ export class ReplSession {
1652
3137
  }
1653
3138
  this.askBuffer.delete(event.taskId);
1654
3139
  this.askBufferPending.delete(event.taskId);
3140
+ // Honour the work-done signal from admin-api.
3141
+ // `outcome === 'replied'` means the turn was a pure text reply
3142
+ // with no delegate XML and no tool call — render it as
3143
+ // "replied" so the operator can tell the difference between
3144
+ // "the orchestrator just talked" and "real work shipped".
3145
+ // Older servers omit the field; default to 'shipped' so the
3146
+ // existing wire stays back-compat.
3147
+ const completedStatus = event.outcome === 'replied' ? 'replied' : 'shipped';
1655
3148
  this.patch({
1656
3149
  agents: this.state.agents.map((a) => a.taskId === event.taskId
1657
- ? { ...a, status: 'shipped', detail: 'shipped' }
3150
+ ? { ...a, status: completedStatus, detail: completedStatus }
1658
3151
  : a),
3152
+ // Mirror the outcome to top-level state so the status-bar
3153
+ // can render `replied` instead of the legacy `shipped`
3154
+ // label when the FSM lands in `completed`. Without this
3155
+ // the bottom-bar would still say "shipped" while the
3156
+ // agent-tree said "replied", restoring the same
3157
+ // contradiction this PR is fixing (Codex triple-review P2).
3158
+ //
3159
+ // r2: gate on the same stale-dispatch check that
3160
+ // advanceFsmOnDispatchEnd applies. If this completion
3161
+ // belongs to a SUPERSEDED dispatch (a newer dispatchBrief
3162
+ // already bumped dispatchSeq before this late terminal
3163
+ // arrived), don't let the status-bar label flip to the
3164
+ // stale outcome — the current turn is the live one.
3165
+ // The agent-tree row patch above is still safe because
3166
+ // it only updates the row keyed by taskId.
3167
+ ...(this.isStaleTaskEvent(event.taskId)
3168
+ ? {}
3169
+ : { lastCompletedOutcome: completedStatus }),
1659
3170
  });
1660
- // α6.9: transition the FSM to `completed` when no other
3171
+ // : transition the FSM to `completed` when no other
1661
3172
  // dispatch is still in flight. The check uses the agents list
1662
3173
  // POST-patch so any sibling task in `queued` / `thinking` keeps
1663
3174
  // the dispatch alive; the FSM only goes terminal when the last
1664
3175
  // agent ships.
1665
3176
  this.advanceFsmOnDispatchEnd('completed', 'agent_completed', event.taskId);
3177
+ // cost-meter sprint — flush the per-turn delta when the
3178
+ // LAST agent settles. Decoupled from the FSM gate so a test
3179
+ // fixture (or a single-agent dispatch that never reached
3180
+ // `awaiting_response` — happens on instant SSE replay) still
3181
+ // gets the row written into recentTurns + lastTurnDelta.
3182
+ this.maybeFlushTurnOnAgentSettle(event.taskId);
1666
3183
  if (target) {
1667
3184
  // If the persona actually produced a reply via incremental
1668
3185
  // agent.step events, render that reply in the transcript so
@@ -1675,16 +3192,16 @@ export class ReplSession {
1675
3192
  if (finalDetail
1676
3193
  && finalDetail !== 'queued for dispatch'
1677
3194
  && finalDetail.trim().length > 4) {
1678
- // α6.12: ship the WHOLE body as one transcript row when the
3195
+ // : ship the WHOLE body as one transcript row when the
1679
3196
  // reply contains ANY Markdown structure (code fence, bullet
1680
3197
  // list, numbered list, headings). The conversation pane
1681
3198
  // routes it through Markdown renderer в one pass, preserving
1682
3199
  // grouped bullets + heading hierarchy. Plain prose still
1683
3200
  // splits per line so word-wrap stays correct.
1684
3201
  //
1685
- // Claude triple-review P1 (PR #369): the prior `includes('```')`
3202
+ // Claude triple-review P1 (PR): the prior `includes('```')`
1686
3203
  // gate only caught fences - multi-line bullets fragmented
1687
- // per row showed as `▸ Mira • read PUGI.md / ▸ Mira • patched
3204
+ // per row showed as `▸ Pugi • read PUGI.md / ▸ Pugi • patched
1688
3205
  // bug / ...` instead of a single grouped bullet block.
1689
3206
  if (looksLikeMarkdown(finalDetail)) {
1690
3207
  this.appendPersonaLine(target.personaSlug, finalDetail);
@@ -1699,7 +3216,7 @@ export class ReplSession {
1699
3216
  }
1700
3217
  }
1701
3218
  else {
1702
- // α6.14.3 CEO dogfood 2026-05-25: drop the literal
3219
+ // CEO dogfood: drop the literal
1703
3220
  // "shipped." fallback row. If we have no cached detail to
1704
3221
  // surface, stay silent. The agent tree pane already shows
1705
3222
  // the green check + duration.
@@ -1723,11 +3240,15 @@ export class ReplSession {
1723
3240
  if (target) {
1724
3241
  this.appendPersonaLine(target.personaSlug, `blocked: ${event.detail}`);
1725
3242
  }
1726
- // α6.9: `blocked` is a graceful refusal, not a crash — treat it
3243
+ // : `blocked` is a graceful refusal, not a crash — treat it
1727
3244
  // as a `completed` outcome from the FSM's perspective so the
1728
3245
  // operator sees the bottom-bar settle back to `idle` after the
1729
3246
  // last block clears.
1730
3247
  this.advanceFsmOnDispatchEnd('completed', 'agent_blocked', event.taskId);
3248
+ // cost-meter sprint — flush the per-turn delta (blocked
3249
+ // still counts as a billable turn — the operator paid for the
3250
+ // tokens that landed before the refusal).
3251
+ this.maybeFlushTurnOnAgentSettle(event.taskId);
1731
3252
  return;
1732
3253
  }
1733
3254
  case 'agent.failed': {
@@ -1746,17 +3267,21 @@ export class ReplSession {
1746
3267
  if (target) {
1747
3268
  this.appendPersonaLine(target.personaSlug, `failed: ${event.error}`);
1748
3269
  }
1749
- // α6.9: terminal `failed` transition when no sibling task
3270
+ // : terminal `failed` transition when no sibling task
1750
3271
  // remains. Same defer-until-last-agent semantics as
1751
3272
  // `completed` so the bottom-bar surface tracks the dispatch
1752
3273
  // collectively.
1753
3274
  this.advanceFsmOnDispatchEnd('failed', 'agent_failed', event.taskId);
3275
+ // cost-meter sprint — flush the per-turn delta when the
3276
+ // dispatch fails (the operator still paid for whatever tokens
3277
+ // landed before the failure).
3278
+ this.maybeFlushTurnOnAgentSettle(event.taskId);
1754
3279
  return;
1755
3280
  }
1756
3281
  }
1757
3282
  }
1758
3283
  /**
1759
- * α6.9 helper: advance the FSM to `tool_running` when a tool call
3284
+ * helper: advance the FSM to `tool_running` when a tool call
1760
3285
  * lands mid-dispatch. Guarded against terminal / aborting states so
1761
3286
  * a late tool event after `cancel()` does not throw on an illegal
1762
3287
  * transition. The `tool` label drives the bottom-bar's
@@ -1780,7 +3305,7 @@ export class ReplSession {
1780
3305
  this.patch({ dispatchToolLabel: `tool: ${tool}` });
1781
3306
  }
1782
3307
  /**
1783
- * α6.9 helper: advance the FSM toward a terminal outcome when the
3308
+ * helper: advance the FSM toward a terminal outcome when the
1784
3309
  * LAST in-flight agent's lifecycle ends. The dispatch is "still
1785
3310
  * running" when any other agent in the tree is in `queued` /
1786
3311
  * `thinking`; the FSM only goes terminal when the last one settles.
@@ -1789,13 +3314,25 @@ export class ReplSession {
1789
3314
  * after a manual `cancel()` finds the FSM already in `aborted` and
1790
3315
  * is silently dropped.
1791
3316
  */
3317
+ /**
3318
+ * — shared stale-task check used by both the FSM advance
3319
+ * gate AND the status-bar `lastCompletedOutcome` mirror. Lifts the
3320
+ * R2 dispatchSeq compare out of `advanceFsmOnDispatchEnd` so other
3321
+ * agent.completed-handler side-effects (status-bar label, future
3322
+ * metric counters) can apply the same guard without duplicating it.
3323
+ * Returns true iff the task's stamped dispatchSeq is older than the
3324
+ * current dispatchSeq — i.e. a newer dispatchBrief() superseded it
3325
+ * and the late terminal event must not corrupt live-turn state.
3326
+ */
3327
+ isStaleTaskEvent(taskId) {
3328
+ const taskSeq = this.taskDispatchSeq.get(taskId);
3329
+ return taskSeq !== undefined && taskSeq < this.dispatchSeq;
3330
+ }
1792
3331
  advanceFsmOnDispatchEnd(outcome, reason, taskId) {
1793
- // R2 P1 fix (Codex triple-review 2026-05-25): a terminal event
3332
+ // R2 P1 fix (Codex triple-review): a terminal event
1794
3333
  // for a SUPERSEDED dispatch must NOT advance the live FSM or null
1795
- // the live token. If the event carries a taskId and the stamped
1796
- // dispatchSeq for that task is older than the current dispatchSeq,
1797
- // the event belongs to a prior dispatch that was replaced by a
1798
- // newer `dispatchBrief()`. Silently drop the FSM advance.
3334
+ // the live token. Delegates to isStaleTaskEvent so the agent.completed
3335
+ // status-bar mirror in the handler above uses the same gate.
1799
3336
  if (taskId !== undefined) {
1800
3337
  const taskSeq = this.taskDispatchSeq.get(taskId);
1801
3338
  if (taskSeq !== undefined && taskSeq < this.dispatchSeq) {
@@ -1827,6 +3364,63 @@ export class ReplSession {
1827
3364
  this.currentDispatchToken = null;
1828
3365
  this.patch({ briefStartedAtEpochMs: undefined });
1829
3366
  }
3367
+ /**
3368
+ * cost-meter sprint — gate the per-turn flush on "this was the
3369
+ * LAST in-flight agent". Mirrors the `stillActive` guard inside
3370
+ * `advanceFsmOnDispatchEnd` so a multi-agent dispatch only emits a
3371
+ * single recentTurns row + a single lastTurnDelta flash.
3372
+ *
3373
+ * Idempotent: if no tokens have been billed this turn, the inner
3374
+ * `flushTurnAccumulator` short-circuits without pushing an empty row.
3375
+ */
3376
+ maybeFlushTurnOnAgentSettle(taskId) {
3377
+ const stillActive = this.state.agents.some((a) => a.status === 'queued' || a.status === 'thinking');
3378
+ if (stillActive)
3379
+ return;
3380
+ this.flushTurnAccumulator(taskId);
3381
+ }
3382
+ /**
3383
+ * cost-meter sprint — flush the per-turn accumulator into
3384
+ * `recentTurns` + `lastTurnDelta`. Idempotent + safe to call from any
3385
+ * terminal-state branch (`agent.completed` / `agent.blocked` /
3386
+ * `agent.failed`). When no tokens have been billed this turn
3387
+ * (instant abort, cap-warning gate), the helper short-circuits
3388
+ * without pushing an empty row.
3389
+ */
3390
+ flushTurnAccumulator(taskId) {
3391
+ const turnTokensIn = this.state.turnTokensIn;
3392
+ const turnTokensOut = this.state.turnTokensOut;
3393
+ const turnCostUsd = this.state.turnCostUsd;
3394
+ if (turnTokensIn === 0 && turnTokensOut === 0) {
3395
+ // Idempotent zero-flush — never push an empty row into recentTurns.
3396
+ return;
3397
+ }
3398
+ const turnId = taskId !== undefined ? taskId : `turn-${this.dispatchSeq}-${this.now()}`;
3399
+ const newTurn = {
3400
+ id: turnId,
3401
+ tokensIn: turnTokensIn,
3402
+ tokensOut: turnTokensOut,
3403
+ costUsd: turnCostUsd,
3404
+ completedAt: new Date(this.now()).toISOString(),
3405
+ };
3406
+ // Keep the buffer capped at 5 entries (oldest first). The push
3407
+ // order matches the surface contract: `/cost` paginates oldest →
3408
+ // newest so the operator scans top-down chronologically.
3409
+ const recent = [...this.state.recentTurns, newTurn];
3410
+ const trimmed = recent.length > 5 ? recent.slice(-5) : recent;
3411
+ this.patch({
3412
+ recentTurns: trimmed,
3413
+ lastTurnDelta: {
3414
+ tokensIn: turnTokensIn,
3415
+ tokensOut: turnTokensOut,
3416
+ costUsd: turnCostUsd,
3417
+ completedAtEpochMs: this.now(),
3418
+ },
3419
+ turnTokensIn: 0,
3420
+ turnTokensOut: 0,
3421
+ turnCostUsd: 0,
3422
+ });
3423
+ }
1830
3424
  /* ------------- transcript helpers -------------- */
1831
3425
  /**
1832
3426
  * Look up the persona slug for a running task. Used by the tool call
@@ -1839,6 +3433,73 @@ export class ReplSession {
1839
3433
  const agent = this.state.agents.find((a) => a.taskId === taskId);
1840
3434
  return agent?.personaSlug ?? 'unknown';
1841
3435
  }
3436
+ /**
3437
+ * small-CC-parity batch : public ingest path for
3438
+ * a backend-driven `tool.call.delta` event. Appends the delta tail
3439
+ * onto the row's `streamingDelta` (capped at
3440
+ * `STREAMING_DELTA_MAX_CHARS` so the row stays single-line) when the
3441
+ * id matches a `running` row. No-op when the id is unknown OR when
3442
+ * the row already transitioned to a terminal status — late deltas
3443
+ * from a completed call must not overwrite the final detail.
3444
+ *
3445
+ * The renderer in `tool-stream-pane.tsx` reads `streamingDelta` to
3446
+ * paint the inline preview after the canonical args. This method is
3447
+ * the seam the future admin-api SSE consumer hooks into; until then
3448
+ * the spec drives it directly so the delta-append branch is locked
3449
+ * down behaviourally.
3450
+ */
3451
+ appendToolCallDelta(id, deltaChunk) {
3452
+ if (!id || !deltaChunk)
3453
+ return;
3454
+ const idx = this.state.toolCalls.findIndex((c) => c.id === id);
3455
+ if (idx < 0)
3456
+ return;
3457
+ const existing = this.state.toolCalls[idx];
3458
+ if (existing.status !== 'running')
3459
+ return;
3460
+ const current = existing.streamingDelta ?? '';
3461
+ let combined = current + deltaChunk;
3462
+ if (combined.length > STREAMING_DELTA_MAX_CHARS) {
3463
+ // Keep the TAIL — the operator wants the freshest bytes (the
3464
+ // line being written right now), not the stale head. The leading
3465
+ // ellipsis signals truncation.
3466
+ combined = `…${combined.slice(combined.length - STREAMING_DELTA_MAX_CHARS + 1)}`;
3467
+ }
3468
+ const next = this.state.toolCalls.slice();
3469
+ next[idx] = { ...existing, streamingDelta: combined };
3470
+ this.patch({ toolCalls: next });
3471
+ }
3472
+ /**
3473
+ * small-CC-parity batch : public ingest path for
3474
+ * the terminal `tool.call.end` event. Flips the row to `ok` / `error`
3475
+ * with the resolved duration + optional result preview. Cleans up the
3476
+ * transient `streamingDelta` so the completed row renders cleanly
3477
+ * without the live tail. No-op when the id is unknown.
3478
+ */
3479
+ endToolCall(input) {
3480
+ if (!input.id)
3481
+ return;
3482
+ const idx = this.state.toolCalls.findIndex((c) => c.id === input.id);
3483
+ if (idx < 0)
3484
+ return;
3485
+ const existing = this.state.toolCalls[idx];
3486
+ const endedAt = input.endedAtEpochMs ?? Date.now();
3487
+ const durationMs = Math.max(0, endedAt - existing.startedAtEpochMs);
3488
+ const preview = input.resultPreview
3489
+ ? truncatePreview(input.resultPreview, RESULT_PREVIEW_MAX_CHARS)
3490
+ : undefined;
3491
+ const next = this.state.toolCalls.slice();
3492
+ next[idx] = {
3493
+ ...existing,
3494
+ status: input.status,
3495
+ detail: input.detail ?? existing.detail,
3496
+ resultLines: input.resultLines ?? existing.resultLines,
3497
+ durationMs,
3498
+ resultPreview: preview,
3499
+ streamingDelta: undefined,
3500
+ };
3501
+ this.patch({ toolCalls: next });
3502
+ }
1842
3503
  /**
1843
3504
  * Fold a tool call entry into the rolling list. If the entry id
1844
3505
  * already exists, replace it in-place (so a synthesised `running` →
@@ -1868,10 +3529,10 @@ export class ReplSession {
1868
3529
  this.appendRow({ source: 'system', text });
1869
3530
  }
1870
3531
  appendPersonaLine(personaSlug, text) {
1871
- // α6.14.2 wave 5: dedup the persona display-name prefix. The
3532
+ // wave 5: dedup the persona display-name prefix. The
1872
3533
  // conversation pane already renders `▸ <DisplayName> <text>` from
1873
3534
  // the slug → name map; when the model's own reply begins with
1874
- // the same display name (CEO 2026-05-25 screenshot: "Pugi Pugi,
3535
+ // the same display name (CEO screenshot: "Pugi Pugi,
1875
3536
  // координатор Pugi"), the operator sees the name twice. Strip
1876
3537
  // the leading display-name token (with optional trailing comma /
1877
3538
  // colon / whitespace) so the prefix the pane adds is the only one
@@ -1883,13 +3544,14 @@ export class ReplSession {
1883
3544
  this.appendRow({ source: 'persona', text: stripped, personaSlug });
1884
3545
  }
1885
3546
  appendRow(input) {
1886
- if (input.text.length === 0)
3547
+ if (input.text.length === 0 && input.source !== 'compact-boundary')
1887
3548
  return;
1888
3549
  const row = {
1889
3550
  id: randomUUID(),
1890
3551
  source: input.source,
1891
3552
  text: input.text,
1892
3553
  personaSlug: input.personaSlug,
3554
+ compaction: input.compaction,
1893
3555
  timestampEpochMs: this.now(),
1894
3556
  };
1895
3557
  const next = this.state.transcript.concat(row).slice(-MAX_TRANSCRIPT_ROWS);
@@ -1898,10 +3560,66 @@ export class ReplSession {
1898
3560
  // Persistence is fail-safe: a single error becomes one system
1899
3561
  // line, subsequent errors are silent so a stuck disk does not
1900
3562
  // flood the operator. The mapping from row.source -> store kind:
1901
- // operator -> 'user' (drives turn_count + title)
1902
- // persona -> 'persona'
1903
- // system -> 'system'
3563
+ // operator -> 'user' (drives turn_count + title)
3564
+ // persona -> 'persona'
3565
+ // system -> 'system'
1904
3566
  this.persistRow(row);
3567
+ // evaluate the auto-compact gate after
3568
+ // every appendRow that produces a transcript turn. Wrapped in a
3569
+ // setImmediate so the gate never blocks the input-handling fast
3570
+ // path; if the threshold is tripped, the auto-trigger dispatches
3571
+ // `/compact` in the background while the operator keeps typing.
3572
+ if (row.source === 'operator' || row.source === 'persona') {
3573
+ this.maybeAutoCompact();
3574
+ }
3575
+ }
3576
+ /**
3577
+ * Auto-compact gate. Cheap: builds an in-memory token estimate from
3578
+ * the current transcript and consults `evaluateAutoCompact`. When the
3579
+ * gate fires AND a compaction is not already in flight, we dispatch
3580
+ * `/compact` with `trigger='auto'`. The fire-and-forget shape means
3581
+ * the input box stays responsive while the background round-trip
3582
+ * runs.
3583
+ *
3584
+ * Hysteresis: `compactionInFlight` blocks re-entry. The gate is
3585
+ * cleared when the dispatch promise resolves regardless of outcome
3586
+ * so a transient transport failure does not permanently disable the
3587
+ * auto-trigger.
3588
+ */
3589
+ compactionInFlight = false;
3590
+ maybeAutoCompact() {
3591
+ if (this.compactionInFlight)
3592
+ return;
3593
+ if (!this.store || !this.localSessionId)
3594
+ return;
3595
+ if (process.env['PUGI_AUTOCOMPACT_DISABLED'] === '1')
3596
+ return;
3597
+ // Token estimate from the in-memory transcript. The estimate is a
3598
+ // lower bound on actual context pressure (server-side system
3599
+ // prompts add overhead) but the 4-char/token heuristic plus the
3600
+ // 0.75 default threshold gives generous headroom.
3601
+ const texts = this.state.transcript.map((r) => r.text);
3602
+ const tokenCount = estimateTokensInMany(texts);
3603
+ // Conservative default: assume the smallest commonly-used window
3604
+ // (32k tokens for deepseek-v3.1). Resolving the live model slug
3605
+ // through DispatchFSM + admin-api adds latency on a hot path; the
3606
+ // 0.75 threshold + smallest-window assumption errs toward
3607
+ // EARLY trigger which is the safe direction.
3608
+ const verdict = evaluateAutoCompact({
3609
+ tokenCount,
3610
+ windowSize: 32_000,
3611
+ });
3612
+ if (verdict.kind !== 'fire')
3613
+ return;
3614
+ this.compactionInFlight = true;
3615
+ void (async () => {
3616
+ try {
3617
+ await this.dispatchCompact('auto');
3618
+ }
3619
+ finally {
3620
+ this.compactionInFlight = false;
3621
+ }
3622
+ })();
1905
3623
  }
1906
3624
  /**
1907
3625
  * Best-effort write of one transcript row into the local
@@ -1912,6 +3630,15 @@ export class ReplSession {
1912
3630
  persistRow(row) {
1913
3631
  if (!this.store)
1914
3632
  return;
3633
+ // L29 : `compact-boundary` transcript rows are echoes of
3634
+ // the JSONL `compaction` event the compact runner already appended
3635
+ // via `appendCompactBoundary`. Persisting them here would double-
3636
+ // write the marker (and worse, with a stripped payload that lacks
3637
+ // `summary` / `coversUntilOffset`) — `isCompactBoundary` would
3638
+ // reject the duplicate but `applyCompactMask` would still index off
3639
+ // the wrong offset. Skip the write.
3640
+ if (row.source === 'compact-boundary')
3641
+ return;
1915
3642
  const kind = row.source === 'operator' ? 'user'
1916
3643
  : row.source === 'persona' ? 'persona'
1917
3644
  : 'system';
@@ -1939,7 +3666,7 @@ export class ReplSession {
1939
3666
  });
1940
3667
  }
1941
3668
  /**
1942
- * Restore a transcript from a stored event log - α6.4. Called by
3669
+ * Restore a transcript from a stored event log - . Called by
1943
3670
  * the CLI bootstrap when the operator runs `pugi resume <id>` or
1944
3671
  * picks an entry from the `/resume` picker. Replays each event into
1945
3672
  * the local transcript WITHOUT writing back to the store so the
@@ -1952,12 +3679,30 @@ export class ReplSession {
1952
3679
  * write the restored events.
1953
3680
  */
1954
3681
  restoreTranscript(events) {
3682
+ // apply compact-boundary masking BEFORE the
3683
+ // row conversion. Events strictly before the latest marker are
3684
+ // condensed into the boundary's `keptTailTurns + marker` slice so
3685
+ // the post-resume transcript starts at the most-recent context
3686
+ // floor rather than re-playing the full pre-compaction history.
3687
+ //
3688
+ // then apply rewind-marker masking. Any
3689
+ // event inside an active rewind range is stripped from the
3690
+ // visible transcript; the on-disk events stay durable so a
3691
+ // follow-up `pugi sessions undo-rewind` can restore them.
3692
+ const masked = applyRewindMask(applyCompactMask(events));
1955
3693
  const rows = [];
1956
- for (const event of events) {
3694
+ for (const event of masked) {
1957
3695
  const row = eventToTranscriptRow(event);
1958
3696
  if (row)
1959
3697
  rows.push(row);
1960
3698
  }
3699
+ // L29 : tag each compact-boundary row with the count of
3700
+ // operator + persona turns that landed AFTER it in the replay
3701
+ // window. The banner reads `turnsAgo` to render the "N turns ago"
3702
+ // suffix so a long session that resumes across multiple compactions
3703
+ // stays self-orienting. System rows + sibling boundaries are NOT
3704
+ // counted — they are chrome, not operator-visible turns.
3705
+ annotateBoundaryTurnsAgo(rows);
1961
3706
  // Cap at MAX_TRANSCRIPT_ROWS - the same cap appendRow uses so the
1962
3707
  // window math stays consistent post-restore.
1963
3708
  const capped = rows.slice(-MAX_TRANSCRIPT_ROWS);
@@ -1971,7 +3716,7 @@ export class ReplSession {
1971
3716
  getLocalSessionId() {
1972
3717
  return this.localSessionId;
1973
3718
  }
1974
- /* ------------- α6.3 buffered tag detection -------------- */
3719
+ /* ------------- buffered tag detection -------------- */
1975
3720
  /**
1976
3721
  * Scan the running `agent.step.detail` buffer for `<pugi-ask>` /
1977
3722
  * `<pugi-plan-review>` envelopes. If a complete envelope is found,
@@ -2076,7 +3821,7 @@ export class ReplSession {
2076
3821
  }
2077
3822
  }
2078
3823
  /* ------------------------------------------------------------------ */
2079
- /* Helpers */
3824
+ /* Helpers */
2080
3825
  /* ------------------------------------------------------------------ */
2081
3826
  /**
2082
3827
  * Resolve role → display name without throwing on unknown roles. The
@@ -2092,9 +3837,9 @@ export class ReplSession {
2092
3837
  * tool stream rows, not transcript rows). The shape mirrors the
2093
3838
  * `persistRow` mapping in reverse:
2094
3839
  *
2095
- * 'user' -> operator (brief)
2096
- * 'persona' -> persona (text + personaSlug)
2097
- * 'system' -> system (text)
3840
+ * 'user' -> operator (brief)
3841
+ * 'persona' -> persona (text + personaSlug)
3842
+ * 'system' -> system (text)
2098
3843
  *
2099
3844
  * Exported indirectly via `restoreTranscript`.
2100
3845
  */
@@ -2141,13 +3886,76 @@ function eventToTranscriptRow(event) {
2141
3886
  timestampEpochMs: event.t,
2142
3887
  };
2143
3888
  }
3889
+ if (event.kind === 'compaction') {
3890
+ // L8 + L29 : render the marker as a structured
3891
+ // `compact-boundary` row so the renderer can route it to the
3892
+ // dedicated <CompactBanner /> Ink component. The full summary text
3893
+ // is intentionally NOT inlined here (a 2k-token summary in the
3894
+ // transcript would defeat the purpose of compacting); the operator
3895
+ // sees the "context compacted" banner and can run `/context` to
3896
+ // inspect the marker payload when they want the details. The plain
3897
+ // text fallback stays in place for non-Ink consumers (snapshot
3898
+ // tests, future JSON exports).
3899
+ const compactionPayload = (event.payload ?? null);
3900
+ const trigger = compactionPayload?.trigger === 'auto' ? 'auto' : 'manual';
3901
+ const turns = typeof compactionPayload?.summaryTurnsBefore === 'number'
3902
+ ? compactionPayload.summaryTurnsBefore
3903
+ : 0;
3904
+ const tokens = typeof compactionPayload?.summaryTokenCount === 'number'
3905
+ ? compactionPayload.summaryTokenCount
3906
+ : undefined;
3907
+ return {
3908
+ id: randomUUID(),
3909
+ source: 'compact-boundary',
3910
+ text: `─── context compacted (${turns} turns → 1 summary, ${trigger}) ───`,
3911
+ compaction: {
3912
+ turnsBefore: turns,
3913
+ trigger,
3914
+ summaryTokenCount: tokens,
3915
+ },
3916
+ timestampEpochMs: event.t,
3917
+ };
3918
+ }
2144
3919
  return null;
2145
3920
  }
3921
+ /**
3922
+ * L29 : walk a chronological transcript window and stamp
3923
+ * every `compact-boundary` row's `compaction.turnsAgo` with the count of
3924
+ * operator + persona rows that land AFTER it. The annotation runs in
3925
+ * place on the array — boundaries earlier in time get larger `turnsAgo`
3926
+ * values, the boundary at the head of the window gets zero. System rows
3927
+ * and sibling boundaries are excluded from the count (they are chrome,
3928
+ * not operator-visible turns).
3929
+ *
3930
+ * Exported so a future spec can lock the contract and so the in-REPL
3931
+ * `/compact` path can reuse the same counter on live appends if it ever
3932
+ * needs to. Pure function (mutates only the input slice).
3933
+ */
3934
+ export function annotateBoundaryTurnsAgo(rows) {
3935
+ let trailingTurns = 0;
3936
+ for (let i = rows.length - 1; i >= 0; i -= 1) {
3937
+ const row = rows[i];
3938
+ if (row.source === 'operator' || row.source === 'persona') {
3939
+ trailingTurns += 1;
3940
+ continue;
3941
+ }
3942
+ if (row.source === 'compact-boundary') {
3943
+ // Re-assign with the live `turnsAgo`. Carry forward the existing
3944
+ // structured payload so we never lose the trigger / token-count
3945
+ // data the renderer needs.
3946
+ const compaction = row.compaction ?? { turnsBefore: 0, trigger: 'manual' };
3947
+ rows[i] = {
3948
+ ...row,
3949
+ compaction: { ...compaction, turnsAgo: trailingTurns },
3950
+ };
3951
+ }
3952
+ }
3953
+ }
2146
3954
  /**
2147
3955
  * Heuristic: does this text contain Markdown structures that benefit
2148
3956
  * from atomic grouping? Code fences, bullet lists, numbered lists,
2149
3957
  * headings - anything where per-line splitting would fragment visual
2150
- * grouping (Claude triple-review P1 PR #369).
3958
+ * grouping (Claude triple-review P1 PR).
2151
3959
  */
2152
3960
  function looksLikeMarkdown(text) {
2153
3961
  if (text.includes('```'))
@@ -2180,10 +3988,10 @@ function safePersonaName(role) {
2180
3988
  * Render a millisecond delta as a compact human-readable age. Used by
2181
3989
  * `/context` to surface the oldest working-set entry's age:
2182
3990
  *
2183
- * < 60s -> `45s`
2184
- * < 1h -> `4m`
2185
- * < 24h -> `2h`
2186
- * >= 24h -> `3d`
3991
+ * < 60s -> `45s`
3992
+ * < 1h -> `4m`
3993
+ * < 24h -> `2h`
3994
+ * >= 24h -> `3d`
2187
3995
  *
2188
3996
  * Negative deltas (clock skew) clamp to `0s`.
2189
3997
  */
@@ -2209,23 +4017,103 @@ function formatAgeSeconds(deltaMs) {
2209
4017
  export function knownRoles() {
2210
4018
  return listRoles();
2211
4019
  }
4020
+ /**
4021
+ * cost-meter sprint — render a session-elapsed ms delta as the
4022
+ * status-row's compact `XmYs` / `XhYm` shape. Distinct from
4023
+ * `formatAgeSeconds` above because `/cost` needs minute-granularity
4024
+ * uniformly (operator wants `2m44s`, not `2m`). Pure / branch-cheap;
4025
+ * the TUI status row + `/cost` both call this on every render.
4026
+ */
4027
+ function formatElapsedShort(elapsedMs) {
4028
+ if (!Number.isFinite(elapsedMs) || elapsedMs <= 0)
4029
+ return '0s';
4030
+ const totalSec = Math.floor(elapsedMs / 1000);
4031
+ if (totalSec < 60)
4032
+ return `${totalSec}s`;
4033
+ const min = Math.floor(totalSec / 60);
4034
+ const sec = totalSec % 60;
4035
+ if (min < 60)
4036
+ return `${min}m${sec.toString().padStart(2, '0')}s`;
4037
+ const hr = Math.floor(min / 60);
4038
+ const restMin = min % 60;
4039
+ return `${hr}h${restMin.toString().padStart(2, '0')}m`;
4040
+ }
4041
+ /**
4042
+ * cost-meter sprint — public-facing tier labels for the `/quota`
4043
+ * slash. Mirrors `TIER_PRICE_LABEL` in `runtime/cli.ts` (kept in sync
4044
+ * via `pricing.spec.ts` gate). Falls through to the raw slug when an
4045
+ * unknown tier ships from a forward-compat admin-api build.
4046
+ */
4047
+ const QUOTA_TIER_LABELS = Object.freeze({
4048
+ free: 'Free',
4049
+ founder: 'Founder ($20/mo)',
4050
+ builder: 'Builder ($99/mo)',
4051
+ team: 'Team ($199/mo)',
4052
+ });
4053
+ /**
4054
+ * cost-meter sprint — render the time-until-reset window for the
4055
+ * `/quota` plan line. `resetAt` is the ISO string admin-api returns;
4056
+ * `now` is the current epoch ms (injected for test determinism). Falls
4057
+ * back to the raw ISO string when parsing fails so the operator never
4058
+ * sees an empty hint.
4059
+ */
4060
+ function formatResetWindow(resetAtIso, nowEpochMs) {
4061
+ const resetMs = Date.parse(resetAtIso);
4062
+ if (!Number.isFinite(resetMs))
4063
+ return resetAtIso;
4064
+ const deltaMs = resetMs - nowEpochMs;
4065
+ if (deltaMs <= 0)
4066
+ return 'now';
4067
+ const days = Math.floor(deltaMs / (24 * 60 * 60 * 1000));
4068
+ if (days >= 2)
4069
+ return `in ${days}d`;
4070
+ const hours = Math.floor(deltaMs / (60 * 60 * 1000));
4071
+ if (hours >= 1)
4072
+ return `in ${hours}h`;
4073
+ const minutes = Math.max(1, Math.floor(deltaMs / (60 * 1000)));
4074
+ return `in ${minutes}m`;
4075
+ }
4076
+ /**
4077
+ * cleanup : wrap a `/quota` counter row in ANSI
4078
+ * color codes by utilisation percent. Thresholds match the upstream tool's
4079
+ * tier-meter convention so operators trained on that surface read the
4080
+ * same signal here:
4081
+ *
4082
+ * - 0..70% → green (32m) — comfortable headroom
4083
+ * - 70..90% → yellow (33m) — approaching cap, plan ahead
4084
+ * - 90..100% → red (31m) — burn rate alarm, throttle now
4085
+ *
4086
+ * The wrap is whole-row (not just the percent) so the eye registers
4087
+ * the level on the line, not just the trailing parenthesis. Tests
4088
+ * that match the inner row text via regex are unaffected because the
4089
+ * regex anchors live inside the wrapped substring; the ANSI codes
4090
+ * sit at the boundaries.
4091
+ */
4092
+ export function colorizeQuotaRow(row, pct) {
4093
+ const RESET = '\x1b[0m';
4094
+ if (pct >= 90)
4095
+ return `\x1b[31m${row}${RESET}`;
4096
+ if (pct >= 70)
4097
+ return `\x1b[33m${row}${RESET}`;
4098
+ return `\x1b[32m${row}${RESET}`;
4099
+ }
2212
4100
  /* ------------------------------------------------------------------ */
2213
- /* Tool call synthesiser - α6.12 */
4101
+ /* Tool call synthesiser - */
2214
4102
  /* ------------------------------------------------------------------ */
2215
4103
  /**
2216
4104
  * Match canonical tool invocation grammar in an `agent.step.detail`
2217
4105
  * string and emit a synthesised `ToolCallEntry`. Returns null when no
2218
4106
  * known tool pattern matches.
2219
4107
  *
2220
- * The grammar mirrors the way Claude Code, Codex CLI, and Gemini CLI
4108
+ * The grammar mirrors the way the upstream tool, peer CLI, and Gemini CLI
2221
4109
  * display tool calls in their tool stream panes:
2222
4110
  *
2223
- * Read(path)
2224
- * Edit(path[:lines])
2225
- * Bash(command)
2226
- * Grep("pattern" [in path])
2227
- * Glob(pattern)
2228
- * WebFetch(url)
4111
+ * Read(path)
4112
+ * Edit(path[:lines])
4113
+ * Bash(command)
4114
+ * Grep("pattern" [in path])
4115
+ * Glob(pattern)
4116
+ * WebFetch(url)
2229
4117
  *
2230
4118
  * The matcher is case-insensitive on the tool name so a persona that
2231
4119
  * spells the tool as `READ(...)` or `web_fetch(...)` still lands in
@@ -2240,9 +4128,9 @@ export function synthesiseToolCall(input) {
2240
4128
  if (detail.length === 0)
2241
4129
  return null;
2242
4130
  // Pattern: ToolName(args) optionally suffixed with a result hint.
2243
- // We allow the canonical Claude Code casing AND the snake_case
4131
+ // We allow the canonical the upstream tool casing AND the snake_case
2244
4132
  // alias `web_fetch` so the synthesiser matches what personas write.
2245
- const match = /^(Read|Edit|Bash|Grep|Glob|WebFetch|web_fetch)\s*\(\s*([^)]*)\s*\)\s*(.*)$/i
4133
+ const match = /^(Read|Write|Edit|Bash|Grep|Glob|WebFetch|web_fetch)\s*\(\s*([^)]*)\s*\)\s*(.*)$/i
2246
4134
  .exec(detail);
2247
4135
  if (!match)
2248
4136
  return null;
@@ -2260,12 +4148,32 @@ export function synthesiseToolCall(input) {
2260
4148
  startedAtEpochMs: input.now,
2261
4149
  };
2262
4150
  }
4151
+ /**
4152
+ * small-CC-parity batch : collapse a multi-line
4153
+ * result preview down to a single-line head capped at `max` chars. The
4154
+ * collapsed-result row on a completed tool call uses this so the
4155
+ * preview never expands the row vertically. Exported для the spec so
4156
+ * the truncation behaviour is locked down.
4157
+ */
4158
+ export function truncatePreview(value, max) {
4159
+ if (!value)
4160
+ return '';
4161
+ // Strip CR/LF + tab so the preview stays single-line. Multiple
4162
+ // whitespace runs collapse to single space — operator wants signal,
4163
+ // not formatting noise.
4164
+ const single = value.replace(/[\r\n\t]+/g, ' ').replace(/\s{2,}/g, ' ').trim();
4165
+ if (single.length <= max)
4166
+ return single;
4167
+ return `${single.slice(0, Math.max(0, max - 1))}…`;
4168
+ }
2263
4169
  function normaliseToolName(raw) {
2264
4170
  const lower = raw.toLowerCase();
2265
4171
  if (lower === 'webfetch' || lower === 'web_fetch')
2266
4172
  return 'web_fetch';
2267
4173
  if (lower === 'read')
2268
4174
  return 'read';
4175
+ if (lower === 'write')
4176
+ return 'write';
2269
4177
  if (lower === 'edit')
2270
4178
  return 'edit';
2271
4179
  if (lower === 'bash')
@@ -2296,12 +4204,12 @@ function parseStatusFromTail(tail) {
2296
4204
  return { status: 'ok', detail: tail };
2297
4205
  }
2298
4206
  /* ------------------------------------------------------------------ */
2299
- /* α6.3 office-hours encoders */
2300
- /* */
2301
- /* Mirrors `tui/ask-modal.tsx#encodeAskVerdict` so the session can */
2302
- /* synthesise the operator-side echo without dragging an Ink module */
2303
- /* into the test surface. The two encoders MUST agree byte-for-byte - */
2304
- /* a divergence would silently mis-prefix the persona's follow-up. */
4207
+ /* office-hours encoders */
4208
+ /* */
4209
+ /* Mirrors `tui/ask-modal.tsx#encodeAskVerdict` so the session can */
4210
+ /* synthesise the operator-side echo without dragging an Ink module */
4211
+ /* into the test surface. The two encoders MUST agree byte-for-byte - */
4212
+ /* a divergence would silently mis-prefix the persona's follow-up. */
2305
4213
  /* ------------------------------------------------------------------ */
2306
4214
  function encodeAskVerdictLocal(verdict) {
2307
4215
  if (verdict.cancelled)
@@ -2327,7 +4235,7 @@ function encodeAskVerdictLocal(verdict) {
2327
4235
  * `[ASK-RESPONSE:other] [ASK-RESPONSE:vercel] my real answer` which
2328
4236
  * a prefix-greedy persona could read as "operator chose vercel".
2329
4237
  *
2330
- * Claude triple-review P1 (PR #375).
4238
+ * Claude triple-review P1 (PR).
2331
4239
  */
2332
4240
  function sanitiseVerdictText(raw) {
2333
4241
  let cleaned = raw;
@@ -2385,7 +4293,7 @@ function encodePlanReviewVerdictLocal(result) {
2385
4293
  }
2386
4294
  /**
2387
4295
  * Compose the human-readable transcript line that records the
2388
- * operator's ask verdict. Mirrors Codex CLI's "you chose: <label>"
4296
+ * operator's ask verdict. Mirrors peer CLI's "you chose: <label>"
2389
4297
  * pattern so the conversation reads linearly.
2390
4298
  */
2391
4299
  function humanLabelForVerdict(tag, verdict) {
@@ -2433,7 +4341,7 @@ export function synthesiseLocalAskTag(question) {
2433
4341
  // Use the single-source signature helper so a persona-emitted ask
2434
4342
  // with the same question + same option values does not collide with
2435
4343
  // this synthesised one under a divergent algorithm. Claude
2436
- // triple-review P1 (PR #375).
4344
+ // triple-review P1 (PR).
2437
4345
  const signature = signatureForAsk(trimmed, options);
2438
4346
  return {
2439
4347
  question: trimmed,
@@ -2450,20 +4358,20 @@ export function synthesiseLocalAskTag(question) {
2450
4358
  * production callers go through `appendPersonaLine`.
2451
4359
  *
2452
4360
  * Examples (display name = "Pugi"):
2453
- * "Pugi, координатор Pugi. Брифую..." -> "координатор Pugi. Брифую..."
2454
- * "Pugi: вот результат" -> "вот результат"
2455
- * "<workspace-context-abc>Pugi, привет" -> "привет"
2456
- * "обычный ответ без префикса" -> "обычный ответ без префикса"
4361
+ * "Pugi, координатор Pugi. Брифую..." -> "координатор Pugi. Брифую..."
4362
+ * "Pugi: вот результат" -> "вот результат"
4363
+ * "<workspace-context-abc>Pugi, привет" -> "привет"
4364
+ * "обычный ответ без префикса" -> "обычный ответ без префикса"
2457
4365
  *
2458
4366
  * The strip is conservative - we only remove the display name when it
2459
4367
  * is followed by a separator (comma, colon, dash, space) so a sentence
2460
4368
  * that legitimately contains the name mid-text ("спроси у Pugi") is
2461
- * not mangled. (α6.14.2 wave 5 - CEO dogfood fix.)
4369
+ * not mangled.
2462
4370
  */
2463
4371
  export function stripPersonaPrefixEcho(personaSlug, text) {
2464
4372
  let working = text.trimStart();
2465
4373
  // Drop any leaked `<workspace-context-...>` / `</workspace-context-...>`
2466
- // wrapper at the head. The Mira prompt v1.1 sometimes echoes the
4374
+ // wrapper at the head. The Pugi prompt v1.1 sometimes echoes the
2467
4375
  // scaffolding envelope back when the model is warm-starting the
2468
4376
  // first turn; cosmetic noise the operator never needs to see.
2469
4377
  // We strip both opening tag and any text up to (and including) the
@@ -2491,7 +4399,22 @@ export function stripPersonaPrefixEcho(personaSlug, text) {
2491
4399
  // Escape regex specials in the display name even though THE_TEN
2492
4400
  // names are alpha-only today (forward-defense).
2493
4401
  const escaped = display.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
4402
+ // Match `<DisplayName>` (case-insensitive) followed by EITHER:
4403
+ // - an end-of-string, OR
4404
+ // - a separator (whitespace / comma / colon / dash / period+space).
4405
+ // The `i` flag is needed so a model writing "PUGI:" or "pugi," still
4406
+ // strips. After this match the post-fix `noSepUppercaseRe` handles
4407
+ // the "PugiПринял" / "PugiHello" no-separator emission pattern
4408
+ // (CEO red-alert) using a SEPARATE regex without the `i`
4409
+ // flag so the lookahead is case-strict (Pugineous must NOT strip).
2494
4410
  const re = new RegExp(`^${escaped}(?:[\\s,:;\\-—–]+|$)`, 'i');
4411
+ // No-separator case-strict matcher. Display name in either of its
4412
+ // canonical casings ("Pugi" / "PUGI") immediately followed by an
4413
+ // uppercase Cyrillic or Latin letter. The strip is intentionally
4414
+ // narrower than the case-insensitive `re` above because a lowercase
4415
+ // continuation ("Pugineous") is a single word, not a display-name
4416
+ // echo - we must not eat real content.
4417
+ const noSepUppercaseRe = new RegExp(`^(?:${escaped}|${escaped.toUpperCase()})(?=[А-ЯЁA-Z])`);
2495
4418
  // Loop the strip so cascading echoes ("Pugi Pugi Pugi, координатор ...")
2496
4419
  // collapse to a single name. The model occasionally emits the display
2497
4420
  // name two or three times back-to-back when the pane prefix also
@@ -2503,10 +4426,18 @@ export function stripPersonaPrefixEcho(personaSlug, text) {
2503
4426
  // matches an empty string (defence-in-depth even though the current
2504
4427
  // pattern guarantees at least one consumed char).
2505
4428
  for (let i = 0; i < 3; i += 1) {
2506
- const m = re.exec(working);
2507
- if (!m || m[0].length === 0)
2508
- break;
2509
- working = working.slice(m[0].length).trimStart();
4429
+ let m = re.exec(working);
4430
+ if (m && m[0].length > 0) {
4431
+ working = working.slice(m[0].length).trimStart();
4432
+ continue;
4433
+ }
4434
+ // Fallback: no-separator match for "PugiПринял" / "PugiHello" shape.
4435
+ m = noSepUppercaseRe.exec(working);
4436
+ if (m && m[0].length > 0) {
4437
+ working = working.slice(m[0].length);
4438
+ continue;
4439
+ }
4440
+ break;
2510
4441
  }
2511
4442
  return working;
2512
4443
  }