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.
90 lines
4.9 KiB
C++
90 lines
4.9 KiB
C++
// SPDX-License-Identifier: AGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
export module Crafter.Review:State;
|
|
import std;
|
|
import :Json;
|
|
|
|
// unit-state.json (per-unit reuse), the run lock, the usage totals and the
|
|
// stdout summary. unit-state.json is merged on write, because drivers share
|
|
// one gate-state dir, and one full run per hash holds the hash's lock: every
|
|
// driver on the machine reads and writes these, so the files, the locks and
|
|
// the layout do not change.
|
|
export namespace Crafter::Review::State {
|
|
// {version: 1, entries: {}, personaPass: null}
|
|
Json EmptyState();
|
|
// The file as JSON, whatever its shape, or the empty state when it is
|
|
// missing or does not parse.
|
|
Json LoadState(const std::filesystem::path& path);
|
|
// This run's stores and drops applied to the state on disk — pure. Drops
|
|
// win over sets; an entry of `sets` that is not a whole pass gets its
|
|
// clean-read count from the FILE's previous entry (or the one-read
|
|
// synthesis of an entry stored before the count was kept) plus `read`
|
|
// unless that (hash, round) is counted already; `persona` sets
|
|
// personaPass when given. Garbage on disk is the empty state. `sets` is
|
|
// an object; a null `read` counts nothing.
|
|
Json MergeState(const Json& disk, const Json& sets, std::span<const std::string> drops = {}, const Json& read = nullptr, const Json& persona = nullptr);
|
|
// The whole state written as it is, through `<path>.tmp`.
|
|
void SaveWhole(const std::filesystem::path& path, const Json& state);
|
|
// Merge-on-write: the file re-read under `<path>.lock`, this run's deltas
|
|
// applied (MergeState), written atomically; returns the merged state. Two
|
|
// drivers on one gate-state dir would otherwise overwrite each other's
|
|
// stores, and a --no-reuse run would wipe the file from its empty object.
|
|
Json SaveState(const std::filesystem::path& path, const Json& sets, std::span<const std::string> drops = {}, const Json& read = nullptr, const Json& persona = nullptr);
|
|
|
|
// ---- one full run per hash -------------------------------------------------
|
|
// Another full run holds this hash's lock; what() names the holder.
|
|
class RunLocked : public std::runtime_error {
|
|
public:
|
|
using std::runtime_error::runtime_error;
|
|
};
|
|
std::filesystem::path LockPath(const std::filesystem::path& gateDir, std::string_view mode, std::string_view hash8);
|
|
// The holder's JSON, {} when there is none or it does not parse.
|
|
Json ReadLockInfo(const std::filesystem::path& path);
|
|
std::string Describe(const Json& info);
|
|
|
|
// An exclusive, non-blocking flock on gate-state/.run-<kind>-<hash8>.lock,
|
|
// kept for the life of the process: the kernel releases it when the
|
|
// holder dies, and no child inherits it. The file is never unlinked: an
|
|
// unlink and re-create would hand two drivers one hash.
|
|
class RunLock {
|
|
public:
|
|
// Throws RunLocked naming the holder. A probe holds the lock shared
|
|
// for an instant (LockHolder): it is tried for 200 ms first. Taken,
|
|
// it clears the last run's identity.
|
|
RunLock(const std::filesystem::path& gateDir, std::string_view mode, std::string_view hash8);
|
|
~RunLock();
|
|
RunLock(const RunLock&) = delete;
|
|
RunLock& operator=(const RunLock&) = delete;
|
|
// The holder's identity, once it knows its run number: one JSON
|
|
// document, truncated and rewritten.
|
|
void WriteInfo(const Json& info);
|
|
// One more field in the identity last written (a preflight check's
|
|
// message, for `wait`); nothing before the identity is written.
|
|
void AddInfo(std::string_view key, const Json& value);
|
|
|
|
private:
|
|
std::int32_t fd = -1;
|
|
Json written;
|
|
};
|
|
// The run lock at `path` as one probe sees it, nullopt when the file
|
|
// cannot be opened (there is none): taken shared for an instant (a failure
|
|
// to take it reads as held), and the identity - read under that hold when
|
|
// nobody holds the lock, so no holder can be rewriting it then.
|
|
struct LockState {
|
|
bool held = false;
|
|
Json info = Json::object();
|
|
};
|
|
std::optional<LockState> ProbeRunLock(const std::filesystem::path& path);
|
|
// Who holds this hash's run lock - its identity, {} from its clearing of
|
|
// the last run's until it names itself - nullopt when nobody does
|
|
// (ProbeRunLock): a test or dry run takes no lock but numbers its scratch
|
|
// dir past a live holder.
|
|
std::optional<Json> LockHolder(const std::filesystem::path& gateDir, std::string_view mode, std::string_view hash8);
|
|
std::int64_t NextRunNumber(const std::filesystem::path& gateDir, std::string_view mode, std::string_view hash8);
|
|
|
|
// The usage of a set of pass records, summed in the order they come.
|
|
Json UsageTotals(std::span<const Json> records);
|
|
// The stdout summary of a run's dump.
|
|
std::string Summary(const Json& dump);
|
|
}
|