From 56b38d2fbeea8168c3b424fde8362befe59d2878 Mon Sep 17 00:00:00 2001 From: Vignesh Natarajan Date: Sat, 14 Feb 2026 16:40:37 -0800 Subject: [PATCH] TUI: honor explicit session key in global scope --- src/tui/tui.test.ts | 34 +++++++++++++++++++++++++++++++- src/tui/tui.ts | 48 +++++++++++++++++++++++++++++---------------- 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/tui/tui.test.ts b/src/tui/tui.test.ts index 789b950099..7ef276526b 100644 --- a/src/tui/tui.test.ts +++ b/src/tui/tui.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { resolveFinalAssistantText } from "./tui.js"; +import { resolveFinalAssistantText, resolveTuiSessionKey } from "./tui.js"; describe("resolveFinalAssistantText", () => { it("falls back to streamed text when final text is empty", () => { @@ -15,3 +15,35 @@ describe("resolveFinalAssistantText", () => { ).toBe("All done"); }); }); + +describe("resolveTuiSessionKey", () => { + it("uses global only as the default when scope is global", () => { + expect( + resolveTuiSessionKey({ + raw: "", + sessionScope: "global", + currentAgentId: "main", + sessionMainKey: "agent:main:main", + }), + ).toBe("global"); + expect( + resolveTuiSessionKey({ + raw: "test123", + sessionScope: "global", + currentAgentId: "main", + sessionMainKey: "agent:main:main", + }), + ).toBe("agent:main:test123"); + }); + + it("keeps explicit agent-prefixed keys unchanged", () => { + expect( + resolveTuiSessionKey({ + raw: "agent:ops:incident", + sessionScope: "global", + currentAgentId: "main", + sessionMainKey: "agent:main:main", + }), + ).toBe("agent:ops:incident"); + }); +}); diff --git a/src/tui/tui.ts b/src/tui/tui.ts index 982749d248..f62fd2e646 100644 --- a/src/tui/tui.ts +++ b/src/tui/tui.ts @@ -77,6 +77,31 @@ export function createEditorSubmitHandler(params: { }; } +export function resolveTuiSessionKey(params: { + raw?: string; + sessionScope: SessionScope; + currentAgentId: string; + sessionMainKey: string; +}) { + const trimmed = (params.raw ?? "").trim(); + if (!trimmed) { + if (params.sessionScope === "global") { + return "global"; + } + return buildAgentMainSessionKey({ + agentId: params.currentAgentId, + mainKey: params.sessionMainKey, + }); + } + if (trimmed === "global" || trimmed === "unknown") { + return trimmed; + } + if (trimmed.startsWith("agent:")) { + return trimmed; + } + return `agent:${params.currentAgentId}:${trimmed}`; +} + export async function runTui(opts: TuiOptions) { const config = loadConfig(); const initialSessionInput = (opts.session ?? "").trim(); @@ -298,23 +323,12 @@ export async function runTui(opts: TuiOptions) { }; const resolveSessionKey = (raw?: string) => { - const trimmed = (raw ?? "").trim(); - if (sessionScope === "global") { - return "global"; - } - if (!trimmed) { - return buildAgentMainSessionKey({ - agentId: currentAgentId, - mainKey: sessionMainKey, - }); - } - if (trimmed === "global" || trimmed === "unknown") { - return trimmed; - } - if (trimmed.startsWith("agent:")) { - return trimmed; - } - return `agent:${currentAgentId}:${trimmed}`; + return resolveTuiSessionKey({ + raw, + sessionScope, + currentAgentId, + sessionMainKey, + }); }; currentSessionKey = resolveSessionKey(initialSessionInput);