Skip to content

feat(cookbook): add container-overlays recipe - #855

Merged
umputun merged 2 commits into
umputun:masterfrom
Rulexec:cookbook-container-overlays
Nov 1, 2026
Merged

umputun merged 2 commits into
umputun:masterfrom
Rulexec:cookbook-container-overlays

Conversation

@Rulexec

@Rulexec Rulexec commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

recipe from #714: use agterm overlays from inside a docker container, with the program running in the container and files opened by their Mac paths. the discussion asked for a host tunnel so overlay commands execute where they belong; this is the extension-model answer — an in-container agtermctl wrapper plus a small Mac-side receiver, no core changes.

what works from inside the container after setup:

  • agtermctl session overlay open -- htop runs htop in the container: the wrapper replaces COMMAND with docker exec -it … <name> sh -c "$(printf %s <b64> | base64 -d)", the payload being cd '<caller cwd>' && <COMMAND>, base64-armored so the double shell evaluation can never mangle it. --block returns the program's real exit code.
  • --html FILE opens by its Mac path through a bind map (bind-map.py, from docker inspect); a file outside any bind is staged into a bind-mounted pages dir (capped at 200 files / 20 MiB), and a --cwd grant is always synthesized — without it agterm loads the page via loadHTMLString at about:blank with zero file access, so relative assets silently vanish. overlay reload re-copies staged entries, then passes through.
  • --url http://localhost:PORT/ works because publishing already happened at docker run -p: the identity expose script just answers the wrapper's contract line, and an optional port-map file translates remapped ports.
  • every other call passes through to the receiver verbatim. only the five session-identity env names cross the wire, so a caller cannot retarget the receiver at another instance; the wrapper also injects --target from AGTERM_SESSION_ID, since agtermctl defaults to the active session and an overlay from a container would land on whatever pane is focused.

the parser knows 0.39.0's closed flag surface; anything it does not recognize passes through unrewritten rather than guessed at. the exec-back line uses an absolute docker path (the app, not your shell, spawns it) overridable with AGT_OVL_EXEC_TEMPLATE for non-standard docker locations.

files: agtermctl.py (in-container wrapper, python 3 stdlib only), agtermctl-receiver.py (runs the real agtermctl and streams out/err/rc over one connection per call, binds 127.0.0.1:59998), bind-map.py, agt-ovl-expose.sh, test_shim.py — cookbook CI executes the shim, which runs the wrapper's 12 embedded tests.

the receiver runs any agtermctl call that reaches it with your terminal's own authority; the README's Limits spells that out — 127.0.0.1 keeps other machines out but nobody else on this Mac, so run it on a single-user machine and stop it when done.

needs agterm 0.39.0 (the flag surface the parser knows), docker with host.docker.internal, python 3.8+ on both sides, and sh/printf/base64 in the image; the optional xterm-ghostty terminfo entry buys full-color TUIs.

checked: test_shim.py and shellcheck clean; verified end to end against a live instance — alpine image with the wrapper, htop and the compiled terminfo entry, docker run from an agterm shell carrying the session identity, then overlay open -- htop and --size-percent 80 from inside the container, with the overlay read-back and the receiver log confirming the rewritten call.

@Rulexec
Rulexec requested a review from umputun as a code owner October 31, 2026 22:59

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one blocking issue, the rest are small notes.

blocking

cookbook/container-overlays/agtermctl.py:216 - parse_open stops reading flags at the first positional, so overlay open htop --block or overlay open "make test" --target "$AGTERM_SESSION_ID" passes through unrewritten, so the command runs on the Mac in the target session's Mac cwd; without --target, that is whichever session is focused. The real CLI accepts flags after COMMAND (agtermCore/Tests/agtermctlKitTests/CommandsTests.swift:929) and the bundled agent skill uses that order (plugins/agterm/skills/agterm/examples.md). Fix: keep scanning known flags after COMMAND, and die() instead of passing through when an open call fails to parse and carries no --html/--url; a selftest with trailing --block and --size-percent would pin it.

smaller

  • README.md:124 - "a caller cannot retarget it at another agterm instance" doesn't hold: --socket goes through in argv, and an overlay COMMAND sent straight to the receiver runs any shell command on the Mac. Pls say it plainly in Limits: any container or local process that reaches the receiver can run any command on the Mac as you. Same for the receiver docstring and the FORWARDED_ENV comment.
  • agtermctl.py:342 - gc_staging removes every non-hidden dir under the staging root older than 24h, not only manifest entries. Prune only manifest entries. Also, opening another staged page can delete a still-open copy whose dir mtime is over 24h old and drop its manifest entry, so a later overlay reload can't restore it; mention that 24h deletion in Limits.
  • agtermctl.py:609 - URL opens that are not rewritten (a public URL, or AGT_OVL_EXPOSE="") return before inject_target(), so the page covers the focused session, contrary to README:110. Inject the target on every parsed URL open.
  • agtermctl-receiver.py:123 - the receiver ignores AGTERMCTL, which cookbook/CONTRIBUTING.md asks scripts to honor. Resolve --agtermctl, then AGTERMCTL, then PATH.
  • agtermctl.py:561 - with COMMAND, --cwd is ignored for where the program runs (the payload always cd's to os.getcwd()), while the container path still reaches the Mac as the overlay's cwd. Use the absolutized --cwd for the payload and drop it from argv.
  • agtermctl.py:493 - frames go to sys.stdout.buffer/sys.stderr.buffer without a flush, so agtermctl events inside the container shows nothing until the buffer fills or the call ends. Flush after each frame.

@umputun

umputun commented Nov 1, 2026

Copy link
Copy Markdown
Owner

one more, for the receiver, on top of the review above.

agtermctl-receiver.py:95-111 - the receiver never notices the container side going away. Ctrl-C on agtermctl events inside the container closes the connection, but the handler keeps waiting in proc.wait(): a quiet child is never touched, and a child that writes again only kills its pump thread on the failed send. Nothing terminates or reaps the child, and stopping the receiver doesn't either, so every aborted events leaves an agtermctl events process on the Mac polling the socket. Pls watch the client connection for EOF and terminate and reap the child when it closes.

minor, not blocking.

The parser now reads flags before AND after COMMAND, as the host CLI does;
an unparsable open without --html/--url dies instead of passing through,
since sent as-is it would run its COMMAND on the Mac. A parsed COMMAND with
a page flag dies locally too, and --cwd folds into the exec payload.

COMMAND no longer crosses as text: the wrapper sends a structured exec
request and the receiver builds the only docker-exec line it will pass,
from its own template (--exec-template / AGT_OVL_EXEC_TEMPLATE, moved to
the Mac where docker lives). The receiver resolves --agtermctl, then
AGTERMCTL, then PATH; refuses --socket anywhere and raw COMMAND overlay
opens, always with an err frame; watches client EOF with select + MSG_PEEK
(sends stay blocking, so a slow client gets TCP backpressure, never a
killed child) and terminates and reaps the abandoned call; a child registry
plus SIGTERM cleanup ends live children at shutdown.

Wrapper: --target injected on every parsed URL open, output flushed per
frame so events streams live, staging GC prunes manifest entries only.
Receiver gains an embedded suite; test_shim runs both (30 tests). README
states the hardening and keeps the blunt residual Limits sentence first.
@Rulexec

Rulexec commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

all in 669e120.

blocking

  • agtermctl.py — parse_open now scans known flags before AND after COMMAND, both orders the real CLI accepts; an open that fails to parse and carries no --html/--url dies on the spot instead of passing a Mac-executing call through, and a parsed COMMAND with --html/--url dies locally with the host's own wording (it can never succeed, and sent on it would read as a raw COMMAND at the receiver). test_parse_open_forms pins trailing --block, trailing --size-percent 60 and --target after COMMAND; test_unparsable_open and test_contradictory_command_with_page_dies pin the die rules.

smaller

  • README.md — Limits opens with the plain sentence: anyone who reaches the receiver drives your terminal as you. What the hardening below took off the table follows it. The receiver docstring and the FORWARDED_ENV comment say the same two-sided thing.
  • gc_staging — prunes manifest entries only: a missing dir drops its entry, an old one goes with its entry, anything the manifest does not list is never touched. Limits states the 24h window: a staged page nobody reloads for over 24h can be deleted by the next staged open, so a later overlay reload cannot restore it; a reload refreshes the age.
  • URL opens — --target is injected on every parsed URL open, rewritten or not, and the substitution runs before the injection splice. That ordering mattered: my first version spliced first and the rewritten URL overwrote the literal --url token, which the live run surfaced as no such session: <url>. test_url_target_injection_all_paths pins the full argv of the rewritten path.
  • the receiver resolves --agtermctl, then $AGTERMCTL, then PATH.
  • --cwd — absolutized and baked into the payload's cd; the tokens no longer cross, and the receiver refuses --cwd in exec flags outright. test_op_exec_request pins the payload and the sliced flags.
  • reply frames are written through write+flush each, so agtermctl events inside the container streams live.

the follow-up

The receiver watches the client connection with select + MSG_PEEK and the socket stays fully blocking for the pumps; on EOF the handler terminates the child, escalates to kill() after a 5s grace and reaps it, and a child registry plus a SIGTERM handler end live children when the receiver stops. Deliberately no settimeout — it bounds writes as well as reads, and a slow-but-open client would get a healthy child killed; test_slow_reader_gets_backpressure_not_kill pins 8 MiB to a stalled reader ending in a clean rc frame, test_client_disconnect_reaps_child pins the reap.

beyond the review, deliberate

Pass-through argv made the receiver a full-authority remote, so I made the two edges you named structural refusals instead of doc caveats: the receiver refuses --socket in any request (its own environment decides which instance it drives, making the README claim true), and refuses a raw COMMAND on session overlay open. A COMMAND now crosses only as a structured exec request — container, base64 payload, open flags — and the receiver builds the one docker-exec line it will ever pass from its own template; AGT_OVL_EXEC_TEMPLATE moved to the Mac side, where docker actually lives. Wrapper and receiver update together; an old wrapper gets a refusal saying so, and the wrapper recognizes a reason-less rc 2 and prints an update hint. The receiver still only ever starts its one resolved agtermctl binary — true before, stated in the docstring now. Your Limits sentence stays first because it stays true: session new --command, session type and friends still pass.

verification

test_shim.py runs both embedded suites now (30 tests), ruff is clean under the CI pin (0.16.10, --ignore EXE001), shellcheck clean. Live, from a real container through the receiver driving a live agterm instance: --block returned the program's real exit code with a marker file crossing the bind; overlay open top --size-percent 60 (flags after COMMAND) read back overlaySizePercent: 60 on the caller's session; --cwd ran the payload where asked; a staged page opened and overlay reload re-copied it; a public URL and a published-port URL both opened on the caller's session; Ctrl-C on container-side agtermctl events left zero receiver-spawned processes on the Mac; handcrafted raw-COMMAND and --socket requests answered rc 2 with the messages; $AGTERMCTL and the template override were exercised live.


I hope this is not too overcomplicated now. My agent tested that he can brew a docker image and run overlays with command/html/url.

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, last round's items check out. Three small notes for a follow-up, none blocking:

  • agtermctl-receiver.py:250 - CONTAINER_NAME_RE.match accepts a trailing newline ($ matches before a final \n), so a hand-crafted exec request with container alpine\n splits the docker line under agterm's eval, and the decoded payload runs on the Mac. The wrapper strips the name, so normal use never hits it. Fix: fullmatch, plus a "dev\n" case in the refusal test.
  • README.md:129 and agtermctl-receiver.py:20,23-25 - "no caller-written shell line reaches the Mac" reads as absolute, while session new --command still passes, as the bold line above it says. Pls scope both to overlay COMMANDs.
  • agtermctl-receiver.py:413 - the child inherits the receiver's stdin and no stdin crosses the wire, so piped container input (printf 'a\nb\n' | agtermctl pick) never reaches agtermctl pick; the child reads the Mac receiver's stdin and can hang when it inherits a terminal. Fix: stdin=subprocess.DEVNULL on the Popen, and a Limits line that stdin does not cross.

@umputun
umputun merged commit 250b45c into umputun:master Nov 1, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants