Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(facts): a facts batch refused for embedding config lands once the owner restarts repaired
garrytan90daf1ecea41
diff --git a/docs/architecture/key-files/core-persistence-facts.md b/docs/architecture/key-files/core-persistence-facts.mdindex 28f34718f7..fe5de9067c 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 CHANGELManaged 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, 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 follow-up's source-page check in `facts-prepare.ts` accepts a source page moved only by a committed request of the same extraction (same principal and input digest, at the page's current revision), such as a fact the batch wrote onto its own source page; any other change still refuses `The source page changed during fact extraction.` A refusal `collectManagedFacts` still throws after the walks is marked `retryable: false`, so the worker ends the facts-absorb job dead on its first attempt (`jobRefusal`; `revision_conflict` is not in `DETERMINISTIC_WRITE_REFUSALS`, so this is not the `facts_absorb_write_refused` path). Test: `test/facts-entity-moved-7602.test.ts` (PGLite and Postgres). `gbrain repair failed-writes` (`failed-writes.ts`, [Core Utilities (2/2)](core-utilities-2.md)) lists refused fact extractions. #7602: fact rows (moved-entity refusals included) use `factsRecovery`: `recover:` (retry-dead reset) only while the page write's facts-absorb job is dead and the entity page is the one admitted, `pending:` while that job runs, `not recoverable:` for a deleted page, and `re-extract:` otherwise (replaced page, completed or missing job, exhausted follow-ups, a page write the job's own `readFactsBackstopJobPage` check now skips (written again since, or a narrowed writer grant), an extraction whose open entity requests are not all moved-entity or all held-file refusals, a guard refusal). Only the preview reads fact rows (`failedWrites(..., { facts: true })`); the sync-time remember replay and apply-by-id keep the guard/drift query.+- `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 follow-up's source-page check in `facts-prepare.ts` accepts a source page moved only by a committed request of the same extraction (same principal and input digest, at the page's current revision), such as a fact the batch wrote onto its own source page; any other change still refuses `The source page changed during fact extraction.` A refusal `collectManagedFacts` still throws after the walks is marked `retryable: false`, so the worker ends the facts-absorb job dead on its first attempt (`jobRefusal`; `revision_conflict` is not in `DETERMINISTIC_WRITE_REFUSALS`, so this is not the `facts_absorb_write_refused` path). Test: `test/facts-entity-moved-7602.test.ts` (PGLite and Postgres). #8526: a page-triggered or input-keyed session also runs `followEmbeddingRefusals`, re-admitting facts the owner's consumer refused `embedding_configuration` (through `readmitMovedFacts`: retained visibility, unattributed groups kept) under `digest([batchKey, 'embedding-retry', n])` only once no live full or promoted consumer that started before the refusal remains (`ownerRestartedSince`: the worktree owner host's consumers, every consumer for a batch with no worktree; with no live consumer row, as on PGLite, admission's check of the retained signature against the brain's current config is the gate), bounded by `MAX_FILE_REFUSAL_RETRIES`. Test: `test/facts-embedding-refusal-heal-8526.test.ts` (PGLite and Postgres). `gbrain repair failed-writes` (`failed-writes.ts`, [Core Utilities (2/2)](core-utilities-2.md)) lists refused fact extractions. #7602: fact rows (moved-entity refusals included) use `factsRecovery`: `recover:` (retry-dead reset) only while the page write's facts-absorb job is dead and the entity page is the one admitted, `pending:` while that job runs, `not recoverable:` for a deleted page, and `re-extract:` otherwise (replaced page, completed or missing job, exhausted follow-ups, a page write the job's own `readFactsBackstopJobPage` check now skips (written again since, or a narrowed writer grant), an extraction whose open entity requests are not all moved-entity or all held-file refusals, a guard refusal). Only the preview reads fact rows (`failedWrites(..., { facts: true })`); the sync-time remember replay and apply-by-id keep the guard/drift query.- `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/persistence/facts-maintenance.ts b/src/core/persistence/facts-maintenance.tsindex ad7bb918eb..fcd592feb0 100644--- a/src/core/persistence/facts-maintenance.ts+++ b/src/core/persistence/facts-maintenance.ts@@ -27,6 +27,7 @@ import { authorizeFactsBackstop } from './effect-facts.ts';import { admitWriteInTransaction, getWriteRequest, getWriteRequestById, receiptFor } from './journal.ts';import { assertPersistenceAccepting, awaitWrite, persistenceConsumerConfig, writeResponse } from './service.ts';import { MaintenanceWriteWait } from './maintenance-wait.ts';+import { CONSUMER_LIVE_MS } from './consumer-heartbeat.ts';import { digest, requireUuid, sha256 } from './digest.ts';import { isTerminal, type WriteAuthority, type WriteRequest } from './model.ts';import type { WriteReceipt } from './types.ts';@@ -389,7 +390,8 @@ export async function resumeManagedFacts(engine: BrainEngine, session: ManagedFaconst stored = await storedBatch(engine, session);if (!stored) return null;const files = session.fileRefusalRetry ? await followFileRefusals(engine, session, stored) : stored;- const rows = session.inputKeyed ? await followWithdrawalRefusals(engine, session, files) : files;+ const embedded = session.fileRefusalRetry || session.inputKeyed ? await followEmbeddingRefusals(engine, session, files) : files;+ const rows = session.inputKeyed ? await followWithdrawalRefusals(engine, session, embedded) : embedded;return collectManagedFacts(engine, session, session.entityMoveRetry ? await followEntityMoves(engine, session, rows) : rows);}@@ -424,6 +426,30 @@ async function followFileRefusals(engine: BrainEngine, session: ManagedFactsSessreturn followRefusals(engine, session, stored, 'file-refusal-retry', row => row.error_code === 'source_changed', readmitRefusedFacts);}+/**+ * #8526: the same walk for a batch the write owner's consumer refused `embedding_configuration`, typically because it ran+ * with a stale embedding config. The worker cannot see that consumer's config, so a follow-up is admitted only once no+ * full consumer that was already running at the refusal is still live (the owner host's, or any when the rows have no+ * worktree); until then the stored refusal replays and no follow-up is spent. The retained facts re-enter as #7602's do (readmitMovedFacts: current revision,+ * retained visibility, an unattributed group kept, a deleted or replaced entity left refused), and admission rechecks the+ * retained signature against the brain's current config, so after a real model change nothing is admitted. No+ * extraction runs again.+ */+async function followEmbeddingRefusals(engine: BrainEngine, session: ManagedFactsSession, stored: WriteRequest[]): Promise<WriteRequest[]> {+ return followRefusals(engine, session, stored, 'embedding-retry', row => row.error_code === 'embedding_configuration',+ async (tx, followUp, refused) => await ownerRestartedSince(tx, refused) ? readmitMovedFacts(tx, followUp, refused) : null);+}++async function ownerRestartedSince(engine: BrainEngine, refused: WriteRequest[]): Promise<boolean> {+ const worktrees = [...new Set(refused.map(row => row.worktree_id).filter((id): id is string => !!id))];+ const since = refused.map(row => new Date(row.completed_at ?? row.updated_at ?? 0).getTime()).reduce((a, b) => Math.max(a, b), 0);+ const [row] = await engine.executeRaw<{ stale: boolean }>(`SELECT EXISTS (SELECT 1 FROM persistence_consumers c+ WHERE c.mode IN ('full','promoted') AND c.renewed_at>now()-($2::integer*interval '1 millisecond') AND c.started_at<=$3::timestamptz+ AND (cardinality($1::uuid[])=0 OR c.host_id IN (SELECT owner_host_id FROM persistence_worktrees WHERE id=ANY($1::uuid[])))) AS stale`,+ [worktrees, CONSUMER_LIVE_MS, new Date(since).toISOString()]);+ return !row?.stale;+}+/*** #6506 (UC1): the same walk for an input-keyed batch whose entity request was* refused because a candidate's exact claim had been forgotten. Older batchesdiff --git a/test/facts-embedding-refusal-heal-8526.test.ts b/test/facts-embedding-refusal-heal-8526.test.tsnew file mode 100644index 0000000000..3f765fe23a--- /dev/null+++ b/test/facts-embedding-refusal-heal-8526.test.ts@@ -0,0 +1,140 @@+/**+ * #8526: a page-triggered facts batch that the write owner's consumer refused `embedding_configuration` (it ran with a+ * stale embedding config) replayed that stored refusal on every retry, even after the owner restarted with the repaired+ * config, because the batch is keyed by the page write. It now walks follow-up batches once an owner consumer has started+ * since the refusal; while the refusing consumer is still the one running, a retry spends no follow-up.+ */+import { expect, test } from 'bun:test';+import { randomUUID } from 'node:crypto';+import type { GBrainConfig } from '../src/core/config.ts';+import type { FactsBackstopCtx } from '../src/core/facts/backstop.ts';+import type { ExtractedFact } from '../src/core/facts/extract.ts';+import { recordFactWithdrawal } from '../src/core/facts/withdrawal.ts';+import { prepareManagedFactsSession, publishManagedFacts, resumeManagedFacts } from '../src/core/persistence/facts-maintenance.ts';+import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { managedBrain, type ManagedBrain } from './helpers/managed-brain.ts';+import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';++const ORIGIN = 'notes/input', TARGET = 'companies/acme-example', CLAIM = 'Acme Example ships its roadmap weekly.';+const MODEL = 'openai:text-embedding-3-small', DIMENSIONS = 1536;+const UNATTRIBUTED = 'The quarterly review moved to Thursdays.';+const vector = () => new Float32Array([1, ...Array(DIMENSIONS - 1).fill(0)]);+const fact = (claim: string, entity: string | null = null): ExtractedFact => ({ fact: claim, kind: 'fact', source: 'mcp:put_page', entity_slug: entity,+ confidence: 0.9, notability: 'high', embedding: vector() });++/**+ * The owner's consumer runs with ctx.config (embedding_disabled: true); the worker embeds under the brain's model and+ * publishes with `withdrawn: 'skip'`, as the backstop does.+ */+async function refusedBatch({ engine, ctx }: ManagedBrain, facts: ExtractedFact[] = [fact(CLAIM, TARGET)], visibility: 'private' | 'world' = 'private') {+ await engine.setConfig('embedding_model', MODEL);+ await engine.setConfig('embedding_dimensions', String(DIMENSIONS));+ await engine.setConfig('embedding_disabled', 'false');+ const requestId = randomUUID();+ for (const slug of [TARGET, ORIGIN]) {+ await submitPageMutation(ctx, { operation: 'put_page', params: {+ slug, content: `---\ntype: ${slug === TARGET ? 'company' : 'concept'}\ntitle: Example\n---\nAn example note for ${slug}.\n`,+ request_id: slug === ORIGIN ? requestId : randomUUID() } });+ }+ const snapshot = (await engine.readPageSnapshot(ORIGIN, { sourceId: 'default' }))!;+ const [write] = await engine.executeRaw<{ id: string }>('SELECT id FROM persistence_requests WHERE request_id=$1::uuid', [requestId]);+ const worker = { engine: engine.kind, embedding_disabled: false } as GBrainConfig;+ const backstop: FactsBackstopCtx = { engine, sourceId: 'default', source: 'mcp:put_page', sessionId: null, persistenceRequestId: write!.id, config: worker };+ const input = { turnText: snapshot.page.compiled_truth, pageSlug: ORIGIN };+ const session = (await prepareManagedFactsSession(backstop, input))!;+ session.embedding = { model: MODEL, dimensions: DIMENSIONS };+ const refused = await publishManagedFacts(engine, session, backstop, facts, visibility, ORIGIN, { withdrawn: 'skip' }).catch(error => error);+ expect(refused.code).toBe('embedding_configuration');+ return async () => resumeManagedFacts(engine, (await prepareManagedFactsSession(backstop, input))!);+}++/** A live heartbeat row for a consumer of the owner's host (or of `hostId`), started `startedAgoMs` ago. */+async function ownerConsumer(engine: ManagedBrain['engine'], startedAgoMs: number, opts: { mode?: string; kind?: string; hostId?: string } = {}) {+ await engine.executeRaw(`INSERT INTO persistence_consumers (host_id,pid,nonce,kind,mode,started_at,renewed_at,host_json_path,persistence_home,version)+ SELECT COALESCE($5::uuid, owner_host_id),4242,$1,$3,$4,now()-($2::integer*interval '1 millisecond'),now(),'/tmp/host.json','/tmp/home','test'+ FROM persistence_worktrees WHERE owner_host_id IS NOT NULL LIMIT 1`, [randomUUID(), startedAgoMs, opts.kind ?? 'serve', opts.mode ?? 'full', opts.hostId ?? null]);+}++/** The refusing consumer stops (its row lapses) and a repaired one starts. */+async function restartOwner({ engine, ctx }: ManagedBrain) {+ await disposePersistenceConsumer(engine);+ await engine.executeRaw("UPDATE persistence_consumers SET renewed_at=now()-interval '1 hour' WHERE started_at<now()-interval '1 second'");+ ctx.config.embedding_disabled = false;+ await ownerConsumer(engine, 0);+}++const code = (retry: () => Promise<unknown>) => retry().then(() => null, error => error.code);+const facts = (engine: ManagedBrain['engine'], claim = CLAIM) => engine.executeRaw<{ fact: string; embedded: boolean }>(+ 'SELECT fact, embedding IS NOT NULL AS embedded FROM facts WHERE fact=$1', [claim]);+const receipts = (engine: ManagedBrain['engine']) => engine.executeRaw<{ request_id: string; state: string; error_code: string | null }>(+ "SELECT request_id, state, error_code FROM persistence_requests WHERE operation='extract_facts' AND slug=$1 ORDER BY sequence", [TARGET]);++for (const backend of testBackends()) {+ const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;++ test(`#8526 [${backend}]: a refused batch spends nothing while the refusing owner runs, then publishes once the owner restarts repaired`, () => managedBrain(async brain => {+ const { engine } = brain;+ const retry = await refusedBatch(brain);+ await ownerConsumer(engine, 3_600_000);+ const original = await receipts(engine);+ expect(original).toMatchObject([{ state: 'failed', error_code: 'embedding_configuration' }]);+ for (let attempt = 0; attempt < 5; attempt++) expect(await code(retry)).toBe('embedding_configuration');+ await ownerConsumer(engine, 0, { kind: 'sync', mode: 'waiter_only' });+ expect(await code(retry)).toBe('embedding_configuration');+ expect(await receipts(engine)).toEqual(original);+ await restartOwner(brain);+ expect(await retry()).toMatchObject({ inserted: 1 });+ expect(await facts(engine)).toEqual([{ fact: CLAIM, embedded: true }]);+ expect((await receipts(engine))[0]).toEqual(original[0]!);+ expect(await retry()).toMatchObject({ inserted: 1 });+ expect(await facts(engine)).toHaveLength(1);+ }, { databaseUrl }), 120_000);++ test(`#8526 [${backend}]: after a real model change the retry keeps the refusal and admits nothing`, () => managedBrain(async brain => {+ const { engine } = brain;+ const retry = await refusedBatch(brain);+ await restartOwner(brain);+ await engine.setConfig('embedding_model', 'openai:text-embedding-3-large');+ const before = await receipts(engine);+ expect(await code(retry)).toBe('embedding_configuration');+ expect(await receipts(engine)).toEqual(before);+ expect(await facts(engine)).toEqual([]);+ }, { databaseUrl }), 120_000);++ test(`#8526 [${backend}]: a database-only batch (no worktree) waits on every live consumer of the brain`, () => managedBrain(async brain => {+ const { engine } = brain;+ await engine.setConfig('sync.write_through', 'false');+ const retry = await refusedBatch(brain);+ expect(await engine.executeRaw("SELECT 1 FROM persistence_requests WHERE operation='extract_facts' AND worktree_id IS NOT NULL")).toEqual([]);+ await ownerConsumer(engine, 3_600_000, { hostId: randomUUID() });+ const original = await receipts(engine);+ for (let attempt = 0; attempt < 4; attempt++) expect(await code(retry)).toBe('embedding_configuration');+ expect(await receipts(engine)).toEqual(original);+ await restartOwner(brain);+ expect(await retry()).toMatchObject({ inserted: 1 });+ }, { databaseUrl }), 120_000);++ test(`#8526 [${backend}]: a batch with an unattributed fact recovers too`, () => managedBrain(async brain => {+ const { engine } = brain;+ const retry = await refusedBatch(brain, [fact(CLAIM, TARGET), fact(UNATTRIBUTED)]);+ await restartOwner(brain);+ expect(await retry()).toMatchObject({ inserted: 2 });+ expect(await facts(engine)).toEqual([{ fact: CLAIM, embedded: true }]);+ expect(await facts(engine, UNATTRIBUTED)).toEqual([{ fact: UNATTRIBUTED, embedded: true }]);+ }, { databaseUrl }), 120_000);++ test(`#8526 [${backend}]: a claim forgotten while the batch waited is not saved again`, () => managedBrain(async brain => {+ const { engine } = brain;+ await engine.setConfig('facts.default_visibility', 'world');+ const retry = await refusedBatch(brain, [fact(CLAIM, TARGET)], 'world');+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ const saved = await engine.insertFact({ fact: CLAIM, entity_slug: TARGET, visibility: 'world', kind: 'fact', source: 'fixture' }, { source_id: 'default' });+ expect((await recordFactWithdrawal(engine, saved.id, 'default', false, { semanticReview: false })).withdrawn).toBe(true);+ await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');+ await engine.setConfig('facts.default_visibility', 'private');+ await restartOwner(brain);+ expect(await retry()).toMatchObject({ inserted: 0, withdrawn_skipped: 1 });+ expect(await engine.executeRaw("SELECT id FROM facts WHERE fact=$1 AND visibility='private'", [CLAIM])).toEqual([]);+ }, { databaseUrl }), 120_000);+}diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex 72ad5657dd..68baa822c4 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -90,6 +90,7 @@ test/fact-embedding-oversized-7211.test.tstest/fact-fence-adoption-exits.test.tstest/fact-purge-lookup-timeout-6511.test.tstest/facts-absorb-stalled-receipt-7439.test.ts+test/facts-embedding-refusal-heal-8526.test.tstest/facts-entity-moved-7602.test.tstest/facts-insert-guards-7105.test.tstest/facts-publication-large-fence-6898.test.ts