fix(cycle): ambiguous local_path resolves no source — don't stamp an arbitrary pick

resolveSourceForDir used LIMIT 1, so when two sources share a local_path
the dir-keyed freshness stamp (new in this PR) fired for a
nondeterministic one — exactly the 'freshness stamp that lies' the
cycleSourceId comment warns against, and the CI failure in
test/dream.test.ts ('gbrain dream (no --source) leaves all sources
untouched'). Require exactly one match; ambiguous or no match falls back
to the pre-v0.18 behavior (undefined), same as before this PR for the
no-match case.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-07-22 11:06:41 -07:00
co-authored by Claude Fable 5
parent ded4aeaeae
commit 3fa01a4538
+6 -3
View File
@@ -854,7 +854,10 @@ interface SyncPhaseResult extends PhaseResult {
/**
* Resolve the source id for a brain directory by looking up the sources
* table. Returns undefined when no registered source matches (falls back
* to pre-v0.18 global config.sync.* keys).
* to pre-v0.18 global config.sync.* keys) OR when MORE than one source
* claims the path — an ambiguous match must not scope phases or stamp
* last_full_cycle_at for an arbitrarily-picked source (the "freshness
* stamp that lies" this resolution exists to prevent).
*/
async function resolveSourceForDir(
engine: BrainEngine,
@@ -865,10 +868,10 @@ async function resolveSourceForDir(
if (brainDir === null) return undefined;
try {
const rows = await engine.executeRaw<{ id: string }>(
`SELECT id FROM sources WHERE local_path = $1 LIMIT 1`,
`SELECT id FROM sources WHERE local_path = $1 LIMIT 2`,
[brainDir],
);
return rows[0]?.id;
return rows.length === 1 ? rows[0]!.id : undefined;
} catch {
// sources table might not exist on very old brains — fall through.
return undefined;