Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(segmented synopsis): the derivation context hashes the chunker version the page's rows were cut at, so a re-chunk unsettles its segments

garrytand671b60a2cbe
diff --git a/src/core/contextual-retrieval-service.ts b/src/core/contextual-retrieval-service.ts
index 7364a8df7f..ac382eec7c 100644
--- a/src/core/contextual-retrieval-service.ts
+++ b/src/core/contextual-retrieval-service.ts
@@ -72,7 +72,6 @@ import { isSegmentedPage, pageBodyBytes } from './page-body/page-shape.ts';
import { installSegmentedVectors, listLiveEmbedSegments, listSegmentChunkRows, readSegmentedEmbedPage } from './page-body/segmented-embedding.ts';
import { acquirePageEmbeddingLease } from './page-embedding-lease.ts';
import { planSegmentDerivations, segmentDerivationContextHash } from './page-body/derivations.ts';
-import { SEGMENTED_CHUNKER_VERSION } from './page-body/segment-chunks.ts';
import { embeddingInputHash, synopsisDocumentHash, type EmbeddingTier } from './embedding-input-hash.ts';
import { currentEmbeddingSignature } from './embedding.ts';
import { estimatePageEmbedUsd, reservePageSpend, spendGrant } from './page-embedding-budget.ts';
@@ -497,7 +496,7 @@ export const SYNOPSIS_DERIVATION_VERSION = 1;
* (synopsisDocumentHash), so a chunk whose recorded input already matches keeps its vector. The pipeline's
* page_segment_derivations rows (kind synopsis, one per occurrence, output = the window anchors) record each segment
* done under a context hash over its entry state, the title, the lead's hash, the synopsis model, its output cap, the
- * segmented chunker version, the corpus generation and the write target: a rerun reads no chunk row of a settled
+ * chunker version the page's rows were cut at (so a re-chunk unsettles its segments), the corpus generation and the write target: a rerun reads no chunk row of a settled
* segment, and an append pays only for the windows of its new segments. Segments run newest first, so a per-page cap
* spends on the newest text, as the embedding path's grants do.
*
@@ -537,8 +536,9 @@ async function reembedSegmentedPage(args: ReembedPageArgs, initial: Page): Promi
const titlePrefix = buildContextualPrefix(page.title, null);
const embedSignature = currentEmbeddingSignature();
const lead = await segmentSynopsisLead(engine, page);
+ const [chunked] = await engine.executeRaw<{ chunker_version: number | string | null }>('SELECT chunker_version FROM pages WHERE id = $1', [page.id]);
const inputs = { title: page.title, lead_sha256: sha256(lead), synopsis_model: synopsisModel, synopsis_max_tokens: synopsisMaxTokens ?? SYNOPSIS_MAX_TOKENS,
- chunker_version: SEGMENTED_CHUNKER_VERSION, corpus_generation: corpusGeneration,
+ chunker_version: chunked?.chunker_version == null ? null : Number(chunked.chunker_version), corpus_generation: corpusGeneration,
embedding: { column: target.column.name, model: target.provenanceModel, dimensions: target.column.dimensions } };
const segments = await listLiveEmbedSegments(engine, page.id);
const live = segments.map(s => ({ occurrenceId: s.occurrenceId, sha256: s.sha256,
diff --git a/test/helpers/synopsis-segment-hash-cases.ts b/test/helpers/synopsis-segment-hash-cases.ts
index f50da7e5af..be029de4a3 100644
--- a/test/helpers/synopsis-segment-hash-cases.ts
+++ b/test/helpers/synopsis-segment-hash-cases.ts
@@ -31,6 +31,8 @@ import { embedStalePages } from '../../src/core/embed-stale.ts';
import { pageEmbeddingLeaseProbe } from '../../src/core/page-embedding-lease.ts';
import { installSegmentedVectors, readSegmentedEmbedPage } from '../../src/core/page-body/segmented-embedding.ts';
import type { AppendBrain } from './append-brain.ts';
+import { needlePage, needlePageBody, sealAtOldChunker } from './rechunk-segmented-cases.ts';
+import { runSegmentedRechunk } from '../../src/core/page-body/rechunk.ts';
const WORDS = ['launch', 'pricing', 'roadmap', 'hiring', 'metrics', 'retention', 'design', 'latency', 'budget', 'customer'];
const paragraph = (d: number, k: number) =>
@@ -322,6 +324,33 @@ export function defineSynopsisSegmentHashCases(opts: { getBrain: () => AppendBra
expect(calls.every(call => call.documentText.startsWith(editedLead))).toBe(true);
}, 300_000);
+ test('re-chunk: segments settled at the old chunker go unsettled after reindex --segmented --apply; the pass synopsizes every re-chunked row once', async () => {
+ const brain = opts.getBrain();
+ await roomyCap();
+ const slug = 'notes/synopsis-rechunk';
+ await brain.seed(slug, needlePage(needlePageBody()));
+ const id = await sealAtOldChunker(brain.engine, slug);
+ reset();
+ expect(await run(slug)).toMatchObject({ kind: 'success', mode_applied: 'per_chunk_synopsis' });
+ expect((await chunks(slug)).every(c => c.vector !== null)).toBe(true);
+ const receipt = await runSegmentedRechunk(brain.engine, { apply: true, slug });
+ expect(receipt.pages).toMatchObject([{ slug, state: 'rechunked' }]);
+ const rechunked = await chunks(slug);
+ const pending = rechunked.filter(c => c.vector === null);
+ expect(pending.length).toBeGreaterThan(0);
+ const segments = new Set(rechunked.map(c => c.occurrence_id)).size;
+ reset();
+ expect(await run(slug)).toMatchObject({ kind: 'success', mode_applied: 'per_chunk_synopsis', chunks_embedded: pending.length });
+ expect({ calls: calls.length, embedded: brain.embedded.length, segmentReads }).toEqual({ calls: nonCode(pending).length, embedded: pending.length, segmentReads: segments });
+ const done = await chunks(slug);
+ expect(done.every(c => c.vector !== null)).toBe(true);
+ for (const c of done) expect(c.embedding_input_hash).not.toBe(await titleHash(c));
+ reset();
+ expect(await run(slug)).toMatchObject({ kind: 'success', chunks_embedded: 0 });
+ expect({ calls: calls.length, segmentReads }).toEqual({ calls: 0, segmentReads: 0 });
+ expect((await synopsisRows(id)).every(r => r.state === 'done')).toBe(true);
+ }, 300_000);
+
test('an inline page keeps the synopsisBodyHash input', async () => {
const brain = opts.getBrain();
const slug = 'notes/synopsis-inline';