fix(tui): render network approval history by target (#22229)

## Why

Network approval prompts are rendered without a command string on the
app-server path. After the user approves one of those prompts, the TUI
history cell previously fell back to command-oriented copy and produced
malformed lines such as:

```text
You approved codex to run  every time this session
```

That hid the network target the user actually approved and left a
visibly broken transcript entry.

## What changed

- Preserve the approval subject as either a command or a network target
when recording TUI approval decisions.
- Render target-aware history copy for network approval outcomes:
  - approve once
  - approve for the current session
  - cancel
- Include the approval protocol and preserve the managed-proxy
`network-access` target when present, including non-default ports such
as `https://example.com:8443`.
- Fall back to formatting the network approval context as
`protocol://host` when no generated target command is available.
- Keep ordinary command approval history, Guardian approval history, and
persisted network-rule history behavior unchanged.
- Add focused regression coverage and snapshots for the three
network-history cases.

## How to Test

1. Start Codex in a flow that triggers a network approval prompt.
2. Approve network access only for the current conversation.
3. Confirm the transcript records the approved network target, for
example:
- `You approved codex network access to https://example.com:8443 every
time this session`
4. Trigger the prompt again and verify the one-time approval and cancel
paths also record target-specific history text instead of an empty
command gap.

Targeted automated coverage:
- `cargo test -p codex-tui network_exec_approval_history`

## Additional verification

- `cargo insta pending-snapshots`
- `git diff --check`
- `just fix -p codex-tui`
- `just argument-comment-lint`

## Known unrelated local test noise

A full `cargo test -p codex-tui` run still hits a pre-existing stack
overflow outside this change:
- `tests::fork_last_filters_latest_session_by_cwd_unless_show_all`
aborts with a stack overflow
This commit is contained in:
Felipe Coury authored and GitHub committed 2026-05-14 14:33:54 -03:00
1 parent 6ec8c4a6ec
commit 5a02962519
8 files changed
+449 -71

No files matched your search

@@ -47,6 +47,7 @@ use codex_app_server_protocol::FileSystemSandboxEntry;
use codex_app_server_protocol::FileSystemSpecialPath;
use codex_app_server_protocol::McpServerElicitationAction;
use codex_app_server_protocol::NetworkApprovalContext;
use codex_app_server_protocol::NetworkApprovalProtocol;
use codex_app_server_protocol::NetworkPolicyRuleAction;
use codex_app_server_protocol::RequestId;
use codex_features::Features;
@@ -354,8 +355,25 @@ impl ApprovalOverlay {
return;
};
if request.thread_label().is_none() {
let subject = match request {
ApprovalRequest::Exec {
network_approval_context: Some(network_approval_context),
..
} => history_cell::ApprovalDecisionSubject::NetworkAccess {
target: network_approval_target(network_approval_context, command),
},
_ => {
if let Some(target) = network_approval_command_target(command) {
history_cell::ApprovalDecisionSubject::NetworkAccess {
target: target.to_string(),
}
} else {
history_cell::ApprovalDecisionSubject::Command(command.to_vec())
}
}
};
let cell = history_cell::new_approval_decision_cell(
command.to_vec(),
subject,
command_decision_to_review_decision(&decision),
history_cell::ApprovalDecisionActor::User,
);
@@ -623,6 +641,35 @@ fn approval_footer_hint(
Line::from(spans)
}
fn network_approval_target(
network_approval_context: &NetworkApprovalContext,
command: &[String],
) -> String {
if let Some(target) = network_approval_command_target(command) {
return target.to_string();
}
let scheme = match network_approval_context.protocol {
NetworkApprovalProtocol::Http => "http",
NetworkApprovalProtocol::Https => "https",
NetworkApprovalProtocol::Socks5Tcp => "socks5-tcp",
NetworkApprovalProtocol::Socks5Udp => "socks5-udp",
};
format!("{scheme}://{}", network_approval_context.host)
}
fn network_approval_command_target(command: &[String]) -> Option<&str> {
match command {
[program, target] if program == "network-access" && !target.is_empty() => {
Some(target.as_str())
}
[command] => command
.strip_prefix("network-access ")
.filter(|target| !target.is_empty()),
_ => None,
}
}
fn build_header(request: &ApprovalRequest) -> Box<dyn Renderable> {
match request {
ApprovalRequest::Exec {
@@ -1102,6 +1149,21 @@ mod tests {
.join("\n")
}
fn render_history_cell_lines(
cell: &dyn crate::history_cell::HistoryCell,
width: u16,
) -> Vec<String> {
cell.display_lines(width)
.iter()
.map(|line| {
line.spans
.iter()
.map(|span| span.content.as_ref())
.collect::<String>()
})
.collect()
}
fn normalize_snapshot_paths(rendered: String) -> String {
[
(absolute_path("/tmp/readme.txt"), "/tmp/readme.txt"),
@@ -2111,7 +2173,7 @@ mod tests {
"git add tui/src/render/mod.rs tui/src/render/renderable.rs".into(),
];
let cell = history_cell::new_approval_decision_cell(
command,
history_cell::ApprovalDecisionSubject::Command(command),
ReviewDecision::Approved,
history_cell::ApprovalDecisionActor::User,
);
@@ -2134,6 +2196,73 @@ mod tests {
assert_eq!(rendered, expected);
}
#[test]
fn exec_history_cell_does_not_render_blank_action_for_empty_command() {
let approved = history_cell::new_approval_decision_cell(
history_cell::ApprovalDecisionSubject::Command(Vec::new()),
ReviewDecision::Approved,
history_cell::ApprovalDecisionActor::User,
);
assert_eq!(
render_history_cell_lines(approved.as_ref(), /*width*/ 80),
vec!["✔ You approved this request this time".to_string()]
);
let approved_for_session = history_cell::new_approval_decision_cell(
history_cell::ApprovalDecisionSubject::Command(Vec::new()),
ReviewDecision::ApprovedForSession,
history_cell::ApprovalDecisionActor::User,
);
assert_eq!(
render_history_cell_lines(approved_for_session.as_ref(), /*width*/ 80),
vec!["✔ You approved this request every time this session".to_string()]
);
}
#[test]
fn network_access_command_history_uses_target_without_structured_context() {
let (tx_raw, mut rx) = unbounded_channel::<AppEvent>();
let tx = AppEventSender::new(tx_raw);
let mut view = make_overlay(
ApprovalRequest::Exec {
thread_id: ThreadId::new(),
thread_label: None,
id: "test".into(),
command: vec![
"network-access".to_string(),
"https://example.com:8443".to_string(),
],
reason: None,
available_decisions: vec![
CommandExecutionApprovalDecision::Accept,
CommandExecutionApprovalDecision::Cancel,
],
network_approval_context: None,
additional_permissions: None,
},
tx,
Features::with_defaults(),
);
view.handle_key_event(KeyEvent::new(KeyCode::Char('y'), KeyModifiers::NONE));
let mut decision = None;
while let Ok(event) = rx.try_recv() {
if let AppEvent::InsertHistoryCell(cell) = event {
decision = Some(cell);
break;
}
}
let decision = decision.expect("expected decision cell in history");
assert_eq!(
render_history_cell_lines(decision.as_ref(), /*width*/ 80),
vec![
"✔ You approved codex network access to https://example.com:8443 this time"
.to_string(),
]
);
}
#[test]
fn esc_cancels_mcp_elicitation() {
let (tx_raw, mut rx) = unbounded_channel::<AppEvent>();