fix(pi): keep active runs alive during background sleep

This commit is contained in:
2026-08-25 20:56:17 +08:00
parent 92f4c91088
commit 621ebb1781
22 changed files with 1061 additions and 68 deletions

View File

@@ -635,21 +635,27 @@ export class PiWorkerPool {
return this.publicState(await record.rebuildFlight);
}
async dispose(conversationId: string): Promise<void> {
async dispose(
conversationId: string,
reason: PiWorkerStopReason,
canStop: () => boolean = () => true,
): Promise<boolean> {
const pendingPrepare = this.prepareFlights.get(conversationId);
if (pendingPrepare) await pendingPrepare.catch(() => undefined);
let record = this.workers.get(conversationId);
if (!record) return;
if (!record) return false;
if (record.rebuildFlight) {
record = await record.rebuildFlight.catch(() => record as WorkerRecord);
}
if (!canStop()) return false;
this.cancelConversationRuns(conversationId, new Error('Conversation worker was disposed'));
record.unsubscribeEvent();
record.unsubscribeInvalidation();
this.cancelGenerationResources(record);
this.revisions.removeWorker(record.revisionWorkerId);
if (this.workers.get(conversationId) === record) this.workers.delete(conversationId);
await this.ensureStoppedAndReleased(record, 'dispose');
await this.ensureStoppedAndReleased(record, reason);
return true;
}
private async performShutdown(): Promise<void> {