Skip to content

fix(cli): never fall back to another instance when the selected one is stopped - #78

Merged
tenequm merged 2 commits into
mainfrom
fix/stopped-instance-targeting
Sep 18, 2026
Merged

tenequm merged 2 commits into
mainfrom
fix/stopped-instance-targeting

Conversation

@tenequm

@tenequm tenequm commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

What was broken

On a stopped or absent named instance, resolveRunning kept the default ports (9222/6080) when discovery failed - docker port prints nothing for a stopped container - and no verb checked the selected container's state before reaching that endpoint. Every verb therefore landed on whatever instance owns the default ports, usually the operator's main cuttle:

  • status reported that other instance's CDP as if it were this one's,
  • open navigated the other instance's browser (observed in a stress run),
  • secret ls listed, and secret set would have stored a credential in, another session,
  • downloads, auth and grab took the same path.

Two adjacent up failures came from the same place: ensureHostPortsFree pre-flighted the default ports for a stopped container that docker start would rebind to its own, so the down hint cuttle --name X up failed with "host port 9222 already in use"; and up --recreate without ports tore the container down and only then failed on the default port, destroying an instance a command meant to rebuild.

What changed

  • discoverPorts (backend) falls back to the container's configured .HostConfig.PortBindings when docker port is silent, so a stopped container's ports - exactly what docker start rebinds - are discoverable. k8s and direct are not port discoverers and are untouched.
  • resolveRunning becomes resolveInstance: it reads the state once and returns it, then discovers ports only for a container that exists. It never keeps the defaults for one - unreadable ports are an error naming the instance. A lone --cdp-port/--vnc-port pins only that port; the other is still discovered (before, pinning one turned discovery off for both, so open could open another instance's viewer).
  • New resolveLive gates open, downloads, secret, auth and grab on the selected instance actually running, refusing by name with the exact resume command (cuttle --name X up). status reports a stopped instance honestly - state, image and log tail - and never probes its ports. errCDPNotAnswering now names the instance and its cuttle ... status.
  • up resolves the existing container's own ports, so a restart, an idempotent up and --recreate all use and report them; unreadable ports fail before the --recreate/--purge-profile teardown. --cdp-port/--vnc-port on a plain up of an existing container now warn and are ignored, like the other creation-fixed flags - only a rebuild can move them.
  • down drops to plain resolve - stopping needs no ports (the standing tunnel is keyed by context), so it still works when ports are unreadable.

Tests

TestVerbsRefuseAStoppedSelectedInstance (status/open/secret/downloads on a stopped instance, absent too - asserting the instance's own endpoint sees zero requests), TestUpRestartsAStoppedInstanceOnItsOwnPorts (with and without a stray --cdp-port), TestUpKeepsARunningInstancesPorts (plain + --recreate), TestUnreadablePortsFailBeforeTeardown (--recreate, --purge-profile, and down still working), and TestDiscoverPortsReadsAStoppedContainersBindings. All fail on main.

Live verification

Throwaway container fs-a on 9741/6741, --ephemeral, while the operator's cuttle held 9222. Pre-fix binary: --name fs-a status printed 127.0.0.1:9222 and secret ls listed the other session's secrets. Post-fix: every session verb refuses with "container 'fs-a': stopped - run cuttle --name fs-a up first"; up restarted it on 9741, plain up and up --recreate kept 9741; open, secret set/ls then worked against fs-a. Container and volume removed afterwards.

Release notes

Verbs on a stopped or absent named instance no longer fall back to the default ports and hit a different browser - they refuse by name with the command that resumes it. cuttle --name X up now restarts, reuses and recreates a container on the ports it was created with instead of failing on 9222.

Release-As: 0.15.0

…s stopped

On a stopped or absent named instance every verb silently kept the default
ports 9222/6080 and reached whatever instance owns them: `status` reported
that instance's CDP, `open` navigated its browser, and `secret set` handed it
a credential. Port discovery now reads a stopped container's own bindings, the
session verbs refuse anything but a running instance by name, and `up` starts,
restarts and recreates an existing container on the ports it was created with -
failing before a --recreate teardown when they cannot be read.
…r's ports

resolveInstance (was resolveRunning) now reads the state first and returns it,
so up, status and the session verbs stop re-reading it and skip discovery for an
absent container. A lone --cdp-port or --vnc-port no longer turns discovery off
for the other port, and a plain `up` of an existing container keeps its own
ports with a warning - only a rebuild can move them. `status` on a stopped
instance keeps its image and log-tail diagnostics while still never probing.
The restart test no longer calls t.* off the test goroutine.
@tenequm
tenequm merged commit a1df361 into main Sep 18, 2026
11 checks passed
@tenequm
tenequm deleted the fix/stopped-instance-targeting branch September 18, 2026 22:32
tenequm added a commit that referenced this pull request Sep 18, 2026
## What was broken

A lifecycle stress run (after #77/#78, which fixed its blockers) left
these:

1. **`pw --takeover --name X <verb>` hit the wrong instance.**
`--name`/`--context` were peeled off before `--takeover`, so instance
flags after `--takeover` went to the driver while the lease was
force-released on the *default* instance.
2. **Old images advertised a driver they lack.** Against a container
from an image that predates the bundled playwright-cli (0.14.2 and
earlier), the briefing advertised `pw` and `jev-browse`, and both failed
with a raw `OCI runtime exec failed ... executable file not found` (exit
127).
3. **`up --recreate` switched images silently.** Without `--image` it
moves the container to the CLI's default image and the briefing hid it.
4. **A driving verb right after `docker restart` failed** with a raw
`reaching the session lease: exit status 7` (daemon not listening yet)
or `exit status 127` (exec into the driver workdir while the daemon
recreates it).
5. **`open --until` ignored a dead browser**, blocking the whole
`--timeout` and then printing a stale URL.
6. Smaller: status said `up --recreate` "discards the profile" (it keeps
it); a repeated `down --purge` claimed "removed container"; `--cdp-port
0` and `--cdp-port X --vnc-port X` were accepted (and the latter left an
orphan volume); an invalid `--name` reached docker and was printed
unquoted in hints; a failed `docker run` always ran `docker rm -f
<name>`, even when it failed on a name conflict with a concurrent `up`
that won.
7. Deferred from #78: the ssh standing tunnel counted as healthy if its
pid lived and the local port accepted connections - which is also what
another process holding that port looks like. And the direct backend's
not-running hint said to run `up`, which direct refuses.

## What changed

- **pw flags:** `splitCuttleFlags` peels `--context`, `--name` and
`--takeover` in any order from the leading run; anything from the verb
on is still the driver's.
- **Driver detection:** a cheap in-container probe (`sh -c 'command -v
playwright-cli'`, run in `/`) decides whether the briefing shows the
driver block or a note naming `<cuttle> up --recreate`. `pw` recognizes
the exec's 127 "not found" and returns the same hint; `jev-browse`
probes before taking the lease. An exec that fails outright is not read
as a missing driver. The daemon has no version endpoint and older ones
never will, so the container itself is the only reliable source.
- **Image on recreate:** kept the upgrade semantics (`up --recreate`
without `--image` moves to this CLI's image - that is the upgrade path
the new hint relies on) and print `note: image changed <old> -> <new>`
when it changes. `SSH` gains `Image` so this works over ssh too.
- **Lease after restart:** `leaseCall` retries curl's exit 7 for up to
20s and execs in `/` instead of the driver's workdir. Only the lease
call changed in `lease.go`.
- **`open --until`:** a websocket failure (new `errCDPConn`) ends the
wait at once with the last URL. It deliberately does not re-dial:
`/json` relaunches a crashed browser, and its blank tab would satisfy a
`gone:` predicate and read as a finished sign-in.
- **up/down/status:** `checkPorts` before any docker call; docker's
container-name rule enforced in `resolve` for local/ssh; `checkPorts`
runs after port discovery, so `up --recreate --cdp-port <the viewer's
port>` is refused before teardown; failed runs skip cleanup on a name
conflict and remove the profile volume only when docker said it did not
exist beforehand (an inspect that errors counts as present); wording
fixes.
- **Tunnel identity:** the daemon's root now reports `hostname` (the
container's). The ssh tunnel health check compares it with the remote
container's `Config.Hostname`; a mismatch respawns the forward, and a
mismatch after respawn stops the supervisor and fails with the port. A
daemon that reports no hostname (older image, or not up yet) passes as
before. k8s is unchanged.
- **Direct hints:** `status`, `open` and `pw` on a direct context that
does not answer say to start that browser yourself; its briefing says
`cuttle pw` needs a container.

## Verification

Throwaway containers only (`lf-*`, ports 9710-9712/6710-6712), config
isolated via `XDG_*`:

- `pw --takeover --name lf-a press Tab` with a lease held on lf-a: lease
freed on lf-a, verb ran; the default instance's lease endpoint
untouched.
- `docker restart lf-a` then `pw goto`: main fails 3/3 (`reaching the
session lease: exit status 127`), this branch succeeds 3/3 in about 2s.
- `open --until title:never --timeout` with `docker kill` 6s in: main
waits the full timeout; this branch returns in 6s with `lost the page
while waiting; last at https://example.com/`. Success paths (`title:`, a
`url:` reached by navigation) still resolve.
- 0.14.2 container: main briefing advertises the driver and
`pw`/`jev-browse` fail with the OCI error; this branch shows the upgrade
note and `pw snapshot`, `pw click`, `jev-browse --mock` all fail with
the hint. `up --recreate --image cuttle:lf-fix` prints `image changed
ghcr.io/glim-sh/cuttle:0.14.2 -> cuttle:lf-fix`, the driver block comes
back, and the root endpoint's `hostname` matches `docker inspect
.Config.Hostname`.
- Bad ports, `--cdp-port 0` and `--name 'lf bad'` are refused before any
container or volume exists; a repeated `down --purge` says the instance
was already gone; four concurrent `up`s of one instance leave the winner
running (losers hit the name conflict and no longer remove it).
- A direct context that does not answer: main says run `up`, this branch
says start it yourself.

The ssh tunnel identity check is unit-tested
(`TestTunnelReachesInstance`); there was no ssh host to test it on.
`just check` is green.

## Release notes

`cuttle pw --takeover` now works in any order with `--name`/`--context`.
Against an image older than the bundled driver, the briefing and
`pw`/`jev-browse` say to run `up --recreate`. `up --recreate` names an
image change. Verbs right after a restart wait for the daemon.
tenequm added a commit that referenced this pull request Sep 21, 2026
🤖 I have created a release *beep* *boop*
---


## [0.15.0](v0.14.2...v0.15.0)
(2026-09-21)


### Features

* bundle playwright-cli into the image with a cuttle pw passthrough
([#67](#67))
([28d9846](28d9846))
* **cli:** center the skill and briefing on cuttle pw, drop host driver
routing ([#75](#75))
([33eca16](33eca16))
* **cli:** colored jev-browse output
([#70](#70))
([cf6f109](cf6f109))
* **cli:** cuttle jev-browse - autonomous browsing loop on the bundled
driver ([#66](#66))
([0496957](0496957))
* **cli:** deliver pw action snapshots as a host file, secrets masked
([#103](#103))
([78adbbb](78adbbb))
* **cli:** make --context/--name global so pw and jev-browse reach any
instance ([#74](#74))
([9df5c33](9df5c33))
* **cli:** zero-ceremony cuttle pw - auto-attach on demand, allow open
([a7e6491](a7e6491))
* **jev:** withhold write actions and halve jev-browse run time
([#102](#102))
([9ca9386](9ca9386))
* **serve:** mask password field values in /snapshot
([#109](#109))
([2a352b5](2a352b5))
* **serve:** session lease - one driver at a time, with takeover
([#69](#69))
([87724c8](87724c8))


### Bug Fixes

* **backend:** keep profile cleanup and remote argv from reaching past
their own ([#92](#92))
([f52cfd3](f52cfd3))
* **cli:** close the gaps a review of the lifecycle fixes found
([#90](#90))
([c46aa5e](c46aa5e))
* **cli:** close the lifecycle gaps a stress run left open
([#88](#88))
([899a535](899a535))
* **cli:** discarding the profile removes the instance's host snapshot
dir ([#113](#113))
([57cd07a](57cd07a))
* **cli:** downloads --wait accepts a finished download, logs drops
D-Bus spam ([#111](#111))
([dcc2f46](dcc2f46))
* **cli:** name the in-page dialog behind a pw click timeout
([#99](#99))
([11beeeb](11beeeb))
* **cli:** never fall back to another instance when the selected one is
stopped ([#78](#78))
([a1df361](a1df361))
* **cli:** re-attach the driver after a restart leaves a stale session
([#77](#77))
([bbeb0f0](bbeb0f0))
* **cli:** run --extract on every ending, not only done
([#71](#71))
([4954195](4954195))
* **jev:** judge done on the page that landed, and end done on a none
above even odds ([#108](#108))
([a9cb5a8](a9cb5a8))
* **jev:** offer filter-apply controls the write gate withheld
([#114](#114))
([bdf440a](bdf440a))
* **jev:** read quoted snapshot lines and keep field values out of
--extract ([#89](#89))
([09111a2](09111a2))
* **pw:** compact find output, downloads into a directory, quieter logs
([#116](#116))
([62f2ca8](62f2ca8))
* **pw:** snapshot links a host file like an action, and the console log
line names its verb
([#110](#110))
([c902c58](c902c58))
* **serve:** dismiss a native dialog the driver leaves open
([#87](#87))
([14f24cb](14f24cb))
* **serve:** keep the default seed's downloads across browser relaunch
([aac8e40](aac8e40))
* **serve:** keep the dialog watch alive through a signal shutdown
([#94](#94))
([4f4f1d0](4f4f1d0))
* **serve:** pool-mode reaping, capture tab, hung tabs and launch bursts
([#86](#86))
([3b42ea2](3b42ea2))
* **serve:** skip an idle reap whose timer a launch has since re-armed
([#93](#93))
([c667868](c667868))
* **smoke:** never exec into a container the run was not pointed at
([#79](#79))
([9075244](9075244))
* **viewer:** keep websocket inside proxy prefix
([#62](#62))
([3b10ba7](3b10ba7))


### Performance Improvements

* **cli:** run a pw verb in one docker exec, lease check included
([#100](#100))
([a9c3c90](a9c3c90))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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.

1 participant