@sema-agent/core
Version:
Stateless, task-oriented AI agent core
44 lines (43 loc) • 1.43 kB
JavaScript
export class SessionReadFileStates {
seats = new Map();
static CAP = 1024;
get(sessionId) {
const seat = this.seats.get(sessionId);
if (seat !== undefined) {
this.seats.delete(sessionId);
this.seats.set(sessionId, seat);
}
return seat;
}
adopt(sessionId, state, anchorLeafId) {
this.seats.delete(sessionId);
this.seats.set(sessionId, { state, anchorLeafId });
while (this.seats.size > SessionReadFileStates.CAP) {
const oldest = this.seats.keys().next().value;
if (oldest === undefined)
break;
this.seats.delete(oldest);
}
}
reanchor(sessionId, state, anchorLeafId) {
const seat = this.seats.get(sessionId);
if (seat !== undefined && seat.state === state)
seat.anchorLeafId = anchorLeafId;
}
}
export async function sessionSeatAnchorHolds(getEntry, anchorLeafId, currentLeafId) {
if (anchorLeafId === null)
return false;
let id = currentLeafId;
for (let steps = 0; id !== null && steps < 100_000; steps++) {
if (id === anchorLeafId)
return true;
const entry = await getEntry(id);
if (entry === null || entry === undefined)
return false;
if (entry.type === "compaction")
return false;
id = entry.parentId;
}
return false;
}