From 25ea32e85bfa8cb82a7f777a2f984a9539039a72 Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 6 Nov 2026 04:51:31 -0800 Subject: [PATCH] fix(cline): key Cline-family tasks on ui_messages.json, not the task directory Cline rewrites ui_messages.json in place, which never moves the task directory's mtime or size, so a growing task kept serving its first cached snapshot. The source is now the file itself; the new path re-parses every task once on its own, so no parse-version bump is needed. --- src/providers/cline.ts | 6 +++--- src/providers/vscode-cline-parser.ts | 10 +++++---- tests/providers/cline.test.ts | 20 ++++++++--------- tests/providers/ibm-bob.test.ts | 6 +++--- tests/providers/kilo-code.test.ts | 2 +- tests/providers/vscode-cline-parser.test.ts | 6 +++--- tests/reported-cost-cache.test.ts | 24 ++++++++++++++++++++- 7 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/providers/cline.ts b/src/providers/cline.ts index 3d9d9844..8b61c6df 100644 --- a/src/providers/cline.ts +++ b/src/providers/cline.ts @@ -1,6 +1,6 @@ import { stat } from 'fs/promises' import { homedir } from 'os' -import { basename, join } from 'path' +import { basename, dirname, join } from 'path' import { discoverClineTasks, createClineParser, clineTaskRoots } from './vscode-cline-parser.js' import type { ProbeRoot, Provider, SessionSource, SessionParser } from './types.js' @@ -20,14 +20,14 @@ function normalizeOverrideDirs(overrideDirs?: string | string[]): string[] | und async function dedupeTaskSources(sources: SessionSource[]): Promise { const candidates = await Promise.all(sources.map(async source => ({ source, - mtimeMs: (await stat(join(source.path, 'ui_messages.json')).catch(() => null))?.mtimeMs ?? 0, + mtimeMs: (await stat(source.path).catch(() => null))?.mtimeMs ?? 0, }))) const seenTaskIds = new Set() const deduped: SessionSource[] = [] for (const { source } of candidates.sort((a, b) => b.mtimeMs - a.mtimeMs)) { - const taskId = basename(source.path) + const taskId = basename(dirname(source.path)) if (seenTaskIds.has(taskId)) continue seenTaskIds.add(taskId) deduped.push(source) diff --git a/src/providers/vscode-cline-parser.ts b/src/providers/vscode-cline-parser.ts index 6837ae96..ce3032b2 100644 --- a/src/providers/vscode-cline-parser.ts +++ b/src/providers/vscode-cline-parser.ts @@ -1,5 +1,5 @@ import { readdir, readFile, stat } from 'fs/promises' -import { basename, join, posix, win32 } from 'path' +import { basename, dirname, join, posix, win32 } from 'path' import { homedir } from 'os' import { calculateCost } from '../models.js' @@ -89,7 +89,9 @@ async function discoverClineTasksInBaseDir(baseDir: string, providerName: string const uiStat = await stat(uiPath).catch(() => null) if (!uiStat?.isFile()) continue - sources.push({ path: taskDir, project: displayName, provider: providerName }) + // The file, not the task directory: Cline rewrites ui_messages.json in + // place, which never moves the directory's own mtime or size. + sources.push({ path: uiPath, project: displayName, provider: providerName }) } return sources @@ -135,12 +137,12 @@ function workspaceToProject(workspace: string): string { export function createClineParser(source: SessionSource, seenKeys: Set, providerName: string, fallbackModel = 'cline-auto'): SessionParser { return { async *parse(): AsyncGenerator { - const taskDir = source.path + const taskDir = dirname(source.path) const taskId = basename(taskDir) let uiRaw: string try { - uiRaw = await readFile(join(taskDir, 'ui_messages.json'), 'utf-8') + uiRaw = await readFile(source.path, 'utf-8') } catch { return } diff --git a/tests/providers/cline.test.ts b/tests/providers/cline.test.ts index d692fac0..829f47ac 100644 --- a/tests/providers/cline.test.ts +++ b/tests/providers/cline.test.ts @@ -65,8 +65,8 @@ describe('cline provider - discovery', () => { expect(sessions.map(s => s.provider)).toEqual(['cline', 'cline']) expect(sessions.map(s => s.project)).toEqual(['Cline', 'Cline']) expect(sessions.map(s => s.path).sort()).toEqual([ - join(homeDataDir, 'tasks', 'task-home'), - join(vscodeDir, 'tasks', 'task-vscode'), + join(homeDataDir, 'tasks', 'task-home', 'ui_messages.json'), + join(vscodeDir, 'tasks', 'task-vscode', 'ui_messages.json'), ].sort()) }) @@ -82,7 +82,7 @@ describe('cline provider - discovery', () => { const sessions = await provider.discoverSessions() expect(sessions).toHaveLength(1) - expect(sessions[0]!.path).toBe(newTask) + expect(sessions[0]!.path).toBe(join(newTask, 'ui_messages.json')) }) it('skips task directories without ui_messages.json', async () => { @@ -127,9 +127,9 @@ describe('cline provider - default roots', () => { expect(sessions).toHaveLength(3) expect(sessions.every(s => s.provider === 'cline')).toBe(true) expect(sessions.map(s => s.path).sort()).toEqual([ - join(stableRoot!, 'tasks', 'task-stable'), - join(insidersRoot!, 'tasks', 'task-insiders'), - join(codiumRoot!, 'tasks', 'task-codium'), + join(stableRoot!, 'tasks', 'task-stable', 'ui_messages.json'), + join(insidersRoot!, 'tasks', 'task-insiders', 'ui_messages.json'), + join(codiumRoot!, 'tasks', 'task-codium', 'ui_messages.json'), ].sort()) }) @@ -143,8 +143,8 @@ describe('cline provider - default roots', () => { const sessions = await createClineProvider().discoverSessions() expect(sessions.map(s => s.path).sort()).toEqual([ - join(stableRoot, 'tasks', 'task-vscode'), - join(clineDataDir, 'tasks', 'task-home'), + join(stableRoot, 'tasks', 'task-vscode', 'ui_messages.json'), + join(clineDataDir, 'tasks', 'task-home', 'ui_messages.json'), ].sort()) }) @@ -160,7 +160,7 @@ describe('cline provider - default roots', () => { const sessions = await createClineProvider().discoverSessions() expect(sessions).toHaveLength(1) - expect(sessions[0]!.path).toBe(insidersTask) + expect(sessions[0]!.path).toBe(join(insidersTask, 'ui_messages.json')) }) }) @@ -182,7 +182,7 @@ describe('cline provider - parsing', () => { cost: 0.07, }) - const source = { path: taskDir, project: 'Cline', provider: 'cline' } + const source = { path: join(taskDir, 'ui_messages.json'), project: 'Cline', provider: 'cline' } const calls: ParsedProviderCall[] = [] for await (const call of cline.createSessionParser(source, new Set()).parse()) calls.push(call) diff --git a/tests/providers/ibm-bob.test.ts b/tests/providers/ibm-bob.test.ts index d61f92e3..7eaac866 100644 --- a/tests/providers/ibm-bob.test.ts +++ b/tests/providers/ibm-bob.test.ts @@ -98,7 +98,7 @@ describe('ibm-bob provider - discovery and parsing', () => { })) await writeFile(join(taskDir, 'api_conversation_history.json'), makeApiHistory('anthropic/claude-sonnet-4-6')) - const source = { path: taskDir, project: 'IBM Bob', provider: 'ibm-bob' } + const source = { path: join(taskDir, 'ui_messages.json'), project: 'IBM Bob', provider: 'ibm-bob' } const calls: ParsedProviderCall[] = [] for await (const call of ibmBob.createSessionParser(source, new Set()).parse()) calls.push(call) @@ -123,7 +123,7 @@ describe('ibm-bob provider - discovery and parsing', () => { await writeFile(join(taskDir, 'ui_messages.json'), makeUiMessages({ tokensIn: 100, tokensOut: 50 })) await writeFile(join(taskDir, 'api_conversation_history.json'), makeApiHistory()) - const source = { path: taskDir, project: 'IBM Bob', provider: 'ibm-bob' } + const source = { path: join(taskDir, 'ui_messages.json'), project: 'IBM Bob', provider: 'ibm-bob' } const calls: ParsedProviderCall[] = [] for await (const call of ibmBob.createSessionParser(source, new Set()).parse()) calls.push(call) @@ -137,7 +137,7 @@ describe('ibm-bob provider - discovery and parsing', () => { await mkdir(taskDir, { recursive: true }) await writeFile(join(taskDir, 'ui_messages.json'), makeUiMessages({ tokensIn: 100, tokensOut: 50 })) - const source = { path: taskDir, project: 'IBM Bob', provider: 'ibm-bob' } + const source = { path: join(taskDir, 'ui_messages.json'), project: 'IBM Bob', provider: 'ibm-bob' } const seenKeys = new Set() const calls1: ParsedProviderCall[] = [] diff --git a/tests/providers/kilo-code.test.ts b/tests/providers/kilo-code.test.ts index 13385eae..9e5f3bc9 100644 --- a/tests/providers/kilo-code.test.ts +++ b/tests/providers/kilo-code.test.ts @@ -39,7 +39,7 @@ describe('kilo-code provider - discovery path differentiation', () => { { type: 'say', say: 'api_req_started', text: JSON.stringify({ tokensIn: 200, tokensOut: 100 }), ts: 1700000000000 }, ])) - const source = { path: task, project: 'task-kilo-2', provider: 'kilo-code' } + const source = { path: join(task, 'ui_messages.json'), project: 'task-kilo-2', provider: 'kilo-code' } const calls: ParsedProviderCall[] = [] for await (const call of kiloCode.createSessionParser(source, new Set()).parse()) calls.push(call) diff --git a/tests/providers/vscode-cline-parser.test.ts b/tests/providers/vscode-cline-parser.test.ts index 83520462..401e618a 100644 --- a/tests/providers/vscode-cline-parser.test.ts +++ b/tests/providers/vscode-cline-parser.test.ts @@ -51,8 +51,8 @@ describe('VS Code Cline-family storage discovery', () => { expect(sessions).toHaveLength(2) expect(sessions.map(s => s.path).sort()).toEqual([ - join(codeRoot, 'tasks', 'task-code'), - join(codiumRoot, 'tasks', 'task-codium'), + join(codeRoot, 'tasks', 'task-code', 'ui_messages.json'), + join(codiumRoot, 'tasks', 'task-codium', 'ui_messages.json'), ].sort()) }) }) @@ -73,7 +73,7 @@ describe('VS Code Cline-family parse hardening', () => { { role: 'user', content: [{ type: 'text', text: 'hi\n\n' }] }, ])) - const source = { path: taskDir, project: 'p', provider: 'cline' } + const source = { path: join(taskDir, 'ui_messages.json'), project: 'p', provider: 'cline' } const calls: ParsedProviderCall[] = [] for await (const call of createClineParser(source, new Set(), 'cline').parse()) calls.push(call) diff --git a/tests/reported-cost-cache.test.ts b/tests/reported-cost-cache.test.ts index fee8fce9..aab1b125 100644 --- a/tests/reported-cost-cache.test.ts +++ b/tests/reported-cost-cache.test.ts @@ -12,7 +12,7 @@ // modules are first evaluated, so HOME must be set before any import. import { afterAll, beforeEach, expect, it, vi } from 'vitest' -import { mkdir, rm, writeFile } from 'fs/promises' +import { mkdir, rm, stat, writeFile } from 'fs/promises' import { mkdirSync } from 'fs' import { join } from 'path' import { createRequire } from 'node:module' @@ -280,3 +280,25 @@ it('keeps a Cline request cost cold and warm, and re-prices requests without one expect(cold).toBeCloseTo(0.5 + floor, 10) expect(warm).toBeCloseTo(cold, 10) }) + +it('re-reads a Cline task whose ui_messages.json is rewritten in place', async () => { + const taskDir = join(HOME, '.cline', 'data', 'tasks', 'growing') + await mkdir(taskDir, { recursive: true }) + const request = { type: 'say', say: 'api_req_started', ts: Date.UTC(2026, 7, 16, 10), text: JSON.stringify({ tokensIn: 1000, tokensOut: 200 }) } + const uiPath = join(taskDir, 'ui_messages.json') + await writeFile(uiPath, JSON.stringify([request])) + + const { clearSessionCache, parseAllSessions } = await import('../src/parser.js') + const calls = async () => (await parseAllSessions(undefined, 'cline')).reduce((s, p) => s + p.totalApiCalls, 0) + clearSessionCache() + expect(await calls()).toBe(1) + + // Cline saves with an in-place writeFile: the task directory's entries, and + // so its own mtime and size, stay exactly as they were. + const dirBefore = await stat(taskDir) + await writeFile(uiPath, JSON.stringify([request, request, request])) + expect((await stat(taskDir)).mtimeMs).toBe(dirBefore.mtimeMs) + + clearSessionCache() + expect(await calls()).toBe(3) +})