provenance: track agent phase, use task_local + thread_local

Split TASK_PROVENANCE into TASK_AGENT (task_local, set once per agent
run) and TASK_PHASE (thread_local, updated between steps). Provenance
now reports "agent:surface-observe:observe" instead of just
"agent:surface-observe", making it possible to identify which pipeline
phase created a node.

Priority: task_local agent + thread_local phase > POC_PROVENANCE env
var > "manual".

Also includes memory_search catchup throttle and pipelining fixes
from the surface-observe refactor.
This commit is contained in:
ProofOfConcept 2026-03-27 15:11:17 -04:00
parent b1efdf0b9a
commit 85302c11d4
5 changed files with 79 additions and 53 deletions

View file

@ -37,7 +37,7 @@ pub use parse::{MemoryUnit, parse_units};
pub use view::{StoreView, AnyView};
pub use persist::fsck;
pub use persist::strip_md_keys;
pub use ops::TASK_PROVENANCE;
pub use ops::{TASK_AGENT, set_phase};
use crate::graph::{self, Graph};

View file

@ -8,17 +8,33 @@ use super::types::*;
use std::collections::{HashMap, HashSet};
tokio::task_local! {
/// Task-scoped provenance for agent writes. Set by the daemon before
/// running an agent's tool calls, so all writes within that task are
/// automatically attributed to the agent.
pub static TASK_PROVENANCE: String;
/// Task-scoped agent name for provenance. Set before running an agent's
/// tool calls, so all writes within that task are attributed to the agent.
pub static TASK_AGENT: String;
}
/// Provenance priority: task_local (agent context) > env var > "manual".
fn current_provenance() -> String {
TASK_PROVENANCE.try_with(|p| p.clone())
.or_else(|_| std::env::var("POC_PROVENANCE").map_err(|_| ()))
.unwrap_or_else(|_| "manual".to_string())
thread_local! {
/// Current phase within a multi-step agent. Updated by the bail function
/// between steps. Combined with TASK_AGENT to form the full provenance.
static TASK_PHASE: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
}
/// Set the current phase (called from bail function between steps).
pub fn set_phase(phase: &str) {
TASK_PHASE.with(|p| *p.borrow_mut() = Some(phase.to_string()));
}
/// Get the full provenance string: "agent:phase" or "agent" or env var or "manual".
pub fn current_provenance() -> String {
let agent = TASK_AGENT.try_with(|a| a.clone()).ok();
let phase = TASK_PHASE.with(|p| p.borrow().clone());
match (agent, phase) {
(Some(a), Some(p)) => format!("{}:{}", a, p),
(Some(a), None) => a,
_ => std::env::var("POC_PROVENANCE")
.unwrap_or_else(|_| "manual".to_string()),
}
}
impl Store {