Files
plan2code/plan2code-app/src-tauri/src/restart_brief.rs
T

182 lines
8.6 KiB
Rust
Raw Normal View History

//! What a Restart leaves for the dashboard: the restart brief, written as a
//! pick-up handoff, so the fresh agent (which starts on the plain `/plan2code`
//! prompt and lands on the dashboard) finds the chip waiting. Nothing new is
//! stored anywhere: the brief is built from the session's files each time.
use std::path::Path;
use std::sync::Mutex;
use chrono::{DateTime, Local};
use serde_json::Value;
use crate::brief;
use crate::handoff;
use crate::session_reader;
pub const READY_NOTE: &str = "Brief ready on the dashboard";
/// The handoff the last Restart wrote. The next Restart retires it if the person
/// has not picked it up, so unused briefs do not pile up as chips.
static LAST_BRIEF: Mutex<Option<String>> = Mutex::new(None);
/// The skill named on a handoff record for a workflow `brief::skill_for_workflow`
/// does not map, such as `dashboard`: the console's own name for that pseudo-skill.
const UNMAPPED_SKILL: &str = "plan2code";
/// Write the brief for session `sid` as a handoff. A workflow with no mapped
/// skill (a dashboard, or one not yet known) still gets a short, honest chip
/// rather than nothing. Returns the handoff's id, or `None` when there is
/// truly nothing to write (the session names no worktree is an error, not a
/// `None`, since that is a broken session rather than an empty one).
pub fn leave_brief(console_home: &Path, handoffs: &Path, sid: &str, when: DateTime<Local>) -> Result<Option<String>, String> {
let session = session_reader::load(console_home, sid).map_err(|e| e.to_string())?;
let state = &session.state;
let workflow = text(state, "workflow").unwrap_or("");
let skill = brief::skill_for_workflow(workflow);
let worktree = text(state, "worktree").ok_or("the session names no worktree")?;
let project = text(state, "project").unwrap_or(worktree);
let spec = text(state, "specDir").filter(|s| valid_spec(s));
let spec_dir = spec.map(|s| Path::new(worktree).join(s));
let git = brief::git_summary(Path::new(worktree));
let events = session_reader::read_ndjson(&session.dir.join("events.ndjson"));
let body = brief::build_brief(state, spec_dir.as_deref(), git.as_ref(), &events);
let title = match skill {
Some(skill) => match spec {
Some(spec) => format!("Restart of {skill} on {spec}"),
None => format!("Restart of {skill}"),
},
None => format!("Restart while on {workflow}"),
};
let record = handoff::Record { title: &title, worktree, project, spec, workflow, skill: skill.unwrap_or(UNMAPPED_SKILL) };
handoff::write(handoffs, when, &record, &body).map(Some)
}
/// The bar's line for a Restart on session `sid`, written to the real console
/// and handoff folders. `None` when there was nothing to leave.
pub fn note_for_restart(sid: &str) -> Option<String> {
let console_home = session_reader::console_home()?;
let handoffs = handoff::handoffs_dir()?;
match leave_brief(&console_home, &handoffs, sid, handoff::now()) {
Ok(Some(id)) => {
let previous = LAST_BRIEF.lock().ok().and_then(|mut last| last.replace(id.clone()));
if let Some(previous) = previous.filter(|p| *p != id) {
let _ = handoff::retire_if_unused(&handoffs, &previous);
}
Some(READY_NOTE.to_string())
}
Ok(None) => None,
Err(why) => Some(format!("The brief could not be written: {why}")),
}
}
fn text<'a>(state: &'a Value, key: &str) -> Option<&'a str> {
state.get(key).and_then(Value::as_str).filter(|s| !s.is_empty())
}
fn valid_spec(spec: &str) -> bool {
spec.strip_prefix("specs/").is_some_and(|name| !name.is_empty() && name != "." && name != ".." && !name.contains(['/', '\\']))
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::TimeZone;
use std::fs;
use std::path::PathBuf;
fn fixture(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("plan2code-restart-{}-{name}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
dir
}
/// A console home with one session on `workflow`, and a project with a spec.
fn session(name: &str, workflow: &str, spec: Option<&str>) -> (PathBuf, PathBuf, PathBuf) {
let root = fixture(name);
let home = root.join("console");
let project = root.join("project");
let dir = home.join("sessions").join("s1");
fs::create_dir_all(&dir).unwrap();
fs::create_dir_all(&project).unwrap();
let state = serde_json::json!({
"schema": 1,
"workflow": workflow,
"specDir": spec,
"worktree": project.to_string_lossy(),
"project": project.to_string_lossy(),
"headline": { "stage": "Phase 5: Restart brief", "cleared": 4, "total": 11 },
"items": [],
});
fs::write(dir.join("state.json"), state.to_string()).unwrap();
(home, project, root.join("handoffs"))
}
fn when() -> DateTime<Local> {
Local.with_ymd_and_hms(2026, 10, 21, 14, 5, 0).unwrap()
}
#[test]
fn a_mid_implement_session_leaves_a_pick_up_the_dashboard_lists() {
let (home, project, handoffs) = session("pickup", "implement", Some("specs/feature"));
let spec = project.join("specs").join("feature");
fs::create_dir_all(&spec).unwrap();
fs::write(spec.join("overview.md"), "- [/] [Phase 5: Restart](./phase-5.md)\n").unwrap();
fs::write(spec.join("phase-5.md"), "- [ ] **Task 5.1:** Create the reader\n").unwrap();
let id = leave_brief(&home, &handoffs, "s1", when()).unwrap().expect("a handoff");
assert_eq!(id, "2026-10-21-1405-handoff");
let record: Value = serde_json::from_str(&fs::read_to_string(handoffs.join(format!("{id}.json"))).unwrap()).unwrap();
assert_eq!(record["workflow"], "implement");
assert_eq!(record["spec"], "specs/feature");
let doc = fs::read_to_string(handoffs.join(format!("{id}.md"))).unwrap();
assert!(doc.contains("## Next task\n\nTask 5.1: Create the reader"));
assert!(doc.contains("Phase 5: Restart brief: 4 of 11 done."));
}
#[test]
fn a_dashboard_session_still_leaves_a_chip() {
let (home, _, handoffs) = session("dashboard", "dashboard", None);
let id = leave_brief(&home, &handoffs, "s1", when()).unwrap().expect("a handoff");
let record: Value = serde_json::from_str(&fs::read_to_string(handoffs.join(format!("{id}.json"))).unwrap()).unwrap();
assert_eq!(record["title"], "Restart while on dashboard");
assert_eq!(record["skill"], "plan2code");
assert_eq!(record["workflow"], "dashboard");
let doc = fs::read_to_string(handoffs.join(format!("{id}.md"))).unwrap();
assert!(doc.contains("- Workflow: `dashboard`"));
assert!(doc.contains("## Next task\n\nOpen the dashboard"));
}
#[test]
fn the_brief_carries_the_agent_activity_and_recent_events() {
let (home, _project, handoffs) = session("activity", "implement", None);
let dir = home.join("sessions").join("s1");
let mut state: Value = serde_json::from_str(&fs::read_to_string(dir.join("state.json")).unwrap()).unwrap();
state["agent"] = serde_json::json!({ "status": "working", "activity": "Spiking Claude's context signal (Task 10.1)" });
fs::write(dir.join("state.json"), state.to_string()).unwrap();
fs::write(
dir.join("events.ndjson"),
"{\"at\":\"2026-10-23T01:08:47.152Z\",\"type\":\"post\",\"items\":0}\n{\"at\":\"2026-10-23T01:09:00.000Z\",\"type\":\"consume\",\"kind\":\"submit\"}\n",
)
.unwrap();
let id = leave_brief(&home, &handoffs, "s1", when()).unwrap().expect("a handoff");
let doc = fs::read_to_string(handoffs.join(format!("{id}.md"))).unwrap();
assert!(doc.contains("Doing: Spiking Claude's context signal (Task 10.1)"));
assert!(doc.contains("## Recent activity\n\n- post (0 items)\n- consume: submit\n"));
}
#[test]
fn a_session_that_does_not_exist_is_an_error_not_a_brief() {
let (home, _, handoffs) = session("missing", "implement", None);
assert!(leave_brief(&home, &handoffs, "nope", when()).is_err());
}
#[test]
fn a_spec_that_is_not_one_folder_is_left_out_of_the_record() {
let (home, _, handoffs) = session("badspec", "pathfinder", Some("specs/a/b"));
let id = leave_brief(&home, &handoffs, "s1", when()).unwrap().unwrap();
let record: Value = serde_json::from_str(&fs::read_to_string(handoffs.join(format!("{id}.json"))).unwrap()).unwrap();
assert!(record["spec"].is_null());
}
}