limitcast 0.17.3 → 0.18.1

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/cold.mjs CHANGED
@@ -3,9 +3,10 @@ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
4
  import { stateDir } from './store.mjs';
5
5
 
6
- // Cold-cache warning (Pro, UserPromptSubmit hook). After a long pause the prompt cache has expired, so the next
7
- // message re-sends the whole context as new tokens, which count against the limit far more than cached ones.
8
- // The first message after such a pause is held once with its cost; sending it again goes through.
6
+ // Cold-cache warning (UserPromptSubmit hook). After a pause the prompt cache has expired, so the next message
7
+ // re-sends the whole context as new tokens, which count against the limit far more than cached ones.
8
+ // From 5 minutes (the cheap cache) the first message is announced and still sent. From 60 minutes it is held
9
+ // once with its cost; sending it again goes through.
9
10
 
10
11
  const MIN_TOKENS = 100_000;
11
12
  const stateFile = () => join(stateDir(), 'cold.json');
@@ -15,18 +16,21 @@ const stateFile = () => join(stateDir(), 'cold.json');
15
16
  * @param {unknown} prompt
16
17
  * @param {{tokens:number, t:number}|null} last the session's latest model call
17
18
  * @param {number} now epoch ms
18
- * @param {{minutes:number, calib?:{fresh:number, read:number}|null}} o minutes: the cache lifetime
19
- * @returns {{tokens:number, idle:number, pct:number|null, warm:number|null}|null} pct/warm: % of the 5h window cold/warm
19
+ * @param {{minutes:number, noticeMinutes?:number, calib?:{fresh:number, read:number}|null}} o
20
+ * minutes: idle time that holds the message. noticeMinutes: idle time that only announces it (default: the hold).
21
+ * @returns {{tokens:number, idle:number, pct:number|null, warm:number|null, hold:boolean}|null} pct/warm: % of the 5h window cold/warm
20
22
  */
21
- export function coldCost(prompt, last, now, { minutes, calib }) {
23
+ export function coldCost(prompt, last, now, { minutes, noticeMinutes, calib }) {
22
24
  if (!last?.t || last.tokens < MIN_TOKENS) return null;
23
25
  const p = String(prompt ?? '').trimStart();
24
26
  // Background agents and other sessions report back as prompts; /compact and /clear are what the warning suggests.
25
27
  if (p.startsWith('<') || /^\/(compact|clear)\b/.test(p)) return null;
26
28
  const idle = Math.round((now - last.t) / 60_000);
27
- if (idle < minutes) return null;
29
+ // A notice threshold past the hold would skip the hold entirely. Keep the notice at or before it.
30
+ const noticeAt = Math.min(noticeMinutes ?? minutes, minutes);
31
+ if (idle < noticeAt) return null;
28
32
  const m = last.tokens / 1e6;
29
- return { tokens: last.tokens, idle, pct: calib ? m * calib.fresh : null, warm: calib ? m * calib.read : null };
33
+ return { tokens: last.tokens, idle, pct: calib ? m * calib.fresh : null, warm: calib ? m * calib.read : null, hold: idle >= minutes };
30
34
  }
31
35
 
32
36
  /** @param {NonNullable<ReturnType<typeof coldCost>>} c */
@@ -42,6 +46,22 @@ export function coldMessage(c) {
42
46
  );
43
47
  }
44
48
 
49
+ /**
50
+ * The short-idle announcement. The message is not held.
51
+ * @param {NonNullable<ReturnType<typeof coldCost>>} c
52
+ */
53
+ export function coldNotice(c) {
54
+ const idle = c.idle >= 120 ? `${Math.round(c.idle / 60)} hours` : `${c.idle} minutes`;
55
+ const cost =
56
+ c.pct !== null && c.warm !== null
57
+ ? `≈${pct(c.pct)} of your 5-hour window (≈${pct(c.warm)} if the cache were still warm)`
58
+ : 'all at the new-token rate instead of the much cheaper cached one';
59
+ return (
60
+ `limitcast: this session sat idle for ${idle}, so its cheap prompt cache may have expired. Your message re-sends ${Math.round(c.tokens / 1000)}k tokens of context: ${cost}. ` +
61
+ 'It is going through. Run /compact before a longer pause to shrink the next one.'
62
+ );
63
+ }
64
+
45
65
  const pct = (/** @type {number} */ x) => `${x < 1 ? x.toFixed(2) : x.toFixed(1)}%`;
46
66
 
47
67
  /**
@@ -51,6 +71,23 @@ const pct = (/** @type {number} */ x) => `${x < 1 ? x.toFixed(2) : x.toFixed(1)}
51
71
  * @param {number} lastT the latest model call's time
52
72
  */
53
73
  export function firstWarning(sid, lastT) {
74
+ return claim(lastT, sid);
75
+ }
76
+
77
+ /**
78
+ * True the first time this pause is announced (the message still goes through). Independent of the hold.
79
+ * @param {string} sid
80
+ * @param {number} lastT
81
+ */
82
+ export function firstNotice(sid, lastT) {
83
+ return claim(lastT, `n:${sid}`);
84
+ }
85
+
86
+ /**
87
+ * @param {number} lastT
88
+ * @param {string} key session id, or `n:` plus the session id for the idle notice
89
+ */
90
+ function claim(lastT, key) {
54
91
  /** @type {Record<string, number>} */
55
92
  let seen = {};
56
93
  try {
@@ -58,9 +95,9 @@ export function firstWarning(sid, lastT) {
58
95
  } catch {
59
96
  /* first warning */
60
97
  }
61
- if (seen[sid] === lastT) return false;
98
+ if (seen[key] === lastT) return false;
62
99
  const keep = Object.entries(seen).slice(-49); // the 49 most recent other sessions
63
100
  mkdirSync(stateDir(), { recursive: true });
64
- writeFileSync(stateFile(), JSON.stringify(Object.fromEntries([...keep.filter(([k]) => k !== sid), [sid, lastT]])));
101
+ writeFileSync(stateFile(), JSON.stringify(Object.fromEntries([...keep.filter(([k]) => k !== key), [key, lastT]])));
65
102
  return true;
66
103
  }
package/src/grok.mjs CHANGED
@@ -73,14 +73,16 @@ export function grokStartup(root = grokRoot()) {
73
73
  }
74
74
 
75
75
  /**
76
- * @param {{servers:{name:string, enabled:boolean|undefined}[], used:Set<string>, startup:{prompt:number, tools:number}|null, days:number, pro:boolean, keep?:string[]}} r
77
- * keep: the `trim keep` list; unused servers on it are reported as kept and left out of `unused`
76
+ * @param {{servers:{name:string, enabled:boolean|undefined}[], used:Set<string>, usedMonth?:Set<string>|null, startup:{prompt:number, tools:number}|null, days:number, pro:boolean, keep?:string[]}} r
77
+ * keep: the `trim keep` list; unused servers on it are reported as kept and left out of `unused`.
78
+ * usedMonth: normalized names used in 30 days. Servers in it stay out of `unused`.
78
79
  */
79
- export function grokTrimReport({ servers, used, startup, days, pro, keep = [] }) {
80
+ export function grokTrimReport({ servers, used, usedMonth = null, startup, days, pro, keep = [] }) {
80
81
  const on = servers.filter((s) => s.enabled !== false);
81
82
  const idle = on.filter((s) => !used.has(norm(s.name)));
82
83
  const kept = idle.filter((s) => keep.some((k) => norm(k) === norm(s.name)));
83
- const unused = idle.filter((s) => !kept.includes(s));
84
+ const standing = usedMonth ? idle.filter((s) => !kept.includes(s) && usedMonth.has(norm(s.name))) : [];
85
+ const unused = idle.filter((s) => !kept.includes(s) && !standing.includes(s));
84
86
  const tok = (/** @type {number} */ c) => `${(c / 4000).toFixed(1)}k`;
85
87
  const out = ['# limitcast trim --grok', ''];
86
88
  if (startup)
@@ -88,17 +90,28 @@ export function grokTrimReport({ servers, used, startup, days, pro, keep = [] })
88
90
  `Grok sessions start with ~${tok(startup.prompt)} tokens of system prompt and ~${tok(startup.tools)} of tool definitions, re-sent on every turn.`,
89
91
  '',
90
92
  );
91
- out.push(`## MCP servers: ${on.length} on, ${unused.length} not used in ${days} days`);
92
- if (!unused.length) out.push(kept.length ? 'Nothing else to turn off.' : 'Every server that is on was used.');
93
+ out.push(`## MCP servers: ${on.length} on, ${unused.length + standing.length} not used in ${days} days`);
94
+ if (standing.length)
95
+ out.push('', `${standing.length} used in the last 30 days, so \`trim --apply\` leaves them on:`, ...standing.map((s) => `- ${s.name}`));
96
+ if (!unused.length) out.push(kept.length || standing.length ? 'Nothing else to turn off.' : 'Every server that is on was used.');
93
97
  else if (pro)
94
- out.push(...unused.map((s) => `- ${s.name}`), '', 'Turn them off: `limitcast trim --grok --apply` (backed up; `--undo` reverses it).');
98
+ out.push(
99
+ ...unused.map((s) => `- ${s.name}`),
100
+ '',
101
+ `${usedMonth ? 'Turn the servers that have been quiet for 30 days off' : 'Turn them off'}: \`limitcast trim --grok --apply\` (backed up; \`--undo\` reverses it).`,
102
+ );
95
103
  else out.push(`Pro names them and turns them off for you: https://limitcast.dev/#pricing`);
96
104
  if (kept.length) out.push('', `Would keep (your keep-list): ${kept.map((s) => s.name).join(', ')}.`);
97
105
  out.push(
98
106
  '',
99
107
  'Each server that is on starts with every Grok session and adds its tool list to the context. Moving unused servers off is `enabled = false` in ~/.grok/config.toml.',
100
108
  );
101
- return { text: out.join('\n'), unused: unused.map((s) => s.name), kept: kept.map((s) => s.name) };
109
+ return {
110
+ text: out.join('\n'),
111
+ unused: unused.map((s) => s.name),
112
+ standing: standing.map((s) => s.name),
113
+ kept: kept.map((s) => s.name),
114
+ };
102
115
  }
103
116
 
104
117
  /**
package/src/guard.mjs CHANGED
@@ -8,6 +8,16 @@ import { budgetMessage, budgetSpent, getBudget, getTodayHold, loadSubagents, pin
8
8
 
9
9
  const NAMES = { claude: 'Claude', codex: 'Codex', grok: 'Grok' };
10
10
 
11
+ /**
12
+ * How far back a Codex guard read must go. Today's share of a weekly window needs a reading from before
13
+ * local midnight. Just after midnight, keep the last two hours so the pace still has a slope.
14
+ * @param {number} now epoch ms
15
+ */
16
+ export function codexGuardSince(now) {
17
+ const midnight = new Date(now).setHours(0, 0, 0, 0);
18
+ return Math.min(now - 2 * 3600_000, midnight - 60_000);
19
+ }
20
+
11
21
  /**
12
22
  * Whose limits a subagent-spawning tool call draws on, or null if the call spawns nothing.
13
23
  * Claude Code: Task/Agent. Codex: spawn_agent (reported with a namespace prefix). Grok: spawn_subagent.
@@ -65,12 +75,13 @@ export async function runGuard(hook, now, minutes, { preview = false } = {}) {
65
75
  const tool = String(hook?.tool_name ?? hook?.toolName ?? hook?.tool ?? '');
66
76
  const src = spawnSource(tool);
67
77
  const operation = hook?.tool_use_id ?? hook?.toolUseId ?? hook?.call_id;
68
- // Codex: only the last two hours of rollouts, so the hook stays fast. Grok: spend vs the user's dollar cap.
78
+ // Codex: back to local midnight, and at least two hours, so today's share can see a reading from before midnight.
79
+ // A two-hour cap hid that reading and the share read low. Grok: spend vs the user's dollar cap.
69
80
  const samples =
70
81
  src === 'claude'
71
82
  ? oneClaudeAccount(load(now - KEEP_MS), hook.session_id ?? hook.sessionId)
72
83
  : src === 'codex'
73
- ? await readCodex(now - 2 * 3600_000)
84
+ ? await readCodex(codexGuardSince(now))
74
85
  : src === 'grok'
75
86
  ? grokSamples(now)
76
87
  : [];
@@ -99,7 +110,7 @@ export async function runGuard(hook, now, minutes, { preview = false } = {}) {
99
110
  if (reason) return preview ? null : reason;
100
111
  // Today's share of a weekly window is used up: hold new subagents until local midnight, when the share starts over.
101
112
  // Opt-in for Pro (`limitcast budget today on`; the default is a notification at 80%); a free preview records it either way.
102
- // A window already at 100% is left to the tool. shortcut: Codex readings here span only 2 hours, so its share reads low.
113
+ // A window already at 100% is left to the tool.
103
114
  const day = todayOver(src, forecasts);
104
115
  if (day && (preview || getTodayHold())) {
105
116
  try {
@@ -0,0 +1,99 @@
1
+ // @ts-check
2
+ import { createHash } from 'node:crypto';
3
+ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
4
+ import { join } from 'node:path';
5
+ import { spawnSource } from './guard.mjs';
6
+ import { stateDir } from './store.mjs';
7
+
8
+ // A second spawn of the same type and prompt, in the same session, while the first has not finished.
9
+ // SubagentStop clears the Claude one. Codex and Grok have no stop event here, so a record older than
10
+ // 30 minutes is forgotten: a missed stop must not hold the same prompt for the rest of the day.
11
+
12
+ export const INFLIGHT_MS = 30 * 60_000;
13
+
14
+ /** @typedef {{sid:string, type:string, hash:string, t:number}} Inflight */
15
+
16
+ /**
17
+ * Identity of a spawn the duplicate hold can see, or null when the call is not one.
18
+ * An empty prompt or type is not tracked: those retries are how a host re-sends, and they are not the same request.
19
+ * @param {any} hook
20
+ */
21
+ export function spawnIdentity(hook) {
22
+ const tool = String(hook?.tool_name ?? hook?.toolName ?? hook?.tool ?? '');
23
+ const src = spawnSource(tool);
24
+ if (!src) return null;
25
+ const input = hook?.tool_input ?? hook?.toolInput ?? {};
26
+ const type = String(input?.subagent_type ?? input?.subagentType ?? input?.agent_type ?? '').trim();
27
+ const prompt = String(input?.prompt ?? input?.description ?? input?.task ?? '')
28
+ .replace(/\s+/g, ' ')
29
+ .trim();
30
+ const sid = String(hook?.session_id ?? hook?.sessionId ?? '').trim();
31
+ if (!type || !prompt || !sid) return null;
32
+ const hash = createHash('sha256').update(`${type.toLowerCase()}\0${prompt}`).digest('hex').slice(0, 16);
33
+ return { src, type, prompt, hash, sid };
34
+ }
35
+
36
+ /** @param {Inflight[]} entries @param {number} now @param {number} [maxAge] */
37
+ export function pruneInflight(entries, now, maxAge = INFLIGHT_MS) {
38
+ return entries.filter((e) => now - e.t >= 0 && now - e.t < maxAge);
39
+ }
40
+
41
+ /**
42
+ * The still-running spawn with this session, type and prompt, if any.
43
+ * @param {Inflight[]} entries already pruned
44
+ * @param {{sid:string, type:string, hash:string}} id
45
+ */
46
+ export function findDuplicate(entries, id) {
47
+ return entries.find((e) => e.sid === id.sid && e.hash === id.hash) ?? null;
48
+ }
49
+
50
+ /**
51
+ * @param {Inflight[]} entries already pruned
52
+ * @param {{sid:string, type:string, hash:string}} id
53
+ * @param {number} now
54
+ */
55
+ export function rememberInflight(entries, id, now) {
56
+ return [...entries, { sid: id.sid, type: id.type, hash: id.hash, t: now }];
57
+ }
58
+
59
+ /**
60
+ * Drop the oldest still-running spawn of this type in this session (SubagentStop does not carry the prompt).
61
+ * Clearing the oldest fails open: the spawn that is still running may be a different prompt, and that one is then allowed again.
62
+ * @param {Inflight[]} entries
63
+ * @param {string} sid
64
+ * @param {string} type
65
+ * @param {number} now
66
+ */
67
+ export function forgetInflight(entries, sid, type, now) {
68
+ const live = pruneInflight(entries, now);
69
+ if (!sid || !type) return live;
70
+ const i = live.findIndex((e) => e.sid === sid && e.type.toLowerCase() === type.toLowerCase());
71
+ if (i >= 0) live.splice(i, 1);
72
+ return live;
73
+ }
74
+
75
+ /** @param {{type:string}} id */
76
+ export function duplicateReason(id) {
77
+ return (
78
+ `limitcast: a ${id.type} subagent with this same prompt is already running in this session. ` +
79
+ 'Wait for it to finish, or set LIMITCAST_ALLOW=1 to spawn another.'
80
+ );
81
+ }
82
+
83
+ const file = () => join(stateDir(), 'inflight.json');
84
+
85
+ /** @returns {Inflight[]} */
86
+ export function loadInflight() {
87
+ try {
88
+ const parsed = JSON.parse(readFileSync(file(), 'utf8'));
89
+ return Array.isArray(parsed) ? parsed : [];
90
+ } catch {
91
+ return [];
92
+ }
93
+ }
94
+
95
+ /** @param {Inflight[]} entries */
96
+ export function saveInflight(entries) {
97
+ mkdirSync(stateDir(), { recursive: true });
98
+ writeFileSync(file(), JSON.stringify(entries.slice(-100)));
99
+ }
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
+ }