@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
80 lines • 2.7 kB
JavaScript
import { LOSSY_FRAME_TYPES, TOOL_CHILD_LIMITS } from "./protocol.js";
export class WakeAdmissionError extends Error {
constructor(bytes) {
super(`wake_too_large: ${bytes} bytes exceeds the ${TOOL_CHILD_LIMITS.wakePendingBytes}-byte pending budget`);
this.code = "wake_too_large";
this.name = "WakeAdmissionError";
}
}
/**
* Wake notifications awaiting `wake_ack`, bounded by count and summed bytes. A wake past either bound waits, FIFO,
* until an ack frees room: wakes are durable wake-source demand and are never dropped.
*/
export class WakeAdmission {
#pending = new Map();
#blocked = [];
#pendingBytes = 0;
get pending() {
return this.#pending.size;
}
get blocked() {
return this.#blocked.length;
}
send(wakeId, bytes) {
if (bytes > TOOL_CHILD_LIMITS.wakePendingBytes)
return Promise.reject(new WakeAdmissionError(bytes));
if (this.#blocked.length === 0 && this.#fits(bytes)) {
this.#admit(wakeId, bytes);
return Promise.resolve();
}
return new Promise((release) => {
this.#blocked.push({ wakeId, bytes, release });
});
}
ack(wakeId) {
const bytes = this.#pending.get(wakeId);
if (bytes === undefined)
return;
this.#pending.delete(wakeId);
this.#pendingBytes -= bytes;
for (let next = this.#blocked[0]; next !== undefined && this.#fits(next.bytes); next = this.#blocked[0]) {
this.#blocked.shift();
this.#admit(next.wakeId, next.bytes);
next.release();
}
}
#fits(bytes) {
return (this.#pending.size < TOOL_CHILD_LIMITS.wakePending &&
this.#pendingBytes + bytes <= TOOL_CHILD_LIMITS.wakePendingBytes);
}
#admit(wakeId, bytes) {
this.#pending.set(wakeId, bytes);
this.#pendingBytes += bytes;
}
}
/** Per-second budget for `trace` frames, the only lossy class: an over-budget frame is dropped and counted. */
export class TraceBudget {
#second = Number.NaN;
#admitted = 0;
#dropped = 0;
get dropped() {
return this.#dropped;
}
admit(_frame, nowMs) {
const second = Math.floor(nowMs / 1000);
if (second !== this.#second) {
this.#second = second;
this.#admitted = 0;
}
if (this.#admitted >= TOOL_CHILD_LIMITS.tracePerSecond) {
this.#dropped += 1;
return false;
}
this.#admitted += 1;
return true;
}
}
export function isLossy(message) {
return LOSSY_FRAME_TYPES.has(message.type);
}
//# sourceMappingURL=flow-control.js.map