context-doctor 0.38.0 → 0.40.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 (65) hide show
  1. package/README.md +28 -19
  2. package/dist/antigravity.d.ts +44 -0
  3. package/dist/antigravity.js +80 -0
  4. package/dist/autoclear.d.ts +15 -0
  5. package/dist/autoclear.js +17 -0
  6. package/dist/autopilot.js +3 -2
  7. package/dist/cachemisses.d.ts +56 -2
  8. package/dist/cachemisses.js +252 -42
  9. package/dist/cachettl.d.ts +127 -0
  10. package/dist/cachettl.js +357 -0
  11. package/dist/claudedir.d.ts +17 -0
  12. package/dist/claudedir.js +32 -0
  13. package/dist/cli.js +303 -26
  14. package/dist/coldresume.d.ts +15 -0
  15. package/dist/coldresume.js +60 -11
  16. package/dist/compactquality.d.ts +5 -0
  17. package/dist/compactquality.js +108 -61
  18. package/dist/compactwindow.d.ts +14 -5
  19. package/dist/compactwindow.js +36 -43
  20. package/dist/doctor.d.ts +14 -0
  21. package/dist/doctor.js +94 -8
  22. package/dist/effective.d.ts +55 -0
  23. package/dist/effective.js +163 -0
  24. package/dist/experiment.js +2 -2
  25. package/dist/extract.js +4 -1
  26. package/dist/fanout.js +2 -2
  27. package/dist/fix.d.ts +176 -0
  28. package/dist/fix.js +574 -0
  29. package/dist/hook.js +2 -2
  30. package/dist/install.d.ts +27 -1
  31. package/dist/install.js +248 -37
  32. package/dist/ledger.js +2 -2
  33. package/dist/lens.d.ts +2 -0
  34. package/dist/lens.js +50 -24
  35. package/dist/limits.d.ts +10 -0
  36. package/dist/limits.js +36 -12
  37. package/dist/mcp.js +61 -61
  38. package/dist/mcpclient.js +28 -28
  39. package/dist/mcpschema.d.ts +18 -0
  40. package/dist/mcpschema.js +74 -31
  41. package/dist/missledger.d.ts +61 -0
  42. package/dist/missledger.js +198 -0
  43. package/dist/overhead.js +22 -25
  44. package/dist/profile.js +8 -8
  45. package/dist/proxy.d.ts +1 -1
  46. package/dist/proxy.js +2 -1
  47. package/dist/prune.d.ts +4 -0
  48. package/dist/prune.js +89 -49
  49. package/dist/reads.js +2 -2
  50. package/dist/recall.js +2 -1
  51. package/dist/savings.d.ts +5 -9
  52. package/dist/savings.js +217 -115
  53. package/dist/scan.d.ts +96 -0
  54. package/dist/scan.js +482 -0
  55. package/dist/session.d.ts +6 -0
  56. package/dist/session.js +16 -3
  57. package/dist/settingsedit.d.ts +72 -0
  58. package/dist/settingsedit.js +493 -0
  59. package/dist/statusline.d.ts +38 -2
  60. package/dist/statusline.js +66 -4
  61. package/dist/subagentpay.js +2 -2
  62. package/dist/tasks.js +2 -2
  63. package/dist/tokens.d.ts +5 -0
  64. package/dist/tokens.js +11 -1
  65. package/package.json +2 -2
@@ -0,0 +1,357 @@
1
+ /**
2
+ * `context-doctor cache-ttl`: how long Claude Code's prompt cache should
3
+ * live, measured on your own pauses.
4
+ *
5
+ * Every request re-sends the whole conversation; the part the previous
6
+ * request cached is read at 5-10% of the input price, if the cache is still
7
+ * alive. Claude Code writes the cache with a lifetime of 5 minutes (1.25x
8
+ * input per written token) or 1 hour (2x). A pause longer than the lifetime
9
+ * makes the next request write the whole context again. Claude Code 2.1.243+
10
+ * lets the user choose (code.claude.com/docs/en/env-vars):
11
+ * - `promptCacheTtl` ("5m" | "1h") for the main conversation; unset it is
12
+ * 1 hour on a Claude subscription within its usage limits and 5 minutes on
13
+ * an API key, Bedrock, Vertex or Foundry (the setting's description in the
14
+ * 2.1.309 binary); interactive sessions wrote 5-minute entries for a while
15
+ * from 2.1.301 (anthropics/claude-code#103673);
16
+ * - `subagentPromptCacheTtl` for subagents, workflows and background work;
17
+ * unset, 5 minutes (unless ENABLE_PROMPT_CACHING_1H=1).
18
+ * The environment variables CLAUDE_CODE_PROMPT_CACHE_TTL and
19
+ * CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL take precedence, and
20
+ * FORCE_PROMPT_CACHING_5M overrides them all. An older Claude Code ignores
21
+ * the keys (2.1.62 still ran the file's hooks with them present; measured),
22
+ * so writing them is safe everywhere and takes effect where supported.
23
+ *
24
+ * The lifetime changes only the price: the same content is sent either way,
25
+ * so there is no quality question. The replay, request by request over each
26
+ * transcript, prices the same requests three ways: as billed, every write at
27
+ * 5 minutes, every write at 1 hour. A request that found the cache cold (its
28
+ * gap from the previous request past that entry's lifetime, and over 60% of
29
+ * the previous context written again) would have read it at a lifetime that
30
+ * covered the gap; a request that read a warm cache would have written it
31
+ * all again at a lifetime its gap exceeded. Compactions and model switches
32
+ * start a new cache in every arm. The main conversation and subagents are
33
+ * replayed apart, since each has its own setting.
34
+ */
35
+ import { existsSync, readdirSync, statSync } from "node:fs";
36
+ import { homedir } from "node:os";
37
+ import { join } from "node:path";
38
+ import { listSessions, replyCounter, isClaudeSession } from "./session.js";
39
+ import { REQUEST_ROWS } from "./coldresume.js";
40
+ import { rowsOf } from "./scan.js";
41
+ import { CACHE_WRITE_1H, CACHE_WRITE_5M, cacheWriteSplit, pricingFor, usageCost } from "./pricing.js";
42
+ import { realModel } from "./tokens.js";
43
+ import { backupOnce } from "./install.js";
44
+ import { ABSENT, readSettings, setJsonPath, writeAtomic } from "./settingsedit.js";
45
+ import { effectiveTtl, settingsSources, ttlOverrides } from "./effective.js";
46
+ import { compareVersions, describeInstalls } from "./modelswitch.js";
47
+ import { claudeDir } from "./claudedir.js";
48
+ /** The first Claude Code with the promptCacheTtl and subagentPromptCacheTtl settings (CHANGELOG). */
49
+ export const CACHE_TTL_SINCE = "2.1.243";
50
+ const FIVE_MIN = 300_000;
51
+ const HOUR = 3_600_000;
52
+ /** Over this share of the previous context written again: the cache was cold. */
53
+ const REWRITE_SHARE = 0.6;
54
+ /** A recommendation needs at least this saving, absolute and as a share of the billed input. */
55
+ const MIN_SAVING_USD = 1;
56
+ const MIN_SAVING_SHARE = 0.02;
57
+ const empty = () => ({ transcripts: 0, requests: 0, billedUsd: 0, at5mUsd: 0, at1hUsd: 0, oneHourShare: 0, keptWarmAt1h: 0, wentColdAt5m: 0, unpriced: 0 });
58
+ const num = (v) => (typeof v === "number" && Number.isFinite(v) && v > 0 ? v : 0);
59
+ /**
60
+ * Replay one transcript into `acc`, counting requests in [since, until).
61
+ * `sidechains`: a subagent's own transcript, whose rows are all sidechain
62
+ * rows; a main transcript skips them. Returns the requests counted.
63
+ */
64
+ export function replayTtlTranscript(path, acc, since, until = Infinity, sidechains = false, runRows, otherIds) {
65
+ let prev;
66
+ let lastTtl = FIVE_MIN;
67
+ let compacted = false;
68
+ let counted = 0;
69
+ const sighting = replyCounter(runRows);
70
+ const mine = new Set();
71
+ rowsOf(path, REQUEST_ROWS).forEach((e) => {
72
+ if (!e || typeof e !== "object" || (!sidechains && e.isSidechain))
73
+ return;
74
+ if (e.type === "system" && e.subtype === "compact_boundary") {
75
+ compacted = true;
76
+ return;
77
+ }
78
+ const msg = e.message;
79
+ if (e.type !== "assistant" || !msg?.usage)
80
+ return;
81
+ let seen = sighting(e);
82
+ if (seen === "repeat")
83
+ return;
84
+ // The same reply in another transcript (a subagent's file copied or resumed): billed once.
85
+ const id = typeof msg.id === "string" ? msg.id : undefined;
86
+ if (id && otherIds) {
87
+ if (otherIds.has(id) && !mine.has(id))
88
+ seen = "copy";
89
+ mine.add(id);
90
+ }
91
+ const model = realModel(msg.model);
92
+ if (!model)
93
+ return;
94
+ const at = Date.parse(String(e.timestamp ?? ""));
95
+ if (!Number.isFinite(at))
96
+ return;
97
+ const u = msg.usage;
98
+ const W = num(u.cache_creation_input_tokens);
99
+ const R = num(u.cache_read_input_tokens);
100
+ const I = num(u.input_tokens);
101
+ const total = W + R + I;
102
+ const split = cacheWriteSplit(u);
103
+ // An entry lives as long as the TTL it was written at; a request that
104
+ // wrote nothing keeps the lifetime of the last one that did.
105
+ if (W > 0)
106
+ lastTtl = split.oneHour > split.fiveMinute ? HOUR : FIVE_MIN;
107
+ const p = pricingFor(model);
108
+ if (at >= since && at < until && seen === "new") {
109
+ counted++;
110
+ acc.requests++;
111
+ if (!p)
112
+ acc.unpriced++;
113
+ else {
114
+ acc.billedUsd += usageCost(u, p).promptUsd;
115
+ acc.writes += W;
116
+ acc.writes1h += split.oneHour;
117
+ const inp = p.inputPerM / 1e6;
118
+ const rd = p.cacheReadPerM / 1e6;
119
+ const continued = prev !== undefined && !compacted && prev.model === model;
120
+ for (const [ttl, mult] of [[FIVE_MIN, CACHE_WRITE_5M], [HOUR, CACHE_WRITE_1H]]) {
121
+ let read = R, write = W;
122
+ if (continued && prev) {
123
+ const gap = at - prev.at;
124
+ const warmAsBilled = gap < prev.ttl;
125
+ const warmHere = gap < ttl;
126
+ if (warmAsBilled && !warmHere && R > 0) {
127
+ // Read warm as billed; at this lifetime the entry had expired.
128
+ write = W + R;
129
+ read = 0;
130
+ if (ttl === FIVE_MIN)
131
+ acc.wentColdAt5m++;
132
+ }
133
+ else if (!warmAsBilled && warmHere && W > REWRITE_SHARE * prev.total) {
134
+ // Re-written cold as billed; at this lifetime the previous context was still there.
135
+ const kept = Math.max(0, Math.min(W, prev.total - R));
136
+ read = R + kept;
137
+ write = W - kept;
138
+ if (ttl === HOUR && kept > 0)
139
+ acc.keptWarmAt1h++;
140
+ }
141
+ }
142
+ const usd = I * inp + read * rd + write * inp * mult;
143
+ if (ttl === FIVE_MIN)
144
+ acc.at5mUsd += usd;
145
+ else
146
+ acc.at1hUsd += usd;
147
+ }
148
+ }
149
+ }
150
+ prev = { at, ttl: lastTtl, total, model };
151
+ compacted = false;
152
+ });
153
+ if (otherIds)
154
+ for (const id of mine)
155
+ otherIds.add(id);
156
+ return counted;
157
+ }
158
+ function finish(acc) {
159
+ const { writes, writes1h, ...r } = acc;
160
+ return { ...r, oneHourShare: writes > 0 ? writes1h / writes : 0 };
161
+ }
162
+ /** Subagent transcripts of the main transcripts under ~/.claude/projects written since `since`. */
163
+ export function subagentTranscripts(since, home = homedir()) {
164
+ const root = join(claudeDir(home), "projects");
165
+ const out = [];
166
+ let projects;
167
+ try {
168
+ projects = readdirSync(root);
169
+ }
170
+ catch {
171
+ return out;
172
+ }
173
+ for (const project of projects) {
174
+ let entries;
175
+ try {
176
+ entries = readdirSync(join(root, project));
177
+ }
178
+ catch {
179
+ continue;
180
+ }
181
+ for (const entry of entries) {
182
+ if (entry.endsWith(".jsonl"))
183
+ continue;
184
+ const dir = join(root, project, entry, "subagents");
185
+ if (!existsSync(dir))
186
+ continue;
187
+ let files;
188
+ try {
189
+ files = readdirSync(dir);
190
+ }
191
+ catch {
192
+ continue;
193
+ }
194
+ for (const f of files) {
195
+ if (!f.endsWith(".jsonl"))
196
+ continue;
197
+ const p = join(dir, f);
198
+ try {
199
+ if (statSync(p).mtimeMs >= since)
200
+ out.push(p);
201
+ }
202
+ catch {
203
+ /* gone */
204
+ }
205
+ }
206
+ }
207
+ }
208
+ return out;
209
+ }
210
+ export function estimateCacheTtl(days = 30, opts = {}) {
211
+ const since = opts.since ?? Date.now() - days * 86_400_000;
212
+ const until = opts.until ?? Infinity;
213
+ const mains = opts.paths ?? listSessions(10_000).filter((s) => isClaudeSession(s) && s.modifiedAt.getTime() >= since).map((s) => s.path);
214
+ const subs = opts.subagentPaths ?? subagentTranscripts(since);
215
+ const minRequests = opts.minRequests ?? 10;
216
+ const main = { ...empty(), writes: 0, writes1h: 0 };
217
+ const sub = { ...empty(), writes: 0, writes1h: 0 };
218
+ const runRows = new Set();
219
+ const total = mains.length + subs.length;
220
+ for (const [i, p] of mains.entries()) {
221
+ opts.onProgress?.(i, total);
222
+ // Into a scratch accumulator first: a session counts only with enough requests.
223
+ const one = { ...empty(), writes: 0, writes1h: 0 };
224
+ try {
225
+ if (replayTtlTranscript(p, one, since, until, false, runRows) < minRequests)
226
+ continue;
227
+ }
228
+ catch {
229
+ continue;
230
+ }
231
+ main.transcripts++;
232
+ for (const k of ["requests", "billedUsd", "at5mUsd", "at1hUsd", "keptWarmAt1h", "wentColdAt5m", "unpriced", "writes", "writes1h"])
233
+ main[k] += one[k];
234
+ }
235
+ const subIds = new Set();
236
+ for (const [i, p] of subs.entries()) {
237
+ opts.onProgress?.(mains.length + i, total);
238
+ try {
239
+ if (replayTtlTranscript(p, sub, since, until, true, undefined, subIds) > 0)
240
+ sub.transcripts++;
241
+ }
242
+ catch {
243
+ /* unreadable */
244
+ }
245
+ }
246
+ opts.onProgress?.(total, total);
247
+ return { days, main: finish(main), subagents: finish(sub) };
248
+ }
249
+ /** The lifetime to set, when one is clearly cheaper than the input as billed; else undefined. */
250
+ export function adviseTtl(r) {
251
+ if (!(r.billedUsd > 0))
252
+ return undefined;
253
+ const best = r.at1hUsd <= r.at5mUsd ? "1h" : "5m";
254
+ const saved = r.billedUsd - (best === "1h" ? r.at1hUsd : r.at5mUsd);
255
+ if (saved < Math.max(MIN_SAVING_USD, MIN_SAVING_SHARE * r.billedUsd))
256
+ return undefined;
257
+ return { ttl: best, savedUsd: saved, savedShare: saved / r.billedUsd };
258
+ }
259
+ // -- The settings ---------------------------------------------------------------
260
+ const KEY = { main: "promptCacheTtl", subagents: "subagentPromptCacheTtl" };
261
+ export function claudeSettingsPath(home) {
262
+ return join(claudeDir(home), "settings.json");
263
+ }
264
+ const asTtl = (v) => (v === "5m" || v === "1h" ? v : undefined);
265
+ /**
266
+ * The lifetimes Claude Code will use as far as its settings and environment
267
+ * decide (managed, project and user settings merged, CLAUDE_CONFIG_DIR
268
+ * honoured), and what overrides a value in the user's settings.json.
269
+ */
270
+ export function currentTtl(home, env = process.env, cwd = process.cwd()) {
271
+ const sources = settingsSources({ home, cwd, env });
272
+ const main = effectiveTtl("main", sources, env);
273
+ const sub = effectiveTtl("subagents", sources, env);
274
+ const mainOverrides = ttlOverrides("main", sources, env);
275
+ const subagentOverrides = ttlOverrides("subagents", sources, env);
276
+ return { main: asTtl(main.value), subagents: asTtl(sub.value), overrides: [...new Set([...mainOverrides, ...subagentOverrides])], mainOverrides, subagentOverrides };
277
+ }
278
+ /** Set (or with undefined, remove) promptCacheTtl / subagentPromptCacheTtl in the user's settings.json, editing only that key. */
279
+ export function setCacheTtl(kind, ttl, home) {
280
+ const p = claudeSettingsPath(home);
281
+ const cur = readSettings(p);
282
+ const previous = cur.data[KEY[kind]];
283
+ if (ttl === undefined ? previous === undefined : previous === ttl)
284
+ return { changed: false, created: false, ...(previous !== undefined ? { previous } : {}) };
285
+ const next = setJsonPath(cur.text, [KEY[kind]], ttl === undefined ? ABSENT : ttl);
286
+ backupOnce(p);
287
+ writeAtomic(p, next);
288
+ return { changed: true, created: cur.text === undefined, backup: cur.text !== undefined ? `${p}.context-doctor.backup` : undefined, previous };
289
+ }
290
+ export function settingKey(kind) {
291
+ return KEY[kind];
292
+ }
293
+ /** Claude Code installs too old for the setting (it does nothing there). */
294
+ export function ttlUnsupported(installs) {
295
+ return installs.filter((i) => compareVersions(i.version, CACHE_TTL_SINCE) < 0);
296
+ }
297
+ // -- The report -------------------------------------------------------------------
298
+ function money(n) {
299
+ if (!(Math.abs(n) > 0.005))
300
+ return "$0";
301
+ const a = Math.abs(n);
302
+ const s = a >= 100 ? `$${Math.round(a).toLocaleString("en-US")}` : `$${a.toFixed(2)}`;
303
+ return n < 0 ? `-${s}` : s;
304
+ }
305
+ const pct = (x) => `${Math.round(x * 100)}%`;
306
+ function block(label, r, set, kind) {
307
+ const lines = [];
308
+ lines.push(`${label}: ${r.requests.toLocaleString("en-US")} requests in ${r.transcripts} ${kind === "main" ? "sessions" : "subagent transcripts"}, ${pct(r.oneHourShare)} of cache writes at 1 hour`);
309
+ if (!(r.billedUsd > 0)) {
310
+ lines.push(" nothing priced to replay");
311
+ return lines;
312
+ }
313
+ const row = (name, usd, note = "") => ` ${name.padEnd(22)} ${money(usd).padStart(9)}${note}`;
314
+ lines.push(row("as billed", r.billedUsd, set ? ` (set: ${set})` : ""));
315
+ const d5 = r.billedUsd - r.at5mUsd;
316
+ const d1 = r.billedUsd - r.at1hUsd;
317
+ lines.push(row("all at 5 minutes", r.at5mUsd, ` ${d5 >= 0 ? "saves" : "costs"} ${money(Math.abs(d5))} (${pct(Math.abs(d5) / r.billedUsd)})`));
318
+ lines.push(row("all at 1 hour", r.at1hUsd, ` ${d1 >= 0 ? "saves" : "costs"} ${money(Math.abs(d1))} (${pct(Math.abs(d1) / r.billedUsd)})${r.keptWarmAt1h ? `; ${r.keptWarmAt1h} cold re-writes kept warm` : ""}`));
319
+ return lines;
320
+ }
321
+ export function renderCacheTtl(r, current, installs = [], command = (s) => `context-doctor ${s}`) {
322
+ const out = [];
323
+ out.push("Prompt cache lifetime: 5 minutes or 1 hour (Claude Code's own setting)");
324
+ out.push(`Last ${r.days} days, at list prices; the same content is sent either way`);
325
+ out.push("─".repeat(66));
326
+ out.push(...block("Main conversation", r.main, current.main, "main"));
327
+ out.push("");
328
+ out.push(...block("Subagents", r.subagents, current.subagents, "subagents"));
329
+ out.push("");
330
+ const advice = [];
331
+ for (const kind of ["main", "subagents"]) {
332
+ const a = adviseTtl(r[kind]);
333
+ const set = current[kind];
334
+ if (a && a.ttl !== set) {
335
+ advice.push(` ${kind === "main" ? "Main conversation" : "Subagents"}: ${a.ttl} would have saved ${money(a.savedUsd)} (${pct(a.savedShare)}). Set: ${command(`cache-ttl ${a.ttl}${kind === "subagents" ? " --subagents" : ""}`)}`);
336
+ }
337
+ }
338
+ if (advice.length) {
339
+ out.push("Recommended");
340
+ out.push(...advice);
341
+ }
342
+ else {
343
+ out.push("No change pays: the lifetimes as billed are within 2% of the cheaper one.");
344
+ }
345
+ if (current.main || current.subagents)
346
+ out.push(`Undo: ${command("cache-ttl off")}${current.subagents ? ` and ${command("cache-ttl off --subagents")}` : ""}`);
347
+ if (current.overrides.length)
348
+ out.push(`Note: ${current.overrides.join(", ")} overrides the setting.`);
349
+ const old = ttlUnsupported(installs);
350
+ if (old.length)
351
+ out.push(`Note: ${describeInstalls(old)} predates the setting (${CACHE_TTL_SINCE}) and ignores it.`);
352
+ out.push("");
353
+ out.push("A 1-hour write costs 2x input, a 5-minute one 1.25x; a pause longer than the");
354
+ out.push("lifetime makes the next message write the whole context again. On a subscription");
355
+ out.push("the same tokens come out of your usage limit instead of a bill.");
356
+ return out.join("\n");
357
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Where Claude Code keeps its user configuration.
3
+ *
4
+ * `~/.claude` (settings.json, projects/, skills/) and `~/.claude.json` (MCP
5
+ * servers, per-project state), unless CLAUDE_CONFIG_DIR names another
6
+ * folder: then both live there (read from the 2.1.309 binary: the global
7
+ * file is `join(CLAUDE_CONFIG_DIR || home, ".claude.json")`, the settings
8
+ * folder CLAUDE_CONFIG_DIR itself). A caller that passes its own `home`
9
+ * (tests, a throwaway home) gets that home's folders: the variable describes
10
+ * the real user's setup, not someone else's.
11
+ */
12
+ /** `~/.claude`, or CLAUDE_CONFIG_DIR. */
13
+ export declare function claudeDir(home?: string, env?: NodeJS.ProcessEnv): string;
14
+ /** `~/.claude.json`, or `$CLAUDE_CONFIG_DIR/.claude.json`. */
15
+ export declare function claudeJsonPath(home?: string, env?: NodeJS.ProcessEnv): string;
16
+ /** A path for messages: the home folder as `~`. */
17
+ export declare function tilde(p: string): string;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Where Claude Code keeps its user configuration.
3
+ *
4
+ * `~/.claude` (settings.json, projects/, skills/) and `~/.claude.json` (MCP
5
+ * servers, per-project state), unless CLAUDE_CONFIG_DIR names another
6
+ * folder: then both live there (read from the 2.1.309 binary: the global
7
+ * file is `join(CLAUDE_CONFIG_DIR || home, ".claude.json")`, the settings
8
+ * folder CLAUDE_CONFIG_DIR itself). A caller that passes its own `home`
9
+ * (tests, a throwaway home) gets that home's folders: the variable describes
10
+ * the real user's setup, not someone else's.
11
+ */
12
+ import { homedir } from "node:os";
13
+ import { join, resolve } from "node:path";
14
+ function configDirEnv(home, env) {
15
+ if (home !== undefined && home !== homedir())
16
+ return undefined;
17
+ const v = env.CLAUDE_CONFIG_DIR?.trim();
18
+ return v ? resolve(v) : undefined;
19
+ }
20
+ /** `~/.claude`, or CLAUDE_CONFIG_DIR. */
21
+ export function claudeDir(home, env = process.env) {
22
+ return configDirEnv(home, env) ?? join(home ?? homedir(), ".claude");
23
+ }
24
+ /** `~/.claude.json`, or `$CLAUDE_CONFIG_DIR/.claude.json`. */
25
+ export function claudeJsonPath(home, env = process.env) {
26
+ return join(configDirEnv(home, env) ?? home ?? homedir(), ".claude.json");
27
+ }
28
+ /** A path for messages: the home folder as `~`. */
29
+ export function tilde(p) {
30
+ const h = homedir();
31
+ return p.startsWith(h) ? "~" + p.slice(h.length) : p;
32
+ }