Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(facts): re-admit pre-fix stale-revision fact refusals; one moved-refusal predicate; one extraction lookup per preview (#7602 review)

garrytancacd075279cd
diff --git a/docs/architecture/key-files/core-persistence-facts.md b/docs/architecture/key-files/core-persistence-facts.md
index e52c8d1abf..d6f75a2947 100644
--- a/docs/architecture/key-files/core-persistence-facts.md
+++ b/docs/architecture/key-files/core-persistence-facts.md
@@ -5,7 +5,7 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL
Managed facts extraction: the entity-intent preparer and publisher, conversation facts batches and the unmanaged derived index. The extraction pipeline itself is in [Google and Loops (continued)](google-and-loops-continued.md) (`src/core/facts/backstop.ts`) and [Core Services (2/3)](core-services-2.md) (`src/core/facts/extract.ts`).
- `src/core/facts/bulk-days.ts` — #7247 the one rule doctor `fact_bulk_dates` and `gbrain repair fact-claim-dates` share. `findBulkFactDays` (one read-only aggregate over active facts of active sources, optionally scoped) returns each UTC `valid_from` day holding at least `BULK_DAY_RULE` (200 facts, 25% of the source, 20 entities) with counts, top source tags and `dated_claims`. `claimDate` returns a claim's only `YYYY-MM-DD` token when it is a real date from 1900 to the day before; the aggregate's SQL applies the same token and bounds.
-- `src/core/persistence/{facts-maintenance,facts-prepare}.ts` — the managed facts publisher and preparer behind the backstop's `publishManagedFacts` / `resumeManagedFacts` and the explicit fact writers (`managed-fact-write.ts`, `remember`); the shared maintenance dispatch they ride is in [Core Persistence](core-persistence.md). Each `ManagedFactsSession` carries one `MaintenanceWriteWait` (`maintenance-wait.ts`) built from the caller's `FactsBackstopCtx.deadlineAtMs` (the facts-absorb job passes its deadline; other callers get the 30 s base) and its abort signal: `collectManagedFacts` waits that long for each accepted request through `awaitWrite`, a cancelled job stops waiting at once, and a request still nonterminal after the wait leaves as `write_pending` with its receipt (#6778, `test/facts-absorb-publish-wait-6778.test.ts`). GBRA-130 A-36: `collectManagedFacts` awaits every row before it throws. An entity request that ended on a code of its own page (`ENTITY_REFUSAL_CODES`: `revision_conflict`, `invalid_fence`, `page_body_segmented`, …; not a file-check or withdrawal refusal, which the follow-up walks own) is reported as `refused` (slug, code, request id) on `ManagedFactsResult` and the `extract_facts` result when the session sets `reportRefusals` and another entity of the batch committed (the backstop sets it; `managed-fact-write.ts` and `remember` do not and keep the refusal), and the completion receipt refused only as incomplete is not a failure of its own; any other terminal row, then a pending one, then a refusal with nothing committed beside it, throws as its receipt. Test: `test/pathological/a-36-facts-batch-partial-refusal.test.ts` (PGLite and Postgres). `facts-maintenance.ts` / `facts-prepare.ts` (#6506): an automatic extraction's entity intent carries `withdrawn: 'skip'` (set by the backstop's `publishManagedFacts` call, never by `managed-fact-write.ts` or `remember`), under which `prepareManagedFactsMutation` skips a candidate `isFactWithdrawn` matches (after `assertFactNotPurged`, which still refuses `purged_content`) and counts it as `withdrawn_skipped` on the entity outcome, summed into the completion outcome, `ManagedFactsResult` and the `extract_facts` result (the unmanaged pipeline counts its own B-10 skip into the same field); without the flag the refusal stands. At locked validation the per-entry purge and withdrawal checks run before the page's fence re-validation, and under the flag a claim forgotten since preparation refuses `revision_conflict` with `error.withdrawn` (so the receipt's `error_detail.origin` is `withdrawal`); no `extract_facts` request is re-prepared. `resumeManagedFacts` runs `followWithdrawalRefusals` for an input-keyed session (`inputKeyed`, no request id): the `followFileRefusals` walk generalized as `followRefusals`, matching entity refusals by `withdrawnFromDetail(error_detail)` or the pre-detail `invalid_params` + `FACT_WITHDRAWN_MESSAGE` pair, re-admitting the retained facts under `digest([batchKey, 'withdrawal-retry', n])` with the flag (`readmitWithdrawnFacts`: a fact forgotten at its retained visibility keeps that visibility so preparation skips it instead of narrowing it to private), bounded by `MAX_FILE_REFUSAL_RETRIES`, historical receipts untouched. Tests: `test/managed-facts-withdrawn-skip-6506.test.ts`, `test/managed-facts-withdrawal-recovery-6506.test.ts` (PGLite and Postgres). #7602: an automatic extraction with no caller request id (`entityMoveRetry`, set by the backstop; explicit writers keep the refusal) also runs `followEntityMoves`, after admission in `publishManagedFacts` and on resume: an entity request refused `revision_conflict` because another write moved its entity page (`FACTS_ENTITY_MOVED_MESSAGES` in `verb-errors.ts`: changed after admission, changed during preparation, deduplication state changed, and the coordinator's `PAGE_CHANGED_DURING_PREPARATION_MESSAGE`, which `coordinator.ts` and `group-publish.ts` throw) is re-admitted under `digest([batchKey, 'entity-moved-retry', n])` at the page's current revision with its retained intent (`readmitMovedFacts`; a deleted or replaced page keeps the refusal), no model call, each batch settled first so a follow-up that loses again is followed in the same run, bounded by `MAX_FILE_REFUSAL_RETRIES`; a follow-up refused for another reason (a held file) is not re-admitted, and the walk carries every request it did not follow. A refusal `collectManagedFacts` still throws after the walks is marked `retryable: false`, and the facts-absorb handler dead-letters such a journaled refusal on its first attempt. Test: `test/facts-entity-moved-7602.test.ts` (PGLite and Postgres).
+- `src/core/persistence/{facts-maintenance,facts-prepare}.ts` — the managed facts publisher and preparer behind the backstop's `publishManagedFacts` / `resumeManagedFacts` and the explicit fact writers (`managed-fact-write.ts`, `remember`); the shared maintenance dispatch they ride is in [Core Persistence](core-persistence.md). Each `ManagedFactsSession` carries one `MaintenanceWriteWait` (`maintenance-wait.ts`) built from the caller's `FactsBackstopCtx.deadlineAtMs` (the facts-absorb job passes its deadline; other callers get the 30 s base) and its abort signal: `collectManagedFacts` waits that long for each accepted request through `awaitWrite`, a cancelled job stops waiting at once, and a request still nonterminal after the wait leaves as `write_pending` with its receipt (#6778, `test/facts-absorb-publish-wait-6778.test.ts`). GBRA-130 A-36: `collectManagedFacts` awaits every row before it throws. An entity request that ended on a code of its own page (`ENTITY_REFUSAL_CODES`: `revision_conflict`, `invalid_fence`, `page_body_segmented`, …; not a file-check or withdrawal refusal, which the follow-up walks own) is reported as `refused` (slug, code, request id) on `ManagedFactsResult` and the `extract_facts` result when the session sets `reportRefusals` and another entity of the batch committed (the backstop sets it; `managed-fact-write.ts` and `remember` do not and keep the refusal), and the completion receipt refused only as incomplete is not a failure of its own; any other terminal row, then a pending one, then a refusal with nothing committed beside it, throws as its receipt. Test: `test/pathological/a-36-facts-batch-partial-refusal.test.ts` (PGLite and Postgres). `facts-maintenance.ts` / `facts-prepare.ts` (#6506): an automatic extraction's entity intent carries `withdrawn: 'skip'` (set by the backstop's `publishManagedFacts` call, never by `managed-fact-write.ts` or `remember`), under which `prepareManagedFactsMutation` skips a candidate `isFactWithdrawn` matches (after `assertFactNotPurged`, which still refuses `purged_content`) and counts it as `withdrawn_skipped` on the entity outcome, summed into the completion outcome, `ManagedFactsResult` and the `extract_facts` result (the unmanaged pipeline counts its own B-10 skip into the same field); without the flag the refusal stands. At locked validation the per-entry purge and withdrawal checks run before the page's fence re-validation, and under the flag a claim forgotten since preparation refuses `revision_conflict` with `error.withdrawn` (so the receipt's `error_detail.origin` is `withdrawal`); no `extract_facts` request is re-prepared. `resumeManagedFacts` runs `followWithdrawalRefusals` for an input-keyed session (`inputKeyed`, no request id): the `followFileRefusals` walk generalized as `followRefusals`, matching entity refusals by `withdrawnFromDetail(error_detail)` or the pre-detail `invalid_params` + `FACT_WITHDRAWN_MESSAGE` pair, re-admitting the retained facts under `digest([batchKey, 'withdrawal-retry', n])` with the flag (`readmitWithdrawnFacts`: a fact forgotten at its retained visibility keeps that visibility so preparation skips it instead of narrowing it to private), bounded by `MAX_FILE_REFUSAL_RETRIES`, historical receipts untouched. Tests: `test/managed-facts-withdrawn-skip-6506.test.ts`, `test/managed-facts-withdrawal-recovery-6506.test.ts` (PGLite and Postgres). #7602: an automatic extraction with no caller request id (`entityMoveRetry`, set by the backstop; explicit writers keep the refusal) also runs `followEntityMoves`, after admission in `publishManagedFacts` and on resume: an entity request refused `revision_conflict` because another write moved its entity page (`FACTS_ENTITY_MOVED_MESSAGES` in `verb-errors.ts`: changed after admission, changed during preparation, deduplication state changed, the coordinator's `PAGE_CHANGED_DURING_PREPARATION_MESSAGE`, which `coordinator.ts` and `group-publish.ts` throw, and the page layer's `PAGE_CHANGED_AFTER_READ_MESSAGE`, how a move during preparation was stored before the fix; `isEntityMovedRefusal` is the one predicate the walk and the repair preview share) is re-admitted under `digest([batchKey, 'entity-moved-retry', n])` at the page's current revision with its retained intent (`readmitMovedFacts`; a deleted or replaced page keeps the refusal), no model call, each batch settled first so a follow-up that loses again is followed in the same run, bounded by `MAX_FILE_REFUSAL_RETRIES`; a follow-up refused for another reason (a held file) is not re-admitted, and the walk carries every request it did not follow. A refusal `collectManagedFacts` still throws after the walks is marked `retryable: false`, and the facts-absorb handler dead-letters such a journaled refusal on its first attempt. Test: `test/facts-entity-moved-7602.test.ts` (PGLite and Postgres).
- `src/core/facts/same-page-shadow.ts` — #6333 the shadow counter both fact writers share. `samePageRewording(engine, sourceId, fact, embedding)` re-reads the entity's nearest live same-visibility candidate (`listSupersessionCandidates`, the pool and filters of `decideSingleFact`'s cosine pass) and answers whether `wouldDedupSamePage` (capture-dedup.ts: same lane, same `source_session`, same visibility, cosine ≥ 0.85, no diverging guard token) would have called the fact a rewording; `warnSamePageShadow` is the one stderr spelling (`[facts-backstop] #6333 shadow: …; both kept`). `facts-prepare.ts` calls it for every candidate `decideSingleFact` did not match and counts `would_dedup_same_page` on the entity outcome, the completion outcome, `ManagedFactsResult` and the `extract_facts` result; the count is frozen in the receipt, so a replay reports it and does not recount. Nothing is dropped; this is the managed mirror of the unmanaged backstop's counter (`backstop.ts`), which never fired on a managed brain. Tests: `test/backstop-same-page-shadow-managed-6333.test.ts` (PGLite and Postgres), `test/backstop-same-page-shadow-6333.test.ts` (unmanaged).
- `src/core/persistence/derived-facts.ts` — `writeDerivedFacts`: the unmanaged conversation fact index commits in one maintenance transaction under the maintenance principal; on a managed brain it refuses with `writer_coordinator_required`, because every managed facts/takes writer publishes a receipted maintenance request (`test/receipt-coverage.test.ts` lists every coordinated write site). Tests: `test/write-attribution-misc.test.ts`.
- `src/core/facts/conversation-publication.ts` — managed conversation facts as database-only `managed_maintenance_conversation_facts` batch requests (`submitDatabaseMaintenanceIntent` on `ANCHOR_SLUG`, a dot-led slug no page can hold, dispatched from `prepareMaintenanceMutation`). Each page's frozen entry (`buildConversationPage`: `FrozenExtractedFact` rows, its generation and attempt, vectors only under the brain's facts embedding signature) joins the run's batch; `enqueueManagedPage` publishes it once `DEFAULT_BATCH_CAPS` is reached (25 pages, 10 with extracted facts, 8 MiB) and `flushManagedBatch` at the end of the run. The generation key (protocol, source incarnation, page id, revision, parser-input version token, `CONVERSATION_EXTRACTOR_VERSION`, selectors, newest extractor row id) is checked before model work by `startManagedPage` against `loadGenerationIndex` (the writer's unsettled and failed batches on the anchor, read once per run): a member of an unsettled batch waits and replays its receipt, a member of a retryably failed batch is resubmitted from its stored entry, a deterministic failure of a batch that held it alone or three failed batches block it (GBRA-130 A-51: `publishBatch` re-publishes a multi-page batch that failed deterministically in halves, so only the page that fails alone takes the code; `test/pathological/a-51-conversation-batch-booking.test.ts`); all without model calls. The batch request id digests its members' (generation, attempt) pairs; `submitConversationPages` raises every member's attempt past an id another write holds, or whose receipt lost its intent without committing (#7516: a redacted `purged_content` refusal or a compacted failure takes the attempt history `loadGenerationIndex` reads, so the same id came back on every run as `idempotency_conflict` after a paid extraction; test `test/conversation-facts-redacted-batch-7516.test.ts`, + Postgres arm). The preparer locks every member's key and applies each on its own (`applyEntry`): a deleted, replaced or moved page is skipped and reported, a page whose rows fail validation gets a durable `blocked:` not-extractable outcome, vectors under another signature are dropped; none fails the batch. `estimateConversationIntentBytes`/`conversationIntentByteLimit` skip a page whose largest entry cannot fit before any model call; `assertConversationAdmissionHeadroom` stops admitting at 80% of the writer's outstanding requests, reserved receipt bytes or lifetime ids (`maintenance_backpressure`, exit 12, naming the resource and a `--limit` that fits), up front for the run's planned batches and before each new batch. `publishConversationFacts` (the managed apply and the unmanaged writer) passes fact rows through the write gate at the page's derived tier and retires the prior batch only for a complete replacement (#7013: `full` scope, at least one fact, nothing held or rejected) or on a page with no active extractor facts, through `replaceConversationFacts`, which deletes the page's extractor rows (expiring referenced and keeping label-retired ones) and inserts the rows numbered above every remaining row, failing on a lost insert. Otherwise the prior facts stay: the outcome row replaces the page's prior outcome rows (so the page is not paid for again), a `window` run (`--since` dropped messages) adds its facts beside them, and a `partial` run (a `--segment-limit` cut, a page changed mid-run) publishes no facts over them. Each entry carries its `scope`; an entry frozen without one is `partial` without its outcome, `window` with a `--since` selector, else `full`. Tests: `test/pathological/h4-conversation-facts-zero-yield.test.ts` (managed and unmanaged, + Postgres arm). Tests: `test/conversation-facts-managed.test.ts` (+ Postgres arm), managed-connector contract cases `extract_conversation_facts*`.
diff --git a/src/core/page-state/types.ts b/src/core/page-state/types.ts
index 5020a17fad..bd2b6a0f83 100644
--- a/src/core/page-state/types.ts
+++ b/src/core/page-state/types.ts
@@ -84,13 +84,16 @@ export interface PageSnapshot {
* supplied none and the page exists (#5385 → `revision_required`: the caller's omission, not a race, so a client
* can read the page and resubmit instead of treating it as contention).
*/
+/** A stale expected revision; stored on receipts (#7602 matches it for fact entity requests refused before the fix). */
+export const PAGE_CHANGED_AFTER_READ_MESSAGE = 'The page changed after it was read. Read its current revision before retrying.';
+
export class PageRevisionConflictError extends Error {
readonly code: 'revision_conflict' | 'revision_required' | 'revision_backfill_pending';
constructor(readonly expectedRevision: string | null | undefined, readonly currentRevision: string | null) {
super(currentRevision === null ? 'The page no longer exists at the expected revision.'
: expectedRevision === undefined ? 'The page already exists; an expected revision is required.'
: expectedRevision === null ? 'The page was created after it was read. Read its current revision before retrying.'
- : 'The page changed after it was read. Read its current revision before retrying.');
+ : PAGE_CHANGED_AFTER_READ_MESSAGE);
this.name = 'PageRevisionConflictError';
this.code = expectedRevision === undefined && currentRevision !== null ? 'revision_required' : 'revision_conflict';
}
diff --git a/src/core/persistence/facts-maintenance.ts b/src/core/persistence/facts-maintenance.ts
index fc7b97746f..ea49e58692 100644
--- a/src/core/persistence/facts-maintenance.ts
+++ b/src/core/persistence/facts-maintenance.ts
@@ -30,7 +30,7 @@ import { MaintenanceWriteWait } from './maintenance-wait.ts';
import { digest, requireUuid, sha256 } from './digest.ts';
import { isTerminal, type WriteAuthority, type WriteRequest } from './model.ts';
import type { WriteReceipt } from './types.ts';
-import { FACTS_BATCH_INCOMPLETE_MESSAGE, FACTS_ENTITY_MOVED_MESSAGES } from './verb-errors.ts';
+import { FACTS_BATCH_INCOMPLETE_MESSAGE, isEntityMovedRefusal } from './verb-errors.ts';
import { assertAmbientCaptureAdmissible, captureGateLaneForSource } from '../facts/capture-sources.ts';
import { declareDerivation, type DerivationDeclaration } from '../trust/taint.ts';
import type { TaintInput, WriteTrust } from '../trust/tier.ts';
@@ -451,11 +451,7 @@ async function followEntityMoves(engine: BrainEngine, session: ManagedFactsSessi
: session.wait.observe((await awaitWrite(engine, row, session.config, { waitMs: session.wait.ms(), signal: session.signal })).row));
return settled;
};
- return followRefusals(engine, session, await settle(rows), 'entity-moved-retry', entityMovedRefusal, readmitMovedFacts, settle);
-}
-
-function entityMovedRefusal(row: WriteRequest): boolean {
- return row.error_code === 'revision_conflict' && FACTS_ENTITY_MOVED_MESSAGES.includes(row.error_message ?? '');
+ return followRefusals(engine, session, await settle(rows), 'entity-moved-retry', isEntityMovedRefusal, readmitMovedFacts, settle);
}
function withdrawalRefusal(row: WriteRequest): boolean {
diff --git a/src/core/persistence/verb-errors.ts b/src/core/persistence/verb-errors.ts
index 76817f5a8d..924a9ec6f9 100644
--- a/src/core/persistence/verb-errors.ts
+++ b/src/core/persistence/verb-errors.ts
@@ -7,6 +7,7 @@ import { isFrontmatterHoldMessage } from '../markdown.ts';
import { fenceLocationFromMessage, fenceWhere } from '../fence-repair/refusal.ts';
import { FACT_WITHDRAWN_SUGGESTION } from '../facts/withdrawal.ts';
import { CHECKPOINT_MOVED_MESSAGE } from './sync-slug-mode.ts';
+import { PAGE_CHANGED_AFTER_READ_MESSAGE } from '../page-state/types.ts';
/** The refusal of a fact-batch completion request whose per-entity requests did not all commit (facts-prepare.ts). */
export const FACTS_BATCH_INCOMPLETE_MESSAGE = 'Some extracted facts have not committed.';
@@ -17,9 +18,17 @@ export const FACTS_DEDUP_CHANGED_MESSAGE = 'The fact deduplication state changed
/** The coordinator's refusal when the page moved between preparation and publication (coordinator.ts, group-publish.ts), also
* thrown when a staged large file no longer matches its stage (large-file.ts); fact requests never stage large files. */
export const PAGE_CHANGED_DURING_PREPARATION_MESSAGE = 'The page changed during preparation.';
-/** #7602: entity request refusals a follow-up batch re-admits at the entity's current revision (facts-maintenance.ts). */
+/**
+ * #7602: entity request refusals a follow-up batch re-admits at the entity's current revision (facts-maintenance.ts), and
+ * that `gbrain repair failed-writes` lists. Stored receipts are matched by this exact text, so never reword these messages.
+ * PAGE_CHANGED_AFTER_READ_MESSAGE is how a move during preparation was stored before the fix.
+ */
export const FACTS_ENTITY_MOVED_MESSAGES: readonly string[] = [FACTS_ENTITY_CHANGED_MESSAGE, FACTS_ENTITY_CHANGED_IN_PREPARATION_MESSAGE, FACTS_DEDUP_CHANGED_MESSAGE,
- PAGE_CHANGED_DURING_PREPARATION_MESSAGE];
+ PAGE_CHANGED_DURING_PREPARATION_MESSAGE, PAGE_CHANGED_AFTER_READ_MESSAGE];
+
+export function isEntityMovedRefusal(row: { error_code: string | null; error_message: string | null }): boolean {
+ return row.error_code === 'revision_conflict' && FACTS_ENTITY_MOVED_MESSAGES.includes(row.error_message ?? '');
+}
const FRONTMATTER_SLUG_CONFLICT = /^The frontmatter slug "[^"\n]{1,300}" in [^/"\n][^"\n]{0,1000} conflicts with its path, which expects slug "[^"\n]{1,300}"\. Remove `slug:` or make it match the path\.$/;
diff --git a/src/core/repair/failed-writes.ts b/src/core/repair/failed-writes.ts
index 7ed58b38cf..efc81ac8f9 100644
--- a/src/core/repair/failed-writes.ts
+++ b/src/core/repair/failed-writes.ts
@@ -54,7 +54,7 @@ import { submitRememberMutation } from '../persistence/memory-mutations.ts';
import { getWorktreeBinding } from '../persistence/ownership.ts';
import { prepareFileTarget } from '../persistence/page-prepare.ts';
import { PENDING_IMPORT, SYNC_HELD } from '../persistence/pending-import.ts';
-import { FACTS_ENTITY_MOVED_MESSAGES } from '../persistence/verb-errors.ts';
+import { FACTS_ENTITY_MOVED_MESSAGES, isEntityMovedRefusal } from '../persistence/verb-errors.ts';
import { MAX_FILE_REFUSAL_RETRIES } from '../persistence/facts-maintenance.ts';
import { factsAbsorbWriteKey, factsRetryDeadCommand } from '../facts/dead-absorb-retry.ts';
import { clearApprovedSet, loadApprovedSet, previewChangedError, previewHash, saveApprovedSet } from '../persistence/preview-approval.ts';
@@ -80,7 +80,23 @@ const FACTS_RECOVERED = `EXISTS (SELECT 1 FROM persistence_requests c WHERE r.op
AND COALESCE(c.intent->>'batchKey', c.outcome->>'batch_key') IS DISTINCT FROM r.intent->>'batchKey')`;
/** One managed fact entity request of an extraction (every request sharing its source and input digest). */
-interface ExtractionRequest { slug: string; state: string; error_code: string | null; error_message: string | null; batch_key: string | null }
+interface ExtractionRequest { source_id: string; input_digest: string; slug: string; state: string; error_code: string | null; error_message: string | null; batch_key: string | null }
+
+const extractionKey = (sourceId: string, inputDigest: unknown) => `${sourceId}\n${String(inputDigest)}`;
+
+/** Every entity request of the listed fact rows' extractions, read in one query and grouped by extraction. */
+async function factExtractions(engine: BrainEngine, sourceIds: string[], rows: FailedWrite[]): Promise<Map<string, ExtractionRequest[]>> {
+ const extractions = new Map<string, ExtractionRequest[]>();
+ const digests = [...new Set(rows.filter(row => row.operation === 'extract_facts').map(row => String(row.intent.inputDigest)))];
+ if (!digests.length) return extractions;
+ for (const row of await engine.executeRaw<ExtractionRequest>(`SELECT source_id, intent->>'inputDigest' AS input_digest, slug, state, error_code, error_message,
+ intent->>'batchKey' AS batch_key FROM persistence_requests WHERE operation='extract_facts' AND source_id = ANY($1::text[])
+ AND intent->>'kind'='managed_facts_entity' AND intent->>'inputDigest' = ANY($2::text[])`, [sourceIds, digests])) {
+ const key = extractionKey(row.source_id, row.input_digest);
+ extractions.set(key, [...(extractions.get(key) ?? []), row]);
+ }
+ return extractions;
+}
interface FailedWrite {
id: string; request_id: string; sequence: string; operation: string; source_id: string; slug: string; digest: string; state: string;
@@ -110,13 +126,8 @@ async function factsRecovery(engine: BrainEngine, write: FailedWrite, extraction
const job = typeof write.intent.originalRequestId === 'string' ? write.intent.originalRequestId : null;
const extractAgain = `re-extract: gbrain call extract_facts again on the current text of ${origin ?? 'the source turn'} with a new request_id (one model call)`;
if (!job || write.state !== 'conflict') return extractAgain;
- const extraction = `${write.source_id}\n${String(write.intent.inputDigest)}`;
- if (!extractions.has(extraction)) extractions.set(extraction, await engine.executeRaw<ExtractionRequest>(
- `SELECT slug, state, error_code, error_message, intent->>'batchKey' AS batch_key FROM persistence_requests
- WHERE operation='extract_facts' AND source_id=$1 AND intent->>'inputDigest'=$2 AND intent->>'kind'='managed_facts_entity'`, [write.source_id, write.intent.inputDigest]));
- const open = extractions.get(extraction)!.filter(row => row.state !== 'committed');
- const moved = (row: ExtractionRequest) => row.error_code === 'revision_conflict' && FACTS_ENTITY_MOVED_MESSAGES.includes(row.error_message ?? '');
- if (!open.every(moved) && !open.every(row => row.error_code === 'source_changed')) {
+ const open = (extractions.get(extractionKey(write.source_id, write.intent.inputDigest)) ?? []).filter(row => row.state !== 'committed');
+ if (!open.every(isEntityMovedRefusal) && !open.every(row => row.error_code === 'source_changed')) {
return `${extractAgain}; its extraction also holds refusals one job retry does not re-admit together`;
}
if (write.error_code === 'revision_conflict') {
@@ -362,7 +373,7 @@ export const failedWritesRepair: RepairHandler = {
const command = previewCommand(scope);
if (!opts?.apply) {
const rows = await failedWrites(engine, scope.source_ids, { facts: true });
- const extractions = new Map<string, ExtractionRequest[]>();
+ const extractions = await factExtractions(engine, scope.source_ids, rows);
const residuals: Record<string, number> = {};
const listing: NonNullable<RepairPlan['listing']> = [];
const producers = new Map<string, string>();
diff --git a/test/facts-entity-moved-7602.test.ts b/test/facts-entity-moved-7602.test.ts
index 0c81926ac4..32137f2067 100644
--- a/test/facts-entity-moved-7602.test.ts
+++ b/test/facts-entity-moved-7602.test.ts
@@ -22,6 +22,7 @@ import { join } from 'node:path';
import { configureGateway, resetGateway, __setChatTransportForTests, type ChatResult } from '../src/core/ai/gateway.ts';
import { FENCE_FACTS_INTENT } from '../src/core/cycle/extract-facts.ts';
import type { FactsBackstopCtx } from '../src/core/facts/backstop.ts';
+import { PAGE_CHANGED_AFTER_READ_MESSAGE } from '../src/core/page-state/types.ts';
import { RECOVERY_VERSION } from '../src/core/markdown.ts';
import { FACTS_DEFAULT_VISIBILITY_KEY } from '../src/core/facts/visibility.ts';
import { makeFactsAbsorbHandler } from '../src/core/minions/handlers/facts-absorb.ts';
@@ -195,6 +196,7 @@ for (const backend of testBackends()) {
for (const entry of listing) {
expect(entry.class).toBe('producer_owned');
expect(entry.detail).toStartWith(`re-extract: gbrain call extract_facts again on the current text of ${loser.slug} with a new request_id`);
+ expect(entry.detail).toEndWith('; its follow-up batches were refused as well, so a job retry replays the refusal');
expect(entry.detail).not.toContain('extract --stale');
}
}, { databaseUrl }), 120_000);
@@ -216,6 +218,15 @@ for (const backend of testBackends()) {
expect((await factsListing(engine)).map(entry => entry.detail)).toEqual([`pending: its facts-absorb job (${key}) is waiting; preview again once it ends`]);
expect(await jobStatus('completed')).toHaveLength(1);
expect((await factsListing(engine)).map(entry => entry.detail)).toEqual([`re-extract: gbrain call extract_facts again on the current text of ${loser.slug} with a new request_id (one model call); its facts-absorb job is completed, so gbrain facts retry-dead has no dead job to reset`]);
+ // Value: protects=a cancelled or vanished facts-absorb job is listed for re-extraction, never as pending or with the job reset;
+ // fails_when=the pending check stops excluding cancelled (an endless "preview again") or a missing job gets retry-dead; why_new=the
+ // rows above cover waiting, completed and dead only; seam=none; no_claim=the status and the job's absence are set by SQL
+ expect(await jobStatus('cancelled')).toHaveLength(1);
+ expect((await factsListing(engine)).map(entry => entry.detail)).toEqual([`re-extract: gbrain call extract_facts again on the current text of ${loser.slug} with a new request_id (one model call); its facts-absorb job is cancelled, so gbrain facts retry-dead has no dead job to reset`]);
+ const rekey = (from: string, to: string) => engine.executeRaw("UPDATE minion_jobs SET idempotency_key=$2 WHERE name='facts-absorb' AND idempotency_key=$1 RETURNING id", [from, to]);
+ expect(await rekey(key, `${key}:elsewhere`)).toHaveLength(1);
+ expect((await factsListing(engine)).map(entry => entry.detail)).toEqual([`re-extract: gbrain call extract_facts again on the current text of ${loser.slug} with a new request_id (one model call); its facts-absorb job is gone, so gbrain facts retry-dead has no dead job to reset`]);
+ expect(await rekey(`${key}:elsewhere`, key)).toHaveLength(1);
expect(await jobStatus('dead')).toHaveLength(1);
const [entry, ...others] = await factsListing(engine);
expect(others).toEqual([]);
@@ -245,6 +256,54 @@ for (const backend of testBackends()) {
});
expect((await factsListing(engine)).map(entry => [entry.class, entry.detail])).toEqual([['producer_owned',
`re-extract: gbrain call extract_facts again on the current text of ${loser.slug} with a new request_id (one model call)`]]);
+ // Value: protects=the preview's producer warning for fact requests points at each listed request's own command; fails_when=the
+ // warning names extract --stale again or one request's recover command for every fact request; why_new=no #7602 row reads the
+ // preview warnings; seam=none; no_claim=one fact request only, so the per-request commands are not compared to each other
+ const preview = await (await repairRunner(engine, { apply: false, logger: { info() {}, warn() {}, error() {} } }))
+ .run('failed-writes', await resolveRepairScope(engine, 'default'), { explicit: true, sourceFlag: 'default' });
+ expect((preview.warnings ?? []).filter(warning => warning.includes('extract_facts'))).toEqual([
+ 'extract_facts:managed_facts_entity in default: produced by gbrain itself, not replayed here; each listed request names the command that recovers its facts']);
+ }, { databaseUrl }), 120_000);
+
+ // Value: protects=a moved-entity refusal of an extraction with its own request id (no page write, no facts-absorb job) is listed for
+ // re-extraction with no job advice; fails_when=the listing drops refusals that have no job or offers retry-dead/pending for them;
+ // why_new=every other #7602 listing row is page-triggered; seam=none; no_claim=the move is the wrapped entity read; no MCP caller
+ test(`#7602 [${backend}]: a moved-entity refusal with no page write is listed for re-extraction of the source turn`, () => managedBrain(async brain => {
+ const { engine } = brain;
+ const { move, loser } = await setup(brain, 1);
+ const backstop: FactsBackstopCtx = { ...loser.backstop, persistenceRequestId: undefined, requestId: randomUUID() };
+ const session = Object.assign((await prepareManagedFactsSession(backstop, { turnText: loser.input.turnText }))!, { reportRefusals: true });
+ expect([session.originalRequestId, session.entityMoveRetry]).toEqual([null, undefined]);
+ const race = raceEntityReads(engine, 1, move);
+ const refused = await publishManagedFacts(engine, session, backstop, [fact(CLAIM_B)], 'private').catch(error => error).finally(race.restore);
+ expect(race.moves()).toBe(1);
+ expect([refused.code, refused.retryable]).toEqual(['revision_conflict', false]);
+ expect(await claimB(engine)).toEqual([]);
+ expect((await factsListing(engine)).map(entry => [entry.class, entry.detail])).toEqual([['producer_owned',
+ 're-extract: gbrain call extract_facts again on the current text of the source turn with a new request_id (one model call)']]);
+ }, { databaseUrl }), 120_000);
+
+ // Value: protects=a move during preparation refused before the fix (stored with the page layer's stale-revision message) is
+ // listed with the job reset and re-admitted by it; fails_when=that message is missing from FACTS_ENTITY_MOVED_MESSAGES, so the
+ // receipt is never listed and a reset replays it; why_new=the row above stores the admission-time message; seam=the stored message
+ // is set by SQL to the pre-fix text; no_claim=does not reproduce the pre-fix preparation path itself
+ test(`#7602 [${backend}]: a pre-fix refusal stored with the stale-revision message is listed for the job reset and re-admitted`, () => managedBrain(async brain => {
+ const { engine } = brain;
+ const { move, loser } = await setup(brain, 1);
+ const before = await loser.session();
+ before.entityMoveRetry = false;
+ const race = raceEntityReads(engine, 1, move);
+ expect((await publishManagedFacts(engine, before, loser.backstop, [fact(CLAIM_B)], 'private', loser.slug).catch(error => error).finally(race.restore)).code).toBe('revision_conflict');
+ await engine.transaction(async tx => {
+ await declarePersistenceProtocol(tx);
+ expect(await tx.executeRaw("UPDATE persistence_requests SET error_message=$2 WHERE operation='extract_facts' AND slug=$1 AND state='conflict' RETURNING id", [TARGET, PAGE_CHANGED_AFTER_READ_MESSAGE])).toHaveLength(1);
+ });
+ const key = `facts-absorb:write:${loser.write}`;
+ expect(await engine.executeRaw("UPDATE minion_jobs SET status='dead' WHERE name='facts-absorb' AND idempotency_key=$1 RETURNING id", [key])).toHaveLength(1);
+ expect((await factsListing(engine)).map(entry => entry.detail?.split(' (')[0])).toEqual([`recover: gbrain facts retry-dead --reset '${key}'`]);
+ expect((await resumeManagedFacts(engine, await loser.session()))?.inserted).toBe(1);
+ expect(await claimB(engine)).toEqual([{ fact: CLAIM_B, source_markdown_slug: TARGET }]);
+ expect(await factsListing(engine)).toEqual([]);
}, { databaseUrl }), 120_000);
// Value: protects=a re-admitted fact is never wider than the current default visibility, and keeps world under a world default;