Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(transcripts): carry the discovered format per file into ingest --all (#7700)
garrytan514c9b3e289b
diff --git a/src/commands/transcripts.ts b/src/commands/transcripts.tsindex 5f4d35baf5..92fcaa1cc3 100644--- a/src/commands/transcripts.ts+++ b/src/commands/transcripts.ts@@ -398,6 +398,7 @@ async function runIngest(engine: BrainEngine, args: string[], dispatch: Pick<Cli// No paths: discovery. Without the all flag, show what WOULD be imported// and stop (a safe default for a command that can touch five harness// histories); with it, import the discovered set.+ let discoveredFormats: Map<string, TranscriptFormat> | undefined;if (parsed.paths.length === 0) {const { discoverTranscriptFiles } = await import('../core/transcripts/discover.ts');// #4472: discovery excludes gbrain's own claude-cli subprocess sessions@@ -423,6 +424,7 @@ async function runIngest(engine: BrainEngine, args: string[], dispatch: Pick<Clireturn;}parsed.paths = discovered.map((d) => d.path);+ discoveredFormats = new Map(discovered.map((d) => [d.path, d.format]));}// Source: the canonical 6-tier chain (capture.ts pattern) — one resolved@@ -502,6 +504,7 @@ async function runIngest(engine: BrainEngine, args: string[], dispatch: Pick<Cliresult = await runTranscriptsIngest(engine, {paths,format: parsed.format,+ discoveredFormats,dryRun: parsed.dryRun,limit: parsed.limit,sinceIso,diff --git a/src/core/transcripts/ingest.ts b/src/core/transcripts/ingest.tsindex 083743e1bd..2cc096f79b 100644--- a/src/core/transcripts/ingest.ts+++ b/src/core/transcripts/ingest.ts@@ -82,6 +82,8 @@ export interface TranscriptsIngestOpts {paths: string[];/** Explicit format wins over detection. */format?: TranscriptFormat;+ /** Per-file format harness discovery already established; used when no explicit format is set. */+ discoveredFormats?: ReadonlyMap<string, TranscriptFormat>;/** Parse + redact + render + report; ZERO engine writes. */dryRun?: boolean;/** Max sessions imported this run (session granularity; truncation ⇒ not a clean scan). */@@ -265,7 +267,7 @@ export async function runTranscriptsIngest(result.files.push(fileOutcome);const detected = detectAdapter(path, {- explicitFormat: opts.format,+ explicitFormat: opts.format ?? opts.discoveredFormats?.get(path),adapters: opts.adapters,});if (!detected.ok) {diff --git a/test/transcripts-all-discovered-format-7700.test.ts b/test/transcripts-all-discovered-format-7700.test.tsnew file mode 100644index 0000000000..d1b252e68c--- /dev/null+++ b/test/transcripts-all-discovered-format-7700.test.ts@@ -0,0 +1,92 @@+/**+ * #7700: `transcripts ingest --all` discovered each file under a native+ * harness root with a known format, then kept only the paths, so detection+ * ran again on every file. A Claude Code log whose leading bookkeeping record+ * is larger than the detector's 2 MiB fallback window then failed as+ * `unknown format`, while `--format claude-code` parsed the same file.+ *+ * Protects: the --all lane carries the format discovery established for each+ * file into the import, so that log parses; a mixed scan keeps each file's own+ * format. Seams: none. The CLI runs as a subprocess because the harness roots+ * hang off os.homedir(), which Bun fixes at process start; HOME points at a+ * throwaway harness tree holding a keyless PGLite brain.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import type { TranscriptsIngestResult } from '../src/core/transcripts/ingest.ts';+import { keylessBrainEnv } from './helpers/provider-env.ts';++const CLI = join(import.meta.dir, '..', 'src', 'cli.ts');+let home: string;++async function gbrain(args: string[]): Promise<{ exitCode: number; stdout: string; stderr: string }> {+ const env = keylessBrainEnv(process.env, home, {+ DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined, GBRAIN_SOURCE: undefined,+ HERMES_HOME: join(home, '.hermes'), GROK_HOME: join(home, '.grok'),+ GBRAIN_NON_INTERACTIVE: '1', GBRAIN_SKIP_STARTUP_HOOKS: '1', GBRAIN_NO_BANNER: '1', NO_COLOR: '1',+ });+ const proc = Bun.spawn([process.execPath, '--no-env-file', CLI, ...args], {+ cwd: home, env, stdin: Bun.file('/dev/null'), stdout: 'pipe', stderr: 'pipe',+ });+ const [stdout, stderr, exitCode] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]);+ return { exitCode, stdout, stderr };+}++beforeAll(async () => {+ home = mkdtempSync(join(tmpdir(), 'gb-transcripts-7700-'));+ const init = await gbrain(['init', '--pglite', '--no-embedding', '--json']);+ if (init.exitCode !== 0) throw new Error(`init failed: ${init.stdout}\n${init.stderr}`);+}, 180_000);++afterAll(() => {+ if (home) rmSync(home, { recursive: true, force: true });+});++async function ingestAllDryRun(): Promise<TranscriptsIngestResult> {+ const run = await gbrain(['transcripts', 'ingest', '--all', '--dry-run', '--json', '--quiet']);+ try {+ return JSON.parse(run.stdout) as TranscriptsIngestResult;+ } catch {+ throw new Error(`exit ${run.exitCode}: ${run.stdout}\n${run.stderr}`);+ }+}++let claudeLog = '';++describe('#7700: --all keeps the format discovery established', () => {+ test('a Claude Code log with a leading record over 2 MiB imports under --all', async () => {+ const folder = join(home, '.claude', 'projects', 'synthetic');+ mkdirSync(folder, { recursive: true });+ claudeLog = join(folder, 'synthetic.jsonl');+ writeFileSync(claudeLog, [+ { type: 'queue-operation', operation: 'add', sessionId: 'synthetic', timestamp: '2026-10-26T00:00:00Z', content: 'x'.repeat(3 * 1024 * 1024) },+ { type: 'user', sessionId: 'synthetic', uuid: 'u1', timestamp: '2026-10-26T00:00:01Z', message: { role: 'user', content: 'Synthetic question.' } },+ { type: 'assistant', sessionId: 'synthetic', uuid: 'a1', timestamp: '2026-10-26T00:00:02Z', message: { role: 'assistant', content: [{ type: 'text', text: 'Synthetic answer.' }] } },+ ].map((r) => JSON.stringify(r)).join('\n') + '\n');++ const result = await ingestAllDryRun();+ expect(result.files.map((f) => ({ path: f.path, format: f.format, error: f.error }))).toEqual([+ { path: claudeLog, format: 'claude-code', error: undefined },+ ]);+ expect(result).toMatchObject({ erroredFiles: 0, sessionsSeen: 1, driftFiles: 0 });+ expect(result.pages.planned).toBe(1);+ }, 120_000);++ test('a mixed scan keeps each file on its own discovered format', async () => {+ const codexDir = join(home, '.codex', 'sessions', '2026', '10', '26');+ mkdirSync(codexDir, { recursive: true });+ const codex = join(codexDir, 'rollout-synthetic.jsonl');+ const line = (timestamp: string, type: string, payload: unknown) => JSON.stringify({ timestamp, type, payload });+ writeFileSync(codex, [+ line('2026-10-26T01:00:00.000Z', 'session_meta', { id: 'codex-synthetic', timestamp: '2026-10-26T01:00:00.000Z', cwd: '/tmp/acme-example', cli_version: '0.159.3' }),+ line('2026-10-26T01:00:01.000Z', 'event_msg', { type: 'user_message', message: 'Synthetic codex question.' }),+ line('2026-10-26T01:00:02.000Z', 'response_item', { type: 'message', role: 'assistant', content: [{ type: 'output_text', text: 'Synthetic codex answer.' }] }),+ ].join('\n') + '\n');++ const result = await ingestAllDryRun();+ expect(Object.fromEntries(result.files.map((f) => [f.path, f.format ?? f.error]))).toEqual({ [claudeLog]: 'claude-code', [codex]: 'codex' });+ expect(result).toMatchObject({ erroredFiles: 0, sessionsSeen: 2 });+ }, 120_000);+});