@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
46 lines • 1.65 kB
JavaScript
/** Only sessions for which the normal registry actually found a standing foreign claim get a route. */
export class RouteTable {
constructor(host) {
this.byPath = new Map();
this.byId = new Map();
this.host = host;
}
holds(route) {
return this.byId.get(route.id) === route;
}
route(path, endpoint) {
const existing = this.byPath.get(path);
if (existing)
return existing;
const id = this.host.registry.reserveHandle?.();
if (!id)
throw new Error("registry_does_not_support_generation_routes");
const route = {
path,
id,
endpoint,
attachments: new Map(),
switching: false,
local: Promise.withResolvers(),
};
// A switch nobody waits on may still fail; its waiters each read the outcome themselves.
route.local.promise.catch(() => { });
this.byPath.set(path, route);
this.byId.set(route.id, route);
return route;
}
/** A forgotten route releases everything it owns: every attachment's peer is closed (senpi#7140). */
forget(route) {
this.byId.delete(route.id);
this.byPath.delete(route.path);
for (const caller of [...route.attachments.keys()])
this.dropPeer(route, caller);
route.local.resolve();
}
dropPeer(route, caller) {
const attachment = route.attachments.get(caller);
route.attachments.delete(caller);
void attachment?.peer.then((peer) => peer.close(), () => { });
}
}
//# sourceMappingURL=predecessor-route-table.js.map