Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/src/ui/commands/contextCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export async function collectContextData(
// single render — that resolves the moment any send happens.
//
// TODO: plumb the chat history into collectContextData and use
// estimatePromptTokens(history, undefined, 0, imageTokenEstimate) here
// estimatePromptTokens(history, undefined, 0, 0, imageTokenEstimate) here
// for same-source-of-truth as the cheap-gate. Defer because Config
// doesn't expose the active chat instance today.
const tierTokens = isEstimated ? rawOverhead : apiTotalTokens;
Expand Down
43 changes: 42 additions & 1 deletion packages/core/src/core/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from './contentGenerator.js';
import { BaseLlmClient } from './baseLlmClient.js';
import { buildAgentContentGeneratorConfig } from '../models/content-generator-config.js';
import { type GeminiChat } from './geminiChat.js';
import { GeminiChat } from './geminiChat.js';
import type { Config } from '../config/config.js';
import { ApprovalMode } from '../config/config.js';
import {
Expand Down Expand Up @@ -598,6 +598,47 @@ describe('Gemini Client (client.ts)', () => {
expect(resumedClient.getChat().getLastPromptTokenCount()).toBe(123_456);
});

it('seeds resumed chat with previous response output token count', async () => {
const seedResumeTokenCountsSpy = vi.spyOn(
GeminiChat.prototype,
'seedResumeTokenCounts',
);
vi.mocked(mockConfig.getResumedSessionData).mockReturnValue({
conversation: {
sessionId: 'resumed-session-id',
projectHash: 'project-hash',
startTime: new Date(0).toISOString(),
lastUpdated: new Date(0).toISOString(),
messages: [
{
uuid: 'assistant-1',
parentUuid: null,
sessionId: 'resumed-session-id',
timestamp: new Date(0).toISOString(),
type: 'assistant',
cwd: '/test/project',
version: '1.0.0',
message: { role: 'model', parts: [{ text: 'done' }] },
usageMetadata: {
promptTokenCount: 200,
candidatesTokenCount: 60,
thoughtsTokenCount: 20,
totalTokenCount: 280,
},
},
],
},
filePath: '/test/session.jsonl',
lastCompletedUuid: null,
});

const resumedClient = new GeminiClient(mockConfig);
await resumedClient.initialize();

expect(resumedClient.getChat().getLastPromptTokenCount()).toBe(200);
expect(seedResumeTokenCountsSpy).toHaveBeenCalledWith(200, 80);
});

it('seeds recently completed tools from resumed history', async () => {
vi.mocked(mockConfig.getResumedSessionData).mockReturnValue({
conversation: {
Expand Down
16 changes: 12 additions & 4 deletions packages/core/src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export class GeminiClient {
// Check if we're resuming from a previous session
const resumedSessionData = this.config.getResumedSessionData();
if (resumedSessionData) {
replayUiTelemetryFromConversation(
const resumeTokenCounts = replayUiTelemetryFromConversation(
resumedSessionData.conversation,
this.config.getSessionId(),
);
Expand All @@ -304,9 +304,17 @@ export class GeminiClient {
resumedHistory,
sessionStartSource ?? SessionStartSource.Resume,
);
this.getChat().setLastPromptTokenCount(
uiTelemetryService.getLastPromptTokenCount(),
);
const chat = this.getChat();
if (resumeTokenCounts) {
chat.seedResumeTokenCounts(
resumeTokenCounts.promptTokenCount,
resumeTokenCounts.outputTokenCount,
);
} else {
chat.setLastPromptTokenCount(
uiTelemetryService.getLastPromptTokenCount(),
);
}

// Restore attribution state from the last snapshot in the session
this.restoreAttributionFromSession(resumedSessionData.conversation);
Expand Down
Loading
Loading