turno-cli 0.1.1 → 0.2.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 (104) hide show
  1. package/CHANGELOG.md +284 -0
  2. package/README.md +120 -53
  3. package/dist/accounts/audit.js +137 -7
  4. package/dist/accounts/builtin.js +68 -21
  5. package/dist/accounts/delegate.js +176 -35
  6. package/dist/accounts/policy.js +77 -0
  7. package/dist/accounts/registry.js +37 -21
  8. package/dist/agents/adapter.js +7 -0
  9. package/dist/agents/claude.js +195 -0
  10. package/dist/agents/codex.js +98 -0
  11. package/dist/agents/copilot.js +39 -0
  12. package/dist/agents/cursor.js +41 -0
  13. package/dist/agents/index.js +26 -86
  14. package/dist/agents/snippets.js +7 -4
  15. package/dist/cli/args.js +19 -4
  16. package/dist/cli/commands/accounts.js +96 -51
  17. package/dist/cli/commands/auto.js +82 -32
  18. package/dist/cli/commands/browser.js +216 -135
  19. package/dist/cli/commands/complete.js +2 -1
  20. package/dist/cli/commands/completion.js +4 -2
  21. package/dist/cli/commands/config.js +103 -18
  22. package/dist/cli/commands/demo.js +292 -0
  23. package/dist/cli/commands/doctor.js +387 -119
  24. package/dist/cli/commands/handoff.js +33 -22
  25. package/dist/cli/commands/identity.js +101 -50
  26. package/dist/cli/commands/init.js +616 -0
  27. package/dist/cli/commands/limit.js +25 -13
  28. package/dist/cli/commands/mcp-oauth.js +194 -0
  29. package/dist/cli/commands/mcp-observe.js +434 -0
  30. package/dist/cli/commands/mcp.js +653 -185
  31. package/dist/cli/commands/mcpd.js +323 -75
  32. package/dist/cli/commands/migrate.js +6 -4
  33. package/dist/cli/commands/open.js +92 -27
  34. package/dist/cli/commands/profile.js +205 -69
  35. package/dist/cli/commands/resolve.js +6 -2
  36. package/dist/cli/commands/run.js +15 -5
  37. package/dist/cli/commands/secret.js +61 -42
  38. package/dist/cli/commands/session.js +2 -2
  39. package/dist/cli/commands/sessions.js +42 -12
  40. package/dist/cli/commands/setup.js +136 -158
  41. package/dist/cli/commands/status.js +172 -41
  42. package/dist/cli/commands/statusline.js +305 -113
  43. package/dist/cli/commands/ui.js +64 -66
  44. package/dist/cli/commands/undo.js +161 -18
  45. package/dist/cli/commands/uninstall.js +462 -0
  46. package/dist/cli/commands/upgrade.js +75 -19
  47. package/dist/cli/commands/usage.js +117 -44
  48. package/dist/cli/commands/version.js +7 -5
  49. package/dist/cli/commands/which.js +7 -24
  50. package/dist/cli/compile-cache.js +32 -0
  51. package/dist/cli/group.js +10 -10
  52. package/dist/cli/help.js +57 -0
  53. package/dist/cli/highlight.js +236 -0
  54. package/dist/cli/io.js +170 -1
  55. package/dist/cli/launch.js +21 -37
  56. package/dist/cli/launchers.js +145 -94
  57. package/dist/cli/main.js +163 -27
  58. package/dist/cli/picker.js +447 -0
  59. package/dist/cli/registry.js +42 -5
  60. package/dist/cli/security.js +539 -0
  61. package/dist/cli/style.js +281 -0
  62. package/dist/core/config.js +145 -23
  63. package/dist/core/exhausted.js +6 -8
  64. package/dist/core/fsutil.js +47 -0
  65. package/dist/core/journal.js +281 -11
  66. package/dist/core/manifest.js +126 -6
  67. package/dist/core/paths.js +114 -7
  68. package/dist/core/profiles.js +139 -9
  69. package/dist/core/resolve.js +129 -23
  70. package/dist/core/secrets.js +215 -20
  71. package/dist/core/sessions.js +13 -20
  72. package/dist/core/update.js +18 -8
  73. package/dist/demo/server.js +34 -0
  74. package/dist/mcpd/cache.js +18 -4
  75. package/dist/mcpd/calllog.js +253 -0
  76. package/dist/mcpd/client-names.js +51 -0
  77. package/dist/mcpd/connectors.js +148 -17
  78. package/dist/mcpd/daemon.js +452 -116
  79. package/dist/mcpd/folder-sets.js +39 -0
  80. package/dist/mcpd/gateway.js +441 -47
  81. package/dist/mcpd/health.js +269 -0
  82. package/dist/mcpd/importer.js +356 -36
  83. package/dist/mcpd/instances.js +425 -65
  84. package/dist/mcpd/jsonrpc.js +8 -0
  85. package/dist/mcpd/launchd.js +65 -48
  86. package/dist/mcpd/modern.js +648 -0
  87. package/dist/mcpd/oauth.js +791 -0
  88. package/dist/mcpd/peerpid.js +122 -6
  89. package/dist/mcpd/pressure.js +85 -0
  90. package/dist/mcpd/rss.js +151 -0
  91. package/dist/mcpd/service.js +167 -0
  92. package/dist/mcpd/state.js +183 -0
  93. package/dist/mcpd/sync.js +55 -80
  94. package/dist/mcpd/systemd.js +80 -0
  95. package/dist/mcpd/tokens.js +89 -0
  96. package/dist/mcpd/upstream.js +154 -44
  97. package/dist/statusline/cost.js +154 -44
  98. package/dist/statusline/input.js +66 -0
  99. package/dist/statusline/render.js +9 -3
  100. package/dist/ui/fonts.js +8 -3
  101. package/dist/ui/settings.js +27 -15
  102. package/npm-shrinkwrap.json +1093 -0
  103. package/package.json +6 -6
  104. package/dist/cli/commands/available.js +0 -18
@@ -1,8 +1,23 @@
1
1
  import { LATEST_PROTOCOL_VERSION } from "@modelcontextprotocol/sdk/types.js";
2
2
  import { ErrorCode, isNotification, isRequest, isResponse, rpcError } from "./jsonrpc.js";
3
+ /** Server-to-client requests and the client capability each needs. */
4
+ const CLIENT_CAPABILITY = {
5
+ "elicitation/create": "elicitation",
6
+ "sampling/createMessage": "sampling",
7
+ "roots/list": "roots",
8
+ };
9
+ /** Server notifications that belong to one call and are routed like server requests. */
10
+ const ROUTED_NOTIFICATIONS = new Set(["notifications/message", "notifications/elicitation/complete"]);
11
+ /** True for the error the SDK raises when a remote server no longer knows our session. */
12
+ function sessionGone(err, hadSession) {
13
+ const code = err.code;
14
+ return hadSession && code === 404;
15
+ }
3
16
  /**
4
17
  * One running MCP server, spoken to over a raw transport. Request ids and progress tokens
5
- * are rewritten so several clients can share it without their messages crossing.
18
+ * are rewritten so several clients can share it without their messages crossing. Every
19
+ * request it relays is settled exactly once: by the server's answer, a cancellation, a
20
+ * timeout, the client going away, or the server stopping (ADR 0016).
6
21
  */
7
22
  export class Upstream {
8
23
  opts;
@@ -14,6 +29,8 @@ export class Upstream {
14
29
  init;
15
30
  lastActivity = Date.now();
16
31
  startedAt = Date.now();
32
+ /** A remote server answered 404 to our session: the instance must be replaced. */
33
+ sessionLost = false;
17
34
  constructor(opts) {
18
35
  this.opts = opts;
19
36
  }
@@ -29,6 +46,22 @@ export class Upstream {
29
46
  get isClosed() {
30
47
  return this.closed;
31
48
  }
49
+ /** Calls in flight on behalf of one client. */
50
+ inflightFor(client) {
51
+ let n = 0;
52
+ for (const p of this.pending.values())
53
+ if (p.origin === client)
54
+ n++;
55
+ return n;
56
+ }
57
+ /** Calls in flight per client session id, for /status. */
58
+ inflightByClient() {
59
+ const out = {};
60
+ for (const p of this.pending.values())
61
+ if (p.origin)
62
+ out[p.origin.id] = (out[p.origin.id] ?? 0) + 1;
63
+ return out;
64
+ }
32
65
  async start() {
33
66
  const t = await this.opts.connect();
34
67
  this.transport = t;
@@ -40,7 +73,7 @@ export class Upstream {
40
73
  await t.start();
41
74
  const res = await this.call("initialize", {
42
75
  protocolVersion: LATEST_PROTOCOL_VERSION,
43
- capabilities: { roots: { listChanged: true }, elicitation: { form: {}, url: {} } },
76
+ capabilities: this.opts.capabilities ?? { roots: { listChanged: true }, elicitation: { form: {} } },
44
77
  clientInfo: { name: "turno-mcpd", version: "0.1.0" },
45
78
  }, undefined, undefined, this.opts.initTimeoutMs ?? 60_000);
46
79
  if ("error" in res) {
@@ -71,15 +104,23 @@ export class Upstream {
71
104
  }
72
105
  this.lastActivity = Date.now();
73
106
  return new Promise((resolve) => {
74
- const p = { resolve, ...(origin ? { origin } : {}), ...(originId !== undefined ? { originId } : {}) };
107
+ const p = { resolve, method, ...(origin ? { origin } : {}), ...(originId !== undefined ? { originId } : {}) };
75
108
  if (timeoutMs) {
76
109
  p.timer = setTimeout(() => {
77
- this.pending.delete(id);
78
- resolve(rpcError(ErrorCode.Unavailable, `${this.label}: ${method} timed out`));
110
+ this.cancelUpstream(id, "timed out");
111
+ const secs = Math.round(timeoutMs / 100) / 10;
112
+ this.settle(id, rpcError(ErrorCode.RequestTimeout, `${this.label}: ${method} timed out after ${secs}s`));
79
113
  }, timeoutMs);
80
114
  }
81
115
  this.pending.set(id, p);
116
+ const hadSession = this.hasRemoteSession();
82
117
  this.send({ jsonrpc: "2.0", id, method, ...(outParams ? { params: outParams } : {}) }).catch((err) => {
118
+ if (sessionGone(err, hadSession)) {
119
+ this.sessionLost = true;
120
+ this.settle(id, rpcError(ErrorCode.Unavailable, `${this.label}: the remote server forgot the session`));
121
+ void this.close("remote session expired");
122
+ return;
123
+ }
83
124
  this.settle(id, rpcError(ErrorCode.Unavailable, `${this.label}: ${err.message}`));
84
125
  });
85
126
  });
@@ -88,21 +129,35 @@ export class Upstream {
88
129
  notify(method, params, origin) {
89
130
  if (this.closed)
90
131
  return;
91
- let out = params;
92
- if (method === "notifications/cancelled" && params?.requestId !== undefined) {
132
+ if (method === "notifications/cancelled") {
133
+ if (params?.requestId === undefined)
134
+ return;
93
135
  const upstreamId = this.findUpstreamId(params.requestId, origin?.id);
94
136
  if (!upstreamId)
95
137
  return;
96
- out = { ...params, requestId: upstreamId };
138
+ // The server will never answer a cancelled request (the spec says it must not), so
139
+ // settle it here: otherwise it stays in flight forever and the instance never idles out.
140
+ this.cancelUpstream(upstreamId, typeof params.reason === "string" ? params.reason : undefined);
141
+ this.settle(upstreamId, rpcError(ErrorCode.RequestCancelled, "cancelled by the client"));
142
+ return;
97
143
  }
98
144
  this.lastActivity = Date.now();
99
- void this.send({ jsonrpc: "2.0", method, ...(out ? { params: out } : {}) }).catch(() => { });
145
+ void this.send({ jsonrpc: "2.0", method, ...(params ? { params } : {}) }).catch(() => { });
100
146
  }
101
- /** Drop everything a client had in flight (it went away). */
102
- forgetClient(client) {
147
+ /** A client went away: cancel and settle everything it had in flight, forget its progress tokens. */
148
+ forgetClient(client, reason = "client disconnected") {
149
+ let n = 0;
150
+ for (const [id, p] of [...this.pending]) {
151
+ if (p.origin !== client)
152
+ continue;
153
+ this.cancelUpstream(id, reason);
154
+ this.settle(id, rpcError(ErrorCode.RequestCancelled, reason));
155
+ n++;
156
+ }
103
157
  for (const [token, p] of this.progress)
104
158
  if (p.origin === client)
105
159
  this.progress.delete(token);
160
+ return n;
106
161
  }
107
162
  /** Fetch every page of a list method. */
108
163
  async listAll(method, key) {
@@ -119,23 +174,27 @@ export class Upstream {
119
174
  }
120
175
  return items;
121
176
  }
122
- async close() {
177
+ async close(reason = "closed") {
123
178
  if (this.closed)
124
179
  return;
125
180
  this.closed = true;
126
- for (const [id, p] of this.pending) {
127
- if (p.timer)
128
- clearTimeout(p.timer);
129
- p.resolve(rpcError(ErrorCode.Unavailable, `${this.label} stopped`));
130
- this.pending.delete(id);
131
- }
181
+ this.failAll(`${this.label} stopped`);
132
182
  try {
133
183
  await this.transport?.close();
134
184
  }
135
185
  catch {
136
186
  /* already gone */
137
187
  }
138
- this.opts.onClose?.("closed");
188
+ this.opts.onClose?.(reason);
189
+ }
190
+ hasRemoteSession() {
191
+ return typeof this.transport?.sessionId === "string";
192
+ }
193
+ cancelUpstream(id, reason) {
194
+ if (this.closed || !this.transport)
195
+ return;
196
+ const params = { requestId: id, ...(reason ? { reason } : {}) };
197
+ void this.send({ jsonrpc: "2.0", method: "notifications/cancelled", params }).catch(() => { });
139
198
  }
140
199
  findUpstreamId(clientId, originId) {
141
200
  for (const [id, p] of this.pending) {
@@ -159,23 +218,55 @@ export class Upstream {
159
218
  clearTimeout(p.timer);
160
219
  p.resolve(o);
161
220
  }
162
- handleClose(reason) {
163
- if (this.closed)
164
- return;
165
- this.closed = true;
166
- for (const [id, p] of this.pending) {
221
+ failAll(message) {
222
+ for (const [id, p] of [...this.pending]) {
167
223
  if (p.timer)
168
224
  clearTimeout(p.timer);
169
- p.resolve(rpcError(ErrorCode.Unavailable, `${this.label} exited`));
170
225
  this.pending.delete(id);
226
+ p.resolve(rpcError(ErrorCode.Unavailable, message));
171
227
  }
228
+ this.progress.clear();
229
+ }
230
+ handleClose(reason) {
231
+ if (this.closed)
232
+ return;
233
+ this.closed = true;
234
+ this.failAll(`${this.label} exited`);
172
235
  this.opts.onClose?.(reason);
173
236
  }
174
- /** The client whose request is in flight, most recent first. */
175
- activeOrigin() {
176
- const all = [...this.pending.values()].filter((p) => p.origin);
177
- const last = all[all.length - 1];
178
- return last?.origin ? { origin: last.origin, ...(last.originId !== undefined ? { originId: last.originId } : {}) } : undefined;
237
+ /**
238
+ * The client a server-to-client message belongs to. Stdio carries no link between a server
239
+ * request and the call that caused it, so: an explicit related request id wins; else the one
240
+ * client with calls in flight; else the only attached client. With calls in flight from
241
+ * several clients the message is refused rather than sent to a guess (ADR 0016).
242
+ */
243
+ route(method, params) {
244
+ const meta = params?._meta;
245
+ const related = meta?.relatedRequestId;
246
+ if (typeof related === "string" || typeof related === "number") {
247
+ const p = this.pending.get(related);
248
+ if (p?.origin)
249
+ return { origin: p.origin, ...(p.originId !== undefined ? { originId: p.originId } : {}) };
250
+ }
251
+ const latest = new Map();
252
+ for (const p of this.pending.values())
253
+ if (p.origin)
254
+ latest.set(p.origin, p);
255
+ if (latest.size === 1) {
256
+ const [[origin, p]] = [...latest.entries()];
257
+ return { origin, ...(p.originId !== undefined ? { originId: p.originId } : {}) };
258
+ }
259
+ if (latest.size > 1) {
260
+ return {
261
+ code: ErrorCode.Ambiguous,
262
+ error: `${this.label} is shared by ${latest.size} sessions with calls in flight, so turno cannot tell which one this ${method} belongs to. ` +
263
+ `Retry when the other calls finish, or set scope = "session" (or exclusive = true) for this server in mcp.toml.`,
264
+ };
265
+ }
266
+ const attached = this.opts.attached?.() ?? [];
267
+ if (attached.length === 1)
268
+ return { origin: attached[0] };
269
+ return { code: ErrorCode.MethodNotFound, error: `no client session is waiting on ${this.label}` };
179
270
  }
180
271
  onMessage(m) {
181
272
  this.lastActivity = Date.now();
@@ -190,19 +281,35 @@ export class Upstream {
190
281
  if (isNotification(m))
191
282
  this.relayNotification(m);
192
283
  }
193
- async relayRequest(m) {
194
- const id = m.id;
195
- const method = m.method;
196
- let outcome;
197
- if (method === "ping") {
198
- outcome = { result: {} };
284
+ declared(capability) {
285
+ const caps = this.opts.capabilities ?? { roots: {}, elicitation: {} };
286
+ return caps[capability] !== undefined;
287
+ }
288
+ async answer(method, params) {
289
+ if (method === "ping")
290
+ return { result: {} };
291
+ const capability = CLIENT_CAPABILITY[method];
292
+ if (method === "roots/list") {
293
+ const local = this.opts.localRoots?.();
294
+ if (local)
295
+ return { result: local };
296
+ }
297
+ if (capability && !this.declared(capability)) {
298
+ return rpcError(ErrorCode.MethodNotFound, `turno did not offer ${capability} to ${this.label}`);
199
299
  }
200
- else {
201
- const target = this.activeOrigin();
202
- outcome = target
203
- ? await target.origin.request(method, m.params, target.originId)
204
- : rpcError(ErrorCode.MethodNotFound, `no client session is waiting on ${this.label}`);
300
+ const target = this.route(method, params);
301
+ if ("error" in target) {
302
+ this.opts.log?.(`${this.label}: refused ${method}: ${target.error}`);
303
+ return rpcError(target.code, target.error);
205
304
  }
305
+ if (capability && target.origin.supports && !target.origin.supports(capability)) {
306
+ return rpcError(ErrorCode.MethodNotFound, `the client session behind ${this.label} does not support ${capability}`);
307
+ }
308
+ return target.origin.request(method, params, target.originId);
309
+ }
310
+ async relayRequest(m) {
311
+ const id = m.id;
312
+ const outcome = await this.answer(m.method, m.params);
206
313
  const reply = "error" in outcome ? { jsonrpc: "2.0", id, error: outcome.error } : { jsonrpc: "2.0", id, result: outcome.result };
207
314
  await this.send(reply).catch(() => { });
208
315
  }
@@ -219,12 +326,15 @@ export class Upstream {
219
326
  // The server gave up on a request it sent us; nothing to undo on our side.
220
327
  return;
221
328
  }
222
- if (method === "notifications/message") {
223
- const target = this.activeOrigin();
224
- if (target) {
329
+ if (ROUTED_NOTIFICATIONS.has(method)) {
330
+ const target = this.route(method, m.params);
331
+ if (!("error" in target)) {
225
332
  target.origin.notify(method, m.params, target.originId);
226
333
  return;
227
334
  }
335
+ // Several sessions in flight: a log line or completion for one of them must not reach the others.
336
+ if (target.code === ErrorCode.Ambiguous)
337
+ return;
228
338
  }
229
339
  this.opts.onBroadcast?.(method, m.params);
230
340
  }
@@ -1,62 +1,114 @@
1
- import { existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, closeSync } from "node:fs";
1
+ import { existsSync, mkdirSync, openSync, readSync, readdirSync, renameSync, rmSync, statSync, writeFileSync, closeSync } from "node:fs";
2
2
  import { dirname, join } from "node:path";
3
- /** $/MTok: input, output, cache write 5 m, cache write 1 h, cache read. Longest prefix wins. */
4
- export const PRICES = {
5
- "claude-fable-5-2": [10, 50, 12.5, 20, 0.25],
3
+ import { PRIVATE_DIR_MODE, PRIVATE_FILE_MODE } from "../core/paths.js";
4
+ import { readJsonFile, realOr } from "../core/fsutil.js";
5
+ /**
6
+ * $/MTok: input, output, cache write 5 m, cache write 1 h, cache read, for Anthropic first-party
7
+ * API rates (Bedrock and Vertex ids map to the same model; their regional prices can differ a
8
+ * little). Ids are matched after `normalizeModel()`, exactly: an unknown id has no price, so the
9
+ * status line can say "$?" instead of a silent $0.00. Retired models stay so old transcripts
10
+ * keep their cost.
11
+ */
12
+ const PRICES = {
13
+ "claude-fable-5-2": [8, 40, 10, 16, 0.2],
14
+ "claude-mythos-5-2": [8, 40, 10, 16, 0.2],
6
15
  "claude-fable-5-1": [10, 50, 12.5, 20, 0.25],
7
16
  "claude-mythos-5-1": [10, 50, 12.5, 20, 0.25],
8
17
  "claude-fable-5": [10, 50, 12.5, 20, 1.0],
18
+ "claude-mythos-5": [10, 50, 12.5, 20, 1.0],
9
19
  "claude-opus-5-5": [4, 20, 5, 8, 0.2],
10
20
  "claude-opus-5": [5, 25, 6.25, 10, 0.5],
11
21
  "claude-opus-4-8": [5, 25, 6.25, 10, 0.5],
12
22
  "claude-opus-4-7": [5, 25, 6.25, 10, 0.5],
13
23
  "claude-opus-4-6": [5, 25, 6.25, 10, 0.5],
14
- "claude-sonnet-5-5": [2, 10, 2.5, 4, 0.2],
24
+ "claude-opus-4-5": [5, 25, 6.25, 10, 0.5],
25
+ "claude-opus-4-1": [15, 75, 18.75, 30, 1.5],
26
+ "claude-opus-4": [15, 75, 18.75, 30, 1.5],
27
+ "claude-3-opus": [15, 75, 18.75, 30, 1.5],
28
+ "claude-sonnet-5-5": [2, 10, 2.5, 4, 0.1],
15
29
  "claude-sonnet-5": [2, 10, 2.5, 4, 0.2],
16
30
  "claude-sonnet-4-6": [3, 15, 3.75, 6, 0.3],
17
- "claude-haiku-5-5": [1, 5, 1.25, 2, 0.1],
31
+ "claude-sonnet-4-5": [3, 15, 3.75, 6, 0.3],
32
+ "claude-sonnet-4": [3, 15, 3.75, 6, 0.3],
33
+ "claude-3-7-sonnet": [3, 15, 3.75, 6, 0.3],
34
+ "claude-3-5-sonnet": [3, 15, 3.75, 6, 0.3],
35
+ "claude-haiku-5-5": [0.1, 0.5, 0.125, 0.2, 0.01],
18
36
  "claude-haiku-4-5": [1, 5, 1.25, 2, 0.1],
37
+ "claude-3-5-haiku": [0.8, 4, 1, 1.6, 0.08],
38
+ "claude-3-haiku": [0.25, 1.25, 0.3, 0.5, 0.03],
19
39
  };
20
- const KEYS = Object.keys(PRICES).sort((a, b) => b.length - a.length);
40
+ /** Models whose price rises once the prompt passes a size: above `over` tokens, `price` applies. */
41
+ const LONG_PROMPT = {
42
+ "claude-haiku-5-5": { over: 100_000, price: [0.5, 2.5, 0.625, 1, 0.05] },
43
+ };
44
+ /**
45
+ * The first-party id of a model as it appears in a transcript: drops the Bedrock prefixes
46
+ * (`us.anthropic.`, ARNs), the Vertex `@date` suffix, Bedrock `-v1:0` suffixes, `[1m]`
47
+ * markers and `-YYYYMMDD` snapshot dates.
48
+ */
49
+ export function normalizeModel(model) {
50
+ let m = model.trim().toLowerCase();
51
+ const at = m.lastIndexOf("claude-");
52
+ if (at > 0)
53
+ m = m.slice(at);
54
+ return m
55
+ .replace(/\[[^\]]*\]$/, "")
56
+ .replace(/@.*$/, "")
57
+ .replace(/-v\d+(:\d+)?$/, "")
58
+ .replace(/-\d{8}$/, "");
59
+ }
21
60
  export function priceFor(model) {
22
- if (!model)
61
+ if (typeof model !== "string" || !model)
23
62
  return undefined;
24
- for (const k of KEYS)
25
- if (model.startsWith(k))
26
- return PRICES[k];
27
- return undefined;
63
+ return PRICES[normalizeModel(model)];
64
+ }
65
+ const num = (v) => (typeof v === "number" && Number.isFinite(v) ? v : 0);
66
+ /** Tokens a usage record bills: zero for Claude Code's synthetic messages. */
67
+ function usageTokens(u) {
68
+ return num(u.input_tokens) + num(u.output_tokens) + num(u.cache_creation_input_tokens) + num(u.cache_read_input_tokens);
28
69
  }
29
- /** API cost in USD of one assistant usage record. */
70
+ /**
71
+ * API cost in USD of one assistant usage record, or undefined when the model has no price.
72
+ * A record without tokens costs 0 whatever its model.
73
+ */
30
74
  export function usageCost(model, u) {
31
- const p = priceFor(model);
32
- if (!p)
75
+ if (usageTokens(u) === 0)
33
76
  return 0;
34
- const w1h = u.cache_creation?.ephemeral_1h_input_tokens ?? 0;
35
- const w5m = u.cache_creation?.ephemeral_5m_input_tokens ?? Math.max(0, (u.cache_creation_input_tokens ?? 0) - w1h);
36
- const usd = ((u.input_tokens ?? 0) * p[0] + (u.output_tokens ?? 0) * p[1] + w5m * p[2] + w1h * p[3] + (u.cache_read_input_tokens ?? 0) * p[4]) / 1e6;
77
+ let p = priceFor(model);
78
+ if (!p)
79
+ return undefined;
80
+ const long = LONG_PROMPT[normalizeModel(model)];
81
+ if (long && num(u.input_tokens) + num(u.cache_creation_input_tokens) + num(u.cache_read_input_tokens) > long.over)
82
+ p = long.price;
83
+ const w1h = num(u.cache_creation?.ephemeral_1h_input_tokens);
84
+ const w5m = u.cache_creation?.ephemeral_5m_input_tokens !== undefined
85
+ ? num(u.cache_creation.ephemeral_5m_input_tokens)
86
+ : Math.max(0, num(u.cache_creation_input_tokens) - w1h);
87
+ const usd = (num(u.input_tokens) * p[0] + num(u.output_tokens) * p[1] + w5m * p[2] + w1h * p[3] + num(u.cache_read_input_tokens) * p[4]) / 1e6;
37
88
  return usd * (u.speed === "fast" ? 2 : 1);
38
89
  }
90
+ /** Bumped when an older state would give a wrong total (v2: unknown models are tracked). */
91
+ const STATE_VERSION = 2;
39
92
  function readJson(file) {
40
- try {
41
- return JSON.parse(readFileSync(file, "utf8"));
42
- }
43
- catch {
44
- return undefined;
45
- }
93
+ return readJsonFile(file);
46
94
  }
47
95
  function writeJson(file, data) {
48
- mkdirSync(dirname(file), { recursive: true });
96
+ mkdirSync(dirname(file), { recursive: true, mode: PRIVATE_DIR_MODE });
49
97
  const tmp = `${file}.${process.pid}.tmp`;
50
- writeFileSync(tmp, JSON.stringify(data));
98
+ writeFileSync(tmp, JSON.stringify(data), { mode: PRIVATE_FILE_MODE });
51
99
  renameSync(tmp, file);
52
100
  }
53
- function real(p) {
54
- try {
55
- return realpathSync(p);
56
- }
57
- catch {
58
- return p;
59
- }
101
+ function validState(v) {
102
+ if (!v || typeof v !== "object")
103
+ return false;
104
+ const o = v;
105
+ return o.v === STATE_VERSION && !!o.files && typeof o.files === "object" && !Array.isArray(o.files);
106
+ }
107
+ function validFile(v) {
108
+ if (!v || typeof v !== "object")
109
+ return false;
110
+ const f = v;
111
+ return typeof f.offset === "number" && typeof f.usd === "number" && Array.isArray(f.recent);
60
112
  }
61
113
  /** Read only complete new lines of a transcript and add their cost. */
62
114
  function scan(path, st) {
@@ -68,7 +120,7 @@ function scan(path, st) {
68
120
  return;
69
121
  }
70
122
  if (size < st.offset)
71
- Object.assign(st, { offset: 0, usd: 0, recent: [] });
123
+ Object.assign(st, { offset: 0, usd: 0, recent: [], unknown: [] });
72
124
  if (size === st.offset)
73
125
  return;
74
126
  const fd = openSync(path, "r");
@@ -92,8 +144,8 @@ function scan(path, st) {
92
144
  catch {
93
145
  continue;
94
146
  }
95
- const m = o.message;
96
- if (o.type !== "assistant" || !m?.usage)
147
+ const m = o?.message;
148
+ if (o?.type !== "assistant" || !m?.usage || typeof m.usage !== "object")
97
149
  continue;
98
150
  const key = `${m.id}|${o.requestId}`; // every block of one response repeats its usage
99
151
  if (st.recent.includes(key))
@@ -101,7 +153,15 @@ function scan(path, st) {
101
153
  st.recent.push(key);
102
154
  if (st.recent.length > 64)
103
155
  st.recent.splice(0, st.recent.length - 64);
104
- st.usd += usageCost(m.model, m.usage);
156
+ const usd = usageCost(m.model, m.usage);
157
+ if (usd !== undefined)
158
+ st.usd += usd;
159
+ else {
160
+ const id = typeof m.model === "string" && m.model ? m.model.slice(0, 120) : "(none)";
161
+ st.unknown ??= [];
162
+ if (!st.unknown.includes(id))
163
+ st.unknown.push(id);
164
+ }
105
165
  }
106
166
  }
107
167
  finally {
@@ -111,25 +171,75 @@ function scan(path, st) {
111
171
  /** Incremental cost of a conversation: the main transcript plus its subagents. */
112
172
  export function computeCost(transcript, out, now = Date.now() / 1000) {
113
173
  const prev = readJson(out);
114
- const files = prev?.files ?? {};
115
- const main = real(transcript);
174
+ const files = {};
175
+ if (validState(prev))
176
+ for (const [k, f] of Object.entries(prev.files))
177
+ if (validFile(f))
178
+ files[k] = f;
179
+ const main = realOr(transcript);
116
180
  const stem = main.endsWith(".jsonl") ? main.slice(0, -6) : main;
117
181
  const subDir = join(stem, "subagents");
118
182
  const subs = existsSync(subDir)
119
183
  ? readdirSync(subDir)
120
184
  .filter((f) => f.endsWith(".jsonl"))
121
- .map((f) => real(join(subDir, f)))
185
+ .map((f) => realOr(join(subDir, f)))
122
186
  .sort()
123
187
  : [];
124
188
  for (const p of [main, ...subs]) {
125
- files[p] ??= { offset: 0, usd: 0, recent: [] };
189
+ files[p] ??= { offset: 0, usd: 0, recent: [], unknown: [] };
126
190
  scan(p, files[p]);
127
191
  }
128
192
  const mainUsd = files[main]?.usd ?? 0;
129
193
  const total = Object.values(files).reduce((a, f) => a + f.usd, 0);
130
- const state = { files, main: mainUsd, sub: total - mainUsd, total, updated_at: now };
131
- writeJson(out, state);
132
- rmSync(`${out}.lock`, { force: true });
194
+ const unknown = [...new Set(Object.values(files).flatMap((f) => f.unknown ?? []))];
195
+ const state = { v: STATE_VERSION, files, main: mainUsd, sub: total - mainUsd, total, unknown, updated_at: now };
196
+ try {
197
+ writeJson(out, state);
198
+ }
199
+ finally {
200
+ rmSync(`${out}.lock`, { force: true });
201
+ }
133
202
  return state;
134
203
  }
204
+ /** Remove status line cache files not touched for `maxAgeDays`, at most once a day. */
205
+ export function pruneCache(dir, now = Date.now() / 1000, maxAgeDays = 7) {
206
+ const stamp = join(dir, ".pruned");
207
+ try {
208
+ if (now - statSync(stamp).mtimeMs / 1000 < 86_400)
209
+ return 0;
210
+ }
211
+ catch {
212
+ /* never pruned */
213
+ }
214
+ let removed = 0;
215
+ let names;
216
+ try {
217
+ names = readdirSync(dir);
218
+ }
219
+ catch {
220
+ return 0;
221
+ }
222
+ for (const name of names) {
223
+ if (name === ".pruned")
224
+ continue;
225
+ const file = join(dir, name);
226
+ try {
227
+ const st = statSync(file);
228
+ if (st.isFile() && now - st.mtimeMs / 1000 > maxAgeDays * 86_400) {
229
+ rmSync(file, { force: true });
230
+ removed++;
231
+ }
232
+ }
233
+ catch {
234
+ /* raced with another worker */
235
+ }
236
+ }
237
+ try {
238
+ writeFileSync(stamp, "", { mode: PRIVATE_FILE_MODE });
239
+ }
240
+ catch {
241
+ /* read-only: try again next time */
242
+ }
243
+ return removed;
244
+ }
135
245
  //# sourceMappingURL=cost.js.map
@@ -0,0 +1,66 @@
1
+ export const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
2
+ export const num = (v) => (typeof v === "number" && Number.isFinite(v) ? v : undefined);
3
+ const str = (v) => (typeof v === "string" ? v : undefined);
4
+ const bool = (v) => (typeof v === "boolean" ? v : undefined);
5
+ /** Session ids become file names, so only plain ids are accepted. */
6
+ const SESSION_ID = /^[A-Za-z0-9-]{1,128}$/;
7
+ export function sessionId(v) {
8
+ return typeof v === "string" && SESSION_ID.test(v) ? v : undefined;
9
+ }
10
+ /** Copy the keys of `from` whose checked value is defined. */
11
+ function pick(from, checks) {
12
+ const out = {};
13
+ for (const k of Object.keys(checks)) {
14
+ const v = checks[k](from[k]);
15
+ if (v !== undefined)
16
+ out[k] = v;
17
+ }
18
+ return out;
19
+ }
20
+ function window(v) {
21
+ if (!isObj(v))
22
+ return undefined;
23
+ const w = pick(v, { used_percentage: num, resets_at: num });
24
+ return Object.keys(w).length ? w : undefined;
25
+ }
26
+ export function limits(v) {
27
+ if (!isObj(v))
28
+ return {};
29
+ return pick(v, { five_hour: window, seven_day: window });
30
+ }
31
+ const sub = (checks) => (v) => isObj(v) ? pick(v, checks) : undefined;
32
+ /** The stdin payload, with every field checked. Anything that is not an object gives {}. */
33
+ export function sanitize(raw) {
34
+ if (!isObj(raw))
35
+ return {};
36
+ const data = pick(raw, {
37
+ session_id: sessionId,
38
+ prompt_id: str,
39
+ cwd: str,
40
+ transcript_path: str,
41
+ workspace: sub({
42
+ current_dir: str,
43
+ project_dir: str,
44
+ repo: sub({ name: str }),
45
+ git_worktree: (v) => (v === null ? undefined : v),
46
+ }),
47
+ model: sub({ display_name: str }),
48
+ effort: sub({ level: str }),
49
+ cost: sub({ total_cost_usd: num, total_lines_added: num, total_lines_removed: num }),
50
+ context_window: sub({ used_percentage: num, total_input_tokens: num, context_window_size: num }),
51
+ prompt_cache: sub({
52
+ caching_observed: bool,
53
+ warm: bool,
54
+ expires_at: num,
55
+ recache_tokens_if_cold: num,
56
+ }),
57
+ rate_limits: (v) => (isObj(v) ? limits(v) : undefined),
58
+ });
59
+ return data;
60
+ }
61
+ export function turns(v) {
62
+ if (!Array.isArray(v))
63
+ return [];
64
+ return v.filter((e) => Array.isArray(e) && e.length === 3 && typeof e[0] === "string" && num(e[1]) !== undefined && (e[2] === null || num(e[2]) !== undefined));
65
+ }
66
+ //# sourceMappingURL=input.js.map