benignswarm 0.12.110 → 0.12.112

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/dist/usage.js CHANGED
@@ -1,6 +1,13 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
2
  import { claudeTranscript, dshUsage } from "./harnesses.js";
3
3
  import { antigravityUsage } from "./antigravity.js";
4
+ import { scanLinesBackward, TAIL_CHUNK_BYTES } from "./tail-lines.js";
5
+ /**
6
+ * How far back a usage window may be read. The watcher measures every completed turn, so this is read
7
+ * once per turn rather than per pass, and a turn rarely writes more than a few MB; past the cap the
8
+ * result is "unavailable" with the reason, never a partial count (WU #474).
9
+ */
10
+ export const USAGE_MAX_BYTES = 32 * 1024 * 1024;
4
11
  /** Keep hook-only Claude compaction metadata only when it belongs to the exact session selected. */
5
12
  export function claudeSessionWithHookMetadata(sessionId, transcript, provider, remembered) {
6
13
  const sameSession = remembered?.harness === "claude-code" && remembered.session_id === sessionId;
@@ -155,23 +162,34 @@ export function sessionCompactions(session, since, until) {
155
162
  * last total at or before `until` minus the last total before `since` (zero when the session began
156
163
  * inside the window).
157
164
  */
158
- export function codexUsage(path, since, until) {
165
+ export function codexUsage(path, since, until, chunkBytes = TAIL_CHUNK_BYTES, maxBytes = USAGE_MAX_BYTES) {
159
166
  let before = null, end = null;
160
167
  let plan = null;
161
- for (const j of lines(path)) {
168
+ // From the end: the first match met going back is the last one in file order, which is what the whole-file read kept.
169
+ const scan = scanLinesBackward(path, (line) => {
170
+ let j;
171
+ try {
172
+ j = JSON.parse(line);
173
+ }
174
+ catch {
175
+ return false;
176
+ }
162
177
  const p = j.payload;
163
178
  const at = typeof j.timestamp === "string" ? j.timestamp : null;
164
179
  if (j.type !== "event_msg" || p?.type !== "token_count" || !at)
165
- continue;
180
+ return false;
166
181
  const total = p.info?.total_token_usage;
167
- if (total && at < since)
182
+ if (total && at < since && !before)
168
183
  before = total;
169
- if (total && at <= until)
184
+ if (total && at <= until && !end)
170
185
  end = total;
171
- if (at <= until && p.rate_limits?.primary && typeof p.rate_limits.primary.used_percent === "number")
186
+ if (at <= until && !plan && p.rate_limits?.primary && typeof p.rate_limits.primary.used_percent === "number")
172
187
  plan = { used_percent: p.rate_limits.primary.used_percent, window_minutes: p.rate_limits.primary.window_minutes ?? null };
173
- }
188
+ return before !== null && plan !== null;
189
+ }, chunkBytes, maxBytes);
174
190
  const base = { harness: "codex", source: "codex-rollout", scope: "session", since, until, tokens: null, plan };
191
+ if (scan.capped && !before)
192
+ return { ...base, reason: `the window's start is beyond the last ${Math.round(maxBytes / 1048576)} MB of the session file; not counted` };
175
193
  if (!end)
176
194
  return { ...base, reason: "no token counts in the session up to now" };
177
195
  const d = (k) => (end[k] ?? 0) - (before?.[k] ?? 0);
@@ -179,20 +197,38 @@ export function codexUsage(path, since, until) {
179
197
  t.total = t.input + t.output;
180
198
  return { ...base, tokens: t };
181
199
  }
200
+ /** Claude transcripts are appended in time order; entries this far before the window's start end the backward read. */
201
+ const CLAUDE_ORDER_SLACK_MS = 5 * 60_000;
182
202
  /**
183
203
  * Claude Code transcript: assistant entries carry `message.usage`. One message is written once per
184
204
  * content block with the same id, so each message id counts once (its last entry).
185
205
  */
186
- export function claudeUsage(path, since, until) {
206
+ export function claudeUsage(path, since, until, chunkBytes = TAIL_CHUNK_BYTES, maxBytes = USAGE_MAX_BYTES) {
187
207
  const byMessage = new Map();
188
- for (const j of lines(path)) {
208
+ const stopBefore = new Date(Date.parse(since) - CLAUDE_ORDER_SLACK_MS).toISOString();
209
+ // From the end; the first entry met per message id is its last in file order, the one the whole-file read kept.
210
+ const scan = scanLinesBackward(path, (line) => {
211
+ let j;
212
+ try {
213
+ j = JSON.parse(line);
214
+ }
215
+ catch {
216
+ return false;
217
+ }
189
218
  const m = j.message;
190
219
  const at = typeof j.timestamp === "string" ? j.timestamp : null;
220
+ if (at && at < stopBefore)
221
+ return true;
191
222
  if (j.type !== "assistant" || !m?.usage || !at || at < since || at > until)
192
- continue;
193
- byMessage.set(m.id ?? `${at}`, m.usage);
194
- }
223
+ return false;
224
+ const key = m.id ?? `${at}`;
225
+ if (!byMessage.has(key))
226
+ byMessage.set(key, m.usage);
227
+ return false;
228
+ }, chunkBytes, maxBytes);
195
229
  const base = { harness: "claude-code", source: "claude-transcript", scope: "session", since, until, tokens: null };
230
+ if (scan.capped)
231
+ return { ...base, reason: `the window's start is beyond the last ${Math.round(maxBytes / 1048576)} MB of the transcript; not counted` };
196
232
  if (!byMessage.size)
197
233
  return { ...base, reason: "no assistant messages in the window" };
198
234
  const t = zero();
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = "0.12.110";
1
+ export const CLI_VERSION = "0.12.112";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "benignswarm",
3
- "version": "0.12.110",
3
+ "version": "0.12.112",
4
4
  "description": "b9s: chat, status board and evidence store for coding agents (benignswarm.org)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {