mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
## What changed - Add an optional `ExtensionMetrics` capability to thread, turn-input, and world-state contributors, backed by session telemetry in the host. - Record skill counts, omissions, and truncated description characters for extension-rendered catalogs, tagged by the catalog surface. - Preserve host session attribution on extension metrics, including the effective model for each turn, and prevent extension tags from overriding host metadata. - Avoid duplicate host-catalog samples by recording world-state metrics only when the section is published or changes. ## Testing - Cover metric values and surface tags, session metadata preservation, turn-level model changes, and host world-state publication behavior. GitOrigin-RevId: 9059e521943a8f81df25a38b03692eddbc1d990d
153 lines
4.5 KiB
Rust
153 lines
4.5 KiB
Rust
use std::sync::Arc;
|
|
|
|
use codex_exec_server_protocol::ExecutorCapabilityDiscoverySnapshot;
|
|
use codex_protocol::ThreadId;
|
|
use codex_protocol::capabilities::SelectedCapabilityRoot;
|
|
use codex_protocol::protocol::TurnEnvironmentSelection;
|
|
use serde_json::Value;
|
|
|
|
use crate::ExtensionData;
|
|
use crate::ExtensionMetrics;
|
|
|
|
/// Host state available while an extension contributes one sampling step's World State.
|
|
pub struct WorldStateContributionInput<'a> {
|
|
pub thread_id: ThreadId,
|
|
pub turn_id: &'a str,
|
|
pub environments: &'a [TurnEnvironmentSelection],
|
|
/// Selected roots whose stable environments are ready in this sampling step.
|
|
pub ready_selected_capability_roots: &'a [SelectedCapabilityRoot],
|
|
/// Executor-materialized capability files shared by all consumers in this exact step.
|
|
pub executor_capability_discovery: Option<&'a ExecutorCapabilityDiscoverySnapshot>,
|
|
/// Metrics bound to the effective model for this turn.
|
|
pub extension_metrics: Option<Arc<dyn ExtensionMetrics>>,
|
|
pub session_store: &'a ExtensionData,
|
|
pub thread_store: &'a ExtensionData,
|
|
pub turn_store: &'a ExtensionData,
|
|
}
|
|
|
|
/// What the harness knows about the previous value of one extension-owned section.
|
|
pub enum PreviousWorldStateSection<'a> {
|
|
Absent,
|
|
Unknown,
|
|
Known(&'a Value),
|
|
}
|
|
|
|
/// Plain model-visible data rendered by an extension-owned World State section.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct RenderedWorldStateFragment {
|
|
role: &'static str,
|
|
markers: (&'static str, &'static str),
|
|
body: String,
|
|
}
|
|
|
|
impl RenderedWorldStateFragment {
|
|
pub fn new(
|
|
role: &'static str,
|
|
markers: (&'static str, &'static str),
|
|
body: impl Into<String>,
|
|
) -> Self {
|
|
Self {
|
|
role,
|
|
markers,
|
|
body: body.into(),
|
|
}
|
|
}
|
|
|
|
pub fn role(&self) -> &'static str {
|
|
self.role
|
|
}
|
|
|
|
pub fn markers(&self) -> (&'static str, &'static str) {
|
|
self.markers
|
|
}
|
|
|
|
pub fn body(&self) -> &str {
|
|
&self.body
|
|
}
|
|
}
|
|
|
|
type RenderDiff = dyn for<'a> Fn(PreviousWorldStateSection<'a>) -> Option<RenderedWorldStateFragment>
|
|
+ Send
|
|
+ Sync;
|
|
type LegacyFragmentMatcher = dyn Fn(&str, &str) -> bool + Send + Sync;
|
|
|
|
/// One extension-owned World State section captured for a sampling step.
|
|
///
|
|
/// The extension owns the stable ID, comparison snapshot, and diff rendering. The harness owns
|
|
/// persistence and the concrete model-context fragment envelope.
|
|
#[derive(Clone)]
|
|
pub struct WorldStateSectionContribution {
|
|
id: &'static str,
|
|
snapshot: Value,
|
|
render_diff: Arc<RenderDiff>,
|
|
matches_legacy_fragment: Arc<LegacyFragmentMatcher>,
|
|
matches_retained_fragment: Option<Arc<LegacyFragmentMatcher>>,
|
|
}
|
|
|
|
impl WorldStateSectionContribution {
|
|
pub fn new(
|
|
id: &'static str,
|
|
snapshot: Value,
|
|
render_diff: impl for<'a> Fn(
|
|
PreviousWorldStateSection<'a>,
|
|
) -> Option<RenderedWorldStateFragment>
|
|
+ Send
|
|
+ Sync
|
|
+ 'static,
|
|
) -> Self {
|
|
Self {
|
|
id,
|
|
snapshot,
|
|
render_diff: Arc::new(render_diff),
|
|
matches_legacy_fragment: Arc::new(|_, _| false),
|
|
matches_retained_fragment: None,
|
|
}
|
|
}
|
|
|
|
pub fn with_legacy_matcher(
|
|
mut self,
|
|
matcher: impl Fn(&str, &str) -> bool + Send + Sync + 'static,
|
|
) -> Self {
|
|
self.matches_legacy_fragment = Arc::new(matcher);
|
|
self
|
|
}
|
|
|
|
/// Requires a matching model-visible fragment whenever a persisted snapshot is reused.
|
|
pub fn with_retained_fragment_matcher(
|
|
mut self,
|
|
matcher: impl Fn(&str, &str) -> bool + Send + Sync + 'static,
|
|
) -> Self {
|
|
self.matches_retained_fragment = Some(Arc::new(matcher));
|
|
self
|
|
}
|
|
|
|
pub fn id(&self) -> &'static str {
|
|
self.id
|
|
}
|
|
|
|
pub fn snapshot(&self) -> &Value {
|
|
&self.snapshot
|
|
}
|
|
|
|
pub fn render_diff(
|
|
&self,
|
|
previous: PreviousWorldStateSection<'_>,
|
|
) -> Option<RenderedWorldStateFragment> {
|
|
(self.render_diff)(previous)
|
|
}
|
|
|
|
pub fn matches_legacy_fragment(&self, role: &str, text: &str) -> bool {
|
|
(self.matches_legacy_fragment)(role, text)
|
|
}
|
|
|
|
pub fn has_retained_fragment_matcher(&self) -> bool {
|
|
self.matches_retained_fragment.is_some()
|
|
}
|
|
|
|
pub fn matches_retained_fragment(&self, role: &str, text: &str) -> bool {
|
|
self.matches_retained_fragment
|
|
.as_ref()
|
|
.is_some_and(|matcher| matcher(role, text))
|
|
}
|
|
}
|