Skip to content

Commit 92de8b0

Browse files
author
lijiuyang.5137
committed
Fix remote control auth environment
1 parent a21e34e commit 92de8b0

3 files changed

Lines changed: 63 additions & 4 deletions

File tree

src/runner.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ fn spawn_tty_process(
223223
args: args.to_vec(),
224224
cwd: cwd.to_path_buf(),
225225
env: env.clone(),
226-
unset_env: interactive_claude_unset_env(),
226+
unset_env: interactive_claude_unset_env(args),
227227
})
228228
}
229229

@@ -234,15 +234,42 @@ fn interactive_claude_env() -> HashMap<String, String> {
234234
])
235235
}
236236

237-
fn interactive_claude_unset_env() -> Vec<String> {
238-
[
237+
fn interactive_claude_unset_env(args: &[String]) -> Vec<String> {
238+
let mut keys = [
239239
"CLAUDE_CODE_ENTRYPOINT",
240240
"CLAUDE_AGENT_SDK_VERSION",
241241
"NO_COLOR",
242242
]
243243
.into_iter()
244244
.map(ToOwned::to_owned)
245-
.collect()
245+
.collect::<Vec<_>>();
246+
if enables_remote_control(args) {
247+
if std::env::var_os("CLAUDE_CODE_OAUTH_TOKEN").is_some() {
248+
logging::event(
249+
"env_unset reason=remote_control_full_scope name=CLAUDE_CODE_OAUTH_TOKEN",
250+
);
251+
}
252+
keys.push("CLAUDE_CODE_OAUTH_TOKEN".to_owned());
253+
}
254+
keys
255+
}
256+
257+
fn enables_remote_control(args: &[String]) -> bool {
258+
let mut enabled = false;
259+
for arg in args {
260+
if arg == "--no-chrome" {
261+
enabled = false;
262+
} else if matches!(
263+
arg.as_str(),
264+
"--chrome" | "--remote-control" | "--remote" | "--rc"
265+
) || arg.starts_with("--remote-control=")
266+
|| arg.starts_with("--remote=")
267+
|| arg.starts_with("--rc=")
268+
{
269+
enabled = true;
270+
}
271+
}
272+
enabled
246273
}
247274

248275
fn is_bad_resume_startup_error(error: &CcttyError) -> bool {

tests/cctty_cli.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ fn interactive_claude_gets_terminal_env_not_sdk_transport_env() {
159159
.env("CLAUDE_CODE_ENTRYPOINT", "sdk-py")
160160
.env("CLAUDE_AGENT_SDK_VERSION", "0.0.0")
161161
.env("CLAUDE_AGENT_SDK_SKIP_VERSION_CHECK", "1")
162+
.env("CLAUDE_CODE_OAUTH_TOKEN", "synthetic-setup-token")
162163
.current_dir(workspace.path())
163164
.args(["--print", "--output-format", "stream-json", "Say OK"])
164165
.assert()
@@ -171,6 +172,36 @@ fn interactive_claude_gets_terminal_env_not_sdk_transport_env() {
171172
assert_eq!(env["NO_COLOR"], Value::Null);
172173
assert_eq!(env["CLAUDE_CODE_ENTRYPOINT"], Value::Null);
173174
assert_eq!(env["CLAUDE_AGENT_SDK_VERSION"], Value::Null);
175+
assert_eq!(env["CLAUDE_CODE_OAUTH_TOKEN"], "synthetic-setup-token");
176+
}
177+
178+
#[test]
179+
fn remote_control_uses_login_auth_instead_of_inference_only_oauth_env() {
180+
let fixture = FakeClaude::new();
181+
let workspace = tempfile::tempdir().unwrap();
182+
let config_dir = tempfile::tempdir().unwrap();
183+
let env_path = tempfile::NamedTempFile::new().unwrap();
184+
185+
Command::cargo_bin("cctty")
186+
.unwrap()
187+
.env("CCTTY_CLAUDE_PATH", fixture.path())
188+
.env("CLAUDE_CONFIG_DIR", config_dir.path())
189+
.env("FAKE_CLAUDE_ENV_PATH", env_path.path())
190+
.env("CLAUDE_CODE_OAUTH_TOKEN", "synthetic-setup-token")
191+
.current_dir(workspace.path())
192+
.args([
193+
"--print",
194+
"--output-format",
195+
"stream-json",
196+
"--chrome",
197+
"Say OK",
198+
])
199+
.assert()
200+
.success();
201+
202+
let env: Value =
203+
serde_json::from_str(&std::fs::read_to_string(env_path.path()).unwrap()).unwrap();
204+
assert_eq!(env["CLAUDE_CODE_OAUTH_TOKEN"], Value::Null);
174205
}
175206

176207
#[test]

tests/support/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ if env_path:
5454
"CLAUDE_CODE_ENTRYPOINT",
5555
"CLAUDE_AGENT_SDK_VERSION",
5656
"CLAUDE_AGENT_SDK_SKIP_VERSION_CHECK",
57+
"CLAUDE_CODE_OAUTH_TOKEN",
5758
]
5859
Path(env_path).write_text(json.dumps({key: os.environ.get(key) for key in keys}), encoding="utf-8")
5960

0 commit comments

Comments
 (0)