Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sources): mirror-readonly / mirror-writable --dry-run preview and write nothing (#7086)

garrytan34084f4c0ce4
diff --git a/src/commands/sources-mirror.ts b/src/commands/sources-mirror.ts
index 84b5a11c84..e5c7480240 100644
--- a/src/commands/sources-mirror.ts
+++ b/src/commands/sources-mirror.ts
@@ -5,7 +5,8 @@
* writes a file into the source's checkout: sync imports canonical metadata
* database-only and page writes are stored database-only, so a mirror kept
* current by `git pull --ff-only` never gains local modifications.
- * Follows `federate` / `unfederate`.
+ * Follows `federate` / `unfederate`. `--dry-run` prints the change and writes
+ * nothing (#7086), as `sources remove --dry-run` does.
*/
import type { BrainEngine } from '../core/engine.ts';
import { normalizeSourceConfig, parseSourceConfig } from '../core/sources-load.ts';
@@ -13,7 +14,7 @@ import { normalizeSourceConfig, parseSourceConfig } from '../core/sources-load.t
export async function runMirrorMode(engine: BrainEngine, args: string[], readOnly: boolean): Promise<void> {
const id = args[0];
if (!id || id.startsWith('-')) {
- console.error(`Usage: gbrain sources ${readOnly ? 'mirror-readonly' : 'mirror-writable'} <id>`);
+ console.error(`Usage: gbrain sources ${readOnly ? 'mirror-readonly' : 'mirror-writable'} <id> [--dry-run]`);
process.exit(2);
}
const [src] = await engine.executeRaw<{ config: unknown }>('SELECT config FROM sources WHERE id=$1', [id]);
@@ -22,6 +23,14 @@ export async function runMirrorMode(engine: BrainEngine, args: string[], readOnl
process.exit(4);
}
const config = parseSourceConfig(src.config);
+ if (args.includes('--dry-run')) {
+ const current = config.mirror_read_only === true;
+ console.log(current === readOnly
+ ? `Source "${id}" is already ${readOnly ? 'a read-only mirror' : 'writable'}; nothing would change.`
+ : `Source "${id}" would become ${readOnly ? 'a read-only mirror (mirror_read_only: false -> true)' : 'writable (mirror_read_only: true -> false)'}.`);
+ console.log('(dry-run; no side effects)');
+ return;
+ }
if (readOnly) config.mirror_read_only = true;
else delete config.mirror_read_only;
await engine.executeRaw('UPDATE sources SET config = $1::text::jsonb WHERE id = $2', [JSON.stringify(normalizeSourceConfig(config)), id]);
diff --git a/src/commands/sources.ts b/src/commands/sources.ts
index 13b8440f44..0717e821f9 100644
--- a/src/commands/sources.ts
+++ b/src/commands/sources.ts
@@ -1990,8 +1990,8 @@ Subcommands:
brain needed. See docs/guides/github-source.md.
federate <id> Make source appear in cross-source default search.
unfederate <id> Isolate source from default search.
- mirror-readonly <id> Read-only mirror (#5409): managed writes never touch its checkout.
- mirror-writable <id> Undo mirror-readonly.
+ mirror-readonly <id> [--dry-run] Read-only mirror (#5409): managed writes never touch its checkout.
+ mirror-writable <id> [--dry-run] Undo mirror-readonly. --dry-run previews either and writes nothing.
set-strategy <id> <markdown|code|auto> Persist the sync strategy every sync of the source uses (PR #5955).
set-pull-strategy <id> <rebase|merge> How pulls reconcile local commits with origin (#5429); default rebase.
refresh <id> [--dry-run] [--wait-drain <s>] [--fetch-timeout-ms <ms>] [--resume|--abandon] [--json]
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index 9d67f0f57c..56e2c5fd05 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -976,6 +976,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
{ since: '0.60.316.0', text: '`gbrain autopilot --status` grades a running daemon from its own schedule (#7003): the daemon writes `autopilot.heartbeat.json` beside its lock with its next tick, so a healthy daemon on a long adaptive sleep no longer reads `stale` (exit 1); a missing or outdated sidecar falls back to the interval-flag rule. `--status --json` adds `stale_after_source` (`daemon` or `flag`) and `next_tick_at`.' },
// GBRA-133 PR 2b (truthful status). `since` is restamped at the merge slot.
{ since: '0.60.316.0', text: 'Doctor `managed_sync_latency` now grades the commit that is still waiting, not only commits that already synced: with `sync.on_commit` on or `sync.freshness_target_seconds` set, a source whose checkout HEAD has waited past the target (default 300 s) warns and past one hour fails, dated from the reflog entry that moved HEAD or the notifier\'s recorded first_due_at, and a waiting commit it cannot date (no reflog, rewritten history) warns as unknown. It grades classic sources the same way and reports keyword-visible and embedded freshness per source (`details.freshness_by_source`). `gbrain sources status` compares HEAD only (an uncommitted edit is no longer lag) and dates a behind source\'s lag from when the commit landed instead of from the last no-op sync.' },
+ { since: '0.60.316.0', text: '`gbrain sources mirror-readonly <id> --dry-run` and `gbrain sources mirror-writable <id> --dry-run` now print the change they would make with a `(dry-run; no side effects)` banner and write nothing (#7086); before, both ignored `--dry-run` and set or cleared the read-only mirror flag.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/test/e2e/s-7086-mirror-dry-run-postgres.test.ts b/test/e2e/s-7086-mirror-dry-run-postgres.test.ts
new file mode 100644
index 0000000000..e75f70a57f
--- /dev/null
+++ b/test/e2e/s-7086-mirror-dry-run-postgres.test.ts
@@ -0,0 +1,3 @@
+import { registerPostgresTests } from '../helpers/test-backends.ts';
+
+await registerPostgresTests(() => import('../pathological/s-7086-mirror-dry-run.test.ts'));
diff --git a/test/pathological/s-7086-mirror-dry-run.test.ts b/test/pathological/s-7086-mirror-dry-run.test.ts
new file mode 100644
index 0000000000..a0fb6a90d7
--- /dev/null
+++ b/test/pathological/s-7086-mirror-dry-run.test.ts
@@ -0,0 +1,62 @@
+/**
+ * #7086: `gbrain sources mirror-readonly <id> --dry-run` and `mirror-writable <id> --dry-run` preview the change and write
+ * nothing, as `sources remove --dry-run` does. Up to v0.60.316.0 both ignored --dry-run and set or cleared
+ * `sources.config.mirror_read_only`. Asserts the source row (config and updated state) is byte-identical after each dry
+ * run and that the real command still writes. PGLite always, Postgres when DATABASE_URL is set
+ * (twin: test/e2e/s-7086-mirror-dry-run-postgres.test.ts).
+ */
+import { describe, expect, test } from 'bun:test';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import type { BrainEngine } from '../../src/core/engine.ts';
+import { runSources } from '../../src/commands/sources.ts';
+import { isolatedSharedSkillsEngine } from '../helpers/shared-skills-engine.ts';
+import { withEnv } from '../helpers/with-env.ts';
+import { pathologicalArms } from './harness.ts';
+
+const SOURCE = 'acme-example';
+
+async function cli(engine: BrainEngine, args: string[]): Promise<string> {
+ const lines: string[] = [];
+ const original = console.log;
+ console.log = (...parts: unknown[]) => { lines.push(parts.map(String).join(' ')); };
+ try { await runSources(engine, args); } finally { console.log = original; }
+ return lines.join('\n');
+}
+const row = async (engine: BrainEngine) =>
+ (await engine.executeRaw<{ config: string }>('SELECT config::text AS config FROM sources WHERE id = $1', [SOURCE]))[0]!.config;
+
+for (const { backend, databaseUrl } of pathologicalArms()) {
+ describe(`sources mirror-readonly / mirror-writable --dry-run (#7086) [${backend}]`, () => {
+ test('--dry-run previews with a (dry-run) banner and leaves the source row unchanged; without it the flag is written', async () => {
+ const home = mkdtempSync(join(tmpdir(), 'gbrain-mirror-dry-run-'));
+ await withEnv({ GBRAIN_HOME: home }, async () => {
+ const { engine, close } = await isolatedSharedSkillsEngine(databaseUrl);
+ try {
+ await engine.executeRaw("INSERT INTO sources(id,name,config) VALUES($1,$1,'{\"federated\":true}')", [SOURCE]);
+ const before = await row(engine);
+ const preview = await cli(engine, ['mirror-readonly', SOURCE, '--dry-run']);
+ expect(preview).toContain('(dry-run; no side effects)');
+ expect(preview).toContain('would become a read-only mirror');
+ expect(await row(engine)).toBe(before);
+
+ await cli(engine, ['mirror-readonly', SOURCE]);
+ const readOnly = await row(engine);
+ expect(JSON.parse(readOnly).mirror_read_only).toBe(true);
+
+ const undo = await cli(engine, ['mirror-writable', SOURCE, '--dry-run']);
+ expect(undo).toContain('(dry-run; no side effects)');
+ expect(undo).toContain('would become writable');
+ expect(await row(engine)).toBe(readOnly);
+
+ await cli(engine, ['mirror-writable', SOURCE]);
+ expect(JSON.parse(await row(engine)).mirror_read_only).toBeUndefined();
+ } finally {
+ await close();
+ rmSync(home, { recursive: true, force: true });
+ }
+ });
+ }, 120_000);
+ });
+}