@pugi/cli 0.1.0-beta.99 → 1.0.0-alpha.1

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 (448) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +11 -191
  3. package/bin/pugi +8 -0
  4. package/package.json +15 -71
  5. package/postinstall.mjs +31 -0
  6. package/CHANGELOG.md +0 -132
  7. package/THIRD_PARTY_NOTICES.md +0 -40
  8. package/assets/pugi-mascot.ansi +0 -16
  9. package/assets/pugi-prozr2-mascot.ansi +0 -9
  10. package/bin/run.js +0 -34
  11. package/dist/commands/deploy.js +0 -439
  12. package/dist/commands/flatten.js +0 -191
  13. package/dist/commands/jobs-watch.js +0 -201
  14. package/dist/commands/jobs.js +0 -260
  15. package/dist/commands/retro.js +0 -210
  16. package/dist/commands/smoke.js +0 -133
  17. package/dist/core/agent-progress/cleanup.js +0 -134
  18. package/dist/core/agent-progress/schema.js +0 -144
  19. package/dist/core/agent-progress/writer.js +0 -101
  20. package/dist/core/agents/adaptive-router.js +0 -330
  21. package/dist/core/agents/loader.js +0 -104
  22. package/dist/core/agents/query-decomposer.js +0 -297
  23. package/dist/core/agents/registry.js +0 -69
  24. package/dist/core/approvals/shortcut-resolver.js +0 -98
  25. package/dist/core/artifact-chain/dispatcher.js +0 -148
  26. package/dist/core/artifact-chain/exporter.js +0 -164
  27. package/dist/core/artifact-chain/state.js +0 -243
  28. package/dist/core/artifact-chain/steps.js +0 -169
  29. package/dist/core/ask-user/question.js +0 -92
  30. package/dist/core/audit/audit-trail.js +0 -275
  31. package/dist/core/auth/ensure-authenticated.js +0 -129
  32. package/dist/core/auth/env-provider.js +0 -238
  33. package/dist/core/auto-open-browser.js +0 -128
  34. package/dist/core/auto-update/channels.js +0 -122
  35. package/dist/core/auto-update/checker.js +0 -241
  36. package/dist/core/auto-update/state.js +0 -235
  37. package/dist/core/bare-mode/index.js +0 -107
  38. package/dist/core/bash/redirect.js +0 -281
  39. package/dist/core/bash-classifier.js +0 -1397
  40. package/dist/core/checkpoint/resumer.js +0 -149
  41. package/dist/core/checkpoint/rewinder.js +0 -291
  42. package/dist/core/checkpoints/shadow-git.js +0 -670
  43. package/dist/core/citations/parser.js +0 -109
  44. package/dist/core/classifier/yolo-classifier.js +0 -88
  45. package/dist/core/clipboard.js +0 -70
  46. package/dist/core/codegraph/decision-store.js +0 -248
  47. package/dist/core/codegraph/detect-repo.js +0 -459
  48. package/dist/core/codegraph/install.js +0 -134
  49. package/dist/core/codegraph/offer-hook.js +0 -220
  50. package/dist/core/compact/auto-trigger.js +0 -96
  51. package/dist/core/compact/buffer-rewriter.js +0 -115
  52. package/dist/core/compact/summarizer.js +0 -208
  53. package/dist/core/compact/token-counter.js +0 -108
  54. package/dist/core/consensus/anvil-fanout.js +0 -276
  55. package/dist/core/consensus/diff-capture.js +0 -491
  56. package/dist/core/consensus/rubric.js +0 -233
  57. package/dist/core/context/builder.js +0 -114
  58. package/dist/core/context/compaction-events.js +0 -99
  59. package/dist/core/context/compaction.js +0 -602
  60. package/dist/core/context/index.js +0 -28
  61. package/dist/core/context/invariants.js +0 -250
  62. package/dist/core/context/markdown-loader.js +0 -288
  63. package/dist/core/context/markdown-traverse.js +0 -255
  64. package/dist/core/context/pugiignore.js +0 -316
  65. package/dist/core/context/repo-skeleton.js +0 -533
  66. package/dist/core/context/tool-eviction.js +0 -55
  67. package/dist/core/context/watcher.js +0 -342
  68. package/dist/core/context/working-set.js +0 -165
  69. package/dist/core/coordinator/agent-tools.js +0 -77
  70. package/dist/core/coordinator/agent-toolset.js +0 -65
  71. package/dist/core/coordinator/fsm.js +0 -73
  72. package/dist/core/coordinator/mode-fsm.js +0 -70
  73. package/dist/core/cost/rate-card.js +0 -129
  74. package/dist/core/cost/tracker.js +0 -221
  75. package/dist/core/credentials.js +0 -355
  76. package/dist/core/cron/scheduler.js +0 -138
  77. package/dist/core/denial-tracking/index.js +0 -8
  78. package/dist/core/denial-tracking/state.js +0 -264
  79. package/dist/core/diagnostics/probe-runner.js +0 -93
  80. package/dist/core/diagnostics/probes/api.js +0 -46
  81. package/dist/core/diagnostics/probes/auth.js +0 -93
  82. package/dist/core/diagnostics/probes/bare-mode.js +0 -42
  83. package/dist/core/diagnostics/probes/cli-version.js +0 -127
  84. package/dist/core/diagnostics/probes/config.js +0 -72
  85. package/dist/core/diagnostics/probes/denial-tracking.js +0 -57
  86. package/dist/core/diagnostics/probes/disk.js +0 -81
  87. package/dist/core/diagnostics/probes/engine-live.js +0 -46
  88. package/dist/core/diagnostics/probes/git.js +0 -65
  89. package/dist/core/diagnostics/probes/hooks.js +0 -118
  90. package/dist/core/diagnostics/probes/mcp.js +0 -75
  91. package/dist/core/diagnostics/probes/node.js +0 -59
  92. package/dist/core/diagnostics/probes/pnpm.js +0 -36
  93. package/dist/core/diagnostics/probes/pugi-md.js +0 -89
  94. package/dist/core/diagnostics/probes/sandbox.js +0 -72
  95. package/dist/core/diagnostics/probes/session.js +0 -74
  96. package/dist/core/diagnostics/probes/status-snapshot.js +0 -488
  97. package/dist/core/diagnostics/probes/workspace.js +0 -63
  98. package/dist/core/diagnostics/types.js +0 -70
  99. package/dist/core/dispatch/cache-cleanup.js +0 -197
  100. package/dist/core/dispatch/cache-handoff.js +0 -295
  101. package/dist/core/edits/apply-patch-layer-e.js +0 -189
  102. package/dist/core/edits/dispatch.js +0 -511
  103. package/dist/core/edits/format-detector.js +0 -260
  104. package/dist/core/edits/format-matrix.js +0 -26
  105. package/dist/core/edits/fuzzy-ladder.js +0 -650
  106. package/dist/core/edits/index.js +0 -19
  107. package/dist/core/edits/journal.js +0 -199
  108. package/dist/core/edits/layer-a-apply.js +0 -217
  109. package/dist/core/edits/layer-a-fuzzy-apply.js +0 -198
  110. package/dist/core/edits/layer-b-apply.js +0 -211
  111. package/dist/core/edits/layer-c-apply.js +0 -160
  112. package/dist/core/edits/layer-d-ast.js +0 -572
  113. package/dist/core/edits/marker-parser.js +0 -401
  114. package/dist/core/edits/security-gate.js +0 -223
  115. package/dist/core/edits/verify-hook.js +0 -273
  116. package/dist/core/edits/worktree.js +0 -322
  117. package/dist/core/engine/adapter-runner.js +0 -8
  118. package/dist/core/engine/anvil-client.js +0 -344
  119. package/dist/core/engine/auto-compact.js +0 -179
  120. package/dist/core/engine/budgets.js +0 -195
  121. package/dist/core/engine/context-prefix.js +0 -155
  122. package/dist/core/engine/index.js +0 -12
  123. package/dist/core/engine/intensity.js +0 -163
  124. package/dist/core/engine/intent.js +0 -260
  125. package/dist/core/engine/native-pugi.js +0 -1616
  126. package/dist/core/engine/noop.js +0 -27
  127. package/dist/core/engine/prompts.js +0 -236
  128. package/dist/core/engine/strip-internal-fields.js +0 -124
  129. package/dist/core/engine/tool-bridge.js +0 -2173
  130. package/dist/core/engine/verification-patterns.js +0 -195
  131. package/dist/core/evaluation/golden-dataset.js +0 -293
  132. package/dist/core/feedback/queue.js +0 -177
  133. package/dist/core/feedback/submitter.js +0 -145
  134. package/dist/core/file-cache.js +0 -141
  135. package/dist/core/flatten/flatten-repo.js +0 -439
  136. package/dist/core/format/osc8-link.js +0 -28
  137. package/dist/core/hook-chains.js +0 -392
  138. package/dist/core/hooks/citation-verify-hook.js +0 -138
  139. package/dist/core/hooks/citation-verify.js +0 -112
  140. package/dist/core/hooks/events.js +0 -46
  141. package/dist/core/hooks/index.js +0 -15
  142. package/dist/core/hooks/registry.js +0 -216
  143. package/dist/core/hooks/runner.js +0 -236
  144. package/dist/core/hooks/v2/event-emitter.js +0 -115
  145. package/dist/core/hooks/v2/executor.js +0 -282
  146. package/dist/core/hooks/v2/index.js +0 -25
  147. package/dist/core/hooks/v2/lifecycle.js +0 -104
  148. package/dist/core/hooks/v2/loader.js +0 -216
  149. package/dist/core/hooks/v2/matcher.js +0 -125
  150. package/dist/core/hooks/v2/trust.js +0 -143
  151. package/dist/core/hooks/v2/types.js +0 -86
  152. package/dist/core/hooks/worktree-events.js +0 -158
  153. package/dist/core/hooks.js +0 -415
  154. package/dist/core/image/renderer.js +0 -71
  155. package/dist/core/index-store.js +0 -260
  156. package/dist/core/init/detector.js +0 -582
  157. package/dist/core/init/template-renderer.js +0 -242
  158. package/dist/core/jobs/registry.js +0 -462
  159. package/dist/core/ledger/results-tsv.js +0 -142
  160. package/dist/core/log-discipline/stdout-redirect.js +0 -51
  161. package/dist/core/lsp/cache.js +0 -105
  162. package/dist/core/lsp/client.js +0 -1229
  163. package/dist/core/lsp/language-detect.js +0 -66
  164. package/dist/core/lsp/post-edit-diagnostics.js +0 -171
  165. package/dist/core/lsp/server-detect.js +0 -173
  166. package/dist/core/lsp/symbol-cache.js +0 -162
  167. package/dist/core/lsp/symbol-tools.js +0 -664
  168. package/dist/core/mcp/client.js +0 -385
  169. package/dist/core/mcp/http-server.js +0 -553
  170. package/dist/core/mcp/orchestrator-config.js +0 -192
  171. package/dist/core/mcp/orchestrator-tools.js +0 -806
  172. package/dist/core/mcp/permission.js +0 -190
  173. package/dist/core/mcp/registry.js +0 -193
  174. package/dist/core/mcp/server-tools.js +0 -219
  175. package/dist/core/mcp/server.js +0 -397
  176. package/dist/core/mcp/trust.js +0 -91
  177. package/dist/core/memory/dual-write.js +0 -416
  178. package/dist/core/memory/passive-extract.js +0 -130
  179. package/dist/core/memory/phase1-kinds.js +0 -20
  180. package/dist/core/memory/secret-scanner.js +0 -304
  181. package/dist/core/memory-sync/queue.js +0 -170
  182. package/dist/core/metrics/extract.js +0 -113
  183. package/dist/core/modes/roo-modes.js +0 -68
  184. package/dist/core/onboarding/ensure-initialized.js +0 -133
  185. package/dist/core/onboarding/marker.js +0 -111
  186. package/dist/core/onboarding/telemetry-state.js +0 -108
  187. package/dist/core/output-style/presets.js +0 -176
  188. package/dist/core/output-style/state.js +0 -185
  189. package/dist/core/path-security.js +0 -345
  190. package/dist/core/permission.js +0 -369
  191. package/dist/core/permissions/auto-classifier.js +0 -124
  192. package/dist/core/permissions/bash-parser.js +0 -371
  193. package/dist/core/permissions/circuit-breaker.js +0 -83
  194. package/dist/core/permissions/constrained-edit.js +0 -91
  195. package/dist/core/permissions/gate.js +0 -278
  196. package/dist/core/permissions/index.js +0 -20
  197. package/dist/core/permissions/mode.js +0 -174
  198. package/dist/core/permissions/network-egress.js +0 -137
  199. package/dist/core/permissions/state.js +0 -241
  200. package/dist/core/permissions/tool-class.js +0 -107
  201. package/dist/core/plan-mode/ui-state.js +0 -51
  202. package/dist/core/plans/plan-artifact.js +0 -721
  203. package/dist/core/policy-limits/etag-store.js +0 -122
  204. package/dist/core/prd-check/parser.js +0 -215
  205. package/dist/core/prd-check/reporter.js +0 -127
  206. package/dist/core/prd-check/session-review.js +0 -557
  207. package/dist/core/prd-check/verifiers.js +0 -223
  208. package/dist/core/prompt-cache/client-cache.js +0 -99
  209. package/dist/core/prompts/assembly.js +0 -29
  210. package/dist/core/prompts/registry.js +0 -364
  211. package/dist/core/pugi-gitignore.js +0 -52
  212. package/dist/core/pugi-md/cc-compat-rules.js +0 -735
  213. package/dist/core/pugi-md/context-injector.js +0 -76
  214. package/dist/core/pugi-md/walk-up.js +0 -207
  215. package/dist/core/python/uv-installer.js +0 -270
  216. package/dist/core/python/uv-resolver.js +0 -83
  217. package/dist/core/rate-limit/narrator.js +0 -146
  218. package/dist/core/recipes/cli-types.js +0 -20
  219. package/dist/core/recipes/loader.js +0 -103
  220. package/dist/core/recipes/runner.js +0 -345
  221. package/dist/core/recipes/schema.js +0 -587
  222. package/dist/core/release-notes/parser.js +0 -241
  223. package/dist/core/release-notes/state.js +0 -116
  224. package/dist/core/repl/ask.js +0 -512
  225. package/dist/core/repl/cancellation.js +0 -98
  226. package/dist/core/repl/cap-warning.js +0 -91
  227. package/dist/core/repl/clipboard-read.js +0 -174
  228. package/dist/core/repl/dispatch-fsm.js +0 -220
  229. package/dist/core/repl/engine-bridge.js +0 -303
  230. package/dist/core/repl/history-search.js +0 -175
  231. package/dist/core/repl/history.js +0 -182
  232. package/dist/core/repl/kill-ring.js +0 -138
  233. package/dist/core/repl/model-pricing.js +0 -135
  234. package/dist/core/repl/privacy-banner.js +0 -71
  235. package/dist/core/repl/session.js +0 -4962
  236. package/dist/core/repl/slash-commands.js +0 -747
  237. package/dist/core/repl/store/index.js +0 -12
  238. package/dist/core/repl/store/jsonl-log.js +0 -321
  239. package/dist/core/repl/store/lockfile.js +0 -155
  240. package/dist/core/repl/store/session-store.js +0 -821
  241. package/dist/core/repl/store/types.js +0 -44
  242. package/dist/core/repl/store/uuid-v7.js +0 -68
  243. package/dist/core/repl/tool-route.js +0 -382
  244. package/dist/core/repl/workspace-context.js +0 -206
  245. package/dist/core/repo-map/build.js +0 -125
  246. package/dist/core/repo-map/cache.js +0 -185
  247. package/dist/core/repo-map/extractor.js +0 -254
  248. package/dist/core/repo-map/formatter.js +0 -145
  249. package/dist/core/repo-map/page-rank.js +0 -105
  250. package/dist/core/repo-map/scanner.js +0 -211
  251. package/dist/core/retro/git-collector.js +0 -251
  252. package/dist/core/retro/health-card.js +0 -25
  253. package/dist/core/retro/metrics.js +0 -342
  254. package/dist/core/retro/narrative.js +0 -249
  255. package/dist/core/retro/plane-collector.js +0 -274
  256. package/dist/core/retro/pr-issue-link.js +0 -65
  257. package/dist/core/retro/types.js +0 -16
  258. package/dist/core/retry-budget/budget.js +0 -284
  259. package/dist/core/retry-budget/index.js +0 -5
  260. package/dist/core/retry-budget/retry-cap.js +0 -74
  261. package/dist/core/routing/lead-worker.js +0 -43
  262. package/dist/core/routing/pre-flight-estimator.js +0 -108
  263. package/dist/core/runs/run-tree.js +0 -103
  264. package/dist/core/sandboxing/adapter.js +0 -29
  265. package/dist/core/sandboxing/index.js +0 -49
  266. package/dist/core/sandboxing/none.js +0 -19
  267. package/dist/core/sandboxing/seatbelt.js +0 -183
  268. package/dist/core/security/injection-scanner.js +0 -367
  269. package/dist/core/security/output-filter.js +0 -418
  270. package/dist/core/session/env-file.js +0 -105
  271. package/dist/core/session/section-budgets.js +0 -140
  272. package/dist/core/session.js +0 -377
  273. package/dist/core/settings.js +0 -400
  274. package/dist/core/share/formatter.js +0 -271
  275. package/dist/core/share/redactor.js +0 -221
  276. package/dist/core/share/uploader.js +0 -267
  277. package/dist/core/skills/defaults.js +0 -457
  278. package/dist/core/skills/loader.js +0 -454
  279. package/dist/core/skills/sources.js +0 -480
  280. package/dist/core/skills/trust.js +0 -172
  281. package/dist/core/smoke/headless-driver.js +0 -174
  282. package/dist/core/smoke/orchestrator.js +0 -194
  283. package/dist/core/smoke/runner.js +0 -238
  284. package/dist/core/smoke/scenario-parser.js +0 -316
  285. package/dist/core/statusline.js +0 -99
  286. package/dist/core/subagents/dispatcher-real.js +0 -600
  287. package/dist/core/subagents/dispatcher.js +0 -352
  288. package/dist/core/subagents/index.js +0 -39
  289. package/dist/core/subagents/isolation-matrix.js +0 -213
  290. package/dist/core/subagents/spawn.js +0 -101
  291. package/dist/core/telemetry/emitter.js +0 -229
  292. package/dist/core/telemetry/queue.js +0 -251
  293. package/dist/core/theme/context.js +0 -91
  294. package/dist/core/theme/presets.js +0 -228
  295. package/dist/core/theme/state.js +0 -181
  296. package/dist/core/todos/invariant.js +0 -10
  297. package/dist/core/todos/state.js +0 -177
  298. package/dist/core/tool-schema/compressor.js +0 -89
  299. package/dist/core/transport/version-interceptor.js +0 -166
  300. package/dist/core/trust.js +0 -109
  301. package/dist/core/tui/thinking-block.js +0 -64
  302. package/dist/core/vim/keymap.js +0 -288
  303. package/dist/core/vim/state.js +0 -92
  304. package/dist/core/watch-markers/marker-watcher.js +0 -133
  305. package/dist/core/worktree/include-parser.js +0 -249
  306. package/dist/core/worktree-manager/cleanup.js +0 -123
  307. package/dist/core/worktree-manager/manager.js +0 -303
  308. package/dist/index.js +0 -44
  309. package/dist/runtime/bootstrap.js +0 -190
  310. package/dist/runtime/cli.js +0 -8121
  311. package/dist/runtime/commands/agents.js +0 -385
  312. package/dist/runtime/commands/budget.js +0 -192
  313. package/dist/runtime/commands/cancel.js +0 -231
  314. package/dist/runtime/commands/chain.js +0 -489
  315. package/dist/runtime/commands/codegraph-status.js +0 -227
  316. package/dist/runtime/commands/compact.js +0 -297
  317. package/dist/runtime/commands/config.js +0 -595
  318. package/dist/runtime/commands/cost.js +0 -199
  319. package/dist/runtime/commands/delegate.js +0 -312
  320. package/dist/runtime/commands/dispatch.js +0 -126
  321. package/dist/runtime/commands/doctor.js +0 -579
  322. package/dist/runtime/commands/feedback.js +0 -184
  323. package/dist/runtime/commands/hooks.js +0 -187
  324. package/dist/runtime/commands/init.js +0 -254
  325. package/dist/runtime/commands/lsp.js +0 -368
  326. package/dist/runtime/commands/mcp.js +0 -935
  327. package/dist/runtime/commands/memory.js +0 -582
  328. package/dist/runtime/commands/model.js +0 -237
  329. package/dist/runtime/commands/onboarding.js +0 -275
  330. package/dist/runtime/commands/patch.js +0 -128
  331. package/dist/runtime/commands/permissions.js +0 -112
  332. package/dist/runtime/commands/plan.js +0 -143
  333. package/dist/runtime/commands/prd-check.js +0 -285
  334. package/dist/runtime/commands/privacy.js +0 -107
  335. package/dist/runtime/commands/recipe.js +0 -325
  336. package/dist/runtime/commands/redo-blob-store.js +0 -92
  337. package/dist/runtime/commands/redo.js +0 -361
  338. package/dist/runtime/commands/release-notes.js +0 -229
  339. package/dist/runtime/commands/repo-map.js +0 -95
  340. package/dist/runtime/commands/report.js +0 -299
  341. package/dist/runtime/commands/resume.js +0 -118
  342. package/dist/runtime/commands/review-consensus.js +0 -414
  343. package/dist/runtime/commands/rewind.js +0 -333
  344. package/dist/runtime/commands/roster.js +0 -117
  345. package/dist/runtime/commands/sessions.js +0 -163
  346. package/dist/runtime/commands/share.js +0 -316
  347. package/dist/runtime/commands/skills.js +0 -401
  348. package/dist/runtime/commands/status.js +0 -186
  349. package/dist/runtime/commands/stickers.js +0 -82
  350. package/dist/runtime/commands/style.js +0 -194
  351. package/dist/runtime/commands/theme.js +0 -196
  352. package/dist/runtime/commands/undo.js +0 -361
  353. package/dist/runtime/commands/update.js +0 -289
  354. package/dist/runtime/commands/vim.js +0 -140
  355. package/dist/runtime/commands/worktree.js +0 -177
  356. package/dist/runtime/commands/worktrees.js +0 -155
  357. package/dist/runtime/deprecation-warning.js +0 -69
  358. package/dist/runtime/engine-exit-code.js +0 -50
  359. package/dist/runtime/headless-repl.js +0 -195
  360. package/dist/runtime/headless.js +0 -548
  361. package/dist/runtime/load-hooks-or-exit.js +0 -71
  362. package/dist/runtime/plan-decompose.js +0 -531
  363. package/dist/runtime/sigint-guard.js +0 -272
  364. package/dist/runtime/stream-renderer.js +0 -195
  365. package/dist/runtime/update-check.js +0 -294
  366. package/dist/runtime/version.js +0 -65
  367. package/dist/runtime/worktree-bootstrap.js +0 -579
  368. package/dist/skills/bundled/batch.js +0 -617
  369. package/dist/skills/bundled/index.js +0 -45
  370. package/dist/skills/bundled/loop.js +0 -358
  371. package/dist/skills/bundled/remember.js +0 -383
  372. package/dist/skills/bundled/simplify.js +0 -289
  373. package/dist/skills/bundled/skillify.js +0 -373
  374. package/dist/skills/bundled/stuck.js +0 -558
  375. package/dist/skills/bundled/verify.js +0 -439
  376. package/dist/testing/vcr.js +0 -486
  377. package/dist/tools/agent-tool.js +0 -229
  378. package/dist/tools/apply-patch.js +0 -556
  379. package/dist/tools/ask-user-question.js +0 -337
  380. package/dist/tools/ask-user.js +0 -115
  381. package/dist/tools/bash.js +0 -1238
  382. package/dist/tools/brief.js +0 -224
  383. package/dist/tools/cron.js +0 -433
  384. package/dist/tools/enter-worktree.js +0 -250
  385. package/dist/tools/exit-worktree.js +0 -147
  386. package/dist/tools/file-tools.js +0 -553
  387. package/dist/tools/http-request.js +0 -336
  388. package/dist/tools/lsp-tools.js +0 -565
  389. package/dist/tools/mcp-tool.js +0 -260
  390. package/dist/tools/multi-edit.js +0 -361
  391. package/dist/tools/powershell.js +0 -268
  392. package/dist/tools/registry.js +0 -166
  393. package/dist/tools/server-tools.js +0 -892
  394. package/dist/tools/skill-tool.js +0 -96
  395. package/dist/tools/sleep.js +0 -99
  396. package/dist/tools/synthetic-output.js +0 -133
  397. package/dist/tools/tasks.js +0 -208
  398. package/dist/tools/todo-write.js +0 -184
  399. package/dist/tools/verify-plan-execution.js +0 -295
  400. package/dist/tools/web-fetch-injection-scanner.js +0 -207
  401. package/dist/tools/web-fetch.js +0 -720
  402. package/dist/tools/web-search.js +0 -458
  403. package/dist/tui/agent-progress-card.js +0 -111
  404. package/dist/tui/agent-tree-pane.js +0 -9
  405. package/dist/tui/agent-tree.js +0 -87
  406. package/dist/tui/ask-cli.js +0 -52
  407. package/dist/tui/ask-modal.js +0 -211
  408. package/dist/tui/ask-user-question-chips.js +0 -315
  409. package/dist/tui/ask-user-question-prompt.js +0 -203
  410. package/dist/tui/compact-banner.js +0 -81
  411. package/dist/tui/conversation-pane.js +0 -164
  412. package/dist/tui/cost-table.js +0 -111
  413. package/dist/tui/device-flow.js +0 -142
  414. package/dist/tui/doctor-table.js +0 -46
  415. package/dist/tui/feedback-prompt.js +0 -156
  416. package/dist/tui/input-box.js +0 -732
  417. package/dist/tui/login-picker.js +0 -69
  418. package/dist/tui/markdown-render.js +0 -266
  419. package/dist/tui/multi-file-diff-approval.js +0 -375
  420. package/dist/tui/onboarding-wizard.js +0 -240
  421. package/dist/tui/permissions-picker.js +0 -86
  422. package/dist/tui/render.js +0 -160
  423. package/dist/tui/repl-render.js +0 -770
  424. package/dist/tui/repl-splash-art.js +0 -64
  425. package/dist/tui/repl-splash-mascot.js +0 -154
  426. package/dist/tui/repl-splash.js +0 -117
  427. package/dist/tui/repl.js +0 -378
  428. package/dist/tui/slash-palette.js +0 -106
  429. package/dist/tui/splash-data.js +0 -61
  430. package/dist/tui/splash.js +0 -31
  431. package/dist/tui/status-bar.js +0 -209
  432. package/dist/tui/status-table.js +0 -7
  433. package/dist/tui/stickers-art.js +0 -136
  434. package/dist/tui/style-table.js +0 -28
  435. package/dist/tui/theme-table.js +0 -29
  436. package/dist/tui/thinking-spinner.js +0 -123
  437. package/dist/tui/tool-stream-pane.js +0 -140
  438. package/dist/tui/update-banner.js +0 -33
  439. package/dist/tui/vim-input.js +0 -267
  440. package/dist/tui/welcome-banner.js +0 -107
  441. package/dist/tui/welcome-data.js +0 -293
  442. package/dist/tui/workspace-context.js +0 -105
  443. package/docs/examples/codegraph.mcp.json +0 -10
  444. package/test/scenarios/codegen-create-file.scenario.txt +0 -13
  445. package/test/scenarios/compact-force.scenario.txt +0 -12
  446. package/test/scenarios/identity.scenario.txt +0 -11
  447. package/test/scenarios/persona-handoff.scenario.txt +0 -12
  448. package/test/scenarios/walkback.scenario.txt +0 -12
@@ -1,1397 +0,0 @@
1
- /**
2
- * Bash command classifier — Sprint .
3
- *
4
- * Splits a shell command into a 7-class taxonomy so the permission
5
- * engine can apply class-aware policy instead of the prior bool gate
6
- * (`destructiveBashPatterns ? deny : ask`).
7
- *
8
- * Design notes:
9
- * - The classifier is a conservative pattern matcher, not a full
10
- * bash AST parser. M2 will replace it with a real parser (see
11
- * bash-security.md §4). For M1 the rules are explicit-substring +
12
- * simple tokenization, which is good enough to gate every command
13
- * the engine loop currently emits.
14
- * - Compound commands (`a && b`, `a || b`, `a ; b`, `a | b`) are
15
- * split on the four separators and every component is classified
16
- * individually. The overall class is the most dangerous component.
17
- * - The `destructive` patterns originally lived in
18
- * `permission.ts::destructiveBashPatterns`. They are now the
19
- * single source of truth here; `permission.ts` re-exports the
20
- * hard-deny check through `classifyBash`.
21
- * - The `unknown` class fires on parse failure (`eval`, deep
22
- * `$(...)` nesting, `curl | sh` install pipes) so the permission
23
- * engine can fail closed in interactive modes.
24
- */
25
- /**
26
- * Class rank for worst-component reduction in compound commands.
27
- *
28
- * `unknown` ranks ABOVE `read` and `build_test` so that a chain like
29
- * `pwd && bash ./payload.sh` does not silently disarm the fail-closed
30
- * unknown gate when the worst-component loop reduces over the
31
- * components. The matrix in `permission.ts` treats `unknown` as
32
- * `deny` in `plan`/`dontAsk` and `ask` everywhere else; this rank
33
- * placement preserves that fail-closed posture for compounds while
34
- * still letting genuine `write_workspace`, `network`, `write_protected`
35
- * and `destructive` components win when they appear.
36
- *
37
- * Code Reviewer P0 retro: previously `unknown: 0` meant
38
- * `read` (rank 1) won over `unknown` (rank 0) in the worst-component
39
- * reduction. That bypassed the file-level promise of fail-closed on
40
- * parse failure.
41
- */
42
- const CLASS_RANK = {
43
- destructive: 7,
44
- write_protected: 6,
45
- write_workspace: 5,
46
- network: 4,
47
- unknown: 3,
48
- build_test: 2,
49
- read: 1,
50
- };
51
- const DESTRUCTIVE_PATTERNS = [
52
- // Filesystem wipe
53
- { pattern: 'rm -rf /' },
54
- { pattern: 'rm -rf ~' },
55
- { pattern: 'rm -rf .' },
56
- { pattern: 'rm -rf *' },
57
- { pattern: 'rm -rf "/' },
58
- { pattern: 'rm -rf "~' },
59
- { pattern: 'rm -rf .git' },
60
- { pattern: 'rm -r /' },
61
- { pattern: 'rm -r ~' },
62
- { pattern: 'rm -r .git' },
63
- { pattern: 'sudo rm -rf' },
64
- { pattern: 'sudo rm -r' },
65
- { pattern: 'dd if=/dev/zero' },
66
- { pattern: 'dd if=/dev/random' },
67
- { pattern: 'dd of=/dev/' },
68
- { pattern: 'mkfs' },
69
- { pattern: 'shred ' },
70
- { pattern: 'wipefs' },
71
- { pattern: '> /dev/sda' },
72
- { pattern: '> /dev/disk' },
73
- // Permission wipe
74
- { pattern: 'chmod 777 /' },
75
- { pattern: 'chmod -R 777 /' },
76
- { pattern: 'chmod -R 777 ~' },
77
- { pattern: 'chown -R root /' },
78
- { pattern: 'chown -R / ' },
79
- // Shell tricks
80
- { pattern: ':(){ :|:& };:' },
81
- { pattern: 'eval "$' },
82
- { pattern: "eval '$" },
83
- // Git history loss
84
- { pattern: 'git reset --hard' },
85
- { pattern: 'git clean -fdx' },
86
- { pattern: 'git push --force origin main' },
87
- { pattern: 'git push -f origin main' },
88
- { pattern: 'git push --force origin master' },
89
- { pattern: 'git push -f origin master' },
90
- { pattern: 'git push --force origin production' },
91
- { pattern: 'git push -f origin production' },
92
- // Container / infra
93
- { pattern: 'docker system prune' },
94
- { pattern: 'docker rm -f $(docker' },
95
- { pattern: 'kubectl delete --all' },
96
- { pattern: 'kubectl delete namespace' },
97
- { pattern: 'terraform destroy' },
98
- // SQL destructive (case-insensitive — model can emit any case).
99
- { pattern: 'DROP DATABASE', caseInsensitive: true },
100
- { pattern: 'DROP TABLE', caseInsensitive: true },
101
- { pattern: 'TRUNCATE TABLE', caseInsensitive: true },
102
- // Firewall / network
103
- { pattern: 'ufw disable' },
104
- { pattern: 'iptables -F' },
105
- { pattern: 'iptables --flush' },
106
- // Credential exfil
107
- { pattern: 'cat ~/.ssh/id_rsa' },
108
- { pattern: 'cat ~/.ssh/id_ed25519' },
109
- { pattern: 'gpg --export-secret' },
110
- // SSH config write paths (reads are OK; only redirections/tee block)
111
- { pattern: '> sshd_config' },
112
- { pattern: '>> sshd_config' },
113
- { pattern: '> /etc/ssh/sshd_config' },
114
- { pattern: '>> /etc/ssh/sshd_config' },
115
- { pattern: 'tee sshd_config' },
116
- { pattern: 'tee /etc/ssh/sshd_config' },
117
- { pattern: 'tee -a sshd_config' },
118
- { pattern: 'tee -a /etc/ssh/sshd_config' },
119
- // History destruction
120
- { pattern: 'history -c' },
121
- { pattern: ' >/dev/null 2>&1; rm' },
122
- // ---------------------------------------------------------------
123
- // Patterns ported from external utility (Apache-2.0)
124
- // and `safety-guard.sh` BLOCKED_PATTERNS array. Upstream source:
125
- // external hooks/destructive-guard.sh (lines 7-13)
126
- // external hooks/safety-guard.sh (lines 14-50)
127
- //
128
- //
129
- // The patterns below need word-boundary matching because their
130
- // tokens (kill, halt, reboot, ...) appear as substrings of common
131
- // unrelated words (skills, default, chrooted-rebooter, etc.).
132
- // Substring `.includes` cannot express that — `regex` is required.
133
- // ---------------------------------------------------------------
134
- // Process termination — `kill`, `pkill`, `killall` at command head
135
- // or after `sudo`. Matches `kill 1234`, `kill -9 $$`, `sudo killall
136
- // node`, but NOT `skill issue` (no leading boundary) or
137
- // `git commit -m "skill kill story"` (the kill is inside a quoted
138
- // string — quote-aware split handled upstream; here we still need
139
- // the boundary). Anchored to start-of-component or `sudo ` prefix.
140
- {
141
- pattern: 'kill',
142
- regex: /^(?:sudo\s+)?(?:pkill|killall|kill)\b/,
143
- },
144
- // System power state — reboot / shutdown / halt / poweroff / init 0
145
- // / init 6. External tooling matches these anywhere in the command; we
146
- // tighten to start-of-component or `sudo ` prefix to avoid FPs on
147
- // file paths or variable names containing the substring.
148
- {
149
- pattern: 'reboot',
150
- regex: /^(?:sudo\s+)?reboot\b/,
151
- },
152
- {
153
- pattern: 'shutdown',
154
- regex: /^(?:sudo\s+)?shutdown\b/,
155
- },
156
- {
157
- pattern: 'halt',
158
- regex: /^(?:sudo\s+)?halt\b/,
159
- },
160
- {
161
- pattern: 'poweroff',
162
- regex: /^(?:sudo\s+)?poweroff\b/,
163
- },
164
- {
165
- pattern: 'init 0',
166
- regex: /^(?:sudo\s+)?init\s+0\b/,
167
- },
168
- {
169
- pattern: 'init 6',
170
- regex: /^(?:sudo\s+)?init\s+6\b/,
171
- },
172
- // `git clean -f` (without -dx) — External tooling lists this as destructive
173
- // because it still deletes untracked files. Pugi previously only
174
- // gated `git clean -fdx`; broaden to any `-f` variant.
175
- {
176
- pattern: 'git clean -f',
177
- regex: /\bgit\s+clean\s+-[A-Za-z]*f/,
178
- },
179
- ];
180
- /**
181
- * Compound separators. We split on `&&`, `||`, `;`, `|` to classify
182
- * each component, then pick the most dangerous. `&` (background fork)
183
- * is intentionally NOT a separator — backgrounding does not change
184
- * what runs, only when.
185
- */
186
- const COMPOUND_SEPARATORS = /\s*(?:&&|\|\||;|\|)\s*/;
187
- /**
188
- * Split a shell command on compound separators (`&&`, `||`, `;`, `|`)
189
- * while RESPECTING quoted strings (`'...'`, `"..."`, `` `...` ``) so
190
- * that script bodies passed to `awk`, `sed`, `perl`, `python -c` are
191
- * not mis-split when they contain bare `;` or `|` glyphs.
192
- *
193
- * Code Reviewer P0 retro: a naive regex split on
194
- * `awk 'BEGIN { for (i=0;i<5000;i++) ... }'` produces 3 components
195
- * (the awk script header + two for-loop fragments) that get
196
- * classified as `unknown` each and — with the unknown:3 rank above
197
- * read:1 — escalate the overall verdict to `unknown`, breaking
198
- * legitimate read-class scripts.
199
- */
200
- function splitCompoundRespectingQuotes(cmd) {
201
- const out = [];
202
- let buf = '';
203
- let inSingle = false;
204
- let inDouble = false;
205
- let inBacktick = false;
206
- for (let i = 0; i < cmd.length; i += 1) {
207
- const ch = cmd[i];
208
- const prev = i > 0 ? cmd[i - 1] : '';
209
- if (ch === '\\') {
210
- buf += ch;
211
- if (i + 1 < cmd.length) {
212
- buf += cmd[i + 1];
213
- i += 1;
214
- }
215
- continue;
216
- }
217
- if (!inDouble && !inBacktick && ch === "'") {
218
- inSingle = !inSingle;
219
- buf += ch;
220
- continue;
221
- }
222
- if (!inSingle && !inBacktick && ch === '"') {
223
- inDouble = !inDouble;
224
- buf += ch;
225
- continue;
226
- }
227
- if (!inSingle && !inDouble && ch === '`') {
228
- inBacktick = !inBacktick;
229
- buf += ch;
230
- continue;
231
- }
232
- if (!inSingle && !inDouble && !inBacktick) {
233
- // `&&`
234
- if (ch === '&' && cmd[i + 1] === '&') {
235
- out.push(buf.trim());
236
- buf = '';
237
- i += 1;
238
- continue;
239
- }
240
- // `||`
241
- if (ch === '|' && cmd[i + 1] === '|') {
242
- out.push(buf.trim());
243
- buf = '';
244
- i += 1;
245
- continue;
246
- }
247
- // `;` (single semicolon, not part of `;;`)
248
- if (ch === ';' && cmd[i + 1] !== ';' && prev !== ';') {
249
- out.push(buf.trim());
250
- buf = '';
251
- continue;
252
- }
253
- // bare `|` (single pipe, not `||` and not `|&`)
254
- if (ch === '|' && cmd[i + 1] !== '|' && cmd[i + 1] !== '&') {
255
- out.push(buf.trim());
256
- buf = '';
257
- continue;
258
- }
259
- }
260
- buf += ch;
261
- }
262
- const tail = buf.trim();
263
- if (tail !== '')
264
- out.push(tail);
265
- return out.filter((s) => s !== '');
266
- }
267
- /** Network commands. */
268
- const NETWORK_TOKENS = new Set([
269
- 'curl',
270
- 'wget',
271
- 'ssh',
272
- 'scp',
273
- 'rsync',
274
- 'nc',
275
- 'netcat',
276
- ]);
277
- const NETWORK_PREFIXES = [
278
- 'git clone',
279
- 'git fetch',
280
- 'git pull',
281
- 'git push',
282
- 'npm install',
283
- 'npm i ',
284
- 'npm ci',
285
- 'pnpm install',
286
- 'pnpm i ',
287
- 'pnpm add',
288
- 'yarn install',
289
- 'yarn add',
290
- 'pip install',
291
- 'pip3 install',
292
- 'brew install',
293
- 'brew upgrade',
294
- 'apt-get',
295
- 'apt install',
296
- 'yum install',
297
- 'dnf install',
298
- 'docker pull',
299
- 'docker push',
300
- 'cargo install',
301
- 'go get',
302
- 'go install',
303
- ];
304
- /**
305
- * Build / test prefixes. These are common enough that the permission
306
- * engine grants them auto in `acceptEdits`/`auto` modes (the rule of
307
- * thumb is "ask first time, then allow rule" per bash-security.md §3).
308
- *
309
- * IMPORTANT: every prefix here must NOT also match a network installer
310
- * (we handle `npm install` / `pnpm install` before this list).
311
- */
312
- const BUILD_TEST_PREFIXES = [
313
- 'pnpm test',
314
- 'pnpm build',
315
- 'pnpm typecheck',
316
- 'pnpm lint',
317
- 'pnpm run test',
318
- 'pnpm run build',
319
- 'pnpm run typecheck',
320
- 'pnpm run lint',
321
- 'npm test',
322
- 'npm run build',
323
- 'npm run lint',
324
- 'npm run typecheck',
325
- 'npm run test',
326
- 'yarn test',
327
- 'yarn build',
328
- 'yarn lint',
329
- 'cargo test',
330
- 'cargo build',
331
- 'cargo check',
332
- 'go test',
333
- 'go build',
334
- 'go vet',
335
- 'pytest',
336
- 'jest',
337
- 'vitest',
338
- 'make test',
339
- 'make build',
340
- 'make check',
341
- 'mvn test',
342
- 'mvn package',
343
- 'gradle test',
344
- 'gradle build',
345
- 'tsc --noEmit',
346
- 'tsc -p',
347
- 'eslint',
348
- 'prettier --check',
349
- // P0 fix (#37 CRITICAL): customer-blocking gap surfaced
350
- // during dogfood. Engine emitted `chmod +x build.sh`, `node script.js`,
351
- // `python3 -m pytest`, `git status`, `pnpm build`, `docker ps`, etc.
352
- // and the classifier returned `unknown` → permission matrix denied
353
- // в bypassPermissions mode (which the customer expected to auto-allow
354
- // basic dev tools). Customers could not run ANY real build/test/git
355
- // workflow through Pugi.
356
- //
357
- // The prefixes below cover three classes of developer tooling that
358
- // are always allowed in `auto`/`dontAsk`/`bypassPermissions` modes,
359
- // `ask` in interactive modes, and `deny` in `plan` (read-only) mode:
360
- // - Language runtimes: `node`, `python`, `python3`, `ruby`, etc.
361
- // - Native build chains: `gcc`, `clang`, `cmake`, `rustc`, etc.
362
- // - Container/k8s read-class: `docker ps/inspect/logs`, `kubectl get`.
363
- //
364
- // Destructive variants are already gated upstream by DESTRUCTIVE_PATTERNS
365
- // (e.g. `docker system prune`, `kubectl delete --all`). The first-token
366
- // gate in classifyComponent runs THIS list before the unknown fallback.
367
- //
368
- // Language runtime invocations (first-token match, with or without args).
369
- 'node',
370
- 'python',
371
- 'python3',
372
- 'ruby',
373
- 'perl',
374
- 'php',
375
- 'deno',
376
- 'bun',
377
- 'tsx',
378
- 'ts-node',
379
- // Native build chains.
380
- 'gcc',
381
- 'g++',
382
- 'clang',
383
- 'clang++',
384
- 'cmake',
385
- 'rustc',
386
- 'javac',
387
- 'java',
388
- // Container/k8s read-class (the destructive subcommands are pre-empted
389
- // by DESTRUCTIVE_PATTERNS: `docker system prune`, `kubectl delete --all`,
390
- // `kubectl delete namespace`).
391
- 'docker ps',
392
- 'docker images',
393
- 'docker inspect',
394
- 'docker logs',
395
- 'docker version',
396
- 'docker info',
397
- 'docker exec',
398
- 'docker run',
399
- 'docker stop',
400
- 'docker start',
401
- 'docker restart',
402
- 'docker rm',
403
- 'docker rmi',
404
- 'docker build',
405
- 'docker tag',
406
- 'docker compose',
407
- 'docker-compose',
408
- 'kubectl get',
409
- 'kubectl describe',
410
- 'kubectl logs',
411
- 'kubectl exec',
412
- 'kubectl apply',
413
- 'kubectl create',
414
- 'kubectl rollout',
415
- 'kubectl port-forward',
416
- 'kubectl config',
417
- // Git read+write surface (network ops already handled by NETWORK_PREFIXES;
418
- // destructive ops `reset --hard`/`clean -fdx`/`push --force` blocked above).
419
- // Note: WRITE_WORKSPACE_PREFIXES already covers `git commit/add/checkout/...`.
420
- // These entries handle plain `git rev-list`, `git cherry-pick`, `git worktree`,
421
- // `git submodule`, etc that customer scripts commonly invoke.
422
- 'git rev-list',
423
- 'git cherry-pick',
424
- 'git worktree',
425
- 'git submodule',
426
- 'git blame',
427
- 'git describe',
428
- 'git tag --list',
429
- 'git tag -l',
430
- 'git for-each-ref',
431
- 'git ls-remote',
432
- // gh CLI (GitHub). `gh repo delete` / `gh release delete` reach into
433
- // network operations but are non-destructive for the local workspace.
434
- // Permission matrix asks before allowing in auto.
435
- 'gh',
436
- ];
437
- /** Single-token read-only commands. Argument-free entries match exact. */
438
- const READ_TOKENS = new Set([
439
- 'pwd',
440
- 'ls',
441
- 'cat',
442
- 'head',
443
- 'tail',
444
- 'wc',
445
- 'which',
446
- 'whereis',
447
- 'file',
448
- 'stat',
449
- 'du',
450
- 'df',
451
- 'echo',
452
- 'printenv',
453
- 'env',
454
- 'date',
455
- 'uname',
456
- 'hostname',
457
- 'id',
458
- 'whoami',
459
- 'true',
460
- 'false',
461
- 'basename',
462
- 'dirname',
463
- 'realpath',
464
- // `sleep` has no FS/network/proc impact beyond a timer; treated as
465
- // read so background jobs can use it without tripping the unknown
466
- // gate. Same logic for the no-op coreutils below.
467
- 'sleep',
468
- 'yes',
469
- 'seq',
470
- 'tr',
471
- 'cut',
472
- 'sort',
473
- 'uniq',
474
- // P0 fix (#37 CRITICAL): structured-data inspection tools
475
- // are pure stdin/stdout transformers (no FS write, no network) when
476
- // не paired с `>` redirection (the redirection branch above promotes
477
- // к write_workspace independently). Common в dev scripts for parsing
478
- // package.json, tsconfig.json, Helm values.yaml, etc.
479
- // `tee` is INTENTIONALLY excluded — it writes by definition, even
480
- // в protected paths (`tee /etc/...` is already in DESTRUCTIVE_PATTERNS).
481
- 'jq',
482
- 'yq',
483
- 'column',
484
- ]);
485
- const READ_PREFIXES = [
486
- 'git status',
487
- 'git log',
488
- 'git diff',
489
- 'git show',
490
- 'git branch',
491
- 'git remote',
492
- 'git rev-parse',
493
- 'git ls-files',
494
- 'git config --get',
495
- 'less ',
496
- 'more ',
497
- 'grep ',
498
- 'rg ',
499
- 'fd ',
500
- 'tree',
501
- ];
502
- /** Write_workspace prefixes. Destination boundary is checked separately. */
503
- const WRITE_WORKSPACE_PREFIXES = [
504
- 'mkdir',
505
- 'touch',
506
- 'cp ',
507
- 'mv ',
508
- 'ln ',
509
- 'git commit',
510
- 'git add',
511
- 'git checkout',
512
- 'git switch',
513
- 'git restore',
514
- 'git stash',
515
- 'git tag',
516
- 'git rebase',
517
- 'git merge',
518
- // P0 fix (#37 CRITICAL): file-permission ops are common
519
- // в build scripts (`chmod +x build.sh`, `chown $USER file`). The
520
- // destructive variants (`chmod 777 /`, `chmod -R 777 /`, `chmod -R
521
- // 777 ~`, `chown -R root /`, `chown -R / ...`) are pre-empted by
522
- // DESTRUCTIVE_PATTERNS which runs BEFORE this list — safe to add
523
- // here for the non-destructive path. detectProtectedWrite's `\bchmod\b`
524
- // / `\bchown\b` regex also catches writes into protected paths
525
- // regardless of this list.
526
- 'chmod ',
527
- 'chown ',
528
- ];
529
- /**
530
- * Protected-write triggers. If a command writes to any of these paths
531
- * the class is `write_protected` regardless of the operation type.
532
- *
533
- * Wildcards are handled as substring matches (e.g. `/.ssh/` matches
534
- * `~/.ssh/foo` and `[HOME]/USER/.ssh/bar`).
535
- */
536
- const PROTECTED_PATH_SUBSTRINGS = [
537
- '/.ssh/',
538
- '/.aws/',
539
- '/.gnupg/',
540
- '/.config/',
541
- '~/.ssh/',
542
- '~/.aws/',
543
- '~/.gnupg/',
544
- '~/.config/',
545
- '~/.npmrc',
546
- '~/.pypirc',
547
- '~/.bashrc',
548
- '~/.zshrc',
549
- '~/.profile',
550
- '~/.bash_profile',
551
- '/etc/',
552
- '/usr/',
553
- '/var/',
554
- ];
555
- /**
556
- * Protected basename triggers — files whose CONTENT must never leak
557
- * through the bash surface, even when the literal path is workspace-
558
- * local. Mirrors `permission.ts::protectedBasenames` and `.env.*`
559
- * pattern so the read-tool gate (which fires on `read .env`) and the
560
- * bash gate (which fires on `cat .env`) stay symmetric.
561
- *
562
- * P0 fix (Codex audit): before this list existed, the
563
- * engine model could circumvent the `read` tool's `protectedTargetReason`
564
- * check by emitting `bash cat .env` — the classifier saw `cat` (read
565
- * token) + `.env` (not in PROTECTED_PATH_SUBSTRINGS) and returned class
566
- * `read`, which the permission matrix allows under every mode. The
567
- * `local-first-invariants` spec proved the leak: `pugi explain .env`
568
- * surfaced `SECRET=should_never_leak` in the engine summary.
569
- *
570
- * Match shape: the substring must touch a `.` boundary (`/.env`,
571
- * ` .env`, `.env\b`) or appear as the full token so a path like
572
- * `apps/codeforge/file.env-template` (no real secret) does not
573
- * over-trigger.
574
- */
575
- const PROTECTED_BASENAME_PATTERNS = [
576
- // `.env`, `.env.production`, `.env.local` — anywhere in the command.
577
- // Boundary on the left is start/whitespace/quote/`/`, on the right
578
- // start/whitespace/end/quote/`>`/`|`/`;`.
579
- /(^|[\s'"\/=])\.env(\.[A-Za-z0-9_-]+)?($|[\s'"<>|;&])/,
580
- // SSH key basenames (covers both `id_rsa` and `id_ed25519` even
581
- // outside `~/.ssh/`). The `/.ssh/` substring above gates the
582
- // directory case; this catches a key file copied to the workspace.
583
- /(^|[\s'"\/])id_(rsa|ed25519|ecdsa|dsa)(\.pub)?($|[\s'"<>|;&])/,
584
- // Other credential basenames mirrored from permission.ts.
585
- /(^|[\s'"\/])\.npmrc($|[\s'"<>|;&])/,
586
- /(^|[\s'"\/])\.pypirc($|[\s'"<>|;&])/,
587
- /(^|[\s'"\/])\.gitconfig($|[\s'"<>|;&])/,
588
- ];
589
- /**
590
- * Obfuscation triggers — any of these forces the `unknown` class so
591
- * the permission engine can fail closed.
592
- */
593
- const OBFUSCATION_TRIGGERS = [
594
- { needle: 'curl', reason: 'curl piped into shell installer' },
595
- { needle: 'wget', reason: 'wget piped into shell installer' },
596
- ];
597
- /**
598
- * Classify a single (non-compound) command component.
599
- *
600
- * Order of checks (most-specific first):
601
- * 1. destructive substring (hard deny path)
602
- * 2. obfuscation (curl|sh, deep $() nesting, raw eval)
603
- * 3. cd-escape (covered by classifyBash for the overall command;
604
- * single-component cd is handled here too)
605
- * 4. protected-write (redirection or write op into a protected path)
606
- * 5. write_workspace (mkdir/touch/cp/mv/git-write etc)
607
- * 6. network (curl/wget/ssh/installers)
608
- * 7. build_test (pnpm test, cargo build, ...)
609
- * 8. read (pwd, ls, cat, ...)
610
- * 9. unknown (default)
611
- */
612
- function classifyComponent(cmd, ctx) {
613
- const trimmed = cmd.trim();
614
- if (trimmed === '') {
615
- return { class: 'unknown', reason: 'empty component', matched: '' };
616
- }
617
- // 1. Destructive hard-deny patterns.
618
- const destructive = findDestructiveMatch(trimmed);
619
- if (destructive) {
620
- return {
621
- class: 'destructive',
622
- reason: `Destructive command pattern matched: ${destructive}`,
623
- matched: destructive,
624
- };
625
- }
626
- // 2. Obfuscation — curl|sh, wget|bash, deep $() nesting, raw eval.
627
- const obfuscation = detectObfuscation(trimmed);
628
- if (obfuscation) {
629
- return {
630
- class: 'unknown',
631
- reason: obfuscation.reason,
632
- matched: obfuscation.matched,
633
- };
634
- }
635
- // 3. find with -delete / -exec is destructive-adjacent.
636
- if (/\bfind\b[^|;]*\s-(?:delete|exec\b)/.test(trimmed)) {
637
- return {
638
- class: 'destructive',
639
- reason: 'find with -delete or -exec is treated as destructive',
640
- matched: 'find ... -delete|-exec',
641
- };
642
- }
643
- // 4. Protected-write check (redirection OR write op into protected path).
644
- const protectedWrite = detectProtectedWrite(trimmed, ctx);
645
- if (protectedWrite) {
646
- return {
647
- class: 'write_protected',
648
- reason: protectedWrite.reason,
649
- matched: protectedWrite.matched,
650
- };
651
- }
652
- // 4a. Protected-read check. Reads from credential / config paths
653
- // (`cat ~/.aws/credentials`, `head ~/.npmrc`, `grep . ~/.ssh/id_ed25519`,
654
- // `tail -f ~/.bash_history`) classify as `write_protected` so the
655
- // permission matrix gates them in plan/dontAsk and asks elsewhere.
656
- // The hard-coded DESTRUCTIVE entries for `cat ~/.ssh/id_rsa` /
657
- // `cat ~/.ssh/id_ed25519` still win when matched (they run before
658
- // this check).
659
- //
660
- // Code Reviewer P0 retro: previously these reads fell
661
- // through to READ_TOKENS and were allowed in every mode.
662
- const protectedRead = detectProtectedRead(trimmed);
663
- if (protectedRead) {
664
- return {
665
- class: 'write_protected',
666
- reason: protectedRead.reason,
667
- matched: protectedRead.matched,
668
- };
669
- }
670
- // 4a-bis. Parent-traversal in read arguments. The file-tools layer
671
- // refuses `..` segments via `resolveWorkspacePath`, but the bash
672
- // surface had no equivalent gate — the engine could emit
673
- // `cat ../README.md` or `ls ..` to enumerate / read outside the
674
- // workspace, sidestepping the path-security check that the `read`
675
- // and `glob` tools enforce.
676
- //
677
- // P0 fix (Codex audit): treat `..` as a path segment
678
- // (`../`, ` ..`, `..\n`) in any read-class command as a workspace
679
- // escape. We classify it as `write_protected` so the auto/dontAsk
680
- // modes refuse, mirroring the `Path escapes workspace` semantics
681
- // the file-tools layer already provides.
682
- const traversal = detectParentTraversalRead(trimmed);
683
- if (traversal) {
684
- return {
685
- class: 'write_protected',
686
- reason: traversal.reason,
687
- matched: traversal.matched,
688
- };
689
- }
690
- // 4b. .env writes are always protected, even inside the workspace
691
- // (CEO directive feedback_never_delete_untracked_env.md).
692
- const envWrite = detectEnvWrite(trimmed);
693
- if (envWrite) {
694
- return {
695
- class: 'write_protected',
696
- reason: envWrite.reason,
697
- matched: envWrite.matched,
698
- };
699
- }
700
- // 5. Write_workspace ops (mkdir / touch / cp / mv / git commit / etc).
701
- for (const prefix of WRITE_WORKSPACE_PREFIXES) {
702
- if (trimmed.startsWith(prefix)) {
703
- return {
704
- class: 'write_workspace',
705
- reason: `Workspace write op: ${prefix.trim()}`,
706
- matched: prefix.trim(),
707
- };
708
- }
709
- }
710
- // 5b. Shell redirection (`>`/`>>`) without a protected target →
711
- // workspace write. Pipes (`|`) are not redirections. The regex
712
- // allows optional whitespace around `>` (catches `>file`, `> file`,
713
- // `>>file`, `>> file`) and skips file-descriptor redirects
714
- // (`>&1`, `2>&1`, `>&2`).
715
- if (/(^|[^0-9&])>>?\s*[^&\s|;<>]/.test(trimmed) &&
716
- !trimmed.includes('/dev/null')) {
717
- return {
718
- class: 'write_workspace',
719
- reason: 'Shell redirection into a workspace target',
720
- matched: '>',
721
- };
722
- }
723
- // 6. Network commands.
724
- const network = detectNetwork(trimmed);
725
- if (network) {
726
- return {
727
- class: 'network',
728
- reason: network.reason,
729
- matched: network.matched,
730
- };
731
- }
732
- // 7. Build/test runners.
733
- for (const prefix of BUILD_TEST_PREFIXES) {
734
- if (trimmed === prefix || trimmed.startsWith(`${prefix} `)) {
735
- return {
736
- class: 'build_test',
737
- reason: `Build/test runner: ${prefix}`,
738
- matched: prefix,
739
- };
740
- }
741
- }
742
- // 7b. Bare `make` (no subcommand) is build-class.
743
- if (trimmed === 'make' || trimmed.startsWith('make ')) {
744
- return { class: 'build_test', reason: 'make runner', matched: 'make' };
745
- }
746
- // 7c. Operator-override safe tokens (P0 fix #37).
747
- // `PUGI_CLASSIFIER_EXTRA_SAFE=tool1,tool2,...` extends the BUILD_TEST
748
- // first-token list at runtime. This is a security-sensitive escape
749
- // hatch — operators can add their custom build tools without a
750
- // recompile. Destructive patterns ALREADY ran above (step 1) so this
751
- // cannot whitelist `rm`, `mkfs`, `git push --force`, etc. The match
752
- // is strict first-token equality — not substring — and the env var
753
- // is read fresh on every classify call so tests can mutate it.
754
- const extraSafe = readExtraSafeTokens();
755
- if (extraSafe.size > 0) {
756
- const firstTokenForExtraSafe = trimmed.split(/\s+/)[0] ?? '';
757
- if (extraSafe.has(firstTokenForExtraSafe)) {
758
- return {
759
- class: 'build_test',
760
- reason: `PUGI_CLASSIFIER_EXTRA_SAFE override: ${firstTokenForExtraSafe}`,
761
- matched: firstTokenForExtraSafe,
762
- };
763
- }
764
- }
765
- // 7c. Bare `cd <path>` (inside workspace — the cwd-escape detector
766
- // upgrades the class to write_protected when the target is
767
- // outside). Standalone `cd` (HOME) is escape, also handled by the
768
- // cwd-escape detector.
769
- if (/^cd(\s+\S+)?\s*$/.test(trimmed)) {
770
- return { class: 'read', reason: 'cd inside workspace', matched: 'cd' };
771
- }
772
- // 8. Read-only commands.
773
- const firstToken = trimmed.split(/\s+/)[0] ?? '';
774
- if (READ_TOKENS.has(firstToken)) {
775
- // `sed` and `awk` are allowed only without `>` (already gated by
776
- // step 5b above) — they fall through to here when they are pure
777
- // reads. We list them explicitly for clarity even though set
778
- // membership is the source of truth.
779
- return { class: 'read', reason: `Read-only command: ${firstToken}`, matched: firstToken };
780
- }
781
- for (const prefix of READ_PREFIXES) {
782
- if (trimmed === prefix.trim() || trimmed.startsWith(prefix)) {
783
- return {
784
- class: 'read',
785
- reason: `Read-only command: ${prefix.trim()}`,
786
- matched: prefix.trim(),
787
- };
788
- }
789
- }
790
- // sed/awk: read-only when no `>` redirect (the redirect branch above
791
- // catches the write case).
792
- if (firstToken === 'sed' || firstToken === 'awk') {
793
- return { class: 'read', reason: `Stream editor as read: ${firstToken}`, matched: firstToken };
794
- }
795
- // `find` without -delete / -exec is a read.
796
- if (firstToken === 'find') {
797
- return { class: 'read', reason: 'find (no -delete/-exec)', matched: 'find' };
798
- }
799
- // 9. Default: unknown.
800
- return {
801
- class: 'unknown',
802
- reason: `Unrecognized command: ${firstToken || trimmed}`,
803
- matched: firstToken || trimmed,
804
- };
805
- }
806
- function findDestructiveMatch(cmd) {
807
- const upper = cmd.toUpperCase();
808
- for (const { pattern, caseInsensitive, regex } of DESTRUCTIVE_PATTERNS) {
809
- if (regex) {
810
- // Word-boundary regex form (external-derived patterns). Match
811
- // against the trimmed component so `^` anchors to command head,
812
- // not surrounding whitespace from the compound split.
813
- if (regex.test(cmd.trim()))
814
- return pattern;
815
- continue;
816
- }
817
- if (caseInsensitive) {
818
- if (upper.includes(pattern))
819
- return pattern;
820
- }
821
- else if (cmd.includes(pattern)) {
822
- return pattern;
823
- }
824
- }
825
- return null;
826
- }
827
- function detectObfuscation(cmd) {
828
- // Raw `eval` with shell expansion. (`eval "$VAR"` is already in
829
- // DESTRUCTIVE_PATTERNS — this catches the more general case of
830
- // `eval $(...)`, `eval `...``, etc.)
831
- if (/(^|\s)eval\s+[`$"']/.test(cmd)) {
832
- return { reason: 'eval with shell expansion is treated as unknown', matched: 'eval' };
833
- }
834
- // `bash -c '...base64-decoded...'` — base64-decoded payloads are
835
- // a common obfuscation. We trigger on the substring `base64 -d`
836
- // anywhere in the command.
837
- if (/\bbase64\s+-d\b/.test(cmd) || /\bbase64\s+--decode\b/.test(cmd)) {
838
- return { reason: 'base64 decode pipeline is treated as unknown', matched: 'base64 -d' };
839
- }
840
- // Deep nested `$(...)` — more than 3 levels of nesting is treated
841
- // as obfuscation.
842
- if (nestingDepth(cmd, '$(', ')') > 3) {
843
- return { reason: 'deeply nested command substitution', matched: '$(...)' };
844
- }
845
- if (nestingDepth(cmd, '`', '`') > 3) {
846
- return { reason: 'deeply nested backtick substitution', matched: '`...`' };
847
- }
848
- // `curl ... | sh`, `wget ... | bash` — remote installer pipe.
849
- // We require the entire command (including pipes) to contain both
850
- // the network fetcher and the shell receiver.
851
- for (const trigger of OBFUSCATION_TRIGGERS) {
852
- if (cmd.includes(trigger.needle) && /\|\s*(?:sh|bash|zsh|fish)\b/.test(cmd)) {
853
- return {
854
- reason: `${trigger.reason}: ${trigger.needle} | <shell>`,
855
- matched: `${trigger.needle} | sh`,
856
- };
857
- }
858
- }
859
- return null;
860
- }
861
- function nestingDepth(cmd, open, close) {
862
- if (open === close) {
863
- // Backtick pair — count occurrences / 2 for matched pairs and
864
- // approximate "depth" as `pairs > 3` triggers.
865
- const count = (cmd.match(new RegExp(escapeRegex(open), 'g')) ?? []).length;
866
- return Math.floor(count / 2);
867
- }
868
- let depth = 0;
869
- let max = 0;
870
- for (let i = 0; i < cmd.length; i += 1) {
871
- if (cmd.startsWith(open, i)) {
872
- depth += 1;
873
- max = Math.max(max, depth);
874
- i += open.length - 1;
875
- }
876
- else if (cmd.startsWith(close, i) && depth > 0) {
877
- depth -= 1;
878
- }
879
- }
880
- return max;
881
- }
882
- function escapeRegex(s) {
883
- return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
884
- }
885
- /**
886
- * Operator-override safe tokens. Read from `PUGI_CLASSIFIER_EXTRA_SAFE`
887
- * (comma-separated). Allows operators to extend the BUILD_TEST first-
888
- * token list at runtime for site-specific tooling без recompile.
889
- *
890
- * Security note: destructive substring patterns run BEFORE this gate
891
- * (step 1 in classifyComponent), so this cannot whitelist `rm`, `mkfs`,
892
- * `git push --force`, etc. The env var only adds tools to the benign
893
- * build_test class. Invalid entries (empty strings, тokens containing
894
- * shell metas) are silently dropped to avoid surprising classifications.
895
- *
896
- * Read fresh on every call so per-test mutations work и so operators
897
- * can update without restarting the agent loop. The cost (one env var
898
- * read + Set construction per call) is negligible for the classifier's
899
- * call frequency.
900
- */
901
- function readExtraSafeTokens() {
902
- const raw = process.env.PUGI_CLASSIFIER_EXTRA_SAFE;
903
- if (!raw || raw.trim() === '')
904
- return new Set();
905
- const tokens = new Set();
906
- for (const candidate of raw.split(',')) {
907
- const trimmed = candidate.trim();
908
- if (trimmed === '')
909
- continue;
910
- // Reject anything containing shell metas or whitespace — only bare
911
- // tool names allowed. Defends against accidental
912
- // `PUGI_CLASSIFIER_EXTRA_SAFE='rm -rf /'` smuggling.
913
- if (/[\s;|&<>$`(){}\[\]'"\\]/.test(trimmed))
914
- continue;
915
- tokens.add(trimmed);
916
- }
917
- return tokens;
918
- }
919
- function detectProtectedWrite(cmd, ctx) {
920
- // Surface every write target this command produces so we can both
921
- // protected-path-check and outside-workspace-check them uniformly.
922
- // Captures `sort -o`, `uniq <in> <out>`, `sed -i` files, `awk '... > "file"'`,
923
- // and `>` / `>>` redirections without surrounding whitespace.
924
- const writeTargets = extractWriteTargets(cmd);
925
- // Strip heredoc bodies before substring scan. Heredoc payloads are
926
- // DATA (file contents the script writes), not commands the shell
927
- // executes — a `package.json` body containing `/usr/local/bin/...`
928
- // would FP as "Write into protected path: /usr/" under the broad
929
- // includes() scan below. The per-target check at the bottom of this
930
- // function still catches real `cat > /usr/file << EOF` attempts
931
- // because extractWriteTargets reads the redirection target, not the
932
- // heredoc body. CEO dogfood (#28 follow-up).
933
- const cmdForScan = stripHeredocBodies(cmd);
934
- for (const needle of PROTECTED_PATH_SUBSTRINGS) {
935
- if (!cmdForScan.includes(needle))
936
- continue;
937
- // Reading from a protected path is allowed at the classifier
938
- // layer (the permission engine still gates `read`); writing is
939
- // the trigger here. We say it is a write if any of: `>`, `>>`,
940
- // `tee`, `cp`, `mv`, `mkdir`, `touch`, `chmod`, `chown`,
941
- // `ln`, `rm`, `rsync`, `scp` appears in the same component, or
942
- // if any of the structured write targets (sort -o, uniq two-arg,
943
- // sed -i, awk-redirect) was extracted above.
944
- if (writeTargets.length > 0 ||
945
- /(^|\s)>>?\s*\S/.test(cmd) ||
946
- /\btee\b/.test(cmd) ||
947
- /\bcp\b/.test(cmd) ||
948
- /\bmv\b/.test(cmd) ||
949
- /\bmkdir\b/.test(cmd) ||
950
- /\btouch\b/.test(cmd) ||
951
- /\bchmod\b/.test(cmd) ||
952
- /\bchown\b/.test(cmd) ||
953
- /\bln\b/.test(cmd) ||
954
- /\brm\b/.test(cmd) ||
955
- /\brsync\b/.test(cmd) ||
956
- /\bscp\b/.test(cmd)) {
957
- return {
958
- reason: `Write into protected path: ${needle}`,
959
- matched: needle,
960
- };
961
- }
962
- }
963
- // Per-target protected-path / outside-workspace check. Catches both
964
- // `sort -o ~/.ssh/config` and `echo x > /tmp/other` even when the
965
- // target was not a substring of the raw command (e.g. quoted paths).
966
- for (const target of writeTargets) {
967
- for (const needle of PROTECTED_PATH_SUBSTRINGS) {
968
- if (target.includes(needle)) {
969
- return {
970
- reason: `Write into protected path: ${needle}`,
971
- matched: target,
972
- };
973
- }
974
- }
975
- if (looksAbsoluteOutsideWorkspace(target, ctx)) {
976
- return {
977
- reason: `Write target outside workspace: ${target}`,
978
- matched: target,
979
- };
980
- }
981
- }
982
- return null;
983
- }
984
- /**
985
- * Extract every write-target path the command produces. Covers:
986
- * - shell redirection `> file`, `>> file` (with optional whitespace,
987
- * skipping `>&1`, `>&2`, etc.)
988
- * - `sort -o file`
989
- * - `uniq <input> <output>` (the two-arg form)
990
- * - `sed -i <file>...` (in-place edit treats every trailing file as a
991
- * write target)
992
- * - `awk '... > "file"'` (quoted redirection inside an awk script)
993
- *
994
- * Conservative — we do not try to resolve shell vars or globs; the
995
- * caller still gates absolute paths via `looksAbsoluteOutsideWorkspace`.
996
- */
997
- /**
998
- * Strip heredoc bodies so substring scans (e.g. `cmd.includes('/usr/')`)
999
- * do not false-positive on file content the script is *writing*. A
1000
- * heredoc starts с `<< 'WORD'` (or `<< WORD` / `<<-WORD`) and ends на a
1001
- * line containing only WORD. The body between is DATA, not commands.
1002
- *
1003
- * Best-effort: handles single-heredoc-per-command (the common case)
1004
- * AND multiple sequential heredocs. Nested heredocs (heredoc-inside-
1005
- * heredoc) are rare and out of scope — the substring scan still gates
1006
- * the outer command, just без stripping the nested body. Per-target
1007
- * detection at detectProtectedWrite's tail loop catches real
1008
- * `cat > /usr/file << EOF` attacks regardless of body content.
1009
- *
1010
- * CEO dogfood : `cat > package.json << 'EOF'\n{"bin":
1011
- * "/usr/local/bin/foo"}\nEOF` was rejected as "Write into protected
1012
- * path: /usr/" because the broad substring scan saw `/usr/` in the
1013
- * JSON body. With heredoc-body stripping, the scan now sees only
1014
- * `cat > package.json << 'EOF' EOF` which contains no protected path.
1015
- */
1016
- function stripHeredocBodies(cmd) {
1017
- // Match `<< [-]'WORD'` or `<< [-]"WORD"` or `<< [-]WORD` (quoted form
1018
- // disables variable expansion in real bash; we treat all three the
1019
- // same for stripping). Capture the WORD so we can find the close
1020
- // marker.
1021
- const heredocStart = /<<-?\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1/g;
1022
- let out = cmd;
1023
- let safetyLoops = 0;
1024
- let match;
1025
- while ((match = heredocStart.exec(out)) !== null) {
1026
- if (++safetyLoops > 16)
1027
- break;
1028
- const word = match[2];
1029
- if (!word)
1030
- continue;
1031
- const headEnd = match.index + match[0].length;
1032
- // Find the close-marker line: `\n<optional indent>WORD<\n|$>`.
1033
- const closeRegex = new RegExp(`\\n\\s*${word}(?:\\n|$)`);
1034
- closeRegex.lastIndex = headEnd;
1035
- const closeMatch = closeRegex.exec(out.slice(headEnd));
1036
- if (!closeMatch)
1037
- break;
1038
- const closeStart = headEnd + closeMatch.index;
1039
- const closeEnd = closeStart + closeMatch[0].length;
1040
- // Replace heredoc body + close marker с single space so the regex
1041
- // iterator's lastIndex stays meaningful.
1042
- out = out.slice(0, headEnd) + ' ' + out.slice(closeEnd);
1043
- heredocStart.lastIndex = headEnd + 1;
1044
- }
1045
- return out;
1046
- }
1047
- function extractWriteTargets(cmd) {
1048
- const targets = [];
1049
- // Shell redirection (`>`, `>>`) with optional whitespace. Skip
1050
- // file-descriptor redirects (`>&1`, `>&2`, `2>&1`).
1051
- const redirRegex = /(^|[^0-9&])>>?\s*([^\s|;&<>]+)/g;
1052
- let match;
1053
- while ((match = redirRegex.exec(cmd)) !== null) {
1054
- const candidate = match[2];
1055
- if (!candidate || candidate.startsWith('&'))
1056
- continue;
1057
- if (candidate === '/dev/null')
1058
- continue;
1059
- targets.push(stripQuotes(candidate));
1060
- }
1061
- // sort -o <file>
1062
- const sortMatch = cmd.match(/\bsort\b[^|;]*\s-o\s+(\S+)/);
1063
- if (sortMatch && sortMatch[1]) {
1064
- targets.push(stripQuotes(sortMatch[1]));
1065
- }
1066
- // uniq <input> <output> (two-arg form). Three-arg uniq does not exist
1067
- // in POSIX, so the second non-flag arg is the output file.
1068
- const uniqMatch = cmd.match(/\buniq\b(?:\s+-[^\s]+)*\s+(\S+)\s+(\S+)/);
1069
- if (uniqMatch && uniqMatch[2] && !uniqMatch[2].startsWith('-')) {
1070
- targets.push(stripQuotes(uniqMatch[2]));
1071
- }
1072
- // sed -i [SUFFIX] <expr> <file>... — every trailing positional is a
1073
- // write target. We capture the tail after `-i` and treat each
1074
- // whitespace-delimited token that is not a flag as a target.
1075
- const sedMatch = cmd.match(/\bsed\b[^|;]*\s-i\b([^|;]*)/);
1076
- if (sedMatch && sedMatch[1]) {
1077
- const tail = sedMatch[1].trim().split(/\s+/);
1078
- for (const token of tail) {
1079
- if (token === '' || token.startsWith('-'))
1080
- continue;
1081
- if (token.startsWith("'") || token.startsWith('"'))
1082
- continue;
1083
- targets.push(stripQuotes(token));
1084
- }
1085
- }
1086
- // awk '... > "file"' or awk "... > \"file\""
1087
- const awkQuoteRegex = /\bawk\b[^|;]*?['"][^'"]*?>\s*['"]([^'"]+)['"]/g;
1088
- while ((match = awkQuoteRegex.exec(cmd)) !== null) {
1089
- if (match[1])
1090
- targets.push(stripQuotes(match[1]));
1091
- }
1092
- return targets;
1093
- }
1094
- function stripQuotes(s) {
1095
- return s.replace(/^["']|["']$/g, '');
1096
- }
1097
- /**
1098
- * Detect READ operations targeted at protected paths. Runs after
1099
- * `detectProtectedWrite` and before the READ_TOKENS fallback. The leading
1100
- * token must be in READ_TOKENS so that we do not double-flag writes
1101
- * (which `detectProtectedWrite` already covers).
1102
- *
1103
- * Re-uses the `write_protected` class to keep the permission matrix
1104
- * simple — the matrix already gates that class as deny in plan/dontAsk
1105
- * and ask elsewhere, which is the intended fail-closed posture for
1106
- * unrestricted credential reads.
1107
- */
1108
- const READ_PREFIX_TOKENS = new Set(['grep', 'rg', 'less', 'more', 'fd', 'tree']);
1109
- function detectProtectedRead(cmd) {
1110
- const firstToken = cmd.split(/\s+/)[0] ?? '';
1111
- const isReadTool = READ_TOKENS.has(firstToken) ||
1112
- READ_PREFIX_TOKENS.has(firstToken) ||
1113
- firstToken === 'sed' ||
1114
- firstToken === 'awk' ||
1115
- firstToken === 'find';
1116
- if (!isReadTool)
1117
- return null;
1118
- // Strip heredoc bodies so `cat > config << 'EOF'\n... /etc/... \nEOF`
1119
- // does not FP as "Read from protected path" when first-token=`cat` +
1120
- // redirection writes к workspace-local file. Heredoc payload is data.
1121
- // CEO dogfood .
1122
- const cmdForScan = stripHeredocBodies(cmd);
1123
- for (const needle of PROTECTED_PATH_SUBSTRINGS) {
1124
- if (cmdForScan.includes(needle)) {
1125
- return {
1126
- reason: `Read from protected path: ${needle}`,
1127
- matched: needle,
1128
- };
1129
- }
1130
- }
1131
- // P0 fix: extend protected-read detection to credential
1132
- // basenames (`cat .env`, `head id_rsa`, `grep TOKEN .env.production`).
1133
- // Without this branch, the engine model can bypass the `read` tool's
1134
- // `protectedTargetReason` gate by emitting a bash `cat` — the read
1135
- // tool refuses, the model falls back to bash, and the classifier
1136
- // (which only knew about full-path substrings) classified `cat .env`
1137
- // as benign `read`. The `local-first-invariants` spec proved the leak.
1138
- for (const pattern of PROTECTED_BASENAME_PATTERNS) {
1139
- const match = cmd.match(pattern);
1140
- if (match) {
1141
- return {
1142
- reason: `Read from protected basename: ${match[0].trim()}`,
1143
- matched: match[0].trim(),
1144
- };
1145
- }
1146
- }
1147
- return null;
1148
- }
1149
- /**
1150
- * Detect parent-traversal segments (`..`) inside read-class commands.
1151
- * The file-tools layer (`resolveWorkspacePath`) refuses these for the
1152
- * `read`/`glob`/`grep` tools, but bash had no equivalent gate. We
1153
- * trigger on the SAME shape `path-security.ts` rejects: a `..` segment
1154
- * separated by `/` or whitespace. Quoted/escaped variants get the same
1155
- * treatment.
1156
- *
1157
- * Returns null on the safe path (no `..` segment) so the caller falls
1158
- * through to the regular read classification.
1159
- */
1160
- function detectParentTraversalRead(cmd) {
1161
- const firstToken = cmd.split(/\s+/)[0] ?? '';
1162
- const isReadTool = READ_TOKENS.has(firstToken) ||
1163
- READ_PREFIX_TOKENS.has(firstToken) ||
1164
- firstToken === 'sed' ||
1165
- firstToken === 'awk' ||
1166
- firstToken === 'find';
1167
- if (!isReadTool)
1168
- return null;
1169
- // Match `..` as a path segment: preceded by start/whitespace/quote/`/`
1170
- // and followed by `/`, end-of-string, whitespace, or shell metas.
1171
- // Avoids over-matching `v1..v2` (range syntax inside a single token)
1172
- // and `1..5` (numeric ranges) because those lack the path boundary.
1173
- const traversalPattern = /(^|[\s'"\/])\.\.(\/|$|[\s'"<>|;&])/;
1174
- const m = cmd.match(traversalPattern);
1175
- if (m) {
1176
- return {
1177
- reason: 'Read command escapes workspace via parent traversal',
1178
- matched: '..',
1179
- };
1180
- }
1181
- // Absolute path read of /etc, /usr, /var, etc is already covered by
1182
- // PROTECTED_PATH_SUBSTRINGS in detectProtectedRead — no extra branch
1183
- // needed here.
1184
- return null;
1185
- }
1186
- function detectEnvWrite(cmd) {
1187
- // .env / .env.<suffix> writes — match `\.env\b` adjacent to a write
1188
- // op (redirection, tee, cp, mv into) or `rm`. Reading .env in shell
1189
- // (`cat .env`) is gated by the permission engine, not classified
1190
- // here.
1191
- const envHit = /(^|\s|\/)\.env(\.[a-zA-Z0-9_-]+)?(\s|$|[>])/m.test(cmd);
1192
- if (!envHit)
1193
- return null;
1194
- if (/>>?\s/.test(cmd) ||
1195
- /\btee\b/.test(cmd) ||
1196
- /\bcp\b.*\.env/.test(cmd) ||
1197
- /\bmv\b.*\.env/.test(cmd) ||
1198
- /\brm\b.*\.env/.test(cmd)) {
1199
- return { reason: 'Write touches .env file', matched: '.env' };
1200
- }
1201
- return null;
1202
- }
1203
- function looksAbsoluteOutsideWorkspace(target, ctx) {
1204
- // Strip surrounding quotes and shell expansion artifacts.
1205
- const cleaned = target.replace(/^["']|["']$/g, '');
1206
- if (cleaned.startsWith('~')) {
1207
- // Home-relative path; treat as outside unless it explicitly
1208
- // re-enters an allowed dir (we are conservative — `~/foo` is
1209
- // outside).
1210
- return true;
1211
- }
1212
- if (!cleaned.startsWith('/')) {
1213
- // Workspace-relative path (or shell var); allowed at this layer.
1214
- // The path-security layer in `path-security.ts` already gates
1215
- // traversal escapes for the file tools; the bash classifier
1216
- // cannot resolve shell vars so we trust them.
1217
- return false;
1218
- }
1219
- const allowedRoots = [ctx.workspaceRoot, ...ctx.additionalDirectories].map((p) => p.endsWith('/') ? p : `${p}/`);
1220
- const cleanedWithSlash = cleaned.endsWith('/') ? cleaned : `${cleaned}/`;
1221
- return !allowedRoots.some((root) => cleanedWithSlash === root || cleanedWithSlash.startsWith(root));
1222
- }
1223
- function detectNetwork(cmd) {
1224
- for (const prefix of NETWORK_PREFIXES) {
1225
- if (cmd.startsWith(prefix) || cmd.includes(` ${prefix}`)) {
1226
- return { reason: `Network operation: ${prefix.trim()}`, matched: prefix.trim() };
1227
- }
1228
- }
1229
- const firstToken = cmd.split(/\s+/)[0] ?? '';
1230
- if (NETWORK_TOKENS.has(firstToken)) {
1231
- return { reason: `Network tool: ${firstToken}`, matched: firstToken };
1232
- }
1233
- return null;
1234
- }
1235
- /**
1236
- * Normalize a POSIX-ish path without resolving symlinks. Used by the
1237
- * `cd ... && rest` boundary check so we can decide whether the
1238
- * destination is inside `workspaceRoot ∪ additionalDirectories`
1239
- * before we ever spawn /bin/sh.
1240
- */
1241
- function normalizePosix(input, baseDir) {
1242
- const cleaned = input.replace(/^["']|["']$/g, '');
1243
- const expanded = cleaned.startsWith('~') ? cleaned : cleaned;
1244
- // ~ expansion is intentionally not done (it would force HOME read);
1245
- // a `cd ~/...` is treated as outside-workspace by default since the
1246
- // home directory is generally outside the workspace.
1247
- const isAbsolute = expanded.startsWith('/') || expanded.startsWith('~');
1248
- const start = isAbsolute ? expanded : `${baseDir.replace(/\/$/, '')}/${expanded}`;
1249
- const parts = start.split('/');
1250
- const stack = [];
1251
- for (const part of parts) {
1252
- if (part === '' || part === '.')
1253
- continue;
1254
- if (part === '..') {
1255
- if (stack.length > 0)
1256
- stack.pop();
1257
- continue;
1258
- }
1259
- stack.push(part);
1260
- }
1261
- return `/${stack.join('/')}`;
1262
- }
1263
- function isInsideAllowedRoot(absPath, ctx) {
1264
- const allowedRoots = [ctx.workspaceRoot, ...ctx.additionalDirectories];
1265
- for (const rootRaw of allowedRoots) {
1266
- const root = rootRaw.endsWith('/') ? rootRaw.slice(0, -1) : rootRaw;
1267
- if (absPath === root || absPath.startsWith(`${root}/`))
1268
- return true;
1269
- }
1270
- return false;
1271
- }
1272
- /**
1273
- * Detect `cd <path>` at the head of a component and decide whether
1274
- * the destination escapes the workspace boundary. Returns a
1275
- * classification when an escape is detected; otherwise null.
1276
- *
1277
- * Per spec: a command of shape `cd <path> && <rest>` should classify
1278
- * the cwd target — if `<path>` resolves outside the workspace, the
1279
- * overall class becomes `write_protected` regardless of `<rest>`.
1280
- *
1281
- * We treat `cd -` (last-dir) and `cd` (HOME) as escapes since the
1282
- * resulting cwd is not under our control.
1283
- */
1284
- function detectCwdEscape(components, ctx) {
1285
- if (components.length === 0)
1286
- return null;
1287
- // Walk every component, threading a simulated cwd through the chain.
1288
- // Pure `cd <path>` components update the simulated cwd; anything
1289
- // else leaves it untouched. If any hop lands outside
1290
- // `workspaceRoot ∪ additionalDirectories`, the overall classification
1291
- // upgrades to write_protected. Subshells (`(cd foo && rest)`) are
1292
- // out of scope for M1 — the parent cwd is unaffected there, and the
1293
- // classifier already treats parentheses as part of the component.
1294
- let cwd = ctx.workspaceRoot;
1295
- for (let i = 0; i < components.length; i += 1) {
1296
- const trimmed = components[i]?.trim() ?? '';
1297
- const cdMatch = trimmed.match(/^cd(?:\s+(\S+))?\s*$/);
1298
- if (!cdMatch)
1299
- continue;
1300
- const target = cdMatch[1];
1301
- if (target === undefined || target === '-' || target === '~') {
1302
- return {
1303
- class: 'write_protected',
1304
- reason: `cd chain escapes workspace boundary at hop ${i + 1}`,
1305
- matched: `cd${target ? ` ${target}` : ''}`,
1306
- };
1307
- }
1308
- cwd = normalizePosix(target, cwd);
1309
- if (!isInsideAllowedRoot(cwd, ctx)) {
1310
- return {
1311
- class: 'write_protected',
1312
- reason: `cd chain escapes workspace boundary at hop ${i + 1}`,
1313
- matched: `cd ${target}`,
1314
- };
1315
- }
1316
- }
1317
- return null;
1318
- }
1319
- export function classifyBash(cmd, ctx) {
1320
- const normalized = cmd.trim();
1321
- if (normalized === '') {
1322
- return { class: 'unknown', reason: 'empty command', matched: '' };
1323
- }
1324
- // Full-command destructive check first. Patterns like the fork bomb
1325
- // (`:(){ :|:& };:`) and SQL-in-pipe (`echo X | mysql -e 'DROP ...'`)
1326
- // would otherwise split on `|` or `;` and the components would each
1327
- // look benign. We catch them by matching the destructive substrings
1328
- // against the raw command before splitting.
1329
- const fullDestructive = findDestructiveMatch(normalized);
1330
- if (fullDestructive) {
1331
- return {
1332
- class: 'destructive',
1333
- reason: `Destructive command pattern matched: ${fullDestructive}`,
1334
- matched: fullDestructive,
1335
- };
1336
- }
1337
- // Full-command obfuscation check. `curl ... | sh` splits into two
1338
- // benign-looking components (`curl ...` is network; `sh` is unknown)
1339
- // but the pipeline together is the remote-installer pattern we want
1340
- // to flag as unknown so the engine can fail closed.
1341
- const fullObfuscation = detectObfuscation(normalized);
1342
- if (fullObfuscation) {
1343
- return {
1344
- class: 'unknown',
1345
- reason: fullObfuscation.reason,
1346
- matched: fullObfuscation.matched,
1347
- };
1348
- }
1349
- // Compound-command split. We classify each component, then pick
1350
- // the most dangerous one as the overall class.
1351
- const components = splitCompoundRespectingQuotes(normalized);
1352
- // Cwd-escape check runs over the raw component list so the `cd`
1353
- // verdict trumps the `<rest>` classification even when `<rest>` is
1354
- // a benign `ls`.
1355
- const cwdEscape = detectCwdEscape(components, ctx);
1356
- const classified = components.map((c) => classifyComponent(c, ctx));
1357
- // Pick the worst component.
1358
- let worst;
1359
- for (const candidate of classified) {
1360
- if (!worst || CLASS_RANK[candidate.class] > CLASS_RANK[worst.class]) {
1361
- worst = candidate;
1362
- }
1363
- }
1364
- if (!worst) {
1365
- return {
1366
- class: 'unknown',
1367
- reason: 'no recognizable component',
1368
- matched: normalized,
1369
- };
1370
- }
1371
- // Cwd escape upgrades the class to at least `write_protected`. A
1372
- // destructive component still wins (the user might be trying to
1373
- // wipe a protected path AND escape cwd — we report the worse one).
1374
- if (cwdEscape && CLASS_RANK[cwdEscape.class] > CLASS_RANK[worst.class]) {
1375
- return {
1376
- class: cwdEscape.class,
1377
- reason: cwdEscape.reason,
1378
- matched: cwdEscape.matched,
1379
- components: classified,
1380
- };
1381
- }
1382
- if (classified.length === 1) {
1383
- return worst;
1384
- }
1385
- return { ...worst, components: classified };
1386
- }
1387
- /**
1388
- * Re-exported destructive-pattern source. The permission engine used
1389
- * to maintain its own `destructiveBashPatterns` array; that array
1390
- * now lives here as the single source of truth. Callers that need to
1391
- * audit the list (e.g. doctor output) read this export instead of
1392
- * duplicating the regex set.
1393
- */
1394
- export function listDestructivePatterns() {
1395
- return DESTRUCTIVE_PATTERNS.map((p) => p.pattern);
1396
- }
1397
- //# sourceMappingURL=bash-classifier.js.map