Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(git): clear index before staging so OKF migration can't crash-loop (#594)

AkitaOnRails28726272f766
diff --git a/CHANGELOG.md b/CHANGELOG.md
index aa97ef85..b8ceb1b5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
+- The OKF v0.2 migration no longer crash-loops on startup with a
+ libgit2 `invalid object specified … class=Tree` error that left the
+ server unable to boot (#594). The wiki commit re-hashed files through
+ libgit2's index, whose stat cache could trust a cached blob OID absent
+ from the object database (a store carried across libgit2/git versions,
+ or an interrupted earlier operation) — `write_tree` then aborted and,
+ because the migration commits through that path, every restart failed.
+ The commit now clears the index before staging so every file is
+ re-hashed from the working tree. (The Docker runtime ships no `git`,
+ so the fix is in-library, not a CLI fallback.)
- `as_of` time-travel queries and the entity retrieval stream now work
on real stores. Both read the entity index, which was populated only
from an LLM consolidator's `entities:` frontmatter — absent on the
diff --git a/crates/ai-memory-wiki/src/git.rs b/crates/ai-memory-wiki/src/git.rs
index 046a6c01..d3a70952 100644
--- a/crates/ai-memory-wiki/src/git.rs
+++ b/crates/ai-memory-wiki/src/git.rs
@@ -85,8 +85,20 @@ impl GitAdapter {
fn commit_all_git2(&self, message: &str) -> Result<Option<git2::Oid>, CommitGit2Error> {
let repo = Repository::open(&self.root).map_err(CommitGit2Error::Open)?;
- // Stage everything (including deletions).
+ // Stage everything (including deletions). Clear the index first so
+ // every entry is re-hashed from the working tree: libgit2 keeps a
+ // stat cache and will skip re-reading a file whose size/mtime look
+ // unchanged, trusting the cached blob OID. When that cached OID is
+ // stale or its blob is absent from the object database — which a
+ // store carried across libgit2/git versions or an interrupted
+ // earlier operation can leave behind — `write_tree` below aborts
+ // with "invalid object specified … class=Tree" and, since the wiki
+ // migration commits through this path, the server crash-loops and
+ // never starts (#594). Clearing drops the stat cache, so `add_all`
+ // re-hashes each working-tree file into the ODB and every entry the
+ // tree references is guaranteed present.
let mut index = repo.index().map_err(CommitGit2Error::Other)?;
+ index.clear().map_err(CommitGit2Error::Other)?;
index
.add_all(["*"].iter(), IndexAddOption::DEFAULT, None)
.map_err(CommitGit2Error::Other)?;
@@ -476,6 +488,41 @@ mod tests {
assert_eq!(adapter.commit_count(), 1);
}
+ /// #594 hardening: the commit re-hashes every file from the working
+ /// tree rather than trusting cached index blob OIDs. A store carried
+ /// across libgit2/git versions (or an interrupted operation) can leave
+ /// the index's stat cache pointing at a blob absent from the object
+ /// database; `write_tree` then aborts with "invalid object specified"
+ /// and the migration crash-loops. The fix clears the index before
+ /// `add_all`. We can't fabricate a missing-blob entry through git2's
+ /// safe API (it validates the OID on `add`), so we verify the
+ /// behaviour the clear guarantees: the *current* working-tree content
+ /// is what gets committed, even after the same path was committed
+ /// before — i.e. staging always reflects disk, never a cache.
+ #[test]
+ fn commit_all_commits_current_working_tree_content() {
+ let tmp = tempdir();
+ let root = tmp.path().join("wiki");
+ let adapter = GitAdapter::open_or_init(&root).unwrap();
+
+ std::fs::write(root.join("log-2026-07.md"), "v1").unwrap();
+ adapter.commit_all("v1").unwrap();
+
+ // Overwrite in place and commit again: the committed blob must be v2.
+ std::fs::write(root.join("log-2026-07.md"), "v2 rewritten").unwrap();
+ let oid = adapter.commit_all("v2").unwrap();
+ assert!(oid.is_some(), "a content change must produce a commit");
+
+ let committed = adapter
+ .file_at_rev("HEAD", Path::new("log-2026-07.md"))
+ .unwrap();
+ assert_eq!(
+ committed, b"v2 rewritten",
+ "commit must reflect the working-tree file, re-hashed from disk"
+ );
+ assert_eq!(adapter.commit_count(), 2);
+ }
+
#[test]
fn commit_all_captures_deletes_too() {
let tmp = tempdir();