Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

feat(init): grant scoped machine-home writes [I1,I2]

Corey Olson5731332b7309
diff --git a/src/init.js b/src/init.js
index 40ed9586..31c53eb3 100644
--- a/src/init.js
+++ b/src/init.js
@@ -5,10 +5,12 @@
* write.
*/
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
-import { basename, join } from 'node:path';
+import { userInfo } from 'node:os';
+import { basename, isAbsolute, join, parse, relative, resolve, sep } from 'node:path';
import { CONFIG_FILE, COORDINATOR, doctrineFile, loadConfig } from './config.js';
import { loadDoctrine } from './doctrine.js';
import { installHooks } from './hooks.js';
+import { machineHome } from './machine-home.js';
import { Refused } from './refused.js';
import { installSkills } from './skills.js';
import {
@@ -18,6 +20,17 @@ import {
const NPM_DEFAULT_TEST = 'echo "Error: no test specified" && exit 1';
+/** Keep an automatic machine-home write grant below the OS user home and away from filesystem roots. */
+export function machineHomeWriteScopeIsNarrow(machinePath, userHome = userInfo().homedir) {
+ const target = resolve(machinePath);
+ const home = resolve(userHome);
+ if (target === parse(target).root) return false;
+ const homeFromTarget = relative(target, home);
+ const targetContainsHome = homeFromTarget === '' || (homeFromTarget !== '..'
+ && !homeFromTarget.startsWith(`..${sep}`) && !isAbsolute(homeFromTarget));
+ return !targetContainsHome;
+}
+
/**
* Detect an existing ecosystem's test command without running it during setup (I1, I2).
*
@@ -189,7 +202,7 @@ function writeSessionHook(root, onWrite) {
}
/**
- * Merge only missing Claude project allow rules, preserving every unrelated setting and rule.
+ * Merge Claude project rules and the exact machine-home sandbox write scope, preserving other settings.
*
* @param {string} root
* @param {string} gate
@@ -221,15 +234,43 @@ function writeClaudeLoopPermissions(root, gate, onWrite) {
const existing = allow ?? [];
const requested = claudeLoopRules({ gate });
const missing = requested.filter(rule => !existing.includes(rule));
- if (!missing.length) return 'kept the existing Claude Code Pullboard permissions';
- settings.permissions = { ...permissions, allow: [...existing, ...missing] };
- mkdirSync(join(root, '.claude'), { recursive: true });
- writeFileSync(file, `${JSON.stringify(settings, null, 2)}\n`);
- onWrite('.claude/settings.json');
+ const sandbox = settings.sandbox;
+ if (sandbox !== undefined && (!sandbox || typeof sandbox !== 'object' || Array.isArray(sandbox))) {
+ return 'kept .claude/settings.json because sandbox is not an object; add the Pullboard machine-home sandbox write path manually';
+ }
+ const currentFilesystem = sandbox?.filesystem;
+ if (currentFilesystem !== undefined && (!currentFilesystem || typeof currentFilesystem !== 'object' || Array.isArray(currentFilesystem))) {
+ return 'kept .claude/settings.json because sandbox.filesystem is not an object; add the Pullboard machine-home sandbox write path manually';
+ }
+ const filesystem = currentFilesystem ?? {};
+ const allowWrite = filesystem.allowWrite;
+ if (allowWrite !== undefined && !Array.isArray(allowWrite)) {
+ return 'kept .claude/settings.json because sandbox.filesystem.allowWrite is not an array; add the Pullboard machine-home sandbox write path manually';
+ }
+ const machinePath = machineHome();
+ const machinePathIsNarrow = machineHomeWriteScopeIsNarrow(machinePath);
+ const machineWrites = allowWrite ?? [];
+ const missingMachineWrite = machinePathIsNarrow && !machineWrites.includes(machinePath);
+ if (missing.length || missingMachineWrite) {
+ settings.permissions = { ...permissions, allow: [...existing, ...missing] };
+ if (missingMachineWrite) {
+ settings.sandbox = {
+ ...sandbox,
+ filesystem: { ...filesystem, allowWrite: [...machineWrites, machinePath] },
+ };
+ }
+ mkdirSync(join(root, '.claude'), { recursive: true });
+ writeFileSync(file, `${JSON.stringify(settings, null, 2)}\n`);
+ onWrite('.claude/settings.json');
+ }
const gateWords = plainGateWords(gate);
const gateNote = gateWords ? `, and the detected gate (${gateWords.join(' ')})` : gate ? '; the configured shell gate was left for manual approval' : '';
const editNote = requested.some(rule => rule.startsWith('Edit(')) ? ', plus edits within this worktree' : '';
- return `allowed Claude Code's Pullboard loop and listed Git commands${gateNote}${editNote}; existing deny and ask rules still apply`;
+ const permissionsNote = missing.length ? `allowed Claude Code's Pullboard loop and listed Git commands${gateNote}${editNote}` : 'kept the existing Claude Code Pullboard permissions';
+ const machineWriteNote = !machinePathIsNarrow
+ ? 'did not grant machine-home write because it is the OS home, an ancestor of the OS home, or a filesystem root; choose a narrower PULLBOARD_MACHINE_HOME'
+ : missingMachineWrite ? 'added the exact Pullboard machine-home Bash write path' : 'kept the existing exact Pullboard machine-home Bash write path';
+ return `${permissionsNote}; ${machineWriteNote}; an admin-required sandbox may ignore project-level write additions; existing deny and ask rules still apply`;
}
/**
@@ -242,18 +283,26 @@ function writeClaudeLoopPermissions(root, gate, onWrite) {
*/
function writeCodexLoopRules(root, gate, onWrite) {
const file = join(root, '.codex', 'rules', 'pullboard.rules');
+ const machinePath = machineHome();
const rules = codexLoopRules(gate);
let contents = '';
if (existsSync(file)) contents = readFileSync(file, 'utf8');
const lines = new Set(contents.split(/\r?\n/u));
const missing = rules.filter(rule => !lines.has(rule));
- if (!missing.length) return 'kept the existing Codex project rules';
- mkdirSync(join(root, '.codex', 'rules'), { recursive: true });
- const prefix = contents && !contents.endsWith('\n') ? `${contents}\n` : contents;
- writeFileSync(file, `${prefix}${missing.join('\n')}\n`);
- onWrite('.codex/rules/pullboard.rules');
+ if (missing.length) {
+ mkdirSync(join(root, '.codex', 'rules'), { recursive: true });
+ const prefix = contents && !contents.endsWith('\n') ? `${contents}\n` : contents;
+ writeFileSync(file, `${prefix}${missing.join('\n')}\n`);
+ onWrite('.codex/rules/pullboard.rules');
+ }
const gateNote = plainGateWords(gate) ? '' : gate ? '; the configured shell gate was left for manual approval' : '';
- return `added trusted-project Codex rules for Pullboard and the listed Git commands${gateNote}; trust this project and restart the session to load them`;
+ const rulesNote = missing.length ? `added trusted-project Codex rules for Pullboard and the listed Git commands${gateNote}` : 'kept the existing Codex project rules';
+ if (!machineHomeWriteScopeIsNarrow(machinePath)) {
+ return `${rulesNote}; trust this project and restart the session to load rules. Machine-home write was not granted because the path is the OS home, an ancestor of it, or a filesystem root; choose a narrower PULLBOARD_MACHINE_HOME`;
+ }
+ const workspaceWrite = `[sandbox_workspace_write]\nwritable_roots=${JSON.stringify([machinePath])}`;
+ const profileWrite = `${JSON.stringify(machinePath)} = "write"`;
+ return `${rulesNote}; trust this project and restart the session to load rules. For machine-home access, legacy Codex settings add:\n${workspaceWrite}\nPermission-profile users add ${profileWrite} under their existing [permissions.<active-profile>.filesystem] table instead; do not mix the two configuration formats`;
}
/**
diff --git a/test/init-permissions.test.js b/test/init-permissions.test.js
index 28181bef..f19144ad 100644
--- a/test/init-permissions.test.js
+++ b/test/init-permissions.test.js
@@ -2,8 +2,10 @@
import assert from 'node:assert/strict';
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
-import { join, resolve } from 'node:path';
+import { join, parse, resolve } from 'node:path';
import { test } from 'node:test';
+import { machineHomeWriteScopeIsNarrow } from '../src/init.js';
+import { machineHome } from '../src/machine-home.js';
import { runFixtureChild, runFixtureGit } from './fixture-child.js';
const BIN = resolve(import.meta.dirname, '../bin/pullboard.js');
@@ -48,7 +50,23 @@ function initFixture(t, { existingSettings, existingCodexRules } = {}) {
const child = runFixtureChild(process.execPath, [BIN, 'init', ...args, '--json'], { cwd: root, env, encoding: 'utf8' });
return { code: child.status, document: child.stdout ? JSON.parse(child.stdout) : null, stderr: child.failure ?? child.stderr };
}
- return { root, init };
+ return { root, env, init };
+}
+
+/** Resolve the fixture's real Pullboard machine home, while giving HOME a distinct decoy path. */
+function actualMachineHome(env) {
+ const priorHome = process.env.HOME;
+ const priorMachineHome = process.env.PULLBOARD_MACHINE_HOME;
+ try {
+ process.env.HOME = env.HOME;
+ process.env.PULLBOARD_MACHINE_HOME = env.PULLBOARD_MACHINE_HOME;
+ return machineHome();
+ } finally {
+ if (priorHome === undefined) delete process.env.HOME;
+ else process.env.HOME = priorHome;
+ if (priorMachineHome === undefined) delete process.env.PULLBOARD_MACHINE_HOME;
+ else process.env.PULLBOARD_MACHINE_HOME = priorMachineHome;
+ }
}
/** Parse Codex argv-prefix rules without depending on JSON whitespace. */
@@ -63,11 +81,20 @@ function codexPrefixes(contents) {
}
test('init grants the agent the permissions the Pullboard loop needs [I1,I2]', t => {
+ const syntheticHome = join(resolve(tmpdir()), 'permission-scope-user');
+ assert.equal(machineHomeWriteScopeIsNarrow(join(syntheticHome, '.pullboard'), syntheticHome), true, 'a private machine-home below the OS home is eligible for a write grant');
+ assert.equal(machineHomeWriteScopeIsNarrow(syntheticHome, syntheticHome), false, 'the OS home itself is never granted');
+ assert.equal(machineHomeWriteScopeIsNarrow(resolve(syntheticHome, '..'), syntheticHome), false, 'an ancestor containing the OS home is never granted');
+ assert.equal(machineHomeWriteScopeIsNarrow(parse(syntheticHome).root, syntheticHome), false, 'a filesystem root is never granted');
+
const fresh = initFixture(t);
const freshInit = fresh.init();
assert.equal(freshInit.code, 0, freshInit.stderr);
const freshSettings = JSON.parse(readFileSync(join(fresh.root, '.claude', 'settings.json'), 'utf8'));
+ const freshMachineHome = actualMachineHome(fresh.env);
assert.ok(freshSettings.permissions?.allow?.includes('Bash(pullboard:*)'), 'fresh projects receive Claude loop rules');
+ assert.deepEqual(freshSettings.sandbox?.filesystem?.allowWrite, [freshMachineHome], 'Claude sandbox can write only to Pullboard machine home');
+ assert.notEqual(freshMachineHome, fresh.env.HOME, 'the effective machine home ignores HOME when an explicit machine-home override is set');
const freshCodexRules = readFileSync(join(fresh.root, '.codex', 'rules', 'pullboard.rules'), 'utf8');
assert.deepEqual(codexPrefixes(freshCodexRules), [['pullboard'], ['git', 'status'], ['git', 'diff'], ['git', 'log'], ['git', 'add'], ['git', 'commit'], ['git', 'merge'], ['git', 'rebase'], ['git', 'switch'], ['git', 'checkout'], ['git', 'worktree'], ['npm', 'test']], 'fresh projects receive only the required Codex command prefixes');
@@ -79,6 +106,7 @@ test('init grants the agent the permissions the Pullboard loop needs [I1,I2]', t
ask: ['Bash(git push *)'],
deny: ['Bash(rm -rf *)'],
},
+ sandbox: { enabled: true, filesystem: { allowWrite: ['/tmp/existing-write'], denyWrite: ['/tmp/existing-deny'] } },
};
const box = initFixture(t, { existingSettings: existing, existingCodexRules: '# user rule stays\n' });
const initialized = box.init();
@@ -91,6 +119,9 @@ test('init grants the agent the permissions the Pullboard loop needs [I1,I2]', t
assert.ok(settings.hooks.SessionStart, 'the existing Pullboard resume hook remains installed');
assert.deepEqual(settings.permissions.ask, existing.permissions.ask, 'existing ask rules remain unchanged');
assert.deepEqual(settings.permissions.deny, existing.permissions.deny, 'existing deny rules remain unchanged');
+ const machineHomePath = actualMachineHome(box.env);
+ assert.deepEqual(settings.sandbox.filesystem.allowWrite, ['/tmp/existing-write', machineHomePath], 'the exact machine home is merged with existing sandbox write paths');
+ assert.deepEqual(settings.sandbox.filesystem.denyWrite, existing.sandbox.filesystem.denyWrite, 'existing sandbox deny paths remain unchanged');
const claudeRules = settings.permissions.allow;
for (const rule of [
'Bash(pullboard:*)',
@@ -108,6 +139,9 @@ test('init grants the agent the permissions the Pullboard loop needs [I1,I2]', t
assert.deepEqual(codexPrefixes(codexRules), [['pullboard'], ['git', 'status'], ['git', 'diff'], ['git', 'log'], ['git', 'add'], ['git', 'commit'], ['git', 'merge'], ['git', 'rebase'], ['git', 'switch'], ['git', 'checkout'], ['git', 'worktree'], ['npm', 'test']], 'Codex rules stay scoped to the exact loop commands');
assert.ok(initialized.document.notes.some(note => /Claude Code.*Pullboard loop/u.test(note)), 'init says what Claude rules it added');
assert.ok(initialized.document.notes.some(note => /trusted-project Codex rules.*trust this project and restart the session/u.test(note)), 'init explains trust and session restart for Codex rules');
+ const expectedLegacyCodex = `[sandbox_workspace_write]\nwritable_roots=${JSON.stringify([machineHomePath])}`;
+ assert.ok(initialized.document.notes.some(note => note.includes(expectedLegacyCodex)), 'Codex setup prints the exact machine-home legacy workspace-write lines');
+ assert.ok(initialized.document.notes.some(note => /permission-profile users.*existing \[permissions\.<active-profile>\.filesystem\].*do not mix/iu.test(note)), 'Codex setup explains the permission-profile alternative without mixing config systems');
const settingsBytes = readFileSync(settingsPath, 'utf8');
const codexBytes = readFileSync(codexPath, 'utf8');
@@ -128,9 +162,11 @@ test('init grants the agent the permissions the Pullboard loop needs [I1,I2]', t
const skipSettingsPath = join(skipExisting.root, '.claude', 'settings.json');
const skipCodexPath = join(skipExisting.root, '.codex', 'rules', 'pullboard.rules');
const skipPermissions = JSON.parse(readFileSync(skipSettingsPath, 'utf8')).permissions;
+ const skipSandbox = JSON.parse(readFileSync(skipSettingsPath, 'utf8')).sandbox;
const skipCodexBytes = readFileSync(skipCodexPath, 'utf8');
const skipExistingResult = skipExisting.init('--no-permissions');
assert.equal(skipExistingResult.code, 0, skipExistingResult.stderr);
assert.deepEqual(JSON.parse(readFileSync(skipSettingsPath, 'utf8')).permissions, skipPermissions, '--no-permissions preserves existing Claude permissions');
+ assert.deepEqual(JSON.parse(readFileSync(skipSettingsPath, 'utf8')).sandbox, skipSandbox, '--no-permissions preserves existing sandbox scope');
assert.equal(readFileSync(skipCodexPath, 'utf8'), skipCodexBytes, '--no-permissions preserves existing Codex rules byte-for-byte');
});