Skip to content

Commit fd63914

Browse files
fix(server): show why Claude refuses full access as root (pingdotgg#23804)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 18ac394 commit fd63914

3 files changed

Lines changed: 107 additions & 4 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ import * as Context from "effect/Context";
123123
import * as SecureRandom from "effect/crypto/SecureRandom";
124124
import * as DateTime from "effect/DateTime";
125125
import * as Deferred from "effect/Deferred";
126+
import * as Duration from "effect/Duration";
126127
import * as Effect from "effect/Effect";
127128
import * as Equal from "effect/Equal";
128129
import * as Exit from "effect/Exit";
@@ -338,6 +339,13 @@ const WORKFLOW_MEMBER_TRANSCRIPT_FLUSH_MS = 500;
338339
const BACKGROUND_MOVE_TIMEOUT_MS = 5_000;
339340
// How long a Stop waits for the CLI to exit before the user is offered Force stop.
340341
const CLAUDE_STOP_CONFIRM_TIMEOUT_MS = 10_000;
342+
// Matches the stderr tail the SDK keeps for a CLI it spawns itself.
343+
const CLAUDE_STDERR_TAIL_LENGTH = 2_048;
344+
// The process group is killed on exit, so its stderr closes promptly.
345+
const CLAUDE_STDERR_DRAIN_TIMEOUT = Duration.seconds(1);
346+
// Claude Code's refusal of bypassPermissions as root, which it explains only on stderr.
347+
const CLAUDE_ROOT_REFUSAL =
348+
"--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons";
341349

342350
const CLAUDE_GROUP_WATCH_SCRIPT = [
343351
"{ trap '' TERM; read -r _ <&3; kill -TERM 0; sleep 5; kill -KILL 0; } </dev/null >/dev/null 2>&1 &",
@@ -352,6 +360,8 @@ const CLAUDE_GROUP_WATCH_SCRIPT = [
352360
export function spawnClaudeProcessGroup(
353361
options: ClaudeSpawnOptions,
354362
onExit?: () => void,
363+
onStderr: (text: string) => void = () => {},
364+
onStderrClose: () => void = () => {},
355365
): ClaudeSpawnedProcess {
356366
const child = NodeChildProcess.spawn(
357367
"/bin/sh",
@@ -360,10 +370,12 @@ export function spawnClaudeProcessGroup(
360370
cwd: options.cwd,
361371
env: options.env,
362372
signal: options.signal,
363-
stdio: ["pipe", "pipe", "ignore", "pipe"],
373+
stdio: ["pipe", "pipe", "pipe", "pipe"],
364374
detached: true,
365375
},
366376
);
377+
// Always drained, so a chatty CLI never blocks on a full stderr pipe.
378+
child.stderr?.setEncoding("utf8").on("data", onStderr).once("close", onStderrClose);
367379
child.once("exit", () => {
368380
try {
369381
signalProcessGroup(child.pid ?? 0, "SIGKILL");
@@ -558,6 +570,17 @@ export class ClaudeAgentSdkQueryRunnerError extends Schema.TaggedError<ClaudeAge
558570
}
559571
}
560572

573+
export class ClaudeCodeRootRefusalError extends Schema.TaggedError<ClaudeCodeRootRefusalError>()(
574+
"ClaudeCodeRootRefusalError",
575+
{
576+
cause: Schema.Defect(),
577+
},
578+
) {
579+
override get message(): string {
580+
return `Claude Code exited before it started: ${CLAUDE_ROOT_REFUSAL}`;
581+
}
582+
}
583+
561584
class ClaudeMcpRegistrationTimeoutError extends Schema.TaggedError<ClaudeMcpRegistrationTimeoutError>()(
562585
"ClaudeMcpRegistrationTimeoutError",
563586
{},
@@ -1009,10 +1032,25 @@ export const layerQueryRunner: Layer.Layer<
10091032
const unit = launch?.unit;
10101033
const stopScope =
10111034
unit === undefined ? undefined : () => void runFork(agentScope.stop(unit));
1035+
// Only a CLI that fails before its first message explains itself on
1036+
// stderr. The known root refusal becomes a curated failure; any other
1037+
// stderr can hold private text, so it stays out of turn failures.
1038+
let stderrTail = "";
1039+
let stderrClosed: Promise<void> = Promise.resolve();
1040+
let answered = false;
1041+
const appendStderr = (text: string) => {
1042+
if (answered) return;
1043+
stderrTail = (stderrTail + text).slice(-CLAUDE_STDERR_TAIL_LENGTH);
1044+
};
10121045
const spawnProcess =
10131046
options.spawnClaudeCodeProcess ??
10141047
((spawnOptions: ClaudeSpawnOptions) => {
1015-
if (platform !== "win32") return spawnClaudeProcessGroup(spawnOptions, stopScope);
1048+
if (platform !== "win32") {
1049+
// Node can report "exit" before it has read the stderr the CLI wrote.
1050+
let stderrDone = () => {};
1051+
stderrClosed = new Promise((resolve) => (stderrDone = resolve));
1052+
return spawnClaudeProcessGroup(spawnOptions, stopScope, appendStderr, stderrDone);
1053+
}
10161054
const child = NodeChildProcess.spawn(spawnOptions.command, spawnOptions.args, {
10171055
cwd: spawnOptions.cwd,
10181056
env: spawnOptions.env,
@@ -1132,9 +1170,31 @@ export const layerQueryRunner: Layer.Layer<
11321170
);
11331171

11341172
return {
1135-
messages: Stream.fromAsyncIterable(claudeQueryMessages(queryRuntime), (cause) =>
1136-
queryRunnerError(cause, "fromAsyncIterable"),
1173+
messages: Stream.fromAsyncIterable(
1174+
claudeQueryMessages(queryRuntime),
1175+
(cause) => cause,
11371176
).pipe(
1177+
Stream.catch((cause) =>
1178+
Stream.fromEffect(
1179+
Effect.gen(function* () {
1180+
if (!answered) {
1181+
yield* Effect.promise(() => stderrClosed).pipe(
1182+
Effect.timeoutOption(CLAUDE_STDERR_DRAIN_TIMEOUT),
1183+
);
1184+
}
1185+
const refusedRoot = !answered && stderrTail.includes(CLAUDE_ROOT_REFUSAL);
1186+
return yield* queryRunnerError(
1187+
refusedRoot ? new ClaudeCodeRootRefusalError({ cause }) : cause,
1188+
"fromAsyncIterable",
1189+
);
1190+
}),
1191+
),
1192+
),
1193+
Stream.tap(() =>
1194+
Effect.sync(() => {
1195+
answered = true;
1196+
}),
1197+
),
11381198
Stream.tap((message) =>
11391199
logProtocolEvent({
11401200
direction: "incoming",

‎apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as Fiber from "effect/Fiber";
1717
import * as FileSystem from "effect/FileSystem";
1818
import * as Layer from "effect/Layer";
1919
import * as Path from "effect/Path";
20+
import * as Stream from "effect/Stream";
2021
import * as TestClock from "effect/testing/TestClock";
2122
import { vi } from "vite-plus/test";
2223

@@ -546,6 +547,47 @@ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("Claude CLI pro
546547
}),
547548
);
548549

550+
const rootRefusal =
551+
"--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons";
552+
553+
const startupFailureMessage = (stderr: string) =>
554+
Effect.gen(function* () {
555+
const cli = fakeClaudeCli(`printf '%s\\n' '${stderr}' >&2\nexit 1`);
556+
yield* Effect.addFinalizer(() =>
557+
Effect.sync(() => NodeFS.rmSync(NodePath.dirname(cli), { recursive: true, force: true })),
558+
);
559+
const session = yield* openFakeCli(cli);
560+
const exit = yield* Stream.runDrain(session.messages).pipe(Effect.exit);
561+
assert.isTrue(Exit.isFailure(exit));
562+
return Exit.isFailure(exit)
563+
? makeProviderFailure({ cause: exit.cause, class: "transport_error" }).message
564+
: "";
565+
});
566+
567+
// Claude Code refuses bypassPermissions as root and explains why only on
568+
// stderr, so the turn failure has to carry that line.
569+
it.effect("reports the CLI's root refusal when it exits before answering", () =>
570+
Effect.gen(function* () {
571+
assert.include(yield* startupFailureMessage(rootRefusal), rootRefusal);
572+
}).pipe(Effect.provide(realQueryRunner)),
573+
);
574+
575+
// Startup stderr can carry paths, tokens, or settings, so only the known
576+
// refusal reaches the failure the client shows.
577+
it.effect("keeps other startup stderr out of the turn failure", () =>
578+
Effect.gen(function* () {
579+
const surrounded = yield* startupFailureMessage(
580+
`token=sk-private-123\n${rootRefusal}\nat /home/private/settings.json`,
581+
);
582+
assert.include(surrounded, rootRefusal);
583+
assert.notInclude(surrounded, "sk-private-123");
584+
assert.notInclude(surrounded, "/home/private");
585+
586+
const unknown = yield* startupFailureMessage("token=sk-private-123");
587+
assert.notInclude(unknown, "sk-private-123");
588+
}).pipe(Effect.provide(realQueryRunner)),
589+
);
590+
549591
// The CLI keeps running its turn after its pipes close, so a server that
550592
// crashed would leave it working next to the session it resumes.
551593
it.effect("stops the CLI when the server that started it goes away", () =>

‎packages/provider-core/src/server/failure.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function curatedCauseMessages(cause: unknown): {
111111
case "ClaudeAppendSystemPromptMissingValueError":
112112
case "ClaudeSettingsLaunchArgError":
113113
case "ClaudeMcpRegistrationTimeoutError":
114+
case "ClaudeCodeRootRefusalError":
114115
case "CursorModelUnavailableError":
115116
case "CodexGoalObjectiveTooLongError":
116117
case "ProviderSetupError":

0 commit comments

Comments
 (0)