Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(transcripts): a lookalike part-2 page is not split evidence (Wave B PR 4 lane 4e, cso r11)
garrytan1a0b58937f8b
diff --git a/src/core/transcripts/ingest.ts b/src/core/transcripts/ingest.tsindex 61a2682a2d..083743e1bd 100644--- a/src/core/transcripts/ingest.ts+++ b/src/core/transcripts/ingest.ts@@ -691,9 +691,13 @@ const IMPORTER_INGESTED_VIA = 'cli:transcripts-ingest';* from before attribution): trusted when `ingested_via` is the importer's* or NULL; a remote write is always a journaled request, and on this path* it also replaces the stamp with `mcp:<operation>`.- * So a remote edit can't choose the layout or the part size. A live part 2- * counts whoever wrote it last: ignoring it would merge a split session and- * delete its parts. A `part_bytes` is accepted only between PART_TARGET_BYTES+ * So a remote edit can't choose the layout or the part size. A part 2 is+ * split evidence only under the same rule: its id is derivable from part 1's,+ * so any writer can put a page carrying it anywhere. A split session the+ * importer rewrote still reads as split from part 1 (no `of`), or from a+ * part 2 the importer wrote. One exception keeps a remote edit from merging a+ * split session (deleting its parts): when part 1's own fields are not+ * trusted, a part 2 at part 1's slug plus `-p2` counts whoever wrote it. A `part_bytes` is accepted only between PART_TARGET_BYTES* (or the ceiling, when that is lower) and the current ceiling.*/async function storedTranscriptLayout(engine: BrainEngine, sourceId: string, meta: ParsedSession['meta'], pageCeiling: number):@@ -714,7 +718,8 @@ async function storedTranscriptLayout(engine: BrainEngine, sourceId: string, metreturn ti && typeof ti === 'object' ? ti as { of?: unknown; part_bytes?: unknown } : null;};const partOne = rows.find(row => row.fid === first);- const partTwo = rows.find(row => row.fid === second);+ const partTwo = rows.find(row => row.fid === second && (row.trusted === true+ || (partOne !== undefined && partOne.trusted !== true && row.slug === `${partOne.slug}-p2`)));const one = importMeta(partOne);const split = partTwo !== undefined || (one !== null && !(Number(one.of) <= 1));if (!split) return { layout: 'single', partTargetBytes: pageCeiling, existingSlug: partOne?.slug ?? null };diff --git a/test/helpers/transcripts-part-churn-cases.ts b/test/helpers/transcripts-part-churn-cases.tsindex a7920e64df..3695bda580 100644--- a/test/helpers/transcripts-part-churn-cases.ts+++ b/test/helpers/transcripts-part-churn-cases.ts@@ -146,6 +146,19 @@ async function remoteRewriteImportMeta(engine: BrainEngine, dir: string | null,expect((stored!.frontmatter as Record<string, any>).transcript_import).toEqual(fm.transcript_import);}+/** A remote agent (`remote: true`, a registered writer) creates a page through MCP `put_page`. */+async function remoteCreatePage(engine: BrainEngine, dir: string, slug: string, content: string): Promise<void> {+ await withEnv({ GBRAIN_HOME: join(dir, 'home') }, async () => {+ const registration = await registerLocalWriter(engine, 'stdio', { sourceIds: ['*'], operations: null, scopes: ['read', 'write'], slugPrefixes: null } as never);+ const ctx = { remote: true, config: { engine: engine.kind }, sourceId: 'default', logger: { info() {}, warn() {}, error() {} },+ auth: { token: 'fixture', clientId: 'remote-agent-example', scopes: ['read', 'write'], sourceId: 'default' } } as never;+ const put = await withVerifiedLocalRegistration(engine, registration, () => dispatchToolCall(engine, 'put_page',+ { slug, request_id: randomUUID(), content }, ctx));+ expect((put as { isError?: boolean }).isError ? (put as { content: Array<{ text: string }> }).content[0]!.text : 'ok').toBe('ok');+ });+ await disposePersistenceConsumer(engine);+}+async function withDir(fn: (dir: string) => Promise<void>): Promise<void> {const dir = mkdtempSync(join(tmpdir(), 'gb-part-churn-'));try { await fn(dir); } finally { rmSync(dir, { recursive: true, force: true }); }@@ -417,4 +430,49 @@ export function defineTranscriptPartChurnCases(opts: { getEngine: () => BrainEngexpect(splitAfter.map(p => p.frontmatter.transcript_import.part_bytes)).toEqual([54_000, 54_000, 54_000, 54_000]);expect(splitAfter.slice(1, 3).map(p => p.hash)).toEqual(splitBefore.slice(1, 3).map(p => p.hash));}), { databaseUrl: opts.databaseUrl }), 300_000);++ test('a remote page carrying the session\'s part-2 id is not split evidence; an importer-written part 2 still is', () => withDir(async (dir) => {+ const engine = opts.getEngine();+ const id = `squat-${randomUUID().slice(0, 8)}`;+ const base = baseSlugOf(id);+ const squat = `notes/squat-${id}`;+ const partTwoId = `openclaw-${transcriptFullId(id)}-p2`;+ await remoteCreatePage(engine, dir, squat, `---\n${dumpFrontmatterYaml({ type: 'note', title: 'Squat', id: partTwoId,+ transcript_import: { harness: 'openclaw', session_id: id, version: 1, part: 2, part_bytes: PART_TARGET_BYTES } })}---\nsquatted\n`);+ const s0 = regular(50);+ const path = writeSession(dir, id, s0);+ expect(await legacyGroupCount(path)).toBeGreaterThanOrEqual(4);+ const r = await ingest(engine, path);+ expect({ pages: r.pages, partsDeleted: r.partsDeleted, sessionsErrored: r.sessionsErrored })+ .toEqual({ pages: { imported: 1, skipped: 0, errored: 0, planned: 0, pending: 0 }, partsDeleted: 0, sessionsErrored: 0 });+ const pages = await liveParts(engine, base);+ expect(pages.map(p => p.slug)).toEqual([base]);+ expect(pages[0]!.frontmatter.transcript_import).toEqual({ harness: 'openclaw', session_id: id, version: 1, part: 1, of: 1 });+ // The same squat at the session's own -p2 slug: part 1 is the importer's one page, so it stays one page and the stray part is reconciled away.+ // (The first squat goes first: the importer's identity dedup would skip a second page with the same id.)+ await engine.deletePage(squat, { sourceId: 'default' });+ await remoteCreatePage(engine, dir, `${base}-p2`, `---\n${dumpFrontmatterYaml({ type: 'note', title: 'Squat', id: partTwoId,+ transcript_import: { harness: 'openclaw', session_id: id, version: 1, part: 2, part_bytes: PART_TARGET_BYTES } })}---\nsquatted\n`);+ expect((await liveParts(engine, base)).map(p => p.slug)).toEqual([base, `${base}-p2`]);+ const grown = await ingest(engine, writeSession(dir, id, [...s0, { text: 'small follow-up' }]));+ expect({ pages: grown.pages, partsDeleted: grown.partsDeleted })+ .toEqual({ pages: { imported: 1, skipped: 0, errored: 0, planned: 0, pending: 0 }, partsDeleted: 1 });+ expect((await liveParts(engine, base)).map(p => p.slug)).toEqual([base]);++ // A genuinely split session whose part 1 a remote writer edited still reads as split from its importer-written part 2.+ const splitId = `squat-split-${randomUUID().slice(0, 8)}`;+ const splitBase = baseSlugOf(splitId);+ const t0 = regular(30);+ const n = await seedLegacySplit(engine, writeSession(dir, splitId, t0));+ const t1 = [...t0, { text: 'small follow-up A' }];+ expect((await ingest(engine, writeSession(dir, splitId, t1))).pages).toMatchObject({ imported: n, errored: 0 });+ const before = await liveParts(engine, splitBase);+ await remoteRewriteImportMeta(engine, dir, splitBase, ti => { ti.of = 1; });+ const again = await ingest(engine, writeSession(dir, splitId, [...t1, { text: 'small follow-up B' }]));+ expect({ partsDeleted: again.partsDeleted, sessionsErrored: again.sessionsErrored }).toEqual({ partsDeleted: 0, sessionsErrored: 0 });+ expect(again.pages).toMatchObject({ imported: 2, skipped: n - 2, errored: 0 });+ const after = await liveParts(engine, splitBase);+ expect(after.map(p => p.slug)).toEqual(before.map(p => p.slug));+ expect(after.slice(1, n - 1).map(p => p.hash)).toEqual(before.slice(1, n - 1).map(p => p.hash));+ }), 180_000);}