@oyasmi/pipiclaw 0.9.5 → 0.9.6-beta.2

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 (345) hide show
  1. package/README.en.md +300 -142
  2. package/README.md +295 -140
  3. package/dist/agent/builtin-commands.d.ts +1 -1
  4. package/dist/agent/builtin-commands.js +1 -1
  5. package/dist/agent/channel-runner.d.ts +4 -3
  6. package/dist/agent/channel-runner.js +17 -28
  7. package/dist/agent/maintenance-context.js +2 -2
  8. package/dist/agent/prompt/builder.d.ts +1 -1
  9. package/dist/agent/prompt/builder.js +1 -1
  10. package/dist/agent/prompt/extension.d.ts +1 -1
  11. package/dist/agent/prompt/extension.js +1 -1
  12. package/dist/agent/prompt/manifest.d.ts +6 -1
  13. package/dist/agent/prompt/manifest.js +12 -5
  14. package/dist/agent/prompt/resources.d.ts +0 -3
  15. package/dist/agent/prompt/resources.js +0 -6
  16. package/dist/agent/prompt/sections.d.ts +2 -2
  17. package/dist/agent/prompt/sections.js +36 -26
  18. package/dist/agent/prompt/types.d.ts +3 -2
  19. package/dist/agent/runner-factory.js +1 -1
  20. package/dist/agent/session/command-extension.js +3 -1
  21. package/dist/agent/session/compaction-summary.js +3 -6
  22. package/dist/agent/session/compaction.d.ts +2 -0
  23. package/dist/agent/session/compaction.js +22 -14
  24. package/dist/agent/session/event-guards.d.ts +2 -1
  25. package/dist/agent/session/event-guards.js +6 -0
  26. package/dist/agent/session/model-selection.d.ts +2 -1
  27. package/dist/agent/session/model-selection.js +5 -5
  28. package/dist/agent/session/session-events.js +31 -4
  29. package/dist/agent/session/session-host.d.ts +5 -3
  30. package/dist/agent/session/session-host.js +20 -16
  31. package/dist/agent/status-render.js +3 -0
  32. package/dist/agent/stop-watchdog.js +5 -1
  33. package/dist/agent/turn/task-digest.d.ts +2 -5
  34. package/dist/agent/turn/task-digest.js +25 -45
  35. package/dist/agent/turn/turn-context.js +4 -6
  36. package/dist/agent/turn/turn-error.d.ts +1 -1
  37. package/dist/agent/turn/turn-error.js +9 -0
  38. package/dist/agent/turn/turn-pipeline.d.ts +10 -5
  39. package/dist/agent/turn/turn-pipeline.js +36 -7
  40. package/dist/agent/turn/turn-prompt.d.ts +2 -0
  41. package/dist/agent/turn/turn-prompt.js +2 -1
  42. package/dist/agent/turn/turn-recovery.js +4 -1
  43. package/dist/agent/turn/turn-state.d.ts +2 -0
  44. package/dist/agent/turn/turn-state.js +2 -1
  45. package/dist/agent/types.d.ts +23 -3
  46. package/dist/agent/types.js +2 -0
  47. package/dist/app/auth-command.d.ts +25 -0
  48. package/dist/app/auth-command.js +200 -67
  49. package/dist/app/home.d.ts +7 -8
  50. package/dist/app/home.js +143 -56
  51. package/dist/channel/attachments.d.ts +4 -0
  52. package/dist/channel/attachments.js +4 -0
  53. package/dist/channel/channel-context.d.ts +6 -0
  54. package/dist/channel/config.d.ts +3 -0
  55. package/dist/channel/config.js +11 -5
  56. package/dist/channel/store.d.ts +1 -1
  57. package/dist/commands/catalog.js +42 -10
  58. package/dist/commands/router.js +5 -1
  59. package/dist/desk/commands.d.ts +44 -5
  60. package/dist/desk/commands.js +162 -28
  61. package/dist/desk/paths.d.ts +6 -0
  62. package/dist/desk/paths.js +21 -1
  63. package/dist/desk/registry.d.ts +17 -2
  64. package/dist/desk/registry.js +122 -12
  65. package/dist/desk/types.d.ts +83 -1
  66. package/dist/dingtalk/bot.d.ts +1 -1
  67. package/dist/dingtalk/bot.js +4 -3
  68. package/dist/dingtalk/config.js +10 -10
  69. package/dist/dingtalk/delivery.js +15 -8
  70. package/dist/dingtalk/inbound-media.d.ts +36 -14
  71. package/dist/dingtalk/inbound-media.js +71 -12
  72. package/dist/dingtalk/inbound.d.ts +9 -2
  73. package/dist/dingtalk/inbound.js +17 -2
  74. package/dist/dingtalk/media.d.ts +4 -10
  75. package/dist/dingtalk/media.js +58 -32
  76. package/dist/events/commands.d.ts +2 -0
  77. package/dist/events/commands.js +13 -8
  78. package/dist/events/history.d.ts +9 -0
  79. package/dist/events/history.js +66 -0
  80. package/dist/events/parse.js +7 -3
  81. package/dist/events/types.d.ts +3 -0
  82. package/dist/events/watcher.d.ts +4 -8
  83. package/dist/events/watcher.js +53 -58
  84. package/dist/jobs/kill.d.ts +23 -0
  85. package/dist/jobs/kill.js +65 -0
  86. package/dist/jobs/manager.d.ts +19 -18
  87. package/dist/jobs/manager.js +36 -103
  88. package/dist/main.js +15 -3
  89. package/dist/memory/boundary-work.d.ts +2 -1
  90. package/dist/memory/boundary-work.js +1 -1
  91. package/dist/memory/lifecycle.d.ts +2 -1
  92. package/dist/memory/lifecycle.js +2 -2
  93. package/dist/memory/reflect-job.d.ts +2 -1
  94. package/dist/memory/reflect-job.js +1 -1
  95. package/dist/memory/reflect.d.ts +2 -1
  96. package/dist/memory/reflect.js +25 -4
  97. package/dist/memory/scheduler.d.ts +2 -1
  98. package/dist/memory/scheduler.js +1 -1
  99. package/dist/memory/session-corpus.d.ts +1 -1
  100. package/dist/memory/session-corpus.js +76 -1
  101. package/dist/memory/session-search.d.ts +2 -1
  102. package/dist/memory/session-search.js +14 -12
  103. package/dist/memory/sidecar-worker.d.ts +2 -1
  104. package/dist/memory/sidecar-worker.js +2 -4
  105. package/dist/memory/store.d.ts +2 -0
  106. package/dist/memory/store.js +19 -9
  107. package/dist/models/credential-work.d.ts +7 -0
  108. package/dist/models/credential-work.js +36 -0
  109. package/dist/models/login-ui.js +4 -4
  110. package/dist/models/provider-login.js +7 -6
  111. package/dist/models/requests.d.ts +8 -0
  112. package/dist/models/requests.js +69 -0
  113. package/dist/models/runtime.d.ts +3 -1
  114. package/dist/models/runtime.js +9 -2
  115. package/dist/playbooks/agent-delegation.md +20 -45
  116. package/dist/playbooks/background-jobs.md +1 -1
  117. package/dist/playbooks/catalog.js +1 -1
  118. package/dist/playbooks/delegation-inline.md +25 -0
  119. package/dist/playbooks/delegation-recovery.md +28 -0
  120. package/dist/playbooks/event-scheduling.md +18 -37
  121. package/dist/playbooks/event-sensors.md +26 -0
  122. package/dist/playbooks/event-task-template.md +22 -0
  123. package/dist/playbooks/learning-and-skills.md +26 -0
  124. package/dist/playbooks/memory-operations.md +36 -0
  125. package/dist/playbooks/outbound-media.md +2 -2
  126. package/dist/playbooks/runtime-files.md +29 -0
  127. package/dist/playbooks/runtime-operations.md +25 -0
  128. package/dist/playbooks/runtime-orientation.md +18 -53
  129. package/dist/playbooks/task-lead.md +22 -68
  130. package/dist/playbooks/task-manage.md +38 -0
  131. package/dist/playbooks/task-recovery.md +24 -0
  132. package/dist/playbooks/task-verify.md +22 -0
  133. package/dist/runtime/bootstrap.d.ts +10 -0
  134. package/dist/runtime/bootstrap.js +93 -29
  135. package/dist/runtime/daemon-lock.js +3 -3
  136. package/dist/runtime/desk-runtime.d.ts +40 -7
  137. package/dist/runtime/desk-runtime.js +330 -44
  138. package/dist/runtime/durable-dispatch.d.ts +14 -2
  139. package/dist/runtime/durable-dispatch.js +207 -65
  140. package/dist/runtime/ingress-busy.js +17 -8
  141. package/dist/runtime/ingress-commands.js +24 -4
  142. package/dist/runtime/ingress-deps.d.ts +4 -1
  143. package/dist/runtime/ingress.js +6 -1
  144. package/dist/runtime/request-execution.js +7 -6
  145. package/dist/runtime/shutdown.d.ts +4 -1
  146. package/dist/runtime/shutdown.js +88 -35
  147. package/dist/runtime/task-close.d.ts +1 -1
  148. package/dist/runtime/task-close.js +6 -3
  149. package/dist/runtime/task-control.d.ts +2 -3
  150. package/dist/runtime/task-control.js +38 -11
  151. package/dist/runtime/task-input.js +1 -1
  152. package/dist/runtime/task-lane-types.d.ts +12 -2
  153. package/dist/runtime/task-lane-types.js +9 -0
  154. package/dist/runtime/task-lane.d.ts +2 -0
  155. package/dist/runtime/task-lane.js +38 -20
  156. package/dist/runtime/task-step.d.ts +3 -3
  157. package/dist/runtime/task-step.js +34 -16
  158. package/dist/runtime/transports.js +6 -5
  159. package/dist/runtime/turn-run.js +103 -66
  160. package/dist/security/command-guard.d.ts +1 -1
  161. package/dist/security/command-guard.js +259 -314
  162. package/dist/security/path-guard.js +48 -7
  163. package/dist/security/shell-parse.d.ts +10 -0
  164. package/dist/security/shell-parse.js +224 -0
  165. package/dist/security/types.d.ts +2 -0
  166. package/dist/subagents/commands.d.ts +1 -1
  167. package/dist/subagents/commands.js +14 -5
  168. package/dist/subagents/completion-wake.js +6 -0
  169. package/dist/subagents/detached-discovery.d.ts +1 -1
  170. package/dist/subagents/discovery.d.ts +18 -199
  171. package/dist/subagents/discovery.js +35 -289
  172. package/dist/subagents/dispatch.d.ts +1 -2
  173. package/dist/subagents/dispatch.js +50 -29
  174. package/dist/subagents/external/antigravity-cli.d.ts +2 -0
  175. package/dist/subagents/external/antigravity-cli.js +517 -0
  176. package/dist/subagents/external/claude-code.js +8 -17
  177. package/dist/subagents/external/codex-cli.js +8 -18
  178. package/dist/subagents/external/cursor-cli.js +34 -31
  179. package/dist/subagents/external/dispatch.d.ts +21 -7
  180. package/dist/subagents/external/dispatch.js +146 -12
  181. package/dist/subagents/external/exec.js +3 -0
  182. package/dist/subagents/external/harness.d.ts +57 -33
  183. package/dist/subagents/external/harness.js +69 -40
  184. package/dist/subagents/external/reconciliation.js +1 -0
  185. package/dist/subagents/external/registry.js +4 -1
  186. package/dist/subagents/external/role-warnings.d.ts +4 -13
  187. package/dist/subagents/external/role-warnings.js +4 -28
  188. package/dist/subagents/external/run.d.ts +1 -2
  189. package/dist/subagents/external/run.js +16 -26
  190. package/dist/subagents/external/settlement.d.ts +2 -0
  191. package/dist/subagents/external/settlement.js +13 -3
  192. package/dist/subagents/format.d.ts +1 -1
  193. package/dist/subagents/format.js +1 -1
  194. package/dist/subagents/internal/outcome.d.ts +1 -2
  195. package/dist/subagents/internal/outcome.js +19 -12
  196. package/dist/subagents/internal/provider-retry.d.ts +1 -2
  197. package/dist/subagents/internal/provider-retry.js +2 -4
  198. package/dist/subagents/internal/run.d.ts +2 -3
  199. package/dist/subagents/internal/run.js +27 -4
  200. package/dist/subagents/internal/state.d.ts +2 -5
  201. package/dist/subagents/internal/state.js +1 -2
  202. package/dist/subagents/internal/worker.d.ts +1 -2
  203. package/dist/subagents/internal/worker.js +20 -10
  204. package/dist/subagents/resolve.d.ts +56 -0
  205. package/dist/subagents/resolve.js +202 -0
  206. package/dist/subagents/result.d.ts +6 -4
  207. package/dist/subagents/result.js +39 -6
  208. package/dist/subagents/run-record.d.ts +54 -2
  209. package/dist/subagents/run-record.js +7 -0
  210. package/dist/subagents/runs.d.ts +6 -31
  211. package/dist/subagents/runs.js +26 -31
  212. package/dist/subagents/task-envelope.d.ts +1 -2
  213. package/dist/subagents/task-envelope.js +13 -4
  214. package/dist/subagents/task-settlement.js +1 -0
  215. package/dist/subagents/types.d.ts +105 -6
  216. package/dist/subagents/types.js +16 -1
  217. package/dist/subagents/workspace-lease.d.ts +41 -24
  218. package/dist/subagents/workspace-lease.js +159 -24
  219. package/dist/subagents/worktree.d.ts +52 -0
  220. package/dist/subagents/worktree.js +363 -0
  221. package/dist/tasks/archive.js +4 -0
  222. package/dist/tasks/brief.d.ts +5 -1
  223. package/dist/tasks/brief.js +19 -5
  224. package/dist/tasks/commands.d.ts +6 -1
  225. package/dist/tasks/commands.js +32 -7
  226. package/dist/tasks/control.d.ts +11 -0
  227. package/dist/tasks/delivery.d.ts +2 -0
  228. package/dist/tasks/delivery.js +141 -21
  229. package/dist/tasks/loop-log.d.ts +12 -0
  230. package/dist/tasks/loop-log.js +10 -1
  231. package/dist/tasks/receipts.d.ts +7 -3
  232. package/dist/tasks/receipts.js +34 -5
  233. package/dist/tools/bash.d.ts +3 -0
  234. package/dist/tools/bash.js +29 -15
  235. package/dist/tools/contract/presentation.js +7 -5
  236. package/dist/tools/edit-diff.d.ts +11 -0
  237. package/dist/tools/edit-diff.js +148 -0
  238. package/dist/tools/edit-match.d.ts +18 -0
  239. package/dist/tools/edit-match.js +242 -0
  240. package/dist/tools/edit.d.ts +10 -2
  241. package/dist/tools/edit.js +91 -361
  242. package/dist/tools/event-manage.d.ts +2 -1
  243. package/dist/tools/event-manage.js +46 -39
  244. package/dist/tools/file-observations.d.ts +9 -0
  245. package/dist/tools/file-observations.js +50 -0
  246. package/dist/tools/glob-match.d.ts +3 -1
  247. package/dist/tools/glob-match.js +40 -17
  248. package/dist/tools/glob.d.ts +1 -0
  249. package/dist/tools/glob.js +39 -19
  250. package/dist/tools/grep-command.d.ts +13 -0
  251. package/dist/tools/grep-command.js +77 -0
  252. package/dist/tools/grep.js +96 -201
  253. package/dist/tools/index.d.ts +6 -2
  254. package/dist/tools/index.js +9 -4
  255. package/dist/tools/job.js +12 -11
  256. package/dist/tools/read.d.ts +2 -0
  257. package/dist/tools/read.js +10 -2
  258. package/dist/tools/registry.d.ts +16 -11
  259. package/dist/tools/registry.js +32 -12
  260. package/dist/tools/send-media.js +9 -5
  261. package/dist/tools/session-search.d.ts +2 -1
  262. package/dist/tools/session-search.js +15 -5
  263. package/dist/tools/skill.js +13 -12
  264. package/dist/tools/subagent-manage.d.ts +1 -1
  265. package/dist/tools/subagent-manage.js +25 -152
  266. package/dist/tools/subagent.d.ts +24 -2
  267. package/dist/tools/subagent.js +24 -18
  268. package/dist/tools/task-manage/control-tools.js +1 -1
  269. package/dist/tools/task-manage/factory.d.ts +2 -2
  270. package/dist/tools/task-manage/factory.js +40 -4
  271. package/dist/tools/task-manage/lifecycle.d.ts +3 -3
  272. package/dist/tools/task-manage/lifecycle.js +56 -13
  273. package/dist/tools/task-manage/schema.d.ts +4 -1
  274. package/dist/tools/task-manage/schema.js +4 -1
  275. package/dist/tools/task-manage/shared.d.ts +1 -1
  276. package/dist/tools/task-manage/types.d.ts +5 -1
  277. package/dist/tools/write-content.d.ts +7 -1
  278. package/dist/tools/write-content.js +18 -6
  279. package/dist/tools/write.d.ts +3 -0
  280. package/dist/tools/write.js +6 -2
  281. package/dist/tui/cli.d.ts +6 -6
  282. package/dist/tui/cli.js +120 -28
  283. package/dist/tui/client.d.ts +7 -0
  284. package/dist/tui/client.js +67 -16
  285. package/dist/tui/frontend.d.ts +2 -0
  286. package/dist/tui/pitui-frontend.d.ts +4 -0
  287. package/dist/tui/pitui-frontend.js +27 -0
  288. package/dist/tui/render.d.ts +1 -0
  289. package/dist/tui/render.js +5 -0
  290. package/dist/tui/session.js +6 -0
  291. package/dist/usage/ledger.d.ts +1 -1
  292. package/dist/usage/render.js +1 -1
  293. package/dist/web/client.d.ts +1 -0
  294. package/dist/web/client.js +11 -0
  295. package/dist/web/fetch.js +6 -3
  296. package/dist/webapi/auth.d.ts +0 -3
  297. package/dist/webapi/auth.js +3 -7
  298. package/dist/webapi/config.js +17 -14
  299. package/dist/webapi/key-command.js +41 -41
  300. package/dist/webapi/response-state.d.ts +7 -1
  301. package/dist/webapi/response-state.js +166 -3
  302. package/dist/webapi/routes/websocket.js +24 -1
  303. package/dist/webapi/server.d.ts +3 -1
  304. package/dist/webapi/server.js +32 -1
  305. package/dist/webapi/service.js +8 -0
  306. package/dist/webapi/transport.d.ts +2 -0
  307. package/dist/webapi/transport.js +11 -1
  308. package/examples/sub-agents/README.md +43 -75
  309. package/examples/sub-agents/agy-main.md +21 -0
  310. package/examples/sub-agents/claude-high.md +21 -0
  311. package/examples/sub-agents/claude-main.md +21 -0
  312. package/examples/sub-agents/codex-flash.md +21 -0
  313. package/examples/sub-agents/codex-high.md +21 -0
  314. package/examples/sub-agents/codex-main.md +21 -0
  315. package/examples/sub-agents/codex-review.md +20 -0
  316. package/examples/sub-agents/cursor-high.md +20 -0
  317. package/examples/sub-agents/cursor-main.md +20 -0
  318. package/examples/sub-agents/glm-flash.md +21 -0
  319. package/examples/sub-agents/glm-high.md +21 -0
  320. package/examples/sub-agents/pi-scout.md +20 -0
  321. package/package.json +11 -12
  322. package/dist/agent/session/request-tools.d.ts +0 -3
  323. package/dist/agent/session/request-tools.js +0 -20
  324. package/dist/models/api-keys.d.ts +0 -11
  325. package/dist/models/api-keys.js +0 -22
  326. package/dist/playbooks/memory-and-learning.md +0 -55
  327. package/examples/sub-agents/agents/claude-high.md +0 -23
  328. package/examples/sub-agents/agents/claude-main.md +0 -23
  329. package/examples/sub-agents/agents/codex-flash.md +0 -23
  330. package/examples/sub-agents/agents/codex-high.md +0 -23
  331. package/examples/sub-agents/agents/codex-main.md +0 -23
  332. package/examples/sub-agents/agents/codex-review.md +0 -23
  333. package/examples/sub-agents/agents/cursor-high.md +0 -22
  334. package/examples/sub-agents/agents/cursor-main.md +0 -22
  335. package/examples/sub-agents/agents/glm-flash.md +0 -23
  336. package/examples/sub-agents/agents/glm-high.md +0 -23
  337. package/examples/sub-agents/agents/scout.md +0 -30
  338. package/examples/sub-agents/roles/builder-hard.md +0 -34
  339. package/examples/sub-agents/roles/builder.md +0 -34
  340. package/examples/sub-agents/roles/explorer.md +0 -39
  341. package/examples/sub-agents/roles/git-committer.md +0 -43
  342. package/examples/sub-agents/roles/planner.md +0 -33
  343. package/examples/sub-agents/roles/reviewer.md +0 -33
  344. package/examples/sub-agents/roles/verifier.md +0 -35
  345. package/examples/sub-agents/roles/worker.md +0 -34
@@ -3,242 +3,25 @@
3
3
  * rules and a wrapper-aware scan of every place a command can start.
4
4
  */
5
5
  import { basename } from "node:path";
6
- import { stripNullAndNormalize } from "../shared/text-utils.js";
7
- const WHITESPACE = /\s+/;
8
- function stripComments(command) {
9
- let result = "";
10
- let inSingle = false;
11
- let inDouble = false;
12
- let escaped = false;
13
- for (let i = 0; i < command.length; i++) {
14
- const char = command[i];
15
- if (escaped) {
16
- result += char;
17
- escaped = false;
18
- continue;
19
- }
20
- if (!inSingle && char === "\\") {
21
- result += char;
22
- escaped = true;
23
- continue;
24
- }
25
- if (!inDouble && char === "'") {
26
- inSingle = !inSingle;
27
- result += char;
28
- continue;
29
- }
30
- if (!inSingle && char === '"') {
31
- inDouble = !inDouble;
32
- result += char;
33
- continue;
34
- }
35
- if (!inSingle && !inDouble && char === "#") {
36
- break;
37
- }
38
- result += char;
39
- }
40
- return result;
6
+ import { SHELL_DYNAMIC, ShellSyntaxError, shellCommands } from "./shell-parse.js";
7
+ function parseShellWords(command) {
8
+ const commands = shellCommands(command);
9
+ if (commands.length > 1)
10
+ throw new ShellSyntaxError("Expected one command");
11
+ return commands[0] ?? [];
41
12
  }
42
13
  function splitCommandChain(command) {
43
- const atoms = [];
44
- const normalized = stripComments(stripNullAndNormalize(command));
45
- function pushAtom(atom) {
46
- const trimmed = atom.trim();
47
- if (trimmed) {
48
- atoms.push(trimmed);
49
- }
50
- }
51
- function walk(input) {
52
- let buffer = "";
53
- let inSingle = false;
54
- let inDouble = false;
55
- let escaped = false;
56
- let parenDepth = 0;
57
- for (let i = 0; i < input.length; i++) {
58
- const char = input[i];
59
- const next = input[i + 1];
60
- if (escaped) {
61
- buffer += char;
62
- escaped = false;
63
- continue;
64
- }
65
- if (char === "\\") {
66
- buffer += char;
67
- escaped = true;
68
- continue;
69
- }
70
- if (!inDouble && char === "'") {
71
- inSingle = !inSingle;
72
- buffer += char;
73
- continue;
74
- }
75
- if (!inSingle && char === '"') {
76
- inDouble = !inDouble;
77
- buffer += char;
78
- continue;
79
- }
80
- if (!inSingle && char === "`") {
81
- let j = i + 1;
82
- let inner = "";
83
- let innerEscaped = false;
84
- while (j < input.length) {
85
- const innerChar = input[j];
86
- if (innerEscaped) {
87
- inner += innerChar;
88
- innerEscaped = false;
89
- j++;
90
- continue;
91
- }
92
- if (innerChar === "\\") {
93
- inner += innerChar;
94
- innerEscaped = true;
95
- j++;
96
- continue;
97
- }
98
- if (innerChar === "`") {
99
- break;
100
- }
101
- inner += innerChar;
102
- j++;
103
- }
104
- if (j < input.length) {
105
- walk(inner);
106
- buffer += "`subshell`";
107
- i = j;
108
- continue;
109
- }
110
- }
111
- if (!inSingle && char === "$" && next === "(") {
112
- let j = i + 2;
113
- let inner = "";
114
- let depth = 1;
115
- let innerSingle = false;
116
- let innerDouble = false;
117
- let innerEscaped = false;
118
- while (j < input.length) {
119
- const innerChar = input[j];
120
- if (innerEscaped) {
121
- inner += innerChar;
122
- innerEscaped = false;
123
- j++;
124
- continue;
125
- }
126
- if (innerChar === "\\") {
127
- inner += innerChar;
128
- innerEscaped = true;
129
- j++;
130
- continue;
131
- }
132
- if (!innerDouble && innerChar === "'") {
133
- innerSingle = !innerSingle;
134
- inner += innerChar;
135
- j++;
136
- continue;
137
- }
138
- if (!innerSingle && innerChar === '"') {
139
- innerDouble = !innerDouble;
140
- inner += innerChar;
141
- j++;
142
- continue;
143
- }
144
- if (!innerSingle && !innerDouble && innerChar === "(") {
145
- depth++;
146
- inner += innerChar;
147
- j++;
148
- continue;
149
- }
150
- if (!innerSingle && !innerDouble && innerChar === ")") {
151
- depth--;
152
- if (depth === 0) {
153
- break;
154
- }
155
- inner += innerChar;
156
- j++;
157
- continue;
158
- }
159
- inner += innerChar;
160
- j++;
161
- }
162
- if (depth === 0) {
163
- walk(inner);
164
- buffer += "$(subshell)";
165
- i = j;
166
- continue;
167
- }
168
- }
169
- if (!inSingle && !inDouble) {
170
- if (char === "(") {
171
- parenDepth++;
172
- }
173
- else if (char === ")" && parenDepth > 0) {
174
- parenDepth--;
175
- }
176
- const separator = parenDepth === 0 &&
177
- (char === ";" ||
178
- char === "\n" ||
179
- (char === "|" && next === "|") ||
180
- (char === "&" && next === "&") ||
181
- char === "|");
182
- if (separator) {
183
- pushAtom(buffer);
184
- buffer = "";
185
- if ((char === "|" && next === "|") || (char === "&" && next === "&")) {
186
- i++;
187
- }
188
- continue;
189
- }
190
- }
191
- buffer += char;
192
- }
193
- pushAtom(buffer);
194
- }
195
- walk(normalized);
196
- return atoms;
197
- }
198
- function parseShellWords(command) {
199
- const words = [];
200
- let buffer = "";
201
- let inSingle = false;
202
- let inDouble = false;
203
- let escaped = false;
204
- function pushWord() {
205
- if (buffer) {
206
- words.push(buffer);
207
- buffer = "";
208
- }
209
- }
210
- for (let i = 0; i < command.length; i++) {
211
- const char = command[i];
212
- if (escaped) {
213
- buffer += char;
214
- escaped = false;
215
- continue;
216
- }
217
- if (!inSingle && char === "\\") {
218
- escaped = true;
219
- continue;
220
- }
221
- if (!inDouble && char === "'") {
222
- inSingle = !inSingle;
223
- continue;
224
- }
225
- if (!inSingle && char === '"') {
226
- inDouble = !inDouble;
227
- continue;
228
- }
229
- if (!inSingle && !inDouble && WHITESPACE.test(char)) {
230
- pushWord();
231
- continue;
232
- }
233
- buffer += char;
234
- }
235
- pushWord();
236
- return words;
14
+ return shellCommands(command).map((words) => words.join(" "));
237
15
  }
238
16
  function parsedFromWords(words) {
239
17
  if (words.length === 0) {
240
18
  return null;
241
19
  }
20
+ if (words[0].includes(SHELL_DYNAMIC.substitution) ||
21
+ words[0].includes(SHELL_DYNAMIC.expansion) ||
22
+ words[0].includes(SHELL_DYNAMIC.glob) ||
23
+ /[*?[\]{}]/.test(words[0]))
24
+ throw new ShellSyntaxError("Dynamic executable is unsupported");
242
25
  const normalizedCommand = basename(words[0]).toLowerCase();
243
26
  const args = words.slice(1);
244
27
  return {
@@ -270,76 +53,200 @@ const WRAPPER_COMMANDS = new Set([
270
53
  "parallel",
271
54
  ]);
272
55
  const MAX_GUARD_DEPTH = 6;
273
- // Pull the script bodies out of `sh -c <script>` style invocations. Combined
274
- // flags such as `-lc` are handled by matching any single-dash cluster ending
275
- // in `c`. There can be more than one (`bash -c a -c b`), so return all.
56
+ // Shell option clusters may put c anywhere (-lc, -cl). After the first script,
57
+ // further operands are positional parameters, not more shell options.
276
58
  function extractShellScripts(parsed) {
277
- if (!SHELL_COMMANDS.has(parsed.command)) {
59
+ if (!SHELL_COMMANDS.has(parsed.command))
278
60
  return [];
279
- }
280
- const scripts = [];
281
61
  for (let i = 0; i < parsed.args.length; i++) {
282
62
  const arg = parsed.args[i];
283
- if (/^-[a-z]*c$/.test(arg) && i + 1 < parsed.args.length) {
284
- scripts.push(parsed.args[i + 1]);
285
- i++;
63
+ if (/^-[a-zA-Z]*c[a-zA-Z]*$/.test(arg)) {
64
+ if (parsed.args[i + 1] === undefined)
65
+ throw new ShellSyntaxError("Missing shell script");
66
+ return [parsed.args[i + 1]];
286
67
  }
68
+ if (!/^-[a-zA-Z]+$/.test(arg) || /^--/.test(arg))
69
+ throw new ShellSyntaxError("Unsupported shell invocation");
287
70
  }
288
- return scripts;
71
+ throw new ShellSyntaxError("Shell invocation without a literal -c script");
289
72
  }
290
- // Tokens after `find ... -exec`/`-execdir` up to the `;`/`+` terminator form a
291
- // command run per match. Return each such inner command as word arrays.
292
- function extractFindExecCommands(parsed) {
293
- if (parsed.command !== "find") {
294
- return [];
295
- }
296
- const inner = [];
73
+ const FIND_OPERANDS = new Set([
74
+ "-name",
75
+ "-iname",
76
+ "-path",
77
+ "-ipath",
78
+ "-wholename",
79
+ "-iwholename",
80
+ "-regex",
81
+ "-iregex",
82
+ "-type",
83
+ "-xtype",
84
+ "-size",
85
+ "-perm",
86
+ "-user",
87
+ "-group",
88
+ "-uid",
89
+ "-gid",
90
+ "-links",
91
+ "-inum",
92
+ "-mtime",
93
+ "-mmin",
94
+ "-ctime",
95
+ "-cmin",
96
+ "-atime",
97
+ "-amin",
98
+ "-newer",
99
+ "-anewer",
100
+ "-cnewer",
101
+ "-printf",
102
+ "-fprint",
103
+ "-fprint0",
104
+ "-fls",
105
+ "-maxdepth",
106
+ "-mindepth",
107
+ "-regextype",
108
+ "-files0-from",
109
+ ]);
110
+ function findActions(parsed) {
111
+ const actions = { commands: [], deletes: false };
112
+ if (parsed.command !== "find")
113
+ return actions;
297
114
  for (let i = 0; i < parsed.args.length; i++) {
298
- if (parsed.args[i] === "-exec" || parsed.args[i] === "-execdir") {
299
- const words = [];
300
- for (let j = i + 1; j < parsed.args.length; j++) {
301
- const token = parsed.args[j];
302
- if (token === ";" || token === "+" || token === "\\;") {
303
- break;
304
- }
305
- words.push(token);
306
- }
307
- if (words.length > 0) {
308
- inner.push(words);
309
- }
115
+ const arg = parsed.args[i];
116
+ if (FIND_OPERANDS.has(arg) || /^-newer[acmBt][acmBt]$/.test(arg)) {
117
+ i++;
118
+ continue;
310
119
  }
311
- }
312
- return inner;
120
+ if (arg === "-fprintf") {
121
+ i += 2;
122
+ continue;
123
+ }
124
+ if (arg === "-delete")
125
+ actions.deletes = true;
126
+ if (!["-exec", "-execdir", "-ok", "-okdir"].includes(arg))
127
+ continue;
128
+ const words = [];
129
+ while (++i < parsed.args.length && ![";", "+"].includes(parsed.args[i]))
130
+ words.push(parsed.args[i]);
131
+ if (!words.length || i === parsed.args.length)
132
+ throw new ShellSyntaxError("Unterminated find execution");
133
+ actions.commands.push(words);
134
+ }
135
+ return actions;
313
136
  }
314
- /**
315
- * How many suffix positions of a wrapper's arguments are treated as candidate command starts.
316
- *
317
- * The scan below is a superset heuristic, not a soundness requirement: the wrapper's own direct
318
- * rule, `sh -c` bodies and `find -exec` are all matched separately and are unaffected by this
319
- * bound. Without a bound the scan is quadratic in the argument count — every suffix is sliced and
320
- * re-joined into a normalized command line — and it recurses, so a wrapper whose arguments contain
321
- * further wrappers multiplied that cost up to `MAX_GUARD_DEPTH` times. Measured: ~6ms at 200
322
- * arguments, ~36ms at 800, which a long file list reaches without trying.
323
- *
324
- * A real inner command starts within the first few tokens, after the wrapper's own flags; no
325
- * wrapper in `WRAPPER_COMMANDS` takes anything close to 32 of them. Positions beyond this are
326
- * operands of the inner command, which has already been inspected from its own start position.
327
- */
328
- const MAX_WRAPPER_CANDIDATES = 32;
329
- // For a generic wrapper, the inner command can sit anywhere after the wrapper's
330
- // own options/operands (whose grammar varies per tool). Rather than parse each
331
- // wrapper's flags precisely, evaluate the leading suffix positions as candidate
332
- // command starts — this only ever adds detections, never relaxes them.
137
+ // Only wrapper options with known operand counts are accepted. Never treat an operand as
138
+ // another executable (e.g. env printf '%s' 'rm -rf /'), or stop scanning after a token budget.
139
+ const WRAPPER_OPTIONS = {
140
+ env: [
141
+ ["-i", "--ignore-environment", "-0", "--null"],
142
+ ["-u", "--unset", "-C", "--chdir"],
143
+ ],
144
+ time: [
145
+ ["-p", "-v", "--verbose", "-a", "--append"],
146
+ ["-f", "--format", "-o", "--output"],
147
+ ],
148
+ nice: [[], ["-n", "--adjustment"]],
149
+ ionice: [
150
+ ["-t", "--ignore"],
151
+ ["-c", "--class", "-n", "--classdata", "-p", "-P", "-u"],
152
+ ],
153
+ nohup: [[], []],
154
+ timeout: [
155
+ ["--preserve-status", "--foreground", "-v", "--verbose"],
156
+ ["-s", "--signal", "-k", "--kill-after"],
157
+ ],
158
+ stdbuf: [[], ["-i", "-o", "-e", "--input", "--output", "--error"]],
159
+ setsid: [["-c", "--ctty", "-f", "--fork", "-w", "--wait"], []],
160
+ chrt: [
161
+ ["-b", "-f", "-i", "-o", "-r", "-d", "-R", "-a", "--all-tasks"],
162
+ ["-T", "-P", "-D"],
163
+ ],
164
+ xargs: [
165
+ ["-0", "--null", "-r", "--no-run-if-empty", "-t", "--verbose", "-p", "--interactive", "-x"],
166
+ [
167
+ "-n",
168
+ "--max-args",
169
+ "-P",
170
+ "--max-procs",
171
+ "-I",
172
+ "-d",
173
+ "--delimiter",
174
+ "-s",
175
+ "--max-chars",
176
+ "-L",
177
+ "--max-lines",
178
+ "-E",
179
+ "--eof",
180
+ ],
181
+ ],
182
+ parallel: [
183
+ ["--will-cite", "--keep-order", "-k", "-0", "--null"],
184
+ ["-j", "--jobs", "-I", "--replace", "--colsep"],
185
+ ],
186
+ };
333
187
  function extractWrapperCandidates(parsed) {
334
- if (!WRAPPER_COMMANDS.has(parsed.command)) {
188
+ if (!WRAPPER_COMMANDS.has(parsed.command))
335
189
  return [];
190
+ const [flags, operands] = WRAPPER_OPTIONS[parsed.command];
191
+ let i = 0;
192
+ while (i < parsed.args.length) {
193
+ const arg = parsed.args[i];
194
+ if (arg === "--") {
195
+ i++;
196
+ break;
197
+ }
198
+ if (parsed.command === "env" && /^[A-Za-z_][A-Za-z0-9_]*=/.test(arg)) {
199
+ if (Object.values(SHELL_DYNAMIC).some((tag) => arg.includes(tag)))
200
+ throw new ShellSyntaxError("Dynamic wrapper environment is unsupported");
201
+ i++;
202
+ continue;
203
+ }
204
+ if (parsed.command === "nice" && /^-\d+$/.test(arg)) {
205
+ i++;
206
+ continue;
207
+ }
208
+ if (!arg.startsWith("-"))
209
+ break;
210
+ if (parsed.command === "xargs" && (arg.startsWith("-I") || arg.startsWith("--replace")))
211
+ throw new ShellSyntaxError("xargs template substitution is unsupported");
212
+ if (flags.includes(arg)) {
213
+ i++;
214
+ continue;
215
+ }
216
+ if (operands.includes(arg)) {
217
+ if (++i >= parsed.args.length)
218
+ throw new ShellSyntaxError("Missing wrapper option operand");
219
+ i++;
220
+ continue;
221
+ }
222
+ if (operands.some((option) => option.startsWith("--")
223
+ ? arg.startsWith(`${option}=`)
224
+ : arg.startsWith(option) && arg.length > option.length)) {
225
+ i++;
226
+ continue;
227
+ }
228
+ throw new ShellSyntaxError("Unsupported wrapper option");
336
229
  }
337
- const candidates = [];
338
- const limit = Math.min(parsed.args.length, MAX_WRAPPER_CANDIDATES);
339
- for (let i = 0; i < limit; i++) {
340
- candidates.push(parsed.args.slice(i));
341
- }
342
- return candidates;
230
+ if (["timeout", "chrt"].includes(parsed.command))
231
+ i++;
232
+ if (i >= parsed.args.length)
233
+ return [];
234
+ const words = parsed.args.slice(i);
235
+ if (words[0].startsWith("-"))
236
+ throw new ShellSyntaxError("Unresolved wrapper executable");
237
+ if (parsed.command === "parallel") {
238
+ const boundary = words.findIndex((word) => word === ":::" || word === "::::");
239
+ const template = boundary < 0 ? words : words.slice(0, boundary);
240
+ if (!template.length)
241
+ throw new ShellSyntaxError("Missing parallel template");
242
+ // Literal operands stay operands. GNU parallel's command-template operators and shell
243
+ // substitution combinations need a richer grammar; do not guess at those boundaries.
244
+ if (template.slice(1).some((word) => /[;&|`$()<>]/.test(word)))
245
+ throw new ShellSyntaxError("Complex parallel templates are unsupported");
246
+ const quote = (word) => `'${word.replace(/'/g, "'\\''")}'`;
247
+ return shellCommands([template[0], ...template.slice(1).map(quote)].join(" "));
248
+ }
249
+ return [words];
343
250
  }
344
251
  // An allow pattern exempts an atom only when it matches that atom's command
345
252
  // line from the start at a word boundary (exact, or a `pattern + space` prefix).
@@ -406,7 +313,8 @@ function matchRule(parsed, config) {
406
313
  matchedText: normalized,
407
314
  };
408
315
  }
409
- if (parsed.command === "find" && (argsText.includes(" -delete") || /\s-exec\s+rm(?:\s|$)/.test(` ${argsText}`))) {
316
+ const find = findActions(parsed);
317
+ if (find.deletes || find.commands.some((words) => basename(words[0]).toLowerCase() === "rm")) {
410
318
  return {
411
319
  category: "destructive-file-op",
412
320
  rule: "find-delete",
@@ -471,7 +379,6 @@ function matchRule(parsed, config) {
471
379
  (parsed.command === "wget" && parsed.args.includes("--post-file")) ||
472
380
  (parsed.command === "nc" && parsed.args.some((arg) => /^-[^-]*l/.test(arg) || arg === "--listen")) ||
473
381
  (parsed.command === "socat" && /\bexec\b/i.test(argsText)) ||
474
- normalized.includes("/dev/tcp/") ||
475
382
  (normalized.includes("mkfifo") && normalized.includes(" nc")) ||
476
383
  (parsed.command === "bash" && parsed.args.includes("-i") && /[>&]/.test(argsText))) {
477
384
  return {
@@ -501,9 +408,7 @@ function matchRule(parsed, config) {
501
408
  /(os\.system|subprocess|exec\(|eval\()/i.test(argsText)) ||
502
409
  (parsed.command === "perl" && parsed.args.includes("-e") && /\b(system|exec)\b/.test(argsText)) ||
503
410
  (parsed.command === "ruby" && parsed.args.includes("-e") && /\b(system|exec)\b/.test(argsText)) ||
504
- (parsed.command === "node" && parsed.args.includes("-e") && /\b(child_process|exec|spawn)\b/.test(argsText)) ||
505
- /\\x[0-9a-f]{2}/i.test(normalized) ||
506
- /\$'/.test(normalized)) {
411
+ (parsed.command === "node" && parsed.args.includes("-e") && /\b(child_process|exec|spawn)\b/.test(argsText))) {
507
412
  return {
508
413
  category: "obfuscation",
509
414
  rule: parsed.command,
@@ -526,27 +431,56 @@ function matchRule(parsed, config) {
526
431
  }
527
432
  // Evaluate a single already-parsed atom: its direct rule, plus any commands it
528
433
  // wraps (shell `-c` bodies, find `-exec`, generic wrappers), recursively.
529
- function inspectParsed(parsed, config, depth) {
530
- if (atomAllowed(parsed.normalized, config.allowPatterns)) {
531
- return null;
532
- }
533
- const direct = matchRule(parsed, config);
434
+ function inspectParsed(parsed, config, depth, templateSubstitution = false) {
435
+ if (!WRAPPER_COMMANDS.has(parsed.command) &&
436
+ !["echo", "printf"].includes(parsed.command) &&
437
+ parsed.args.some((arg) => arg.includes(SHELL_DYNAMIC.expansion) || arg.includes(SHELL_DYNAMIC.substitution)))
438
+ throw new ShellSyntaxError("Dynamic command operands are unsupported");
439
+ if (!WRAPPER_COMMANDS.has(parsed.command) &&
440
+ ![
441
+ "echo",
442
+ "printf",
443
+ "ls",
444
+ "cat",
445
+ "head",
446
+ "tail",
447
+ "wc",
448
+ "sort",
449
+ "uniq",
450
+ "cut",
451
+ "tr",
452
+ "grep",
453
+ "rg",
454
+ "sed",
455
+ "stat",
456
+ "file",
457
+ "du",
458
+ "diff",
459
+ "jq",
460
+ "sha256sum",
461
+ "md5sum",
462
+ ].includes(parsed.command) &&
463
+ parsed.args.some((arg) => arg.includes(SHELL_DYNAMIC.glob)))
464
+ throw new ShellSyntaxError("Dynamic pathname operands are unsupported for this command");
465
+ const direct = atomAllowed(parsed.normalized, config.allowPatterns) ? null : matchRule(parsed, config);
534
466
  if (direct) {
535
467
  return direct;
536
468
  }
537
469
  if (depth >= MAX_GUARD_DEPTH) {
538
- return null;
470
+ throw new ShellSyntaxError("Command recursion limit");
539
471
  }
540
472
  for (const script of extractShellScripts(parsed)) {
473
+ if (templateSubstitution && script.includes("{}"))
474
+ throw new ShellSyntaxError("Template substitution into shell scripts is unsupported");
541
475
  const nested = evaluateCommand(script, config, depth + 1);
542
476
  if (nested) {
543
477
  return nested;
544
478
  }
545
479
  }
546
- for (const words of [...extractFindExecCommands(parsed), ...extractWrapperCandidates(parsed)]) {
480
+ for (const words of [...findActions(parsed).commands, ...extractWrapperCandidates(parsed)]) {
547
481
  const innerParsed = parsedFromWords(words);
548
482
  if (innerParsed) {
549
- const nested = inspectParsed(innerParsed, config, depth + 1);
483
+ const nested = inspectParsed(innerParsed, config, depth + 1, templateSubstitution || parsed.command === "find" || parsed.command === "parallel");
550
484
  if (nested) {
551
485
  return nested;
552
486
  }
@@ -555,28 +489,25 @@ function inspectParsed(parsed, config, depth) {
555
489
  return null;
556
490
  }
557
491
  function evaluateCommand(command, config, depth) {
558
- const normalizedWhole = stripNullAndNormalize(command);
492
+ const commands = shellCommands(command);
559
493
  if (config.blockObfuscation &&
560
- (/\bbase64\b.*(?:-d|--decode).*?\|\s*(bash|sh|exec|eval)\b/i.test(normalizedWhole) ||
561
- /\beval\s+\$\(/i.test(normalizedWhole) ||
562
- /\\x[0-9a-f]{2}/i.test(normalizedWhole) ||
563
- /\$'/.test(normalizedWhole))) {
494
+ commands.some((words, index) => words[0] === "base64" &&
495
+ words.some((arg) => arg === "-d" || arg === "--decode") &&
496
+ SHELL_COMMANDS.has(commands[index + 1]?.[0]))) {
564
497
  return {
565
498
  category: "obfuscation",
566
- rule: "obfuscation-whole-command",
567
- reason: "Obfuscated command execution is not allowed",
568
- matchedText: normalizedWhole,
499
+ rule: "decoded-shell",
500
+ reason: "Decoded shell input is not allowed",
501
+ matchedText: command,
569
502
  };
570
503
  }
571
- for (const atom of splitCommandChain(command)) {
572
- const parsed = parseCommand(atom);
573
- if (!parsed) {
504
+ for (const words of commands) {
505
+ const parsed = parsedFromWords(words);
506
+ if (!parsed)
574
507
  continue;
575
- }
576
508
  const match = inspectParsed(parsed, config, depth);
577
- if (match) {
509
+ if (match)
578
510
  return match;
579
- }
580
511
  }
581
512
  return null;
582
513
  }
@@ -584,7 +515,21 @@ export function guardCommand(command, config) {
584
515
  if (!config.enabled) {
585
516
  return { allowed: true };
586
517
  }
587
- const match = evaluateCommand(command, config, 0);
518
+ let match;
519
+ try {
520
+ match = evaluateCommand(command, config, 0);
521
+ }
522
+ catch (error) {
523
+ if (!(error instanceof ShellSyntaxError))
524
+ throw error;
525
+ return {
526
+ allowed: false,
527
+ category: "unsupported-shell",
528
+ rule: "shell-parse",
529
+ reason: `${error.message}. Use literal simple commands or separate tool calls.`,
530
+ matchedText: command,
531
+ };
532
+ }
588
533
  if (match) {
589
534
  return {
590
535
  allowed: false,