Track cumulative MCP attribution across requests and thread history (#47081)

## What changed

- Record each MCP source and its first turn, including connector and plugin identifiers when available. Cover direct and Code Mode calls, including returned errors and results that are not printed.
- Persist cumulative attribution checkpoints and restore them across compaction, resume, and forks, including forks limited to recent turns.
- Attach fresh attribution to Responses API `client_metadata` under `mcp_attribution`, outside model-visible content. Restrict transmission to allowed OpenAI HTTPS destinations for both HTTP and WebSocket requests.
- Report `attribution_error` for legacy history without checkpoints, malformed or conflicting attribution, and serialized attribution exceeding 16 KiB.

## Testing

Add coverage for source deduplication, checkpoint persistence and restoration, recent-turn forks, direct and Code Mode calls, destination filtering, and the serialized size limit.

GitOrigin-RevId: fd285e152ca9e24bfa000debb20013709d67a86e
This commit is contained in:
peilin-openai
2026-09-21 18:19:07 +00:00
committed by copyberry
parent 7cec3e110c
commit ac98537678
28 changed files with 1119 additions and 73 deletions
+49 -2
View File
@@ -1,5 +1,4 @@
//! JSON serialization helpers for output that must remain parseable as JSON
//! while staying safe for ASCII-only transports.
//! JSON serialization helpers for ASCII-only transports and bounded output.
use std::io;
@@ -54,6 +53,43 @@ where
.map_err(|err| serde_json::Error::io(io::Error::new(io::ErrorKind::InvalidData, err)))
}
/// Serialize UTF-8 JSON while retaining at most `max_bytes` of output.
pub fn to_json_string_bounded<T>(value: &T, max_bytes: usize) -> serde_json::Result<String>
where
T: Serialize + ?Sized,
{
struct BoundedBuffer {
bytes: Vec<u8>,
max_bytes: usize,
}
impl io::Write for BoundedBuffer {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
if bytes.len() > self.max_bytes.saturating_sub(self.bytes.len()) {
return Err(io::Error::new(
io::ErrorKind::WriteZero,
"JSON byte limit exceeded",
));
}
self.bytes.extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let mut buffer = BoundedBuffer {
bytes: Vec::new(),
max_bytes,
};
let mut serializer = serde_json::Serializer::new(&mut buffer);
value.serialize(&mut serializer)?;
String::from_utf8(buffer.bytes)
.map_err(|err| serde_json::Error::io(io::Error::new(io::ErrorKind::InvalidData, err)))
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
@@ -65,6 +101,7 @@ mod tests {
use serde_json::json;
use super::to_ascii_json_string;
use super::to_json_string_bounded;
#[test]
fn to_ascii_json_string_escapes_non_ascii_strings() {
@@ -119,4 +156,14 @@ mod tests {
let parsed: Value = serde_json::from_str(&serialized).expect("serialized json");
assert_eq!(parsed, expected_value);
}
#[test]
fn bounded_json_counts_utf8_output_bytes() {
assert_eq!(
to_json_string_bounded(&"é", /*max_bytes*/ 4).unwrap(),
r#""é""#
);
assert!(to_json_string_bounded(&"é", /*max_bytes*/ 3).is_err());
assert!(to_json_string_bounded(&"a".repeat(100_000), 16 * 1024).is_err());
}
}
+1
View File
@@ -2,6 +2,7 @@ mod json;
mod truncate;
pub use json::to_ascii_json_string;
pub use json::to_json_string_bounded;
pub use truncate::approx_bytes_for_tokens;
pub use truncate::approx_token_count;
pub use truncate::approx_tokens_from_byte_count;