RAIA™ Specification

3.32 Contact Avatar Media Pipeline

Every contact in RAIA (Real estate Artificial Intelligence Agent) has a photo. Those photos live in a locked storage room (a private Google Cloud bucket). Because the room is locked, the portal can't just point at a photo directly — it has to ask a doorman (an API route) for a temporary key (a signed URL that expires in 15 minutes). The doorman hands the key to your browser, which fetches the photo. If anything goes wrong, the doorman hands back a grey silhouette instead, so the page never shows a broken image.

← Back to Developer Hub TEXTBOOK SPECIFICATION
In Plain English

Every contact in RAIA (Real estate Artificial Intelligence Agent) has a photo. Those photos live in a locked storage room (a private Google Cloud bucket). Because the room is locked, the portal can't just point at a photo directly — it has to ask a doorman (an API route) for a temporary key (a signed URL that expires in 15 minutes). The doorman hands the key to your browser, which fetches the photo. If anything goes wrong, the doorman hands back a grey silhouette instead, so the page never shows a broken image.

How contact photos are stored in a private GCS bucket, signed on demand, and served to <img> tags — and how a single non-idempotent resolver caused the two-week "all avatars grey" incident. This page is the incident knowledge capture: every claim verified against src/lib/media.ts, src/app/api/persons/[id]/avatar/route.ts, and commit 057875dc.

Private GCS Bucket V4 Signed URLs BOLA-Guarded Incident: 2026-07-29
3,857
Avatar objects in raia-gb-private — bare paths stored in tbl_persons.avatar_url
15 min
Signed URL TTL — V4 signatures with in-process cache bucketed at TTL/3
302
Always a redirect, never an error — the only consumer is an <img> tag
2 wks
Cost of the double-resolution bug — captured here so it never happens again

Section A What Is the Avatar Pipeline?

When a landlord, tenant, or guarantor uploads a photo, RAIA (Real estate Artificial Intelligence Agent) does not put it on the public internet. The photo goes into a locked Google Cloud Storage bucket called raia-gb-private, and the database saves only the path to the file — like a coat-check ticket, not the coat itself. When a staff member opens a page showing that contact, the browser asks the portal's doorman route (/api/persons/<id>/avatar) for the photo. The doorman checks you're allowed to see it, swaps the ticket for a 15-minute temporary key (a V4 signed URL), and redirects your browser to fetch the photo. If the person has no photo, or anything breaks, the doorman hands back a built-in grey silhouette so the page still looks fine.

Browser <img src="/api/persons/<id>/avatar"> never sees GCS directly GET /api/persons/[id]/avatar requireStaffAccess → BOLA org check read tbl_persons.avatar_url generateAvatarReadUrl() → 302 any failure → 302 to silhouette sign raia-gb-private persons/<uuid>/avatar.(jpg|png|webp) 3,857 objects · no public read V4 signed URL · 15-min TTL · cached in-process 302 + Cache-Control: private, max-age=300 browser follows redirect → fetches photo from storage.googleapis.com Any failure 302 → /images/avatar-default.svg never a broken image or JSON error The DB stores a coat-check ticket (bare object path) — the route is the only doorman that can exchange it for a key. Request / redirect Signed URL Fallback

The Two-Week Grey Circles (Why This Page Exists)

In July 2026 every contact photo in the portal rendered as a grey silhouette for two weeks. Three diagnoses were wrong before the real one landed. The data was fine. The bucket was fine. The signing was fine. The bug was one function that wasn't idempotent: the API resolved the raw database value into a route path, and then a component resolved that route path again, rejected it (it doesn't look like an https URL or a storage path), and fell through to the silhouette. Every claim in the Developer section below is verified against the fix commit 057875dc so the next agent finds the answer in two minutes, not two weeks.

The One-Sentence Lesson

If a value can arrive at your function already resolved, the resolver must pass it through untouched. Resolution must be idempotent. Resolve(resolve(x)) must equal resolve(x).

Section B What You See in the Portal

Contact photos appear next to landlords, tenants, guarantors, and contractors throughout the portal — in list rows, detail headers, and the biography panel. There are exactly three visual states:

What you seeWhat it meansAction
Contact's photo Photo exists in the private bucket and was signed successfully None — everything is working
Grey silhouette placeholder The person has no usable photo: never uploaded one, or the record holds a legacy path from the old AppSheet system that no longer exists Upload a new photo from the contact's detail page (staff only)
Person-icon (Lucide) instead of an image The contact record has no photo value at all — the UI shows an icon, not an image tag Cosmetic only; upload a photo if you want one

Uploading Your Photo

  1. 1
    Open your profile or a contact you manage

    Staff can upload for themselves via /api/me/avatar/upload-url; contact photos go through the same two-step flow.

  2. 2
    Request an upload URL, then PUT the file

    The portal requests a short-lived (5-minute) signed write URL, uploads the file straight to GCS, then calls /api/me/avatar/confirm which writes the bare object path persons/<uuid>/avatar.<ext> into tbl_persons.avatar_url.

  3. 3
    Photo appears within one redirect

    The next page load requests /api/persons/<id>/avatar, receives a 302 to a 15-minute signed URL, and renders the photo. Redirects are cached privately for 5 minutes.

Why Some Photos Are Grey — And Why That's Expected

A grey silhouette is the designed fallback, not a bug. The route that serves avatars can never return an error to the browser — its only consumer is an <img> tag, which cannot display JSON. So every failure mode (no photo, dead legacy path, signing error, permission denied) resolves to the same grey circle. This is deliberate: the UI degrades to a placeholder instead of a broken-image icon.

When grey IS a bug

If every avatar across the whole portal is grey — including people who definitely have photos — that is a system fault, not missing data. See the Developer debugging table below. The decisive first probe takes 30 seconds.

Section C Storage Model

Developer Section — verified against src/lib/media.ts, route.ts, gcs.ts @ 057875dc

tbl_persons.avatar_url stores a bare GCS object path, not a URL:

persons/<uuid>/avatar.(jpg|png|webp)     — bucket: raia-gb-private (PRIVATE)

The bucket has no public read access, so a storage.googleapis.com URL would 403. That is why a read route exists at all. Three value shapes exist in production data:

Value shape in avatar_urlOriginResolution behavior
persons/<uuid>/avatar.<ext> Current upload flow (/api/me/avatar/upload-url + confirm) Routed through /api/persons/<id>/avatar → V4 signed → 302
https://… (Cloudinary, LinkedIn, etc.) Legacy imports isRenderableAvatar() → passed straight through, no signing needed
Contact_Images/<uuid>.Thumb.… Dead AppSheet paths — never in our bucket Deliberately rejected by isPrivateAvatarObjectPath() → silhouette. Signing them would produce a URL that 404s.
Strict regex, on purpose

isPrivateAvatarObjectPath() only matches paths the upload flow itself writes: /^persons\/[0-9a-f-]{36}\/avatar\.(jpg|png|webp)$/i. Only paths we wrote are worth resolving — everything else falls to the silhouette by design.

Read Path — GET /api/persons/[id]/avatar

  1. 1
    Validate the UUID

    Non-UUID id → immediate 302 fallback to AVATAR_DEFAULT.

  2. 2
    requireStaffAccess

    Must be an authenticated staff session; the guard's own response passes through untouched.

  3. 3
    BOLA org check

    Administrators are cross-org. Everyone else may see their own photo, or a person sharing one of their organisations (tbl_organisations_persons lookup). Failure → silhouette, not 403.

  4. 4
    Branch on the stored value

    Legacy https → 302 straight to it. Private object path → generateAvatarReadUrl() (V4, 15-min TTL, in-process cache bucketed at TTL/3, cache cleared at 500 entries). Anything else → silhouette.

  5. 5
    Failure → 302 to silhouette, and LOG

    Since PR #285 (892afe66) a signing failure logs [avatar] signing failed for <path>. Swallowing it silently is what made a broken GOOGLE_STORAGE_CREDENTIALS_JSON indistinguishable from "nobody has photos" for hours.

302 Location: https://storage.googleapis.com/raia-gb-private/persons/…?X-Goog-Signature=…
Cache-Control: private, max-age=300      ← redirect cache < 15-min signature TTL (deliberate)

THE CRITICAL RULE — Resolution Must Be Idempotent

getContactAvatarUrl(personId, rawAvatarUrl) may receive either a raw DB value or an already-resolved API route path. Its pass-through order (commit 057875dc):

export function getContactAvatarUrl(personId, rawAvatarUrl) {
  if (!personId) return AVATAR_DEFAULT;
  // 1. already resolved by the API → pass through UNTOUCHED
  if (rawAvatarUrl?.startsWith("/api/persons/")) return rawAvatarUrl;
  // 2. legacy public https URL → use as-is
  if (isRenderableAvatar(rawAvatarUrl)) return rawAvatarUrl;
  // 3. private object path → point at the signing route
  if (isPrivateAvatarObjectPath(rawAvatarUrl)) return `/api/persons/${personId}/avatar`;
  // 4. everything else → silhouette
  return AVATAR_DEFAULT;
}
!
Root cause of the 2-week incident: double resolution

The API resolved avatar_url server-side and handed components /api/persons/<uuid>/avatar. A component then called getContactAvatarUrl on that value again. The route path is not https, does not match the object-path regex, and did not have the pass-through branch — so it fell through to AVATAR_DEFAULT. Grey circles for every landlord despite a fully working data chain. The fix is branch 1 above; the commit message on 057875dc is the incident post-mortem.

Debugging Table — The Three Grey Circles

"Grey circle" has three distinct causes with identical visuals. Distinguish them in 60 seconds:

CauseElements panel showsNetwork panel showsMeaning
(a) Falsy avatar_url No <img> at all — a Lucide person icon SVG No avatar request Component never rendered an image; the record has no value. Cosmetic.
(b) Resolution fell through <img src="/images/avatar-default.svg"> No /api/persons/… request getContactAvatarUrl rejected the value client-side. This was the incident signature.
(c) Route fallback <img src="/api/persons/<uuid>/avatar"> 302 → /images/avatar-default.svg Server-side: no photo, dead path, BOLA denial, or signing failure (check logs for [avatar] signing failed).
The decisive first probe (30 seconds)

GET /api/landlords (authenticated) and read one avatar_url value. If the API already returns /api/persons/<uuid>/avatar and the browser still shows a silhouette without requesting that route, the bug is client-side double resolution — branch (b). If the route IS requested and 302s to the SVG, the bug is server-side — branch (c).

Anti-Patterns That Wasted Days

1
Diagnosing credentials from vercel env pull output

The CLI escapes newlines when writing .env files. A private key full of \n literals in the pulled file is an artifact of the pull, not corruption in Vercel. Days were spent "fixing" credentials that were never broken. Verify credentials by signing a URL, not by inspecting the pulled file.

2
Concluding from unauthenticated probes only

The avatar route requires staff auth. Curling it without a session tells you nothing except that the guard works — yet "the route 302s to the silhouette for me" was reported as a signing bug. Always probe with an authenticated session before concluding anything server-side.

3
Auditing code from a stale checkout or worktree

The repo working directory may lag origin/main. The double-resolution bug was "confirmed present" in a worktree after it had already been fixed on main. First command of any audit: git fetch && git status && git log --oneline -1 origin/main -- <file>. Verify the branch before trusting the code in front of you.

Ops Facts & Upload Flow

FactValueSource
Upload endpoints/api/me/avatar/upload-url (V4 signed write, 5-min) → PUT file → /api/me/avatar/confirm (writes bare path to DB)src/app/api/me/avatar/
Failure log line[avatar] signing failed for <path>PR #285 · 892afe66
Route slug fixRenamed avatar route slug to [id] — unbroke ALL API routesPR #284 · 6c93495e
Redirect cacheCache-Control: private, max-age=300 — deliberately below the 15-min signature TTL so a cached redirect never points at an expired signatureroute.ts · REDIRECT_CACHE_SECONDS
Signed URL cacheIn-process Map keyed by path@floor(now / (TTL/3)); cleared at 500 entries to bound long-lived serverless instancesgcs.ts · generateAvatarReadUrl
Default placeholder/images/avatar-default.svg — local static, edge-cached, zero GCS dependencymedia.ts · AVATAR_DEFAULT
Related documents

§ 3.26 Image Carousel & Deduplication covers property photos (Cloudinary). Person Data Architecture covers the tbl_persons schema. This page covers only the contact-avatar read/write path through the private bucket.

AVATAR STORAGE ARCHITECTURE Layer 1 Upload API POST /avatar Multer/GCS File handler Layer 2 GCS Bucket Raw + thumbnails Cache layer CDN edge caching Layer 3 DB Reference avatar_url col Serve API GET /avatar Images stored in GCS; DB holds URL reference only