Replace state tools with Rust binaries
This commit is contained in:
parent
14c69bbef3
commit
fc0e9a3594
8 changed files with 580 additions and 605 deletions
328
epiphany-core/src/bin/epiphany-prepare-compaction.rs
Normal file
328
epiphany-core/src/bin/epiphany-prepare-compaction.rs
Normal file
|
|
@ -0,0 +1,328 @@
|
|||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use epiphany_core::EpiphanyLedgerEvidenceRecord;
|
||||
use epiphany_core::load_state_ledger;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Finding {
|
||||
level: &'static str,
|
||||
message: String,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let strict = env::args().skip(1).any(|arg| arg == "--strict");
|
||||
let root = env::current_dir().context("failed to resolve current directory")?;
|
||||
let state_dir = root.join("state");
|
||||
let notes_dir = root.join("notes");
|
||||
let map_path = state_dir.join("map.yaml");
|
||||
let scratch_path = state_dir.join("scratch.md");
|
||||
let ledger_path = state_dir.join("ledgers.msgpack");
|
||||
let handoff_path = notes_dir.join("fresh-workspace-handoff.md");
|
||||
let plan_path = notes_dir.join("epiphany-fork-implementation-plan.md");
|
||||
let algo_map_path = notes_dir.join("epiphany-current-algorithmic-map.md");
|
||||
let agents_path = root.join("AGENTS.md");
|
||||
|
||||
let mut findings = Vec::new();
|
||||
for path in [
|
||||
&map_path,
|
||||
&scratch_path,
|
||||
&ledger_path,
|
||||
&handoff_path,
|
||||
&plan_path,
|
||||
&algo_map_path,
|
||||
&agents_path,
|
||||
] {
|
||||
if path.exists() {
|
||||
findings.push(ok(format!("found {}", relative(&root, path))));
|
||||
} else {
|
||||
findings.push(error(format!("missing {}", relative(&root, path))));
|
||||
}
|
||||
}
|
||||
|
||||
add_content_checks(
|
||||
&mut findings,
|
||||
&map_path,
|
||||
&scratch_path,
|
||||
&ledger_path,
|
||||
);
|
||||
add_handoff_checks(&mut findings, &handoff_path);
|
||||
add_agents_checks(&mut findings, &agents_path);
|
||||
let (status, log) = match (run_git(&root, ["status", "--short", "--branch"]), run_git(&root, ["log", "--oneline", "-5"])) {
|
||||
(Ok(status), Ok(log)) => {
|
||||
if status.lines().skip(1).any(|line| !line.trim().is_empty()) {
|
||||
findings.push(warn("git worktree has uncommitted changes; commit or explain before compaction"));
|
||||
} else {
|
||||
findings.push(ok("git worktree is clean"));
|
||||
}
|
||||
(status, log)
|
||||
}
|
||||
_ => {
|
||||
findings.push(error("git check failed"));
|
||||
("(git status unavailable)".to_string(), "(git log unavailable)".to_string())
|
||||
}
|
||||
};
|
||||
|
||||
let latest_evidence = load_state_ledger(&ledger_path)
|
||||
.ok()
|
||||
.and_then(|entry| entry.evidence.last().cloned());
|
||||
println!(
|
||||
"{}",
|
||||
render_report(
|
||||
&root,
|
||||
&map_path,
|
||||
&findings,
|
||||
&status,
|
||||
&log,
|
||||
latest_evidence.as_ref(),
|
||||
)?
|
||||
);
|
||||
|
||||
let has_error = findings.iter().any(|finding| finding.level == "error");
|
||||
let has_warning = findings.iter().any(|finding| finding.level == "warn");
|
||||
if has_error || (strict && has_warning) {
|
||||
std::process::exit(1);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_content_checks(
|
||||
findings: &mut Vec<Finding>,
|
||||
map_path: &Path,
|
||||
scratch_path: &Path,
|
||||
ledger_path: &Path,
|
||||
) {
|
||||
match extract_map_field(map_path, "summary") {
|
||||
Ok(Some(_)) => findings.push(ok("state/map.yaml has current_status.summary")),
|
||||
Ok(None) => findings.push(error("state/map.yaml is missing current_status.summary")),
|
||||
Err(err) => findings.push(error(format!("map summary check failed: {err}"))),
|
||||
}
|
||||
match extract_map_field(map_path, "next_action") {
|
||||
Ok(Some(_)) => findings.push(ok("state/map.yaml has current_status.next_action")),
|
||||
Ok(None) => findings.push(error("state/map.yaml is missing current_status.next_action")),
|
||||
Err(err) => findings.push(error(format!("map next_action check failed: {err}"))),
|
||||
}
|
||||
match extract_active_subgoals(map_path) {
|
||||
Ok(subgoals) if !subgoals.is_empty() => {
|
||||
findings.push(ok(format!("state/map.yaml has {} active subgoal(s)", subgoals.len())));
|
||||
}
|
||||
Ok(_) => findings.push(warn("state/map.yaml has no active_subgoals entries")),
|
||||
Err(err) => findings.push(error(format!("active_subgoals check failed: {err}"))),
|
||||
}
|
||||
match current_scratch_subgoal(scratch_path) {
|
||||
Ok(Some(value)) if value == "No active scratch subgoal." => {
|
||||
findings.push(ok("state/scratch.md has no stale active scratch subgoal"));
|
||||
}
|
||||
Ok(Some(value)) => findings.push(warn(format!("state/scratch.md has active scratch subgoal: {value}"))),
|
||||
Ok(None) => findings.push(warn("state/scratch.md has no Current Subgoal value")),
|
||||
Err(err) => findings.push(error(format!("scratch check failed: {err}"))),
|
||||
}
|
||||
match load_state_ledger(ledger_path) {
|
||||
Ok(entry) => {
|
||||
let active = entry
|
||||
.branches
|
||||
.iter()
|
||||
.filter(|branch| branch.status == "active")
|
||||
.count();
|
||||
findings.push(ok(format!(
|
||||
"state/ledgers.msgpack parses ({} evidence record(s))",
|
||||
entry.evidence.len()
|
||||
)));
|
||||
findings.push(ok(format!("state/ledgers.msgpack has {active} active branch(es)")));
|
||||
}
|
||||
Err(err) => findings.push(error(format!("state ledger parse failed: {err}"))),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_handoff_checks(findings: &mut Vec<Finding>, handoff_path: &Path) {
|
||||
let Ok(text) = fs::read_to_string(handoff_path) else {
|
||||
findings.push(error("handoff check failed"));
|
||||
return;
|
||||
};
|
||||
if text.contains("Current branch before") || text.contains("Current HEAD before") {
|
||||
findings.push(error("handoff embeds an exact branch or HEAD snapshot; use git commands instead"));
|
||||
} else {
|
||||
findings.push(ok("handoff avoids exact branch/HEAD snapshots"));
|
||||
}
|
||||
for phrase in [
|
||||
"Do not continue implementation automatically from a rehydrate-only request.",
|
||||
"Do not trust this file for the exact live HEAD.",
|
||||
"Immediate Re-entry Instruction",
|
||||
] {
|
||||
if text.contains(phrase) {
|
||||
findings.push(ok(format!("handoff contains: {phrase}")));
|
||||
} else {
|
||||
findings.push(warn(format!("handoff missing: {phrase}")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_agents_checks(findings: &mut Vec<Finding>, agents_path: &Path) {
|
||||
let Ok(text) = fs::read_to_string(agents_path) else {
|
||||
findings.push(error("AGENTS check failed"));
|
||||
return;
|
||||
};
|
||||
if text.contains("epiphany-prepare-compaction") {
|
||||
findings.push(ok("AGENTS.md tells agents to use the compaction helper"));
|
||||
} else {
|
||||
findings.push(error("AGENTS.md does not mention epiphany-prepare-compaction"));
|
||||
}
|
||||
if text.to_lowercase().contains("prepare for imminent compaction") {
|
||||
findings.push(ok("AGENTS.md names the imminent-compaction trigger"));
|
||||
} else {
|
||||
findings.push(warn("AGENTS.md does not name the imminent-compaction trigger phrase"));
|
||||
}
|
||||
}
|
||||
|
||||
fn render_report(
|
||||
root: &Path,
|
||||
map_path: &Path,
|
||||
findings: &[Finding],
|
||||
status: &str,
|
||||
log: &str,
|
||||
latest: Option<&EpiphanyLedgerEvidenceRecord>,
|
||||
) -> Result<String> {
|
||||
let ok_count = findings.iter().filter(|finding| finding.level == "ok").count();
|
||||
let warn_count = findings.iter().filter(|finding| finding.level == "warn").count();
|
||||
let error_count = findings.iter().filter(|finding| finding.level == "error").count();
|
||||
let mut lines = vec![
|
||||
"Epiphany pre-compaction persistence check".to_string(),
|
||||
format!("Workspace: {}", root.display()),
|
||||
format!("Findings: {ok_count} ok, {warn_count} warn, {error_count} error"),
|
||||
String::new(),
|
||||
"Git status:".to_string(),
|
||||
status.to_string(),
|
||||
String::new(),
|
||||
"Recent commits:".to_string(),
|
||||
log.to_string(),
|
||||
String::new(),
|
||||
format!(
|
||||
"Summary: {}",
|
||||
extract_map_field(map_path, "summary")?.unwrap_or_else(|| "(missing)".to_string())
|
||||
),
|
||||
format!(
|
||||
"Next action: {}",
|
||||
extract_map_field(map_path, "next_action")?.unwrap_or_else(|| "(missing)".to_string())
|
||||
),
|
||||
];
|
||||
let subgoals = extract_active_subgoals(map_path)?;
|
||||
if !subgoals.is_empty() {
|
||||
lines.push("Active subgoals:".to_string());
|
||||
lines.extend(subgoals.into_iter().map(|item| format!("- {item}")));
|
||||
}
|
||||
if let Some(latest) = latest {
|
||||
lines.extend([
|
||||
String::new(),
|
||||
"Latest distilled evidence:".to_string(),
|
||||
format!(
|
||||
"- {} {}/{}: {}",
|
||||
latest.ts, latest.evidence_type, latest.status, latest.note
|
||||
),
|
||||
]);
|
||||
}
|
||||
lines.push(String::new());
|
||||
lines.push("Findings:".to_string());
|
||||
for finding in findings {
|
||||
lines.push(format!("[{}] {}", finding.level.to_uppercase(), finding.message));
|
||||
}
|
||||
lines.extend([
|
||||
String::new(),
|
||||
"Pre-compaction checklist:".to_string(),
|
||||
"- Update state/map.yaml only if current understanding changed.".to_string(),
|
||||
"- Refresh notes/fresh-workspace-handoff.md if re-entry instructions changed.".to_string(),
|
||||
"- Add distilled evidence only for a belief-changing lesson, verification, rejected path, or scar.".to_string(),
|
||||
"- Keep exact branch/HEAD out of handoff prose; git commands own volatile truth.".to_string(),
|
||||
"- Commit completed persistence changes, or state why the worktree must stay dirty.".to_string(),
|
||||
"- Re-run this helper after edits before yielding to compaction.".to_string(),
|
||||
]);
|
||||
Ok(lines.join("\n"))
|
||||
}
|
||||
|
||||
fn extract_map_field(path: &Path, name: &str) -> Result<Option<String>> {
|
||||
let prefix = format!(" {name}:");
|
||||
for line in fs::read_to_string(path)?.lines() {
|
||||
if line.starts_with(&prefix) {
|
||||
return Ok(Some(line.split_once(':').map(|(_, value)| value.trim().to_string()).unwrap_or_default()));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn extract_active_subgoals(path: &Path) -> Result<Vec<String>> {
|
||||
let text = fs::read_to_string(path)?;
|
||||
let mut results = Vec::new();
|
||||
let mut in_section = false;
|
||||
for line in text.lines() {
|
||||
if !in_section {
|
||||
if line.trim() == "active_subgoals:" {
|
||||
in_section = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = line.strip_prefix(" - ") {
|
||||
results.push(rest.trim().to_string());
|
||||
} else if !line.is_empty() && !line.starts_with(' ') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
fn current_scratch_subgoal(path: &Path) -> Result<Option<String>> {
|
||||
let lines = fs::read_to_string(path)?;
|
||||
let mut found_header = false;
|
||||
for line in lines.lines() {
|
||||
if found_header {
|
||||
let stripped = line.trim();
|
||||
if !stripped.is_empty() {
|
||||
return Ok(Some(stripped.to_string()));
|
||||
}
|
||||
} else if line.trim() == "## Current Subgoal" {
|
||||
found_header = true;
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn run_git<const N: usize>(root: &Path, args: [&str; N]) -> Result<String> {
|
||||
let output = Command::new("git")
|
||||
.args(args)
|
||||
.current_dir(root)
|
||||
.output()
|
||||
.context("failed to run git")?;
|
||||
if !output.status.success() {
|
||||
anyhow::bail!("{}", String::from_utf8_lossy(&output.stderr));
|
||||
}
|
||||
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
|
||||
}
|
||||
|
||||
fn relative(root: &Path, path: &Path) -> String {
|
||||
path.strip_prefix(root)
|
||||
.unwrap_or(path)
|
||||
.display()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn ok(message: impl Into<String>) -> Finding {
|
||||
Finding {
|
||||
level: "ok",
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn warn(message: impl Into<String>) -> Finding {
|
||||
Finding {
|
||||
level: "warn",
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn error(message: impl Into<String>) -> Finding {
|
||||
Finding {
|
||||
level: "error",
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
237
epiphany-core/src/bin/epiphany-state.rs
Normal file
237
epiphany-core/src/bin/epiphany-state.rs
Normal file
|
|
@ -0,0 +1,237 @@
|
|||
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::load_state_ledger;
|
||||
use epiphany_core::migrate_state_ledgers_to_cultcache;
|
||||
use std::collections::BTreeMap;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let root = env::current_dir().context("failed to resolve current directory")?;
|
||||
let state_dir = root.join("state");
|
||||
let map_path = state_dir.join("map.yaml");
|
||||
let ledger_store = state_dir.join("ledgers.msgpack");
|
||||
let mut args = env::args().skip(1);
|
||||
let Some(command) = args.next() else {
|
||||
print_usage();
|
||||
std::process::exit(2);
|
||||
};
|
||||
match command.as_str() {
|
||||
"status" => print_status(&root, &map_path, &ledger_store)?,
|
||||
"migrate-json" => {
|
||||
let mut branches = None;
|
||||
let mut evidence = None;
|
||||
let mut store = None;
|
||||
parse_named_args(&mut args, |name, value| match name {
|
||||
"--branches" => {
|
||||
branches = Some(PathBuf::from(value));
|
||||
Ok(())
|
||||
}
|
||||
"--evidence" => {
|
||||
evidence = Some(PathBuf::from(value));
|
||||
Ok(())
|
||||
}
|
||||
"--store" => {
|
||||
store = Some(PathBuf::from(value));
|
||||
Ok(())
|
||||
}
|
||||
other => anyhow::bail!("unexpected argument {other}"),
|
||||
})?;
|
||||
let branches = branches.unwrap_or_else(|| state_dir.join("branches.json"));
|
||||
let evidence = evidence.unwrap_or_else(|| state_dir.join("evidence.jsonl"));
|
||||
let store = store.unwrap_or_else(|| ledger_store.clone());
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&migrate_state_ledgers_to_cultcache(
|
||||
branches, evidence, store
|
||||
)?)?
|
||||
);
|
||||
}
|
||||
"add-evidence" => {
|
||||
let mut evidence_type = None;
|
||||
let mut status = None;
|
||||
let mut note = None;
|
||||
let mut branch = None;
|
||||
parse_named_args(&mut args, |name, value| match name {
|
||||
"--type" => {
|
||||
evidence_type = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
"--status" => {
|
||||
status = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
"--note" => {
|
||||
note = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
"--branch" => {
|
||||
branch = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
other => anyhow::bail!("unexpected argument {other}"),
|
||||
})?;
|
||||
append_state_evidence(
|
||||
&ledger_store,
|
||||
EpiphanyLedgerEvidenceRecord {
|
||||
ts: chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, false),
|
||||
evidence_type: evidence_type.context("missing --type")?,
|
||||
status: status.context("missing --status")?,
|
||||
note: note.context("missing --note")?,
|
||||
branch,
|
||||
extra: BTreeMap::new(),
|
||||
},
|
||||
)?;
|
||||
println!("Appended evidence record.");
|
||||
}
|
||||
"add-branch" => {
|
||||
let mut id = None;
|
||||
let mut hypothesis = None;
|
||||
let mut artifacts = Vec::new();
|
||||
let mut note = None;
|
||||
parse_named_args(&mut args, |name, value| match name {
|
||||
"--id" => {
|
||||
id = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
"--hypothesis" => {
|
||||
hypothesis = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
"--artifact" => {
|
||||
artifacts.push(value);
|
||||
Ok(())
|
||||
}
|
||||
"--note" => {
|
||||
note = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
other => anyhow::bail!("unexpected argument {other}"),
|
||||
})?;
|
||||
let id = id.context("missing --id")?;
|
||||
add_state_branch(
|
||||
&ledger_store,
|
||||
EpiphanyBranchRecord {
|
||||
id: id.clone(),
|
||||
hypothesis: hypothesis.context("missing --hypothesis")?,
|
||||
status: "active".to_string(),
|
||||
artifacts,
|
||||
notes: note.unwrap_or_default(),
|
||||
extra: BTreeMap::new(),
|
||||
},
|
||||
)?;
|
||||
println!("Added branch '{id}'.");
|
||||
}
|
||||
"close-branch" => {
|
||||
let mut id = None;
|
||||
let mut status = None;
|
||||
let mut note = None;
|
||||
parse_named_args(&mut args, |name, value| match name {
|
||||
"--id" => {
|
||||
id = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
"--status" => {
|
||||
status = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
"--note" => {
|
||||
note = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
other => anyhow::bail!("unexpected argument {other}"),
|
||||
})?;
|
||||
let id = id.context("missing --id")?;
|
||||
let status = status.context("missing --status")?;
|
||||
close_state_branch(&ledger_store, &id, &status, note)?;
|
||||
println!("Updated branch '{id}' to status '{status}'.");
|
||||
}
|
||||
_ => {
|
||||
print_usage();
|
||||
std::process::exit(2);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_status(root: &Path, map_path: &Path, ledger_store: &Path) -> Result<()> {
|
||||
let entry = load_state_ledger(ledger_store)?;
|
||||
let active = entry
|
||||
.branches
|
||||
.iter()
|
||||
.filter(|branch| branch.status == "active")
|
||||
.count();
|
||||
println!("Workspace: {}", root.display());
|
||||
println!(
|
||||
"Summary: {}",
|
||||
extract_map_field(map_path, "summary")?.unwrap_or_else(|| "(missing)".to_string())
|
||||
);
|
||||
println!(
|
||||
"Next action: {}",
|
||||
extract_map_field(map_path, "next_action")?.unwrap_or_else(|| "(missing)".to_string())
|
||||
);
|
||||
println!("Active branches: {active} / {}", entry.branches.len());
|
||||
println!("Evidence records: {}", entry.evidence.len());
|
||||
let subgoals = extract_active_subgoals(map_path)?;
|
||||
if !subgoals.is_empty() {
|
||||
println!("Active subgoals:");
|
||||
for item in subgoals {
|
||||
println!("- {item}");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extract_map_field(path: &Path, name: &str) -> Result<Option<String>> {
|
||||
let prefix = format!(" {name}:");
|
||||
for line in fs::read_to_string(path)?.lines() {
|
||||
if line.starts_with(&prefix) {
|
||||
return Ok(Some(line.split_once(':').map(|(_, value)| value.trim().to_string()).unwrap_or_default()));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn extract_active_subgoals(path: &Path) -> Result<Vec<String>> {
|
||||
let text = fs::read_to_string(path)?;
|
||||
let mut results = Vec::new();
|
||||
let mut in_section = false;
|
||||
for line in text.lines() {
|
||||
if !in_section {
|
||||
if line.trim() == "active_subgoals:" {
|
||||
in_section = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = line.strip_prefix(" - ") {
|
||||
results.push(rest.trim().to_string());
|
||||
} else if !line.is_empty() && !line.starts_with(' ') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
fn parse_named_args(
|
||||
args: &mut impl Iterator<Item = String>,
|
||||
mut handle: impl FnMut(&str, String) -> Result<()>,
|
||||
) -> Result<()> {
|
||||
while let Some(name) = args.next() {
|
||||
let value = args
|
||||
.next()
|
||||
.with_context(|| format!("missing value for {name}"))?;
|
||||
handle(&name, value)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_usage() {
|
||||
eprintln!("usage: epiphany-state <status|migrate-json|add-evidence|add-branch|close-branch> ...");
|
||||
}
|
||||
Reference in a new issue