chompyy 0.4.7 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -6
- package/dist/advice.js +2 -1
- package/dist/agentlimits.js +77 -0
- package/dist/agentusage.js +96 -0
- package/dist/appstate.js +72 -5
- package/dist/away.js +44 -5
- package/dist/blueprint.js +3 -7
- package/dist/board.js +4 -0
- package/dist/cli.js +328 -15
- package/dist/codexquota.js +106 -0
- package/dist/codexusage.js +10 -0
- package/dist/config.js +2 -0
- package/dist/context.js +30 -0
- package/dist/costs.js +1 -0
- package/dist/daytime.js +68 -0
- package/dist/demon.js +20 -0
- package/dist/didwork.js +46 -0
- package/dist/label.js +33 -0
- package/dist/learning.js +35 -0
- package/dist/menu.js +17 -0
- package/dist/nightcap.js +60 -0
- package/dist/nudge.js +47 -18
- package/dist/pacing.js +16 -1
- package/dist/questions.js +71 -0
- package/dist/recap.js +74 -0
- package/dist/receipt.js +48 -0
- package/dist/recipe.js +79 -0
- package/dist/send.js +46 -20
- package/dist/stamps.js +14 -0
- package/dist/taste.js +67 -7
- package/dist/triggers.js +5 -0
- package/dist/unkeep.js +220 -0
- package/dist/usage.js +21 -1
- package/dist/vendor/codeburn/antigravity.js +309 -0
- package/dist/vendor/codeburn/codex.js +429 -0
- package/dist/vendor/codeburn/copilot.js +292 -0
- package/dist/vendor/codeburn/gemini.js +195 -0
- package/dist/vendor/codeburn/grokbot.js +182 -0
- package/dist/vendor/codeburn/kimi.js +178 -0
- package/dist/vendor/codeburn/shims/chalk.js +14 -0
- package/dist/vendor/codeburn/shims/strip-ansi.js +6 -0
- package/dist/vendor/codeburn/src/act/backup.js +83 -0
- package/dist/vendor/codeburn/src/act/model-defaults.js +151 -0
- package/dist/vendor/codeburn/src/act/types.js +37 -0
- package/dist/vendor/codeburn/src/bash-utils.js +204 -0
- package/dist/vendor/codeburn/src/behavioral-weight.js +47 -0
- package/dist/vendor/codeburn/src/cache-dir.js +26 -0
- package/dist/vendor/codeburn/src/cache-refresh-lock.js +543 -0
- package/dist/vendor/codeburn/src/cache-sweep.js +67 -0
- package/dist/vendor/codeburn/src/classifier.js +209 -0
- package/dist/vendor/codeburn/src/claude-identity.js +223 -0
- package/dist/vendor/codeburn/src/codex-cache.js +377 -0
- package/dist/vendor/codeburn/src/codex-fork-replay.js +232 -0
- package/dist/vendor/codeburn/src/codex-jsonl-fields.js +99 -0
- package/dist/vendor/codeburn/src/codex-session-index.js +113 -0
- package/dist/vendor/codeburn/src/codex-throughput.js +544 -0
- package/dist/vendor/codeburn/src/compare-stats.js +405 -0
- package/dist/vendor/codeburn/src/config.js +145 -0
- package/dist/vendor/codeburn/src/content-utils.js +56 -0
- package/dist/vendor/codeburn/src/context-tree-codex.js +329 -0
- package/dist/vendor/codeburn/src/context-tree.js +664 -0
- package/dist/vendor/codeburn/src/copilot-aiu.js +16 -0
- package/dist/vendor/codeburn/src/currency.js +189 -0
- package/dist/vendor/codeburn/src/cursor-cache.js +238 -0
- package/dist/vendor/codeburn/src/cursor-import.js +402 -0
- package/dist/vendor/codeburn/src/cursor-sync.js +256 -0
- package/dist/vendor/codeburn/src/daily-cache.js +2036 -0
- package/dist/vendor/codeburn/src/data/litellm-snapshot.json +1 -0
- package/dist/vendor/codeburn/src/data/pricing-fallback.json +1 -0
- package/dist/vendor/codeburn/src/day-aggregator.js +327 -0
- package/dist/vendor/codeburn/src/editor-data-dir.js +11 -0
- package/dist/vendor/codeburn/src/fetch-utils.js +18 -0
- package/dist/vendor/codeburn/src/format.js +118 -0
- package/dist/vendor/codeburn/src/fs-utils.js +270 -0
- package/dist/vendor/codeburn/src/git-origin.js +334 -0
- package/dist/vendor/codeburn/src/granular-history.js +328 -0
- package/dist/vendor/codeburn/src/hermes-session-ledger.js +512 -0
- package/dist/vendor/codeburn/src/launcher-homes.js +142 -0
- package/dist/vendor/codeburn/src/live-sessions.js +358 -0
- package/dist/vendor/codeburn/src/math-utils.js +8 -0
- package/dist/vendor/codeburn/src/menubar-json.js +294 -0
- package/dist/vendor/codeburn/src/models.js +1879 -0
- package/dist/vendor/codeburn/src/optimize-one-hour-cache.js +92 -0
- package/dist/vendor/codeburn/src/optimize.js +4110 -0
- package/dist/vendor/codeburn/src/parse-workers.js +219 -0
- package/dist/vendor/codeburn/src/parser.js +6953 -0
- package/dist/vendor/codeburn/src/path-privacy.js +26 -0
- package/dist/vendor/codeburn/src/plans.js +135 -0
- package/dist/vendor/codeburn/src/pr-signals.js +132 -0
- package/dist/vendor/codeburn/src/project-filter-warnings.js +33 -0
- package/dist/vendor/codeburn/src/provider-issues.js +30 -0
- package/dist/vendor/codeburn/src/providers/amp.js +286 -0
- package/dist/vendor/codeburn/src/providers/antigravity.js +1731 -0
- package/dist/vendor/codeburn/src/providers/claude.js +551 -0
- package/dist/vendor/codeburn/src/providers/cline-cli.js +401 -0
- package/dist/vendor/codeburn/src/providers/cline.js +63 -0
- package/dist/vendor/codeburn/src/providers/codebuff.js +336 -0
- package/dist/vendor/codeburn/src/providers/codewhale.js +442 -0
- package/dist/vendor/codeburn/src/providers/codex.js +1720 -0
- package/dist/vendor/codeburn/src/providers/command-code.js +196 -0
- package/dist/vendor/codeburn/src/providers/copilot.js +3278 -0
- package/dist/vendor/codeburn/src/providers/crush.js +226 -0
- package/dist/vendor/codeburn/src/providers/cursor-agent.js +837 -0
- package/dist/vendor/codeburn/src/providers/cursor.js +1163 -0
- package/dist/vendor/codeburn/src/providers/devin.js +569 -0
- package/dist/vendor/codeburn/src/providers/droid.js +324 -0
- package/dist/vendor/codeburn/src/providers/dsh.js +690 -0
- package/dist/vendor/codeburn/src/providers/forge.js +245 -0
- package/dist/vendor/codeburn/src/providers/gemini.js +230 -0
- package/dist/vendor/codeburn/src/providers/goose.js +252 -0
- package/dist/vendor/codeburn/src/providers/grok.js +581 -0
- package/dist/vendor/codeburn/src/providers/grokbot.js +290 -0
- package/dist/vendor/codeburn/src/providers/hermes.js +771 -0
- package/dist/vendor/codeburn/src/providers/ibm-bob.js +53 -0
- package/dist/vendor/codeburn/src/providers/index.js +360 -0
- package/dist/vendor/codeburn/src/providers/kilo-code.js +50 -0
- package/dist/vendor/codeburn/src/providers/kimi.js +363 -0
- package/dist/vendor/codeburn/src/providers/kimicode.js +402 -0
- package/dist/vendor/codeburn/src/providers/kiro.js +1174 -0
- package/dist/vendor/codeburn/src/providers/lingtai-tui.js +372 -0
- package/dist/vendor/codeburn/src/providers/mistral-vibe-unified.js +236 -0
- package/dist/vendor/codeburn/src/providers/mistral-vibe.js +405 -0
- package/dist/vendor/codeburn/src/providers/mux.js +242 -0
- package/dist/vendor/codeburn/src/providers/open-design.js +217 -0
- package/dist/vendor/codeburn/src/providers/openclaude.js +309 -0
- package/dist/vendor/codeburn/src/providers/openclaw.js +442 -0
- package/dist/vendor/codeburn/src/providers/opencode-file-parser.js +131 -0
- package/dist/vendor/codeburn/src/providers/opencode.js +91 -0
- package/dist/vendor/codeburn/src/providers/pi.js +429 -0
- package/dist/vendor/codeburn/src/providers/quickdesk.js +491 -0
- package/dist/vendor/codeburn/src/providers/qwen.js +193 -0
- package/dist/vendor/codeburn/src/providers/session-message.js +118 -0
- package/dist/vendor/codeburn/src/providers/sqlite-session-parser.js +457 -0
- package/dist/vendor/codeburn/src/providers/types.js +5 -0
- package/dist/vendor/codeburn/src/providers/vercel-gateway.js +135 -0
- package/dist/vendor/codeburn/src/providers/vscode-cline-parser.js +212 -0
- package/dist/vendor/codeburn/src/providers/warp.js +575 -0
- package/dist/vendor/codeburn/src/providers/zcode.js +249 -0
- package/dist/vendor/codeburn/src/providers/zed.js +187 -0
- package/dist/vendor/codeburn/src/providers/zerostack.js +139 -0
- package/dist/vendor/codeburn/src/quota/antigravity.js +310 -0
- package/dist/vendor/codeburn/src/quota/claude.js +244 -0
- package/dist/vendor/codeburn/src/quota/cursor.js +301 -0
- package/dist/vendor/codeburn/src/quota/security.js +299 -0
- package/dist/vendor/codeburn/src/quota/types.js +1 -0
- package/dist/vendor/codeburn/src/scan-facts.js +99 -0
- package/dist/vendor/codeburn/src/session-cache.js +3040 -0
- package/dist/vendor/codeburn/src/session-count-label.js +14 -0
- package/dist/vendor/codeburn/src/session-output.js +139 -0
- package/dist/vendor/codeburn/src/session-population.js +30 -0
- package/dist/vendor/codeburn/src/sessions-report.js +1342 -0
- package/dist/vendor/codeburn/src/sqlite.js +528 -0
- package/dist/vendor/codeburn/src/sync/otlp.js +420 -0
- package/dist/vendor/codeburn/src/telemetry-snapshot.js +244 -0
- package/dist/vendor/codeburn/src/token-estimate.js +5 -0
- package/dist/vendor/codeburn/src/types.js +16 -0
- package/dist/vendor/codeburn/src/work-units.js +126 -0
- package/dist/vendor/codeburn/src/workflow-insights.js +228 -0
- package/dist/vendor/codeburn/src/wsl.js +198 -0
- package/dist/vendor/codeburn/src/yield.js +666 -0
- package/package.json +11 -3
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// @ts-nocheck: codeburn type-checks this under its own tsconfig; Chompyy adds noUncheckedIndexedAccess (see UPSTREAM.md)
|
|
2
|
+
import { readFile, writeFile, mkdir } from 'fs/promises';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { getCodeburnCacheDir } from './cache-dir.js';
|
|
5
|
+
import { readConfig } from './config.js';
|
|
6
|
+
import { fetchWithTimeout } from './fetch-utils.js';
|
|
7
|
+
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
8
|
+
const FRANKFURTER_URL = 'https://api.frankfurter.app/latest?from=USD&to=';
|
|
9
|
+
// Defensive bounds on any fetched FX rate. Outside this band the rate is either a parser bug
|
|
10
|
+
// or a tampered Frankfurter response, and we refuse to multiply it into displayed costs.
|
|
11
|
+
const MIN_VALID_FX_RATE = 0.0001;
|
|
12
|
+
const MAX_VALID_FX_RATE = 1_000_000;
|
|
13
|
+
function isValidRate(value) {
|
|
14
|
+
return typeof value === 'number'
|
|
15
|
+
&& Number.isFinite(value)
|
|
16
|
+
&& value >= MIN_VALID_FX_RATE
|
|
17
|
+
&& value <= MAX_VALID_FX_RATE;
|
|
18
|
+
}
|
|
19
|
+
let active = { code: 'USD', rate: 1, symbol: '$' };
|
|
20
|
+
const USD = { code: 'USD', rate: 1, symbol: '$' };
|
|
21
|
+
const SYMBOL_OVERRIDES = {
|
|
22
|
+
CNY: '¥',
|
|
23
|
+
RON: 'lei',
|
|
24
|
+
RUB: '₽',
|
|
25
|
+
};
|
|
26
|
+
// Intl.NumberFormat throws on invalid ISO 4217 codes, so we use it as a validator
|
|
27
|
+
export function isValidCurrencyCode(code) {
|
|
28
|
+
try {
|
|
29
|
+
new Intl.NumberFormat('en', { style: 'currency', currency: code });
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function resolveSymbol(code) {
|
|
37
|
+
if (SYMBOL_OVERRIDES[code])
|
|
38
|
+
return SYMBOL_OVERRIDES[code];
|
|
39
|
+
const parts = new Intl.NumberFormat('en', {
|
|
40
|
+
style: 'currency',
|
|
41
|
+
currency: code,
|
|
42
|
+
currencyDisplay: 'symbol',
|
|
43
|
+
}).formatToParts(0);
|
|
44
|
+
return parts.find(p => p.type === 'currency')?.value ?? code;
|
|
45
|
+
}
|
|
46
|
+
export function getFractionDigits(code) {
|
|
47
|
+
return new Intl.NumberFormat('en', {
|
|
48
|
+
style: 'currency',
|
|
49
|
+
currency: code,
|
|
50
|
+
}).resolvedOptions().maximumFractionDigits ?? 2;
|
|
51
|
+
}
|
|
52
|
+
/// Round a converted cost to the currency's natural decimal places. JPY/KRW/CLP
|
|
53
|
+
/// resolve to 0 fraction digits — exporting those with `round2` produced rows
|
|
54
|
+
/// like `¥412.37` while the dashboard rendered `¥412`, breaking finance reports
|
|
55
|
+
/// that compare the two surfaces.
|
|
56
|
+
export function roundForActiveCurrency(value) {
|
|
57
|
+
const code = getCurrency().code;
|
|
58
|
+
const digits = getFractionDigits(code);
|
|
59
|
+
const factor = Math.pow(10, digits);
|
|
60
|
+
return Math.round(value * factor) / factor;
|
|
61
|
+
}
|
|
62
|
+
function getRateCachePath() {
|
|
63
|
+
return join(getCodeburnCacheDir(), 'exchange-rate.json');
|
|
64
|
+
}
|
|
65
|
+
async function fetchRate(code) {
|
|
66
|
+
// Bounded so a stalled network can't hang the daily refresh for non-USD users
|
|
67
|
+
// (same wedge as the pricing fetch); callers fall back to USD / cached rate.
|
|
68
|
+
const url = code === 'RUB' ? 'https://api.frankfurter.dev/v2/rate/USD/RUB' : `${FRANKFURTER_URL}${code}`;
|
|
69
|
+
const response = await fetchWithTimeout(url);
|
|
70
|
+
if (!response.ok)
|
|
71
|
+
throw new Error(`HTTP ${response.status}`);
|
|
72
|
+
const data = await response.json();
|
|
73
|
+
const rate = code === 'RUB' ? data.rate : data.rates?.[code];
|
|
74
|
+
if (!isValidRate(rate))
|
|
75
|
+
throw new Error(`Invalid rate returned for ${code}`);
|
|
76
|
+
return rate;
|
|
77
|
+
}
|
|
78
|
+
async function loadCachedRate(code, maxAgeMs = CACHE_TTL_MS) {
|
|
79
|
+
try {
|
|
80
|
+
const raw = await readFile(getRateCachePath(), 'utf-8');
|
|
81
|
+
const cached = JSON.parse(raw);
|
|
82
|
+
// Validate every field -- a tampered cache file could set rate to a string, null, or
|
|
83
|
+
// Infinity and break downstream math silently.
|
|
84
|
+
if (typeof cached.code !== 'string' || cached.code !== code)
|
|
85
|
+
return null;
|
|
86
|
+
if (typeof cached.timestamp !== 'number' || !Number.isFinite(cached.timestamp))
|
|
87
|
+
return null;
|
|
88
|
+
if (Date.now() - cached.timestamp > maxAgeMs)
|
|
89
|
+
return null;
|
|
90
|
+
if (!isValidRate(cached.rate))
|
|
91
|
+
return null;
|
|
92
|
+
return cached.rate;
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function cacheRate(code, rate) {
|
|
99
|
+
await mkdir(getCodeburnCacheDir(), { recursive: true });
|
|
100
|
+
await writeFile(getRateCachePath(), JSON.stringify({ timestamp: Date.now(), code, rate }));
|
|
101
|
+
}
|
|
102
|
+
// null means no usable rate: callers must show USD rather than pair a foreign
|
|
103
|
+
// symbol with an unconverted amount.
|
|
104
|
+
async function getExchangeRate(code) {
|
|
105
|
+
if (code === 'USD')
|
|
106
|
+
return 1;
|
|
107
|
+
const cached = await loadCachedRate(code);
|
|
108
|
+
if (cached)
|
|
109
|
+
return cached;
|
|
110
|
+
// Test-only escape hatch, set for the whole suite in
|
|
111
|
+
// tests/setup/env-isolation.ts: skip the live Frankfurter fetch so a real FX
|
|
112
|
+
// move can't shift assertions. Same fallback an unreachable network gets.
|
|
113
|
+
if (process.env['CODEBURN_FX_NO_FETCH'])
|
|
114
|
+
return loadCachedRate(code, Infinity);
|
|
115
|
+
let rate;
|
|
116
|
+
try {
|
|
117
|
+
rate = await fetchRate(code);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return loadCachedRate(code, Infinity);
|
|
121
|
+
}
|
|
122
|
+
// Persist the rate, but never let a cache-write failure (disk full, no
|
|
123
|
+
// permissions, etc.) cause us to return the USD-equivalent fallback.
|
|
124
|
+
// The original code wrapped fetch + cacheRate in one try/catch, so a
|
|
125
|
+
// disk-full at write time would discard a perfectly good rate and silently
|
|
126
|
+
// make every cost render as if the user had selected USD.
|
|
127
|
+
cacheRate(code, rate).catch(() => { });
|
|
128
|
+
return rate;
|
|
129
|
+
}
|
|
130
|
+
export async function loadCurrency() {
|
|
131
|
+
const config = await readConfig();
|
|
132
|
+
if (!config.currency) {
|
|
133
|
+
// A long-lived `serve` process may previously have loaded a non-USD
|
|
134
|
+
// currency. Removing the config entry is the USD reset contract, so reset
|
|
135
|
+
// the module state as well as letting the output memo invalidate.
|
|
136
|
+
active = USD;
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const code = config.currency.code.toUpperCase();
|
|
140
|
+
const rate = await getExchangeRate(code);
|
|
141
|
+
if (rate === null) {
|
|
142
|
+
active = USD;
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const symbol = config.currency.symbol ?? resolveSymbol(code);
|
|
146
|
+
active = { code, rate, symbol };
|
|
147
|
+
}
|
|
148
|
+
export function getCurrency() {
|
|
149
|
+
return active;
|
|
150
|
+
}
|
|
151
|
+
export async function switchCurrency(code) {
|
|
152
|
+
if (code === 'USD') {
|
|
153
|
+
active = USD;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const rate = await getExchangeRate(code);
|
|
157
|
+
if (rate === null) {
|
|
158
|
+
active = USD;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const symbol = resolveSymbol(code);
|
|
162
|
+
active = { code, rate, symbol };
|
|
163
|
+
}
|
|
164
|
+
export function getCostColumnHeader() {
|
|
165
|
+
return `Cost (${active.code})`;
|
|
166
|
+
}
|
|
167
|
+
export function convertCost(costUSD) {
|
|
168
|
+
// Return the unrounded converted cost. Rounding here meant zero-fraction
|
|
169
|
+
// currencies (JPY, KRW, CLP) clamped every per-session cost to the nearest
|
|
170
|
+
// whole unit before aggregation; a project with 1000 sessions averaging
|
|
171
|
+
// ¥0.4 each would aggregate to ¥0 instead of ¥400 because each row was
|
|
172
|
+
// rounded independently. formatCost (and the export rowsToCsv path) round
|
|
173
|
+
// at the display boundary instead.
|
|
174
|
+
return costUSD * active.rate;
|
|
175
|
+
}
|
|
176
|
+
export function formatCost(costUSD) {
|
|
177
|
+
const { rate, symbol, code } = active;
|
|
178
|
+
const cost = costUSD * rate;
|
|
179
|
+
const digits = getFractionDigits(code);
|
|
180
|
+
if (digits === 0)
|
|
181
|
+
return `${symbol}${Math.round(cost)}`;
|
|
182
|
+
if (cost >= 1)
|
|
183
|
+
return `${symbol}${cost.toFixed(2)}`;
|
|
184
|
+
if (cost >= 0.01)
|
|
185
|
+
return `${symbol}${cost.toFixed(3)}`;
|
|
186
|
+
if (cost >= 0.0001)
|
|
187
|
+
return `${symbol}${cost.toFixed(4)}`;
|
|
188
|
+
return `${symbol}${cost.toFixed(2)}`;
|
|
189
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// @ts-nocheck: codeburn type-checks this under its own tsconfig; Chompyy adds noUncheckedIndexedAccess (see UPSTREAM.md)
|
|
2
|
+
import { readFile, readdir, writeFile, mkdir, rename, stat, unlink } from 'fs/promises';
|
|
3
|
+
import { join, resolve } from 'path';
|
|
4
|
+
import { randomBytes } from 'crypto';
|
|
5
|
+
import { getCodeburnCacheDir, readExistingTextFile } from './cache-dir.js';
|
|
6
|
+
import { acquireCacheRefreshLock } from './cache-refresh-lock.js';
|
|
7
|
+
// Bumped to 3 for the workspace-aware breakdown change: the cursor parser
|
|
8
|
+
// now derives `sessionId` from the bubble row key (the real composer id)
|
|
9
|
+
// rather than the empty `conversationId` JSON field, and the workspace
|
|
10
|
+
// router relies on those composer ids to bucket calls per project.
|
|
11
|
+
// Version 2 caches contain `sessionId: 'unknown'` for every call and would
|
|
12
|
+
// route everything to the orphan project, so we invalidate them.
|
|
13
|
+
// Version 5: parseAgentKv was removed (it double-counted against bubbles);
|
|
14
|
+
// real context tokens from composerData.promptTokenBreakdown now drive
|
|
15
|
+
// input, and agentKv is used only for the tools/bash breakdown. Cached v4
|
|
16
|
+
// results contain stale agentKv calls and lack the real token figures.
|
|
17
|
+
// Version 6: conversation input moved to composer-anchored records
|
|
18
|
+
// (cursor:composer-input:<id>) with per-conversation source selection, the
|
|
19
|
+
// agent stream regained tool/system context and stream-only sessions, and
|
|
20
|
+
// tool names are canonicalized. v5 results mix crediting regimes.
|
|
21
|
+
// Version 7: a redirected data root can select a different database whose
|
|
22
|
+
// size and timestamps happen to match. Cache identity includes its path.
|
|
23
|
+
// Version 8: v7 results held old agent streams billed at the DB mtime; a
|
|
24
|
+
// dormant DB never changes, so only a version bump re-parses them.
|
|
25
|
+
// Version 9: one agentKv value that was not valid JSON emptied the whole agent
|
|
26
|
+
// stream, so v8 results lack its tools, estimates and agent-only sessions.
|
|
27
|
+
// Also v9: v8 results were stored while agent-stream ids were still left
|
|
28
|
+
// past the lookup cap, so an unchanged DB never resolved them.
|
|
29
|
+
export const CURSOR_CACHE_VERSION = 9;
|
|
30
|
+
export const CURSOR_LEGACY_CACHE_FILE = 'cursor-results.json';
|
|
31
|
+
export function cursorCacheFileName(version = CURSOR_CACHE_VERSION) {
|
|
32
|
+
return `cursor-results.v${version}.json`;
|
|
33
|
+
}
|
|
34
|
+
function getCachePath() {
|
|
35
|
+
return join(getCodeburnCacheDir(), cursorCacheFileName());
|
|
36
|
+
}
|
|
37
|
+
function getLegacyCachePath() {
|
|
38
|
+
return join(getCodeburnCacheDir(), CURSOR_LEGACY_CACHE_FILE);
|
|
39
|
+
}
|
|
40
|
+
function isCurrentHit(cache, dbPath, fp, requestedFloor) {
|
|
41
|
+
return (cache.version === CURSOR_CACHE_VERSION
|
|
42
|
+
&& cache.dbPath === resolve(dbPath)
|
|
43
|
+
&& cache.dbMtimeMs === fp.mtimeMs
|
|
44
|
+
&& cache.dbSizeBytes === fp.size
|
|
45
|
+
&& typeof cache.lookbackFloor === 'string'
|
|
46
|
+
&& cache.lookbackFloor <= requestedFloor);
|
|
47
|
+
}
|
|
48
|
+
async function readCacheFile(path) {
|
|
49
|
+
try {
|
|
50
|
+
const cache = JSON.parse(await readFile(path, 'utf-8'));
|
|
51
|
+
if (cache && typeof cache === 'object')
|
|
52
|
+
return cache;
|
|
53
|
+
}
|
|
54
|
+
catch { }
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
async function getDbFingerprint(dbPath) {
|
|
58
|
+
try {
|
|
59
|
+
const s = await stat(dbPath);
|
|
60
|
+
// Cursor writes chat usage through the WAL, so new activity lands in
|
|
61
|
+
// state.vscdb-wal while the main file's mtime/size stay put. Fold the WAL
|
|
62
|
+
// into the fingerprint (mirrors fingerprintDatabase in sqlite.ts).
|
|
63
|
+
let wal = null;
|
|
64
|
+
try {
|
|
65
|
+
wal = await stat(dbPath + '-wal');
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
// -wal absent (checkpointed) is expected; anything else fails the read.
|
|
69
|
+
if (err?.code !== 'ENOENT')
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
mtimeMs: wal ? Math.max(s.mtimeMs, wal.mtimeMs) : s.mtimeMs,
|
|
74
|
+
size: s.size + (wal?.size ?? 0),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export async function readCachedResults(dbPath, requestedFloor) {
|
|
82
|
+
try {
|
|
83
|
+
const fp = await getDbFingerprint(dbPath);
|
|
84
|
+
if (!fp)
|
|
85
|
+
return null;
|
|
86
|
+
const versioned = await readExistingTextFile(getCachePath());
|
|
87
|
+
if (versioned.status === 'ok') {
|
|
88
|
+
try {
|
|
89
|
+
const cache = JSON.parse(versioned.text);
|
|
90
|
+
if (cache && typeof cache === 'object' && isCurrentHit(cache, dbPath, fp, requestedFloor))
|
|
91
|
+
return cache.calls;
|
|
92
|
+
}
|
|
93
|
+
catch { }
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
if (versioned.status === 'unreadable')
|
|
97
|
+
return null;
|
|
98
|
+
// Versioned file is absent (ENOENT). Adopt the unsuffixed file only when its
|
|
99
|
+
// version and fingerprint match — old binaries still own that path.
|
|
100
|
+
const legacy = await readCacheFile(getLegacyCachePath());
|
|
101
|
+
if (legacy && isCurrentHit(legacy, dbPath, fp, requestedFloor))
|
|
102
|
+
return legacy.calls;
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Agent-only Cursor sessions carry no timestamp of their own. The time a
|
|
110
|
+
// request was first seen is the only stable one, so it is kept outside the
|
|
111
|
+
// versioned result cache and survives its version bumps and clears. Wiping the
|
|
112
|
+
// whole cache dir loses it by design: those sessions take the DB mtime again.
|
|
113
|
+
const FIRST_SEEN_FILE = 'cursor-first-seen.json';
|
|
114
|
+
const FIRST_SEEN_LOCK = 'cursor-first-seen.lock';
|
|
115
|
+
// Written when the lock cannot be taken, so no writer ever replaces the shared
|
|
116
|
+
// file from an unlocked view; the next lock holder folds them in.
|
|
117
|
+
const FIRST_SEEN_FRAGMENT = /^cursor-first-seen\.\d+\.json$/;
|
|
118
|
+
// Anything but the ISO string a parse wrote falls back to the mtime rule.
|
|
119
|
+
function isIsoTimestamp(value) {
|
|
120
|
+
if (typeof value !== 'string')
|
|
121
|
+
return false;
|
|
122
|
+
const ms = Date.parse(value);
|
|
123
|
+
return Number.isFinite(ms) && new Date(ms).toISOString() === value;
|
|
124
|
+
}
|
|
125
|
+
function parseFirstSeen(text) {
|
|
126
|
+
try {
|
|
127
|
+
const parsed = JSON.parse(text ?? '');
|
|
128
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
129
|
+
return new Map(Object.entries(parsed).filter((e) => isIsoTimestamp(e[1])));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch { }
|
|
133
|
+
return new Map();
|
|
134
|
+
}
|
|
135
|
+
// The earliest recorded time wins.
|
|
136
|
+
function mergeFirstSeen(into, from) {
|
|
137
|
+
for (const [key, at] of from) {
|
|
138
|
+
const current = into.get(key);
|
|
139
|
+
if (!current || at < current)
|
|
140
|
+
into.set(key, at);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const readText = (path) => readFile(path, 'utf-8').catch(() => null);
|
|
144
|
+
async function readFirstSeenFiles(dir) {
|
|
145
|
+
const merged = parseFirstSeen(await readText(join(dir, FIRST_SEEN_FILE)));
|
|
146
|
+
const fragments = [];
|
|
147
|
+
const names = await readdir(dir).catch(() => []);
|
|
148
|
+
for (const name of names.filter(n => FIRST_SEEN_FRAGMENT.test(n))) {
|
|
149
|
+
const text = await readText(join(dir, name));
|
|
150
|
+
if (text === null)
|
|
151
|
+
continue;
|
|
152
|
+
fragments.push({ path: join(dir, name), text });
|
|
153
|
+
mergeFirstSeen(merged, parseFirstSeen(text));
|
|
154
|
+
}
|
|
155
|
+
return { merged, fragments };
|
|
156
|
+
}
|
|
157
|
+
export async function readFirstSeen() {
|
|
158
|
+
return (await readFirstSeenFiles(getCodeburnCacheDir())).merged;
|
|
159
|
+
}
|
|
160
|
+
// Writes in one process share its fragment, so they run one at a time.
|
|
161
|
+
let firstSeenWrites = Promise.resolve();
|
|
162
|
+
export function writeFirstSeen(firstSeen) {
|
|
163
|
+
const next = firstSeenWrites.then(() => writeFirstSeenNow(firstSeen));
|
|
164
|
+
firstSeenWrites = next.catch(() => { });
|
|
165
|
+
return next;
|
|
166
|
+
}
|
|
167
|
+
async function writeFirstSeenNow(firstSeen) {
|
|
168
|
+
if (process.env['CODEBURN_SUPPRESS_CACHE_WRITES'])
|
|
169
|
+
return;
|
|
170
|
+
const dir = getCodeburnCacheDir();
|
|
171
|
+
await mkdir(dir, { recursive: true }).catch(() => { });
|
|
172
|
+
let lock = { outcome: 'unavailable' };
|
|
173
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
174
|
+
lock = await acquireCacheRefreshLock({ cacheDir: dir, lockFile: FIRST_SEEN_LOCK, waitMs: 2_000, pollMs: 10 });
|
|
175
|
+
if (lock.outcome !== 'completed-by-other')
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
if (lock.outcome !== 'acquired') {
|
|
179
|
+
const own = join(dir, `cursor-first-seen.${process.pid}.json`);
|
|
180
|
+
const merged = new Map(firstSeen);
|
|
181
|
+
mergeFirstSeen(merged, parseFirstSeen(await readText(own)));
|
|
182
|
+
await writeAtomic(own, JSON.stringify(Object.fromEntries(merged)));
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
const { merged, fragments } = await readFirstSeenFiles(dir);
|
|
187
|
+
mergeFirstSeen(merged, firstSeen);
|
|
188
|
+
if (!await writeAtomic(join(dir, FIRST_SEEN_FILE), JSON.stringify(Object.fromEntries(merged))))
|
|
189
|
+
return;
|
|
190
|
+
// A fragment rewritten since it was read keeps its newer entries.
|
|
191
|
+
for (const fragment of fragments) {
|
|
192
|
+
if (await readText(fragment.path) === fragment.text)
|
|
193
|
+
await unlink(fragment.path).catch(() => { });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
await lock.handle.release().catch(() => { });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
export async function writeCachedResults(dbPath, calls, lookbackFloor) {
|
|
201
|
+
// Diagnostic contexts (codeburn doctor) sample-parse providers under a
|
|
202
|
+
// strictly read-only promise; this is the one parse path that writes to
|
|
203
|
+
// disk before its first yield, so it honors the suppression flag.
|
|
204
|
+
if (process.env['CODEBURN_SUPPRESS_CACHE_WRITES'])
|
|
205
|
+
return;
|
|
206
|
+
const fp = await getDbFingerprint(dbPath);
|
|
207
|
+
if (!fp)
|
|
208
|
+
return;
|
|
209
|
+
const dir = getCodeburnCacheDir();
|
|
210
|
+
await mkdir(dir, { recursive: true }).catch(() => { });
|
|
211
|
+
const cache = {
|
|
212
|
+
version: CURSOR_CACHE_VERSION,
|
|
213
|
+
dbPath: resolve(dbPath),
|
|
214
|
+
dbMtimeMs: fp.mtimeMs,
|
|
215
|
+
dbSizeBytes: fp.size,
|
|
216
|
+
lookbackFloor,
|
|
217
|
+
calls,
|
|
218
|
+
};
|
|
219
|
+
await writeAtomic(getCachePath(), JSON.stringify(cache));
|
|
220
|
+
}
|
|
221
|
+
// Atomic write: stage to a randomized temp file in the same directory,
|
|
222
|
+
// then rename onto the final path. rename() is atomic on POSIX, so a
|
|
223
|
+
// crash mid-write never leaves a half-written cache, and concurrent
|
|
224
|
+
// CLI invocations using their own random temp names cannot interleave
|
|
225
|
+
// bytes in the destination file (they only race on the final rename,
|
|
226
|
+
// last-writer-wins, both with valid content).
|
|
227
|
+
async function writeAtomic(target, text) {
|
|
228
|
+
const tempPath = `${target}.${randomBytes(8).toString('hex')}.tmp`;
|
|
229
|
+
try {
|
|
230
|
+
await writeFile(tempPath, text, 'utf-8');
|
|
231
|
+
await rename(tempPath, target);
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
await unlink(tempPath).catch(() => { });
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
}
|