diff --git a/crates/dim-agent-capabilities/src/context_usage.rs b/crates/dim-agent-capabilities/src/context_usage.rs index c0a446a053..de986e65f5 100644 --- a/crates/dim-agent-capabilities/src/context_usage.rs +++ b/crates/dim-agent-capabilities/src/context_usage.rs @@ -1044,6 +1044,49 @@ impl ContextUsage { reading(through, &summary, None) } + /// The position of the last model request at or before `through`. + async fn request_position(&self, through: u64) -> Result> { + let reply = self + .timeline + .query(TimelineQuery { + scope: TimelineQueryScope::Branch, + from: None, + through: Some(through), + include_publishing: false, + query: QuerySpec { + filter: Some(QueryFilter::In { + value: QueryOperand::Field { + path: "kind".into(), + }, + values: vec![serde_json::json!("model/call")], + }), + select: QuerySelect::Fields { + fields: vec!["position.sequence".into()], + }, + group_by: vec![], + pick: None, + order_by: vec![QueryOrder { + key: QueryOrderKey::Position, + direction: TimelineQueryDirection::Descending, + }], + }, + page: Some(QueryPage { + limit: NonZeroU64::MIN, + after: None, + }), + }) + .await?; + Ok(match &reply.result { + QueryOutput::Rows { rows } => { + rows.first().and_then(|row| match row.values.as_slice() { + [QueryValue::Value { value }] => value.as_u64(), + _ => None, + }) + } + _ => None, + }) + } + /// The tools the last model request at or before `through` declared. async fn request_tools(&self, through: u64) -> Result>> { let reply = self @@ -1105,17 +1148,15 @@ impl ContextUsage { .map(|source| (&source.key, &source.version)) .collect::>(), )?; - let tools = self - .request_tools(through) - .await? - .unwrap_or_else(|| self.registry.visible_definitions()); - let tools_fingerprint = fingerprint( - &tools - .iter() - .map(|tool| (&tool.name, &tool.description)) - .collect::>(), - )?; - let versions = format!("{versions}:{tools_fingerprint}"); + let (identity, cached_tools) = match self.request_position(through).await? { + Some(sequence) => (format!("request:{sequence}"), None), + None => { + let tools = self.registry.visible_definitions(); + let identity = format!("live:{}", fingerprint(&tools)?); + (identity, Some(tools)) + } + }; + let versions = format!("{versions}:{identity}"); if let Some(cached) = self .dynamic_fixed .lock() @@ -1127,13 +1168,17 @@ impl ContextUsage { { return Ok(cached.clone()); } + let tools = match cached_tools { + Some(tools) => Some(tools), + None => self.request_tools(through).await?, + }; let mut system = self .fields(&configuration.event_id, through, &["data.system"]) .await?; let system: Option = sources::optional(system.remove(0))?; let identity = fingerprint(&( &configuration.control_identity, - fingerprint(&(&system, &Some(&tools)))?, + fingerprint(&(&system, &tools))?, ))?; let previous = self .dynamic_fixed @@ -1148,7 +1193,7 @@ impl ContextUsage { .contribution_counter() .filter(|counter| counter.algorithm() == configuration.algorithm) .ok_or_else(|| fault("context counter algorithm changed"))?; - let fixed = previous.unwrap_or_else(|| counter.fixed(system.as_ref(), Some(&tools))); + let fixed = previous.unwrap_or_else(|| counter.fixed(system.as_ref(), tools.as_deref())); let value = DynamicFixed { configuration: configuration.event_id.clone(), versions, diff --git a/crates/dim-agent-capabilities/tests/context_flow/context_reads.rs b/crates/dim-agent-capabilities/tests/context_flow/context_reads.rs index a9c37a3203..557018c9f5 100644 --- a/crates/dim-agent-capabilities/tests/context_flow/context_reads.rs +++ b/crates/dim-agent-capabilities/tests/context_flow/context_reads.rs @@ -5706,3 +5706,243 @@ async fn model_only_reload_hands_off_a_running_preparation_at_its_recovery_cut() .unwrap_or_else(|_| panic!("ModelProvider-only {window} handoff timed out")); } } + +struct SchemaSource { + sets: std::sync::Mutex>>, + emitter: Option<(Ctx, rutis::host::InstanceId)>, +} +impl PublicService for SchemaSource {} +impl SchemaSource { + fn shared() -> Arc { + Arc::new(Self { + sets: std::sync::Mutex::default(), + emitter: None, + }) + } + fn offered(&self, sets: Vec>) { + let (ctx, session) = self + .emitter + .as_ref() + .expect("offered after the source started"); + *self.sets.lock().unwrap() = sets; + ctx.emit(dim_agent_capabilities::tool_registry::ToolProviderChanged { + name: dim_agent_capabilities::tool_registry::tool_provider_changes(*session), + }) + .unwrap(); + } +} +impl dim_agent_capabilities::tool_registry::ToolProvider for SchemaSource { + fn sets(&self) -> Vec> { + self.sets.lock().unwrap().clone() + } +} + +struct SchemaHandler; +impl dim_agent_capabilities::tool_registry::ToolHandler for SchemaHandler { + fn execute( + &self, + _: ToolCall, + _: ToolExecutionContext, + _: CancellationToken, + ) -> HostFuture<'_, ToolResult> { + Box::pin(async { Ok(ToolResult::text("ok")) }) + } +} + +fn schema_set( + version: &str, + schema: serde_json::Value, +) -> Arc { + Arc::new(dim_agent_capabilities::tool_registry::ToolSourceSet { + key: "schema".into(), + version: version.into(), + tools: vec![Arc::new( + dim_agent_capabilities::tool_registry::ToolContribution { + definition: dim_agent_capabilities::tool_registry::definition( + "report", + "Report a result.", + schema, + ), + handler: Arc::new(SchemaHandler), + }, + )], + }) +} + +struct SchemaPlugin { + session: rutis::host::InstanceId, + key: Key, + tools: Key, + source: Arc, +} +impl DynPlugin for SchemaPlugin { + fn name(&self) -> &str { + "SchemaSource" + } + fn injects(&self) -> Vec { + vec![self.tools.erase()] + } + fn apply<'a>(&'a self, ctx: &'a Ctx) -> BoxFuture<'a, rutis::Result> { + Box::pin(async move { + let tools = ctx.require_at(&self.tools)?; + let source = Arc::new(SchemaSource { + sets: std::sync::Mutex::new(std::mem::take(&mut *self.source.sets.lock().unwrap())), + emitter: Some((ctx.clone(), self.session)), + }); + ctx.provide_at(self.key.clone(), source.clone())?; + tools.register_source(ctx, "schema", source)?; + Ok(Effect::Done) + }) + } +} + +fn add_schema_source(builder: &mut AgentBuilder, source: Arc) { + names::register::(builder.catalog_mut(), Some(SESSION_GROUP)); + builder.require_feature( + Feature::new("SchemaSource").session("SchemaSource", move |names| { + Ok(Box::new(SchemaPlugin { + session: names.instance(SESSION_GROUP)?, + key: names.key()?, + tools: names.key()?, + source: source.clone(), + })) + }), + ); +} + +#[tokio::test] +async fn usage_reuses_a_warm_request_and_prices_a_changed_tool_schema() { + use dim_agent_capabilities::context_preparation::ContextPreparation; + use dim_agent_capabilities::tool_registry::ToolSourcesView; + let counter = Arc::new(ObservedCounter::default()); + let source = SchemaSource::shared(); + source + .sets + .lock() + .unwrap() + .push(schema_set("1", serde_json::json!({"type": "object"}))); + let mut profile = builder_with_usage_resolver(counter.clone()); + add_schema_source(&mut profile, source.clone()); + let cell = Arc::new(std::sync::OnceLock::new()); + let capture = cell.clone(); + profile.require_feature(Feature::new("test.raw-timeline").branch( + "test.raw-timeline", + move |names| { + Ok(Box::new(super::manual_compaction::RawTimeline::new( + names, + capture.clone(), + )?)) + }, + )); + let agent = MinimalHost::start(&profile).await.unwrap(); + let session = agent + .create_session(SessionConfig::default(), Default::default()) + .await + .unwrap(); + let timeline = cell.get().unwrap(); + timeline + .append( + EventKind::ContextConfigured(ContextConfiguredData { + timeline: "main".into(), + revision: "fixture".into(), + previous: None, + prompt: None, + settings: Box::new(ModelConfiguration { + provider_id: "fixture".into(), + model_id: "counter".into(), + capability_snapshot: None, + tools: None, + tool_sources: vec![], + config: None, + metadata: None, + }), + system: None, + system_source: None, + }) + .into(), + ) + .unwrap(); + let preparation = session.service::("main").unwrap(); + session + .submit("bootstrap request".into(), InputMode::Queue, "main") + .await + .unwrap(); + session.idle("main").await.unwrap(); + let first = preparation.usage(CancellationToken::new()).await.unwrap(); + let counted = counter.fixed.load(Ordering::SeqCst); + let bytes = counter.fixed_value.lock().unwrap().0; + assert!( + counter + .fixed_value + .lock() + .unwrap() + .1 + .iter() + .any(|name| name == "report"), + "the request declares the source tool" + ); + for _ in 0..2 { + assert_eq!( + preparation + .usage(CancellationToken::new()) + .await + .unwrap() + .estimated_input_tokens, + first.estimated_input_tokens + ); + } + assert_eq!(counter.fixed.load(Ordering::SeqCst), counted); + assert_eq!(counter.fixed_value.lock().unwrap().0, bytes); + session + .session_service::() + .unwrap() + .offered(vec![schema_set( + "2", + serde_json::json!({ + "type": "object", + "properties": { + "query": {"type": "string"}, + "limit": {"type": "integer"}, + "flags": {"type": "array", "items": {"type": "string"}}, + } + }), + )]); + let view = session.session_service::().unwrap(); + tokio::time::timeout(Duration::from_secs(5), async { + loop { + if view + .get() + .0 + .get("schema") + .and_then(|status| status.adopted.as_deref()) + == Some("2") + { + break; + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + }) + .await + .expect("source v2 visible"); + session + .submit("second".into(), InputMode::Queue, "main") + .await + .unwrap(); + session.idle("main").await.unwrap(); + let after = preparation.usage(CancellationToken::new()).await.unwrap(); + assert!( + after.estimated_input_tokens > first.estimated_input_tokens, + "the schema change is priced by the next request" + ); + let changed = counter.fixed_value.lock().unwrap().0; + assert!( + changed > bytes, + "the fixed contribution counts the new schema" + ); + let priced = counter.fixed.load(Ordering::SeqCst); + assert!(priced > counted, "the changed schema is counted again"); + let settled = preparation.usage(CancellationToken::new()).await.unwrap(); + assert_eq!(settled.estimated_input_tokens, after.estimated_input_tokens); + assert_eq!(counter.fixed.load(Ordering::SeqCst), priced); + agent.close().await.unwrap(); +}