Skip to content

Commit 404c4d0

Browse files
committed
Merge PR #137: preserve cached indexing results
2 parents 3f91aac + ed4b3fb commit 404c4d0

2 files changed

Lines changed: 107 additions & 4 deletions

File tree

‎src/indexing/facade.rs‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,9 +1050,6 @@ impl IndexFacade {
10501050
// Mutation Methods (delegate to Pipeline)
10511051
// =========================================================================
10521052

1053-
/// Index a single file using the parallel pipeline.
1054-
///
1055-
/// Returns `IndexingResult::Indexed` with the file ID on success.
10561053
/// File records key off path text: an uncanonical root or file path
10571054
/// (`./src`, `x/../x`) addresses a key space disjoint from the
10581055
/// registered indexed_paths walks, re-indexing every file as new and
@@ -1108,6 +1105,7 @@ impl IndexFacade {
11081105
.collect()
11091106
}
11101107

1108+
/// Index a single file, returning `Cached` when its content hash is unchanged.
11111109
pub fn index_file(
11121110
&mut self,
11131111
path: impl AsRef<std::path::Path>,
@@ -1125,7 +1123,11 @@ impl IndexFacade {
11251123
self.embedding_pool.clone(),
11261124
)?;
11271125

1128-
Ok(crate::IndexingResult::Indexed(stats.file_id))
1126+
if stats.cached {
1127+
Ok(crate::IndexingResult::Cached(stats.file_id))
1128+
} else {
1129+
Ok(crate::IndexingResult::Indexed(stats.file_id))
1130+
}
11291131
}
11301132

11311133
/// Index a single file with optional force re-indexing.
@@ -4603,4 +4605,40 @@ mod tests {
46034605
.unwrap();
46044606
assert_cross_root_edge(&facade, "post-edit");
46054607
}
4608+
4609+
// Lock for the pipeline's cached result: a second index of unchanged
4610+
// content is `Cached`; an edit is `Indexed` again and replaces the
4611+
// symbols.
4612+
#[test]
4613+
fn index_file_returns_cached_for_unchanged_content_and_indexed_for_an_edit() {
4614+
let dir = tempfile::tempdir().unwrap();
4615+
let root = dir.path().canonicalize().unwrap().join("src");
4616+
std::fs::create_dir_all(&root).unwrap();
4617+
let file = root.join("a.py");
4618+
std::fs::write(&file, "def alpha():\n pass\n").unwrap();
4619+
let mut settings = Settings {
4620+
index_path: dir.path().join("index"),
4621+
workspace_root: None,
4622+
..Default::default()
4623+
};
4624+
settings.add_indexed_path(root.clone()).unwrap();
4625+
let mut facade = IndexFacade::new(std::sync::Arc::new(settings)).unwrap();
4626+
4627+
let first = facade.index_file(&file).unwrap();
4628+
let crate::IndexingResult::Indexed(file_id) = first else {
4629+
panic!("first index: expected Indexed, got {first:?}");
4630+
};
4631+
assert_eq!(
4632+
facade.index_file(&file).unwrap(),
4633+
crate::IndexingResult::Cached(file_id)
4634+
);
4635+
4636+
std::fs::write(&file, "def beta():\n pass\n").unwrap();
4637+
assert!(matches!(
4638+
facade.index_file(&file).unwrap(),
4639+
crate::IndexingResult::Indexed(_)
4640+
));
4641+
assert!(facade.find_symbols_by_name("alpha", None).is_empty());
4642+
assert_eq!(facade.find_symbols_by_name("beta", None).len(), 1);
4643+
}
46064644
}

‎src/watcher/unified.rs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,71 @@ mod tests {
16821682
watcher
16831683
}
16841684

1685+
#[tokio::test]
1686+
async fn unchanged_code_events_do_not_notify_but_edits_do() {
1687+
let dir = tempfile::tempdir().unwrap();
1688+
let root = dir.path().join("root");
1689+
std::fs::create_dir(&root).unwrap();
1690+
let source = root.join("a.py");
1691+
std::fs::write(&source, "def alpha():\n pass\n").unwrap();
1692+
let watcher = watcher_over(dir.path(), &root).await;
1693+
let mut events = watcher.broadcaster.subscribe();
1694+
1695+
watcher
1696+
.execute_action(
1697+
WatchAction::ReindexCode {
1698+
path: source.clone(),
1699+
created: true,
1700+
},
1701+
"code",
1702+
)
1703+
.await
1704+
.unwrap();
1705+
assert!(matches!(
1706+
events.try_recv().unwrap(),
1707+
FileChangeEvent::FileCreated { path } if path == source
1708+
));
1709+
1710+
// Duplicate observations take the cached branch and emit no reindex
1711+
// notification. This facade has no semantic search, so the snapshot
1712+
// save that the cached branch also skips is not exercised here.
1713+
for _ in 0..2 {
1714+
watcher
1715+
.execute_action(
1716+
WatchAction::ReindexCode {
1717+
path: source.clone(),
1718+
created: false,
1719+
},
1720+
"code",
1721+
)
1722+
.await
1723+
.unwrap();
1724+
assert!(matches!(
1725+
events.try_recv(),
1726+
Err(tokio::sync::broadcast::error::TryRecvError::Empty)
1727+
));
1728+
}
1729+
1730+
std::fs::write(&source, "def beta():\n pass\n").unwrap();
1731+
watcher
1732+
.execute_action(
1733+
WatchAction::ReindexCode {
1734+
path: source.clone(),
1735+
created: false,
1736+
},
1737+
"code",
1738+
)
1739+
.await
1740+
.unwrap();
1741+
assert!(matches!(
1742+
events.try_recv().unwrap(),
1743+
FileChangeEvent::FileReindexed { path } if path == source
1744+
));
1745+
let facade = watcher.facade.read().await;
1746+
assert!(facade.find_symbols_by_name("alpha", None).is_empty());
1747+
assert_eq!(facade.find_symbols_by_name("beta", None).len(), 1);
1748+
}
1749+
16851750
// A dir rename's from-side arrives as Modify(Name) on a path that no
16861751
// longer exists, and no per-file events follow. A vanished path that
16871752
// prefixes watched directories is a directory removal observation:

0 commit comments

Comments
 (0)