Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

Keep the draft when opening the agents view (#1372)

Sebastian Müllerd51590c418d5
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md
index 73fdf74faa..b352e3e250 100644
--- a/packages/coding-agent/CHANGELOG.md
+++ b/packages/coding-agent/CHANGELOG.md
@@ -2,6 +2,7 @@
## [Unreleased]
+- Changed opening the agents view (full or scoped) with a draft prompt to auto-stash the draft instead of refusing; the draft is restored into the editor when the session is reopened.
- Fixed Shift+Enter no longer inserting a newline in terminals that send a literal `\n` (for example a Ghostty `shift+enter=text:\n` mapping): the byte decoded as `ctrl+j` and triggered the new edit-diff toggle instead of the editor newline.
- Removed a system prompt paragraph referring to an async `bash()` kernel helper and managed jobs that do not exist in the runtime.
- Changed RLM guidance to orchestrate independent workers in parallel, use available async shell helpers safely, end the turn instead of sleeping, polling, or blocking on long awaits, provide proactive outcome-focused progress updates from root agents, and use simplified technical English for user-facing prose.
diff --git a/packages/coding-agent/src/modes/interactive/interactive-mode.ts b/packages/coding-agent/src/modes/interactive/interactive-mode.ts
index 15f4f79e09..acdb0cdd05 100644
--- a/packages/coding-agent/src/modes/interactive/interactive-mode.ts
+++ b/packages/coding-agent/src/modes/interactive/interactive-mode.ts
@@ -1464,6 +1464,7 @@ export class InteractiveMode {
async run(): Promise<InteractiveModeRunResult> {
await this.init();
+ this.restorePromptStashOnOpen();
// Global, environment-scoped notices (app update, extension updates, tmux setup)
// belong on the agents view, not in a conversation. When the agents view already
@@ -4137,6 +4138,27 @@ export class InteractiveMode {
return this.snapshotPromptStashFrom(this.editor, text);
}
+ private restorePromptStashOnOpen(): void {
+ if (!this.promptStash?.restoreOnOpen) return;
+ // Land the restore notice in its own status block: init may have just posted
+ // a notice (e.g. compaction) that showStatus would otherwise replace.
+ this.lastStatusText = undefined;
+ this.lastStatusSpacer = undefined;
+ this.restorePromptStashIfEditorEmpty();
+ }
+
+ private stashDraftForAgentsView(): void {
+ const text = this.editor.getText();
+ if (!text.trim()) return;
+ // Head of the durable queue so it restores first on return; an existing
+ // manual stash stays queued behind it and keeps its manual-stash semantics.
+ const existing = [this.promptStashState.stash, ...(this.promptStashState.queuedStashes ?? [])].filter(
+ (stash): stash is PromptStash => stash !== undefined,
+ );
+ this.promptStashState.stash = { ...this.snapshotPromptStash(text), restoreOnOpen: true };
+ this.promptStashState.queuedStashes = existing.length > 0 ? existing : undefined;
+ }
+
private handlePromptStash(): void {
const text = this.editor.getText();
if (!text.trim()) {
@@ -5878,11 +5900,6 @@ export class InteractiveMode {
}
private async openScopedAgentsView(): Promise<void> {
- if (this.editor.getText().trim()) {
- this.focusEditor();
- this.showStatus("Send, stash, or clear your draft before opening agents");
- return;
- }
if (!this.options.returnToAgentsView) {
this.focusEditor();
this.showStatus("The agents view needs the daemon; start without --no-daemon to browse sessions");
@@ -6717,10 +6734,6 @@ export class InteractiveMode {
}
private async requestAgentsView(): Promise<void> {
- if (this.editor.getText().length > 0) {
- this.showStatus("Send, stash, or clear your draft before opening agents");
- return;
- }
if (!this.options.returnToAgentsView) {
this.showStatus("The agents view needs the daemon; start without --no-daemon to browse sessions");
return;
@@ -6730,6 +6743,7 @@ export class InteractiveMode {
private async returnToAgentsView(request: InteractiveModeRunResult["type"] = "agents_view"): Promise<void> {
if (this.isShuttingDown || this.agentsViewRequest) return;
+ this.stashDraftForAgentsView();
this.agentsViewRequest = request;
this.isShuttingDown = true;
this.unregisterSignalHandlers();
diff --git a/packages/coding-agent/src/modes/interactive/prompt-stash-state.ts b/packages/coding-agent/src/modes/interactive/prompt-stash-state.ts
index e8c99824f0..ff4b5b9a70 100644
--- a/packages/coding-agent/src/modes/interactive/prompt-stash-state.ts
+++ b/packages/coding-agent/src/modes/interactive/prompt-stash-state.ts
@@ -6,6 +6,7 @@ export interface PromptStash {
expandedText?: string;
pasteSnapshot?: EditorPasteSnapshot;
images?: readonly (readonly [number, ImageContent])[];
+ restoreOnOpen?: boolean;
}
export interface PromptStashState {
diff --git a/packages/coding-agent/test/interactive-mode-prompt-stash.test.ts b/packages/coding-agent/test/interactive-mode-prompt-stash.test.ts
index c2443d1362..9c033e8c26 100644
--- a/packages/coding-agent/test/interactive-mode-prompt-stash.test.ts
+++ b/packages/coding-agent/test/interactive-mode-prompt-stash.test.ts
@@ -15,6 +15,7 @@ type PromptStash = {
expandedText?: string;
pasteSnapshot?: FakePasteSnapshot;
images?: readonly (readonly [number, ImageContent])[];
+ restoreOnOpen?: boolean;
};
type FakeEditor = {
@@ -109,6 +110,8 @@ type PromptStashMethods = {
hydratePromptStash: (this: SharedPromptStashHarness) => void;
resetCurrentSessionRenderState: (this: ResetHarness, options?: { clearPromptStash?: boolean }) => void;
restorePromptStashIfEditorEmpty: (this: PromptStashHarness, stash?: PromptStash) => boolean;
+ restorePromptStashOnOpen: (this: PromptStashHarness) => void;
+ stashDraftForAgentsView: (this: SharedPromptStashHarness) => void;
liveImageMarkerIds: (this: PromptStashLiveMarkerHarness) => Set<number>;
setupEditorSubmitHandler: (this: SubmitHarness) => void;
};
@@ -322,6 +325,95 @@ describe("InteractiveMode prompt stash", () => {
expect(state.queuedStashes).toBeUndefined();
});
+ it("auto-stashes a draft for the agents view and restores it on reopen", () => {
+ const store = new ClientPromptStashStore();
+ const pasteSnapshot: FakePasteSnapshot = {
+ pastes: [[1, "line one\nline two"]],
+ pasteCounter: 1,
+ };
+ const mode = createSharedPromptStashHarness(store, "session-a", {
+ text: "draft [paste #1 +2 lines]",
+ expandedText: "draft line one\nline two",
+ pasteSnapshot,
+ });
+
+ interactiveModeMethods.stashDraftForAgentsView.call(mode);
+
+ expect(mode.promptStashState.stash).toMatchObject({
+ text: "draft [paste #1 +2 lines]",
+ restoreOnOpen: true,
+ });
+
+ const reopenedMode = createSharedPromptStashHarness(store, "session-a");
+ interactiveModeMethods.hydratePromptStash.call(reopenedMode);
+ interactiveModeMethods.restorePromptStashOnOpen.call(reopenedMode);
+
+ expect(reopenedMode.editor.getText()).toBe("draft [paste #1 +2 lines]");
+ expect(reopenedMode.editor.restoredPasteSnapshot).toBe(pasteSnapshot);
+ expect(reopenedMode.promptStashState.stash).toBeUndefined();
+ });
+
+ it("lands the on-open restore notice in a fresh status block", () => {
+ const store = new ClientPromptStashStore();
+ const mode = createSharedPromptStashHarness(store, "session-a", { text: "draft" });
+ interactiveModeMethods.stashDraftForAgentsView.call(mode);
+
+ const reopenedMode = createSharedPromptStashHarness(store, "session-a");
+ // init() may have just posted a status (e.g. a compaction notice); showStatus
+ // replaces the anchored last status, so the restore must drop the anchor first.
+ const priorStatus = { setText: vi.fn() };
+ (reopenedMode as { lastStatusText?: unknown }).lastStatusText = priorStatus;
+ (reopenedMode as { lastStatusSpacer?: unknown }).lastStatusSpacer = { spacer: true };
+ interactiveModeMethods.restorePromptStashOnOpen.call(reopenedMode);
+
+ expect(reopenedMode.editor.getText()).toBe("draft");
+ expect((reopenedMode as { lastStatusText?: unknown }).lastStatusText).toBeUndefined();
+ expect((reopenedMode as { lastStatusSpacer?: unknown }).lastStatusSpacer).toBeUndefined();
+ expect(priorStatus.setText).not.toHaveBeenCalled();
+ });
+
+ it("queues an existing manual stash behind the agents-view auto-stash", () => {
+ const store = new ClientPromptStashStore();
+ const state = store.forSession("session-a");
+ state.stash = { text: "manual draft" };
+ const mode = createSharedPromptStashHarness(store, "session-a", { text: "typed draft" });
+
+ interactiveModeMethods.stashDraftForAgentsView.call(mode);
+
+ expect(state.stash).toMatchObject({ text: "typed draft", restoreOnOpen: true });
+ expect(state.queuedStashes).toEqual([{ text: "manual draft" }]);
+
+ const reopenedMode = createSharedPromptStashHarness(store, "session-a");
+ interactiveModeMethods.restorePromptStashOnOpen.call(reopenedMode);
+
+ expect(reopenedMode.editor.getText()).toBe("typed draft");
+ expect(state.stash).toEqual({ text: "manual draft" });
+ expect(state.queuedStashes).toBeUndefined();
+ });
+
+ it("skips the agents-view auto-stash for whitespace-only drafts", () => {
+ const store = new ClientPromptStashStore();
+ const state = store.forSession("session-a");
+ state.stash = { text: "manual draft" };
+ const mode = createSharedPromptStashHarness(store, "session-a", { text: " \n\t" });
+
+ interactiveModeMethods.stashDraftForAgentsView.call(mode);
+
+ expect(state.stash).toEqual({ text: "manual draft" });
+ expect(state.queuedStashes).toBeUndefined();
+ });
+
+ it("does not auto-restore a manual stash on reopen", () => {
+ const store = new ClientPromptStashStore();
+ store.forSession("session-a").stash = { text: "manual draft" };
+
+ const reopenedMode = createSharedPromptStashHarness(store, "session-a");
+ interactiveModeMethods.restorePromptStashOnOpen.call(reopenedMode);
+
+ expect(reopenedMode.editor.getText()).toBe("");
+ expect(store.forSession("session-a").stash).toEqual({ text: "manual draft" });
+ });
+
it("rebinds prompt stash state when the connected session changes", () => {
const store = new ClientPromptStashStore();
const firstState = store.forSession("session-a");
diff --git a/packages/coding-agent/test/interactive-mode-startup.test.ts b/packages/coding-agent/test/interactive-mode-startup.test.ts
index c7ea2bc7ac..c29a166a87 100644
--- a/packages/coding-agent/test/interactive-mode-startup.test.ts
+++ b/packages/coding-agent/test/interactive-mode-startup.test.ts
@@ -8,6 +8,7 @@ import {
InteractiveMode,
START_HINTS,
} from "../src/modes/interactive/interactive-mode.js";
+import type { PromptStashState } from "../src/modes/interactive/prompt-stash-state.js";
import { getMarkdownTheme, initTheme } from "../src/modes/interactive/theme/theme.js";
describe("InteractiveMode startup hints", () => {
@@ -91,7 +92,7 @@ describe("InteractiveMode startup hints", () => {
expect(returnToAgentsView).toHaveBeenCalledOnce();
});
- it("explains why a draft blocks the destructive agents-view handoff", async () => {
+ it("no longer blocks the agents-view handoff on a draft", async () => {
const returnToAgentsView = vi.fn(async () => {});
const showStatus = vi.fn();
const mode = Object.assign(
@@ -101,8 +102,51 @@ describe("InteractiveMode startup hints", () => {
await Reflect.get(InteractiveMode.prototype, "requestAgentsView").call(mode);
- expect(returnToAgentsView).not.toHaveBeenCalled();
- expect(showStatus).toHaveBeenCalledWith("Send, stash, or clear your draft before opening agents");
+ expect(returnToAgentsView).toHaveBeenCalledOnce();
+ expect(showStatus).not.toHaveBeenCalled();
+ });
+
+ it("no longer blocks the scoped agents-view handoff on a draft", async () => {
+ const returnToAgentsView = vi.fn(async () => {});
+ const showStatus = vi.fn();
+ const mode = Object.assign(
+ createMode(false, true, () => "scoped draft"),
+ { returnToAgentsView, showStatus },
+ );
+
+ await Reflect.get(InteractiveMode.prototype, "openScopedAgentsView").call(mode);
+
+ expect(returnToAgentsView).toHaveBeenCalledWith("scoped_agents_view");
+ expect(showStatus).not.toHaveBeenCalled();
+ });
+
+ it("stashes the draft once per agents-view handoff even when re-requested mid-teardown", async () => {
+ const promptStashState: PromptStashState = {};
+ let resolveDispose!: () => void;
+ const disposePromise = new Promise<void>((resolve) => {
+ resolveDispose = resolve;
+ });
+ const mode = Object.assign(
+ createMode(false, true, () => "draft prompt"),
+ {
+ promptStashState,
+ pastedImages: new Map(),
+ isShuttingDown: false,
+ agentsViewRequest: undefined,
+ unregisterSignalHandlers: vi.fn(),
+ teardownSessionUi: vi.fn(async () => {}),
+ agentConnection: { dispose: vi.fn(() => disposePromise) },
+ },
+ );
+ const returnToAgentsView = Reflect.get(InteractiveMode.prototype, "returnToAgentsView");
+
+ const firstHandoff = returnToAgentsView.call(mode);
+ await returnToAgentsView.call(mode);
+
+ expect(promptStashState.stash).toMatchObject({ text: "draft prompt", restoreOnOpen: true });
+ expect(promptStashState.queuedStashes).toBeUndefined();
+ resolveDispose();
+ await firstHandoff;
});
it("opens the shared session view on back navigation for process-local chats", async () => {
diff --git a/packages/coding-agent/test/interactive-mode-status.test.ts b/packages/coding-agent/test/interactive-mode-status.test.ts
index 165d219d8d..d699993abd 100644
--- a/packages/coding-agent/test/interactive-mode-status.test.ts
+++ b/packages/coding-agent/test/interactive-mode-status.test.ts
@@ -3001,6 +3001,7 @@ describe("InteractiveMode Prime CLI onboarding", () => {
): Record<string, unknown> & { getUserInput: ReturnType<typeof vi.fn> } {
return {
init: vi.fn(async () => {}),
+ restorePromptStashOnOpen: vi.fn(),
options: { agentsViewOwnsStartupNotices: true, ...options },
modelRegistry: { getError: vi.fn(() => undefined) },
runStartupOnboarding: vi.fn(async () => true),