sprag-cli 3.138.1 → 3.139.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/bin/cli.js CHANGED
@@ -38,6 +38,7 @@ import {
38
38
  extractContextUsage,
39
39
  extractEffort,
40
40
  extractModel,
41
+ extractPromptCache,
41
42
  extractSessionId,
42
43
  extractTranscriptPath,
43
44
  } from '../src/stdin-payload.js';
@@ -1230,6 +1231,10 @@ async function main() {
1230
1231
  ? readStarChip({ savedUsd: delegationSaved + (Number(doc2mdTotals?.total) || 0) })
1231
1232
  : null,
1232
1233
  lastActivity,
1234
+ // Claude Code's own cache figures for this session (2.1.333+, and the
1235
+ // desktop mod); when present the TTL chip counts down to them instead of
1236
+ // estimating from the last activity across transcripts.
1237
+ promptCache: isStatusline ? extractPromptCache(stdinJson) : null,
1233
1238
  monthSpend: monthSpendInfo,
1234
1239
  modelShare,
1235
1240
  spikeReport,
@@ -63,7 +63,8 @@ export function statuslineArgv(settings: Readonly<Record<string, unknown>>): str
63
63
  * The JSON Claude Code's statusline would have been fed. Fields the engine has
64
64
  * no value for are left out, never zeroed: sprag reads an absent rate_limits as
65
65
  * "no data" but a 0 as "0% used". transcript_path is left out because the mod
66
- * cannot know it, and sprag takes it only for the effort chip.
66
+ * cannot know it, and sprag takes it only for the effort chip. prompt_cache
67
+ * lets the cache chip count down to the engine's own expiry time.
67
68
  */
68
69
  export function statuslinePayload(input: {
69
70
  sessionId: string
@@ -96,9 +97,40 @@ export function statuslinePayload(input: {
96
97
  payload.rate_limits = limits
97
98
  }
98
99
  if (usage.cost) payload.cost = { total_cost_usd: usage.cost.usd }
100
+ const cache = promptCacheOf(usage)
101
+ if (cache) payload.prompt_cache = cache
99
102
  return payload
100
103
  }
101
104
 
105
+ /**
106
+ * The engine's prompt cache figures (2.1.333+) as the terminal statusline gets
107
+ * them in `prompt_cache`: epoch seconds for the expiry, null when nothing is
108
+ * cached. Read through a local type so the module still type-checks against
109
+ * engines whose SessionUsage has no promptCache; there it is simply absent and
110
+ * sprag keeps estimating the countdown from the transcripts.
111
+ */
112
+ type EnginePromptCache = {
113
+ isWarm?: unknown
114
+ expiresAt?: unknown
115
+ ttl?: unknown
116
+ lastMissCauses?: unknown
117
+ recacheTokens?: unknown
118
+ }
119
+
120
+ export function promptCacheOf(usage: SessionUsage): Record<string, unknown> | undefined {
121
+ const pc = (usage as { promptCache?: EnginePromptCache }).promptCache
122
+ if (!pc || (pc.ttl !== '5m' && pc.ttl !== '1h')) return undefined
123
+ const at = typeof pc.expiresAt === 'number' && Number.isFinite(pc.expiresAt) ? Math.floor(pc.expiresAt / 1000) : null
124
+ const causes = Array.isArray(pc.lastMissCauses) ? pc.lastMissCauses.filter(c => typeof c === 'string') : undefined
125
+ return {
126
+ warm: pc.isWarm === true,
127
+ ttl: pc.ttl,
128
+ expires_at: at,
129
+ ...(causes?.length ? { last_miss_cause: { causes } } : {}),
130
+ ...(typeof pc.recacheTokens === 'number' ? { recache_tokens_if_cold: pc.recacheTokens } : {}),
131
+ }
132
+ }
133
+
102
134
  /**
103
135
  * The argv with `--columns <n>` added, so sprag fits its chips to the band and
104
136
  * wraps them at chip boundaries instead of the band wrapping them mid-chip. A
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sprag-cli",
3
- "version": "3.138.1",
3
+ "version": "3.139.0",
4
4
  "description": "The delegation layer for Claude Code and Codex: each task goes to the model and effort that fits it, down to cheaper models for lookups and up to higher effort when a cheaper run would miss, backed by public replay cases, with a live statusline and team rules for Pro and Enterprise.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -993,7 +993,15 @@ function buildPeriodSeg(options, c, verbose) {
993
993
  * Which cache bucket the session is in, and the countdown length that follows
994
994
  * from it: `{ is1h, bucketLabel, bucketColor, ttlSeconds }`.
995
995
  */
996
- function resolveTtlBucket(ttl, ttlBucket) {
996
+ function resolveTtlBucket(ttl, ttlBucket, promptCache) {
997
+ // Claude Code's own report of the bucket the last request wrote is a
998
+ // measurement of this very session, so it settles what the transcript split
999
+ // and the gateway guess below can only estimate. A pinned bucket still wins:
1000
+ // it exists for providers whose reports cannot be trusted.
1001
+ if (promptCache && (promptCache.ttl === '5m' || promptCache.ttl === '1h') && ttlBucket !== '5m' && ttlBucket !== '1h') {
1002
+ const is1h = promptCache.ttl === '1h';
1003
+ return { is1h, bucketLabel: promptCache.ttl, bucketColor: is1h ? GREEN : YELLOW, ttlSeconds: is1h ? 3600 : 300 };
1004
+ }
997
1005
  // TTL dominance → color signal (1h = good, 5m = warning).
998
1006
  // The subscription plan fixes this, so the bucket rarely changes — it's the countdown that matters.
999
1007
  // When ttl.total === 0 (no cache writes observed in the window), we cannot
@@ -1029,8 +1037,16 @@ function resolveTtlBucket(ttl, ttlBucket) {
1029
1037
  * Cache TTL chip: the countdown on the last call's cache entry, or just the
1030
1038
  * bucket when the timer is off or there is no last call.
1031
1039
  */
1032
- function buildTtlSeg({ ttl, ttlBucket, lastActivity }, timer, c, isIcon, verbose, g) {
1033
- const { is1h, bucketLabel, bucketColor, ttlSeconds } = resolveTtlBucket(ttl, ttlBucket);
1040
+ function buildTtlSeg({ ttl, ttlBucket, lastActivity, promptCache }, timer, c, isIcon, verbose, g) {
1041
+ const { is1h, bucketLabel, bucketColor, ttlSeconds } = resolveTtlBucket(ttl, ttlBucket, promptCache);
1042
+ // With Claude Code's figures in hand the countdown runs to its expiry time.
1043
+ // A null expiry there means the last response cached nothing, so there is no
1044
+ // clock to show, only the bucket; estimating one from other sessions'
1045
+ // activity would count down a cache this session does not have.
1046
+ const reported = promptCache && (promptCache.ttl === '5m' || promptCache.ttl === '1h');
1047
+ if (reported) {
1048
+ lastActivity = Number.isFinite(promptCache.expiresAtMs) ? promptCache.expiresAtMs - ttlSeconds * 1000 : null;
1049
+ }
1034
1050
 
1035
1051
  // TTL countdown — how much time is left on the last API call's cache entry.
1036
1052
  // Matches Anthropic's actual prompt-cache behaviour: each call starts a fresh
@@ -1907,7 +1923,7 @@ export function formatReport(data, { color = true, verbose: verboseOpt = false,
1907
1923
  usage: buildUsageSegs(caps, capWarn, c, isIcon, verbose, g, color, mode),
1908
1924
  ctx: buildCtxSeg(ctxLive, contextWindow, c, isIcon, verbose, g),
1909
1925
  compactions: buildCompactionsSeg(data.compactions, c, isIcon, verbose, g),
1910
- ttl: buildTtlSeg({ ttl, ttlBucket: data.ttlBucket, lastActivity }, timer, c, isIcon, verbose, g),
1926
+ ttl: buildTtlSeg({ ttl, ttlBucket: data.ttlBucket, lastActivity, promptCache: data.promptCache }, timer, c, isIcon, verbose, g),
1911
1927
  // Model, then the effort that qualifies it: the same model answers
1912
1928
  // differently at `low` and at `max`, and the pair is one glance.
1913
1929
  model: buildModelSeg(model, c, isIcon, g),
@@ -172,6 +172,28 @@ export function extractContextUsage(stdinJson) {
172
172
  };
173
173
  }
174
174
 
175
+ /**
176
+ * The main conversation's prompt cache as Claude Code 2.1.333+ reports it
177
+ * (`prompt_cache`), the desktop mod passing `$.session.usage().promptCache` in
178
+ * the same shape. Subset used:
179
+ * "prompt_cache": { "warm": true, "ttl": "1h", "expires_at": 1764550000 }
180
+ * `expires_at` is epoch seconds, or null when the last response cached
181
+ * nothing. Returns `{ ttl, expiresAtMs }` with `expiresAtMs` null in that case,
182
+ * or null when the key is absent or its ttl is not one of the two buckets: an
183
+ * older Claude Code, or a payload this reader does not understand, keeps the
184
+ * transcript-based estimate.
185
+ */
186
+ export function extractPromptCache(stdinJson) {
187
+ const pc = stdinJson && stdinJson.prompt_cache;
188
+ if (!pc || typeof pc !== 'object') return null;
189
+ if (pc.ttl !== '5m' && pc.ttl !== '1h') return null;
190
+ const at = typeof pc.expires_at === 'number' ? pc.expires_at : NaN;
191
+ return {
192
+ ttl: pc.ttl,
193
+ expiresAtMs: Number.isFinite(at) && at > 0 ? (at < 1e12 ? at * 1000 : at) : null,
194
+ };
195
+ }
196
+
175
197
  /** The session the payload belongs to, or null when the field is absent. */
176
198
  export function extractSessionId(stdinJson) {
177
199
  const id = stdinJson?.session_id;