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.
JavaScript agent-collaborationagent-harnessagent-orchestrationagent-teamagent-workflowai-assistantai-automationai-employeeai-productivityai-teamai-workflowautonomous-agentsclaude-code-guiclaude-coworkdesktop-agentlocal-firstmulti-agentmulti-agent-systemself-evolving-agentstask-automation
Read BOM-prefixed JSON without changing stored content
CloudTianTian7965edfb579e
diff --git a/src/main/storage.ts b/src/main/storage.tsindex c1e0e826..1b83f7c0 100644--- a/src/main/storage.ts+++ b/src/main/storage.ts@@ -62,7 +62,8 @@ export async function readJson<T = Record<string, any>>(): Promise<T> {try {const text = await fsp.readFile(filePath, 'utf8');- return JSON.parse(text) as T;+ // A single leading UTF-8 BOM is an encoding marker, not JSON content.+ return JSON.parse(text.charCodeAt(0) === 0xFEFF ? text.slice(1) : text) as T;} catch (error) {if (opts?.strict) throw error;return {} as T;@@ -72,7 +73,8 @@ export async function readJson<T = Record<string, any>>(/** Synchronous variant — used in startup paths before the event loop is hot. */export function readJsonSync<T = Record<string, any>>(filePath: string): T {try {- return JSON.parse(fs.readFileSync(filePath, 'utf8')) as T;+ const text = fs.readFileSync(filePath, 'utf8');+ return JSON.parse(text.charCodeAt(0) === 0xFEFF ? text.slice(1) : text) as T;} catch {return {} as T;}diff --git a/test/main/features/agents.test.ts b/test/main/features/agents.test.tsindex 1b349199..7d8d4d57 100644--- a/test/main/features/agents.test.ts+++ b/test/main/features/agents.test.ts@@ -1731,6 +1731,18 @@ describe('agents › createAgentFromBlocks', () => {});describe('agents › listAgents', () => {+ it.each(['custom', 'marketplace'])('loads a BOM-prefixed Agent from %s without changing its definition', async (source) => {+ const dir = path.join(tmpDir, TEST_UID, source === 'custom' ? 'cloud' : 'local/marketplace', 'agents', 'bom-agent');+ fs.mkdirSync(dir, { recursive: true });+ const file = path.join(dir, 'agent.json');+ const bytes = Buffer.from('\uFEFF' + JSON.stringify({ agent_id: 'bom-agent', name: 'Fixture', workflow: 'Preserve this workflow' }));+ fs.writeFileSync(file, bytes);+ const a = await loadAgents();+ expect(await a.listAgentSummaries()).toMatchObject([{ agent_id: 'bom-agent', name: 'Fixture', source }]);+ expect(await a.listAgents()).toMatchObject([{ agent_id: 'bom-agent', workflow: 'Preserve this workflow', source }]);+ expect(fs.readFileSync(file)).toEqual(bytes);+ });+it('returns empty when both dirs are missing', async () => {const a = await loadAgents();expect(await a.listAgents()).toEqual([]);@@ -3203,7 +3215,10 @@ describe('agents › list cache invalidation', () => {writeCustomAgent('a-healthy', { name: 'Healthy' });writeCustomAgent('z-recoverable', { name: 'Recoverable' });const file = path.join(customAgentsDir(), 'z-recoverable', 'agent.json');- const source = fs.readFileSync(file, 'utf8');+ // Compatibility with an encoding marker must not turn a failed scan into+ // a partial success; restoring these exact bytes must be enough to recover.+ const source = '\uFEFF' + fs.readFileSync(file, 'utf8');+ fs.writeFileSync(file, source);const a = await loadAgents();const paths = await import('../../../src/main/paths');const cache = paths.userAgentCatalogCacheFile(TEST_UID);@@ -3228,9 +3243,11 @@ describe('agents › list cache invalidation', () => {fs.writeFileSync(file, source);}expect((await a.listAgents()).map(agent => agent.agent_id).sort()).toEqual(['a-healthy', 'z-recoverable']);+ expect(fs.readFileSync(file, 'utf8')).toBe(source);vi.resetModules();(await import('../../../src/main/features/users')).activateUser(TEST_UID);expect((await (await loadAgents()).listAgents()).map(agent => agent.agent_id).sort()).toEqual(['a-healthy', 'z-recoverable']);+ expect(fs.readFileSync(file, 'utf8')).toBe(source);});it('reuses the persisted catalog after a module restart and honors force invalidation', async () => {diff --git a/test/main/storage.test.ts b/test/main/storage.test.tsindex f3dfbff1..7716206f 100644--- a/test/main/storage.test.ts+++ b/test/main/storage.test.ts@@ -115,6 +115,28 @@ describe('storage › exclusive file publication', () => {});describe('storage › JSON IO', () => {+ it.each(['\n', '\r\n'])('reads one leading UTF-8 BOM without changing JSON values or source bytes (%j)', async (newline) => {+ const file = path.join(tmpDir, 'bom.json');+ const data = { name: 'Fixture', text: 'inside\uFEFFvalue', nested: [false, 3] };+ const bytes = Buffer.from('\uFEFF' + JSON.stringify(data, null, 2).replace(/\n/g, newline));+ fs.writeFileSync(file, bytes);+ expect(await readJson(file, { strict: true })).toEqual(data);+ expect(await readJson(file)).toEqual(data);+ expect(readJsonSync(file)).toEqual(data);+ expect(fs.readFileSync(file)).toEqual(bytes);+ });++ it('does not repair malformed JSON or consume repeated BOMs', async () => {+ const file = path.join(tmpDir, 'invalid-bom.json');+ for (const text of ['\uFEFF{broken', '\uFEFF\uFEFF{"name":"Fixture"}']) {+ fs.writeFileSync(file, text);+ await expect(readJson(file, { strict: true })).rejects.toBeInstanceOf(SyntaxError);+ expect(await readJson(file)).toEqual({});+ expect(readJsonSync(file)).toEqual({});+ expect(fs.readFileSync(file, 'utf8')).toBe(text);+ }+ });+it('strict reads preserve read failures and parse failures while tolerant callers retain compatibility', async () => {const missing = path.join(tmpDir, 'missing-strict.json');await expect(readJson(missing, { strict: true })).rejects.toMatchObject({ code: 'ENOENT' });@@ -127,6 +149,26 @@ describe('storage › JSON IO', () => {expect(await readJson(file, { strict: true })).toEqual({ agents: ['kept'] });});+ it.each(['EACCES', 'EIO'])('preserves %s on BOM files and reads them after the fault clears', async (code) => {+ const file = path.join(tmpDir, 'denied-bom.json');+ const bytes = Buffer.from('\uFEFF{"name":"Preserved fixture"}');+ fs.writeFileSync(file, bytes);+ const error = Object.assign(new Error('Controlled read failure'), { code });+ const asyncRead = vi.spyOn(nativeFs.promises, 'readFile').mockRejectedValue(error);+ const syncRead = vi.spyOn(nativeFs, 'readFileSync').mockImplementation(() => { throw error; });+ syncBuiltinESMExports();+ try {+ await expect(readJson(file, { strict: true })).rejects.toMatchObject({ code });+ expect(await readJson(file)).toEqual({});+ expect(readJsonSync(file)).toEqual({});+ } finally {+ asyncRead.mockRestore(); syncRead.mockRestore(); syncBuiltinESMExports();+ }+ expect(fs.readFileSync(file)).toEqual(bytes);+ expect(await readJson(file, { strict: true })).toEqual({ name: 'Preserved fixture' });+ expect(readJsonSync(file)).toEqual({ name: 'Preserved fixture' });+ });+it('writeJson + readJson roundtrip preserves UTF-8 and structure', async () => {const p = path.join(tmpDir, 'nested', 'a.json');const data = { x: 1, 中: '文', arr: [1, 2, 3], nested: { a: true } };