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,52 +1,252 @@
1
- import { randomBytes } from "node:crypto";
2
- import { appendFileSync, chmodSync, existsSync, mkdirSync, readFileSync, watch, writeFileSync } from "node:fs";
1
+ import { createWriteStream, existsSync, fchmodSync, fstatSync, openSync, readFileSync, renameSync, rmSync, watch, writeFileSync, } from "node:fs";
3
2
  import { dirname, join } from "node:path";
4
3
  import { loadConfig } from "../core/config.js";
5
- import { loadManifest } from "../core/manifest.js";
6
- import { paths } from "../core/paths.js";
4
+ import { atomicWrite } from "../core/journal.js";
5
+ import { parseManifest } from "../core/manifest.js";
6
+ import { PRIVATE_FILE_MODE, ensurePrivateDir, paths } from "../core/paths.js";
7
7
  import { keychainResolver } from "../core/secrets.js";
8
+ import { packageInfo } from "../core/version.js";
8
9
  import { DescriptionCache } from "./cache.js";
10
+ import { createCallLog } from "./calllog.js";
9
11
  import { DefaultConnector } from "./connectors.js";
10
12
  import { Gateway } from "./gateway.js";
11
- import { InstanceManager, descriptionFingerprint, processAlive } from "./instances.js";
12
- import { peerPid } from "./peerpid.js";
13
- /** The bearer token sessions use; created once with mode 600. */
14
- export function ensureToken(env = process.env) {
15
- const file = paths(env).mcpdToken;
13
+ import { ClientNames } from "./client-names.js";
14
+ import { InstanceManager, descriptionFingerprint } from "./instances.js";
15
+ import { describeOwner, peerPid, portOwner } from "./peerpid.js";
16
+ import { memoryPressureProbe } from "./pressure.js";
17
+ import { RssSampler } from "./rss.js";
18
+ import { pidAlive } from "../core/fsutil.js";
19
+ import { answersAsMcpd, daemonFiles, ensureToken, isMcpdProcess, loadConfigSafe, readLock, readToken } from "./state.js";
20
+ export { daemonFiles, loadConfigSafe, masterToken, mcpdPort, readStartError, readToken, rotateToken } from "./state.js";
21
+ /** Another mcpd already holds the lock or answers on the port. Not a failure for a supervisor. */
22
+ export class AlreadyRunningError extends Error {
23
+ constructor(message) {
24
+ super(message);
25
+ this.name = "AlreadyRunningError";
26
+ }
27
+ }
28
+ /** The configured port belongs to some other process. */
29
+ export class PortBusyError extends Error {
30
+ port;
31
+ constructor(message, port) {
32
+ super(message);
33
+ this.port = port;
34
+ this.name = "PortBusyError";
35
+ }
36
+ }
37
+ /** Take the single-instance lock (O_EXCL), clearing a stale one once. */
38
+ function acquireLock(env) {
39
+ const file = daemonFiles(env).lock;
40
+ ensurePrivateDir(dirname(file), env);
41
+ const body = JSON.stringify({ pid: process.pid, port: 0, startedAt: new Date().toISOString() });
42
+ for (let attempt = 0; attempt < 2; attempt++) {
43
+ try {
44
+ writeFileSync(file, `${body}\n`, { flag: "wx", mode: PRIVATE_FILE_MODE });
45
+ return;
46
+ }
47
+ catch (err) {
48
+ if (err.code !== "EEXIST")
49
+ throw err;
50
+ }
51
+ let text = "";
52
+ try {
53
+ text = readFileSync(file, "utf8");
54
+ }
55
+ catch {
56
+ continue; // removed meanwhile: try again
57
+ }
58
+ const held = readLock(env);
59
+ if (held && isMcpdProcess(held.pid)) {
60
+ throw new AlreadyRunningError(`mcpd is already running (pid ${held.pid}${held.port ? `, port ${held.port}` : ""}); stop it with: turno mcp daemon stop`);
61
+ }
62
+ // Stale: remove it only if nobody replaced it since we read it.
63
+ try {
64
+ if (readFileSync(file, "utf8") === text)
65
+ rmSync(file, { force: true });
66
+ }
67
+ catch {
68
+ /* gone already */
69
+ }
70
+ }
71
+ throw new AlreadyRunningError("another mcpd is starting right now");
72
+ }
73
+ function releaseLock(env, port) {
74
+ const f = daemonFiles(env);
75
+ if (readLock(env)?.pid !== process.pid)
76
+ return;
77
+ rmSync(f.lock, { force: true });
16
78
  try {
17
- const t = readFileSync(file, "utf8").trim();
18
- if (t.length >= 32)
19
- return t;
79
+ if (Number.parseInt(readFileSync(paths(env).mcpdPort, "utf8"), 10) === port)
80
+ rmSync(paths(env).mcpdPort, { force: true });
20
81
  }
21
82
  catch {
22
- /* create it */
83
+ /* no port file */
23
84
  }
24
- mkdirSync(dirname(file), { recursive: true });
25
- const token = randomBytes(32).toString("hex");
26
- writeFileSync(file, `${token}\n`, { mode: 0o600 });
27
- chmodSync(file, 0o600);
28
- return token;
29
85
  }
30
- export function readToken(env = process.env) {
86
+ function writeStartError(env, e) {
87
+ const file = daemonFiles(env).error;
31
88
  try {
32
- return readFileSync(paths(env).mcpdToken, "utf8").trim() || undefined;
89
+ if (!e)
90
+ rmSync(file, { force: true });
91
+ else {
92
+ ensurePrivateDir(dirname(file), env);
93
+ atomicWrite(file, `${JSON.stringify(e)}\n`, PRIVATE_FILE_MODE);
94
+ }
33
95
  }
34
96
  catch {
35
- return undefined;
97
+ /* best effort */
36
98
  }
37
99
  }
38
- /** The port mcpd listens on: the one it recorded, else the configured one. */
39
- export function mcpdPort(env = process.env) {
100
+ /**
101
+ * The mcpd log: one append stream, rotated by size (mcpd.log.1 ... mcpd.log.<keep>), with an
102
+ * error handler so a full disk never takes the daemon down. Timestamps are UTC ISO;
103
+ * `turno mcp daemon logs` shows them in local time.
104
+ */
105
+ export function createLogger(file, opts = {}) {
106
+ const maxBytes = opts.maxBytes ?? 5 * 1024 * 1024;
107
+ const keep = opts.keep ?? 2;
108
+ let stream;
109
+ let size = 0;
110
+ let broken = false;
111
+ const onError = (err) => {
112
+ if (broken)
113
+ return;
114
+ broken = true;
115
+ try {
116
+ process.stderr.write(`mcpd: cannot write ${file}: ${err.message}\n`);
117
+ }
118
+ catch {
119
+ /* nowhere to report */
120
+ }
121
+ };
122
+ let retryAt = 0;
123
+ // Opened synchronously so the file exists before the first rotation renames it.
124
+ const open = () => {
125
+ try {
126
+ ensurePrivateDir(dirname(file), opts.env ?? process.env);
127
+ const fd = openSync(file, "a", PRIVATE_FILE_MODE);
128
+ const st = fstatSync(fd);
129
+ if (st.mode & 0o077)
130
+ fchmodSync(fd, PRIVATE_FILE_MODE);
131
+ size = st.size;
132
+ const s = createWriteStream("", { fd });
133
+ s.on("error", onError);
134
+ broken = false;
135
+ return s;
136
+ }
137
+ catch (err) {
138
+ onError(err);
139
+ retryAt = Date.now() + 30_000;
140
+ return undefined;
141
+ }
142
+ };
143
+ const rotate = () => {
144
+ stream?.end();
145
+ try {
146
+ for (let i = keep - 1; i >= 1; i--)
147
+ if (existsSync(`${file}.${i}`))
148
+ renameSync(`${file}.${i}`, `${file}.${i + 1}`);
149
+ renameSync(file, `${file}.1`);
150
+ }
151
+ catch (err) {
152
+ onError(err);
153
+ }
154
+ stream = open();
155
+ };
156
+ const log = ((line) => {
157
+ const text = `${new Date().toISOString()} ${line}\n`;
158
+ if (opts.toStdout) {
159
+ process.stdout.write(text);
160
+ return;
161
+ }
162
+ if (!stream && Date.now() >= retryAt)
163
+ stream = open();
164
+ if (!stream)
165
+ return; // the disk refused; onError said so once
166
+ const n = Buffer.byteLength(text);
167
+ if (size > 0 && size + n > maxBytes)
168
+ rotate();
169
+ stream?.write(text);
170
+ size += n;
171
+ });
172
+ log.close = () => new Promise((resolve) => (stream ? stream.end(() => resolve()) : resolve()));
173
+ return log;
174
+ }
175
+ /** Errors a socket or the disk raises that do not leave the daemon in a bad state. */
176
+ const RECOVERABLE = new Set(["EPIPE", "ECONNRESET", "ECONNABORTED", "ETIMEDOUT", "ENOSPC", "EIO", "EAGAIN", "ERR_STREAM_DESTROYED", "ERR_STREAM_WRITE_AFTER_END", "ERR_STREAM_PREMATURE_CLOSE"]);
177
+ /**
178
+ * For the daemon process only (not for tests that start mcpd in process): log unhandled
179
+ * rejections and recoverable exceptions and keep serving; on anything else log it, stop
180
+ * cleanly and exit 1 so the service manager restarts mcpd.
181
+ */
182
+ export function installCrashGuards(log, stop) {
183
+ const onRejection = (reason) => log(`unhandled rejection, still running: ${reason?.stack ?? String(reason)}`);
184
+ const onException = (err) => {
185
+ const code = err.code;
186
+ if (code && RECOVERABLE.has(code)) {
187
+ log(`uncaught ${code}, still running: ${err.stack ?? err.message}`);
188
+ return;
189
+ }
190
+ log(`fatal: ${err.stack ?? err.message}`);
191
+ let exited = false;
192
+ const exit = () => {
193
+ if (exited)
194
+ return;
195
+ exited = true;
196
+ clearTimeout(timer);
197
+ process.exit(1);
198
+ };
199
+ const timer = setTimeout(exit, 3000);
200
+ timer.unref();
201
+ stop().then(exit, exit);
202
+ };
203
+ process.on("unhandledRejection", onRejection);
204
+ process.on("uncaughtException", onException);
205
+ return () => {
206
+ process.off("unhandledRejection", onRejection);
207
+ process.off("uncaughtException", onException);
208
+ };
209
+ }
210
+ /**
211
+ * mcp.toml, never throwing: when it does not validate, the last copy that did (kept in the
212
+ * state folder on every good load), else no servers. The error is kept for /status and doctor.
213
+ */
214
+ export function loadManifestSafe(env = process.env) {
215
+ const file = paths(env).mcpFile;
216
+ const lastGood = daemonFiles(env).lastGood;
40
217
  try {
41
- const n = Number.parseInt(readFileSync(paths(env).mcpdPort, "utf8"), 10);
42
- if (n > 0)
43
- return n;
218
+ let text = "";
219
+ try {
220
+ text = readFileSync(file, "utf8");
221
+ }
222
+ catch (err) {
223
+ if (err.code !== "ENOENT")
224
+ throw err;
225
+ }
226
+ const manifest = parseManifest(text, file);
227
+ if (text) {
228
+ try {
229
+ if (!existsSync(lastGood) || readFileSync(lastGood, "utf8") !== text)
230
+ atomicWrite(lastGood, text, 0o600);
231
+ }
232
+ catch {
233
+ /* the copy is a convenience */
234
+ }
235
+ }
236
+ return { manifest, source: "mcp.toml" };
44
237
  }
45
- catch {
46
- /* fall through */
238
+ catch (err) {
239
+ const error = err.message;
240
+ try {
241
+ return { manifest: parseManifest(readFileSync(lastGood, "utf8"), lastGood), source: "last good copy", error };
242
+ }
243
+ catch {
244
+ return { manifest: parseManifest(""), source: "empty", error };
245
+ }
47
246
  }
48
- return loadConfig(env).mcpd.port;
49
247
  }
248
+ // ---------------------------------------------------------------------------------------------
249
+ // Daemon
50
250
  async function loadBuiltins() {
51
251
  const builtins = {};
52
252
  try {
@@ -59,105 +259,241 @@ async function loadBuiltins() {
59
259
  }
60
260
  return builtins;
61
261
  }
62
- /** Start mcpd in this process. Used by `turno mcpd run` (foreground or under launchd). */
262
+ /** The gateway plus what only the daemon knows (port, manifest state), in /status. */
263
+ class DaemonGateway extends Gateway {
264
+ extra;
265
+ constructor(opts, extra) {
266
+ super(opts);
267
+ this.extra = extra;
268
+ }
269
+ status() {
270
+ return { ...super.status(), ...this.extra() };
271
+ }
272
+ }
273
+ /**
274
+ * Start mcpd in this process (`turno mcp daemon run`, in a terminal or under the service).
275
+ * Single instance: a lock file with the PID, and no port fallback. If the configured port is
276
+ * busy it fails with PortBusyError naming the owner, because every synced profile points at
277
+ * that port. A broken mcp.toml or config.toml never stops it from starting.
278
+ */
63
279
  export async function startDaemon(opts = {}) {
64
280
  const env = opts.env ?? process.env;
65
281
  const p = paths(env);
66
- mkdirSync(p.logsDir, { recursive: true });
67
- const logFile = join(p.logsDir, "mcpd.log");
68
- const log = (line) => {
69
- const text = `${new Date().toISOString()} ${line}\n`;
70
- if (opts.logToStdout)
71
- process.stdout.write(text);
72
- else
73
- appendFileSync(logFile, text);
74
- };
75
- let manifest = loadManifest(env);
76
- const config = loadConfig(env);
77
- const token = ensureToken(env);
78
- const manager = new InstanceManager({
79
- cache: new DescriptionCache(p.mcpdCacheDir),
80
- connector: new DefaultConnector({
81
- resolve: keychainResolver(env),
82
- serversDir: p.serversDir,
83
- logsDir: p.logsDir,
84
- builtins: await loadBuiltins(),
85
- baseEnv: Object.fromEntries(["PATH", "HOME", "USER", "LOGNAME", "SHELL", "LANG", "TMPDIR"].flatMap((k) => (env[k] ? [[k, env[k]]] : []))),
86
- }),
87
- home: p.home,
88
- log,
89
- });
90
- const gateway = new Gateway({ manager, manifest: () => manifest, token, peerPid, log });
282
+ const files = daemonFiles(env);
283
+ const log = createLogger(files.log, { env, toStdout: Boolean(opts.logToStdout), ...(opts.logMaxBytes ? { maxBytes: opts.logMaxBytes } : {}) });
284
+ const { config, error: firstConfigError } = loadConfigSafe(env);
285
+ let configError = firstConfigError;
286
+ if (configError)
287
+ log(`config.toml is invalid, using defaults: ${configError}`);
288
+ // Folder server sets (ADR 0031): the last good [[projects]] rules, reloaded with config.toml.
289
+ let folderRules = config.projects;
91
290
  const wanted = opts.port ?? config.mcpd.port;
291
+ const fail = async (err) => {
292
+ if (!(err instanceof AlreadyRunningError))
293
+ writeStartError(env, { at: new Date().toISOString(), message: err.message, port: wanted });
294
+ log(`mcpd could not start: ${err.message}`);
295
+ await log.close();
296
+ throw err;
297
+ };
298
+ try {
299
+ acquireLock(env);
300
+ }
301
+ catch (err) {
302
+ return fail(err);
303
+ }
304
+ let state = loadManifestSafe(env);
305
+ if (state.error)
306
+ log(`mcp.toml is invalid, serving the ${state.source}: ${state.error}`);
307
+ const startedAt = new Date().toISOString();
92
308
  let port = 0;
93
- for (let attempt = 0; attempt < 20; attempt++) {
309
+ try {
310
+ const token = ensureToken(env, (m) => log(m));
311
+ // The call log follows `[mcpd] call_log`, also when config.toml changes while mcpd runs.
312
+ let calls = config.mcpd.callLog ? createCallLog(files.calls, { onError: (m) => log(m) }) : undefined;
313
+ const sampler = new RssSampler(files.rss, { env });
314
+ const manager = new InstanceManager({
315
+ cache: new DescriptionCache(p.mcpdCacheDir),
316
+ connector: new DefaultConnector({
317
+ resolve: keychainResolver(env),
318
+ serversDir: p.serversDir,
319
+ logsDir: p.logsDir,
320
+ vars: env,
321
+ builtins: await loadBuiltins(),
322
+ baseEnv: Object.fromEntries(["PATH", "HOME", "USER", "LOGNAME", "SHELL", "LANG", "TMPDIR"].flatMap((k) => (env[k] ? [[k, env[k]]] : []))),
323
+ }),
324
+ home: p.home,
325
+ log,
326
+ onCall: (e) => calls?.record(e),
327
+ pressure: memoryPressureProbe(),
328
+ idleUnderPressure: config.mcpd.idleUnderPressure,
329
+ });
330
+ const gwOpts = {
331
+ manager,
332
+ manifest: () => state.manifest,
333
+ token,
334
+ peerPid: (rp) => peerPid(rp),
335
+ log,
336
+ folderRules: () => folderRules,
337
+ home: p.home,
338
+ clientNames: new ClientNames(join(p.state, "mcpd-clients.json"), pidAlive),
339
+ ...(config.mcpd.maxRequestBody ? { maxRequestBodyBytes: config.mcpd.maxRequestBody } : {}),
340
+ };
341
+ const gateway = new DaemonGateway(gwOpts, () => ({
342
+ version: packageInfo().version,
343
+ port,
344
+ startedAt,
345
+ manifest: { source: state.source, servers: Object.keys(state.manifest.servers).length, error: state.error ?? null },
346
+ config: { error: configError ?? null, folderSets: folderRules.filter((r) => r.mcp || r.mcpDeny).length },
347
+ callLog: { enabled: Boolean(calls) },
348
+ serverRss: sampler.samples(),
349
+ }));
94
350
  try {
95
- port = await gateway.listen(wanted === 0 ? 0 : wanted + attempt);
96
- break;
351
+ port = await gateway.listen(wanted);
97
352
  }
98
353
  catch (err) {
99
- if (err.code !== "EADDRINUSE" || wanted === 0)
354
+ await gateway.close().catch(() => { });
355
+ if (err.code !== "EADDRINUSE")
100
356
  throw err;
357
+ const ours = await answersAsMcpd(wanted, token);
358
+ if (ours)
359
+ throw new AlreadyRunningError(`mcpd is already running on port ${wanted}${ours.pid ? ` (pid ${ours.pid})` : ""}`);
360
+ const owner = await portOwner(wanted, { env });
361
+ throw new PortBusyError(`port ${wanted} is in use by ${describeOwner(owner)}. mcpd does not move to another port, because every synced profile points at this one: free the port, or pick another with \`turno config set mcpd.port <port>\` and run \`turno mcp sync\``, wanted);
101
362
  }
102
- }
103
- if (!port)
104
- throw new Error(`no free port from ${wanted}`);
105
- mkdirSync(dirname(p.mcpdPort), { recursive: true });
106
- writeFileSync(p.mcpdPort, `${port}\n`);
107
- log(`mcpd listening on 127.0.0.1:${port} with ${Object.keys(manifest.servers).length} servers`);
108
- const reload = async (why) => {
109
- let next;
110
- try {
111
- next = loadManifest(env);
112
- }
113
- catch (err) {
114
- log(`reload skipped (${why}): ${err.message}`);
115
- return;
116
- }
117
- // Stop instances whose definition changed or disappeared; the next call starts the new one.
118
- for (const inst of manager.list()) {
119
- const def = next.servers[inst.server.name];
120
- if (!def || descriptionFingerprint(def, inst.variant) !== descriptionFingerprint(inst.server, inst.variant)) {
121
- await manager.stop(inst.key);
122
- log(`stopped ${inst.upstream.label} after manifest change`);
363
+ writeFileSync(files.lock, `${JSON.stringify({ pid: process.pid, port, startedAt })}\n`, { mode: PRIVATE_FILE_MODE });
364
+ ensurePrivateDir(dirname(p.mcpdPort), env);
365
+ atomicWrite(p.mcpdPort, `${port}\n`, PRIVATE_FILE_MODE);
366
+ writeStartError(env, undefined);
367
+ log(`mcpd ${packageInfo().version} listening on 127.0.0.1:${port} with ${Object.keys(state.manifest.servers).length} servers`);
368
+ const reload = async (why) => {
369
+ const next = loadManifestSafe(env);
370
+ if (next.error) {
371
+ // Keep serving what runs now; remember why the file was refused.
372
+ state = { ...state, error: next.error, source: state.source === "mcp.toml" ? "last good copy" : state.source };
373
+ log(`reload skipped (${why}): ${next.error}`);
374
+ return;
123
375
  }
124
- }
125
- manifest = next;
126
- log(`manifest reloaded (${why}): ${Object.keys(manifest.servers).length} servers`);
127
- };
128
- let debounce;
129
- let watcher;
130
- if (existsSync(p.config)) {
131
- watcher = watch(p.config, (_event, file) => {
132
- if (file !== "mcp.toml")
376
+ const tell = gateway.beforeViewChange();
377
+ // Stop instances whose definition changed or disappeared; the next call starts the new one.
378
+ for (const inst of manager.list()) {
379
+ const def = next.manifest.servers[inst.server.name];
380
+ if (!def || descriptionFingerprint(def, inst.variant) !== descriptionFingerprint(inst.server, inst.variant)) {
381
+ await manager.stop(inst.key);
382
+ log(`stopped ${inst.upstream.label} after manifest change`);
383
+ }
384
+ }
385
+ state = next;
386
+ log(`manifest reloaded (${why}): ${Object.keys(state.manifest.servers).length} servers`);
387
+ tell();
388
+ };
389
+ /** config.toml: a broken file keeps the last good settings and is reported in /status. */
390
+ const reloadConfig = (why) => {
391
+ let next;
392
+ try {
393
+ next = loadConfig(env);
394
+ }
395
+ catch (err) {
396
+ configError = err.message;
397
+ log(`config.toml reload skipped (${why}): ${configError}`);
133
398
  return;
134
- clearTimeout(debounce);
135
- debounce = setTimeout(() => void reload("mcp.toml changed"), 300);
136
- });
399
+ }
400
+ configError = undefined;
401
+ const tell = gateway.beforeViewChange();
402
+ folderRules = next.projects;
403
+ tell();
404
+ manager.setIdleUnderPressure(next.mcpd.idleUnderPressure);
405
+ if (next.mcpd.callLog !== Boolean(calls)) {
406
+ if (calls) {
407
+ calls.close();
408
+ calls = undefined;
409
+ }
410
+ else
411
+ calls = createCallLog(files.calls, { onError: (m) => log(m) });
412
+ log(`call log ${calls ? "on" : "off"} (${why})`);
413
+ }
414
+ log(`config.toml reloaded (${why}): ${folderRules.filter((r) => r.mcp || r.mcpDeny).length} folder server sets`);
415
+ };
416
+ let debounce;
417
+ let configDebounce;
418
+ let watcher;
419
+ if (existsSync(p.config)) {
420
+ try {
421
+ watcher = watch(p.config, (_event, file) => {
422
+ if (file === "config.toml") {
423
+ clearTimeout(configDebounce);
424
+ configDebounce = setTimeout(() => reloadConfig("config.toml changed"), 300);
425
+ return;
426
+ }
427
+ if (file !== "mcp.toml")
428
+ return;
429
+ clearTimeout(debounce);
430
+ debounce = setTimeout(() => void reload("mcp.toml changed").catch((e) => log(`reload failed: ${e.message}`)), 300);
431
+ });
432
+ watcher.on("error", (e) => log(`config watch failed: ${e.message}`));
433
+ }
434
+ catch (e) {
435
+ log(`config watch failed: ${e.message}`);
436
+ }
437
+ }
438
+ // SIGHUP reloads the manifest, config.toml and the token (after `turno mcp token rotate`).
439
+ const onHup = () => {
440
+ reloadConfig("SIGHUP");
441
+ const t = readToken(env, (m) => log(m));
442
+ if (t && t !== gwOpts.token) {
443
+ gwOpts.token = t;
444
+ log("token reloaded");
445
+ }
446
+ void reload("SIGHUP").catch((e) => log(`reload failed: ${e.message}`));
447
+ };
448
+ process.on("SIGHUP", onHup);
449
+ const timer = setInterval(() => {
450
+ gateway.pruneSessions(pidAlive);
451
+ manager
452
+ .reap()
453
+ .then((stopped) => {
454
+ for (const s of stopped)
455
+ log(`reaped ${s}`);
456
+ })
457
+ .catch((err) => log(`reap failed: ${err.message}`));
458
+ void sampler.sample(manager.list().map((i) => ({ descKey: i.descKey, pid: i.upstream.pid })));
459
+ }, 15_000);
460
+ timer.unref();
461
+ // /status shows the pressure from the start instead of after the first reap.
462
+ void manager.readPressure().catch(() => { });
463
+ let stopped = false;
464
+ return {
465
+ port,
466
+ gateway,
467
+ manager,
468
+ log,
469
+ async stop() {
470
+ if (stopped)
471
+ return;
472
+ stopped = true;
473
+ clearInterval(timer);
474
+ clearTimeout(debounce);
475
+ clearTimeout(configDebounce);
476
+ watcher?.close();
477
+ process.off("SIGHUP", onHup);
478
+ // Rotated OAuth tokens the secret store refused so far: one last try before they are gone.
479
+ const lost = await Promise.race([
480
+ manager.flushLogins().catch(() => []),
481
+ new Promise((r) => setTimeout(() => r([]), 3000).unref()),
482
+ ]);
483
+ if (lost.length)
484
+ log(`OAuth tokens not saved, a new login is needed for: ${lost.join(", ")} (turno mcp login <server>)`);
485
+ await manager.stopAll();
486
+ await gateway.close();
487
+ releaseLock(env, port);
488
+ calls?.close();
489
+ log("mcpd stopped");
490
+ await log.close();
491
+ },
492
+ };
493
+ }
494
+ catch (err) {
495
+ releaseLock(env, port);
496
+ return fail(err);
137
497
  }
138
- const onHup = () => void reload("SIGHUP");
139
- process.on("SIGHUP", onHup);
140
- const timer = setInterval(() => {
141
- gateway.pruneSessions(processAlive);
142
- manager
143
- .reap()
144
- .then((stopped) => stopped.forEach((s) => log(`reaped ${s}`)))
145
- .catch((err) => log(`reap failed: ${err.message}`));
146
- }, 15_000);
147
- timer.unref();
148
- return {
149
- port,
150
- gateway,
151
- manager,
152
- async stop() {
153
- clearInterval(timer);
154
- clearTimeout(debounce);
155
- watcher?.close();
156
- process.off("SIGHUP", onHup);
157
- await manager.stopAll();
158
- await gateway.close();
159
- log("mcpd stopped");
160
- },
161
- };
162
498
  }
163
499
  //# sourceMappingURL=daemon.js.map
@@ -0,0 +1,39 @@
1
+ import { execFile } from "node:child_process";
2
+ import { realpath } from "node:fs/promises";
3
+ import { promisify } from "node:util";
4
+ const run = promisify(execFile);
5
+ /**
6
+ * The folders a session's server set is matched against (ADR 0031): the session folder, and
7
+ * for a git worktree the main repository it belongs to, the same candidates `turno auto` uses
8
+ * for profile rules. Async (mcpd must not block on git) and cached for 10 s, because an agent
9
+ * opens one session per server at the same time.
10
+ */
11
+ const cache = new Map();
12
+ export function folderRoots(cwd, now = Date.now()) {
13
+ const hit = cache.get(cwd);
14
+ if (hit && now - hit.at < 10_000)
15
+ return hit.value;
16
+ if (cache.size > 500)
17
+ cache.clear();
18
+ const value = compute(cwd);
19
+ cache.set(cwd, { at: now, value });
20
+ return value;
21
+ }
22
+ async function compute(cwd) {
23
+ const own = await realpath(cwd).catch(() => cwd);
24
+ const out = [own];
25
+ try {
26
+ const { stdout } = await run("git", ["-C", cwd, "rev-parse", "--path-format=absolute", "--git-common-dir"], { encoding: "utf8", timeout: 3000 });
27
+ const common = stdout.trim();
28
+ if (common.endsWith("/.git")) {
29
+ const main = await realpath(common.slice(0, -5)).catch(() => common.slice(0, -5));
30
+ if (!out.includes(main))
31
+ out.push(main);
32
+ }
33
+ }
34
+ catch {
35
+ // not a git repository
36
+ }
37
+ return out;
38
+ }
39
+ //# sourceMappingURL=folder-sets.js.map