Track resource-backed skill invocations (#38066)

## What changed

- Emit skill invocation analytics when a resource-backed skill is explicitly selected or its main resource is successfully read from the first page.
- Preserve provider-supplied skill IDs and user scope when available, and derive a stable ID from the main resource otherwise.
- Deduplicate repeated implicit reads of the same main resource within a turn.

## Testing

- Extend the orchestrator skill integration test to cover explicit and implicit invocation events, stable fallback IDs, failed reads, and repeated-read deduplication.

GitOrigin-RevId: 7d8453b31b5a2205847e65cda2c46183d6c97b45
This commit is contained in:
rphilizaire-openai authored and copyberry committed 2026-08-11 19:52:54 +00:00
1 parent 104e25ac5a
commit c8f673fddc
14 files changed
+311 -71

No files matched your search

+52 -20
View File
@@ -86,11 +86,13 @@ use crate::facts::ExternalAgentConfigImportFailureInput;
use crate::facts::HookRunInput;
use crate::facts::ImagePreparationFact;
use crate::facts::ImagePreparationMetadata;
use crate::facts::InvocationType;
use crate::facts::PluginInstallFailedInput;
use crate::facts::PluginInstallRequestedInput;
use crate::facts::PluginState;
use crate::facts::PluginStateChangedInput;
use crate::facts::PluginUsedInput;
use crate::facts::SkillInvocationLocation;
use crate::facts::SkillInvokedInput;
use crate::facts::SubAgentThreadStartedInput;
use crate::facts::ThreadInitializationMode;
@@ -153,6 +155,7 @@ use codex_protocol::request_permissions::PermissionGrantScope as CorePermissionG
use codex_protocol::request_permissions::RequestPermissionsResponse as CoreRequestPermissionsResponse;
use sha1::Digest;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::path::Path;
use std::path::PathBuf;
@@ -394,6 +397,7 @@ struct TurnState {
latest_diff: Option<String>,
steer_count: usize,
tool_counts: TurnToolCounts,
resource_skill_invocations: HashSet<String>,
}
#[derive(Clone, Hash, Eq, PartialEq)]
@@ -1078,26 +1082,54 @@ impl AnalyticsReducer {
invocations,
} = input;
for invocation in invocations {
let skill_scope = match invocation.skill_scope {
SkillScope::User => "user",
SkillScope::Repo => "repo",
SkillScope::System => "system",
SkillScope::Admin => "admin",
let (skill_id, repo_url, skill_scope) = match invocation.location {
SkillInvocationLocation::Host { path, scope } => {
let skill_scope = match scope {
SkillScope::User => "user",
SkillScope::Repo => "repo",
SkillScope::System => "system",
SkillScope::Admin => "admin",
};
let repo_root = get_git_repo_root(path.as_path());
let repo_url = if let Some(root) = repo_root.as_ref() {
collect_git_info(root)
.await
.and_then(|info| info.repository_url)
} else {
None
};
let skill_id = skill_id_for_local_skill(
repo_url.as_deref(),
repo_root.as_deref(),
path.as_path(),
invocation.skill_name.as_str(),
);
(skill_id, repo_url, Some(skill_scope.to_string()))
}
SkillInvocationLocation::Resource {
id,
skill_id,
scope,
} => {
if matches!(invocation.invocation_type, InvocationType::Implicit) {
let turn_state = self.turns.entry(tracking.turn_id.clone()).or_default();
if !turn_state.resource_skill_invocations.insert(id.clone()) {
continue;
}
}
let skill_id = skill_id
.unwrap_or_else(|| format!("{:x}", sha1::Sha1::digest(id.as_bytes())));
let skill_scope = scope
.map(|scope| match scope {
SkillScope::User => "user",
SkillScope::Repo => "repo",
SkillScope::System => "system",
SkillScope::Admin => "admin",
})
.map(str::to_owned);
(skill_id, None, skill_scope)
}
};
let repo_root = get_git_repo_root(invocation.skill_path.as_path());
let repo_url = if let Some(root) = repo_root.as_ref() {
collect_git_info(root)
.await
.and_then(|info| info.repository_url)
} else {
None
};
let skill_id = skill_id_for_local_skill(
repo_url.as_deref(),
repo_root.as_deref(),
invocation.skill_path.as_path(),
invocation.skill_name.as_str(),
);
out.push(TrackEventRequest::SkillInvocation(
SkillInvocationEventRequest {
event_type: "skill_invocation",
@@ -1110,7 +1142,7 @@ impl AnalyticsReducer {
model_slug: Some(tracking.model_slug.clone()),
product_client_id: Some(tracking.product_client_id.clone()),
repo_url,
skill_scope: Some(skill_scope.to_string()),
skill_scope,
plugin_id: invocation.plugin_id,
remote_plugin_id: invocation.remote_plugin_id,
},