Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

A dry run of a Google or GitHub source no longer writes pages (#9449) (#9455)

Garry Tanad767892f954
diff --git a/changelog.d/fix-connector-dry-run-9449.md b/changelog.d/fix-connector-dry-run-9449.md
new file mode 100644
index 0000000000..86b809adec
--- /dev/null
+++ b/changelog.d/fix-connector-dry-run-9449.md
@@ -0,0 +1,9 @@
+**A dry run of a Google or GitHub source no longer writes pages (#9449).**
+
+### Fixed
+- `gbrain sync --source <id> --dry-run` on a Google (Gmail, Calendar, Contacts) or GitHub source of a brain without managed persistence fetched from the provider and wrote what it fetched: pages, chunks and soft-deletes. It now fetches and writes nothing, prints that the source has no preview, and returns the `dry_run` result, as Slack and meetings sources already did. A brain with managed persistence still refuses a connector dry run before any provider call (#9449).
+- Tests: `test/sync-connector-dry-run-9449.test.ts` runs a dry run of every connector kind on PGLite and Postgres and checks that nothing was fetched or written.
+
+Reported by @kostjakla-del-ok. First fixed by @Masashi-Ono0611.
+
+Supersedes #9450
diff --git a/src/core/github-source.ts b/src/core/github-source.ts
index 2472f4e196..bfdd80646f 100644
--- a/src/core/github-source.ts
+++ b/src/core/github-source.ts
@@ -3,6 +3,7 @@ import { resolveGitHubAccount } from './persistence/connector-account.ts';
import { isValidRepoName } from './github-source-config.ts';
import { connectorRender } from './connectors/connector-text.ts';
import { ConnectorHoldSession, connectorHoldsResult } from './connectors/connector-hold-session.ts';
+import { connectorDryRunResult } from './connectors/api-source.ts';
export { isValidRepoName, parseGitHubSourceConfig } from './github-source-config.ts';
export { AppTokenProvider, mintAppInstallationToken } from './github-app-token.ts';
import { AppTokenProvider } from './github-app-token.ts';
@@ -1091,7 +1092,7 @@ export async function runGitHubSync(
fetchImpl?: FetchImpl,
): Promise<import('../commands/sync.ts').SyncResult> {
return withConnectorSync(engine, sourceId, 'github', cfg, opts,
- (managed, options) => runGitHubSyncInner(engine, sourceId, cfg, options, managed, fetchImpl), pendingConnectorResult);
+ async (managed, options) => options.dryRun ? connectorDryRunResult('github', sourceId) : runGitHubSyncInner(engine, sourceId, cfg, options, managed, fetchImpl), pendingConnectorResult);
}
async function runGitHubSyncInner(engine: BrainEngine, sourceId: string, cfg: GitHubSourceConfig, opts: SyncOpts,
diff --git a/src/core/google/google-source.ts b/src/core/google/google-source.ts
index 4a5df66472..55c725defa 100644
--- a/src/core/google/google-source.ts
+++ b/src/core/google/google-source.ts
@@ -3,6 +3,7 @@ import { resolveGoogleAccount } from '../persistence/connector-account.ts';
import { googleApiProblem, googleUserinfo } from './api-access.ts';
import { ConnectorHoldSession, connectorHoldsResult } from '../connectors/connector-hold-session.ts';
import { carryLegacyFailCounts } from '../connectors/item-holds.ts';
+import { connectorDryRunResult } from '../connectors/api-source.ts';
/**
* google-source — Gmail/Calendar/Contacts sync for the `google` source kind.
*
@@ -720,7 +721,7 @@ export async function runGoogleSync(
vaultOverride?: CredentialVault,
): Promise<SyncResult> {
return withConnectorSync(engine, sourceId, 'google', cfg, opts,
- (managed, options) => runGoogleSyncInner(engine, sourceId, cfg, options, managed, fetchImpl, vaultOverride), pendingConnectorResult);
+ async (managed, options) => options.dryRun ? connectorDryRunResult('google', sourceId) : runGoogleSyncInner(engine, sourceId, cfg, options, managed, fetchImpl, vaultOverride), pendingConnectorResult);
}
async function runGoogleSyncInner(engine: BrainEngine, sourceId: string, cfg: GoogleSourceConfig, opts: SyncOpts,
diff --git a/test/sync-connector-dry-run-9449.test.ts b/test/sync-connector-dry-run-9449.test.ts
new file mode 100644
index 0000000000..99920c7092
--- /dev/null
+++ b/test/sync-connector-dry-run-9449.test.ts
@@ -0,0 +1,96 @@
+/**
+ * #9449: `gbrain sync --source <id> --dry-run` on a connector source of an unmanaged brain fetched from the provider and
+ * wrote what it fetched (pages, chunks, soft-deletes); only the cursor was left alone. Every connector kind the sync
+ * command dispatches to now says it has no preview and returns before any provider call or write.
+ */
+import { afterAll, afterEach, beforeAll, expect, test } from 'bun:test';
+import { readdirSync } from 'node:fs';
+import { join } from 'node:path';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { performSync } from '../src/commands/sync.ts';
+import { CONNECTOR_SOURCE_KINDS } from '../src/core/persistence/connector-kinds.ts';
+import { contact, createConnectorFixture, githubConfig, githubFetch, googleConfig, json, withGoogleAccount } from './helpers/connector-fixture.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+const { engines, env, source, setup, teardown } = createConnectorFixture();
+beforeAll(setup, 120_000);
+afterAll(teardown);
+const realFetch = globalThis.fetch;
+afterEach(() => { globalThis.fetch = realFetch; });
+
+const configs: Record<string, Record<string, unknown>> = {
+ google: googleConfig,
+ github: githubConfig,
+ meetings: { kind: 'meetings', m_provider: 'fireflies', m_record: 'database', federated: false },
+ slack: { kind: 'slack', s_channels: 'CGENERAL01' },
+};
+const googleContacts = withGoogleAccount(async () => json({ connections: [contact('first', 'First Example')], nextSyncToken: 'contacts-1' }));
+const providers: Record<string, (url: string, init?: RequestInit) => Promise<Response>> = {
+ google: googleContacts,
+ github: githubFetch(),
+ meetings: async () => json({ data: {} }),
+ slack: async () => json({ ok: true, messages: [] }),
+};
+
+function files(dir: string): string[] {
+ return readdirSync(dir, { recursive: true, withFileTypes: true }).filter(e => e.isFile()).map(e => join(e.parentPath, e.name)).sort();
+}
+
+async function state(engine: BrainEngine, sourceId: string, dir: string) {
+ const q = <T>(sql: string, params: unknown[] = []) => engine.executeRaw<T>(sql, params);
+ return {
+ pages: await q("SELECT slug, deleted_at::text AS deleted_at, content_hash FROM pages WHERE source_id=$1 ORDER BY slug", [sourceId]),
+ chunks: await q('SELECT count(*)::int AS n FROM content_chunks c JOIN pages p ON p.id=c.page_id WHERE p.source_id=$1', [sourceId]),
+ source: await q('SELECT last_sync_at::text AS last_sync_at, config::text AS config FROM sources WHERE id=$1', [sourceId]),
+ checkpoints: await q('SELECT op, fingerprint, completed_keys::text AS keys FROM op_checkpoints ORDER BY op, fingerprint'),
+ config: await q('SELECT key, value FROM config ORDER BY key'),
+ requests: await q('SELECT count(*)::int AS n FROM persistence_requests WHERE source_id=$1', [sourceId]),
+ files: files(dir),
+ };
+}
+
+// Value: protects=a connector dry run on an unmanaged brain fetches nothing and writes nothing (#9449);
+// fails_when=runGoogleSync or runGitHubSync (or a new connector kind) sweeps the provider on --dry-run;
+// why_new=the managed refusal test covers only managed brains, and the meetings and slack guards had no sync-command test;
+// seam=performSync({ sourceId, dryRun }) with the provider stubbed at fetch; no_claim=a per-item preview (connectors have none).
+test('a dry run of every connector kind on an unmanaged brain calls no provider and writes nothing', async () => withEnv(env, async () => {
+ expect(Object.keys(configs).sort()).toEqual([...CONNECTOR_SOURCE_KINDS].sort());
+ for (const engine of engines) for (const kind of CONNECTOR_SOURCE_KINDS) {
+ const f = await source(engine, configs[kind]!);
+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');
+ const calls: string[] = [];
+ globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => {
+ const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
+ calls.push(url);
+ return providers[kind]!(url, init);
+ }) as unknown as typeof fetch;
+ const before = await state(engine, f.id, f.dir);
+ const result = await performSync(engine, { sourceId: f.id, dryRun: true, noExtract: true, noSchemaPack: true });
+ globalThis.fetch = realFetch;
+ expect({ engine: engine.kind, kind, status: result.status, added: result.added, modified: result.modified, deleted: result.deleted, chunks: result.chunksCreated, embedded: result.embedded })
+ .toEqual({ engine: engine.kind, kind, status: 'dry_run', added: 0, modified: 0, deleted: 0, chunks: 0, embedded: 0 });
+ expect({ kind, calls }).toEqual({ kind, calls: [] });
+ expect(await state(engine, f.id, f.dir)).toEqual(before);
+ }
+}), 120_000);
+
+// Value: protects=a managed brain refuses a connector dry run before any provider call or write, for every kind;
+// fails_when=a connector kind skips beginConnectorSync's dry-run refusal on the sync command's managed route;
+// why_new=the existing refusal test calls runGoogleSync and runGitHubSync directly, not the sync command, and skips
+// meetings and slack; seam=performSync({ sourceId, dryRun }) on persistence_brain.enabled=true.
+test('a dry run of every connector kind on a managed brain is refused before any provider call or write', async () => withEnv(env, async () => {
+ for (const engine of engines) for (const kind of CONNECTOR_SOURCE_KINDS) {
+ const f = await source(engine, configs[kind]!);
+ const calls: string[] = [];
+ globalThis.fetch = (async (input: string | URL | Request) => {
+ calls.push(typeof input === 'string' ? input : input instanceof URL ? input.href : input.url);
+ throw new Error('Unexpected external request');
+ }) as unknown as typeof fetch;
+ const before = await state(engine, f.id, f.dir);
+ await expect(performSync(engine, { sourceId: f.id, dryRun: true, noExtract: true, noSchemaPack: true })).rejects.toMatchObject({ code: 'invalid_params' });
+ globalThis.fetch = realFetch;
+ expect({ kind, calls }).toEqual({ kind, calls: [] });
+ expect(await state(engine, f.id, f.dir)).toEqual(before);
+ }
+}), 120_000);
+