Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: line-grammar toggle scenarios shared by PGLite and the Postgres E2E arm; concurrent toggle converges

garrytan2b87cff55f82
diff --git a/test/e2e/line-grammar-toggle-postgres.test.ts b/test/e2e/line-grammar-toggle-postgres.test.ts
new file mode 100644
index 0000000000..f51562e0cf
--- /dev/null
+++ b/test/e2e/line-grammar-toggle-postgres.test.ts
@@ -0,0 +1,15 @@
+/**
+ * Postgres arm of the line-grammar toggle scenarios: settings-bound link
+ * extraction, the generation row and the one watermark on a real database
+ * (and through transaction-mode PgBouncer when the E2E shard routes there).
+ */
+import { describe, test } from 'bun:test';
+import { concurrentToggleConverges, enableThenDisableConverges, noOpChangesKeepTheGeneration, preparedBeforeChangeStaysStale } from '../helpers/line-grammar-toggle-scenarios.ts';
+
+const url = process.env.DATABASE_URL;
+describe.skipIf(!url)('Postgres line-grammar toggles converge', () => {
+ test('enable then disable re-extracts and follows the setting', () => enableThenDisableConverges(url), 240_000);
+ test('no-op changes keep the generation', () => noOpChangesKeepTheGeneration(url), 180_000);
+ test('a page prepared before a change stays stale', () => preparedBeforeChangeStaysStale(url), 180_000);
+ test('a toggle racing an extraction converges', () => concurrentToggleConverges(url), 240_000);
+});
diff --git a/test/helpers/line-grammar-toggle-scenarios.ts b/test/helpers/line-grammar-toggle-scenarios.ts
new file mode 100644
index 0000000000..393e661610
--- /dev/null
+++ b/test/helpers/line-grammar-toggle-scenarios.ts
@@ -0,0 +1,106 @@
+/**
+ * Line-grammar toggle scenarios shared by the PGLite tests and their Postgres
+ * E2E arm (test/line-grammar-toggle.test.ts, test/e2e/line-grammar-toggle-postgres.test.ts).
+ */
+import { expect } from 'bun:test';
+import { randomUUID } from 'node:crypto';
+import type { BrainEngine } from '../../src/core/engine.ts';
+import type { OperationContext } from '../../src/core/ops/contract.ts';
+import { submitPageMutation } from '../../src/core/persistence/page-mutations.ts';
+import { extractManagedStaleLinks } from '../../src/core/persistence/links-maintenance.ts';
+import { prepareAutomaticLinks } from '../../src/core/persistence/links-preparation.ts';
+import { applyLineGrammarConfigChange, describeLineGrammarChange } from '../../src/core/line-grammar-config.ts';
+import { effectiveLinkExtractorWatermark } from '../../src/core/link-extraction-watermark.ts';
+import { LINK_EXTRACTION_GENERATION_KEY } from '../../src/core/line-grammar.ts';
+import { managedBrain } from './managed-brain.ts';
+
+const put = (ctx: OperationContext, slug: string, body: string, type = 'person') =>
+ submitPageMutation(ctx, { operation: 'put_page', params: { slug, request_id: randomUUID(),
+ content: `---\ntype: ${type}\ntitle: ${slug}\n---\n\n${body}\n` } }) as Promise<Record<string, any>>;
+
+const aliceTypes = async (engine: BrainEngine) => (await engine.executeRaw<{ link_type: string }>(`SELECT DISTINCT l.link_type FROM links l
+ JOIN pages f ON f.id=l.from_page_id JOIN pages t ON t.id=l.to_page_id
+ WHERE f.slug='people/alice-example' AND t.slug='companies/acme-example' ORDER BY 1`)).map(r => r.link_type);
+
+const stale = async (engine: BrainEngine) => engine.countStalePagesForExtraction({ versionTs: await effectiveLinkExtractorWatermark(engine) });
+
+async function seed(engine: BrainEngine, ctx: OperationContext) {
+ await put(ctx, 'companies/acme-example', 'Acme.', 'company');
+ await put(ctx, 'people/alice-example', 'Alice builds things.\n\n- works_at [[companies/acme-example]]');
+ await extractManagedStaleLinks(engine, { mentions: false });
+}
+
+export async function enableThenDisableConverges(databaseUrl?: string) {
+ await managedBrain(async ({ engine, ctx }) => {
+ await seed(engine, ctx);
+ expect(await aliceTypes(engine)).not.toContain('works_at');
+ expect(await stale(engine)).toBe(0);
+
+ const on = await applyLineGrammarConfigChange(engine, tx => tx.setConfig('line_grammar.enabled', 'true'));
+ expect(on.changed).toBe(true);
+ expect(await engine.getConfig(LINK_EXTRACTION_GENERATION_KEY)).toBe(on.generation);
+ expect(await stale(engine)).toBeGreaterThanOrEqual(2);
+ const described = await describeLineGrammarChange(engine, on);
+ expect(described.lines.join('\n')).toContain('gbrain extract --stale');
+ expect(described.json).toMatchObject({ changed: true, effective: { enabled: true } });
+ await extractManagedStaleLinks(engine, { mentions: false });
+ expect(await stale(engine)).toBe(0);
+ expect(await aliceTypes(engine)).toContain('works_at');
+
+ const off = await applyLineGrammarConfigChange(engine, tx => tx.setConfig('line_grammar.enabled', 'false'));
+ expect(off.changed).toBe(true);
+ expect(Date.parse(off.generation!)).toBeGreaterThanOrEqual(Date.parse(on.generation!));
+ expect(await stale(engine)).toBeGreaterThanOrEqual(2);
+ await extractManagedStaleLinks(engine, { mentions: false });
+ expect(await stale(engine)).toBe(0);
+ expect(await aliceTypes(engine)).not.toContain('works_at');
+ }, { databaseUrl });
+}
+
+export async function noOpChangesKeepTheGeneration(databaseUrl?: string) {
+ await managedBrain(async ({ engine, ctx }) => {
+ await seed(engine, ctx);
+ for (const mutate of [
+ (tx: BrainEngine) => tx.setConfig('line_grammar.enabled', 'false'),
+ (tx: BrainEngine) => tx.setConfig('line_grammar.enabled', 'off'),
+ async (tx: BrainEngine) => { await tx.unsetConfig('line_grammar.enabled'); },
+ (tx: BrainEngine) => tx.setConfig('line_grammar.effective_ranges', 'false'),
+ (tx: BrainEngine) => tx.setConfig('line_grammar.allow_undeclared_types', 'true'),
+ ]) {
+ const change = await applyLineGrammarConfigChange(engine, mutate);
+ expect(change.changed).toBe(false);
+ }
+ expect(await engine.getConfig(LINK_EXTRACTION_GENERATION_KEY)).toBeNull();
+ expect(await stale(engine)).toBe(0);
+ }, { databaseUrl });
+}
+
+export async function preparedBeforeChangeStaysStale(databaseUrl?: string) {
+ await managedBrain(async ({ engine, ctx }) => {
+ await seed(engine, ctx);
+ const snapshot = await engine.readPageSnapshot('people/alice-example', { sourceId: 'default' });
+ const prepared = await prepareAutomaticLinks(engine, 'people/alice-example', snapshot!.page, 'default');
+ expect(prepared.settings?.enabled).toBe(false);
+ await applyLineGrammarConfigChange(engine, tx => tx.setConfig('line_grammar.enabled', 'true'));
+ const written = await engine.transaction(tx => prepared.apply(tx));
+ expect(written.errors).toBe(1);
+ expect(await stale(engine)).toBeGreaterThanOrEqual(2);
+ await extractManagedStaleLinks(engine, { mentions: false });
+ expect(await aliceTypes(engine)).toContain('works_at');
+ }, { databaseUrl });
+}
+
+/** A toggle racing an extraction: whatever interleaving happens, one more extraction converges to the final setting. */
+export async function concurrentToggleConverges(databaseUrl?: string) {
+ await managedBrain(async ({ engine, ctx }) => {
+ await seed(engine, ctx);
+ for (let i = 0; i < 20; i++) await put(ctx, `people/extra-${i}`, `- works_at [[companies/acme-example]]\n\nExtra ${i}.`);
+ await Promise.all([
+ extractManagedStaleLinks(engine, { mentions: false }),
+ applyLineGrammarConfigChange(engine, tx => tx.setConfig('line_grammar.enabled', 'true')),
+ ]);
+ await extractManagedStaleLinks(engine, { mentions: false });
+ expect(await stale(engine)).toBe(0);
+ expect(await aliceTypes(engine)).toContain('works_at');
+ }, { databaseUrl });
+}
diff --git a/test/line-grammar-toggle.test.ts b/test/line-grammar-toggle.test.ts
index f8d9e50e18..59acc6fd17 100644
--- a/test/line-grammar-toggle.test.ts
+++ b/test/line-grammar-toggle.test.ts
@@ -4,96 +4,19 @@
* page extracted before the change reads as stale through the one watermark
* (core/link-extraction-watermark.ts), managed extraction re-derives it, and
* a page prepared under the old settings is never published as fresh.
- * Managed PGLite brain.
+ * Managed PGLite brain; the Postgres arm is test/e2e/line-grammar-toggle-postgres.test.ts.
*/
import { expect, test } from 'bun:test';
-import { randomUUID } from 'node:crypto';
-import type { BrainEngine } from '../src/core/engine.ts';
-import type { OperationContext } from '../src/core/ops/contract.ts';
-import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';
-import { extractManagedStaleLinks } from '../src/core/persistence/links-maintenance.ts';
-import { prepareAutomaticLinks } from '../src/core/persistence/links-preparation.ts';
-import { applyLineGrammarConfigChange, describeLineGrammarChange, invalidLineGrammarValue, isInternalConfigKey } from '../src/core/line-grammar-config.ts';
-import { effectiveLinkExtractorWatermark, laterInstant } from '../src/core/link-extraction-watermark.ts';
+import { invalidLineGrammarValue, isInternalConfigKey } from '../src/core/line-grammar-config.ts';
+import { laterInstant } from '../src/core/link-extraction-watermark.ts';
import { LINK_EXTRACTOR_VERSION_TS } from '../src/core/link-extraction.ts';
import { LINK_EXTRACTION_GENERATION_KEY } from '../src/core/line-grammar.ts';
-import { managedBrain } from './helpers/managed-brain.ts';
+import { concurrentToggleConverges, enableThenDisableConverges, noOpChangesKeepTheGeneration, preparedBeforeChangeStaysStale } from './helpers/line-grammar-toggle-scenarios.ts';
-const put = (ctx: OperationContext, slug: string, body: string, type = 'person') =>
- submitPageMutation(ctx, { operation: 'put_page', params: { slug, request_id: randomUUID(),
- content: `---\ntype: ${type}\ntitle: ${slug}\n---\n\n${body}\n` } }) as Promise<Record<string, any>>;
-
-const aliceTypes = async (engine: BrainEngine) => (await engine.executeRaw<{ link_type: string }>(`SELECT DISTINCT l.link_type FROM links l
- JOIN pages f ON f.id=l.from_page_id JOIN pages t ON t.id=l.to_page_id
- WHERE f.slug='people/alice-example' AND t.slug='companies/acme-example' ORDER BY 1`)).map(r => r.link_type);
-
-const stale = async (engine: BrainEngine) => engine.countStalePagesForExtraction({ versionTs: await effectiveLinkExtractorWatermark(engine) });
-
-async function seed(engine: BrainEngine, ctx: OperationContext) {
- await put(ctx, 'companies/acme-example', 'Acme.', 'company');
- await put(ctx, 'people/alice-example', 'Alice builds things.\n\n- works_at [[companies/acme-example]]');
- await extractManagedStaleLinks(engine, { mentions: false });
-}
-
-test('enable then disable: each effective change re-extracts and the stated type follows the setting', async () => {
- await managedBrain(async ({ engine, ctx }) => {
- await seed(engine, ctx);
- expect(await aliceTypes(engine)).not.toContain('works_at');
- expect(await stale(engine)).toBe(0);
-
- const on = await applyLineGrammarConfigChange(engine, tx => tx.setConfig('line_grammar.enabled', 'true'));
- expect(on.changed).toBe(true);
- expect(await engine.getConfig(LINK_EXTRACTION_GENERATION_KEY)).toBe(on.generation);
- expect(await stale(engine)).toBeGreaterThanOrEqual(2);
- const described = await describeLineGrammarChange(engine, on);
- expect(described.lines.join('\n')).toContain('gbrain extract --stale');
- expect(described.json).toMatchObject({ changed: true, effective: { enabled: true } });
- await extractManagedStaleLinks(engine, { mentions: false });
- expect(await stale(engine)).toBe(0);
- expect(await aliceTypes(engine)).toContain('works_at');
-
- const off = await applyLineGrammarConfigChange(engine, tx => tx.setConfig('line_grammar.enabled', 'false'));
- expect(off.changed).toBe(true);
- expect(Date.parse(off.generation!)).toBeGreaterThanOrEqual(Date.parse(on.generation!));
- expect(await stale(engine)).toBeGreaterThanOrEqual(2);
- await extractManagedStaleLinks(engine, { mentions: false });
- expect(await stale(engine)).toBe(0);
- expect(await aliceTypes(engine)).not.toContain('works_at');
- });
-}, 180_000);
-
-test('no-op changes keep the generation: repeated settings, spellings, unset to the same default, subordinate keys while off', async () => {
- await managedBrain(async ({ engine, ctx }) => {
- await seed(engine, ctx);
- for (const mutate of [
- (tx: BrainEngine) => tx.setConfig('line_grammar.enabled', 'false'),
- (tx: BrainEngine) => tx.setConfig('line_grammar.enabled', 'off'),
- async (tx: BrainEngine) => { await tx.unsetConfig('line_grammar.enabled'); },
- (tx: BrainEngine) => tx.setConfig('line_grammar.effective_ranges', 'false'),
- (tx: BrainEngine) => tx.setConfig('line_grammar.allow_undeclared_types', 'true'),
- ]) {
- const change = await applyLineGrammarConfigChange(engine, mutate);
- expect(change.changed).toBe(false);
- }
- expect(await engine.getConfig(LINK_EXTRACTION_GENERATION_KEY)).toBeNull();
- expect(await stale(engine)).toBe(0);
- });
-}, 120_000);
-
-test('a page prepared before a change is not published or stamped fresh after it', async () => {
- await managedBrain(async ({ engine, ctx }) => {
- await seed(engine, ctx);
- const snapshot = await engine.readPageSnapshot('people/alice-example', { sourceId: 'default' });
- const prepared = await prepareAutomaticLinks(engine, 'people/alice-example', snapshot!.page, 'default');
- expect(prepared.settings?.enabled).toBe(false);
- await applyLineGrammarConfigChange(engine, tx => tx.setConfig('line_grammar.enabled', 'true'));
- const written = await engine.transaction(tx => prepared.apply(tx));
- expect(written.errors).toBe(1);
- expect(await stale(engine)).toBeGreaterThanOrEqual(2);
- await extractManagedStaleLinks(engine, { mentions: false });
- expect(await aliceTypes(engine)).toContain('works_at');
- });
-}, 120_000);
+test('enable then disable: each effective change re-extracts and the stated type follows the setting', () => enableThenDisableConverges(), 180_000);
+test('no-op changes keep the generation: repeated settings, spellings, unset to the same default, subordinate keys while off', () => noOpChangesKeepTheGeneration(), 120_000);
+test('a page prepared before a change is not published or stamped fresh after it', () => preparedBeforeChangeStaysStale(), 120_000);
+test('a toggle racing an extraction converges after one more extraction', () => concurrentToggleConverges(), 180_000);
test('validation and internal keys', () => {
expect(invalidLineGrammarValue('line_grammar.enabled', 'tru')).toContain('true or false');