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
@@ -0,0 +1,269 @@
1
+ import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { DEFAULT_AGENT, adapter, mcpFileFor } from "../agents/index.js";
4
+ import { loadConfig } from "../core/config.js";
5
+ import { paths, tildify } from "../core/paths.js";
6
+ import { agentOf, listProfiles } from "../core/profiles.js";
7
+ import { profileTokenFile, tokenProfile } from "./tokens.js";
8
+ const startFix = {
9
+ label: "turno mcp daemon start",
10
+ run: async (io) => (await import("../cli/commands/mcpd.js")).installDaemonService(io),
11
+ };
12
+ const syncFix = {
13
+ label: "turno mcp sync",
14
+ run: async (io) => (await import("../cli/commands/mcp.js")).command.run(["sync"], io),
15
+ };
16
+ /** Ports the gateway URLs in a synced config file point at. */
17
+ export function syncedPorts(text) {
18
+ return [...new Set([...text.matchAll(/127\.0\.0\.1:(\d+)\/mcp\//g)].map((m) => Number(m[1])))];
19
+ }
20
+ async function serviceChecks(env) {
21
+ const { installedBackend, isUnstablePath } = await import("./service.js");
22
+ const backend = await installedBackend(env);
23
+ if (!backend)
24
+ return [];
25
+ const file = backend.file(env);
26
+ const text = readFileSync(file, "utf8");
27
+ const program = backend.programOf(text);
28
+ const shown = tildify(file, env);
29
+ const out = [];
30
+ const missing = program.filter((a) => a.startsWith("/") && !existsSync(a));
31
+ if (!program.length)
32
+ out.push({ level: "fail", text: `mcpd: ${shown} has no program to run`, fix: startFix });
33
+ else if (missing.length)
34
+ out.push({ level: "fail", text: `mcpd: ${shown} runs ${missing.map((m) => tildify(m, env)).join(", ")}, which no longer exists`, fix: startFix });
35
+ else if (program.some((a) => a.startsWith("/") && isUnstablePath(a))) {
36
+ out.push({ level: "warn", text: `mcpd: ${shown} runs a versioned path (${tildify(program[0], env)}) that breaks when that version is removed`, fix: startFix });
37
+ }
38
+ else
39
+ out.push({ level: "ok", text: `mcpd: ${backend.noun} ${shown}` });
40
+ if (backend.kind === "launchd" && /<key>ProcessType<\/key>\s*<string>Background<\/string>/.test(text)) {
41
+ out.push({ level: "warn", text: "mcpd: the LaunchAgent runs as a Background process, which macOS throttles", fix: startFix });
42
+ }
43
+ return out;
44
+ }
45
+ function tokenCheck(env) {
46
+ const file = paths(env).mcpdToken;
47
+ try {
48
+ const st = statSync(file);
49
+ const uid = process.getuid?.();
50
+ if (uid !== undefined && st.uid !== uid)
51
+ return { level: "fail", text: `mcpd: ${tildify(file, env)} belongs to uid ${st.uid}`, hint: `sudo chown "$(id -un)" ${tildify(file, env)}` };
52
+ if (st.mode & 0o077) {
53
+ return {
54
+ level: "warn",
55
+ text: `mcpd: ${tildify(file, env)} is readable by others (mode ${(st.mode & 0o777).toString(8)})`,
56
+ fix: { label: `chmod 600 ${tildify(file, env)}`, run: async () => (await import("./state.js")).readToken(env, () => { }) },
57
+ };
58
+ }
59
+ return undefined;
60
+ }
61
+ catch {
62
+ return undefined;
63
+ }
64
+ }
65
+ async function portChecks(env) {
66
+ const { answersAsMcpd, daemonFiles, loadConfigSafe, mcpdPort, readLock, readStartError, readToken } = await import("./state.js");
67
+ const { describeOwner, portOwner } = await import("./peerpid.js");
68
+ const out = [];
69
+ const port = mcpdPort(env);
70
+ const token = readToken(env, () => { });
71
+ if (!token) {
72
+ out.push({ level: "info", text: `mcpd: no token at ${tildify(paths(env).mcpdToken, env)}, so it has not started yet`, hint: "turno mcp daemon start" });
73
+ return out;
74
+ }
75
+ const ours = await answersAsMcpd(port, token);
76
+ const owner = await portOwner(port, { env });
77
+ const uid = process.getuid?.();
78
+ if (ours) {
79
+ if (owner?.uid !== undefined && uid !== undefined && owner.uid !== uid) {
80
+ out.push({ level: "fail", text: `mcpd: port ${port} answers with your token but belongs to uid ${owner.uid}`, hint: "turno mcp token rotate" });
81
+ }
82
+ else if (owner?.pid !== undefined && ours.pid !== undefined && owner.pid !== ours.pid) {
83
+ out.push({ level: "warn", text: `mcpd: port ${port} is held by pid ${owner.pid}, but mcpd reports pid ${ours.pid}`, hint: "turno mcp daemon restart" });
84
+ }
85
+ else
86
+ out.push({ level: "ok", text: `mcpd: reachable on port ${port}${ours.pid ? ` (pid ${ours.pid})` : ""}` });
87
+ try {
88
+ const res = await fetch(`http://127.0.0.1:${port}/status`, { headers: { authorization: `Bearer ${token}` }, signal: AbortSignal.timeout(2000) });
89
+ const s = (await res.json());
90
+ if (s.manifest?.error) {
91
+ out.push({
92
+ level: "warn",
93
+ text: `mcpd: mcp.toml is invalid, serving the ${s.manifest.source}: ${s.manifest.error}`,
94
+ manual: `correct ${tildify(paths(env).mcpFile, env)} where the error points, then turno mcp daemon restart`,
95
+ });
96
+ }
97
+ if (s.config?.error)
98
+ out.push({ level: "warn", text: `mcpd: config.toml is invalid, mcpd uses defaults: ${s.config.error}`, hint: "turno config edit" });
99
+ }
100
+ catch {
101
+ /* answered a moment ago; nothing more to add */
102
+ }
103
+ return out;
104
+ }
105
+ const configured = loadConfigSafe(env).config.mcpd.port;
106
+ if (owner) {
107
+ out.push({
108
+ level: "fail",
109
+ text: `mcpd: port ${port} is in use by ${describeOwner(owner)}, so mcpd cannot listen there`,
110
+ manual: "stop that process, or move mcpd: turno config set mcpd.port <port>, then turno mcp sync",
111
+ });
112
+ }
113
+ else
114
+ out.push({ level: "info", text: `mcpd: not reachable on port ${port}`, hint: "turno mcp daemon start" });
115
+ if (configured !== port)
116
+ out.push({ level: "info", text: `mcpd: config.toml says port ${configured}, the last mcpd used ${port}` });
117
+ const e = readStartError(env);
118
+ if (e)
119
+ out.push({ level: "warn", text: `mcpd: last start failed at ${e.at}: ${e.message}`, hint: "turno mcp daemon start" });
120
+ const lock = readLock(env);
121
+ if (lock)
122
+ out.push({ level: "info", text: `mcpd: stale lock from pid ${lock.pid} in ${tildify(daemonFiles(env).lock, env)} (the next start clears it)` });
123
+ return out;
124
+ }
125
+ async function syncedPortCheck(env) {
126
+ const { readManaged } = await import("./sync.js");
127
+ const { mcpdPort } = await import("./state.js");
128
+ const port = mcpdPort(env);
129
+ const off = [];
130
+ const seen = new Set();
131
+ for (const file of Object.keys(readManaged(paths(env).mcpdManaged))) {
132
+ let text;
133
+ try {
134
+ text = readFileSync(file, "utf8");
135
+ }
136
+ catch {
137
+ continue;
138
+ }
139
+ const ports = syncedPorts(text).filter((p) => p !== port);
140
+ if (ports.length) {
141
+ off.push(tildify(file, env));
142
+ for (const p of ports)
143
+ seen.add(p);
144
+ }
145
+ }
146
+ if (!off.length)
147
+ return undefined;
148
+ return {
149
+ level: "warn",
150
+ text: `mcpd: ${off.length} synced config${off.length === 1 ? "" : "s"} point at port ${[...seen].join(", ")} but mcpd uses ${port} (${off.join(", ")})`,
151
+ fix: syncFix,
152
+ };
153
+ }
154
+ const PROFILE_BEARER = /Bearer (tp1\.[a-z0-9][a-z0-9_-]*\.[A-Za-z0-9_-]{43})/g;
155
+ /**
156
+ * The agent config files `turno mcp sync` writes: those mcp-managed.json lists, and every
157
+ * profile's config and the hub's, so a sync from before that file existed (or a lost one)
158
+ * is still checked.
159
+ */
160
+ function syncedFiles(env) {
161
+ const files = new Set();
162
+ try {
163
+ const managed = JSON.parse(readFileSync(paths(env).mcpdManaged, "utf8"));
164
+ if (managed && typeof managed === "object")
165
+ for (const f of Object.keys(managed))
166
+ files.add(f);
167
+ }
168
+ catch {
169
+ /* no mcp-managed.json */
170
+ }
171
+ try {
172
+ const config = loadConfig(env);
173
+ for (const name of listProfiles(config, env))
174
+ files.add(mcpFileFor(name, agentOf(name, config), env));
175
+ }
176
+ catch {
177
+ /* a broken config.toml is reported elsewhere */
178
+ }
179
+ files.add(adapter(DEFAULT_AGENT).mcp.hubFile(env));
180
+ return [...files];
181
+ }
182
+ /**
183
+ * Synced configs and launcher token files that mcpd would refuse (ADR 0023): configs written
184
+ * before per-profile tokens still carry the master token, and every derived token dies with a
185
+ * `turno mcp token rotate`. Both are fixed by `turno mcp sync`. Works without mcp-managed.json.
186
+ * Never prints a token.
187
+ */
188
+ export function tokenSyncCheck(env) {
189
+ const p = paths(env);
190
+ let master;
191
+ try {
192
+ master = readFileSync(p.mcpdToken, "utf8").trim();
193
+ }
194
+ catch {
195
+ return undefined;
196
+ }
197
+ if (!master)
198
+ return undefined;
199
+ const legacy = [];
200
+ const stale = [];
201
+ for (const file of syncedFiles(env)) {
202
+ let text;
203
+ try {
204
+ text = readFileSync(file, "utf8");
205
+ }
206
+ catch {
207
+ continue;
208
+ }
209
+ if (text.includes(master))
210
+ legacy.push(tildify(file, env));
211
+ else if ([...text.matchAll(PROFILE_BEARER)].some((m) => !tokenProfile(master, m[1])))
212
+ stale.push(tildify(file, env));
213
+ }
214
+ const dir = dirname(profileTokenFile("x", env));
215
+ let names = [];
216
+ try {
217
+ names = readdirSync(dir).filter((f) => f.endsWith(".token"));
218
+ }
219
+ catch {
220
+ /* no token files */
221
+ }
222
+ for (const f of names) {
223
+ const profile = f.slice(0, -".token".length);
224
+ try {
225
+ if (tokenProfile(master, readFileSync(join(dir, f), "utf8").trim()) !== profile)
226
+ stale.push(tildify(join(dir, f), env));
227
+ }
228
+ catch {
229
+ /* unreadable: the launcher sends nothing and mcpd says why */
230
+ }
231
+ }
232
+ const s = (n) => (n === 1 ? "" : "s");
233
+ if (legacy.length) {
234
+ return {
235
+ level: "warn",
236
+ text: `mcpd: ${legacy.length} synced config${s(legacy.length)} still send${legacy.length === 1 ? "s" : ""} the master token, which mcpd no longer accepts for MCP sessions (${legacy.join(", ")})`,
237
+ fix: syncFix,
238
+ };
239
+ }
240
+ if (stale.length) {
241
+ return {
242
+ level: "warn",
243
+ text: `mcpd: ${stale.length} profile token${s(stale.length)} predate${stale.length === 1 ? "s" : ""} the current gateway token (${stale.join(", ")})`,
244
+ fix: syncFix,
245
+ };
246
+ }
247
+ return undefined;
248
+ }
249
+ export async function mcpdChecks(env) {
250
+ const out = [];
251
+ try {
252
+ out.push(...(await serviceChecks(env)));
253
+ }
254
+ catch (err) {
255
+ out.push({ level: "warn", text: `mcpd: cannot read the service file: ${err.message}`, hint: "turno mcp daemon start" });
256
+ }
257
+ const t = tokenCheck(env);
258
+ if (t)
259
+ out.push(t);
260
+ out.push(...(await portChecks(env)));
261
+ const s = await syncedPortCheck(env);
262
+ if (s)
263
+ out.push(s);
264
+ const tk = tokenSyncCheck(env);
265
+ if (tk)
266
+ out.push(tk);
267
+ return out;
268
+ }
269
+ //# sourceMappingURL=health.js.map