Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(#5988): remote put_page held-file refusals name the code and the host operator command, never the path or key

garrytand983906d0972
diff --git a/src/core/persistence/held-reads.ts b/src/core/persistence/held-reads.ts
index 856145f566..3de135a9be 100644
--- a/src/core/persistence/held-reads.ts
+++ b/src/core/persistence/held-reads.ts
@@ -96,7 +96,7 @@ function repairArgv(sourceId: string): string[] {
}
/** The fix a remote caller relays: only the brain host operator can inspect and repair held files. */
-function hostOperatorFix(sourceIds: string[], why: string): Action {
+export function hostOperatorFix(sourceIds: string[], why: string): Action {
const commands = sourceIds.map(id => `'${repairArgv(id).join(' ')}'`).join(', ');
return { argv: repairArgv(sourceIds[0]!), consent: [], actor: 'host_admin', requires_exclusive: false, why,
user_message: `Some files in your brain could not be imported, so answers from it can miss or show outdated notes. Please run ${commands} on the brain host to preview the fixes, then apply them.` };
diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.ts
index c5bf2f01bf..3cdda8e792 100644
--- a/src/core/persistence/page-prepare.ts
+++ b/src/core/persistence/page-prepare.ts
@@ -16,6 +16,7 @@ import { contentRefusalError } from '../import-screen.ts';
import { ContentSanityBlockError } from '../content-sanity.ts';
import { assertPageRevision, type PageSnapshot } from '../page-state/types.ts';
import { gitHoldFix, readGitHold } from './sync-holds.ts';
+import { hostOperatorFix } from './held-reads.ts';
import { heldFileMessage } from './verb-errors.ts';
import { isWriteTargetContained } from '../path-confine.ts';
import { recordedPathFromFileUri, scannerSlugRootMode, scannerSourcePath } from '../write-through.ts';
@@ -109,7 +110,7 @@ export function publishesDatabaseOnly(root: string, slug: string, snapshot: Page
return isSourceDbOnlySlug(root, slug, 'refuse') || isNeverFiledDerivedPage(slug, snapshot.page);
}
export async function prepareFileTarget(engine: BrainEngine, row: Pick<WriteRequest, 'source_id' | 'worktree_id' | 'slug'>, snapshot: PageSnapshot | null,
- content: string | null, hostId?: string, options: { allowMissing?: boolean; capture?: { path: string; hash: string }; activePack?: ParseOpts['activePack'] } = {}): Promise<PreparedMutation['file']> {
+ content: string | null, hostId?: string, options: { allowMissing?: boolean; capture?: { path: string; hash: string }; activePack?: ParseOpts['activePack']; remote?: boolean } = {}): Promise<PreparedMutation['file']> {
if (!row.worktree_id) return undefined;
// #5409: a read-only mirror's checkout belongs to its Git remote; nothing is written or removed there.
if (await sourceMirrorReadOnly(engine, row.source_id)) return undefined;
@@ -162,7 +163,7 @@ export async function prepareFileTarget(engine: BrainEngine, row: Pick<WriteRequ
// Withdrawal overlays intentionally precede physical mirroring. The ledger
// is applied by the import preparation and cannot be undone by this check.
if (digest(actual) !== digest(expected)) {
- const held = await heldFileRefusal(engine, row, root, path, 'drift');
+ const held = await heldFileRefusal(engine, row, root, path, 'drift', options.remote === true);
if (held) throw held;
const error = new OperationError('source_changed', 'The canonical file contains an uncoordinated local edit.',
`On the brain host, run gbrain sources reconcile ${row.source_id} ${row.slug} --brain <brain id, host by default> --preview, review and apply the resolved preview, then retry this write with a new request_id. Neither copy was overwritten.`);
@@ -171,7 +172,7 @@ export async function prepareFileTarget(engine: BrainEngine, row: Pick<WriteRequ
}
} else if (before && !snapshot && content !== null && sha256(before) !== sha256(content)
&& !(options.capture && sha256(before) === options.capture.hash)) {
- throw await heldFileRefusal(engine, row, root, path, 'occupied')
+ throw await heldFileRefusal(engine, row, root, path, 'occupied', options.remote === true)
?? new OperationError('source_changed', 'An unindexed file already occupies the canonical page path.', 'Import the file before replacing it.');
}
return { path, root, content, expectedBeforeHash: before ? sha256(before) : null };
@@ -183,15 +184,25 @@ export async function prepareFileTarget(engine: BrainEngine, row: Pick<WriteRequ
* file is repaired. Two point reads, only when the refusal already fires.
*/
async function heldFileRefusal(engine: BrainEngine, row: Pick<WriteRequest, 'source_id' | 'slug'>, root: string, path: string,
- kind: 'drift' | 'occupied'): Promise<OperationError | null> {
+ kind: 'drift' | 'occupied', remote: boolean): Promise<OperationError | null> {
const [source] = await engine.executeRaw<{ incarnation: string }>('SELECT incarnation::text AS incarnation FROM sources WHERE id=$1', [row.source_id]);
const hold = source ? await readGitHold(engine, row.source_id, source.incarnation, relative(root, path).split(sep).join('/')) : null;
if (!hold) return null;
+ const effect = kind === 'drift' ? `page ${row.slug} keeps its last good revision and` : `page ${row.slug} does not exist yet and its path`;
+ if (remote) {
+ // Remote callers get the code and the host operator's command, never the held path or key.
+ const error = opError('source_changed', heldFileMessage(kind, hold.code),
+ `Sync holds this page's canonical file in source ${row.source_id} (${hold.code}${hold.meta.reason ? `, ${hold.meta.reason}` : ''}) because gbrain cannot import it, `
+ + `so ${effect} is read-only for put_page until the brain host operator repairs the file; retrying this write refuses the same way. Relay the fix to the user, then submit the intended write with a new request_id. Neither copy was overwritten.`,
+ { fix: hostOperatorFix([row.source_id], `The canonical file of page ${row.slug} is held (${hold.code}): only the brain host operator can inspect and repair it.`) });
+ if (kind === 'drift') error.detail = 'file_database_drift';
+ return error;
+ }
const fix = gitHoldFix(hold);
const where = [hold.meta.line !== undefined ? `line ${hold.meta.line}` : '', hold.meta.key ? `key "${hold.meta.key}"` : ''].filter(Boolean).join(', ');
const error = opError('source_changed', heldFileMessage(kind, hold.code),
`Sync holds ${hold.path} in source ${row.source_id} (${hold.code}${hold.meta.reason ? `, ${hold.meta.reason}` : ''}${where ? ` at ${where}` : ''}) because gbrain cannot import it, `
- + `so ${kind === 'drift' ? `page ${row.slug} keeps its last good revision and` : `page ${row.slug} does not exist yet and its path`} is read-only for put_page until the file is repaired; retrying this write refuses the same way. `
+ + `so ${effect} is read-only for put_page until the file is repaired; retrying this write refuses the same way. `
+ `Repair the file first (${shellQuote(fix.argv ?? [])}: ${fix.why}), then submit the intended write with a new request_id. Neither copy was overwritten.`, { fix });
if (kind === 'drift') error.detail = 'file_database_drift';
return error;
@@ -247,7 +258,7 @@ export async function preparePageMutation(engine: BrainEngine, row: WriteRequest
// Tombstones still own their recorded artifact. Purge always attempts its
// removal before the guarded hard-delete and receipt commit; failure rolls
// back to the prior row, and replay survives the eventual absence of that row.
- const file = await prepareFileTarget(engine, row, snapshot, null, undefined, { allowMissing: purge || options.allowMissingFile, activePack });
+ const file = await prepareFileTarget(engine, row, snapshot, null, undefined, { allowMissing: purge || options.allowMissingFile, activePack, remote: row.authority.remote });
return { observedRevision, noop, file, ...await pageDatabaseOnlyPublication(engine, row, file), apply: async tx => {
if (purge) {
await tx.deletePage(row.slug, source);
@@ -310,7 +321,7 @@ export async function preparePageMutation(engine: BrainEngine, row: WriteRequest
resolveParsedSubtype(incoming, snapshot.page);
const tags = versionTags ?? [...new Set([...snapshot.tags,...incoming.tags])].sort();
if (digest(canonical(snapshot.page,snapshot.tags)) === digest(canonical(incoming,tags))) {
- const file=await prepareFileTarget(engine,row,snapshot,targetDeleted ? null : serializePageToMarkdown(snapshot.page,snapshot.tags),undefined,{ activePack });
+ const file=await prepareFileTarget(engine,row,snapshot,targetDeleted ? null : serializePageToMarkdown(snapshot.page,snapshot.tags),undefined,{ activePack, remote: row.authority.remote });
return {observedRevision,noop:true,file,...await pageDatabaseOnlyPublication(engine,row,file),
apply:async()=>({...pageNoopAdvisories(row),status:'skipped',slug:row.slug,source_id:row.source_id,noop:true,chunks:0,chunk_skip_reason:'write_skipped',
...(row.operation==='capture'?{channel:'capture',content_hash:p.capture_hash}:{})})};
@@ -375,7 +386,7 @@ export async function preparePageMutation(engine: BrainEngine, row: WriteRequest
? await prepareAutomaticLinks(engine,row.slug,ready.parsedPage,row.source_id) : undefined;
const capture = row.operation === 'capture' && typeof p.capture_path === 'string' && typeof p.capture_file_hash === 'string'
? { path: p.capture_path, hash: p.capture_file_hash } : undefined;
- const file = await prepareFileTarget(engine, row, snapshot, targetDeleted ? null : rendered, undefined, { capture, allowMissing: options.allowMissingFile, activePack });
+ const file = await prepareFileTarget(engine, row, snapshot, targetDeleted ? null : rendered, undefined, { capture, allowMissing: options.allowMissingFile, activePack, remote: row.authority.remote });
const mintMode = file && !snapshot?.page.source_path ? await scannerSlugRootMode(engine, row.source_id, file.root) : undefined;
const sourcePath = file && mintMode ? scannerSourcePath(file.root, file.path, mintMode) : undefined;
// An inferred mode is pinned with the first origin it mints, so later pages cannot flip the inference (#5610).
diff --git a/test/sync-hold-surfaces.test.ts b/test/sync-hold-surfaces.test.ts
index 1302853413..662cbb969d 100644
--- a/test/sync-hold-surfaces.test.ts
+++ b/test/sync-hold-surfaces.test.ts
@@ -200,6 +200,19 @@ describe('a page whose file is held refuses put_page naming the hold', () => {
expect(error.message).not.toContain('continued tweet text');
});
+ test('a remote caller hitting a held file gets the code and the host operator command, never the path or key', async () => {
+ const { snapshot, file, row } = await seed('notes/held-remote');
+ writeFileSync(file, readFileSync(file, 'utf8').replace(/^title:.*$/m, 'title: Broken\n continued tweet text'));
+ await writeGitHold(engine, hold(sourceId, await incarnationOf(engine, sourceId), 'notes/held-remote.md', { page_id: Number(snapshot.page.id) }));
+ let error: any;
+ try { await prepareFileTarget(engine, row, snapshot, 'Replacement', undefined, { remote: true }); } catch (e) { error = e; }
+ expect(error).toMatchObject({ code: 'source_changed', detail: 'file_database_drift', message: heldFileMessage('drift', 'invalid_frontmatter'),
+ fix: { actor: 'host_admin', argv: ['gbrain', 'repair', 'frontmatter', '--source', sourceId] } });
+ const text = JSON.stringify({ message: error.message, suggestion: error.suggestion, fix: error.fix });
+ expect(text).toContain('read-only for put_page until the brain host operator repairs the file');
+ for (const hidden of ['held-remote.md', '"title"', root, 'continued tweet text']) expect(text).not.toContain(hidden);
+ });
+
test('an unindexed held file occupying a new page path names the hold', async () => {
const file = join(root, 'notes', 'new-held.md');
mkdirSync(dirname(file), { recursive: true });