@@ -37,7 +37,9 @@ fn run(p: &Project, id: u32) -> Result<String, String> {
3737 && git config user.name conveyor && git config user.email conveyor@localhost", b"")?;
3838 let (url, git_ssh) = vm.git_remote("/home/sacenox/work");
3939 // A widget build starts from what the user sees: the library with every build on trial.
40- git(&p.path, &git_ssh, &["push", "-q", &url, if p.library { library::BASE } else { "main:main" }])?;
40+ // A code ticket starts from main plus every branch in review: the stack.
41+ let base = if p.library { library::BASE.into() } else { format!("{}:refs/heads/main", stack(p, id)?) };
42+ git(&p.path, &git_ssh, &["push", "-q", &url, &base])?;
4143 // A ticket that ran before (it failed, or was parked) picks up its own branch.
4244 let resumed = git(&p.path, "", &["rev-parse", "-q", "--verify", &branch]).is_ok();
4345 if resumed { git(&p.path, &git_ssh, &["push", "-q", &url, &format!("{branch}:{branch}")])?; }
@@ -46,7 +48,7 @@ fn run(p: &Project, id: u32) -> Result<String, String> {
4648
4749 let local = crate::paths::vm_sock(&name, "verbs");
4850 let remote = format!("/home/{}/.conveyor/{name}-{}.sock", vm::USER, std::process::id());
49- let check = merges_onto(p, &url, &git_ssh, &branch);
51+ let check = merges_onto(p, id, &url, &git_ssh, &branch);
5052 let verbs = Listener::start(&p.slug, id, local.clone(), check).map_err(|e| e.to_string())?;
5153 note("agent working");
5254 let stream = File::create(store::dir(&p.slug, id).join("stream.jsonl")).map_err(|e| e.to_string())?;
@@ -78,27 +80,54 @@ fn run(p: &Project, id: u32) -> Result<String, String> {
7880 })
7981 }
8082
81-/// What `done` checks: the branch still merges onto what it goes on (the trial, or main), which
82-/// may have moved while the agent worked. If not, that goes into the guest as a branch of the
83-/// same name, for the agent to merge.
84-pub(crate) fn merges_onto(p: &Project, url: &str, git_ssh: &str, branch: &str) -> impl Fn() -> Result<(), String> + Send + 'static {
85- let (repo, url, git_ssh, branch) = (p.path.clone(), url.to_owned(), git_ssh.to_owned(), branch.to_owned());
86- let onto = if p.library { "trial" } else { "main" };
83+/// What `done` checks: the branch still merges onto what it goes on (the trial, or the stack),
84+/// which may have moved while the agent worked. If not, that goes into the guest as its branch
85+/// (`trial`, or `main`), for the agent to merge.
86+pub(crate) fn merges_onto(p: &Project, id: u32, url: &str, git_ssh: &str, branch: &str) -> impl Fn() -> Result<(), String> + Send + 'static {
87+ let (p, url, git_ssh, branch) = (p.clone(), url.to_owned(), git_ssh.to_owned(), branch.to_owned());
8788 move || {
88- git(&repo, &git_ssh, &["fetch", "-q", &url, &format!("+{branch}:{branch}")])?;
89- let Err(files) = library::merges(&repo, onto, &branch) else { return Ok(()) };
90- git(&repo, &git_ssh, &["push", "-q", "-f", &url, &format!("{onto}:{onto}")])?;
91- Err(format!("Not done yet: while you worked, the user kept or tried other builds, and your branch conflicts with them in {files}. \
92- Run `git merge {onto}`, resolve it so both changes survive, check it again, commit, then `conveyor done` again."))
89+ // One check at a time, and a passed one counts in the stack at once: two tickets that
90+ // finish together can't both pass without seeing each other.
91+ static ONE: std::sync::Mutex<()> = std::sync::Mutex::new(());
92+ let _one = ONE.lock().unwrap_or_else(|e| e.into_inner());
93+ git(&p.path, &git_ssh, &["fetch", "-q", &url, &format!("+{branch}:{branch}")])?;
94+ let (onto, name) = if p.library { ("trial".into(), "trial") } else { (stack(&p, id)?, "main") };
95+ let Err(files) = library::merges(&p.path, &onto, &branch) else {
96+ store::event(&p.slug, id, json!({"kind": "conveyor", "msg": "done: it merges onto the work in review", "checked": true}));
97+ return Ok(());
98+ };
99+ git(&p.path, &git_ssh, &["push", "-q", "-f", &url, &format!("{onto}:refs/heads/{name}")])?;
100+ Err(format!("Not done yet: while you worked, the user kept or tried other work, and your branch conflicts with it in {files}. \
101+ Run `git merge {name}`, resolve it so both changes survive, check it again, commit, then `conveyor done` again."))
93102 }
94103 }
95104
96-/// For a ticket that ran before: where its work is, and why it stopped.
105+/// A code ticket's stack: main plus the branches of the project's other tickets in review, or
106+/// running with `done` passed.
107+fn stack(p: &Project, id: u32) -> Result<String, String> {
108+ let branches: Vec<String> = store::tickets(&p.slug).iter()
109+ .filter(|t| t.id != id && (t.state == "review" || t.state == "running" && checked(&p.slug, t.id)))
110+ .map(|t| format!("t-{}", t.id)).filter(|b| git(&p.path, "", &["rev-parse", "-q", "--verify", b]).is_ok()).collect();
111+ crate::land::stack(&p.path, &branches)
112+}
113+
114+/// Whether this run of the ticket passed its `done` check.
115+fn checked(project: &str, id: u32) -> bool {
116+ let events = store::events(project, id);
117+ let run = events.iter().rposition(|e| e["kind"] == "state" && e["state"] == "running").unwrap_or(0);
118+ events[run..].iter().any(|e| e["checked"] == true)
119+}
120+
121+/// For a ticket that ran before: where its work is, why it stopped, and the user's note since.
97122 fn stopped(project: &str, id: u32) -> String {
98- let why = store::events(project, id).into_iter().rev()
123+ let events = store::events(project, id);
124+ let why = events.iter().rev()
99125 .find_map(|e| e["msg"].as_str().filter(|_| e["kind"] == "error").or(e["args"]["reason"].as_str().filter(|_| e["verb"] == "block")).map(String::from))
100- .unwrap_or_else(|| "it stopped before finishing".into());
101- format!("\n\nThis ticket ran before. Its work is on your branch (`git log main..`): continue from it. It stopped because:\n\n{why}")
126+ .unwrap_or_else(|| "the user moved it back to ready.".into());
127+ let since = events.iter().rposition(|e| e["kind"] == "reply").map_or(0, |i| i + 1);
128+ let note = events[since..].iter().rev().find(|e| e["kind"] == "conveyor" && e["show"] == true)
129+ .and_then(|e| e["msg"].as_str()).map_or(String::new(), |n| format!("\n\nThe user's note since:\n\n{n}"));
130+ format!("\n\nThis ticket ran before. Its work is on your branch (`git log main..`): continue from it. It stopped because:\n\n{why}{note}")
102131 }
103132
104133 /// Copies the ticket's attached files to ~/ticket/files in the guest. What the prompt says of them.