fix: wait for background shutdown notifications

This commit is contained in:
liruifengv 2026-05-25 18:29:39 +08:00
parent ee261d9af8
commit cf98059b8d
3 changed files with 19 additions and 5 deletions

View file

@ -3,4 +3,4 @@
"@moonshot-ai/kimi-code": patch
---
Persist model selections from the terminal UI to the default configuration, and honor the configured default thinking state for new sessions.
Persist model selections from the terminal UI to the default configuration, honor the configured default thinking state for new sessions, and wait for background task terminal notifications during session shutdown before flushing records.

View file

@ -35,6 +35,7 @@ const NOTIFICATION_TAIL_BYTES = 3_000;
export class BackgroundManager extends BackgroundProcessManager {
private readonly scheduledNotificationKeys = new Set<string>();
private readonly deliveredNotificationKeys = new Set<string>();
private readonly terminalNotificationPromises = new Set<Promise<void>>();
constructor(
public readonly agent: Agent,
@ -85,7 +86,18 @@ export class BackgroundManager extends BackgroundProcessManager {
}
protected override onLiveTaskTerminal(info: BackgroundTaskInfo): void | Promise<void> {
return this.notifyBackgroundTask(info);
const promise = this.notifyBackgroundTask(info).catch(() => {});
this.terminalNotificationPromises.add(promise);
void promise.finally(() => {
this.terminalNotificationPromises.delete(promise);
});
return promise;
}
async settleTerminalNotifications(): Promise<void> {
while (this.terminalNotificationPromises.size > 0) {
await Promise.all(Array.from(this.terminalNotificationPromises));
}
}
private async restoreBackgroundTaskNotifications(): Promise<void> {
@ -183,6 +195,7 @@ export class BackgroundManager extends BackgroundProcessManager {
super._reset();
this.scheduledNotificationKeys.clear();
this.deliveredNotificationKeys.clear();
this.terminalNotificationPromises.clear();
}
}

View file

@ -194,9 +194,10 @@ export class Session {
});
if (keepAliveOnExit) return;
await Promise.all(
Array.from(this.agents.values(), (agent) =>
agent.background.stopAll('Session closed'),
),
Array.from(this.agents.values(), async (agent) => {
await agent.background.stopAll('Session closed');
await agent.background.settleTerminalNotifications();
}),
);
}