@buffcore/cli 0.8.40 → 0.8.42

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 (182) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/commands/agents-file.d.ts +5 -0
  3. package/dist/commands/agents-file.js +30 -7
  4. package/dist/commands/change.d.ts +4 -0
  5. package/dist/commands/change.js +12 -5
  6. package/dist/commands/create-block.d.ts +2 -1
  7. package/dist/commands/create-block.js +7 -5
  8. package/dist/commands/create-steps.en.js +1 -1
  9. package/dist/commands/create-steps.js +1 -1
  10. package/dist/commands/create.d.ts +3 -2
  11. package/dist/commands/create.en.js +1 -1
  12. package/dist/commands/create.es.js +1 -1
  13. package/dist/commands/create.js +8 -6
  14. package/dist/commands/design/check.d.ts +2 -0
  15. package/dist/commands/design/check.js +6 -0
  16. package/dist/commands/design/journey-roles.d.ts +6 -0
  17. package/dist/commands/design/journey-roles.js +60 -0
  18. package/dist/commands/design/references.d.ts +3 -0
  19. package/dist/commands/design/references.js +25 -0
  20. package/dist/commands/design/tells-text.js +2 -2
  21. package/dist/commands/design/tells.d.ts +3 -1
  22. package/dist/commands/design/tells.en.d.ts +2 -0
  23. package/dist/commands/design/tells.en.js +2 -0
  24. package/dist/commands/design/tells.es.js +2 -0
  25. package/dist/commands/design/tells.js +10 -7
  26. package/dist/commands/dev.js +3 -0
  27. package/dist/commands/port-claims.d.ts +23 -0
  28. package/dist/commands/port-claims.js +95 -0
  29. package/dist/commands/prototype/browser.d.ts +9 -4
  30. package/dist/commands/prototype/browser.js +26 -18
  31. package/dist/commands/prototype/export-base.d.ts +27 -0
  32. package/dist/commands/prototype/export-base.js +80 -0
  33. package/dist/commands/prototype/export.d.ts +3 -2
  34. package/dist/commands/prototype/export.js +13 -6
  35. package/dist/commands/prototype/index.js +1 -0
  36. package/dist/commands/prototype/media-ready.d.ts +8 -0
  37. package/dist/commands/prototype/media-ready.js +36 -0
  38. package/dist/commands/prototype/messages.en.d.ts +6 -0
  39. package/dist/commands/prototype/messages.en.js +6 -0
  40. package/dist/commands/prototype/messages.js +6 -0
  41. package/dist/commands/prototype/scene.d.ts +3 -1
  42. package/dist/commands/prototype/scene.js +22 -6
  43. package/dist/commands/prototype/snapshot-inputs.js +35 -7
  44. package/dist/commands/prototype/snapshot-setup.d.ts +1 -1
  45. package/dist/commands/prototype/snapshot-setup.js +2 -1
  46. package/dist/commands/prototype/snapshot-themes.js +3 -1
  47. package/dist/commands/prototype/snapshot.d.ts +2 -0
  48. package/dist/commands/prototype/snapshot.js +2 -1
  49. package/dist/commands/ui/autopilot-records.d.ts +3 -0
  50. package/dist/commands/ui/chat/allow-folder-route.d.ts +9 -0
  51. package/dist/commands/ui/chat/allow-folder-route.js +56 -0
  52. package/dist/commands/ui/chat/allow-folder.d.ts +18 -0
  53. package/dist/commands/ui/chat/allow-folder.js +76 -0
  54. package/dist/commands/ui/chat/ask-person.d.ts +17 -0
  55. package/dist/commands/ui/chat/ask-person.en.d.ts +4 -0
  56. package/dist/commands/ui/chat/ask-person.en.js +4 -0
  57. package/dist/commands/ui/chat/ask-person.es.d.ts +4 -0
  58. package/dist/commands/ui/chat/ask-person.es.js +4 -0
  59. package/dist/commands/ui/chat/ask-person.js +60 -0
  60. package/dist/commands/ui/chat/claude-permissions.d.ts +21 -0
  61. package/dist/commands/ui/chat/claude-permissions.js +69 -0
  62. package/dist/commands/ui/chat/codex-idle.d.ts +1 -0
  63. package/dist/commands/ui/chat/codex-overrides.js +2 -1
  64. package/dist/commands/ui/chat/codex-permissions.d.ts +12 -0
  65. package/dist/commands/ui/chat/codex-permissions.js +97 -0
  66. package/dist/commands/ui/chat/command-parts.d.ts +33 -0
  67. package/dist/commands/ui/chat/command-parts.js +219 -0
  68. package/dist/commands/ui/chat/command-wrappers.d.ts +21 -0
  69. package/dist/commands/ui/chat/command-wrappers.js +100 -0
  70. package/dist/commands/ui/chat/design-deletes.d.ts +1 -0
  71. package/dist/commands/ui/chat/design-deletes.js +39 -0
  72. package/dist/commands/ui/chat/launch-input.d.ts +4 -2
  73. package/dist/commands/ui/chat/launch-input.js +16 -3
  74. package/dist/commands/ui/chat/limit-mentions.d.ts +5 -0
  75. package/dist/commands/ui/chat/limit-mentions.js +74 -0
  76. package/dist/commands/ui/chat/limit-patterns.d.ts +15 -0
  77. package/dist/commands/ui/chat/limit-patterns.js +182 -0
  78. package/dist/commands/ui/chat/limits-hook.d.ts +1 -0
  79. package/dist/commands/ui/chat/limits-hook.js +30 -0
  80. package/dist/commands/ui/chat/limits.d.ts +37 -0
  81. package/dist/commands/ui/chat/limits.en.d.ts +18 -0
  82. package/dist/commands/ui/chat/limits.en.js +18 -0
  83. package/dist/commands/ui/chat/limits.es.d.ts +2 -0
  84. package/dist/commands/ui/chat/limits.es.js +17 -0
  85. package/dist/commands/ui/chat/limits.js +224 -0
  86. package/dist/commands/ui/chat/local-settings.d.ts +17 -0
  87. package/dist/commands/ui/chat/local-settings.js +73 -0
  88. package/dist/commands/ui/chat/opencode-config.d.ts +22 -0
  89. package/dist/commands/ui/chat/opencode-config.js +83 -0
  90. package/dist/commands/ui/chat/parse-claude.js +18 -2
  91. package/dist/commands/ui/chat/permission-ask.d.ts +44 -0
  92. package/dist/commands/ui/chat/permission-ask.js +142 -0
  93. package/dist/commands/ui/chat/permission-channel.d.ts +18 -0
  94. package/dist/commands/ui/chat/permission-channel.js +67 -0
  95. package/dist/commands/ui/chat/permission-route.d.ts +10 -0
  96. package/dist/commands/ui/chat/permission-route.js +47 -0
  97. package/dist/commands/ui/chat/permission-tool.d.ts +1 -0
  98. package/dist/commands/ui/chat/permission-tool.js +86 -0
  99. package/dist/commands/ui/chat/permissions.d.ts +11 -12
  100. package/dist/commands/ui/chat/permissions.js +20 -102
  101. package/dist/commands/ui/chat/planning-limits.d.ts +12 -0
  102. package/dist/commands/ui/chat/planning-limits.js +31 -0
  103. package/dist/commands/ui/chat/process.js +4 -1
  104. package/dist/commands/ui/chat/prompt.d.ts +8 -1
  105. package/dist/commands/ui/chat/prompt.en.d.ts +4 -0
  106. package/dist/commands/ui/chat/prompt.en.js +4 -0
  107. package/dist/commands/ui/chat/prompt.es.d.ts +4 -0
  108. package/dist/commands/ui/chat/prompt.es.js +4 -0
  109. package/dist/commands/ui/chat/prompt.js +18 -50
  110. package/dist/commands/ui/chat/recorded-turn.js +5 -0
  111. package/dist/commands/ui/chat/routes.js +4 -0
  112. package/dist/commands/ui/chat/secret-folders.d.ts +16 -0
  113. package/dist/commands/ui/chat/secret-folders.js +35 -0
  114. package/dist/commands/ui/chat/stdin-shells.d.ts +8 -0
  115. package/dist/commands/ui/chat/stdin-shells.js +102 -0
  116. package/dist/commands/ui/chat/step-rules.d.ts +13 -0
  117. package/dist/commands/ui/chat/step-rules.js +47 -0
  118. package/dist/commands/ui/chat/store.d.ts +2 -0
  119. package/dist/commands/ui/chat/store.js +25 -3
  120. package/dist/commands/ui/chat/stream.en.d.ts +17 -0
  121. package/dist/commands/ui/chat/stream.en.js +18 -1
  122. package/dist/commands/ui/chat/stream.es.d.ts +17 -0
  123. package/dist/commands/ui/chat/stream.es.js +18 -1
  124. package/dist/commands/ui/chat/stream.js +4 -1
  125. package/dist/commands/ui/chat/turn-guard.d.ts +25 -0
  126. package/dist/commands/ui/chat/turn-guard.js +74 -0
  127. package/dist/commands/ui/chat/turn-input.d.ts +3 -2
  128. package/dist/commands/ui/chat/turn-input.js +3 -1
  129. package/dist/commands/ui/chat/turn-reserve.js +2 -0
  130. package/dist/commands/ui/chat/turn-scenes.d.ts +19 -0
  131. package/dist/commands/ui/chat/turn-scenes.js +44 -0
  132. package/dist/commands/ui/chat/turn-selection.d.ts +3 -0
  133. package/dist/commands/ui/chat/turn-selection.js +9 -3
  134. package/dist/commands/ui/chat/turn-state.d.ts +6 -0
  135. package/dist/commands/ui/chat/turn.js +8 -3
  136. package/dist/commands/ui/level-route.js +8 -3
  137. package/dist/commands/ui/page.d.ts +19 -1
  138. package/dist/commands/ui/page.js +9 -6
  139. package/dist/commands/ui/plan-structure.d.ts +9 -2
  140. package/dist/commands/ui/plan-structure.en.d.ts +1 -0
  141. package/dist/commands/ui/plan-structure.en.js +1 -0
  142. package/dist/commands/ui/plan-structure.es.d.ts +1 -0
  143. package/dist/commands/ui/plan-structure.es.js +1 -0
  144. package/dist/commands/ui/plan-structure.js +36 -10
  145. package/dist/commands/ui/prototype-main.d.ts +1 -0
  146. package/dist/commands/ui/prototype-scenes-child.js +3 -1
  147. package/dist/commands/ui/prototype-scenes-read.d.ts +1 -0
  148. package/dist/commands/ui/prototype-scenes-route.js +7 -1
  149. package/dist/commands/ui/save-commit.js +6 -2
  150. package/dist/commands/ui/server-options.js +2 -0
  151. package/dist/commands/ui/server.d.ts +5 -22
  152. package/dist/commands/ui/server.js +3 -45
  153. package/dist/commands/ui/snapshot-runners.d.ts +22 -0
  154. package/dist/commands/ui/snapshot-runners.js +45 -0
  155. package/dist/commands/ui/studio-routes.d.ts +1 -0
  156. package/dist/commands/ui/studio-routes.js +1 -0
  157. package/dist/commands/ui/studio-state.d.ts +4 -1
  158. package/dist/commands/ui/studio-state.js +1 -0
  159. package/dist/commands/ui/studio.en.d.ts +13 -0
  160. package/dist/commands/ui/studio.en.js +13 -0
  161. package/dist/commands/ui/studio.es.d.ts +13 -0
  162. package/dist/commands/ui/studio.es.js +13 -0
  163. package/dist/commands/ui/theme-option.d.ts +8 -0
  164. package/dist/commands/ui/theme-option.js +40 -0
  165. package/dist/commands/ui/theme-values.d.ts +5 -0
  166. package/dist/commands/ui/theme-values.js +113 -0
  167. package/dist/ui/page.js +86 -86
  168. package/docs/PROTOTYPE.md +4 -1
  169. package/docs/WORKBENCH.md +90 -26
  170. package/package.json +11 -11
  171. package/skills/ape-critique/SKILL.md +8 -4
  172. package/skills/ape-design-system/SKILL.md +14 -3
  173. package/skills/ape-plan-change/SKILL.md +9 -5
  174. package/skills/ape-prototype/SKILL.md +8 -1
  175. package/templates/example/apps/app/src/i18n/en.ts +2 -0
  176. package/templates/example/apps/app/src/i18n/es.json +2 -0
  177. package/templates/product/README.md +1 -1
  178. package/templates/product/apps/admin/src/fonts.ts +2 -1
  179. package/templates/product/apps/app/src/fonts.ts +5 -1
  180. package/templates/product/apps/app/src/i18n/en.ts +2 -0
  181. package/templates/product/apps/app/src/i18n/es.json +2 -0
  182. package/templates/product/prototype/journeys.ts +3 -0
@@ -0,0 +1,97 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import { globSync, readdirSync, realpathSync } from 'node:fs';
3
+ import { join, matchesGlob, resolve } from 'node:path';
4
+ import { mergeHoldPath } from '../merge-hold.js';
5
+ import { windowsExampleDenials } from './codex-examples.js';
6
+ import { LINKED_SECRETS, linkedPackages } from './linked.js';
7
+ import { APPROVAL_PATH, CHAT_PATH, COMMAND_PATHS, COMMENT_PATH, LEVEL_PATHS, turnWriteDenials, } from './permissions.js';
8
+ /** JSON's string escaping is also valid for TOML basic keys, including quotes and backslashes. */
9
+ export function codexTrust(folder) {
10
+ return `projects={${JSON.stringify(realpathSync.native(folder))}={trust_level="trusted"}}`;
11
+ }
12
+ const linkedScans = new AsyncLocalStorage();
13
+ /** Share a scan between argument construction and shim validation, only for this launch. */
14
+ export function withLinkedPermissions(launch) {
15
+ return linkedScans.run(new Map(), launch);
16
+ }
17
+ function linkedSecrets(packages) {
18
+ const denied = new Set([join(packages, '.ape/comments')]);
19
+ const pending = [packages];
20
+ while (pending.length) {
21
+ const folder = pending.pop();
22
+ for (const entry of readdirSync(folder, { withFileTypes: true })) {
23
+ const path = join(folder, entry.name);
24
+ if (entry.name === 'node_modules')
25
+ continue;
26
+ if (entry.name === '.ape') {
27
+ denied.add(join(path, 'comments'));
28
+ continue;
29
+ }
30
+ if (entry.name === '.git' || entry.name === '.secrets') {
31
+ denied.add(path);
32
+ continue;
33
+ }
34
+ // Dirent does not classify junctions/symlinks as directories; never follow one.
35
+ if (entry.isDirectory() && !entry.isSymbolicLink())
36
+ pending.push(path);
37
+ else if (LINKED_SECRETS.some((pattern) => matchesGlob(entry.name, pattern)))
38
+ denied.add(path);
39
+ }
40
+ }
41
+ return [...denied].sort();
42
+ }
43
+ function codexLinked(product) {
44
+ const cache = linkedScans.getStore();
45
+ const key = resolve(product);
46
+ const cached = cache?.get(key);
47
+ if (cached)
48
+ return cached;
49
+ const packages = linkedPackages(product);
50
+ const entries = packages
51
+ ? [
52
+ `${JSON.stringify(packages)}="read"`,
53
+ ...linkedSecrets(packages).map((secret) => `${JSON.stringify(secret)}="deny"`),
54
+ ]
55
+ : [];
56
+ cache?.set(key, entries);
57
+ return entries;
58
+ }
59
+ /**
60
+ * Every turn denies comments and the approval record; design and validation, which write the
61
+ * product folder, also the change trees (planning writes plan/ only). Codex takes absolute
62
+ * paths only (0.166 rejects read globs), so a nested product's prototype/versions is not
63
+ * named here; Claude's rule covers it. Design/validation expand read paths.
64
+ */
65
+ export function codexFilesystem(work, folder, product = folder, examples) {
66
+ const root = realpathSync.native(folder);
67
+ const bases = [...new Set([root, realpathSync.native(product)])];
68
+ const open = work === 'design' || work === 'validation';
69
+ // A change tree keeps its own copy of the record (git merge main writes it); the product's is guarded.
70
+ const guards = open
71
+ ? bases.flatMap((base) => turnWriteDenials(work).map((path) => join(base, path)))
72
+ : [join(realpathSync.native(product), APPROVAL_PATH)];
73
+ const hold = mergeHoldPath(product);
74
+ const denied = [
75
+ ...windowsExampleDenials,
76
+ ...(examples ? [`${JSON.stringify(examples)}="read"`] : []),
77
+ ...codexLinked(product),
78
+ ...bases.map((base) => `${JSON.stringify(join(base, COMMENT_PATH))}="deny"`),
79
+ ...bases.flatMap((base) => LEVEL_PATHS.map((path) => `${JSON.stringify(join(base, path))}="read"`)),
80
+ ...guards.map((path) => `${JSON.stringify(path)}="read"`),
81
+ ...(hold ? [hold, `${hold}.lock`].map((path) => `${JSON.stringify(path)}="deny"`) : []),
82
+ ].join(',');
83
+ if (!open)
84
+ return [`permissions.ape.filesystem={${denied}}`];
85
+ const roots = [...COMMAND_PATHS.map((path) => path.replace(/\/\*\*$/, '')), CHAT_PATH];
86
+ const nested = globSync(roots.map((path) => `**/${path}`), {
87
+ cwd: root,
88
+ exclude: (path) => /(?:^|[/\\])(?:node_modules|\.git|\.ape)[/\\]/.test(path),
89
+ });
90
+ const paths = [...new Set([...roots, ...nested.map((path) => path.replaceAll('\\', '/'))])];
91
+ const entries = paths
92
+ .sort()
93
+ .map((path) => `${JSON.stringify(join(root, path))}="read"`)
94
+ .join(',');
95
+ // Absolute entries leave the product's :workspace_roots deny table untouched, including overlaps.
96
+ return [`permissions.ape.filesystem={${entries},${denied}}`];
97
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * A shell command read the way the limits and the permission card need it: its parts (what runs
3
+ * between pipes, `;`, `&&`, `||`, `&`, new lines, subshells and command substitutions) and each
4
+ * part's words. Over-splitting is safe here: every part is checked, so a missed separator could
5
+ * only hide a part, never add one. A line continuation joins its lines first; a redirection
6
+ * (`2>&1`, `>&2`, `&>`) stays in its part; a here-document's body is data, not a command, unless
7
+ * it can run a substitution.
8
+ */
9
+ export type Shell = 'bash' | 'powershell' | 'cmd';
10
+ /** Each shell's escape character: it also joins a line ending in it to the next. */
11
+ export declare const ESCAPE: Record<Shell, string>;
12
+ /**
13
+ * What reading a command could not follow, so the limits ask the person instead of trusting the
14
+ * parse: too much work (`budget`), a here-document whose end line never comes (`heredoc`), a shell
15
+ * or interpreter fed its script on standard input (`stdin`), or text run as a command (`expression`).
16
+ */
17
+ export type Unsure = 'budget' | 'heredoc' | 'stdin' | 'expression';
18
+ /** A tally the limits share across one call's reading: characters parsed, and what was unsure. */
19
+ export interface Reading {
20
+ steps: number;
21
+ unsure?: Unsure;
22
+ }
23
+ export declare function commandParts(command: string, shell?: Shell, reading?: Reading): string[];
24
+ /**
25
+ * A part's words, quotes removed; a quoted word keeps its spaces. With the shell's escape
26
+ * character, an escaped character stands for itself (`p\ush` is `push`); without it, every
27
+ * character is kept as written, which keeps a Windows path's backslashes.
28
+ */
29
+ export declare function partWords(part: string, escaper?: string): string[];
30
+ /** The program a word names, without its folder or a Windows extension, in lower case. */
31
+ export declare const programName: (word: string) => string;
32
+ /** The part's program: its first word after any `NAME=value` settings. */
33
+ export declare function partProgram(part: string): string;
@@ -0,0 +1,219 @@
1
+ /** Each shell's escape character: it also joins a line ending in it to the next. */
2
+ export const ESCAPE = { bash: '\\', powershell: '`', cmd: '^' };
3
+ /**
4
+ * A here-document's operator at `index` (just past `<<`): its word as Bash reads it, quotes and
5
+ * backslashes removed (`E"OF"` and `E\OF` are `EOF`), and where the word ends. Any quoting makes
6
+ * the body plain text.
7
+ */
8
+ function heredocWord(command, index) {
9
+ let at = index;
10
+ const strip = command[at] === '-';
11
+ if (strip)
12
+ at++;
13
+ while (command[at] === ' ' || command[at] === '\t')
14
+ at++;
15
+ let word = '';
16
+ let quoted = false;
17
+ for (; at < command.length; at++) {
18
+ const char = command[at];
19
+ if (char === "'" || char === '"') {
20
+ const close = command.indexOf(char, at + 1);
21
+ if (close < 0)
22
+ return undefined;
23
+ word += command.slice(at + 1, close);
24
+ quoted = true;
25
+ at = close;
26
+ }
27
+ else if (char === '\\' && at + 1 < command.length) {
28
+ word += command[++at];
29
+ quoted = true;
30
+ }
31
+ else if (/[\s;|&<>()`]/.test(char))
32
+ break;
33
+ else
34
+ word += char;
35
+ }
36
+ if (!word && !quoted)
37
+ return undefined;
38
+ return { doc: { word, strip, expand: !quoted }, end: at };
39
+ }
40
+ /**
41
+ * Skips the bodies after a line's here-documents; returns the index of their last line's end. A
42
+ * body whose end line never comes swallows the rest of the command, so the reading marks it.
43
+ */
44
+ function skipBodies(command, from, docs, parts, reading) {
45
+ let at = from;
46
+ for (const doc of docs) {
47
+ const body = [];
48
+ let ended = false;
49
+ while (at < command.length) {
50
+ const end = command.indexOf('\n', at + 1);
51
+ const line = command.slice(at + 1, end < 0 ? command.length : end).replace(/\r$/, '');
52
+ at = end < 0 ? command.length : end;
53
+ ended = (doc.strip ? line.replace(/^\t+/, '') : line) === doc.word;
54
+ if (ended)
55
+ break;
56
+ body.push(line);
57
+ }
58
+ if (!ended && reading)
59
+ reading.unsure ??= 'heredoc';
60
+ // An unquoted word lets the body run `$(…)` and backticks: check the body as commands then.
61
+ const text = body.join('\n');
62
+ if (doc.expand && /`|\$\(/.test(text))
63
+ parts.push(...commandParts(text, 'bash', reading));
64
+ }
65
+ return at;
66
+ }
67
+ export function commandParts(command, shell = 'bash', reading) {
68
+ if (reading)
69
+ reading.steps += command.length;
70
+ const parts = [];
71
+ const escaper = ESCAPE[shell];
72
+ const stack = [{ text: '', depth: 0 }];
73
+ let frame = stack[0];
74
+ let docs = [];
75
+ const cut = () => {
76
+ if (frame.text.trim())
77
+ parts.push(frame.text.trim());
78
+ frame.text = '';
79
+ };
80
+ const open = (closer, arithmetic = false) => {
81
+ frame.text += '$(…)';
82
+ frame = { text: '', closer, depth: 0, arithmetic };
83
+ stack.push(frame);
84
+ };
85
+ const close = () => {
86
+ cut();
87
+ stack.pop();
88
+ frame = stack.at(-1);
89
+ };
90
+ for (let index = 0; index < command.length; index++) {
91
+ const char = command[index];
92
+ const next = command[index + 1];
93
+ if (frame.quote === "'") {
94
+ if (char === "'")
95
+ frame.quote = undefined;
96
+ frame.text += char;
97
+ continue;
98
+ }
99
+ if (char === escaper && next !== undefined && (shell !== 'cmd' || !frame.quote)) {
100
+ const crlf = next === '\r' && command[index + 2] === '\n';
101
+ if (next === '\n' || crlf)
102
+ index += crlf ? 2 : 1;
103
+ else
104
+ frame.text += char + command[++index];
105
+ continue;
106
+ }
107
+ // Command substitution runs inside double quotes too.
108
+ if (shell === 'bash' && char === '`') {
109
+ if (frame.closer === '`')
110
+ close();
111
+ else
112
+ open('`');
113
+ continue;
114
+ }
115
+ if (shell !== 'cmd' && char === '$' && next === '(') {
116
+ open(')', command[index + 2] === '(');
117
+ index++;
118
+ continue;
119
+ }
120
+ if (frame.quote === '"') {
121
+ if (char === '"')
122
+ frame.quote = undefined;
123
+ frame.text += char;
124
+ continue;
125
+ }
126
+ if (char === "'" || char === '"') {
127
+ if (char === '"' || shell !== 'cmd')
128
+ frame.quote = char;
129
+ frame.text += char;
130
+ continue;
131
+ }
132
+ const shift = frame.arithmetic || command[index + 2] === '<';
133
+ if (shell === 'bash' && char === '<' && next === '<' && !shift) {
134
+ const found = heredocWord(command, index + 2);
135
+ if (found) {
136
+ docs.push(found.doc);
137
+ frame.text += command.slice(index, found.end);
138
+ index = found.end - 1;
139
+ continue;
140
+ }
141
+ }
142
+ const last = frame.text.at(-1);
143
+ const redirect = (char === '&' && (last === '>' || last === '<' || next === '>')) ||
144
+ (char === '|' && last === '>');
145
+ if (redirect)
146
+ frame.text += char;
147
+ else if (char === '\n') {
148
+ cut();
149
+ if (docs.length)
150
+ index = skipBodies(command, index, docs, parts, reading) - 1;
151
+ docs = [];
152
+ }
153
+ else if (char === ')' && frame.closer === ')' && !frame.depth)
154
+ close();
155
+ else if ('|&;\r()'.includes(char)) {
156
+ if (frame.closer === ')')
157
+ frame.depth += char === '(' ? 1 : char === ')' ? -1 : 0;
158
+ cut();
159
+ }
160
+ else
161
+ frame.text += char;
162
+ }
163
+ while (stack.length > 1)
164
+ close();
165
+ cut();
166
+ return parts;
167
+ }
168
+ /**
169
+ * A part's words, quotes removed; a quoted word keeps its spaces. With the shell's escape
170
+ * character, an escaped character stands for itself (`p\ush` is `push`); without it, every
171
+ * character is kept as written, which keeps a Windows path's backslashes.
172
+ */
173
+ export function partWords(part, escaper) {
174
+ const words = [];
175
+ let word = '';
176
+ let quote;
177
+ let started = false;
178
+ for (let index = 0; index < part.length; index++) {
179
+ const char = part[index];
180
+ const next = part[index + 1];
181
+ if (escaper && char === escaper && next !== undefined && quote !== "'") {
182
+ // Bash's backslash keeps its meaning inside double quotes only before $ ` " \.
183
+ if (quote !== '"' || escaper !== '\\' || '$`"\\'.includes(next)) {
184
+ word += next;
185
+ index++;
186
+ started = true;
187
+ continue;
188
+ }
189
+ }
190
+ if (quote) {
191
+ if (char === quote)
192
+ quote = undefined;
193
+ else
194
+ word += char;
195
+ }
196
+ else if (char === "'" || char === '"') {
197
+ quote = char;
198
+ started = true;
199
+ }
200
+ else if (/\s/.test(char)) {
201
+ if (started || word)
202
+ words.push(word);
203
+ word = '';
204
+ started = false;
205
+ }
206
+ else
207
+ word += char;
208
+ }
209
+ if (started || word)
210
+ words.push(word);
211
+ return words;
212
+ }
213
+ /** The program a word names, without its folder or a Windows extension, in lower case. */
214
+ export const programName = (word) => (word.split(/[\\/]/).at(-1) ?? '').toLowerCase().replace(/\.(?:exe|cmd|bat|ps1|mjs|js)$/, '');
215
+ /** The part's program: its first word after any `NAME=value` settings. */
216
+ export function partProgram(part) {
217
+ const words = partWords(part);
218
+ return words.find((word) => !/^[A-Za-z_][A-Za-z0-9_]*=/.test(word)) ?? words[0] ?? part;
219
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * The commands a part hands to another shell: `sh -c '…'`, `bash -lc "…"`, `cmd /c …`,
3
+ * `powershell -Command …` (or `-EncodedCommand`, or a bare command), `pwsh -c …`, `eval …` and
4
+ * `iex …` (`Invoke-Expression`),
5
+ * found anywhere in the part, so `pnpm exec sh -c "…"` and `env X=1 bash -c …` count too. The
6
+ * limits check each one with the same rules (limits.ts). Plain Node only: the hook runs it.
7
+ */
8
+ import { type Shell } from './command-parts.ts';
9
+ export interface Wrapped {
10
+ script: string;
11
+ shell: Shell;
12
+ }
13
+ export declare const POSIX: Set<string>;
14
+ export declare const POWERSHELL: Set<string>;
15
+ /** `sh`'s script: the first word after its options, when one of them is `-c` (`-lc`, `-ec`…). */
16
+ export declare function posixScript(words: string[], from: number): string | undefined;
17
+ /** PowerShell's command: after `-Command` (or any prefix), `-EncodedCommand`, or bare words. */
18
+ export declare function powershellScript(words: string[], from: number): string | undefined;
19
+ export declare const EXPRESSIONS: Set<string>;
20
+ /** Every script the part's words hand to another shell; past `cap` of them, one more and stop. */
21
+ export declare function wrappedScripts(words: string[], cap?: number): Wrapped[];
@@ -0,0 +1,100 @@
1
+ /**
2
+ * The commands a part hands to another shell: `sh -c '…'`, `bash -lc "…"`, `cmd /c …`,
3
+ * `powershell -Command …` (or `-EncodedCommand`, or a bare command), `pwsh -c …`, `eval …` and
4
+ * `iex …` (`Invoke-Expression`),
5
+ * found anywhere in the part, so `pnpm exec sh -c "…"` and `env X=1 bash -c …` count too. The
6
+ * limits check each one with the same rules (limits.ts). Plain Node only: the hook runs it.
7
+ */
8
+ import { programName } from './command-parts.js';
9
+ export const POSIX = new Set(['sh', 'bash', 'zsh', 'dash', 'ksh', 'ash', 'fish']);
10
+ export const POWERSHELL = new Set(['powershell', 'pwsh']);
11
+ // PowerShell options that take a value, by their shortest accepted names.
12
+ const VALUED = [
13
+ 'executionpolicy',
14
+ 'ep',
15
+ 'windowstyle',
16
+ 'workingdirectory',
17
+ 'wd',
18
+ 'configurationname',
19
+ 'outputformat',
20
+ 'inputformat',
21
+ 'settingsfile',
22
+ 'custompipename',
23
+ 'psconsolefile',
24
+ 'version',
25
+ ];
26
+ /** `sh`'s script: the first word after its options, when one of them is `-c` (`-lc`, `-ec`…). */
27
+ export function posixScript(words, from) {
28
+ let command = false;
29
+ for (let index = from; index < words.length; index++) {
30
+ const word = words[index];
31
+ if (/^[-+][oO]$/.test(word))
32
+ index++;
33
+ else if (/^-[A-Za-z]*c[A-Za-z]*$/.test(word))
34
+ command = true;
35
+ else if (!/^[-+]/.test(word))
36
+ return command ? word : undefined;
37
+ }
38
+ }
39
+ const decoded = (text) => {
40
+ try {
41
+ return text ? Buffer.from(text, 'base64').toString('utf16le') : undefined;
42
+ }
43
+ catch {
44
+ return undefined;
45
+ }
46
+ };
47
+ /** PowerShell's command: after `-Command` (or any prefix), `-EncodedCommand`, or bare words. */
48
+ export function powershellScript(words, from) {
49
+ for (let index = from; index < words.length; index++) {
50
+ const word = words[index];
51
+ if (!/^[-/]/.test(word))
52
+ return words.slice(index).join(' ');
53
+ const name = word.replace(/^[-/]+/, '').toLowerCase();
54
+ if (!name)
55
+ return words.slice(index + 1).join(' ');
56
+ if ('command'.startsWith(name))
57
+ return words.slice(index + 1).join(' ');
58
+ if (name === 'e' || name === 'ec' || (name.length > 1 && 'encodedcommand'.startsWith(name)))
59
+ return decoded(words[index + 1]);
60
+ if ('file'.startsWith(name))
61
+ return undefined;
62
+ if (VALUED.some((option) => option.startsWith(name) && name.length > 1))
63
+ index++;
64
+ }
65
+ }
66
+ export const EXPRESSIONS = new Set(['iex', 'invoke-expression']);
67
+ /** Every script the part's words hand to another shell; past `cap` of them, one more and stop. */
68
+ export function wrappedScripts(words, cap = Number.POSITIVE_INFINITY) {
69
+ const found = [];
70
+ for (let index = 0; index < words.length && found.length <= cap; index++) {
71
+ const name = programName(words[index]);
72
+ let script;
73
+ let shell = 'bash';
74
+ if (POSIX.has(name))
75
+ script = posixScript(words, index + 1);
76
+ else if (name === 'eval')
77
+ script = words.slice(index + 1).join(' ');
78
+ else if (EXPRESSIONS.has(name)) {
79
+ script = words
80
+ .slice(index + 1)
81
+ .filter((value) => !/^-command$/i.test(value))
82
+ .join(' ');
83
+ shell = 'powershell';
84
+ }
85
+ else if (name === 'cmd') {
86
+ const flag = words.findIndex((value, at) => at > index && /^\/[ck]$/i.test(value));
87
+ if (flag > 0) {
88
+ script = words.slice(flag + 1).join(' ');
89
+ shell = 'cmd';
90
+ }
91
+ }
92
+ else if (POWERSHELL.has(name)) {
93
+ script = powershellScript(words, index + 1);
94
+ shell = 'powershell';
95
+ }
96
+ if (script?.trim())
97
+ found.push({ script, shell });
98
+ }
99
+ return found;
100
+ }
@@ -0,0 +1 @@
1
+ export declare function designDelete(command: string, cwd: string): boolean;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Design turns may delete files inside `design/` and `prototype/` without asking: a single
3
+ * delete command, alone, whose every path stays below one of those folders. The approval record
4
+ * (`prototype/versions`) is never one of them, and neither is either folder as a whole.
5
+ */
6
+ import { isAbsolute, relative, resolve } from 'node:path';
7
+ import { commandParts, partWords, programName } from './command-parts.js';
8
+ const DELETES = new Set(['rm', 'del', 'erase', 'ri', 'remove-item', 'rmdir']);
9
+ const OPTION = /^-(?:[rRfv]+|-recursive|-force|recurse|force)$/i;
10
+ const GLOB = /[*?[\]]/;
11
+ function deletable(word, cwd) {
12
+ if (/[$`{}~%!]/.test(word))
13
+ return false;
14
+ const path = relative(cwd, resolve(cwd, word));
15
+ if (!path || isAbsolute(path))
16
+ return false;
17
+ const parts = path.split(/[\\/]+/);
18
+ const [top, second] = parts;
19
+ if (parts.includes('..') || parts.length < 2 || !top || !second)
20
+ return false;
21
+ if (top !== 'design' && top !== 'prototype')
22
+ return false;
23
+ // The first two names are literal, so a pattern can never reach the approval record.
24
+ if (GLOB.test(top) || GLOB.test(second))
25
+ return false;
26
+ return !(top === 'prototype' && second.toLowerCase() === 'versions');
27
+ }
28
+ export function designDelete(command, cwd) {
29
+ const parts = commandParts(command);
30
+ if (parts.length !== 1)
31
+ return false;
32
+ const [program, ...rest] = partWords(parts[0]);
33
+ if (!program || !DELETES.has(programName(program)) || program !== program.trim())
34
+ return false;
35
+ const paths = rest.filter((word) => !OPTION.test(word));
36
+ if (rest.some((word) => word.startsWith('-') && !OPTION.test(word)))
37
+ return false;
38
+ return paths.length > 0 && paths.every((word) => deletable(word, cwd));
39
+ }
@@ -2,17 +2,19 @@ import type { Chat } from '@buffcore/contracts/workbench';
2
2
  import type { AgentTurn, Start } from '../runner.ts';
3
3
  import { copyCodexExamples } from './codex-examples.ts';
4
4
  import type { TurnMigrations } from './migration-turn.ts';
5
+ import { type TurnGuard } from './turn-guard.ts';
5
6
  import { type Active } from './turn-state.ts';
6
7
  export declare function startAgentTurn(start: Start, spec: Parameters<Start>[0], folder: string, turn: AgentTurn, work: Chat['work']): ReturnType<Start>;
7
- export declare function startCodingTurn(start: Start, spec: Parameters<Start>[0], run: Active, migrations?: TurnMigrations, examples?: ReturnType<typeof copyCodexExamples>): import("child_process").ChildProcess;
8
+ export declare function startCodingTurn(start: Start, spec: Parameters<Start>[0], run: Active, migrations?: TurnMigrations, examples?: ReturnType<typeof copyCodexExamples>, guard?: TurnGuard): import("child_process").ChildProcess;
8
9
  /** Resolve flags, the start record and stdin from the same per-launch settings. */
9
- export declare function launchInput(run: Active, root: string, context: string): {
10
+ export declare function launchInput(run: Active, root: string, context: string, save: () => void): {
10
11
  adapter: import("./prompt.ts").ChatAdapter;
11
12
  input: string;
12
13
  examples: {
13
14
  folder: string;
14
15
  close: () => void;
15
16
  } | undefined;
17
+ guard: TurnGuard | undefined;
16
18
  spec: {
17
19
  program: 'pnpm';
18
20
  args: string[];
@@ -1,3 +1,4 @@
1
+ import { effectiveLevel } from '../../agents-file.js';
1
2
  import { migrationTurnEnv } from '../../migrate-create-protocol.js';
2
3
  import { stdinInput } from './attachments.js';
3
4
  import { startWithClaudeTaskOutput } from './claude-task-output.js';
@@ -7,6 +8,7 @@ import { scrubLaunchIds } from './launch-ids.js';
7
8
  import { inTree } from './permissions.js';
8
9
  import { buildEnv, chatError, composePrompt, t } from './prompt.js';
9
10
  import { message } from './store.js';
11
+ import { openTurnGuard } from './turn-guard.js';
10
12
  import { adapterFor, turnArgs } from './turn-input.js';
11
13
  import { turnContext } from './turn-state.js';
12
14
  import { writesInChange } from './worktree.js';
@@ -15,7 +17,7 @@ export function startAgentTurn(start, spec, folder, turn, work) {
15
17
  ? startWithClaudeTaskOutput(start, spec, folder, turn)
16
18
  : start(spec, folder, turn);
17
19
  }
18
- export function startCodingTurn(start, spec, run, migrations, examples) {
20
+ export function startCodingTurn(start, spec, run, migrations, examples, guard) {
19
21
  let grant;
20
22
  try {
21
23
  grant = writesInChange(run.chat)
@@ -29,16 +31,20 @@ export function startCodingTurn(start, spec, run, migrations, examples) {
29
31
  child.once(event, grant.close);
30
32
  if (examples)
31
33
  child.once('close', examples.close);
34
+ if (guard)
35
+ for (const event of ['error', 'close'])
36
+ child.once(event, guard.close);
32
37
  return child;
33
38
  }
34
39
  catch (error) {
35
40
  grant?.close();
36
41
  examples?.close();
42
+ guard?.close();
37
43
  throw error;
38
44
  }
39
45
  }
40
46
  /** Resolve flags, the start record and stdin from the same per-launch settings. */
41
- export function launchInput(run, root, context) {
47
+ export function launchInput(run, root, context, save) {
42
48
  const adapter = adapterFor(run.chat.agent);
43
49
  if (!adapter)
44
50
  throw chatError('chat.unavailable', { agent: run.chat.agent });
@@ -46,8 +52,12 @@ export function launchInput(run, root, context) {
46
52
  if (run.images.length && !def?.turn?.images)
47
53
  message(run.chat, 'system', t('chat.imagesUnavailable', { agent: adapter.id }));
48
54
  const examples = adapter.id === 'codex' && inTree(run.chat.work) ? copyCodexExamples(root) : undefined;
55
+ let guard;
49
56
  try {
50
- const args = turnArgs(run, root, adapter, examples?.folder);
57
+ guard = adapter.id === 'claude' ? openTurnGuard(run, root, save) : undefined;
58
+ // One read of the level serves the arguments and the settings alike.
59
+ const level = guard?.level ?? effectiveLevel(root);
60
+ const args = turnArgs(run, root, adapter, examples?.folder, guard, level);
51
61
  const input = stdinInput(adapter.id, scrubLaunchIds(context) +
52
62
  (run.selection?.instructions ?? '') +
53
63
  composePrompt(run.chat, root, run.retryNotice ? run.accepted : undefined), run.images);
@@ -58,6 +68,7 @@ export function launchInput(run, root, context) {
58
68
  root,
59
69
  folder: run.tree,
60
70
  contained: run.contained,
71
+ level,
61
72
  });
62
73
  if (examples)
63
74
  env = { ...env, ...codexExampleEnv(run.tree, examples.folder) };
@@ -65,6 +76,7 @@ export function launchInput(run, root, context) {
65
76
  adapter,
66
77
  input,
67
78
  examples,
79
+ guard,
68
80
  spec: {
69
81
  program: 'pnpm',
70
82
  args: ['exec', def?.bin, ...args],
@@ -75,6 +87,7 @@ export function launchInput(run, root, context) {
75
87
  }
76
88
  catch (error) {
77
89
  examples?.close();
90
+ guard?.close();
78
91
  throw error;
79
92
  }
80
93
  }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The limit a command's text mentions, as the card names it (`git push`, `ape deploy`, `.ssh`,
3
+ * `.env.production`, `curl`); nothing when it mentions none.
4
+ */
5
+ export declare function mentionedLimit(command: string, cwd: string): string | undefined;