Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/diagrams/graph-inventory-room.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Fresh run · inventory room
---
stateDiagram-v2
direction TB
classDef ok stroke-width:1.6px
classDef fail stroke-width:1.6px

[*] --> count : fresh start, lease held
state count <<choice>>
count --> admit : [fewer than 64]
count --> archive : [64, a removed run]
count --> refused : [64 keep data]
archive --> count : oldest removed archived
admit --> [*]
refused --> [*]

note right of archive
Same proof as graph archive:
every owned resource absent,
no pending journal.
end note
note right of refused
graph_retention_budget:
remove the data of a
project you no longer use.
end note

class admit ok
class refused fail
Binary file added docs/diagrams/graph-inventory-room.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions docs/diagrams/graph-inventory-room.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions docs/guides/native-candidate.md
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,18 @@ hostname change mixed with other edits refuses with `graph_hostname_change` and
service name. Every refusal says to revert the edit, or to run
`hack down --remove-data` and then `hack up`. `hack restart` of a stopped graph
refuses and points to `hack up`.
The home's active graph inventory (`run/graphs`) holds at most 64 runs. Removed
runs used to stay in it until an explicit `graph archive`, which no `hack` command
ran, so every `down --remove-data` and every failed fresh start used a place for
good, and the 65th fresh start refused for ever (HACK-2576). Now a start (or a source
job) that needs a new run and finds the inventory full archives removed runs itself, oldest first, under the
provider lease it already holds and with the same proof `graph archive` uses (every
owned resource absent, no pending journal). Runs that keep data are never archived;
when they alone fill the inventory, `hack up` refuses with `graph_retention_budget`
and says to remove the data of a project you no longer use. The archive keeps up to
256 runs; past that the refusal is `graph_archive_budget`. A project whose removed
run was archived this way before its `hack down --remove-data` confirmed the removal
still confirms it: inspection reads a removed run from the archive.
For a shared-source graph that reconciliation does not select (live source, a
carried cache scope, a pending source-device rebind or initializer cache release)
and that was admitted with a retained compatibility contract, ordinary
Expand Down
6 changes: 6 additions & 0 deletions docs/versions/v5/native/limits.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Projects the pool was started with can run together. If a running pool does
not share a project root, `hack up` refuses the start. Stop the pool and add
the root before the next boot. A pool accepts up to eight project shares.

A native home keeps up to 64 graph runs. A run whose data was removed
(`hack down --remove-data`, or a fresh start that failed) is archived on its own
when a new run needs the room. Runs that keep their data are never archived; when
64 of them fill the home, `hack up` refuses with `graph_retention_budget` until you
remove the data of a project you no longer use.

## Native startup limits

See the [native startup settings and refusal codes](https://github.com/hack-dance/hack/blob/next/docs/native/limits.md#native-startup-limits)
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-core/src/provider/graph/admission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ fn check_using(
for config in requested.values() {
budget.add(config)?;
}
if except.is_none() {
super::archive::make_room(candidate, engine)?;
}
let runs = storage_inventory::runs(&candidate.state_root.join("run/graphs"))?;
if runs.len() == 64 && except.is_none() {
return Err(error(
"graph_retention_budget",
"64 graph attempts are retained; explicit archival is required.",
"64 graph attempts that keep data are retained, so no fresh run fits; remove the data of a project you no longer use (`hack down --remove-data` there) and start again.",
));
}
for run in runs {
Expand Down
91 changes: 88 additions & 3 deletions packages/runtime-core/src/provider/graph/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,16 @@ fn read_confirmation(
/// reserved permanently; this operation neither deletes evidence nor permits execution replay.
pub fn archive(candidate: &Candidate, run: &str) -> Result<Receipt, CandidateError> {
let engine = Engine::connect_cleanup(candidate)?;
let (receipt, root) = load(candidate, &engine, run)?;
archive_with(candidate, &engine, run)
}

/// [`archive`] under a provider lease the caller already holds.
fn archive_with(
candidate: &Candidate,
engine: &Engine<'_>,
run: &str,
) -> Result<Receipt, CandidateError> {
let (receipt, root) = load(candidate, engine, run)?;
initializer_cache::require_resolved(&receipt)?;
if receipt.phase != "removed"
|| root.join("state.pending").exists()
Expand All @@ -122,8 +131,8 @@ pub fn archive(candidate: &Candidate, run: &str) -> Result<Receipt, CandidateErr
"Archive requires completed explicit data removal and no pending journal.",
));
}
verify_removed(&engine, &receipt)?;
environment::archive_slots(candidate, &engine, &receipt, &root)?;
verify_removed(engine, &receipt)?;
environment::archive_slots(candidate, engine, &receipt, &root)?;
let target = path(candidate, run)?;
let parent = target.parent().expect("archive parent");
state::private_directory(parent)?;
Expand All @@ -149,6 +158,82 @@ pub fn archive(candidate: &Candidate, run: &str) -> Result<Receipt, CandidateErr
Ok(receipt)
}

/// Graph attempts the active inventory holds (`run/graphs`); a fresh run needs fewer.
const ACTIVE_LIMIT: usize = 64;

/// Room for a fresh run (HACK-2576). Every fully removed attempt stays in the active
/// inventory until something archives it, and nothing in the CLI did, so 64 of them
/// (each `down --remove-data` followed by `up`, each failed fresh start) refused
/// every later start for ever. At the bound, removed attempts are archived oldest
/// first, each proven removed exactly as `graph archive` proves it, until there is
/// room. An attempt that keeps data, has a pending journal or fails that proof is
/// never touched. The caller holds the provider lease.
pub(super) fn make_room(candidate: &Candidate, engine: &Engine<'_>) -> Result<(), CandidateError> {
let (mut count, removed) = archivable(&candidate.state_root.join("run/graphs"))?;
let mut skipped = 0;
for run in removed {
if count < ACTIVE_LIMIT {
break;
}
match archive_with(candidate, engine, &run) {
Ok(_) => count -= 1,
// A run that fails the removal proof stays where it is; any other error
// (the archive's own bound, the engine, the guest) is reported as it is.
Err(error) if error.code == "graph_archive_refused" => skipped += 1,
Err(error) => return Err(error),
}
}
if count >= ACTIVE_LIMIT && skipped > 0 {
return Err(error(
"graph_retention_budget",
"64 graph attempts are retained and not enough of the removed ones among them could be proven removed to make room; inspect them with hack doctor --native-cleanup inspect.",
));
}
Ok(())
}

/// The active inventory's size and, when it is at the bound, its removed attempts,
/// oldest receipt first. A run with a pending journal, an unreadable or foreign
/// receipt, or any phase but `removed` is never selected.
fn archivable(parent: &std::path::Path) -> Result<(usize, Vec<String>), CandidateError> {
if !parent.exists() && !parent.is_symlink() {
return Ok((0, Vec::new()));
}
state::check_private_directory(parent)?;
// Below the bound no receipt is read.
let entries: Vec<_> = fs::read_dir(parent)
.map_err(state::io)?
.take(ACTIVE_LIMIT + 1)
.collect::<Result<_, _>>()
.map_err(state::io)?;
let count = entries.len();
if count < ACTIVE_LIMIT {
return Ok((count, Vec::new()));
}
let mut removed = Vec::new();
for entry in entries {
let Some(run) = entry.file_name().to_str().map(str::to_owned) else {
continue;
};
let state = entry.path().join("state.json");
let pending = entry.path().join("state.pending");
if !hex(&run, 32) || pending.exists() || pending.is_symlink() {
continue;
}
let Ok(receipt) = state::read::<Receipt>(&state) else {
continue;
};
if receipt.phase == "removed" && receipt.run == run {
let modified = fs::symlink_metadata(&state)
.and_then(|metadata| metadata.modified())
.map_err(state::io)?;
removed.push((modified, run));
}
}
removed.sort();
Ok((count, removed.into_iter().map(|(_, run)| run).collect()))
}

pub(super) fn verify_removed(engine: &Engine<'_>, receipt: &Receipt) -> Result<(), CandidateError> {
for resource in receipt.resources.values() {
if resource.cache.is_some() && resource.phase == "released" {
Expand Down
Loading
Loading