Commit Graph
7 Commits
Author SHA1 Message Date
yetone e4197144a8 perf: every package's package-level regexps compile on first use
v0.1.3516 made gateway's and redact's patterns lazy. The other 299
package-level regexp.MustCompile calls, in 148 files across agent,
provider, gui, library, codexcat, sessions, usage, plugin and the rest,
now go through lazyre.New too, so a launch that never reaches them
(`magpie statusline`, run by Claude Code on every message) doesn't
compile them. Fields, maps and params that held them are *lazyre.RE.

Measured on mote (M5 Pro), sandbox HOME, v0.1.3516-equivalent build
(t42) against this one (t43):
- GODEBUG=inittrace=1 sum: 7.39 MB / 69,837 allocs -> 4.62 MB / 46,765
- `magpie statusline`, 3 runs each: instructions retired ~212M -> ~176M,
  peak memory footprint 19.2-19.4 MB -> 16.8-17.0 MB

Run: go vet ./... (darwin) ok; GOOS=windows go vet on each of the 30
changed packages ok; GOOS=linux go vet ok except ., internal/cli and
internal/gui, which stop in wails v3's cgo files when cross-compiled
(the same at main). New TestAllLazyPatternsCompile at the root compiles
all 574 registered patterns (ok), so a bad pattern fails a test rather
than a first use.
Not run before push: the changed packages' own test suites; they run
on mote after it.
2026-11-10 12:54:39 +08:00
yetone 6712e367e8 agent: a look at the stash shares one read of it
Each /api/state and /api/drift poll asked the stash (stash.json) for one
value hundreds of times, through Detected, Drift and the agents' wiring
checks, and each ask read the file and parsed it again. In a real-account
heap profile (20 rounds of drift, state and providers) stashLoad was
24-34 MB of what was allocated.

stashShared reads it through filememo.Read, so every look shares one
parse until the file changes, by magpie or by another process. Every
read-only caller uses it now, the 24 files here and QwenPaw's
unstashPeek. stashLoad, which callers change and write back, still gives
each its own map, cloned from the same parse.

Real use: the owner's real-account sandbox, offline (proxy to :9),
20 warm rounds of /api/drift, /api/state and /api/providers:
- allocated 477 MB (d307aa648) -> 411 MB; stashLoad is out of the
  profile.
- /api/state and /api/drift bytes identical to d307aa648's (cmp).

Run on mote (mote-run, Go 1.26.8), at this head rebased on d307aa648:
- gofmt clean; go vet ok, also GOOS=windows and GOOS=linux.
- TestStashLooksShareOneRead alone, 3 times: ok.
- Broken by hand (stashShared reading through stashLoad): red, "a look
  at the stash read and parsed it again".
- go test -timeout 45m ./internal/agent ./internal/gateway: ok (263s,
  691s).
2026-11-01 05:32:29 +08:00
yetone 2597c96365 agent: Codex's config.toml is decoded once a read for a sync's own looks
A sync to Codex decoded the whole config.toml again for each thing it
checked: codexMultiAgentV2On, putThreadTables, codexEnableEfforts and
codexAsyncNow each ran toml.Unmarshal over the file. Codex writes a
[projects."…"] table for every folder it was opened in; the owner's
config is 1.5 MB with 10,932 tables, and with 10,000 such tables one
sync allocated 21 MB.

codexTopOf decodes only the top tables these look at (features,
model_providers, desktop, profile, profiles) and keeps the result in
filememo by the file's mod time and size, so a sync and the polls
after it share one decode until the file changes. A missing file is
an empty config, as before.

TestCodexSyncDecodesTheConfigOnce: 10,000 project tables, one sync
to settle, then five more; each must allocate under 12x the file and
leave config.toml unwritten. Now ~5 MB a sync. Red on the old code:
51,257 KB a sync over an 868 KB file, past the 10,421 KB bound.

Run at this head on mote: gofmt clean; go vet (darwin, GOOS=windows,
GOOS=linux) ok; go test -timeout 45m ./internal/agent
./internal/gateway ok.
2026-11-01 04:06:09 +08:00
yetone 5d955a4d4b tune: Codex can take the compaction advice in the background, its own limit kept (herman_hong)
herman_hong on Discord: Tuned for you advises Codex compact at ~120K.
A foreground compaction kept them waiting, so they had set 384K by hand.
Now that Codex compacts in the background, they want the advice there
and the 384K kept as the backstop.

Codex has this since 0.163.0 (checked in codex-rs at rust-v0.166.0, not
in 0.162.1): [features.async_compaction] enabled and
model_async_compact_threshold_percent, an integer share (1 to 100) of
the model's window, 65 by default. It is Stage::UnderDevelopment, runs
only with remote compaction v2 (which magpie already sets on its
provider), and starts none once the foreground limit is reached. magpie
already writes it from Settings' Background compaction (#2490), with
the version gate and the user's own keys kept. This drives that setting
from tune:

- agent: knob TuneAsync ("async_compact") on codex. TuneRead gives the
  share, TuneApply turns Background compaction on at it (stashing what
  Settings had the first time), and TuneUndo puts that back unless the
  user changed it since. The foreground limit (Long conversations, or
  the user's model_auto_compact_token_limit) is never touched. Async
  keys the user wrote in config.toml come first (codexAsyncOwn): the
  row says so and Apply errors. A table magpie wrote itself doesn't
  count as the user's.
- gui/tune.go: advice.async holds the advice as a share of the window,
  rounded down. With timed compactions it uses the token-cheapest
  window, since a background compaction keeps no one waiting. It is
  offered only when it would start before the foreground limit (the
  lesser of the limit now and 90% of the window). held lists Codexes
  too old for a share.
- context.js: the row gets a text button, "Use 120K in the background",
  whose tip gives the share and says the foreground limit stays. After
  Use a chip says so, with Undo background. A config.toml of the
  user's, or a Codex too old, gets a one-line caveat and no button.
  The too-old line is shared with Settings' row (codexHeld). No select,
  no layout change. zh, zh-TW, ja and de are in i18n.js.
- The core script budget goes up by 1,571 bytes for the row
  (core_budget_test.go says why). app.js is 278 bytes smaller.

Run (at this change on origin/main 6893cfbe3; rebased on d0a61b856,
whose new commits touch none of these files, and TestCoreScriptBudget,
TestTune*, TestCodexAsync* run again there: ok):
- gofmt -l: clean.
- mote-run go vet ./internal/agent ./internal/gui, plus GOOS=linux
  -tags nogui and GOOS=windows vet of both: ok.
- mote-run go test -count=1 -timeout 45m ./internal/agent
  ./internal/tune ./internal/provider: ok. ./internal/gui: the new
  tests and TestCoreScriptBudget pass. TestSettingsSaveKeepsWhatItDoes
  NotSend is red on clean origin/main too (#4514). Eleven AppKit window
  tests (TestMainWindowCorners, TestPanelCloseHidesIt, ...) were killed
  at their 30-40s on mote at load ~25; this change touches no window
  code.
- wsl-run node --test context-tune, codex-async-compact and i18n-keys:
  the new test passes in chromium and webkit, in all five languages,
  at 360 and 1100. It checks the label, the tip, the POST body
  {knob: async_compact, value: "30"}, the chip and Undo, the locked
  and held caveats, and no overflow. i18n-keys' "a plugin's words are
  its own" is red on main (#4524).

Broken by hand (each went red, then restored):
- codexAsyncOwn always "": TestTuneCodexAsyncOwn red in all 4 cases.
- asyncOurs ignoring the set share: TestTuneCodexAsync red (it would
  undo over the user's 50).
- codexAsyncAt keyed on WaitBest, and always ok: TestCodexAsyncAt red
  (timed cheapest, foreground, 90%).
- context.js posting without the knob: the GUI test red, knob
  "compact" where "async_compact" was wanted.

Not tried: a real Codex 0.163+ turn compacting in the background
through magpie, and a real account's /api/tune. When the models' windows
differ, the share comes from the advice's window, so the tokens are
approximate; the tip shows the share Codex gets.
2026-10-31 17:12:16 +08:00
yetone 41357f3935 codex: the ChatGPT desktop app on Linux is found, and a switch reaches it
pota on Discord: magpie switches the ChatGPT (Codex) desktop app's
account on macOS and Windows but not on Linux. OpenAI ships the app for
Linux since 08-11 (preview; learn.chatgpt.com/docs/linux/linux-app): a
deb, an rpm and an Arch package, which install /usr/lib/chatgpt. Its
app-server is /usr/lib/chatgpt/resources/codex ... app-server, run with
the user's HOME and no CODEX_HOME, so it reads ~/.codex like the CLI.
isCodexAppServer took only .app/Contents/ and Windows paths, so on Linux
nothing found the app: no "still signed in as" notice after a switch,
no restart of its app-server, and no version on the Agents row.

- provider: linuxCodexApp takes an absolute .../chatgpt/resources/codex
  as the app's own app-server (findCodexApp, CodexAppStale, the switch's
  restart all read it).
- agent: restartOnLinux; codexAppOpen knows on Linux whether the app is
  open (any /usr/lib/chatgpt/ChatGPT process, not its codex alone or
  another program's ChatGPT). desktopApps["codex"].linux reads the
  version from resources/linux-package-metadata.json, HasApp/AppVersion
  see it, and codexReaders counts the bundled codex's --version as a
  reader of Codex's config. version() no longer stops at an empty store
  read before the other sources.

Real run, WSL magpie-desktop (Ubuntu 24.04), OpenAI's chatgpt_amd64.deb
26.1027.30824 (bundled codex-cli 0.166.0-alpha.5.1) under Xvfb, sandbox
HOME, fake unsigned JWT accounts alice/bob (no real credentials), magpie
web on 127.0.0.1:3591:
- POST /api/login/switch alice: 200, auth.json alice; /api/providers
  then says codexApp "bob@example.com" (the stale notice).
- POST /api/codex/daemon/restart-app: 200 in ~3s; the old app-server
  ended (app log "Codex CLI process exited classifiedAsExpected=true
  code=0"), a new one started with the sandbox HOME, the notice cleared.
- /api/agents/cli: "codex":{"app":"26.1027.30824"}.
- The same run with linuxCodexApp reverted: no notice, restart-app 400,
  the app-server untouched (pota's case).

Run at 5b1709eee: go test -tags nogui -timeout 45m ./internal/agent
./internal/provider on mote, ok; gofmt -l clean; GOOS=linux go vet
-tags nogui and GOOS=windows go vet, ok; go build ./... ok. Full
go test -tags nogui -timeout 45m ./... on mote at cae01311c (the
earlier base; nothing under internal/agent, provider or proc changed
since): every package ok except two reds that are on main and filed,
TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestNoCommandBypassesProc (#4491, now child_unix.go too). WSL
./internal/agent ./internal/provider: provider ok; agent red only in
TestGrokInstalled, TestCursorFolderFromTheEditorIsNotTheCLI and
TestOmpDetectedWithPiDir, red the same alone at origin/main 5b1709eee
in WSL (#4033, #3548); the new Linux tests pass there.

Broken by hand: linuxCodexApp returning false turns TestFindCodexApp and
TestCodexAppOwnServer red; codexAppOpen's Linux case removed and
linuxVersion returning "" turn TestCodexLinuxAppOpen and
TestCodexLinuxAppVersion red; the old store early return turns
TestCodexLinuxAppVersion red; codexReaders' linux case removed turns
TestCodexLinuxAppReadsTheConfig red in WSL.

Not tried: the rpm and Arch packages (the Arch one has the same
usr/lib/chatgpt layout; the rpm's is assumed), a real ChatGPT account
(the app's own UI asks to sign in, since the fake tokens fail refresh),
and reopening the app when no app-server comes back (Windows only;
Linux says to quit and open it again, as macOS does). A switch made from
the CLI while magpie web runs elsewhere doesn't restart the app; the
notice says to, as on macOS.
2026-10-31 16:08:28 +08:00
yetone 1184862583 agent: Codex background compaction writes the bare async_compaction = true where a Codex older than 0.163 reads the config (多喝热水)
多喝热水 (Discord #help, Windows, v0.1.1384, Codex desktop with ChatGPT
sign-in): Codex wouldn't open, "config.toml:154:1: invalid type: map,
expected a boolean". That line is the [features.async_compaction] table
1978eabe5 (#2490/#2493) writes.

Codex versions, from codex-rs source and the real binaries:
- up to 0.163.0-alpha.5 (0.160.0, 0.162.1 included) FeaturesToml is a
  flattened BTreeMap<String, bool>: any table under [features] fails the
  whole config; an unknown bool key is only a warning.
- from 0.163.0-alpha.8, async_compaction is FeatureToml<…>, bool or
  table, and model_async_compact_threshold_percent is read only from the
  table (the bool gives Codex's default 65).
- the provider table's capabilities = {...} is safe: 0.161 and older
  ignore unknown provider keys (schemars-only deny_unknown_fields),
  0.162 parses it. 0.160.0 and 0.162.1 load a config holding it.
- ChatGPT.app 26.1013.40934 bundles Codex 0.163.0-alpha.10.2
  (Contents/Resources/codex-cli/codex-package.json); the reporter's
  Windows app rejects the table, so it bundles one before alpha.8.

Fix: the table is written only when every Codex found here that reads
this config (the CLI on PATH, the desktop app's bundled codex on macOS,
the Store app on Windows, the editor extension, Codex in WSL) has a
version at or after 0.163.0-alpha.8. Otherwise, or when a version can't
be read (the Windows app, the extension), magpie writes the bare
[features] async_compaction = true with no share. A table magpie wrote
before is taken back through its stash (only the keys the stash names
as magpie's; an emptied table goes), so a config already broken opens
again on the next sync. A table or bool the user wrote stays. The stash
records a bool it wrote (`bool`), and the table comes back once every
reader is new enough. Settings' state carries codexAsyncHeld, and the
Codex row says which Codex holds the share back and that it needs
0.163. The note costs app.js 390 bytes; its budget and the core's are
raised by exactly that, the agent's own row in the core.

Run:
- real binaries, sandbox CODEX_HOME/HOME, `codex features list` on the
  config magpie writes (TestCodexAsyncConfigLoadsInRealCodex,
  MAGPIE_TEST_CODEX_BINS): 0.162.1, 0.163.0-alpha.8 and 0.164.0 load it;
  alpha.8 and 0.164.0 report async_compaction true. By hand before the
  fix: 0.160.0 and 0.162.1 exit 1 on the table ("invalid type: map,
  expected a boolean"), load the bool and nothing; alpha.8 and 0.164.0
  load all three.
- broken by hand (table always allowed): the real-binary test is red
  for 0.162.1 with Codex's own error, OldCodexGetsTheBareKey red in all
  four cases, TableTakenBackForOldCodex red ("magpie's table not
  mended"). Green again with the fix.
- go test -tags nogui ./internal/agent ./internal/gui ./internal/edit
  ./internal/settings: ok (gui after the budget, re-run alone: ok).
- GUI (node --test, chromium + webkit): codex-async-compact,
  agent-prefs, settings-general, segs-glide, i18n-keys: 100 pass, 0 fail.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...; go build for darwin, linux (nogui),
  windows: ok.

Not tried: the Windows box (Store-app detection, reading its bundled
codex's version, a non-Store install), a WSL distro, a real Codex run
past the share, the whole GUI suite. The full go test ./... is queued
behind other sessions' suites (one per machine) and wasn't waited for.
2026-10-24 22:02:49 +08:00
yetone 1978eabe58 agent: Codex's background compaction is turned on from Settings › Long conversations (#2490, #2493)
Codex 0.163 can compact a long conversation in the background while a
turn goes on ([features.async_compaction]). A new row under Long
conversations turns it on, with the share of the window it starts at
(empty: Codex's own 65%). magpie writes it into each Codex config it
routes (codex_async.go):

- [features.async_compaction] enabled = true and
  model_async_compact_threshold_percent. Codex reads the threshold only
  in that table; one at the top level is ignored (tried).
- capabilities = { remote_compaction = "v2" } on [model_providers.magpie].
  Codex starts a background compaction only on a provider with v2, which
  it gives OpenAI and Azure alone. Tried with Codex 0.163 against a
  sandbox gateway and a fake upstream reporting 70% use: without the
  capability, no compaction in a whole turn; with it, compaction_trigger
  requests ran beside the turn's steps and the next step's input held
  the summary. The capability is written for a feature the user turned
  on by hand too (#2493 asked whether magpie supports it: it didn't).
  putProvider carries the table's capabilities over when a model switch
  writes it again.
- What magpie wrote is in the stash (codex.async). A value the user set,
  or changed since, is never written over and stays when the switch is
  turned off; a bare async_compaction = true, an inline or a dotted
  table is the user's as written. A config that doesn't parse is not
  written to.

Not tried: Codex signed in with ChatGPT (openai_base_url to magpie on
the built-in provider, v2 by default), only the custom provider path.

Tests: TestCodexAsyncCompactionWritten, ...KeepsTheUsers and
...KeepsUsersCapability (internal/agent), each red with the matching
part removed by hand (no writing; no capability carry-over; no
ownership check on enabled). codex-async-compact.test.cjs: Chromium and
WebKit, en/zh/zh-TW/ja/de, red with the 1-100 check removed;
compact-at, i18n-keys, settings-general, settings-search pass.
TestSettingsSaveKeepsWhatItDoesNotSend holds the two new fields.
gofmt clean; go vet -tags nogui on darwin, linux, windows.
Full suite: go test -tags nogui -timeout 45m ./... (pipefail, suite-lock) at the four #2494/#2504/#2505/#2490 commits on origin/main a751f5282: 65 packages ok, among them internal/agent (2124s), internal/gateway (1845s) and internal/sessions (148s). internal/gui was red on TestSetAnswersWhileDevinAndProvidersHang alone (the state after a set took 11.5s at load ~250; #1375); internal/gui run again alone: ok (206s).
2026-10-17 00:39:37 +08:00