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

@@ -47,6 +47,7 @@ export interface CreateCodingCompositionOptions {
browser: AgentBrowserModule;
paths: CodingCompositionPaths;
getLocalProxyCredential?(): string | undefined;
acquireBackgroundLease?(lease: { id: string; kind: 'coding-run' }): () => void;
}
export function resolveCodingPiRuntimePaths(input: {
@@ -158,6 +159,9 @@ export function createCodingComposition(
mimeType: record.mime,
};
})),
...(options.acquireBackgroundLease
? { acquireBackgroundLease: options.acquireBackgroundLease }
: {}),
});
const projects = new CodingProjectService(projectStore, {
migration: {
@@ -174,13 +178,13 @@ export function createCodingComposition(
.catch(() => []);
for (const conversation of conversations) registry.forget(conversation.id);
},
onProjectDeactivated: async (project) => {
onProjectDeactivated: async (project, reason) => {
const conversations = await conversationStoreForProject(project.path).read()
.then((file) => file.conversations)
.catch(() => []);
await Promise.allSettled([
options.browser.close(project.path),
...conversations.map(({ id }) => runtime.dispose(id)),
...conversations.map(({ id }) => runtime.dispose(id, reason)),
]);
},
});
@@ -205,9 +209,12 @@ export function createCodingComposition(
conversations,
runtime,
host,
async sleep() {
async sleep(reason) {
if (reason === 'background_sleep' && runtime.hasActiveWork()) return;
const conversationIds = runtime.getDiagnostics().workers.map((worker) => worker.conversationId);
await Promise.allSettled(conversationIds.map((conversationId) => runtime.dispose(conversationId)));
await Promise.allSettled(conversationIds.map((conversationId) => (
runtime.dispose(conversationId, reason)
)));
},
async shutdown() {
await subagents.close();