Move Epiphany state off JSON ledgers

This commit is contained in:
Meta Vens 2026-05-06 05:57:33 +01:00
commit 14c69bbef3
35 changed files with 2117 additions and 1703 deletions

View file

@ -0,0 +1,105 @@
use anyhow::Context;
use anyhow::Result;
use epiphany_core::agent_memory_status;
use epiphany_core::apply_agent_self_patch;
use epiphany_core::migrate_agent_memory_json_dir_to_cultcache;
use epiphany_core::project_agent_memory_to_json_dir;
use epiphany_core::review_agent_self_patch;
use epiphany_core::validate_agent_memory_store;
use std::env;
use std::fs;
use std::path::PathBuf;
fn main() -> Result<()> {
let mut args = env::args().skip(1);
let Some(command) = args.next() else {
print_usage();
std::process::exit(2);
};
match command.as_str() {
"migrate-json-dir" => {
let agent_dir = require_path_arg(&mut args, "--agent-dir")?;
let store = require_path_arg(&mut args, "--store")?;
print_json(&migrate_agent_memory_json_dir_to_cultcache(agent_dir, store)?)?;
}
"project-json-dir" => {
let store = require_path_arg(&mut args, "--store")?;
let output_dir = require_path_arg(&mut args, "--output-dir")?;
print_json(&project_agent_memory_to_json_dir(store, output_dir)?)?;
}
"status" => {
let store = require_path_arg(&mut args, "--store")?;
print_json(&agent_memory_status(store)?)?;
}
"validate" => {
let store = require_path_arg(&mut args, "--store")?;
let errors = validate_agent_memory_store(&store)?;
print_json(&serde_json::json!({
"ok": errors.is_empty(),
"store": store,
"errors": errors,
}))?;
if !errors.is_empty() {
std::process::exit(1);
}
}
"review-patch" => {
let store = require_path_arg(&mut args, "--store")?;
let role_id = require_string_arg(&mut args, "--role-id")?;
let patch = read_patch_arg(&require_string_arg(&mut args, "--patch")?)?;
print_json(&review_agent_self_patch(&role_id, &patch, store))?;
}
"apply-patch" => {
let store = require_path_arg(&mut args, "--store")?;
let role_id = require_string_arg(&mut args, "--role-id")?;
let patch = read_patch_arg(&require_string_arg(&mut args, "--patch")?)?;
let result = apply_agent_self_patch(&role_id, &patch, store)?;
let accepted = result.status == "accepted";
print_json(&result)?;
if !accepted {
std::process::exit(1);
}
}
_ => {
print_usage();
std::process::exit(2);
}
}
Ok(())
}
fn require_path_arg(args: &mut impl Iterator<Item = String>, name: &str) -> Result<PathBuf> {
Ok(PathBuf::from(require_string_arg(args, name)?))
}
fn require_string_arg(args: &mut impl Iterator<Item = String>, name: &str) -> Result<String> {
let Some(flag) = args.next() else {
anyhow::bail!("missing {name}");
};
if flag != name {
anyhow::bail!("expected {name}, got {flag}");
}
args.next().with_context(|| format!("missing value for {name}"))
}
fn read_patch_arg(value: &str) -> Result<serde_json::Value> {
let path = PathBuf::from(value);
if path.exists() {
let raw = fs::read_to_string(&path)
.with_context(|| format!("failed to read patch {}", path.display()))?;
return serde_json::from_str(&raw)
.with_context(|| format!("failed to decode patch {}", path.display()));
}
serde_json::from_str(value).context("failed to decode patch JSON")
}
fn print_json<T: serde::Serialize>(value: &T) -> Result<()> {
println!("{}", serde_json::to_string_pretty(value)?);
Ok(())
}
fn print_usage() {
eprintln!(
"usage: epiphany-agent-memory-store <migrate-json-dir|project-json-dir|status|validate|review-patch|apply-patch> ..."
);
}

View file

@ -0,0 +1,147 @@
use anyhow::Context;
use anyhow::Result;
use epiphany_core::EpiphanyBranchRecord;
use epiphany_core::EpiphanyLedgerEvidenceRecord;
use epiphany_core::add_state_branch;
use epiphany_core::append_state_evidence;
use epiphany_core::close_state_branch;
use epiphany_core::migrate_state_ledgers_to_cultcache;
use epiphany_core::project_state_ledgers_to_json;
use epiphany_core::state_ledger_status;
use std::collections::BTreeMap;
use std::env;
use std::path::PathBuf;
fn main() -> Result<()> {
let mut args = env::args().skip(1);
let Some(command) = args.next() else {
print_usage();
std::process::exit(2);
};
match command.as_str() {
"migrate-json" => {
let branches = require_path_arg(&mut args, "--branches")?;
let evidence = require_path_arg(&mut args, "--evidence")?;
let store = require_path_arg(&mut args, "--store")?;
print_json(&migrate_state_ledgers_to_cultcache(branches, evidence, store)?)?;
}
"project-json" => {
let store = require_path_arg(&mut args, "--store")?;
let branches = require_path_arg(&mut args, "--branches")?;
let evidence = require_path_arg(&mut args, "--evidence")?;
print_json(&project_state_ledgers_to_json(store, branches, evidence)?)?;
}
"status" => {
let store = require_path_arg(&mut args, "--store")?;
print_json(&state_ledger_status(store)?)?;
}
"add-evidence" => {
let store = require_path_arg(&mut args, "--store")?;
let record = EpiphanyLedgerEvidenceRecord {
ts: require_string_arg(&mut args, "--ts")?,
evidence_type: require_string_arg(&mut args, "--type")?,
status: require_string_arg(&mut args, "--status")?,
note: require_string_arg(&mut args, "--note")?,
branch: optional_string_arg(&mut args, "--branch")?,
extra: BTreeMap::new(),
};
print_json(&append_state_evidence(store, record)?)?;
}
"add-branch" => {
let store = require_path_arg(&mut args, "--store")?;
let id = require_string_arg(&mut args, "--id")?;
let hypothesis = require_string_arg(&mut args, "--hypothesis")?;
let (artifacts, note) = collect_branch_tail(&mut args)?;
let branch = EpiphanyBranchRecord {
id,
hypothesis,
status: "active".to_string(),
artifacts,
notes: note.unwrap_or_default(),
extra: BTreeMap::new(),
};
print_json(&add_state_branch(store, branch)?)?;
}
"close-branch" => {
let store = require_path_arg(&mut args, "--store")?;
let id = require_string_arg(&mut args, "--id")?;
let status = require_string_arg(&mut args, "--status")?;
let note = optional_string_arg(&mut args, "--note")?;
print_json(&close_state_branch(store, &id, &status, note)?)?;
}
_ => {
print_usage();
std::process::exit(2);
}
}
Ok(())
}
fn require_path_arg(args: &mut impl Iterator<Item = String>, name: &str) -> Result<PathBuf> {
Ok(PathBuf::from(require_string_arg(args, name)?))
}
fn require_string_arg(args: &mut impl Iterator<Item = String>, name: &str) -> Result<String> {
let Some(flag) = args.next() else {
anyhow::bail!("missing {name}");
};
if flag != name {
anyhow::bail!("expected {name}, got {flag}");
}
args.next().with_context(|| format!("missing value for {name}"))
}
fn optional_string_arg(args: &mut impl Iterator<Item = String>, name: &str) -> Result<Option<String>> {
let remaining = args.collect::<Vec<_>>();
let mut found = None;
let mut index = 0;
while index < remaining.len() {
if remaining[index] == name {
found = remaining.get(index + 1).cloned();
index += 2;
} else {
anyhow::bail!("unexpected argument {}", remaining[index]);
}
}
Ok(found)
}
fn collect_branch_tail(args: &mut impl Iterator<Item = String>) -> Result<(Vec<String>, Option<String>)> {
let remaining = args.collect::<Vec<_>>();
let mut artifacts = Vec::new();
let mut note = None;
let mut index = 0;
while index < remaining.len() {
if remaining[index] == "--artifact" {
artifacts.push(
remaining
.get(index + 1)
.cloned()
.context("missing value for --artifact")?,
);
index += 2;
} else if remaining[index] == "--note" {
note = Some(
remaining
.get(index + 1)
.cloned()
.context("missing value for --note")?,
);
index += 2;
} else {
anyhow::bail!("unexpected argument {}", remaining[index]);
}
}
Ok((artifacts, note))
}
fn print_json<T: serde::Serialize>(value: &T) -> Result<()> {
println!("{}", serde_json::to_string_pretty(value)?);
Ok(())
}
fn print_usage() {
eprintln!(
"usage: epiphany-state-ledger-store <migrate-json|project-json|status|add-evidence|add-branch|close-branch> ..."
);
}