@pugi/cli 0.1.0-beta.8 → 0.1.0-beta.87

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 (402) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/THIRD_PARTY_NOTICES.md +40 -0
  3. package/assets/pugi-prozr2-mascot.ansi +9 -0
  4. package/bin/run.js +33 -1
  5. package/dist/commands/deploy.js +40 -40
  6. package/dist/commands/flatten.js +191 -0
  7. package/dist/commands/jobs-watch.js +201 -0
  8. package/dist/commands/jobs.js +42 -27
  9. package/dist/commands/smoke.js +133 -0
  10. package/dist/core/agent-progress/cleanup.js +134 -0
  11. package/dist/core/agent-progress/schema.js +144 -0
  12. package/dist/core/agent-progress/writer.js +101 -0
  13. package/dist/core/agents/adaptive-router.js +330 -0
  14. package/dist/core/agents/query-decomposer.js +297 -0
  15. package/dist/core/agents/registry.js +2 -2
  16. package/dist/core/approvals/shortcut-resolver.js +98 -0
  17. package/dist/core/artifact-chain/dispatcher.js +148 -0
  18. package/dist/core/artifact-chain/exporter.js +164 -0
  19. package/dist/core/artifact-chain/state.js +243 -0
  20. package/dist/core/artifact-chain/steps.js +169 -0
  21. package/dist/core/ask-user/question.js +92 -0
  22. package/dist/core/audit/audit-trail.js +275 -0
  23. package/dist/core/auth/ensure-authenticated.js +129 -0
  24. package/dist/core/auth/env-provider.js +238 -0
  25. package/dist/core/auto-open-browser.js +4 -4
  26. package/dist/core/auto-update/channels.js +122 -0
  27. package/dist/core/auto-update/checker.js +241 -0
  28. package/dist/core/auto-update/state.js +235 -0
  29. package/dist/core/bare-mode/index.js +107 -0
  30. package/dist/core/bash/redirect.js +281 -0
  31. package/dist/core/bash-classifier.js +436 -40
  32. package/dist/core/checkpoint/resumer.js +149 -0
  33. package/dist/core/checkpoint/rewinder.js +291 -0
  34. package/dist/core/checkpoints/shadow-git.js +670 -0
  35. package/dist/core/citations/parser.js +109 -0
  36. package/dist/core/classifier/yolo-classifier.js +88 -0
  37. package/dist/core/codegraph/decision-store.js +248 -0
  38. package/dist/core/codegraph/detect-repo.js +459 -0
  39. package/dist/core/codegraph/install.js +134 -0
  40. package/dist/core/codegraph/offer-hook.js +220 -0
  41. package/dist/core/compact/auto-trigger.js +96 -0
  42. package/dist/core/compact/buffer-rewriter.js +115 -0
  43. package/dist/core/compact/summarizer.js +208 -0
  44. package/dist/core/compact/token-counter.js +108 -0
  45. package/dist/core/consensus/anvil-fanout.js +25 -25
  46. package/dist/core/consensus/diff-capture.js +121 -12
  47. package/dist/core/consensus/rubric.js +21 -21
  48. package/dist/core/context/builder.js +6 -6
  49. package/dist/core/context/compaction-events.js +8 -8
  50. package/dist/core/context/compaction.js +31 -31
  51. package/dist/core/context/index.js +15 -8
  52. package/dist/core/context/invariants.js +51 -51
  53. package/dist/core/context/markdown-loader.js +28 -10
  54. package/dist/core/context/markdown-traverse.js +255 -0
  55. package/dist/core/context/pugiignore.js +41 -41
  56. package/dist/core/context/repo-skeleton.js +37 -37
  57. package/dist/core/context/tool-eviction.js +55 -0
  58. package/dist/core/context/watcher.js +32 -32
  59. package/dist/core/context/working-set.js +23 -23
  60. package/dist/core/coordinator/agent-tools.js +77 -0
  61. package/dist/core/coordinator/agent-toolset.js +65 -0
  62. package/dist/core/coordinator/fsm.js +73 -0
  63. package/dist/core/coordinator/mode-fsm.js +70 -0
  64. package/dist/core/cost/rate-card.js +129 -0
  65. package/dist/core/cost/tracker.js +221 -0
  66. package/dist/core/credentials.js +12 -12
  67. package/dist/core/cron/scheduler.js +138 -0
  68. package/dist/core/denial-tracking/index.js +8 -0
  69. package/dist/core/denial-tracking/state.js +264 -0
  70. package/dist/core/diagnostics/probe-runner.js +93 -0
  71. package/dist/core/diagnostics/probes/api.js +46 -0
  72. package/dist/core/diagnostics/probes/auth.js +93 -0
  73. package/dist/core/diagnostics/probes/bare-mode.js +42 -0
  74. package/dist/core/diagnostics/probes/cli-version.js +127 -0
  75. package/dist/core/diagnostics/probes/config.js +72 -0
  76. package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
  77. package/dist/core/diagnostics/probes/disk.js +81 -0
  78. package/dist/core/diagnostics/probes/engine-live.js +46 -0
  79. package/dist/core/diagnostics/probes/git.js +65 -0
  80. package/dist/core/diagnostics/probes/hooks.js +118 -0
  81. package/dist/core/diagnostics/probes/mcp.js +75 -0
  82. package/dist/core/diagnostics/probes/node.js +59 -0
  83. package/dist/core/diagnostics/probes/pnpm.js +36 -0
  84. package/dist/core/diagnostics/probes/pugi-md.js +89 -0
  85. package/dist/core/diagnostics/probes/sandbox.js +40 -0
  86. package/dist/core/diagnostics/probes/session.js +74 -0
  87. package/dist/core/diagnostics/probes/status-snapshot.js +488 -0
  88. package/dist/core/diagnostics/probes/workspace.js +63 -0
  89. package/dist/core/diagnostics/types.js +70 -0
  90. package/dist/core/dispatch/cache-cleanup.js +197 -0
  91. package/dist/core/dispatch/cache-handoff.js +295 -0
  92. package/dist/core/edits/apply-patch-layer-e.js +189 -0
  93. package/dist/core/edits/dispatch.js +293 -7
  94. package/dist/core/edits/format-matrix.js +26 -0
  95. package/dist/core/edits/fuzzy-ladder.js +650 -0
  96. package/dist/core/edits/index.js +3 -1
  97. package/dist/core/edits/journal.js +199 -0
  98. package/dist/core/edits/layer-a-apply.js +15 -15
  99. package/dist/core/edits/layer-a-fuzzy-apply.js +198 -0
  100. package/dist/core/edits/layer-b-apply.js +9 -9
  101. package/dist/core/edits/layer-c-apply.js +6 -6
  102. package/dist/core/edits/layer-d-ast.js +557 -14
  103. package/dist/core/edits/marker-parser.js +12 -12
  104. package/dist/core/edits/security-gate.js +27 -27
  105. package/dist/core/edits/verify-hook.js +273 -0
  106. package/dist/core/edits/worktree.js +322 -0
  107. package/dist/core/engine/anvil-client.js +140 -26
  108. package/dist/core/engine/auto-compact.js +179 -0
  109. package/dist/core/engine/budgets.js +186 -0
  110. package/dist/core/engine/context-prefix.js +155 -0
  111. package/dist/core/engine/index.js +1 -1
  112. package/dist/core/engine/intensity.js +158 -0
  113. package/dist/core/engine/intent.js +260 -0
  114. package/dist/core/engine/native-pugi.js +1295 -227
  115. package/dist/core/engine/prompts.js +134 -16
  116. package/dist/core/engine/strip-internal-fields.js +124 -0
  117. package/dist/core/engine/tool-bridge.js +1295 -59
  118. package/dist/core/evaluation/golden-dataset.js +293 -0
  119. package/dist/core/feedback/queue.js +177 -0
  120. package/dist/core/feedback/submitter.js +145 -0
  121. package/dist/core/file-cache.js +113 -1
  122. package/dist/core/flatten/flatten-repo.js +439 -0
  123. package/dist/core/format/osc8-link.js +28 -0
  124. package/dist/core/hook-chains.js +392 -0
  125. package/dist/core/hooks/citation-verify-hook.js +138 -0
  126. package/dist/core/hooks/citation-verify.js +112 -0
  127. package/dist/core/hooks/events.js +44 -0
  128. package/dist/core/hooks/index.js +15 -0
  129. package/dist/core/hooks/registry.js +213 -0
  130. package/dist/core/hooks/runner.js +236 -0
  131. package/dist/core/hooks/v2/event-emitter.js +115 -0
  132. package/dist/core/hooks/v2/executor.js +282 -0
  133. package/dist/core/hooks/v2/index.js +25 -0
  134. package/dist/core/hooks/v2/lifecycle.js +104 -0
  135. package/dist/core/hooks/v2/loader.js +216 -0
  136. package/dist/core/hooks/v2/matcher.js +125 -0
  137. package/dist/core/hooks/v2/trust.js +143 -0
  138. package/dist/core/hooks/v2/types.js +86 -0
  139. package/dist/core/image/renderer.js +71 -0
  140. package/dist/core/init/detector.js +582 -0
  141. package/dist/core/init/template-renderer.js +242 -0
  142. package/dist/core/jobs/registry.js +18 -18
  143. package/dist/core/ledger/results-tsv.js +142 -0
  144. package/dist/core/log-discipline/stdout-redirect.js +51 -0
  145. package/dist/core/lsp/cache.js +105 -0
  146. package/dist/core/lsp/client.js +776 -0
  147. package/dist/core/lsp/language-detect.js +66 -0
  148. package/dist/core/lsp/post-edit-diagnostics.js +171 -0
  149. package/dist/core/lsp/symbol-tools.js +372 -0
  150. package/dist/core/mcp/client.js +97 -28
  151. package/dist/core/mcp/http-server.js +553 -0
  152. package/dist/core/mcp/orchestrator-tools.js +662 -0
  153. package/dist/core/mcp/permission.js +190 -0
  154. package/dist/core/mcp/registry.js +39 -17
  155. package/dist/core/mcp/server-tools.js +219 -0
  156. package/dist/core/mcp/server.js +397 -0
  157. package/dist/core/mcp/trust.js +10 -10
  158. package/dist/core/memory/dual-write.js +416 -0
  159. package/dist/core/memory/passive-extract.js +130 -0
  160. package/dist/core/memory/phase1-kinds.js +20 -0
  161. package/dist/core/memory/secret-scanner.js +304 -0
  162. package/dist/core/memory-sync/queue.js +170 -0
  163. package/dist/core/metrics/extract.js +113 -0
  164. package/dist/core/modes/roo-modes.js +68 -0
  165. package/dist/core/onboarding/ensure-initialized.js +133 -0
  166. package/dist/core/onboarding/marker.js +111 -0
  167. package/dist/core/onboarding/telemetry-state.js +108 -0
  168. package/dist/core/output-style/presets.js +176 -0
  169. package/dist/core/output-style/state.js +185 -0
  170. package/dist/core/path-security.js +287 -5
  171. package/dist/core/permission.js +82 -22
  172. package/dist/core/permissions/auto-classifier.js +124 -0
  173. package/dist/core/permissions/bash-parser.js +371 -0
  174. package/dist/core/permissions/circuit-breaker.js +83 -0
  175. package/dist/core/permissions/constrained-edit.js +91 -0
  176. package/dist/core/permissions/gate.js +278 -0
  177. package/dist/core/permissions/index.js +20 -0
  178. package/dist/core/permissions/mode.js +174 -0
  179. package/dist/core/permissions/network-egress.js +137 -0
  180. package/dist/core/permissions/state.js +241 -0
  181. package/dist/core/permissions/tool-class.js +93 -0
  182. package/dist/core/plan-mode/ui-state.js +51 -0
  183. package/dist/core/plans/plan-artifact.js +721 -0
  184. package/dist/core/policy-limits/etag-store.js +122 -0
  185. package/dist/core/prd-check/parser.js +215 -0
  186. package/dist/core/prd-check/reporter.js +127 -0
  187. package/dist/core/prd-check/session-review.js +557 -0
  188. package/dist/core/prd-check/verifiers.js +223 -0
  189. package/dist/core/prompt-cache/client-cache.js +99 -0
  190. package/dist/core/prompts/assembly.js +29 -0
  191. package/dist/core/prompts/registry.js +364 -0
  192. package/dist/core/pugi-md/cc-compat-rules.js +735 -0
  193. package/dist/core/pugi-md/context-injector.js +76 -0
  194. package/dist/core/pugi-md/walk-up.js +207 -0
  195. package/dist/core/python/uv-installer.js +270 -0
  196. package/dist/core/python/uv-resolver.js +83 -0
  197. package/dist/core/rate-limit/narrator.js +146 -0
  198. package/dist/core/recipes/cli-types.js +20 -0
  199. package/dist/core/recipes/loader.js +103 -0
  200. package/dist/core/recipes/runner.js +345 -0
  201. package/dist/core/recipes/schema.js +587 -0
  202. package/dist/core/release-notes/parser.js +241 -0
  203. package/dist/core/release-notes/state.js +116 -0
  204. package/dist/core/repl/ask.js +37 -37
  205. package/dist/core/repl/cancellation.js +26 -26
  206. package/dist/core/repl/cap-warning.js +4 -4
  207. package/dist/core/repl/clipboard-read.js +11 -11
  208. package/dist/core/repl/dispatch-fsm.js +12 -12
  209. package/dist/core/repl/history-search.js +15 -15
  210. package/dist/core/repl/history.js +28 -18
  211. package/dist/core/repl/kill-ring.js +5 -5
  212. package/dist/core/repl/model-pricing.js +135 -0
  213. package/dist/core/repl/privacy-banner.js +22 -22
  214. package/dist/core/repl/session.js +2157 -214
  215. package/dist/core/repl/slash-commands.js +533 -40
  216. package/dist/core/repl/store/index.js +1 -1
  217. package/dist/core/repl/store/jsonl-log.js +22 -22
  218. package/dist/core/repl/store/lockfile.js +10 -10
  219. package/dist/core/repl/store/session-store.js +136 -107
  220. package/dist/core/repl/store/types.js +15 -15
  221. package/dist/core/repl/store/uuid-v7.js +12 -12
  222. package/dist/core/repl/workspace-context.js +43 -21
  223. package/dist/core/repo-map/build.js +125 -0
  224. package/dist/core/repo-map/cache.js +185 -0
  225. package/dist/core/repo-map/extractor.js +254 -0
  226. package/dist/core/repo-map/formatter.js +145 -0
  227. package/dist/core/repo-map/page-rank.js +105 -0
  228. package/dist/core/repo-map/scanner.js +211 -0
  229. package/dist/core/retry-budget/budget.js +284 -0
  230. package/dist/core/retry-budget/index.js +5 -0
  231. package/dist/core/retry-budget/retry-cap.js +74 -0
  232. package/dist/core/routing/lead-worker.js +43 -0
  233. package/dist/core/routing/pre-flight-estimator.js +108 -0
  234. package/dist/core/runs/run-tree.js +103 -0
  235. package/dist/core/security/injection-scanner.js +367 -0
  236. package/dist/core/security/output-filter.js +418 -0
  237. package/dist/core/session/env-file.js +105 -0
  238. package/dist/core/session/section-budgets.js +140 -0
  239. package/dist/core/session.js +92 -0
  240. package/dist/core/settings.js +286 -5
  241. package/dist/core/share/formatter.js +271 -0
  242. package/dist/core/share/redactor.js +221 -0
  243. package/dist/core/share/uploader.js +267 -0
  244. package/dist/core/skills/defaults.js +457 -0
  245. package/dist/core/skills/loader.js +22 -22
  246. package/dist/core/skills/sources.js +27 -27
  247. package/dist/core/smoke/headless-driver.js +174 -0
  248. package/dist/core/smoke/orchestrator.js +194 -0
  249. package/dist/core/smoke/runner.js +238 -0
  250. package/dist/core/smoke/scenario-parser.js +316 -0
  251. package/dist/core/statusline.js +99 -0
  252. package/dist/core/subagents/dispatcher-real.js +600 -0
  253. package/dist/core/subagents/dispatcher.js +132 -43
  254. package/dist/core/subagents/index.js +19 -6
  255. package/dist/core/subagents/isolation-matrix.js +213 -0
  256. package/dist/core/subagents/spawn.js +19 -4
  257. package/dist/core/telemetry/emitter.js +229 -0
  258. package/dist/core/telemetry/queue.js +251 -0
  259. package/dist/core/theme/context.js +91 -0
  260. package/dist/core/theme/presets.js +228 -0
  261. package/dist/core/theme/state.js +181 -0
  262. package/dist/core/todos/invariant.js +10 -0
  263. package/dist/core/todos/state.js +177 -0
  264. package/dist/core/tool-schema/compressor.js +89 -0
  265. package/dist/core/transport/version-interceptor.js +166 -0
  266. package/dist/core/trust.js +2 -2
  267. package/dist/core/tui/thinking-block.js +64 -0
  268. package/dist/core/vim/keymap.js +288 -0
  269. package/dist/core/vim/state.js +92 -0
  270. package/dist/core/watch-markers/marker-watcher.js +133 -0
  271. package/dist/core/worktree-manager/cleanup.js +123 -0
  272. package/dist/core/worktree-manager/manager.js +303 -0
  273. package/dist/index.js +28 -0
  274. package/dist/runtime/bootstrap.js +190 -0
  275. package/dist/runtime/cli.js +4151 -489
  276. package/dist/runtime/commands/agents.js +30 -30
  277. package/dist/runtime/commands/budget.js +5 -5
  278. package/dist/runtime/commands/cancel.js +231 -0
  279. package/dist/runtime/commands/chain.js +489 -0
  280. package/dist/runtime/commands/codegraph-status.js +227 -0
  281. package/dist/runtime/commands/compact.js +297 -0
  282. package/dist/runtime/commands/config.js +32 -32
  283. package/dist/runtime/commands/cost.js +199 -0
  284. package/dist/runtime/commands/delegate.js +244 -13
  285. package/dist/runtime/commands/dispatch.js +126 -0
  286. package/dist/runtime/commands/doctor.js +579 -0
  287. package/dist/runtime/commands/feedback.js +184 -0
  288. package/dist/runtime/commands/hooks.js +184 -0
  289. package/dist/runtime/commands/init.js +254 -0
  290. package/dist/runtime/commands/lsp.js +368 -0
  291. package/dist/runtime/commands/mcp.js +879 -0
  292. package/dist/runtime/commands/memory.js +582 -0
  293. package/dist/runtime/commands/model.js +237 -0
  294. package/dist/runtime/commands/onboarding.js +275 -0
  295. package/dist/runtime/commands/patch.js +128 -0
  296. package/dist/runtime/commands/permissions.js +112 -0
  297. package/dist/runtime/commands/plan.js +143 -0
  298. package/dist/runtime/commands/prd-check.js +285 -0
  299. package/dist/runtime/commands/privacy.js +17 -17
  300. package/dist/runtime/commands/recipe.js +325 -0
  301. package/dist/runtime/commands/redo-blob-store.js +92 -0
  302. package/dist/runtime/commands/redo.js +361 -0
  303. package/dist/runtime/commands/release-notes.js +229 -0
  304. package/dist/runtime/commands/repo-map.js +95 -0
  305. package/dist/runtime/commands/report.js +299 -0
  306. package/dist/runtime/commands/resume.js +118 -0
  307. package/dist/runtime/commands/review-consensus.js +68 -53
  308. package/dist/runtime/commands/rewind.js +333 -0
  309. package/dist/runtime/commands/roster.js +14 -14
  310. package/dist/runtime/commands/sessions.js +163 -0
  311. package/dist/runtime/commands/share.js +316 -0
  312. package/dist/runtime/commands/skills.js +31 -31
  313. package/dist/runtime/commands/status.js +186 -0
  314. package/dist/runtime/commands/stickers.js +82 -0
  315. package/dist/runtime/commands/style.js +194 -0
  316. package/dist/runtime/commands/theme.js +196 -0
  317. package/dist/runtime/commands/undo.js +54 -22
  318. package/dist/runtime/commands/update.js +289 -0
  319. package/dist/runtime/commands/vim.js +140 -0
  320. package/dist/runtime/commands/worktree.js +177 -0
  321. package/dist/runtime/commands/worktrees.js +155 -0
  322. package/dist/runtime/headless-repl.js +195 -0
  323. package/dist/runtime/headless.js +543 -0
  324. package/dist/runtime/load-hooks-or-exit.js +71 -0
  325. package/dist/runtime/plan-decompose.js +531 -0
  326. package/dist/runtime/update-check.js +28 -28
  327. package/dist/runtime/version.js +65 -0
  328. package/dist/skills/bundled/batch.js +617 -0
  329. package/dist/skills/bundled/index.js +45 -0
  330. package/dist/skills/bundled/loop.js +358 -0
  331. package/dist/skills/bundled/remember.js +383 -0
  332. package/dist/skills/bundled/simplify.js +289 -0
  333. package/dist/skills/bundled/skillify.js +373 -0
  334. package/dist/skills/bundled/stuck.js +558 -0
  335. package/dist/skills/bundled/verify.js +439 -0
  336. package/dist/testing/vcr.js +486 -0
  337. package/dist/tools/agent-tool.js +229 -0
  338. package/dist/tools/apply-patch.js +556 -0
  339. package/dist/tools/ask-user-question.js +222 -0
  340. package/dist/tools/ask-user.js +115 -0
  341. package/dist/tools/bash.js +623 -45
  342. package/dist/tools/brief.js +224 -0
  343. package/dist/tools/enter-worktree.js +250 -0
  344. package/dist/tools/exit-worktree.js +147 -0
  345. package/dist/tools/file-tools.js +161 -44
  346. package/dist/tools/lsp-tools.js +189 -0
  347. package/dist/tools/mcp-tool.js +260 -0
  348. package/dist/tools/multi-edit.js +361 -0
  349. package/dist/tools/powershell.js +268 -0
  350. package/dist/tools/registry.js +85 -0
  351. package/dist/tools/skill-tool.js +96 -0
  352. package/dist/tools/sleep.js +99 -0
  353. package/dist/tools/synthetic-output.js +133 -0
  354. package/dist/tools/tasks.js +208 -0
  355. package/dist/tools/todo-write.js +184 -0
  356. package/dist/tools/verify-plan-execution.js +295 -0
  357. package/dist/tools/web-fetch-injection-scanner.js +207 -0
  358. package/dist/tools/web-fetch.js +195 -10
  359. package/dist/tools/web-search.js +458 -0
  360. package/dist/tui/agent-progress-card.js +111 -0
  361. package/dist/tui/agent-tree.js +11 -1
  362. package/dist/tui/ask-modal.js +14 -14
  363. package/dist/tui/ask-user-question-prompt.js +203 -0
  364. package/dist/tui/compact-banner.js +81 -0
  365. package/dist/tui/conversation-pane.js +85 -11
  366. package/dist/tui/cost-table.js +111 -0
  367. package/dist/tui/device-flow.js +2 -2
  368. package/dist/tui/doctor-table.js +46 -0
  369. package/dist/tui/feedback-prompt.js +156 -0
  370. package/dist/tui/input-box.js +247 -32
  371. package/dist/tui/login-picker.js +3 -3
  372. package/dist/tui/markdown-render.js +6 -6
  373. package/dist/tui/onboarding-wizard.js +240 -0
  374. package/dist/tui/permissions-picker.js +86 -0
  375. package/dist/tui/render.js +35 -0
  376. package/dist/tui/repl-render.js +332 -54
  377. package/dist/tui/repl-splash-art.js +16 -16
  378. package/dist/tui/repl-splash-mascot.js +48 -24
  379. package/dist/tui/repl-splash.js +22 -22
  380. package/dist/tui/repl.js +124 -44
  381. package/dist/tui/slash-palette.js +6 -6
  382. package/dist/tui/splash.js +2 -2
  383. package/dist/tui/status-bar.js +109 -31
  384. package/dist/tui/status-table.js +7 -0
  385. package/dist/tui/stickers-art.js +136 -0
  386. package/dist/tui/style-table.js +28 -0
  387. package/dist/tui/theme-table.js +29 -0
  388. package/dist/tui/thinking-spinner.js +123 -0
  389. package/dist/tui/tool-stream-pane.js +53 -4
  390. package/dist/tui/update-banner.js +27 -2
  391. package/dist/tui/vim-input.js +267 -0
  392. package/dist/tui/welcome-banner.js +107 -0
  393. package/dist/tui/welcome-data.js +293 -0
  394. package/dist/tui/workspace-context.js +2 -2
  395. package/docs/examples/codegraph.mcp.json +10 -0
  396. package/package.json +23 -6
  397. package/test/scenarios/codegen-create-file.scenario.txt +13 -0
  398. package/test/scenarios/compact-force.scenario.txt +11 -0
  399. package/test/scenarios/identity.scenario.txt +11 -0
  400. package/test/scenarios/persona-handoff.scenario.txt +11 -0
  401. package/test/scenarios/walkback.scenario.txt +12 -0
  402. package/dist/core/engine/compaction-hook.js +0 -154
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Class-aware bash tool — Sprint α5.2 (ADR-0056 PR-PUGI-CLI-M1-GAP-B).
2
+ * Class-aware bash tool — Sprint .
3
3
  *
4
4
  * The agent loop invokes this tool through the registry name `bash`.
5
5
  * It supersedes `file-tools.ts::bashTool`, which used the legacy
@@ -7,32 +7,34 @@
7
7
  * registry entry (`registry.ts` `bash`) is not duplicated.
8
8
  *
9
9
  * Behavioural changes vs the legacy tool:
10
- * 1. Permission decision routes through `evaluateBashPermission`
11
- * (7-class taxonomy, mode-aware, destructive override gate).
12
- * 2. Output cap is 32 KB combined stdout+stderr per call (down
13
- * from 64 KB). Overflow is persisted to
14
- * `.pugi/artifacts/<sessionId>/bash-<callId>.out` with the path
15
- * returned as `artifactRef`.
16
- * 3. Cwd carry-over: the tool receives `cwd` from the previous
17
- * turn's session state and writes the new cwd back when the
18
- * command was a `cd <path>` that landed inside
19
- * `workspaceRoot ∪ additionalDirectories`. Escapes reset the
20
- * cwd to workspaceRoot and emit `bash.cwd_escape`.
21
- * 4. Background jobs: when `background: true`, spawn detached,
22
- * track in `~/.pugi/jobs.json`, return immediately with
23
- * `jobId`. `listJobs()` and `killJob(jobId)` are exported.
24
- * 5. 60s default timeout. SIGTERM at deadline, SIGKILL 5s later.
25
- * Emit `bash.timeout`.
26
- * 6. POSIX-only (`/bin/sh`). The non-goal in ADR-0056 explicitly
27
- * drops Windows shell support for M1.
10
+ * 1. Permission decision routes through `evaluateBashPermission`
11
+ * (7-class taxonomy, mode-aware, destructive override gate).
12
+ * 2. Output cap is 32 KB combined stdout+stderr per call (down
13
+ * from 64 KB). Overflow is persisted to
14
+ * `.pugi/artifacts/<sessionId>/bash-<callId>.out` with the path
15
+ * returned as `artifactRef`.
16
+ * 3. Cwd carry-over: the tool receives `cwd` from the previous
17
+ * turn's session state and writes the new cwd back when the
18
+ * command was a `cd <path>` that landed inside
19
+ * `workspaceRoot ∪ additionalDirectories`. Escapes reset the
20
+ * cwd to workspaceRoot and emit `bash.cwd_escape`.
21
+ * 4. Background jobs: when `background: true`, spawn detached,
22
+ * track in `~/.pugi/jobs.json`, return immediately with
23
+ * `jobId`. `listJobs()` and `killJob(jobId)` are exported.
24
+ * 5. 60s default timeout. SIGTERM at deadline, SIGKILL 5s later.
25
+ * Emit `bash.timeout`.
26
+ * 6. POSIX-only (`/bin/sh`). The non-goal in ADR-0056 explicitly
27
+ * drops Windows shell support for M1.
28
28
  */
29
29
  import { randomUUID } from 'node:crypto';
30
- import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync, } from 'node:fs';
30
+ import { appendFileSync, closeSync, existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync, } from 'node:fs';
31
31
  import { homedir } from 'node:os';
32
32
  import { isAbsolute, join, resolve } from 'node:path';
33
33
  import { spawn, spawnSync } from 'node:child_process';
34
34
  import { classifyBash } from '../core/bash-classifier.js';
35
+ import { applyRedirect, finaliseRedirectFile, normalizeTailLines, openRedirectFile, resolveRedirectTarget, } from '../core/bash/redirect.js';
35
36
  import { evaluateBashPermission } from '../core/permission.js';
37
+ import { writeAuditEvent } from '../core/audit/audit-trail.js';
36
38
  import { getJobRegistry, } from '../core/jobs/registry.js';
37
39
  import { recordToolCall, recordToolResult } from '../core/session.js';
38
40
  export const BASH_OUTPUT_CAP_BYTES = 32 * 1024;
@@ -44,7 +46,7 @@ export const BASH_SIGKILL_GRACE_MS = 5_000;
44
46
  * SIGTERM the child to prevent a `yes`-style stream from pinning
45
47
  * 60+ MB before the timeout watchdog fires.
46
48
  *
47
- * Code Reviewer P1 retro 2026-05-24: the async path previously
49
+ * Code Reviewer P1 retro: the async path previously
48
50
  * accumulated stdout chunks without bound; only spawnSync had a
49
51
  * 10 MB maxBuffer ceiling. Aligning the async path closes the gap.
50
52
  */
@@ -60,6 +62,33 @@ export async function bashTool(input, ctx) {
60
62
  const additionalDirectories = ctx.additionalDirectories ?? [];
61
63
  const source = ctx.source ?? 'agent';
62
64
  const toolCallId = recordToolCall(ctx.session, 'bash', cmd);
65
+ // Cwd carry-over decision (also re-checked post-run).
66
+ const startCwd = resolveStartCwd(input.cwd ?? ctx.lastBashCwd, ctx.root, additionalDirectories);
67
+ // Workspace-git-boundary guard (CEO P0 #51).
68
+ // Runs BEFORE the permission gate so the boundary escape message is
69
+ // the one the operator/engine sees, regardless of permission policy.
70
+ // The leak is structural (git silently writes to an ancestor .git
71
+ // when the workspace lacks one), not a policy violation, so the
72
+ // diagnostic must surface even when the permission gate would
73
+ // otherwise have asked or auto-allowed.
74
+ const boundaryBlock = enforceGitBoundary(cmd, startCwd, ctx.root);
75
+ if (boundaryBlock !== null) {
76
+ emitEvent(ctx.session, 'bash.git_boundary_escape', {
77
+ cmd,
78
+ workspaceRoot: ctx.root,
79
+ resolvedToplevel: boundaryBlock.resolvedToplevel ?? null,
80
+ });
81
+ recordToolResult(ctx.session, toolCallId, 'error', boundaryBlock.reason);
82
+ return {
83
+ stdout: '',
84
+ stderr: boundaryBlock.reason,
85
+ exitCode: 126,
86
+ nextCwd: ctx.lastBashCwd ?? ctx.root,
87
+ truncated: false,
88
+ timedOut: false,
89
+ cancelled: false,
90
+ };
91
+ }
63
92
  // Permission gate via the new class-aware engine.
64
93
  const decision = evaluateBashPermission(cmd, ctx.settings.permissions.mode, {
65
94
  workspaceRoot: ctx.root,
@@ -69,6 +98,22 @@ export async function bashTool(input, ctx) {
69
98
  if (decision.decision !== 'allow') {
70
99
  const reason = `Permission ${decision.decision}: ${decision.reason}`;
71
100
  recordToolResult(ctx.session, toolCallId, 'error', reason);
101
+ // #21 : emit `permission_denied` to
102
+ // the tenant-wide audit trail. Truncate the cmd preview to 200
103
+ // chars so a long here-doc does not bloat the JSONL row; the
104
+ // session log keeps the full text for forensic replay.
105
+ writeAuditEvent({
106
+ event: 'permission_denied',
107
+ sessionId: ctx.session.id,
108
+ workspaceRoot: ctx.root,
109
+ data: {
110
+ tool: 'bash',
111
+ source,
112
+ decision: decision.decision,
113
+ reason: decision.reason,
114
+ cmdPreview: cmd.slice(0, 200),
115
+ },
116
+ });
72
117
  return {
73
118
  stdout: '',
74
119
  stderr: `Permission denied: ${decision.reason}`,
@@ -76,10 +121,27 @@ export async function bashTool(input, ctx) {
76
121
  nextCwd: ctx.lastBashCwd ?? ctx.root,
77
122
  truncated: false,
78
123
  timedOut: false,
124
+ cancelled: false,
125
+ };
126
+ }
127
+ // CEO P1 #25 — pre-spawn cancellation check. Fires
128
+ // AFTER the permission gate so a cancelled brief never reaches
129
+ // /bin/sh even when the command would have been allowed. Mirrors
130
+ // the `gateOnCancellation` pattern from file-tools.ts.
131
+ if (ctx.cancellation?.isAborted === true) {
132
+ const reason = 'operator_aborted: bash refused before spawn';
133
+ emitEvent(ctx.session, 'bash.cancelled', { cmd, phase: 'pre_spawn' });
134
+ recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
135
+ return {
136
+ stdout: '',
137
+ stderr: reason,
138
+ exitCode: 130,
139
+ nextCwd: ctx.lastBashCwd ?? ctx.root,
140
+ truncated: false,
141
+ timedOut: false,
142
+ cancelled: true,
79
143
  };
80
144
  }
81
- // Cwd carry-over decision (also re-checked post-run).
82
- const startCwd = resolveStartCwd(input.cwd ?? ctx.lastBashCwd, ctx.root, additionalDirectories);
83
145
  // Background job branch.
84
146
  if (input.background === true) {
85
147
  return runBackground({ cmd, ctx, toolCallId, startCwd, additionalDirectories });
@@ -87,12 +149,67 @@ export async function bashTool(input, ctx) {
87
149
  // Foreground branch with timeout watchdog.
88
150
  const timeoutMs = sanitizeTimeout(input.timeoutMs);
89
151
  const childEnv = buildChildEnv();
152
+ // Pugi backlog P2 — redirect path. When the caller opted into
153
+ // stdout redirect, we open a write-only fd at the resolved log
154
+ // path and hand it directly to the child's stdio array so the
155
+ // child writes through the kernel pipe → file fd without buffering
156
+ // hundreds of MB in the Node process. The buffered code path below
157
+ // is the fallback for callers that did not opt in.
158
+ let redirectState = null;
159
+ if (input.redirect !== undefined) {
160
+ try {
161
+ const target = resolveRedirectTarget({
162
+ workspaceRoot: ctx.root,
163
+ sessionId: ctx.session.id,
164
+ toolCallId,
165
+ command: cmd,
166
+ override: input.redirect.path,
167
+ });
168
+ const { fd, tempPath } = openRedirectFile(target);
169
+ redirectState = {
170
+ target,
171
+ fd,
172
+ tempPath,
173
+ tailLines: normalizeTailLines(input.redirect.tailLines),
174
+ };
175
+ }
176
+ catch (error) {
177
+ // Bad caller-supplied path (absolute, traversal escape). Fall
178
+ // back to a structured error rather than crashing the engine
179
+ // loop. Mirrors how the permission gate surfaces a refusal —
180
+ // the model can adjust the redirect spec and retry.
181
+ const reason = `redirect refused: ${error.message}`;
182
+ recordToolResult(ctx.session, toolCallId, 'error', reason);
183
+ return {
184
+ stdout: '',
185
+ stderr: reason,
186
+ exitCode: 126,
187
+ nextCwd: ctx.lastBashCwd ?? ctx.root,
188
+ truncated: false,
189
+ timedOut: false,
190
+ cancelled: false,
191
+ };
192
+ }
193
+ }
90
194
  // POSIX-only `/bin/sh -c <cmd>`. The ADR-0056 non-goals explicitly
91
195
  // exclude Windows for M1.
196
+ //
197
+ // stdio layout:
198
+ // - default: ['ignore', 'pipe', 'pipe'] — buffer chunks in
199
+ // Node so the post-run capToCombined can size them
200
+ // to the report cap.
201
+ // - redirect: ['ignore', fd, fd] — kernel pipes stdout+stderr
202
+ // straight into the log file fd. No Node-side
203
+ // buffering, no truncation marker, no in-memory
204
+ // ceiling. The tail-reader fishes the trailing
205
+ // lines out of the file after the child exits.
206
+ const stdioLayout = redirectState !== null
207
+ ? ['ignore', redirectState.fd, redirectState.fd]
208
+ : ['ignore', 'pipe', 'pipe'];
92
209
  const child = spawn('/bin/sh', ['-c', cmd], {
93
210
  cwd: startCwd,
94
211
  env: childEnv,
95
- stdio: ['ignore', 'pipe', 'pipe'],
212
+ stdio: stdioLayout,
96
213
  detached: false,
97
214
  });
98
215
  const stdoutChunks = [];
@@ -106,6 +223,12 @@ export async function bashTool(input, ctx) {
106
223
  // before the timeout watchdog fires, we enforce a live ceiling
107
224
  // (BASH_LIVE_OUTPUT_CAP_BYTES) and SIGTERM the child when crossed.
108
225
  let truncatedMidStream = false;
226
+ // CEO P1 #25 — mid-stream operator cancellation. The
227
+ // listener registered against the CancellationToken below flips
228
+ // this flag and SIGTERMs the child. The close handler reads it to
229
+ // decide between `cancelled` (operator abort) and `timedOut`
230
+ // (watchdog).
231
+ let cancelledMidStream = false;
109
232
  const enforceLiveCap = () => {
110
233
  if (truncatedMidStream)
111
234
  return;
@@ -119,21 +242,158 @@ export async function bashTool(input, ctx) {
119
242
  // child already exited; the close handler will run
120
243
  }
121
244
  };
122
- child.stdout?.on('data', (chunk) => {
123
- if (truncatedMidStream)
124
- return;
125
- stdoutChunks.push(chunk);
126
- stdoutBytes += chunk.length;
127
- enforceLiveCap();
128
- });
129
- child.stderr?.on('data', (chunk) => {
130
- if (truncatedMidStream)
131
- return;
132
- stderrChunks.push(chunk);
133
- stderrBytes += chunk.length;
134
- enforceLiveCap();
135
- });
245
+ // CEO P1 #25 — live stream callback. When the REPL
246
+ // host wires `onStreamChunk`, we forward each stdout/stderr chunk
247
+ // in real time so the conversation pane / tool-stream pane paint
248
+ // bytes as they arrive instead of waiting for the child to exit.
249
+ // We invoke the callback inside a try/catch so a buggy sink
250
+ // (renderer crash, assertion error) never escalates to killing
251
+ // the bash dispatch. The buffered path below still captures the
252
+ // chunk so the model + audit trail stay consistent regardless of
253
+ // renderer health.
254
+ const onStreamChunk = ctx.onStreamChunk;
255
+ const emitStreamChunk = onStreamChunk
256
+ ? (stream, chunk) => {
257
+ try {
258
+ onStreamChunk({ stream, data: chunk.toString('utf8') });
259
+ }
260
+ catch {
261
+ // Sink crash — swallow.
262
+ }
263
+ }
264
+ : null;
265
+ // When redirect is on, child.stdout / child.stderr are null
266
+ // because the spawn handed the log-file fd in directly. The data
267
+ // listeners only fire on the buffered path, which is exactly what
268
+ // we want — the redirect contract is "no in-memory buffer, full
269
+ // output goes to disk".
270
+ if (redirectState === null) {
271
+ child.stdout?.on('data', (chunk) => {
272
+ if (truncatedMidStream || cancelledMidStream)
273
+ return;
274
+ stdoutChunks.push(chunk);
275
+ stdoutBytes += chunk.length;
276
+ if (emitStreamChunk)
277
+ emitStreamChunk('stdout', chunk);
278
+ enforceLiveCap();
279
+ });
280
+ child.stderr?.on('data', (chunk) => {
281
+ if (truncatedMidStream || cancelledMidStream)
282
+ return;
283
+ stderrChunks.push(chunk);
284
+ stderrBytes += chunk.length;
285
+ if (emitStreamChunk)
286
+ emitStreamChunk('stderr', chunk);
287
+ enforceLiveCap();
288
+ });
289
+ }
290
+ // CEO P1 #25 — wire the cancellation token to SIGTERM. We track
291
+ // the detach handle so a successful run releases the listener
292
+ // instead of leaving it pinned to a long-lived REPL
293
+ // CancellationToken (same anti-leak pattern as
294
+ // native-pugi.ts:262).
295
+ let detachCancelListener;
296
+ if (ctx.cancellation && !ctx.cancellation.isAborted) {
297
+ const onAbort = () => {
298
+ if (cancelledMidStream)
299
+ return;
300
+ cancelledMidStream = true;
301
+ emitEvent(ctx.session, 'bash.cancelled', { cmd, phase: 'mid_stream' });
302
+ try {
303
+ child.kill('SIGTERM');
304
+ }
305
+ catch {
306
+ // child already exited; close handler will run
307
+ }
308
+ // SIGKILL escalation if the child does not honour SIGTERM
309
+ // within the grace window. Mirrors the timeout watchdog's
310
+ // two-phase shutdown.
311
+ setTimeout(() => {
312
+ if (child.exitCode !== null || child.signalCode !== null)
313
+ return;
314
+ try {
315
+ child.kill('SIGKILL');
316
+ }
317
+ catch {
318
+ // gone between the check and the signal
319
+ }
320
+ }, BASH_SIGKILL_GRACE_MS).unref();
321
+ };
322
+ detachCancelListener = ctx.cancellation.onAbort(onAbort);
323
+ }
136
324
  const timeoutOutcome = await waitWithTimeout(child, timeoutMs);
325
+ // Detach the cancellation listener on completion so a long-lived
326
+ // REPL token does not retain a reference to the dead child + this
327
+ // closure.
328
+ if (detachCancelListener) {
329
+ try {
330
+ detachCancelListener();
331
+ }
332
+ catch { /* listener already drained */ }
333
+ }
334
+ // Pugi backlog P2 — redirect path. Close the log fd, rename
335
+ // the temp file into place, and return the envelope before the
336
+ // buffered-path code paths run. We do this for every exit shape
337
+ // (success, non-zero, timeout, cancel) so the log file always
338
+ // lands on disk and the tail reflects whatever the child produced
339
+ // before termination. The cancel/timeout branches still surface
340
+ // the appropriate exitCode through the envelope; the operator
341
+ // discovers the failure via `tail` + `exitCode`, not via the
342
+ // legacy stdout/stderr strings.
343
+ if (redirectState !== null) {
344
+ // Close our copy of the fd before rename so the inode is no
345
+ // longer held open by the parent process. The child's stdio
346
+ // already inherited a separate fd; closing ours does not affect
347
+ // the child's writes that already happened.
348
+ try {
349
+ closeSync(redirectState.fd);
350
+ }
351
+ catch {
352
+ // already closed (shouldn't happen on the happy path)
353
+ }
354
+ try {
355
+ finaliseRedirectFile(redirectState.target, redirectState.tempPath);
356
+ }
357
+ catch {
358
+ // best-effort — the temp file still exists on disk for the
359
+ // operator to inspect even if the rename failed.
360
+ }
361
+ const redirectExitCode = cancelledMidStream
362
+ ? 130
363
+ : timeoutOutcome.timedOut
364
+ ? 124
365
+ : timeoutOutcome.exitCode;
366
+ const envelope = applyRedirect({
367
+ target: redirectState.target,
368
+ exitCode: redirectExitCode,
369
+ tailLines: redirectState.tailLines,
370
+ });
371
+ const nextCwdRedirect = computeNextCwd(cmd, startCwd, ctx.root, additionalDirectories, ctx.session);
372
+ // Emit the same lifecycle events the buffered path emits so the
373
+ // session audit trail is symmetric across redirect vs non-redirect
374
+ // dispatches.
375
+ if (cancelledMidStream) {
376
+ recordToolResult(ctx.session, toolCallId, 'cancelled', `operator_aborted: bash killed mid-stream (redirect=${envelope.logPath})`);
377
+ }
378
+ else if (timeoutOutcome.timedOut) {
379
+ emitEvent(ctx.session, 'bash.timeout', { cmd, timeoutMs });
380
+ recordToolResult(ctx.session, toolCallId, 'error', `bash timed out after ${timeoutMs}ms (redirect=${envelope.logPath})`);
381
+ }
382
+ else {
383
+ recordToolResult(ctx.session, toolCallId, 'success', `bash exit=${redirectExitCode} redirect=${envelope.logPath}`);
384
+ }
385
+ return {
386
+ stdout: envelope.stdout,
387
+ stderr: envelope.stderr,
388
+ exitCode: redirectExitCode,
389
+ nextCwd: nextCwdRedirect,
390
+ truncated: envelope.truncated,
391
+ timedOut: timeoutOutcome.timedOut,
392
+ cancelled: cancelledMidStream,
393
+ logPath: envelope.logPath,
394
+ tail: envelope.tail,
395
+ };
396
+ }
137
397
  const stdoutFull = Buffer.concat(stdoutChunks).toString('utf8');
138
398
  const stderrFull = Buffer.concat(stderrChunks).toString('utf8');
139
399
  const combinedBytes = stdoutBytes + stderrBytes;
@@ -158,6 +418,25 @@ export async function bashTool(input, ctx) {
158
418
  stdoutOut = capToCombined(stdoutFull, stderrFull).stdout;
159
419
  stderrOut = capToCombined(stdoutFull, stderrFull).stderr;
160
420
  }
421
+ // CEO P1 #25 — cancellation wins races against timeout / cap
422
+ // overflow. The token already aborted by the time the close
423
+ // handler fires; we distinguish operator-driven termination from
424
+ // the watchdog so the REPL transcript reads "Aborted." rather
425
+ // than "Timed out."
426
+ if (cancelledMidStream) {
427
+ const reason = 'operator_aborted: bash killed mid-stream';
428
+ recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
429
+ return {
430
+ stdout: stdoutOut,
431
+ stderr: stderrOut === '' ? reason : `${stderrOut}\n${reason}`,
432
+ exitCode: 130,
433
+ artifactRef,
434
+ nextCwd,
435
+ truncated,
436
+ timedOut: false,
437
+ cancelled: true,
438
+ };
439
+ }
161
440
  if (truncatedMidStream) {
162
441
  // We killed the child because output cap exceeded mid-stream.
163
442
  // Report that as the failure cause rather than as a timeout —
@@ -176,6 +455,7 @@ export async function bashTool(input, ctx) {
176
455
  nextCwd,
177
456
  truncated: true,
178
457
  timedOut: false,
458
+ cancelled: false,
179
459
  };
180
460
  }
181
461
  if (timeoutOutcome.timedOut) {
@@ -189,6 +469,7 @@ export async function bashTool(input, ctx) {
189
469
  nextCwd,
190
470
  truncated,
191
471
  timedOut: true,
472
+ cancelled: false,
192
473
  };
193
474
  }
194
475
  const exitCode = timeoutOutcome.exitCode;
@@ -201,6 +482,7 @@ export async function bashTool(input, ctx) {
201
482
  nextCwd,
202
483
  truncated,
203
484
  timedOut: false,
485
+ cancelled: false,
204
486
  };
205
487
  }
206
488
  function sanitizeTimeout(value) {
@@ -430,10 +712,11 @@ function runBackground(input) {
430
712
  nextCwd: ctx.lastBashCwd ?? ctx.root,
431
713
  truncated: false,
432
714
  timedOut: false,
715
+ cancelled: false,
433
716
  };
434
717
  }
435
718
  /**
436
- * Legacy export preserved for α5.2 callers / tests. Delegates to the
719
+ * Legacy export preserved for callers / tests. Delegates to the
437
720
  * new JobRegistry and projects entries back into the historical
438
721
  * `PugiJob` shape.
439
722
  */
@@ -442,7 +725,7 @@ export function listJobs() {
442
725
  return entries.map(entryToLegacyJob);
443
726
  }
444
727
  /**
445
- * Legacy export preserved for α5.2 callers / tests. Delegates to the
728
+ * Legacy export preserved for callers / tests. Delegates to the
446
729
  * new JobRegistry. Returns the same `{ killed, reason? }` shape so the
447
730
  * existing bash-tool test suite continues to pass without an
448
731
  * end-to-end rewrite.
@@ -565,6 +848,160 @@ function readRegistryEntriesSync() {
565
848
  return [];
566
849
  }
567
850
  }
851
+ /**
852
+ * Workspace-git-boundary guard (CEO P0 #51).
853
+ *
854
+ * Background: CEO live REPL surfaced a scenario where the customer
855
+ * workspace dir was created INSIDE another git repository (the Pugi
856
+ * monorepo itself). The model emitted `git init && git add . && git
857
+ * commit -m ...` against that workspace. The workspace had no `.git`
858
+ * of its own so git silently walked up to the outer repo's `.git` and
859
+ * committed the customer's files directly to the monorepo's main
860
+ * branch. Had the outer remote been FF-permissive, those files would
861
+ * have pushed to production. This is a customer-of-customer leak.
862
+ *
863
+ * The guard: when the agent emits a mutating git op (add / commit /
864
+ * push / rebase / reset / checkout) and the effective git toplevel
865
+ * (`git -C $cwd rev-parse --show-toplevel`) sits OUTSIDE the workspace
866
+ * root, block the command. The model is steered (via the persona
867
+ * prompt) to run `git init` first; the guard is the defensive net so
868
+ * a careless model emission cannot cross the boundary.
869
+ *
870
+ * Exported so the spec can exercise the predicate in isolation without
871
+ * having to drive the whole bash tool.
872
+ */
873
+ export const GIT_BOUNDARY_BLOCK_PREFIX = 'git boundary escape:';
874
+ /**
875
+ * Subcommands we treat as definitely mutating for the boundary check.
876
+ * We intentionally OMIT subcommands that have common read-only modes
877
+ * (`branch --list`, `tag --list`, `stash list`, `remote -v`) to keep
878
+ * the guard precise. The CEO P0 #51 leak vector is files written to
879
+ * an ancestor repo's working tree / refs, which the included set
880
+ * fully covers. The omitted subcommands can still create refs in the
881
+ * outer .git, but they do not move customer files into the outer
882
+ * repo's commit graph, so the leak severity is lower and the
883
+ * ergonomic cost of false positives on `--list` flags is higher.
884
+ */
885
+ const MUTATING_GIT_SUBCOMMANDS = new Set([
886
+ 'add',
887
+ 'commit',
888
+ 'push',
889
+ 'rebase',
890
+ 'reset',
891
+ 'checkout',
892
+ 'merge',
893
+ 'restore',
894
+ 'switch',
895
+ 'cherry-pick',
896
+ 'am',
897
+ 'apply',
898
+ 'clean',
899
+ 'rm',
900
+ 'mv',
901
+ ]);
902
+ /**
903
+ * Inspect a shell command for mutating git operations. Returns the
904
+ * first matching subcommand (e.g. 'commit') or null when none of the
905
+ * components are mutating git ops.
906
+ *
907
+ * We split on `&&`, `||`, `;`, `|` so a compound like
908
+ * `mkdir foo && cd foo && git add .` is correctly flagged on the
909
+ * trailing git component.
910
+ */
911
+ export function detectMutatingGitOp(cmd) {
912
+ const components = cmd.split(/\s*(?:&&|\|\||;|\|)\s*/);
913
+ for (const raw of components) {
914
+ const component = raw.trim();
915
+ if (component === '')
916
+ continue;
917
+ // Strip leading `sudo` wrapper which would otherwise hide the verb.
918
+ const stripped = component.replace(/^sudo\s+/, '');
919
+ // Match `git [<global-flags>] <subcommand> ...`. Global flags we
920
+ // tolerate:
921
+ // - long flag: `--no-pager`, `--git-dir=.git`
922
+ // - short flag with attached value: `-C <path>`, `-c <k=v>`
923
+ // - bare short flag: `-P`
924
+ // Anything weirder falls through and the predicate returns null,
925
+ // which means the guard does not fire on that component — safer
926
+ // to err open here because the destructive classifier and the
927
+ // outer permission gate are independent defences.
928
+ const match = stripped.match(/^git(?:\s+(?:--[A-Za-z][A-Za-z0-9-]*(?:=\S+)?|-[CcP](?:\s+\S+)?|-[A-Za-z]+))*\s+([a-z][a-z0-9-]*)\b/);
929
+ if (!match)
930
+ continue;
931
+ const subcommand = match[1];
932
+ if (subcommand && MUTATING_GIT_SUBCOMMANDS.has(subcommand)) {
933
+ return subcommand;
934
+ }
935
+ }
936
+ return null;
937
+ }
938
+ /**
939
+ * Resolve the workspace's effective git boundary. Returns:
940
+ * - the absolute path of the .git toplevel that owns `cwd`
941
+ * - null when no .git ancestor exists at all (standalone, no repo)
942
+ *
943
+ * Pure filesystem walk so the guard does not depend on git being on
944
+ * PATH. We look for either a `.git` directory or a `.git` file (the
945
+ * worktree case where `.git` is a pointer file).
946
+ */
947
+ export function resolveGitToplevel(cwd) {
948
+ let dir = cwd;
949
+ while (true) {
950
+ const dotGit = join(dir, '.git');
951
+ if (existsSync(dotGit))
952
+ return dir;
953
+ const parent = resolve(dir, '..');
954
+ if (parent === dir)
955
+ return null;
956
+ dir = parent;
957
+ }
958
+ }
959
+ /**
960
+ * The actual guard. Returns null when the command is allowed; returns
961
+ * a block descriptor when it should be denied. The block message uses
962
+ * the literal prefix `git boundary escape:` so callers (and the spec)
963
+ * can pattern-match.
964
+ */
965
+ export function enforceGitBoundary(cmd, startCwd, workspaceRoot) {
966
+ const subcommand = detectMutatingGitOp(cmd);
967
+ if (subcommand === null)
968
+ return null;
969
+ // Resolve symlinks on both sides so a /var → /private/var macOS
970
+ // realpath divergence does not produce a false escape.
971
+ const root = safeRealpath(workspaceRoot);
972
+ const toplevel = resolveGitToplevel(safeRealpath(startCwd));
973
+ const resolvedToplevel = toplevel === null ? null : safeRealpath(toplevel);
974
+ if (resolvedToplevel === root)
975
+ return null;
976
+ // Either no .git anywhere (standalone) OR the .git that wins is an
977
+ // ancestor — both are escape scenarios. Operator must run `git init`
978
+ // explicitly inside the workspace.
979
+ if (resolvedToplevel === null) {
980
+ return {
981
+ subcommand,
982
+ resolvedToplevel: null,
983
+ reason: `${GIT_BOUNDARY_BLOCK_PREFIX} workspace root '${workspaceRoot}' has no .git ` +
984
+ `and no ancestor repository exists. Run \`git init\` in the workspace first ` +
985
+ `before \`git ${subcommand}\`.`,
986
+ };
987
+ }
988
+ return {
989
+ subcommand,
990
+ resolvedToplevel,
991
+ reason: `${GIT_BOUNDARY_BLOCK_PREFIX} workspace root '${workspaceRoot}' has no .git; ` +
992
+ `outer toplevel is '${resolvedToplevel}'. Run \`git init\` in the workspace ` +
993
+ `first before \`git ${subcommand}\` (otherwise the operation would write to ` +
994
+ `the ancestor repository, not the workspace).`,
995
+ };
996
+ }
997
+ function safeRealpath(path) {
998
+ try {
999
+ return realpathSync(path);
1000
+ }
1001
+ catch {
1002
+ return path;
1003
+ }
1004
+ }
568
1005
  function removeRegistryEntrySync(jobId) {
569
1006
  const path = join(homedir(), '.pugi', 'jobs.json');
570
1007
  const entries = readRegistryEntriesSync().filter((entry) => entry.id !== jobId);
@@ -592,6 +1029,29 @@ export function bashToolSync(input, ctx) {
592
1029
  const additionalDirectories = ctx.additionalDirectories ?? [];
593
1030
  const source = ctx.source ?? 'agent';
594
1031
  const toolCallId = recordToolCall(ctx.session, 'bash', cmd);
1032
+ const startCwd = resolveStartCwd(input.cwd ?? ctx.lastBashCwd, ctx.root, additionalDirectories);
1033
+ // Workspace-git-boundary guard (CEO P0 #51). Fires
1034
+ // BEFORE the permission gate so the structural boundary diagnostic
1035
+ // is the one the operator sees. See the async path for the full
1036
+ // rationale.
1037
+ const boundaryBlock = enforceGitBoundary(cmd, startCwd, ctx.root);
1038
+ if (boundaryBlock !== null) {
1039
+ emitEvent(ctx.session, 'bash.git_boundary_escape', {
1040
+ cmd,
1041
+ workspaceRoot: ctx.root,
1042
+ resolvedToplevel: boundaryBlock.resolvedToplevel ?? null,
1043
+ });
1044
+ recordToolResult(ctx.session, toolCallId, 'error', boundaryBlock.reason);
1045
+ return {
1046
+ stdout: '',
1047
+ stderr: boundaryBlock.reason,
1048
+ exitCode: 126,
1049
+ nextCwd: ctx.lastBashCwd ?? ctx.root,
1050
+ truncated: false,
1051
+ timedOut: false,
1052
+ cancelled: false,
1053
+ };
1054
+ }
595
1055
  const decision = evaluateBashPermission(cmd, ctx.settings.permissions.mode, {
596
1056
  workspaceRoot: ctx.root,
597
1057
  additionalDirectories,
@@ -600,6 +1060,20 @@ export function bashToolSync(input, ctx) {
600
1060
  if (decision.decision !== 'allow') {
601
1061
  const reason = `Permission ${decision.decision}: ${decision.reason}`;
602
1062
  recordToolResult(ctx.session, toolCallId, 'error', reason);
1063
+ // #21: mirror the async-path emission so sync callers
1064
+ // (spawnSync fallback) produce the same tenant-wide audit trail.
1065
+ writeAuditEvent({
1066
+ event: 'permission_denied',
1067
+ sessionId: ctx.session.id,
1068
+ workspaceRoot: ctx.root,
1069
+ data: {
1070
+ tool: 'bash',
1071
+ source,
1072
+ decision: decision.decision,
1073
+ reason: decision.reason,
1074
+ cmdPreview: cmd.slice(0, 200),
1075
+ },
1076
+ });
603
1077
  return {
604
1078
  stdout: '',
605
1079
  stderr: `Permission denied: ${decision.reason}`,
@@ -607,19 +1081,125 @@ export function bashToolSync(input, ctx) {
607
1081
  nextCwd: ctx.lastBashCwd ?? ctx.root,
608
1082
  truncated: false,
609
1083
  timedOut: false,
1084
+ cancelled: false,
1085
+ };
1086
+ }
1087
+ // CEO P1 #25 — sync path observes pre-spawn cancellation too. The
1088
+ // sync path is used by the engine-loop tool-bridge (`bashToolSync`
1089
+ // from tool-bridge.ts:1385); we cannot mid-stream cancel that path
1090
+ // without rewriting spawnSync, but the pre-spawn gate still gives
1091
+ // the operator a quick-exit window between permission and shell
1092
+ // launch.
1093
+ if (ctx.cancellation?.isAborted === true) {
1094
+ const reason = 'operator_aborted: bash refused before spawn';
1095
+ emitEvent(ctx.session, 'bash.cancelled', { cmd, phase: 'pre_spawn_sync' });
1096
+ recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
1097
+ return {
1098
+ stdout: '',
1099
+ stderr: reason,
1100
+ exitCode: 130,
1101
+ nextCwd: ctx.lastBashCwd ?? ctx.root,
1102
+ truncated: false,
1103
+ timedOut: false,
1104
+ cancelled: true,
610
1105
  };
611
1106
  }
612
- const startCwd = resolveStartCwd(input.cwd ?? ctx.lastBashCwd, ctx.root, additionalDirectories);
613
1107
  const timeoutMs = sanitizeTimeout(input.timeoutMs);
614
1108
  const childEnv = buildChildEnv();
1109
+ // Pugi backlog P2 — redirect path for the sync entry. The
1110
+ // engine loop's tool-bridge dispatches through `bashToolSync`, so
1111
+ // the redirect contract has to be honoured here too — otherwise a
1112
+ // model that asks for log discipline through the bash tool surface
1113
+ // would get its stdout buffered + truncated through the legacy
1114
+ // pipeline. `spawnSync` accepts file descriptors in `stdio` so we
1115
+ // can hand the log fd in directly, same as the async path.
1116
+ let redirectState = null;
1117
+ if (input.redirect !== undefined) {
1118
+ try {
1119
+ const target = resolveRedirectTarget({
1120
+ workspaceRoot: ctx.root,
1121
+ sessionId: ctx.session.id,
1122
+ toolCallId,
1123
+ command: cmd,
1124
+ override: input.redirect.path,
1125
+ });
1126
+ const { fd, tempPath } = openRedirectFile(target);
1127
+ redirectState = {
1128
+ target,
1129
+ fd,
1130
+ tempPath,
1131
+ tailLines: normalizeTailLines(input.redirect.tailLines),
1132
+ };
1133
+ }
1134
+ catch (error) {
1135
+ const reason = `redirect refused: ${error.message}`;
1136
+ recordToolResult(ctx.session, toolCallId, 'error', reason);
1137
+ return {
1138
+ stdout: '',
1139
+ stderr: reason,
1140
+ exitCode: 126,
1141
+ nextCwd: ctx.lastBashCwd ?? ctx.root,
1142
+ truncated: false,
1143
+ timedOut: false,
1144
+ cancelled: false,
1145
+ };
1146
+ }
1147
+ }
1148
+ const stdioLayout = redirectState !== null
1149
+ ? ['ignore', redirectState.fd, redirectState.fd]
1150
+ : ['ignore', 'pipe', 'pipe'];
615
1151
  const result = spawnSync('/bin/sh', ['-c', cmd], {
616
1152
  cwd: startCwd,
617
1153
  env: childEnv,
618
1154
  encoding: 'utf8',
619
- stdio: ['ignore', 'pipe', 'pipe'],
1155
+ stdio: stdioLayout,
620
1156
  timeout: timeoutMs,
621
1157
  maxBuffer: 10 * 1024 * 1024,
622
1158
  });
1159
+ const timedOut = result.error?.code === 'ETIMEDOUT' ||
1160
+ result.signal === 'SIGTERM';
1161
+ const nextCwd = computeNextCwd(cmd, startCwd, ctx.root, additionalDirectories, ctx.session);
1162
+ // Redirect short-circuit before the buffered-path artifact logic.
1163
+ // We close our copy of the fd before rename so the inode is no
1164
+ // longer held open by the parent process.
1165
+ if (redirectState !== null) {
1166
+ try {
1167
+ closeSync(redirectState.fd);
1168
+ }
1169
+ catch {
1170
+ // already closed
1171
+ }
1172
+ try {
1173
+ finaliseRedirectFile(redirectState.target, redirectState.tempPath);
1174
+ }
1175
+ catch {
1176
+ // best-effort
1177
+ }
1178
+ const redirectExitCode = timedOut ? 124 : result.status ?? 1;
1179
+ const envelope = applyRedirect({
1180
+ target: redirectState.target,
1181
+ exitCode: redirectExitCode,
1182
+ tailLines: redirectState.tailLines,
1183
+ });
1184
+ if (timedOut) {
1185
+ emitEvent(ctx.session, 'bash.timeout', { cmd, timeoutMs });
1186
+ recordToolResult(ctx.session, toolCallId, 'error', `bash timed out after ${timeoutMs}ms (redirect=${envelope.logPath})`);
1187
+ }
1188
+ else {
1189
+ recordToolResult(ctx.session, toolCallId, 'success', `bash exit=${redirectExitCode} redirect=${envelope.logPath}`);
1190
+ }
1191
+ return {
1192
+ stdout: envelope.stdout,
1193
+ stderr: envelope.stderr,
1194
+ exitCode: redirectExitCode,
1195
+ nextCwd,
1196
+ truncated: envelope.truncated,
1197
+ timedOut,
1198
+ cancelled: false,
1199
+ logPath: envelope.logPath,
1200
+ tail: envelope.tail,
1201
+ };
1202
+ }
623
1203
  const stdoutFull = (result.stdout ?? '').toString();
624
1204
  const stderrFull = (result.stderr ?? '').toString();
625
1205
  const truncated = stdoutFull.length + stderrFull.length > BASH_OUTPUT_CAP_BYTES;
@@ -636,10 +1216,7 @@ export function bashToolSync(input, ctx) {
636
1216
  });
637
1217
  ({ stdout: stdoutOut, stderr: stderrOut } = capToCombined(stdoutFull, stderrFull));
638
1218
  }
639
- const timedOut = result.error?.code === 'ETIMEDOUT' ||
640
- result.signal === 'SIGTERM';
641
1219
  const exitCode = timedOut ? 124 : result.status ?? 1;
642
- const nextCwd = computeNextCwd(cmd, startCwd, ctx.root, additionalDirectories, ctx.session);
643
1220
  if (timedOut) {
644
1221
  emitEvent(ctx.session, 'bash.timeout', { cmd, timeoutMs });
645
1222
  recordToolResult(ctx.session, toolCallId, 'error', `bash timed out after ${timeoutMs}ms`);
@@ -655,6 +1232,7 @@ export function bashToolSync(input, ctx) {
655
1232
  nextCwd,
656
1233
  truncated,
657
1234
  timedOut,
1235
+ cancelled: false,
658
1236
  };
659
1237
  }
660
1238
  //# sourceMappingURL=bash.js.map