@pugi/cli 0.1.0-beta.3 → 0.1.0-beta.31

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 (219) hide show
  1. package/THIRD_PARTY_NOTICES.md +40 -0
  2. package/assets/pugi-mascot.ansi +15 -40
  3. package/bin/run.js +33 -1
  4. package/dist/commands/jobs-watch.js +201 -0
  5. package/dist/commands/jobs.js +15 -0
  6. package/dist/core/agent-progress/cleanup.js +134 -0
  7. package/dist/core/agent-progress/schema.js +144 -0
  8. package/dist/core/agent-progress/writer.js +101 -0
  9. package/dist/core/artifact-chain/dispatcher.js +148 -0
  10. package/dist/core/artifact-chain/exporter.js +164 -0
  11. package/dist/core/artifact-chain/state.js +243 -0
  12. package/dist/core/artifact-chain/steps.js +169 -0
  13. package/dist/core/auth/env-provider.js +238 -0
  14. package/dist/core/auto-update/channels.js +122 -0
  15. package/dist/core/auto-update/checker.js +241 -0
  16. package/dist/core/auto-update/state.js +235 -0
  17. package/dist/core/bare-mode/index.js +107 -0
  18. package/dist/core/checkpoint/resumer.js +149 -0
  19. package/dist/core/checkpoint/rewinder.js +291 -0
  20. package/dist/core/compact/auto-trigger.js +96 -0
  21. package/dist/core/compact/buffer-rewriter.js +115 -0
  22. package/dist/core/compact/summarizer.js +208 -0
  23. package/dist/core/compact/token-counter.js +108 -0
  24. package/dist/core/consensus/diff-capture.js +73 -0
  25. package/dist/core/context/index.js +7 -0
  26. package/dist/core/context/markdown-traverse.js +255 -0
  27. package/dist/core/cost/rate-card.js +129 -0
  28. package/dist/core/cost/tracker.js +221 -0
  29. package/dist/core/denial-tracking/index.js +8 -0
  30. package/dist/core/denial-tracking/state.js +264 -0
  31. package/dist/core/diagnostics/probe-runner.js +93 -0
  32. package/dist/core/diagnostics/probes/api.js +46 -0
  33. package/dist/core/diagnostics/probes/auth.js +86 -0
  34. package/dist/core/diagnostics/probes/bare-mode.js +42 -0
  35. package/dist/core/diagnostics/probes/cli-version.js +127 -0
  36. package/dist/core/diagnostics/probes/config.js +72 -0
  37. package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
  38. package/dist/core/diagnostics/probes/disk.js +81 -0
  39. package/dist/core/diagnostics/probes/git.js +65 -0
  40. package/dist/core/diagnostics/probes/mcp.js +75 -0
  41. package/dist/core/diagnostics/probes/node.js +59 -0
  42. package/dist/core/diagnostics/probes/pnpm.js +36 -0
  43. package/dist/core/diagnostics/probes/pugi-md.js +89 -0
  44. package/dist/core/diagnostics/probes/session.js +74 -0
  45. package/dist/core/diagnostics/probes/status-snapshot.js +442 -0
  46. package/dist/core/diagnostics/probes/workspace.js +63 -0
  47. package/dist/core/diagnostics/types.js +70 -0
  48. package/dist/core/dispatch/cache-cleanup.js +197 -0
  49. package/dist/core/dispatch/cache-handoff.js +295 -0
  50. package/dist/core/edits/dispatch.js +218 -2
  51. package/dist/core/edits/journal.js +199 -0
  52. package/dist/core/edits/layer-d-ast.js +557 -14
  53. package/dist/core/edits/verify-hook.js +273 -0
  54. package/dist/core/edits/worktree.js +111 -18
  55. package/dist/core/engine/anvil-client.js +115 -5
  56. package/dist/core/engine/budgets.js +89 -0
  57. package/dist/core/engine/context-prefix.js +155 -0
  58. package/dist/core/engine/intent.js +260 -0
  59. package/dist/core/engine/native-pugi.js +852 -210
  60. package/dist/core/engine/prompts.js +89 -6
  61. package/dist/core/engine/strip-internal-fields.js +124 -0
  62. package/dist/core/engine/tool-bridge.js +972 -33
  63. package/dist/core/feedback/queue.js +177 -0
  64. package/dist/core/feedback/submitter.js +145 -0
  65. package/dist/core/file-cache.js +113 -1
  66. package/dist/core/hooks/events.js +44 -0
  67. package/dist/core/hooks/index.js +15 -0
  68. package/dist/core/hooks/registry.js +213 -0
  69. package/dist/core/hooks/runner.js +236 -0
  70. package/dist/core/init/scaffold.js +195 -0
  71. package/dist/core/lsp/cache.js +105 -0
  72. package/dist/core/lsp/client.js +174 -29
  73. package/dist/core/lsp/language-detect.js +66 -0
  74. package/dist/core/lsp/post-edit-diagnostics.js +171 -0
  75. package/dist/core/mcp/client.js +75 -6
  76. package/dist/core/mcp/http-server.js +553 -0
  77. package/dist/core/mcp/permission.js +190 -0
  78. package/dist/core/mcp/registry.js +24 -2
  79. package/dist/core/mcp/server-tools.js +219 -0
  80. package/dist/core/mcp/server.js +397 -0
  81. package/dist/core/memory/dual-write.js +416 -0
  82. package/dist/core/memory/dual-write.spec.js +297 -0
  83. package/dist/core/memory/phase1-kinds.js +20 -0
  84. package/dist/core/memory-sync/queue.js +158 -0
  85. package/dist/core/memory-sync/queue.spec.js +105 -0
  86. package/dist/core/onboarding/marker.js +111 -0
  87. package/dist/core/onboarding/telemetry-state.js +108 -0
  88. package/dist/core/output-style/presets.js +176 -0
  89. package/dist/core/output-style/state.js +185 -0
  90. package/dist/core/permissions/gate.js +187 -0
  91. package/dist/core/permissions/index.js +18 -0
  92. package/dist/core/permissions/mode.js +102 -0
  93. package/dist/core/permissions/state.js +215 -0
  94. package/dist/core/permissions/tool-class.js +93 -0
  95. package/dist/core/prd-check/parser.js +215 -0
  96. package/dist/core/prd-check/reporter.js +127 -0
  97. package/dist/core/prd-check/session-review.js +557 -0
  98. package/dist/core/prd-check/verifiers.js +223 -0
  99. package/dist/core/pugi-md/context-injector.js +76 -0
  100. package/dist/core/pugi-md/walk-up.js +207 -0
  101. package/dist/core/release-notes/parser.js +241 -0
  102. package/dist/core/release-notes/state.js +116 -0
  103. package/dist/core/repl/codebase-survey.js +308 -0
  104. package/dist/core/repl/history.js +11 -1
  105. package/dist/core/repl/init-interview.js +457 -0
  106. package/dist/core/repl/model-pricing.js +135 -0
  107. package/dist/core/repl/onboarding-state.js +297 -0
  108. package/dist/core/repl/session.js +1529 -30
  109. package/dist/core/repl/slash-commands.js +361 -13
  110. package/dist/core/repl/store/session-store.js +31 -2
  111. package/dist/core/repl/workspace-context.js +22 -0
  112. package/dist/core/repo-map/build.js +125 -0
  113. package/dist/core/repo-map/cache.js +185 -0
  114. package/dist/core/repo-map/extractor.js +254 -0
  115. package/dist/core/repo-map/formatter.js +145 -0
  116. package/dist/core/repo-map/scanner.js +211 -0
  117. package/dist/core/retry-budget/budget.js +284 -0
  118. package/dist/core/retry-budget/index.js +5 -0
  119. package/dist/core/session.js +44 -0
  120. package/dist/core/settings.js +80 -0
  121. package/dist/core/share/formatter.js +271 -0
  122. package/dist/core/share/redactor.js +221 -0
  123. package/dist/core/share/uploader.js +267 -0
  124. package/dist/core/skills/defaults.js +457 -0
  125. package/dist/core/subagents/dispatcher-real.js +600 -0
  126. package/dist/core/subagents/dispatcher.js +113 -24
  127. package/dist/core/subagents/index.js +18 -5
  128. package/dist/core/subagents/isolation-matrix.js +213 -0
  129. package/dist/core/subagents/spawn.js +19 -4
  130. package/dist/core/telemetry/emitter.js +229 -0
  131. package/dist/core/telemetry/queue.js +251 -0
  132. package/dist/core/theme/context.js +91 -0
  133. package/dist/core/theme/presets.js +228 -0
  134. package/dist/core/theme/state.js +181 -0
  135. package/dist/core/todos/invariant.js +10 -0
  136. package/dist/core/todos/state.js +177 -0
  137. package/dist/core/transport/version-interceptor.js +166 -0
  138. package/dist/core/vim/keymap.js +288 -0
  139. package/dist/core/vim/state.js +92 -0
  140. package/dist/index.js +28 -0
  141. package/dist/runtime/bootstrap.js +190 -0
  142. package/dist/runtime/cli.js +2603 -278
  143. package/dist/runtime/commands/chain.js +489 -0
  144. package/dist/runtime/commands/compact.js +297 -0
  145. package/dist/runtime/commands/cost.js +199 -0
  146. package/dist/runtime/commands/delegate.js +312 -0
  147. package/dist/runtime/commands/dispatch.js +126 -0
  148. package/dist/runtime/commands/doctor.js +390 -0
  149. package/dist/runtime/commands/feedback.js +184 -0
  150. package/dist/runtime/commands/hooks.js +184 -0
  151. package/dist/runtime/commands/lsp.js +212 -28
  152. package/dist/runtime/commands/mcp.js +824 -0
  153. package/dist/runtime/commands/memory.js +508 -0
  154. package/dist/runtime/commands/memory.spec.js +174 -0
  155. package/dist/runtime/commands/model.js +237 -0
  156. package/dist/runtime/commands/onboarding.js +275 -0
  157. package/dist/runtime/commands/patch.js +17 -0
  158. package/dist/runtime/commands/permissions.js +87 -0
  159. package/dist/runtime/commands/plan.js +143 -0
  160. package/dist/runtime/commands/prd-check.js +285 -0
  161. package/dist/runtime/commands/release-notes.js +229 -0
  162. package/dist/runtime/commands/repo-map.js +95 -0
  163. package/dist/runtime/commands/report.js +299 -0
  164. package/dist/runtime/commands/resume.js +118 -0
  165. package/dist/runtime/commands/review-consensus.js +17 -2
  166. package/dist/runtime/commands/rewind.js +333 -0
  167. package/dist/runtime/commands/roster.js +117 -0
  168. package/dist/runtime/commands/sessions.js +163 -0
  169. package/dist/runtime/commands/share.js +316 -0
  170. package/dist/runtime/commands/status.js +178 -0
  171. package/dist/runtime/commands/stickers.js +82 -0
  172. package/dist/runtime/commands/style.js +194 -0
  173. package/dist/runtime/commands/theme.js +196 -0
  174. package/dist/runtime/commands/update.js +289 -0
  175. package/dist/runtime/commands/vim.js +140 -0
  176. package/dist/runtime/commands/worktree.js +50 -6
  177. package/dist/runtime/headless.js +543 -0
  178. package/dist/runtime/load-hooks-or-exit.js +71 -0
  179. package/dist/runtime/plan-decompose.js +531 -0
  180. package/dist/runtime/version.js +65 -0
  181. package/dist/tools/agent-tool.js +229 -0
  182. package/dist/tools/apply-patch.js +281 -39
  183. package/dist/tools/ask-user-question.js +213 -0
  184. package/dist/tools/ask-user.js +115 -0
  185. package/dist/tools/file-tools.js +85 -14
  186. package/dist/tools/mcp-tool.js +260 -0
  187. package/dist/tools/multi-edit.js +361 -0
  188. package/dist/tools/registry.js +30 -2
  189. package/dist/tools/skill-tool.js +96 -0
  190. package/dist/tools/tasks.js +208 -0
  191. package/dist/tools/todo-write.js +184 -0
  192. package/dist/tools/web-fetch.js +147 -2
  193. package/dist/tools/web-search.js +458 -0
  194. package/dist/tui/agent-progress-card.js +111 -0
  195. package/dist/tui/agent-tree.js +10 -0
  196. package/dist/tui/ask-modal.js +2 -2
  197. package/dist/tui/ask-user-question-prompt.js +192 -0
  198. package/dist/tui/compact-banner.js +81 -0
  199. package/dist/tui/conversation-pane.js +82 -8
  200. package/dist/tui/cost-table.js +111 -0
  201. package/dist/tui/doctor-table.js +46 -0
  202. package/dist/tui/feedback-prompt.js +156 -0
  203. package/dist/tui/input-box.js +46 -2
  204. package/dist/tui/markdown-render.js +4 -4
  205. package/dist/tui/onboarding-wizard.js +240 -0
  206. package/dist/tui/repl-render.js +293 -35
  207. package/dist/tui/repl-splash.js +2 -2
  208. package/dist/tui/repl.js +45 -13
  209. package/dist/tui/splash.js +1 -1
  210. package/dist/tui/status-bar.js +94 -16
  211. package/dist/tui/status-table.js +7 -0
  212. package/dist/tui/stickers-art.js +136 -0
  213. package/dist/tui/style-table.js +28 -0
  214. package/dist/tui/theme-table.js +29 -0
  215. package/dist/tui/tool-stream-pane.js +7 -0
  216. package/dist/tui/update-banner.js +20 -2
  217. package/dist/tui/vim-input.js +267 -0
  218. package/docs/examples/codegraph.mcp.json +10 -0
  219. package/package.json +9 -6
@@ -27,6 +27,7 @@
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,11 +35,16 @@ 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;
44
50
  const MAX_RECONNECT_ATTEMPTS = 10;
@@ -315,6 +321,19 @@ export class ReplSession {
315
321
  toolCalls: [],
316
322
  transcript: [],
317
323
  tokensDownstreamTotal: 0,
324
+ // α7 cost-meter sprint — cost accumulators land at zero on boot.
325
+ // `sessionStartedAtEpochMs` is set at construction time (vs the
326
+ // server-side `agent.session.opened` event) so the elapsed slot
327
+ // on the status row starts ticking the moment the REPL mounts.
328
+ sessionTokensIn: 0,
329
+ sessionTokensOut: 0,
330
+ sessionCostUsd: 0,
331
+ sessionStartedAtEpochMs: this.now(),
332
+ recentTurns: [],
333
+ turnTokensIn: 0,
334
+ turnTokensOut: 0,
335
+ turnCostUsd: 0,
336
+ lastTurnDelta: null,
318
337
  briefStartedAtEpochMs: undefined,
319
338
  pendingAsk: null,
320
339
  pendingAskSource: null,
@@ -322,6 +341,7 @@ export class ReplSession {
322
341
  pendingPlanReviewSource: null,
323
342
  dispatchState: 'idle',
324
343
  dispatchToolLabel: null,
344
+ lastCompletedOutcome: null,
325
345
  };
326
346
  // α6.9: mirror every FSM transition into the public state so the
327
347
  // status-bar surface can rerender on the next frame. Local listener
@@ -359,6 +379,7 @@ export class ReplSession {
359
379
  apiUrl: this.options.apiUrl,
360
380
  apiKey: this.options.apiKey,
361
381
  workspace: this.options.workspace,
382
+ cyberZoo: this.options.cyberZoo,
362
383
  });
363
384
  this.patch({ sessionId, connection: 'connecting' });
364
385
  this.openStream();
@@ -371,6 +392,11 @@ export class ReplSession {
371
392
  // admin-api down) is silent - the operator can still type
372
393
  // `/privacy` to see the contract.
373
394
  void this.fetchAndAnnouncePrivacyMode().catch(() => undefined);
395
+ // Leak L21 (2026-05-27): silently drain any feedback envelopes
396
+ // that landed offline during a previous session. Best-effort —
397
+ // a failed flush leaves the queue intact for the next start.
398
+ // Never blocks bootstrap.
399
+ void this.flushFeedbackQueueOnBootstrap().catch(() => undefined);
374
400
  }
375
401
  catch (error) {
376
402
  this.appendSystemLine(`Could not open Pugi session: ${this.errorMessage(error)}`);
@@ -414,6 +440,21 @@ export class ReplSession {
414
440
  // Silent fail - offline / DNS / unauth all collapse to no banner.
415
441
  }
416
442
  }
443
+ /**
444
+ * Leak L21 (2026-05-27): on bootstrap, drain the local feedback
445
+ * queue silently. Operators who ran `pugi feedback` while offline
446
+ * see their envelopes flushed on the next online session without
447
+ * any extra command. The drain is best-effort and never blocks
448
+ * the REPL — a failed flush leaves the queue intact for the next
449
+ * bootstrap attempt.
450
+ */
451
+ async flushFeedbackQueueOnBootstrap() {
452
+ const { flushFeedbackQueueSilently } = await import('../../runtime/commands/feedback.js');
453
+ await flushFeedbackQueueSilently(process.cwd(), {
454
+ apiUrl: this.options.apiUrl,
455
+ apiKey: this.options.apiKey,
456
+ });
457
+ }
417
458
  /**
418
459
  * Tear down the SSE stream and stop the reconnect timer. The session
419
460
  * id stays valid server-side; `pugi resume <id>` reopens later.
@@ -539,6 +580,73 @@ export class ReplSession {
539
580
  getDispatchState() {
540
581
  return this.fsm.current;
541
582
  }
583
+ /**
584
+ * Wave 6 BT 8 (Claude Code parity): Esc-Esc walkback. Trim the last
585
+ * operator/persona turn pair from the in-memory transcript so the
586
+ * model's next call sees the conversation as if the most recent
587
+ * turn never happened. The local SessionStore still has the events
588
+ * on disk (append-only); the in-memory mask is advisory and the next
589
+ * `/compact` boundary will fold them naturally.
590
+ *
591
+ * Refusal modes:
592
+ * - `'no-turn'` - transcript has no operator/persona row to pop.
593
+ * - `'in-flight'` - dispatch is mid-flight; popping would race with
594
+ * the streaming persona row. The operator must
595
+ * cancel (Ctrl+C) before walking back.
596
+ *
597
+ * Success mode:
598
+ * - `'walked-back'` - the trailing persona row + the operator row
599
+ * that triggered it are gone from the transcript.
600
+ * A `↩ walked back 1 turn` status row is appended
601
+ * so the operator sees the state change without
602
+ * guessing.
603
+ *
604
+ * The mask is in-memory only on purpose. Disk-side rewind already has
605
+ * a separate first-class command (`/rewind`) with checkpoint
606
+ * semantics — the Esc-Esc shortcut is a one-tap "oops, undo that" for
607
+ * the live transcript, NOT a transactional rollback.
608
+ */
609
+ walkbackLastTurn() {
610
+ // Refuse while a dispatch is running. Popping the operator row that
611
+ // is currently driving the model's response would leave the persona
612
+ // line orphaned on the next streamed chunk; the FSM also lacks a
613
+ // clean teardown path here. The operator gets a one-line refusal
614
+ // and can Ctrl+C first if they really want to walk back.
615
+ const current = this.fsm.current;
616
+ if (current !== 'idle' && current !== 'completed'
617
+ && current !== 'aborted' && current !== 'failed') {
618
+ this.appendSystemLine('Walkback refused: dispatch in flight. Cancel with Ctrl+C, then Esc-Esc again.');
619
+ return 'in-flight';
620
+ }
621
+ // Find the trailing operator row. Walking backwards because the
622
+ // transcript is append-only and the most recent operator turn is
623
+ // by definition the last `source === 'operator'` row.
624
+ const transcript = this.state.transcript;
625
+ let operatorIdx = -1;
626
+ for (let i = transcript.length - 1; i >= 0; i -= 1) {
627
+ const row = transcript[i];
628
+ if (row.source === 'operator') {
629
+ operatorIdx = i;
630
+ break;
631
+ }
632
+ }
633
+ if (operatorIdx === -1) {
634
+ // No operator turn to pop. Quiet refusal — surfacing a "nothing
635
+ // to undo" line on every accidental double-Esc would be noisy.
636
+ return 'no-turn';
637
+ }
638
+ // Trim everything from the operator row onward (its echo + any
639
+ // persona/system rows that landed in response). The slice keeps
640
+ // every row BEFORE the operator turn, which is the conversation
641
+ // exactly as it stood right before the operator pressed Enter.
642
+ const trimmed = transcript.slice(0, operatorIdx);
643
+ this.patch({ transcript: trimmed });
644
+ // Status row so the operator sees the state change without
645
+ // guessing. Brand voice: single ASCII line, return-arrow glyph
646
+ // (U+21A9) which renders across every modern terminal.
647
+ this.appendSystemLine('↩ walked back 1 turn');
648
+ return 'walked-back';
649
+ }
542
650
  /**
543
651
  * Current cancellation token. Returned for the tool execution path
544
652
  * (file-tools.ts) so it can pass the token down into a ToolContext
@@ -577,6 +685,18 @@ export class ReplSession {
577
685
  await this.dispatchStop(verdict.persona);
578
686
  return verdict;
579
687
  }
688
+ case 'delegate': {
689
+ // α7.5 Phase 1: surface the dispatch intent inline. The actual
690
+ // wire shape (POST /api/pugi/sessions/:id/delegate) requires the
691
+ // SDK transport extension that ships alongside this PR; the
692
+ // REPL session module wires the call when the matching transport
693
+ // method lands (paired CLI follow-up). Today we surface the
694
+ // delegation intent in the transcript so the operator sees the
695
+ // verdict echo for muscle-memory before the round-trip lights up.
696
+ this.appendSystemLine(`delegate ${verdict.persona}: ${verdict.brief.length > 80 ? `${verdict.brief.slice(0, 77)}...` : verdict.brief}`);
697
+ this.appendSystemLine('Run `pugi delegate <slug> "<brief>"` from a fresh shell while the REPL transport wiring lands.');
698
+ return verdict;
699
+ }
580
700
  case 'dispatch': {
581
701
  await this.dispatchBrief(verdict.brief);
582
702
  return verdict;
@@ -602,11 +722,15 @@ export class ReplSession {
602
722
  return verdict;
603
723
  }
604
724
  case 'cost': {
605
- this.dispatchCost();
725
+ await this.dispatchCost();
726
+ return verdict;
727
+ }
728
+ case 'quota': {
729
+ await this.dispatchQuota();
606
730
  return verdict;
607
731
  }
608
732
  case 'status': {
609
- this.dispatchStatus();
733
+ await this.dispatchStatus();
610
734
  return verdict;
611
735
  }
612
736
  case 'consensus': {
@@ -651,12 +775,859 @@ export class ReplSession {
651
775
  await this.dispatchPrivacy();
652
776
  return verdict;
653
777
  }
778
+ case 'init': {
779
+ // β1 Sl11 → β1a r1 (real inline scaffold, 2026-05-26): invoke
780
+ // `scaffoldPugiWorkspace` directly so the operator gets the
781
+ // same .pugi/ setup they would from `pugi init` on a fresh
782
+ // shell. Already-initialised workspaces (every artifact already
783
+ // present) get the "Already initialised" copy; partial / fresh
784
+ // workspaces get the full Created+Skipped breakdown. Default
785
+ // skills install is best-effort — any error from the bundled
786
+ // pack is surfaced as a system line and does not break the
787
+ // REPL session. The dynamic import keeps the slash dispatcher
788
+ // free of a runtime/cli.ts cycle on every keystroke.
789
+ try {
790
+ const { scaffoldPugiWorkspace } = await import('../../runtime/cli.js');
791
+ const lines = [];
792
+ const result = await scaffoldPugiWorkspace({
793
+ cwd: process.cwd(),
794
+ // Slash callers default to the full default-skills pack so
795
+ // the in-REPL experience matches `pugi init`. Operators who
796
+ // want a minimal scaffold still have the shell command.
797
+ noDefaults: false,
798
+ log: (line) => {
799
+ const trimmed = line.replace(/\n+$/u, '');
800
+ if (trimmed.length > 0)
801
+ lines.push(trimmed);
802
+ },
803
+ });
804
+ if (result.alreadyInitialized) {
805
+ this.appendSystemLine(`.pugi/ already initialised at ${result.root}. ${result.skipped.length} artefact(s) verified.`);
806
+ }
807
+ else {
808
+ this.appendSystemLine(`Pugi initialised at ${result.root}. Created ${result.created.length} artefact(s), skipped ${result.skipped.length}.`);
809
+ }
810
+ if (result.defaultSkills.length > 0) {
811
+ const installed = result.defaultSkills.filter((s) => s.status === 'installed').length;
812
+ const skippedSkills = result.defaultSkills.filter((s) => s.status === 'skipped-existing').length;
813
+ this.appendSystemLine(`Default skills: ${installed} installed, ${skippedSkills} already present.`);
814
+ }
815
+ for (const line of lines)
816
+ this.appendSystemLine(line);
817
+ }
818
+ catch (error) {
819
+ const message = error instanceof Error ? error.message : String(error);
820
+ this.appendSystemLine(`/init failed: ${message}`);
821
+ }
822
+ return verdict;
823
+ }
824
+ case 'mcp': {
825
+ // β4 Sl7 (2026-05-26): /mcp [sub] [args...] forwards to the
826
+ // runtime command. We deliberately route through the same
827
+ // entry-point used by `pugi mcp` from a fresh shell so the
828
+ // surface stays single-sourced. `serve` is refused inline —
829
+ // booting an MCP server inside an active REPL would compete
830
+ // with the REPL itself for stdio, which is exactly the wrong
831
+ // thing to do.
832
+ if (verdict.args[0] === 'serve') {
833
+ this.appendSystemLine('/mcp serve is not safe inside the REPL (it competes for stdio). ' +
834
+ 'Run `pugi mcp serve` from a fresh shell instead.');
835
+ return verdict;
836
+ }
837
+ try {
838
+ const { runMcpCommand } = await import('../../runtime/commands/mcp.js');
839
+ const lines = [];
840
+ await runMcpCommand(verdict.args, {
841
+ workspaceRoot: process.cwd(),
842
+ writeOutput: (_payload, text) => {
843
+ const trimmed = text.replace(/\n+$/u, '');
844
+ if (trimmed.length > 0)
845
+ lines.push(trimmed);
846
+ },
847
+ });
848
+ for (const line of lines)
849
+ this.appendSystemLine(line);
850
+ if (lines.length === 0) {
851
+ this.appendSystemLine('/mcp: no output.');
852
+ }
853
+ }
854
+ catch (error) {
855
+ const message = error instanceof Error ? error.message : String(error);
856
+ this.appendSystemLine(`/mcp failed: ${message}`);
857
+ }
858
+ return verdict;
859
+ }
860
+ case 'theme': {
861
+ // Leak L30 (2026-05-27): /theme [name] [--persist|--reset|--list]
862
+ // forwards to the shared `runThemeCommand` runner. Same async
863
+ // buffer-then-flush pattern as `/style` so a future async
864
+ // write path inside the runner cannot drop a tail emission
865
+ // and so multi-line payloads (banner + preview table) land
866
+ // one row per visual line in the conversation pane.
867
+ try {
868
+ const { runThemeCommand } = await import('../../runtime/commands/theme.js');
869
+ const lines = [];
870
+ await runThemeCommand(verdict.args, {
871
+ workspaceRoot: process.cwd(),
872
+ writeOutput: (_payload, text) => {
873
+ for (const raw of text.split('\n')) {
874
+ const trimmed = raw.replace(/\s+$/u, '');
875
+ lines.push(trimmed);
876
+ }
877
+ },
878
+ });
879
+ if (lines.length === 0) {
880
+ this.appendSystemLine('/theme: no output.');
881
+ }
882
+ else {
883
+ for (const line of lines)
884
+ this.appendSystemLine(line);
885
+ }
886
+ }
887
+ catch (error) {
888
+ const message = error instanceof Error ? error.message : String(error);
889
+ this.appendSystemLine(`/theme failed: ${message}`);
890
+ }
891
+ return verdict;
892
+ }
893
+ case 'style': {
894
+ // Leak L18 (2026-05-27): /style [name] [--persist|--reset|--list]
895
+ // forwards to the shared `runStyleCommand` runner so the slash
896
+ // + top-level surfaces share one code path. Dynamic import
897
+ // keeps the dispatcher free of the output-style module graph
898
+ // until the operator first invokes the slash. The runner's
899
+ // exit code is captured but NOT propagated to process.exitCode
900
+ // — REPL session should not die because a bad preset slug was
901
+ // typed in the input box.
902
+ try {
903
+ const { runStyleCommand } = await import('../../runtime/commands/style.js');
904
+ // L18 P1 fix (2026-05-27): writeOutput is invoked SYNCHRONOUSLY
905
+ // by `runStyleCommand` for each emitted block. We buffer every
906
+ // emission into `lines` and flush after the await resolves so
907
+ // that:
908
+ // (1) any future async write path inside the runner cannot
909
+ // drop a tail emission (callback never references the
910
+ // Ink frame directly), and
911
+ // (2) multi-line payloads (e.g. the active-style banner +
912
+ // catalogue table) render one row per visual line in the
913
+ // conversation pane, matching the `/stickers` surface.
914
+ const lines = [];
915
+ await runStyleCommand(verdict.args, {
916
+ workspaceRoot: process.cwd(),
917
+ writeOutput: (_payload, text) => {
918
+ for (const raw of text.split('\n')) {
919
+ const trimmed = raw.replace(/\s+$/u, '');
920
+ lines.push(trimmed);
921
+ }
922
+ },
923
+ });
924
+ if (lines.length === 0) {
925
+ this.appendSystemLine('/style: no output.');
926
+ }
927
+ else {
928
+ for (const line of lines)
929
+ this.appendSystemLine(line);
930
+ }
931
+ }
932
+ catch (error) {
933
+ const message = error instanceof Error ? error.message : String(error);
934
+ this.appendSystemLine(`/style failed: ${message}`);
935
+ }
936
+ return verdict;
937
+ }
938
+ case 'onboarding': {
939
+ // Leak L25 (2026-05-27): /onboarding forwards to the shared
940
+ // `runOnboardingCommand` runner. From inside the REPL we ALWAYS
941
+ // route through the non-interactive snapshot path — the REPL
942
+ // already owns the Ink tree and mounting a second Ink wizard
943
+ // on top would conflict over stdin raw mode. Operators who
944
+ // want the interactive walk exit the REPL and run
945
+ // `pugi onboarding` from a fresh shell; the slash surface
946
+ // surfaces the recap card + hints inline so the operator
947
+ // sees current values without leaving the session.
948
+ try {
949
+ const { runOnboardingCommand } = await import('../../runtime/commands/onboarding.js');
950
+ const { resolveActiveCredential } = await import('../credentials.js');
951
+ const credential = resolveActiveCredential();
952
+ const lines = [];
953
+ await runOnboardingCommand(verdict.args, {
954
+ workspaceRoot: process.cwd(),
955
+ env: process.env,
956
+ authPresent: credential !== null,
957
+ interactive: false,
958
+ writeOutput: (_payload, text) => {
959
+ const trimmed = text.replace(/\n+$/u, '');
960
+ if (trimmed.length > 0)
961
+ lines.push(trimmed);
962
+ },
963
+ });
964
+ for (const line of lines)
965
+ this.appendSystemLine(line);
966
+ if (lines.length === 0) {
967
+ this.appendSystemLine('/onboarding: no output.');
968
+ }
969
+ }
970
+ catch (error) {
971
+ const message = error instanceof Error ? error.message : String(error);
972
+ this.appendSystemLine(`/onboarding failed: ${message}`);
973
+ }
974
+ return verdict;
975
+ }
976
+ case 'vim': {
977
+ // Leak L26 (2026-05-27): /vim forwards to the shared
978
+ // `runVimCommand` runner so the slash + top-level surfaces
979
+ // stay single-sourced. Dynamic import mirrors /style so the
980
+ // dispatcher does not drag the vim module graph into every
981
+ // keystroke.
982
+ //
983
+ // The runner mutates `~/.pugi/config.json::vimMode`; the
984
+ // active REPL session does NOT live-pick-up the flip (the
985
+ // VimInput wrapper is mounted once at REPL boot). Operators
986
+ // get a hint that the next session will reflect the change.
987
+ // A follow-up sprint can plumb a state-store subscriber so
988
+ // the flip takes effect mid-session.
989
+ try {
990
+ const { runVimCommand } = await import('../../runtime/commands/vim.js');
991
+ const lines = [];
992
+ await runVimCommand(verdict.args, {
993
+ env: process.env,
994
+ writeOutput: (_payload, text) => {
995
+ for (const raw of text.split('\n')) {
996
+ const trimmed = raw.replace(/\s+$/u, '');
997
+ lines.push(trimmed);
998
+ }
999
+ },
1000
+ });
1001
+ if (lines.length === 0) {
1002
+ this.appendSystemLine('/vim: no output.');
1003
+ }
1004
+ else {
1005
+ for (const line of lines)
1006
+ this.appendSystemLine(line);
1007
+ }
1008
+ }
1009
+ catch (error) {
1010
+ const message = error instanceof Error ? error.message : String(error);
1011
+ this.appendSystemLine(`/vim failed: ${message}`);
1012
+ }
1013
+ return verdict;
1014
+ }
1015
+ case 'doctor': {
1016
+ // L17 (2026-05-27): run the doctor probe sweep inline. We
1017
+ // dynamic-import the runtime/commands/doctor module so the
1018
+ // slash dispatcher does not pull the diagnostics graph
1019
+ // (execFileSync + fs probes) into every keystroke. The
1020
+ // module's output is captured into local lines so we can
1021
+ // render it as system entries in the conversation pane;
1022
+ // an Ink-rendered table inside the REPL frame is a follow-up.
1023
+ try {
1024
+ const { runDoctorCommand, defaultHome } = await import('../../runtime/commands/doctor.js');
1025
+ const lines = [];
1026
+ await runDoctorCommand({
1027
+ cwd: process.cwd(),
1028
+ home: defaultHome(),
1029
+ env: process.env,
1030
+ json: false,
1031
+ writeOutput: (_payload, text) => {
1032
+ const trimmed = text.replace(/\n+$/u, '');
1033
+ if (trimmed.length > 0)
1034
+ lines.push(trimmed);
1035
+ },
1036
+ });
1037
+ for (const line of lines)
1038
+ this.appendSystemLine(line);
1039
+ if (lines.length === 0) {
1040
+ this.appendSystemLine('/doctor: no output.');
1041
+ }
1042
+ }
1043
+ catch (error) {
1044
+ const message = error instanceof Error ? error.message : String(error);
1045
+ this.appendSystemLine(`/doctor failed: ${message}`);
1046
+ }
1047
+ return verdict;
1048
+ }
1049
+ case 'prd-check': {
1050
+ // Wave 6 (2026-05-27): forward to the same handler the shell
1051
+ // surface uses so the verdict is identical between
1052
+ // `/prd-check` and `pugi prd-check`. Dynamic-import the
1053
+ // module to keep the parser + verifier graph out of the
1054
+ // REPL hot path.
1055
+ //
1056
+ // Wave 6 final (2026-05-27): the runner now also honours
1057
+ // `--session` mode (orthogonal to the verifier graph — walks
1058
+ // up for PRD.md, reads NDJSON turns, dispatches a cross-
1059
+ // review subagent). We stream the runner's status lines
1060
+ // directly to the system pane so the operator sees
1061
+ // "Locating PRD..." / "Reviewing against PRD..." while the
1062
+ // dispatch is in flight, then the structured Satisfied /
1063
+ // Outstanding lists when it lands.
1064
+ try {
1065
+ const { parsePrdCheckArgs, runPrdCheckCommand } = await import('../../runtime/commands/prd-check.js');
1066
+ const parsed = parsePrdCheckArgs(verdict.args, { jsonDefault: false });
1067
+ if (!parsed.ok) {
1068
+ this.appendSystemLine(`/prd-check: ${parsed.error}`);
1069
+ return verdict;
1070
+ }
1071
+ let sawOutput = false;
1072
+ await runPrdCheckCommand({
1073
+ cwd: process.cwd(),
1074
+ ...(parsed.prdPath !== undefined ? { prdPath: parsed.prdPath } : {}),
1075
+ flags: parsed.flags,
1076
+ // The REPL slash does not have a snapshot of the CLI
1077
+ // command registry, so we pass an empty set; the
1078
+ // command:<name> verifier will report FAIL for now.
1079
+ // This is a deliberate trade-off — the slash surface
1080
+ // primarily exists for quick eyeball checks during a
1081
+ // session; the shell surface (which DOES inject the
1082
+ // full registry) is the canonical gate.
1083
+ knownCommands: new Set(),
1084
+ writeOutput: (_payload, text) => {
1085
+ const trimmed = text.replace(/\n+$/u, '');
1086
+ if (trimmed.length > 0) {
1087
+ this.appendSystemLine(trimmed);
1088
+ sawOutput = true;
1089
+ }
1090
+ },
1091
+ });
1092
+ if (!sawOutput) {
1093
+ this.appendSystemLine('/prd-check: no output.');
1094
+ }
1095
+ }
1096
+ catch (error) {
1097
+ const message = error instanceof Error ? error.message : String(error);
1098
+ this.appendSystemLine(`/prd-check failed: ${message}`);
1099
+ }
1100
+ return verdict;
1101
+ }
1102
+ case 'chain': {
1103
+ // Wave 6 (2026-05-27): forward to the shell-surface runner so
1104
+ // the slash + top-level CLI share one parser + dispatcher.
1105
+ // Dynamic import keeps the chain module out of the REPL hot
1106
+ // path. The slash variant does NOT inject the live delegate
1107
+ // wire-up — operators wanting full dispatch run `pugi chain
1108
+ // next` from a fresh shell. The slash form is best-effort for
1109
+ // status / show / list which are read-only.
1110
+ try {
1111
+ const { runChainCommand } = await import('../../runtime/commands/chain.js');
1112
+ const lines = [];
1113
+ await runChainCommand(verdict.args, {
1114
+ cwd: process.cwd(),
1115
+ json: false,
1116
+ writeOutput: (_payload, text) => {
1117
+ const trimmed = text.replace(/\n+$/u, '');
1118
+ if (trimmed.length > 0)
1119
+ lines.push(trimmed);
1120
+ },
1121
+ });
1122
+ for (const line of lines)
1123
+ this.appendSystemLine(line);
1124
+ if (lines.length === 0) {
1125
+ this.appendSystemLine('/chain: no output.');
1126
+ }
1127
+ }
1128
+ catch (error) {
1129
+ const message = error instanceof Error ? error.message : String(error);
1130
+ this.appendSystemLine(`/chain failed: ${message}`);
1131
+ }
1132
+ return verdict;
1133
+ }
1134
+ case 'permissions': {
1135
+ // Leak L6: handle the `/permissions [mode] [--persist]` flow.
1136
+ // The session module forwards to the runtime helper so the
1137
+ // workspace + global-config writes share one code path with
1138
+ // the CLI's top-level `--mode` resolution. The dynamic import
1139
+ // keeps the dispatcher free of a session.ts -> runtime/cli.ts
1140
+ // cycle.
1141
+ try {
1142
+ const { runPermissionsCommand } = await import('../../runtime/commands/permissions.js');
1143
+ const lines = [];
1144
+ await runPermissionsCommand(verdict, {
1145
+ workspaceRoot: process.cwd(),
1146
+ writeOutput: (line) => {
1147
+ const trimmed = line.replace(/\n+$/u, '');
1148
+ if (trimmed.length > 0)
1149
+ lines.push(trimmed);
1150
+ },
1151
+ });
1152
+ for (const line of lines)
1153
+ this.appendSystemLine(line);
1154
+ }
1155
+ catch (error) {
1156
+ const message = error instanceof Error ? error.message : String(error);
1157
+ this.appendSystemLine(`/permissions failed: ${message}`);
1158
+ }
1159
+ return verdict;
1160
+ }
1161
+ case 'compact': {
1162
+ // Leak L8 (2026-05-27): /compact summarises older turns and
1163
+ // appends a boundary marker. We forward to the same runner the
1164
+ // top-level `pugi compact` command uses so the surface stays
1165
+ // single-sourced. The session module owns the in-memory
1166
+ // transcript echo (system line + banner row) so the operator
1167
+ // sees the marker land without a fresh REPL bootstrap.
1168
+ //
1169
+ // Wave 6 BT 8 (Claude Code parity): `--force` bypasses the
1170
+ // noop-empty guard so the operator can compact even short
1171
+ // sessions (useful before a manual checkpoint).
1172
+ await this.dispatchCompact('manual', { force: verdict.force });
1173
+ return verdict;
1174
+ }
1175
+ case 'model': {
1176
+ // Wave 6 BT 8 (Claude Code parity): /model lists OR selects the
1177
+ // active model. Slash + top-level CLI share `runModelCommand`.
1178
+ // The session module forwards writeOutput → appendSystemLine so
1179
+ // the menu + the confirmation line land inline in the
1180
+ // transcript. Tier override is undefined at the slash surface;
1181
+ // the runner defaults to 'team' so unauthenticated operators
1182
+ // see every model. Server-side calls enforce the real tier cap.
1183
+ try {
1184
+ const { runModelCommand } = await import('../../runtime/commands/model.js');
1185
+ await runModelCommand({ slug: verdict.slug }, {
1186
+ workspaceRoot: process.cwd(),
1187
+ writeOutput: (line) => {
1188
+ const trimmed = line.replace(/\n+$/u, '');
1189
+ if (trimmed.length > 0)
1190
+ this.appendSystemLine(trimmed);
1191
+ else
1192
+ this.appendSystemLine('');
1193
+ },
1194
+ });
1195
+ }
1196
+ catch (error) {
1197
+ const message = error instanceof Error ? error.message : String(error);
1198
+ this.appendSystemLine(`/model failed: ${message}`);
1199
+ }
1200
+ return verdict;
1201
+ }
1202
+ case 'rewind': {
1203
+ // Leak L9 (2026-05-27): /rewind appends an append-only
1204
+ // tombstone marker that rolls the conversation back to a
1205
+ // checkpoint. The actual replay-mask is advisory — the on-disk
1206
+ // events stay durable so `pugi sessions undo-rewind` can
1207
+ // reverse the operation. We forward to the same runner the
1208
+ // top-level `pugi rewind` command uses to keep the surface
1209
+ // single-sourced. Dynamic import avoids pulling the checkpoint
1210
+ // graph into the dispatcher at module load.
1211
+ if (!this.store || !this.localSessionId) {
1212
+ this.appendSystemLine('Local session store is disabled — /rewind is unavailable.');
1213
+ return verdict;
1214
+ }
1215
+ try {
1216
+ const { runRewindCommand } = await import('../../runtime/commands/rewind.js');
1217
+ await runRewindCommand(verdict.args, {
1218
+ workspaceRoot: process.cwd(),
1219
+ sessionId: this.localSessionId,
1220
+ store: this.store,
1221
+ writeOutput: (_payload, text) => {
1222
+ if (text.length > 0)
1223
+ this.appendSystemLine(text);
1224
+ },
1225
+ });
1226
+ }
1227
+ catch (error) {
1228
+ const message = error instanceof Error ? error.message : String(error);
1229
+ this.appendSystemLine(`/rewind failed: ${message}`);
1230
+ }
1231
+ return verdict;
1232
+ }
1233
+ case 'share': {
1234
+ // Leak L20 (2026-05-27): /share forwards to the same runner the
1235
+ // top-level `pugi share` command uses. The session module
1236
+ // wires writeOutput to appendSystemLine so the upload result +
1237
+ // privacy gate banner land in the REPL transcript inline.
1238
+ // Confirmation prompt + readline still use stdio because the
1239
+ // Ink frame is held by the input box; operators wanting fully
1240
+ // scripted shares pass `--yes` so no prompt fires.
1241
+ try {
1242
+ const { runShareCommand } = await import('../../runtime/commands/share.js');
1243
+ const lines = [];
1244
+ await runShareCommand(verdict.args, {
1245
+ workspaceRoot: process.cwd(),
1246
+ cliVersion: this.options.cliVersion,
1247
+ sessionId: this.localSessionId ?? undefined,
1248
+ writeOutput: (_payload, text) => {
1249
+ const trimmed = text.replace(/\n+$/u, '');
1250
+ if (trimmed.length > 0)
1251
+ lines.push(trimmed);
1252
+ },
1253
+ });
1254
+ for (const line of lines)
1255
+ this.appendSystemLine(line);
1256
+ if (lines.length === 0) {
1257
+ this.appendSystemLine('/share: no output.');
1258
+ }
1259
+ }
1260
+ catch (error) {
1261
+ const message = error instanceof Error ? error.message : String(error);
1262
+ this.appendSystemLine(`/share failed: ${message}`);
1263
+ }
1264
+ return verdict;
1265
+ }
1266
+ case 'plan': {
1267
+ // Leak L7: handle `/plan [--back | --persist] [<prompt>]`.
1268
+ // The session module forwards the mode-switch portion to the
1269
+ // shared runtime helper so the workspace + global-config writes
1270
+ // share one code path with `pugi plan`. When the operator
1271
+ // typed a prompt alongside (`/plan write me X`), the prompt is
1272
+ // forwarded through the dispatch FSM exactly as if they had
1273
+ // typed it directly — the only difference is the gate now
1274
+ // refuses write/dispatch tools because the workspace mode flipped
1275
+ // to plan first. Same dynamic-import trick as /permissions to
1276
+ // avoid pulling the engine adapter graph into the dispatcher.
1277
+ try {
1278
+ const { runPlanCommand } = await import('../../runtime/commands/plan.js');
1279
+ const lines = [];
1280
+ await runPlanCommand({ back: verdict.back, persist: verdict.persist }, {
1281
+ workspaceRoot: process.cwd(),
1282
+ writeOutput: (line) => {
1283
+ const trimmed = line.replace(/\n+$/u, '');
1284
+ if (trimmed.length > 0)
1285
+ lines.push(trimmed);
1286
+ },
1287
+ });
1288
+ for (const line of lines)
1289
+ this.appendSystemLine(line);
1290
+ // Optional one-shot engine dispatch: when the operator typed
1291
+ // a prompt alongside the slash, route it through the existing
1292
+ // dispatch path. We rewrite the verdict into a synthetic
1293
+ // `dispatch` result so the engine sees the user's prompt with
1294
+ // the plan-mode gate already in place. `--auto-back` is NOT
1295
+ // honoured in the slash surface today — operators stay in
1296
+ // plan mode and revert manually with `/plan --back`. The CLI
1297
+ // top-level `pugi plan --auto-back` exists for scripted use.
1298
+ if (verdict.prompt.length > 0 && !verdict.back) {
1299
+ return { kind: 'dispatch', brief: verdict.prompt };
1300
+ }
1301
+ }
1302
+ catch (error) {
1303
+ const message = error instanceof Error ? error.message : String(error);
1304
+ this.appendSystemLine(`/plan failed: ${message}`);
1305
+ }
1306
+ return verdict;
1307
+ }
1308
+ case 'release-notes': {
1309
+ // Leak L24 (2026-05-27): changelog diff between the operator's
1310
+ // last-seen + installed CLI versions. Delegate к the shared
1311
+ // `runReleaseNotesCommand` runner so the slash + top-level
1312
+ // paths stay single-sourced. The renderer collects each line
1313
+ // into the system pane via `appendSystemLine` — no fresh Ink
1314
+ // mount, no boxed render. `--reset` is honoured via the
1315
+ // `verdict.reset` field parsed in slash-commands.ts.
1316
+ try {
1317
+ const { runReleaseNotesCommand, defaultReleaseNotesHome } = await import('../../runtime/commands/release-notes.js');
1318
+ const lines = [];
1319
+ runReleaseNotesCommand({
1320
+ home: defaultReleaseNotesHome(),
1321
+ json: false,
1322
+ reset: verdict.reset,
1323
+ writeOutput: (_payload, text) => {
1324
+ for (const line of text.split('\n')) {
1325
+ lines.push(line.replace(/\s+$/u, ''));
1326
+ }
1327
+ },
1328
+ });
1329
+ if (lines.length === 0) {
1330
+ this.appendSystemLine('/release-notes: no output.');
1331
+ }
1332
+ else {
1333
+ for (const line of lines)
1334
+ this.appendSystemLine(line);
1335
+ }
1336
+ }
1337
+ catch (error) {
1338
+ const message = error instanceof Error ? error.message : String(error);
1339
+ this.appendSystemLine(`/release-notes failed: ${message}`);
1340
+ }
1341
+ return verdict;
1342
+ }
1343
+ case 'stickers': {
1344
+ // Leak L33 (2026-05-27): brand-personality gimmick. Delegate to
1345
+ // the shared `runStickersCommand` so the slash + top-level
1346
+ // paths stay single-sourced. The renderer routes the text
1347
+ // through the system pane line-buffer (ascii-only — no fresh
1348
+ // Ink mount) so the gimmick lands as a single contiguous
1349
+ // block в the conversation transcript.
1350
+ try {
1351
+ const { runStickersCommand } = await import('../../runtime/commands/stickers.js');
1352
+ // L33 P1 fix (2026-05-27): await the runner even though the
1353
+ // current implementation is synchronous. Two reasons:
1354
+ // (1) future-proofs the call site against the runner growing
1355
+ // an async path (e.g. remote stickerpack fetch) — without
1356
+ // this await, a returned promise would resolve AFTER we
1357
+ // flushed `lines` and the gimmick would render blank, and
1358
+ // (2) keeps the slash dispatcher uniform with the other
1359
+ // command runners (style, doctor, permissions, plan), all
1360
+ // of which are awaited.
1361
+ const lines = [];
1362
+ await runStickersCommand({
1363
+ json: false,
1364
+ asciiOnly: true,
1365
+ writeOutput: (_payload, text) => {
1366
+ for (const line of text.split('\n')) {
1367
+ const trimmed = line.replace(/\s+$/u, '');
1368
+ lines.push(trimmed);
1369
+ }
1370
+ },
1371
+ });
1372
+ if (lines.length === 0) {
1373
+ this.appendSystemLine('/stickers: no output.');
1374
+ }
1375
+ else {
1376
+ for (const line of lines)
1377
+ this.appendSystemLine(line);
1378
+ }
1379
+ }
1380
+ catch (error) {
1381
+ const message = error instanceof Error ? error.message : String(error);
1382
+ this.appendSystemLine(`/stickers failed: ${message}`);
1383
+ }
1384
+ return verdict;
1385
+ }
1386
+ case 'update': {
1387
+ // Leak L27 (2026-05-27): /update probes the npm registry for a
1388
+ // newer @pugi/cli version on the configured channel and prints
1389
+ // the install command. The slash form NEVER spawns `npm install
1390
+ // -g` — that would corrupt the binary we are currently running.
1391
+ // Operators see the install command + run it manually (or run
1392
+ // `pugi update --apply` from a fresh shell after the REPL
1393
+ // exits). The slash + top-level paths share the dispatcher so
1394
+ // channel resolution + last-check persistence stay single-
1395
+ // sourced.
1396
+ try {
1397
+ const { parseUpdateArgs, runUpdateCommand } = await import('../../runtime/commands/update.js');
1398
+ const parsed = parseUpdateArgs(verdict.args);
1399
+ if ('error' in parsed) {
1400
+ this.appendSystemLine(parsed.error);
1401
+ return verdict;
1402
+ }
1403
+ // Force `apply=false` on the slash path — see comment above.
1404
+ const slashFlags = { ...parsed, apply: false };
1405
+ const lines = [];
1406
+ await runUpdateCommand({
1407
+ cwd: process.cwd(),
1408
+ home: homedir(),
1409
+ env: process.env,
1410
+ flags: slashFlags,
1411
+ promptConfirm: async () => false,
1412
+ writeOutput: (_payload, text) => {
1413
+ for (const line of text.split('\n')) {
1414
+ const trimmed = line.replace(/\s+$/u, '');
1415
+ if (trimmed.length > 0)
1416
+ lines.push(trimmed);
1417
+ }
1418
+ },
1419
+ });
1420
+ if (lines.length === 0) {
1421
+ this.appendSystemLine('/update: no output.');
1422
+ }
1423
+ else {
1424
+ for (const line of lines)
1425
+ this.appendSystemLine(line);
1426
+ }
1427
+ }
1428
+ catch (error) {
1429
+ const message = error instanceof Error ? error.message : String(error);
1430
+ this.appendSystemLine(`/update failed: ${message}`);
1431
+ }
1432
+ return verdict;
1433
+ }
1434
+ case 'feedback': {
1435
+ // Leak L21 (2026-05-27): in-CLI feedback collector. The wizard
1436
+ // mounts a fresh Ink tree (renderFeedbackPrompt) outside the
1437
+ // live REPL input box so the operator can step through
1438
+ // category / rating / comment / context / confirm without
1439
+ // interleaving with persona output. The session module owns
1440
+ // the submit + queue wiring so the slash + top-level CLI
1441
+ // surfaces stay single-sourced through `runFeedbackCommand`.
1442
+ try {
1443
+ await this.runFeedbackSlash();
1444
+ }
1445
+ catch (error) {
1446
+ const message = error instanceof Error ? error.message : String(error);
1447
+ this.appendSystemLine(`/feedback failed: ${message}`);
1448
+ }
1449
+ return verdict;
1450
+ }
1451
+ case 'repo-map': {
1452
+ // Leak L28 (2026-05-27): AST-light workspace summary. Delegate
1453
+ // к the shared `runRepoMapCommand` so the slash + top-level
1454
+ // paths stay single-sourced. The rendered text lands on the
1455
+ // system pane via `appendSystemLine` (no fresh Ink mount) so
1456
+ // the listing flows into the conversation transcript like
1457
+ // any other command output.
1458
+ try {
1459
+ const { runRepoMapCommand } = await import('../../runtime/commands/repo-map.js');
1460
+ const lines = [];
1461
+ await runRepoMapCommand({
1462
+ cwd: process.cwd(),
1463
+ refresh: verdict.refresh,
1464
+ json: false,
1465
+ writeOutput: (_payload, text) => {
1466
+ for (const line of text.split('\n')) {
1467
+ const trimmed = line.replace(/\s+$/u, '');
1468
+ lines.push(trimmed);
1469
+ }
1470
+ },
1471
+ });
1472
+ if (lines.length === 0) {
1473
+ this.appendSystemLine('/repo-map: no output.');
1474
+ }
1475
+ else {
1476
+ for (const line of lines)
1477
+ this.appendSystemLine(line);
1478
+ }
1479
+ }
1480
+ catch (error) {
1481
+ const message = error instanceof Error ? error.message : String(error);
1482
+ this.appendSystemLine(`/repo-map failed: ${message}`);
1483
+ }
1484
+ return verdict;
1485
+ }
1486
+ case 'undo': {
1487
+ // Wave 6 final (2026-05-27): graduated from stub. The runtime
1488
+ // command `runUndoCommand` already exists with full Aider walk-
1489
+ // back semantics — single-step revert of the most recent
1490
+ // successful `write` / `edit` / `multi_edit` tool result, with
1491
+ // an mtime+hash gate that refuses to overwrite uncommitted
1492
+ // operator work. We open a fresh PugiSession against the cwd
1493
+ // so the inverse-mutation audit lands on the same NDJSON
1494
+ // events stream the REPL writes to; dynamic-import keeps the
1495
+ // runner + git plumbing out of the REPL hot path.
1496
+ try {
1497
+ const [{ runUndoCommand }, { openSession }] = await Promise.all([
1498
+ import('../../runtime/commands/undo.js'),
1499
+ import('../session.js'),
1500
+ ]);
1501
+ const workspaceRoot = process.cwd();
1502
+ const session = openSession(workspaceRoot);
1503
+ this.appendSystemLine('Reverting last write...');
1504
+ await runUndoCommand([], {
1505
+ workspaceRoot,
1506
+ session,
1507
+ writeOutput: (_payload, text) => {
1508
+ const trimmed = text.replace(/\n+$/u, '');
1509
+ if (trimmed.length > 0)
1510
+ this.appendSystemLine(trimmed);
1511
+ },
1512
+ });
1513
+ }
1514
+ catch (error) {
1515
+ const message = error instanceof Error ? error.message : String(error);
1516
+ this.appendSystemLine(`/undo failed: ${message}`);
1517
+ }
1518
+ return verdict;
1519
+ }
654
1520
  case 'stub': {
655
1521
  this.appendSystemLine(verdict.message);
656
1522
  return verdict;
657
1523
  }
658
1524
  }
659
1525
  }
1526
+ /**
1527
+ * Leak L21 (2026-05-27): drive the `/feedback` wizard from inside
1528
+ * the REPL. Mounts the Ink prompt, collects the draft, hands it to
1529
+ * `runFeedbackCommand` (which routes to submit-now or
1530
+ * queue-locally), then writes the operator-facing toast to the
1531
+ * conversation system pane.
1532
+ *
1533
+ * The session module owns the wiring (cwd, cliVersion, apiUrl,
1534
+ * apiKey, transcript provider) so the slash + top-level CLI paths
1535
+ * stay single-sourced through `runFeedbackCommand`.
1536
+ */
1537
+ async runFeedbackSlash() {
1538
+ const { renderFeedbackPrompt } = await import('../../tui/feedback-prompt.js');
1539
+ const { runFeedbackCommand, renderFeedbackToast } = await import('../../runtime/commands/feedback.js');
1540
+ const { submitFeedback, redactSessionContext } = await import('../feedback/submitter.js');
1541
+ const verdict = await renderFeedbackPrompt();
1542
+ if (verdict.cancelled || !verdict.draft) {
1543
+ this.appendSystemLine('Feedback cancelled. Nothing was sent.');
1544
+ return;
1545
+ }
1546
+ // Build a session-context provider that reads the LAST 5 turns
1547
+ // from the live transcript + applies the redactor. Only invoked
1548
+ // when the operator opted in on step 4.
1549
+ const sessionContextProvider = () => {
1550
+ const last5 = this.state.transcript
1551
+ .filter((row) => row.source !== 'system')
1552
+ .slice(-5)
1553
+ .map((row) => ({
1554
+ role: row.source === 'operator' ? 'user' : 'assistant',
1555
+ text: row.text,
1556
+ }));
1557
+ // The workspace context exposed to the session does not carry
1558
+ // a git branch field today, so we omit `gitBranch` here. When
1559
+ // `ReplWorkspaceContext` gains the field we can forward it via
1560
+ // an extra options entry without changing the redactor contract.
1561
+ return redactSessionContext(last5);
1562
+ };
1563
+ const result = await runFeedbackCommand({
1564
+ cwd: process.cwd(),
1565
+ cliVersion: this.options.cliVersion,
1566
+ submit: async (env) => submitFeedback(env, {
1567
+ apiUrl: this.options.apiUrl,
1568
+ apiKey: this.options.apiKey,
1569
+ }),
1570
+ draft: verdict.draft,
1571
+ sessionContext: sessionContextProvider,
1572
+ });
1573
+ this.appendSystemLine(renderFeedbackToast(result));
1574
+ }
1575
+ /**
1576
+ * Leak L8 (2026-05-27): drive the `/compact` flow from inside the
1577
+ * REPL. Reuses the standalone runner so the wire shape + reason
1578
+ * codes stay single-sourced. The result is echoed into the
1579
+ * transcript as a system line; on success the operator sees the
1580
+ * banner sentinel on next render.
1581
+ *
1582
+ * `trigger='manual'` for explicit `/compact` invocations;
1583
+ * `trigger='auto'` for the threshold gate. The runner records the
1584
+ * trigger in the marker payload so the banner can distinguish them.
1585
+ */
1586
+ async dispatchCompact(trigger, options = {}) {
1587
+ if (!this.store || !this.localSessionId) {
1588
+ this.appendSystemLine('Local session store is disabled — /compact is unavailable.');
1589
+ return;
1590
+ }
1591
+ try {
1592
+ const { runCompactCommand } = await import('../../runtime/commands/compact.js');
1593
+ const result = await runCompactCommand([], {
1594
+ workspaceRoot: process.cwd(),
1595
+ sessionId: this.localSessionId,
1596
+ store: this.store,
1597
+ trigger,
1598
+ force: options.force === true,
1599
+ writeOutput: (_payload, text) => {
1600
+ if (text.length > 0)
1601
+ this.appendSystemLine(text);
1602
+ },
1603
+ });
1604
+ if (result.status === 'compacted') {
1605
+ // L29 (2026-05-27): emit a structured `compact-boundary` row so
1606
+ // the conversation pane routes the marker through the dedicated
1607
+ // `<CompactBanner />` Ink component (gray, terminal-width
1608
+ // separator) instead of leaking the raw text into a `system`
1609
+ // row. The plain-text body is kept as a deterministic fallback
1610
+ // for non-Ink consumers (snapshot tests, JSON-mode exports).
1611
+ const turnsBefore = result.turnsBefore ?? 0;
1612
+ this.appendRow({
1613
+ source: 'compact-boundary',
1614
+ text: `─── context compacted (${turnsBefore} turns → 1 summary, ${trigger}) ───`,
1615
+ compaction: {
1616
+ turnsBefore,
1617
+ trigger,
1618
+ summaryTokenCount: result.tokensSummarised,
1619
+ // Fresh in-REPL compaction lands at the head of the
1620
+ // transcript — no turns have followed it yet.
1621
+ turnsAgo: 0,
1622
+ },
1623
+ });
1624
+ }
1625
+ }
1626
+ catch (error) {
1627
+ const message = error instanceof Error ? error.message : String(error);
1628
+ this.appendSystemLine(`/compact failed: ${message}`);
1629
+ }
1630
+ }
660
1631
  /**
661
1632
  * In-REPL `/privacy` - alpha 6.13. Prints the full 3-mode contract
662
1633
  * doc + the current mode banner inline. The current mode is fetched
@@ -938,22 +1909,195 @@ export class ReplSession {
938
1909
  this.appendSystemLine(`/diff failed: ${this.errorMessage(error)}`);
939
1910
  }
940
1911
  }
941
- dispatchCost() {
942
- const { tokensDownstreamTotal, agents } = this.state;
1912
+ async dispatchCost() {
1913
+ // α7 cost-meter sprint full breakdown matching the TUI status row
1914
+ // footer. The session totals line mirrors the footer format
1915
+ // (`↑ <in> ↓ <out> · $X.XX · <elapsed>`) so the operator scans the
1916
+ // same numbers in two places. Per-turn list shows the last 5 turns
1917
+ // oldest → newest; an empty list renders one system line so the
1918
+ // operator knows the surface is wired (`No completed turns yet.`).
1919
+ //
1920
+ // L19 (2026-05-27) — after the in-memory recap, also render the
1921
+ // persisted per-model table from `.pugi/cost.json`. That surface
1922
+ // survives a REPL restart and answers the "what did I spend on
1923
+ // claude-opus vs qwen this week?" question the in-memory recap can
1924
+ // not. Errors loading the file collapse to a single warning line so
1925
+ // the in-memory recap (the older, well-tested surface) is never
1926
+ // gated behind a fresh dependency.
1927
+ const { sessionTokensIn, sessionTokensOut, sessionCostUsd, sessionStartedAtEpochMs, recentTurns, agents, } = this.state;
943
1928
  const active = agents.filter((a) => a.status === 'queued' || a.status === 'thinking').length;
944
- const lineTokens = `Tokens this session: ${tokensDownstreamTotal.toLocaleString()} (in+out).`;
945
- const lineAgents = `Active dispatches: ${active} of cap.`;
946
- this.appendSystemLine(lineTokens);
947
- this.appendSystemLine(lineAgents);
948
- this.appendSystemLine('Full per-persona budget breakdown lands in α6.5.');
949
- }
950
- dispatchStatus() {
951
- const sessionId = this.state.sessionId ?? '(unbound)';
952
- const reach = this.state.connection;
953
- this.appendSystemLine(`Backend: ${this.options.apiUrl} (${reach}).`);
954
- this.appendSystemLine(`Session: ${sessionId}.`);
955
- this.appendSystemLine(`Workspace: ${this.state.workspaceLabel}.`);
956
- this.appendSystemLine(`CLI: pugi ${this.state.cliVersion}.`);
1929
+ const elapsedMs = Math.max(0, this.now() - sessionStartedAtEpochMs);
1930
+ const elapsedLabel = formatElapsedShort(elapsedMs);
1931
+ this.appendSystemLine(`Session: ↑ ${formatTokens(sessionTokensIn)} ↓ ${formatTokens(sessionTokensOut)} · ${formatCostUsd(sessionCostUsd)} · ${elapsedLabel}`);
1932
+ this.appendSystemLine(`Active dispatches: ${active} of cap.`);
1933
+ if (recentTurns.length === 0) {
1934
+ this.appendSystemLine('No completed turns yet — brief the workforce to charge the meter.');
1935
+ }
1936
+ else {
1937
+ this.appendSystemLine(`Recent turns (last ${recentTurns.length}):`);
1938
+ for (let i = 0; i < recentTurns.length; i += 1) {
1939
+ const turn = recentTurns[i];
1940
+ const idx = (i + 1).toString().padStart(2, ' ');
1941
+ this.appendSystemLine(` ${idx}. ${formatTokens(turn.tokensIn)} ↓ ${formatTokens(turn.tokensOut)} · ${formatCostUsd(turn.costUsd)}`);
1942
+ }
1943
+ }
1944
+ // L19: append the persisted per-model table from .pugi/cost.json.
1945
+ try {
1946
+ const [{ createCostTracker }, { renderCostForSlash }] = await Promise.all([
1947
+ import('../cost/tracker.js'),
1948
+ import('../../runtime/commands/cost.js'),
1949
+ ]);
1950
+ const workspaceRoot = this.options.workspace?.workspaceCwd ?? process.cwd();
1951
+ const sessionId = this.state.sessionId ?? 'no-session';
1952
+ const tracker = createCostTracker({
1953
+ workspaceRoot,
1954
+ sessionIdProvider: () => sessionId,
1955
+ now: () => this.now(),
1956
+ });
1957
+ const current = tracker.current();
1958
+ if (current && Object.keys(current.models).length > 0) {
1959
+ this.appendSystemLine('');
1960
+ const { lines } = renderCostForSlash({
1961
+ tracker,
1962
+ allSessions: false,
1963
+ windowDays: 30,
1964
+ now: () => this.now(),
1965
+ });
1966
+ for (const line of lines)
1967
+ this.appendSystemLine(line);
1968
+ }
1969
+ }
1970
+ catch {
1971
+ // best-effort — the persisted view is additive; failure never
1972
+ // breaks the in-memory recap above
1973
+ }
1974
+ }
1975
+ /**
1976
+ * α7 cost-meter sprint — `/quota` slash handler. Fetches the live
1977
+ * `/api/pugi/usage` snapshot and renders three lines: plan tier,
1978
+ * monthly window, and per-counter `used/cap (pct%)`. Failure modes
1979
+ * (offline, unauth, older admin-api) collapse to a single one-line
1980
+ * `Could not fetch quota…` system message so the surface never throws
1981
+ * from a keystroke handler.
1982
+ *
1983
+ * The fetch is best-effort with a 4s timeout — mirrors the `whoami`
1984
+ * pattern in `runtime/cli.ts` so the operator gets the same UX on the
1985
+ * REPL slash and the CLI command.
1986
+ */
1987
+ async dispatchQuota() {
1988
+ const controller = new AbortController();
1989
+ const timer = setTimeout(() => controller.abort(), 4000);
1990
+ try {
1991
+ const url = `${this.options.apiUrl.replace(/\/+$/, '')}/api/pugi/usage`;
1992
+ const res = await fetch(url, {
1993
+ method: 'GET',
1994
+ headers: {
1995
+ authorization: `Bearer ${this.options.apiKey}`,
1996
+ accept: 'application/json',
1997
+ },
1998
+ signal: controller.signal,
1999
+ });
2000
+ if (!res.ok) {
2001
+ this.appendSystemLine(`Could not fetch quota: HTTP ${res.status}.`);
2002
+ return;
2003
+ }
2004
+ const body = (await res.json());
2005
+ const tier = typeof body.tier === 'string' ? body.tier : '(unknown)';
2006
+ const tierLabel = QUOTA_TIER_LABELS[tier] ?? tier;
2007
+ const month = typeof body.billingMonth === 'string' ? body.billingMonth : '(unknown month)';
2008
+ const resetAt = typeof body.resetAt === 'string' ? body.resetAt : null;
2009
+ const resetLine = resetAt ? ` · resets ${formatResetWindow(resetAt, this.now())}` : '';
2010
+ this.appendSystemLine(`Plan: ${tierLabel} · ${month}${resetLine}`);
2011
+ const used = body.used ?? {};
2012
+ const caps = body.quotas ?? {};
2013
+ const counters = [
2014
+ ['sync', used.sync, caps.sync],
2015
+ ['review', used.review, caps.review],
2016
+ ['engine', used.engine, caps.engine],
2017
+ ];
2018
+ for (const [name, value, cap] of counters) {
2019
+ const v = typeof value === 'number' ? value : 0;
2020
+ if (cap === null || cap === undefined) {
2021
+ this.appendSystemLine(` ${name.padEnd(7, ' ')} ${v.toLocaleString()} / unlimited`);
2022
+ }
2023
+ else {
2024
+ const pct = cap > 0 ? Math.round((v / cap) * 100) : 0;
2025
+ this.appendSystemLine(` ${name.padEnd(7, ' ')} ${v.toLocaleString()} / ${cap.toLocaleString()} (${pct}%)`);
2026
+ }
2027
+ }
2028
+ }
2029
+ catch (error) {
2030
+ const msg = error instanceof Error ? error.message : String(error);
2031
+ this.appendSystemLine(`Could not fetch quota: ${msg}.`);
2032
+ }
2033
+ finally {
2034
+ clearTimeout(timer);
2035
+ }
2036
+ }
2037
+ /**
2038
+ * In-REPL `/status` — Leak L34 (2026-05-27). Surfaces the full
2039
+ * session snapshot (id + age, cwd, permission mode, CLI version,
2040
+ * tokens, dispatches, last cmd, compact boundaries, auth identity,
2041
+ * connection) by delegating к the same `runStatusCommand` the
2042
+ * top-level `pugi status` shell uses. Live REPL state (session
2043
+ * id, token totals, last operator command) flows in through the
2044
+ * context so the slash variant shows MORE than the shell path.
2045
+ *
2046
+ * The renderer routes к the system pane via `appendSystemLine`
2047
+ * so the snapshot lands as a single contiguous block в the
2048
+ * conversation transcript. Migrating к the Ink `<StatusTable>`
2049
+ * mounted directly в the REPL frame is a follow-up sprint —
2050
+ * keeping the line-buffered path here avoids cycling the
2051
+ * conversation pane's render model mid-α7.
2052
+ */
2053
+ async dispatchStatus() {
2054
+ try {
2055
+ const { runStatusCommand, defaultStatusHome } = await import('../../runtime/commands/status.js');
2056
+ // Find the most-recent operator transcript row + its timestamp
2057
+ // so the snapshot's `Last cmd` field has real content в REPL
2058
+ // mode. Walking от newest end is O(transcript) worst case but
2059
+ // bounded by MAX_TRANSCRIPT_ROWS so this stays cheap.
2060
+ let lastCommand = null;
2061
+ let lastCommandAtEpochMs = null;
2062
+ for (let i = this.state.transcript.length - 1; i >= 0; i -= 1) {
2063
+ const row = this.state.transcript[i];
2064
+ if (row.source === 'operator') {
2065
+ lastCommand = row.text;
2066
+ lastCommandAtEpochMs = row.timestampEpochMs;
2067
+ break;
2068
+ }
2069
+ }
2070
+ const liveTokens = this.state.sessionTokensIn + this.state.sessionTokensOut;
2071
+ const lines = [];
2072
+ await runStatusCommand({
2073
+ cwd: process.cwd(),
2074
+ home: defaultStatusHome(),
2075
+ env: process.env,
2076
+ json: false,
2077
+ liveSessionId: this.state.sessionId ?? null,
2078
+ sessionStartedAtEpochMs: this.state.sessionStartedAtEpochMs,
2079
+ liveTokensUsed: liveTokens >= 0 ? liveTokens : 0,
2080
+ lastCommand,
2081
+ lastCommandAtEpochMs,
2082
+ writeOutput: (_payload, text) => {
2083
+ for (const line of text.split('\n')) {
2084
+ const trimmed = line.replace(/\s+$/u, '');
2085
+ if (trimmed.length > 0)
2086
+ lines.push(trimmed);
2087
+ }
2088
+ },
2089
+ });
2090
+ if (lines.length === 0) {
2091
+ this.appendSystemLine('/status: no output.');
2092
+ return;
2093
+ }
2094
+ for (const line of lines)
2095
+ this.appendSystemLine(line);
2096
+ }
2097
+ catch (error) {
2098
+ const message = error instanceof Error ? error.message : String(error);
2099
+ this.appendSystemLine(`/status failed: ${message}`);
2100
+ }
957
2101
  }
958
2102
  /**
959
2103
  * α6.5 `/context` slash handler. Surfaces the three-tier context
@@ -1136,7 +2280,10 @@ export class ReplSession {
1136
2280
  this.appendSystemLine(capLine);
1137
2281
  }
1138
2282
  this.appendOperatorLine(brief);
1139
- this.patch({ briefStartedAtEpochMs: this.now() });
2283
+ // Reset `lastCompletedOutcome` so a fresh dispatch does not
2284
+ // inherit the prior turn's status-bar label (e.g. a stale
2285
+ // "replied" sticking around while the next dispatch is in flight).
2286
+ this.patch({ briefStartedAtEpochMs: this.now(), lastCompletedOutcome: null });
1140
2287
  // α6.9 + R3 P1 (Codex triple-review 2026-05-25): supersede the
1141
2288
  // prior dispatch when one is in flight. Steps in order:
1142
2289
  //
@@ -1463,6 +2610,7 @@ export class ReplSession {
1463
2610
  apiUrl: this.options.apiUrl,
1464
2611
  apiKey: this.options.apiKey,
1465
2612
  workspace: this.options.workspace,
2613
+ cyberZoo: this.options.cyberZoo,
1466
2614
  });
1467
2615
  this.patch({ sessionId, connection: 'connecting' });
1468
2616
  this.openStream();
@@ -1619,8 +2767,22 @@ export class ReplSession {
1619
2767
  }
1620
2768
  case 'agent.tokens': {
1621
2769
  const delta = event.tokensIn + event.tokensOut;
2770
+ // α7 cost-meter sprint — bind a client-side USD figure to this
2771
+ // frame. The model slug rides on the event (optional for back-
2772
+ // compat); the price ladder in `model-pricing.ts` falls back to
2773
+ // a Sonnet-tier rate when the slug is missing, so the meter is
2774
+ // always populated. Negative / NaN values are clamped to zero
2775
+ // inside `computeCostUsd` so a buggy upstream never credits the
2776
+ // meter.
2777
+ const deltaCostUsd = computeCostUsd(event.tokensIn, event.tokensOut, event.model);
1622
2778
  this.patch({
1623
2779
  tokensDownstreamTotal: this.state.tokensDownstreamTotal + delta,
2780
+ sessionTokensIn: this.state.sessionTokensIn + event.tokensIn,
2781
+ sessionTokensOut: this.state.sessionTokensOut + event.tokensOut,
2782
+ sessionCostUsd: this.state.sessionCostUsd + deltaCostUsd,
2783
+ turnTokensIn: this.state.turnTokensIn + event.tokensIn,
2784
+ turnTokensOut: this.state.turnTokensOut + event.tokensOut,
2785
+ turnCostUsd: this.state.turnCostUsd + deltaCostUsd,
1624
2786
  agents: this.state.agents.map((a) => a.taskId === event.taskId
1625
2787
  ? {
1626
2788
  ...a,
@@ -1640,10 +2802,36 @@ export class ReplSession {
1640
2802
  }
1641
2803
  this.askBuffer.delete(event.taskId);
1642
2804
  this.askBufferPending.delete(event.taskId);
2805
+ // Honour the work-done signal from admin-api.
2806
+ // `outcome === 'replied'` means the turn was a pure text reply
2807
+ // with no delegate XML and no tool call — render it as
2808
+ // "replied" so the operator can tell the difference between
2809
+ // "the orchestrator just talked" and "real work shipped".
2810
+ // Older servers omit the field; default to 'shipped' so the
2811
+ // existing wire stays back-compat.
2812
+ const completedStatus = event.outcome === 'replied' ? 'replied' : 'shipped';
1643
2813
  this.patch({
1644
2814
  agents: this.state.agents.map((a) => a.taskId === event.taskId
1645
- ? { ...a, status: 'shipped', detail: 'shipped' }
2815
+ ? { ...a, status: completedStatus, detail: completedStatus }
1646
2816
  : a),
2817
+ // Mirror the outcome to top-level state so the status-bar
2818
+ // can render `replied` instead of the legacy `shipped`
2819
+ // label when the FSM lands in `completed`. Without this
2820
+ // the bottom-bar would still say "shipped" while the
2821
+ // agent-tree said "replied", restoring the same
2822
+ // contradiction this PR is fixing (Codex triple-review P2).
2823
+ //
2824
+ // r2: gate on the same stale-dispatch check that
2825
+ // advanceFsmOnDispatchEnd applies. If this completion
2826
+ // belongs to a SUPERSEDED dispatch (a newer dispatchBrief
2827
+ // already bumped dispatchSeq before this late terminal
2828
+ // arrived), don't let the status-bar label flip to the
2829
+ // stale outcome — the current turn is the live one.
2830
+ // The agent-tree row patch above is still safe because
2831
+ // it only updates the row keyed by taskId.
2832
+ ...(this.isStaleTaskEvent(event.taskId)
2833
+ ? {}
2834
+ : { lastCompletedOutcome: completedStatus }),
1647
2835
  });
1648
2836
  // α6.9: transition the FSM to `completed` when no other
1649
2837
  // dispatch is still in flight. The check uses the agents list
@@ -1651,6 +2839,12 @@ export class ReplSession {
1651
2839
  // the dispatch alive; the FSM only goes terminal when the last
1652
2840
  // agent ships.
1653
2841
  this.advanceFsmOnDispatchEnd('completed', 'agent_completed', event.taskId);
2842
+ // α7 cost-meter sprint — flush the per-turn delta when the
2843
+ // LAST agent settles. Decoupled from the FSM gate so a test
2844
+ // fixture (or a single-agent dispatch that never reached
2845
+ // `awaiting_response` — happens on instant SSE replay) still
2846
+ // gets the row written into recentTurns + lastTurnDelta.
2847
+ this.maybeFlushTurnOnAgentSettle(event.taskId);
1654
2848
  if (target) {
1655
2849
  // If the persona actually produced a reply via incremental
1656
2850
  // agent.step events, render that reply in the transcript so
@@ -1716,6 +2910,10 @@ export class ReplSession {
1716
2910
  // operator sees the bottom-bar settle back to `idle` after the
1717
2911
  // last block clears.
1718
2912
  this.advanceFsmOnDispatchEnd('completed', 'agent_blocked', event.taskId);
2913
+ // α7 cost-meter sprint — flush the per-turn delta (blocked
2914
+ // still counts as a billable turn — the operator paid for the
2915
+ // tokens that landed before the refusal).
2916
+ this.maybeFlushTurnOnAgentSettle(event.taskId);
1719
2917
  return;
1720
2918
  }
1721
2919
  case 'agent.failed': {
@@ -1739,6 +2937,10 @@ export class ReplSession {
1739
2937
  // `completed` so the bottom-bar surface tracks the dispatch
1740
2938
  // collectively.
1741
2939
  this.advanceFsmOnDispatchEnd('failed', 'agent_failed', event.taskId);
2940
+ // α7 cost-meter sprint — flush the per-turn delta when the
2941
+ // dispatch fails (the operator still paid for whatever tokens
2942
+ // landed before the failure).
2943
+ this.maybeFlushTurnOnAgentSettle(event.taskId);
1742
2944
  return;
1743
2945
  }
1744
2946
  }
@@ -1777,13 +2979,25 @@ export class ReplSession {
1777
2979
  * after a manual `cancel()` finds the FSM already in `aborted` and
1778
2980
  * is silently dropped.
1779
2981
  */
2982
+ /**
2983
+ * 2026-05-26 — shared stale-task check used by both the FSM advance
2984
+ * gate AND the status-bar `lastCompletedOutcome` mirror. Lifts the
2985
+ * R2 dispatchSeq compare out of `advanceFsmOnDispatchEnd` so other
2986
+ * agent.completed-handler side-effects (status-bar label, future
2987
+ * metric counters) can apply the same guard without duplicating it.
2988
+ * Returns true iff the task's stamped dispatchSeq is older than the
2989
+ * current dispatchSeq — i.e. a newer dispatchBrief() superseded it
2990
+ * and the late terminal event must not corrupt live-turn state.
2991
+ */
2992
+ isStaleTaskEvent(taskId) {
2993
+ const taskSeq = this.taskDispatchSeq.get(taskId);
2994
+ return taskSeq !== undefined && taskSeq < this.dispatchSeq;
2995
+ }
1780
2996
  advanceFsmOnDispatchEnd(outcome, reason, taskId) {
1781
2997
  // R2 P1 fix (Codex triple-review 2026-05-25): a terminal event
1782
2998
  // for a SUPERSEDED dispatch must NOT advance the live FSM or null
1783
- // the live token. If the event carries a taskId and the stamped
1784
- // dispatchSeq for that task is older than the current dispatchSeq,
1785
- // the event belongs to a prior dispatch that was replaced by a
1786
- // newer `dispatchBrief()`. Silently drop the FSM advance.
2999
+ // the live token. Delegates to isStaleTaskEvent so the agent.completed
3000
+ // status-bar mirror in the handler above uses the same gate.
1787
3001
  if (taskId !== undefined) {
1788
3002
  const taskSeq = this.taskDispatchSeq.get(taskId);
1789
3003
  if (taskSeq !== undefined && taskSeq < this.dispatchSeq) {
@@ -1815,6 +3029,63 @@ export class ReplSession {
1815
3029
  this.currentDispatchToken = null;
1816
3030
  this.patch({ briefStartedAtEpochMs: undefined });
1817
3031
  }
3032
+ /**
3033
+ * α7 cost-meter sprint — gate the per-turn flush on "this was the
3034
+ * LAST in-flight agent". Mirrors the `stillActive` guard inside
3035
+ * `advanceFsmOnDispatchEnd` so a multi-agent dispatch only emits a
3036
+ * single recentTurns row + a single lastTurnDelta flash.
3037
+ *
3038
+ * Idempotent: if no tokens have been billed this turn, the inner
3039
+ * `flushTurnAccumulator` short-circuits without pushing an empty row.
3040
+ */
3041
+ maybeFlushTurnOnAgentSettle(taskId) {
3042
+ const stillActive = this.state.agents.some((a) => a.status === 'queued' || a.status === 'thinking');
3043
+ if (stillActive)
3044
+ return;
3045
+ this.flushTurnAccumulator(taskId);
3046
+ }
3047
+ /**
3048
+ * α7 cost-meter sprint — flush the per-turn accumulator into
3049
+ * `recentTurns` + `lastTurnDelta`. Idempotent + safe to call from any
3050
+ * terminal-state branch (`agent.completed` / `agent.blocked` /
3051
+ * `agent.failed`). When no tokens have been billed this turn
3052
+ * (instant abort, cap-warning gate), the helper short-circuits
3053
+ * without pushing an empty row.
3054
+ */
3055
+ flushTurnAccumulator(taskId) {
3056
+ const turnTokensIn = this.state.turnTokensIn;
3057
+ const turnTokensOut = this.state.turnTokensOut;
3058
+ const turnCostUsd = this.state.turnCostUsd;
3059
+ if (turnTokensIn === 0 && turnTokensOut === 0) {
3060
+ // Idempotent zero-flush — never push an empty row into recentTurns.
3061
+ return;
3062
+ }
3063
+ const turnId = taskId !== undefined ? taskId : `turn-${this.dispatchSeq}-${this.now()}`;
3064
+ const newTurn = {
3065
+ id: turnId,
3066
+ tokensIn: turnTokensIn,
3067
+ tokensOut: turnTokensOut,
3068
+ costUsd: turnCostUsd,
3069
+ completedAt: new Date(this.now()).toISOString(),
3070
+ };
3071
+ // Keep the buffer capped at 5 entries (oldest first). The push
3072
+ // order matches the surface contract: `/cost` paginates oldest →
3073
+ // newest so the operator scans top-down chronologically.
3074
+ const recent = [...this.state.recentTurns, newTurn];
3075
+ const trimmed = recent.length > 5 ? recent.slice(-5) : recent;
3076
+ this.patch({
3077
+ recentTurns: trimmed,
3078
+ lastTurnDelta: {
3079
+ tokensIn: turnTokensIn,
3080
+ tokensOut: turnTokensOut,
3081
+ costUsd: turnCostUsd,
3082
+ completedAtEpochMs: this.now(),
3083
+ },
3084
+ turnTokensIn: 0,
3085
+ turnTokensOut: 0,
3086
+ turnCostUsd: 0,
3087
+ });
3088
+ }
1818
3089
  /* ------------- transcript helpers -------------- */
1819
3090
  /**
1820
3091
  * Look up the persona slug for a running task. Used by the tool call
@@ -1871,13 +3142,14 @@ export class ReplSession {
1871
3142
  this.appendRow({ source: 'persona', text: stripped, personaSlug });
1872
3143
  }
1873
3144
  appendRow(input) {
1874
- if (input.text.length === 0)
3145
+ if (input.text.length === 0 && input.source !== 'compact-boundary')
1875
3146
  return;
1876
3147
  const row = {
1877
3148
  id: randomUUID(),
1878
3149
  source: input.source,
1879
3150
  text: input.text,
1880
3151
  personaSlug: input.personaSlug,
3152
+ compaction: input.compaction,
1881
3153
  timestampEpochMs: this.now(),
1882
3154
  };
1883
3155
  const next = this.state.transcript.concat(row).slice(-MAX_TRANSCRIPT_ROWS);
@@ -1890,6 +3162,62 @@ export class ReplSession {
1890
3162
  // persona -> 'persona'
1891
3163
  // system -> 'system'
1892
3164
  this.persistRow(row);
3165
+ // Leak L8 (2026-05-27): evaluate the auto-compact gate after
3166
+ // every appendRow that produces a transcript turn. Wrapped in a
3167
+ // setImmediate so the gate never blocks the input-handling fast
3168
+ // path; if the threshold is tripped, the auto-trigger dispatches
3169
+ // `/compact` in the background while the operator keeps typing.
3170
+ if (row.source === 'operator' || row.source === 'persona') {
3171
+ this.maybeAutoCompact();
3172
+ }
3173
+ }
3174
+ /**
3175
+ * Auto-compact gate. Cheap: builds an in-memory token estimate from
3176
+ * the current transcript and consults `evaluateAutoCompact`. When the
3177
+ * gate fires AND a compaction is not already in flight, we dispatch
3178
+ * `/compact` with `trigger='auto'`. The fire-and-forget shape means
3179
+ * the input box stays responsive while the background round-trip
3180
+ * runs.
3181
+ *
3182
+ * Hysteresis: `compactionInFlight` blocks re-entry. The gate is
3183
+ * cleared when the dispatch promise resolves regardless of outcome
3184
+ * so a transient transport failure does not permanently disable the
3185
+ * auto-trigger.
3186
+ */
3187
+ compactionInFlight = false;
3188
+ maybeAutoCompact() {
3189
+ if (this.compactionInFlight)
3190
+ return;
3191
+ if (!this.store || !this.localSessionId)
3192
+ return;
3193
+ if (process.env['PUGI_AUTOCOMPACT_DISABLED'] === '1')
3194
+ return;
3195
+ // Token estimate from the in-memory transcript. The estimate is a
3196
+ // lower bound on actual context pressure (server-side system
3197
+ // prompts add overhead) but the 4-char/token heuristic plus the
3198
+ // 0.75 default threshold gives generous headroom.
3199
+ const texts = this.state.transcript.map((r) => r.text);
3200
+ const tokenCount = estimateTokensInMany(texts);
3201
+ // Conservative default: assume the smallest commonly-used window
3202
+ // (32k tokens for deepseek-v3.1). Resolving the live model slug
3203
+ // through DispatchFSM + admin-api adds latency on a hot path; the
3204
+ // 0.75 threshold + smallest-window assumption errs toward
3205
+ // EARLY trigger which is the safe direction.
3206
+ const verdict = evaluateAutoCompact({
3207
+ tokenCount,
3208
+ windowSize: 32_000,
3209
+ });
3210
+ if (verdict.kind !== 'fire')
3211
+ return;
3212
+ this.compactionInFlight = true;
3213
+ void (async () => {
3214
+ try {
3215
+ await this.dispatchCompact('auto');
3216
+ }
3217
+ finally {
3218
+ this.compactionInFlight = false;
3219
+ }
3220
+ })();
1893
3221
  }
1894
3222
  /**
1895
3223
  * Best-effort write of one transcript row into the local
@@ -1900,6 +3228,15 @@ export class ReplSession {
1900
3228
  persistRow(row) {
1901
3229
  if (!this.store)
1902
3230
  return;
3231
+ // L29 (2026-05-27): `compact-boundary` transcript rows are echoes of
3232
+ // the JSONL `compaction` event the compact runner already appended
3233
+ // via `appendCompactBoundary`. Persisting them here would double-
3234
+ // write the marker (and worse, with a stripped payload that lacks
3235
+ // `summary` / `coversUntilOffset`) — `isCompactBoundary` would
3236
+ // reject the duplicate but `applyCompactMask` would still index off
3237
+ // the wrong offset. Skip the write.
3238
+ if (row.source === 'compact-boundary')
3239
+ return;
1903
3240
  const kind = row.source === 'operator' ? 'user'
1904
3241
  : row.source === 'persona' ? 'persona'
1905
3242
  : 'system';
@@ -1940,12 +3277,30 @@ export class ReplSession {
1940
3277
  * write the restored events.
1941
3278
  */
1942
3279
  restoreTranscript(events) {
3280
+ // Leak L8 (2026-05-27): apply compact-boundary masking BEFORE the
3281
+ // row conversion. Events strictly before the latest marker are
3282
+ // condensed into the boundary's `keptTailTurns + marker` slice so
3283
+ // the post-resume transcript starts at the most-recent context
3284
+ // floor rather than re-playing the full pre-compaction history.
3285
+ //
3286
+ // Leak L9 (2026-05-27): then apply rewind-marker masking. Any
3287
+ // event inside an active rewind range is stripped from the
3288
+ // visible transcript; the on-disk events stay durable so a
3289
+ // follow-up `pugi sessions undo-rewind` can restore them.
3290
+ const masked = applyRewindMask(applyCompactMask(events));
1943
3291
  const rows = [];
1944
- for (const event of events) {
3292
+ for (const event of masked) {
1945
3293
  const row = eventToTranscriptRow(event);
1946
3294
  if (row)
1947
3295
  rows.push(row);
1948
3296
  }
3297
+ // L29 (2026-05-27): tag each compact-boundary row with the count of
3298
+ // operator + persona turns that landed AFTER it in the replay
3299
+ // window. The banner reads `turnsAgo` to render the "N turns ago"
3300
+ // suffix so a long session that resumes across multiple compactions
3301
+ // stays self-orienting. System rows + sibling boundaries are NOT
3302
+ // counted — they are chrome, not operator-visible turns.
3303
+ annotateBoundaryTurnsAgo(rows);
1949
3304
  // Cap at MAX_TRANSCRIPT_ROWS - the same cap appendRow uses so the
1950
3305
  // window math stays consistent post-restore.
1951
3306
  const capped = rows.slice(-MAX_TRANSCRIPT_ROWS);
@@ -2129,8 +3484,71 @@ function eventToTranscriptRow(event) {
2129
3484
  timestampEpochMs: event.t,
2130
3485
  };
2131
3486
  }
3487
+ if (event.kind === 'compaction') {
3488
+ // L8 + L29 (2026-05-27): render the marker as a structured
3489
+ // `compact-boundary` row so the renderer can route it to the
3490
+ // dedicated <CompactBanner /> Ink component. The full summary text
3491
+ // is intentionally NOT inlined here (a 2k-token summary in the
3492
+ // transcript would defeat the purpose of compacting); the operator
3493
+ // sees the "context compacted" banner and can run `/context` to
3494
+ // inspect the marker payload when they want the details. The plain
3495
+ // text fallback stays in place for non-Ink consumers (snapshot
3496
+ // tests, future JSON exports).
3497
+ const compactionPayload = (event.payload ?? null);
3498
+ const trigger = compactionPayload?.trigger === 'auto' ? 'auto' : 'manual';
3499
+ const turns = typeof compactionPayload?.summaryTurnsBefore === 'number'
3500
+ ? compactionPayload.summaryTurnsBefore
3501
+ : 0;
3502
+ const tokens = typeof compactionPayload?.summaryTokenCount === 'number'
3503
+ ? compactionPayload.summaryTokenCount
3504
+ : undefined;
3505
+ return {
3506
+ id: randomUUID(),
3507
+ source: 'compact-boundary',
3508
+ text: `─── context compacted (${turns} turns → 1 summary, ${trigger}) ───`,
3509
+ compaction: {
3510
+ turnsBefore: turns,
3511
+ trigger,
3512
+ summaryTokenCount: tokens,
3513
+ },
3514
+ timestampEpochMs: event.t,
3515
+ };
3516
+ }
2132
3517
  return null;
2133
3518
  }
3519
+ /**
3520
+ * L29 (2026-05-27): walk a chronological transcript window and stamp
3521
+ * every `compact-boundary` row's `compaction.turnsAgo` with the count of
3522
+ * operator + persona rows that land AFTER it. The annotation runs in
3523
+ * place on the array — boundaries earlier in time get larger `turnsAgo`
3524
+ * values, the boundary at the head of the window gets zero. System rows
3525
+ * and sibling boundaries are excluded from the count (they are chrome,
3526
+ * not operator-visible turns).
3527
+ *
3528
+ * Exported so a future spec can lock the contract and so the in-REPL
3529
+ * `/compact` path can reuse the same counter on live appends if it ever
3530
+ * needs to. Pure function (mutates only the input slice).
3531
+ */
3532
+ export function annotateBoundaryTurnsAgo(rows) {
3533
+ let trailingTurns = 0;
3534
+ for (let i = rows.length - 1; i >= 0; i -= 1) {
3535
+ const row = rows[i];
3536
+ if (row.source === 'operator' || row.source === 'persona') {
3537
+ trailingTurns += 1;
3538
+ continue;
3539
+ }
3540
+ if (row.source === 'compact-boundary') {
3541
+ // Re-assign with the live `turnsAgo`. Carry forward the existing
3542
+ // structured payload so we never lose the trigger / token-count
3543
+ // data the renderer needs.
3544
+ const compaction = row.compaction ?? { turnsBefore: 0, trigger: 'manual' };
3545
+ rows[i] = {
3546
+ ...row,
3547
+ compaction: { ...compaction, turnsAgo: trailingTurns },
3548
+ };
3549
+ }
3550
+ }
3551
+ }
2134
3552
  /**
2135
3553
  * Heuristic: does this text contain Markdown structures that benefit
2136
3554
  * from atomic grouping? Code fences, bullet lists, numbered lists,
@@ -2197,6 +3615,62 @@ function formatAgeSeconds(deltaMs) {
2197
3615
  export function knownRoles() {
2198
3616
  return listRoles();
2199
3617
  }
3618
+ /**
3619
+ * α7 cost-meter sprint — render a session-elapsed ms delta as the
3620
+ * status-row's compact `XmYs` / `XhYm` shape. Distinct from
3621
+ * `formatAgeSeconds` above because `/cost` needs minute-granularity
3622
+ * uniformly (operator wants `2m44s`, not `2m`). Pure / branch-cheap;
3623
+ * the TUI status row + `/cost` both call this on every render.
3624
+ */
3625
+ function formatElapsedShort(elapsedMs) {
3626
+ if (!Number.isFinite(elapsedMs) || elapsedMs <= 0)
3627
+ return '0s';
3628
+ const totalSec = Math.floor(elapsedMs / 1000);
3629
+ if (totalSec < 60)
3630
+ return `${totalSec}s`;
3631
+ const min = Math.floor(totalSec / 60);
3632
+ const sec = totalSec % 60;
3633
+ if (min < 60)
3634
+ return `${min}m${sec.toString().padStart(2, '0')}s`;
3635
+ const hr = Math.floor(min / 60);
3636
+ const restMin = min % 60;
3637
+ return `${hr}h${restMin.toString().padStart(2, '0')}m`;
3638
+ }
3639
+ /**
3640
+ * α7 cost-meter sprint — public-facing tier labels for the `/quota`
3641
+ * slash. Mirrors `TIER_PRICE_LABEL` in `runtime/cli.ts` (kept in sync
3642
+ * via `pricing.spec.ts` gate). Falls through to the raw slug when an
3643
+ * unknown tier ships from a forward-compat admin-api build.
3644
+ */
3645
+ const QUOTA_TIER_LABELS = Object.freeze({
3646
+ free: 'Free',
3647
+ founder: 'Founder ($20/mo)',
3648
+ builder: 'Builder ($99/mo)',
3649
+ team: 'Team ($199/mo)',
3650
+ });
3651
+ /**
3652
+ * α7 cost-meter sprint — render the time-until-reset window for the
3653
+ * `/quota` plan line. `resetAt` is the ISO string admin-api returns;
3654
+ * `now` is the current epoch ms (injected for test determinism). Falls
3655
+ * back to the raw ISO string when parsing fails so the operator never
3656
+ * sees an empty hint.
3657
+ */
3658
+ function formatResetWindow(resetAtIso, nowEpochMs) {
3659
+ const resetMs = Date.parse(resetAtIso);
3660
+ if (!Number.isFinite(resetMs))
3661
+ return resetAtIso;
3662
+ const deltaMs = resetMs - nowEpochMs;
3663
+ if (deltaMs <= 0)
3664
+ return 'now';
3665
+ const days = Math.floor(deltaMs / (24 * 60 * 60 * 1000));
3666
+ if (days >= 2)
3667
+ return `in ${days}d`;
3668
+ const hours = Math.floor(deltaMs / (60 * 60 * 1000));
3669
+ if (hours >= 1)
3670
+ return `in ${hours}h`;
3671
+ const minutes = Math.max(1, Math.floor(deltaMs / (60 * 1000)));
3672
+ return `in ${minutes}m`;
3673
+ }
2200
3674
  /* ------------------------------------------------------------------ */
2201
3675
  /* Tool call synthesiser - α6.12 */
2202
3676
  /* ------------------------------------------------------------------ */
@@ -2230,7 +3704,7 @@ export function synthesiseToolCall(input) {
2230
3704
  // Pattern: ToolName(args) optionally suffixed with a result hint.
2231
3705
  // We allow the canonical Claude Code casing AND the snake_case
2232
3706
  // alias `web_fetch` so the synthesiser matches what personas write.
2233
- const match = /^(Read|Edit|Bash|Grep|Glob|WebFetch|web_fetch)\s*\(\s*([^)]*)\s*\)\s*(.*)$/i
3707
+ const match = /^(Read|Write|Edit|Bash|Grep|Glob|WebFetch|web_fetch)\s*\(\s*([^)]*)\s*\)\s*(.*)$/i
2234
3708
  .exec(detail);
2235
3709
  if (!match)
2236
3710
  return null;
@@ -2254,6 +3728,8 @@ function normaliseToolName(raw) {
2254
3728
  return 'web_fetch';
2255
3729
  if (lower === 'read')
2256
3730
  return 'read';
3731
+ if (lower === 'write')
3732
+ return 'write';
2257
3733
  if (lower === 'edit')
2258
3734
  return 'edit';
2259
3735
  if (lower === 'bash')
@@ -2479,7 +3955,22 @@ export function stripPersonaPrefixEcho(personaSlug, text) {
2479
3955
  // Escape regex specials in the display name even though THE_TEN
2480
3956
  // names are alpha-only today (forward-defense).
2481
3957
  const escaped = display.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
3958
+ // Match `<DisplayName>` (case-insensitive) followed by EITHER:
3959
+ // - an end-of-string, OR
3960
+ // - a separator (whitespace / comma / colon / dash / period+space).
3961
+ // The `i` flag is needed so a model writing "PUGI:" or "pugi," still
3962
+ // strips. After this match the post-fix `noSepUppercaseRe` handles
3963
+ // the "PugiПринял" / "PugiHello" no-separator emission pattern
3964
+ // (CEO red-alert 2026-05-27) using a SEPARATE regex without the `i`
3965
+ // flag so the lookahead is case-strict (Pugineous must NOT strip).
2482
3966
  const re = new RegExp(`^${escaped}(?:[\\s,:;\\-—–]+|$)`, 'i');
3967
+ // No-separator case-strict matcher. Display name in either of its
3968
+ // canonical casings ("Pugi" / "PUGI") immediately followed by an
3969
+ // uppercase Cyrillic or Latin letter. The strip is intentionally
3970
+ // narrower than the case-insensitive `re` above because a lowercase
3971
+ // continuation ("Pugineous") is a single word, not a display-name
3972
+ // echo - we must not eat real content.
3973
+ const noSepUppercaseRe = new RegExp(`^(?:${escaped}|${escaped.toUpperCase()})(?=[А-ЯЁA-Z])`);
2483
3974
  // Loop the strip so cascading echoes ("Pugi Pugi Pugi, координатор ...")
2484
3975
  // collapse to a single name. The model occasionally emits the display
2485
3976
  // name two or three times back-to-back when the pane prefix also
@@ -2491,10 +3982,18 @@ export function stripPersonaPrefixEcho(personaSlug, text) {
2491
3982
  // matches an empty string (defence-in-depth even though the current
2492
3983
  // pattern guarantees at least one consumed char).
2493
3984
  for (let i = 0; i < 3; i += 1) {
2494
- const m = re.exec(working);
2495
- if (!m || m[0].length === 0)
2496
- break;
2497
- working = working.slice(m[0].length).trimStart();
3985
+ let m = re.exec(working);
3986
+ if (m && m[0].length > 0) {
3987
+ working = working.slice(m[0].length).trimStart();
3988
+ continue;
3989
+ }
3990
+ // Fallback: no-separator match for "PugiПринял" / "PugiHello" shape.
3991
+ m = noSepUppercaseRe.exec(working);
3992
+ if (m && m[0].length > 0) {
3993
+ working = working.slice(m[0].length);
3994
+ continue;
3995
+ }
3996
+ break;
2498
3997
  }
2499
3998
  return working;
2500
3999
  }