limitcast 0.19.3 → 0.19.4

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/bin/limitcast.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  // @ts-check
3
3
  import { spawn, spawnSync } from 'node:child_process';
4
- import { existsSync, mkdirSync, openSync, readFileSync, writeFileSync } from 'node:fs';
4
+ import { existsSync, mkdirSync, openSync, readFileSync, readSync, writeFileSync } from 'node:fs';
5
5
  import { join } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
7
7
  import {
@@ -24,6 +24,7 @@ import {
24
24
  } from '../src/apply.mjs';
25
25
  import { calibrate, limitChange, limitChangeMessage, windowPct } from '../src/calibrate.mjs';
26
26
  import { applyCodexTrim, codexConfigPath, codexServers, codexStartup, codexTrimReport, codexUsedServers, undoCodexTrim } from '../src/codex.mjs';
27
+ import { codexHooks, trustLine } from '../src/codex-trust.mjs';
27
28
  import { coldCost, coldMessage, coldNotice, firstNotice, firstWarning } from '../src/cold.mjs';
28
29
  import { unknownFlag } from '../src/flags.mjs';
29
30
  import { forecastAll, oneClaudeAccount, resetTarget } from '../src/forecast.mjs';
@@ -65,14 +66,25 @@ import {
65
66
  spawnIdentity,
66
67
  } from '../src/inflight.mjs';
67
68
  import { insightsReport, insightsShare, periodInsights } from '../src/insights.mjs';
68
- import { doctor, install, installedFeatures, readSettings, settingsPath, uninstall } from '../src/install.mjs';
69
+ import { codexHooksPath, doctor, install, installedFeatures, readSettings, settingsPath, uninstall } from '../src/install.mjs';
69
70
  import { memoryCheck, memoryDir, memoryReport, scanMemory } from '../src/memory.mjs';
70
- import { buildNotifierApp, loadChannels, notifierApp, notifyChanges, notifyEnabled, send, setChannel, setNotify } from '../src/notify.mjs';
71
+ import {
72
+ buildNotifierApp,
73
+ codexNotifyDue,
74
+ loadChannels,
75
+ notifierApp,
76
+ notifyChanges,
77
+ notifyEnabled,
78
+ send,
79
+ setChannel,
80
+ setNotify,
81
+ } from '../src/notify.mjs';
71
82
  import { onboardingReport } from '../src/onboarding.mjs';
72
83
  import { runOptimizationHook, runOptimize } from '../src/optimize-cli.mjs';
73
- import { driftMessage, loadOverhead, recordOverhead, savedPerSession, savedSummary, snapshot } from '../src/overhead.mjs';
84
+ import { driftMessage, hostSnapshot, loadOverhead, recordOverhead, savedPerSession, savedSummary, snapshot } from '../src/overhead.mjs';
74
85
  import { advise, getPlan, loadPeaks, PLANS, planPrices, planStats, recordPeaks, setPlan } from '../src/plan.mjs';
75
86
  import { costOf, fmtUsd } from '../src/prices.mjs';
87
+ import { deferReceipt, hostSubagentUsage, settleReceipts, stopHost } from '../src/receipts.mjs';
76
88
  import { burnReport } from '../src/report.mjs';
77
89
  import { loadBlocks, logBlock, resultsReport, resultsShare, resultsStats, weeklyDue, weeklyLine } from '../src/results.mjs';
78
90
  import { claudeSamples, readCodex } from '../src/sources.mjs';
@@ -94,8 +106,8 @@ import {
94
106
  windowStart,
95
107
  } from '../src/subagents.mjs';
96
108
  import { forTerminal } from '../src/term.mjs';
97
- import { firstFlag, toolKey, toolNotice } from '../src/toolcheck.mjs';
98
- import { loadToolStats, saveToolStats, toolLimitDecision, toolStats } from '../src/toollimit.mjs';
109
+ import { firstFlag, resultTokens, toolKey, toolNotice } from '../src/toolcheck.mjs';
110
+ import { loadLiveStats, loadToolStats, recordToolSize, saveToolStats, toolLimitDecision, toolStats } from '../src/toollimit.mjs';
99
111
  import { baseline, contributors, measuredSavings, shareSummary, startupReport, trimReport } from '../src/trim.mjs';
100
112
  import { claudeRoot, grokRoot, readUsage } from '../src/usage.mjs';
101
113
  import { dailyDue, foldEvents, loadValue, saveValue, scanSessions, valuePeriod, valueSection, valueTotals } from '../src/value.mjs';
@@ -179,7 +191,7 @@ const HELP = `limitcast ${VERSION}: stretch your Claude Code, Codex and Grok pla
179
191
  const [cmd, ...args] = process.argv.slice(2);
180
192
  const now = Date.now();
181
193
  // Hooks run unattended and fail open; everything else rejects a mistyped option instead of ignoring it.
182
- const typo = ['guard', 'prompt-check', 'tool-check', 'tool-limit', 'subagent-receipt', 'drift', 'statusline'].includes(cmd)
194
+ const typo = ['guard', 'prompt-check', 'tool-check', 'tool-limit', 'subagent-receipt', 'codex-notify', 'drift', 'statusline'].includes(cmd)
183
195
  ? undefined
184
196
  : unknownFlag(args);
185
197
  if (typo) {
@@ -190,6 +202,19 @@ const flag = (/** @type {string} */ name) => {
190
202
  const i = args.indexOf(name);
191
203
  return i >= 0 ? args[i + 1] : undefined;
192
204
  };
205
+ /** The tool a hook belongs to: `--src codex|grok` on hooks install writes for them, Claude Code otherwise. @returns {'claude'|'codex'|'grok'} */
206
+ const hookHost = () => {
207
+ const s = flag('--src');
208
+ return s === 'codex' || s === 'grok' ? s : 'claude';
209
+ };
210
+ /** Grok writes a finished subagent's usage after its stop hook ran: log the receipts parked at the stop. Never throws. */
211
+ const settleGrokReceipts = () => {
212
+ try {
213
+ settleReceipts(now);
214
+ } catch {
215
+ /* a hook must never break the session */
216
+ }
217
+ };
193
218
  const days = () => {
194
219
  const n = Number(flag('--days') ?? 7);
195
220
  if (!Number.isFinite(n) || n <= 0 || !Number.isFinite(new Date(now - n * 86400_000).getTime())) {
@@ -309,6 +334,7 @@ if (cmd === '_optimize-hook') {
309
334
  if (process.env.LIMITCAST_ALLOW === '1') process.exit(0);
310
335
  try {
311
336
  const hook = JSON.parse(readStdin());
337
+ settleGrokReceipts();
312
338
  const id = spawnIdentity(hook);
313
339
  const live = pruneInflight(loadInflight(), now);
314
340
  const dup = id ? findDuplicate(live, id) : null;
@@ -409,19 +435,24 @@ if (cmd === '_optimize-hook') {
409
435
  if (process.env.LIMITCAST_ALLOW === '1') process.exit(0);
410
436
  try {
411
437
  const hook = JSON.parse(readStdin());
438
+ // `--src codex|grok` (set by install) reads that tool's payload and its live-learned sizes; Claude's come from `report --why`.
439
+ const host = hookHost();
440
+ if (host === 'grok') settleGrokReceipts();
412
441
  const name = String(hook?.tool_name ?? hook?.toolName ?? '');
413
- const d = name ? toolLimitDecision(name, hook?.tool_input ?? hook?.toolInput, loadToolStats()) : null;
442
+ const stats = host === 'claude' ? loadToolStats() : loadLiveStats(host);
443
+ const d = name ? toolLimitDecision(name, hook?.tool_input ?? hook?.toolInput, stats, { host, rewrite: host !== 'codex' }) : null;
444
+ const opId = hook?.tool_use_id ?? hook?.toolUseId;
414
445
  if (d && 'deny' in d) {
415
446
  logBlock({
416
447
  t: now,
417
448
  kind: 'tool-limit',
418
- src: 'claude',
419
- win: '5h',
449
+ src: host,
450
+ win: host === 'codex' ? '7d' : '5h',
420
451
  pct: 0,
421
452
  wall: now,
422
453
  reset: now,
423
- sid: hook.session_id,
424
- ...(typeof hook?.tool_use_id === 'string' ? { operation: hook.tool_use_id } : {}),
454
+ sid: hook.session_id ?? hook.sessionId,
455
+ ...(typeof opId === 'string' ? { operation: opId } : {}),
425
456
  });
426
457
  console.log(
427
458
  JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', permissionDecisionReason: d.deny } }),
@@ -430,13 +461,13 @@ if (cmd === '_optimize-hook') {
430
461
  logBlock({
431
462
  t: now,
432
463
  kind: 'tool-cap',
433
- src: 'claude',
434
- win: '5h',
464
+ src: host,
465
+ win: host === 'codex' ? '7d' : '5h',
435
466
  pct: 0,
436
467
  wall: now,
437
468
  reset: now,
438
- sid: hook.session_id,
439
- ...(typeof hook?.tool_use_id === 'string' ? { operation: hook.tool_use_id } : {}),
469
+ sid: hook.session_id ?? hook.sessionId,
470
+ ...(typeof opId === 'string' ? { operation: opId } : {}),
440
471
  });
441
472
  console.log(
442
473
  JSON.stringify({
@@ -458,9 +489,16 @@ if (cmd === '_optimize-hook') {
458
489
  try {
459
490
  {
460
491
  const hook = JSON.parse(readStdin());
461
- const name = String(hook?.tool_name ?? '');
462
- const notice = name ? toolNotice(name, hook?.tool_response, loadValue().calib, Number(process.env.LIMITCAST_TOOL_TOKENS) || undefined) : null;
463
- if (notice && firstFlag(String(hook.session_id), toolKey(name))) console.log(JSON.stringify({ systemMessage: notice }));
492
+ const host = hookHost();
493
+ if (host === 'grok') settleGrokReceipts();
494
+ const name = String(hook?.tool_name ?? hook?.toolName ?? '');
495
+ const response = hook?.tool_response ?? hook?.tool_result ?? hook?.toolResult;
496
+ // Codex and Grok have no 5-hour calibration, and no transcript report to learn tool sizes from: learn them here.
497
+ if (name && host !== 'claude') recordToolSize(host, toolKey(name, host), resultTokens(response));
498
+ const notice = name
499
+ ? toolNotice(name, response, host === 'claude' ? loadValue().calib : null, Number(process.env.LIMITCAST_TOOL_TOKENS) || undefined, host)
500
+ : null;
501
+ if (notice && firstFlag(String(hook.session_id ?? hook.sessionId), toolKey(name, host))) console.log(JSON.stringify({ systemMessage: notice }));
464
502
  }
465
503
  } catch {
466
504
  /* a hook must never break the session */
@@ -470,8 +508,8 @@ if (cmd === '_optimize-hook') {
470
508
  // Pro shows the receipt to the user (systemMessage, never the model). Internal agents (empty type) are skipped.
471
509
  try {
472
510
  const hook = JSON.parse(readStdin());
473
- // Claude Code and Codex send snake_case; Grok sends `sessionId` and `subagentType`. Every host clears the duplicate hold;
474
- // only Claude transcripts are priced below (a Codex or Grok transcript yields no calls, so nothing is logged for them).
511
+ // Claude Code and Codex send snake_case; Grok sends `sessionId` and `subagentType`. Every host clears the duplicate hold
512
+ // and gets a receipt: Claude's from its transcript, Codex's from the child rollout, Grok's from the child session ledger.
475
513
  saveInflight(
476
514
  forgetInflight(
477
515
  loadInflight(),
@@ -481,7 +519,29 @@ if (cmd === '_optimize-hook') {
481
519
  ),
482
520
  );
483
521
  const path = hook?.agent_transcript_path;
484
- if (hook?.agent_type && typeof path === 'string') {
522
+ const host = stopHost(hook);
523
+ if (host !== 'claude') {
524
+ const type = String(hook?.agent_type ?? hook?.subagentType ?? hook?.agentType ?? '');
525
+ if (host === 'grok') settleGrokReceipts();
526
+ const got = type ? hostSubagentUsage(hook) : null;
527
+ // Grok has not written this subagent's usage yet (it does a moment after the stop): park it for the next hook.
528
+ if (!got?.usage.calls && host === 'grok' && type) deferReceipt(now, type, hook);
529
+ if (got?.usage.calls) {
530
+ const { usage } = got;
531
+ logSubagent({
532
+ t: now,
533
+ src: got.src,
534
+ id: hook.agent_id ?? hook.subagentId,
535
+ sid: hook.session_id ?? hook.sessionId,
536
+ type,
537
+ calls: usage.calls,
538
+ fresh: usage.fresh,
539
+ read: usage.read,
540
+ ...(usage.usd === null ? {} : { usd: usage.usd }),
541
+ });
542
+ console.log(JSON.stringify({ systemMessage: receiptMessage(type, usage, null, usage.usd) }));
543
+ }
544
+ } else if (hook?.agent_type && typeof path === 'string') {
485
545
  const u = subagentUsage(path);
486
546
  const calib = loadValue().calib;
487
547
  const pct = calib ? windowPct(calib, { writes: u.fresh, reads: u.read }) : null;
@@ -502,6 +562,23 @@ if (cmd === '_optimize-hook') {
502
562
  } catch {
503
563
  /* a hook must never break the session */
504
564
  }
565
+ } else if (cmd === 'codex-notify') {
566
+ // Codex has no status line to raise a notification from. A Stop hook (installed with --guard) calls this with --hook; at most
567
+ // every few minutes it starts the check below in the background, so no Codex turn waits on reading the rollouts.
568
+ try {
569
+ if (args.includes('--hook')) {
570
+ readStdin();
571
+ if (notifyEnabled() && codexNotifyDue(now))
572
+ spawn(process.execPath, [fileURLToPath(import.meta.url), 'codex-notify'], { detached: true, stdio: 'ignore' }).unref();
573
+ } else if (notifyEnabled()) {
574
+ notifyChanges(
575
+ forecastAll(oneClaudeAccount(await readCodex(now - KEEP_MS)), now).filter((f) => f.src === 'codex'),
576
+ clock,
577
+ );
578
+ }
579
+ } catch {
580
+ /* a hook must never break the session */
581
+ }
505
582
  } else if (cmd === 'plan') {
506
583
  if (args[0] === 'set') {
507
584
  const id = args[1];
@@ -700,6 +777,21 @@ if (cmd === '_optimize-hook') {
700
777
  }
701
778
  const checks = await healthChecks();
702
779
  saveHealth(checks);
780
+ // The key in Codex's config.toml only says a hook was approved once, not that this version of it was. When limitcast has hooks
781
+ // in Codex, ask Codex itself which ones it trusts (it takes a second, so it belongs here and not in the daily background check).
782
+ try {
783
+ if (readFileSync(codexHooksPath(), 'utf8').includes('limitcast')) {
784
+ const rows = await codexHooks();
785
+ const line = rows ? trustLine(rows) : null;
786
+ if (line) {
787
+ const at = checks.findIndex((c) => c.line.startsWith('Codex subagent guard installed'));
788
+ if (at >= 0) checks.splice(at, 1);
789
+ checks.push(line);
790
+ }
791
+ }
792
+ } catch {
793
+ /* no Codex hooks file, or Codex did not answer: the checks above stand */
794
+ }
703
795
  // Memory is per project: shown here, but kept out of the saved health that the daily background check overwrites.
704
796
  const mem = memoryCheck(scanMemory(memoryDir()));
705
797
  if (mem) checks.push(mem);
@@ -895,13 +987,22 @@ if (cmd === '_optimize-hook') {
895
987
  } else if (cmd === 'drift') {
896
988
  // SessionStart hook: tells the user (systemMessage, never the model's context) when starting context grew.
897
989
  const hook = args.includes('--hook');
990
+ const host = hookHost();
991
+ if (host === 'grok') settleGrokReceipts();
898
992
  try {
899
- const latest = await latestInteractiveTranscript(claudeRoot());
900
- const d = latest ? recordOverhead(snapshot(latest)) : null;
993
+ // Claude Code: the last interactive transcript. Codex and Grok (`--src`): the sizes their last interactive session logged.
994
+ const claudeLatest = host === 'claude' ? await latestInteractiveTranscript(claudeRoot()) : null;
995
+ const start = host === 'codex' ? await codexStartup() : host === 'grok' ? grokStartup() : null;
996
+ const latest = claudeLatest ?? start;
997
+ const d = claudeLatest
998
+ ? recordOverhead(snapshot(claudeLatest))
999
+ : start && host !== 'claude'
1000
+ ? recordOverhead(hostSnapshot(host, start), host)
1001
+ : null;
901
1002
  if (hook) {
902
- const lines = d?.alert ? [driftMessage(d)] : [];
1003
+ const lines = d?.alert ? [driftMessage(d, host)] : [];
903
1004
  // Claude Code may have rewritten the files `trim --apply` changed: put the cuts back for the next session.
904
- const re = reapplyTrim();
1005
+ const re = host === 'claude' ? reapplyTrim() : { reapplied: [] };
905
1006
  if (re.reapplied.length)
906
1007
  lines.push(
907
1008
  `limitcast: Claude Code had undone ${re.reapplied.length} of your trim cuts; they're back from your next session. (limitcast trim --undo to drop them)`,
@@ -918,9 +1019,9 @@ if (cmd === '_optimize-hook') {
918
1019
  );
919
1020
  }
920
1021
  if (lines.length) console.log(JSON.stringify({ systemMessage: lines.join('\n') }));
921
- } else if (!latest) console.log('No interactive Claude Code session found yet.');
1022
+ } else if (!latest) console.log(`No interactive ${host === 'claude' ? 'Claude Code' : host === 'codex' ? 'Codex' : 'Grok'} session found yet.`);
922
1023
  else if (!d) console.log('Starting context recorded. limitcast will tell you when it grows.');
923
- else console.log(d.alert ? forTerminal(driftMessage(d)) : 'No meaningful change in your starting context since the last session.');
1024
+ else console.log(d.alert ? forTerminal(driftMessage(d, host)) : 'No meaningful change in your starting context since the last session.');
924
1025
  } catch (e) {
925
1026
  if (!hook) throw e; // a hook must never break session start
926
1027
  }
@@ -1264,12 +1365,30 @@ function saveFeedShape(feed, file = 'last-feed.json') {
1264
1365
  writeFileSync(join(stateDir(), file), JSON.stringify(shape, null, 2));
1265
1366
  }
1266
1367
 
1368
+ /**
1369
+ * The whole of stdin. A host that pipes a large payload (a tool result can be 100 KB or more) may hand us a non-blocking pipe,
1370
+ * where readFileSync(0) throws EAGAIN once the pipe runs dry and the rest of the payload is lost. Wait for the writer instead,
1371
+ * for at most ten seconds.
1372
+ */
1267
1373
  function readStdin() {
1268
- try {
1269
- return readFileSync(0, 'utf8');
1270
- } catch {
1271
- return '';
1374
+ const chunks = [];
1375
+ const buf = Buffer.alloc(65536);
1376
+ const until = Date.now() + 10_000;
1377
+ for (;;) {
1378
+ let n = 0;
1379
+ try {
1380
+ n = readSync(0, buf, 0, buf.length, null);
1381
+ } catch (e) {
1382
+ if (/** @type {NodeJS.ErrnoException} */ (e).code === 'EAGAIN' && Date.now() < until) {
1383
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 5);
1384
+ continue;
1385
+ }
1386
+ break; // EOF on some platforms, or a closed or unreadable stdin: what arrived so far is what we have
1387
+ }
1388
+ if (n === 0) break;
1389
+ chunks.push(Buffer.from(buf.subarray(0, n)));
1272
1390
  }
1391
+ return Buffer.concat(chunks).toString('utf8');
1273
1392
  }
1274
1393
 
1275
1394
  /** @param {number|null} ms */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "limitcast",
3
- "version": "0.19.3",
3
+ "version": "0.19.4",
4
4
  "description": "Claude Code usage limits: forecast Codex and Grok too.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,5 +1,6 @@
1
1
  import { execFileSync } from 'node:child_process';
2
- import { existsSync, lstatSync, mkdirSync, readFileSync, statSync, unlinkSync } from 'node:fs';
2
+ import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, statSync, unlinkSync } from 'node:fs';
3
+ import { homedir } from 'node:os';
3
4
  import { dirname, join, relative, sep } from 'node:path';
4
5
  import { codexConfigPath, codexServers } from './codex.mjs';
5
6
  import { grokServers } from './grok.mjs';
@@ -22,16 +23,23 @@ export function configuredServers(host) {
22
23
  }
23
24
 
24
25
  /**
25
- * Add `[mcp_servers.<name>] enabled = false` tables to a project config.toml. The user-level definition (command, env)
26
- * stays where it is: the project table only overrides `enabled`. Refuses a server the file already configures.
26
+ * Add `[mcp_servers.<name>] enabled = false` tables to a project config.toml. Refuses a server the file already configures.
27
+ * Codex merges the table into the user-level definition, which stays where it is (command, env). Grok replaces the server
28
+ * with the project's entry whole, and ignores one that has no command; `stub` writes a command that never runs, because the
29
+ * server is disabled, so nothing of the user's definition (arguments, tokens, headers) is copied into the repository.
30
+ * @param {string} text
31
+ * @param {string[]} names
32
+ * @param {boolean} [stub]
27
33
  */
28
- export function patchMcpServers(text, names) {
34
+ export function patchMcpServers(text, names, stub = false) {
29
35
  const existing = codexServers(text);
30
36
  for (const name of names) {
31
37
  if (!/^[A-Za-z0-9_-]{1,64}$/.test(name)) throw new Error(`unsupported MCP server name ${name}`);
32
38
  if (existing.some((s) => s.name === name)) throw new Error(`preserve explicit ${name} server setting; change it yourself`);
33
39
  }
34
- const tables = names.map((name) => `[mcp_servers.${name}]\nenabled = false # limitcast optimizer`).join('\n\n');
40
+ const tables = names
41
+ .map((name) => `[mcp_servers.${name}]\n${stub ? 'command = "true" # never runs: the server is off\n' : ''}enabled = false # limitcast optimizer`)
42
+ .join('\n\n');
35
43
  const body = text.trimEnd();
36
44
  return `${body ? `${body}\n\n` : ''}${tables}\n`;
37
45
  }
@@ -100,6 +108,11 @@ export function applyCapabilities(repo, host, names, { validate = host === 'grok
100
108
  throw new Error('choose proposed Claude tools, or Codex or Grok capabilities and MCP servers');
101
109
  const file = configPath(repo, host);
102
110
  safePath(repo, file);
111
+ if (host !== 'claude' && [homedir(), realpathSync(homedir())].includes(repo.root))
112
+ // In the home directory the "project" file is the user's own config, which already holds every server it could turn off.
113
+ throw new Error(
114
+ `this repository is your home directory, so ${CONFIG[host]} is your own ${host === 'codex' ? 'Codex' : 'Grok'} config; use \`limitcast trim --${host} --apply\` to turn servers off everywhere`,
115
+ );
103
116
  if (host === 'codex') validate(repo);
104
117
  const servers = configuredServers(host);
105
118
  const result = changeOptimization(repo, (state) => {
@@ -115,7 +128,7 @@ export function applyCapabilities(repo, host, names, { validate = host === 'grok
115
128
  after = before ?? '';
116
129
  if (features.length) after = patchCodexCapabilities(after, features);
117
130
  if (mcp.length) after = patchMcpServers(after, mcp);
118
- } else if (host === 'grok') after = patchMcpServers(before ?? '', names);
131
+ } else if (host === 'grok') after = patchMcpServers(before ?? '', names, true);
119
132
  else {
120
133
  const cfg = JSON.parse(before ?? '{}');
121
134
  if (!cfg || typeof cfg !== 'object' || Array.isArray(cfg)) throw new Error('settings must be an object');
@@ -0,0 +1,85 @@
1
+ // @ts-check
2
+ import { spawn } from 'node:child_process';
3
+ import { homedir } from 'node:os';
4
+ import { createInterface } from 'node:readline';
5
+
6
+ // Codex trusts a hook by a hash of its definition (`trusted_hash` in config.toml), which cannot be recomputed from outside:
7
+ // a hook that was edited keeps its key and still fails the hash. So ask Codex itself, the way its own /hooks screen does:
8
+ // the app server's `hooks/list` says each hook's trust status.
9
+
10
+ /** @typedef {{key:string, trustStatus:string, enabled:boolean, command:string}} HookRow */
11
+
12
+ /**
13
+ * Every hook Codex has configured for `cwd`, with its trust status. Null when Codex does not answer.
14
+ * @param {{cwd?:string, timeoutMs?:number, command?:string[]}} [o] command: the app-server command line, `codex app-server` by default
15
+ * @returns {Promise<HookRow[]|null>}
16
+ */
17
+ export function codexHooks({ cwd = homedir(), timeoutMs = 15_000, command = ['codex', 'app-server'] } = {}) {
18
+ return new Promise((resolve) => {
19
+ let child;
20
+ try {
21
+ child = spawn(command[0], command.slice(1), { cwd, stdio: ['pipe', 'pipe', 'ignore'] });
22
+ } catch {
23
+ resolve(null);
24
+ return;
25
+ }
26
+ let done = false;
27
+ /** @param {HookRow[]|null} rows */
28
+ const finish = (rows) => {
29
+ if (done) return;
30
+ done = true;
31
+ clearTimeout(timer);
32
+ child.kill();
33
+ resolve(rows);
34
+ };
35
+ const timer = setTimeout(() => finish(null), timeoutMs);
36
+ child.on('error', () => finish(null));
37
+ child.on('exit', () => finish(null));
38
+ /** @param {unknown} msg */
39
+ const send = (msg) => child.stdin.write(`${JSON.stringify(msg)}\n`);
40
+ createInterface({ input: child.stdout }).on('line', (line) => {
41
+ let msg;
42
+ try {
43
+ msg = JSON.parse(line);
44
+ } catch {
45
+ return;
46
+ }
47
+ if (msg?.id === 1) {
48
+ send({ method: 'initialized' });
49
+ send({ id: 2, method: 'hooks/list', params: { cwds: [cwd] } });
50
+ } else if (msg?.id === 2) {
51
+ if (msg.error) return finish(null);
52
+ const rows = (msg.result?.data ?? []).flatMap((/** @type {any} */ entry) => entry?.hooks ?? []);
53
+ finish(
54
+ rows.map((/** @type {any} */ h) => ({
55
+ key: String(h.key ?? ''),
56
+ trustStatus: String(h.trustStatus ?? ''),
57
+ enabled: h.enabled !== false,
58
+ command: String(h.command ?? ''),
59
+ })),
60
+ );
61
+ }
62
+ });
63
+ send({ id: 1, method: 'initialize', params: { clientInfo: { name: 'limitcast-doctor', title: 'limitcast doctor', version: '1' } } });
64
+ });
65
+ }
66
+
67
+ /**
68
+ * Doctor line for limitcast's own hooks, from what Codex says about them. Null when none of them is configured.
69
+ * @param {HookRow[]} rows
70
+ * @returns {{ok:boolean, line:string}|null}
71
+ */
72
+ export function trustLine(rows) {
73
+ const mine = rows.filter((h) => /limitcast/i.test(h.command));
74
+ if (!mine.length) return null;
75
+ const waiting = mine.filter((h) => h.trustStatus !== 'trusted');
76
+ if (!waiting.length) return { ok: true, line: `Codex hooks: all ${mine.length} limitcast hooks are trusted` };
77
+ const what = waiting
78
+ .map((h) => h.key.replace(/^.*[\\/]/, '').replace(/^hooks\.json:/, ''))
79
+ .slice(0, 4)
80
+ .join(', ');
81
+ return {
82
+ ok: false,
83
+ line: `Codex hooks: ${waiting.length} of ${mine.length} limitcast hooks are not trusted (${what}): start Codex, open /hooks and approve them. Codex skips an unreviewed hook`,
84
+ };
85
+ }
package/src/codex.mjs CHANGED
@@ -91,34 +91,64 @@ export async function codexUsedServers(since, root = join(codexHome(), 'sessions
91
91
  }
92
92
 
93
93
  /**
94
- * Sizes from the newest rollout's head: the AGENTS.md chain Codex loaded and its own base instructions (characters).
95
- * @returns {Promise<{agentsMd:number, base:number}|null>}
94
+ * What a rollout's head shows of how the session started. A subagent's rollout is its own thread (forked, with its own
95
+ * instructions), so it says nothing about what your interactive sessions load.
96
+ * @param {string} file
97
+ * @returns {Promise<{subagent:boolean, agentsMd:number, base:number, first?:number, at?:number}>}
96
98
  */
97
- export async function codexStartup(root = join(codexHome(), 'sessions')) {
98
- const files = jsonlFiles(root, 0, (n) => n.startsWith('rollout-'))
99
- .map((p) => ({ p, m: statSync(p).mtimeMs }))
100
- .sort((a, b) => b.m - a.m);
101
- if (!files.length) return null;
99
+ async function rolloutHead(file) {
102
100
  let agentsMd = 0;
103
101
  let base = 0;
102
+ let subagent = false;
103
+ /** @type {number|undefined} */
104
+ let first;
105
+ /** @type {number|undefined} */
106
+ let at;
104
107
  let n = 0;
105
- const rl = createInterface({ input: createReadStream(files[0].p), crlfDelay: Infinity });
108
+ const rl = createInterface({ input: createReadStream(file), crlfDelay: Infinity });
106
109
  for await (const line of rl) {
107
- if (++n > 40) break;
110
+ if (++n > 400) break;
108
111
  try {
109
112
  const ev = JSON.parse(line);
110
- if (ev.type === 'session_meta') base = String(ev.payload?.base_instructions?.text ?? '').length;
113
+ if (ev.type === 'session_meta') {
114
+ base = String(ev.payload?.base_instructions?.text ?? '').length;
115
+ subagent = ev.payload?.thread_source === 'subagent' || Boolean(ev.payload?.source?.subagent);
116
+ const started = Date.parse(ev.payload?.timestamp ?? ev.timestamp);
117
+ if (Number.isFinite(started)) at = started;
118
+ }
111
119
  if (ev.type === 'world_state') agentsMd = String(ev.payload?.state?.agents_md?.text ?? '').length;
120
+ const total = ev?.payload?.info?.total_token_usage;
121
+ if (total?.input_tokens) {
122
+ first = total.input_tokens; // the first model call's whole prompt: what a session starts with, measured
123
+ break;
124
+ }
112
125
  } catch {
113
126
  /* skip */
114
127
  }
115
128
  }
116
129
  rl.close();
117
- return { agentsMd, base };
130
+ return { subagent, agentsMd, base, ...(first ? { first } : {}), ...(at ? { at } : {}) };
131
+ }
132
+
133
+ /**
134
+ * Sizes from the newest interactive rollout's head: the AGENTS.md chain Codex loaded and its own base instructions
135
+ * (characters), `first`, the tokens of its first model call when the log reaches one, and `at`, when it started.
136
+ * @returns {Promise<{agentsMd:number, base:number, first?:number, at?:number}|null>}
137
+ */
138
+ export async function codexStartup(root = join(codexHome(), 'sessions')) {
139
+ const files = jsonlFiles(root, 0, (n) => n.startsWith('rollout-'))
140
+ .map((p) => ({ p, m: statSync(p).mtimeMs }))
141
+ .sort((a, b) => b.m - a.m)
142
+ .slice(0, 40);
143
+ for (const f of files) {
144
+ const { subagent, ...sizes } = await rolloutHead(f.p);
145
+ if (!subagent) return sizes;
146
+ }
147
+ return null;
118
148
  }
119
149
 
120
150
  /**
121
- * @param {{servers:{name:string, enabled:boolean|undefined}[], used:Set<string>, usedMonth?:Set<string>|null, startup:{agentsMd:number, base:number}|null, days:number, pro:boolean, keep?:string[]}} r
151
+ * @param {{servers:{name:string, enabled:boolean|undefined}[], used:Set<string>, usedMonth?:Set<string>|null, startup:{agentsMd:number, base:number, first?:number}|null, days:number, pro:boolean, keep?:string[]}} r
122
152
  * keep: the `trim keep` list; unused servers on it are reported as kept and left out of `unused`.
123
153
  * usedMonth: normalized names used in 30 days. Servers in it stay out of `unused`.
124
154
  */
@@ -132,7 +162,9 @@ export function codexTrimReport({ servers, used, usedMonth = null, startup, days
132
162
  const out = ['# limitcast trim --codex', ''];
133
163
  if (startup)
134
164
  out.push(
135
- `Codex sessions start with ~${tok(startup.agentsMd)} tokens of AGENTS.md (your instruction files) and ~${tok(startup.base)} of Codex's own instructions, re-sent on every turn.`,
165
+ startup.first
166
+ ? `Your latest Codex session started with ${(startup.first / 1000).toFixed(1)}k tokens on its first call (measured): ~${tok(startup.agentsMd)} of AGENTS.md (your instruction files), ~${tok(startup.base)} of Codex's own instructions, and the rest tool definitions and environment context, all re-sent on every turn.`
167
+ : `Codex sessions start with ~${tok(startup.agentsMd)} tokens of AGENTS.md (your instruction files) and ~${tok(startup.base)} of Codex's own instructions, re-sent on every turn.`,
136
168
  '',
137
169
  );
138
170
  out.push(`## MCP servers: ${on.length} on, ${unused.length + standing.length} not used in ${days} days`);