saveRoute writes a done route to the file of the day it began, and
pruneHistory, at most once an hour, gzips every day before today. gzipFile
wrote <day>.jsonl.gz whole from <day>.jsonl and removed the file, so a
request that began before midnight and was done after the first prune past
it (a long streamed reply, say) went into a new <day>.jsonl, and the next
prune gzipped that over the day's .gz: every other route of the day was
lost, and only the late ones were left. While both files were there,
History listed the day twice.
* gzipFile keeps a day's .gz and adds the file's routes after it as a
gzip member of their own, which the gzip reader reads back as one
stream. The new .gz is written to <day>.jsonl.gz.tmp and renamed over
the old one (steady.Rename), and the file is removed only once that is
done.
* A route the .gz has already is not added again: a file added and left
behind (magpie stopped before removing it) adds only what was written
to it since, however many times that happens. The .gz is read a route
at a time and matched against a set made from the file's lines, never
held whole unzipped.
* A .gz that doesn't read to its end, or ends in a route cut short (an
older magpie stopped, or the disk filled, while it or its file was
written), is written anew from its whole routes and the file's when the
file has a route to add. Added after the break, the file's routes could
never be read, and after a route cut short the first of them would join
it. A last route whole but for its newline is kept. The old gzipFile
mended a .gz cut short by writing it over from its file, and this keeps
that.
* A .gz that can't be read, other than for not being there, is unknown,
not empty: it and the file are left as they are for a later prune.
* pruneHistory removes a <day>.jsonl.gz.tmp once it is an hour old, left
by a magpie stopped between the write and the rename. A younger one
may be another magpie's, still being written.
* History counts a day's .jsonl beside its .gz into one entry of its day
list.
A day with only its .jsonl, the usual case, is gzipped to the same lines
as before at the same cost: 6.3 MB allocated for a real 4.5 MB day, here
and upstream. A late route beside a real day's .gz (569 KB, 5,481 routes)
allocates 14.7 MB and takes about 16 ms; upstream allocated 1.1 MB in under
a millisecond, writing over the day. While a file added and left behind is
there, until the next prune, History counts its routes twice, as it did
before.
Tests, with what upstream's history.go gives:
* TestHistoryKeepsALateRouteWithItsDay: a route done after its day's
prune, the file it went to left behind once with nothing written since
and twice with a late route written to it, and an older day beside it.
Upstream lists the day twice before the second prune ([{2026-09-29 1}
{2026-09-29 1} {2026-09-28 1}]), and after it returns routes [2] and
"route 1 not found".
* TestHistoryKeepsADayCutShort: a .gz cut short while written in place,
its whole file still there, comes back [1 2 3]; a .gz gzipped from a
file whose last route was cut short, a late route written since, comes
back [1 3]; one whose last route had no newline comes back [1 2 3].
Upstream returns [3] for the last two (the late file written over the
day), and passes the first, which it mends the same way.
* TestHistoryLeavesADayItCantRead: a .gz that can't be read keeps the
file beside it, and both read back [1 2] once it can be. Upstream
returns [2]. It skips where a file at mode 0 still reads: as root, on
Windows, or on a file system without Unix permissions.
* TestHistoryDropsALeftGzTmp: a .gz.tmp an hour and a minute old goes,
one written just now stays. Upstream leaves both.
Each part was also broken on its own, and each break fails a test:
History's old day list (the day twice), matching a file left behind by the
.gz's last member instead of by line ([1 2 2 3 3 4]), no check for a route
the .gz has ([1 2 2]), adding after a .gz cut short ([1] and "flate:
corrupt input before offset 41"), a mend that drops the .gz's routes ([2
3]), no check of the .gz's last newline ([1]), dropping a whole last route
without its newline ([1 3]), keeping a route cut short (4 routes counted
for 3), reading an unreadable .gz as empty ([2]), no sweep of the .gz.tmp,
and a sweep with no hour's grace. Writing the .gz in place instead of
through the .tmp passes them all: the atomic replace isn't covered by a
test.
Verified on 0bf9d77d, under a temporary HOME with Go's caches pinned:
gofmt -l, go vet ./..., go build, GOOS=linux go build -tags nogui and
GOOS=windows go build pass; the four new tests pass with -race -count=20,
and the history tests with -race -count=10. go test -tags nogui ./...
passes but for internal/plugin, whose tests don't build internal/gateway:
its TestHostJSCancellation/ready-wait ran past its 10s limit with the
load average at about 25 to 35, and passes alone (4.6s).
On a copy of a real routing directory, one late route on 2026-10-05 left
that day's 5,481 routes in place and listed it once with 5,482; upstream
kept the late route alone. Not tried on Windows itself. Two magpies pruning
the same directory at once (during a take-over) aren't covered: history.mu
holds within one process only.