@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
101 lines • 4.93 kB
JavaScript
import { acquireFileMutationLock } from "./tools/file-mutation-queue.js";
import { canonicalizeFilesystemPath } from "./tools/filesystem-policy.js";
import { resolveToCwd } from "./tools/path-utils.js";
export class HostCallError extends Error {
constructor(code, message) {
super(`${code}: ${message}`);
this.name = "HostCallError";
this.code = code;
}
}
const ALLOW = { allow: true };
export function createSessionHostCallHandler(deps) {
const locks = new Map();
let nextLock = 0;
const releaseLock = (lockId) => {
const release = locks.get(lockId);
if (release === undefined)
return false;
locks.delete(lockId);
release();
return true;
};
return async (request) => {
// A call of an ended job never runs (it could still spend tokens); only releasing a lock it holds is allowed.
if (request.kind !== "unlock_file" && !request.generation.jobLive())
throw new HostCallError("host_call_job_ended", `${request.kind}: the job has ended`);
switch (request.kind) {
case "tool": {
const parent = callingToolCallId(request.context);
if (parent === undefined)
throw new HostCallError("host_call_no_caller", "the job runs for no tool call");
if (request.name === undefined)
throw new HostCallError("host_call_no_name", "a tool call names no tool");
const outcome = await deps.executeNested(parent, request.name, request.args, request.signal);
return { result: outcome.result, isError: outcome.isError };
}
case "list_tools":
return [...deps.callableToolNames()];
case "completion":
case "reserved": {
const target = deps.target(request.kind);
if (target === undefined)
throw new HostCallError("host_call_unsupported_kind", `no ${request.kind} target is registered on this session`);
return target({ name: request.name, args: request.args, signal: request.signal });
}
case "authorize": {
const policyArgs = await policyRequest(request.args, deps.cwd);
const policy = deps.filesystemPolicy();
return policy === undefined ? ALLOW : policy(policyArgs);
}
case "lock_file": {
const path = stringField(request.args, "path");
const release = await acquireFileMutationLock(resolveToCwd(path, deps.cwd), request.signal);
// Granted only to a live job: one that ended while it waited never holds the slot.
if (!request.generation.jobLive() || request.signal.aborted) {
release();
throw new HostCallError("host_call_job_ended", `lock on ${path}: the job ended while it waited`);
}
const lockId = `L${++nextLock}`;
locks.set(lockId, release);
void request.generation.exited.then(() => releaseLock(lockId));
void request.generation.stuck?.then((holder) => {
if (locks.get(lockId) === release)
release.markStuck(holder);
});
return { lockId };
}
case "unlock_file": {
const lockId = stringField(request.args, "lockId");
if (!releaseLock(lockId))
throw new HostCallError("host_call_unknown_lock", `no held lock ${lockId}`);
return null;
}
}
};
}
function stringField(args, field) {
const value = typeof args === "object" && args !== null ? Reflect.get(args, field) : undefined;
if (typeof value !== "string" || value === "")
throw new HostCallError("host_call_bad_args", `${field} must be a non-empty string`);
return value;
}
const OPERATIONS = new Set(["read", "enumerate", "write"]);
/** The server canonicalizes the child's path the way the built-in file tools do; a child-sent canonical path is never trusted. */
async function policyRequest(args, cwd) {
const operation = stringField(args, "operation");
if (!OPERATIONS.has(operation))
throw new HostCallError("host_call_bad_args", `operation must be read, enumerate or write`);
return {
operation: operation,
canonicalPath: await canonicalizeFilesystemPath(resolveToCwd(stringField(args, "path"), cwd)),
toolName: stringField(args, "toolName"),
};
}
function callingToolCallId(context) {
if (typeof context !== "object" || context === null)
return undefined;
const toolCallId = Reflect.get(context, "toolCallId");
return typeof toolCallId === "string" ? toolCallId : undefined;
}
//# sourceMappingURL=tool-child-host-calls.js.map