Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(dream): synthesize postprocess settles after a local writer replace (#8110)

garrytan4d72a0256991
diff --git a/docs/architecture/key-files/core-cycle-continued.md b/docs/architecture/key-files/core-cycle-continued.md
index 10f4067407..63357f765e 100644
--- a/docs/architecture/key-files/core-cycle-continued.md
+++ b/docs/architecture/key-files/core-cycle-continued.md
@@ -4,7 +4,7 @@
Current behavior and load-bearing invariants; history belongs in Git and CHANGELOG.
Synthesis, patterns, consolidation and connector atom gating.
-- `src/core/cycle/synthesize-postprocess.ts` — Managed synthesis quote repair and provenance stamping publish together through a deterministic maintenance request bound to the source incarnation, transcript hash, child output, and original committed page revision. `loadCommittedSynthesisOutputs` finds each child's committed output by canonicalizing the retained `brain_put_page` input (`parseWriteRequestId` for the request id, `journaledPageSlug` for the slug) before matching request id, source, incarnation, committed put_page, slug and revision (#6616); slug discovery (`collectChildPutPageSlugs`) and postprocessing share that set. The immutable committed receipt is the completion checkpoint: later runs skip finalized output rather than repairing user edits. Pending work replays its retained intent under the original revision and existing authority/file guards; missing provenance fails closed. A publish still `write_pending` after the job's shared maintenance wait is deferred, never written or finalized: `withPublishPending` turns the phase result into `warn` with `publish_pending` and `SYNTH_PUBLISH_DEFERRED` ("publish deferred (writer busy); finishes next cycle, no action needed"), `synthesize.ts` leaves `dream.synthesize.last_completion_ts` unstamped, and the next ordinary cycle resumes the same deterministic request id. Runs the same claim verification as the unmanaged path (`groundSource` + `resolveVerifyPrior` + `verifyDreamPage` from `synthesize-verify.ts`, scoped by the ref's `first_write_at`, else the child write epochs passed as `sinceByTranscript`) and stamps the dream provenance fields only on dream-owned pages (`isDreamOwnedPage`); the published page reconciles its automatic links like an ordinary put_page (`page-prepare.ts` treats `managed_maintenance_page` intents as auto-linked); the unmanaged path retains its fail-open behavior. Covered by `test/managed-synthesis-postprocess.test.ts` and its PostgreSQL E2E wrapper. The optional `grounding` pass (System One S8) runs after `verifyDreamPage` and before `publishMaintenancePage`, so the published revision never carries a unit S8 quarantined; pinned by `test/decide/writepath-phase.serial.test.ts`. The postprocess stamps the transcript's `seat` next to `raw_source` on dream-owned output. `deferPublishOrThrow` defers an output or summary publish that is still pending, whose admission gave up on database contention (`publicationHold` in `persistence/accepted-pending.ts`), or whose unadmitted request was refused `queue_capacity` by a positive principal or brain outstanding-request cap (`isWriteCapacityWait` + `outstandingCapacityOf`, no write receipt) exactly like the pending case: `publish_pending`, phase warn, no cooldown stamp, and the next cycle retries the same request id; a contended or capacity-blocked publish writes one stderr line. A zero cap, cumulative quotas, unknown capacity, a capacity refusal carrying a receipt and every other error fail the phase. Pinned by `test/managed-synthesis-postprocess.test.ts`. #5575 I2: each output publishes with `derivation` from its transcript (`dream-taint.ts` `transcriptDerivation`; `meetingTranscriptsDir` in opts); a retained request replays the declaration it was admitted with. #6360: when the page moved after the child commit, a drift made only by committed local `managed_maintenance_page` requests (for example the next cycle's lint `created:` fix) over a body equal to the child's is adopted: the postprocess runs on the current snapshot under a request id that also binds that base revision (found again on replay among the later requests). Any other later write leaves the page alone as a `PostprocessConflict` (`withPostprocessConflicts`: phase `warn`, `details.postprocess_conflicts` with the read-only `gbrain get` fix); it is reported once (config `dream.synthesize.postprocess_conflicts`, 30-day memory) and does not block the cooldown stamp. #6757: the committed-outputs join (`COMMITTED_OUTPUT_REQUESTS_SQL`) compares `request_id` as a uuid within the source, served by `persistence_requests_request_id`. The phase loads it through `loadCommittedOutputsOrDefer`; a statement timeout there, or on an output's own `getWriteRequest`, defers the pass instead of failing it (`withPublishPending` with `details.postprocess_deferred`: phase warn, no cooldown stamp, no summary). Deferred and publish-pending outputs are carried by `synthesize-postprocess-deferral.ts` and finished by a later pass from the carried fields, with no child; `finishDeferredPostprocess` (run after the phase body by `runPhaseSynthesize`) finishes them when that pass returned before postprocessing. GBRA-130 A-4: outputs run as `ItemRun` items (`synthesize.postprocess`). A postprocess request that ended conflict, failed or cancelled, or whose retained intent no longer matches, is a `PostprocessConflict` (the request's `error_code`, else `recovery_required`; fix `gbrain write-request -- <id>`) dropped from the pending row; the summary names each slug and code. Other item errors let the rest run and the pass save, then fail the phase; run-scoped errors and `item_failures_systemic` stop it and record nothing. Pinned by `test/pathological/a4-synthesize-postprocess.test.ts`.
+- `src/core/cycle/synthesize-postprocess.ts` — Managed synthesis quote repair and provenance stamping publish together through a deterministic maintenance request bound to the source incarnation, transcript hash, child output, and original committed page revision. `loadCommittedSynthesisOutputs` finds each child's committed output by canonicalizing the retained `brain_put_page` input (`parseWriteRequestId` for the request id, `journaledPageSlug` for the slug) before matching request id, source, incarnation, committed put_page, slug and revision (#6616); slug discovery (`collectChildPutPageSlugs`) and postprocessing share that set. The immutable committed receipt is the completion checkpoint: later runs skip finalized output rather than repairing user edits. Pending work replays its retained intent under the original revision and existing authority/file guards; missing provenance fails closed. A publish still `write_pending` after the job's shared maintenance wait is deferred, never written or finalized: `withPublishPending` turns the phase result into `warn` with `publish_pending` and `SYNTH_PUBLISH_DEFERRED` ("publish deferred (writer busy); finishes next cycle, no action needed"), `synthesize.ts` leaves `dream.synthesize.last_completion_ts` unstamped, and the next ordinary cycle resumes the same deterministic request id. Runs the same claim verification as the unmanaged path (`groundSource` + `resolveVerifyPrior` + `verifyDreamPage` from `synthesize-verify.ts`, scoped by the ref's `first_write_at`, else the child write epochs passed as `sinceByTranscript`) and stamps the dream provenance fields only on dream-owned pages (`isDreamOwnedPage`); the published page reconciles its automatic links like an ordinary put_page (`page-prepare.ts` treats `managed_maintenance_page` intents as auto-linked); the unmanaged path retains its fail-open behavior. Covered by `test/managed-synthesis-postprocess.test.ts` and its PostgreSQL E2E wrapper. The optional `grounding` pass (System One S8) runs after `verifyDreamPage` and before `publishMaintenancePage`, so the published revision never carries a unit S8 quarantined; pinned by `test/decide/writepath-phase.serial.test.ts`. The postprocess stamps the transcript's `seat` next to `raw_source` on dream-owned output. `deferPublishOrThrow` defers an output or summary publish that is still pending, whose admission gave up on database contention (`publicationHold` in `persistence/accepted-pending.ts`), or whose unadmitted request was refused `queue_capacity` by a positive principal or brain outstanding-request cap (`isWriteCapacityWait` + `outstandingCapacityOf`, no write receipt) exactly like the pending case: `publish_pending`, phase warn, no cooldown stamp, and the next cycle retries the same request id; a contended or capacity-blocked publish writes one stderr line. A zero cap, cumulative quotas, unknown capacity, a capacity refusal carrying a receipt and every other error fail the phase. Pinned by `test/managed-synthesis-postprocess.test.ts`. #5575 I2: each output publishes with `derivation` from its transcript (`dream-taint.ts` `transcriptDerivation`; `meetingTranscriptsDir` in opts); a retained request replays the declaration it was admitted with. #6360: when the page moved after the child commit, a drift made only by committed local `managed_maintenance_page` requests (for example the next cycle's lint `created:` fix) over a body equal to the child's is adopted: the postprocess runs on the current snapshot under a request id that also binds that base revision (found again on replay among the later requests). Any other later write leaves the page alone as a `PostprocessConflict` (`withPostprocessConflicts`: phase `warn`, `details.postprocess_conflicts` with the read-only `gbrain get` fix); it is reported once (config `dream.synthesize.postprocess_conflicts`, 30-day memory) and does not block the cooldown stamp. #6757: the committed-outputs join (`COMMITTED_OUTPUT_REQUESTS_SQL`) compares `request_id` as a uuid within the source, served by `persistence_requests_request_id`. The phase loads it through `loadCommittedOutputsOrDefer`; a statement timeout there, or on an output's own `getWriteRequest`, defers the pass instead of failing it (`withPublishPending` with `details.postprocess_deferred`: phase warn, no cooldown stamp, no summary). Deferred and publish-pending outputs are carried by `synthesize-postprocess-deferral.ts` and finished by a later pass from the carried fields, with no child; `finishDeferredPostprocess` (run after the phase body by `runPhaseSynthesize`) finishes them when that pass returned before postprocessing. GBRA-130 A-4: outputs run as `ItemRun` items (`synthesize.postprocess`). A postprocess request that ended conflict, failed or cancelled, or whose retained intent no longer matches, is a `PostprocessConflict` (the request's `error_code`, else `recovery_required`; fix `gbrain write-request -- <id>`) dropped from the pending row; the summary names each slug and code. Other item errors let the rest run and the pass save, then fail the phase; run-scoped errors and `item_failures_systemic` stop it and record nothing. Pinned by `test/pathological/a4-synthesize-postprocess.test.ts`. #8110: the earlier postprocess request found among the later requests settles from that row, not a lookup by the current principal, so one made by a local CLI writer registration that `gbrain auth local-writer register cli --replace` revoked still settles; it is authorized under the current local CLI writer's live grant, and a remote principal's row never qualifies. A child output whose writer registration was revoked or no longer grants the page is a `permission_denied` `PostprocessConflict` (fix `gbrain auth local-writer list --json`).
- `src/core/cycle/synthesize-postprocess-deferral.ts` — #6757: config row `dream.synthesize.postprocess_pending`, keyed by source and bound to its incarnation: each pending output (child request id, journaled and input slug, revision, job key, transcript path, content hash, seat, first write) and the source's consecutive deferred passes with the last read and its elapsed time. `isPostprocessReadTimeout` unwinds a cancelled cycle first, then accepts only SQLSTATE 57014 with the statement-timeout message (a user cancel is not deferred). `recordPostprocessPass` drops settled outputs, adds deferred and carried ones and resets the count on a pass that deferred nothing; `repeatedPostprocessDeferrals` lists sources at three or more for doctor `persistence_request_indexes`. The replay runs no child job and verifies a replayed output's quotes against the transcript as it is now, even when it grew after synthesis; a quote not found there goes to `unverified_claims` like any failed quote.
Patterns, drift and the entries after them: [Core Cycle (patterns)](core-cycle-patterns.md).
diff --git a/src/core/cycle/synthesize-postprocess.ts b/src/core/cycle/synthesize-postprocess.ts
index 462f39baae..7304aa32f7 100644
--- a/src/core/cycle/synthesize-postprocess.ts
+++ b/src/core/cycle/synthesize-postprocess.ts
@@ -3,7 +3,7 @@ import type { BrainEngine } from '../engine.ts';
import { throwIfAborted } from '../abort-check.ts';
import { parseMarkdown, serializePageToMarkdown } from '../markdown.ts';
import type { Action } from '../agent-output.ts';
-import { opError } from '../ops/contract.ts';
+import { OperationError, opError } from '../ops/contract.ts';
import { ItemRun } from '../item-failure.ts';
import { readFix } from '../ops/op-fix.ts';
import { publicationHold } from '../persistence/accepted-pending.ts';
@@ -238,6 +238,13 @@ export async function postprocessManagedSynthesis(
return undefined;
}
};
+ /**
+ * #8110: a postprocess receipt found on the page that an earlier local CLI writer registration of this brain made
+ * (`gbrain auth local-writer register cli --replace` revoked it). It settles under this local writer's live grant,
+ * checked as a stored request; a remote principal's row never qualifies.
+ */
+ const localLineage = (row: WriteRequest) => localMaintenance(row) && row.principal_kind === 'local_cli'
+ && authority.writer.principal.kind === 'local_cli' && !authority.writer.remote && row.principal_id !== authority.writer.principal.id;
let grounded: GroundedSource | undefined;
const run = new ItemRun('synthesize.postprocess');
let itemError: unknown;
@@ -275,8 +282,7 @@ export async function postprocessManagedSynthesis(
if (ours) {
base = ours.intent!.expected_revision as string;
requestId = ours.request_id;
- prior = await readPrior(index, requestId);
- if (prior === undefined) return 'stop';
+ prior = ours;
} else if (snapshot && later.filter(r => r.state === 'committed').every(localMaintenance) && sameBody(output.request.intent?.content, snapshot.page, ref.slug)) {
base = snapshot.revision;
requestId = postprocessId(base);
@@ -290,7 +296,7 @@ export async function postprocessManagedSynthesis(
}
}
if (prior) {
- await authorizeStoredRequest(engine, prior);
+ await authorizeStoredRequest(engine, localLineage(prior) ? { ...prior, authority: authority.writer } : prior);
if (prior.state === 'committed') { settled.push(outputRequestId); finalizedRefs.push(finalizedRef); return; }
if (['conflict', 'failed', 'cancelled'].includes(prior.state)) {
settled.push(outputRequestId);
@@ -301,7 +307,16 @@ export async function postprocessManagedSynthesis(
return;
}
}
- await authorizeStoredRequest(engine, output.request);
+ try { await authorizeStoredRequest(engine, output.request); } catch (error) {
+ // #8110: the child's writer registration was revoked (a host move replaces it) or no longer grants the page; that
+ // stays true on every pass, so the output is left alone and reported once instead of failing the phase each pass.
+ if (!(error instanceof OperationError && error.code === 'permission_denied')) throw error;
+ settled.push(outputRequestId);
+ if (!reported.has(outputRequestId)) conflicts.push({ slug: ref.slug, source_id: ref.source_id, request_id: outputRequestId, code: 'permission_denied',
+ message: `Synthesis child request ${outputRequestId} that wrote page ${ref.slug} in source ${ref.source_id} is no longer authorized (${error.message}), so postprocessing left the page alone and did not verify or stamp it. Review the page; this output is not retried.`,
+ fix: readFix('Lists this brain\'s local writer registrations, their trust lanes and grants, read-only.', { argv: ['gbrain', 'auth', 'local-writer', 'list', '--json'] }) });
+ return;
+ }
let content: string, unchanged = false;
if (prior) {
if (prior.intent?.kind !== 'managed_maintenance_page' || prior.intent.expected_revision !== base || typeof prior.intent.content !== 'string') {
diff --git a/test/managed-synthesis-postprocess.test.ts b/test/managed-synthesis-postprocess.test.ts
index 08f98fe424..c0ee501548 100644
--- a/test/managed-synthesis-postprocess.test.ts
+++ b/test/managed-synthesis-postprocess.test.ts
@@ -12,6 +12,8 @@ import { OperationError } from '../src/core/ops/contract.ts';
import { acquireWorktree, getWorktreeBinding } from '../src/core/persistence/ownership.ts';
import { __setMaintenanceWriteWaitForTests } from '../src/core/persistence/maintenance-wait.ts';
import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';
+import { registerLocalWriter } from '../src/core/persistence/identity.ts';
+import { operationsByName } from '../src/core/operations.ts';
import { clientRequestUuid } from '../src/core/persistence/preconditions.ts';
import { COMMITTED_OUTPUT_REQUESTS_SQL, journaledPageSlug } from '../src/core/cycle/synthesize-postprocess.ts';
import { POSTPROCESS_PENDING_KEY } from '../src/core/cycle/synthesize-postprocess-deferral.ts';
@@ -958,3 +960,68 @@ test('synthesis preserves non-outstanding publication refusals', async () => {
expect(caught, item.id).toBe(error);
}
});
+
+/**
+ * #8110: a postprocess that committed under an earlier local CLI writer registration settles after
+ * `gbrain auth local-writer register cli --replace` revokes that registration, instead of re-deriving the page and
+ * failing revision_conflict on every pass; another (remote) principal still cannot read that receipt.
+ */
+test('#8110: a committed postprocess settles after the local CLI writer is replaced; a remote principal still cannot read its receipt', async () => {
+ await fixture(async ({ engine, sourceId, opts, calls }) => {
+ await interruptAfterChild(engine, sourceId, opts);
+ const slug = await outputSlug(engine, sourceId);
+ const read = engine.readPageSnapshot;
+ const spy = spyOn(engine, 'readPageSnapshot').mockImplementation(async function (this: BrainEngine, target, options) {
+ if (this === engine && target.includes('dream-cycle-summaries/') && options?.sourceId === sourceId) throw new Error('simulated phase interruption after postprocessing commit');
+ return read.call(this, target, options);
+ });
+ try { expect((await runPhaseSynthesize(engine, opts)).status).toBe('fail'); } finally { spy.mockRestore(); }
+ const [postprocess] = await engine.executeRaw<{ request_id: string; principal_id: string; state: string }>(
+ "SELECT request_id,principal_id,state FROM persistence_requests WHERE source_id=$1 AND slug=$2 AND intent->>'kind'='managed_maintenance_page'", [sourceId, slug]);
+ expect(postprocess.state).toBe('committed');
+ const processed = (await engine.readPageSnapshot(slug, { sourceId }))!;
+ await disposePersistenceConsumer(engine);
+ const replacement = await registerLocalWriter(engine, 'cli', undefined, true);
+ expect(replacement.id).not.toBe(postprocess.principal_id);
+ const spent = calls();
+ const replay = await runPhaseSynthesize(engine, opts);
+ expect({ status: replay.status, error: replay.error }).toEqual({ status: 'ok', error: undefined });
+ expect(replay.details.pages_written).toBe(0);
+ expect(calls()).toBe(spent);
+ expect((await engine.readPageSnapshot(slug, { sourceId }))!.revision).toBe(processed.revision);
+ expect(await engine.executeRaw("SELECT 1 FROM persistence_requests WHERE source_id=$1 AND slug=$2 AND intent->>'kind'='managed_maintenance_page' AND principal_id<>$3",
+ [sourceId, slug, postprocess.principal_id])).toEqual([]);
+ const clientId = `remote-${randomUUID().slice(0, 8)}`;
+ await engine.executeRaw(`INSERT INTO oauth_clients (client_id, client_name, scope, source_id, federated_read)
+ VALUES ($1, $1, 'read write', $2, ARRAY[$2])`, [clientId, sourceId]);
+ const remote = { engine, sourceId, remote: true as const, config: { engine: engine.kind, embedding_disabled: true }, dryRun: false,
+ logger: { info() {}, warn() {}, error() {} }, auth: { token: 'remote-token', clientId, principal: { kind: 'oauth_client' as const, id: clientId }, scopes: ['read', 'write'], sourceId } };
+ const error = await operationsByName.get_write_request!.handler(remote as never, { request_id: postprocess.request_id }).then(() => null, e => e);
+ expect(error).toBeInstanceOf(OperationError);
+ expect(error.code).toBe('not_found');
+ expect(await journal.getWriteRequest(engine, { kind: 'oauth_client', id: clientId }, postprocess.request_id)).toBeNull();
+ });
+}, 120_000);
+
+
+test('#8110: a child output whose stdio writer was replaced warns once and is left alone, instead of failing every pass', async () => {
+ await fixture(async ({ engine, sourceId, opts, calls }) => {
+ await interruptAfterChild(engine, sourceId, opts);
+ const slug = await outputSlug(engine, sourceId);
+ const [child] = await engine.executeRaw<{ principal_kind: string; principal_id: string }>(
+ "SELECT principal_kind,principal_id FROM persistence_requests WHERE source_id=$1 AND slug=$2 AND operation='put_page' AND state='committed'", [sourceId, slug]);
+ const replacement = await registerLocalWriter(engine, 'stdio', undefined, true);
+ expect({ kind: child.principal_kind, replaced: replacement.id !== child.principal_id }).toEqual({ kind: 'local_stdio', replaced: true });
+ const before = (await engine.readPageSnapshot(slug, { sourceId }))!;
+ const spent = calls();
+ const recovered = await runPhaseSynthesize(engine, opts);
+ expect({ status: recovered.status, error: recovered.error }).toEqual({ status: 'warn', error: undefined });
+ expect(recovered.details.postprocess_conflicts).toEqual([expect.objectContaining({ slug, code: 'permission_denied' })]);
+ expect(calls()).toBe(spent);
+ expect((await engine.readPageSnapshot(slug, { sourceId }))!.revision).toBe(before.revision);
+ await disposePersistenceConsumer(engine);
+ const again = await runPhaseSynthesize(engine, opts);
+ expect({ status: again.status, conflicts: again.details.postprocess_conflicts }).toEqual({ status: 'ok', conflicts: undefined });
+ });
+}, 120_000);
+