@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
81 lines • 3.34 kB
JavaScript
import { TOOL_CHILD_LIMITS } from "./protocol.js";
/**
* The one host-call capacity both sides enforce: `HOST_CALL_ACTIVE` running plus `HOST_CALL_WAITING` queued (264).
* The child admits against it before sending, the server admits against it on receipt; there is no second constant.
*/
export function hostCallCapacity() {
return TOOL_CHILD_LIMITS.HOST_CALL_ACTIVE + TOOL_CHILD_LIMITS.HOST_CALL_WAITING;
}
/** Lossless host-call admission: a call is running, queued FIFO, or refused with a frame naming its `callId`. */
export class HostCallAdmission {
#active = new Set();
#waiting = [];
get active() {
return this.#active.size;
}
get waiting() {
return this.#waiting.length;
}
submit(callId) {
if (this.#active.has(callId) || this.#waiting.includes(callId))
return refuse(callId, "host_call_duplicate");
if (this.#active.size < TOOL_CHILD_LIMITS.HOST_CALL_ACTIVE && this.#waiting.length === 0) {
this.#active.add(callId);
return { state: "active" };
}
if (this.#active.size + this.#waiting.length >= hostCallCapacity())
return refuse(callId, "host_call_capacity");
this.#waiting.push(callId);
return { state: "waiting" };
}
/** Ends a call (or withdraws a queued one) and returns the call promoted into the freed active slot, if any. */
settle(callId) {
if (!this.#active.delete(callId)) {
const index = this.#waiting.indexOf(callId);
if (index >= 0)
this.#waiting.splice(index, 1);
return undefined;
}
const next = this.#waiting.shift();
if (next !== undefined)
this.#active.add(next);
return next;
}
}
function refuse(callId, code) {
return { state: "refused", frame: { type: "host_call_refused", callId, code } };
}
/**
* Per-child job budget. Ordinary jobs are capped at `TOOL_CHILD_LIMITS.jobs`. A nested job (started on behalf of a
* running job, e.g. a tool an eval cell calls) is admitted past that cap, so a full child cannot deadlock its own
* callers; nested jobs arrive through host calls and are bounded by `hostCallCapacity()`.
*/
export class JobAdmission {
#ordinary = new Set();
#nested = new Map();
admit(jobId, parentJobId) {
if (this.#ordinary.has(jobId) || this.#nested.has(jobId))
return refuseJob(jobId, "tool_child_job_duplicate");
if (parentJobId === undefined) {
if (this.#ordinary.size >= TOOL_CHILD_LIMITS.jobs)
return refuseJob(jobId, "tool_child_jobs_full");
this.#ordinary.add(jobId);
return { admitted: true };
}
if (!this.#ordinary.has(parentJobId) && !this.#nested.has(parentJobId)) {
return refuseJob(jobId, "tool_child_parent_unknown");
}
if (this.#nested.size >= hostCallCapacity())
return refuseJob(jobId, "tool_child_nested_full");
this.#nested.set(jobId, parentJobId);
return { admitted: true };
}
finish(jobId) {
this.#ordinary.delete(jobId);
this.#nested.delete(jobId);
}
}
function refuseJob(jobId, code) {
return { admitted: false, frame: { type: "job_refused", jobId, code } };
}
//# sourceMappingURL=admission.js.map