@cleocode/adapters 2026.11.3 → 2026.11.4-canary.1
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/.source-stamp.json +1 -1
- package/dist/index.js +129 -18
- package/dist/index.js.map +2 -2
- package/dist/orca-execution.d.ts +1 -0
- package/dist/orca-execution.d.ts.map +1 -1
- package/dist/orca-execution.js +28 -8
- package/dist/orca-execution.js.map +1 -1
- package/dist/providers/claude-code/index.js +118 -8
- package/dist/providers/claude-code/index.js.map +2 -2
- package/dist/providers/claude-code/lead-mcp.d.ts +40 -0
- package/dist/providers/claude-code/lead-mcp.d.ts.map +1 -1
- package/dist/providers/claude-code/spawn.d.ts.map +1 -1
- package/dist/providers/claude-sdk/index.js.map +1 -1
- package/dist/providers/codex/index.js.map +1 -1
- package/dist/providers/gemini-cli/index.js.map +1 -1
- package/dist/providers/kimi/index.js.map +1 -1
- package/dist/providers/openai-sdk/index.js.map +1 -1
- package/dist/providers/opencode/index.js.map +1 -1
- package/dist/providers/pi/index.js.map +1 -1
- package/package.json +6 -6
- package/src/__tests__/orca-execution.test.ts +130 -15
- package/src/orca-execution.ts +34 -8
- package/src/providers/claude-code/__tests__/lead-mcp.test.ts +341 -4
- package/src/providers/claude-code/lead-mcp.ts +177 -2
- package/src/providers/claude-code/spawn.ts +23 -3
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
mkdirSync,
|
|
23
23
|
mkdtempSync,
|
|
24
24
|
readFileSync,
|
|
25
|
+
realpathSync,
|
|
25
26
|
rmSync,
|
|
26
27
|
statSync,
|
|
27
28
|
symlinkSync,
|
|
@@ -35,7 +36,12 @@ import type { OrcaLeadTerminalLauncher } from '@cleocode/contracts/orca-executio
|
|
|
35
36
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
36
37
|
import { OrcaExecutionError } from '../../../orca-execution.js';
|
|
37
38
|
import * as leadMcpModule from '../lead-mcp.js';
|
|
38
|
-
import {
|
|
39
|
+
import {
|
|
40
|
+
LEAD_MCP_FILE,
|
|
41
|
+
prepareLeadMcp,
|
|
42
|
+
requireLeadProjectTrust,
|
|
43
|
+
resolveMbxMcpServer,
|
|
44
|
+
} from '../lead-mcp.js';
|
|
39
45
|
import {
|
|
40
46
|
LEAD_KICKOFF_PROMPT,
|
|
41
47
|
leadSessionSettings,
|
|
@@ -71,6 +77,11 @@ describe('lead isolation on claude-code (T14363, T14423)', () => {
|
|
|
71
77
|
|
|
72
78
|
beforeEach(() => {
|
|
73
79
|
root = mkdtempSync(join(tmpdir(), 'lead-mcp-'));
|
|
80
|
+
vi.stubEnv('HOME', root);
|
|
81
|
+
vi.stubEnv('CLAUDE_CONFIG_DIR', undefined);
|
|
82
|
+
vi.stubEnv('CLAUDE_CODE_IGNORE_PERSISTED_TRUST', '');
|
|
83
|
+
vi.stubEnv('CLAUDE_CODE_CUSTOM_OAUTH_URL', '');
|
|
84
|
+
vi.stubEnv('CLAUDE_CODE_SANDBOXED', '');
|
|
74
85
|
project = join(root, 'project');
|
|
75
86
|
spawnDir = join(root, 'spawn');
|
|
76
87
|
bin = join(root, 'bin');
|
|
@@ -91,6 +102,313 @@ describe('lead isolation on claude-code (T14363, T14423)', () => {
|
|
|
91
102
|
rmSync(root, { recursive: true, force: true });
|
|
92
103
|
});
|
|
93
104
|
|
|
105
|
+
it.each([
|
|
106
|
+
'interactive',
|
|
107
|
+
'detached',
|
|
108
|
+
] as const)('refuses an untrusted %s lead before preparing MCP or opening an Orca terminal', async (mode) => {
|
|
109
|
+
vi.stubEnv('HOME', root);
|
|
110
|
+
vi.stubEnv('CLAUDE_CONFIG_DIR', undefined);
|
|
111
|
+
const prepare = vi.spyOn(leadMcpModule, 'prepareLeadMcp');
|
|
112
|
+
const launcher = vi.fn<OrcaLeadTerminalLauncher>();
|
|
113
|
+
const started = vi.fn<(pid: number) => void>();
|
|
114
|
+
writeFileSync(
|
|
115
|
+
join(project, '.claude.json'),
|
|
116
|
+
JSON.stringify({ projects: { [project]: { hasTrustDialogAccepted: true } } }),
|
|
117
|
+
);
|
|
118
|
+
for (const config of [
|
|
119
|
+
undefined,
|
|
120
|
+
'{invalid JSON',
|
|
121
|
+
'null',
|
|
122
|
+
JSON.stringify({ projects: { [project]: { hasTrustDialogAccepted: false } } }),
|
|
123
|
+
JSON.stringify({ projects: { [project]: { hasTrustDialogAccepted: 'true' } } }),
|
|
124
|
+
JSON.stringify({ projects: { [`${project}-other`]: { hasTrustDialogAccepted: true } } }),
|
|
125
|
+
]) {
|
|
126
|
+
if (config !== undefined) writeFileSync(claudeJson, config);
|
|
127
|
+
await expect(
|
|
128
|
+
new ClaudeCodeSpawnProvider().spawn({
|
|
129
|
+
...lead(mode),
|
|
130
|
+
workingDirectory: project,
|
|
131
|
+
leadTerminalLauncher: launcher,
|
|
132
|
+
onLeadStarted: started,
|
|
133
|
+
}),
|
|
134
|
+
).rejects.toMatchObject({
|
|
135
|
+
code: 'E_LEAD_PROJECT_UNTRUSTED',
|
|
136
|
+
fix: expect.stringContaining(project),
|
|
137
|
+
});
|
|
138
|
+
if (config !== undefined) expect(readFileSync(claudeJson, 'utf8')).toBe(config);
|
|
139
|
+
else expect(existsSync(claudeJson)).toBe(false);
|
|
140
|
+
}
|
|
141
|
+
expect(prepare).not.toHaveBeenCalled();
|
|
142
|
+
expect(launcher).not.toHaveBeenCalled();
|
|
143
|
+
expect(started).not.toHaveBeenCalled();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('refuses an untrusted native lead with the same one-time trust instructions', async () => {
|
|
147
|
+
vi.stubEnv('HOME', root);
|
|
148
|
+
vi.stubEnv('CLAUDE_CONFIG_DIR', undefined);
|
|
149
|
+
await expect(
|
|
150
|
+
new ClaudeCodeSpawnProvider().spawn({ ...lead(), workingDirectory: project }),
|
|
151
|
+
).rejects.toMatchObject({
|
|
152
|
+
code: 'E_LEAD_PROJECT_UNTRUSTED',
|
|
153
|
+
fix: expect.stringContaining('accept the folder-trust dialog'),
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('recognizes canonical folder trust and trusted ancestors within a repository', () => {
|
|
158
|
+
const child = join(project, 'src');
|
|
159
|
+
mkdirSync(child);
|
|
160
|
+
mkdirSync(join(project, '.git'));
|
|
161
|
+
writeFileSync(
|
|
162
|
+
claudeJson,
|
|
163
|
+
JSON.stringify({ projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } } }),
|
|
164
|
+
);
|
|
165
|
+
expect(requireLeadProjectTrust(child)).toBe(claudeJson);
|
|
166
|
+
if (process.platform !== 'win32') {
|
|
167
|
+
const alias = join(root, 'alias');
|
|
168
|
+
symlinkSync(project, alias);
|
|
169
|
+
expect(requireLeadProjectTrust(alias)).toBe(claudeJson);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
/** Create a real linked worktree with isolated account state and no user hooks. */
|
|
174
|
+
function linkedWorktree(): string {
|
|
175
|
+
const git = (...args: string[]): string =>
|
|
176
|
+
execFileSync('git', ['-C', project, '-c', 'core.hooksPath=', ...args], {
|
|
177
|
+
encoding: 'utf8',
|
|
178
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
179
|
+
});
|
|
180
|
+
git('init', '--quiet', '--template=');
|
|
181
|
+
git(
|
|
182
|
+
'-c',
|
|
183
|
+
'user.name=Trust Test',
|
|
184
|
+
'-c',
|
|
185
|
+
'user.email=trust@example.test',
|
|
186
|
+
'-c',
|
|
187
|
+
'commit.gpgsign=false',
|
|
188
|
+
'commit',
|
|
189
|
+
'--quiet',
|
|
190
|
+
'--allow-empty',
|
|
191
|
+
'-m',
|
|
192
|
+
'fixture',
|
|
193
|
+
);
|
|
194
|
+
const worktree = join(root, 'linked');
|
|
195
|
+
git('worktree', 'add', '--quiet', '--detach', worktree);
|
|
196
|
+
return worktree;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
it('accepts main-checkout trust from a real linked worktree and its subdirectories', () => {
|
|
200
|
+
const worktree = linkedWorktree();
|
|
201
|
+
const child = join(worktree, 'src');
|
|
202
|
+
mkdirSync(child);
|
|
203
|
+
const config = JSON.stringify({
|
|
204
|
+
projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } },
|
|
205
|
+
});
|
|
206
|
+
writeFileSync(claudeJson, config);
|
|
207
|
+
expect(requireLeadProjectTrust(worktree)).toBe(claudeJson);
|
|
208
|
+
expect(requireLeadProjectTrust(child)).toBe(claudeJson);
|
|
209
|
+
expect(readFileSync(claudeJson, 'utf8')).toBe(config);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('refuses missing main-checkout trust and trust outside the linked repository', () => {
|
|
213
|
+
const worktree = linkedWorktree();
|
|
214
|
+
for (const config of [
|
|
215
|
+
{},
|
|
216
|
+
{ [realpathSync(project)]: { hasTrustDialogAccepted: false } },
|
|
217
|
+
{ [realpathSync(root)]: { hasTrustDialogAccepted: true } },
|
|
218
|
+
{ [`${realpathSync(project)}-other`]: { hasTrustDialogAccepted: true } },
|
|
219
|
+
]) {
|
|
220
|
+
writeFileSync(claudeJson, JSON.stringify({ projects: config }));
|
|
221
|
+
expect(() => requireLeadProjectTrust(worktree)).toThrow(OrcaExecutionError);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it.each(['commondir', 'gitdir'])('refuses inconsistent worktree %s metadata', (file) => {
|
|
226
|
+
const worktree = linkedWorktree();
|
|
227
|
+
const gitDir = readFileSync(join(worktree, '.git'), 'utf8').trim().slice(7).trim();
|
|
228
|
+
writeFileSync(join(gitDir, file), file === 'commondir' ? project : join(project, '.git'));
|
|
229
|
+
writeFileSync(
|
|
230
|
+
claudeJson,
|
|
231
|
+
JSON.stringify({ projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } } }),
|
|
232
|
+
);
|
|
233
|
+
expect(() => requireLeadProjectTrust(worktree)).toThrow(OrcaExecutionError);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('does not inherit trust across a repository boundary or from a similarly named folder', () => {
|
|
237
|
+
mkdirSync(join(project, '.git'));
|
|
238
|
+
writeFileSync(
|
|
239
|
+
claudeJson,
|
|
240
|
+
JSON.stringify({ projects: { [realpathSync(root)]: { hasTrustDialogAccepted: true } } }),
|
|
241
|
+
);
|
|
242
|
+
expect(() => requireLeadProjectTrust(project)).toThrow(OrcaExecutionError);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('checks the effective account config, including spawn HOME and CLAUDE_CONFIG_DIR overrides', async () => {
|
|
246
|
+
const account = join(root, 'account');
|
|
247
|
+
mkdirSync(account);
|
|
248
|
+
const accountJson = join(account, '.claude.json');
|
|
249
|
+
writeFileSync(
|
|
250
|
+
accountJson,
|
|
251
|
+
JSON.stringify({ projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } } }),
|
|
252
|
+
);
|
|
253
|
+
expect(requireLeadProjectTrust(project, { HOME: account })).toBe(accountJson);
|
|
254
|
+
const launcher = vi.fn<OrcaLeadTerminalLauncher>().mockResolvedValue({
|
|
255
|
+
runtimeId: 'runtime-fixture',
|
|
256
|
+
terminalHandle: 'term_lead',
|
|
257
|
+
pid: 12345,
|
|
258
|
+
});
|
|
259
|
+
fakeBinary(join(bin, 'claude'), join(root, 'calls.log'));
|
|
260
|
+
vi.stubEnv('PATH', `${bin}:${process.env.PATH}`);
|
|
261
|
+
const prepare = vi.spyOn(leadMcpModule, 'prepareLeadMcp');
|
|
262
|
+
const result = await new ClaudeCodeSpawnProvider().spawn({
|
|
263
|
+
...lead(),
|
|
264
|
+
workingDirectory: project,
|
|
265
|
+
options: { env: { CLAUDE_CONFIG_DIR: account } },
|
|
266
|
+
leadTerminalLauncher: launcher,
|
|
267
|
+
onLeadStarted: vi.fn(),
|
|
268
|
+
});
|
|
269
|
+
expect(result.status).toBe('running');
|
|
270
|
+
expect(prepare).toHaveBeenCalledWith(
|
|
271
|
+
expect.any(String),
|
|
272
|
+
project,
|
|
273
|
+
expect.objectContaining({ claudeJsonPath: accountJson }),
|
|
274
|
+
);
|
|
275
|
+
expect(readFileSync(accountJson, 'utf8')).toContain('"hasTrustDialogAccepted":true');
|
|
276
|
+
expect(existsSync(claudeJson)).toBe(false);
|
|
277
|
+
rmSync(result.leadTempDir ?? '', { recursive: true, force: true });
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('refuses stale legacy trust when Claude uses a migrated config, or ignores saved trust', () => {
|
|
281
|
+
writeFileSync(
|
|
282
|
+
claudeJson,
|
|
283
|
+
JSON.stringify({ projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } } }),
|
|
284
|
+
);
|
|
285
|
+
mkdirSync(join(root, '.claude'));
|
|
286
|
+
const migrated = join(root, '.claude', '.config.json');
|
|
287
|
+
writeFileSync(migrated, '{}');
|
|
288
|
+
expect(() => requireLeadProjectTrust(project)).toThrow(migrated);
|
|
289
|
+
rmSync(migrated);
|
|
290
|
+
vi.stubEnv('CLAUDE_CODE_IGNORE_PERSISTED_TRUST', '1');
|
|
291
|
+
expect(() => requireLeadProjectTrust(project)).toThrow('the lead was not launched');
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('reads the config file Claude 2.1.322 picks: empty .config.json, custom OAuth, sandboxed (T14515)', () => {
|
|
295
|
+
const trusted = JSON.stringify({
|
|
296
|
+
projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } },
|
|
297
|
+
});
|
|
298
|
+
mkdirSync(join(root, '.claude'));
|
|
299
|
+
const migrated = join(root, '.claude', '.config.json');
|
|
300
|
+
// An empty .config.json beside a legacy file: Claude keeps reading .claude.json.
|
|
301
|
+
writeFileSync(claudeJson, trusted);
|
|
302
|
+
writeFileSync(migrated, '');
|
|
303
|
+
expect(requireLeadProjectTrust(project)).toBe(claudeJson);
|
|
304
|
+
// With no legacy file, the empty writable .config.json is Claude's config: untrusted.
|
|
305
|
+
rmSync(claudeJson);
|
|
306
|
+
expect(() => requireLeadProjectTrust(project)).toThrow(migrated);
|
|
307
|
+
// Read-only and empty, it is never Claude's config.
|
|
308
|
+
if (process.platform !== 'win32') {
|
|
309
|
+
chmodSync(migrated, 0o444);
|
|
310
|
+
expect(() => requireLeadProjectTrust(project)).toThrow(claudeJson);
|
|
311
|
+
}
|
|
312
|
+
rmSync(migrated);
|
|
313
|
+
|
|
314
|
+
// CLAUDE_CODE_CUSTOM_OAUTH_URL: Claude reads .claude-custom-oauth.json instead.
|
|
315
|
+
const customJson = join(root, '.claude-custom-oauth.json');
|
|
316
|
+
writeFileSync(claudeJson, trusted);
|
|
317
|
+
const custom = { CLAUDE_CODE_CUSTOM_OAUTH_URL: 'https://oauth.example.test' };
|
|
318
|
+
expect(() => requireLeadProjectTrust(project, { HOME: root, ...custom })).toThrow(customJson);
|
|
319
|
+
writeFileSync(customJson, trusted);
|
|
320
|
+
rmSync(claudeJson);
|
|
321
|
+
expect(requireLeadProjectTrust(project, { HOME: root, ...custom })).toBe(customJson);
|
|
322
|
+
rmSync(customJson);
|
|
323
|
+
|
|
324
|
+
// CLAUDE_CODE_SANDBOXED (Claude's truthy parse): every folder is trusted, no dialog.
|
|
325
|
+
expect(() =>
|
|
326
|
+
requireLeadProjectTrust(project, { HOME: root, CLAUDE_CODE_SANDBOXED: '0' }),
|
|
327
|
+
).toThrow('the lead was not launched');
|
|
328
|
+
for (const value of ['1', ' TRUE ', 'yes', 'on']) {
|
|
329
|
+
expect(requireLeadProjectTrust(project, { HOME: root, CLAUDE_CODE_SANDBOXED: value })).toBe(
|
|
330
|
+
claudeJson,
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
expect(() =>
|
|
334
|
+
requireLeadProjectTrust(project, {
|
|
335
|
+
HOME: root,
|
|
336
|
+
CLAUDE_CODE_SANDBOXED: '1',
|
|
337
|
+
CLAUDE_CODE_IGNORE_PERSISTED_TRUST: '1',
|
|
338
|
+
}),
|
|
339
|
+
).toThrow('the lead was not launched');
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('an empty CLAUDE_CONFIG_DIR reads .config.json from the project but the legacy file from HOME, as Claude does', () => {
|
|
343
|
+
const trusted = JSON.stringify({
|
|
344
|
+
projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } },
|
|
345
|
+
});
|
|
346
|
+
writeFileSync(claudeJson, trusted);
|
|
347
|
+
expect(requireLeadProjectTrust(project, { HOME: root, CLAUDE_CONFIG_DIR: '' })).toBe(
|
|
348
|
+
claudeJson,
|
|
349
|
+
);
|
|
350
|
+
const projectConfig = join(project, '.config.json');
|
|
351
|
+
writeFileSync(projectConfig, '{}');
|
|
352
|
+
expect(() => requireLeadProjectTrust(project, { HOME: root, CLAUDE_CONFIG_DIR: '' })).toThrow(
|
|
353
|
+
projectConfig,
|
|
354
|
+
);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it.skipIf(process.platform === 'win32')(
|
|
358
|
+
'an Orca lead is judged from exactly the environment its terminal receives (T14515)',
|
|
359
|
+
async () => {
|
|
360
|
+
for (const name of ['claude', 'cleo']) fakeBinary(join(bin, name), join(root, 'calls.log'));
|
|
361
|
+
vi.stubEnv('PATH', `${bin}:${process.env.PATH}`);
|
|
362
|
+
const trust = vi.spyOn(leadMcpModule, 'requireLeadProjectTrust');
|
|
363
|
+
const launcher = vi.fn<OrcaLeadTerminalLauncher>().mockResolvedValue({
|
|
364
|
+
runtimeId: 'runtime-fixture',
|
|
365
|
+
terminalHandle: 'term_lead',
|
|
366
|
+
pid: 12345,
|
|
367
|
+
});
|
|
368
|
+
const spawnLead = (env: Record<string, string>) =>
|
|
369
|
+
new ClaudeCodeSpawnProvider().spawn({
|
|
370
|
+
...lead(),
|
|
371
|
+
workingDirectory: project,
|
|
372
|
+
options: { ...lead().options, env: { ...IDENTITY, ...env } },
|
|
373
|
+
leadTerminalLauncher: launcher,
|
|
374
|
+
onLeadStarted: vi.fn(),
|
|
375
|
+
});
|
|
376
|
+
// Only CLEO's environment says sandboxed, or points at a trusted account: never admitted.
|
|
377
|
+
const account = join(root, 'account');
|
|
378
|
+
mkdirSync(account);
|
|
379
|
+
writeFileSync(
|
|
380
|
+
join(account, '.claude.json'),
|
|
381
|
+
JSON.stringify({ projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } } }),
|
|
382
|
+
);
|
|
383
|
+
vi.stubEnv('CLAUDE_CODE_SANDBOXED', '1');
|
|
384
|
+
vi.stubEnv('CLAUDE_CONFIG_DIR', account);
|
|
385
|
+
await expect(spawnLead({})).rejects.toMatchObject({ code: 'E_LEAD_PROJECT_UNTRUSTED' });
|
|
386
|
+
expect(trust.mock.calls.at(-1)?.[1]).toEqual({ HOME: root, ...IDENTITY });
|
|
387
|
+
expect(launcher).not.toHaveBeenCalled();
|
|
388
|
+
// The spawn's own variables reach both the preflight and the terminal.
|
|
389
|
+
for (const env of [{ CLAUDE_CODE_SANDBOXED: '1' }, { CLAUDE_CONFIG_DIR: account }]) {
|
|
390
|
+
const result = await spawnLead(env);
|
|
391
|
+
expect(result.status).toBe('running');
|
|
392
|
+
const launch = launcher.mock.calls.at(-1)?.[0];
|
|
393
|
+
expect(launch?.env).toEqual({ HOME: root, ...IDENTITY, ...env });
|
|
394
|
+
expect(trust.mock.calls.at(-1)?.[1]).toEqual(launch?.env);
|
|
395
|
+
for (const key of Object.keys(env)) expect(launch?.unsetEnv).not.toContain(key);
|
|
396
|
+
expect(launch?.unsetEnv).toContain(
|
|
397
|
+
'CLAUDE_CODE_SANDBOXED' in env ? 'CLAUDE_CONFIG_DIR' : 'CLAUDE_CODE_SANDBOXED',
|
|
398
|
+
);
|
|
399
|
+
rmSync(result.leadTempDir ?? '', { recursive: true, force: true });
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
);
|
|
403
|
+
|
|
404
|
+
it('refuses home-folder trust, which Claude does not persist across sessions', () => {
|
|
405
|
+
writeFileSync(
|
|
406
|
+
claudeJson,
|
|
407
|
+
JSON.stringify({ projects: { [realpathSync(root)]: { hasTrustDialogAccepted: true } } }),
|
|
408
|
+
);
|
|
409
|
+
expect(() => requireLeadProjectTrust(root)).toThrow('the lead was not launched');
|
|
410
|
+
});
|
|
411
|
+
|
|
94
412
|
/** Common allow-list layers of both modes, from `--restricted` to `--setting-sources ""`. */
|
|
95
413
|
function allowLayers(tools: string, allowed: string): string[] {
|
|
96
414
|
return [
|
|
@@ -155,7 +473,13 @@ describe('lead isolation on claude-code (T14363, T14423)', () => {
|
|
|
155
473
|
it.skipIf(process.platform === 'win32').each(['interactive', 'detached'] as const)(
|
|
156
474
|
'hands the production %s guards to Orca and preserves cross-bundle refusals',
|
|
157
475
|
async (mode) => {
|
|
158
|
-
writeFileSync(
|
|
476
|
+
writeFileSync(
|
|
477
|
+
claudeJson,
|
|
478
|
+
JSON.stringify({
|
|
479
|
+
mcpServers: { mbx, shell: SHELL_SERVER },
|
|
480
|
+
projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } },
|
|
481
|
+
}),
|
|
482
|
+
);
|
|
159
483
|
for (const name of ['claude', 'cleo']) fakeBinary(join(bin, name), join(root, 'calls.log'));
|
|
160
484
|
vi.stubEnv('PATH', `${bin}:${process.env.PATH}`);
|
|
161
485
|
const prepare = prepareLeadMcp;
|
|
@@ -167,7 +491,14 @@ describe('lead isolation on claude-code (T14363, T14423)', () => {
|
|
|
167
491
|
const launcher = vi.fn<OrcaLeadTerminalLauncher>(async (launch, onStarted) => {
|
|
168
492
|
const value = (flag: string) => launch.args[launch.args.indexOf(flag) + 1] ?? '';
|
|
169
493
|
expect(launch.command).toBe(join(bin, 'claude'));
|
|
170
|
-
expect(launch.env).toEqual(IDENTITY);
|
|
494
|
+
expect(launch.env).toEqual({ HOME: root, ...IDENTITY });
|
|
495
|
+
expect(launch.unsetEnv).toEqual([
|
|
496
|
+
'USERPROFILE',
|
|
497
|
+
'CLAUDE_CONFIG_DIR',
|
|
498
|
+
'CLAUDE_CODE_CUSTOM_OAUTH_URL',
|
|
499
|
+
'CLAUDE_CODE_SANDBOXED',
|
|
500
|
+
'CLAUDE_CODE_IGNORE_PERSISTED_TRUST',
|
|
501
|
+
]);
|
|
171
502
|
expect(launch.args).toContain('--restricted');
|
|
172
503
|
expect(launch.args).toContain('--strict-mcp-config');
|
|
173
504
|
expect(launch.args).not.toContain('--dangerously-skip-permissions');
|
|
@@ -454,7 +785,13 @@ describe('lead isolation on claude-code (T14363, T14423)', () => {
|
|
|
454
785
|
['not configured', null],
|
|
455
786
|
['refused (a shell)', { command: '/bin/sh', args: ['mcp'] }],
|
|
456
787
|
])('a detached lead with mbx %s does not start: E_LEAD_NO_OWNER_CHANNEL', async (_label, entry) => {
|
|
457
|
-
writeFileSync(
|
|
788
|
+
writeFileSync(
|
|
789
|
+
claudeJson,
|
|
790
|
+
JSON.stringify({
|
|
791
|
+
mcpServers: entry ? { mbx: entry } : {},
|
|
792
|
+
projects: { [realpathSync(project)]: { hasTrustDialogAccepted: true } },
|
|
793
|
+
}),
|
|
794
|
+
);
|
|
458
795
|
vi.stubEnv('HOME', root); // ~/.claude.json is this test's file
|
|
459
796
|
try {
|
|
460
797
|
const result = await new ClaudeCodeSpawnProvider().spawn({
|
|
@@ -19,16 +19,26 @@
|
|
|
19
19
|
* @epic T14351
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
existsSync,
|
|
24
|
+
lstatSync,
|
|
25
|
+
readdirSync,
|
|
26
|
+
readFileSync,
|
|
27
|
+
realpathSync,
|
|
28
|
+
type Stats,
|
|
29
|
+
statSync,
|
|
30
|
+
} from 'node:fs';
|
|
23
31
|
import { writeFile } from 'node:fs/promises';
|
|
24
32
|
import { homedir } from 'node:os';
|
|
25
|
-
import { basename, isAbsolute, join, relative, resolve } from 'node:path';
|
|
33
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
26
34
|
import type { LeadMcpReport } from '@cleocode/contracts';
|
|
27
35
|
import {
|
|
28
36
|
LEAD_MAILBOX_MCP_SERVER,
|
|
29
37
|
LEAD_RUNNER_MCP_SERVER,
|
|
30
38
|
LEAD_RUNNER_TOOL,
|
|
31
39
|
} from '@cleocode/contracts/operations/orchestrate.js';
|
|
40
|
+
import { claudeCodeTrustConfigSchema } from '@cleocode/contracts/provider-paths.js';
|
|
41
|
+
import { OrcaExecutionError } from '../../orca-execution.js';
|
|
32
42
|
|
|
33
43
|
/** One Claude Code MCP server entry, kept verbatim. */
|
|
34
44
|
export type McpServerEntry = Readonly<Record<string, unknown>>;
|
|
@@ -81,6 +91,171 @@ function readJson(file: string): unknown {
|
|
|
81
91
|
}
|
|
82
92
|
}
|
|
83
93
|
|
|
94
|
+
/** Claude's canonical root requires a worktrees entry with a matching backlink. */
|
|
95
|
+
function linkedWorktreeTrustRoot(dir: string): string | null {
|
|
96
|
+
const link = readFileSync(join(dir, '.git'), 'utf8').trim();
|
|
97
|
+
if (!link.startsWith('gitdir:')) return null;
|
|
98
|
+
const gitDir = realpathSync(resolve(dir, link.slice(7).trim()));
|
|
99
|
+
const commonFile = join(gitDir, 'commondir');
|
|
100
|
+
const backFile = join(gitDir, 'gitdir');
|
|
101
|
+
if (![commonFile, backFile].every((file) => lstatSync(file, { throwIfNoEntry: false })?.isFile()))
|
|
102
|
+
return null;
|
|
103
|
+
const commonDir = realpathSync(resolve(gitDir, readFileSync(commonFile, 'utf8').trim()));
|
|
104
|
+
if (
|
|
105
|
+
dirname(gitDir) !== join(commonDir, 'worktrees') ||
|
|
106
|
+
realpathSync(resolve(gitDir, readFileSync(backFile, 'utf8').trim())) !== join(dir, '.git')
|
|
107
|
+
)
|
|
108
|
+
return null;
|
|
109
|
+
if (basename(commonDir) !== '.git' && existsSync(join(commonDir, '.git'))) return null;
|
|
110
|
+
return basename(commonDir) === '.git' ? dirname(commonDir) : commonDir;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Claude Code's truthy env parse: `1`, `true`, `yes` or `on`, any case, trimmed. */
|
|
114
|
+
function envTruthy(value: string | undefined): boolean {
|
|
115
|
+
return ['1', 'true', 'yes', 'on'].includes((value ?? '').trim().toLowerCase());
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The user config file Claude Code 2.1.322 reads (its `getGlobalClaudeFile`):
|
|
120
|
+
* `<configDir>/.config.json` when it is a non-empty file Claude sees under that
|
|
121
|
+
* exact name, or an empty writable file with no usable legacy file beside it;
|
|
122
|
+
* otherwise the legacy `.claude<suffix>.json`, whose suffix is `-custom-oauth`
|
|
123
|
+
* under `CLAUDE_CODE_CUSTOM_OAUTH_URL`.
|
|
124
|
+
*/
|
|
125
|
+
function claudeUserConfigPath(projectDir: string, home: string, env: NodeJS.ProcessEnv): string {
|
|
126
|
+
// Claude reads an empty CLAUDE_CONFIG_DIR as set for `.config.json` (relative
|
|
127
|
+
// to its cwd, the project) but as unset for the legacy file (`??` vs `||`).
|
|
128
|
+
const dirVar = env.CLAUDE_CONFIG_DIR;
|
|
129
|
+
const configDir = dirVar !== undefined ? resolve(projectDir, dirVar) : join(home, '.claude');
|
|
130
|
+
const migrated = join(configDir.normalize('NFC'), '.config.json');
|
|
131
|
+
const suffix = env.CLAUDE_CODE_CUSTOM_OAUTH_URL ? '-custom-oauth' : '';
|
|
132
|
+
const legacy = join(dirVar ? resolve(projectDir, dirVar) : home, `.claude${suffix}.json`);
|
|
133
|
+
const enoent = (err: unknown): boolean =>
|
|
134
|
+
err instanceof Error && 'code' in err && err.code === 'ENOENT';
|
|
135
|
+
try {
|
|
136
|
+
const stat = statSync(migrated);
|
|
137
|
+
// 0o222: any write bit. An empty read-only file is never Claude's config.
|
|
138
|
+
if (!stat.isFile() || (stat.size === 0 && (stat.mode & 0o222) === 0)) return legacy;
|
|
139
|
+
let legacyStat: Stats | undefined;
|
|
140
|
+
try {
|
|
141
|
+
legacyStat = statSync(legacy);
|
|
142
|
+
} catch (err) {
|
|
143
|
+
if (enoent(err) || stat.size === 0) return migrated;
|
|
144
|
+
}
|
|
145
|
+
if (stat.size === 0) return legacy;
|
|
146
|
+
if (legacyStat && (!legacyStat.isFile() || legacyStat.size === 0)) return migrated;
|
|
147
|
+
try {
|
|
148
|
+
// A case-insensitive volume matching `.CONFIG.json` is not the migrated file.
|
|
149
|
+
if (!readdirSync(dirname(migrated)).includes(basename(migrated))) return legacy;
|
|
150
|
+
} catch (err) {
|
|
151
|
+
if (enoent(err)) return legacy;
|
|
152
|
+
}
|
|
153
|
+
return migrated;
|
|
154
|
+
} catch {
|
|
155
|
+
return legacy;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Every variable {@link requireLeadProjectTrust} reads from the lead environment. */
|
|
160
|
+
export const LEAD_TRUST_ENV_KEYS = [
|
|
161
|
+
'HOME',
|
|
162
|
+
'USERPROFILE',
|
|
163
|
+
'CLAUDE_CONFIG_DIR',
|
|
164
|
+
'CLAUDE_CODE_CUSTOM_OAUTH_URL',
|
|
165
|
+
'CLAUDE_CODE_SANDBOXED',
|
|
166
|
+
'CLAUDE_CODE_IGNORE_PERSISTED_TRUST',
|
|
167
|
+
] as const;
|
|
168
|
+
|
|
169
|
+
/** The environment an Orca lead terminal receives, as {@link orcaLeadEnv} pins it. */
|
|
170
|
+
export interface OrcaLeadEnv {
|
|
171
|
+
/** Exported in the terminal, and the environment its trust is judged from. */
|
|
172
|
+
readonly env: Readonly<Record<string, string>>;
|
|
173
|
+
/** Trust variables absent from `env`, unset in the terminal. */
|
|
174
|
+
readonly unset: readonly string[];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Pin the trust-relevant environment of an Orca lead terminal. The terminal
|
|
179
|
+
* inherits Orca's environment, not CLEO's, so the spawn's own variables and
|
|
180
|
+
* CLEO's HOME are exported and every other trust variable is unset: the
|
|
181
|
+
* preflight and the lead's Claude then read the same values, and a variable
|
|
182
|
+
* only CLEO or Orca holds cannot change either.
|
|
183
|
+
*
|
|
184
|
+
* @param spawnEnv - The spawn's own variables (`options.env`).
|
|
185
|
+
* @param base - CLEO's environment, read for HOME only.
|
|
186
|
+
* @returns The variables to export, also passed to the preflight, and those to unset.
|
|
187
|
+
*/
|
|
188
|
+
export function orcaLeadEnv(
|
|
189
|
+
spawnEnv: Readonly<Record<string, string>>,
|
|
190
|
+
base: NodeJS.ProcessEnv = process.env,
|
|
191
|
+
): OrcaLeadEnv {
|
|
192
|
+
const env = { HOME: base.HOME || homedir(), ...spawnEnv };
|
|
193
|
+
return { env, unset: LEAD_TRUST_ENV_KEYS.filter((key) => !(key in env)) };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Require the owner's saved Claude Code folder trust before a lead can launch.
|
|
198
|
+
* Returns the user config path so MCP selection uses the same account state.
|
|
199
|
+
* Missing, malformed or unreadable state refuses; no trust record is written.
|
|
200
|
+
*
|
|
201
|
+
* @remarks
|
|
202
|
+
* Claude Code 2.1.322 stores `projects[path].hasTrustDialogAccepted` in its
|
|
203
|
+
* user config, including the canonical main checkout for linked worktrees.
|
|
204
|
+
* Trust can descend within a repository, never from outside it.
|
|
205
|
+
* A truthy `CLAUDE_CODE_SANDBOXED` makes Claude trust every folder, so it
|
|
206
|
+
* admits too; `CLAUDE_CODE_IGNORE_PERSISTED_TRUST` still refuses.
|
|
207
|
+
* See https://code.claude.com/docs/en/security#additional-safeguards.
|
|
208
|
+
*
|
|
209
|
+
* @param projectDir - The lead's working directory.
|
|
210
|
+
* @param env - Exactly the environment the lead's Claude process receives.
|
|
211
|
+
* @returns The verified Claude Code user config path.
|
|
212
|
+
* @throws OrcaExecutionError when persisted trust cannot be verified.
|
|
213
|
+
*/
|
|
214
|
+
export function requireLeadProjectTrust(
|
|
215
|
+
projectDir: string,
|
|
216
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
217
|
+
): string {
|
|
218
|
+
const home = resolve(projectDir, env.HOME || env.USERPROFILE || homedir());
|
|
219
|
+
const configPath = claudeUserConfigPath(projectDir, home, env);
|
|
220
|
+
let fixPrefix = '';
|
|
221
|
+
try {
|
|
222
|
+
const config = claudeCodeTrustConfigSchema.safeParse(readJson(configPath));
|
|
223
|
+
let dir = realpathSync(projectDir);
|
|
224
|
+
if (env.CLAUDE_CODE_IGNORE_PERSISTED_TRUST) {
|
|
225
|
+
fixPrefix = 'Unset CLAUDE_CODE_IGNORE_PERSISTED_TRUST in the lead environment. ';
|
|
226
|
+
} else if (envTruthy(env.CLAUDE_CODE_SANDBOXED)) {
|
|
227
|
+
// Claude trusts every folder in a sandboxed session and shows no dialog.
|
|
228
|
+
return configPath;
|
|
229
|
+
} else if (dir === realpathSync(home)) {
|
|
230
|
+
fixPrefix = 'Choose a project subdirectory: Claude Code never persists home-folder trust. ';
|
|
231
|
+
} else if (config.success) {
|
|
232
|
+
const trusted = (folder: string): boolean => {
|
|
233
|
+
const key = (
|
|
234
|
+
process.platform === 'win32' ? folder.replaceAll('\\', '/') : folder
|
|
235
|
+
).normalize('NFC');
|
|
236
|
+
return config.data.projects?.[key]?.hasTrustDialogAccepted === true;
|
|
237
|
+
};
|
|
238
|
+
for (;;) {
|
|
239
|
+
if (trusted(dir)) return configPath;
|
|
240
|
+
const gitEntry = lstatSync(join(dir, '.git'), { throwIfNoEntry: false });
|
|
241
|
+
if (gitEntry || dirname(dir) === dir) {
|
|
242
|
+
const mainRoot = gitEntry?.isFile() ? linkedWorktreeTrustRoot(dir) : null;
|
|
243
|
+
if (mainRoot && mainRoot !== realpathSync(home) && trusted(mainRoot)) return configPath;
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
dir = dirname(dir);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
} catch {
|
|
250
|
+
// A failed path/config probe is not evidence that the owner trusted this folder.
|
|
251
|
+
}
|
|
252
|
+
throw new OrcaExecutionError(
|
|
253
|
+
'E_LEAD_PROJECT_UNTRUSTED',
|
|
254
|
+
`Cannot verify saved Claude Code folder trust for ${projectDir} in ${configPath}; the lead was not launched.`,
|
|
255
|
+
`${fixPrefix}Open a terminal in ${JSON.stringify(projectDir)} with the same HOME and CLAUDE_CONFIG_DIR, run claude, accept the folder-trust dialog only if you trust this project, then /exit and retry cleo orchestrate lead. Restore readable valid user config first if needed.`,
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
84
259
|
/** `realpath`, or the resolved path when it cannot be resolved. */
|
|
85
260
|
function realOrResolved(path: string): string {
|
|
86
261
|
try {
|
|
@@ -30,7 +30,13 @@ import { OrcaExecutionError } from '../../orca-execution.js';
|
|
|
30
30
|
import { buildAgentSpawnArgs } from '../shared/agent-spawn-wrapper.js';
|
|
31
31
|
import { leadDeniedTools } from '../shared/lead-tool-strip.js';
|
|
32
32
|
import { removeSpawnPromptFile, writeSpawnPromptFile } from '../shared/prompt-file.js';
|
|
33
|
-
import {
|
|
33
|
+
import {
|
|
34
|
+
type LeadMcpSetup,
|
|
35
|
+
orcaLeadEnv,
|
|
36
|
+
prepareLeadMcp,
|
|
37
|
+
requireLeadProjectTrust,
|
|
38
|
+
runnerOnlyLeadMcp,
|
|
39
|
+
} from './lead-mcp.js';
|
|
34
40
|
import {
|
|
35
41
|
buildLeadArgs,
|
|
36
42
|
leadModeOf,
|
|
@@ -170,6 +176,18 @@ export class ClaudeCodeSpawnProvider implements AdapterSpawnProvider {
|
|
|
170
176
|
* @returns Spawn result with instance ID and status
|
|
171
177
|
*/
|
|
172
178
|
async spawn(context: SpawnContext): Promise<SpawnResult> {
|
|
179
|
+
// T14515: trust is judged from exactly the environment the lead receives.
|
|
180
|
+
// An Orca terminal inherits Orca's environment, so its trust variables are
|
|
181
|
+
// pinned; every other lead path inherits CLEO's with the spawn overrides.
|
|
182
|
+
const isLead = leadDeniedTools(context) !== null;
|
|
183
|
+
const orcaEnv =
|
|
184
|
+
isLead && context.leadTerminalLauncher ? orcaLeadEnv(envStrings(context.options)) : undefined;
|
|
185
|
+
const claudeJsonPath = isLead
|
|
186
|
+
? requireLeadProjectTrust(
|
|
187
|
+
context.workingDirectory ?? process.cwd(),
|
|
188
|
+
orcaEnv?.env ?? { ...process.env, ...envStrings(context.options) },
|
|
189
|
+
)
|
|
190
|
+
: undefined;
|
|
173
191
|
// T14359: a tracked worker keeps the instance id its record was stored under.
|
|
174
192
|
const trackedId = trackedWorkerInstanceId(context.options);
|
|
175
193
|
const instanceId =
|
|
@@ -218,6 +236,7 @@ export class ClaudeCodeSpawnProvider implements AdapterSpawnProvider {
|
|
|
218
236
|
};
|
|
219
237
|
leadMcp = await prepareLeadMcp(dir, context.workingDirectory ?? process.cwd(), {
|
|
220
238
|
identityEnv: envStrings(context.options),
|
|
239
|
+
...(claudeJsonPath !== undefined ? { claudeJsonPath } : {}),
|
|
221
240
|
...(binaries.cleo ? { cleoBin: binaries.cleo } : {}),
|
|
222
241
|
});
|
|
223
242
|
const settingsFile = join(dir, 'lead-settings.json');
|
|
@@ -248,7 +267,7 @@ export class ClaudeCodeSpawnProvider implements AdapterSpawnProvider {
|
|
|
248
267
|
leadMcp: leadMcp.report,
|
|
249
268
|
};
|
|
250
269
|
}
|
|
251
|
-
if (context.leadTerminalLauncher) {
|
|
270
|
+
if (context.leadTerminalLauncher && orcaEnv) {
|
|
252
271
|
const command = resolveAbsoluteBinary('claude');
|
|
253
272
|
if (!command || !context.onLeadStarted) {
|
|
254
273
|
throw new Error(
|
|
@@ -261,7 +280,8 @@ export class ClaudeCodeSpawnProvider implements AdapterSpawnProvider {
|
|
|
261
280
|
args: buildClaudeArgs(context, tmpFile, settingsArgs, leadMcp),
|
|
262
281
|
cwd: context.workingDirectory ?? process.cwd(),
|
|
263
282
|
tempDir: dir,
|
|
264
|
-
env:
|
|
283
|
+
env: orcaEnv.env,
|
|
284
|
+
unsetEnv: orcaEnv.unset,
|
|
265
285
|
},
|
|
266
286
|
context.onLeadStarted,
|
|
267
287
|
);
|