@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
49 lines • 2.79 kB
JavaScript
/**
* The replacement a multi-session entry routes `switchSession` through: serialized on the entry's
* lifecycle mutex, and - when the replacement moved cwd - re-wrapped so every attached client keeps
* resolving against the entry's CURRENT runtime rather than the one built at `open_session`.
*/
import { AgentSessionRuntime, holdRuntimeSession } from "../../core/agent-session-runtime.js";
import { ClientAdmissions } from "./client-admissions.js";
import { EffectLedger } from "./effect-ledger.js";
import { RpcSessionRegistryError } from "./session-registry-types.js";
export function createEntrySwitchSession(entry, createRuntime, syncRuntimeMetadata) {
return (sessionPath, options) => {
const operation = entry.lifecycleMutex.then(async () => {
if (entry.state !== "open" || !entry.runtime) {
throw new RpcSessionRegistryError("unknown_session");
}
const runtime = entry.runtime;
const cwdOverride = options?.cwdOverride;
const cwdChanged = cwdOverride !== undefined && runtime.session.sessionManager.getCwd() !== cwdOverride;
const result = await runtime.switchSession(sessionPath, options);
if (result.cancelled || !cwdChanged)
return result;
// A multi-session binding outlives an individual replacement. Keep the
// entry's runtime object aligned with the replacement so every attached
// client resolves getters and future commands against the new cwd-bound
// runtime, not the object created during open_session.
// The replacement's hold is taken BEFORE the old runtime's is released: both name the same session file, so
// the shared holder record never disappears between the two.
const hold = await holdRuntimeSession(runtime.session.sessionManager, runtime.launchProfile?.holderEndpoint);
let replacement;
try {
replacement = new AgentSessionRuntime(runtime.session, runtime.services, createRuntime, [...runtime.diagnostics], runtime.modelFallbackMessage, runtime.launchProfile, hold);
replacement.setRebindSession(entry.rebindSession);
ClientAdmissions.sealQueuedOnReplace(replacement);
EffectLedger.sealOnReplace(replacement);
}
catch (error) {
hold?.release();
throw error;
}
runtime.releaseSessionHold();
entry.runtime = replacement;
syncRuntimeMetadata();
return result;
});
entry.lifecycleMutex = operation.then(() => undefined, () => undefined);
return operation;
};
}
//# sourceMappingURL=session-registry-switch.js.map