Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-wires-persist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Prevent recent ACP assistant responses from being lost when a session closes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* Agent-scoped telemetry view. New logs receive a metadata
* envelope while non-empty unversioned logs are rejected. Removal awaits the
* agent task manager's graceful exit policy before draining turns and full
* compaction, then disposing the child scope. Fans session-level
* permission-mode switches out to every live agent. Bound at Session scope.
* compaction, flushing the replayable wire, then disposing the child scope.
* Fans session-level permission-mode switches out to every live agent. Bound
* at Session scope.
*
* No agent id is special here: the main agent is simply the agent created
* with the conventional `MAIN_AGENT_ID`, and `fork` requires its source to
Expand Down Expand Up @@ -268,8 +269,12 @@ export class AgentLifecycleService extends Disposable implements IAgentLifecycle
compaction.abortController.abort(reason);
}
await Promise.all([loop.settled(), compactionSettled]);
handle.dispose();
this.onDidDisposeEmitter.fire(agentId);
try {
await handle.accessor.get(IWireService).flush();
} finally {
handle.dispose();
this.onDidDisposeEmitter.fire(agentId);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { createMcpOAuthStore } from '#/app/mcpConfig/oauthStore';
import { ISessionSubagentService } from '#/session/subagent/subagent';
import { SessionSubagentService } from '#/session/subagent/subagentService';
import '#/agent/mcp/mcpService';
import { IWireService } from '#/wire/wire';
import '#/wire/wireService';
import { IAgentTaskService } from '#/agent/task/task';
import { ISessionCronService } from '#/session/cron/sessionCronService';
Expand Down Expand Up @@ -455,6 +456,52 @@ describe('AgentLifecycleService', () => {
expect(removed).toBe(true);
});

it('remove waits for the agent wire to flush before completing', async () => {
const svc = ix.get(IAgentLifecycleService);
const handle = await svc.create({ agentId: 'main' });
let markFlushStarted!: () => void;
const flushStarted = new Promise<void>((resolve) => {
markFlushStarted = resolve;
});
let releaseFlush!: () => void;
const flushReleased = new Promise<void>((resolve) => {
releaseFlush = resolve;
});
const flush = vi
.spyOn(handle.accessor.get(IWireService), 'flush')
.mockImplementation(async () => {
markFlushStarted();
await flushReleased;
});

let removed = false;
const removal = svc.remove('main').then(() => {
removed = true;
});
await flushStarted;
expect(removed).toBe(false);

releaseFlush();
await removal;
expect(flush).toHaveBeenCalledOnce();
expect(removed).toBe(true);
});

it('remove still disposes the agent when the wire flush rejects', async () => {
const svc = ix.get(IAgentLifecycleService);
const handle = await svc.create({ agentId: 'main' });
const flushError = new Error('wire flush failed');
vi.spyOn(handle.accessor.get(IWireService), 'flush').mockRejectedValueOnce(flushError);
const disposed: string[] = [];
disposables.add(svc.onDidDispose((id) => disposed.push(id)));

await expect(svc.remove('main')).rejects.toBe(flushError);

expect(svc.get('main')).toBeUndefined();
expect(disposed).toEqual(['main']);
expect(() => handle.accessor.get(IWireService)).toThrow();
});

it('ignites the self-wiring toolDedupe plugin so its listeners exist before the first turn', async () => {
const svc = ix.get(IAgentLifecycleService);
await svc.create({ agentId: 'main' });
Expand Down