@use-aistack/cli 0.16.0 → 0.16.2
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 +11 -0
- package/dist/cursor-worker.js +605 -0
- package/dist/cursor-worker.js.map +1 -0
- package/dist/index.js +484 -74
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,6 +22,17 @@ On an unlinked machine, `sync` starts the login flow inline - your browser opens
|
|
|
22
22
|
|
|
23
23
|
After a manual sync, the CLI offers auto-sync with three choices: Enable, Maybe later, and Never ask again. Maybe later asks again after your next manual sync. Existing declines from older CLI versions are treated as Maybe later.
|
|
24
24
|
|
|
25
|
+
#### A sync that looks stuck
|
|
26
|
+
|
|
27
|
+
The scan runs on this machine and can take a few minutes when a harness keeps a large history. Cursor is the usual case: its global `state.vscdb` grows past a gigabyte, and the session listing walks all of it before the first result. Every harness is checked at the same time and gets its own row with a progress bar, so one slow harness shows as one slow row while the others finish. To see each step as it starts, with its duration, run with `--verbose`:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npx @use-aistack/cli sync --verbose
|
|
31
|
+
AISTACK_DEBUG=1 npx @use-aistack/cli sync # the same, for a hook or a script
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The lines go to stderr and hold counts and durations only: no paths, prompts, or database values. Paste them into a bug report.
|
|
35
|
+
|
|
25
36
|
### `npx @use-aistack/cli sync --auto on` / `off`
|
|
26
37
|
|
|
27
38
|
Optional: keep your stack fresh without manual syncs. `on` asks your stack for permission, then writes hooks for the harnesses you actually use. The `SessionStart` paths are: `~/.claude/settings.json` for Claude Code, `~/.codex/hooks.json` for Codex, and `$GROK_HOME/hooks/aistack.json` (default `~/.grok/hooks/aistack.json`) for Grok Build. Cursor uses a user-level `stop` hook in `~/.cursor/hooks.json` and reloads that configuration automatically. Its trigger runs when the agent finishes a turn. The other hooks run when a session starts. All triggers share one silent sync throttle, at most once every 6 hours. Existing user hooks are preserved. Grok Build needs a new session or hook reload after installation. `off` disables local publication first, removes the owned hooks, then takes the remote permission back. If removal fails, the disabled local gate still prevents publication and the next interactive sync can retry reconciliation.
|
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/harness/cursor/worker.ts
|
|
4
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
5
|
+
|
|
6
|
+
// src/trace.ts
|
|
7
|
+
var sink = null;
|
|
8
|
+
var startedAtMs = 0;
|
|
9
|
+
var stderrSink = (line) => {
|
|
10
|
+
process.stderr.write(`${line}
|
|
11
|
+
`);
|
|
12
|
+
};
|
|
13
|
+
function enableTrace(write = stderrSink, now = () => performance.now()) {
|
|
14
|
+
sink = write;
|
|
15
|
+
startedAtMs = now();
|
|
16
|
+
}
|
|
17
|
+
function elapsedLabel() {
|
|
18
|
+
const seconds = (performance.now() - startedAtMs) / 1e3;
|
|
19
|
+
return `+${seconds.toFixed(seconds < 10 ? 2 : 1)}s`;
|
|
20
|
+
}
|
|
21
|
+
function trace(message) {
|
|
22
|
+
if (!sink) return;
|
|
23
|
+
sink(`[aistack ${elapsedLabel().padStart(7)}] ${message}`);
|
|
24
|
+
}
|
|
25
|
+
function traceTimer(label) {
|
|
26
|
+
if (!sink) return () => {
|
|
27
|
+
};
|
|
28
|
+
const started = performance.now();
|
|
29
|
+
return (note) => {
|
|
30
|
+
const ms = Math.round(performance.now() - started);
|
|
31
|
+
trace(`${label} \xB7 ${ms} ms${note ? ` \xB7 ${note}` : ""}`);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/harness/cursor/local.ts
|
|
36
|
+
import { stat } from "node:fs/promises";
|
|
37
|
+
import { homedir } from "node:os";
|
|
38
|
+
import path from "node:path";
|
|
39
|
+
|
|
40
|
+
// ../pricing/src/table.ts
|
|
41
|
+
var PROVIDER_SEPARATOR = ":";
|
|
42
|
+
var LOCAL_PRICING_TABLE_VERSION = "local-no-charge";
|
|
43
|
+
var PROVIDER_VENDOR = {
|
|
44
|
+
anthropic: "anthropic",
|
|
45
|
+
openai: "openai",
|
|
46
|
+
google: "google",
|
|
47
|
+
xai: "xai"
|
|
48
|
+
};
|
|
49
|
+
var PRICING_ALIASES = {
|
|
50
|
+
"grok-4.6-build": "grok-4.6"
|
|
51
|
+
};
|
|
52
|
+
var LOCAL_PROVIDERS = /* @__PURE__ */ new Set([
|
|
53
|
+
"ollama",
|
|
54
|
+
"lmstudio",
|
|
55
|
+
"llama.cpp",
|
|
56
|
+
"llamacpp",
|
|
57
|
+
"local"
|
|
58
|
+
]);
|
|
59
|
+
var FREE_PERIOD = {
|
|
60
|
+
from: null,
|
|
61
|
+
to: null,
|
|
62
|
+
input: 0,
|
|
63
|
+
output: 0,
|
|
64
|
+
cacheRead: 0,
|
|
65
|
+
cacheWrite5m: 0,
|
|
66
|
+
cacheWrite1h: 0,
|
|
67
|
+
source: LOCAL_PRICING_TABLE_VERSION
|
|
68
|
+
};
|
|
69
|
+
function splitModelKey(modelKey) {
|
|
70
|
+
const at = modelKey.indexOf(PROVIDER_SEPARATOR);
|
|
71
|
+
if (at === -1) return { provider: null, model: modelKey };
|
|
72
|
+
return {
|
|
73
|
+
provider: modelKey.slice(0, at),
|
|
74
|
+
model: modelKey.slice(at + PROVIDER_SEPARATOR.length)
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
var key = (slug, provider) => `${provider ?? ""}\0${slug}`;
|
|
78
|
+
var PriceIndex = class {
|
|
79
|
+
periods = /* @__PURE__ */ new Map();
|
|
80
|
+
vendors = /* @__PURE__ */ new Map();
|
|
81
|
+
id;
|
|
82
|
+
constructor(table) {
|
|
83
|
+
this.id = table.id;
|
|
84
|
+
const groups = /* @__PURE__ */ new Map();
|
|
85
|
+
for (const row of table.rows) {
|
|
86
|
+
const k = key(row.modelSlug, row.provider);
|
|
87
|
+
const g = groups.get(k) ?? [];
|
|
88
|
+
g.push(row);
|
|
89
|
+
groups.set(k, g);
|
|
90
|
+
if (row.vendor && row.provider === void 0) {
|
|
91
|
+
this.vendors.set(row.modelSlug, row.vendor);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
for (const [k, rows] of groups) {
|
|
95
|
+
rows.sort((a, b) => a.from - b.from);
|
|
96
|
+
this.periods.set(
|
|
97
|
+
k,
|
|
98
|
+
rows.map((r, i) => ({
|
|
99
|
+
from: r.from === 0 ? null : r.from,
|
|
100
|
+
to: i + 1 < rows.length ? rows[i + 1].from : null,
|
|
101
|
+
input: r.input,
|
|
102
|
+
output: r.output,
|
|
103
|
+
cacheRead: r.cacheRead ?? 0,
|
|
104
|
+
cacheWrite5m: r.cacheWrite5m ?? 0,
|
|
105
|
+
cacheWrite1h: r.cacheWrite1h ?? 0,
|
|
106
|
+
source: r.source
|
|
107
|
+
}))
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** The vendor a bare row belongs to, when the table says. */
|
|
112
|
+
vendorOf(slug) {
|
|
113
|
+
return this.vendors.get(slug) ?? null;
|
|
114
|
+
}
|
|
115
|
+
/** True when the table holds any row at all for this (model, provider). */
|
|
116
|
+
has(slug, provider) {
|
|
117
|
+
return this.periods.has(key(slug, provider));
|
|
118
|
+
}
|
|
119
|
+
/** The periods for exactly this (model, provider). */
|
|
120
|
+
rowsFor(slug, provider) {
|
|
121
|
+
return this.periods.get(key(slug, provider)) ?? [];
|
|
122
|
+
}
|
|
123
|
+
get size() {
|
|
124
|
+
return this.periods.size;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
var Pricer = class {
|
|
128
|
+
constructor(layers, vendorHint = () => null) {
|
|
129
|
+
this.layers = layers;
|
|
130
|
+
this.vendorHint = vendorHint;
|
|
131
|
+
}
|
|
132
|
+
/** The ids of the layers, in lookup order. */
|
|
133
|
+
get tableIds() {
|
|
134
|
+
return this.layers.map((l) => l.id);
|
|
135
|
+
}
|
|
136
|
+
vendorOf(slug) {
|
|
137
|
+
for (const layer of this.layers) {
|
|
138
|
+
const v = layer.vendorOf(slug);
|
|
139
|
+
if (v) return v;
|
|
140
|
+
}
|
|
141
|
+
return this.vendorHint(slug);
|
|
142
|
+
}
|
|
143
|
+
firstLayerWith(slug, provider) {
|
|
144
|
+
return this.layers.find((l) => l.has(slug, provider)) ?? null;
|
|
145
|
+
}
|
|
146
|
+
lookupSlug(slug, provider) {
|
|
147
|
+
if (this.firstLayerWith(slug, provider)) return slug;
|
|
148
|
+
const alias = PRICING_ALIASES[slug];
|
|
149
|
+
return alias && this.firstLayerWith(alias, provider) ? alias : null;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Every period that applies to a pricing key, or an empty list when none
|
|
153
|
+
* can be cited.
|
|
154
|
+
*
|
|
155
|
+
* A bare key is the vendor's own rate. A local provider is free. A provider
|
|
156
|
+
* with rows of its own uses them. A provider that IS the vendor reaches the
|
|
157
|
+
* vendor's bare rows. Anything else (a gateway, an unknown provider) holds
|
|
158
|
+
* no rate.
|
|
159
|
+
*/
|
|
160
|
+
periodsFor(modelKey) {
|
|
161
|
+
const { provider, model } = splitModelKey(modelKey);
|
|
162
|
+
if (provider === null) {
|
|
163
|
+
const slug = this.lookupSlug(model, null);
|
|
164
|
+
return slug ? this.firstLayerWith(slug, null)?.rowsFor(slug, null) ?? [] : [];
|
|
165
|
+
}
|
|
166
|
+
if (LOCAL_PROVIDERS.has(provider)) return [FREE_PERIOD];
|
|
167
|
+
const ownSlug = this.lookupSlug(model, provider);
|
|
168
|
+
if (ownSlug) {
|
|
169
|
+
return this.firstLayerWith(ownSlug, provider)?.rowsFor(ownSlug, provider) ?? [];
|
|
170
|
+
}
|
|
171
|
+
const vendor = PROVIDER_VENDOR[provider];
|
|
172
|
+
const vendorSlug = this.lookupSlug(model, null);
|
|
173
|
+
if (!vendor || !vendorSlug || this.vendorOf(vendorSlug) !== vendor)
|
|
174
|
+
return [];
|
|
175
|
+
return this.firstLayerWith(vendorSlug, null)?.rowsFor(vendorSlug, null) ?? [];
|
|
176
|
+
}
|
|
177
|
+
isLocal(modelKey) {
|
|
178
|
+
const { provider } = splitModelKey(modelKey);
|
|
179
|
+
return provider !== null && LOCAL_PROVIDERS.has(provider);
|
|
180
|
+
}
|
|
181
|
+
isPriced(modelKey) {
|
|
182
|
+
return this.periodsFor(modelKey).length > 0;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The rate in effect at `atMs`, or `null` when the model is unknown or the
|
|
186
|
+
* timestamp predates every period. A `null` timestamp also yields `null`:
|
|
187
|
+
* inventing a price for an undated record would attribute the wrong rate.
|
|
188
|
+
*/
|
|
189
|
+
priceAt(modelKey, atMs) {
|
|
190
|
+
if (atMs === null) return null;
|
|
191
|
+
for (const p of this.periodsFor(modelKey)) {
|
|
192
|
+
if ((p.from === null || atMs >= p.from) && (p.to === null || atMs < p.to)) {
|
|
193
|
+
return p;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
/** Every rate that applies anywhere inside `[fromMs, toMs]`. */
|
|
199
|
+
periodsInWindow(modelKey, fromMs, toMs) {
|
|
200
|
+
return this.periodsFor(modelKey).filter(
|
|
201
|
+
(p) => (p.from === null || p.from <= toMs) && (p.to === null || p.to > fromMs)
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The citation for this key: the source of the period in effect at `atMs`,
|
|
206
|
+
* or of the latest period when no time is given. `null` when unpriced.
|
|
207
|
+
*/
|
|
208
|
+
tableFor(modelKey, atMs) {
|
|
209
|
+
const periods = this.periodsFor(modelKey);
|
|
210
|
+
if (periods.length === 0) return null;
|
|
211
|
+
if (atMs !== void 0) return this.priceAt(modelKey, atMs)?.source ?? null;
|
|
212
|
+
return periods[periods.length - 1].source;
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
// ../pricing/src/index.ts
|
|
217
|
+
var PRICING_TABLE_VERSION = "anthropic-list-2026-07-25";
|
|
218
|
+
var OPENAI_PRICING_TABLE_VERSION = "openai-list-2026-08-02";
|
|
219
|
+
var GOOGLE_PRICING_TABLE_VERSION = "google-list-2026-08-09";
|
|
220
|
+
var XAI_PRICING_TABLE_VERSION = "models.dev@2026-08-29";
|
|
221
|
+
var BUNDLED_PRICE_TABLE_ID = "bundled-2026-09-10";
|
|
222
|
+
var CACHE_WRITE_5M_MULTIPLIER = 1.25;
|
|
223
|
+
var CACHE_WRITE_1H_MULTIPLIER = 2;
|
|
224
|
+
var CACHE_READ_MULTIPLIER = 0.1;
|
|
225
|
+
var SONNET_5_INTRO_ENDS_MS = Date.UTC(2026, 8, 1);
|
|
226
|
+
var DEFAULT_CACHE_MULTIPLIERS = {
|
|
227
|
+
write5m: CACHE_WRITE_5M_MULTIPLIER,
|
|
228
|
+
write1h: CACHE_WRITE_1H_MULTIPLIER,
|
|
229
|
+
read: CACHE_READ_MULTIPLIER
|
|
230
|
+
};
|
|
231
|
+
var GOOGLE_CACHE_MULTIPLIERS = {
|
|
232
|
+
write5m: 1,
|
|
233
|
+
write1h: 1,
|
|
234
|
+
read: 0.1
|
|
235
|
+
};
|
|
236
|
+
var XAI_CACHE_MULTIPLIERS = {
|
|
237
|
+
write5m: 0,
|
|
238
|
+
write1h: 0,
|
|
239
|
+
read: 0.25
|
|
240
|
+
};
|
|
241
|
+
var anthropic = (periods) => ({
|
|
242
|
+
vendor: "anthropic",
|
|
243
|
+
table: PRICING_TABLE_VERSION,
|
|
244
|
+
periods,
|
|
245
|
+
cache: DEFAULT_CACHE_MULTIPLIERS
|
|
246
|
+
});
|
|
247
|
+
var openai = (periods) => ({
|
|
248
|
+
vendor: "openai",
|
|
249
|
+
table: OPENAI_PRICING_TABLE_VERSION,
|
|
250
|
+
periods,
|
|
251
|
+
cache: DEFAULT_CACHE_MULTIPLIERS
|
|
252
|
+
});
|
|
253
|
+
var google = (periods) => ({
|
|
254
|
+
vendor: "google",
|
|
255
|
+
table: GOOGLE_PRICING_TABLE_VERSION,
|
|
256
|
+
periods,
|
|
257
|
+
cache: GOOGLE_CACHE_MULTIPLIERS
|
|
258
|
+
});
|
|
259
|
+
var xai = (periods) => ({
|
|
260
|
+
vendor: "xai",
|
|
261
|
+
table: XAI_PRICING_TABLE_VERSION,
|
|
262
|
+
periods,
|
|
263
|
+
cache: XAI_CACHE_MULTIPLIERS
|
|
264
|
+
});
|
|
265
|
+
var flat = (input2, output) => [
|
|
266
|
+
{ from: null, input: input2, output }
|
|
267
|
+
];
|
|
268
|
+
var PRICES = {
|
|
269
|
+
"claude-fable-5": anthropic(flat(10, 50)),
|
|
270
|
+
"claude-mythos-5": anthropic(flat(10, 50)),
|
|
271
|
+
"claude-opus-5": anthropic(flat(5, 25)),
|
|
272
|
+
"claude-opus-4-8": anthropic(flat(5, 25)),
|
|
273
|
+
"claude-opus-4-7": anthropic(flat(5, 25)),
|
|
274
|
+
"claude-opus-4-6": anthropic(flat(5, 25)),
|
|
275
|
+
"claude-sonnet-5": anthropic([
|
|
276
|
+
{ from: null, input: 2, output: 10 },
|
|
277
|
+
{ from: SONNET_5_INTRO_ENDS_MS, input: 3, output: 15 }
|
|
278
|
+
]),
|
|
279
|
+
"claude-sonnet-4-6": anthropic(flat(3, 15)),
|
|
280
|
+
"claude-haiku-4-5": anthropic(flat(1, 5)),
|
|
281
|
+
// Fast mode (research preview) - Claude API only, Opus 5 / Opus 4.8 only.
|
|
282
|
+
// Opus 4.7 fast mode was removed, so there is deliberately no 4-7 entry.
|
|
283
|
+
"claude-opus-5#fast": anthropic(flat(10, 50)),
|
|
284
|
+
"claude-opus-4-8#fast": anthropic(flat(10, 50)),
|
|
285
|
+
// OpenAI (Codex) - standard-context tier (<272K; observed context window is
|
|
286
|
+
// 258,400).
|
|
287
|
+
"gpt-5.5": openai(flat(5, 30)),
|
|
288
|
+
"gpt-5.4": openai(flat(2.5, 15)),
|
|
289
|
+
"gpt-5.4-mini": openai(flat(0.75, 4.5)),
|
|
290
|
+
"gpt-5.3-codex": openai(flat(1.75, 14)),
|
|
291
|
+
// The gpt-5.6 family launched 2026-07-29; Terra and Luna were repriced on
|
|
292
|
+
// 2026-07-30 (-20% / -80%). The one-day launch rates are not on the list
|
|
293
|
+
// page and are NOT encoded - a July-29 Terra/Luna record underprices for
|
|
294
|
+
// one day rather than carrying a rate we cannot cite (#72).
|
|
295
|
+
// Sol: models.dev reports $4 / $20 on 2026-08-29; the earlier $5 / $30 is
|
|
296
|
+
// not dated, so the lower rate prices the whole period (lower bound).
|
|
297
|
+
"gpt-5.6-sol": openai(flat(4, 20)),
|
|
298
|
+
"gpt-5.6-terra": openai(flat(2, 12)),
|
|
299
|
+
"gpt-5.6-luna": openai(flat(0.2, 1.2)),
|
|
300
|
+
// NOT on OpenAI's list page - an internal Codex routing label with no
|
|
301
|
+
// official price (openai/codex#20981). Rate is the aggregator consensus
|
|
302
|
+
// ($2.50 / $15.00), scoped in explicitly by ticket #72 because it carries
|
|
303
|
+
// real token volume in Codex rollouts. A priced lane, see PRICED_LANES.
|
|
304
|
+
"codex-auto-review": openai(flat(2.5, 15)),
|
|
305
|
+
// Google (opencode, pi-mono) - Standard tier. Where a model is
|
|
306
|
+
// context-tiered, the <=200K rate is encoded, exactly as the OpenAI rows
|
|
307
|
+
// encode the standard-context tier: the payload carries no per-response
|
|
308
|
+
// context length, so the cheaper side keeps the figure a lower bound.
|
|
309
|
+
"gemini-3.1-pro-preview": google(flat(2, 12)),
|
|
310
|
+
// models.dev reports $0.75 / $3.75 on 2026-08-29 (was $1.5 / $7.5).
|
|
311
|
+
"gemini-3.6-flash": google(flat(0.75, 3.75)),
|
|
312
|
+
"gemini-3.5-flash": google(flat(1.5, 9)),
|
|
313
|
+
"gemini-3-flash-preview": google(flat(0.5, 3)),
|
|
314
|
+
"gemini-2.5-pro": google(flat(1.25, 10)),
|
|
315
|
+
"gemini-2.5-flash": google(flat(0.3, 2.5)),
|
|
316
|
+
// RETIRED from Google's list page by 2026-08-09, and still the largest
|
|
317
|
+
// single block of Google tokens measured in #122. Encoded at its launch
|
|
318
|
+
// rate: real volume, a rate we can name. Announcement rate, <=200K tier.
|
|
319
|
+
"gemini-3-pro-preview": google(flat(2, 12)),
|
|
320
|
+
// A real Anthropic model with no row until #123. Measured in #122 as
|
|
321
|
+
// `claude-opus-4-5-20251101`, which the dated-suffix rule strips to this key.
|
|
322
|
+
"claude-opus-4-5": anthropic(flat(5, 25)),
|
|
323
|
+
// models.dev's xAI row mirrored into the live table on 2026-08-29. Grok
|
|
324
|
+
// Build's explicit `grok-4.6-build` pricing alias reaches this base rate.
|
|
325
|
+
"grok-4.6": xai(flat(2, 6))
|
|
326
|
+
};
|
|
327
|
+
function bundledPriceTable() {
|
|
328
|
+
const rows = [];
|
|
329
|
+
for (const [modelSlug, entry] of Object.entries(PRICES)) {
|
|
330
|
+
for (const p of entry.periods) {
|
|
331
|
+
rows.push({
|
|
332
|
+
modelSlug,
|
|
333
|
+
from: p.from ?? 0,
|
|
334
|
+
input: p.input,
|
|
335
|
+
output: p.output,
|
|
336
|
+
cacheRead: p.input * entry.cache.read,
|
|
337
|
+
cacheWrite5m: p.input * entry.cache.write5m,
|
|
338
|
+
cacheWrite1h: p.input * entry.cache.write1h,
|
|
339
|
+
source: entry.table,
|
|
340
|
+
vendor: entry.vendor
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return { id: BUNDLED_PRICE_TABLE_ID, rows };
|
|
345
|
+
}
|
|
346
|
+
var BUNDLED_INDEX = new PriceIndex(bundledPriceTable());
|
|
347
|
+
var BUNDLED_PRICER = new Pricer([BUNDLED_INDEX]);
|
|
348
|
+
|
|
349
|
+
// src/harness/shared/aggregate.ts
|
|
350
|
+
var asObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v) ? v : null;
|
|
351
|
+
var asStr = (v) => typeof v === "string" && v.length > 0 ? v : null;
|
|
352
|
+
|
|
353
|
+
// src/harness/shared/window.ts
|
|
354
|
+
function emptyScanStats() {
|
|
355
|
+
return {
|
|
356
|
+
filesFound: 0,
|
|
357
|
+
filesRead: 0,
|
|
358
|
+
filesSkippedByMtime: 0,
|
|
359
|
+
filesSkippedAsDuplicate: 0,
|
|
360
|
+
filesUnreadable: 0,
|
|
361
|
+
filesForeign: 0,
|
|
362
|
+
foreignOriginators: /* @__PURE__ */ new Map(),
|
|
363
|
+
unreadableFiles: [],
|
|
364
|
+
filesZstdUnsupported: 0
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// src/harness/cursor/evidence.ts
|
|
369
|
+
function count(value) {
|
|
370
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
371
|
+
}
|
|
372
|
+
function tokenEvidence(value) {
|
|
373
|
+
const raw = asObj(value) ?? {};
|
|
374
|
+
const direct = asObj(raw.tokenCount);
|
|
375
|
+
const usage = asObj(raw.usage);
|
|
376
|
+
for (const [obj, fields] of [
|
|
377
|
+
[
|
|
378
|
+
direct,
|
|
379
|
+
["inputTokens", "outputTokens", "cacheReadTokens", "cacheWriteTokens"]
|
|
380
|
+
],
|
|
381
|
+
[
|
|
382
|
+
usage,
|
|
383
|
+
[
|
|
384
|
+
"input_tokens",
|
|
385
|
+
"output_tokens",
|
|
386
|
+
"cache_read_input_tokens",
|
|
387
|
+
"cache_creation_input_tokens"
|
|
388
|
+
]
|
|
389
|
+
]
|
|
390
|
+
]) {
|
|
391
|
+
for (const field of fields)
|
|
392
|
+
if (obj?.[field] != null && count(obj[field]) === void 0)
|
|
393
|
+
throw new Error("Invalid Cursor token evidence");
|
|
394
|
+
}
|
|
395
|
+
const out = {};
|
|
396
|
+
out.input = count(direct?.inputTokens) ?? count(usage?.input_tokens);
|
|
397
|
+
out.output = count(direct?.outputTokens) ?? count(usage?.output_tokens);
|
|
398
|
+
out.cacheRead = count(direct?.cacheReadTokens) ?? count(usage?.cache_read_input_tokens);
|
|
399
|
+
out.cacheWrite = count(direct?.cacheWriteTokens) ?? count(usage?.cache_creation_input_tokens);
|
|
400
|
+
if (out.input !== void 0) out.inputSource = "reported";
|
|
401
|
+
if (out.output !== void 0) out.outputSource = "reported";
|
|
402
|
+
if (out.input === void 0) {
|
|
403
|
+
const context = count(asObj(raw.contextWindowStatusAtCreation)?.tokensUsed);
|
|
404
|
+
if (context !== void 0) {
|
|
405
|
+
out.input = context;
|
|
406
|
+
out.inputSource = "context";
|
|
407
|
+
} else if (typeof raw.promptDryRunInfo === "string") {
|
|
408
|
+
try {
|
|
409
|
+
const dry = asObj(JSON.parse(raw.promptDryRunInfo));
|
|
410
|
+
const estimate = count(asObj(dry?.fullConversationTokenCount)?.numTokens) ?? count(asObj(dry?.userMessageTokenCount)?.numTokens);
|
|
411
|
+
if (estimate !== void 0) {
|
|
412
|
+
out.input = estimate;
|
|
413
|
+
out.inputSource = "dry-run";
|
|
414
|
+
}
|
|
415
|
+
} catch {
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return out;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// src/harness/cursor/local.ts
|
|
423
|
+
function dataPath(env = process.env, home = homedir(), platform = process.platform) {
|
|
424
|
+
if (env.CURSOR_DATA_PATH) return path.resolve(env.CURSOR_DATA_PATH);
|
|
425
|
+
const base = platform === "darwin" ? path.join(home, "Library", "Application Support") : platform === "win32" ? env.APPDATA || path.join(home, "AppData", "Roaming") : env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
426
|
+
return path.join(base, "Cursor", "User", "workspaceStorage");
|
|
427
|
+
}
|
|
428
|
+
var storeRoot = () => process.env.CURSOR_STORE_ROOT || path.join(homedir(), ".cursor");
|
|
429
|
+
var globalDb = (root) => path.join(path.dirname(root), "globalStorage", "state.vscdb");
|
|
430
|
+
async function exists(file) {
|
|
431
|
+
try {
|
|
432
|
+
await stat(file);
|
|
433
|
+
return true;
|
|
434
|
+
} catch (error) {
|
|
435
|
+
return error.code !== "ENOENT";
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
async function hasLocalSource(root = dataPath()) {
|
|
439
|
+
return (await Promise.all(
|
|
440
|
+
[
|
|
441
|
+
root,
|
|
442
|
+
globalDb(root),
|
|
443
|
+
path.join(storeRoot(), "projects"),
|
|
444
|
+
path.join(storeRoot(), "chats"),
|
|
445
|
+
path.join(storeRoot(), "acp-sessions")
|
|
446
|
+
].map(exists)
|
|
447
|
+
)).some(Boolean);
|
|
448
|
+
}
|
|
449
|
+
async function readTokenEvidence(root, session) {
|
|
450
|
+
const out = /* @__PURE__ */ new Map();
|
|
451
|
+
if (!session.messages.some((m) => m.identityOrigin?.startsWith("composer")))
|
|
452
|
+
return out;
|
|
453
|
+
const file = globalDb(root);
|
|
454
|
+
if (!await exists(file)) return out;
|
|
455
|
+
const { DatabaseSync } = await import("node:sqlite");
|
|
456
|
+
const db = new DatabaseSync(file, { readOnly: true });
|
|
457
|
+
try {
|
|
458
|
+
db.exec("BEGIN");
|
|
459
|
+
const table = db.prepare(
|
|
460
|
+
"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'cursorDiskKV'"
|
|
461
|
+
).get();
|
|
462
|
+
if (!table) return out;
|
|
463
|
+
const query = db.prepare(`SELECT json_object(
|
|
464
|
+
'tokenCount', json_extract(value, '$.tokenCount'),
|
|
465
|
+
'usage', json_extract(value, '$.usage'),
|
|
466
|
+
'contextWindowStatusAtCreation', json_extract(value, '$.contextWindowStatusAtCreation'),
|
|
467
|
+
'promptDryRunInfo', json_extract(value, '$.promptDryRunInfo')) AS evidence
|
|
468
|
+
FROM cursorDiskKV WHERE key = ?`);
|
|
469
|
+
for (const message of session.messages) {
|
|
470
|
+
if (!message.id || message.identityOrigin !== "composer-native") continue;
|
|
471
|
+
const row = query.get(`bubbleId:${session.id}:${message.id}`);
|
|
472
|
+
if (typeof row?.evidence === "string")
|
|
473
|
+
out.set(message.id, tokenEvidence(JSON.parse(row.evidence)));
|
|
474
|
+
}
|
|
475
|
+
const header = db.prepare(
|
|
476
|
+
"SELECT json_extract(value, '$.conversation') AS conversation FROM cursorDiskKV WHERE key = ?"
|
|
477
|
+
).get(`composerData:${session.id}`);
|
|
478
|
+
if (typeof header?.conversation === "string") {
|
|
479
|
+
const conversation = JSON.parse(header.conversation);
|
|
480
|
+
if (Array.isArray(conversation))
|
|
481
|
+
for (const [index, raw] of conversation.entries()) {
|
|
482
|
+
const obj = asObj(raw);
|
|
483
|
+
const id = asStr(obj?.bubbleId) ?? asStr(obj?.id) ?? `msg:${index}`;
|
|
484
|
+
if (!out.has(id)) out.set(id, tokenEvidence(raw));
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return out;
|
|
488
|
+
} finally {
|
|
489
|
+
db.close();
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
async function sourceStamp(root) {
|
|
493
|
+
const values = await Promise.all(
|
|
494
|
+
[globalDb(root), `${globalDb(root)}-wal`].map(async (file) => {
|
|
495
|
+
try {
|
|
496
|
+
const info = await stat(file);
|
|
497
|
+
return `${info.size}:${info.mtimeMs}`;
|
|
498
|
+
} catch {
|
|
499
|
+
return "unavailable";
|
|
500
|
+
}
|
|
501
|
+
})
|
|
502
|
+
);
|
|
503
|
+
return values.join("/");
|
|
504
|
+
}
|
|
505
|
+
async function readLocalOnce(root, before, onProgress) {
|
|
506
|
+
const stats = emptyScanStats();
|
|
507
|
+
const out = { sessions: [], complete: true, stats };
|
|
508
|
+
if (!await hasLocalSource(root)) {
|
|
509
|
+
trace("cursor \xB7 no local source");
|
|
510
|
+
return out;
|
|
511
|
+
}
|
|
512
|
+
trace(`cursor \xB7 global database ${await globalDbSize(root)}`);
|
|
513
|
+
const readDone = traceTimer("cursor history read");
|
|
514
|
+
let context;
|
|
515
|
+
try {
|
|
516
|
+
const reader = await import("cursor-history");
|
|
517
|
+
const options = {
|
|
518
|
+
dataPath: root,
|
|
519
|
+
sqliteDriver: "node:sqlite",
|
|
520
|
+
onDiagnostic: () => {
|
|
521
|
+
out.complete = false;
|
|
522
|
+
},
|
|
523
|
+
signal: AbortSignal.timeout(12e4)
|
|
524
|
+
};
|
|
525
|
+
context = reader.createSessionReadContext(options);
|
|
526
|
+
const config = { ...options, readContext: context };
|
|
527
|
+
let offset = 0;
|
|
528
|
+
while (true) {
|
|
529
|
+
const pageDone = traceTimer(
|
|
530
|
+
offset === 0 ? "cursor session listing" : "cursor session page"
|
|
531
|
+
);
|
|
532
|
+
const page = await reader.listSessionSummaries({
|
|
533
|
+
...config,
|
|
534
|
+
offset,
|
|
535
|
+
limit: 100
|
|
536
|
+
});
|
|
537
|
+
pageDone(`${page.data.length} of ${page.pagination.total} sessions`);
|
|
538
|
+
if (offset === 0) onProgress?.(0, page.pagination.total);
|
|
539
|
+
for (const summary of page.data) {
|
|
540
|
+
stats.filesFound++;
|
|
541
|
+
if (summary.resolutionState === "ambiguous") {
|
|
542
|
+
out.complete = false;
|
|
543
|
+
}
|
|
544
|
+
try {
|
|
545
|
+
const session = await reader.getSession(summary.id, config);
|
|
546
|
+
if (session.resolutionState !== "complete" || session.messages.some((m) => m.metadata?.corrupted))
|
|
547
|
+
out.complete = false;
|
|
548
|
+
const tokens = await readTokenEvidence(root, session);
|
|
549
|
+
out.sessions.push({ session, tokens });
|
|
550
|
+
stats.filesRead++;
|
|
551
|
+
onProgress?.(stats.filesRead, page.pagination.total);
|
|
552
|
+
} catch {
|
|
553
|
+
out.complete = false;
|
|
554
|
+
stats.filesUnreadable++;
|
|
555
|
+
} finally {
|
|
556
|
+
context.releaseSession(summary.id);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
if (!page.pagination.hasMore) break;
|
|
560
|
+
if (page.data.length === 0 || offset >= 1e5) {
|
|
561
|
+
out.complete = false;
|
|
562
|
+
break;
|
|
563
|
+
}
|
|
564
|
+
offset += page.data.length;
|
|
565
|
+
}
|
|
566
|
+
} catch {
|
|
567
|
+
out.complete = false;
|
|
568
|
+
stats.filesUnreadable++;
|
|
569
|
+
} finally {
|
|
570
|
+
try {
|
|
571
|
+
await context?.dispose();
|
|
572
|
+
} catch {
|
|
573
|
+
out.complete = false;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (before !== await sourceStamp(root)) out.complete = false;
|
|
577
|
+
readDone(
|
|
578
|
+
`${stats.filesRead}/${stats.filesFound} sessions read, ${stats.filesUnreadable} unreadable${out.complete ? "" : ", incomplete"}`
|
|
579
|
+
);
|
|
580
|
+
return out;
|
|
581
|
+
}
|
|
582
|
+
async function globalDbSize(root) {
|
|
583
|
+
try {
|
|
584
|
+
const info = await stat(globalDb(root));
|
|
585
|
+
return `${(info.size / 1048576).toFixed(0)} MB`;
|
|
586
|
+
} catch {
|
|
587
|
+
return "absent";
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// src/harness/cursor/worker.ts
|
|
592
|
+
var port = parentPort;
|
|
593
|
+
if (!port) throw new Error("cursor worker started without a parent port");
|
|
594
|
+
var post = (message) => port.postMessage(message);
|
|
595
|
+
var input = workerData;
|
|
596
|
+
if (input.trace) enableTrace();
|
|
597
|
+
readLocalOnce(
|
|
598
|
+
input.root,
|
|
599
|
+
input.before,
|
|
600
|
+
(files, total) => post({ kind: "progress", files, ...total !== void 0 ? { total } : {} })
|
|
601
|
+
).then(
|
|
602
|
+
(read) => post({ kind: "result", read }),
|
|
603
|
+
() => post({ kind: "error" })
|
|
604
|
+
);
|
|
605
|
+
//# sourceMappingURL=cursor-worker.js.map
|