mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-08-01 04:15:53 +00:00
* fix(cursor): use Cursor's real context tokens for input
Current Cursor builds leave the per-bubble tokenCount at {0,0}, so the provider
fell back to estimating input from visible text plus a second agentKv
content-char pass that double-counted the same conversation. Cursor records its
own tokenizer-accurate context size per conversation in
composerData.promptTokenBreakdown (the number behind the in-app context-window
bar); read that and credit it once per conversation for input instead.
Measured on a real local DB: today's Cursor input went 44,873 -> 168,486 tokens,
matching the sum of per-conversation context. The admin portal still counts
cumulative-per-turn plus cache, which are server-side only, so an opt-in Cursor
API stays the path to exact parity.
Output is a reply-text estimate; agentKv is retained for a tools/bash breakdown
in a follow-up.
* feat(cursor): add tools and bash-command breakdown from agentKv
Cursor logs the agent's tool calls (Read, Grep, Glob, Shell, ...) in agentKv
blobs. Join them to conversations via the turn requestId (carried on the bubble's
$.requestId and inherited positionally by the turn's agentKv rows) and attach each
conversation's tool list and Shell commands to the call that carries its input.
Measured on a real DB: Cursor now reports tools {Read, Grep, Glob, Shell,
SemanticSearch} and the executed shell commands, which were previously empty.
Removes the now-superseded parseAgentKv content-char estimate.
* fix(cursor): price composer-2.5 as Sonnet 4.6
composer-2.5 was missing from the built-in Cursor model aliases, so its usage
showed $0. Map it to claude-sonnet-4-6 like composer-2 (per cursor.com/blog).
* fix(cursor): cache version, model attribution, user message join, tool classification
- Bump CURSOR_CACHE_VERSION to 5: parser semantics changed (parseAgentKv
removed, real context tokens from composerData.promptTokenBreakdown),
stale v4 caches would show double-counted agentKv calls.
- Fix model attribution: real input tokens are credited on user bubbles
(type=1) which carry no modelInfo. Add a pre-pass building composerId ->
model from assistant bubbles so pricing/display uses the conversation's
actual model instead of the default cursor-auto/sonnet-4.5.
- Fix buildUserMessageMap: was keying by JSON conversationId (empty in
current Cursor builds). Now extracts composerId from the bubble key,
matching parseBubbles.
- Add 'Shell' to BASH_TOOLS in classifier: Cursor's agent uses 'Shell'
as the tool name, but it was missing from the bash tool set so Cursor
agent turns with shell commands wouldn't classify as bash/build/test.
- Fix null coalescing in loadComposerInputTokens: r.used ?? r.ctx would
fall through on a valid totalUsedTokens of 0. Use explicit null check.
- Decouple agentTools attachment from input credit: tools/bash were only
attached on the first credited turn (creditedHere), silently dropping
tool usage from subsequent turns in multi-turn conversations.
- Update stale comment about parseAgentKv being kept for a follow-up.
- Add tests for real token crediting, once-per-conversation, fallback,
contextTokensUsed, tool/bash attribution, and model attribution.
* fix(cursor): avoid duplicating aggregated agent tools
* fix(cursor): price house composer models from Cursor's published rates
composer-1/1.5/2/2.5 were proxied to Claude Sonnet, overcounting cost
(~6x for composer-2/2.5). Use Cursor's published per-model rates instead,
and note in the parser why local reads undercount the admin console.
Co-authored-by: AgentSeal <hello@agentseal.org>
* fix(cursor): estimate non-Composer turn input from the agent stream
Non-Composer sessions (e.g. GPT) record no context-window meter and keep
the prompt in the agent stream, so the user bubble's own text is empty.
Those turns hit the 0/0-token fallback with text_length 0 and were dropped
entirely, so that model's traffic never appeared in the report.
loadAgentToolsByComposer now also sums the user-role stream text length per
conversation, and the meterless fallback estimates input from it (chars/4),
credited once per conversation, when the bubble text is empty. Turns with no
stream text are left untouched, so no phantom tokens are invented.
* fix(cursor): stable conversation crediting, restored stream coverage, and cache invalidation
Review fixes for the real-token accounting:
Conversation input now lands on one composer-anchored record
(cursor:composer-input:<id>) timestamped at composerData.createdAt, so
the credited day no longer depends on the parse window or cache floors,
daily-cache gap fills dedupe instead of multiplying, and each
conversation picks exactly one input source (real bubble tokenCounts,
the context meter, the agent stream, or visible text) so sources can
never stack or double count. A zero totalUsedTokens no longer shadows
contextTokensUsed.
The agent stream regained what the parseAgentKv removal dropped: tool
and system rows count as context, stream-only replies count as output,
and sessions with no bubble join are emitted again (DB mtime timestamp,
as before). Block-array content is measured by its text, not its JSON
envelope. Rows written before their requestId appears buffer forward
instead of inheriting the previous conversation, and a system row closes
the boundary. Tool names canonicalize to Bash and commands go through
extractBashCommands so cross-provider breakdowns merge; the classifier
no longer special-cases Shell (which also reclassified Copilot turns).
User bubbles consume their own queue entry so assistant replies pair
with the right question, and every cursor call is flagged
costIsEstimated.
The requestId and model joins ride the existing budgeted bubble scan
instead of two new unbounded full-table decodes, and the composerData
read seeks the key range. SQLITE_BUSY now propagates to the parser's
retry path instead of caching a silently degraded parse.
Upgrades actually take effect: the session cache gets a cursor parse
version, DAILY_CACHE_VERSION bumps to 10 so finalized days re-hydrate
under the new accounting, the cursor results cache bumps to v6, and the
builtin composer rates participate in the price config hash (rates now
cite cursor.com/docs/models).
Verified against a real Cursor store: all metered conversations match
the on-disk meter exactly, narrow and wide parse windows anchor
identically, repeat runs are byte-identical, and agentKv-only sessions
reappear.
---------
Co-authored-by: ozymandiashh <234437643+ozymandiashh@users.noreply.github.com>
109 lines
3.5 KiB
TypeScript
109 lines
3.5 KiB
TypeScript
import { readFile, writeFile, mkdir, rename, stat, unlink } from 'fs/promises'
|
|
import { join } from 'path'
|
|
import { homedir } from 'os'
|
|
import { randomBytes } from 'crypto'
|
|
|
|
import type { ParsedProviderCall } from './providers/types.js'
|
|
|
|
// Bumped to 3 for the workspace-aware breakdown change: the cursor parser
|
|
// now derives `sessionId` from the bubble row key (the real composer id)
|
|
// rather than the empty `conversationId` JSON field, and the workspace
|
|
// router relies on those composer ids to bucket calls per project.
|
|
// Version 2 caches contain `sessionId: 'unknown'` for every call and would
|
|
// route everything to the orphan project, so we invalidate them.
|
|
// Version 5: parseAgentKv was removed (it double-counted against bubbles);
|
|
// real context tokens from composerData.promptTokenBreakdown now drive
|
|
// input, and agentKv is used only for the tools/bash breakdown. Cached v4
|
|
// results contain stale agentKv calls and lack the real token figures.
|
|
// Version 6: conversation input moved to composer-anchored records
|
|
// (cursor:composer-input:<id>) with per-conversation source selection, the
|
|
// agent stream regained tool/system context and stream-only sessions, and
|
|
// tool names are canonicalized. v5 results mix crediting regimes.
|
|
const CURSOR_CACHE_VERSION = 6
|
|
|
|
type ResultCache = {
|
|
version?: number
|
|
dbMtimeMs: number
|
|
dbSizeBytes: number
|
|
lookbackFloor: string
|
|
calls: ParsedProviderCall[]
|
|
}
|
|
|
|
const CACHE_FILE = 'cursor-results.json'
|
|
|
|
function getCacheDir(): string {
|
|
return join(homedir(), '.cache', 'codeburn')
|
|
}
|
|
|
|
function getCachePath(): string {
|
|
return join(getCacheDir(), CACHE_FILE)
|
|
}
|
|
|
|
async function getDbFingerprint(dbPath: string): Promise<{ mtimeMs: number; size: number } | null> {
|
|
try {
|
|
const s = await stat(dbPath)
|
|
return { mtimeMs: s.mtimeMs, size: s.size }
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export async function readCachedResults(
|
|
dbPath: string,
|
|
requestedFloor: string,
|
|
): Promise<ParsedProviderCall[] | null> {
|
|
try {
|
|
const fp = await getDbFingerprint(dbPath)
|
|
if (!fp) return null
|
|
|
|
const raw = await readFile(getCachePath(), 'utf-8')
|
|
const cache = JSON.parse(raw) as ResultCache
|
|
|
|
if (
|
|
cache.version === CURSOR_CACHE_VERSION &&
|
|
cache.dbMtimeMs === fp.mtimeMs &&
|
|
cache.dbSizeBytes === fp.size &&
|
|
typeof cache.lookbackFloor === 'string' &&
|
|
cache.lookbackFloor <= requestedFloor
|
|
) {
|
|
return cache.calls
|
|
}
|
|
return null
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export async function writeCachedResults(
|
|
dbPath: string,
|
|
calls: ParsedProviderCall[],
|
|
lookbackFloor: string,
|
|
): Promise<void> {
|
|
const fp = await getDbFingerprint(dbPath)
|
|
if (!fp) return
|
|
|
|
const dir = getCacheDir()
|
|
await mkdir(dir, { recursive: true }).catch(() => {})
|
|
const cache: ResultCache = {
|
|
version: CURSOR_CACHE_VERSION,
|
|
dbMtimeMs: fp.mtimeMs,
|
|
dbSizeBytes: fp.size,
|
|
lookbackFloor,
|
|
calls,
|
|
}
|
|
|
|
// Atomic write: stage to a randomized temp file in the same directory,
|
|
// then rename onto the final path. rename() is atomic on POSIX, so a
|
|
// crash mid-write never leaves a half-written cache, and concurrent
|
|
// CLI invocations using their own random temp names cannot interleave
|
|
// bytes in the destination file (they only race on the final rename,
|
|
// last-writer-wins, both with valid content).
|
|
const target = getCachePath()
|
|
const tempPath = `${target}.${randomBytes(8).toString('hex')}.tmp`
|
|
try {
|
|
await writeFile(tempPath, JSON.stringify(cache), 'utf-8')
|
|
await rename(tempPath, target)
|
|
} catch {
|
|
await unlink(tempPath).catch(() => {})
|
|
}
|
|
}
|