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,28 +1,43 @@
1
- import { execFileSync } from "node:child_process";
2
- import { existsSync, realpathSync } from "node:fs";
3
- import { join } from "node:path";
1
+ import { execFile } from "node:child_process";
2
+ import { existsSync, readlinkSync, realpathSync, statSync } from "node:fs";
3
+ import { basename, join } from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+ import { promisify } from "node:util";
4
6
  import { effectiveLaunch, variantKey } from "../core/manifest.js";
5
- import { resolveRecord } from "../core/secrets.js";
6
7
  import { fingerprint } from "./cache.js";
7
8
  import { ErrorCode, rpcError } from "./jsonrpc.js";
9
+ import { LoginRequiredError } from "./oauth.js";
10
+ import { effectiveIdle } from "./pressure.js";
8
11
  import { Upstream } from "./upstream.js";
9
- export function processAlive(pid) {
12
+ import { pidAlive } from "../core/fsutil.js";
13
+ const run = promisify(execFile);
14
+ export function isDirectory(p) {
10
15
  try {
11
- process.kill(pid, 0);
12
- return true;
16
+ return statSync(p).isDirectory();
13
17
  }
14
- catch (err) {
15
- return err.code === "EPERM";
18
+ catch {
19
+ return false;
16
20
  }
17
21
  }
18
- /** Git top level of a folder (worktree-specific), else the folder itself. */
19
- export function projectRoot(cwd) {
22
+ /** Results of slow lookups, keyed and shared by concurrent callers (a client opens many sessions at once). */
23
+ function memo(ttlMs, compute) {
24
+ const cache = new Map();
25
+ return (key, now = Date.now()) => {
26
+ const hit = cache.get(key);
27
+ if (hit && now - hit.at < ttlMs)
28
+ return hit.value;
29
+ if (cache.size > 500)
30
+ cache.clear();
31
+ const value = compute(key);
32
+ cache.set(key, { value, at: now });
33
+ return value;
34
+ };
35
+ }
36
+ /** Git top level of a folder (worktree-specific), else the folder itself. Async, cached for 10 s. */
37
+ export const projectRoot = memo(10_000, async (cwd) => {
20
38
  try {
21
- return execFileSync("git", ["-C", cwd, "rev-parse", "--show-toplevel"], {
22
- encoding: "utf8",
23
- stdio: ["ignore", "pipe", "ignore"],
24
- timeout: 3000,
25
- }).trim();
39
+ const { stdout } = await run("git", ["-C", cwd, "rev-parse", "--show-toplevel"], { encoding: "utf8", timeout: 3000 });
40
+ return stdout.trim() || cwd;
26
41
  }
27
42
  catch {
28
43
  try {
@@ -32,9 +47,29 @@ export function projectRoot(cwd) {
32
47
  return cwd;
33
48
  }
34
49
  }
35
- }
36
- export function variantOf(server, profile) {
37
- return server.kind === "stdio" ? variantKey(server, profile) : "default";
50
+ });
51
+ /** Current folder of a process: /proc on Linux, lsof elsewhere. Cached per PID for a minute. */
52
+ export const processCwd = memo(60_000, async (pid) => {
53
+ if (existsSync("/proc/self/cwd")) {
54
+ try {
55
+ return readlinkSync(`/proc/${pid}/cwd`);
56
+ }
57
+ catch {
58
+ return undefined;
59
+ }
60
+ }
61
+ try {
62
+ const { stdout } = await run("lsof", ["-a", "-p", String(pid), "-d", "cwd", "-Fn"], { encoding: "utf8", timeout: 3000 });
63
+ const line = stdout.split("\n").find((l) => l.startsWith("n"));
64
+ return line ? line.slice(1) : undefined;
65
+ }
66
+ catch {
67
+ return undefined;
68
+ }
69
+ });
70
+ /** Stdio servers and OAuth remotes have variants per profile; other remotes have only "default". */
71
+ function variantOf(server, profile) {
72
+ return variantKey(server, profile);
38
73
  }
39
74
  export function scopeOf(server) {
40
75
  if (server.kind === "remote")
@@ -49,6 +84,24 @@ export function descriptionFingerprint(server, variant) {
49
84
  const launch = effectiveLaunch(server, variant);
50
85
  return fingerprint({ c: launch.command, a: launch.args, e: launch.env, v: variant, p: server.package ?? null, b: server.builtin ?? null });
51
86
  }
87
+ /**
88
+ * Fingerprint of the whole definition (launch, scope, cwd, exclusive, idle, lease, timeouts),
89
+ * except who gets it and which tools they see: tool filters apply per request (ADR 0023), so a
90
+ * changed filter needs no new process.
91
+ */
92
+ export function definitionFingerprint(server) {
93
+ const { tools: _tools, ...rest } = server;
94
+ const variants = rest.kind === "stdio" ? Object.fromEntries(Object.entries(rest.variants).map(([k, v]) => [k, { ...v, tools: null }])) : undefined;
95
+ return fingerprint({ ...rest, profiles: null, ...(variants ? { variants } : {}) });
96
+ }
97
+ /** A server that is gone from the manifest (or no longer proxied) while sessions still use it. */
98
+ class ServerGoneError extends Error {
99
+ }
100
+ const LIST_NOTIFICATIONS = [
101
+ ["tools", "notifications/tools/list_changed"],
102
+ ["prompts", "notifications/prompts/list_changed"],
103
+ ["resourceTemplates", "notifications/resources/list_changed"],
104
+ ];
52
105
  /**
53
106
  * Owns every running upstream. Starts them lazily, shares or isolates them by scope,
54
107
  * hands out exclusive leases, and stops them when their owner dies or they go idle.
@@ -59,48 +112,85 @@ export class InstanceManager {
59
112
  starting = new Map();
60
113
  describing = new Map();
61
114
  clients = new Set();
115
+ breakers = new Map();
116
+ /** Starts per instance key since mcpd started; more than one means restarts. */
117
+ starts = new Map();
118
+ manifest;
62
119
  isAlive;
63
120
  now;
121
+ /** When this manager started: descriptions relisted since then were checked against a running server. */
122
+ bornAt;
123
+ /** The memory pressure seen by the last reap. */
124
+ pressureLevel = "normal";
125
+ /** When the last reap read the pressure (manager clock); undefined before that and while the feature is off. */
126
+ pressureCheckedAt;
64
127
  constructor(opts) {
65
128
  this.opts = opts;
66
- this.isAlive = opts.isAlive ?? processAlive;
129
+ this.isAlive = opts.isAlive ?? pidAlive;
67
130
  this.now = opts.now ?? Date.now;
131
+ this.bornAt = this.now();
132
+ if (opts.manifest)
133
+ this.manifest = opts.manifest;
134
+ }
135
+ /** Where definitions come from (the gateway passes its manifest getter). */
136
+ useManifest(manifest) {
137
+ this.manifest ??= manifest;
68
138
  }
69
139
  register(client) {
70
140
  this.clients.add(client);
71
141
  }
72
- /** A client session ended: drop it from every instance and release its leases. */
142
+ /** A client session ended: cancel its calls, drop it from every instance and release its leases. */
73
143
  detach(client) {
74
144
  this.clients.delete(client);
75
145
  for (const inst of this.instances.values()) {
76
- if (inst.clients.delete(client))
77
- inst.upstream.forgetClient(client);
146
+ const cancelled = inst.upstream.forgetClient(client);
147
+ if (cancelled)
148
+ this.opts.log?.(`cancelled ${cancelled} call(s) of a closed session on ${inst.upstream.label}`);
149
+ inst.clients.delete(client);
150
+ for (const [uri, subs] of inst.subscriptions) {
151
+ if (subs.delete(client) && subs.size === 0) {
152
+ inst.subscriptions.delete(uri);
153
+ void inst.upstream.call("resources/unsubscribe", { uri }, undefined, undefined, 30_000);
154
+ }
155
+ }
78
156
  if (inst.holder === client) {
79
157
  delete inst.holder;
80
158
  delete inst.holderSince;
81
159
  }
82
160
  }
83
161
  }
162
+ /** The current definition of the server a session talks to. */
163
+ serverOf(ctx) {
164
+ const def = this.manifest?.().servers[ctx.serverName];
165
+ if (!def || (def.kind === "remote" && !def.proxy)) {
166
+ throw new ServerGoneError(`${ctx.serverName} is no longer served by mcpd (removed from mcp.toml); restart the session to drop it`);
167
+ }
168
+ return def;
169
+ }
84
170
  keyOf(ctx) {
85
- const s = ctx.server;
171
+ const s = this.serverOf(ctx);
86
172
  const variant = variantOf(s, ctx.profile);
87
173
  const descKey = `${s.name}@${variant}`;
88
174
  const scope = scopeOf(s);
175
+ const defFp = definitionFingerprint(s);
176
+ const base = { descKey, variant, server: s, scope, defFp };
177
+ const head = `${s.name}|${variant}|${defFp}`;
89
178
  if (scope === "shared")
90
- return { key: `${s.name}|${variant}`, descKey, variant, workdir: this.opts.home };
179
+ return { ...base, key: head, workdir: this.opts.home, workdirKnown: false };
91
180
  if (scope === "project") {
92
- const root = ctx.cwd ? projectRoot(ctx.cwd) : this.opts.home;
93
- return { key: `${s.name}|${variant}|root:${root}`, descKey, variant, workdir: root };
181
+ const root = ctx.projectRoot ?? ctx.cwd;
182
+ return { ...base, key: `${head}|root:${root ?? this.opts.home}`, workdir: root ?? this.opts.home, workdirKnown: root !== undefined };
94
183
  }
95
- const workdir = ctx.cwd && existsSync(ctx.cwd) ? ctx.cwd : this.opts.home;
184
+ const workdir = ctx.cwd ?? this.opts.home;
185
+ const workdirKnown = ctx.cwd !== undefined;
96
186
  if (ctx.pid)
97
- return { key: `${s.name}|${variant}|pid:${ctx.pid}`, descKey, variant, workdir, pid: ctx.pid };
98
- return { key: `${s.name}|${variant}|sid:${ctx.sessionId}`, descKey, variant, workdir };
187
+ return { ...base, key: `${head}|pid:${ctx.pid}`, workdir, workdirKnown, pid: ctx.pid };
188
+ return { ...base, key: `${head}|sid:${ctx.sessionId}`, workdir, workdirKnown };
99
189
  }
100
190
  /** Description for initialize and the list methods: cached, else from a (kept) running instance. */
101
191
  async describe(client) {
102
- const { descKey, variant } = this.keyOf(client.ctx);
103
- const fp = descriptionFingerprint(client.ctx.server, variant);
192
+ const { descKey, variant, server } = this.keyOf(client.ctx);
193
+ const fp = descriptionFingerprint(server, variant);
104
194
  const cached = this.opts.cache.get(descKey, fp);
105
195
  if (cached)
106
196
  return cached;
@@ -108,13 +198,22 @@ export class InstanceManager {
108
198
  if (!p) {
109
199
  p = (async () => {
110
200
  const inst = await this.instanceFor(client, false);
111
- return this.refreshDescription(inst);
201
+ return inst.described ?? this.relist(inst);
112
202
  })();
113
203
  this.describing.set(descKey, p);
114
204
  p.finally(() => this.describing.delete(descKey)).catch(() => { });
115
205
  }
116
206
  return p;
117
207
  }
208
+ /** The cached description for this client's server, without starting anything. */
209
+ cachedDescription(client) {
210
+ const { descKey, variant, server } = this.keyOf(client.ctx);
211
+ return this.opts.cache.get(descKey, descriptionFingerprint(server, variant));
212
+ }
213
+ /** Whether a description was relisted from a running server since this manager started. */
214
+ verified(d) {
215
+ return d.updatedAt >= this.bornAt;
216
+ }
118
217
  /** The instance that serves a real request from this client (starts it, checks leases). */
119
218
  async acquire(client) {
120
219
  let inst;
@@ -122,9 +221,11 @@ export class InstanceManager {
122
221
  inst = await this.instanceFor(client, true);
123
222
  }
124
223
  catch (err) {
125
- return rpcError(ErrorCode.Unavailable, `could not start ${client.ctx.server.name}: ${err.message}`);
224
+ if (err instanceof ServerGoneError)
225
+ return rpcError(ErrorCode.Unavailable, err.message);
226
+ return rpcError(ErrorCode.Unavailable, `could not start ${client.ctx.serverName}: ${err.message}`);
126
227
  }
127
- const s = client.ctx.server;
228
+ const s = inst.server;
128
229
  if (s.kind === "stdio" && s.exclusive) {
129
230
  this.expireLease(inst);
130
231
  if (inst.holder && inst.holder !== client) {
@@ -139,12 +240,77 @@ export class InstanceManager {
139
240
  inst.clients.add(client);
140
241
  return inst;
141
242
  }
243
+ /**
244
+ * Relay one client request: start or join the instance, apply the server's call_timeout,
245
+ * and retry once on a fresh instance when a remote server forgot our session. `cancelled`
246
+ * says whether the client cancelled the request while the instance was still starting: the
247
+ * upstream has no call to cancel yet, so the request is dropped here instead of sent.
248
+ */
249
+ async forward(client, method, params, originId, cancelled) {
250
+ const startedAt = Date.now();
251
+ const t0 = performance.now();
252
+ let variant;
253
+ const done = (outcome) => {
254
+ try {
255
+ this.opts.onCall?.({ ctx: client.ctx, method, params, ...(variant ? { variant } : {}), outcome, startedAt, durationMs: performance.now() - t0 });
256
+ }
257
+ catch {
258
+ /* observing a call never fails it */
259
+ }
260
+ return outcome;
261
+ };
262
+ for (let attempt = 0;; attempt++) {
263
+ const inst = await this.acquire(client);
264
+ if ("error" in inst || "result" in inst)
265
+ return done(inst);
266
+ variant = inst.variant;
267
+ // Nothing awaits between this check and the call below, so a later cancellation finds it upstream.
268
+ if (cancelled?.())
269
+ return done(rpcError(ErrorCode.RequestCancelled, "cancelled by the client"));
270
+ const timeout = inst.server.callTimeout ? inst.server.callTimeout * 1000 : undefined;
271
+ const out = await inst.upstream.call(method, params, client, originId, timeout);
272
+ if ("error" in out && inst.upstream.sessionLost && attempt === 0) {
273
+ this.opts.log?.(`${inst.upstream.label}: remote session expired; reconnecting and retrying ${method}`);
274
+ if (this.instances.get(inst.key) === inst)
275
+ this.instances.delete(inst.key);
276
+ continue;
277
+ }
278
+ return done(out);
279
+ }
280
+ }
281
+ /** resources/subscribe: one upstream subscription per URI and instance, many client subscribers. */
282
+ async subscribe(client, uri, originId) {
283
+ const inst = await this.acquire(client);
284
+ if ("error" in inst || "result" in inst)
285
+ return inst;
286
+ let subs = inst.subscriptions.get(uri);
287
+ if (!subs) {
288
+ const out = await inst.upstream.call("resources/subscribe", { uri }, client, originId);
289
+ if ("error" in out)
290
+ return out;
291
+ subs = inst.subscriptions.get(uri) ?? new Set();
292
+ inst.subscriptions.set(uri, subs);
293
+ }
294
+ subs.add(client);
295
+ return { result: {} };
296
+ }
297
+ async unsubscribe(client, uri, originId) {
298
+ const inst = this.existing(client);
299
+ const subs = inst?.subscriptions.get(uri);
300
+ if (!inst || !subs?.delete(client))
301
+ return { result: {} };
302
+ if (subs.size > 0)
303
+ return { result: {} };
304
+ inst.subscriptions.delete(uri);
305
+ return inst.upstream.call("resources/unsubscribe", { uri }, client, originId);
306
+ }
142
307
  /** Instance already serving this client, if any (no start). */
143
308
  existing(client) {
144
309
  const inst = this.instances.get(this.keyOf(client.ctx).key);
145
310
  return inst && !inst.upstream.isClosed ? inst : undefined;
146
311
  }
147
- async refreshDescription(inst) {
312
+ /** List tools, prompts and resource templates again; store them and tell clients what changed. */
313
+ async relist(inst, quiet = []) {
148
314
  const init = inst.upstream.init;
149
315
  if (!init)
150
316
  throw new Error(`${inst.server.name} is not initialized`);
@@ -160,13 +326,57 @@ export class InstanceManager {
160
326
  d.tools = await inst.upstream.listAll("tools/list", "tools");
161
327
  if (caps.prompts)
162
328
  d.prompts = await inst.upstream.listAll("prompts/list", "prompts");
163
- if (caps.resources) {
164
- d.resources = await inst.upstream.listAll("resources/list", "resources");
329
+ if (caps.resources)
165
330
  d.resourceTemplates = await inst.upstream.listAll("resources/templates/list", "resourceTemplates").catch(() => []);
166
- }
331
+ const prev = this.opts.cache.peek(inst.descKey);
167
332
  this.opts.cache.set(inst.descKey, d);
333
+ if (prev) {
334
+ const changed = LIST_NOTIFICATIONS.filter(([k, m]) => !quiet.includes(m) && JSON.stringify(prev[k] ?? null) !== JSON.stringify(d[k] ?? null));
335
+ if (changed.length) {
336
+ this.opts.log?.(`${inst.upstream.label}: ${changed.map(([k]) => k).join(", ")} changed; notifying clients`);
337
+ for (const c of this.clientsOf(inst.descKey))
338
+ for (const [, m] of changed)
339
+ c.notify(m, undefined);
340
+ }
341
+ }
168
342
  return d;
169
343
  }
344
+ /**
345
+ * `POST /refresh/<server>`: relist running instances (clients get list_changed when it
346
+ * changed) and forget the cached description of variants that are not running.
347
+ */
348
+ async refresh(name) {
349
+ const def = this.manifest?.().servers[name];
350
+ const variants = def ? ["default", ...Object.keys(def.variants)] : ["default"];
351
+ const out = { refreshed: [], forgotten: [], errors: {} };
352
+ for (const variant of variants) {
353
+ const descKey = `${name}@${variant}`;
354
+ const running = this.list().find((i) => i.descKey === descKey && !i.upstream.isClosed && i.upstream.init);
355
+ if (running) {
356
+ try {
357
+ await this.relist(running);
358
+ out.refreshed.push(descKey);
359
+ }
360
+ catch (err) {
361
+ out.errors[descKey] = err.message;
362
+ }
363
+ }
364
+ else if (this.opts.cache.forget(descKey)) {
365
+ out.forgotten.push(descKey);
366
+ }
367
+ }
368
+ return out;
369
+ }
370
+ clientsOf(descKey) {
371
+ return [...this.clients].filter((c) => {
372
+ try {
373
+ return this.keyOf(c.ctx).descKey === descKey;
374
+ }
375
+ catch {
376
+ return false;
377
+ }
378
+ });
379
+ }
170
380
  async instanceFor(client, join) {
171
381
  const k = this.keyOf(client.ctx);
172
382
  const existing = this.instances.get(k.key);
@@ -177,7 +387,7 @@ export class InstanceManager {
177
387
  }
178
388
  let p = this.starting.get(k.key);
179
389
  if (!p) {
180
- p = this.start(client.ctx, k);
390
+ p = this.startGuarded(client.ctx, k);
181
391
  this.starting.set(k.key, p);
182
392
  p.finally(() => this.starting.delete(k.key)).catch(() => { });
183
393
  }
@@ -186,46 +396,120 @@ export class InstanceManager {
186
396
  inst.clients.add(client);
187
397
  return inst;
188
398
  }
399
+ /** Circuit breaker: after repeated start failures, answer at once with the log tail and back off. */
400
+ async startGuarded(ctx, k) {
401
+ const bkey = `${k.server.name}|${k.variant}|${k.defFp}`;
402
+ const b = this.breakers.get(bkey);
403
+ const now = this.now();
404
+ if (b && b.until > now)
405
+ throw new Error(this.breakerMessage(k, b, now));
406
+ try {
407
+ const inst = await this.start(ctx, k);
408
+ this.breakers.delete(bkey);
409
+ return inst;
410
+ }
411
+ catch (err) {
412
+ // A missing OAuth login is not a crash: no backoff, so the call after `turno mcp login` works.
413
+ if (err instanceof LoginRequiredError)
414
+ throw err;
415
+ const message = err.message;
416
+ const failures = (b?.failures ?? 0) + 1;
417
+ const threshold = this.opts.breakerThreshold ?? 3;
418
+ const base = this.opts.breakerBaseMs ?? 5000;
419
+ const until = failures >= threshold ? this.now() + Math.min(base * 2 ** (failures - threshold), 5 * 60_000) : 0;
420
+ this.breakers.set(bkey, { failures, until, lastError: message });
421
+ if (failures >= threshold)
422
+ this.opts.log?.(`${k.server.name}: ${failures} failed starts in a row; next try in ${Math.round((until - this.now()) / 1000)}s`);
423
+ throw err;
424
+ }
425
+ }
426
+ breakerMessage(k, b, now) {
427
+ const tail = this.opts.connector.logTail?.(k.server, k.variant, 10);
428
+ return (`${k.server.name} failed to start ${b.failures} times in a row (last error: ${b.lastError}); ` +
429
+ `turno tries again in ${Math.max(1, Math.round((b.until - now) / 1000))}s.` +
430
+ (tail ? `\nLast lines of its log:\n${tail}` : ""));
431
+ }
432
+ /** Client capabilities to declare upstream: only what the client behind it can honor. */
433
+ upstreamCapabilities(ctx, scope) {
434
+ const cc = ctx.clientCapabilities;
435
+ const caps = {};
436
+ // Shared instances serve many folders: there is no single right answer to roots/list.
437
+ if (scope !== "shared")
438
+ caps.roots = { listChanged: true };
439
+ if (cc === undefined)
440
+ caps.elicitation = { form: {} };
441
+ else if (cc.elicitation)
442
+ caps.elicitation = cc.elicitation;
443
+ if (cc?.sampling)
444
+ caps.sampling = cc.sampling;
445
+ return caps;
446
+ }
189
447
  async start(ctx, k) {
190
- const server = ctx.server;
191
- const { transport, pid } = await this.opts.connector.connect(server, k.variant, ctx, k.workdir);
448
+ const server = k.server;
449
+ const { transport, pid, warnings } = await this.opts.connector.connect(server, k.variant, ctx, k.workdir);
450
+ for (const w of warnings ?? [])
451
+ this.opts.log?.(`warning: ${w}`);
192
452
  let inst;
193
453
  const upstream = new Upstream({
194
454
  label: `${server.name}${k.variant === "default" ? "" : `@${k.variant}`}`,
195
455
  connect: async () => transport,
196
- pid,
456
+ ...(pid ? { pid } : {}),
457
+ capabilities: this.upstreamCapabilities(ctx, k.scope),
458
+ localRoots: () => inst && inst.scope !== "shared" && inst.workdirKnown
459
+ ? { roots: [{ uri: pathToFileURL(inst.workdir).href, name: basename(inst.workdir) || inst.workdir }] }
460
+ : undefined,
461
+ attached: () => (inst ? [...inst.clients] : []),
197
462
  onBroadcast: (method, params) => this.onBroadcast(inst, method, params),
198
463
  onClose: (reason) => {
199
464
  if (this.instances.get(k.key) === inst)
200
465
  this.instances.delete(k.key);
201
466
  this.opts.log?.(`stopped ${inst.upstream.label} (${reason})`);
202
467
  },
468
+ ...(this.opts.log ? { log: this.opts.log } : {}),
203
469
  });
204
470
  inst = {
205
471
  key: k.key,
206
472
  descKey: k.descKey,
207
473
  server,
474
+ defFp: k.defFp,
475
+ scope: k.scope,
208
476
  variant: k.variant,
209
477
  upstream,
210
478
  clients: new Set(),
211
479
  workdir: k.workdir,
480
+ workdirKnown: k.workdirKnown,
481
+ subscriptions: new Map(),
212
482
  ...(k.pid ? { pid: k.pid } : {}),
213
483
  };
214
484
  await upstream.start();
215
485
  this.instances.set(k.key, inst);
486
+ this.starts.set(k.key, (this.starts.get(k.key) ?? 0) + 1);
216
487
  this.opts.log?.(`started ${upstream.label} pid=${upstream.pid ?? "-"} key=${k.key}`);
488
+ // Every real start relists: a server updated in place (npx, uvx, a new build) shows its new tools.
489
+ inst.described = this.relist(inst);
490
+ inst.described.catch((err) => this.opts.log?.(`relist ${upstream.label} failed: ${err.message}`));
217
491
  return inst;
218
492
  }
219
493
  onBroadcast(inst, method, params) {
494
+ if (method === "notifications/resources/updated") {
495
+ const uri = typeof params?.uri === "string" ? params.uri : "";
496
+ for (const c of inst.subscriptions.get(uri) ?? [])
497
+ c.notify(method, params);
498
+ return;
499
+ }
500
+ if (method === "notifications/resources/list_changed") {
501
+ // Resources are not cached: tell this instance's clients (and listen streams), then refresh the templates quietly.
502
+ const targets = new Set(inst.clients);
503
+ for (const c of this.clientsOf(inst.descKey))
504
+ if (c.listening)
505
+ targets.add(c);
506
+ for (const c of targets)
507
+ c.notify(method, params);
508
+ this.relist(inst, [method]).catch((err) => this.opts.log?.(`refresh ${inst.upstream.label} failed: ${err.message}`));
509
+ return;
510
+ }
220
511
  if (method.endsWith("/list_changed")) {
221
- this.refreshDescription(inst)
222
- .then(() => {
223
- for (const c of this.clients) {
224
- if (this.keyOf(c.ctx).descKey === inst.descKey)
225
- c.notify(method, params);
226
- }
227
- })
228
- .catch((err) => this.opts.log?.(`refresh ${inst.upstream.label} failed: ${err.message}`));
512
+ this.relist(inst).catch((err) => this.opts.log?.(`refresh ${inst.upstream.label} failed: ${err.message}`));
229
513
  return;
230
514
  }
231
515
  for (const c of inst.clients)
@@ -237,29 +521,90 @@ export class InstanceManager {
237
521
  return;
238
522
  const h = inst.holder;
239
523
  const dead = h.ctx.pid ? !this.isAlive(h.ctx.pid) : false;
240
- const idle = this.now() - h.lastSeen > s.lease * 1000;
524
+ // A long call keeps the lease: the holder is busy, not idle.
525
+ const idle = inst.upstream.inflightFor(h) === 0 && this.now() - h.lastSeen > s.lease * 1000;
241
526
  if (dead || idle || !this.clients.has(h)) {
242
527
  delete inst.holder;
243
528
  delete inst.holderSince;
244
529
  }
245
530
  }
246
- /** Stop instances whose owner died or that sat idle too long; forget dead clients. */
531
+ /** Whether an instance still matches the manifest (false once its definition changed or went away). */
532
+ current(inst) {
533
+ if (!this.manifest)
534
+ return true;
535
+ const def = this.manifest().servers[inst.server.name];
536
+ return def !== undefined && definitionFingerprint(def) === inst.defFp;
537
+ }
538
+ /** `[mcpd] idle_under_pressure` changed in config.toml: applies from the next reap. */
539
+ setIdleUnderPressure(seconds) {
540
+ this.opts.idleUnderPressure = seconds;
541
+ }
542
+ /**
543
+ * The memory pressure for /status and `turno mcp top` (ADR 0028): the level the last reap
544
+ * read (null while the feature is off or before the first read), the configured limit, and
545
+ * the idle cap that applies now (null when servers keep their own limits).
546
+ */
547
+ memoryPressure() {
548
+ const under = this.opts.idleUnderPressure ?? 0;
549
+ const watched = this.opts.pressure !== undefined && under > 0 && this.pressureCheckedAt !== undefined;
550
+ const level = watched ? this.pressureLevel : null;
551
+ const cap = level ? effectiveIdle(Number.POSITIVE_INFINITY, level, under) : Number.POSITIVE_INFINITY;
552
+ return { level, checkedAt: watched ? (this.pressureCheckedAt ?? null) : null, idleUnderPressure: under, idleCap: Number.isFinite(cap) ? cap : null };
553
+ }
554
+ /** Read memory pressure; a failing probe counts as normal. Logs when the level changes. */
555
+ async readPressure() {
556
+ let level = "normal";
557
+ if (this.opts.pressure && (this.opts.idleUnderPressure ?? 0) > 0) {
558
+ try {
559
+ level = await this.opts.pressure();
560
+ }
561
+ catch {
562
+ level = "normal";
563
+ }
564
+ this.pressureCheckedAt = this.now();
565
+ }
566
+ else {
567
+ this.pressureCheckedAt = undefined;
568
+ }
569
+ if (level !== this.pressureLevel) {
570
+ const under = this.opts.idleUnderPressure ?? 0;
571
+ this.opts.log?.(level === "normal"
572
+ ? "memory pressure is normal again: servers keep their own idle limits"
573
+ : `memory pressure is ${level}: idle servers stop after ${effectiveIdle(Number.POSITIVE_INFINITY, level, under)}s at most`);
574
+ this.pressureLevel = level;
575
+ }
576
+ return level;
577
+ }
578
+ /**
579
+ * Stop instances whose owner died, whose definition changed, or that sat idle too long; forget
580
+ * dead clients. Under memory pressure the idle limit shrinks (ADR 0028).
581
+ */
247
582
  async reap() {
248
583
  const stopped = [];
584
+ const level = await this.readPressure();
249
585
  for (const c of [...this.clients]) {
250
586
  if (c.ctx.pid && !this.isAlive(c.ctx.pid))
251
587
  this.detach(c);
252
588
  }
253
589
  for (const inst of [...this.instances.values()]) {
254
590
  this.expireLease(inst);
255
- const s = inst.server;
256
- const idleLimit = (s.kind === "stdio" ? s.idle : s.idle) * 1000;
257
- const ownerDead = inst.pid !== undefined && !this.isAlive(inst.pid);
258
- const idle = inst.upstream.inflight === 0 && this.now() - inst.upstream.lastActivity > idleLimit;
259
- if (ownerDead || idle) {
591
+ const quiet = inst.upstream.inflight === 0;
592
+ const ownerAlive = inst.pid !== undefined && this.isAlive(inst.pid);
593
+ const ownerDead = inst.pid !== undefined && !ownerAlive;
594
+ const stale = quiet && !this.current(inst);
595
+ // A session server lives as long as its session: its state (a browser, a login) is the point.
596
+ const idleApplies = !(inst.scope === "session" && ownerAlive);
597
+ const limit = effectiveIdle(inst.server.idle, level, this.opts.idleUnderPressure ?? 0);
598
+ const idleFor = this.now() - inst.upstream.lastActivity;
599
+ const idle = idleApplies && quiet && idleFor > limit * 1000;
600
+ if (ownerDead || stale || idle) {
601
+ const why = ownerDead ? "owner exited" : stale ? "definition changed" : idleFor > inst.server.idle * 1000 ? "idle" : "idle (memory pressure)";
260
602
  this.instances.delete(inst.key);
603
+ // That key never comes back (dead PID, old definition): forget its start count.
604
+ if (ownerDead || stale)
605
+ this.starts.delete(inst.key);
261
606
  await inst.upstream.close();
262
- stopped.push(`${inst.upstream.label}: ${ownerDead ? "owner exited" : "idle"}`);
607
+ stopped.push(`${inst.upstream.label}: ${why}`);
263
608
  }
264
609
  }
265
610
  return stopped;
@@ -267,9 +612,21 @@ export class InstanceManager {
267
612
  list() {
268
613
  return [...this.instances.values()];
269
614
  }
615
+ /** How many times the instance under `key` started again after its first start. */
616
+ restarts(key) {
617
+ return Math.max(0, (this.starts.get(key) ?? 0) - 1);
618
+ }
270
619
  clientCount() {
271
620
  return this.clients.size;
272
621
  }
622
+ /** `turno mcp logout` (through `POST /logout/<server>`): the next request reads the secret store. */
623
+ forgetLogin(server, variants) {
624
+ this.opts.connector.forgetLogin?.(server, variants);
625
+ }
626
+ /** At shutdown: save OAuth tokens still only in memory; the labels whose tokens are lost. */
627
+ async flushLogins() {
628
+ return (await this.opts.connector.flushLogins?.()) ?? [];
629
+ }
273
630
  async stopAll() {
274
631
  const all = [...this.instances.values()];
275
632
  this.instances.clear();
@@ -285,19 +642,22 @@ export class InstanceManager {
285
642
  }
286
643
  }
287
644
  /**
288
- * Resolve a command against an installed package (turno mcp install). A declared package
289
- * that is not installed yet runs through `npx -y -p <package> <command>` so it still works.
645
+ * Resolve a command against an installed package (turno mcp install). A declared package that
646
+ * is not installed still runs through `npx -y -p <package> <command>`, with a warning: `mcp
647
+ * import` writes `package` and `mcp install` is a separate step, so failing here would break
648
+ * imported setups (ADR 0016).
290
649
  */
291
650
  export function resolveCommand(server, command, args, serversDir) {
292
651
  if (server.package) {
293
652
  const local = join(serversDir, server.name, "node_modules", ".bin", command);
294
653
  if (existsSync(local))
295
654
  return { command: local, args };
296
- return { command: "npx", args: ["-y", "-p", server.package, command, ...args] };
655
+ return {
656
+ command: "npx",
657
+ args: ["-y", "-p", server.package, command, ...args],
658
+ warning: `${server.name}: package ${server.package} is not installed, so npx resolves and runs it at every start. Run \`turno mcp install ${server.name}\` to install it once.`,
659
+ };
297
660
  }
298
661
  return { command, args };
299
662
  }
300
- export async function resolvedEnv(server, variant, resolve) {
301
- return resolveRecord(effectiveLaunch(server, variant).env, resolve);
302
- }
303
663
  //# sourceMappingURL=instances.js.map