Files
archived-orca/src/shared/claude-background-run-isolation.ts
T
Brennan Benson 36d13f14a5 Background Claude runs no longer start your MCP servers and hooks (#30939)
* fix(claude): keep Orca's background Claude runs off the user's MCP servers and hooks

The model picker and / menu listings, chat naming, and commit message, PR,
branch and Jira text generation each start a short Claude run. Every one but
the / probe loaded the user's MCP servers and hooks, so each run started every
server (one cut off a Notion OAuth refresh and lost the sign-in) and fired
SessionStart hooks.

Every background run now passes --strict-mcp-config and disableAllHooks.
A user's own inline --settings merges into the one settings value Claude
keeps, and their --mcp-config still loads. When Claude refuses strict mode
because an enterprise MCP config is present (which already keeps user
servers out), the run is retried once without the flag.

Tracked in STA-11095 and #30411.

* fix: centralize background Claude generation and retry policy

* fix: rebuild Claude background retries from generated arguments

* test: include branch in generation retry fixture

* fix: preserve user settings arguments in background generation

* test: define retry fixture streams across runtimes
2026-10-20 11:50:07 -07:00

35 lines
1.3 KiB
TypeScript

const ENTERPRISE_REFUSAL =
'You cannot use --strict-mcp-config when an enterprise MCP config is present'
/** Background policy is generated separately from user arguments. */
export function claudeBackgroundArgs(strictMcp = true, userArgs: readonly string[] = []): string[] {
// A literal option value also preserves existing hooks; never interpret user argument grammar.
const hasSettings = userArgs.some((arg) => arg === '--settings' || arg.startsWith('--settings='))
return [
...(strictMcp ? ['--strict-mcp-config'] : []),
...(hasSettings ? [] : ['--settings', '{"disableAllHooks":true}'])
]
}
export type ClaudeBackgroundFailedExit = { exitCode: number | null; stdout: string; stderr: string }
/** Rebuild once without generated strict mode, only after Claude's completed enterprise-policy refusal. */
export async function runClaudeBackgroundAttempts<T>(input: {
agentId: string
run: (strictMcp: boolean) => Promise<T>
failedExit: (result: T) => ClaudeBackgroundFailedExit | undefined
}): Promise<T> {
const result = await input.run(true)
const failure = input.failedExit(result)
if (
input.agentId !== 'claude' ||
!failure ||
typeof failure.exitCode !== 'number' ||
failure.exitCode === 0 ||
!`${failure.stdout}\n${failure.stderr}`.includes(ENTERPRISE_REFUSAL)
) {
return result
}
return input.run(false)
}