Conventions
How code is laid out and why. Read this before adding a new page,
hook, or backend call. For why the structure is shaped this way see
ARCHITECTURE.md;
for the end-to-end "add a route" recipe see the repo's contributor docs at
internal_docs/DEVELOPMENT.md.
Layering
src/lib/ — pure TS, no React. Types, fetch wrappers, utils, config.
src/hooks/ — React hooks. Depend on lib + React.
src/components/ — UI. Depend on hooks + lib.
src/app/ — Pages. Compose components.
src/app/api/ — BFF route handlers. Only server-side code.Each layer depends only on layers below it. A lib/ module that
imports from hooks/ is wrong.
Hooks
- One file per resource:
src/hooks/use-<resource>.ts. - REST:
useQuery/useMutationwrapping the typed helpers insrc/lib/api/client.ts. - SSE: build on
useSse(src/hooks/use-sse.ts). Don't constructEventSourcedirectly —useSsehandles reconnect, visibility, and CP backpressure. - Refetch cadence comes from
REFETCHinsrc/lib/query-options.ts. Don't write raw millisecond literals. ExtendREFETCHif no bucket fits. - Mutations invalidate every query key they affect on success.
Components
- Everything is
'use client'except the thin page wrappers that destructureparams. We render live data; server components would just hand off to client components anyway. - Render explicit empty, error, and loading states for
every backend-dependent view. Use
EmptyStateandLoadingSkeletonfromsrc/components/shared/. - Show AIDs through
AidCell— never raw. It truncates intelligently and copies the full value on click. - Show relative times through
TimeAgo. It re-renders once a second. - Don't hard-code colors. Source them from
src/lib/colors.ts(which mirrors the@themeblock insrc/app/globals.css).
BFF proxies
- Every browser fetch goes through
/api/playground/**or/api/cp/**. Never call sibling services directly. - Route handlers are 4-line delegations to
proxyGet/Post/Put/Patch/ Delete/Sseinsrc/lib/api/proxy.ts. No business logic in the route file. URLSearchParamsand path segments under user control must beencodeURIComponent-ed. The proxy helpers don't double-encode.- Status codes and bodies pass through unchanged. Connection failures
synthesize a sanitized
{ error: 'Upstream unreachable', target, upstream_status: 502 }; raw error detail is logged server-side viaconsole.error, never sent to the browser.
Filter / tab state
If it's operator-visible — search inputs, status pickers, active tab —
sync it to the URL with useUrlState / useUrlEnum / useUrlInt from
src/hooks/use-url-state.ts. Local useState for filters is for
ephemeral UI state only (e.g. an inline confirm dialog).
Accessibility
- Every icon-only
<button>needs anaria-label. - Tables:
<th scope="col">on every header cell. - Form inputs: associate the label by wrapping the control or by
htmlFor+ matchingid. TheFieldhelpers in trust-anchors, pinned-keys, revocation, and audit are all wrapper-style. :focus-visiblestyles live insrc/app/globals.css. Don't re-style focus inline.
Tests
- Unit + component tests live next to the source:
<name>.test.ts(x). - Hook tests mock
@/lib/api/clientdirectly instead of stubbing the globalfetch— keeps the test independent of jsdom's missingResponseconstructor and focused on hook behaviour. - SSE tests drive the polyfilled
EventSourcefromsrc/test/polyfills.ts. Seesrc/hooks/use-sse.test.tsxfor the pattern. - Integration tests live in
src/test/*.integration.test.ts(node environment,jest.integration.config.js). Two kinds:- Self-contained — run everywhere, including CI, with no services.
bff-routes.integration.test.tscalls the real route handlers against an in-process mock upstream;sdk-verification.integration.test.tscalls the realaitpnative addon directly against committed fixtures (it needs the addon, not a running service, so it isn'tRUN_INTEGRATION-gated either). - Live-service (
proxies,cp-mutations,scenario-run) — gated byRUN_INTEGRATION=1(andRUN_LLM_INTEGRATION=1for the scenario run); they hit a real running console + CP + playground and skip silently otherwise.
- Self-contained — run everywhere, including CI, with no services.
- CI also runs
next buildas a gate — it catches missing Suspense boundaries, prerender errors, and bad RSC boundaries that type/lint can't.
Things we don't do
- shadcn/ui — we hand-rolled
src/components/shared/so the mock's visual fidelity stayed intact. Opt in later if we ever need a full library. - Global state stores — no zustand, no Redux. URL state and TanStack Query cover what we need today.
- Server actions — every page is client-side because it streams or polls; server actions add no value.
- Inline color hex literals — go through
Cfromsrc/lib/colors.ts.
When in doubt, grep for an existing example and follow that pattern.