@@ -20,6 +20,11 @@ import { getContextWindow, resolveModel } from "../llm-api/providers.ts";
2020 import { type CoreMessage, runTurn } from "../llm-api/turn.ts";
2121 import type { ToolDef } from "../llm-api/types.ts";
2222 import { connectMcpServer } from "../mcp/client.ts";
23+import {
24+ type GitStashPopResult,
25+ gitStashPop,
26+ gitStashTurn,
27+} from "../tools/git-stash.ts";
2328
2429 import {
2530 deleteLastTurn,
@@ -286,7 +291,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
286291 cwd,
287292 runSubagent: (prompt) => runSubagent(prompt),
288293
289- undoLastTurn: () => {
294+ undoLastTurn: async () => {
290295 // Nothing to undo if there are no messages
291296 if (session.messages.length === 0) return false;
292297
@@ -315,7 +320,20 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
315320
316321 // Delete from DB and decrement turn counter
317322 const deleted = deleteLastTurn(session.id);
323+ const poppedTurn = lastStash;
318324 if (turnIndex > 0) turnIndex--;
325+ lastStash = null;
326+
327+ // Restore files via git stash pop for the specific turn being undone
328+ if (poppedTurn !== null) {
329+ const popResult: GitStashPopResult = await gitStashPop(cwd, poppedTurn);
330+ if (popResult.restored === false && popResult.conflict) {
331+ renderError(
332+ "git stash pop conflict — your working tree may need manual resolution",
333+ );
334+ }
335+ }
336+
319337 return deleted;
320338 },
321339 connectMcpServer: connectAndAddMcp,
@@ -326,12 +344,15 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
326344 totalIn = 0;
327345 totalOut = 0;
328346 lastContextTokens = 0;
347+ lastStash = null;
329348 },
330349 };
331350 const spinner = new Spinner();
332351 let totalIn = 0;
333352 let totalOut = 0;
334353 let lastContextTokens = 0;
354+ // turnIndex of the last stash created, or null if no stash was made
355+ let lastStash: number | null = null;
335356
336357 // ── Handle initial prompt (non-interactive) ────────────────────────────────
337358 if (opts.initialPrompt) {
@@ -401,6 +422,11 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
401422 // Capture turn index for this turn (user + response share the same index)
402423 const thisTurn = turnIndex++;
403424
425+ // Snapshot working tree before anything is persisted or sent to the LLM.
426+ // If stashing fails (not a git repo, clean tree) stashed=false and /undo
427+ // will only roll back conversation history, not files — which is fine.
428+ const stashed = await gitStashTurn(cwd, thisTurn);
429+
404430 const coreContent = planMode
405431 ? `${resolvedText}\n\n<system-message>PLAN MODE ACTIVE: Help the user gather context for the plan -- READ ONLY</system-message>`
406432 : resolvedText;
@@ -416,6 +442,9 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
416442 const onSigInt = () => abortController.abort();
417443 process.once("SIGINT", onSigInt);
418444
445+ // Store stash metadata so undoLastTurn can pop the right stash
446+ lastStash = stashed ? thisTurn : null;
447+
419448 spinner.start("thinking");
420449
421450 const events = runTurn({