Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
End notice reads on write and sync paths at a one-second bound, so a held pages lock cannot stall a sync behind the notice
garrytan35ad5e38e3aa
diff --git a/src/core/search/ignored-privacy-labels.ts b/src/core/search/ignored-privacy-labels.tsindex 83a7d750b0..6aafdf0d56 100644--- a/src/core/search/ignored-privacy-labels.ts+++ b/src/core/search/ignored-privacy-labels.ts@@ -25,6 +25,8 @@ const DISPLAY_CHARS = 80;const NOTICE_VALUES = 5;/** Pages a bulk run's notice or the doctor check reads at most. */export const SCAN_CAP = 100_000;+/** A notice read on a write or sync path ends on the server after this long, and a read it ends gives no notice. */+export const NOTICE_READ_TIMEOUT_MS = 1_000;const LABEL_LINE_RE = new RegExp(`^\\s*["']?(?:${PRIVACY_LABEL_KEYS.join('|')})["']?\\s*:`, 'im');export interface IgnoredPrivacyLabel { key: string; value: string }@@ -85,7 +87,7 @@ export async function sourceHasRepo(engine: Pick<BrainEngine, 'executeRaw'>, souconst [row] = await engine.executeRaw<{ repo: boolean }>(`SELECT (COALESCE(s.local_path, '') <> '' OR s.config->>'canonical' = 'git'OR (s.id = 'default' AND EXISTS (SELECT 1 FROM config c WHERE c.key = 'sync.repo_path' AND COALESCE(c.value, '') <> ''))) AS repo- FROM sources s WHERE s.id = $1`, [sourceId]);+ FROM sources s WHERE s.id = $1`, [sourceId], { timeoutMs: NOTICE_READ_TIMEOUT_MS });return row ? row.repo === true : false;} catch {return true;@@ -157,7 +159,7 @@ export async function ignoredPrivacyLabelRunNotice(engine: Pick<BrainEngine, 'exWHERE p.source_id = $1 AND ${'slugs' in scope ? 'p.slug = ANY($2::text[])' : 'p.updated_at >= $2::timestamptz'} AND p.deleted_at IS NULLAND NOT (p.frontmatter ? 'readable_by') AND p.folder_rule_ids IS NULLAND ${privacyLabelKeySql('p')} AND ${ignoredPrivacyLabelSql('p')}- LIMIT ${SCAN_CAP}`, [sourceId, 'slugs' in scope ? [...new Set(scope.slugs)] : scope.since]);+ LIMIT ${SCAN_CAP}`, [sourceId, 'slugs' in scope ? [...new Set(scope.slugs)] : scope.since], { timeoutMs: NOTICE_READ_TIMEOUT_MS });const tally = tallyIgnoredLabels(rows.map(row => ignoredPrivacyLabels(row)));if (!tally.pages) return null;return ignoredPrivacyLabelNotice({ ...tally, repo: await sourceHasRepo(engine, sourceId), sourceId, run });@@ -179,7 +181,9 @@ export async function withIgnoredPrivacyLabelNotice<R>(ctx: Pick<OperationContexconst written = (result ?? {}) as Record<string, unknown>;const stored = typeof written.slug === 'string' ? written.slug : String(params.slug);const committed = written.status === 'created_or_updated' || written.status === 'skipped';- const page = committed ? await ctx.engine.getPage(stored, { sourceId }) : null;+ const [page] = committed ? await ctx.engine.executeRaw<{ type: string | null; frontmatter: Record<string, unknown> | null }>(+ 'SELECT type, frontmatter FROM pages WHERE source_id = $1 AND slug = $2 AND deleted_at IS NULL', [sourceId, stored],+ { timeoutMs: NOTICE_READ_TIMEOUT_MS }) : [];const labels = ignoredPrivacyLabels(page ?? parseMarkdown(content, `${stored}.md`));if (labels.length) ctx.emitNotice(ignoredPrivacyLabelNotice({ labels: labels.map(l => ({ ...l, pages: 1 })), pages: 1,repo: await sourceHasRepo(ctx.engine, sourceId), sourceId, slug: stored }));diff --git a/test/e2e/ignored-privacy-label-notice-lock-postgres.test.ts b/test/e2e/ignored-privacy-label-notice-lock-postgres.test.tsnew file mode 100644index 0000000000..c11de3bff6--- /dev/null+++ b/test/e2e/ignored-privacy-label-notice-lock-postgres.test.ts@@ -0,0 +1,80 @@+/**+ * GST-2462 on Postgres: the ignored-privacy-label notice reads that run on write and sync paths end on the server.+ * `gbrain sync` reads the pages a run stored for its end-of-run notice (`ignoredPrivacyLabelRunNotice`), and put_page+ * reads the stored page back (`withIgnoredPrivacyLabelNotice`). As plain autocommit statements they waited on+ * `LOCK TABLE pages IN ACCESS EXCLUSIVE MODE` held in another session for as long as the lock lasted, so a sync stalled+ * past its preparation budget behind a notice (#8933's full E2E, persistence-preparation-engine-reads-lock-postgres).+ *+ * Value: protects=NOTICE_READ_TIMEOUT_MS on every notice read; fails_when=a notice read is still waiting on the lock+ * after the bound plus slack, or the run notice / put_page result does not come back while the lock is held;+ * seam=none (the real functions against a real Postgres, the lock taken in a second session).+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import postgres from '#postgres';+import { isolatedPersistencePostgres } from '../helpers/persistence-postgres.ts';+import type { BrainEngine } from '../../src/core/engine.ts';+import type { Notice } from '../../src/core/agent-output.ts';+import { ignoredPrivacyLabelRunNotice, NOTICE_READ_TIMEOUT_MS, withIgnoredPrivacyLabelNotice } from '../../src/core/search/ignored-privacy-labels.ts';+import { hasDatabase } from './helpers.ts';++let engine: BrainEngine | undefined;+let databaseUrl = '';+let closePostgres: (() => Promise<void>) | undefined;+const SLACK_MS = 3_000;+const LIMIT_MS = NOTICE_READ_TIMEOUT_MS + SLACK_MS;++beforeAll(async () => {+ if (!hasDatabase()) return;+ const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!, 'instance', 4);+ engine = pg.engine; closePostgres = pg.close; databaseUrl = pg.databaseUrl;+}, 120_000);+afterAll(async () => { await closePostgres?.(); });++/** Runs `work` while another session holds `pages` ACCESS EXCLUSIVE; reports whether it came back within LIMIT_MS. */+async function underPagesLock<T>(work: () => Promise<T>): Promise<{ returned: boolean; value?: T; stillWaiting: string[] }> {+ const holder = postgres(databaseUrl, { max: 2, prepare: false, onnotice: () => {} });+ const dropped = Promise.withResolvers<void>();+ const acquired = Promise.withResolvers<void>();+ const transaction = holder.begin(async sql => {+ await sql.unsafe('LOCK TABLE pages IN ACCESS EXCLUSIVE MODE');+ acquired.resolve();+ await dropped.promise;+ }).catch(error => { acquired.reject(error); });+ try {+ await acquired.promise;+ const run = work();+ const outcome = await Promise.race([+ run.then(value => ({ returned: true as const, value })),+ Bun.sleep(LIMIT_MS).then(() => ({ returned: false as const })),+ ]);+ const waiting = Array.from(await holder.unsafe(+ `SELECT left(query, 120) AS query FROM pg_stat_activity+ WHERE datname = current_database() AND pid <> pg_backend_pid() AND wait_event_type = 'Lock'`)) as Array<{ query: string }>;+ dropped.resolve();+ await run.catch(() => undefined);+ return { ...outcome, stillWaiting: waiting.map(row => row.query) };+ } finally {+ dropped.resolve();+ await transaction;+ await holder.end();+ }+}++describe.skipIf(!hasDatabase())('ignored-privacy-label notice reads end at their bound under a held pages lock (Postgres, GST-2462)', () => {+ test('a sync run notice by slug and by time comes back as no notice, and leaves no statement waiting', async () => {+ for (const scope of [{ slugs: ['notes/locked-example'] }, { since: '2000-01-01T00:00:00Z' }]) {+ const result = await underPagesLock(() => ignoredPrivacyLabelRunNotice(engine!, 'default', scope, 'sync'));+ expect({ scope, returned: result.returned, value: result.value ?? null, stillWaiting: result.stillWaiting })+ .toEqual({ scope, returned: true, value: null, stillWaiting: [] });+ }+ }, 60_000);++ test('put_page returns its result with no notice while the stored page cannot be read', async () => {+ const notices: Notice[] = [];+ const written = { slug: 'notes/locked-example', status: 'created_or_updated' };+ const result = await underPagesLock(() => withIgnoredPrivacyLabelNotice({ engine: engine!, emitNotice: n => { notices.push(n); } }, 'default',+ { slug: 'notes/locked-example', content: '---\naccess: personal\n---\nBody.\n' }, written));+ expect({ returned: result.returned, value: result.value, notices, stillWaiting: result.stillWaiting })+ .toEqual({ returned: true, value: written, notices: [], stillWaiting: [] });+ }, 60_000);+});