mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
refactor(agent): extract memory fork source validation
This commit is contained in:
parent
b863f1cff6
commit
4f069996d4
1 changed files with 34 additions and 23 deletions
|
|
@ -444,29 +444,7 @@ export class MemorySessionRepo implements SessionRepo {
|
|||
snapshot.registers.map((register) => [registerKey(register.namespace, register.key), register]),
|
||||
);
|
||||
const sourceLeaves = snapshot.registers.filter((register) => register.namespace === "lane.leaf");
|
||||
const sourceLeafKeys = new Set(sourceLeaves.map((register) => register.key));
|
||||
if (!sourceLeafKeys.has("main")) throw new Error("Source session is missing main lane");
|
||||
for (const register of snapshot.registers) {
|
||||
if (
|
||||
(register.namespace === "lane.config" ||
|
||||
register.namespace === "lane.state" ||
|
||||
register.namespace === "lane.lastResult") &&
|
||||
!sourceLeafKeys.has(register.key)
|
||||
) {
|
||||
throw new Error(`Source session lane ${JSON.stringify(register.key)} is missing lane.leaf`);
|
||||
}
|
||||
}
|
||||
for (const leaf of sourceLeaves) {
|
||||
if (!sourceRegisters.has(registerKey("lane.state", leaf.key))) {
|
||||
throw new Error(`Source session lane ${JSON.stringify(leaf.key)} is missing lane.state`);
|
||||
}
|
||||
if (leaf.key !== "main" && !sourceRegisters.has(registerKey("lane.config", leaf.key))) {
|
||||
throw new Error(`Source session lane ${JSON.stringify(leaf.key)} is missing lane.config`);
|
||||
}
|
||||
if (leaf.value !== null && !sourceEntries.has(leaf.value as string)) {
|
||||
throw new Error(`Source session lane ${JSON.stringify(leaf.key)} has an unknown leaf`);
|
||||
}
|
||||
}
|
||||
this.validateForkSourceSnapshot(snapshot, sourceEntries, sourceRegisters, sourceLeaves);
|
||||
|
||||
const copiedEntryIds = new Set<string>();
|
||||
const destinationLeaves = new Map<string, string | null>();
|
||||
|
|
@ -530,6 +508,39 @@ export class MemorySessionRepo implements SessionRepo {
|
|||
);
|
||||
}
|
||||
|
||||
private validateForkSourceSnapshot(
|
||||
snapshot: { entries: Entry[]; registers: Register[] },
|
||||
sourceEntries: Map<string, Entry>,
|
||||
sourceRegisters: Map<string, Register>,
|
||||
sourceLeaves: Register[],
|
||||
): void {
|
||||
const sourceLeafKeys = new Set(sourceLeaves.map((register) => register.key));
|
||||
|
||||
// TODO: do all these validations need to happen here? maybe somewhere else when the source session is loaded?
|
||||
if (!sourceLeafKeys.has("main")) throw new Error("Source session is missing main lane");
|
||||
for (const register of snapshot.registers) {
|
||||
if (
|
||||
(register.namespace === "lane.config" ||
|
||||
register.namespace === "lane.state" ||
|
||||
register.namespace === "lane.lastResult") &&
|
||||
!sourceLeafKeys.has(register.key)
|
||||
) {
|
||||
throw new Error(`Source session lane ${JSON.stringify(register.key)} is missing lane.leaf`);
|
||||
}
|
||||
}
|
||||
for (const leaf of sourceLeaves) {
|
||||
if (!sourceRegisters.has(registerKey("lane.state", leaf.key))) {
|
||||
throw new Error(`Source session lane ${JSON.stringify(leaf.key)} is missing lane.state`);
|
||||
}
|
||||
if (leaf.key !== "main" && !sourceRegisters.has(registerKey("lane.config", leaf.key))) {
|
||||
throw new Error(`Source session lane ${JSON.stringify(leaf.key)} is missing lane.config`);
|
||||
}
|
||||
if (leaf.value !== null && !sourceEntries.has(leaf.value as string)) {
|
||||
throw new Error(`Source session lane ${JSON.stringify(leaf.key)} has an unknown leaf`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private reserveId(id: string): void {
|
||||
if (this.sessions.has(id) || this.pendingIds.has(id)) throw new Error(`Session already exists: ${id}`);
|
||||
this.pendingIds.add(id);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue