UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

266 lines • 12.4 kB
import { callLeaves, callOrder, terminal } from "./compact-call-model.js"; import { CompactComponentRoots } from "./compact-component-roots.js"; import { CompactValues } from "./compact-values.js"; const turnKey = (turnId, toolCallId) => `${turnId}\u0000${toolCallId}`; const messageKey = (messageId, contentIndex) => `${messageId}#${contentIndex}`; const sharedKey = (messageId, toolCallId) => `${messageId}\u0000${toolCallId}`; /** * The resident side of the calls component (S06 design section 2): the live entries, four indexes, the owned slot * values, and the persistent root every change republishes through the shared component roots. */ export class CompactCallTable { constructor(store) { this.root = { header: undefined, ref: undefined }; this.entries = new Map(); this.byTurn = new Map(); this.byMessage = new Map(); this.byId = new Map(); this.children = new Map(); this.shared = new Set(); this.slotBytes = 0; this.headerBytes = 0; this.counters = { replacements: 0, progressReplacements: 0, unknown: 0, late: 0, collisions: 0, reused: 0 }; this.slotBytesOf = new WeakMap(); this.store = store; this.values = new CompactValues(store); this.roots = new CompactComponentRoots(store, callLeaves); } get(key) { return this.entries.get(key)?.call; } byTurnId(turnId, toolCallId) { if (turnId === null || !toolCallId) return undefined; const key = this.byTurn.get(turnKey(turnId, toolCallId)); return key === undefined ? undefined : this.get(key); } byOwner(messageId, contentIndex) { const key = this.byMessage.get(messageKey(messageId, contentIndex)); return key === undefined ? undefined : this.get(key); } /** The newest call with this id that is non-terminal or holds a synthetic outcome (design section 3). */ newestById(toolCallId) { const keys = this.byId.get(toolCallId) ?? []; for (let index = keys.length - 1; index >= 0; index--) { const call = this.get(keys[index]); if (call && (!terminal(call) || call.outcome?.kind === "abandoned")) return call; } return undefined; } /** * The oldest `open` call with this id, its own turn first (FIFO by creation, senpi#4278): when a reused id overlaps * a call still executing, each execution end closes, and each carrier settles, the call it belongs to. */ oldestById(toolCallId, turnId, open) { const rank = (entry) => `${entry.call.turnId === turnId ? 0 : 1}${entry.order}`; let oldest; for (const key of this.byId.get(toolCallId) ?? []) { const entry = this.entries.get(key); if (entry && open(entry.call) && (!oldest || rank(entry) < rank(oldest))) oldest = entry; } return oldest?.call; } /** * The oldest call of `turnId` started under `id` before any block joined it (a bridge-started call, RULING-127 (b)): * a block joins it and keeps its first key; oldest first, the order the producer assigns their keys to blocks. */ bridgeStarted(turnId, id) { if (turnId === null || id === null) return undefined; const unowned = (call) => call.turnId === turnId && call.owningMessageId === null && call.parentKey === null; return this.oldestById(id, turnId, unowned); } /** * RULING-123, C3.4: whether another call of `call`'s own message took the same toolCallId. An execution or hook * record names only the toolCallId, so it cannot say which of those calls it is about. The mark lasts while any call * of that message and id is live, so a later record is not attributed by elimination either. */ sharesIdInMessage(call) { const { owningMessageId, toolCallId } = call; return owningMessageId !== null && toolCallId !== null && this.shared.has(sharedKey(owningMessageId, toolCallId)); } *ofTurn(turnId) { for (const { call } of this.entries.values()) if (call.turnId === turnId) yield call; } *all() { for (const { call } of this.entries.values()) yield call; } childrenOf(parentKey) { const keys = [...(this.children.get(parentKey) ?? [])]; return keys.flatMap((key) => this.get(key) ?? []); } /** Measured before owning: a value the engine cannot serialize (too deep) throws with nothing owned yet (RULING-59). */ own(value) { const bytes = Buffer.byteLength(JSON.stringify(value) ?? ""); return { ref: this.values.own(value), bytes }; } /** X07 `slotBytes` counts one copy per slot: the replaced value's bytes leave with it. */ replaceSlot(call, field, value) { const slot = this.own(value); const previous = call[field]; const next = { ...call, [field]: slot.ref }; this.slotBytes += slot.bytes - (previous ? (this.slotBytesOf.get(previous) ?? 0) : 0); this.slotBytesOf.set(slot.ref, slot.bytes); if (previous) this.slotBytesOf.delete(previous); this.reput(next); this.store.adjust(slot.ref.owner, -1, 0); return next; } /** * Publishes a created or changed call at its creation ordinal and refreshes every index it is reachable by. The * stored header carries `callUnknown: true` exactly while `sharesIdInMessage` holds for it (#5348), so the snapshot * row, the in-process view and the settled projection all state the mark this table keeps; the first call to share * an id with another of its message republishes the others marked. */ put(input, creation) { this.putAt(input, callOrder(creation.sequence, creation.k)); } /** A changed call keeps the creation ordinal its key was put at (RULING-13 Q1), whatever minted that key. */ reput(input) { const entry = this.entries.get(input.runtimeItemKey); if (!entry) throw new Error(`unknown call ${input.runtimeItemKey}`); this.putAt(input, entry.order); } putAt(input, order) { const call = marked(input, this.willShare(input)); // Measured before anything changes, so a header the engine cannot serialize leaves the table as it was. const bytes = headerBytes(call); const previous = this.entries.get(call.runtimeItemKey)?.call; if (previous) this.unindex(previous); this.root = this.roots.set(this.root, order, call); this.counters.replacements++; this.headerBytes += bytes - (previous ? headerBytes(previous) : 0); this.entries.set(call.runtimeItemKey, { call, order }); this.index(call); if (call.callUnknown && call.owningMessageId !== null && call.toolCallId !== null) for (const peer of this.inMessage(call.owningMessageId, call.toolCallId).map((key) => this.get(key))) if (peer && !peer.callUnknown) this.reput(peer); } /** Whether `call`, once put, shares its toolCallId inside its message: the mark is set, or another live call has it. */ willShare(call) { const { owningMessageId, toolCallId, runtimeItemKey } = call; if (owningMessageId === null || !toolCallId) return false; if (this.shared.has(sharedKey(owningMessageId, toolCallId))) return true; return this.inMessage(owningMessageId, toolCallId).some((key) => key !== runtimeItemKey); } remove(key) { const entry = this.entries.get(key); if (!entry) return; this.unindex(entry.call); for (const slot of [entry.call.hook, entry.call.progress, entry.call.result]) if (slot) { this.slotBytes -= this.slotBytesOf.get(slot) ?? 0; this.slotBytesOf.delete(slot); } this.headerBytes -= headerBytes(entry.call); this.entries.delete(key); const { owningMessageId, toolCallId } = entry.call; if (owningMessageId !== null && toolCallId && !this.inMessage(owningMessageId, toolCallId).length) this.shared.delete(sharedKey(owningMessageId, toolCallId)); this.root = this.roots.set(this.root, entry.order, undefined); this.counters.replacements++; } /** Whether another live call already owns this (turn, id) slot: a collision leaves the index unchanged. */ indexedTo(turnId, toolCallId) { return this.byTurn.get(turnKey(turnId, toolCallId)); } /** A reused id (senpi#4278) points the (turn, id) slot at the fresh call. */ repoint(turnId, toolCallId, key) { this.byTurn.set(turnKey(turnId, toolCallId), key); } snapshot() { let carrierless = 0; for (const { call } of this.entries.values()) if (call.join === "carrierless") carrierless++; return { live: this.entries.size, carrierless, replacements: this.counters.replacements, headerBytes: this.headerBytes, slotBytes: this.slotBytes, progressReplacements: this.counters.progressReplacements, ignored: { unknown: this.counters.unknown, late: this.counters.late }, collisions: this.counters.collisions, reused: this.counters.reused, }; } freeze() { if (this.root.ref) this.store.adjust(this.root.ref.owner, 0, 1); return this.root.ref; } release(ref) { if (ref) this.store.adjust(ref.owner, 0, -1); } entriesOf(ref, valid) { return this.roots.entries(ref, valid); } index(call) { if (call.toolCallId) { const slot = turnKey(call.turnId, call.toolCallId); if (!this.byTurn.has(slot) || this.byTurn.get(slot) === call.runtimeItemKey) this.byTurn.set(slot, call.runtimeItemKey); const keys = this.byId.get(call.toolCallId) ?? []; if (!keys.includes(call.runtimeItemKey)) this.byId.set(call.toolCallId, [...keys, call.runtimeItemKey]); if (call.owningMessageId !== null && call.callUnknown) this.shared.add(sharedKey(call.owningMessageId, call.toolCallId)); } if (call.owningMessageId !== null && call.contentIndex !== null) this.byMessage.set(messageKey(call.owningMessageId, call.contentIndex), call.runtimeItemKey); if (call.parentKey !== null) { const siblings = this.children.get(call.parentKey) ?? new Set(); siblings.add(call.runtimeItemKey); this.children.set(call.parentKey, siblings); } } /** The live calls of one message under one toolCallId. */ inMessage(messageId, toolCallId) { return (this.byId.get(toolCallId) ?? []).filter((key) => this.get(key)?.owningMessageId === messageId); } unindex(call) { if (call.toolCallId) { const slot = turnKey(call.turnId, call.toolCallId); if (this.byTurn.get(slot) === call.runtimeItemKey) this.byTurn.delete(slot); const keys = (this.byId.get(call.toolCallId) ?? []).filter((key) => key !== call.runtimeItemKey); if (keys.length) this.byId.set(call.toolCallId, keys); else this.byId.delete(call.toolCallId); } if (call.owningMessageId !== null && call.contentIndex !== null) { const slot = messageKey(call.owningMessageId, call.contentIndex); if (this.byMessage.get(slot) === call.runtimeItemKey) this.byMessage.delete(slot); } if (call.parentKey !== null) { const siblings = this.children.get(call.parentKey); siblings?.delete(call.runtimeItemKey); if (siblings?.size === 0) this.children.delete(call.parentKey); } } } function marked(call, ambiguous) { const { callUnknown: _mark, ...header } = call; return ambiguous ? { ...header, callUnknown: true } : header; } function headerBytes(call) { return Buffer.byteLength(JSON.stringify(call)); } //# sourceMappingURL=compact-call-table.js.map