From ebb023699d63c2498e171d98232a890361f44bbb Mon Sep 17 00:00:00 2001 From: hechibing Date: Mon, 16 Feb 2026 15:48:08 +0800 Subject: [PATCH] fix(agent): support utf-16 files in agent edit flow --- src/cortex-cli/src/agent_cmd/handlers/edit.rs | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/cortex-cli/src/agent_cmd/handlers/edit.rs b/src/cortex-cli/src/agent_cmd/handlers/edit.rs index 116fc6c..b350e95 100644 --- a/src/cortex-cli/src/agent_cmd/handlers/edit.rs +++ b/src/cortex-cli/src/agent_cmd/handlers/edit.rs @@ -4,7 +4,7 @@ use anyhow::{Context, Result, bail}; use std::io::{self, BufRead, Write}; use crate::agent_cmd::cli::EditArgs; -use crate::agent_cmd::loader::{load_all_agents, parse_frontmatter}; +use crate::agent_cmd::loader::{load_all_agents, parse_frontmatter, read_file_with_encoding}; /// Edit agent command. /// @@ -47,8 +47,8 @@ pub async fn run_edit(args: EditArgs) -> Result<()> { } }); - // Make a backup of the original file - let backup_content = std::fs::read_to_string(path) + // Make a byte-for-byte backup of the original file so rollback preserves encoding. + let backup_content = std::fs::read(path) .with_context(|| format!("Failed to read agent file: {}", path.display()))?; loop { @@ -64,7 +64,7 @@ pub async fn run_edit(args: EditArgs) -> Result<()> { } // Read and validate the edited file - let content = std::fs::read_to_string(path) + let content = read_file_with_encoding(path) .with_context(|| format!("Failed to read edited file: {}", path.display()))?; // Try to parse the frontmatter to validate @@ -120,3 +120,29 @@ pub async fn run_edit(args: EditArgs) -> Result<()> { } } } + +#[cfg(test)] +mod tests { + use super::*; + use std::path::Path; + + fn utf16le_with_bom(input: &str) -> Vec { + let mut bytes = vec![0xFF, 0xFE]; + for unit in input.encode_utf16() { + bytes.extend_from_slice(&unit.to_le_bytes()); + } + bytes + } + + #[test] + fn test_read_file_with_encoding_supports_utf16_for_edit_flow() { + let temp_dir = tempfile::tempdir().expect("tempdir"); + let file_path = temp_dir.path().join("agent.md"); + + let content = "---\nname: utf16\nmode: primary\n---\nPrompt"; + std::fs::write(&file_path, utf16le_with_bom(content)).expect("write utf16 file"); + + let read = read_file_with_encoding(Path::new(&file_path)).expect("read with encoding"); + assert_eq!(read, content); + } +}