Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Label repairs decline a page the managed publish refuses instead of counting it as a systemic failure (GST-2603)

garrytanb81301921bf9
diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.ts
index a075bf650a..7cb7d38d11 100644
--- a/src/core/persistence/page-prepare.ts
+++ b/src/core/persistence/page-prepare.ts
@@ -318,14 +318,25 @@ async function frontmatterDriftWrite(engine: BrainEngine, slug: string, fileText
export function labelEditFile(slug: string, before: Buffer | FileBytes, content: string, edit: LabelEdit, snapshot: PageSnapshot, activePack?: ParseOpts['activePack']): string {
const fit = labelEditFit(slug, before, content, edit, snapshot, activePack);
if (typeof fit === 'string') return fit;
- const keys = fit.keys.join(', ');
- throw opError('invalid_params', 'The label edit cannot be made to the page\'s file line by line.', fit.keys.length
- ? `Nothing was written for ${slug}: with only its label lines edited, its file still differs from the page in ${keys}, so the label-only write refuses rather than re-render the file. Make ${keys} in the file match the page (or make the label change in the file by hand), commit it, and run gbrain sync for the source.`
- : `Nothing was written for ${slug}: its file cannot take the change without being re-rendered (a label key with a block or multi-line value or a duplicate key, no frontmatter block, or a file too large to edit in memory), so the label-only write refuses. Make the change in the file by hand, commit it, and run gbrain sync for the source.`,
- { detail: LABEL_EDIT_REFUSED });
+ throw labelEditRefused(snapshot.page, fit.keys, edit);
+}
+/**
+ * GST-2603: a label write's refusal when the page's file holds the page but cannot take `edit` line by line (`keys`: what
+ * still differs, empty when the label line itself cannot be edited). Repairs count `label_edit_refused` as the page declining
+ * the write, never toward the run's systemic-failure stop (repair/core.ts), whether the preview's check, the apply's check or
+ * the managed publish (labelEditFile) refuses.
+ */
+export function labelEditRefused(page: { source_id: string; slug: string }, keys: string[], edit: LabelEdit): OperationError {
+ const ref = `${page.source_id}:${page.slug}`;
+ const labels = [...Object.keys(edit.set), ...edit.remove].join(', ') || 'the label';
+ return keys.length
+ ? opError('label_edit_refused', `${ref} was not written: with only its label lines edited, its file still differs from the page in ${keys.join(', ')}.`,
+ `In the page's file, make ${keys.join(', ')} match the page (gbrain get --source ${page.source_id} -- ${page.slug} prints it), commit it, run gbrain sync --source ${page.source_id}, then preview the repair again.`,
+ { reason: 'file_differs', detail: keys.join(',') })
+ : opError('label_edit_refused', `${ref} was not written: its file's ${labels} line cannot be edited as one line.`,
+ `In the page's file, write ${labels} as one plain line in the frontmatter block (no block or multi-line value, no duplicate key; add a frontmatter block if the file has none), commit it, run gbrain sync --source ${page.source_id}, then preview the repair again.`,
+ { reason: 'label_line', detail: labels });
}
-/** GST-2603: the detail of labelEditFile's refusal; repairs count it as the page declining the write (repair/core.ts). */
-export const LABEL_EDIT_REFUSED = 'label_edit_refused';
/**
* labelEditFile without the refusal: the file to publish, or the fields that keep the edited file from reading as `content`
* (a frontmatter key, title, type, tags, body or timeline; never a stamp only the database holds, align-stored.ts), empty
diff --git a/src/core/repair/label-write.ts b/src/core/repair/label-write.ts
index cbf0d8ddf3..4c0f4fdc26 100644
--- a/src/core/repair/label-write.ts
+++ b/src/core/repair/label-write.ts
@@ -22,7 +22,7 @@ import { withTrustKeep } from '../persistence/context.ts';
import { existingHostIdFor } from '../persistence/identity.ts';
import type { LabelEdit } from '../persistence/label-edit.ts';
import { getWorktreeBinding, managedPersistenceEnabled } from '../persistence/ownership.ts';
-import { fileSnapshotDifference, labelEditFit, prepareFileTarget } from '../persistence/page-prepare.ts';
+import { fileSnapshotDifference, labelEditFit, labelEditRefused, prepareFileTarget } from '../persistence/page-prepare.ts';
import { readSyncFile } from '../persistence/sync-discovery.ts';
import { maintenancePreflight, publishMaintenancePage, publishMaintenancePages, type MaintenanceAuthority } from '../persistence/prepared-maintenance.ts';
import type { RepairItemOutcome } from './core.ts';
@@ -159,19 +159,6 @@ export async function labelFileRefusal(engine: BrainEngine, snapshot: PageSnapsh
return { reason: 'label_edit_refused', keys: fit.keys, next: error.suggestion ?? '', error };
}
-/** GST-2603: a label write's refusal when the page's file holds the page but cannot take `edit` line by line (`keys`: what still differs, empty when the label line itself cannot be edited). */
-export function labelEditRefused(page: { source_id: string; slug: string }, keys: string[], edit: LabelEdit): OperationError {
- const ref = `${page.source_id}:${page.slug}`;
- const labels = [...Object.keys(edit.set), ...edit.remove].join(', ') || 'the label';
- return keys.length
- ? opError('label_edit_refused', `${ref} was not written: with only its label lines edited, its file still differs from the page in ${keys.join(', ')}.`,
- `In the page's file, make ${keys.join(', ')} match the page (gbrain get --source ${page.source_id} -- ${page.slug} prints it), commit it, run gbrain sync --source ${page.source_id}, then preview the repair again.`,
- { reason: 'file_differs', detail: keys.join(',') })
- : opError('label_edit_refused', `${ref} was not written: its file's ${labels} line cannot be edited as one line.`,
- `In the page's file, write ${labels} as one plain line in the frontmatter block (no block or multi-line value, no duplicate key; add a frontmatter block if the file has none), commit it, run gbrain sync --source ${page.source_id}, then preview the repair again.`,
- { reason: 'label_line', detail: labels });
-}
-
/**
* One label-only page write at `revision`: the maintenance door on a managed brain (`authority`, from maintenancePreflight),
* the import under the page lock otherwise. The revision it wrote; null when the page moved before an unmanaged import.
diff --git a/test/label-repair-derived-stamps-gst2603.test.ts b/test/label-repair-derived-stamps-gst2603.test.ts
index cdb937aa39..23d3fee294 100644
--- a/test/label-repair-derived-stamps-gst2603.test.ts
+++ b/test/label-repair-derived-stamps-gst2603.test.ts
@@ -31,6 +31,7 @@ import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
import { _resetWriteThroughCacheForTest } from '../src/core/write-through.ts';
import { runRepairCommand } from '../src/commands/repair.ts';
import { runAccessRules } from '../src/commands/access-rules.ts';
+import { addMember } from '../src/core/access/members.ts';
import { _resetCliExitVerdictForTests, currentExitCode } from '../src/core/cli-force-exit.ts';
import { GBrainOAuthProvider } from '../src/core/oauth-provider.ts';
import { sqlQueryForEngine } from '../src/core/sql-query.ts';
@@ -241,6 +242,20 @@ for (const backend of testBackends()) describe('[' + backend + '] label repairs
expect(next.details.held[0].next).toContain('write visibility as one plain line');
}, { databaseUrl, setup }), 240_000);
+ test('a --readers apply declines pages whose readable_by line its file cannot take at publish: label_edit_refused, never item_failures_systemic, exit 0', () => managedBrain(async ({ engine, root }) => {
+ const slugs = [1, 2, 3, 4, 5, 6].map(n => 'notes/readers-' + n);
+ // Five files write readable_by as a block list, which a label-only write cannot splice; the preview holds none of them, so the managed publish refuses.
+ await seed(engine, root, Object.fromEntries(slugs.map((slug, index) => [slug, note('Readers ' + (index + 1), index < 5 ? ['readable_by:', ' - bob-example'] : [], 'Readers body.')])));
+ await addMember(engine, 'alice-example');
+ const argv = ['visibility', ...slugs.flatMap(slug => ['--slug', slug]), '--readers', 'alice-example', '--allow-without-owner'];
+ const { applied, exit } = await previewApply(engine, argv);
+ expect({ applied: applied.applied, declined: applied.declined, stopped: applied.stopped, complete: applied.complete, exit })
+ .toEqual({ applied: 1, declined: { label_edit_refused: 5 }, stopped: undefined, complete: true, exit: 0 });
+ expect(applied.failed.map((failed: { item: string; reason: string }) => [failed.item, failed.reason]).sort()).toEqual(slugs.slice(0, 5).map(slug => ['default:' + slug, 'label_edit_refused']));
+ await settleManagedGitEffects(engine);
+ expect(onDisk(root, 'notes/readers-1')).toBe(note('Readers 1', ['readable_by:', ' - bob-example'], 'Readers body.'));
+ }, { databaseUrl, setup }), 240_000);
+
test('repair private-labels clears a label on a page with gbrain\'s stamps, changing only the label line', () => managedBrain(async ({ engine, root }) => {
const slug = 'notes/labelled-example';
await seed(engine, root, { [slug]: note('Labelled', ['type: note', 'access: private'], 'Labelled body.') },