From f77daf2269dfcb8d729a73d04d7df7aa12b10c7f Mon Sep 17 00:00:00 2001 From: Aditya Mitra <61635505+uinstinct@users.noreply.github.com> Date: Sat, 14 Mar 2026 00:41:48 +0530 Subject: [PATCH] fix(cli): prevent empty sessions from being saved (#11076) * add /sessions slash command * do not save empty sessions * update tests * Update extensions/cli/src/services/ChatHistoryService.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * remove duplicate telemetry capture --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --- extensions/cli/src/commands/commands.ts | 5 ++++ .../src/services/ChatHistoryService.test.ts | 2 +- .../cli/src/services/ChatHistoryService.ts | 28 +++++++++++-------- extensions/cli/src/session.test.ts | 28 ++++++++++++++++++- extensions/cli/src/session.ts | 26 ++++++++++++++++- extensions/cli/src/slashCommands.ts | 8 ++++-- 6 files changed, 79 insertions(+), 18 deletions(-) diff --git a/extensions/cli/src/commands/commands.ts b/extensions/cli/src/commands/commands.ts index b14a5c4a0..dabfc82ee 100644 --- a/extensions/cli/src/commands/commands.ts +++ b/extensions/cli/src/commands/commands.ts @@ -111,6 +111,11 @@ export const SYSTEM_SLASH_COMMANDS: SystemCommand[] = [ description: "List background jobs", category: "system", }, + { + name: "sessions", + description: "Show all chat sessions", + category: "system", + }, ]; // Remote mode specific commands diff --git a/extensions/cli/src/services/ChatHistoryService.test.ts b/extensions/cli/src/services/ChatHistoryService.test.ts index a7e98803d..dcd4cf4d9 100644 --- a/extensions/cli/src/services/ChatHistoryService.test.ts +++ b/extensions/cli/src/services/ChatHistoryService.test.ts @@ -38,7 +38,7 @@ describe("ChatHistoryService", () => { expect(state.history).toEqual([]); expect(state.compactionIndex).toBeNull(); - expect(state.sessionId).toBe("test-session-id"); + expect(state.sessionId).toBe(""); expect(state.isRemoteMode).toBe(false); }); diff --git a/extensions/cli/src/services/ChatHistoryService.ts b/extensions/cli/src/services/ChatHistoryService.ts index 6fbc24e04..b4a1c56a7 100644 --- a/extensions/cli/src/services/ChatHistoryService.ts +++ b/extensions/cli/src/services/ChatHistoryService.ts @@ -1,11 +1,7 @@ import type { ChatHistoryItem, ToolStatus } from "core/index.js"; import { createHistoryItem } from "core/util/messageConversion.js"; -import { - updateSessionHistory, - loadSessionById, - createSession, -} from "../session.js"; +import { loadSessionById, updateSessionHistory } from "../session.js"; import { logger } from "../util/logger.js"; import { BaseService } from "./BaseService.js"; @@ -76,23 +72,31 @@ export class ChatHistoryService extends BaseService { /** * Initialize the service with optional session + * If no session is provided, we delay session creation until content is added */ async doInitialize( session?: any, isRemoteMode = false, ): Promise { - const activeSession = session || createSession([]); - logger.debug("Initializing ChatHistoryService", { - sessionId: activeSession.sessionId, - historyLength: activeSession.history.length, + sessionId: session?.sessionId, + historyLength: session?.history?.length, isRemoteMode, }); + if (session) { + return { + history: session.history || [], + compactionIndex: this.findCompactionIndex(session.history || []), + sessionId: session.sessionId, + isRemoteMode, + }; + } + return { - history: activeSession.history || [], - compactionIndex: this.findCompactionIndex(activeSession.history || []), - sessionId: activeSession.sessionId, + history: [], + compactionIndex: null, + sessionId: "", isRemoteMode, }; } diff --git a/extensions/cli/src/session.test.ts b/extensions/cli/src/session.test.ts index dc73922bd..51bb64566 100644 --- a/extensions/cli/src/session.test.ts +++ b/extensions/cli/src/session.test.ts @@ -195,7 +195,7 @@ describe("SessionManager", () => { }); describe("saveSession", () => { - it("should save the current session to file", () => { + it("should save the current session to file when it has content", () => { const session = getCurrentSession(); session.title = "Test Session"; session.history = [ @@ -216,6 +216,32 @@ describe("SessionManager", () => { ); }); + it("should not save empty sessions", () => { + const session = getCurrentSession(); + session.history = []; + + saveSession(); + + expect(mockFs.writeFileSync).not.toHaveBeenCalled(); + }); + + it("should not save sessions with only system messages", () => { + const session = getCurrentSession(); + session.history = [ + { + message: { + role: "system", + content: "System message", + }, + contextItems: [], + }, + ]; + + saveSession(); + + expect(mockFs.writeFileSync).not.toHaveBeenCalled(); + }); + it("should filter out system messages except the first one", () => { const session = getCurrentSession(); session.history = [ diff --git a/extensions/cli/src/session.ts b/extensions/cli/src/session.ts index 8a0173b95..a3dcc5b5a 100644 --- a/extensions/cli/src/session.ts +++ b/extensions/cli/src/session.ts @@ -264,12 +264,36 @@ export function getCompleteStateSnapshot( }; } +/** + * Check if a session has meaningful content for saving + */ +function hasSessionContent(session: Session): boolean { + if (!session.history || session.history.length === 0) { + return false; + } + + const nonSystemMessages = session.history.filter( + (item) => item.message.role !== "system", + ); + + return nonSystemMessages.length > 0; +} + /** * Save the current session to file */ export function saveSession(): void { try { - const session = SessionManager.getInstance().getCurrentSession(); + const manager = SessionManager.getInstance(); + if (!manager.hasSession()) { + return; + } + + const session = manager.getCurrentSession(); + if (!hasSessionContent(session)) { + return; + } + const sessionToSave = getSessionPersistenceSnapshot(session); historyManager.save(sessionToSave); } catch (error) { diff --git a/extensions/cli/src/slashCommands.ts b/extensions/cli/src/slashCommands.ts index 8ec3cc91e..7aaa49b1a 100644 --- a/extensions/cli/src/slashCommands.ts +++ b/extensions/cli/src/slashCommands.ts @@ -143,8 +143,6 @@ async function handleFork() { } function handleTitle(args: string[]) { - posthogService.capture("useSlashCommand", { name: "title" }); - const title = args.join(" ").trim(); if (!title) { return { @@ -173,6 +171,10 @@ function handleJobs() { return { openJobsSelector: true }; } +function handleSessions() { + return { openSessionSelector: true }; +} + const commandHandlers: Record = { help: handleHelp, clear: () => { @@ -202,13 +204,13 @@ const commandHandlers: Record = { title: handleTitle, rename: handleTitle, init: (args, assistant) => { - posthogService.capture("useSlashCommand", { name: "init" }); return handleInit(args, assistant); }, update: () => { return { openUpdateSelector: true }; }, jobs: handleJobs, + sessions: handleSessions, }; export async function handleSlashCommands(