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

@ -5,8 +5,12 @@
// refreshes, keeps what it did not write, replaces the old link into the
// submodule, refuses a foreign link, a subdirectory and the home directory
// before it writes anything), doc, stop (a scratch dir, --hash on the
// board), seats on a claude stand-in, and browser's usage.
// board), --version, wait (a run's end at the hash's run lock), seats on a
// claude stand-in, and browser's usage.
#include <fcntl.h>
#include <signal.h>
#include <sys/file.h>
#include <unistd.h>
import std;
import Crafter.Review;
@ -66,7 +70,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"ok","total
}
std::int32_t Checks() {
std::println("commands: init, doc, stop, seats on a claude stand-in, browser's usage");
std::println("commands: init, doc, stop, --version, wait, seats on a claude stand-in, browser's usage");
Platform::SetMode(Platform::Mode::Native);
const fs::path checkout = fs::current_path();
SetHome(checkout);
@ -316,6 +320,265 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"ok","total
Check("stop --hash: every live run of the hash on the board gets its stop file (the first 8 characters decide); another hash finds none (exit 1); the board is read, never written",
byHash.returnCode == 0 && wroteHash && otherHash.returnCode == 1 && otherHash.err.contains("no live run of 00000000 on the board") && Read(base / "board.json") == boardBefore,
byHash.out + byHash.err + otherHash.err);
// --version: the driver, the home, every charter's sha256.
const Process::Captured version = Self({"--version"}, base);
std::int64_t charters = 0;
bool chartersHashed = true;
for (const fs::directory_entry& entry : fs::directory_iterator(checkout / "process" / "lenses")) {
if (entry.path().extension() != ".md") continue;
++charters;
chartersHashed = chartersHashed && version.out.contains(std::format("charter {} {}\n", entry.path().filename().string(), Sha256Hex(Read(entry.path()))));
}
Check("--version: the home and every charter's sha256", version.returnCode == 0 && version.out.contains(std::format("home {}\n", checkout.string())) && charters > 0 && chartersHashed,
version.out);
// wait (1.2.0): a run's end, for the background beside a detached
// run, read from the hash's run lock and the run's dump. This process
// stands in for the run's driver on a thread while the wait runs: it
// holds the lock, names a run in it, writes the dump, lets the lock go.
auto artifactOf = [&](std::string_view name) {
const fs::path art = base / "waits" / name;
fs::create_directories(art / "gate-state");
return art;
};
auto dumpOf = [](std::string_view h8, std::int64_t n, std::string_view overall) {
return Json {{"overall", overall}, {"results", Json::array()}, {"dumpPath", std::format("gate-state/commits-{}-run-{}.json", h8, n)}};
};
auto identity = [&](std::int64_t n) {
return Json {{"pid", 4242}, {"since", std::format("2026-11-02T22:00:0{}", n)}, {"scratch", (base / "scratch" / std::format("x-run-{}", n)).string()}, {"run_n", n}};
};
auto waitOn = [&](const fs::path& art, std::string_view hash, std::vector<std::string> extra = {}) {
std::vector<std::string> argv = {"wait", "--artifact", art.string(), "--hash", std::string(hash)};
std::ranges::move(extra, std::back_inserter(argv));
return Self(argv, base);
};
// A run live when the wait begins, ending with its final dump.
const fs::path artA = artifactOf("a");
const Json dumpA = dumpOf("beefa001", 1, "SEND-READY");
auto lockA = std::make_unique<State::RunLock>(artA / "gate-state", "commits", "beefa001");
lockA->WriteInfo(identity(1));
std::jthread endA([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
Write(artA / "gate-state" / "commits-beefa001-run-1.json", DumpFile(dumpA));
lockA.reset();
});
const double startA = Platform::Now();
const Process::Captured ended = waitOn(artA, "beefa001ffff");
const double waitedA = Platform::Now() - startA;
endA.join();
// A run whose lock goes without its final dump (killed).
const fs::path artB = artifactOf("b");
Write(artB / "gate-state" / "commits-beefb002-run-1.json", DumpFile(dumpOf("beefb002", 1, "IN PROGRESS - after Gate 1 — Approach")));
auto lockB = std::make_unique<State::RunLock>(artB / "gate-state", "commits", "beefb002");
lockB->WriteInfo(identity(1));
std::jthread endB([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
lockB.reset();
});
const Process::Captured killed = waitOn(artB, "beefb002");
endB.join();
// A run a preflight check stops after it took the lock.
const fs::path artC = artifactOf("c");
auto lockC = std::make_unique<State::RunLock>(artC / "gate-state", "commits", "beefc003");
lockC->WriteInfo(identity(1));
std::jthread endC([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
lockC->AddInfo("preflight", "no lens runs: --without drops all");
lockC.reset();
});
const Process::Captured stopped = waitOn(artC, "beefc003");
endC.join();
// Run 1 ended before the wait began; a new holder takes the lock and
// names itself only a poll later - the wait follows run 2, not run 1.
const fs::path artD = artifactOf("d");
{
State::RunLock first(artD / "gate-state", "commits", "beefd004");
first.WriteInfo(identity(1));
}
Write(artD / "gate-state" / "commits-beefd004-run-1.json", DumpFile(dumpOf("beefd004", 1, "FIX FIRST")));
const Json dumpD2 = dumpOf("beefd004", 2, "SEND-READY");
std::jthread runD([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
State::RunLock second(artD / "gate-state", "commits", "beefd004");
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
second.WriteInfo(identity(2));
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
Write(artD / "gate-state" / "commits-beefd004-run-2.json", DumpFile(dumpD2));
});
const Process::Captured newer = waitOn(artD, "beefd004", {"--appear", "10"});
runD.join();
// A wait that begins just after a new holder took the lock, before it
// names itself: the lock no longer names the last run (killed, its dump
// IN PROGRESS), and the new run is followed.
const fs::path artE = artifactOf("e");
{
State::RunLock first(artE / "gate-state", "commits", "beefe005");
first.WriteInfo(identity(1));
}
Write(artE / "gate-state" / "commits-beefe005-run-1.json", DumpFile(dumpOf("beefe005", 1, "IN PROGRESS - after Gate 1 — Approach")));
auto lockE = std::make_unique<State::RunLock>(artE / "gate-state", "commits", "beefe005");
const Json dumpE2 = dumpOf("beefe005", 2, "SEND-READY");
std::jthread runE([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
lockE->WriteInfo(identity(2));
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
Write(artE / "gate-state" / "commits-beefe005-run-2.json", DumpFile(dumpE2));
lockE.reset();
});
const Process::Captured fresh = waitOn(artE, "beefe005", {"--appear", "10"});
runE.join();
// The instant before a new holder clears the identity, held by a bare
// flock: the lock still names the last run, killed (its dump IN
// PROGRESS) - not that run; the new one is reported once it ends.
const fs::path artG = artifactOf("g");
{
State::RunLock first(artG / "gate-state", "commits", "beef9006");
first.WriteInfo(identity(1));
}
Write(artG / "gate-state" / "commits-beef9006-run-1.json", DumpFile(dumpOf("beef9006", 1, "IN PROGRESS - after Gate 1 — Approach")));
const fs::path lockG = State::LockPath(artG / "gate-state", "commits", "beef9006");
const std::int32_t bare = open(lockG.c_str(), O_RDWR | O_CLOEXEC);
const bool bareHeld = bare >= 0 && flock(bare, LOCK_EX | LOCK_NB) == 0;
const Json dumpG2 = dumpOf("beef9006", 2, "SEND-READY");
std::jthread runG([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
Write(lockG, DumpFile(identity(2)));
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
Write(artG / "gate-state" / "commits-beef9006-run-2.json", DumpFile(dumpG2));
close(bare);
});
const Process::Captured notLast = waitOn(artG, "beef9006", {"--appear", "10"});
runG.join();
Check("wait: begun just after a new holder took the lock, it follows the new run - the lock no longer names the last one (killed)",
fresh.returnCode == 0 && fresh.out == std::format("{}\n", State::Summary(dumpE2)), fresh.out + fresh.err);
Check("wait: a held lock still naming the last run, killed - a new holder's instant before it clears the identity - is not that run: the new one is reported once it ends",
bareHeld && notLast.returnCode == 0 && notLast.out == std::format("{}\n", State::Summary(dumpG2)), notLast.out + notLast.err);
// A run rewriting its identity while it holds the lock - here held
// empty across a poll, as in the instant inside AddInfo's rewrite - is
// read only once nobody holds the lock: its preflight message, exit 3.
const fs::path artT = artifactOf("t");
auto lockT = std::make_unique<State::RunLock>(artT / "gate-state", "commits", "beef7007");
lockT->WriteInfo(identity(1));
const fs::path lockPathT = State::LockPath(artT / "gate-state", "commits", "beef7007");
std::jthread endT([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
Write(lockPathT, "");
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
lockT->AddInfo("preflight", "the application does not answer");
lockT.reset();
});
const Process::Captured rewritten = waitOn(artT, "beef7007");
endT.join();
Check("wait: a run whose identity is mid-rewrite while it holds the lock (here empty across a poll) is read once nobody holds it - its preflight message, exit 3",
rewritten.returnCode == 3 && rewritten.out == "preflight: the application does not answer\n", rewritten.out + rewritten.err);
// A run that takes the lock after the wait began and leaves it between
// two polls without naming itself: seen by the identity it cleared,
// exit 2.
const fs::path artU = artifactOf("u");
{
State::RunLock first(artU / "gate-state", "commits", "beef8008");
first.WriteInfo(identity(1));
}
Write(artU / "gate-state" / "commits-beef8008-run-1.json", DumpFile(dumpOf("beef8008", 1, "FIX FIRST")));
std::jthread runU([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
const State::RunLock unnamed(artU / "gate-state", "commits", "beef8008");
});
const Process::Captured unnamedRun = waitOn(artU, "beef8008", {"--appear", "10"});
runU.join();
Check("wait: a run that takes the lock after the wait began and leaves it between two polls unnamed is seen by the identity it cleared - exit 2",
unnamedRun.returnCode == 2 && unnamedRun.out.contains("took its run lock and ended before it named itself"), unnamedRun.out + unnamedRun.err);
// A wait started after a run a preflight check stopped: the lock names
// that run and the check's message.
const fs::path artV = artifactOf("v");
{
State::RunLock stopped(artV / "gate-state", "commits", "beef9009");
stopped.WriteInfo(identity(1));
stopped.AddInfo("preflight", "no lens runs: --without drops all");
}
const Process::Captured latePreflight = waitOn(artV, "beef9009", {"--appear", "0"});
Check("wait: started after a run a preflight check stopped, it exits 3 naming that run and the check's message",
latePreflight.returnCode == 3
&& latePreflight.err.contains("the lock names run 1, which a preflight check stopped before this wait began: no lens runs: --without drops all"),
latePreflight.err);
// A wait started after a run that ended without a dump: the lock names
// that run and its log.
const fs::path artW = artifactOf("w");
{
State::RunLock nodump(artW / "gate-state", "commits", "beefa010");
nodump.WriteInfo(identity(1));
}
const Process::Captured lateNoDump = waitOn(artW, "beefa010", {"--appear", "0"});
Check("wait: started after a run that ended without a dump, it exits 3 naming that run and its log",
lateNoDump.returnCode == 3
&& lateNoDump.err.contains(std::format("the lock names run 1, which ended before this wait began without a dump: its log is {}",
(base / "scratch" / "x-run-1" / "log.txt").string())),
lateNoDump.err);
// A run that ends CONFIRMING: its summary, exit 2 - a round to run
// again, as run's code says.
const fs::path artY = artifactOf("y");
const Json dumpY = dumpOf("beefc012", 1, "CONFIRMING - NOT SEND-READY (gates clean; 2 code unit(s) owe 3 clean read(s) to reach 3: run the same line again)");
auto lockY = std::make_unique<State::RunLock>(artY / "gate-state", "commits", "beefc012");
lockY->WriteInfo(identity(1));
std::jthread endY([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
Write(artY / "gate-state" / "commits-beefc012-run-1.json", DumpFile(dumpY));
lockY.reset();
});
const Process::Captured confirming = waitOn(artY, "beefc012");
endY.join();
Check("wait: a run whose final dump is CONFIRMING - its summary, exit 2, the code run gives it",
confirming.returnCode == 2 && confirming.out == std::format("{}\n", State::Summary(dumpY)), confirming.out + confirming.err);
// A wait only reads: stopped while its run is live, it leaves the lock
// and its identity as they were, a wait started again reports the run,
// and a wait leaves the lock file and the dump byte for byte as found.
const fs::path artK = artifactOf("k");
auto lockK = std::make_unique<State::RunLock>(artK / "gate-state", "commits", "beefb011");
lockK->WriteInfo(identity(1));
const fs::path lockPathK = State::LockPath(artK / "gate-state", "commits", "beefb011");
const std::string identityK = Read(lockPathK);
const Process::Child stoppedWait = Process::Start({.argv = {Platform::SelfExe().string(), "wait", "--artifact", artK.string(), "--hash", "beefb011"},
.environment = Env,
.cwd = base,
.input = {.kind = Process::Stream::Kind::Null},
.output = {.kind = Process::Stream::Kind::File, .file = base / "stopped-wait.out"},
.error = {.kind = Process::Stream::Kind::File, .file = base / "stopped-wait.err"}});
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
Process::Signal(stoppedWait.pid, SIGKILL);
const std::optional<std::int32_t> stoppedCode = Process::Wait(stoppedWait.pid, std::chrono::seconds(10));
const bool untouched = Read(lockPathK) == identityK && State::LockHolder(artK / "gate-state", "commits", "beefb011").has_value();
const Json dumpK = dumpOf("beefb011", 1, "SEND-READY");
std::jthread endK([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
Write(artK / "gate-state" / "commits-beefb011-run-1.json", DumpFile(dumpK));
lockK.reset();
});
const Process::Captured restarted = waitOn(artK, "beefb011");
endK.join();
const std::string lockBytes = Read(lockPathK);
const std::string dumpBytes = Read(artK / "gate-state" / "commits-beefb011-run-1.json");
const Process::Captured readsOnly = waitOn(artK, "beefb011", {"--appear", "0"});
Check("wait: stopped while its run is live, it leaves the lock and its identity as they were; one started again reports the run; a wait writes neither the lock file nor the dump",
stoppedCode.has_value() && untouched && restarted.returnCode == 0 && restarted.out == std::format("{}\n", State::Summary(dumpK)) && readsOnly.returnCode == 3
&& Read(lockPathK) == lockBytes && Read(artK / "gate-state" / "commits-beefb011-run-1.json") == dumpBytes,
restarted.out + restarted.err + readsOnly.err);
// A wait started after its run's end, and one no run ever shows for.
const Process::Captured late = waitOn(artD, "beefd004", {"--appear", "0"});
const Process::Captured noRun = waitOn(artifactOf("f"), "00000000", {"--appear", "0"});
const Process::Captured noArtifact = Self({"wait", "--hash", "00000000"}, base);
const Process::Captured badAppear = waitOn(artifactOf("f"), "00000000", {"--appear=-1"});
Check("wait: a run live when it begins - its summary as `run` prints it from the dump, its exit code, once the lock goes",
ended.returnCode == 0 && ended.out == std::format("{}\n", State::Summary(dumpA)) && waitedA > 2.0, std::format("{} {:.1f}s", ended.out + ended.err, waitedA));
Check("wait: a run whose lock goes without its final dump (killed) exits 2 naming its log; one a preflight check stopped after the lock, 3 with the message",
killed.returnCode == 2 && killed.out.contains("ended without its final dump") && killed.out.contains((base / "scratch" / "x-run-1" / "log.txt").string())
&& stopped.returnCode == 3 && stopped.out == "preflight: no lens runs: --without drops all\n",
killed.out + killed.err + stopped.out + stopped.err);
Check("wait: a new holder that has not named itself is not the run the lock still names - the run it then names is followed",
newer.returnCode == 0 && newer.out == std::format("{}\n", State::Summary(dumpD2)), newer.out + newer.err);
Check("wait: started after its run's end it exits 3 naming that run's dump; no run, exit 3; no --artifact or a negative --appear is a usage error (3)",
late.returnCode == 3 && late.err.contains(std::format("ended before this wait began: {}", (artD / "gate-state" / "commits-beefd004-run-2.json").string())) && noRun.returnCode == 3
&& noRun.err.contains("no full run of 00000000 seen at its run lock") && noArtifact.returnCode == 3 && noArtifact.err.contains("the following arguments are required: --artifact")
&& badAppear.returnCode == 3 && badAppear.err.contains("invalid seconds"),
late.err + noRun.err + noArtifact.err + badAppear.err);
// Only `run` and `seats` read the seats file, and its lines reach the
// placement readers alone (rounds 8 and 11 of the self-review): a
// `stop` with the board path only in the working directory's .env