Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
persistence: add_tag / remove_tag defer embedding (#5251)
garrytan4bf8e3b22bb7
diff --git a/docs/architecture/key-files/core-persistence.md b/docs/architecture/key-files/core-persistence.mdindex 4f7b837a32..739e798e91 100644--- a/docs/architecture/key-files/core-persistence.md+++ b/docs/architecture/key-files/core-persistence.md@@ -56,6 +56,6 @@ Connector ingestion, item holds, checkpoint validation and the single-write fast- `src/core/persistence/unbound-source.ts` — #5254 policy for a Postgres page write to a filesystem source (`local_path`, or `sync.repo_path` for `default`) with default write-through and no worktree binding. `submitPageMutation` refuses `owner_unavailable` with `detail: unbound_source`, a `docs` pointer to `write-refusals.md#unbound-sources-on-postgres`, and a hint naming the filled `sources writer status`/`writer claim ... --admin-intent writer_claim --expected-state <admin_state>` bind command (host path only for trusted local callers) and, for `put_page` to a page without `source_path`, `gbrain config set persistence.unbound_write database_only`. That key (`refuse` default, validated by `config set`; any other stored value refuses) lets such a `put_page` admit with `databaseOnlyReason: 'unbound_source'`. Publication rechecks inside its transaction, after `authorizeStoredRequest` share-locks the source row, that the source is still unbound and the page still has no `source_path` (else `owner_unavailable`/`unbound_source`, hint rebuilt from the persisted message by `writeResponse`), then stamps `pages.database_only_reason='unbound_source'` (migration 177). After binding, `prepareFileTarget` keeps a stamped page with no file database-only (receipt `write_through.skipped: unbound_source` with a warning) and managed sync refuses to import a file at its path (`source_changed`, diagnostic reason `unbound_source`). PGLite keeps auto-claiming. Tests: `test/e2e/unbound-source-postgres.test.ts`, `test/persistence-unbound-source.test.ts`. See [persistence stalls](persistence-stalls.md).-- `src/core/persistence/{semantic,semantic-pages}.ts` — revision-bound semantic tag/timeline merges through the journal. Supported merges reprepare against a newer snapshot; exact timeline replay leaves canonical bytes, revision, versions and structured rows unchanged. The public timeline handler resolves durable replay before page-existence preflight, retaining original outcomes after page purge or recreation; fresh writes preserve same-source diagnostics. `page-state/projections.ts` completes sanitized text and page search vectors together, and installs delayed embeddings with UPDATE-only revision/chunk/text/index-context checks. New vectors receive the full provider:model captured before provider execution, and a runtime model switch invalidates the prepared indexing context. Metadata updates preserve the sanitized vector. Tests: `persistence-semantic-pages.test.ts`, `page-projection-concurrency.test.ts`.+- `src/core/persistence/{semantic,semantic-pages}.ts` — revision-bound semantic tag/timeline merges through the journal. Supported merges reprepare against a newer snapshot; exact timeline replay leaves canonical bytes, revision, versions and structured rows unchanged. The public timeline handler resolves durable replay before page-existence preflight, retaining original outcomes after page purge or recreation; fresh writes preserve same-source diagnostics. #5251: `add_tag` / `remove_tag` prepare with `deferEmbedding: true` (a tag change leaves every chunk's text as it was, so no `embedding` effect is queued and `embedding_state` reads `deferred`; the stale sweep still covers any chunk the republication left without a vector); the full page republication they ride stays, so the issue stays open. `page-state/projections.ts` completes sanitized text and page search vectors together, and installs delayed embeddings with UPDATE-only revision/chunk/text/index-context checks. New vectors receive the full provider:model captured before provider execution, and a runtime model switch invalidates the prepared indexing context. Metadata updates preserve the sanitized vector. Tests: `persistence-semantic-pages.test.ts`, `page-projection-concurrency.test.ts`.- `src/core/persistence/{source-lifecycle,managed-sources,topology-schema,topology-locks,topology-receipts,topology-clone,topology-clone-model,topology-filesystem,topology-recovery}.ts` — migration 157 coordinates source add/claim/archive/restore/remove/purge/rebind/reclone. Native root locks precede topology/source/grant/counter/request guards; a shared root advances every membership and permanently conflicts accepted old-topology requests. Withdrawal mirrors and running publication must settle first. Administrative UUIDs share the page journal's principal-scoped conflict domain and permanent capacity counters. Directory replacement reserves the configured recovery budget before provider/staging work, records a verified manifest and physical identity, and recovers only known bytes; clone failure retains the original checkout and a replayable failure receipt. Rebinding accepts a fresh candidate with exactly matching canonical content, including deletions. Stored worktree manifests and clone recovery records keep `digest`, `file_count`, `canonical_stamp` and `self_transfer` but no per-file map (#5790), so their size is independent of the worktree; `compactStoredManifest` drops a legacy map on rewrite, and transfer prepare/accept hash under the native lock before their transaction. Large-repo tests: `test/persistence-large-manifest.test.ts` (12k files, both engines via `test/e2e/persistence-large-manifest-postgres.test.ts`) and `test/e2e/persistence-large-manifest-50k.test.ts`. Old root refusal records remain. Metadata deletion retains local storage. Restart recovery rotates a bounded root scan so one conflict cannot starve other roots. Tests: `test/persistence-source-lifecycle.test.ts` on PGLite and guarded isolated Postgres. #6099: manifests are Git-scoped for Git checkouts; see `worktree-manifest.ts` in [managed sync and persistence stalls](persistence-stalls.md).diff --git a/src/core/persistence/semantic-pages.ts b/src/core/persistence/semantic-pages.tsindex 015742c1a0..708ed62888 100644--- a/src/core/persistence/semantic-pages.ts+++ b/src/core/persistence/semantic-pages.ts@@ -56,7 +56,9 @@ export async function prepareSemanticPageMutation(engine: BrainEngine, row: Writconst prepared = await preparePageMutation(engine, row, config, {expectedRevision: snapshot.revision, tags, content: serializePageToMarkdown(snapshot.page, tags),});- return { ...prepared, apply: async tx => ({ ...await prepared.apply(tx), status: 'ok', tag }) };+ // #5251: a tag change leaves every chunk's text as it was, so the republication queues no+ // embedding effect; the stale sweep still picks up any chunk the publish left without a vector.+ return { ...prepared, deferEmbedding: true, apply: async tx => ({ ...await prepared.apply(tx), status: 'ok', tag }) };}if (row.operation !== 'add_timeline_entry') {throw opError('writer_coordinator_required', 'This semantic writer has no registered preparation handler.',diff --git a/test/persistence-semantic-pages.test.ts b/test/persistence-semantic-pages.test.tsindex 1b763513cd..ac15200707 100644--- a/test/persistence-semantic-pages.test.ts+++ b/test/persistence-semantic-pages.test.ts@@ -37,6 +37,23 @@ test('concurrent semantic tag mutations preserve every accepted tag and coherentexpect(replay.revision).toBe(revision);});+test('#5251 a tag change defers embedding: no embedding effect is queued and chunks keep their text', async () => {+ const before = (await engine.readPageSnapshot('page', { sourceId }))!;+ const chunksBefore = (await engine.getChunks('page', { sourceId })).map(chunk => chunk.chunk_text);+ const result = await submit('add_tag', { slug: 'page', tag: 'deferred-example' }) as { state: string; embedding_state?: string; persistence?: { embedding_state?: string }; id?: string; request_id?: string };+ expect(result.state).toBe('committed');+ expect(result.embedding_state ?? result.persistence?.embedding_state).toBe('deferred');+ const [request] = await engine.executeRaw<{ id: string }>(+ "SELECT id::text AS id FROM persistence_requests WHERE source_id=$1 AND slug='page' AND operation='add_tag' ORDER BY sequence DESC LIMIT 1", [sourceId]);+ expect(await engine.executeRaw("SELECT kind FROM persistence_effects WHERE request_id=$1::uuid AND kind='embedding'", [request.id])).toEqual([]);+ const after = (await engine.readPageSnapshot('page', { sourceId }))!;+ expect(after.revision).not.toBe(before.revision);+ expect(after.tags).toContain('deferred-example');+ expect((await engine.getChunks('page', { sourceId })).map(chunk => chunk.chunk_text)).toEqual(chunksBefore);+ const removed = await submit('remove_tag', { slug: 'page', tag: 'deferred-example' }) as { embedding_state?: string; persistence?: { embedding_state?: string } };+ expect(removed.embedding_state ?? removed.persistence?.embedding_state).toBe('deferred');+});+test('timeline replay is an exact no-op in Markdown, revision, versions and structured rows', async () => {const params = { slug: 'page', date: '2026-09-15', summary: 'Example milestone', detail: 'Example detail', source: 'example-source' };await submit('add_timeline_entry', params);