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.

Harden local storage migration and record recovery

wytc6eabdb6b93a
diff --git a/src/main/install-data-root.cjs b/src/main/install-data-root.cjs
index 4613b66c..883ed8d6 100644
--- a/src/main/install-data-root.cjs
+++ b/src/main/install-data-root.cjs
@@ -110,7 +110,7 @@ function writePin(container) {
const winner = readPin();
if (winner && pinIsLive(winner)) {
_earlyWarn(
- `[install-data-root] pin race: deferring to existing pin ${winner.container}\n`,
+ '[install-data-root] pin race: deferring to existing pin\n',
);
return winner.container;
}
@@ -119,12 +119,12 @@ function writePin(container) {
fs.writeFileSync(pinPath, JSON.stringify(body, null, 2), 'utf8');
} catch (err2) {
_earlyWarn(
- `[install-data-root] failed to overwrite stale pin: ${err2.message}\n`,
+ '[install-data-root] failed to overwrite stale pin\n',
);
}
return container;
}
- _earlyWarn(`[install-data-root] failed to write pin: ${err.message}\n`);
+ _earlyWarn('[install-data-root] failed to write pin\n');
return container;
}
}
@@ -150,7 +150,7 @@ function resolveWindowsContainer() {
if (pin && pinIsLive(pin)) return pin.container;
if (pin) {
_earlyWarn(
- `[install-data-root] pinned container ${pin.container} unreachable; re-deciding\n`,
+ '[install-data-root] pinned container unreachable; re-deciding\n',
);
}
@@ -236,7 +236,7 @@ function listFixedDrivesWin() {
migrateSourceDataRoot(container);
} catch (err) {
_earlyWarn(
- `[install-data-root] source-run data migration failed: ${err.message}\n`,
+ '[install-data-root] source-run data migration failed\n',
);
}
diff --git a/src/main/util/migrate-source-data-root.cjs b/src/main/util/migrate-source-data-root.cjs
index 8c45cd15..75db4a1d 100644
--- a/src/main/util/migrate-source-data-root.cjs
+++ b/src/main/util/migrate-source-data-root.cjs
@@ -34,6 +34,7 @@
const fs = require('node:fs');
const path = require('node:path');
+const { randomUUID } = require('node:crypto');
const STAMP_FILENAME = '.migrated-from-source';
@@ -107,7 +108,7 @@ function safeRm(dir) {
fs.rmSync(dir, { recursive: true, force: true });
} catch (err) {
process.stderr.write(
- `[migrate-source-data-root] failed to remove ${dir}: ${err.message}\n`,
+ '[migrate-source-data-root] source cleanup failed\n',
);
}
}
@@ -142,7 +143,7 @@ function unionUsersJson(srcFile, dstFile) {
if (!srcDoc) return; // Nothing to merge (no src users.json).
if (!dstDoc) {
// Target had no users.json → just write src's verbatim.
- fs.writeFileSync(dstFile, JSON.stringify(srcDoc, null, 2), 'utf8');
+ writeUsersJsonAtomic(dstFile, srcDoc);
return;
}
@@ -173,7 +174,20 @@ function unionUsersJson(srcFile, dstFile) {
String(a.created_at).localeCompare(String(b.created_at)),
),
};
- fs.writeFileSync(dstFile, JSON.stringify(merged, null, 2), 'utf8');
+ writeUsersJsonAtomic(dstFile, merged);
+}
+
+// Bootstrap runs before the TypeScript loader and storage.ts. Publish the
+// registry through a sibling so a failed write never truncates the old users.
+function writeUsersJsonAtomic(file, body) {
+ const temporary = `${file}.${randomUUID()}.tmp`;
+ try {
+ fs.writeFileSync(temporary, JSON.stringify(body, null, 2), { encoding: 'utf8', flag: 'wx', mode: 0o600 });
+ fs.renameSync(temporary, file);
+ } catch (error) {
+ try { fs.rmSync(temporary, { force: true }); } catch { /* preserve the write error */ }
+ throw error;
+ }
}
function readUsersJsonOrNull(file) {
diff --git a/test/main/storage-strict-jsonl.test.ts b/test/main/storage-strict-jsonl.test.ts
new file mode 100644
index 00000000..edd89e61
--- /dev/null
+++ b/test/main/storage-strict-jsonl.test.ts
@@ -0,0 +1,57 @@
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { syncBuiltinESMExports } from 'node:module';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { readJsonl, readJsonlPage, readJsonlPageWithOffsets } from '../../src/main/storage';
+let root:string,file:string;
+const strict={strict:true};
+const permissionCase=process.platform !== 'win32' && process.getuid?.() !== 0;
+beforeEach(() => { root=fs.mkdtempSync(path.join(os.tmpdir(),'orkas-strict-jsonl-'));file=path.join(root,'records.jsonl'); });
+afterEach(() => { vi.restoreAllMocks();syncBuiltinESMExports();fs.rmSync(root,{recursive:true,force:true}); });
+describe('strict JSONL reads for durable projections',() => {
+ it.each([0,2])('reports a missing source for limit %s while preserving the ordinary empty view',async limit => {
+ await expect(readJsonl(file,limit,strict)).rejects.toMatchObject({code:'ENOENT'});
+ expect(await readJsonl(file,limit)).toEqual([]);
+ });
+ it.each([0,2])('rejects corrupt source rows for limit %s without including source text in the error',async limit => {
+ fs.writeFileSync(file,'{"id":"first"}\nPRIVATE_FIXTURE_CONTENT\n{"id":"last"}\n');
+ await expect(readJsonl(file,limit,strict)).rejects.toThrow('JSONL record is invalid');
+ expect(await readJsonl(file,limit)).toEqual([{id:'first'},{id:'last'}]);
+ });
+ it('accepts valid generic JSON values and blank lines without applying a conversation-specific schema',async () => {
+ fs.writeFileSync(file,' \r\nnull\n0\nfalse\n{"ok":true}\n');
+ expect(await readJsonl(file,0,strict)).toEqual([null,0,false,{ok:true}]);
+ expect(await readJsonl(file,4,strict)).toEqual([null,0,false,{ok:true}]);
+ });
+ it('preserves UTF-8 records spanning chunks, CRLF, blank lines and an unterminated tail',async () => {
+ const rows=[{id:1,text:'中文🚀'.repeat(30_000)},{id:2,text:'tail'}];
+ fs.writeFileSync(file,'\r\n'+JSON.stringify(rows[0])+'\r\n\r\n'+JSON.stringify(rows[1]));
+ expect(await readJsonl(file,2,strict)).toEqual(rows);
+ });
+ it('validates only the selected tail page and propagates strictness to offset-preserving older pages',async () => {
+ fs.writeFileSync(file,'OLDER_PRIVATE_CORRUPTION\n{"id":"latest"}\n');
+ const tail=await readJsonlPage(file,1,undefined,strict);
+ expect(tail.records).toEqual([{id:'latest'}]);expect(tail.nextCursor).toBeGreaterThan(0);
+ await expect(readJsonlPageWithOffsets(file,1,tail.nextCursor,strict)).rejects.toThrow('JSONL record is invalid');
+ });
+ it('rejects an actual short read after truncation and closes its owned file handle',async () => {
+ fs.writeFileSync(file,'{"id":"before-truncation"}\n');
+ const open=fs.promises.open.bind(fs.promises);let close:ReturnType<typeof vi.fn>|undefined;
+ vi.spyOn(fs.promises,'open').mockImplementation(async (...args:any[]) => {
+ const handle=await (open as any)(...args);if(String(args[0]) !== file) return handle;
+ const stat=handle.stat.bind(handle),finish=handle.close.bind(handle);
+ handle.stat=async () => { const result=await stat();fs.truncateSync(file,0);return result; };
+ close=vi.fn(finish);handle.close=close;return handle;
+ });
+ syncBuiltinESMExports();
+ await expect(readJsonlPageWithOffsets(file,1,undefined,strict)).rejects.toThrow('JSONL read is incomplete');
+ expect(close).toHaveBeenCalledOnce();
+ });
+ it.runIf(permissionCase).each([0,2])('reports native read denial for limit %s while retaining the source',async limit => {
+ const source='{"id":"protected"}\n';fs.writeFileSync(file,source);fs.chmodSync(file,0);
+ try { await expect(readJsonl(file,limit,strict)).rejects.toMatchObject({code:'EACCES'}); }
+ finally { fs.chmodSync(file,0o600); }
+ expect(fs.readFileSync(file,'utf8')).toBe(source);
+ });
+});
diff --git a/test/main/util/local-secret-store.test.ts b/test/main/util/local-secret-store.test.ts
new file mode 100644
index 00000000..9c37a514
--- /dev/null
+++ b/test/main/util/local-secret-store.test.ts
@@ -0,0 +1,50 @@
+import { describe, expect, it } from 'vitest';
+import * as vault from '../../../src/main/util/crypto-vault';
+import {
+ decryptLocalSecret, decryptLocalSecretWithMeta, encryptLocalSecret,
+ isEncryptedSecret, shouldRewriteLocalSecret,
+} from '../../../src/main/util/local-secret-store';
+
+const context = { namespace: 'fixture', ownerId: 'fixture-user', recordId: 'fixture-record' };
+
+describe('local secret facade and compatible legacy reads', () => {
+ it('uses the local vault and keeps owner and record context authoritative', () => {
+ const payload = encryptLocalSecret(context, 'synthetic-secret');
+ expect(vault.isEncryptedPayload(payload)).toBe(true);
+ expect(isEncryptedSecret(payload)).toBe(true);
+ expect(decryptLocalSecretWithMeta(context, payload)).toEqual({ plaintext: 'synthetic-secret', kind: 'context' });
+ expect(decryptLocalSecret(context, payload)).toBe('synthetic-secret');
+ expect(shouldRewriteLocalSecret('context')).toBe(false);
+ for (const changed of [{ namespace: 'other' }, { ownerId: 'other' }, { recordId: 'other' }]) {
+ expect(() => decryptLocalSecret({ ...context, ...changed }, payload, { legacySeeds: ['fixture-user'] })).toThrow();
+ }
+ });
+
+ it('reads context and explicitly selected legacy vaults without accepting plaintext or a wrong seed', () => {
+ const seed = `${context.namespace}\0${context.ownerId}\0${context.recordId}`;
+ const current = vault.encrypt(seed, 'context-fixture');
+ expect(decryptLocalSecretWithMeta(context, current)).toEqual({ plaintext: 'context-fixture', kind: 'context' });
+ expect(shouldRewriteLocalSecret('context')).toBe(false);
+ const legacy = vault.encrypt('legacy-fixture', 'legacy-secret');
+ expect(isEncryptedSecret(legacy)).toBe(true);
+ expect(() => decryptLocalSecret(context, legacy)).toThrow();
+ expect(decryptLocalSecretWithMeta(context, legacy, { legacySeeds: ['', 'wrong', 'legacy-fixture', 'legacy-fixture'] }))
+ .toEqual({ plaintext: 'legacy-secret', kind: 'legacy' });
+ expect(shouldRewriteLocalSecret('legacy')).toBe(true);
+ for (const payload of ['plaintext-secret', 'ORKLSEC1:corrupt', vault.encrypt('wrong', 'fixture')]) {
+ expect(() => decryptLocalSecret(context, payload)).toThrow();
+ }
+ expect(isEncryptedSecret(null)).toBe(false); expect(isEncryptedSecret('plaintext-secret')).toBe(false);
+ // A recognized private envelope must fail closed without its private backend.
+ expect(isEncryptedSecret('ORKLSEC1:corrupt')).toBe(true);
+ expect(() => decryptLocalSecret(context, 'ORKLSEC1:corrupt')).toThrow(/hosted backend unavailable/);
+ });
+
+ it('rejects missing context before attempting any secret operation', () => {
+ for (const key of ['namespace', 'ownerId', 'recordId']) {
+ const invalid = { ...context, [key]: '' };
+ expect(() => encryptLocalSecret(invalid, 'fixture')).toThrow(/missing context/);
+ expect(() => decryptLocalSecret(invalid, 'fixture')).toThrow(/missing context/);
+ }
+ });
+});
diff --git a/test/main/util/migrate-source-data-root.test.ts b/test/main/util/migrate-source-data-root.test.ts
new file mode 100644
index 00000000..71ea7180
--- /dev/null
+++ b/test/main/util/migrate-source-data-root.test.ts
@@ -0,0 +1,75 @@
+import { afterEach, describe, expect, it } from 'vitest';
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import vm from 'node:vm';
+import { createRequire } from 'node:module';
+
+const require = createRequire(import.meta.url);
+const roots: string[] = [];
+afterEach(() => { for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true }); });
+function load(file: string, overrides: Record<string, unknown>) {
+ const filename = path.resolve(file), module = { exports: {} as any };
+ vm.runInNewContext(fs.readFileSync(filename, 'utf8'), {
+ module, exports: module.exports, __dirname: path.dirname(filename),
+ require: (name: string) => name in overrides ? overrides[name] : require(name),
+ process: overrides.process || process, Buffer,
+ }, { filename });
+ return module.exports;
+}
+function fixture() {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-source-migration-')); roots.push(root);
+ const repo = path.join(root, 'source'), container = path.join(root, 'container');
+ const src = path.join(repo, 'data/users.json'), dst = path.join(container, 'data/users.json');
+ for (const [file, uid] of [[src, 'source-user'], [dst, 'target-user']]) {
+ fs.mkdirSync(path.dirname(file), { recursive: true });
+ fs.writeFileSync(file, JSON.stringify({ current_user_id: uid, users: [{ user_id: uid, created_at: '2026-01-01' }] }));
+ }
+ return { root, repo, container, src, dst };
+}
+describe('source-run boot migration continuity', () => {
+ for (const fault of ['partial-write', 'publish']) it(`preserves both registries and retries after ${fault}`, () => {
+ const f = fixture(), sourceBefore = fs.readFileSync(f.src, 'utf8'), targetBefore = fs.readFileSync(f.dst, 'utf8');
+ let fail = true, injected = false;
+ const fakeFs = { ...fs,
+ writeFileSync(file: any, body: any, opts: any) {
+ if (fail && fault === 'partial-write' && path.dirname(String(file)) === path.dirname(f.dst)
+ && path.basename(String(file)).startsWith('users.json')) {
+ injected = true; fs.writeFileSync(file, '{"partial":', opts); throw new Error('synthetic disk failure');
+ }
+ return fs.writeFileSync(file, body, opts);
+ },
+ renameSync(from: any, to: any) {
+ if (fail && fault === 'publish' && String(to) === f.dst) { injected = true; throw new Error('synthetic disk failure'); }
+ return fs.renameSync(from, to);
+ },
+ };
+ const migration = load('src/main/util/migrate-source-data-root.cjs', { 'node:fs': fakeFs });
+ expect(() => migration.migrateSourceDataRoot(f.container, f.repo)).toThrow('synthetic disk failure');
+ expect(injected).toBe(true);
+ expect(fs.readFileSync(f.src, 'utf8')).toBe(sourceBefore);
+ expect(fs.readFileSync(f.dst, 'utf8')).toBe(targetBefore);
+ expect(fs.readdirSync(path.dirname(f.dst))).toEqual(['users.json']);
+ fail = false; migration.migrateSourceDataRoot(f.container, f.repo);
+ const merged = JSON.parse(fs.readFileSync(f.dst, 'utf8'));
+ expect(merged.users.map((user: any) => user.user_id).sort()).toEqual(['source-user', 'target-user']);
+ expect(merged.current_user_id).toBe('source-user');
+ expect(fs.existsSync(f.src)).toBe(false);
+ expect(fs.existsSync(path.join(path.dirname(f.dst), '.migrated-from-source'))).toBe(true);
+ migration.migrateSourceDataRoot(f.container, f.repo);
+ expect(JSON.parse(fs.readFileSync(f.dst, 'utf8'))).toEqual(merged);
+ });
+ it('keeps early boot diagnostics free of private source paths and error text', () => {
+ const f = fixture(), output: string[] = [], env: Record<string, string> = {};
+ const root = load('src/main/install-data-root.cjs', {
+ 'node:os': { ...os, homedir: () => f.root },
+ './util/migrate-source-data-root.cjs': { migrateSourceDataRoot() { throw new Error('EACCES /private/person/personal-file.json'); } },
+ process: { platform: 'darwin', env, stderr: { write: (message: string) => output.push(message) } },
+ });
+ const diagnostics: string[] = []; root.flushEarlyDiagnostics((value: string) => diagnostics.push(value));
+ expect(diagnostics).toEqual(['[install-data-root] source-run data migration failed']);
+ expect(output).toEqual(['[install-data-root] source-run data migration failed\n']);
+ expect(env.ORKAS_WORKSPACE_ROOT).toBe(path.join(f.root, '.orkas/data'));
+ root.flushEarlyDiagnostics((value: string) => diagnostics.push(value)); expect(diagnostics).toHaveLength(1);
+ });
+});