Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(synthesize): route corpus sessions by real path and re-check the source when the child runs (#7871, #7850)
garrytan13e447335f14
diff --git a/scripts/function-size-baseline.tsv b/scripts/function-size-baseline.tsvindex cd70f3608d..86b6d796ca 100644--- a/scripts/function-size-baseline.tsv+++ b/scripts/function-size-baseline.tsv@@ -40,8 +40,8 @@ src/core/embed-stale.ts embedStaleForSource 302 PR 4 E round 2 (gbrain-evals#180src/core/facts/backstop.ts runPipelineBodyInner 358 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1)src/core/import-file.ts importFromContent 692 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1); +2 wave 8 lane D1 #5158: key-order fallback equality and legacy-hash ordering; lowered to 807 (#5988: screen and sanity config peeled to core/import-screen.ts); +1 #6007: the prepared apply reports its written page and seal (PreparedImportApplied) so put_page skips a re-read and a second seal; +1 #5333: embed-skip transition defeats the unchanged fast paths (the stale-marker clear stays inside the sanity block); +3 #6188: Tier 1 fence step in place before the hidden-row merge (logic in fence-repair/import-step.ts); -146 #5575: content-sanity/write-gate disposition moved to import-screen.ts settleContentDisposition; +4 TODO: large-pages A6/T33: a page over PAGE_INLINE_MAX_BYTES commits with NULL vectors and embeds through the metered stale sweep; +1 #6722: an embed_skip page is chunked and never embedded (noEmbed folds the marker); +16 Wave B lane 1d (TODO wave-b PR 1): a body above the inline threshold takes the segmented stage and generation flip at the storage step (logic in persistence/segmented-apply.ts); +3 Wave B review: reuseSegments option (a rename re-preparation reuses the staged segmented body) and the remote fence merge over fenceBody; +2 Wave B gem A: a coordinated demotion versions the segmented page before demoting itsrc/core/minions/handlers/subagent-oneshot.ts runSubagentOneshot 378 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1); +1 fix wave 9 lane B #4921: the oneshot sub-budget is passed as the call timeoutMs so the 300 s gateway backstop cannot cut it-src/core/minions/handlers/subagent.ts makeSubagentHandler 889 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1); +3 fix wave 9 lane B #4921: per-turn timeout read once, passed to oneshot and gateway loop; +2 GBRA-60: a trusted dream child gets the put_pages batch tool (buildBrainPutPagesTool) from the oneshot tool options-src/core/minions/handlers/subagent.ts makeSubagentHandler>subagentHandlerInner 819 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1); +3 fix wave 9 lane B #4921: per-turn timeout read once, passed to oneshot and gateway loop; +2 GBRA-60: a trusted dream child gets the put_pages batch tool (buildBrainPutPagesTool) from the oneshot tool options+src/core/minions/handlers/subagent.ts makeSubagentHandler 890 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1); +3 fix wave 9 lane B #4921: per-turn timeout read once, passed to oneshot and gateway loop; +2 GBRA-60: a trusted dream child gets the put_pages batch tool (buildBrainPutPagesTool) from the oneshot tool options; +1 #7850: a dream synthesis child re-checks its session source before any model call (one call line; the check is refuseChangedSessionSource in cycle/synthesize-source-routing.ts)+src/core/minions/handlers/subagent.ts makeSubagentHandler>subagentHandlerInner 820 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1); +3 fix wave 9 lane B #4921: per-turn timeout read once, passed to oneshot and gateway loop; +2 GBRA-60: a trusted dream child gets the put_pages batch tool (buildBrainPutPagesTool) from the oneshot tool options; +1 #7850: a dream synthesis child re-checks its session source before any model call (one call line; the check is refuseChangedSessionSource in cycle/synthesize-source-routing.ts)src/core/minions/handlers/subagent.ts runSubagentViaGateway 329 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1); +1 fix wave 9 lane B #4921: turnTimeoutMs forwarded to gateway.toolLoopsrc/core/minions/queue.ts MinionQueue.add 498 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1)src/core/minions/queue.ts MinionQueue.add>result=this.engine.transaction(#0) 359 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1)diff --git a/src/core/cycle/synthesize-source-routing.ts b/src/core/cycle/synthesize-source-routing.tsindex 92c21549ef..7f675b0252 100644--- a/src/core/cycle/synthesize-source-routing.ts+++ b/src/core/cycle/synthesize-source-routing.ts@@ -15,62 +15,141 @@* (read from the cycle's source) are not session captures and pass unchanged.* extract_atoms routes its session-corpus transcripts through the same two* functions (#7846).+ *+ * A file is a corpus file when its spelled path or its real path lies in the+ * corpus (listing follows symlinks, so an aliasing meeting dir or a linked+ * spool dir still names corpus files); a path whose real path cannot be read is+ * held (`corpus_path_unresolvable`), never passed through (#7871). Work queued+ * or retained by an earlier run is checked again before it writes: a synthesis+ * child re-resolves its pinned file (`corpusSourceChanged`, #7850), and an+ * explicit atom retry or a resumed generation member re-checks its transcript+ * (`transcriptSourceHold`, #7869, #7870). Dry runs resolve read-only.*/++import { realpathSync } from 'node:fs';import { basename, dirname, isAbsolute, relative, resolve, sep } from 'node:path';import type { BrainEngine } from '../engine.ts';import type { PhaseResult } from '../cycle.ts';import { corpusSpoolDir } from '../context/corpus-segments.ts';-import { resolveCorpusFileSource } from '../context/corpus-source.ts';+import { resolveCorpusFileSource, type CorpusFileLocation, type CorpusFileSource } from '../context/corpus-source.ts';import { CONVERSATION_PAGE_URI_PREFIX, type DiscoveredTranscript } from './transcript-discovery.ts';+import { UnrecoverableError } from '../minions/types.ts';export interface SessionSourceRouting {/** Corpus files of other sources (`corpus_source_other`), counted by the source each belongs to. */corpus_source_other: Record<string, number>;/** Corpus files with no evidence of their source, counted by the resolver's hold reason. */held: Record<string, number>;+ /** extract_atoms: pending generation members not resubmitted because their transcript no longer resolves to this source, by reason. */+ generation_members_held?: Record<string, number>;}export const emptySessionSourceRouting = (): SessionSourceRouting => ({ corpus_source_other: {}, held: {} });-function corpusLocation(filePath: string, corpusDir: string): 'spool' | 'legacy' | null {- const root = resolve(corpusDir);- const file = resolve(filePath);+/** The corpus file a synthesis child writes for, and the source incarnation discovery resolved it to (#7850). */+export interface CorpusSourcePin { raw_source: string; corpus_dir: string; incarnation: string }++function realpathOrMissing(path: string): string | null | undefined {+ try { return realpathSync(path); } catch (e) { return (e as NodeJS.ErrnoException).code === 'ENOENT' ? null : undefined; }+}++function locate(file: string, root: string, spool: string | null): CorpusFileLocation | null {const rel = relative(root, file);- if (!rel || rel === '..' || rel.startsWith('..' + sep) || isAbsolute(rel)) return null;- return dirname(file) === resolve(corpusSpoolDir(root)) ? 'spool' : 'legacy';+ if (dirname(file) === spool) return 'spool';+ return !rel || rel === '..' || rel.startsWith('..' + sep) || isAbsolute(rel) ? null : 'legacy';+}++function corpusLocation(filePath: string, corpusDir: string): CorpusFileLocation | 'unresolvable' | null {+ const root = resolve(corpusDir);+ const lexical = locate(resolve(filePath), root, resolve(corpusSpoolDir(root)));+ const realRoot = realpathOrMissing(root);+ if (realRoot === null) return lexical;+ const realSpool = realpathOrMissing(corpusSpoolDir(root));+ const realFile = realpathOrMissing(filePath);+ if (realRoot === undefined || realSpool === undefined || !realFile) return 'unresolvable';+ const real = locate(realFile, realRoot, realSpool);+ return lexical === 'spool' || real === 'spool' ? 'spool' : lexical ?? real;+}++/** The session source of a corpus file, or null when `filePath` is not a corpus file (an unreadable real path is held). */+export async function corpusFileOwner(engine: BrainEngine, filePath: string, corpusDir: string | null,+ opts: { readOnly?: boolean } = {}): Promise<CorpusFileSource | null> {+ if (!corpusDir || filePath.startsWith(CONVERSATION_PAGE_URI_PREFIX)) return null;+ const location = corpusLocation(filePath, corpusDir);+ if (location === 'unresolvable') return { ok: false, reason: 'corpus_path_unresolvable' };+ return location ? resolveCorpusFileSource(engine, corpusDir, basename(filePath), location, opts) : null;}-/** The transcripts this cycle may synthesize into `sourceId`; every other corpus file is counted in `routing`. */-export async function keepSessionSourceTranscripts(engine: BrainEngine, transcripts: DiscoveredTranscript[],- opts: { corpusDir: string | null; sourceId: string }, routing: SessionSourceRouting): Promise<DiscoveredTranscript[]> {- if (!opts.corpusDir) return transcripts;- const kept: DiscoveredTranscript[] = [];+/** The transcripts this cycle may synthesize into `sourceId`, each corpus file pinned; every other corpus file is counted in `routing`. */+export async function keepSessionSourceTranscripts<T extends DiscoveredTranscript>(engine: BrainEngine, transcripts: T[],+ opts: { corpusDir: string | null; sourceId: string; readOnly?: boolean }, routing: SessionSourceRouting): Promise<Array<T & { corpusSource?: CorpusSourcePin }>> {+ const kept: Array<T & { corpusSource?: CorpusSourcePin }> = [];for (const t of transcripts) {- const location = t.filePath.startsWith(CONVERSATION_PAGE_URI_PREFIX) ? null : corpusLocation(t.filePath, opts.corpusDir);- if (!location) { kept.push(t); continue; }- const owner = await resolveCorpusFileSource(engine, opts.corpusDir, basename(t.filePath), location);- if (!owner.ok) routing.held[owner.reason] = (routing.held[owner.reason] ?? 0) + 1;+ const owner = await corpusFileOwner(engine, t.filePath, opts.corpusDir, opts);+ if (!owner) kept.push(t);+ else if (!owner.ok) routing.held[owner.reason] = (routing.held[owner.reason] ?? 0) + 1;else if (owner.sourceId !== opts.sourceId) routing.corpus_source_other[owner.sourceId] = (routing.corpus_source_other[owner.sourceId] ?? 0) + 1;- else kept.push(t);+ else kept.push({ ...t, corpusSource: { raw_source: t.filePath, corpus_dir: opts.corpusDir!, incarnation: owner.incarnation } });}return kept;}+/**+ * Why a transcript an earlier run extracted may not be written into `sourceId` now, or null: a corpus file must resolve+ * to `sourceId`, and any other transcript is a meeting file, which only `default` reads (#7846).+ */+export async function transcriptSourceHold(engine: BrainEngine, filePath: string,+ opts: { corpusDir: string | null; sourceId: string }): Promise<{ reason: string; owner?: string } | null> {+ const owner = await corpusFileOwner(engine, filePath, opts.corpusDir);+ if (!owner) return opts.sourceId === 'default' ? null : { reason: 'transcript_outside_corpus' };+ if (!owner.ok) return { reason: owner.reason };+ return owner.sourceId === opts.sourceId ? null : { reason: 'corpus_source_other', owner: owner.sourceId };+}++/** Why a queued synthesis child may no longer write `pin.raw_source`'s pages into `sourceId`, or null (read-only). */+export async function corpusSourceChanged(engine: BrainEngine, pin: { raw_source: string; corpus_dir: string | null; incarnation?: string },+ sourceId: string): Promise<string | null> {+ const owner = await corpusFileOwner(engine, pin.raw_source, pin.corpus_dir, { readOnly: true });+ if (!owner) return pin.incarnation ? 'corpus_path_unresolvable' : null;+ if (!owner.ok) return owner.reason;+ if (owner.sourceId !== sourceId) return 'corpus_source_other';+ return pin.incarnation && owner.incarnation !== pin.incarnation ? 'corpus_source_incarnation_changed' : null;+}++/**+ * A dream synthesis child's check before any model call: refused (`session_source_changed: <reason>`, no retry) when its+ * pinned file no longer resolves to the job's source or incarnation. A child queued before the pin existed is checked+ * from `dream_output.raw_source` and the configured corpus dir.+ */+export async function refuseChangedSessionSource(engine: BrainEngine,+ data: { corpus_source?: CorpusSourcePin; dream_output?: { raw_source: string }; source_id?: string }): Promise<void> {+ if (!data.corpus_source && !data.dream_output) return;+ const sourceId = data.source_id ?? 'default';+ const pin = data.corpus_source ?? { raw_source: data.dream_output!.raw_source,+ corpus_dir: await engine.getConfig('dream.synthesize.session_corpus_dir').catch(() => null) };+ const changed = await corpusSourceChanged(engine, pin, sourceId);+ if (changed) throw new UnrecoverableError(`session_source_changed: ${changed}: the session of ${pin.raw_source} no longer resolves to source ${sourceId} as it did when this job was queued, so nothing was synthesized or written. It synthesizes when the dream runs for its own source, or after gbrain sweep --assign-corpus <source> maps it.`);+}+/** Names every corpus file the phase left out, with the command that synthesizes (or extracts) or releases it. */export function withSessionSourceRouting(result: PhaseResult, routing: SessionSourceRouting, sourceId: string,phase: 'synthesize' | 'extract_atoms' = 'synthesize'): PhaseResult {const others = Object.entries(routing.corpus_source_other).sort(([a], [b]) => a.localeCompare(b));const held = Object.entries(routing.held).sort(([a], [b]) => a.localeCompare(b));- if (!others.length && !held.length) return result;+ const members = Object.entries(routing.generation_members_held ?? {}).sort(([a], [b]) => a.localeCompare(b));+ if (!others.length && !held.length && !members.length) return result;const otherCount = others.reduce((n, [, c]) => n + c, 0);const heldCount = held.reduce((n, [, c]) => n + c, 0);+ const memberCount = members.reduce((n, [, c]) => n + c, 0);const fix = [...others.map(([id]) => `gbrain dream --source ${id} --phase ${phase}`),...(heldCount ? ['gbrain sweep --assign-corpus <source>'] : []),+ ...(memberCount ? [`gbrain doctor --only generation_pending --json`] : []),];const parts = [...(otherCount ? [`${otherCount} corpus file(s) belong to other sources (${others.map(([id, c]) => `${id}: ${c}`).join(', ')}) and ${phase === 'synthesize' ? 'synthesize' : 'extract'} only under their own source`] : []),...(heldCount ? [`${heldCount} held with no frozen session source (${held.map(([r, c]) => `${r}: ${c}`).join(', ')})`] : []),+ ...(memberCount ? [`${memberCount} pending generation member(s) held, not resubmitted: their transcript no longer resolves to this source (${members.map(([r, c]) => `${r}: ${c}`).join(', ')})`] : []),];process.stderr.write(`[dream] ${phase} for source ${sourceId}: ${parts.join('; ')}. Fix: ${fix.join(' | ')}\n`);return { ...result, summary: `${result.summary}; ${parts.join('; ')}`,diff --git a/src/core/cycle/synthesize.ts b/src/core/cycle/synthesize.tsindex aa28034252..114de100d5 100644--- a/src/core/cycle/synthesize.ts+++ b/src/core/cycle/synthesize.ts@@ -527,7 +527,7 @@ async function runPhaseSynthesizeInner(].map(prefix => join(opts.brainDir, prefix)),// #5413: corpus files captured from gbrain's own claude-cli calls.selfCaptureSessionIds: claudeCliSelfSessionIds(),- })), { corpusDir: config.corpusDir, sourceId: opts.sourceId ?? 'default' }, routing);+ })), { corpusDir: config.corpusDir, sourceId: opts.sourceId ?? 'default', readOnly: opts.dryRun }, routing);if (transcripts.length === 0) {return ok('no transcripts to process', { transcripts_processed: 0, pages_written: 0 });@@ -927,7 +927,7 @@ async function runPhaseSynthesizeInner(: t.contentHash.slice(0, 6),oneshot_task_prefixes: [config.reflectionsPrefix, config.originalsPrefix], // #6160: oneshot task shapes...(maintenance ? { dream_output: { raw_source: t.filePath, transcript_hash: t.contentHash, cycle_date: summaryDate, quote_verify: config.quoteVerify, ...(t.seat ? { seat: t.seat } : {}) } } : {}), // GBRA-60- require_writes: true,+ require_writes: true, ...(t.corpusSource ? { corpus_source: t.corpusSource } : {}), // #7850: re-resolved when the child runs// #1586: scope every child tool call to the cycle's resolved source// so put_page writes land there instead of the hardcoded 'default'....(opts.sourceId ? { source_id: opts.sourceId } : {}),diff --git a/src/core/minions/handlers/subagent.ts b/src/core/minions/handlers/subagent.tsindex ec2bed7468..113463cacb 100644--- a/src/core/minions/handlers/subagent.ts+++ b/src/core/minions/handlers/subagent.ts@@ -330,6 +330,7 @@ export function makeSubagentHandler(deps: SubagentDeps) {if (!data.prompt || typeof data.prompt !== 'string') {throw new Error('subagent job data.prompt is required (string)');}+ if (!submitted) await (await import('../../cycle/synthesize-source-routing.ts')).refuseChangedSessionSource(engine, data); // #7850// v0.38 (S1.5 + S1.7) — capability-based gate replaces the v0.31.12// Anthropic-only check. The handler now routes between two paths:diff --git a/src/core/minions/types.ts b/src/core/minions/types.tsindex 1abb098a9b..b03f43ea62 100644--- a/src/core/minions/types.ts+++ b/src/core/minions/types.ts@@ -648,6 +648,14 @@ export interface SubagentHandlerData {* outputs postprocessing verifies and republishes.*/dream_output?: DreamOutputSpec;+ /**+ * #7850: the session-corpus file a dream synthesis child writes for, the corpus dir and the source incarnation discovery+ * resolved it to. The child resolves the file again (read-only) before any model call and refuses+ * (`session_source_changed: <reason>`) when the session no longer resolves to `source_id` or the incarnation changed; a+ * child without it is re-checked from `dream_output.raw_source` and the configured corpus dir. Trusted field+ * (PROTECTED_JOB_NAMES); a delegated (remote-owned) job ignores it.+ */+ corpus_source?: { raw_source: string; corpus_dir: string; incarnation: string };/*** GBRA-60: the agent loop's put_page stages each page instead of writing it (minions/tools/staged-put-page.ts): every* check put_page runs before journaling still refuses, but the page lives only in this job's tool-execution ledgerdiff --git a/test/e2e/synthesize-session-source-recheck-7850.test.ts b/test/e2e/synthesize-session-source-recheck-7850.test.tsnew file mode 100644index 0000000000..40f1d63a05--- /dev/null+++ b/test/e2e/synthesize-session-source-recheck-7850.test.ts@@ -0,0 +1,3 @@+import { registerPostgresTests } from '../helpers/test-backends.ts';++await registerPostgresTests(() => import('../synthesize-session-source-recheck-7850.test.ts'));diff --git a/test/e2e/synthesize-source-routing-realpath-7871.test.ts b/test/e2e/synthesize-source-routing-realpath-7871.test.tsnew file mode 100644index 0000000000..03b21f65ad--- /dev/null+++ b/test/e2e/synthesize-source-routing-realpath-7871.test.ts@@ -0,0 +1,3 @@+import { registerPostgresTests } from '../helpers/test-backends.ts';++await registerPostgresTests(() => import('../synthesize-source-routing-realpath-7871.test.ts'));diff --git a/test/synthesize-session-source-recheck-7850.test.ts b/test/synthesize-session-source-recheck-7850.test.tsnew file mode 100644index 0000000000..7e3846ef52--- /dev/null+++ b/test/synthesize-session-source-recheck-7850.test.ts@@ -0,0 +1,200 @@+/**+ * #7850: dream synthesis re-checks a session's source when the work runs, and+ * dry runs write no session record.+ *+ * 1. Each synthesis child job carries the corpus file it synthesizes, the+ * corpus dir and the source incarnation discovery resolved+ * (`corpus_source`). When the child runs it resolves the file again+ * (read-only) and refuses, before any model call or write, when the session+ * no longer resolves to the job's source or the source's incarnation+ * changed. A child queued by an earlier release (no pin) is re-checked from+ * its `dream_output.raw_source` and the configured corpus dir.+ * 2. `gbrain dream --dry-run` (synthesize) resolves corpus files read-only:+ * an unresolved session's record is not frozen by a preview.+ *+ * Repo-less brain directories; PGLite and, with DATABASE_URL, Postgres.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { randomUUID } from 'node:crypto';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { runPhaseSynthesize } from '../src/core/cycle/synthesize.ts';+import { CORPUS_UNRESOLVED_STAMP, corpusSpoolDir, ensureCorpusSpoolDir, sessionCorpusFileName } from '../src/core/context/corpus-segments.ts';+import { openSessionSource, readSessionSource } from '../src/core/context/corpus-source.ts';+import { makeSubagentHandler } from '../src/core/minions/handlers/subagent.ts';+import type { MinionJobContext, SubagentHandlerData } from '../src/core/minions/types.ts';+import { claimWorktree } from '../src/core/persistence/ownership.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { configureGateway, resetGateway, __setChatTransportForTests } from '../src/core/ai/gateway.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { withEnv } from './helpers/with-env.ts';+import { testBackends } from './helpers/test-backends.ts';++const backends = testBackends();+const engines: BrainEngine[] = [];+let dataDir: string;+let closePostgres: (() => Promise<void>) | undefined;+beforeAll(async () => {+ dataDir = mkdtempSync(join(tmpdir(), 'gbrain-synth-recheck-db-'));+ configureGateway({ embedding_model: 'openai:text-embedding-3-large', embedding_dimensions: 1536, env: {} });+ if (backends.includes('pglite')) {+ const engine = new PGLiteEngine();+ await engine.connect({ database_path: dataDir });+ await engine.initSchema();+ engines.push(engine);+ }+ if (backends.includes('postgres')) {+ const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!);+ engines.push(pg.engine);+ closePostgres = pg.close;+ }+}, 120_000);+afterAll(async () => {+ for (const engine of engines) { await disposePersistenceConsumer(engine); await engine.disconnect(); }+ await closePostgres?.();+ resetGateway();+ rmSync(dataDir, { recursive: true, force: true });+});++const quote = 'we keep the quarterly plan in one place so every team reads the same numbers';+const usage = { input_tokens: 100, output_tokens: 100, cache_read_tokens: 0, cache_creation_tokens: 0 };++interface Fixture { engine: BrainEngine; dir: string; cycle: { id: string; root: string }; other: string; corpus: string; calls: () => number }++async function fixture(run: (f: Fixture) => Promise<void>): Promise<void> {+ for (const engine of engines) {+ const dir = mkdtempSync(join(tmpdir(), 'gbrain-synth-recheck-'));+ const corpus = join(dir, 'corpus');+ const tag = randomUUID().slice(0, 8);+ const cycle = { id: `source-a-${tag}`, root: join(dir, 'brain-a') };+ const other = `source-b-${tag}`;+ mkdirSync(corpus);+ mkdirSync(cycle.root);+ let calls = 0;+ try {+ await withEnv({ GBRAIN_HOME: join(dir, 'home'), ANTHROPIC_API_KEY: 'sk-test-synth-recheck', GBRAIN_SOURCE: undefined }, async () => {+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ await engine.executeRaw('INSERT INTO sources(id,name,local_path) VALUES($1,$1,$2)', [cycle.id, cycle.root]);+ await engine.executeRaw('INSERT INTO sources(id,name) VALUES($1,$1)', [other]);+ await engine.setConfig('sync.write_through', 'true');+ await claimWorktree(engine, cycle.id, cycle.root);+ for (const [key, value] of Object.entries({+ 'dream.synthesize.enabled': 'true', 'dream.synthesize.cooldown_hours': '0',+ 'dream.synthesize.min_chars': '100', 'dream.synthesize.link_manifest': 'false',+ 'dream.synthesize.mode': 'oneshot', 'dream.synthesize.session_corpus_dir': corpus,+ 'models.dream.synthesize': 'anthropic:claude-sonnet-4-6', 'models.dream.triage': 'anthropic:claude-sonnet-4-6',+ 'agent.use_gateway_loop': 'true',+ })) await engine.setConfig(key, value);+ __setChatTransportForTests(async opts => {+ calls++;+ const user = String(opts.messages?.[0]?.content ?? '');+ const hash = /hash suffix \(USE THIS in slugs\): ([a-z0-9-]+)/i.exec(user)?.[1] ?? 'missing';+ const text = (opts.system ?? '').startsWith('You triage a conversation transcript')+ ? JSON.stringify({ score: 0.9, content_type: 'reflection', segments: [{ quote, note: 'evidence' }], entities: [], reasons: ['durable insight'] })+ : JSON.stringify({ pages: [{ slug: `wiki/personal/reflections/session-${hash}`, title: 'Session', type: 'note',+ body: 'A durable planning habit with [[people/example]].' }], skipped: false });+ return { text, blocks: [{ type: 'text', text }], stopReason: 'end', usage, model: opts.model!, providerId: 'anthropic' };+ });+ await run({ engine, dir, cycle, other, corpus, calls: () => calls });+ });+ } finally {+ __setChatTransportForTests(null);+ await disposePersistenceConsumer(engine);+ await engine.executeRaw("DELETE FROM pages WHERE slug LIKE 'wiki/personal/reflections/%'");+ rmSync(dir, { recursive: true, force: true });+ }+ }+}++function writeTranscript(path: string, topic: string): void {+ writeFileSync(path, `[user]\n${quote}. Today we covered ${topic}.\n\n${'[assistant]\nDiscuss the long term roadmap.\n\n'.repeat(15)}`);+}++let clientCalls = 0;++/** A synthesis child as the fan-out queues it, run by the real subagent handler (no queue worker). */+async function runChild(engine: BrainEngine, data: Partial<SubagentHandlerData>): Promise<unknown> {+ const full = { prompt: 'Synthesize the transcript.', model: 'anthropic:claude-sonnet-4-6', mode: 'oneshot', require_writes: true,+ allowed_slug_prefixes: ['wiki/personal/reflections/*'], oneshot_slug_suffix: 'abc123', ...data };+ const [job] = await engine.executeRaw<{ id: number }>(+ `INSERT INTO minion_jobs (submission_authority, name, status, data, queue, priority, created_at)+ VALUES ('{"version":1,"kind":"application"}'::jsonb, 'subagent', 'active', $1::text::jsonb, 'default', 0, now()) RETURNING id`,+ [JSON.stringify(full)]);+ const ctx = {+ id: job.id, name: 'subagent', data: full, attempts_made: 1,+ signal: new AbortController().signal, deadlineAtMs: null, shutdownSignal: new AbortController().signal,+ updateProgress: async () => {}, updateTokens: async () => {}, log: async () => {},+ isActive: async () => true, readInbox: async () => [],+ } as unknown as MinionJobContext;+ const client = { create: async () => { clientCalls++; throw new Error('the legacy Messages client was called'); } };+ const handler = makeSubagentHandler({ engine, client: client as never, config: { engine: engine.kind } as never });+ return handler(ctx);+}++const reflections = (engine: BrainEngine) => engine.executeRaw<{ source_id: string; slug: string }>(+ "SELECT source_id, slug FROM pages WHERE slug LIKE 'wiki/personal/reflections/%' AND deleted_at IS NULL ORDER BY slug");++describe('#7850 synthesis re-checks the session source when the child runs', () => {+ test('every queued child carries the corpus file, corpus dir and source incarnation discovery resolved', async () => {+ await fixture(async ({ engine, cycle, corpus }) => {+ const own = join(ensureCorpusSpoolDir(corpus), sessionCorpusFileName('sess-own', cycle.id));+ writeTranscript(own, 'the shared planning calendar');+ const result = await runPhaseSynthesize(engine, { brainDir: cycle.root, sourceId: cycle.id, dryRun: false });+ expect(result.summary).toContain('1 transcript(s) synthesized');+ const [incarnation] = await engine.executeRaw<{ incarnation: string }>('SELECT incarnation::text AS incarnation FROM sources WHERE id=$1', [cycle.id]);+ const jobs = await engine.executeRaw<{ data: Record<string, unknown> }>("SELECT data FROM minion_jobs WHERE name='subagent' AND data->>'source_id'=$1", [cycle.id]);+ expect(jobs.length).toBeGreaterThan(0);+ for (const job of jobs) {+ const data = typeof job.data === 'string' ? JSON.parse(job.data) : job.data;+ expect(data.corpus_source).toEqual({ raw_source: own, corpus_dir: corpus, incarnation: incarnation.incarnation });+ }+ });+ }, 180_000);++ test('a child whose session belongs to another source, or whose source was recreated, is refused before any model call', async () => {+ await fixture(async ({ engine, cycle, other, corpus, calls }) => {+ const spool = ensureCorpusSpoolDir(corpus);+ const foreign = join(spool, sessionCorpusFileName('sess-foreign', other));+ writeTranscript(foreign, 'the client renewal terms');+ const legacy = await runChild(engine, { source_id: cycle.id,+ dream_output: { raw_source: foreign, transcript_hash: 'h', cycle_date: '2026-10-28', quote_verify: false } }).then(() => new Error('the child ran'), (e: Error) => e);+ expect(legacy).toBeInstanceOf(Error);+ expect(String(legacy.message)).toMatch(/^session_source_changed: corpus_source_other/);++ const own = join(spool, sessionCorpusFileName('sess-own', cycle.id));+ writeTranscript(own, 'the shared planning calendar');+ const pinned = await runChild(engine, { source_id: cycle.id,+ corpus_source: { raw_source: own, corpus_dir: corpus, incarnation: '00000000-0000-4000-8000-000000000000' } }).then(() => new Error('the child ran'), (e: Error) => e);+ expect(pinned).toBeInstanceOf(Error);+ expect(String(pinned.message)).toMatch(/^session_source_changed: corpus_source_incarnation_changed/);++ const unresolved = join(spool, sessionCorpusFileName('sess-unresolved', CORPUS_UNRESOLVED_STAMP));+ writeTranscript(unresolved, 'the hiring loop');+ const held = await runChild(engine, { source_id: cycle.id,+ corpus_source: { raw_source: unresolved, corpus_dir: corpus, incarnation: '00000000-0000-4000-8000-000000000000' } }).then(() => new Error('the child ran'), (e: Error) => e);+ expect(String(held.message)).toMatch(/^session_source_changed: corpus_source_unresolved/);++ expect(calls()).toBe(0);+ expect(clientCalls).toBe(0);+ expect(await reflections(engine)).toEqual([]);+ });+ }, 180_000);+});++describe('#7850 a synthesize dry run writes no session record', () => {+ test('an unresolved session in the cycle source\'s checkout is previewed, not frozen', async () => {+ await fixture(async ({ engine, cycle, corpus }) => {+ const cwd = join(cycle.root, 'task');+ mkdirSync(cwd);+ expect((await openSessionSource(corpus, 'sess-preview', { cwd, harness: 'test' })).stamp).toBe(CORPUS_UNRESOLVED_STAMP);+ writeTranscript(join(ensureCorpusSpoolDir(corpus), sessionCorpusFileName('sess-preview', CORPUS_UNRESOLVED_STAMP)), 'the hiring loop');+ const result = await runPhaseSynthesize(engine, { brainDir: cycle.root, sourceId: cycle.id, dryRun: true });+ expect(result.details.source_routing).toBeUndefined();+ expect(readSessionSource(corpusSpoolDir(corpus), 'sess-preview')).toMatchObject({ source_id: null, tier: null, source_incarnation: null });+ expect(await reflections(engine)).toEqual([]);+ });+ }, 180_000);+});diff --git a/test/synthesize-source-routing-realpath-7871.test.ts b/test/synthesize-source-routing-realpath-7871.test.tsnew file mode 100644index 0000000000..4af2dc4b50--- /dev/null+++ b/test/synthesize-source-routing-realpath-7871.test.ts@@ -0,0 +1,142 @@+/**+ * #7871: a corpus file is classified by its real path as well as its spelled+ * path. Transcript listing follows symlinks, so a meeting dir (or an --input+ * path) that aliases the session corpus, a corpus dir configured through a+ * symlink, or a spool dir linked outside the corpus all name corpus files by a+ * path outside the corpus. Before the fix `corpusLocation` compared+ * `resolve()` paths only, so those files passed as non-corpus transcripts+ * and were synthesized (or atom-extracted) into the cycle's source. A file+ * whose real path cannot be read is held, never passed through.+ *+ * A multi-source brain with repo-less directories; PGLite and, with+ * DATABASE_URL, Postgres.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { randomUUID } from 'node:crypto';+import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { basename, join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { CORPUS_SPOOL_SUBDIR, sessionCorpusFileName } from '../src/core/context/corpus-segments.ts';+import { emptySessionSourceRouting, keepSessionSourceTranscripts } from '../src/core/cycle/synthesize-source-routing.ts';+import type { DiscoveredTranscript } from '../src/core/cycle/transcript-discovery.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { testBackends } from './helpers/test-backends.ts';++const backends = testBackends();+const engines: BrainEngine[] = [];+let closePostgres: (() => Promise<void>) | undefined;+beforeAll(async () => {+ if (backends.includes('pglite')) {+ const engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+ engines.push(engine);+ }+ if (backends.includes('postgres')) {+ const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!);+ engines.push(pg.engine);+ closePostgres = pg.close;+ }+}, 120_000);+afterAll(async () => {+ for (const engine of engines) { await disposePersistenceConsumer(engine); await engine.disconnect(); }+ await closePostgres?.();+});++const transcript = (filePath: string): DiscoveredTranscript =>+ ({ filePath, content: 'x', contentHash: 'h', basename: basename(filePath, '.txt'), inferredDate: null });++async function route(engine: BrainEngine, files: string[], corpusDir: string, sourceId: string) {+ const routing = emptySessionSourceRouting();+ const kept = await keepSessionSourceTranscripts(engine, files.map(transcript), { corpusDir, sourceId }, routing);+ return { kept: kept.map(t => t.filePath), routing };+}++async function brain(run: (engine: BrainEngine, dir: string, other: string) => Promise<void>): Promise<void> {+ for (const engine of engines) {+ const dir = mkdtempSync(join(tmpdir(), 'gbrain-corpus-realpath-'));+ const other = `source-b-${randomUUID().slice(0, 8)}`;+ try {+ await engine.executeRaw('INSERT INTO sources(id,name) VALUES($1,$1)', [other]);+ await run(engine, dir, other);+ } finally {+ await engine.executeRaw('DELETE FROM sources WHERE id = $1', [other]);+ rmSync(dir, { recursive: true, force: true });+ }+ }+}++describe('#7871 corpus files are recognized through symlinks', () => {+ test('a meeting dir that aliases the corpus: its legacy file is held and its stamped file goes to its own source', async () => {+ await brain(async (engine, dir, other) => {+ const corpus = join(dir, 'corpus');+ mkdirSync(join(corpus, CORPUS_SPOOL_SUBDIR), { recursive: true });+ writeFileSync(join(corpus, 'sess-legacy.txt'), 'x');+ const stamped = sessionCorpusFileName('sess-stamped', other);+ writeFileSync(join(corpus, CORPUS_SPOOL_SUBDIR, stamped), 'x');+ const meetings = join(dir, 'meetings');+ symlinkSync(corpus, meetings);+ const { kept, routing } = await route(engine, [join(meetings, 'sess-legacy.txt'), join(meetings, CORPUS_SPOOL_SUBDIR, stamped)], corpus, 'default');+ expect(kept).toEqual([]);+ expect(routing).toEqual({ corpus_source_other: { [other]: 1 }, held: { corpus_legacy_unstamped: 1 } });+ });+ }, 60_000);++ test('a corpus dir configured through a symlink still owns the files listed by their real path', async () => {+ await brain(async (engine, dir, other) => {+ const real = join(dir, 'corpus-real');+ mkdirSync(join(real, CORPUS_SPOOL_SUBDIR), { recursive: true });+ const stamped = sessionCorpusFileName('sess-stamped', other);+ writeFileSync(join(real, CORPUS_SPOOL_SUBDIR, stamped), 'x');+ const link = join(dir, 'corpus');+ symlinkSync(real, link);+ const { kept, routing } = await route(engine, [join(real, CORPUS_SPOOL_SUBDIR, stamped)], link, 'default');+ expect(kept).toEqual([]);+ expect(routing.corpus_source_other).toEqual({ [other]: 1 });+ });+ }, 60_000);++ test('a spool dir linked outside the corpus still classifies its files as spool files', async () => {+ await brain(async (engine, dir, other) => {+ const corpus = join(dir, 'corpus');+ const elsewhere = join(dir, 'spool-elsewhere');+ mkdirSync(corpus);+ mkdirSync(elsewhere);+ symlinkSync(elsewhere, join(corpus, CORPUS_SPOOL_SUBDIR));+ const stamped = sessionCorpusFileName('sess-stamped', other);+ writeFileSync(join(elsewhere, stamped), 'x');+ const { kept, routing } = await route(engine, [join(elsewhere, stamped)], corpus, 'default');+ expect(kept).toEqual([]);+ expect(routing.corpus_source_other).toEqual({ [other]: 1 });+ });+ }, 60_000);++ test('a corpus entry that links to a file outside stays a corpus file; a real outside file still passes', async () => {+ await brain(async (engine, dir) => {+ const corpus = join(dir, 'corpus');+ mkdirSync(corpus);+ const outside = join(dir, 'notes.txt');+ writeFileSync(outside, 'x');+ symlinkSync(outside, join(corpus, 'sess-link.txt'));+ const { kept, routing } = await route(engine, [join(corpus, 'sess-link.txt'), outside], corpus, 'default');+ expect(kept).toEqual([outside]);+ expect(routing.held).toEqual({ corpus_legacy_unstamped: 1 });+ });+ }, 60_000);++ test('a listed path whose real path cannot be read is held, never passed to the cycle source', async () => {+ await brain(async (engine, dir) => {+ const corpus = join(dir, 'corpus');+ mkdirSync(corpus);+ const dangling = join(dir, 'meetings', 'gone.txt');+ mkdirSync(join(dir, 'meetings'));+ symlinkSync(join(dir, 'nowhere.txt'), dangling);+ const { kept, routing } = await route(engine, [dangling], corpus, 'default');+ expect(kept).toEqual([]);+ expect(routing.held).toEqual({ corpus_path_unresolvable: 1 });+ });+ }, 60_000);+});