Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes (scope-source races, write-source guard)
garrytan794750e789ed
diff --git a/src/commands/auth.ts b/src/commands/auth.tsindex 3579944cd8..4fca72c412 100644--- a/src/commands/auth.ts+++ b/src/commands/auth.ts@@ -793,7 +793,7 @@ async function registerClient(name: string, args: string[]) {const columns = parsed.tokenTtlSeconds !== undefined? await preflightOauthClientColumns(sql): undefined;- const registered = await registerScopedClient(sql, name, parsed, { columns });+ const registered = await engine.transaction(tx => registerScopedClient(sqlQueryForEngine(tx), name, parsed, { columns }));const bound = binding.patch ? await bindCredential(engine, { kind: 'client', id: registered.clientId }, binding.patch) : null;if (registered.skipped?.tokenTtl) {console.error('Note: this brain predates the token_ttl column; run `gbrain apply-migrations --yes`, then rescope. The server default TTL applies.');diff --git a/src/core/access/attest-sources.ts b/src/core/access/attest-sources.tsindex 37687f314f..57e8001295 100644--- a/src/core/access/attest-sources.ts+++ b/src/core/access/attest-sources.ts@@ -106,12 +106,17 @@ async function credentialSources(sql: SqlQuery): Promise<CredentialSources[]> {* being written (null for one not inserted yet), whose stored attest_sources are honoured. `default` is never a scope* source (applyAttestSources refuses it), so a default-only grant reads nothing.*/-export async function assertScopeSourceGrant(db: Db, self: CredentialRef | null, reads: readonly string[], opts: { privatePages?: boolean } = {}): Promise<void> {+export async function assertScopeSourceGrant(db: Db, self: CredentialRef | null, reads: readonly string[], opts: { privatePages?: boolean; writeSource?: string | null } = {}): Promise<void> {const sql = sqlOf(db);- if (opts.privatePages && self && (await credentialSources(sql)).find(c => same(c.ref, self))?.attest.length) {+ const attest = self && (opts.privatePages || opts.writeSource) ? (await credentialSources(sql)).find(c => same(c.ref, self))?.attest ?? [] : [];+ if (opts.privatePages && attest.length) {throw new GrantError('invalid_grant', 'This credential lists scope sources (--attest-sources), so it cannot hold the private-pages grant; nothing changed. '+ 'A scope credential reads only shared memory: use a separate credential for private pages.');}+ if (opts.writeSource && attest.includes(opts.writeSource)) {+ throw new GrantError('invalid_grant', `Source ${opts.writeSource} is in this credential's --attest-sources, so it cannot be its write source (where member pages live); nothing changed. `+ + 'Keep member pages and each scope in separate sources: pick another write source.');+ }const candidates = reads.filter(s => s !== 'default');if (!candidates.length) return;const marked = await markedScopeSources(sql, candidates, true);diff --git a/src/core/grants/legacy-token.ts b/src/core/grants/legacy-token.tsindex c980503a8b..be5a64b11b 100644--- a/src/core/grants/legacy-token.ts+++ b/src/core/grants/legacy-token.ts@@ -200,7 +200,7 @@ function schemaError(error: unknown): unknown {async function writeTokenGrant(tx: BrainEngine, row: Record<string, unknown>, grant: TokenGrantAxes, scopes?: string[]): Promise<number> {const { assertScopeSourceGrant, sourcesOf } = await import('../access/attest-sources.ts');const before = sourcesOf(grantFromTokenRow(row).sources).reads;- await assertScopeSourceGrant(tx, { kind: 'token', id: String(row.id) }, sourcesOf(grant.sources).reads.filter(s => !before.includes(s)));+ await assertScopeSourceGrant(tx, { kind: 'token', id: String(row.id) }, sourcesOf(grant.sources).reads.filter(s => !before.includes(s)), { writeSource: sourcesOf(grant.sources).write });const [written] = await executeRawJsonb<{ grant_revision: number }>(tx,`UPDATE access_tokens SET source_grant = $2, source_id = $3, federated_read = $4::text[], allowed_operations = $5::text[],diff --git a/src/core/grants/service.ts b/src/core/grants/service.tsindex 8b423696b8..ddedc983d1 100644--- a/src/core/grants/service.ts+++ b/src/core/grants/service.ts@@ -194,7 +194,9 @@ export async function recordFirstConsentInTransaction(db: GrantDatabase, clientIexport async function persistGrant(sql: SqlQuery, before: ClientGrant, after: ClientGrant, actor: string, action: string): Promise<void> {const { assertScopeSourceGrant, clientGrantReads } = await import('../access/attest-sources.ts');const widened = clientGrantReads(after).filter(s => !clientGrantReads(before).includes(s));- await assertScopeSourceGrant(sql, { kind: 'client', id: before.clientId }, widened, { privatePages: after.privatePages === 'granted_sources' && before.privatePages !== 'granted_sources' });+ await assertScopeSourceGrant(sql, { kind: 'client', id: before.clientId }, widened, {+ privatePages: after.privatePages === 'granted_sources' && before.privatePages !== 'granted_sources', writeSource: after.sourcesNone === true ? null : after.sourceId ?? 'default',+ });const result = await sql`WITH updated AS (UPDATE oauth_clients SETdiff --git a/src/core/persistence/authority.ts b/src/core/persistence/authority.tsindex d5ed750384..4829b454f2 100644--- a/src/core/persistence/authority.ts+++ b/src/core/persistence/authority.ts@@ -154,6 +154,9 @@ export async function authorizeWrite(engine: SqlEngine, a: WriteAuthority, operaif (a.actingFor && !Array.isArray(a.actingFor)) {const actor = parseStoredActor(a.actingFor);if (!actor) deny('The accepted attested actor is malformed.');+ // GST-1113: lock the credential row before reading its attest right, so a concurrent removal commits before or after this publication.+ if (lock && a.principal.kind === 'oauth_client') await engine.executeRaw('SELECT 1 FROM oauth_clients WHERE client_id=$1 FOR SHARE', [a.principal.id]);+ if (lock && a.principal.kind === 'legacy_token') await engine.executeRaw('SELECT 1 FROM access_tokens WHERE id=$1 FOR SHARE', [a.principal.id]);await storedActorIdentity(engine, a.principal, actor, a.attestedOp ?? operation, storedPerCallWriteSource(a, slug) ?? undefined);}// GST-1113: the one exception to the owner's single write source (the scope page), re-checked against the live attest_sources above.diff --git a/src/core/token-mint.ts b/src/core/token-mint.tsindex c5e891f322..7d3723c64d 100644--- a/src/core/token-mint.ts+++ b/src/core/token-mint.ts@@ -104,14 +104,20 @@ export async function insertUnifiedToken(engine: BrainEngine, opts: {}): Promise<Array<{ id: string }>> {const scopes = opts.scopes === undefined ? null : `{${opts.scopes.join(',')}}`;const { assertScopeSourceGrant, sourcesOf } = await import('./access/attest-sources.ts');- await assertScopeSourceGrant(engine, null, sourcesOf(opts.grant.sources).reads);+ const reads = sourcesOf(opts.grant.sources).reads;+ const insert = (db: BrainEngine) => db.executeRaw<{ id: string }>(+ `INSERT INTO access_tokens (name, token_hash, scopes, permissions, ${TOKEN_GRANT_COLUMNS.join(', ')}, grant_revision)+ VALUES ($1, $2, $3::text[], $4::jsonb, $5, $6, $7::text[], $8::text[], $9::text[], 1)+ RETURNING id`,+ [opts.name, opts.tokenHash, scopes, permissionsMirror(opts.grant, {}), ...tokenGrantColumnValues(opts.grant)],+ );try {- return await engine.executeRaw<{ id: string }>(- `INSERT INTO access_tokens (name, token_hash, scopes, permissions, ${TOKEN_GRANT_COLUMNS.join(', ')}, grant_revision)- VALUES ($1, $2, $3::text[], $4::jsonb, $5, $6, $7::text[], $8::text[], $9::text[], 1)- RETURNING id`,- [opts.name, opts.tokenHash, scopes, permissionsMirror(opts.grant, {}), ...tokenGrantColumnValues(opts.grant)],- );+ // GST-1113: a grant reading a named source checks it and inserts in one transaction, under the source row locks.+ if (!reads.some(s => s !== 'default')) return await insert(engine);+ return await engine.transaction(async tx => {+ await assertScopeSourceGrant(tx, null, reads);+ return insert(tx);+ });} catch (e) {// isUndefinedColumnError also matches message-shaped variants: some// driver-wrapped errors drop the SQLSTATE code.diff --git a/test/qm-scope-writes.test.ts b/test/qm-scope-writes.test.tsindex 25a8fcbff9..2ec25f94f5 100644--- a/test/qm-scope-writes.test.ts+++ b/test/qm-scope-writes.test.ts@@ -237,6 +237,70 @@ for (const kind of testBackends()) {expect((await markedScopeSources(engine, [SCOPE_A, SCOPE_B])).size).toBe(2);}, 60_000);+ test('a token minted while --attest-sources marks its source never ends up reading the scope (the check and insert commit together)', async () => {+ const RACE = 'qm-scope-race';+ await engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', [RACE]);+ const racer = await register('racer', HOME);+ await bindCredential(engine, { kind: 'client', id: racer.id }, { attestActor: 'qm', attestOps: MEMORY_OPS });+ const original = engine.executeRaw;+ let bind: Promise<Error | null> | null = null;+ engine.executeRaw = (async function (this: BrainEngine, ...args: Parameters<typeof engine.executeRaw>) {+ if (!bind && String(args[0]).includes('INSERT INTO access_tokens')) {+ let settled = false;+ bind = refusal(bindCredential(engine, { kind: 'client', id: racer.id }, { attestSources: [RACE] })).finally(() => { settled = true; });+ for (let i = 0; i < 30 && !settled; i++) await new Promise(r => setTimeout(r, 50));+ }+ return original.apply(this, args);+ }) as typeof engine.executeRaw;+ let minted: Error | null;+ try {+ minted = await refusal(mintLegacyToken(engine, { name: 'race-' + randomUUID().slice(0, 8), scopes: ['read'], takesHolders: ['world'], sourceGrant: [RACE] }));+ } finally {+ engine.executeRaw = original;+ }+ const bound = await bind;+ expect(bind).not.toBeNull();+ expect(minted === null && bound === null).toBe(false);+ if (minted === null) expect((await markedScopeSources(engine, [RACE])).has(RACE)).toBe(false);+ }, 60_000);++ test('publication rechecks attest_sources under the credential lock: a removal that lands first refuses the scope write', async () => {+ const out = await write(alice, [{ fact: 'Lock order canarylockorder', provenance: 'turn 9' }], { source_id: SCOPE_A });+ expect(out.structured!.status).toBe('committed');+ const [row] = await engine.executeRaw<{ authority: any }>('SELECT authority FROM persistence_requests WHERE request_id = $1::uuid', [out.structured!.request_id]);+ const authority = typeof row!.authority === 'string' ? JSON.parse(row!.authority) : row!.authority;+ const original = engine.executeRaw;+ let removed = false;+ engine.executeRaw = (async function (this: BrainEngine, ...args: Parameters<typeof engine.executeRaw>) {+ if (!removed && /FROM oauth_clients WHERE client_id\s*=\s*\$1.*FOR SHARE/s.test(String(args[0]))) {+ removed = true;+ await original.call(this, 'UPDATE oauth_clients SET attest_sources = NULL WHERE client_id = $1', [clients.scopes!.id]);+ }+ return original.apply(this, args);+ }) as typeof engine.executeRaw;+ const ROLLBACK = new Error('rollback');+ let outcome: Error | null = null;+ try {+ await engine.transaction(async tx => {+ outcome = await refusal(authorizeWrite(tx as never, authority, 'remember', 'memory/scope', true));+ throw ROLLBACK;+ }).catch(e => { if (e !== ROLLBACK) throw e; });+ } finally {+ engine.executeRaw = original;+ }+ expect(removed).toBe(true);+ expect((outcome as (Error & { reason?: string }) | null)?.reason).toBe('source_not_attested');+ const [still] = await engine.executeRaw<{ attest_sources: string[] | null }>('SELECT attest_sources FROM oauth_clients WHERE client_id = $1', [clients.scopes!.id]);+ expect(still!.attest_sources).toEqual([SCOPE_A, SCOPE_B]);+ }, 60_000);++ test('a credential cannot take one of its own scope sources as its write source', async () => {+ const refused = await refusal(rescopeClientGrant(engine, clients.scopes!.id, { sourceId: SCOPE_A }, { actor: 'test' }));+ expect(refused?.message).toContain('cannot be its write source');+ const [row] = await engine.executeRaw<{ source_id: string }>('SELECT source_id FROM oauth_clients WHERE client_id = $1', [clients.scopes!.id]);+ expect(row!.source_id).toBe(HOME);+ }, 60_000);+test('a squatted scope page takes no scope write; no other remote writer may write memory/scope', async () => {await importFromContent(engine, 'memory/scope', '---\ntype: note\ntitle: Scope memory\nreadable_by:\n - member:alice-example\n - member:bob-example\n---\n# Scope memory\n', { sourceId: SCOPE_B });const squat = await write(alice, [{ fact: 'Squatted scope canarysquatscope', provenance: 'x' }], { source_id: SCOPE_B });diff --git a/test/token-mint-shared-skills.test.ts b/test/token-mint-shared-skills.test.tsindex 8f0c77df17..b4ba1e8d57 100644--- a/test/token-mint-shared-skills.test.ts+++ b/test/token-mint-shared-skills.test.ts@@ -4,7 +4,8 @@ import type { BrainEngine } from '../src/core/engine.ts';test('legacy self-member issuance stores the explicit operation snapshot without granting editing', async () => {let params: unknown[] = [];- const engine = { executeRaw: async (_sql: string, values: unknown[]) => { params = values; return [{ id: '11111111-1111-4111-8111-111111111111' }]; } } as unknown as BrainEngine;+ const engine = { executeRaw: async (_sql: string, values: unknown[]) => { params = values; return [{ id: '11111111-1111-4111-8111-111111111111' }]; },+ transaction: (fn: (tx: BrainEngine) => Promise<unknown>) => fn(engine) } as unknown as BrainEngine;const minted = await mintLegacyToken(engine, { name: 'fixture-member', takesHolders: ['world'], scopes: ['read', 'skills_member_self'],sourceGrant: ['workspace'], allowedOperations: ['join_brain', 'sync_brain_skills', 'leave_brain', 'get_skill', 'get_skill'] });expect(minted.scopes).toEqual(['read', 'skills_member_self']);diff --git a/test/token-mint.test.ts b/test/token-mint.test.tsindex 153d39b2da..a30a980c22 100644--- a/test/token-mint.test.ts+++ b/test/token-mint.test.ts@@ -24,6 +24,8 @@ beforeAll(async () => {// original schema) + the v38 permissions column + the F3 grant columns.await engine.executeRaw(PRE_GRANT_COLUMNS_TABLE);for (const statement of ACCESS_TOKEN_GRANT_SCHEMA_SQL.split(";").map(s => s.trim()).filter(Boolean)) await engine.executeRaw(statement);+ // GST-1113: a grant naming a source is checked against sources (QM scope sources) before it is minted.+ await engine.executeRaw('CREATE TABLE IF NOT EXISTS sources (id TEXT PRIMARY KEY, config JSONB)');}, 60_000);const PRE_GRANT_COLUMNS_TABLE = `