From 0bbfc629076eaf13f41803a67f467708c2cd59f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E9=98=BF=E6=B1=9F=28Relakkes?= =?UTF-8?q?=29?= Date: Fri, 17 Jul 2026 18:33:19 +0800 Subject: [PATCH] fix(context): normalize attachments in status analysis (#1022) --- src/utils/analyzeContext.test.ts | 59 ++++++++++++++++++++++++++++++++ src/utils/analyzeContext.ts | 5 ++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/utils/analyzeContext.test.ts diff --git a/src/utils/analyzeContext.test.ts b/src/utils/analyzeContext.test.ts new file mode 100644 index 00000000..76555131 --- /dev/null +++ b/src/utils/analyzeContext.test.ts @@ -0,0 +1,59 @@ +import { afterAll, beforeAll, describe, expect, test } from 'bun:test' +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +let configDir: string +const originalConfigDir = process.env.CLAUDE_CONFIG_DIR +const originalSimpleMode = process.env.CLAUDE_CODE_SIMPLE + +describe('analyzeContextUsage', () => { + beforeAll(async () => { + configDir = await mkdtemp(join(tmpdir(), 'cc-haha-analyze-context-')) + process.env.CLAUDE_CONFIG_DIR = configDir + process.env.CLAUDE_CODE_SIMPLE = '1' + }) + + afterAll(async () => { + if (originalConfigDir === undefined) { + delete process.env.CLAUDE_CONFIG_DIR + } else { + process.env.CLAUDE_CONFIG_DIR = originalConfigDir + } + if (originalSimpleMode === undefined) { + delete process.env.CLAUDE_CODE_SIMPLE + } else { + process.env.CLAUDE_CODE_SIMPLE = originalSimpleMode + } + await rm(configDir, { recursive: true, force: true }) + }) + + test('analyzes attachment messages for the context status view', async () => { + const { analyzeContextUsage } = await import('./analyzeContext.js') + + const result = await analyzeContextUsage( + [ + { + type: 'attachment', + attachment: { type: 'directory', path: configDir }, + uuid: 'issue-1022', + timestamp: '2026-07-17T00:00:00.000Z', + }, + ], + 'claude-sonnet-4-20250514', + async () => ({ mode: 'default' }), + [], + { activeAgents: [], allAgents: [] }, + undefined, + undefined, + undefined, + undefined, + { estimateOnly: true }, + ) + + expect(result.messageBreakdown?.attachmentTokens).toBeGreaterThan(0) + expect(result.messageBreakdown?.attachmentsByType).toEqual([ + { name: 'directory', tokens: expect.any(Number) }, + ]) + }) +}) diff --git a/src/utils/analyzeContext.ts b/src/utils/analyzeContext.ts index 505ed300..12ff8b77 100644 --- a/src/utils/analyzeContext.ts +++ b/src/utils/analyzeContext.ts @@ -60,7 +60,10 @@ import { logForDebugging } from './debug.js' import { isEnvTruthy } from './envUtils.js' import { errorMessage, toError } from './errors.js' import { logError } from './log.js' -import { normalizeMessagesForAPI } from './messages.js' +import { + normalizeAttachmentForAPI, + normalizeMessagesForAPI, +} from './messages.js' import { getRuntimeMainLoopModel } from './model/model.js' import { isFirstPartyAnthropicBaseUrl } from './model/providers.js' import type { SettingSource } from './settings/constants.js'