@link-assistant/hive-mind 1.46.8 → 1.46.9

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/CHANGELOG.md CHANGED
@@ -1,9 +1,16 @@
1
1
  # @link-assistant/hive-mind
2
2
 
3
+ ## 1.46.9
4
+
5
+ ### Patch Changes
6
+
7
+ - 8104fad: Fix wrong context window calculation showing impossible percentages like 250% (Issue #1539). When peakContextUsage is unknown (e.g. sub-agent models from result JSON only), skip the context window input tokens display entirely instead of falling back to cumulative totals across all requests, which are not valid per-request context window metrics.
8
+
3
9
  ## 1.46.8
4
10
 
5
11
  ### Patch Changes
6
12
 
13
+ - Fix wrong context window calculation showing impossible percentages like 250% (Issue #1539). When peakContextUsage is unknown (e.g. sub-agent models from result JSON only), skip the context window input tokens display entirely instead of falling back to cumulative totals across all requests, which are not valid per-request context window metrics.
7
14
  - bcf2b9b: Retry on network issues and minimize terminal/log output differences (#1536): add ghRetry/ghCmdRetry utilities with exponential backoff for transient network errors (TCP reset, TLS timeout, connection refused, unexpected EOF). Apply retry to critical gh CLI calls: accept-invite, repository setup, auto-fork permission check, visibility detection, write permission check. Log stderr to log file on command failure for terminal/log parity. Add 'unexpected eof' to transient error detection patterns.
8
15
 
9
16
  ## 1.46.7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@link-assistant/hive-mind",
3
- "version": "1.46.8",
3
+ "version": "1.46.9",
4
4
  "description": "AI-powered issue solver and hive mind for collaborative problem solving",
5
5
  "main": "src/hive.mjs",
6
6
  "type": "module",
@@ -158,16 +158,19 @@ export const displayBudgetStats = async (usage, tokenUsage, log) => {
158
158
  const subSessions = tokenUsage?.subSessions || [];
159
159
  const hasMultipleSubSessions = subSessions.length > 1;
160
160
 
161
+ const peakContext = usage.peakContextUsage || 0;
162
+
161
163
  if (hasMultipleSubSessions) {
162
164
  for (let i = 0; i < subSessions.length; i++) {
163
165
  const sub = subSessions[i];
164
166
  const subPeak = sub.peakContextUsage || 0;
165
- const subCumulative = (sub.inputTokens || 0) + (sub.cacheCreationTokens || 0) + (sub.cacheReadTokens || 0);
166
- const contextValue = subPeak > 0 ? subPeak : subCumulative;
167
+ // Issue #1539: Only use peak per-request context for context window display.
168
+ // Cumulative totals across all requests can exceed the context limit and produce
169
+ // impossible percentages (e.g. 250%). When peak is unknown, skip context display.
167
170
  const parts = [];
168
- if (contextLimit && contextValue > 0) {
169
- const pct = ((contextValue / contextLimit) * 100).toFixed(0);
170
- parts.push(`${formatNumber(contextValue)} / ${formatNumber(contextLimit)} input tokens (${pct}%)`);
171
+ if (contextLimit && subPeak > 0) {
172
+ const pct = ((subPeak / contextLimit) * 100).toFixed(0);
173
+ parts.push(`${formatNumber(subPeak)} / ${formatNumber(contextLimit)} input tokens (${pct}%)`);
171
174
  }
172
175
  if (outputLimit) {
173
176
  const outPct = ((sub.outputTokens / outputLimit) * 100).toFixed(0);
@@ -177,15 +180,12 @@ export const displayBudgetStats = async (usage, tokenUsage, log) => {
177
180
  await log(` ${i + 1}. Context window: ${parts.join(', ')}`);
178
181
  }
179
182
  }
180
- } else {
181
- // Single sub-session: single-line format
182
- const peakContext = usage.peakContextUsage || 0;
183
- const cumulativeContext = usage.inputTokens + usage.cacheCreationTokens + usage.cacheReadTokens;
184
- const contextValue = peakContext > 0 ? peakContext : cumulativeContext;
183
+ } else if (peakContext > 0) {
184
+ // Single sub-session with known peak: single-line format
185
185
  const parts = [];
186
- if (contextLimit && contextValue > 0) {
187
- const pct = ((contextValue / contextLimit) * 100).toFixed(0);
188
- parts.push(`${formatNumber(contextValue)} / ${formatNumber(contextLimit)} input tokens (${pct}%)`);
186
+ if (contextLimit) {
187
+ const pct = ((peakContext / contextLimit) * 100).toFixed(0);
188
+ parts.push(`${formatNumber(peakContext)} / ${formatNumber(contextLimit)} input tokens (${pct}%)`);
189
189
  }
190
190
  if (outputLimit) {
191
191
  const outPct = ((usage.outputTokens / outputLimit) * 100).toFixed(0);
@@ -195,6 +195,8 @@ export const displayBudgetStats = async (usage, tokenUsage, log) => {
195
195
  await log(` Context window: ${parts.join(', ')}`);
196
196
  }
197
197
  }
198
+ // Issue #1539: When peakContextUsage is unknown, skip context window line entirely.
199
+ // Cumulative totals are shown on the Total line below — no duplication needed.
198
200
 
199
201
  // Cumulative totals — single line
200
202
  const totalInputNonCached = usage.inputTokens + usage.cacheCreationTokens;
@@ -202,6 +204,11 @@ export const displayBudgetStats = async (usage, tokenUsage, log) => {
202
204
  let totalLine = `${formatNumber(totalInputNonCached)}`;
203
205
  if (cachedTokens > 0) totalLine += ` + ${formatNumber(cachedTokens)} cached`;
204
206
  totalLine += ` input tokens, ${formatNumber(usage.outputTokens)} output tokens`;
207
+ // Issue #1539: When peakContextUsage is unknown, embed output percentage in Total line
208
+ if (peakContext === 0 && outputLimit) {
209
+ const outPct = ((usage.outputTokens / outputLimit) * 100).toFixed(0);
210
+ totalLine += ` (${outPct}% of ${formatNumber(outputLimit)} output limit)`;
211
+ }
205
212
  await log(` Total: ${totalLine}`);
206
213
  };
207
214
 
@@ -230,6 +237,15 @@ export const mergeResultModelUsage = (modelUsage, resultModelUsage) => {
230
237
  if (resultUsage.costUSD != null) {
231
238
  modelUsage[modelId]._resultCostUSD = resultUsage.costUSD;
232
239
  }
240
+ // Issue #1539: Extract model limits from result JSON for sub-agent models
241
+ // Claude Code's result event includes contextWindow and maxOutputTokens per model,
242
+ // which we use as fallback when modelInfo API is unavailable.
243
+ if (resultUsage.contextWindow) {
244
+ modelUsage[modelId]._resultContextWindow = resultUsage.contextWindow;
245
+ }
246
+ if (resultUsage.maxOutputTokens) {
247
+ modelUsage[modelId]._resultMaxOutputTokens = resultUsage.maxOutputTokens;
248
+ }
233
249
  } else {
234
250
  const jsonlUsage = modelUsage[modelId];
235
251
  const jsonlTotal = jsonlUsage.inputTokens + jsonlUsage.cacheCreationTokens + jsonlUsage.cacheReadTokens + jsonlUsage.outputTokens;
@@ -244,6 +260,13 @@ export const mergeResultModelUsage = (modelUsage, resultModelUsage) => {
244
260
  if (resultUsage.costUSD != null) {
245
261
  jsonlUsage._resultCostUSD = resultUsage.costUSD;
246
262
  }
263
+ // Issue #1539: Also extract model limits from result JSON as fallback
264
+ if (resultUsage.contextWindow) {
265
+ jsonlUsage._resultContextWindow = resultUsage.contextWindow;
266
+ }
267
+ if (resultUsage.maxOutputTokens) {
268
+ jsonlUsage._resultMaxOutputTokens = resultUsage.maxOutputTokens;
269
+ }
247
270
  }
248
271
  }
249
272
  };
@@ -274,36 +297,35 @@ const formatSubSessionsList = (subSessions, contextLimit, outputLimit) => {
274
297
  let result = '';
275
298
  for (let i = 0; i < subSessions.length; i++) {
276
299
  const sub = subSessions[i];
300
+ // Issue #1539: Only use peak per-request context; skip context display when unknown
277
301
  const subPeakContext = sub.peakContextUsage || 0;
278
- // Cumulative fallback: inputTokens + cacheCreationTokens + cacheReadTokens for this sub-session
279
- const subCumulative = (sub.inputTokens || 0) + (sub.cacheCreationTokens || 0) + (sub.cacheReadTokens || 0);
280
- result += formatContextOutputLine(subPeakContext, contextLimit, sub.outputTokens, outputLimit, `${i + 1}. `, subCumulative);
302
+ result += formatContextOutputLine(subPeakContext, contextLimit, sub.outputTokens, outputLimit, `${i + 1}. `);
281
303
  }
282
304
  return result;
283
305
  };
284
306
 
285
307
  /**
286
308
  * Issue #1526: Build a single-line context window + output tokens string.
309
+ * Issue #1539: Only show context window when peakContext > 0 (per-request peak known).
310
+ * When peakContext is 0 (unknown), context part is omitted to avoid misleading percentages.
287
311
  * Format: "- Context window: X / Y input tokens (Z%), A / B output tokens (W%)"
288
- * When only one of context or output limits is available, shows just that part.
289
- * @param {number} peakContext - Peak context usage (0 if unknown)
312
+ * @param {number} peakContext - Peak context usage (0 if unknown context display skipped)
290
313
  * @param {number} contextLimit - Context window limit (null if unknown)
291
314
  * @param {number} outputTokens - Output tokens used
292
315
  * @param {number} outputLimit - Output token limit (null if unknown)
293
316
  * @param {string} [prefix='- '] - Line prefix
294
317
  * @returns {string} Formatted line or empty string
295
318
  */
296
- const formatContextOutputLine = (peakContext, contextLimit, outputTokens, outputLimit, prefix = '- ', cumulativeContext = 0) => {
319
+ const formatContextOutputLine = (peakContext, contextLimit, outputTokens, outputLimit, prefix = '- ') => {
297
320
  const parts = [];
298
321
  if (contextLimit) {
299
- // Use peakContextUsage when available (per-request peak from JSONL tracking).
300
- // Fall back to cumulative total (inputTokens + cacheCreationTokens + cacheReadTokens)
301
- // when peak is unknown (e.g., model only from result JSON, not in JSONL).
302
- // Issue #1526: Never skip context display always show what data we have.
303
- const contextValue = peakContext > 0 ? peakContext : cumulativeContext;
304
- if (contextValue > 0) {
305
- const pct = ((contextValue / contextLimit) * 100).toFixed(0);
306
- parts.push(`${formatTokensCompact(contextValue)} / ${formatTokensCompact(contextLimit)} input tokens (${pct}%)`);
322
+ // Issue #1539: Only use peak per-request context for context window display.
323
+ // When peak is unknown (e.g., model only from result JSON, not in JSONL),
324
+ // skip context display. Cumulative totals across all requests are not valid
325
+ // context window metrics and produce impossible percentages (e.g. 250%).
326
+ if (peakContext > 0) {
327
+ const pct = ((peakContext / contextLimit) * 100).toFixed(0);
328
+ parts.push(`${formatTokensCompact(peakContext)} / ${formatTokensCompact(contextLimit)} input tokens (${pct}%)`);
307
329
  }
308
330
  }
309
331
  if (outputLimit) {
@@ -322,7 +344,8 @@ const formatContextOutputLine = (peakContext, contextLimit, outputTokens, output
322
344
  * Sub-sessions are shown as a global section (not duplicated per model) since JSONL
323
345
  * sub-session tracking is global across all models.
324
346
  * Issue #1526: Shorter output format — context window + output tokens on single line.
325
- * Fix: exclude cacheReadTokens from context window fallback calculation (cumulative ≠ per-request).
347
+ * Issue #1539: Only display context window when peak per-request usage is known.
348
+ * Cumulative totals are never used as context window metrics (they can exceed model limits).
326
349
  * @param {Object} tokenUsage - Token usage data from calculateSessionTokens or buildAgentBudgetStats
327
350
  * @returns {string} Formatted markdown string for PR comment
328
351
  */
@@ -358,17 +381,17 @@ export const buildBudgetStatsString = tokenUsage => {
358
381
 
359
382
  if (isMultiModel) stats += `\n\n**${modelName}:**`;
360
383
 
384
+ const peakContext = usage.peakContextUsage || 0;
385
+
361
386
  if (!isMultiModel && hasMultipleSubSessions) {
362
387
  // Single-model + multiple sub-sessions: show numbered sub-sessions under that model
363
388
  stats += formatSubSessionsList(subSessions, contextLimit, outputLimit);
364
- } else {
389
+ } else if (peakContext > 0) {
365
390
  // Issue #1526: Single line format for context window + output tokens
366
- // Use peakContextUsage when available; fall back to cumulative total when peak is unknown
367
- // (e.g., for result-JSON-sourced sub-agent models where only cumulative totals are available)
368
- const peakContext = usage.peakContextUsage || 0;
369
- const cumulativeContext = usage.inputTokens + usage.cacheCreationTokens + usage.cacheReadTokens;
370
- stats += formatContextOutputLine(peakContext, contextLimit, usage.outputTokens, outputLimit, '- ', cumulativeContext);
391
+ stats += formatContextOutputLine(peakContext, contextLimit, usage.outputTokens, outputLimit, '- ');
371
392
  }
393
+ // Issue #1539: When peakContextUsage is unknown, skip context window line entirely.
394
+ // Cumulative totals are shown on the Total line below — no duplication needed.
372
395
 
373
396
  // Cumulative totals per model: input tokens + cached shown separately
374
397
  // Issue #1526: Shorter format — single "Total:" line
@@ -378,6 +401,13 @@ export const buildBudgetStatsString = tokenUsage => {
378
401
  if (cachedTokens > 0) totalLine += ` + ${formatTokensCompact(cachedTokens)} cached`;
379
402
  totalLine += ` input tokens, ${formatTokensCompact(usage.outputTokens)} output tokens`;
380
403
 
404
+ // Issue #1539: When peakContextUsage is unknown (no per-request data), embed
405
+ // output token percentage in the Total line so no data is lost.
406
+ if (peakContext === 0 && outputLimit) {
407
+ const outPct = ((usage.outputTokens / outputLimit) * 100).toFixed(0);
408
+ totalLine += ` (${outPct}% of ${formatTokensCompact(outputLimit)} output limit)`;
409
+ }
410
+
381
411
  // Issue #1508: Show per-model cost when available
382
412
  if (usage.costUSD !== null && usage.costUSD !== undefined) {
383
413
  totalLine += `, $${usage.costUSD.toFixed(6)} cost`;
@@ -498,13 +498,10 @@ export const calculateSessionTokens = async (sessionId, tempDir, resultModelUsag
498
498
  }
499
499
  // Initialize per-model usage tracking
500
500
  const modelUsage = {};
501
- // Issue #1501: Deduplicate JSONL entries by message ID (upstream: anthropics/claude-code#6805)
502
- // Claude Code's stream-json mode splits single API responses with multiple content blocks
503
- // into separate JSONL entries, each with the same message ID and identical usage stats.
501
+ // Issue #1501: Deduplicate JSONL entries by message ID (stream-json splits responses)
504
502
  const seenMessageIds = new Set();
505
503
  let duplicateCount = 0;
506
504
  // Issue #1501: Track peak context usage per request (not cumulative)
507
- // The context window limit is per-request, so we track the max single-request fill.
508
505
  const peakContextByModel = {};
509
506
  let globalPeakContext = 0;
510
507
  // Issue #1491: Track sub-sessions between compactification events
@@ -610,7 +607,10 @@ export const calculateSessionTokens = async (sessionId, tempDir, resultModelUsag
610
607
  usage.costUSD = usage._resultCostUSD ?? null;
611
608
  usage.costBreakdown = null;
612
609
  usage.modelName = modelId;
613
- usage.modelInfo = null;
610
+ // Issue #1539: Use contextWindow/maxOutputTokens from result JSON as fallback model limits
611
+ const ctx = usage._resultContextWindow,
612
+ out = usage._resultMaxOutputTokens;
613
+ usage.modelInfo = ctx || out ? { limit: { context: ctx || null, output: out || null } } : null;
614
614
  }
615
615
  }
616
616
  // Calculate grand totals across all models