Files
magpie/internal/sessions/codex_discovery.go
T
yetone 79f9b3752b sessions: a Codex archived copy rewritten at the same size in the clock tick of its last comparison is compared again, not taken from the cache, on Windows too (#1299, wizzy-yang)
The copy cache trusted a change time it had seen as proof the file had not
changed. NTFS stamps ChangeTime from Windows' clock tick, not the precise
clock: on the Windows box, 300 ms of back-to-back same-size writes got a
new ChangeTime only every ~0.5 ms (2147 of 2691 writes kept the previous
stamp; diffs 0.5/1.0/1.5 ms). A rewrite in the tick of the comparison kept
the stamp the cache saw, so the cached relation was reused. That, not write
merging, is why TestCodexArchivedSameSizeRewriteInvalidatesDiscovery failed
19 of 300 runs on Windows, alone as well as in the full package.

A comparison is now cached only when both files' change times are more than
a second older than the moment it started (codexStampSettle); a file changed
more recently is compared again next time. A later rewrite then lands in a
later tick and changes the stamp. Change times are compared as times
(Windows FILETIME converted).

TestCodexSameTickRewriteIsNotCached gives every write one change time and a
clock 10 ms into that tick. With the settle rule taken out (cacheable = both
stamps present), it fails: "a rewrite in the tick of the comparison reused
its stale relation". With it, it passes, and the second half checks settled
copies are still compared once, not every time.
TestCodexArchivedDiscoveryCacheAndAppend moves its clock a minute on so the
just-written copies count as settled.

The Windows CI step now runs the Codex copy tests (CodexArchived,
CodexSameTick, CodexPlainTwin), which it had never run.

Run: Windows 11 box (cross-built sessions.test.exe, sandbox USERPROFILE/
APPDATA/TEMP): old binary 19/300 failures of the reported test; new binary
0/1000 for the three Codex copy tests, and the Windows CI regex x5 with
testdata passes. macOS: go test -race ./internal/sessions, -race -count=30 of
the Codex copy tests, go vet (darwin, GOOS=linux, GOOS=windows), gofmt -l.
Not run: Linux with a coarse ctime clock (kernels before 6.13); the rule
covers it the same way.
2026-10-08 19:04:18 +08:00

229 lines
6.1 KiB
Go

package sessions
import (
"bytes"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"time"
)
const (
codexCopiesDiffer = iota
codexCopiesEqual
codexFirstPrefix
codexSecondPrefix
maxCodexCopyPairs = 4096
)
type codexCopyPair struct{ First, Second string }
type codexCopyResult struct {
First, Second os.FileInfo
Relation int
}
var codexCopyCache = struct {
sync.Mutex
Pairs map[codexCopyPair]codexCopyResult
}{Pairs: map[codexCopyPair]codexCopyResult{}}
// Windows replaces this with a same-handle stat carrying ChangeTime.
var codexDiscoveryStat = os.Stat
var compareCodexCopies = readCodexCopies
var codexDiscoveryNow = time.Now
// A file system stamps change times from the clock tick, not the precise
// clock: NTFS from Windows' system time (0.5 ms to 15.6 ms), Linux from the
// coarse kernel clock. Two writes within one tick get the same change time,
// so a comparison is cached only for files whose change time is older than
// this when it starts. A later rewrite is then in a later tick and changes
// the stamp; a file changed more recently is compared again.
const codexStampSettle = time.Second
type codexDiscoveryInfo struct {
os.FileInfo
ChangeTime time.Time
}
// codexChangeTime is the file's change time, zero when the file system
// doesn't expose one.
func codexChangeTime(info os.FileInfo) time.Time {
if wrapped, ok := info.(codexDiscoveryInfo); ok {
return wrapped.ChangeTime
}
// Darwin and Linux expose nanosecond ctime under different field names.
// An unsupported filesystem gets no cached content comparison: mtime
// alone cannot detect a same-size rewrite with a restored timestamp.
v := reflect.ValueOf(info.Sys())
if v.Kind() == reflect.Pointer && !v.IsNil() {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return time.Time{}
}
for _, name := range []string{"Ctimespec", "Ctim"} {
c := v.FieldByName(name)
if c.IsValid() && c.Kind() == reflect.Struct {
sec, nsec := c.FieldByName("Sec"), c.FieldByName("Nsec")
if sec.IsValid() && nsec.IsValid() && sec.CanInt() && nsec.CanInt() {
return time.Unix(sec.Int(), nsec.Int())
}
}
}
return time.Time{}
}
// settledCodexStamp reports whether a comparison of this file, started at
// start, can be cached: it has a change time from before the current tick.
func settledCodexStamp(info os.FileInfo, start time.Time) bool {
c := codexChangeTime(info)
return !c.IsZero() && c.Before(start.Add(-codexStampSettle))
}
func sameCodexSnapshot(a, b os.FileInfo) bool {
if a == nil || b == nil || a.Size() != b.Size() || !a.ModTime().Equal(b.ModTime()) || !codexChangeTime(a).Equal(codexChangeTime(b)) {
return false
}
if wrapped, ok := a.(codexDiscoveryInfo); ok {
a = wrapped.FileInfo
}
if wrapped, ok := b.(codexDiscoveryInfo); ok {
b = wrapped.FileInfo
}
return os.SameFile(a, b)
}
// Only a proven decoded prefix is a redundant copy. A shared thread/name
// alone cannot establish ownership of divergent logs or distinct segments.
func distinctCodexFiles(files []file) []file {
groups := map[string][]int{}
keep := make([]bool, len(files))
for i, f := range files {
keep[i] = true
name := strings.TrimSuffix(filepath.Base(f.path), zstSuffix)
for _, j := range groups[name] {
if !keep[j] {
continue
}
switch codexCopyRelation(files[j].path, f.path) {
case codexCopiesEqual, codexSecondPrefix:
keep[i] = false
case codexFirstPrefix:
keep[j] = false
}
if !keep[i] {
break
}
}
groups[name] = append(groups[name], i)
}
out := files[:0]
for i, f := range files {
if keep[i] {
out = append(out, f)
}
}
return out
}
func codexCopyRelation(first, second string) int {
start := codexDiscoveryNow()
a, errA := codexDiscoveryStat(first)
b, errB := codexDiscoveryStat(second)
if errA != nil || errB != nil {
return codexCopiesDiffer
}
key := codexCopyPair{first, second}
cacheable := settledCodexStamp(a, start) && settledCodexStamp(b, start)
codexCopyCache.Lock()
saved, found := codexCopyCache.Pairs[key]
codexCopyCache.Unlock()
if cacheable && found && sameCodexSnapshot(a, saved.First) && sameCodexSnapshot(b, saved.Second) {
return saved.Relation
}
relation, err := compareCodexCopies(first, second)
if err != nil {
return codexCopiesDiffer
}
// A comparison spanning an append/replacement is not a stable proof.
afterA, errA := codexDiscoveryStat(first)
afterB, errB := codexDiscoveryStat(second)
if errA != nil || errB != nil || !sameCodexSnapshot(a, afterA) || !sameCodexSnapshot(b, afterB) {
return codexCopiesDiffer
}
if cacheable {
codexCopyCache.Lock()
if len(codexCopyCache.Pairs) >= maxCodexCopyPairs {
for old := range codexCopyCache.Pairs {
delete(codexCopyCache.Pairs, old)
break
}
}
codexCopyCache.Pairs[key] = codexCopyResult{a, b, relation}
codexCopyCache.Unlock()
}
return relation
}
// Stream comparisons use constant memory. When one stream ends, consume the
// other to validate its compression trailer before selecting the longer copy.
func readCodexCopies(first, second string) (int, error) {
a, err := openLines(first)
if err != nil {
return 0, err
}
defer a.Close()
b, err := openLines(second)
if err != nil {
return 0, err
}
defer b.Close()
x, y := make([]byte, 32<<10), make([]byte, 32<<10)
for {
nx, endA, ex := readCodexChunk(a, x)
ny, endB, ey := readCodexChunk(b, y)
if ex != nil {
return 0, ex
}
if ey != nil {
return 0, ey
}
if !bytes.Equal(x[:min(nx, ny)], y[:min(nx, ny)]) {
return codexCopiesDiffer, nil
}
if nx < ny {
_, err = io.Copy(io.Discard, b)
return codexFirstPrefix, err
}
if ny < nx {
_, err = io.Copy(io.Discard, a)
return codexSecondPrefix, err
}
if endA && endB {
return codexCopiesEqual, nil
}
}
}
// io.ReadFull turns a clean short EOF into ErrUnexpectedEOF, making it
// indistinguishable from a truncated zstd frame. Preserve that distinction.
func readCodexChunk(r io.Reader, b []byte) (n int, end bool, err error) {
for n < len(b) {
read, err := r.Read(b[n:])
n += read
if err == io.EOF {
return n, true, nil
}
if err != nil {
return n, false, err
}
if read == 0 {
return n, false, io.ErrNoProgress
}
}
return n, false, nil
}