Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(extract-conversation-facts): record completion at exact segment limit (#5464)

time-attack3aa68a3f71e0
diff --git a/src/commands/extract-conversation-facts.ts b/src/commands/extract-conversation-facts.ts
index 77d8c06e04..322a5e959a 100644
--- a/src/commands/extract-conversation-facts.ts
+++ b/src/commands/extract-conversation-facts.ts
@@ -1209,10 +1209,10 @@ async function processPage(
}
// Eng-v2 C7 / E16: write terminal audit row after all segments commit
- // successfully. Only run when we got through every
- // segment (no break on segmentLimit; that's an explicit partial run).
- const fullyProcessed =
- state.segmentLimit === 0 || segmentsThisPage < state.segmentLimit;
+ // successfully. Only run when every segment was processed — including a
+ // page whose segment count equals --segment-limit exactly, which
+ // previously missed the terminal row and re-extracted on every run.
+ const fullyProcessed = segmentsThisPage === segments.length;
if (
fullyProcessed &&
newestEnd !== null &&
diff --git a/test/extract-conversation-facts.test.ts b/test/extract-conversation-facts.test.ts
index d7b82f835e..3d2ab036bd 100644
--- a/test/extract-conversation-facts.test.ts
+++ b/test/extract-conversation-facts.test.ts
@@ -1161,6 +1161,46 @@ describe('runExtractConversationFactsCore', () => {
expect(Number(terminalRows[0]?.count ?? 0)).toBe(1);
});
+ // #5430 item E.3: completion means every in-scope segment was processed.
+ // A page with exactly --segment-limit segments is complete; a page cut
+ // short by the limit is a partial run that advances its checkpoint but
+ // never writes the terminal audit row.
+ test.each([
+ { name: 'unlimited (0) on a two-segment page', slug: 'conversations/imessage/alice-example', limit: 0, segments: 2, terminal: 1 },
+ { name: 'limit 1 on a two-segment page (partial)', slug: 'conversations/imessage/alice-example', limit: 1, segments: 1, terminal: 0 },
+ { name: 'limit 2 on a two-segment page (exact boundary)', slug: 'conversations/imessage/alice-example', limit: 2, segments: 2, terminal: 1 },
+ { name: 'limit 3 on a two-segment page', slug: 'conversations/imessage/alice-example', limit: 3, segments: 2, terminal: 1 },
+ { name: 'limit 1 on a one-segment page (exact boundary)', slug: 'conversations/imessage/one-segment-example', limit: 1, segments: 1, terminal: 1 },
+ ])('segment limit: $name (#5430)', async (c) => {
+ await engine.putPage('conversations/imessage/one-segment-example', {
+ type: 'conversation',
+ title: 'iMessage: One segment example',
+ compiled_truth: [
+ fmt('Alice Example', '2024-03-15', '9:00 AM', 'Shipping the report today.'),
+ fmt('Bob Demo', '2024-03-15', '9:02 AM', 'Thanks, I will review it.'),
+ ].join('\n'),
+ timeline: '',
+ frontmatter: {},
+ });
+ const result = await runExtractConversationFactsCore(engine, {
+ sourceId: 'default',
+ slug: c.slug,
+ segmentLimit: c.limit,
+ sleepMs: 0,
+ });
+ expect(result).toMatchObject({ pages_processed: 1, pages_failed: 0, segments_processed: c.segments });
+ const terminalRows = await engine.executeRaw<{ count: string | number }>(
+ `SELECT COUNT(*) AS count FROM facts WHERE source = $1 AND source_markdown_slug = $2`,
+ [TERMINAL_AUDIT_SOURCE, c.slug],
+ );
+ expect(Number(terminalRows[0]?.count ?? 0)).toBe(c.terminal);
+ const checkpoint = await loadOpCheckpoint(engine, {
+ op: 'extract-conversation-facts',
+ fingerprint: extractConversationFactsFingerprint({ sourceId: 'default' }),
+ });
+ expect(checkpoint.filter(entry => entry.includes(c.slug))).toHaveLength(1);
+ });
+
test('canonicalizes a raw LLM entity display name before writing facts.entity_slug', async () => {
chatTextOverride = JSON.stringify({
facts: [{