Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: Append keeps the log open between calls, not an open and a close for each request (#3725)

yetone39526c06905a
diff --git a/internal/usage/held_log_test.go b/internal/usage/held_log_test.go
new file mode 100644
index 000000000..c1d04b4d0
--- /dev/null
+++ b/internal/usage/held_log_test.go
@@ -0,0 +1,59 @@
+package usage
+
+import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+ "time"
+)
+
+// Append keeps the log open between calls (#3725), and still writes to
+// the file Path names after another program replaces or removes it.
+func TestAppendHeldLogFollowsThePath(t *testing.T) {
+ pageHome(t)
+ holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
+ t.Cleanup(func() {
+ mu.Lock()
+ defer mu.Unlock()
+ if heldLog.f != nil {
+ heldLog.f.Close()
+ heldLog.f = nil
+ }
+ })
+ Append(Record{Agent: "codex", Provider: "relay", Model: "m", Input: 2, Status: 200})
+ first := heldLog.f
+ Append(Record{Agent: "codex", Provider: "relay", Model: "m", Input: 3, Status: 200})
+ if runtime.GOOS != "windows" && (first == nil || heldLog.f != first) {
+ t.Fatalf("the log was opened again for the second call: %p then %p", first, heldLog.f)
+ }
+ if runtime.GOOS == "windows" && heldLog.f != nil {
+ t.Fatal("Windows kept the log open")
+ }
+
+ f := filepath.Join(filepath.Dir(Path()), "replacement.jsonl")
+ if err := os.WriteFile(f, nil, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Rename(f, Path()); err != nil {
+ t.Fatal(err)
+ }
+ Append(Record{Agent: "codex", Provider: "relay", Model: "m", Input: 7, Status: 200})
+ if s := Summarize(Today); s.Input != 7 || s.Calls != 1 {
+ t.Fatalf("after a replacement: %+v", s.Totals)
+ }
+
+ if err := os.Remove(Path()); err != nil {
+ t.Fatal(err)
+ }
+ Append(Record{Agent: "codex", Provider: "relay", Model: "m", Input: 11, Status: 200})
+ if s := Summarize(Today); s.Input != 11 || s.Calls != 1 {
+ t.Fatalf("after a removal: %+v", s.Totals)
+ }
+
+ pageHome(t) // another HOME: the log is another file
+ Append(Record{Agent: "codex", Provider: "relay", Model: "m", Input: 13, Status: 200})
+ if s := Summarize(Today); s.Input != 13 || s.Calls != 1 {
+ t.Fatalf("in another HOME: %+v", s.Totals)
+ }
+}
diff --git a/internal/usage/usage.go b/internal/usage/usage.go
index 608c8d8ba..b6adaf71f 100644
--- a/internal/usage/usage.go
+++ b/internal/usage/usage.go
@@ -13,6 +13,7 @@ import (
"math"
"os"
"path/filepath"
+ "runtime"
"slices"
"sort"
"strconv"
@@ -245,21 +246,67 @@ func Append(r Record) {
}
mu.Lock()
defer mu.Unlock()
+ path := Path()
+ f, before := openLog(path)
+ if f == nil {
+ return
+ }
+ if !holdLog {
+ defer f.Close()
+ }
+ if _, err := f.Write(append(b, '\n')); err != nil {
+ if holdLog {
+ f.Close()
+ heldLog.f = nil
+ }
+ return
+ }
+ after, _ := statLogHandle(f)
+ noteLogAppend(path, before, after)
+}
+
+// heldLog is the log Append keeps open between calls, under mu: every
+// request appends, and an open and a close of usage.jsonl each time was
+// most of Append's time (#3725). It is written to only while Path still
+// names that same file, so a log moved, removed or replaced is opened
+// again. Windows closes it after each write, since a file open there
+// can't be removed or renamed.
+var heldLog struct {
+ path string
+ f *os.File
+}
+
+const holdLog = runtime.GOOS != "windows"
+
+// openLog returns the log to append to and its state before the write,
+// or nil when it can't be opened.
+func openLog(path string) (*os.File, os.FileInfo) {
+ if f := heldLog.f; f != nil {
+ heldLog.f = nil
+ if heldLog.path == path {
+ before, err := statLogHandle(f)
+ now, nerr := statLogFile(path)
+ if err == nil && nerr == nil && sameLogFile(before, now) {
+ heldLog.f = f
+ return f, before
+ }
+ }
+ f.Close()
+ }
// the folder is made only when it is missing: every request appends,
// and MkdirAll stat'ed it each time (#3725)
- f, err := os.OpenFile(Path(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
- if errors.Is(err, fs.ErrNotExist) && os.MkdirAll(filepath.Dir(Path()), 0o755) == nil {
- f, err = os.OpenFile(Path(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
+ f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
+ if errors.Is(err, fs.ErrNotExist) && os.MkdirAll(filepath.Dir(path), 0o755) == nil {
+ f, err = os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
}
if err != nil {
- return
+ return nil, nil
}
- defer f.Close()
before, _ := statLogHandle(f)
- if _, err := f.Write(append(b, '\n')); err == nil {
- after, _ := statLogHandle(f)
- noteLogAppend(Path(), before, after)
+ if holdLog {
+ heldLog.path, heldLog.f = path, f
}
+ return f, before
}
// IsRejected also recognizes local rejections written before the explicit flag.