AAnvio
Backend

Coding Standards

The real lint, format, and TypeScript rules enforced across the Anvio monorepo.

3 min read

Anvio's coding standards come from three files at the repo root: eslint.config.js, prettier.config.js, and tsconfig.base.json. Every package extends these — there is no per-package override for style.

TypeScript (tsconfig.base.json)

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "lib": ["ES2022"],
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "composite": true,
    "incremental": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    "resolveJsonModule": true,
    "isolatedModules": true
  },
  "exclude": ["node_modules", "dist"]
}

Practical consequences:

  • strict: true — no implicit any, strict null checks, everywhere.
  • module/moduleResolution: NodeNext — relative imports must include the file extension: import { foo } from './bar.js', even though the source file is bar.ts. This trips up newcomers coming from bundler resolution.
  • noUnusedLocals / noUnusedParameters — dead variables fail the build, not just lint. Prefix an intentionally-unused parameter with _ to satisfy both TypeScript and the ESLint rule below.
  • noFallthroughCasesInSwitch — every switch case needs an explicit break, return, or throw. This is why the tool gateway's dispatch switch (packages/tools/src/builtins/index.ts) returns from every branch.
  • composite: true + declaration/declarationMap — packages build as TypeScript project references; pnpm build runs tsc -p tsconfig.json per package via Turborepo, respecting the dependency graph.

ESLint (eslint.config.js)

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
  eslint.configs.recommended,
  ...tseslint.configs.recommended,
  {
    ignores: ['**/dist/**', '**/node_modules/**', '**/.turbo/**'],
  },
  {
    rules: {
      '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
      '@typescript-eslint/no-explicit-any': 'warn',
    },
  },
);
  • Base: eslint:recommended + typescript-eslint:recommended.
  • no-unused-vars is an error, but allows a leading underscore (_req, _unused) — matches the noUnusedParameters TypeScript rule above.
  • no-explicit-any is a warning, not an error — any is discouraged but not banned outright (useful at process/IPC boundaries like the MCP stdio client).
  • dist/, node_modules/, and .turbo/ are excluded everywhere.

Run it with:

pnpm lint          # turbo run lint (all packages, respects build deps)
pnpm --filter @anvio/tools lint   # single package

Prettier (prettier.config.js)

export default {
  semi: true,
  singleQuote: true,
  trailingComma: 'all',
  printWidth: 100,
  tabWidth: 2,
};

Semicolons required, single quotes, trailing commas everywhere valid (all, not es5), 100-character line width, 2-space indent.

pnpm format          # write
pnpm format:check    # CI check, no writes

Testing

Anvio uses Vitest, not Jest. Test files are colocated as *.spec.ts next to the implementation (for example packages/tools/src/builtins/utility-tools.spec.ts), not in a separate __tests__ directory.

pnpm test                 # vitest run --config vitest.config.mts (unit tests)
pnpm test:integration      # tests/integration only, no turbo cache

Module boundaries as a coding standard

The folder structure dependency rule (apps → platform → packages → core) is enforced by convention and package.json dependencies, not by a custom ESLint import-boundary rule today. When adding code, the practical checks are:

  • Does this belong in packages/core? Only if it's a schema, type, or port with zero dependencies on other Anvio packages.
  • Does an apps/* file contain business logic? If yes, move it into a package — apps/ should only wire NestJS modules/controllers to package exports.
  • Are you importing a package's internal file directly (bypassing src/index.ts)? Don't — import the package's public export instead.

There is no automated lint rule that blocks illegal cross-layer imports today. Reviewers enforce the dependency rule manually in code review — see docs/02-architecture.md and ADR 0002.

NestJS usage (apps/ only)

Per ADR 0002, NestJS is scoped to apps/ as the composition root — dependency injection and module wiring, nothing else. Domain logic (agent runtime, tool execution, model routing) stays in packages/ as plain TypeScript classes and functions with no framework dependency, so they can be unit-tested and reused by the CLI without booting Nest.

Edit on GitHub

Last updated on

On this page