Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
access serves several: never overwrite a key file no live key owns (GST-363 review fix)
garrytanb79d995aec4d
diff --git a/scripts/structural-suites.tsv b/scripts/structural-suites.tsvindex 5b16690214..440d585ac6 100644--- a/scripts/structural-suites.tsv+++ b/scripts/structural-suites.tsv@@ -375,7 +375,7 @@ test/serve-http-mcp-transport-cleanup.test.ts POST /mcp transport cleanup (#2844test/serve-http-metrics.test.ts serve-http.ts wiring (structural — the two defects of the original PR) 2 source-surface-helpertest/serve-stdin-eof-drain.test.ts serve-sync-runner stays off the boot path (#4409) 1 readFileSynctest/setup-serves-several.test.ts gbrain setup --serves (spawned, PGLite) 7 readFileSync-test/setup-serves-several.test.ts labelling refuses or reports what it cannot make safe (pglite, repo-less) 7 readFileSync+test/setup-serves-several.test.ts labelling refuses or reports what it cannot make safe (pglite, repo-less) 8 readFileSynctest/setup.test.ts gbrain setup: setup migrations on the brain it creates (Cat 41 run 4) 3 readFileSynctest/shared-skills-catalog.test.ts (file-level) 22 readFileSynctest/shared-skills-export.serial.test.ts (file-level) 7 readFileSyncdiff --git a/src/commands/access-serves.ts b/src/commands/access-serves.tsindex 307db7ca60..f93468742a 100644--- a/src/commands/access-serves.ts+++ b/src/commands/access-serves.ts@@ -113,10 +113,11 @@ async function plan(engine: BrainEngine, roster: string[], sourceId: string): Prconst path = join(keyDir, `${id}.key`);const [token] = await engine.executeRaw<{ member_id: string | null; token_hash: string }>('SELECT member_id, token_hash FROM access_tokens WHERE name = $1 AND revoked_at IS NULL', [name]);if (members.find(m => m.id === id)?.action === 'blocked') keys.push({ member: id, token_name: name, path, action: 'blocked', detail: 'the member is not enabled' });+ else if (!token && existsSync(path)) keys.push({ member: id, token_name: name, path, action: 'blocked', detail: `a key file is already at ${path} but no live key is named ${name} (another brain's key, or one revoked by hand); move or delete the file, then run this again` });else if (!token) keys.push({ member: id, token_name: name, path, action: 'mint' });else if (token.member_id !== id) keys.push({ member: id, token_name: name, path, action: 'blocked', detail: `a token named ${name} exists but is not bound to ${id}; revoke it (gbrain auth revoke ${name}) or bind it (gbrain auth rescope --token ${name} --member ${id})` });else if (!existsSync(path)) keys.push({ member: id, token_name: name, path, action: 'blocked', detail: `the key ${name} exists but its file is gone and a key is shown only once; revoke it (gbrain auth revoke ${name}) and run this again to mint a new one` });- else if (hashToken(readFileSync(path, 'utf8').trim()) !== token.token_hash) keys.push({ member: id, token_name: name, path, action: 'blocked', detail: `the file does not hold the live key ${name} (it was revoked and made again, or another brain wrote it); revoke it (gbrain auth revoke ${name}) and run this again to mint a new one` });+ else if (hashToken(readFileSync(path, 'utf8').trim()) !== token.token_hash) keys.push({ member: id, token_name: name, path, action: 'blocked', detail: `the file does not hold the live key ${name} (it was revoked and made again, or another brain wrote it); revoke it (gbrain auth revoke ${name}), delete ${path} and run this again to mint a new one` });else keys.push({ member: id, token_name: name, path, action: 'keep' });}return { serves: 'several', source_id: sourceId, applied: false, config: { key: SERVES_KEY, value: 'several', action: explicit === 'several' ? 'keep' : 'write' },diff --git a/test/setup-serves-several.test.ts b/test/setup-serves-several.test.tsindex 76b3b99e94..d17ab75b04 100644--- a/test/setup-serves-several.test.ts+++ b/test/setup-serves-several.test.ts@@ -227,14 +227,25 @@ describe('labelling refuses or reports what it cannot make safe (pglite, repo-leexpect(r.keys[0]!.detail).toContain('does not hold the live key member-nia-example');}, 60_000);+ test('a key file left by another brain or a revoked key is BLOCKED, never overwritten', async () => {+ const path = join(home, '.gbrain', 'member-keys', 'sam-example.key');+ mkdirSync(join(home, '.gbrain', 'member-keys'), { recursive: true, mode: 0o700 });+ writeFileSync(path, 'gbrain_anotherbrainskey\n', { mode: 0o600 });+ const r = await label(['sam-example']);+ expect(r.keys[0]).toMatchObject({ action: 'blocked' });+ expect(r.keys[0]!.detail).toContain('move or delete the file');+ expect(readFileSync(path, 'utf8')).toBe('gbrain_anotherbrainskey\n');+ expect(await liveToken('member-sam-example')).toBeUndefined();+ }, 60_000);+test('a key file that cannot be written revokes the key it just minted, and the next run mints a new one', async () => {- const path = join(home, '.gbrain', 'member-keys', 'oli-example.key');- mkdirSync(join(path, 'in-the-way'), { recursive: true });+ const inTheWay = join(home, '.gbrain', 'member-keys', `oli-example.key.${process.pid}.tmp`);+ mkdirSync(join(inTheWay, 'x'), { recursive: true });const r = await label(['oli-example']);expect(r.error).toBeDefined();expect(r.completed).toEqual(['members', 'rules']);expect(await liveToken('member-oli-example')).toBeUndefined();- rmSync(path, { recursive: true });+ rmSync(inTheWay, { recursive: true });const again = await label(['oli-example']);expect(again.error).toBeUndefined();expect(again.keys[0]!.action).toBe('mint');