diff --git a/docs/agent-tooling.md b/docs/agent-tooling.md index 39a6d5cf926..0328f7c8658 100644 --- a/docs/agent-tooling.md +++ b/docs/agent-tooling.md @@ -54,6 +54,11 @@ re-read those at every launch (`buildDefaultAccountResolver` in agent-setup), so existing terminals the next time the agent starts; a value the user exported by hand always wins. Paradev never touches the credential stores; the provider CLIs own every login end to end. +The Claude wrapper passes `$SUPERSET_HOME_DIR/hooks/claude-settings.json` with `--settings` +in Paradev terminals, so its managed hooks also load for a `CLAUDE_CONFIG_DIR` exported by +hand. This file contains only hooks; it does not copy another account's model, environment, +or instructions. An explicit `--settings` argument is forwarded unchanged and takes priority. + Because those CLIs read *everything* from their active config dir, a second dir would otherwise mean a second, empty setup. `packages/agent-setup/src/provider-profiles.ts` provisions each non-default profile from the default account (`~/.claude`, `~/.codex`): diff --git a/packages/agent-setup/src/agent-wrappers-claude-codex-opencode.ts b/packages/agent-setup/src/agent-wrappers-claude-codex-opencode.ts index 2aef8a9d901..412158f845b 100644 --- a/packages/agent-setup/src/agent-wrappers-claude-codex-opencode.ts +++ b/packages/agent-setup/src/agent-wrappers-claude-codex-opencode.ts @@ -4,6 +4,7 @@ import { buildDefaultAccountResolver, buildWrapperScript, createWrapper, + DYNAMIC_NOTIFY_PATH_MARKER, getManagedArtifactGuardHookCommand, getManagedNotifyHookCommand, isManagedArtifactGuardCommand, @@ -23,6 +24,7 @@ import { } from "./managed-json-hooks"; import { getNotifyScriptPath } from "./notify-hook"; import { + getHooksDir, getOpenCodeConfigDir, getOpenCodePluginDir, getOpenCodeTuiPluginDir, @@ -82,6 +84,12 @@ export function getClaudeGlobalSettingsJsonPath(): string { return path.join(resolveUserHomeDir(), ".claude", "settings.json"); } +const CLAUDE_MANAGED_SETTINGS_NAME = "claude-settings.json"; + +function getClaudeManagedSettingsJsonPath(): string { + return path.join(getHooksDir(), CLAUDE_MANAGED_SETTINGS_NAME); +} + // StopFailure is the API-error hook; it fires while the session stays alive, // unlike Stop. SubagentStart/SubagentStop feed the per-terminal subagent // roster; the notify script routes them by their agent_id, never as the @@ -162,6 +170,11 @@ export function createClaudeSettingsJson(): void { * not exist — teardown must never create config files. */ export function removeClaudeManagedHooks(): void { + removeOwnedFileIfMarked( + getClaudeManagedSettingsJsonPath(), + DYNAMIC_NOTIFY_PATH_MARKER, + "Claude wrapper hook settings", + ); removeManagedJsonHooks(claudeHooksSpec(getNotifyScriptPath())); } @@ -310,11 +323,23 @@ export function getOpenCodeTuiPluginContent(notifyPath: string): string { /** * Wrapper for Claude: forwards SUPERSET_* env vars into the agent process - * tree and follows the Usage-tab account switch at launch time. Hooks live - * in ~/.claude/settings.json (createClaudeSettingsJson). + * tree and follows the Usage-tab account switch at launch time. A hooks-only + * settings file also covers config dirs exported outside the Usage tab. */ export function createClaudeWrapper(): void { - const script = buildWrapperScript("claude", `exec "$REAL_BIN" "$@"`, { + const settingsPath = getClaudeManagedSettingsJsonPath(); + fs.mkdirSync(path.dirname(settingsPath), { recursive: true }); + writeFileIfChanged( + settingsPath, + `${JSON.stringify({ hooks: claudeHooksSpec(getNotifyScriptPath()).desiredEntriesByEvent }, null, 2)}\n`, + 0o644, + ); + const execLine = `if [ -n "$SUPERSET_TERMINAL_ID" ] \\ + && [ -n "$SUPERSET_HOME_DIR" ] && [ -r "$SUPERSET_HOME_DIR/hooks/${CLAUDE_MANAGED_SETTINGS_NAME}" ]; then + set -- --settings "$SUPERSET_HOME_DIR/hooks/${CLAUDE_MANAGED_SETTINGS_NAME}" "$@" +fi +exec "$REAL_BIN" "$@"`; + const script = buildWrapperScript("claude", execLine, { agentId: "claude", beforeLaunch: buildDefaultAccountResolver( "CLAUDE_CONFIG_DIR", diff --git a/packages/agent-setup/src/agent-wrappers.test.ts b/packages/agent-setup/src/agent-wrappers.test.ts index 8c3c2ed4507..73be82f0751 100644 --- a/packages/agent-setup/src/agent-wrappers.test.ts +++ b/packages/agent-setup/src/agent-wrappers.test.ts @@ -70,6 +70,7 @@ const { buildCopilotWrapperExecLine, buildWrapperScript, createClaudeSettingsJson, + createClaudeWrapper, createCodexHooksJson, createCodexWrapper, COPILOT_HOOK_MARKER, @@ -1973,8 +1974,16 @@ describe("agent-wrappers claude settings.json", () => { hooks: [{ type: "command", command: "/opt/my-pretooluse.sh" }], }); writeFileSync(claudeSettingsPath, JSON.stringify(withUserHook, null, 2)); + mkdirSync(TEST_BIN_DIR, { recursive: true }); + createClaudeWrapper(); + const wrapperSettingsPath = path.join( + TEST_HOOKS_DIR, + "claude-settings.json", + ); + expect(existsSync(wrapperSettingsPath)).toBe(true); removeClaudeManagedHooks(); + expect(existsSync(wrapperSettingsPath)).toBe(false); const parsed = JSON.parse(readFileSync(claudeSettingsPath, "utf-8")) as { hooks?: Record }>>; diff --git a/packages/agent-setup/src/claude-wrapper.test.ts b/packages/agent-setup/src/claude-wrapper.test.ts new file mode 100644 index 00000000000..0909849e4cf --- /dev/null +++ b/packages/agent-setup/src/claude-wrapper.test.ts @@ -0,0 +1,131 @@ +import { afterEach, expect, test } from "bun:test"; +import { + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { getManagedNotifyHookCommand } from "./agent-wrappers-common"; + +const roots: string[] = []; +afterEach(() => { + for (const root of roots.splice(0)) + rmSync(root, { recursive: true, force: true }); +}); + +function setup() { + const root = mkdtempSync(path.join(tmpdir(), "claude-wrapper-")); + roots.push(root); + const appHome = path.join(root, "paradev home"); + const bin = path.join(root, "real-bin"); + const profile = path.join(root, "alternate profile"); + for (const dir of [ + bin, + profile, + path.join(appHome, "bin"), + path.join(appHome, "hooks"), + ]) { + mkdirSync(dir, { recursive: true }); + } + const settings = JSON.stringify({ + model: "alternate-model", + env: { REGION: "alternate" }, + }); + writeFileSync(path.join(profile, "settings.json"), settings); + writeFileSync( + path.join(bin, "claude"), + '#!/bin/bash\nprintf "%s\\0" "$CLAUDE_CONFIG_DIR" "$@"\n', + { mode: 0o755 }, + ); + const created = Bun.spawnSync( + [ + process.execPath, + "-e", + `import { createClaudeWrapper } from ${JSON.stringify(path.join(import.meta.dir, "agent-wrappers-claude-codex-opencode.ts"))}; createClaudeWrapper();`, + ], + { + env: { ...process.env, SUPERSET_HOME_DIR: appHome }, + }, + ); + expect(created.exitCode).toBe(0); + const run = (args: string[], terminalId = "terminal-test") => { + const result = Bun.spawnSync( + ["/bin/bash", path.join(appHome, "bin", "claude"), ...args], + { + env: { + ...process.env, + PATH: `${bin}:/usr/bin:/bin`, + BASH_ENV: "", + SUPERSET_HOME_DIR: appHome, + SUPERSET_TERMINAL_ID: terminalId, + SUPERSET_AGENT_ID: "", + CLAUDE_CONFIG_DIR: profile, + }, + }, + ); + expect(result.exitCode).toBe(0); + const [configDir, ...forwarded] = result.stdout + .toString() + .split("\0") + .slice(0, -1); + expect(configDir).toBe(profile); + expect(readFileSync(path.join(profile, "settings.json"), "utf8")).toBe( + settings, + ); + return forwarded; + }; + return { run, appHome, profile }; +} + +test("loads only managed hooks when Claude uses an exported config directory", () => { + const { run } = setup(); + const args = run(["-p", "Reply with PONG"]); + expect(args[0]).toBe("--settings"); + expect(args.slice(2)).toEqual(["-p", "Reply with PONG"]); + const settings = JSON.parse(readFileSync(args[1] ?? "", "utf8")); + expect(Object.keys(settings)).toEqual(["hooks"]); + expect(settings.hooks.Stop).toEqual([ + { + hooks: [ + { type: "command", command: getManagedNotifyHookCommand("claude") }, + ], + }, + ]); +}); + +test.each([ + false, + true, +])("preserves explicit settings (equals form: %s)", (equals) => { + const { run, profile } = setup(); + const settings = path.join(profile, "caller settings.json"); + const args = [ + ...(equals ? [`--settings=${settings}`] : ["--settings", settings]), + "-p", + "Reply with PONG", + ]; + const forwarded = run(args); + expect(forwarded[0]).toBe("--settings"); + expect(forwarded.slice(2)).toEqual(args); +}); + +test("preserves prompt text after --", () => { + const { run } = setup(); + const args = run(["--", "--settings"]); + expect(args[0]).toBe("--settings"); + expect(args.slice(2)).toEqual(["--", "--settings"]); +}); + +test("leaves launches outside Paradev unchanged", () => { + const { run } = setup(); + expect(run(["-p", "Reply with PONG"], "")).toEqual(["-p", "Reply with PONG"]); +}); + +test("keeps Claude runnable when the generated hook settings are absent", () => { + const { run, appHome } = setup(); + rmSync(path.join(appHome, "hooks", "claude-settings.json")); + expect(run(["-p", "Reply with PONG"])).toEqual(["-p", "Reply with PONG"]); +});