Open-source Xbox-inspired desktop launcher for Xodus on Linux
TypeScript
fix(runtime): persist pointer commits with explicit durability outcomes
domsvoboda7fb9397c47e9
diff --git a/src/main/runtime-pointer-commit.test.ts b/src/main/runtime-pointer-commit.test.tsnew file mode 100644index 0000000..7bd1944--- /dev/null+++ b/src/main/runtime-pointer-commit.test.ts@@ -0,0 +1,35 @@+import {afterEach,expect,it,vi} from 'vitest';+import {promises as fs} from 'node:fs';+import path from 'node:path';+import os from 'node:os';+import {commitRuntimePointer,type PointerCommitIO} from './runtime-pointer-commit';+const roots:string[]=[];+afterEach(async()=>{vi.restoreAllMocks();await Promise.all(roots.splice(0).map(root=>fs.rm(root,{recursive:true,force:true})));});+async function fixture(){const root=await fs.mkdtemp(path.join(os.tmpdir(),'pointer-commit-'));roots.push(root);await fs.writeFile(path.join(root,'current.json'),'old pointer');const save=path.join(root,'old-prefix/save');await fs.mkdir(path.dirname(save),{recursive:true});await fs.writeFile(save,'old progress');return{root,save,ino:(await fs.stat(save)).ino};}+function ioFault(root:string,fault:string,events:string[]):PointerCommitIO {+ return {open:async(file,flags,mode)=>{const directory=file===root;const tag=directory?'dir':'file';events.push(tag+' open');if(fault===tag+' open')throw Error(fault);const handle=await fs.open(file,flags,mode);return {+ writeFile:async data=>{events.push('write');if(fault==='write')throw Error(fault);await handle.writeFile(data);},+ sync:async()=>{events.push(tag+' sync');if(fault===tag+' sync')throw Error(fault);await handle.sync();},+ close:async()=>{events.push(tag+' close');await handle.close();if(fault===tag+' close')throw Error(fault);}};},+ rename:async(from,to)=>{events.push('rename');if(fault==='rename')throw Error(fault);await fs.rename(from,to);},unlink:async file=>{events.push('unlink');await fs.unlink(file);}};+}+it.each(['file open','write','file sync','file close','dir open','rename'])('preserves old pointer and nonempty save when %s fails before visibility',async fault=>{+ const f=await fixture(),events:string[]=[];await expect(commitRuntimePointer(f.root,{name:'new'},ioFault(f.root,fault,events))).rejects.toThrow('not activated');+ expect(await fs.readFile(path.join(f.root,'current.json'),'utf8')).toBe('old pointer');expect(await fs.readFile(f.save,'utf8')).toBe('old progress');expect((await fs.stat(f.save)).ino).toBe(f.ino);expect((await fs.readdir(f.root)).filter(n=>n.startsWith('.current-'))).toEqual([]);+});+it.each(['dir sync','dir close'])('reports committed with unknown durability after %s failure',async fault=>{+ const f=await fixture(),events:string[]=[];const result=await commitRuntimePointer(f.root,{name:'new'},ioFault(f.root,fault,events));expect(result.committed).toBe(true);expect(result.durability).toBe('unknown');expect(result.errors.map(e=>e.message).join()).toContain(fault==='dir sync'?'directory sync':'directory close');expect(JSON.parse(await fs.readFile(path.join(f.root,'current.json'),'utf8'))).toEqual({name:'new'});expect(await fs.readFile(f.save,'utf8')).toBe('old progress');+});+it('publishes only after full write, file sync and close, then syncs directory',async()=>{+ const f=await fixture(),events:string[]=[];expect(await commitRuntimePointer(f.root,{name:'new'},ioFault(f.root,'',events))).toEqual({committed:true,durability:'confirmed',errors:[]});expect(events).toEqual(['file open','write','file sync','file close','dir open','rename','dir sync','dir close']);+});+it('uses exclusive private temps and never deletes an existing collision or unrelated stale link',async()=>{+ const f=await fixture(),stale=path.join(f.root,'.current-stale.tmp');await fs.symlink('old-prefix/save',stale);let collision='';+ const io:PointerCommitIO={...fs,open:async(file,flags,mode)=>{collision=file;await fs.writeFile(file,'preexisting bytes');return fs.open(file,flags,mode);}};+ await expect(commitRuntimePointer(f.root,{name:'new'},io)).rejects.toThrow('not activated');expect(await fs.readFile(collision,'utf8')).toBe('preexisting bytes');expect(await fs.readlink(stale)).toBe('old-prefix/save');expect(await fs.readFile(f.save,'utf8')).toBe('old progress');expect(await fs.readFile(path.join(f.root,'current.json'),'utf8')).toBe('old pointer');+});++it('does not unlink a replacement at the former temporary path after rename',async()=>{+ const f=await fixture();let former='';const io:PointerCommitIO={...fs,rename:async(from,to)=>{await fs.rename(from,to);former=from;await fs.writeFile(from,'unrelated replacement');}};+ expect((await commitRuntimePointer(f.root,{name:'new'},io)).durability).toBe('confirmed');expect(await fs.readFile(former,'utf8')).toBe('unrelated replacement');+});diff --git a/src/main/runtime-pointer-commit.ts b/src/main/runtime-pointer-commit.tsnew file mode 100644index 0000000..9d24bcc--- /dev/null+++ b/src/main/runtime-pointer-commit.ts@@ -0,0 +1,41 @@+import {promises as fs} from 'node:fs';+import {randomUUID} from 'node:crypto';+import path from 'node:path';+interface CommitHandle { writeFile(data:string):Promise<void>; sync():Promise<void>; close():Promise<void>; }+export interface PointerCommitIO {+ open(file:string,flags:string,mode?:number):Promise<CommitHandle>;+ rename(from:string,to:string):Promise<void>;+ unlink(file:string):Promise<void>;+}+export interface PointerCommitResult {+ committed:true;+ durability:'confirmed'|'unknown';+ errors:Error[];+}+function failure(stage:string,error:unknown):Error {+ return new Error(`Runtime activation record ${stage} failed: ${error instanceof Error?error.message:String(error)}`,{cause:error});+}+/** Rename is the visibility boundary. A later sync failure cannot make the old+ * pointer active again. This persists the pointer, not the whole payload. */+export async function commitRuntimePointer(root:string,pointer:object,io:PointerCommitIO=fs):Promise<PointerCommitResult> {+ const temporary=path.join(root,'.current-'+randomUUID()+'.tmp');+ let file:CommitHandle|undefined,directory:CommitHandle|undefined,owned=false,committed=false,durable=false,stage='create';+ const errors:Error[]=[];+ try {+ file=await io.open(temporary,'wx',0o600);owned=true;+ stage='write';await file.writeFile(JSON.stringify(pointer,null,2)+'\n');+ stage='file sync';await file.sync();+ stage='file close';await file.close();file=undefined;+ // Open the directory before publication so open failure cannot publish.+ stage='directory open';directory=await io.open(root,'r');+ stage='rename';await io.rename(temporary,path.join(root,'current.json'));committed=true;owned=false;+ stage='directory sync';await directory.sync();durable=true;+ }catch(error){errors.push(failure(stage,error));}+ finally {+ if(file)try{await file.close();}catch(error){errors.push(failure('file cleanup close',error));}+ if(directory)try{await directory.close();}catch(error){errors.push(failure('directory close',error));if(committed)durable=false;}+ if(owned)try{await io.unlink(temporary);}catch(error){if((error as NodeJS.ErrnoException).code!=='ENOENT')errors.push(failure('temporary cleanup',error));}+ }+ if(!committed)throw new AggregateError(errors,'Runtime was not activated. '+errors.map(error=>error.message).join(' '));+ return {committed:true,durability:durable?'confirmed':'unknown',errors};+}