Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(grants): on-read token conversion skips a locked row

garrytan2aaa407ac529
diff --git a/src/core/grants/legacy-token.ts b/src/core/grants/legacy-token.ts
index d49466c84a..cafbb391f3 100644
--- a/src/core/grants/legacy-token.ts
+++ b/src/core/grants/legacy-token.ts
@@ -332,9 +332,10 @@ export async function migrateLegacyTokens(engine: BrainEngine, opts: { dryRun: b
* migration has not run) is converted on this read: one guarded UPDATE writes
* the columns `migrateLegacyTokens` would write and leaves `permissions` as
* the mirror it already is. The guard (`source_grant IS NULL`) makes
- * concurrent first reads a no-op for the loser, and a failed write (a
- * read-only role, a lost race, a schema without the columns) falls back to
- * the same grant computed in memory. A malformed row is never converted; it
+ * concurrent first reads a no-op for the loser, SKIP LOCKED keeps a row lock
+ * held elsewhere from parking the request (#5730), and a skipped or failed
+ * write (a locked row, a read-only role, a schema without the columns) falls
+ * back to the same grant computed in memory. A malformed row is never converted; it
* stays deny-all.
*/
export async function resolveTokenGrant(sql: SqlQuery, row: Record<string, unknown>): Promise<PrincipalGrant> {
@@ -345,7 +346,7 @@ export async function resolveTokenGrant(sql: SqlQuery, row: Record<string, unkno
const [converted] = await sql`
UPDATE access_tokens SET source_grant = ${kind}, source_id = ${write}, federated_read = ${reads}::text[],
allowed_operations = ${ops}::text[], takes_holders = ${holders}::text[], grant_revision = grant_revision + 1
- WHERE id = ${String(row.id)}::uuid AND source_grant IS NULL
+ WHERE id IN (SELECT id FROM access_tokens WHERE id = ${String(row.id)}::uuid AND source_grant IS NULL FOR UPDATE SKIP LOCKED)
RETURNING *
`;
if (converted) return grantFromTokenRow(converted);
diff --git a/test/fixtures/shared-skills-transport-cases.ts b/test/fixtures/shared-skills-transport-cases.ts
index 93f6063a54..215b9f5c1f 100644
--- a/test/fixtures/shared-skills-transport-cases.ts
+++ b/test/fixtures/shared-skills-transport-cases.ts
@@ -8,6 +8,7 @@ import type { MembershipSnapshot } from '../../src/core/shared-skills/membership
import { sharedSkillKey } from '../../src/core/shared-skills/membership-types.ts';
import { setSharedSkillPolicy } from '../../src/core/shared-skills/policy.ts';
import { disposePersistenceConsumer } from '../../src/core/persistence/service.ts';
+import { parseRescopeTokenArgs, rescopeLegacyToken } from '../../src/core/grants/legacy-token.ts';
import { sharedSkillResourceUri } from '../../src/mcp/skill-resources.ts';
import { resetStrictParamsModeCache } from '../../src/mcp/validate-params.ts';
import { ASSET_PATH, blobResource, call, FULL_POLICY, READ_OPERATIONS, skillFiles, textResource, withTransportFixture } from './shared-skills-transports.ts';
@@ -165,8 +166,12 @@ export function sharedSkillsTransportCases(databaseUrl?: string) {
await expect(reader.readResource({ uri: sharedSkillResourceUri(visible.qualified_id, visible.revision, path) })).rejects.toThrow();
}
expect(JSON.stringify(await reader.readResource({ uri: 'gbrain://skills' }))).not.toContain('Hidden-source canary');
+ // A raw permissions edit (an older gbrain binary) after the token's first read converted it is
+ // grant drift: the operations axis denies everything until an operator adopts one side.
await f.engine.executeRaw('UPDATE access_tokens SET permissions=$2::text::jsonb WHERE id=$1::uuid',
[f.peers.reader.id, JSON.stringify({ source_id: 'default', allowed_operations: ['list_skills'] })]);
+ expect((await reader.listTools()).tools.map(t => t.name)).toEqual([]);
+ await rescopeLegacyToken(f.engine, parseRescopeTokenArgs(['--id', f.peers.reader.id, '--adopt-permissions']));
expect((await reader.listTools()).tools.map(t => t.name)).toEqual(['list_skills']);
await expect(call(reader, 'get_skill', { schema_version: 2, name: 'alpha' })).rejects.toMatchObject({ code: 'permission_denied' });
await expect(reader.readResource({ uri: sharedSkillResourceUri(visible.qualified_id, visible.revision) })).rejects.toThrow();