fix(agent-core): suppress close-time background notifications (#804)

This commit is contained in:
7Sageer 2026-06-16 11:34:44 +08:00 committed by GitHub
parent aa1896ca74
commit 299b9fcad4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 3 deletions

View file

@ -0,0 +1,6 @@
---
"@moonshot-ai/agent-core": patch
"@moonshot-ai/kimi-code": patch
---
Prevent session shutdown from resuming the agent when stopping background tasks.

View file

@ -346,9 +346,15 @@ export class Session {
});
if (keepAliveOnExit) return;
await Promise.all(
Array.from(this.readyAgents(), (agent) =>
agent.background.stopAll('Session closed'),
),
Array.from(this.readyAgents(), async (agent) => {
const activeTasks = agent.background.list(true);
await Promise.all(
activeTasks.map((task) =>
agent.background.suppressTerminalNotification(task.taskId),
),
);
await agent.background.stopAll('Session closed');
}),
);
}

View file

@ -131,6 +131,30 @@ describe('Session lifecycle hooks', () => {
expect(agent.background.getTask(taskId)?.status).toBe('killed');
});
it('does not steer background task notifications while closing the session', async () => {
const { sessionDir, workDir } = await hookFixture();
const session = new Session({
kaos: testKaos.withCwd(workDir),
id: 'session-bg-cleanup-no-steer',
homedir: sessionDir,
rpc: createSessionRpc(),
skills: { explicitDirs: [join(workDir, 'missing-skills')] },
});
const agent = await session.createMain();
const steerSpy = vi.spyOn(agent.turn, 'steer');
const { proc, killSpy } = pendingProcess();
const taskId = agent.background.registerTask(
new ProcessBackgroundTask(proc, 'sleep 60', 'exit cleanup without steer'),
);
await session.close();
await new Promise((resolve) => setImmediate(resolve));
expect(killSpy).toHaveBeenCalledWith('SIGTERM');
expect(agent.background.getTask(taskId)?.status).toBe('killed');
expect(steerSpy).not.toHaveBeenCalled();
});
it('keeps background tasks alive on close when keepAliveOnExit is true', async () => {
const { sessionDir, workDir } = await hookFixture();
const session = new Session({