mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 01:05:10 +08:00
refactor: rebuild desktop app with Tauri
This commit is contained in:
1 parent
a3ec2a0dfc
commit
4053a7fb20
808 files changed
+87359
-181491
No files matched your search
+65
@@ -0,0 +1,65 @@
|
||||
//! On-disk paths derived from source identity and index configuration.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::{config::INDEX_FORMAT_VERSION, ContentType};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CacheLayout {
|
||||
pub snapshot: PathBuf,
|
||||
pub runtime: PathBuf,
|
||||
}
|
||||
|
||||
impl CacheLayout {
|
||||
pub fn new(
|
||||
root: &Path,
|
||||
source_identity: &str,
|
||||
model: &str,
|
||||
content: &[ContentType],
|
||||
chunk_bytes: usize,
|
||||
) -> Self {
|
||||
let source = hex::encode(Sha256::digest(source_identity.as_bytes()));
|
||||
let mut signature = format!("{model}:{chunk_bytes}:");
|
||||
let mut content = content.to_vec();
|
||||
content.sort_by_key(|item| *item as u8);
|
||||
for item in content {
|
||||
signature.push_str(&format!("{item:?},"));
|
||||
}
|
||||
let signature = hex::encode(Sha256::digest(signature.as_bytes()));
|
||||
let directory = root
|
||||
.join(format!("indexes/v{INDEX_FORMAT_VERSION}"))
|
||||
.join(source)
|
||||
.join(signature);
|
||||
Self {
|
||||
snapshot: directory.join("index.bin"),
|
||||
runtime: directory.join("runtime.bin"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::ffi::OsStr;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn snapshot_path_tracks_the_index_format_version() {
|
||||
let layout = CacheLayout::new(
|
||||
Path::new("cache"),
|
||||
"source",
|
||||
"model",
|
||||
&[ContentType::Code],
|
||||
750,
|
||||
);
|
||||
let version = format!("v{INDEX_FORMAT_VERSION}");
|
||||
assert!(layout
|
||||
.snapshot
|
||||
.components()
|
||||
.any(|component| component.as_os_str() == OsStr::new(&version)));
|
||||
assert_eq!(layout.runtime.file_name(), Some(OsStr::new("runtime.bin")));
|
||||
assert_eq!(layout.snapshot.parent(), layout.runtime.parent());
|
||||
}
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
//! Stable cache keys plus atomic, versioned index snapshot persistence.
|
||||
|
||||
mod layout;
|
||||
mod store;
|
||||
|
||||
pub use layout::CacheLayout;
|
||||
pub use store::{load_runtime, load_snapshot, save_runtime, save_snapshot};
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
//! Corruption-resistant bincode snapshot loading and atomic replacement.
|
||||
|
||||
use std::{fs, io::Write, path::Path};
|
||||
|
||||
use crate::{
|
||||
index::{IndexSnapshot, LoadedIndex},
|
||||
Error, Result,
|
||||
};
|
||||
|
||||
pub fn load_snapshot(path: &Path) -> Result<Option<IndexSnapshot>> {
|
||||
let Some(snapshot) = load::<IndexSnapshot>(path)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
snapshot.validate()?;
|
||||
Ok(Some(snapshot))
|
||||
}
|
||||
|
||||
pub fn save_snapshot(path: &Path, snapshot: &IndexSnapshot) -> Result<()> {
|
||||
snapshot.validate()?;
|
||||
save(path, snapshot)
|
||||
}
|
||||
|
||||
pub fn load_runtime(path: &Path) -> Result<Option<LoadedIndex>> {
|
||||
let Some(runtime) = load::<LoadedIndex>(path)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
runtime.validate()?;
|
||||
Ok(Some(runtime))
|
||||
}
|
||||
|
||||
pub fn save_runtime(path: &Path, runtime: &LoadedIndex) -> Result<()> {
|
||||
runtime.validate()?;
|
||||
save(path, runtime)
|
||||
}
|
||||
|
||||
fn load<T>(path: &Path) -> Result<Option<T>>
|
||||
where
|
||||
T: serde::de::DeserializeOwned,
|
||||
{
|
||||
if !path.exists() {
|
||||
return Ok(None);
|
||||
}
|
||||
let bytes = fs::read(path).map_err(|error| Error::io(path, error))?;
|
||||
let (value, consumed): (T, usize) =
|
||||
bincode::serde::decode_from_slice(&bytes, bincode::config::standard())
|
||||
.map_err(|error| Error::CorruptIndex(error.to_string()))?;
|
||||
if consumed != bytes.len() {
|
||||
return Err(Error::CorruptIndex("cache has trailing bytes".into()));
|
||||
}
|
||||
Ok(Some(value))
|
||||
}
|
||||
|
||||
fn save<T>(path: &Path, value: &T) -> Result<()>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
let parent = path
|
||||
.parent()
|
||||
.ok_or_else(|| Error::InvalidRequest("snapshot path has no parent".into()))?;
|
||||
fs::create_dir_all(parent).map_err(|error| Error::io(parent, error))?;
|
||||
let temporary = parent.join(format!(".cache-{}-{}.tmp", std::process::id(), now_nanos()));
|
||||
let bytes = bincode::serde::encode_to_vec(value, bincode::config::standard())
|
||||
.map_err(|error| Error::Serialization(error.to_string()))?;
|
||||
let mut file = fs::File::create(&temporary).map_err(|error| Error::io(&temporary, error))?;
|
||||
file.write_all(&bytes)
|
||||
.and_then(|_| file.sync_all())
|
||||
.map_err(|error| Error::io(&temporary, error))?;
|
||||
if path.exists() {
|
||||
fs::remove_file(path).map_err(|error| Error::io(path, error))?;
|
||||
}
|
||||
fs::rename(&temporary, path).map_err(|error| Error::io(path, error))
|
||||
}
|
||||
|
||||
fn now_nanos() -> u128 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |value| value.as_nanos())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
config::INDEX_FORMAT_VERSION, index::IndexedFile, source::FileStamp, Chunk, ContentType,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn snapshot() -> IndexSnapshot {
|
||||
IndexSnapshot {
|
||||
format_version: INDEX_FORMAT_VERSION,
|
||||
source_identity: "source".into(),
|
||||
model_id: "test-model".into(),
|
||||
dimensions: 2,
|
||||
desired_chunk_bytes: 128,
|
||||
content: vec![ContentType::Code],
|
||||
files: vec![IndexedFile {
|
||||
path: "src/lib.rs".into(),
|
||||
stamp: FileStamp {
|
||||
modified_ns: 1,
|
||||
size: 4,
|
||||
},
|
||||
content_type: ContentType::Code,
|
||||
chunks: vec![Chunk {
|
||||
file_path: "src/lib.rs".into(),
|
||||
start_line: 1,
|
||||
end_line: 1,
|
||||
language: Some("rust".into()),
|
||||
content: "code".into(),
|
||||
}],
|
||||
definitions: Vec::new(),
|
||||
lexical_documents: vec![crate::index::lexical_document("code")],
|
||||
vectors: vec![127, 0],
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshots_round_trip_and_reject_trailing_corruption() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let path = directory.path().join("nested/index.bin");
|
||||
save_snapshot(&path, &snapshot()).unwrap();
|
||||
let loaded = load_snapshot(&path).unwrap().unwrap();
|
||||
assert!(loaded.files[0].chunks[0].content.is_empty());
|
||||
let mut bytes = fs::read(&path).unwrap();
|
||||
bytes.push(0xff);
|
||||
fs::write(&path, bytes).unwrap();
|
||||
assert!(matches!(load_snapshot(&path), Err(Error::CorruptIndex(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_indexes_round_trip() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let path = directory.path().join("nested/runtime.bin");
|
||||
let runtime = LoadedIndex::from_snapshot(snapshot()).unwrap();
|
||||
save_runtime(&path, &runtime).unwrap();
|
||||
let loaded = load_runtime(&path).unwrap().unwrap();
|
||||
assert_eq!(loaded.chunks.len(), 1);
|
||||
assert_eq!(loaded.vectors, vec![127, 0]);
|
||||
assert!(loaded
|
||||
.lexical
|
||||
.exact_symbol("missing", &loaded.chunks, 1)
|
||||
.is_empty());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user