@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
200 lines • 10.6 kB
JavaScript
import { bindToProviderScope, runWithProviderScope } from "@earendil-works/pi-ai/node/provider-scope";
import { randomUUID } from "node:crypto";
import { carriesExtensionOwner } from "./extension-owner.js";
import { recoveryCarrier, setSessionRecoveryEpoch, } from "../../core/session-recovery-identity.js";
import { executionCallKey } from "../../core/session-call-keys.js";
import { CanonicalPairing, carryCanonical } from "./compact-canonical-pairing.js";
import { CompactProducerStamp } from "./compact-producer-stamp.js";
import { createRpcConnectionHandler, } from "./connection-handler.js";
import { enqueueLegacyRecord } from "./legacy-record-depth.js";
import { createToolAttributionSpans } from "./session-attribution.js";
import { structuredLaneEnabled } from "./structured-session-records.js";
/**
* Creates the extension UI bridge and subscriptions within the entry's provider
* scope. The classic handler remains the single source of command semantics.
*/
export async function createRpcSessionBinding(sessionId, entry, writer, requestClose, options = {}) {
if (entry.worker)
return entry.worker.bind(sessionId, writer, requestClose, options);
if (!entry.runtime)
throw new Error("Session runtime was not created");
let rpcBindingGeneration = randomUUID();
const rpcOwnersByConnection = new Map();
// An in-process session executes its tools ON the host loop, so the tool this
// session is inside is what the loop-lag watchdog blames for a stall. The record
// stream already carries that transition; no extra subscription is needed.
const toolSpans = createToolAttributionSpans(sessionId);
const lane = entry.acceptance;
if (lane)
setSessionRecoveryEpoch(entry.runtime.session, lane.recoveryEpoch);
if (lane)
writer.registerAcceptance(sessionId, lane);
// The manager whose journal the lane follows; a replacement's bootstrap moves the lane to the new one.
let journaled = lane ? entry.runtime.session.sessionManager : undefined;
// One acceptance unit per legacy record or block control (RULING-26 Q2); a worker frames it, in process the lane folds it.
const structured = structuredLaneEnabled();
const pairing = structured ? new CanonicalPairing() : undefined;
const stamp = new CompactProducerStamp();
const unit = (carrier, record, legacy) => {
const { ordinal, firstOrdinal } = stamp.stamp(carrier, record, legacy);
return carrier && record && firstOrdinal !== undefined ? { record, carrier, ordinal, firstOrdinal } : undefined;
};
const enqueueRecord = (record) => {
if (entry.speculative)
return;
toolSpans.observe(record);
const canonical = pairing?.take(record);
const refused = pairing?.refused(record) === true;
// Accepted before the writer serializes or routes it; a replacement is the next incarnation's bootstrap.
if (lane && "type" in record && record.type === "session_replaced") {
journaled = entry.runtime?.session.sessionManager;
lane.bootstrap({ transcript: journaled?.beginTranscriptJournal() ?? [] });
stamp.forget();
if (entry.runtime)
setSessionRecoveryEpoch(entry.runtime.session, lane.recoveryEpoch);
}
else if (lane) {
lane.applyTranscript(journaled?.drainTranscript() ?? []);
const carrier = recoveryCarrier(record);
lane.accept(record, carrier, unit(carrier, canonical, record), refused, executionCallKey(record));
}
else
carryCanonical(record, canonical, refused);
enqueueLegacyRecord(writer, sessionId, record);
};
// Attachments share one entry, so resolve the host from the live runtime. In
// particular, switch_session must use the entry's replacement-aware method
// instead of a runtime captured during open_session.
const runtimeHost = new Proxy({}, {
get(_target, property) {
if (property === "switchSession" && entry.switchSession)
return entry.switchSession;
if (property === "setRebindSession")
return (callback) => {
entry.rebindSession =
lane && callback
? async (session) => {
await lane.generations.bind(session.sessionId);
await callback(session);
}
: callback;
entry.runtime?.setRebindSession(entry.rebindSession);
};
const runtime = entry.runtime;
if (!runtime)
throw new Error("Session runtime was not created");
const value = Reflect.get(runtime, property, runtime);
return typeof value === "function" ? value.bind(runtime) : value;
},
});
// A reply can be written after its session closed: a prompt's preflight that outlived the client's
// deadline answers once the client already discarded the session. That record has no reader left, so it
// is dropped with one line naming the session instead of throwing "Provider scope is closed" out of
// an unawaited prompt (senpi#2871).
let droppedAfterClose = false;
const handler = await runWithProviderScope(entry.scope, async () => {
const writeInScope = bindToProviderScope(enqueueRecord);
const dropAfterClose = () => {
if (!droppedAfterClose)
process.stderr.write(`senpi rpc host: dropped a record for closed session ${sessionId} at ${new Date().toISOString()}\n`);
droppedAfterClose = true;
};
const writeRecord = (record) => {
if (entry.scope.state === "closed")
return dropAfterClose();
writeInScope(record);
};
const writeStructuredInScope = bindToProviderScope((record) => {
if (entry.speculative)
return;
writer.structuredLanes.accept(sessionId, record);
const control = pairing?.structured(record);
if (control && lane)
lane.acceptControl(unit(control.carrier, control.record));
else if (control)
writer.enqueueCanonical(sessionId, control);
});
const taggedSink = {
writeRecord,
...(structured
? {
writeStructured: (record) => {
if (entry.scope.state !== "closed")
writeStructuredInScope(record);
},
structuredFailed: (cause) => {
if (lane)
lane.compactUnavailable(cause);
else
writer.canonicalUnavailable(sessionId, cause);
},
}
: {}),
// A late raw reply after the scope closed is dropped like any record (senpi#2871); while the scope is
// open, records arrive only through writeRecord, so a raw write is a wiring bug.
writeRaw: () => {
if (entry.scope.state === "closed")
return dropAfterClose();
throw new Error("Shared session bindings deliver records through writeRecord, never writeRaw");
},
waitForBackpressure: bindToProviderScope(async () => { }),
};
return createRpcConnectionHandler(runtimeHost, taggedSink, {
sessionId,
shutdownHandler: bindToProviderScope(requestClose),
disposeRuntime: false,
rpcOwner: () => {
const owner = writer.connectionContext.owner;
const connection = writer.currentConnection();
if (owner && connection)
rpcOwnersByConnection.set(connection, owner);
return owner;
},
retireRpcBinding: () => {
rpcBindingGeneration = randomUUID();
rpcOwnersByConnection.clear();
},
publishLoadedSurfacesChanged: (previous, digest) => {
if (!entry.speculative && entry.scope.state !== "closed")
writer.publishLoadedSurfacesChanged(sessionId, previous, digest);
},
...options,
workerSecurityBridge: writer.workerSecurityBridge,
secretInputRoute: writer.secretInputRoute(sessionId, true),
grantDialogSettled: (id) => writer.settleGrantDialog(sessionId, id),
grantDialogRaised: (id, title) => writer.noticeGrantDialog(sessionId, id, title),
bindSecurityPresentation: (session, ui) => writer.bindSecurityPresentations(sessionId, session.sessionId, session.securityNotices, ui),
bindMcpForms: (session, ui) => writer.bindMcpForms(sessionId, session.sessionId, ui, () => session.admitHumanAnswer()),
clientInfo: options.clientInfo === undefined
? undefined
: {
...options.clientInfo,
hasCapability: (connection, capability) => writer.getConnectionCapabilities(connection)?.includes(capability) === true,
},
});
});
await handler.ready;
return {
setClientCapabilities: (capabilities) => runWithProviderScope(entry.scope, () => handler.setClientCapabilities(capabilities)),
handle: (command) => {
if (writer.handleMcpForm(command))
return Promise.resolve();
const invoke = () => runWithProviderScope(entry.scope, () => handler.handleInputLine(JSON.stringify(command)));
return carriesExtensionOwner(command)
? writer.connectionContext.withBinding(rpcBindingGeneration, invoke)
: invoke();
},
cancelPendingExtensionUiRequests: () => runWithProviderScope(entry.scope, () => handler.cancelPendingExtensionUiRequests()),
pendingPrompts: () => handler.pendingPrompts(),
connectionDetached: (connectionId) => {
rpcOwnersByConnection.get(connectionId)?.retire();
rpcOwnersByConnection.delete(connectionId);
runWithProviderScope(entry.scope, () => handler.connectionDetached(connectionId));
},
dispose: () => {
toolSpans.closeAll();
return runWithProviderScope(entry.scope, () => handler.dispose());
},
};
}
//# sourceMappingURL=session-binding.js.map