Skip to content
Merged
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
40 changes: 40 additions & 0 deletions apps/code/src/renderer/features/sessions/service/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,46 @@ describe("SessionService", () => {
expect(mockTrpcAgent.reconnect.mutate).toHaveBeenCalled();
});

it("creates fresh session when initialPrompt is set (prompt never delivered)", async () => {
const service = getSessionService();
const mockSession = createMockSession({
status: "error",
initialPrompt: [{ type: "text", text: "fix the bug" }],
});
// First call returns the error session, subsequent calls return connected
mockSessionStoreSetters.getSessionByTaskId
.mockReturnValueOnce(mockSession)
.mockReturnValue(
createMockSession({
taskRunId: "new-run",
status: "connected",
}),
);
mockTrpcAgent.start.mutate.mockResolvedValue({
channel: "agent-event:new-run",
configOptions: [],
});
mockTrpcAgent.onSessionEvent.subscribe.mockReturnValue({
unsubscribe: vi.fn(),
});
mockTrpcAgent.onPermissionRequest.subscribe.mockReturnValue({
unsubscribe: vi.fn(),
});
mockTrpcAgent.prompt.mutate.mockResolvedValue({ stopReason: "end_turn" });
mockBuildAuthenticatedClient.mockReturnValue({
createTaskRun: vi.fn().mockResolvedValue({ id: "new-run" }),
appendTaskRunLog: vi.fn(),
});

await service.clearSessionError("task-123", "/repo");

// Should tear down old session and create a new one
expect(mockTrpcAgent.cancel.mutate).toHaveBeenCalledWith({
sessionId: "run-123",
});
expect(mockTrpcAgent.start.mutate).toHaveBeenCalled();
});

it("handles missing session gracefully", async () => {
const service = getSessionService();
mockSessionStoreSetters.getSessionByTaskId.mockReturnValue(undefined);
Expand Down
8 changes: 8 additions & 0 deletions apps/code/src/renderer/features/sessions/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,14 @@ export class SessionService {
isJsonRpcRequest(acpMsg.message) &&
acpMsg.message.method === "session/prompt";

// Once the agent starts responding, clear initialPrompt so that
// retry reconnects to this session instead of creating a new one.
if (!isUserPromptEcho && session.initialPrompt?.length) {
sessionStoreSetters.updateSession(taskRunId, {
initialPrompt: undefined,
});
}

if (isUserPromptEcho) {
sessionStoreSetters.replaceOptimisticWithEvent(taskRunId, acpMsg);
} else {
Expand Down
Loading