burnoff 1.1.0 → 1.3.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 +11 -0
- package/.github/workflows/burnoff-pr-gate.yml +3 -3
- package/.github/workflows/burnoff-startup-lint.yml +37 -0
- package/README.md +90 -75
- package/bin/burnoff.mjs +185 -24
- package/commands/burnoff.md +16 -0
- package/docs/CLAUDE_CODE_PLUGIN.md +67 -0
- package/docs/DESIGN_PARTNER_INTAKE.md +44 -0
- package/hooks/hook-io.mjs +19 -7
- package/hooks/post-tool-use.mjs +33 -26
- package/hooks/pre-tool-use.mjs +43 -41
- package/hooks/session-lifecycle.mjs +40 -0
- package/package.json +15 -7
- package/scripts/burnoff-statusline.mjs +84 -0
- package/src/ci/ci-gate.mjs +22 -0
- package/src/ci/pr-cost.mjs +42 -10
- package/src/ci/team-digest.mjs +5 -3
- package/src/cli/audit.mjs +103 -18
- package/src/cli/doctor.mjs +209 -14
- package/src/cli/init.mjs +349 -96
- package/src/cli/startup-audit.mjs +712 -62
- package/src/clients/antigravity.mjs +285 -0
- package/src/clients/claude-code.mjs +180 -0
- package/src/clients/detect.mjs +64 -0
- package/src/config/loader.mjs +11 -4
- package/src/config/paths.mjs +62 -2
- package/src/config/policy.mjs +34 -5
- package/src/config/pricing.mjs +16 -5
- package/src/guards/loop-breaker.mjs +198 -13
- package/src/guards/post-tool-engine.mjs +289 -29
- package/src/guards/pre-tool-engine.mjs +382 -151
- package/src/guards/read-clamp.mjs +78 -18
- package/src/guards/session-hook.mjs +207 -0
- package/src/guards/shell-guard.mjs +583 -73
- package/src/guards/snooze.mjs +360 -0
- package/src/ipc/daemon.mjs +50 -8
- package/src/ipc/fast-client.mjs +62 -5
- package/src/ipc/protocol.mjs +1 -1
- package/src/storage/antigravity-parser.mjs +258 -0
- package/src/storage/file-lock.mjs +13 -4
- package/src/storage/fts5-archive.mjs +4 -4
- package/src/storage/jsonl-parser.mjs +7 -1
- package/src/storage/ledger.mjs +10 -3
- package/src/storage/sqlite-client.mjs +4 -2
- package/hooks/hooks.json +0 -12
|
@@ -9,6 +9,7 @@ import path from 'node:path';
|
|
|
9
9
|
import crypto from 'node:crypto';
|
|
10
10
|
import { resolveEffectiveRates } from '../config/loader.mjs';
|
|
11
11
|
import { loadBurnoffPolicy } from '../config/policy.mjs';
|
|
12
|
+
import { getWorkspaceStateDir } from '../config/paths.mjs';
|
|
12
13
|
import { withFileLockSync } from '../storage/file-lock.mjs';
|
|
13
14
|
import { atomicWriteFileSync } from '../storage/atomic-io.mjs';
|
|
14
15
|
import { recordInterventionSavings } from '../storage/ledger.mjs';
|
|
@@ -32,6 +33,7 @@ export const MEASURED_REREAD_P_CODE = 0.73; // 0.27 realization factor (1 - 0.73
|
|
|
32
33
|
export const MEASURED_REREAD_P_DOC = 0.15;
|
|
33
34
|
export const MEASURED_SOLO_TURN_P_GIVEN_REREAD = 0.19;
|
|
34
35
|
export const ASSUMED_MIN_PROMPT_TOKENS = 20_000;
|
|
36
|
+
export const DEFAULT_READ_INTERVAL_TTL_MS = 30 * 60 * 1000;
|
|
35
37
|
|
|
36
38
|
const BURNOFF_CACHE_ARTIFACT_PATH_RE =
|
|
37
39
|
/(?:^|\/)(?:\.claude\/cache\/artifacts|\.burnoff\/cache)(?:\/|$)/i;
|
|
@@ -39,6 +41,7 @@ const GENERATED_OR_MINIFIED_CODE_RE =
|
|
|
39
41
|
/(?:\.min|\.bundle|\.chunk)\.(?:m?js|cjs|css)$|(?:^|\/)(?:node_modules|dist|build|vendor|\.next|coverage)(?:\/|$)/i;
|
|
40
42
|
const ACTIVE_SOURCE_CODE_EXT_RE =
|
|
41
43
|
/\.(?:mjs|cjs|js|jsx|ts|tsx|py|go|rs|java|kt|kts|cs|cpp|cc|cxx|c|h|hpp|rb|php|swift|scala|vue|svelte|sql|sh|bash|zsh|ps1)$/i;
|
|
44
|
+
export const STRUCTURED_DATA_EXT_RE = /\.(?:json|yaml|yml|toml|xml|csv)$/i;
|
|
42
45
|
|
|
43
46
|
export function isBurnoffArtifactPath(filePath = '') {
|
|
44
47
|
const norm = String(filePath || '').replace(/\\/g, '/').trim();
|
|
@@ -56,6 +59,25 @@ export function isActiveSourceCodePath(filePath = '') {
|
|
|
56
59
|
return ACTIVE_SOURCE_CODE_EXT_RE.test(raw);
|
|
57
60
|
}
|
|
58
61
|
|
|
62
|
+
export function isStructuredDataPath(filePath = '') {
|
|
63
|
+
const raw = String(filePath || '').trim();
|
|
64
|
+
if (!raw || isGeneratedOrMinifiedCodePath(raw)) return false;
|
|
65
|
+
return STRUCTURED_DATA_EXT_RE.test(raw);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function matchesNeverClampExt(filePath = '', neverClampExt = []) {
|
|
69
|
+
if (!Array.isArray(neverClampExt) || neverClampExt.length === 0) return false;
|
|
70
|
+
const ext = path.extname(String(filePath || '').trim()).toLowerCase();
|
|
71
|
+
if (!ext) return false;
|
|
72
|
+
for (const item of neverClampExt) {
|
|
73
|
+
const norm = String(item || '').trim().toLowerCase();
|
|
74
|
+
if (!norm) continue;
|
|
75
|
+
const dotted = norm.startsWith('.') ? norm : `.${norm}`;
|
|
76
|
+
if (dotted === ext) return true;
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
59
81
|
export function computeByteBoundedSliceWindow(
|
|
60
82
|
lines = [],
|
|
61
83
|
targetBytes = CLAMP_TARGET_BYTES,
|
|
@@ -209,6 +231,9 @@ export function evaluateShieldReadGuard({
|
|
|
209
231
|
shieldConfig?.neverClampCodeOffCliff !== undefined
|
|
210
232
|
? Boolean(shieldConfig.neverClampCodeOffCliff)
|
|
211
233
|
: rc.neverClampCodeOffCliff !== false;
|
|
234
|
+
const neverClampExt = Array.isArray(shieldConfig?.neverClampExt)
|
|
235
|
+
? shieldConfig.neverClampExt
|
|
236
|
+
: (Array.isArray(rc.neverClampExt) ? rc.neverClampExt : []);
|
|
212
237
|
const codeCeilingBytes = Math.max(
|
|
213
238
|
16_384,
|
|
214
239
|
Number(shieldConfig?.codeNeverClampCeilingBytes ?? NEVER_TRIM_CODE_HARD_CEILING_BYTES)
|
|
@@ -395,6 +420,32 @@ export function evaluateShieldReadGuard({
|
|
|
395
420
|
return { action: 'allow' };
|
|
396
421
|
}
|
|
397
422
|
|
|
423
|
+
if (!nearCliff && matchesNeverClampExt(absFile, neverClampExt)) {
|
|
424
|
+
return {
|
|
425
|
+
action: 'allow',
|
|
426
|
+
neverClampExtBypassed: true,
|
|
427
|
+
neverTrimReason: 'policy_never_clamp_ext',
|
|
428
|
+
fileBytes: stat.size,
|
|
429
|
+
sliceBytes: targetBytes
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (
|
|
434
|
+
!nearCliff &&
|
|
435
|
+
isStructuredDataPath(absFile) &&
|
|
436
|
+
stat.size < NEVER_TRIM_CODE_HARD_CEILING_BYTES &&
|
|
437
|
+
targetBytes < NEVER_TRIM_CODE_HARD_CEILING_BYTES
|
|
438
|
+
) {
|
|
439
|
+
return {
|
|
440
|
+
action: 'allow',
|
|
441
|
+
structuredDataNeverClampBypassed: true,
|
|
442
|
+
neverTrimReason: 'structured_data_under_128kb',
|
|
443
|
+
fileBytes: stat.size,
|
|
444
|
+
sliceBytes: targetBytes,
|
|
445
|
+
ceilingBytes: NEVER_TRIM_CODE_HARD_CEILING_BYTES
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
398
449
|
if (
|
|
399
450
|
neverClampCodeOffCliff &&
|
|
400
451
|
!nearCliff &&
|
|
@@ -520,14 +571,20 @@ export function evaluateShieldReadGuard({
|
|
|
520
571
|
};
|
|
521
572
|
}
|
|
522
573
|
|
|
523
|
-
export function getReadSnapFilePath(baseCwd, sessionId = 'default') {
|
|
574
|
+
export function getReadSnapFilePath(baseCwd, sessionId = 'default', options = {}) {
|
|
524
575
|
if (!baseCwd) return '';
|
|
525
|
-
const safeSid =
|
|
576
|
+
const safeSid =
|
|
577
|
+
typeof sessionId === 'string' && sessionId.trim()
|
|
578
|
+
? sessionId.trim().replace(/[^a-zA-Z0-9._-]/g, '_').slice(0, 64) || 'default'
|
|
579
|
+
: 'default';
|
|
580
|
+
if (options?.client === 'antigravity') {
|
|
581
|
+
return path.join(getWorkspaceStateDir(baseCwd, options.homeDir), `read_snapshots_${safeSid}.json`);
|
|
582
|
+
}
|
|
526
583
|
return path.join(path.resolve(String(baseCwd)), '.claude', 'cache', `read_snapshots_${safeSid}.json`);
|
|
527
584
|
}
|
|
528
585
|
|
|
529
|
-
export function loadReadSnapStore(baseCwd, sessionId = 'default') {
|
|
530
|
-
const snapFilePath = getReadSnapFilePath(baseCwd, sessionId);
|
|
586
|
+
export function loadReadSnapStore(baseCwd, sessionId = 'default', options = {}) {
|
|
587
|
+
const snapFilePath = getReadSnapFilePath(baseCwd, sessionId, options);
|
|
531
588
|
let store = {};
|
|
532
589
|
if (snapFilePath && fs.existsSync(snapFilePath)) {
|
|
533
590
|
try {
|
|
@@ -568,7 +625,7 @@ export function mergeLineIntervals(intervals = []) {
|
|
|
568
625
|
|
|
569
626
|
/**
|
|
570
627
|
* Checks whether `[reqStart, reqEnd]` overlaps already-in-context intervals and returns a
|
|
571
|
-
* contiguous unread trim
|
|
628
|
+
* contiguous unread trim when partial overlap is substantial.
|
|
572
629
|
*/
|
|
573
630
|
export function trimRequestedRangeAgainstIntervals(reqStart, reqEnd, rawIntervals = []) {
|
|
574
631
|
const start = Math.max(1, Math.floor(Number(reqStart) || 1));
|
|
@@ -601,14 +658,6 @@ export function trimRequestedRangeAgainstIntervals(reqStart, reqEnd, rawInterval
|
|
|
601
658
|
}
|
|
602
659
|
|
|
603
660
|
if (overlapLines === 0) return { status: 'none' };
|
|
604
|
-
if (overlapLines >= reqLen && reqLen >= 25) {
|
|
605
|
-
return {
|
|
606
|
-
status: 'full_covered',
|
|
607
|
-
anchorOffset: start,
|
|
608
|
-
anchorLimit: 5,
|
|
609
|
-
savedLines: reqLen - 5
|
|
610
|
-
};
|
|
611
|
-
}
|
|
612
661
|
if (
|
|
613
662
|
uncovered.length === 1 &&
|
|
614
663
|
overlapLines >= Math.ceil(reqLen * 0.5) &&
|
|
@@ -625,7 +674,7 @@ export function trimRequestedRangeAgainstIntervals(reqStart, reqEnd, rawInterval
|
|
|
625
674
|
return { status: 'none' };
|
|
626
675
|
}
|
|
627
676
|
|
|
628
|
-
export function recordReadIntervalEntry(store, normKey, statObj, startLine, endLine) {
|
|
677
|
+
export function recordReadIntervalEntry(store, normKey, statObj, startLine, endLine, nowMs = Date.now()) {
|
|
629
678
|
if (!store || !normKey || !statObj || endLine < startLine) return;
|
|
630
679
|
if (!store.__readIntervals || typeof store.__readIntervals !== 'object') {
|
|
631
680
|
store.__readIntervals = {};
|
|
@@ -636,9 +685,11 @@ export function recordReadIntervalEntry(store, normKey, statObj, startLine, endL
|
|
|
636
685
|
Number(prev.mtimeMs) === Number(statObj.mtimeMs) &&
|
|
637
686
|
Number(prev.size) === Number(statObj.size);
|
|
638
687
|
const existing = sameStat && Array.isArray(prev.intervals) ? prev.intervals : [];
|
|
688
|
+
const recordedAt = Number.isFinite(Number(nowMs)) ? Number(nowMs) : Date.now();
|
|
639
689
|
store.__readIntervals[normKey] = {
|
|
640
690
|
mtimeMs: Number(statObj.mtimeMs),
|
|
641
691
|
size: Number(statObj.size),
|
|
692
|
+
recordedAt,
|
|
642
693
|
intervals: mergeLineIntervals([...existing, [startLine, endLine]])
|
|
643
694
|
};
|
|
644
695
|
}
|
|
@@ -670,13 +721,18 @@ function writeReadSnapStoreUnlocked(
|
|
|
670
721
|
Number(prev.mtimeMs) === Number(v.mtimeMs) &&
|
|
671
722
|
Number(prev.size) === Number(v.size)
|
|
672
723
|
) {
|
|
724
|
+
const recordedAt =
|
|
725
|
+
Math.max(Number(prev.recordedAt || 0), Number(v.recordedAt || 0)) || Date.now();
|
|
673
726
|
nextIntervals[k] = {
|
|
674
727
|
mtimeMs: Number(v.mtimeMs),
|
|
675
728
|
size: Number(v.size),
|
|
729
|
+
recordedAt,
|
|
676
730
|
intervals: mergeLineIntervals([...(prev.intervals || []), ...(v.intervals || [])])
|
|
677
731
|
};
|
|
678
732
|
} else {
|
|
679
|
-
nextIntervals[k] = v
|
|
733
|
+
nextIntervals[k] = v && typeof v === 'object' && !v.recordedAt
|
|
734
|
+
? { ...v, recordedAt: Date.now() }
|
|
735
|
+
: v;
|
|
680
736
|
}
|
|
681
737
|
}
|
|
682
738
|
for (const delKey of clearIntervalKeys) {
|
|
@@ -705,11 +761,11 @@ export function saveReadSnapStore(snapFilePath, store, options = {}) {
|
|
|
705
761
|
}
|
|
706
762
|
}
|
|
707
763
|
|
|
708
|
-
export function updateReadSnapStoreAtomicSync(baseCwd, sessionId, mutatorFn) {
|
|
709
|
-
const snapFilePath = getReadSnapFilePath(baseCwd, sessionId);
|
|
764
|
+
export function updateReadSnapStoreAtomicSync(baseCwd, sessionId, mutatorFn, options = {}) {
|
|
765
|
+
const snapFilePath = getReadSnapFilePath(baseCwd, sessionId, options);
|
|
710
766
|
if (!snapFilePath || typeof mutatorFn !== 'function') return null;
|
|
711
767
|
return withFileLockSync(snapFilePath, () => {
|
|
712
|
-
const { store } = loadReadSnapStore(baseCwd, sessionId);
|
|
768
|
+
const { store } = loadReadSnapStore(baseCwd, sessionId, options);
|
|
713
769
|
const updated = mutatorFn(store) || store;
|
|
714
770
|
return writeReadSnapStoreUnlocked(snapFilePath, updated);
|
|
715
771
|
});
|
|
@@ -727,9 +783,13 @@ export default {
|
|
|
727
783
|
HUGE_FILE_HEAD_PROBE_BYTES,
|
|
728
784
|
MEASURED_REREAD_P_CODE,
|
|
729
785
|
MEASURED_REREAD_P_DOC,
|
|
786
|
+
DEFAULT_READ_INTERVAL_TTL_MS,
|
|
787
|
+
STRUCTURED_DATA_EXT_RE,
|
|
730
788
|
isBurnoffArtifactPath,
|
|
731
789
|
isGeneratedOrMinifiedCodePath,
|
|
732
790
|
isActiveSourceCodePath,
|
|
791
|
+
isStructuredDataPath,
|
|
792
|
+
matchesNeverClampExt,
|
|
733
793
|
computeByteBoundedSliceWindow,
|
|
734
794
|
buildCompactClampLandmarks,
|
|
735
795
|
evaluateClampExpectedValue,
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/guards/session-hook.mjs
|
|
3
|
+
* Session lifecycle handler for Claude Code `SessionStart` and `PreCompact` hooks.
|
|
4
|
+
* Invalidates stale per-session read snapshots (`read_snapshots_<sid>.json`) across compaction,
|
|
5
|
+
* resume, clear, and startup, and emits a 1-line last-session savings receipt on `SessionStart`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { getHomeDir } from '../config/paths.mjs';
|
|
11
|
+
import { classifyLedgerBucket, LEDGER_BUCKETS, readLedgerEntries } from '../storage/ledger.mjs';
|
|
12
|
+
import { getReadSnapFilePath } from './read-clamp.mjs';
|
|
13
|
+
|
|
14
|
+
function normalizeCwdKey(cwdPath = '') {
|
|
15
|
+
const raw = String(cwdPath || '').trim();
|
|
16
|
+
if (!raw) return '';
|
|
17
|
+
return path.resolve(raw).replace(/\\/g, '/').toLowerCase();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function formatTokenCount(tokens) {
|
|
21
|
+
const n = Math.max(0, Math.round(Number(tokens) || 0));
|
|
22
|
+
if (n >= 1000) {
|
|
23
|
+
return `~${(n / 1000).toFixed(1)}k tokens`;
|
|
24
|
+
}
|
|
25
|
+
return `~${n} tokens`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Deletes the cached read-interval snapshot file for `sessionId` so post-compaction,
|
|
30
|
+
* resumed, or cleared sessions start with a fresh view of loaded file intervals.
|
|
31
|
+
*/
|
|
32
|
+
export function invalidateReadSnapshots(cwd, sessionId = 'default', options = {}) {
|
|
33
|
+
if (!cwd) return { removed: false, path: null };
|
|
34
|
+
const sid = String(sessionId || 'default').trim() || 'default';
|
|
35
|
+
const snapPath = getReadSnapFilePath(cwd, sid, options);
|
|
36
|
+
if (!snapPath) return { removed: false, path: null };
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
if (fs.existsSync(snapPath)) {
|
|
40
|
+
fs.unlinkSync(snapPath);
|
|
41
|
+
return { removed: true, path: snapPath };
|
|
42
|
+
}
|
|
43
|
+
return { removed: false, path: snapPath };
|
|
44
|
+
} catch (err) {
|
|
45
|
+
if (err?.code === 'ENOENT') {
|
|
46
|
+
// ignore: snapshot file already absent
|
|
47
|
+
return { removed: false, path: snapPath };
|
|
48
|
+
}
|
|
49
|
+
void err; // ignore: fail-open snapshot invalidation
|
|
50
|
+
return { removed: false, path: snapPath };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Summarizes the most recent prior session's Burnoff guard activity from the ledger into a single
|
|
56
|
+
* concise status line for the `SessionStart` `systemMessage`, or `null` when nothing occurred.
|
|
57
|
+
*/
|
|
58
|
+
export function buildLastSessionReceipt(cwd, currentSessionId = '', options = {}) {
|
|
59
|
+
const homeDir = options.homeDir || getHomeDir(options);
|
|
60
|
+
const allEntries = readLedgerEntries({
|
|
61
|
+
...options,
|
|
62
|
+
homeDir
|
|
63
|
+
});
|
|
64
|
+
if (!Array.isArray(allEntries) || allEntries.length === 0) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const curSid = String(currentSessionId || '').trim();
|
|
69
|
+
const normCwd = cwd ? normalizeCwdKey(cwd) : '';
|
|
70
|
+
const sessions = new Map();
|
|
71
|
+
|
|
72
|
+
for (const entry of allEntries) {
|
|
73
|
+
if (!entry || typeof entry !== 'object') continue;
|
|
74
|
+
const sid = String(entry.sessionId || '').trim();
|
|
75
|
+
if (!sid || (curSid && sid === curSid)) continue;
|
|
76
|
+
if (normCwd && entry.cwd && normalizeCwdKey(entry.cwd) !== normCwd) continue;
|
|
77
|
+
|
|
78
|
+
let agg = sessions.get(sid);
|
|
79
|
+
if (!agg) {
|
|
80
|
+
agg = {
|
|
81
|
+
sessionId: sid,
|
|
82
|
+
latestTs: 0,
|
|
83
|
+
readsBounded: 0,
|
|
84
|
+
loopsStopped: 0,
|
|
85
|
+
guardsBlocked: 0,
|
|
86
|
+
tokensNotLoaded: 0
|
|
87
|
+
};
|
|
88
|
+
sessions.set(sid, agg);
|
|
89
|
+
}
|
|
90
|
+
const ts = Number(entry.timestamp || 0);
|
|
91
|
+
if (Number.isFinite(ts) && ts > agg.latestTs) {
|
|
92
|
+
agg.latestTs = ts;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const cat = String(entry.category || '').trim().toUpperCase();
|
|
96
|
+
const detectorId = String(entry.detectorId || '').trim().toUpperCase();
|
|
97
|
+
if (cat === 'BOUNDED_READ_ENFORCED' || cat === 'READ_CLAMPED') {
|
|
98
|
+
agg.readsBounded += 1;
|
|
99
|
+
} else if (
|
|
100
|
+
cat === 'LOOP_TURN_PREVENTED' ||
|
|
101
|
+
cat === 'DET_04_LOOP_BREAKER' ||
|
|
102
|
+
(cat === 'RING0_VIOLATION_BLOCKED' && detectorId === 'DET-04')
|
|
103
|
+
) {
|
|
104
|
+
agg.loopsStopped += 1;
|
|
105
|
+
} else if (
|
|
106
|
+
cat === 'RING0_VIOLATION_BLOCKED' ||
|
|
107
|
+
cat === 'SUBAGENT_FANOUT_BLOCKED' ||
|
|
108
|
+
cat === 'SCOPE_QUOTA_BRAKE'
|
|
109
|
+
) {
|
|
110
|
+
agg.guardsBlocked += 1;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (classifyLedgerBucket(entry) === LEDGER_BUCKETS.AVOIDED_WRITE) {
|
|
114
|
+
agg.tokensNotLoaded += Math.max(0, Math.round(Number(entry.savedTokens || 0)));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const candidates = [...sessions.values()]
|
|
119
|
+
.filter(
|
|
120
|
+
(s) =>
|
|
121
|
+
s.readsBounded > 0 ||
|
|
122
|
+
s.loopsStopped > 0 ||
|
|
123
|
+
s.guardsBlocked > 0 ||
|
|
124
|
+
s.tokensNotLoaded > 0
|
|
125
|
+
)
|
|
126
|
+
.sort((a, b) => b.latestTs - a.latestTs);
|
|
127
|
+
|
|
128
|
+
if (candidates.length === 0) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const latest = candidates[0];
|
|
133
|
+
const parts = [];
|
|
134
|
+
if (latest.readsBounded > 0) {
|
|
135
|
+
parts.push(`${latest.readsBounded} read${latest.readsBounded === 1 ? '' : 's'} bounded`);
|
|
136
|
+
}
|
|
137
|
+
if (latest.loopsStopped > 0) {
|
|
138
|
+
parts.push(`${latest.loopsStopped} loop${latest.loopsStopped === 1 ? '' : 's'} stopped`);
|
|
139
|
+
}
|
|
140
|
+
if (latest.guardsBlocked > 0) {
|
|
141
|
+
parts.push(`${latest.guardsBlocked} guard${latest.guardsBlocked === 1 ? '' : 's'} blocked`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const tokPart =
|
|
145
|
+
latest.tokensNotLoaded > 0
|
|
146
|
+
? `${formatTokenCount(latest.tokensNotLoaded)} kept out of context`
|
|
147
|
+
: '';
|
|
148
|
+
|
|
149
|
+
if (parts.length > 0 && tokPart) {
|
|
150
|
+
return `Burnoff: Last session — ${parts.join(', ')} (${tokPart})`;
|
|
151
|
+
}
|
|
152
|
+
if (parts.length > 0) {
|
|
153
|
+
return `Burnoff: Last session — ${parts.join(', ')}`;
|
|
154
|
+
}
|
|
155
|
+
if (tokPart) {
|
|
156
|
+
return `Burnoff: Last session — ${tokPart}`;
|
|
157
|
+
}
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Evaluates a `SessionStart` or `PreCompact` lifecycle hook payload.
|
|
163
|
+
*/
|
|
164
|
+
export function runSessionLifecycleHook(event = {}, options = {}) {
|
|
165
|
+
const hookEventName = String(
|
|
166
|
+
event.hook_event_name || event.hookEventName || options.hookEventName || ''
|
|
167
|
+
).trim();
|
|
168
|
+
const source = String(event.source || options.source || '').trim().toLowerCase();
|
|
169
|
+
const sessionId = String(event.session_id || event.sessionId || options.sessionId || 'default');
|
|
170
|
+
const cwd = path.resolve(String(event.cwd || options.cwd || process.cwd()));
|
|
171
|
+
|
|
172
|
+
const invalidated = invalidateReadSnapshots(cwd, sessionId, options);
|
|
173
|
+
const isCompactEvent = /^PreCompact$/i.test(hookEventName) || source === 'compact';
|
|
174
|
+
|
|
175
|
+
if (isCompactEvent) {
|
|
176
|
+
return {
|
|
177
|
+
exitCode: 0,
|
|
178
|
+
stdout: '',
|
|
179
|
+
stderr: '',
|
|
180
|
+
invalidated
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const receipt = buildLastSessionReceipt(cwd, sessionId, options);
|
|
185
|
+
if (!receipt) {
|
|
186
|
+
return {
|
|
187
|
+
exitCode: 0,
|
|
188
|
+
stdout: '',
|
|
189
|
+
stderr: '',
|
|
190
|
+
invalidated
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
exitCode: 0,
|
|
196
|
+
stdout: JSON.stringify({ systemMessage: receipt }),
|
|
197
|
+
stderr: '',
|
|
198
|
+
systemMessage: receipt,
|
|
199
|
+
invalidated
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export default {
|
|
204
|
+
invalidateReadSnapshots,
|
|
205
|
+
buildLastSessionReceipt,
|
|
206
|
+
runSessionLifecycleHook
|
|
207
|
+
};
|