Repository navigation
fix(ui): emotion avatars were cache-busted by a constant, so a regenerated avatar kept its old face for 24h (#2374) - #2416
Conversation
…rated avatar kept its old face for 24h (#2374) Emotion variants are served from a STABLE url — `/avatar/emotion/{emotion}` — under `Cache-Control: public, max-age=86400`. The `?v=` the client appends is the only thing separating a fresh variant from a day-old cached one, and it was derived as `agent.avatar_url?.split('v=')[1] || '1'`. That fails two ways, and the second is the one that made this permanent: 1. It reads the version out of possibly-stale in-memory state. An avatar regenerated in another tab, or before `agent` was refreshed, leaves `avatar_url` unchanged — so every emotion URL keys the same old cache entry. 2. When `avatar_url` carries no `v=`, the fallback collapses to the CONSTANT `'1'`. Every emotion URL is then permanently `?v=1`, and no regeneration can ever change it: the browser serves the previous avatar's face until the entry expires 24 hours later. A constant is not a cache-buster. ## The stamp tracks the variants, not the base avatar `GET /avatar/emotions` now returns a `version` — the newest mtime among the files it is listing. That is deliberately not `avatar_updated_at`: these URLs serve the VARIANTS, and regeneration replaces them one at a time in the background, so a client polling this endpoint re-keys its URLs as the new set lands rather than holding whatever it cached on the first poll. It also does not depend on the DB column being populated. `"0"` means no variants exist and is treated as absent rather than as a new constant. A file that vanishes between `exists()` and `stat()` is mid-regeneration; skipping its mtime only understates the version and the next poll picks it up, so it never fails the listing. ## The fallback ladder never ends in a constant Endpoint stamp -> the `v=` already on `avatar_url` (correct whenever the payload is fresh, and the path for a backend predating the stamp) -> a per-load value. The last rung costs one refetch per page load and cannot go stale, which is the safe direction for a control whose failure mode is showing the wrong person's face. A failed poll clears the stamp rather than reusing a stale one — keeping it would be the same bug by a slower route. The derivation lives in `utils/avatarEmotion.js` because `AgentDetail.vue` cannot be mounted in this project's test setup, so a rule kept inline is a rule no test can reach. ## Verification 17 tests, mutation-checked: restoring the constant `'1'` fallback turns 7 red. Full frontend suite 64 files / 1453 tests; `vite build` clean; backend avatar tests pass. Closes #2374 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
obasilakis
left a comment
There was a problem hiding this comment.
/validate-pr — approving.
Clean fix with the right shape. The version is derived from the files these URLs actually serve rather than from the base avatar's avatar_updated_at, which is what makes it move as background regeneration replaces variants one at a time. Extracting the derivation into utils/avatarEmotion.js because AgentDetail.vue cannot be mounted in this test setup is the correct call, and the reason is stated in the module rather than left implicit.
The part I want to single out: refusing to let the fallback be a constant. A constant is the actual defect — || '1' pinned every emotion URL to one cache entry for 24 hours — and String(Date.now()) costs one refetch per page load while being structurally incapable of going stale. The "0" case falling through rather than becoming a new constant is the same instinct applied once more.
Test coverage is genuinely non-happy-path: empty v=, v= followed by another param, null and undefined URLs, and a source-level assertion that the failed-poll branch clears the stamp rather than reusing a stale one. The last one is the case that would have re-created the bug by a slower route.
Checklist: base dev, Closes #2374 present so the issue auto-promotes, security scan clean, no infra or packaging surface, no new top-level backend module. Docs are commit-message-only per the tiered rule for a bug fix. CI green.
Two non-blocking notes, neither worth another round:
str(int(newest))is second-resolution, so two regenerations landing inside the same second produce the same version. Vanishingly unlikely given the background generator's pacing, and the next poll fixes it anyway.GET /{agent}/avatar/emotionsis unauthenticated, so the stamp discloses when an agent's avatar was last regenerated to anyone who can guess a name. The endpoint already leaked which emotions exist for a given name, so this widens an existing surface rather than opening one, and the value is an mtime rather than anything sensitive.
Summary
Emotion variants are served from a stable URL under
Cache-Control: public, max-age=86400, so the?v=the client appends is the only thing separating a fresh variant from a day-old cached one. It was:Two failures, and the second is what made it permanent:
agentrefreshed, leavesavatar_urlunchanged, so every emotion URL keys the same old cache entry.v=onavatar_urlthe fallback collapses to'1'. Every emotion URL is then permanently?v=1, and no regeneration can ever change it — the browser serves the previous face until the entry expires 24 hours later.The stamp tracks the variants, not the base avatar
GET /avatar/emotionsnow returns aversion: the newest mtime among the files it is listing.Deliberately not
avatar_updated_at, though the issue suggests it. These URLs serve the variants, and regeneration replaces them one at a time in the background — so a stamp taken from the files moves as the new set lands, and a client polling this endpoint re-keys its URLs progressively instead of holding whatever it cached on the first poll. It also doesn't depend on the DB column being populated. This addresses the secondary race the issue flags, not just the primary suspect."0"(no variants) is treated as absent rather than as a new constant. A file that vanishes betweenexists()andstat()is mid-regeneration — skipping its mtime only understates the version and the next poll picks it up, so it never fails the listing.The fallback ladder never ends in a constant
endpoint stamp → the
v=already onavatar_url(correct whenever the payload is fresh; the path for a backend predating the stamp) → a per-load value.The last rung costs one refetch per page load and cannot go stale, which is the safe direction for a control whose failure mode is showing the wrong person's face. A failed poll clears the stamp rather than reusing a stale one — keeping it would be the same bug by a slower route.
The derivation lives in
utils/avatarEmotion.jsbecauseAgentDetail.vuecannot be mounted in this project's test setup (@vue/test-utilsisn't a dependency, vitest runsenvironment: 'node'), so a rule kept inline is a rule no test can reach.Acceptance criteria
'1'fallbackVerification
Full frontend suite 64 files / 1453 tests;
vite buildclean; backend avatar tests pass.Closes #2374
🤖 Generated with Claude Code