From fc0e9a3594b57e246c8828f279c0983cc004c830 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 6 May 2026 06:14:13 +0100 Subject: [PATCH] Replace state tools with Rust binaries --- AGENTS.md | 20 +- .../src/bin/epiphany-prepare-compaction.rs | 328 ++++++++++++++++ epiphany-core/src/bin/epiphany-state.rs | 237 ++++++++++++ notes/fresh-workspace-handoff.md | 6 +- state/ledgers.msgpack | Bin 38510 -> 38798 bytes state/map.yaml | 4 +- tools/epiphany_prepare_compaction.py | 353 ------------------ tools/epiphany_state.py | 237 ------------ 8 files changed, 580 insertions(+), 605 deletions(-) create mode 100644 epiphany-core/src/bin/epiphany-prepare-compaction.rs create mode 100644 epiphany-core/src/bin/epiphany-state.rs delete mode 100644 tools/epiphany_prepare_compaction.py delete mode 100644 tools/epiphany_state.py diff --git a/AGENTS.md b/AGENTS.md index 8ff7465c..5347b06f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -24,17 +24,17 @@ The motivating failure mode is that an agent can make many plausible local edits - Handoff summary: `E:\Projects\EpiphanyAgent\notes\fresh-workspace-handoff.md` - Epiphany algorithmic map: `E:\Projects\EpiphanyAgent\notes\epiphany-current-algorithmic-map.md` - Epiphany safety architecture: `E:\Projects\EpiphanyAgent\notes\epiphany-safety-architecture.md` -- State CLI: `E:\Projects\EpiphanyAgent\tools\epiphany_state.py` -- Pre-compaction helper: `E:\Projects\EpiphanyAgent\tools\epiphany_prepare_compaction.py` +- State CLI: `cargo run --manifest-path E:\Projects\EpiphanyAgent\epiphany-core\Cargo.toml --bin epiphany-state -- ...` +- Pre-compaction helper: `cargo run --manifest-path E:\Projects\EpiphanyAgent\epiphany-core\Cargo.toml --bin epiphany-prepare-compaction -- ...` ## Useful Commands -Use the bundled Python runtime if `python` is not on PATH: +Use the native Rust tools for state and compaction: ```powershell -& 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_state.py' status -& 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_state.py' add-evidence --type research --status ok --note '...' -& 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_prepare_compaction.py' +cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-state -- status +cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-state -- add-evidence --type research --status ok --note '...' +cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-prepare-compaction -- ``` Useful Codex repo searches: @@ -56,13 +56,13 @@ On fresh session load, do this before wandering off into implementation: - `notes/epiphany-fork-implementation-plan.md` - `notes/epiphany-safety-architecture.md` when the task touches capability growth, autonomy, permissions, governance, or deployment authority 2. run: - - `& 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_state.py' status` + - `cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-state -- status` 3. restate the current next action from the persisted state before starting edits 4. if the user only asked to rehydrate or reorient, stop after orientation and wait for an explicit continue instruction instead of treating the persisted next action as permission to start coding After compaction, resume, or any suspicious loss of continuity: -1. rerun `epiphany_state.py status` +1. rerun `cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-state -- status` 2. reread `state/map.yaml` and `notes/fresh-workspace-handoff.md` 3. treat the persisted next action as authoritative unless fresh evidence in the repo contradicts it @@ -76,10 +76,10 @@ Do not wait for the blackout and then act surprised. When the user says to prepare for imminent compaction: -1. run `tools/epiphany_prepare_compaction.py` before editing persistence surfaces +1. run `cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-prepare-compaction --` before editing persistence surfaces 2. use its warnings as the checklist for map, handoff, scratch, evidence, and git hygiene 3. update only the state that actually needs to change -4. run `tools/epiphany_prepare_compaction.py` again after edits +4. run `cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-prepare-compaction --` again after edits 5. fix errors, address warnings, and commit the completed persistence pass unless the work is deliberately mid-surgery ## Operating Discipline diff --git a/epiphany-core/src/bin/epiphany-prepare-compaction.rs b/epiphany-core/src/bin/epiphany-prepare-compaction.rs new file mode 100644 index 00000000..9020d85c --- /dev/null +++ b/epiphany-core/src/bin/epiphany-prepare-compaction.rs @@ -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, + 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, 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, 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 { + 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> { + 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> { + 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> { + 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(root: &Path, args: [&str; N]) -> Result { + 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) -> Finding { + Finding { + level: "ok", + message: message.into(), + } +} + +fn warn(message: impl Into) -> Finding { + Finding { + level: "warn", + message: message.into(), + } +} + +fn error(message: impl Into) -> Finding { + Finding { + level: "error", + message: message.into(), + } +} diff --git a/epiphany-core/src/bin/epiphany-state.rs b/epiphany-core/src/bin/epiphany-state.rs new file mode 100644 index 00000000..781e5a26 --- /dev/null +++ b/epiphany-core/src/bin/epiphany-state.rs @@ -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> { + 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> { + 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, + 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 ..."); +} diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 30d95145..db49bb67 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -12,14 +12,14 @@ smoke artifacts, and the distilled `state/ledgers.msgpack` evidence ledger; exac From the repo root: ```powershell -& 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_state.py' status +cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-state -- status Get-Content '.\state\map.yaml' Get-Content '.\notes\fresh-workspace-handoff.md' Get-Content '.\notes\epiphany-current-algorithmic-map.md' Get-Content '.\notes\epiphany-fork-implementation-plan.md' git status --short --branch git log --oneline -5 -& 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_state.py' status +cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-state -- status ``` Do not trust this file for the exact live HEAD. Always check git. @@ -305,7 +305,7 @@ Rules now in force: - `state/map.yaml` is canonical current truth. - `state/scratch.md` is disposable scratch. - `state/ledgers.msgpack` is a distilled durable belief and branch ledger. -- `tools/epiphany_prepare_compaction.py` is the pre-compaction persistence check; run it before and after imminent-compaction persistence passes. +- `epiphany-prepare-compaction` is the native pre-compaction persistence check; run it before and after imminent-compaction persistence passes. - this handoff is a compact re-entry packet. - `notes/epiphany-fork-implementation-plan.md` is the distilled forward plan. - `notes/epiphany-rider-unity-integration-plan.md` is the detailed Rider-as-IDE and Unity-as-editor/runtime integration plan. diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index ac87c255b20bb34d59102f82e541c18c8cadf21f..4d65583bf0acc92c5876db1beca91d26263d078c 100644 GIT binary patch delta 246 zcmaF2hN*8o(*#AP>0uj{r!g`{Zr;T>Z;C|AqLSjBMg~S^x(24Y24*1!rdEc=Ks4Dt zTvOrZ-H?n_g|y6~;u3{`%94!yJcZPX)a25VOdtzLO*Zh8;3+OiEJ@WXs8mSIOPTyj zM6JF&wJ24gD77FbF*!9wA*oU!FR>)EEL9<>6lhFpL1sZlVqT>#SQ|(WLZ|?!DX}P3 zH#tAIATb$iNm6EBVo_#lu|j@XYLP+-&^6AbIVH}C$v_swgq+lr^wc7~hAl8p@F4jj F3IIN5SBL-r delta 45 zcmeBM&-88$(*#APX=WRhr!g`{Y~IB str: - return path.read_text(encoding="utf-8") - - -def run_git(args: list[str]) -> str: - completed = subprocess.run( - ["git", *args], - cwd=ROOT, - check=True, - capture_output=True, - text=True, - ) - return completed.stdout.strip() - - -def run_state_ledger_status() -> dict[str, Any]: - if STATE_LEDGER_EXE.exists(): - command = [ - str(STATE_LEDGER_EXE), - "status", - "--store", - str(LEDGER_STORE_PATH), - ] - else: - command = [ - "cargo", - "run", - "--quiet", - "--manifest-path", - str(ROOT / "epiphany-core" / "Cargo.toml"), - "--bin", - "epiphany-state-ledger-store", - "--", - "status", - "--store", - str(LEDGER_STORE_PATH), - ] - completed = subprocess.run( - command, - cwd=ROOT, - check=True, - capture_output=True, - text=True, - ) - return json.loads(completed.stdout) - - -def extract_map_field(name: str) -> str | None: - prefix = f" {name}:" - for line in read_text(MAP_PATH).splitlines(): - if line.startswith(prefix): - return line.split(":", 1)[1].strip() - return None - - -def extract_active_subgoals() -> list[str]: - lines = read_text(MAP_PATH).splitlines() - results: list[str] = [] - in_section = False - for line in lines: - if not in_section: - if line.strip() == "active_subgoals:": - in_section = True - continue - if line.startswith(" - "): - results.append(line[4:].strip()) - continue - if line and not line.startswith(" "): - break - return results - - -def current_scratch_subgoal() -> str | None: - lines = read_text(SCRATCH_PATH).splitlines() - for index, line in enumerate(lines): - if line.strip() != "## Current Subgoal": - continue - for candidate in lines[index + 1 :]: - stripped = candidate.strip() - if stripped: - return stripped - return None - - -def load_evidence() -> list[dict[str, Any]]: - ledger = run_state_ledger_status() - latest = ledger.get("latestEvidence") or [] - return list(reversed(latest)) - - -def load_branches() -> dict[str, Any]: - ledger = run_state_ledger_status() - return { - "branches": [None] * int(ledger.get("branches") or 0), - "activeBranches": int(ledger.get("activeBranches") or 0), - "evidence": int(ledger.get("evidence") or 0), - } - - -def add_required_file_checks(findings: list[Finding]) -> None: - for path in [ - MAP_PATH, - SCRATCH_PATH, - LEDGER_STORE_PATH, - HANDOFF_PATH, - PLAN_PATH, - ALGO_MAP_PATH, - AGENTS_PATH, - ]: - if path.exists(): - findings.append(Finding("ok", f"found {path.relative_to(ROOT)}")) - else: - findings.append(Finding("error", f"missing {path.relative_to(ROOT)}")) - - -def add_content_checks(findings: list[Finding]) -> tuple[list[dict[str, Any]], dict[str, Any]]: - evidence: list[dict[str, Any]] = [] - branches: dict[str, Any] = {} - - summary = extract_map_field("summary") - next_action = extract_map_field("next_action") - if summary: - findings.append(Finding("ok", "state/map.yaml has current_status.summary")) - else: - findings.append(Finding("error", "state/map.yaml is missing current_status.summary")) - if next_action: - findings.append(Finding("ok", "state/map.yaml has current_status.next_action")) - else: - findings.append(Finding("error", "state/map.yaml is missing current_status.next_action")) - - subgoals = extract_active_subgoals() - if subgoals: - findings.append(Finding("ok", f"state/map.yaml has {len(subgoals)} active subgoal(s)")) - else: - findings.append(Finding("warn", "state/map.yaml has no active_subgoals entries")) - - scratch_subgoal = current_scratch_subgoal() - if scratch_subgoal == "No active scratch subgoal.": - findings.append(Finding("ok", "state/scratch.md has no stale active scratch subgoal")) - elif scratch_subgoal: - findings.append(Finding("warn", f"state/scratch.md has active scratch subgoal: {scratch_subgoal}")) - else: - findings.append(Finding("warn", "state/scratch.md has no Current Subgoal value")) - - try: - evidence = load_evidence() - branches = load_branches() - findings.append(Finding("ok", f"state/ledgers.msgpack parses ({branches.get('evidence', 0)} evidence record(s))")) - except ValueError as exc: - findings.append(Finding("error", str(exc))) - - try: - branches = load_branches() - active = branches.get("activeBranches", 0) - findings.append(Finding("ok", f"state/ledgers.msgpack has {active} active branch(es)")) - except (json.JSONDecodeError, ValueError) as exc: - findings.append(Finding("error", f"state ledger parse failed: {exc}")) - - return evidence, branches - - -def add_handoff_checks(findings: list[Finding]) -> None: - text = read_text(HANDOFF_PATH) - stale_branch = re.search(r"Current branch before .*ahead \d+", text) - stale_head = re.search(r"Current HEAD before .*\b[0-9a-f]{7,40}\b", text) - if stale_branch or stale_head: - findings.append(Finding("error", "handoff embeds an exact branch or HEAD snapshot; use git commands instead")) - else: - findings.append(Finding("ok", "handoff avoids exact branch/HEAD snapshots")) - - required_phrases = [ - "Do not continue implementation automatically from a rehydrate-only request.", - "Do not trust this file for the exact live HEAD.", - "Immediate Re-entry Instruction", - ] - for phrase in required_phrases: - if phrase in text: - findings.append(Finding("ok", f"handoff contains: {phrase}")) - else: - findings.append(Finding("warn", f"handoff missing: {phrase}")) - - -def add_agents_checks(findings: list[Finding]) -> None: - text = read_text(AGENTS_PATH) - if "epiphany_prepare_compaction.py" in text: - findings.append(Finding("ok", "AGENTS.md tells agents to use the compaction helper")) - else: - findings.append(Finding("error", "AGENTS.md does not mention tools/epiphany_prepare_compaction.py")) - if "prepare for imminent compaction" in text.lower(): - findings.append(Finding("ok", "AGENTS.md names the imminent-compaction trigger")) - else: - findings.append(Finding("warn", "AGENTS.md does not name the imminent-compaction trigger phrase")) - - -def add_git_checks(findings: list[Finding]) -> tuple[str, str]: - status = run_git(["status", "--short", "--branch"]) - log = run_git(["log", "--oneline", "-5"]) - dirty_lines = [ - line - for line in status.splitlines()[1:] - if line.strip() - ] - if dirty_lines: - findings.append(Finding("warn", "git worktree has uncommitted changes; commit or explain before compaction")) - else: - findings.append(Finding("ok", "git worktree is clean")) - return status, log - - -def render_report(findings: list[Finding], status: str, log: str, evidence: list[dict[str, Any]]) -> str: - summary = extract_map_field("summary") or "(missing)" - next_action = extract_map_field("next_action") or "(missing)" - subgoals = extract_active_subgoals() - - counts = { - "ok": sum(1 for finding in findings if finding.level == "ok"), - "warn": sum(1 for finding in findings if finding.level == "warn"), - "error": sum(1 for finding in findings if finding.level == "error"), - } - - lines = [ - "Epiphany pre-compaction persistence check", - f"Workspace: {ROOT}", - f"Findings: {counts['ok']} ok, {counts['warn']} warn, {counts['error']} error", - "", - "Git status:", - status, - "", - "Recent commits:", - log, - "", - f"Summary: {summary}", - f"Next action: {next_action}", - ] - if subgoals: - lines.append("Active subgoals:") - lines.extend(f"- {item}" for item in subgoals) - if evidence: - latest = evidence[-1] - lines.extend( - [ - "", - "Latest distilled evidence:", - f"- {latest.get('ts', '(missing ts)')} {latest.get('type', '(missing type)')}/{latest.get('status', '(missing status)')}: {latest.get('note', '(missing note)')}", - ] - ) - - lines.append("") - lines.append("Findings:") - for finding in findings: - lines.append(f"[{finding.level.upper()}] {finding.message}") - - lines.extend( - [ - "", - "Pre-compaction checklist:", - "- Update state/map.yaml only if current understanding changed.", - "- Refresh notes/fresh-workspace-handoff.md if re-entry instructions changed.", - "- Add distilled evidence only for a belief-changing lesson, verification, rejected path, or scar.", - "- Keep exact branch/HEAD out of handoff prose; git commands own volatile truth.", - "- Commit completed persistence changes, or state why the worktree must stay dirty.", - "- Re-run this helper after edits before yielding to compaction.", - ] - ) - return "\n".join(lines) - - -def build_parser() -> argparse.ArgumentParser: - parser = argparse.ArgumentParser( - description="Audit Epiphany persistent state before imminent compaction." - ) - parser.add_argument( - "--strict", - action="store_true", - help="Exit nonzero on warnings as well as errors.", - ) - return parser - - -def main() -> int: - args = build_parser().parse_args() - findings: list[Finding] = [] - evidence: list[dict[str, Any]] = [] - - add_required_file_checks(findings) - try: - evidence, _branches = add_content_checks(findings) - except OSError as exc: - findings.append(Finding("error", f"state content check failed: {exc}")) - try: - add_handoff_checks(findings) - except OSError as exc: - findings.append(Finding("error", f"handoff check failed: {exc}")) - try: - add_agents_checks(findings) - except OSError as exc: - findings.append(Finding("error", f"AGENTS check failed: {exc}")) - try: - status, log = add_git_checks(findings) - except (OSError, subprocess.CalledProcessError) as exc: - findings.append(Finding("error", f"git check failed: {exc}")) - status = "(git status unavailable)" - log = "(git log unavailable)" - - print(render_report(findings, status, log, evidence)) - - has_error = any(finding.level == "error" for finding in findings) - has_warning = any(finding.level == "warn" for finding in findings) - if has_error or (args.strict and has_warning): - return 1 - return 0 - - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/tools/epiphany_state.py b/tools/epiphany_state.py deleted file mode 100644 index 3761040e..00000000 --- a/tools/epiphany_state.py +++ /dev/null @@ -1,237 +0,0 @@ -from __future__ import annotations - -import argparse -import json -import os -import subprocess -from datetime import datetime, timezone -from pathlib import Path -from typing import Any - - -ROOT = Path(__file__).resolve().parents[1] -STATE_DIR = ROOT / "state" -MAP_PATH = STATE_DIR / "map.yaml" -BRANCHES_PATH = STATE_DIR / "branches.json" -EVIDENCE_PATH = STATE_DIR / "evidence.jsonl" -LEDGER_STORE_PATH = STATE_DIR / "ledgers.msgpack" -STATE_LEDGER_BIN = "epiphany-state-ledger-store" -STATE_LEDGER_EXE = ( - Path(os.environ.get("CARGO_TARGET_DIR", r"C:\Users\Meta\.cargo-target-codex")) - / "debug" - / "epiphany-state-ledger-store.exe" -) - - -def native_ledger_command(*args: str) -> dict[str, Any]: - if STATE_LEDGER_EXE.exists(): - command = [str(STATE_LEDGER_EXE), *args] - else: - command = [ - "cargo", - "run", - "--quiet", - "--manifest-path", - str(ROOT / "epiphany-core" / "Cargo.toml"), - "--bin", - STATE_LEDGER_BIN, - "--", - *args, - ] - completed = subprocess.run( - command, - cwd=ROOT, - text=True, - capture_output=True, - check=False, - ) - if completed.returncode != 0: - message = completed.stderr.strip() or completed.stdout.strip() or f"{STATE_LEDGER_BIN} failed" - raise ValueError(message) - return json.loads(completed.stdout) - - -def read_text(path: Path) -> str: - return path.read_text(encoding="utf-8") - - -def utc_stamp() -> str: - return datetime.now(timezone.utc).replace(microsecond=0).isoformat() - - -def extract_map_field(name: str) -> str | None: - prefix = f" {name}:" - for line in read_text(MAP_PATH).splitlines(): - if line.startswith(prefix): - return line.split(":", 1)[1].strip() - return None - - -def extract_active_subgoals() -> list[str]: - lines = read_text(MAP_PATH).splitlines() - results: list[str] = [] - in_section = False - for line in lines: - if not in_section: - if line.strip() == "active_subgoals:": - in_section = True - continue - if line.startswith(" - "): - results.append(line[4:].strip()) - continue - if line and not line.startswith(" "): - break - return results - - -def cmd_migrate_json(args: argparse.Namespace) -> int: - print( - json.dumps( - native_ledger_command( - "migrate-json", - "--branches", - str(args.branches), - "--evidence", - str(args.evidence), - "--store", - str(args.store), - ), - indent=2, - ) - ) - return 0 - - -def cmd_status(_: argparse.Namespace) -> int: - ledger = native_ledger_command("status", "--store", str(LEDGER_STORE_PATH)) - summary = extract_map_field("summary") or "(missing)" - next_action = extract_map_field("next_action") or "(missing)" - subgoals = extract_active_subgoals() - - print(f"Workspace: {ROOT}") - print(f"Summary: {summary}") - print(f"Next action: {next_action}") - print(f"Active branches: {ledger.get('activeBranches', 0)} / {ledger.get('branches', 0)}") - print(f"Evidence records: {ledger.get('evidence', 0)}") - if subgoals: - print("Active subgoals:") - for item in subgoals: - print(f"- {item}") - return 0 - - -def cmd_add_evidence(args: argparse.Namespace) -> int: - command = [ - "add-evidence", - "--store", - str(LEDGER_STORE_PATH), - "--ts", - utc_stamp(), - "--type", - args.type, - "--status", - args.status, - "--note", - args.note, - ] - if args.branch: - command.extend(["--branch", args.branch]) - native_ledger_command(*command) - print("Appended evidence record.") - return 0 - - -def cmd_add_branch(args: argparse.Namespace) -> int: - command = [ - "add-branch", - "--store", - str(LEDGER_STORE_PATH), - "--id", - args.id, - "--hypothesis", - args.hypothesis, - ] - for artifact in args.artifact or []: - command.extend(["--artifact", artifact]) - if args.note: - command.extend(["--note", args.note]) - native_ledger_command(*command) - print(f"Added branch '{args.id}'.") - return 0 - - -def cmd_close_branch(args: argparse.Namespace) -> int: - command = [ - "close-branch", - "--store", - str(LEDGER_STORE_PATH), - "--id", - args.id, - "--status", - args.status, - ] - if args.note: - command.extend(["--note", args.note]) - native_ledger_command(*command) - print(f"Updated branch '{args.id}' to status '{args.status}'.") - return 0 - - -def build_parser() -> argparse.ArgumentParser: - parser = argparse.ArgumentParser( - description="Inspect and update EpiphanyAgent typed state ledgers." - ) - subparsers = parser.add_subparsers(dest="command", required=True) - - migrate_parser = subparsers.add_parser("migrate-json", help="One-shot migration from legacy JSON ledgers.") - migrate_parser.add_argument("--branches", type=Path, default=BRANCHES_PATH) - migrate_parser.add_argument("--evidence", type=Path, default=EVIDENCE_PATH) - migrate_parser.add_argument("--store", type=Path, default=LEDGER_STORE_PATH) - migrate_parser.set_defaults(func=cmd_migrate_json) - - status_parser = subparsers.add_parser("status", help="Show a compact state summary.") - status_parser.set_defaults(func=cmd_status) - - evidence_parser = subparsers.add_parser( - "add-evidence", help="Append one distilled evidence record." - ) - evidence_parser.add_argument("--type", required=True) - evidence_parser.add_argument("--status", required=True) - evidence_parser.add_argument("--note", required=True) - evidence_parser.add_argument("--branch") - evidence_parser.set_defaults(func=cmd_add_evidence) - - add_branch_parser = subparsers.add_parser( - "add-branch", help="Create a new active branch entry." - ) - add_branch_parser.add_argument("--id", required=True) - add_branch_parser.add_argument("--hypothesis", required=True) - add_branch_parser.add_argument("--artifact", action="append") - add_branch_parser.add_argument("--note") - add_branch_parser.set_defaults(func=cmd_add_branch) - - close_branch_parser = subparsers.add_parser( - "close-branch", help="Close an existing branch." - ) - close_branch_parser.add_argument("--id", required=True) - close_branch_parser.add_argument( - "--status", required=True, choices=["accepted", "rejected", "archived"] - ) - close_branch_parser.add_argument("--note") - close_branch_parser.set_defaults(func=cmd_close_branch) - - return parser - - -def main() -> int: - parser = build_parser() - args = parser.parse_args() - try: - return args.func(args) - except ValueError as exc: - print(json.dumps({"ok": False, "error": str(exc)}, indent=2)) - return 1 - - -if __name__ == "__main__": - raise SystemExit(main())