Commit 562da4c
fix(session-storage): bound getSessionMessages cache to prevent unbounded growth
`getSessionMessages` was implemented via `lodash memoize`, which has no
size cap. Each unique sessionId adds a `Promise<Set<UUID>>` entry that
is only cleared by `clearSessionMessagesCache()` — and that call is
only made from `postCompactCleanup`, which fires once per compaction.
In long-running daemon / swarm sessions that spawn many agents (each
with its own transcript), the cache grows unbounded for the process
lifetime. Per-entry Sets are MUCH larger than the file-path strings
the adjacent `existingSessionFiles` cache stores, so the leak is more
severe than the one fixed for that cache at `sessionStorage.ts:535`.
The fix mirrors the existing `existingSessionFiles` pattern: replace
lodash `memoize` with a `Map<UUID, Promise<Set<UUID>>>` and evict the
oldest entry (FIFO via Map insertion order) when at
`MAX_CACHED_SESSION_FILES` capacity. Re-uses the same constant for
consistency.
Concretely:
- `sessionStorage.ts`: refactor `getSessionMessages` to be a regular
`async function` backed by a module-level `Map`, with FIFO eviction
at `MAX_CACHED_SESSION_FILES`. Export `getSessionMessagesCache()` so
`getLastSessionLog`'s priming logic (and tests) can interact with the
underlying Map directly. Export `getSessionMessages` itself with a
`@internal`-style comment noting it's not part of the public API
(callers should keep using `doesMessageExistInSession`).
- `src/utils/__tests__/sessionStorage.test.ts` (new): 7 tests covering
cache identity, clear behavior, concurrent-call dedup, bounded size
after many distinct sessionIds, FIFO eviction order, and refill
after clear. Uses real temp directory + `CLAUDE_CONFIG_DIR` env to
keep tests hermetic — no `mock.module` pollution.
Verified on Windows 11 + Bun 1.3.14:
- `bun run precheck`: 5899/5900 pass (the 1 fail is a pre-existing flaky
`toolEventObserver` test that times out on Windows runners, unrelated
to this change)
- 7/7 new tests pass
- No regressions in other test files
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 75c0376 commit 562da4c
2 files changed
Lines changed: 193 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3941 | 3941 | | |
3942 | 3942 | | |
3943 | 3943 | | |
| 3944 | + | |
| 3945 | + | |
| 3946 | + | |
| 3947 | + | |
| 3948 | + | |
| 3949 | + | |
| 3950 | + | |
| 3951 | + | |
| 3952 | + | |
3944 | 3953 | | |
3945 | 3954 | | |
3946 | | - | |
| 3955 | + | |
| 3956 | + | |
| 3957 | + | |
| 3958 | + | |
| 3959 | + | |
| 3960 | + | |
3947 | 3961 | | |
3948 | | - | |
3949 | | - | |
| 3962 | + | |
| 3963 | + | |
| 3964 | + | |
| 3965 | + | |
| 3966 | + | |
| 3967 | + | |
| 3968 | + | |
| 3969 | + | |
| 3970 | + | |
| 3971 | + | |
| 3972 | + | |
| 3973 | + | |
| 3974 | + | |
3950 | 3975 | | |
3951 | 3976 | | |
3952 | | - | |
3953 | | - | |
3954 | | - | |
| 3977 | + | |
| 3978 | + | |
| 3979 | + | |
| 3980 | + | |
| 3981 | + | |
| 3982 | + | |
| 3983 | + | |
| 3984 | + | |
| 3985 | + | |
3955 | 3986 | | |
3956 | 3987 | | |
3957 | | - | |
| 3988 | + | |
3958 | 3989 | | |
3959 | 3990 | | |
3960 | 3991 | | |
3961 | | - | |
| 3992 | + | |
3962 | 3993 | | |
3963 | 3994 | | |
3964 | 3995 | | |
| |||
3996 | 4027 | | |
3997 | 4028 | | |
3998 | 4029 | | |
3999 | | - | |
4000 | | - | |
4001 | | - | |
4002 | | - | |
4003 | | - | |
| 4030 | + | |
| 4031 | + | |
| 4032 | + | |
4004 | 4033 | | |
4005 | 4034 | | |
4006 | 4035 | | |
| |||
0 commit comments