Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agents): drop null bytes before storing session logs (#7048)

Wolf Mermelsteine48ce72d0c9a
diff --git a/crates/agent_session/src/outbound/postgres.rs b/crates/agent_session/src/outbound/postgres.rs
index c42178e769..e5df76997e 100644
--- a/crates/agent_session/src/outbound/postgres.rs
+++ b/crates/agent_session/src/outbound/postgres.rs
@@ -96,9 +96,42 @@ fn parse_sandbox_size(value: &str) -> anyhow::Result<SandboxSize> {
/// The wire direction and JSON payload for a [`Message`].
fn message_columns(message: &Message) -> anyhow::Result<(&'static str, serde_json::Value)> {
- match message {
- Message::ToServer(message) => Ok(("to_server", serde_json::to_value(message)?)),
- Message::ToRuntime(message) => Ok(("to_runtime", serde_json::to_value(message)?)),
+ let (direction, mut content) = match message {
+ Message::ToServer(message) => ("to_server", serde_json::to_value(message)?),
+ Message::ToRuntime(message) => ("to_runtime", serde_json::to_value(message)?),
+ };
+ // Postgres `jsonb` rejects U+0000, and a failed log insert stops the session.
+ strip_json_nuls(&mut content);
+ Ok((direction, content))
+}
+
+/// Remove U+0000 from every string in `value`, including object keys.
+fn strip_json_nuls(value: &mut serde_json::Value) {
+ match value {
+ serde_json::Value::String(text) => {
+ if text.contains('\0') {
+ text.retain(|character| character != '\0');
+ }
+ }
+ serde_json::Value::Array(items) => {
+ for item in items {
+ strip_json_nuls(item);
+ }
+ }
+ serde_json::Value::Object(map) => {
+ if map.keys().any(|key| key.contains('\0')) {
+ let entries = std::mem::take(map);
+ for (key, mut child) in entries {
+ strip_json_nuls(&mut child);
+ map.insert(key.replace('\0', ""), child);
+ }
+ } else {
+ for child in map.values_mut() {
+ strip_json_nuls(child);
+ }
+ }
+ }
+ serde_json::Value::Null | serde_json::Value::Bool(_) | serde_json::Value::Number(_) => {}
}
}
diff --git a/crates/agent_session/src/outbound/postgres/test.rs b/crates/agent_session/src/outbound/postgres/test.rs
index 96818fc4eb..a24aeed5bd 100644
--- a/crates/agent_session/src/outbound/postgres/test.rs
+++ b/crates/agent_session/src/outbound/postgres/test.rs
@@ -2616,3 +2616,43 @@ async fn turn_projection_is_atomic_fenced_and_backfill_cannot_overwrite_live_sta
assert_eq!(turn.as_deref(), Some("blocked"));
repo.release(&current).await.unwrap();
}
+
+#[test]
+fn log_json_drops_null_bytes_before_it_reaches_postgres() {
+ let message = Message::ToServer(ToServerMessage::Acp(AcpMessage(
+ RawJsonRpcMessage::notification(
+ "session/update".to_owned(),
+ serde_json::json!({
+ "update": {
+ "content": {"type": "text", "text": "before\0after"},
+ "k\0": ["v\0", "plain"]
+ }
+ }),
+ )
+ .expect("notification"),
+ )));
+
+ let (direction, content) = message_columns(&message).expect("columns");
+
+ assert_eq!(direction, "to_server");
+ let encoded = serde_json::to_string(&content).expect("encode");
+ assert!(
+ !encoded.contains("\\u0000"),
+ "jsonb would reject this payload: {encoded}"
+ );
+ assert_eq!(
+ content
+ .pointer("/params/update/content/text")
+ .and_then(|value| value.as_str()),
+ Some("beforeafter")
+ );
+ assert_eq!(
+ content
+ .pointer("/params/update/k")
+ .and_then(|value| value.as_array()),
+ Some(&vec![
+ serde_json::Value::String("v".to_owned()),
+ serde_json::Value::String("plain".to_owned()),
+ ])
+ );
+}