turno-cli 0.1.1 → 0.3.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 +432 -0
- package/README.md +228 -73
- package/dist/accounts/audit.js +154 -12
- package/dist/accounts/builtin.js +100 -25
- package/dist/accounts/delegate.js +199 -35
- package/dist/accounts/policy.js +79 -0
- package/dist/accounts/registry.js +39 -21
- package/dist/agents/adapter.js +29 -0
- package/dist/agents/claude.js +246 -0
- package/dist/agents/codex.js +186 -0
- package/dist/agents/copilot.js +56 -0
- package/dist/agents/cursor.js +58 -0
- package/dist/agents/index.js +26 -86
- package/dist/agents/snippets.js +43 -13
- package/dist/cli/args.js +38 -6
- package/dist/cli/commands/accounts.js +119 -52
- package/dist/cli/commands/auto.js +121 -33
- package/dist/cli/commands/browser-identity.js +514 -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 +319 -138
- package/dist/cli/commands/complete.js +14 -3
- package/dist/cli/commands/completion.js +8 -2
- package/dist/cli/commands/config.js +186 -24
- package/dist/cli/commands/demo.js +321 -0
- package/dist/cli/commands/doctor.js +525 -123
- package/dist/cli/commands/handoff.js +33 -22
- package/dist/cli/commands/identity.js +192 -54
- package/dist/cli/commands/init.js +648 -0
- package/dist/cli/commands/limit.js +84 -16
- package/dist/cli/commands/mcp-call.js +240 -0
- package/dist/cli/commands/mcp-oauth.js +221 -0
- package/dist/cli/commands/mcp-observe.js +539 -0
- package/dist/cli/commands/mcp-share.js +201 -0
- package/dist/cli/commands/mcp.js +816 -187
- package/dist/cli/commands/mcpd.js +388 -76
- package/dist/cli/commands/migrate.js +6 -4
- package/dist/cli/commands/open.js +115 -27
- package/dist/cli/commands/profile.js +396 -78
- package/dist/cli/commands/resolve.js +14 -3
- package/dist/cli/commands/run.js +36 -6
- package/dist/cli/commands/secret.js +80 -76
- package/dist/cli/commands/session.js +17 -6
- package/dist/cli/commands/sessions.js +48 -12
- package/dist/cli/commands/setup.js +214 -164
- package/dist/cli/commands/status.js +227 -44
- package/dist/cli/commands/statusline.js +311 -114
- package/dist/cli/commands/ui.js +78 -68
- package/dist/cli/commands/undo.js +193 -18
- package/dist/cli/commands/uninstall.js +481 -0
- package/dist/cli/commands/upgrade.js +172 -79
- package/dist/cli/commands/usage.js +125 -51
- package/dist/cli/commands/version.js +21 -7
- package/dist/cli/commands/which.js +9 -24
- package/dist/cli/compile-cache.js +32 -0
- package/dist/cli/diagnostics.js +184 -0
- package/dist/cli/exit.js +65 -0
- package/dist/cli/group.js +24 -12
- package/dist/cli/help.js +62 -0
- package/dist/cli/highlight.js +236 -0
- package/dist/cli/hints.js +30 -0
- package/dist/cli/io.js +186 -1
- package/dist/cli/launch.js +52 -39
- package/dist/cli/launchers.js +161 -95
- package/dist/cli/main.js +201 -31
- package/dist/cli/optional.js +144 -0
- package/dist/cli/output.js +123 -0
- package/dist/cli/picker.js +459 -0
- package/dist/cli/registry.js +61 -6
- package/dist/cli/safe.js +87 -0
- package/dist/cli/secret-input.js +63 -0
- package/dist/cli/security.js +561 -0
- package/dist/cli/style.js +311 -0
- package/dist/cli/topics.js +142 -0
- package/dist/core/config.js +181 -23
- package/dist/core/exhausted.js +6 -8
- package/dist/core/fsutil.js +39 -0
- package/dist/core/journal.js +294 -16
- package/dist/core/manifest.js +153 -6
- package/dist/core/paths.js +14 -13
- package/dist/core/profiles.js +160 -10
- package/dist/core/resolve.js +139 -23
- package/dist/core/secrets.js +68 -23
- package/dist/core/sessions.js +13 -20
- package/dist/core/state-file.js +107 -0
- package/dist/core/update.js +31 -18
- package/dist/core/version.js +57 -10
- package/dist/demo/server.js +42 -0
- package/dist/mcpd/cache.js +23 -10
- package/dist/mcpd/calllog.js +254 -0
- package/dist/mcpd/client-names.js +46 -0
- package/dist/mcpd/connectors.js +188 -18
- package/dist/mcpd/daemon.js +472 -115
- package/dist/mcpd/folder-sets.js +42 -0
- package/dist/mcpd/gateway.js +473 -51
- package/dist/mcpd/health.js +313 -0
- package/dist/mcpd/importer.js +453 -36
- package/dist/mcpd/instances.js +453 -65
- package/dist/mcpd/jsonrpc.js +11 -1
- package/dist/mcpd/launchd.js +65 -48
- package/dist/mcpd/middleware.js +144 -0
- package/dist/mcpd/modern.js +686 -0
- package/dist/mcpd/oauth.js +845 -0
- package/dist/mcpd/peerpid.js +134 -6
- package/dist/mcpd/pressure.js +88 -0
- package/dist/mcpd/rss.js +166 -0
- package/dist/mcpd/service.js +191 -0
- package/dist/mcpd/share.js +217 -0
- package/dist/mcpd/state.js +183 -0
- package/dist/mcpd/sync.js +72 -87
- package/dist/mcpd/systemd.js +83 -0
- package/dist/mcpd/tokens.js +85 -0
- package/dist/mcpd/upstream.js +163 -46
- package/dist/statusline/cost.js +167 -44
- package/dist/statusline/input.js +78 -0
- package/dist/statusline/render.js +9 -3
- package/dist/ui/fonts.js +8 -3
- package/dist/ui/settings.js +33 -16
- package/npm-shrinkwrap.json +1102 -0
- package/package.json +10 -9
- package/dist/cli/commands/available.js +0 -18
package/dist/core/journal.js
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
|
-
import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, readlinkSync, realpathSync, renameSync, rmSync, rmdirSync, statSync, symlinkSync, writeFileSync, } from "node:fs";
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import { basename, dirname, join } from "node:path";
|
|
4
|
-
import { paths } from "./paths.js";
|
|
4
|
+
import { PRIVATE_FILE_MODE, ensurePrivateDir, isPrivatePath, paths } from "./paths.js";
|
|
5
|
+
import { hasFields, readState } from "./state-file.js";
|
|
5
6
|
/** Local "YYYYMMDD-HHMMSS". */
|
|
6
7
|
export function stamp(d = new Date()) {
|
|
7
8
|
const p = (n) => String(n).padStart(2, "0");
|
|
8
9
|
return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`;
|
|
9
10
|
}
|
|
10
11
|
const MANIFEST = "journal.json";
|
|
12
|
+
/** Automatic pruning when a new backup is written: older than 30 days, newest 10 always kept. */
|
|
13
|
+
export const PRUNE_DEFAULT_AGE_MS = 30 * 24 * 60 * 60 * 1000;
|
|
14
|
+
export const PRUNE_DEFAULT_KEEP = 10;
|
|
15
|
+
function writeManifest(dir, m) {
|
|
16
|
+
const file = join(dir, MANIFEST);
|
|
17
|
+
writeFileSync(file, `${JSON.stringify(m, null, 2)}\n`, { mode: PRIVATE_FILE_MODE });
|
|
18
|
+
chmodSync(file, PRIVATE_FILE_MODE);
|
|
19
|
+
}
|
|
11
20
|
function digest(file) {
|
|
12
21
|
try {
|
|
13
22
|
return createHash("sha256").update(readFileSync(file)).digest("hex");
|
|
@@ -30,6 +39,23 @@ function real(p) {
|
|
|
30
39
|
}
|
|
31
40
|
}
|
|
32
41
|
}
|
|
42
|
+
/** `p` with its folder resolved but not `p` itself: the path of a link, not of what it points to. */
|
|
43
|
+
function realParent(p) {
|
|
44
|
+
try {
|
|
45
|
+
return join(realpathSync(dirname(p)), basename(p));
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return p;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function lstatOf(p) {
|
|
52
|
+
try {
|
|
53
|
+
return lstatSync(p);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
33
59
|
/** Write text atomically, keeping the mode of the file it replaces. */
|
|
34
60
|
export function atomicWrite(file, data, mode) {
|
|
35
61
|
mkdirSync(dirname(file), { recursive: true });
|
|
@@ -42,15 +68,19 @@ export function atomicWrite(file, data, mode) {
|
|
|
42
68
|
export class Journal {
|
|
43
69
|
env;
|
|
44
70
|
dryRun;
|
|
71
|
+
protect;
|
|
45
72
|
stamp;
|
|
46
73
|
dir;
|
|
47
74
|
manifest;
|
|
48
75
|
seen = new Set();
|
|
49
76
|
constructor(env, command,
|
|
50
77
|
/** Dry runs record nothing and write nothing. */
|
|
51
|
-
dryRun = false
|
|
78
|
+
dryRun = false,
|
|
79
|
+
/** Stamps the automatic pruning must not touch (the journal an undo reads from). */
|
|
80
|
+
protect = []) {
|
|
52
81
|
this.env = env;
|
|
53
82
|
this.dryRun = dryRun;
|
|
83
|
+
this.protect = protect;
|
|
54
84
|
const root = paths(env).backupsDir;
|
|
55
85
|
let s = stamp();
|
|
56
86
|
for (let n = 2; existsSync(join(root, s)); n++)
|
|
@@ -75,8 +105,11 @@ export class Journal {
|
|
|
75
105
|
throw new Error(`${target} is not a regular file; turno only journals files`);
|
|
76
106
|
entry.mode = st.mode & 0o777;
|
|
77
107
|
entry.backup = join("files", `${this.manifest.entries.length}-${basename(target)}`);
|
|
78
|
-
|
|
79
|
-
|
|
108
|
+
ensurePrivateDir(join(this.dir, "files"), this.env);
|
|
109
|
+
// The copy may hold secrets (a config with a literal token): 0600 inside 0700 folders.
|
|
110
|
+
const copy = join(this.dir, entry.backup);
|
|
111
|
+
copyFileSync(target, copy);
|
|
112
|
+
chmodSync(copy, PRIVATE_FILE_MODE);
|
|
80
113
|
}
|
|
81
114
|
this.manifest.entries.push(entry);
|
|
82
115
|
this.flush();
|
|
@@ -93,16 +126,27 @@ export class Journal {
|
|
|
93
126
|
entry.wrote = digest(target);
|
|
94
127
|
this.flush();
|
|
95
128
|
}
|
|
129
|
+
flushed = false;
|
|
96
130
|
flush() {
|
|
97
|
-
|
|
98
|
-
|
|
131
|
+
ensurePrivateDir(this.dir, this.env);
|
|
132
|
+
writeManifest(this.dir, this.manifest);
|
|
133
|
+
if (!this.flushed) {
|
|
134
|
+
this.flushed = true;
|
|
135
|
+
try {
|
|
136
|
+
pruneJournals(this.env, { exclude: [this.stamp, ...this.protect] });
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
/* pruning is housekeeping: never let it break the command that writes */
|
|
140
|
+
}
|
|
141
|
+
}
|
|
99
142
|
}
|
|
100
143
|
/** Save, then write atomically through symlinks so linked profiles keep linking. */
|
|
101
144
|
write(file, data, mode) {
|
|
102
145
|
if (this.dryRun)
|
|
103
146
|
return;
|
|
104
147
|
const target = this.save(file);
|
|
105
|
-
|
|
148
|
+
// turno's own files (config.toml, mcp.toml, accounts.toml) are private whatever mode they had.
|
|
149
|
+
let keep = mode ?? (isPrivatePath(target, this.env) ? PRIVATE_FILE_MODE : undefined);
|
|
106
150
|
if (keep === undefined) {
|
|
107
151
|
try {
|
|
108
152
|
keep = statSync(target).mode & 0o777;
|
|
@@ -111,6 +155,8 @@ export class Journal {
|
|
|
111
155
|
keep = undefined;
|
|
112
156
|
}
|
|
113
157
|
}
|
|
158
|
+
if (isPrivatePath(target, this.env))
|
|
159
|
+
ensurePrivateDir(dirname(target), this.env);
|
|
114
160
|
atomicWrite(target, data, keep);
|
|
115
161
|
this.settle(target);
|
|
116
162
|
}
|
|
@@ -121,13 +167,80 @@ export class Journal {
|
|
|
121
167
|
rmSync(target, { force: true });
|
|
122
168
|
this.settle(target);
|
|
123
169
|
}
|
|
170
|
+
record(entry) {
|
|
171
|
+
this.manifest.entries.push(entry);
|
|
172
|
+
this.flush();
|
|
173
|
+
}
|
|
174
|
+
/** Create a link at `path` to `target`. */
|
|
175
|
+
symlink(target, path) {
|
|
176
|
+
if (this.dryRun)
|
|
177
|
+
return;
|
|
178
|
+
const at = realParent(path);
|
|
179
|
+
symlinkSync(target, at);
|
|
180
|
+
this.record({ kind: "symlink", path: at, target, existed: false });
|
|
181
|
+
}
|
|
182
|
+
/** Remove the link at `path` (never what it points to). */
|
|
183
|
+
unlink(path) {
|
|
184
|
+
if (this.dryRun)
|
|
185
|
+
return;
|
|
186
|
+
const at = realParent(path);
|
|
187
|
+
const target = readlinkSync(at);
|
|
188
|
+
rmSync(at);
|
|
189
|
+
this.record({ kind: "unlink", path: at, target, existed: true });
|
|
190
|
+
}
|
|
191
|
+
/** Rename `from` to `to` (a file, a folder or a link). */
|
|
192
|
+
move(from, to) {
|
|
193
|
+
if (this.dryRun)
|
|
194
|
+
return;
|
|
195
|
+
const src = realParent(from);
|
|
196
|
+
const dst = realParent(to);
|
|
197
|
+
renameSync(src, dst);
|
|
198
|
+
this.record({ kind: "move", path: src, to: dst, existed: true });
|
|
199
|
+
}
|
|
200
|
+
/** Create the folder `path` and any missing parents, recording each one it creates. */
|
|
201
|
+
mkdir(path, mode) {
|
|
202
|
+
if (this.dryRun)
|
|
203
|
+
return;
|
|
204
|
+
const missing = [];
|
|
205
|
+
for (let p = path; !lstatOf(p); p = dirname(p)) {
|
|
206
|
+
missing.unshift(p);
|
|
207
|
+
if (dirname(p) === p)
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
for (const p of missing) {
|
|
211
|
+
mkdirSync(p, mode !== undefined ? { mode } : undefined);
|
|
212
|
+
this.record({ kind: "mkdir", path: realParent(p), existed: false, ...(mode !== undefined ? { mode } : {}) });
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** Remove the empty folder `path`. */
|
|
216
|
+
rmdir(path) {
|
|
217
|
+
if (this.dryRun)
|
|
218
|
+
return;
|
|
219
|
+
const at = realParent(path);
|
|
220
|
+
const mode = statSync(at).mode & 0o777;
|
|
221
|
+
rmdirSync(at);
|
|
222
|
+
this.record({ kind: "rmdir", path: at, existed: true, mode });
|
|
223
|
+
}
|
|
224
|
+
/** Set the permission bits of `path`, recording the old ones when they change. */
|
|
225
|
+
chmod(path, mode) {
|
|
226
|
+
if (this.dryRun)
|
|
227
|
+
return;
|
|
228
|
+
const at = realParent(path);
|
|
229
|
+
const old = statSync(at).mode & 0o777;
|
|
230
|
+
if (old === mode)
|
|
231
|
+
return;
|
|
232
|
+
chmodSync(at, mode);
|
|
233
|
+
this.record({ kind: "chmod", path: at, existed: true, mode: old, newMode: mode });
|
|
234
|
+
}
|
|
124
235
|
/** The `turno undo` hint to print after a command that changed something. */
|
|
125
236
|
hint() {
|
|
126
237
|
return this.used ? `undo with: turno undo ${this.stamp}` : undefined;
|
|
127
238
|
}
|
|
128
239
|
}
|
|
240
|
+
const isManifest = (v) => hasFields(v, { stamp: "string", command: "string", entries: "array" }) &&
|
|
241
|
+
v.entries.every((e) => hasFields(e, { path: "string" }));
|
|
129
242
|
/** Backup folders, newest first. */
|
|
130
|
-
export function listJournals(env
|
|
243
|
+
export function listJournals(env) {
|
|
131
244
|
const root = paths(env).backupsDir;
|
|
132
245
|
let names = [];
|
|
133
246
|
try {
|
|
@@ -142,13 +255,114 @@ export function listJournals(env = process.env) {
|
|
|
142
255
|
.reverse()
|
|
143
256
|
.map((n) => {
|
|
144
257
|
const dir = join(root, n);
|
|
145
|
-
|
|
146
|
-
|
|
258
|
+
// No manifest, or one that cannot be used: the folder is shown as restore-by-hand.
|
|
259
|
+
const manifest = readState(join(dir, MANIFEST), isManifest, 1);
|
|
260
|
+
return manifest ? { stamp: n, dir, manifest } : { stamp: n, dir };
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
function isEmptyDir(p) {
|
|
264
|
+
try {
|
|
265
|
+
return readdirSync(p).length === 0;
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/** Undo one link, move or folder entry through `journal` (which records the inverse). */
|
|
272
|
+
function undoTreeEntry(e, journal) {
|
|
273
|
+
const st = lstatOf(e.path);
|
|
274
|
+
switch (e.kind) {
|
|
275
|
+
case "symlink": {
|
|
276
|
+
if (!st)
|
|
277
|
+
return undefined;
|
|
278
|
+
if (!st.isSymbolicLink() || readlinkSync(e.path) !== e.target)
|
|
279
|
+
return { path: e.path, action: "skip", reason: "no longer the link turno made; left as it is" };
|
|
280
|
+
journal.unlink(e.path);
|
|
281
|
+
return { path: e.path, action: "unlink" };
|
|
147
282
|
}
|
|
148
|
-
|
|
149
|
-
|
|
283
|
+
case "unlink": {
|
|
284
|
+
if (st)
|
|
285
|
+
return { path: e.path, action: "skip", reason: `something else is there now; it linked to ${e.target}` };
|
|
286
|
+
journal.symlink(e.target, e.path);
|
|
287
|
+
return { path: e.path, action: "link" };
|
|
150
288
|
}
|
|
151
|
-
|
|
289
|
+
case "move": {
|
|
290
|
+
if (st)
|
|
291
|
+
return { path: e.path, action: "skip", reason: `something is there now; the moved copy stays at ${e.to}` };
|
|
292
|
+
if (!lstatOf(e.to))
|
|
293
|
+
return { path: e.path, action: "skip", reason: `${e.to} is gone` };
|
|
294
|
+
journal.move(e.to, e.path);
|
|
295
|
+
return { path: e.path, action: "move", from: e.to };
|
|
296
|
+
}
|
|
297
|
+
case "mkdir": {
|
|
298
|
+
if (!st)
|
|
299
|
+
return undefined;
|
|
300
|
+
if (!st.isDirectory() || !isEmptyDir(e.path))
|
|
301
|
+
return { path: e.path, action: "keep", reason: "not empty (it holds files made since)" };
|
|
302
|
+
journal.rmdir(e.path);
|
|
303
|
+
return { path: e.path, action: "rmdir" };
|
|
304
|
+
}
|
|
305
|
+
case "rmdir": {
|
|
306
|
+
if (st)
|
|
307
|
+
return undefined;
|
|
308
|
+
journal.mkdir(e.path, e.mode);
|
|
309
|
+
return { path: e.path, action: "mkdir" };
|
|
310
|
+
}
|
|
311
|
+
case "chmod": {
|
|
312
|
+
const step = chmodStep(e, st);
|
|
313
|
+
if (step?.action === "chmod")
|
|
314
|
+
journal.chmod(e.path, e.mode);
|
|
315
|
+
return step;
|
|
316
|
+
}
|
|
317
|
+
default:
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/** Undoing a mode change: set the old mode again, unless the file is gone or its mode changed since. */
|
|
322
|
+
function chmodStep(e, st) {
|
|
323
|
+
if (!st || e.mode === undefined)
|
|
324
|
+
return undefined;
|
|
325
|
+
const now = st.mode & 0o777;
|
|
326
|
+
if (now === e.mode)
|
|
327
|
+
return undefined;
|
|
328
|
+
if (now !== e.newMode)
|
|
329
|
+
return { path: e.path, action: "skip", reason: `its mode changed since (now ${now.toString(8)}); left as it is` };
|
|
330
|
+
return { path: e.path, action: "chmod", mode: e.mode };
|
|
331
|
+
}
|
|
332
|
+
/** What undoing a tree entry would do, without doing it (for --dry-run). */
|
|
333
|
+
function planTreeEntry(e) {
|
|
334
|
+
const st = lstatOf(e.path);
|
|
335
|
+
switch (e.kind) {
|
|
336
|
+
case "symlink":
|
|
337
|
+
if (!st)
|
|
338
|
+
return undefined;
|
|
339
|
+
return st.isSymbolicLink() && readlinkSync(e.path) === e.target
|
|
340
|
+
? { path: e.path, action: "unlink" }
|
|
341
|
+
: { path: e.path, action: "skip", reason: "no longer the link turno made; left as it is" };
|
|
342
|
+
case "unlink":
|
|
343
|
+
return st
|
|
344
|
+
? { path: e.path, action: "skip", reason: `something else is there now; it linked to ${e.target}` }
|
|
345
|
+
: { path: e.path, action: "link" };
|
|
346
|
+
case "move":
|
|
347
|
+
if (st)
|
|
348
|
+
return { path: e.path, action: "skip", reason: `something is there now; the moved copy stays at ${e.to}` };
|
|
349
|
+
return lstatOf(e.to)
|
|
350
|
+
? { path: e.path, action: "move", from: e.to }
|
|
351
|
+
: { path: e.path, action: "skip", reason: `${e.to} is gone` };
|
|
352
|
+
case "mkdir":
|
|
353
|
+
if (!st)
|
|
354
|
+
return undefined;
|
|
355
|
+
// In a dry run nothing inside was removed yet: report the folder as removable.
|
|
356
|
+
return st.isDirectory()
|
|
357
|
+
? { path: e.path, action: "rmdir" }
|
|
358
|
+
: { path: e.path, action: "keep", reason: "not a folder any more" };
|
|
359
|
+
case "rmdir":
|
|
360
|
+
return st ? undefined : { path: e.path, action: "mkdir" };
|
|
361
|
+
case "chmod":
|
|
362
|
+
return chmodStep(e, st);
|
|
363
|
+
default:
|
|
364
|
+
return undefined;
|
|
365
|
+
}
|
|
152
366
|
}
|
|
153
367
|
/**
|
|
154
368
|
* Put back every file of a journal. The current contents are journaled first, so an undo can
|
|
@@ -159,9 +373,15 @@ export function undoJournal(info, env, opts = {}) {
|
|
|
159
373
|
const m = info.manifest;
|
|
160
374
|
if (!m)
|
|
161
375
|
throw new Error(`${info.stamp} was written before journals existed; restore it by hand from ${info.dir}`);
|
|
162
|
-
const journal = new Journal(env, `undo ${info.stamp}`, dryRun);
|
|
376
|
+
const journal = new Journal(env, `undo ${info.stamp}`, dryRun, [info.stamp]);
|
|
163
377
|
const steps = [];
|
|
164
378
|
for (const e of [...m.entries].reverse()) {
|
|
379
|
+
if (e.kind && e.kind !== "file") {
|
|
380
|
+
const step = dryRun ? planTreeEntry(e) : undoTreeEntry(e, journal);
|
|
381
|
+
if (step)
|
|
382
|
+
steps.push(step);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
165
385
|
if (!opts.force && e.wrote && digest(e.path) !== e.wrote) {
|
|
166
386
|
steps.push({ path: e.path, action: "skip", reason: "changed since; --force puts the old copy back anyway" });
|
|
167
387
|
continue;
|
|
@@ -178,8 +398,66 @@ export function undoJournal(info, env, opts = {}) {
|
|
|
178
398
|
}
|
|
179
399
|
if (!dryRun) {
|
|
180
400
|
m.undoneBy = journal.stamp;
|
|
181
|
-
|
|
401
|
+
writeManifest(info.dir, m);
|
|
182
402
|
}
|
|
183
403
|
return { steps, journal };
|
|
184
404
|
}
|
|
405
|
+
/** When a journal was written: its manifest, else its stamp (local time), else the folder mtime. */
|
|
406
|
+
function journalTime(info) {
|
|
407
|
+
const created = info.manifest ? Date.parse(info.manifest.created) : Number.NaN;
|
|
408
|
+
if (!Number.isNaN(created))
|
|
409
|
+
return created;
|
|
410
|
+
const m = /^(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})/.exec(info.stamp);
|
|
411
|
+
if (m)
|
|
412
|
+
return new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]).getTime();
|
|
413
|
+
try {
|
|
414
|
+
return statSync(info.dir).mtimeMs;
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
return Date.now();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* The first file a journal can still put back cleanly: not undone, and the file holds exactly
|
|
422
|
+
* what the command left there. Its backup is then the only copy of what was there before.
|
|
423
|
+
*/
|
|
424
|
+
function liveEntry(info) {
|
|
425
|
+
const m = info.manifest;
|
|
426
|
+
if (!m || m.undoneBy)
|
|
427
|
+
return undefined;
|
|
428
|
+
return m.entries.find((e) => (e.kind ?? "file") === "file" && e.wrote !== undefined && digest(e.path) === e.wrote);
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Delete old backup folders. The newest `keep` stay, and so does any journal that is still the
|
|
432
|
+
* live undo point of a file (see liveEntry): pruning it would lose the only copy of what that
|
|
433
|
+
* file held before a change the user has not undone.
|
|
434
|
+
*/
|
|
435
|
+
export function pruneJournals(env, opts = {}) {
|
|
436
|
+
const olderThan = opts.olderThanMs ?? PRUNE_DEFAULT_AGE_MS;
|
|
437
|
+
const keep = opts.keep ?? PRUNE_DEFAULT_KEEP;
|
|
438
|
+
const now = opts.now ?? Date.now();
|
|
439
|
+
const exclude = new Set(opts.exclude ?? []);
|
|
440
|
+
const result = { pruned: [], kept: [] };
|
|
441
|
+
for (const info of listJournals(env).slice(keep)) {
|
|
442
|
+
if (exclude.has(info.stamp) || now - journalTime(info) < olderThan)
|
|
443
|
+
continue;
|
|
444
|
+
const live = liveEntry(info);
|
|
445
|
+
if (live) {
|
|
446
|
+
result.kept.push({ stamp: info.stamp, path: live.path });
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (!opts.dryRun)
|
|
450
|
+
rmSync(info.dir, { recursive: true, force: true });
|
|
451
|
+
result.pruned.push(info.stamp);
|
|
452
|
+
}
|
|
453
|
+
return result;
|
|
454
|
+
}
|
|
455
|
+
/** Parse an age such as 30d, 12h, 2w, 90m or a bare number of days. */
|
|
456
|
+
export function parseAge(text) {
|
|
457
|
+
const m = /^(\d+)\s*([smhdw]?)$/.exec(text.trim());
|
|
458
|
+
if (!m)
|
|
459
|
+
throw new Error(`invalid age ${JSON.stringify(text)}: use a number with s, m, h, d or w (30d)`);
|
|
460
|
+
const unit = { s: 1, m: 60, h: 3600, d: 86400, w: 604800, "": 86400 }[m[2]];
|
|
461
|
+
return Number(m[1]) * unit * 1000;
|
|
462
|
+
}
|
|
185
463
|
//# sourceMappingURL=journal.js.map
|
package/dist/core/manifest.js
CHANGED
|
@@ -3,12 +3,12 @@ import { parse } from "smol-toml";
|
|
|
3
3
|
import { ConfigError, isValidProfileName } from "./config.js";
|
|
4
4
|
import { paths } from "./paths.js";
|
|
5
5
|
/** A value resolved at spawn time: "keychain:<service>" or "env:<NAME>". */
|
|
6
|
-
|
|
6
|
+
function isSecretRef(v) {
|
|
7
7
|
return v.startsWith("keychain:") || v.startsWith("env:");
|
|
8
8
|
}
|
|
9
9
|
const SERVER_NAME = /^[A-Za-z0-9][A-Za-z0-9_.-]*$/;
|
|
10
10
|
/** "90s", "15m", "2h" or a number of seconds. */
|
|
11
|
-
|
|
11
|
+
function parseDuration(v, file, key) {
|
|
12
12
|
if (typeof v === "number" && v >= 0)
|
|
13
13
|
return v;
|
|
14
14
|
if (typeof v === "string") {
|
|
@@ -45,6 +45,71 @@ function optStr(v, file, key) {
|
|
|
45
45
|
throw new ConfigError(file, `${key} must be a non-empty string`);
|
|
46
46
|
return v;
|
|
47
47
|
}
|
|
48
|
+
const TOOL_PATTERN = /^[^\s]{1,128}$/;
|
|
49
|
+
/** `tools = { allow = [...], deny = [...] }`: a table with only those keys, each a list of patterns. */
|
|
50
|
+
function toolFilterOf(v, file, key) {
|
|
51
|
+
if (v === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
if (typeof v !== "object" || v === null || Array.isArray(v))
|
|
54
|
+
throw new ConfigError(file, `${key} must be a table with allow and/or deny`);
|
|
55
|
+
const out = {};
|
|
56
|
+
for (const [k, val] of Object.entries(v)) {
|
|
57
|
+
if (k !== "allow" && k !== "deny")
|
|
58
|
+
throw new ConfigError(file, `${key}.${k} is not a tool filter key (allow, deny)`);
|
|
59
|
+
const list = strArray(val, file, `${key}.${k}`);
|
|
60
|
+
for (const p of list)
|
|
61
|
+
if (!TOOL_PATTERN.test(p))
|
|
62
|
+
throw new ConfigError(file, `${key}.${k}: invalid tool pattern ${JSON.stringify(p)}`);
|
|
63
|
+
out[k] = list;
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
const OAUTH_KEYS = {
|
|
68
|
+
client_id: "clientId",
|
|
69
|
+
client_secret: "clientSecret",
|
|
70
|
+
client_metadata_url: "clientMetadataUrl",
|
|
71
|
+
scope: "scope",
|
|
72
|
+
callback_port: "callbackPort",
|
|
73
|
+
authorization_server: "authorizationServer",
|
|
74
|
+
};
|
|
75
|
+
/** `auth = "oauth"` and the optional `[server.<name>.oauth]` table; undefined without OAuth. */
|
|
76
|
+
function oauthOf(v, file, key, headers) {
|
|
77
|
+
if (v.auth === undefined) {
|
|
78
|
+
if (v.oauth !== undefined)
|
|
79
|
+
throw new ConfigError(file, `${key}.oauth needs auth = "oauth"`);
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
if (v.auth !== "oauth")
|
|
83
|
+
throw new ConfigError(file, `${key}.auth must be "oauth"`);
|
|
84
|
+
if (Object.keys(headers).some((h) => h.toLowerCase() === "authorization")) {
|
|
85
|
+
throw new ConfigError(file, `${key}: auth = "oauth" sets the Authorization header itself; remove headers.Authorization`);
|
|
86
|
+
}
|
|
87
|
+
const raw = v.oauth ?? {};
|
|
88
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw))
|
|
89
|
+
throw new ConfigError(file, `${key}.oauth must be a table`);
|
|
90
|
+
const out = {};
|
|
91
|
+
for (const [k, val] of Object.entries(raw)) {
|
|
92
|
+
const field = OAUTH_KEYS[k];
|
|
93
|
+
if (!field)
|
|
94
|
+
throw new ConfigError(file, `${key}.oauth.${k} is not an OAuth setting (${Object.keys(OAUTH_KEYS).join(", ")})`);
|
|
95
|
+
if (field === "callbackPort") {
|
|
96
|
+
if (typeof val !== "number" || !Number.isInteger(val) || val < 1024 || val > 65535) {
|
|
97
|
+
throw new ConfigError(file, `${key}.oauth.callback_port must be a port number from 1024 to 65535`);
|
|
98
|
+
}
|
|
99
|
+
out.callbackPort = val;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const s = optStr(val, file, `${key}.oauth.${k}`);
|
|
103
|
+
if (field === "clientSecret" && !isSecretRef(s)) {
|
|
104
|
+
throw new ConfigError(file, `${key}.oauth.client_secret must be a keychain: or env: reference (turno secret set <name>), not the secret itself`);
|
|
105
|
+
}
|
|
106
|
+
if ((field === "clientMetadataUrl" || field === "authorizationServer") && !/^https?:\/\/\S+$/.test(s)) {
|
|
107
|
+
throw new ConfigError(file, `${key}.oauth.${k} must be a URL`);
|
|
108
|
+
}
|
|
109
|
+
out[field] = s;
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
48
113
|
function profilesOf(v, file, key) {
|
|
49
114
|
const list = v === undefined ? ["*"] : strArray(v, file, key);
|
|
50
115
|
for (const p of list)
|
|
@@ -71,11 +136,29 @@ export function parseManifest(text, file = "mcp.toml") {
|
|
|
71
136
|
if (v.url !== undefined) {
|
|
72
137
|
if (typeof v.url !== "string")
|
|
73
138
|
throw new ConfigError(file, `${key}.url must be a string`);
|
|
74
|
-
const { profiles: _p, idle: _i, ...rest } = v;
|
|
139
|
+
const { profiles: _p, idle: _i, call_timeout: _t, tools: _f, auth: _a, oauth: _o, variant: _v, ...rest } = v;
|
|
75
140
|
const entry = { type: "http", ...rest };
|
|
76
141
|
const headers = strRecord(v.headers, file, `${key}.headers`);
|
|
77
|
-
const
|
|
78
|
-
|
|
142
|
+
const oauth = oauthOf(v, file, key, headers);
|
|
143
|
+
const proxy = oauth !== undefined || Object.values(headers).some(isSecretRef);
|
|
144
|
+
const tools = toolFilterOf(v.tools, file, `${key}.tools`);
|
|
145
|
+
if (tools && !proxy) {
|
|
146
|
+
throw new ConfigError(file, `${key}.tools only works for servers that go through mcpd; a remote server does when a header uses keychain: or env:, or with auth = "oauth"`);
|
|
147
|
+
}
|
|
148
|
+
const remoteVariants = {};
|
|
149
|
+
for (const [prof, vv] of Object.entries((v.variant ?? {}))) {
|
|
150
|
+
if (!oauth)
|
|
151
|
+
throw new ConfigError(file, `${key}.variant: a remote server has variants only with auth = "oauth" (one login per variant)`);
|
|
152
|
+
if (!isValidProfileName(prof))
|
|
153
|
+
throw new ConfigError(file, `${key}.variant: invalid profile "${prof}"`);
|
|
154
|
+
const o = (vv ?? {});
|
|
155
|
+
const variant = {};
|
|
156
|
+
const vtools = toolFilterOf(o.tools, file, `${key}.variant.${prof}.tools`);
|
|
157
|
+
if (vtools)
|
|
158
|
+
variant.tools = vtools;
|
|
159
|
+
remoteVariants[prof] = variant;
|
|
160
|
+
}
|
|
161
|
+
const remote = {
|
|
79
162
|
kind: "remote",
|
|
80
163
|
name,
|
|
81
164
|
entry,
|
|
@@ -84,7 +167,15 @@ export function parseManifest(text, file = "mcp.toml") {
|
|
|
84
167
|
url: v.url,
|
|
85
168
|
headers,
|
|
86
169
|
idle: v.idle === undefined ? 30 * 60 : parseDuration(v.idle, file, `${key}.idle`),
|
|
170
|
+
variants: remoteVariants,
|
|
87
171
|
};
|
|
172
|
+
if (oauth)
|
|
173
|
+
remote.oauth = oauth;
|
|
174
|
+
if (v.call_timeout !== undefined)
|
|
175
|
+
remote.callTimeout = parseDuration(v.call_timeout, file, `${key}.call_timeout`);
|
|
176
|
+
if (tools)
|
|
177
|
+
remote.tools = tools;
|
|
178
|
+
servers[name] = remote;
|
|
88
179
|
continue;
|
|
89
180
|
}
|
|
90
181
|
const builtin = optStr(v.builtin, file, `${key}.builtin`);
|
|
@@ -114,6 +205,9 @@ export function parseManifest(text, file = "mcp.toml") {
|
|
|
114
205
|
const vcwd = optStr(o.cwd, file, `${key}.variant.${prof}.cwd`);
|
|
115
206
|
if (vcwd)
|
|
116
207
|
variant.cwd = vcwd;
|
|
208
|
+
const vtools = toolFilterOf(o.tools, file, `${key}.variant.${prof}.tools`);
|
|
209
|
+
if (vtools)
|
|
210
|
+
variant.tools = vtools;
|
|
117
211
|
variants[prof] = variant;
|
|
118
212
|
}
|
|
119
213
|
const server = {
|
|
@@ -137,11 +231,22 @@ export function parseManifest(text, file = "mcp.toml") {
|
|
|
137
231
|
server.package = pkg;
|
|
138
232
|
if (builtin)
|
|
139
233
|
server.builtin = builtin;
|
|
234
|
+
if (v.call_timeout !== undefined)
|
|
235
|
+
server.callTimeout = parseDuration(v.call_timeout, file, `${key}.call_timeout`);
|
|
236
|
+
if (v.stop_grace !== undefined) {
|
|
237
|
+
const grace = parseDuration(v.stop_grace, file, `${key}.stop_grace`);
|
|
238
|
+
if (grace > MAX_STOP_GRACE)
|
|
239
|
+
throw new ConfigError(file, `${key}.stop_grace must be at most ${MAX_STOP_GRACE}s`);
|
|
240
|
+
server.stopGrace = grace;
|
|
241
|
+
}
|
|
242
|
+
const tools = toolFilterOf(v.tools, file, `${key}.tools`);
|
|
243
|
+
if (tools)
|
|
244
|
+
server.tools = tools;
|
|
140
245
|
servers[name] = server;
|
|
141
246
|
}
|
|
142
247
|
return { servers };
|
|
143
248
|
}
|
|
144
|
-
export function loadManifest(env
|
|
249
|
+
export function loadManifest(env) {
|
|
145
250
|
const file = paths(env).mcpFile;
|
|
146
251
|
let text = "";
|
|
147
252
|
try {
|
|
@@ -162,6 +267,24 @@ export function variantKey(server, profile) {
|
|
|
162
267
|
return profile && server.variants[profile] ? profile : "default";
|
|
163
268
|
}
|
|
164
269
|
/** Effective launch settings for a variant. */
|
|
270
|
+
/** The MCP SDK's fixed wait between closing stdin and SIGTERM, in seconds. */
|
|
271
|
+
const SDK_STOP_GRACE = 2;
|
|
272
|
+
/** turno-browser-mcp closes Chrome within about 2.5 s of stdin closing; 5 s leaves room. */
|
|
273
|
+
const BROWSER_STOP_GRACE = 5;
|
|
274
|
+
const MAX_STOP_GRACE = 60;
|
|
275
|
+
/**
|
|
276
|
+
* Seconds mcpd gives a stdio server between closing its stdin and SIGTERM: its
|
|
277
|
+
* `stop_grace`, else 5 for turno-browser-mcp however it is started (a path, npx), else the
|
|
278
|
+
* SDK's 2.
|
|
279
|
+
*/
|
|
280
|
+
export function effectiveStopGrace(server, variant) {
|
|
281
|
+
if (server.stopGrace !== undefined)
|
|
282
|
+
return server.stopGrace;
|
|
283
|
+
const { command, args } = effectiveLaunch(server, variant);
|
|
284
|
+
return [command, ...args].some((a) => a.split(/[\\/]/).pop() === "turno-browser-mcp")
|
|
285
|
+
? BROWSER_STOP_GRACE
|
|
286
|
+
: SDK_STOP_GRACE;
|
|
287
|
+
}
|
|
165
288
|
export function effectiveLaunch(server, variant) {
|
|
166
289
|
const v = server.variants[variant];
|
|
167
290
|
const out = {
|
|
@@ -174,4 +297,28 @@ export function effectiveLaunch(server, variant) {
|
|
|
174
297
|
out.cwd = cwd;
|
|
175
298
|
return out;
|
|
176
299
|
}
|
|
300
|
+
/** The tool filter a profile's sessions get: the server's, with the variant's keys in place of its own. */
|
|
301
|
+
export function toolFilterFor(server, profile) {
|
|
302
|
+
const base = server.tools ?? {};
|
|
303
|
+
const v = server.variants[variantKey(server, profile)]?.tools;
|
|
304
|
+
return v ? { ...base, ...v } : base;
|
|
305
|
+
}
|
|
306
|
+
function globRegExp(pattern) {
|
|
307
|
+
const body = pattern
|
|
308
|
+
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
309
|
+
.replace(/\*/g, ".*")
|
|
310
|
+
.replace(/\?/g, ".");
|
|
311
|
+
return new RegExp(`^${body}$`);
|
|
312
|
+
}
|
|
313
|
+
/** Whether a tool passes a filter. */
|
|
314
|
+
export function toolAllowed(filter, tool) {
|
|
315
|
+
const hit = (patterns) => patterns.some((p) => globRegExp(p).test(tool));
|
|
316
|
+
if (filter.allow && !hit(filter.allow))
|
|
317
|
+
return false;
|
|
318
|
+
return !(filter.deny && hit(filter.deny));
|
|
319
|
+
}
|
|
320
|
+
/** Whether a filter can hide anything. */
|
|
321
|
+
export function filtersTools(filter) {
|
|
322
|
+
return filter.allow !== undefined || filter.deny !== undefined;
|
|
323
|
+
}
|
|
177
324
|
//# sourceMappingURL=manifest.js.map
|
package/dist/core/paths.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const data = env.TURNO_DATA_DIR || join(env.XDG_DATA_HOME || join(home, ".local", "share"), "turno");
|
|
1
|
+
import { isAbsolute, join, normalize } from "node:path";
|
|
2
|
+
import { browserPaths, turnoRoots } from "turno-core/paths";
|
|
3
|
+
// The roots, private folders and PATH lookup are shared with turno-browser.
|
|
4
|
+
export { PRIVATE_DIR_MODE, PRIVATE_FILE_MODE, ensurePrivateDir, findOnPath, isPrivatePath, privateRoots, resetPrivateDirCache, writePrivateFile, } from "turno-core/paths";
|
|
5
|
+
export function paths(env) {
|
|
6
|
+
const { home, config, state, data } = turnoRoots(env);
|
|
8
7
|
return {
|
|
9
8
|
home,
|
|
10
9
|
hub: env.TURNO_HUB || join(home, ".claude"),
|
|
@@ -28,22 +27,24 @@ export function paths(env = process.env) {
|
|
|
28
27
|
mcpdManaged: join(state, "mcp-managed.json"),
|
|
29
28
|
mcpdCacheDir: join(data, "mcpd", "cache"),
|
|
30
29
|
serversDir: join(data, "servers"),
|
|
31
|
-
browserDir:
|
|
30
|
+
browserDir: browserPaths(env).browserDir,
|
|
32
31
|
chromeProfilesDir: join(data, "chrome"),
|
|
33
32
|
legacyConfig: join(home, ".config", "claudio"),
|
|
34
33
|
};
|
|
35
34
|
}
|
|
36
35
|
/** Config dir of a profile: ~/.claude-<name>. */
|
|
37
|
-
export function profileDir(name, env
|
|
36
|
+
export function profileDir(name, env) {
|
|
38
37
|
return join(paths(env).home, `.claude-${name}`);
|
|
39
38
|
}
|
|
40
39
|
/** Replace the home prefix with ~ for display. */
|
|
41
|
-
export function tildify(p, env
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
export function tildify(p, env) {
|
|
41
|
+
// HOME may come with a trailing slash or doubled slashes (HOME=/Users/a/): compare normalized.
|
|
42
|
+
const home = normalize(paths(env).home).replace(/(.)\/+$/, "$1");
|
|
43
|
+
const q = isAbsolute(p) ? normalize(p) : p;
|
|
44
|
+
return q === home ? "~" : home !== "/" && q.startsWith(`${home}/`) ? `~${q.slice(home.length)}` : p;
|
|
44
45
|
}
|
|
45
46
|
/** Expand a leading ~ to the home directory. */
|
|
46
|
-
export function expandHome(p, env
|
|
47
|
+
export function expandHome(p, env) {
|
|
47
48
|
const home = paths(env).home;
|
|
48
49
|
if (p === "~")
|
|
49
50
|
return home;
|