burnoff 1.4.1 → 2.1.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.
Files changed (62) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/.github/workflows/burnoff-pr-gate.yml +5 -3
  3. package/.github/workflows/burnoff-startup-lint.yml +2 -2
  4. package/CHANGELOG.md +74 -0
  5. package/README.md +187 -213
  6. package/SECURITY.md +38 -0
  7. package/bin/burnoff.mjs +307 -32
  8. package/commands/burnoff.md +6 -4
  9. package/docs/CLAUDE_CODE_PLUGIN.md +29 -28
  10. package/docs/DESIGN_PARTNER_INTAKE.md +3 -3
  11. package/hooks/hook-io.mjs +216 -11
  12. package/hooks/hooks.json +44 -0
  13. package/hooks/post-tool-use.mjs +3 -1
  14. package/hooks/pre-tool-use.mjs +47 -8
  15. package/hooks/session-lifecycle.mjs +24 -3
  16. package/hooks/user-prompt-submit.mjs +98 -0
  17. package/package.json +6 -4
  18. package/scripts/bench-hooks.mjs +34 -3
  19. package/scripts/bench-savings.mjs +318 -0
  20. package/scripts/burnoff-statusline.mjs +138 -6
  21. package/src/ci/ci-gate.mjs +199 -13
  22. package/src/ci/pr-cost.mjs +417 -45
  23. package/src/ci/pr-summary.mjs +467 -0
  24. package/src/cli/audit.mjs +179 -28
  25. package/src/cli/doctor.mjs +32 -6
  26. package/src/cli/hook-health.mjs +9 -1
  27. package/src/cli/init.mjs +226 -39
  28. package/src/cli/preview.mjs +299 -0
  29. package/src/cli/startup-audit/formatter.mjs +248 -0
  30. package/src/cli/startup-audit/jsonc.mjs +601 -0
  31. package/src/cli/startup-audit/mcp-audit.mjs +735 -0
  32. package/src/cli/startup-audit/rules-hygiene.mjs +645 -0
  33. package/src/cli/startup-audit/telemetry-scan.mjs +975 -0
  34. package/src/cli/startup-audit.mjs +451 -2471
  35. package/src/clients/claude-code.mjs +41 -4
  36. package/src/config/catalog.json +295 -0
  37. package/src/config/effort.mjs +145 -0
  38. package/src/config/harness-quirks.mjs +340 -0
  39. package/src/config/loader.mjs +27 -2
  40. package/src/config/policy.mjs +81 -9
  41. package/src/config/pricing.mjs +247 -19
  42. package/src/config/semver.mjs +25 -0
  43. package/src/config/uncached-attribution.mjs +280 -41
  44. package/src/guards/compaction-baton.mjs +248 -0
  45. package/src/guards/loop-breaker.mjs +328 -47
  46. package/src/guards/model-switch-hook.mjs +126 -0
  47. package/src/guards/phase5-detectors.mjs +275 -21
  48. package/src/guards/post-tool-engine.mjs +333 -11
  49. package/src/guards/pre-tool-engine.mjs +607 -45
  50. package/src/guards/prefix-guard.mjs +609 -0
  51. package/src/guards/prompt-guard.mjs +309 -0
  52. package/src/guards/read-clamp.mjs +192 -8
  53. package/src/guards/session-hook.mjs +149 -7
  54. package/src/guards/shell-guard.mjs +56 -5
  55. package/src/guards/snooze.mjs +169 -5
  56. package/src/ipc/daemon.mjs +76 -0
  57. package/src/ipc/fast-client.mjs +165 -4
  58. package/src/ipc/protocol.mjs +16 -3
  59. package/src/storage/antigravity-parser.mjs +16 -2
  60. package/src/storage/file-lock.mjs +16 -3
  61. package/src/storage/jsonl-parser.mjs +133 -22
  62. package/src/storage/telemetry-metrics.mjs +254 -4
@@ -234,6 +234,17 @@ export function discoverAntigravityConversationDbs({
234
234
  return found.slice(0, cap).map((x) => x.dbPath);
235
235
  }
236
236
 
237
+ function decodeErrorDetailsText(blob) {
238
+ const bytes = toUint8View(blob);
239
+ if (!bytes || bytes.length === 0) return null;
240
+ const text = Buffer.from(bytes)
241
+ .toString('utf8')
242
+ .replace(/[^\x20-\x7e\r\n\t]+/g, ' ')
243
+ .replace(/\s+/g, ' ')
244
+ .trim();
245
+ return text ? text.slice(0, 500) : null;
246
+ }
247
+
237
248
  /**
238
249
  * Reads an Antigravity conversation SQLite `.db` strictly read-only to extract
239
250
  * step `durationMs`, `ttftMs`, token counts, and `tokensPerSec`.
@@ -251,6 +262,26 @@ export function readAntigravityConversationDbMetrics(dbPath) {
251
262
  let db = null;
252
263
  try {
253
264
  db = new DbCtor(resolved, { readOnly: true });
265
+ try {
266
+ db.exec?.('PRAGMA busy_timeout = 3000;');
267
+ } catch {
268
+ // ignore: mock or immutable DB without PRAGMA support
269
+ }
270
+ let sessionId = path.basename(resolved, '.db');
271
+ let trajectoryType = null;
272
+ try {
273
+ const metaRow = db.prepare('SELECT trajectory_id, cascade_id, trajectory_type, source FROM trajectory_meta LIMIT 1').get();
274
+ if (metaRow) {
275
+ if (metaRow.cascade_id) sessionId = String(metaRow.cascade_id);
276
+ if (metaRow.trajectory_type !== undefined && metaRow.trajectory_type !== null) {
277
+ const tt = Number(metaRow.trajectory_type);
278
+ if (Number.isFinite(tt)) trajectoryType = tt;
279
+ }
280
+ }
281
+ } catch {
282
+ // ignore: trajectory_meta table optional in minimal Antigravity test DBs
283
+ }
284
+
254
285
  const genMap = new Map();
255
286
  try {
256
287
  const genRows = db.prepare('SELECT idx, data FROM gen_metadata WHERE data IS NOT NULL').all();
@@ -261,19 +292,75 @@ export function readAntigravityConversationDbMetrics(dbPath) {
261
292
  // ignore: gen_metadata table optional in partial or minimal Antigravity DBs
262
293
  }
263
294
 
264
- const stepRows = db.prepare('SELECT idx, metadata FROM steps WHERE metadata IS NOT NULL').all();
295
+ const colNames = new Set();
296
+ try {
297
+ const cols = db.prepare('PRAGMA table_info(steps)').all();
298
+ for (const col of cols) {
299
+ if (col?.name) colNames.add(String(col.name));
300
+ }
301
+ } catch {
302
+ // ignore: fallback to minimal steps schema
303
+ }
304
+ const hasStepType = colNames.has('step_type');
305
+ const hasStatus = colNames.has('status');
306
+ const hasErrorDetails = colNames.has('error_details');
307
+
308
+ const selectCols = ['idx', 'metadata'];
309
+ if (hasStepType) selectCols.push('step_type');
310
+ if (hasStatus) selectCols.push('status');
311
+ if (hasErrorDetails) selectCols.push('error_details');
312
+
313
+ const stepRows = db
314
+ .prepare(`SELECT ${selectCols.join(', ')} FROM steps WHERE metadata IS NOT NULL ORDER BY idx ASC`)
315
+ .all();
265
316
  const steps = [];
317
+ const seenGenIdx = new Set();
318
+ const compactionStepIndices = [];
319
+ const errorSteps = [];
320
+ let pendingCompaction = false;
321
+
266
322
  for (const row of stepRows) {
323
+ const rowIdx = Number(row.idx);
267
324
  const meta = decodeAntigravityStepMetadata(row.metadata);
268
- const joinIdx = meta.genMetadataIdx !== null ? meta.genMetadataIdx : Number(row.idx);
269
- const gen = genMap.get(joinIdx) || { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0 };
325
+ const stepType = hasStepType && row.step_type !== undefined && row.step_type !== null ? Number(row.step_type) : null;
326
+ const status = hasStatus && row.status !== undefined && row.status !== null ? Number(row.status) : null;
327
+ const errorDetailsText = hasErrorDetails ? decodeErrorDetailsText(row.error_details) : null;
328
+
329
+ if (stepType === 23) {
330
+ compactionStepIndices.push(rowIdx);
331
+ pendingCompaction = true;
332
+ continue;
333
+ }
334
+
335
+ const isModelStep = hasStepType ? stepType === 15 : meta.genMetadataIdx !== null;
336
+ if (!isModelStep) {
337
+ if (errorDetailsText) {
338
+ errorSteps.push({ idx: rowIdx, stepType, status, errorDetailsText });
339
+ }
340
+ continue;
341
+ }
342
+
343
+ let gen = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0 };
344
+ if (meta.genMetadataIdx !== null && !seenGenIdx.has(meta.genMetadataIdx)) {
345
+ seenGenIdx.add(meta.genMetadataIdx);
346
+ gen = genMap.get(meta.genMetadataIdx) || gen;
347
+ }
348
+
270
349
  const tokensPerSec =
271
350
  meta.durationMs !== null && meta.durationMs > 0 && gen.outputTokens > 0
272
351
  ? Number((gen.outputTokens / (meta.durationMs / 1000)).toFixed(2))
273
352
  : null;
274
353
  steps.push({
275
- idx: Number(row.idx),
354
+ idx: rowIdx,
355
+ stepIndex: rowIdx,
356
+ stepType,
357
+ status,
358
+ errorDetailsText,
359
+ trajectoryType,
360
+ hadCompactionBefore: pendingCompaction,
361
+ genMetadataIdx: meta.genMetadataIdx,
276
362
  createdMs: meta.createdMs,
363
+ firstTokenMs: meta.firstTokenMs,
277
364
  finishedMs: meta.finishedMs,
278
365
  durationMs: meta.durationMs,
279
366
  ttftMs: meta.ttftMs,
@@ -282,11 +369,18 @@ export function readAntigravityConversationDbMetrics(dbPath) {
282
369
  cacheReadTokens: gen.cacheReadTokens,
283
370
  tokensPerSec
284
371
  });
372
+ pendingCompaction = false;
285
373
  }
286
374
 
287
375
  return {
288
376
  ok: true,
289
377
  dbPath: resolved,
378
+ sessionId,
379
+ trajectoryType,
380
+ hasStepType,
381
+ totalRawStepRows: stepRows.length,
382
+ compactionStepIndices,
383
+ errorSteps,
290
384
  stepCount: steps.length,
291
385
  steps
292
386
  };
@@ -309,6 +403,160 @@ export function readAntigravityConversationDbMetrics(dbPath) {
309
403
  }
310
404
  }
311
405
 
406
+ const MAX_SANITY_STEP_TOKENS = 50_000_000;
407
+
408
+ /**
409
+ * Validates Antigravity SQLite decoded counters against non-negative bounds and the
410
+ * warm-cache invariant (`cacheReadTokens <= prevPromptPlusOutput * 1.05 + 512`).
411
+ */
412
+ export function validateAntigravityDbCounters(dbResult) {
413
+ if (!dbResult || !dbResult.ok) {
414
+ return { valid: false, reason: dbResult?.reason || 'sqlite_unreadable' };
415
+ }
416
+ const steps = Array.isArray(dbResult.steps) ? dbResult.steps : [];
417
+ if (steps.length === 0) {
418
+ return { valid: false, reason: 'empty_db_steps' };
419
+ }
420
+
421
+ for (let i = 0; i < steps.length; i += 1) {
422
+ const st = steps[i] || {};
423
+ for (const field of ['inputTokens', 'cacheReadTokens', 'outputTokens']) {
424
+ const val = Number(st[field]);
425
+ if (!Number.isFinite(val) || !Number.isInteger(val) || val < 0 || val > MAX_SANITY_STEP_TOKENS) {
426
+ return { valid: false, reason: `invalid_${field}`, stepIndex: st.idx ?? i, value: st[field] };
427
+ }
428
+ }
429
+ if (i > 0 && st.cacheReadTokens > 0) {
430
+ const prev = steps[i - 1];
431
+ const prevPromptPlusOutput =
432
+ Number(prev.inputTokens || 0) + Number(prev.cacheReadTokens || 0) + Number(prev.outputTokens || 0);
433
+ const maxAllowed = prevPromptPlusOutput * 1.05 + 512;
434
+ if (prevPromptPlusOutput > 0 && st.cacheReadTokens > maxAllowed) {
435
+ return {
436
+ valid: false,
437
+ reason: 'warm_cache_invariant_violated',
438
+ stepIndex: st.idx ?? i,
439
+ cacheReadTokens: st.cacheReadTokens,
440
+ maxAllowed: Math.round(maxAllowed)
441
+ };
442
+ }
443
+ }
444
+ }
445
+
446
+ return { valid: true, reason: null };
447
+ }
448
+
449
+ /**
450
+ * Safely merges Antigravity `<uuid>.db` counters/timings with `transcript.jsonl` metadata
451
+ * using field-level single-source authority (never sums `.db` and `.jsonl` counters).
452
+ */
453
+ export function mergeAntigravitySessionSources({ sessionId = 'antigravity-session', dbResult = null, transcriptParsed = null } = {}) {
454
+ const transcriptSteps = Array.isArray(transcriptParsed?.steps) ? transcriptParsed.steps : [];
455
+
456
+ if (!dbResult || dbResult.reason === 'missing_db') {
457
+ return {
458
+ counterSource: 'transcript_jsonl',
459
+ trajectoryType: null,
460
+ notices: [],
461
+ steps: transcriptSteps.map((s) => ({ ...s, stepSource: 'transcript_jsonl' }))
462
+ };
463
+ }
464
+
465
+ const sanity = validateAntigravityDbCounters(dbResult);
466
+ if (!sanity.valid) {
467
+ const reason = sanity.reason || dbResult.reason || 'sqlite_unreadable';
468
+ return {
469
+ counterSource: 'transcript_fallback',
470
+ trajectoryType: dbResult?.trajectoryType ?? null,
471
+ notices: [
472
+ {
473
+ code: 'SQLITE_SANITY_FALLBACK',
474
+ sessionId,
475
+ reason,
476
+ message: `Antigravity SQLite counters failed sanity check (${reason}) for session ${sessionId}; fell back to transcript.jsonl.`
477
+ }
478
+ ],
479
+ steps: transcriptSteps.map((s) => ({ ...s, stepSource: 'transcript_jsonl' }))
480
+ };
481
+ }
482
+
483
+ const transcriptByIdx = new Map();
484
+ for (const ts of transcriptSteps) {
485
+ if (ts && Number.isFinite(Number(ts.stepIndex))) {
486
+ transcriptByIdx.set(Number(ts.stepIndex), ts);
487
+ }
488
+ }
489
+ const consumed = new Set();
490
+ const mergedSteps = [];
491
+
492
+ for (const dbStep of dbResult.steps) {
493
+ const stepIdx = Number(dbStep.idx);
494
+ const tStep = transcriptByIdx.get(stepIdx) || null;
495
+ if (tStep) consumed.add(stepIdx);
496
+
497
+ const thinkingTokens = Number(tStep?.thinkingTokens || 0);
498
+ const createdAt = dbStep.createdMs != null ? new Date(dbStep.createdMs).toISOString() : (tStep?.createdAt ?? null);
499
+ const finishedAt = dbStep.finishedMs != null ? new Date(dbStep.finishedMs).toISOString() : (tStep?.finishedAt ?? null);
500
+
501
+ mergedSteps.push({
502
+ ...(tStep || {}),
503
+ stepIndex: stepIdx,
504
+ type: tStep?.type || 'assistant',
505
+ rawType: tStep?.rawType || 'PLANNER_RESPONSE',
506
+ client: 'antigravity',
507
+ entrypoint: tStep?.entrypoint || 'antigravity',
508
+ createdAt,
509
+ finishedAt,
510
+ createdMs: dbStep.createdMs,
511
+ firstTokenMs: dbStep.firstTokenMs,
512
+ finishedMs: dbStep.finishedMs,
513
+ durationMs: dbStep.durationMs,
514
+ ttftMs: dbStep.ttftMs,
515
+ tokensPerSec: dbStep.tokensPerSec,
516
+ stepType: dbStep.stepType,
517
+ status: dbStep.status,
518
+ trajectoryType: dbStep.trajectoryType,
519
+ hadCompactionBefore: Boolean(dbStep.hadCompactionBefore || tStep?.hadCompactionBefore),
520
+ model: tStep?.model || null,
521
+ label: tStep?.label || null,
522
+ toolCalls: Array.isArray(tStep?.toolCalls) ? tStep.toolCalls : [],
523
+ errorText: tStep?.errorText || dbStep.errorDetailsText || null,
524
+ freshInputTokens: dbStep.inputTokens,
525
+ inputTokens: dbStep.inputTokens,
526
+ cacheReadTokens: dbStep.cacheReadTokens,
527
+ cacheCreationTokens: 0,
528
+ outputTokens: dbStep.outputTokens,
529
+ thinkingTokens,
530
+ totalPromptTokens: dbStep.inputTokens + dbStep.cacheReadTokens,
531
+ tokenUsage: {
532
+ freshInputTokens: dbStep.inputTokens,
533
+ inputTokens: dbStep.inputTokens,
534
+ cacheReadTokens: dbStep.cacheReadTokens,
535
+ cacheCreationTokens: 0,
536
+ outputTokens: dbStep.outputTokens,
537
+ thinkingTokens
538
+ },
539
+ stepSource: tStep ? 'merged' : 'sqlite_db'
540
+ });
541
+ }
542
+
543
+ for (const ts of transcriptSteps) {
544
+ const idx = Number(ts?.stepIndex);
545
+ if (!Number.isFinite(idx) || !consumed.has(idx)) {
546
+ mergedSteps.push({ ...ts, stepSource: 'transcript_jsonl' });
547
+ }
548
+ }
549
+
550
+ mergedSteps.sort((a, b) => Number(a.stepIndex ?? 0) - Number(b.stepIndex ?? 0));
551
+
552
+ return {
553
+ counterSource: 'sqlite_db',
554
+ trajectoryType: dbResult.trajectoryType ?? null,
555
+ notices: [],
556
+ steps: mergedSteps
557
+ };
558
+ }
559
+
312
560
  function readAttrValue(val) {
313
561
  if (val === null || val === undefined) return undefined;
314
562
  if (typeof val !== 'object') return val;
@@ -696,6 +944,8 @@ export default {
696
944
  decodeAntigravityGenMetadata,
697
945
  discoverAntigravityConversationDbs,
698
946
  readAntigravityConversationDbMetrics,
947
+ validateAntigravityDbCounters,
948
+ mergeAntigravitySessionSources,
699
949
  parseOtelGenAiSpans,
700
950
  discoverLocalOtelFiles,
701
951
  extractStepTimingMetrics,