Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

perf(conversation-facts): a held page keeps its hold at an unchanged version without reading it

garrytan5d9f4ee32c3c
diff --git a/src/core/extraction-watermark.ts b/src/core/extraction-watermark.ts
index b7f877596f..6f04c32a9e 100644
--- a/src/core/extraction-watermark.ts
+++ b/src/core/extraction-watermark.ts
@@ -63,8 +63,9 @@ export interface InputTruncated {
prior_facts?: number;
/** PR 4 (Wave B incremental derivation, transcripts): `detection_ambiguous`: the messages each format that parses any parses over the page. */
grammars?: Record<string, number>;
- /** PR 4 (Wave B incremental derivation, transcripts): `grammar_chosen`: the chosen format (null: none, the page is not a transcript) and the page version it holds for. */
+ /** PR 4 (Wave B incremental derivation, transcripts): `grammar_chosen`: the chosen format (null: none, the page is not a transcript). */
grammar?: string | null;
+ /** PR 4 (Wave B incremental derivation, transcripts): `grammar_chosen`, `detection_ambiguous`: the page version the choice or the hold is for. */
version_token?: string;
}
diff --git a/src/core/facts/conversation-incremental.ts b/src/core/facts/conversation-incremental.ts
index 66e4089834..b0ea22a2af 100644
--- a/src/core/facts/conversation-incremental.ts
+++ b/src/core/facts/conversation-incremental.ts
@@ -29,8 +29,9 @@
* choice; one unpaid read, counted in conversationDetectionWork), pinned on the rows while the occurrences that
* recorded it under the same context are mostly unchanged by bytes, or the one the operator chose for this page
* version (--grammar; none settles the page as non-extractable); with none detected while some built-in format still
- * parses a message, the page is held (a detection_ambiguous receipt with each format's count; no call, nothing
- * published or collected) until it changes or the operator chooses; a page no format parses a message in is not a
+ * parses a message, the page is held (a detection_ambiguous receipt with each format's count for the page version;
+ * no call, nothing published or collected; a plain run on the same version reuses it without reading the page)
+ * until it changes or the operator chooses; a page no format parses a message in is not a
* conversation: it settles as non-extractable and its conversation facts are collected; a pinned grammar under which nothing parses is detected
* again; a detected grammar under which nothing parses leaves a no_messages receipt and changes nothing;
* - reuses every window an earlier run extracted with the same rendered text under the same context (re-anchored
@@ -209,22 +210,25 @@ export async function processSegmentedConversationPage(run: SegmentedConversatio
: !opts.redetect && kept.length && 2 * bytesOf(kept) > bytesOf(recorded) ? rowOutput(kept[0]).pattern ?? null : null;
const pinned = choice === undefined && pattern !== null;
const detectedBytes = conversationDetectionWork.bytes;
+ const hold = async (grammars: Record<string, number>, checked: boolean) => {
+ if (checked && !run.dryRun) await setExtractionReceipt(engine, page.id, KIND, { kind: 'detection_ambiguous', pipeline: KIND, processed_chars: 0, grammars, version_token: versionToken });
+ result.pages_detection_ambiguous = (result.pages_detection_ambiguous ?? 0) + 1;
+ process.stderr.write(`[extract-conversation-facts] ${page.slug}: held${checked ? '' : ' (unchanged since it was held, nothing read)'}: no format is detected over the page, yet ${Object.entries(grammars).map(([id, n]) => `${id} parses ${n}`).join(', ')} message(s); its facts were kept and nothing was extracted; gbrain extract-conversation-facts --slug ${page.slug} --grammar <format|none> --dry-run previews a choice\n`);
+ return { newestEnd: null };
+ };
+ // A page held at this version stays held without reading it (a --force run checks it again).
+ const heldAt = choice === undefined && !opts.retryFailed && !opts.sinceIso && stored?.kind === 'detection_ambiguous' && stored.version_token === versionToken ? stored.grammars : undefined;
+ if (heldAt && !pattern && from < liveIds.length) return hold(heldAt, false);
if (choice === undefined && !pattern && from < liveIds.length) pattern = await detectPagePattern(occurrences, i => readOccurrenceText(engine, body, occurrences[i]!.row, { detection: true }));
if (choice === undefined && !pattern && from < liveIds.length) {
if (opts.sinceIso) { result.pages_skipped++; return { newestEnd: null }; }
// No format is detected over the whole page. One that still parses a message here may be the page's (text around a
// transcript can tip the density test) or a quoted part of it, and guessing has deleted current facts: the page is
- // held, with no call, nothing published or collected and a detection_ambiguous receipt naming each format's count,
- // until it changes or the operator chooses its format. A page no format parses a message in is not a transcript: it
+ // held, with no call, nothing published or collected and a detection_ambiguous receipt naming each format's count
+ // for this page version, until it changes or the operator chooses its format. A page no format parses a message in is not a transcript: it
// settles as non-extractable below and its conversation facts are collected.
const grammars = await messagesByGrammar(occurrences, { page, gapMinutes: opts.gapMinutes }, i => readOccurrenceText(engine, body, occurrences[i]!.row, { detection: true }));
- if (Object.keys(grammars).length) {
- if (!run.dryRun) await setExtractionReceipt(engine, page.id, KIND, { kind: 'detection_ambiguous', pipeline: KIND, processed_chars: 0, grammars });
- result.pages_detection_ambiguous = (result.pages_detection_ambiguous ?? 0) + 1;
- process.stderr.write(`[extract-conversation-facts] ${page.slug}: held: no format is detected over the page, yet ${Object.entries(grammars).map(([id, n]) => `${id} parses ${n}`).join(', ')} message(s);`
- + ` its facts were kept and nothing was extracted; gbrain extract-conversation-facts --slug ${page.slug} --grammar <format|none> --dry-run previews a choice\n`);
- return { newestEnd: null };
- }
+ if (Object.keys(grammars).length) return hold(grammars, true);
from = 0;
}
// Earlier windows of the occurrences the stream covers (and of retired ones), reusable by rendered text under the same context.
diff --git a/test/helpers/conversation-facts-invariant-cases.ts b/test/helpers/conversation-facts-invariant-cases.ts
index 8ccdf6fd78..c466b02107 100644
--- a/test/helpers/conversation-facts-invariant-cases.ts
+++ b/test/helpers/conversation-facts-invariant-cases.ts
@@ -254,6 +254,30 @@ export function defineConversationFactsInvariantCases(opts: { getBrain: () => Ap
expect(out).toEqual(expected);
}, 600_000);
+ test('a held page keeps its hold while its version is unchanged: the next plain run reads no detection byte; a --force run and a changed page check it again', async () => {
+ const brain = opts.getBrain();
+ const slug = `notes/held-reuse-${randomUUID().slice(0, 8)}`;
+ await caps(brain.engine, 'off');
+ await brain.seed(slug, chatPage('Was a chat', chatDays(4800, 100, 41)));
+ await extract(brain.engine, slug);
+ const quoted = '**Alice Example** (1:05 PM): the orchard review moved.';
+ await brain.seed(slug, chatPage('Was a chat', `${references(9000)}\n\nA line we quoted:\n\n${quoted}`));
+ expect(await undetected(brain, slug)).toBeNull();
+ const facts = (await factsOf(brain.engine, slug)).map(f => f.id);
+ const run = async (force = false) => {
+ const before = conversationDetectionWork.bytes;
+ const { calls, result } = await extract(brain.engine, slug, { force });
+ return { read: conversationDetectionWork.bytes === before ? 'nothing' : 'the page', calls: calls.length, held: result.pages_detection_ambiguous ?? 0, failed: result.pages_failed,
+ facts: (await factsOf(brain.engine, slug)).map(f => f.id) };
+ };
+ const runs: Record<string, unknown> = { first: await run(), again: await run(), forced: await run(true), afterForce: await run() };
+ await brain.seed(slug, chatPage('Was a chat', `${references(9001)}\n\nA line we quoted:\n\n${quoted}`));
+ runs.changed = await run();
+ runs.afterChange = await run();
+ const held = (read: string) => ({ read, calls: 0, held: 1, failed: 0, facts });
+ expect(runs).toEqual({ first: held('the page'), again: held('nothing'), forced: held('the page'), afterForce: held('nothing'), changed: held('the page'), afterChange: held('nothing') });
+ }, 600_000);
+
test('R4 (cases B, B-heading): a transcript with reference lines added, before and after they move below a heading, is held every run with its facts kept', async () => {
const brain = opts.getBrain();
const slug = `notes/references-${randomUUID().slice(0, 8)}`;