MCPcopy Create free account
hub / github.com/ScriptedAlchemy/tracedecay / read_changed_file

Function read_changed_file

src/sessions/source.rs:381–413  ·  view source on GitHub ↗

`ContentHash`** reader for full-file-rewrite JSON. Detects a change via `(content_hash64, mtime)` versus the stored cursor and, on change, returns the whole file so the caller can re-derive every message with deterministic ids. Idempotent upserts make re-adding unchanged messages a no-op. Returns `None` when the file cannot be read or is unchanged since the last run.

(path: &Path, prev: StoredCursor)

Source from the content-addressed store, hash-verified

379/// a no-op. Returns `None` when the file cannot be read or is unchanged since
380/// the last run.
381pub fn read_changed_file(path: &Path, prev: StoredCursor) -> Option<ChangedFile> {
382 let meta = match std::fs::metadata(path) {
383 Ok(meta) => meta,
384 Err(error) => {
385 log_source_skip(path, "stat transcript file", &error);
386 return None;
387 }
388 };
389 let mtime = file_mtime_secs(&meta);
390 let contents = match std::fs::read_to_string(path) {
391 Ok(contents) => contents,
392 Err(error) => {
393 log_source_skip(path, "read transcript file", &error);
394 return None;
395 }
396 };
397 let hash = content_hash64(&contents);
398
399 // Unchanged since last run (we have read it before and neither content hash
400 // nor mtime moved) -> nothing to do.
401 if prev.position == hash && prev.mtime == mtime && (prev.position != 0 || prev.mtime != 0) {
402 return None;
403 }
404
405 Some(ChangedFile {
406 contents,
407 new_cursor: StoredCursor {
408 position: hash,
409 mtime,
410 file_id: 0,
411 },
412 })
413}
414
415/// Like [`read_changed_file`], but treats `primary` as changed when either its
416/// own content hash moves or a companion sidecar file's hash moves. The stored

Calls 3

log_source_skipFunction · 0.85
content_hash64Function · 0.85
file_mtime_secsFunction · 0.70