Skip to content
Open
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
4 changes: 3 additions & 1 deletion packages/core/src/agents/local-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
): Promise<AgentTurnResult> {
const promptId = `${this.agentId}#${turnCounter}`;

await this.tryCompressChat(chat, promptId);
await this.tryCompressChat(chat, promptId, combinedSignal);

const { functionCalls } = await promptIdContext.run(promptId, async () =>
this.callModel(chat, currentMessage, combinedSignal, promptId),
Expand Down Expand Up @@ -668,6 +668,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
private async tryCompressChat(
chat: GeminiChat,
prompt_id: string,
abortSignal: AbortSignal,
): Promise<void> {
const model = this.definition.modelConfig.model ?? DEFAULT_GEMINI_MODEL;

Expand All @@ -678,6 +679,7 @@ export class LocalAgentExecutor<TOutput extends z.ZodTypeAny> {
model,
this.runtimeContext,
this.hasFailedCompressionAttempt,
abortSignal,
);

if (
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ export class GeminiClient {
// Check for context window overflow
const modelForLimitCheck = this._getActiveModelForCurrentTurn();

const compressed = await this.tryCompressChat(prompt_id, false);
const compressed = await this.tryCompressChat(prompt_id, false, signal);

if (compressed.compressionStatus === CompressionStatus.COMPRESSED) {
yield { type: GeminiEventType.ChatCompressed, value: compressed };
Expand Down Expand Up @@ -1049,19 +1049,25 @@ export class GeminiClient {
async tryCompressChat(
prompt_id: string,
force: boolean = false,
abortSignal?: AbortSignal,
): Promise<ChatCompressionInfo> {
// If the model is 'auto', we will use a placeholder model to check.
// Compression occurs before we choose a model, so calling `count_tokens`
// before the model is chosen would result in an error.
const model = this._getActiveModelForCurrentTurn();

// Use the provided signal, or create a no-op signal as a fallback for
// callers that don't have one (e.g. the /compress command).
const signal = abortSignal ?? new AbortController().signal;

const { newHistory, info } = await this.compressionService.compress(
this.getChat(),
prompt_id,
force,
model,
this.config,
this.hasFailedCompressionAttempt,
signal,
);

if (
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/services/chatCompressionService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ describe('ChatCompressionService', () => {
let testTempDir: string;
const mockModel = 'gemini-2.5-pro';
const mockPromptId = 'test-prompt-id';
const mockAbortSignal = new AbortController().signal;

beforeEach(() => {
testTempDir = fs.mkdtempSync(
Expand Down Expand Up @@ -211,6 +212,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);
expect(result.info.compressionStatus).toBe(CompressionStatus.NOOP);
expect(result.newHistory).toBeNull();
Expand All @@ -227,6 +229,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);
// It should now attempt compression even if previously failed (logic removed)
// But since history is small, it will be NOOP due to threshold
Expand All @@ -253,6 +256,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);
expect(result.info.compressionStatus).toBe(CompressionStatus.NOOP);
expect(result.newHistory).toBeNull();
Expand All @@ -276,6 +280,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.info.compressionStatus).toBe(CompressionStatus.COMPRESSED);
Expand Down Expand Up @@ -317,6 +322,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.info.compressionStatus).toBe(CompressionStatus.COMPRESSED);
Expand Down Expand Up @@ -344,6 +350,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

const firstCall = vi.mocked(mockConfig.getBaseLlmClient().generateContent)
Expand Down Expand Up @@ -371,6 +378,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.info.compressionStatus).toBe(CompressionStatus.COMPRESSED);
Expand Down Expand Up @@ -408,6 +416,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.info.compressionStatus).toBe(
Expand Down Expand Up @@ -448,6 +457,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.info.compressionStatus).toBe(
Expand Down Expand Up @@ -504,6 +514,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.info.compressionStatus).toBe(CompressionStatus.COMPRESSED);
Expand Down Expand Up @@ -570,6 +581,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

// Verify it compressed
Expand Down Expand Up @@ -636,6 +648,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.newHistory).not.toBeNull();
Expand Down Expand Up @@ -703,6 +716,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.info.compressionStatus).toBe(CompressionStatus.COMPRESSED);
Expand Down Expand Up @@ -760,6 +774,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.info.compressionStatus).toBe(CompressionStatus.COMPRESSED);
Expand Down Expand Up @@ -817,6 +832,7 @@ describe('ChatCompressionService', () => {
mockModel,
mockConfig,
false,
mockAbortSignal,
);

expect(result.info.compressionStatus).toBe(CompressionStatus.COMPRESSED);
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/services/chatCompressionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class ChatCompressionService {
model: string,
config: Config,
hasFailedCompressionAttempt: boolean,
abortSignal?: AbortSignal,
abortSignal: AbortSignal,
): Promise<{ newHistory: Content[] | null; info: ChatCompressionInfo }> {
const curatedHistory = chat.getHistory(true);

Expand Down Expand Up @@ -365,8 +365,7 @@ export class ChatCompressionService {
],
systemInstruction: { text: getCompressionPrompt(config) },
promptId,
// TODO(joshualitt): wire up a sensible abort signal,
abortSignal: abortSignal ?? new AbortController().signal,
abortSignal,
role: LlmRole.UTILITY_COMPRESSOR,
});
const summary = getResponseText(summaryResponse) ?? '';
Expand Down Expand Up @@ -395,7 +394,7 @@ export class ChatCompressionService {
systemInstruction: { text: getCompressionPrompt(config) },
promptId: `${promptId}-verify`,
role: LlmRole.UTILITY_COMPRESSOR,
abortSignal: abortSignal ?? new AbortController().signal,
abortSignal,
});

const finalSummary = (
Expand Down