Skip redirected external-agent migration destinations (#39221)

## Why

Repository-scoped imports must not follow redirected Codex paths and write
generated configuration outside the repository.

## What changed

- Reject repository migration scopes when generated configuration paths are
  symlinks or, on Windows, reparse points.
- Recheck hook and subagent destinations during import, skipping redirected
  directories, files, and dangling links without reporting them as imported.

## Testing

Add service and app-server coverage for redirected generated destinations,
including a repository path replaced with a symlink after detection.

GitOrigin-RevId: 3afb521ead9d76250f0e967eccfdd1dc194f027a
This commit is contained in:
jif authored and copyberry committed 2026-08-18 16:05:51 +00:00
1 parent 76ceaddb29
commit 2a30972fcb
5 files changed
+202 -5

No files matched your search

@@ -1,6 +1,77 @@
use super::super::*;
use pretty_assertions::assert_eq;
#[cfg(unix)]
#[tokio::test]
async fn repo_migration_skips_redirected_generated_destinations() -> std::io::Result<()> {
let root = TempDir::new()?;
let repo = root.path().join("repo");
let source = repo.join(EXTERNAL_AGENT_DIR);
let target = repo.join(".codex");
let outside = root.path().join("outside");
fs::create_dir_all(repo.join(".git"))?;
fs::create_dir_all(source.join("agents"))?;
fs::create_dir_all(source.join("hooks/nested"))?;
fs::create_dir_all(target.join("agents"))?;
fs::create_dir_all(target.join("hooks"))?;
fs::create_dir_all(&outside)?;
fs::write(
source.join("agents/researcher.md"),
"---\nname: researcher\ndescription: Research\n---\nInstructions\n",
)?;
fs::write(
source.join("settings.json"),
r#"{"hooks":{"Stop":[{"hooks":[{"command":"echo done"}]}]}}"#,
)?;
fs::write(source.join("hooks/nested/escaped.sh"), "nested")?;
fs::write(source.join("hooks/escaped.sh"), "dangling")?;
fs::write(source.join("hooks/safe.sh"), "safe")?;
std::os::unix::fs::symlink(
outside.join("researcher.toml"),
target.join("agents/researcher.toml"),
)?;
std::os::unix::fs::symlink(&outside, target.join("hooks/nested"))?;
std::os::unix::fs::symlink(outside.join("escaped.sh"), target.join("hooks/escaped.sh"))?;
let service = service_for_paths(
root.path().join(EXTERNAL_AGENT_DIR),
root.path().join(".codex"),
);
let mut items = service
.detect(ExternalAgentConfigDetectOptions {
include_home: false,
include_memory: false,
cwds: Some(vec![repo.clone()]),
})
.await?;
assert_eq!(
items.iter().map(|item| item.item_type).collect::<Vec<_>>(),
vec![ExternalAgentConfigMigrationItemType::Hooks]
);
items.push(ExternalAgentConfigMigrationItem {
item_type: ExternalAgentConfigMigrationItemType::Subagents,
description: String::new(),
cwd: Some(repo),
details: None,
});
let outcome = service.import(items).await;
assert_eq!(
outcome
.item_results
.iter()
.map(|result| (result.item_type, result.success_count, result.error_count))
.collect::<Vec<_>>(),
vec![
(ExternalAgentConfigMigrationItemType::Hooks, 1, 0),
(ExternalAgentConfigMigrationItemType::Subagents, 0, 0),
]
);
assert_eq!(fs::read_to_string(target.join("hooks/safe.sh"))?, "safe");
assert!(fs::read_dir(&outside)?.next().is_none());
Ok(())
}
#[tokio::test]
async fn import_repo_agents_md_from_nested_cwd_rewrites_terms_and_skips_non_empty_targets() {
let root = TempDir::new().expect("create tempdir");