chore: Save agent ID on exit (#274)

This commit is contained in:
Devansh Jain
2025-12-17 17:38:51 -08:00
committed by GitHub
parent fdcd19e587
commit 085728ce30
2 changed files with 32 additions and 4 deletions

View File

@@ -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) {

View File

@@ -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 (
<Box flexDirection="column" paddingTop={1}>
<Text dimColor>Total duration (API): {apiDuration}</Text>
<Text dimColor>Total duration (wall): {wallDuration}</Text>
<Text dimColor>
Usage: {stats.usage.stepCount} steps,{" "}
{formatNumber(stats.usage.promptTokens)} input,{" "}
{formatNumber(stats.usage.completionTokens)} output
Usage: {steps} steps · {inputTokens} input · {outputTokens} output
</Text>
{agentId && <Text dimColor>Agent ID: {agentId}</Text>}
</Box>