feat(wait): a run's end, for the background beside a detached run
All checks were successful
Flatpak / check (push) Has been skipped
Flatpak / publish (push) Successful in 6m6s

A run launched detached, as the prepush skill says, tells the session
nothing when it ends: a round finished unread for hours.

`crafter-review wait --artifact <artifact> --hash <hash>`, the two
values of the run line, follows the hash's run lock. A full run holds it
until it ends; taking it, the run now clears the last run's identity
there, and it names itself with its run number once it has one. When a
run the wait has seen hold or take the lock lets it go, the wait reads
the identity - under its own instant shared hold, so never a rewrite in
flight - prints the summary of the final dump of the run the identity
names and exits with the code its verdict maps to, as run's does: 2 when
there is no final dump or no run number, 3 with the message of a
preflight check that stopped the run after it took the lock, which the
run adds to the identity. A wait that sees no run within --appear (120 s
by default) exits 3. Every probe of the lock, a test or dry run's too,
takes it shared for an instant, and the run lock is tried for 200 ms
before another driver counts as its holder. A wait only reads: one that
is stopped is started again and the run goes on.

freeze prints the wait line before the run line, and the skill and the
process start the wait in the background, then launch the run detached.
Released as 1.2.0.
This commit is contained in:
Jorijn van der Graaf 2026-11-02 20:34:01 +01:00
commit 4318c7d8f2
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
15 changed files with 607 additions and 33 deletions

View file

@ -229,6 +229,20 @@ echo "{\"type\":\"result\",\"subtype\":\"success\",\"is_error\":false,\"result\"
}
const std::string hash = freezeJson.at("hash").get<std::string>();
const std::string h8 = hash.substr(0, 8);
// The wait line freeze prints, before the run line: Freeze::WaitLine's,
// the invocation this binary's path (main() runs every child native).
// Every wait below starts from it, as the skill does.
const std::string waitMark = "first the wait, in the background:\n ";
const std::size_t waitAt = frozen.out.find(waitMark);
const std::size_t runAt = frozen.out.find("then the run,");
std::string printedWait;
if (waitAt != std::string::npos) {
const std::size_t from = waitAt + waitMark.size();
printedWait = frozen.out.substr(from, frozen.out.find('\n', from) - from);
}
Check("freeze prints the wait line, Freeze::WaitLine's, before the run line",
waitAt != std::string::npos && runAt != std::string::npos && waitAt < runAt && printedWait == Freeze::WaitLine(freezeJson, ".review-gates/push/x"), frozen.out);
const std::vector<std::string> waitLine = Dyn::Split(printedWait);
auto runArgs = [&](std::vector<std::string> extra) {
std::vector<std::string> args = {Platform::SelfExe().string(), "run", "--artifact", ".review-gates/push/x", "--hash", hash, "--base", baseSha, "--trees", ".",
"--dispositions", ".review-gates/push/x/gate-state/dispositions.json", "--date", Text(freezeJson.at("date")),
@ -284,6 +298,18 @@ echo "{\"type\":\"result\",\"subtype\":\"success\",\"is_error\":false,\"result\"
// driver is SIGKILLed once every other pass has.
const fs::path scratchA = scratchRoot / std::format("{}-run-1", h8);
const Process::Child a = start({}, {{"FAKE_SLEEP", "571"}, {"FAKE_SLEEP_ON", std::format("repo-security:{}", securityUnit)}}, "a");
// `wait` beside it, as the skill runs it: the line freeze printed.
auto waitBeside = [&](std::string_view name) {
std::vector<std::string> argv = waitLine;
argv.insert(argv.end(), {"--appear", "30"});
return Process::Start({.argv = argv,
.environment = Env,
.cwd = repo,
.input = {.kind = Process::Stream::Kind::Null},
.output = {.kind = Process::Stream::Kind::File, .file = base / std::format("{}.out", name)},
.error = {.kind = Process::Stream::Kind::File, .file = base / std::format("{}.err", name)}});
};
const Process::Child waitA = waitBeside("wait-a");
auto answered = [&] {
std::int64_t n = 0;
std::error_code error;
@ -295,6 +321,9 @@ echo "{\"type\":\"result\",\"subtype\":\"success\",\"is_error\":false,\"result\"
const bool settled = WaitFor([&] { return calls() == all && answered() == all - 1; }, std::chrono::seconds(30));
Process::Signal(a.pid, SIGKILL);
const std::optional<std::int32_t> aCode = Process::Wait(a.pid, std::chrono::seconds(10));
const std::optional<std::int32_t> waitACode = Process::Wait(waitA.pid, std::chrono::seconds(30));
Check("...and a wait beside it reports a run that left no final dump: exit 2", waitACode == 2 && Read(base / "wait-a.out").contains("ended without its final dump"),
Read(base / "wait-a.out") + Read(base / "wait-a.err"));
const std::vector<std::string> aLeft = LeftoversAfter(fakeDir);
Json resumeA;
try {
@ -310,7 +339,9 @@ echo "{\"type\":\"result\",\"subtype\":\"success\",\"is_error\":false,\"result\"
// The next run: the first gate resumed, the outputs of the killed run
// salvaged, the one pass that never answered bought again.
std::int64_t before = calls();
const Process::Child waitB = waitBeside("wait-b");
const Process::Captured b = run({});
const std::optional<std::int32_t> waitBCode = Process::Wait(waitB.pid, std::chrono::seconds(30));
const Json dumpB = LatestDump(gateDir, h8);
const Json& processesB = dumpB.is_object() ? dumpB.at("processes") : Json::array();
Check("the next run resumes the first gate from the checkpoint, salvages every output the killed run finished and buys only the pass that never answered - SEND-READY, exit 0",
@ -318,6 +349,9 @@ echo "{\"type\":\"result\",\"subtype\":\"success\",\"is_error\":false,\"result\"
&& std::ranges::count_if(processesB, [](const Json& r) { return Dyn::Truthy(Dyn::Get(r, "salvaged")); }) == all - gate1 - 1
&& Count(processesB, "status", "ok") == all - gate1,
std::format("exit {}, {} call(s), {}", b.returnCode, calls() - before, dumpB.is_object() ? Text(dumpB.at("overall")) : b.out + b.err));
const std::string waitBOut = Read(base / "wait-b.out");
Check("a wait beside the next run ends with it, printing the summary the run printed at its end, with its exit code", waitBCode == 0 && !waitBOut.empty() && b.out.ends_with(waitBOut),
waitBOut + Read(base / "wait-b.err"));
const Json stored = State::LoadState(gateDir / "unit-state.json").at("entries");
Check("...and stores the clean block results of the range for reuse", static_cast<std::int64_t>(stored.size()) >= all - gate1, std::to_string(stored.size()));
@ -329,14 +363,28 @@ echo "{\"type\":\"result\",\"subtype\":\"success\",\"is_error\":false,\"result\"
c.returnCode == 0 && calls() == before && dumpC.is_object() && dumpC.at("overall") == "SEND-READY" && dumpC.at("resumedLenses").size() == 3,
std::format("exit {}, {} call(s)", c.returnCode, calls() - before));
// A full run a preflight check stops after it took the run lock: the
// wait beside it prints the check's message, exit 3.
const Process::Child waitP = waitBeside("wait-p");
const Process::Captured p = run({"--without", "repo-better-solution.md,repo-bug-prover.md,repo-security.md"});
const std::optional<std::int32_t> waitPCode = Process::Wait(waitP.pid, std::chrono::seconds(30));
Check("a wait beside a full run that a preflight check stops after its run lock prints the check's message, exit 3", p.returnCode == 3 && waitPCode == 3
&& Read(base / "wait-p.out").starts_with("preflight: no lens runs") && p.err.contains(Dyn::Strip(Read(base / "wait-p.out"))),
Read(base / "wait-p.out") + Read(base / "wait-p.err") + p.err);
// A defect, on a run that reuses nothing; the scratch dir it claims
// holds a stop file from before, which is not this run's.
const std::string nextRun = std::format("{}-run-{}", h8, RunNumber(dumpC) + 1);
const fs::path stale = scratchRoot / nextRun / "stop";
Write(stale, "");
before = calls();
const Process::Child waitD = waitBeside("wait-d");
const Process::Captured d = run({"--no-reuse", "--no-salvage"},
{{"FAKE_FIX", std::format("repo-bug-prover:{}", proverUnit)}, {"FAKE_SLEEP", "3"}, {"FAKE_SLEEP_ON", "repo-better-solution:whole:order"}});
const std::optional<std::int32_t> waitDCode = Process::Wait(waitD.pid, std::chrono::seconds(30));
const std::string waitDOut = Read(base / "wait-d.out");
Check("a wait beside a FIX FIRST run prints the summary the run printed at its end, exit 1", waitDCode == 1 && !waitDOut.empty() && d.out.ends_with(waitDOut),
waitDOut + Read(base / "wait-d.err"));
const Json dumpD = LatestDump(gateDir, h8);
Check("FIX FIRST: a defect in the last gate stops the run there, exit 1 - every pass bought again with --no-reuse --no-salvage",
d.returnCode == 1 && calls() - before == all && dumpD.is_object() && dumpD.at("overall") == "FIX FIRST" && dumpD.at("stoppedAt") == "Gate 3 — Proof",