diff --git a/src/cli/App.tsx b/src/cli/App.tsx index ada1c1a..6f785f4 100644 --- a/src/cli/App.tsx +++ b/src/cli/App.tsx @@ -17,7 +17,7 @@ import type { ApprovalResult } from "../agent/approval-execution"; import { prefetchAvailableModelHandles } from "../agent/available-models"; import { getResumeData } from "../agent/check-approval"; import { getClient } from "../agent/client"; -import { setCurrentAgentId } from "../agent/context"; +import { getCurrentAgentId, setCurrentAgentId } from "../agent/context"; import type { AgentProvenance } from "../agent/create"; import { sendMessageStream } from "../agent/message"; import { SessionStats } from "../agent/stats"; @@ -118,6 +118,18 @@ function uid(prefix: string) { return `${prefix}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`; } +// Save current agent as lastAgent before exiting +// This ensures subagent overwrites during the session don't persist +function saveLastAgentBeforeExit() { + try { + const currentAgentId = getCurrentAgentId(); + settingsManager.updateLocalProjectSettings({ lastAgent: currentAgentId }); + settingsManager.updateSettings({ lastAgent: currentAgentId }); + } catch { + // Ignore if no agent context set + } +} + // Get plan mode system reminder if in plan mode function getPlanModeReminder(): string { if (permissionMode.getMode() !== "plan") { @@ -1315,6 +1327,7 @@ export default function App({ ); const handleExit = useCallback(() => { + saveLastAgentBeforeExit(); setShowExitStats(true); // Give React time to render the stats, then exit setTimeout(() => { @@ -1721,6 +1734,8 @@ export default function App({ }); refreshDerived(); + saveLastAgentBeforeExit(); + // Exit after a brief delay to show the message setTimeout(() => process.exit(0), 500); } catch (error) { diff --git a/src/cli/components/SessionStats.tsx b/src/cli/components/SessionStats.tsx index 9679b5b..a65420f 100644 --- a/src/cli/components/SessionStats.tsx +++ b/src/cli/components/SessionStats.tsx @@ -10,6 +10,18 @@ function formatDuration(ms: number): string { if (ms < 1000) { return `${Math.round(ms)}ms`; } + + const totalSeconds = Math.floor(ms / 1000); + const hours = Math.floor(totalSeconds / 3600); + const minutes = Math.floor((totalSeconds % 3600) / 60); + const seconds = totalSeconds % 60; + + if (hours > 0) { + return `${hours}h ${minutes}m`; + } + if (minutes > 0) { + return `${minutes}m ${seconds}s`; + } return `${(ms / 1000).toFixed(1)}s`; } @@ -20,15 +32,16 @@ function formatNumber(n: number): string { export function SessionStats({ stats, agentId }: SessionStatsProps) { const wallDuration = formatDuration(stats.totalWallMs); const apiDuration = formatDuration(stats.totalApiMs); + const steps = stats.usage.stepCount; + const inputTokens = formatNumber(stats.usage.promptTokens); + const outputTokens = formatNumber(stats.usage.completionTokens); return ( Total duration (API): {apiDuration} Total duration (wall): {wallDuration} - Usage: {stats.usage.stepCount} steps,{" "} - {formatNumber(stats.usage.promptTokens)} input,{" "} - {formatNumber(stats.usage.completionTokens)} output + Usage: {steps} steps · {inputTokens} input · {outputTokens} output {agentId && Agent ID: {agentId}}