Pattern lift from eyaltoledano/claude-task-master (MIT + Commons Clause — pattern only, no code lift). Adds BOOCODE_TOOLS env var with three tiers: - core (4 tools): view_file, list_dir, grep, find_files. ~2k token schema cost. - standard (15 tools): core + web_search, web_fetch, git_status, all 8 codecontext_* tools. ~10k token schema cost. - all (default; current behavior): every tool in ALL_TOOLS (20). ~21k token schema cost. The env var is a CEILING — narrows agent whitelists, never expands. Default behavior unchanged when var is unset. resolveToolTier is case-insensitive and falls back to 'all' on unknown values. CORE_TOOL_NAMES + STANDARD_TOOL_NAMES validated at module load against TOOLS_BY_NAME via two top-level for-loops that throw on the first missing name. Module fails to import if a tier references a tool that doesn't exist in the registry — catches typos and stale tier definitions at boot rather than silently filtering valid tools out of agent whitelists. Wiring: agents.ts parseAgentBlock now reads BOOCODE_TOOLS from process.env per parse, intersects with the agent's declared frontmatter tools (or DEFAULT_TOOLS when frontmatter omits the field). Per-parse read is fine — agents are re-parsed on the existing 60s cache TTL. Tests: tools.test.ts grows from 1 to 10 tests. Covers resolveToolTier across tiers/case/unknown values + the CORE-subset-of-STANDARD invariant + TOOLS_BY_NAME existence for both tier sets. 204/204 pass (was 195; +9 new). Deviation from the brief: the codecontext tools in the actual registry have NO codecontext_* prefix (the brief's STANDARD list assumed it). Used the actual names (get_codebase_overview, search_symbols, etc.). Module-load validation would have failed boot with the prefixed names. Smoke: with BOOCODE_TOOLS unset, agents return their full 12-tool whitelists. With BOOCODE_TOOLS=core in .env + container restart, the same agents narrow to 4 tools (find_files, grep, list_dir, view_file) — intersection of declared whitelist ∩ core tier. Reverted after confirmation. CLAUDE.md updated with BOOCODE_TOOLS in the Environment section's Optional list. .env.example gained a commented BOOCODE_TOOLS=all line with the per-tier token-cost table. ~110 LoC across 5 files (4 modified + 1 test expansion). Under the brief's ~30 LoC estimate for code; the test suite expansion drove most of the growth.
27 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
What is BooCode
Self-hosted single-user developer chat app. AI assistant with read-only file tools (view_file, list_dir, grep, find_files) running against a local llama-swap inference server. Sessions organized by project, with a multi-pane workspace (chat + file browser side by side).
Plus apps/booterm (second container, port 9501, bookworm-slim+glibc): Fastify + node-pty + tmux. Browser terminal panes WS to /ws/term/sessions/:sid/panes/:pid; per-session tmux session bc-<sid>, per-pane window term-<pid>. Shells drop privs to samkintop via gosu in tmux.conf default-command.
Commands
# Development (run in separate terminals)
pnpm dev:server # tsx watch, port 3000
pnpm dev:web # Vite dev server, port 5173 (proxies /api to :3000)
# Build
pnpm build # builds web then server
pnpm -C apps/server build # server only (tsc + copy schema.sql)
pnpm -C apps/web build # web only (vite)
# Type checking (no emit)
npx tsc --noEmit # project references (root)
npx tsc -p apps/web/tsconfig.app.json --noEmit # web app specifically
# IMPORTANT: root tsc --noEmit uses project references and can miss errors
# that the per-app tsconfig catches. Always verify with the per-app command
# when editing web code. The server build (pnpm -C apps/server build) is
# authoritative for server code.
# Production
docker compose build --no-cache boocode && docker compose up -d
Tests: pnpm -C apps/server test runs the vitest suite. No test harness on apps/web (adding it requires installing vitest as a new devDep). Vitest pinned to ^3 because Vite 5 / vitest 4 are incompatible. No linters configured. Vitest include glob is src/**/__tests__/**/*.test.ts (see apps/server/vitest.config.ts) — tests outside src/**/__tests__/ silently won't run; match the per-domain convention (apps/server/src/services/__tests__/foo.test.ts).
Architecture
Monorepo: pnpm workspaces with apps/server (Fastify + postgres), apps/web (React + Vite), and apps/booterm (Fastify + node-pty + tmux).
Server (apps/server/src/)
- Fastify with
@fastify/websocketand@fastify/static(serves built frontend) - postgres (porsager/postgres) with tagged-template SQL — no ORM. Schema in
schema.sql, applied on startup. LSP may false-positive onsql<Type[]>\...`generics; CLItsc/pnpm build` is authoritative. - Zod for request validation and config parsing.
Key services:
services/inference/— Public surface re-exported viainference/index.ts; callers import from./services/inference/index.jsexplicitly (NodeNext doesn't honor directory-index resolution). Layout:turn.ts(runAssistantTurn / runInference / createInferenceRunner; exportsInferenceFrame,InferenceContext,TurnArgs,StreamResult),stream-phase.ts(streamCompletion as a v1.13.1-A AI SDK adapter + executeStreamPhase),provider.ts(upstreamModel(baseURL, modelId)wrappingcreateOpenAICompatibleagainst llama-swap),tool-phase.ts(executeToolPhase; value back-edges into turn.ts for the runAssistantTurn recursion — cycle safe because deref at call time, not module top-level),sentinel-summaries.ts(runCapHitSummary + runDoomLoopSummary + their sentinel inserters),error-handler.ts(handleAbortOrError, finalizeCompletion),payload.ts(buildMessagesPayload, loadContext, maybeFlagForCompaction,OpenAiMessage),sentinels.ts(detectDoomLoop,DOOM_LOOP_THRESHOLD, sentinel predicates),budget.ts(resolveToolBudget),xml-parser.ts(qwen3.6 XML tool-call fallback — KEEP, AI SDK doesn't handle inline-XML tool calls),parts.ts(v1.13.0 dual-write helpers:partsFromAssistantMessage,partsFromToolMessage,insertParts),prune.ts(v1.13.4 two-tier compaction;selectPruneTargetsis the pure decision helper),types.ts(StreamPhaseState,DB_FLUSH_INTERVAL_MS).TurnArgsis the per-turn state envelope threaded through theexecuteToolPhase → runAssistantTurnrecursion; reset inrunInferenceat user-message boundary. Add new per-turn state toTurnArgs, not module-level closures.- AI SDK v6 streamCompletion adapter (v1.13.1-A;
services/inference/stream-phase.ts).streamTextis the underlying call; the BooCode layer above (executeStreamPhase, finalize, dual-write) is shape-preserved via an adapter. Five gotchas the LSP/test suite won't catch:- Abort signals are swallowed.
streamText'sfullStreamiterator exits cleanly whenabortSignalfires — no throw. Post-iterationif (signal?.aborted) throw <AbortError>is required; without it the row finalizes ascompleteinstead ofcancelled. Comment in stream-phase.ts pins this; don't refactor it away. - Usage lands only at stream end via
await result.usage(inputTokens/outputTokensv6 names → mapped topromptTokens/completionTokensfor the existing onUsage callback). Mid-stream live tok/s is gone vs v1.12.2; ChatThroughput shows a single value at stream end. - Tools have NO
executefield. BooCode dispatches tools in tool-phase.ts, not the AI SDK loop. Onlydescription+inputSchema: jsonSchema(parameters)— surfacing tool-call parts viafullStreamand stopping is what we want. includeUsage: trueMUST be set oncreateOpenAICompatibleinservices/inference/provider.ts. The adapter defaults it false, omittingstream_options.include_usagefrom the request body; llama-swap then never emits the usage block andresult.usage.inputTokens/outputTokensresolve toundefined. Latent regression from v1.13.1-A through v1.13.7 — every assistant row in that window hastokens_used/ctx_usedNULL. Don't remove this flag during refactor.- Tool-call-only turns may emit a leading
\ntext-delta as the assistant content.MessageList.flatten'shasTextandMessageBubble'shasContentboth.trim()before the length check — otherwise whitespace-only content renders an empty bubble + ActionRow between every tool call (v1.13.7 fix).payload.ts:buildMessagesPayloadalso skipsstatus='failed'AND complete-but-empty (no content, no tool_calls) assistant rows to avoid "Cannot have 2 or more assistant messages at the end of the list" upstream rejections after cap-hit + Continue.
- Abort signals are swallowed.
- AI SDK ModelMessage conversion (
toModelMessagesin stream-phase.ts). Tool messages need atoolNameforToolResultPart— BooCode's OpenAI-shape history doesn't carry it, so a forward-scan builds atool_call_id → toolNamemap from prior assistanttool_calls. Tool outputs wrapped as{ type: 'json' | 'text', value }matching the v6ToolResultOutputunion. Assistant messages with reasoning emit aReasoningPartfirst in the content array (v1.13.1-C). experimental_repairToolCall(v1.13.3) wired intostreamTextto keep the stream alive when qwen3.6 emits malformed tool args. Pass-through implementation — logs the bad call and returns it unmodified;executeToolPhase's existing zod-reject error path routes it to the model on the next turn.chat_statusframe shape (published viabroker.publishUser) —status: 'streaming' | 'tool_running' | 'waiting_for_input' | 'idle' | 'error'(widened fromworking|idle|errorin v1.12.1). FrontenduseChatStatusderivesidle_warm(<30s since idle) vsidle_cold.ChatThroughputrenders inline besideStatusDotonly when streaming or tool_running, fed by 500ms-throttled'usage'WS frames (completion_tokens+ctx_used+ctx_max). ThePOST /api/chats/:id/discard_staleendpoint exists to mark a stuck-streaming row asfailedwhen the frontend's 60s no-token-activity timer (ChatPanecontent-length watcher) gives up.- Boot-time stale-streaming sweep in
apps/server/src/index.tsafterapplySchema(): anymessages.status='streaming'older than 5 minutes flips to'failed'. Logs only on non-zero count. Recovers from container restart while inference was mid-stream (v1.12.1). - Periodic 60s sweeper in
apps/server/src/index.ts(v1.13.3 + v1.13.5). SamesetIntervalrunssweepStaleStreaming(marksmessages.status='streaming'older than 5 min asfailed, publisheschat_status='idle'so the UI dot drops) andcleanupTruncations(TTL + orphan reap of tmpfs truncation files).app.addHook('onClose')clears the timer. No-op when nothing to reap. services/broker.ts— In-memory pub/sub with two channel types: per-session (message streaming) and per-user (sidebar updates). No persistence; clients reconnect on restart.services/tools.ts— Tool registry (ALL_TOOLS,READ_ONLY_TOOL_NAMES,TOOLS_BY_NAME). Filesystem tools (view_file/list_dir/grep/find_files) go through three guard layers:path_guard.ts(workspace scope),secret_guard.ts(filename deny list),url_guard.ts(SSRF/private-IP block for web_fetch). v1.11.8+ web tools (web_search,web_fetch) are opt-in per chat viasession.web_search_enabled(resolved withproject.default_web_search_enabledfallback) and filtered out of the LLM's tool schema when false. v1.13.5 truncation: when a tool slice cuts content,services/truncate.tsstashes the full text on tmpfs atBOOCODE_TRUNCATION_DIR(default/tmp/boocode-truncations, 0o700) keyed by an opaquetr_<12 base32 chars>id, and theview_truncated_output(id)tool retrieves it. 5MB cap (matchesview_file'sMAX_FILE_BYTES), 7-day TTL, reaped by the periodic sweeper. Tmpfs path means container restart loses retrieval — acceptable, the model usually has moved on.services/compaction.ts+services/model-context.ts— v1.11.0 anchored rolling summary (singlesummary=trueassistant row per chat, supersedes itself on each compaction). Triggered whenchats.needs_compactionis set after an inference turn exceedsusable(ctx_max) = floor(0.85 × ctx_max)(v1.13.9 opencode-pattern early trigger; wasctx_max - 20kpre-v1.13.9, which gave only 7.6% headroom at 262k and 0 budget for ≤20k contexts).ctx_maxcomes frommodel-context.getModelContext()which fetches${LLAMA_SWAP_URL}/upstream/<model>/props— NOT fromparsed.timings.n_ctx(the stream completion'stimingsdoesn't carry n_ctx; that read was dead code until v1.11.3 ripped it out). First inferences after a boocode boot may havectx_max=NULLif llama-swap hasn't loaded the model yet; negative cache TTL is 60s, recovers on next turn. v1.13.6:buildHeadPayloadembedsreasoning_partsas a<reasoning>...</reasoning>prose prefix on the assistantcontent(OpenAI wire shape has no structured reasoning field; the summarizer reads text). Standalone tag when content is empty (tool-call-only turn).buildHeadPayload+OpenAiMessageexported for test access — keep them exported.services/system-prompt.ts—buildSystemPromptis the string-returning shim;buildSystemPromptWithFingerprintis the canonical impl returning{prompt, fingerprint, drift}. v1.13.8 instrumentation: SHA-256 of the assembled prefix is logged perbuildMessagesPayloadcall (msgprefix-fingerprint, level=info); aMap<sessionId, lastHash>observer firesprefix-drift(level=warn) on hash change with a field-levelchanged_inputsdiff. Smoke proved the prefix is byte-stable across turns in steady-state — the originally-plannedsystem_prompt_cacheDB table was dropped as redundant against the v1.12.0 input-layer mtime caches (BOOCHAT.md here + AGENTS.md global+per-project inagents.ts:safeStat).services/inference/budget.ts— tool-call budgets:BUDGET_READ_ONLY = 30,BUDGET_NON_READ_ONLY = 10(forward-looking; no write tools yet),BUDGET_NO_AGENT = 30(v1.13.7; was 15 — every tool inALL_TOOLSis read-only today, so no-agent mode shares the read-only-agent cap). Per-agentmax_tool_callsfrom AGENTS.md frontmatter overrides.messages_with_partsview (v1.13.1-B;schema.sql). Read sites that needtool_calls/tool_results/reasoning_partsSELECT from this view, NOTmessagesdirectly.COALESCEs parts-table rows over the legacy JSON columns, so pre-v1.13.0 history still resolves. Writes still targetmessages; the v1.13.0 dual-write intomessage_partskeeps both halves in sync. New payload-assembly code must use the view — callingmessages.tool_callsdirectly will miss anything written post-v1.13.1-B if the JSON column ever drifts (and dual-write makes that easy to miss). Shapes:tool_calls jsonb[],tool_results jsonbsingle object,reasoning_parts jsonb[]of{text}.services/file_ops.ts— Shared file operation implementations used by both inference tools and HTTP routes.services/auto_name.ts— Non-streaming LLM call to generate 4-word session titles after first assistant reply.
Route registration: all routes registered in index.ts via register*Routes(app, sql, ...) functions. Routes are in routes/*.ts.
Frontend (apps/web/src/)
- React 18 + React Router v6 + Tailwind v4 + shadcn/radix-ui primitives.
- Shiki for syntax highlighting (async
codeToHtmlinCodeBlock.tsxandFileViewerinFileBrowserPane.tsx). - Path alias:
@/maps tosrc/. - Mobile interaction primitives (post-v1.6):
useViewport(matchMedia, breakpoints mobile <768 / tablet 768–1023 / desktop ≥1024),useSidebarDrawer/useRightRailDrawer(Context + auto-close onuseLocation().pathnamechange),useLongPress(500ms timer, dispatches syntheticcontextmenuon[data-tab-id]),usePullToRefresh(80px threshold, 600ms hold),SwipeablePaneTab(60px close, 30px vertical bail). Tap-target convention:max-md:min-h-[44px] max-md:min-w-[44px]. Mobile headers:border-b px-3 sm:px-4 py-2+style={{ paddingTop: 'max(0.5rem, env(safe-area-inset-top))' }}. Hamburger left, FolderTree right.
Key patterns:
hooks/sessionEvents.ts— Module-singleton event bus (Set of listeners). Used for cross-component communication: session renames, file-open events, attachment dispatch. 9 event types in the discriminated union. When adding a new event type to theSessionEventunion, you must also add a case to theapplyEventswitch inuseSidebar.ts(even if it's a no-opreturn prev).hooks/useSessionStream.ts— WebSocket per session,applyFramereducer builds message list from streaming frames.hooks/useUserEvents.ts— Single app-level WS to/api/ws/userwith exponential backoff reconnect. Forwards frames onto the sessionEvents bus.hooks/useSidebar.ts— Module-singleton with Set subscriber pattern; one bus subscription guarded byglobalThis.__boocode_sidebar_subscribedfor HMR safety. Every newSessionEventtype needs acasein theapplyEventswitch (no-opreturn previs fine).api/client.ts— Centralized typed fetch wrapper. All endpoints underapi.*namespace.
Font / CSS pipeline (apps/web):
- Tailwind v4's
@import "tailwindcss"directive strips font URLs from subsequent CSS@imports —@fontsource*packages must be imported as JS side-effect modules inapps/web/src/main.tsx, not via@importinglobals.css. Otherwise the woff2 files never make it todist/. - Lightning CSS (inside
@tailwindcss/postcssv4) collapses contiguous unicode-ranges to wildcard shorthand (U+0000-FFFF→U+????), which iOS Safari/Vivaldi mishandles (silently drops the font from those codepoints). Use explicit non-wildcard-collapsible subranges (e.g.U+2500-259FnotU+2500-25FF). Theapps/webbuild script grepsdist/assets/*.cssforU+2500-259Fand fails the build if missing — preserve that guard. @font-faceblocks must live AFTER all@importstatements (CSS spec). Earlier placement silently breaks every subsequent@import(this broke the 18 theme palette imports in globals.css for one session).- JetBrainsMono Nerd Font self-hosted in
apps/web/src/fonts/(TTF from ryanoasis/nerd-fonts release) — needed because@fontsource-variable/jetbrains-monoships subsetted woff2s that don't coverU+2500-259F(box drawing + block elements, used by opencode's banner). "NL" = No Ligatures (matchesfont-feature-settings: "liga" 0); "Mono" = single-cell icon width so TUI layouts don't desync. - xterm-addon-webgl rasterizes glyphs via Canvas2D into a GPU texture atlas. Canvas2D does NOT honor
font-display: block— it uses whatever font is currently registered. Gate xterm initialization ondocument.fonts.load(<font-name>)resolving before callingterm.open()(seefontsReadyuseState inTerminalPane.tsx). iOS Safari/Vivaldi also reclaims WebGL contexts from backgrounded tabs: keepwebgl.onContextLoss(() => webgl.dispose())+ recreate via visibilitychange. Do NOT manually dispose+recreate the addon after font load — iOS silently fails the second GL context creation and the terminal drops to DOM renderer with stale metrics.
Data flow for chat
- User sends message → POST
/api/sessions/:id/messagescreates user + assistant (status=streaming) rows inference.enqueue()starts async streaming loop- LLM deltas published via
broker.publish(sessionId, frame) - Client's
useSessionStreamWS receives frames,applyFramereducer updates message list - Tool calls: inference executes tools server-side, publishes tool_call/tool_result frames, loops back to LLM
- Terminal states (complete/error): DB updated with final content + token counts,
session_updatedframe published on user channel
Multi-pane workspace
Sessions hold 1–5 panes (chat / empty / placeholder terminal+agent). v1.12.1 moved pane state from per-device localStorage to sessions.workspace_panes jsonb for cross-device sync. PATCH /api/sessions/:id/workspace persists; session_workspace_updated user-channel frame broadcasts to every device watching the session. useWorkspacePanes debounces saves 300ms and dedups echoes by JSON string. Legacy localStorage key boocode.workspace.panes.<sessionId> is read once on first hydrate (one-time seed-and-delete migration when server is empty but localStorage has data); no longer written. The deprecated session_panes table was dropped. validatePanes(validChatIds) prunes panes referencing chat IDs that no longer exist (called by useSessionChats after the chat list fetch lands). Each chat lives in at most one pane; tab strip is per-pane and tracks chatIds[] + activeChatIdx. Tab reorder via native HTML5 drag events.
Database
PostgreSQL 16. Tables: projects, sessions, chats, messages, settings. (session_panes was dropped in v1.12.1; workspace pane state lives in sessions.workspace_panes jsonb.) Schema applied idempotently on startup via applySchema(). Use clock_timestamp() (not NOW()) inside transactions. CHECK constraints in place: projects_status_chk ('open'|'archived'), sessions_status_chk (same), chats_status_chk (same), messages_role_chk, messages_status_chk — keep in sync with the *_STATUSES const arrays in apps/server/src/types/api.ts. The older anonymous messages_status_check (without 'cancelled') and messages_role_check (without 'system') were dropped in v1.12.1; only the _chk variants remain.
Schema CHECK migration order when renaming allowed values: (1) ALTER TABLE ... DROP CONSTRAINT IF EXISTS <system_name> (inline CREATE TABLE checks get <table>_<column>_check), (2) UPDATE rows to new values, (3) wrap new constraint ADD in DO $$ ... pg_constraint guard — that block is the only way to get ADD CONSTRAINT IF NOT EXISTS.
Environment
Required: DATABASE_URL, LLAMA_SWAP_URL. Optional: PORT (3000), HOST (0.0.0.0), PROJECT_ROOT_WHITELIST (/opt, read-only scope for add-existing path resolution), BOOTSTRAP_ROOT (/opt/projects, writable scope for create-new-project bootstrap mkdir target — host must mkdir -p /opt/projects before container start), DEFAULT_MODEL, LOG_LEVEL, SEARXNG_URL (default http://100.114.205.53:8888 — internal Tailscale Fathom; the public search.indifferentketchup.com is behind Authelia and unusable from server context), BOOCODE_TOOLS (core | standard | all, default all; v1.13.15-tools tier filter — ceiling, never expands an agent's whitelist).
Workflow
- Sam reviews all diffs and commits manually. Do not commit unless explicitly asked.
- Per-batch docs live under
openspec/changes/<slug>/{proposal,tasks,design}.md. Already-shipped batches are snapshots inopenspec/changes/archived/. New batches follow the proposal+tasks shape; seeopenspec/README.mdfor the convention. - Deploy:
cd /opt/boocode && docker compose up --build -d(ordocker compose build --no-cache boocode && docker compose up -dif you suspect a layer-cache issue). - Git push to Gitea:
GIT_SSH_COMMAND="ssh -i /opt/boocode/secrets/boocode_gitea -o IdentitiesOnly=yes" git push origin <branch>. The default agent identity is rejected; the in-repo deploy key (secrets/, gitignored) is the working one. TransientConnection reset by peerretries cleanly aftersleep 5. - Don't accumulate
.bak-*files. Clean them up in the same batch or immediately after merge. - Fastify global JSON parser tolerates empty bodies (overridden in
index.ts); bodyless POSTs (archive, unarchive, stop) work without settingContent-Typetricks on the client. - Event dedup discipline: for any mutation the server publishes via
broker.publishUser, do NOT add a localsessionEvents.emit(...)after the API call —useUserEventsforwards the WS frame onto the bus. Frontend mutation handlers must be idempotent (dedup by id, no-op on already-present). node:20-*base images ship anodeuser at uid/gid 1000 — delete it (userdel/groupdelon debian,deluser/delgroupon alpine) before adding samkintop at 1000.- node-pty's compiled
.nodeis libc-specific: proddeps and runtime Dockerfile stages must share libc (alpine↔musl or bookworm-slim↔glibc); the TS-only builder stage can stay alpine for speed. - pnpm 10
--frozen-lockfileskips node-pty's postinstall — the Docker proddeps stage runscd node_modules/node-pty && npm run installto force the native compile. - A local PreToolUse hook (
security_reminder_hook.py) regex-flags Node's olderchild_processspawn helpers as unsafe (false positive even on the File-suffixed variant). Usespawn— it's accepted. /opt/boolabhosts a working sibling BooCode terminal atboocode.indifferentketchup.com. Useful for visual side-by-side comparison on the same iPhone when debugging booterm rendering. Boolab uses Tailwind v3 (@tailwind base); boocode uses v4 — many subtle build differences. Don't assume parity.- booterm SSHs to the host as
samkintop@100.114.205.53(the Tailscale IP). The hostnameubuntu-homelab(shown in the bash prompt after login) does NOT resolve from inside the container — only the host's/etc/hostsknows it. Override viaBOOTERM_SSH_HOST/BOOTERM_SSH_USERenv vars in docker-compose if you ever move the shell to a different machine. - codecontext sidecar lives at
/opt/boocode/codecontext/. Sidecar HTTP API athttp://codecontext:8080/v1/<tool_name>over theboocode_netbridge (no host port). BooCode wrappers inapps/server/src/services/tools/codecontext/. The.codecontextignore.templatedocuments recommended ignore patterns; users copy and adapt to project root manually. os/execchild supervisors must explicitly callchild.Wait()in a goroutine andos.Exiton child death.Signal(0)returns nil on zombies and is NOT a liveness check. WithoutWait(), docker'srestart: unless-stoppedpolicy never fires because the parent stays alive. Thecodecontext/shim.goimplementation is the reference pattern.
Conventions
overflowWrapnotwordWrap— TypeScript's CSSStyleDeclaration markswordWrapas deprecated (error 6385).- No app-layer auth. Authelia handles auth at the reverse proxy. All
broker.publishUser/subscribeUsercalls use'default'as the user key. - TypeScript strict mode. Both apps share
tsconfig.base.json. - Server uses NodeNext module resolution (
.jsextensions in imports). - Discriminated unions for type narrowing:
Pane(bykind),SessionEvent(bytype),InferenceFrame(bytype). - Adding a new WS frame type requires updating BOTH the server's
InferenceFrame(loosetype:union + optional fields inservices/inference/turn.ts) AND the webWsFrame(strict discriminated union inapps/web/src/api/types.ts). Server publish is permissive; the frontend type is the wire-format gate. The'usage'frame added in v1.12.2 needed both sides; missing the web side silently drops the frame at JSON-parse. - shadcn primitives live in
components/ui/. Don't modify them unless adding a new primitive. inferLanguage()fromlib/attachments.tsis the canonical file-extension-to-language map.CodeBlock.tsxkeeps its ownLANG_MAPbecause it also resolves markdown fence names.- Two UI event buses:
hooks/sessionEvents.tsfor DB-state events (chat_created, session_updated);lib/events.tsfor ephemeral UI (sendToTerminal,terminalsRegistry). Don't merge — different subscriber lifecycles. vite.config.tsproxy entries are order-sensitive: more-specific prefixes (/api/term,/ws/term) must come BEFORE/api.- Mobile pane URL sync (
Session.tsx): the?pane=<id>effect resetsactivePaneIdxwheneverpaneschanges. New-pane creation on mobile must push?pane=atomically —addPaneAndSwitchis the wrapper that does this.addSplitPanereturns the new pane id for callers. - xterm.js v5 uses canvas rendering — browser doesn't see xterm's selection; the native right-click menu has no working Copy for terminal text. App keybindings (
Cmd/Ctrl-C,Cmd/Ctrl-Shift-C) are the path. - New tools live in their own
services/<name>.tsfile (seeweb_search.ts,web_fetch.ts) — exports a pureexecuteFoo(input, ...deps)for direct test access plus aToolDefwrapper thatloadConfig()s its real dependencies. Register the ToolDef intools.tsALL_TOOLS(andREAD_ONLY_TOOL_NAMESif applicable). Injectfetcher: typeof fetch = fetchrather thanvi.spyOn(globalThis, 'fetch')— cleanup is simpler and the production call site stays unchanged. - Sentinels are
role='system'rows with structuredmetadata.kind(cap_hit,doom_loop). UI-only —buildMessagesPayloadstrips them viaisAnySentinelso the LLM never sees them. A new kind requires arms inMessageMetadatain BOTHapps/server/src/types/api.tsANDapps/web/src/api/types.ts, plus a render branch inapps/web/src/components/MessageBubble.tsx. - ReadableStream test stubs use
pull()(notstart()) so chunks are produced lazily —start()enqueues everything and callscontroller.close()before the consumer reads, so a subsequentreader.cancel()finds the stream already closed and thecancel()callback never fires. Also provide MORE chunks than the test will consume so the source stays in 'readable' state when cancel runs (e.g. cap test reads ~6 chunks, stub provides 10). - Tool-name whitelists must derive from
ALL_TOOLSinservices/tools.ts, never hardcoded.services/agents.tsALL_TOOL_NAMEShad this drift class until v1.12 — same pattern applies to any future tool-aware code. - Agent registry lives at
data/AGENTS.md(global, bind-mounted at/data/AGENTS.md). No per-projectAGENTS.mdin this repo — removed in v1.12 to eliminate the two-files-must-stay-in-sync drift. ThegetAgentsForProjectper-project override mechanism remains for other projects. - MCP stdio transport uses newline-delimited JSON (NDJSON), NOT LSP-style
Content-Lengthheaders. Thecodecontext/shim.goframing implementation is the reference; per the MCP spec (modelcontextprotocol.io/specification/server/transports).