turno-cli 0.2.0 → 0.4.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.
- package/CHANGELOG.md +266 -0
- package/README.md +134 -42
- package/dist/accounts/audit.js +27 -15
- package/dist/accounts/builtin.js +35 -7
- package/dist/accounts/delegate.js +30 -7
- package/dist/accounts/policy.js +4 -2
- package/dist/accounts/registry.js +3 -1
- package/dist/agents/adapter.js +22 -0
- package/dist/agents/claude.js +54 -3
- package/dist/agents/codex.js +94 -6
- package/dist/agents/copilot.js +19 -2
- package/dist/agents/cursor.js +19 -2
- package/dist/agents/index.js +1 -1
- package/dist/agents/snippets.js +37 -10
- package/dist/cli/args.js +32 -15
- package/dist/cli/commands/accounts.js +29 -7
- package/dist/cli/commands/auto.js +50 -12
- package/dist/cli/commands/browser-do-session.js +170 -0
- package/dist/cli/commands/browser-do.js +740 -0
- package/dist/cli/commands/browser-flows.js +300 -0
- package/dist/cli/commands/browser-identity.js +952 -0
- package/dist/cli/commands/browser-live.js +118 -0
- package/dist/cli/commands/browser-session.js +102 -0
- package/dist/cli/commands/browser-vault.js +193 -0
- package/dist/cli/commands/browser.js +303 -62
- package/dist/cli/commands/complete.js +13 -3
- package/dist/cli/commands/completion.js +4 -0
- package/dist/cli/commands/config.js +90 -13
- package/dist/cli/commands/demo.js +36 -7
- package/dist/cli/commands/doctor.js +170 -36
- package/dist/cli/commands/identity.js +101 -12
- package/dist/cli/commands/init.js +42 -10
- package/dist/cli/commands/limit.js +65 -9
- package/dist/cli/commands/mcp-call.js +240 -0
- package/dist/cli/commands/mcp-oauth.js +33 -6
- package/dist/cli/commands/mcp-observe.js +128 -23
- package/dist/cli/commands/mcp-share.js +201 -0
- package/dist/cli/commands/mcp.js +203 -42
- package/dist/cli/commands/mcpd.js +76 -12
- package/dist/cli/commands/open.js +26 -3
- package/dist/cli/commands/profile.js +212 -30
- package/dist/cli/commands/resolve.js +8 -1
- package/dist/cli/commands/run.js +24 -4
- package/dist/cli/commands/secret.js +23 -38
- package/dist/cli/commands/session.js +16 -5
- package/dist/cli/commands/sessions.js +7 -1
- package/dist/cli/commands/setup.js +83 -11
- package/dist/cli/commands/status.js +67 -15
- package/dist/cli/commands/statusline.js +7 -2
- package/dist/cli/commands/ui.js +17 -5
- package/dist/cli/commands/undo.js +39 -7
- package/dist/cli/commands/uninstall.js +24 -5
- package/dist/cli/commands/upgrade.js +113 -76
- package/dist/cli/commands/usage.js +11 -10
- package/dist/cli/commands/version.js +20 -8
- package/dist/cli/commands/which.js +5 -3
- package/dist/cli/diagnostics.js +184 -0
- package/dist/cli/exit.js +65 -0
- package/dist/cli/group.js +20 -8
- package/dist/cli/help.js +6 -1
- package/dist/cli/hints.js +30 -0
- package/dist/cli/io.js +24 -8
- package/dist/cli/launch.js +31 -2
- package/dist/cli/launchers.js +25 -10
- package/dist/cli/main.js +52 -18
- package/dist/cli/optional.js +144 -0
- package/dist/cli/output.js +123 -0
- package/dist/cli/picker.js +17 -5
- package/dist/cli/registry.js +20 -2
- package/dist/cli/safe.js +87 -0
- package/dist/cli/secret-input.js +63 -0
- package/dist/cli/security.js +31 -9
- package/dist/cli/style.js +35 -5
- package/dist/cli/topics.js +155 -0
- package/dist/core/config.js +39 -3
- package/dist/core/fsutil.js +2 -10
- package/dist/core/journal.js +19 -11
- package/dist/core/manifest.js +28 -1
- package/dist/core/paths.js +6 -112
- package/dist/core/profiles.js +28 -8
- package/dist/core/resolve.js +13 -3
- package/dist/core/secrets.js +4 -154
- package/dist/core/state-file.js +107 -0
- package/dist/core/update.js +14 -11
- package/dist/core/version.js +57 -10
- package/dist/demo/server.js +10 -2
- package/dist/mcpd/cache.js +6 -7
- package/dist/mcpd/calllog.js +10 -9
- package/dist/mcpd/client-names.js +8 -13
- package/dist/mcpd/connectors.js +42 -3
- package/dist/mcpd/daemon.js +29 -8
- package/dist/mcpd/folder-sets.js +5 -2
- package/dist/mcpd/gateway.js +57 -29
- package/dist/mcpd/health.js +70 -26
- package/dist/mcpd/importer.js +106 -9
- package/dist/mcpd/instances.js +70 -32
- package/dist/mcpd/jsonrpc.js +3 -1
- package/dist/mcpd/middleware.js +156 -0
- package/dist/mcpd/modern.js +57 -19
- package/dist/mcpd/oauth.js +71 -17
- package/dist/mcpd/peerpid.js +63 -17
- package/dist/mcpd/pressure.js +4 -1
- package/dist/mcpd/rss.js +34 -19
- package/dist/mcpd/service.js +28 -4
- package/dist/mcpd/share.js +217 -0
- package/dist/mcpd/state.js +16 -16
- package/dist/mcpd/sync.js +22 -12
- package/dist/mcpd/systemd.js +4 -1
- package/dist/mcpd/tokens.js +4 -8
- package/dist/mcpd/upstream.js +12 -5
- package/dist/statusline/cost.js +17 -4
- package/dist/statusline/input.js +15 -3
- package/dist/ui/fonts.js +1 -1
- package/dist/ui/settings.js +6 -1
- package/npm-shrinkwrap.json +12 -3
- package/package.json +7 -6
package/dist/core/secrets.js
CHANGED
|
@@ -1,159 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { findOnPath } from "./paths.js";
|
|
3
|
-
export class SecretError extends Error {
|
|
4
|
-
constructor(ref, why) {
|
|
5
|
-
super(ref ? `cannot resolve ${ref}: ${why}` : why);
|
|
6
|
-
this.name = "SecretError";
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
/** The one-line error when the machine has neither the Keychain nor libsecret. */
|
|
10
|
-
export const NO_SECRET_STORE = "no secret store found: turno uses the macOS Keychain (security) or libsecret (secret-tool, package libsecret-tools); install one, or use env:NAME references in mcp.toml";
|
|
11
|
-
/** Run a command with optional stdin; never rejects (a missing binary is code 127). */
|
|
12
|
-
function exec(cmd, args, env, input, timeout = 10_000) {
|
|
13
|
-
return new Promise((resolve) => {
|
|
14
|
-
let child;
|
|
15
|
-
try {
|
|
16
|
-
child = spawn(cmd, args, { env, stdio: ["pipe", "pipe", "pipe"], timeout });
|
|
17
|
-
}
|
|
18
|
-
catch (err) {
|
|
19
|
-
resolve({ code: 127, stdout: "", stderr: err.message });
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
const out = [];
|
|
23
|
-
const err = [];
|
|
24
|
-
child.stdout.on("data", (c) => out.push(c));
|
|
25
|
-
child.stderr.on("data", (c) => err.push(c));
|
|
26
|
-
let done = false;
|
|
27
|
-
const finish = (r) => {
|
|
28
|
-
if (done)
|
|
29
|
-
return;
|
|
30
|
-
done = true;
|
|
31
|
-
resolve(r);
|
|
32
|
-
};
|
|
33
|
-
child.on("error", (e) => finish({ code: e.code === "ENOENT" ? 127 : 1, stdout: "", stderr: e.message }));
|
|
34
|
-
child.on("close", (code) => finish({ code: code ?? 1, stdout: Buffer.concat(out).toString("utf8"), stderr: Buffer.concat(err).toString("utf8") }));
|
|
35
|
-
child.stdin.on("error", () => {
|
|
36
|
-
/* the process exited before reading; its exit code tells */
|
|
37
|
-
});
|
|
38
|
-
child.stdin.end(input ?? "");
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
function firstLine(text) {
|
|
42
|
-
return text.trim().split("\n")[0] ?? "";
|
|
43
|
-
}
|
|
44
|
-
/** Service names travel as arguments and in `security -i` lines: keep them to one plain line. */
|
|
45
|
-
function checkService(service) {
|
|
46
|
-
if (!service || /[\0\r\n]/.test(service))
|
|
47
|
-
throw new SecretError("", `invalid secret name ${JSON.stringify(service)}`);
|
|
48
|
-
}
|
|
49
|
-
/** Quote one word for a `security -i` command line (double quotes, backslash escapes). */
|
|
50
|
-
export function securityQuote(word) {
|
|
51
|
-
return `"${word.replace(/[\\"]/g, (c) => `\\${c}`)}"`;
|
|
52
|
-
}
|
|
53
|
-
/** The line `security -i` reads on stdin to store an item; exported for tests. */
|
|
54
|
-
export function keychainAddLine(service, value, account = "turno") {
|
|
55
|
-
if (/[\0\r\n]/.test(value))
|
|
56
|
-
throw new SecretError("", "the value has a line break or NUL byte; the Keychain store reads one line");
|
|
57
|
-
return `add-generic-password -U -a ${securityQuote(account)} -s ${securityQuote(service)} -w ${securityQuote(value)} -T /usr/bin/security\n`;
|
|
58
|
-
}
|
|
59
|
-
/** Exit code of `security` when the item does not exist (errSecItemNotFound). */
|
|
60
|
-
const KEYCHAIN_NOT_FOUND = 44;
|
|
61
|
-
function keychainStore(bin, env) {
|
|
62
|
-
return {
|
|
63
|
-
kind: "keychain",
|
|
64
|
-
label: "Keychain",
|
|
65
|
-
async get(service) {
|
|
66
|
-
checkService(service);
|
|
67
|
-
const r = await exec(bin, ["find-generic-password", "-s", service, "-w"], env);
|
|
68
|
-
if (r.code === 0)
|
|
69
|
-
return r.stdout.replace(/\n$/, "");
|
|
70
|
-
if (r.code === KEYCHAIN_NOT_FOUND)
|
|
71
|
-
return undefined;
|
|
72
|
-
throw new SecretError(`keychain:${service}`, firstLine(r.stderr) || `security exited with ${r.code}`);
|
|
73
|
-
},
|
|
74
|
-
async has(service) {
|
|
75
|
-
checkService(service);
|
|
76
|
-
// Without -w this reads the item's attributes, never its value.
|
|
77
|
-
return (await exec(bin, ["find-generic-password", "-s", service], env)).code === 0;
|
|
78
|
-
},
|
|
79
|
-
async set(service, value) {
|
|
80
|
-
checkService(service);
|
|
81
|
-
// `security -i` reads commands from stdin, so the value never shows in `ps`. The output is
|
|
82
|
-
// not echoed back: an error line could quote the command.
|
|
83
|
-
const r = await exec(bin, ["-i"], env, keychainAddLine(service, value), 60_000);
|
|
84
|
-
if (r.code !== 0)
|
|
85
|
-
throw new SecretError("", `the Keychain did not store ${service} (security exited with ${r.code})`);
|
|
86
|
-
},
|
|
87
|
-
async remove(service) {
|
|
88
|
-
checkService(service);
|
|
89
|
-
const r = await exec(bin, ["delete-generic-password", "-s", service], env);
|
|
90
|
-
if (r.code === 0)
|
|
91
|
-
return true;
|
|
92
|
-
if (r.code === KEYCHAIN_NOT_FOUND)
|
|
93
|
-
return false;
|
|
94
|
-
throw new SecretError("", `the Keychain did not delete ${service}: ${firstLine(r.stderr) || `exit ${r.code}`}`);
|
|
95
|
-
},
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
/** libsecret attributes of an item: service=turno, account=<name>. */
|
|
99
|
-
function attrs(service) {
|
|
100
|
-
return ["service", "turno", "account", service];
|
|
101
|
-
}
|
|
102
|
-
function libsecretStore(bin, env) {
|
|
103
|
-
const lookup = (service) => exec(bin, ["lookup", ...attrs(service)], env);
|
|
104
|
-
return {
|
|
105
|
-
kind: "libsecret",
|
|
106
|
-
label: "Secret Service",
|
|
107
|
-
async get(service) {
|
|
108
|
-
checkService(service);
|
|
109
|
-
const r = await lookup(service);
|
|
110
|
-
if (r.code === 0)
|
|
111
|
-
return r.stdout.replace(/\n$/, "");
|
|
112
|
-
if (!r.stderr.trim())
|
|
113
|
-
return undefined; // not found: exit 1, nothing on stderr
|
|
114
|
-
throw new SecretError(`keychain:${service}`, firstLine(r.stderr));
|
|
115
|
-
},
|
|
116
|
-
async has(service) {
|
|
117
|
-
checkService(service);
|
|
118
|
-
return (await lookup(service)).code === 0;
|
|
119
|
-
},
|
|
120
|
-
async set(service, value) {
|
|
121
|
-
checkService(service);
|
|
122
|
-
// secret-tool reads the value from stdin when stdin is not a terminal.
|
|
123
|
-
const r = await exec(bin, ["store", `--label=turno: ${service}`, ...attrs(service)], env, value, 60_000);
|
|
124
|
-
if (r.code !== 0)
|
|
125
|
-
throw new SecretError("", `secret-tool did not store ${service}: ${firstLine(r.stderr) || `exit ${r.code}`}`);
|
|
126
|
-
},
|
|
127
|
-
async remove(service) {
|
|
128
|
-
checkService(service);
|
|
129
|
-
if (!(await this.has(service)))
|
|
130
|
-
return false;
|
|
131
|
-
const r = await exec(bin, ["clear", ...attrs(service)], env);
|
|
132
|
-
if (r.code !== 0)
|
|
133
|
-
throw new SecretError("", `secret-tool did not delete ${service}: ${firstLine(r.stderr) || `exit ${r.code}`}`);
|
|
134
|
-
return true;
|
|
135
|
-
},
|
|
136
|
-
};
|
|
137
|
-
}
|
|
1
|
+
import { NO_SECRET_STORE, SecretError, requireSecretStore, secretStore } from "turno-core/secrets";
|
|
138
2
|
/**
|
|
139
|
-
* The
|
|
140
|
-
*
|
|
3
|
+
* The store itself (macOS Keychain, Linux Secret Service) lives in turno-core, shared with
|
|
4
|
+
* turno-browser; this module keeps what mcp.toml references need on top of it.
|
|
141
5
|
*/
|
|
142
|
-
export
|
|
143
|
-
if (platform === "darwin") {
|
|
144
|
-
const bin = findOnPath("security", env);
|
|
145
|
-
return bin ? keychainStore(bin, env) : undefined;
|
|
146
|
-
}
|
|
147
|
-
const bin = findOnPath("secret-tool", env);
|
|
148
|
-
return bin ? libsecretStore(bin, env) : undefined;
|
|
149
|
-
}
|
|
150
|
-
/** The store, or a SecretError with the one-line hint. */
|
|
151
|
-
export function requireSecretStore(env, platform = process.platform) {
|
|
152
|
-
const store = secretStore(env, platform);
|
|
153
|
-
if (!store)
|
|
154
|
-
throw new SecretError("", NO_SECRET_STORE);
|
|
155
|
-
return store;
|
|
156
|
-
}
|
|
6
|
+
export { NO_SECRET_STORE, SecretError, keychainAddLine, requireSecretStore, secretStore, securityQuote, } from "turno-core/secrets";
|
|
157
7
|
/** `${VAR}`, `${VAR:-default}` and Cursor's `${env:VAR}` anywhere inside a string. */
|
|
158
8
|
const VAR_REF = /\$\{(env:)?([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}/g;
|
|
159
9
|
/** Cursor's own placeholders: not environment variables, so they stay as written. */
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading turno's own state files: JSON that turno wrote earlier (locks, caches, the
|
|
3
|
+
* managed-entries list, journal manifests, audit and call log lines). A file another program or
|
|
4
|
+
* a crash left half-written, a hand edit, or a newer turno's format must never become a cast that
|
|
5
|
+
* crashes later: each reader passes a small guard for the shape it needs and the format version
|
|
6
|
+
* it understands, and gets the value, or a reason it cannot have it.
|
|
7
|
+
*
|
|
8
|
+
* Versions: an object may carry a top-level `version` number. A file without one is format 1
|
|
9
|
+
* (everything written before versioning). A file whose version is above the reader's is from a
|
|
10
|
+
* newer turno and is not read. Keep this file free of heavy imports: the status line uses it.
|
|
11
|
+
*/
|
|
12
|
+
import { readFileSync } from "node:fs";
|
|
13
|
+
/** A state file that exists but cannot be used: main prints its message on one line. */
|
|
14
|
+
export class StateError extends Error {
|
|
15
|
+
problem;
|
|
16
|
+
constructor(message, problem) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.problem = problem;
|
|
19
|
+
this.name = "StateError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** The format version of a parsed value: its `version` field, 1 without one. */
|
|
23
|
+
function formatVersion(v) {
|
|
24
|
+
const n = typeof v === "object" && v !== null && !Array.isArray(v) ? v.version : undefined;
|
|
25
|
+
return typeof n === "number" && Number.isInteger(n) && n > 0 ? n : 1;
|
|
26
|
+
}
|
|
27
|
+
/** Check parsed JSON against a guard and a version; `label` names it in the message. */
|
|
28
|
+
function checkState(v, guard, version, label) {
|
|
29
|
+
const found = formatVersion(v);
|
|
30
|
+
if (found > version)
|
|
31
|
+
return {
|
|
32
|
+
ok: false,
|
|
33
|
+
problem: "newer",
|
|
34
|
+
message: `${label} is format ${found}, from a newer turno (this one reads up to ${version})`,
|
|
35
|
+
};
|
|
36
|
+
if (!guard(v))
|
|
37
|
+
return { ok: false, problem: "shape", message: `${label} does not hold what turno expects` };
|
|
38
|
+
return { ok: true, value: v };
|
|
39
|
+
}
|
|
40
|
+
/** Parse JSON text (one file, or one line of a log) and check it. */
|
|
41
|
+
export function parseState(text, guard, version, label) {
|
|
42
|
+
let v;
|
|
43
|
+
try {
|
|
44
|
+
v = JSON.parse(text);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return { ok: false, problem: "corrupt", message: `${label} is not valid JSON` };
|
|
48
|
+
}
|
|
49
|
+
return checkState(v, guard, version, label);
|
|
50
|
+
}
|
|
51
|
+
/** Read and check a state file, saying what went wrong when it cannot be used. */
|
|
52
|
+
export function readStateResult(file, guard, version = 1) {
|
|
53
|
+
let text;
|
|
54
|
+
try {
|
|
55
|
+
text = readFileSync(file, "utf8");
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
const code = err.code;
|
|
59
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
60
|
+
return { ok: false, problem: "missing", message: `${file} does not exist` };
|
|
61
|
+
return { ok: false, problem: "unreadable", message: `${file} cannot be read (${code ?? err.message})` };
|
|
62
|
+
}
|
|
63
|
+
return parseState(text, guard, version, file);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The value of a state file, or undefined when it is missing or cannot be used. For caches and
|
|
67
|
+
* hints, where the safe default is to act as if the file were not there.
|
|
68
|
+
*/
|
|
69
|
+
export function readState(file, guard, version = 1) {
|
|
70
|
+
const r = readStateResult(file, guard, version);
|
|
71
|
+
return r.ok ? r.value : undefined;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The value of a state file, undefined when it is missing, and a StateError (one clear line that
|
|
75
|
+
* names the file and says to fix or remove it) when it exists but cannot be used. For state
|
|
76
|
+
* whose loss would make turno act wrongly, such as the list of entries it may remove.
|
|
77
|
+
*/
|
|
78
|
+
export function requireState(file, guard, version = 1) {
|
|
79
|
+
const r = readStateResult(file, guard, version);
|
|
80
|
+
if (r.ok)
|
|
81
|
+
return r.value;
|
|
82
|
+
if (r.problem === "missing")
|
|
83
|
+
return undefined;
|
|
84
|
+
const fix = r.problem === "newer" ? "upgrade turno (turno upgrade)" : "fix it by hand or remove it";
|
|
85
|
+
throw new StateError(`${r.message}; ${fix}`, r.problem);
|
|
86
|
+
}
|
|
87
|
+
// Small guards shared by the readers ------------------------------------------------------------
|
|
88
|
+
export const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
89
|
+
export const isStringArray = (v) => Array.isArray(v) && v.every((x) => typeof x === "string");
|
|
90
|
+
/** An object whose every value passes `each` (a map such as `file -> names`). */
|
|
91
|
+
export function recordOf(each) {
|
|
92
|
+
return (v) => isRecord(v) && Object.values(v).every(each);
|
|
93
|
+
}
|
|
94
|
+
/** An object with these fields of these types (others are allowed, as newer files add fields). */
|
|
95
|
+
export function hasFields(v, fields) {
|
|
96
|
+
if (!isRecord(v))
|
|
97
|
+
return false;
|
|
98
|
+
return Object.entries(fields).every(([k, t]) => {
|
|
99
|
+
const x = v[k];
|
|
100
|
+
if (t === "array")
|
|
101
|
+
return Array.isArray(x);
|
|
102
|
+
if (t === "object")
|
|
103
|
+
return isRecord(x);
|
|
104
|
+
return typeof x === t;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=state-file.js.map
|
package/dist/core/update.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { readFileSync } from "node:fs";
|
|
3
2
|
import { join } from "node:path";
|
|
4
3
|
import { paths, writePrivateFile } from "./paths.js";
|
|
5
4
|
import { compareVersions, packageInfo } from "./version.js";
|
|
5
|
+
import { hasFields, readState } from "./state-file.js";
|
|
6
6
|
const DAY = 24 * 3600 * 1000;
|
|
7
7
|
/** Strict semver 2.0.0 (no leading v, no ranges, no URLs): what may follow `turno-cli@`. */
|
|
8
8
|
const SEMVER = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
|
|
@@ -12,21 +12,20 @@ export function isSemver(v) {
|
|
|
12
12
|
function file(env) {
|
|
13
13
|
return join(paths(env).state, "update.json");
|
|
14
14
|
}
|
|
15
|
+
const isUpdateState = (v) => hasFields(v, { checkedAt: "number", channel: "string" });
|
|
15
16
|
function readUpdateState(env) {
|
|
16
|
-
|
|
17
|
-
return JSON.parse(readFileSync(file(env), "utf8"));
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
return undefined;
|
|
21
|
-
}
|
|
17
|
+
return readState(file(env), isUpdateState);
|
|
22
18
|
}
|
|
23
19
|
function writeUpdateState(env, s) {
|
|
24
20
|
writePrivateFile(file(env), `${JSON.stringify(s)}\n`, env);
|
|
25
21
|
}
|
|
26
22
|
const truthy = (v) => v !== undefined && v !== "" && v !== "0" && v.toLowerCase() !== "false";
|
|
27
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Why the environment turns update checks off: TURNO_NO_UPDATE_CHECK, TURNO_NO_UPDATE_NOTIFIER
|
|
25
|
+
* (gh's GH_NO_UPDATE_NOTIFIER), NO_UPDATE_NOTIFIER, DO_NOT_TRACK, CI.
|
|
26
|
+
*/
|
|
28
27
|
export function envOptOut(env) {
|
|
29
|
-
if (
|
|
28
|
+
if ([env.TURNO_NO_UPDATE_CHECK, env.TURNO_NO_UPDATE_NOTIFIER, env.NO_UPDATE_NOTIFIER, env.DO_NOT_TRACK].some(truthy)) {
|
|
30
29
|
return "turned off by the environment";
|
|
31
30
|
}
|
|
32
31
|
if (truthy(env.CI))
|
|
@@ -35,7 +34,7 @@ export function envOptOut(env) {
|
|
|
35
34
|
}
|
|
36
35
|
/**
|
|
37
36
|
* Why update checks are off, or undefined when they run. Off for unpublished (private)
|
|
38
|
-
* copies, in CI, with TURNO_NO_UPDATE_CHECK, NO_UPDATE_NOTIFIER or DO_NOT_TRACK, and with
|
|
37
|
+
* copies, in CI, with TURNO_NO_UPDATE_CHECK, TURNO_NO_UPDATE_NOTIFIER, NO_UPDATE_NOTIFIER or DO_NOT_TRACK, and with
|
|
39
38
|
* `update_check = false` in config.toml.
|
|
40
39
|
*/
|
|
41
40
|
export function updateChecksOff(env, configured = true) {
|
|
@@ -55,7 +54,11 @@ export function registryUrl(env, name, channel) {
|
|
|
55
54
|
/** Ask the registry for the version of a dist-tag. Sends nothing but the package name. */
|
|
56
55
|
export async function fetchLatest(env, channel = "latest", doFetch = fetch) {
|
|
57
56
|
const { name, version } = packageInfo();
|
|
58
|
-
const state = {
|
|
57
|
+
const state = {
|
|
58
|
+
checkedAt: Date.now(),
|
|
59
|
+
channel,
|
|
60
|
+
...(readUpdateState(env)?.notifiedAt ? { notifiedAt: readUpdateState(env).notifiedAt } : {}),
|
|
61
|
+
};
|
|
59
62
|
try {
|
|
60
63
|
const res = await doFetch(registryUrl(env, name, channel), {
|
|
61
64
|
headers: { accept: "application/json", "user-agent": `turno/${version}` },
|
package/dist/core/version.js
CHANGED
|
@@ -11,6 +11,41 @@ export function packageInfo() {
|
|
|
11
11
|
cached = { name: pkg.name, version: pkg.version, private: Boolean(pkg.private), root: root.replace(/\/$/, "") };
|
|
12
12
|
return cached;
|
|
13
13
|
}
|
|
14
|
+
/** The runner that executes turno from its cache for one run, and the command that runs the newest release. */
|
|
15
|
+
export const ONE_OFF_RUNNERS = {
|
|
16
|
+
npx: { runner: "npx", latest: "npx turno-cli@latest", install: "npm install --global turno-cli" },
|
|
17
|
+
"pnpm-dlx": { runner: "pnpm dlx", latest: "pnpm dlx turno-cli@latest", install: "pnpm add --global turno-cli" },
|
|
18
|
+
bunx: { runner: "bunx", latest: "bunx turno-cli@latest", install: "bun add --global turno-cli" },
|
|
19
|
+
};
|
|
20
|
+
function oneOffMethod(path) {
|
|
21
|
+
const parts = path.split(sep);
|
|
22
|
+
// npx: <npm cache>/_npx/<hash>/node_modules/<pkg>.
|
|
23
|
+
if (parts.includes("_npx"))
|
|
24
|
+
return "npx";
|
|
25
|
+
// pnpm dlx: <pnpm cache>/dlx/<hash>/<stamp>/node_modules/... (~/.cache/pnpm, ~/Library/Caches/pnpm).
|
|
26
|
+
const dlx = parts.indexOf("dlx");
|
|
27
|
+
if (dlx > 0 && (parts[dlx - 1] === "pnpm" || parts.includes(".pnpm") || parts.includes(".pacquet")))
|
|
28
|
+
return "pnpm-dlx";
|
|
29
|
+
// bunx: $TMPDIR/bunx-<uid>-<pkg>@<version>/node_modules/<pkg>.
|
|
30
|
+
if (parts.some((p) => p.startsWith("bunx-")))
|
|
31
|
+
return "bunx";
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
/** "npx", "pnpm dlx" or "bunx" when turno runs from that runner's cache, which it may delete at any time. */
|
|
35
|
+
export function oneOffRunner(path) {
|
|
36
|
+
const m = oneOffMethod(path);
|
|
37
|
+
return m ? ONE_OFF_RUNNERS[m].runner : undefined;
|
|
38
|
+
}
|
|
39
|
+
export function isOneOff(method) {
|
|
40
|
+
return method in ONE_OFF_RUNNERS;
|
|
41
|
+
}
|
|
42
|
+
/** The lockfile each package manager keeps next to its global node_modules. */
|
|
43
|
+
const LOCKFILES = [
|
|
44
|
+
["bun.lock", "bun"],
|
|
45
|
+
["bun.lockb", "bun"],
|
|
46
|
+
["yarn.lock", "yarn"],
|
|
47
|
+
["pnpm-lock.yaml", "pnpm"],
|
|
48
|
+
];
|
|
14
49
|
/** How the running turno was installed, from where its files are. */
|
|
15
50
|
export function detectInstall(root = packageInfo().root) {
|
|
16
51
|
let real = root;
|
|
@@ -25,15 +60,24 @@ export function detectInstall(root = packageInfo().root) {
|
|
|
25
60
|
if (parts.includes("Cellar") && parts.includes("node_modules"))
|
|
26
61
|
return { method: "brew", root: real };
|
|
27
62
|
if (parts.includes("node_modules")) {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
: real
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
63
|
+
const once = oneOffMethod(real);
|
|
64
|
+
if (once)
|
|
65
|
+
return { method: once, root: real };
|
|
66
|
+
if (real.includes(`${sep}.volta${sep}`))
|
|
67
|
+
return { method: "volta", root: real };
|
|
68
|
+
// The folder that holds the outermost node_modules: yarn, bun and pnpm keep their lockfile
|
|
69
|
+
// there, wherever a custom prefix (--global-folder, BUN_INSTALL, PNPM_HOME) put it.
|
|
70
|
+
const top = parts.slice(0, parts.indexOf("node_modules")).join(sep) || sep;
|
|
71
|
+
const locked = LOCKFILES.find(([file]) => existsSync(join(top, file)));
|
|
72
|
+
if (locked)
|
|
73
|
+
return { method: locked[1], root: real };
|
|
74
|
+
const method = real.includes(`${sep}.bun${sep}`)
|
|
75
|
+
? "bun"
|
|
76
|
+
: real.includes(`${sep}pnpm${sep}`) || parts.includes(".pnpm")
|
|
77
|
+
? "pnpm"
|
|
78
|
+
: real.includes(`${sep}yarn${sep}`) || real.includes(`${sep}.yarn${sep}`)
|
|
79
|
+
? "yarn"
|
|
80
|
+
: "npm";
|
|
37
81
|
return { method, root: real };
|
|
38
82
|
}
|
|
39
83
|
for (let dir = real; dir !== dirname(dir); dir = dirname(dir)) {
|
|
@@ -58,7 +102,10 @@ export function upgradeCommand(install, name, spec) {
|
|
|
58
102
|
return [["volta", "install", target]];
|
|
59
103
|
case "brew":
|
|
60
104
|
// The formula follows the newest release; brew cannot install an arbitrary version.
|
|
61
|
-
return [
|
|
105
|
+
return [
|
|
106
|
+
["brew", "update"],
|
|
107
|
+
["brew", "upgrade", "turno"],
|
|
108
|
+
];
|
|
62
109
|
case "source":
|
|
63
110
|
return [
|
|
64
111
|
["git", "-C", install.repo, "pull", "--ff-only"],
|
package/dist/demo/server.js
CHANGED
|
@@ -11,8 +11,16 @@ const account = process.env.DEMO_ACCOUNT ?? "shared";
|
|
|
11
11
|
const startedAt = new Date().toISOString();
|
|
12
12
|
let calls = 0;
|
|
13
13
|
const tools = [
|
|
14
|
-
{
|
|
15
|
-
|
|
14
|
+
{
|
|
15
|
+
name: "whoami",
|
|
16
|
+
description: `Which ${name} process answered: account, pid, start time and calls served`,
|
|
17
|
+
inputSchema: { type: "object", properties: {} },
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
name: "echo",
|
|
21
|
+
description: "Return the text it is given",
|
|
22
|
+
inputSchema: { type: "object", properties: { text: { type: "string" } } },
|
|
23
|
+
},
|
|
16
24
|
];
|
|
17
25
|
const server = new Server({ name: `turno-demo-${name}`, version: "1.0.0" }, { capabilities: { tools: {} }, instructions: `A demo server of turno (${name}).` });
|
|
18
26
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools }));
|
package/dist/mcpd/cache.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import { mkdirSync,
|
|
2
|
+
import { mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { PRIVATE_DIR_MODE, PRIVATE_FILE_MODE } from "../core/paths.js";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
+
import { hasFields, readState } from "../core/state-file.js";
|
|
5
6
|
export function fingerprint(parts) {
|
|
6
7
|
return createHash("sha256").update(JSON.stringify(parts)).digest("hex").slice(0, 16);
|
|
7
8
|
}
|
|
9
|
+
const isDescription = (v) => hasFields(v, { fingerprint: "string", serverInfo: "object", capabilities: "object", updatedAt: "number" });
|
|
8
10
|
export class DescriptionCache {
|
|
9
11
|
dir;
|
|
10
12
|
memory = new Map();
|
|
@@ -22,13 +24,10 @@ export class DescriptionCache {
|
|
|
22
24
|
peek(key) {
|
|
23
25
|
let d = this.memory.get(key);
|
|
24
26
|
if (!d) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.memory.set(key, d);
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
27
|
+
d = readState(this.file(key), isDescription);
|
|
28
|
+
if (!d)
|
|
30
29
|
return undefined;
|
|
31
|
-
|
|
30
|
+
this.memory.set(key, d);
|
|
32
31
|
}
|
|
33
32
|
return d;
|
|
34
33
|
}
|
package/dist/mcpd/calllog.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { closeSync, existsSync, fstatSync, mkdirSync, openSync, readSync, renameSync, statSync, writeSync } from "node:fs";
|
|
1
|
+
import { closeSync, existsSync, fstatSync, mkdirSync, openSync, readSync, renameSync, statSync, writeSync, } from "node:fs";
|
|
2
2
|
import { dirname } from "node:path";
|
|
3
3
|
import { ErrorCode } from "./jsonrpc.js";
|
|
4
|
+
import { hasFields, parseState } from "../core/state-file.js";
|
|
4
5
|
/** Methods that get a record. */
|
|
5
6
|
const LOGGED = new Set(["tools/call"]);
|
|
6
7
|
const MAX_ERROR = 300;
|
|
@@ -39,7 +40,10 @@ function bytes(v) {
|
|
|
39
40
|
export function cleanAgentName(name) {
|
|
40
41
|
if (typeof name !== "string")
|
|
41
42
|
return undefined;
|
|
42
|
-
const clean = name
|
|
43
|
+
const clean = name
|
|
44
|
+
.replace(/[^\x20-\x7e]/g, "")
|
|
45
|
+
.trim()
|
|
46
|
+
.slice(0, 64);
|
|
43
47
|
return clean || undefined;
|
|
44
48
|
}
|
|
45
49
|
/** The record for one relayed request, or undefined when the method is not logged. */
|
|
@@ -128,16 +132,13 @@ export function createCallLog(file, opts = {}) {
|
|
|
128
132
|
close,
|
|
129
133
|
};
|
|
130
134
|
}
|
|
135
|
+
const isCallRecord = (v) => hasFields(v, { server: "string", time: "string" });
|
|
136
|
+
/** One line of the call log; a line cut short or of another shape is skipped. */
|
|
131
137
|
function parseLine(line) {
|
|
132
138
|
if (!line.trim())
|
|
133
139
|
return undefined;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return r && typeof r === "object" && typeof r.server === "string" && typeof r.time === "string" ? r : undefined;
|
|
137
|
-
}
|
|
138
|
-
catch {
|
|
139
|
-
return undefined;
|
|
140
|
-
}
|
|
140
|
+
const r = parseState(line, isCallRecord, 1, "call log line");
|
|
141
|
+
return r.ok ? r.value : undefined;
|
|
141
142
|
}
|
|
142
143
|
function matches(r, f) {
|
|
143
144
|
return (f.server === undefined || r.server === f.server) && (f.profile === undefined || r.profile === f.profile);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
1
|
import { atomicWrite } from "../core/journal.js";
|
|
3
2
|
import { PRIVATE_FILE_MODE } from "../core/paths.js";
|
|
3
|
+
import { isRecord, readState } from "../core/state-file.js";
|
|
4
4
|
/**
|
|
5
5
|
* The client name (`clientInfo.name` from initialize) of each agent process, by PID, kept in a
|
|
6
|
-
* small state file so a session mcpd rebuilds after a restart
|
|
7
|
-
* name for the call log and `/status
|
|
6
|
+
* small state file so a session mcpd rebuilds after a restart still has its agent's
|
|
7
|
+
* name for the call log and `/status`. Entries of processes that are gone are dropped
|
|
8
8
|
* on the next write. Never holds anything but PIDs and printable names.
|
|
9
9
|
*/
|
|
10
10
|
export class ClientNames {
|
|
@@ -16,16 +16,11 @@ export class ClientNames {
|
|
|
16
16
|
this.isAlive = isAlive;
|
|
17
17
|
if (!file)
|
|
18
18
|
return;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
this.names.set(n, name);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
catch {
|
|
28
|
-
/* none yet, or unreadable: start empty */
|
|
19
|
+
// None yet, or unreadable: start empty.
|
|
20
|
+
for (const [pid, name] of Object.entries(readState(file, isRecord) ?? {})) {
|
|
21
|
+
const n = Number(pid);
|
|
22
|
+
if (Number.isInteger(n) && n > 0 && typeof name === "string")
|
|
23
|
+
this.names.set(n, name);
|
|
29
24
|
}
|
|
30
25
|
}
|
|
31
26
|
get(pid) {
|
package/dist/mcpd/connectors.js
CHANGED
|
@@ -1,15 +1,51 @@
|
|
|
1
|
-
import { closeSync, fchmodSync, fstatSync, mkdirSync, openSync, readSync, renameSync, statSync, writeSync } from "node:fs";
|
|
1
|
+
import { closeSync, fchmodSync, fstatSync, mkdirSync, openSync, readSync, renameSync, statSync, writeSync, } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
4
4
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
5
5
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
6
6
|
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
7
|
-
import { effectiveLaunch } from "../core/manifest.js";
|
|
7
|
+
import { effectiveLaunch, effectiveStopGrace } from "../core/manifest.js";
|
|
8
8
|
import { PRIVATE_DIR_MODE, PRIVATE_FILE_MODE } from "../core/paths.js";
|
|
9
9
|
import { expandVars, resolveRecord, secretStore } from "../core/secrets.js";
|
|
10
10
|
import { isDirectory, resolveCommand } from "./instances.js";
|
|
11
11
|
import { OAuthSessions, oauthFetch } from "./oauth.js";
|
|
12
12
|
const MAX_LOG = 5 * 1024 * 1024;
|
|
13
|
+
/** What the MCP SDK waits after closing stdin, and again after SIGTERM, in milliseconds. */
|
|
14
|
+
const SDK_STEP_MS = 2000;
|
|
15
|
+
/**
|
|
16
|
+
* Replaces the SDK's close of a stdio server (stdin closed, 2 s, SIGTERM, 2 s, SIGKILL) with one
|
|
17
|
+
* that waits `graceMs` before SIGTERM (`stop_grace`). The rest is the SDK's: SIGTERM,
|
|
18
|
+
* 2 s, SIGKILL. It reads the transport's child process, a private field of the SDK; if that
|
|
19
|
+
* ever changes, the SDK's own close runs unchanged.
|
|
20
|
+
*/
|
|
21
|
+
function withStopGrace(transport, graceMs) {
|
|
22
|
+
const sdkClose = transport.close.bind(transport);
|
|
23
|
+
transport.close = async () => {
|
|
24
|
+
const proc = transport._process;
|
|
25
|
+
const running = () => proc !== undefined && proc.exitCode === null && proc.signalCode === null;
|
|
26
|
+
if (proc && running()) {
|
|
27
|
+
const closed = new Promise((resolve) => proc.once("close", () => resolve()));
|
|
28
|
+
const wait = (ms) => Promise.race([closed, new Promise((resolve) => setTimeout(resolve, ms).unref())]);
|
|
29
|
+
try {
|
|
30
|
+
proc.stdin?.end();
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
/* already closed */
|
|
34
|
+
}
|
|
35
|
+
await wait(graceMs);
|
|
36
|
+
if (running()) {
|
|
37
|
+
proc.kill("SIGTERM");
|
|
38
|
+
await wait(SDK_STEP_MS);
|
|
39
|
+
}
|
|
40
|
+
if (running()) {
|
|
41
|
+
proc.kill("SIGKILL");
|
|
42
|
+
await wait(1000);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// The process is gone (the SDK cleared it on "close"): this only resets the read buffer.
|
|
46
|
+
await sdkClose();
|
|
47
|
+
};
|
|
48
|
+
}
|
|
13
49
|
function logFileOf(logsDir, label) {
|
|
14
50
|
return join(logsDir, `${label.replace(/[^A-Za-z0-9_.-]/g, "_")}.log`);
|
|
15
51
|
}
|
|
@@ -136,7 +172,7 @@ export class DefaultConnector {
|
|
|
136
172
|
const headers = await resolveRecord(server.headers, this.opts.resolve);
|
|
137
173
|
let fetchOpt = {};
|
|
138
174
|
if (server.oauth) {
|
|
139
|
-
// mcpd is the OAuth client
|
|
175
|
+
// mcpd is the OAuth client. Asking for a token now fails fast with the login
|
|
140
176
|
// hint instead of a failed initialize; every request then gets a current token.
|
|
141
177
|
const session = this.sessions().get(server.name, variant);
|
|
142
178
|
await session.accessToken(server);
|
|
@@ -175,6 +211,9 @@ export class DefaultConnector {
|
|
|
175
211
|
cwd,
|
|
176
212
|
stderr: "pipe",
|
|
177
213
|
});
|
|
214
|
+
const graceMs = effectiveStopGrace(server, variant) * 1000;
|
|
215
|
+
if (graceMs !== SDK_STEP_MS)
|
|
216
|
+
withStopGrace(transport, graceMs);
|
|
178
217
|
const log = new RotatingLog(logFileOf(this.opts.logsDir, `${server.name}${variant === "default" ? "" : `@${variant}`}`), this.opts.maxLogBytes ?? MAX_LOG);
|
|
179
218
|
const warnings = resolved.warning ? [resolved.warning] : [];
|
|
180
219
|
for (const w of warnings)
|