sprag-cli 3.131.0 → 3.132.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/src/installer.js CHANGED
@@ -573,6 +573,48 @@ export function installSessionStartHook() {
573
573
  return { path: file, action: 'created' };
574
574
  }
575
575
 
576
+ /**
577
+ * SessionStart hook for source "compact". The context warning sprag injects at
578
+ * 80% and 95% can reach the user after the compaction it warned about has run;
579
+ * this hook tells the model that the earlier warning is out of date.
580
+ *
581
+ * Its own group with matcher `compact`: the `startup|clear` group may hold hooks
582
+ * that are not ours, and widening its matcher would change when those run.
583
+ */
584
+ const ROUTE_SCAN_COMPACT_HOOK_COMMAND = `${CLI} route-scan --hook-compact`;
585
+
586
+ export function installCompactNoticeHook() {
587
+ const dir = claudeUserDir();
588
+ const file = join(dir, 'settings.json');
589
+ mkdirSync(dir, { recursive: true });
590
+
591
+ const read = readSettings(file);
592
+ if (read.state === 'unusable') return { path: file, action: 'skipped', reason: read.reason };
593
+ const settings = read.settings;
594
+
595
+ const hooksProblem = ensureHooksObject(settings);
596
+ if (hooksProblem) return { path: file, action: 'skipped', reason: hooksProblem };
597
+ if (settings.hooks.SessionStart !== undefined && !Array.isArray(settings.hooks.SessionStart)) {
598
+ return { path: file, action: 'skipped', reason: 'hooks.SessionStart is not an array — fix settings.json manually' };
599
+ }
600
+ const list = Array.isArray(settings.hooks.SessionStart) ? settings.hooks.SessionStart : [];
601
+ const already = list.some((m) =>
602
+ Array.isArray(m?.hooks) && m.hooks.some((h) => isOurSubcommand(h?.command, 'route-scan', '--hook-compact')),
603
+ );
604
+ if (already) return { path: file, action: 'exists' };
605
+
606
+ list.push({
607
+ matcher: 'compact',
608
+ hooks: [{ type: 'command', command: ROUTE_SCAN_COMPACT_HOOK_COMMAND, timeout: 10 }],
609
+ });
610
+ settings.hooks.SessionStart = list;
611
+ {
612
+ const writeProblem = writeSettings(file, settings);
613
+ if (writeProblem) return { path: file, action: 'skipped', reason: writeProblem };
614
+ }
615
+ return { path: file, action: 'created' };
616
+ }
617
+
576
618
  // Registers the UserPromptSubmit hook that briefs mid-session state changes
577
619
  // (ctx tier crossings, new route candidates, rule-health flips) into the
578
620
  // conversation — the model cannot see statusline chips, so without this a
@@ -1474,6 +1516,7 @@ export function installAll({ force = false } = {}) {
1474
1516
  skill: installSkill({ force }),
1475
1517
  statusline: installStatusline({ force }),
1476
1518
  sessionStartHook: installSessionStartHook(),
1519
+ compactNoticeHook: installCompactNoticeHook(),
1477
1520
  delegationHook: installDelegationHook(),
1478
1521
  delegationMeterMod: installDelegationMeterMod(),
1479
1522
  statuslineMod: installStatuslineMod(),
@@ -155,6 +155,25 @@ export function putCached(cache, file, session) {
155
155
  };
156
156
  }
157
157
 
158
+ /**
159
+ * The last summary stored for a transcript path, whatever the file looks like
160
+ * now. It skips the (mtimeMs, size) check that getCached does, so it can be a
161
+ * few seconds behind a transcript that is still growing: that is fine for a
162
+ * display, and it must never feed an aggregate.
163
+ *
164
+ * @returns {SessionSummary|null} null when the path has no entry
165
+ */
166
+ export function peekCached(cache, path, projectDir = '') {
167
+ const e = cache?.entries?.[path];
168
+ if (!e || !e.s) return null;
169
+ try {
170
+ return deserialize(e.s, path, projectDir);
171
+ } catch (err) {
172
+ debug('session-cache:peek', err);
173
+ return null;
174
+ }
175
+ }
176
+
158
177
  /**
159
178
  * A session re-aggregated from a fixed point in time (the start of the month),
160
179
  * stored beside the full summary of the same file. One slot per entry: the
@@ -117,6 +117,23 @@ export function readLastRender({ dir = userDataDir(), key = 'default' } = {}) {
117
117
  }
118
118
  }
119
119
 
120
+ /**
121
+ * Epoch ms of the newest statusline render of any session, or null. A render
122
+ * rewrites its own entry every refresh, so a stale value means no statusline
123
+ * is running.
124
+ */
125
+ export function newestRenderAt({ dir = userDataDir() } = {}) {
126
+ try {
127
+ let best = null;
128
+ for (const v of Object.values(readLastMap(dir))) {
129
+ if (v && Number.isFinite(v.at) && (best === null || v.at > best)) best = v.at;
130
+ }
131
+ return best;
132
+ } catch {
133
+ return null;
134
+ }
135
+ }
136
+
120
137
  export function writeLastRender({
121
138
  dir = userDataDir(), key = 'default', text, now = Date.now(), maxAgeMs = DAY_MS,
122
139
  } = {}) {
@@ -78,3 +78,14 @@ export function labelForKey(key) {
78
78
  const short = deriveShort(key);
79
79
  return { short, long: deriveLong(key), icon: '⏱', narrowIcon: '◕', usageLabel: short };
80
80
  }
81
+
82
+ /**
83
+ * Label for a Codex rate-limit window from its length in minutes: 300 is `5H`,
84
+ * 10080 is `7D`, 90 is `90m`. Without a usable length it falls back to `fallback`.
85
+ */
86
+ export function windowLabel(minutes, fallback) {
87
+ if (!Number.isFinite(minutes) || minutes <= 0) return fallback;
88
+ if (minutes % 1440 === 0) return `${minutes / 1440}D`;
89
+ if (minutes % 60 === 0) return `${minutes / 60}H`;
90
+ return `${minutes}m`;
91
+ }