Skip to content

plugin-clean

Placement compiled-in (in-process)
Source github.com/opencharly/plugin-clean/candy/plugin-clean
Candy plugin-clean

This plugin is listed in charly/charly.yml’s compiled_plugins:, so its providers are compiled into the charly binary and register in-process.

The reserved words this plugin serves:

  • clean — command class
  • retention — verb class

COMPILED-IN charly COMMAND-class plugin that OWNS the externalized charly clean CLI — the build-artifact retention/prune surface. The plugin owns the command end to end: the flag grammar (–dry-run / –images / –check / –deep / –cache / –scopes / –build-cache / –store / –keep / –invalidate), the category orchestration, and the report output. No plugin-specific command LOGIC is left in core.

The CATEGORY SET is declared once, in schema/clean.cue (#CleanPlugin.categories), and the Go grammar is DERIVED from it: newCleanFlagSet registers one flag per declared row with the row’s own contract: as its help text, and cleanCategories computes the run set from the rows’ bare:/implies: fields. So “naming a category flag suppresses the default set” and “–deep also reaps build scopes” are declared DATA, not a hand-written anyCategory := … chain plus a doScopes = … rule that every reader had to re-derive and every new category had to edit.

–deep is the store-wide untagged/dangling-image purge category — the CLI-only- interface gap-closing capability for issue #173: a multi-stage build’s INTERMEDIATE stage images are only ever labeled at the FINAL stage (WriteLabels emits at the end of the last stage), so they accumulate as unlabeled dangling images the default charly-labeled sweep (images/DanglingIDs, no flag needed) can never see. --deep removes EVERY untagged image in local storage, respecting the SAME InUse/live-build backstops as the default sweep (never a tagged image, never a container-referenced one, never mid-build — see pruneDanglingImages/selectDanglingImages in retention.go); removing a dangling image also frees any layer blobs it alone held (podman’s overlay storage GCs an unreferenced layer on last-reference removal), so this is EFFECTIVELY a dangling-image-plus-unused-layer prune. --deep NEVER fires implicitly on a plain charly clean (R5: zero default-behavior change) — it is strictly opt-in, mirroring --images/--check’s “runs ONLY this category” semantics. --deep --dry-run is the safe default probe: it reports the would-remove count + an UPPER-BOUND reclaimable-bytes figure (DeepBytes, summed from each candidate’s reported storage Size) and touches nothing. That figure is “up to”, never a firm prediction: RDD-verified live, a –deep purge removing 68 untagged images (3,552 → 3,484) reported ~92.6 GiB via the naive per-image Size sum but freed only ~4.6 GiB of real disk (132.6 GB → 128 GB), because most of those bytes were layers SHARED with the ~3,400 remaining (largely stale-tagged) images — removal only frees layers an image held UNIQUELY. Pair --deep with --invalidate (which removes stale TAGS, freeing their exclusively-held layers too) to get closer to the reported figure.

A live-build-guarded sweep DECLINES; it is not an error, and it no longer looks like an empty store. While any build is in flight (a held lock under ~/.cache/charly/ locks/builds) the dangling-image sweeps (dangling for the charly-labeled default, deep for the store-wide purge) and the buildah staging sweep remove NOTHING — and the CLI now prints <label>: SKIPPED — N build(s) in flight; <cause> (re-run when builds are idle) IN PLACE OF the removed count. Previously a declined sweep and a genuinely empty store printed the identical removed 0 untagged image(s) line, so a host with tens of GB of removable dangling images looked like a blind tool — which is how an operator ends up at raw podman rmi -f, deleting the build-layer cache that makes the next build ~8x faster. The staging line now also prints unconditionally, so its absence can no longer mean two different things.

This plugin OWNS the SHARED retention ENGINE too (retention.go: pruneImagesByRetention / pruneCheckRuns / pruneBuildCandyDirs / invalidateImageTags / pruneDeepDanglingImages + the charly-labeled image-tag CalVer/label inventory) — K1-alpha core-minimization relocated it here from charly/retention.go, since it has ZERO core-only dependencies (kit.CalVer/kit.ParseCalVer/kit.ListLocalImages/kit.BuildActivityDir are all sdk-portable). Image-tag retention orders by CREATION TIME — the only recency key total over the tags charly mints — with the build-tag CalVer as the tiebreak, and budgets two ordinals per box (the newest N distinct IMAGES, a distinct image being a distinct content identity = the image-config digest the engine reports as the image ID, and at most N tags of each). The ai.opencharly.version label is DELIBERATELY NOT an ordering key: the schema-versioning-removal cutover stopped emitting it, so ordering by it ranked a pre-cutover labelled image ABOVE a newer unlabelled one — an inversion that selected the newest image for removal (opencharly/plugin-clean#10). It survives only as the charly box list tags version column, whose wire field the spec repo still defines; the retention exemption (a tag whose :YYYY.DDD.HHMM cannot be read is never removed) therefore guards on the build tag ALONE. charly clean’s own CLI calls the engine LOCALLY (no wire hop, same package). The other three callers — charly box build’s post-build prune, charly box list tags, and candy/plugin-check’s post-run prune — reach it via verb:retention (a spec.RetentionRequest → spec.RetentionReply Invoke), the SAME peer/core-adapter pattern verb:credential/verb:gpu/verb:tunnel already use: core’s two callers (already running LoadConfig in-process) resolve defaults.keep_images/ keep_check_runs themselves and pass the resolved ints in the request; plugin-check (a peer plugin, not core) reaches verb:retention via InvokeProvider.

The project’s defaults.keep_images/keep_check_runs, for ITS OWN CLI (charly clean, no –keep flag), resolve PLUGIN-SIDE via the shared sdk/loaderkit.ResolveRetentionDefaultsViaExecutor (K-wave 2 cone R6 — the former “retention-defaults” HostBuild seam charly/host_build_retention_defaults.go is DELETED: the loader is plugin-reachable over the reverse channel, so every verb:retention caller resolves the tunables itself; “retention” is a class-generic action noun, not a provider word (F11).

clean is COMPILED-IN (listed in charly/charly.yml compiled_plugins) because command:clean’s Invoke(OpRun) needs the in-proc reverse channel — threaded by dispatchInProcCommand (“Seam A”) — to reach the host loader legs for the retention-defaults resolve. The out-of-process CliMain path has no reverse channel, so the categories needing a resolved keep-default (images/check/deep) error there; list/invalidate need no default and run standalone even out-of-process. There is NO hidden core-command forward — the plugin does the work directly, resolving the project config default itself; no core symbol crosses the boundary, no ad-hoc podman.

The scopes category reaps HUNG transient build scopes — the systemd USER scopes buildah/podman put a build container into (<runtime>-buildah-<id>.scope; measured live on a real build: runc-buildah-buildah2754860408.scope). Image builds are the only charly operation that starts a transient scope, and nothing else reaps a hung one: the builder’s own teardown dies with the builder, and the run metadata charly reap-orphans scans is gone with the worktree (opencharly/plugin-clean#11 measured one ACTIVE for seven days at 309% CPU and 12.27 load on 16 cores). A scope is stopped (systemctl --user stop, whose default KillMode=control-group takes its whole tree down) only when it has been ACTIVE for at least 1h AND no buildah/podman builder process is alive anywhere on the host; the sweep also declines wholesale while charly’s own build-activity lock is held. It runs by default and with –deep, and any other explicit category suppresses it. It needs neither the engine binary nor the resolved keep-defaults, so it is a CLI-only host-hygiene surface: no peer reaches it over verb:retention, and no new spec wire field carries it.

The build-cache category reclaims the builder’s PERSISTENT cache — the one large artefact a host accumulates during builds that no other category could see (opencharly/plugin-clean#23): sdk/buildkitdrive owns the daemon’s --root at CacheDir()/buildkitd (StateRoot), and it is not one of charly’s stores (D-STORE-3: a store tree IS an OCI Image Layout and nothing else — it carries no oci-layout marker and it MUST carry a buildkitd.lock), so --images (the engine’s storage), --check (.check run dirs), --deep (dangling engine images), --cache (the OCI-layout stores under the cache root) and --scopes (systemd build scopes) all correctly never saw it. Measured on this host: the live root held 50 GiB and a QUARANTINED sibling (buildkitd.broken-1794801825) held 105 GiB — removed by hand with sudo rm -rf because no verb could reclaim it and nothing in the tree emitted that name. Two instruments, one category: the LIVE root is removed only when no daemon owns it (a running buildkitd holds an exclusive flock on <root>/buildkitd.lock — the lock IS the holder’s liveness signal — so the sweep SEIZES that lock before touching the root, refuses with the daemon NAMED when it cannot, and no daemon can start while the sweep holds it), and every quarantined sibling (buildkitd.broken-*, dead by construction) is reclaimed unconditionally. Bytes are reported PER ROOT, --dry-run measures the same figures and touches nothing (it does not even create the lock file to ask), and nothing is removed implicitly on a bare charly clean. A tree this user cannot delete WHOLE — a rootless build leaves its store owned by container-mapped UIDs, measured at 301 708 files in that quarantined root — is reported BY NAME with the blocking path and uid and left exactly as it was: a half-deleted store is a corpse nobody can diagnose. One implementation: both paths come from sdk/buildkitdrive (StateRoot/SocketPath/LockName) and the roots from the ONE resolver behind them, never from a hand-built <cache>/buildkitd.

The store category implements D-STORE-7 (DESIGN.md:2488) for the charly image store — the registry NAMED store under the cache root, whose entries are the artifacts every build appends (never replaces). “keeps what state references and evicts the rest within a size budget” is a REFERENCE rule, not a recency one: the sweep reads the host’s charly state by name and resolver — the container engine’s containers (podman ps -a, every container running or stopped, its image ref and its image ID) and the quadlet units ($HOME/.config/containers/systemd/charly-*.container, Image= — the only record of an ENABLED deploy whose container does not exist yet) — resolves each state reference through ImageStore.Lookup (tag → digest is the STORE’s answer, and it is what makes a superseded rebuild of a referenced tag reclaimable), keeps every entry a reference or a content address reaches, and evicts the rest before reclaiming the blobs no surviving entry reaches. It REFUSES — and evicts nothing — when a state surface cannot be read: an unreadable surface is not an empty one, and assuming nothing is referenced would delete the image a running deployment was built from. The size budget is AUTHORED in schema/clean.cue (#CleanPlugin.store_budget, SDD) and never a Go constant; an over-budget store is REPORTED, because the only entries left are the ones state references. It never fires on a bare charly clean, and a live build makes it DECLINE.

The --cache category’s routing was fixed in the same change (opencharly/plugin-clean#27): the sweep used to run in two halves and SKIP the storekit half whenever the two store roots coincided — which CHARLY_CACHE_DIR makes them, so on the very hosts that isolate their cache the image store was handed to spec/cache’s image-blind GC, which deleted a live image’s manifest, config and layers while leaving its entry answering Lookup(ref) for bytes that were gone. The reader is now chosen by the STORE’S OWN IDENTITY (index.json descriptor media types: storekit entry manifests → storekit, OCI image manifests → spec/cache), the two enumerations are deduped into ONE inventory, a store whose identity cannot be decided is REFUSED and reported, and storekit’s own index lock lives in the runtime root so no spec/cache .lock is written inside a store tree (D-STORE-3).

Two capabilities: command:clean dispatches through the COMPILED-IN registry path (registerCompiledPlugin → resolve(ClassCommand,“clean”) → dispatchInProcCommand → Invoke(OpRun) with the threaded in-proc reverse channel); verb:retention is invoked directly by core adapters / peer plugins with no authored plugin_input, mirroring verb:credential. NewMeta advertises both while the served CUE schema carries no plugin_input (verb:retention’s params are the internal spec.RetentionRequest RPC, never an authored plan step; command:clean’s args are plain CLI tokens). The full charly clean end-to-end — including the --cache category’s CAS ArtifactStore GC — is exercised by this candy’s own Go tests (TestGCCacheStores / TestPrintRetentionResult_CacheCategory) and by a live charly clean --cache run against real stores; the --dry-run / --deep sweeps are additionally witnessed by the charly superproject’s check-commands-local bed.

The CUE schema below is the authoritative grammar for this plugin’s input. It is the same single source that generates the plugin’s Go parameter types and answers the runtime Describe RPC, so this page cannot disagree with either.

// plugin-clean's OWN self-contained CUE schema — the plugin's declaration
// surface, used two ways exactly like every other plugin's schema (there is
// no schema-less plugin):
//
// 1. SERVE over Describe — the host splices `base ++ plugin` at the load gate, so the
// plugin's declarations travel WITH it and a self-contained schema that will not
// splice is a LOUD load failure.
// 2. DOCUMENT the plugin's published surface — the reference site's per-plugin page is
// rendered from its providers, this schema, and the candy description.
//
// command:clean's authored input is its pass-through CLI grammar (the OpRun `{args:
// [...]}` envelope) and verb:retention is invoked by peer plugins via InvokeProvider, so
// this schema DOCUMENTS both capability contracts rather than a structured
// plugin_input. SELF-CONTAINED: it references no base def, so it compiles STANDALONE
// (the property that lets the SDK compile it serve-side).
#CleanPlugin: {
// The declared capability words (the plugin.providers surface) — the command:clean
// CLI plus the verb:retention engine peers invoke — recorded here as part of the
// plugin's published declaration surface.
providers: [...string]
// The command word the plugin serves.
command: "clean"
// The verb word the shared retention engine is invoked by.
verb: "retention"
// What the command does, in one line (the public-docs surface).
contract: string & !=""
// The configuration surface: env var names the plugin reads, recorded here as part
// of the plugin's published declaration surface.
config?: [string]: string
// store_budget — D-STORE-7's SIZE BUDGET for the charly image store, authored here
// because it is a VALUE the sweep decides against and no hard-coded constant may stand
// in for it (SDD: authored configuration is CUE before it is code). The `store`
// category reports the bytes the store RETAINS against this ceiling; it never evicts a
// state-referenced entry to meet it, and a store its own referenced entries keep above
// the budget is reported as OVER budget, naming the referenced bytes as the reason.
//
// The budget is the plugin's own declaration surface rather than a project `defaults:`
// key because that key would have to be a field of the shared `#Defaults` in
// opencharly/spec — a cross-repo schema change, deliberately not guessed at here
// (opencharly/plugin-clean#28 carries that half). Until it lands, this value is the ONE
// place the ceiling is written down, and the Go that reads it derives it here.
store_budget: #StoreBudget
store_budget: {
// 32 GiB: a full multi-stage stack for one box family is a few GiB, so this holds
// several live families beside the referenced set while still bounding a host whose
// store grows by one artifact per rebuild (the 283 GiB / 172-entry store measured
// when D-STORE-7 was implemented had no ceiling at all).
bytes: 32 * 1024 * 1024 * 1024
}
// categories — the AUTHORED category set of `charly clean`. This list is the ONE
// declaration of what a category IS: its flag word, the one line a reader is told
// about it, whether a BARE invocation runs it, and what naming it also runs. The Go
// flag grammar is DERIVED from it (command.go: newCleanFlagSet registers one flag per
// row, and cleanCategories computes the run set from `bare`/`implies`), so a category
// cannot exist in the CLI and be missing here, or the reverse — a second, hand-written
// copy of this discipline would be the drift this declaration exists to end.
//
// The list is CLOSED: a category is added by adding a row, and a flag that is not a
// category (`--dry-run`, `--keep`, `--invalidate` — the MODIFIERS, which tune or target
// the categories rather than name one) stays in Go and is deliberately absent here.
//
// The two halves below are ONE field (CUE unifies them): the first is the row SHAPE, the
// second the value. `bare` is load-bearing for exactly one question — a category flag
// named on the command line SUPPRESSES the default set — and `implies` is that rule's
// other half: `--deep` also reaps build scopes, because a store-wide image purge on a
// host that has a hung build scope is the wrong half of the job.
categories: [...#CleanCategory]
categories: [
{
flag: "images"
contract: "Only image-tag retention: prune each box's image tags to the project's keep_images (the newest N distinct images, at most N tags of each), then the charly-labeled dangling-image sweep and the dead buildah staging sweep"
bare: true
},
{
flag: "check"
contract: "Only check-run retention: trim each .check/<bed> run dir to the project's keep_check_runs; NOTES.md is always preserved"
bare: true
},
{
flag: "deep"
contract: "Purge EVERY untagged/dangling image in local storage, not only the charly-labeled ones, and report UP TO the summed image size (layers shared with kept images reduce the actual reclaim, so pair it with --invalidate); never fires on a bare run"
bare: false
implies: ["scopes"]
},
{
flag: "cache"
contract: "Reclaim unreferenced blobs from every named charly cache — the content-addressed store's own GC — and sweep the retired cache roots; reports each store's live entries and the blobs/bytes reclaimed"
bare: false
},
{
flag: "scopes"
contract: "Reap HUNG transient build scopes — the systemd user scopes buildah/podman put a build container into. A scope is stopped only when it has been active for at least the reaper's age bound (named in the report) AND no buildah/podman builder is alive anywhere on the host; runs on a bare run and with --deep, and any other explicit category suppresses it"
bare: true
},
{
flag: "store"
contract: "Only the charly image store's ENTRY retention (D-STORE-7): keep the entries the host's charly state still references — the container engine's containers and the quadlet units — evict the rest, free the blobs no surviving entry reaches, and report the retained size against the store budget; it REFUSES to evict when a state surface cannot be read, because an unreadable surface is not an empty one. Never fires on a bare run"
bare: false
},
{
flag: "build-cache"
// ⚑ No BACKTICKS in a contract string: Go's flag package reads a backtick-quoted name
// as the flag's VALUE NAME, so a contract carrying one is printed as
// "-build-cache <cache root>/buildkitd" — a category flag that appears to take an
// argument it does not accept (measured on a live run). The paths below are named in
// prose instead.
contract: "Reclaim the builder's persistent cache — the BuildKit daemon's StateRoot() at <cache root>/buildkitd and every quarantined sibling (buildkitd.broken-*). The live root is removed only when no daemon holds its one-instance lock (a running daemon is named and the root is left alone), and a tree this user cannot delete whole is reported by name and left whole. Never fires on a bare run"
bare: false
},
]
}
// #CleanCategory is ONE row of `charly clean`'s category set (see #CleanPlugin.categories).
#CleanCategory: {
// flag — the CLI word: `--<flag>`. A USER-TERMS word (D-NAME-3): a tool's own name is
// never a category word, so the daemon whose cache `build-cache` reclaims is named in
// that row's `contract:` and in the report, never here.
flag: string & !=""
// contract — what the category reclaims, in one line. It IS the flag's help text.
contract: string & !=""
// bare — a plain `charly clean`, with no category flag named, runs this category.
bare: bool
// implies — the categories that ALSO run when this one is named. Every entry names a
// declared row (checked when the set is decoded, so a typo fails loudly).
implies?: [...string]
}
// #StoreBudget is D-STORE-7's size budget for the charly image store (see
// #CleanPlugin.store_budget). It is a DEF because the ceiling will grow clauses — what a
// sweep does when the store is over it is a policy question this row has to be able to
// answer later without re-shaping every reader.
#StoreBudget: {
// bytes — the ceiling, in bytes, of what the store may RETAIN after a sweep: the bytes
// the entries that survive still reach. A budget is a ceiling on the RETAINED set, never
// a licence to evict a referenced entry, so a store whose own referenced entries exceed
// it is reported (never "fixed" by deleting live state). Zero is rejected rather than
// read as "unlimited": a disabled budget and an unset one would be the same value, and
// the sweep could not tell "the operator set no ceiling" from "the ceiling is zero".
bytes: int & >0
}

See also the candy reference for this candy’s install surface.