limitcast 0.17.2 → 0.18.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/src/install.mjs CHANGED
@@ -112,6 +112,13 @@ export function install({ guard, alerts = false, path = settingsPath(), codexPat
112
112
  isToolCheck,
113
113
  );
114
114
  changes.push('added the large tool-output notice');
115
+ upsertHook(
116
+ cfg,
117
+ 'PreToolUse',
118
+ { matcher: 'Bash|Read|Grep|WebFetch|mcp__.*', hooks: [{ type: 'command', command: `${self} tool-limit`, timeout: 10 }] },
119
+ isToolLimit,
120
+ );
121
+ changes.push('added the fat-tool limit check');
115
122
  }
116
123
  writeSettings(path, cfg);
117
124
  if (guard && existsSync(join(codexPath, '..'))) {
@@ -174,6 +181,7 @@ export function uninstall({ path = settingsPath(), codexPath = codexHooksPath(),
174
181
  ['UserPromptSubmit', isPromptCheck, 'the cold-cache warning'],
175
182
  ['SubagentStop', isReceipt, 'subagent receipts'],
176
183
  ['PostToolUse', isToolCheck, 'the large tool-output notice'],
184
+ ['PreToolUse', isToolLimit, 'the fat-tool limit check'],
177
185
  ])) {
178
186
  const n = cfg.hooks?.[event]?.length ?? 0;
179
187
  if (!n) continue;
@@ -231,6 +239,7 @@ const isAlert = runs(/\bdrift --hook\b/);
231
239
  const isPromptCheck = runs(/\bprompt-check\b/);
232
240
  const isReceipt = runs(/\bsubagent-receipt\b/);
233
241
  const isToolCheck = runs(/\btool-check\b/);
242
+ const isToolLimit = runs(/\btool-limit\b/);
234
243
 
235
244
  /** @param {string} path */
236
245
  export function readSettings(path) {
package/src/results.mjs CHANGED
@@ -146,8 +146,12 @@ export function resultsStats({ now, days, samples, blocks, pro }) {
146
146
  const switches = mine.filter((b) => b.kind === 'switch').length;
147
147
  const defaults = mine.filter((b) => b.kind === 'default').length;
148
148
  const colds = mine.filter((b) => b.kind === 'cold');
149
+ const coldNotices = mine.filter((b) => b.kind === 'cold-notice').length;
149
150
  const budgets = mine.filter((b) => b.kind === 'budget').length;
150
151
  const todays = mine.filter((b) => b.kind === 'today').length;
152
+ const duplicates = mine.filter((b) => b.kind === 'duplicate').length;
153
+ const toolLimits = mine.filter((b) => b.kind === 'tool-limit').length;
154
+ const toolCaps = mine.filter((b) => b.kind === 'tool-cap').length;
151
155
  return {
152
156
  readings,
153
157
  hits,
@@ -160,6 +164,10 @@ export function resultsStats({ now, days, samples, blocks, pro }) {
160
164
  coldPct: colds.reduce((a, b) => a + b.pct, 0),
161
165
  budgets,
162
166
  todays,
167
+ coldNotices,
168
+ duplicates,
169
+ toolLimits,
170
+ toolCaps,
163
171
  };
164
172
  }
165
173
 
@@ -295,13 +303,15 @@ export function resultsReport({ now, days, samples, blocks, pro, saved, value =
295
303
  `${pro ? 'Ran' : 'Pro would have run'} ${st.defaults} subagent${st.defaults === 1 ? '' : 's'} on the model you set for their type (limitcast subagent-model).`,
296
304
  );
297
305
  if (st.budgets) out.push(`Held ${st.budgets} subagent${st.budgets === 1 ? '' : 's'} once subagents had used their budget of the 5-hour window.`);
306
+ if (st.duplicates)
307
+ out.push(`Held ${st.duplicates} duplicate subagent${st.duplicates === 1 ? '' : 's'} that repeated a prompt already running in that session.`);
298
308
  if (st.todays)
299
309
  out.push(
300
310
  `${pro ? 'Held' : 'Pro could have held'} ${st.todays} subagent${st.todays === 1 ? '' : 's'} once today's share of a weekly window was used up (limitcast budget today on).`,
301
311
  );
302
312
 
303
313
  const recent = subagents.filter((e) => e.t >= now - days * 86400_000);
304
- if (recent.length || st.colds) {
314
+ if (recent.length || st.colds || st.coldNotices) {
305
315
  out.push('', '## Subagents and cold caches');
306
316
  const priced = recent.filter((e) => e.pct !== undefined).map((e) => /** @type {number} */ (e.pct));
307
317
  if (recent.length)
@@ -316,6 +326,16 @@ export function resultsReport({ now, days, samples, blocks, pro, saved, value =
316
326
  `${pro ? 'Held' : 'Pro would have held'} ${st.colds} message${st.colds === 1 ? '' : 's'} sent after the prompt cache had expired` +
317
327
  (st.coldPct ? `, each re-sending the whole context (≈${pctText(st.coldPct)} of a 5-hour window in all).` : '.'),
318
328
  );
329
+ if (st.coldNotices)
330
+ out.push(
331
+ `Noted ${st.coldNotices} message${st.coldNotices === 1 ? '' : 's'} sent after a shorter idle, while the cheap prompt cache may already have expired.`,
332
+ );
333
+ }
334
+ if (st.toolLimits || st.toolCaps) {
335
+ out.push('', '## Fat tool calls');
336
+ if (st.toolLimits)
337
+ out.push(`Held ${st.toolLimits} call${st.toolLimits === 1 ? '' : 's'} that set no limit on a tool whose recent results were large.`);
338
+ if (st.toolCaps) out.push(`Lowered the limit on ${st.toolCaps} call${st.toolCaps === 1 ? '' : 's'} whose recent results were large.`);
319
339
  }
320
340
 
321
341
  out.push('', '## Starting context');
package/src/startup.mjs CHANGED
@@ -115,6 +115,34 @@ export function skillPieces(content) {
115
115
  });
116
116
  }
117
117
 
118
+ /**
119
+ * Pieces whose size changed between two runs of the same job. Char counts are summed per group and name.
120
+ * @param {Piece[]} earlier
121
+ * @param {Piece[]} later
122
+ */
123
+ export function pieceDiff(earlier, later) {
124
+ const sum = (/** @type {Piece[]} */ list) => {
125
+ /** @type {Map<string, number>} */
126
+ const m = new Map();
127
+ for (const p of list ?? []) {
128
+ const k = `${p.group}|${p.name}`;
129
+ m.set(k, (m.get(k) ?? 0) + p.chars);
130
+ }
131
+ return m;
132
+ };
133
+ const a = sum(earlier);
134
+ const b = sum(later);
135
+ return [...new Set([...a.keys(), ...b.keys()])]
136
+ .map((k) => {
137
+ const split = k.indexOf('|');
138
+ const from = a.get(k) ?? 0;
139
+ const to = b.get(k) ?? 0;
140
+ return { group: k.slice(0, split), name: k.slice(split + 1), from, to, delta: to - from };
141
+ })
142
+ .filter((x) => x.delta !== 0)
143
+ .sort((x, y) => Math.abs(y.delta) - Math.abs(x.delta));
144
+ }
145
+
118
146
  /** @param {unknown} v */
119
147
  const text = (v) => (typeof v === 'string' ? v : v == null ? '' : JSON.stringify(v));
120
148
 
@@ -0,0 +1,137 @@
1
+ // @ts-check
2
+ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
3
+ import { join } from 'node:path';
4
+ import { stateDir } from './store.mjs';
5
+ import { toolKey } from './toolcheck.mjs';
6
+
7
+ // PreToolUse: a tool whose recent results are fat, and whose call sets no limit, is denied before it runs.
8
+ // Rewriting a limit that is already present but huge keeps the call and caps it. Bash and WebFetch are left
9
+ // alone: they have no limit argument, so a missing one is every call. Stats come from `report --why` / `--jobs`.
10
+
11
+ export const FAT_AVG = 5_000;
12
+ export const MIN_CALLS = 3;
13
+ const MCP_CAP = 100;
14
+ const READ_CAP = 400;
15
+ const LIMIT_KEYS = new Set(['limit', 'max_results', 'maxresults', 'page_size', 'pagesize', 'top_k', 'topk', 'first']);
16
+
17
+ /** @typedef {{avg:number, calls:number}} ToolStat */
18
+ /** @typedef {{path:string[], value:number}} LimitField */
19
+
20
+ /**
21
+ * @param {Map<string, {tokens:number, calls:number}>} tools attribution totals, keyed the way reports key them
22
+ */
23
+ export function toolStats(tools) {
24
+ /** @type {Record<string, ToolStat>} */
25
+ const stats = {};
26
+ for (const [key, e] of tools) {
27
+ if (e.calls > 0) stats[key] = { avg: Math.round(e.tokens / e.calls), calls: e.calls };
28
+ }
29
+ return stats;
30
+ }
31
+
32
+ const statsFile = () => join(stateDir(), 'tool-stats.json');
33
+
34
+ /** @param {Record<string, ToolStat>} stats */
35
+ export function saveToolStats(stats) {
36
+ mkdirSync(stateDir(), { recursive: true });
37
+ writeFileSync(statsFile(), JSON.stringify(stats));
38
+ }
39
+
40
+ /** @returns {Record<string, ToolStat>} */
41
+ export function loadToolStats() {
42
+ try {
43
+ const parsed = JSON.parse(readFileSync(statsFile(), 'utf8'));
44
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {};
45
+ } catch {
46
+ return {};
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Limit-shaped numbers anywhere in the arguments, a few levels down (MCP tools nest them).
52
+ * @param {unknown} input
53
+ * @returns {LimitField[]}
54
+ */
55
+ export function findLimits(input) {
56
+ /** @type {LimitField[]} */
57
+ const found = [];
58
+ const walk = (/** @type {any} */ node, /** @type {string[]} */ path, /** @type {number} */ depth) => {
59
+ if (!node || typeof node !== 'object' || depth > 4) return;
60
+ for (const [key, value] of Object.entries(node)) {
61
+ const here = [...path, key];
62
+ if (LIMIT_KEYS.has(key.toLowerCase())) {
63
+ const n = typeof value === 'number' ? value : typeof value === 'string' && /^\d+$/.test(value) ? Number(value) : NaN;
64
+ if (Number.isFinite(n)) found.push({ path: here, value: n });
65
+ } else walk(value, here, depth + 1);
66
+ }
67
+ };
68
+ walk(input, [], 0);
69
+ return found;
70
+ }
71
+
72
+ /**
73
+ * @param {any} input
74
+ * @param {string[]} path
75
+ * @param {number} value
76
+ */
77
+ function setAt(input, path, value) {
78
+ const copy = structuredClone(input);
79
+ let node = copy;
80
+ for (let i = 0; i < path.length - 1; i++) node = node[path[i]];
81
+ node[path[path.length - 1]] = value;
82
+ return copy;
83
+ }
84
+
85
+ const k = (/** @type {number} */ n) => `${Math.max(1, Math.round(n / 1000))}k`;
86
+
87
+ /**
88
+ * What to do with one tool call, or null to leave it alone.
89
+ * Deny reaches the model as the hook reason so it can retry with a limit. A cap rewrites the arguments in place.
90
+ * @param {string} toolName
91
+ * @param {unknown} input
92
+ * @param {Record<string, ToolStat>} stats
93
+ * @returns {{deny:string}|{updatedInput:any, reason:string}|null}
94
+ */
95
+ export function toolLimitDecision(toolName, input, stats) {
96
+ const key = toolKey(toolName);
97
+ const stat = stats?.[key];
98
+ if (!stat || stat.calls < MIN_CALLS || stat.avg < FAT_AVG) return null;
99
+ const args = typeof input === 'string' ? tryJson(input) : input;
100
+ const obj = args && typeof args === 'object' ? args : {};
101
+ if (toolName === 'Grep' || key === 'Grep') {
102
+ const path = /** @type {any} */ (obj).path ?? /** @type {any} */ (obj).file_path;
103
+ const glob = /** @type {any} */ (obj).glob;
104
+ if ((typeof path === 'string' && path.trim()) || (typeof glob === 'string' && glob.trim())) return null;
105
+ return {
106
+ deny: `limitcast: Grep results average ≈${k(stat.avg)} tokens a call over ${stat.calls} calls. Add a path or a glob, then retry. Set LIMITCAST_ALLOW=1 to run it as written.`,
107
+ };
108
+ }
109
+ if (toolName !== 'Read' && key !== 'Read' && !toolName.startsWith('mcp__') && !key.startsWith('mcp:')) return null;
110
+ const cap = toolName === 'Read' || key === 'Read' ? READ_CAP : MCP_CAP;
111
+ const limits = findLimits(obj);
112
+ if (limits.some((f) => f.value > 0 && f.value <= cap)) return null;
113
+ const big = limits.find((f) => f.value > cap);
114
+ const label = toolName === 'Read' || key === 'Read' ? 'Read' : key.replace(/^mcp:/, '');
115
+ if (big) {
116
+ return {
117
+ updatedInput: setAt(obj, big.path, cap),
118
+ reason: `limitcast: ${label} results average ≈${k(stat.avg)} tokens a call, so this limit was lowered to ${cap}. Set LIMITCAST_ALLOW=1 to keep ${big.value}.`,
119
+ };
120
+ }
121
+ const what =
122
+ toolName === 'Read' || key === 'Read'
123
+ ? `Pass offset and limit (${cap} lines or fewer)`
124
+ : `Add a limit of ${cap} or fewer, and ask for fewer fields`;
125
+ return {
126
+ deny: `limitcast: ${label} results average ≈${k(stat.avg)} tokens a call over ${stat.calls} calls. This call sets no limit. ${what}, then retry. Set LIMITCAST_ALLOW=1 to run it as written.`,
127
+ };
128
+ }
129
+
130
+ /** @param {string} text */
131
+ function tryJson(text) {
132
+ try {
133
+ return JSON.parse(text);
134
+ } catch {
135
+ return {};
136
+ }
137
+ }
package/src/trim.mjs CHANGED
@@ -140,12 +140,21 @@ export function savings(c, used) {
140
140
  * @param {ReturnType<typeof contributors>} c
141
141
  * @param {Activity} used
142
142
  * @param {number} days
143
+ * @param {boolean} [share] totals only, including when no startup transcript is available
143
144
  */
144
- export function trimReport(base, c, used, days) {
145
+ export function trimReport(base, c, used, days, share = false) {
145
146
  const s = savings(c, used);
146
147
  const fileTokens = c.files.reduce((a, f) => a + f.tokens, 0);
147
148
  const visible = c.skills.filter((k) => !k.hidden);
148
149
  const listing = visible.reduce((a, k) => a + k.tokens, 0);
150
+ if (share)
151
+ return [
152
+ base ? `Claude starting context: ${fmt(base.median)} tokens across ${base.sessions} sessions.` : 'Claude starting context: unavailable.',
153
+ `Estimated removable skill listings: ~${fmt(s.skillTokens)} tokens per session.`,
154
+ `Unused in ${days} days: ${s.unusedSkills.length} skills, ${s.unusedMcp.length} MCP servers (server sizes unavailable).`,
155
+ `Instruction files: ${c.files.length}, ~${fmt(fileTokens)} tokens. Visible skill listings: ~${fmt(listing)} tokens.`,
156
+ 'Totals only; no configuration names or paths. https://limitcast.dev',
157
+ ].join('\n');
149
158
  const lines = ['# limitcast trim', ''];
150
159
  if (base) {
151
160
  lines.push(
package/src/where.mjs CHANGED
@@ -4,6 +4,7 @@ import { homedir } from 'node:os';
4
4
  import { join } from 'node:path';
5
5
  import { createInterface } from 'node:readline';
6
6
  import { fmt } from './report.mjs';
7
+ import { attachmentPieces, pieceDiff } from './startup.mjs';
7
8
  import { stateDir } from './store.mjs';
8
9
  import { BIG_RESULT, tipFor, toolKey } from './toolcheck.mjs';
9
10
  import { jsonlFiles } from './usage.mjs';
@@ -13,8 +14,8 @@ import { jsonlFiles } from './usage.mjs';
13
14
  // call i-1: its own output (written to the cache) plus the tool results it asked for plus your messages. So the input
14
15
  // of call i, less call i-1's output, is attributed to the tools call i-1 used. Claude Code transcripts only.
15
16
 
16
- /** @typedef {{t:number, input:number, cacheWrite:number, cacheRead:number, output:number, tools:string[], side:boolean, entry:string, project:string, model?:string, prompt?:string, agentType?:string}} Call
17
- * prompt: first words of the session's first message, on its first main-thread call; agentType: a subagent transcript's type, on its first call */
17
+ /** @typedef {{t:number, input:number, cacheWrite:number, cacheRead:number, output:number, tools:string[], side:boolean, entry:string, project:string, model?:string, prompt?:string, agentType?:string, startup?:import('./startup.mjs').Piece[]}} Call
18
+ * prompt: first words of the session's first message, on its first main-thread call; agentType: a subagent transcript's type, on its first call; startup: context loaded before the first response */
18
19
 
19
20
  const COLD_MIN = 100_000; // prompt size from which a missed cache is called a rebuild after idle
20
21
  const NO_TOOL = '(your messages)';
@@ -48,7 +49,18 @@ export async function readCalls(file) {
48
49
  const calls = new Map();
49
50
  /** @type {string|undefined} */
50
51
  let prompt;
52
+ /** @type {import('./startup.mjs').Piece[]} */
53
+ const startup = [];
54
+ let startupOpen = true;
51
55
  for await (const line of createInterface({ input: createReadStream(file), crlfDelay: Infinity })) {
56
+ if (startupOpen && line.includes('"type":"attachment"')) {
57
+ try {
58
+ const ev = JSON.parse(line);
59
+ if (ev?.type === 'attachment') startup.push(...attachmentPieces(ev.attachment));
60
+ } catch {
61
+ /* a torn line: the call list is what matters */
62
+ }
63
+ }
52
64
  if (prompt === undefined && line.includes('"type":"user"')) prompt = firstPrompt(line);
53
65
  if (!line.includes('"usage"')) continue;
54
66
  let ev;
@@ -61,6 +73,7 @@ export async function readCalls(file) {
61
73
  const u = m?.usage;
62
74
  const t = Date.parse(ev?.timestamp);
63
75
  if (ev?.type !== 'assistant' || !u || !Number.isFinite(t)) continue;
76
+ startupOpen = false;
64
77
  const id = `${m.id ?? ''}:${ev.requestId ?? ''}`;
65
78
  let c = calls.get(id);
66
79
  if (!c || id === ':') {
@@ -85,6 +98,7 @@ export async function readCalls(file) {
85
98
  const all = [...calls.values()];
86
99
  const first = all.find((c) => !c.side);
87
100
  if (first && prompt) first.prompt = prompt;
101
+ if (first && startup.length) first.startup = startup;
88
102
  // A subagent's transcript has its type in a file next to it: agent-<id>.jsonl and agent-<id>.meta.json.
89
103
  if (all.length && file.includes('/subagents/')) {
90
104
  try {
@@ -117,7 +131,7 @@ export function attribute(sessions, since = 0) {
117
131
  tools: new Map(),
118
132
  /** @type {{tokens:number, tool:string, t:number, project:string}[]} */
119
133
  steps: [],
120
- /** @type {{t:number, tokens:number, size:number, calls:number, prompt:string, project:string, entry:string}[]} headless session starts: when, new tokens, and the whole starting prompt (cached or not) */
134
+ /** @type {{t:number, tokens:number, size:number, calls:number, prompt:string, project:string, entry:string, startup?:import('./startup.mjs').Piece[]}[]} headless session starts: when, new tokens, and the whole starting prompt (cached or not) */
121
135
  starts: [],
122
136
  /** @type {Map<string, number>} every new token, by model */
123
137
  byModel: new Map(),
@@ -165,6 +179,7 @@ export function attribute(sessions, since = 0) {
165
179
  prompt: c.prompt ?? '',
166
180
  project: c.project,
167
181
  entry: c.entry,
182
+ ...(c.startup?.length ? { startup: c.startup } : {}),
168
183
  });
169
184
  }
170
185
  return;
@@ -340,6 +355,40 @@ function coldSection(runs, jobs) {
340
355
  return lines;
341
356
  }
342
357
 
358
+ const tok = (/** @type {number} */ chars) => Math.round(chars / 4);
359
+
360
+ /**
361
+ * Which startup piece changed between the earliest and latest recorded run of a job.
362
+ * Only jobs with two startup breakdowns are named. A stable prefix is omitted.
363
+ * @param {[string, {starts:{startup?:import('./startup.mjs').Piece[]}[]} ][]} ranked
364
+ */
365
+ function driftSection(ranked) {
366
+ /** @type {string[]} */
367
+ const rows = [];
368
+ for (const [name, job] of ranked) {
369
+ const marked = job.starts.filter((s) => s.startup?.length);
370
+ if (marked.length < 2) continue;
371
+ const diff = pieceDiff(
372
+ /** @type {import('./startup.mjs').Piece[]} */ (marked[0].startup),
373
+ /** @type {import('./startup.mjs').Piece[]} */ (marked[marked.length - 1].startup),
374
+ );
375
+ for (const d of diff.slice(0, 5)) {
376
+ rows.push(`| ${name.replace(/\|/g, '/').slice(0, 45)} | ${d.group}: ${d.name} | ${fmt(tok(d.from))} | ${fmt(tok(d.to))} |`);
377
+ }
378
+ }
379
+ if (!rows.length) return [];
380
+ return [
381
+ '',
382
+ '## What changed between runs',
383
+ '',
384
+ 'The earliest and latest run of each job that recorded its startup. A piece that changes every run (a hook that prints a date or a count) keeps the prompt cache from being reused, so running the job again sooner does not warm it.',
385
+ '',
386
+ '| job | piece | earlier | later |',
387
+ '|---|---|---:|---:|',
388
+ ...rows,
389
+ ];
390
+ }
391
+
343
392
  /**
344
393
  * Markdown: the scheduled (headless) jobs whose runs spent the most new tokens just starting. A run's start is what
345
394
  * it loads before reading its prompt: settings, plugins, hooks, MCP servers, skills and instruction files.
@@ -388,6 +437,7 @@ export function jobsReport(r, { since, now }) {
388
437
  );
389
438
  }
390
439
  lines.push(...coldSection(r.starts.length, [...jobs.values()]));
440
+ lines.push(...driftSection(ranked));
391
441
  const [leanJob, lean] = [...jobs].sort((a, b) => a[1].size / a[1].runs - b[1].size / b[1].runs)[0];
392
442
  lines.push(
393
443
  '',