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.
This commit is contained in:
Resham Joshi 2026-11-06 04:51:31 -08:00
parent 0392c07232
commit 25ea32e85b
7 changed files with 49 additions and 25 deletions

View file

@ -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<SessionSource[]> {
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<string>()
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)

View file

@ -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<string>, providerName: string, fallbackModel = 'cline-auto'): SessionParser {
return {
async *parse(): AsyncGenerator<ParsedProviderCall> {
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
}

View file

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

View file

@ -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<string>()
const calls1: ParsedProviderCall[] = []

View file

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

View file

@ -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<environment_details>\n</environment_details>' }] },
]))
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)

View file

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