burnoff 1.3.0 → 1.4.1

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.
@@ -17,6 +17,9 @@ export const ALLOWED_SNOOZE_DETECTORS = Object.freeze([
17
17
  'DET-02',
18
18
  'DET-03',
19
19
  'DET-04',
20
+ 'DET-07',
21
+ 'DET-08',
22
+ 'DET-09',
20
23
  '*'
21
24
  ]);
22
25
 
@@ -34,14 +37,14 @@ function readJsonSafe(filePath, fallback = null) {
34
37
  }
35
38
 
36
39
  /**
37
- * Normalizes a detector identifier for ask snooze grants (`DET-01`..`DET-04` or `*`).
40
+ * Normalizes a detector identifier for ask snooze grants (`DET-01`..`DET-04`, `DET-07`..`DET-09`, or `*`).
38
41
  * Returns `null` for unsupported or invalid IDs (such as `DET-20` or empty non-default input).
39
42
  */
40
43
  export function normalizeSnoozeDetectorId(raw = '*') {
41
44
  if (raw === undefined || raw === null || raw === '') return '*';
42
45
  const s = String(raw).trim().toUpperCase();
43
46
  if (!s || s === '*' || s === 'ALL') return '*';
44
- if (/^DET-0[1-4]$/.test(s)) return s;
47
+ if (/^DET-0[1-47-9]$/.test(s)) return s;
45
48
  return null;
46
49
  }
47
50
 
@@ -0,0 +1,175 @@
1
+ /**
2
+ * @file src/guards/structural-boundaries.mjs
3
+ * Pure structural boundary detector for >=128 KB bounded read clamping (BUG-16).
4
+ * Zero Node built-ins; safe in any ESM runtime.
5
+ */
6
+
7
+ export const UNSTRUCTURED_FALLBACK_LINE_THRESHOLD = 400;
8
+ export const UNSTRUCTURED_SPAN_SIZE = 150;
9
+ export const MIN_STRUCTURAL_BOUNDARIES = 3;
10
+
11
+ const MD_HEADING_RE = /^(#{1,4})\s+(.+?)\s*#*$/;
12
+ const JS_TS_BOUNDARY_RE =
13
+ /^(?:export\s+(?:default\s+)?)?(?:async\s+)?(?:function\*?\s+([A-Za-z_$][\w$]*)|class\s+([A-Za-z_$][\w$]*)|interface\s+([A-Za-z_$][\w$]*)|type\s+([A-Za-z_$][\w$]*)\s*=|(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*(?:async\s*)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>)/;
14
+ const PY_BOUNDARY_RE = /^(?:async\s+)?def\s+([A-Za-z_]\w*)|^class\s+([A-Za-z_]\w*)/;
15
+ const GO_BOUNDARY_RE = /^func\s+(?:\([^)]+\)\s*)?([A-Za-z_]\w*)/;
16
+ const RUST_BOUNDARY_RE = /^(?:pub(?:\([^)]+\))?\s+)?(?:async\s+)?(?:fn|struct|enum|trait|impl)\s+([A-Za-z_]\w*)/;
17
+ const JSON_TOP_KEY_RE = /^ {0,2}"([^"\\]{1,120})"\s*:\s*[\[{]/;
18
+ const YAML_TOP_KEY_RE = /^([A-Za-z0-9_.-]{1,120})\s*:(?:\s|$)/;
19
+ const FENCE_RE = /^ {0,3}(`{3,}|~{3,})(.*)$/;
20
+
21
+ /**
22
+ * Matches a structural boundary line (heading, symbol declaration, top-level JSON/YAML key).
23
+ *
24
+ * @param {string} line
25
+ * @param {boolean} [isMarkdownLike=false]
26
+ * @returns {string | null}
27
+ */
28
+ export function matchStructuralBoundary(line, isMarkdownLike = false) {
29
+ const raw = typeof line === 'string' ? line : String(line || '');
30
+ if (!raw || !raw.trim()) return null;
31
+
32
+ if (isMarkdownLike) {
33
+ const md = raw.trimEnd().match(MD_HEADING_RE);
34
+ if (md) return `${md[1]} ${md[2].trim()}`.slice(0, 120);
35
+ }
36
+
37
+ const trimmedRight = raw.trimEnd();
38
+
39
+ if (!/^\s/.test(raw)) {
40
+ const js = trimmedRight.match(JS_TS_BOUNDARY_RE);
41
+ if (js) {
42
+ const name = js[1] || js[2] || js[3] || js[4] || js[5];
43
+ return name ? `sym:${name}` : trimmedRight.slice(0, 80);
44
+ }
45
+ const py = trimmedRight.match(PY_BOUNDARY_RE);
46
+ if (py) {
47
+ const name = py[1] || py[2];
48
+ return name ? `sym:${name}` : trimmedRight.slice(0, 80);
49
+ }
50
+ const go = trimmedRight.match(GO_BOUNDARY_RE);
51
+ if (go) return `func:${go[1]}`;
52
+ const rs = trimmedRight.match(RUST_BOUNDARY_RE);
53
+ if (rs) return `rs:${rs[1]}`;
54
+ }
55
+
56
+ const jsonKey = trimmedRight.match(JSON_TOP_KEY_RE);
57
+ if (jsonKey) {
58
+ return `json:${jsonKey[1]}`;
59
+ }
60
+
61
+ if (!raw.startsWith('#') && !raw.startsWith('-')) {
62
+ const yamlKey = trimmedRight.match(YAML_TOP_KEY_RE);
63
+ if (yamlKey) {
64
+ return `yaml:${yamlKey[1]}`;
65
+ }
66
+ }
67
+
68
+ return null;
69
+ }
70
+
71
+ /**
72
+ * Scans lines for structural boundaries while ignoring lines inside fenced code blocks.
73
+ *
74
+ * @param {string[]} [lines=[]]
75
+ * @param {{ filePath?: string }} [options={}]
76
+ * @returns {{ boundaries: Array<{ lineNum: number, label: string }>, totalLines: number, usedFallback: boolean }}
77
+ */
78
+ export function detectStructuralBoundaries(lines = [], { filePath = '' } = {}) {
79
+ const list = Array.isArray(lines) ? lines : [];
80
+ const isMarkdownLike = /\.(?:md|mdx|markdown)$/i.test(String(filePath || ''));
81
+ const boundaries = [];
82
+
83
+ let activeFenceChar = '';
84
+ let activeFenceLen = 0;
85
+
86
+ for (let i = 0; i < list.length; i += 1) {
87
+ const line = String(list[i] ?? '');
88
+ const fenceMatch = line.match(FENCE_RE);
89
+ if (fenceMatch) {
90
+ const marker = fenceMatch[1];
91
+ const markerChar = marker[0];
92
+ const rest = fenceMatch[2] || '';
93
+ if (!activeFenceChar) {
94
+ activeFenceChar = markerChar;
95
+ activeFenceLen = marker.length;
96
+ continue;
97
+ }
98
+ if (markerChar === activeFenceChar && marker.length >= activeFenceLen && /^\s*$/.test(rest)) {
99
+ activeFenceChar = '';
100
+ activeFenceLen = 0;
101
+ continue;
102
+ }
103
+ }
104
+
105
+ if (activeFenceChar) continue;
106
+
107
+ const label = matchStructuralBoundary(line, isMarkdownLike);
108
+ if (label) {
109
+ boundaries.push({ lineNum: i + 1, label });
110
+ }
111
+ }
112
+
113
+ const totalLines = list.length;
114
+ const usedFallback =
115
+ boundaries.length < MIN_STRUCTURAL_BOUNDARIES && totalLines > UNSTRUCTURED_FALLBACK_LINE_THRESHOLD;
116
+
117
+ return { boundaries, totalLines, usedFallback };
118
+ }
119
+
120
+ /**
121
+ * Selects a clamp end line within `[minLimit, maxLimit]` snapped cleanly before a structural boundary,
122
+ * or falling back to a fixed 150-line span for large unstructured files (>400 lines, <3 boundaries).
123
+ *
124
+ * @param {string[]} [lines=[]]
125
+ * @param {{ filePath?: string, maxLimit?: number, minLimit?: number }} [options={}]
126
+ * @returns {{ endLine: number, snapped: boolean, usedFallback: boolean, boundaryCount: number }}
127
+ */
128
+ export function chooseStructuralClampEndLine(
129
+ lines = [],
130
+ { filePath = '', maxLimit = 250, minLimit = 15 } = {}
131
+ ) {
132
+ const list = Array.isArray(lines) ? lines : [];
133
+ const floorMin = Math.max(1, Number(minLimit) || 15);
134
+ const rawMax = Math.max(1, Math.min(Number(maxLimit) || 250, list.length || Number(maxLimit) || 250));
135
+ const effectiveMax = Math.max(Math.min(floorMin, rawMax), rawMax);
136
+
137
+ const { boundaries, usedFallback } = detectStructuralBoundaries(list, { filePath });
138
+
139
+ if (usedFallback) {
140
+ const endLine = Math.max(Math.min(floorMin, effectiveMax), Math.min(effectiveMax, UNSTRUCTURED_SPAN_SIZE));
141
+ return {
142
+ endLine,
143
+ snapped: true,
144
+ usedFallback: true,
145
+ boundaryCount: boundaries.length
146
+ };
147
+ }
148
+
149
+ if (boundaries.length >= 1) {
150
+ let candidateBoundary = null;
151
+ for (let i = boundaries.length - 1; i >= 0; i -= 1) {
152
+ const b = boundaries[i];
153
+ if (b.lineNum > floorMin && b.lineNum <= effectiveMax + 1) {
154
+ candidateBoundary = b;
155
+ break;
156
+ }
157
+ }
158
+ if (candidateBoundary) {
159
+ const endLine = candidateBoundary.lineNum - 1;
160
+ return {
161
+ endLine,
162
+ snapped: endLine !== effectiveMax,
163
+ usedFallback: false,
164
+ boundaryCount: boundaries.length
165
+ };
166
+ }
167
+ }
168
+
169
+ return {
170
+ endLine: effectiveMax,
171
+ snapped: false,
172
+ usedFallback: false,
173
+ boundaryCount: boundaries.length
174
+ };
175
+ }