@buffcore/cli 0.8.41 → 0.8.43

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 (219) 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.d.ts +3 -2
  9. package/dist/commands/create.en.js +1 -1
  10. package/dist/commands/create.es.js +1 -1
  11. package/dist/commands/create.js +8 -6
  12. package/dist/commands/design/check.d.ts +2 -0
  13. package/dist/commands/design/check.js +6 -0
  14. package/dist/commands/design/journey-roles.d.ts +6 -0
  15. package/dist/commands/design/journey-roles.js +60 -0
  16. package/dist/commands/design/references.d.ts +12 -0
  17. package/dist/commands/design/references.js +44 -0
  18. package/dist/commands/design/tells-text.js +2 -2
  19. package/dist/commands/design/tells.d.ts +3 -1
  20. package/dist/commands/design/tells.en.d.ts +2 -0
  21. package/dist/commands/design/tells.en.js +2 -0
  22. package/dist/commands/design/tells.es.js +2 -0
  23. package/dist/commands/design/tells.js +10 -7
  24. package/dist/commands/dev.js +3 -0
  25. package/dist/commands/extension/places-approval.d.ts +1 -1
  26. package/dist/commands/port-claims.d.ts +23 -0
  27. package/dist/commands/port-claims.js +95 -0
  28. package/dist/commands/prototype/browser.d.ts +9 -4
  29. package/dist/commands/prototype/browser.js +26 -18
  30. package/dist/commands/prototype/comp-record.d.ts +27 -0
  31. package/dist/commands/prototype/comp-record.js +85 -0
  32. package/dist/commands/prototype/comp-words.d.ts +12 -0
  33. package/dist/commands/prototype/comp-words.js +24 -0
  34. package/dist/commands/prototype/comp.d.ts +26 -0
  35. package/dist/commands/prototype/comp.js +71 -0
  36. package/dist/commands/prototype/export-base.d.ts +27 -0
  37. package/dist/commands/prototype/export-base.js +80 -0
  38. package/dist/commands/prototype/export.d.ts +3 -2
  39. package/dist/commands/prototype/export.js +13 -6
  40. package/dist/commands/prototype/index.js +1 -0
  41. package/dist/commands/prototype/media-ready.d.ts +8 -0
  42. package/dist/commands/prototype/media-ready.js +36 -0
  43. package/dist/commands/prototype/messages.en.d.ts +6 -0
  44. package/dist/commands/prototype/messages.en.js +6 -0
  45. package/dist/commands/prototype/messages.js +6 -0
  46. package/dist/commands/prototype/milestone-versions.d.ts +1 -1
  47. package/dist/commands/prototype/record-look-child.js +4 -1
  48. package/dist/commands/prototype/scene.d.ts +3 -1
  49. package/dist/commands/prototype/scene.js +22 -6
  50. package/dist/commands/prototype/snapshot-command.js +1 -1
  51. package/dist/commands/prototype/snapshot-inputs.js +35 -7
  52. package/dist/commands/prototype/snapshot-setup.d.ts +12 -1
  53. package/dist/commands/prototype/snapshot-setup.js +31 -6
  54. package/dist/commands/prototype/snapshot.d.ts +2 -0
  55. package/dist/commands/prototype/snapshot.js +2 -1
  56. package/dist/commands/prototype/versions.d.ts +3 -3
  57. package/dist/commands/ui/agents-route.d.ts +2 -0
  58. package/dist/commands/ui/agents-route.js +8 -5
  59. package/dist/commands/ui/approval-authority.d.ts +1 -1
  60. package/dist/commands/ui/approve.js +7 -1
  61. package/dist/commands/ui/chat/allow-folder.js +23 -77
  62. package/dist/commands/ui/chat/claude-permissions.d.ts +21 -0
  63. package/dist/commands/ui/chat/claude-permissions.js +69 -0
  64. package/dist/commands/ui/chat/codex-idle.d.ts +2 -0
  65. package/dist/commands/ui/chat/codex-overrides.js +2 -1
  66. package/dist/commands/ui/chat/codex-permissions.d.ts +12 -0
  67. package/dist/commands/ui/chat/codex-permissions.js +97 -0
  68. package/dist/commands/ui/chat/command-parts.d.ts +33 -0
  69. package/dist/commands/ui/chat/command-parts.js +219 -0
  70. package/dist/commands/ui/chat/command-wrappers.d.ts +21 -0
  71. package/dist/commands/ui/chat/command-wrappers.js +100 -0
  72. package/dist/commands/ui/chat/design-deletes.d.ts +1 -0
  73. package/dist/commands/ui/chat/design-deletes.js +39 -0
  74. package/dist/commands/ui/chat/fresh-critique.d.ts +28 -0
  75. package/dist/commands/ui/chat/fresh-critique.js +121 -0
  76. package/dist/commands/ui/chat/fresh-launch.d.ts +31 -0
  77. package/dist/commands/ui/chat/fresh-launch.js +122 -0
  78. package/dist/commands/ui/chat/fresh-prompts.d.ts +17 -0
  79. package/dist/commands/ui/chat/fresh-prompts.js +76 -0
  80. package/dist/commands/ui/chat/fresh-report.d.ts +8 -0
  81. package/dist/commands/ui/chat/fresh-report.js +36 -0
  82. package/dist/commands/ui/chat/fresh-routes.d.ts +3 -0
  83. package/dist/commands/ui/chat/fresh-routes.js +64 -0
  84. package/dist/commands/ui/chat/fresh-settings.d.ts +12 -0
  85. package/dist/commands/ui/chat/fresh-settings.js +66 -0
  86. package/dist/commands/ui/chat/fresh-store.d.ts +17 -0
  87. package/dist/commands/ui/chat/fresh-store.js +67 -0
  88. package/dist/commands/ui/chat/fresh.d.ts +31 -0
  89. package/dist/commands/ui/chat/fresh.en.d.ts +7 -0
  90. package/dist/commands/ui/chat/fresh.en.js +7 -0
  91. package/dist/commands/ui/chat/fresh.es.d.ts +7 -0
  92. package/dist/commands/ui/chat/fresh.es.js +7 -0
  93. package/dist/commands/ui/chat/fresh.js +112 -0
  94. package/dist/commands/ui/chat/launch-input.d.ts +4 -2
  95. package/dist/commands/ui/chat/launch-input.js +16 -3
  96. package/dist/commands/ui/chat/limit-mentions.d.ts +5 -0
  97. package/dist/commands/ui/chat/limit-mentions.js +94 -0
  98. package/dist/commands/ui/chat/limit-patterns.d.ts +15 -0
  99. package/dist/commands/ui/chat/limit-patterns.js +182 -0
  100. package/dist/commands/ui/chat/limits-hook.d.ts +1 -0
  101. package/dist/commands/ui/chat/limits-hook.js +30 -0
  102. package/dist/commands/ui/chat/limits.d.ts +44 -0
  103. package/dist/commands/ui/chat/limits.en.d.ts +20 -0
  104. package/dist/commands/ui/chat/limits.en.js +20 -0
  105. package/dist/commands/ui/chat/limits.es.d.ts +2 -0
  106. package/dist/commands/ui/chat/limits.es.js +19 -0
  107. package/dist/commands/ui/chat/limits.js +236 -0
  108. package/dist/commands/ui/chat/local-settings.d.ts +17 -0
  109. package/dist/commands/ui/chat/local-settings.js +73 -0
  110. package/dist/commands/ui/chat/opencode-config.d.ts +22 -0
  111. package/dist/commands/ui/chat/opencode-config.js +83 -0
  112. package/dist/commands/ui/chat/parse-claude.js +25 -5
  113. package/dist/commands/ui/chat/permission-ask.d.ts +44 -0
  114. package/dist/commands/ui/chat/permission-ask.js +145 -0
  115. package/dist/commands/ui/chat/permission-channel.d.ts +18 -0
  116. package/dist/commands/ui/chat/permission-channel.js +67 -0
  117. package/dist/commands/ui/chat/permission-route.d.ts +10 -0
  118. package/dist/commands/ui/chat/permission-route.js +47 -0
  119. package/dist/commands/ui/chat/permission-tool.d.ts +1 -0
  120. package/dist/commands/ui/chat/permission-tool.js +86 -0
  121. package/dist/commands/ui/chat/permissions.d.ts +11 -12
  122. package/dist/commands/ui/chat/permissions.js +20 -102
  123. package/dist/commands/ui/chat/planning-limits.d.ts +12 -0
  124. package/dist/commands/ui/chat/planning-limits.js +46 -0
  125. package/dist/commands/ui/chat/process.js +4 -1
  126. package/dist/commands/ui/chat/prompt.d.ts +8 -1
  127. package/dist/commands/ui/chat/prompt.en.d.ts +4 -0
  128. package/dist/commands/ui/chat/prompt.en.js +4 -0
  129. package/dist/commands/ui/chat/prompt.es.d.ts +4 -0
  130. package/dist/commands/ui/chat/prompt.es.js +4 -0
  131. package/dist/commands/ui/chat/prompt.js +18 -50
  132. package/dist/commands/ui/chat/publish-limits.d.ts +4 -0
  133. package/dist/commands/ui/chat/publish-limits.js +97 -0
  134. package/dist/commands/ui/chat/recorded-turn.js +6 -0
  135. package/dist/commands/ui/chat/routes.js +2 -0
  136. package/dist/commands/ui/chat/secret-folders.d.ts +16 -0
  137. package/dist/commands/ui/chat/secret-folders.js +35 -0
  138. package/dist/commands/ui/chat/stdin-shells.d.ts +8 -0
  139. package/dist/commands/ui/chat/stdin-shells.js +102 -0
  140. package/dist/commands/ui/chat/step-rules.d.ts +13 -0
  141. package/dist/commands/ui/chat/step-rules.js +47 -0
  142. package/dist/commands/ui/chat/store.js +11 -2
  143. package/dist/commands/ui/chat/stream.en.d.ts +6 -0
  144. package/dist/commands/ui/chat/stream.en.js +6 -0
  145. package/dist/commands/ui/chat/stream.es.d.ts +6 -0
  146. package/dist/commands/ui/chat/stream.es.js +6 -0
  147. package/dist/commands/ui/chat/stream.js +8 -2
  148. package/dist/commands/ui/chat/turn-guard.d.ts +27 -0
  149. package/dist/commands/ui/chat/turn-guard.js +86 -0
  150. package/dist/commands/ui/chat/turn-input.d.ts +3 -2
  151. package/dist/commands/ui/chat/turn-input.js +3 -1
  152. package/dist/commands/ui/chat/turn-scenes.d.ts +19 -0
  153. package/dist/commands/ui/chat/turn-scenes.js +44 -0
  154. package/dist/commands/ui/chat/turn-state.d.ts +11 -0
  155. package/dist/commands/ui/chat/turn.js +9 -3
  156. package/dist/commands/ui/comp-approval.d.ts +6 -0
  157. package/dist/commands/ui/comp-approval.js +42 -0
  158. package/dist/commands/ui/host.js +3 -0
  159. package/dist/commands/ui/index.js +18 -6
  160. package/dist/commands/ui/level-route.js +8 -3
  161. package/dist/commands/ui/loop-approve.d.ts +4 -4
  162. package/dist/commands/ui/page.d.ts +19 -1
  163. package/dist/commands/ui/page.js +9 -6
  164. package/dist/commands/ui/places-change.d.ts +1 -1
  165. package/dist/commands/ui/plan-structure.d.ts +12 -2
  166. package/dist/commands/ui/plan-structure.en.d.ts +4 -0
  167. package/dist/commands/ui/plan-structure.en.js +4 -0
  168. package/dist/commands/ui/plan-structure.es.d.ts +4 -0
  169. package/dist/commands/ui/plan-structure.es.js +4 -0
  170. package/dist/commands/ui/plan-structure.js +48 -14
  171. package/dist/commands/ui/prototype-comp-route.d.ts +7 -0
  172. package/dist/commands/ui/prototype-comp-route.js +109 -0
  173. package/dist/commands/ui/prototype-main.d.ts +3 -1
  174. package/dist/commands/ui/prototype-scenes-child.js +6 -1
  175. package/dist/commands/ui/prototype-scenes-read.d.ts +1 -0
  176. package/dist/commands/ui/prototype-scenes-route.js +7 -1
  177. package/dist/commands/ui/prototype-snapshot-session.d.ts +1 -1
  178. package/dist/commands/ui/prototype-snapshot-session.js +3 -0
  179. package/dist/commands/ui/prototype-snapshot.d.ts +1 -1
  180. package/dist/commands/ui/routes.d.ts +6 -3
  181. package/dist/commands/ui/routes.js +3 -3
  182. package/dist/commands/ui/save-commit.js +6 -2
  183. package/dist/commands/ui/save-publish.js +3 -0
  184. package/dist/commands/ui/server-options.js +2 -0
  185. package/dist/commands/ui/server.d.ts +12 -22
  186. package/dist/commands/ui/server.js +10 -48
  187. package/dist/commands/ui/snapshot-runners.d.ts +22 -0
  188. package/dist/commands/ui/snapshot-runners.js +45 -0
  189. package/dist/commands/ui/structure-surface.d.ts +6 -0
  190. package/dist/commands/ui/structure-surface.js +94 -0
  191. package/dist/commands/ui/studio-routes.d.ts +1 -0
  192. package/dist/commands/ui/studio-routes.js +5 -0
  193. package/dist/commands/ui/studio-state.d.ts +9 -4
  194. package/dist/commands/ui/studio-state.js +1 -0
  195. package/dist/commands/ui/studio-versions.d.ts +3 -3
  196. package/dist/commands/ui/studio.en.d.ts +13 -0
  197. package/dist/commands/ui/studio.en.js +13 -0
  198. package/dist/commands/ui/studio.es.d.ts +13 -0
  199. package/dist/commands/ui/studio.es.js +13 -0
  200. package/dist/commands/ui/theme-option.d.ts +8 -0
  201. package/dist/commands/ui/theme-option.js +40 -0
  202. package/dist/commands/ui/theme-values.d.ts +5 -0
  203. package/dist/commands/ui/theme-values.js +113 -0
  204. package/dist/ui/page.js +94 -94
  205. package/docs/PROCESS.md +5 -0
  206. package/docs/PROTOTYPE.md +24 -1
  207. package/docs/WORKBENCH.md +76 -22
  208. package/package.json +11 -11
  209. package/skills/ape-critique/SKILL.md +16 -6
  210. package/skills/ape-design-system/SKILL.md +4 -3
  211. package/skills/ape-plan-change/SKILL.md +7 -3
  212. package/skills/ape-prototype/SKILL.md +12 -1
  213. package/templates/example/apps/app/src/i18n/en.ts +2 -0
  214. package/templates/example/apps/app/src/i18n/es.json +2 -0
  215. package/templates/product/apps/admin/src/fonts.ts +2 -1
  216. package/templates/product/apps/app/src/fonts.ts +5 -1
  217. package/templates/product/apps/app/src/i18n/en.ts +2 -0
  218. package/templates/product/apps/app/src/i18n/es.json +2 -0
  219. package/templates/product/prototype/journeys.ts +3 -0
@@ -1,7 +1,8 @@
1
- import { randomUUID } from 'node:crypto';
2
1
  import { promises as fs } from 'node:fs';
3
2
  import { homedir } from 'node:os';
4
- import { isAbsolute, join, parse, relative, resolve, sep } from 'node:path';
3
+ import { isAbsolute, parse, relative, resolve, sep } from 'node:path';
4
+ import { isObject, SettingsRefusal, updateLocalSettings } from './local-settings.js';
5
+ import { holdsSecrets } from './secret-folders.js';
5
6
  export class AllowRefusal extends Error {
6
7
  why;
7
8
  constructor(why) {
@@ -10,22 +11,6 @@ export class AllowRefusal extends Error {
10
11
  }
11
12
  }
12
13
  const windows = process.platform === 'win32';
13
- /**
14
- * Folders that hold sign-ins and keys. The folder an agent was refused comes from what the agent
15
- * tried to read, so one click must never hand it these, at any depth.
16
- */
17
- const SECRET_FOLDERS = new Set([
18
- '.ssh',
19
- '.gnupg',
20
- '.aws',
21
- '.azure',
22
- '.kube',
23
- '.docker',
24
- '.gcloud',
25
- '.password-store',
26
- 'secrets',
27
- ]);
28
- const holdsSecrets = (path) => path.split(/[\\/]+/).some((part) => SECRET_FOLDERS.has(part.toLowerCase()));
29
14
  const same = (a, b) => (windows ? a.toLowerCase() === b.toLowerCase() : a === b);
30
15
  /** `child` is `parent` or inside it. */
31
16
  function within(parent, child) {
@@ -63,68 +48,29 @@ export async function allowableFolder(product, path) {
63
48
  throw new AllowRefusal('linked');
64
49
  return real;
65
50
  }
66
- const object = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
67
- async function readSettings(file) {
68
- let text;
69
- try {
70
- text = await fs.readFile(file, 'utf8');
71
- }
72
- catch (error) {
73
- if (error.code === 'ENOENT')
74
- return {};
75
- throw new AllowRefusal('settings');
76
- }
77
- try {
78
- const value = JSON.parse(text);
79
- if (object(value))
80
- return value;
81
- }
82
- catch {
83
- // Not JSON: never overwritten.
84
- }
85
- throw new AllowRefusal('settings');
86
- }
87
- /** A scanner or an editor can hold the file for a moment on Windows: a few short tries. */
88
- async function replace(temporary, file) {
89
- for (let n = 1;; n++) {
90
- try {
91
- return await fs.rename(temporary, file);
92
- }
93
- catch (error) {
94
- const code = error.code;
95
- if (n >= 5 || !['EPERM', 'EBUSY', 'EACCES'].includes(code ?? '')) {
96
- await fs.rm(temporary, { force: true });
97
- throw error;
98
- }
99
- await new Promise((done) => setTimeout(done, 20 * n));
100
- }
101
- }
102
- }
103
- let queue = Promise.resolve();
104
51
  /**
105
52
  * Adds `folder` to `permissions.additionalDirectories` in `<product>/.claude/settings.local.json`:
106
53
  * made when missing, every other setting kept, never twice, written whole or not at all.
107
54
  * Returns false when the folder was there already.
108
55
  */
109
- export function addAdditionalDirectory(product, folder) {
110
- const next = queue.then(async () => {
111
- const file = join(product, '.claude', 'settings.local.json');
112
- const settings = await readSettings(file);
113
- const permissions = settings.permissions ?? {};
114
- if (!object(permissions))
115
- throw new AllowRefusal('settings');
116
- const list = permissions.additionalDirectories ?? [];
117
- if (!Array.isArray(list))
118
- throw new AllowRefusal('settings');
119
- if (list.some((entry) => typeof entry === 'string' && same(resolve(product, entry), folder)))
120
- return false;
121
- const text = `${JSON.stringify({ ...settings, permissions: { ...permissions, additionalDirectories: [...list, folder] } }, null, 2)}\n`;
122
- await fs.mkdir(join(product, '.claude'), { recursive: true });
123
- const temporary = `${file}.${randomUUID()}.tmp`;
124
- await fs.writeFile(temporary, text, { flag: 'wx' });
125
- await replace(temporary, file);
126
- return true;
127
- });
128
- queue = next.catch(() => undefined);
129
- return next;
56
+ export async function addAdditionalDirectory(product, folder) {
57
+ try {
58
+ return await updateLocalSettings(product, (settings) => {
59
+ const permissions = settings.permissions ?? {};
60
+ if (!isObject(permissions))
61
+ throw new AllowRefusal('settings');
62
+ const list = permissions.additionalDirectories ?? [];
63
+ if (!Array.isArray(list))
64
+ throw new AllowRefusal('settings');
65
+ if (list.some((entry) => typeof entry === 'string' && same(resolve(product, entry), folder)))
66
+ return undefined;
67
+ return {
68
+ ...settings,
69
+ permissions: { ...permissions, additionalDirectories: [...list, folder] },
70
+ };
71
+ });
72
+ }
73
+ catch (error) {
74
+ throw error instanceof SettingsRefusal ? new AllowRefusal('settings') : error;
75
+ }
130
76
  }
@@ -0,0 +1,21 @@
1
+ import type { Chat } from '@buffcore/contracts/workbench';
2
+ import type { TurnGuard } from './turn-guard.ts';
3
+ /** The permission prompt tool's name: the studio's MCP server `ape_permission`, tool `ask`. */
4
+ export declare const PROMPT_TOOL = "mcp__ape_permission__ask";
5
+ /** Deny rules hold in every mode, bypass included: the file tools never read these folders. */
6
+ export declare const SECRET_READS: string[];
7
+ /**
8
+ * The turn's secret-folder denies. A folder name the turn's own folder lies in (a product kept
9
+ * under `secrets/`) is denied inside the product and in the person's home only, as the limits
10
+ * hook counts it; every other name is denied anywhere.
11
+ */
12
+ export declare function secretReads(folder: string): string[];
13
+ /**
14
+ * The level's permission mode. Autopilot runs Claude's `auto` (its classifier decides), or
15
+ * `bypassPermissions` with "Never ask", planning included. Below Autopilot, and in a launch
16
+ * without a guard, planning runs `dontAsk` (it writes plan/ only, with two commands) and every
17
+ * other work accepts edits and asks.
18
+ */
19
+ export declare function permissionMode(work: Chat['work'], guard?: Pick<TurnGuard, 'level' | 'neverAsk'>): "acceptEdits" | "auto" | "bypassPermissions" | "dontAsk";
20
+ /** Claude's permission flags for one turn, spliced in after the fixed start arguments. */
21
+ export declare function claudePermissions(args: string[], work: Chat['work'], product: string, folder: string, verification: readonly string[], guard?: TurnGuard): void;
@@ -0,0 +1,69 @@
1
+ import { claudeDenied, claudeLinked, claudeRules, isPlanning, PLANNING_SETTINGS, } from './permissions.js';
2
+ import { PLANNING_LIMITS } from './planning-limits.js';
3
+ import { enclosingSecrets, homeSecret, SECRET_FOLDERS } from './secret-folders.js';
4
+ import { stepRules } from './step-rules.js';
5
+ import { verificationRules } from './verification.js';
6
+ /** The permission prompt tool's name: the studio's MCP server `ape_permission`, tool `ask`. */
7
+ export const PROMPT_TOOL = 'mcp__ape_permission__ask';
8
+ /** Deny rules hold in every mode, bypass included: the file tools never read these folders. */
9
+ export const SECRET_READS = [...SECRET_FOLDERS].map((folder) => `Read(//**/${folder}/**)`);
10
+ /**
11
+ * The turn's secret-folder denies. A folder name the turn's own folder lies in (a product kept
12
+ * under `secrets/`) is denied inside the product and in the person's home only, as the limits
13
+ * hook counts it; every other name is denied anywhere.
14
+ */
15
+ export function secretReads(folder) {
16
+ const enclosing = enclosingSecrets(folder);
17
+ if (!enclosing.length)
18
+ return SECRET_READS;
19
+ return [...SECRET_FOLDERS].flatMap((name) => enclosing.includes(name)
20
+ ? [`Read(./**/${name}/**)`, ...(homeSecret(name, folder) ? [`Read(~/${name}/**)`] : [])]
21
+ : [`Read(//**/${name}/**)`]);
22
+ }
23
+ /**
24
+ * The level's permission mode. Autopilot runs Claude's `auto` (its classifier decides), or
25
+ * `bypassPermissions` with "Never ask", planning included. Below Autopilot, and in a launch
26
+ * without a guard, planning runs `dontAsk` (it writes plan/ only, with two commands) and every
27
+ * other work accepts edits and asks.
28
+ */
29
+ export function permissionMode(work, guard) {
30
+ if (guard?.level === 'auto')
31
+ return guard.neverAsk ? 'bypassPermissions' : 'auto';
32
+ return isPlanning(work) ? 'dontAsk' : 'acceptEdits';
33
+ }
34
+ /** Claude's permission flags for one turn, spliced in after the fixed start arguments. */
35
+ export function claudePermissions(args, work, product, folder, verification, guard) {
36
+ const linked = claudeLinked(product);
37
+ const mode = permissionMode(work, guard);
38
+ // dontAsk turns disable every hook, the limits hook included: deny rules hold their limits.
39
+ const unhooked = mode === 'dontAsk';
40
+ const asks = guard?.settings && guard.mcp
41
+ ? ['--settings', guard.settings, '--mcp-config', guard.mcp, '--permission-prompt-tool']
42
+ : [];
43
+ args.splice(5, 0, '--permission-mode', mode, ...(unhooked ? ['--settings', PLANNING_SETTINGS] : []), ...(asks.length ? [...asks, PROMPT_TOOL] : []));
44
+ // Autopilot has no step list, planning included: the classifier decides what it would allow.
45
+ // Planning's edits outside plan/ are still held by the studio's save.
46
+ const allowed = [
47
+ ...(guard?.level === 'auto' ? [] : claudeRules(work, folder)),
48
+ ...verificationRules(verification),
49
+ ...(asks.length ? stepRules(product, work) : []),
50
+ ...linked.allowed,
51
+ ];
52
+ if (allowed.length)
53
+ args.push('--allowedTools', ...allowed);
54
+ // The tool answers permission requests only: the model never sees it as a tool of its own.
55
+ const denied = [
56
+ ...claudeDenied(work, guard?.level === 'auto'),
57
+ ...linked.denied,
58
+ ...(guard ? secretReads(folder) : []),
59
+ ...(guard && unhooked ? PLANNING_LIMITS : []),
60
+ ...(asks.length ? [PROMPT_TOOL] : []),
61
+ ];
62
+ if (denied.length)
63
+ args.push('--disallowedTools', ...denied);
64
+ if (!guard)
65
+ return;
66
+ guard.allowed.splice(0, guard.allowed.length, ...allowed);
67
+ for (const arg of [...asks, PROMPT_TOOL, ...allowed, ...denied])
68
+ guard.args.add(arg);
69
+ }
@@ -9,6 +9,7 @@ export declare function watchCodexIdle(child: ChildProcess, adapter: ChatAdapter
9
9
  id: import("@buffcore/contracts/workbench").AgentId;
10
10
  buildArgs: (options: import("./prompt.ts").TurnOptions) => string[];
11
11
  reply?: (line: string) => string | undefined;
12
+ notice?: (line: string) => string | undefined;
12
13
  parse(line: string): ({
13
14
  kind: "text";
14
15
  text: string;
@@ -24,6 +25,7 @@ export declare function watchCodexIdle(child: ChildProcess, adapter: ChatAdapter
24
25
  kind: "refused";
25
26
  id: string;
26
27
  reason?: "command" | "monitor" | "permission" | "redirect" | "taskOutput" | undefined;
28
+ limit?: "deploy" | "publish" | "push" | "release" | "secrets" | "settings" | undefined;
27
29
  } | {
28
30
  kind: "file";
29
31
  path: string;
@@ -1,6 +1,7 @@
1
1
  import { changeWorktreeGit } from '../../worktree-git-trust.js';
2
2
  import { codexExampleEnv } from './codex-examples.js';
3
- import { codexFilesystem, codexTrust, inTree } from './permissions.js';
3
+ import { codexFilesystem, codexTrust } from './codex-permissions.js';
4
+ import { inTree } from './permissions.js';
4
5
  // An extended Windows path can use 32,767 UTF-16 code units. Every directory and its separator
5
6
  // consume at least two; a lower, arbitrary scan limit could miss a path the existing globs deny.
6
7
  export const CODEX_WINDOWS_OVERRIDES = [
@@ -0,0 +1,12 @@
1
+ import type { Chat } from '@buffcore/contracts/workbench';
2
+ /** JSON's string escaping is also valid for TOML basic keys, including quotes and backslashes. */
3
+ export declare function codexTrust(folder: string): string;
4
+ /** Share a scan between argument construction and shim validation, only for this launch. */
5
+ export declare function withLinkedPermissions<T>(launch: () => T): T;
6
+ /**
7
+ * Every turn denies comments and the approval record; design and validation, which write the
8
+ * product folder, also the change trees (planning writes plan/ only). Codex takes absolute
9
+ * paths only (0.166 rejects read globs), so a nested product's prototype/versions is not
10
+ * named here; Claude's rule covers it. Design/validation expand read paths.
11
+ */
12
+ export declare function codexFilesystem(work: Chat['work'], folder: string, product?: string, examples?: string): string[];
@@ -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[];