canonry

AGENTS.md

Project Overview

canonry is an agent-first open-source AEO operating platform that tracks how AI answer engines cite a domain for tracked queries and acts on the signal through the content engine and integrations. Published exclusively as @canonry/canonry on npm. The CLI and API are the primary interfaces — the web dashboard is supplementary.

It ships a built-in analyst agent, Aero (packages/canonry/src/agent/), and a stdio MCP adapter, canonry-mcp (packages/canonry/src/mcp/).

Where rules live

This file holds the rules for every change. Before you edit, read the AGENTS.md files for the affected packages and apps. See docs/CODEMAP.md for source paths and docs/README.md for documentation.

apps/api/                Cloud Run REST entry point (imports packages/api-routes)
apps/web/                 Vite SPA, bundled into packages/canonry/assets/ (also read PRODUCT.md, DESIGN.md)
packages/canonry/         Publishable npm package: CLI + server + SPA (+ src/mcp/AGENTS.md, src/agent/AGENTS.md)
packages/api-routes/      Shared Fastify route plugins (+ src/doctor/AGENTS.md, src/discovery/AGENTS.md)
packages/contracts/       DTOs, enums, config schema, error codes, shared utilities
packages/config/          Typed environment parsing
packages/db/              Drizzle ORM schema, SQLite client, migrations
packages/intelligence/    Insight and health analysis over snapshots
packages/provider-*/      Answer-engine adapters (gemini, openai, claude, perplexity, local, cdp)
packages/integration-*/   Third-party integrations (Google, GA4, Bing, OpenAI ads, Cloudflare, WordPress, ...)
skills/, plugins/         Canonical agent skills and the portable plugin (rules for both: plugins/AGENTS.md)
docs/                     Architecture, data model, setup guides, testing

Deployment Posture (Critical)

canonry serve (local) and apps/api (Cloud Run) are single-tenant: one trust boundary per instance — one operator’s projects on one machine, or one team’s projects on one Cloud Run service. Never multiplex unrelated tenants behind one instance.

Key types, scope gates, read-only keys, keys.write, and what a multi-tenant migration would cost: packages/api-routes/AGENTS.md → “Deployment posture and key authority”.

Local validation and merging (Critical)

Commands

./canonry-install.sh            # one-command dev setup: install deps, build all packages, install canonry globally
pnpm install
pnpm check                      # fast lint of staged, unstaged, and untracked JS/TS files
pnpm lint:staged                # fast lint of the exact staged JS/TS content
pnpm verify                     # full check; required before outside code PRs
pnpm build:cli                  # CLI/server bundle only; skips the dashboard
pnpm build:web                  # build or reuse dashboard output and copy changed assets
pnpm build                      # complete publishable package
pnpm run typecheck && pnpm run test && pnpm run lint
pnpm typecheck:tsc              # typecheck with TypeScript's own tsc instead of tsrs (docs/testing.md)
pnpm gen                        # regenerate the API client after changing openapi.ts
pnpm plugin:sync                # refresh plugin skill mirrors + portable/client manifest versions
pnpm plugin:check               # fail on plugin spec, skill, or version drift (CI gate)
pnpm guide:sync                 # generate MCP guidance + optional native skills from docs/agent-operations/v1.md
pnpm run dev:web

For the product CLI, canonry <command> --help is authoritative and skills/canonry/references/canonry-cli.md is the full reference. Rules a command must follow live with the code that implements it (“Where rules live”), not in a command list.

Portfolio Feature Parity (Critical)

Here, a simple portfolio means the standard project flow. A custom portfolio means an Advanced Measurement portfolio.

Response sections and feedback

Use these sections only for review handoffs. Keep simple answers and routine status updates short.

When proposing work, requesting feedback, or showcasing changes:

Dependency Boundary

Vocabulary (Critical)

Every (query × provider) snapshot carries two independent signals — a model can do either, both, or neither. Never conflate them in code, copy, or contract field names.

Term Meaning Source field
mention / mentioned The project’s brand or domain appears in the LLM’s answer prose. Citation links written into the text (chips such as ([domain](url)), links labelled with a URL or a lowercase host, [1] markers, bare http(s):// URLs) are citations and never count; match through answerProseForMentions. query_snapshots.answer_mentioned (boolean)
cited The project’s domain appears in the answer’s source links / grounding. An engine that returns no sources (Local) records 'not-measured', which is never a miss. query_snapshots.citation_state = 'cited', read through readCitationState
  1. Say mention / mentioned for answer-text presence. answer, visible, and visibility are legacy terms (visibility_state, run kind answer-visibility, visibilityStateFromAnswerMentioned); new APIs, fields, flags, and UI labels say mentioned (mentionRate, never answerRate).
  2. Say cited for source-list presence; never use citation as an umbrella for both.
  3. Never compute one signal from the other. A “cited” label reads citationState; a “mentioned” label reads answerMentioned. A metric named for one and computed from the other is a bug — fix it.
  4. When you mean both, say “citation + mention coverage” or “visibility (cited or mentioned)” and disambiguate immediately.
  5. Public API field names use this vocabulary from the start; a rename needs a version bump.
  6. Render both signals in CLI/UI snapshot output: a two-glyph cell (C/c cited or not, · citation not measured, M/m mentioned or not, – missing) with the legend above the table, as canonry citations and canonry run do (packages/canonry/src/snapshot-cell.ts).
  7. canonry-vocabulary/no-banned-metric-literal bans conflating literals ('answerRate', 'visibility run', 'paid mentions', …) in the CLI, api-routes, and web trees; the list is in eslint-rules/canonry-vocabulary.js. Bare 'visible' stays legal (DOM, legacy enum) — review owns it.

Branded vs non-brand (Critical)

Branded and non-brand queries never share a denominator, except the API’s compatibility overall sentiment headline (rule 7). A branded query names the project, so the model was handed the answer; pooling lets brand recall outvote the category and can invert a ranking (measured on a real basket: pooled ranked the subject FIRST at 42%, non-brand ranked it LAST at 3%).

  1. Mention Share (card, breakdown chart, trend buckets), visibility-stats --share-of-voice, and visibility-compare default to non-brand. Historical competitor landscapes require explicit queryClass=non-brand (--query-class non-brand) for competitive percentages; omitted class or all returns pooled counts with null shares.
  2. Branded stays visible as a sibling field (branded) with its own labelled section and denominator — never dropped, never pooled.
  3. The class travels with the number: scope / queryClass on the wire; “· non-brand queries” in the delta, chart title, column header, and CLI line. A GROUP of figures sitting directly under a heading that names the class may rely on that heading for the visible label (the dashboard’s AI Visibility headline strip names the class once in its h2), but each figure still carries the class in its own accessible text via an sr-only suffix beside the value. A reader who sees only the number, or hears only the figure, must still be able to tell which instrument produced it.
  4. pooled appears only when the project has no usable brand alias. Never label an unsplit figure non-brand, and never silently classify an unclassifiable basket.
  5. One classifier: compileQueryClassifier (packages/contracts/src/query-class.ts) runs effectiveBrandNames through the shared brand matcher; queryClassSchema IS measurementQueryClassSchema. No hand-rolled regex, no second enum (canonry-guards/no-second-query-classifier). Surfaces show the server’s queryClass (canonry query list).
  6. competitorOverlap is legacy MIXED evidence (answer text, source links, or both). Citation metrics use citedDomains plus grounding-source hosts; mention metrics use answer text with the shared matcher.
  7. Sentiment is a branded figure: a non-brand answer almost always names the brand to recommend it, so its favorable share says little. The portfolio overview shows the server’s sentiment.branded, or no metric when no branded judgment exists. overall (queryClass = 'all', computed server-side from distinct answer-subject assessments, never averaged class rates) stays in the API for compatibility and is not shown on the dashboard or the human CLI overview. The dashboard shows non-brand sentiment only as its unfavorable and mixed answers (exceptions), never a favorable share; the detailed canonry sentiment --query-class non-brand read keeps the class’s full counts. Preserve both class summaries in the API; competitive metrics remain separate.

Query vs question

The tracked thing (canonry query add, the queries / query_snapshots tables, queryText / queryId / queryClass on the wire) is a query.

  1. Human-facing copy says query: UI labels, headings, tooltips, aria-label, placeholders, CLI output, and exported output — with correct agreement (“Assign at least one query”, “3 query assignments”).
  2. The frozen wire names stay: routes /measurement-property-questions and /measurement-question-result, MCP tools canonry_measurement_property_questions and canonry_measurement_question_result, and every SDK symbol, field, prop, and file built on them. The copy/wire mismatch is deliberate.
  3. Discovery’s generative framing (“questions your customers might ask”) is a real-world noun; once a candidate is promoted into the basket it is a query.
  4. canonry-vocabulary/no-question-ui-copy enforces this in apps/web/src. Machine tokens (no whitespace, e.g. property-questions) and className / id / aria-labelledby values are exempt; only two files are excluded, permanently: DiscoverySection.tsx (rule 3’s framing can’t be separated by regex) and mock-data.ts (test fixture).

Enum Constants (Critical)

Never compare domain values as raw string literals: write kind === RunKinds['answer-visibility'], not kind === 'answer-visibility'. The constant objects (RunKinds, RunStatuses, RunTriggers, CitationStates, VisibilityStates, ComputedTransitions) come from packages/contracts/src/run.ts via @canonry/canonry-contracts.

  1. Type parameters with the union type (kind: RunKind, not string).
  2. Use exhaustive switches with no default, or default: { const _exhaustive: never = value }.
  3. Add new variants to the Zod schema in run.ts; the constant object derives from it. Worked example: packages/contracts/AGENTS.md → “Enum constants”.

Surface Priority

Agent-first. The API is the backbone, the CLI is the primary user-facing surface, and the web UI is important but never blocks a release. A new feature needs an API endpoint in packages/api-routes/ and a CLI command in packages/canonry/; UI in apps/web/ is ideal.

UI/CLI parity (Critical)

Everything the dashboard shows must be readable through the API and CLI, with the same data. The UI consumes the API; it is not a privileged surface.

  1. No UI-only calculations. Derived metrics (percentages, trends, diffs, scores, roll-ups) are computed in the API response; components only lay out and present.
  2. No UI-only state. Every panel, section, or page that displays data maps to a CLI command.
  3. Mirror granularity. A UI summary and detail view need a CLI summary and detail command.
  4. Same data, same shape. --format json output matches the API response the UI consumes.
  5. Same capabilities across UI, API, CLI, and MCP for the equivalent authorized credential: scopes, OAuth consent, project boundaries, usage limits, initiating identity, inputs, saved results, and errors. Cover Simple and Advanced paths. Test real calls across authentication/transport boundaries — tool lists alone don’t prove parity. Document deliberate exclusions, and never turn a narrow action grant into general write access.

Calculation Testing (Critical)

Every derived number — percentage, trend, score, rank, bucket, residual, roll-up, dedupe, classification — is tested against the business invariant it claims to represent.

  1. Assert exact expected math, not shape (toBeGreaterThanOrEqual(0), typeof, “renders without crashing”).
  2. Test the invariant: buckets sum to the total; disjoint metrics don’t double-count seeded overlap; a rate asserts numerator, denominator, rounded value, and display value.
  3. Cover zero totals, missing/partial data, duplicates, overlapping categories, rounding boundaries (<1%, 0%, 100%), clamping, and stale/legacy rows.
  4. The canonical calculation lives and is tested in the API/shared layer; UI tests check the UI renders API values without recomputing.
  5. Test the machine-readable contract (CLI JSON, reports, MCP/API) as well as any display string.

Agent & automation design principles

The CLI and API are the agent interface. If an agent can’t do something with canonry <command> --format json or an HTTP call, it’s a bug.

  1. No interactive prompts. Everything works through flags, env vars, or config.yaml; node:readline is lint-banned in command files. Only canonry init may prompt, and all its values are also flags.
  2. JSON everywhere. --format json goes to stdout; errors go to stderr as { "error": { "code": "...", "message": "..." } } with a code from CliError.
  3. Idempotent writes, with canonry apply as the model. Creating POSTs (runs) return a stable identifier and handle conflicts (e.g. runInProgress with the existing run id).
  4. Single-call reads. If a common question needs two calls, add a composite endpoint (/projects/:name/runs/latest, /projects/:name/search?q=).
  5. Exit codes: 0 success, 1 user error (bad input, not found, validation), 2 system error (network, provider failure, internal) — agents use them to decide whether to retry.
  6. Stable output contracts. JSON fields, endpoint paths, and error codes are public API: add freely, never rename or remove without a version bump.
  7. MCP adapter boundary. canonry-mcp may use createApiClient() and public client methods only — no DB, route, job-runner, CLI-dispatch, telemetry, or logger imports, and nothing on stdout but MCP frames.
  8. MCP parity by default. Every new public API endpoint and CLI command gets an MCP tool, or its OpenAPI operation is classified deferred / excluded-protocol with a rationale in packages/canonry/src/mcp/openapi-classification.ts. MCP never adds capabilities the API/CLI lack. Details and the deliberate exceptions: packages/canonry/src/mcp/AGENTS.md.
  9. Classify new request parameters on any operation that skips or reuses work as identity or tuning (packages/api-routes/AGENTS.md → “Request parameters: identity vs tuning”).

Spec-driven typing (Critical)

packages/api-routes/src/openapi.ts is the single source of truth for HTTP shapes; the web client, the CLI’s ApiClient, and the MCP adapter consume types regenerated from it.

  1. Every new route registers a Zod schema and uses jsonResponse(...); new rawJsonResponse(..., looseObjectSchema) routes fail no-new-loose-routes.test.ts. Steps: packages/api-routes/AGENTS.md → “Typed responses”.
  2. Every web call goes through the generated SDK; only api.ts and api-aero.ts may use raw fetch. Details: apps/web/AGENTS.md → “API calls”.

openapi-contract.test.ts requires every registered schema to be referenced by a route.

Shared Utilities (Critical)

Generic, pure helpers (formatX, parseX, normalizeX, clampX, …) live once in packages/contracts/ and are imported everywhere they’re needed.

  1. Check packages/contracts/src/ first; the “Where utilities live” table in packages/contracts/AGENTS.md maps each concern to its file.
  2. Make helpers generic enough for the next caller; domain wrappers stay in the consumer and call the generic core.
  3. No duplicate implementations: replace a second copy with the import.
  4. Pure functions only — no side effects, I/O, DB access, or logging.
  5. Test the helper in packages/contracts/test/<name>.test.ts, not in its callers.
  6. When you find an inline helper that should be shared, migrate it and every caller in the same change.

Show every percentage with formatPercent(value, unit), never (x * 100).toFixed(…): each ratio field declares its wire unit on its schema (fraction() / percent(), checked by ratio-units.test.ts), and canonry-guards/no-inline-percent flags inline percent formatting.

Fit trends and other statistics server-side (linearTrend, wilsonInterval) and put them in the DTO; a regression computed in a chart component is invisible to the CLI. Render a caught unknown with describeError, never err instanceof Error ? err.message : String(err).

Backend rules at a glance

Each rule is detailed in the linked file. The one-liners live here because these have caused real bugs.

Third-party HTTP calls (Critical)

Every integration that calls a third party over HTTP must back off when that service pushes back. Wrap the package’s HTTP layer in withRetry from @canonry/canonry-contracts — one private fetchOnce, one exported wrapper (reference: packages/integration-bing/src/bing-client.ts) — and use isRetryableHttpError rather than a hand-written status check. packages/contracts/test/integration-retry-coverage.test.ts fails CI for a new HTTP-calling integration without it. Throttles that aren’t HTTP 429, Retry-After, base-delay tuning, and testing both directions: packages/contracts/AGENTS.md → “Third-party HTTP calls”.

Authentication Storage

API Surface and Stability

All endpoints live under /api/v1/ with Authorization: Bearer cnry_...; the full contract is GET /api/v1/openapi.json (no auth). Never change an existing endpoint path or HTTP method — the CLI, UI, and external integrations are hard-coded to them. Additive changes (new endpoints, new optional fields) are fine; renaming or restructuring needs a versioned migration plan and explicit user approval. If a route is wrong, fix the logic, not the URL.

Probe runs (Critical)

A probe run (runs.trigger = 'probe', RunTriggers.probe; canonry run --probe or POST /api/v1/projects/:name/runs with "trigger": "probe") writes a snapshot for operator/agent inspection but MUST NOT influence the dashboard, analytics, intelligence, reports, or notifications.

  1. Aggregate reads (dashboard, analytics, report, timeline, intelligence) AND-in notProbeRun() from packages/api-routes/src/helpers.ts, including recent-runs windows. Add a case to packages/api-routes/test/probe-exclusion.test.ts for every new aggregate endpoint.
  2. Per-run detail endpoints that take a runId (GET /runs/:id, screenshots, browser diffs, GSC inspect lookups) include probes.
  3. Operator lists (GET /runs, GET /projects/:name/runs) include probes. The dashboard overview filters them in apps/web/src/queries/use-dashboard-overview.ts.
  4. RunCoordinator (packages/canonry/src/run-coordinator.ts) returns early for probes — no intelligence, webhooks, or Aero wake. New post-run subscribers keep that check.
  5. External callers may send only manual or probe (runTriggerRequestSchema); scheduled, config-apply, and backfill are server-set.

Base Path Awareness (Critical)

Canonry can run behind a reverse proxy sub-path (e.g. /canonry/); code that ignores basePath produces silent 404s in production.

Versioning

Bump the package version only for non-documentation changes of more than 100 changed lines (features, bug fixes, refactors, dependency updates, and the tests that accompany them); documentation-only changes (README, docs/, AGENTS.md) and smaller changes don’t bump. A bump updates the root package.json and packages/canonry/package.json together, in sync with the latest published @canonry/canonry. Use semver: patch for fixes, minor for features, major for breaking changes.

Testing

Use the repository test-audit skill whenever writing, changing, reviewing, running, or auditing tests. Read it before selecting or editing tests. Apply its authoring gate to new or changed tests; for routine runs, check the affected tests’ observable contract and credible failure mode without starting a full-suite audit. Follow its evidence and retention bars before removing coverage. Canonry validation commands and workflow details live in docs/testing.md.

Every non-trivial change ships with tests — features, bug fixes, and refactors. Typo, comment, and config-only changes are exempt. Vitest runs the workspace projects in vitest.config.ts; tests live in each package’s test/ directory.

Code Comments

Lint Guards (Critical)

Several rules in this file are true only because a lint guard enforces them — see docs/GUARDS.md for the full guard table and Adding a guard procedure. Lint runs on oxlint: .oxlintrc.json, with the custom rules as JS plugins in eslint-rules/. Every guard has its own rule id; never add options to core no-restricted-syntax (overrides resolve rules last-wins, so a later block clobbers prior guards with no diagnostic — 4 dead guards found 2026-08-05). Key guards: canonry-guards/no-raw-http-web (apps/web → SDK), canonry-guards/no-raw-http-cli (canonry → ApiClient), canonry-vocabulary/no-banned-metric-literal, design-tokens/no-literal-palette — full list in docs/GUARDS.md.

Keeping Documentation Current

Per-package AGENTS.md must stay in sync — see docs/DOC_UPDATE.md for the full “When you… → Update…” table (route/CLI/MCP/doctor/guard/provider etc.).

Put a new rule in the AGENTS.md closest to the code it governs; add it here only if every change must follow it. Point to the source (path or file:line) instead of copying it, and leave out what an agent can learn from the code, --help, or docs/CODEMAP.md.

AGENTS.md is the only agent-instruction file. Claude Code v2.1.277+ reads it directly, but only where no CLAUDE.md, .claude/CLAUDE.md, or CLAUDE.local.md is on the path, so never add one: it hides the AGENTS.md beside and below it. Claude Code sessions that cannot read AGENTS.md directly (before v2.1.277, on Bedrock / Vertex / Foundry, or with telemetry disabled) get no project instructions from this repo.