Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

feat(workstream): retain sanitized event provenance

Éverton Toffanetto9c64ec4a5c5d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 762d7fbb..9b27ffb1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
+- Added bounded, sanitized native-record and adapter correlation provenance to
+ managed-workstream search/tail results and the CLI's text/JSON output, with
+ compatible optional fields and sanitization of legacy provenance on reads.
- Added `install-hooks --agent claude-code --scope project`, which writes the
hook configuration to the checkout's gitignored `.claude/settings.local.json`
(where Claude Code reads it: the git root, or the launch directory on
@@ -118,6 +121,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
older servers leave unsupported fields unknown. (#1016)
### Changed
+- Restricted managed-workstream provenance to scrubbed source labels and an
+ explicit scalar metadata allowlist, with 512-byte string bounds. Client event
+ ids beginning with `managed-run:` are reserved for server checkpoints and
+ extraction-loss annotations. Active-run retries reject changes to an existing
+ event's agent, native session or kind, and retain its first indexed content
+ and provenance across sanitizer changes.
- Documented FutureInfra as an endpoint for the existing `openai-compat`
provider. (#1026)
- `.github/workflows/nix.yml` builds the flake on `x86_64-linux` for path-
diff --git a/crates/ai-memory-cli/src/commands/workstream_search.rs b/crates/ai-memory-cli/src/commands/workstream_search.rs
index 25e7cad7..24abed47 100644
--- a/crates/ai-memory-cli/src/commands/workstream_search.rs
+++ b/crates/ai-memory-cli/src/commands/workstream_search.rs
@@ -29,12 +29,18 @@ pub async fn run(config: &Config, args: WorkstreamSearchArgs) -> Result<()> {
for event in events {
let role = event.role.as_deref().unwrap_or(event.kind.as_str());
println!(
- "## Event {} | {} | {}\n{}\n",
+ "## Event {} | {} | {}",
event.sequence,
event.agent.as_str(),
role,
- event.content
);
+ if let Some(source) = &event.source_record_id {
+ println!("Source record: {}", serde_json::to_string(source)?);
+ }
+ if !event.metadata.is_null() {
+ println!("Provenance: {}", serde_json::to_string(&event.metadata)?);
+ }
+ println!("{}\n", event.content);
}
Ok(())
}
diff --git a/crates/ai-memory-cli/tests/suite/main.rs b/crates/ai-memory-cli/tests/suite/main.rs
index 78aac145..4075e0a9 100644
--- a/crates/ai-memory-cli/tests/suite/main.rs
+++ b/crates/ai-memory-cli/tests/suite/main.rs
@@ -30,4 +30,5 @@ mod serve_shutdown;
mod server_profiles;
mod shutdown_signals;
mod upgrade_e2e;
+mod workstream_provenance;
mod yolo_ai_jail;
diff --git a/crates/ai-memory-cli/tests/suite/workstream_provenance.rs b/crates/ai-memory-cli/tests/suite/workstream_provenance.rs
new file mode 100644
index 00000000..5f360da3
--- /dev/null
+++ b/crates/ai-memory-cli/tests/suite/workstream_provenance.rs
@@ -0,0 +1,76 @@
+//! The shipped CLI renders provenance and accepts older server DTOs.
+
+use axum::{Json, Router, routing::get};
+use serde_json::{Value, json};
+
+#[tokio::test]
+async fn workstream_search_renders_provenance_and_accepts_older_servers() {
+ let workstream = "018f0000-0000-7000-8000-000000000001";
+ for provenance in [true, false] {
+ let mut event = json!({"sequence": 1, "event_id": "event-1", "agent": "codex",
+ "native_session_id": "native-1", "kind": "tool_result", "role": "tool", "content": "visible result"});
+ if provenance {
+ event["source_record_id"] = json!("record-1");
+ event["metadata"] = json!({"tool_call_id": "call-1", "is_error": false});
+ }
+ let fixture = event.clone();
+ let app = Router::new().route(
+ &format!("/workstream/{workstream}/events"),
+ get(move || {
+ let event = fixture.clone();
+ async move { Json(vec![event]) }
+ }),
+ );
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let base = format!("http://{}", listener.local_addr().unwrap());
+ let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
+ let data = tempfile::tempdir().unwrap();
+ let home = tempfile::tempdir().unwrap();
+ let cwd = tempfile::tempdir().unwrap();
+ for structured in [false, true] {
+ let mut command = crate::e2e_support::hermetic(env!("CARGO_BIN_EXE_ai-memory"));
+ command
+ .current_dir(cwd.path())
+ .env("AI_MEMORY_HOME", home.path())
+ .env("AI_MEMORY_DATA_DIR", data.path())
+ .env("AI_MEMORY_SERVER_URL", &base)
+ .env("RUST_LOG", "off")
+ .args([
+ "workstream-search",
+ "--workstream-id",
+ workstream,
+ "visible",
+ ]);
+ if structured {
+ command.arg("--json");
+ }
+ let output = tokio::process::Command::from(command)
+ .output()
+ .await
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "{}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let stdout = String::from_utf8(output.stdout).unwrap();
+ if structured {
+ let events: Value = serde_json::from_str(&stdout).unwrap();
+ assert_eq!(events[0]["source_record_id"], event["source_record_id"]);
+ assert_eq!(events[0]["metadata"], event["metadata"]);
+ } else {
+ assert!(stdout.contains("visible result"));
+ if provenance {
+ assert!(
+ stdout.contains("Source record: \"record-1\""),
+ "missing source record: {stdout}"
+ );
+ assert!(stdout.contains("\"tool_call_id\":\"call-1\""));
+ } else {
+ assert!(!stdout.contains("Source record:"));
+ }
+ }
+ }
+ server.abort();
+ }
+}
diff --git a/crates/ai-memory-core/src/lib.rs b/crates/ai-memory-core/src/lib.rs
index ded98d7f..16cee73d 100644
--- a/crates/ai-memory-core/src/lib.rs
+++ b/crates/ai-memory-core/src/lib.rs
@@ -103,4 +103,5 @@ pub use workstream::{
ManagedRunStatus, ManagedWorkstreamSummary, NewWorkstreamEvent, PrepareManagedRunRequest,
PrepareManagedRunResponse, RenameManagedWorkstreamRequest, RenamedManagedWorkstream,
UNTRUSTED_MEMORY_NOTICE, WorkstreamCheckpoint, WorkstreamEvent, WorkstreamEventKind,
+ scrub_workstream_provenance,
};
diff --git a/crates/ai-memory-core/src/workstream.rs b/crates/ai-memory-core/src/workstream.rs
index 035de2a4..ac722595 100644
--- a/crates/ai-memory-core/src/workstream.rs
+++ b/crates/ai-memory-core/src/workstream.rs
@@ -100,6 +100,60 @@ pub struct NewWorkstreamEvent {
pub metadata: serde_json::Value,
}
+/// Scrub correlation labels with the configured privacy strip, then bound each
+/// UTF-8 value. Unknown keys and non-scalar adapter dumps are discarded.
+/// These labels are untrusted attribution, never scope or authority.
+#[must_use]
+pub fn scrub_workstream_provenance(
+ sanitizer: &crate::Sanitizer,
+ source_record_id: Option<&str>,
+ metadata: &serde_json::Value,
+) -> (Option<String>, serde_json::Value) {
+ let scrub = |value: &str| crate::truncate_utf8_bytes(&sanitizer.scrub(value), 512);
+ let source = source_record_id
+ .map(scrub)
+ .filter(|id| !id.trim().is_empty());
+ let mut allowed = serde_json::Map::new();
+ // Only fields emitted by the shipped native adapters and run boundaries.
+ for key in [
+ "tool",
+ "tool_call_id",
+ "tool_use_id",
+ "parent_id",
+ "summary_type",
+ "status",
+ ] {
+ if let Some(value) = metadata.get(key).and_then(serde_json::Value::as_str) {
+ allowed.insert(key.into(), serde_json::Value::String(scrub(value)));
+ }
+ }
+ if let Some(value) = metadata
+ .get("is_error")
+ .and_then(serde_json::Value::as_bool)
+ {
+ allowed.insert("is_error".into(), value.into());
+ }
+ if let Some(value) = metadata
+ .get("exit_code")
+ .and_then(serde_json::Value::as_i64)
+ && i32::try_from(value).is_ok()
+ {
+ allowed.insert("exit_code".into(), value.into());
+ }
+ if let Some(value) = metadata
+ .get("loss_count")
+ .and_then(serde_json::Value::as_u64)
+ {
+ allowed.insert("loss_count".into(), value.into());
+ }
+ let metadata = if allowed.is_empty() {
+ serde_json::Value::Null
+ } else {
+ serde_json::Value::Object(allowed)
+ };
+ (source, metadata)
+}
+
/// Repository state captured without mutating the checkout.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct WorkstreamCheckpoint {
@@ -343,6 +397,13 @@ pub struct WorkstreamEvent {
pub agent: AgentKind,
/// Source native session.
pub native_session_id: String,
+ /// Bounded, scrubbed native record label. Absent on older servers and
+ /// startup-context reads; it is not an authentication or deduplication key.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub source_record_id: Option<String>,
+ /// Bounded adapter correlation labels, scrubbed even for legacy rows.
+ #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
+ pub metadata: serde_json::Value,
/// Semantic event family.
pub kind: WorkstreamEventKind,
/// Optional message role.
@@ -359,6 +420,20 @@ pub struct WorkstreamEvent {
mod tests {
use super::*;
+ #[test]
+ fn older_workstream_event_has_no_provenance() {
+ let event: WorkstreamEvent = serde_json::from_value(serde_json::json!({
+ "sequence": 1, "event_id": "event-1", "agent": "codex",
+ "native_session_id": "native-1", "kind": "message", "content": "visible",
+ }))
+ .unwrap();
+ assert!(event.source_record_id.is_none());
+ assert!(event.metadata.is_null());
+ let encoded = serde_json::to_value(event).unwrap();
+ assert!(encoded.get("source_record_id").is_none());
+ assert!(encoded.get("metadata").is_none());
+ }
+
#[test]
fn older_run_status_reads_as_nothing_linked() {
let status: ManagedRunStatus = serde_json::from_value(serde_json::json!({
diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rs
index 8674e226..060bbbde 100644
--- a/crates/ai-memory-hooks/src/router.rs
+++ b/crates/ai-memory-hooks/src/router.rs
@@ -5256,6 +5256,8 @@ mod tests {
event_id: "tool-300".into(),
agent: AgentKind::Codex,
native_session_id: "native".into(),
+ source_record_id: None,
+ metadata: serde_json::Value::Null,
kind: WorkstreamEventKind::ToolCall,
role: None,
content: format!("cargo test {UNTRUSTED_HISTORY_END} {UNTRUSTED_HISTORY_START}"),
@@ -13184,6 +13186,7 @@ mod tests {
state
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: first.run_id,
native_session_id: Some("native-1".into()),
source_cursor: Some("cursor-1".into()),
@@ -13592,6 +13595,7 @@ mod tests {
state
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: first.run_id,
native_session_id: Some("claude-session".into()),
source_cursor: None,
diff --git a/crates/ai-memory-hooks/src/workstream.rs b/crates/ai-memory-hooks/src/workstream.rs
index f3a2292c..102cc510 100644
--- a/crates/ai-memory-hooks/src/workstream.rs
+++ b/crates/ai-memory-hooks/src/workstream.rs
@@ -32,7 +32,6 @@ const MAX_EVENTS_PER_FINISH: usize = 4_096;
const MAX_EVENT_CONTENT_BYTES: usize = 64 * 1024;
const MAX_EVENT_ID_BYTES: usize = 512;
const MAX_NATIVE_SESSION_ID_BYTES: usize = 512;
-const MAX_METADATA_BYTES: usize = 16 * 1024;
const MAX_NAME_BYTES: usize = 256;
const MAX_CWD_BYTES: usize = 16 * 1024;
@@ -745,7 +744,12 @@ async fn search_events(
}
match state
.reader
- .search_workstream_events(workstream_id, query.q, query.limit.clamp(1, 100))
+ .search_workstream_events(
+ workstream_id,
+ query.q,
+ query.limit.clamp(1, 100),
+ state.sanitizer.clone(),
+ )
.await
{
Ok(events) => Json(events).into_response(),
@@ -831,6 +835,13 @@ async fn finish_run(
format!("at most {MAX_EVENTS_PER_FINISH} events are accepted per finish"),
);
}
+ if request
+ .events
+ .iter()
+ .any(|event| event.event_id.starts_with("managed-run:"))
+ {
+ return error(StatusCode::BAD_REQUEST, "reserved workstream event id");
+ }
if request
.native_session_id
.as_deref()
@@ -847,6 +858,7 @@ async fn finish_run(
return match state
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: state.sanitizer.clone(),
run_id,
native_session_id: None,
source_cursor: None,
@@ -903,6 +915,7 @@ async fn finish_run(
}
};
let input = FinishWorkstreamRun {
+ sanitizer: state.sanitizer.clone(),
run_id,
native_session_id: request.native_session_id.or(status.native_session_id),
source_cursor: request.source_cursor,
@@ -976,14 +989,11 @@ fn sanitize_events(
{
return Err("invalid workstream message role".to_string());
}
- let raw_metadata = serde_json::to_string(&event.metadata).map_err(|e| e.to_string())?;
- if raw_metadata.len() > MAX_METADATA_BYTES {
- event.metadata = serde_json::json!({ "truncated": true });
- } else {
- let scrubbed = sanitizer.scrub(&raw_metadata);
- event.metadata = serde_json::from_str(&scrubbed)
- .unwrap_or_else(|_| serde_json::json!({ "redacted": true }));
- }
+ (event.source_record_id, event.metadata) = ai_memory_core::scrub_workstream_provenance(
+ sanitizer,
+ event.source_record_id.as_deref(),
+ &event.metadata,
+ );
}
Ok(())
}
@@ -1164,6 +1174,266 @@ mod tests {
(workspace_id, project_id)
}
+ #[tokio::test]
+ async fn workstream_provenance_finish_scrubs_before_raw_segment_and_search() {
+ let temp = TempDir::new().unwrap();
+ let store = Store::open(temp.path()).unwrap();
+ let mut state = test_state(&store, temp.path());
+ state.sanitizer = Sanitizer::new(&ai_memory_core::SanitizeConfig {
+ extra_patterns: vec!["private-correlation".into()],
+ allowlist: Vec::new(),
+ })
+ .unwrap();
+ let (workspace_id, project_id) = seed_scope(&store).await;
+ let run = store
+ .writer
+ .prepare_workstream_run(prepare_input(
+ workspace_id,
+ project_id,
+ AgentKind::Codex,
+ "launcher",
+ ))
+ .await
+ .unwrap();
+ let response = finish_run(
+ State(state.clone()), None, None, AxumPath(run.run_id.to_string()),
+ Json(FinishManagedRunRequest {
+ native_session_id: Some("native-1".into()), source_cursor: None,
+ events: vec![NewWorkstreamEvent {
+ event_id: "event-1".into(), agent: AgentKind::Codex,
+ native_session_id: "native-1".into(),
+ source_record_id: Some("private-correlation".into()),
+ kind: WorkstreamEventKind::ToolResult, role: Some("tool".into()),
+ content: "portable provenance sentinel".into(), occurred_at: None,
+ metadata: serde_json::json!({"tool_call_id": "call-1", "parent_id": "private-correlation", "dump": "private payload"}),
+ }],
+ complete: false, checkpoint: Default::default(), exit_code: None, losses: Vec::new(),
+ }),
+ ).await;
+ assert_eq!(response.status(), StatusCode::OK);
+ let segment_dir = temp
+ .path()
+ .join("raw/workstreams")
+ .join(run.workstream_id.to_string())
+ .join("segments");
+ let path = std::fs::read_dir(segment_dir)
+ .unwrap()
+ .next()
+ .unwrap()
+ .unwrap()
+ .path();
+ let raw = std::fs::read_to_string(path).unwrap();
+ assert!(
+ !raw.contains("private-correlation"),
+ "raw segment leaked source id"
+ );
+ assert!(!raw.contains("private payload"));
+ for q in ["", "portable"] {
+ let response = search_events(
+ State(state.clone()),
+ None,
+ None,
+ AxumPath(run.workstream_id.to_string()),
+ Query(EventQuery {
+ q: q.into(),
+ ..Default::default()
+ }),
+ )
+ .await;
+ assert_eq!(response.status(), StatusCode::OK);
+ let body = to_bytes(response.into_body(), 64 * 1024).await.unwrap();
+ let events: serde_json::Value = serde_json::from_slice(&body).unwrap();
+ assert_eq!(events[0]["source_record_id"], "[REDACTED:custom]");
+ assert_eq!(events[0]["metadata"]["parent_id"], "[REDACTED:custom]");
+ assert_eq!(events[0]["metadata"]["tool_call_id"], "call-1");
+ assert!(events[0]["metadata"].get("dump").is_none());
+ }
+ let original: NewWorkstreamEvent = serde_json::from_str(raw.trim()).unwrap();
+ let mut next = original.clone();
+ next.event_id = "event-2".into();
+ next.source_record_id = Some("record-2".into());
+ let mut collision = original.clone();
+ collision.kind = WorkstreamEventKind::Message;
+ let import = |events| {
+ finish_run(
+ State(state.clone()),
+ None,
+ None,
+ AxumPath(run.run_id.to_string()),
+ Json(FinishManagedRunRequest {
+ native_session_id: Some("native-1".into()),
+ source_cursor: None,
+ events,
+ complete: false,
+ checkpoint: Default::default(),
+ exit_code: None,
+ losses: Vec::new(),
+ }),
+ )
+ };
+ assert_eq!(
+ import(vec![next.clone(), collision]).await.status(),
+ StatusCode::BAD_REQUEST
+ );
+ let read = || {
+ store.reader.search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 10,
+ state.sanitizer.clone(),
+ )
+ };
+ assert_eq!(
+ read().await.unwrap().len(),
+ 1,
+ "failed segment must not become indexed history"
+ );
+ let recovered = import(vec![next, original]).await;
+ assert_eq!(recovered.status(), StatusCode::OK);
+ let body = to_bytes(recovered.into_body(), 64 * 1024).await.unwrap();
+ let recovered: FinishManagedRunResponse = serde_json::from_slice(&body).unwrap();
+ assert_eq!(recovered.imported_events, 1);
+ assert_eq!(recovered.latest_sequence, 2);
+ assert_eq!(read().await.unwrap().len(), 2);
+ let original: NewWorkstreamEvent = serde_json::from_str(raw.trim()).unwrap();
+ let complete = |events| {
+ finish_run(
+ State(state.clone()),
+ None,
+ None,
+ AxumPath(run.run_id.to_string()),
+ Json(FinishManagedRunRequest {
+ native_session_id: Some("native-1".into()),
+ source_cursor: None,
+ events,
+ complete: true,
+ checkpoint: Default::default(),
+ exit_code: None,
+ losses: Vec::new(),
+ }),
+ )
+ };
+ assert_eq!(
+ complete(vec![original.clone()]).await.status(),
+ StatusCode::OK
+ );
+ let mut changed = original.clone();
+ changed.source_record_id = Some("different-record".into());
+ let mut late = original.clone();
+ late.event_id = "late-new-event".into();
+ assert_eq!(complete(vec![changed, late]).await.status(), StatusCode::OK);
+ assert_eq!(complete(vec![original]).await.status(), StatusCode::OK);
+ let hits = read().await.unwrap();
+ assert_eq!(hits.len(), 3);
+ assert!(
+ hits.iter()
+ .any(|hit| hit.kind == WorkstreamEventKind::Checkpoint)
+ );
+ assert!(!hits.iter().any(|hit| hit.event_id == "late-new-event"));
+ }
+
+ #[tokio::test]
+ async fn workstream_provenance_client_cannot_use_server_event_ids() {
+ let temp = TempDir::new().unwrap();
+ let store = Store::open(temp.path()).unwrap();
+ let state = test_state(&store, temp.path());
+ let (workspace_id, project_id) = seed_scope(&store).await;
+ let run = store
+ .writer
+ .prepare_workstream_run(prepare_input(
+ workspace_id,
+ project_id,
+ AgentKind::Codex,
+ "launcher",
+ ))
+ .await
+ .unwrap();
+ let import = |id: String, complete| {
+ finish_run(
+ State(state.clone()),
+ None,
+ None,
+ AxumPath(run.run_id.to_string()),
+ Json(FinishManagedRunRequest {
+ native_session_id: Some("native-1".into()),
+ source_cursor: None,
+ events: vec![NewWorkstreamEvent {
+ event_id: id,
+ agent: AgentKind::Codex,
+ native_session_id: "native-1".into(),
+ source_record_id: Some("record-1".into()),
+ kind: WorkstreamEventKind::Message,
+ role: Some("assistant".into()),
+ content: "legitimate native event".into(),
+ occurred_at: None,
+ metadata: serde_json::Value::Null,
+ }],
+ complete,
+ checkpoint: Default::default(),
+ exit_code: Some(0),
+ losses: if complete {
+ vec!["missing native record".into()]
+ } else {
+ Vec::new()
+ },
+ }),
+ )
+ };
+ // Legitimate native ids work before and after the attempted forgery.
+ assert_eq!(
+ import("native-event".into(), false).await.status(),
+ StatusCode::OK
+ );
+ let segment_dir = temp
+ .path()
+ .join("raw/workstreams")
+ .join(run.workstream_id.to_string())
+ .join("segments");
+ for id in [
+ format!("managed-run:{}:checkpoint", run.run_id),
+ format!("managed-run:{}:losses", run.run_id),
+ "managed-run:other-run:annotation".into(),
+ ] {
+ assert_eq!(import(id, false).await.status(), StatusCode::BAD_REQUEST);
+ assert_eq!(
+ std::fs::read_dir(&segment_dir).unwrap().count(),
+ 1,
+ "reserved ids must be refused before writing raw segments"
+ );
+ }
+ assert_eq!(
+ import("native-event".into(), true).await.status(),
+ StatusCode::OK
+ );
+ let hits = store
+ .reader
+ .search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 10,
+ state.sanitizer.clone(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(hits.len(), 3);
+ assert!(hits.iter().any(|hit| hit.event_id == "native-event"));
+ for (suffix, kind) in [
+ ("checkpoint", WorkstreamEventKind::Checkpoint),
+ ("losses", WorkstreamEventKind::Annotation),
+ ] {
+ assert!(hits.iter().any(|hit| hit.event_id
+ == format!("managed-run:{}:{suffix}", run.run_id)
+ && hit.kind == kind
+ && hit.source_record_id.is_none()));
+ }
+ assert_eq!(
+ import("managed-run:late:checkpoint".into(), false)
+ .await
+ .status(),
+ StatusCode::BAD_REQUEST
+ );
+ }
+
/// The launcher reads whether the run's child linked a session from the
/// run status, so the route must carry it.
#[tokio::test]
@@ -1321,6 +1591,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: prepared.run_id,
native_session_id: Some("private-native-id".into()),
source_cursor: None,
@@ -1414,6 +1685,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: claude.run_id,
native_session_id: Some("claude-current".into()),
source_cursor: Some("cursor".into()),
@@ -1492,6 +1764,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: prepared.run_id,
native_session_id: Some("session_abc".into()),
source_cursor: None,
@@ -1753,6 +2026,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: prepared.run_id,
native_session_id: Some("7c1d5698-204a-4c0f-ae9c-43db7fc4e41d".into()),
source_cursor: None,
@@ -1822,6 +2096,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: prepared.run_id,
native_session_id: Some("2cce5126-f57d-4ddd-8f66-e5bb409f60db".into()),
source_cursor: None,
@@ -1891,6 +2166,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: prepared.run_id,
native_session_id: Some("019f-session".into()),
source_cursor: None,
@@ -1963,6 +2239,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: prepared.run_id,
native_session_id: Some("a0d5ac62-2501-4780-b783-76d159c56cb3".into()),
source_cursor: None,
@@ -2085,6 +2362,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: codex.run_id,
native_session_id: Some("codex-native".into()),
source_cursor: None,
@@ -2272,6 +2550,30 @@ mod tests {
.await
.unwrap()
.expect("the run was just prepared");
+ store
+ .writer
+ .finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: state.sanitizer.clone(),
+ run_id: prepared.run_id,
+ native_session_id: Some("claude-native".into()),
+ source_cursor: None,
+ events: vec![NewWorkstreamEvent {
+ event_id: "scope-provenance".into(),
+ agent: AgentKind::ClaudeCode,
+ native_session_id: "claude-native".into(),
+ source_record_id: Some("record-1".into()),
+ kind: WorkstreamEventKind::ToolResult,
+ role: Some("tool".into()),
+ content: "scope provenance sentinel".into(),
+ occurred_at: None,
+ metadata: serde_json::json!({"tool_use_id": "call-1"}),
+ }],
+ complete: false,
+ segment_path: None,
+ exit_code: None,
+ })
+ .await
+ .unwrap();
let human = |name: &'static str| {
let writer = store.writer.clone();
@@ -2359,8 +2661,16 @@ mod tests {
Query(EventQuery::default()),
)
};
- assert_eq!(events(as_viewer(bob)).await.status(), StatusCode::FORBIDDEN);
- assert_eq!(events(as_viewer(carol)).await.status(), StatusCode::OK);
+ let refused = events(as_viewer(bob)).await;
+ assert_eq!(refused.status(), StatusCode::FORBIDDEN);
+ let body = to_bytes(refused.into_body(), 64 * 1024).await.unwrap();
+ assert!(!String::from_utf8_lossy(&body).contains("record-1"));
+ let allowed = events(as_viewer(carol)).await;
+ assert_eq!(allowed.status(), StatusCode::OK);
+ let body = to_bytes(allowed.into_body(), 64 * 1024).await.unwrap();
+ let provenance: serde_json::Value = serde_json::from_slice(&body).unwrap();
+ assert_eq!(provenance[0]["source_record_id"], "record-1");
+ assert_eq!(provenance[0]["metadata"]["tool_use_id"], "call-1");
// No viewer — an install with no database users, or root — is unchanged.
assert_eq!(
diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rs
index c4f07d39..b3eaf643 100644
--- a/crates/ai-memory-store/src/lib.rs
+++ b/crates/ai-memory-store/src/lib.rs
@@ -5016,6 +5016,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: first.run_id,
native_session_id: Some("native-1".into()),
source_cursor: None,
@@ -5060,6 +5061,7 @@ mod tests {
);
assert_eq!(status(second.run_id).await, (Some("native-1".into()), true));
let finish = |native: &str, complete: bool| FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: second.run_id,
native_session_id: Some(native.into()),
source_cursor: None,
@@ -5160,6 +5162,7 @@ mod tests {
metadata: serde_json::json!({}),
};
let partial = FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: run.run_id,
native_session_id: Some("native-1".into()),
source_cursor: None,
@@ -5200,6 +5203,7 @@ mod tests {
let finished = store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: run.run_id,
native_session_id: Some("native-1".into()),
source_cursor: Some("cursor-1".into()),
@@ -5213,7 +5217,12 @@ mod tests {
assert_eq!(finished.imported_events, 0);
let search = store
.reader
- .search_workstream_events(run.workstream_id, "portable".into(), 10)
+ .search_workstream_events(
+ run.workstream_id,
+ "portable".into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
assert_eq!(search.len(), 1);
@@ -5261,6 +5270,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: next.run_id,
native_session_id: Some("native-2".into()),
source_cursor: Some("cursor-2".into()),
@@ -5292,6 +5302,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: retry.run_id,
native_session_id: Some("native-3".into()),
source_cursor: Some("cursor-3".into()),
@@ -5403,6 +5414,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: blank.run_id,
native_session_id: None,
source_cursor: None,
@@ -5433,6 +5445,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: first.run_id,
native_session_id: Some("claude-native".into()),
source_cursor: None,
@@ -5490,6 +5503,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: claude.run_id,
native_session_id: Some("claude-current".into()),
source_cursor: Some("cursor".into()),
@@ -5554,6 +5568,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: run.run_id,
native_session_id: Some(format!("native-{workspace_name}")),
source_cursor: Some("cursor".into()),
@@ -5580,12 +5595,22 @@ mod tests {
assert_ne!(runs[0].workstream_id, runs[1].workstream_id);
let first = store
.reader
- .search_workstream_events(runs[0].workstream_id, "visible".into(), 10)
+ .search_workstream_events(
+ runs[0].workstream_id,
+ "visible".into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
let second = store
.reader
- .search_workstream_events(runs[1].workstream_id, "visible".into(), 10)
+ .search_workstream_events(
+ runs[1].workstream_id,
+ "visible".into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
assert_eq!(first.len(), 1);
@@ -5651,6 +5676,7 @@ mod tests {
events: Vec<NewWorkstreamEvent>,
) -> FinishWorkstreamRun {
FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id,
native_session_id: None,
source_cursor: None,
@@ -5809,6 +5835,7 @@ mod tests {
);
let finish = FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: run.run_id,
native_session_id: Some("native-1".into()),
source_cursor: Some("cursor-1".into()),
@@ -5844,7 +5871,12 @@ mod tests {
);
let events = store
.reader
- .search_workstream_events(run.workstream_id, String::new(), 10)
+ .search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
assert_eq!(
@@ -5906,6 +5938,7 @@ mod tests {
let wrong_agent = store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: run.run_id,
native_session_id: Some("native-1".into()),
source_cursor: None,
@@ -5926,7 +5959,12 @@ mod tests {
assert!(
store
.reader
- .search_workstream_events(run.workstream_id, String::new(), 10)
+ .search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 10,
+ ai_memory_core::Sanitizer::default()
+ )
.await
.unwrap()
.is_empty(),
@@ -5947,6 +5985,7 @@ mod tests {
let wrong_session = store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: run.run_id,
native_session_id: Some("native-1".into()),
source_cursor: None,
@@ -5971,6 +6010,7 @@ mod tests {
let finished = store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: run.run_id,
native_session_id: Some("native-1".into()),
source_cursor: None,
@@ -6357,14 +6397,24 @@ mod tests {
// An empty query tails the ledger, newest first.
let all = store
.reader
- .search_workstream_events(run.workstream_id, String::new(), 10)
+ .search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
assert_eq!(ids(all), ["ev-5", "ev-4", "ev-3", "ev-2", "ev-1"]);
let top_two = store
.reader
- .search_workstream_events(run.workstream_id, String::new(), 2)
+ .search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 2,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
assert_eq!(ids(top_two), ["ev-5", "ev-4"]);
@@ -6372,13 +6422,23 @@ mod tests {
// The limit is clamped into 1..=100.
let clamped_low = store
.reader
- .search_workstream_events(run.workstream_id, String::new(), 0)
+ .search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 0,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
assert_eq!(ids(clamped_low), ["ev-5"]);
let clamped_high = store
.reader
- .search_workstream_events(run.workstream_id, String::new(), 500)
+ .search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 500,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
assert_eq!(clamped_high.len(), 5);
@@ -6387,14 +6447,24 @@ mod tests {
// accepted by the search surface are stripped before matching.
let fts = store
.reader
- .search_workstream_events(run.workstream_id, "zebra".into(), 10)
+ .search_workstream_events(
+ run.workstream_id,
+ "zebra".into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
assert_eq!(ids(fts), ["ev-3"]);
for prefixed in ["title:zebra", "body:zebra", "content:zebra"] {
let hit = store
.reader
- .search_workstream_events(run.workstream_id, prefixed.into(), 10)
+ .search_workstream_events(
+ run.workstream_id,
+ prefixed.into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
.await
.unwrap();
assert_eq!(ids(hit), ["ev-3"], "prefix must be stripped: {prefixed}");
@@ -6552,6 +6622,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: current.run_id,
native_session_id: Some("codex-native".into()),
source_cursor: None,
@@ -6574,6 +6645,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: claude.run_id,
native_session_id: Some("claude-native".into()),
source_cursor: None,
@@ -6589,6 +6661,7 @@ mod tests {
store
.writer
.finish_workstream_run(FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
run_id: older.run_id,
native_session_id: Some("open-code-native".into()),
source_cursor: None,
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rs
index a2ce885f..9efa6d6e 100644
--- a/crates/ai-memory-store/src/reader.rs
+++ b/crates/ai-memory-store/src/reader.rs
@@ -1922,10 +1922,18 @@ impl ReaderPool {
workstream_id: WorkstreamId,
query: String,
limit: usize,
+ sanitizer: ai_memory_core::Sanitizer,
) -> StoreResult<Vec<WorkstreamEvent>> {
let stopwords = self.fts_stopwords.clone();
self.with_conn(move |conn| {
- crate::workstream::search_events(conn, workstream_id, &query, limit, &stopwords)
+ crate::workstream::search_events(
+ conn,
+ workstream_id,
+ &query,
+ limit,
+ &stopwords,
+ &sanitizer,
+ )
})
.await
}
diff --git a/crates/ai-memory-store/src/workstream.rs b/crates/ai-memory-store/src/workstream.rs
index f85a2c34..b8991b42 100644
--- a/crates/ai-memory-store/src/workstream.rs
+++ b/crates/ai-memory-store/src/workstream.rs
@@ -3,8 +3,8 @@
use std::str::FromStr as _;
use ai_memory_core::{
- AgentKind, ManagedRunId, NewWorkstreamEvent, ProjectId, WorkspaceId, WorkstreamEvent,
- WorkstreamEventKind, WorkstreamId,
+ AgentKind, ManagedRunId, NewWorkstreamEvent, ProjectId, Sanitizer, WorkspaceId,
+ WorkstreamEvent, WorkstreamEventKind, WorkstreamId, scrub_workstream_provenance,
};
use jiff::Timestamp;
use rusqlite::{Connection, OptionalExtension as _, Transaction, params};
@@ -116,6 +116,8 @@ pub struct LinkOrAdoptManagedRunSession {
/// Store-level finish input after the raw segment has been made durable.
#[derive(Debug, Clone)]
pub struct FinishWorkstreamRun {
+ /// Configured, reusable privacy strip for the provenance storage boundary.
+ pub sanitizer: Sanitizer,
/// Managed invocation.
pub run_id: ManagedRunId,
/// Actual native session, when observed.
@@ -906,25 +908,40 @@ pub(crate) fn finish_run(
event.event_id, event.native_session_id
)));
}
- let exists: bool = tx.query_row(
- "SELECT EXISTS(SELECT 1 FROM workstream_events \
- WHERE workstream_id = ?1 AND event_id = ?2)",
- params![workstream, event.event_id],
- |row| row.get(0),
- )?;
- if exists {
+ let existing: Option<(String, String, String)> = tx
+ .query_row(
+ "SELECT agent_kind, native_session_id, kind FROM workstream_events \
+ WHERE workstream_id = ?1 AND event_id = ?2",
+ params![workstream, event.event_id],
+ |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
+ )
+ .optional()?;
+ if let Some(existing) = existing {
+ // Privacy patterns and bounds can change between uploads. Replay
+ // keeps the first indexed data and compares only stable identity.
+ if existing.0 != event.agent.as_str()
+ || existing.1 != event.native_session_id
+ || existing.2 != event.kind.as_str()
+ {
+ return Err(StoreError::InvalidState(
+ "workstream event id reused with different identity".into(),
+ ));
+ }
continue;
}
+ let (source_record_id, metadata) = scrub_workstream_provenance(
+ &input.sanitizer,
+ event.source_record_id.as_deref(),
+ &event.metadata,
+ );
+ let metadata_json = serde_json::to_string(&metadata)?;
+ let content =
+ ai_memory_core::truncate_utf8_bytes(&event.content, WORKSTREAM_CONTENT_MAX_BYTES);
latest += 1;
// Store-boundary bound (defense in depth): the hook layer already
// scrubs and normalizes event content, but the store is the last gate
// before durable persistence — bound the free-text `content` so a
// caller that ever forgets cannot write unbounded prose to the DB.
- // `metadata_json` is left intact: it is structured JSON and
- // truncating the serialized form would corrupt it; its size is
- // bounded by the event schema, not free text.
- let content =
- ai_memory_core::truncate_utf8_bytes(&event.content, WORKSTREAM_CONTENT_MAX_BYTES);
tx.execute(
"INSERT INTO workstream_events( \
workstream_id, sequence, event_id, agent_kind, native_session_id, \
@@ -937,12 +954,12 @@ pub(crate) fn finish_run(
event.event_id,
event.agent.as_str(),
event.native_session_id,
- event.source_record_id,
+ source_record_id,
event.kind.as_str(),
event.role,
content,
event.occurred_at,
- serde_json::to_string(&event.metadata)?,
+ metadata_json,
input.segment_path,
now,
],
@@ -1305,6 +1322,8 @@ pub(crate) fn run_context(
event_id,
agent: AgentKind::from_wire(&agent),
native_session_id,
+ source_record_id: None,
+ metadata: serde_json::Value::Null,
kind: WorkstreamEventKind::from_str(&kind)?,
role,
content,
@@ -1332,6 +1351,7 @@ pub(crate) fn search_events(
query: &str,
limit: usize,
stopwords: &crate::fts_query::FtsStopwords,
+ sanitizer: &Sanitizer,
) -> StoreResult<Vec<WorkstreamEvent>> {
let limit = i64::try_from(limit.clamp(1, 100)).unwrap_or(100);
let free_text = query
@@ -1340,65 +1360,92 @@ pub(crate) fn search_events(
.replace("content:", "");
let fts_query = crate::prepare_fts5_query(&free_text, stopwords);
let sql = if fts_query.is_empty() {
- "SELECT sequence, event_id, agent_kind, native_session_id, kind, role, content, occurred_at \
+ "SELECT sequence, event_id, agent_kind, native_session_id, kind, role, content, occurred_at, \
+ source_record_id, metadata_json \
FROM workstream_events WHERE workstream_id = ?1 ORDER BY sequence DESC LIMIT ?2"
} else {
"SELECT e.sequence, e.event_id, e.agent_kind, e.native_session_id, e.kind, \
- e.role, e.content, e.occurred_at \
+ e.role, e.content, e.occurred_at, e.source_record_id, e.metadata_json \
FROM workstream_events_fts f \
JOIN workstream_events e ON e.rowid = f.rowid \
WHERE workstream_events_fts MATCH ?2 AND e.workstream_id = ?1 \
ORDER BY f.rank LIMIT ?3"
};
let mut statement = conn.prepare(sql)?;
- let read_row = |row: &rusqlite::Row<'_>| {
- Ok((
- row.get::<_, i64>(0)?,
- row.get::<_, String>(1)?,
- row.get::<_, String>(2)?,
- row.get::<_, String>(3)?,
- row.get::<_, String>(4)?,
- row.get::<_, Option<String>>(5)?,
- row.get::<_, String>(6)?,
- row.get::<_, Option<String>>(7)?,
- ))
- };
let mut events = Vec::new();
if fts_query.is_empty() {
- let rows = statement.query_map(params![workstream_id.as_bytes(), limit], read_row)?;
+ let rows = statement.query_map(params![workstream_id.as_bytes(), limit], read_event_row)?;
for row in rows {
- events.push(stored_event(row?)?);
+ events.push(stored_event(row?, sanitizer)?);
}
} else {
let rows = statement.query_map(
params![workstream_id.as_bytes(), fts_query, limit],
- read_row,
+ read_event_row,
)?;
for row in rows {
- events.push(stored_event(row?)?);
+ events.push(stored_event(row?, sanitizer)?);
}
}
Ok(events)
}
-fn stored_event(
- row: (
- i64,
- String,
- String,
- String,
- String,
- Option<String>,
- String,
- Option<String>,
- ),
-) -> StoreResult<WorkstreamEvent> {
- let (sequence, event_id, agent, native_session_id, kind, role, content, occurred_at) = row;
+type EventRow = (
+ i64,
+ String,
+ String,
+ String,
+ String,
+ Option<String>,
+ String,
+ Option<String>,
+ Option<String>,
+ String,
+);
+
+fn read_event_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<EventRow> {
+ Ok((
+ row.get(0)?,
+ row.get(1)?,
+ row.get(2)?,
+ row.get(3)?,
+ row.get(4)?,
+ row.get(5)?,
+ row.get(6)?,
+ row.get(7)?,
+ row.get(8)?,
+ row.get(9)?,
+ ))
+}
+
+fn stored_event(row: EventRow, sanitizer: &Sanitizer) -> StoreResult<WorkstreamEvent> {
+ let (
+ sequence,
+ event_id,
+ agent,
+ native_session_id,
+ kind,
+ role,
+ content,
+ occurred_at,
+ source,
+ metadata,
+ ) = row;
+ // Oversized or malformed legacy dumps carry no trusted correlation data.
+ let metadata = if metadata.len() <= 16 * 1024 {
+ serde_json::from_str(&metadata).unwrap_or(serde_json::Value::Null)
+ } else {
+ serde_json::Value::Null
+ };
+ let (source_record_id, metadata) =
+ scrub_workstream_provenance(sanitizer, source.as_deref(), &metadata);
Ok(WorkstreamEvent {
sequence,
event_id,
agent: AgentKind::from_wire(&agent),
native_session_id,
+ source_record_id,
+ metadata,
kind: WorkstreamEventKind::from_str(&kind)?,
role,
content,
diff --git a/crates/ai-memory-store/tests/suite/mod.rs b/crates/ai-memory-store/tests/suite/mod.rs
index 74fc492b..95d0e055 100644
--- a/crates/ai-memory-store/tests/suite/mod.rs
+++ b/crates/ai-memory-store/tests/suite/mod.rs
@@ -28,3 +28,4 @@ mod session_scope_from_observations;
mod sessions_by_agent;
mod slot_visibility;
mod stress_writer_throughput;
+mod workstream_provenance;
diff --git a/crates/ai-memory-store/tests/suite/workstream_provenance.rs b/crates/ai-memory-store/tests/suite/workstream_provenance.rs
new file mode 100644
index 00000000..73afd677
--- /dev/null
+++ b/crates/ai-memory-store/tests/suite/workstream_provenance.rs
@@ -0,0 +1,531 @@
+//! Provenance uses the configured privacy strip on writes and legacy reads.
+
+use ai_memory_core::{AgentKind, NewWorkstreamEvent, WorkstreamEventKind};
+use ai_memory_store::{FinishWorkstreamRun, PrepareWorkstreamRun, Store, WorkstreamSelection};
+use rusqlite::{Connection, params};
+use serde_json::{Value, json};
+
+fn event(id: &str, source: &str, metadata: Value) -> NewWorkstreamEvent {
+ NewWorkstreamEvent {
+ event_id: id.into(),
+ agent: AgentKind::Codex,
+ native_session_id: "native-1".into(),
+ source_record_id: Some(source.into()),
+ kind: WorkstreamEventKind::ToolResult,
+ role: Some("tool".into()),
+ content: "portable provenance sentinel".into(),
+ occurred_at: None,
+ metadata,
+ }
+}
+
+async fn prepare(store: &Store) -> ai_memory_store::PreparedWorkstreamRun {
+ let workspace_id = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .unwrap();
+ let project_id = store
+ .writer
+ .get_or_create_project(workspace_id, "managed", None)
+ .await
+ .unwrap();
+ store
+ .writer
+ .prepare_workstream_run(PrepareWorkstreamRun {
+ workspace_id,
+ project_id,
+ repo_fingerprint: "repo".into(),
+ worktree_fingerprint: "worktree".into(),
+ cwd: "/repo".into(),
+ agent: AgentKind::Codex,
+ automatic_harness: false,
+ available_agents: Vec::new(),
+ selection: WorkstreamSelection::Current,
+ lease_owner: "test".into(),
+ })
+ .await
+ .unwrap()
+}
+
+fn finish(
+ run_id: ai_memory_core::ManagedRunId,
+ events: Vec<NewWorkstreamEvent>,
+) -> FinishWorkstreamRun {
+ FinishWorkstreamRun {
+ sanitizer: ai_memory_core::Sanitizer::default(),
+ run_id,
+ native_session_id: Some("native-1".into()),
+ source_cursor: None,
+ events,
+ complete: false,
+ segment_path: None,
+ exit_code: None,
+ }
+}
+
+#[tokio::test]
+async fn workstream_provenance_is_bounded_scrubbed_and_idempotent_before_storage() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let run = prepare(&store).await;
+ // The credential crosses the cap: truncate-before-scrub would leak it.
+ let hostile = format!("{}sk-{}\u{1b}[31m", "é".repeat(250), "a".repeat(40));
+ let oversized = format!("{}-one", "é".repeat(600));
+ let batch = finish(
+ run.run_id,
+ vec![
+ event(
+ "hostile",
+ &hostile,
+ json!({
+ "tool": "Read", "tool_call_id": "call-1", "parent_id": "record-0",
+ "is_error": false, "status": "completed", "summary_type": "branch_summary",
+ "tool_use_id": "sk-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "exit_code": 0, "loss_count": 2,
+ "unknown": {"prompt": "private dump"}, "authority": "root",
+ }),
+ ),
+ event(
+ "oversized-one",
+ &oversized,
+ json!({"tool": ["private dump"]}),
+ ),
+ event(
+ "oversized-two",
+ &oversized.replace("-one", "-two"),
+ json!({}),
+ ),
+ event("legitimate", "record-1", json!({"tool_call_id": "call-1"})),
+ ],
+ );
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(batch.clone())
+ .await
+ .unwrap()
+ .imported_events,
+ 4
+ );
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(batch)
+ .await
+ .unwrap()
+ .imported_events,
+ 0
+ );
+ let conn = Connection::open(store.db_path()).unwrap();
+ let (source, metadata): (String, String) = conn.query_row(
+ "SELECT source_record_id, metadata_json FROM workstream_events WHERE event_id = 'hostile'",
+ [], |row| Ok((row.get(0)?, row.get(1)?)),
+ ).unwrap();
+ assert!(!source.contains("sk-"), "source id leaked a credential");
+ assert!(!source.contains('\u{1b}'));
+ assert!(source.len() <= 512);
+ let metadata: Value = serde_json::from_str(&metadata).unwrap();
+ assert!(metadata.get("unknown").is_none());
+ assert!(metadata.get("authority").is_none());
+ assert_eq!(metadata["tool"], "Read");
+ assert_eq!(metadata["tool_call_id"], "call-1");
+ assert_eq!(metadata["parent_id"], "record-0");
+ assert_eq!(metadata["summary_type"], "branch_summary");
+ assert_eq!(metadata["is_error"], false);
+ assert_eq!(metadata["status"], "completed");
+ assert_eq!(metadata["exit_code"], 0);
+ assert_eq!(metadata["loss_count"], 2);
+ assert!(!metadata["tool_use_id"].as_str().unwrap().contains("sk-"));
+ for query in ["", "portable"] {
+ let hits = store
+ .reader
+ .search_workstream_events(
+ run.workstream_id,
+ query.into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ hits.len(),
+ 4,
+ "distinct event ids must survive source-id truncation"
+ );
+ let hit = serde_json::to_value(
+ hits.iter()
+ .find(|hit| hit.event_id == "legitimate")
+ .unwrap(),
+ )
+ .unwrap();
+ assert_eq!(hit["source_record_id"], "record-1");
+ assert_eq!(hit["metadata"]["tool_call_id"], "call-1");
+ for hit in hits
+ .iter()
+ .filter(|hit| hit.event_id.starts_with("oversized"))
+ {
+ assert!(hit.source_record_id.as_ref().unwrap().len() <= 512);
+ assert!(hit.metadata.is_null());
+ }
+ }
+}
+
+#[tokio::test]
+async fn workstream_provenance_legacy_reads_strip_secrets_and_unknown_metadata() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let run = prepare(&store).await;
+ store
+ .writer
+ .finish_workstream_run(finish(
+ run.run_id,
+ vec![event("legacy", "record-1", json!({}))],
+ ))
+ .await
+ .unwrap();
+ // Simulate rows written before provenance was scrubbed at ingress.
+ let conn = Connection::open(store.db_path()).unwrap();
+ conn.execute("UPDATE workstream_events SET source_record_id = ?1, metadata_json = ?2 WHERE event_id = 'legacy'",
+ params!["sk-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", json!({"parent_id": "record-0", "tool_call_id": "sk-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "dump": "private"}).to_string()]).unwrap();
+ for query in ["", "portable"] {
+ let hits = store
+ .reader
+ .search_workstream_events(
+ run.workstream_id,
+ query.into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
+ .await
+ .unwrap();
+ let hit = serde_json::to_value(&hits[0]).unwrap();
+ assert!(
+ hit["source_record_id"].as_str().is_some(),
+ "legacy provenance must be returned safely"
+ );
+ assert!(!hit.to_string().contains("sk-"));
+ assert_eq!(hit["metadata"]["parent_id"], "record-0");
+ assert!(hit["metadata"].get("dump").is_none());
+ }
+ for malformed in ["not JSON".to_string(), "x".repeat(16 * 1024 + 1)] {
+ conn.execute(
+ "UPDATE workstream_events SET metadata_json = ?1 WHERE event_id = 'legacy'",
+ params![malformed],
+ )
+ .unwrap();
+ let hits = store
+ .reader
+ .search_workstream_events(
+ run.workstream_id,
+ "portable".into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
+ .await
+ .unwrap();
+ assert!(hits[0].metadata.is_null());
+ assert!(!hits[0].source_record_id.as_ref().unwrap().contains("sk-"));
+ }
+}
+
+#[tokio::test]
+async fn workstream_provenance_reused_event_id_retains_data_and_refuses_identity_change() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let run = prepare(&store).await;
+ let first = event("stable-id", "record-1", json!({"tool_call_id": "call-1"}));
+ store
+ .writer
+ .finish_workstream_run(finish(run.run_id, vec![first.clone()]))
+ .await
+ .unwrap();
+ let mut reused = first.clone();
+ reused.source_record_id = Some("record-2".into());
+ reused.content = "different source content".into();
+ reused.metadata = json!({"tool_call_id": "call-2"});
+ reused.role = Some("assistant".into());
+ reused.occurred_at = Some("2026-10-02T00:00:00Z".into());
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(finish(run.run_id, vec![reused.clone()]))
+ .await
+ .unwrap()
+ .imported_events,
+ 0
+ );
+ reused.kind = WorkstreamEventKind::Message;
+ assert!(matches!(
+ store
+ .writer
+ .finish_workstream_run(finish(
+ run.run_id,
+ vec![event("new-before-collision", "record-3", json!({})), reused,]
+ ))
+ .await,
+ Err(ai_memory_store::StoreError::InvalidState(_))
+ ));
+ let hits = store
+ .reader
+ .search_workstream_events(
+ run.workstream_id,
+ "".into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ hits.len(),
+ 1,
+ "the rejected batch must roll back earlier inserts"
+ );
+ assert_eq!(hits[0].content, first.content);
+ assert_eq!(hits[0].source_record_id, first.source_record_id);
+ assert_eq!(hits[0].metadata, first.metadata);
+ assert_eq!(hits[0].role, first.role);
+ assert_eq!(hits[0].occurred_at, first.occurred_at);
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(finish(run.run_id, vec![first]))
+ .await
+ .unwrap()
+ .imported_events,
+ 0
+ );
+}
+
+#[tokio::test]
+async fn workstream_provenance_finished_run_ignores_late_events() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let run = prepare(&store).await;
+ let first = event("stable-id", "record-1", json!({"tool_call_id": "call-1"}));
+ let mut batch = finish(run.run_id, vec![first]);
+ batch.complete = true;
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(batch.clone())
+ .await
+ .unwrap()
+ .imported_events,
+ 1
+ );
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(batch.clone())
+ .await
+ .unwrap()
+ .imported_events,
+ 0
+ );
+ batch.events[0].source_record_id = Some("record-2".into());
+ batch
+ .events
+ .push(event("late-new-event", "record-3", json!({})));
+ let result = store.writer.finish_workstream_run(batch).await.unwrap();
+ assert_eq!(result.imported_events, 0);
+ assert_eq!(result.latest_sequence, 1);
+ let hits = store
+ .reader
+ .search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(hits.len(), 1);
+ assert_eq!(hits[0].source_record_id.as_deref(), Some("record-1"));
+}
+
+#[tokio::test]
+async fn workstream_provenance_replay_survives_sanitizer_changes() {
+ let token = format!("sk-{}", "a".repeat(40));
+ for (source, old_config, new_config) in [
+ (
+ "private-correlation".to_string(),
+ ai_memory_core::SanitizeConfig {
+ extra_patterns: Vec::new(),
+ allowlist: Vec::new(),
+ },
+ ai_memory_core::SanitizeConfig {
+ extra_patterns: vec!["private-correlation".into()],
+ allowlist: Vec::new(),
+ },
+ ),
+ (
+ token.clone(),
+ ai_memory_core::SanitizeConfig {
+ extra_patterns: Vec::new(),
+ allowlist: vec![token],
+ },
+ ai_memory_core::SanitizeConfig {
+ extra_patterns: Vec::new(),
+ allowlist: Vec::new(),
+ },
+ ),
+ ] {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let run = prepare(&store).await;
+ let mut original = event("stable-id", &source, json!({"parent_id": source}));
+ original.content = format!("portable {source}");
+ let mut batch = finish(run.run_id, vec![original.clone()]);
+ batch.sanitizer = ai_memory_core::Sanitizer::new(&old_config).unwrap();
+ batch.events[0].content = batch.sanitizer.scrub(&original.content);
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(batch.clone())
+ .await
+ .unwrap()
+ .imported_events,
+ 1
+ );
+ let conn = Connection::open(store.db_path()).unwrap();
+ let read_raw = || {
+ conn.query_row(
+ "SELECT content, source_record_id, metadata_json FROM workstream_events WHERE event_id = 'stable-id'",
+ [], |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?, row.get::<_, String>(2)?)),
+ ).unwrap()
+ };
+ let first = read_raw();
+ batch.sanitizer = ai_memory_core::Sanitizer::new(&new_config).unwrap();
+ batch.events[0].content = batch.sanitizer.scrub(&original.content);
+ assert_ne!(batch.events[0].content, first.0);
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(batch.clone())
+ .await
+ .unwrap()
+ .imported_events,
+ 0
+ );
+ assert_eq!(
+ read_raw(),
+ first,
+ "replay must retain the first stored version"
+ );
+ let hits = store
+ .reader
+ .search_workstream_events(
+ run.workstream_id,
+ String::new(),
+ 10,
+ batch.sanitizer.clone(),
+ )
+ .await
+ .unwrap();
+ assert!(!hits[0].source_record_id.as_ref().unwrap().contains(&source));
+ assert!(!hits[0].metadata.to_string().contains(&source));
+ batch.events[0].kind = WorkstreamEventKind::Message;
+ assert!(matches!(
+ store.writer.finish_workstream_run(batch).await,
+ Err(ai_memory_store::StoreError::InvalidState(_))
+ ));
+ }
+}
+
+#[tokio::test]
+async fn workstream_provenance_replay_preserves_legacy_rows_above_bounds() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let run = prepare(&store).await;
+ let mut original = event(
+ "legacy",
+ &"s".repeat(2048),
+ json!({"parent_id": "p".repeat(2048), "dump": "x".repeat(20 * 1024)}),
+ );
+ original.content = "portable ".repeat(4096);
+ store
+ .writer
+ .finish_workstream_run(finish(run.run_id, vec![original.clone()]))
+ .await
+ .unwrap();
+ let conn = Connection::open(store.db_path()).unwrap();
+ // Legacy data can exceed today's write bounds; replay must not rewrite it.
+ conn.execute("UPDATE workstream_events SET content = ?1, source_record_id = ?2, metadata_json = ?3 WHERE event_id = 'legacy'",
+ params![original.content, original.source_record_id, original.metadata.to_string()]).unwrap();
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(finish(run.run_id, vec![original.clone()]))
+ .await
+ .unwrap()
+ .imported_events,
+ 0
+ );
+ let raw: (String, String, String) = conn.query_row(
+ "SELECT content, source_record_id, metadata_json FROM workstream_events WHERE event_id = 'legacy'",
+ [], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
+ ).unwrap();
+ assert_eq!(
+ raw,
+ (
+ original.content,
+ original.source_record_id.unwrap(),
+ original.metadata.to_string()
+ )
+ );
+ for query in ["", "portable"] {
+ let hits = store
+ .reader
+ .search_workstream_events(
+ run.workstream_id,
+ query.into(),
+ 10,
+ ai_memory_core::Sanitizer::default(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(hits.len(), 1);
+ assert_eq!(hits[0].source_record_id.as_ref().unwrap().len(), 512);
+ assert!(hits[0].metadata.is_null());
+ }
+}
+
+#[tokio::test]
+async fn workstream_provenance_retry_with_marker_matching_pattern_is_idempotent() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let run = prepare(&store).await;
+ let mut batch = finish(
+ run.run_id,
+ vec![event(
+ "stable-id",
+ "custom-source",
+ json!({"parent_id": "custom-parent"}),
+ )],
+ );
+ // Valid operator patterns can also match the scrubber's own markers.
+ batch.sanitizer = ai_memory_core::Sanitizer::new(&ai_memory_core::SanitizeConfig {
+ extra_patterns: vec!["custom".into()],
+ allowlist: Vec::new(),
+ })
+ .unwrap();
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(batch.clone())
+ .await
+ .unwrap()
+ .imported_events,
+ 1
+ );
+ assert_eq!(
+ store
+ .writer
+ .finish_workstream_run(batch)
+ .await
+ .unwrap()
+ .imported_events,
+ 0
+ );
+}
diff --git a/docs/managed-workstreams.md b/docs/managed-workstreams.md
index 1d4164ca..62675de3 100644
--- a/docs/managed-workstreams.md
+++ b/docs/managed-workstreams.md
@@ -419,6 +419,45 @@ another shell, pass `--workstream-id <uuid>` explicitly. Search results preserve
the source harness, role, event sequence, and content. Historical tool activity
is labelled completed evidence and must never be replayed as a pending call.
+Search and tail also return optional `source_record_id` and `metadata` fields.
+The text CLI prints the source record and correlation metadata as escaped JSON;
+`--json` preserves these fields for consumers. Older server responses without
+them remain readable. Startup context packets keep their existing format and
+omit these additional fields.
+
+Source labels and string metadata values are scrubbed with the server's
+configured sanitizer **before** the 512-byte UTF-8 cap. The metadata allowlist
+matches the shipped adapters: string `tool`, `tool_call_id`, `tool_use_id`,
+`parent_id`, `summary_type`, and `status`; boolean `is_error`; signed 32-bit
+`exit_code`; and unsigned 64-bit `loss_count`. Unknown keys, nested objects,
+arrays, and wrong scalar types are discarded. Empty metadata is omitted. These
+labels describe untrusted historical evidence; they cannot grant authority,
+choose a project, or prove a tool's success independently.
+
+The same scrub and allowlist protect legacy SQLite rows during search/tail,
+without rewriting them. Malformed metadata or legacy dumps over 16 KiB return
+no metadata. Existing raw segments are not rewritten by a read. The ledger's
+FTS query, ranking, ordering, and limits remain unchanged; provenance is fetched
+in the existing event query, without additional per-result SQL reads.
+
+`event_id` remains the deduplication key. Native adapters derive it from the
+original source before provenance redaction; truncating or redacting two source
+labels therefore cannot merge distinct event ids. Repeating an event is a
+no-op when its stable identity (`agent`, `native_session_id`, `kind`) matches.
+The first indexed content, role, timestamp and provenance remain unchanged,
+even if a later upload differs or the sanitizer's patterns have changed. This
+also permits replay of legacy rows above today's write bounds; read-time
+provenance sanitization still applies. Changing the stable identity is rejected
+and the SQL batch rolls back. A rejected immutable raw segment is not indexed;
+a corrected retry can import the valid batch without advancing history for the
+rejected one. Finished runs retain the existing no-op behavior for late events,
+including new event ids; their history and latest sequence do not advance.
+
+Client event ids beginning with `managed-run:` are refused before raw storage,
+including on retries of a finished run. This namespace belongs to the server's
+checkpoint and extraction-loss events, which it appends when completing an
+active run.
+
## Native adapter behavior
| Harness | Fresh native session | Returning native session | Read-only source |
diff --git a/docs/security-boundaries.md b/docs/security-boundaries.md
index 53774ace..6bf6e797 100644
--- a/docs/security-boundaries.md
+++ b/docs/security-boundaries.md
@@ -60,10 +60,11 @@ boundary not yet built.
| 11f | Native hook executable-path integrity after self-replacement (#1027) | `ai-memory-cli/src/commands/render_shared.rs` `hook_embedded_exe_path` is the single executable resolver for every persisted native hook form (POSIX/Windows command, Windows exec, Zero, ZCode, OpenCode 2 assistant capture): it prefers an existing literal path, strips Linux's trailing ` (deleted)` procfs marker only when the stripped installed binary exists, and otherwise falls back to the platform command name. Upgrade keeps the already-loaded config and refreshes in-process | `render_shared.rs` `embedded_exe_strips_the_marker_when_the_stripped_path_exists`, `embedded_exe_preserves_a_real_file_whose_name_has_the_marker_suffix`, `embedded_exe_falls_back_when_the_resolved_path_is_missing`, `hook_embedded_exe_path_names_an_existing_file_without_marker`; `upgrade_e2e.rs` `native_upgrade_refreshes_hooks_without_a_deleted_path_or_losing_cli_data_dir` reproduces the replacement window through the shipped binary, plants the corrupt suffix, and proves refresh removes it while preserving a CLI-only data dir | STRONG on Linux reproduction plus platform-neutral resolver/render coverage; Windows path rendering remains covered by its existing command/exec tests and full Windows CI |
| 11g | Cross-project file attribution cannot bypass destination capture or server routing (#932) | `ai-memory-hooks/src/capture_policy.rs` `absolute_file_tool_paths` accepts only bounded, recognized, absolute file-tool candidates; `ai-memory-cli/src/commands/hook.rs` `file_tool_routing_cwd` requires every path to prove the same repository/marker boundary and resolves that destination before capture policy, server profile, scope hints, or spool credentials. Relative, mixed-project, unsupported, and arbitrary non-project paths keep the payload cwd | `hook.rs` `absolute_file_target_uses_destination_scope_and_capture_policy` (hostile content under the destination exclusion is never spooled; a public control is routed there), `absolute_file_target_cannot_escape_a_destination_server_profile` (unregistered destination profile drops instead of falling back to source/install default), `file_target_routing_refuses_relative_external_and_mixed_project_paths` | STRONG for native `ai-memory hook`; generated and compatibility hooks retain cwd-only routing |
| 12 | Network/auth posture | `config.rs` loopback `DEFAULT_BIND`; `serve.rs` `validate_http_exposure`, `require_allowed_host`; `auth.rs` `require_bearer`; `serve.rs` mounts `/identity` inside the host and machine-auth gates even with web disabled; `nix/nixos-module.nix` treats only literal `127.0.0.1`/`::1` as loopback, requires exactly one secret environment source for other binds, keeps secret auth and `llm_headers` values out of the Nix store, and brackets IPv6 in `ExecStart` | `serve.rs` host-guard (missing→400 / forged→403), non-loopback-requires-token; `auth.rs` wrong-token 401; `serve.rs` `machine_identity_remains_authenticated_with_web_disabled_and_expired_keys` (wrong and expired keys refused, valid machine key accepted, web remains disabled); `flake.nix` `nixos-module-eval` refuses `localhost` and `0.0.0.0` without a secret source, refuses two secret sources, `settings.auth` secrets, and `settings.llm_headers`, with literal IPv4/IPv6 loopback and one-source controls | STRONG |
+| 12b | Managed-workstream provenance privacy, stable replay identity and server event namespace | `ai-memory-core/src/workstream.rs` `scrub_workstream_provenance`; `ai-memory-hooks/src/workstream.rs` `finish_run` reserved client-id check and `sanitize_events` before immutable segments; `ai-memory-store/src/workstream.rs` `finish_run` stable identity check and `stored_event`, using an explicitly supplied configured sanitizer; no changes to the typed observation boundary | `store/tests/suite/workstream_provenance.rs` `workstream_provenance_is_bounded_scrubbed_and_idempotent_before_storage` (hostile and oversized source ids, unknown metadata, legitimate correlation, retry and distinct event ids), `workstream_provenance_legacy_reads_strip_secrets_and_unknown_metadata` (search and tail, malformed/oversized legacy JSON), `workstream_provenance_reused_event_id_retains_data_and_refuses_identity_change` (first indexed version retained, identity refusal and batch rollback), `workstream_provenance_replay_survives_sanitizer_changes` (new extra patterns and removal of a built-in-pattern allowlist exemption, with identity-refusal control), `workstream_provenance_replay_preserves_legacy_rows_above_bounds`, `workstream_provenance_finished_run_ignores_late_events`, `workstream_provenance_retry_with_marker_matching_pattern_is_idempotent`; `hooks` `workstream_provenance_finish_scrubs_before_raw_segment_and_search` (custom sanitizer, immutable segment, SQL recovery and finished-run no-op), `workstream_provenance_client_cannot_use_server_event_ids` (reserved ids refused before raw writes, normal client event and server-generated checkpoint/loss controls); `cli/tests/suite/workstream_provenance.rs` (text/JSON and old server DTOs) | STRONG: privacy, stable-identity and reserved-namespace regressions fail with their guards bypassed; an isolated allowlist bypass fails on the unknown key, and bypassing the raw-id search authorization returns 200 where its test requires 403. No provenance is emitted in startup packets, and old raw segments are not rewritten |
| 13 | Managed-run transcript attribution (concurrent launches in one checkout, invariant #16) | `ai-memory-store/src/workstream.rs` `link_native_session` stamps `native_session_linked_at` on its own run only, both `finish` updates drop the stamp when the session changes, `run_status` reports it; `ai-memory-cli/src/commands/run.rs` `resolve_native_session_after_run` takes a linked session only when `ai-memory-workstream` `native_session_in_checkout` holds it for this checkout (OpenCode by recorded directory), never falls back to a link it set aside, and turns an `AmbiguousNativeSession` into a warning with nothing imported; `ai-memory-workstream/src/transcript.rs` `discover_crush` claims only the one top-level session created (or, with `--continue`, touched) during the run, and in a data directory outside the project only one that edited a file in it | `multi_session.rs` `a_session_linked_by_one_managed_run_is_not_another_runs`; `run.rs` `a_session_linked_during_the_run_wins_over_discovery` (concurrent newer session, another checkout's link refused, no fallback to it, unlinked control); `transcript.rs` `native_session_in_checkout_checks_the_opencode_directory`; `store/src/lib.rs` `managed_run_status_reports_a_link_made_during_the_run`; `run.rs` `ambiguous_crush_discovery_keeps_the_run`; `transcript.rs` `crush_discovery_claims_only_the_session_the_run_created`, `crush_discovery_in_a_shared_store_claims_only_an_edit_here` | PARTIAL: a run whose child links nothing still falls back to discovering the newest session in the checkout; Crush, which has no hooks, relies on that discovery alone and claims nothing when it is ambiguous |
| 13a | Managed Codex stale-daemon run recovery (#987; concurrent launches, project/worktree isolation, operator isolation) | `managed_runs.owner_user` (V71) uses the topology-aware `IdentityKey` owner stamp; `router.rs` resolves and authorizes the SessionStart repository before handling the untrusted run id; store `link_or_adopt_native_session` selects and links in one writer transaction and admits only the sole live, undelivered, never-linked Codex run in the exact workspace/project/checkout-cwd/owner bucket. An active supplied id must itself match every boundary; another harness, an ambiguity, or a second native session is refused | `multi_session.rs` `stale_codex_run_recovery_cannot_cross_project_owner_or_session_boundaries` (foreign owner, foreign checkout, and cross-project candidates refused; project-local controls and racing rebind), `stale_codex_run_recovery_fails_closed_on_ambiguity_and_active_mismatch` (two candidates and active foreign id refused, exact control); `workstream.rs` `prepare_stamps_the_operator_bucket_used_by_session_start_recovery` (HTTP actor stamp with foreign and legitimate controls); `router.rs` `managed_session_start_delivers_ledger_and_pending_handoff` supplies a finished stale id and proves the new run is linked and delivered | STRONG for SessionStart recovery. No candidate is selected from a run id's stale scope, another checkout, or another operator bucket; ambiguity deliberately falls back to no managed packet |
| 13b | Managed-run lease exclusivity and explicit recovery (one active run per workstream, invariant #16) | `ai-memory-store/src/workstream.rs` `prepare_run` expires lapsed leases and otherwise refuses an `active` run inside the writer transaction (`StoreError::WorkstreamBusy`). The explicit `force_unlock` path may expire and replace that row in the same transaction only when its durable `owner_user` equals the incoming actor's attribution; different operators remain isolated. `heartbeat` renews only `active` rows. `ai-memory-cli/src/commands/run.rs` normally uses `wait_out_held_lease`; `--force-unlock` instead asks once and fails immediately when refused. Neither path treats the informational `lease_owner` label as authority | `multi_session.rs` `force_unlock_replaces_only_the_same_operators_active_run` (foreign operator refused with live control, same operator succeeds, old run loses heartbeat, unattributed single-user control); hooks `workstream.rs` `force_unlock_cannot_evict_another_operator` exercises the HTTP actor boundary; CLI `run.rs` `force_unlock_asks_once_instead_of_waiting_out_the_lease`; existing controls `managed_workstream_batches_are_idempotent_and_release_the_lease`, `a_renewed_lease_is_reported_as_a_live_owner_not_taken_over`, `interactive_launch_waits_out_a_lapsing_lease_then_proceeds`, and `ctrl_c_aborts_the_held_lease_wait_immediately` | STRONG for exclusivity. The `lease_owner` label (`host:pid`) is informational only and not unique inside ai-jail (every jailed launcher reports `ai-sandbox:<ns-pid>`), so it must never become an ownership key. A shared unattributed/root context is intentionally one operator bucket; deployments needing person-level refusal must authenticate DB users |
-| 14 | Per-project authorization (#708) | `ai-memory-store/src/project_authz.rs` `authorize_project` / `ProjectAuthz::authorize` choke point (V68 `project_grants` + `projects.access_mode`, default `open`; V69 `projects.created_by` feeds `is_creator`); `scope.rs` `ScopeResolver::with_project_authz` (reader pool for reads, writer actor for writes) and its free forms `authorize_scope_for` / `*_guarded`, attached for every DB user by `ai-memory-mcp` `scope_resolver_as`, `ai-memory-web` `authorize_read` / `lookup_project`, and `ai-memory-hooks` (`grants.rs` `authorize_resolved` for run/workstream ids, the capture check in `router.rs`); read-shaped mutations resolve at `ProjectAccess::Write` (`resolve_existing_args(.., need)`); unscoped reads filtered before `LIMIT` by `reader.rs` `readable_repository_sql`; `WriterHandle::authorize_project` as defense in depth. Page ids are never taken from a caller (only derived from already-authorized hits), so there is no page-id entry point to guard; incremental `recent` resolves and authorizes the requested project before decoding its scope-bound cursor; batch outcomes do not bypass the capture grant or session owner check | `tests/suite/project_authz.rs` (decision matrix, ship-inert, resolver gate); `tests/suite/access_mode.rs` `every_caller_against_both_modes`, `a_restricted_project_admits_the_team_and_refuses_the_outsider`, `new_projects_follow_the_server_default_and_admit_their_creator`; `scope.rs` `the_argument_shape_does_not_decide_the_level`, `a_user_reaches_only_what_they_were_granted`, `creating_authorizes_against_a_project_that_already_exists`, `a_refused_scope_fails_the_search_instead_of_shortening_it`; `grants.rs` `search_finds_only_what_the_viewer_may_read`, `the_limit_counts_only_what_the_viewer_may_see`, `the_workspace_handoff_comes_only_from_readable_repositories`; `ai-memory-mcp` `server.rs` `a_reader_may_read_everything_and_change_nothing`, `bob_cannot_read_alices_page_in_a_restricted_project`, `bob_cannot_find_alices_page_by_searching_in_a_restricted_project`, `bob_cannot_consolidate_a_session_in_alices_repository`, `a_restricted_projects_queues_need_write`; `ai-memory-hooks` `a_capture_needs_writer_on_the_repository_it_lands_in`, `session_start_delivers_nothing_from_a_repository_the_viewer_cannot_read`, `run_and_workstream_ids_only_answer_someone_who_may_reach_the_repository`; `ai-memory-web` `web_reads_honour_grants_in_a_restricted_project`, `metadata_shows_only_what_the_viewer_may_read` — each with a granted or open-project control, and proven to fail with the choke point (18 tests) or the SQL filter (9 tests) neutralized; `routes.rs` `api_recent_incremental_rechecks_restricted_scope_on_cursor` (a valid cursor cannot bypass a revoked grant; granted control passes), `api_recent_incremental_paginates_over_120_pages_and_binds_cursor`; `mcp_stateless_http.rs` `generic_machine_client_writes_queries_and_claims_a_handoff` (real machine key, foreign scope and partial scope refused); `router.rs` `an_unauthorized_batch_item_is_consumed_rather_than_retried`, `batch_acknowledges_foreign_collision_then_commits_next_item` (refused foreign item followed by a legitimate stored item) | STRONG — slice 3 closed both bypass classes (unscoped reads, raw-id entry points) and added the root-only management surface. Out of scope by design: per-project administrators (granting/restricting is root-only), and access modes, creators and grants live only in SQLite, so `reindex` resets them |
+| 14 | Per-project authorization (#708) | `ai-memory-store/src/project_authz.rs` `authorize_project` / `ProjectAuthz::authorize` choke point (V68 `project_grants` + `projects.access_mode`, default `open`; V69 `projects.created_by` feeds `is_creator`); `scope.rs` `ScopeResolver::with_project_authz` (reader pool for reads, writer actor for writes) and its free forms `authorize_scope_for` / `*_guarded`, attached for every DB user by `ai-memory-mcp` `scope_resolver_as`, `ai-memory-web` `authorize_read` / `lookup_project`, and `ai-memory-hooks` (`grants.rs` `authorize_resolved` for run/workstream ids, the capture check in `router.rs`); read-shaped mutations resolve at `ProjectAccess::Write` (`resolve_existing_args(.., need)`); unscoped reads filtered before `LIMIT` by `reader.rs` `readable_repository_sql`; `WriterHandle::authorize_project` as defense in depth. Page ids are never taken from a caller (only derived from already-authorized hits), so there is no page-id entry point to guard; incremental `recent` resolves and authorizes the requested project before decoding its scope-bound cursor; batch outcomes do not bypass the capture grant or session owner check | `tests/suite/project_authz.rs` (decision matrix, ship-inert, resolver gate); `tests/suite/access_mode.rs` `every_caller_against_both_modes`, `a_restricted_project_admits_the_team_and_refuses_the_outsider`, `new_projects_follow_the_server_default_and_admit_their_creator`; `scope.rs` `the_argument_shape_does_not_decide_the_level`, `a_user_reaches_only_what_they_were_granted`, `creating_authorizes_against_a_project_that_already_exists`, `a_refused_scope_fails_the_search_instead_of_shortening_it`; `grants.rs` `search_finds_only_what_the_viewer_may_read`, `the_limit_counts_only_what_the_viewer_may_see`, `the_workspace_handoff_comes_only_from_readable_repositories`; `ai-memory-mcp` `server.rs` `a_reader_may_read_everything_and_change_nothing`, `bob_cannot_read_alices_page_in_a_restricted_project`, `bob_cannot_find_alices_page_by_searching_in_a_restricted_project`, `bob_cannot_consolidate_a_session_in_alices_repository`, `a_restricted_projects_queues_need_write`; `ai-memory-hooks` `a_capture_needs_writer_on_the_repository_it_lands_in`, `session_start_delivers_nothing_from_a_repository_the_viewer_cannot_read`, `run_and_workstream_ids_only_answer_someone_who_may_reach_the_repository` (foreign raw workstream id refuses populated provenance; granted reader receives the source record and tool correlation); `ai-memory-web` `web_reads_honour_grants_in_a_restricted_project`, `metadata_shows_only_what_the_viewer_may_read` — each with a granted or open-project control, and proven to fail with the choke point (18 tests) or the SQL filter (9 tests) neutralized; `routes.rs` `api_recent_incremental_rechecks_restricted_scope_on_cursor` (a valid cursor cannot bypass a revoked grant; granted control passes), `api_recent_incremental_paginates_over_120_pages_and_binds_cursor`; `mcp_stateless_http.rs` `generic_machine_client_writes_queries_and_claims_a_handoff` (real machine key, foreign scope and partial scope refused); `router.rs` `an_unauthorized_batch_item_is_consumed_rather_than_retried`, `batch_acknowledges_foreign_collision_then_commits_next_item` (refused foreign item followed by a legitimate stored item) | STRONG — slice 3 closed both bypass classes (unscoped reads, raw-id entry points) and added the root-only management surface. Out of scope by design: per-project administrators (granting/restricting is root-only), and access modes, creators and grants live only in SQLite, so `reindex` resets them |
| 14b | Repository identity routing (#708) | `ai-memory-store/src/ops.rs` `resolve_project_by_identity` — one transaction; an identity already on a project is never overwritten; an unclaimed project is claimed only when the capturing user may write it (the choke point's `resolve_project_authz` on the same transaction), otherwise it is returned unclaimed; a different identity under the same name splits into a new project with no shared `repo_path`; `ai-memory-hooks/src/router.rs` `cache_key_for` keys the path cache by identity too; `ai-memory-core/src/repository_identity.rs` strips credentials from remote URLs client-side, and the server accepts only the routing rungs (`explicit`, `git_remote`) from the wire | `tests/suite/identity_resolution.rs` `an_outsider_cannot_take_an_unclaimed_projects_identity` (control: `an_existing_project_is_claimed_in_place`), `two_unrelated_repositories_with_one_folder_name_stay_apart`, `a_created_or_split_project_admits_its_creator`; `router.rs` `one_path_with_two_remotes_is_two_projects`, `two_api_checkouts_with_different_remotes_get_two_projects` (a manifest rung on the wire is ignored); `repository_identity.rs` credential and normalisation tables — the claim guard and the cache key each proven to fail their test when removed | STRONG — first claimant wins: in a restricted workspace a user who creates a project under a remote identity first holds it until root grants others |
| 14c | Client-supplied event time is bounded and self-scoped (#919) | `ai-memory-hooks/src/payload.rs` `HookEnvelope::occurred_at_micros` — a `/hook` caller's optional `occurred_at` must be `> 0` and `<= now + 5min`; it only sets the caller's *own* admitted session's `started_at`/`ended_at`/`created_at`, never another session/project/user, and the ingest dedup `seen_at`/TTL stays on `now` | `payload.rs` `occurred_at_micros_rejects_a_far_future_timestamp`, `…_rejects_non_positive_values`, `…_rejects_garbage_strings` | MEDIUM — self-scoped numeric bound; no cross-tenant surface (a client already controls its own content) |
| 14d | `include_related` shows and walks only readable projects (#708) | `ai-memory-store/src/reader.rs` `related_walk(.., viewer)` — both the outgoing and incoming hop queries carry `readable_repository_predicate("pg.project_id", viewer)`, the same far-end filter `page_links` applies, so a page in a project the viewer cannot read is neither returned nor expanded; `ai-memory-mcp/src/server.rs` `memory_read_page` passes `viewer_from_parts`, and the access bump only touches the filtered nodes | `related_walk.rs` `the_walk_hides_and_does_not_cross_projects_the_viewer_cannot_read` (an outgoing and an incoming neighbour in an ungranted restricted project are absent, and so is a granted page reachable only through one; control: a granted restricted project is shown, and no viewer walks the whole graph) — fails with the predicate removed; `ai-memory-mcp` `server.rs` `bob_does_not_see_restricted_pages_through_the_related_walk` (an ungranted user's `memory_read_page{include_related}` omits the restricted page; control: no viewer still reaches it) — fails with `memory_read_page` passing `None` instead of `viewer_from_parts` | STRONG |