Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(connector-retry): the baseline sweep must finish before the cursor snapshot
garrytane061479b2082
diff --git a/test/helpers/connector-fixture.ts b/test/helpers/connector-fixture.tsindex a0e4733fa5..57383919a3 100644--- a/test/helpers/connector-fixture.ts+++ b/test/helpers/connector-fixture.ts@@ -7,7 +7,7 @@ import type { BrainEngine } from '../../src/core/engine.ts';import type { GBrainConfig } from '../../src/core/config.ts';import { claimWorktree } from '../../src/core/persistence/ownership.ts';import { disposePersistenceConsumer, waitForWrites } from '../../src/core/persistence/service.ts';-import type { WriteRequest } from '../../src/core/persistence/model.ts';+import { TERMINAL_STATES, type WriteRequest } from '../../src/core/persistence/model.ts';import { isolatedPersistencePostgres } from './persistence-postgres.ts';import { syncLockId } from '../../src/core/db-lock.ts';import { testBackends } from './test-backends.ts';@@ -41,6 +41,23 @@ export function githubFetch(opts: { failDetail?: boolean; failSecondPage?: boole};}+/**+ * A baseline sweep that finished: re-run `run` until the source has no+ * accepted-pending connector writes and no unfinished write request. A sweep+ * whose writes outlast its wait budget ends partial (its cursor is saved by the+ * next run), so a test that snapshots the cursor right after one `run()` could+ * read the state before the cursor moved and then see the next run move it.+ */+export async function completeBaselineSweep(engine: BrainEngine, sourceId: string, run: () => Promise<unknown>, attempts = 5): Promise<void> {+ for (let i = 0; i < attempts; i++) {+ await run();+ const unfinished = await engine.executeRaw<{ n: number }>(+ 'SELECT count(*)::int AS n FROM persistence_requests WHERE source_id=$1 AND NOT (state = ANY($2::text[]))', [sourceId, [...TERMINAL_STATES]]);+ if ((await connectorPendingSet(engine, sourceId)).length === 0 && unfinished[0]!.n === 0) return;+ }+ throw new Error(`baseline sweep of ${sourceId} still had pending connector writes after ${attempts} runs`);+}+export async function sourceCheckpoint(engine: BrainEngine, id: string) {return engine.executeRaw("SELECT completed_keys FROM op_checkpoints WHERE op='managed-connector' AND fingerprint IN (SELECT intent->>'checkpointKey' FROM persistence_requests WHERE source_id=$1)", [id]);}diff --git a/test/persistence-connector-retry.test.ts b/test/persistence-connector-retry.test.tsindex f6b0d1a50a..cabb3036f6 100644--- a/test/persistence-connector-retry.test.ts+++ b/test/persistence-connector-retry.test.ts@@ -12,7 +12,7 @@ import { beginConnectorSync } from '../src/core/persistence/connector-sync.ts';import { compactWriteReceipts } from '../src/core/persistence/journal.ts';import { purgeStaleCheckpoints } from '../src/core/op-checkpoint.ts';import { withEnv } from './helpers/with-env.ts';-import { createConnectorFixture, options, json, googleConfig, githubConfig, contact, issueFixture, githubFetch, sourceCheckpoint, sourceCursor, cursorOf, connectorPendingSet } from './helpers/connector-fixture.ts';+import { createConnectorFixture, options, json, googleConfig, githubConfig, contact, issueFixture, githubFetch, sourceCheckpoint, sourceCursor, cursorOf, connectorPendingSet, completeBaselineSweep } from './helpers/connector-fixture.ts';const { engines, env, backends, source, boundSource, standaloneConnector, setup, teardown } = createConnectorFixture();beforeAll(setup, 120_000);@@ -54,9 +54,10 @@ test('explicit connector retry replaces a real storage failure without changingconst run = (retryFailed = false) => runGitHubSync(engine, f.id, parseGitHubSourceConfig(githubConfig, f.dir),{ ...options, retryFailed, githubItem: { repo: 'acme-example/app', number: 1, kind: 'issue' } },async url => new URL(url).pathname.endsWith('/issues/1') ? json({ ...issueFixture, body }) : githubFetch()(url));- await run();+ await completeBaselineSweep(engine, f.id, run);await disposePersistenceConsumer(engine);const checkpoint = await sourceCheckpoint(engine, f.id);+ expect(checkpoint).toHaveLength(1);const path = join(f.dir, 'gh/acme-example/app/1.md');const initial = readFileSync(path, 'utf8');body = 'Updated organization retry fixture';@@ -129,9 +130,10 @@ async function retryFixture(engine: BrainEngine, connector: 'google' | 'github',const run = (retryFailed = false) => connector === 'google'? runGoogleSync(engine, f.id, cfg as ReturnType<typeof parseGoogleSourceConfig>, { ...options, retryFailed }, withGoogleAccount(fetcher)): runGitHubSync(engine, f.id, cfg as ReturnType<typeof parseGitHubSourceConfig>, { ...options, retryFailed }, fetcher);- await run();+ await completeBaselineSweep(engine, f.id, run);await disposePersistenceConsumer(engine);const checkpoint = await sourceCheckpoint(engine, f.id);+ expect(checkpoint).toHaveLength(1);const session = (await beginConnectorSync(engine, f.id, connector, cfg, options))!;await engine.executeRaw('INSERT INTO connector_retry_faults(source_id,checkpoint_key) VALUES($1,$2)', [f.id, session.checkpointKey]);updated = true;