把 AGC 的 cc 执行器与本机 Claude Code 配置彻底隔离
Project CI / AI game creator shell Rust crates (push) Successful in 1m43s
Project CI / AI game creator shell Rust lane 2/2 (push) Successful in 4m14s
Project CI / Backend tests (push) Successful in 4m38s
Project CI / AI game creator shell Rust lane 1/2 (push) Successful in 5m23s
Project CI / Frontend tests (push) Successful in 2m8s
Project CI / Native shell tests (push) Successful in 6m11s
Project CI / Repository checks (push) Successful in 3m15s
Project CI / AI game creator shell web tests (push) Successful in 2m34s
Project CI / AI game creator shell Rust crates (push) Successful in 1m43s
Project CI / AI game creator shell Rust lane 2/2 (push) Successful in 4m14s
Project CI / Backend tests (push) Successful in 4m38s
Project CI / AI game creator shell Rust lane 1/2 (push) Successful in 5m23s
Project CI / Frontend tests (push) Successful in 2m8s
Project CI / Native shell tests (push) Successful in 6m11s
Project CI / Repository checks (push) Successful in 3m15s
Project CI / AI game creator shell web tests (push) Successful in 2m34s
- sidecar 子进程把 HOME、USERPROFILE、CLAUDE_CONFIG_DIR 全部指向项目内隔离目录,不再读用户自己的 ~/.claude.json(个人 MCP、插件市场、凭据),也不再往真实 profile 写 projects / sessions - requestTimeoutMs 在 cc 执行器里改为静默预算:sidecar 每有一条事件就重置,连续静默超预算才按 sidecar-turn-timeout 收口 - 新增 CLAUDE_CODE_TURN_MAX_DURATION(45 分钟)兜底事件流假活,正常情况更早到的是 DirectProject 的 maxTurnSeconds - claude_route 诊断行改成 source / host / auth,避开日志脱敏标记(原 credential=bearer 会被整行替换) - 新增 sidecar 环境隔离单测:USERPROFILE / CLAUDE_CONFIG_DIR 指向隔离目录,且不下发本机 ANTHROPIC_* - pitfalls.md 记录配置隔离、静默超时语义与脱敏标记的坑
This commit is contained in:
@@ -29,6 +29,9 @@ const CLAUDE_CODE_STDERR_DRAIN_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
/// 不留痕的话用户与离线诊断只能看到一个卡住的回合(真实案例:ECONNRESET 重试,
|
||||
/// 每轮 ~60 秒,共 10 轮)。
|
||||
const CLAUDE_CODE_EVENT_IDLE_LOG_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
/// cc 回合的绝对上限。`requestTimeoutMs` 只算静默预算,事件流假活(一直有心跳但永远
|
||||
/// 不出终态)靠这个兜底;正常情况下先到的是 DirectProject 的 `maxTurnSeconds`。
|
||||
const CLAUDE_CODE_TURN_MAX_DURATION: Duration = Duration::from_secs(45 * 60);
|
||||
|
||||
struct ActiveClaudeCodeTurn {
|
||||
client_turn_id: String,
|
||||
@@ -163,6 +166,18 @@ struct SidecarTurnResult {
|
||||
result: serde_json::Value,
|
||||
}
|
||||
|
||||
/// sidecar 回合的失败分类:静默超时必须先杀进程树再收口,普通失败直接透传。
|
||||
enum SidecarTurnFailure {
|
||||
Silent { silent_ms: u64, events: usize },
|
||||
Message(String),
|
||||
}
|
||||
|
||||
impl SidecarTurnFailure {
|
||||
fn message(message: String) -> Self {
|
||||
Self::Message(message)
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_sidecar_turn(
|
||||
root: &Path,
|
||||
llm: Option<&GameCreatorLlmConfig>,
|
||||
@@ -224,35 +239,58 @@ async fn run_sidecar_turn(
|
||||
.take()
|
||||
.ok_or_else(|| "Claude Agent SDK sidecar stderr 未建立".to_string())?;
|
||||
let stderr_task = tokio::spawn(read_capped(stderr, CLAUDE_CODE_OUTPUT_MAX_BYTES));
|
||||
let result = match tokio::time::timeout(Duration::from_millis(timeout_ms.max(1)), async {
|
||||
// cc 回合经常是一串模型调用 + MCP 工具调用,网关慢的时候单次响应就要几十秒;
|
||||
// `requestTimeoutMs` 只当**静默预算**用:只要 sidecar 还在出事件就不算超时,
|
||||
// 连续静默超过预算才按超时收口。整体上限由 `maxTurnSeconds`(DirectProject 硬上限)
|
||||
// 兜底,这里再加一个更宽松的绝对上限防止事件流假活。
|
||||
let silence_budget_ms = timeout_ms.max(1);
|
||||
let mut timeout_reason: Option<String> = None;
|
||||
let turn = async {
|
||||
let mut lines = BufReader::new(stdout).lines();
|
||||
let mut events = Vec::new();
|
||||
let mut terminal = None;
|
||||
let mut idle_rounds = 0_u64;
|
||||
let mut silent_ms = 0_u64;
|
||||
loop {
|
||||
let line =
|
||||
match tokio::time::timeout(CLAUDE_CODE_EVENT_IDLE_LOG_TIMEOUT, lines.next_line())
|
||||
.await
|
||||
{
|
||||
Ok(Ok(Some(line))) => line,
|
||||
Ok(Ok(Some(line))) => {
|
||||
silent_ms = 0;
|
||||
line
|
||||
}
|
||||
Ok(Ok(None)) => break,
|
||||
Ok(Err(error)) => {
|
||||
return Err(format!("读取 Claude Agent SDK sidecar 输出失败:{error}"));
|
||||
return Err(SidecarTurnFailure::message(format!(
|
||||
"读取 Claude Agent SDK sidecar 输出失败:{error}"
|
||||
)));
|
||||
}
|
||||
Err(_) => {
|
||||
idle_rounds += 1;
|
||||
silent_ms += CLAUDE_CODE_EVENT_IDLE_LOG_TIMEOUT.as_millis() as u64;
|
||||
app_log!(
|
||||
"agent.direct_codex.claude_sidecar_idle idleSeconds={}",
|
||||
CLAUDE_CODE_EVENT_IDLE_LOG_TIMEOUT.as_secs() * idle_rounds
|
||||
"agent.direct_codex.claude_sidecar_idle idleSeconds={} events={}",
|
||||
CLAUDE_CODE_EVENT_IDLE_LOG_TIMEOUT.as_secs() * idle_rounds,
|
||||
events.len()
|
||||
);
|
||||
if silent_ms >= silence_budget_ms {
|
||||
return Err(SidecarTurnFailure::Silent {
|
||||
silent_ms,
|
||||
events: events.len(),
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if line.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let value: serde_json::Value = serde_json::from_str(&line)
|
||||
.map_err(|_| "Claude Agent SDK sidecar 输出不是有效 JSON".to_string())?;
|
||||
let value: serde_json::Value = serde_json::from_str(&line).map_err(|_| {
|
||||
SidecarTurnFailure::message(
|
||||
"Claude Agent SDK sidecar 输出不是有效 JSON".to_string(),
|
||||
)
|
||||
})?;
|
||||
match value.get("type").and_then(serde_json::Value::as_str) {
|
||||
Some("event") => {
|
||||
if let Some(event) = value.get("event") {
|
||||
@@ -265,29 +303,53 @@ async fn run_sidecar_turn(
|
||||
}
|
||||
Some("error") => {
|
||||
kill_claude_code_process_tree(pid);
|
||||
return Err(value
|
||||
.get("message")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.unwrap_or("Claude Agent SDK sidecar 执行失败")
|
||||
.to_string());
|
||||
return Err(SidecarTurnFailure::message(
|
||||
value
|
||||
.get("message")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.unwrap_or("Claude Agent SDK sidecar 执行失败")
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let status = child
|
||||
.wait()
|
||||
.await
|
||||
.map_err(|error| format!("等待 Claude Agent SDK sidecar 结束失败:{error}"))?;
|
||||
let result = terminal.ok_or_else(|| "Claude Agent SDK sidecar 缺少终态".to_string())?;
|
||||
let status = child.wait().await.map_err(|error| {
|
||||
SidecarTurnFailure::message(format!("等待 Claude Agent SDK sidecar 结束失败:{error}"))
|
||||
})?;
|
||||
let result = terminal.ok_or_else(|| {
|
||||
SidecarTurnFailure::message("Claude Agent SDK sidecar 缺少终态".to_string())
|
||||
})?;
|
||||
if !status.success() {
|
||||
return Err("Claude Agent SDK sidecar 非零退出".to_string());
|
||||
return Err(SidecarTurnFailure::message(
|
||||
"Claude Agent SDK sidecar 非零退出".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(SidecarTurnResult { events, result })
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(result) => result?,
|
||||
};
|
||||
let outcome = match tokio::time::timeout(CLAUDE_CODE_TURN_MAX_DURATION, turn).await {
|
||||
Ok(Ok(result)) => Ok(result),
|
||||
Ok(Err(SidecarTurnFailure::Message(message))) => Err(message),
|
||||
Ok(Err(SidecarTurnFailure::Silent { silent_ms, events })) => {
|
||||
timeout_reason = Some(format!(
|
||||
"Claude Agent SDK sidecar 回合超时:连续 {silent_ms} ms 没有任何事件(已收到 {events} 个事件)"
|
||||
));
|
||||
Err(String::new())
|
||||
}
|
||||
Err(_) => {
|
||||
timeout_reason = Some(format!(
|
||||
"Claude Agent SDK sidecar 回合超时:超过 {} 分钟硬上限",
|
||||
CLAUDE_CODE_TURN_MAX_DURATION.as_secs() / 60
|
||||
));
|
||||
Err(String::new())
|
||||
}
|
||||
};
|
||||
let result = match outcome {
|
||||
Ok(result) => result,
|
||||
Err(message) => {
|
||||
let Some(reason) = timeout_reason else {
|
||||
return Err(message);
|
||||
};
|
||||
// 先杀进程树:不杀的话 sidecar(连同它守护的 Claude Code CLI)会一直活着,
|
||||
// stderr 等不到 EOF,这一轮就永远出不了终态。
|
||||
kill_claude_code_process_tree(pid);
|
||||
@@ -303,7 +365,7 @@ async fn run_sidecar_turn(
|
||||
let detail = stderr_detail
|
||||
.map(|value| format!(":{value}"))
|
||||
.unwrap_or_default();
|
||||
return Err(format!("Claude Agent SDK sidecar 回合超时{detail}"));
|
||||
return Err(format!("{reason}{detail}"));
|
||||
}
|
||||
};
|
||||
let _ = stderr_task.await;
|
||||
@@ -519,8 +581,15 @@ fn configure_claude_code_environment(
|
||||
] {
|
||||
copy_env(command, name);
|
||||
}
|
||||
let isolated_claude_home = isolated_home.join("claude");
|
||||
let _ = std::fs::create_dir_all(&isolated_claude_home);
|
||||
command
|
||||
.env("HOME", isolated_home)
|
||||
// Windows 上 Node 的 `os.homedir()` 只看 `USERPROFILE`;不隔离它,Claude Code 会去读
|
||||
// 用户自己的 `~/.claude.json` / `~/.claude`(个人 MCP、插件市场、凭据),并把会话写回
|
||||
// 那里——AGC 的 cc 执行器必须与用户本机的 Claude Code 配置完全隔离。
|
||||
.env("USERPROFILE", isolated_home)
|
||||
.env("CLAUDE_CONFIG_DIR", isolated_claude_home)
|
||||
.env("CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", "1")
|
||||
.env("DISABLE_AUTOUPDATER", "1")
|
||||
.env("CI", "1")
|
||||
@@ -529,7 +598,7 @@ fn configure_claude_code_environment(
|
||||
let session = crate::platform_session::current_platform_session();
|
||||
let route = claude_code_route(llm, session.as_ref());
|
||||
app_log!(
|
||||
"agent.direct_codex.claude_route source={:?} host={} credential={}",
|
||||
"agent.direct_codex.claude_route source={:?} host={} auth={}",
|
||||
route.source,
|
||||
route
|
||||
.base_url
|
||||
@@ -537,8 +606,8 @@ fn configure_claude_code_environment(
|
||||
.map(claude_route_host)
|
||||
.unwrap_or_default(),
|
||||
match route.credential {
|
||||
Some(ClaudeCodeCredential::ApiKey(_)) => "x-api-key",
|
||||
Some(ClaudeCodeCredential::Bearer(_)) => "bearer",
|
||||
Some(ClaudeCodeCredential::ApiKey(_)) => "key",
|
||||
Some(ClaudeCodeCredential::Bearer(_)) => "session",
|
||||
None => "none",
|
||||
}
|
||||
);
|
||||
@@ -1145,4 +1214,37 @@ mod tests {
|
||||
);
|
||||
assert_eq!(claude_route_host("http://127.0.0.1:8084"), "127.0.0.1:8084");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sidecar_environment_is_isolated_from_the_local_claude_code_home() {
|
||||
let home = std::env::temp_dir().join("agc-cc-isolated-home-test");
|
||||
let mut command = tokio::process::Command::new("node");
|
||||
configure_claude_code_environment(&mut command, None, &home);
|
||||
let envs = command
|
||||
.as_std()
|
||||
.get_envs()
|
||||
.map(|(name, value)| {
|
||||
(
|
||||
name.to_string_lossy().to_string(),
|
||||
value.map(|value| value.to_string_lossy().to_string()),
|
||||
)
|
||||
})
|
||||
.collect::<std::collections::HashMap<_, _>>();
|
||||
let home_text = home.to_string_lossy().to_string();
|
||||
for name in ["HOME", "USERPROFILE"] {
|
||||
assert_eq!(envs.get(name), Some(&Some(home_text.clone())), "{name}");
|
||||
}
|
||||
assert_eq!(
|
||||
envs.get("CLAUDE_CONFIG_DIR"),
|
||||
Some(&Some(home.join("claude").to_string_lossy().to_string()))
|
||||
);
|
||||
// 本机个人的 Claude Code 环境(中转地址与凭据)不得进入 sidecar。
|
||||
for name in [
|
||||
"ANTHROPIC_API_KEY",
|
||||
"ANTHROPIC_AUTH_TOKEN",
|
||||
"ANTHROPIC_BASE_URL",
|
||||
] {
|
||||
assert!(!envs.contains_key(name), "{name} 不应下发到 sidecar");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user