ac4af582301a4e8a29245dbba767eba31334ddf9
4
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
c9d253e4a2 |
gateway: an agent's compaction is recorded as Compaction, not a plain turn
shangyuan0818 (Discord, v0.1.3354): Claude Code's compactions don't show in Usage → Requests. Through the gateway they were recorded and counted, but as ordinary turns (Kind ""), so nothing read as a compaction: the Compaction purpose listed none, the OTel export skipped them as turns the transcript has (Claude Code's compaction is skipTranscript, so it has none), and the reply ended the agent's turn. The main handler now marks a call compacting() recognises (Claude Code's summary ask, OpenCode's and the other agents' prompts) as KindCompact, on every route it serves (messages, chat, responses, gemini), unless a header already set its kind. The check reads the facts parse the request already makes (bodyFacts.compact). Codex's backend route already took the kind from its header; it now uses the same constant. Not fixable here: Claude Code used directly (not through magpie) never writes a compaction's usage to its transcript (only compact_boundary with token counts before and after), so magpie can't count those. Run (mote, at this change on |
||
|
|
d088030315 |
gateway: a long Codex request's facts, tier and metadata skip the walks over its conversation (#3394)
A Codex Responses request carries the whole conversation, and the gateway
walked it several times per request for what it reads at the top level:
- requestSessionMetadata searched the body for "client_metadata", which
Codex sends last, before asking topMember. A long body now asks the
memoized split straight away.
- bodyTier asked gjson for speed and service_tier, walking the body each
time; a long body now asks topMember too (topValue).
- userWords ran its reminder and "## My request" regexes on every text;
each now runs only when its marker is present.
- readFacts (turn count, work folder) ran the full parse. It now parses
in facts mode (readResponses): views over the raw bytes, no parts for
assistant replies, and tool outputs as bare ToolResult parts, which is
all turnIn and workPlace read.
Bench (a fake ChatGPT backend, ~2MB Codex body, client_metadata last),
per request: 31.7ms, 24.2MB, 71.2k allocs on origin/main; 19.2-21.4ms,
17.2MB, 59.6k allocs with these and the provider commit after it.
Real account: a sandbox HOME with only the deepseek provider, 310KB
Responses bodies to deepseek/deepseek-flash through `magpie serve`:
base 15ms process CPU per request (19/20 ok), new 13ms (20/20, then
8/8 again).
TestResponsesFactsAsTheWholeParse compares readFacts with the full
parse's turnIn and workPlace over constructed bodies and five real Codex
fixtures. Broken by hand twice (user parts skipped; ToolResult turned
into Text): red both times. tier_record_test adds four long-body cases
(tier after the conversation, a nested one ignored, Anthropic speed
fast, a Chat numeric tier).
Run at this head (
|
||
|
|
254461829b |
gateway: a Codex Lite request is no longer decoded whole, nor parsed twice for its facts
XiaoYin_OK on X: after a long run the app's memory climbs toward 2 GB. The owner's own app (26h up) showed RSS 550 MB, HeapInuse 317 MB but HeapSys 992 MB: the heap peaks while requests are in flight. Its profile had 209 GB allocated, ~94 GB of it under codexBackend/serve, of which: - parseResponses 24.7 GB, liftAdditionalTools 14.9 GB of it; - factsOf 24.6 GB, reached from turnOf (13.6 GB) and dirOf (11.0 GB), the same conversation parsed twice per request. Three causes, each fixed where it is: 1. liftAdditionalTools decoded the whole Lite request into maps to move its additional_tools item, and wrote it all again: ~7.6x the body. Now only that item and the tools are decoded; every other item and member goes byte for byte as it came, written once into one buffer. 2. affine counted the turn in the masked body while dirOf read the plain one, two different slices, so factsOf's memo could never serve both. affine now counts it in the body as the agent wrote it. 3. That body is rarely the one readRequestBody held (requestModel, spawnAgentModels and the lift each write a new one), so factsOf's memo missed for it anyway. serve now holds it (holdBody), and holdBody lists a body once when it is held twice. Tests (internal/gateway/lite_lift_test.go), each red on the old code: - TestLiteLiftMovesOnlyTheTools: same result as the old whole decode (liftWhole, kept in the test) on testdata/context/codex.json, the merged-namespace case, a 2,000-word conversation and the no-op shapes; an item with its keys unsorted, \u escapes and 2.50 goes as written. Old: the kept item came back re-sorted and re-escaped. - TestLiteLiftAllocatesAboutTheBody: under 2x a 1.35 MB body. Old: 14,126,976 B (10.4x). - TestMaskedRequestParsedOnceForItsFacts: a served masked Lite request is parsed once for its turn and folder (factsParsed hook in readFacts). Old: 2. With only change 2 it was still 2 (cause 3). Measured (darwin/arm64, a 4,012,240 B Lite body): - liftAdditionalTools: 30.46 MB/op, 98,560 allocs -> 5.17 MB/op, 6,609. - readFacts of it: 54.0 MB/op -> 26.7 MB/op. - Served through New().Handler() with masking on, 8 concurrent x 5 rounds of a 4,010,268 B body, two runs each: allocated per request 215.6 / 220.7 MB -> 116.2 / 116.2 MB; peak HeapInuse 993 / 802 MB -> 612 / 561 MB; HeapSys 1001 / 892 MB -> 699 / 674 MB. - The live app's WebKit processes were ~8-9 MB RSS each, so the webview is not where the memory went on macOS; the live pid sat at 476-596 MB RSS over a 15-minute sample. Run at this head: gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet ./...; go build (gui and nogui); go test -tags nogui -timeout 45m ./... in a sandbox HOME at |
||
|
|
41147956d2 |
gateway: a conversation's turn count and folder come from one parse of it, not one each (yg6888 on X: ~1 GB)
turnOf (affinity), dirOf (the trace's folder) and tellWorkPlace (a plugin's work-place headers) each parsed the whole conversation for a number and a folder. A parse decodes every message and tool output into strings of its own, so a long conversation was decoded twice over on every request for them. In the 60-min soak before ( |