Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

fix(storage): restore durable drafts when rollback cleanup fails

BlueSkyID6667d3b918d9bdf
diff --git a/src/main/util/atomic-directory-replace.ts b/src/main/util/atomic-directory-replace.ts
index 8d90614c..7d1efb0f 100644
--- a/src/main/util/atomic-directory-replace.ts
+++ b/src/main/util/atomic-directory-replace.ts
@@ -124,7 +124,15 @@ export async function replaceDirectoryAtomically(
await commit();
} catch (err) {
if (stagedActivated) {
- await fsp.rm(target, { recursive: true, force: true });
+ try {
+ await fsp.rm(target, { recursive: true, force: true });
+ } catch {
+ // Cleanup can fail on an open or protected child even though the
+ // parent permits a rename. Quarantine the rejected candidate at
+ // its now-vacant staging path so the durable backup can return.
+ // Never sweep the backup while an uncommitted target hides it.
+ await fsp.rename(target, staged);
+ }
stagedActivated = false;
}
if (previousMoved) {
diff --git a/test/main/features/auto_tasks.test.ts b/test/main/features/auto_tasks.test.ts
index 02974d09..008937b4 100644
--- a/test/main/features/auto_tasks.test.ts
+++ b/test/main/features/auto_tasks.test.ts
@@ -2523,4 +2523,37 @@ describe('automation › recovery discovery sequences', () => {
expect(autoRuntime.send).not.toHaveBeenCalled();
expect(armedDueAtForTest(taskId)).toBeNull();
});
+
+ it('preserves the original task and destination draft when migration commit and rollback deletion fail', async () => {
+ const taskId='at_19191919', projectId='p_rollback_cleanup';
+ writeProject(TEST_UID,projectId);
+ expect((await createTask(TEST_UID,{id:taskId,project_id:projectId,content:'Keep this schedule',enabled:false,
+ schedule:{type:'daily',hour:18,minute:0},attachments:['source.txt']})).ok).toBe(true);
+ await uploadAttachment(TEST_UID,taskId,'source.txt',Buffer.from('source attachment'));
+ const source=path.dirname(projectAutoTaskConfigFile(TEST_UID,projectId,taskId));
+ const target=autoTaskDir(TEST_UID,taskId), attachments=autoTaskAttachmentsDir(TEST_UID,taskId);
+ fs.mkdirSync(attachments,{recursive:true});fs.writeFileSync(path.join(attachments,'draft.txt'),'only draft');
+ const rename=nativeFs.renameSync.bind(nativeFs),rm=nativeFs.promises.rm.bind(nativeFs.promises);
+ const renameSpy=vi.spyOn(nativeFs,'renameSync').mockImplementation((from,to)=>{
+ if(String(from)===source)throw new Error('source retirement denied');
+ return rename(from,to);
+ });
+ const rmSpy=vi.spyOn(nativeFs.promises,'rm').mockImplementation(async(file,options)=>{
+ if(String(file)===target)throw new Error('rollback cleanup denied');
+ return rm(file,options);
+ });
+ syncBuiltinESMExports();
+ try {await expect(updateTask(TEST_UID,taskId,{project_id:null})).rejects.toThrow('source retirement denied');}
+ finally {renameSpy.mockRestore();rmSpy.mockRestore();syncBuiltinESMExports();}
+ expect(await getTask(TEST_UID,taskId)).toMatchObject({project_id:projectId,enabled:false});
+ expect(fs.existsSync(autoTaskConfigFile(TEST_UID,taskId))).toBe(false);
+ expect(fs.readFileSync(path.join(attachments,'draft.txt'),'utf8')).toBe('only draft');
+ expect((await updateTask(TEST_UID,taskId,{project_id:null})).ok).toBe(true);
+ stopScheduler();await rescheduleAllForTest(TEST_UID);
+ expect(await listTasks(TEST_UID)).toMatchObject([{id:taskId,enabled:false}]);
+ expect(await listTasks(TEST_UID)).toHaveLength(1);
+ expect(fs.readFileSync(path.join(attachments,'draft.txt'),'utf8')).toBe('only draft');
+ expect(fs.readFileSync(path.join(attachments,'source.txt'),'utf8')).toBe('source attachment');
+ expect(fs.existsSync(source)).toBe(false);expect(autoRuntime.send).not.toHaveBeenCalled();
+ });
});
diff --git a/test/main/util/atomic-directory-replace.test.ts b/test/main/util/atomic-directory-replace.test.ts
index 3f40e52d..548dfed2 100644
--- a/test/main/util/atomic-directory-replace.test.ts
+++ b/test/main/util/atomic-directory-replace.test.ts
@@ -239,6 +239,33 @@ describe('atomic directory replacement', () => {
expect(transactionArtifacts()).toEqual([]);
});
+ it('restores the durable draft when rollback removal fails before an explicit retry and sweep', async () => {
+ writeCurrent('only durable draft');
+ const rm = nativeFs.promises.rm.bind(nativeFs.promises);
+ const spy = vi.spyOn(nativeFs.promises, 'rm').mockImplementation(async (file, options) => {
+ if (String(file) === target) throw Object.assign(new Error('injected removal denial'), { code: 'EACCES' });
+ return rm(file, options);
+ });
+ syncBuiltinESMExports();
+ try {
+ await expect(replaceDirectoryAtomically(target, async staged => {
+ fs.writeFileSync(path.join(staged, 'uncommitted.txt'), 'not durable');
+ }, async () => { throw new Error('metadata rejected'); })).rejects.toThrow();
+ } finally { spy.mockRestore(); syncBuiltinESMExports(); }
+ // The caller reopens after rejection, before choosing to retry.
+ expect(fs.existsSync(path.join(target, 'content.txt'))).toBe(true);
+ expect(fs.readFileSync(path.join(target, 'content.txt'), 'utf8')).toBe('only durable draft');
+ expect(fs.existsSync(path.join(target, 'uncommitted.txt'))).toBe(false);
+ await replaceDirectoryAtomically(target, async staged => {
+ fs.cpSync(target, staged, { recursive: true });
+ fs.writeFileSync(path.join(staged, 'retry.txt'), 'accepted');
+ });
+ await sweepStaleReplacementArtifacts(root);
+ expect(fs.readFileSync(path.join(target, 'content.txt'), 'utf8')).toBe('only durable draft');
+ expect(fs.readFileSync(path.join(target, 'retry.txt'), 'utf8')).toBe('accepted');
+ expect(transactionArtifacts()).toEqual([]);
+ });
+
it('checks the continuation guard both before and after activation', async () => {
writeCurrent();
let calls = 0;