修复: spawn panic 兜底 + namespace 淘汰提示 + run_command 超时与破坏命令信任门控
This commit is contained in:
@@ -120,8 +120,9 @@ pub(super) async fn maybe_auto_compress(
|
||||
|
||||
// T2-修复(方案A):展开 namespace 引用为真实内容,再喂压缩 LLM/关键词摘要。
|
||||
// 同 agentic/mod.rs 的 build_for_request 出口展开。未展开则压缩 LLM 只看到
|
||||
// "namespace://..." 占位符,摘要基于占位符丢主题。read_only None(重启后 namespace
|
||||
// 清空/残留引用)→ 保留占位符降级,非崩溃。
|
||||
// "namespace://..." 占位符,摘要基于占位符丢主题。read_only None(条目被 LRU 淘汰 /
|
||||
// 重启后 namespace 清空 / 跨会话残留引用)→ 替换为 EVICTED_PLACEHOLDER 提示文案
|
||||
// (非保留无意义 namespace:// URI)+ warn 日志。
|
||||
{
|
||||
let session = session_arc.lock().await;
|
||||
for m in &mut active_msgs {
|
||||
@@ -129,6 +130,13 @@ pub(super) async fn maybe_auto_compress(
|
||||
let path = m.content.clone();
|
||||
if let Some(original) = session.namespace_store.read_only(&path) {
|
||||
m.content = original.to_string();
|
||||
} else {
|
||||
tracing::warn!(
|
||||
conv_id = %conv_id,
|
||||
path = %path,
|
||||
"[namespace] 引用已淘汰,替换为提示文案"
|
||||
);
|
||||
m.content = df_ai::namespace_store::EVICTED_PLACEHOLDER.to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -809,7 +809,13 @@ pub(crate) async fn run_agentic_loop(
|
||||
// conv_id 来源:run_agentic_loop 入参(loop 启动快照,与 guard 一致)。
|
||||
let (stop_flag, notify) = {
|
||||
let session = session_arc.lock().await;
|
||||
let conv = session.conv_read(&conv_id).expect("[F-09] loop 入口桥接后 per_conv 必存在");
|
||||
// panic-guard:原裸 .expect 在 conv 已删(并发删除/状态竞态)时 panic,
|
||||
// guard 不 reset/终态不发/registry 不清致永久卡。改为 None 显式退出(对齐同函数其他 return 点;
|
||||
// guard Drop 兜底复位 generating)。
|
||||
let Some(conv) = session.conv_read(&conv_id) else {
|
||||
tracing::warn!(stale_conv = %conv_id, "[ai] loop 入口 conv 已删,退出");
|
||||
return;
|
||||
};
|
||||
(conv.stop_flag.clone(), conv.notify.clone())
|
||||
};
|
||||
|
||||
@@ -1215,12 +1221,20 @@ pub(crate) async fn run_agentic_loop(
|
||||
// namespace_store 引用化进 messages;持久化(conversation.rs)已展开写 DB,但发 LLM 的
|
||||
// 实时请求此前未展开 → LLM 每轮只看到 "namespace://..." 占位符,AI 实际拿不到工具结果。
|
||||
// 此处展开,namespace 退化为内存/持久化层优化,LLM 永远看真实内容。
|
||||
// read_only None(重启后 namespace 清空 / 跨会话残留引用)→ 保留占位符降级,非崩溃。
|
||||
// read_only None(条目被 LRU 淘汰 / 重启后 namespace 清空 / 跨会话残留引用)→
|
||||
// 替换为 EVICTED_PLACEHOLDER 提示文案(非保留无意义 namespace:// URI)+ warn 日志。
|
||||
for m in &mut history_msgs {
|
||||
if df_ai::namespace_store::is_namespace_ref(&m.content) {
|
||||
let path = m.content.clone();
|
||||
if let Some(original) = session.namespace_store.read_only(&path) {
|
||||
m.content = original.to_string();
|
||||
} else {
|
||||
tracing::warn!(
|
||||
conv_id = %conv_id,
|
||||
path = %path,
|
||||
"[namespace] 引用已淘汰,替换为提示文案"
|
||||
);
|
||||
m.content = df_ai::namespace_store::EVICTED_PLACEHOLDER.to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ use super::cache::{find_cached_high_risk_result, pending_placeholder_for};
|
||||
use super::diff::build_write_file_diff;
|
||||
use super::reason::build_approval_reason;
|
||||
use super::record::audit_tool_call;
|
||||
use super::super::{AiChatEvent, AiSession, ApprovalKind, PendingApproval, ToolCallDraft, trust_key_for, TrustKey};
|
||||
use super::super::{AiChatEvent, AiSession, ApprovalKind, PendingApproval, ToolCallDraft, is_destructive_command, trust_key_for, TrustKey};
|
||||
|
||||
/// 按 risk_level + auto_exec_mode 判定是否自动执行。
|
||||
///
|
||||
@@ -102,6 +102,22 @@ pub(super) async fn check_trust_hits(
|
||||
conv_id: &str,
|
||||
) -> Option<TrustKey> {
|
||||
let key = trust_key_for(&draft.name, args)?;
|
||||
// 安全门控(run_command 会话信任按目录不按命令的补丁):
|
||||
// run_command 的信任 key 只用 working_dir,一次批准 → 同目录任意命令 auto。
|
||||
// 破坏性命令(rm/del/format/... 不可逆)绝不 auto 放行,即使同目录已批准过,
|
||||
// 也必须每轮走正常审批。命中黑名单直接返 None,让流程继续走 pending 审批。
|
||||
if matches!(key, TrustKey::Execute { .. }) {
|
||||
if let Some(cmd) = args.get("command").and_then(|v| v.as_str()) {
|
||||
if is_destructive_command(cmd) {
|
||||
tracing::info!(
|
||||
tool = %draft.name,
|
||||
new_tool_call_id = %draft.id,
|
||||
"[会话信任] 命中破坏性命令黑名单,跳过 auto 放行(走正常审批)"
|
||||
);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 短 lock 读 session_trust(仅 contains 判定,无 await,纳秒级),命中即返 key
|
||||
let hit = {
|
||||
let session = session_arc.lock().await;
|
||||
|
||||
@@ -139,17 +139,33 @@ async fn execute_with_heartbeat(
|
||||
let _guard = HeartbeatGuard { stop, handle: heartbeat };
|
||||
// tools.execute 无 timeout 时,卡死工具(run_command 长命令/read_file 大文件/同步
|
||||
// 阻塞工具)永久挂起 → process_tool_calls 持 session lock 永久 → guard.reset 等 lock → AiCompleted
|
||||
// 永不发 → 前端"回答完卡住/超时清空"。60s timeout 兜底:超时返错误 tool_result,锁释放,loop 续跑。
|
||||
// 心跳 30s 续命前端 watchdog,60s timeout 覆盖绝大多数工具(run_command 已自带 10s 子超时)。
|
||||
match tokio::time::timeout(Duration::from_secs(60), tools.execute(name, args)).await {
|
||||
// 永不发 → 前端"回答完卡住/超时清空"。外层 timeout 兜底:超时返错误 tool_result,锁释放,loop 续跑。
|
||||
// 心跳 30s 续命前端 watchdog。
|
||||
//
|
||||
// 外层 timeout 取值(BUF-run-cmd-timeout):原硬编码 60s 会先于 run_command 自带 timeout_secs
|
||||
// (上限 600s)drop,致 run_command 跑长构建(cargo/npm)永远到不了用户配的 timeout_secs。
|
||||
// run_command 分支从 args.timeout_secs 取值(clamp [60,600],默认 60,与 tool_registry.rs:2707
|
||||
// run_command 内部 clamp 同源),其余工具仍 60s。run_command 内部还有自己的子 timeout,
|
||||
// 外层只需 >= 内部 timeout 即不抢断(run_command 内部超时会返带语义的 tool_result,优于外层裸中止)。
|
||||
let outer_secs: u64 = if name == "run_command" {
|
||||
args.get("timeout_secs")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(60)
|
||||
.clamp(60, 600)
|
||||
} else {
|
||||
60
|
||||
};
|
||||
match tokio::time::timeout(Duration::from_secs(outer_secs), tools.execute(name, args)).await {
|
||||
Ok(result) => result,
|
||||
Err(_elapsed) => {
|
||||
tracing::error!(
|
||||
conv_id = %conv_id,
|
||||
tool = %name,
|
||||
"[ai] 工具执行超时(60s),返回错误 tool_result(防 process_tool_calls 持 session lock 永久卡死)"
|
||||
outer_secs,
|
||||
"[ai] 工具执行超时({}s),返回错误 tool_result(防 process_tool_calls 持 session lock 永久卡死)",
|
||||
outer_secs
|
||||
);
|
||||
Err(anyhow::anyhow!("工具执行超时(60s),已中止(防死锁)"))
|
||||
Err(anyhow::anyhow!("工具执行超时({}s),已中止(防死锁)", outer_secs))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -489,6 +489,26 @@ fn dir_of_path_normalized(path: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// 判定 run_command 的命令串是否「破坏性」(rm/del/format/... 等不可逆操作)。
|
||||
///
|
||||
/// 用于会话信任放行门控:破坏性命令**永不**走 session_trust auto 放行,即使同会话
|
||||
/// 已批准过同目录的 run_command,也必须每轮走正常审批(避免「一次批准同目录 →
|
||||
/// rm -rf 任意文件被自动放行」的安全漏洞)。
|
||||
///
|
||||
/// 判定规则:取命令**首 token** 精确匹配,避免前缀法误伤 delta/delve 等以 del 开头的
|
||||
/// 合法命令。黑名单只覆盖「不可逆破坏」类;curl/wget/scp 等下载拷贝类的风险(数据外泄/
|
||||
/// 恶意脚本)由 run_command 的 High risk 审批(用户每轮看完整命令)覆盖,不进黑名单,
|
||||
/// 避免误伤频繁合法下载。
|
||||
/// 局限:不解析 sudo/doas/env 等提权前缀(`sudo rm` 首 token 是 sudo 不命中)——本层是
|
||||
/// 「尽力」补充,主防线仍是 High risk 审批。
|
||||
pub fn is_destructive_command(cmd: &str) -> bool {
|
||||
const DESTRUCTIVE_VERBS: &[&str] = &[
|
||||
"rm", "rmdir", "del", "erase", "format", "remove-item", "dd", "mkfs",
|
||||
];
|
||||
let first = cmd.trim().split_whitespace().next().unwrap_or("").to_lowercase();
|
||||
DESTRUCTIVE_VERBS.iter().any(|v| first == *v)
|
||||
}
|
||||
|
||||
/// run_command / write_file 共用的目录 key 规范化:
|
||||
/// 尝试 canonicalize(去 symlink / .. / 大小写归一),失败回退原字面量 trim。
|
||||
fn normalize_dir_key(dir: &str) -> String {
|
||||
|
||||
Reference in New Issue
Block a user