Compare commits

..
Author SHA1 Message Date
Codex 54f3d19afb fix(mcp): ограничить доступ к журналу и уточнить подтверждение маршрута 2026-11-14 18:28:44 +07:00
Codex cc190a381c fix(mcp): устранить конкуренцию фоновых записей при опросе 2026-11-14 15:37:11 +07:00
Codex 2ac36a6ca5 fix(mcp): защитить операции обслуживания от блокировок строк 2026-11-13 19:10:50 +07:00
Codex cffcfada88 feat(mcp): расширить управление маршрутизацией, доступом и обслуживанием базы 2026-11-13 13:37:55 +07:00
Codex 9240e8be11 fix(netflow): связать отметки потока SSE с диагностикой NetFlow 2026-11-12 17:46:59 +07:00
Denozordec d79fb3815a fix(mcp): упорядочить записи журнала опроса 2026-11-12 10:23:34 +07:00
Codex 765b9a1658 fix(netflow): читать активные записи на одном снимке 2026-11-12 08:40:06 +07:00
Codex 5d181e7cb0 fix(netflow): пропустить загрузку флага неизвестной страны 2026-11-11 23:46:51 +07:00
Codex cf211f7418 fix(netflow): восстановить открытие потока после временного ограничения 2026-11-11 22:54:42 +07:00
Codex 04225886e0 fix(mcp): довести базовые сценарии и проверку маршрутизации 2026-11-11 20:28:37 +07:00
Denozordec 1f0704add0 fix(mcp): включить проверяемый контроллер маршрутизации 2026-11-05 05:36:19 +07:00
Codex 6569dbfc60 fix(mcp): привязать OAuth-согласие и завершать чтение логов при обслуживании 2026-11-05 03:56:17 +07:00
Denozordec 78b268cc39 fix(wireguard): безопасно повторить чтение зависимостей 2026-11-05 01:39:26 +07:00
Denozordec 3d5b7f0ea6 fix(traffic): загружать данные потоков последовательно 2026-11-04 23:51:03 +07:00
Denozordec b4d2d4bc20 fix(mcp): ограничить очередь NetFlow и защитить чтение истории 2026-11-04 21:58:22 +07:00
Denozordec c90a6ca3b8 fix(test): сравнивать тела миграций с учётом окончаний строк 2026-11-04 19:09:45 +07:00
Denozordec bf4b9b5443 fix(mcp): уточнить переключение маршрутов и хранение операций 2026-11-04 19:04:51 +07:00
Denozordec 5ce56b5168 fix(mcp): обновить проверенный снимок маршрутизации и хранения операций 2026-11-04 17:14:57 +07:00
Denozordec f1af1a85e5 fix(mcp): применять права к прямым переходам и проверять маршрутизацию 2026-11-04 01:57:07 +07:00
Denozordec 414a419f18 fix(ci): синхронизировать состав оператора базы для образов 2026-11-03 23:56:14 +07:00
Denozordec 6a286a1f66 fix(mcp): обновить снимок приёмки маршрутизации и базы данных 2026-11-03 23:38:19 +07:00
Denozordec bf3d50e1ff feat(mcp): подготовить сервер и доступ к приёмке 2026-11-03 03:12:03 +07:00
1512 changed files with 2455 additions and 408494 deletions

No files matched your search

-67
View File
@@ -1,67 +0,0 @@
---
name: reui
description: Use the ReUI registry from your AI agent - find, install, and correctly use ReUI components (the 22 free building blocks like data-grid, kanban, filters), their free examples, premium blocks, and Motion Icons. Applies in any project using ReUI, the @reui registry, REUI_LICENSE_KEY, or any shadcn project where the user asks for premium blocks, data grids, kanban boards, dashboards, or full pages. Pairs with the free ReUI MCP server for live, scored registry search and inline component APIs.
user-invocable: false
allowed-tools: Bash(npx shadcn@latest *), Bash(pnpm dlx shadcn@latest *), Bash(bunx --bun shadcn@latest *)
---
> **ReUI skill version `fe6742e29c`.** If the ReUI MCP's `get_agent_skill` reports a newer `version`, re-run the ReUI installer (see `get_agent_skill` -> `install.recommended`) to update this skill. Cloud/tools-only agents have no local file and always read the latest - they can ignore this.
# ReUI for Agents
ReUI is a shadcn-compatible registry. It ships four things you **reuse** - never redesign:
- **components** - the 22 ReUI building blocks with real APIs: `data-grid`, `kanban`, `filters`, `date-selector`, `tree`, `stepper`, ... (free)
- **examples** - free `c-*` single-pattern use-cases of a component (`c-kanban-1`); install one and read it to see exact composition
- **blocks** - premium full-page sections that compose components (`data-grid-2`, `pricing-page-1`); Pro or Ultimate license at install
- **icons** - Motion Icons in 4 styles, static + hover-animated variants; Ultimate license at install
The skill is free and this MCP is free to use; it just needs a ReUI account. On first use your agent opens a browser "Sign in with ReUI" prompt (a free account is created if you don't have one). Free covers components and examples with a daily request allowance; a Pro or Ultimate license unlocks premium blocks and Motion Icons and removes the limit (see [rules/registry.md](./rules/registry.md)). The same account and skill work in every agent and service the MCP connects to - this skill is agent-agnostic.
Skill + MCP are a team: this skill is the workflow (how to find, install, read the API, and adapt by reuse); the MCP is the live data and the hands (search, get_component, install commands). Your job: find the right item, install it with the shadcn CLI, read its real API, and **adapt by reuse** - wire real data and theme it; do not hand-roll or restyle what ReUI already provides. This skill **layers on the shadcn skill**: follow that for generic rules (spacing, `cn()`, semantic colors, forms); follow this for everything ReUI-specific.
## The core loop (MCP-native)
1. **Find** - call the ReUI MCP `search` tool with the user's intent. It returns a ranked, scored list across components/examples/blocks/icons, each with an `install` command, `previewUrl`, `docsUrl`, and `componentsUsed`. Pass hints (`type`, `component`, `category`, `features`, `free`) when you can infer them.
2. **Install** - run the returned command non-interactively (`npx shadcn@latest add @reui/<name> --yes`). The CLI resolves deps, aliases, and the base/style from `components.json`. See [cli.md](./rules/cli.md).
3. **Read the API (on your base)** - first note your base from `components.json` -> `style` (`base-nova` -> Base UI, `radix-nova` -> Radix UI). For each component an item uses, call `get_component(name)` and read its **inline `api`** (no web fetch); then `get_examples(name)` to install a worked example and copy its composition - the installed files are already in your base. Whenever you work with a component's API, also **share its `docsUrl`** (the primitive's API documentation page) with the user so they have the full reference. See [components.md](./rules/components.md).
4. **Adapt (reuse-first)** - swap demo data for real data, fix icon imports, align tokens. Do not redesign. See [adapting.md](./rules/adapting.md).
**Always show the preview.** Every item a tool returns carries a `previewUrl` (a live preview page). Whenever you list, recommend, or present ReUI items to the user - blocks, components, examples, or icons, whether from `search`, `search_icons`, `list_components`, `compose_page`, or any getter - include each item's `previewUrl` so they can SEE it before installing. Blocks and examples open an individual live preview; icons and components link to their live category/component page. Never present an item without its preview link.
If the ReUI MCP is not configured, fall back to `npx shadcn@latest search @reui -q "..."` then `add` - but the MCP gives scored matches + inline APIs; prefer it.
## Commands
Run ReUI as explicit slash commands (via the ReUI MCP) **or** just ask in plain language - both run the same workflow.
| Command | Invoke | Does |
| ----------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------ |
| **build** | `/mcp__reui__build <what>` | Compose a page/section/feature from ReUI: plan → install → read API → adapt → craft → audit. |
| **add** | `/mcp__reui__add <item>` | Find & install one component/example/block/icon and wire it in. |
| **fix** | `/mcp__reui__fix [target]` | Diagnose & fix ReUI usage: wrong/undocumented props, base/radix mismatch, missing states, a11y/scroll. |
| **improve** | `/mcp__reui__improve [target]` | Refine + extend existing ReUI UI to a production-exceptional bar (hierarchy, density, states, responsive, motion). |
Invocation differs slightly per agent (`/mcp__reui__build` in Claude Code/Cursor/Windsurf, `/mcp.reui.build` in VS Code). No command surface? Just describe what you want - this skill drives the identical loop.
## When to reach for ReUI vs plain shadcn
| Need | Reach for |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| A full page or section (dashboard, billing, auth, pricing, settings) | `compose_page` first (plans sections + best blocks), then ReUI **blocks** |
| A data table with sorting/filtering/pagination/virtualization | the **data-grid** component (never hand-roll a `<table>`) |
| A drag-and-drop board | the **kanban** component |
| Advanced column filtering, date range, tree, stepper, ... | the matching ReUI **component** |
| A single generic control already in shadcn (Button, Dialog, Select) | plain **shadcn** |
## Detailed references
- [rules/registry.md](./rules/registry.md) - the four types, the @reui registry, base/radix, free vs premium + license
- [rules/workflow.md](./rules/workflow.md) - the find -> install -> read-API -> adapt loop (most important)
- [rules/components.md](./rules/components.md) - the 22 components, the data-grid contract, base vs radix
- [rules/adapting.md](./rules/adapting.md) - reuse-first: preserve the design (no over-customizing), reuse examples + a block's own elements, real data, don't invent APIs
- [rules/craft.md](./rules/craft.md) - make it exceptional: point of view, hierarchy, density, states, responsive, motion, the bar
- [rules/quality.md](./rules/quality.md) - security, accessibility, and scroll gates (the done gate)
- [rules/styling.md](./rules/styling.md) - ReUI extended tokens, theme adaptation, density
- [rules/icons.md](./rules/icons.md) - portable icons, swapping imports, Motion Icons (static + animated)
- [tools.md](./tools.md) - the ReUI MCP: golden path, the 19 tools, token rules, result shapes, errors
-43
View File
@@ -1,43 +0,0 @@
# Adapting installed ReUI code (reuse-first, no AI slop)
ReUI items ship production-quality. Your job is to **adapt by reuse** - wire real data and fit the app - not to redesign or hand-roll. The output should look like ReUI built it for this product.
## Preserve the design - don't over-customize
The design IS the product. A ReUI block/component encodes senior-designer decisions: spacing, hierarchy, density, color treatment, and component choices. The fastest way to turn a premium block back into generic AI slop is to "improve" its look - so don't.
- Change **data, copy, and props**; keep the **structure and styling** it ships with. Make the **smallest** change that wires the real data. If your diff touches `className` / JSX structure more than data / props, you are over-customizing - stop and reuse.
- Don't swap ReUI components for hand-rolled ones, restructure the layout, re-skin spacing / radius / colors, or add decorative chrome. Let the installed components carry the default spacing, radius, sizing, icon rhythm, density, and state styling; add custom Tailwind only when a component genuinely lacks a contract you need.
- Want a different look? `search` for a block whose design already fits and reuse that - don't restyle this one into a new design.
## Reuse the parts: examples and the block's own elements
- **Examples are building parts.** A free `c-*` example is a correct, single-pattern composition you can reuse. Before composing from scratch, `get_examples(component)`, install the closest one, and reuse its wiring - assemble UI from examples instead of hand-rolling what an example already shows.
- **Reuse a block's own elements.** Need more rows, cards, items, or sections than ship by default? Repeat the block's **existing** element by mapping real data through the same markup - never invent parallel markup that drifts from its design. Need a variant (empty / loading / expanded)? Derive it from an element the block already has.
## Don't invent (read, don't guess)
- Never write a prop, variant value, import path, or `@reui/...` name you didn't read in a component's inline `api`, an installed example, or a `search` result. If you didn't see it, treat it as nonexistent - call `get_component` / `get_examples` / `search` first, or run the MCP `validate_usage` tool to check planned names + props against the docs before writing code.
- If a getter returns `found: false` or `search` returns nothing, say so and fall back (plain shadcn, or ask) - never fabricate an install command or an API.
## What to change vs leave alone
- **Change:** the item's own data, copy, props, and layout to fit the app.
- **Leave alone:** installed component files, hooks, and the shared theme - do not edit vendored ReUI internals; change behavior through props and the documented API.
- Blocks are **portable React** - no `next/link`, `next/image`, or other framework-runtime imports inside them. Keep them portable.
## Demo data -> real data
- Replace every placeholder with the user's real data. Model it as **typed data structures** and **map over arrays** - never duplicate JSX per row/card. Keep small block-specific formatters next to the data.
- Wire the real source (columns, fields, fetch). For `data-grid`, implement the server fetch contract if the user needs server-side data.
- **Type from the component API, derive during render.** Type domain state through the component's own types - e.g. map status to `BadgeProps["variant"]` via a typed `Record<Status, …>` - instead of stringly-typed values. Compute view state during render; don't mirror derived data into `useState`/`useEffect`.
- **Adapt on the right base.** Use the API for the project's base (Base UI vs Radix - see [components.md](./components.md)); the installed files are already base-correct, so reuse their shape rather than translating from memory.
## Believable content (no AI tells)
- Use realistic labels, counts, timestamps, and statuses that map to a real workflow.
- No decorative buttons, fake tabs, meaningless toggles, equal-weight card walls, empty gradients, ornamental icons, or generic SaaS filler. Every element should do something.
## Operational surfaces (settings / profile / admin)
Pick ONE archetype and keep the family consistent: a vertical rail (3-6 sections), horizontal tabs (5-8), or a frame/stack. Prefer `frame` for tool-like surfaces, a card for profile-like ones. Don't mix archetypes in one surface.
-60
View File
@@ -1,60 +0,0 @@
# CLI: registry setup, license, non-interactive install
## Registry setup (one-time, per project)
Free items (the 22 components and all `c-*` examples) need only the plain string registry in `components.json`:
```json
{ "registries": { "@reui": "https://reui.io/r/{style}/{name}.json" } }
```
Premium items (blocks; Motion Icons and templates) require a ReUI license at install:
1. Add the key to `.env.local`:
```bash
REUI_LICENSE_KEY=your-license-key
```
2. Switch `components.json` to the authenticated object form:
```json
{
"registries": {
"@reui": {
"url": "https://reui.io/r/{style}/{name}.json",
"headers": { "Authorization": "Bearer ${REUI_LICENSE_KEY}" }
}
}
}
```
The shadcn CLI expands `${REUI_LICENSE_KEY}` from `.env.local` inside `components.json`, but an MCP client config never expands variables, so a ReUI MCP server config must carry the raw token instead (for example `reui_pat_your_token_here`).
The MCP `get_project_context` tool returns the right config. Full guide: https://reui.io/docs/registry?ref=skill
## Installing
Use the project's package runner (check `packageManager`):
```bash
npx shadcn@latest add @reui/<name> --yes # npm
pnpm dlx shadcn@latest add @reui/<name> --yes # pnpm
bunx --bun shadcn@latest add @reui/<name> --yes # bun
```
`--yes` skips confirmation prompts. The CLI auto-detects the package manager from the lockfile (there is no `--package-manager` flag). It also resolves the correct base+style variant from `components.json`, so do not pass a style.
## Handling prompts and conflicts
- **Always pass `--yes`** so the CLI does not block on confirmation prompts.
- **Do NOT pass `--overwrite` by default.** If the CLI reports an existing file, read the output and resolve deliberately: install under a different name, adjust the path, or ask the user. Only use `--overwrite` when the user explicitly wants to replace a file.
- **Preview first when touching an existing project**: `npx shadcn@latest add @reui/<name> --dry-run` shows what would change; `--diff <file>` shows a specific file's diff. Use these before overwriting.
- Run from the **project root** so `components.json` and `.env.local` are found.
## Free vs premium boundary
- Public, no key: `c-*` examples and the 22 components (`@reui/data-grid`, `@reui/badge`, ...) that those examples depend on.
- Key required at install: blocks (`@reui/<category>-N`) need a Pro or Ultimate license; Motion Icons (`@reui/icons/...`) and templates need Ultimate.
If an install 401/403s, the license key is missing, invalid, or the plan does not cover that resource (blocks: Pro or higher; icons and templates: Ultimate). Point the user to https://reui.io/account?ref=skill (their key) or https://reui.io/pricing?ref=skill (upgrade).
-428
View File
@@ -1,428 +0,0 @@
# ReUI components
The 22 ReUI building blocks: `alert`, `autocomplete`, `badge`, `cascader`, `code-block`, `data-grid`, `date-selector`, `event-calendar`, `filters`, `frame`, `gantt`, `icon-stack`, `icon-tile`, `kanban`, `number-field`, `phone-input`, `rating`, `scrollspy`, `sortable`, `stepper`, `timeline`, `tree`. Examples and blocks are composed from these.
**Rule one: never guess a component's API. Read it first.** Call **`get_component(name)`** for its inline `api` (props + usage, no web fetch), and **share the result's `docsUrl`** (the component's API documentation page) with the user whenever you work with that component's API, so they have the full reference (the `/llms.txt` index is a further fallback). Then call **`get_examples(name)`** to install a worked example and copy real composition. The contracts below are first-try orientation (required props, composition shape, the one gotcha); the inline `api` is the full reference. No single block fits? Compose: search the components you need, read each `get_component`, install a `get_examples` example per component, and adapt.
## data-grid (the flagship - read its API every time)
`data-grid` wraps TanStack Table v9. It is NOT a styled `<table>` and does NOT take `data`/`columns` props directly. The contract:
- Build a TanStack table instance with `useTable({ features: dataGridFeatures, ... })` (columns, data). `dataGridFeatures` is exported by the primitive and already bundles sorting, filtering, pagination, row selection, expanding, pinning, resizing and faceting, so there are no per-table row models to wire.
- Pass that instance to `<DataGrid table={table} recordCount={total}>`.
- Compose the body with `DataGridTable` inside `DataGrid`, and enable features through `tableLayout` (e.g. `{ headerSticky: true, columnsResizable: true }`), not ad-hoc classes.
- Server-side data uses the documented fetch shape (`recordCount` is the total for pagination).
```tsx
const table = useTable({
features: dataGridFeatures,
data,
columns,
})
<DataGrid table={table} recordCount={data.length}>
<DataGridTable />
</DataGrid>
```
Common mistakes:
- **Incorrect:** `<DataGrid data={rows} columns={cols} />` - these props do not exist. **Correct:** build a `useTable({ features: dataGridFeatures, ... })` instance and pass `table={table}` + `recordCount`.
- **Incorrect:** a raw `<table>` / hand-rolled pagination. **Correct:** use `data-grid`; read its API for sticky header, pagination, virtualization, row selection.
- **Incorrect:** styling rows/cells with arbitrary classes. **Correct:** drive layout via `tableLayout` and the primitive's `DataGridColumnMeta` (e.g. `cellClassName`, `headerTitle`), set through the bundle's `columnMeta` slot.
## event-calendar
**Required:** events via `events`/`onEventsChange` (controlled) or `defaultEvents` (uncontrolled), plus a height on the root.
**Shape:**
```tsx
<EventCalendar defaultEvents={events} defaultView="month" className="h-[560px]">
<EventCalendarNav />
<EventCalendarContent />
</EventCalendar>
```
**Gotcha:** headless-first: `EventCalendarContent` renders the active view (month/week/day/days/agenda; a resource view activates when `resources` is passed) - there is no per-view JSX to compose. Events are `{ id, title, start, end (exclusive), allDay?, color?, recurrence?, resourceId? }`. Mutations flow through `onEventUpdate`/`canDropEvent` (return `false` to reject); the root needs an explicit height because it is a min-h-0 flex column.
## gantt
**Required:** `resources` (the left tree) plus bars via `events`/`defaultEvents` attached by `resourceId`.
**Shape:**
```tsx
<Gantt defaultEvents={bars} resources={tasks} defaultScale="month" className="h-[480px]">
<GanttNav />
<GanttView />
</Gantt>
```
**Gotcha:** bars move along the time axis only (never across rows) and are all-day spans with exclusive `end`; `progress` is 0-100. Scales are `day | week | month | quarter | year`. Zoom control, infinite scroll, summary rollups, and row checkboxes are ON by default - turn off what you do not need. Same `onEventUpdate`/`canDropEvent` commit pipeline as `event-calendar`; the root needs an explicit height.
## kanban
**Required:** `value` (`Record<string, T[]>`), `onValueChange`, `getItemValue`
**Shape:**
```tsx
<Kanban value={cols} onValueChange={setCols} getItemValue={(i) => i.id}>
<KanbanBoard>
{Object.entries(cols).map(([id, items]) => (
<KanbanColumn key={id} value={id}>
<KanbanColumnHandle><h3>{id}</h3></KanbanColumnHandle>
<KanbanColumnContent value={id}>
{items.map((i) => (
<KanbanItem key={i.id} value={i.id}>
<KanbanItemHandle>{i.title}</KanbanItemHandle>
</KanbanItem>
))}
</KanbanColumnContent>
</KanbanColumn>
))}
</KanbanBoard>
<KanbanOverlay><div className="bg-muted size-full rounded-md" /></KanbanOverlay>
</Kanban>
```
**Gotcha:** state is `Record<columnId, T[]>`. Each `KanbanColumnContent value` must match its parent `KanbanColumn value`. Omit `KanbanOverlay` and the drag preview silently breaks.
## sortable
**Required:** `value` (`T[]`), `onValueChange`, `getItemValue`
**Shape:**
```tsx
<Sortable value={items} onValueChange={setItems} getItemValue={(i) => i.id}>
{items.map((i) => (
<SortableItem key={i.id} value={i.id}>
<SortableItemHandle><GripVertical /></SortableItemHandle>
{i.label}
</SortableItem>
))}
</Sortable>
```
**Gotcha:** a flat 1D reorder list (not columns - that is `kanban`). `getItemValue` must return a stable, unique string. Pass `layout="grid"` or `layout="nested"` for non-list layouts.
## filters
**Required:** `fields` (`FilterField[]`). The value is ONE `FilterQuery` tree - `query` + `onQueryChange`, or uncontrolled `defaultQuery`.
**Shape:**
```tsx
const fields: FilterField[] = [
{ id: "title", label: "Title", type: "text" },
{
id: "status",
label: "Status",
type: "select",
options: [
{ value: "active", label: "Active" },
{ value: "archived", label: "Archived" },
],
},
]
const [query, setQuery] = useState<FilterQuery>(() => createFilterQuery())
<Filters fields={fields} query={query} onQueryChange={setQuery} />
```
**Gotcha:** the state is a TREE, not a list of chips. `FilterQuery` is a group of rules joined by `and`/`or` and a group may hold another group, so `(A and B) or C` is expressible; a rule is `{ id, type: "rule", path: ["status"], operator, value }` and `path` is the whole nested attribute path, root first. The pre-rewrite API is GONE: there is no `filters`/`onChange` prop, no `FilterFieldConfig` (fields are `FilterField`, nested through their own `fields`, keyed `id` not `key`), and no `createFilter()` - it minted ids inside a pure function and broke hydration, so ids now come from `createFilterIdFactory(seed)` seeded off `useId`, and `createFilterQuery()` / `createFilterRule()` take one. Read the query back with `flattenFilterConditions` (`{ path, field, operator, values, negated }` per rule, incomplete rules skipped) and walk the tree yourself when the parentheses carry meaning - the primitive compiles nothing, no SQL, no query string.
`variant` picks the chrome over that one query: `"basic"`, the default, is the flat chip row for a toolbar over a table; `"advanced"` is the condition builder, hung off a trigger or rendered in place with `advancedMode="inline"`. Both read and write the same tree, so a saved view built in one opens in the other. Other props worth knowing before you hand-roll them: `size` is two rungs, `"sm" | "default"`, resolved per style (there is no `lg`); `reorderable` turns on drag and Alt+Arrow row moves in the builder; `onBeforeQueryChange` is the ONE veto point for every write (return `false` to refuse, it cannot rewrite); `editors` registers custom value editors a field selects by `editor` name; `labels` / `operatorLabels` own every rendered string; `pathCollapse` + `maxPathSegments` shorten deep attribute paths; `renderChip` / `renderValue` / `renderEmpty` replace rendered parts. On a field, `loadOptions` supplies async options with paging and `resolveValues` renders a chip restored from a saved view whose option was never loaded. Pairs naturally with `data-grid`.
## cascader
**Required:** `items` (a tree of `{ value, label, children? }`), plus the panel parts inside `CascaderContent`.
**Shape:**
```tsx
<Cascader items={items} value={value} onValueChange={setValue}>
<CascaderTrigger render={<Button variant="outline" />}>
<CascaderValue placeholder="Select an attribute" />
</CascaderTrigger>
<CascaderContent className="w-80">
<CascaderPanel>
<CascaderNav>
<CascaderBreadcrumb />
<CascaderInput />
</CascaderNav>
<CascaderEmpty />
<CascaderList maxHeight={288}>
<CascaderItems />
</CascaderList>
<CascaderStatus />
</CascaderPanel>
</CascaderContent>
</Cascader>
```
**Gotcha:** pressing a branch NAVIGATES, it does not select - only leaves are selectable until you pass `selectable="any"` or a predicate, and once a branch is selectable its chevron becomes the only way to open it. `CascaderInput` must stay inside `CascaderContent` (Base UI refills the query from the selection when the input sits outside the popup). Always include `CascaderStatus`: it is the live region announcing level changes, which the visual breadcrumb does not provide to screen readers. Accepts a flat adjacency list via `getParent` as well as nested `children`. `searchScope="deep"` searches every level and annotates results with their path; `multiple` gives checkbox rows; `inline` + a bare `CascaderPanel` embeds it with no popover.
The shape above is `mode="drill"`, the default. `mode="tree"` keeps the same parts (drop `CascaderBreadcrumb`, pass `showBack={false}`, drive expansion with `expanded`/`onExpandedChange`); `mode="columns"` REPLACES `CascaderList` + `CascaderItems` with a single `CascaderColumns`, and has no breadcrumb. Other props worth knowing before you hand-roll them: `cascade` (multi-select only, parent/child selection with indeterminate branches - pair it with `selectable="any"`, since a leaf-only tree can never cascade), `indicator={false}` to drop the single-select check and its gutter (visual only, no-op with `multiple`), `virtualize`/`virtualizeThreshold` plus `CascaderVirtualItems` for long levels, and `getChildren` for async levels with cursor paging, retry on failure and optional `prefetch`. `CascaderFooter` pins commands below the list (`actions` is the quick path) and `CascaderSubmenu` opens one as a side-anchored flyout with the full menu keyboard model. To head a run of rows use `CascaderGroup` wrapping a `CascaderLabel` - a bare label inside a listbox names nothing and is dropped from the accessibility tree - and `CascaderSeparator` for the rule between runs. Every rendered string comes from `labels`, and the panel is RTL-correct under a `DirectionProvider` or `dir="rtl"`.
## date-selector
**Required:** none, but wire `onChange` to capture the value.
**Shape:**
```tsx
const [value, setValue] = useState<DateSelectorValue | undefined>()
<DateSelector value={value} onChange={setValue} label="Due date" />
```
**Gotcha:** the value is a structured `DateSelectorValue` (period / operator / start+end dates), NOT a `Date` - never pass a raw `Date`. Use `allowRange={false}` to lock single-date picking. Read `get_component("date-selector")` for the value shape.
## tree
**Required:** `tree` (a `@headless-tree/core` instance you construct)
**Shape:**
```tsx
<Tree tree={tree}>
{tree.getItems().map((item) => (
<TreeItem key={item.getId()} item={item}>
<TreeItemLabel />
</TreeItem>
))}
</Tree>
```
**Gotcha:** `Tree` is a styled shell - it takes a headless-tree instance via `tree`, NOT `data`/`items` props. Build the instance with `@headless-tree/react`. External API: https://headless-tree.lukasbach.com/
## stepper
**Required:** `StepperItem step` (number), `StepperContent value` (number)
**Shape:**
```tsx
<Stepper defaultValue={1}>
<StepperNav>
<StepperItem step={1}>
<StepperTrigger><StepperIndicator>1</StepperIndicator></StepperTrigger>
<StepperSeparator />
</StepperItem>
<StepperItem step={2}>
<StepperTrigger><StepperIndicator>2</StepperIndicator></StepperTrigger>
</StepperItem>
</StepperNav>
<StepperPanel>
<StepperContent value={1}>Step 1 content</StepperContent>
<StepperContent value={2}>Step 2 content</StepperContent>
</StepperPanel>
</Stepper>
```
**Gotcha:** steps are 1-indexed. Without `StepperPanel` + `StepperContent` you render the nav trail but no body. Put `StepperSeparator` in every `StepperItem` except the last.
## timeline
**Required:** `TimelineItem step` (number)
**Shape:**
```tsx
<Timeline>
<TimelineItem step={1}>
<TimelineHeader>
<TimelineDate>March 2024</TimelineDate>
<TimelineTitle>Project initialized</TimelineTitle>
</TimelineHeader>
<TimelineIndicator />
<TimelineSeparator />
<TimelineContent>Repo and architecture set up.</TimelineContent>
</TimelineItem>
</Timeline>
```
**Gotcha:** each item needs a unique `step`. `orientation` is `"vertical"` (default) or `"horizontal"`. This is a static event display, not interactive like `stepper`.
## autocomplete
**Required:** `items` (array; each item has at least `value`)
**Shape:**
```tsx
<Autocomplete items={items}>
<AutocompleteInput placeholder="Search..." />
<AutocompleteContent>
<AutocompleteEmpty>No results found.</AutocompleteEmpty>
<AutocompleteList>
{(item) => (
<AutocompleteItem key={item.value} value={item}>{item.label}</AutocompleteItem>
)}
</AutocompleteList>
</AutocompleteContent>
</Autocomplete>
```
**Gotcha:** `AutocompleteList` takes a render-prop `(item) => ReactNode`, NOT a mapped array of children. External API: https://base-ui.com/react/components/autocomplete
## phone-input
**Required:** none, but wire `onChange`.
**Shape:**
```tsx
<PhoneInput placeholder="Enter phone number" defaultCountry="US" value={value} onChange={setValue} />
```
**Gotcha:** `value`/`onChange` use an E.164 string (e.g. `"+14155551234"`), not a display-formatted string; `onChange` can fire `undefined`. `defaultCountry` is a 2-letter ISO code. Wraps `react-phone-number-input`.
## number-field
**Required:** wrap the controls in `NumberFieldGroup`.
**Shape:**
```tsx
<NumberField defaultValue={0}>
<NumberFieldScrubArea label="Quantity" />
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>
```
**Gotcha:** import from `@/components/ui/number-field`. The accessible label goes on `NumberFieldScrubArea`, not `NumberField`. External API: https://base-ui.com/react/components/number-field
## rating
**Required:** `rating` (number)
**Shape:**
```tsx
<Rating rating={4.5} showValue editable onRatingChange={setRating} />
```
**Gotcha:** supports decimals (partial stars). Pass `editable` + `onRatingChange` for interactive input; omit both for a read-only display.
## scrollspy
**Required:** `targetRef` (the scroll container ref)
**Shape:**
```tsx
<Scrollspy targetRef={containerRef}>
<a href="#s1" data-scrollspy-anchor="s1">Section 1</a>
<a href="#s2" data-scrollspy-anchor="s2">Section 2</a>
</Scrollspy>
<div ref={containerRef}>
<div id="s1">...</div>
<div id="s2">...</div>
</div>
```
**Gotcha:** each link's `data-scrollspy-anchor` must match a section `id`. `targetRef` is the scrollable container (defaults to the window).
## frame
**Required:** `Frame` > `FramePanel`
**Shape:**
```tsx
<Frame>
<FramePanel>
<FrameHeader>
<FrameTitle>Title</FrameTitle>
<FrameDescription>Description</FrameDescription>
</FrameHeader>
<div className="p-5">Content</div>
<FrameFooter>Footer</FrameFooter>
</FramePanel>
</Frame>
```
**Gotcha:** a structured card shell for tool-like surfaces. `stacked` connects multiple panels with shared borders; `dense` removes panel padding; radius via the `--frame-radius` CSS variable.
## icon-stack
**Required:** one child icon
**Shape:**
```tsx
<IconStack aria-hidden="true">
<InboxIcon className="size-4" />
</IconStack>
```
**Gotcha:** isometric layered artwork for empty states and illustrations; style the inner icon via its own `className`. Mark purely decorative stacks `aria-hidden="true"` and keep the real label in surrounding copy.
## icon-tile
**Required:** one child icon
**Shape:**
```tsx
<IconTile variant="elevated" size="lg">
<PackageIcon />
</IconTile>
```
**Gotcha:** the square container an icon sits in, so every list row, feature card and empty state shares one affordance. `variant`: `outline` (default) | `elevated` (muted fill, raised ring) | `soft` (tinted nested, tone from currentColor) | `solid` (filled tone, contrasting glyph) | `frame` (double container). `soft` and `solid` retint from one text color class (they default to `text-primary`). `size`: `xs | sm | default | lg | xl` (24/32/40/48/64px tile, glyph scales 12/14/16/20/24px). `radius`: `default | full`. Do not set a `size-*` class on the child icon unless you mean to override the tile's glyph size; recolor with `className` on the tile, not the icon.
## code-block
**Required:** `code` + `language`, or pre-highlighted `lines`.
**Shape:**
```tsx
<CodeBlock code={code} language="tsx" />
<CodeBlock code={code} language="tsx" showLineNumbers maxLines={20}>
<CodeBlockHeader>
<CodeBlockTitle>use-totals.ts</CodeBlockTitle>
<CodeBlockLanguage />
<CodeBlockCopyButton className="ml-auto" />
</CodeBlockHeader>
<CodeBlockExpandButton />
</CodeBlock>
```
**Gotcha:** the one-liner is already a complete block; every child (header, title, language label, copy button, wrap toggle, expand button, line actions) is optional chrome, at any depth. Shiki is the only npm dependency and loads lazily, one chunk per language, on first highlight; `lines` (from `highlightCode` in `code-block-highlight`, which is server-safe and has no `"use client"`) or `highlight={false}` loads nothing at all. `maxLines` caps the height AND marks the block collapsible, which is what makes `CodeBlockExpandButton` appear. Use `variant="ghost"` when the block sits inside a surface that already has a border.
## alert
**Required:** `Alert` > `AlertTitle`
**Shape:**
```tsx
<Alert variant="success">
<ShieldCheckIcon />
<AlertTitle>Security update</AlertTitle>
<AlertDescription>Enable two-factor authentication.</AlertDescription>
<AlertAction><Button size="xs">Update</Button></AlertAction>
</Alert>
```
**Gotcha:** shadcn-compatible API. `variant`: `default | destructive | info | success | warning | invert`. The non-default variants use ReUI extended color tokens (`--success`/`--info`/`--warning`/`--invert`), which the install adds. Defer generic alert rules to the shadcn skill.
## badge
**Required:** none (text child).
**Shape:**
```tsx
<Badge variant="success-light" size="sm">Success</Badge>
<Badge variant="outline" radius="full">Pill</Badge>
```
**Gotcha:** shadcn-compatible. Rich `variant` set (solid, `-outline`, `-light` per color), `size` `xs..xl`, `radius` `default | full`. Like `alert`, the color variants rely on ReUI extended tokens. Prefer `Badge` variants over raw color classes for statuses.
## base vs radix - write for the project's base
ReUI ships every component in two builds: `base` (Base UI) and `radix` (Radix UI). The install command and name are identical, and the CLI installs the build matching the project. But you must write/adapt code against the **right base**, because their APIs differ.
**Detect the base first.** Read `components.json` -> `style` and take the segment before the first `-`:
- `"style": "base-nova"` -> **Base UI**
- `"style": "radix-nova"` -> **Radix UI**
**Then use that base's API.** The deltas mirror shadcn's base-vs-radix split:
- Slot/composition: Base UI `render={<… />}` vs Radix `asChild`.
- `Select`: Base UI takes `items`; Radix uses `<SelectItem>` children.
- `ToggleGroup`: Base UI `multiple` boolean vs Radix `type="single" | "multiple"`.
The safest path is to **read the installed files and `c-*` examples** - they're already in your base, so reuse their wiring instead of guessing. When `get_component`'s inline `api` or an example shows the other base's shape, translate it to your base (or `validate_usage` to confirm). Defer the generic base/radix mechanics to the shadcn skill.
-45
View File
@@ -1,45 +0,0 @@
# Craft: make ReUI UI exceptional, not generic
ReUI items ship senior-designer quality. Your adaptation has to hold that bar, so the result reads like a real product surface a team would keep - not a wireframe an AI generated. Use these alongside the reuse rules in [adapting.md](./adapting.md).
## Have a point of view
Pick an emotional register before you compose - calm, operational, premium, editorial, dense, energetic - and let layout, spacing, surface treatment, and icon behavior all reinforce it. One or two memorable decisions and restraint everywhere else beats ten generic ones. UI with no point of view reads as generated.
## Brutally clear hierarchy
One focal point per card or panel: the dominant metric or task first, its label second, supporting detail third. The first thing the eye lands on should be the right thing; secondary text must read as secondary. Borders, separators, and surfaces do real work to create 2-3 information bands - don't flatten everything to equal weight.
## Spacing rhythm and deliberate density
Gaps are a signal, not a default. Keep them intentional and consistent within a family (`gap-1`/`gap-2` for tight operational rows, larger gaps for section breaks), and smaller within a group than between groups. Match the surrounding ReUI density; don't pad an operational surface like a marketing page, and don't drift density mid-section. The composition should still feel authored in grayscale.
## Cover the real states (the usual miss)
A surface isn't done at the happy path. Compose, and wire:
- **Empty** - a purposeful empty state (short message + the primary action), never a blank panel.
- **Loading** - a **skeleton** that matches the real layout, not a centered spinner.
- **Error** - an inline, recoverable error with a retry, announced via `role="status"`/`aria-live`.
Derive these from an element the block already has (don't invent parallel markup), or `get_examples` for a state-specific example.
## Responsive by default
Mobile-first, not mobile-afterthought. In constrained rows/cards/sidebars, put `min-w-0` on the shrinking container and `truncate` long single-line labels; protect the primary label's width and let secondary content compress. Reflow layouts (multi-column -> single column) rather than just shrinking them. Desktop and mobile should both look designed.
## Motion, subtly
Motion should clarify, not decorate. Use ReUI Motion Icons on primary actions for a subtle hover cue; keep transitions short (~200-300ms) with calm easing; prefer a skeleton pulse over a spinner. No bouncing, no gratuitous entrance animations on every element.
## Real, activated content
Use believable, typed data (realistic labels, counts, timestamps, statuses that map to a real workflow) - never lorem or abstract filler. Every visible control does something: no decorative buttons, fake tabs, meaningless toggles, or stats with no job. It must still hold with long names, empty values, and crowded data.
## Avoid the AI tells
These instantly read as generated - don't ship them: equal-weight card walls, empty gradients, repetitive padding everywhere, generic enterprise copy, ornamental icons, and number tiles that don't earn their place.
## The bar
Before you finish, ask: **would a product team keep this instead of replacing it? Does it still feel strong after swapping in real content?** If not, reuse the shipped ReUI design harder - don't restyle it into something new - then run the [quality.md](./quality.md) gates.
-39
View File
@@ -1,39 +0,0 @@
# Icons (ReUI delta over shadcn)
Follow the shadcn icon rules (use the project's configured `iconLibrary`, `data-icon` on icons inside `Button`, no sizing classes on icons inside components, pass icons as component objects not string keys). ReUI adds the following.
## Portable icons (library-agnostic)
ReUI components, examples, and blocks are authored to be icon-library-agnostic. When `iconLibrary` is set in `components.json`, the shadcn CLI installs each item's icons in **your** library automatically - you swap nothing. If an installed item's icons don't match your project (for example `iconLibrary` isn't set, so they came in from the item's demo library), change the **import source and component name** to your library, keeping the same icon-name semantics:
- `lucide` -> `lucide-react`
- `tabler` -> `@tabler/icons-react`
- `phosphor` -> `@phosphor-icons/react`
- `remix` -> `@remixicon/react`
- `hugeicons` -> `@hugeicons/react`
Don't assume `lucide-react`; read `iconLibrary` from `components.json`.
## Keep icons purposeful
Icons support the hierarchy, they don't replace it: keep them small, matched to the surrounding density, and decorative ones `aria-hidden="true"` (an icon-only control still needs an accessible label on the control). Don't add ornamental icons that do no job.
## Motion Icons (the `@reui/icons/...` set)
ReUI ships its own icon set in 4 styles (outline, solid, duotone, filled), each icon in two variants:
```bash
npx shadcn@latest add @reui/icons/default/<style>/<name> --yes # static
npx shadcn@latest add @reui/icons/animated/<style>/<name> --yes # hover-animated (motion/react)
```
Finding them via the MCP is free; installing requires an Ultimate license (`REUI_LICENSE_KEY`, see [cli.md](./cli.md)). Reach for a Motion Icon on a primary action when a subtle hover cue helps; keep motion restrained.
Finding icons:
- Several icons (the common case): **`search_icons(concepts[])`** - up to 24 concepts in one call, the best icons per concept with install commands. Pass `animated: true` to get only icons with a hover-animated Motion variant.
- One icon: `search` with `type: "icon"`.
- Icon results and `get_icon` carry `animated: true` and `installAnimated` when an animated variant exists - use those install strings, do not construct paths by hand.
- Every icon result carries a `previewUrl` (its live icon-category page) - **share it with the user** so they can SEE the icon before installing.
The `icon-stack` component composes multiple icons into a stacked display.
-22
View File
@@ -1,22 +0,0 @@
# Quality gates (security, accessibility, scroll)
These are the **done gate**, not a nice-to-have: before you call any ReUI work finished, call the MCP `get_audit_checklist` tool and pass every item below (plus the craft bar in [craft.md](./craft.md)). Then typecheck and lint.
## Security
- Never `dangerouslySetInnerHTML`. Render data as text/components.
- External links (`target="_blank"`) must always pair `rel="noopener noreferrer"`.
- No real PII, secrets, or tokens in demo or committed code. Remote media only from sources the project already allows.
## Accessibility
- Implicit list/card items that navigate get real anchors with a standard hover affordance.
- Icon-only or numeric buttons need an `aria-label`; decorative icons get `aria-hidden`.
- Every non-submit button is `type="button"`.
- Keyboard + focus: everything interactive is reachable in a sensible Tab order with a visible focus ring; layers (dialogs/sheets/menus) trap focus and close on `Escape`. ReUI components ship standard keyboard behavior - read each component's inline `api` rather than re-implementing it.
- Announce async UI: loading and error messages use `role="status"` / `aria-live` so they're not silent to screen readers.
## Scroll mechanics
- Make scroll regions with a parent-owned height: a `min-h-0` + flex chain down to the scroll container. Never guess a `max-h`.
- The scroll container owns `overflow-auto`; ancestors stay `min-h-0` so the height resolves.
-35
View File
@@ -1,35 +0,0 @@
# ReUI registry structure
ReUI is a shadcn-compatible registry with four entity types. **Examples and blocks are built FROM components** - reuse them, don't rebuild.
- **component** - one of the 22 ReUI building blocks with a real API (`data-grid`, `kanban`, `filters`, `date-selector`, `tree`, ...). Install directly (`@reui/data-grid`) or let it come in as a dependency of an example/block. Free. Read its API with `get_component(name)`.
- **example** - a free `c-*` single-pattern use-case of a component (`c-kanban-1`, `c-data-grid-3`). Install one and read it to copy real composition. Find a component's examples with `get_examples(name)`.
- **block** - a premium, full-page section that composes several components (`data-grid-2`, `pricing-page-1`). Pro or Ultimate license at install. Adapts to your active theme via semantic tokens.
- **icon** - Motion Icons in 4 styles (outline, solid, duotone, filled), static (`@reui/icons/default/<style>/<name>`) and hover-animated (`@reui/icons/animated/<style>/<name>`). Ultimate license at install. See [icons.md](./icons.md).
## The @reui registry
Install everything through the shadcn CLI: `npx shadcn@latest add @reui/<name> --yes`. The CLI reads the `@reui` registry from the project's `components.json`. Free items need only the plain string form:
```json
{ "registries": { "@reui": "https://reui.io/r/{style}/{name}.json" } }
```
Premium installs need the authenticated form + `REUI_LICENSE_KEY` in `.env.local` - see [cli.md](./cli.md). The MCP `get_project_context` tool returns the right config.
## Know your base: base or radix
ReUI ships every item in two builds - `base` (Base UI) and `radix` (Radix UI) - with mirrored names. The CLI installs the build matching your project automatically, but **you must write code against the right base's API**. Detect it from `components.json` -> `style`: the segment before the first `-` is the base (`base-nova` -> Base UI, `radix-nova` -> Radix UI). The installed files and `c-*` examples are already in your base - read them and adapt on that base. See [components.md](./components.md) for the API deltas.
**So the MCP's own `docsUrl` and `previewUrl` match your base**, send your `style` to the MCP: add `?style=<your components.json style>` to the ReUI MCP server URL (or set an `X-Reui-Style` header) in your MCP client config - set once, applies to every call. The MCP then resolves docs/preview links to YOUR library (`/docs/components/radix/...`, `/preview/radix/...` for a radix project) instead of the default base; `get_project_context` echoes back the style it currently sees so you can confirm it. Install commands are unaffected (the CLI already installs the right variant). If you notice the MCP returning `base` links for a `radix` project, tell the user to add `?style=` to the server URL.
Blocks adapt to your active theme through semantic tokens and CSS variables - change the theme and every block follows.
## Free vs premium
- **Free, no key:** the 22 components, all `c-*` examples, the ReUI MCP, and this skill.
- **Premium, license required at install:** blocks (Pro or Ultimate), Motion Icons and templates (Ultimate). Set `REUI_LICENSE_KEY` (see [cli.md](./cli.md)).
## Component API index
The canonical index of every component's API docs is **https://reui.io/llms.txt** (returned as `componentsApiUrl` in MCP results). Prefer the inline `api` from `get_component`; use the index/docs as the fallback.
-26
View File
@@ -1,26 +0,0 @@
# Styling (ReUI delta over shadcn)
Follow the shadcn skill for the generic rules (semantic colors not raw values, `gap-*` not `space-y-*`, `size-*`, `cn()`, no manual `dark:` overrides, no overlay `z-index`). This file is only the ReUI-specific additions.
## ReUI extended semantic tokens
ReUI adds semantic tokens beyond shadcn's base set. Use these instead of raw colors for status and emphasis:
- `--success` / `--success-foreground`
- `--info` / `--info-foreground`
- `--warning` / `--warning-foreground`
- `--destructive-foreground` (paired with shadcn's `--destructive`)
- `--invert` / `--invert-foreground` (inverted surfaces)
Use them as Tailwind utilities (`bg-success text-success-foreground`, `text-warning`, ...). They are defined in the project's global CSS and registered with Tailwind (`@theme inline` on v4). If a token is missing in the project, add it to the global CSS file (never a new file) following the same `name` / `name-foreground` convention, exactly as the shadcn customization rules describe.
**Incorrect:** `<span className="text-green-600">Active</span>`
**Correct:** `<Badge variant="success">Active</Badge>` or `<span className="text-success">Active</span>`
## Blocks follow your theme
When you install a block it adapts to your active theme through the semantic tokens above and the project's CSS variables. Don't hardcode style-specific values into installed block code and don't fork it to "restyle" - change the theme via the CSS variables / a preset and every block follows. Want a different look? `search` for a block whose design already fits instead of re-skinning one.
## Density and typography rhythm
ReUI operational UI usually feels dense, not airy. Keep the gap between a title and its supporting description tight by default (`gap-0.5`, `space-y-1`, or `space-y-px`), and smaller than the gap between sections. Match the surrounding ReUI density when you add rows or fields; do not pad operational surfaces like a marketing page.
-54
View File
@@ -1,54 +0,0 @@
# Workflow: find -> install -> read API -> adapt
The core ReUI loop. The MCP tells you what to install and gives you the API; the shadcn CLI installs it; you turn the installed files into correct, themed, data-wired code by **reuse**, not redesign.
## 1. Find (ReUI MCP `search` / `compose_page`)
**Full multi-section page ask?** Call `compose_page(intent, sections?)` FIRST, before searching block-by-block. It returns ordered sections, each with the best block for the intent (top pick + alternates); sections listed in `unavailableSections` have no real inventory - compose those from components, do not force a bad block.
For everything else, call `search` with the user's intent. Pass structured hints whenever you can infer them - you are an LLM, so do the parsing the server cannot:
- `type`: `"component"` (one of the 22 building blocks), `"example"` (a c-\* use-case), `"block"` (a full page/section), `"icon"`.
- `component`: the ReUI component the request implies (`"data-grid"`, `"kanban"`, ...).
- `category`, `features` (e.g. `["sortable","pagination"]`), `free`.
Example: "build a users management page with filters" -> `search({ query: "users management page with filters", type: "block", component: "data-grid", features: ["filters"] })`.
Each result has `install`, `previewUrl`, `docsUrl`, `componentsUsed`, `score`, `termCoverage`, and `whyMatch`. `score` is relative to the top hit (the top is ~100 by construction), not an absolute quality - compare results to each other, and show the user the top options if several score closely; do not silently guess. A low `termCoverage` means a weak match even with a high score - rephrase or widen.
**Always show the preview link.** Whenever you list or recommend items - from `search`, `search_icons`, `list_components`, `compose_page`, or a getter - include each item's `previewUrl` (a live preview page) so the user can SEE it before you install. Blocks and examples link to an individual live preview; icons and components to their live category/component page. This applies to every listing, not only a single pick.
## 2. Install (shadcn CLI)
Run the result's `install` command from the project root, non-interactively:
```bash
npx shadcn@latest add @reui/<name> --yes
```
The CLI reads `components.json`, installs the correct base+style variant, resolves `registryDependencies` (a block pulls in its components), installs npm deps, and rewrites aliases. Do not pass the base/style. See [cli.md](./cli.md).
## 3. Read the API (do not guess props)
Before writing code against any component an item uses:
1. The item's `componentDigests` already give a 1-line contract per component - often enough to wire it. For the full API, call **`get_component(names)`** with ALL of `componentsUsed` in ONE call (it accepts an array) and read each inline `api` - no web fetch. **Share the component's `docsUrl`** (its API documentation page) with the user whenever you work with that component's API, so they have the full reference; the `/llms.txt` index is a further fallback.
2. Call **`get_examples(name)`** for the free `c-*` examples of that component; install one and **read the added files** to copy the exact composition. This is the fastest correct path - the example shows real wiring you adapt, not invent.
3. About to write a prop you did not see in an `api` or installed file? Run **`validate_usage`** BEFORE writing the code - per-prop documented / notDocumented verdicts plus did-you-mean suggestions. notDocumented means read the API, not push on.
## 4. Adapt (reuse-first) - do not skip
Installing files is not the end, and redesigning them defeats the point. First note the project's **base** so you write the right API - read `components.json` -> `style` and take the segment before the first `-` (`base-nova` -> Base UI, `radix-nova` -> Radix UI), see [components.md](./components.md). After `add`:
1. **Read the added files**; keep the composition intact. For a block, verify the components are wired correctly (for `data-grid`: a `useTable({ features: dataGridFeatures, ... })` instance passed as `table`, `recordCount` set - see [components.md](./components.md)).
2. **Replace demo data with the user's real data** via typed structures (see [adapting.md](./adapting.md)).
3. **Fix icon imports** to the project's icon library (see [icons.md](./icons.md)).
4. **Align styling** to semantic tokens and the active theme - no raw colors (see [styling.md](./styling.md)).
5. **Validate before finalizing**: if your adaptation introduced components or props you did not read in an `api` or example, run `validate_usage` on them.
6. **Hit the craft bar** - clear hierarchy, deliberate density, the empty / loading / error states, subtle motion, and mobile-first responsiveness (see [craft.md](./craft.md)). Generic-looking output means you under-reused the design, not that it needs restyling.
7. **Pass the quality gates** (security, a11y, scroll) - call the MCP `get_audit_checklist` tool and clear every item (see [quality.md](./quality.md)).
8. **Typecheck / lint**.
## If no single block fits
Compose from components (`compose_page` tells you which sections have no block inventory via `unavailableSections`). `search` the components you need, read each `get_component` API, install a worked `get_examples` example for each, and assemble by adapting those examples. A block in the same category is a useful reference - install it and read its files to see how ReUI composes those components, then adapt.
-58
View File
@@ -1,58 +0,0 @@
# ReUI MCP: full reference
The ReUI MCP (`https://mcp.reui.io`, Streamable HTTP) is free to use but needs a ReUI account: on first use the agent signs in with ReUI (a free account is created if the user has none), so every request is tied to an account. Free covers components and examples; a Pro or Ultimate license unlocks premium blocks and Motion Icons and removes the daily request limit. It does **discovery + guidance** (search, inline APIs, page planning, validation) and never serves source; the shadcn CLI does **installation**, and the license key lives there (the `@reui` entry in `components.json`, backed by `.env.local`). Goal: from the user's intent to correct, themed, data-wired ReUI code in the **fewest tokens and calls**, with **no guessing**.
## Golden path (token-optimal - follow this order)
Most tasks need 2-4 MCP calls and ZERO web fetches:
1. **`search(query, ...hints)`** -> pick the top 1-3 results. Each result already carries `install`, `previewUrl`, `docsUrl`, `componentsUsed`, `score`, `whyMatch`. The payload is complete - do not call another tool just to "confirm" a result.
2. **`get_component([...componentsUsed])`** in ONE batched call (one name or an array of up to 20) -> read each inline `api`. This **replaces** fetching docs pages. Often skippable: search responses carry `componentDigests`, a compact API contract per referenced component.
3. **`get_examples(component)`** -> install ONE returned `c-*` example, read the added files, copy the composition.
4. **`get_install_command(item)`** only to validate a name you are unsure of (results already include `install`). Run the install with the shadcn CLI (`--yes`).
5. **`get_audit_checklist()`** before declaring done.
If you already know the exact item name, skip `search`. Everything else is situational.
## The 5 task-specific tools (when to reach for each)
- **`compose_page`** - BEFORE building any full page (dashboard, settings, billing, landing). Pass the intent (and optionally the sections you want); it returns ordered sections, each with the best premium block for the intent (top pick + alternates). Sections with no real inventory are listed honestly in `unavailableSections` - compose those from components instead of forcing a bad block.
- **`search_icons`** - whenever you need icons, especially several. Batch up to 24 concepts in one call; each concept returns its best icons with install commands. Pass `animated: true` to get only icons that have a hover-animated Motion variant.
- **`validate_usage`** - BEFORE writing code with component names or props you have not read in an inline `api` or an installed example. It checks planned names + props against the indexed API docs and registry item names; returns did-you-mean suggestions and per-prop documented / notDocumented verdicts. Deterministic, no inference - a notDocumented prop means stop and read the API, not push on.
- **`whats_new`** - when your registry knowledge might be stale (a name 404s, the user mentions an item you don't know). Returns items added/removed per build, newest first.
- **`report_issue`** - when an installed item is actually broken (bad source, wrong dependency, broken preview). Goes straight to the ReUI team; rate-limited 5/hour. Not for usage questions.
## All 19 tools
`search`, `get_block`, `get_example`, `get_icon`, `list_block_groups`, `list_block_categories`, `list_example_categories`, `list_icon_categories`, `list_components`, `get_component`, `get_examples`, `search_icons`, `compose_page`, `validate_usage`, `whats_new`, `report_issue`, `get_install_command`, `get_project_context`, `get_audit_checklist`. The MCP serves the full parameter schemas; do not guess parameters beyond them.
## Token + speed rules
- **Batch `get_component`** - ONE call with the whole `componentsUsed` array, never N calls. Skip it entirely when `componentDigests` already answers the question.
- **Read source by installing** - the MCP serves no source. To read or analyze an item's real code, install it with the shadcn CLI and open the local files. Learn an API from the inline `api` / `componentDigests`, never by reading raw source.
- **Infer `search` hints yourself** (`type`, `component`, `category`, `features`, `free`) - hints shrink the result set and the tokens. Keep `limit` low; one right result beats ten.
- Run independent calls (and the shadcn install) concurrently in one turn - serial tool calls are the main source of slowness.
- Don't repeat a search for the same intent; don't call `list_*` to "see everything" - `search` is the entry point, `list_*` is only for browsing a taxonomy the user explicitly wants to explore.
- Prefer `get_component`'s inline `api` over `docsUrl` / `/llms.txt`. Fetch a web page only as a last resort.
## Result shapes (so you don't re-fetch)
- `score` is 0-100 RELATIVE to the top hit (the top is ~100 by construction), not absolute - compare results to each other.
- `termCoverage` (0-1) is the share of the query the item matched - low means a weak match even if the score looks high; rephrase or widen the search.
- Each result carries `whyMatch`, `install`, docs/preview URLs, and a `free` flag; premium items carry `requiredPlan` (`"pro"` for blocks, `"ultimate"` for icons).
- `componentDigests` is a top-level map: a compact API contract per referenced component - often enough to wire an item without a `get_component` call.
- Icon results and `get_icon` include `animated: true` and `installAnimated` when a hover-animated Motion variant exists (animated: `@reui/icons/animated/<style>/<name>`; static: `@reui/icons/default/<style>/<name>`).
## Error playbook
- **401** - the MCP requires a signed-in ReUI account. The client prompts "Sign in with ReUI" (OAuth) on first use; a free account is created if needed. For headless/CI, pass a personal token (`reui_pat_...`, created at https://reui.io/account/mcp?ref=skill) as `Authorization: Bearer`.
- **403 / locked result** - a valid account but the plan does not cover the item: premium blocks need Pro, Motion Icons need Ultimate. Point to https://reui.io/pricing?ref=skill (upgrade). Free accounts still get all components + examples.
- **429** - rate limited (120 requests/min per IP); back off, honor `Retry-After`.
- **not found** (`found: false`) - use the returned `suggestions`, or `search`; check `whats_new` if you suspect a stale name. Never run a fabricated install command.
## Fallbacks
- No ReUI MCP: `npx shadcn@latest search @reui -q "..."` then `add` (generic, no scoring / inline API).
- The shadcn project's own MCP also works over the `@reui` registry: https://ui.shadcn.com/docs/mcp.
Per-agent MCP setup: https://reui.io/docs/mcp?ref=skill
-1
View File
@@ -1 +0,0 @@
{"version":"0.0.1","configurations":[{"name":"MikrotikManager","runtimeExecutable":"npm","runtimeArgs":["run","dev"],"port":3000,"autoPort":true}]}
-70
View File
@@ -1,70 +0,0 @@
{
"permissions": {
"allow": [
"Bash(npm install *)",
"mcp__Claude_Preview__preview_start",
"Bash(npx tsc *)",
"Bash(npm run *)",
"Bash(curl -s -o /dev/null -w \"%{http_code}\" http://localhost:3000/dashboard)",
"Bash(curl -s -o /dev/null -w '%{http_code}' http://localhost:3000__TRACKED_VAR__)",
"Bash(curl -s -o /dev/null -w '%{http_code}' http://localhost:3001__TRACKED_VAR__)",
"PowerShell(netstat -ano | Select-String \":300\" | Where-Object { $_ -match \"LISTENING\" })",
"Bash(curl -s -o /dev/null -w '%{http_code}' http://localhost:__TRACKED_VAR__/dashboard)",
"Bash(curl -s -o /dev/null -w '%{http_code}' http://localhost:3333__TRACKED_VAR__)",
"Bash(curl -s -o /dev/null -w '%{http_code}' http://localhost:59959__TRACKED_VAR__)",
"WebFetch(domain:git.shx.one)",
"Bash(curl -s \"https://git.shx.one/denozord/router-lists-ui/raw/branch/v5/frontend/src/RouteOptimizerPage.jsx\")",
"Bash(curl -s \"https://git.shx.one/denozord/router-lists-ui/raw/branch/v5/frontend/src/OspfToolsPage.jsx\")",
"Bash(node -e ' *)",
"Bash(powershell -Command \"Get-Item 'C:\\\\Users\\\\shats\\\\.claude\\\\projects\\\\C--Users-shats-Dev-MikrotikManager-3\\\\b1dbd554-4665-40c0-bb5b-19d63bb494a0.jsonl'\")",
"Bash(node -e \"const { createRequire } = require\\('module'\\); const r = createRequire\\(__filename\\); const lucide = r\\('lucide-react'\\); ['SlackIcon','WebhookIcon'].forEach\\(n => console.log\\(n, !!lucide[n]\\)\\)\")",
"mcp__Claude_in_Chrome__tabs_context_mcp",
"Bash(npx eslint *)",
"Bash(grep -E \"^C:\\\\\\\\\\\\\\\\| [0-9]+:[0-9]+ error\")",
"Bash(grep -E \"\\(\\\\\\\\\\\\\\\\.*\\\\.tsx$\\)|\\(error\\)\")",
"Bash(python -c \"import sys,json; data=json.load\\(sys.stdin\\); [print\\(m['ruleId'], m['line'], m['message'][:60]\\) for f in data for m in f['messages'] if m['severity']==2]\")",
"Bash(python -c \"import sys,json; data=json.load\\(sys.stdin\\); [print\\(m['ruleId'], m['line'], m['message'][:80]\\) for f in data for m in f['messages'] if m['severity']==2]\")",
"Bash(npx next *)",
"Bash(dir \"C:\\\\Users\\\\shats\\\\Dev\\\\MikrotikManager-3\\\\app\\\\\\(main\\)\")",
"Bash(Get-ChildItem \"C:\\\\Users\\\\shats\\\\Dev\\\\MikrotikManager-3\\\\app\\\\\\(main\\)\" -Directory)",
"Bash(Select-Object Name)",
"Bash(Sort-Object Name)",
"Bash(awk 'NR>=508 && NR<=730' \"app/\\(main\\)/servers/page.tsx\")",
"Bash(grep -n \"Fragment\\\\|<>\\\\s*$\" \"app/\\(main\\)/filters/page.tsx\")",
"Bash(grep -n \"\\\\.map\\\\|Fragment\\\\|<>\\\\s*$\" \"app/\\(main\\)/ospf/page.tsx\")",
"Bash(grep -v \"//.*console\\\\|ErrorRecord\\\\|console\\\\.error.*err\\\\|any>\\\\|: any\\\\|as any\\\\|any\\\\[\\\\]\\\\|Record<.*any\\\\|Promise<any\")",
"Bash(curl -s http://localhost:8000/api/servers)",
"Bash(curl -s -X POST http://localhost:8000/api/servers/1/poll)",
"Bash(curl -s http://localhost:8000/api/servers/1)",
"Bash(curl -s http://localhost:8000/api/servers/1/snapshots)",
"Bash(curl -s -X DELETE http://localhost:8000/api/servers/1 -w \"\\\\nHTTP %{http_code}\")",
"Bash(curl -s http://localhost:8000/api/servers/999)",
"Bash(curl -s -X POST http://localhost:8000/api/servers -H \"Content-Type: application/json\" -d \"{}\")",
"Bash(curl -s -X POST http://localhost:8000/api/servers/test-connection -H 'Content-Type: application/json' -d '{\"host\":\"192.168.0.254\",\"port\":80,\"useSsl\":false,\"verifySsl\":false,\"apiPath\":\"/rest\",\"username\":\"admin\",\"password\":\"\"}')",
"Bash(curl -s -X POST http://localhost:8000/api/servers -H 'Content-Type: application/json' -d '{\"host\":\"192.168.0.254\",\"port\":80,\"useSsl\":false,\"verifySsl\":false,\"username\":\"admin\",\"password\":\"test123\",\"name\":\"RB5009 Home\",\"type\":\"home-router\",\"site\":\"MSK\",\"country\":\"RU\",\"asn\":\"\",\"comment\":\"Тестовый роутер\",\"enabled\":true}')",
"Bash(python3 -m json.tool)",
"Bash(curl -s -X POST http://localhost:8000/api/servers -H 'Content-Type: application/json' -d '{\"host\":\"192.168.0.254\",\"port\":80,\"useSsl\":false,\"verifySsl\":false,\"username\":\"admin\",\"password\":\"test123\",\"name\":\"RB5009 Home\",\"type\":\"home-router\",\"site\":\"MSK\",\"country\":\"RU\"}')",
"Bash(node -e \"const d=require\\('fs'\\).readFileSync\\('/dev/stdin','utf8'\\); const a=JSON.parse\\(d\\); a.forEach\\(s=>console.log\\(s.id, s.host, s.name\\)\\)\")",
"Bash(curl -s http://localhost:8000/api/bgp/sessions)",
"Bash(curl -s \"http://localhost:8000/api/bgp/sessions/raw/3\")",
"Bash(node -e \"0\")",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://vpsville.msk.rt.shx.su/rest/routing/ospf/neighbor\")",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://vpsville.msk.rt.shx.su/rest/routing/ospf/instance\")",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://vpsville.msk.rt.shx.su/rest/routing/ospf/area\")",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://vpsville.msk.rt.shx.su/rest/routing/ospf/interface-template\")",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://vpsville.msk.rt.shx.su/rest/ip/route?.type=ospf\")",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://vpsville.msk.rt.shx.su/rest/ip/route\")",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://192.168.0.254/rest/routing/ospf/neighbor\")",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://192.168.0.254/rest/routing/ospf/instance\")",
"Bash(curl -s http://localhost:8000/api/ospf/all)",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://192.168.0.254/rest/routing/bfd/session\")",
"Bash(curl -su \"admin:Ifwrbqifwrbq1995\" \"http://vpsville.msk.rt.shx.su/rest/routing/bfd/session\")",
"Bash(node -e \"const j=JSON.parse\\(require\\('fs'\\).readFileSync\\(0,'utf8'\\)\\); console.log\\('bfd:', JSON.stringify\\(j.bfdSessions, null, 2\\)\\)\")",
"Bash(xargs grep *)",
"Bash(npx shadcn@latest add popover --yes)"
]
},
"env": {
"REUI_LICENSE_KEY": "REUI-EAF4-EA40-6C5C-5D86"
}
}
-67
View File
@@ -1,67 +0,0 @@
---
name: reui
description: Use the ReUI registry from your AI agent - find, install, and correctly use ReUI components (the 22 free building blocks like data-grid, kanban, filters), their free examples, premium blocks, and Motion Icons. Applies in any project using ReUI, the @reui registry, REUI_LICENSE_KEY, or any shadcn project where the user asks for premium blocks, data grids, kanban boards, dashboards, or full pages. Pairs with the free ReUI MCP server for live, scored registry search and inline component APIs.
user-invocable: false
allowed-tools: Bash(npx shadcn@latest *), Bash(pnpm dlx shadcn@latest *), Bash(bunx --bun shadcn@latest *)
---
> **ReUI skill version `fe6742e29c`.** If the ReUI MCP's `get_agent_skill` reports a newer `version`, re-run the ReUI installer (see `get_agent_skill` -> `install.recommended`) to update this skill. Cloud/tools-only agents have no local file and always read the latest - they can ignore this.
# ReUI for Agents
ReUI is a shadcn-compatible registry. It ships four things you **reuse** - never redesign:
- **components** - the 22 ReUI building blocks with real APIs: `data-grid`, `kanban`, `filters`, `date-selector`, `tree`, `stepper`, ... (free)
- **examples** - free `c-*` single-pattern use-cases of a component (`c-kanban-1`); install one and read it to see exact composition
- **blocks** - premium full-page sections that compose components (`data-grid-2`, `pricing-page-1`); Pro or Ultimate license at install
- **icons** - Motion Icons in 4 styles, static + hover-animated variants; Ultimate license at install
The skill is free and this MCP is free to use; it just needs a ReUI account. On first use your agent opens a browser "Sign in with ReUI" prompt (a free account is created if you don't have one). Free covers components and examples with a daily request allowance; a Pro or Ultimate license unlocks premium blocks and Motion Icons and removes the limit (see [rules/registry.md](./rules/registry.md)). The same account and skill work in every agent and service the MCP connects to - this skill is agent-agnostic.
Skill + MCP are a team: this skill is the workflow (how to find, install, read the API, and adapt by reuse); the MCP is the live data and the hands (search, get_component, install commands). Your job: find the right item, install it with the shadcn CLI, read its real API, and **adapt by reuse** - wire real data and theme it; do not hand-roll or restyle what ReUI already provides. This skill **layers on the shadcn skill**: follow that for generic rules (spacing, `cn()`, semantic colors, forms); follow this for everything ReUI-specific.
## The core loop (MCP-native)
1. **Find** - call the ReUI MCP `search` tool with the user's intent. It returns a ranked, scored list across components/examples/blocks/icons, each with an `install` command, `previewUrl`, `docsUrl`, and `componentsUsed`. Pass hints (`type`, `component`, `category`, `features`, `free`) when you can infer them.
2. **Install** - run the returned command non-interactively (`npx shadcn@latest add @reui/<name> --yes`). The CLI resolves deps, aliases, and the base/style from `components.json`. See [cli.md](./rules/cli.md).
3. **Read the API (on your base)** - first note your base from `components.json` -> `style` (`base-nova` -> Base UI, `radix-nova` -> Radix UI). For each component an item uses, call `get_component(name)` and read its **inline `api`** (no web fetch); then `get_examples(name)` to install a worked example and copy its composition - the installed files are already in your base. Whenever you work with a component's API, also **share its `docsUrl`** (the primitive's API documentation page) with the user so they have the full reference. See [components.md](./rules/components.md).
4. **Adapt (reuse-first)** - swap demo data for real data, fix icon imports, align tokens. Do not redesign. See [adapting.md](./rules/adapting.md).
**Always show the preview.** Every item a tool returns carries a `previewUrl` (a live preview page). Whenever you list, recommend, or present ReUI items to the user - blocks, components, examples, or icons, whether from `search`, `search_icons`, `list_components`, `compose_page`, or any getter - include each item's `previewUrl` so they can SEE it before installing. Blocks and examples open an individual live preview; icons and components link to their live category/component page. Never present an item without its preview link.
If the ReUI MCP is not configured, fall back to `npx shadcn@latest search @reui -q "..."` then `add` - but the MCP gives scored matches + inline APIs; prefer it.
## Commands
Run ReUI as explicit slash commands (via the ReUI MCP) **or** just ask in plain language - both run the same workflow.
| Command | Invoke | Does |
| ----------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------ |
| **build** | `/mcp__reui__build <what>` | Compose a page/section/feature from ReUI: plan → install → read API → adapt → craft → audit. |
| **add** | `/mcp__reui__add <item>` | Find & install one component/example/block/icon and wire it in. |
| **fix** | `/mcp__reui__fix [target]` | Diagnose & fix ReUI usage: wrong/undocumented props, base/radix mismatch, missing states, a11y/scroll. |
| **improve** | `/mcp__reui__improve [target]` | Refine + extend existing ReUI UI to a production-exceptional bar (hierarchy, density, states, responsive, motion). |
Invocation differs slightly per agent (`/mcp__reui__build` in Claude Code/Cursor/Windsurf, `/mcp.reui.build` in VS Code). No command surface? Just describe what you want - this skill drives the identical loop.
## When to reach for ReUI vs plain shadcn
| Need | Reach for |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| A full page or section (dashboard, billing, auth, pricing, settings) | `compose_page` first (plans sections + best blocks), then ReUI **blocks** |
| A data table with sorting/filtering/pagination/virtualization | the **data-grid** component (never hand-roll a `<table>`) |
| A drag-and-drop board | the **kanban** component |
| Advanced column filtering, date range, tree, stepper, ... | the matching ReUI **component** |
| A single generic control already in shadcn (Button, Dialog, Select) | plain **shadcn** |
## Detailed references
- [rules/registry.md](./rules/registry.md) - the four types, the @reui registry, base/radix, free vs premium + license
- [rules/workflow.md](./rules/workflow.md) - the find -> install -> read-API -> adapt loop (most important)
- [rules/components.md](./rules/components.md) - the 22 components, the data-grid contract, base vs radix
- [rules/adapting.md](./rules/adapting.md) - reuse-first: preserve the design (no over-customizing), reuse examples + a block's own elements, real data, don't invent APIs
- [rules/craft.md](./rules/craft.md) - make it exceptional: point of view, hierarchy, density, states, responsive, motion, the bar
- [rules/quality.md](./rules/quality.md) - security, accessibility, and scroll gates (the done gate)
- [rules/styling.md](./rules/styling.md) - ReUI extended tokens, theme adaptation, density
- [rules/icons.md](./rules/icons.md) - portable icons, swapping imports, Motion Icons (static + animated)
- [tools.md](./tools.md) - the ReUI MCP: golden path, the 19 tools, token rules, result shapes, errors
-43
View File
@@ -1,43 +0,0 @@
# Adapting installed ReUI code (reuse-first, no AI slop)
ReUI items ship production-quality. Your job is to **adapt by reuse** - wire real data and fit the app - not to redesign or hand-roll. The output should look like ReUI built it for this product.
## Preserve the design - don't over-customize
The design IS the product. A ReUI block/component encodes senior-designer decisions: spacing, hierarchy, density, color treatment, and component choices. The fastest way to turn a premium block back into generic AI slop is to "improve" its look - so don't.
- Change **data, copy, and props**; keep the **structure and styling** it ships with. Make the **smallest** change that wires the real data. If your diff touches `className` / JSX structure more than data / props, you are over-customizing - stop and reuse.
- Don't swap ReUI components for hand-rolled ones, restructure the layout, re-skin spacing / radius / colors, or add decorative chrome. Let the installed components carry the default spacing, radius, sizing, icon rhythm, density, and state styling; add custom Tailwind only when a component genuinely lacks a contract you need.
- Want a different look? `search` for a block whose design already fits and reuse that - don't restyle this one into a new design.
## Reuse the parts: examples and the block's own elements
- **Examples are building parts.** A free `c-*` example is a correct, single-pattern composition you can reuse. Before composing from scratch, `get_examples(component)`, install the closest one, and reuse its wiring - assemble UI from examples instead of hand-rolling what an example already shows.
- **Reuse a block's own elements.** Need more rows, cards, items, or sections than ship by default? Repeat the block's **existing** element by mapping real data through the same markup - never invent parallel markup that drifts from its design. Need a variant (empty / loading / expanded)? Derive it from an element the block already has.
## Don't invent (read, don't guess)
- Never write a prop, variant value, import path, or `@reui/...` name you didn't read in a component's inline `api`, an installed example, or a `search` result. If you didn't see it, treat it as nonexistent - call `get_component` / `get_examples` / `search` first, or run the MCP `validate_usage` tool to check planned names + props against the docs before writing code.
- If a getter returns `found: false` or `search` returns nothing, say so and fall back (plain shadcn, or ask) - never fabricate an install command or an API.
## What to change vs leave alone
- **Change:** the item's own data, copy, props, and layout to fit the app.
- **Leave alone:** installed component files, hooks, and the shared theme - do not edit vendored ReUI internals; change behavior through props and the documented API.
- Blocks are **portable React** - no `next/link`, `next/image`, or other framework-runtime imports inside them. Keep them portable.
## Demo data -> real data
- Replace every placeholder with the user's real data. Model it as **typed data structures** and **map over arrays** - never duplicate JSX per row/card. Keep small block-specific formatters next to the data.
- Wire the real source (columns, fields, fetch). For `data-grid`, implement the server fetch contract if the user needs server-side data.
- **Type from the component API, derive during render.** Type domain state through the component's own types - e.g. map status to `BadgeProps["variant"]` via a typed `Record<Status, …>` - instead of stringly-typed values. Compute view state during render; don't mirror derived data into `useState`/`useEffect`.
- **Adapt on the right base.** Use the API for the project's base (Base UI vs Radix - see [components.md](./components.md)); the installed files are already base-correct, so reuse their shape rather than translating from memory.
## Believable content (no AI tells)
- Use realistic labels, counts, timestamps, and statuses that map to a real workflow.
- No decorative buttons, fake tabs, meaningless toggles, equal-weight card walls, empty gradients, ornamental icons, or generic SaaS filler. Every element should do something.
## Operational surfaces (settings / profile / admin)
Pick ONE archetype and keep the family consistent: a vertical rail (3-6 sections), horizontal tabs (5-8), or a frame/stack. Prefer `frame` for tool-like surfaces, a card for profile-like ones. Don't mix archetypes in one surface.
-60
View File
@@ -1,60 +0,0 @@
# CLI: registry setup, license, non-interactive install
## Registry setup (one-time, per project)
Free items (the 22 components and all `c-*` examples) need only the plain string registry in `components.json`:
```json
{ "registries": { "@reui": "https://reui.io/r/{style}/{name}.json" } }
```
Premium items (blocks; Motion Icons and templates) require a ReUI license at install:
1. Add the key to `.env.local`:
```bash
REUI_LICENSE_KEY=your-license-key
```
2. Switch `components.json` to the authenticated object form:
```json
{
"registries": {
"@reui": {
"url": "https://reui.io/r/{style}/{name}.json",
"headers": { "Authorization": "Bearer ${REUI_LICENSE_KEY}" }
}
}
}
```
The shadcn CLI expands `${REUI_LICENSE_KEY}` from `.env.local` inside `components.json`, but an MCP client config never expands variables, so a ReUI MCP server config must carry the raw token instead (for example `reui_pat_your_token_here`).
The MCP `get_project_context` tool returns the right config. Full guide: https://reui.io/docs/registry?ref=skill
## Installing
Use the project's package runner (check `packageManager`):
```bash
npx shadcn@latest add @reui/<name> --yes # npm
pnpm dlx shadcn@latest add @reui/<name> --yes # pnpm
bunx --bun shadcn@latest add @reui/<name> --yes # bun
```
`--yes` skips confirmation prompts. The CLI auto-detects the package manager from the lockfile (there is no `--package-manager` flag). It also resolves the correct base+style variant from `components.json`, so do not pass a style.
## Handling prompts and conflicts
- **Always pass `--yes`** so the CLI does not block on confirmation prompts.
- **Do NOT pass `--overwrite` by default.** If the CLI reports an existing file, read the output and resolve deliberately: install under a different name, adjust the path, or ask the user. Only use `--overwrite` when the user explicitly wants to replace a file.
- **Preview first when touching an existing project**: `npx shadcn@latest add @reui/<name> --dry-run` shows what would change; `--diff <file>` shows a specific file's diff. Use these before overwriting.
- Run from the **project root** so `components.json` and `.env.local` are found.
## Free vs premium boundary
- Public, no key: `c-*` examples and the 22 components (`@reui/data-grid`, `@reui/badge`, ...) that those examples depend on.
- Key required at install: blocks (`@reui/<category>-N`) need a Pro or Ultimate license; Motion Icons (`@reui/icons/...`) and templates need Ultimate.
If an install 401/403s, the license key is missing, invalid, or the plan does not cover that resource (blocks: Pro or higher; icons and templates: Ultimate). Point the user to https://reui.io/account?ref=skill (their key) or https://reui.io/pricing?ref=skill (upgrade).
-428
View File
@@ -1,428 +0,0 @@
# ReUI components
The 22 ReUI building blocks: `alert`, `autocomplete`, `badge`, `cascader`, `code-block`, `data-grid`, `date-selector`, `event-calendar`, `filters`, `frame`, `gantt`, `icon-stack`, `icon-tile`, `kanban`, `number-field`, `phone-input`, `rating`, `scrollspy`, `sortable`, `stepper`, `timeline`, `tree`. Examples and blocks are composed from these.
**Rule one: never guess a component's API. Read it first.** Call **`get_component(name)`** for its inline `api` (props + usage, no web fetch), and **share the result's `docsUrl`** (the component's API documentation page) with the user whenever you work with that component's API, so they have the full reference (the `/llms.txt` index is a further fallback). Then call **`get_examples(name)`** to install a worked example and copy real composition. The contracts below are first-try orientation (required props, composition shape, the one gotcha); the inline `api` is the full reference. No single block fits? Compose: search the components you need, read each `get_component`, install a `get_examples` example per component, and adapt.
## data-grid (the flagship - read its API every time)
`data-grid` wraps TanStack Table v9. It is NOT a styled `<table>` and does NOT take `data`/`columns` props directly. The contract:
- Build a TanStack table instance with `useTable({ features: dataGridFeatures, ... })` (columns, data). `dataGridFeatures` is exported by the primitive and already bundles sorting, filtering, pagination, row selection, expanding, pinning, resizing and faceting, so there are no per-table row models to wire.
- Pass that instance to `<DataGrid table={table} recordCount={total}>`.
- Compose the body with `DataGridTable` inside `DataGrid`, and enable features through `tableLayout` (e.g. `{ headerSticky: true, columnsResizable: true }`), not ad-hoc classes.
- Server-side data uses the documented fetch shape (`recordCount` is the total for pagination).
```tsx
const table = useTable({
features: dataGridFeatures,
data,
columns,
})
<DataGrid table={table} recordCount={data.length}>
<DataGridTable />
</DataGrid>
```
Common mistakes:
- **Incorrect:** `<DataGrid data={rows} columns={cols} />` - these props do not exist. **Correct:** build a `useTable({ features: dataGridFeatures, ... })` instance and pass `table={table}` + `recordCount`.
- **Incorrect:** a raw `<table>` / hand-rolled pagination. **Correct:** use `data-grid`; read its API for sticky header, pagination, virtualization, row selection.
- **Incorrect:** styling rows/cells with arbitrary classes. **Correct:** drive layout via `tableLayout` and the primitive's `DataGridColumnMeta` (e.g. `cellClassName`, `headerTitle`), set through the bundle's `columnMeta` slot.
## event-calendar
**Required:** events via `events`/`onEventsChange` (controlled) or `defaultEvents` (uncontrolled), plus a height on the root.
**Shape:**
```tsx
<EventCalendar defaultEvents={events} defaultView="month" className="h-[560px]">
<EventCalendarNav />
<EventCalendarContent />
</EventCalendar>
```
**Gotcha:** headless-first: `EventCalendarContent` renders the active view (month/week/day/days/agenda; a resource view activates when `resources` is passed) - there is no per-view JSX to compose. Events are `{ id, title, start, end (exclusive), allDay?, color?, recurrence?, resourceId? }`. Mutations flow through `onEventUpdate`/`canDropEvent` (return `false` to reject); the root needs an explicit height because it is a min-h-0 flex column.
## gantt
**Required:** `resources` (the left tree) plus bars via `events`/`defaultEvents` attached by `resourceId`.
**Shape:**
```tsx
<Gantt defaultEvents={bars} resources={tasks} defaultScale="month" className="h-[480px]">
<GanttNav />
<GanttView />
</Gantt>
```
**Gotcha:** bars move along the time axis only (never across rows) and are all-day spans with exclusive `end`; `progress` is 0-100. Scales are `day | week | month | quarter | year`. Zoom control, infinite scroll, summary rollups, and row checkboxes are ON by default - turn off what you do not need. Same `onEventUpdate`/`canDropEvent` commit pipeline as `event-calendar`; the root needs an explicit height.
## kanban
**Required:** `value` (`Record<string, T[]>`), `onValueChange`, `getItemValue`
**Shape:**
```tsx
<Kanban value={cols} onValueChange={setCols} getItemValue={(i) => i.id}>
<KanbanBoard>
{Object.entries(cols).map(([id, items]) => (
<KanbanColumn key={id} value={id}>
<KanbanColumnHandle><h3>{id}</h3></KanbanColumnHandle>
<KanbanColumnContent value={id}>
{items.map((i) => (
<KanbanItem key={i.id} value={i.id}>
<KanbanItemHandle>{i.title}</KanbanItemHandle>
</KanbanItem>
))}
</KanbanColumnContent>
</KanbanColumn>
))}
</KanbanBoard>
<KanbanOverlay><div className="bg-muted size-full rounded-md" /></KanbanOverlay>
</Kanban>
```
**Gotcha:** state is `Record<columnId, T[]>`. Each `KanbanColumnContent value` must match its parent `KanbanColumn value`. Omit `KanbanOverlay` and the drag preview silently breaks.
## sortable
**Required:** `value` (`T[]`), `onValueChange`, `getItemValue`
**Shape:**
```tsx
<Sortable value={items} onValueChange={setItems} getItemValue={(i) => i.id}>
{items.map((i) => (
<SortableItem key={i.id} value={i.id}>
<SortableItemHandle><GripVertical /></SortableItemHandle>
{i.label}
</SortableItem>
))}
</Sortable>
```
**Gotcha:** a flat 1D reorder list (not columns - that is `kanban`). `getItemValue` must return a stable, unique string. Pass `layout="grid"` or `layout="nested"` for non-list layouts.
## filters
**Required:** `fields` (`FilterField[]`). The value is ONE `FilterQuery` tree - `query` + `onQueryChange`, or uncontrolled `defaultQuery`.
**Shape:**
```tsx
const fields: FilterField[] = [
{ id: "title", label: "Title", type: "text" },
{
id: "status",
label: "Status",
type: "select",
options: [
{ value: "active", label: "Active" },
{ value: "archived", label: "Archived" },
],
},
]
const [query, setQuery] = useState<FilterQuery>(() => createFilterQuery())
<Filters fields={fields} query={query} onQueryChange={setQuery} />
```
**Gotcha:** the state is a TREE, not a list of chips. `FilterQuery` is a group of rules joined by `and`/`or` and a group may hold another group, so `(A and B) or C` is expressible; a rule is `{ id, type: "rule", path: ["status"], operator, value }` and `path` is the whole nested attribute path, root first. The pre-rewrite API is GONE: there is no `filters`/`onChange` prop, no `FilterFieldConfig` (fields are `FilterField`, nested through their own `fields`, keyed `id` not `key`), and no `createFilter()` - it minted ids inside a pure function and broke hydration, so ids now come from `createFilterIdFactory(seed)` seeded off `useId`, and `createFilterQuery()` / `createFilterRule()` take one. Read the query back with `flattenFilterConditions` (`{ path, field, operator, values, negated }` per rule, incomplete rules skipped) and walk the tree yourself when the parentheses carry meaning - the primitive compiles nothing, no SQL, no query string.
`variant` picks the chrome over that one query: `"basic"`, the default, is the flat chip row for a toolbar over a table; `"advanced"` is the condition builder, hung off a trigger or rendered in place with `advancedMode="inline"`. Both read and write the same tree, so a saved view built in one opens in the other. Other props worth knowing before you hand-roll them: `size` is two rungs, `"sm" | "default"`, resolved per style (there is no `lg`); `reorderable` turns on drag and Alt+Arrow row moves in the builder; `onBeforeQueryChange` is the ONE veto point for every write (return `false` to refuse, it cannot rewrite); `editors` registers custom value editors a field selects by `editor` name; `labels` / `operatorLabels` own every rendered string; `pathCollapse` + `maxPathSegments` shorten deep attribute paths; `renderChip` / `renderValue` / `renderEmpty` replace rendered parts. On a field, `loadOptions` supplies async options with paging and `resolveValues` renders a chip restored from a saved view whose option was never loaded. Pairs naturally with `data-grid`.
## cascader
**Required:** `items` (a tree of `{ value, label, children? }`), plus the panel parts inside `CascaderContent`.
**Shape:**
```tsx
<Cascader items={items} value={value} onValueChange={setValue}>
<CascaderTrigger render={<Button variant="outline" />}>
<CascaderValue placeholder="Select an attribute" />
</CascaderTrigger>
<CascaderContent className="w-80">
<CascaderPanel>
<CascaderNav>
<CascaderBreadcrumb />
<CascaderInput />
</CascaderNav>
<CascaderEmpty />
<CascaderList maxHeight={288}>
<CascaderItems />
</CascaderList>
<CascaderStatus />
</CascaderPanel>
</CascaderContent>
</Cascader>
```
**Gotcha:** pressing a branch NAVIGATES, it does not select - only leaves are selectable until you pass `selectable="any"` or a predicate, and once a branch is selectable its chevron becomes the only way to open it. `CascaderInput` must stay inside `CascaderContent` (Base UI refills the query from the selection when the input sits outside the popup). Always include `CascaderStatus`: it is the live region announcing level changes, which the visual breadcrumb does not provide to screen readers. Accepts a flat adjacency list via `getParent` as well as nested `children`. `searchScope="deep"` searches every level and annotates results with their path; `multiple` gives checkbox rows; `inline` + a bare `CascaderPanel` embeds it with no popover.
The shape above is `mode="drill"`, the default. `mode="tree"` keeps the same parts (drop `CascaderBreadcrumb`, pass `showBack={false}`, drive expansion with `expanded`/`onExpandedChange`); `mode="columns"` REPLACES `CascaderList` + `CascaderItems` with a single `CascaderColumns`, and has no breadcrumb. Other props worth knowing before you hand-roll them: `cascade` (multi-select only, parent/child selection with indeterminate branches - pair it with `selectable="any"`, since a leaf-only tree can never cascade), `indicator={false}` to drop the single-select check and its gutter (visual only, no-op with `multiple`), `virtualize`/`virtualizeThreshold` plus `CascaderVirtualItems` for long levels, and `getChildren` for async levels with cursor paging, retry on failure and optional `prefetch`. `CascaderFooter` pins commands below the list (`actions` is the quick path) and `CascaderSubmenu` opens one as a side-anchored flyout with the full menu keyboard model. To head a run of rows use `CascaderGroup` wrapping a `CascaderLabel` - a bare label inside a listbox names nothing and is dropped from the accessibility tree - and `CascaderSeparator` for the rule between runs. Every rendered string comes from `labels`, and the panel is RTL-correct under a `DirectionProvider` or `dir="rtl"`.
## date-selector
**Required:** none, but wire `onChange` to capture the value.
**Shape:**
```tsx
const [value, setValue] = useState<DateSelectorValue | undefined>()
<DateSelector value={value} onChange={setValue} label="Due date" />
```
**Gotcha:** the value is a structured `DateSelectorValue` (period / operator / start+end dates), NOT a `Date` - never pass a raw `Date`. Use `allowRange={false}` to lock single-date picking. Read `get_component("date-selector")` for the value shape.
## tree
**Required:** `tree` (a `@headless-tree/core` instance you construct)
**Shape:**
```tsx
<Tree tree={tree}>
{tree.getItems().map((item) => (
<TreeItem key={item.getId()} item={item}>
<TreeItemLabel />
</TreeItem>
))}
</Tree>
```
**Gotcha:** `Tree` is a styled shell - it takes a headless-tree instance via `tree`, NOT `data`/`items` props. Build the instance with `@headless-tree/react`. External API: https://headless-tree.lukasbach.com/
## stepper
**Required:** `StepperItem step` (number), `StepperContent value` (number)
**Shape:**
```tsx
<Stepper defaultValue={1}>
<StepperNav>
<StepperItem step={1}>
<StepperTrigger><StepperIndicator>1</StepperIndicator></StepperTrigger>
<StepperSeparator />
</StepperItem>
<StepperItem step={2}>
<StepperTrigger><StepperIndicator>2</StepperIndicator></StepperTrigger>
</StepperItem>
</StepperNav>
<StepperPanel>
<StepperContent value={1}>Step 1 content</StepperContent>
<StepperContent value={2}>Step 2 content</StepperContent>
</StepperPanel>
</Stepper>
```
**Gotcha:** steps are 1-indexed. Without `StepperPanel` + `StepperContent` you render the nav trail but no body. Put `StepperSeparator` in every `StepperItem` except the last.
## timeline
**Required:** `TimelineItem step` (number)
**Shape:**
```tsx
<Timeline>
<TimelineItem step={1}>
<TimelineHeader>
<TimelineDate>March 2024</TimelineDate>
<TimelineTitle>Project initialized</TimelineTitle>
</TimelineHeader>
<TimelineIndicator />
<TimelineSeparator />
<TimelineContent>Repo and architecture set up.</TimelineContent>
</TimelineItem>
</Timeline>
```
**Gotcha:** each item needs a unique `step`. `orientation` is `"vertical"` (default) or `"horizontal"`. This is a static event display, not interactive like `stepper`.
## autocomplete
**Required:** `items` (array; each item has at least `value`)
**Shape:**
```tsx
<Autocomplete items={items}>
<AutocompleteInput placeholder="Search..." />
<AutocompleteContent>
<AutocompleteEmpty>No results found.</AutocompleteEmpty>
<AutocompleteList>
{(item) => (
<AutocompleteItem key={item.value} value={item}>{item.label}</AutocompleteItem>
)}
</AutocompleteList>
</AutocompleteContent>
</Autocomplete>
```
**Gotcha:** `AutocompleteList` takes a render-prop `(item) => ReactNode`, NOT a mapped array of children. External API: https://base-ui.com/react/components/autocomplete
## phone-input
**Required:** none, but wire `onChange`.
**Shape:**
```tsx
<PhoneInput placeholder="Enter phone number" defaultCountry="US" value={value} onChange={setValue} />
```
**Gotcha:** `value`/`onChange` use an E.164 string (e.g. `"+14155551234"`), not a display-formatted string; `onChange` can fire `undefined`. `defaultCountry` is a 2-letter ISO code. Wraps `react-phone-number-input`.
## number-field
**Required:** wrap the controls in `NumberFieldGroup`.
**Shape:**
```tsx
<NumberField defaultValue={0}>
<NumberFieldScrubArea label="Quantity" />
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>
```
**Gotcha:** import from `@/components/ui/number-field`. The accessible label goes on `NumberFieldScrubArea`, not `NumberField`. External API: https://base-ui.com/react/components/number-field
## rating
**Required:** `rating` (number)
**Shape:**
```tsx
<Rating rating={4.5} showValue editable onRatingChange={setRating} />
```
**Gotcha:** supports decimals (partial stars). Pass `editable` + `onRatingChange` for interactive input; omit both for a read-only display.
## scrollspy
**Required:** `targetRef` (the scroll container ref)
**Shape:**
```tsx
<Scrollspy targetRef={containerRef}>
<a href="#s1" data-scrollspy-anchor="s1">Section 1</a>
<a href="#s2" data-scrollspy-anchor="s2">Section 2</a>
</Scrollspy>
<div ref={containerRef}>
<div id="s1">...</div>
<div id="s2">...</div>
</div>
```
**Gotcha:** each link's `data-scrollspy-anchor` must match a section `id`. `targetRef` is the scrollable container (defaults to the window).
## frame
**Required:** `Frame` > `FramePanel`
**Shape:**
```tsx
<Frame>
<FramePanel>
<FrameHeader>
<FrameTitle>Title</FrameTitle>
<FrameDescription>Description</FrameDescription>
</FrameHeader>
<div className="p-5">Content</div>
<FrameFooter>Footer</FrameFooter>
</FramePanel>
</Frame>
```
**Gotcha:** a structured card shell for tool-like surfaces. `stacked` connects multiple panels with shared borders; `dense` removes panel padding; radius via the `--frame-radius` CSS variable.
## icon-stack
**Required:** one child icon
**Shape:**
```tsx
<IconStack aria-hidden="true">
<InboxIcon className="size-4" />
</IconStack>
```
**Gotcha:** isometric layered artwork for empty states and illustrations; style the inner icon via its own `className`. Mark purely decorative stacks `aria-hidden="true"` and keep the real label in surrounding copy.
## icon-tile
**Required:** one child icon
**Shape:**
```tsx
<IconTile variant="elevated" size="lg">
<PackageIcon />
</IconTile>
```
**Gotcha:** the square container an icon sits in, so every list row, feature card and empty state shares one affordance. `variant`: `outline` (default) | `elevated` (muted fill, raised ring) | `soft` (tinted nested, tone from currentColor) | `solid` (filled tone, contrasting glyph) | `frame` (double container). `soft` and `solid` retint from one text color class (they default to `text-primary`). `size`: `xs | sm | default | lg | xl` (24/32/40/48/64px tile, glyph scales 12/14/16/20/24px). `radius`: `default | full`. Do not set a `size-*` class on the child icon unless you mean to override the tile's glyph size; recolor with `className` on the tile, not the icon.
## code-block
**Required:** `code` + `language`, or pre-highlighted `lines`.
**Shape:**
```tsx
<CodeBlock code={code} language="tsx" />
<CodeBlock code={code} language="tsx" showLineNumbers maxLines={20}>
<CodeBlockHeader>
<CodeBlockTitle>use-totals.ts</CodeBlockTitle>
<CodeBlockLanguage />
<CodeBlockCopyButton className="ml-auto" />
</CodeBlockHeader>
<CodeBlockExpandButton />
</CodeBlock>
```
**Gotcha:** the one-liner is already a complete block; every child (header, title, language label, copy button, wrap toggle, expand button, line actions) is optional chrome, at any depth. Shiki is the only npm dependency and loads lazily, one chunk per language, on first highlight; `lines` (from `highlightCode` in `code-block-highlight`, which is server-safe and has no `"use client"`) or `highlight={false}` loads nothing at all. `maxLines` caps the height AND marks the block collapsible, which is what makes `CodeBlockExpandButton` appear. Use `variant="ghost"` when the block sits inside a surface that already has a border.
## alert
**Required:** `Alert` > `AlertTitle`
**Shape:**
```tsx
<Alert variant="success">
<ShieldCheckIcon />
<AlertTitle>Security update</AlertTitle>
<AlertDescription>Enable two-factor authentication.</AlertDescription>
<AlertAction><Button size="xs">Update</Button></AlertAction>
</Alert>
```
**Gotcha:** shadcn-compatible API. `variant`: `default | destructive | info | success | warning | invert`. The non-default variants use ReUI extended color tokens (`--success`/`--info`/`--warning`/`--invert`), which the install adds. Defer generic alert rules to the shadcn skill.
## badge
**Required:** none (text child).
**Shape:**
```tsx
<Badge variant="success-light" size="sm">Success</Badge>
<Badge variant="outline" radius="full">Pill</Badge>
```
**Gotcha:** shadcn-compatible. Rich `variant` set (solid, `-outline`, `-light` per color), `size` `xs..xl`, `radius` `default | full`. Like `alert`, the color variants rely on ReUI extended tokens. Prefer `Badge` variants over raw color classes for statuses.
## base vs radix - write for the project's base
ReUI ships every component in two builds: `base` (Base UI) and `radix` (Radix UI). The install command and name are identical, and the CLI installs the build matching the project. But you must write/adapt code against the **right base**, because their APIs differ.
**Detect the base first.** Read `components.json` -> `style` and take the segment before the first `-`:
- `"style": "base-nova"` -> **Base UI**
- `"style": "radix-nova"` -> **Radix UI**
**Then use that base's API.** The deltas mirror shadcn's base-vs-radix split:
- Slot/composition: Base UI `render={<… />}` vs Radix `asChild`.
- `Select`: Base UI takes `items`; Radix uses `<SelectItem>` children.
- `ToggleGroup`: Base UI `multiple` boolean vs Radix `type="single" | "multiple"`.
The safest path is to **read the installed files and `c-*` examples** - they're already in your base, so reuse their wiring instead of guessing. When `get_component`'s inline `api` or an example shows the other base's shape, translate it to your base (or `validate_usage` to confirm). Defer the generic base/radix mechanics to the shadcn skill.
-45
View File
@@ -1,45 +0,0 @@
# Craft: make ReUI UI exceptional, not generic
ReUI items ship senior-designer quality. Your adaptation has to hold that bar, so the result reads like a real product surface a team would keep - not a wireframe an AI generated. Use these alongside the reuse rules in [adapting.md](./adapting.md).
## Have a point of view
Pick an emotional register before you compose - calm, operational, premium, editorial, dense, energetic - and let layout, spacing, surface treatment, and icon behavior all reinforce it. One or two memorable decisions and restraint everywhere else beats ten generic ones. UI with no point of view reads as generated.
## Brutally clear hierarchy
One focal point per card or panel: the dominant metric or task first, its label second, supporting detail third. The first thing the eye lands on should be the right thing; secondary text must read as secondary. Borders, separators, and surfaces do real work to create 2-3 information bands - don't flatten everything to equal weight.
## Spacing rhythm and deliberate density
Gaps are a signal, not a default. Keep them intentional and consistent within a family (`gap-1`/`gap-2` for tight operational rows, larger gaps for section breaks), and smaller within a group than between groups. Match the surrounding ReUI density; don't pad an operational surface like a marketing page, and don't drift density mid-section. The composition should still feel authored in grayscale.
## Cover the real states (the usual miss)
A surface isn't done at the happy path. Compose, and wire:
- **Empty** - a purposeful empty state (short message + the primary action), never a blank panel.
- **Loading** - a **skeleton** that matches the real layout, not a centered spinner.
- **Error** - an inline, recoverable error with a retry, announced via `role="status"`/`aria-live`.
Derive these from an element the block already has (don't invent parallel markup), or `get_examples` for a state-specific example.
## Responsive by default
Mobile-first, not mobile-afterthought. In constrained rows/cards/sidebars, put `min-w-0` on the shrinking container and `truncate` long single-line labels; protect the primary label's width and let secondary content compress. Reflow layouts (multi-column -> single column) rather than just shrinking them. Desktop and mobile should both look designed.
## Motion, subtly
Motion should clarify, not decorate. Use ReUI Motion Icons on primary actions for a subtle hover cue; keep transitions short (~200-300ms) with calm easing; prefer a skeleton pulse over a spinner. No bouncing, no gratuitous entrance animations on every element.
## Real, activated content
Use believable, typed data (realistic labels, counts, timestamps, statuses that map to a real workflow) - never lorem or abstract filler. Every visible control does something: no decorative buttons, fake tabs, meaningless toggles, or stats with no job. It must still hold with long names, empty values, and crowded data.
## Avoid the AI tells
These instantly read as generated - don't ship them: equal-weight card walls, empty gradients, repetitive padding everywhere, generic enterprise copy, ornamental icons, and number tiles that don't earn their place.
## The bar
Before you finish, ask: **would a product team keep this instead of replacing it? Does it still feel strong after swapping in real content?** If not, reuse the shipped ReUI design harder - don't restyle it into something new - then run the [quality.md](./quality.md) gates.
-39
View File
@@ -1,39 +0,0 @@
# Icons (ReUI delta over shadcn)
Follow the shadcn icon rules (use the project's configured `iconLibrary`, `data-icon` on icons inside `Button`, no sizing classes on icons inside components, pass icons as component objects not string keys). ReUI adds the following.
## Portable icons (library-agnostic)
ReUI components, examples, and blocks are authored to be icon-library-agnostic. When `iconLibrary` is set in `components.json`, the shadcn CLI installs each item's icons in **your** library automatically - you swap nothing. If an installed item's icons don't match your project (for example `iconLibrary` isn't set, so they came in from the item's demo library), change the **import source and component name** to your library, keeping the same icon-name semantics:
- `lucide` -> `lucide-react`
- `tabler` -> `@tabler/icons-react`
- `phosphor` -> `@phosphor-icons/react`
- `remix` -> `@remixicon/react`
- `hugeicons` -> `@hugeicons/react`
Don't assume `lucide-react`; read `iconLibrary` from `components.json`.
## Keep icons purposeful
Icons support the hierarchy, they don't replace it: keep them small, matched to the surrounding density, and decorative ones `aria-hidden="true"` (an icon-only control still needs an accessible label on the control). Don't add ornamental icons that do no job.
## Motion Icons (the `@reui/icons/...` set)
ReUI ships its own icon set in 4 styles (outline, solid, duotone, filled), each icon in two variants:
```bash
npx shadcn@latest add @reui/icons/default/<style>/<name> --yes # static
npx shadcn@latest add @reui/icons/animated/<style>/<name> --yes # hover-animated (motion/react)
```
Finding them via the MCP is free; installing requires an Ultimate license (`REUI_LICENSE_KEY`, see [cli.md](./cli.md)). Reach for a Motion Icon on a primary action when a subtle hover cue helps; keep motion restrained.
Finding icons:
- Several icons (the common case): **`search_icons(concepts[])`** - up to 24 concepts in one call, the best icons per concept with install commands. Pass `animated: true` to get only icons with a hover-animated Motion variant.
- One icon: `search` with `type: "icon"`.
- Icon results and `get_icon` carry `animated: true` and `installAnimated` when an animated variant exists - use those install strings, do not construct paths by hand.
- Every icon result carries a `previewUrl` (its live icon-category page) - **share it with the user** so they can SEE the icon before installing.
The `icon-stack` component composes multiple icons into a stacked display.
-22
View File
@@ -1,22 +0,0 @@
# Quality gates (security, accessibility, scroll)
These are the **done gate**, not a nice-to-have: before you call any ReUI work finished, call the MCP `get_audit_checklist` tool and pass every item below (plus the craft bar in [craft.md](./craft.md)). Then typecheck and lint.
## Security
- Never `dangerouslySetInnerHTML`. Render data as text/components.
- External links (`target="_blank"`) must always pair `rel="noopener noreferrer"`.
- No real PII, secrets, or tokens in demo or committed code. Remote media only from sources the project already allows.
## Accessibility
- Implicit list/card items that navigate get real anchors with a standard hover affordance.
- Icon-only or numeric buttons need an `aria-label`; decorative icons get `aria-hidden`.
- Every non-submit button is `type="button"`.
- Keyboard + focus: everything interactive is reachable in a sensible Tab order with a visible focus ring; layers (dialogs/sheets/menus) trap focus and close on `Escape`. ReUI components ship standard keyboard behavior - read each component's inline `api` rather than re-implementing it.
- Announce async UI: loading and error messages use `role="status"` / `aria-live` so they're not silent to screen readers.
## Scroll mechanics
- Make scroll regions with a parent-owned height: a `min-h-0` + flex chain down to the scroll container. Never guess a `max-h`.
- The scroll container owns `overflow-auto`; ancestors stay `min-h-0` so the height resolves.
-35
View File
@@ -1,35 +0,0 @@
# ReUI registry structure
ReUI is a shadcn-compatible registry with four entity types. **Examples and blocks are built FROM components** - reuse them, don't rebuild.
- **component** - one of the 22 ReUI building blocks with a real API (`data-grid`, `kanban`, `filters`, `date-selector`, `tree`, ...). Install directly (`@reui/data-grid`) or let it come in as a dependency of an example/block. Free. Read its API with `get_component(name)`.
- **example** - a free `c-*` single-pattern use-case of a component (`c-kanban-1`, `c-data-grid-3`). Install one and read it to copy real composition. Find a component's examples with `get_examples(name)`.
- **block** - a premium, full-page section that composes several components (`data-grid-2`, `pricing-page-1`). Pro or Ultimate license at install. Adapts to your active theme via semantic tokens.
- **icon** - Motion Icons in 4 styles (outline, solid, duotone, filled), static (`@reui/icons/default/<style>/<name>`) and hover-animated (`@reui/icons/animated/<style>/<name>`). Ultimate license at install. See [icons.md](./icons.md).
## The @reui registry
Install everything through the shadcn CLI: `npx shadcn@latest add @reui/<name> --yes`. The CLI reads the `@reui` registry from the project's `components.json`. Free items need only the plain string form:
```json
{ "registries": { "@reui": "https://reui.io/r/{style}/{name}.json" } }
```
Premium installs need the authenticated form + `REUI_LICENSE_KEY` in `.env.local` - see [cli.md](./cli.md). The MCP `get_project_context` tool returns the right config.
## Know your base: base or radix
ReUI ships every item in two builds - `base` (Base UI) and `radix` (Radix UI) - with mirrored names. The CLI installs the build matching your project automatically, but **you must write code against the right base's API**. Detect it from `components.json` -> `style`: the segment before the first `-` is the base (`base-nova` -> Base UI, `radix-nova` -> Radix UI). The installed files and `c-*` examples are already in your base - read them and adapt on that base. See [components.md](./components.md) for the API deltas.
**So the MCP's own `docsUrl` and `previewUrl` match your base**, send your `style` to the MCP: add `?style=<your components.json style>` to the ReUI MCP server URL (or set an `X-Reui-Style` header) in your MCP client config - set once, applies to every call. The MCP then resolves docs/preview links to YOUR library (`/docs/components/radix/...`, `/preview/radix/...` for a radix project) instead of the default base; `get_project_context` echoes back the style it currently sees so you can confirm it. Install commands are unaffected (the CLI already installs the right variant). If you notice the MCP returning `base` links for a `radix` project, tell the user to add `?style=` to the server URL.
Blocks adapt to your active theme through semantic tokens and CSS variables - change the theme and every block follows.
## Free vs premium
- **Free, no key:** the 22 components, all `c-*` examples, the ReUI MCP, and this skill.
- **Premium, license required at install:** blocks (Pro or Ultimate), Motion Icons and templates (Ultimate). Set `REUI_LICENSE_KEY` (see [cli.md](./cli.md)).
## Component API index
The canonical index of every component's API docs is **https://reui.io/llms.txt** (returned as `componentsApiUrl` in MCP results). Prefer the inline `api` from `get_component`; use the index/docs as the fallback.
-26
View File
@@ -1,26 +0,0 @@
# Styling (ReUI delta over shadcn)
Follow the shadcn skill for the generic rules (semantic colors not raw values, `gap-*` not `space-y-*`, `size-*`, `cn()`, no manual `dark:` overrides, no overlay `z-index`). This file is only the ReUI-specific additions.
## ReUI extended semantic tokens
ReUI adds semantic tokens beyond shadcn's base set. Use these instead of raw colors for status and emphasis:
- `--success` / `--success-foreground`
- `--info` / `--info-foreground`
- `--warning` / `--warning-foreground`
- `--destructive-foreground` (paired with shadcn's `--destructive`)
- `--invert` / `--invert-foreground` (inverted surfaces)
Use them as Tailwind utilities (`bg-success text-success-foreground`, `text-warning`, ...). They are defined in the project's global CSS and registered with Tailwind (`@theme inline` on v4). If a token is missing in the project, add it to the global CSS file (never a new file) following the same `name` / `name-foreground` convention, exactly as the shadcn customization rules describe.
**Incorrect:** `<span className="text-green-600">Active</span>`
**Correct:** `<Badge variant="success">Active</Badge>` or `<span className="text-success">Active</span>`
## Blocks follow your theme
When you install a block it adapts to your active theme through the semantic tokens above and the project's CSS variables. Don't hardcode style-specific values into installed block code and don't fork it to "restyle" - change the theme via the CSS variables / a preset and every block follows. Want a different look? `search` for a block whose design already fits instead of re-skinning one.
## Density and typography rhythm
ReUI operational UI usually feels dense, not airy. Keep the gap between a title and its supporting description tight by default (`gap-0.5`, `space-y-1`, or `space-y-px`), and smaller than the gap between sections. Match the surrounding ReUI density when you add rows or fields; do not pad operational surfaces like a marketing page.
-54
View File
@@ -1,54 +0,0 @@
# Workflow: find -> install -> read API -> adapt
The core ReUI loop. The MCP tells you what to install and gives you the API; the shadcn CLI installs it; you turn the installed files into correct, themed, data-wired code by **reuse**, not redesign.
## 1. Find (ReUI MCP `search` / `compose_page`)
**Full multi-section page ask?** Call `compose_page(intent, sections?)` FIRST, before searching block-by-block. It returns ordered sections, each with the best block for the intent (top pick + alternates); sections listed in `unavailableSections` have no real inventory - compose those from components, do not force a bad block.
For everything else, call `search` with the user's intent. Pass structured hints whenever you can infer them - you are an LLM, so do the parsing the server cannot:
- `type`: `"component"` (one of the 22 building blocks), `"example"` (a c-\* use-case), `"block"` (a full page/section), `"icon"`.
- `component`: the ReUI component the request implies (`"data-grid"`, `"kanban"`, ...).
- `category`, `features` (e.g. `["sortable","pagination"]`), `free`.
Example: "build a users management page with filters" -> `search({ query: "users management page with filters", type: "block", component: "data-grid", features: ["filters"] })`.
Each result has `install`, `previewUrl`, `docsUrl`, `componentsUsed`, `score`, `termCoverage`, and `whyMatch`. `score` is relative to the top hit (the top is ~100 by construction), not an absolute quality - compare results to each other, and show the user the top options if several score closely; do not silently guess. A low `termCoverage` means a weak match even with a high score - rephrase or widen.
**Always show the preview link.** Whenever you list or recommend items - from `search`, `search_icons`, `list_components`, `compose_page`, or a getter - include each item's `previewUrl` (a live preview page) so the user can SEE it before you install. Blocks and examples link to an individual live preview; icons and components to their live category/component page. This applies to every listing, not only a single pick.
## 2. Install (shadcn CLI)
Run the result's `install` command from the project root, non-interactively:
```bash
npx shadcn@latest add @reui/<name> --yes
```
The CLI reads `components.json`, installs the correct base+style variant, resolves `registryDependencies` (a block pulls in its components), installs npm deps, and rewrites aliases. Do not pass the base/style. See [cli.md](./cli.md).
## 3. Read the API (do not guess props)
Before writing code against any component an item uses:
1. The item's `componentDigests` already give a 1-line contract per component - often enough to wire it. For the full API, call **`get_component(names)`** with ALL of `componentsUsed` in ONE call (it accepts an array) and read each inline `api` - no web fetch. **Share the component's `docsUrl`** (its API documentation page) with the user whenever you work with that component's API, so they have the full reference; the `/llms.txt` index is a further fallback.
2. Call **`get_examples(name)`** for the free `c-*` examples of that component; install one and **read the added files** to copy the exact composition. This is the fastest correct path - the example shows real wiring you adapt, not invent.
3. About to write a prop you did not see in an `api` or installed file? Run **`validate_usage`** BEFORE writing the code - per-prop documented / notDocumented verdicts plus did-you-mean suggestions. notDocumented means read the API, not push on.
## 4. Adapt (reuse-first) - do not skip
Installing files is not the end, and redesigning them defeats the point. First note the project's **base** so you write the right API - read `components.json` -> `style` and take the segment before the first `-` (`base-nova` -> Base UI, `radix-nova` -> Radix UI), see [components.md](./components.md). After `add`:
1. **Read the added files**; keep the composition intact. For a block, verify the components are wired correctly (for `data-grid`: a `useTable({ features: dataGridFeatures, ... })` instance passed as `table`, `recordCount` set - see [components.md](./components.md)).
2. **Replace demo data with the user's real data** via typed structures (see [adapting.md](./adapting.md)).
3. **Fix icon imports** to the project's icon library (see [icons.md](./icons.md)).
4. **Align styling** to semantic tokens and the active theme - no raw colors (see [styling.md](./styling.md)).
5. **Validate before finalizing**: if your adaptation introduced components or props you did not read in an `api` or example, run `validate_usage` on them.
6. **Hit the craft bar** - clear hierarchy, deliberate density, the empty / loading / error states, subtle motion, and mobile-first responsiveness (see [craft.md](./craft.md)). Generic-looking output means you under-reused the design, not that it needs restyling.
7. **Pass the quality gates** (security, a11y, scroll) - call the MCP `get_audit_checklist` tool and clear every item (see [quality.md](./quality.md)).
8. **Typecheck / lint**.
## If no single block fits
Compose from components (`compose_page` tells you which sections have no block inventory via `unavailableSections`). `search` the components you need, read each `get_component` API, install a worked `get_examples` example for each, and assemble by adapting those examples. A block in the same category is a useful reference - install it and read its files to see how ReUI composes those components, then adapt.
-58
View File
@@ -1,58 +0,0 @@
# ReUI MCP: full reference
The ReUI MCP (`https://mcp.reui.io`, Streamable HTTP) is free to use but needs a ReUI account: on first use the agent signs in with ReUI (a free account is created if the user has none), so every request is tied to an account. Free covers components and examples; a Pro or Ultimate license unlocks premium blocks and Motion Icons and removes the daily request limit. It does **discovery + guidance** (search, inline APIs, page planning, validation) and never serves source; the shadcn CLI does **installation**, and the license key lives there (the `@reui` entry in `components.json`, backed by `.env.local`). Goal: from the user's intent to correct, themed, data-wired ReUI code in the **fewest tokens and calls**, with **no guessing**.
## Golden path (token-optimal - follow this order)
Most tasks need 2-4 MCP calls and ZERO web fetches:
1. **`search(query, ...hints)`** -> pick the top 1-3 results. Each result already carries `install`, `previewUrl`, `docsUrl`, `componentsUsed`, `score`, `whyMatch`. The payload is complete - do not call another tool just to "confirm" a result.
2. **`get_component([...componentsUsed])`** in ONE batched call (one name or an array of up to 20) -> read each inline `api`. This **replaces** fetching docs pages. Often skippable: search responses carry `componentDigests`, a compact API contract per referenced component.
3. **`get_examples(component)`** -> install ONE returned `c-*` example, read the added files, copy the composition.
4. **`get_install_command(item)`** only to validate a name you are unsure of (results already include `install`). Run the install with the shadcn CLI (`--yes`).
5. **`get_audit_checklist()`** before declaring done.
If you already know the exact item name, skip `search`. Everything else is situational.
## The 5 task-specific tools (when to reach for each)
- **`compose_page`** - BEFORE building any full page (dashboard, settings, billing, landing). Pass the intent (and optionally the sections you want); it returns ordered sections, each with the best premium block for the intent (top pick + alternates). Sections with no real inventory are listed honestly in `unavailableSections` - compose those from components instead of forcing a bad block.
- **`search_icons`** - whenever you need icons, especially several. Batch up to 24 concepts in one call; each concept returns its best icons with install commands. Pass `animated: true` to get only icons that have a hover-animated Motion variant.
- **`validate_usage`** - BEFORE writing code with component names or props you have not read in an inline `api` or an installed example. It checks planned names + props against the indexed API docs and registry item names; returns did-you-mean suggestions and per-prop documented / notDocumented verdicts. Deterministic, no inference - a notDocumented prop means stop and read the API, not push on.
- **`whats_new`** - when your registry knowledge might be stale (a name 404s, the user mentions an item you don't know). Returns items added/removed per build, newest first.
- **`report_issue`** - when an installed item is actually broken (bad source, wrong dependency, broken preview). Goes straight to the ReUI team; rate-limited 5/hour. Not for usage questions.
## All 19 tools
`search`, `get_block`, `get_example`, `get_icon`, `list_block_groups`, `list_block_categories`, `list_example_categories`, `list_icon_categories`, `list_components`, `get_component`, `get_examples`, `search_icons`, `compose_page`, `validate_usage`, `whats_new`, `report_issue`, `get_install_command`, `get_project_context`, `get_audit_checklist`. The MCP serves the full parameter schemas; do not guess parameters beyond them.
## Token + speed rules
- **Batch `get_component`** - ONE call with the whole `componentsUsed` array, never N calls. Skip it entirely when `componentDigests` already answers the question.
- **Read source by installing** - the MCP serves no source. To read or analyze an item's real code, install it with the shadcn CLI and open the local files. Learn an API from the inline `api` / `componentDigests`, never by reading raw source.
- **Infer `search` hints yourself** (`type`, `component`, `category`, `features`, `free`) - hints shrink the result set and the tokens. Keep `limit` low; one right result beats ten.
- Run independent calls (and the shadcn install) concurrently in one turn - serial tool calls are the main source of slowness.
- Don't repeat a search for the same intent; don't call `list_*` to "see everything" - `search` is the entry point, `list_*` is only for browsing a taxonomy the user explicitly wants to explore.
- Prefer `get_component`'s inline `api` over `docsUrl` / `/llms.txt`. Fetch a web page only as a last resort.
## Result shapes (so you don't re-fetch)
- `score` is 0-100 RELATIVE to the top hit (the top is ~100 by construction), not absolute - compare results to each other.
- `termCoverage` (0-1) is the share of the query the item matched - low means a weak match even if the score looks high; rephrase or widen the search.
- Each result carries `whyMatch`, `install`, docs/preview URLs, and a `free` flag; premium items carry `requiredPlan` (`"pro"` for blocks, `"ultimate"` for icons).
- `componentDigests` is a top-level map: a compact API contract per referenced component - often enough to wire an item without a `get_component` call.
- Icon results and `get_icon` include `animated: true` and `installAnimated` when a hover-animated Motion variant exists (animated: `@reui/icons/animated/<style>/<name>`; static: `@reui/icons/default/<style>/<name>`).
## Error playbook
- **401** - the MCP requires a signed-in ReUI account. The client prompts "Sign in with ReUI" (OAuth) on first use; a free account is created if needed. For headless/CI, pass a personal token (`reui_pat_...`, created at https://reui.io/account/mcp?ref=skill) as `Authorization: Bearer`.
- **403 / locked result** - a valid account but the plan does not cover the item: premium blocks need Pro, Motion Icons need Ultimate. Point to https://reui.io/pricing?ref=skill (upgrade). Free accounts still get all components + examples.
- **429** - rate limited (120 requests/min per IP); back off, honor `Retry-After`.
- **not found** (`found: false`) - use the returned `suggestions`, or `search`; check `whats_new` if you suspect a stale name. Never run a fabricated install command.
## Fallbacks
- No ReUI MCP: `npx shadcn@latest search @reui -q "..."` then `add` (generic, no scoring / inline API).
- The shadcn project's own MCP also works over the `@reui` registry: https://ui.shadcn.com/docs/mcp.
Per-agent MCP setup: https://reui.io/docs/mcp?ref=skill
-16
View File
@@ -1,16 +0,0 @@
# CodeGraph data files
# These are local to each machine and should not be committed
# Database
*.db
*.db-wal
*.db-shm
# Cache
cache/
# Logs
*.log
# Hook markers
.dirty
-3
View File
@@ -1,3 +0,0 @@
[mcp_servers.reui]
url = "https://mcp.reui.io/api/mcp?style=base-nova"
bearer_token_env_var = "REUI_LICENSE_KEY"
-21
View File
@@ -1,21 +0,0 @@
{"sessionId":"00844d","runId":"initial","hypothesisId":"H6","location":"components/debug-runtime-probe.tsx","message":"cursor-ref-count-on-mount","data":{"pathname":"/bgp","count":80},"timestamp":1777738439411}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H5","location":"components/debug-runtime-probe.tsx","message":"route-change","data":{"pathname":"/bgp","search":""},"timestamp":1777738439411}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"provider-mounted","data":{"mode":"mock","backendUrl":"http://localhost:8000","storedMode":"live","storedBackend":"http://localhost:8000"},"timestamp":1777738439416}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"hydrated-from-storage","data":{"mode":"live","backendUrl":"http://localhost:8000"},"timestamp":1777738439428}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"provider-mounted","data":{"mode":"live","backendUrl":"http://localhost:8000","storedMode":"live","storedBackend":"http://localhost:8000"},"timestamp":1777738439603}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H4","location":"lib/data-source.tsx","message":"check-backend-start","data":{"url":"http://localhost:8000"},"timestamp":1777738439605}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H4","location":"lib/data-source.tsx","message":"check-backend-result","data":{"url":"http://localhost:8000","ok":true,"status":200},"timestamp":1777738439621}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"provider-mounted","data":{"mode":"mock","backendUrl":"http://localhost:8000","storedMode":"live","storedBackend":"http://localhost:8000"},"timestamp":1777738508126}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"provider-mounted","data":{"mode":"mock","backendUrl":"http://localhost:8000","storedMode":"live","storedBackend":"http://localhost:8000"},"timestamp":1777738508134}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"hydrated-from-storage","data":{"mode":"live","backendUrl":"http://localhost:8000"},"timestamp":1777738508145}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"hydrated-from-storage","data":{"mode":"live","backendUrl":"http://localhost:8000"},"timestamp":1777738508145}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"provider-mounted","data":{"mode":"live","backendUrl":"http://localhost:8000","storedMode":"live","storedBackend":"http://localhost:8000"},"timestamp":1777738508204}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H4","location":"lib/data-source.tsx","message":"check-backend-start","data":{"url":"http://localhost:8000"},"timestamp":1777738508205}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H4","location":"lib/data-source.tsx","message":"check-backend-result","data":{"url":"http://localhost:8000","ok":true,"status":200},"timestamp":1777738508228}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"provider-mounted","data":{"mode":"mock","backendUrl":"http://localhost:8000","storedMode":"live","storedBackend":"http://localhost:8000"},"timestamp":1777738508553}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"provider-mounted","data":{"mode":"mock","backendUrl":"http://localhost:8000","storedMode":"live","storedBackend":"http://localhost:8000"},"timestamp":1777738508560}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"hydrated-from-storage","data":{"mode":"live","backendUrl":"http://localhost:8000"},"timestamp":1777738508571}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"hydrated-from-storage","data":{"mode":"live","backendUrl":"http://localhost:8000"},"timestamp":1777738508572}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H7","location":"lib/data-source.tsx","message":"provider-mounted","data":{"mode":"live","backendUrl":"http://localhost:8000","storedMode":"live","storedBackend":"http://localhost:8000"},"timestamp":1777738508662}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H4","location":"lib/data-source.tsx","message":"check-backend-start","data":{"url":"http://localhost:8000"},"timestamp":1777738508663}
{"sessionId":"00844d","runId":"initial","hypothesisId":"H4","location":"lib/data-source.tsx","message":"check-backend-result","data":{"url":"http://localhost:8000","ok":true,"status":200},"timestamp":1777738508676}
-34
View File
@@ -1,34 +0,0 @@
---
description: "Generate Commit Message: русский subject, fix vs feat, Conventional Commits"
alwaysApply: true
---
# Git commit message (Generate Commit Message)
При **Generate Commit Message**, автогенерации subject и предложении `git commit`:
## Обязательно
- Subject и body — **только на русском** (после `:` в conventional commit).
- Локальный hook `.githooks/commit-msg` **отклоняет** коммиты без кириллицы в subject.
- Префикс и scope — английский: `fix(ui):`, `chore(rules):`.
## Тип: сначала не `feat`
- **`fix`** — баг, регрессия, правка вёрстки/модалки, некорректное поведение, CI/CD, мелкий дефект.
- **`chore` / `docs` / `ci` / `build`** — правила Cursor, README, workflow, зависимости, рефакторинг без новой UX-фичи.
- **`feat`** — только **новая** пользовательская возможность (страница, API, сценарий), которой не было.
Если сомневаешься между `feat` и `fix` — выбирай **`fix`** или **`chore`**, не `feat`.
## Примеры
- `fix(ui): выровнять модалку сводки релиза`
- `fix(ci): исправить сборку release manifest`
- `chore(rules): усилить правила русских commit message`
- `feat(releases): добавить страницу релизов`
## Запрещено
- `feat: update layout`, `fix: add modal`, `chore: update rules` — английский subject.
- `feat` для правки UI, правил, документации и багфиксов.
-44
View File
@@ -1,44 +0,0 @@
---
description: "Быстрый запуск локального frontend и backend"
alwaysApply: true
---
# Локальный запуск проекта
## Требования
- **Node.js 22**, npm с workspaces.
- Первый раз (или после смены зависимостей): `npm install` из корня репозитория.
- Backend: скопировать `backend/.env.example` → `backend/.env` (по умолчанию `PORT=8000`, `CORS_ORIGIN=http://localhost:3000`).
## Запуск (два процесса)
Из корня репозитория поднять **два** long-running процесса в **отдельных** терминалах:
```powershell
npm run dev
npm --prefix backend run dev
```
| Сервис | URL | Проверка |
|--------|-----|----------|
| Frontend (Next.js 16, Turbopack) | http://localhost:3000 | открыть в браузере |
| Backend (Fastify) | http://localhost:8000 | `GET /health` |
Проверка backend в PowerShell:
```powershell
Invoke-WebRequest -Uri http://localhost:8000/health -UseBasicParsing | Select-Object -ExpandProperty Content
```
Ожидаемый ответ: `{"status":"ok",...}`.
## Поведение агента
Если пользователь просит «запусти проект», «запусти фронт и бэк» или похожее:
1. Сначала проверить уже запущенные терминалы — не дублировать процессы.
2. Запустить обе команды выше как фоновые long-running процессы.
3. Дождаться готовности: frontend — `Ready`, backend — `Server listening` / успешный `/health`.
Подробности архитектуры и env — `README.md`, раздел «Запуск».
-131
View File
@@ -1,131 +0,0 @@
---
description: "Fastify 5 + Zod + Drizzle + SQLite — модульная архитектура, слои, валидация, ошибки (ответы на русском)"
globs: "**/{features,shared,lib,plugins}/**/*.ts,**/app.ts,**/server.ts"
alwaysApply: false
---
# Backend: Fastify 5 + TypeScript (ESM) + Zod + Drizzle + SQLite
**Язык:** все ответы пользователю — **только на русском**.
**Стек:** TypeScript (`"type": "module"`), **Fastify 5.x**, **Zod** + `@fastify/type-provider-zod`, **SQLite** (`better-sqlite3`) + **Drizzle ORM**, `@fastify/cors`, `pino-pretty`, `undici`, `dotenv`.
**Цель:** масштабируемый модульный backend, предсказуемый DX, строгое разделение ответственности.
---
## 1. Архитектура (критично)
- **Feature-based модули**, не монолитные файлы. Каждая фича — изолированная папка.
Структура фичи:
```text
/features/<feature-name>/
route.ts — регистрация маршрутов Fastify
controller.ts — разбор запроса/ответа, вызов сервиса
service.ts — бизнес-логика (переиспользуемая)
schema.ts — Zod-схемы (params, query, body, response)
repository.ts — только Drizzle-запросы к БД
types.ts — опционально
```
**Запреты:** не смешивать слои; **нет** бизнес-логики в `route`; **нет** SQL/Drizzle в `controller`; сервисы не должны дублировать обязанности репозитория.
---
## 2. Fastify
- Регистрация фич через **плагины** и инкапсулированные модули.
- Везде **async/await**; декорации инстанса Fastify — только при реальной необходимости.
---
## 3. Валидация (Zod обязательна)
- **Все входы** — через Zod; `@fastify/type-provider-zod` для типобезопасности.
- Явно задавать схемы: **params**, **query**, **body**, **response** (где уместно).
- Нет «сырого» входа; схемы **переиспользовать** между слоями (импорт из `schema.ts`).
---
## 4. База данных (Drizzle)
- Любой доступ к БД — **только** из `repository.ts` / слоя репозитория.
- Типизированные запросы Drizzle; **без** raw SQL, если нет веской причины.
- Запросы — простые, композируемые; логику БД не выносить наружу репозитория.
- Учитывать возможную смену БД: избегать sqlite-специфичных костылей вне репозитория/миграций.
---
## 5. Ошибки
- Единый формат ошибок API (код/сообщение для клиента).
- Обрабатывать: ошибки валидации, БД, внешних HTTP (`undici`).
- **Не** отдавать клиенту внутренние детали (стейки, SQL, секреты).
---
## 6. Логирование
- **Pino**; в dev — **pino-pretty**.
- Логировать ошибки и значимые действия; не засорять лог шумом.
---
## 7. Внешние HTTP (undici)
- Вызовы только из **service** (или выделенного клиента, вызываемого из сервиса).
- Таймауты, обработка ошибок сети/статусов, без утечки сырого ответа наружу.
---
## 8. Конфигурация окружения
- `dotenv` для локальной разработки; **валидация env** (предпочтительно через Zod-схему).
---
## 9. Организация кода
- Небольшие файлы, одна ответственность на файл.
- Композиция вместо копипаста; общее — в `/shared/` и `/lib/`.
---
## 10. Масштабируемость
- Расширяемые границы модулей, слабая связанность, явные контракты между слоями.
---
## 11. Процесс изменений (обязательно)
Для каждого изменения в ответе указать: что сделано; зачем (архитектура + практики); затронутые файлы; минимальный полный дифф без лишнего рефакторинга.
---
## 12. Формат ответа агента
1. Что изменено
2. Почему (архитектура + практики)
3. Структура файлов (если новая фича)
4. Код (несколько файлов при необходимости)
5. Замечания / улучшения
---
## 13. Антипаттерны (строго запрещено)
- Толстые route handlers
- Смешение БД и бизнес-логики в одном месте
- Пропуск валидации входа
- Глобальное мутабельное состояние
- Дублирование запросов к БД
- Хардкод секретов и конфигурации (всё через env/конфиг)
---
## Роль агента
Вести себя как **senior backend engineer**: чистый модульный код, строгое разделение слоёв, долгосрочная поддерживаемость и предсказуемый процесс разработки.
-38
View File
@@ -1,38 +0,0 @@
---
description: "[Multi-agent] Роль ANALYZER — анализ кодовой базы перед реализацией, без кода"
alwaysApply: false
---
# Агент: ANALYZER (анализатор)
Ты выполняешь **только фазу анализа** перед любыми изменениями кода.
## Язык
- Все ответы пользователю — **на русском**.
## Обязанности
- Искать по **всему** репозиторию релевантные места.
- Зафиксировать:
- похожие компоненты и дубликаты по смыслу;
- существующие UI-паттерны, **особенно в зоне `/servers`** (`app/(main)/servers` и связанные компоненты);
- общие хуки, утилиты, UI-обёртки;
- стратегии загрузки данных (RSC, route handlers, server actions, клиентский fetch — что уже принято в проекте).
- Явно отметить **риски дублирования** и конфликты с текущими абстракциями.
## Запреты
- **Нельзя** писать реализацию (патчи, готовый production-код, большие вставки «как сделать»). Допустимы короткие псевдокод-намёки уровня «расширить X», без полной реализации.
- **Нельзя** предлагать «с нуля», если в кодовой базе уже есть переиспользуемое решение — приоритет **reuse**.
## Формат вывода (строго)
1. **Релевантные файлы** (пути).
2. **Существующие паттерны** (кратко, с привязкой к файлам).
3. **Что переиспользовать** (компоненты, хуки, утилиты).
4. **Риски и конфликты** (дубли, расхождение с `/servers`, ломкие зависимости).
## Цель фазы
Дать Implementer’у однозначный вход: **что трогать, что не плодить, откуда копировать поведение**.
-30
View File
@@ -1,30 +0,0 @@
---
description: "[Multi-agent] Глобальные правила пайплайна (Next 16.2.4, Turbopack, shadcn) — подключать с ролями"
alwaysApply: false
---
# Глобальные правила (все агенты пайплайна)
**Язык:** отвечать пользователю **только на русском**.
## Стек и документация
- Next.js **16.2.4** (App Router, **Turbopack**), React, **shadcn/ui**.
- Сверять решения с **официальной** документацией Next.js и shadcn для версий проекта. Не придумывать API и недокументированные паттерны.
## Встроенные возможности Next.js (предпочтительно)
- App Router, Server Components, Server Actions, Route Handlers — вместо обходных кастомных путей, если задача покрывается фреймворком.
## Эталон UI
- **`app/(main)/servers`** — эталон **поведения** CRUD. Оболочка ops: **Frame + DataGrid**, не Card-shell. Не копировать `DataPageCard` на новые экраны.
## Качество и совместимость
- Устранять: ошибки сборки, TypeScript, гидратации, предупреждения о плохих практиках; учитывать **Turbopack** в dev.
## Связь с проектом
- Полные стандарты проекта (формат ответа, чеклисты) — в правиле **`next-shadcn-production.mdc`** (обычно уже подключено). Эти правила **дополняют** его ролью в пайплайне, не отменяют.
- Сообщения коммитов: **`commit-messages-ru.mdc`** (язык и `fix` vs `feat`), semver bump — **`release-versioning.mdc`**.
-37
View File
@@ -1,37 +0,0 @@
---
description: "[Multi-agent] Роль IMPLEMENTER — реализация по результатам анализа, /servers, shadcn"
alwaysApply: false
---
# Агент: IMPLEMENTER (реализатор)
Ты пишешь **production-ready** изменения **на основе** отчёта Analyzer (и уточнённой задачи пользователя).
## Язык
- Все ответы пользователю — **на русском**.
## Обязанности
- Внедрять фичу **минимально**, но **полностью**: все затронутые файлы (типы, UI, API, actions/handlers) согласованы.
- **Переиспользовать** существующие абстракции из отчёта анализа и кодовой базы.
- Выравнивать UI/UX с **`/servers`** как с главным эталоном (layout, отступы, сетка, типографика, композиция).
- Предпочитать **Server Components**; `'use client'` — только при необходимости интерактива (состояние, браузерные API, обработчики).
- UI: по возможности **shadcn/ui**; не вводить новый визуальный/UX-паттерн, если на `/servers` уже есть эквивалент.
- При завершении задачи предлагать subject по **`commit-messages-ru.mdc`**: правки UI/багов → `fix`, инфраструктура/правила → `chore`, новая фича → `feat`; описание **на русском**.
## Формат вывода (строго)
1. **Что изменено** (файлы и суть).
2. **Почему так** — связка Next.js + shadcn/ui + эталон **`/servers`** + принятые в репозитории паттерны.
3. **Код** — диффы или полные фрагменты по всем затронутым файлам.
4. По возможности: **Server vs Client** для новых/изменённых границ компонентов.
## Запреты
- Не оставлять «полфичи» (забытые типы, сломанные импорты, несогласованный API).
- Не копипастить большие куски без привязки к существующим компонентам, если анализ уже указал переиспользование.
## Согласование с проектом
- Если активно правило **`next-shadcn-production.mdc`**, выдерживай его структуру ответа (шаги 0–6) и требования к проверкам.
-58
View File
@@ -1,58 +0,0 @@
---
description: "[Multi-agent] Оркестратор — последовательность Analyzer→Implementer→Reviewer→UI Guardian"
alwaysApply: false
---
# Оркестратор пайплайна (multi-agent)
Используй это правило, когда нужно прогнать задачу через **все роли по очереди**. Подключай вместе с **`ma-global.mdc`** и при необходимости **`next-shadcn-production.mdc`**.
## Язык
- На каждой фазе ответы пользователю — **на русском**.
## Порядок фаз (не нарушать)
1. **Analyzer** — правило **`ma-analyzer.mdc`**: только анализ, без реализации.
2. **Implementer** — правило **`ma-implementer.mdc`**: код по результатам фазы 1.
3. **Reviewer** — правило **`ma-reviewer.mdc`**: строгое ревью результата фазы 2.
4. **UI Guardian** — правило **`ma-ui-guardian.mdc`**: только сверка с **`/servers`**.
## Как исполнять в одном чате
- Выполни **фазу 1** целиком и остановись на выходном формате Analyzer.
- Затем **фаза 2** с явной ссылкой: «реализуй согласно отчёту Analyzer выше».
- Затем **фаза 3** по артефакту фазы 2.
- Затем **фаза 4** по UI-части изменений.
Если пользователь просит «только одну фазу» — выполни только её и напомни номер следующей.
## Промпт-шаблон для пользователя (скопировать в чат)
```text
Запусти multi-agent пайплайн для задачи ниже.
Фазы строго по порядку: Analyzer → Implementer → Reviewer → UI Guardian.
Язык ответов — русский. Стек: Next.js 16.2.4 App Router + Turbopack, shadcn/ui.
Эталон UI: app/(main)/servers.
Следуй правилам ma-global и соответствующим ma-* ролям.
Задача:
<опишите фичу или баг>
```
## Файлы ролей в `.cursor/rules/`
| Роль | Файл |
|------------|-------------------------|
| Глобальные | `ma-global.mdc` |
| Analyzer | `ma-analyzer.mdc` |
| Implementer| `ma-implementer.mdc` |
| Reviewer | `ma-reviewer.mdc` |
| UI Guardian| `ma-ui-guardian.mdc` |
| Оркестратор| `ma-orchestrator.mdc` |
## Ожидаемый результат пайплайна
- Согласованность с кодовой базой и **максимальный reuse**.
- UI, выровненный под **`/servers`**.
- Отсутствие известных блокеров по Next.js, гидратации, TS и заметных anti-patterns после фазы Reviewer.
-42
View File
@@ -1,42 +0,0 @@
---
description: "[Multi-agent] Роль REVIEWER — строгая проверка Next/shadcn/a11y/гидратации/Turbopack"
alwaysApply: false
---
# Агент: REVIEWER (ревьюер)
Ты **валидируешь** предложенные или уже внесённые изменения. Режим: **строгий**.
## Язык
- Все ответы пользователю — **на русском**.
## Что проверять
- **Next.js:** App Router, границы RSC/Client, данные ближе к серверу, кэширование/revalidate где уместно, корректные route handlers и server actions.
- **shadcn/ui:** композиция, токены/классы проекта, отсутствие «левого» самописного UI там, где хватает shadcn.
- **Производительность:** лишние клиентские деревья, waterfall fetch, чрезмерные `'use client'` границы.
- **a11y:** семантика, фокус, aria, клавиатура (в разумных пределах контекста правки).
- **Гидратация:** несовпадение разметки/случайные значения на сервере и клиенте, неправильное использование браузерных API в RSC.
- **Turbopack:** не предлагать паттерны, ломающие dev-сборку Next 16 + Turbopack; учитывать типичные предупреждения toolchain.
## Обязательные находки
- Ошибочное **смешение Server/Client** (хуки в RSC, серверные модули в клиенте без обёртки и т.д.).
- **Over-fetching** и дублирующая логика запросов.
- **Дублирование** логики/UI там, где уже есть общий модуль или паттерн `/servers`.
## Формат вывода (строго)
Таблица или списки:
1. **Проблема** (кратко).
2. **Серьёзность:** блокер / высокая / средняя / низкая.
3. **Рекомендация по исправлению** (конкретные шаги или патч-уровень).
Если дефектов нет — явно напиши **«Блокирующих замечаний нет»** и перечисли **что именно** было проверено.
## Запреты
- Нельзя «мягко пропускать» очевидные проблемы ради скорости.
- Нельзя ограничиваться общими фразами — каждая **рекомендация должна быть actionable**.
-43
View File
@@ -1,43 +0,0 @@
---
description: "[Multi-agent] Роль UI GUARDIAN — сверка UI с CRUD /servers и оболочкой Frame+DataGrid"
alwaysApply: false
---
# Агент: UI GUARDIAN (хранитель UI)
Ты отвечаешь **только** за **визуальную и UX-согласованность**.
## Язык
- Все ответы пользователю — **на русском**.
## Эталон
Два слоя (не смешивать):
1. **Поведение CRUD** — каталог **`app/(main)/servers`**: фильтры, таблица, диалоги, формы, плотность, паттерны кнопок.
2. **Оболочка ops** — **ReUI Frame + DataGrid**, не shadcn `Card` как page shell. KPI — `IconTile` `variant="elevated"` `className="size-10.5"`. Preview: [frame](https://reui.io/docs/components/base/frame) · [data-grid](https://reui.io/docs/components/base/data-grid) · [stats-12](https://reui.io/preview/base/stats-12) · [icon-tile](https://reui.io/docs/components/base/icon-tile).
`DataPageCard` — ops list shell на ReUI Frame; новые экраны не копируют Card-shell.
## Обязанности
- Сравнить **каждое** спорное UI-решение: CRUD — с `/servers`; оболочка — с Frame+DataGrid.
- Требовать **переиспользование** существующих компонентов/обёрток, если они покрывают задачу.
## Правила
- **Нельзя** принимать новый Card page-shell без явной технической необходимости (назвать её).
- **Нельзя** предлагать «новый стиль» ради вариации — только выравнивание с эталоном или расширение существующих примитивов.
## Формат вывода (строго)
1. **Несоответствия** эталону (конкретно: CRUD vs Frame-shell).
2. **Обязательные правки** (что поменять).
3. **Что переиспользовать**: файлы/компоненты и паттерн.
Если расхождений нет — явно: **«Расхождений с эталоном CRUD /servers и Frame+DataGrid не выявлено»** и кратко перечисли проверенные аспекты.
## Граница ответственности
- Не дублируй полный код-ревью Implementer/Reviewer: фокус только на **консистентности** и **reuse** UI.
-84
View File
@@ -1,84 +0,0 @@
---
description: Next.js App Router + shadcn/ui — эталон /servers, поиск по репозиторию, формат ответа и проверки
alwaysApply: true
---
# Next.js 16 (App Router, Turbopack) + shadcn/ui — правила проекта
Стек ориентира: **Next.js 16.2.4** (App Router, Turbopack), **React**, **shadcn/ui**. При расхождении с документацией — верифицировать по официальным источникам для вашей версии.
**Язык:** все ответы пользователю — **только на русском**.
**Git / Generate Commit Message:** subject и body — **на русском**; `feat` только для новой UX-фичи, иначе `fix`/`chore`. См. **`commit-messages-ru.mdc`** и **`release-versioning.mdc`**.
## 1. Документация в первую очередь
- Решения сверять с **официальной** документацией Next.js и shadcn/ui (актуальные версии проекта).
- Не выдумывать API и «недокументированные» паттерны; предпочитать стабильные, описанные в доках решения.
## 2. Эталон UI/UX: `/servers` (CRUD) + Frame+DataGrid (оболочка)
- **`app/(main)/servers`** — эталон **поведения** CRUD: фильтры, сортировка, пагинация, диалоги, формы, плотность.
- **Оболочка ops-списков / dashboard / KPI:** **ReUI Frame + DataGrid**, не shadcn `Card` как page shell. Preview: [frame](https://reui.io/docs/components/base/frame) · [data-grid](https://reui.io/docs/components/base/data-grid) · [stats-12](https://reui.io/preview/base/stats-12) · [icon-tile](https://reui.io/docs/components/base/icon-tile).
- `DataPageCard` — ops list shell на ReUI Frame (`components/data-page-card.tsx`). Внутренние панели — `OpsPanel` / Frame, не shadcn Card как page shell.
- Переиспользовать паттерны `/servers` для CRUD; **не** вводить Card-grid как новый SoT.
## 3. Большой репозиторий (критично)
- **Перед изменениями** искать по проекту: похожие компоненты, существующие паттерны, общие утилиты, хуки, UI-абстракции.
- **Не дублировать:** если похожее решение уже есть — расширять или переиспользовать, а не писать с нуля.
## 4. Согласованность между файлами
При правке фичи проверять и при необходимости обновлять **все** связанное: компоненты, хуки, route handlers / server actions, типы, стили/обёртки UI — не только открытый файл.
## 5. Зависимости и влияние
Перед реализацией: от чего зависит целевой файл, где используется, какие побочные эффекты; избегать ломающих изменений без явной необходимости.
## 6. Нативные возможности фреймворка
- Next.js: App Router, Server Components, Server Actions, Route Handlers — по умолчанию предпочтительнее кастомных обходных путей.
- UI: компоненты **shadcn/ui**; не плодить самописное, если стандартное решение покрывает задачу.
## 7. Практики качества
- Рекомендации Next.js: загрузка данных, рендеринг, производительность.
- Минимизировать `'use client'` там, где достаточно серверных компонентов.
- Композиция shadcn/ui и доступность (a11y).
## 8. Валидация каждого изменения (обязательно)
В ответе явно указать:
- **Почему** это согласуется с Next.js, shadcn/ui, CRUD `/servers` и оболочкой Frame+DataGrid.
- **Server vs Client Component** и обоснование.
- Что **переиспользовано** из проекта (паттерны/компоненты).
- Какие **файлы проанализированы**.
## 9. Turbopack и консоль разработки
После правок: проверить сборку/типы (по возможности запустить проверки проекта), ошибки гидратации, предупреждения; критичное исправить; не игнорировать предупреждения о плохих практиках.
## 10. Правки кода
- Минимальные, но **полные** (все зависимые места обновлены).
- Не рефакторить несвязанную логику.
## 11. Формат ответа (обязательная структура)
1. **Шаг 0:** список проанализированных файлов.
2. **Шаг 1:** что изменено.
3. **Шаг 2:** почему (Next.js + shadcn/ui + CRUD `/servers` + Frame+DataGrid + паттерны кодовой базы).
4. **Шаг 3:** анализ влияния (что ещё затронуто).
5. **Шаг 4:** код (диффы или несколько файлов).
6. **Шаг 5:** проверка dev/консоли (ошибки, предупреждения, исправления).
7. **Шаг 6:** опциональные улучшения.
## 12. Дублирование и антипаттерны
Явно указывать, если найдено дублирование или антипаттерн; ссылаться на существующую реализацию в проекте; предлагать переиспользование или рефакторинг вместо копипаста.
## Цель
Вести себя как **senior** в production-кодовой базе: консистентность, переиспользование, CRUD как на `/servers`, оболочка Frame+DataGrid (не Card-shell), безопасные масштабируемые изменения по best practices Next.js и shadcn/ui, готовый к продакшену код без ошибок сборки/рантайма по возможности.
-49
View File
@@ -1,49 +0,0 @@
---
description: "Conventional Commits, semver релизов MikrotikManager и русские git commit message (Generate Commit Message)"
alwaysApply: true
---
# Версионирование релизов
## Generate Commit Message (критично)
Для **Generate Commit Message** в Cursor, предложения subject в чате, `git commit` и любой автогенерации сообщения коммита:
- Формат: `тип(scope): описание на русском` — префикс Conventional Commits и scope **на английском**, текст после `:` **только на русском**.
- Body и footer (если нужны) — **только на русском**.
- В описании после `:` не использовать английские слова: `add`, `update`, `implement`, `rename` и т.п.
- **`feat` не по умолчанию.** Сначала проверь `fix` / `chore` / `ci` / `docs`; `feat` — только новая пользовательская возможность.
- Примеры: `fix(ui): выровнять модалку релиза`; `chore(rules): усилить правила commit message`; `feat(releases): добавить страницу релизов`.
- Детальный чеклист типов — **`commit-messages-ru.mdc`**.
- Версию **не** править вручную в UI, `package.json` и git-тегах; bump делает CI через [`.ci/scripts/compute-release.mjs`](.ci/scripts/compute-release.mjs).
- Базовая линия: `v1.0.0`. Формат отображения: `v1.2.3`.
- **Язык коммитов:** subject и body — **только на русском**; префикс Conventional Commits (`feat`, `fix`, `chore` и т.д.) и scope — на английском.
## Patch (`1.0.x`)
`fix:`, `chore:`, `docs:`, `refactor:`, `style:`, `test:`, `build:`, `ci:`
- Один логический смысл на коммит; subject в повелительном наклонении, до ~72 символов, **на русском**.
- Scope по желанию: `fix(servers):`, `chore(ci):`.
## Minor (`1.x.0`)
`feat:` — новая пользовательская возможность.
## Major (`2.0.0` после `1.x`)
`тип!:`, `тип(scope)!:` или footer `BREAKING CHANGE:` / `BREAKING-CHANGE:` повышают major, обнуляя minor и patch. При смешанной истории major имеет приоритет над minor и patch. Версия, тег, manifest и Docker tags вычисляются CI; package.json вручную не меняется. Для согласованного перехода на вторую основную версию используется `chore(release)!:` с русским описанием.
## Примеры
- `fix(servers): исправить опрос API после таймаута` — patch
- `feat(filters): добавить синхронизацию BGP in` — minor
- `chore(ci): добавить job prepare-release` — хорошо
- `feat: add release modal` — плохо (subject не на русском)
## Антипаттерны
WIP, «update», subject на английском, смешение несвязанных изменений, маскировка фич под `fix`/`chore`, ручной bump в sidebar.
Подробности — раздел CI/CD в `README.md`.
-73
View File
@@ -1,73 +0,0 @@
---
description: Use the ReUI registry (blocks, primitives, icons) correctly
globs: ["**/*.tsx","**/*.ts"]
alwaysApply: false
---
---
name: reui
description: Use the ReUI registry from your AI agent - find, install, and correctly use ReUI components (the 22 free building blocks like data-grid, kanban, filters), their free examples, premium blocks, and Motion Icons. Applies in any project using ReUI, the @reui registry, REUI_LICENSE_KEY, or any shadcn project where the user asks for premium blocks, data grids, kanban boards, dashboards, or full pages. Pairs with the free ReUI MCP server for live, scored registry search and inline component APIs.
user-invocable: false
allowed-tools: Bash(npx shadcn@latest *), Bash(pnpm dlx shadcn@latest *), Bash(bunx --bun shadcn@latest *)
---
> **ReUI skill version `fe6742e29c`.** If the ReUI MCP's `get_agent_skill` reports a newer `version`, re-run the ReUI installer (see `get_agent_skill` -> `install.recommended`) to update this skill. Cloud/tools-only agents have no local file and always read the latest - they can ignore this.
# ReUI for Agents
ReUI is a shadcn-compatible registry. It ships four things you **reuse** - never redesign:
- **components** - the 22 ReUI building blocks with real APIs: `data-grid`, `kanban`, `filters`, `date-selector`, `tree`, `stepper`, ... (free)
- **examples** - free `c-*` single-pattern use-cases of a component (`c-kanban-1`); install one and read it to see exact composition
- **blocks** - premium full-page sections that compose components (`data-grid-2`, `pricing-page-1`); Pro or Ultimate license at install
- **icons** - Motion Icons in 4 styles, static + hover-animated variants; Ultimate license at install
The skill is free and this MCP is free to use; it just needs a ReUI account. On first use your agent opens a browser "Sign in with ReUI" prompt (a free account is created if you don't have one). Free covers components and examples with a daily request allowance; a Pro or Ultimate license unlocks premium blocks and Motion Icons and removes the limit (see [rules/registry.md](./rules/registry.md)). The same account and skill work in every agent and service the MCP connects to - this skill is agent-agnostic.
Skill + MCP are a team: this skill is the workflow (how to find, install, read the API, and adapt by reuse); the MCP is the live data and the hands (search, get_component, install commands). Your job: find the right item, install it with the shadcn CLI, read its real API, and **adapt by reuse** - wire real data and theme it; do not hand-roll or restyle what ReUI already provides. This skill **layers on the shadcn skill**: follow that for generic rules (spacing, `cn()`, semantic colors, forms); follow this for everything ReUI-specific.
## The core loop (MCP-native)
1. **Find** - call the ReUI MCP `search` tool with the user's intent. It returns a ranked, scored list across components/examples/blocks/icons, each with an `install` command, `previewUrl`, `docsUrl`, and `componentsUsed`. Pass hints (`type`, `component`, `category`, `features`, `free`) when you can infer them.
2. **Install** - run the returned command non-interactively (`npx shadcn@latest add @reui/<name> --yes`). The CLI resolves deps, aliases, and the base/style from `components.json`. See [cli.md](./rules/cli.md).
3. **Read the API (on your base)** - first note your base from `components.json` -> `style` (`base-nova` -> Base UI, `radix-nova` -> Radix UI). For each component an item uses, call `get_component(name)` and read its **inline `api`** (no web fetch); then `get_examples(name)` to install a worked example and copy its composition - the installed files are already in your base. Whenever you work with a component's API, also **share its `docsUrl`** (the primitive's API documentation page) with the user so they have the full reference. See [components.md](./rules/components.md).
4. **Adapt (reuse-first)** - swap demo data for real data, fix icon imports, align tokens. Do not redesign. See [adapting.md](./rules/adapting.md).
**Always show the preview.** Every item a tool returns carries a `previewUrl` (a live preview page). Whenever you list, recommend, or present ReUI items to the user - blocks, components, examples, or icons, whether from `search`, `search_icons`, `list_components`, `compose_page`, or any getter - include each item's `previewUrl` so they can SEE it before installing. Blocks and examples open an individual live preview; icons and components link to their live category/component page. Never present an item without its preview link.
If the ReUI MCP is not configured, fall back to `npx shadcn@latest search @reui -q "..."` then `add` - but the MCP gives scored matches + inline APIs; prefer it.
## Commands
Run ReUI as explicit slash commands (via the ReUI MCP) **or** just ask in plain language - both run the same workflow.
| Command | Invoke | Does |
| ----------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------ |
| **build** | `/mcp__reui__build <what>` | Compose a page/section/feature from ReUI: plan → install → read API → adapt → craft → audit. |
| **add** | `/mcp__reui__add <item>` | Find & install one component/example/block/icon and wire it in. |
| **fix** | `/mcp__reui__fix [target]` | Diagnose & fix ReUI usage: wrong/undocumented props, base/radix mismatch, missing states, a11y/scroll. |
| **improve** | `/mcp__reui__improve [target]` | Refine + extend existing ReUI UI to a production-exceptional bar (hierarchy, density, states, responsive, motion). |
Invocation differs slightly per agent (`/mcp__reui__build` in Claude Code/Cursor/Windsurf, `/mcp.reui.build` in VS Code). No command surface? Just describe what you want - this skill drives the identical loop.
## When to reach for ReUI vs plain shadcn
| Need | Reach for |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| A full page or section (dashboard, billing, auth, pricing, settings) | `compose_page` first (plans sections + best blocks), then ReUI **blocks** |
| A data table with sorting/filtering/pagination/virtualization | the **data-grid** component (never hand-roll a `<table>`) |
| A drag-and-drop board | the **kanban** component |
| Advanced column filtering, date range, tree, stepper, ... | the matching ReUI **component** |
| A single generic control already in shadcn (Button, Dialog, Select) | plain **shadcn** |
## Detailed references
- [rules/registry.md](./rules/registry.md) - the four types, the @reui registry, base/radix, free vs premium + license
- [rules/workflow.md](./rules/workflow.md) - the find -> install -> read-API -> adapt loop (most important)
- [rules/components.md](./rules/components.md) - the 22 components, the data-grid contract, base vs radix
- [rules/adapting.md](./rules/adapting.md) - reuse-first: preserve the design (no over-customizing), reuse examples + a block's own elements, real data, don't invent APIs
- [rules/craft.md](./rules/craft.md) - make it exceptional: point of view, hierarchy, density, states, responsive, motion, the bar
- [rules/quality.md](./rules/quality.md) - security, accessibility, and scroll gates (the done gate)
- [rules/styling.md](./rules/styling.md) - ReUI extended tokens, theme adaptation, density
- [rules/icons.md](./rules/icons.md) - portable icons, swapping imports, Motion Icons (static + animated)
- [tools.md](./tools.md) - the ReUI MCP: golden path, the 19 tools, token rules, result shapes, errors
-67
View File
@@ -1,67 +0,0 @@
---
name: reui
description: Use the ReUI registry from your AI agent - find, install, and correctly use ReUI components (the 22 free building blocks like data-grid, kanban, filters), their free examples, premium blocks, and Motion Icons. Applies in any project using ReUI, the @reui registry, REUI_LICENSE_KEY, or any shadcn project where the user asks for premium blocks, data grids, kanban boards, dashboards, or full pages. Pairs with the free ReUI MCP server for live, scored registry search and inline component APIs.
user-invocable: false
allowed-tools: Bash(npx shadcn@latest *), Bash(pnpm dlx shadcn@latest *), Bash(bunx --bun shadcn@latest *)
---
> **ReUI skill version `fe6742e29c`.** If the ReUI MCP's `get_agent_skill` reports a newer `version`, re-run the ReUI installer (see `get_agent_skill` -> `install.recommended`) to update this skill. Cloud/tools-only agents have no local file and always read the latest - they can ignore this.
# ReUI for Agents
ReUI is a shadcn-compatible registry. It ships four things you **reuse** - never redesign:
- **components** - the 22 ReUI building blocks with real APIs: `data-grid`, `kanban`, `filters`, `date-selector`, `tree`, `stepper`, ... (free)
- **examples** - free `c-*` single-pattern use-cases of a component (`c-kanban-1`); install one and read it to see exact composition
- **blocks** - premium full-page sections that compose components (`data-grid-2`, `pricing-page-1`); Pro or Ultimate license at install
- **icons** - Motion Icons in 4 styles, static + hover-animated variants; Ultimate license at install
The skill is free and this MCP is free to use; it just needs a ReUI account. On first use your agent opens a browser "Sign in with ReUI" prompt (a free account is created if you don't have one). Free covers components and examples with a daily request allowance; a Pro or Ultimate license unlocks premium blocks and Motion Icons and removes the limit (see [rules/registry.md](./rules/registry.md)). The same account and skill work in every agent and service the MCP connects to - this skill is agent-agnostic.
Skill + MCP are a team: this skill is the workflow (how to find, install, read the API, and adapt by reuse); the MCP is the live data and the hands (search, get_component, install commands). Your job: find the right item, install it with the shadcn CLI, read its real API, and **adapt by reuse** - wire real data and theme it; do not hand-roll or restyle what ReUI already provides. This skill **layers on the shadcn skill**: follow that for generic rules (spacing, `cn()`, semantic colors, forms); follow this for everything ReUI-specific.
## The core loop (MCP-native)
1. **Find** - call the ReUI MCP `search` tool with the user's intent. It returns a ranked, scored list across components/examples/blocks/icons, each with an `install` command, `previewUrl`, `docsUrl`, and `componentsUsed`. Pass hints (`type`, `component`, `category`, `features`, `free`) when you can infer them.
2. **Install** - run the returned command non-interactively (`npx shadcn@latest add @reui/<name> --yes`). The CLI resolves deps, aliases, and the base/style from `components.json`. See [cli.md](./rules/cli.md).
3. **Read the API (on your base)** - first note your base from `components.json` -> `style` (`base-nova` -> Base UI, `radix-nova` -> Radix UI). For each component an item uses, call `get_component(name)` and read its **inline `api`** (no web fetch); then `get_examples(name)` to install a worked example and copy its composition - the installed files are already in your base. Whenever you work with a component's API, also **share its `docsUrl`** (the primitive's API documentation page) with the user so they have the full reference. See [components.md](./rules/components.md).
4. **Adapt (reuse-first)** - swap demo data for real data, fix icon imports, align tokens. Do not redesign. See [adapting.md](./rules/adapting.md).
**Always show the preview.** Every item a tool returns carries a `previewUrl` (a live preview page). Whenever you list, recommend, or present ReUI items to the user - blocks, components, examples, or icons, whether from `search`, `search_icons`, `list_components`, `compose_page`, or any getter - include each item's `previewUrl` so they can SEE it before installing. Blocks and examples open an individual live preview; icons and components link to their live category/component page. Never present an item without its preview link.
If the ReUI MCP is not configured, fall back to `npx shadcn@latest search @reui -q "..."` then `add` - but the MCP gives scored matches + inline APIs; prefer it.
## Commands
Run ReUI as explicit slash commands (via the ReUI MCP) **or** just ask in plain language - both run the same workflow.
| Command | Invoke | Does |
| ----------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------ |
| **build** | `/mcp__reui__build <what>` | Compose a page/section/feature from ReUI: plan → install → read API → adapt → craft → audit. |
| **add** | `/mcp__reui__add <item>` | Find & install one component/example/block/icon and wire it in. |
| **fix** | `/mcp__reui__fix [target]` | Diagnose & fix ReUI usage: wrong/undocumented props, base/radix mismatch, missing states, a11y/scroll. |
| **improve** | `/mcp__reui__improve [target]` | Refine + extend existing ReUI UI to a production-exceptional bar (hierarchy, density, states, responsive, motion). |
Invocation differs slightly per agent (`/mcp__reui__build` in Claude Code/Cursor/Windsurf, `/mcp.reui.build` in VS Code). No command surface? Just describe what you want - this skill drives the identical loop.
## When to reach for ReUI vs plain shadcn
| Need | Reach for |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| A full page or section (dashboard, billing, auth, pricing, settings) | `compose_page` first (plans sections + best blocks), then ReUI **blocks** |
| A data table with sorting/filtering/pagination/virtualization | the **data-grid** component (never hand-roll a `<table>`) |
| A drag-and-drop board | the **kanban** component |
| Advanced column filtering, date range, tree, stepper, ... | the matching ReUI **component** |
| A single generic control already in shadcn (Button, Dialog, Select) | plain **shadcn** |
## Detailed references
- [rules/registry.md](./rules/registry.md) - the four types, the @reui registry, base/radix, free vs premium + license
- [rules/workflow.md](./rules/workflow.md) - the find -> install -> read-API -> adapt loop (most important)
- [rules/components.md](./rules/components.md) - the 22 components, the data-grid contract, base vs radix
- [rules/adapting.md](./rules/adapting.md) - reuse-first: preserve the design (no over-customizing), reuse examples + a block's own elements, real data, don't invent APIs
- [rules/craft.md](./rules/craft.md) - make it exceptional: point of view, hierarchy, density, states, responsive, motion, the bar
- [rules/quality.md](./rules/quality.md) - security, accessibility, and scroll gates (the done gate)
- [rules/styling.md](./rules/styling.md) - ReUI extended tokens, theme adaptation, density
- [rules/icons.md](./rules/icons.md) - portable icons, swapping imports, Motion Icons (static + animated)
- [tools.md](./tools.md) - the ReUI MCP: golden path, the 19 tools, token rules, result shapes, errors
-43
View File
@@ -1,43 +0,0 @@
# Adapting installed ReUI code (reuse-first, no AI slop)
ReUI items ship production-quality. Your job is to **adapt by reuse** - wire real data and fit the app - not to redesign or hand-roll. The output should look like ReUI built it for this product.
## Preserve the design - don't over-customize
The design IS the product. A ReUI block/component encodes senior-designer decisions: spacing, hierarchy, density, color treatment, and component choices. The fastest way to turn a premium block back into generic AI slop is to "improve" its look - so don't.
- Change **data, copy, and props**; keep the **structure and styling** it ships with. Make the **smallest** change that wires the real data. If your diff touches `className` / JSX structure more than data / props, you are over-customizing - stop and reuse.
- Don't swap ReUI components for hand-rolled ones, restructure the layout, re-skin spacing / radius / colors, or add decorative chrome. Let the installed components carry the default spacing, radius, sizing, icon rhythm, density, and state styling; add custom Tailwind only when a component genuinely lacks a contract you need.
- Want a different look? `search` for a block whose design already fits and reuse that - don't restyle this one into a new design.
## Reuse the parts: examples and the block's own elements
- **Examples are building parts.** A free `c-*` example is a correct, single-pattern composition you can reuse. Before composing from scratch, `get_examples(component)`, install the closest one, and reuse its wiring - assemble UI from examples instead of hand-rolling what an example already shows.
- **Reuse a block's own elements.** Need more rows, cards, items, or sections than ship by default? Repeat the block's **existing** element by mapping real data through the same markup - never invent parallel markup that drifts from its design. Need a variant (empty / loading / expanded)? Derive it from an element the block already has.
## Don't invent (read, don't guess)
- Never write a prop, variant value, import path, or `@reui/...` name you didn't read in a component's inline `api`, an installed example, or a `search` result. If you didn't see it, treat it as nonexistent - call `get_component` / `get_examples` / `search` first, or run the MCP `validate_usage` tool to check planned names + props against the docs before writing code.
- If a getter returns `found: false` or `search` returns nothing, say so and fall back (plain shadcn, or ask) - never fabricate an install command or an API.
## What to change vs leave alone
- **Change:** the item's own data, copy, props, and layout to fit the app.
- **Leave alone:** installed component files, hooks, and the shared theme - do not edit vendored ReUI internals; change behavior through props and the documented API.
- Blocks are **portable React** - no `next/link`, `next/image`, or other framework-runtime imports inside them. Keep them portable.
## Demo data -> real data
- Replace every placeholder with the user's real data. Model it as **typed data structures** and **map over arrays** - never duplicate JSX per row/card. Keep small block-specific formatters next to the data.
- Wire the real source (columns, fields, fetch). For `data-grid`, implement the server fetch contract if the user needs server-side data.
- **Type from the component API, derive during render.** Type domain state through the component's own types - e.g. map status to `BadgeProps["variant"]` via a typed `Record<Status, …>` - instead of stringly-typed values. Compute view state during render; don't mirror derived data into `useState`/`useEffect`.
- **Adapt on the right base.** Use the API for the project's base (Base UI vs Radix - see [components.md](./components.md)); the installed files are already base-correct, so reuse their shape rather than translating from memory.
## Believable content (no AI tells)
- Use realistic labels, counts, timestamps, and statuses that map to a real workflow.
- No decorative buttons, fake tabs, meaningless toggles, equal-weight card walls, empty gradients, ornamental icons, or generic SaaS filler. Every element should do something.
## Operational surfaces (settings / profile / admin)
Pick ONE archetype and keep the family consistent: a vertical rail (3-6 sections), horizontal tabs (5-8), or a frame/stack. Prefer `frame` for tool-like surfaces, a card for profile-like ones. Don't mix archetypes in one surface.
-60
View File
@@ -1,60 +0,0 @@
# CLI: registry setup, license, non-interactive install
## Registry setup (one-time, per project)
Free items (the 22 components and all `c-*` examples) need only the plain string registry in `components.json`:
```json
{ "registries": { "@reui": "https://reui.io/r/{style}/{name}.json" } }
```
Premium items (blocks; Motion Icons and templates) require a ReUI license at install:
1. Add the key to `.env.local`:
```bash
REUI_LICENSE_KEY=your-license-key
```
2. Switch `components.json` to the authenticated object form:
```json
{
"registries": {
"@reui": {
"url": "https://reui.io/r/{style}/{name}.json",
"headers": { "Authorization": "Bearer ${REUI_LICENSE_KEY}" }
}
}
}
```
The shadcn CLI expands `${REUI_LICENSE_KEY}` from `.env.local` inside `components.json`, but an MCP client config never expands variables, so a ReUI MCP server config must carry the raw token instead (for example `reui_pat_your_token_here`).
The MCP `get_project_context` tool returns the right config. Full guide: https://reui.io/docs/registry?ref=skill
## Installing
Use the project's package runner (check `packageManager`):
```bash
npx shadcn@latest add @reui/<name> --yes # npm
pnpm dlx shadcn@latest add @reui/<name> --yes # pnpm
bunx --bun shadcn@latest add @reui/<name> --yes # bun
```
`--yes` skips confirmation prompts. The CLI auto-detects the package manager from the lockfile (there is no `--package-manager` flag). It also resolves the correct base+style variant from `components.json`, so do not pass a style.
## Handling prompts and conflicts
- **Always pass `--yes`** so the CLI does not block on confirmation prompts.
- **Do NOT pass `--overwrite` by default.** If the CLI reports an existing file, read the output and resolve deliberately: install under a different name, adjust the path, or ask the user. Only use `--overwrite` when the user explicitly wants to replace a file.
- **Preview first when touching an existing project**: `npx shadcn@latest add @reui/<name> --dry-run` shows what would change; `--diff <file>` shows a specific file's diff. Use these before overwriting.
- Run from the **project root** so `components.json` and `.env.local` are found.
## Free vs premium boundary
- Public, no key: `c-*` examples and the 22 components (`@reui/data-grid`, `@reui/badge`, ...) that those examples depend on.
- Key required at install: blocks (`@reui/<category>-N`) need a Pro or Ultimate license; Motion Icons (`@reui/icons/...`) and templates need Ultimate.
If an install 401/403s, the license key is missing, invalid, or the plan does not cover that resource (blocks: Pro or higher; icons and templates: Ultimate). Point the user to https://reui.io/account?ref=skill (their key) or https://reui.io/pricing?ref=skill (upgrade).
-428
View File
@@ -1,428 +0,0 @@
# ReUI components
The 22 ReUI building blocks: `alert`, `autocomplete`, `badge`, `cascader`, `code-block`, `data-grid`, `date-selector`, `event-calendar`, `filters`, `frame`, `gantt`, `icon-stack`, `icon-tile`, `kanban`, `number-field`, `phone-input`, `rating`, `scrollspy`, `sortable`, `stepper`, `timeline`, `tree`. Examples and blocks are composed from these.
**Rule one: never guess a component's API. Read it first.** Call **`get_component(name)`** for its inline `api` (props + usage, no web fetch), and **share the result's `docsUrl`** (the component's API documentation page) with the user whenever you work with that component's API, so they have the full reference (the `/llms.txt` index is a further fallback). Then call **`get_examples(name)`** to install a worked example and copy real composition. The contracts below are first-try orientation (required props, composition shape, the one gotcha); the inline `api` is the full reference. No single block fits? Compose: search the components you need, read each `get_component`, install a `get_examples` example per component, and adapt.
## data-grid (the flagship - read its API every time)
`data-grid` wraps TanStack Table v9. It is NOT a styled `<table>` and does NOT take `data`/`columns` props directly. The contract:
- Build a TanStack table instance with `useTable({ features: dataGridFeatures, ... })` (columns, data). `dataGridFeatures` is exported by the primitive and already bundles sorting, filtering, pagination, row selection, expanding, pinning, resizing and faceting, so there are no per-table row models to wire.
- Pass that instance to `<DataGrid table={table} recordCount={total}>`.
- Compose the body with `DataGridTable` inside `DataGrid`, and enable features through `tableLayout` (e.g. `{ headerSticky: true, columnsResizable: true }`), not ad-hoc classes.
- Server-side data uses the documented fetch shape (`recordCount` is the total for pagination).
```tsx
const table = useTable({
features: dataGridFeatures,
data,
columns,
})
<DataGrid table={table} recordCount={data.length}>
<DataGridTable />
</DataGrid>
```
Common mistakes:
- **Incorrect:** `<DataGrid data={rows} columns={cols} />` - these props do not exist. **Correct:** build a `useTable({ features: dataGridFeatures, ... })` instance and pass `table={table}` + `recordCount`.
- **Incorrect:** a raw `<table>` / hand-rolled pagination. **Correct:** use `data-grid`; read its API for sticky header, pagination, virtualization, row selection.
- **Incorrect:** styling rows/cells with arbitrary classes. **Correct:** drive layout via `tableLayout` and the primitive's `DataGridColumnMeta` (e.g. `cellClassName`, `headerTitle`), set through the bundle's `columnMeta` slot.
## event-calendar
**Required:** events via `events`/`onEventsChange` (controlled) or `defaultEvents` (uncontrolled), plus a height on the root.
**Shape:**
```tsx
<EventCalendar defaultEvents={events} defaultView="month" className="h-[560px]">
<EventCalendarNav />
<EventCalendarContent />
</EventCalendar>
```
**Gotcha:** headless-first: `EventCalendarContent` renders the active view (month/week/day/days/agenda; a resource view activates when `resources` is passed) - there is no per-view JSX to compose. Events are `{ id, title, start, end (exclusive), allDay?, color?, recurrence?, resourceId? }`. Mutations flow through `onEventUpdate`/`canDropEvent` (return `false` to reject); the root needs an explicit height because it is a min-h-0 flex column.
## gantt
**Required:** `resources` (the left tree) plus bars via `events`/`defaultEvents` attached by `resourceId`.
**Shape:**
```tsx
<Gantt defaultEvents={bars} resources={tasks} defaultScale="month" className="h-[480px]">
<GanttNav />
<GanttView />
</Gantt>
```
**Gotcha:** bars move along the time axis only (never across rows) and are all-day spans with exclusive `end`; `progress` is 0-100. Scales are `day | week | month | quarter | year`. Zoom control, infinite scroll, summary rollups, and row checkboxes are ON by default - turn off what you do not need. Same `onEventUpdate`/`canDropEvent` commit pipeline as `event-calendar`; the root needs an explicit height.
## kanban
**Required:** `value` (`Record<string, T[]>`), `onValueChange`, `getItemValue`
**Shape:**
```tsx
<Kanban value={cols} onValueChange={setCols} getItemValue={(i) => i.id}>
<KanbanBoard>
{Object.entries(cols).map(([id, items]) => (
<KanbanColumn key={id} value={id}>
<KanbanColumnHandle><h3>{id}</h3></KanbanColumnHandle>
<KanbanColumnContent value={id}>
{items.map((i) => (
<KanbanItem key={i.id} value={i.id}>
<KanbanItemHandle>{i.title}</KanbanItemHandle>
</KanbanItem>
))}
</KanbanColumnContent>
</KanbanColumn>
))}
</KanbanBoard>
<KanbanOverlay><div className="bg-muted size-full rounded-md" /></KanbanOverlay>
</Kanban>
```
**Gotcha:** state is `Record<columnId, T[]>`. Each `KanbanColumnContent value` must match its parent `KanbanColumn value`. Omit `KanbanOverlay` and the drag preview silently breaks.
## sortable
**Required:** `value` (`T[]`), `onValueChange`, `getItemValue`
**Shape:**
```tsx
<Sortable value={items} onValueChange={setItems} getItemValue={(i) => i.id}>
{items.map((i) => (
<SortableItem key={i.id} value={i.id}>
<SortableItemHandle><GripVertical /></SortableItemHandle>
{i.label}
</SortableItem>
))}
</Sortable>
```
**Gotcha:** a flat 1D reorder list (not columns - that is `kanban`). `getItemValue` must return a stable, unique string. Pass `layout="grid"` or `layout="nested"` for non-list layouts.
## filters
**Required:** `fields` (`FilterField[]`). The value is ONE `FilterQuery` tree - `query` + `onQueryChange`, or uncontrolled `defaultQuery`.
**Shape:**
```tsx
const fields: FilterField[] = [
{ id: "title", label: "Title", type: "text" },
{
id: "status",
label: "Status",
type: "select",
options: [
{ value: "active", label: "Active" },
{ value: "archived", label: "Archived" },
],
},
]
const [query, setQuery] = useState<FilterQuery>(() => createFilterQuery())
<Filters fields={fields} query={query} onQueryChange={setQuery} />
```
**Gotcha:** the state is a TREE, not a list of chips. `FilterQuery` is a group of rules joined by `and`/`or` and a group may hold another group, so `(A and B) or C` is expressible; a rule is `{ id, type: "rule", path: ["status"], operator, value }` and `path` is the whole nested attribute path, root first. The pre-rewrite API is GONE: there is no `filters`/`onChange` prop, no `FilterFieldConfig` (fields are `FilterField`, nested through their own `fields`, keyed `id` not `key`), and no `createFilter()` - it minted ids inside a pure function and broke hydration, so ids now come from `createFilterIdFactory(seed)` seeded off `useId`, and `createFilterQuery()` / `createFilterRule()` take one. Read the query back with `flattenFilterConditions` (`{ path, field, operator, values, negated }` per rule, incomplete rules skipped) and walk the tree yourself when the parentheses carry meaning - the primitive compiles nothing, no SQL, no query string.
`variant` picks the chrome over that one query: `"basic"`, the default, is the flat chip row for a toolbar over a table; `"advanced"` is the condition builder, hung off a trigger or rendered in place with `advancedMode="inline"`. Both read and write the same tree, so a saved view built in one opens in the other. Other props worth knowing before you hand-roll them: `size` is two rungs, `"sm" | "default"`, resolved per style (there is no `lg`); `reorderable` turns on drag and Alt+Arrow row moves in the builder; `onBeforeQueryChange` is the ONE veto point for every write (return `false` to refuse, it cannot rewrite); `editors` registers custom value editors a field selects by `editor` name; `labels` / `operatorLabels` own every rendered string; `pathCollapse` + `maxPathSegments` shorten deep attribute paths; `renderChip` / `renderValue` / `renderEmpty` replace rendered parts. On a field, `loadOptions` supplies async options with paging and `resolveValues` renders a chip restored from a saved view whose option was never loaded. Pairs naturally with `data-grid`.
## cascader
**Required:** `items` (a tree of `{ value, label, children? }`), plus the panel parts inside `CascaderContent`.
**Shape:**
```tsx
<Cascader items={items} value={value} onValueChange={setValue}>
<CascaderTrigger render={<Button variant="outline" />}>
<CascaderValue placeholder="Select an attribute" />
</CascaderTrigger>
<CascaderContent className="w-80">
<CascaderPanel>
<CascaderNav>
<CascaderBreadcrumb />
<CascaderInput />
</CascaderNav>
<CascaderEmpty />
<CascaderList maxHeight={288}>
<CascaderItems />
</CascaderList>
<CascaderStatus />
</CascaderPanel>
</CascaderContent>
</Cascader>
```
**Gotcha:** pressing a branch NAVIGATES, it does not select - only leaves are selectable until you pass `selectable="any"` or a predicate, and once a branch is selectable its chevron becomes the only way to open it. `CascaderInput` must stay inside `CascaderContent` (Base UI refills the query from the selection when the input sits outside the popup). Always include `CascaderStatus`: it is the live region announcing level changes, which the visual breadcrumb does not provide to screen readers. Accepts a flat adjacency list via `getParent` as well as nested `children`. `searchScope="deep"` searches every level and annotates results with their path; `multiple` gives checkbox rows; `inline` + a bare `CascaderPanel` embeds it with no popover.
The shape above is `mode="drill"`, the default. `mode="tree"` keeps the same parts (drop `CascaderBreadcrumb`, pass `showBack={false}`, drive expansion with `expanded`/`onExpandedChange`); `mode="columns"` REPLACES `CascaderList` + `CascaderItems` with a single `CascaderColumns`, and has no breadcrumb. Other props worth knowing before you hand-roll them: `cascade` (multi-select only, parent/child selection with indeterminate branches - pair it with `selectable="any"`, since a leaf-only tree can never cascade), `indicator={false}` to drop the single-select check and its gutter (visual only, no-op with `multiple`), `virtualize`/`virtualizeThreshold` plus `CascaderVirtualItems` for long levels, and `getChildren` for async levels with cursor paging, retry on failure and optional `prefetch`. `CascaderFooter` pins commands below the list (`actions` is the quick path) and `CascaderSubmenu` opens one as a side-anchored flyout with the full menu keyboard model. To head a run of rows use `CascaderGroup` wrapping a `CascaderLabel` - a bare label inside a listbox names nothing and is dropped from the accessibility tree - and `CascaderSeparator` for the rule between runs. Every rendered string comes from `labels`, and the panel is RTL-correct under a `DirectionProvider` or `dir="rtl"`.
## date-selector
**Required:** none, but wire `onChange` to capture the value.
**Shape:**
```tsx
const [value, setValue] = useState<DateSelectorValue | undefined>()
<DateSelector value={value} onChange={setValue} label="Due date" />
```
**Gotcha:** the value is a structured `DateSelectorValue` (period / operator / start+end dates), NOT a `Date` - never pass a raw `Date`. Use `allowRange={false}` to lock single-date picking. Read `get_component("date-selector")` for the value shape.
## tree
**Required:** `tree` (a `@headless-tree/core` instance you construct)
**Shape:**
```tsx
<Tree tree={tree}>
{tree.getItems().map((item) => (
<TreeItem key={item.getId()} item={item}>
<TreeItemLabel />
</TreeItem>
))}
</Tree>
```
**Gotcha:** `Tree` is a styled shell - it takes a headless-tree instance via `tree`, NOT `data`/`items` props. Build the instance with `@headless-tree/react`. External API: https://headless-tree.lukasbach.com/
## stepper
**Required:** `StepperItem step` (number), `StepperContent value` (number)
**Shape:**
```tsx
<Stepper defaultValue={1}>
<StepperNav>
<StepperItem step={1}>
<StepperTrigger><StepperIndicator>1</StepperIndicator></StepperTrigger>
<StepperSeparator />
</StepperItem>
<StepperItem step={2}>
<StepperTrigger><StepperIndicator>2</StepperIndicator></StepperTrigger>
</StepperItem>
</StepperNav>
<StepperPanel>
<StepperContent value={1}>Step 1 content</StepperContent>
<StepperContent value={2}>Step 2 content</StepperContent>
</StepperPanel>
</Stepper>
```
**Gotcha:** steps are 1-indexed. Without `StepperPanel` + `StepperContent` you render the nav trail but no body. Put `StepperSeparator` in every `StepperItem` except the last.
## timeline
**Required:** `TimelineItem step` (number)
**Shape:**
```tsx
<Timeline>
<TimelineItem step={1}>
<TimelineHeader>
<TimelineDate>March 2024</TimelineDate>
<TimelineTitle>Project initialized</TimelineTitle>
</TimelineHeader>
<TimelineIndicator />
<TimelineSeparator />
<TimelineContent>Repo and architecture set up.</TimelineContent>
</TimelineItem>
</Timeline>
```
**Gotcha:** each item needs a unique `step`. `orientation` is `"vertical"` (default) or `"horizontal"`. This is a static event display, not interactive like `stepper`.
## autocomplete
**Required:** `items` (array; each item has at least `value`)
**Shape:**
```tsx
<Autocomplete items={items}>
<AutocompleteInput placeholder="Search..." />
<AutocompleteContent>
<AutocompleteEmpty>No results found.</AutocompleteEmpty>
<AutocompleteList>
{(item) => (
<AutocompleteItem key={item.value} value={item}>{item.label}</AutocompleteItem>
)}
</AutocompleteList>
</AutocompleteContent>
</Autocomplete>
```
**Gotcha:** `AutocompleteList` takes a render-prop `(item) => ReactNode`, NOT a mapped array of children. External API: https://base-ui.com/react/components/autocomplete
## phone-input
**Required:** none, but wire `onChange`.
**Shape:**
```tsx
<PhoneInput placeholder="Enter phone number" defaultCountry="US" value={value} onChange={setValue} />
```
**Gotcha:** `value`/`onChange` use an E.164 string (e.g. `"+14155551234"`), not a display-formatted string; `onChange` can fire `undefined`. `defaultCountry` is a 2-letter ISO code. Wraps `react-phone-number-input`.
## number-field
**Required:** wrap the controls in `NumberFieldGroup`.
**Shape:**
```tsx
<NumberField defaultValue={0}>
<NumberFieldScrubArea label="Quantity" />
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>
```
**Gotcha:** import from `@/components/ui/number-field`. The accessible label goes on `NumberFieldScrubArea`, not `NumberField`. External API: https://base-ui.com/react/components/number-field
## rating
**Required:** `rating` (number)
**Shape:**
```tsx
<Rating rating={4.5} showValue editable onRatingChange={setRating} />
```
**Gotcha:** supports decimals (partial stars). Pass `editable` + `onRatingChange` for interactive input; omit both for a read-only display.
## scrollspy
**Required:** `targetRef` (the scroll container ref)
**Shape:**
```tsx
<Scrollspy targetRef={containerRef}>
<a href="#s1" data-scrollspy-anchor="s1">Section 1</a>
<a href="#s2" data-scrollspy-anchor="s2">Section 2</a>
</Scrollspy>
<div ref={containerRef}>
<div id="s1">...</div>
<div id="s2">...</div>
</div>
```
**Gotcha:** each link's `data-scrollspy-anchor` must match a section `id`. `targetRef` is the scrollable container (defaults to the window).
## frame
**Required:** `Frame` > `FramePanel`
**Shape:**
```tsx
<Frame>
<FramePanel>
<FrameHeader>
<FrameTitle>Title</FrameTitle>
<FrameDescription>Description</FrameDescription>
</FrameHeader>
<div className="p-5">Content</div>
<FrameFooter>Footer</FrameFooter>
</FramePanel>
</Frame>
```
**Gotcha:** a structured card shell for tool-like surfaces. `stacked` connects multiple panels with shared borders; `dense` removes panel padding; radius via the `--frame-radius` CSS variable.
## icon-stack
**Required:** one child icon
**Shape:**
```tsx
<IconStack aria-hidden="true">
<InboxIcon className="size-4" />
</IconStack>
```
**Gotcha:** isometric layered artwork for empty states and illustrations; style the inner icon via its own `className`. Mark purely decorative stacks `aria-hidden="true"` and keep the real label in surrounding copy.
## icon-tile
**Required:** one child icon
**Shape:**
```tsx
<IconTile variant="elevated" size="lg">
<PackageIcon />
</IconTile>
```
**Gotcha:** the square container an icon sits in, so every list row, feature card and empty state shares one affordance. `variant`: `outline` (default) | `elevated` (muted fill, raised ring) | `soft` (tinted nested, tone from currentColor) | `solid` (filled tone, contrasting glyph) | `frame` (double container). `soft` and `solid` retint from one text color class (they default to `text-primary`). `size`: `xs | sm | default | lg | xl` (24/32/40/48/64px tile, glyph scales 12/14/16/20/24px). `radius`: `default | full`. Do not set a `size-*` class on the child icon unless you mean to override the tile's glyph size; recolor with `className` on the tile, not the icon.
## code-block
**Required:** `code` + `language`, or pre-highlighted `lines`.
**Shape:**
```tsx
<CodeBlock code={code} language="tsx" />
<CodeBlock code={code} language="tsx" showLineNumbers maxLines={20}>
<CodeBlockHeader>
<CodeBlockTitle>use-totals.ts</CodeBlockTitle>
<CodeBlockLanguage />
<CodeBlockCopyButton className="ml-auto" />
</CodeBlockHeader>
<CodeBlockExpandButton />
</CodeBlock>
```
**Gotcha:** the one-liner is already a complete block; every child (header, title, language label, copy button, wrap toggle, expand button, line actions) is optional chrome, at any depth. Shiki is the only npm dependency and loads lazily, one chunk per language, on first highlight; `lines` (from `highlightCode` in `code-block-highlight`, which is server-safe and has no `"use client"`) or `highlight={false}` loads nothing at all. `maxLines` caps the height AND marks the block collapsible, which is what makes `CodeBlockExpandButton` appear. Use `variant="ghost"` when the block sits inside a surface that already has a border.
## alert
**Required:** `Alert` > `AlertTitle`
**Shape:**
```tsx
<Alert variant="success">
<ShieldCheckIcon />
<AlertTitle>Security update</AlertTitle>
<AlertDescription>Enable two-factor authentication.</AlertDescription>
<AlertAction><Button size="xs">Update</Button></AlertAction>
</Alert>
```
**Gotcha:** shadcn-compatible API. `variant`: `default | destructive | info | success | warning | invert`. The non-default variants use ReUI extended color tokens (`--success`/`--info`/`--warning`/`--invert`), which the install adds. Defer generic alert rules to the shadcn skill.
## badge
**Required:** none (text child).
**Shape:**
```tsx
<Badge variant="success-light" size="sm">Success</Badge>
<Badge variant="outline" radius="full">Pill</Badge>
```
**Gotcha:** shadcn-compatible. Rich `variant` set (solid, `-outline`, `-light` per color), `size` `xs..xl`, `radius` `default | full`. Like `alert`, the color variants rely on ReUI extended tokens. Prefer `Badge` variants over raw color classes for statuses.
## base vs radix - write for the project's base
ReUI ships every component in two builds: `base` (Base UI) and `radix` (Radix UI). The install command and name are identical, and the CLI installs the build matching the project. But you must write/adapt code against the **right base**, because their APIs differ.
**Detect the base first.** Read `components.json` -> `style` and take the segment before the first `-`:
- `"style": "base-nova"` -> **Base UI**
- `"style": "radix-nova"` -> **Radix UI**
**Then use that base's API.** The deltas mirror shadcn's base-vs-radix split:
- Slot/composition: Base UI `render={<… />}` vs Radix `asChild`.
- `Select`: Base UI takes `items`; Radix uses `<SelectItem>` children.
- `ToggleGroup`: Base UI `multiple` boolean vs Radix `type="single" | "multiple"`.
The safest path is to **read the installed files and `c-*` examples** - they're already in your base, so reuse their wiring instead of guessing. When `get_component`'s inline `api` or an example shows the other base's shape, translate it to your base (or `validate_usage` to confirm). Defer the generic base/radix mechanics to the shadcn skill.
-45
View File
@@ -1,45 +0,0 @@
# Craft: make ReUI UI exceptional, not generic
ReUI items ship senior-designer quality. Your adaptation has to hold that bar, so the result reads like a real product surface a team would keep - not a wireframe an AI generated. Use these alongside the reuse rules in [adapting.md](./adapting.md).
## Have a point of view
Pick an emotional register before you compose - calm, operational, premium, editorial, dense, energetic - and let layout, spacing, surface treatment, and icon behavior all reinforce it. One or two memorable decisions and restraint everywhere else beats ten generic ones. UI with no point of view reads as generated.
## Brutally clear hierarchy
One focal point per card or panel: the dominant metric or task first, its label second, supporting detail third. The first thing the eye lands on should be the right thing; secondary text must read as secondary. Borders, separators, and surfaces do real work to create 2-3 information bands - don't flatten everything to equal weight.
## Spacing rhythm and deliberate density
Gaps are a signal, not a default. Keep them intentional and consistent within a family (`gap-1`/`gap-2` for tight operational rows, larger gaps for section breaks), and smaller within a group than between groups. Match the surrounding ReUI density; don't pad an operational surface like a marketing page, and don't drift density mid-section. The composition should still feel authored in grayscale.
## Cover the real states (the usual miss)
A surface isn't done at the happy path. Compose, and wire:
- **Empty** - a purposeful empty state (short message + the primary action), never a blank panel.
- **Loading** - a **skeleton** that matches the real layout, not a centered spinner.
- **Error** - an inline, recoverable error with a retry, announced via `role="status"`/`aria-live`.
Derive these from an element the block already has (don't invent parallel markup), or `get_examples` for a state-specific example.
## Responsive by default
Mobile-first, not mobile-afterthought. In constrained rows/cards/sidebars, put `min-w-0` on the shrinking container and `truncate` long single-line labels; protect the primary label's width and let secondary content compress. Reflow layouts (multi-column -> single column) rather than just shrinking them. Desktop and mobile should both look designed.
## Motion, subtly
Motion should clarify, not decorate. Use ReUI Motion Icons on primary actions for a subtle hover cue; keep transitions short (~200-300ms) with calm easing; prefer a skeleton pulse over a spinner. No bouncing, no gratuitous entrance animations on every element.
## Real, activated content
Use believable, typed data (realistic labels, counts, timestamps, statuses that map to a real workflow) - never lorem or abstract filler. Every visible control does something: no decorative buttons, fake tabs, meaningless toggles, or stats with no job. It must still hold with long names, empty values, and crowded data.
## Avoid the AI tells
These instantly read as generated - don't ship them: equal-weight card walls, empty gradients, repetitive padding everywhere, generic enterprise copy, ornamental icons, and number tiles that don't earn their place.
## The bar
Before you finish, ask: **would a product team keep this instead of replacing it? Does it still feel strong after swapping in real content?** If not, reuse the shipped ReUI design harder - don't restyle it into something new - then run the [quality.md](./quality.md) gates.
-39
View File
@@ -1,39 +0,0 @@
# Icons (ReUI delta over shadcn)
Follow the shadcn icon rules (use the project's configured `iconLibrary`, `data-icon` on icons inside `Button`, no sizing classes on icons inside components, pass icons as component objects not string keys). ReUI adds the following.
## Portable icons (library-agnostic)
ReUI components, examples, and blocks are authored to be icon-library-agnostic. When `iconLibrary` is set in `components.json`, the shadcn CLI installs each item's icons in **your** library automatically - you swap nothing. If an installed item's icons don't match your project (for example `iconLibrary` isn't set, so they came in from the item's demo library), change the **import source and component name** to your library, keeping the same icon-name semantics:
- `lucide` -> `lucide-react`
- `tabler` -> `@tabler/icons-react`
- `phosphor` -> `@phosphor-icons/react`
- `remix` -> `@remixicon/react`
- `hugeicons` -> `@hugeicons/react`
Don't assume `lucide-react`; read `iconLibrary` from `components.json`.
## Keep icons purposeful
Icons support the hierarchy, they don't replace it: keep them small, matched to the surrounding density, and decorative ones `aria-hidden="true"` (an icon-only control still needs an accessible label on the control). Don't add ornamental icons that do no job.
## Motion Icons (the `@reui/icons/...` set)
ReUI ships its own icon set in 4 styles (outline, solid, duotone, filled), each icon in two variants:
```bash
npx shadcn@latest add @reui/icons/default/<style>/<name> --yes # static
npx shadcn@latest add @reui/icons/animated/<style>/<name> --yes # hover-animated (motion/react)
```
Finding them via the MCP is free; installing requires an Ultimate license (`REUI_LICENSE_KEY`, see [cli.md](./cli.md)). Reach for a Motion Icon on a primary action when a subtle hover cue helps; keep motion restrained.
Finding icons:
- Several icons (the common case): **`search_icons(concepts[])`** - up to 24 concepts in one call, the best icons per concept with install commands. Pass `animated: true` to get only icons with a hover-animated Motion variant.
- One icon: `search` with `type: "icon"`.
- Icon results and `get_icon` carry `animated: true` and `installAnimated` when an animated variant exists - use those install strings, do not construct paths by hand.
- Every icon result carries a `previewUrl` (its live icon-category page) - **share it with the user** so they can SEE the icon before installing.
The `icon-stack` component composes multiple icons into a stacked display.
-22
View File
@@ -1,22 +0,0 @@
# Quality gates (security, accessibility, scroll)
These are the **done gate**, not a nice-to-have: before you call any ReUI work finished, call the MCP `get_audit_checklist` tool and pass every item below (plus the craft bar in [craft.md](./craft.md)). Then typecheck and lint.
## Security
- Never `dangerouslySetInnerHTML`. Render data as text/components.
- External links (`target="_blank"`) must always pair `rel="noopener noreferrer"`.
- No real PII, secrets, or tokens in demo or committed code. Remote media only from sources the project already allows.
## Accessibility
- Implicit list/card items that navigate get real anchors with a standard hover affordance.
- Icon-only or numeric buttons need an `aria-label`; decorative icons get `aria-hidden`.
- Every non-submit button is `type="button"`.
- Keyboard + focus: everything interactive is reachable in a sensible Tab order with a visible focus ring; layers (dialogs/sheets/menus) trap focus and close on `Escape`. ReUI components ship standard keyboard behavior - read each component's inline `api` rather than re-implementing it.
- Announce async UI: loading and error messages use `role="status"` / `aria-live` so they're not silent to screen readers.
## Scroll mechanics
- Make scroll regions with a parent-owned height: a `min-h-0` + flex chain down to the scroll container. Never guess a `max-h`.
- The scroll container owns `overflow-auto`; ancestors stay `min-h-0` so the height resolves.
-35
View File
@@ -1,35 +0,0 @@
# ReUI registry structure
ReUI is a shadcn-compatible registry with four entity types. **Examples and blocks are built FROM components** - reuse them, don't rebuild.
- **component** - one of the 22 ReUI building blocks with a real API (`data-grid`, `kanban`, `filters`, `date-selector`, `tree`, ...). Install directly (`@reui/data-grid`) or let it come in as a dependency of an example/block. Free. Read its API with `get_component(name)`.
- **example** - a free `c-*` single-pattern use-case of a component (`c-kanban-1`, `c-data-grid-3`). Install one and read it to copy real composition. Find a component's examples with `get_examples(name)`.
- **block** - a premium, full-page section that composes several components (`data-grid-2`, `pricing-page-1`). Pro or Ultimate license at install. Adapts to your active theme via semantic tokens.
- **icon** - Motion Icons in 4 styles (outline, solid, duotone, filled), static (`@reui/icons/default/<style>/<name>`) and hover-animated (`@reui/icons/animated/<style>/<name>`). Ultimate license at install. See [icons.md](./icons.md).
## The @reui registry
Install everything through the shadcn CLI: `npx shadcn@latest add @reui/<name> --yes`. The CLI reads the `@reui` registry from the project's `components.json`. Free items need only the plain string form:
```json
{ "registries": { "@reui": "https://reui.io/r/{style}/{name}.json" } }
```
Premium installs need the authenticated form + `REUI_LICENSE_KEY` in `.env.local` - see [cli.md](./cli.md). The MCP `get_project_context` tool returns the right config.
## Know your base: base or radix
ReUI ships every item in two builds - `base` (Base UI) and `radix` (Radix UI) - with mirrored names. The CLI installs the build matching your project automatically, but **you must write code against the right base's API**. Detect it from `components.json` -> `style`: the segment before the first `-` is the base (`base-nova` -> Base UI, `radix-nova` -> Radix UI). The installed files and `c-*` examples are already in your base - read them and adapt on that base. See [components.md](./components.md) for the API deltas.
**So the MCP's own `docsUrl` and `previewUrl` match your base**, send your `style` to the MCP: add `?style=<your components.json style>` to the ReUI MCP server URL (or set an `X-Reui-Style` header) in your MCP client config - set once, applies to every call. The MCP then resolves docs/preview links to YOUR library (`/docs/components/radix/...`, `/preview/radix/...` for a radix project) instead of the default base; `get_project_context` echoes back the style it currently sees so you can confirm it. Install commands are unaffected (the CLI already installs the right variant). If you notice the MCP returning `base` links for a `radix` project, tell the user to add `?style=` to the server URL.
Blocks adapt to your active theme through semantic tokens and CSS variables - change the theme and every block follows.
## Free vs premium
- **Free, no key:** the 22 components, all `c-*` examples, the ReUI MCP, and this skill.
- **Premium, license required at install:** blocks (Pro or Ultimate), Motion Icons and templates (Ultimate). Set `REUI_LICENSE_KEY` (see [cli.md](./cli.md)).
## Component API index
The canonical index of every component's API docs is **https://reui.io/llms.txt** (returned as `componentsApiUrl` in MCP results). Prefer the inline `api` from `get_component`; use the index/docs as the fallback.
-26
View File
@@ -1,26 +0,0 @@
# Styling (ReUI delta over shadcn)
Follow the shadcn skill for the generic rules (semantic colors not raw values, `gap-*` not `space-y-*`, `size-*`, `cn()`, no manual `dark:` overrides, no overlay `z-index`). This file is only the ReUI-specific additions.
## ReUI extended semantic tokens
ReUI adds semantic tokens beyond shadcn's base set. Use these instead of raw colors for status and emphasis:
- `--success` / `--success-foreground`
- `--info` / `--info-foreground`
- `--warning` / `--warning-foreground`
- `--destructive-foreground` (paired with shadcn's `--destructive`)
- `--invert` / `--invert-foreground` (inverted surfaces)
Use them as Tailwind utilities (`bg-success text-success-foreground`, `text-warning`, ...). They are defined in the project's global CSS and registered with Tailwind (`@theme inline` on v4). If a token is missing in the project, add it to the global CSS file (never a new file) following the same `name` / `name-foreground` convention, exactly as the shadcn customization rules describe.
**Incorrect:** `<span className="text-green-600">Active</span>`
**Correct:** `<Badge variant="success">Active</Badge>` or `<span className="text-success">Active</span>`
## Blocks follow your theme
When you install a block it adapts to your active theme through the semantic tokens above and the project's CSS variables. Don't hardcode style-specific values into installed block code and don't fork it to "restyle" - change the theme via the CSS variables / a preset and every block follows. Want a different look? `search` for a block whose design already fits instead of re-skinning one.
## Density and typography rhythm
ReUI operational UI usually feels dense, not airy. Keep the gap between a title and its supporting description tight by default (`gap-0.5`, `space-y-1`, or `space-y-px`), and smaller than the gap between sections. Match the surrounding ReUI density when you add rows or fields; do not pad operational surfaces like a marketing page.
-54
View File
@@ -1,54 +0,0 @@
# Workflow: find -> install -> read API -> adapt
The core ReUI loop. The MCP tells you what to install and gives you the API; the shadcn CLI installs it; you turn the installed files into correct, themed, data-wired code by **reuse**, not redesign.
## 1. Find (ReUI MCP `search` / `compose_page`)
**Full multi-section page ask?** Call `compose_page(intent, sections?)` FIRST, before searching block-by-block. It returns ordered sections, each with the best block for the intent (top pick + alternates); sections listed in `unavailableSections` have no real inventory - compose those from components, do not force a bad block.
For everything else, call `search` with the user's intent. Pass structured hints whenever you can infer them - you are an LLM, so do the parsing the server cannot:
- `type`: `"component"` (one of the 22 building blocks), `"example"` (a c-\* use-case), `"block"` (a full page/section), `"icon"`.
- `component`: the ReUI component the request implies (`"data-grid"`, `"kanban"`, ...).
- `category`, `features` (e.g. `["sortable","pagination"]`), `free`.
Example: "build a users management page with filters" -> `search({ query: "users management page with filters", type: "block", component: "data-grid", features: ["filters"] })`.
Each result has `install`, `previewUrl`, `docsUrl`, `componentsUsed`, `score`, `termCoverage`, and `whyMatch`. `score` is relative to the top hit (the top is ~100 by construction), not an absolute quality - compare results to each other, and show the user the top options if several score closely; do not silently guess. A low `termCoverage` means a weak match even with a high score - rephrase or widen.
**Always show the preview link.** Whenever you list or recommend items - from `search`, `search_icons`, `list_components`, `compose_page`, or a getter - include each item's `previewUrl` (a live preview page) so the user can SEE it before you install. Blocks and examples link to an individual live preview; icons and components to their live category/component page. This applies to every listing, not only a single pick.
## 2. Install (shadcn CLI)
Run the result's `install` command from the project root, non-interactively:
```bash
npx shadcn@latest add @reui/<name> --yes
```
The CLI reads `components.json`, installs the correct base+style variant, resolves `registryDependencies` (a block pulls in its components), installs npm deps, and rewrites aliases. Do not pass the base/style. See [cli.md](./cli.md).
## 3. Read the API (do not guess props)
Before writing code against any component an item uses:
1. The item's `componentDigests` already give a 1-line contract per component - often enough to wire it. For the full API, call **`get_component(names)`** with ALL of `componentsUsed` in ONE call (it accepts an array) and read each inline `api` - no web fetch. **Share the component's `docsUrl`** (its API documentation page) with the user whenever you work with that component's API, so they have the full reference; the `/llms.txt` index is a further fallback.
2. Call **`get_examples(name)`** for the free `c-*` examples of that component; install one and **read the added files** to copy the exact composition. This is the fastest correct path - the example shows real wiring you adapt, not invent.
3. About to write a prop you did not see in an `api` or installed file? Run **`validate_usage`** BEFORE writing the code - per-prop documented / notDocumented verdicts plus did-you-mean suggestions. notDocumented means read the API, not push on.
## 4. Adapt (reuse-first) - do not skip
Installing files is not the end, and redesigning them defeats the point. First note the project's **base** so you write the right API - read `components.json` -> `style` and take the segment before the first `-` (`base-nova` -> Base UI, `radix-nova` -> Radix UI), see [components.md](./components.md). After `add`:
1. **Read the added files**; keep the composition intact. For a block, verify the components are wired correctly (for `data-grid`: a `useTable({ features: dataGridFeatures, ... })` instance passed as `table`, `recordCount` set - see [components.md](./components.md)).
2. **Replace demo data with the user's real data** via typed structures (see [adapting.md](./adapting.md)).
3. **Fix icon imports** to the project's icon library (see [icons.md](./icons.md)).
4. **Align styling** to semantic tokens and the active theme - no raw colors (see [styling.md](./styling.md)).
5. **Validate before finalizing**: if your adaptation introduced components or props you did not read in an `api` or example, run `validate_usage` on them.
6. **Hit the craft bar** - clear hierarchy, deliberate density, the empty / loading / error states, subtle motion, and mobile-first responsiveness (see [craft.md](./craft.md)). Generic-looking output means you under-reused the design, not that it needs restyling.
7. **Pass the quality gates** (security, a11y, scroll) - call the MCP `get_audit_checklist` tool and clear every item (see [quality.md](./quality.md)).
8. **Typecheck / lint**.
## If no single block fits
Compose from components (`compose_page` tells you which sections have no block inventory via `unavailableSections`). `search` the components you need, read each `get_component` API, install a worked `get_examples` example for each, and assemble by adapting those examples. A block in the same category is a useful reference - install it and read its files to see how ReUI composes those components, then adapt.
-58
View File
@@ -1,58 +0,0 @@
# ReUI MCP: full reference
The ReUI MCP (`https://mcp.reui.io`, Streamable HTTP) is free to use but needs a ReUI account: on first use the agent signs in with ReUI (a free account is created if the user has none), so every request is tied to an account. Free covers components and examples; a Pro or Ultimate license unlocks premium blocks and Motion Icons and removes the daily request limit. It does **discovery + guidance** (search, inline APIs, page planning, validation) and never serves source; the shadcn CLI does **installation**, and the license key lives there (the `@reui` entry in `components.json`, backed by `.env.local`). Goal: from the user's intent to correct, themed, data-wired ReUI code in the **fewest tokens and calls**, with **no guessing**.
## Golden path (token-optimal - follow this order)
Most tasks need 2-4 MCP calls and ZERO web fetches:
1. **`search(query, ...hints)`** -> pick the top 1-3 results. Each result already carries `install`, `previewUrl`, `docsUrl`, `componentsUsed`, `score`, `whyMatch`. The payload is complete - do not call another tool just to "confirm" a result.
2. **`get_component([...componentsUsed])`** in ONE batched call (one name or an array of up to 20) -> read each inline `api`. This **replaces** fetching docs pages. Often skippable: search responses carry `componentDigests`, a compact API contract per referenced component.
3. **`get_examples(component)`** -> install ONE returned `c-*` example, read the added files, copy the composition.
4. **`get_install_command(item)`** only to validate a name you are unsure of (results already include `install`). Run the install with the shadcn CLI (`--yes`).
5. **`get_audit_checklist()`** before declaring done.
If you already know the exact item name, skip `search`. Everything else is situational.
## The 5 task-specific tools (when to reach for each)
- **`compose_page`** - BEFORE building any full page (dashboard, settings, billing, landing). Pass the intent (and optionally the sections you want); it returns ordered sections, each with the best premium block for the intent (top pick + alternates). Sections with no real inventory are listed honestly in `unavailableSections` - compose those from components instead of forcing a bad block.
- **`search_icons`** - whenever you need icons, especially several. Batch up to 24 concepts in one call; each concept returns its best icons with install commands. Pass `animated: true` to get only icons that have a hover-animated Motion variant.
- **`validate_usage`** - BEFORE writing code with component names or props you have not read in an inline `api` or an installed example. It checks planned names + props against the indexed API docs and registry item names; returns did-you-mean suggestions and per-prop documented / notDocumented verdicts. Deterministic, no inference - a notDocumented prop means stop and read the API, not push on.
- **`whats_new`** - when your registry knowledge might be stale (a name 404s, the user mentions an item you don't know). Returns items added/removed per build, newest first.
- **`report_issue`** - when an installed item is actually broken (bad source, wrong dependency, broken preview). Goes straight to the ReUI team; rate-limited 5/hour. Not for usage questions.
## All 19 tools
`search`, `get_block`, `get_example`, `get_icon`, `list_block_groups`, `list_block_categories`, `list_example_categories`, `list_icon_categories`, `list_components`, `get_component`, `get_examples`, `search_icons`, `compose_page`, `validate_usage`, `whats_new`, `report_issue`, `get_install_command`, `get_project_context`, `get_audit_checklist`. The MCP serves the full parameter schemas; do not guess parameters beyond them.
## Token + speed rules
- **Batch `get_component`** - ONE call with the whole `componentsUsed` array, never N calls. Skip it entirely when `componentDigests` already answers the question.
- **Read source by installing** - the MCP serves no source. To read or analyze an item's real code, install it with the shadcn CLI and open the local files. Learn an API from the inline `api` / `componentDigests`, never by reading raw source.
- **Infer `search` hints yourself** (`type`, `component`, `category`, `features`, `free`) - hints shrink the result set and the tokens. Keep `limit` low; one right result beats ten.
- Run independent calls (and the shadcn install) concurrently in one turn - serial tool calls are the main source of slowness.
- Don't repeat a search for the same intent; don't call `list_*` to "see everything" - `search` is the entry point, `list_*` is only for browsing a taxonomy the user explicitly wants to explore.
- Prefer `get_component`'s inline `api` over `docsUrl` / `/llms.txt`. Fetch a web page only as a last resort.
## Result shapes (so you don't re-fetch)
- `score` is 0-100 RELATIVE to the top hit (the top is ~100 by construction), not absolute - compare results to each other.
- `termCoverage` (0-1) is the share of the query the item matched - low means a weak match even if the score looks high; rephrase or widen the search.
- Each result carries `whyMatch`, `install`, docs/preview URLs, and a `free` flag; premium items carry `requiredPlan` (`"pro"` for blocks, `"ultimate"` for icons).
- `componentDigests` is a top-level map: a compact API contract per referenced component - often enough to wire an item without a `get_component` call.
- Icon results and `get_icon` include `animated: true` and `installAnimated` when a hover-animated Motion variant exists (animated: `@reui/icons/animated/<style>/<name>`; static: `@reui/icons/default/<style>/<name>`).
## Error playbook
- **401** - the MCP requires a signed-in ReUI account. The client prompts "Sign in with ReUI" (OAuth) on first use; a free account is created if needed. For headless/CI, pass a personal token (`reui_pat_...`, created at https://reui.io/account/mcp?ref=skill) as `Authorization: Bearer`.
- **403 / locked result** - a valid account but the plan does not cover the item: premium blocks need Pro, Motion Icons need Ultimate. Point to https://reui.io/pricing?ref=skill (upgrade). Free accounts still get all components + examples.
- **429** - rate limited (120 requests/min per IP); back off, honor `Retry-After`.
- **not found** (`found: false`) - use the returned `suggestions`, or `search`; check `whats_new` if you suspect a stale name. Never run a fabricated install command.
## Fallbacks
- No ReUI MCP: `npx shadcn@latest search @reui -q "..."` then `add` (generic, no scoring / inline API).
- The shadcn project's own MCP also works over the `@reui` registry: https://ui.shadcn.com/docs/mcp.
Per-agent MCP setup: https://reui.io/docs/mcp?ref=skill
-30
View File
@@ -3,33 +3,3 @@ deploy/netflow-agent/*.py text eol=lf
# Migration hashes authenticate exact reviewed bytes, including existing CRLF files. # Migration hashes authenticate exact reviewed bytes, including existing CRLF files.
# Preserve those bytes on every checkout instead of applying core.autocrlf. # Preserve those bytes on every checkout instead of applying core.autocrlf.
backend/drizzle/*.sql -text backend/drizzle/*.sql -text
# Receipts pinned by SHA-256 over their exact CRLF bytes; keep them byte-for-byte.
/docs/bandwidth-operator-effect-ledger.md -text
/docs/bandwidth-operator-metadata-effect-contract.md -text
/docs/mcp-2gb-operating-progress-20261028.json -text
/docs/mcp-chr-ospf-source-cleanup.json -text
/docs/mcp-chr-ssh-forwarding-policy-observation-v1.json -text
/docs/mcp-db-connected-publisher-source-applied-20261102.json -text
/docs/mcp-db-publisher-journal-lease-source-applied-20261102.json -text
/docs/mcp-db-registry-fusion-acceptance-20261101.json -text
/docs/mcp-db-registry-read-contract-acceptance-20261101.json -text
/docs/mcp-guarded-peer-sources-acceptance-20261029.json -text
/docs/mcp-ipsec-user-create-acceptance-v2.json -text
/docs/mcp-linux-inventory-collector-raw-acceptance-20261101.json -text
/docs/mcp-netflow-settings-acceptance-v2.json -text
/docs/mcp-node-key-effect-progress-20261029.json -text
/docs/mcp-operator-peer-metadata-managed-acceptance-20261028.json -text
/docs/mcp-operator-public-metadata-progress-20261028.json -text
/docs/mcp-portal-permission-catalog-source-applied-20261103.json -text
/docs/mcp-public-registry-metadata-acceptance-20261028.json -text
/docs/mcp-publisher-current-operator-linux-acceptance-20261103.json -text
/docs/mcp-routing-manual-inverse-charge-reproduction-20261101.json -text
/docs/mcp-routing-manual-inverse-charge-reproduction-20261101.md -text
/docs/mcp-routing-manual-inverse-six-case-acceptance-20261101.json -text
/docs/mcp-routing-manual-inverse-six-case-acceptance-20261101.md -text
/docs/mcp-routing44-upgrade-bridge-acceptance-20261101.json -text
/docs/mcp-server-poll-expiry-alignment-source-applied-20261102.json -text
/docs/mcp-two-chr-v40-execution-20261106.json -text
/docs/mcp-vrf-production-ticket-acceptance-v1.json -text
/docs/netflow-agent.md -text
+6 -2
View File
@@ -16,7 +16,7 @@ permissions:
packages: write packages: write
jobs: jobs:
backend-preview: backend-preview:
if: ${{ gitea.event_name == 'workflow_dispatch' }} if: ${{ gitea.event_name == 'workflow_dispatch' && gitea.ref == 'refs/heads/codex/mcp-acceptance-current-20261103' }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 40 timeout-minutes: 40
env: env:
@@ -24,6 +24,7 @@ jobs:
EXPECTED_SHA: ${{ inputs.expected_sha }} EXPECTED_SHA: ${{ inputs.expected_sha }}
DISPATCH_SHA: ${{ gitea.sha }} DISPATCH_SHA: ${{ gitea.sha }}
DISPATCH_EVENT: ${{ gitea.event_name }} DISPATCH_EVENT: ${{ gitea.event_name }}
DISPATCH_REF: ${{ gitea.ref }}
REPOSITORY: ${{ gitea.repository }} REPOSITORY: ${{ gitea.repository }}
RUN_ID: ${{ gitea.run_id }} RUN_ID: ${{ gitea.run_id }}
RUN_ATTEMPT: ${{ gitea.run_attempt }} RUN_ATTEMPT: ${{ gitea.run_attempt }}
@@ -42,6 +43,7 @@ jobs:
run: | run: |
set -euo pipefail set -euo pipefail
test "$DISPATCH_EVENT" = workflow_dispatch test "$DISPATCH_EVENT" = workflow_dispatch
test "$DISPATCH_REF" = refs/heads/codex/mcp-acceptance-current-20261103
[[ "$EXPECTED_SHA" =~ ^[0-9a-f]{40}$ ]] [[ "$EXPECTED_SHA" =~ ^[0-9a-f]{40}$ ]]
test "$EXPECTED_SHA" = "$DISPATCH_SHA" test "$EXPECTED_SHA" = "$DISPATCH_SHA"
test "$(git rev-parse HEAD)" = "$EXPECTED_SHA" test "$(git rev-parse HEAD)" = "$EXPECTED_SHA"
@@ -137,7 +139,7 @@ jobs:
if-no-files-found: error if-no-files-found: error
retention-days: 14 retention-days: 14
frontend-preview: frontend-preview:
if: ${{ gitea.event_name == 'workflow_dispatch' }} if: ${{ gitea.event_name == 'workflow_dispatch' && gitea.ref == 'refs/heads/codex/mcp-acceptance-current-20261103' }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 40 timeout-minutes: 40
env: env:
@@ -145,6 +147,7 @@ jobs:
EXPECTED_SHA: ${{ inputs.expected_sha }} EXPECTED_SHA: ${{ inputs.expected_sha }}
DISPATCH_SHA: ${{ gitea.sha }} DISPATCH_SHA: ${{ gitea.sha }}
DISPATCH_EVENT: ${{ gitea.event_name }} DISPATCH_EVENT: ${{ gitea.event_name }}
DISPATCH_REF: ${{ gitea.ref }}
REPOSITORY: ${{ gitea.repository }} REPOSITORY: ${{ gitea.repository }}
RUN_ID: ${{ gitea.run_id }} RUN_ID: ${{ gitea.run_id }}
RUN_ATTEMPT: ${{ gitea.run_attempt }} RUN_ATTEMPT: ${{ gitea.run_attempt }}
@@ -176,6 +179,7 @@ jobs:
run: | run: |
set -euo pipefail set -euo pipefail
test "$DISPATCH_EVENT" = workflow_dispatch test "$DISPATCH_EVENT" = workflow_dispatch
test "$DISPATCH_REF" = refs/heads/codex/mcp-acceptance-current-20261103
[[ "$EXPECTED_SHA" =~ ^[0-9a-f]{40}$ ]] [[ "$EXPECTED_SHA" =~ ^[0-9a-f]{40}$ ]]
test "$EXPECTED_SHA" = "$DISPATCH_SHA" test "$EXPECTED_SHA" = "$DISPATCH_SHA"
test "$(git rev-parse HEAD)" = "$EXPECTED_SHA" test "$(git rev-parse HEAD)" = "$EXPECTED_SHA"
-6
View File
@@ -78,9 +78,3 @@ backend/dist-operator/
# Agent-local permissions can contain commands with deployment credentials. # Agent-local permissions can contain commands with deployment credentials.
/.claude/settings.local.json /.claude/settings.local.json
# Local scratch and disposable build outputs (root-anchored).
/tmp-*
/.next-*/
/.serena/
/backend/dist-*/
-67
View File
@@ -1,67 +0,0 @@
---
name: reui
description: Use the ReUI registry from your AI agent - find, install, and correctly use ReUI components (the 22 free building blocks like data-grid, kanban, filters), their free examples, premium blocks, and Motion Icons. Applies in any project using ReUI, the @reui registry, REUI_LICENSE_KEY, or any shadcn project where the user asks for premium blocks, data grids, kanban boards, dashboards, or full pages. Pairs with the free ReUI MCP server for live, scored registry search and inline component APIs.
user-invocable: false
allowed-tools: Bash(npx shadcn@latest *), Bash(pnpm dlx shadcn@latest *), Bash(bunx --bun shadcn@latest *)
---
> **ReUI skill version `fe6742e29c`.** If the ReUI MCP's `get_agent_skill` reports a newer `version`, re-run the ReUI installer (see `get_agent_skill` -> `install.recommended`) to update this skill. Cloud/tools-only agents have no local file and always read the latest - they can ignore this.
# ReUI for Agents
ReUI is a shadcn-compatible registry. It ships four things you **reuse** - never redesign:
- **components** - the 22 ReUI building blocks with real APIs: `data-grid`, `kanban`, `filters`, `date-selector`, `tree`, `stepper`, ... (free)
- **examples** - free `c-*` single-pattern use-cases of a component (`c-kanban-1`); install one and read it to see exact composition
- **blocks** - premium full-page sections that compose components (`data-grid-2`, `pricing-page-1`); Pro or Ultimate license at install
- **icons** - Motion Icons in 4 styles, static + hover-animated variants; Ultimate license at install
The skill is free and this MCP is free to use; it just needs a ReUI account. On first use your agent opens a browser "Sign in with ReUI" prompt (a free account is created if you don't have one). Free covers components and examples with a daily request allowance; a Pro or Ultimate license unlocks premium blocks and Motion Icons and removes the limit (see [rules/registry.md](./rules/registry.md)). The same account and skill work in every agent and service the MCP connects to - this skill is agent-agnostic.
Skill + MCP are a team: this skill is the workflow (how to find, install, read the API, and adapt by reuse); the MCP is the live data and the hands (search, get_component, install commands). Your job: find the right item, install it with the shadcn CLI, read its real API, and **adapt by reuse** - wire real data and theme it; do not hand-roll or restyle what ReUI already provides. This skill **layers on the shadcn skill**: follow that for generic rules (spacing, `cn()`, semantic colors, forms); follow this for everything ReUI-specific.
## The core loop (MCP-native)
1. **Find** - call the ReUI MCP `search` tool with the user's intent. It returns a ranked, scored list across components/examples/blocks/icons, each with an `install` command, `previewUrl`, `docsUrl`, and `componentsUsed`. Pass hints (`type`, `component`, `category`, `features`, `free`) when you can infer them.
2. **Install** - run the returned command non-interactively (`npx shadcn@latest add @reui/<name> --yes`). The CLI resolves deps, aliases, and the base/style from `components.json`. See [cli.md](./rules/cli.md).
3. **Read the API (on your base)** - first note your base from `components.json` -> `style` (`base-nova` -> Base UI, `radix-nova` -> Radix UI). For each component an item uses, call `get_component(name)` and read its **inline `api`** (no web fetch); then `get_examples(name)` to install a worked example and copy its composition - the installed files are already in your base. Whenever you work with a component's API, also **share its `docsUrl`** (the primitive's API documentation page) with the user so they have the full reference. See [components.md](./rules/components.md).
4. **Adapt (reuse-first)** - swap demo data for real data, fix icon imports, align tokens. Do not redesign. See [adapting.md](./rules/adapting.md).
**Always show the preview.** Every item a tool returns carries a `previewUrl` (a live preview page). Whenever you list, recommend, or present ReUI items to the user - blocks, components, examples, or icons, whether from `search`, `search_icons`, `list_components`, `compose_page`, or any getter - include each item's `previewUrl` so they can SEE it before installing. Blocks and examples open an individual live preview; icons and components link to their live category/component page. Never present an item without its preview link.
If the ReUI MCP is not configured, fall back to `npx shadcn@latest search @reui -q "..."` then `add` - but the MCP gives scored matches + inline APIs; prefer it.
## Commands
Run ReUI as explicit slash commands (via the ReUI MCP) **or** just ask in plain language - both run the same workflow.
| Command | Invoke | Does |
| ----------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------ |
| **build** | `/mcp__reui__build <what>` | Compose a page/section/feature from ReUI: plan → install → read API → adapt → craft → audit. |
| **add** | `/mcp__reui__add <item>` | Find & install one component/example/block/icon and wire it in. |
| **fix** | `/mcp__reui__fix [target]` | Diagnose & fix ReUI usage: wrong/undocumented props, base/radix mismatch, missing states, a11y/scroll. |
| **improve** | `/mcp__reui__improve [target]` | Refine + extend existing ReUI UI to a production-exceptional bar (hierarchy, density, states, responsive, motion). |
Invocation differs slightly per agent (`/mcp__reui__build` in Claude Code/Cursor/Windsurf, `/mcp.reui.build` in VS Code). No command surface? Just describe what you want - this skill drives the identical loop.
## When to reach for ReUI vs plain shadcn
| Need | Reach for |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| A full page or section (dashboard, billing, auth, pricing, settings) | `compose_page` first (plans sections + best blocks), then ReUI **blocks** |
| A data table with sorting/filtering/pagination/virtualization | the **data-grid** component (never hand-roll a `<table>`) |
| A drag-and-drop board | the **kanban** component |
| Advanced column filtering, date range, tree, stepper, ... | the matching ReUI **component** |
| A single generic control already in shadcn (Button, Dialog, Select) | plain **shadcn** |
## Detailed references
- [rules/registry.md](./rules/registry.md) - the four types, the @reui registry, base/radix, free vs premium + license
- [rules/workflow.md](./rules/workflow.md) - the find -> install -> read-API -> adapt loop (most important)
- [rules/components.md](./rules/components.md) - the 22 components, the data-grid contract, base vs radix
- [rules/adapting.md](./rules/adapting.md) - reuse-first: preserve the design (no over-customizing), reuse examples + a block's own elements, real data, don't invent APIs
- [rules/craft.md](./rules/craft.md) - make it exceptional: point of view, hierarchy, density, states, responsive, motion, the bar
- [rules/quality.md](./rules/quality.md) - security, accessibility, and scroll gates (the done gate)
- [rules/styling.md](./rules/styling.md) - ReUI extended tokens, theme adaptation, density
- [rules/icons.md](./rules/icons.md) - portable icons, swapping imports, Motion Icons (static + animated)
- [tools.md](./tools.md) - the ReUI MCP: golden path, the 19 tools, token rules, result shapes, errors
-43
View File
@@ -1,43 +0,0 @@
# Adapting installed ReUI code (reuse-first, no AI slop)
ReUI items ship production-quality. Your job is to **adapt by reuse** - wire real data and fit the app - not to redesign or hand-roll. The output should look like ReUI built it for this product.
## Preserve the design - don't over-customize
The design IS the product. A ReUI block/component encodes senior-designer decisions: spacing, hierarchy, density, color treatment, and component choices. The fastest way to turn a premium block back into generic AI slop is to "improve" its look - so don't.
- Change **data, copy, and props**; keep the **structure and styling** it ships with. Make the **smallest** change that wires the real data. If your diff touches `className` / JSX structure more than data / props, you are over-customizing - stop and reuse.
- Don't swap ReUI components for hand-rolled ones, restructure the layout, re-skin spacing / radius / colors, or add decorative chrome. Let the installed components carry the default spacing, radius, sizing, icon rhythm, density, and state styling; add custom Tailwind only when a component genuinely lacks a contract you need.
- Want a different look? `search` for a block whose design already fits and reuse that - don't restyle this one into a new design.
## Reuse the parts: examples and the block's own elements
- **Examples are building parts.** A free `c-*` example is a correct, single-pattern composition you can reuse. Before composing from scratch, `get_examples(component)`, install the closest one, and reuse its wiring - assemble UI from examples instead of hand-rolling what an example already shows.
- **Reuse a block's own elements.** Need more rows, cards, items, or sections than ship by default? Repeat the block's **existing** element by mapping real data through the same markup - never invent parallel markup that drifts from its design. Need a variant (empty / loading / expanded)? Derive it from an element the block already has.
## Don't invent (read, don't guess)
- Never write a prop, variant value, import path, or `@reui/...` name you didn't read in a component's inline `api`, an installed example, or a `search` result. If you didn't see it, treat it as nonexistent - call `get_component` / `get_examples` / `search` first, or run the MCP `validate_usage` tool to check planned names + props against the docs before writing code.
- If a getter returns `found: false` or `search` returns nothing, say so and fall back (plain shadcn, or ask) - never fabricate an install command or an API.
## What to change vs leave alone
- **Change:** the item's own data, copy, props, and layout to fit the app.
- **Leave alone:** installed component files, hooks, and the shared theme - do not edit vendored ReUI internals; change behavior through props and the documented API.
- Blocks are **portable React** - no `next/link`, `next/image`, or other framework-runtime imports inside them. Keep them portable.
## Demo data -> real data
- Replace every placeholder with the user's real data. Model it as **typed data structures** and **map over arrays** - never duplicate JSX per row/card. Keep small block-specific formatters next to the data.
- Wire the real source (columns, fields, fetch). For `data-grid`, implement the server fetch contract if the user needs server-side data.
- **Type from the component API, derive during render.** Type domain state through the component's own types - e.g. map status to `BadgeProps["variant"]` via a typed `Record<Status, …>` - instead of stringly-typed values. Compute view state during render; don't mirror derived data into `useState`/`useEffect`.
- **Adapt on the right base.** Use the API for the project's base (Base UI vs Radix - see [components.md](./components.md)); the installed files are already base-correct, so reuse their shape rather than translating from memory.
## Believable content (no AI tells)
- Use realistic labels, counts, timestamps, and statuses that map to a real workflow.
- No decorative buttons, fake tabs, meaningless toggles, equal-weight card walls, empty gradients, ornamental icons, or generic SaaS filler. Every element should do something.
## Operational surfaces (settings / profile / admin)
Pick ONE archetype and keep the family consistent: a vertical rail (3-6 sections), horizontal tabs (5-8), or a frame/stack. Prefer `frame` for tool-like surfaces, a card for profile-like ones. Don't mix archetypes in one surface.
-60
View File
@@ -1,60 +0,0 @@
# CLI: registry setup, license, non-interactive install
## Registry setup (one-time, per project)
Free items (the 22 components and all `c-*` examples) need only the plain string registry in `components.json`:
```json
{ "registries": { "@reui": "https://reui.io/r/{style}/{name}.json" } }
```
Premium items (blocks; Motion Icons and templates) require a ReUI license at install:
1. Add the key to `.env.local`:
```bash
REUI_LICENSE_KEY=your-license-key
```
2. Switch `components.json` to the authenticated object form:
```json
{
"registries": {
"@reui": {
"url": "https://reui.io/r/{style}/{name}.json",
"headers": { "Authorization": "Bearer ${REUI_LICENSE_KEY}" }
}
}
}
```
The shadcn CLI expands `${REUI_LICENSE_KEY}` from `.env.local` inside `components.json`, but an MCP client config never expands variables, so a ReUI MCP server config must carry the raw token instead (for example `reui_pat_your_token_here`).
The MCP `get_project_context` tool returns the right config. Full guide: https://reui.io/docs/registry?ref=skill
## Installing
Use the project's package runner (check `packageManager`):
```bash
npx shadcn@latest add @reui/<name> --yes # npm
pnpm dlx shadcn@latest add @reui/<name> --yes # pnpm
bunx --bun shadcn@latest add @reui/<name> --yes # bun
```
`--yes` skips confirmation prompts. The CLI auto-detects the package manager from the lockfile (there is no `--package-manager` flag). It also resolves the correct base+style variant from `components.json`, so do not pass a style.
## Handling prompts and conflicts
- **Always pass `--yes`** so the CLI does not block on confirmation prompts.
- **Do NOT pass `--overwrite` by default.** If the CLI reports an existing file, read the output and resolve deliberately: install under a different name, adjust the path, or ask the user. Only use `--overwrite` when the user explicitly wants to replace a file.
- **Preview first when touching an existing project**: `npx shadcn@latest add @reui/<name> --dry-run` shows what would change; `--diff <file>` shows a specific file's diff. Use these before overwriting.
- Run from the **project root** so `components.json` and `.env.local` are found.
## Free vs premium boundary
- Public, no key: `c-*` examples and the 22 components (`@reui/data-grid`, `@reui/badge`, ...) that those examples depend on.
- Key required at install: blocks (`@reui/<category>-N`) need a Pro or Ultimate license; Motion Icons (`@reui/icons/...`) and templates need Ultimate.
If an install 401/403s, the license key is missing, invalid, or the plan does not cover that resource (blocks: Pro or higher; icons and templates: Ultimate). Point the user to https://reui.io/account?ref=skill (their key) or https://reui.io/pricing?ref=skill (upgrade).
-428
View File
@@ -1,428 +0,0 @@
# ReUI components
The 22 ReUI building blocks: `alert`, `autocomplete`, `badge`, `cascader`, `code-block`, `data-grid`, `date-selector`, `event-calendar`, `filters`, `frame`, `gantt`, `icon-stack`, `icon-tile`, `kanban`, `number-field`, `phone-input`, `rating`, `scrollspy`, `sortable`, `stepper`, `timeline`, `tree`. Examples and blocks are composed from these.
**Rule one: never guess a component's API. Read it first.** Call **`get_component(name)`** for its inline `api` (props + usage, no web fetch), and **share the result's `docsUrl`** (the component's API documentation page) with the user whenever you work with that component's API, so they have the full reference (the `/llms.txt` index is a further fallback). Then call **`get_examples(name)`** to install a worked example and copy real composition. The contracts below are first-try orientation (required props, composition shape, the one gotcha); the inline `api` is the full reference. No single block fits? Compose: search the components you need, read each `get_component`, install a `get_examples` example per component, and adapt.
## data-grid (the flagship - read its API every time)
`data-grid` wraps TanStack Table v9. It is NOT a styled `<table>` and does NOT take `data`/`columns` props directly. The contract:
- Build a TanStack table instance with `useTable({ features: dataGridFeatures, ... })` (columns, data). `dataGridFeatures` is exported by the primitive and already bundles sorting, filtering, pagination, row selection, expanding, pinning, resizing and faceting, so there are no per-table row models to wire.
- Pass that instance to `<DataGrid table={table} recordCount={total}>`.
- Compose the body with `DataGridTable` inside `DataGrid`, and enable features through `tableLayout` (e.g. `{ headerSticky: true, columnsResizable: true }`), not ad-hoc classes.
- Server-side data uses the documented fetch shape (`recordCount` is the total for pagination).
```tsx
const table = useTable({
features: dataGridFeatures,
data,
columns,
})
<DataGrid table={table} recordCount={data.length}>
<DataGridTable />
</DataGrid>
```
Common mistakes:
- **Incorrect:** `<DataGrid data={rows} columns={cols} />` - these props do not exist. **Correct:** build a `useTable({ features: dataGridFeatures, ... })` instance and pass `table={table}` + `recordCount`.
- **Incorrect:** a raw `<table>` / hand-rolled pagination. **Correct:** use `data-grid`; read its API for sticky header, pagination, virtualization, row selection.
- **Incorrect:** styling rows/cells with arbitrary classes. **Correct:** drive layout via `tableLayout` and the primitive's `DataGridColumnMeta` (e.g. `cellClassName`, `headerTitle`), set through the bundle's `columnMeta` slot.
## event-calendar
**Required:** events via `events`/`onEventsChange` (controlled) or `defaultEvents` (uncontrolled), plus a height on the root.
**Shape:**
```tsx
<EventCalendar defaultEvents={events} defaultView="month" className="h-[560px]">
<EventCalendarNav />
<EventCalendarContent />
</EventCalendar>
```
**Gotcha:** headless-first: `EventCalendarContent` renders the active view (month/week/day/days/agenda; a resource view activates when `resources` is passed) - there is no per-view JSX to compose. Events are `{ id, title, start, end (exclusive), allDay?, color?, recurrence?, resourceId? }`. Mutations flow through `onEventUpdate`/`canDropEvent` (return `false` to reject); the root needs an explicit height because it is a min-h-0 flex column.
## gantt
**Required:** `resources` (the left tree) plus bars via `events`/`defaultEvents` attached by `resourceId`.
**Shape:**
```tsx
<Gantt defaultEvents={bars} resources={tasks} defaultScale="month" className="h-[480px]">
<GanttNav />
<GanttView />
</Gantt>
```
**Gotcha:** bars move along the time axis only (never across rows) and are all-day spans with exclusive `end`; `progress` is 0-100. Scales are `day | week | month | quarter | year`. Zoom control, infinite scroll, summary rollups, and row checkboxes are ON by default - turn off what you do not need. Same `onEventUpdate`/`canDropEvent` commit pipeline as `event-calendar`; the root needs an explicit height.
## kanban
**Required:** `value` (`Record<string, T[]>`), `onValueChange`, `getItemValue`
**Shape:**
```tsx
<Kanban value={cols} onValueChange={setCols} getItemValue={(i) => i.id}>
<KanbanBoard>
{Object.entries(cols).map(([id, items]) => (
<KanbanColumn key={id} value={id}>
<KanbanColumnHandle><h3>{id}</h3></KanbanColumnHandle>
<KanbanColumnContent value={id}>
{items.map((i) => (
<KanbanItem key={i.id} value={i.id}>
<KanbanItemHandle>{i.title}</KanbanItemHandle>
</KanbanItem>
))}
</KanbanColumnContent>
</KanbanColumn>
))}
</KanbanBoard>
<KanbanOverlay><div className="bg-muted size-full rounded-md" /></KanbanOverlay>
</Kanban>
```
**Gotcha:** state is `Record<columnId, T[]>`. Each `KanbanColumnContent value` must match its parent `KanbanColumn value`. Omit `KanbanOverlay` and the drag preview silently breaks.
## sortable
**Required:** `value` (`T[]`), `onValueChange`, `getItemValue`
**Shape:**
```tsx
<Sortable value={items} onValueChange={setItems} getItemValue={(i) => i.id}>
{items.map((i) => (
<SortableItem key={i.id} value={i.id}>
<SortableItemHandle><GripVertical /></SortableItemHandle>
{i.label}
</SortableItem>
))}
</Sortable>
```
**Gotcha:** a flat 1D reorder list (not columns - that is `kanban`). `getItemValue` must return a stable, unique string. Pass `layout="grid"` or `layout="nested"` for non-list layouts.
## filters
**Required:** `fields` (`FilterField[]`). The value is ONE `FilterQuery` tree - `query` + `onQueryChange`, or uncontrolled `defaultQuery`.
**Shape:**
```tsx
const fields: FilterField[] = [
{ id: "title", label: "Title", type: "text" },
{
id: "status",
label: "Status",
type: "select",
options: [
{ value: "active", label: "Active" },
{ value: "archived", label: "Archived" },
],
},
]
const [query, setQuery] = useState<FilterQuery>(() => createFilterQuery())
<Filters fields={fields} query={query} onQueryChange={setQuery} />
```
**Gotcha:** the state is a TREE, not a list of chips. `FilterQuery` is a group of rules joined by `and`/`or` and a group may hold another group, so `(A and B) or C` is expressible; a rule is `{ id, type: "rule", path: ["status"], operator, value }` and `path` is the whole nested attribute path, root first. The pre-rewrite API is GONE: there is no `filters`/`onChange` prop, no `FilterFieldConfig` (fields are `FilterField`, nested through their own `fields`, keyed `id` not `key`), and no `createFilter()` - it minted ids inside a pure function and broke hydration, so ids now come from `createFilterIdFactory(seed)` seeded off `useId`, and `createFilterQuery()` / `createFilterRule()` take one. Read the query back with `flattenFilterConditions` (`{ path, field, operator, values, negated }` per rule, incomplete rules skipped) and walk the tree yourself when the parentheses carry meaning - the primitive compiles nothing, no SQL, no query string.
`variant` picks the chrome over that one query: `"basic"`, the default, is the flat chip row for a toolbar over a table; `"advanced"` is the condition builder, hung off a trigger or rendered in place with `advancedMode="inline"`. Both read and write the same tree, so a saved view built in one opens in the other. Other props worth knowing before you hand-roll them: `size` is two rungs, `"sm" | "default"`, resolved per style (there is no `lg`); `reorderable` turns on drag and Alt+Arrow row moves in the builder; `onBeforeQueryChange` is the ONE veto point for every write (return `false` to refuse, it cannot rewrite); `editors` registers custom value editors a field selects by `editor` name; `labels` / `operatorLabels` own every rendered string; `pathCollapse` + `maxPathSegments` shorten deep attribute paths; `renderChip` / `renderValue` / `renderEmpty` replace rendered parts. On a field, `loadOptions` supplies async options with paging and `resolveValues` renders a chip restored from a saved view whose option was never loaded. Pairs naturally with `data-grid`.
## cascader
**Required:** `items` (a tree of `{ value, label, children? }`), plus the panel parts inside `CascaderContent`.
**Shape:**
```tsx
<Cascader items={items} value={value} onValueChange={setValue}>
<CascaderTrigger render={<Button variant="outline" />}>
<CascaderValue placeholder="Select an attribute" />
</CascaderTrigger>
<CascaderContent className="w-80">
<CascaderPanel>
<CascaderNav>
<CascaderBreadcrumb />
<CascaderInput />
</CascaderNav>
<CascaderEmpty />
<CascaderList maxHeight={288}>
<CascaderItems />
</CascaderList>
<CascaderStatus />
</CascaderPanel>
</CascaderContent>
</Cascader>
```
**Gotcha:** pressing a branch NAVIGATES, it does not select - only leaves are selectable until you pass `selectable="any"` or a predicate, and once a branch is selectable its chevron becomes the only way to open it. `CascaderInput` must stay inside `CascaderContent` (Base UI refills the query from the selection when the input sits outside the popup). Always include `CascaderStatus`: it is the live region announcing level changes, which the visual breadcrumb does not provide to screen readers. Accepts a flat adjacency list via `getParent` as well as nested `children`. `searchScope="deep"` searches every level and annotates results with their path; `multiple` gives checkbox rows; `inline` + a bare `CascaderPanel` embeds it with no popover.
The shape above is `mode="drill"`, the default. `mode="tree"` keeps the same parts (drop `CascaderBreadcrumb`, pass `showBack={false}`, drive expansion with `expanded`/`onExpandedChange`); `mode="columns"` REPLACES `CascaderList` + `CascaderItems` with a single `CascaderColumns`, and has no breadcrumb. Other props worth knowing before you hand-roll them: `cascade` (multi-select only, parent/child selection with indeterminate branches - pair it with `selectable="any"`, since a leaf-only tree can never cascade), `indicator={false}` to drop the single-select check and its gutter (visual only, no-op with `multiple`), `virtualize`/`virtualizeThreshold` plus `CascaderVirtualItems` for long levels, and `getChildren` for async levels with cursor paging, retry on failure and optional `prefetch`. `CascaderFooter` pins commands below the list (`actions` is the quick path) and `CascaderSubmenu` opens one as a side-anchored flyout with the full menu keyboard model. To head a run of rows use `CascaderGroup` wrapping a `CascaderLabel` - a bare label inside a listbox names nothing and is dropped from the accessibility tree - and `CascaderSeparator` for the rule between runs. Every rendered string comes from `labels`, and the panel is RTL-correct under a `DirectionProvider` or `dir="rtl"`.
## date-selector
**Required:** none, but wire `onChange` to capture the value.
**Shape:**
```tsx
const [value, setValue] = useState<DateSelectorValue | undefined>()
<DateSelector value={value} onChange={setValue} label="Due date" />
```
**Gotcha:** the value is a structured `DateSelectorValue` (period / operator / start+end dates), NOT a `Date` - never pass a raw `Date`. Use `allowRange={false}` to lock single-date picking. Read `get_component("date-selector")` for the value shape.
## tree
**Required:** `tree` (a `@headless-tree/core` instance you construct)
**Shape:**
```tsx
<Tree tree={tree}>
{tree.getItems().map((item) => (
<TreeItem key={item.getId()} item={item}>
<TreeItemLabel />
</TreeItem>
))}
</Tree>
```
**Gotcha:** `Tree` is a styled shell - it takes a headless-tree instance via `tree`, NOT `data`/`items` props. Build the instance with `@headless-tree/react`. External API: https://headless-tree.lukasbach.com/
## stepper
**Required:** `StepperItem step` (number), `StepperContent value` (number)
**Shape:**
```tsx
<Stepper defaultValue={1}>
<StepperNav>
<StepperItem step={1}>
<StepperTrigger><StepperIndicator>1</StepperIndicator></StepperTrigger>
<StepperSeparator />
</StepperItem>
<StepperItem step={2}>
<StepperTrigger><StepperIndicator>2</StepperIndicator></StepperTrigger>
</StepperItem>
</StepperNav>
<StepperPanel>
<StepperContent value={1}>Step 1 content</StepperContent>
<StepperContent value={2}>Step 2 content</StepperContent>
</StepperPanel>
</Stepper>
```
**Gotcha:** steps are 1-indexed. Without `StepperPanel` + `StepperContent` you render the nav trail but no body. Put `StepperSeparator` in every `StepperItem` except the last.
## timeline
**Required:** `TimelineItem step` (number)
**Shape:**
```tsx
<Timeline>
<TimelineItem step={1}>
<TimelineHeader>
<TimelineDate>March 2024</TimelineDate>
<TimelineTitle>Project initialized</TimelineTitle>
</TimelineHeader>
<TimelineIndicator />
<TimelineSeparator />
<TimelineContent>Repo and architecture set up.</TimelineContent>
</TimelineItem>
</Timeline>
```
**Gotcha:** each item needs a unique `step`. `orientation` is `"vertical"` (default) or `"horizontal"`. This is a static event display, not interactive like `stepper`.
## autocomplete
**Required:** `items` (array; each item has at least `value`)
**Shape:**
```tsx
<Autocomplete items={items}>
<AutocompleteInput placeholder="Search..." />
<AutocompleteContent>
<AutocompleteEmpty>No results found.</AutocompleteEmpty>
<AutocompleteList>
{(item) => (
<AutocompleteItem key={item.value} value={item}>{item.label}</AutocompleteItem>
)}
</AutocompleteList>
</AutocompleteContent>
</Autocomplete>
```
**Gotcha:** `AutocompleteList` takes a render-prop `(item) => ReactNode`, NOT a mapped array of children. External API: https://base-ui.com/react/components/autocomplete
## phone-input
**Required:** none, but wire `onChange`.
**Shape:**
```tsx
<PhoneInput placeholder="Enter phone number" defaultCountry="US" value={value} onChange={setValue} />
```
**Gotcha:** `value`/`onChange` use an E.164 string (e.g. `"+14155551234"`), not a display-formatted string; `onChange` can fire `undefined`. `defaultCountry` is a 2-letter ISO code. Wraps `react-phone-number-input`.
## number-field
**Required:** wrap the controls in `NumberFieldGroup`.
**Shape:**
```tsx
<NumberField defaultValue={0}>
<NumberFieldScrubArea label="Quantity" />
<NumberFieldGroup>
<NumberFieldDecrement />
<NumberFieldInput />
<NumberFieldIncrement />
</NumberFieldGroup>
</NumberField>
```
**Gotcha:** import from `@/components/ui/number-field`. The accessible label goes on `NumberFieldScrubArea`, not `NumberField`. External API: https://base-ui.com/react/components/number-field
## rating
**Required:** `rating` (number)
**Shape:**
```tsx
<Rating rating={4.5} showValue editable onRatingChange={setRating} />
```
**Gotcha:** supports decimals (partial stars). Pass `editable` + `onRatingChange` for interactive input; omit both for a read-only display.
## scrollspy
**Required:** `targetRef` (the scroll container ref)
**Shape:**
```tsx
<Scrollspy targetRef={containerRef}>
<a href="#s1" data-scrollspy-anchor="s1">Section 1</a>
<a href="#s2" data-scrollspy-anchor="s2">Section 2</a>
</Scrollspy>
<div ref={containerRef}>
<div id="s1">...</div>
<div id="s2">...</div>
</div>
```
**Gotcha:** each link's `data-scrollspy-anchor` must match a section `id`. `targetRef` is the scrollable container (defaults to the window).
## frame
**Required:** `Frame` > `FramePanel`
**Shape:**
```tsx
<Frame>
<FramePanel>
<FrameHeader>
<FrameTitle>Title</FrameTitle>
<FrameDescription>Description</FrameDescription>
</FrameHeader>
<div className="p-5">Content</div>
<FrameFooter>Footer</FrameFooter>
</FramePanel>
</Frame>
```
**Gotcha:** a structured card shell for tool-like surfaces. `stacked` connects multiple panels with shared borders; `dense` removes panel padding; radius via the `--frame-radius` CSS variable.
## icon-stack
**Required:** one child icon
**Shape:**
```tsx
<IconStack aria-hidden="true">
<InboxIcon className="size-4" />
</IconStack>
```
**Gotcha:** isometric layered artwork for empty states and illustrations; style the inner icon via its own `className`. Mark purely decorative stacks `aria-hidden="true"` and keep the real label in surrounding copy.
## icon-tile
**Required:** one child icon
**Shape:**
```tsx
<IconTile variant="elevated" size="lg">
<PackageIcon />
</IconTile>
```
**Gotcha:** the square container an icon sits in, so every list row, feature card and empty state shares one affordance. `variant`: `outline` (default) | `elevated` (muted fill, raised ring) | `soft` (tinted nested, tone from currentColor) | `solid` (filled tone, contrasting glyph) | `frame` (double container). `soft` and `solid` retint from one text color class (they default to `text-primary`). `size`: `xs | sm | default | lg | xl` (24/32/40/48/64px tile, glyph scales 12/14/16/20/24px). `radius`: `default | full`. Do not set a `size-*` class on the child icon unless you mean to override the tile's glyph size; recolor with `className` on the tile, not the icon.
## code-block
**Required:** `code` + `language`, or pre-highlighted `lines`.
**Shape:**
```tsx
<CodeBlock code={code} language="tsx" />
<CodeBlock code={code} language="tsx" showLineNumbers maxLines={20}>
<CodeBlockHeader>
<CodeBlockTitle>use-totals.ts</CodeBlockTitle>
<CodeBlockLanguage />
<CodeBlockCopyButton className="ml-auto" />
</CodeBlockHeader>
<CodeBlockExpandButton />
</CodeBlock>
```
**Gotcha:** the one-liner is already a complete block; every child (header, title, language label, copy button, wrap toggle, expand button, line actions) is optional chrome, at any depth. Shiki is the only npm dependency and loads lazily, one chunk per language, on first highlight; `lines` (from `highlightCode` in `code-block-highlight`, which is server-safe and has no `"use client"`) or `highlight={false}` loads nothing at all. `maxLines` caps the height AND marks the block collapsible, which is what makes `CodeBlockExpandButton` appear. Use `variant="ghost"` when the block sits inside a surface that already has a border.
## alert
**Required:** `Alert` > `AlertTitle`
**Shape:**
```tsx
<Alert variant="success">
<ShieldCheckIcon />
<AlertTitle>Security update</AlertTitle>
<AlertDescription>Enable two-factor authentication.</AlertDescription>
<AlertAction><Button size="xs">Update</Button></AlertAction>
</Alert>
```
**Gotcha:** shadcn-compatible API. `variant`: `default | destructive | info | success | warning | invert`. The non-default variants use ReUI extended color tokens (`--success`/`--info`/`--warning`/`--invert`), which the install adds. Defer generic alert rules to the shadcn skill.
## badge
**Required:** none (text child).
**Shape:**
```tsx
<Badge variant="success-light" size="sm">Success</Badge>
<Badge variant="outline" radius="full">Pill</Badge>
```
**Gotcha:** shadcn-compatible. Rich `variant` set (solid, `-outline`, `-light` per color), `size` `xs..xl`, `radius` `default | full`. Like `alert`, the color variants rely on ReUI extended tokens. Prefer `Badge` variants over raw color classes for statuses.
## base vs radix - write for the project's base
ReUI ships every component in two builds: `base` (Base UI) and `radix` (Radix UI). The install command and name are identical, and the CLI installs the build matching the project. But you must write/adapt code against the **right base**, because their APIs differ.
**Detect the base first.** Read `components.json` -> `style` and take the segment before the first `-`:
- `"style": "base-nova"` -> **Base UI**
- `"style": "radix-nova"` -> **Radix UI**
**Then use that base's API.** The deltas mirror shadcn's base-vs-radix split:
- Slot/composition: Base UI `render={<… />}` vs Radix `asChild`.
- `Select`: Base UI takes `items`; Radix uses `<SelectItem>` children.
- `ToggleGroup`: Base UI `multiple` boolean vs Radix `type="single" | "multiple"`.
The safest path is to **read the installed files and `c-*` examples** - they're already in your base, so reuse their wiring instead of guessing. When `get_component`'s inline `api` or an example shows the other base's shape, translate it to your base (or `validate_usage` to confirm). Defer the generic base/radix mechanics to the shadcn skill.
-45
View File
@@ -1,45 +0,0 @@
# Craft: make ReUI UI exceptional, not generic
ReUI items ship senior-designer quality. Your adaptation has to hold that bar, so the result reads like a real product surface a team would keep - not a wireframe an AI generated. Use these alongside the reuse rules in [adapting.md](./adapting.md).
## Have a point of view
Pick an emotional register before you compose - calm, operational, premium, editorial, dense, energetic - and let layout, spacing, surface treatment, and icon behavior all reinforce it. One or two memorable decisions and restraint everywhere else beats ten generic ones. UI with no point of view reads as generated.
## Brutally clear hierarchy
One focal point per card or panel: the dominant metric or task first, its label second, supporting detail third. The first thing the eye lands on should be the right thing; secondary text must read as secondary. Borders, separators, and surfaces do real work to create 2-3 information bands - don't flatten everything to equal weight.
## Spacing rhythm and deliberate density
Gaps are a signal, not a default. Keep them intentional and consistent within a family (`gap-1`/`gap-2` for tight operational rows, larger gaps for section breaks), and smaller within a group than between groups. Match the surrounding ReUI density; don't pad an operational surface like a marketing page, and don't drift density mid-section. The composition should still feel authored in grayscale.
## Cover the real states (the usual miss)
A surface isn't done at the happy path. Compose, and wire:
- **Empty** - a purposeful empty state (short message + the primary action), never a blank panel.
- **Loading** - a **skeleton** that matches the real layout, not a centered spinner.
- **Error** - an inline, recoverable error with a retry, announced via `role="status"`/`aria-live`.
Derive these from an element the block already has (don't invent parallel markup), or `get_examples` for a state-specific example.
## Responsive by default
Mobile-first, not mobile-afterthought. In constrained rows/cards/sidebars, put `min-w-0` on the shrinking container and `truncate` long single-line labels; protect the primary label's width and let secondary content compress. Reflow layouts (multi-column -> single column) rather than just shrinking them. Desktop and mobile should both look designed.
## Motion, subtly
Motion should clarify, not decorate. Use ReUI Motion Icons on primary actions for a subtle hover cue; keep transitions short (~200-300ms) with calm easing; prefer a skeleton pulse over a spinner. No bouncing, no gratuitous entrance animations on every element.
## Real, activated content
Use believable, typed data (realistic labels, counts, timestamps, statuses that map to a real workflow) - never lorem or abstract filler. Every visible control does something: no decorative buttons, fake tabs, meaningless toggles, or stats with no job. It must still hold with long names, empty values, and crowded data.
## Avoid the AI tells
These instantly read as generated - don't ship them: equal-weight card walls, empty gradients, repetitive padding everywhere, generic enterprise copy, ornamental icons, and number tiles that don't earn their place.
## The bar
Before you finish, ask: **would a product team keep this instead of replacing it? Does it still feel strong after swapping in real content?** If not, reuse the shipped ReUI design harder - don't restyle it into something new - then run the [quality.md](./quality.md) gates.
-39
View File
@@ -1,39 +0,0 @@
# Icons (ReUI delta over shadcn)
Follow the shadcn icon rules (use the project's configured `iconLibrary`, `data-icon` on icons inside `Button`, no sizing classes on icons inside components, pass icons as component objects not string keys). ReUI adds the following.
## Portable icons (library-agnostic)
ReUI components, examples, and blocks are authored to be icon-library-agnostic. When `iconLibrary` is set in `components.json`, the shadcn CLI installs each item's icons in **your** library automatically - you swap nothing. If an installed item's icons don't match your project (for example `iconLibrary` isn't set, so they came in from the item's demo library), change the **import source and component name** to your library, keeping the same icon-name semantics:
- `lucide` -> `lucide-react`
- `tabler` -> `@tabler/icons-react`
- `phosphor` -> `@phosphor-icons/react`
- `remix` -> `@remixicon/react`
- `hugeicons` -> `@hugeicons/react`
Don't assume `lucide-react`; read `iconLibrary` from `components.json`.
## Keep icons purposeful
Icons support the hierarchy, they don't replace it: keep them small, matched to the surrounding density, and decorative ones `aria-hidden="true"` (an icon-only control still needs an accessible label on the control). Don't add ornamental icons that do no job.
## Motion Icons (the `@reui/icons/...` set)
ReUI ships its own icon set in 4 styles (outline, solid, duotone, filled), each icon in two variants:
```bash
npx shadcn@latest add @reui/icons/default/<style>/<name> --yes # static
npx shadcn@latest add @reui/icons/animated/<style>/<name> --yes # hover-animated (motion/react)
```
Finding them via the MCP is free; installing requires an Ultimate license (`REUI_LICENSE_KEY`, see [cli.md](./cli.md)). Reach for a Motion Icon on a primary action when a subtle hover cue helps; keep motion restrained.
Finding icons:
- Several icons (the common case): **`search_icons(concepts[])`** - up to 24 concepts in one call, the best icons per concept with install commands. Pass `animated: true` to get only icons with a hover-animated Motion variant.
- One icon: `search` with `type: "icon"`.
- Icon results and `get_icon` carry `animated: true` and `installAnimated` when an animated variant exists - use those install strings, do not construct paths by hand.
- Every icon result carries a `previewUrl` (its live icon-category page) - **share it with the user** so they can SEE the icon before installing.
The `icon-stack` component composes multiple icons into a stacked display.
-22
View File
@@ -1,22 +0,0 @@
# Quality gates (security, accessibility, scroll)
These are the **done gate**, not a nice-to-have: before you call any ReUI work finished, call the MCP `get_audit_checklist` tool and pass every item below (plus the craft bar in [craft.md](./craft.md)). Then typecheck and lint.
## Security
- Never `dangerouslySetInnerHTML`. Render data as text/components.
- External links (`target="_blank"`) must always pair `rel="noopener noreferrer"`.
- No real PII, secrets, or tokens in demo or committed code. Remote media only from sources the project already allows.
## Accessibility
- Implicit list/card items that navigate get real anchors with a standard hover affordance.
- Icon-only or numeric buttons need an `aria-label`; decorative icons get `aria-hidden`.
- Every non-submit button is `type="button"`.
- Keyboard + focus: everything interactive is reachable in a sensible Tab order with a visible focus ring; layers (dialogs/sheets/menus) trap focus and close on `Escape`. ReUI components ship standard keyboard behavior - read each component's inline `api` rather than re-implementing it.
- Announce async UI: loading and error messages use `role="status"` / `aria-live` so they're not silent to screen readers.
## Scroll mechanics
- Make scroll regions with a parent-owned height: a `min-h-0` + flex chain down to the scroll container. Never guess a `max-h`.
- The scroll container owns `overflow-auto`; ancestors stay `min-h-0` so the height resolves.
-35
View File
@@ -1,35 +0,0 @@
# ReUI registry structure
ReUI is a shadcn-compatible registry with four entity types. **Examples and blocks are built FROM components** - reuse them, don't rebuild.
- **component** - one of the 22 ReUI building blocks with a real API (`data-grid`, `kanban`, `filters`, `date-selector`, `tree`, ...). Install directly (`@reui/data-grid`) or let it come in as a dependency of an example/block. Free. Read its API with `get_component(name)`.
- **example** - a free `c-*` single-pattern use-case of a component (`c-kanban-1`, `c-data-grid-3`). Install one and read it to copy real composition. Find a component's examples with `get_examples(name)`.
- **block** - a premium, full-page section that composes several components (`data-grid-2`, `pricing-page-1`). Pro or Ultimate license at install. Adapts to your active theme via semantic tokens.
- **icon** - Motion Icons in 4 styles (outline, solid, duotone, filled), static (`@reui/icons/default/<style>/<name>`) and hover-animated (`@reui/icons/animated/<style>/<name>`). Ultimate license at install. See [icons.md](./icons.md).
## The @reui registry
Install everything through the shadcn CLI: `npx shadcn@latest add @reui/<name> --yes`. The CLI reads the `@reui` registry from the project's `components.json`. Free items need only the plain string form:
```json
{ "registries": { "@reui": "https://reui.io/r/{style}/{name}.json" } }
```
Premium installs need the authenticated form + `REUI_LICENSE_KEY` in `.env.local` - see [cli.md](./cli.md). The MCP `get_project_context` tool returns the right config.
## Know your base: base or radix
ReUI ships every item in two builds - `base` (Base UI) and `radix` (Radix UI) - with mirrored names. The CLI installs the build matching your project automatically, but **you must write code against the right base's API**. Detect it from `components.json` -> `style`: the segment before the first `-` is the base (`base-nova` -> Base UI, `radix-nova` -> Radix UI). The installed files and `c-*` examples are already in your base - read them and adapt on that base. See [components.md](./components.md) for the API deltas.
**So the MCP's own `docsUrl` and `previewUrl` match your base**, send your `style` to the MCP: add `?style=<your components.json style>` to the ReUI MCP server URL (or set an `X-Reui-Style` header) in your MCP client config - set once, applies to every call. The MCP then resolves docs/preview links to YOUR library (`/docs/components/radix/...`, `/preview/radix/...` for a radix project) instead of the default base; `get_project_context` echoes back the style it currently sees so you can confirm it. Install commands are unaffected (the CLI already installs the right variant). If you notice the MCP returning `base` links for a `radix` project, tell the user to add `?style=` to the server URL.
Blocks adapt to your active theme through semantic tokens and CSS variables - change the theme and every block follows.
## Free vs premium
- **Free, no key:** the 22 components, all `c-*` examples, the ReUI MCP, and this skill.
- **Premium, license required at install:** blocks (Pro or Ultimate), Motion Icons and templates (Ultimate). Set `REUI_LICENSE_KEY` (see [cli.md](./cli.md)).
## Component API index
The canonical index of every component's API docs is **https://reui.io/llms.txt** (returned as `componentsApiUrl` in MCP results). Prefer the inline `api` from `get_component`; use the index/docs as the fallback.
-26
View File
@@ -1,26 +0,0 @@
# Styling (ReUI delta over shadcn)
Follow the shadcn skill for the generic rules (semantic colors not raw values, `gap-*` not `space-y-*`, `size-*`, `cn()`, no manual `dark:` overrides, no overlay `z-index`). This file is only the ReUI-specific additions.
## ReUI extended semantic tokens
ReUI adds semantic tokens beyond shadcn's base set. Use these instead of raw colors for status and emphasis:
- `--success` / `--success-foreground`
- `--info` / `--info-foreground`
- `--warning` / `--warning-foreground`
- `--destructive-foreground` (paired with shadcn's `--destructive`)
- `--invert` / `--invert-foreground` (inverted surfaces)
Use them as Tailwind utilities (`bg-success text-success-foreground`, `text-warning`, ...). They are defined in the project's global CSS and registered with Tailwind (`@theme inline` on v4). If a token is missing in the project, add it to the global CSS file (never a new file) following the same `name` / `name-foreground` convention, exactly as the shadcn customization rules describe.
**Incorrect:** `<span className="text-green-600">Active</span>`
**Correct:** `<Badge variant="success">Active</Badge>` or `<span className="text-success">Active</span>`
## Blocks follow your theme
When you install a block it adapts to your active theme through the semantic tokens above and the project's CSS variables. Don't hardcode style-specific values into installed block code and don't fork it to "restyle" - change the theme via the CSS variables / a preset and every block follows. Want a different look? `search` for a block whose design already fits instead of re-skinning one.
## Density and typography rhythm
ReUI operational UI usually feels dense, not airy. Keep the gap between a title and its supporting description tight by default (`gap-0.5`, `space-y-1`, or `space-y-px`), and smaller than the gap between sections. Match the surrounding ReUI density when you add rows or fields; do not pad operational surfaces like a marketing page.
-54
View File
@@ -1,54 +0,0 @@
# Workflow: find -> install -> read API -> adapt
The core ReUI loop. The MCP tells you what to install and gives you the API; the shadcn CLI installs it; you turn the installed files into correct, themed, data-wired code by **reuse**, not redesign.
## 1. Find (ReUI MCP `search` / `compose_page`)
**Full multi-section page ask?** Call `compose_page(intent, sections?)` FIRST, before searching block-by-block. It returns ordered sections, each with the best block for the intent (top pick + alternates); sections listed in `unavailableSections` have no real inventory - compose those from components, do not force a bad block.
For everything else, call `search` with the user's intent. Pass structured hints whenever you can infer them - you are an LLM, so do the parsing the server cannot:
- `type`: `"component"` (one of the 22 building blocks), `"example"` (a c-\* use-case), `"block"` (a full page/section), `"icon"`.
- `component`: the ReUI component the request implies (`"data-grid"`, `"kanban"`, ...).
- `category`, `features` (e.g. `["sortable","pagination"]`), `free`.
Example: "build a users management page with filters" -> `search({ query: "users management page with filters", type: "block", component: "data-grid", features: ["filters"] })`.
Each result has `install`, `previewUrl`, `docsUrl`, `componentsUsed`, `score`, `termCoverage`, and `whyMatch`. `score` is relative to the top hit (the top is ~100 by construction), not an absolute quality - compare results to each other, and show the user the top options if several score closely; do not silently guess. A low `termCoverage` means a weak match even with a high score - rephrase or widen.
**Always show the preview link.** Whenever you list or recommend items - from `search`, `search_icons`, `list_components`, `compose_page`, or a getter - include each item's `previewUrl` (a live preview page) so the user can SEE it before you install. Blocks and examples link to an individual live preview; icons and components to their live category/component page. This applies to every listing, not only a single pick.
## 2. Install (shadcn CLI)
Run the result's `install` command from the project root, non-interactively:
```bash
npx shadcn@latest add @reui/<name> --yes
```
The CLI reads `components.json`, installs the correct base+style variant, resolves `registryDependencies` (a block pulls in its components), installs npm deps, and rewrites aliases. Do not pass the base/style. See [cli.md](./cli.md).
## 3. Read the API (do not guess props)
Before writing code against any component an item uses:
1. The item's `componentDigests` already give a 1-line contract per component - often enough to wire it. For the full API, call **`get_component(names)`** with ALL of `componentsUsed` in ONE call (it accepts an array) and read each inline `api` - no web fetch. **Share the component's `docsUrl`** (its API documentation page) with the user whenever you work with that component's API, so they have the full reference; the `/llms.txt` index is a further fallback.
2. Call **`get_examples(name)`** for the free `c-*` examples of that component; install one and **read the added files** to copy the exact composition. This is the fastest correct path - the example shows real wiring you adapt, not invent.
3. About to write a prop you did not see in an `api` or installed file? Run **`validate_usage`** BEFORE writing the code - per-prop documented / notDocumented verdicts plus did-you-mean suggestions. notDocumented means read the API, not push on.
## 4. Adapt (reuse-first) - do not skip
Installing files is not the end, and redesigning them defeats the point. First note the project's **base** so you write the right API - read `components.json` -> `style` and take the segment before the first `-` (`base-nova` -> Base UI, `radix-nova` -> Radix UI), see [components.md](./components.md). After `add`:
1. **Read the added files**; keep the composition intact. For a block, verify the components are wired correctly (for `data-grid`: a `useTable({ features: dataGridFeatures, ... })` instance passed as `table`, `recordCount` set - see [components.md](./components.md)).
2. **Replace demo data with the user's real data** via typed structures (see [adapting.md](./adapting.md)).
3. **Fix icon imports** to the project's icon library (see [icons.md](./icons.md)).
4. **Align styling** to semantic tokens and the active theme - no raw colors (see [styling.md](./styling.md)).
5. **Validate before finalizing**: if your adaptation introduced components or props you did not read in an `api` or example, run `validate_usage` on them.
6. **Hit the craft bar** - clear hierarchy, deliberate density, the empty / loading / error states, subtle motion, and mobile-first responsiveness (see [craft.md](./craft.md)). Generic-looking output means you under-reused the design, not that it needs restyling.
7. **Pass the quality gates** (security, a11y, scroll) - call the MCP `get_audit_checklist` tool and clear every item (see [quality.md](./quality.md)).
8. **Typecheck / lint**.
## If no single block fits
Compose from components (`compose_page` tells you which sections have no block inventory via `unavailableSections`). `search` the components you need, read each `get_component` API, install a worked `get_examples` example for each, and assemble by adapting those examples. A block in the same category is a useful reference - install it and read its files to see how ReUI composes those components, then adapt.
-58
View File
@@ -1,58 +0,0 @@
# ReUI MCP: full reference
The ReUI MCP (`https://mcp.reui.io`, Streamable HTTP) is free to use but needs a ReUI account: on first use the agent signs in with ReUI (a free account is created if the user has none), so every request is tied to an account. Free covers components and examples; a Pro or Ultimate license unlocks premium blocks and Motion Icons and removes the daily request limit. It does **discovery + guidance** (search, inline APIs, page planning, validation) and never serves source; the shadcn CLI does **installation**, and the license key lives there (the `@reui` entry in `components.json`, backed by `.env.local`). Goal: from the user's intent to correct, themed, data-wired ReUI code in the **fewest tokens and calls**, with **no guessing**.
## Golden path (token-optimal - follow this order)
Most tasks need 2-4 MCP calls and ZERO web fetches:
1. **`search(query, ...hints)`** -> pick the top 1-3 results. Each result already carries `install`, `previewUrl`, `docsUrl`, `componentsUsed`, `score`, `whyMatch`. The payload is complete - do not call another tool just to "confirm" a result.
2. **`get_component([...componentsUsed])`** in ONE batched call (one name or an array of up to 20) -> read each inline `api`. This **replaces** fetching docs pages. Often skippable: search responses carry `componentDigests`, a compact API contract per referenced component.
3. **`get_examples(component)`** -> install ONE returned `c-*` example, read the added files, copy the composition.
4. **`get_install_command(item)`** only to validate a name you are unsure of (results already include `install`). Run the install with the shadcn CLI (`--yes`).
5. **`get_audit_checklist()`** before declaring done.
If you already know the exact item name, skip `search`. Everything else is situational.
## The 5 task-specific tools (when to reach for each)
- **`compose_page`** - BEFORE building any full page (dashboard, settings, billing, landing). Pass the intent (and optionally the sections you want); it returns ordered sections, each with the best premium block for the intent (top pick + alternates). Sections with no real inventory are listed honestly in `unavailableSections` - compose those from components instead of forcing a bad block.
- **`search_icons`** - whenever you need icons, especially several. Batch up to 24 concepts in one call; each concept returns its best icons with install commands. Pass `animated: true` to get only icons that have a hover-animated Motion variant.
- **`validate_usage`** - BEFORE writing code with component names or props you have not read in an inline `api` or an installed example. It checks planned names + props against the indexed API docs and registry item names; returns did-you-mean suggestions and per-prop documented / notDocumented verdicts. Deterministic, no inference - a notDocumented prop means stop and read the API, not push on.
- **`whats_new`** - when your registry knowledge might be stale (a name 404s, the user mentions an item you don't know). Returns items added/removed per build, newest first.
- **`report_issue`** - when an installed item is actually broken (bad source, wrong dependency, broken preview). Goes straight to the ReUI team; rate-limited 5/hour. Not for usage questions.
## All 19 tools
`search`, `get_block`, `get_example`, `get_icon`, `list_block_groups`, `list_block_categories`, `list_example_categories`, `list_icon_categories`, `list_components`, `get_component`, `get_examples`, `search_icons`, `compose_page`, `validate_usage`, `whats_new`, `report_issue`, `get_install_command`, `get_project_context`, `get_audit_checklist`. The MCP serves the full parameter schemas; do not guess parameters beyond them.
## Token + speed rules
- **Batch `get_component`** - ONE call with the whole `componentsUsed` array, never N calls. Skip it entirely when `componentDigests` already answers the question.
- **Read source by installing** - the MCP serves no source. To read or analyze an item's real code, install it with the shadcn CLI and open the local files. Learn an API from the inline `api` / `componentDigests`, never by reading raw source.
- **Infer `search` hints yourself** (`type`, `component`, `category`, `features`, `free`) - hints shrink the result set and the tokens. Keep `limit` low; one right result beats ten.
- Run independent calls (and the shadcn install) concurrently in one turn - serial tool calls are the main source of slowness.
- Don't repeat a search for the same intent; don't call `list_*` to "see everything" - `search` is the entry point, `list_*` is only for browsing a taxonomy the user explicitly wants to explore.
- Prefer `get_component`'s inline `api` over `docsUrl` / `/llms.txt`. Fetch a web page only as a last resort.
## Result shapes (so you don't re-fetch)
- `score` is 0-100 RELATIVE to the top hit (the top is ~100 by construction), not absolute - compare results to each other.
- `termCoverage` (0-1) is the share of the query the item matched - low means a weak match even if the score looks high; rephrase or widen the search.
- Each result carries `whyMatch`, `install`, docs/preview URLs, and a `free` flag; premium items carry `requiredPlan` (`"pro"` for blocks, `"ultimate"` for icons).
- `componentDigests` is a top-level map: a compact API contract per referenced component - often enough to wire an item without a `get_component` call.
- Icon results and `get_icon` include `animated: true` and `installAnimated` when a hover-animated Motion variant exists (animated: `@reui/icons/animated/<style>/<name>`; static: `@reui/icons/default/<style>/<name>`).
## Error playbook
- **401** - the MCP requires a signed-in ReUI account. The client prompts "Sign in with ReUI" (OAuth) on first use; a free account is created if needed. For headless/CI, pass a personal token (`reui_pat_...`, created at https://reui.io/account/mcp?ref=skill) as `Authorization: Bearer`.
- **403 / locked result** - a valid account but the plan does not cover the item: premium blocks need Pro, Motion Icons need Ultimate. Point to https://reui.io/pricing?ref=skill (upgrade). Free accounts still get all components + examples.
- **429** - rate limited (120 requests/min per IP); back off, honor `Retry-After`.
- **not found** (`found: false`) - use the returned `suggestions`, or `search`; check `whats_new` if you suspect a stale name. Never run a fabricated install command.
## Fallbacks
- No ReUI MCP: `npx shadcn@latest search @reui -q "..."` then `add` (generic, no scoring / inline API).
- The shadcn project's own MCP also works over the `@reui` registry: https://ui.shadcn.com/docs/mcp.
Per-agent MCP setup: https://reui.io/docs/mcp?ref=skill
@@ -1,63 +0,0 @@
# Локальные GeoLite2-базы (Country + ASN) для потоков по странам и ASN
## Контекст
Сейчас страна и ASN для netflow-потоков резолвятся через внешний RIPEstat API (`backend/src/services/traffic-flow-ripe.ts`): лимит 30 новых префиксов/мин, очередь на 90, кэш в PG `flow_ip_meta`. Новые IP «дозревают» с задержкой, IPv6 не покрывается (кэш индексируется только по IPv4). Локальные mmdb-базы дают мгновенный синхронный lookup всех IP без внешних вызовов.
Решения (подтверждены):
- Источник — **MaxMind GeoLite2 через P3TERX-зеркало**: `https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-Country.mmdb` и `.../GeoLite2-ASN.mmdb`. Без регистрации, ключей и tar-распаковки. Точность по стране у GeoLite2 и IPinfo паритетная (<1% ошибок у обоих, arXiv 2026); выбран P3TERX за надёжность зеркала (5.2k звёзд) и преемственность: текущий RIPE-путь и так читает GeoLite (`maxmind-geo-lite`), история в кэше остаётся консистентной.
- **RIPEstat остаётся fallback** (до первой загрузки баз / если lookup не дал результата).
- City-базу не качаем (lat/lng фронтенд не использует).
## Изменения
### 1. Зависимость
- `npm install -w mikrotik-manager-backend maxmind` — sync-чтение mmdb, встроенные TS-типы, без транзитивных зависимостей, Node 22 ок.
### 2. Новый сервис `backend/src/services/traffic-flow-geoip.ts`
(по конвенциям окружения traffic-flow-*: контракт → маршрут → сервис, без БД-логики в маршрутах)
- Каталог: `backend/storage/geoip/` (конвенция `storage/backups`), файлы `GeoLite2-Country.mmdb`, `GeoLite2-ASN.mmdb`.
- `initGeoip()` — открыть ридеры best-effort при старте (из `index.ts` рядом с `startTrafficFlowListener`), независимо от настроек автообновления: файлы есть — работают.
- `lookupGeoip(ip): FlowIpMeta | null` — синхронно: `country.iso_code` (fallback `registered_country.iso_code`) с валидацией `isIsoCountry`, ASN = `autonomous_system_number`, holder = `autonomous_system_organization`; приватные IP → negative-запись как в RIPE (`isNonPublicIp`); в PG не пишем (lookup и так быстрый). IPv6 поддержан ридером.
- `resolveFlowIp(ip)` — фасад: `lookupGeoip(ip) ?? lookupRipeCached(ip)`; главный экспорт для потребителей.
- `geoipStatus()` — loaded, даты сборки баз (метаданные mmdb). Тест-хук `setGeoipReadersForTests`. Смена ридеров после обновления — атомарная замена ссылок.
### 3. Коллектор `backend/src/services/geoip-update-collector.ts`
`collectGeoipUpdateOnce()` по образцу `certificate-renew-collector.ts`:
1. Conditional GET с ETag/If-None-Match из настроек → 304 = skip (фолбэк-сравнение: размер/содержимое).
2. Скачивание в `*.tmp` через глобальный `fetch` + AbortController с таймаутом (внешний HTTP из service-слоя — по правилу fastify-backend-drizzle).
3. Валидация: открыть ридер из tmp-файла, пробой 8.8.8.8 (страна US, ASN 15169).
4. `fs.rename` атомарная подмена, старый файл → `*.prev` (откат, если новый ридер не открылся).
5. Перезагрузка ридеров, статус в настройках; snapshot для `scheduler_runs` (checked/downloaded/skipped/bytes/error).
### 4. Планировщик (`backend/src/services/scheduler.ts`)
- `JOB_KEYS` += `geoip_update`; case в `runSchedulerJobBody`; блок в `refreshScheduler()` по образцу `certificates_renew`: интервал `Math.max(6ч, updateIntervalSec*1000)`, по умолчанию 7 дней (upstream обновляется еженедельно) + немедленный первый запуск при включённой настройке.
### 5. Схема и миграция
- `backend/src/db/schema.ts`: singleton `geoip_settings` — `enabled` (default true), `updateIntervalSec` (default 604800), `lastCheckAt`, `lastSuccessAt`, `lastError`, `countryBuildAt`, `asnBuildAt`, `etagsJson` (jsonb), `createdAt/updatedAt`.
- Миграция: `npm run db:generate` → файл в `backend/drizzle/`.
### 6. API + контракты
- `packages/contracts/src/geoip.ts`: zod-схемы настроек/статуса (все входы — Zod, по правилам проекта).
- Новый `backend/src/routes/geoip.ts`, регистрация в `index.ts` с prefix `/api`:
- `GET /api/geoip` — настройки + статус (ready, даты сборки, последняя проверка/ошибка);
- `PUT /api/geoip` — сохранить настройки, затем `refreshScheduler()`;
- `POST /api/geoip/update` — запустить загрузку сейчас (409, если уже идёт; флаг-гард как в коллекторах).
### 7. Интеграция в пайплайн (geoip-first, RIPE-fallback)
- `traffic-flow-engine.ts` (`queueParsedFlows`, ~строка 336): `lookupRipeCached` → `resolveFlowIp`. Логика misses не меняется: при готовом mmdb публичные IP (v4+v6) резолвятся сразу, очередь RIPE пустеет; до скачивания баз — прежнее поведение.
- Остальные вызовы `lookupRipeCached` → `resolveFlowIp` (grep: как минимум `traffic-flow-analytics.ts` ~258–271).
- `classifyFlowDst`/бренды не трогаем: holder из mmdb (org name) встаёт в существующие `HOLDER_BRANDS`-регулярки как есть.
### 8. Frontend (по next-shadcn-production / ui-guardian: только переиспользование)
- Секция «GeoIP-базы (GeoLite2)» внутри существующей `components/traffic/netflow-settings-panel.tsx`: статус (готово/не скачано, даты сборки Country/ASN, последняя проверка, ошибка), тумблер автообновления, интервал, кнопка «Обновить сейчас» с индикатором. Только уже используемые в панели примитивы (Switch/Button/поля) — никаких новых визуальных паттернов и Card-shell. API-клиент через существующие http-хелперы.
### 9. Хаускипинг, тесты, проверки
- `backend/.gitignore`: `storage/geoip/`.
- Тесты `backend/src/services/traffic-flow-geoip.test.ts` + скрипт `test:geoip` (по образцу `test:traffic-flow`): приоритет фасада (geoip hit → RIPE не зовётся; miss → fallback), negative на приватных IP, фильтрация EU/ZZ через `isIsoCountry`, коллектор с мокнутым fetch (304-skip, битый файл → подмены нет, `.prev` сохранён), dims по стране/ASN с засеянным ридером.
- Проверки после реализации (обязательно по правилам): типы/сборка бэка (`npm run build -w mikrotik-manager-backend`), типы фронта при правке UI (`npx tsc --noEmit`), `npm run test:geoip` и `test:traffic-flow`; предупреждения не игнорировать.
- Коммит: `feat(netflow): <subject по-русски>` — новая пользовательская фича (мгновенные страна/ASN в потоках), по commit-messages-ru.
- README: короткий раздел о GeoIP; примечание, что в Docker `storage/geoip` ephemeral без тома — базы перекачаются после пересоздания контейнера (~17 МБ); при желании смонтировать volume.
## Что это даёт
Страна и ASN появляются у потока мгновенно при ingest (включая IPv6), без ограничения скорости RIPE; dims `country`/`asn` в `flow_daily_dims`, аналитика (карта, топы, monthly) становятся полными сразу. Внешняя зависимость от stat.ripe.net остаётся только как fallback до первой загрузки баз.
@@ -1,46 +0,0 @@
# IKEv2/IPsec VPN: клиенты, IP-адреса и сертификаты — по образцу WireGuard
## Архитектура (как у WireGuard)
Роутер — источник истины: live-чтение через существующий REST-клиент (`mikrotik.ts`), мутации прямыми REST-вызовами, история через `config_revisions` (новая секция `"ipsec"`; `section` — TEXT без CHECK → **SQL-миграция не нужна**). Новых таблиц нет. Объекты, созданные мастером/при создании клиентов, помечаются managed-комментарием `mm-ipsec` (по образцу managed-markers).
## Модель RouterOS (что создаём/читаем)
Мастер инициализации сервера (per router): CA-сертификат (`/certificate add` + `sign`, key-usage=key-cert-sign,crl-sign), серверный сертификат (CN=адрес/домен, SAN, tls-server), `/ip/ipsec/peer` (passive, exchange-mode=ike2, certificate=серверный cert), `/ip/ipsec/profile`+`proposal` (aes-256/sha256/modp2048, pfs), `/ip pool` + `/ip/ipsec/mode-config` «VPN» (address-pool, dns), policy-template, и опциональное (default on, чекбокс) managed srcnat masquerade-правило «интернет клиентам».
Клиент = `/ip/ipsec/identity`: **Сертификат** (`/certificate add` CN=<имя> → `sign` ca=<CA> → `export-certificate type=pkcs12` → файл `.p12` скачивается с роутера → identity match-by=certificate) или **PSK** (auth-method=pre-shared-key, remote-id+secret). IP: «из пула» (общий mode-config) или статический (персональный `mc-<user>` mode-config с `address=x.x.x.x/32`). Онлайн — `/ip/ipsec/active-peers`.
Клиентские загрузки: `.p12` (cert+key+CA, passphrase) + strongSwan `.sswan` (генерация на бэкенде) + текстовая инструкция. Перекачка `.p12` — повторный export с новой passphrase (ключ остаётся на роутере).
## Бэкенд
1. **`mikrotik.ts`**: `downloadFile(name): Promise<Buffer>` — бинарно-безопасный GET `/rest/file/<name>` (с fallback `flash/<name>`, как у `uploadTextFile`; текущий конвейер парсит JSON как utf8 — нужен Buffer-режим); хелперы `signCertificate` (с поллингом готовности, sign небыстрый), `exportCertificatePkcs12(name, passphrase)`.
2. **`services/ipsec-config.ts`** (чистые, тестируемые): генератор `.sswan` и инструкции, поиск свободного IP в пуле, naming-конвенции (`ipsec-ca`, `ipsec-server`, `mc-<user>`, `ipsec-user-<name>`).
3. **`services/ipsec-ca.ts`**: `ensureCa/ensureServerCert/issueClientCert/exportClientP12` — add → sign (poll) → export → download.
4. **`services/ipsec-ros.ts`**: put/patch/delete для `/ip/ipsec/{peer,identity,mode-config,profile,proposal,policy}`, `/ip/pool`, NAT managed-правило.
5. **`services/ipsec-live.ts`**: `fetchIpsecState(server)` — параллельные GET peer/identity/mode-config/pool/active-peers/certificate (+какие из них наши по маркеру/имени) → DTO; `listIpsec()` fan-out с failures; `countIpsecClients()` для сайдбара.
6. **`entity-snapshots.ts`**: `canonicalIpsecSnapshot/parseIpsecSnapshot/planIpsecRestore` (секреты через `isHiddenSecret`); `CONFIG_SECTIONS` += `"ipsec"`; enum в `schema.ts` (миграции нет).
7. **`routes/ipsec.ts`**: `GET /ipsec` (+observed revision), `POST /ipsec/server/init`, `DELETE /ipsec/server/:serverId` (только managed-объекты), `POST /ipsec/users` (ответ включает одноразовый p12 base64), `PATCH /ipsec/users/:serverId/:rosId` (имя/статический IP/psk), `DELETE /ipsec/users/...` (identity + mc + опционально клиентский cert), `POST /ipsec/users/:serverId/:rosId/cert` (перекачка p12), `GET /ipsec/revisions` + `POST /ipsec/revisions/:id/restore` — всё по образцу `routes/wireguard.ts` (`captureAndAppendRevision`). Регистрация в `index.ts`; `/api/ipsec` в обе группы network-правил `permissions.ts`.
8. **Модуль Пользователи**: `InterfaceType` += `"ipsec"` (iface-type.ts, schema.ts enum — TEXT, без миграции); каталог интерфейсов дополняется IPsec-клиентами (identity name + CN в peerPublicKey/peerName) — привязка app-пользователя к VPN-клиенту.
## Контракт
`packages/contracts/src/ipsec.ts` (+`package.json` exports, `index.ts`): `ipsecPeerDto/identityDto/modeConfigDto/poolDto/activePeerDto/certInfoDto/serverSummaryDto`, list-response с failures, `initRequest` (CN/SAN, пул, DNS, NAT-чекбокс), `userCreateRequest` (имя, auth: certificate|psk, psk?, ip: static|pool, passphrase, daysValid), `certDownloadResponse {filename, contentB64, mime}`.
## Фронт
`app/(main)/ipsec/page.tsx` — «одно окно» по образцу WG-страницы: ServerRail + KPI (серверы IKEv2 / клиенты / онлайн / CA) + Tabs **«Клиенты»** (grid: имя, сервер, аутентификация, IP, онлайн, действия — скачать .p12/.sswan, изменить, удалить; Sheet создания клиента), **«Сервер»** (peer/pool/mode-config/certs + мастер инициализации Stepper + статус NAT-правила), **«CLI»** (шпаргалка). История/restore — `ConfigHistorySheet`. Компоненты `components/ipsec/*` на существующих DataGridShell/form-kit/Sheet; скачивание бинарного p12 — Blob из base64 (по образцу `downloadText`), `.sswan`/инструкция — через `CodeExportSheet`. Сайдбар «IPsec / IKEv2» в «Управление» + бейдж (`sidebar-counts.ts` + mock в `sidebar-badges.ts`) + command-palette. Mock-данные в `lib/data.ts` для демо-режима.
## Тесты и проверка
- `ipsec-config.test.ts` (чистые функции: sswan, свободный IP, naming); `entity-snapshots.test.ts` — `planIpsecRestore` + `opsTouchOnly(["/ip/ipsec", ...])`.
- `npm run build -w @mmapp/contracts`; `tsc` backend/root; `npm --prefix backend run test:config-sync && test:wireguard` + новые; eslint без новых ошибок.
- Демо-режим: визуальная проверка страницы в браузере (как в прошлый раз).
## Риски (проверить на живом роутере при внедрении)
- REST-скачивание файла `GET /rest/file/<name>` — еслиRouterOS отдаёт только метаданные, fallback: чтение `contents` маленьких PEM-файлов или сборка p12 из PEM-частей.
- Точные имена полей mode-config (`address` vs `address-pool`) сверить с живым GET и адаптировать.
- `/certificate/sign` — поллинг готовности с таймаутом ~30–60 с.
Объём большой; коммит/пуш — по готовности, отдельным `feat(ipsec): …`.
-2
View File
@@ -4,8 +4,6 @@
## Содержание ## Содержание
Разрабатываемый MCP для AI-клиентов: [настройка, возможности и текущие ограничения](docs/mcp.md), [полный план реализации](docs/mcp-implementation-plan.md).
1. [Состав монорепозитория](#состав-монорепозитория) 1. [Состав монорепозитория](#состав-монорепозитория)
2. [Архитектура](#архитектура) 2. [Архитектура](#архитектура)
3. [Локальная разработка](#локальная-разработка) 3. [Локальная разработка](#локальная-разработка)
-81
View File
@@ -1,81 +0,0 @@
# PostgreSQL connection string
DATABASE_URL=postgres://mmapp:mmapp@127.0.0.1:5432/mmapp
# Optional operator-approved request and CA for runtime PostgreSQL TLS and SQL identity checks.
# DATABASE_RUNTIME_REQUEST_FILE=/run/config/mmapp/approved-request.json
# DATABASE_RUNTIME_REQUEST_SHA256=<operator-approved SHA-256 of request bytes>
# DATABASE_RUNTIME_CA_FILE=/run/config/mmapp/postgresql-ca.pem
# Optional live operator activation directory (mount the directory read-only).
# DATABASE_RUNTIME_ACTIVATION_DIRECTORY=/var/lib/mmapp/source-activation
# DATABASE_RUNTIME_ACTIVATION_SHA256=<confirmed activation SHA-256>
# Optional operator-approved logical schema SHA-256 from db:inspect-schema.
# DATABASE_RUNTIME_SCHEMA_SHA256=<approved schema SHA-256>
# Restore-aware signed writer profile. Requires source activation, schema pin and restore checkpoint.
# DATABASE_RUNTIME_WRITER_APPROVAL_FILE=/run/config/mmapp/writer-approval.json
# DATABASE_RUNTIME_WRITER_APPROVAL_SHA256=<operator-approved SHA-256 of approval bytes>
# Protected exports use the approved capture role and require this private password file.
# The PostgreSQL TLS peer must present the approved self-signed non-CA leaf.
# DATABASE_ARCHIVE_PASSWORD_FILE=/run/secrets/mmapp_database_archive_password
# Optional owner connection URI file for restore and its schema migrations.
# DATABASE_RESTORE_URL_FILE=/run/secrets/mmapp_database_restore_url
# Opt in on every backend/CLI after provisioning the restore quarantine in mmapp_control.
# DATABASE_RESTORE_QUARANTINE=true
# Optional operator-approved pair for verifying one archive's restored data.
# Requires DATABASE_RESTORE_QUARANTINE and DATABASE_RESTORE_URL_FILE.
# DATABASE_RESTORE_ARCHIVE_SHA256=<approved archive SHA-256>
# DATABASE_RESTORE_DATA_SHA256=<approved data SHA-256>
# Optional checkpoint in an operator-managed directory outside the PostgreSQL and MCP state backups.
# Mount the whole directory read-only. Restore requires a staged pending checkpoint.
# DATABASE_RESTORE_CHECKPOINT_FILE=/run/config/mmapp-restore/database-restore-checkpoint.json
# Local socket served by the operator-side db:serve-restore-claims process.
# DATABASE_RESTORE_CHECKPOINT_CLAIM_SOCKET=/run/mmapp-restore/claims.sock
# SQLite file used only for one-shot boot ETL (empty PG + existing mikrotik.db)
DATABASE_PATH=./mikrotik.db
# SQLITE_IMPORT_FULL_HISTORY=true
# Port for the Fastify server
PORT=8000
# Allowed CORS origin (Next.js frontend)
CORS_ORIGIN=http://localhost:3000
# Portal SSO (false = open API for local/dev)
AUTH_REQUIRED=false
# Same HS256 secret as auth-portal JWT_SECRET when AUTH_REQUIRED=true
AUTH_JWT_SECRET=dev-secret-change-me
AUTH_ISSUER=https://auth.shnt.top
AUTH_PORTAL_URL=http://localhost:5175
# Optional portal -> MCP revocation webhook. Separate random secret, at least 32 characters.
# Portal must send signed events to /_internal/mcp/portal/revocations; see docs/mcp.md.
# MCP_PORTAL_WEBHOOK_SECRET=
# MCP_PUBLIC_URL=https://manager.example/mcp
# MCP_STATE_DIR=/var/lib/mikrotik-manager/mcp-state
# Optional separate, externally provisioned bandwidth control source. No automatic
# schema/key/identity creation. See docs/mcp-bandwidth-control-source.md.
# Use the restricted control runtime role, never the owner/application role.
# MMAPP_BANDWIDTH_CONTROL_URL=postgresql://bandwidth_runtime:change-me@control-db:5432/bandwidth_control
# MMAPP_BANDWIDTH_CONTROL_ATTESTATION_FILE=/etc/mmapp/control/source.json
# MMAPP_BANDWIDTH_CONTROL_TRUST_KEY_FILE=/etc/mmapp/control/issuer-public.pem
# SHA256 of the Ed25519 public SPKI DER, pinned outside the attestation itself.
# MMAPP_BANDWIDTH_CONTROL_TRUST_KEY_SHA256=
# MMAPP_BANDWIDTH_CONTROL_EVENT_KEY_FILE=/etc/mmapp/control/event.key
# Separate persistent registry issuer key (raw 32 bytes, different from event.key)
# and operator bootstrap signed by the pinned control-source Ed25519 issuer.
# These inputs grant no actor/network permissions and create no registry rows.
# MMAPP_BANDWIDTH_NODE_ISSUER_KEY_FILE=/etc/mmapp/control/node-issuer.key
# MMAPP_BANDWIDTH_NODE_ISSUER_KEY_SHA256=
# MMAPP_BANDWIDTH_NODE_BOOTSTRAP_FILE=/etc/mmapp/control/node-bootstrap.json
# Optional: enabled by the NetFlow agent Compose installer. Linux Unix socket only.
# FLOW_AGENT_SOCKET=/run/mm-netflow-host/agent.sock
# Optional UDP NetFlow v9/IPFIX template lifetime in milliseconds (restart required).
# Collector policy defaults to 90 minutes, not a RouterOS default. Set at least
# 3 times the longest configured exporter template refresh interval; data does
# not refresh templates. Missing/expired templates refuse decoding, not zero traffic.
# FLOW_TEMPLATE_LIFETIME_MS=5400000
# Optional NetFlow read admission: standard (default) or constrained.
# Constrained admits two responses (UI + AI), limits SQL sources to 2 MiB,
# cumulative sources to 4 MiB and encoded responses to 2 MiB; large reads refuse.
# This is not a complete installation memory limit. See docs/mcp-2gb-resource-acceptance.md.
# MM_FLOW_READ_RESOURCE_PROFILE=standard
@@ -1,8 +0,0 @@
BEGIN;
CREATE INDEX mcp_audit_routing_charge_profile_window
ON public.mcp_audit (connection_id, (payload -> 'anchor' ->> 'profileId'), created_at, id)
WHERE action = 'routing.charge.admitted';
INSERT INTO public.schema_migrations (id) VALUES ('0057_mcp_routing_charge_audit_index') ON CONFLICT (id) DO NOTHING;
COMMIT;
-8
View File
@@ -16,7 +16,6 @@
"test:traffic-flow:pg": "node scripts/test-mcp-postgres.mjs --flow-writes", "test:traffic-flow:pg": "node scripts/test-mcp-postgres.mjs --flow-writes",
"test:traffic-flow:quality:pg": "node scripts/test-mcp-postgres.mjs --flow-quality", "test:traffic-flow:quality:pg": "node scripts/test-mcp-postgres.mjs --flow-quality",
"test:traffic-flow:chr-evidence": "tsx --test src/test/traffic-flow-chr-capture-check.test.ts src/test/traffic-flow-chr-wire.test.ts src/test/traffic-flow-chr-target-harness.test.ts src/test/traffic-flow-chr-owned-sequence.test.ts src/test/traffic-flow-chr-reconcile.test.ts", "test:traffic-flow:chr-evidence": "tsx --test src/test/traffic-flow-chr-capture-check.test.ts src/test/traffic-flow-chr-wire.test.ts src/test/traffic-flow-chr-target-harness.test.ts src/test/traffic-flow-chr-owned-sequence.test.ts src/test/traffic-flow-chr-reconcile.test.ts",
"test:changed": "node scripts/test-changed.mjs",
"test:mcp:pg:all": "node scripts/test-mcp-postgres.mjs --all", "test:mcp:pg:all": "node scripts/test-mcp-postgres.mjs --all",
"mcp:stdio": "node dist/scripts/mcp-stdio.js", "mcp:stdio": "node dist/scripts/mcp-stdio.js",
"mcp:stdio:dev": "tsx src/scripts/mcp-stdio.ts", "mcp:stdio:dev": "tsx src/scripts/mcp-stdio.ts",
@@ -31,13 +30,6 @@
"start": "node dist/index.js", "start": "node dist/index.js",
"db:generate": "drizzle-kit generate", "db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate", "db:migrate": "drizzle-kit migrate",
"db:prepare": "tsx src/scripts/prepare-database.ts",
"db:complete-restore": "tsx src/scripts/complete-database-restore.ts",
"db:serve-restore-claims": "tsx src/scripts/serve-restore-checkpoint-claims.ts",
"db:seal-restore-checkpoint": "tsx src/scripts/seal-database-restore-checkpoint.ts",
"db:stage-restore-checkpoint": "tsx src/scripts/stage-database-restore-checkpoint.ts",
"db:inspect-restore-checkpoint": "tsx src/scripts/inspect-database-restore-checkpoint.ts",
"db:inspect-schema": "tsx src/scripts/inspect-database-schema.ts",
"db:studio": "drizzle-kit studio", "db:studio": "drizzle-kit studio",
"db:migrate-from-sqlite": "tsx src/scripts/migrate-sqlite-to-pg.ts", "db:migrate-from-sqlite": "tsx src/scripts/migrate-sqlite-to-pg.ts",
"facts:rebuild": "tsx src/scripts/rebuild-flow-facts.ts", "facts:rebuild": "tsx src/scripts/rebuild-flow-facts.ts",
@@ -1,72 +0,0 @@
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
import { cpSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const root = fileURLToPath(new URL('../../../', import.meta.url));
const baseRevision = 'bf3d50e1ff2ee3b5c9268991a255c3b49ce6378e';
const sha = bytes => createHash('sha256').update(bytes).digest('hex');
const require = (value, reason) => { if (!value) throw Error(reason); };
const git = (...args) => execFileSync('git', args, { cwd: root, stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 4 * 1024 * 1024 });
const fromRoot = filename => path.join(root, filename);
const readJson = bytes => JSON.parse(bytes.toString('utf8'));
const manifestPath = ['backend/package.json', 'packages/contracts/package.json'];
const runtimeKeys = ['type', 'exports', 'engines', 'dependencies', 'optionalDependencies', 'peerDependencies'];
const normalizeLock = bytes => bytes.toString('utf8').replace(/\r\n/g, '\n');
const sort = values => [...new Set(values)].sort();
function runtimePins() {
const lockFile = readFileSync(fromRoot('package-lock.json'));
require(normalizeLock(lockFile) === normalizeLock(git('show', baseRevision + ':package-lock.json')), 'Runtime lock differs from cached source');
const lock = readJson(lockFile).packages;
return manifestPath.flatMap(filename => {
const current = readJson(readFileSync(fromRoot(filename)));
const cached = readJson(git('show', baseRevision + ':' + filename));
for (const key of runtimeKeys) {
require(JSON.stringify(current[key]) === JSON.stringify(cached[key]), 'Runtime package manifest differs from cached source');
}
const workspace = path.posix.dirname(filename);
return sort(Object.keys(current.dependencies ?? {})).filter(name => name !== '@mmapp/contracts').map(name => {
const nested = workspace + '/node_modules/' + name, shared = 'node_modules/' + name;
const installed = lock[nested] ? nested : shared;
const version = lock[installed]?.version;
require(typeof version === 'string', 'Runtime dependency pin unavailable');
return { path: installed + '/package.json', name, version };
});
}).filter((item, index, all) => all.findIndex(other => other.path === item.path) === index);
}
function compile(project, outDir) {
execFileSync(process.execPath, [fromRoot('node_modules/typescript/bin/tsc'), '--project', fromRoot(project), '--outDir', outDir,
'--declaration', 'false', '--declarationMap', 'false', '--sourceMap', 'false', '--incremental', 'false'],
{ cwd: root, stdio: 'inherit' });
}
function files(directory, prefix = '') {
return readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)).flatMap(entry => {
const key = prefix + entry.name, item = path.join(directory, entry.name);
if (entry.isDirectory()) return files(item, key + '/');
require(entry.isFile(), 'Packet contains a nonregular entry');
const bytes = readFileSync(item);
return [{ path: key, size: bytes.length, sha256: sha(bytes) }];
});
}
const [runId, ...extra] = process.argv.slice(2);
require(/^[a-f0-9]{8}$/.test(runId ?? '') && !extra.length, 'Expected one eight-digit hexadecimal run ID');
const directory = fromRoot('tmp/mcp-netflow-smoke-packet-' + runId);
const dependencies = runtimePins();
mkdirSync(directory);
compile('packages/contracts/tsconfig.json', path.join(directory, 'contracts-dist'));
compile('backend/tsconfig.json', path.join(directory, 'dist'));
require(statSync(path.join(directory, 'dist/test/traffic-flow-chr-collector-bootstrap.js')).isFile(), 'Compiled collector entry unavailable');
cpSync(fromRoot('backend/drizzle'), path.join(directory, 'drizzle'), { recursive: true, errorOnExist: true });
const entries = files(directory);
require(entries.length > 0 && entries.length <= 4096 && entries.reduce((sum, entry) => sum + entry.size, 0) <= 128 * 1024 * 1024,
'Packet budget exceeded');
const body = Buffer.from(JSON.stringify({ format: 'mmapp-netflow-collector-smoke-v1', cachedSourceRevision: baseRevision, dependencies,
files: entries }, null, 2) + '\n');
writeFileSync(path.join(directory, 'manifest.json'), body, { flag: 'wx' });
console.log(JSON.stringify({ directory, manifestSha256: sha(body), files: entries.length, dependencies: dependencies.length,
collectorStarted: false, networkUsed: false }));
@@ -1,325 +0,0 @@
"""Fixed, opt-in local Docker owner for the disposable NetFlow collector."""
import argparse
import json
import os
import re
import secrets
import stat
import subprocess
import sys
from pathlib import Path
PG_IMAGE = 'postgres@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873'
NODE_IMAGE = 'git.shx.one/denozord/mikrotikmanager-backend-mcp-acceptance@sha256:151ab635caca5f2f6b50a9d0c27980a1559d7bd67f0e3a8d2b74cfa3f9ec6945'
CID = re.compile(r'[a-f0-9]{64}\Z')
RUN = re.compile(r'[a-f0-9]{8}\Z')
LABEL = 'io.mmapp.netflow.run'
ENV = {'PATH': '/usr/sbin:/usr/bin:/sbin:/bin', 'LC_ALL': 'C',
'HOME': '/nonexistent', 'DOCKER_CONFIG': '/nonexistent'}
def require(value, code):
if not value:
raise ValueError(code)
def root(run):
require(isinstance(run, str) and RUN.fullmatch(run), 'RUN_ID_REFUSED')
return '/tmp/mcp-netflow-' + run
def mounts(source, target, readonly=False):
return ['--mount', 'type=bind,src=' + source + ',dst=' + target + (',readonly' if readonly else '')]
def build_pg(run, uid, gid):
base = root(run)
require(type(uid) is int and 0 < uid <= 65534 and type(gid) is int and 0 < gid <= 65534, 'FIXTURE_ACCOUNT_REFUSED')
return [
'create', '--pull=never', '--name', 'mm-nf-pg-' + run,
'--label', LABEL + '=' + run, '--label', 'io.mmapp.netflow.role=postgres',
'--network=none', '--user', f'{uid}:{gid}', '--read-only',
'--cap-drop=ALL', '--security-opt=no-new-privileges',
'--memory=192m', '--memory-swap=192m',
'--tmpfs', '/tmp:rw,nosuid,nodev,noexec,mode=1777,size=32m',
'--tmpfs', '/var/run/postgresql:rw,nosuid,nodev,noexec,mode=1777,size=1m',
*mounts(base + '/pg', '/owned-pg'),
*mounts(base + '/pg-socket', base + '/pg-socket'),
*mounts(base + '/pg-password', '/run/mm-netflow-pg-secret', True),
'--env', 'PGDATA=/owned-pg/data', '--env', 'POSTGRES_USER=mcp_test',
'--env', 'POSTGRES_DB=mcp_netflow_' + run,
'--env', 'POSTGRES_PASSWORD_FILE=/run/mm-netflow-pg-secret',
'--env', 'POSTGRES_INITDB_ARGS=--auth-local=scram-sha-256 --auth-host=scram-sha-256',
PG_IMAGE, 'postgres', '-c', 'listen_addresses=', '-c', 'unix_socket_directories=' + base + '/pg-socket',
'-c', 'unix_socket_permissions=0700',
]
def build_node(run, uid, gid, anchor_id, port, namespace, host_namespace, packet_digest):
base = root(run)
require(type(uid) is int and 0 < uid <= 65534 and type(gid) is int and 0 < gid <= 65534, 'FIXTURE_ACCOUNT_REFUSED')
require(isinstance(anchor_id, str) and CID.fullmatch(anchor_id), 'ANCHOR_ID_REFUSED')
require(isinstance(packet_digest, str) and CID.fullmatch(packet_digest), 'PACKET_PIN_REFUSED')
require(type(port) is int and 49152 <= port <= 65535, 'COLLECTOR_PORT_REFUSED')
require(all(type(pair) in (tuple, list) and len(pair) == 2 and
all(type(value) is int and 0 < value < 2**64 for value in pair)
for pair in (namespace, host_namespace)) and list(namespace) != list(host_namespace), 'NAMESPACE_PINS_REFUSED')
return [
'create', '--pull=never', '--name', 'mm-nf-node-' + run,
'--label', LABEL + '=' + run, '--label', 'io.mmapp.netflow.role=collector',
'--network=container:' + anchor_id, '--user', f'{uid}:{gid}', '--read-only',
'--cap-drop=ALL', '--security-opt=no-new-privileges', '--interactive', '--tty',
'--memory=320m', '--memory-swap=320m',
'--tmpfs', '/tmp:rw,nosuid,nodev,noexec,mode=1777,size=32m',
*mounts(base + '/collector', base),
*mounts(base + '/pg-socket', base + '/pg-socket'),
*mounts(base + '/pg-password', '/run/mm-netflow-pg-secret', True),
*mounts(base + '/packet/dist', '/app/backend/dist', True),
*mounts(base + '/packet/drizzle', '/app/backend/drizzle', True),
*mounts(base + '/packet/contracts-dist', '/app/packages/contracts/dist', True),
*mounts(base + '/packet/manifest.json', '/run/mm-netflow-packet.json', True),
'--workdir', base, '--env', 'MMAPP_OWN_CHR_NETFLOW_COLLECTOR=true',
'--env', 'MMAPP_NETFLOW_PACKET_SHA256=' + packet_digest,
'--entrypoint', 'node', NODE_IMAGE,
'--max-old-space-size=160', '--max-semi-space-size=8',
'/app/backend/dist/test/traffic-flow-chr-collector-bootstrap.js',
'--container-collector-cycle', run, str(port), *map(str, namespace), *map(str, host_namespace),
]
def capture_args(run, first, second, port):
root(run)
require((first, second) == ('198.18.241.2', '198.18.241.9') and
type(port) is int and 49152 <= port <= 65535, 'CAPTURE_SCOPE_REFUSED')
return ['nsenter', '--net=/run/netns/nf' + run + 'nc', '--',
'tcpdump', '-n', '-p', '-U', '-i', 'nf' + run + 'cv', '-y', 'EN10MB', '-s', '4138',
'-c', '64', '-w', '-', 'udp and dst host 198.18.242.3 and dst port ' + str(port) +
' and (src host ' + first + ' or src host ' + second + ')']
def local_docker():
require(sys.platform == 'linux' and os.geteuid() == 0 and
os.environ.get('MMAPP_OWN_CHR_NETFLOW_CONTAINER') == 'true', 'DOCKER_OWNER_REFUSED')
socket = Path('/var/run/docker.sock')
require(stat.S_ISSOCK(socket.stat().st_mode), 'LOCAL_DOCKER_SOCKET_UNAVAILABLE')
choices = [Path(folder, 'docker') for folder in ('/usr/bin', '/usr/sbin', '/bin')]
binary = next((path.resolve(strict=True) for path in choices if path.is_file()), None)
require(binary is not None and binary.stat().st_uid == 0 and not binary.stat().st_mode & 0o022,
'DOCKER_BINARY_UNPINNED')
def call(*args):
result = subprocess.run([str(binary), '--host', 'unix:///var/run/docker.sock', *args],
env=ENV, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, timeout=30)
require(result.returncode == 0 and len(result.stdout) <= 256 * 1024, 'DOCKER_OUTCOME_UNCONFIRMED')
return result.stdout.decode('utf-8', 'strict').strip()
def attach(cid):
require(CID.fullmatch(cid) and sys.stdin.isatty() and sys.stdout.isatty(), 'EXACT_TTY_REFUSED')
return subprocess.run([str(binary), '--host', 'unix:///var/run/docker.sock', 'start', '--attach', '--interactive', cid],
env=ENV, timeout=330).returncode
return call, attach
def prepare_private_storage(run, uid, gid):
"""Create only new private bind sources. A retained fixture is never reused."""
require(sys.platform == 'linux' and os.geteuid() == 0 and
os.environ.get('MMAPP_OWN_CHR_NETFLOW_CONTAINER') == 'true', 'DOCKER_OWNER_REFUSED')
require(type(uid) is int and 0 < uid <= 65534 and type(gid) is int and 0 < gid <= 65534, 'FIXTURE_ACCOUNT_REFUSED')
base = Path(root(run))
info = base.lstat()
require(base.resolve(strict=True) == base and stat.S_ISDIR(info.st_mode) and info.st_uid == uid and
stat.S_IMODE(info.st_mode) == 0o700, 'FIXTURE_ROOT_REFUSED')
for name in ('pg', 'pg-socket', 'collector'):
directory = base / name
directory.mkdir(mode=0o700)
os.chown(directory, uid, gid, follow_symlinks=False)
fd = os.open(base / 'pg-password', os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o400)
with os.fdopen(fd, 'wb') as output:
output.write((secrets.token_hex(32) + '\n').encode('ascii'))
output.flush()
os.fsync(output.fileno())
os.fchown(output.fileno(), uid, gid)
class CollectorContainerOwner:
"""Outer Linux parent retains each acknowledged ID and journals it.
An unknown create/start/retire acknowledgement requires reconciliation.
It never searches by label, restarts, prunes, or removes a matched name.
"""
def __init__(self, run, docker, checkpoint):
root(run)
self.run, self.docker, self.checkpoint = run, docker, checkpoint
self.ids = {}
self.attempted = set()
self.verified_exits = {}
self.anchor_start = None
self.adopted = None
self.topology_retired = False
def image(self, reference):
rows = json.loads(self.docker('image', 'inspect', reference))
require(len(rows) == 1 and reference in rows[0]['RepoDigests'] and rows[0]['Os'] == 'linux' and
re.fullmatch(r'sha256:[a-f0-9]{64}', rows[0]['Id']), 'CACHED_IMAGE_UNPINNED')
return rows[0]['Id']
def inspect(self, cid, role, image_id, network):
require(CID.fullmatch(cid), 'CONTAINER_ID_UNPINNED')
rows = json.loads(self.docker('container', 'inspect', cid))
require(len(rows) == 1, 'CONTAINER_UNCONFIRMED')
row = rows[0]
labels = row['Config']['Labels']
memory = (192 if role == 'postgres' else 320) * 1024 * 1024
require(row['Id'] == cid and row['Image'] == image_id and labels[LABEL] == self.run and
labels['io.mmapp.netflow.role'] == role and row['HostConfig']['NetworkMode'] == network and
not row['HostConfig']['Privileged'] and not row['HostConfig']['PortBindings'] and
row['HostConfig']['ReadonlyRootfs'] is True and row['HostConfig']['CapDrop'] == ['ALL'] and
not row['HostConfig']['CapAdd'] and row['HostConfig']['Memory'] == memory and
row['HostConfig']['MemorySwap'] == memory, 'CONTAINER_SCOPE_DRIFT')
return row['State']
def _create(self, role, argv, reference, network):
require(role not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
self.attempted.add(role)
image_id = self.image(reference)
self.checkpoint(role, 'create-intent', None)
cid = self.docker(*argv)
require(CID.fullmatch(cid), 'CREATE_ACK_UNCONFIRMED')
self.checkpoint(role, 'create-acknowledged', cid)
state = self.inspect(cid, role, image_id, network)
require(state['Status'] == 'created' and state['Running'] is False, 'CREATED_STATE_UNCONFIRMED')
self.ids[role] = (cid, image_id, network)
self.checkpoint(role, 'create-verified', cid)
return cid
def create_anchor(self, uid, gid):
return self._create('postgres', build_pg(self.run, uid, gid), PG_IMAGE, 'none')
def create_collector(self, uid, gid, port, namespace, host_namespace, packet_digest, assert_packet):
anchor_id, image_id, network = self.ids['postgres']
anchor = self.inspect(anchor_id, 'postgres', image_id, network)
require(anchor['Running'] is True and anchor['Pid'] > 0, 'ANCHOR_PID_UNCONFIRMED')
require(self.adopted is not None, 'C_ADOPTION_UNCONFIRMED')
adopted, read_mount = self.adopted
require(adopted == self.anchor_namespace() and adopted['namespace'] == list(namespace) and
adopted['hostNamespace'] == list(host_namespace) and read_mount() == adopted['namespace'],
'C_ADOPTION_DRIFT')
assert_packet()
return self._create('collector', build_node(self.run, uid, gid, anchor_id, port, namespace, host_namespace, packet_digest),
NODE_IMAGE, 'container:' + anchor_id)
def start_anchor(self):
cid, image_id, network = self.ids['postgres']
require('postgres-start' not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
self.attempted.add('postgres-start')
self.checkpoint('postgres', 'start-intent', cid)
require(self.docker('start', cid) == cid, 'START_ACK_UNCONFIRMED')
state = self.inspect(cid, 'postgres', image_id, network)
require(state['Running'] is True and type(state['Pid']) is int and state['Pid'] > 0 and
re.fullmatch(r'\d{4}-\d{2}-\d{2}T[0-9:.]+Z', state['StartedAt']), 'ANCHOR_PID_UNCONFIRMED')
self.checkpoint('postgres', 'start-verified', cid)
self.anchor_start = (state['Pid'], state['StartedAt'])
return state['Pid']
def anchor_namespace(self):
"""Return live anchor and host netns pins for the topology owner."""
require(sys.platform == 'linux' and os.geteuid() == 0, 'LINUX_OWNER_REQUIRED')
cid, image_id, network = self.ids['postgres']
state = self.inspect(cid, 'postgres', image_id, network)
pid = state['Pid']
started_at = state['StartedAt']
require(state['Running'] is True and self.anchor_start == (pid, started_at), 'ANCHOR_PID_UNCONFIRMED')
pin = Path('/proc', str(pid), 'ns/net').stat()
host = Path('/proc/self/ns/net').stat()
namespace, host_namespace = [pin.st_dev, pin.st_ino], [host.st_dev, host.st_ino]
latest = self.inspect(cid, 'postgres', image_id, network)
require(latest['Running'] is True and latest['Pid'] == pid and latest['StartedAt'] == started_at and
namespace != host_namespace,
'ANCHOR_NAMESPACE_DRIFT')
return {'containerId': cid, 'pid': pid, 'startedAt': started_at,
'namespace': namespace, 'hostNamespace': host_namespace}
def create_linux_topology(self, executor, backend, journal, planned, pins):
"""Bridge the exact live anchor into the existing guarded Linux owner."""
name = 'nf' + self.run + 'nc'
require(pins['nonce'][:8] == self.run and planned['names']['C'] == name, 'TOPOLOGY_RUN_DIFFERS')
require('topology-adopt' not in self.attempted, 'C_ADOPTION_REPLAY_REFUSED')
self.attempted.add('topology-adopt')
result = executor(backend, journal, planned, pins, read_collector_anchor=self.anchor_namespace)
adopted = journal.value['collectorAnchor']
require(adopted == self.anchor_namespace() and backend.namespace(name) == adopted['namespace'],
'C_ADOPTION_DRIFT')
self.adopted = (adopted, lambda: backend.namespace(name))
return result
def assert_linux_anchor_exited(self, recorded):
"""Validate the journalled container and its exact exit before C cleanup."""
cid, image_id, network = self.ids['postgres']
require(recorded['containerId'] == cid and 'postgres' in self.verified_exits and
('collector' not in self.ids or 'collector' in self.verified_exits), 'ANCHOR_EXIT_NOT_OWNED')
state = self.inspect(cid, 'postgres', image_id, network)
require(state['Running'] is False and state['Status'] == 'exited' and state['Pid'] == 0 and
state['StartedAt'] == recorded['startedAt'] == self.verified_exits['postgres'], 'ANCHOR_EXIT_UNCONFIRMED')
def cleanup_linux_topology(self, executor, backend, journal, planned):
require(self.adopted is not None and self.adopted[0] == journal.value.get('collectorAnchor') and
backend.root_baseline() == journal.value.get('rootBaseline'), 'TOPOLOGY_CLEANUP_SCOPE_DRIFT')
executor(backend, journal, planned, assert_collector_anchor_exited=self.assert_linux_anchor_exited)
require(journal.value['state'] == 'retired' and backend.root_baseline() == journal.value['rootBaseline'],
'TOPOLOGY_CLEANUP_UNCONFIRMED')
self.topology_retired = True
def start_collector(self, attach):
cid, image_id, network = self.ids['collector']
require('collector-start' not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
require(self.adopted is not None and self.adopted[0] == self.anchor_namespace() and
self.adopted[1]() == self.adopted[0]['namespace'], 'C_ADOPTION_DRIFT')
self.attempted.add('collector-start')
self.checkpoint('collector', 'start-intent', cid)
status = attach(cid)
state = self.inspect(cid, 'collector', image_id, network)
require(state['Running'] is False and state['Status'] == 'exited' and state['Pid'] == 0 and
state['ExitCode'] == status and re.fullmatch(r'\d{4}-\d{2}-\d{2}T[0-9:.]+Z', state['StartedAt']),
'COLLECTOR_EXIT_UNCONFIRMED')
self.checkpoint('collector', 'exit-verified', cid)
self.verified_exits['collector'] = state['StartedAt']
require(status == 0, 'COLLECTOR_RUN_REFUSED')
def stop_anchor(self, assert_restored):
cid, image_id, network = self.ids['postgres']
require('postgres-stop' not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
require('collector' not in self.ids or 'collector' in self.verified_exits, 'COLLECTOR_EXIT_UNCONFIRMED')
self.attempted.add('postgres-stop')
assert_restored()
self.checkpoint('postgres', 'stop-intent', cid)
current = self.inspect(cid, 'postgres', image_id, network)
require(current['Running'] is True and self.anchor_start == (current['Pid'], current['StartedAt']), 'ANCHOR_START_DRIFT')
require(self.docker('stop', '--time', '15', cid) == cid, 'STOP_ACK_UNCONFIRMED')
state = self.inspect(cid, 'postgres', image_id, network)
require(state['Running'] is False and state['Status'] == 'exited' and state['Pid'] == 0 and
state['StartedAt'] == self.anchor_start[1] and state['ExitCode'] == 0, 'ANCHOR_EXIT_UNCONFIRMED')
self.checkpoint('postgres', 'exit-verified', cid)
self.verified_exits['postgres'] = state['StartedAt']
def retire_exited(self, role):
cid, image_id, network = self.ids[role]
operation = role + '-retire'
require(operation not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
require(role != 'postgres' or self.adopted is None or self.topology_retired, 'C_RETIREMENT_UNCONFIRMED')
self.attempted.add(operation)
state = self.inspect(cid, role, image_id, network)
require(role in self.verified_exits and state['Running'] is False and state['Status'] == 'exited' and
state['Pid'] == 0 and state['StartedAt'] == self.verified_exits[role], 'EXIT_NOT_CONFIRMED')
self.checkpoint(role, 'retire-intent', cid)
require(self.docker('rm', cid) == cid, 'RETIRE_ACK_UNCONFIRMED')
self.checkpoint(role, 'retire-acknowledged', cid)
if __name__ == '__main__':
cli = argparse.ArgumentParser()
cli.add_argument('--check-only', required=True, metavar='RUN_ID')
args = cli.parse_args()
print(json.dumps({'fixtureRoot': root(args.check_only), 'postgresImage': PG_IMAGE, 'nodeImage': NODE_IMAGE,
'requiresOwnedCollectorNamespace': True, 'dockerUsed': False, 'networkUsed': False}))
@@ -1,143 +0,0 @@
import json
import runpy
import unittest
from pathlib import Path
owner = runpy.run_path(str(Path(__file__).with_name('chr-netflow-collector-container.py')))
RUN = 'a012bcde'
PG_ID = 'a' * 64
NODE_ID = 'b' * 64
PACKET = 'd' * 64
IMAGE_ID = 'sha256:' + 'c' * 64
STARTED = '2026-11-04T12:00:00.000Z'
class DockerFixture:
def __init__(self):
self.calls = []
self.records = {}
self.lose_create = False
def __call__(self, *args):
self.calls.append(args)
if args[:2] == ('image', 'inspect'):
return json.dumps([{'Id': IMAGE_ID, 'Os': 'linux', 'RepoDigests': [args[2]]}])
if args[0] == 'create':
if self.lose_create:
raise RuntimeError('lost ACK')
pg = '--network=none' in args
role, cid = ('postgres', PG_ID) if pg else ('collector', NODE_ID)
self.records[cid] = {'Id': cid, 'Image': IMAGE_ID, 'Config': {'Labels': {
owner['LABEL']: RUN, 'io.mmapp.netflow.role': role}},
'HostConfig': {'NetworkMode': 'none' if pg else 'container:' + PG_ID, 'Privileged': False,
'PortBindings': None, 'ReadonlyRootfs': True, 'CapDrop': ['ALL'], 'CapAdd': None,
'Memory': (192 if pg else 320) * 1024 * 1024,
'MemorySwap': (192 if pg else 320) * 1024 * 1024},
'State': {'Running': False, 'Status': 'created', 'Pid': 0}}
return cid
cid = args[-1]
if args[:2] == ('container', 'inspect'):
return json.dumps([self.records[cid]])
if args[0] == 'start':
self.records[cid]['State'] = {'Running': True, 'Status': 'running', 'Pid': 42, 'StartedAt': STARTED}
elif args[0] == 'stop':
self.records[cid]['State'] = {'Running': False, 'Status': 'exited', 'Pid': 0, 'StartedAt': STARTED, 'ExitCode': 0}
elif args[0] == 'rm':
del self.records[cid]
else:
raise AssertionError(args)
return cid
class TestCollectorLauncher(unittest.TestCase):
def test_exact_images_no_pull_no_privilege_and_fixed_namespace(self):
pg = owner['build_pg'](RUN, 1000, 1000)
node = owner['build_node'](RUN, 1000, 1000, PG_ID, 50100, (4, 5), (4, 6), PACKET)
self.assertIn('--network=none', pg)
self.assertIn('--network=container:' + PG_ID, node)
self.assertIn('listen_addresses=', pg)
for argv in (pg, node):
self.assertIn('--pull=never', argv)
self.assertIn('--cap-drop=ALL', argv)
self.assertNotIn('--privileged', argv)
self.assertNotIn('/var/run/docker.sock', ' '.join(argv))
self.assertNotIn('POSTGRES_PASSWORD=', ' '.join(pg))
with self.assertRaisesRegex(ValueError, 'NAMESPACE_PINS'):
owner['build_node'](RUN, 1000, 1000, PG_ID, 50100, (4, 5), (4, 5), PACKET)
self.assertIn('EN10MB', owner['capture_args'](RUN, '198.18.241.2', '198.18.241.9', 50100))
def test_one_shot_ack_exact_ids_and_verified_cleanup(self):
docker, journal = DockerFixture(), []
launcher = owner['CollectorContainerOwner'](RUN, docker, lambda *entry: journal.append(entry))
self.assertEqual(launcher.create_anchor(1000, 1000), PG_ID)
self.assertEqual(launcher.start_anchor(), 42)
with self.assertRaisesRegex(ValueError, 'ADOPTION_UNCONFIRMED'):
launcher.create_collector(1000, 1000, 50100, (4, 5), (4, 6), PACKET, lambda: None)
anchor = {'containerId': PG_ID, 'pid': 42, 'startedAt': STARTED, 'namespace': [4, 5], 'hostNamespace': [4, 6]}
launcher.anchor_namespace = lambda: anchor
name = 'nf' + RUN + 'nc'
class Topology:
def namespace(self, target):
return [4, 5] if target == name else None
def root_baseline(self):
return 'root-baseline'
class Journal:
value = {}
def provision(backend, journal, planned, pins, read_collector_anchor):
journal.value.update(collectorAnchor=read_collector_anchor(), rootBaseline='root-baseline', state='created')
return {'ns:' + name: backend.namespace(name)}
def cleanup(backend, journal, planned, assert_collector_anchor_exited):
assert_collector_anchor_exited(journal.value['collectorAnchor'])
journal.value['state'] = 'retired'
topology, topology_journal, planned = Topology(), Journal(), {'names': {'C': name}}
launcher.create_linux_topology(provision, topology, topology_journal, planned, {'nonce': RUN + '0' * 24})
self.assertEqual(launcher.create_collector(1000, 1000, 50100, (4, 5), (4, 6), PACKET, lambda: None), NODE_ID)
with self.assertRaisesRegex(ValueError, 'EXIT_NOT_OWNED'):
launcher.cleanup_linux_topology(cleanup, topology, topology_journal, planned)
def attach(cid):
self.assertEqual(cid, NODE_ID)
docker.records[cid]['State'] = {'Running': False, 'Status': 'exited', 'Pid': 0, 'ExitCode': 0, 'StartedAt': STARTED}
return 0
launcher.start_collector(attach)
launcher.retire_exited('collector')
launcher.stop_anchor(lambda: None)
with self.assertRaisesRegex(ValueError, 'C_RETIREMENT_UNCONFIRMED'):
launcher.retire_exited('postgres')
launcher.cleanup_linux_topology(cleanup, topology, topology_journal, planned)
launcher.retire_exited('postgres')
self.assertEqual(docker.records, {})
self.assertIn(('collector', 'exit-verified', NODE_ID), journal)
self.assertEqual([call for call in docker.calls if call[0] == 'rm'], [('rm', NODE_ID), ('rm', PG_ID)])
def test_lost_create_ack_is_not_replayed_or_cleaned_by_name(self):
docker, journal = DockerFixture(), []
docker.lose_create = True
launcher = owner['CollectorContainerOwner'](RUN, docker, lambda *entry: journal.append(entry))
with self.assertRaisesRegex(RuntimeError, 'lost ACK'):
launcher.create_anchor(1000, 1000)
with self.assertRaisesRegex(ValueError, 'REPLAY'):
launcher.create_anchor(1000, 1000)
self.assertEqual(journal, [('postgres', 'create-intent', None)])
self.assertEqual([call[0] for call in docker.calls], ['image', 'create'])
def test_anchor_process_replaced_under_same_container_id_is_never_stopped(self):
docker = DockerFixture()
launcher = owner['CollectorContainerOwner'](RUN, docker, lambda *_: None)
launcher.create_anchor(1000, 1000)
launcher.start_anchor()
docker.records[PG_ID]['State']['StartedAt'] = '2026-11-04T13:00:00.000Z'
with self.assertRaisesRegex(ValueError, 'ANCHOR_START_DRIFT'):
launcher.stop_anchor(lambda: None)
self.assertFalse(any(call[0] == 'stop' for call in docker.calls))
if __name__ == '__main__':
unittest.main()
@@ -1,242 +0,0 @@
"""Disposable collector-only cycle. No RouterOS clients or capture are opened."""
import argparse
import gzip
import hashlib
import io
import json
import os
import re
import runpy
import stat
import subprocess
import sys
import tarfile
import time
from pathlib import Path
owner = runpy.run_path(str(Path(__file__).with_name('chr-netflow-collector-container.py')))
require = owner['require']
MAX_PACKET = 128 * 1024 * 1024
PACKET_PATH = re.compile(r'(?:dist|drizzle|contracts-dist)/(?:[A-Za-z0-9_.-]+/)*[A-Za-z0-9_.-]+\Z')
def verify_packet(directory, digest, require_root=False):
require(re.fullmatch(r'[a-f0-9]{64}', digest), 'PACKET_DIGEST_REFUSED')
require(directory.resolve(strict=True) == directory and directory.is_dir(), 'PACKET_DIRECTORY_REFUSED')
manifest = directory / 'manifest.json'
info = manifest.lstat()
require(stat.S_ISREG(info.st_mode) and 0 < info.st_size <= 1048576, 'PACKET_MANIFEST_REFUSED')
raw = manifest.read_bytes()
require(hashlib.sha256(raw).hexdigest() == digest, 'PACKET_MANIFEST_DRIFT')
value = json.loads(raw)
require(value.get('format') == 'mmapp-netflow-collector-smoke-v1' and
value.get('cachedSourceRevision') == 'bf3d50e1ff2ee3b5c9268991a255c3b49ce6378e' and
type(value.get('files')) is list and 1 <= len(value['files']) <= 4096, 'PACKET_MANIFEST_REFUSED')
total, listed = 0, {'manifest.json'}
for row in value['files']:
relative = row.get('path', '')
require(PACKET_PATH.fullmatch(relative) and
not any(part in ('.', '..') for part in relative.split('/')) and relative not in listed and
type(row.get('size')) is int and 0 < row['size'] <= 2 * 1024 * 1024 and
re.fullmatch(r'[a-f0-9]{64}', row.get('sha256', '')), 'PACKET_FILE_REFUSED')
item = directory / relative
info = item.lstat()
total += row['size']
require(total <= MAX_PACKET and stat.S_ISREG(info.st_mode) and item.resolve(strict=True) == item and
info.st_size == row['size'] and hashlib.sha256(item.read_bytes()).hexdigest() == row['sha256'], 'PACKET_FILE_DRIFT')
listed.add(relative)
observed = set()
for folder, directories, filenames in os.walk(directory, followlinks=False):
parent = Path(folder)
for entry in [parent, *(parent / name for name in directories + filenames)]:
info = entry.lstat()
require(not stat.S_ISLNK(info.st_mode) and
(not require_root or info.st_uid == 0 and not info.st_mode & 0o022), 'PACKET_OWNERSHIP_REFUSED')
observed.update((parent / filename).relative_to(directory).as_posix() for filename in filenames)
require(observed == listed and 'dist/test/traffic-flow-chr-collector-bootstrap.js' in listed, 'PACKET_FILE_SET_DRIFT')
return {'files': len(listed) - 1, 'bytes': total, 'manifestSha256': digest}
def pack(directory, digest):
match = re.fullmatch(r'mcp-netflow-smoke-packet-([a-f0-9]{8})', directory.name)
require(match is not None, 'PACKET_DIRECTORY_REFUSED')
verified = verify_packet(directory, digest)
archive = directory.parent / ('mcp-netflow-collector-smoke-' + match[1] + '.tar.gz')
manifest = json.loads((directory / 'manifest.json').read_bytes())
script_root = Path(__file__).resolve().parent
scripts = ('chr-netflow-collector-smoke.py', 'chr-netflow-collector-container.py')
with archive.open('xb') as destination, gzip.GzipFile(fileobj=destination, mode='wb', filename='', mtime=0) as compressed:
with tarfile.open(fileobj=compressed, mode='w|', format=tarfile.USTAR_FORMAT) as tar:
def add(name, body):
item = tarfile.TarInfo(name)
item.mode = 0o644
item.size = len(body)
tar.addfile(item, io.BytesIO(body))
for name in scripts:
add(name, (script_root / name).read_bytes())
add('packet/manifest.json', (directory / 'manifest.json').read_bytes())
for row in manifest['files']:
add('packet/' + row['path'], (directory / row['path']).read_bytes())
with archive.open('rb') as stream:
archive_sha = hashlib.file_digest(stream, 'sha256').hexdigest()
return {**verified, 'archive': str(archive), 'archiveSha256': archive_sha, 'dockerUsed': False, 'networkUsed': False}
class SmokeNamespace:
"""Only the C alias and one local dummy address, never the transit topology."""
def __init__(self, run, ip, namespace, checkpoint):
self.name, self.device = 'nf' + run + 'nc', 'nf' + run + 'cv'
self.ip, self.namespace, self.checkpoint = ip, namespace, checkpoint
self.anchor = None
def create(self, read_anchor):
require(self.anchor is None and self.namespace(self.name) is None, 'SMOKE_NAMESPACE_PRESENT')
anchor = read_anchor()
require(anchor['namespace'] != anchor['hostNamespace'] and read_anchor() == anchor, 'SMOKE_ANCHOR_DRIFT')
self.checkpoint('smoke', 'attach-intent', anchor)
self.ip('netns', 'attach', self.name, str(anchor['pid']))
require(self.namespace(self.name) == anchor['namespace'] and read_anchor() == anchor, 'SMOKE_ATTACH_UNCONFIRMED')
self.checkpoint('smoke', 'attach-verified', anchor)
self.anchor = anchor
def current():
require(self.namespace(self.name) == anchor['namespace'] and read_anchor() == anchor, 'SMOKE_ANCHOR_DRIFT')
links = json.loads(self.ip('-n', self.name, '-j', 'link', 'show'))
require([item['ifname'] for item in links] == ['lo'], 'SMOKE_NAMESPACE_NOT_EMPTY')
self.checkpoint('smoke', 'dummy-intent', self.device)
current()
self.ip('-n', self.name, 'link', 'add', self.device, 'type', 'dummy')
current()
created = json.loads(self.ip('-n', self.name, '-j', '-d', 'link', 'show', 'dev', self.device))
require(len(created) == 1 and created[0]['ifname'] == self.device and
created[0]['linkinfo']['info_kind'] == 'dummy' and created[0]['ifindex'] > 0, 'SMOKE_DUMMY_UNCONFIRMED')
self.ip('-n', self.name, 'address', 'add', '198.18.242.3/32', 'dev', self.device)
current()
self.ip('-n', self.name, 'link', 'set', self.device, 'up')
rows = json.loads(self.ip('-n', self.name, '-j', '-d', 'address', 'show', 'dev', self.device))
routes = json.loads(self.ip('-n', self.name, '-j', '-4', 'route', 'show', 'table', 'all'))
require(len(rows) == 1 and rows[0]['ifname'] == self.device and rows[0]['linkinfo']['info_kind'] == 'dummy' and
rows[0]['ifindex'] == created[0]['ifindex'] and
'UP' in rows[0]['flags'] and any(row.get('local') == '198.18.242.3' and row.get('prefixlen') == 32
for row in rows[0]['addr_info']) and all(row.get('dst') != 'default' for row in routes) and
self.namespace(self.name) == anchor['namespace'] and read_anchor() == anchor, 'SMOKE_DUMMY_UNCONFIRMED')
self.checkpoint('smoke', 'dummy-verified', rows[0]['ifindex'])
return anchor
def restore(self, assert_anchor_exited):
require(self.anchor is not None and self.namespace(self.name) == self.anchor['namespace'],
'SMOKE_NAMESPACE_DRIFT')
assert_anchor_exited(self.anchor)
require(not self.ip('netns', 'pids', self.name).strip(), 'SMOKE_NAMESPACE_BUSY')
self.checkpoint('smoke', 'delete-intent', self.anchor)
self.ip('netns', 'delete', self.name)
require(self.namespace(self.name) is None, 'SMOKE_NAMESPACE_RETIREMENT_UNCONFIRMED')
self.checkpoint('smoke', 'delete-verified', self.anchor)
def linux_ip():
binary = next((item.resolve(strict=True) for item in (Path('/usr/sbin/ip'), Path('/usr/bin/ip'), Path('/sbin/ip'))
if item.is_file()), None)
require(binary is not None and binary.stat().st_uid == 0 and not binary.stat().st_mode & 0o022, 'IP_BINARY_UNPINNED')
def call(*args):
result = subprocess.run([str(binary), *args], env=owner['ENV'], stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=8)
require(result.returncode == 0 and len(result.stdout) <= 128 * 1024, 'IP_OUTCOME_UNCONFIRMED')
return result.stdout.decode('utf-8', 'strict')
return call
def ns_pin(name):
try:
row = Path('/run/netns', name).lstat()
except FileNotFoundError:
return None
require(stat.S_ISREG(row.st_mode) and row.st_uid == 0, 'SMOKE_NAMESPACE_HANDLE_UNSAFE')
return [row.st_dev, row.st_ino]
def run_smoke(run, uid, gid, port, digest):
docker, attach = owner['local_docker']()
base = Path(owner['root'](run))
packet = base / 'packet'
verify_packet(packet, digest, require_root=True)
ip = linux_ip()
require(ns_pin('nf' + run + 'nc') is None, 'SMOKE_NAMESPACE_PRESENT')
journal = os.open(base / 'collector-smoke.jsonl', os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
with os.fdopen(journal, 'w', encoding='utf-8') as report:
def checkpoint(role, phase, value):
report.write(json.dumps({'role': role, 'phase': phase, 'value': value}, separators=(',', ':')) + '\n')
report.flush()
os.fsync(report.fileno())
checkpoint('smoke', 'packet-verified', digest)
owner['prepare_private_storage'](run, uid, gid)
runner = owner['CollectorContainerOwner'](run, docker, checkpoint)
runner.create_anchor(uid, gid)
runner.start_anchor()
scope = SmokeNamespace(run, ip, ns_pin, checkpoint)
# Reuse the container owner's live-C admission and retirement checks.
class Journal:
value = {}
class Backend:
def namespace(self, name):
return ns_pin(name)
def root_baseline(self):
raw = [ip('-j', '-4', 'route', 'show', 'table', 'all'), ip('-j', '-d', 'link', 'show')]
# Link counters/traffic are outside root configuration; use exact names and ifindex.
links = sorted((row['ifname'], row['ifindex']) for row in json.loads(raw[1]))
return hashlib.sha256(json.dumps([json.loads(raw[0]), links], sort_keys=True).encode()).hexdigest()
def provision(backend, ledger, _plan, _pins, read_collector_anchor):
baseline = backend.root_baseline()
anchor = scope.create(read_collector_anchor)
require(backend.root_baseline() == baseline, 'ROOT_NETWORK_DRIFT')
ledger.value.update(collectorAnchor=anchor, rootBaseline=baseline, state='created')
return anchor
def cleanup(_backend, ledger, _plan, assert_collector_anchor_exited):
scope.restore(assert_collector_anchor_exited)
ledger.value['state'] = 'retired'
backend, ledger, plan = Backend(), Journal(), {'names': {'C': scope.name}}
pin = runner.create_linux_topology(provision, backend, ledger, plan, {'nonce': run + '0' * 24})
wait_until = time.monotonic() + 20
socket = base / 'pg-socket' / '.s.PGSQL.5432'
while not socket.exists() and time.monotonic() < wait_until:
time.sleep(.1)
require(stat.S_ISSOCK(socket.lstat().st_mode), 'PRIVATE_PG_NOT_READY')
runner.create_collector(uid, gid, port, pin['namespace'], pin['hostNamespace'], digest,
lambda: verify_packet(packet, digest, require_root=True))
runner.start_collector(attach)
runner.retire_exited('collector')
runner.stop_anchor(lambda: require(ledger.value['state'] == 'created', 'SMOKE_TOPOLOGY_DRIFT'))
runner.cleanup_linux_topology(cleanup, backend, ledger, plan)
runner.retire_exited('postgres')
checkpoint('smoke', 'complete', None)
return {'collectorSmokeCompleted': True, 'chrMutations': 0, 'capturePerformed': False, 'nativeAcceptance': False}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
sub = parser.add_subparsers(dest='mode', required=True)
check = sub.add_parser('check')
check.add_argument('packet', type=Path)
check.add_argument('sha256')
package = sub.add_parser('pack')
package.add_argument('packet', type=Path)
package.add_argument('sha256')
run = sub.add_parser('run')
run.add_argument('run_id')
run.add_argument('uid', type=int)
run.add_argument('gid', type=int)
run.add_argument('port', type=int)
run.add_argument('sha256')
args = parser.parse_args()
try:
if args.mode == 'check':
result = {**verify_packet(args.packet.resolve(), args.sha256), 'dockerUsed': False, 'networkUsed': False}
elif args.mode == 'pack':
result = pack(args.packet.resolve(), args.sha256)
else:
result = run_smoke(args.run_id, args.uid, args.gid, args.port, args.sha256)
print(json.dumps(result))
except (OSError, ValueError, RuntimeError, subprocess.SubprocessError, KeyError) as error:
print(json.dumps({'completed': False, 'failureClass': type(error).__name__, 'automaticReplay': False}))
raise SystemExit(1) from None
@@ -1,69 +0,0 @@
import hashlib
import json
from pathlib import Path
import runpy
import tempfile
import unittest
smoke = runpy.run_path(str(Path(__file__).with_name('chr-netflow-collector-smoke.py')))
class SmokeTests(unittest.TestCase):
def test_pin_and_file_set_drift_fail_closed(self):
with tempfile.TemporaryDirectory(dir=Path(__file__).resolve().parents[3] / 'tmp') as temp:
root = Path(temp).resolve()
entry = root / 'dist/test/traffic-flow-chr-collector-bootstrap.js'
entry.parent.mkdir(parents=True)
entry.write_bytes(b'owned fixture\n')
body = json.dumps({'format': 'mmapp-netflow-collector-smoke-v1',
'cachedSourceRevision': 'bf3d50e1ff2ee3b5c9268991a255c3b49ce6378e',
'files': [{'path': entry.relative_to(root).as_posix(), 'size': entry.stat().st_size,
'sha256': hashlib.sha256(entry.read_bytes()).hexdigest()}]}).encode()
(root / 'manifest.json').write_bytes(body)
digest = hashlib.sha256(body).hexdigest()
self.assertEqual(smoke['verify_packet'](root, digest)['files'], 1)
entry.write_bytes(b'foreign data!\n')
with self.assertRaisesRegex(ValueError, 'FILE_DRIFT'):
smoke['verify_packet'](root, digest)
with self.assertRaisesRegex(ValueError, 'MANIFEST_DRIFT'):
smoke['verify_packet'](root, '0' * 64)
def test_exact_dummy_namespace_and_busy_cleanup(self):
anchor = {'containerId': 'a' * 64, 'pid': 42, 'namespace': [10, 20], 'hostNamespace': [10, 21]}
calls, journal = [], []
namespace = None
busy = True
name, device = 'nfa012bcdenc', 'nfa012bcdecv'
link = {'ifname': device, 'ifindex': 5, 'linkinfo': {'info_kind': 'dummy'}, 'flags': ['UP'],
'addr_info': [{'local': '198.18.242.3', 'prefixlen': 32}]}
def ip(*args):
nonlocal namespace
calls.append(args)
if args[:2] == ('netns', 'attach'):
self.assertEqual(args, ('netns', 'attach', name, '42'))
namespace = anchor['namespace']
elif args[:2] == ('netns', 'pids'):
return '42' if busy else ''
elif args[:2] == ('netns', 'delete'):
self.assertEqual(args, ('netns', 'delete', name))
namespace = None
elif args == ('-n', name, '-j', 'link', 'show'):
return json.dumps([{'ifname': 'lo'}])
elif '-d' in args:
return json.dumps([link])
elif 'route' in args:
return '[]'
return ''
fixture = smoke['SmokeNamespace']('a012bcde', ip, lambda _name: namespace, lambda *row: journal.append(row))
self.assertEqual(fixture.create(lambda: anchor), anchor)
self.assertIn(('-n', name, 'address', 'add', '198.18.242.3/32', 'dev', device), calls)
with self.assertRaisesRegex(ValueError, 'NAMESPACE_BUSY'):
fixture.restore(lambda pin: self.assertEqual(pin, anchor))
self.assertNotIn(('netns', 'delete', name), calls)
busy = False
fixture.restore(lambda pin: self.assertEqual(pin, anchor))
self.assertEqual(journal[-1][1], 'delete-verified')
if __name__ == '__main__':
unittest.main()
@@ -1,221 +0,0 @@
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { isIPv4 } from 'node:net';
import { pathToFileURL } from 'node:url';
const routes = Object.freeze(['/system/resource', '/ip/traffic-flow', '/ip/traffic-flow/target']);
const policyRoutes = Object.freeze(['/system/resource', '/ip/settings', '/ip/firewall/connection/tracking',
'/ip/firewall/filter', '/ip/firewall/raw', '/ip/firewall/mangle', '/ip/firewall/nat', '/routing/rule', '/ip/vrf']);
const counterFields = ['ifindex', 'rx-packet', 'tx-packet', 'fp-rx-byte', 'fp-tx-byte', 'fp-rx-packet', 'fp-tx-packet'];
const nativeId = value => typeof value === 'string' && /^\*[a-f0-9]{1,16}$/i.test(value);
const ports = Object.freeze({ '194.226.121.150': 19081, '104.165.244.7': 19082 });
const attestation = 'verified-known-host-161.104.46.222-owned-forward';
const reviewedTransportHash = '67d86dbae2710a25145df8ab9469029182f21a31972c49f063cdb6bc3daa8ea6';
const sha = bytes => createHash('sha256').update(bytes).digest('hex');
const deny = () => { throw new Error('NetFlow discovery refused'); };
export async function loadPinnedNetFlowTransport(policy = false) {
const allowedPaths = policy ? policyRoutes : routes;
return loadTransport(allowedPaths, allowedPaths.length * 2, policy ? 131072 : 65536);
}
async function loadTransport(allowedPaths, requests, responseLimit, totalLimit = responseLimit * requests) {
const bytes = await readFile(new URL('./chr-recursive-observation.mjs', import.meta.url));
if (sha(bytes) !== reviewedTransportHash) deny();
const reviewed = bytes.toString('utf8');
const source = reviewed.slice(0, reviewed.indexOf('async function privateStdin()'))
.replace(/const ROOT = .*?;/, 'const ROOT = null;')
.replace(/export const PATHS = Object.freeze\(\[[\s\S]*?\]\);/, `export const PATHS = Object.freeze(${JSON.stringify(allowedPaths)});`)
.replace('response: 1048576, bytes: 8388608, requests: 256',
`response: ${responseLimit}, bytes: ${totalLimit}, requests: ${requests}`);
const transport = await import('data:text/javascript;base64,' + Buffer.from(source).toString('base64'));
if (JSON.stringify(transport.PATHS) !== JSON.stringify(allowedPaths)) deny();
for (const [host, port] of Object.entries(ports)) {
if (endpoint(transport, host) !== `http://127.0.0.1:${port}`) deny();
}
return transport;
}
export async function discoverPinnedInterfaceCounterShape(read) {
const devices = [];
for (const host of Object.keys(ports)) {
const inventory = await read(host, '/interface');
if (!Array.isArray(inventory) || inventory.length > 256 ||
inventory.some(row => !nativeId(row?.['.id'])) ||
new Set(inventory.map(row => row['.id'])).size !== inventory.length) deny();
const preferred = type => inventory.find(row => row.type === type &&
observedFlag(row.running) === true && observedFlag(row.disabled) === false);
const chosen = preferred('wg') ?? preferred('ether');
if (!chosen) {
devices.push({ status: 'no-eligible-interface' });
continue;
}
const exact = single(await read(host, '/interface/' + encodeURIComponent(chosen['.id'])));
if (exact['.id'] !== chosen['.id'] || exact.type !== chosen.type ||
observedFlag(exact.running) !== true || observedFlag(exact.disabled) !== false) deny();
const fields = Object.fromEntries(counterFields.map(key => {
const value = exact[key];
return [key, { present: Object.hasOwn(exact, key),
type: value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value }];
}));
devices.push({ status: 'shape-observed', fields });
}
return { devices, ingressVerified: false, capturePerformed: false, mutationsPerformed: false };
}
function endpoint(transport, host) {
return transport.configuration(['--source', host, '--transport', 'ssh-loopback', '--output',
'tmp/netflow-endpoint-discovery', '--owned-tunnel-attestation', attestation]).endpoint;
}
function bool(value) {
if (value === true || value === 'true') return true;
if (value === false || value === 'false') return false;
return deny();
}
function single(rows) {
if (rows.length !== 1) deny();
return rows[0];
}
function observedFlag(value) {
if ([true, 'true', 'yes'].includes(value)) return true;
if ([false, 'false', 'no'].includes(value)) return false;
return null;
}
export async function discoverPinnedNetFlowPolicy(read) {
const exporters = [];
for (const host of Object.keys(ports)) {
const resource = single(await read(host, policyRoutes[0]));
if (typeof resource.version !== 'string' || !/^\d+\.\d+(?:\.\d+)?(?: \([a-z-]+\))?$/.test(resource.version)) deny();
const ip = single(await read(host, policyRoutes[1]));
const tracking = single(await read(host, policyRoutes[2]));
const firewall = {};
const vetoes = [];
const ipForward = observedFlag(ip['ip-forward']);
const allowFastPath = observedFlag(ip['allow-fast-path']);
const conntrack = tracking.enabled === 'auto' ? 'auto' : observedFlag(tracking.enabled);
if (ipForward !== true) vetoes.push('ip-forward-not-confirmed');
if (allowFastPath !== false) vetoes.push('fast-path-not-confirmed-disabled');
if (conntrack !== true) vetoes.push('conntrack-not-explicitly-enabled');
for (const path of policyRoutes.slice(3, 7)) {
const rows = await read(host, path);
const enabled = rows.filter(row => observedFlag(row.disabled) !== true);
firewall[path.split('/').at(-1)] = {
total: rows.length,
notExplicitlyDisabled: enabled.length,
fastTrack: enabled.filter(row => row.action === 'fasttrack-connection').length,
};
}
const routingRules = await read(host, policyRoutes[7]);
const activeRoutingRules = routingRules.filter(row => observedFlag(row.disabled) !== true).length;
if (activeRoutingRules) vetoes.push('routing-policy-present');
const vrfs = await read(host, policyRoutes[8]);
const mainVrfOnly = vrfs.length === 1 && vrfs[0].name === 'main' && vrfs[0].interfaces === 'all' &&
observedFlag(vrfs[0].disabled) !== true;
if (!mainVrfOnly) vetoes.push('main-vrf-only-not-confirmed');
exporters.push({ host, version: resource.version, ipForward, allowFastPath, conntrack, firewall,
activeRoutingRules, vrfCount: vrfs.length, mainVrfOnly, vetoes });
}
return {
status: exporters.some(item => item.vetoes.length) ? 'pre-topology-veto' : 'no-global-veto-observed',
exporters, routeSelectionVerified: false, tuplePolicyVerified: false,
ingressVerified: false, capturePerformed: false, mutationsPerformed: false,
};
}
function target(row) {
if (bool(row.disabled) || row.version !== 'ipfix') return null;
const destination = row['dst-address'];
const portText = String(row.port);
if (!isIPv4(destination) || !/^[1-9][0-9]{0,4}$/.test(portText) || Number(portText) > 65535) deny();
const source = row['src-address'];
if (source !== undefined && source !== '' && !isIPv4(source)) deny();
return { destination, port: Number(portText), configuredSource: source && source !== '0.0.0.0' ? source : null };
}
export async function discoverPinnedNetFlowEndpoints(read) {
const exporters = [];
for (const host of Object.keys(ports)) {
const resource = single(await read(host, routes[0]));
if (typeof resource.version !== 'string' || !/^\d+\.\d+(?:\.\d+)?(?: \([a-z-]+\))?$/.test(resource.version)) deny();
const enabled = bool(single(await read(host, routes[1])).enabled);
const rows = await read(host, routes[2]);
if (rows.length > 64) deny();
const configuredTargets = rows.map(target).filter(Boolean);
exporters.push({ host, version: resource.version, enabled, configuredTargets });
}
const [first, second] = exporters;
const sameEndpoint = (a, b) => a.destination === b.destination && a.port === b.port;
const common = first.enabled && second.enabled
? first.configuredTargets.filter(a => second.configuredTargets.some(b => sameEndpoint(a, b)))
: [];
const unique = common.length === 1 && second.configuredTargets.filter(b => sameEndpoint(common[0], b)).length === 1;
return {
status: common.length === 0 ? 'no-common-active-ipfix-target' : unique ? 'configured-endpoint' : 'ambiguous-endpoint',
configuredEndpoint: unique ? { destination: common[0].destination, port: common[0].port } : null,
exporters,
ingressVerified: false,
capturePerformed: false,
mutationsPerformed: false,
};
}
async function main() {
const mode = process.argv.slice(2);
if (mode.length !== 1) deny();
const policy = mode[0] === '--check-policy' || mode[0] === '--read-pinned-policy';
const counters = mode[0] === '--check-counters' || mode[0] === '--read-pinned-counters';
const transport = counters ? await loadTransport(['/interface'], 4, 131072) : await loadPinnedNetFlowTransport(policy);
if (mode[0] === '--check-only' || mode[0] === '--check-policy') {
console.log(JSON.stringify({ transportSha256: reviewedTransportHash, loopbackPorts: ports, readPaths: transport.PATHS, networkUsed: false }));
return;
}
if (mode[0] === '--check-counters') {
const detail = await loadTransport(['/interface/*A'], 4, 16384);
if (detail.PATHS[0] !== '/interface/*A') deny();
console.log(JSON.stringify({ transportSha256: reviewedTransportHash, loopbackPorts: ports,
readPaths: ['/interface', '/interface/<observed-native-id>'], networkUsed: false }));
return;
}
if (!['--read-pinned', '--read-pinned-policy', '--read-pinned-counters'].includes(mode[0]) || process.stdin.isTTY) deny();
const chunks = [];
let length = 0;
for await (const chunk of process.stdin) {
length += chunk.length;
if (length > 4096) deny();
chunks.push(chunk);
}
const input = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks)));
if (!input || input.attestation !== attestation ||
Object.keys(input).length !== 2 || !input.passwords || Object.keys(input.passwords).length !== 2) deny();
const passwords = Object.keys(ports).map(host => input.passwords[host]);
if (passwords.some(password => typeof password !== 'string' || !/^[^\u0000-\u001f\u007f]{1,512}$/u.test(password))) deny();
const authorizations = passwords.map(password => 'Basic ' + Buffer.from('admin:' + password).toString('base64'));
const secrets = [...passwords, ...authorizations, ...authorizations.map(value => value.slice(6))].map(value => Buffer.from(value));
const budget = { requests: 0, bytes: 0, rows: 0, deadline: Date.now() + 30000 };
const discovery = counters ? discoverPinnedInterfaceCounterShape : policy ? discoverPinnedNetFlowPolicy : discoverPinnedNetFlowEndpoints;
const report = await discovery(async (host, route) => {
const authorization = authorizations[Object.keys(ports).indexOf(host)];
let selected = transport;
if (counters && route !== '/interface') {
if (!route.startsWith('/interface/') || !nativeId(route.slice('/interface/'.length))) deny();
selected = await loadTransport([route], 4, 16384, 524288);
}
const response = await selected.readResponse(endpoint(selected, host), route, authorization, budget);
if (secrets.some(secret => response.body.includes(secret))) deny();
return response.rows;
});
const output = JSON.stringify(report);
if (secrets.some(secret => Buffer.from(output).includes(secret))) deny();
console.log(output);
}
if (process.argv[1] && pathToFileURL(process.argv[1]).href === import.meta.url) {
main().catch(() => {
console.error('NetFlow discovery refused');
process.exitCode = 1;
});
}
@@ -1,102 +0,0 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { discoverPinnedInterfaceCounterShape, discoverPinnedNetFlowEndpoints, discoverPinnedNetFlowPolicy, loadPinnedNetFlowTransport } from './chr-netflow-endpoint-discovery.mjs';
const hosts = ['194.226.121.150', '104.165.244.7'];
const routes = ['/system/resource', '/ip/traffic-flow', '/ip/traffic-flow/target'];
const target = { disabled: 'false', version: 'ipfix', 'dst-address': '192.0.2.10', port: '2055', 'src-address': '0.0.0.0' };
function fixture(targets, enabled = ['true', 'true']) {
const calls = [];
const read = async (host, path) => {
calls.push([host, path]);
if (path === routes[0]) return [{ version: '7.24.5 (stable)' }];
if (path === routes[1]) return [{ enabled: enabled[hosts.indexOf(host)] }];
assert.equal(path, routes[2]);
return targets[hosts.indexOf(host)];
};
return { read, calls };
}
test('loads the reviewed source with the exact read-only path and relay fence', async () => {
const transport = await loadPinnedNetFlowTransport();
assert.deepEqual(transport.PATHS, routes);
assert.throws(() => transport.readResponse('http://127.0.0.1:19082', '/ip/address', '', {}), /request-budget/);
assert.throws(() => transport.readResponse('http://127.0.0.1:19083', routes[0], '', {}), /non-whitelisted-endpoint/);
});
test('reports only a common configured endpoint, with no ingress or capture claim', async () => {
const { read, calls } = fixture([[target], [{ ...target, 'src-address': '192.0.2.20' }]]);
const report = await discoverPinnedNetFlowEndpoints(read);
assert.deepEqual(calls, hosts.flatMap(host => routes.map(path => [host, path])));
assert.equal(report.status, 'configured-endpoint');
assert.deepEqual(report.configuredEndpoint, { destination: '192.0.2.10', port: 2055 });
assert.deepEqual(report.exporters.map(item => item.configuredTargets[0].configuredSource), [null, '192.0.2.20']);
assert.equal(report.ingressVerified, false);
assert.equal(report.capturePerformed, false);
assert.equal(report.mutationsPerformed, false);
});
test('returns successful absence for missing or disabled baseline', async () => {
for (const input of [
fixture([[], [target]]),
fixture([[target], [target]], [true, false]),
fixture([[target], [{ ...target, disabled: true }]]),
fixture([[target], [{ ...target, version: '9' }]]),
]) {
const result = await discoverPinnedNetFlowEndpoints(input.read);
assert.equal(result.status, 'no-common-active-ipfix-target');
assert.equal(result.configuredEndpoint, null);
}
});
test('refuses malformed relevant rows and withholds ambiguous common endpoints', async () => {
const result = await discoverPinnedNetFlowEndpoints(fixture([[target], [target, target]]).read);
assert.equal(result.status, 'ambiguous-endpoint');
assert.equal(result.configuredEndpoint, null);
await assert.rejects(discoverPinnedNetFlowEndpoints(fixture([[target], [{ ...target, port: '65536' }]]).read), /^Error: NetFlow discovery refused$/);
});
test('reports preliminary global vetoes and counts with no claim on tuple policy or routes', async () => {
const transport = await loadPinnedNetFlowTransport(true);
const calls = [];
const report = await discoverPinnedNetFlowPolicy(async (host, path) => {
calls.push([host, path]);
return ({
'/system/resource': [{ version: '7.24.5 (stable)' }],
'/ip/settings': [{ 'ip-forward': 'yes', 'allow-fast-path': host === hosts[0] ? 'yes' : 'no' }],
'/ip/firewall/connection/tracking': [{ enabled: host === hosts[0] ? 'auto' : 'yes' }],
'/ip/firewall/filter': [{ action: 'fasttrack-connection' }, { action: 'drop', disabled: 'true' }],
'/ip/firewall/raw': [], '/ip/firewall/mangle': [], '/ip/firewall/nat': [],
'/routing/rule': host === hosts[0] ? [{}] : [],
'/ip/vrf': [{ name: 'main', interfaces: 'all' }],
})[path];
});
assert.deepEqual(calls, hosts.flatMap(host => transport.PATHS.map(path => [host, path])));
assert.equal(report.status, 'pre-topology-veto');
assert.deepEqual(report.exporters[0].vetoes, ['fast-path-not-confirmed-disabled', 'conntrack-not-explicitly-enabled', 'routing-policy-present']);
assert.deepEqual(report.exporters[1].vetoes, []);
assert.deepEqual(report.exporters[0].firewall.filter, { total: 2, notExplicitlyDisabled: 1, fastTrack: 1 });
assert.equal(report.routeSelectionVerified, false);
assert.equal(report.tuplePolicyVerified, false);
assert.equal(report.ingressVerified, false);
});
test('counter shape reads exact selected native IDs and emits no identities or counter values', async () => {
const ether = { '.id': '*A', name: 'private-ether', type: 'ether', running: 'true', disabled: 'false' };
const wg = { ...ether, '.id': '*B', name: 'private-wg', type: 'wg' };
const calls = [];
const report = await discoverPinnedInterfaceCounterShape(async (host, route) => {
calls.push([host, route]);
if (route === '/interface') return host === hosts[0] ? [ether, wg] : [ether];
return [{ ...(host === hosts[0] ? wg : ether), ifindex: '61455', 'rx-packet': 71922, 'fp-rx-byte': null }];
});
assert.deepEqual(calls, [[hosts[0], '/interface'], [hosts[0], '/interface/*B'], [hosts[1], '/interface'], [hosts[1], '/interface/*A']]);
assert.deepEqual(report.devices[0].fields.ifindex, { present: true, type: 'string' });
assert.deepEqual(report.devices[0].fields['rx-packet'], { present: true, type: 'number' });
assert.deepEqual(report.devices[0].fields['fp-rx-byte'], { present: true, type: 'null' });
assert.deepEqual(report.devices[0].fields['tx-packet'], { present: false, type: 'undefined' });
assert.doesNotMatch(JSON.stringify(report), /private|61455|71922|104\.165|194\.226|\*[AB]/);
await assert.rejects(discoverPinnedInterfaceCounterShape(async (_host, route) =>
route === '/interface' ? [ether] : [{ ...ether, '.id': '*C' }]), /discovery refused/);
});
@@ -1,86 +0,0 @@
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { lstat, readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { decodeBody, SOURCES } from './chr-recursive-observation.mjs';
const reviewedReportSha256 = 'fccf5171c8e50730a3e7c1fcceeeb77f1e946589ac4a56c6e04668e0ff4663f9';
const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
const opaqueId = value => typeof value === 'string' && /^\*[a-f0-9]+$/i.test(value);
const decimalIndex = value => typeof value === 'string' && /^[1-9][0-9]{0,15}$/.test(value);
export function inspectDirectLinks(routes, nexthops, interfaces, tables) {
function keyed(rows, field, valid) {
const index = new Map();
for (const row of rows) {
const key = row[field];
assert(valid(key) && !index.has(key), 'Missing or ambiguous ' + field);
index.set(key, row);
}
return index;
}
const byNexthopId = keyed(nexthops, '.id', opaqueId);
const byInterfaceIndex = keyed(interfaces, 'ifindex', decimalIndex);
const byTableName = keyed(tables, 'name', value => typeof value === 'string' && value !== '' && !/^[0-9]+$/.test(value));
let opaqueReferences = 0, namedInterfaceCorroborations = 0, interfaceIdSubstitutionRefusals = 0;
for (const route of routes) {
assert(byTableName.has(route['routing-table']), 'Missing named routing table');
if (!Object.hasOwn(route, 'nexthop-id')) continue;
const nexthop = byNexthopId.get(route['nexthop-id']);
assert(nexthop, 'No exact opaque nexthop match');
opaqueReferences++;
const gateway = route['immediate-gw'];
const name = typeof gateway === 'string' ? /%([^%@]+)$/.exec(gateway)?.[1] : undefined;
if (!name) continue;
const index = nexthop['immediate-gw.interface-idx'];
const observed = byInterfaceIndex.get(index);
assert(observed?.name === name && interfaces.filter(row => row.name === name).length === 1,
'No exact named interface corroboration for ifindex');
namedInterfaceCorroborations++;
if (!interfaces.some(row => row.id === index && row.name === name)) interfaceIdSubstitutionRefusals++;
}
assert(opaqueReferences && namedInterfaceCorroborations && interfaceIdSubstitutionRefusals, 'Capture lacks discriminating direct links');
return { opaqueReferences, namedInterfaceCorroborations, interfaceIdSubstitutionRefusals,
tableNames: byTableName.size, recursiveLinkRows: nexthops.filter(row => Object.keys(row).some(key => key === 'links' || key.startsWith('links.'))).length };
}
async function readPinned(directory, filename, digest) {
const file = path.join(directory, filename), stat = await lstat(file);
assert(stat.isFile() && !stat.isSymbolicLink() && stat.size <= 1048576, 'Unavailable bounded raw file');
const bytes = await readFile(file);
assert(bytes.length === stat.size && sha256(bytes) === digest, 'Raw capture hash changed');
return bytes;
}
export async function checkReviewedDirectLinks(directory) {
const report = JSON.parse(await readPinned(directory, 'report.json', reviewedReportSha256));
const sources = [];
for (const source of report.sources) {
assert(source.complete && source.versionBracketEqual && SOURCES[source.source]?.serverId === source.serverId);
const read = async (index, resource) => {
const record = source.records[index];
assert(record.resource === resource && record.status === 200 && record.version === '7.24.5 (stable)');
assert(record.file === `server${source.serverId}-${String(index + 1).padStart(2, '0')}-${resource.slice(1).replaceAll('/', '-')}.json`);
return decodeBody(await readPinned(directory, record.file, record.rawSha256)).rows;
};
const routes = await read(1, '/routing/route'), nexthops = await read(2, '/routing/nexthop');
assert.deepEqual(await read(7, '/routing/route'), routes);
assert.deepEqual(await read(6, '/routing/nexthop'), nexthops);
assert.equal(source.records[1].rawSha256, source.records[7].rawSha256);
assert.equal(source.records[2].rawSha256, source.records[6].rawSha256);
const interfaces = await read(3, '/interface'), tables = await read(4, '/routing/table');
sources.push({ serverId: source.serverId, ...inspectDirectLinks(routes, nexthops, interfaces, tables) });
}
return { reviewedReportSha256, sources, recursiveIdentity: 'unknown', semanticProof: false };
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
try {
assert(process.argv.length === 3, 'Supply the reviewed offline capture directory');
process.stdout.write(JSON.stringify(await checkReviewedDirectLinks(process.argv[2])) + '\n');
} catch {
process.stderr.write('Reviewed direct-link corroboration refused.\n');
process.exitCode = 1;
}
}
@@ -1,17 +0,0 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { inspectDirectLinks } from './chr-recursive-direct-links.mjs';
test('direct evidence requires exact opaque reference and corroborated ifindex namespace', () => {
const route = { 'routing-table': 'main', 'nexthop-id': '*A', 'immediate-gw': '192.0.2.1%ether1' };
const nexthop = { '.id': '*A', 'immediate-gw.interface-idx': '4' };
const iface = { id: '9', ifindex: '4', name: 'ether1' };
const table = { '.id': '*0', name: 'main' };
assert.deepEqual(inspectDirectLinks([route], [nexthop], [iface], [table]), {
opaqueReferences: 1, namedInterfaceCorroborations: 1, interfaceIdSubstitutionRefusals: 1, tableNames: 1, recursiveLinkRows: 0,
});
assert.throws(() => inspectDirectLinks([{ ...route, 'nexthop-id': '*a' }], [nexthop], [iface], [table]), /exact opaque/);
assert.throws(() => inspectDirectLinks([{ ...route, 'routing-table': '0' }], [nexthop], [iface], [table]), /named routing table/);
assert.throws(() => inspectDirectLinks([route], [nexthop], [{ ...iface, id: '4', ifindex: '9' }], [table]), /corroboration/);
assert.throws(() => inspectDirectLinks([route], [nexthop], [iface, { ...iface, id: '3' }], [table]), /ambiguous ifindex/);
});
@@ -111,7 +111,7 @@ export async function acquire(config, credentials) {
const authorization = 'Basic ' + Buffer.from(credentials.username + ':' + credentials.password).toString('base64'); const authorization = 'Basic ' + Buffer.from(credentials.username + ':' + credentials.password).toString('base64');
const report = { format: 'chr-recursive-raw-acquisition-v1', source: fixed.source, serverId: fixed.serverId, const report = { format: 'chr-recursive-raw-acquisition-v1', source: fixed.source, serverId: fixed.serverId,
transport: fixed.transport, tunnelAttestation: fixed.tunnelAttestation, startedAt: new Date().toISOString(), transport: fixed.transport, tunnelAttestation: fixed.tunnelAttestation, startedAt: new Date().toISOString(),
semanticProof: false, complete: false, snapshotBracket: null, records: [], limits: LIMITS }; semanticProof: false, complete: false, records: [], limits: LIMITS };
let version, attemptedResource; let version, attemptedResource;
try { try {
for (const [index, resource] of PATHS.entries()) { for (const [index, resource] of PATHS.entries()) {
@@ -130,13 +130,6 @@ export async function acquire(config, credentials) {
report.records.push({ file, resource, startedAt, finishedAt: new Date().toISOString(), version, report.records.push({ file, resource, startedAt, finishedAt: new Date().toISOString(), version,
status: response.status, contentType: response.contentType, bytes: response.body.length, rows: response.rows.length, rawSha256: sha(response.body) }); status: response.status, contentType: response.contentType, bytes: response.body.length, rows: response.rows.length, rawSha256: sha(response.body) });
} }
const hashFor = resource => report.records.filter(record => record.resource === resource).map(record => record.rawSha256);
const [firstRoute, lastRoute] = hashFor('/routing/route');
const [firstNexthop, lastNexthop] = hashFor('/routing/nexthop');
report.snapshotBracket = {
routeByteEqual: firstRoute === lastRoute,
nexthopByteEqual: firstNexthop === lastNexthop,
};
report.complete = true; report.complete = true;
} catch (error) { } catch (error) {
report.failure = 'acquisition-incomplete'; report.failure = 'acquisition-incomplete';
@@ -170,8 +163,7 @@ if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.me
const config = configuration(process.argv.slice(2)); const config = configuration(process.argv.slice(2));
const report = await acquire(config, await privateStdin()); const report = await acquire(config, await privateStdin());
// Stdout contains metadata only; no native bodies, credentials or errors. // Stdout contains metadata only; no native bodies, credentials or errors.
process.stdout.write(JSON.stringify({ complete: report.complete, snapshotBracket: report.snapshotBracket, process.stdout.write(JSON.stringify({ complete: report.complete, semanticProof: false, source: report.source, records: report.records.length }) + '\n');
semanticProof: false, source: report.source, records: report.records.length }) + '\n');
if (!report.complete) process.exitCode = 1; if (!report.complete) process.exitCode = 1;
} catch { process.stderr.write('Read-only acquisition refused or incomplete.\n'); process.exitCode = 1; } } catch { process.stderr.write('Read-only acquisition refused or incomplete.\n'); process.exitCode = 1; }
} }
@@ -17,13 +17,11 @@ test('source/transport allowlist refuses arbitrary hosts, plaintext external, mi
test('full own fixture saves nine exact immutable captures and metadata; reflected errors never enter reports', async () => { test('full own fixture saves nine exact immutable captures and metadata; reflected errors never enter reports', async () => {
const output = fileURLToPath(new URL('../../../tmp/chr-recursive-own-fixture-' + randomUUID(), import.meta.url)), seen = []; const output = fileURLToPath(new URL('../../../tmp/chr-recursive-own-fixture-' + randomUUID(), import.meta.url)), seen = [];
let reflect = false, driftingResource; let reflect = false;
const server = createServer((req, res) => { const server = createServer((req, res) => {
seen.push(req.url); res.setHeader('Content-Type', 'application/json'); seen.push(req.url); res.setHeader('Content-Type', 'application/json');
if (reflect) { res.statusCode = 401; res.end('{"message":"own-fixture-secret-only"}'); return; } if (reflect) { res.statusCode = 401; res.end('{"message":"own-fixture-secret-only"}'); return; }
if (req.url === '/rest/system/resource') res.end('[{"version":"7.26"}]\n'); res.end(req.url === '/rest/system/resource' ? '[{"version":"7.26"}]\n' : '[{".id":"*1"}]\n');
else if (req.url === driftingResource && seen.indexOf(req.url) !== seen.length - 1) res.end('[{".id":"*2"}]\n');
else res.end('[{".id":"*1"}]\n');
}); });
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(19081, '127.0.0.1', resolve); }); await new Promise((resolve, reject) => { server.once('error', reject); server.listen(19081, '127.0.0.1', resolve); });
const makeConfig = destination => configuration(['--source', '194.226.121.150', '--transport', 'ssh-loopback', '--output', destination, const makeConfig = destination => configuration(['--source', '194.226.121.150', '--transport', 'ssh-loopback', '--output', destination,
@@ -31,7 +29,6 @@ test('full own fixture saves nine exact immutable captures and metadata; reflect
try { try {
const report = await acquire(makeConfig(output), { username: 'own-fixture', password: 'own-fixture-secret-only' }); const report = await acquire(makeConfig(output), { username: 'own-fixture', password: 'own-fixture-secret-only' });
assert.equal(report.complete, true); assert.equal(report.semanticProof, false); assert.equal(report.complete, true); assert.equal(report.semanticProof, false);
assert.deepEqual(report.snapshotBracket, { routeByteEqual: true, nexthopByteEqual: true });
assert.deepEqual(seen, PATHS.map(resource => '/rest' + resource)); assert.deepEqual(seen, PATHS.map(resource => '/rest' + resource));
for (const record of report.records) { for (const record of report.records) {
const body = await readFile(output + '/' + record.file); const body = await readFile(output + '/' + record.file);
@@ -41,22 +38,10 @@ test('full own fixture saves nine exact immutable captures and metadata; reflect
await assert.rejects(acquire(makeConfig(output), { username: 'own-fixture', password: 'own-fixture-secret-only' })); await assert.rejects(acquire(makeConfig(output), { username: 'own-fixture', password: 'own-fixture-secret-only' }));
assert.equal(seen.length, 9); assert.equal(seen.length, 9);
await assert.rejects(privateDirectory('../outside-tmp')); await assert.rejects(privateDirectory('../outside-tmp'));
for (const resource of ['route', 'nexthop']) {
seen.length = 0;
driftingResource = '/rest/routing/' + resource;
const driftOutput = output + '-' + resource;
const drift = await acquire(makeConfig(driftOutput), { username: 'own-fixture', password: 'own-fixture-secret-only' });
assert.equal(drift.complete, true);
assert.equal(drift.semanticProof, false);
assert.deepEqual(drift.snapshotBracket, { routeByteEqual: resource !== 'route', nexthopByteEqual: resource !== 'nexthop' });
assert.equal(drift.records.length, 9);
assert.deepEqual(JSON.parse(await readFile(driftOutput + '/report.json', 'utf8')).snapshotBracket, drift.snapshotBracket);
}
reflect = true; reflect = true;
const failedOutput = output + '-failed'; const failedOutput = output + '-failed';
const failed = await acquire(makeConfig(failedOutput), { username: 'own-fixture', password: 'own-fixture-secret-only' }); const failed = await acquire(makeConfig(failedOutput), { username: 'own-fixture', password: 'own-fixture-secret-only' });
assert.equal(failed.complete, false); assert.equal(failed.records.length, 0); assert.equal(failed.complete, false); assert.equal(failed.records.length, 0);
assert.equal(failed.snapshotBracket, null);
assert.equal(failed.failedResource, '/system/resource'); assert.equal(failed.failedHttpStatus, 401); assert.equal(failed.failedResource, '/system/resource'); assert.equal(failed.failedHttpStatus, 401);
assert.deepEqual((await readdir(failedOutput)).sort(), ['capture.claim', 'report.json']); assert.deepEqual((await readdir(failedOutput)).sort(), ['capture.claim', 'report.json']);
assert(!String(await readFile(failedOutput + '/report.json')).includes('own-fixture-secret-only')); assert(!String(await readFile(failedOutput + '/report.json')).includes('own-fixture-secret-only'));
@@ -1,58 +0,0 @@
import assert from 'node:assert/strict'
import { setTimeout as delay } from 'node:timers/promises'
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client'
import { resumeMcpSteps } from './mcp-step-journal.mjs'
const [phase, port, planId, connectionId] = process.argv.slice(2)
assert(process.send && process.env.MCP_FIXTURE_TOKEN && ['dispatch', 'resume'].includes(phase))
const uuid = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i
assert.match(planId, uuid)
assert.match(connectionId, uuid)
const client = new Client({ name: 'owned-routing-observer', version: '1.0.0' })
async function call(name, args) {
const reply = await client.callTool({ name, arguments: args })
assert(!reply.isError, 'MCP fixture call failed: ' + name)
return reply.structuredContent?.result ?? JSON.parse(reply.content[0].text)
}
try {
await client.connect(new StreamableHTTPClientTransport(new URL(`http://127.0.0.1:${port}/mcp`), {
requestInit: { headers: { Authorization: 'Bearer ' + process.env.MCP_FIXTURE_TOKEN } },
}))
let operationId
const result = await resumeMcpSteps({ journal: 'routing-forward.jsonl', fixture: `routing-scoped-forward:${connectionId}:${planId}`, steps: [{
id: 'forward-apply', kind: 'apply',
async run({ idempotencyKey, accept }) {
assert.equal(phase, 'dispatch', 'A resumed observer cannot send apply')
const receipt = await call('mm_change_apply', { planId, idempotencyKey })
assert.deepEqual([receipt.plan_id, receipt.connection_id], [planId, connectionId])
accept(receipt.id)
// The supervisor terminates this process after the journal fsync.
process.send(receipt.id)
await new Promise(resolve => process.once('disconnect', resolve))
throw new Error('Observer was disconnected by its supervisor')
},
async reconcile({ accepted }) {
assert.equal(phase, 'resume')
assert.ok(accepted)
const deadline = Date.now() + 60_000
while (Date.now() < deadline) {
const receipt = await call('mm_operation_get', { operationId: accepted })
assert.deepEqual([receipt.id, receipt.plan_id, receipt.connection_id], [accepted, planId, connectionId])
if (!['pending', 'running'].includes(receipt.state)) {
assert.deepEqual([receipt.state, receipt.verification], ['applied', 'passed'])
operationId = accepted
return { state: 'verified', evidence: accepted }
}
await delay(400)
}
return { state: 'unknown' }
},
}] })
assert.deepEqual(result, { status: 'passed' })
assert.ok(operationId)
await new Promise((resolve, reject) => process.send(operationId, error => error ? reject(error) : resolve()))
} finally {
await client.close()
if (process.connected) process.disconnect()
}
@@ -1,84 +0,0 @@
import assert from 'node:assert/strict'
import { resumeMcpSteps } from './mcp-step-journal.mjs'
const uuid = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i
function isProtocolStorageBusy(error) {
const text = error?.data?.text
if (error?.name !== 'SdkHttpError' || error.status !== 503 ||
typeof text !== 'string' || Buffer.byteLength(text) > 1024) return false
try {
const body = JSON.parse(text)
return body?.code === 'PROTOCOL_STORAGE_BUSY'
} catch {
return false
}
}
/** The caller supplies an already registered synthetic test server and owner review. */
export function resumeServerPoll({ journal, fixture, serverId, connectionId, call, findPlan, findAcceptedOperation, assertApproved }) {
assert(Number.isSafeInteger(serverId) && serverId > 0)
assert.match(connectionId, uuid)
const find = async args => {
const planId = await findPlan?.({ ...args, connectionId, serverId })
return planId ? { state: 'verified', evidence: planId } : { state: 'unknown' }
}
return resumeMcpSteps({ journal, fixture: `${fixture}:server-${serverId}:connection-${connectionId}`, steps: [
{ id: 'mm_server_poll_plan', kind: 'plan', reconcile: find, async run({ accept }) {
const result = await call('mm_server_poll_plan', { serverId })
accept(result.planId)
return result.planId
} },
{ id: 'manager.plan-approved', kind: 'read', async run({ evidence }) {
const planId = evidence.get('mm_server_poll_plan')
await assertApproved(planId)
return planId
} },
{ id: 'mm_change_apply', kind: 'apply',
async reconcile(args) {
const id = args.accepted ?? await findAcceptedOperation?.({
...args, connectionId, planId: args.evidence.get('mm_server_poll_plan'),
})
return id ? { state: 'verified', evidence: id } : { state: 'unknown' }
},
async run({ evidence, idempotencyKey, accept }) {
const planId = evidence.get('mm_server_poll_plan')
const result = await call('mm_change_apply', { planId, idempotencyKey })
accept(result.id)
return result.id
} },
{ id: 'mm_operation_get', kind: 'read', async run({ evidence }) {
const operationId = evidence.get('mm_change_apply')
let result
try {
result = await call('mm_operation_get', { operationId })
} catch (error) {
if (!isProtocolStorageBusy(error)) throw error
throw Object.assign(new Error('MCP протокольное хранилище занято', { cause: error }), {
code: 'PROTOCOL_STORAGE_BUSY',
})
}
assert.equal(result.id, operationId)
assert.equal(result.plan_id, evidence.get('mm_server_poll_plan'))
assert.equal(result.connection_id, connectionId)
if (result.state !== 'applied' || result.verification !== 'passed') {
const code = ['pending', 'running'].includes(result.state) ? 'POLL_PENDING' : 'POLL_UNVERIFIED'
throw Object.assign(new Error('Poll terminal result is unverified'), { code })
}
assert.equal(result.steps.length, 1)
assert.equal(result.steps[0].state, 'verified')
const receipt = result.steps[0].result
assert.equal(receipt.operationId, operationId)
assert.match(receipt.receiptId, uuid)
assert.equal(receipt.nativeReads, 4)
assert.equal(receipt.complete, true)
assert.equal(receipt.maintenanceComplete, true)
assert.equal(receipt.configurationVerified, true)
assert.equal(receipt.automaticReplay, false)
assert(Number.isSafeInteger(receipt.completedPublications) && receipt.completedPublications >= 2)
assert.equal(receipt.confirmedPublications, receipt.completedPublications)
assert.equal(receipt.snapshot.serverId, serverId)
return operationId
} },
] })
}
@@ -1,119 +0,0 @@
import assert from 'node:assert/strict'
import { createHash } from 'node:crypto'
import { closeSync, constants, fstatSync, fsyncSync, openSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
import { isDeepStrictEqual } from 'node:util'
const key = /^[a-z0-9][a-z0-9_.:-]{0,127}$/i
const evidenceId = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i
const errorCode = /^[A-Z][A-Z0-9_]{0,63}$/
const validEvidence = (receipt, kind) => receipt === null ? kind === 'read' : typeof receipt === 'string' && evidenceId.test(receipt)
/** A test journal only. The caller owns the fixture and all reconciliation reads. */
export async function resumeMcpSteps({ journal, fixture, steps }) {
assert(key.test(fixture))
const scope = steps.map(({ id, kind }) => ({ id, kind }))
assert(scope.length && new Set(scope.map(step => step.id)).size === scope.length)
assert(scope.every(({ id, kind }) => key.test(id) && ['read', 'plan', 'apply'].includes(kind)))
// One process owns a journal at a time. A leftover lock requires operator reconciliation.
const lock = openSync(journal + '.lock', 'wx', 0o600)
let records
try {
let fd
try {
fd = openSync(journal, constants.O_RDWR | constants.O_APPEND | constants.O_CREAT | (constants.O_NOFOLLOW ?? 0), 0o600)
assert(fstatSync(fd).isFile(), 'Test journal must be a regular file')
const raw = readFileSync(fd, 'utf8')
assert(!raw || raw.endsWith('\n'), 'Truncated test receipt')
records = raw.trimEnd().split('\n').filter(Boolean).map(line => JSON.parse(line))
const header = { phase: 'fixture', fixture, steps: scope }
const append = entry => {
writeFileSync(fd, JSON.stringify(entry) + '\n', 'utf8')
fsyncSync(fd)
records.push(entry)
}
if (!records.length) append(header)
assert(isDeepStrictEqual(records[0], header), 'Test fixture or ordered step list changed')
const state = new Map()
let cursor = 0
for (const entry of records.slice(1)) {
const previous = state.get(entry.id)
assert(scope[cursor]?.id === entry.id, 'Unexpected test receipt order')
if (entry.phase === 'started') {
assert(!previous || previous.phase === 'retryable', 'Unexpected resumed dispatch')
assert(entry.attempt === (previous?.attempt ?? 0) + 1, 'Unexpected test attempt')
} else {
assert(['started', 'accepted', 'failed'].includes(previous?.phase) && entry.attempt === previous.attempt, 'Unbound test receipt')
assert(['passed', 'retryable', 'accepted', 'failed'].includes(entry.phase), 'Unknown test receipt')
if (entry.phase === 'accepted') {
assert(previous.phase === 'started' && scope[cursor].kind !== 'read' &&
validEvidence(entry.evidence, scope[cursor].kind), 'Invalid MCP acceptance receipt')
}
if (entry.phase === 'failed') {
assert(previous.phase !== 'failed' && typeof entry.code === 'string' && errorCode.test(entry.code), 'Invalid failure receipt')
assert(entry.accepted === (previous.phase === 'accepted' ? previous.evidence : null), 'MCP acceptance receipt changed')
}
if (entry.phase === 'passed') {
assert(validEvidence(entry.evidence, scope[cursor].kind), 'Invalid stored MCP receipt ID')
const accepted = previous.phase === 'accepted' ? previous.evidence : previous.accepted
assert(!accepted || accepted === entry.evidence, 'MCP acceptance receipt changed')
cursor++
}
if (entry.phase === 'retryable') {
assert(entry.accepted === null && (previous.phase === 'started' ||
previous.phase === 'failed' && previous.accepted === null), 'Cannot retry an accepted MCP step')
}
}
state.set(entry.id, entry)
}
const evidence = new Map([...state].filter(([, entry]) => entry.phase === 'passed').map(([id, entry]) => [id, entry.evidence]))
const attemptKey = (step, attempt) => 'acceptance-' + createHash('sha256')
.update(JSON.stringify([fixture, step.id, step.kind, attempt])).digest('hex')
const record = (step, attempt, phase, details = {}) => append({ id: step.id, attempt, phase, ...details })
const complete = (step, attempt, receipt) => {
assert(validEvidence(receipt, step.kind), 'Invalid MCP receipt ID')
record(step, attempt, 'passed', { evidence: receipt })
evidence.set(step.id, receipt)
}
for (const step of steps.slice(cursor)) {
const previous = state.get(step.id)
let attempt = previous?.attempt ?? 0
if (previous && ['started', 'accepted', 'failed'].includes(previous.phase)) {
const accepted = previous.phase === 'accepted' ? previous.evidence : previous.accepted ?? null
const result = step.kind === 'read' ? { state: 'no-effect' } :
await step.reconcile?.({ attempt, accepted, idempotencyKey: attemptKey(step, attempt), evidence: new Map(evidence) })
if (result?.state === 'verified') {
assert(!accepted || accepted === result.evidence, 'Reconciled a different MCP receipt')
complete(step, attempt, result.evidence)
continue
}
if (accepted || result?.state !== 'no-effect') return { status: 'reconcile', step: step.id }
record(step, attempt, 'retryable', { accepted })
}
attempt++
record(step, attempt, 'started')
let receipt
let accepted = null
try {
receipt = await step.run({ attempt, idempotencyKey: attemptKey(step, attempt), evidence: new Map(evidence), accept(id) {
assert(accepted === null && step.kind !== 'read' && validEvidence(id, step.kind), 'Invalid MCP acceptance receipt')
record(step, attempt, 'accepted', { evidence: id })
accepted = id
} })
} catch (error) {
const code = typeof error?.code === 'string' && errorCode.test(error.code) ? error.code : 'TEST_STEP_FAILED'
record(step, attempt, 'failed', { code, accepted })
return { status: 'failed', step: step.id }
}
assert(!accepted || accepted === receipt, 'Verified a different MCP receipt')
complete(step, attempt, receipt)
}
return { status: 'passed' }
} finally {
if (fd !== undefined) closeSync(fd)
}
} finally {
closeSync(lock)
unlinkSync(journal + '.lock')
}
}
@@ -1,176 +0,0 @@
import assert from 'node:assert/strict'
import { appendFileSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import test from 'node:test'
import { resumeMcpSteps } from './mcp-step-journal.mjs'
import { resumeServerPoll } from './mcp-server-poll-steps.mjs'
const uuid = 'ec4c6a09-3c3b-4a37-a370-c8821321807c'
const connectionId = 'd0d96834-948b-4cea-81d8-d8f9dc5e1429'
const operationId = 'c2cc9f5d-6d62-43b5-a6d2-ba7bedbd4fc1'
const pollReceipt = { operationId, receiptId: uuid, nativeReads: 4, complete: true,
maintenanceComplete: true, configurationVerified: true, automaticReplay: false,
confirmedPublications: 22, completedPublications: 22, snapshot: { serverId: 3, status: 'online' } }
function options(t, steps) {
const dir = mkdtempSync(join(tmpdir(), 'mm-mcp-step-test-'))
t.after(() => rmSync(dir, { recursive: true }))
return { journal: join(dir, 'history.jsonl'), fixture: 'owned-test', steps }
}
test('resumes the failed read and preserves successful reads and failed attempts', async t => {
const calls = []
const config = options(t, [
{ id: 'first', kind: 'read', run: async () => { calls.push('first'); return null } },
{ id: 'second', kind: 'read', run: async ({ attempt }) => {
calls.push('second:' + attempt)
if (attempt === 1) throw Error('test failure')
return null
} },
])
assert.deepEqual(await resumeMcpSteps(config), { status: 'failed', step: 'second' })
assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' })
assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' })
assert.deepEqual(calls, ['first', 'second:1', 'second:2'])
assert.deepEqual(readFileSync(config.journal, 'utf8').trim().split('\n').map(line => JSON.parse(line).phase),
['fixture', 'started', 'passed', 'started', 'failed', 'retryable', 'started', 'passed'])
})
test('unknown apply awaits reconciliation and a verified receipt advances without resending', async t => {
let sends = 0
let verified = false
let sentKey
const config = options(t, [
{ id: 'plan', kind: 'plan', run: async () => uuid },
{ id: 'apply', kind: 'apply', run: async ({ evidence, accept, idempotencyKey }) => {
assert.equal(evidence.get('plan'), uuid)
sentKey = idempotencyKey
sends++
accept(uuid)
throw Error('lost reply')
}, reconcile: async ({ accepted, idempotencyKey }) => {
assert.equal(accepted, uuid)
assert.equal(idempotencyKey, sentKey)
return verified ? { state: 'verified', evidence: uuid } : { state: 'no-effect' }
} },
{ id: 'next', kind: 'read', run: async ({ evidence }) => { assert.equal(evidence.get('apply'), uuid); return null } },
])
assert.deepEqual(await resumeMcpSteps(config), { status: 'failed', step: 'apply' })
assert.deepEqual(await resumeMcpSteps(config), { status: 'reconcile', step: 'apply' })
verified = true
assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' })
assert.equal(sends, 1)
})
test('cannot resume a different fixture or an altered ordered step list', async t => {
const config = options(t, [{ id: 'read', kind: 'read', run: async () => null }])
await resumeMcpSteps(config)
await assert.rejects(resumeMcpSteps({ ...config, fixture: 'other' }), /fixture or ordered step/)
await assert.rejects(resumeMcpSteps({ ...config, steps: [{ ...config.steps[0], id: 'other' }] }), /fixture or ordered step/)
})
test('refuses a stored retry marker after an accepted apply', async t => {
const config = options(t, [{ id: 'apply', kind: 'apply', async run({ accept }) {
accept(uuid)
throw new Error('lost reply')
} }])
await resumeMcpSteps(config)
appendFileSync(config.journal, JSON.stringify({ id: 'apply', attempt: 1, phase: 'retryable', accepted: null }) + '\n')
await assert.rejects(resumeMcpSteps(config), /Cannot retry an accepted MCP step/)
})
test('the poll scenario resumes owner review and then reads the same accepted operation', async t => {
const { journal, fixture } = options(t, [])
const calls = []
let approved = false
let running = true
let storageBusy = true
const config = { journal, fixture, serverId: 3, connectionId, assertApproved: async id => {
assert.equal(id, uuid)
if (!approved) throw Error('approval not visible')
}, async call(tool, args) {
calls.push(tool)
if (tool === 'mm_server_poll_plan') { assert.deepEqual(args, { serverId: 3 }); return { planId: uuid } }
if (tool === 'mm_change_apply') {
assert.equal(args.planId, uuid)
assert.match(args.idempotencyKey, /^acceptance-[0-9a-f]{64}$/)
return { id: operationId }
}
assert.deepEqual(args, { operationId })
if (storageBusy) throw Object.assign(new Error('MCP storage busy'), {
name: 'SdkHttpError', status: 503, data: { text: '{"code":"PROTOCOL_STORAGE_BUSY"}' },
})
return { id: operationId, plan_id: uuid, connection_id: connectionId,
...(running ? { state: 'running' } : { state: 'applied', verification: 'passed',
steps: [{ state: 'verified', result: pollReceipt }] }) }
} }
assert.equal((await resumeServerPoll(config)).step, 'manager.plan-approved')
approved = true
assert.equal((await resumeServerPoll(config)).step, 'mm_operation_get')
storageBusy = false
assert.equal((await resumeServerPoll(config)).step, 'mm_operation_get')
running = false
assert.deepEqual(await resumeServerPoll(config), { status: 'passed' })
await assert.rejects(resumeServerPoll({ ...config, connectionId: uuid }), /fixture or ordered step/)
assert.deepEqual(calls, ['mm_server_poll_plan', 'mm_change_apply', 'mm_operation_get', 'mm_operation_get', 'mm_operation_get'])
const failures = readFileSync(journal, 'utf8').trim().split('\n').map(line => JSON.parse(line))
.filter(entry => entry.id === 'mm_operation_get' && entry.phase === 'failed')
assert.deepEqual(failures.map(entry => entry.code), ['PROTOCOL_STORAGE_BUSY', 'POLL_PENDING'])
})
test('recovers a lost poll apply response by the exact connection, plan and idempotency key', async t => {
const { journal, fixture } = options(t, [])
let key, sends = 0, wrongBinding = true
const config = { journal, fixture, serverId: 3, connectionId, assertApproved: async () => {},
async findAcceptedOperation(binding) {
assert.equal(binding.connectionId, connectionId)
assert.equal(binding.planId, uuid)
assert.equal(binding.idempotencyKey, key)
return operationId
},
async call(tool, args) {
if (tool === 'mm_server_poll_plan') return { planId: uuid }
if (tool === 'mm_change_apply') {
sends++
key = args.idempotencyKey
throw Error('apply response lost')
}
return { id: operationId, plan_id: uuid, connection_id: wrongBinding ? uuid : connectionId,
state: 'applied', verification: 'passed', steps: [{ state: 'verified', result: pollReceipt }] }
},
}
assert.equal((await resumeServerPoll(config)).step, 'mm_change_apply')
assert.equal((await resumeServerPoll(config)).step, 'mm_operation_get')
wrongBinding = false
assert.deepEqual(await resumeServerPoll(config), { status: 'passed' })
assert.equal(sends, 1)
})
test('rejects incomplete or misbound terminal poll receipts before recording success', async t => {
const { journal, fixture } = options(t, [])
const calls = []
const receipts = [
{ ...pollReceipt, operationId: uuid },
{ ...pollReceipt, receiptId: undefined },
{ ...pollReceipt, confirmedPublications: 21 },
{ ...pollReceipt, confirmedPublications: 0, completedPublications: 0 },
{ ...pollReceipt, maintenanceComplete: false },
{ ...pollReceipt, snapshot: { serverId: 4, status: 'online' } },
pollReceipt,
]
const config = { journal, fixture, serverId: 3, connectionId, assertApproved: async () => {},
async call(tool, args) {
calls.push(tool)
if (tool === 'mm_server_poll_plan') return { planId: uuid }
if (tool === 'mm_change_apply') return { id: operationId }
assert.deepEqual(args, { operationId })
return { id: operationId, plan_id: uuid, connection_id: connectionId, state: 'applied', verification: 'passed',
steps: [{ state: 'verified', result: receipts.shift() }] }
},
}
for (let i = 0; i < 6; i++) {
assert.deepEqual(await resumeServerPoll(config), { status: 'failed', step: 'mm_operation_get' })
}
assert.deepEqual(await resumeServerPoll(config), { status: 'passed' })
assert.deepEqual(calls, ['mm_server_poll_plan', 'mm_change_apply', ...Array(7).fill('mm_operation_get')])
})
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-require-imports -- standalone CommonJS Playwright script */
const fs = require('fs'), assert = require('node:assert/strict'), { createHash } = require('crypto'); const fs = require('fs'), assert = require('node:assert/strict'), { createHash } = require('crypto');
const { chromium, expect } = require('@playwright/test'); const { chromium, expect } = require('@playwright/test');
(async () => { (async () => {
@@ -20,18 +20,7 @@ test('fixed artifact is reproducible, resolves the compiled backend graph, and r
const packageJson=JSON.parse(await readFile(path.join(backend,'package.json'),'utf8')) const packageJson=JSON.parse(await readFile(path.join(backend,'package.json'),'utf8'))
await mkdir(projectBackend,{recursive:true});await writeFile(path.join(projectBackend,'package.json'),JSON.stringify({type:'module',dependencies:packageJson.dependencies})) await mkdir(projectBackend,{recursive:true});await writeFile(path.join(projectBackend,'package.json'),JSON.stringify({type:'module',dependencies:packageJson.dependencies}))
const first=await buildDatabaseOperator({backendDirectory:backend,outputDirectory:output}),second=await buildDatabaseOperator({backendDirectory:backend,outputDirectory:output}) const first=await buildDatabaseOperator({backendDirectory:backend,outputDirectory:output}),second=await buildDatabaseOperator({backendDirectory:backend,outputDirectory:output})
assert.deepEqual(first,second);assert.equal(first.modules.length,OPERATOR_SOURCES.length);assert.equal(first.modules.length,31);assert.deepEqual(first.runtimeDependencies,['dotenv','pg','zod']);assert.equal(first.backendDistModules.length,30) assert.deepEqual(first,second);assert.equal(first.modules.length,OPERATOR_SOURCES.length);assert.equal(first.modules.length,20);assert.deepEqual(first.runtimeDependencies,['dotenv','pg','zod']);assert.equal(first.backendDistModules.length,12)
assert(first.modules.some(item=>item.file==='writer-profile/registry-enrollment.mjs'))
assert(first.modules.some(item=>item.file==='writer-profile/marker-fence-enrollment.mjs'))
assert(first.modules.some(item=>item.file==='writer-profile/fixed58-build-definition.js'))
assert(first.backendDistImports.includes('db/database-restore-quarantine-profile.js'))
assert(first.backendDistImports.includes('db/database-source-activation-record.js'))
assert(first.backendDistImports.includes('services/server-probe-legacy-source-inventory.js'))
assert(first.backendDistImports.includes('services/server-probe-legacy-archive-approval.js'))
assert(first.backendDistImports.includes('services/server-probe-legacy-handoff-inspection.js'))
assert(first.backendDistImports.includes('db/legacy-probe-0037-body.js'))
assert(first.backendDistImports.includes('db/controlled-schema-reference-core.js'))
assert(first.backendDistImports.includes('services/database-dump-connection.js'))
const copied=new Set() const copied=new Set()
async function copyDist(relative){ async function copyDist(relative){
if(copied.has(relative))return;copied.add(relative) if(copied.has(relative))return;copied.add(relative)
@@ -50,7 +39,7 @@ test('fixed artifact is reproducible, resolves the compiled backend graph, and r
assert.equal(await stageDatabaseOperatorScripts(scripts,path.join(backend,'scripts')),OPERATOR_SOURCES.length+2) assert.equal(await stageDatabaseOperatorScripts(scripts,path.join(backend,'scripts')),OPERATOR_SOURCES.length+2)
const buildScript=path.join(scripts,'build-database-operator.mjs') const buildScript=path.join(scripts,'build-database-operator.mjs')
const built=spawnSync(process.execPath,[buildScript],{cwd:temporary,env:{...process.env,NODE_OPTIONS:''},encoding:'utf8',timeout:30000}) const built=spawnSync(process.execPath,[buildScript],{cwd:temporary,env:{...process.env,NODE_OPTIONS:''},encoding:'utf8',timeout:30000})
assert.equal(built.status,0,built.stderr);assert.equal(JSON.parse(built.stdout.trim()).modules,first.modules.length) assert.equal(built.status,0,built.stderr);assert.equal(JSON.parse(built.stdout.trim()).modules,20)
assert.deepEqual(JSON.parse(await readFile(path.join(output,'build-manifest.json'),'utf8')),first) assert.deepEqual(JSON.parse(await readFile(path.join(output,'build-manifest.json'),'utf8')),first)
const configFile=path.join(projectBackend,'dist','config.js'),originalConfig=await readFile(configFile,'utf8') const configFile=path.join(projectBackend,'dist','config.js'),originalConfig=await readFile(configFile,'utf8')
for(const injected of ["import 'tsx';","import '../src/config.js';","import '../outside.js';"]){ for(const injected of ["import 'tsx';","import '../src/config.js';","import '../outside.js';"]){
@@ -72,30 +61,12 @@ test('fixed artifact is reproducible, resolves the compiled backend graph, and r
const modulesToImport=first.modules.map(item=>path.join(output,item.file)) const modulesToImport=first.modules.map(item=>path.join(output,item.file))
const importer=path.join(temporary,'import-all.mjs');await writeFile(importer,`import {pathToFileURL} from 'node:url';for(const file of ${JSON.stringify(modulesToImport)})await import(pathToFileURL(file).href);console.log(JSON.stringify({imported:${modulesToImport.length},readyAuthorized:false}));`) const importer=path.join(temporary,'import-all.mjs');await writeFile(importer,`import {pathToFileURL} from 'node:url';for(const file of ${JSON.stringify(modulesToImport)})await import(pathToFileURL(file).href);console.log(JSON.stringify({imported:${modulesToImport.length},readyAuthorized:false}));`)
const imported=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,importer],{cwd:temporary,env,encoding:'utf8',timeout:30000}) const imported=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,importer],{cwd:temporary,env,encoding:'utf8',timeout:30000})
assert.equal(imported.status,0,imported.stderr);assert.equal(JSON.parse(imported.stdout.trim()).imported,first.modules.length);assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0) assert.equal(imported.status,0,imported.stderr);assert.equal(JSON.parse(imported.stdout.trim()).imported,20);assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0)
const refusals=[] const refusals=[]
for(const [entry,event,args] of [['database-writer-bootstrap-operator.mjs','application-writer-bootstrap-refused',['invalid']],['database-writer-profile-operator.mjs','application-writer-profile-publication-refused',['invalid']],['database-writer-profile-operator.mjs','database-maintenance-command-refused',['maintenance-inspect']],['database-writer-profile-operator.mjs','database-maintenance-command-refused',['maintenance-resolve']],['database-writer-profile-operator.mjs','database-source-activation-refused',['source-activate']],['database-writer-profile-operator.mjs','database-legacy-inspection-refused',['legacy-inspect']],['database-writer-profile-operator.mjs','application-writer-registry-enrollment-refused',['registry-enroll']],['database-writer-profile-operator.mjs','application-writer-marker-fence-enrollment-refused',['marker-fence-enroll']],['database-writer-profile-operator.mjs','application-writer-restore-profile-enrollment-refused',['restore-profile-enroll']]]){ for(const [entry,event,args] of [['database-writer-bootstrap-operator.mjs','application-writer-bootstrap-refused',['invalid']],['database-writer-profile-operator.mjs','application-writer-profile-publication-refused',['invalid']],['database-writer-profile-operator.mjs','database-maintenance-command-refused',['maintenance-inspect']],['database-writer-profile-operator.mjs','database-maintenance-command-refused',['maintenance-resolve']],['database-writer-profile-operator.mjs','database-source-activation-refused',['source-activate']]]){
const child=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,path.join(output,entry),...args],{cwd:temporary,env,encoding:'utf8',timeout:30000}) const child=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,path.join(output,entry),...args],{cwd:temporary,env,encoding:'utf8',timeout:30000})
assert.equal(child.status,1,child.stderr);assert.equal(child.stdout,'');assert.equal(JSON.parse(child.stderr.trim()).event,event);assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0);refusals.push(event) assert.equal(child.status,1,child.stderr);assert.equal(child.stdout,'');assert.equal(JSON.parse(child.stderr.trim()).event,event);assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0);refusals.push(event)
} }
const catalogue=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,path.join(output,'database-writer-profile-operator.mjs'),'source-catalogue-inspect'],{cwd:temporary,env,encoding:'utf8',timeout:30000})
assert.equal(catalogue.status,1,catalogue.stderr);assert.equal(catalogue.stdout,'');assert.equal(JSON.parse(catalogue.stderr.trim()).event,'application-writer-source-catalogue-inspection-refused')
assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0);refusals.push('application-writer-source-catalogue-inspection-refused')
for(const command of ['legacy-export','legacy-custody','legacy-handoff-inspect','legacy-cold-verify','legacy-handoff-execute','legacy-handoff-adopt','legacy-handoff-release','legacy-control-reference','legacy-source58-preflight','source58-foundation-enroll','source57-admission-enroll']){
const invalid=spawnSync(process.execPath,['--import',pathToFileURL(guard).href,path.join(output,'database-writer-profile-operator.mjs'),command],{cwd:temporary,env,encoding:'utf8',timeout:30000})
const event={
'legacy-handoff-inspect':'database-legacy-handoff-inspection-refused','legacy-cold-verify':'database-legacy-cold-proof-refused',
'legacy-handoff-execute':'database-legacy-handoff-execution-refused',
'legacy-handoff-adopt':'database-legacy-handoff-adoption-refused',
'legacy-handoff-release':'database-legacy-handoff-release-refused',
'legacy-control-reference':'database-legacy-control-reference-refused',
'legacy-source58-preflight':'database-legacy-source58-preflight-refused',
'source58-foundation-enroll':'database-source58-foundation-refused',
'source57-admission-enroll':'database-source57-admission-refused',
}[command]??`database-${command}-refused`
assert.equal(invalid.status,1,invalid.stderr);assert.equal(invalid.stdout,'');assert.equal(JSON.parse(invalid.stderr.trim()).event,event)
assert.equal(JSON.parse(await readFile(telemetry,'utf8')).connections,0);refusals.push(event)
}
const receipt={node:process.version,projection:temporary,modules:first.modules.length,compiledBackendModules:copied.size,buildCliExitCode:built.status,importedModules:modulesToImport.length,invalidArgumentsRefused:refusals,developmentDependenciesBlocked:denied,networkConnections:0,productionActivated:false,readyAuthorized:false} const receipt={node:process.version,projection:temporary,modules:first.modules.length,compiledBackendModules:copied.size,buildCliExitCode:built.status,importedModules:modulesToImport.length,invalidArgumentsRefused:refusals,developmentDependenciesBlocked:denied,networkConnections:0,productionActivated:false,readyAuthorized:false}
await writeFile(path.join(temporary,'acceptance-receipt.json'),JSON.stringify(receipt,null,2)+'\n');console.log(JSON.stringify(receipt)) await writeFile(path.join(temporary,'acceptance-receipt.json'),JSON.stringify(receipt,null,2)+'\n');console.log(JSON.stringify(receipt))
}) })
@@ -3,15 +3,8 @@ export const OPERATOR_SOURCES=Object.freeze([
'database-writer-bootstrap-operator.mjs','database-writer-profile-operator.mts', 'database-writer-bootstrap-operator.mjs','database-writer-profile-operator.mts',
'writer-profile/operator-files.mjs','writer-profile/operator-contract.mjs','writer-profile/publisher.mjs','writer-profile/context.mjs', 'writer-profile/operator-files.mjs','writer-profile/operator-contract.mjs','writer-profile/publisher.mjs','writer-profile/context.mjs',
'writer-profile/protected-source-registry.mts','writer-profile/maintenance-resolution.mts','writer-profile/source-activation.mts', 'writer-profile/protected-source-registry.mts','writer-profile/maintenance-resolution.mts','writer-profile/source-activation.mts',
'writer-profile/legacy-cold-verification.mts',
'writer-profile/legacy-handoff-execution.mts',
'writer-profile/legacy-control-reference.mts',
'writer-profile/source58-foundation.mts',
'writer-profile/source57-admission-plan.mts',
'writer-profile/registry-enrollment.mts',
'writer-profile/marker-fence-enrollment.mts',
'writer-profile/catalogue.mts','writer-profile/catalogue-value-bridge.ts','writer-profile/catalogue-projection.ts','writer-profile/catalogue-leaves.ts', 'writer-profile/catalogue.mts','writer-profile/catalogue-value-bridge.ts','writer-profile/catalogue-projection.ts','writer-profile/catalogue-leaves.ts',
'writer-profile/application-writer-profile-catalogue.ts','writer-profile/application-writer-profile-catalogue-values.ts', 'writer-profile/application-writer-profile-catalogue.ts','writer-profile/application-writer-profile-catalogue-values.ts',
'writer-profile/application-writer-profile-catalogue-parent-policy.ts','writer-profile/application-writer-profile-canonical.ts', 'writer-profile/application-writer-profile-catalogue-parent-policy.ts','writer-profile/application-writer-profile-canonical.ts',
'writer-profile/fixed44-build-sql.ts','writer-profile/fixed44-build-definition.ts','writer-profile/fixed56-build-definition.ts','writer-profile/fixed56-build-delta.ts','writer-profile/fixed57-build-definition.ts','writer-profile/fixed58-build-definition.ts','writer-profile/expanded44-data-policy.ts', 'writer-profile/fixed44-build-sql.ts','writer-profile/fixed44-build-definition.ts','writer-profile/expanded44-data-policy.ts',
]) ])
@@ -2,38 +2,20 @@
import assert from 'node:assert/strict' import assert from 'node:assert/strict'
import path from 'node:path' import path from 'node:path'
import {pathToFileURL} from 'node:url' import {pathToFileURL} from 'node:url'
import {createPrivateKey,createPublicKey,sign} from 'node:crypto' import {createPublicKey} from 'node:crypto'
import {readdir} from 'node:fs/promises' import {readProtected,protectedDirectory,sha} from './writer-profile/operator-files.mjs'
import {readProtected,protectedDirectory,sha,withJournalLease,writeFile} from './writer-profile/operator-files.mjs' import {approval,reconciliationApproval,poolOptions,roleKeys,assertJsDriverEnvironment} from './writer-profile/operator-contract.mjs'
import {approval,upgradeApproval,reconciliationApproval,poolOptions,roleKeys,assertJsDriverEnvironment} from './writer-profile/operator-contract.mjs'
import {deploymentRequest} from './database-writer-bootstrap-operator.mjs' import {deploymentRequest} from './database-writer-bootstrap-operator.mjs'
const hex=(v:unknown)=>typeof v==='string'&&/^[a-f0-9]{64}$/.test(v) const hex=(v:unknown)=>typeof v==='string'&&/^[a-f0-9]{64}$/.test(v)
const parse=(b:Buffer)=>JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(b)) const parse=(b:Buffer)=>JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(b))
const sourceCommands=['source-activate','source-revoke','source-status'] const sourceCommands=['source-activate','source-revoke','source-status']
export async function main(args:string[]){ export async function main(args:string[]){
if(args[0]==='legacy-cold-verify')return (await import('./writer-profile/legacy-cold-verification.mjs')).verifyLegacyColdMain(args)
if(args[0]==='source58-foundation-enroll'||args[0]==='source57-admission-enroll')return (await import('./writer-profile/source58-foundation.mjs')).source58FoundationMain(args)
if(args[0]==='legacy-control-reference'||args[0]==='legacy-source58-preflight')return (await import('./writer-profile/legacy-control-reference.mjs')).legacyControlReferenceMain(args)
if(['legacy-handoff-execute','legacy-handoff-adopt','legacy-handoff-release'].includes(args[0]))return (await import('./writer-profile/legacy-handoff-execution.mjs')).legacyHandoffExecuteMain(args)
if(['registry-enroll','registry-inspect','restore-profile-enroll'].includes(args[0]))return (await import('./writer-profile/registry-enrollment.mjs')).registryEnrollmentMain(args)
if(args[0]==='marker-fence-enroll')return (await import('./writer-profile/marker-fence-enrollment.mjs')).markerFenceEnrollmentMain(args)
if(sourceCommands.includes(args[0]))return (await import('./writer-profile/source-activation.mjs')).sourceActivationMain(args) if(sourceCommands.includes(args[0]))return (await import('./writer-profile/source-activation.mjs')).sourceActivationMain(args)
if(['maintenance-inspect','maintenance-resolve'].includes(args[0]))return (await import('./writer-profile/maintenance-resolution.mjs')).maintenanceMain(args) if(['maintenance-inspect','maintenance-resolve'].includes(args[0]))return (await import('./writer-profile/maintenance-resolution.mjs')).maintenanceMain(args)
const legacyCustody=args[0]==='legacy-custody'
const legacyHandoff=args[0]==='legacy-handoff-inspect'
const legacyExport=legacyCustody||args[0]==='legacy-export'
const legacyInspection=legacyExport||legacyHandoff||args[0]==='legacy-inspect'
const catalogueInspection=args[0]==='source-catalogue-inspect'
const activePreflight=args[0]==='source-preflight',preflight=activePreflight||args[0]==='preflight' const activePreflight=args[0]==='source-preflight',preflight=activePreflight||args[0]==='preflight'
const upgrade=args[0]==='upgrade' const flags=['bootstrap','bootstrap-sha256','approval','approval-sha256','definition','definition-sha256','transport','transport-sha256','ca','source-registry','source-spki-sha256','journal']
const writes=upgrade||args[0]==='publish' if(activePreflight)flags.push('activation-directory','activation-sha256')
const flags=['bootstrap','bootstrap-sha256','transport','transport-sha256','ca','source-registry','source-spki-sha256'] assert((preflight||['publish','reconcile'].includes(args[0]))&&args.length===1+2*flags.length)
if(!legacyInspection)flags.push('definition','definition-sha256')
if(!legacyInspection&&!catalogueInspection)flags.push('approval','approval-sha256','journal')
if(legacyInspection||catalogueInspection||activePreflight||writes)flags.push('activation-directory','activation-sha256')
if(legacyExport)flags.push('archive-directory','source-signing-key')
if(legacyHandoff)flags.push('archive-directory','handoff-intent-sha256')
assert((legacyInspection||catalogueInspection||preflight||writes||args[0]==='reconcile')&&args.length===1+2*flags.length)
assertJsDriverEnvironment(process.env) assertJsDriverEnvironment(process.env)
const values=new Map<string,string>() const values=new Map<string,string>()
for(let i=1;i<args.length;i+=2){assert(args[i].startsWith('--')&&flags.includes(args[i].slice(2))&&!values.has(args[i].slice(2))&&args[i+1]);values.set(args[i].slice(2),args[i+1])} for(let i=1;i<args.length;i+=2){assert(args[i].startsWith('--')&&flags.includes(args[i].slice(2))&&!values.has(args[i].slice(2))&&args[i+1]);values.set(args[i].slice(2),args[i+1])}
@@ -43,78 +25,38 @@ export async function main(args:string[]){
assert(bootstrap.format==='application-writer-bootstrap-v1'&&bootstrap.publicationAuthorized===false&&bootstrap.readyAuthorized===false) assert(bootstrap.format==='application-writer-bootstrap-v1'&&bootstrap.publicationAuthorized===false&&bootstrap.readyAuthorized===false)
const request=deploymentRequest(bootstrap.deploymentRequest),subject=bootstrap.verificationSubject const request=deploymentRequest(bootstrap.deploymentRequest),subject=bootstrap.verificationSubject
assert(subject.installationId===request.installationId&&hex(subject.publicKeySpkiSha256)&&sha(createPublicKey(subject.publicKeyPem).export({format:'der',type:'spki'}))===subject.publicKeySpkiSha256) assert(subject.installationId===request.installationId&&hex(subject.publicKeySpkiSha256)&&sha(createPublicKey(subject.publicKeyPem).export({format:'der',type:'spki'}))===subject.publicKeySpkiSha256)
const pinned=legacyInspection||catalogueInspection?undefined:(args[0]==='reconcile'?reconciliationApproval:upgrade?upgradeApproval:approval)(parse(await readProtected(get('approval'),16384,get('approval-sha256')))) const pinned=(args[0]==='reconcile'?reconciliationApproval:approval)(parse(await readProtected(get('approval'),16384,get('approval-sha256'))))
const definition=legacyInspection?undefined:parse(await readProtected(get('definition'),4194304,get('definition-sha256'))) const definition=parse(await readProtected(get('definition'),4194304,get('definition-sha256')))
const transport=parse(await readProtected(get('transport'),16384,get('transport-sha256'))) const transport=parse(await readProtected(get('transport'),16384,get('transport-sha256')))
const ca=await readProtected(get('ca'),65536,request.tls.caSha256) const ca=await readProtected(get('ca'),65536,request.tls.caSha256)
const options=poolOptions(transport,request,ca) const options=poolOptions(transport,request,ca)
if(!legacyInspection&&!catalogueInspection)await protectedDirectory(get('journal')) await protectedDirectory(get('journal'));await protectedDirectory(get('source-registry'))
await protectedDirectory(get('source-registry'))
const privateKeyFile=path.join(path.dirname(get('bootstrap')),'signing-private.pem') const privateKeyFile=path.join(path.dirname(get('bootstrap')),'signing-private.pem')
if(writes){const key=await readProtected(privateKeyFile,8192);key.fill(0)} if(args[0]==='publish'){const key=await readProtected(privateKeyFile,8192);key.fill(0)}
// All explicit trust files are checked before app helper/PG imports. No .env URL is used. // All explicit trust files are checked before app helper/PG imports. No .env URL is used.
// Existing source helper loads config/dotenv. Complete it BEFORE pg's first // Existing source helper loads config/dotenv. Complete it BEFORE pg's first
// import so a newly loaded NODE_PG_FORCE_NATIVE cannot select another driver. // import so a newly loaded NODE_PG_FORCE_NATIVE cannot select another driver.
const {createProtectedDatabaseSourceBindingVerifier}=await import('./writer-profile/protected-source-registry.mjs') const {createProtectedDatabaseSourceBindingVerifier}=await import('./writer-profile/protected-source-registry.mjs')
assertJsDriverEnvironment(process.env) assertJsDriverEnvironment(process.env)
const [{default:pg},{createConnectedPublisher,requireCurrentCatalogueDefinition},{collectSource44Catalogue,collectSource56Catalogue,collectSource57Catalogue,collectSource58Catalogue,approvedDefinition}]=await Promise.all([ const [{default:pg},{createConnectedPublisher,requireCurrentCatalogueDefinition},{collectSource44Catalogue,approvedDefinition}]=await Promise.all([
import('pg'),import('./writer-profile/publisher.mjs'),import('./writer-profile/catalogue.mjs')]) import('pg'),import('./writer-profile/publisher.mjs'),import('./writer-profile/catalogue.mjs')])
assertJsDriverEnvironment(process.env) assertJsDriverEnvironment(process.env)
const reviewedDefinition=legacyInspection?undefined:approvedDefinition(definition) const reviewedDefinition=approvedDefinition(definition)
const collectCatalogues={44:collectSource44Catalogue,56:collectSource56Catalogue,57:collectSource57Catalogue,58:collectSource58Catalogue} assert.equal(reviewedDefinition.definitionSha256,pinned.definitionSha256)
const collectCatalogue=collectCatalogues[reviewedDefinition&&'edition' in reviewedDefinition?reviewedDefinition.edition:44] if(args[0]!=='reconcile')requireCurrentCatalogueDefinition(pinned,reviewedDefinition)
if(!legacyInspection&&!catalogueInspection){
assert.equal(reviewedDefinition!.definitionSha256,pinned!.definitionSha256)
if(args[0]!=='reconcile')requireCurrentCatalogueDefinition(pinned,reviewedDefinition)
}
const installation=Object.freeze({...subject,roles:Object.fromEntries(roleKeys.map(k=>[k,request.roles[k].signedOid])),roleBindings:request.roles,expectedSource:request.source,tls:Object.freeze({...request.tls})}) const installation=Object.freeze({...subject,roles:Object.fromEntries(roleKeys.map(k=>[k,request.roles[k].signedOid])),roleBindings:request.roles,expectedSource:request.source,tls:Object.freeze({...request.tls})})
const sourcePin={registryDirectory:get('source-registry'),installationId:subject.installationId,publicKeySpkiSha256:get('source-spki-sha256'),currentHeadSha256:request.sourceHeadSha256} const sourcePin={registryDirectory:get('source-registry'),installationId:subject.installationId,publicKeySpkiSha256:get('source-spki-sha256'),currentHeadSha256:request.sourceHeadSha256}
const archiveDirectory=legacyExport?get('archive-directory'):undefined
let handoff
if(legacyHandoff){
const {verifyLegacyProbeHandoffIntent}=await import('../src/services/server-probe-legacy-handoff-inspection.js')
const directory=get('archive-directory'),maximum=64*1024*1024
const [archive,signedApproval,coldArchive,intent,operatorPublicKeyPem]=await Promise.all([
readProtected(path.join(directory,'legacy-probe.dump'),maximum),readProtected(path.join(directory,'legacy-probe-approval.json'),4096),
readProtected(path.join(directory,'legacy-probe-cold.dump'),maximum),
readProtected(path.join(directory,'legacy-probe-handoff-intent.json'),4096,get('handoff-intent-sha256')),
readProtected(path.join(sourcePin.registryDirectory,'operator-public.pem'),4096),
])
handoff=await verifyLegacyProbeHandoffIntent(sourcePin,{archive,signedApproval,coldArchive,intent,operatorPublicKeyPem})
}
// Use the same protected registry evidence for inspection and publication. // Use the same protected registry evidence for inspection and publication.
const verifier=createProtectedDatabaseSourceBindingVerifier(sourcePin) const verifier=createProtectedDatabaseSourceBindingVerifier(sourcePin)
async function connected(sourceVerifier:typeof verifier,deadlineAt:number){ async function connected(sourceVerifier:typeof verifier,deadlineAt:number){
const publisher=createConnectedPublisher(installation,(s:Parameters<typeof collectCatalogue>[0])=>collectCatalogue(s,installation),sourceVerifier,pinned,reviewedDefinition) const publisher=createConnectedPublisher(installation,(s:Parameters<typeof collectSource44Catalogue>[0])=>collectSource44Catalogue(s,installation),sourceVerifier,pinned,reviewedDefinition)
const pool=new pg.Pool(options) const pool=new pg.Pool(options)
// A pool-level async error never becomes raw stdout or a retry signal. // A pool-level async error never becomes raw stdout or a retry signal.
let asyncFailure=false;pool.on('error',()=>{asyncFailure=true}) let asyncFailure=false;pool.on('error',()=>{asyncFailure=true})
let result,primary:unknown,failed=false let result,primary:unknown,failed=false
try{ try{result=preflight?await publisher.preflightCurrentProfile(pool,{deadlineAt}):args[0]==='reconcile'?await publisher.reconcilePendingPublication(pool,{journalDirectory:get('journal'),deadlineAt}):await publisher.publishCurrentProfile(pool,{privateKeyFile,expectedHeadSha256:pinned.expectedHeadSha256,journalDirectory:get('journal'),validityMs:pinned.validityMs,deadlineAt})}
const journalDirectory=get('journal')
if(legacyInspection)result=await publisher.inspectLegacyStorage(pool,{deadlineAt,archiveDirectory,custody:legacyCustody,handoff})
else if(catalogueInspection)result=await publisher.inspectCurrentCatalogue(pool,{deadlineAt})
else if(preflight)result=await publisher.preflightCurrentProfile(pool,{deadlineAt})
else if(args[0]==='reconcile')result=await publisher.reconcilePendingPublication(pool,{journalDirectory,deadlineAt})
else{
const publication={privateKeyFile,expectedHeadSha256:pinned.expectedHeadSha256,journalDirectory,validityMs:pinned.validityMs,deadlineAt}
result=upgrade?await publisher.upgradeCurrentProfile(pool,publication):await publisher.publishCurrentProfile(pool,publication)
}
}
catch(error){primary=error;failed=true} catch(error){primary=error;failed=true}
try{await pool.end()}catch(error){primary??=error;failed=true} try{await pool.end()}catch(error){primary??=error;failed=true}
if(legacyInspection&&result?.state==='observed'&&result.source.platformProtectionVerified===true&&!failed&&!asyncFailure){
const {installationId,sourceIdentityId,generation,ordinal,headSha256,bindingSha256}=result.source
if(legacyHandoff){assert(result.resolution);return {event:'database-legacy-handoff-inspection',...result.resolution,sourceActivationMatched:true,conversionAllowed:false}}
return {...(legacyExport?{archive:result.archive}:{}),...(result.lineageSnapshot?{lineageSnapshot:result.lineageSnapshot}:{}),event:'database-legacy-inspection',outcome:'observed',snapshot:result.snapshot,
source:{installationId,sourceIdentityId,generation,ordinal,headSha256,bindingSha256},sourceActivationMatched:true,
platformProtectionVerified:true,generationAuthenticated:false,allWriterDrainAuthenticated:false,
conversionAllowed:false,readyAuthorized:false,automaticReplay:false}
}
if(catalogueInspection&&result?.state==='observed'&&result.platformProtectionVerified===true&&!failed&&!asyncFailure){
const {state:_state,...catalogue}=result
return {event:'application-writer-source-catalogue-inspection',outcome:'observed',...catalogue,automaticReplay:false,writerAuthorized:false}
}
if(preflight&&result?.state==='matched'&&result.platformProtectionVerified===true&&!failed&&!asyncFailure)return {event:'application-writer-profile-preflight',outcome:'matched',epoch:result.epoch,headSha256:result.headSha256,notAfter:result.notAfter,sourceMatched:true,rolesMatched:true,catalogueMatched:true,platformProtectionVerified:true,writerAuthorized:false,cleanupUnconfirmed:false,automaticReplay:false,generationAuthenticated:false,readyAuthorized:false} if(preflight&&result?.state==='matched'&&result.platformProtectionVerified===true&&!failed&&!asyncFailure)return {event:'application-writer-profile-preflight',outcome:'matched',epoch:result.epoch,headSha256:result.headSha256,notAfter:result.notAfter,sourceMatched:true,rolesMatched:true,catalogueMatched:true,platformProtectionVerified:true,writerAuthorized:false,cleanupUnconfirmed:false,automaticReplay:false,generationAuthenticated:false,readyAuthorized:false}
if(result?.outcome==='committed')return {event:'application-writer-profile-publication',outcome:'committed',epoch:result.epoch,headSha256:result.headSha256,notAfter:result.notAfter, if(result?.outcome==='committed')return {event:'application-writer-profile-publication',outcome:'committed',epoch:result.epoch,headSha256:result.headSha256,notAfter:result.notAfter,
cleanupUnconfirmed:result.cleanupUnconfirmed||asyncFailure||failed,journalUnconfirmed:!!result.journalUnconfirmed,automaticReplay:false,generationAuthenticated:false,readyAuthorized:false} cleanupUnconfirmed:result.cleanupUnconfirmed||asyncFailure||failed,journalUnconfirmed:!!result.journalUnconfirmed,automaticReplay:false,generationAuthenticated:false,readyAuthorized:false}
@@ -122,36 +64,12 @@ export async function main(args:string[]){
throw primary??Error('Publisher terminal unconfirmed') throw primary??Error('Publisher terminal unconfirmed')
} }
const deadlineAt=Date.now()+120000 const deadlineAt=Date.now()+120000
if(!legacyInspection&&!catalogueInspection&&!activePreflight&&!writes)return connected(verifier,deadlineAt) if(!activePreflight)return connected(verifier,deadlineAt)
const {withActiveSource}=await import('./writer-profile/source-activation.mjs') const {withActiveSourcePreflight}=await import('./writer-profile/source-activation.mjs')
const publisherRole=request.roles.publisher.name const publisherRole=request.roles.publisher.name
const expectedSource={...request.source,databaseOid:Number(request.source.databaseOid),sessionUser:publisherRole,currentUser:publisherRole} const expectedSource={...request.source,databaseOid:Number(request.source.databaseOid),sessionUser:publisherRole,currentUser:publisherRole}
const active=()=>withActiveSource({directory:get('activation-directory'),expectedSha256:get('activation-sha256'),sourcePin,expectedSource,tlsSpkiSha256:request.tls.spkiSha256,deadlineAt},verifier, const result=await withActiveSourcePreflight({directory:get('activation-directory'),expectedSha256:get('activation-sha256'),sourcePin,expectedSource,tlsSpkiSha256:request.tls.spkiSha256,deadlineAt},verifier,
guardedVerifier=>connected(guardedVerifier,deadlineAt)) guardedVerifier=>connected(guardedVerifier,deadlineAt))
if(archiveDirectory)return withJournalLease(archiveDirectory,async()=>{
assert.deepEqual(await readdir(archiveDirectory),['publisher-lease.lock'],'Empty protected archive directory required')
const keyBytes=await readProtected(get('source-signing-key'),8192)
let key
try{key=createPrivateKey(keyBytes)}finally{keyBytes.fill(0)}
assert(key.asymmetricKeyType==='ed25519'&&sha(createPublicKey(key).export({type:'spki',format:'der'}))===sourcePin.publicKeySpkiSha256,'Source signer differs from deployment pin')
const {sourceBindingBytes,sourceBindingCanonical}=await import('../src/db/database-source-binding.js')
const {LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN,LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN}=await import('../src/services/server-probe-legacy-archive-approval.js')
const result=await active()
assert(result.event==='database-legacy-inspection'&&Buffer.isBuffer(result.archive)&&Date.now()<deadlineAt)
const {archive,...receipt}=result
const base={...receipt.source,archiveSha256:sha(archive),storageSha256:receipt.snapshot.sha256}
assert(!legacyCustody||receipt.lineageSnapshot)
const body=legacyCustody?{format:'legacy-probe-custody-approval-v1',...base,lineageSha256:receipt.lineageSnapshot.sha256}
:{format:'legacy-probe-archive-approval-v1',...base}
const domain=legacyCustody?LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN:LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN
const signedApproval=sourceBindingBytes({body,signature:sign(null,Buffer.from(domain+sourceBindingCanonical(body)),key).toString('base64url')})
const dumpFile='legacy-probe.dump',approvalFile='legacy-probe-approval.json'
await writeFile(path.join(archiveDirectory,dumpFile),archive,{flag:'wx',mode:0o600})
await writeFile(path.join(archiveDirectory,approvalFile),signedApproval,{flag:'wx',mode:0o600})
return {...receipt,event:legacyCustody?'database-legacy-custody':'database-legacy-export',outcome:'exported',dumpFile,approvalFile,archiveSha256:body.archiveSha256,approvalSha256:sha(signedApproval)}
})
const result=await active()
if(writes||legacyInspection||catalogueInspection)return result
assert.equal(result.event,'application-writer-profile-preflight') assert.equal(result.event,'application-writer-profile-preflight')
return {...result,event:'application-writer-profile-source-preflight',sourceActivationMatched:true} return {...result,event:'application-writer-profile-source-preflight',sourceActivationMatched:true}
} }
@@ -159,28 +77,8 @@ if(process.argv[1]&&import.meta.url===pathToFileURL(path.resolve(process.argv[1]
try{const result=await main(process.argv.slice(2));console.log(JSON.stringify(result));if(result.cleanupUnconfirmed||result.journalUnconfirmed)process.exitCode=2} try{const result=await main(process.argv.slice(2));console.log(JSON.stringify(result));if(result.cleanupUnconfirmed||result.journalUnconfirmed)process.exitCode=2}
catch(error){ catch(error){
const sourceCommand=sourceCommands.includes(process.argv[2]) const sourceCommand=sourceCommands.includes(process.argv[2])
const command=process.argv[2], code=(error as NodeJS.ErrnoException|null)?.code const unknown=sourceCommand&&(error as NodeJS.ErrnoException|null)?.code==='SOURCE_ACTIVATION_TERMINAL_UNKNOWN'
const unknown=sourceCommand&&code==='SOURCE_ACTIVATION_TERMINAL_UNKNOWN'||['registry-enroll','restore-profile-enroll'].includes(command)&&code==='WRITER_REGISTRY_TERMINAL_UNKNOWN'||
command==='marker-fence-enroll'&&code==='MARKER_FENCE_TERMINAL_UNKNOWN'||command==='legacy-handoff-execute'&&code==='LEGACY_HANDOFF_EXECUTION_UNKNOWN'||
command==='legacy-handoff-adopt'&&code==='LEGACY_HANDOFF_ADOPTION_UNKNOWN'||command==='legacy-handoff-release'&&code==='LEGACY_HANDOFF_RELEASE_UNKNOWN'||
command==='source58-foundation-enroll'&&code==='SOURCE58_FOUNDATION_UNKNOWN'||command==='source57-admission-enroll'&&code==='SOURCE57_ADMISSION_UNKNOWN'
console.error(JSON.stringify({event:sourceCommand?(unknown?'database-source-activation-unknown':'database-source-activation-refused'): console.error(JSON.stringify({event:sourceCommand?(unknown?'database-source-activation-unknown':'database-source-activation-refused'):
command==='registry-inspect'?'application-writer-registry-inspection-refused': ['maintenance-inspect','maintenance-resolve'].includes(process.argv[2])?'database-maintenance-command-refused':process.argv[2]==='source-preflight'?'application-writer-profile-source-preflight-refused':'application-writer-profile-publication-refused',automaticReplay:false,inspectPendingBeforeRetry:true,readyAuthorized:false}));process.exitCode=unknown?2:1
command==='registry-enroll'?(unknown?'application-writer-registry-enrollment-unknown':'application-writer-registry-enrollment-refused'):
command==='restore-profile-enroll'?(unknown?'application-writer-restore-profile-enrollment-unknown':'application-writer-restore-profile-enrollment-refused'):
command==='source-catalogue-inspect'?'application-writer-source-catalogue-inspection-refused':
command==='marker-fence-enroll'?(unknown?'application-writer-marker-fence-enrollment-unknown':'application-writer-marker-fence-enrollment-refused'):
command==='legacy-export'?'database-legacy-export-refused':
command==='legacy-custody'?'database-legacy-custody-refused':
command==='legacy-handoff-inspect'?'database-legacy-handoff-inspection-refused':
command==='legacy-cold-verify'?'database-legacy-cold-proof-refused':
command==='legacy-handoff-execute'?(unknown?'database-legacy-handoff-execution-unknown':'database-legacy-handoff-execution-refused'):
command==='legacy-handoff-adopt'?(unknown?'database-legacy-handoff-adoption-unknown':'database-legacy-handoff-adoption-refused'):
command==='legacy-handoff-release'?(unknown?'database-legacy-handoff-release-unknown':'database-legacy-handoff-release-refused'):
command==='legacy-control-reference'?'database-legacy-control-reference-refused':
command==='legacy-source58-preflight'?'database-legacy-source58-preflight-refused':
command==='source58-foundation-enroll'?(unknown?'database-source58-foundation-unknown':'database-source58-foundation-refused'):
command==='source57-admission-enroll'?(unknown?'database-source57-admission-unknown':'database-source57-admission-refused'):
['maintenance-inspect','maintenance-resolve'].includes(process.argv[2])?'database-maintenance-command-refused':process.argv[2]==='legacy-inspect'?'database-legacy-inspection-refused':process.argv[2]==='source-preflight'?'application-writer-profile-source-preflight-refused':'application-writer-profile-publication-refused',automaticReplay:false,inspectPendingBeforeRetry:true,readyAuthorized:false}));process.exitCode=unknown?2:1
} }
} }
@@ -1,10 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any -- loosely typed fixtures in an operator test */
import test from 'node:test' import test from 'node:test'
import assert from 'node:assert/strict' import assert from 'node:assert/strict'
import {generateKeyPairSync,createHash,X509Certificate,sign} from 'node:crypto' import {generateKeyPairSync,createHash,X509Certificate,sign} from 'node:crypto'
import {rootCertificates} from 'node:tls' import {rootCertificates} from 'node:tls'
import {createRequire} from 'node:module' import {createRequire} from 'node:module'
import {approval,upgradeApproval,reconciliationApproval,poolOptions,STARTUP_OPTIONS,assertJsDriverEnvironment,REBIND58_APPROVAL_FORMAT} from './writer-profile/operator-contract.mjs' import {approval,reconciliationApproval,poolOptions,STARTUP_OPTIONS,assertJsDriverEnvironment} from './writer-profile/operator-contract.mjs'
import {bytes,canonical,POLICY_DOMAIN,signedEnvelope} from '../src/db/application-writer-profile-format.ts' import {bytes,canonical,POLICY_DOMAIN,signedEnvelope} from '../src/db/application-writer-profile-format.ts'
const hash=(v:Buffer|string)=>createHash('sha256').update(v).digest('hex') const hash=(v:Buffer|string)=>createHash('sha256').update(v).digest('hex')
const pinned=()=>({format:'application-writer-publication-approval-v1',manifestSha256:'a'.repeat(64),definitionSha256:'b'.repeat(64),catalogSha256:'c'.repeat(64),expectedHeadSha256:null,validityMs:86400000,expiresAt:Date.now()+60000}) const pinned=()=>({format:'application-writer-publication-approval-v1',manifestSha256:'a'.repeat(64),definitionSha256:'b'.repeat(64),catalogSha256:'c'.repeat(64),expectedHeadSha256:null,validityMs:86400000,expiresAt:Date.now()+60000})
@@ -53,11 +52,3 @@ test('reconciliation keeps exact digest/head/schema and original lifetime bounds
test('publication expiry guard remains mandatory at the real clock boundary',()=>{ test('publication expiry guard remains mandatory at the real clock boundary',()=>{
const value=pinned();value.expiresAt=Date.now();assert.throws(()=>approval(value));assert.deepEqual(reconciliationApproval(value),value) const value=pinned();value.expiresAt=Date.now();assert.throws(()=>approval(value));assert.deepEqual(reconciliationApproval(value),value)
}) })
test('restore rebind requires a protected exact old head and changed approved catalogue',()=>{
const value={...pinned(),format:REBIND58_APPROVAL_FORMAT,previousCatalogSha256:'d'.repeat(64),expectedHeadSha256:'e'.repeat(64)}
assert.deepEqual(upgradeApproval(value),value)
assert.throws(()=>approval(value))
assert.throws(()=>upgradeApproval({...value,previousCatalogSha256:value.catalogSha256}))
assert.throws(()=>upgradeApproval({...value,expectedHeadSha256:null}))
assert.throws(()=>upgradeApproval({...value,expiresAt:1}))
})
@@ -6,6 +6,7 @@ import fs from 'node:fs/promises'
import {createRequire} from 'node:module' import {createRequire} from 'node:module'
import {runInNewContext} from 'node:vm' import {runInNewContext} from 'node:vm'
import {EventEmitter} from 'node:events' import {EventEmitter} from 'node:events'
import {generateKeyPairSync} from 'node:crypto'
import path from 'node:path' import path from 'node:path'
import * as format from '../src/db/application-writer-profile-format.ts' import * as format from '../src/db/application-writer-profile-format.ts'
import {MIGRATION_MANIFEST,migrationManifestDigest} from '../src/db/migration-manifest.ts' import {MIGRATION_MANIFEST,migrationManifestDigest} from '../src/db/migration-manifest.ts'
@@ -13,20 +14,14 @@ import {readCurrentApplicationWriterProfile} from '../src/db/application-writer-
import {syntheticFixture,stagedModelClient} from '../src/db/application-writer-profile-registry-read-model.test.ts' import {syntheticFixture,stagedModelClient} from '../src/db/application-writer-profile-registry-read-model.test.ts'
import {approvedDefinition} from './writer-profile/catalogue.mts' import {approvedDefinition} from './writer-profile/catalogue.mts'
import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './writer-profile/fixed44-build-definition.ts' import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './writer-profile/fixed44-build-definition.ts'
import {BUILD56_PROVENANCE} from './writer-profile/fixed56-build-definition.ts'
import {BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE} from './writer-profile/fixed57-build-definition.ts'
import {approval as parseApproval,upgradeApproval,reconciliationApproval,isUpgradeApprovalFormat,UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT} from './writer-profile/operator-contract.mjs'
const require=createRequire(new URL('../package.json',import.meta.url)),ts=require('typescript'),pg=require('pg') const require=createRequire(new URL('../package.json',import.meta.url)),ts=require('typescript'),pg=require('pg')
const candidate=await fs.readFile(new URL('./writer-profile/publisher.mjs',import.meta.url),'utf8') const candidate=await fs.readFile(new URL('./writer-profile/publisher.mjs',import.meta.url),'utf8')
interface PreflightDiagnostics {state:string;headSha256:string;sourceMatched:boolean;rolesMatched:boolean;catalogueMatched:boolean;platformProtectionVerified:boolean;writerAuthorized:boolean;generationAuthenticated:boolean;readyAuthorized:boolean} interface PreflightDiagnostics {state:string;headSha256:string;sourceMatched:boolean;rolesMatched:boolean;catalogueMatched:boolean;platformProtectionVerified:boolean;writerAuthorized:boolean;generationAuthenticated:boolean;readyAuthorized:boolean}
type PublishOptions={privateKeyFile:string;expectedHeadSha256:string|null;journalDirectory:string;deadlineAt:number;validityMs?:number}
interface PublisherModule { interface PublisherModule {
requireCurrentCatalogueDefinition(approval:unknown,definition:unknown):string requireCurrentCatalogueDefinition(approval:unknown,definition:unknown):string
createConnectedPublisher(installation:unknown,collect:()=>Promise<unknown>,sourceVerifier:{verify:()=>Promise<{platformProtectionVerified:boolean}>},approval:unknown,definition:unknown):{ createConnectedPublisher(installation:unknown,collect:()=>Promise<unknown>,sourceVerifier:{verify:()=>Promise<{platformProtectionVerified:boolean}>},approval:unknown,definition:unknown):{
inspectLegacyStorage(pool:unknown,options:{deadlineAt:number}):Promise<{state:string;snapshot:{rowCount:number}}>
preflightCurrentProfile(pool:unknown,options:{deadlineAt:number}):Promise<PreflightDiagnostics> preflightCurrentProfile(pool:unknown,options:{deadlineAt:number}):Promise<PreflightDiagnostics>
publishCurrentProfile(pool:unknown,options:PublishOptions):Promise<{outcome:string}> publishCurrentProfile(pool:unknown,options:{privateKeyFile:string;expectedHeadSha256:null;journalDirectory:string;deadlineAt:number}):Promise<{outcome:string}>
upgradeCurrentProfile(pool:unknown,options:PublishOptions):Promise<{outcome:string;epoch:string;headSha256:string}>
} }
} }
interface OperatorModule {main(args:string[]):Promise<{event:string;outcome:string;platformProtectionVerified:boolean;writerAuthorized:boolean;readyAuthorized:boolean;sourceActivationMatched?:boolean}>} interface OperatorModule {main(args:string[]):Promise<{event:string;outcome:string;platformProtectionVerified:boolean;writerAuthorized:boolean;readyAuthorized:boolean;sourceActivationMatched?:boolean}>}
@@ -39,16 +34,11 @@ function compile<T>(source:string,modules:Record<string,unknown>):T{
const frozenDefinition=approvedDefinition({format:'application-writer-source44-definition-approval-v1',provenance:BUILD44_PROVENANCE,template:FIXED44_ROLE_TEMPLATE}) const frozenDefinition=approvedDefinition({format:'application-writer-source44-definition-approval-v1',provenance:BUILD44_PROVENANCE,template:FIXED44_ROLE_TEMPLATE})
const frozenMigrations=MIGRATION_MANIFEST.slice(0,44),markerIds=frozenMigrations.map(pin=>pin.file.slice(0,-4)) const frozenMigrations=MIGRATION_MANIFEST.slice(0,44),markerIds=frozenMigrations.map(pin=>pin.file.slice(0,-4))
const markerSql='SELECT id FROM public.schema_migrations ORDER BY id COLLATE "C" LIMIT $1' const markerSql='SELECT id FROM public.schema_migrations ORDER BY id COLLATE "C" LIMIT $1'
function fixture(fault='',forPublication=false,upgradeMode?:'upgrade'|'upgrade57'|'ordinary-renewal'){ function fixture(fault='',forPublication=false){
const f=syntheticFixture('1'),security={sha256:'e'.repeat(64),roles:{...f.policy.roles}} const f=syntheticFixture('1'),catalogue={...frozenDefinition,catalogSha256:'f'.repeat(64),leafCount:117},security={sha256:'e'.repeat(64),roles:{...f.policy.roles}}
const predecessor=upgradeMode==='upgrade57'?BUILD56_PROVENANCE:BUILD44_PROVENANCE
const successor=upgradeMode==='upgrade57'?BUILD57_PROVENANCE:BUILD56_PROVENANCE
const target=upgradeMode?{definitionSha256:successor.templateSha256,manifestSha256:successor.manifestSha256}:frozenDefinition
const targetMigrations=MIGRATION_MANIFEST.slice(0,upgradeMode==='upgrade57'?57:56)
const catalogue={...target,catalogSha256:'f'.repeat(64),leafCount:117}
const currentBuild=fault==='current-build'||fault==='current-approved-frozen-definition' const currentBuild=fault==='current-build'||fault==='current-approved-frozen-definition'
f.policy.manifestSha256=predecessor.manifestSha256;f.policy.definitionSha256=predecessor.templateSha256 f.policy.manifestSha256=frozenDefinition.manifestSha256;f.policy.definitionSha256=frozenDefinition.definitionSha256
f.policy.catalogSha256=upgradeMode?'d'.repeat(64):format.sha(format.canonical({base:catalogue.catalogSha256,security:security.sha256})) f.policy.catalogSha256=format.sha(format.canonical({base:catalogue.catalogSha256,security:security.sha256}))
if(fault==='signed-source')f.policy.source.databaseOid='999' if(fault==='signed-source')f.policy.source.databaseOid='999'
if(fault==='signed-role')f.policy.roles.operator='99' if(fault==='signed-role')f.policy.roles.operator='99'
if(fault==='signed-catalogue')f.policy.catalogSha256='1'.repeat(64) if(fault==='signed-catalogue')f.policy.catalogSha256='1'.repeat(64)
@@ -58,66 +48,52 @@ function fixture(fault='',forPublication=false,upgradeMode?:'upgrade'|'upgrade57
const raw=f.signBody(f.policy,format.POLICY_DOMAIN),hash=format.sha(raw),top=f.state.latest[0] const raw=f.signBody(f.policy,format.POLICY_DOMAIN),hash=format.sha(raw),top=f.state.latest[0]
top.policy_sha256=hash;top.head_bytes=f.signBody({format:'application-writer-head-v1',installationId:f.subject.installationId,epoch:'1',policySha256:hash,previousHeadSha256:null},format.HEAD_DOMAIN);top.head_sha256=format.sha(top.head_bytes) top.policy_sha256=hash;top.head_bytes=f.signBody({format:'application-writer-head-v1',installationId:f.subject.installationId,epoch:'1',policySha256:hash,previousHeadSha256:null},format.HEAD_DOMAIN);top.head_sha256=format.sha(top.head_bytes)
f.state.current[0].head_sha256=top.head_sha256;f.state.policy=[{policy_sha256:hash,policy_bytes:raw}] f.state.current[0].head_sha256=top.head_sha256;f.state.policy=[{policy_sha256:hash,policy_bytes:raw}]
const priorHead=top.head_sha256
if(fault==='tamper')f.state.policy[0].policy_bytes=Buffer.from(raw.toString().replace('synthetic','tampered')) if(fault==='tamper')f.state.policy[0].policy_bytes=Buffer.from(raw.toString().replace('synthetic','tampered'))
if(fault==='empty'){f.state.current=[];f.state.latest=[];f.state.policy=[]} if(fault==='empty'){f.state.current=[];f.state.latest=[];f.state.policy=[]}
const model=stagedModelClient(f.state),queries:string[]=[],events:string[]=[],markerLimits:unknown[][]=[] const model=stagedModelClient(f.state),queries:string[]=[],events:string[]=[],markerLimits:unknown[][]=[]
const writes:{sql:string;values?:unknown[]}[]=[]
let clocks=0,checkouts=0 let clocks=0,checkouts=0
const client=Object.assign(new EventEmitter(),{query:async(sql:string,values?:unknown[])=>{ const client=Object.assign(new EventEmitter(),{query:async(sql:string,values?:unknown[])=>{
queries.push(sql) queries.push(sql)
if(/^(INSERT|UPDATE) INTO |^UPDATE mmapp_control\./.test(sql))writes.push({sql,values})
if(sql.includes('clock_timestamp()')&&++clocks===2&&fault==='late-clock')return {rows:[{now:'1'}]} if(sql.includes('clock_timestamp()')&&++clocks===2&&fault==='late-clock')return {rows:[{now:'1'}]}
if(sql===markerSql){ if(sql===markerSql){
markerLimits.push(values??[]) markerLimits.push(values??[])
const expected=upgradeMode?targetMigrations.map(pin=>pin.file.slice(0,-4)):markerIds const ids=fault==='marker-missing'?markerIds.slice(0,-1):fault==='marker-extra'||fault==='marker-drift'&&markerLimits.length===2?[...markerIds,'9999_foreign']:markerIds
const ids=fault==='marker-missing'?expected.slice(0,-1):fault==='marker-extra'||fault==='marker-drift'&&markerLimits.length===2?[...expected,'9999_foreign']:expected
return {rows:ids.map(id=>({id}))} return {rows:ids.map(id=>({id}))}
} }
if(sql.startsWith('WITH')||sql.includes('clock_timestamp()'))return model.client.query(sql,values) if(sql.startsWith('WITH')||sql.includes('clock_timestamp()'))return model.client.query(sql,values)
if(sql.startsWith('SELECT pg_catalog.pg_advisory_unlock'))return {rows:[{released:true}]} if(sql.startsWith('SELECT pg_catalog.pg_advisory_unlock'))return {rows:[{released:true}]}
if(sql==='ROLLBACK')return {rows:[],command:fault==='rollback-ack'?'':'ROLLBACK'} if(sql==='ROLLBACK')return {rows:[],command:fault==='rollback-ack'?'':'ROLLBACK'}
if(sql.startsWith('INSERT INTO mmapp_control.current')||sql.startsWith('UPDATE mmapp_control.current'))return {rows:[{id:1}]} if(sql.startsWith('INSERT INTO mmapp_control.current'))return {rows:[{id:1}]}
return {rows:[],command:sql.split(' ')[0]} return {rows:[],command:sql.split(' ')[0]}
},release:(bad:boolean)=>{events.push('release:'+bad)}}) },release:(bad:boolean)=>{events.push('release:'+bad)}})
const pool=new pg.Pool();pool.connect=async()=>{checkouts++;return client} const pool=new pg.Pool();pool.connect=async()=>{checkouts++;return client}
const source={systemIdentifier:'123',databaseOid:'456',databaseName:'synthetic',serverVersionNum:180004,inRecovery:false} const source={systemIdentifier:'123',databaseOid:'456',databaseName:'synthetic',serverVersionNum:180004,inRecovery:false}
let contexts=0,verifications=0 let contexts=0,verifications=0
const installation={...f.subject,expectedSource:source,roleBindings:{publisher:{name:'publisher',signedOid:'14'}}} const fixtureKey=forPublication?generateKeyPairSync('ed25519'):null
const pinned={expiresAt:Date.now()+60000,manifestSha256:['approval-manifest','current-approved-frozen-definition'].includes(fault)?migrationManifestDigest():target.manifestSha256,definitionSha256:catalogue.definitionSha256,catalogSha256:format.sha(format.canonical({base:catalogue.catalogSha256,security:security.sha256})),...(upgradeMode==='upgrade'||upgradeMode==='upgrade57'?{ const subject=fixtureKey?{...f.subject,publicKeyPem:fixtureKey.publicKey.export({format:'pem',type:'spki'}).toString(),publicKeySpkiSha256:format.sha(fixtureKey.publicKey.export({format:'der',type:'spki'}))}:f.subject
format:upgradeMode==='upgrade57'&&fault!=='wrong-transition'?UPGRADE57_APPROVAL_FORMAT:UPGRADE_APPROVAL_FORMAT,expectedHeadSha256:fault==='wrong-head'?'0'.repeat(64):priorHead,previousCatalogSha256:fault==='wrong-previous-catalogue'?'0'.repeat(64):'d'.repeat(64),validityMs:60000, const installation={...subject,expectedSource:source,roleBindings:{publisher:{name:'publisher',signedOid:'14'}}}
}:{})} const approval={expiresAt:Date.now()+60000,manifestSha256:['approval-manifest','current-approved-frozen-definition'].includes(fault)?migrationManifestDigest():frozenDefinition.manifestSha256,definitionSha256:catalogue.definitionSha256,catalogSha256:format.sha(format.canonical({base:catalogue.catalogSha256,security:security.sha256}))}
const definition=fault==='static-definition'?{...target,definitionSha256:'0'.repeat(64)}:target const definition=fault==='static-definition'?{...frozenDefinition,definitionSha256:'0'.repeat(64)}:frozenDefinition
const privateKeyFile=path.resolve('modeled-fixture-key.pem'),journalDirectory=path.resolve('modeled-journal'),files=new Map<string,Buffer>() const privateKeyFile=path.resolve('modeled-fixture-key.pem'),journalDirectory=path.resolve('modeled-journal'),files=new Map<string,Buffer>()
if(forPublication)files.set(privateKeyFile,Buffer.from(f.privateKey.export({format:'pem',type:'pkcs8'}))) if(fixtureKey)files.set(privateKeyFile,Buffer.from(fixtureKey.privateKey.export({format:'pem',type:'pkcs8'})))
function file(filename:string){const value=files.get(filename);if(!value)throw Object.assign(Error('Fixture file absent'),{code:'ENOENT'});return value} function file(filename:string){const value=files.get(filename);if(!value)throw Object.assign(Error('Fixture file absent'),{code:'ENOENT'});return value}
function stat(filename:string){return {size:file(filename).length,dev:1,ino:1,isFile:()=>true,isSymbolicLink:()=>false}} function stat(filename:string){return {size:file(filename).length,dev:1,ino:1,isFile:()=>true,isSymbolicLink:()=>false}}
const modules={ const modules={
pg,'node:crypto':require('node:crypto'),'node:fs/promises':{realpath:async(file:string)=>file,lstat:async(filename:string)=>stat(filename),unlink:async(filename:string)=>files.delete(filename),open:async(filename:string)=>({stat:async()=>stat(filename),readFile:async()=>Buffer.from(file(filename)),close:async()=>{}})},'node:path':path, pg,'node:crypto':require('node:crypto'),'node:fs/promises':{realpath:async(file:string)=>file,lstat:async(filename:string)=>stat(filename),unlink:async(filename:string)=>files.delete(filename),open:async(filename:string)=>({stat:async()=>stat(filename),readFile:async()=>Buffer.from(file(filename)),close:async()=>{}})},'node:path':path,
'./fixed44-build-definition.js':{BUILD44_PROVENANCE},'./fixed56-build-definition.js':{BUILD56_PROVENANCE},'./fixed57-build-definition.js':{BUILD57_PROVENANCE}, './operator-contract.mjs':{retainPublisherTlsPeer:(actual:unknown)=>{
'./operator-contract.mjs':{UPGRADE_APPROVAL_FORMAT,UPGRADE57_APPROVAL_FORMAT,isUpgradeApprovalFormat,upgradeApproval,retainPublisherTlsPeer:(actual:unknown)=>{
assert.equal(actual,client) assert.equal(actual,client)
if(fault==='initial-tls')throw Error('TLS peer refused') if(fault==='initial-tls')throw Error('TLS peer refused')
return ()=>{if(fault==='lost-tls'&&verifications===2)throw Error('Retained TLS lost')} return ()=>{if(fault==='lost-tls'&&verifications===2)throw Error('Retained TLS lost')}
}}, }},
'./operator-files.mjs':{withJournalLease:async(_directory:string,work:()=>Promise<unknown>)=>{assert(forPublication);events.push('journal-lease');return work()},writeFile:async(filename:string,value:Buffer)=>{assert(forPublication);files.set(filename,value)}}, './operator-files.mjs':{withJournalLease:async(_directory:string,work:()=>Promise<unknown>)=>{assert(forPublication);events.push('journal-lease');return work()},writeFile:async(filename:string,value:Buffer)=>{assert(forPublication);files.set(filename,value)}},
'../../src/db/database-source-observation.js':{captureDatabaseSourceObservation:async()=>{events.push('capture-source');return {}},assertDatabaseSourceObservationCurrent:async()=>{events.push('fresh-source');if(fault==='late-source')throw Error('Source changed')}}, '../../src/db/database-source-observation.js':{captureDatabaseSourceObservation:async()=>{events.push('capture-source');return {}},assertDatabaseSourceObservationCurrent:async()=>{events.push('fresh-source');if(fault==='late-source')throw Error('Source changed')}},
'../../src/services/server-probe-legacy-source-inventory.js':{captureLegacyProbeStorage:async()=>{events.push('capture-legacy');return {rowCount:1}}}, '../../src/db/migration-manifest.js':{MIGRATION_MANIFEST:currentBuild?MIGRATION_MANIFEST:frozenMigrations,migrationManifestDigest:()=>currentBuild?migrationManifestDigest():frozenDefinition.manifestSha256},
'../../src/db/migration-manifest.js':{MIGRATION_MANIFEST:currentBuild?MIGRATION_MANIFEST:upgradeMode?targetMigrations:frozenMigrations,migrationManifestDigest:()=>currentBuild?migrationManifestDigest():target.manifestSha256},
'../../src/db/application-writer-profile-format.js':format, '../../src/db/application-writer-profile-format.js':format,
'./context.mjs':{ownedContext:async()=>{contexts++;if(fault==='late-actor'&&contexts===2)throw Error('Actor changed');return {pid:1,source}},sameOwned:(a:unknown,b:unknown)=>assert.deepEqual(a,b),sourceCoordinates:(v:{source:typeof source})=>v.source,protectedStructure:async()=>{events.push('roles');if(fault==='role-structure')throw Error('Unsafe role');return security},readCurrent:forPublication&&!upgradeMode?async()=>null:readCurrentApplicationWriterProfile,HISTORY:'history',CURRENT:'current'}, './context.mjs':{ownedContext:async()=>{contexts++;if(fault==='late-actor'&&contexts===2)throw Error('Actor changed');return {pid:1,source}},sameOwned:(a:unknown,b:unknown)=>assert.deepEqual(a,b),sourceCoordinates:(v:{source:typeof source})=>v.source,protectedStructure:async()=>{events.push('roles');if(fault==='role-structure')throw Error('Unsafe role');return security},readCurrent:forPublication?async()=>null:readCurrentApplicationWriterProfile,HISTORY:'history',CURRENT:'current'},
} }
const api=compile<PublisherModule>(candidate,modules).createConnectedPublisher(installation,async()=>{events.push('catalogue');if(fault==='catalogue-eof')throw Error('Incomplete EOF');return fault==='catalogue-manifest'?{...catalogue,manifestSha256:migrationManifestDigest()}:fault==='catalogue-definition'?{...catalogue,definitionSha256:'0'.repeat(64)}:catalogue},{verify:async()=>{verifications++;events.push('verify-source');if(fault==='source-verifier')throw Error('Unsigned source');if(fault==='late-expiry'&&verifications===2)pinned.expiresAt=1;return {platformProtectionVerified:!(fault==='initial-unprotected'&&verifications===1||fault==='terminal-unprotected'&&verifications===2)}}},pinned,definition) const api=compile<PublisherModule>(candidate,modules).createConnectedPublisher(installation,async()=>{events.push('catalogue');if(fault==='catalogue-eof')throw Error('Incomplete EOF');return fault==='catalogue-manifest'?{...catalogue,manifestSha256:migrationManifestDigest()}:fault==='catalogue-definition'?{...catalogue,definitionSha256:'0'.repeat(64)}:catalogue},{verify:async()=>{verifications++;events.push('verify-source');if(fault==='source-verifier')throw Error('Unsigned source');if(fault==='late-expiry'&&verifications===2)approval.expiresAt=1;return {platformProtectionVerified:!(fault==='initial-unprotected'&&verifications===1||fault==='terminal-unprotected'&&verifications===2)}}},approval,definition)
const publication=()=>({privateKeyFile,journalDirectory,expectedHeadSha256:upgradeMode?pinned.expectedHeadSha256??priorHead:null,deadlineAt:Date.now()+30000,validityMs:60000}) return {run:()=>api.preflightCurrentProfile(pool,{deadlineAt:Date.now()+30000}),publish:()=>api.publishCurrentProfile(pool,{privateKeyFile,journalDirectory,expectedHeadSha256:null,deadlineAt:Date.now()+30000}),queries,events,markerLimits,files,get checkouts(){return checkouts},f}
return {run:()=>api.preflightCurrentProfile(pool,{deadlineAt:Date.now()+30000}),inspectLegacy:()=>api.inspectLegacyStorage(pool,{deadlineAt:Date.now()+30000}),publish:()=>api.publishCurrentProfile(pool,publication()),upgrade:()=>api.upgradeCurrentProfile(pool,publication()),queries,events,markerLimits,files,writes,priorHead,pinned,get checkouts(){return checkouts},f}
} }
test('legacy inspection requires retained TLS, terminal source proof and acknowledged rollback',async()=>{
const valid=fixture(),result=await valid.inspectLegacy()
assert.equal(result.state,'observed');assert.equal(result.snapshot.rowCount,1);assert.equal(valid.checkouts,1)
assert.deepEqual(valid.events,['capture-source','verify-source','capture-legacy','verify-source','fresh-source','release:false'])
assert.equal(valid.queries.filter(q=>q==='ROLLBACK').length,1);assert(!valid.queries.includes('COMMIT'))
for(const fault of ['initial-tls','lost-tls','late-source','rollback-ack'])await assert.rejects(fixture(fault).inspectLegacy())
})
test('current44 preflight authenticates signed current and actual component inputs on one read-only lease',async()=>{ test('current44 preflight authenticates signed current and actual component inputs on one read-only lease',async()=>{
const f=fixture(),result=await f.run();assert.equal(result.state,'matched');assert.equal(result.headSha256,f.f.state.current[0].head_sha256) const f=fixture(),result=await f.run();assert.equal(result.state,'matched');assert.equal(result.headSha256,f.f.state.current[0].head_sha256)
assert.equal(result.sourceMatched,true);assert.equal(result.rolesMatched,true);assert.equal(result.catalogueMatched,true) assert.equal(result.sourceMatched,true);assert.equal(result.rolesMatched,true);assert.equal(result.catalogueMatched,true)
@@ -169,73 +145,11 @@ test('publication checks exact marker set on both retained transactions and refu
assert(!drift.queries.some(q=>/^(?:INSERT|UPDATE|DELETE)\b/.test(q))) assert(!drift.queries.some(q=>/^(?:INSERT|UPDATE|DELETE)\b/.test(q)))
assert([...drift.files.keys()].some(filename=>filename.endsWith('pending-publication.json'))) assert([...drift.files.keys()].some(filename=>filename.endsWith('pending-publication.json')))
}) })
test('explicit 44 to 56 upgrade pins the signed predecessor, exact catalogue and CAS',async()=>{
const f=fixture('',true,'upgrade')
const approved=upgradeApproval(f.pinned)
assert.equal(approved.expectedHeadSha256,f.priorHead)
assert.throws(()=>parseApproval(approved))
for(const changed of [{expectedHeadSha256:null},{previousCatalogSha256:undefined},{expiresAt:1}]) {
assert.throws(()=>upgradeApproval({...approved,...changed}))
}
assert.equal(reconciliationApproval({...approved,expiresAt:1}).expectedHeadSha256,f.priorHead)
const result=await f.upgrade()
assert.equal(result.outcome,'committed');assert.equal(result.epoch,'2')
assert.equal(format.canonical(f.markerLimits),'[[57],[57]]')
assert.equal(f.writes.length,2)
const [history,current]=f.writes
assert.match(history.sql,/INSERT INTO mmapp_control.history/)
assert.equal(format.canonical(history.values?.slice(0,6)),format.canonical([f.f.subject.installationId,'2',result.headSha256,f.f.subject.installationId,'1',f.priorHead]))
const policy=format.signedEnvelope(history.values![7] as Buffer,'policy',f.f.subject).body
const head=format.signedEnvelope(history.values![8] as Buffer,'head',f.f.subject).body
assert.deepEqual(policy.source,f.f.policy.source);assert.deepEqual(policy.roles,f.f.policy.roles)
assert.equal(policy.manifestSha256,BUILD56_PROVENANCE.manifestSha256);assert.equal(policy.definitionSha256,BUILD56_PROVENANCE.templateSha256)
assert.equal(head.previousHeadSha256,f.priorHead)
assert.equal(format.canonical(current.values),format.canonical(['2',result.headSha256,f.f.subject.installationId,'1',f.priorHead]))
})
test('upgrade refuses drift and ordinary renewal cannot perform the 44 to 56 transition',async()=>{
for(const fault of ['wrong-head','wrong-previous-catalogue','signed-manifest','signed-definition','signed-role','signed-source','marker-drift','initial-unprotected','terminal-unprotected']){
const f=fixture(fault,true,'upgrade')
await assert.rejects(f.upgrade())
assert.deepEqual(f.writes,[],fault)
}
const explicit=fixture('',true,'upgrade')
await assert.rejects(explicit.publish(),/explicit upgrade command/)
assert.equal(explicit.checkouts,0)
const renewal=fixture('',true,'ordinary-renewal')
await assert.rejects(renewal.publish(),error=>error instanceof Error&&/Renewal cannot adopt/.test((error.cause as Error).message))
assert.deepEqual(renewal.writes,[])
})
test('explicit 56 to 57 upgrade signs only the exact reviewed successor',async()=>{
const definition=approvedDefinition({format:'application-writer-source57-definition-approval-v1',provenance:BUILD57_PROVENANCE,template:FIXED57_ROLE_TEMPLATE})
assert.equal(definition.manifestSha256,migrationManifestDigest())
assert.throws(()=>approvedDefinition({format:'application-writer-source57-definition-approval-v1',provenance:BUILD57_PROVENANCE,template:FIXED44_ROLE_TEMPLATE}))
const f=fixture('',true,'upgrade57')
await assert.rejects(f.publish(),/explicit upgrade command/)
const result=await f.upgrade()
assert.equal(result.outcome,'committed')
assert.equal(format.canonical(f.markerLimits),'[[58],[58]]')
const history=f.writes[0]
const policy=format.signedEnvelope(history.values![7] as Buffer,'policy',f.f.subject).body
const head=format.signedEnvelope(history.values![8] as Buffer,'head',f.f.subject).body
assert.equal(policy.manifestSha256,definition.manifestSha256)
assert.equal(policy.definitionSha256,definition.definitionSha256)
assert.equal(head.previousHeadSha256,f.priorHead)
for(const fault of ['wrong-transition','wrong-previous-catalogue','signed-manifest','signed-definition']){
const refused=fixture(fault,true,'upgrade57')
await assert.rejects(refused.upgrade())
assert.deepEqual(refused.writes,[],fault)
}
})
test('operator requires the protected source verifier for preflight, publication and reconciliation',async()=>{ test('operator requires the protected source verifier for preflight, publication and reconciliation',async()=>{
const cli=await fs.readFile(new URL('./database-writer-profile-operator.mts',import.meta.url),'utf8'),f=syntheticFixture('1'),reads:string[]=[],events:string[]=[] const cli=await fs.readFile(new URL('./database-writer-profile-operator.mts',import.meta.url),'utf8'),f=syntheticFixture('1'),reads:string[]=[],events:string[]=[]
const request={installationId:f.subject.installationId,roles:Object.fromEntries(['runtime','operator','capture','publisher','control'].map((k,i)=>[k,{name:k,signedOid:String(i+11)}])),source:f.policy.source,sourceHeadSha256:'a'.repeat(64),tls:{caSha256:'a'.repeat(64),spkiSha256:'d'.repeat(64)}} const request={installationId:f.subject.installationId,roles:Object.fromEntries(['runtime','operator','capture','publisher','control'].map((k,i)=>[k,{name:k,signedOid:String(i+11)}])),source:f.policy.source,sourceHeadSha256:'a'.repeat(64),tls:{caSha256:'a'.repeat(64),spkiSha256:'d'.repeat(64)}}
const files:Record<string,unknown>={bootstrap:{format:'application-writer-bootstrap-v1',publicationAuthorized:false,readyAuthorized:false,deploymentRequest:request,verificationSubject:f.subject},approval:{expiresAt:Date.now()+60000,definitionSha256:'b'.repeat(64)},definition:{},transport:{},ca:'fixture','signing-private.pem':'fixture'} const files:Record<string,unknown>={bootstrap:{format:'application-writer-bootstrap-v1',publicationAuthorized:false,readyAuthorized:false,deploymentRequest:request,verificationSubject:f.subject},approval:{expiresAt:Date.now()+60000,definitionSha256:'b'.repeat(64)},definition:{},transport:{},ca:'fixture','signing-private.pem':'fixture'}
let failEnd=false,asyncError=false,protectedRefused=false,activationRefused=false,incompatibleDefinition=false let failEnd=false,asyncError=false,protectedRefused=false,activationRefused=false,incompatibleDefinition=false
let catalogueEdition:44|56|57=44
const catalogueCalls:number[]=[]
const retainedPools:Pool[]=[] const retainedPools:Pool[]=[]
class Pool extends EventEmitter{constructor(){super();retainedPools.push(this)}async end(){events.push('pool-end');if(failEnd)throw Error('Pool cleanup unconfirmed')}} class Pool extends EventEmitter{constructor(){super();retainedPools.push(this)}async end(){events.push('pool-end');if(failEnd)throw Error('Pool cleanup unconfirmed')}}
const result={state:'matched',epoch:'1',headSha256:'c'.repeat(64),notAfter:Date.now()+60000,platformProtectionVerified:true} const result={state:'matched',epoch:'1',headSha256:'c'.repeat(64),notAfter:Date.now()+60000,platformProtectionVerified:true}
@@ -244,32 +158,25 @@ test('operator requires the protected source verifier for preflight, publication
const modules={ const modules={
'node:assert/strict':assert,'node:path':require('node:path'),'node:url':require('node:url'),'node:crypto':require('node:crypto'), 'node:assert/strict':assert,'node:path':require('node:path'),'node:url':require('node:url'),'node:crypto':require('node:crypto'),
'./writer-profile/operator-files.mjs':{readProtected:async(file:string)=>{reads.push(file);assert(file in files);return Buffer.from(JSON.stringify(files[file]))},protectedDirectory:async()=>{},sha:format.sha}, './writer-profile/operator-files.mjs':{readProtected:async(file:string)=>{reads.push(file);assert(file in files);return Buffer.from(JSON.stringify(files[file]))},protectedDirectory:async()=>{},sha:format.sha},
'./writer-profile/operator-contract.mjs':{approval:(value:unknown)=>{events.push('approval');return value},upgradeApproval:(value:unknown)=>{events.push('upgrade-approval');return upgradeApproval(value)},reconciliationApproval:(value:unknown)=>{events.push('reconciliation-approval');return value},poolOptions:()=>{events.push('transport');return {}},roleKeys:['runtime','operator','capture','publisher','control'],assertJsDriverEnvironment:()=>{}}, './writer-profile/operator-contract.mjs':{approval:(value:unknown)=>{events.push('approval');return value},reconciliationApproval:(value:unknown)=>{events.push('reconciliation-approval');return value},poolOptions:()=>{events.push('transport');return {}},roleKeys:['runtime','operator','capture','publisher','control'],assertJsDriverEnvironment:()=>{}},
'./database-writer-bootstrap-operator.mjs':{deploymentRequest:(value:unknown)=>value}, './database-writer-bootstrap-operator.mjs':{deploymentRequest:(value:unknown)=>value},
'./writer-profile/protected-source-registry.mjs':{createProtectedDatabaseSourceBindingVerifier:(pin:{registryDirectory:string;currentHeadSha256:string})=>{events.push('protected-source');assert.equal(pin.registryDirectory,'source-registry');assert.equal(pin.currentHeadSha256,request.sourceHeadSha256);if(protectedRefused)throw Error('Protected source refused');return protectedVerifier}}, './writer-profile/protected-source-registry.mjs':{createProtectedDatabaseSourceBindingVerifier:(pin:{registryDirectory:string;currentHeadSha256:string})=>{events.push('protected-source');assert.equal(pin.registryDirectory,'source-registry');assert.equal(pin.currentHeadSha256,request.sourceHeadSha256);if(protectedRefused)throw Error('Protected source refused');return protectedVerifier}},
'./writer-profile/source-activation.mjs':{withActiveSource:async(input:{directory:string;expectedSha256:string;sourcePin:{currentHeadSha256:string};expectedSource:unknown;tlsSpkiSha256:string;deadlineAt:number},verifier:unknown,work:(verifier:unknown)=>Promise<unknown>)=>{ './writer-profile/source-activation.mjs':{withActiveSourcePreflight:async(input:{directory:string;expectedSha256:string;sourcePin:{currentHeadSha256:string};expectedSource:unknown;tlsSpkiSha256:string;deadlineAt:number},verifier:unknown,work:(verifier:unknown)=>Promise<unknown>)=>{
assert.equal(verifier,protectedVerifier);assert.equal(input.directory,'active-registry');assert.equal(input.expectedSha256,'e'.repeat(64)) assert.equal(verifier,protectedVerifier);assert.equal(input.directory,'active-registry');assert.equal(input.expectedSha256,'e'.repeat(64))
assert.equal(input.sourcePin.currentHeadSha256,request.sourceHeadSha256);assert.equal(input.tlsSpkiSha256,request.tls.spkiSha256);assert(input.deadlineAt>Date.now()) assert.equal(input.sourcePin.currentHeadSha256,request.sourceHeadSha256);assert.equal(input.tlsSpkiSha256,request.tls.spkiSha256);assert(input.deadlineAt>Date.now())
assert.equal(format.canonical(input.expectedSource),format.canonical({...request.source,databaseOid:Number(request.source.databaseOid),sessionUser:'publisher',currentUser:'publisher'})) assert.equal(format.canonical(input.expectedSource),format.canonical({...request.source,databaseOid:Number(request.source.databaseOid),sessionUser:'publisher',currentUser:'publisher'}))
events.push('activation-admission');if(activationRefused)throw Error('Activation refused') events.push('activation-admission');if(activationRefused)throw Error('Activation refused')
const value=await work(guardedVerifier);events.push('activation-terminal');return value const value=await work(guardedVerifier);events.push('activation-terminal');return value
}}, }},
pg:{Pool},'./writer-profile/publisher.mjs':{requireCurrentCatalogueDefinition:()=>{if(incompatibleDefinition)throw Error('Build manifest refused')},createConnectedPublisher:(_installation:unknown,collect:(session:unknown)=>Promise<unknown>,verifier:unknown)=>{ pg:{Pool},'./writer-profile/publisher.mjs':{requireCurrentCatalogueDefinition:()=>{if(incompatibleDefinition)throw Error('Build manifest refused')},createConnectedPublisher:(_installation:unknown,_collect:unknown,verifier:unknown)=>{
assert.equal(verifier,events.includes('activation-admission')?guardedVerifier:protectedVerifier) assert.equal(verifier,events.includes('activation-admission')?guardedVerifier:protectedVerifier)
return { return {
inspectLegacyStorage:async()=>{events.push('legacy-inspect');return {state:'observed',snapshot:{rowCount:1,sha256:'9'.repeat(64)},source:{platformProtectionVerified:true,generation:'source-generation'}}}, preflightCurrentProfile:async()=>{events.push('preflight');if(asyncError){const activePool=retainedPools.at(-1);assert(activePool);activePool.emit('error',Error('Async pool fault'))}return result},
preflightCurrentProfile:async()=>{await collect({});events.push('preflight');if(asyncError){const activePool=retainedPools.at(-1);assert(activePool);activePool.emit('error',Error('Async pool fault'))}return result}, publishCurrentProfile:async()=>{events.push('publish');return {...result,outcome:'committed'}},
publishCurrentProfile:async()=>{await collect({});events.push('publish');return {...result,outcome:'committed'}},
upgradeCurrentProfile:async()=>{await collect({});events.push('upgrade');return {...result,outcome:'committed'}},
reconcilePendingPublication:async()=>{events.push('reconcile');return {state:'confirmed',headSha256:result.headSha256}}, reconcilePendingPublication:async()=>{events.push('reconcile');return {state:'confirmed',headSha256:result.headSha256}},
} }
}}, }},
'./writer-profile/catalogue.mjs':{ './writer-profile/catalogue.mjs':{collectSource44Catalogue:()=>{},approvedDefinition:()=>({definitionSha256:'b'.repeat(64),manifestSha256:frozenDefinition.manifestSha256})},
collectSource44Catalogue:async()=>{catalogueCalls.push(44)},
collectSource56Catalogue:async()=>{catalogueCalls.push(56)},
collectSource57Catalogue:async()=>{catalogueCalls.push(57)},
approvedDefinition:()=>({...(catalogueEdition===44?{}:{edition:catalogueEdition}),definitionSha256:'b'.repeat(64),manifestSha256:frozenDefinition.manifestSha256}),
},
} }
const flags=['bootstrap','bootstrap-sha256','approval','approval-sha256','definition','definition-sha256','transport','transport-sha256','ca','source-registry','source-spki-sha256','journal'],args=['preflight',...flags.flatMap(flag=>['--'+flag,flag.endsWith('sha256')?'a'.repeat(64):flag])] const flags=['bootstrap','bootstrap-sha256','approval','approval-sha256','definition','definition-sha256','transport','transport-sha256','ca','source-registry','source-spki-sha256','journal'],args=['preflight',...flags.flatMap(flag=>['--'+flag,flag.endsWith('sha256')?'a'.repeat(64):flag])]
const output=await compile<OperatorModule>(cli,modules).main(args) const output=await compile<OperatorModule>(cli,modules).main(args)
@@ -280,10 +187,8 @@ test('operator requires the protected source verifier for preflight, publication
asyncError=false;result.platformProtectionVerified=false;await assert.rejects(compile<OperatorModule>(cli,modules).main(args),/Publisher terminal unconfirmed/) asyncError=false;result.platformProtectionVerified=false;await assert.rejects(compile<OperatorModule>(cli,modules).main(args),/Publisher terminal unconfirmed/)
result.platformProtectionVerified=true;protectedRefused=true;const before=retainedPools.length result.platformProtectionVerified=true;protectedRefused=true;const before=retainedPools.length
await assert.rejects(compile<OperatorModule>(cli,modules).main(args),/Protected source refused/);assert.equal(retainedPools.length,before,'Protection failure must not open a Pool or fall back') await assert.rejects(compile<OperatorModule>(cli,modules).main(args),/Protected source refused/);assert.equal(retainedPools.length,before,'Protection failure must not open a Pool or fall back')
const activationFlags=['--activation-directory','active-registry','--activation-sha256','e'.repeat(64)]
const activated=(command:string)=>[command,...args.slice(1),...activationFlags]
for(const [command,approvalEvent,outcome] of [['publish','approval','committed'],['reconcile','reconciliation-approval','confirmed']]){ for(const [command,approvalEvent,outcome] of [['publish','approval','committed'],['reconcile','reconciliation-approval','confirmed']]){
const commandArgs=command==='publish'?activated(command):[command,...args.slice(1)] const commandArgs=[command,...args.slice(1)]
const poolCount=retainedPools.length const poolCount=retainedPools.length
await assert.rejects(compile<OperatorModule>(cli,modules).main(commandArgs),/Protected source refused/) await assert.rejects(compile<OperatorModule>(cli,modules).main(commandArgs),/Protected source refused/)
assert.equal(retainedPools.length,poolCount) assert.equal(retainedPools.length,poolCount)
@@ -291,65 +196,28 @@ test('operator requires the protected source verifier for preflight, publication
events.length=0;reads.length=0 events.length=0;reads.length=0
const terminal=await compile<OperatorModule>(cli,modules).main(commandArgs) const terminal=await compile<OperatorModule>(cli,modules).main(commandArgs)
assert.equal(terminal.outcome,outcome);assert.equal(terminal.readyAuthorized,false) assert.equal(terminal.outcome,outcome);assert.equal(terminal.readyAuthorized,false)
assert.deepEqual(events,command==='publish' assert.deepEqual(events,[approvalEvent,'transport','protected-source',command,'pool-end'])
?[approvalEvent,'transport','protected-source','activation-admission',command,'pool-end','activation-terminal']
:[approvalEvent,'transport','protected-source',command,'pool-end'])
assert.equal(reads.includes('signing-private.pem'),command==='publish') assert.equal(reads.includes('signing-private.pem'),command==='publish')
protectedRefused=true protectedRefused=true
} }
const activeArgs=activated('source-preflight') const activeArgs=['source-preflight',...args.slice(1),'--activation-directory','active-registry','--activation-sha256','e'.repeat(64)]
protectedRefused=false;activationRefused=true;events.length=0;reads.length=0 protectedRefused=false;activationRefused=true;events.length=0;reads.length=0
const beforeActivation=retainedPools.length const beforeActivation=retainedPools.length
await assert.rejects(compile<OperatorModule>(cli,modules).main(activeArgs),/Activation refused/) await assert.rejects(compile<OperatorModule>(cli,modules).main(activeArgs),/Activation refused/)
assert.equal(retainedPools.length,beforeActivation) assert.equal(retainedPools.length,beforeActivation)
assert.deepEqual(events,['approval','transport','protected-source','activation-admission']) assert.deepEqual(events,['approval','transport','protected-source','activation-admission'])
await assert.rejects(compile<OperatorModule>(cli,modules).main(['publish',...args.slice(1)]))
await assert.rejects(compile<OperatorModule>(cli,modules).main(activated('publish')), /Activation refused/)
assert.equal(retainedPools.length,beforeActivation)
activationRefused=false;events.length=0;reads.length=0 activationRefused=false;events.length=0;reads.length=0
const active=await compile<OperatorModule>(cli,modules).main(activeArgs) const active=await compile<OperatorModule>(cli,modules).main(activeArgs)
assert.equal(active.event,'application-writer-profile-source-preflight');assert.equal(active.sourceActivationMatched,true) assert.equal(active.event,'application-writer-profile-source-preflight');assert.equal(active.sourceActivationMatched,true)
assert.equal(active.readyAuthorized,false);assert.equal(active.writerAuthorized,false) assert.equal(active.readyAuthorized,false);assert.equal(active.writerAuthorized,false)
assert.deepEqual(events,['approval','transport','protected-source','activation-admission','preflight','pool-end','activation-terminal']) assert.deepEqual(events,['approval','transport','protected-source','activation-admission','preflight','pool-end','activation-terminal'])
assert.deepEqual(reads,['bootstrap','approval','definition','transport','ca']) assert.deepEqual(reads,['bootstrap','approval','definition','transport','ca'])
const legacyFlags=['bootstrap','bootstrap-sha256','transport','transport-sha256','ca','source-registry','source-spki-sha256']
const legacyArgs=['legacy-inspect',...legacyFlags.flatMap(flag=>['--'+flag,flag.endsWith('sha256')?'a'.repeat(64):flag]),...activationFlags]
events.length=0;reads.length=0
const legacy=await compile<OperatorModule>(cli,modules).main(legacyArgs)
assert.equal(legacy.event,'database-legacy-inspection');assert.equal(legacy.outcome,'observed');assert.equal(legacy.readyAuthorized,false)
assert.deepEqual(events,['transport','protected-source','activation-admission','legacy-inspect','pool-end','activation-terminal'])
assert.deepEqual(reads,['bootstrap','transport','ca'])
await assert.rejects(compile<OperatorModule>(cli,modules).main(legacyArgs.slice(0,-2)))
activationRefused=true;await assert.rejects(compile<OperatorModule>(cli,modules).main(legacyArgs),/Activation refused/);activationRefused=false
assert(catalogueCalls.length>0&&catalogueCalls.every(edition=>edition===44))
catalogueEdition=56;catalogueCalls.length=0
for(const commandArgs of [args,activeArgs,activated('publish')]){
events.length=0
await compile<OperatorModule>(cli,modules).main(commandArgs)
}
assert.deepEqual(catalogueCalls,[56,56,56])
const publicationApproval=files.approval
files.approval={...fixture('',true,'upgrade').pinned,definitionSha256:'b'.repeat(64)}
events.length=0;reads.length=0;catalogueCalls.length=0
const upgraded=await compile<OperatorModule>(cli,modules).main(activated('upgrade'))
assert.equal(upgraded.outcome,'committed')
assert.deepEqual(events,['upgrade-approval','transport','protected-source','activation-admission','upgrade','pool-end','activation-terminal'])
assert.deepEqual(catalogueCalls,[56])
assert(reads.includes('signing-private.pem'))
catalogueEdition=57
files.approval={...fixture('',true,'upgrade57').pinned,definitionSha256:'b'.repeat(64)}
events.length=0;reads.length=0;catalogueCalls.length=0
assert.equal((await compile<OperatorModule>(cli,modules).main(activated('upgrade'))).outcome,'committed')
assert.deepEqual(catalogueCalls,[57])
files.approval=publicationApproval
catalogueEdition=44;catalogueCalls.length=0
incompatibleDefinition=true;events.length=0;reads.length=0 incompatibleDefinition=true;events.length=0;reads.length=0
const beforeBuildRefusal=retainedPools.length const beforeBuildRefusal=retainedPools.length
for(const incompatibleArgs of [args,activeArgs,activated('publish')])await assert.rejects(compile<OperatorModule>(cli,modules).main(incompatibleArgs),/Build manifest refused/) for(const incompatibleArgs of [args,activeArgs,['publish',...args.slice(1)]])await assert.rejects(compile<OperatorModule>(cli,modules).main(incompatibleArgs),/Build manifest refused/)
assert.equal(retainedPools.length,beforeBuildRefusal) assert.equal(retainedPools.length,beforeBuildRefusal)
assert(!events.some(event=>event==='protected-source'||event==='activation-admission')) assert(!events.some(event=>event==='protected-source'||event==='activation-admission'))
const historical=await compile<OperatorModule>(cli,modules).main(['reconcile',...args.slice(1)]) const historical=await compile<OperatorModule>(cli,modules).main(['reconcile',...args.slice(1)])
assert.equal(historical.outcome,'confirmed');assert.equal(historical.readyAuthorized,false) assert.equal(historical.outcome,'confirmed');assert.equal(historical.readyAuthorized,false)
assert.deepEqual(catalogueCalls,[])
}) })
-196
View File
@@ -1,196 +0,0 @@
import assert from 'node:assert/strict'
import {createPublicKey,generateKeyPairSync,randomBytes,randomUUID,sign,X509Certificate} from 'node:crypto'
import {readFile,mkdir,writeFile} from 'node:fs/promises'
import {basename,dirname,join,resolve} from 'node:path'
import {tmpdir} from 'node:os'
export async function verifyOwnedLegacy0037Handoff(pg,options,leafBytes,client,directory,inspectOnly=false){
assert.equal(dirname(directory),resolve(tmpdir()))
assert.match(basename(directory),/^mm-archive-leaf-tls-/)
const fixtureState=join(directory,'mm-mcp-test-legacy-0037','state')
await mkdir(fixtureState,{recursive:true,mode:0o700})
const tail=inspectOnly==='tail'?{user:'owned_commit_operator',password:randomBytes(24).toString('hex')}:null
const localLogin=tail??options
Object.assign(process.env,{MMAPP_OWN_PROBE_LEGACY_0037:'true',MMAPP_OWN_PROBE_LEGACY_ORPHANS:'true',
DATABASE_URL:`postgres://${localLogin.user}:${localLogin.password}@127.0.0.1:${options.port}/postgres`,MCP_STATE_DIR:fixtureState,AUTH_REQUIRED:'false'})
const [{applyLegacyProbeFixtureMigrations},{initialDatabaseSchemaControlState},{runOwnedLegacyProbe0037Handoff},
{captureLegacyProbeLineage,captureLegacyProbeStorage,LEGACY_PROBE_CUSTODY_LOCK,LEGACY_PROBE_RELATIONS},
{runPinnedDatabaseSnapshotDump,runPinnedLegacyProbeSnapshotDump},{LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN},
{sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,readDatabaseSourceBindingRegistry},
{provisionDatabaseSourceBindingOffline},{retainPublisherTlsPeer},security,{collectLegacyServerProbeSnapshot}]=await Promise.all([
import('../src/db/legacy-probe-fixture.ts'),import('../src/db/database-schema-control-model.ts'),import('../src/db/proposals/server-probe-legacy-0037-owned.ts'),
import('../src/services/server-probe-legacy-source-inventory.ts'),import('../src/services/database-dump-connection.ts'),import('../src/services/server-probe-legacy-archive-approval.ts'),
import('../src/db/database-source-binding.ts'),import('./database-source-binding-operator.ts'),import('./writer-profile/operator-contract.mjs'),
import('../src/modules/mcp/security.ts'),import('../src/services/server-probe-legacy-snapshot.ts'),
])
const live=new pg.Pool({...options,max:4}),coldDatabase='mm_owned_legacy_cold_'+randomUUID().replaceAll('-','')
let cold,coldAdmin
const store=async(name,value)=>{const file=join(directory,name);await writeFile(file,value,{flag:'wx',mode:0o600});return file}
const inspect=async(sql)=>{
await sql.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY')
try {
let sqlState
const summary=await collectLegacyServerProbeSnapshot({query:async(text,values)=>{
try { return await sql.query(text,values) } catch(error) { sqlState=error.code;throw error }
}},{deadlineAt:Date.now()+30000})
assert.equal(summary.complete,true,JSON.stringify({reasons:summary.reasons,sqlState}))
const snapshot=await captureLegacyProbeStorage(sql),lineage=await captureLegacyProbeLineage(sql)
return {summary,snapshot,lineage}
} finally { await sql.query('ROLLBACK') }
}
try{
if(tail){
// Use the same canonical application owner from the first byte. The
// signed reference compares live owners and ACLs as part of its digest.
await live.query(`CREATE ROLE ${tail.user} LOGIN CREATEDB PASSWORD '${tail.password}';
ALTER DATABASE postgres OWNER TO ${tail.user}; COMMENT ON DATABASE postgres IS NULL`)
const application=new pg.Pool({...options,...tail,max:4})
try{await applyLegacyProbeFixtureMigrations(application)}finally{await application.end()}
}else await applyLegacyProbeFixtureMigrations(live)
await live.query(`CREATE SCHEMA mmapp_control; CREATE TABLE mmapp_control.database_schema_state(id smallint PRIMARY KEY CHECK(id=1),revision bigint NOT NULL CHECK(revision>=0),state jsonb NOT NULL);
CREATE TABLE mmapp_control.database_schema_intents(id uuid PRIMARY KEY,intent jsonb NOT NULL,terminal jsonb)`)
await live.query('INSERT INTO mmapp_control.database_schema_state VALUES(1,0,$1::jsonb)',[JSON.stringify(initialDatabaseSchemaControlState())])
const owner='owned-legacy-history-owner',generation=randomUUID(),connection=randomUUID(),credential=randomUUID(),plan=randomUUID(),operation=randomUUID()
const restReceipt=randomUUID(),restExecution=randomUUID(),mcpReceipt=randomUUID(),quarantine=randomUUID(),quarantinedExecution=randomUUID()
const expiresAt=Date.UTC(2000,0,1),issuedAt=expiresAt-3600000,expiry=new Date(expiresAt).toISOString(),bindingProof='a'.repeat(64)
await live.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation) VALUES($1,'owned history',$2,'[]','{}',$3)",[connection,owner,generation])
await live.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)",[credential,connection,randomUUID(),expiry])
const binding={version:1,operation:'servers.probes.run',principal:{connectionId:connection,credentialId:credential,credentialExpiresAt:expiry},
ownerId:owner,issuedAt,authorityExpiresAt:expiresAt,prepared:{receiptId:mcpReceipt,serverId:1,tool:'ping',expiresAt:expiry,
bindingCiphertext:await security.encryptSecret('owned original provider request')},bindingProof,stageBudget:2}
const step={kind:'service',service:'servers.probes.run',input:{receiptId:mcpReceipt,serverId:1,
inputDigest:await security.privateSecretFingerprint(security.fingerprint(binding),'server-probe-mcp-binding-v1'),
bindingCiphertext:await security.encryptSecret(JSON.stringify(binding))}}
const evidence={sealed:await security.encryptSecret(JSON.stringify({steps:[step],target:{scope:'servers',serverIds:[1]},requiresHuman:true,
verificationScope:'acknowledgement',mutationCost:2,validUntil:expiry}))}
await live.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by)
VALUES($1,$2,'servers.probes.run',1,1,$3,'{}',$4::jsonb,'completed',$5,$6)`,[plan,connection,generation,JSON.stringify(evidence),expiry,owner])
await live.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,finished_at) VALUES($1,$2,$3,$4,'applied',clock_timestamp())",[operation,plan,connection,randomUUID()])
await live.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)",[operation,JSON.stringify(security.redact(step))])
await live.query('INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,$3)',[operation,connection,owner])
const context={identity:{kind:'rest',ownerId:owner,executionId:restExecution,requestId:'owned-original-request',authentication:'jwt',issuedAt,expiresAt},sourceServerId:1,tool:'ping'}
await live.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,request_id,authority_issued_at,authority_expires_at,source_server_id,tool)
VALUES($1,$2,'rest',$3,$4,'jwt','owned-original-request',$5,$6,1,'ping')`,[restReceipt,bindingProof,restExecution,owner,issuedAt,expiry])
await live.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,received_at) VALUES($1,1,'native-ping',1000,'received','received',clock_timestamp())",[restReceipt])
const receipt={receiptId:restReceipt,tool:'ping',serverId:1,state:'completed',httpStatus:200,output:'owned original result',data:{kind:'ping'},
attemptedStages:1,completedStages:1,automaticReplay:false,forwardingVerified:false}
const plaintext=JSON.stringify({version:1,context,bindingProof,receipt})
await live.query(`UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,finished_at=clock_timestamp(),receipt_ciphertext=$2,receipt_proof=$3
WHERE receipt_id=$1`,[restReceipt,await security.encryptSecret(plaintext),await security.privateSecretFingerprint(security.fingerprint(plaintext),'server-probe-journal-v1:receipt')])
await live.query(`INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages)
VALUES($1,repeat('b',64),'rest',$2,repeat('c',64),'JOURNAL_UNKNOWN',0,0)`,[quarantine,quarantinedExecution])
const dependencySql=`SELECT jsonb_build_object('operation',to_jsonb(o),'plan',to_jsonb(p),'connection',to_jsonb(c),'step',to_jsonb(s),'credential',to_jsonb(k)) AS original
FROM public.mcp_operations o JOIN public.mcp_plans p ON p.id=o.plan_id JOIN public.mcp_connections c ON c.id=o.connection_id
JOIN public.mcp_operation_steps s ON s.operation_id=o.id JOIN public.mcp_credentials k ON k.connection_id=c.id WHERE o.id=$1 AND k.id=$2`
const dependencies=(await live.query(dependencySql,[operation,credential])).rows
assert.equal(dependencies.length,1)
const dependencyHash=sourceBindingSha256(sourceBindingBytes(dependencies))
const beforeClient=await live.connect()
let before
try { before=await inspect(beforeClient) } finally { beforeClient.release() }
assert.equal(before.snapshot.rowCount,4);assert.equal(before.lineage.rowCount,4)
assert.equal(before.summary.counts.verified,2,JSON.stringify(before.summary.reasons));assert.equal(before.summary.counts.unresolved,1)
const privateKey=generateKeyPairSync('ed25519').privateKey, signer=await store('owned-source-signer.pem',privateKey.export({type:'pkcs8',format:'pem'}))
const privatePassword=await store('owned-archive-password',options.password+'\n')
const peerLeaf=new X509Certificate(leafBytes),host='localhost',exporter=await live.connect()
let originalPin,archive,fullArchive,signedApproval
try{
retainPublisherTlsPeer(exporter,{serverName:host,spkiSha256:sourceBindingSha256(peerLeaf.publicKey.export({type:'spki',format:'der'}))})()
await exporter.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY')
await exporter.query(LEGACY_PROBE_CUSTODY_LOCK)
const {rows:[identity]}=await exporter.query(`SELECT current_database() AS "databaseName",current_user::text AS "currentUser",session_user::text AS "sessionUser",
(pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier",pg_catalog.pg_is_in_recovery() AS "inRecovery",
current_setting('server_version_num')::int AS "serverVersionNum",d.oid::int AS "databaseOid",r.oid::text AS "signedOid"
FROM pg_catalog.pg_database d,pg_catalog.pg_roles r WHERE d.datname=current_database() AND r.rolname=current_user`)
const {signedOid,...source}=identity
const installationId=randomUUID(),sourceIdentityId=randomUUID(),request={installationId,sourceIdentityId,generation,source}
;({deploymentPin:originalPin}=await provisionDatabaseSourceBindingOffline({request,privateKeyFile:signer,outputDirectory:join(directory,'owned-original-registry')}))
const registry=await readDatabaseSourceBindingRegistry(originalPin)
const storage=await captureLegacyProbeStorage(exporter),lineage=await captureLegacyProbeLineage(exporter)
assert.deepEqual(storage,before.snapshot);assert.deepEqual(lineage,before.lineage)
const {rows:[{snapshot}]}=await exporter.query('SELECT pg_catalog.pg_export_snapshot() AS snapshot')
const peer={host,port:options.port,database:'postgres',capture:{name:options.user,signedOid},leaf:peerLeaf.toString()}
archive=await runPinnedLegacyProbeSnapshotDump(peer,options.password,snapshot,directory,{})
fullArchive=await runPinnedDatabaseSnapshotDump(peer,privatePassword,snapshot,{})
const body={format:'legacy-probe-custody-approval-v1',installationId,sourceIdentityId,generation,ordinal:1,
headSha256:registry.headSha256,bindingSha256:registry.head.bindingSha256,archiveSha256:sourceBindingSha256(archive),
storageSha256:storage.sha256,lineageSha256:lineage.sha256}
signedApproval=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_PROBE_CUSTODY_APPROVAL_DOMAIN+sourceBindingCanonical(body)),privateKey).toString('base64url')})
} finally { try { await exporter.query('ROLLBACK') } finally { exporter.release() } }
await store('owned-legacy-probe.dump',archive);await store('owned-legacy-probe-approval.json',signedApproval)
const archiveFile=await store('owned-full-cold.dump',fullArchive)
const {stdout:toc}=await client('pg_restore',['--list',archiveFile],'pinned')
assert.doesNotMatch(toc,/mmapp_control/)
await live.query(`CREATE DATABASE ${coldDatabase} TEMPLATE template0`)
coldAdmin=new pg.Client({...options,database:coldDatabase});await coldAdmin.connect()
if(/\bSCHEMA - public\b/.test(toc))await coldAdmin.query('DROP SCHEMA public RESTRICT')
try { await client('pg_restore',['--dbname='+coldDatabase,'--exit-on-error','--no-owner',archiveFile],'pinned') }
catch { throw Error('Owned full legacy cold restore failed') }
const coldPassword=randomUUID()
await coldAdmin.query(`CREATE ROLE owned_legacy_reader LOGIN PASSWORD '${coldPassword}'; GRANT USAGE ON SCHEMA public TO owned_legacy_reader; GRANT SELECT ON TABLE
${['schema_migrations','mcp_operations','mcp_operation_steps','mcp_plans','mcp_connections','mcp_credentials',...LEGACY_PROBE_RELATIONS].map(name=>`public.${name}`).join(', ')} TO owned_legacy_reader`)
cold=new pg.Client({...options,database:coldDatabase,user:'owned_legacy_reader',password:coldPassword});await cold.connect()
const coldState=join(directory,'owned-cold-key-state')
await mkdir(coldState,{mode:0o700})
const archivedKey=await readFile(join(fixtureState,'encryption-key'))
try { await writeFile(join(coldState,'encryption-key'),archivedKey,{flag:'wx',mode:0o600}) } finally { archivedKey.fill(0) }
const assertCold=async()=>{
let read
process.env.MCP_STATE_DIR=coldState
try { read=await inspect(cold) } finally { process.env.MCP_STATE_DIR=fixtureState }
assert.deepEqual(read,before)
assert.equal(sourceBindingSha256(sourceBindingBytes((await cold.query(dependencySql,[operation,credential])).rows)),dependencyHash)
assert((await cold.query('SELECT execution_id FROM public.server_probe_executions WHERE receipt_id=$1',[restReceipt])).rows[0]?.execution_id===restExecution,'Original receipt ID drift')
assert((await cold.query('SELECT execution_id FROM public.server_probe_execution_quarantines WHERE receipt_id=$1',[quarantine])).rows[0]?.execution_id===quarantinedExecution,'Original unresolved ID drift')
await assert.rejects(cold.query('DELETE FROM public.server_probe_execution_quarantines WHERE receipt_id=$1',[quarantine]),{code:'42501'})
}
await assertCold()
if(['cold','execute','adopt','release','tail'].includes(inspectOnly)){
const {verifyOwnedLegacyColdCommand}=await import('./legacy-probe-cold-verify-owned.mjs')
return await verifyOwnedLegacyColdCommand({live,coldAdmin,cold,options,directory,coldState,leafBytes,originalPin,archive,fullArchive,signedApproval,
execute:inspectOnly!=='cold',adopt:['adopt','release','tail'].includes(inspectOnly),release:['release','tail'].includes(inspectOnly),
tail,sourcePrivateKey:privateKey})
}
if(inspectOnly){
const {verifyOwnedHandoffInspector}=await import('./legacy-probe-handoff-inspect-owned.mjs')
return await verifyOwnedHandoffInspector({live,directory,options,leafBytes,originalPin,archive,fullArchive,signedApproval,privateKey,intentOnly:inspectOnly==='intent'})
}
const operatorPublicKeyPem=Buffer.from(createPublicKey(privateKey).export({type:'spki',format:'pem'}))
const archiveApproval={archive,signedApproval,operatorPublicKeyPem}, input={live,cold,coldDatabase,fixtureDirectory:directory,originalPin,archiveApproval}
const expectPre0037=async()=>{
const sql=await live.connect()
try { assert.deepEqual(await inspect(sql),before) } finally { sql.release() }
const {rows:[row]}=await live.query(`SELECT count(*)::int AS markers, to_regclass('public.server_probe_storage_admissions')::text AS v2
FROM public.schema_migrations`)
assert.deepEqual(row,{markers:36,v2:null})
}
await assert.rejects(runOwnedLegacyProbe0037Handoff({...input,archiveApproval:{...archiveApproval,archive:Buffer.concat([archive,Buffer.from('tamper')])}}),/approval refused/)
await coldAdmin.query("UPDATE public.mcp_operation_steps SET intended_state='{\"drift\":true}' WHERE operation_id=$1",[operation])
await assert.rejects(runOwnedLegacyProbe0037Handoff(input),{code:'ERR_ASSERTION'})
await coldAdmin.query('UPDATE public.mcp_operation_steps SET intended_state=$2::jsonb WHERE operation_id=$1',
[operation,JSON.stringify(dependencies[0].original.step.intended_state)])
await expectPre0037()
const competingWriter=await live.connect()
try{
await competingWriter.query('BEGIN')
await competingWriter.query('LOCK TABLE public.mcp_plans IN ROW EXCLUSIVE MODE')
await assert.rejects(runOwnedLegacyProbe0037Handoff(input),{code:'55P03'})
} finally { try { await competingWriter.query('ROLLBACK') } finally { competingWriter.release() } }
await assert.rejects(runOwnedLegacyProbe0037Handoff({...input,failAfterBody:true}),{code:'22012'})
await expectPre0037()
const outcome=await runOwnedLegacyProbe0037Handoff(input)
assert.deepEqual(outcome,{archivedRows:4,lineageRows:4,readyAuthorized:false})
await assertCold()
assert.equal(sourceBindingSha256(sourceBindingBytes((await live.query(dependencySql,[operation,credential])).rows)),dependencyHash)
const {rows:history}=await live.query("SELECT terminal->>'outcome' AS outcome FROM mmapp_control.database_schema_intents ORDER BY (intent->>'epoch')::int")
assert.deepEqual(history,[{outcome:'observed-failure'},{outcome:'observed-success'}])
return {ownedAtomicLegacy0037Handoff:true,coldArchiveRows:4,verifiedHistoricalIdentities:2,unresolvedHistoricalIdentities:1,readyAuthorized:false}
} finally {
await Promise.allSettled([cold?.end(),coldAdmin?.end(),live.end()])
if(inspectOnly){
const {closePool}=await import('../src/db/index.ts')
await closePool()
}
}
}
@@ -1,78 +0,0 @@
import assert from 'node:assert/strict'
import {createPublicKey,generateKeyPairSync,X509Certificate} from 'node:crypto'
import {mkdir,open,readFile,realpath,readdir,unlink,writeFile} from 'node:fs/promises'
import {dirname,join,sep} from 'node:path'
export async function modelOwnedOperatorFiles(directory,sha){
if(process.platform!=='win32')return
const {mock}=await import('node:test'),url=new URL('./writer-profile/operator-files.mjs',import.meta.url),files=await import(url.href)
const protectedDirectory=async path=>{assert(path===directory||path.startsWith(directory+sep));assert.equal(await realpath(path),path)}
return mock.module(url,{namedExports:{...files,protectedDirectory,async writeFile(path,blob,opts){
await protectedDirectory(dirname(path));const file=await open(path,opts.flag,opts.mode);try{await file.writeFile(blob);await file.sync()}finally{await file.close()}
},async readProtected(path,limit,hash){
await protectedDirectory(dirname(path));const bytes=await readFile(path);assert(bytes.length>0&&bytes.length<=limit);if(hash!==undefined)assert.equal(sha(bytes),hash);return bytes
},async withJournalLease(path,work){
await protectedDirectory(path);const lock=join(path,'publisher-lease.lock'),file=await open(lock,'wx',0o600)
try{return await work()}finally{await file.close();await unlink(lock)}
}}})
}
export async function verifyOwnedLegacyColdCommand({live,coldAdmin,cold,options,directory,coldState,leafBytes,originalPin,archive,fullArchive,signedApproval,execute,adopt,release,tail,sourcePrivateKey}){
assert.equal(process.env.MMAPP_OWN_PROBE_LEGACY_0037,'true')
const [{sourceBindingBytes,sourceBindingSha256},{captureLegacyProbeCredentialDigest,verifyLegacyColdProof},{captureLegacyProbeStorage,captureLegacyProbeLineage}]=await Promise.all([
import('../src/db/database-source-binding.ts'),import('../src/services/server-probe-legacy-cold-proof.ts'),import('../src/services/server-probe-legacy-source-inventory.ts'),
])
const sha=sourceBindingSha256,store=async(path,data)=>{await writeFile(path,data,{flag:'wx',mode:0o600});return path}
const archiveDirectory=join(directory,'cold-protected-archive'),outputDirectory=join(directory,'cold-proof-output')
await mkdir(archiveDirectory,{mode:0o700});await mkdir(outputDirectory,{mode:0o700})
for(const [name,data] of [['legacy-probe.dump',archive],['legacy-probe-approval.json',signedApproval],['legacy-probe-cold.dump',fullArchive]])await store(join(archiveDirectory,name),data)
await coldAdmin.query('GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO owned_legacy_reader')
const {rows:[identity]}=await cold.query(`SELECT current_database() AS "databaseName", current_user::text AS "currentUser", session_user::text AS "sessionUser",
(pg_catalog.pg_control_system()).system_identifier::text AS "systemIdentifier",pg_catalog.pg_is_in_recovery() AS "inRecovery",
current_setting('server_version_num')::int AS "serverVersionNum",d.oid::int AS "databaseOid",r.oid::text AS "roleOid"
FROM pg_catalog.pg_database d,pg_catalog.pg_roles r WHERE d.datname=current_database() AND r.rolname=current_user`)
const {roleOid,...coldSource}=identity
const key=generateKeyPairSync('ed25519').privateKey,publicKey=createPublicKey(key),host='localhost'
const signerSpkiSha256=sha(publicKey.export({type:'spki',format:'der'}))
const storedKey=await readFile(join(coldState,'encryption-key'));const keySha256=sha(storedKey);storedKey.fill(0)
const approval=sourceBindingBytes({format:'legacy-probe-cold-verification-approval-v1',installationId:originalPin.installationId,
sourceHeadSha256:originalPin.currentHeadSha256,sourceSignerSpkiSha256:originalPin.publicKeySpkiSha256,custodySha256:sha(signedApproval),
coldArchiveSha256:sha(fullArchive),coldSignerSpkiSha256:signerSpkiSha256,keySha256,coldSource,coldRoleOid:roleOid,
tls:{serverName:host,caSha256:sha(leafBytes),spkiSha256:sha(new X509Certificate(leafBytes).publicKey.export({type:'spki',format:'der'}))},expiresAt:Date.now()+120000})
const transport=sourceBindingBytes({host,port:options.port,user:cold.user,password:cold.password,database:cold.database})
const argv=['legacy-cold-verify','--approval',await store(join(directory,'cold-verification.json'),approval),'--approval-sha256',sha(approval),
'--transport',await store(join(directory,'cold-transport.json'),transport),'--transport-sha256',sha(transport),
'--ca',await store(join(directory,'cold-ca.pem'),leafBytes),'--source-registry',originalPin.registryDirectory,'--archive-directory',archiveDirectory,
'--legacy-key-directory',coldState,'--cold-signing-key',await store(join(directory,'cold-signing.pem'),key.export({type:'pkcs8',format:'pem'})),
'--output-directory',outputDirectory]
const modeledFiles=await modelOwnedOperatorFiles(directory,sha)
try{
const {main}=await import('./database-writer-profile-operator.mts')
if(!execute){
await coldAdmin.query('GRANT UPDATE ON public.mcp_credentials TO owned_legacy_reader')
try{await assert.rejects(main(argv),/read-only privileges/)}finally{await coldAdmin.query('REVOKE UPDATE ON public.mcp_credentials FROM owned_legacy_reader')}
assert.deepEqual(await readdir(outputDirectory),[])
}
const result=await main(argv),proof=await readFile(join(outputDirectory,result.proofFile))
assert.equal(result.event,'database-legacy-cold-proof');assert.equal(result.proofSha256,sha(proof));assert.equal(result.conversionAllowed,false)
const source=await live.connect()
try{
await source.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY')
const storage=await captureLegacyProbeStorage(source),lineage=await captureLegacyProbeLineage(source)
const pinned={publicKeyPem:Buffer.from(publicKey.export({type:'spki',format:'pem'})),signerSpkiSha256,
sourceSignerSpkiSha256:originalPin.publicKeySpkiSha256,approvalSha256:sha(approval),coldArchiveSha256:sha(fullArchive),custodySha256:sha(signedApproval),
storageSha256:storage.sha256,lineageSha256:lineage.sha256}
const verified=verifyLegacyColdProof(proof,pinned)
assert.equal(verified.credentialSha256,await captureLegacyProbeCredentialDigest(source));assert.equal(verified.keySha256,keySha256)
assert.deepEqual(verified.coldSource,coldSource)
assert.throws(()=>verifyLegacyColdProof(proof,{...pinned,coldArchiveSha256:'0'.repeat(64)}),/proof refused/)
assert.throws(()=>verifyLegacyColdProof(proof,{...pinned,sourceSignerSpkiSha256:signerSpkiSha256}),/proof refused/)
}finally{try{await source.query('ROLLBACK')}finally{source.release()}}
if(execute){
const {verifyOwnedLegacyExecution}=await import('./legacy-probe-handoff-execute-owned.mjs')
return await verifyOwnedLegacyExecution({main,live,options,directory,archiveDirectory,coldState,leafBytes,originalPin,fullArchive,signedApproval,
adopt,release,tail,sourcePrivateKey,coldPrivateKey:key,coldPublicKey:publicKey,proof,coldApprovalSha256:sha(approval)})
}
return {coldProofIssued:true,distinctIssuerVerified:true,credentialDigestMatched:true,writePrivilegedRoleRefused:true,readyAuthorized:false}
}finally{modeledFiles?.restore()}
}
@@ -1,259 +0,0 @@
import assert from 'node:assert/strict'
import {randomBytes,randomUUID,sign,X509Certificate} from 'node:crypto'
import {mkdir,readFile,writeFile} from 'node:fs/promises'
import {join} from 'node:path'
import pg from 'pg'
export async function verifyOwnedLegacyExecution({main,live,options,directory,archiveDirectory,coldState,leafBytes,originalPin,fullArchive,signedApproval,
adopt,release,tail,sourcePrivateKey,coldPrivateKey,coldPublicKey,proof,coldApprovalSha256}){
assert.equal(process.env.MMAPP_OWN_PROBE_LEGACY_0037,'true')
const [{sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,readDatabaseSourceBindingRegistry},{readReviewedLegacyProbe0037},
{LEGACY_HANDOFF_EXECUTION_DOMAIN,LEGACY_HANDOFF_RELEASE_DOMAIN},{captureLegacyHandoffSchema},{LEGACY_PROBE_RELATIONS},{acquireDatabaseSchemaControl},{observation,exactHistory},
{MIGRATION_MANIFEST,migrationManifestDigest},{DATABASE_RESTORE_QUARANTINE_DDL},{createBootstrapMaterial}]=await Promise.all([
import('../src/db/database-source-binding.ts'),import('../src/db/legacy-probe-0037-body.ts'),import('./writer-profile/legacy-handoff-execution.mts'),
import('../src/services/server-probe-legacy-handoff-inspection.ts'),import('../src/services/server-probe-legacy-source-inventory.ts'),
import('../src/db/database-schema-control.ts'),import('../src/db/controlled-migration-history.ts'),import('../src/db/migration-manifest.ts'),
import('../src/db/database-restore-quarantine.ts'),import('./database-writer-bootstrap-operator.mjs'),
])
const sha=sourceBindingSha256,store=async(path,data)=>{await writeFile(path,data,{flag:'wx',mode:0o600});return path}
const publisher=options.user,operator='owned_commit_operator',control='owned_commit_control'
const appPassword=tail?.password??randomBytes(24).toString('hex'),controlPassword=randomBytes(24).toString('hex')
await live.query(DATABASE_RESTORE_QUARANTINE_DDL)
if(tail)assert.equal(tail.user,operator)
else await live.query(`CREATE ROLE ${operator} LOGIN PASSWORD '${appPassword}'; GRANT ${publisher} TO ${operator}; ALTER SCHEMA public OWNER TO ${operator}`)
await live.query(`CREATE ROLE ${control} LOGIN PASSWORD '${controlPassword}';
CREATE ROLE owned_commit_runtime NOLOGIN; CREATE ROLE owned_commit_capture NOLOGIN;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_system() TO ${control},${operator}; GRANT pg_read_all_stats TO ${control};
GRANT USAGE ON SCHEMA mmapp_control TO ${control},${operator}; GRANT SELECT, UPDATE ON mmapp_control.database_schema_state,mmapp_control.database_restore_quarantine TO ${control},${operator};
GRANT SELECT, INSERT, UPDATE ON mmapp_control.database_schema_intents TO ${control},${operator}`)
let runTail
if(tail){
const [{pool},{executeControlledSqlMigrations},{LEGACY_CONTROL_REFERENCE_DOMAIN,readControlledSchemaReference},{verifyControlledSchemaCurrentState}]=await Promise.all([
import('../src/db/index.ts'),import('../src/db/controlled-migrations.ts'),import('../src/db/controlled-schema-reference-core.ts'),
import('../src/db/controlled-schema-verification.ts'),
])
runTail=async(quarantined,handoff)=>{
const files=[],fileHashes=new Map(MIGRATION_MANIFEST.map(pin=>[pin.sha256,pin.file]))
const originalConnect=pool.connect
pool.connect=async()=>{
const owned=await originalConnect.call(pool),query=owned.query,release=owned.release
owned.query=function(text,...args){
const file=typeof text==='string'&&fileHashes.get(sha(Buffer.from(text)))
if(file)files.push(file)
return query.call(this,text,...args)
}
owned.release=function(...args){owned.query=query;owned.release=release;return release.apply(this,args)}
return owned
}
try{
if(quarantined){
await assert.rejects(executeControlledSqlMigrations(),error=>error.cause?.message==='Authenticated legacy migration release required')
assert.deepEqual(files,[])
return
}
const result=await executeControlledSqlMigrations()
assert.equal(result.completedFiles,MIGRATION_MANIFEST.length);assert.equal(result.ready,false)
assert.deepEqual(files,MIGRATION_MANIFEST.slice(37).map(pin=>pin.file))
}finally{pool.connect=originalConnect}
const referenceRoles={publisher:request.roles.publisher,operator:request.roles.operator,control:request.roles.control}
const approvalBody={format:'legacy-control-reference-approval-v1',manifestSha256,sourceHeadSha256:source.headSha256,
sourceTlsSpkiSha256:tls.spkiSha256,...referenceRoles,expiresAt:Date.now()+90000}
const referenceArgs=async(body,filename)=>{
const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_CONTROL_REFERENCE_DOMAIN+sourceBindingCanonical(body)),coldPrivateKey).toString('base64url')})
const flags=['--bootstrap','--bootstrap-sha256','--publisher-transport','--publisher-transport-sha256','--operator-transport','--operator-transport-sha256',
'--ca','--source-registry','--source-spki-sha256','--activation-directory','--activation-sha256','--cold-public-key','--cold-spki-sha256']
return ['legacy-control-reference',...flags.flatMap(flag=>[flag,args[args.indexOf(flag)+1]]),
'--approval',await store(join(directory,filename),bytes),'--approval-sha256',sha(bytes)]
}
await assert.rejects(main(await referenceArgs({...approvalBody,control:referenceRoles.operator},'wrong-control-reference.json')),/independent split-control approval/)
const approved=await referenceArgs(approvalBody,'control-reference.json')
const reference=await main(approved),current=await verifyControlledSchemaCurrentState()
assert.equal(reference.splitControlApprovalSha256,approved.at(-1));assert.equal(reference.referenceDatabaseDropped,true)
assert.equal((await readControlledSchemaReference()).sha256,current.schemaSha256)
assert.equal(current.schemaSha256,reference.logicalSchemaSha256);assert.equal(current.referenceStatus,'matched');assert.equal(current.ready,false)
assert.equal(current.currentPristineDataInvariants,false)
await live.query('GRANT USAGE ON SCHEMA mmapp_control TO owned_commit_runtime')
try{
const extra=await verifyControlledSchemaCurrentState()
assert.equal(extra.referenceStatus,'different');assert.equal(extra.ready,false);assert.equal(extra.dataFacts.length,0)
}finally{await live.query('REVOKE USAGE ON SCHEMA mmapp_control FROM owned_commit_runtime')}
const [{LEGACY_SOURCE58_PREFLIGHT_DOMAIN},{RESTORE_WRITER_PROFILE_DEFINITION_SHA256}]=await Promise.all([
import('../src/db/legacy-source58-transition.ts'),import('../src/db/database-restore-quarantine-profile.ts'),
])
const targetCatalogueSha256=sha(Buffer.from('owned signed intended Source58 target; installation outside this preflight'))
const target={format:'legacy-source58-preflight-approval-v1',manifestSha256,sourceHeadSha256:source.headSha256,
sourceTlsSpkiSha256:tls.spkiSha256,currentSchemaSha256:reference.logicalSchemaSha256,...handoff,
targetDefinitionSha256:RESTORE_WRITER_PROFILE_DEFINITION_SHA256,targetCatalogueSha256,roles:request.roles,expiresAt:Date.now()+90000}
const preflightArgs=async(body,name)=>{
const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_SOURCE58_PREFLIGHT_DOMAIN+sourceBindingCanonical(body)),coldPrivateKey).toString('base64url')})
const flags=approved.slice(1,-4),operatorAt=flags.indexOf('--operator-transport')
flags.splice(operatorAt,4)
return ['legacy-source58-preflight',...flags,'--approval',await store(join(directory,name),bytes),'--approval-sha256',sha(bytes)]
}
await assert.rejects(main(await preflightArgs({...target,releaseApprovalSha256:'0'.repeat(64)},'wrong-source58-target.json')),/exact authenticated release/)
const observed=await main(await preflightArgs(target,'source58-target.json'))
assert.equal(observed.authenticatedFiles,MIGRATION_MANIFEST.length);assert.equal(observed.transitionAuthorized,false);assert.equal(observed.readyAuthorized,false)
assert.equal(observed.approvedTargetCatalogueSha256,targetCatalogueSha256)
const expectedBlockers=['CONTROL_NAMESPACE_OWNER','QUARANTINE_OWNER','WRITER_REGISTRY_MISSING','REVIEWED_MARKER_FENCE_MISSING',
'ROLE_PROFILE_OPERATOR','ROLE_PROFILE_PUBLISHER','ROLE_PROFILE_RUNTIME','ROLE_PROFILE_CAPTURE']
assert.deepEqual(observed.blockers,expectedBlockers)
return {manifestSha256,logicalSchemaSha256:current.schemaSha256,referenceLogicalSchemaSha256:reference.logicalSchemaSha256,
referenceStatus:current.referenceStatus,splitControlApprovalSha256:reference.splitControlApprovalSha256,exactSplitControlCatalogueMatched:true,
invalidRoleRefused:true,excessNamespaceAclRefused:true,
referenceDatabaseDropped:reference.referenceDatabaseDropped,
files,legacySource58PredecessorAttested:true,wrongReleasePinRefused:true,source58Blockers:observed.blockers,
source58TransitionAuthorized:false,readyAuthorized:current.ready,historicalDataEquivalent:current.historicalDataEquivalent}
}
}
const manifestSha256=migrationManifestDigest(),controller=await acquireDatabaseSchemaControl(live)
try{
for(const migration of MIGRATION_MANIFEST.slice(0,36)){
const id=randomUUID(),begun=await controller.transition({type:'begin',id,kind:'migration',manifestSha256,artifactSha256:migration.sha256})
await controller.transition({type:'terminal',id,outcome:'observed-success',observationSha256:await observation(begun.state.intent,migration)})
}
}finally{await controller.close()}
const {body,pin}=await readReviewedLegacyProbe0037(),preview=new pg.Client({...options,user:operator,password:appPassword})
await preview.connect()
let preSchemaSha256,postSchemaSha256
try{
await preview.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY')
preSchemaSha256=await captureLegacyHandoffSchema(preview,Date.now()+30000);await preview.query('ROLLBACK')
await preview.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ WRITE')
await preview.query(`TRUNCATE TABLE ${LEGACY_PROBE_RELATIONS.map(name=>`ONLY public.${name}`).join(', ')} CONTINUE IDENTITY RESTRICT`)
await preview.query(body);await preview.query('SET TRANSACTION READ ONLY')
postSchemaSha256=await captureLegacyHandoffSchema(preview,Date.now()+30000)
}finally{try{await preview.query('ROLLBACK')}finally{await preview.end()}}
assert.notEqual(preSchemaSha256,postSchemaSha256)
const source=await readDatabaseSourceBindingRegistry(originalPin),host='localhost',tls={serverName:host,caSha256:sha(leafBytes),
spkiSha256:sha(new X509Certificate(leafBytes).publicKey.export({type:'spki',format:'der'}))}
const names={publisher,operator,control,runtime:'owned_commit_runtime',capture:'owned_commit_capture'}
const {rows:rows}=await live.query('SELECT rolname AS name,oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname=ANY($1)',[Object.values(names)])
const request={format:'application-writer-bootstrap-request-v1',installationId:originalPin.installationId,sourceHeadSha256:originalPin.currentHeadSha256,tls,
roles:Object.fromEntries(Object.entries(names).map(([scope,name])=>[scope,rows.find(role=>role.name===name)])),source:{systemIdentifier:source.body.source.systemIdentifier,
databaseOid:String(source.body.source.databaseOid),databaseName:source.body.source.databaseName,serverVersionNum:source.body.source.serverVersionNum,inRecovery:false}}
const bootstrap=sourceBindingBytes(createBootstrapMaterial(request,sha(sourceBindingBytes(request))).bootstrap)
const activeDirectory=join(directory,'owned-execute-activation');await mkdir(activeDirectory,{mode:0o700})
const activation=sourceBindingBytes({format:'database-source-activation-v1',activationDirectory:activeDirectory,installationId:originalPin.installationId,
sourceIdentityId:source.body.sourceIdentityId,status:'active',publicKeySpkiSha256:originalPin.publicKeySpkiSha256,
generations:[{generation:source.body.generation,headSha256:source.headSha256,bindingSha256:source.head.bindingSha256,tlsSpkiSha256:tls.spkiSha256}]})
await store(join(activeDirectory,'source-activation.json'),activation)
const operationId=randomUUID(),approvalBody={format:'legacy-probe-0037-execution-approval-v1',decision:'execute-and-commit',operationId,
sourceHeadSha256:originalPin.currentHeadSha256,sourceTlsSpkiSha256:tls.spkiSha256,custodySha256:sha(signedApproval),coldArchiveSha256:sha(fullArchive),
coldProofSha256:sha(proof),coldApprovalSha256,preSchemaSha256,postSchemaSha256,manifestSha256,migrationSha256:pin.sha256,protectedRuntimePaused:true,expiresAt:Date.now()+90000}
const approved=sourceBindingBytes({body:approvalBody,signature:sign(null,Buffer.from(LEGACY_HANDOFF_EXECUTION_DOMAIN+sourceBindingCanonical(approvalBody)),coldPrivateKey).toString('base64url')})
await store(join(archiveDirectory,'legacy-probe-cold-proof.json'),proof)
const shared=['--bootstrap',await store(join(directory,'execute-bootstrap.json'),bootstrap),'--bootstrap-sha256',sha(bootstrap),
'--ca',await store(join(directory,'execute-ca.pem'),leafBytes),'--source-registry',originalPin.registryDirectory,'--source-spki-sha256',originalPin.publicKeySpkiSha256,
'--activation-directory',activeDirectory,'--activation-sha256',sha(activation),'--archive-directory',archiveDirectory]
const args=['legacy-handoff-execute',...shared,'--legacy-key-directory',coldState,'--source-signing-key',await store(join(directory,'execute-source-key.pem'),sourcePrivateKey.export({type:'pkcs8',format:'pem'})),
'--approval',await store(join(directory,'execute-approval.json'),approved),'--approval-sha256',sha(approved),'--cold-public-key',await store(join(directory,'execute-cold-pub.pem'),coldPublicKey.export({type:'spki',format:'pem'})),
'--cold-spki-sha256',sha(coldPublicKey.export({type:'spki',format:'der'}))]
let publisherTransport,publisherSha
for(const [scope,user,password] of [['publisher',publisher,options.password],['operator',operator,appPassword],['control',control,controlPassword]]){
const transport=sourceBindingBytes({host,port:options.port,database:options.database,user,password})
const file=await store(join(directory,'execute-'+scope+'.json'),transport);args.push('--'+scope+'-transport',file,'--'+scope+'-transport-sha256',sha(transport))
if(scope==='publisher'){publisherTransport=file;publisherSha=sha(transport)}
}
const {rows:[credential]}=await live.query('SELECT id,digest FROM public.mcp_credentials LIMIT 1')
await live.query('UPDATE public.mcp_credentials SET digest=$2 WHERE id=$1',[credential.id,randomUUID()])
try{await assert.rejects(main(args),/Original credentials differ/)}finally{await live.query('UPDATE public.mcp_credentials SET digest=$2 WHERE id=$1',[credential.id,credential.digest])}
assert.equal((await live.query('SELECT operation_id FROM mmapp_control.database_restore_quarantine')).rows[0].operation_id,null)
const connect=pg.Pool.prototype.connect
let applicationCommits=0
pg.Pool.prototype.connect=function(...args){
const connection=connect.apply(this,args)
if(args.length||this.options.application_name!=='mmapp-legacy-handoff-owner')return connection
return connection.then(client=>{
const query=client.query
client.query=function(text,...params){
const result=query.call(this,text,...params)
if(text!=='COMMIT')return result
return result.then(()=>{applicationCommits++;throw Error('owned application COMMIT ACK lost')})
}
return client
})
}
try{await assert.rejects(main(args),{code:'LEGACY_HANDOFF_EXECUTION_UNKNOWN'})}finally{pg.Pool.prototype.connect=connect}
assert.equal(applicationCommits,1)
const intent=await readFile(join(archiveDirectory,'legacy-probe-handoff-intent.json'))
const unknown=JSON.parse(await readFile(join(archiveDirectory,'legacy-probe-handoff-application-unknown.json'),'utf8'))
assert.equal(unknown.operationId,operationId);assert.equal(unknown.automaticReplay,false)
const inspectArgs=['legacy-handoff-inspect',...shared,'--transport',publisherTransport,'--transport-sha256',publisherSha,'--handoff-intent-sha256',sha(intent)]
const inspected=await main(inspectArgs)
assert.equal(inspected.outcome,'committed');assert.equal(inspected.operationId,operationId);assert.equal(inspected.quarantineActive,true)
assert.equal((await live.query('SELECT operation_id FROM mmapp_control.database_restore_quarantine')).rows[0].operation_id,operationId)
await assert.rejects(main(args),{code:'DATABASE_RESTORE_QUARANTINE_UNAVAILABLE'})
assert.equal(applicationCommits,1)
if(adopt){
assert.equal(inspected.adoptionRecorded,false)
const adoptArgs=[...args];adoptArgs[0]='legacy-handoff-adopt'
adoptArgs.splice(adoptArgs.indexOf('--source-signing-key'),2,'--handoff-intent-sha256',sha(intent))
let action=adoptArgs,unknownCode='LEGACY_HANDOFF_ADOPTION_UNKNOWN',releaseApprovalSha256
if(release){
const adopted=await main(adoptArgs)
assert.equal(adopted.quarantineActive,true)
if(runTail)await runTail(true)
const signedRelease=async(adoptionSha256,name)=>{
const body={format:'legacy-probe-0037-release-approval-v1',decision:'release-migration-quarantine',operationId,intentSha256:sha(intent),adoptionSha256,
sourceHeadSha256:originalPin.currentHeadSha256,sourceTlsSpkiSha256:tls.spkiSha256,coldArchiveSha256:sha(fullArchive),
coldProofSha256:sha(proof),postSchemaSha256,manifestSha256,migrationSha256:pin.sha256,protectedRuntimePaused:true,expiresAt:Date.now()+90000}
const bytes=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_HANDOFF_RELEASE_DOMAIN+sourceBindingCanonical(body)),coldPrivateKey).toString('base64url')})
return ['legacy-handoff-release',...adoptArgs.slice(1),'--release-approval',await store(join(directory,name),bytes),'--release-approval-sha256',sha(bytes)]
}
await assert.rejects(main(await signedRelease('0'.repeat(64),'wrong-release.json')),/Exact committed legacy handoff phase/)
assert.equal((await main(inspectArgs)).quarantineActive,true)
action=await signedRelease(adopted.observationSha256,'release.json')
releaseApprovalSha256=action.at(-1);unknownCode='LEGACY_HANDOFF_RELEASE_UNKNOWN'
}
let controlCommits=0
pg.Pool.prototype.connect=function(...parameters){
const connection=connect.apply(this,parameters)
if(parameters.length||this.options.application_name!=='mmapp-writer-registry-enroll')return connection
return connection.then(client=>{
const query=client.query
client.query=function(text,...params){
const result=query.call(this,text,...params)
if(text!=='COMMIT')return result
return result.then(ack=>{if(++controlCommits===2)throw Error('owned adoption COMMIT ACK lost');return ack})
}
return client
})
}
try{await assert.rejects(main(action),{code:unknownCode})}finally{pg.Pool.prototype.connect=connect}
assert.equal(controlCommits,2)
const readback=await main(inspectArgs)
assert.equal(readback.adoptionRecorded,true);assert.equal(readback.outcome,'committed');assert.equal(readback.readyAuthorized,false)
const {rows:[gate]}=await live.query('SELECT q.operation_id::text AS active,s.state FROM mmapp_control.database_restore_quarantine q CROSS JOIN mmapp_control.database_schema_state s WHERE q.id=1 AND s.id=1')
const {rows:history}=await live.query('SELECT id::text,intent,terminal FROM mmapp_control.database_schema_intents')
assert.equal(gate.active,release?null:operationId);assert.equal(gate.state.intent,null)
assert.equal(gate.state.terminal.intent.artifactSha256,sha(intent))
assert.equal(await exactHistory(history,gate.state,manifestSha256,true),37)
if(release){
assert.equal(readback.releaseRecorded,true);assert.equal(readback.quarantineActive,false);assert.equal(readback.releaseApprovalSha256,releaseApprovalSha256)
assert.equal(gate.state.terminal.legacyRelease.approvalSha256,releaseApprovalSha256)
const {rows:[head]}=await live.query('SELECT journal_prefix_bytes,journal_prefix_sha256 FROM mmapp_control.database_restore_quarantine')
assert.deepEqual(head,{journal_prefix_bytes:null,journal_prefix_sha256:null})
const unknown=JSON.parse(await readFile(join(archiveDirectory,'legacy-probe-handoff-release-unknown.json'),'utf8'))
assert.deepEqual(unknown,{format:'legacy-probe-0037-release-unknown-v1',operationId,intentSha256:sha(intent),automaticReplay:false})
await assert.rejects(main(action),/Legacy release attempt requires inspection/)
if(runTail){
const markers=()=>live.query('SELECT id,xmin::text AS xmin,applied_at::text FROM public.schema_migrations ORDER BY id').then(result=>result.rows)
const ledger=()=>live.query('SELECT id::text,xmin::text AS xmin,intent,terminal FROM mmapp_control.database_schema_intents ORDER BY id').then(result=>result.rows)
const beforeMarkers=await markers(),beforeLedger=await ledger()
const result=await runTail(false,{operationId,intentSha256:sha(intent),releaseApprovalSha256})
assert.deepEqual((await markers()).slice(0,37),beforeMarkers)
const afterLedger=new Map((await ledger()).map(row=>[row.id,row]))
assert(beforeLedger.every(row=>JSON.stringify(row)===JSON.stringify(afterLedger.get(row.id))))
assert.equal((await main(inspectArgs)).releaseRecorded,true)
return {controlledLegacyTailCompleted:true,quarantinedDispatchRefused:true,originalReceiptsAndMarkersPreserved:true,...result}
}
return {legacyHandoffReleased:true,wrongAdoptionRefused:true,releaseAckLossObserved:true,exactAuthenticatedPrefix:37,
replayRefused:true,restoreJournalUnchanged:true,readyAuthorized:false}
}
const unknown=JSON.parse(await readFile(join(archiveDirectory,'legacy-probe-handoff-adoption-unknown.json'),'utf8'))
assert.deepEqual(unknown,{format:'legacy-probe-0037-adoption-unknown-v1',operationId,intentSha256:sha(intent),automaticReplay:false})
await assert.rejects(main(adoptArgs),/Legacy adoption attempt requires inspection/)
return {legacyHandoffAdopted:true,adoptionAckLossObserved:true,exactAuthenticatedPrefix:37,replayRefused:true,quarantineCleared:false,readyAuthorized:false}
}
return {legacyHandoffExecuted:true,credentialDriftRefused:true,applicationAckLossObserved:true,committedReconciled:true,quarantineCleared:false,readyAuthorized:false}
}
@@ -1,177 +0,0 @@
import assert from 'node:assert/strict'
import {randomUUID,sign,X509Certificate} from 'node:crypto'
import {mkdir,open,readFile,realpath,unlink,writeFile} from 'node:fs/promises'
import {dirname,join,sep} from 'node:path'
export async function verifyOwnedHandoffInspector({live,directory,options,leafBytes,originalPin,archive,fullArchive,signedApproval,privateKey,intentOnly=false}){
assert.equal(process.env.MMAPP_OWN_PROBE_LEGACY_0037,'true')
const [{acquireDatabaseSchemaControl},{observation},{MIGRATION_MANIFEST,migrationManifestDigest},
{captureMigrationSchema},{ownedLegacyProbe0037Body},{sourceBindingBytes,sourceBindingCanonical,sourceBindingSha256,readDatabaseSourceBindingRegistry},
{LEGACY_PROBE_HANDOFF_DOMAIN},{createBootstrapMaterial},{LEGACY_PROBE_RELATIONS,LEGACY_PROBE_CUSTODY_RELATIONS},{DATABASE_RESTORE_QUARANTINE_DDL,beginDatabaseRestoreQuarantine}]=await Promise.all([
import('../src/db/database-schema-control.ts'),import('../src/db/controlled-migration-history.ts'),import('../src/db/migration-manifest.ts'),
import('../src/db/migration-schema-inventory.ts'),import('../src/db/proposals/server-probe-legacy-0037-owned.ts'),import('../src/db/database-source-binding.ts'),
import('../src/services/server-probe-legacy-handoff-inspection.ts'),import('./database-writer-bootstrap-operator.mjs'),
import('../src/services/server-probe-legacy-source-inventory.ts'),import('../src/db/database-restore-quarantine.ts'),
])
const store=async(path,data)=>{await writeFile(path,data,{flag:'wx',mode:0o600});return path}
const {body:migrationBody,pin}=await ownedLegacyProbe0037Body(),manifestSha256=migrationManifestDigest()
const truncate=`TRUNCATE TABLE ${LEGACY_PROBE_RELATIONS.map(name=>`ONLY public.${name}`).join(', ')} CONTINUE IDENTITY RESTRICT`
const base=await live.connect()
let before,after
const schema=async sql=>{
await sql.query("SET LOCAL search_path=pg_catalog; SET LOCAL TimeZone='UTC'; SET LOCAL DateStyle='ISO, YMD'")
return (await captureMigrationSchema(sql,{scope:{applicationNamespaces:['public'],excludedControlNamespaces:['mmapp_control']},deadlineAt:Date.now()+30000})).sha256
}
try {
await base.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');before=await schema(base);await base.query('ROLLBACK')
await base.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ WRITE')
await base.query(truncate);await base.query(migrationBody);await base.query('SET TRANSACTION READ ONLY');after=await schema(base)
} finally { try { await base.query('ROLLBACK') } finally { base.release() } }
assert.notEqual(before,after)
await live.query(DATABASE_RESTORE_QUARANTINE_DDL)
const control=await acquireDatabaseSchemaControl(live)
try {
// The guarded fixture already installed exactly these 36 files on the owned DB.
for(const migration of MIGRATION_MANIFEST.slice(0,36)){
const id=randomUUID(), begun=await control.transition({type:'begin',id,kind:'migration',manifestSha256,artifactSha256:migration.sha256})
await control.transition({type:'terminal',id,outcome:'observed-success',observationSha256:await observation(begun.state.intent,migration)})
}
} finally { await control.close() }
const baseline=(await live.query('SELECT state FROM mmapp_control.database_schema_state WHERE id=1')).rows[0].state
const registry=await readDatabaseSourceBindingRegistry(originalPin),record=registry.body.source,host='localhost'
const tls={serverName:host,caSha256:sourceBindingSha256(leafBytes),spkiSha256:sourceBindingSha256(new X509Certificate(leafBytes).publicKey.export({type:'spki',format:'der'}))}
const names={publisher:options.user,runtime:'owned_handoff_runtime',operator:'owned_handoff_operator',control:'owned_handoff_control',capture:'owned_handoff_capture'}
await live.query(Object.values(names).filter(name=>name!==options.user).map(name=>`CREATE ROLE ${name} NOLOGIN`).join(';'))
const {rows:roles}=await live.query('SELECT rolname AS name,oid::text AS "signedOid" FROM pg_catalog.pg_roles WHERE rolname=ANY($1)',[Object.values(names)])
const request={format:'application-writer-bootstrap-request-v1',installationId:originalPin.installationId,sourceHeadSha256:originalPin.currentHeadSha256,tls,
roles:Object.fromEntries(Object.entries(names).map(([scope,name])=>[scope,roles.find(role=>role.name===name)])),
source:{systemIdentifier:record.systemIdentifier,databaseOid:String(record.databaseOid),databaseName:record.databaseName,serverVersionNum:record.serverVersionNum,inRecovery:false}}
const boot=sourceBindingBytes(createBootstrapMaterial(request,sourceBindingSha256(sourceBindingBytes(request))).bootstrap)
const transport=sourceBindingBytes({host,port:options.port,user:options.user,password:options.password,database:options.database})
const activationDirectory=join(directory,'owned-handoff-active');await mkdir(activationDirectory,{mode:0o700})
const activation=sourceBindingBytes({format:'database-source-activation-v1',activationDirectory,installationId:originalPin.installationId,
sourceIdentityId:registry.body.sourceIdentityId,status:'active',publicKeySpkiSha256:originalPin.publicKeySpkiSha256,
generations:[{generation:registry.body.generation,headSha256:registry.headSha256,bindingSha256:registry.head.bindingSha256,tlsSpkiSha256:tls.spkiSha256}]})
await store(join(activationDirectory,'source-activation.json'),activation)
const argv=['legacy-handoff-inspect','--bootstrap',await store(join(directory,'owned-handoff-bootstrap.json'),boot),'--bootstrap-sha256',sourceBindingSha256(boot),
'--transport',await store(join(directory,'owned-handoff-transport.json'),transport),'--transport-sha256',sourceBindingSha256(transport),
'--ca',await store(join(directory,'owned-handoff-ca.pem'),leafBytes),'--source-registry',originalPin.registryDirectory,'--source-spki-sha256',originalPin.publicKeySpkiSha256,
'--activation-directory',activationDirectory,'--activation-sha256',sourceBindingSha256(activation)]
let modeledFiles
if(process.platform==='win32'){
const {mock}=await import('node:test'),url=new URL('./writer-profile/operator-files.mjs',import.meta.url),files=await import(url.href)
const protectedDirectory=async path=>{assert(path===directory||path.startsWith(directory+sep));assert.equal(await realpath(path),path)}
modeledFiles=mock.module(url,{namedExports:{...files,protectedDirectory,async writeFile(path,blob,options){
await protectedDirectory(dirname(path));const file=await open(path,options.flag,options.mode)
try{await file.writeFile(blob);await file.sync()}finally{await file.close()}
},async readProtected(path,limit,hash){
await protectedDirectory(dirname(path));const bytes=await readFile(path);assert(bytes.length>0&&bytes.length<=limit);if(hash!==undefined)assert.equal(sourceBindingSha256(bytes),hash);return bytes
},async withJournalLease(path,work){
await protectedDirectory(path);const lock=join(path,'publisher-lease.lock'),file=await open(lock,'wx',0o600)
try{return await work()}finally{await file.close();await unlink(lock)}
}}})
}
try{
const {main}=await import('./database-writer-profile-operator.mts')
const {publishLegacyHandoffIntent}=await import('./writer-profile/publisher.mjs')
if(intentOnly){
const cases=['no-drain','control-refusal','acknowledged','lost-ack']
for(const testCase of cases){
const operationId=randomUUID(),archiveDirectory=join(directory,operationId);await mkdir(archiveDirectory,{mode:0o700})
for(const [name,blob] of [['legacy-probe.dump',archive],['legacy-probe-approval.json',signedApproval],['legacy-probe-cold.dump',fullArchive]])await store(join(archiveDirectory,name),blob)
const application=await live.connect()
if(testCase==='control-refusal')await live.query(`CREATE FUNCTION mmapp_control.owned_refuse_quarantine() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'owned control refusal'; END $$;
CREATE TRIGGER owned_refuse_quarantine BEFORE UPDATE ON mmapp_control.database_restore_quarantine FOR EACH ROW EXECUTE FUNCTION mmapp_control.owned_refuse_quarantine()`)
let control,commits=0
try{
if(testCase!=='no-drain')for(const key of [8723130,8723102,8723110])await application.query('SELECT pg_advisory_lock($1::bigint)',[key])
await application.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ WRITE')
if(testCase!=='no-drain')await application.query(`LOCK TABLE ${[...LEGACY_PROBE_CUSTODY_RELATIONS,'mcp_settings'].map(name=>`public.${name}`).join(', ')} IN ACCESS EXCLUSIVE MODE NOWAIT`)
const {rows:[{xid}]}=await application.query('SELECT pg_current_xact_id()::text AS xid')
const intent={format:'legacy-probe-0037-handoff-intent-v1',operationId,applicationXid:xid,approvalSha256:sourceBindingSha256(signedApproval),
coldArchiveSha256:sourceBindingSha256(fullArchive),preSchemaSha256:before,postSchemaSha256:after,manifestSha256,migrationSha256:pin.sha256}
const signedIntent=sourceBindingBytes({body:intent,signature:sign(null,Buffer.from(LEGACY_PROBE_HANDOFF_DOMAIN+sourceBindingCanonical(intent)),privateKey).toString('base64url')})
control=await acquireDatabaseSchemaControl({async connect(){
const client=await live.connect()
return {release:bad=>client.release(bad),async query(text,values){
const result=await client.query(text,values)
if(text==='COMMIT'&&++commits===2&&testCase==='lost-ack')throw Error('owned control COMMIT ACK lost')
return result
}}
}})
const publish=()=>publishLegacyHandoffIntent({application,control,sourcePin:originalPin,tlsPin:tls,archiveDirectory,signedIntent,deadlineAt:Date.now()+30000})
const intentFile=join(archiveDirectory,'legacy-probe-handoff-intent.json'), unknownFile=join(archiveDirectory,'legacy-probe-handoff-control-unknown.json')
if(testCase==='acknowledged'){
const receipt=await publish();assert.equal(receipt.intentRecorded,true);assert.equal(receipt.readyAuthorized,false)
}else if(testCase==='no-drain'){
await assert.rejects(publish(),/exclusive drain no longer held/)
await assert.rejects(readFile(intentFile),{code:'ENOENT'})
}else await assert.rejects(publish(),{outcomeUnknown:testCase==='lost-ack'})
if(testCase!=='no-drain')assert.equal(sourceBindingSha256(await readFile(intentFile)),sourceBindingSha256(signedIntent))
if(testCase==='lost-ack'){
const observation=JSON.parse(await readFile(unknownFile,'utf8'))
assert.equal(observation.operationId,operationId);assert.equal(observation.automaticReplay,false)
await assert.rejects(publish(),{code:'EEXIST'})
assert.equal(commits,2)
}else await assert.rejects(readFile(unknownFile),{code:'ENOENT'})
const recorded=testCase==='acknowledged'||testCase==='lost-ack'
const {rows:[facts]}=await live.query(`SELECT q.operation_id,s.state->'intent'->>'id' AS pending,
(SELECT count(*)::int FROM mmapp_control.database_schema_intents) AS history
FROM mmapp_control.database_restore_quarantine q CROSS JOIN mmapp_control.database_schema_state s WHERE q.id=1 AND s.id=1`)
assert.deepEqual(facts,{operation_id:recorded?operationId:null,pending:recorded?operationId:null,history:recorded?37:36})
await application.query('ROLLBACK')
await application.query('SELECT pg_advisory_unlock_all()')
if(recorded){
const result=await main([...argv,'--archive-directory',archiveDirectory,'--handoff-intent-sha256',sourceBindingSha256(signedIntent)])
assert.equal(result.outcome,'rolled-back');assert.equal(result.operationId,operationId);assert.equal(result.quarantineActive,true)
}
if(recorded&&testCase!=='lost-ack'){
await live.query('DELETE FROM mmapp_control.database_schema_intents WHERE id=$1',[operationId])
await live.query('UPDATE mmapp_control.database_schema_state SET state=$1::jsonb WHERE id=1',[JSON.stringify(baseline)])
await live.query('UPDATE mmapp_control.database_restore_quarantine SET operation_id=NULL WHERE id=1')
}
}finally{
try{await control?.close()}finally{application.release(true)}
if(testCase==='control-refusal')await live.query('DROP TRIGGER owned_refuse_quarantine ON mmapp_control.database_restore_quarantine; DROP FUNCTION mmapp_control.owned_refuse_quarantine()')
}
}
return {legacyHandoffIntentPublished:true,controlRefusalAtomic:true,controlAckLossRecordedUnknown:true,applicationCommit:false,readyAuthorized:false}
}
for(const commits of [false,true]){
const operationId=randomUUID(),archiveDirectory=join(directory,operationId);await mkdir(archiveDirectory,{mode:0o700})
for(const [name,blob] of [['legacy-probe.dump',archive],['legacy-probe-approval.json',signedApproval],['legacy-probe-cold.dump',fullArchive]])await store(join(archiveDirectory,name),blob)
const application=await live.connect()
try{
await application.query('BEGIN');const {rows:[{xid}]}=await application.query('SELECT pg_current_xact_id()::text AS xid')
const body={format:'legacy-probe-0037-handoff-intent-v1',operationId,applicationXid:xid,approvalSha256:sourceBindingSha256(signedApproval),
coldArchiveSha256:sourceBindingSha256(fullArchive),preSchemaSha256:before,postSchemaSha256:after,manifestSha256,migrationSha256:pin.sha256}
const intent=sourceBindingBytes({body,signature:sign(null,Buffer.from(LEGACY_PROBE_HANDOFF_DOMAIN+sourceBindingCanonical(body)),privateKey).toString('base64url')})
const file=await open(join(archiveDirectory,'legacy-probe-handoff-intent.json'),'wx',0o600)
try { await file.writeFile(intent);await file.sync() } finally { await file.close() }
const input=[...argv,'--archive-directory',archiveDirectory,'--handoff-intent-sha256',sourceBindingSha256(intent)]
const controller=await acquireDatabaseSchemaControl(live)
try{await controller.transition({type:'begin',id:operationId,kind:'migration',manifestSha256,artifactSha256:sourceBindingSha256(intent)})}finally{await controller.close()}
await beginDatabaseRestoreQuarantine(live,operationId)
const inspect=async expected=>{
const result=await main(input)
assert.deepEqual({outcome:result.outcome,operationId:result.operationId,quarantine:result.quarantineActive,ready:result.readyAuthorized},
{outcome:expected,operationId,quarantine:true,ready:false})
const {rows:[gates]}=await live.query(`SELECT q.operation_id,s.state->'intent'->>'id' AS pending
FROM mmapp_control.database_restore_quarantine q CROSS JOIN mmapp_control.database_schema_state s WHERE q.id=1 AND s.id=1`)
assert.deepEqual(gates,{operation_id:operationId,pending:operationId})
}
await inspect('unknown')
if(commits){await application.query(truncate);await application.query(migrationBody)}
await application.query(commits?'COMMIT':'ROLLBACK')
await inspect(commits?'committed':'rolled-back')
if(!commits){
// Own harness resets its control-only fixture for the independent COMMIT case.
await live.query('DELETE FROM mmapp_control.database_schema_intents WHERE id=$1',[operationId])
await live.query('UPDATE mmapp_control.database_schema_state SET state=$1::jsonb WHERE id=1',[JSON.stringify(baseline)])
await live.query('UPDATE mmapp_control.database_restore_quarantine SET operation_id=NULL WHERE id=1')
}
}finally{try{await application.query('ROLLBACK')}finally{application.release()}}
}
} finally { modeledFiles?.restore() }
return {legacyHandoffInspection:true,unknownObserved:true,rollbackObserved:true,commitObserved:true,quarantineCleared:false,readyAuthorized:false}
}
+3 -135
View File
@@ -64,47 +64,6 @@ async function scoped(client) {
} }
try { try {
assert.notEqual(process.pid, Number(process.env.OWN_SERVER_PID)) assert.notEqual(process.pid, Number(process.env.OWN_SERVER_PID))
phase = 'http:owner-logs-lifecycle'
{
const headers = { authorization: 'Bearer ' + process.env.OWN_PORTAL_JWT, 'content-type': 'application/json' }
const allowedServer = Number(process.env.OWN_ALLOWED_SERVER), forbiddenServer = Number(process.env.OWN_FORBIDDEN_SERVER)
const issuedResponse = await boundedFetch(origin + '/api/mcp/connections', {
method: 'POST', headers,
body: JSON.stringify({ name: 'Owned portal log reader', mode: 'observer', expiresAt: new Date(Date.now() + 600000).toISOString(),
permissions: ['mm:servers:read'], scope: { serverIds: [allowedServer], operationIds: ['logs.query'], allowGlobal: false } }),
})
assert.equal(issuedResponse.status, 200)
const { connection, token } = await issuedResponse.json()
const client = await opened('http', token)
const queryLogs = serverId => client.callTool({
name: 'mm_logs_query', arguments: { serverId, q: 'external-sdk-log-', limit: 1 },
}, undefined, { timeout: 5000 })
try {
assert.deepEqual((await client.listTools({}, { timeout: 5000 })).tools.map(tool => tool.name), ['mm_logs_query'])
const result = content(await queryLogs(allowedServer))
assert.deepEqual(result.entries.map(entry => entry.serverId), [allowedServer])
await denied(() => queryLogs(forbiddenServer))
const monitor = await boundedFetch(origin + '/api/mcp/requests?' + new URLSearchParams({
connectionId: connection.id, tool: 'mm_logs_query',
}), { headers })
assert.equal(monitor.status, 200)
const body = await monitor.text()
assert(!body.includes(token))
const { items } = JSON.parse(body)
assert.equal(items.length, 2)
assert(items.every(row => row.connection_id === connection.id && row.tool === 'mm_logs_query'))
assert(items.some(row => row.status === 'denied'))
assert(items.some(row => row.error_code === null && row.finished_at && row.result?.entries?.[0]?.serverId === allowedServer))
const revoke = await boundedFetch(origin + '/api/mcp/connections/' + connection.id + '/status', {
method: 'POST', headers, body: JSON.stringify({ status: 'revoked' }),
})
assert.equal(revoke.status, 200)
await denied(() => queryLogs(allowedServer))
await assert.rejects(opened('http', token))
cases.push('http: owner API issues restricted log access, monitors allow/deny and revokes the same SDK connection')
} finally { await client.close().catch(() => {}) }
}
for (const mode of ['http', 'stdio']) { for (const mode of ['http', 'stdio']) {
phase = mode + ':issue'; const initial = await control('new-connection'); let token = initial.token phase = mode + ':issue'; const initial = await control('new-connection'); let token = initial.token
phase = mode + ':initialize'; let client = await opened(mode, token) phase = mode + ':initialize'; let client = await opened(mode, token)
@@ -158,24 +117,9 @@ try {
} finally { await client.close().catch(() => {}) } } finally { await client.close().catch(() => {}) }
} }
phase = 'http:body-bound'; const bound = await control('new-connection') phase = 'http:body-bound'; const bound = await control('new-connection')
// Exercise the declared length check without racing the rejected upload. const oversized = await fetch(endpoint, { method: 'POST', headers: { authorization: 'Bearer ' + bound.token, 'content-type': 'application/json', accept: 'application/json,text/event-stream', connection: 'close' },
const { request } = await import('node:http') body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'mm_servers_list', arguments: { padding: 'x'.repeat(1048576) } } }), signal: AbortSignal.timeout(5000) })
const oversized = await new Promise((resolve, reject) => { assert.equal(oversized.status, 413); assert((await oversized.text()).length < 65536)
const req = request(endpoint, { method: 'POST', headers: { authorization: 'Bearer ' + bound.token,
'content-type': 'application/json', accept: 'application/json,text/event-stream', connection: 'close',
'content-length': '1048577' }, signal: AbortSignal.timeout(5000) }, response => {
let bytes = 0
response.on('data', chunk => {
bytes += chunk.length
if (bytes >= 65536) req.destroy(Error('Oversized refusal response'))
})
response.once('error', reject)
response.once('end', () => resolve(response.statusCode))
})
req.once('error', reject)
req.flushHeaders()
})
assert.equal(oversized, 413)
cases.push('actual HTTP request envelope over1MiB refused before tool execution; successful client results remain bounded') cases.push('actual HTTP request envelope over1MiB refused before tool execution; successful client results remain bounded')
phase = 'sdk:pkce'; const grant = await control('new-connection') phase = 'sdk:pkce'; const grant = await control('new-connection')
@@ -307,82 +251,6 @@ try {
assert.equal(management.status, 200) assert.equal(management.status, 200)
cases.push('portal owner revocation blocks an existing unexpired log artifact even with refreshed owner login') cases.push('portal owner revocation blocks an existing unexpired log artifact even with refreshed owner login')
} finally { await exportClient.close().catch(() => {}) } } finally { await exportClient.close().catch(() => {}) }
phase = 'portal:approved-plan-apply'
{
const headers = { authorization: 'Bearer ' + process.env.OWN_PORTAL_JWT, 'content-type': 'application/json' }
const settings = async () => {
const response = await boundedFetch(origin + '/api/logs/settings', { headers })
assert.equal(response.status, 200)
return response.json()
}
const before = await settings()
const intervalSec = before.intervalSec === 60 ? 61 : 60
const issued = await boundedFetch(origin + '/api/mcp/connections', {
method: 'POST', headers,
body: JSON.stringify({ name: 'Owned portal log settings approval', mode: 'approval',
expiresAt: new Date(Date.now() + 600000).toISOString(), permissions: ['mm:settings:admin'],
scope: { serverIds: [Number(process.env.OWN_ALLOWED_SERVER), Number(process.env.OWN_FORBIDDEN_SERVER)],
operationIds: ['logs.settings.update', 'changes.apply', 'operations.read'], allowGlobal: true } }),
})
assert.equal(issued.status, 200)
const { connection, token } = await issued.json()
const client = await opened('http', token)
try {
phase = 'portal:read-approved-plan-preview'
const call = (name, arguments_) => client.callTool({ name, arguments: arguments_ }, undefined, { timeout: 5000 })
const plan = content(await call('mm_logs_settings_update_plan', { settings: { intervalSec } }))
assert.equal(plan.state, 'awaiting-approval')
const planUrl = origin + '/api/mcp/plans/' + plan.planId
const review = await boundedFetch(planUrl, { headers })
assert.equal(review.status, 200)
const preview = await review.json()
assert.equal(preview.id, plan.planId)
assert.equal(preview.connectionId, connection.id)
assert.equal(preview.operationId, 'logs.settings.update')
assert.equal(preview.state, 'awaiting-approval')
assert.deepEqual(preview.input.settings, { intervalSec })
phase = 'portal:refuse-unapproved-plan'
const idempotencyKey = randomUUID()
const apply = () => call('mm_change_apply', { planId: plan.planId, idempotencyKey })
await denied(apply)
assert.deepEqual(await settings(), before)
phase = 'portal:approve-exact-plan'
const decision = await boundedFetch(planUrl + '/decision', {
method: 'POST', headers, body: JSON.stringify({ approve: true }),
})
assert.equal(decision.status, 200)
phase = 'portal:apply-and-read-settings'
const operation = content(await apply())
assert.match(operation.id, /^[a-f0-9-]{36}$/)
let terminal
const deadline = Date.now() + 10000
do {
terminal = content(await call('mm_operation_get', { operationId: operation.id }))
if (!['pending', 'running'].includes(terminal.state)) break
await new Promise(resolve => setTimeout(resolve, 100))
} while (Date.now() < deadline)
assert.equal(terminal.state, 'applied')
assert.equal(terminal.verification, 'passed')
assert.deepEqual(await settings(), { ...before, intervalSec })
assert.equal(content(await apply()).id, operation.id)
phase = 'portal:monitor-applied-plan'
const list = await boundedFetch(origin + '/api/mcp/operations?' + new URLSearchParams({ connectionId: connection.id }), { headers })
assert.equal(list.status, 200)
const { items } = await list.json()
assert.deepEqual(items.map(({ id, plan_id, state, verification }) => ({ id, plan_id, state, verification })), [
{ id: operation.id, plan_id: plan.planId, state: 'applied', verification: 'passed' },
])
phase = 'portal:revoke-after-apply'
const revoked = await boundedFetch(origin + '/api/mcp/connections/' + connection.id + '/status', {
method: 'POST', headers, body: JSON.stringify({ status: 'revoked' }),
})
assert.equal(revoked.status, 200)
await denied(apply)
await assert.rejects(opened('http', token))
cases.push('portal API approves the exact log settings plan; SDK applies once and owner sees the verified operation')
} finally { await client.close().catch(() => {}) }
}
phase = 'portal:revoke-approved-plan' phase = 'portal:revoke-approved-plan'
const planAccess = await control('new-revocable-plan-connection') const planAccess = await control('new-revocable-plan-connection')
const planClient = await opened('http', planAccess.token) const planClient = await opened('http', planAccess.token)
@@ -1,205 +0,0 @@
import assert from 'node:assert/strict'
import { createHash } from 'node:crypto'
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client'
import { resumeMcpSteps } from '../acceptance/mcp-step-journal.mjs'
async function withClient(endpoint, token, run) {
const client = new Client({ name: 'owned-logs-sdk', version: '1.0.0' })
try {
await client.connect(new StreamableHTTPClientTransport(endpoint, {
requestInit: { headers: { authorization: 'Bearer ' + token } },
}), { timeout: 5000 })
return await run(async (name, args = {}) => {
const reply = await client.callTool({ name, arguments: args }, undefined, { timeout: 5000 })
assert(!reply.isError, name + ' failed')
return reply.structuredContent.result
})
} finally {
await client.close()
}
}
export async function verifyLogsHistory(endpoint, token, serverId, entries, journal, fixture, appendEvent) {
assert.equal(endpoint.hostname, '127.0.0.1')
const [newest, oldest] = entries
const filters = { serverId, q: 'external-sdk-log-', levels: ['info'] }
const config = { journal, fixture, steps: [
{ id: 'mm_logs_query', async check(call) {
const first = await call('mm_logs_query', { ...filters, limit: 1 })
assert.deepEqual(first.entries.map(entry => entry.id), [newest.id])
assert.ok(first.nextCursor)
const next = await call('mm_logs_query', { ...filters, limit: 1, cursor: first.nextCursor })
assert.deepEqual(next.entries.map(entry => entry.id), [oldest.id])
assert.equal(next.nextCursor, null)
} },
{ id: 'mm_logs_summary_get', async check(call) {
const summary = await call('mm_logs_summary_get', filters)
assert.equal(summary.total, 2)
assert.equal(summary.levels.info, 2)
} },
{ id: 'mm_logs_get', async check(call) {
const entry = await call('mm_logs_get', { serverId, id: newest.id })
assert.equal(entry.id, newest.id)
assert.equal(entry.serverId, serverId)
assert.equal(entry.message, newest.message)
} },
{ id: 'mm_logs_context_get', async check(call) {
const context = await call('mm_logs_context_get', { serverId, id: newest.id })
assert.equal(context.entry.id, newest.id)
assert.deepEqual(context.before.map(entry => [entry.serverId, entry.id]), [[serverId, oldest.id]])
assert.deepEqual(context.after, [])
} },
{ id: 'mm_logs_follow', async check(call) {
const args = { ...filters, limit: 1, waitSeconds: 0 }
const first = await call('mm_logs_follow', args)
assert.deepEqual(first.entries, [], 'Initial follow starts at the current watermark')
assert.ok(first.cursor)
const id = await appendEvent()
const next = await call('mm_logs_follow', { ...args, cursor: first.cursor })
assert.deepEqual(next.entries.map(entry => entry.id), [id])
const end = await call('mm_logs_follow', { ...args, cursor: next.cursor })
assert.deepEqual(end.entries, [])
} },
].map(({ id, check }) => ({ id, kind: 'read', async run() {
await withClient(endpoint, token, check)
return null
} })) }
const result = await resumeMcpSteps(config)
assert.equal(result.status, 'passed', JSON.stringify(result))
return config.steps.map(step => step.id)
}
export async function verifyLogsSettingsPlan(endpoint, token, connectionId, ownerJwt, journal, fixture) {
assert.equal(endpoint.hostname, '127.0.0.1')
const owner = async (path, body) => {
const response = await fetch(new URL(path, endpoint), {
headers: { authorization: 'Bearer ' + ownerJwt, 'content-type': 'application/json' },
redirect: 'error',
signal: AbortSignal.timeout(5000),
...(body && { method: 'POST', body: JSON.stringify(body) }),
})
assert.equal(response.status, 200, path)
return response.json()
}
const settingsPath = '/api/logs/settings'
const before = await owner(settingsPath)
const intervalSec = before.intervalSec === 60 ? 61 : 60
return withClient(endpoint, token, async call => {
const planStep = 'mm_logs_settings_update_plan'
const applyStep = 'mm_change_apply'
const config = { journal, fixture, steps: [
{ id: planStep, kind: 'plan', async run({ accept }) {
const plan = await call(planStep, { settings: { intervalSec } })
accept(plan.planId)
assert.equal(plan.state, 'awaiting-approval')
return plan.planId
} },
{ id: 'owner.decision', kind: 'apply', async run({ evidence, accept }) {
const planId = evidence.get(planStep)
const path = '/api/mcp/plans/' + planId
const plan = await owner(path)
assert.equal(plan.id, planId)
assert.equal(plan.operationId, 'logs.settings.update')
assert.equal(plan.connectionId, connectionId)
assert.equal(plan.state, 'awaiting-approval')
assert.deepEqual(plan.input.settings, { intervalSec })
assert.deepEqual(await owner(settingsPath), before)
assert.deepEqual(await owner(path + '/decision', { approve: true }), { ok: true })
accept(planId)
return planId
}, async reconcile({ evidence }) {
const planId = evidence.get(planStep)
const plan = await owner('/api/mcp/plans/' + planId)
return plan.state === 'approved' && plan.connectionId === connectionId
? { state: 'verified', evidence: planId } : { state: 'unknown' }
} },
{ id: applyStep, kind: 'apply', async run({ evidence, idempotencyKey, accept }) {
const operation = await call(applyStep, { planId: evidence.get(planStep), idempotencyKey })
accept(operation.id)
return operation.id
} },
{ id: 'mm_operation_get', kind: 'read', async run({ evidence, attempt }) {
if (attempt === 1) throw Object.assign(new Error('Simulated interruption before status read'), { code: 'FIXTURE_INTERRUPTED' })
const operationId = evidence.get(applyStep)
const deadline = Date.now() + 10000
let operation
do {
operation = await call('mm_operation_get', { operationId })
assert.equal(operation.id, operationId)
assert.equal(operation.plan_id, evidence.get(planStep))
assert.equal(operation.connection_id, connectionId)
if (!['pending', 'running'].includes(operation.state)) break
await new Promise(resolve => setTimeout(resolve, 100))
} while (Date.now() < deadline)
assert.equal(operation.state, 'applied')
assert.equal(operation.verification, 'passed')
assert.deepEqual(await owner(settingsPath), { ...before, intervalSec })
return operationId
} },
] }
assert.deepEqual(await resumeMcpSteps(config), { status: 'failed', step: 'mm_operation_get' }, journal)
assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' }, journal)
return config.steps.map(step => step.id)
})
}
export async function verifyLogsReads(endpoint, serverId, exportToken, settingsToken, ownerJwt, journal, fixture) {
assert.equal(endpoint.hostname, '127.0.0.1')
const publish = async (call, accept) => {
const artifact = await call('mm_logs_export', { serverId, q: 'external-sdk-log-1', format: 'json' })
accept(artifact.artifactId)
assert.equal(artifact.rows, 1)
return artifact.artifactId
}
const read = async (call, artifactId) => {
const first = await call('mm_logs_export_read', { artifactId, limit: 16 })
assert(Number.isSafeInteger(first.bytes) && first.bytes > 16 && first.bytes <= 8192)
assert.equal(first.offset, 0)
assert.equal(first.nextOffset, 16)
assert.equal(first.complete, false)
const last = await call('mm_logs_export_read', { artifactId, offset: first.nextOffset })
assert.equal(last.offset, 16)
assert.equal(last.nextOffset, null)
assert.equal(last.complete, true)
for (const part of [first, last]) {
assert.equal(part.artifactId, artifactId)
assert.equal(part.sha256, first.sha256)
assert.equal(part.bytes, first.bytes)
}
const content = Buffer.concat([first, last].map(part => Buffer.from(part.dataBase64, 'base64')))
assert.equal(content.length, first.bytes)
assert.equal(createHash('sha256').update(content).digest('hex'), first.sha256)
assert.match(content.toString('utf8'), /external-sdk-log-1/)
assert.doesNotMatch(content.toString('utf8'), /external-sdk-log-2/)
return artifactId
}
const config = { journal, fixture, steps: [
{ id: 'mm_logs_export', kind: 'apply',
run: ({ accept }) => withClient(endpoint, exportToken, call => publish(call, accept)),
// An unknown publication has no idempotency key. Stop for reconciliation.
},
{ id: 'mm_logs_export_read', kind: 'read',
run: ({ evidence }) => withClient(endpoint, exportToken, call => read(call, evidence.get('mm_logs_export'))),
},
{ id: 'mm_logs_settings_get', kind: 'read', async run({ attempt }) {
if (attempt === 1) throw Object.assign(new Error('Simulated interruption before settings read'), {
code: 'FIXTURE_INTERRUPTED',
})
await withClient(endpoint, settingsToken, async call => {
const response = await fetch(new URL('/api/logs/settings', endpoint), {
headers: { authorization: 'Bearer ' + ownerJwt },
redirect: 'error',
signal: AbortSignal.timeout(5000),
})
assert.equal(response.status, 200)
const expected = await response.json()
assert.equal(expected.enabled, false)
assert.deepEqual(await call('mm_logs_settings_get'), expected)
})
return null
} },
] }
assert.deepEqual(await resumeMcpSteps(config), { status: 'failed', step: 'mm_logs_settings_get' }, journal)
assert.deepEqual(await resumeMcpSteps(config), { status: 'passed' }, journal)
return config.steps.map(step => step.id)
}
@@ -1,147 +0,0 @@
import assert from 'node:assert/strict'
import { createServer } from 'node:http'
import { join } from 'node:path'
import { setTimeout as delay } from 'node:timers/promises'
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client'
import { resumeServerPoll } from '../acceptance/mcp-server-poll-steps.mjs'
export async function verifyPollSteps(endpoint, pool, jwt, output) {
assert.equal(endpoint.hostname, '127.0.0.1')
const replies = {
'/rest/system/identity': { name: 'Owned synthetic router' },
'/rest/system/resource': { version: '7.owned', 'board-name': 'Owned', uptime: '1h', 'cpu-load': '2', 'free-memory': '1024', 'total-memory': '2048' },
'/rest/interface': [{ name: 'ether1', type: 'ether' }],
'/rest/ip/address': [{ interface: 'ether1', address: '192.0.2.1/24' }],
}
const native = []
const router = createServer((request, response) => {
native.push(request.url)
if (request.method !== 'GET' || !Object.hasOwn(replies, request.url)) return response.writeHead(403).end()
response.setHeader('content-type', 'application/json')
response.end(JSON.stringify(replies[request.url]))
})
let client
try {
await new Promise((resolve, reject) => router.once('error', reject).listen(0, '127.0.0.1', resolve))
const { rows: [server] } = await pool.query(`INSERT INTO servers(name,host,port,use_ssl,verify_ssl,enabled,username,password)
VALUES('Owned poll','127.0.0.1',$1,false,false,true,'owned-router','fixture') RETURNING id`, [router.address().port])
const serverId = Number(server.id)
const { rows: catalogue } = await pool.query('SELECT id FROM servers ORDER BY id')
const owner = async (path, body) => {
const response = await fetch(new URL(path, endpoint), {
headers: { authorization: 'Bearer ' + jwt, 'content-type': 'application/json' },
...(body && { method: 'POST', body: JSON.stringify(body) }),
redirect: 'error', signal: AbortSignal.timeout(5000),
})
assert.equal(response.status, 200, path)
return response.json()
}
const { connection, token } = await owner('/api/mcp/connections', {
name: 'Owned poll steps', mode: 'approval', expiresAt: new Date(Date.now() + 180000).toISOString(),
permissions: ['mm:servers:read', 'mm:servers:write', 'mm:settings:admin'],
scope: { serverIds: catalogue.map(row => Number(row.id)), operationIds: ['servers.poll', 'changes.apply', 'operations.read'], allowGlobal: true },
limits: { callsPerMinute: 120, mutationsPerHour: 10000, objectsPerPlan: 10000 },
})
const connect = async () => {
await client?.close()
client = new Client({ name: 'owned-poll-steps', version: '1.0.0' })
await client.connect(new StreamableHTTPClientTransport(endpoint, {
requestInit: { headers: { authorization: 'Bearer ' + token } },
}), { timeout: 5000 })
}
await connect()
const journal = join(output, 'poll-' + connection.id + '.jsonl')
let planId
let approvedExpiresAt
let operationId
let operation
let interruptStatus = true
let applyKey
const config = {
journal, fixture: 'owned-poll', serverId, connectionId: connection.id,
async findAcceptedOperation({ connectionId: scope, planId: plan, idempotencyKey }) {
assert.equal(operationId, undefined)
assert.equal(scope, connection.id)
assert.equal(plan, planId)
assert.equal(idempotencyKey, applyKey)
const { rows } = await pool.query(`SELECT id FROM mcp_operations
WHERE connection_id=$1 AND plan_id=$2 AND idempotency_key=$3`, [scope, plan, idempotencyKey])
assert.equal(rows.length, 1)
operationId = rows[0].id
return operationId
},
async call(name, args) {
if (name === 'mm_operation_get' && interruptStatus) {
interruptStatus = false
assert.equal(args.operationId, operationId)
throw Object.assign(new Error('Simulated interruption before reading status'), { code: 'FIXTURE_INTERRUPTED' })
}
const reply = await client.callTool({ name, arguments: args }, undefined, { timeout: name === 'mm_server_poll_plan' ? 45000 : 5000 })
if (reply.isError) {
const error = JSON.parse(reply.content.find(item => item.type === 'text').text)
const code = /^[A-Z0-9_]{1,64}$/.test(error.code) ? error.code : 'POLL_SDK_ERROR'
console.error('Owned SDK tool refused:', name, code)
throw Object.assign(new Error(name + ' failed'), { code })
}
const result = reply.structuredContent.result
if (name === 'mm_server_poll_plan') planId = result.planId
if (name === 'mm_change_apply') {
assert.equal(applyKey, undefined, 'Apply cannot be sent twice')
applyKey = args.idempotencyKey
// The endpoint accepted the request, but the journal never sees its response.
throw Object.assign(new Error('Simulated lost apply response'), { code: 'FIXTURE_LOST_APPLY' })
}
if (name === 'mm_operation_get') operation = result
return result
},
async assertApproved(id) {
const plan = await owner('/api/mcp/plans/' + id)
assert.equal(plan.connectionId, connection.id)
assert.equal(plan.operationId, 'servers.poll')
if (plan.state !== 'approved') throw Object.assign(new Error('Waiting for owner approval'), { code: 'POLL_AWAITING_APPROVAL' })
const expiresAt = Date.parse(plan.expiresAt)
assert(expiresAt > Date.now(), 'Approved poll must have a live expiry')
approvedExpiresAt = expiresAt
},
}
assert.deepEqual(await resumeServerPoll(config), { status: 'failed', step: 'manager.plan-approved' }, journal)
assert.ok(planId)
assert.deepEqual(await owner('/api/mcp/plans/' + planId + '/decision', { approve: true }), { ok: true })
assert.deepEqual(await resumeServerPoll(config), { status: 'failed', step: 'mm_change_apply' }, journal)
assert.ok(applyKey)
assert.equal(operationId, undefined)
await connect()
assert.deepEqual(await resumeServerPoll(config), { status: 'failed', step: 'mm_operation_get' }, journal)
assert.ok(operationId)
await connect()
const observationStartedAt = Date.now()
let state
while (true) {
const { rows } = await pool.query('SELECT state FROM mcp_operations WHERE id=$1 AND connection_id=$2', [operationId, connection.id])
state = rows[0]?.state
const remaining = approvedExpiresAt - Date.now()
if (!['pending', 'running'].includes(state) || remaining <= 0) break
await delay(Math.min(500, remaining))
}
const observedMs = Date.now() - observationStartedAt
const approvedLifetimeRemainingAtStartMs = approvedExpiresAt - observationStartedAt
assert.deepEqual(await resumeServerPoll(config), { status: 'passed' },
`${journal} DB state ${state} after ${observedMs} ms (original approval window ${approvedLifetimeRemainingAtStartMs} ms at start)`)
assert.equal(operation.id, operationId)
const receipt = operation.steps[0].result
assert.equal(receipt.snapshot.status, 'online')
assert.deepEqual(native.sort(), Object.keys(replies).sort())
const { rows } = await pool.query(`SELECT tool,count(*)::int AS count FROM mcp_requests
WHERE connection_id=$1 AND tool IN ('mm_server_poll_plan','mm_change_apply','mm_operation_get') GROUP BY tool`, [connection.id])
const calls = Object.fromEntries(rows.map(row => [row.tool, row.count]))
assert.equal(calls.mm_server_poll_plan, 1)
assert.equal(calls.mm_change_apply, 1)
assert.equal(calls.mm_operation_get, 1)
return { journal, operationId, calls, reconciledLostApply: true, sdkReconnects: 2, nativeReads: native.length, publications: receipt.completedPublications,
state: operation.state, verification: operation.verification, localPostgres: true, sdkHttp: true,
approvedExpiresAt: new Date(approvedExpiresAt).toISOString(), approvedLifetimeRemainingAtStartMs, observedMs }
} finally {
await client?.close()
if (router.listening) await new Promise((resolve, reject) => router.close(error => error ? reject(error) : resolve()))
}
}
@@ -1,138 +0,0 @@
import assert from 'node:assert/strict'
import { randomUUID } from 'node:crypto'
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client'
import { StdioClientTransport } from '@modelcontextprotocol/client/stdio'
const limit = 1024 * 1024
const budgetMeta = 'io.mikrotikmanager/result-budget'
const comment = 'Ж'.repeat(11000)
const payloadBytes = value => Buffer.byteLength(JSON.stringify(value), 'utf8')
const tools = ['mm_servers_list', 'mm_logs_settings_update_plan', 'mm_change_apply', 'mm_operation_get']
export async function verifyResultEnvelope(endpoint, pool, jwt, bridgeFile) {
assert.equal(new URL(endpoint).hostname, '127.0.0.1')
const { rows: [{ count: existingServers }] } = await pool.query('SELECT count(*)::int AS count FROM servers')
await pool.query(`
INSERT INTO servers(name,host,enabled,comment)
SELECT 'owned-envelope-'||n, '192.0.2.'||(10+n), false, $1 FROM generate_series(1,24) AS n
`, [comment])
const { rows: servers } = await pool.query('SELECT id FROM servers ORDER BY id')
// Independently observe actual writes, including an accidental repeat with unchanged settings.
await pool.query(`
CREATE TABLE owned_log_settings_writes (interval_sec integer NOT NULL);
CREATE FUNCTION owned_count_log_settings_write() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO owned_log_settings_writes VALUES (NEW.interval_sec);
RETURN NEW;
END $$;
CREATE TRIGGER owned_count_log_settings_write AFTER UPDATE ON router_log_settings
FOR EACH ROW EXECUTE FUNCTION owned_count_log_settings_write()`)
const headers = { authorization: 'Bearer ' + jwt, 'content-type': 'application/json' }
const json = async (path, body) => {
const response = await fetch(new URL(path, endpoint), {
headers, signal: AbortSignal.timeout(10000), ...(body ? { method: 'POST', body: JSON.stringify(body) } : {}),
})
assert.equal(response.status, 200, path)
return response.json()
}
const cases = []
for (const mode of ['http', 'stdio']) {
const { connection, token } = await json('/api/mcp/connections', {
name: 'Owned result envelope ' + mode, mode: 'approval', expiresAt: new Date(Date.now() + 600000).toISOString(),
permissions: ['mm:servers:read', 'mm:settings:admin'],
limits: { serversPerPlan: servers.length, mutationsPerHour: servers.length },
scope: { serverIds: servers.map(row => Number(row.id)), allowGlobal: true,
operationIds: ['servers.list', 'logs.settings.update', 'changes.apply', 'operations.read'] },
})
const client = new Client({ name: 'owned-result-envelope-' + mode, version: '1.0.0' })
const transport = mode === 'http'
? new StreamableHTTPClientTransport(new URL(endpoint), { requestInit: { headers: { authorization: 'Bearer ' + token } } })
: new StdioClientTransport({ command: process.execPath, args: ['--import', import.meta.resolve('tsx'), bridgeFile],
cwd: process.cwd(), env: { MCP_URL: endpoint, MCP_TOKEN: token }, maxBufferSize: limit + 65536, stderr: 'pipe' })
transport.stderr?.on('data', () => {})
try {
await client.connect(transport, { timeout: 10000 })
const observedTools = (await client.listTools({}, { timeout: 5000 })).tools.map(tool => tool.name)
assert.deepEqual(observedTools.sort(), [...tools].sort())
const call = async (name, args) => {
const result = await client.callTool({ name, arguments: args }, undefined, { timeout: 10000 })
assert(payloadBytes(result) <= limit, mode + ' ' + name)
return result
}
const output = result => {
if (result.isError) assert.fail(JSON.parse(result.content[0].text).code)
const text = JSON.parse(result.content[0].text)
assert.deepEqual(result.structuredContent.result, text)
return text
}
const accepted = await call('mm_servers_list', { limit: 23, offset: existingServers })
const page = output(accepted)
assert.equal(page.items.length, 23)
assert(page.items.every(item => item.comment === comment))
assert(payloadBytes(accepted) >= 1000000)
// Even without field names and framing this page's duplicated UTF-8 text exceeds 1 MiB.
assert(24 * Buffer.byteLength(comment, 'utf8') * 2 > limit)
const refused = await call('mm_servers_list', { limit: 24, offset: existingServers })
assert.equal(refused.isError, true)
assert.equal(refused.structuredContent, undefined)
assert.equal(JSON.parse(refused.content[0].text).code, 'RESULT_TOO_LARGE')
assert.deepEqual(refused._meta[budgetMeta], {
version: 1, detailsOmitted: true, reason: 'byte-budget', automaticReplay: false,
})
const before = await json('/api/logs/settings')
const intervalSec = before.intervalSec === 60 ? 61 : 60
const plan = output(await call('mm_logs_settings_update_plan', { settings: { intervalSec } }))
assert.equal(plan.state, 'awaiting-approval')
await json('/api/mcp/plans/' + plan.planId + '/decision', { approve: true })
const applyArgs = { planId: plan.planId, idempotencyKey: randomUUID() }
const operation = output(await call('mm_change_apply', applyArgs))
let terminal
for (const end = Date.now() + 10000; Date.now() < end;) {
terminal = output(await call('mm_operation_get', { operationId: operation.id }))
if (!['pending', 'running'].includes(terminal.state)) break
await new Promise(resolve => setTimeout(resolve, 100))
}
assert.equal(terminal.state, 'applied')
assert.equal(terminal.verification, 'passed')
const writes = async () => (await pool.query('SELECT count(*)::int AS n FROM owned_log_settings_writes')).rows[0].n
const checkEffect = async () => {
assert.equal(await writes(), cases.length + 1)
assert.deepEqual(await json('/api/logs/settings'), { ...before, intervalSec })
}
await checkEffect()
// Supply namespaced synthetic optional details to the actual, already applied operation.
// The ordinary runtime read and duplicate-apply paths must keep its original outcome.
const oversizedDetails = JSON.stringify(Array(24).fill(comment))
const staged = await pool.query(`
UPDATE mcp_operations SET result=jsonb_set(COALESCE(result, '{}'::jsonb), '{ownedEnvelopeDetails}', $2::jsonb)
WHERE id=$1 AND state='applied' RETURNING id`, [operation.id, oversizedDetails])
assert.equal(staged.rowCount, 1)
const compactRead = await call('mm_operation_get', { operationId: operation.id })
const compactApply = await call('mm_change_apply', applyArgs)
for (const compact of [compactRead, compactApply]) {
assert(payloadBytes(compact) < 2048)
assert.deepEqual(compact._meta[budgetMeta], refused._meta[budgetMeta])
assert.deepEqual(output(compact), {
id: operation.id, plan_id: plan.planId, state: 'applied', verification: 'passed', error_code: null,
})
}
await checkEffect()
const { rows: [counts] } = await pool.query(`
SELECT
(SELECT count(*)::int FROM mcp_operations WHERE connection_id=$1) AS operations,
(SELECT count(*)::int FROM mcp_operation_steps WHERE operation_id=$2 AND state='verified') AS verified_steps,
(SELECT count(*)::int FROM mcp_requests WHERE connection_id=$1 AND tool='mm_change_apply') AS apply_requests
`, [connection.id, operation.id])
assert.deepEqual(counts, { operations: 1, verified_steps: 1, apply_requests: 2 })
cases.push({ mode, nearLimitBytes: payloadBytes(accepted), refusalBytes: payloadBytes(refused),
compactReadBytes: payloadBytes(compactRead), compactApplyBytes: payloadBytes(compactApply),
...counts, settingsWrites: 1 })
} finally {
await client.close()
}
}
return { scope: 'complete CallToolResult from HTTP SDK and production stdio bridge on owned PostgreSQL',
limitBytes: limit, wholeGenericResultEnvelopeLimitAccepted: true, cases }
}
@@ -7,28 +7,15 @@ import { createServer } from 'node:net'
import { spawn } from 'node:child_process' import { spawn } from 'node:child_process'
const sourceRoot = resolve(process.argv[2] ?? fileURLToPath(new URL('../../../', import.meta.url))) const sourceRoot = resolve(process.argv[2] ?? fileURLToPath(new URL('../../../', import.meta.url)))
const scenario = process.argv[3] ?? 'acceptance' const scenario = process.argv[3] ?? 'acceptance'
if (!['acceptance', 'bridge-vlan', 'logs-read', 'logs-history', 'logs-settings', 'poll-steps', 'result-envelope'].includes(scenario)) throw Error('Unsupported owned external scenario') if (!['acceptance', 'bridge-vlan'].includes(scenario)) throw Error('Unsupported owned external scenario')
const logsScenario = scenario.startsWith('logs-') const output = resolve(sourceRoot, scenario === 'bridge-vlan' ? 'tmp/mcp-bridge-vlan-sdk-20261105' : 'tmp/mcp-external-process-acceptance'); await mkdir(output, { recursive: true })
const envelopeScenario = scenario === 'result-envelope'
const reportRoot = resolve(sourceRoot, scenario === 'bridge-vlan' ? 'tmp/mcp-bridge-vlan-sdk-20261105' :
logsScenario || scenario === 'poll-steps' || envelopeScenario ? 'tmp/mcp-external-' + scenario : 'tmp/mcp-external-process-acceptance')
await mkdir(reportRoot, { recursive: true })
const output = scenario === 'poll-steps' || envelopeScenario ? await mkdtemp(join(reportRoot, 'run-')) : reportRoot
const paths = ['backend/src/scripts/mcp-stdio.ts', 'backend/src/routes/mcp-transport.ts', 'backend/src/routes/mcp-oauth.ts', 'backend/src/modules/mcp/store.ts', const paths = ['backend/src/scripts/mcp-stdio.ts', 'backend/src/routes/mcp-transport.ts', 'backend/src/routes/mcp-oauth.ts', 'backend/src/modules/mcp/store.ts',
'backend/src/modules/mcp/runtime.ts', 'backend/src/modules/mcp/actions.ts', 'backend/src/modules/mcp/security.ts', 'backend/src/modules/mcp/protocol-limits.ts', 'backend/src/modules/mcp/runtime.ts', 'backend/src/modules/mcp/actions.ts', 'backend/src/modules/mcp/security.ts', 'backend/src/modules/mcp/protocol-limits.ts',
'backend/src/modules/mcp/portal-revocation.ts', 'backend/src/plugins/auth.ts', 'backend/src/index.ts', 'node_modules/@modelcontextprotocol/client/package.json', 'node_modules/@modelcontextprotocol/server/package.json'] 'backend/src/modules/mcp/portal-revocation.ts', 'backend/src/plugins/auth.ts', 'backend/src/index.ts', 'node_modules/@modelcontextprotocol/client/package.json', 'node_modules/@modelcontextprotocol/server/package.json']
if (scenario !== 'bridge-vlan') paths.push('backend/scripts/mcp-external-process/client.mjs', 'backend/scripts/mcp-external-process/server-fixture.ts', if (scenario === 'acceptance') paths.push('backend/scripts/mcp-external-process/client.mjs', 'backend/scripts/mcp-external-process/server-fixture.ts',
'backend/src/routes/mcp-portal.ts', 'backend/src/modules/mcp/portal-revocation-event.ts', 'backend/src/services/router-logs-query.ts') 'backend/src/routes/mcp-portal.ts', 'backend/src/modules/mcp/portal-revocation-event.ts', 'backend/src/services/router-logs-query.ts')
else paths.push('backend/scripts/mcp-external-process/bridge-vlan-client.mjs', 'backend/scripts/mcp-external-process/bridge-vlan-fixture.ts', else paths.push('backend/scripts/mcp-external-process/bridge-vlan-client.mjs', 'backend/scripts/mcp-external-process/bridge-vlan-fixture.ts',
'backend/src/modules/mcp/bridge-vlan-actions.ts', 'backend/src/services/bridge-vlan-configuration.ts', 'packages/contracts/src/bridge-vlan.ts') 'backend/src/modules/mcp/bridge-vlan-actions.ts', 'backend/src/services/bridge-vlan-configuration.ts', 'packages/contracts/src/bridge-vlan.ts')
if (logsScenario) paths.push('backend/scripts/mcp-external-process/logs-read-client.mjs',
'backend/scripts/acceptance/mcp-step-journal.mjs', 'backend/src/modules/mcp/log-artifacts.ts')
if (scenario === 'poll-steps') paths.push('backend/scripts/mcp-external-process/poll-steps-fixture.mjs',
'backend/scripts/acceptance/mcp-step-journal.mjs', 'backend/scripts/acceptance/mcp-server-poll-steps.mjs',
'backend/src/modules/mcp/server-poll-production.ts', 'backend/src/services/server-poll-execution.ts')
if (envelopeScenario) paths.push('backend/scripts/mcp-external-process/run-owned.mjs', 'backend/scripts/mcp-external-process/result-envelope-fixture.mjs',
'backend/src/modules/mcp/result-budget.ts', 'backend/src/modules/mcp/result-delivery.ts', 'backend/src/modules/mcp/flow-envelope-allowance.ts',
'backend/src/modules/mcp/sdk-terminal-budget.ts', 'backend/src/modules/mcp/stdio-terminal-budget.ts')
async function pins() { const result = []; for (const path of paths) result.push({ path, sha256: createHash('sha256').update(await readFile(resolve(sourceRoot, path))).digest('hex') }); return result } async function pins() { const result = []; for (const path of paths) result.push({ path, sha256: createHash('sha256').update(await readFile(resolve(sourceRoot, path))).digest('hex') }); return result }
const before = await pins(), directory = await mkdtemp(join(resolve(sourceRoot, 'tmp'), 'mm-mcp-external-')), state = join(directory, 'state'); await mkdir(state) const before = await pins(), directory = await mkdtemp(join(resolve(sourceRoot, 'tmp'), 'mm-mcp-external-')), state = join(directory, 'state'); await mkdir(state)
const listener = createServer(); await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)); const port = listener.address().port; await new Promise(resolve => listener.close(resolve)) const listener = createServer(); await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)); const port = listener.address().port; await new Promise(resolve => listener.close(resolve))
@@ -40,17 +27,12 @@ try {
process.exitCode = await new Promise((done, reject) => { process.exitCode = await new Promise((done, reject) => {
const child = spawn(process.execPath, ['--import', import.meta.resolve('tsx'), fileURLToPath(new URL(scenario === 'bridge-vlan' ? './bridge-vlan-fixture.ts' : './server-fixture.ts', import.meta.url))], { cwd: directory, const child = spawn(process.execPath, ['--import', import.meta.resolve('tsx'), fileURLToPath(new URL(scenario === 'bridge-vlan' ? './bridge-vlan-fixture.ts' : './server-fixture.ts', import.meta.url))], { cwd: directory,
windowsHide: true, stdio: 'inherit', env: { ...Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'PATH', 'Path'].filter(k => process.env[k]).map(k => [k, process.env[k]])), windowsHide: true, stdio: 'inherit', env: { ...Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'PATH', 'Path'].filter(k => process.env[k]).map(k => [k, process.env[k]])),
OWN_EXTERNAL_CLEANUP_FAULT: process.env.OWN_EXTERNAL_CLEANUP_FAULT, OWN_EXTERNAL_LOGS_SCENARIO: logsScenario ? scenario : '', OWN_EXTERNAL_CLEANUP_FAULT: process.env.OWN_EXTERNAL_CLEANUP_FAULT, OWN_EXTERNAL_SOURCE_ROOT: sourceRoot, OWN_EXTERNAL_RESULT_FILE: join(output, 'result.json'), NODE_ENV: 'test', OWN_EXTERNAL_PROCESS_PG: 'true', DATABASE_URL: `postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`, DATABASE_PATH: join(directory, 'no-legacy.sqlite'),
OWN_EXTERNAL_POLL_STEPS: scenario === 'poll-steps' ? 'true' : '',
OWN_EXTERNAL_RESULT_ENVELOPE: envelopeScenario ? 'true' : '',
OWN_EXTERNAL_SOURCE_ROOT: sourceRoot, OWN_EXTERNAL_RESULT_FILE: join(output, 'result.json'), NODE_ENV: 'test', OWN_EXTERNAL_PROCESS_PG: 'true', DATABASE_URL: `postgresql://mcp_test:${password}@127.0.0.1:${port}/postgres`, DATABASE_PATH: join(directory, 'no-legacy.sqlite'),
MCP_STATE_DIR: state, AUTH_REQUIRED: 'true', AUTH_JWT_SECRET: 'owned-external-synthetic-issuer-test-only', AUTH_ISSUER: 'https://synthetic-portal.invalid', AUTH_PORTAL_URL: 'https://synthetic-portal.invalid' } }) MCP_STATE_DIR: state, AUTH_REQUIRED: 'true', AUTH_JWT_SECRET: 'owned-external-synthetic-issuer-test-only', AUTH_ISSUER: 'https://synthetic-portal.invalid', AUTH_PORTAL_URL: 'https://synthetic-portal.invalid' } })
child.once('error', reject); child.once('exit', code => done(code ?? 1)) child.once('error', reject); child.once('exit', code => done(code ?? 1))
}) })
const after = await pins(), unchanged = JSON.stringify(before) === JSON.stringify(after) const after = await pins(), unchanged = JSON.stringify(before) === JSON.stringify(after)
await writeFile(join(output, 'source-pins.json'), JSON.stringify({ before, after, unchanged, exitCode: process.exitCode }, null, 2)) await writeFile(join(output, 'source-pins.json'), JSON.stringify({ before, after, unchanged, exitCode: process.exitCode }, null, 2))
if (scenario === 'poll-steps') console.log('Poll checkpoint evidence:', output)
if (envelopeScenario) console.log('Result envelope evidence:', output)
console.log('External-client source/SDK pins unchanged:', unchanged); if (!unchanged) process.exitCode = 1 console.log('External-client source/SDK pins unchanged:', unchanged); if (!unchanged) process.exitCode = 1
} finally { } finally {
await postgres.stop(); const target = resolve(directory), rel = relative(resolve(resolve(sourceRoot, 'tmp')), target) await postgres.stop(); const target = resolve(directory), rel = relative(resolve(resolve(sourceRoot, 'tmp')), target)
Loaded 100 of 1512 files, more files were not shown because too many files have changed in this diff. Show more