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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/scripts/fork-contracts.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4175,6 +4175,9 @@ def result_step(jobs, name):
"fork-ci/actions/head-check/review_guide.test.py": ["ReleaseTags." + t for t in (
"test_override_authors_is_first_in_the_release_after_the_one_the_notes_name",
"test_the_path_rules_are_first_in_the_release_the_guide_names")],
# Runs an older release's installer, found by its ci/vN tag; CI's checkout fetches none.
"fork-kit/heavy/tests/install.test.py": [
"Install.test_older_installers_install_and_uninstall_keep_the_hosts_t3_capacity"],
}
check("records: the tests a part run may skip are exactly the pinned ones, each by name (MAY_SKIP)",
{f: sorted(n) if isinstance(n, list) else n for f, n in DECLARED_SKIPS.items()} == MAY_SKIP,
Expand Down
56 changes: 43 additions & 13 deletions fork-kit/heavy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Linux with systemd (user manager) and cgroup v2 only. Python 3 standard library
| ---------------------------- | -------------------------------------------------------------------------------------------- |
| `heavy <command> [args...]` | runs the command in `heavy-<name>-<random>.scope` in the slice; stops leftovers when it ends |
| `HEAVY_KEEP=1 heavy <cmd>` | the same in `heavy-keep-<name>-<random>.scope`, but leftovers keep running (a daemon) |
| `heavy --self-check` | checks the config, that the slice has a unit with a memory limit, and the installed files |
| `heavy --self-check` | checks the config, the slice's limit and the installed files; warns on another `t3-capacity` |
| `systemctl --user stop UNIT` | stops what `HEAVY_KEEP=1` kept (the name is in heavy's message) |
| `heavy sweep [--dry-run]` | one leftover sweep (below); the user timer runs it every 5 minutes |
| `heavy sweep --json` | looks only: what a sweep would stop or report, as JSON |
Expand Down Expand Up @@ -569,13 +569,17 @@ after another, up to the group's ceiling), the memory budget, the queue and note
cache folder nothing carries, so every call starts "elevated" (only the rule's floors admit; never straight to
"ok").

Two thin callers come with it (not linked onto PATH: a host wires them in itself):
Two thin callers come with it:

- `bin/t3-capacity [--kind KIND] [MB]`: the older host check's output (first line `available=..MB swapfree=..MB
load=..`, available less the rule's whole reservation hold, last line `OK` or `NOT OK: why`, exit 0 or 1), now
from `heavy facts`. MB defaults to 3072.
- `bin/agent-slots`: a Claude Code hook (UserPromptSubmit, Stop) for orchestrator sessions: free agent slots are
the smaller of the process limit and the machine's `fits` for its kind.
from `heavy facts`. MB defaults to 3072. The installer links `/usr/local/bin/t3-capacity` to it, so the command
has one source, the release in use (a host's own plain copy there only with `--replace-t3-capacity`: Install,
below); `heavy --self-check` warns when the `t3-capacity` found on PATH is another copy (its readings can differ:
an older copy, for one, counts no reservations).
- `bin/agent-slots` (not linked onto PATH: a host wires it in itself): a Claude Code hook (UserPromptSubmit, Stop)
for orchestrator sessions: free agent slots are the smaller of the process limit and the machine's `fits` for
its kind.

A `heavy facts` that does not answer within 5 s (it is killed), fails or prints something else is never free for
either caller.
Expand Down Expand Up @@ -645,9 +649,11 @@ limit options. A user's own unit in `~/.config/systemd/user/` wins over the one
| ----------------------------------------------- | ------------------------------------------------------------------------- |
| `/usr/local/bin/heavy` | link to `current/bin/heavy` |
| `/usr/local/sbin/heavy-install` | link to `current/sbin/heavy-install` |
| `/usr/local/bin/t3-capacity` | link to `current/bin/t3-capacity` |
| `/usr/local/libexec/heavy/versions/<sha>/` | the code of one release, with `MANIFEST.sha256` (root 755) |
| `/usr/local/libexec/heavy/current` | link to the release in use (switched in one step) |
| `/usr/local/libexec/heavy/replaced/` | a plain `/usr/local/bin/heavy` from before the kit, put back on uninstall |
| `/usr/local/libexec/heavy-kept/` | a plain `/usr/local/bin/t3-capacity` (a host's copy), put back likewise |
| `/etc/heavy/config.json` | slice name and grace time |
| `/etc/heavy/installed.tsv` | every installed path with its link target or hash (`--self-check`) |
| `/etc/systemd/user/<slice>` | only with limit options: the slice unit |
Expand All @@ -674,17 +680,30 @@ It prints the sha, the hash of the download and every installed path. Add `--sli
runs `systemctl --user daemon-reload`). Add `--sweep-timer` to install the leftover sweep's user units (each user
then runs `systemctl --user daemon-reload && systemctl --user enable --now heavy-sweep.timer`; `--no-sweep-timer`
removes them again). Upgrade: the same line again. Remove:
`/usr/local/sbin/heavy-install uninstall` (puts an old plain `/usr/local/bin/heavy` back). Check:
`heavy --self-check` (as the user: config, slice and files) or `heavy-install self-check` (files only).
`/usr/local/sbin/heavy-install uninstall` (puts an old plain `/usr/local/bin/heavy` and `/usr/local/bin/t3-capacity`
back). Check: `heavy --self-check` (as the user: config, slice and files, and a warning when `t3-capacity` on the
user's PATH is not the kit's) or `heavy-install self-check` (files only).

`/usr/local/bin/t3-capacity`: with nothing there, or the kit's link, the installer links it. A plain file there is a
host's own copy, which the host's scripts may read or compare: the installer leaves it as it is and says so (in a
note and again in its closing `installed release` line), until it is run once with `--replace-t3-capacity` (it
keeps the copy in `/usr/local/libexec/heavy-kept/`, links the kit's, and later runs keep the link without the
option). A link there that the kit did not make is left alone the same way; with `--replace-t3-capacity` it is
refused (remove it by hand first: there is no file to keep). A host whose own installer wrote that copy first changes it to stop writing
that path and to run `heavy-install` (or check that the path is the link to
`/usr/local/libexec/heavy/current/bin/t3-capacity`): a copy over the link would land in the release's own folder
(`heavy-install self-check` then says `changed since install`), and one written beside it and renamed over it puts
a plain copy back (`heavy --self-check` says the link differs and warns about the copy).

## Runs again, stops half-way, old or empty state

- **Runs again:** every step checks what is there and only fixes what differs; a second run changes nothing.
- **Stops half-way:** the steps run in this order: check, folders, copy, self-test, config, slice unit, switch,
links, sweep units, record. A version folder is copied to a temporary name and renamed; a half-copied or changed
one is made again. The old `/usr/local/bin/heavy` keeps working until the links step, which copies it to
`replaced/` before the link replaces it. The sweep's units are written only after that, so a timer never runs
before the release it needs is in use. Running the same line again finishes the job.
one is made again. The old `/usr/local/bin/heavy` and `/usr/local/bin/t3-capacity` keep working until the links
step, which copies each to its kept folder before its link replaces it in one rename. The sweep's units are
written only after that, so a timer never runs before the release it needs is in use. Running the same line again
finishes the job.
- **Rollback:** the timer's service runs `/usr/local/libexec/heavy/current/lib/heavy_sweep.py` (the sweep of the
release in use, never an older `heavy` without the `sweep` verb), and only when that file exists. Before going
back to a release without the sweep, run the newer installer with `--no-sweep-timer`; if you did not, the
Expand All @@ -697,10 +716,21 @@ removes them again). Upgrade: the same line again. Remove:
- **Rollback past the memory queue:** remove the `queue` key from the machine file's `heavy` section first. A
release from before the queue does not know it: its sweep refuses the whole machine file and says which key. That
release cannot be changed from here, so the order is the guard: key out, then the older release.
- **Rollback past the `t3-capacity` link:** an older installer leaves `/usr/local/bin/t3-capacity` as it is, a link
to `current/bin/t3-capacity`; every release since `heavy facts` has that file. Before going back further, run
`heavy-install uninstall` (it puts the old copy back). The old copy is kept in `/usr/local/libexec/heavy-kept/`,
outside `/usr/local/libexec/heavy` and `/etc/heavy`, because an older installer's uninstall deletes those two
folders whole (and puts the newest file of `replaced/` back as `heavy`). That older uninstall does not know the
`t3-capacity` link, so it leaves it broken (every `t3-capacity N && …` gate then fails): run this release's
`sbin/heavy-install uninstall` from its source folder, as root, which removes the broken link and puts the kept
copy back, or install again.
- **Old state:** a plain-file `/usr/local/bin/heavy` (the one-line wrapper from before) is kept and put back by
uninstall. A link there that points elsewhere, a folder where the kit expects its own, or a slice unit in
`/etc/systemd/user/` the kit did not write is refused with a message, never overwritten. Config values stay
unless an option names a new one.
uninstall; a plain `/usr/local/bin/t3-capacity` (a host's own copy) is left alone, or with `--replace-t3-capacity`
kept and put back by uninstall. A link at `t3-capacity` that points elsewhere is left alone (refused with
`--replace-t3-capacity`). A link at `heavy` or `heavy-install` that points elsewhere, a folder where the kit
expects its own, or a slice unit in `/etc/systemd/user/` the kit did not write is refused with a message, never
overwritten. Config
values stay unless an option names a new one.
- **Empty state:** a fresh machine installs with the defaults. heavy then refuses to run commands until the slice
has a unit with a memory limit (install with limit options, or add the unit).

Expand Down
19 changes: 18 additions & 1 deletion fork-kit/heavy/bin/heavy
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

heavy <command> [args...] run <command> in its own scope in the slice, wait for it, stop the scope
HEAVY_KEEP=1 heavy <command> the same, but processes the command leaves running are kept (a daemon)
heavy --self-check check the config, the slice and the installed files; exit 1 on a problem
heavy --self-check check the config, the slice and the installed files (exit 1 on a problem), and
warn when the t3-capacity on PATH is not this kit's
heavy sweep [--dry-run|--json] stop or report what commands left behind outside heavy (lib/heavy_sweep.py)
heavy leftovers [--older-than AGE] [--json]
look only: what is left behind (processes, heavy scopes, containers) older
Expand Down Expand Up @@ -1059,6 +1060,20 @@ def sha256_file(path):
return h.hexdigest()


def capacity_on_path():
"""None when the `t3-capacity` found on PATH is this kit's bin/t3-capacity (links resolved), else why not. Only a
warning: a host may wire the command in itself, but another copy's readings can differ from the kit's."""
import shutil
kit = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'bin', 't3-capacity')
found = shutil.which('t3-capacity')
if found is None:
return f't3-capacity is not on PATH (this kit\'s is {kit}; heavy-install links /usr/local/bin/t3-capacity)'
if os.path.realpath(found) != os.path.realpath(kit):
return (f't3-capacity on PATH is {found}, not this kit\'s {kit}: its readings can differ from the kit\'s '
f'(heavy-install links /usr/local/bin/t3-capacity; remove the other copy or put that folder first)')
return None


def self_check():
problems = []
try:
Expand Down Expand Up @@ -1095,6 +1110,8 @@ def self_check():
problems.append(f'{path}: differs from what was installed ({kind})')
if rows is not None:
print(f'installed files: {len(rows)} checked')
warning = capacity_on_path()
print(f'WARNING: {warning}' if warning else 't3-capacity on PATH: this kit\'s')
for p in problems:
print(f'PROBLEM: {p}')
print('self-check: ' + ('OK' if not problems else f'{len(problems)} problems'))
Expand Down
Loading
Loading