From 3443a00a439da24ded1001d5689a184cd26b21f0 Mon Sep 17 00:00:00 2001 From: 7Sageer <7sageer@djwcb.cn> Date: Mon, 22 Jun 2026 12:09:29 +0800 Subject: [PATCH] feat(agent-core): add thinkingLevel to compaction telemetry (#954) Report the effective thinking effort (off/low/medium/high/xhigh/max) on compaction_finished, compaction_failed, and micro_compaction_finished events. The value matches the thinking level applied to the compaction provider, so we can analyze how thinking mode affects compaction token usage, duration, and failures. --- packages/agent-core/src/agent/compaction/full.ts | 2 ++ packages/agent-core/src/agent/compaction/micro.ts | 1 + .../agent-core/test/agent/compaction/full.test.ts | 11 ++++++++++- .../agent-core/test/agent/compaction/micro.test.ts | 1 + 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/agent-core/src/agent/compaction/full.ts b/packages/agent-core/src/agent/compaction/full.ts index e444aee52..2d608bae5 100644 --- a/packages/agent-core/src/agent/compaction/full.ts +++ b/packages/agent-core/src/agent/compaction/full.ts @@ -344,6 +344,7 @@ export class FullCompaction { compactedCount: result.compactedCount, retryCount, round, + thinkingLevel: this.agent.config.thinkingLevel, ...usage, ...data, }); @@ -357,6 +358,7 @@ export class FullCompaction { duration: Date.now() - startedAt, round, retryCount, + thinkingLevel: this.agent.config.thinkingLevel, errorType: error instanceof Error ? error.name : 'Unknown', }); if (isKimiError(error) && error.code === ErrorCodes.AUTH_LOGIN_REQUIRED) throw error; diff --git a/packages/agent-core/src/agent/compaction/micro.ts b/packages/agent-core/src/agent/compaction/micro.ts index 65a045a3d..912812f60 100644 --- a/packages/agent-core/src/agent/compaction/micro.ts +++ b/packages/agent-core/src/agent/compaction/micro.ts @@ -90,6 +90,7 @@ export class MicroCompaction { cutoff: nextCutoff, message_count: history.length, cache_age_ms: cacheAgeMs, + thinkingLevel: this.agent.config.thinkingLevel, }); } } diff --git a/packages/agent-core/test/agent/compaction/full.test.ts b/packages/agent-core/test/agent/compaction/full.test.ts index d0ab4062b..85da0ed46 100644 --- a/packages/agent-core/test/agent/compaction/full.test.ts +++ b/packages/agent-core/test/agent/compaction/full.test.ts @@ -240,6 +240,7 @@ describe('FullCompaction', () => { duration: expect.any(Number), compactedCount: 6, retryCount: 0, + thinkingLevel: 'off', inputOther: 520, output: 8, inputCacheRead: 0, @@ -1589,6 +1590,7 @@ describe('FullCompaction', () => { it('preserves thinking effort when compacting after provider context overflow', async () => { let callCount = 0; + const records: TelemetryRecord[] = []; const providerThinkingEfforts: Array[0]['thinkingEffort']> = []; const generate: GenerateFn = async (provider, _system, _tools, _history, callbacks) => { callCount += 1; @@ -1612,7 +1614,7 @@ describe('FullCompaction', () => { } throw new Error(`Unexpected generate call ${String(callCount)}`); }; - const ctx = testAgent({ generate }); + const ctx = testAgent({ generate, telemetry: recordingTelemetry(records) }); ctx.configure({ provider: CATALOGUED_PROVIDER, modelCapabilities: CATALOGUED_MODEL_CAPABILITIES, @@ -1626,6 +1628,13 @@ describe('FullCompaction', () => { expect(callCount).toBe(3); expect(providerThinkingEfforts).toEqual(['high', 'high', 'high']); + expect(records).toContainEqual({ + event: 'compaction_finished', + properties: expect.objectContaining({ + source: 'auto', + thinkingLevel: 'high', + }), + }); }); it('compacts provider overflow when model context size is unknown', async () => { diff --git a/packages/agent-core/test/agent/compaction/micro.test.ts b/packages/agent-core/test/agent/compaction/micro.test.ts index 34254d58c..91be825d1 100644 --- a/packages/agent-core/test/agent/compaction/micro.test.ts +++ b/packages/agent-core/test/agent/compaction/micro.test.ts @@ -482,6 +482,7 @@ describe('MicroCompaction', () => { truncatedToolResultTokensAfter: expect.any(Number), tokensBefore: expect.any(Number), tokensAfter: expect.any(Number), + thinkingLevel: 'off', }); expect(numberProperty(event, 'truncatedToolResultTokensBefore')).toBeGreaterThan( numberProperty(event, 'truncatedToolResultTokensAfter'),