[codex] simplify memory read metrics (#28164)

## Why

Memory read telemetry currently reconstructs the executable shell
command after a tool call finishes. That duplicates shell, login-policy,
and cwd resolution owned by the tool handlers, and can diverge from the
environment-specific command that unified exec actually ran.

## What changed

- Expose the existing restricted shell-script parser directly for raw
script text.
- Parse `shell_command` and `exec_command` input into plain command argv
before classifying memory reads.
- Preserve all-or-nothing safe-command validation for multi-command
scripts.
- Remove cwd resolution, shell selection, and the unnecessary async
boundary from memory read metric emission.

## Testing

- `just test -p codex-shell-command`
- `cargo check -p codex-core`
This commit is contained in:
pakrym-oai
2026-06-15 08:28:02 -07:00
committed by GitHub
parent bbcfed8ac2
commit 42ad752f36
6 changed files with 36 additions and 58 deletions
+6 -40
View File
@@ -5,20 +5,15 @@ use crate::tools::handlers::unified_exec::ExecCommandArgs;
use codex_memories_read::usage::MEMORIES_USAGE_METRIC;
use codex_memories_read::usage::memories_usage_kinds_from_command;
use codex_protocol::models::ShellCommandToolCallParams;
use std::path::PathBuf;
pub(crate) async fn emit_metric_for_tool_read(invocation: &ToolInvocation, success: bool) {
let Some((command, _)) = shell_command_for_invocation(invocation) else {
pub(crate) fn emit_metric_for_tool_read(invocation: &ToolInvocation, success: bool) {
let Some(command) = shell_script_for_invocation(invocation) else {
return;
};
let kinds = memories_usage_kinds_from_command(&command);
if kinds.is_empty() {
return;
}
let success = if success { "true" } else { "false" };
let tool_name = flat_tool_name(&invocation.tool_name);
for kind in kinds {
for kind in memories_usage_kinds_from_command(&command) {
invocation.turn.session_telemetry.counter(
MEMORIES_USAGE_METRIC,
/*inc*/ 1,
@@ -31,7 +26,7 @@ pub(crate) async fn emit_metric_for_tool_read(invocation: &ToolInvocation, succe
}
}
fn shell_command_for_invocation(invocation: &ToolInvocation) -> Option<(Vec<String>, PathBuf)> {
fn shell_script_for_invocation(invocation: &ToolInvocation) -> Option<String> {
let ToolPayload::Function { arguments } = &invocation.payload else {
return None;
};
@@ -42,39 +37,10 @@ fn shell_command_for_invocation(invocation: &ToolInvocation) -> Option<(Vec<Stri
) {
(None, "shell_command") => serde_json::from_str::<ShellCommandToolCallParams>(arguments)
.ok()
.map(|params| {
if !invocation.turn.config.permissions.allow_login_shell
&& params.login == Some(true)
{
#[allow(deprecated)]
let cwd = invocation.turn.resolve_path(params.workdir).to_path_buf();
return (Vec::new(), cwd);
}
let use_login_shell = params
.login
.unwrap_or(invocation.turn.config.permissions.allow_login_shell);
let command = invocation
.session
.user_shell()
.derive_exec_args(&params.command, use_login_shell);
#[allow(deprecated)]
let cwd = invocation.turn.resolve_path(params.workdir).to_path_buf();
(command, cwd)
}),
.map(|params| params.command),
(None, "exec_command") => serde_json::from_str::<ExecCommandArgs>(arguments)
.ok()
.and_then(|params| {
let command = crate::tools::handlers::unified_exec::get_command(
&params,
invocation.session.user_shell(),
&invocation.turn.unified_exec_shell_mode,
invocation.turn.config.permissions.allow_login_shell,
)
.ok()?;
#[allow(deprecated)]
let cwd = invocation.turn.resolve_path(params.workdir).to_path_buf();
Some((command.command, cwd))
}),
.map(|params| params.cmd),
(Some(_), _) | (None, _) => None,
}
}