feat(agent): Ed25519-signed checkpoints
Makes the README's "cryptographically verifiable memory" true. With HDF5Memory::set_signing_key(key), every checkpoint stores a signed manifest of the store: a SHA-256 per memory record (text, embedding as stored, channel, timestamp, session, tags, deleted flag, activation) in a Merkle tree, plus hashes of the settings (and WAL mark), sessions and knowledge graph. The signature, public key and manifest hashes go in /meta; the per-record hashes in /integrity/record_hashes, so HDF5Memory::verify(path, &public_key) can say which records changed, not just that something did. A forged manifest fails the signature. Decisions, as agreed: - the key is set on the open store and never persisted; - a signed store refuses to checkpoint without its key (MemoryError::SigningKeyRequired); remove_signature() is the deliberate way back to unsigned; - checkpoints only: saves still in the WAL are not covered, and verify reports how many there are. The hashes cover exactly what the file persists, in the form the loader returns it (strings lose trailing NULs; an empty WAL mark is not written), so untouched stores verify across any number of reopen and checkpoint cycles. MemoryError becomes #[non_exhaustive] (it already gains variants in this unreleased version). CLI: keygen (owner-only key file), --signing-key / CLAWHDF5_SIGNING_KEY on writing commands (create signs immediately), verify --public-key (JSON; exit 2 if not valid), `signed` in create/stats output. Tests: reopen/checkpoint cycles with awkward strings (f16 and f32), refusal without the key, wrong and rotated keys, eight kinds of edit each detected and located, a forged manifest, unsigned stores, NULs in text, and an edit made in place with h5py that verify pinpoints. Cost on tank (search_harness --signing-study --full, 3 runs): ~20% of a checkpoint (+9 ms at 10K, +89-112 ms at 100K), verify 18.6 ms / 247 ms, 32 bytes per record in the file. New deps ed25519-dalek, sha2, rand_core: pure Rust, the no-C check passes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
@@ -36,6 +36,7 @@ pub mod reranker;
|
||||
pub mod schema;
|
||||
pub mod search;
|
||||
pub mod session;
|
||||
pub mod signing;
|
||||
pub mod storage;
|
||||
mod store_lock;
|
||||
pub mod temporal;
|
||||
@@ -78,6 +79,7 @@ pub use session::{SessionCache, SessionEntry};
|
||||
// --- Error type ---
|
||||
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
pub enum MemoryError {
|
||||
Io(std::io::Error),
|
||||
Hdf5(String),
|
||||
@@ -88,6 +90,11 @@ pub enum MemoryError {
|
||||
/// A record the store cannot hold as given, e.g. an embedding value
|
||||
/// outside the half-precision range of a `float16` store.
|
||||
InvalidEntry(String),
|
||||
/// The store's checkpoints are signed and no signing key is set, so a
|
||||
/// checkpoint would leave it unsigned. Set the key with
|
||||
/// [`HDF5Memory::set_signing_key`], or drop the signature on purpose with
|
||||
/// [`HDF5Memory::remove_signature`].
|
||||
SigningKeyRequired(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for MemoryError {
|
||||
@@ -99,6 +106,7 @@ impl std::fmt::Display for MemoryError {
|
||||
MemoryError::NotFound(e) => write!(f, "not found: {e}"),
|
||||
MemoryError::Locked(e) => write!(f, "store is locked: {e}"),
|
||||
MemoryError::InvalidEntry(e) => write!(f, "invalid entry: {e}"),
|
||||
MemoryError::SigningKeyRequired(e) => write!(f, "signing key required: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -317,6 +325,12 @@ pub struct HDF5Memory {
|
||||
activations_dirty: bool,
|
||||
/// Opened with [`HDF5Memory::open_read_only`]: nothing may reach the disk.
|
||||
read_only: bool,
|
||||
/// Key that signs every checkpoint; never persisted. See
|
||||
/// [`HDF5Memory::set_signing_key`].
|
||||
signing_key: Option<signing::SigningKey>,
|
||||
/// Checkpoints of this store are signed: the file on disk is, or a key
|
||||
/// has been set. A checkpoint without a key is then refused.
|
||||
signed: bool,
|
||||
/// A WAL that `open()` could not read and moved aside; see
|
||||
/// [`HDF5Memory::quarantined_wal`].
|
||||
quarantined_wal: Option<PathBuf>,
|
||||
@@ -372,6 +386,8 @@ impl HDF5Memory {
|
||||
bm25_filter: bm25::TokenFilter::default(),
|
||||
activations_dirty: false,
|
||||
read_only: false,
|
||||
signing_key: None,
|
||||
signed: false,
|
||||
quarantined_wal: None,
|
||||
_lock: Some(lock),
|
||||
})
|
||||
@@ -550,6 +566,8 @@ impl HDF5Memory {
|
||||
bm25_filter: bm25::TokenFilter::default(),
|
||||
activations_dirty: false,
|
||||
read_only,
|
||||
signing_key: None,
|
||||
signed: checkpoint.signed,
|
||||
quarantined_wal,
|
||||
_lock: lock,
|
||||
})
|
||||
@@ -710,6 +728,39 @@ impl HDF5Memory {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sign every checkpoint from now on with `key` (Ed25519). The key is
|
||||
/// never written anywhere; set it again after every `open`. Once a store
|
||||
/// is signed, a checkpoint without the key is refused
|
||||
/// ([`MemoryError::SigningKeyRequired`]) rather than silently leaving it
|
||||
/// unsigned. Setting a different key re-signs the store under that key
|
||||
/// from the next checkpoint; a verifier trusting the old key will then
|
||||
/// reject it, which is the point. Call [`AgentMemory::flush_wal`] to sign
|
||||
/// right away.
|
||||
pub fn set_signing_key(&mut self, key: signing::SigningKey) {
|
||||
self.signing_key = Some(key);
|
||||
self.signed = true;
|
||||
}
|
||||
|
||||
/// Stop signing: the next checkpoint writes the store unsigned. The
|
||||
/// deliberate way out of [`MemoryError::SigningKeyRequired`].
|
||||
pub fn remove_signature(&mut self) {
|
||||
self.signing_key = None;
|
||||
self.signed = false;
|
||||
}
|
||||
|
||||
/// Checkpoints of this store are signed (on disk, or from the next
|
||||
/// checkpoint because a key has been set).
|
||||
pub fn is_signed(&self) -> bool {
|
||||
self.signed
|
||||
}
|
||||
|
||||
/// Check the checkpoint at `path` against the public key the caller
|
||||
/// trusts; see [`signing::verify_store`]. Reads the file only: it works
|
||||
/// on a store another process has open.
|
||||
pub fn verify(path: &Path, trusted: &signing::VerifyingKey) -> Result<signing::VerifyReport> {
|
||||
signing::verify_store(path, trusted)
|
||||
}
|
||||
|
||||
/// Flush current state to disk and truncate the WAL.
|
||||
///
|
||||
/// Every code path that persists the full cache to the .h5 file must
|
||||
@@ -725,10 +776,28 @@ impl HDF5Memory {
|
||||
// Record which WAL prefix this checkpoint contains, so a crash before
|
||||
// the truncate below can't replay those entries a second time.
|
||||
let wal_applied = self.wal.as_ref().map(|w| w.mark());
|
||||
let signature = match &self.signing_key {
|
||||
Some(key) => Some(signing::sign(
|
||||
key,
|
||||
&self.config,
|
||||
&self.cache,
|
||||
&self.sessions,
|
||||
&self.knowledge,
|
||||
wal_applied,
|
||||
)),
|
||||
None if self.signed => {
|
||||
return Err(MemoryError::SigningKeyRequired(format!(
|
||||
"{} is signed; set its signing key before a checkpoint \
|
||||
(saves so far are held in the WAL or in memory)",
|
||||
self.config.path.display()
|
||||
)));
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
// Written before the .h5 so a crash in between leaves a sidecar whose
|
||||
// generation matches no checkpoint (ignored), never the reverse.
|
||||
let ann_generation = self.persist_vector_index();
|
||||
storage::write_to_disk_with_meta(
|
||||
storage::write_to_disk_signed(
|
||||
&self.config.path,
|
||||
&self.config,
|
||||
&self.cache,
|
||||
@@ -737,7 +806,9 @@ impl HDF5Memory {
|
||||
&schema::CheckpointMeta {
|
||||
wal_applied,
|
||||
ann_generation,
|
||||
signed: signature.is_some(),
|
||||
},
|
||||
signature.as_ref(),
|
||||
)?;
|
||||
if let Some(ref mut w) = self.wal {
|
||||
w.truncate()?;
|
||||
|
||||
Reference in New Issue
Block a user