@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,74 @@
1
+ /**
2
+ * The limits' fail-safe: a command's whole text, read for every limit's words whatever the shape
3
+ * around them. When the parse (limits.ts) refused nothing but the text still mentions a limit,
4
+ * the call goes to the person's card for Allow once, so a shape the parse misses costs one click,
5
+ * not a hole. Case does not count; line continuations are joined first, and the text is also read
6
+ * with its quotes and escapes taken out (`'gi't pu\sh`). Plain Node only: the hook runs it.
7
+ */
8
+ import { SECRET_FOLDERS } from './secret-folders.js';
9
+ const escaped = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
10
+ /** A word on its own, not inside a longer word or file name. */
11
+ const word = (pattern, after = '(?![\\w-])') => new RegExp(`(?<![\\w.-])(?:${pattern})${after}`, 'gi');
12
+ const GIT = /(?<![\w.])git(?!\w)/i;
13
+ const PUSH = /(?<![\w.])(?:push|send-pack)(?!\w)/i;
14
+ const APE = word('ape');
15
+ const DEPLOY = word('deploy');
16
+ const PRODUCTION = /(?<![\w-])\.env\.(?:production|prod|staging)(?![\w-])[\w.-]*/i;
17
+ const SECRETS = word([...SECRET_FOLDERS].map(escaped).join('|'), '(?![\\w.-])');
18
+ const DOWNLOAD_NAMES = [
19
+ 'curl',
20
+ 'wget2?',
21
+ 'aria2c',
22
+ 'iwr',
23
+ 'irm',
24
+ 'invoke-webrequest',
25
+ 'invoke-restmethod',
26
+ 'start-bitstransfer',
27
+ 'bitsadmin',
28
+ 'net\\.webclient',
29
+ 'downloadstring',
30
+ 'downloadfile',
31
+ ];
32
+ const DOWNLOADS = word(`(?:${DOWNLOAD_NAMES.join('|')})(?:\\.exe)?`, '(?![\\w.-])');
33
+ const CERTUTIL = /(?<![\w.-])certutil(?:\.exe)?(?![\w-])/i;
34
+ const URLCACHE = /[-/]urlcache(?![\w-])/i;
35
+ const first = (pattern, text) => {
36
+ pattern.lastIndex = 0;
37
+ return pattern.exec(text)?.[0];
38
+ };
39
+ /** The text with the turn's own folder taken out, so a product kept under `secrets/` still works. */
40
+ function withoutFolder(text, cwd) {
41
+ const parts = cwd
42
+ .split(/[\\/]+/)
43
+ .filter(Boolean)
44
+ .map(escaped);
45
+ if (!parts.length)
46
+ return text;
47
+ return text.replace(new RegExp(parts.join('[\\\\/]+'), 'gi'), '.');
48
+ }
49
+ function mentionIn(text, cwd) {
50
+ // `git stash push` keeps work locally; any other `push` in the text still counts.
51
+ if (GIT.test(text) && PUSH.test(text.replace(/\bstash\s+push\b/gi, '')))
52
+ return 'git push';
53
+ if (first(APE, text) && first(DEPLOY, text))
54
+ return 'ape deploy';
55
+ const settings = PRODUCTION.exec(text)?.[0];
56
+ if (settings)
57
+ return settings;
58
+ const secret = first(SECRETS, withoutFolder(text, cwd))?.toLowerCase();
59
+ if (secret)
60
+ return secret;
61
+ // `certutil -urlcache` fetches a URL; its other uses read local files.
62
+ if (CERTUTIL.test(text) && URLCACHE.test(text))
63
+ return 'certutil';
64
+ return first(DOWNLOADS, text)?.toLowerCase();
65
+ }
66
+ /**
67
+ * The limit a command's text mentions, as the card names it (`git push`, `ape deploy`, `.ssh`,
68
+ * `.env.production`, `curl`); nothing when it mentions none.
69
+ */
70
+ export function mentionedLimit(command, cwd) {
71
+ const joined = command.replace(/[\\`^]\r?\n/g, '');
72
+ const plain = joined.replace(/['"`^]/g, '').replace(/\\(?=[\w.])/g, '');
73
+ return (mentionIn(joined, cwd) ?? mentionIn(plain, cwd))?.slice(0, 80);
74
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The limits' read of shell patterns: a word with `*`, `?`, `[…]` or `{a,b}` names whatever files
3
+ * the shell finds, so `cat .env.*` or `cat ~/.ss?/id_rsa` reaches a limit no word spells out. Each
4
+ * such word's braces are expanded (bounded), each path segment becomes a pattern, and the pattern
5
+ * is tried against every secret folder's name and production settings file's name; a match sends
6
+ * the call to the card (limits.ts). A segment of wildcards only (`*`, `**`, `[a-z]?`) names every
7
+ * file and tells nothing, so `dist/*` and `src/**` stay free. Plain Node only: the hook runs it.
8
+ */
9
+ import type { Reading } from './command-parts.ts';
10
+ /**
11
+ * The secret folder or settings file a pattern in the command could reach (`.ssh`,
12
+ * `.env.production`); nothing when none does. Its work counts in the reading's steps; past the
13
+ * cap, or a word whose braces expand too far, it marks the reading `budget` and names nothing.
14
+ */
15
+ export declare function patternReach(command: string, reading: Reading, cap: number): string | undefined;
@@ -0,0 +1,182 @@
1
+ /**
2
+ * The limits' read of shell patterns: a word with `*`, `?`, `[…]` or `{a,b}` names whatever files
3
+ * the shell finds, so `cat .env.*` or `cat ~/.ss?/id_rsa` reaches a limit no word spells out. Each
4
+ * such word's braces are expanded (bounded), each path segment becomes a pattern, and the pattern
5
+ * is tried against every secret folder's name and production settings file's name; a match sends
6
+ * the call to the card (limits.ts). A segment of wildcards only (`*`, `**`, `[a-z]?`) names every
7
+ * file and tells nothing, so `dist/*` and `src/**` stay free. Plain Node only: the hook runs it.
8
+ */
9
+ import { SECRET_FOLDERS } from './secret-folders.js';
10
+ /** The settings files the limits refuse (limits.ts), as names a pattern is tried against. */
11
+ const SETTINGS = ['.env.production', '.env.prod', '.env.staging'].flatMap((name) => [
12
+ name,
13
+ `${name}.local`,
14
+ ]);
15
+ const NAMES = [...SETTINGS, ...SECRET_FOLDERS];
16
+ const SETTINGS_NAME = /^\.env\.(?:production|prod|staging)(?:\.[\w.-]*)?$/i;
17
+ /** The most words one word's braces may expand to, and how deep braces may nest. */
18
+ const EXPANSIONS = 256;
19
+ const NESTING = 16;
20
+ class Over extends Error {
21
+ }
22
+ /** Each `{` and `}` matched by nesting; an unmatched one is a plain character. */
23
+ function bracePairs(word) {
24
+ const pairs = new Map();
25
+ const open = [];
26
+ for (let index = 0; index < word.length; index++) {
27
+ if (word[index] === '{')
28
+ open.push(index);
29
+ else if (word[index] === '}' && open.length)
30
+ pairs.set(open.pop(), index);
31
+ }
32
+ return pairs;
33
+ }
34
+ /** `word[from, to)` as literal pieces and brace groups (`{a,b}`, split at its own commas). */
35
+ function sequence(word, from, to, pairs, depth = 0) {
36
+ if (depth > NESTING)
37
+ throw new Over();
38
+ const seq = [];
39
+ let text = '';
40
+ for (let index = from; index < to; index++) {
41
+ const close = word[index] === '{' ? pairs.get(index) : undefined;
42
+ const commas = close === undefined ? [] : topCommas(word, index + 1, close, pairs);
43
+ if (close === undefined || !commas.length) {
44
+ text += word[index];
45
+ continue;
46
+ }
47
+ if (text)
48
+ seq.push(text);
49
+ text = '';
50
+ const bounds = [index, ...commas, close];
51
+ seq.push(bounds
52
+ .slice(1)
53
+ .map((end, at) => sequence(word, bounds[at] + 1, end, pairs, depth + 1)));
54
+ index = close;
55
+ }
56
+ if (text)
57
+ seq.push(text);
58
+ return seq;
59
+ }
60
+ function topCommas(word, from, to, pairs) {
61
+ const commas = [];
62
+ for (let index = from; index < to; index++) {
63
+ const close = word[index] === '{' ? pairs.get(index) : undefined;
64
+ if (close !== undefined && close < to)
65
+ index = close;
66
+ else if (word[index] === ',')
67
+ commas.push(index);
68
+ }
69
+ return commas;
70
+ }
71
+ /** How many words a sequence expands to, stopping counting past the cap. */
72
+ const count = (seq) => seq.reduce((total, part) => typeof part === 'string'
73
+ ? total
74
+ : Math.min(EXPANSIONS + 1, total * part.reduce((sum, alt) => sum + count(alt), 0)), 1);
75
+ const expand = (seq) => seq.reduce((words, part) => typeof part === 'string'
76
+ ? words.map((word) => word + part)
77
+ : words.flatMap((word) => part.flatMap(expand).map((alt) => word + alt)), ['']);
78
+ /** A bracket class from `[` at `from`: its regular expression and where it ends, or nothing. */
79
+ function bracketClass(text, from) {
80
+ let index = from + 1;
81
+ const negated = text[index] === '!' || text[index] === '^';
82
+ if (negated)
83
+ index++;
84
+ const start = index;
85
+ if (text[index] === ']')
86
+ index++;
87
+ let named = false;
88
+ while (index < text.length && text[index] !== ']') {
89
+ const end = text.startsWith('[:', index) ? text.indexOf(':]', index + 2) : -1;
90
+ if (end >= 0)
91
+ named = true;
92
+ index = end >= 0 ? end + 2 : index + 1;
93
+ }
94
+ if (index >= text.length)
95
+ return;
96
+ // A named class (`[[:alpha:]]`) is read as any character: wider, never narrower.
97
+ const body = text.slice(start, index).replace(/[\\[\]^]/g, '\\$&');
98
+ return { source: named ? '.' : `[${negated ? '^' : ''}${body}]`, end: index };
99
+ }
100
+ /** A segment as a regular expression when it holds a wildcard and at least one plain character. */
101
+ function segmentPattern(segment) {
102
+ let source = '';
103
+ let plain = false;
104
+ let wild = false;
105
+ for (let index = 0; index < segment.length; index++) {
106
+ const char = segment[index];
107
+ const found = char === '[' ? bracketClass(segment, index) : undefined;
108
+ if (char === '*' || char === '?') {
109
+ wild = true;
110
+ source += char === '*' ? '.*' : '.';
111
+ }
112
+ else if (found) {
113
+ wild = true;
114
+ source += found.source;
115
+ index = found.end;
116
+ }
117
+ else {
118
+ plain = true;
119
+ source += char.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
120
+ }
121
+ }
122
+ if (!wild || !plain)
123
+ return;
124
+ try {
125
+ return new RegExp(`^${source}$`, 'i');
126
+ }
127
+ catch {
128
+ return;
129
+ }
130
+ }
131
+ /** The limit name one expanded word's path segments could reach. */
132
+ function wordReach(word, braced) {
133
+ for (const segment of word.split(/[\\/]+/)) {
134
+ const pattern = segmentPattern(segment);
135
+ if (pattern) {
136
+ const name = NAMES.find((candidate) => pattern.test(candidate));
137
+ if (name)
138
+ return name;
139
+ }
140
+ else if (braced) {
141
+ // `.env.{production,x}` spells a limit out only once its braces are expanded.
142
+ if (SETTINGS_NAME.test(segment))
143
+ return segment.toLowerCase();
144
+ if (SECRET_FOLDERS.has(segment.toLowerCase()))
145
+ return segment.toLowerCase();
146
+ }
147
+ }
148
+ }
149
+ const GLOB = /[*?[]|\{[^}]*,/;
150
+ const words = (text) => text.split(/[\s;|&()<>=`"']+/).filter((word) => GLOB.test(word));
151
+ /**
152
+ * The secret folder or settings file a pattern in the command could reach (`.ssh`,
153
+ * `.env.production`); nothing when none does. Its work counts in the reading's steps; past the
154
+ * cap, or a word whose braces expand too far, it marks the reading `budget` and names nothing.
155
+ */
156
+ export function patternReach(command, reading, cap) {
157
+ const joined = command.replace(/[\\`^]\r?\n/g, '');
158
+ const plain = joined.replace(/['"`^]/g, '');
159
+ for (const word of new Set([...words(joined), ...words(plain)])) {
160
+ try {
161
+ const pairs = bracePairs(word);
162
+ const seq = sequence(word, 0, word.length, pairs);
163
+ if (count(seq) > EXPANSIONS)
164
+ throw new Over();
165
+ const braced = seq.some((part) => typeof part !== 'string');
166
+ for (const each of braced ? expand(seq) : [word]) {
167
+ reading.steps += each.length;
168
+ if (reading.steps > cap)
169
+ throw new Over();
170
+ const name = wordReach(each, braced);
171
+ if (name)
172
+ return name;
173
+ }
174
+ }
175
+ catch (error) {
176
+ if (!(error instanceof Over))
177
+ throw error;
178
+ reading.unsure = 'budget';
179
+ return;
180
+ }
181
+ }
182
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The studio's PreToolUse hook, installed for each Claude turn through `--settings`: it reads the
3
+ * call from stdin and answers with Claude Code's hook output. Run by plain Node, as
4
+ * `node limits-hook.ts <work>`; the decision is `hookAnswer` in limits.ts. Anything unexpected
5
+ * refuses the call (exit 2, the reason on stderr): an exit 1 would let it run.
6
+ */
7
+ import { readFileSync } from 'node:fs';
8
+ try {
9
+ const { hookAnswer } = await import('./limits.js');
10
+ const answer = hookAnswer(readFileSync(0, 'utf8'), process.argv[2] ?? '');
11
+ if (answer)
12
+ process.stdout.write(JSON.stringify({
13
+ hookSpecificOutput: {
14
+ hookEventName: 'PreToolUse',
15
+ permissionDecision: answer.decision,
16
+ ...(answer.reason ? { permissionDecisionReason: answer.reason } : {}),
17
+ },
18
+ }));
19
+ }
20
+ catch {
21
+ let reason = '';
22
+ try {
23
+ reason = (await import('./limits.js')).limitText('limits.unreadable');
24
+ }
25
+ catch {
26
+ reason = (await import('./limits.en.js').catch(() => undefined))?.en['limits.unreadable'] ?? '';
27
+ }
28
+ process.stderr.write(reason);
29
+ process.exitCode = 2;
30
+ }
@@ -0,0 +1,37 @@
1
+ import { type Unsure } from './command-parts.ts';
2
+ import { en } from './limits.en.ts';
3
+ export type LimitKind = 'secrets' | 'deploy' | 'push' | 'settings' | 'download' | 'unsure';
4
+ export interface Limit {
5
+ /** `ask` sends the call to the person's Allow card; `deny` refuses it at every level. */
6
+ decision: 'deny' | 'ask';
7
+ kind: LimitKind;
8
+ /** The refused part, as the agent wrote it. */
9
+ part: string;
10
+ /**
11
+ * Why an `unsure` call asks: its text mentions a limit, a pattern in it could reach one, or the
12
+ * reading could not follow it.
13
+ */
14
+ unsure?: Unsure | 'mention' | 'pattern';
15
+ /** The limit an `unsure` call's text mentions or a pattern could reach (`git push`, `.ssh`). */
16
+ mention?: string;
17
+ }
18
+ /**
19
+ * A path that names a folder of sign-ins or keys. Only the part below the folder it shares with
20
+ * the turn's folder counts, so a product kept under such a folder still works.
21
+ */
22
+ export declare function secretPath(path: string, cwd: string): boolean;
23
+ /** The first limit a tool call crosses, refusals before asks; nothing when it crosses none. */
24
+ export declare function crossedLimit(tool: string, input: unknown, cwd: string): Limit | undefined;
25
+ /** The agent's words in the turn's language (LANG), with `{name}` values filled in. */
26
+ export declare function limitText(key: keyof typeof en, values?: Record<string, string>): string;
27
+ /** The sentence the agent reads for a crossed limit: it names the refused part. */
28
+ export declare const limitReason: (limit: Limit) => string;
29
+ /**
30
+ * The hook's answer to one call: a crossed limit, a design turn's delete inside `design/` or
31
+ * `prototype/` (allowed), or nothing. A call it cannot read is refused, so a broken call never
32
+ * slips past the limits.
33
+ */
34
+ export declare function hookAnswer(text: string, work: string): {
35
+ decision: 'allow' | 'deny' | 'ask';
36
+ reason: string;
37
+ } | undefined;
@@ -0,0 +1,18 @@
1
+ /** What the agent reads when a call crosses a limit or a person refuses it. */
2
+ export declare const en: {
3
+ readonly 'limits.secrets': '`{part}` is not allowed here: it reaches a folder of sign-ins or keys. Work without it.';
4
+ readonly 'limits.deploy': '`{part}` is not allowed here: only a person deploys, from the studio.';
5
+ readonly 'limits.push': '`{part}` is not allowed here: the studio saves and shares the work, not the agent.';
6
+ readonly 'limits.settings': '`{part}` is not allowed here: production settings stay with a person.';
7
+ readonly 'limits.download': '`{part}` downloads from the network: the person must allow it, and did not.';
8
+ readonly 'limits.unsure.mention': 'This command mentions `{mention}`, and the studio couldn’t tell if it runs it: the person must allow it, and did not.';
9
+ readonly 'limits.unsure.pattern': 'This command uses a pattern that could reach `{mention}`; the studio couldn’t tell: the person must allow it, and did not. Name the files you need.';
10
+ readonly 'limits.unsure.budget': 'This command is too long or too deeply nested for the studio to read: the person must allow it, and did not. Run shorter commands, one at a time.';
11
+ readonly 'limits.unsure.heredoc': 'A here-document in this command never reaches its end line, so the studio couldn’t tell what runs after it: the person must allow it, and did not.';
12
+ readonly 'limits.unsure.stdin': 'This command hands a shell or interpreter its script on standard input, which the studio can’t read: the person must allow it, and did not. Run the command itself.';
13
+ readonly 'limits.unsure.expression': '`Invoke-Expression` (`iex`) runs text as a command, so the studio couldn’t tell what it runs: the person must allow it, and did not.';
14
+ readonly 'limits.outside': '`{parts}` is not allowed here; run `{allowed}` alone.';
15
+ readonly 'limits.refused': 'The person did not allow `{parts}`. Do the work another way, or ask them.';
16
+ readonly 'limits.unreadable': 'The studio could not read this call, so it was not run.';
17
+ readonly 'limits.ended': 'Nobody answered in time, so `{parts}` was not run.';
18
+ };
@@ -0,0 +1,18 @@
1
+ /** What the agent reads when a call crosses a limit or a person refuses it. */
2
+ export const en = {
3
+ 'limits.secrets': '`{part}` is not allowed here: it reaches a folder of sign-ins or keys. Work without it.',
4
+ 'limits.deploy': '`{part}` is not allowed here: only a person deploys, from the studio.',
5
+ 'limits.push': '`{part}` is not allowed here: the studio saves and shares the work, not the agent.',
6
+ 'limits.settings': '`{part}` is not allowed here: production settings stay with a person.',
7
+ 'limits.download': '`{part}` downloads from the network: the person must allow it, and did not.',
8
+ 'limits.unsure.mention': 'This command mentions `{mention}`, and the studio couldn’t tell if it runs it: the person must allow it, and did not.',
9
+ 'limits.unsure.pattern': 'This command uses a pattern that could reach `{mention}`; the studio couldn’t tell: the person must allow it, and did not. Name the files you need.',
10
+ 'limits.unsure.budget': 'This command is too long or too deeply nested for the studio to read: the person must allow it, and did not. Run shorter commands, one at a time.',
11
+ 'limits.unsure.heredoc': 'A here-document in this command never reaches its end line, so the studio couldn’t tell what runs after it: the person must allow it, and did not.',
12
+ 'limits.unsure.stdin': 'This command hands a shell or interpreter its script on standard input, which the studio can’t read: the person must allow it, and did not. Run the command itself.',
13
+ 'limits.unsure.expression': '`Invoke-Expression` (`iex`) runs text as a command, so the studio couldn’t tell what it runs: the person must allow it, and did not.',
14
+ 'limits.outside': '`{parts}` is not allowed here; run `{allowed}` alone.',
15
+ 'limits.refused': 'The person did not allow `{parts}`. Do the work another way, or ask them.',
16
+ 'limits.unreadable': 'The studio could not read this call, so it was not run.',
17
+ 'limits.ended': 'Nobody answered in time, so `{parts}` was not run.',
18
+ };
@@ -0,0 +1,2 @@
1
+ import type { en } from './limits.en.ts';
2
+ export declare const es: Record<keyof typeof en, string>;
@@ -0,0 +1,17 @@
1
+ export const es = {
2
+ 'limits.secrets': '`{part}` no está permitido aquí: llega a una carpeta de accesos o claves. Trabaja sin ella.',
3
+ 'limits.deploy': '`{part}` no está permitido aquí: solo una persona publica, desde el estudio.',
4
+ 'limits.push': '`{part}` no está permitido aquí: el estudio guarda y comparte el trabajo, no el agente.',
5
+ 'limits.settings': '`{part}` no está permitido aquí: los ajustes de producción quedan en manos de una persona.',
6
+ 'limits.download': '`{part}` descarga de la red: la persona debe permitirlo, y no lo hizo.',
7
+ 'limits.unsure.mention': 'Este comando menciona `{mention}` y el estudio no pudo saber si lo ejecuta: la persona debe permitirlo, y no lo hizo.',
8
+ 'limits.unsure.pattern': 'Este comando usa un patrón que podría llegar a `{mention}`; el estudio no pudo saberlo: la persona debe permitirlo, y no lo hizo. Nombra los archivos que necesitas.',
9
+ 'limits.unsure.budget': 'Este comando es demasiado largo o anidado para que el estudio lo lea: la persona debe permitirlo, y no lo hizo. Ejecuta comandos más cortos, de uno en uno.',
10
+ 'limits.unsure.heredoc': 'Un here-document de este comando nunca llega a su línea final, así que el estudio no pudo saber qué se ejecuta después: la persona debe permitirlo, y no lo hizo.',
11
+ 'limits.unsure.stdin': 'Este comando pasa a un shell o intérprete su script por la entrada estándar, que el estudio no puede leer: la persona debe permitirlo, y no lo hizo. Ejecuta el comando directamente.',
12
+ 'limits.unsure.expression': '`Invoke-Expression` (`iex`) ejecuta texto como comando, así que el estudio no pudo saber qué ejecuta: la persona debe permitirlo, y no lo hizo.',
13
+ 'limits.outside': '`{parts}` no está permitido aquí; ejecuta `{allowed}` solo.',
14
+ 'limits.refused': 'La persona no permitió `{parts}`. Haz el trabajo de otra forma, o pregúntale.',
15
+ 'limits.unreadable': 'El estudio no pudo leer esta llamada, así que no se ejecutó.',
16
+ 'limits.ended': 'Nadie respondió a tiempo, así que `{parts}` no se ejecutó.',
17
+ };
@@ -0,0 +1,224 @@
1
+ /**
2
+ * The limits every turn keeps at every level, "Never ask" included: no reading folders of sign-ins
3
+ * or keys, no deploy, no production settings, no `git push`, and no network download without a
4
+ * person's click. The studio's hook (limits-hook.ts) runs this before each tool call; the
5
+ * permission mode never decides them. Plain Node only: the hook runs it without the CLI.
6
+ */
7
+ import { relative, resolve } from 'node:path';
8
+ import { commandParts, ESCAPE, partWords, programName, } from './command-parts.js';
9
+ import { EXPRESSIONS, wrappedScripts } from './command-wrappers.js';
10
+ import { designDelete } from './design-deletes.js';
11
+ import { mentionedLimit } from './limit-mentions.js';
12
+ import { patternReach } from './limit-patterns.js';
13
+ import { en } from './limits.en.js';
14
+ import { es } from './limits.es.js';
15
+ import { holdsSecrets } from './secret-folders.js';
16
+ import { readsStdin } from './stdin-shells.js';
17
+ const SHELLS = new Map([
18
+ ['Bash', 'bash'],
19
+ ['PowerShell', 'powershell'],
20
+ ]);
21
+ const DOWNLOADS = /(?:^|[\s"'`=\\/])(?:curl|wget2?|aria2c|iwr|irm|invoke-webrequest|invoke-restmethod|start-bitstransfer)(?:\.exe)?(?=$|[\s"'`])/i;
22
+ const PRODUCTION = /(?:^|[\\/\s"'=])\.env\.(?:production|prod|staging)(?:\.[\w.-]*)?(?=$|[\s"'<>])/i;
23
+ /** How deep a shell inside a shell is still read: deeper ones are not ordinary commands. */
24
+ const DEPTH = 4;
25
+ /**
26
+ * The reading's work budget, so the hook always answers well inside its 30 second timeout (a hook
27
+ * that times out lets the call run): the longest command read, the characters parsed in all, the
28
+ * time, and the scripts one part may hand on. Past any of them the person is asked.
29
+ */
30
+ const TEXT_CAP = 20_000;
31
+ const STEP_CAP = 200_000;
32
+ const TIME_CAP = 50;
33
+ const WRAPPED_CAP = 32;
34
+ // Grep's pattern is a search, not a path; Glob's is a path.
35
+ const fileFields = (tool) => [
36
+ 'file_path',
37
+ 'path',
38
+ 'notebook_path',
39
+ 'glob',
40
+ ...(tool === 'Glob' ? ['pattern'] : []),
41
+ ];
42
+ /**
43
+ * A path that names a folder of sign-ins or keys. Only the part below the folder it shares with
44
+ * the turn's folder counts, so a product kept under such a folder still works.
45
+ */
46
+ export function secretPath(path, cwd) {
47
+ const home = process.env.HOME || process.env.USERPROFILE || '';
48
+ const expanded = path.replace(/^~(?=$|[\\/])/, home);
49
+ // Below the common folder only: from a sibling, `../../.ssh` still names `.ssh`.
50
+ return holdsSecrets(relative(cwd, resolve(cwd, expanded)));
51
+ }
52
+ /** A word worth reading as a path: it has a separator, or starts like a hidden or home folder. */
53
+ const pathLike = (word) => /[\\/]/.test(word) || /^[.~]/.test(word);
54
+ // Git's options before the subcommand that take the next word as their value.
55
+ const VALUED = new Set([
56
+ '-C',
57
+ '-c',
58
+ '--git-dir',
59
+ '--work-tree',
60
+ '--namespace',
61
+ '--super-prefix',
62
+ '--config-env',
63
+ '--attr-source',
64
+ ]);
65
+ /** The first word after `from` that is not an option (or an option's value), and its index. */
66
+ function subcommandAt(words, from) {
67
+ for (let index = from; index < words.length; index++) {
68
+ const word = words[index];
69
+ if (VALUED.has(word))
70
+ index++;
71
+ else if (!word.startsWith('-'))
72
+ return { name: word.toLowerCase(), at: index };
73
+ }
74
+ }
75
+ const subcommand = (words, from) => subcommandAt(words, from)?.name;
76
+ /** A commit's or tag's message is words, not a command or a path: `git commit -m "…"`. */
77
+ function withoutMessages(words) {
78
+ const git = words.findIndex((word) => programName(word) === 'git');
79
+ const sub = git < 0 ? undefined : subcommandAt(words, git + 1);
80
+ if (sub?.name !== 'commit' && sub?.name !== 'tag')
81
+ return words;
82
+ const at = sub.at;
83
+ return words.filter((_, index) => index <= at || !/^(?:-[A-Za-z]*m|--message)$/.test(words[index - 1] ?? ''));
84
+ }
85
+ function wordsLimit(words, cwd) {
86
+ const names = words.map(programName);
87
+ const checked = withoutMessages(words);
88
+ if (checked.some((word) => pathLike(word) && secretPath(word, cwd)))
89
+ return { decision: 'deny', kind: 'secrets' };
90
+ if (checked.some((word) => PRODUCTION.test(word)))
91
+ return { decision: 'deny', kind: 'settings' };
92
+ const git = names.indexOf('git');
93
+ // `git send-pack` is the plumbing under `git push`.
94
+ const sub = git >= 0 ? subcommand(words, git + 1) : undefined;
95
+ if (sub === 'push' || sub === 'send-pack')
96
+ return { decision: 'deny', kind: 'push' };
97
+ const ape = names.indexOf('ape');
98
+ if (ape >= 0) {
99
+ const next = subcommand(words, ape + 1);
100
+ const server = next === 'server' && subcommand(words, words.indexOf('server', ape) + 1);
101
+ if (next === 'deploy' || server === 'deploy')
102
+ return { decision: 'deny', kind: 'deploy' };
103
+ }
104
+ if (checked.some((word) => DOWNLOADS.test(word)))
105
+ return { decision: 'ask', kind: 'download' };
106
+ // `certutil -urlcache` fetches a URL; its other uses read local files.
107
+ if (names.includes('certutil') && words.some((word) => /^[-/]urlcache$/i.test(word)))
108
+ return { decision: 'ask', kind: 'download' };
109
+ }
110
+ /**
111
+ * One part's limits: its words as written (a Windows path keeps its backslashes) and with the
112
+ * shell's escapes read (`git p\ush` is `git push`), and every script it hands to another shell.
113
+ */
114
+ function partLimits(part, check, shell, depth) {
115
+ const raw = partWords(part);
116
+ const plain = partWords(part, ESCAPE[shell]);
117
+ const own = wordsLimit(raw, check.cwd) ?? wordsLimit(plain, check.cwd);
118
+ if (readsStdin(plain))
119
+ check.unsure ??= 'stdin';
120
+ if (plain.some((word) => EXPRESSIONS.has(programName(word))))
121
+ check.unsure ??= 'expression';
122
+ const wrapped = depth < DEPTH ? wrappedScripts(plain, WRAPPED_CAP) : [];
123
+ if (wrapped.length > WRAPPED_CAP)
124
+ check.unsure = 'budget';
125
+ const inner = wrapped.slice(0, WRAPPED_CAP).flatMap(({ script, shell: inside }) => {
126
+ // The same script handed on twice is read once.
127
+ const key = `${inside}\0${script}`;
128
+ if (check.seen.has(key))
129
+ return [];
130
+ check.seen.add(key);
131
+ return commandLimits(script, check, inside, depth + 1);
132
+ });
133
+ return [...(own ? [{ ...own, part }] : []), ...inner];
134
+ }
135
+ function commandLimits(command, check, shell, depth = 0) {
136
+ if (check.steps > STEP_CAP || Date.now() > check.until) {
137
+ check.unsure = 'budget';
138
+ return [];
139
+ }
140
+ return commandParts(command, shell, check).flatMap((part) => partLimits(part, check, shell, depth));
141
+ }
142
+ /**
143
+ * A shell command's first limit: what the parse refuses, then what it asks; when it found
144
+ * neither, a reading it could not follow, or a limit the text still mentions, asks the person.
145
+ */
146
+ function shellLimit(command, cwd, shell) {
147
+ const check = { cwd, steps: 0, until: Date.now() + TIME_CAP, seen: new Set() };
148
+ if (command.length > TEXT_CAP)
149
+ check.unsure = 'budget';
150
+ else {
151
+ const limits = commandLimits(command, check, shell);
152
+ const found = limits.find((limit) => limit.decision === 'deny') ?? limits[0];
153
+ if (found)
154
+ return found;
155
+ }
156
+ const named = command.length > TEXT_CAP ? undefined : mentionedLimit(command, cwd);
157
+ const reach = named || check.unsure ? undefined : patternReach(command, check, STEP_CAP);
158
+ const mention = named ?? reach;
159
+ const unsure = check.unsure ?? (named ? 'mention' : reach ? 'pattern' : undefined);
160
+ if (!unsure)
161
+ return;
162
+ return {
163
+ decision: 'ask',
164
+ kind: 'unsure',
165
+ part: command,
166
+ unsure,
167
+ ...(mention ? { mention } : {}),
168
+ };
169
+ }
170
+ /** The first limit a tool call crosses, refusals before asks; nothing when it crosses none. */
171
+ export function crossedLimit(tool, input, cwd) {
172
+ const fields = (input ?? {});
173
+ const shell = SHELLS.get(tool);
174
+ if (shell) {
175
+ return shellLimit(typeof fields.command === 'string' ? fields.command : '', cwd, shell);
176
+ }
177
+ for (const field of fileFields(tool)) {
178
+ const value = fields[field];
179
+ if (typeof value !== 'string' || !value)
180
+ continue;
181
+ if (secretPath(value, cwd))
182
+ return { decision: 'deny', kind: 'secrets', part: value };
183
+ if (PRODUCTION.test(value))
184
+ return { decision: 'deny', kind: 'settings', part: value };
185
+ }
186
+ if (tool === 'WebFetch' && typeof fields.url === 'string')
187
+ return { decision: 'ask', kind: 'download', part: fields.url };
188
+ }
189
+ const catalogs = { en, es };
190
+ /** The agent's words in the turn's language (LANG), with `{name}` values filled in. */
191
+ export function limitText(key, values = {}) {
192
+ const locale = (process.env.LC_ALL || process.env.LC_MESSAGES || process.env.LANG || 'en')
193
+ .slice(0, 2)
194
+ .toLowerCase();
195
+ const words = catalogs[locale === 'es' ? 'es' : 'en'];
196
+ return words[key].replace(/\{(\w+)\}/g, (_, name) => values[name] ?? '');
197
+ }
198
+ /** The sentence the agent reads for a crossed limit: it names the refused part. */
199
+ export const limitReason = (limit) => limit.kind === 'unsure'
200
+ ? limitText(`limits.unsure.${limit.unsure ?? 'mention'}`, { mention: limit.mention ?? '' })
201
+ : limitText(`limits.${limit.kind}`, { part: limit.part.slice(0, 200) });
202
+ /**
203
+ * The hook's answer to one call: a crossed limit, a design turn's delete inside `design/` or
204
+ * `prototype/` (allowed), or nothing. A call it cannot read is refused, so a broken call never
205
+ * slips past the limits.
206
+ */
207
+ export function hookAnswer(text, work) {
208
+ let call;
209
+ try {
210
+ call = JSON.parse(text);
211
+ }
212
+ catch {
213
+ return { decision: 'deny', reason: limitText('limits.unreadable') };
214
+ }
215
+ const tool = typeof call.tool_name === 'string' ? call.tool_name : '';
216
+ const cwd = typeof call.cwd === 'string' && call.cwd ? call.cwd : process.cwd();
217
+ const limit = crossedLimit(tool, call.tool_input, cwd);
218
+ if (limit)
219
+ return { decision: limit.decision, reason: limitReason(limit) };
220
+ const command = call.tool_input?.command;
221
+ if (work === 'design' && SHELLS.has(tool) && typeof command === 'string')
222
+ if (designDelete(command, cwd))
223
+ return { decision: 'allow', reason: '' };
224
+ }
@@ -0,0 +1,17 @@
1
+ type Json = Record<string, unknown>;
2
+ export declare const isObject: (value: unknown) => value is Json;
3
+ /** The file is not one the studio may write: not JSON, not an object, or unreadable. */
4
+ export declare class SettingsRefusal extends Error {
5
+ constructor();
6
+ }
7
+ /** `<product>/.claude/settings.local.json`, the product's own settings that git never shares. */
8
+ export declare const localSettingsFile: (product: string) => string;
9
+ /**
10
+ * One change to the product's local settings, one at a time: made when missing, every other
11
+ * setting kept, written whole or not at all. `change` returns the next settings, or nothing to
12
+ * leave the file as it is. Resolves whether the file was written.
13
+ */
14
+ export declare function updateLocalSettings(product: string, change: (settings: Json) => Json | undefined): Promise<boolean>;
15
+ /** Reads the local settings for a launch; a missing or damaged file is empty. */
16
+ export declare function readLocalSettings(product: string): Promise<Json>;
17
+ export {};