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 atd2e0ea393plus this change: every package ok (internal/gateway 619s) except two guard reds already on main and untouched here: internal/proc's TestNoCommandBypassesProc (#3567) and internal/agent's TestFolderVarsAreListed (TZ in grouprule.go, filed as #3600). After the rebase onto642d430ec(one gateway test file added upstream), internal/gateway was run again: ok (425s), with gofmt and the three vets of it clean. Not tried: a 26h run of the new build against real Codex traffic, and Windows (#3383's 4.2 GB report) beyond go vet.
85 lines
2.4 KiB
Go
85 lines
2.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"unsafe"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// bodyFacts is what the readers of a request's conversation that only
|
|
// read it take from it: the turn count (turnOf), the folder its agent
|
|
// works in and whether that is a git repository, as the text says
|
|
// (dirOf, tellWorkPlace). A body that doesn't parse says none of them.
|
|
type bodyFacts struct {
|
|
turn int
|
|
within bool
|
|
dir, git string
|
|
}
|
|
|
|
// factsOf is bodyFacts of body, read in protocol proto. Each of its
|
|
// readers parsed the whole conversation, every message and tool output
|
|
// decoded into strings of its own, one just after the other: a 2 MB
|
|
// conversation was decoded twice over on every request for a number and
|
|
// a folder (yg6888 on X: ~1 GB).
|
|
//
|
|
// For a body being served (holdBody), the facts are kept while the body
|
|
// is held, so it is parsed for them once: they are a few words, kept
|
|
// whole, where keeping the parse itself would keep a copy of the
|
|
// conversation for the request's life.
|
|
func factsOf(proto provider.Protocol, body []byte) bodyFacts {
|
|
if len(body) < validMemoMin {
|
|
return readFacts(proto, body)
|
|
}
|
|
key := unsafe.SliceData(body)
|
|
heldBodies.Lock()
|
|
e, ok := heldBodies.facts[key]
|
|
heldBodies.Unlock()
|
|
if ok && e.n == len(body) && e.proto == proto {
|
|
return e.facts
|
|
}
|
|
f := readFacts(proto, body)
|
|
heldBodies.Lock()
|
|
defer heldBodies.Unlock()
|
|
// only while body is held, whole: one released may be freed, and its
|
|
// memory given to another body that would read as this one. A body
|
|
// read in two protocols keeps the last.
|
|
if l := heldBodies.list.Load(); l != nil {
|
|
for _, b := range *l {
|
|
if unsafe.SliceData(b) == key && len(b) == len(body) {
|
|
if heldBodies.facts == nil {
|
|
heldBodies.facts = map[*byte]heldFacts{}
|
|
}
|
|
heldBodies.facts[key] = heldFacts{n: len(body), proto: proto, facts: f}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return f
|
|
}
|
|
|
|
// readFacts parses body for its bodyFacts.
|
|
func readFacts(proto provider.Protocol, body []byte) bodyFacts {
|
|
if f := factsParsed.Load(); f != nil {
|
|
(*f)(body)
|
|
}
|
|
req, err := parse(proto, body)
|
|
if err != nil {
|
|
return bodyFacts{}
|
|
}
|
|
var f bodyFacts
|
|
f.turn, f.within = turnIn(req)
|
|
f.dir, f.git = workPlace(req)
|
|
return f
|
|
}
|
|
|
|
// heldFacts is a held body's bodyFacts (factsOf).
|
|
type heldFacts struct {
|
|
n int
|
|
proto provider.Protocol
|
|
facts bodyFacts
|
|
}
|
|
|
|
// factsParsed, when set (tests), is told of each body readFacts parses.
|
|
var factsParsed atomic.Pointer[func(body []byte)]
|