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:
1 parent
b8e1a07e77
commit
09542e05a1
7 files changed
+276
-32
No files matched your search
+47
-2
@@ -346,6 +346,9 @@ impl SqliteStore {
|
||||
conn.pragma_update(None, "journal_mode", "WAL")?;
|
||||
conn.pragma_update(None, "synchronous", "NORMAL")?;
|
||||
conn.pragma_update(None, "foreign_keys", "ON")?;
|
||||
// Wait briefly for a concurrent writer (e.g. an external reader/tool in
|
||||
// WAL) instead of failing immediately with SQLITE_BUSY.
|
||||
conn.pragma_update(None, "busy_timeout", 5000)?;
|
||||
conn.execute_batch(SCHEMA)?;
|
||||
migrate(&conn)?;
|
||||
seed_context_sections(&conn)?;
|
||||
@@ -637,13 +640,16 @@ impl Store for SqliteStore {
|
||||
fn stats(&self) -> Result<Stats> {
|
||||
let conn = self.lock();
|
||||
let sessions: i64 = conn.query_row("SELECT COUNT(*) FROM sessions", [], |r| r.get(0))?;
|
||||
// Totals + tool-call count in one pass. tool_calls_json is always a JSON
|
||||
// array of calls (or NULL); json_array_length counts the calls themselves.
|
||||
// Totals + tool-call count in one pass. tool_calls_json is a JSON array
|
||||
// of calls (or NULL); guard with json_valid/json_type so one corrupt or
|
||||
// non-array row cannot fail the whole stats query.
|
||||
let (messages, prompt, completion, tool_calls): (i64, i64, i64, i64) = conn.query_row(
|
||||
"SELECT COUNT(*),
|
||||
COALESCE(SUM(prompt_tokens),0),
|
||||
COALESCE(SUM(completion_tokens),0),
|
||||
COALESCE(SUM(CASE WHEN tool_calls_json IS NOT NULL
|
||||
AND json_valid(tool_calls_json)
|
||||
AND json_type(tool_calls_json) = 'array'
|
||||
THEN json_array_length(tool_calls_json) ELSE 0 END),0)
|
||||
FROM messages",
|
||||
[],
|
||||
@@ -975,6 +981,45 @@ mod tests {
|
||||
assert_eq!(st.daily[0].completion_tokens, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stats_survives_a_corrupt_tool_calls_row() {
|
||||
let s = store();
|
||||
let sess = s
|
||||
.create_session("Chat", "m", &SessionSettings::default())
|
||||
.unwrap();
|
||||
// A corrupt (non-JSON) tool_calls_json row must not fail the whole query.
|
||||
s.append_message(
|
||||
&sess.id,
|
||||
NewMessage {
|
||||
role: "assistant",
|
||||
content: "x",
|
||||
reasoning_content: None,
|
||||
tool_calls_json: Some("not-json"),
|
||||
tool_call_id: None,
|
||||
prompt_tokens: 1,
|
||||
completion_tokens: 1,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
// A valid array still counts.
|
||||
s.append_message(
|
||||
&sess.id,
|
||||
NewMessage {
|
||||
role: "assistant",
|
||||
content: "y",
|
||||
reasoning_content: None,
|
||||
tool_calls_json: Some(r#"[{"id":"1"}]"#),
|
||||
tool_call_id: None,
|
||||
prompt_tokens: 1,
|
||||
completion_tokens: 1,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let st = s.stats().unwrap();
|
||||
assert_eq!(st.messages, 2);
|
||||
assert_eq!(st.tool_calls, 1); // corrupt row skipped, valid array counted
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_instance_and_name_lock() {
|
||||
let s = store();
|
||||
|
||||
Reference in new issue
Block a user