Skip to content

Commit 0423c18

Browse files
fix(code): preserve initial prompt across session error transitions (#1384)
1 parent fba8bef commit 0423c18

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

apps/code/src/renderer/features/sessions/service/service.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ export class SessionService {
202202
session.status = "error";
203203
session.errorMessage =
204204
"Authentication required. Please sign in to continue.";
205+
if (initialPrompt?.length) {
206+
session.initialPrompt = initialPrompt;
207+
}
205208
sessionStoreSetters.setSession(session);
206209
return;
207210
}
@@ -296,6 +299,9 @@ export class SessionService {
296299
session.status = "error";
297300
session.errorTitle = "Failed to connect";
298301
session.errorMessage = message;
302+
if (initialPrompt?.length) {
303+
session.initialPrompt = initialPrompt;
304+
}
299305

300306
if (latestRun?.log_url) {
301307
try {
@@ -504,6 +510,9 @@ export class SessionService {
504510
if (existing?.logUrl) {
505511
session.logUrl = existing.logUrl;
506512
}
513+
if (existing?.initialPrompt?.length) {
514+
session.initialPrompt = existing.initialPrompt;
515+
}
507516
sessionStoreSetters.setSession(session);
508517
}
509518

@@ -564,6 +573,13 @@ export class SessionService {
564573
useSessionAdapterStore.getState().setAdapter(taskRun.id, adapter);
565574
}
566575

576+
// Store the initial prompt on the session so retry/reset flows can
577+
// re-send it if the session errors after this point (e.g. subscription
578+
// error, agent crash, or prompt failure).
579+
if (initialPrompt?.length) {
580+
session.initialPrompt = initialPrompt;
581+
}
582+
567583
sessionStoreSetters.setSession(session);
568584
this.subscribeToChannel(taskRun.id);
569585

@@ -1786,8 +1802,32 @@ export class SessionService {
17861802
* Retry connecting to the existing session (resume attempt using
17871803
* the sessionId from logs). Does NOT tear down — avoids the connect
17881804
* effect loop.
1805+
*
1806+
* If the session failed before any conversation started (has an
1807+
* initialPrompt saved from the original creation attempt), creates
1808+
* a fresh session and re-sends the prompt instead of reconnecting
1809+
* to an empty session.
17891810
*/
17901811
async clearSessionError(taskId: string, repoPath: string): Promise<void> {
1812+
const session = sessionStoreSetters.getSessionByTaskId(taskId);
1813+
if (session?.initialPrompt?.length) {
1814+
const { taskTitle, initialPrompt } = session;
1815+
await this.teardownSession(session.taskRunId);
1816+
const auth = this.getAuthCredentials();
1817+
if (!auth) {
1818+
throw new Error(
1819+
"Unable to reach server. Please check your connection.",
1820+
);
1821+
}
1822+
await this.createNewLocalSession(
1823+
taskId,
1824+
taskTitle,
1825+
repoPath,
1826+
auth,
1827+
initialPrompt,
1828+
);
1829+
return;
1830+
}
17911831
await this.reconnectInPlace(taskId, repoPath);
17921832
}
17931833

apps/code/src/renderer/features/sessions/stores/sessionStore.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
ContentBlock,
23
SessionConfigOption,
34
SessionConfigSelectGroup,
45
SessionConfigSelectOption,
@@ -68,6 +69,8 @@ export interface AgentSession {
6869
cloudOutput?: Record<string, unknown> | null;
6970
/** Cloud task error message */
7071
cloudErrorMessage?: string | null;
72+
/** Initial prompt to re-send on retry if the first connection attempt failed */
73+
initialPrompt?: ContentBlock[];
7174
/** Cloud task branch */
7275
cloudBranch?: string | null;
7376
/** Number of session/prompt events to skip from polled logs (set during resume) */

0 commit comments

Comments
 (0)