Repository navigation
fix(install): protect startup ownership and roll back partial WorkBuddy wiring #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cangen185-sudo
merged 4 commits into
starsstreaming:main
from
cangen185-sudo:codex/fix-workbuddy-install-rollback
Nov 18, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d9dd1c5
fix(install): preserve owned startup files and roll back partial wiring
cangen185-sudo e89263a
test(install): verify packaged transaction dependency imports
cangen185-sudo 02f494a
fix(install): preserve launcher permissions and finish preflight cleanup
cangen185-sudo cec0af8
fix(install): register file recovery before publication
cangen185-sudo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import test from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import {workbuddyWindowsPaths,workbuddyWindowsWiring} from '../../../scripts/workbuddy-windows-wiring.mjs'; | ||
| import {workbuddyLinuxPaths,workbuddyLinuxWiring} from '../../../scripts/workbuddy-linux-wiring.mjs'; | ||
| import {commitWorkbuddyInstall} from '../../../scripts/workbuddy-install-transaction.mjs'; | ||
| import {renderWindowsStartupVbs} from '../../../scripts/portable-runtime.mjs'; | ||
|
|
||
| function fixture(t){const root=fs.mkdtempSync(path.join(os.tmpdir(),'bc-install-tx-'));t.after(()=>fs.rmSync(root,{recursive:true,force:true}));return root;} | ||
| test('late startup publication failure restores files and registry', t => { | ||
| const root = fixture(t), env = { APPDATA: root }; | ||
| const paths = workbuddyWindowsPaths(env, root); | ||
| let registryValue = null; | ||
| const wiring = workbuddyWindowsWiring({ | ||
| env, home: root, launcher: path.join(root, 'launcher.ps1'), journal: path.join(root, 'journal'), | ||
| registry: { read: () => registryValue, write: value => { registryValue = value; } }, | ||
| replace(file, _before, value) { | ||
| fs.mkdirSync(path.dirname(file), { recursive: true }); | ||
| fs.writeFileSync(file, value); | ||
| throw new Error('temporary cleanup failed'); | ||
| }, | ||
| }); | ||
| assert.throws(() => wiring.install('9335'), /cleanup failed/); | ||
| assert.equal(fs.existsSync(paths.startup), false); | ||
| assert.equal(registryValue, null); | ||
| }); | ||
|
|
||
| test('identical launcher contents still restore original POSIX permissions', { skip: process.platform === 'win32' }, async t => { | ||
| const root = fixture(t); | ||
| const runtime = { pointerPath: path.join(root, 'pointer'), launcher: path.join(root, 'launcher.sh') }; | ||
| fs.writeFileSync(runtime.launcher, 'unchanged', { mode: 0o751 }); | ||
| fs.chmodSync(runtime.launcher, 0o751); | ||
| await assert.rejects(commitWorkbuddyInstall({ | ||
| runtime, expectedLauncher: 'unchanged', portFile: path.join(root, 'port'), portText: '9335', | ||
| install: () => ({ rollback() {} }), discard() {}, | ||
| activate() { fs.chmodSync(runtime.launcher, 0o755); throw new Error('activation failed'); }, | ||
| }), /activation failed/); | ||
| assert.equal(fs.statSync(runtime.launcher).mode & 0o777, 0o751); | ||
| }); | ||
|
|
||
| test('preflight failures discard the candidate before any wiring', async t => { | ||
| const root = fixture(t); | ||
| let discarded = 0; | ||
| const options = { | ||
| runtime: { pointerPath: root, launcher: path.join(root, 'launcher') }, | ||
| install: () => assert.fail('unexpected install'), | ||
| discard: () => { discarded++; }, | ||
| }; | ||
| await assert.rejects(commitWorkbuddyInstall(options), /regular file/); | ||
| await assert.rejects(commitWorkbuddyInstall({ ...options, expectedLauncher() { throw Error('render failed'); } }), /render failed/); | ||
| assert.equal(discarded, 2); | ||
| }); | ||
| test('foreign startup and redirected APPDATA are checked before registry writes; later edits preserved',t=>{ | ||
| const root=fixture(t),env={APPDATA:path.join(root,'redirected')},paths=workbuddyWindowsPaths(env,root);let value={value:'9000',kind:'String'}; | ||
| assert.ok(paths.startup.startsWith(env.APPDATA));fs.mkdirSync(path.dirname(paths.startup),{recursive:true});fs.writeFileSync(paths.startup,'foreign'); | ||
| const registry={read:()=>value,write:v=>{value=v}},journal=path.join(root,'journal'); | ||
| const options={env,home:root,launcher:path.join(root,'launcher.ps1'),journal,registry}; | ||
| assert.throws(()=>workbuddyWindowsWiring(options).install('9335'),/not owned/);assert.equal(value.value,'9000'); | ||
| fs.unlinkSync(paths.startup);fs.writeFileSync(paths.legacy,'foreign legacy'); | ||
| workbuddyWindowsWiring(options).install('9335');assert.equal(value.value,'9335'); | ||
| assert.equal(fs.readFileSync(paths.legacy,'utf8'),'foreign legacy'); | ||
| fs.writeFileSync(paths.startup,'user edited');assert.throws(()=>workbuddyWindowsWiring(options).remove(),/not owned/);assert.equal(value.value,'9335'); | ||
| }); | ||
| test('Windows startup write failure restores previous registry/journal state; successful wiring can undo',t=>{ | ||
| const root=fixture(t),env={APPDATA:root};let value=null; | ||
| const registry={read:()=>value,write:v=>{value=v}},journal=path.join(root,'journal'),opts={env,home:root,launcher:path.join(root,'launcher.ps1'),journal,registry}; | ||
| assert.throws(()=>workbuddyWindowsWiring({...opts,replace(){throw new Error('fixture disk full')}}).install('9335'),/disk full/); | ||
| assert.equal(value,null);assert.equal(fs.existsSync(journal),false); | ||
| const tx=workbuddyWindowsWiring(opts).install('9335');tx.rollback();assert.equal(value,null); | ||
| assert.equal(fs.existsSync(workbuddyWindowsPaths(env,root).startup),false); | ||
| }); | ||
| test('Linux environment failure removes newly created autostart; rollback preserves externally changed target',t=>{ | ||
| const root=fixture(t),env={XDG_CONFIG_HOME:path.join(root,'config')},paths=workbuddyLinuxPaths(env,root);fs.mkdirSync(env.XDG_CONFIG_HOME,{recursive:true});fs.writeFileSync(path.dirname(paths.environment),'block'); | ||
| const opts={env,home:root,launcher:'/tmp/fixture/launcher.sh'}; | ||
| assert.throws(()=>workbuddyLinuxWiring(opts).install('9335'));assert.equal(fs.existsSync(paths.desktop),false); | ||
| fs.unlinkSync(path.dirname(paths.environment));const tx=workbuddyLinuxWiring(opts).install('9335'); | ||
| fs.writeFileSync(paths.environment,'external edit');assert.throws(()=>tx.rollback(),/recovery incomplete/);assert.equal(fs.readFileSync(paths.environment,'utf8'),'external edit'); | ||
| }); | ||
| test('runtime activation failure restores wiring, port, pointer and launcher before discarding candidate',async t=>{ | ||
| const root=fixture(t),runtime={pointerPath:path.join(root,'current.json'),launcher:path.join(root,'launcher.ps1'),version:'v-new'}; | ||
| const portFile=path.join(root,'port.json');fs.writeFileSync(runtime.pointerPath,'{"version":"v-old"}');fs.writeFileSync(runtime.launcher,'old launcher');fs.writeFileSync(portFile,'old port');let wired=false,discarded=false; | ||
| if(process.platform !== 'win32') fs.chmodSync(runtime.launcher, 0o751); | ||
| await assert.rejects(commitWorkbuddyInstall({runtime,expectedLauncher:'new launcher',portFile,portText:'new port',install:()=>{wired=true;return{rollback(){wired=false}}},activate:()=>{fs.writeFileSync(runtime.launcher,'new launcher');fs.writeFileSync(runtime.pointerPath,'{"version":"v-new"}');throw new Error('fixture activation')},discard:()=>{discarded=true}}),/fixture activation/); | ||
| if(process.platform !== 'win32') assert.equal(fs.statSync(runtime.launcher).mode & 0o777, 0o751); | ||
| assert.equal(wired,false);assert.equal(discarded,true);assert.equal(fs.readFileSync(runtime.pointerPath,'utf8'),'{"version":"v-old"}');assert.equal(fs.readFileSync(runtime.launcher,'utf8'),'old launcher');assert.equal(fs.readFileSync(portFile,'utf8'),'old port'); | ||
| }); |
13 changes: 13 additions & 0 deletions
13
packages/beauticode-desktop/test/followup-package.test.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import test from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import fs from 'node:fs/promises'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import {pathToFileURL} from 'node:url'; | ||
| import {stageDesktopAggregate} from '../../../scripts/pack-desktop-aggregate.mjs'; | ||
|
|
||
| test('packaged WorkBuddy wiring imports every transaction dependency without the workspace',async t=>{ | ||
| const root=await fs.mkdtemp(path.join(os.tmpdir(),'bc-followup-pack-'));t.after(()=>fs.rm(root,{recursive:true,force:true})); | ||
| await stageDesktopAggregate(root,{build:false}); | ||
| for(const name of ['workbuddy-windows-wiring.mjs','workbuddy-linux-wiring.mjs','workbuddy-install-transaction.mjs'])await import(pathToFileURL(path.join(root,'runtime/workbuddy/scripts',name)).href); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { randomUUID } from 'node:crypto'; | ||
|
|
||
| export function readOwnedFile(file) { | ||
| try { | ||
| const stat=fs.lstatSync(file); | ||
| if(!stat.isFile() || stat.isSymbolicLink()) throw new Error('Managed target is not a regular file.'); | ||
| return fs.readFileSync(file,'utf8'); | ||
| } catch(error) { if(error.code==='ENOENT')return null;throw error; } | ||
| } | ||
| export function replaceOwnedFile(file, before, after, mode) { | ||
| if(readOwnedFile(file)!==before)throw new Error('Managed file changed externally; preserved.'); | ||
| if(after===null){if(before!==null)fs.unlinkSync(file);return;} | ||
| fs.mkdirSync(path.dirname(file),{recursive:true}); | ||
| const temporary=file+'.tmp-'+randomUUID(); | ||
| fs.writeFileSync(temporary,after,{flag:'wx',mode:0o600}); | ||
| try { | ||
| if(before===null)fs.linkSync(temporary,file); | ||
| else { | ||
| if(readOwnedFile(file)!==before)throw new Error('Managed file changed externally; preserved.'); | ||
| fs.chmodSync(temporary, mode ?? (fs.statSync(file).mode & 0o777)); | ||
| fs.renameSync(temporary,file); | ||
| } | ||
| } finally {fs.rmSync(temporary,{force:true});} | ||
| } | ||
| // Register compensation before publication. A write can succeed before temp cleanup throws. | ||
| export function undoFileChange(file, before, published, mode) { | ||
| const current = readOwnedFile(file); | ||
| if (current === before) return; | ||
| replaceOwnedFile(file, published, before, mode); | ||
| } | ||
| export function rollbackSteps(undos, original) { | ||
| const errors=[]; | ||
| for(const undo of undos.reverse())try{undo();}catch(error){errors.push(error);} | ||
| if(errors.length)throw new AggregateError([...(original?[original]:[]),...errors],'Installation failed; recovery incomplete. Saved ownership records must be retained.'); | ||
| if(original)throw original; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import fs from 'node:fs'; | ||
| import { readOwnedFile, replaceOwnedFile, undoFileChange } from './owned-file.mjs'; | ||
|
|
||
| /** Keep the previous runtime active until wiring succeeds; restore only this operation's values. */ | ||
| export async function commitWorkbuddyInstall({runtime,expectedLauncher,portFile,portText,install,activate,discard}) { | ||
| let before; | ||
| try { | ||
| if (typeof expectedLauncher === 'function') expectedLauncher = expectedLauncher(); | ||
| const launcher = readOwnedFile(runtime.launcher); | ||
| before = { | ||
| launcher, | ||
| launcherMode: launcher === null ? undefined : fs.statSync(runtime.launcher).mode & 0o777, | ||
| pointer: readOwnedFile(runtime.pointerPath), | ||
| port: readOwnedFile(portFile), | ||
| }; | ||
| } catch (error) { | ||
| await discard(); | ||
| throw error; | ||
| } | ||
| let wiring, portAttempted=false, activationAttempted=false; | ||
| try { | ||
| wiring=await install(); | ||
| portAttempted = true; | ||
| replaceOwnedFile(portFile,before.port,portText); | ||
| activationAttempted = true; | ||
| await activate(); | ||
| } catch(error) { | ||
| const errors=[]; | ||
| for(const undo of [ | ||
| ()=>{const current=readOwnedFile(runtime.pointerPath);if(current!==before.pointer){if(JSON.parse(current)?.version!==runtime.version)throw new Error('Runtime changed externally.');replaceOwnedFile(runtime.pointerPath,current,before.pointer);}}, | ||
| ()=>{ | ||
| if (!activationAttempted) return; | ||
| const current = readOwnedFile(runtime.launcher); | ||
| if (current !== before.launcher) { | ||
| undoFileChange(runtime.launcher, before.launcher, expectedLauncher, before.launcherMode); | ||
| } else if (current !== null && current === expectedLauncher && process.platform !== 'win32') { | ||
| const mode = fs.statSync(runtime.launcher).mode & 0o777; | ||
| if (mode !== before.launcherMode) { | ||
| if (mode !== 0o755 && mode !== (0o666 & ~process.umask())) throw new Error('Launcher mode changed externally; preserved.'); | ||
| fs.chmodSync(runtime.launcher, before.launcherMode); | ||
| } | ||
| } | ||
| }, | ||
| ()=>{if(portAttempted)undoFileChange(portFile,before.port,portText);}, | ||
| ()=>wiring?.rollback(), | ||
| ])try{undo();}catch(restoreError){errors.push(restoreError);} | ||
| if(!errors.length)await discard(); | ||
| if(errors.length)throw new AggregateError([error,...errors],'Installation failed; recovery incomplete. Retain runtime and ownership records.'); | ||
| throw error; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.