Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn build_checkpoint_attrs(

/// Persistent local rate limit keyed by prompt ID hash.
#[cfg(not(any(test, feature = "test-support")))]
fn should_emit_agent_usage(agent_id: &AgentId) -> bool {
pub(crate) fn should_emit_agent_usage(agent_id: &AgentId) -> bool {
let prompt_id = generate_short_hash(&agent_id.id, &agent_id.tool);
let now_ts = SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -107,7 +107,7 @@ fn should_emit_agent_usage(agent_id: &AgentId) -> bool {

/// Always returns false in test mode — no metrics DB access needed.
#[cfg(any(test, feature = "test-support"))]
fn should_emit_agent_usage(_agent_id: &AgentId) -> bool {
pub(crate) fn should_emit_agent_usage(_agent_id: &AgentId) -> bool {
false
}

Expand Down
29 changes: 29 additions & 0 deletions src/commands/git_ai_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::authorship::authorship_log_serialization::generate_short_hash;
use crate::authorship::ignore::effective_ignore_patterns;
use crate::authorship::internal_db::InternalDatabase;
use crate::authorship::range_authorship;
Expand Down Expand Up @@ -589,6 +590,7 @@ fn handle_checkpoint(args: &[String]) {
"Failed to find any git repositories for the edited files. Orphaned files: {:?}",
orphan_files
);
emit_no_repo_agent_metrics(agent_run_result.as_ref());
std::process::exit(0);
}

Expand Down Expand Up @@ -719,6 +721,7 @@ fn handle_checkpoint(args: &[String]) {
eprintln!(
"Failed to find repository: workspace root is not a git repository and no edited files provided"
);
emit_no_repo_agent_metrics(agent_run_result.as_ref());
std::process::exit(0);
}

Expand Down Expand Up @@ -1091,6 +1094,32 @@ fn handle_git_hooks(args: &[String]) {
}
}

fn emit_no_repo_agent_metrics(agent_run_result: Option<&AgentRunResult>) {
let Some(result) = agent_run_result else {
return;
};
if result.checkpoint_kind == CheckpointKind::Human {
return;
}

let agent_id = &result.agent_id;
if !commands::checkpoint::should_emit_agent_usage(agent_id) {
return;
}

let prompt_id = generate_short_hash(&agent_id.id, &agent_id.tool);
let attrs = crate::metrics::EventAttributes::with_version(env!("CARGO_PKG_VERSION"))
.tool(&agent_id.tool)
.model(&agent_id.model)
.prompt_id(prompt_id)
.external_prompt_id(&agent_id.id);

let values = crate::metrics::AgentUsageValues::new();
crate::metrics::record(values, attrs);

observability::spawn_background_flush();
}

fn get_all_files_for_mock_ai(working_dir: &str) -> Vec<String> {
// Find the git repository
let repo = match find_repository_in_path(working_dir) {
Expand Down
Loading