perf: per-session mutex in WriteStatsDetailed#12
Merged
Conversation
…iled Closes #10 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the proxy session stats writer to reduce cross-session contention during .ctx.json updates by moving from a single process-wide mutex to per-session locking, and expands the written stats structure/tests to support richer per-turn/category analytics.
Changes:
- Replace the single global stats write mutex with a per-session-ID lock map to allow parallel writes across different sessions.
- Introduce
WriteStatsDetailedplus new on-disk fields (per_turn,total,identity_tokens,context_tokens) for more detailed session analytics. - Update/add tests to validate the new schema and concurrent writing behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| internal/proxy/session.go | Implements per-session locking and extends the ctx stats schema + adds WriteStatsDetailed. |
| internal/proxy/session_test.go | Updates tests for the new schema and adds concurrent multi-session write coverage. |
Comments suppressed due to low confidence (1)
internal/proxy/session.go:104
- sessionID is concatenated directly into a filename (
filepath.Join(sessionsDir, sessionID+".ctx.json")). Because sessionID can come from the X-Engram-Session header, a value containing path separators (e.g. "../") can escape sessionsDir and overwrite arbitrary files. Restrict sessionID to a safe character set (or use filepath.Base + explicit rejection of path separators) before using it in filesystem paths.
if err := os.MkdirAll(sessionsDir, 0o700); err != nil {
return fmt.Errorf("create sessions dir: %w", err)
}
path := filepath.Join(sessionsDir, sessionID+".ctx.json")
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+13
to
+33
| // sessionMu provides per-session-ID locking so concurrent writes to different | ||
| // sessions do not block each other. | ||
| var sessionMu struct { | ||
| sync.Mutex | ||
| m map[string]*sync.Mutex | ||
| } | ||
|
|
||
| func sessionLock(id string) func() { | ||
| sessionMu.Lock() | ||
| if sessionMu.m == nil { | ||
| sessionMu.m = make(map[string]*sync.Mutex) | ||
| } | ||
| mu, ok := sessionMu.m[id] | ||
| if !ok { | ||
| mu = &sync.Mutex{} | ||
| sessionMu.m[id] = mu | ||
| } | ||
| sessionMu.Unlock() | ||
| mu.Lock() | ||
| return mu.Unlock | ||
| } |
Comment on lines
+48
to
+51
| PerTurn tokenTotals `json:"per_turn,omitempty"` | ||
| Total tokenTotals `json:"total,omitempty"` | ||
| Identity tokenSeries `json:"identity_tokens,omitempty"` | ||
| Context tokenSeries `json:"context_tokens,omitempty"` |
Comment on lines
43
to
+52
| // ctxStats is the on-disk structure for proxy-measured context token accounting. | ||
| type ctxStats struct { | ||
| CtxOrig int `json:"ctx_orig"` | ||
| CtxComp int `json:"ctx_comp"` | ||
| Turns int `json:"turns"` | ||
| CtxOrig int `json:"ctx_orig"` | ||
| CtxComp int `json:"ctx_comp"` | ||
| Turns int `json:"turns"` | ||
| PerTurn tokenTotals `json:"per_turn,omitempty"` | ||
| Total tokenTotals `json:"total,omitempty"` | ||
| Identity tokenSeries `json:"identity_tokens,omitempty"` | ||
| Context tokenSeries `json:"context_tokens,omitempty"` | ||
| } |
Comment on lines
+110
to
+117
| stats.CtxOrig += turn.Total.Orig | ||
| stats.CtxComp += turn.Total.Comp | ||
| stats.Turns++ | ||
| stats.PerTurn = tokenTotals{ | ||
| Orig: turn.Total.Orig, | ||
| Comp: turn.Total.Comp, | ||
| Saved: clampSaved(turn.Total.Orig, turn.Total.Comp), | ||
| } |
Comment on lines
+118
to
+122
| for _, key := range []string{"ctx_orig", "ctx_comp", "turns", "per_turn", "total", "identity_tokens", "context_tokens"} { | ||
| if _, ok := got[key]; !ok { | ||
| t.Errorf("ctx file missing key %q; got keys: %v", key, got) | ||
| } | ||
| } |
Comment on lines
+164
to
+179
| // TestWriteStats_PerSessionLocking verifies that writes to different sessions | ||
| // do not block each other — distinct sessions must hold independent locks. | ||
| func TestWriteStats_PerSessionLocking(t *testing.T) { | ||
| dir := t.TempDir() | ||
| const sessions = 20 | ||
| done := make(chan error, sessions) | ||
| for i := 0; i < sessions; i++ { | ||
| go func(n int) { | ||
| done <- WriteStats(dir, fmt.Sprintf("session-%d", n), n*100, n*30) | ||
| }(i) | ||
| } | ||
| for i := 0; i < sessions; i++ { | ||
| if err := <-done; err != nil { | ||
| t.Errorf("parallel WriteStats failed: %v", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #10
Summary
mu sync.Mutexwith a per-session-ID map of mutexesTest plan
TestWriteStats_*andTestWriteStatsDetailed_*tests passTestWriteStats_ConcurrentDifferentSessionsverifies concurrent writes to distinct sessions all succeed with correct totals