Files
magpie/internal
yetone 49ebd63578 codex: the app's app-server is not ended under a turn still running
Norethcy on X (v0.1.4045): the Codex app (ChatGPT.app) went through a
routing group. The ChatGPT account ran out mid-conversation, and the
group's fallback answered. While that reply went on, magpie restarted
the app's app-server and cut the turn off.

magpie ends the app's app-server (codexAppStep, every 3s) once Codex's
route has changed. That happens here: the account is spent, so magpie
becomes Codex's provider. The end waits for the gateway to be idle for
10s, and for no turn open in a session file. The causes:

- sessionTurnOpen read only the last 512 KB of a rollout. A turn that
  wrote more than that counted as no turn at all: a tool's long output,
  images, an hour of work (one of the owner's own turns is 22 MB). A tool
  running 10s between two requests was then taken for the turn's end.
  The file is now read back in 512 KB chunks to the last task_started,
  task_complete or turn_aborted. Each chunk overlaps the next by a
  mark's length, so a mark across two reads is found whole.
- With the account spent, a turn open past 2m with quiet sessions
  counted as stuck on the spent account and was ended. A turn that went
  on through magpie after the route changed is served, not stuck. It is
  no longer ended for that (goneOn).
- busy was read before CodexBeforeAppEnd moved the open chats. A request
  that began meanwhile was ended under. It is read again just before the
  end.
- When the listing showed no new app-server after the end, the app was
  quit and reopened (Windows) even while it sent through magpie. Now it
  isn't reopened while a request is in flight or one began since the end.
- A magpie that took the gateway over (an update) saw none of the old
  magpie's streams, so its first look could end the app-server under one.
  Its start now counts as a Codex request just ended.

The crash relaunch (codexAppCrashStep) and RestartCodexAppWhenIdle use
the same turn check, so they wait for a long turn too. The buttons the
user presses restart at once, as before.

Tests (internal/provider/codex_app_inflight_test.go). All 6 fail with
the old codex_app_server.go swapped in, and pass now:
- TestCodexTurnOpenPastTheTail: turns of 512 KB+1, 1.5 MB and 22 MB,
  open and ended, with the app-server's start before and after the turn.
- TestCodexTurnOpenMarkAcrossReads: each mark straddling the read
  boundary at every byte.
- TestCodexAppServerWaitsForATurnTheFallbackServes: the reporter's case
  with the real session-file check. The route goes out while the
  fallback streams; a 600 KB turn has a 15s tool gap, then a tool running
  past 2m. The app-server is ended only once the turn ends.
- TestCodexAppServerEndLooksAtRequestsAgain,
  TestCodexAppNotReopenedWhileItSends, TestCodexAppStartCountsAsARequest.
Broken by hand: with goneOn forced false, the fallback test fails at
"a tool running over 2m0s in a turn the fallback served".

Cost: an open 22 MB turn takes 97 ms and 559 KB per look (benchmark on
mica). A look happens at most every 3s, and only while an app-server is
waiting to be ended, the gateway is idle and such a turn is open. An
ended or short turn is found in the last chunk, as before.

Run on mote:
- Full suite, go test -timeout 45m ./..., at this change on b3395ffda:
  every package ok (internal/gateway 969s, internal/provider 223s)
  except two. TestNoEagerPackageRegexps failed on
  internal/davsync/setuptext.go, which 48e394146 fixed on main.
  TestMainWindowCorners/macOS_15_SDK was killed at its 40s deadline
  at load ~29 (#1936, #2645). Log kept in the scratch folder.
- Rebased onto origin/main 36c5ee1e6 (three commits: davsync, claude,
  gui WebView2), then ran . ./internal/provider ./internal/davsync: ok.
  TestMainWindowCorners alone: ok (17s).
- gofmt -l: clean. GOOS=windows go vet ./...: ok. GOOS=linux go vet
  -tags nogui ./...: ok.

Not tried: the real ChatGPT app in a sandbox HOME, or a real account
running out. Real rollout files were checked for the marks
(task_started, task_complete, turn_aborted) and for turn sizes of 14 to
22 MB.
2026-11-13 20:12:28 +08:00
..
…
…
…
…
…
2026-11-11 08:14:28 +08:00
…
…
…