# Pekit Technical Reference Manual

> How pekit loads a workspace, evaluates targets into steps, keys, sandboxes, and records them, keeps its store, and fails.

---

# 1.1 Overview

_Pekit / Pekit TRM / Introduction_

> What pekit is as an implementation, and the decisions that shape it which its name and its commands don't reveal.

pekit is a build system that embeds GCSL. It loads a workspace of GCSL
files, evaluates the definitions it is asked for in GCSL's graph mode, and
runs each call of a function marked `#[hermetic]` as a step: a unit of
work run in a bubblewrap sandbox holding only its inputs, keyed by those
inputs, and recorded in a content-addressed store. It is one Rust crate,
`pekit`, with the `pekit` command as its only executable.

## 1.1.1 What is unusual about it

Three decisions shape the implementation more than any other.

**The build language is a general programming language, evaluated
incrementally.** A target's code is not a declaration that pekit reads and
then plans from. It is a GCSL program that pekit evaluates as a *unit*:
the program runs until it needs a result pekit has not given it, stops,
and runs again from where it stopped once the result is given. A call of a
hermetic function does not run where it is made; GCSL records it as a node
of an action graph, and the program carries on with a *hole* where the
result will be. So a target's code learns every step it can reach before
any runs, and still decides what to build next from what an earlier step
made. One thread evaluates every unit; what runs in parallel is the
programs steps run, on a pool of threads.

**Every path is in one logical layout.** The workspace is at `/w` and
every output is under `/pekit-out`, in GCSL values, in keys, and in every
sandbox, whatever the host's paths are. A step's output directory is named
by a digest of what the step is, not by the target that reached it. So
the same call is the same step, at the same paths, in every build,
checkout, and machine of a platform, and two targets that make it share
it.

**Steps are keyed by what they read, and so are the programs inside
them.** A step's key holds the contents of every file it is given, and of
the GCSL contexts only those it read as it ran. A step that runs again,
because one input changed, runs its programs against keys of their own:
each program a step runs is recorded by what it can see, and a program that
sees what it saw before is not run, its writes put back from the store.

## 1.1.2 The rest of this manual

[Loading a Workspace](/pekit/technical-reference/loading/finding-the-workspace.md) describes how pekit
reads and checks a workspace and its libraries. [Evaluation](/pekit/technical-reference/evaluation/units-and-the-graph.md)
describes how targets become steps, and [Steps](/pekit/technical-reference/steps/identities.md) how
steps are named, keyed, and recorded. [Sandboxes](/pekit/technical-reference/sandboxes/staging.md) and
[The Store](/pekit/technical-reference/the-store/store-layout.md) describe where steps run and what is kept.
[Failure Modes](/pekit/technical-reference/failure-modes/failures-of-a-build.md) collects what goes wrong.

---

# 1.2 What This Manual Covers

_Pekit / Pekit TRM / Introduction_

> The scope of this manual, the source it describes, and the documents that hold what it leaves out.

This manual describes the `pekit` crate in pekit's repository, and the
behavior of the `pekit` command built from it. It covers the engine's
state, its keys and on-disk formats, its concurrency, its limits, and its
failure behavior, with the reasoning behind its design where the reasoning
is the implementation's own.

A manual's authority comes from the software. Where this manual and the
source disagree, the source is right and the manual is stale. Nothing here
is a promise about a later version: the encodings of keys, records, and
trees change whenever the implementation needs them to, and old records
are then not found.

## 1.2.1 What is covered elsewhere

These documents hold what this manual leaves out:

| Subject | Where |
|---|---|
| Using pekit: commands, workflows, and concepts | The other topics of this product, starting at [Pekit](/pekit/getting-started/pekit.md) |
| Every command, option, exit status, environment variable, and file | `pekit(1)` |
| The lock file's format | `pekit.lock(5)` |
| The GCSL language, its standard library, and graph mode | The [GCSL documentation](/gcsl/getting-started/gcsl.md), `gcsl(5)`, and `gcsl-stdlib(3)` |
| What a library such as gcc or rust does | The library's own README and source |
| Design history, and decisions made and changed | The task records of the work that produced them |

Where this manual names a function or type of the source, such as
`Store::put_tree`, it does so to help a reader find the implementation. The
names are not an interface.

---

# 1.3 Terminology

_Pekit / Pekit TRM / Introduction_

> The terms this manual uses with a meaning of pekit's own.

The terms in this manual have these meanings. GCSL's own terms, such as
context, effect, and definition type, have the meanings GCSL's
documentation gives them.

| Term | Meaning |
|---|---|
| Blob | A file's contents in the store, named by their SHA-256 digest. |
| Composition | A directory a step makes with `fs.compose` or `fs.compose_dir`, sealed once made. |
| Effect | A built-in call that touches the world, such as `exec` or `fs.write`, run as a job on the pool. |
| Execution platform | The machine steps run on: the machine pekit runs on, named as `<arch>-<os>`. |
| Exposure | Writing an output into `pekit-out/` on the host, once something that is not hermetic might read it there. |
| Host snapshot | The host's system directories that steps see outside a strict build, and the fingerprint that identifies them. |
| Host view | The effect context of a target's own code: the host, with the logical layout mapped onto it. |
| Identity | A 64-bit digest of what a step is, apart from its inputs' contents, naming its output directory. |
| Logical layout | The paths every GCSL value and sandbox uses: `/w` for the workspace, `/pekit-out` for outputs. |
| Manifest | For a step's base receipt, every set of contexts a run of it has read. |
| Materialized tree | A store tree made once as a directory of links to its blobs, for steps to mount. |
| Pool | The threads, `--jobs` of them, that run effects and pekit's own work for steps. |
| Program record | The record of one program a step ran, keyed by what the program could see. |
| Receipt | The SHA-256 key of a step: everything that can change what it gives. |
| Record | What a step gave, kept in the store under its receipt. |
| Scratch directory | A directory kept between a step's runs for a step marked `#[scratch]`. |
| Staging directory | The directory on the host that a step's sandbox is made of. |
| Step | A call of a hermetic function made by a target's code, run in a sandbox and recorded. |
| Store | The content-addressed store under the cache directory, shared by every workspace and process on the machine. |
| Target | A definition named on the command line, evaluated as a unit. |
| Token | The 16 random hexadecimal digits a process names its temporary files and directories with. |
| Tree | A directory in the store, kept as a blob of its sorted entries in a canonical encoding. |
| Unit | A GCSL evaluation that stops at each result it needs and runs again from there: a target's, or a step's body. |

---

# 1.4 Compatibility

_Pekit / Pekit TRM / Introduction_

> The platforms pekit runs on, the programs it needs, and what is shared or not between versions and machines.

pekit runs on Linux. It needs:

- `bwrap`, bubblewrap, on `PATH`, for every step and every program a
  target's code runs, and unprivileged user namespaces for it to make.
  pekit never nests one bubblewrap inside another, so a host that forbids
  that, such as Ubuntu, is supported.
- `git`, on `PATH`, for `pekit lock`, for putting back pinned libraries,
  for `fetch_git`, and for `pekit release`.
- A file system for the cache directory that supports hard links.
- A kernel that lets bubblewrap mount an overlay in an unprivileged user
  namespace, for a `Dir.sandbox` of a composition, which pekit makes with
  bubblewrap's `--overlay`.

The execution platform is the machine pekit runs on, named as Rust names
architectures and operating systems: `x86_64-linux`, `aarch64-linux`.
Steps never run on another machine.

## 1.4.1 What is shared

The store is shared by every workspace and every pekit process of the same
user on a machine. Its records are found only by the receipts of the same
encoding: a version of pekit that changes how receipts, identities, trees,
or records are encoded finds none of the old ones, and runs the steps
again. Nothing is migrated.

A step's receipt names the execution platform, and outside a strict build
the host snapshot's fingerprint. So a store copied between machines reuses
only what the machines agree on: a strict build's steps on any machine of
the platform, and others only on a machine with the same installed
packages.

A step given a secret is keyed by an identity hashed with the machine's own
key, so it is reused only on the machine that recorded it.

---

# 2.1 Finding and Checking the Workspace

_Pekit / Pekit TRM / Loading a Workspace_

> How pekit finds a workspace's root, which files it reads, how it checks them through GCSL in the logical layout, and how it tells which workspace files changed since the last build.

Every command but `pekit clean` starts by loading the workspace (`load`).
Loading finds the root, reads the GCSL sources, and checks them. Nothing
is evaluated until every source checks.

## 2.1.1 The root

The workspace's root is the nearest directory, from the current directory
upward, that holds a `workspace.gcsl` (`gcsl_disk::find_root`). With none,
the command fails with an error that names the directory it searched from.
The root is canonicalized, so a workspace reached through a symbolic link
is the same workspace as one reached directly.

## 2.1.2 The sources

The sources are read as the `gcsl` command reads them
(`gcsl_disk::read_workspace`): every `.gcsl` file under the root, except
under a hidden directory, a hidden file, and any directory below the root
that holds its own `workspace.gcsl`. A nested workspace is never part of
the outer one.

Each library the sources import is read too, with every library those
import, from where [Libraries and the Lock](/pekit/technical-reference/loading/libraries-and-the-lock.md)
says. GCSL's standard modules are compiled into pekit and come first in
the order of inputs, so that a diagnostic names its file by its input
number correctly.

## 2.1.3 Checking

The sources are checked with GCSL's standard library
(`gcsl::workspace::check_with_standard`), rooted at `/w`, the workspace's
place in the logical layout. So `fileroot.workspace` is `/w`, and
`fileroot.this` in a directory `app` is `/w/app`. A diagnostic is shown
against its file, named relative to the root, and the command fails with
status 1 if any is an error.

Two rules are pekit's own, checked once GCSL's checking has passed:

- A `build` and a `script` of the same name in the same directory are an
  error, since `pekit run NAME` could mean either.
- A build, test, or package that can reach a call of a script fails to
  check. The prelude's `script` definition type reads the context
  `^script_session`, which has no default and which only `pekit run`
  binds, so GCSL reports "context `script_session` has no value in this
  definition".

## 2.1.4 A workspace checked afresh

A build leaves state in the checked workspace: each file's top level and
each memoized call's result, which name what it fetched, unpacked, and
built. A second build in the same process, `--check-reproducible`'s second
pass or a release's publishing pass, has to make those itself. So every
build after the first checks the workspace again from the same sources
(`Loaded::ready`), rather than reusing the evaluated one.

## 2.1.5 Which files changed

pekit tells a workspace file by its contents. To avoid reading every file
on every build, the store keeps, for each workspace root, the *stamp* of
each file it read: its device and inode, size, mode, and modification and
change times, with the digest of its contents and whether it is
executable. They are in the store's
`seen/` directory, in a file named by the SHA-256 of the root's path. The
file is binary, not JSON, because a large workspace has a hundred
thousand stamps and every build reads them all. Stamps that an older pekit
kept as JSON are read once and replaced.

A file whose stamp matches is taken to have the recorded digest, unread. A
file whose stamp differs, or which is new, is read and hashed, and so is a
file whose blob is gone from the store (see
[Blobs and Trees](/pekit/technical-reference/the-store/blobs-and-trees.md)). A stamp taken
in the same tick of the file system's clock as the build began, or later,
isn't trusted: a file changed again within that tick would keep the same
times. The next build reads such a file again. Stamps of files that are
gone are dropped as the build ends. The file is written again only if its
stamps changed.

The workspace's path is noted beside its stamps, in `seen/<digest>.root`.
A build that ends with no other pekit using the store removes the stamps of
each workspace whose directory is gone. Stamps from a pekit that noted no
path are removed once they are 30 days unchanged.

## 2.1.6 Files that change as they are read

A workspace file or directory that a step is given can be changing as
pekit reads it, as a package repository is while a package is published
into it. pekit reads a directory one entry at a time, so it could read
part of it before a change and part after. It could fail on an entry that
was listed and is gone, or read a ref without the manifest it names.

So pekit keeps what it saw: the names each directory listed, and the
stamp of each file and link. Once it has read the directory, it looks
again. If anything differs, or is gone, the directory changed as it was
read. pekit discards what it read, even if the read failed because of
the change, and reads it again after a pause. The pause starts at 50
milliseconds and doubles each time, up to 2 seconds. A file is read the
same way, by its stamp. A read that fails while nothing changed fails the
build.

A directory still changing after 30 seconds, and after at least five
reads, fails the build with "`PATH`
kept changing while it was read, `HOST-PATH` last: wait for whatever is
writing there to finish, then build again". pekit never gives a step a mix
of before and after.

A file changed in place within one tick of the file system's clock, to
the same size, after pekit read it, keeps its stamp, so pekit cannot see
that change. A writer that renames each file into place, as ppk does, is
always seen.

---

# 2.2 Libraries and the Lock

_Pekit / Pekit TRM / Loading a Workspace_

> Where pekit finds a library, where it places one in the logical layout, how pekit lock pins and exports libraries, how a build puts back and checks them, and what a strict build requires.

A library is a directory of GCSL files named by an import path, such as
`github.com/gcslib/gcc`. pekit finds, places, and pins libraries itself;
GCSL only reads the sources it is given.

## 2.2.1 Where a library is found

A library's directory is looked for in each directory of
`$GCSL_LIBRARY_PATH`, in order, then in `.gcsl/libraries/` at the
workspace's root, and the first found is used. Its sources are the `.gcsl`
files directly in that directory.

Wherever a library was found, its sources are given to GCSL at
`/w/.gcsl/libraries/<path>/` in the logical layout
(`gcsl::SourceFile::with_dir`). A relative path in a library's code
therefore names the same logical path in every workspace. To the rest of
the engine, a library's files are workspace files under that path: they
are hashed, put into the store, and staged as any are. Only where one is
on the host differs, for a library found through `$GCSL_LIBRARY_PATH`
(`Libraries::host`).

So a step a library makes has the same identity, the same receipt for the
same library contents, and the same outputs in every workspace, whether
the library came from a checkout or from the lock.

## 2.2.2 The lock

`pekit.lock` is JSON at the workspace's root, described in
`pekit.lock(5)`: `sources`, each a prefix of import paths with a git
repository, a directory in it, and a revision; and `libraries`, each pinned
by its path, repository, directory, commit, and the digest of its exported
tree.

A library's source is the first source whose prefix is its path or a part
of its path ending before a `/`. A library no source covers is given one by
the convention of Go's modules: the first three parts of its path,
`HOST/OWNER/REPO`, are the prefix, `https://HOST/OWNER/REPO` the
repository, its root the directory, and `HEAD` the revision. The source is
added to the lock.

## 2.2.3 Pinning

`pekit lock` reads the import paths of the workspace's sources, without
checking them, and works through them and every library they import,
transitively:

1. A library the lock pins, without `--update`, is exported again at its
   pinned commit.
2. Any other is fetched from its source: its repository's bare clone is
   made or fetched, the source's revision resolved to a commit with
   `git rev-parse`, and the library exported at that commit.
3. Each exported library's tree is put into the store, and its digest
   recorded as the pin's `tree`.

Each pin that changed is reported. Each library the lock pinned that
nothing imports any more is dropped, and its directory under
`.gcsl/libraries/` removed. A library's directory can be inside another
library's, as `example.org/sdk/tools` is inside `example.org/sdk`. A
dropped library's directory that is inside a library still pinned is that
library's, so it stays. A library still pinned whose directory was inside
the one removed is exported again. The lock is written with its libraries
sorted by path.

Two libraries whose directories nest, one's directory inside the other's,
are pinned at one commit or not at all (`lock::nested`). The outer
library's export holds the inner one's directory, so the two agree only if
both have one repository and commit, and the inner library's directory in
the repository is the outer one's followed by the rest of its path.
Otherwise each export replaces the other's files, and whichever is exported
last wins. Before each library is exported, its pin is compared with every
pin made so far. A pin that disagrees fails the command, naming both pins,
and the lock is not written. Libraries exported before the failure stay as
exported, so the next build can find the outer library differing from its
pin.

A library is exported with `git archive` of the commit's directory, into
`.gcsl/libraries/<path>/`, replacing what was there. Repositories are bare
clones under the cache directory's `git/`, fetched only to resolve a
revision or to get a pinned commit the clone lacks. Git runs as the `git`
program found on `PATH`.

Each successful export records its commit and tree digest, by import path,
in `.gcsl/pekit-exports.json`. This local JSON file is outside every library
tree and is not committed. The export is unpacked and hashed in a temporary
directory beside its destination before replacing it. The old directory is
renamed aside, the complete new directory renamed into place, and the old
one removed. Only then is the record written through a temporary file and
renamed into place. An interrupted replacement cannot record a half-export
as the new one.

## 2.2.4 Putting back and checking

Before loading, `build`, `test`, `run`, `query`, `package`, and `publish`
restore the lock's libraries (`lock::restore`):

- Every pair of pins is compared, as pinning compares them, before anything
  is exported. Two nested libraries pinned at two commits fail the command.
- A pinned library whose directory is missing is exported from its commit.
- A pinned library whose directory is there is put into the store as a
  tree, and its digest compared with the pin's. If it matches but its
  record is missing or names another commit or tree, it is recorded: it is
  what an export would write, so it refreshes when the pin moves. If it
  differs but still
  matches pekit's export record, it is an untouched older export: it is
  replaced from the pinned commit and its record updated. Outer libraries
  are handled before libraries inside them; after an outer export, each
  pinned library inside it is exported and recorded at that commit too.
  Putting a library into the store reads only the files whose stamps
  changed, as with a workspace's files (see
  [Finding the Workspace](/pekit/technical-reference/loading/finding-the-workspace.md)). The stamps of
  library files are kept apart, in `seen/<digest>-libraries.stamps`.
- A differing library whose tree does not match its record, or has no
  record, is left alone and reported with a recovery hint: preserve any
  edits, remove `.gcsl/libraries/<path>/`, and pekit puts back the pin.

Outside a strict build, each difference is a warning, and the build goes on
with the library as it is. Refreshing an untouched older export happens
outside strict builds too. An export made by an older pekit has no record;
if it matches the pin it is recorded then, and if it already differs it
needs removing once.

A strict build fails, listing every problem, if any of these holds
(`lock::check_strict`):

- The workspace imports a library and has no `pekit.lock`.
- A library it loaded isn't pinned.
- A pinned library differs from its pin.
- `$GCSL_LIBRARY_PATH` is set and not empty.

## 2.2.5 What the lock tells a build

pekit sets the `libraries` module's `pinned_libraries` context, where the
workspace declares it, to the lock's libraries, each with its path,
repository, directory, and commit. A library can compare these with what it
takes another way, as the gcslib rust library compares a git crate's commit
in `Cargo.lock` with the commit a library of the same repository is pinned
at.

---

# 2.3 Selecting Targets

_Pekit / Pekit TRM / Loading a Workspace_

> How pekit turns the names and globs on its command line into definitions, and how several selected targets become one build.

A target is a definition made with a definition type: `build hello` is the
definition `hello` of the definition type `build`. The command decides the
definition type: `build` for `pekit build`, `test` for `pekit test`,
`package` for `pekit package`, `pekit publish`, and `pekit release`, the
`--kind` option's for `pekit query`, and `script`, or else `build`, for
`pekit run`. Selection is `target::select`.

## 2.3.1 Names

A name is a definition's path alone, `hello`, or qualified by its directory
relative to the root, `app/hello`, with `/hello` for one in the root
directory. A path alone resolves in order:

1. The one definition of that path in the workspace, wherever pekit runs
   from.
2. Of several, the one in the directory pekit runs from.
3. Otherwise none. The command fails with "`build hello` is defined in
   several places", listing each by its qualified name.

A qualified name means the definition in that directory, or none. A name
that matches nothing fails with "no `build` is named `hello`", with the
closest names there are.

## 2.3.2 Globs

A name with `*` in it is a glob. It selects every definition of the
definition type that it matches, relative to the directory pekit runs from,
or to the root if it starts with `/`. Its last part matches the
definition's name, and the parts before it match directories:

- `*` in a part matches any run of characters within the part.
- `**` matches any number of directories, including none. As the last part,
  it matches every name in them too.
- `.` matches the directory itself, and every name in it.

So `app/*` is every definition of `app`, `pkgs/*/*` every definition of a
directory directly below `pkgs`, and `**` every definition at or below the
current directory.

A glob matches the workspace's directories alone. A library's definitions
are in no directory of the workspace, so no glob selects them, whatever
their names. A library's tests therefore never run in `pekit test` of a
workspace that imports it.

`pekit test`, `pekit package`, and `pekit query rdeps` select `**` when
given no names.

## 2.3.3 Several targets

What several names and globs select is selected once, and the targets are
sorted by their qualified names. They are evaluated as one build, against
one action graph: each target is a unit of its own, and a step two units
reach is one node, run once. Reports come in the sorted order.

For `pekit run`, a definition of one name in one directory is a script or a
build, never both, by the check of
[Finding and Checking the Workspace](/pekit/technical-reference/loading/finding-the-workspace.md). Of one
name in two directories, `pekit run` asks for the directory, as any
command does.

---

# 2.4 Root Contexts

_Pekit / Pekit TRM / Loading a Workspace_

> The GCSL contexts pekit binds around every target, where each value comes from, and when pekit binds it.

pekit evaluates every target as if wrapped in a `with` block that binds
some GCSL contexts (`root_contexts` and `root_fields`). It binds a context
only where the workspace declares it: where some loaded module, the
standard library's included, declares the context. A workspace that never
imports `keychain`, for example, has no `^keychain_profile` to bind.

| Context | Module | Value |
|---|---|---|
| `host_platform` | prelude | The execution platform, as `{arch; os}`. |
| `platform` | prelude | The platform `--platform` names, if given. Otherwise unbound, and its default is `^host_platform`. |
| `toolchains` | `toolchain` | `pekitconf.toolchains` of `workspace.gcsl`, if it has the field. |
| `keychain_keys` | `keychain` | `pekitconf.keychain` of `workspace.gcsl`, if it has the field. |
| `keychain_profile` | `keychain` | The `--profile` option's value, `dev` by default, or `release` in `pekit release`. |
| `packagers` | `packaging` | `pekitconf.packagers` of `workspace.gcsl`, if it has the field. |
| `pinned_libraries` | `libraries` | The libraries `pekit.lock` pins, each as `{commit; dir; git; path}`. |
| `script_session` | prelude | For `pekit run` of a script only: `{args}`, the words after the script's name. |

The fields of `pekitconf` are evaluated as part of `workspace.gcsl`, in
GCSL, so a mistake in one is a check error of that file. A `pekitconf`
without one of these fields leaves the context at its default: the
`toolchain` module's `[Toolchain::C system_c]`, and an empty keychain and
packager list.

`toolchains`, `keychain`, `packagers`, `contexts`, and `require_strict` are
the only fields pekit reads. `require_strict` sets no context, and is read
before the build, as [Strict and Reproducible
Builds](/pekit/technical-reference/commands/strict-and-reproducible-builds.md) describes. Before it
prepares any target, pekit fails the build if:

- `pekitconf` is not a record of named fields;
- it has any other field, which a different pekit might read but this one
  would ignore; or
- it has `toolchains`, `keychain`, or `packagers` but no loaded file
  declares the context the field sets, which `import toolchain`, `import
  keychain`, or `import packaging` would.

## 2.4.1 Contexts the workspace sets

`pekitconf.contexts` sets any other context, each of its fields the context
of its name, as [Setting a Context for the Whole
Workspace](/pekit/building/setting-a-context-for-the-workspace.md) shows. pekit binds
them in the same `with` as the fields above, so a target's own `with`
binds over them. Before it prepares any target, pekit fails the build if:

- `contexts` is not a record whose fields name contexts, such as a record
  type with a rest-field;
- a field names a context that no loaded file declares; or
- a field names one of the contexts in the table above, which pekit sets
  itself.

GCSL checks each value against its context's type, as it checks a `with`
binding, so a value of another type is a check error. Each value is
evaluated as part of `workspace.gcsl`, in the declared defaults of every
context, so it can't depend on a target's `^platform`.

## 2.4.2 Contexts and keys

Binding a context changes no step that doesn't read it. A step is keyed by
the contexts it read as it ran, as [Receipts](/pekit/technical-reference/steps/receipts.md) describes,
so registering another toolchain reruns no step that didn't read
`^toolchains`. A library that resolves its toolchain in a parameter default
reads `^toolchains` outside the step, and gives the step the toolchain
itself, which keys it as an argument. `^script_session` has no default, so
a step that could read it is one only a script can reach.

---

# 3.1 Units and the Action Graph

_Pekit / Pekit TRM / Evaluation_

> How pekit evaluates each target as a GCSL graph-mode unit, which calls become steps, how a unit stops and resumes, and how a build that cannot progress fails.

pekit evaluates every target in GCSL's graph mode, against one action
graph for the whole build. GCSL's [Running Steps for a
Host](/gcsl/embedding/graph-mode.md) page describes graph mode; this article describes
what pekit does with it.

## 3.1.1 Prepared calls

Each selected target is a *prepared call* of its definition: `build hello()`,
`test greeting()`, or `script deploy()`. Two commands wrap the call in a
function of the standard `packaging` module, since pekit can't call a GCSL
function value from outside GCSL:

- `pekit package` prepares `packaging.pack(package NAME())`, which packs the
  package with each of `^packagers`.
- `pekit publish` and `pekit release` prepare
  `packaging.publish(package NAME(), "STORE")`. The store is an argument,
  not a context, so that publishing into a second store keys no step
  differently.

A prepared call binds its own definition as a reference's call does
(GCSL's `run_prepared`), so the steps it makes are named under that
definition. Each prepared call is evaluated as a *unit* of its own.

## 3.1.2 Steps and other calls

In graph mode, a call of a function whose attributes defer it is not run
where it is made. GCSL adds it to the graph as a node, and the calling code
goes on with a *hole* where its result will be. pekit takes a deferred node
as a step if one of its function's attributes is `hermetic` and not
`hermetic(false)`.

Inside a step's own unit, a deferred call is not a node: it runs inline,
as part of the step. So only a call that a target's code makes, directly or
through any number of ordinary functions, becomes a step.

Every effect, a built-in that touches the world, is a node too, carrying
its position in program order. pekit runs a step's effects in the step's
sandbox, in the step's program order, and a target's own effects on the
host view, one at a time, in the target's program order. A target's
effects whose order cannot matter run out of order, as
[Order](/pekit/technical-reference/evaluation/the-pool.md#order) describes.

## 3.1.3 Stopping and resuming

A unit runs until it ends or until it needs a node's result that pekit
hasn't given: a step's value it reads, an effect's result, or a memoized
call another unit is computing. It then stops, and GCSL keeps its position
as data. Once pekit gives the result, the unit resumes from where it
stopped, without running any code twice.

A unit that ends with a value whose holes are steps not yet run is done
evaluating: its value is complete once those steps give their results. A
target is built when its value has no holes.

Within a unit, GCSL schedules its own branches, and runs everything it can
before stopping. Where several branches fail, the failure reported is the
one a sequential run would have reached first.

## 3.1.4 The build loop

The engine repeats, until every target is done (`build_all_observed`):

1. Run each unit that can progress: one whose results are given.
2. For each step node a unit needs whose inputs are given, look its
   receipt up in the store. A hit gives the step's recorded result. A miss
   stages the step's sandbox on the pool, then runs the step's body as a
   unit of its own.
3. Queue each ready effect of a step's unit on the pool, and run each
   target's next effect in program order.
4. Take each finished piece of work from the pool, and give its result to
   the node that waited for it.

A memoized call or a `lazy` that two units need is computed by the first
to reach it, and the other waits for it. Once another unit waits for it,
the target's own effects inside it don't wait for the target's earlier
work outside it, so they can reach the system out of the target's program
order. They still run one at a time. GCSL's [Running Steps for a
Host](/gcsl/embedding/graph-mode.md#units-that-share-work) page explains why. A target
that `--no-dynamic` fails, or that a query stops, while its unit waits
gives up the shared work it was computing, and a target waiting for that
work computes it itself.

If no unit can progress and no work is running, the units wait for each
other. When they wait in a circle through memoized calls or `lazy` values,
some call needs its own result, and the build fails with "this call
depends on its own result". Each call in the circle is labeled with the
target or step that needs it and the one computing it. Any other such
build fails with "the build's targets and steps wait for each other's
shared results".

## 3.1.5 Naming steps under definitions

A step is reported, and its outputs linked, under the target whose unit
first met it, or under the target of the step that met it. Its identity
and directory don't depend on that, as [Identities](/pekit/technical-reference/steps/identities.md)
describes. A step made inside a `build` called from another target, a
test, or a script is made under the innermost definition around the call
(GCSL's `Workspace::step_definition`), so `build lib()` called anywhere
makes the steps `pekit build lib` makes.

---

# 3.2 The Pool

_Pekit / Pekit TRM / Evaluation_

> The threads that run a build's effects and pekit's own work for steps, what each kind of work is, and why evaluation stays on one thread.

GCSL's values are reference-counted for one thread, so one thread evaluates
every unit of a build. What runs in parallel is work that needs no GCSL
value: programs, file-system effects, and pekit's own handling of whole
trees. That work runs on the *pool* (`Pool`): two fixed sets of threads,
each taking work from a queue of its own.

## 3.2.1 Its size

The pool has `--jobs` threads for effects, by default as many as the
machine has processors. A running program holds its thread until it ends,
so `--jobs` is the most programs a build runs at once. A step's program
also waits on its thread for the machine's budget, which bounds every
build's programs together (see [The Machine's
Budget](/pekit/technical-reference/evaluation/the-machines-budget.md)). A step's own `make` or Cargo borrows
the budget's free jobs through its jobserver.

The pool has as many threads again for the rest of its work, so that
programs waiting for the budget hold up none of it.

## 3.2.2 Kinds of work

Each piece of work (`Work`) is one of these:

- **An effect of a unit,** with its effect context and its deadline: a
  step's effect in the step's sandbox, or a target's effect on the host
  view.
- **A fetch,** answered from the store or downloaded.
- **A read of an output** by a target's code, answered from the store's
  tree of it while the output isn't exposed.
- **Staging** a step's sandbox before its body runs.
- **Keeping** a step's result: putting every file and directory it names
  into the store, once it has given one.
- **A program a step runs whose result can be recorded,** with the hashing
  and placing of trees that finding or recording it takes. See [Program
  Records](/pekit/technical-reference/the-store/program-records.md).

Staging and keeping place or hash whole trees, often of tens of thousands
of files, so they run on the pool beside the programs, never on the thread
that evaluates. A step's unit runs once its sandbox is staged, and the step
is recorded once its files are kept.

Each piece of work reports what it came to, with the thread that ran it and
when it started and ended, for the build's events, and the store's files it
used, for eviction.

## 3.2.3 Order

Effects of different steps run in any order and at the same time. A step's
own effects run in its program order, one after another, since they share
the step's sandbox. A target's own effects run one at a time in the
target's program order, since they share the host.

The host view isolates some of the target's effects
(`HostView::isolates`). GCSL makes an isolated effect as soon as the code
reaches it and has its arguments, without waiting for earlier effects in
program order (see [Code that waits](/gcsl/embedding/graph-mode.md#code-that-waits)).
The isolated effects are:

- A fetch or an unpacking, which pekit answers by content.
- `file.read` and `dir.files`. Each waits only for the `File` or `Dir` it
  reads. `file` and `dir` are not effects in graph mode, so outside a
  strict build such a read can run before a program or write of the
  target's that is earlier in program order, and find what was there
  before it.
- In a strict build, `fs.exists` and `fs.read`. Each is given only a path,
  so outside a strict build it waits for the target's earlier programs and
  writes, which can change what is at that path.

A strict build refuses a target's programs and writes, so nothing the
target does changes what any of these reads finds.

One more exception holds for effects that are not isolated: those inside
a memoised call or `lazy` that another target or step is waiting for. GCSL
then orders them only after earlier work inside that call, not after the
target's other earlier work, still one at a time per target, so that two
targets that need each other's shared calls don't wait for each other (see
[Code that waits](/gcsl/embedding/graph-mode.md#code-that-waits)). Such a call's effects
already reach the host only once, in whichever target got to the call
first, so the other targets never saw them in their own order anyway.

---

# 3.3 A Target's Own Effects

_Pekit / Pekit TRM / Evaluation_

> How pekit runs the effects of a target's code outside any step, which effects it answers itself, and what a strict build refuses.

A target's own code is not hermetic. Its effects run on the *host view*
(`HostView`), one at a time in program order, every time the code runs.
Fetches, unpacks and some reads are isolated and run out of order, as
[Order](/pekit/technical-reference/evaluation/the-pool.md#order) describes. None of them is cached.

## 3.3.1 The host view

The host view maps the logical layout onto the host. GCSL's own file-system
work, such as `fs.read` or `dir.files()`, maps `/w` to the workspace's root
and `/pekit-out` to its output directory, and any other absolute path to
itself. A relative path given to `file` or `dir` resolves against the
directory of the file it is written in, never the working directory. The
`fs` built-ins, such as `fs.exists` and `fs.read`, take only absolute
paths: GCSL refuses a relative one.

A program a target runs runs under bubblewrap, with the host's own
directories bound where they are and the workspace and outputs bound at
`/w` and `/pekit-out`, so that it sees the paths GCSL code gave it. It has
the network and the environment pekit was run with. Its working directory
is the target's directory under `/w`. Because it runs under bubblewrap, a
program can't gain privileges: `sudo` fails in one. Temporary directories
GCSL makes for a target are under the cache directory, never `/tmp`.

## 3.3.2 Effects pekit answers itself

pekit answers some effects itself rather than running them as plain GCSL
would:

- **`fetch`.** From the store by the pinned digest, or downloaded on the
  host and kept only if its digest matches. See [Fetches, Unpacks, and Git
  Commits](/pekit/technical-reference/the-store/fetches-unpacks-and-git.md).
- **`fetch_git`.** From the store, or fetched into a bare clone and checked
  out.
- **`archive.unpack`.** Unpacked by pekit itself, without `tar`, and kept.
- **`keychain.string` and `keychain.file`.** From the workspace's keychain
  configuration or the environment. See [Secrets](/pekit/technical-reference/steps/secrets-in-steps.md).
- **A read of an output.** `dir.files()` and `file.read()` of a path inside
  a build's output are answered from the store's tree of it until a target's
  own effect has run a program or written. See
  [Exposure](/pekit/technical-reference/outputs/exposure.md).

`fetch`, `fetch_git`, and the keychain are refused inside a step, each with
an error saying to resolve it in a parameter default or in the caller and
pass the result in.

## 3.3.3 What a strict build refuses

Under `--strict`, a target's own effect is refused unless it is one of
these:

- `fetch`, `fetch_git`, `archive.unpack`, `keychain.string`, or
  `keychain.file`
- A read, `fs.exists`, `fs.read`, `file`, `file.read`, `dir`, or
  `dir.files`, of a path at or under `/w` or `/pekit-out`

pekit judges a path once each `.` is left out and each `..` has taken the
component before it, so `/w/../etc` is `/etc`. It follows no symbolic
link, so a link in the workspace to a directory outside it, such as a
package repository, can be read. A read elsewhere fails with
"a strict build reads only the workspace and the build's outputs, and this
reads `PATH`", `PATH` as judged. A relative path is left to GCSL, which
refuses it. Any other effect fails with "a
strict build refuses a target's own `NAME`: a release's targets run
programs and write files only in steps". Each failure is reported where the
target's code made the effect.

The host's toolchains, such as `system_c`, call `which` to find the host's
tools, which a strict build refuses, so they fail a strict build where
their definition runs.

## 3.3.4 Dynamic dependencies

A target's unit that stops for a step's result has a *dynamic dependency*:
what it builds after depends on what the step made. The step runs, its
result is given, and the unit resumes.

Under `--no-dynamic`, a target whose unit stops for a step's result fails
there instead. Only a stop for a step counts. A stop for an effect of the
target's own, such as a fetch, is part of evaluating it.

---

# 3.4 The Machine's Budget

_Pekit / Pekit TRM / Evaluation_

> The lock files every pekit process on a machine shares to bound the programs and memory of all its builds, how a program waits for them, how its memory is estimated and measured, and the jobserver each program is offered.

Every pekit process on a machine shares one budget (`Budget`): at most
`jobs` programs at once, expected to use at most `memory` bytes at once.
Nothing runs to keep it. It's a directory of lock files, each held with
`flock` by whatever uses it, so a process that ends, however it ends, lets
go of everything it held.

## 3.4.1 Its limits

Each limit is read, in order of precedence, from:

1. the environment: `PEKIT_MACHINE_JOBS`, `PEKIT_MACHINE_MEMORY`, and
   `PEKIT_MACHINE_UNSEEN`;
2. `machine.conf` in the configuration directory, `$XDG_CONFIG_HOME/pekit`
   or `~/.config/pekit`, a line `KEY = VALUE` for each of `jobs`, `memory`,
   and `unseen`, with `#` starting a comment;
3. the machine: a job for each processor, half of `MemTotal` in
   `/proc/meminfo`, and 1 GiB.

`jobs` is a count above 0. `memory` and `unseen` are sizes, written as
`PEKIT_CACHE_LIMIT`'s are, above 0. `machine.conf` can also set
`cache_limit`, `evaluating` and `keep_free`, which are not limits of the
budget but are read the same way, after `PEKIT_CACHE_LIMIT`,
`PEKIT_EVALUATING` and `PEKIT_KEEP_FREE`.
Anything else, or a line of `machine.conf` that sets nothing it knows,
fails every command that builds.

## 3.4.2 Its files

The budget's directory is `pekit/budget` under `$XDG_RUNTIME_DIR`, which the
system empties when the user's last session ends, or `budget` in the cache
directory where that's unset or can't be used. It holds:

| File | Held by |
|---|---|
| `job-<n>`, for `n` below `jobs` | A running program, exclusively, or a job a program's jobserver lent. |
| `memory-<n>`, for `n` below `memory` / 256 MiB | A running program, exclusively, a file for each 256 MiB it's expected to use. |
| `waiting` | Every process with a program waiting for the budget, shared. |
| `gate` | A program that has waited 2 seconds, exclusively, until it starts; every other process, shared, while it tries to start one. |
| `fifo-<pid>-<n>` | A program's jobserver, made by its meter and removed when it ends. |

Each process uses the files numbered below its own limits. Processes that
read different limits share the larger.

## 3.4.3 Waiting for the budget

A program of a step takes its job and its memory on the pool's thread,
before it runs (`Budget::admit`). These programs wait:

- `exec`, `exec_live`, and `exec_secret`;
- a sandbox's `exec`;

unless `interactive`. A program found in the store doesn't run, and takes
nothing. A target's own effects, which run one at a time, don't wait.

A process's programs wait in the order they asked, and only the first of
them tries the files. It tries to hold, without waiting, a `job-<n>` file
and as many `memory-<n>` files as it needs, at least one and at most all of
them. It holds all of them or none, so no two programs wait for each
other. While it can't, it holds `waiting`, and tries again, at first after
2 ms and then less often, up to every 50 ms, or as soon as a program of its
process ends.

Once it has waited 2 seconds, it holds `gate` exclusively, waiting for it.
No program of another process starts while it does, since each holds
`gate` shared while it tries, so the program starts as soon as running
programs end, however many smaller programs would otherwise start first.

A budget whose files can't be opened lets the program run at once: the
budget paces programs, and never fails one.

The time a program waits is added to its step's deadline and left out of
its cost, as the time its staging took is. Programs wait on the pool's
threads for effects, so they hold up none of the engine's own work. See
[The Pool](/pekit/technical-reference/evaluation/the-pool.md).

## 3.4.4 Estimates

A program of a step is expected to use a quarter more than the largest of
its kept peaks, each in bytes (`Budget::estimate`):

1. its own, kept under the step's name `<function>/<identity>` and the
   program's file name;
2. otherwise its kind's, kept under the step's function and the program's
   file name, by every step of that function;
3. otherwise `unseen`, or half as much again for a program named `rustc`
   (`Budget::unseen`).

Peaks are kept in the state directory under `peaks/`: a file for each,
named by a SHA-256 of the names it's kept under, the kind's prefixed
`kind-`, holding the last four peaks, oldest first, a decimal number of
bytes on each line. Each file is replaced whole. Peaks key nothing.

## 3.4.5 Metering

Each program of a step runs under `pekit __meter`, outside its sandbox,
which runs the program's bubblewrap and waits for it. The meter:

- dies with the pool's thread that started it, by `PR_SET_PDEATHSIG`, and
  exits at once if that has already ended;
- is a *subreaper*, so that the process bubblewrap starts as the sandbox's
  `init`, which bubblewrap's own process doesn't wait for, is reparented to
  it and waited for. Its peak, the largest of the program's processes',
  then reaches the meter;
- waits for what the program left, its `init`, for 5 seconds at most;
- keeps `ru_maxrss` of `getrusage(RUSAGE_CHILDREN)` as the program's peak,
  for the step and its kind;
- exits with the program's status, or 128 and the signal's number for a
  program a signal killed, which `exec` reports alike.

The meter is added after the program is keyed. Neither it nor anything it
adds reaches a receipt or a record.

## 3.4.6 Jobservers

Each metered program is offered a GNU make jobserver. The meter makes a
FIFO, `fifo-<pid>-<n>` in the budget's directory, and bubblewrap binds it
at `/dev/pekit-jobserver`. `MAKEFLAGS` and `CARGO_MAKEFLAGS` are set to
`--jobserver-auth=fifo:/dev/pekit-jobserver`, each unless the program's
`exec` sets it. Neither has `-j`. GNU make reads the FIFO form from 4.4;
an older make doesn't, and by its source stops with "invalid
--jobserver-auth string" (not tested).

The program holds one job, which the protocol leaves implicit. Every 10 ms,
the meter:

- reads back every token the FIFO holds: tokens unread, or given back;
- writes one back, unless a program of any process waits for the budget,
  since `waiting` is held, or a token has sat unread for 250 ms; and gives
  back to the budget the job of every other token it read;
- if the FIFO held nothing, so every token it lent is the program's, lends
  one more, taking a free job without waiting, if one is free, no program
  waits, `gate` isn't held, it has lent fewer than `jobs` less one, and it
  isn't backing off.

A token that sat unread for 250 ms makes the meter back off before it lends
again: 250 ms, doubled each time up to 4 seconds, and reset once a token is
read. So a program that never reads the jobserver holds a second job for
at most 250 ms at a time, less and less often. When the program ends, the
meter gives back every job it lent.

---

# 4.1 Identities

_Pekit / Pekit TRM / Steps_

> How pekit names a step's output directory by a digest of what the step is, apart from its inputs' contents, and why.

A step's *identity* names its output directory,
`/pekit-out/.steps/<function>/<identity>`, as 16 hexadecimal digits: the
first 64 bits of a SHA-256 (`receipt::identity`). Every file a step makes is
under that directory, so the identity is part of every path its outputs
have, and of every key those paths reach.

## 4.1.1 What it covers

The identity is a digest of what the step is, encoded after the tag
`pekit-identity-2`:

- **Its callee.** The name of its code's file, its function's number in that
  code, and the function's captures.
- **Its arguments,** as plain data. Every file and directory counts by its
  path alone, not its contents. The result of a step not yet given counts
  by that step's own name, so that a step is named before the steps it waits
  for have run.
- **The platform it runs on.**

The contexts it was made in don't name it, so binding a context moves only
the steps that read it, as their receipts say.

## 4.1.2 Steps that only their contexts tell apart

GCSL makes one call made in two contexts two steps, such as one compile
made through `with` for two platforms. Both have one identity and share one
name. Whether they are one step depends on whether the step reads a context
they were made in differently, and only running it says which contexts it
reads. So pekit settles it as they start:

1. The first of them to start holds the name, and is found in the store or
   runs, as any step is.
2. Each of the others waits until the first is done. Then it is keyed by the
   contexts the first was keyed by: those its run read, or those the record
   it was found by was keyed by.
3. If its receipt is the same as the first step's, it is the same step. It
   takes the first step's result, and is neither run nor looked up.
4. If its receipt differs, it is another step. It reads a context they were
   made in with different values, or it was given the result of a step that
   does. It is named `<identity>-<receipt>`, with the first 16 hexadecimal
   digits of that receipt, and is found or runs there. Others with that
   receipt share that name in the same way.

A step that doesn't read the contexts the steps differ in therefore has one
name and runs once, in whatever order evaluation meets the steps. Steps that
really differ never share a name or a result. Which of them keeps the plain
name still depends on which starts first, so reordering a build can rename
those steps, and the steps after them.

If the first step fails, the next of the others to start holds the name
instead, since what the failed step read is not known.

## 4.1.3 The tag's version

The tag's version changes when what every step's sandbox holds, beyond
the step's inputs, changes in a way that can change what a step gives.
A new version gives every step a new identity, so no step recorded under
the old sandbox is found in the store, and every step runs again once.

| Version | What changed |
|---|---|
| 1 | The first. |
| 2 | Every step runs as user and group 1000, `pekit`, with a fixed `/etc/passwd` and `/etc/group`, and `LOGNAME` and `USER` set, as [The Step Sandbox](/pekit/technical-reference/sandboxes/the-step-sandbox.md) describes. Before, a step ran as whoever ran pekit, with no user database. |

## 4.1.4 Why not its contents

An identity doesn't hold its inputs' contents, so editing a source keeps
every step at the same paths: the step runs again in the same directory,
and gives files at the same paths. A step after it that is given a rebuilt
file with the same bytes at the same path has the same receipt, and is
found in the store. This is early cutoff. Naming a step by its contents
would move every later step whenever an earlier one ran again, even to the
same bytes.

An identity doesn't hold the target that reached the step. Two targets that
make the same call make one step, and a step's paths don't change with what
else is built, or in what order. A target's view of its steps, under
`pekit-out/<target>/<function>[.N]`, is a link into the step's directory,
made after the build.

## 4.1.5 Collisions

Two different steps with the same function and identity would share a
directory. An identity is 64 bits for each function's steps, which makes
that chance negligible for any number of steps a store holds.

---

# 4.2 Receipts

_Pekit / Pekit TRM / Steps_

> What a step's receipt covers, how contexts key a step in two levels through a manifest, how a step is looked up, and when a context's files are staged.

A step's *receipt* is the SHA-256 that its record is kept under: a digest
of everything that can change what the step gives (`receipt::receipt`). A
step whose receipt the store has a record for is not run.

## 4.2.1 What it covers

The receipt is a canonical encoding, after the tag `pekit-receipt-3`, of:

- **Its code.** The path and contents' digest of every source file that
  makes the step's code: the file of its function and of every function it
  can reach, library and standard files included, in order. Any change to
  one of these files, a comment included, changes the receipt.
- **Its arguments and its function's captures,** as plain data. A record's
  fields are sorted by name, a float is encoded by its bits, every NaN as
  one, and `-0.0` as `0.0`.
- **Each file it is given,** by its path and the digest of its contents, and
  each directory by its path and its tree's digest. The path stays in the
  key because it reaches command lines and names outputs. An input is
  keyed by its contents, never by the step that made it.
- **Each secret,** by its identity and whether it is revealed, never by its
  value.
- **Each function value** among its captures or contexts, such as a
  registered toolchain's definition, by the name and contents of its code's
  file, its number in that code, and its captures.
- **The contexts it read,** each by its key, with its value or a tag
  saying that it was unbound.
- **Its own output directory,** which names the files it makes.
- **The platform it runs on.** Outside a strict build, this is the execution
  platform with the host snapshot's fingerprint. In a strict build, it is
  the execution platform marked as having no host tools.

`#[retry]`, `#[timeout]`, `#[device]`, and `#[scratch]` are not in it: they
change how a step runs, never what it gives.

## 4.2.2 Two levels: base and manifest

A step can't know which contexts it reads before it runs. So a step is
keyed in two levels:

1. Its *base* is the receipt without any context.
2. The store keeps, under a key made from the base (`pekit-manifest-1`), a
   *manifest*: every set of context keys a run of a step with that base has
   read.

To look a step up, pekit takes each set in its manifest in turn, and makes
the full receipt of the base and, for each key of the set, the value the
step's context gives it, or that it is unbound. A record under that receipt
is the step's result.

A step that runs is recorded under the receipt of the set its run read,
and that set is merged into the manifest. Merging never removes a set.

This is sound because a run is a function of what it reads. Two runs with
one base, given the same values for the contexts the first read, read the
same contexts in the same order, and give the same result.

## 4.2.3 When a context's files are staged

A step's sandbox holds the files of its arguments and captures from its
start. A context's value can name files too, such as the registered
toolchains' trees. Those are staged into the sandbox only once the step
reads the context, at the read itself
(`StepSandbox::context_read`). So a program a step runs can't reach a file
of a context its receipt doesn't hold.

The files of every context a step can read are hashed when the step
starts, so that they can be staged on a read.

---

# 4.3 Records

_Pekit / Pekit TRM / Steps_

> What pekit keeps of a step that succeeds, what a record holds, which results are never recorded, and how a found record gives a step's result without running it.

A step that succeeds is recorded: its receipt, its result, and how long it
ran. A step found in the store gives its record's result without running.

## 4.3.1 Keeping a result's files

When a step's unit ends with a value, every file and directory the value
names is put into the store (`record::keep_files`), whether or not the
result can be recorded, since later steps are given them. A file becomes a
blob, and a directory a tree. A directory that holds a mount point is put
into the store with the mounted tree's digest, unread.

A step fails unless every file and directory its result names is one of
these:

- A file or directory in the step's own output directory
- A file or directory it was given, or a context's file that a read staged
- A file of `/pekit-out/.fetch/` or `/pekit-out/.unpack/`, which are named
  by their contents

Any other path fails the step: another step's file at that path could
otherwise stand for it.

## 4.3.2 What a record holds

A record (`StepRecord`) is JSON in the store's `records/`, under its
receipt's digest. It holds:

- **`receipt`,** the digest it is kept under.
- **`result`,** the step's value as plain data (`Stored`): integers,
  floats, booleans, strings, durations, sizes, records with their fields in
  order, lists, enum variants, sandboxes, and every file and directory by
  its path and what the store keeps of it: a file's digest, size, and
  whether it is executable, and a directory's tree. A string longer than
  64 KiB (`INLINE_TEXT`), such as a program's whole output, is kept as a
  blob, and the record names it by digest and length (`Stored::Text`).
  This matters because a record is read whole, by every eviction too, and
  JSON writes some bytes as six. A record whose string blob was evicted
  isn't live.
- **`duration_ms`,** how long the step ran, from the end of its staging to
  the end of its last attempt. Eviction weighs a record's contents by it.

Records are never evicted. A record whose contents were evicted is not
*live*: a step whose record isn't live runs again, as if it weren't found,
and records again.

## 4.3.3 What is never recorded

These results are not recorded, and the step runs on every build:

- A result that holds a secret (`StoreError::Secret`). A secret is never
  stored. Its files are still kept, so later steps are given them.
- A result that holds a function value, which is not data.

A step that fails isn't recorded either.

## 4.3.4 Giving a found record's result

A found record's result is turned back into a GCSL value, its files and
directories at their recorded paths. The steps after it are staged with
those files from the store. The record's files are exposed under
`pekit-out/` only once something that is not hermetic might read them, as
[Exposure](/pekit/technical-reference/outputs/exposure.md) describes.

Before a record is used, pekit checks that the store has everything it
names: every blob, and every tree with everything it names at any depth.
A tree found complete is marked in the store's `complete/`, so that it is
walked once rather than in every build. An eviction removes every mark
before it removes a blob.

---

# 4.4 Retries and Timeouts

_Pekit / Pekit TRM / Steps_

> How pekit runs a step's attempts under #[retry], what makes a step flaky, how #[timeout] spans the attempts, and what a step's failure does to the steps and targets after it.

GCSL applies `#[retry]` and `#[timeout]` to an ordinary call itself. For a
step's own call, pekit applies them instead, since the step's body runs as
a unit of its own on pekit's schedule.

## 4.4.1 Attempts

A step's first attempt is attempt 0. When an attempt fails, by its code's
own failure, and the step's `#[retry(n)]` allows another, the step runs
again from its start, in a new sandbox staged afresh as
`step-<token>-<node>-<attempt>` under the cache's scratch directory. Nothing
of the failed attempt carries over: not its files, not its `/tmp`, and
not its programs' writes.

A program that can't start because its file is open for writing isn't an
attempt. GCSL starts it again for about a second, whatever the step's
`#[retry]`. A newly written file can stay open for moments in a process forked
meanwhile, and a program that didn't start did nothing.

A step that passes on an attempt after the first is *flaky*. It is
recorded as any step that succeeds, and the build warns:

```text
warning: step `NAME` passed only when it was run again: it is flaky
```

`pekit test` reports a test whose step was flaky as `flaky`.

## 4.4.2 Timeouts

A step's `#[timeout(limit)]` is a deadline for all its attempts together,
from the start of its first attempt. The time the pool spends staging each
attempt's sandbox doesn't count: the deadline moves on by that time. When
the deadline passes, the running program is stopped, and the step fails
with "the step timed out", at the program it was running. A step that
fails once its deadline has passed isn't retried, whatever its `#[retry]`
allows.

## 4.4.3 What a failure reaches

A step that fails after its attempts fails every step that needs its
result, without running them, with the same failure. A target whose value
needs the failed step fails with that failure, reported where the target's
code, or the library code it called, made the step. Other targets of the
same build carry on, and the command exits with status 1 once every target
has finished or failed.

---

# 4.5 Secrets in Steps

_Pekit / Pekit TRM / Steps_

> How pekit's keychain finds a secret, identifies it without revealing it, gives a secret file to one step, and keeps secrets out of the store, records, and events.

GCSL's `keychain` module asks the host for a secret by name. pekit
answers (`Keychain::answer`) from the workspace's keychain configuration,
`pekitconf.keychain`, for the build's profile, or else from the
environment: `$GCSL_SECRET_FILE_<NAME>` for a file, or `$GCSL_SECRET_<NAME>`
for a string. `NAME` is the secret's name in capitals, with `_` for each
character that is not a letter or a digit. A secret found nowhere fails the
call. A string secret is the file's contents without one final newline.

## 4.5.1 Identity

Each secret's identity is what receipts record of it:
`<profile>/<name>#<hash>`, where the hash is the first 32 hexadecimal digits
of HMAC-SHA-256 of the secret's contents under the machine's key.

The machine's key is 32 random bytes, in `keychain-key` in the state
directory, readable only by its owner. pekit makes it the first time a
build needs it, writing it beside its place and linking it into place, so
that two builds starting at once use one key.

Consequences of the identity:

- A changed secret has a new identity, and reruns the steps given it.
- Nothing about the contents can be learned from the identity without the
  key, since a guess can't be tested.
- Two machines give one secret two identities, so a step given a secret is
  found in the store only on the machine that recorded it.
- A new key gives every secret a new identity, and nothing made with the old
  identities is reused.

## 4.5.2 A secret file in a step

A step given a secret file has the file bound read-only at
`/pekit-secrets/<profile>/<name>`, from where the keychain found it. The
path is the same on every machine. No other step has the file, and it is
never staged by copying or linking, and never put into the store.

A step can't ask the keychain itself: a `keychain` call in a step's code
fails, since what a step reads has to be among its inputs. A library
resolves a secret in a parameter default instead.

## 4.5.3 What never holds a secret

- **The store.** A result that holds a secret is never recorded. A program
  of a step that has been given a secret is never recorded either, from the
  moment it is given one, so a program's output that reveals a secret
  isn't kept.
- **Events and the trace.** An event names a step by its name and an effect
  by its built-in and program, never the program's other arguments. A
  program named by a secret is shown as `<secret>`.
- **Errors.** A program given a secret through `exec_secret` that fails
  reports neither its status nor its output.

---

# 5.1 Staging

_Pekit / Pekit TRM / Sandboxes_

> How pekit makes a step's staging directory from the store, which files it links, which trees it mounts, and how it removes the directory when the step is done.

A step's sandbox is made of a *staging directory* on the host, under the
cache directory's `scratch/`, named `step-<token>-<node>-<attempt>`. The
pool stages it before the step's body runs (`Work::Stage`).

## 5.1.1 What it holds

The staging directory has four parts, which the sandbox binds at their
logical paths:

| On the host | In the sandbox | Holds |
|---|---|---|
| `w/` | `/w` | The workspace files and directories the step is given. |
| `out/` | `/pekit-out` | The outputs of other steps, fetches, and unpacks the step is given. |
| `out/.steps/<function>/<identity>/` | The same path, writable | The step's own output directory, empty at the start. |
| `tmp/` | `/tmp` | An empty directory of the step's own. |

Every input is staged from the store, never from the workspace, so the
step sees exactly the contents its receipt holds, even if a workspace file
changes while the build runs.

## 5.1.2 Files and trees

Each input is staged by what the store keeps of it (`StepSandbox::stage`):

- **A file** is a hard link to its blob, `cas/<xx>/<digest>`, or to
  `<digest>.x` if it is executable. A blob is read-only, so the step can't
  change it.
- **A directory of fewer than 64 files** is made file by file, each a link
  to its blob.
- **A directory of 64 files or more** is mounted: the store's materialized
  tree of it is bound read-only on an empty directory at the input's path.
  See [Materialized Trees](/pekit/technical-reference/the-store/materialized-trees.md).

A tree that holds one content at several paths gives the first a link to
the blob and each other a link to a numbered copy, `<digest>.r1` and on,
which the store keeps. So no two files of a staged tree are one file, and
placing the tree copies nothing that was copied before.

Where linking fails, as across file systems, a file is copied.

## 5.1.3 Removing it

When a step is done, its staging directory is renamed, which takes no
time, and a thread of its own removes it while the build goes on
(`sweep::Sweeper`). The build waits for that thread before it ends, and a
directory that can't be removed fails the build at its end.

A process killed before it removed a staging directory leaves it behind.
The next build that holds the store's lock alone removes it, as
[Concurrency and Sweeping](/pekit/technical-reference/maintenance/concurrency-and-sweeping.md) describes.

---

# 5.2 The Step Sandbox

_Pekit / Pekit TRM / Sandboxes_

> The bubblewrap command pekit runs a step's programs under, every mount, namespace, and variable it sets, and what a program sees.

Each program a step runs is wrapped in its own bubblewrap command
(`StepSandbox::command`). Every program of a step sees the same sandbox,
since each binds the same staging directory.

## 5.2.1 Isolation

The command unshares every namespace bubblewrap can: user, IPC, PID,
network, UTS, and cgroup, with a new session, and dies with its parent. So
a program has no network, its own processes and users, and stops if pekit
stops. Its root is an empty `tmpfs`.

## 5.2.2 The step's user

A program runs as user 1000 and group 1000, named `pekit`, whoever runs
pekit (`sandbox::STEP_UID`). Without this, it would run as the number of
the user who ran pekit, which differs between machines. Files the step is
given, and files it makes, belong to that user. The host's other files,
such as those under `/usr`, belong to `nobody`.

Every step has the same user database (`sandbox::PASSWD` and
`sandbox::GROUP`), as Nix gives its builds. `/etc/passwd` holds:

```text
root:x:0:0:root:/root:/bin/sh
pekit:x:1000:1000:pekit:/nonexistent:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
```

`/etc/group` holds:

```text
root:x:0:
pekit:x:1000:
nogroup:x:65534:
```

Programs that look up users by name or number find them there, and tests
that need a file every system has, such as `/etc/passwd`, find one. The
step's user has no home directory, so a program that writes to one fails
rather than leaving files nothing keys.

A program can be in more groups than `pekit`. A user namespace that
pekit makes without privileges keeps the groups of the user who ran pekit,
and shows each of them as 65534, `nogroup`. So the number of groups `id -G`
lists depends on the machine.

## 5.2.3 Mounts

The sandbox binds, in this order:

1. The host snapshot's directories, read-only at their own paths, unless
   the build is strict. Each directory of the snapshot that is hidden, such
   as `/usr/local`, is an empty `tmpfs`.
2. The user database, read-only at `/etc/passwd` and `/etc/group`, in
   every build, strict or not.
3. The staging directory's `w/` at `/w`, and `out/` at `/pekit-out`,
   read-only.
4. The step's own output directory at its path, writable.
5. Each mounted tree and each sealed composition, read-only at its path.
6. Each secret file given, read-only at `/pekit-secrets/<profile>/<name>`.
7. The staging directory's `tmp/` at `/tmp`, writable.
8. The step's scratch directory at `/pekit-scratch`, writable, for a step
   marked `#[scratch]`.
9. `/proc`, and a minimal `/dev`: `null`, `zero`, `full`, `random`,
   `urandom`, `tty`, `ptmx`, `pts`, `shm`, and the standard streams.
10. `/dev/kvm`, for a step marked `#[device("kvm")]` on a machine that has
    it.

A path outside these isn't there. A GCSL file function given such a path
fails with "it is outside the step".

## 5.2.4 Working directory and environment

A program starts in the step's own output directory, unless its `exec`
gives `cwd`. Its environment is cleared, then set to the variables its
`exec` gives. `LOGNAME` and `USER` are `pekit` unless the `exec` gives
them, since some programs, such as DejaGnu's `runtest`, need them. If it
gives no `PATH`, and the build isn't strict, `PATH` is
`/usr/bin:/bin:/usr/sbin:/sbin`. In a strict build a program given no
`PATH` has none.

## 5.2.5 What a program doesn't inherit from pekit

How pekit was started isn't part of a step's key, so pekit keeps it from
reaching a step's programs:

- **Standard input.** A program's standard input is `/dev/null`, even when
  its `exec` is interactive. It can't read the terminal pekit runs in, or
  whatever else pekit's standard input is.
- **Signal dispositions.** A program starts with every signal's default
  disposition. A shell's `&` starts pekit with `SIGINT` and `SIGQUIT`
  ignored, and a signal ignored stays ignored across `exec`, so without
  this every program of the step would ignore them, which test suites of
  signals, such as bash's, notice. pekit catches each signal it inherited
  as ignored with a handler that does nothing (`default_dispositions`):
  `exec` resets a caught signal to its default, and pekit itself still
  ignores the signal. `SIGILL`, `SIGFPE` and `SIGSEGV` can't be caught
  this way, so a pekit started with one of them ignored passes it on.
- **The file-creation mask.** A program's `umask` is `022`
  (`sandbox::UMASK`), whatever pekit was started with. The modes a step
  gives its files are part of what it made, as an archive it makes
  records them. pekit can't set a child's mask alone, so it sets its own
  before a build runs anything (`fixed_umask`). GCSL's file work for a
  step, which runs in pekit's process, gets the same mask, and so does
  every other program pekit starts and every file it makes, a step's or
  not.

A program does inherit pekit's resource limits, such as `ulimit -n`, the
number of files it can open. These can differ between a terminal and
another launcher: an agent's shell can raise the open-file limit from
1,024 to 524,288. They aren't part of a step's key.

## 5.2.6 GCSL's own file work

GCSL's file functions inside a step, such as `fs.write`, run in pekit's
process, not in the sandbox, against the staging directory. pekit maps each
logical path to the staging directory itself, and refuses two things:

- **A path outside what the step has.** It fails with "it is outside the
  step".
- **A write to an input or a sealed composition.** `fs.write`, `fs.mkdir`,
  `fs.copy`'s destination, and `fs.set_executable` fail on a path in an
  input of the step or in a sealed composition. The error says that the
  path is an input of the step, or a finished composition.

Temporary directories GCSL makes for a step, a `Dir.sandbox`'s copy among
them, are `.pekit-tmp-<n>` in the step's own output directory, so that a
file given from one has a path no other step's file has.

## 5.2.7 Sandboxes a step makes

A `Dir.sandbox`'s commands run in a bubblewrap of their own beside the
step's, never inside it, since a host such as Ubuntu forbids one
bubblewrap inside another. That bubblewrap has the sandbox's directory as
its root, and the same isolation, user, `LOGNAME` and `USER`, devices,
and scratch directory as the step's own. It has the step's user database
unless its root has its own `/etc/passwd` or `/etc/group`.
[Compositions and Overlays](/pekit/technical-reference/sandboxes/compositions-and-overlays.md) describes
how its root is made.

---

# 5.3 Compositions and Overlays

_Pekit / Pekit TRM / Sandboxes_

> How pekit makes a step's composed directories without copying, seals them, and runs a sandbox of one as an overlay, with nested overlays for the trees it mounts.

A step composes directories with `fs.compose` and `fs.compose_dir`, and
runs programs in a `Dir.sandbox` of one, as a toolchain library does to
run a pinned compiler on its own runtime. Plain GCSL copies for both. A
step's sandbox answers both itself (`Layers`), without copying.

## 5.3.1 Compositions

A composition places each entry under its key, as GCSL describes. pekit
places each kind of entry this way (`Layers::place_tree` and
`Layers::place_file`):

- **A directory of 64 files or more** that is a store tree is mounted: an
  empty directory of its own, the mount point, is made at its place, and
  the materialized tree is bound read-only there for every program. GCSL
  finds what it holds there too.
- **A file nothing can change,** one of a staged input, a materialized
  tree, or another sealed composition, is hard-linked.
- **Any other file** is copied.

A content a composition places twice, as a toolchain's runtime is placed
both alone and within the toolchain, is a link to the blob once and to a
numbered copy each other time. A tree mounted in a composition that holds
contents the composition already has is an *instance* of the tree whose
files of those contents link later copies. So no two files of a
composition are one, and nothing is copied anew.

Directories GCSL doesn't place whole, because an entry is placed inside
them, are made entry by entry.

## 5.3.2 Sealing

Once a composition is made it is *sealed*: its files are made read-only on
the host, and it is bound read-only to every program of the step. No
program can change it, and so none can change a blob through a link.
GCSL's file functions refuse to write into it. Its directories keep their
modes, since an overlay's merged directory shows its lower directory's
mode, and a read-only one would refuse every write.

## 5.3.3 Sandboxes of a composition

A `Dir.sandbox` of a directory that nothing can change, a sealed
composition or a staged input, is an overlay (`Layers::make_sandbox`):

- The directory is the *lower* layer, which no program changes.
- A directory of the step's own outputs is the *upper* layer, where
  every write goes.
- Each tree mounted in the directory is an overlay of its own, *nested* on
  its mount point, with an upper layer of its own, `.pekit-upper-<n>` in
  the step's output directory.

- The step's user database, the directory holding its `etc/passwd` and
  `etc/group`, is a layer below the directory. So a composition that has
  its own `/etc/passwd` or `/etc/group` shows its own, and one with none
  shows the step's.

So a program in the sandbox can write anywhere, and nothing it writes
reaches the composition. It can't move or remove a nested overlay's mount
point, which fails with `EBUSY`, "Device or resource busy". GCSL's own
reads of the sandbox don't see the user database layer, only programs do.

A `Dir.sandbox` of any other directory is copied, as plain GCSL does.
Before a program runs in a copy, pekit writes the step's `etc/passwd` and
`etc/group` into it, each unless the copy already has it. If the copy's
`etc` is a symbolic link or a file, pekit leaves it alone.
[The Step Sandbox](/pekit/technical-reference/sandboxes/the-step-sandbox.md) lists their contents.

## 5.3.4 Reading an overlay

GCSL reads a path of an overlay sandbox as the overlay shows it
(`Layers::resolve`):

- What the upper layer has, unless it is a *whiteout*, a character device
  of number 0, which hides what the lower has.
- Otherwise what the lower has, unless a directory of the upper layer on
  the way is *opaque*, marked `user.overlay.opaque`, which hides all the
  lower has below it.
- A directory both layers have is merged into a new tree of hard links, a
  snapshot of it as it is at the time.
- A path in a nested overlay is read from that overlay's layers.

GCSL writes to an overlay sandbox's path in the upper layer of the overlay
the path is in (`Layers::write_target`). A step's result can name a file or
directory of an overlay sandbox; it is kept from the merged view.

---

# 5.4 The Host Snapshot

_Pekit / Pekit TRM / Sandboxes_

> The host directories steps see outside a strict build, how pekit fingerprints them, and what happens on a host it can't identify.

Outside a strict build, every step sees the host's system directories, so
that it can run the host's programs: the *host snapshot* (`HostSnapshot`).
A strict build gives steps none of it.

## 5.4.1 What it binds

Each of these that exists is bound read-only at its own path:

- `/usr`, `/bin`, `/lib`, `/lib64`, and `/sbin`.
- `/etc/alternatives`, where Debian's `cc` and similar names lead.
- `/etc/ld.so.cache`, the dynamic linker's cache.

`/usr/local` is hidden by an empty `tmpfs`, since no package manager
manages it, and so its changes would change no fingerprint. Nothing else
of `/etc` is there.

## 5.4.2 The fingerprint

The snapshot is an input of every step, and too large to hash on every
build. It is identified instead by a *fingerprint*, a digest of these:

- `/etc/os-release`
- The package manager's database, which changes whenever a package is
  installed, upgraded, or removed
- What the bound parts of `/etc` say, which an administrator can change
  without a package: each alternative's target, and the linker's cache

The databases pekit knows are dpkg's `/var/lib/dpkg/status`, rpm's
`rpmdb.sqlite` in `/var/lib/rpm/` or `/usr/lib/sysimage/rpm/`, apk's
`/lib/apk/db/installed`, and pacman's `/var/lib/pacman/local/`, whose
entries name each package and version.

The fingerprint is part of the platform every receipt names. A step
recorded before a compiler was upgraded is not found after it.

## 5.4.3 A host it can't identify

On a host with none of those databases, the snapshot has no fingerprint.
Its steps are recorded, but never found in the store, since nothing would
say when its tools changed. Every command that builds warns:

```text
warning: the host's packages cannot be identified, so no step is reused from the store
```

A strict build doesn't use the snapshot, so it reuses steps on any host.

## 5.4.4 The note after a build

After a build that ran at least one step and wasn't strict, pekit notes
that steps ran with the host's own tools, so the build isn't hermetic.
Every step of such a build could see the snapshot, whether or not it used
it.

---

# 5.5 Scratch Directories

_Pekit / Pekit TRM / Sandboxes_

> Where pekit keeps the directory of a step marked #[scratch], whose it is, how runs share it, and when a run gets an empty one.

A step marked `#[scratch]` has a directory at `/pekit-scratch`, kept
between its runs, for a tool that does less on a second run: a build
system's object directory, Cargo's target directory, or a compiler cache
(`Scratch`).

## 5.5.1 Whose it is

A scratch directory belongs to a step's output directory under
`/pekit-out`, which its identity names, and to the platform it builds for.
So it is kept when the contents of the step's inputs change, and a
different call of the same function, or the same call for another
platform, has its own.

It keys nothing. A step has to give exactly what it would give with an
empty directory, and pekit relies on that.

## 5.5.2 Where it is

Scratch directories are in the store's directory, under `scratch/`, each
named by a digest of whose it is. Beside each is a lock file, which a run
holds while it uses the directory, and whose modification time is when it
was last held, and a size file, which eviction reads. While a run has the
directory, a `.dirty` file is beside it too, removed as the run lets the
directory go.

## 5.5.3 When a run gets an empty one

A run gets an empty directory of its own, removed when it ends, when:

- The build is strict, so that what a release gives depends on nothing a
  scratch directory kept.
- Another run, of this process or another, holds the step's directory, so
  that no run waits for one.

A run is also given the step's directory empty when a `.dirty` file is
beside it: the run that last had it was killed before it let it go, and might
have left it half written, a truncated object file newer than its source
say, which a tool would take as up to date.

A program of the step found in the store leaves the directory as it is.

## 5.5.4 Removing them

An eviction removes scratch directories whole, ordered as other contents
are, as costing nothing to make again, and never one a run holds.
`pekit clean --cache` removes them with the store.

---

# 6.1 Store Layout

_Pekit / Pekit TRM / The Store_

> The directories of pekit's store, what each holds, and how pekit writes into them so that no reader sees anything in part.

The store is the directory `store/` under the cache directory, shared by
every workspace and every pekit process on the machine (`Store`). It
holds:

| Path | Holds |
|---|---|
| `cas/<xx>/` | Blobs by their SHA-256 digests, in directories named by a digest's first two hexadecimal digits, with their executable and numbered copies. |
| `records/<xx>/` | Step records, program records, manifests, and the records of fetches, unpacks, and git commits, by their keys. |
| `trees/` | Materialized trees, made once as directories of links to blobs. |
| `complete/` | A mark for each tree found to have everything it names, at any depth. |
| `seen/` | For each workspace root, the stamps of the files a build read, and the root's path. |
| `git/` | Bare clones of the repositories `fetch_git` fetched from. |
| `scratch/` | The scratch directories of steps marked `#[scratch]`, with their lock and size files. |
| `claims/` | A lock file for each step a build claimed. See [Claims](/pekit/technical-reference/the-store/claims.md). |
| `usage/` | One journal for each build of what it used, for eviction. |
| `eviction.json` | Each record's eviction priority, and what eviction removed. |
| `orphaned` | What workspaces' output directories held of evicted steps, when an eviction last looked. |
| `size`, `size.lock` | The blobs' total size as last measured and added to, and its lock. |
| `tmp/` | What is being written. |
| `lock` | The store's lock. |

## 6.1.1 Writing

A blob, a tree, or a record is written in full into `tmp/`, under a name
holding the writing process's token, then renamed into place. A rename is
atomic, so a reader, or another process, sees a file whole or not at all.
A process that loses a race to write the same blob finds it there, and
drops its own.

A file put into the store is hashed as it is copied into `tmp/`, flushed to
the disk unless `PEKIT_NO_SYNC` is set, and renamed to its digest's name
(`Store::put_file`). A blob is read-only. A blob under its digest's name is
therefore whole even after a power loss, and is trusted as it's found.

A file that is pekit's own to discard is *adopted* instead
(`Store::adopting`): a step's results, which its staging directory holds
until it is removed, an unpacked archive, and a checked-out commit. Such a
file with one link is hashed where it is. If the store holds its contents,
nothing is written. Otherwise the file is linked into `tmp/`, flushed, and
renamed into place, so it becomes the blob without being copied. A
workspace's files are never adopted, since someone might change them later.

A tree is put at once (`Store::put_tree_mounted`,
`Store::put_tree_seen_at`). Each new blob it holds, and each of its trees'
encodings, is written whole into `tmp/` as the tree is walked. Then 16
threads flush them all together, so that one commit of the file system's
journal serves many files. Only then is each renamed into place, in the
order it was written, so a tree's entries come before the tree. Flushing
files one by one cost a journal commit each: storing 20,000 small files
took 43 seconds, and takes 7 at once. A put that fails removes everything
it wrote.

## 6.1.2 The lock

Every process holds `lock` shared, with `flock`, while it has the store
open, and an eviction or a sweep of stale files takes it exclusively,
without waiting. So an eviction or a sweep runs only while no other process
has the store, and a process that opens the store waits for one that is
running.

---

# 6.2 Blobs and Trees

_Pekit / Pekit TRM / The Store_

> How the store keeps files as blobs with executable and numbered copies, directories as trees, and how it avoids reading a tree it has seen before.

The store keeps a file's contents once, as a *blob*, and a directory as a
*tree* of its entries. Both are named by SHA-256 digests, so the same
contents are kept once, whichever step, fetch, or workspace they came from.

## 6.2.1 Blobs

A blob is `cas/<xx>/<digest>`, where `<xx>` is the digest's first two
hexadecimal digits. It is read-only.

Whether a file is executable is not part of its contents, but a file's
mode is shared by every hard link to it. So an executable file's blob is a
copy beside the plain one, `<digest>.x`, made the first time it is needed.

A tree or a composition that holds one content at several paths needs a
separate file for each, so that no two of its files are one. The store
keeps numbered copies for this, `<digest>.r1`, `<digest>.r2`, and on, and
`<digest>.x.r1` and on for an executable one, each made the first time it
is needed and kept (`Store::link_copy`). The first path is a link to the
blob, and each other path a link to the next copy. No record names a copy,
so an eviction removes copies first, as costing nothing to make again.

A blob is taken out of the store by a hard link (`Store::materialise_file`),
or by a copy where a link can't be made, as across file systems.

## 6.2.2 Trees

A tree is a directory's entries, sorted by name. Each entry is one of:

- A file, by its contents' digest and whether it is executable
- A directory, by its own tree's digest
- A symbolic link, by its target, kept as it is

A tree is encoded canonically as JSON and stored as a blob itself, so a
tree's digest covers its whole contents at any depth, as the directories
of the Remote Execution API's content store do. Names have to be UTF-8; a
directory holding another name can't be put into the store.

A tree found to have every blob and tree it names, at any depth, is marked
in `complete/` by its digest (`Store::holds_tree`), so that it is walked
for that once, not in every build that finds a record naming it.

## 6.2.3 Putting a tree it has seen

A tree put into the store again and again, as a step's writable directories
are around each of its programs, is mostly what it was. `Store::put_tree_seen`
keeps each file's stamp, its device, inode, size, mode, and times, from
when it was last put, and reads again only a file whose stamp differs, or
whose blob is gone.

A file changed since it was stamped has another change time, unless it
changed in the same tick of the file system's clock as it was stamped. So a
stamp taken in the tick a put begins in, or later, by the clock of the
tree's own file system, isn't trusted, as git treats its index.

For each file taken by its stamp, the store checks for its blob once the
walk is over, unless the tree is already marked complete. A gone blob is
put back from its file. When every blob is found, the tree is marked
complete, so walking the same tree again checks for none. These marks
accumulate until an eviction removes them all.

Before a walk, the tree is read ahead, one level at a time: each directory
of the level is listed, then each entry of the level is stamped, on up to
eight threads where there are enough entries. The walk then takes each
listing and stamp as it was read ahead.

A file that is a hard link to a blob this process linked, by its device and
inode, is that blob, and isn't read. A file that is read is hashed before
it is copied, and isn't copied if its blob is there. A directory known not
to change, such as a sealed composition, is walked once. A mount point's
directory takes the mounted tree's digest, unread.

---

# 6.3 Materialized Trees

_Pekit / Pekit TRM / The Store_

> How the store makes a tree once as a directory of links for steps to mount, how instances keep a composition's files apart, and when materialized trees are removed.

A step is given, and composes, whole trees, such as a toolchain of tens of
thousands of files. Linking each file into each step's staging directory
would cost as much as the step. Instead the store makes a tree's directory
once, in `trees/`, of hard links to its blobs and their copies, and every
step that needs the tree mounts that directory read-only (`store::trees`).
Nothing changes it, so every step of every build shares it.

## 6.3.1 When a tree is mounted

A tree is mounted rather than linked file by file when it holds 64 files
and symbolic links or more, at any depth (`MOUNT_FILES`). That applies to a
directory a step is given, and to one a composition places whole.

## 6.3.2 Instances

A tree is materialized as an *instance*: its digest, and for some of its
contents a *base*, the number of the copy its first file of that content
and mode links. The instance with no bases is named by the tree's digest,
`trees/<digest>`. Any other is named by the digest and the first 16
hexadecimal digits of a digest of its bases, `trees/<digest>-<bases>`.

A composition that mounts a tree whose contents it already holds
elsewhere mounts an instance whose files of those contents link copies the
composition doesn't use yet. So no two of the composition's files are one.

## 6.3.3 Making one

An instance is made in `tmp/` and renamed into `trees/` whole, so an
instance there is complete. Within a process, each instance has its own
lock, so two threads never make one twice. A process that loses a race with
another finds the other's instance there, and drops its own.

pekit learns what is in an instance from the tree, not by walking its
directory: the digest of a directory inside it, and which blob or copy each
of its files links.

## 6.3.4 Removal

An instance holds links to blobs, so an eviction removes every
materialized tree before it removes any blob (`Store::forget_materialised`).
The next step that needs a tree materializes it again.

---

# 6.4 Fetches, Unpacks, and Git Commits

_Pekit / Pekit TRM / The Store_

> How pekit answers fetch, archive.unpack, and fetch_git from the store, where each result's path comes from, and how each is kept and recorded.

pekit answers GCSL's `fetch`, `archive.unpack`, and `fetch_git` itself,
never running the programs plain GCSL runs. Each result is named by its
contents, so it has the same path in every build, and is kept in the store
so that it is fetched or unpacked once on a machine.

## 6.4.1 Fetches

`fetch(url, sha256)` is the only part of a build that touches the network
(`fetch`). With a pin:

1. A blob of the pinned digest in the store is the file, and the network is
   not used.
2. Otherwise the file is downloaded on the host, hashed as it is streamed
   into the store, and kept only if its digest is the pin. A download that
   goes 60 seconds without a byte fails.

The file's path is `/pekit-out/.fetch/<sha256>/<name>`, where `<name>` is
the URL's last part as GCSL names it. Without a pin, the file is downloaded
only to report its digest, and the call fails with that digest. A pin that
is no digest, or a file that doesn't match, fails the call too.

How long a download took is recorded, for eviction to weigh.

## 6.4.2 Unpacks

`archive.unpack(archive, strip)` is answered by pekit's own unpacker
(`unpack`), since the build environment that would give a step `tar` is
itself an archive.

- **Formats.** An archive is read as zip by its first bytes, as a wheel or
  a jar is; otherwise as gzip, xz, bzip2, or zstd, or a plain tar.
- **Paths.** Each entry's path has its first `strip` components left off,
  and an entry with no more is left out. An entry whose path would leave the
  directory, by being absolute, holding `..`, or passing through a symbolic
  link the archive made, fails the unpacking. Nothing is ever written
  outside it.
- **Kinds of entry.** A symbolic link is kept as it is, and a sandbox
  resolves it inside itself. A hard link becomes a copy. A device or a pipe
  is left out. Of each file's mode, only whether it is executable is kept.

An unpacking is keyed by the archive's digest and `strip`, and recorded as
the tree it made. Its path is `/pekit-out/.unpack/<key>`. It runs in a
directory `unpack-<token>-<n>` under the cache's scratch directory, then is
put into the store.

## 6.4.3 Git commits

`fetch_git(url, commit)` gives the files of the repository at the commit,
whose whole name pins its files as a digest does (`git_fetch`):

1. A record for the URL and commit names the tree of its files, which the
   store holds whole.
2. Otherwise the commit is fetched into a bare clone of the URL in the
   store's `git/`: the commit alone, shallowly, or, if the server won't give
   a commit by its name, with every branch and tag. It is checked out into
   `git-<token>-<n>` under the cache's scratch directory, without `.git` or
   submodules, put into the store, and recorded with how long it took.

Fetches of one repository take turns, since a build fetches several crates
of one repository at once into one clone. One that waited finds the record
the other made.

Git runs with no configuration but the repository's own: the user's and
the system's are left out, so that nothing of the machine, such as a
line-ending conversion or a filter, changes the files a commit gives.
`GIT_CONFIG_PARAMETERS` and `GIT_CONFIG_COUNT` are removed from its
environment for the same reason. The path is
`/pekit-out/.git/<key>/<name>`, where the key is a digest of the URL and
the commit.

Credentials are the exception. Git's one credential helper runs
`git credential fill` with the user's configuration environment restored:
`GIT_CONFIG_GLOBAL`, `GIT_CONFIG_NOSYSTEM`, `GIT_CONFIG_PARAMETERS` and
`GIT_CONFIG_COUNT`, each as the user had it, or unset. Credentials therefore
come from wherever the user's git gets them. The helper answers `get` only;
`store` and `erase` do nothing, so a fetch never changes the user's
credential store. `GIT_TERMINAL_PROMPT` is `0` throughout, so nothing
prompts. Credentials are part of neither the key nor any step.

## 6.4.4 In a step

A step has no network, so `fetch` and `fetch_git` in a step's code fail.
`archive.unpack` works in a step, on an archive the step was given, and is
answered and kept the same way.

---

# 6.5 Program Records

_Pekit / Pekit TRM / The Store_

> How pekit records each program a step runs by what the program can see, how it replays a program it finds, and when a step's programs are neither found nor recorded.

A step runs again whenever anything in its receipt changes, including any
byte of its code's files. Most of the programs it runs then see what they
saw before. So pekit also records each program a step runs, by what the
program can see, and replays a program it finds instead of running it
(`programs` and `engine::programs`).

## 6.5.1 Which programs

Each `exec`, `exec_live`, and `Sandbox.exec` of a step is a program. The
first time a step is given a secret, at its start or when a read binds one,
its programs are neither found nor recorded from then on. With `--rebuild`,
programs are run and recorded but not found. In `--check-reproducible`'s
second build, they are neither found nor recorded.

## 6.5.2 A program's key

A program's key covers:

- **The platform,** as the step's receipt names it, with the host snapshot's
  fingerprint.
- **The program itself:** the built-in, its words, its working directory,
  its environment, and `exec`'s options. Its words name files by path
  alone; their contents are the next part's.
- **What it can see:** every input the step has staged, by path and
  contents' digest, and the digests of the trees it can write, the step's
  own output directory and its `/tmp`, as they are before it runs.
- **The step's view:** a chain that starts from the step's platform and own
  output directory, and folds in each effect the step had run before the
  program. An effect that only reads, `fs.exists`, `fs.read`, `File.read`,
  `Dir.files`, or `which`, isn't folded in. Any other folds in its built-in,
  its arguments by path, and what it staged, by contents.

A device and a scratch directory key nothing, and a scratch directory is
neither captured nor put back.

## 6.5.3 Recording

A program that runs is recorded with its status, its output and error, and
the digests of the two writable trees after it, whose contents go into the
store. A tree's digest leaves out modification times and modes other than
the executable bit, so the record also keeps a blob of them: each file's,
directory's and symbolic link's modification time, and each file's and
directory's permission bits, by path. A sealed composition and a mount
point are left out of it. Hashing the trees again after each program is
cheap: the step's sandbox keeps a stamp of each file, as [Blobs and
Trees](/pekit/technical-reference/the-store/blobs-and-trees.md) describes, and reads again only a file
whose stamp changed.

## 6.5.4 Replaying

A program found is not run. Each writable tree whose digest differs from
the record's is placed again from the store, whole, so the step's world is
exactly as the recorded run left it, and the record's result is given to
the step. A record whose contents aren't all in the store is not found, and
neither is a record without the blob of times and modes, such as one made
before pekit kept them.

A replay puts back what the store keeps of a tree: contents, symbolic
links, and whether a file is executable. Then it gives every entry of both
trees, placed again or not, the modification time and mode the recorded run
left, what a directory holds before the directory. So a `make` after a
replay sees which files are newer than others as it would have after the
run, and a `chmod` a found program ran is kept. A replay doesn't put back
which files were hard links of one another, and leaves alone a file with
more than one link. A sealed composition put back is sealed again.

## 6.5.5 What stops recording

An entry the store can't hold stops a step's programs being found or
recorded from then on:

- An entry that is neither a file, a directory, nor a symbolic link, such
  as an overlay's whiteout
- A directory an overlay's attributes mark, such as an opaque one

So a step whose sandbox removes a file of the directory it was made of,
which leaves a whiteout, runs its later programs every time. Overlay
attributes that only say where an entry came from, or which layer it is, are
dropped from a copy and don't stop recording.

## 6.5.6 Where the work is done

The engine keeps each step's view, and the writable trees' digests while it
knows them. A program is sent to the pool, and the thread that takes it
hashes the trees if needed, makes the key, and finds and replays the record
or runs the program, hashes again, and records. No tree is hashed or placed
on the thread that evaluates.

A build reports how many programs its steps found:

```text
built app/hello: 1 step run (2 programs found), 0 found in the store
```

---

# 6.6 The Remote Cache

_Pekit / Pekit TRM / The Store_

> How pekit keeps records and their contents in a server of Bazel's Remote Execution API, how a step missing from the store is looked for there, and how a recorded step is written to it.

A remote cache holds records and their contents for many machines. pekit
uses a server's content-addressable store and action cache, never its
execution service (`remote`). The server is any of the Remote Execution
API, version 2, spoken over gRPC.

## 6.6.1 Records as action results

- **Contents.** A pekit blob is named by the SHA-256 digest of its
  contents, as a REAPI blob is, so a file is the same blob in both.
- **Keys.** A record is kept as an action result. Its action cache key is
  the digest and size of an *action blob*: `pekit-action-1`, a zero byte,
  and the record's key in hex. The action blob is uploaded too, so a
  server that checks an action exists finds it.
- **Outputs.** The result's output files are the record's own encoding, at
  `record`, and each file the record names, at `blobs/<digest>`. Its output
  directories are each tree the record names, at `trees/<digest>`, each as
  a REAPI `Tree` message.
- **What a record names.** Every 64-digit hex string in the record's
  encoding that names a blob in the store, a tree if the blob is a tree's
  encoding, as eviction finds them.

A `Tree` message holds a root `Directory` and every directory under it,
each once. A `Directory` lists its files, with digest, size, and whether
each is executable, its directories by digest, and its symbolic links by
target. pekit's trees name the same entries, so a tree is made again from
its `Tree` message and has the same digest.

## 6.6.2 Finding a step

A step whose receipt has no usable record in the store is looked for in
the cache, as work on the pool, never on the thread that evaluates
(`engine::looked_remotely`):

1. Its manifest is asked for by its key and merged into the store's: each
   set of contexts either holds is kept, the store's first.
2. A receipt is made for each set of the merged manifest, from the values
   the step's contexts give now. Each is asked for in turn, until one is
   found.
3. The step is looked up in the store again. Found, it is counted as found
   in the store and as found remotely. Missed, it runs.

A record found is put into the store only once everything it names is
there:

- Every file blob the result and its trees name that the store lacks is
  downloaded and checked against its digest.
- Each tree is made again from its `Tree` message and checked against the
  digest its path names.
- The record is written last.

A blob that is missing, or not its digest's contents, fails the lookup.
The record is not written, so the step runs. Blobs already downloaded stay
in the store, each its digest's.

Steps are looked for only where records are reused. A build with
`--rebuild`, the second build of `--check-reproducible`, and a build on a
host whose packages cannot be identified ask the cache nothing.

## 6.6.3 Writing a step

A build that uploads writes each step it records, after the record and the
manifest are in the store, as work on the pool:

1. The cache's manifest for the step's base is merged into the store's.
2. For the step's record and then the manifest, the cache is asked which
   of their blobs it lacks.
3. Those blobs are sent, batches of small ones by `BatchUpdateBlobs` and
   each large one by `ByteStream`.
4. The action result is written.

The build waits for its uploads before it ends, and before the store is
evicted. A failed upload fails no build.

## 6.6.4 Transfers

- **Batches.** A batch is kept within the cache's
  `max_batch_total_size_bytes`, and within 4 MiB, gRPC's default message
  limit, counting 256 bytes for each blob besides its contents. A blob too
  large for a batch is read or written by `ByteStream`, in parts of 1 MiB,
  and a read blob is hashed as it is written to the store.
- **Empty blobs.** The empty blob is never transferred, since every cache
  has it.
- **Capabilities.** The cache's capabilities are asked for once, before
  its first transfer. A cache that names its digest functions without
  SHA-256 is not used.

## 6.6.5 Configuration and failure

The cache's URL, instance name, headers, and whether to upload come from
`pekit`'s options and environment, never from the workspace's files.
`pekit(1)` documents them. One runtime of two threads per process runs
every call, and the pool's threads wait on it.

Connecting has 10 seconds. Once a call fails, because the cache cannot be
reached or refuses it, the process asks the cache nothing more and builds
as if it had none. A lookup that fails for a missing or wrong blob affects
that step only. The build's first failure of either kind is reported as a
warning when it ends.

---

# 6.7 Claims

_Pekit / Pekit TRM / The Store_

> How a build claims a step it missed in the store, so that another build that needs the step at the same time waits for its record rather than running it again.

A build that misses a step in the store claims it before it looks in the
remote cache or runs it (`try_claim`). The claim is the lock file
`claims/<base>` in the store, by the step's base receipt, the receipt
without its contexts (see [Receipts](/pekit/technical-reference/steps/receipts.md)), held exclusively
with `flock`. The build holds it until the step is recorded, or has failed
every attempt, or the process ends.

A build that finds the step claimed waits for the claim on a thread of its
own (`Pool::claim`), not one of the pool's, holding nothing of the
machine's budget, and goes on with every other step meanwhile. Once it
holds the claim, it looks in the store again, as it looked first:

- the step's record is there, with everything it names: the step is found
  in the store, and its claim let go of;
- otherwise the build that held it failed the step or ended first: this
  build looks further, and runs the step, holding the claim.

A step's base receipt is the same whatever contexts it reads, so a build
can wait for a step that read a context bound otherwise, miss, and run its
own. That costs a wait, never a wrong result.

Claims are taken only where steps are taken from the store, not by
`--rebuild` or `--check-reproducible`. A claim that can't be opened or
locked isn't taken, and the step runs as it would have.

An eviction, which runs only while no other process has the store, removes
`claims/`. Nothing else removes a claim's file.

---

# 7.1 Exposure

_Pekit / Pekit TRM / Outputs_

> When pekit writes a build's outputs into pekit-out on the host, how a target's code reads outputs before then, and how a target's results are copied out and linked under its name.

Every output of a build, a step's, a fetch's, or an unpacking's, is kept in
the store by its logical path under `/pekit-out`. Steps are staged from the
store, so only what is not hermetic reads outputs from `pekit-out/` on the
disk: a target's own code, the programs it runs, and `pekit run`. An output
is *exposed* there, as read-only hard links to the store's files, only
once something might read it from the disk (`exposure`). A build that
changed nothing writes nothing there for outputs no one reads.

## 7.1.1 When an output is exposed

- **Each exposure waits.** A kept output is noted, and a later note of the
  same path replaces an earlier one.
- **A target's reads are answered from the store.** A target's
  `dir.files()` or `file.read()` of a path inside an output of the build is
  answered from the store's tree of it, which is what the disk would show
  once it's exposed. That holds until a target's own effect runs a program
  or writes, which might have changed the output directory; from then on,
  reads go to the disk, as a read of a path in no output does.
- **Every waiting exposure is made** before a target's own effect that
  might read the output directory: a read that isn't answered from the
  store and whose path is, or leads into, the output directory; a read of a
  path that GCSL code names as a string; a keychain's file; and any effect
  that runs a program or writes.
- **`pekit run`** makes every waiting exposure before the program or script
  runs, since it might read any of them.

Any other build leaves an exposure no one needed unmade, so
`pekit-out/.steps/` and `pekit-out/.unpack/` hold only what was.

## 7.1.2 What a target gives

As a build ends, each file and directory a target's value names under
`/pekit-out` is copied out to `pekit-out/` on the host. Each exposure above
one of its paths is made first, and those at or below its paths are
dropped, the copy taking their place. The copy is the user's: writable,
and a file of its own, not a link to the store.

Each of a target's results under `.steps/<function>/<identity>/` is then
linked under the target's name (`target_view`), as
`pekit-out/<target>/<function>/<rest>`, a symbolic link to the step's
copy. The second step of one function a target's value names is under
`<function>.2`, and so on, numbered in the order the value names them.

A step found in the store has its outputs placed the same way, so a
target's results are there after `pekit clean`, or in a release's fresh
tree, without running anything.

## 7.1.3 What the output directory isn't

`pekit-out/` is not part of the workspace. GCSL's directory listings of the
workspace leave it out, and a step can't be given a file of it by its
workspace path; outputs are named by their `/pekit-out` paths. The listings
leave out the root's `.git` too, and any directory below the root that
holds a `.git` of its own (`HostView::hides`), without descending into it.

---

# 8.1 Eviction

_Pekit / Pekit TRM / Maintenance_

> How pekit keeps its store within PEKIT_CACHE_LIMIT by GreedyDual-Size, what an eviction removes and in what order, how the store's size is known, and when an eviction runs.

The store can grow to the size `PEKIT_CACHE_LIMIT` gives: 100 GiB by default,
or no limit for `0` (`evict::limit_of`). As a build ends, pekit journals
what the build used and, if the store is larger than its limit, evicts until
it is below nine tenths of the limit (`evict`).

## 8.1.1 What is evicted

An eviction removes a record's contents together: every blob and tree a
record names, whether a step's, a program's, an unpacking's, or a
download's. The record stays, but isn't *live*: a later build that
finds it runs what made it again. A blob is removed once no live record
names it at any depth.

An eviction never lists a record's contents whole. Each blob counts what
names it: each live record that names it directly, and each tree a live
record reaches that holds it as an entry. Evicting a record lets go of what
it names. A blob that nothing names any more is removed, and a removed tree
lets go of its entries in turn. Trees share their subtrees. So the memory
an eviction needs depends on how many blobs the store holds, together with
how many entries its trees have. It doesn't depend on how much each record
names. A store of 37 GiB in 128,000 records
evicts in about 200 MiB.

Before it removes any blob, an eviction removes every materialized tree and
every completeness mark, since both depend on blobs being there. In order,
it then removes:

1. Blobs no record names, and numbered and executable copies, which cost
   nothing to make again.
2. Records' contents, and scratch directories, by priority, lowest first.

A scratch directory is evicted whole, as costing nothing to make again, and
never while a run holds it.

## 8.1.2 The order

The order is GreedyDual-Size. Each record's priority is `L + cost / size`,
set when the record was last used, where:

- `cost` is the time it took to make, in milliseconds.
- `size` is the size of its contents, in bytes. A file that two of its
  directories hold counts for each.
- `L` is the priority of the last record evicted.

So what is cheapest to make again for the space it takes goes first, and
among records alike in that, what was used longest ago, since `L` grows as
evictions go on. A large file that took a second to make goes before a
small one that took a minute.

## 8.1.3 What was used

Each build writes a journal of the records and scratch directories it used
into the store's `usage/`, one file per build. An eviction reads every
journal, and keeps each record's priority, with `L`, in `eviction.json`,
with which records it evicted.

## 8.1.4 The store's size

The store's size is its blobs' and its scratch directories'. Walking every
blob on every build would cost too much, so the blobs' size is kept in
`size`, a *ledger*: what an eviction last measured, walking every blob,
plus the bytes of the blobs each build has written since, added under
`size.lock` as each of its steps is recorded and as it ends.

The blobs are walked again only when the ledger is above the limit, is a day
old or older, or isn't there. A build killed part way has added what it
wrote up to its last recorded step, so the ledger falls short by little
more than one step's output, which the next walk sets right.

## 8.1.5 When it runs

An eviction takes the store's lock exclusively without waiting. If another
process has the store open, the build evicts nothing, and a later build
does. A store that can't be kept within its limit is a warning, not a
failure:

```text
warning: the store could not be kept within `PEKIT_CACHE_LIMIT`: MESSAGE
```

If another process has the store open and the ledger says the store is over
its limit, the build says so in the same warning, with the store's size and
its limit, rather than evicting nothing silently. A command shows each
warning once, however many builds it runs.

## 8.1.6 Room during a build

With `keep_free` set, a build evicts during the run as well
(`evict::make_room`), when less than that much is free on the file system
of the store or the scratch directory. It starts no new step until none of
its own steps, effects, or jobs is running. It then evicts until the floor,
and a tenth more, is free. Whatever it costs, it keeps:

- every blob the build has used: put, looked up, or taken by its stamp;
- every record it found or wrote;
- every scratch directory it held.

A step that fails for want of room is held, unstaged, and run once more after
room is made for it: at least the floor, and at least 10 GiB. A step counts as
failing for want of room if its error says "No space left on device", "Disk
quota exceeded", or tar's "Wrote only", or if less than the floor, or 1 GiB,
is free once it has failed. If it fails so again, or no room could be made,
its error ends with a note:

```text
= note: not enough space: the file system of the store and the scratch directory has FREE free, and the build could make no more room
```

As with eviction at the end, room is made beside other processes only as
[Eviction beside other builds](#eviction-beside-other-builds) describes.
Otherwise the build warns that nothing was evicted, and why, and goes on.
It warns too if it could go on only by starting a step it holds, and it
then starts that step.

## 8.1.7 Outputs of evicted steps

A build exposes outputs under its output directory as hard links to the
store's blobs (see [Exposure](/pekit/concepts/where-results-go.md)). An eviction removes
a blob's name in the store, but an exposure's link keeps its contents on the
disk, though the store's size leaves them out. No build can use them,
because a build exposes what it needs again from the store.

Each build notes its workspace's output directory beside its stamps, in
`seen/<digest>.outputs`. An eviction that evicted something with the store to
itself, and every `pekit store evict`, then removes each exposure under
`.steps/`, `.unpack/`, `.git/`, or `.fetch/` of a noted output directory that
holds a file of the store's file system that no name in `cas/` links. It
never touches the files a target gives, nor anything on another file system,
which is a copy rather than a link. What it found is kept in `orphaned`, which
`pekit store stats` shows.

## 8.1.8 Eviction beside other builds

While other processes have the store open, a build evicts beside them, if
every process that holds the store publishes what it uses. Each process
keeps a file of its own in `using/`, `<pid>-<token>`, locked for as long as
it lives. Before it relies on anything of the store, it appends it there,
once: each record before it reads it, each blob before it looks for it or
links it, and each tree it takes as complete. The kernel's `/proc/locks` says
which processes hold the store. One that publishes nothing, an older pekit
say, keeps the build from evicting beside it, and the build warns as it
always did. So does a process that could not write its file, the disk full
say: it removes the file, and publishes no more.

One such eviction runs at a time, holding the `evicting` lock. It also holds
the store's `gate` for its whole length, so that no process opens the store
meanwhile; if either is held, the eviction does not run:

1. It evicts as an eviction alone does, keeping everything any live
   process has published, but it moves each blob it evicts into
   `tmp/evicting-<token>/` rather than deleting it.
2. It reads what each live process has published again, and moves back
   each blob published meanwhile, directly or through a record or a tree.
   If a process has meanwhile put a blob of the same name, of the same
   contents, the one moved aside is dropped instead.
3. It deletes what is still aside.

A process that published a blob before step 2 has it moved back. One that
published it after finds it gone, and puts or builds it again. So no
process relies on what is deleted. Such an eviction removes every mark that
a tree is complete, but it leaves the materialized trees, which a running
step might have mounted, and the outputs of what it evicted, for an
eviction with the store to itself. `pekit store stats` shows how many
materialized trees there are. A process removes its file in `using/` as
it exits. A file there whose lock no process holds is a dead process's, and
the next such eviction, or sweep with the store to itself, removes it.
What a dead evictor moved aside is moved back by the next eviction or
sweep, unless a blob of the same name is there again, and the rest is
deleted.

An eviction as a build ends keeps everything that build used, not only what
other processes use: it still holds the store as it evicts. A store that only
such evictions trim keeps each running build's working set, and is trimmed
further by `pekit store evict` or by an eviction with the store to itself.

---

# 8.2 Concurrency and Sweeping

_Pekit / Pekit TRM / Maintenance_

> How several pekit processes share one store and cache directory, how each names what it makes, and how what a killed process left is removed.

Any number of pekit processes can use one store at once, from one workspace
or several. Each writes only through names of its own and atomic renames,
and nothing they share is changed in place.

## 8.2.1 The store's lock

Every process holds the store's `lock` file shared while it has the store
open. An eviction, and a sweep of what killed processes left, take it
exclusively without waiting, and do nothing if they can't. So neither runs
while another process has the store open, and a process opening the store
waits for one that is running.

## 8.2.2 Names of its own

Every process names what it makes in a directory others share, the store's
`tmp/` and the cache's `scratch/`, with its *token*: 16 hexadecimal digits
read once from `/dev/urandom` (`sweep::token`). A process's number isn't
used, since another process sharing the store, in another PID namespace,
can have the same one.

## 8.2.3 Races

Two processes that make the same blob, tree, record, or materialized tree
each write their own in `tmp/` and rename it into place. The rename that
loses finds the other's there, and drops its own, which is as good, since
both are of the same contents. Within a process, a lock for each
materialized tree, and for each repository `fetch_git` fetches into, keeps
two threads from doing the same work.

Two runs of one step marked `#[scratch]` don't share its scratch directory:
the first holds it by a lock, and the other gets an empty one of its own.

## 8.2.4 Sweeping as a build goes

A step's staging directory often holds tens of thousands of links, and
removing it takes as long as staging it. So a build renames a finished
step's staging directory, which takes no time, and a thread of its own, the
*sweeper*, removes it while the build goes on. The build waits for the
sweeper before it ends, and fails if a directory couldn't be removed.

## 8.2.5 Sweeping what a killed process left

A process killed before it removed what it made leaves it behind. Once the
sweeper is done, a build tries the store's lock exclusively. If it gets it,
no other process has the store open, so everything in the shared
directories is some dead process's or its own, and it removes
(`sweep::sweep_stale`):

- Every entry of the store's `tmp/` that isn't its own
- Every staging directory, swept directory, empty scratch directory,
  unpacking, and checkout in the cache's `scratch/`: the entries whose
  names start with `step-`, `swept-`, `kept-`, `unpack-`, or `git-`

If another process has the store, nothing is swept, and a later build does
it.

---

# 9.1 Queries

_Pekit / Pekit TRM / Commands_

> How pekit query evaluates targets without running a step, what it counts as a file a target reads, and how it treats a target that waits for a step's result.

`pekit query deps` and `pekit query rdeps` evaluate targets as a build
does, in a query mode of the engine (`engine::query_all`), but start no
step.

## 9.1.1 What a query runs

A target's own effects run, since evaluation needs them: its fetches,
unpacks, and reads of the workspace. No step starts, and no step's record
is looked up. What a target reaches is the steps its value needs, or the
steps the result it stopped for needs, each with the steps it is given.

A step's outputs are known only once it runs. A target that stops for a
step's result is done there: what it reached so far is reported, and where
it waits (`TargetDeps::waits`). The query never builds to find out more.

## 9.1.2 What a target reads

A target reads a workspace file if any of these holds:

- Its prepared call's code needs the file, as a GCSL source.
- A step it reaches has code in the file.
- One of its own effects reads the file.
- A step it reaches is given the file as an argument, capture, or context
  value, or is given a directory that holds it.

A GCSL file is also read by every target whose code is in its directory or
below, since its declarations are visible there. A change to it can change
what a name there means, whether or not anything names it yet.
`workspace.gcsl` is read by every target.

## 9.1.3 Reverse dependencies

`pekit query rdeps FILE` lists, of the targets it selects, those that read
the file (`TargetDeps::reads_file`). A target that waits for a step's
result might read the file after the wait, so it is listed too, with a note
on standard error: an unknown is never taken for no.

The prelude's `test` definition type awaits its definition's step inside a
`recover`, so every test's unit waits for its own step's result. So
`pekit query rdeps --kind test` lists every test, those it can't rule out
with a note.

---

# 9.2 Strict and Reproducible Builds

_Pekit / Pekit TRM / Commands_

> What --strict changes in loading, keying, sandboxes, and a target's effects, and how --check-reproducible runs and compares a second build.

## 9.2.1 Strict builds

`--strict` changes four things:

- **Libraries.** Loading fails unless the workspace's libraries are exactly
  those `pekit.lock` pins, as [Libraries and the
  Lock](/pekit/technical-reference/loading/libraries-and-the-lock.md) describes.
- **Sandboxes.** Steps are given no host snapshot: no `/usr`, `/bin`,
  `/lib`, `/lib64`, `/sbin`, or `/etc`, and no default `PATH`. A step runs
  its programs from what it is given.
- **Keys.** Every receipt's platform is the execution platform marked as
  having no host tools, `<platform> no-host-tools`, without the host's
  fingerprint. So a strict build's records are the same on every machine
  of the platform, and apart from a build's that borrowed host tools.
- **Effects.** A target's own effects are limited to fetches, unpacks, the
  keychain, and reads under `/w` and `/pekit-out`, as [A Target's Own
  Effects](/pekit/technical-reference/evaluation/target-effects.md) describes. Since a target then cannot
  write, its `fs.exists` and `fs.read` run out of order, as
  [Order](/pekit/technical-reference/evaluation/the-pool.md#order) describes. Steps marked `#[scratch]` get
  an empty directory.

## 9.2.2 Requiring strict builds

A workspace whose `pekitconf` has `require_strict = true` fails `build`,
`test`, `package`, and `publish` without `--strict`, after loading and
before selecting targets (`strict_as_required`). `run`, `query`, `lock`,
and `clean` don't check it, and `release` is always strict.

pekit needs the value before it builds, to choose the build's settings, and
running `workspace.gcsl` early would evaluate it outside the build. So
`requires_strict` reads the field from the parsed file, running nothing:

- `pekitconf = {require_strict = true}` or `false`, a `Bool` literal in the
  record written at the top level, is read as written.
- A dotted `pekitconf.require_strict = ...` fails the command. pekit reads
  no dotted field of `pekitconf`, and ignoring this one would build what it
  forbids.
- Any other expression, or a `pekitconf` whose type has the field but whose
  record isn't written at the top level, fails the command.
- No such field requires nothing.

## 9.2.3 Checking reproducibility

`--check-reproducible` builds twice (`Settings::compare`):

1. The first build is an ordinary one, finding and recording steps as
   ever.
2. The workspace is checked afresh, and every step whose receipt has a
   record from the first build is run again, its programs neither found
   nor recorded. Its result and outputs are compared, byte for byte, with
   the record's. A directory is compared through its trees, so each file
   that differs is named.

Only steps given the same inputs both times are compared, so a step whose
input differed because an earlier step wasn't reproducible isn't reported
too. The second build records nothing, so a step's first record stays.

A step that differs fails the command:

```text
error: a step is not reproducible: run again with the same inputs, it gave other outputs
  .steps/stamp/7367551baf538abf
    /pekit-out/.steps/stamp/7367551baf538abf/stamp.txt
```

`pekit run` and `pekit publish` refuse `--check-reproducible`, since their
effects change the world and would be done twice.

A `--gcsl-profile` of a checked build covers both builds.

---

# 9.3 Packing, Publishing, and Releasing

_Pekit / Pekit TRM / Commands_

> How pekit package and pekit publish call the workspace's packagers, and the stages of pekit release, from checking the working tree to tagging the commit.

## 9.3.1 Packing

The workspace's packagers are the `packaging` module's `^packagers`, set
from `pekitconf.packagers`. Each is a `packaging.Packager`: a `name`, a
`pack` function of a package to the directory it packed it into, and a
`publish` function of a package, that directory, and a store.

`pekit package` evaluates `packaging.pack(package NAME())` for each package
selected. The module's `pack` calls each packager's `pack`, which is
normally a hermetic step. A workspace with no packager fails with an error
saying to list one in `pekitconf.packagers`.

## 9.3.2 Publishing

`pekit publish` evaluates `packaging.publish(package NAME(), "STORE")` for
each package. The module packs the package with each packager, then calls
each packager's `publish` with the packed directory and the store, as the
target's own effects, in program order, outside any step. The store is an
argument rather than a context, so packing doesn't depend on it, and
publishing into a second store packs nothing again.

A package that fails to publish fails alone, as any target does, and the
packages published before it stay published.

## 9.3.3 Publishing strictly

A packager's `publish` is a target's own effect, which a strict build
refuses. So `pekit publish --strict` builds in two passes, as a release
does (`published_strictly`):

1. **Pack strictly.** Every package selected is packed, as `pekit package
   --strict` packs it.
2. **Gate.** A recipe any of whose packages failed to pack is held back:
   none of its packages is published, each fails, and so does the command.
   A packager such as ppk never publishes a version twice with other
   contents, so a recipe's family of packages is published whole or not at
   all. The other recipes go on to be published.
3. **Publish.** The packages are published with the strict pass's
   settings: no host tools, and the same platform key. The target's own
   effects are allowed, and every step is taken from the store, even with
   `--rebuild`. This pass writes no trace or GCSL profile.

The steps counted are those of the strict pass, and any the publishing ran.

## 9.3.4 Releasing

`pekit release PACKAGE --to STORE` works in these stages (`release`):

1. **Check the tree.** The workspace has to be in a git repository and match
   its `HEAD`: every file committed, nothing changed or untracked, except
   `pekit-out/` and `.gcsl/`. Otherwise the command fails, naming each file
   that differs.
2. **Export.** The workspace's tree at `HEAD` is exported with `git archive`
   into a directory of its own under the cache, so no file the repository
   ignores, or doesn't hold, reaches the build.
3. **Build strictly.** The package is packed there with `--strict`,
   `--check-reproducible`, and the `release` profile. Its value gives the
   package's name and version.
4. **Check the tag.** The tag is `NAME@VERSION`. If a tag of that name is on
   another commit, the release fails, before anything is published.
5. **Publish.** The package is published with each packager into the store.
   Publishing is a target's own effect, which a strict build refuses, so
   this pass keeps the strict build's step settings, no host tools and the
   same platform key, so that its steps are found in the store, and allows
   the target's effects, which the strict pass checked.
6. **Tag.** `HEAD` is tagged `NAME@VERSION` with a lightweight tag, which
   needs no git identity. If the tag is already on `HEAD`, the package is
   published again and no tag is added.

A tag is only ever added, never moved or deleted.

---

# 10.1 Failures of a Build

_Pekit / Pekit TRM / Failure Modes_

> What goes wrong in a pekit build, what each failure looks like, the signal it gives, and how to get back to a known state.

pekit reports every failure on standard error, prefixed `error:`, and
exits with status 1. A failure in GCSL code is shown at the line that made
it, with a library's line shown beneath as "failed here, in library code"
where a library raised it. This chapter lists failures by where they
happen.

## 10.1.1 Before anything is evaluated

| Failure | What it looks like | Cause and recovery |
|---|---|---|
| No workspace | An error naming the directory searched from. | No `workspace.gcsl` at or above the current directory. Run pekit inside a workspace. |
| A check error | GCSL diagnostics, with the file and line. | A source doesn't check. Nothing runs until every source checks. |
| A build and a script of one name | An error naming both. | One directory declares `build NAME` and `script NAME`. Rename one. |
| An ambiguous name | "`build hello` is defined in several places", with each candidate. | Several directories define the name. Give its qualified name. |
| An unknown name | "no `build` is named `hello`", with the closest names. | A typo, or a definition of another kind. |
| A bad cache limit | "`PEKIT_CACHE_LIMIT` is `X`, which is no size". | Set it to a number with a unit, or `0`. |
| A strict build's libraries | "a strict build uses the libraries `pekit.lock` pins, and only them", with each problem. | Run `pekit lock`, remove a changed library's directory, or unset `$GCSL_LIBRARY_PATH`. |
| A library can't be put back | A git error naming what was run. | The repository or commit is unreachable. Check the network and the lock's `git` field. |

## 10.1.2 While evaluating

| Failure | What it looks like | Cause and recovery |
|---|---|---|
| An unpinned fetch | "`fetch` of `URL` is not pinned: its sha256 is `DIGEST`". | Add the digest the error gives, after checking it. |
| A fetch that doesn't match | An error naming the pin and the digest found. | The server's file changed, or the pin is wrong. Nothing is kept. |
| A fetch in a step | "`fetch` runs outside hermetic steps". | Fetch in a parameter default or the caller. The same holds for `fetch_git` and the keychain. |
| A strict refusal | "a strict build refuses a target's own `NAME`", or "reads only the workspace and the build's outputs". | Move the effect into a step, or out of the release. |
| A dynamic dependency under `--no-dynamic` | An error where the target waits for a step's result. | Remove `--no-dynamic`, or restructure the target. |
| Shared calls that need their own results | "this call depends on its own result", each call in the circle labeled with the target or step that needs it and the one computing it. | Memoized calls or `lazy` values that targets share call each other in a circle. A run of either target alone would fail the same way. Break the circle. |
| Units waiting for each other | "the build's targets and steps wait for each other's shared results". | Two units each need a result the other is computing, through a step's result. Restructure the shared code. |
| No toolchain | An error naming the kind and both platforms, and saying that registering replaces the default. | Register a toolchain of the kind for the platforms. |

## 10.1.3 While running a step

| Failure | What it looks like | Cause and recovery |
|---|---|---|
| A program fails | "`PROGRAM` exited with status N", at the `exec`, with its standard output and standard error. | The step's own failure. Its retries run first, if it has any. |
| A timeout | "the step timed out", at the program it was running. | The step's attempts together ran past its `#[timeout]`. |
| A path outside the step | "it is outside the step". | The step uses a file it wasn't given. Pass it as an argument. |
| A write to an input | An error that the path is an input of the step, or a finished composition. | Work on a copy, or in a `Dir.sandbox`. |
| A mount point moved | A program fails with "Device or resource busy". | A program tried to move or remove a directory a composition mounts. Copy it first. |
| A result outside the step | An error naming the file the result names. | A step gave a path it neither made nor was given. |
| No bubblewrap | "pekit runs programs under bubblewrap (`bwrap`), which is not installed". | Install bubblewrap. |
| An unknown device | "pekit gives a step no device `NAME`: the one it knows is `kvm`". | Use `#[device("kvm")]` only. |
| A flaky step | A warning: "step `NAME` passed only when it was run again: it is flaky". | Not a failure. Find what makes it vary. |

A step that fails fails every step that needs its result, unrun, with the
same failure, and every target that needs it. Other targets carry on.

## 10.1.4 At the end of a build

| Failure | What it looks like | Cause and recovery |
|---|---|---|
| Not reproducible | "a step is not reproducible", with the step and its files. | The step's outputs vary. See the common causes on [Checking That a Build Is Reproducible](/pekit/building/checking-reproducibility.md). |
| A staging directory not removed | An error from the sweeper. | Something under the cache's `scratch/` can't be removed. Check its permissions. |
| The store over its limit | A warning that it "could not be kept within `PEKIT_CACHE_LIMIT`". | Not a failure. Another process might hold the store; a later build evicts. |
| The host unidentified | A warning that no step is reused from the store. | The host has no package database pekit knows. Build strictly to reuse steps. |

## 10.1.5 Getting back to a known state

These restore a known state, from least to most drastic:

1. **Remove a changed library.** Remove its directory under
   `.gcsl/libraries/`, preserving any edits first; the next build puts back
   the pinned one. Untouched exports with pekit's record refresh
   automatically when the lock moves. An export made by an older pekit
   that already differs has no record and needs this one-time removal too.
2. **`pekit clean`.** Removes `pekit-out/`. The next build puts every
   target's results back from the store, running nothing.
3. **`--rebuild`.** Runs every step and program again, and records each. It
   doesn't repair a changed blob: a blob already in the store isn't
   written again.
4. **`pekit clean --cache`.** Removes the store, its scratch directories,
   and the cache's staging directories. The next build runs everything.
   The state directory, and with it the machine's keychain key, stays.

## 10.1.6 A store whose blobs were changed

A blob is shared by hard links with every staging directory, materialized
tree, and exposed output that holds it. Changing one of those files, its
contents or its permissions, changes the blob, and pekit doesn't detect
it: later steps are given the changed file, and their records are wrong.
A blob's name is its contents' SHA-256, so a blob can be checked by hashing
it and comparing the digest with its name, ignoring a `.x` or `.rN`
suffix. A plain blob's mode is `0444`, and an executable one's `0555`.
After such a change, `pekit clean --cache` is the safe recovery.

## 10.1.7 A killed build

A build killed part way leaves files in the store's `tmp/` and staging
directories in the cache's `scratch/`. Nothing it left is seen as complete,
since everything is renamed into place only once whole. The next build that
finds no other process using the store sweeps them, as [Concurrency and
Sweeping](/pekit/technical-reference/maintenance/concurrency-and-sweeping.md) describes.

---

# Appendix A On-Disk Layout

_Pekit / Pekit TRM_

> Every file and directory pekit reads or writes, in the workspace, the cache directory, and the state directory, with what each holds.

This appendix collects every path pekit uses. `CACHE` is the cache
directory, `$PEKIT_CACHE_DIR`, `$XDG_CACHE_HOME/pekit`, or
`~/.cache/pekit`. `STATE` is the state directory, `$PEKIT_STATE_DIR`,
`$XDG_STATE_HOME/pekit`, or `~/.local/state/pekit`. `CONFIG` is the
configuration directory: `$XDG_CONFIG_HOME/pekit`, else `~/.config/pekit`.
`BUDGET` is the machine's budget's directory: `$XDG_RUNTIME_DIR/pekit/budget`,
else `CACHE/budget`.

## A.1 In the workspace

| Path | Holds | Written by |
|---|---|---|
| `workspace.gcsl` | The root file, and `pekitconf`. | The user. |
| `pekit.lock` | The pinned libraries and their sources, as JSON. | `pekit lock`. |
| `pekit.sources` | What each recipe's packages are built from, as JSON. | `pekit sources`. |
| `.gcsl/libraries/<path>/` | Each pinned library, exported. | `pekit lock`, and restoring before a build. |
| `pekit-out/<target>/<function>[.N]/` | Symbolic links to each target's results. | Each build. |
| `pekit-out/.steps/<function>/<identity>/` | A step's outputs: copies of what targets gave, and read-only links for exposed outputs. | Each build. |
| `pekit-out/.fetch/<sha256>/<name>` | An exposed fetched file. | Each build. |
| `pekit-out/.unpack/<key>/` | An exposed unpacked archive. | Each build. |
| `pekit-out/.git/<key>/<name>/` | An exposed git commit's files. | Each build. |
| `pekit-out/.pekit-copies` | What each copy a build made holds, and its files' stamps as made, so a later build leaves an unchanged copy. | Each build that copies. |

## A.2 In the cache directory

| Path | Holds |
|---|---|
| `CACHE/git/` | Bare clones of the repositories libraries are pinned from. |
| `CACHE/scratch/step-<token>-<node>-<attempt>/` | A step attempt's staging directory: `w/`, `out/`, and `tmp/`. |
| `CACHE/scratch/swept-<token>/` | Staging directories renamed for the sweeper to remove. |
| `CACHE/scratch/kept-<token>-<node>/` | An empty scratch directory for one run of a step marked `#[scratch]`. |
| `CACHE/scratch/unpack-<token>-<n>/` | An archive being unpacked. |
| `CACHE/scratch/git-<token>-<n>/` | A git commit being checked out. |
| `CACHE/store/` | The store, below. |

## A.3 In the store

| Path | Holds |
|---|---|
| `lock` | The store's lock, held shared by every process with the store open. |
| `cas/<xx>/<digest>` | A blob, read-only. |
| `cas/<xx>/<digest>.x` | The executable copy of a blob. |
| `cas/<xx>/<digest>.rN`, `<digest>.x.rN` | Numbered copies of a blob. |
| `records/<xx>/<key>` | A record, as JSON: a step's, a program's, a manifest, a fetch's, an unpacking's, or a git commit's. |
| `trees/<digest>`, `trees/<digest>-<bases>` | A materialized tree's instance. |
| `complete/<digest>` | A mark that a tree has everything it names. |
| `seen/<digest>.stamps` | The stamps of a workspace's files, by a digest of its root's path, in a binary form. |
| `seen/<digest>-libraries.stamps` | The stamps of the files of a workspace's locked libraries. |
| `seen/<digest>.root` | The path of the workspace whose stamps these are. |
| `seen/<digest>.outputs` | The path of that workspace's output directory. |
| `git/` | Bare clones of the repositories `fetch_git` fetched from, each with a `.lock` beside it that a fetch into it holds. |
| `scratch/<digest>` | A step's scratch directory, with `<digest>.lock`, `<digest>.size`, and `<digest>.dirty` while a run has it. |
| `claims/<base>` | The lock file of the claim on a step, by its base receipt. |
| `usage/` | A journal of what each build used. |
| `eviction.json` | Each record's eviction priority, and what was evicted. |
| `using/<pid>-<token>` | What each process that has the store open uses of it, published, held locked by the process, and removed as it exits. |
| `evicting` | The lock of an eviction beside other processes. |
| `tmp/evicting-<token>/` | What such an eviction has moved aside, until it puts back or deletes it. |
| `orphaned` | What output directories held of evicted steps when an eviction last looked: bytes, exposures, and when. |
| `size`, `size.lock` | The ledger of the blobs' size, and its lock. |
| `tmp/` | Files and directories being written, each named with a process's token. |

## A.4 In the state directory

| Path | Holds |
|---|---|
| `STATE/keychain-key` | The machine's 32-byte key that secrets' identities are hashed with, mode `0600`. |
| `STATE/peaks/<digest>`, `STATE/peaks/kind-<digest>` | The last peaks of a step's program, and of its kind's. |

## A.5 The machine's budget

| Path | Holds |
|---|---|
| `CONFIG/machine.conf` | The budget's limits, `jobs`, `memory`, and `unseen`, and the machine's `cache_limit` and `evaluating`. Written by the user. |
| `BUDGET/job-<n>`, `BUDGET/memory-<n>` | The lock files of the budget's jobs and units of memory. |
| `BUDGET/waiting`, `BUDGET/gate` | The lock files a waiting program holds. |
| `BUDGET/fifo-<pid>-<n>` | A running program's jobserver. |

[The Machine's Budget](/pekit/technical-reference/evaluation/the-machines-budget.md) says what each is.

## A.6 In a step's sandbox

[Paths in a Step](/pekit/reference/paths-in-a-step.md) lists the paths a step sees.
