feat: Tambahkan file baru ke mention_index saat tool write membuatnya

Ketika write tool membuat file baru (bukan overwrite), path file
sekarang di-push ke mention_index untuk autocomplete @file mention.

- Capture file existence status sebelum write
- Push ke mention_index hanya untuk genuinely new files
- Add 2 tests: one untuk new file push, one untuk overwrite non-duplication

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
asepharyana
2026-07-15 06:32:57 +07:00
co-authored by Claude Sonnet 5
parent 93be92a5fb
commit 930961bd85
+25
View File
@@ -60,12 +60,16 @@ impl Tool for Write {
let check_matches = check_graduated_checks(&rel, &content, &ctx.graduated_checks);
let path = resolve_path(&ctx.workspaces, &rel)?;
let old_content = fs::read_to_string(&path).ok();
let existed_before = path.exists();
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)
.map_err(|e| anyhow!("failed to create parent directories for '{rel}': {e}"))?;
}
fs::write(&path, &content)
.map_err(|e| anyhow!("failed to write '{rel}': {e}"))?;
if !existed_before {
ctx.mention_index.push(rel.clone());
}
// Notify the LSP server of the on-disk change so diagnostics stay in
// sync. Never fails the write itself: a lock failure or LSP error is
// folded into the returned message instead of propagated as an Err.
@@ -144,4 +148,25 @@ mod tests {
assert!(result.contains("wrote"));
fs::remove_dir_all(&workspace).ok();
}
#[test]
fn write_creating_a_new_file_appends_to_the_mention_index() {
let workspace = temp_workspace();
let ctx = test_ctx(workspace.clone());
let args = json!({"path": "brand_new.txt", "content": "hi\n", "reason": "test mention index"});
Write.run(&ctx, &args).unwrap();
assert_eq!(ctx.mention_index.snapshot(), vec!["brand_new.txt".to_string()]);
fs::remove_dir_all(&workspace).ok();
}
#[test]
fn write_overwriting_a_file_does_not_duplicate_the_mention_index_entry() {
let workspace = temp_workspace();
fs::write(workspace.join("existing.txt"), "old\n").unwrap();
let ctx = test_ctx(workspace.clone());
let args = json!({"path": "existing.txt", "content": "new\n", "reason": "test no duplicate"});
Write.run(&ctx, &args).unwrap();
assert!(ctx.mention_index.snapshot().is_empty());
fs::remove_dir_all(&workspace).ok();
}
}