Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.
TypeScript
fix(chat): a message that gains its author mid-render must not change the hook count (#284)
OKNOTOK9782298f5958
diff --git a/biome.json b/biome.jsonindex 1d06542..d930856 100644--- a/biome.json+++ b/biome.json@@ -89,7 +89,7 @@},"correctness": {"useExhaustiveDependencies": "off",- "useHookAtTopLevel": "off",+ "useHookAtTopLevel": "error","noUnusedFunctionParameters": "off"}}diff --git a/src/App.tsx b/src/App.tsxindex b9a220a..2250aa3 100644--- a/src/App.tsx+++ b/src/App.tsx@@ -200,8 +200,15 @@ export function App() {// with a `#notifications` hash. Bypass everything else (auth, stores,// routing) and just render the toast stack — it receives payloads over// IPC from the main window.- if (isNotificationWindow) return <NotificationWindow />+ //+ // This lives in its own component rather than as an early return inside+ // `MainApp` so the bypass stays a *total* bypass: none of MainApp's hooks+ // or its three URL-consuming state initializers run in the notification+ // window, and neither shell can inherit the other's hook sequence.+ return isNotificationWindow ? <NotificationWindow /> : <MainApp />+}+function MainApp() {// Waitlist landing — handleCallback redirects here with `#waitlist=1`// when a brand-new OAuth visitor hit the gate. Consume the fragment// once so refresh doesn't pin them on this screen forever. State isdiff --git a/src/components/Message.tsx b/src/components/Message.tsxindex ab878f9..1ba2509 100644--- a/src/components/Message.tsx+++ b/src/components/Message.tsx@@ -1062,8 +1062,15 @@ function AttachmentCard({ msg }: { msg: Message }) {* When the message has a richer HTML version, the "html" chip becomes a* toggle that opens the rendered HTML inside a sandboxed iframe — the* server sanitizes and the sandbox forbids scripts, so even residual- * hostile markup can't escape. */-function _EmailCard({ msg }: { msg: Message }) {+ * hostile markup can't escape.+ *+ * Dormant: nothing renders it today (email rows fall through to the ordinary+ * text bubble). Kept under its real PascalCase name rather than an `_` prefix+ * so `useHookAtTopLevel` still sees a component and keeps checking its six+ * hooks — an underscore-prefixed function reads as a plain function to the+ * rule, which is how it went unchecked before. */+// biome-ignore lint/correctness/noUnusedVariables: dormant email renderer — see above+function EmailCard({ msg }: { msg: Message }) {const t = useT()const openComposeReply = useApp((s) => s.openComposeReply)const [showHtml, setShowHtml] = useState(false)@@ -1708,6 +1715,18 @@ function MessageRowImpl({ msg, author, delay = 0, animate = true }: MessageRowPrconst openThreadView = useApp((s) => s.openThreadView)const meId = useMe()const split = useChatLayoutStore((s) => s.layout === 'bubble')+ // Hoisted ABOVE the branches below on purpose. `MessageRow` is memoized and+ // both lists key rows by `m.clientId ?? m.id`, so a props change re-renders+ // the SAME fiber: a message that starts out author-less and gains its author+ // on the next store patch would go from five hooks to six on one fiber and+ // React would throw "Rendered more hooks than during the previous render",+ // taking the whole list to the error boundary. Every call site currently+ // pre-guards that case, which is the only reason this has never fired — the+ // duplicated caller-side guard is not the invariant, this hoist is.+ const artifactRefs = useMemo(+ () => artifactRefsForMessage(msg),+ [msg.body, msg.tool?.arg, msg.tool?.detail],+ )// System / whisper rows don't need a resolved author — handle them before// touching `author` so a synthetic-author system message (the calendar-fired// notice, authored by CALENDAR_SYSTEM_AUTHOR_ID) renders instead of being@@ -1722,10 +1741,6 @@ function MessageRowImpl({ msg, author, delay = 0, animate = true }: MessageRowPrconst isAttachOnly = Boolean(msg.attachment) && !msg.bodyconst _isEmail = msg.kind === 'email'const isPoll = msg.kind === 'poll'- const artifactRefs = useMemo(- () => artifactRefsForMessage(msg),- [msg.body, msg.tool?.arg, msg.tool?.detail],- )// Avatar click opens InfoPane for both kinds — humans now have profile// cards (their auth email is the most useful new piece). The "yourself"// case is gated below via the disabled prop.diff --git a/tests/hook-rule-enabled.test.ts b/tests/hook-rule-enabled.test.tsnew file mode 100644index 0000000..4c657e1--- /dev/null+++ b/tests/hook-rule-enabled.test.ts@@ -0,0 +1,50 @@+/**+ * `useHookAtTopLevel` is the only automated guard against a conditional hook,+ * and it has to stay on.+ *+ * The project has been bitten by this class three times. Twice it was caught by+ * hand and left a comment behind (`ChatPane.tsx`, `EmailComposer.tsx`, both+ * describing the same hoist); the third — a `useMemo` below three early returns+ * in the memoized `MessageRow` — survived in `main` because every call site+ * happened to pre-guard the branch that would have changed the hook count.+ * Nothing caught any of them, because `biome.json` had the rule set to `"off"`,+ * so a fully green `npm run lint` proved nothing about hook ordering.+ *+ * Turning it back off is a one-word edit that leaves lint green, which is+ * exactly why the switch itself needs a test rather than just the code it+ * protects. A conditional hook is not a style issue: React throws "Rendered+ * more hooks than during the previous render" and the nearest error boundary+ * eats the whole message list.+ */+import assert from 'node:assert/strict'+import { readFile } from 'node:fs/promises'+import { fileURLToPath } from 'node:url'+import { describe, it } from 'node:test'++const BIOME_CONFIG = fileURLToPath(new URL('../biome.json', import.meta.url))++interface BiomeConfig {+ linter?: { rules?: { correctness?: Record<string, unknown> } }+}++async function readBiomeConfig(): Promise<BiomeConfig> {+ return JSON.parse(await readFile(BIOME_CONFIG, 'utf8')) as BiomeConfig+}++describe('biome.json', () => {+ it('keeps useHookAtTopLevel enabled', async () => {+ const config = await readBiomeConfig()+ const level = config.linter?.rules?.correctness?.useHookAtTopLevel+ assert.notEqual(+ level,+ 'off',+ 'useHookAtTopLevel must stay enabled — it is the only check that a hook is not called after an early return',+ )+ // `undefined` would inherit from the recommended preset, which does NOT+ // include this rule, so an omitted key is the same silent hole as "off".+ assert.ok(+ level === 'error' || level === 'warn' || (typeof level === 'object' && level !== null),+ `useHookAtTopLevel must be set explicitly in biome.json, got ${JSON.stringify(level)}`,+ )+ })+})