Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: each read-only check runs the statement its call site ran on master
garrytand83fbf3e58db
diff --git a/src/core/cycle/derived-write-through.ts b/src/core/cycle/derived-write-through.tsindex c8eebd6cae..c537f02e75 100644--- a/src/core/cycle/derived-write-through.ts+++ b/src/core/cycle/derived-write-through.ts@@ -35,7 +35,7 @@ import { throwIfAborted } from '../abort-check.ts';import { validateSourceId } from '../utils.ts';import { isDbOnly, loadStorageConfig } from '../storage-config.ts';import { deletePageThrough, isWriteThroughDisabled, resolvePageWriteTarget, writePageThrough, type PageWriteTarget } from '../write-through.ts';-import { sourceMirrorReadOnly } from '../persistence/mirror-read-only.ts';+import { readOnlyCheckout } from '../persistence/mirror-read-only.ts';import { gitChildEnv, literalPathspecs } from '../git-env.ts';import { classifyGitCheckout, type GitCheckoutClass } from '../git-checkout.ts';import { acquireCommitLock, acquireCommitLockAsync, checkoutRoot, commitAndPush, commitPathsLocked, COMMIT_LOCK_ENV, notePending, scopePaths, settlePending } from '../git/commit-push.ts';@@ -407,7 +407,7 @@ async function dreamFileTarget(engine: BrainEngine, brainDir: string, slug: striconst target = await resolvePageWriteTarget(engine, slug, sourceId);if (!target.ok && (!brainDir || (target.skipped !== 'no_repo_configured' && target.skipped !== 'repo_not_found'))) return { skipped: target.skipped };if (target.ok) return { filePath: target.filePath, root: target.writeRoot };- if (sourceId !== nativeSourceId && await sourceMirrorReadOnly(engine, nativeSourceId)) return { skipped: 'read_only_checkout' };+ if (sourceId !== nativeSourceId && await readOnlyCheckout(engine, nativeSourceId)) return { skipped: 'read_only_checkout' };return { filePath: sourceId === nativeSourceId ? join(brainDir, `${slug}.md`) : join(brainDir, '.sources', sourceId, `${slug}.md`), root: brainDir };}@@ -415,7 +415,7 @@ async function dreamFileTarget(engine: BrainEngine, brainDir: string, slug: stri* #6982: write the file of a page the dream cycle wrote in the database (a synthesis or patterns output, the cycle* summary) without replacing an edit only the file holds, then commit it (#8073: `commitDerivedChanges`; a refused* commit rolls the file back and returns its `write_through_failed` hold). Nothing is written with `sync.write_through`- * off, for a read-only mirror or a clone gbrain owns (#8935: `sourceMirrorReadOnly`, both checked before any path is+ * off, for a read-only mirror or a clone gbrain owns (#8935: `readOnlyCheckout`, both checked before any path is* resolved, so a mirror without a checkout gets no brainDir fallback file either), or for a declared `storage.db_only`* slug: the database is the copy of record. The file is* the page's file of record (`resolvePageWriteTarget`: recorded source_path, own-local_path roots); a source with no@@ -442,7 +442,7 @@ export async function writeDreamPageFile(engine: BrainEngine, brainDir: string,async function writeDreamFile(engine: BrainEngine, brainDir: string, slug: string, sourceId: string,opts: { nativeSourceId: string; markdown: string }): Promise<{ result: DreamPageFileWrite; change?: DerivedFileChange }> {if (await isWriteThroughDisabled(engine)) return { result: { written: false, skipped: 'disabled_by_config' } };- if (await sourceMirrorReadOnly(engine, sourceId)) return { result: { written: false, skipped: 'read_only_checkout' } };+ if (await readOnlyCheckout(engine, sourceId)) return { result: { written: false, skipped: 'read_only_checkout' } };const target = await dreamFileTarget(engine, brainDir, slug, sourceId, opts.nativeSourceId);if ('skipped' in target) return { result: { written: false, skipped: target.skipped } };try {@@ -474,7 +474,7 @@ async function writeDreamFile(engine: BrainEngine, brainDir: string, slug: strin*/export async function removeDreamPageFile(engine: BrainEngine, brainDir: string, slug: string, sourceId: string,nativeSourceId: string): Promise<{ removed: boolean; held?: string }> {- if (await isWriteThroughDisabled(engine) || await sourceMirrorReadOnly(engine, sourceId)) return { removed: false };+ if (await isWriteThroughDisabled(engine) || await readOnlyCheckout(engine, sourceId)) return { removed: false };const target = await dreamFileTarget(engine, brainDir, slug, sourceId, nativeSourceId);if ('skipped' in target) return { removed: false };const { filePath } = target;diff --git a/src/core/persistence/mirror-read-only.ts b/src/core/persistence/mirror-read-only.tsindex 74a263b8c8..67a4d3ff39 100644--- a/src/core/persistence/mirror-read-only.ts+++ b/src/core/persistence/mirror-read-only.ts@@ -23,18 +23,21 @@ import { urlCloneReadOnly } from '../url-clone.ts';/** `lock`: inside a publication, hold the source row so a concurrent mode change waits for this write. */export async function sourceMirrorReadOnly(engine: SqlEngine, sourceId: string, lock = false): Promise<boolean> {- return await readOnlyCheckout(engine, sourceId, lock) !== null;+ const [row] = await engine.executeRaw<{ read_only: boolean; local_path: string | null; config: unknown }>(+ `SELECT COALESCE(config->>'mirror_read_only','false')='true' AS read_only, local_path, config FROM sources WHERE id=$1${lock ? ' FOR SHARE' : ''}`, [sourceId]);+ if (!row) return false;+ return row.read_only === true || await urlCloneReadOnly(engine, sourceId, row);}/*** Why the source's checkout is never written, or null: `mirror_read_only` (the flag) or `url_clone` (#6946). #7098: the* legacy disk writers (write-through, fact fences, timeline, forget, unfenced facts) skip on it as managed publication does.*/-export async function readOnlyCheckout(engine: SqlEngine, sourceId: string, lock = false): Promise<'mirror_read_only' | 'url_clone' | null> {- const [row] = await engine.executeRaw<{ read_only: boolean; local_path: string | null; config: unknown }>(- `SELECT COALESCE(config->>'mirror_read_only','false')='true' AS read_only, local_path, config FROM sources WHERE id=$1${lock ? ' FOR SHARE' : ''}`, [sourceId]);+export async function readOnlyCheckout(engine: SqlEngine, sourceId: string): Promise<'mirror_read_only' | 'url_clone' | null> {+ const [row] = await engine.executeRaw<{ local_path: string | null; config: unknown }>('SELECT local_path, config FROM sources WHERE id = $1', [sourceId]);if (!row) return null;- if (row.read_only === true) return 'mirror_read_only';+ const config = typeof row.config === 'string' ? JSON.parse(row.config) as Record<string, unknown> : (row.config ?? {}) as Record<string, unknown>;+ if (String(config.mirror_read_only) === 'true') return 'mirror_read_only';return await urlCloneReadOnly(engine, sourceId, row) ? 'url_clone' : null;}