Repository navigation
Commit 4c7d2b2
authored
fix(truncate): pick a stable record path when arrays tie (#1770)
## Bug
When a tool response is over the truncation limit, `findArraysRecursive`
(`internal/truncate/truncator.go`) collects candidate record arrays by
ranging over each JSON object, and `chooseBestArray` picks one. On a
tie, `chooseBestArray` keeps whichever candidate came first. Go
randomizes map iteration, so identical payloads with equally good
sibling arrays get a different `RecordPath` on different calls. The
`read_cache` handle minted for that response then pages a different
array each time.
## Trigger
An upstream tool returns, for example, `{"zeta":[…20 rows], "alpha":[…20
rows], "mid":[…20 rows]}` and the response is over the limit.
`content_forward.go` calls `Truncate` on every such response. Repeated
identical calls return `record_path` values of `zeta`, `alpha`, or `mid`
depending on map order.
## Fix
Object keys are now traversed in sorted order, so the tie-break is
stable: the first key in sorted order wins. Non-tied selection is
unchanged.
## Verification
- `go test -count=1 -run TestTruncate_TiedArraysPickStableRecordPath
./internal/truncate/` passes. The test runs 50 calls on the payload
above and expects `alpha` every time.
- With `truncator.go` reverted (`git stash push
internal/truncate/truncator.go`), the same test fails: `call 0: record
path "zeta", want "alpha"`.
- `go test -count=1 ./internal/truncate/` passes.
- `go vet ./internal/truncate/` is clean.
- `gofmt -l internal/truncate/` is clean.
- I also ran `go test -count=1 ./internal/server/` locally, but it hit
the default 10-minute test timeout, which isn't a result either way. A
narrower rerun failed at setup because of a local build-cache error. I'm
relying on CI for that package.
Commands were run with `GOFLAGS=-mod=readonly CGO_ENABLED=0`.1 parent 464dea4 commit 4c7d2b2
2 files changed
Lines changed: 42 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
137 | 138 | | |
138 | 139 | | |
139 | 140 | | |
140 | | - | |
141 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
142 | 151 | | |
143 | 152 | | |
144 | 153 | | |
| |||
0 commit comments