Skip to content

fix(ui): emotion avatars were cache-busted by a constant, so a regenerated avatar kept its old face for 24h (#2374) - #2416

Merged
dolho merged 1 commit into
devfrom
fix/2374-emotion-avatar-cache
Aug 28, 2026
Merged

dolho merged 1 commit into
devfrom
fix/2374-emotion-avatar-cache

Conversation

@dolho

@dolho dolho commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

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:

const avatarVersion = agent.value.avatar_url?.split('v=')[1] || '1'

Two failures, and the second is what made it permanent:

  1. Stale source. Read out of possibly-stale in-memory state — an avatar regenerated in another tab, or before agent refreshed, leaves avatar_url unchanged, so every emotion URL keys the same old cache entry.
  2. A constant is not a cache-buster. With no v= on avatar_url the 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/emotions now returns a version: 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 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; 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 (@vue/test-utils isn't a dependency, vitest runs environment: 'node'), so a rule kept inline is a rule no test can reach.

Acceptance criteria

AC Status
Cycling never shows a previous avatar's variant, same session ✅
Version tied to actual generation, not parsed with a '1' fallback ✅ tied to the variant files themselves
Regeneration elsewhere reflected without a 24h stale window ✅ stamp moves per poll
Regression test covering the version derivation ✅ 17, mutation-checked

Verification

MUTANT: constant '1' fallback restored → 7 failed
restored                                → 17 passed

Full frontend suite 64 files / 1453 tests; vite build clean; backend avatar tests pass.

Closes #2374

🤖 Generated with Claude Code

…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]>
@dolho dolho added priority-p2 Important theme-ui-ux Theme: UI/UX type-bug Bug fix labels Aug 27, 2026

@obasilakis obasilakis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/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:

  1. 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.
  2. GET /{agent}/avatar/emotions is 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.

@dolho
dolho merged commit 8d9af18 into dev Aug 28, 2026
25 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority-p2 Important theme-ui-ux Theme: UI/UX type-bug Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants