Skip to content

feat(host): give back the space Apple sandboxes freed as they stop - #120

Merged
kalaspuff merged 3 commits into
mainfrom
fctl/63-can-you-fix-https-github-com-hyperstruct
Oct 20, 2026
Merged

kalaspuff merged 3 commits into
mainfrom
fctl/63-can-you-fix-https-github-com-hyperstruct

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

An Apple container sandbox's root filesystem is a sparse 512 GiB disk image on the Mac. It grows as the session writes, and it never shrank: the guest mounts it without discard, the runtime never trimmed it, and the container can't run fstrim without CAP_SYS_ADMIN. So a stopped sandbox held the most its session ever stored (one cargo xtask check --quick of fctl leaves 5.5 GB). Since #93, a host low on disk removes such sandboxes early, and those sessions resume without their conversation (#92).

What changed

Hosts trim Apple sandboxes as they stop them. Before the Apple driver stops a running sandbox, it runs:

  1. container exec <sandbox> sync, because ext4 trims only the blocks whose release its journal has committed.
  2. container clean <sandbox>, which is new in Apple's container 1.4 (apple/container#1949). The VM's init, vminitd, enters the container's mount namespace and runs FITRIM on /, so the Mac gets back the space of the files the session deleted.
  3. container stop, as before.

That covers every stop of a sandbox the host keeps: the end of a session's run (idle stops included), a stop before the sandbox started, and leftover sandboxes the control plane asks to stop.

Best effort. When clean fails, the sandbox stops as before and the host logs, for example, fctl-ses-… keeps the disk space of the files deleted in it: container clean failed: Error: Plugin 'container-clean' not found. (what container before 1.4 says). When the sync doesn't finish within a minute (a VM that doesn't answer), the host skips the trim.

Docs. docs/architecture.md (Sandboxes and drivers) and docs/cli.md (Hosts) say what a stopped Apple sandbox keeps on the Mac. The new ADR 0025 records the decision.

The decision #92 left to a person

#92 asked whether to give Apple sandboxes CAP_SYS_ADMIN so that fctl-agent could run fstrim. This takes an alternative the issue named instead, trimming the image from the Mac, which Apple's runtime now offers. Sandboxes get no new capability, nothing in the sandbox decides when the trim happens, and fctl-agent doesn't change. The person who started the session chose trimming as sandboxes stop over trimming after each turn: idle sessions stop ten minutes after their last turn, and stopped sandboxes are what a host keeps for days. ADR 0025 has the reasons, and why not discard.

Compatibility

Only the host changes: no protocol, control plane or sandbox image change.

  • Hosts with Apple's container before 1.4 stop sandboxes as before, and log it at each stop. Sandboxes made by container 1.0 or earlier have an init without the trim, and keep their space too.
  • Hosts of earlier fctl versions never trim.
  • The Docker, Lima and process drivers don't change.

Verification

  • A unit test with a stand-in CLI (apple_sandboxes_give_back_the_space_of_deleted_files_as_they_stop):

    • An Apple sandbox gets inspect, exec … sync, clean and stop -t 20, in that order.
    • A runtime without clean still stops it.
    • Docker gets neither sync nor clean.
    • The test fails with the trim turned off.
  • Checks: cargo xtask check --quick passes.

  • In this session's sandbox, which is an Apple container sandbox on the hyperstructure Mac:

    • lsblk -D: the root disk vdb takes discards, with a granularity of 512 B and up to 513G per request.
    • /proc/self/status: CapBnd: 00000000a80425fb, without CAP_SYS_ADMIN.
    • /.cz-init, the init binary the runtime puts in the container, was built on 2026-09-24 and has the trim messages (FilesystemOperation, FiTrimParams). containerization 0.47.0 is from that day, and container 1.5.0 ships it, so the Mac most likely has clean already.
  • Apple's sources and tests at apple/container@ca4884f and apple/containerization@836080d:

    • clean in the runtime service, and filesystemOperation in vminitd.
    • TestCLIClean, which Apple's CI runs on Macs with macOS 26. It writes 10 MiB in a container, deletes it, runs sync and clean, and checks that the container's rootfs.ext4 on the Mac lost at least 80% of what the write allocated.
  • On a Mac, the check Stopped Apple sandboxes keep the space their sessions freed #92 asked for, by the person who started the session:

    container run -d --name trim-check alpine:3.20 sleep 3600
    container exec trim-check sh -c 'dd if=/dev/urandom of=/big bs=1M count=1024 && sync'
    du -k "$img"     # 2190692  …/containers/trim-check/rootfs.ext4
    container exec trim-check sh -c 'rm /big && sync'
    container clean trim-check
    du -k "$img"     # 1109288  …/containers/trim-check/rootfs.ext4
    container delete --force trim-check
    

    The image's allocated size fell by 1,081,404 KiB (1.03 GiB) once the 1 GiB file was deleted and trimmed.

Not run: a host with this change on a Mac. The unit test checks that the Apple driver runs those same commands.

Not done

Running sandboxes keep the space of what their sessions deleted until they stop. A host low on disk could trim them before it removes stopped sandboxes. #92 is about stopped sandboxes, so this pull request doesn't do that.

Fixes #92

Session: https://app.fctl.sh/s/ses_01M5C8QXR7X7WV5F31111V8KE0

An Apple container sandbox's root filesystem is a sparse disk image that
only shrinks where the guest discards blocks, and nothing discarded them:
the guest mounts it without discard and the container has no
CAP_SYS_ADMIN for fstrim. So a stopped sandbox held the most its session
ever stored.

Before the host stops a running Apple sandbox, it now runs `sync` in it
and then `container clean` (Apple's container 1.4 and later), whose init
in the VM trims the root filesystem. The container gets no new
capability. A runtime without `clean` stops the sandbox as before, and
the host log says the sandbox keeps the space.

ADR 0025 records the decision; docs/architecture.md and docs/cli.md say
what a stopped Apple sandbox keeps on the Mac.

Refs #92
fctl-sh Bot added 2 commits October 20, 2026 09:29
…heir files

The check on a Mac showed the space of a deleted file coming back, but
the image still holds the filesystem's own blocks: Apple's unpacker gives
each root filesystem a journal, 1 GiB for a 512 GiB filesystem. So the
docs no longer say that a stopped sandbox takes what its files take.

Refs #92
@kalaspuff
kalaspuff merged commit 29d1a88 into main Oct 20, 2026
5 checks passed
@kalaspuff
kalaspuff deleted the fctl/63-can-you-fix-https-github-com-hyperstruct branch October 20, 2026 09:49
kalaspuff pushed a commit that referenced this pull request Oct 20, 2026
)

Sessions showed one pull request: the one fctl-agent reported last, the
primary repository's first, as `#120` with no repository. A session that
opened pull requests in several of its project's repositories, or a
second one after the first was merged, listed only one of them and
didn't say where it was.

## What changed

- **The control plane keeps every pull request a session's agent
finds.** After each turn fctl-agent reports the pull request of the
branch each repository has checked out, so it only ever reports the
latest one per repository. SessionDO now adds the new ones to the
session's list, in the order they come, and takes the newer title and
state of the ones it has (`fctl_domain::pulls`, at most 20). Each
carries the repository it was opened in: the one its URL names, or else
the repository whose state it came in. The list is written to D1 in a
new `sessions.pull_requests` column (migration 0014) and is part of the
account export.
- **Protocol.** `Session.pull_requests` and `PullRequestRef.repo` are
new and optional; `Session.pull_request` stays for clients a version
behind. Agents and hosts don't change. Sessions from before this get
their one pull request in the list, with its repository, when they are
read, from D1 or from their SessionDO.
- **Web app.** The session header links every pull request as
`repository#number` (`fctl#120`, `design#14`), with its title on hover;
they wrap together as one list. The rail has a **Pull requests** section
with each one's title and `owner/name#number`, in place of the card
under **Changes**. The floor, a workspace's page and a project's page
say `PRs fctl#120, design#14`.
- **The floor stays current.** SessionDO told the dashboards about a
session when its status changed, and a turn's git report arrives after
the turn ends. So a new pull request, and the +/− counts, reached the
floor's rows only after a reload or the next turn. A git report that
changes what the lists show now goes to the dashboards too.
- **CLI.** `fctl show` prints each pull request with its repository
(below); `--json` has the list.
- **Docs.** `getting-started.md` and `cli.md` say so. The privacy
policy's "branch and pull request" becomes "pull requests" in
hyperstructure/fctl-site#28.

```
$ fctl show playground#3
playground#3 Name every pull request a session opens
…
prs:      fctl#121 #121
          design#15 hyperstructure/design#15
          fctl-site#32 hyperstructure/fctl-site#32
```

## Verification

- Unit tests for `fctl_domain::pulls` (repositories from URLs and from
repository states, merging over turns, the limit, sessions from before
the list), for `fctl show`'s lines (also from a control plane before the
list) and for the web app's helpers. `cargo xtask check` passes.
- End to end with `cargo xtask dev`. The dev instance has no GitHub, so
the dev agents got a stand-in `gh` that answers `gh pr view` with a pull
request in another repository after each turn, and the project a setup
script that pushes each commit to a local remote, so fctl-agent sees its
branch pushed and looks the pull request up.
- On `main`, a session whose agent found fctl#120, design#14 and
fctl-site#31 showed only `#31`.
- I restarted the stack on this branch with the same data, and migration
0014 applied. The sessions `main` made read with their one pull request
in the list, with its repository. Another turn in one of them added
sandbox#7 after fctl-site#31.
- A new session's agent found fctl#121, design#15 and fctl-site#32: D1,
the API, `fctl show` and the web app list all three. With the session
page and the floor open, the pull request a further turn found showed up
on both without a reload.

## Screenshots

At b04cf34, with the dev stack's data. Both columns show the same
sessions, read from this branch's control plane: on the left `main`'s
web app, on the right this branch's. Playground #3's agent found a pull
request in four repositories.

| Before | After |
|---|---|
| ![Before: the session page, dark: the header shows #8, the rail one
pull
request](https://app.fctl.sh/api/v1/published/D113R6f8QYfhpPbDsQWQBqaaeThsuZZ7L0RdkPoAJkJ/before-session-dark.png)
| ![After: the session page, dark: the header shows fctl#121, design#15,
fctl-site#32 and sandbox#8, the rail lists all four with their
titles](https://app.fctl.sh/api/v1/published/hEwHrUiZXk7zFxUG9iZ7Y53SBjoB878mQF3hFb07YsB/after-session-dark.png)
|
| ![Before: the session page,
light](https://app.fctl.sh/api/v1/published/LckXlfcezPClddzNC6B2OUWvHZChEVCqtrdRdgNgX0d/before-session-light.png)
| ![After: the session page,
light](https://app.fctl.sh/api/v1/published/ccRFWprX5eggRAMX2lN2GcXGOr3X4GBO80RZbBv695n/after-session-light.png)
|
| ![Before: the floor, light: each row says PR and a
number](https://app.fctl.sh/api/v1/published/SIV2JOxyppoBtHGBMP8P4x0x5bYinwN8MapwzQHvbvf/before-floor-light.png)
| ![After: the floor, light: each row names every pull request with its
repository](https://app.fctl.sh/api/v1/published/y7aCSgrM7AgSEFQs2G3G6CdENU9bUbwejAfJ38v8EPn/after-floor-light.png)
|
| ![Before: a project's sessions,
dark](https://app.fctl.sh/api/v1/published/eq2u9b9zYTAlQQqcG4JoDK0Xc8u7g2uvJTMXzA8YNig/before-project-dark.png)
| ![After: a project's sessions,
dark](https://app.fctl.sh/api/v1/published/rLWM8Sw691S2ApXBgxjEqycNJCtHc8kk9HAtV1bcXgd/after-project-dark.png)
|
| ![Before: the session page on a phone,
dark](https://app.fctl.sh/api/v1/published/VOW8yJhpGqiKJMtHMk1Q344QHS1PQeIYVu2VI1TcszD/before-session-dark-390.png)
| ![After: the session page on a phone, dark: the pull requests wrap
below the
branch](https://app.fctl.sh/api/v1/published/mrkQpRYwa6xS1VnMedhkYL3Wno5OHd9dPmEIZmgAsCb/after-session-dark-390.png)
|

Made in fctl session
https://app.fctl.sh/s/ses_01M5CBQV867BG6ARZKJHZPGVY0

---------

Co-authored-by: fctl-sh[bot] <fctl-sh[bot]@users.noreply.github.com>
Co-authored-by: fctl-sh[bot] <343971779+fctl-sh[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Stopped Apple sandboxes keep the space their sessions freed

1 participant