UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

63 lines • 3.46 kB
/** * The "opening" entry an `openSession` records once its path is reserved and claimed. Built in one * synchronous step so the registry can give the reservation back if building it throws, instead of * leaving a reserved path no entry backs (senpi#3485). */ import { existsSync } from "node:fs"; import { ProviderScope } from "@earendil-works/pi-ai/node/provider-scope"; import { createEntrySwitchSession } from "./session-registry-switch.js"; import { frozenProfile, sessionIdentity, } from "./session-registry-types.js"; export function buildOpeningEntry(input) { const { profile, sessionPath, requestedDurableId, options } = input; // Resume vs create parity (D1 + omo SenpiSessionRuntime.ts:198-200): // Create-only launch semantics mirror classic startup flags. A resumed // session restores its persisted model and thinking level instead of being // overridden by the new open_session request. const isResume = sessionPath !== undefined && existsSync(sessionPath); // Re-opening an existing session file is a resume, exactly like interactive // /resume (AgentSessionRuntime.switchSession). Without the event the session // starts with reason "startup" and every extension that only rebuilds state // on a resume - the ask-user dangling-question hook - stays unreachable from // the RPC restart path. A session created by this open stays "startup". const sessionStartEvent = isResume || profile.attended === false || profile.continued === true ? { type: "session_start", reason: isResume ? "resume" : "startup", ...(profile.attended === false ? { attended: false } : {}), ...(profile.continued === true ? { continued: true } : {}), } : undefined; const storedProfile = frozenProfile({ ...profile, ...(sessionPath ? { sessionPath } : {}) }); const runtimeProfile = isResume ? frozenProfile({ ...storedProfile, creationModel: undefined, initialThinkingLevel: undefined }) : storedProfile; let settleOpen; const openCompletion = new Promise((resolve, reject) => { settleOpen = { resolve, reject }; }); // A concurrent explicit open for the same path waits on this to attach, never to time it out. openCompletion.catch(() => { }); const entry = { state: "opening", ...(options?.speculative ? { speculative: true } : {}), scope: new ProviderScope(), profile: storedProfile, ...sessionIdentity(storedProfile), sessionPath, reservationKey: sessionPath, cwd: storedProfile.cwd, attachments: options?.speculative || options?.unattached ? 0 : 1, retainOnDisconnect: options?.retainOnDisconnect === true, lastCommandAt: input.now, lifecycleMutex: Promise.resolve(), openCompletion, }; entry.switchSession = createEntrySwitchSession(entry, input.createRuntime, input.sync); // Recorded before the first await so the synchronous collision guard sees an open that is still // being built. `manager.getSessionId()` overwrites it later with the authoritative value, which on // a resume is the header's id, not the requested one. if (requestedDurableId !== undefined) entry.durableSessionId = requestedDurableId; return { entry, storedProfile, runtimeProfile, sessionStartEvent, settleOpen }; } //# sourceMappingURL=session-registry-opening.js.map