Files
CatoandClaude Opus 5 95d3ab3262 feat(chat): the chat gets a room of its own (#48)
* feat(chat): conversations that survive a reload, for people who signed in

The chat has been the product wearing a marketing site: the assistant works,
and then you reload and it is gone. This is the storage half — schema, rules,
queries and routes — with no UI yet, so the risky part lands reviewable on its
own.

Not reusing the group tables. `invite_token` is NOT NULL UNIQUE and is a
credential: every private chat would mint a joinable room key. And deletion
runs the opposite way — a group keeps `left_at` rows precisely so old messages
stay authored, while a private conversation must be destroyable.

Decisions the schema states out loud:

- `image_count`, never the images. Attachments would be the largest rows and
  the most private artefact the product touches. The count is enough for a
  reopened thread to say a picture was here.
- `locale` on the conversation. Reopening a thread from a browser negotiated
  to French must not switch Heidi mid-conversation.
- Delete destroys. The conversation row is tombstoned so a deleted id keeps
  answering a stable 404; the messages are hard-deleted in the same
  transaction. A `deleted_at` that leaves the words in the table is a lie the
  privacy section would then have to tell.
- The caps refuse; they never prune. Dropping someone's oldest thread to make
  room is data loss dressed as a feature.

`APP_TABLES` gains both tables — it is the only detector for the
postgres-owns-the-table grant failure, and missing it leaves /api/health green
while every write 500s. The new store suite asserts that check passes.

Nothing anonymous reaches the database: no cookie-minted pseudo-actor, which
would be a tracking id whose rows nobody can ever authenticate to delete.
`/api/conversations/import` adopts a signed-out draft only when asked, and
rebuilds every field — ids, authorship, timestamps and the decoded answer —
because a draft lives in localStorage, where anyone with a dev console writes.

`requireActor` moves to lib/domain/actor.ts; groups/session.ts stays as a thin
delegator on purpose, because groups/routes.test.ts stubs authorisation by
mocking that module at its resolved file URL.

Also ends a duplication the linter caught: MAX_IMAGES existed four times — two
routes, the composer, and a dead copy a refactor left behind. It now lives with
the server-side validation, which is the side that enforces it.

275 tests, 31 of them new.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FzYBD3sZroCAYeRgTYrpfT

* feat(chat): the chat gets a room of its own

The assistant worked and then you reloaded and it was gone. `/chat` is the same
conversation with space to be one: a transcript that scrolls inside the
viewport, a composer that stays put, and — signed in — the thread you had
yesterday in a sidebar you can resume, rename and delete.

The home page box and the full-screen chat are ONE conversation, not two. Both
read the same store, so "Vollbild öffnen" continues the thread and coming back
to the home page finds it still going. Nothing is transferred: a transcript in
a query string would land in the access log, the Referer header and browser
history.

Signed out still works, and nothing anonymous reaches the database.

**Two bugs this found, both by reloading the page rather than by reading it:**

- The persist effect ran on mount with an empty thread and CLEARED the draft it
  was about to restore. Visiting the home page would have wiped a conversation
  without touching anything. Writing now waits for the seed.
- Seeding through the subscribed store value raced its own `ready` flag — two
  independent `useSyncExternalStore` subscriptions with nothing ordering them,
  so the draft could still be null on the tick `ready` flipped, seeding an
  empty thread and marking the job done. It reads the store directly instead.

**The chrome escape is one CSS rule**, keyed on `body:has([data-chrome="chat"])`,
not a second root layout. There can only be one root layout — `<html lang>` has
to carry the real language of the page — and navigating between two of them
costs a full document reload, so every trip from the home page into the chat
would throw the app away and start again. `min-height: 0` on `main` is the
load-bearing line: a flex child refuses to shrink below its content, so without
it the page grows instead of the transcript scrolling. `dvh`, not `vh`, or the
composer hides under iOS Safari's toolbar.

**Signed-in visitors land in the chat**, decided in middleware rather than in
the page. Calling `auth()` in `page.tsx` reads a cookie, which would opt the
`priority: 1` marketing page into dynamic rendering for every visitor. Crawlers
are always signed out, so they still get the home page — confirmed in the build
output, where `/[locale]` is still SSG. The decision itself is pure and tested
(`lib/i18n/landing.ts`), including the case that would have bounced `/de/chat`
to itself forever.

**The conversation row is created on the first message**, not when the page
opens, so a "new chat" button that gets pressed and abandoned leaves nothing
behind. Which row a transport writes to lives in its own closure with an
in-flight promise, because React state is not visible to the second message of
a fast double-send — which is how one conversation becomes two.

HEIDI.md §5, §9 and §10 rewritten. §10 was four sentences of intent written
when there was nothing to be private about; there is now, so it says what
actually happens — where a signed-out conversation lives, that delete destroys
the messages, that pictures are counted and forgotten, and that adoption is
offered rather than assumed.

Verified with `pnpm run verify` (290 tests) and against a production build in a
browser at 1280px and 390px: reload restores, home↔chat keeps the thread, the
body does not scroll, the footer is gone, the header is static.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FzYBD3sZroCAYeRgTYrpfT

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 02:50:20 -07:00

22 lines
902 B
TypeScript

import { requireActor, type Caller } from "../actor.ts";
/**
* Who is asking, for a group route.
*
* Groups are one of two parts of Heidi that REQUIRE an account, and the reason
* is not policy — a message needs an author other people can see, and an
* anonymous visitor has no stable name to attribute anything to. The chat and
* the dialect gate work signed out and stay that way.
*
* THIS FILE STAYS, thin, on purpose. `groups/routes.test.ts` stubs
* authorisation by mocking this module at its resolved file URL
* (`mock.module(new URL("./session.ts", …))`), so moving or deleting it would
* silently stop the group route tests from controlling who the caller is —
* and a test that no longer controls the actor still passes.
*/
export type { Caller };
export function caller(): Promise<Caller> {
return requireActor({ name: "Study groups", toDo: "to use study groups" });
}