From 803521383016b55214dce59b4420e6ea600bec3a Mon Sep 17 00:00:00 2001
From: Zack Jackson <25274700+ScriptedAlchemy@users.noreply.github.com>
Date: Mon, 9 Nov 2026 00:31:16 -0800
Subject: [PATCH] perf(sessions): isolate Codex directory witness optimization
Refs #4756
Publish the exact Codex-only source and regression blobs previously tested
in PR #4815 directly against master. This removes the Git fixture draft
branch from the merge prerequisite without rewriting the original history.
Skip redundant name enumeration on settled historical directories. Retain
native change evidence and conservative unsettled directory detection.
The change is partial: durable mid-scan continuation and affected-host CPU
and SQLite write-latency measurements remain outstanding.
---
.../src/runtime/hosts/codex.rs | 61 ++++++-
.../src/runtime/hosts/codex/tests.rs | 151 +++++++++++++++++-
2 files changed, 205 insertions(+), 7 deletions(-)
diff --git a/crates/tracedecay-sessions/src/runtime/hosts/codex.rs b/crates/tracedecay-sessions/src/runtime/hosts/codex.rs
index c75fba704f2..2ab2ff738ac 100644
--- a/crates/tracedecay-sessions/src/runtime/hosts/codex.rs
+++ b/crates/tracedecay-sessions/src/runtime/hosts/codex.rs
@@ -1056,7 +1056,7 @@ struct CodexDirectoryResume {
#[derive(Clone, Copy)]
struct CodexDirectoryWitness {
stat: [u8; 32],
- entries: [u8; 32],
+ entries: Option<[u8; 32]>,
}
#[derive(Clone)]
@@ -1735,6 +1735,12 @@ impl CodexSource {
} else {
index + 1
};
+ // The hot prefix was already checked earlier in this tick.
+ // Advance the rotating cursor through it without repeating
+ // directory stats or unsettled entry-name fingerprint scans.
+ if index < hot {
+ continue;
+ }
if let Some(directory) = idle.directories.get(index) {
if codex_directory_changed(&directory.path, directory.identity)? {
changed = true;
@@ -2578,11 +2584,23 @@ fn codex_directory_changed(
let Some(stored) = stored else {
return Ok(true);
};
- // An unsettled stat proves nothing either way; the entry names do.
+ // A different native identity proves the directory changed, even while
+ // the filesystem change clock is unsettled. Do not enumerate and sort its
+ // entries (or miss a metadata-only change with identical entry names).
+ if current.stat != stored.stat {
+ return Ok(true);
+ }
+ // A settled, identical stat proves no directory mutation.
if current.settled {
- return Ok(current.stat != stored.stat);
+ return Ok(false);
}
- Ok(directory_entry_fingerprint(path)? != stored.entries)
+ // An older settled directory did not need an entry-name witness.
+ // If its stat becomes unsettled, conservatively revalidate it rather
+ // than assuming that newly added rollouts were already discovered.
+ let Some(entries) = stored.entries else {
+ return Ok(true);
+ };
+ Ok(directory_entry_fingerprint(path)? != entries)
}
fn codex_directory_witness(path: &Path) -> TranscriptIngestResult