sideby 0.40.1 → 0.41.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/dist/build.json +1 -1
- package/dist/src/cli/commands.d.ts +7 -0
- package/dist/src/cli/commands.js +144 -0
- package/dist/src/cli/json-schemas.js +68 -0
- package/dist/src/cli/main.js +30 -0
- package/dist/src/core/claude-trust.d.ts +198 -0
- package/dist/src/core/claude-trust.js +559 -0
- package/dist/src/core/cli-commands.js +14 -2
- package/dist/src/core/config.d.ts +7 -3
- package/dist/src/core/config.js +12 -5
- package/dist/src/core/search-catalog.js +23 -0
- package/dist/src/panel/handler.d.ts +14 -0
- package/dist/src/panel/handler.js +55 -0
- package/dist/src/panel/i18n.d.ts +22 -0
- package/dist/src/panel/i18n.js +44 -0
- package/dist/src/panel/page-script.js +79 -1
- package/dist/src/runtime.d.ts +32 -0
- package/dist/src/runtime.js +74 -1
- package/package.json +1 -1
- package/schemas/config.json +5 -1
- package/schemas/trust-set.json +117 -0
- package/schemas/trust-undo.json +155 -0
- package/schemas/trust.json +183 -0
- package/skills/sideby/SKILL.md +2 -1
- package/skills/sideby/references/trust.md +28 -0
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
// Carried trust between Claude Accounts (spec §3.43, ADR-0078). Claude Code asks "Is this a project you created or
|
|
2
|
+
// one you trust?" in every Account that has not trusted a folder yet, and keeps the answer per Account as
|
|
3
|
+
// `projects["<path>"].hasTrustDialogAccepted` in that Account's `.claude.json`. With config `claude.carryTrust` on,
|
|
4
|
+
// before sideby starts an interactive Claude session it copies that one mark from another Account that already trusts
|
|
5
|
+
// the folder: the same key, never a wider or narrower one, never trust nobody gave.
|
|
6
|
+
//
|
|
7
|
+
// `.claude.json` is a credential file: of it only `projects.*.hasTrustDialogAccepted` is read, no other value reaches
|
|
8
|
+
// an output, an error or a record, and errors name the path only. No git command is ever run in the folder (an
|
|
9
|
+
// untrusted repository's config can run programs); its `.git` files are read instead.
|
|
10
|
+
import { constants } from 'node:fs';
|
|
11
|
+
import { lstat, open, readdir, realpath, rename, rm } from 'node:fs/promises';
|
|
12
|
+
import { basename, dirname, isAbsolute, join, resolve } from 'node:path';
|
|
13
|
+
import { isObj, render } from './claude-settings-file.js';
|
|
14
|
+
import { ownedFolder, readSmallFile, sha256, writeFileAtomic, writeIfUnchanged } from './fs-safe.js';
|
|
15
|
+
import { takeFileLock } from './lock.js';
|
|
16
|
+
/** The one field of a `projects` entry sideby reads or writes. */
|
|
17
|
+
export const TRUST_FIELD = 'hasTrustDialogAccepted';
|
|
18
|
+
/** `.claude.json` holds history and caches; a file larger than this is not read. */
|
|
19
|
+
const MAX_JSON_BYTES = 64 * 1024 * 1024;
|
|
20
|
+
const realOrNull = (p) => realpath(p).catch(() => null);
|
|
21
|
+
/**
|
|
22
|
+
* The main checkout root of a linked worktree whose checkout root is `root` (its `.git` is a file), or null when
|
|
23
|
+
* `.git` does not describe one exactly: `gitdir: <common>/worktrees/<name>`, that folder's `gitdir` pointing back at
|
|
24
|
+
* `root/.git`, its `commondir` (or the folder layout) leading to `<common>`, and `<common>` being the `.git` folder of
|
|
25
|
+
* a checkout.
|
|
26
|
+
*/
|
|
27
|
+
async function worktreeMain(root) {
|
|
28
|
+
const text = await readSmallFile(join(root, '.git'), 4096);
|
|
29
|
+
const m = text === undefined ? null : /^gitdir:[ \t]*(\S.*?)\s*$/m.exec(text.split('\n')[0] ?? '');
|
|
30
|
+
if (!m)
|
|
31
|
+
return null;
|
|
32
|
+
const dir = await realOrNull(resolve(root, m[1]));
|
|
33
|
+
if (!dir || basename(dirname(dir)) !== 'worktrees')
|
|
34
|
+
return null;
|
|
35
|
+
const back = (await readSmallFile(join(dir, 'gitdir'), 4096))?.trim();
|
|
36
|
+
if (!back)
|
|
37
|
+
return null;
|
|
38
|
+
const [backReal, ownReal] = await Promise.all([
|
|
39
|
+
realOrNull(resolve(dir, back)),
|
|
40
|
+
realOrNull(join(root, '.git')),
|
|
41
|
+
]);
|
|
42
|
+
if (!backReal || backReal !== ownReal)
|
|
43
|
+
return null;
|
|
44
|
+
const layout = await realOrNull(dirname(dirname(dir)));
|
|
45
|
+
const named = (await readSmallFile(join(dir, 'commondir'), 4096))?.trim();
|
|
46
|
+
const common = named ? await realOrNull(resolve(dir, named)) : layout;
|
|
47
|
+
if (!common || common !== layout || basename(common) !== '.git')
|
|
48
|
+
return null;
|
|
49
|
+
const main = dirname(common);
|
|
50
|
+
const st = await lstat(join(main, '.git')).catch(() => null);
|
|
51
|
+
if (!st?.isDirectory() || (await realOrNull(join(main, '.git'))) !== common)
|
|
52
|
+
return null;
|
|
53
|
+
return main;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Where trust is looked up for `cwd`, from the files alone: walks up from its real path to the first `.git`. A `.git`
|
|
57
|
+
* folder makes that folder the root; a `.git` file must describe a linked worktree exactly (`worktreeMain`). Anything
|
|
58
|
+
* else (a submodule, a separate git folder, a link, a file sideby cannot read) is `unclear`. Without any `.git`, the
|
|
59
|
+
* folder and its parents count.
|
|
60
|
+
*/
|
|
61
|
+
export async function trustRoot(cwd) {
|
|
62
|
+
const folder = await realOrNull(cwd);
|
|
63
|
+
if (!folder)
|
|
64
|
+
return { kind: 'unclear', folder: cwd, why: 'the folder cannot be found' };
|
|
65
|
+
for (let d = folder;;) {
|
|
66
|
+
const st = await lstat(join(d, '.git')).catch((err) => err.code === 'ENOENT' || err.code === 'ENOTDIR' ? null : err);
|
|
67
|
+
if (st instanceof Error)
|
|
68
|
+
return { kind: 'unclear', folder, why: `${join(d, '.git')} cannot be read` };
|
|
69
|
+
if (st?.isDirectory())
|
|
70
|
+
return { kind: 'git', folder, path: d };
|
|
71
|
+
if (st?.isFile()) {
|
|
72
|
+
const main = await worktreeMain(d);
|
|
73
|
+
return main
|
|
74
|
+
? { kind: 'git', folder, path: main }
|
|
75
|
+
: {
|
|
76
|
+
kind: 'unclear',
|
|
77
|
+
folder,
|
|
78
|
+
why: `${join(d, '.git')} is not a linked worktree sideby can follow (a submodule or a separate git folder)`,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (st)
|
|
82
|
+
return { kind: 'unclear', folder, why: `${join(d, '.git')} is not a folder or a file` };
|
|
83
|
+
const up = dirname(d);
|
|
84
|
+
if (up === d)
|
|
85
|
+
break;
|
|
86
|
+
d = up;
|
|
87
|
+
}
|
|
88
|
+
return { kind: 'folder', folder, path: folder };
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The keys that make an Account trust the folder, narrowest first: the main checkout root in a git checkout, else the
|
|
92
|
+
* folder and every parent. HOME and a filesystem root never count, as Claude Code does not keep trust for them.
|
|
93
|
+
*/
|
|
94
|
+
export async function trustCandidates(root, home) {
|
|
95
|
+
if (root.kind === 'unclear')
|
|
96
|
+
return [];
|
|
97
|
+
const homes = new Set([home, (await realOrNull(home)) ?? home]);
|
|
98
|
+
const skip = (p) => homes.has(p) || dirname(p) === p;
|
|
99
|
+
if (root.kind === 'git')
|
|
100
|
+
return skip(root.path) ? [] : [root.path];
|
|
101
|
+
const out = [];
|
|
102
|
+
for (let d = root.path; dirname(d) !== d; d = dirname(d))
|
|
103
|
+
if (!skip(d))
|
|
104
|
+
out.push(d);
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* `file` with every folder above it resolved and its own name kept: the file sideby reads and writes, fixed once, so a
|
|
109
|
+
* folder swapped afterwards cannot move a write (a link at the file itself is still refused when it is opened).
|
|
110
|
+
*/
|
|
111
|
+
async function realFile(file) {
|
|
112
|
+
try {
|
|
113
|
+
return join(await realpath(dirname(file)), basename(file));
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
const code = err.code;
|
|
117
|
+
return code === 'ENOENT' || code === 'ENOTDIR' ? null : Promise.reject(err);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** The text of `file`, opened without following a link; never quotes the file in an error. */
|
|
121
|
+
async function readNoFollow(file) {
|
|
122
|
+
let st;
|
|
123
|
+
try {
|
|
124
|
+
st = await lstat(file);
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
// Only a file that is not there is missing; a folder sideby cannot enter is a problem, so undo keeps its record.
|
|
128
|
+
const code = err.code;
|
|
129
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
130
|
+
return { status: 'missing' };
|
|
131
|
+
return { status: 'problem', why: `${file} cannot be read` };
|
|
132
|
+
}
|
|
133
|
+
if (st.isSymbolicLink())
|
|
134
|
+
return { status: 'problem', why: `${file} is a link` };
|
|
135
|
+
if (!st.isFile())
|
|
136
|
+
return { status: 'problem', why: `${file} is not a regular file` };
|
|
137
|
+
if (st.size > MAX_JSON_BYTES)
|
|
138
|
+
return { status: 'problem', why: `${file} is too large to read` };
|
|
139
|
+
let fh;
|
|
140
|
+
try {
|
|
141
|
+
fh = await open(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
142
|
+
const fst = await fh.stat();
|
|
143
|
+
if (!fst.isFile() || fst.size > MAX_JSON_BYTES)
|
|
144
|
+
return { status: 'problem', why: `${file} is not a regular file` };
|
|
145
|
+
return { text: await fh.readFile('utf8') };
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return { status: 'problem', why: `${file} cannot be read` };
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
await fh?.close().catch(() => { });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** Parses `.claude.json` text into an object; the error names the file only, never a slice of it. */
|
|
155
|
+
function parseObject(text, file) {
|
|
156
|
+
let parsed;
|
|
157
|
+
try {
|
|
158
|
+
parsed = JSON.parse(text);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return { why: `${file} is not valid JSON` };
|
|
162
|
+
}
|
|
163
|
+
return isObj(parsed) ? { obj: parsed } : { why: `${file} is not a JSON object` };
|
|
164
|
+
}
|
|
165
|
+
/** The keys of `.claude.json` whose `hasTrustDialogAccepted` is `true`; nothing else of the file is kept. */
|
|
166
|
+
export async function readTrustKeys(file) {
|
|
167
|
+
const read = await readNoFollow(file);
|
|
168
|
+
if (!('text' in read))
|
|
169
|
+
return read;
|
|
170
|
+
const parsed = parseObject(read.text, file);
|
|
171
|
+
if ('why' in parsed)
|
|
172
|
+
return { status: 'problem', why: parsed.why };
|
|
173
|
+
const projects = parsed.obj.projects;
|
|
174
|
+
if (projects === undefined)
|
|
175
|
+
return { status: 'ok', keys: new Set() };
|
|
176
|
+
if (!isObj(projects))
|
|
177
|
+
return { status: 'problem', why: `projects in ${file} is not an object` };
|
|
178
|
+
const keys = new Set();
|
|
179
|
+
for (const [k, v] of Object.entries(projects))
|
|
180
|
+
if (isObj(v) && v[TRUST_FIELD] === true)
|
|
181
|
+
keys.add(k);
|
|
182
|
+
return { status: 'ok', keys };
|
|
183
|
+
}
|
|
184
|
+
/** The narrowest key of `candidates` that `read` trusts, or null. Keys compare as written (a real path). */
|
|
185
|
+
export function trustedKey(read, candidates) {
|
|
186
|
+
if (read.status !== 'ok')
|
|
187
|
+
return null;
|
|
188
|
+
return candidates.find((k) => read.keys.has(k)) ?? null;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Sets `projects[key].hasTrustDialogAccepted` to true in `file`, under the rules of Share Mode `json-key`: never
|
|
192
|
+
* through a link, never in a file that is not a JSON object, a `projects` or an entry that is not an object, or a field
|
|
193
|
+
* that is neither absent nor `false`; only that one field (a new entry is `{ "hasTrustDialogAccepted": true }`), every
|
|
194
|
+
* other key and its order and the file's indentation kept; written atomically (mode 600) only while the file still
|
|
195
|
+
* has the hash it was read with. Never creates the file.
|
|
196
|
+
*/
|
|
197
|
+
export async function writeTrust(file, key, hooks = {}) {
|
|
198
|
+
const read = await readNoFollow(file);
|
|
199
|
+
if (!('text' in read))
|
|
200
|
+
return {
|
|
201
|
+
ok: false,
|
|
202
|
+
why: read.status === 'missing'
|
|
203
|
+
? `${file} does not exist yet (the account has not been started)`
|
|
204
|
+
: read.why,
|
|
205
|
+
};
|
|
206
|
+
const parsed = parseObject(read.text, file);
|
|
207
|
+
if ('why' in parsed)
|
|
208
|
+
return { ok: false, why: parsed.why };
|
|
209
|
+
const obj = parsed.obj;
|
|
210
|
+
const projects = obj.projects;
|
|
211
|
+
if (projects !== undefined && !isObj(projects))
|
|
212
|
+
return { ok: false, why: `projects in ${file} is not an object` };
|
|
213
|
+
const entry = projects?.[key];
|
|
214
|
+
if (entry !== undefined && !isObj(entry))
|
|
215
|
+
return { ok: false, why: `the entry for this folder in ${file} is not an object` };
|
|
216
|
+
const field = entry?.[TRUST_FIELD];
|
|
217
|
+
if (field === true)
|
|
218
|
+
return { ok: true, already: true };
|
|
219
|
+
if (field !== undefined && field !== false)
|
|
220
|
+
return { ok: false, why: `the trust field for this folder in ${file} is neither true nor false` };
|
|
221
|
+
const before = entry === undefined ? 'no-entry' : field === undefined ? 'no-field' : 'false';
|
|
222
|
+
// Spreading keeps every key where it was; a key the object did not have goes last.
|
|
223
|
+
const nextEntry = entry === undefined ? { [TRUST_FIELD]: true } : { ...entry, [TRUST_FIELD]: true };
|
|
224
|
+
const next = { ...obj, projects: { ...(projects ?? {}), [key]: nextEntry } };
|
|
225
|
+
await hooks.afterRead?.();
|
|
226
|
+
const wrote = await writeIfUnchanged(file, sha256(read.text), render(next, read.text), 0o600).catch((err) => err);
|
|
227
|
+
if (wrote instanceof Error)
|
|
228
|
+
return { ok: false, why: `${file} could not be written` };
|
|
229
|
+
if (!wrote)
|
|
230
|
+
return { ok: false, why: `${file} changed while sideby was writing it` };
|
|
231
|
+
return { ok: true, before };
|
|
232
|
+
}
|
|
233
|
+
const carriedDir = (stateDir, create) => ownedFolder(stateDir, ['trust', 'carried'], { create, private: create });
|
|
234
|
+
const carryName = (file, key) => `${sha256(`${file}\0${key}`).slice(0, 32)}.json`;
|
|
235
|
+
function isCarry(v) {
|
|
236
|
+
if (!isObj(v))
|
|
237
|
+
return false;
|
|
238
|
+
return (v.version === 1 &&
|
|
239
|
+
['account', 'file', 'real', 'key', 'from', 'at'].every((k) => typeof v[k] === 'string') &&
|
|
240
|
+
isAbsolute(v.file) &&
|
|
241
|
+
isAbsolute(v.real) &&
|
|
242
|
+
['no-entry', 'no-field', 'false'].includes(v.before));
|
|
243
|
+
}
|
|
244
|
+
/** Every carry sideby recorded, oldest first; a record that cannot be read is left out. */
|
|
245
|
+
export async function readCarries(stateDir) {
|
|
246
|
+
const dir = await carriedDir(stateDir, false).catch(() => null);
|
|
247
|
+
if (!dir)
|
|
248
|
+
return [];
|
|
249
|
+
const out = [];
|
|
250
|
+
const names = (await readdir(dir).catch(() => [])).sort();
|
|
251
|
+
for (const name of names) {
|
|
252
|
+
// `<id>.json.undoing` is a record an undo set aside and could not put back: listed, never used to undo.
|
|
253
|
+
const parked = /^[0-9a-f]{32}\.json\.undoing$/.test(name);
|
|
254
|
+
if (!parked && !/^[0-9a-f]{32}\.json$/.test(name))
|
|
255
|
+
continue;
|
|
256
|
+
if (parked && names.includes(name.slice(0, -'.undoing'.length)))
|
|
257
|
+
continue;
|
|
258
|
+
const carry = await readCarry(join(dir, name));
|
|
259
|
+
if (carry)
|
|
260
|
+
out.push({ ...carry, record: join(dir, name), ...(parked ? { parked: true } : {}) });
|
|
261
|
+
}
|
|
262
|
+
return out.sort((a, b) => a.at.localeCompare(b.at) || `${a.account}\0${a.key}`.localeCompare(`${b.account}\0${b.key}`));
|
|
263
|
+
}
|
|
264
|
+
/** One carry record, or null when it is gone or not a record. */
|
|
265
|
+
async function readCarry(record) {
|
|
266
|
+
const text = await readSmallFile(record, 64 * 1024);
|
|
267
|
+
if (text === undefined)
|
|
268
|
+
return null;
|
|
269
|
+
try {
|
|
270
|
+
const v = JSON.parse(text);
|
|
271
|
+
return isCarry(v) ? v : null;
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
async function recordCarry(stateDir, carry) {
|
|
278
|
+
const dir = await carriedDir(stateDir, true);
|
|
279
|
+
if (!dir)
|
|
280
|
+
throw new Error('no state folder');
|
|
281
|
+
await writeFileAtomic(join(dir, carryName(carry.file, carry.key)), `${JSON.stringify(carry, null, 2)}\n`, 0o600);
|
|
282
|
+
}
|
|
283
|
+
/** How long a carry waits for another sideby change of the same `.claude.json`; mutable for tests only. */
|
|
284
|
+
export const trustLockWait = { timeoutMs: 2_000, pollMs: 50 };
|
|
285
|
+
/** Runs `fn` while no other sideby process changes `file` (the lock Claude settings writers use too). */
|
|
286
|
+
async function withFileLock(stateDir, file, fn) {
|
|
287
|
+
const dir = await ownedFolder(stateDir, ['settings-locks'], { create: true });
|
|
288
|
+
if (!dir)
|
|
289
|
+
return null;
|
|
290
|
+
const release = await takeFileLock(join(dir, `${sha256(file).slice(0, 16)}.lock`), {
|
|
291
|
+
staleMs: 15_000,
|
|
292
|
+
beatMs: 3_000,
|
|
293
|
+
wait: { ...trustLockWait },
|
|
294
|
+
});
|
|
295
|
+
if (!release)
|
|
296
|
+
return null;
|
|
297
|
+
try {
|
|
298
|
+
return await fn();
|
|
299
|
+
}
|
|
300
|
+
finally {
|
|
301
|
+
await release();
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Before `target` starts in `cwd`: when it does not trust the folder and another Claude Account does, copies that
|
|
306
|
+
* Account's matching key into `target`'s `.claude.json` and records the carry. Reads the other Accounts only when the
|
|
307
|
+
* target lacks trust. Never throws for a file it cannot read or write: the answer says why.
|
|
308
|
+
*/
|
|
309
|
+
export async function carryTrust(ctx, target, cwd, opts) {
|
|
310
|
+
const candidates = await trustCandidates(await trustRoot(cwd), ctx.home);
|
|
311
|
+
if (!candidates.length)
|
|
312
|
+
return { status: 'no-root' };
|
|
313
|
+
const file = ctx.jsonOf(target);
|
|
314
|
+
if (trustedKey(await readTrustKeys(file), candidates))
|
|
315
|
+
return { status: 'already' };
|
|
316
|
+
let pick = null;
|
|
317
|
+
const reads = new Map();
|
|
318
|
+
const others = ctx.accounts.filter((a) => a.family === 'claude' && a.ref !== target.ref);
|
|
319
|
+
for (const key of candidates) {
|
|
320
|
+
for (const a of others) {
|
|
321
|
+
const other = ctx.jsonOf(a);
|
|
322
|
+
if (other === file)
|
|
323
|
+
continue;
|
|
324
|
+
if (!reads.has(other))
|
|
325
|
+
reads.set(other, await readTrustKeys(other));
|
|
326
|
+
if (trustedKey(reads.get(other), [key])) {
|
|
327
|
+
pick = { key, from: a };
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (pick)
|
|
332
|
+
break;
|
|
333
|
+
}
|
|
334
|
+
if (!pick)
|
|
335
|
+
return { status: 'none' };
|
|
336
|
+
const { key, from } = pick;
|
|
337
|
+
// The file first, the record after, both under the file's lock: a write that failed leaves no record claiming a
|
|
338
|
+
// carry, and an undo running meanwhile cannot remove the record of a carry made after it restored the field.
|
|
339
|
+
let recorded = false;
|
|
340
|
+
const done = await withFileLock(ctx.stateDir, file, async () => {
|
|
341
|
+
const real = await realFile(file).catch(() => null);
|
|
342
|
+
if (!real)
|
|
343
|
+
return { ok: false, why: `${file} cannot be read` };
|
|
344
|
+
// Looked at again under the lock: a carry that ran meanwhile already wrote it, and its record stays.
|
|
345
|
+
const now = await readTrustKeys(real);
|
|
346
|
+
// A file sideby cannot read now is not taken for one without trust: nothing changes, the earlier record stays.
|
|
347
|
+
if (now.status !== 'ok')
|
|
348
|
+
return { ok: false, why: now.status === 'missing' ? `${file} is not there` : now.why };
|
|
349
|
+
if (trustedKey(now, [key]))
|
|
350
|
+
return { ok: true, already: true };
|
|
351
|
+
// A record left from an earlier carry of this folder is stale (the mark it wrote is gone, or this carry would not
|
|
352
|
+
// run): it goes first, so it can never undo with an old `before` if the new record cannot be written. A record
|
|
353
|
+
// folder sideby cannot check is not taken for an empty one.
|
|
354
|
+
let dir;
|
|
355
|
+
try {
|
|
356
|
+
dir = await carriedDir(ctx.stateDir, false);
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
return { ok: false, why: "sideby's record folder is not safe to use" };
|
|
360
|
+
}
|
|
361
|
+
if (dir) {
|
|
362
|
+
const cleared = await rm(join(dir, carryName(file, key))).then(() => true, (err) => err.code === 'ENOENT');
|
|
363
|
+
if (!cleared)
|
|
364
|
+
return { ok: false, why: 'sideby could not clear its earlier record of this folder' };
|
|
365
|
+
}
|
|
366
|
+
const wrote = await writeTrust(real, key, opts.hooks);
|
|
367
|
+
if (wrote.ok && !('already' in wrote))
|
|
368
|
+
recorded = await recordCarry(ctx.stateDir, {
|
|
369
|
+
version: 1,
|
|
370
|
+
account: target.ref,
|
|
371
|
+
file,
|
|
372
|
+
real,
|
|
373
|
+
key,
|
|
374
|
+
before: wrote.before,
|
|
375
|
+
from: from.ref,
|
|
376
|
+
at: opts.now.toISOString(),
|
|
377
|
+
})
|
|
378
|
+
.then(() => true)
|
|
379
|
+
.catch(() => false);
|
|
380
|
+
return wrote;
|
|
381
|
+
});
|
|
382
|
+
if (!done)
|
|
383
|
+
return {
|
|
384
|
+
status: 'skipped',
|
|
385
|
+
key,
|
|
386
|
+
from: from.ref,
|
|
387
|
+
why: 'another sideby change of the file is still running',
|
|
388
|
+
};
|
|
389
|
+
if (!done.ok)
|
|
390
|
+
return { status: 'skipped', key, from: from.ref, why: done.why };
|
|
391
|
+
if ('already' in done)
|
|
392
|
+
return { status: 'already' };
|
|
393
|
+
return { status: 'carried', key, from: from.ref, ...(recorded ? {} : { recordProblem: true }) };
|
|
394
|
+
}
|
|
395
|
+
/** The one line a Launch prints for a carry, or null when there is nothing to say. `show` shortens a path. */
|
|
396
|
+
export function carryLine(r, target, show) {
|
|
397
|
+
if (r.status === 'carried')
|
|
398
|
+
return `${target} now trusts ${show(r.key)}, as ${r.from} does (claude.carryTrust; undo: sideby trust undo)${r.recordProblem ? '; sideby could not record it, so undo will not find it' : ''}`;
|
|
399
|
+
if (r.status === 'skipped')
|
|
400
|
+
return `${target} was not given ${r.from}'s trust of ${show(r.key)}: ${show(r.why)}. Claude Code will ask whether you trust this folder`;
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
export async function trustStates(ctx, cwd) {
|
|
404
|
+
const root = await trustRoot(cwd);
|
|
405
|
+
const candidates = await trustCandidates(root, ctx.home);
|
|
406
|
+
const list = ctx.accounts.filter((a) => a.family === 'claude');
|
|
407
|
+
const reads = await Promise.all(list.map((a) => readTrustKeys(ctx.jsonOf(a))));
|
|
408
|
+
const trusts = reads.map((r) => r.status === 'ok' && candidates.length ? Boolean(trustedKey(r, candidates)) : undefined);
|
|
409
|
+
const accounts = list.map((a, i) => {
|
|
410
|
+
const r = reads[i];
|
|
411
|
+
const own = trusts[i];
|
|
412
|
+
let carryFrom;
|
|
413
|
+
// The same pick a Launch makes: the narrowest key, then the first Account that trusts it.
|
|
414
|
+
for (const key of own === false ? candidates : []) {
|
|
415
|
+
const j = list.findIndex((b, k) => k !== i && ctx.jsonOf(b) !== ctx.jsonOf(a) && trustedKey(reads[k], [key]));
|
|
416
|
+
if (j >= 0) {
|
|
417
|
+
carryFrom = list[j].ref;
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return {
|
|
422
|
+
ref: a.ref,
|
|
423
|
+
...(own === undefined ? {} : { trusts: own }),
|
|
424
|
+
...(r.status === 'problem' ? { problem: r.why } : {}),
|
|
425
|
+
...(r.status === 'missing' ? { problem: `${ctx.jsonOf(a)} does not exist yet` } : {}),
|
|
426
|
+
...(carryFrom ? { carryFrom } : {}),
|
|
427
|
+
};
|
|
428
|
+
});
|
|
429
|
+
return { root, accounts };
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Takes back what one carry wrote, only while the field still holds `true` (what sideby wrote): a field that was
|
|
433
|
+
* absent goes again (with the entry, when sideby made it and nothing else was added to it), one that was `false` is
|
|
434
|
+
* `false` again. Every other key is kept. A change of the field since then is the user's (`changed`).
|
|
435
|
+
*/
|
|
436
|
+
async function undoOne(c, apply, hooks) {
|
|
437
|
+
const real = await realFile(c.file).catch(() => undefined);
|
|
438
|
+
if (real === undefined)
|
|
439
|
+
return { status: 'blocked', why: `${c.file} cannot be read` };
|
|
440
|
+
if (real !== null && real !== c.real)
|
|
441
|
+
return {
|
|
442
|
+
status: 'blocked',
|
|
443
|
+
why: `${c.file} now leads somewhere else than when sideby wrote it, so sideby left it alone`,
|
|
444
|
+
};
|
|
445
|
+
// From here on only the path sideby wrote: a folder swapped after this check cannot move the write elsewhere.
|
|
446
|
+
const read = await readNoFollow(c.real);
|
|
447
|
+
if (!('text' in read))
|
|
448
|
+
return read.status === 'missing' ? { status: 'gone' } : { status: 'blocked', why: read.why };
|
|
449
|
+
const parsed = parseObject(read.text, c.real);
|
|
450
|
+
if ('why' in parsed)
|
|
451
|
+
return { status: 'blocked', why: parsed.why };
|
|
452
|
+
const obj = parsed.obj;
|
|
453
|
+
const projects = obj.projects;
|
|
454
|
+
const entry = isObj(projects) ? projects[c.key] : undefined;
|
|
455
|
+
if (!isObj(projects) || !isObj(entry) || entry[TRUST_FIELD] !== true)
|
|
456
|
+
return { status: 'changed' };
|
|
457
|
+
if (!apply)
|
|
458
|
+
return { status: 'undo' };
|
|
459
|
+
let nextProjects;
|
|
460
|
+
if (c.before === 'false')
|
|
461
|
+
nextProjects = { ...projects, [c.key]: { ...entry, [TRUST_FIELD]: false } };
|
|
462
|
+
else {
|
|
463
|
+
const { [TRUST_FIELD]: _drop, ...rest } = entry;
|
|
464
|
+
if (c.before === 'no-entry' && !Object.keys(rest).length) {
|
|
465
|
+
const { [c.key]: _gone, ...others } = projects;
|
|
466
|
+
nextProjects = others;
|
|
467
|
+
}
|
|
468
|
+
else
|
|
469
|
+
nextProjects = { ...projects, [c.key]: rest };
|
|
470
|
+
}
|
|
471
|
+
await hooks.afterRead?.();
|
|
472
|
+
const wrote = await writeIfUnchanged(c.real, sha256(read.text), render({ ...obj, projects: nextProjects }, read.text), 0o600).catch(() => null);
|
|
473
|
+
if (wrote === null)
|
|
474
|
+
return { status: 'blocked', why: `${c.file} could not be written` };
|
|
475
|
+
return wrote
|
|
476
|
+
? { status: 'undone' }
|
|
477
|
+
: { status: 'busy', why: `${c.file} changed while sideby was writing it` };
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* `sideby trust undo`: every recorded carry, with what undoing it does (`apply` false) or did. A carry that was undone,
|
|
481
|
+
* or whose field the user changed since or whose file is gone, loses its record; one that is blocked or busy keeps it.
|
|
482
|
+
*/
|
|
483
|
+
export async function undoCarries(stateDir, opts) {
|
|
484
|
+
const out = [];
|
|
485
|
+
const idOf = (record) => basename(record).slice(0, 32);
|
|
486
|
+
for (const c of await readCarries(stateDir)) {
|
|
487
|
+
if (opts.only && !opts.only.some((o) => o.id === idOf(c.record) && o.at === c.at))
|
|
488
|
+
continue;
|
|
489
|
+
const r = c.parked
|
|
490
|
+
? {
|
|
491
|
+
status: 'blocked',
|
|
492
|
+
why: `an earlier undo stopped halfway, so sideby will not use this record again (${c.record}); check in Claude Code whether ${c.account} should still trust this folder`,
|
|
493
|
+
}
|
|
494
|
+
: opts.apply
|
|
495
|
+
? ((await withFileLock(stateDir, c.file, async () => {
|
|
496
|
+
// Read again under the lock: a carry made since the listing replaced this record and is not this one.
|
|
497
|
+
const now = await readCarry(c.record);
|
|
498
|
+
if (!now || now.at !== c.at || now.before !== c.before || now.key !== c.key)
|
|
499
|
+
return {
|
|
500
|
+
status: 'busy',
|
|
501
|
+
why: 'sideby carried this trust again meanwhile; run `sideby trust undo` again',
|
|
502
|
+
};
|
|
503
|
+
// Set the record aside first: when its folder cannot be changed, the mark is not touched either, so no record
|
|
504
|
+
// that could later take back trust the user gave by hand is left behind a successful undo.
|
|
505
|
+
const parked = `${c.record}.undoing`;
|
|
506
|
+
try {
|
|
507
|
+
await rename(c.record, parked);
|
|
508
|
+
}
|
|
509
|
+
catch {
|
|
510
|
+
return {
|
|
511
|
+
status: 'blocked',
|
|
512
|
+
why: `${dirname(c.record)} cannot be changed, so sideby left the trust as it is`,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
const res = await undoOne(c, true, opts.hooks ?? {});
|
|
516
|
+
if (res.status === 'undone' || res.status === 'changed' || res.status === 'gone')
|
|
517
|
+
await rm(parked, { force: true }).catch(() => { }); // a parked name is never used to undo
|
|
518
|
+
else if (!(await rename(parked, c.record).then(() => true, () => false)))
|
|
519
|
+
return {
|
|
520
|
+
status: 'blocked',
|
|
521
|
+
why: `${res.why ?? 'the trust was not changed'}; sideby could not put its record back, so the next \`sideby trust undo\` lists it as stopped halfway`,
|
|
522
|
+
};
|
|
523
|
+
return res;
|
|
524
|
+
})) ?? {
|
|
525
|
+
status: 'busy',
|
|
526
|
+
why: 'another sideby change of the file is still running',
|
|
527
|
+
})
|
|
528
|
+
: await undoOne(c, false, {});
|
|
529
|
+
out.push({
|
|
530
|
+
id: idOf(c.record),
|
|
531
|
+
account: c.account,
|
|
532
|
+
key: c.key,
|
|
533
|
+
from: c.from,
|
|
534
|
+
at: c.at,
|
|
535
|
+
status: r.status,
|
|
536
|
+
...(r.why ? { why: r.why } : {}),
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
return out;
|
|
540
|
+
}
|
|
541
|
+
/** `sideby trust` (spec §3.43): never reads a `.claude.json` while the setting is off. */
|
|
542
|
+
export async function trustStatus(ctx, on, cwd) {
|
|
543
|
+
const carried = (await readCarries(ctx.stateDir)).map(({ account, key, from, at }) => ({
|
|
544
|
+
account,
|
|
545
|
+
key,
|
|
546
|
+
from,
|
|
547
|
+
at,
|
|
548
|
+
}));
|
|
549
|
+
if (!on || cwd === undefined)
|
|
550
|
+
return { on, carried };
|
|
551
|
+
const { root, accounts } = await trustStates(ctx, cwd);
|
|
552
|
+
return {
|
|
553
|
+
on,
|
|
554
|
+
carried,
|
|
555
|
+
dir: root.folder,
|
|
556
|
+
root: root.kind === 'unclear' ? { kind: root.kind, why: root.why } : { kind: root.kind, path: root.path },
|
|
557
|
+
accounts,
|
|
558
|
+
};
|
|
559
|
+
}
|
|
@@ -99,6 +99,18 @@ export const CLI_COMMANDS = [
|
|
|
99
99
|
example: 'sideby advisor opus',
|
|
100
100
|
feature: 'setting.advisor',
|
|
101
101
|
},
|
|
102
|
+
{
|
|
103
|
+
id: 'trust',
|
|
104
|
+
group: 'accounts',
|
|
105
|
+
usage: 'sideby trust [on|off|undo] [--dir <path>] [--yes]',
|
|
106
|
+
help: 'stop Claude asking "do you trust this folder?" again in another account: before sideby starts a Claude session, it copies the trust another of your Claude accounts already gave the folder (on|off, shows the change first; undo takes the copies back)',
|
|
107
|
+
summary: {
|
|
108
|
+
en: 'Let a Claude account reuse folder trust another account already gave',
|
|
109
|
+
zh: '让 Claude 账号沿用另一个账号已给过的文件夹信任',
|
|
110
|
+
},
|
|
111
|
+
example: 'sideby trust on',
|
|
112
|
+
feature: 'setting.trust',
|
|
113
|
+
},
|
|
102
114
|
{
|
|
103
115
|
id: 'edit',
|
|
104
116
|
group: 'accounts',
|
|
@@ -878,9 +890,9 @@ export function cliHelp(list = CLI_COMMANDS) {
|
|
|
878
890
|
const HELP_TAIL = `Options:
|
|
879
891
|
--json machine-readable output (list, new, next, alias, doctor, quota, tune, model, advisor, remote-control, handoff,
|
|
880
892
|
families, commands, plugins, app, desktop, sessions, history, send, messages, edit, browser, update, local, ask,
|
|
881
|
-
terminal, key, try, autostart, menubar, config, network, unattended, progress)
|
|
893
|
+
terminal, key, try, autostart, menubar, config, network, unattended, progress, trust)
|
|
882
894
|
-h, --help show this help; \`sideby <command> --help\` for new, alias, login, doctor, families, commands, desktop, edit, browser,
|
|
883
|
-
autostart, menubar, config, sessions, history, send, messages, network, unattended, progress
|
|
895
|
+
autostart, menubar, config, sessions, history, send, messages, network, unattended, progress, trust
|
|
884
896
|
-v, --version show the version
|
|
885
897
|
|
|
886
898
|
An <account> is <family>:<name> (claude:work) or just <name> when it is unique.
|
|
@@ -123,6 +123,7 @@ export declare const ConfigSchema: Type.TObject<{
|
|
|
123
123
|
}>>;
|
|
124
124
|
claude: Type.TOptional<Type.TObject<{
|
|
125
125
|
apiBetasOff: Type.TOptional<Type.TBoolean>;
|
|
126
|
+
carryTrust: Type.TOptional<Type.TBoolean>;
|
|
126
127
|
}>>;
|
|
127
128
|
unattended: Type.TOptional<Type.TObject<{
|
|
128
129
|
schedule: Type.TOptional<Type.TArray<Type.TObject<{
|
|
@@ -212,10 +213,13 @@ export declare function editConfigAdvisor(file: string, edit: (current: AdvisorC
|
|
|
212
213
|
}>;
|
|
213
214
|
export type ClaudeConfig = NonNullable<Config['claude']>;
|
|
214
215
|
/**
|
|
215
|
-
* The config `claude` key (spec §3.41): changed with `edit` (see `editConfigKey` for how the file is written);
|
|
216
|
-
* returns undefined to remove the section, or null to leave the file as it is.
|
|
216
|
+
* The config `claude` key (spec §3.41, §3.43): changed with `edit` (see `editConfigKey` for how the file is written);
|
|
217
|
+
* `edit` returns undefined to remove the section, or null to leave the file as it is. With `dryRun`, shows the change
|
|
218
|
+
* without writing.
|
|
217
219
|
*/
|
|
218
|
-
export declare function editConfigClaude(file: string, edit: (current: ClaudeConfig) => ClaudeConfig | undefined | null
|
|
220
|
+
export declare function editConfigClaude(file: string, edit: (current: ClaudeConfig) => ClaudeConfig | undefined | null, opts?: {
|
|
221
|
+
dryRun?: boolean;
|
|
222
|
+
}): Promise<{
|
|
219
223
|
changed: boolean;
|
|
220
224
|
before: ClaudeConfig | undefined;
|
|
221
225
|
after: ClaudeConfig | undefined;
|
package/dist/src/core/config.js
CHANGED
|
@@ -250,7 +250,13 @@ export const ConfigSchema = Type.Object({
|
|
|
250
250
|
apiBetasOff: Type.Optional(Type.Boolean({
|
|
251
251
|
description: "Give Claude API accounts `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1` when sideby starts them, for gateways that reject Claude Code's `anthropic-beta` headers. The account's own proxy.env and a value already in the environment win. Off by default; `sideby advisor <model>` turns it on when it removes that variable from a settings file API accounts share.",
|
|
252
252
|
})),
|
|
253
|
-
|
|
253
|
+
carryTrust: Type.Optional(Type.Boolean({
|
|
254
|
+
description: 'Carry folder trust between Claude accounts (spec §3.43, ADR-0078): before sideby starts an interactive Claude session in a folder this account has not trusted yet, when another Claude account of yours already trusts it (the folder, a parent folder outside any git repository, or the repository\'s main checkout), sideby copies that one trust mark (`projects["<path>"].hasTrustDialogAccepted`) into this account\'s `.claude.json`, so Claude Code does not ask again. It never adds trust no account gave. Off by default; turning it off does not remove marks already copied (`sideby trust undo` does).',
|
|
255
|
+
})),
|
|
256
|
+
}, {
|
|
257
|
+
additionalProperties: false,
|
|
258
|
+
description: 'Claude Code launches (spec §3.41, ADR-0074; `carryTrust`: spec §3.43, ADR-0078).',
|
|
259
|
+
})),
|
|
254
260
|
unattended: Type.Optional(Type.Object({
|
|
255
261
|
schedule: Type.Optional(Type.Array(UnattendedSlotSchema, {
|
|
256
262
|
description: "Slots that turn Unattended Mode on and off by themselves, in this computer's time zone. Empty by default.",
|
|
@@ -496,11 +502,12 @@ export function editConfigAdvisor(file, edit, opts = {}) {
|
|
|
496
502
|
return editConfigKey(file, 'advisor', 'change the setting', (current) => edit(current ?? {}), opts);
|
|
497
503
|
}
|
|
498
504
|
/**
|
|
499
|
-
* The config `claude` key (spec §3.41): changed with `edit` (see `editConfigKey` for how the file is written);
|
|
500
|
-
* returns undefined to remove the section, or null to leave the file as it is.
|
|
505
|
+
* The config `claude` key (spec §3.41, §3.43): changed with `edit` (see `editConfigKey` for how the file is written);
|
|
506
|
+
* `edit` returns undefined to remove the section, or null to leave the file as it is. With `dryRun`, shows the change
|
|
507
|
+
* without writing.
|
|
501
508
|
*/
|
|
502
|
-
export function editConfigClaude(file, edit) {
|
|
503
|
-
return editConfigKey(file, 'claude', 'change the setting', (current) => edit(current ?? {}));
|
|
509
|
+
export function editConfigClaude(file, edit, opts = {}) {
|
|
510
|
+
return editConfigKey(file, 'claude', 'change the setting', (current) => edit(current ?? {}), opts);
|
|
504
511
|
}
|
|
505
512
|
/**
|
|
506
513
|
* `sideby unattended on|schedule …` (spec §3.37): changes the config `unattended` key with `edit` (see `editConfigKey`
|