Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Membership reads still work on a schema behind the build; probe the stale-plan retry on a git-backed source (GST-2314)
garrytana7b164ac54cc
diff --git a/src/core/shared-skills/membership-authority.ts b/src/core/shared-skills/membership-authority.tsindex 88219d0d87..bdd6d8db02 100644--- a/src/core/shared-skills/membership-authority.ts+++ b/src/core/shared-skills/membership-authority.ts@@ -4,7 +4,7 @@ import { sourceScopeOpts } from '../ops/context.ts';import { currentVerifiedLocalWriter, readLocalWriter, verifyLocalWriter, type LocalGrant } from '../persistence/identity.ts';import type { Principal } from '../persistence/model.ts';import { normalizeTokenScopes } from '../legacy-token-scope.ts';-import { authSourcesFromGrant, grantFromTokenRow, TOKEN_GRANT_ROW_SELECT, type TokenGrantRow } from '../grants/model.ts';+import { authSourcesFromGrant, grantFromTokenRow, TOKEN_GRANT_ROW_COLUMNS, type TokenGrantRow } from '../grants/model.ts';import { hasScope } from '../scope.ts';const deny = (): never => {@@ -31,7 +31,11 @@ export async function memberAuthority(ctx: OperationContext, operation: string):grant = row;} else {// GST-2314: named columns, not SELECT *: inside this transaction a plan cached before a migration added a column fails 0A000.- const [row] = await ctx.engine.executeRaw<TokenGrantRow>(`SELECT ${TOKEN_GRANT_ROW_SELECT} FROM access_tokens WHERE id=$1 AND revoked_at IS NULL FOR SHARE`, [principal.id]);+ // No schema admission guards membership, so a schema behind this build reads the columns it has, as SELECT * did.+ const present = new Set((await ctx.engine.executeRaw<{ attname: string }>(+ "SELECT attname FROM pg_attribute WHERE attrelid=to_regclass('access_tokens') AND attnum>0 AND NOT attisdropped")).map(c => c.attname));+ const columns = TOKEN_GRANT_ROW_COLUMNS.filter(c => present.has(c)).join(',');+ const [row] = await ctx.engine.executeRaw<TokenGrantRow>(`SELECT ${columns} FROM access_tokens WHERE id=$1 AND revoked_at IS NULL FOR SHARE`, [principal.id]);if (!row) deny();const scopes = normalizeTokenScopes(row.scopes) ?? [];const tokenGrant = grantFromTokenRow(row);diff --git a/test/access-token-grant-columns.test.ts b/test/access-token-grant-columns.test.tsindex 45d86fa4f8..2199eec196 100644--- a/test/access-token-grant-columns.test.ts+++ b/test/access-token-grant-columns.test.ts@@ -7,7 +7,8 @@* 1. Protects: the publication authority read (FOR SHARE) and the membership authority read, run on every* pooled connection before and after `ALTER TABLE access_tokens ADD COLUMN`, succeed; so does a* legacy-token put_page after the column is added.- * 2. Fails when: either read selects every column of access_tokens inside its transaction.+ * 2. Fails when: either read selects every column of access_tokens inside its transaction, or the membership read+ * (which no schema admission guards) names a column a schema behind this build does not have.* 3. Real seams: the token service, the serve --http token verifier, MCP dispatch and the durable write path.* Each read runs in POOL concurrent transactions held open together, so every pooled connection prepares it* first and every one reuses its cached plan after the column is added.@@ -75,6 +76,18 @@ for (const kind of testBackends()) {expect(await read()).toEqual(Array(POOL).fill(principal));}, 240_000);+ test('the membership authority read works on a schema that predates an access_tokens column it names', async () => {+ const minted = await mintLegacyToken(engine, { name: 'behind', scopes: ['read', 'skills_member_self'], takesHolders: ['world'], allowedOperations: ['join_brain'] });+ const rollback = new Error('roll back the simulated older schema');+ const principal = await engine.transaction(async tx => {+ await tx.executeRaw('ALTER TABLE access_tokens DROP COLUMN private_pages');+ const result = (await memberAuthority({ engine: tx, remote: true, sourceId: 'default',+ auth: { principal: { kind: 'legacy_token', id: minted.id }, scopes: ['read', 'skills_member_self'], allowedOperations: ['join_brain'] } } as unknown as OperationContext, 'join_brain')).principal;+ throw Object.assign(rollback, { result });+ }).catch(error => { if (error !== rollback) throw error; return (error as { result: unknown }).result; });+ expect(principal).toEqual({ kind: 'legacy_token', id: minted.id });+ }, 240_000);+test('a legacy-token put_page after the column is added publishes', async () => {const minted = await mintLegacyToken(engine, { name: 'writer', scopes: ['read', 'write'], takesHolders: ['world'] });const provider = new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });diff --git a/test/persistence-stale-plan-retry.test.ts b/test/persistence-stale-plan-retry.test.tsindex 4248c4ad76..cd2ace7fb5 100644--- a/test/persistence-stale-plan-retry.test.ts+++ b/test/persistence-stale-plan-retry.test.ts@@ -8,21 +8,45 @@* 2. Fails when: the stale-plan error is not a transient interruption (the write ends failed/storage_error, never* claimed again) or admission does not re-run it (the caller gets internal_error).* 3. Real seams: the serve --http token verifier, MCP dispatch, admission, the resident consumer and publication.+ * The worktree-backed arm takes the recovery route (the request holds a recovery record): every page's file lands+ * once, with one Git commit per page, so a stale plan never publishes a page twice.*/import { afterAll, beforeAll, describe, expect, test } from 'bun:test';import { randomUUID } from 'node:crypto';+import { execFileSync } from 'node:child_process';+import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';import type { BrainEngine } from '../src/core/engine.ts';import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { GBrainOAuthProvider } from '../src/core/oauth-provider.ts';import { sqlQueryForEngine } from '../src/core/sql-query.ts';import { mintLegacyToken } from '../src/core/token-mint.ts';import { dispatchToolCall } from '../src/mcp/dispatch.ts';+import { operations, type OperationContext } from '../src/core/operations.ts';+import { claimWorktree } from '../src/core/persistence/ownership.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { runPersistenceEffects } from '../src/core/persistence/effects.ts';+import { localHostId } from '../src/core/persistence/identity.ts';+import { _resetWriteThroughCacheForTest } from '../src/core/write-through.ts';+import { withEnv } from './helpers/with-env.ts';+import { durableGitRepo } from './helpers/git-publication.ts';import type { AuthInfo } from '../src/core/ops/contract.ts';import { testBackends } from './helpers/test-backends.ts';import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';const note = (title: string) => ['---', 'type: note', 'title: ' + title, '---', '# ' + title, '', 'Body of ' + title + '.'].join('\n');const MAX_TRANSIENT_RETRIES = 5;+type Settled = { request_id: string; state: string; error_code: string | null; transient_interruptions: number };+async function settle(engine: BrainEngine, ids: string[]): Promise<Settled[]> {+ const read = () => engine.executeRaw<Settled>(+ 'SELECT request_id::text, state, error_code, transient_interruptions FROM persistence_requests WHERE request_id = ANY($1::uuid[]) ORDER BY request_id', [ids]);+ let rows = await read();+ for (const until = Date.now() + 120_000; Date.now() < until && rows.some(r => r.state !== 'committed' && r.state !== 'failed'); rows = await read()) {+ await new Promise(resolve => setTimeout(resolve, 500));+ }+ return rows;+}for (const kind of testBackends()) {describe('accepted writes after a migration adds a persistence_requests column (' + kind + ')', () => {@@ -34,7 +58,8 @@ for (const kind of testBackends()) {if (kind === 'postgres') ({ engine, close } = await isolatedPersistencePostgres(process.env.DATABASE_URL!, 'instance', POOL));else { engine = new PGLiteEngine(); await engine.connect({ engine: 'pglite' } as never); await engine.initSchema(); close = () => engine.disconnect(); }}, 240_000);- afterAll(async () => { await close?.(); }, 60_000);+ afterAll(async () => { await disposePersistenceConsumer(engine); _resetWriteThroughCacheForTest(); await close?.(); rmSync(home, { recursive: true, force: true }); }, 60_000);+ const home = mkdtempSync(join(tmpdir(), 'gbrain-stale-plan-' + kind + '-'));test('every put_page after the column is added publishes within the transient retry limit', async () => {const minted = await mintLegacyToken(engine, { name: 'writer', scopes: ['read', 'write'], takesHolders: ['world'] });@@ -70,5 +95,56 @@ for (const kind of testBackends()) {expect(rows.map(r => r.state + (r.error_code ? '/' + r.error_code : ''))).toEqual(ids.map(() => 'committed'));expect(Math.max(...rows.map(r => r.transient_interruptions))).toBeLessThanOrEqual(MAX_TRANSIENT_RETRIES);}, 300_000);++ test('on a worktree-backed source every page is published once, with one commit, after the column is added', async () => {+ const brainDir = mkdtempSync(join(home, 'brain-'));+ const git = (...args: string[]) => execFileSync('git', ['-C', brainDir, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });+ writeFileSync(join(brainDir, 'seed.md'), 'seed\n');+ durableGitRepo(brainDir, ['seed.md']);+ const sourceId = 'wt-' + randomUUID().slice(0, 8);+ await disposePersistenceConsumer(engine);+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ await engine.executeRaw('INSERT INTO sources(id,name,local_path) VALUES($1,$1,$2)', [sourceId, brainDir]);+ await withEnv({ GBRAIN_HOME: home }, () => claimWorktree(engine, sourceId, brainDir));+ await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');+ _resetWriteThroughCacheForTest();+ const ctx = { engine, config: { engine: kind, embedding_disabled: true } as never, logger: { info() {}, warn() {}, error() {} },+ dryRun: false, remote: false, sourceId } as unknown as OperationContext;+ const putPage = operations.find(o => o.name === 'put_page')!;+ let n = 0;+ const put = async () => {+ const slug = 'wt/stale-plan-' + (n++);+ const requestId = randomUUID();+ const out = await withEnv({ GBRAIN_HOME: home }, () => putPage.handler(ctx, { slug, content: note(slug), request_id: requestId, wait_ms: 20_000 }))+ .then(result => String((result as { state?: string }).state), (error: { code?: string }) => String(error.code));+ return { slug, requestId, out };+ };+ const warm = [];+ for (let i = 0; i < 4; i++) warm.push(await put());+ warm.push(...await Promise.all(Array.from({ length: POOL }, () => put())));+ expect(warm.map(w => w.out)).toEqual(warm.map(() => 'committed'));++ await engine.executeRaw('ALTER TABLE persistence_requests ADD COLUMN probe_added_worktree_column TEXT');+ const after = [];+ for (let i = 0; i < 6; i++) after.push(await put());+ after.push(...await Promise.all(Array.from({ length: POOL }, () => put())));+ expect(after.map(a => a.out).filter(out => out !== 'committed' && out !== 'write_pending')).toEqual([]);++ const rows = await settle(engine, after.map(a => a.requestId));+ expect(rows.map(r => r.state + (r.error_code ? '/' + r.error_code : ''))).toEqual(after.map(() => 'committed'));+ expect(Math.max(...rows.map(r => r.transient_interruptions))).toBeLessThanOrEqual(MAX_TRANSIENT_RETRIES);++ const pages = [...warm, ...after];+ expect(readdirSync(join(brainDir, 'wt')).sort()).toEqual(pages.map(p => p.slug.slice(3) + '.md').sort());+ for (const p of pages) expect(readFileSync(join(brainDir, p.slug + '.md'), 'utf8')).toContain('Body of ' + p.slug + '.');+ const commits = () => pages.map(p => git('log', '--format=%H', '--', p.slug + '.md').split('\n').filter(Boolean).length);+ let counts = commits();+ for (const until = Date.now() + 120_000; Date.now() < until && counts.some(c => c < 1); counts = commits()) {+ await withEnv({ GBRAIN_HOME: home }, () => runPersistenceEffects(engine, { engine: kind, embedding_disabled: true } as never, { hostId: localHostId(), limit: 50 }));+ }+ await withEnv({ GBRAIN_HOME: home }, () => runPersistenceEffects(engine, { engine: kind, embedding_disabled: true } as never, { hostId: localHostId(), limit: 50 }));+ counts = commits();+ expect(counts).toEqual(pages.map(() => 1));+ }, 300_000);});}diff --git a/test/publication-failure-owner-exception.test.ts b/test/publication-failure-owner-exception.test.tsindex 2e7d3acbbc..45ff3089dd 100644--- a/test/publication-failure-owner-exception.test.ts+++ b/test/publication-failure-owner-exception.test.ts@@ -128,7 +128,8 @@ describe('#5929 owner exceptions keep a bounded identity', () => {describe('#6719 owner_exception diagnostics', () => {test('writer status recent_failures gives an owner exception its own next_action, never the database-trigger hint', async () => {- const { row } = await failingWrite(Object.assign(new Error('cached plan must not change result type'), { code: '0A000' }));+ // GST-2314: the stale-plan 0A000 ("cached plan must not change result type") is a transient retry, so any other 0A000 stands in.+ const { row } = await failingWrite(Object.assign(new Error('FOR SHARE is not allowed with aggregate functions'), { code: '0A000' }));expect(row.error_detail).toMatchObject({ origin: 'owner_exception', errno: '0A000' });const status = await withEnv({ GBRAIN_HOME: join(dir, 'home') }, () => readWriterDiagnostics(engine));const failure = status.recent_failures.find(f => f.request_id === row.request_id)!;