Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(persistence): a conversion apply pushes its door commits where durability applies (#7644)

garrytan33e0429bd5bb
diff --git a/src/commands/sources-canonical.ts b/src/commands/sources-canonical.ts
index 1d955309a6..97c2ccbd7d 100644
--- a/src/commands/sources-canonical.ts
+++ b/src/commands/sources-canonical.ts
@@ -61,6 +61,7 @@ export function describeGitPreview(p: GitConversionPreview): string {
function describeResult(r: GitConversionResult): string {
const lines = [`Source ${r.source_id} ${r.status === 'converted' ? 'is git-canonical' : 'is git-canonical, with work left'}: ${r.committed.pages} page(s) committed in ${r.committed.groups} door commit(s), ${r.withheld.pages} private page(s) withheld (${r.withheld.commits.length} removal commit(s)), ${r.holds_cleared} hold(s) cleared, ${r.parked.retried} parked effect(s) retried and ${r.parked.reclassified} reclassified.`];
+ if (r.push.state === 'pending') lines.push(`Push pending (${r.push.code}): ${r.push.notice}`);
if (r.status === 'incomplete') lines.push(describeGitPreview(r.after));
return lines.join('\n');
}
@@ -105,7 +106,7 @@ async function withConversionReport<T>(engine: BrainEngine, sourceId: string, js
{ docs: DOCS, detail: 'interrupted' });
const report = { status: 'interrupted', signal, source_id: sourceId, ...at, canonical };
process.stdout.write(`${json ? JSON.stringify({ ...report, ...toAgentError(error, { transport: 'cli', command: 'sources canonical', render: cliRenderContext() }) })
- : `Conversion interrupted (${signal}) during ${at.phase}${at.round ? ` round ${at.round}` : ''}: ${at.scanned}/${at.total} scanned, ${at.groups_committed} group(s) committed; source canonical state: ${canonical}.\nNext: ${error.suggestion}`}\n`);
+ : `Conversion interrupted (${signal}) during ${at.phase}${at.round ? ` round ${at.round}` : ''}: ${at.scanned}/${at.total} scanned, ${at.groups_committed} group(s) committed; source canonical state: ${canonical}.${at.push?.state === 'pending' ? `\nPush pending (${at.push.code}): ${at.push.notice}` : ''}\nNext: ${error.suggestion}`}\n`);
await triggerCleanupAndExit(SIGNAL_EXIT[signal] ?? 1);
})();
};
diff --git a/src/core/persistence/git-conversion.ts b/src/core/persistence/git-conversion.ts
index d2643124ed..fe925e5bd8 100644
--- a/src/core/persistence/git-conversion.ts
+++ b/src/core/persistence/git-conversion.ts
@@ -44,7 +44,7 @@ import type { BrainEngine } from '../engine.ts';
import type { SyncOpts } from '../../commands/sync.ts';
import { loadConfig } from '../config.ts';
import { resolveSourceId } from '../source-resolver.ts';
-import { opError, type OperationError } from '../ops/contract.ts';
+import { opError, OperationError } from '../ops/contract.ts';
import { trustedCliRequired } from '../ops/op-fix.ts';
import { currentSubmissionAuthority } from '../minions/submission-authority.ts';
import { inlineExportMarkdown, readExportPage, readExportPages, readExportWithdrawals, type ExportSnapshot } from '../export-snapshot.ts';
@@ -52,6 +52,8 @@ import { scannerSlugRootMode, scannerSourcePath } from '../write-through.ts';
import { censusGitGap, type GitGapCensus, type GitGapPage } from '../../commands/sources-git-gap.ts';
import { compareInlineExportFile, ignoredPaths, readBlobs, type DeclaredClass, type ExportFileComparison } from './git-gap.ts';
import { commitThroughDoor } from './git-door.ts';
+import { gitRootAhead, pushGitRoot } from './effect-git.ts';
+import { DURABILITY_NOT_ENABLED_REASON, gitDurabilityPolicy } from './git-durability-policy.ts';
import { currentVerifiedLocalWriter, localHostId } from './identity.ts';
import { acquireWorktree, readManagedMode } from './ownership.ts';
import { resolveManagedSyncContext, type ManagedSyncContext } from './sync-discovery.ts';
@@ -84,7 +86,13 @@ export type ConversionPhase = typeof CONVERSION_PHASES[number];
* went through of its total. In a strict pass, `round` is the pass round, `total` the paths its level read until it
* admits, then its batch, and `scanned` the batch entries settled.
*/
-export interface ConversionProgress { phase: ConversionPhase; groups_committed: number; scanned: number; total: number; round?: number }
+export interface ConversionProgress { phase: ConversionPhase; groups_committed: number; scanned: number; total: number; round?: number; push?: ConversionPush }
+/**
+ * Where the push of the apply's door commits stands: `pushed`; `pending` (the commits are local and the next page write's
+ * push-only effect pushes them; `code` and `notice` say why it did not land); `skipped` (`reason`: durability is off,
+ * there was nothing to push, or the branch tracks no remote).
+ */
+export interface ConversionPush { state: 'pushed' | 'pending' | 'skipped'; reason?: string; code?: string; notice?: string }
type ConversionReporter = (patch: Partial<ConversionProgress>) => void;
export const ADOPT_STATES = ['database_ahead', 'undeclared_database_only', 'worktree_only'] as const;
@@ -447,6 +455,8 @@ export interface GitConversionResult {
withheld: { pages: number; commits: string[] };
holds_cleared: number;
parked: { retried: number; reclassified: number };
+ /** #7644: the push of the door commits, as it stood when the apply ended. */
+ push: ConversionPush;
/** The plan after the flag was set and a strict pass ran: nothing left on a completed conversion. */
after: GitConversionPreview;
}
@@ -473,6 +483,7 @@ export async function applyGitConversion(engine: BrainEngine, sourceId: string,
const committed = await adoptGroups(engine, plan, previewCommand, report);
report({ phase: 'withhold', scanned: 0, total: plan.withhold.length });
const withheld = await withholdGroups(engine, plan, previewCommand, groups => report({ scanned: progress.scanned + groups.pages, groups_committed: committed.groups + groups.commits }));
+ report({ push: await pushConversion(engine, plan.context) });
const parked = await settleParked(engine, sourceId, plan.parked);
report({ phase: 'recheck', scanned: 0, total: 0 });
// The planned holds are cleared only once this check passes, so a refusal keeps them: a held path is judged as the
@@ -503,8 +514,35 @@ export async function applyGitConversion(engine: BrainEngine, sourceId: string,
await recordConversion(engine, sourceId, plan.incarnation, 'git', expect, plan.preview.head);
report({ phase: 'strict_pass_2', scanned: 0, total: 0, round: undefined });
passOrThrow(sourceId, await performManagedSync(engine, { sourceId, noPull: true, full: true, ...processing, onProgress: passProgress(report) }));
+ const final = await pushConversion(engine, plan.context);
+ const push = final.reason === 'nothing_to_push' ? progress.push! : final;
+ report({ push });
const after = (await planGitConversion(engine, sourceId)).preview;
- return { status: after.remaining === 0 ? 'converted' : 'incomplete', source_id: sourceId, head: after.head, committed, withheld, holds_cleared: holdsCleared, parked, after };
+ return { status: after.remaining === 0 ? 'converted' : 'incomplete', source_id: sourceId, head: after.head, committed, withheld, holds_cleared: holdsCleared, parked, push, after };
+}
+
+/**
+ * #7644: a door commit of the apply belongs to no write request, so no push-only Git effect pushes it (G2 pairs every other
+ * door commit with one). Where durability applies, the apply pushes them itself under the worktree writer lock, after the
+ * adoption and withhold groups and once at the end. A push that fails leaves them local and the apply goes on: the next
+ * page write's push-only effect pushes the branch.
+ */
+async function pushConversion(engine: BrainEngine, context: ManagedSyncContext): Promise<ConversionPush> {
+ const { root } = context;
+ const pending = (code: string, why: string): ConversionPush => ({ state: 'pending', code,
+ notice: `The conversion's door commits are committed locally, but their push did not land (${why}). The next page write's push sends them; to push now, run gbrain git commit-push --push-only --path ${root}.` });
+ try {
+ if (!(await gitDurabilityPolicy(context.binding, root)).durable) return { state: 'skipped', reason: DURABILITY_NOT_ENABLED_REASON };
+ if (!(await gitRootAhead(root))) return { state: 'skipped', reason: 'nothing_to_push' };
+ const lock = await acquireWorktree(context.binding, LOCK_WAIT_MS, undefined, engine, { yieldLanes: true });
+ if (!lock) return pending('writer_busy', `the writer lock stayed busy for ${LOCK_WAIT_MS / 1000}s`);
+ try {
+ const pushed = await pushGitRoot(root);
+ return pushed.push === 'committed' ? { state: 'pushed' } : { state: 'skipped', reason: pushed.reason };
+ } finally { await lock.release(); }
+ } catch (error) {
+ return pending(error instanceof OperationError ? error.code : 'git_push_unavailable', error instanceof Error ? error.message : String(error));
+ }
}
/** A strict pass's progress events as the apply's: a round's level read sets the total, its batch the scanned entries. */
diff --git a/test/git-canonical-conversion-progress.test.ts b/test/git-canonical-conversion-progress.test.ts
index da8f8e8ecb..fbe4ebf65a 100644
--- a/test/git-canonical-conversion-progress.test.ts
+++ b/test/git-canonical-conversion-progress.test.ts
@@ -8,7 +8,7 @@ import { afterAll, describe, expect, test } from 'bun:test';
import { execFileSync, spawnSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
-import { join } from 'node:path';
+import { dirname, join } from 'node:path';
import { withCoordinatedWrite } from '../src/core/persistence/context.ts';
import { performManagedSync } from '../src/core/persistence/sync-run.ts';
import { applyGitConversion, CONVERSION_GROUP_MAX, CONVERSION_PHASES, planGitConversion, type ConversionProgress } from '../src/core/persistence/git-conversion.ts';
@@ -30,6 +30,18 @@ const bulkSetup = ({ root }: { root: string }) => {
for (let i = 0; i < BULK; i++) writeFileSync(join(root, 'bulk/p' + String(i).padStart(3, '0') + '.md'), note('bulk/p' + i, 'Committed body ' + i + '.'));
git(root, 'add', '-A'); git(root, 'commit', '-qm', 'bulk');
};
+/** bulkSetup with git durability's shape: a bare tracking remote beside the checkout that holds the seeded commit. */
+const remoteOf = (root: string) => join(dirname(root), 'remote.git');
+const durableSetup = (brain: { root: string }) => {
+ bulkSetup(brain);
+ execFileSync('git', ['init', '-q', '--bare', remoteOf(brain.root)]);
+ git(brain.root, 'remote', 'add', 'origin', remoteOf(brain.root));
+ git(brain.root, 'push', '-q', 'origin', 'HEAD:refs/heads/main');
+ git(brain.root, 'branch', '--set-upstream-to=origin/main');
+};
+const enableDurability = (engine: Parameters<Parameters<typeof managedBrain>[0]>[0]['engine']) =>
+ engine.executeRaw("UPDATE persistence_host_bindings SET git_durability='enabled'");
+const remoteHead = (root: string) => execFileSync('git', ['-C', remoteOf(root), 'rev-parse', 'refs/heads/main'], { encoding: 'utf8' }).trim();
/** Every bulk page edited in the database alone: the plan adopts them in two door groups. */
const editBulk = (engine: Parameters<Parameters<typeof managedBrain>[0]>[0]['engine']) => engine.transaction(tx => withCoordinatedWrite(tx, ['default'], () => tx.executeRaw(
"UPDATE pages SET compiled_truth=compiled_truth || ' Edited in the database.', knowledge_revision=gen_random_uuid(), revision_write_request_id=NULL WHERE source_id='default' AND slug LIKE 'bulk/%'"), TEST_WRITE_ATTRIBUTION));
@@ -73,6 +85,35 @@ for (const backend of testBackends()) describe('[' + backend + '] #7644: a conve
expect(lines.map(line => line.phase)).toEqual([...CONVERSION_PHASES]);
expect(lines.at(-1)).toMatchObject({ event: 'conversion_progress', source_id: 'default', phase: 'strict_pass_2', groups_committed: 2 });
}, { databaseUrl, setup: bulkSetup }), 300_000);
+
+ test('with durability on, the apply pushes its door commits: the remote branch ends at HEAD', () => managedBrain(async ({ engine, root }) => {
+ await enableDurability(engine);
+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
+ await editBulk(engine);
+ const seeded = remoteHead(root);
+ const { preview } = await planGitConversion(engine, 'default');
+ const seen: ConversionProgress[] = [];
+ const result = await applyGitConversion(engine, 'default', preview.preview_hash, { noEmbed: true, noExtract: true, onProgress: progress => seen.push(progress) });
+ expect(result).toMatchObject({ status: 'converted', committed: { groups: 2 }, push: { state: 'pushed' } });
+ expect(remoteHead(root)).toBe(git(root, 'rev-parse', 'HEAD'));
+ expect(remoteHead(root)).not.toBe(seeded);
+ // The push lands before the strict pass, so an apply stopped there has already sent its door commits.
+ expect(seen.find(progress => progress.phase === 'strict_pass_1')?.push).toEqual({ state: 'pushed' });
+ }, { databaseUrl, setup: durableSetup }), 300_000);
+
+ test('a remote that rejects the push still converts, and the receipt reports the push pending', () => managedBrain(async ({ engine, root }) => {
+ await enableDurability(engine);
+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
+ await editBulk(engine);
+ const seeded = remoteHead(root);
+ writeFileSync(join(remoteOf(root), 'hooks', 'pre-receive'), '#!/bin/sh\necho "pushes are closed" >&2\nexit 1\n', { mode: 0o755 });
+ const { preview } = await planGitConversion(engine, 'default');
+ const result = await applyGitConversion(engine, 'default', preview.preview_hash, { noEmbed: true, noExtract: true });
+ expect(result).toMatchObject({ status: 'converted', committed: { groups: 2 }, push: { state: 'pending', code: 'git_push_unavailable' } });
+ expect(result.push.notice).toContain('committed locally');
+ expect(remoteHead(root)).toBe(seeded);
+ expect(git(root, 'rev-list', '--count', '@{upstream}..HEAD')).toBe('2');
+ }, { databaseUrl, setup: durableSetup }), 300_000);
});
describe('#7644: SIGTERM during the strict pass', () => {
@@ -115,7 +156,7 @@ describe('#7644: SIGTERM during the strict pass', () => {
expect(code).toBe(143);
const report = JSON.parse(stdout.trim().split('\n').at(-1)!);
expect(report).toMatchObject({ status: 'interrupted', signal: 'SIGTERM', source_id: 'notes', phase: 'strict_pass_1', groups_committed: 0, canonical: 'mixed',
- code: 'git_conversion_incomplete', detail: 'interrupted' });
+ code: 'git_conversion_incomplete', detail: 'interrupted', push: { state: 'skipped', reason: 'durability_not_enabled' } });
expect(report.scanned).toBeLessThanOrEqual(report.total);
// The conversion is crash-safe: the source stays mixed, and a new preview applies.
expect(JSON.parse(cli('sources', 'canonical', 'notes', '--to', 'git', '--dry-run', '--json').trim().split('\n').at(-1)!)).toMatchObject({ strict: false, discard: { count: 0 } });