Use session permission profiles for local /diff (#63400)

## Why

`/diff` runs Git through standalone app-server commands, so it can pick up
the server's default permissions even when the active session uses a
different profile.

## What changed

- Use the session's named profile for all Git commands when the local daemon
  can resolve it for the session's single workspace root. Use server defaults
  when the profile depends on session-only settings or the workspace cannot
  be represented by the command's working directory.
- Disable Git transports and lazy fetching for profiled diff commands.
  Surface repository discovery errors under the selected profile.
- Display asynchronous diff results only when both the thread and working
  directory still match.

## Testing

Cover profile fallback after reattaching to a live thread, missing blobs
without lazy fetching, and stale results from another thread or directory.

GitOrigin-RevId: be8259d8d7af536c2d542d2edce2c601ed4482d0
This commit is contained in:
Eric Traut
2026-11-23 18:26:30 +00:00
committed by copyberry
parent 1b6ef00d25
commit 6755b0d25a
9 changed files with 394 additions and 57 deletions
+41 -4
View File
@@ -12,6 +12,7 @@
use std::collections::HashMap;
use std::future::Future;
use std::path::Path;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::Arc;
@@ -21,7 +22,10 @@ use codex_app_server_client::AppServerRequestHandle;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::CommandExecParams;
use codex_app_server_protocol::CommandExecResponse;
use codex_app_server_protocol::PermissionProfileListParams;
use codex_app_server_protocol::PermissionProfileListResponse;
use codex_app_server_protocol::RequestId;
use futures::future::BoxFuture;
use uuid::Uuid;
/// Shared handle for running workspace commands from TUI components.
@@ -39,6 +43,8 @@ pub(crate) struct WorkspaceCommand {
pub(crate) argv: Vec<String>,
/// Explicit working directory on the host that executes the command.
pub(crate) cwd: PathBuf,
/// Named permissions to resolve in the command cwd. Omission uses server defaults.
pub(crate) permission_profile: Option<String>,
/// Environment overrides where `None` removes a variable.
pub(crate) env: HashMap<String, Option<String>>,
/// Maximum wall-clock duration before app-server cancels the command.
@@ -56,6 +62,7 @@ impl WorkspaceCommand {
Self {
argv: argv.into_iter().map(Into::into).collect(),
cwd: cwd.into(),
permission_profile: None,
env: HashMap::new(),
timeout: Duration::from_secs(/*secs*/ 5),
disable_output_cap: false,
@@ -129,6 +136,15 @@ impl std::error::Error for WorkspaceCommandError {}
/// Implementations decide where the workspace lives. Callers provide argv/cwd/env and should not
/// branch on local versus remote execution.
pub(crate) trait WorkspaceCommandExecutor: Send + Sync {
/// Checks whether a custom profile is defined outside the thread.
fn has_permission_profile<'a>(
&'a self,
_cwd: &'a Path,
_id: &'a str,
) -> BoxFuture<'a, Result<bool, WorkspaceCommandError>> {
Box::pin(async { Ok(false) })
}
/// Runs a workspace command and returns captured output or an app-server request error.
///
/// Callers should treat errors as infrastructure failures and should treat successful output
@@ -156,12 +172,33 @@ impl AppServerWorkspaceCommandRunner {
}
impl WorkspaceCommandExecutor for AppServerWorkspaceCommandRunner {
fn has_permission_profile<'a>(
&'a self,
cwd: &'a Path,
id: &'a str,
) -> BoxFuture<'a, Result<bool, WorkspaceCommandError>> {
Box::pin(async move {
let catalog: PermissionProfileListResponse = self
.request_handle
.request_typed(ClientRequest::PermissionProfileList {
request_id: RequestId::String(format!("workspace-profiles-{}", Uuid::new_v4())),
params: PermissionProfileListParams {
cwd: Some(cwd.to_string_lossy().into_owned()),
cursor: None,
limit: None, // Full catalog.
},
})
.await
.map_err(|err| WorkspaceCommandError::new(err.to_string()))?;
// Let command/exec enforce requirements for defined profiles.
Ok(catalog.data.iter().any(|profile| profile.id == id))
})
}
/// Sends the command as a one-off app-server `command/exec` request.
///
/// The request is non-tty, does not stream stdin/stdout/stderr, and uses the caller's timeout.
/// Bounded commands use app-server's default output cap. It leaves sandbox and permission
/// profile selection to app-server so the same runner follows the active session's embedded or
/// remote execution policy.
/// App-server resolves any named permission profile in the command cwd.
fn run(
&self,
command: WorkspaceCommand,
@@ -193,7 +230,7 @@ impl WorkspaceCommandExecutor for AppServerWorkspaceCommandRunner {
env,
size: None,
sandbox_policy: None,
permission_profile: None,
permission_profile: command.permission_profile,
},
})
.await