Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(GST-1209): full-ci follow-ups: a retried move to the state a staging holds is a no-op; the repair resolves the host id through hostIdFor and spawns git with gitChildEnv; attribution inventory lists the repair; the manifest-reads fixture stages a tiling row

garrytandc350eeadf83
diff --git a/docs/architecture/system-of-record.md b/docs/architecture/system-of-record.md
index d6dd1bfd43..3c56c4b6d0 100644
--- a/docs/architecture/system-of-record.md
+++ b/docs/architecture/system-of-record.md
@@ -307,6 +307,7 @@ or a physical projection that leaves attribution untouched):
- `src/core/repair/redacted-facts.ts` (1): a `managed_maintenance_restore_redacted_facts` request on managed brains; the unmanaged restore of database-only fact text runs in `maintenanceTransaction`.
- `src/core/repair/rename-identity.ts` (5): `gbrain repair rename-identity` carry, unmanaged brains only (a managed brain refuses `managed_remedy_unavailable` before writing); the fence-row merge, the provisional page's retirement, the slug move and the import run in one `maintenanceTransaction` under both page locks after the plan-hash recheck.
- `src/core/repair/secret-spans.ts` (1): `gbrain repair secret-spans` (GST-327) rewrites the credential spans of each preview-approved page under the page key: inside `withCoordinatedWrite` with maintenance attribution on managed brains, `withWriteAttribution` with maintenance attribution on unmanaged ones.
+- `src/core/repair/segmented-tiling.ts` (1): `gbrain repair segmented-tiling` (GST-1209) versions each preview-approved damaged page (`createVersion`) before republishing it from the file Git holds at HEAD, under the page key: inside `withCoordinatedWrite` with maintenance attribution on managed brains, `withWriteAttribution` with maintenance attribution on unmanaged ones.
- `src/core/repair/stale-atoms.ts` (1): the retirement stamp. On managed brains it runs in the `managed_maintenance_retire_stale_atoms` request's apply; on unmanaged brains the retirement is an approved atom generation (`publishGeneration`, one `maintenanceTransaction`) and the stamp runs in its `onRetired` callback inside that transaction.
- `src/core/repair/take-supersession.ts` (1): `gbrain repair take-supersession` reprojection: a receipted `managed_maintenance_take_reproject` request on managed brains, `maintenanceTransaction` on unmanaged ones; fence edits go through a revision-bound `put_page`.
- `src/core/repair/timeline-comments.ts` (1): `gbrain repair timeline-comments` row cleanup: `withCoordinatedWrite` under the page key on managed brains, `maintenanceTransaction` otherwise.
diff --git a/src/core/engine-sql/page-body.ts b/src/core/engine-sql/page-body.ts
index bf0a4b9764..b880326138 100644
--- a/src/core/engine-sql/page-body.ts
+++ b/src/core/engine-sql/page-body.ts
@@ -386,7 +386,7 @@ export async function openBodyStaging(exec: SqlExecutor, input: BodyStagingInput
* Moves a live staging to `state`; a move to a live state restarts its idle deadline (never past its hard one). A staging
* that is no longer live stays as it is (GST-1209): a fact purge's abort or a sweep's expiry deleted its staged rows, so a
* write moving it on (to prepared or committed) refuses body_staging_expired instead of reviving it, and an abort or expiry
- * of it changes nothing.
+ * of it changes nothing. A move to the state it already holds (a retried commit) is a no-op.
*/
export async function setBodyStagingState(exec: SqlExecutor, id: string, state: BodyStagingState, opts: { commitRequestId?: string | null } = {}): Promise<void> {
const { rows } = await exec.run<{ id: string }>(sqlFragment`UPDATE page_body_stagings SET state = ${state}, updated_at = now(),
@@ -396,6 +396,7 @@ export async function setBodyStagingState(exec: SqlExecutor, id: string, state:
WHERE id = ${id}::uuid AND state = ANY(${LIVE_STAGING_STATES}::text[]) RETURNING id`);
if (rows.length || state === 'aborted' || state === 'expired') return;
const { rows: gone } = await exec.run<{ slug: string; state: string }>(sqlFragment`SELECT slug, state FROM page_body_stagings WHERE id = ${id}::uuid`);
+ if (gone[0]?.state === state) return;
throw bodyStagingGoneError(gone[0]?.slug ?? id, id, gone[0]?.state ?? null);
}
diff --git a/src/core/repair/segmented-tiling.ts b/src/core/repair/segmented-tiling.ts
index 5333319b61..40150f99f7 100644
--- a/src/core/repair/segmented-tiling.ts
+++ b/src/core/repair/segmented-tiling.ts
@@ -37,7 +37,8 @@ import { withCoordinatedWrite, withWriteAttribution } from '../persistence/conte
import { readManagedMode } from '../persistence/managed-mode.ts';
import { getWorktreeBinding } from '../persistence/ownership.ts';
import { checkOwner, ownerUnavailableError } from '../persistence/owner-refusal.ts';
-import { localHostId } from '../persistence/identity.ts';
+import { hostIdFor } from '../persistence/identity.ts';
+import { gitChildEnv } from '../git-env.ts';
import { loadRepairBusySet, repairBusy, repairBusyError } from '../persistence/repair-busy.ts';
import { clearGitHold, readGitHold, requestGitHoldRetry, type GitHoldRecord } from '../persistence/sync-holds.ts';
import { unreadPageImportScreen } from '../persistence/sync-screen.ts';
@@ -74,7 +75,7 @@ const previewCommand = (selection: Selection) => `gbrain repair segmented-tiling
const receiptKey = (hash: string, pageId: number) => `${hash}:${pageId}`;
const audit = createAuditWriter<{ ts: string } & Record<string, unknown>>({ featureName: 'segmented-tiling', errorLabel: 'segmented-tiling-audit', errorTrailer: '; repair continues' });
const sha256 = (bytes: Uint8Array) => createHash('sha256').update(bytes).digest('hex');
-const git = (root: string, args: string[], maxBuffer = 1024 * 1024) => execFileSync('git', ['-C', root, ...args], { maxBuffer, stdio: ['ignore', 'pipe', 'pipe'] });
+const git = (root: string, args: string[], maxBuffer = 1024 * 1024) => execFileSync('git', ['-C', root, ...args], { env: gitChildEnv(), maxBuffer, stdio: ['ignore', 'pipe', 'pipe'] });
const gitText = (root: string, args: string[]): string | null => { try { return git(root, args).toString('utf8').trim(); } catch { return null; } };
/**
@@ -89,8 +90,8 @@ async function sourceCheckout(engine: BrainEngine, sourceId: string): Promise<{
if (managed) {
const binding = await getWorktreeBinding(engine, sourceId);
if (binding) {
- const owner = checkOwner(binding, source.incarnation, localHostId());
- if (owner.reason) throw ownerUnavailableError({ sourceId, reason: owner.reason, binding, incarnation: source.incarnation, hostId: localHostId(), remote: false, work: 'the segmented-tiling repair' });
+ const owner = checkOwner(binding, source.incarnation, hostIdFor(engine));
+ if (owner.reason) throw ownerUnavailableError({ sourceId, reason: owner.reason, binding, incarnation: source.incarnation, hostId: hostIdFor(engine), remote: false, work: 'the segmented-tiling repair' });
root = join(owner.binding.local_path, owner.binding.relative_path);
}
}
diff --git a/test/engine-sql-page-body.test.ts b/test/engine-sql-page-body.test.ts
index 19a9901dfe..cfa38b64ab 100644
--- a/test/engine-sql-page-body.test.ts
+++ b/test/engine-sql-page-body.test.ts
@@ -90,6 +90,7 @@ describe('manifest and flip', () => {
const replacement = segmentText('## Three, edited\nthree!\n');
const staged = await stageGeneration(engine, { pageId: page.pageId, slug: 'notes/manifest-reads', genFrom: 2, texts: [replacement] });
await engine.executeRaw('UPDATE page_body_manifest SET seq = 2 WHERE staging_id = $1::uuid', [staged.stagingId]);
+ await engine.executeRaw('UPDATE page_body_manifest SET byte_start = $2 WHERE staging_id = $1::uuid', [staged.stagingId, t[0]!.body.byteLength + t[1]!.body.byteLength]);
expect((await engine.readManifest(page.pageId)).length).toBe(3);
const stagedRows = await engine.readManifest(page.pageId, { stagingId: staged.stagingId });
expect(stagedRows.map(r => r.occurrenceId)).toEqual(staged.occurrences);