fix(agent): validate fork entries against named branch ancestry

Reject off-branch and unknown fork points, handle null tips and before-root placement, and make SQLite validate from the named branch tip.
This commit is contained in:
David Brailovsky 2026-09-01 14:58:49 +02:00
parent 435b0c69f5
commit f2eae920c6
5 changed files with 100 additions and 66 deletions

View file

@ -114,8 +114,9 @@ function readForkSourceEntries(
(stored) => stored.address.namespace === sourceAddress.namespace && stored.address.key === sourceAddress.key,
) as StoredValue<string | null> | undefined;
if (sourceTip === undefined) throw new Error(`Unknown source branch: ${options.branch}`);
const requested = options.entryId ?? sourceTip.value;
return requested === null ? [] : scanBranchEntries(db, sessionId, { start: requested, order: "oldestFirst" });
return sourceTip.value === null
? []
: scanBranchEntries(db, sessionId, { start: sourceTip.value, order: "oldestFirst" });
}
function createSqliteForkSnapshot(

View file

@ -155,10 +155,9 @@ export class SqliteStorage implements Storage {
(stored) => stored.address.namespace === sourceAddress.namespace && stored.address.key === sourceAddress.key,
) as StoredValue<string | null> | undefined;
if (sourceTip === undefined) throw new Error(`Unknown source branch: ${options.branch}`);
const requested = options.entryId ?? sourceTip.value;
return requested === null
return sourceTip.value === null
? []
: scanBranchEntries(this.db, this.sessionId, { start: requested, order: "oldestFirst" });
: scanBranchEntries(this.db, this.sessionId, { start: sourceTip.value, order: "oldestFirst" });
}
private applyCommit(writes: Write[]): CommitResult {