TypeScript
docs: put the Effect and web UI review rules in the docs (#16286)
Erik Thorelli6262e5f50081
diff --git a/AGENTS.md b/AGENTS.mdindex fae5e1c7e21..ef6dbcff766 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -151,7 +151,7 @@ Architecture and its constraints: `docs/internals/overview.md`. Glossary: `docs/- Complexity belongs at the adapter boundary. Orchestration stays pure, UI stays dumb.- Server features are services; transports stay thin. A `ws.ts` RPC handler, HTTP route, or MCP tool decodes input, calls one service method, and maps errors. See [Effect services](docs/internals/effect-services.md).-- `apps/web/src/components/ui` exports own their look. Pick a `variant` or `size`; do not restyle one with `className`. If none fits and the look is a generic concept, add a variant to the component; a look that belongs to one feature stays in that feature's own component, not in `components/ui`. Layout classes (width, flex, margin, position) belong on the parent. `shadcn/no-restyle` fails lint on violations.+- `apps/web/src/components/ui` exports own their look. Pick a `variant` or `size`; do not restyle one with `className`. If none fits and the look is a generic concept, add a variant to the component; a look that belongs to one feature stays in that feature's own component, not in `components/ui`. Layout classes (width, flex, margin, position) belong on the parent. `shadcn/no-restyle` fails lint on violations. See [Web UI](docs/internals/web-ui.md).- Inferred types over annotations. `any` is the enemy.- Comments describe how a thing is used, and move when the code moves. To be used mostly to describe functions, not to annotate every line of behavior.- Our users drive agents all day and notice a dropped frame, a lying spinner, and a stale label. No continuously repainting animations; they peg the GPU on high-refresh displays.diff --git a/docs/internals/effect-services.md b/docs/internals/effect-services.mdindex 7ae4671d2e6..49a7d2424ca 100644--- a/docs/internals/effect-services.md+++ b/docs/internals/effect-services.md@@ -13,8 +13,8 @@ A server capability is a method on a service in its domain folder (`project/`, `does.A transport handler does three things: decode the request, call one service method, and map the-service's typed errors to the transport's error. Nothing else. Filesystem, Git, or process work,-folder naming, multi-step dispatch, retries, and rollback belong in the service.+service's typed errors to the transport's error. Nothing else. Filesystem, Git, process, or+persistence work, folder naming, multi-step dispatch, retries, and rollback belong in the service.The reason is reach. Users trigger a capability from the WebSocket, agents reach it through MCPtools, and scheduled tasks and the CLI run it too. Logic written into one handler is missing from@@ -64,21 +64,76 @@ const make = Effect.gen(function* () {export const layer = Layer.effect(Foo, make);```-- **Imports.** Consumers use the module as a namespace: `import * as Foo from "./Foo.ts"`, then- `yield* Foo.Foo` and `Foo.layer`. Never `import { layer as fooLayer }`.-- **Dependencies** come from the environment (`yield* FileSystem.FileSystem`), never as parameters to- `make`.-- **`make`** stays private unless another module imports it. Knip fails CI on an unused export.-- **Errors** are `Schema.TaggedError` classes with structured attributes and a `cause` when they wrap- a failure. The message is fixed or built from attributes, never from `cause`. Construct the error- where the failure happens; map it to a transport error only in the transport. Catch known tags- with `Effect.catchTags`.+- **Imports.** Import Effect modules as namespaces from their subpaths:+ `import * as Effect from "effect/Effect"`, never `import { Effect } from "effect"`. Consumers use+ a service module the same way: `import * as Foo from "./Foo.ts"`, then `yield* Foo.Foo` and+ `Foo.layer`. Never `import { layer as fooLayer }`. Named imports are fine for packages like+ `@t3tools/contracts` and for modules used only for a pure helper, error, schema, config value, or+ type. A barrel exposes a whole service module as `export * as TokenStore from "./tokenStore.ts"`,+ not as renamed `make` and `layer` exports.+- **Interface.** No standalone `FooShape`; name the type `Foo["Service"]`.+- **Dependencies** come from the environment (`yield* FileSystem.FileSystem`), never as parameters+ to `make`, so the types of `make` and `layer` show what they need. Never hide one in a module+ global, a closure over a singleton, or a `Layer.succeed` that calls runtime-backed or imperative+ APIs. Tests may pass service instances directly. Configuration, immutable values, and deliberate+ callbacks are fine as parameters; they aren't services.+- **`make`** exists when the module owns construction and stays private unless another module+ imports it. Knip fails CI on an unused export. Don't write `make = Effect.succeed(...)` to force+ `Layer.effect`; use the constructor that fits, like `Layer.succeed` or `Layer.sync`.+- **Names.** A module named for its implementation uses plain `make` and `layer`+ ([`NodePtyAdapter.ts`](../../apps/server/src/terminal/NodePtyAdapter.ts)). A port module that also+ holds implementations names them, like `makeCloudflaredRelayClient` and `layerCloudflared`.+- **Moves.** Moving a service deletes the old files and updates every consumer, including+ orchestration, MCP, tests, and integration harnesses. No re-export shims.- **Tests** exercise behavior through the service, with test layers only for external dependencies.Don't mock the logic under test.+## Runtime boundaries++`ManagedRuntime.make`, `runPromise`, and `runPromiseExit` belong at application and framework+boundaries: React, native callbacks, the CLI, HTTP adapters. Never in a domain service, repository,+persistence code, or service constructor. A named adapter may bridge a service into a Promise API,+but no Effect service depends on it.++Compose a shared resource once in an application-owned layer and provide its context to integration+runtimes. Don't create a managed or Atom runtime per feature to hand it out. When acquisition can+fail and callers need a fallback, keep the failure typed: an error on the operation or an explicit+optional-service layer. Don't route around the layer with an imperative runtime.++## Errors++- **Attributes.** Failures are `Schema.TaggedError` classes with structured attributes: the+ operation or stage, the resource path or entity id, a normalized category or status. The message+ is fixed or built from those attributes, never from `cause`, `cause.message`, or a stringified+ defect. No `detail` field that copies `cause.message`.+- **Cause.** An error that wraps a failure keeps the immediate underlying error as `cause`; make it+ required when every construction wraps one. Validation and domain errors with nothing underneath+ have none.+- **Safe values.** Attributes and log annotations stay bounded: no raw payloads, command arguments+ or output, signed URLs, credentials, query strings, or arbitrary defect text. The exact value+ lives only in `cause`. Expose a category, length, count, or a URL's protocol and host instead.+- **Translation.** Construct the error where the failure happens, and map it to a transport error+ only in the transport. A translation boundary passes through domain errors already in the target+ channel and wraps only unknown or lower-level failures. Map each failure where its context is+ known; don't wrap a whole pipeline in one generic error.+- **Discriminators.** Don't encode one distinction twice, as a specific tag and a single-value+ `operation`, `reason`, `kind`, or `phase` literal. Split classes when the distinction drives+ control flow or the user-facing message; a field that only helps diagnostics stays a field. A+ message that reaches HTTP, RPC, persisted state, or the UI is behavior, and a refactor keeps it.+- **Mappers.** Don't write a helper that only does `(...args) => new SomeError({ ...args })`. Keep+ a mapper only when it normalizes, passes domain errors through, or adds context. A mapper that+ belongs to the target error is a static factory on that class.+- **Predicates** are exported directly as `export const isFoo = Schema.is(Foo)`, not a function+ wrapping a private `Schema.is`.+- **Catching.** Catch known tags with `Effect.catchTags({ ... })`, even for one tag, not `catchTag`+ or `catchIf` with a schema predicate. `Effect.catch` is for handling the whole channel; `catchIf`+ is for structural checks like a platform error code.+## Before you push- Does any handler you touched do more than decode, call, and map errors?- Could an agent (MCP) or a scheduled task use this capability? If not, is that deliberate?- Did you extend the domain's existing service before adding a new one?- Did you run knip? A new export with no importer fails it.+- Does every directive you added that disables a lint, type-checker, or LSP diagnostic say why, in+ a `-- reason` suffix or a comment above it?diff --git a/docs/internals/web-ui.md b/docs/internals/web-ui.mdnew file mode 100644index 00000000000..8f8213cc7dc--- /dev/null+++ b/docs/internals/web-ui.md@@ -0,0 +1,39 @@+# Web UI++## Primitives++Controls come from [`components/ui`](../../apps/web/src/components/ui).+[AGENTS.md](../../AGENTS.md#taste) says how to use and extend them.++Don't style a raw `<button>`, `<input>`, `<select>`, or `<textarea>` to look like a control a+primitive provides. Raw elements are right when the element isn't that control: semantic rows, tabs,+resize handles, swatches, editor surfaces, anything whose behavior or geometry differs by design.++## Styles++Style with Tailwind classes in the component that renders the element.++- Global CSS, in [`index.css`](../../apps/web/src/index.css) or any other stylesheet, is only for+ what classes on the owning component can't express: generated Markdown or imperative DOM, custom+ elements and shadow roots, animations, runtime theme variables, and browser or Electron+ integration.+- No `style={{ ... }}` or inline `<style>` for static values a class covers. Values computed at+ runtime are fine.+- Theme-only rules use `@variant dark` and `@variant light`, not `.dark` selectors.++## Composition++The composer banners are the reference.++- [`ComposerBanner.tsx`](../../apps/web/src/components/chat/ComposerBanner.tsx) exports one object+ of small slots. Slots own their classes, take `className`, and spread the rest of their props,+ merging classes with `cn`. Variants and density are props on `Root`. A slot that needs a control+ renders `Button`.+- [`ComposerBannerStack.tsx`](../../apps/web/src/components/chat/ComposerBannerStack.tsx) composes+ the slots and owns only stack behavior: priority, expand and collapse, dismiss transitions.+- Consumers are a few lines of slots, and the slots carry the styling:+ [`ComposerActivityStatus.tsx`](../../apps/web/src/components/chat/ComposerActivityStatus.tsx).++Build new UI the same way. Compose or extend a component instead of copying its markup and classes.+When a call site repeats a treatment another one already uses, make it a variant or a slot on the+shared component. A new composer banner or notice uses the `ComposerBanner` slots.