Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: avoid repeated sudo authentication during backup setup (#13502)

Adam Eivy1302d6663709
diff --git a/scripts/lib/timeMachineExclude.js b/scripts/lib/timeMachineExclude.js
index a3c6a4f2c7..d8f3819686 100644
--- a/scripts/lib/timeMachineExclude.js
+++ b/scripts/lib/timeMachineExclude.js
@@ -53,15 +53,30 @@ export function excludeFromTimeMachine(installRoot, {
if (platform !== 'darwin') return { status: 'unsupported' };
if (env.PORTOS_SKIP_TIMEMACHINE_EXCLUDE === '1') return { status: 'skipped' };
const result = { status: 'present', added: 0, excluded: 0, failures: [] };
+ let authenticated = false;
+ let authenticationError = null;
const options = { encoding: 'utf8', stdio: ['inherit', 'pipe', 'pipe'], timeout: 30_000 };
for (const relativePath of TIME_MACHINE_EXCLUDED_DIRS) {
const path = resolve(installRoot, relativePath);
+ if (authenticationError) {
+ result.failures.push({ path, error: authenticationError });
+ continue;
+ }
try {
const check = run('tmutil', ['isexcluded', path], options);
if (check.status === 0 && check.stdout?.includes('[Excluded]')) {
result.excluded++;
continue;
}
+ if (!authenticated) {
+ const auth = run('sudo', ['-v'], options);
+ if (auth.status !== 0) {
+ authenticationError = auth.error?.message || auth.stderr?.trim() || `sudo exited with status ${auth.status}`;
+ result.failures.push({ path, error: authenticationError });
+ continue;
+ }
+ authenticated = true;
+ }
const add = run('sudo', ['--', 'tmutil', 'addexclusion', '-p', path], options);
if (add.status === 0) {
result.added++;
diff --git a/scripts/lib/timeMachineExclude.test.js b/scripts/lib/timeMachineExclude.test.js
index bc650e140a..b18324d1c2 100644
--- a/scripts/lib/timeMachineExclude.test.js
+++ b/scripts/lib/timeMachineExclude.test.js
@@ -46,6 +46,7 @@ describe('Time Machine setup backup boundary', () => {
calls.push([command, ...args]);
const path = args.at(-1);
if (command === 'tmutil') return { status: 0, stdout: `${excluded.has(path) ? '[Excluded]' : '[Included]'} ${path}` };
+ if (args[0] === '-v') return { status: 0 };
excluded.add(path);
return { status: 0 };
});
@@ -54,7 +55,7 @@ describe('Time Machine setup backup boundary', () => {
expect(TIME_MACHINE_EXCLUDED_DIRS.slice().sort()).toEqual(expected.slice().sort());
const result = setup({ run });
expect(result).toEqual({ status: 'updated', added: expected.length - 1, excluded: 1, failures: [] });
- const additions = calls.filter(call => call[0] === 'sudo');
+ const additions = calls.filter(call => call[0] === 'sudo' && call[1] === '--');
expect(additions).toEqual(expected.filter(path => path !== 'data/cache')
.map(path => ['sudo', '--', 'tmutil', 'addexclusion', '-p', resolve(root, path)]));
for (const path of preservedData.map(path => `data/${path}`)) {
@@ -71,13 +72,15 @@ describe('Time Machine setup backup boundary', () => {
const log = vi.fn();
const run = vi.fn((command) => command === 'tmutil'
? { status: 0, stdout: '[Included]' }
- : { status: 1, stderr: 'sudo: a password is required' });
+ : { status: 1, stderr: 'sudo: a terminal is required\nsudo: a password is required' });
const result = setup({ run, log });
expect(result.status).toBe('partial');
expect(result.added).toBe(0);
+ expect(run.mock.calls.filter(([command]) => command === 'sudo')).toHaveLength(1);
expect(result.failures).toHaveLength(TIME_MACHINE_EXCLUDED_DIRS.length);
- expect(result.failures[0].error).toBe('sudo: a password is required');
+ expect(result.failures[0].error).toBe('sudo: a terminal is required\nsudo: a password is required');
expect(log.mock.calls.flat().join('\n')).toContain('Setup will continue');
+ expect(log.mock.calls.every(([line]) => !line.includes('\n'))).toBe(true);
run.mockImplementation(command => command === 'tmutil'
? { status: 0, stdout: '[Included]' } : { status: 0 });
expect(setup({ run }).status).toBe('updated');
@@ -92,6 +95,12 @@ describe('Time Machine setup backup boundary', () => {
expect(result.excluded).toBe(0);
expect(result.failures[0].error).toBe('spawn sudo ENOENT');
expect(run.mock.calls.some(([command]) => command === 'sudo')).toBe(true);
+ run.mockImplementation((command, args) => command === 'tmutil'
+ ? { status: 1, stderr: 'No such file or directory' }
+ : args[0] === '-v' ? { status: 0 } : { status: 1, stderr: 'Path does not exist' });
+ const missing = setup({ run });
+ expect(missing.failures).toHaveLength(TIME_MACHINE_EXCLUDED_DIRS.length);
+ expect(run.mock.calls.filter(([command, args]) => command === 'sudo' && args[0] === '--')).toHaveLength(TIME_MACHINE_EXCLUDED_DIRS.length);
run.mockImplementation(() => { throw new Error('runner unavailable'); });
expect(setup({ run }).failures[0].error).toBe('runner unavailable');
});
diff --git a/scripts/setup-timemachine.js b/scripts/setup-timemachine.js
index 7b184eb956..426bf3bea6 100644
--- a/scripts/setup-timemachine.js
+++ b/scripts/setup-timemachine.js
@@ -11,7 +11,7 @@ export function setupTimeMachine(root = installRoot, { log = console.log, ...opt
const result = excludeFromTimeMachine(root, options);
if (result.status === 'updated') log(`💾 Added ${result.added} fixed-path Time Machine exclusions for regenerable PortOS directories`);
if (result.status === 'partial') {
- for (const failure of result.failures) log(`⚠️ Time Machine exclusion failed for ${failure.path}: ${failure.error}`);
+ for (const failure of result.failures) log(`⚠️ Time Machine exclusion failed for ${failure.path}: ${failure.error.replace(/\s+/g, ' ')}`);
log('⚠️ Setup will continue. Fixed-path Time Machine exclusions require sudo; rerun node scripts/setup-timemachine.js to retry.');
}
return result;