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.
This commit is contained in:
Resham Joshi 2026-10-08 05:48:17 +02:00 • committed by GitHub
parent fce75d5420
commit 687ca2f19c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 5 deletions

View file

@ -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

View file

@ -168,11 +168,12 @@ async function discoverSources(root: string): Promise<SessionSource[]> {
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<string>): 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<string, string>()
const prompts = new Map<string, string>()
let currentPrompt = ''

View file

@ -586,7 +586,9 @@ export const PROVIDER_PARSE_VERSIONS: Record<string, string> = {
// 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',

View file

@ -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