1use crate::workflow::model::{Task, Workflow}; 2 3const SCHEMA: &str = r#"{"tasks":[{"id":"T1","step":1,"title":"short imperative title","decisions":["a decision you made and why"],"acceptance":["a checkable acceptance criterion"],"evidence":["test-output.txt"]}]}"#; 4 5const RULES: &str = "\ 6How cast runs a workflow: 7- Steps run in ascending order. Tasks with the same step number run at the same time, each with its own agent, in the same /work checkout. 8- Each agent sees only the original prompt and its own task, so every task must carry the decisions and context it needs. 9- The VM has an XFCE desktop on DISPLAY=:0 that all agents share. Visual work is checked there, in real windows, with real screenshots. 10- Each task lists the decisions it made, its acceptance criteria and the evidence to attach. 11- Evidence is the bare file names the task will write as proof, such as test output, logs or screenshots. Cast picks the folder, so give no directory. 12 13Rules for tasks that run at the same time: 14- Read the code to find the files each task will create or change. Tasks in one step must not change the same files, including shared ones such as lockfiles, manifests, generated code, snapshots, docs and changelogs. 15- If two tasks need the same files, put them in different steps, or have each work in its own git worktree on its own branch and add a later task that merges them. 16- They never commit, stash, reset, rebase, check out or clean, and never revert changes they did not make: another agent may be in the middle of its work. 17- They do not run commands that rewrite files they do not own, such as project-wide formatters or code generators. 18 19Rules for git: 20- If /work is not a git repository, no task uses git, and parallel tasks keep to separate files. 21- Only a task that runs alone in its step commits. When parallel tasks change the project, follow them with a consolidation task that reviews the combined diff, merges any worktree branches, resolves conflicts, runs the full checks and commits the work. 22- Group commits by logical change and follow the project's own instructions, such as CLAUDE.md, AGENTS.md or CONTRIBUTING.md, on commits and checks. 23- A task that adds worktrees removes them once its branch is merged."; 24 25pub fn workflow(prompt: &str, prior: Option<(&Workflow, &str)>) -> String { 26 let mut out = format!( 27 "You plan software work. The working directory /work is the user's project; read it as needed but do not change any files.\n\ 28 Turn the user's prompt below into a workflow of tasks.\n\n{RULES}\n\n\ 29 Answer with only JSON matching this shape, no prose and no code fence:\n{SCHEMA}\n\n<prompt>\n{prompt}\n</prompt>\n" 30 ); 31 if let Some((wf, feedback)) = prior { 32 out += &format!( 33 "\nThis is the current workflow:\n{}\n\nRevise it with this feedback:\n{feedback}\n", 34 json(wf) 35 ); 36 } 37 out 38} 39 40pub fn check(prompt: &str, draft: &Workflow) -> String { 41 format!( 42 "You review a workflow that another agent planned from the user's prompt. The working directory /work is the user's project; read it as needed but do not change any files.\n\ 43 Make sure the workflow is ready to run and correct, and fix it where it is not.\n\n{RULES}\n\n\ 44 Check each of these against the prompt and the code:\n\ 45 - It does everything the prompt asks and nothing it does not.\n\ 46 - Tasks in the same step change separate files. Open the files to confirm it.\n\ 47 - Each task depends only on tasks in earlier steps.\n\ 48 - Commits come only from tasks that run alone, and parallel work is consolidated before anything builds on it.\n\ 49 - Every acceptance criterion can be checked by running something or reading the evidence.\n\ 50 - Every task has the decisions and context its agent needs without seeing the other tasks.\n\n\ 51 Answer with only JSON, no prose and no code fence: the corrected workflow in this shape, plus \"changes\", a list with one short sentence for each change you made and why. Leave \"changes\" empty if the workflow was already right.\n\ 52 {SCHEMA}\n\n<prompt>\n{prompt}\n</prompt>\n\n<workflow>\n{}\n</workflow>\n", 53 json(draft) 54 ) 55} 56 57fn json(wf: &Workflow) -> String { 58 serde_json::to_string_pretty(&wf.tasks).unwrap_or_default() 59} 60 61pub fn assist(doc: &str, range: (usize, usize), instruction: &str) -> String { 62 let (a, b) = range; 63 let doc = format!("{}<<<{}>>>{}", &doc[..a], &doc[a..b], &doc[b..]); 64 let task = if a == b { 65 "Write new markdown to insert at the <<<>>> marker." 66 } else { 67 "Rewrite the markdown between <<< and >>>." 68 }; 69 format!( 70 "You help write a markdown prompt document. Do not use tools.\n{task}\nInstruction: {instruction}\n\ 71 Answer with only the replacement markdown, without the markers, without a code fence and without commentary.\n\n<document>\n{doc}\n</document>\n" 72 ) 73} 74 75pub fn task(wf: &Workflow, task: &Task, evidence_dir: &str, prompt: &str) -> String { 76 let list = |xs: &[String]| xs.iter().map(|x| format!("- {x}\n")).collect::<String>(); 77 let others: Vec<String> = wf 78 .tasks 79 .iter() 80 .filter(|t| t.step == task.step && t.id != task.id) 81 .map(|t| format!("{}: {}", t.id, t.title)) 82 .collect(); 83 let parallel = if others.is_empty() { 84 String::new() 85 } else { 86 format!( 87 "\nThese tasks run at the same time as yours, each with its own agent, in the same /work:\n{}\ 88 Change only the files your task needs. Do not commit, stash, reset, rebase, check out or clean, do not revert changes you did not make, and do not run project-wide formatters or generators. If your task says to use a git worktree, work only inside it.\n\ 89 The other agents share the desktop with you: find your windows by the PID you started, never close or type into windows you did not open, and give agent-browser a --session of your own.\n", 90 list(&others) 91 ) 92 }; 93 format!( 94 "You are running task {id} of a workflow in /work, the user's project.\n\ 95 Original prompt:\n<prompt>\n{prompt}\n</prompt>\n\n\ 96 Task {id}: {title}\nDecisions already made:\n{d}\nAcceptance criteria:\n{a}\nEvidence to attach:\n{e}{parallel}\n\ 97 The VM runs an XFCE desktop on DISPLAY=:0 at 1280x800, and agent-browser opens real windows on it. Look at and use visual work there as a person would, and follow the xfce-computer-use skill to drive and capture it.\n\ 98 Do the work. Save every piece of evidence into {evidence_dir}/ (create it).\n\ 99 Finish with a short summary of what changed and how each acceptance criterion was met.\n\ 100 Workflow {name} v{v}, step {step} of {steps}.\n", 101 id = task.id, 102 title = task.title, 103 d = list(&task.decisions), 104 a = list(&task.acceptance), 105 e = list(&task.evidence), 106 name = wf.prompt, 107 v = wf.version, 108 step = task.step, 109 steps = wf.steps().len(), 110 ) 111} 112 113#[cfg(test)] 114mod tests { 115 use crate::workflow::model::{Task, Workflow}; 116 117 #[test] 118 fn a_task_prompt_names_only_the_tasks_running_beside_it() { 119 let task = |id: &str, step, title: &str| Task { 120 id: id.into(), 121 step, 122 title: title.into(), 123 ..Task::default() 124 }; 125 let wf = Workflow { 126 tasks: vec![ 127 task("T1", 1, "api"), 128 task("T2", 1, "ui"), 129 task("T3", 2, "merge"), 130 ], 131 ..Workflow::default() 132 }; 133 let p = super::task(&wf, &wf.tasks[0], "/cast/runs/x/T1", "go"); 134 assert!(p.contains("T2: ui") && !p.contains("T3: merge") && p.contains("Do not commit")); 135 let alone = super::task(&wf, &wf.tasks[2], "/cast/runs/x/T3", "go"); 136 assert!(!alone.contains("Do not commit")); 137 } 138}