fix(pi): reopen persisted conversation sessions

This commit is contained in:
2026-08-25 08:46:55 +08:00
parent e74b59a674
commit 862d1b6d92
3 changed files with 148 additions and 8 deletions

View File

@@ -191,6 +191,7 @@ export function createPiManagedWorkerOpener(
return async (input) => {
const resourcesStartedAt = now();
const registered = await options.registry.prepare(input.conversation);
const existingSession = input.existingSession ?? registered.session ?? undefined;
const model = registered.conversation.model;
if (!model || registered.conversation.modelResolution !== 'resolved') {
throw new CodingRuntimeContractError(
@@ -238,11 +239,12 @@ export function createPiManagedWorkerOpener(
'resources.ready',
now() - resourcesStartedAt,
now(),
!existingSession,
);
const sessionKey = validateSessionKey(
input.existingSession?.sessionKey ?? createSessionKey(),
existingSession?.sessionKey ?? createSessionKey(),
);
if (input.existingSession && input.fork) {
if (existingSession && input.fork) {
throw new Error('Pi worker cannot reopen and fork a session at the same time');
}
const process = createProcess({
@@ -272,6 +274,7 @@ export function createPiManagedWorkerOpener(
'worker.spawn',
now() - spawnStartedAt,
now(),
!existingSession,
);
if (input.fork?.sourceEntryId) {
await process.request({ type: 'fork', entryId: input.fork.sourceEntryId });
@@ -287,10 +290,11 @@ export function createPiManagedWorkerOpener(
'rpc.ready',
now() - readyStartedAt,
now(),
!existingSession,
);
const piSessionId = response.data?.sessionId?.trim();
if (!piSessionId) throw new Error('Pi worker did not return a session id');
if (input.existingSession && input.existingSession.piSessionId !== piSessionId) {
if (existingSession && existingSession.piSessionId !== piSessionId) {
throw new CodingRuntimeContractError(
'CODING_SESSION_UNREADABLE',
'Pi reopened a different Conversation session',
@@ -327,6 +331,7 @@ export function createPiManagedWorkerOpener(
'session.open',
now() - sessionStartedAt,
now(),
!existingSession,
);
return {
worker: new ManagedPiConversationWorker(
@@ -356,12 +361,13 @@ function recordManagedMilestone(
milestone: Extract<PiRuntimeMilestone, 'resources.ready' | 'worker.spawn' | 'rpc.ready' | 'session.open'>,
durationMs: number,
at: number,
cold: boolean,
): void {
listener?.(createPiRuntimeTelemetryEvent({
milestone,
conversationId: input.conversation.conversationId,
workerGeneration: input.generation,
cold: !input.existingSession,
cold,
durationMs,
at,
}));