@@ -6,7 +6,7 @@ import {
66 insertToolUsageReminder,
77 MAIN_PROMPT,
88 } from "./prompt";
9-import { estimateTokens } from "./shared";
9+import { estimateTokens, secureRandomString } from "./shared";
1010 import { bash, runBashTool } from "./tool-bash";
1111 import { edit, runEditTool } from "./tool-edit";
1212 import { runTaskTool, task } from "./tool-task";
@@ -23,6 +23,7 @@ import { Conversation, emptyState } from "./tui-conversation";
2323 import { Editor } from "./tui-editor";
2424 import { mainMenu } from "./tui-overlay";
2525 import type { AgentContex, ToolAndRunner, TUIState } from "./types";
26+import { updateSession } from "./session";
2627
2728 // TODO: move all git things to `git.ts`
2829 const git = simpleGit();
@@ -40,6 +41,8 @@ function clearOrAbort(state: TUIState) {
4041 }
4142
4243 export function initTUI(state: TUIState, leave: (s: string) => void) {
44+ // TODO: Cleanup accumulated sessions for this cwd.
45+
4346 const { spinnerEvery, currentSpinner } = Spinner();
4447
4548 // Stable 60fps rendering.
@@ -195,6 +198,11 @@ async function streamAgentTUI(state: TUIState) {
195198 }
196199 } finally {
197200 state.streaming = false;
201+ if (!state.sessionId) {
202+ const id = secureRandomString(10);
203+ state.sessionId = id;
204+ }
205+ await updateSession(state.sessionId, state.messages)
198206 }
199207
200208 try {