From 687ca2f19c1c8eef62ebe708d5af7589b096ce38 Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Thu, 8 Oct 2026 05:48:17 +0200 Subject: [PATCH] fix(kimicode): use state.json cwd when workDir is absent (#1688) Kimi Code now writes cwd instead of workDir. Without it the project path was decoded from the wd_ directory name, so /x/crewroom-app-mem5 became crewroom/app/mem5 and never joined its repository. Cost is unchanged; only the per-project split moves. --- src/daily-cache.ts | 5 ++++- src/providers/kimicode.ts | 7 ++++--- src/session-cache.ts | 4 +++- tests/providers/kimicode.test.ts | 24 ++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/daily-cache.ts b/src/daily-cache.ts index b6ac661b..b6db9c3b 100644 --- a/src/daily-cache.ts +++ b/src/daily-cache.ts @@ -328,7 +328,10 @@ import type { DateRange, ProjectSummary } from './types.js' // label (e.g. every home-folder Claude session) to whichever project owned it. // Day and provider totals are unchanged, only the split inside them moves, so // no PENDING_REDERIVE_PROVIDER_VERSIONS entry is needed. -export const DAILY_CACHE_VERSION = 67 +// v68: Kimi Code sessions whose state.json carries `cwd` but no `workDir` land +// on that folder. Only the per-project split moves; cost, tokens and calls are +// unchanged, so the bump re-derives surviving days. +export const DAILY_CACHE_VERSION = 68 const MIN_SUPPORTED_VERSION = 28 /// Providers whose per-day CALL COUNT means something different at diff --git a/src/providers/kimicode.ts b/src/providers/kimicode.ts index d7f61be2..fc50d825 100644 --- a/src/providers/kimicode.ts +++ b/src/providers/kimicode.ts @@ -168,11 +168,12 @@ async function discoverSources(root: string): Promise { const agents = (await mapWithConcurrency(sessionDirs, FS_SCAN_CONCURRENCY, async sd => { const state = await readState(sd.sessionDir) - const project = projectFromWorkDir(state.workDir ?? '', sd.workDirKey) + const workDir = state.workDir ?? state.cwd + const project = projectFromWorkDir(workDir ?? '', sd.workDirKey) const agentsDir = join(sd.sessionDir, 'agents') return (await directoryEntries(agentsDir)) .filter(e => e.isDirectory()) - .map(e => ({ agentName: e.name, wirePath: join(agentsDir, e.name, 'wire.jsonl'), project, workDir: state.workDir })) + .map(e => ({ agentName: e.name, wirePath: join(agentsDir, e.name, 'wire.jsonl'), project, workDir })) })).flat() const present = await mapWithConcurrency(agents, FS_SCAN_CONCURRENCY, a => isFile(a.wirePath)) @@ -285,7 +286,7 @@ function createParser(source: SessionSource, seenKeys: Set): SessionPars const agentId = source.sourceId || agentIdForWire(source.path) const state = await readState(sessionDir) const fallbackTimestamp = timestampIso(state.updatedAt) || timestampIso(state.createdAt) - const projectPath = state.workDir || source.sourcePath + const projectPath = state.workDir || state.cwd || source.sourcePath const aliasModels = new Map() const prompts = new Map() let currentPrompt = '' diff --git a/src/session-cache.ts b/src/session-cache.ts index 5508eecd..68548701 100644 --- a/src/session-cache.ts +++ b/src/session-cache.ts @@ -586,7 +586,9 @@ export const PROVIDER_PARSE_VERSIONS: Record = { // sessions without the lineage field gain it. The field is purely // additive; every cost / token / call total is byte-identical to a build // that omits it. - kimicode: 'wire-usage-v1-est-cost-session-lineage-capture-v1', + // cwd-project-v1: sessions whose state.json has `cwd` but no `workDir` land + // on that folder instead of a path decoded from the wd_ directory name. + kimicode: 'wire-usage-v1-est-cost-session-lineage-capture-v1-cwd-project-v1', // archived-subtree-v1: KiloCode shares the SQLite parser and the same schema. // billing-routes-v2: its warm cache must move with both shared route fields. 'kilo-code': 'worktree-project-grouping-v1-session-model-v1-archived-subtree-v1-billing-routes-v2-v2-legacy-union-v1-unknown-usage-v1-vertex-fallback-cost-v1', diff --git a/tests/providers/kimicode.test.ts b/tests/providers/kimicode.test.ts index 2ff426ad..5d04ad9d 100644 --- a/tests/providers/kimicode.test.ts +++ b/tests/providers/kimicode.test.ts @@ -167,6 +167,30 @@ describe('Kimi Code provider', () => { expect(sources.every(source => source.sourcePath === '/workspace/neutral-project')).toBe(true) }) + it('falls back to state.json cwd when workDir is absent', async () => { + const agentDir = join(fixtureHome, 'sessions', 'wd_neutral-project-wt1_0123456789ab', 'session_cwd', 'agents', 'main') + await mkdir(agentDir, { recursive: true }) + await writeFile(join(agentDir, '..', '..', 'state.json'), JSON.stringify({ + createdAt: '2026-07-01T10:00:00.000Z', + updatedAt: '2026-07-01T10:05:00.000Z', + cwd: '/workspace/neutral-project-wt1', + })) + await writeFile(join(agentDir, 'wire.jsonl'), [ + JSON.stringify({ type: 'metadata', protocol_version: '1.4', created_at: 1782900000000 }), + JSON.stringify(prompt('hello', 1782900000000)), + JSON.stringify(request('0.1', 'k3-agent', 'k3-agent', 1782900001000)), + JSON.stringify(usage('k3-agent', 1782900002000, { input: 100, output: 50 })), + '', + ].join('\n')) + + const provider = createKimicodeProvider(fixtureHome) + const [source] = await provider.discoverSessions() + expect(source?.project).toBe('neutral-project-wt1') + expect(source?.sourcePath).toBe('/workspace/neutral-project-wt1') + const calls = await collect(provider, source!, new Set()) + expect(calls.map(call => call.projectPath)).toEqual(['/workspace/neutral-project-wt1']) + }) + it('discovers embedded-runtime conv-* and ctitle-* session directories', async () => { // Embedded runtimes (Kimi desktop app, Kimi Code IDE) name session dirs // conv-*/ctitle-* instead of session_*; their wires carry the same event