Commit Graph
2 Commits
Author SHA1 Message Date
yetoneandFlandern1211 b90e130902 dsh: a fork's call waiting for its reply counts on neither card, not its source's (#5542)
d052fec0e gave a dsh fork without a parentSession its own row, named by
the reply ids it gets. A call whose reply dsh hasn't written down yet
still went by the opening, which the fork copied and only the source
owns, so the source's context line climbed with the fork's turns
(Flandern1211: the source's card showed the fork's 30,778 while its own
last call was 21,944).

routeOwners keeps each opening a fork's seed carried a second time
(ownerCopied). joinConvs leaves a call that names no session out of both
cards (ctxRec.Unmatched) only when its opening is such a shared one, it
has a reply id, and it ended within convsKept: the window Routing's
looseKeys waits in. A call with no reply id (routes from before
e8df78f33, a dsh that writes none) and one past that window go by their
opening as before, so the source keeps its history. An answer that left
a call out stands ctxWhileUnmatched (5s) instead of ctxAgain's minute,
and only while such a call is inside that window. This answers both
review points on 1bce689ed.

Ported from Flandern1211's ad461f7d4, with these changes:
- ctxRec.End time.Time is now Millis int64. The tab keeps one ctxRec
  per route in the range (up to 30 days); End took it from 240 to 264
  bytes, Millis to 248.
- The conflict with #2398's Canceled is resolved by keeping both.
- TestDshForkRouteAwaitingItsReplyIsNeithers also has a long call
  (begun 2m ago, ended 20s ago), and clears Session as well as
  Unmatched before its second join.
- New TestContextHeldAnswerStandsOnlyWhileAReplyIsAwaited covers
  contextFor's cache: an answer without held stands past 6s, one with
  held does not, one with held stands for 1s.

Run (mote, HOME=$(mktemp -d), -tags nogui):
- go test -run 'Dsh|Fork|Routing|Context|Conv' ./internal/gui/, and
  -run 'Dsh|ResponseID' ./internal/gateway/ ./internal/sessions/: ok.
- go test -count=1 ./internal/gui/ ./internal/sessions/: ok (117s, 60s).
- go vet ./internal/gui/ for darwin, linux and windows; gofmt -l: clean.
- node --test internal/gui/tests/context-*.test.cjs and
  compact-one-value.test.cjs (11 files): 474 of 474 pass. No GUI file
  changed; they guard the Context page it draws.
- Benchmark (temporary, not committed), 300k dsh routes over 2000
  sources and 2000 forks: joinConvs 125-135ms per pass with the change,
  140-270ms without; contextOfRecs 130-145ms with, 140-150ms without;
  allocations the same (108MB per pass). mote was at load ~120.

Broken by hand, each red: the age check, the reply-id check, the
Unmatched skip (phantom magpie-… row), held never set, held not
shortening the cache, Millis ignored, sharedOpening always false.

Not tried: a real dsh fork on this side. Flandern1211 measured the pair
on their computer: the source kept its 8 pre-#5491 calls, and a second
/api/context 6s later took 9ms instead of 575ms. Not changed: Routing
still goes by the opening for such a call (its looseKeys retries every
2s), and a fork call whose reply never comes goes back to the source
after a minute.

Reported-by: Flandern1211
Co-authored-by: Flandern1211 <163982805+Flandern1211@users.noreply.github.com>
2026-11-07 20:10:28 +08:00
yetone fbeed7e7f7 usage: the Context tab reads each day and each session file once, not at every refresh (Yayoi_no_yume on X)
A user running magpie on a 1-core, 962 MB server opened Usage › Context.
CPU went to 100%, memory to 91%, and the server went down.

Cause: the tab asks /api/context and /api/tune at every refresh of the
Usage page (every 5s by default). Each ask:
- /api/context read up to 7 days of routing history again, as whole
  gateway.Route values (prompt items, order, tries). A gzipped day not yet
  counted was parsed whole, every route of it, before being cut to its
  latest 2000 (gateway.History).
- /api/tune read every Claude Code and Codex session file in the range
  again, whole, into []sessions.Call, then worked the advice out of all of
  them.

Now:
- gateway.History counts a .gz day's lines first and then parses only its
  latest historyMax+historySlack routes, as it already did for .jsonl.
- /api/context keeps each day's routes slimmed (ctxRec: the parts' tokens
  without their items; the whole prompt only on the routes that can be a
  session's latest), keyed by the size and mtime of the day's files. A
  day is read again only when its files change. The answer is given again
  while its days and the trace are unchanged, for at most a minute.
- /api/tune keeps each file's calls as tune.Call (~100 B each), keyed by
  size, mtime and agent, read one file at a time through
  sessions.ReadCallSource. A message copied into a resumed session is
  still counted once (the earlier file owns it, as in sessions.Calls). The
  advice stands for a minute, and apply or undo lets it go at once.
- tune's stable sorts use slices.SortStableFunc instead of
  sort.SliceStable, which was most of the CPU over 300k calls.

Measured on mote with a generated fixture: 8 days of 20,000 routes each
(7 gzipped, built from a real 2.2 KB route), and 200 Claude Code files of
1,500 calls each (300k calls). The scratch bench was not committed. mote
was at load 130-170 from other suites, so wall times are noisy; CPU time
is from rusage and peak heap is sampled every 2 ms.
  /api/context  before: 1st ask 4.2s CPU, 158 MB peak heap, 202 MB RSS;
                        every refresh 0.63s CPU, 111-121 MB peak heap
                after:  1st ask 0.84s CPU, 48-55 MB, 90-93 MB RSS;
                        every refresh 3-5 ms CPU, 21-34 MB
  /api/tune     before: 1st ask 4.3s CPU, 498 MB peak heap, 717 MB RSS;
                        every refresh 1.0s CPU, 370-494 MB peak heap
                after:  1st ask 0.83s CPU, 373 MB, 409 MB RSS;
                        within a minute 0 CPU, nothing read;
                        after a minute with a file changed, that file is
                        read and the advice worked out again (~0.5s CPU)

Tests:
- TestContextReadsADayOnce: a day whose files are unchanged is not read
  again; a grown day is, and only that day; each session's latest prompt
  keeps its items; the kept answer equals contextOf over every route.
- TestTuneReadsAFileOnce: an unchanged file is not read again; a grown
  one is; a copied message counts once; the call count matches
  tuneOf(sessions.Calls(...)); within a minute nothing is read.
- TestHistoryParsesOnlyAGzDaysLatest: a 5,200-route gz day parses at
  most 2,200 routes.
Broken by hand: always re-read the day, skip tune's file check, drop
History's count-first block. All three tests went red (days read 4
times, files read 5 times, 5,200 routes parsed), then green again once
restored.

Run on mote at this tree:
- gofmt -l: clean.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows
  go vet ./..., go build ./...: all ok.
- go test ./internal/gui ./internal/gateway ./internal/tune
  ./internal/sessions: tune and sessions ok. gui is red only on
  TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27 on mote). gateway
  is red only on the Antigravity turned-away tests and
  TestCodexSpentGrokFailingIsTold, which are red on main (#4362). This
  change touches neither.
- GUI (node --test, Chromium and WebKit): context-*.test.cjs,
  compact-one-value, usage-refresh, usage-refresh-flight and
  usage-stale-answer: 270 of 273 pass. The 3 reds are context-map's
  "two tiles overlap" in WebKit at 420px (en, ja, de), the overlap
  #3488 has under load (mote at load ~160). No .js changed, so app.js's budget is
  untouched.

Not tried:
- A real 1-core, 1 GB box.
- Codex rollouts in the fixture: Codex files take the same path.
- internal/usage's codexCalls, which still uses sessions.AgentCalls
  (Codex only, on the Usage identity path) and is unchanged.
- tune's first ask, and each ask after a minute with a file changed,
  still works over the range's calls at once: ~30 MB kept per 300k calls,
  and a few hundred MB peak while the advice is worked out. It is bounded
  by the calls in the range, not by a fixed size.
2026-10-30 13:23:19 +08:00