claude-usage-limits 1.11.7 → 1.13.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/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/README.md +133 -3
- package/bin/cli.js +1 -0
- package/commands/relay.md +45 -0
- package/commands/voice.md +33 -0
- package/hooks/hooks.json +24 -1
- package/package.json +1 -1
- package/skills/usage-limits/SKILL.md +130 -5
- package/skills/usage-limits/scripts/bars.js +56 -0
- package/skills/usage-limits/scripts/brief.js +257 -17
- package/skills/usage-limits/scripts/codex-lowpower.js +135 -0
- package/skills/usage-limits/scripts/codex.js +92 -22
- package/skills/usage-limits/scripts/feed.js +136 -5
- package/skills/usage-limits/scripts/install-codex-hook.js +71 -20
- package/skills/usage-limits/scripts/lowpower.js +10 -2
- package/skills/usage-limits/scripts/panel.js +180 -15
- package/skills/usage-limits/scripts/pulse.js +82 -22
- package/skills/usage-limits/scripts/reading.js +121 -0
- package/skills/usage-limits/scripts/recommend.js +16 -3
- package/skills/usage-limits/scripts/relay.js +859 -0
- package/skills/usage-limits/scripts/tally.js +7 -8
- package/skills/usage-limits/scripts/usage.js +842 -53
- package/skills/usage-limits/scripts/view.js +150 -8
- package/skills/usage-limits/scripts/voice.js +416 -0
- package/skills/usage-limits/scripts/wake.js +312 -0
|
@@ -22,6 +22,7 @@ const fs = require('fs');
|
|
|
22
22
|
const path = require('path');
|
|
23
23
|
|
|
24
24
|
const usage = require('./usage.js');
|
|
25
|
+
const reading = require('./reading.js');
|
|
25
26
|
const brief = require('./brief.js');
|
|
26
27
|
const host = require('./host.js');
|
|
27
28
|
const activity = require('./activity.js');
|
|
@@ -30,8 +31,15 @@ const live = require('./live.js');
|
|
|
30
31
|
const SECOND = 1000;
|
|
31
32
|
const DEFAULT_INTERVAL_SECONDS = 120;
|
|
32
33
|
|
|
34
|
+
// Same reasoning as the brief: ten seconds for the hook, four for a live
|
|
35
|
+
// reading, five for the scan, one of slack. This hook interrupts work in
|
|
36
|
+
// progress, so being late is worse here than anywhere else.
|
|
37
|
+
const SCAN_BUDGET_MS = 5000;
|
|
38
|
+
|
|
33
39
|
// One slot per session, same shape and same trimming as the brief's cache.
|
|
34
|
-
|
|
40
|
+
// Two keys per session now - the spoken pulse and the quiet subagent refresh -
|
|
41
|
+
// so this is double what it was.
|
|
42
|
+
const KEEP_SESSIONS = 16;
|
|
35
43
|
|
|
36
44
|
function stateFile() {
|
|
37
45
|
const dir = usage.isCodex()
|
|
@@ -58,14 +66,10 @@ function readState() {
|
|
|
58
66
|
}
|
|
59
67
|
|
|
60
68
|
function writeState(all) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
} catch (err) {
|
|
66
|
-
// Losing the throttle means one extra scan, which is survivable. Failing
|
|
67
|
-
// the tool call it runs after is not.
|
|
68
|
-
}
|
|
69
|
+
// Never throws. Losing the throttle means one extra scan, which is
|
|
70
|
+
// survivable; failing the tool call it runs after is not. Atomic because
|
|
71
|
+
// several sessions' tool calls land on this file within the same second.
|
|
72
|
+
usage.writeJsonAtomic(stateFile(), all);
|
|
69
73
|
}
|
|
70
74
|
|
|
71
75
|
function trim(all, sessionId, at) {
|
|
@@ -97,7 +101,18 @@ function pulseText(parts) {
|
|
|
97
101
|
if (parts.sessions > 1) bits.push(parts.sessions + ' sessions sharing it');
|
|
98
102
|
if (!bits.length) return '';
|
|
99
103
|
|
|
100
|
-
const head = '[usage-limits] ' + bits.join(', ') + '.';
|
|
104
|
+
const head = '[usage-limits] ' + (parts.fanout ? 'Before this fan-out: ' : '') + bits.join(', ') + '.';
|
|
105
|
+
if (parts.fanout) {
|
|
106
|
+
// Said before every Workflow or Agent call. The agents spend this same
|
|
107
|
+
// window, nothing can speak again until they stop, and a main-loop turn
|
|
108
|
+
// with a large context costs more than one whole fresh-context agent.
|
|
109
|
+
return (
|
|
110
|
+
head + ' Subagents spend this window too and nothing can warn you until they ' +
|
|
111
|
+
'stop, so size the fan-out to what is left' +
|
|
112
|
+
(parts.pressure === 'gone' ? ' - which is nothing: do not launch it' : parts.pressure === 'tight' ? ' - a handful, not dozens' : '') +
|
|
113
|
+
'. Fewer agents with a fresh context beat another turn of a long one.'
|
|
114
|
+
);
|
|
115
|
+
}
|
|
101
116
|
if (parts.pressure === 'gone') {
|
|
102
117
|
return head + ' The budget is gone. Stop adding work, save what exists and write the handoff.';
|
|
103
118
|
}
|
|
@@ -118,18 +133,49 @@ async function run(now, hookInput) {
|
|
|
118
133
|
usage.setHost(host.detect(process.argv.slice(2), process.env));
|
|
119
134
|
|
|
120
135
|
const sessionId = hookInput && hookInput.session_id ? hookInput.session_id : null;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
136
|
+
const event = hookInput && hookInput.hook_event_name ? String(hookInput.hook_event_name) : 'PostToolUse';
|
|
137
|
+
|
|
138
|
+
// A subagent finishing is the other reason to look, and on a busy afternoon
|
|
139
|
+
// it is the more important one.
|
|
140
|
+
//
|
|
141
|
+
// This hook exists because PostToolUse fires on the MAIN thread's tool calls,
|
|
142
|
+
// and a turn that hands its work to a workflow makes none for half an hour.
|
|
143
|
+
// On 2026-09-06 two workflows spent a whole five-hour window between one
|
|
144
|
+
// prompt and the next, and nothing ran in between to notice: the reading on
|
|
145
|
+
// disk aged eleven minutes while forty-three agents spent against it, and the
|
|
146
|
+
// session was cut off at a figure the plugin still believed was 3%.
|
|
147
|
+
//
|
|
148
|
+
// So it refreshes and says nothing. The mark is deliberately not written -
|
|
149
|
+
// a subagent is not a session, and marking one would put it in the list of
|
|
150
|
+
// windows sharing this budget and split the headroom with a ghost.
|
|
151
|
+
const quiet = event === 'SubagentStop';
|
|
152
|
+
// The call about to fan out. Everything it spawns spends this window with no
|
|
153
|
+
// main-thread tool call to pulse on, so this is the last word before the
|
|
154
|
+
// bill. On 2026-09-07 a 34-agent workflow took a five-hour window from 28%
|
|
155
|
+
// to 100% in sixteen minutes, and the session was cut off at a figure the
|
|
156
|
+
// pulse had last read as 24%.
|
|
157
|
+
const tool = hookInput && hookInput.tool_name ? String(hookInput.tool_name) : '';
|
|
158
|
+
const fanout = event === 'PreToolUse' && /^(Workflow|Agent|Task)$/.test(tool);
|
|
159
|
+
if (!quiet) {
|
|
160
|
+
// A tool call just finished, so the turn is still running. A few bytes, so
|
|
161
|
+
// the panel beside the chat keeps animating through a long turn.
|
|
162
|
+
activity.mark('working', sessionId, null, now);
|
|
163
|
+
}
|
|
124
164
|
|
|
125
165
|
const all = readState();
|
|
126
166
|
const every = intervalMs();
|
|
127
|
-
// The
|
|
128
|
-
|
|
167
|
+
// The quiet refresh keeps its own throttle. Sharing one with the spoken
|
|
168
|
+
// pulse would mean a workflow's subagents used up the interval and the tool
|
|
169
|
+
// call right after it, the first chance to actually tell Claude, said
|
|
170
|
+
// nothing because something had already "pulsed" two minutes ago.
|
|
171
|
+
const throttleKey = quiet ? (sessionId || '_') + '#subagent' : sessionId;
|
|
172
|
+
// The cheap path, and the one taken almost every time. A fan-out is never
|
|
173
|
+
// throttled: it is said every time, because every time it is about to cost.
|
|
174
|
+
if (!fanout && !due(all, throttleKey, now, every)) return '';
|
|
129
175
|
|
|
130
176
|
// Claimed before the scan rather than after, so a slow scan cannot let a
|
|
131
177
|
// second tool call start another one.
|
|
132
|
-
writeState(trim(all,
|
|
178
|
+
writeState(trim(all, throttleKey, now));
|
|
133
179
|
|
|
134
180
|
// A reading as old as the interval is replaced with the one Claude Code
|
|
135
181
|
// would take for /usage, so a turn that runs for an hour is measured
|
|
@@ -151,9 +197,16 @@ async function run(now, hookInput) {
|
|
|
151
197
|
// The reading on disk is still there.
|
|
152
198
|
}
|
|
153
199
|
|
|
154
|
-
|
|
200
|
+
// Refreshing was the whole errand. The next prompt, or the next tool call on
|
|
201
|
+
// the main thread, reports the number this just brought up to date.
|
|
202
|
+
if (quiet) return '';
|
|
203
|
+
|
|
204
|
+
const data = await usage.report(now, { sessionId, budgetMs: SCAN_BUDGET_MS });
|
|
155
205
|
const binding = data.binding;
|
|
156
206
|
if (!binding) return '';
|
|
207
|
+
// This turn paid for the scan, so leave the corrected figure where the
|
|
208
|
+
// status line and --status can read it without paying for one.
|
|
209
|
+
reading.recordAll(data.windows || [binding], now, usage.isCodex() ? require('./codex.js').homeDir() : null);
|
|
157
210
|
|
|
158
211
|
// The same count and the same split as the brief, so the two lines never
|
|
159
212
|
// disagree about how many sessions there are or how much of the budget is
|
|
@@ -171,7 +224,7 @@ async function run(now, hookInput) {
|
|
|
171
224
|
|
|
172
225
|
// Quiet when there is nothing to act on. A line every two minutes saying the
|
|
173
226
|
// budget is fine is noise that costs the budget it is reporting on.
|
|
174
|
-
if (pressure === 'roomy' && String(process.env.USAGE_LIMITS_PULSE || '').toLowerCase() !== 'always') {
|
|
227
|
+
if (!fanout && pressure === 'roomy' && String(process.env.USAGE_LIMITS_PULSE || '').toLowerCase() !== 'always') {
|
|
175
228
|
return '';
|
|
176
229
|
}
|
|
177
230
|
|
|
@@ -186,15 +239,18 @@ async function run(now, hookInput) {
|
|
|
186
239
|
: null,
|
|
187
240
|
sessions: active,
|
|
188
241
|
pressure,
|
|
242
|
+
fanout,
|
|
189
243
|
});
|
|
190
244
|
}
|
|
191
245
|
|
|
192
246
|
// PostToolUse does not take plain stdout as context the way UserPromptSubmit
|
|
193
247
|
// does, so the line is returned in the documented envelope instead.
|
|
194
|
-
function envelope(text) {
|
|
248
|
+
function envelope(text, event) {
|
|
249
|
+
// PreToolUse and PostToolUse both take additionalContext, each under its own
|
|
250
|
+
// event name; the wrong name is dropped without a word.
|
|
195
251
|
return JSON.stringify({
|
|
196
252
|
hookSpecificOutput: {
|
|
197
|
-
hookEventName: 'PostToolUse',
|
|
253
|
+
hookEventName: event === 'PreToolUse' ? 'PreToolUse' : 'PostToolUse',
|
|
198
254
|
additionalContext: text,
|
|
199
255
|
},
|
|
200
256
|
});
|
|
@@ -226,11 +282,15 @@ function readHookInput() {
|
|
|
226
282
|
}
|
|
227
283
|
|
|
228
284
|
if (require.main === module) {
|
|
285
|
+
let hookEvent = null;
|
|
229
286
|
readHookInput()
|
|
230
|
-
.then((input) =>
|
|
287
|
+
.then((input) => {
|
|
288
|
+
hookEvent = input && input.hook_event_name ? String(input.hook_event_name) : null;
|
|
289
|
+
return run(Date.now(), input);
|
|
290
|
+
})
|
|
231
291
|
.then(
|
|
232
292
|
(text) => {
|
|
233
|
-
if (text) process.stdout.write(envelope(text) + '\n');
|
|
293
|
+
if (text) process.stdout.write(envelope(text, hookEvent) + '\n');
|
|
234
294
|
process.exit(0);
|
|
235
295
|
},
|
|
236
296
|
() => {
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The freshest corrected percentage, left where the cheap readers can find it.
|
|
4
|
+
//
|
|
5
|
+
// There are two ways to know how much of a window is gone. The snapshot is what
|
|
6
|
+
// the account last told us, and it is free to read. The correction is the spend
|
|
7
|
+
// measured out of the transcripts since that snapshot was taken, and it costs a
|
|
8
|
+
// scan of up to several seconds - far too much for something that redraws under
|
|
9
|
+
// the prompt on every keystroke.
|
|
10
|
+
//
|
|
11
|
+
// So the fast readers used the snapshot alone, and during a heavy session that
|
|
12
|
+
// is not a small error. Measured on a real session: the snapshot said 13 per
|
|
13
|
+
// cent while the same codebase, given a scan, said 73. The status line is the
|
|
14
|
+
// number a person actually looks at, and it was sixty points wrong, in the
|
|
15
|
+
// flattering direction, for twenty minutes.
|
|
16
|
+
//
|
|
17
|
+
// The fix is not to make the cheap path expensive. It is to notice that
|
|
18
|
+
// something already paid for the scan - the prompt hook before every turn, the
|
|
19
|
+
// pulse hook during long ones - and to have it leave the answer here. One small
|
|
20
|
+
// file, one object per window key, and the readers prefer it whenever it is
|
|
21
|
+
// fresher than the snapshot it would otherwise trust.
|
|
22
|
+
|
|
23
|
+
const fs = require('fs');
|
|
24
|
+
const os = require('os');
|
|
25
|
+
const path = require('path');
|
|
26
|
+
|
|
27
|
+
// Older than this and the spend it measured is history: turns have happened
|
|
28
|
+
// since, and a stale correction that says 40 per cent is worse than an honest
|
|
29
|
+
// snapshot that says 13, because it looks authoritative.
|
|
30
|
+
const FRESH_MS = 8 * 60 * 1000;
|
|
31
|
+
|
|
32
|
+
function configDir() {
|
|
33
|
+
return process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function readingFile(codexHome) {
|
|
37
|
+
return path.join(codexHome || configDir(), 'usage-limits-reading.json');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function read(codexHome) {
|
|
41
|
+
try {
|
|
42
|
+
const parsed = JSON.parse(fs.readFileSync(readingFile(codexHome), 'utf8'));
|
|
43
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
|
|
44
|
+
return parsed;
|
|
45
|
+
} catch (err) {
|
|
46
|
+
return {};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Every window that has a correction, not just the binding one.
|
|
51
|
+
//
|
|
52
|
+
// The status line prints all three, and only one of them can be binding. So
|
|
53
|
+
// recording the binding window alone left the other two showing their raw
|
|
54
|
+
// snapshots - which is the same bug, on two thirds of the line.
|
|
55
|
+
function recordAll(windows, now, codexHome) {
|
|
56
|
+
let written = 0;
|
|
57
|
+
for (const window of Array.isArray(windows) ? windows : [windows]) {
|
|
58
|
+
if (record(window, now, codexHome)) written += 1;
|
|
59
|
+
}
|
|
60
|
+
return written;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Never throws. This is written from inside hooks, and a hook that fails over a
|
|
64
|
+
// cache file would be worse than the stale number it was trying to fix.
|
|
65
|
+
function record(binding, now, codexHome) {
|
|
66
|
+
if (!binding || !binding.key) return false;
|
|
67
|
+
if (binding.percentUsed === null || binding.percentUsed === undefined) return false;
|
|
68
|
+
// A rebuilt or unreliable figure is not an improvement on the snapshot; it is
|
|
69
|
+
// a different kind of guess. Only a correction the report itself trusts is
|
|
70
|
+
// worth putting in front of the cheap readers.
|
|
71
|
+
if (binding.stale || binding.estimated || binding.correctionUnreliable) return false;
|
|
72
|
+
try {
|
|
73
|
+
const all = read(codexHome);
|
|
74
|
+
all[binding.key] = {
|
|
75
|
+
at: Number.isFinite(now) ? now : Date.now(),
|
|
76
|
+
percentUsed: binding.percentUsed,
|
|
77
|
+
pointsSinceSnapshot: binding.pointsSinceSnapshot || 0,
|
|
78
|
+
adjusted: Boolean(binding.adjusted),
|
|
79
|
+
resetsAt: Number.isFinite(binding.resetsAt) ? binding.resetsAt : null,
|
|
80
|
+
turnsLeft: Number.isFinite(binding.turnsLeft) ? binding.turnsLeft : null,
|
|
81
|
+
};
|
|
82
|
+
// One entry per window key, and there are only ever a handful of those, so
|
|
83
|
+
// this file cannot grow. Anything whose reset has passed describes a window
|
|
84
|
+
// that no longer exists.
|
|
85
|
+
const at = Number.isFinite(now) ? now : Date.now();
|
|
86
|
+
for (const key of Object.keys(all)) {
|
|
87
|
+
const entry = all[key];
|
|
88
|
+
if (!entry || !Number.isFinite(entry.at) || at - entry.at > 24 * 60 * 60 * 1000) delete all[key];
|
|
89
|
+
else if (Number.isFinite(entry.resetsAt) && entry.resetsAt <= at) delete all[key];
|
|
90
|
+
}
|
|
91
|
+
fs.mkdirSync(path.dirname(readingFile(codexHome)), { recursive: true });
|
|
92
|
+
fs.writeFileSync(readingFile(codexHome), JSON.stringify(all));
|
|
93
|
+
return true;
|
|
94
|
+
} catch (err) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// The corrected percentage for one window, or null when there is nothing better
|
|
100
|
+
// than the snapshot. `snapshotAt` is when the snapshot being compared against
|
|
101
|
+
// was fetched: a correction measured before it is already included in it, and
|
|
102
|
+
// applying it again would double-count the same spend.
|
|
103
|
+
function correctedFor(key, now, snapshotAt, codexHome) {
|
|
104
|
+
const entry = read(codexHome)[key];
|
|
105
|
+
if (!entry || !Number.isFinite(entry.at)) return null;
|
|
106
|
+
const at = Number.isFinite(now) ? now : Date.now();
|
|
107
|
+
if (at - entry.at > FRESH_MS) return null;
|
|
108
|
+
if (Number.isFinite(snapshotAt) && entry.at < snapshotAt) return null;
|
|
109
|
+
if (Number.isFinite(entry.resetsAt) && entry.resetsAt <= at) return null;
|
|
110
|
+
return entry;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function clear(codexHome) {
|
|
114
|
+
try {
|
|
115
|
+
fs.unlinkSync(readingFile(codexHome));
|
|
116
|
+
} catch (err) {
|
|
117
|
+
// Already gone is the outcome that was asked for.
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
module.exports = { FRESH_MS, configDir, readingFile, read, record, recordAll, correctedFor, clear };
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
// judgement in it costs more in rework than it saves; the ladder loses height
|
|
20
20
|
// a step at a time and 'critical' is the only posture that goes straight to
|
|
21
21
|
// the floor.
|
|
22
|
-
const NEXT_LOWER = { max: 'high', xhigh: 'medium', high: 'medium', medium: 'low', low: 'low' };
|
|
22
|
+
const NEXT_LOWER = { ultra: 'xhigh', none: 'none', minimal: 'minimal', max: 'high', xhigh: 'medium', high: 'medium', medium: 'low', low: 'low' };
|
|
23
23
|
|
|
24
24
|
// Below this share of output, reasoning is not where the money is going, and
|
|
25
25
|
// turning effort down would trade quality for a saving that is not there.
|
|
@@ -187,7 +187,7 @@ function decide(inputs) {
|
|
|
187
187
|
'reasoning is only ' + Math.round(share * 100) +
|
|
188
188
|
'% of output, so effort is not where the money is going';
|
|
189
189
|
} else {
|
|
190
|
-
const target = base.posture === 'critical' ? 'low' : NEXT_LOWER[effortNow] || 'medium';
|
|
190
|
+
const target = base.posture === 'critical' ? (['none', 'minimal'].includes(effortNow) ? effortNow : 'low') : NEXT_LOWER[effortNow] || 'medium';
|
|
191
191
|
if (target !== effortNow) {
|
|
192
192
|
base.effort.target = target;
|
|
193
193
|
base.effort.changes = true;
|
|
@@ -200,6 +200,19 @@ function decide(inputs) {
|
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
+
if (inputs.codex) {
|
|
204
|
+
// Claude's family ladder and slash commands do not describe Codex.
|
|
205
|
+
base.model.why = 'Keep the current Codex model; select another supported model in the host controls when needed.';
|
|
206
|
+
if (base.effort.changes) {
|
|
207
|
+
base.apply.now = 'use the Codex model and effort controls and select ' + base.effort.target;
|
|
208
|
+
base.apply.next = 'node scripts/lowpower.js on --host codex --effort ' + base.effort.target;
|
|
209
|
+
}
|
|
210
|
+
base.notes.push('Saved defaults affect new sessions, not the running task. Profiles and CLI overrides take precedence.');
|
|
211
|
+
if (Number.isFinite(inputs.sessions) && inputs.sessions > 1)
|
|
212
|
+
base.notes.push(inputs.sessions + ' sessions share this account budget.');
|
|
213
|
+
return base;
|
|
214
|
+
}
|
|
215
|
+
|
|
203
216
|
// The main model is only worth flipping when things are critical, and even
|
|
204
217
|
// then it lands in settings.json for the next session: switching the running
|
|
205
218
|
// session's model mid-task invalidates the prompt cache, so the change
|
|
@@ -321,7 +334,7 @@ function renderRecommend(data, turns) {
|
|
|
321
334
|
lines.push(' new sessions: ' + decision.apply.next);
|
|
322
335
|
}
|
|
323
336
|
lines.push(' Model ' + decision.model.why);
|
|
324
|
-
lines.push(' ' + decision.apply.delegate);
|
|
337
|
+
if (decision.apply.delegate) lines.push(' ' + decision.apply.delegate);
|
|
325
338
|
if (decision.model.nextSession) {
|
|
326
339
|
lines.push(' new sessions: main model to ' + decision.model.nextSession + ' until the window resets');
|
|
327
340
|
}
|