audit: tool_call_id replay, CoT dup guard, notepad/shell/sqlite/detok hardening (BUG-04/05/06/07/11/16, SEC-11)

This commit is contained in:
adm committed 2026-11-27 18:13:18 +01:00
1 parent b8e1a07e77
commit 09542e05a1
7 files changed
+276 -32

No files matched your search

+104 -16
View File
@@ -103,25 +103,43 @@ fn prefix_messages(
// Tool/system turns stay untagged. Strip any tag the model echoed into
// stored content so we never stack a second one (duplication loop).
let clean = strip_speaker_tags(&m.content);
let content = match m.role.as_str() {
"user" => format!(
"{}{}",
speaker_prefix(user_name, m.created_at, tz_offset),
clean
),
"assistant" => format!(
"{}{}",
speaker_prefix(&sess.title, m.created_at, tz_offset),
clean
),
_ => clean,
};
let mut msg = text_message(role_of(&m.role), content);
msg.tool_calls = m
let tool_calls = m
.tool_calls_json
.as_deref()
.and_then(|s| serde_json::from_str(s).ok());
messages.push(msg);
// A tool-call-only assistant turn carries no text: keep content None to
// match `agent::run_loop` (which pushed the model's message verbatim).
// Stamping a bare [SPEAKER] here would emit a spurious <|content_text|>
// block, priming empty speaker tags and diverging from the cached prefix.
let tool_only = m.role == "assistant" && clean.is_empty() && tool_calls.is_some();
let content = if tool_only {
None
} else {
let text = match m.role.as_str() {
"user" => format!(
"{}{}",
speaker_prefix(user_name, m.created_at, tz_offset),
clean
),
"assistant" => format!(
"{}{}",
speaker_prefix(&sess.title, m.created_at, tz_offset),
clean
),
_ => clean,
};
Some(serde_json::Value::String(text))
};
messages.push(ChatMessage {
role: role_of(&m.role),
content,
// Restore the tool-call id so the template resolves the tool name from
// the preceding assistant turn (and the prefix matches run_loop).
tool_call_id: m.tool_call_id.clone(),
tool_calls,
name: None,
reasoning_content: None,
});
}
messages
}
@@ -326,6 +344,76 @@ mod tests {
);
}
#[test]
fn history_restores_tool_call_id_and_skips_speaker_on_tool_only_turns() {
let sess = Session {
id: "s".into(),
title: "Nebula".into(),
model: "m".into(),
settings: crate::store::SessionSettings::default(),
name_locked: true,
folder: None,
created_at: 0,
updated_at: 0,
};
let calls = serde_json::json!([{
"id": "call_1", "type": "function",
"function": {"name": "shell_exec", "arguments": "{\"command\":\"ls\"}"}
}]);
let history = vec![
StoredMessage {
id: 1,
role: "user".into(),
content: "lista i file".into(),
reasoning_content: None,
tool_calls_json: None,
tool_call_id: None,
prompt_tokens: 0,
completion_tokens: 0,
created_at: 1_700_000_000,
},
StoredMessage {
id: 2,
role: "assistant".into(),
content: "".into(),
reasoning_content: Some("penso".into()),
tool_calls_json: Some(calls.to_string()),
tool_call_id: None,
prompt_tokens: 0,
completion_tokens: 0,
created_at: 1_700_000_001,
},
StoredMessage {
id: 3,
role: "tool".into(),
content: "file.txt".into(),
reasoning_content: None,
tool_calls_json: None,
tool_call_id: Some("call_1".into()),
prompt_tokens: 0,
completion_tokens: 0,
created_at: 1_700_000_002,
},
];
let prefix = compose_prefix(&sess, &history, "Federico", &[], &[]);
// BUG-05: the tool-only assistant turn has no text (no bare [SPEAKER] block).
let asst = prefix
.iter()
.find(|m| matches!(m.role, Role::Assistant))
.unwrap();
assert!(
asst.content.is_none(),
"tool-only turn must not carry a [SPEAKER] text block"
);
assert!(asst.tool_calls.is_some());
// BUG-04: the tool result keeps its id so the template resolves the name.
let tool = prefix
.iter()
.find(|m| matches!(m.role, Role::Tool))
.unwrap();
assert_eq!(tool.tool_call_id.as_deref(), Some("call_1"));
}
#[test]
fn format_stamp_known_dates_and_offset() {
assert_eq!(format_stamp(0, 0), "1970-01-01 00:00");