Files
magpie/internal/cli
yetone a1118187b5 gateway: who answered a conversation is written to affinity.json behind the answer, once in a while, not on every answer
answered() (and unanswered()) called saveSticks on every request
answered: up to 512 conversations sorted, marshalled, written to a temp
file and renamed, one answer after another under sticksSaving. Now the
stick is in memory at once (stickOf reads the map first) and the file
follows within sticksFlushAfter (2s): what is answered meanwhile goes in
one write. The gateway's drain (ListenAndServe) and magpie ending on a
signal (cli/signals.go, beside FlushServed) write what is waiting, so a
restart or the magpie handed over to reads every answer. A crash loses
the last 2s, as served.json does since 4f9e8331a. In a test binary the
wait is 0 and the file is written at once, as it was (no timer outlives
a test; 8215e8275 did the same for served.json). A write waiting for
another HOME's file (a test's) is dropped, not written into this one.

Test: TestSticksWrittenBehind (2s set by the test): 200 answers write
no file, the last is known in memory, flushSticks writes all 200, and a
short wait writes by itself. Broken by hand (noteSticks calling
saveSticks always): red, "affinity.json written on a request answered".

BenchmarkAnswered (512 sticks kept, 200x, 2 runs; load average ~450):
  old (saveSticks inline)  21.3 / 25.0 ms/op  506KB/op  2140 allocs/op
  new                      2.5 / 5.6 us/op    392-434B/op  8 allocs/op

Run: go test ./internal/gateway -run '^TestSticksWrittenBehind$' -count 1:
ok. go test ./internal/gateway -run 'TestSticksWrittenBehind|Affinity|
Sealed|Stick|Strict' -count 1: ok (57.8s).
2026-10-19 15:19:31 +08:00
..