Skip to content
app-architecture logo

App Architecture

app-architecture

Create apps following contract-port architecture with composition roots. Use when creating new apps in apps/, scaffolding CLI tools, setting up dependency injection, or when the user asks about app structure, entrypoints, or platform-agnostic design.

SKILL.md

Full skill instructions

App Architecture

Apps mirror the package architecture - they should be platform-agnostic outside of the entrypoint where ports are assembled with platform-specific globals.

Core Principle

Assemble all ports at the top level (composition root), then feed them down to individual modules.

┌─────────────────────────────────────────────────────────────┐
│                     Entry Point (cli.ts)                    │
│                  Parse args, call composition root          │
└─────────────────────────────────────────────────────────────┘
                           │
                           ▼
┌─────────────────────────────────────────────────────────────┐
│                    Composition Root                         │
│                    (composition.ts)                         │
│         Inject platform globals → Create ports → Return     │
└─────────────────────────────────────────────────────────────┘
                           │
         ┌─────────────────┼─────────────────┐
         ▼                 ▼                 ▼
  ┌─────────────┐   ┌─────────────┐   ┌─────────────┐
  │   Module A  │   │   Module B  │   │   Module C  │
  │ (no globals)│   │ (no globals)│   │ (no globals)│
  └─────────────┘   └─────────────┘   └─────────────┘

File Structure

apps/​{app-name}/
├── src/
│   ├── cli.ts              # Entry point (thin) - only parses args, calls composition
│   ├── composition.ts      # Composition root - assembles all ports
│   ├── types.ts            # App-specific types
│   ├── errors.ts           # Error classes (extend ConveauxError)
│   └── {domain}.ts         # Domain modules - platform-agnostic
├── CLAUDE.md               # Inline instructions for this app
├── README.md               # User documentation
├── package.json
└── tsconfig.json

Composition Root Pattern

The composition root is the ONLY place where platform globals appear:

// composition.ts
import type { Env } from '@scope/​contract-env';
import type { Logger } from '@scope/​contract-logger';
import type { WallClock } from '@scope/​contract-wall-clock';
import type { EphemeralScheduler } from '@scope/​contract-ephemeral-scheduler';

import { createEnv, createShellEnvSource, createStaticEnvSource } from '@scope/​port-env';
import { createEphemeralScheduler } from '@scope/​port-ephemeral-scheduler';
import { createLogger, createJsonFormatter, createPrettyFormatter } from '@scope/​port-logger';
import { createOutChannel } from '@scope/​port-outchannel';
import { createWallClock } from '@scope/​port-wall-clock';

// Define what deps the app needs
export interface RuntimeDeps {
  readonly logger: Logger;
  readonly clock: WallClock;
  readonly env: Env;
  readonly scheduler: EphemeralScheduler;
}

export interface RuntimeOptions {
  readonly json?: boolean;
  readonly verbose?: boolean;
}

// Factory that creates all deps - platform globals only appear HERE
export function createRuntimeDeps(options: RuntimeOptions = {}): RuntimeDeps {
  // Inject platform globals into ports
  const clock = createWallClock({ Date });

  const scheduler = createEphemeralScheduler({
    setTimeout: globalThis.setTimeout,
    clearTimeout: globalThis.clearTimeout,
    setInterval: globalThis.setInterval,
    clearInterval: globalThis.clearInterval,
  });

  const logChannel = createOutChannel(process.stderr);

  const formatter = options.json
    ? createJsonFormatter()
    : createPrettyFormatter({ colors: process.stderr.isTTY ?? false });

  const logger = createLogger({
    Date,
    channel: logChannel,
    clock,
    options: { formatter, minLevel: options.verbose ? 'debug' : 'info' },
  });

  const env = createEnv({
    sources: [
      createShellEnvSource(
        { getEnv: (name) => process.env[name] },
        { name: 'shell', priority: 100 }
      ),
      createStaticEnvSource(
        { DEFAULT_TIMEOUT: '30' },
        { name: 'defaults', priority: 0 }
      ),
    ],
  });

  return { logger, clock, env, scheduler };
}

Entry Point Pattern

The entry point should be thin - just parse args and call composition:

// cli.ts
#!/​usr/​bin/​env node

import type { Logger } from '@scope/​contract-logger';
import { Command } from 'commander';
import { createRuntimeDeps } from './​composition.js';
import { runApp } from './​app.js';
import { createClient } from './​client.js';

const program = new Command();

program
  .name('my-app')
  .action(async (options: CliOptions) => {
    // 1. Create all deps at the top level
    const deps = createRuntimeDeps({
      json: options.json,
      verbose: options.verbose,
    });
    const { logger, clock, env, scheduler } = deps;

    // 2. Create app-specific services, passing deps down
    const client = createClient({ logger, clock });

    // 3. Run the app, passing deps down
    const result = await runApp({ logger, clock, client, scheduler }, config);

    console.log(JSON.stringify(result, null, 2));
    process.exit(EXIT_CODES[result.status]);
  });

program.parse();

Domain Module Pattern

Domain modules receive deps via injection - they never use globals:

// client.ts
import type { Logger } from '@scope/​contract-logger';
import type { WallClock } from '@scope/​contract-wall-clock';

// Define what deps THIS module needs (subset of RuntimeDeps)
export interface ClientDeps {
  readonly logger: Logger;
  readonly clock: WallClock;
}

// Interface for what this module provides
export interface Client {
  fetch(url: string): Promise<Response>;
}

// Factory receives deps, returns implementation
export function createClient(deps: ClientDeps): Client {
  const { logger, clock } = deps;

  return {
    async fetch(url: string) {
      const startTime = clock.nowMs();  // Use injected clock, not Date.now()
      logger.debug('Fetching', { url });

      // ... implementation

      const durationMs = clock.nowMs() - startTime;
      logger.debug('Fetched', { url, durationMs });
      return response;
    }
  };
}

Checklist for Creating Apps

Composition Root

  • Single composition.ts file that creates all runtime deps
  • Platform globals (Date, setTimeout, process.env) only appear here
  • Exports RuntimeDeps interface and createRuntimeDeps factory
  • Options (json, verbose) are separate from deps

Entry Point

  • Thin cli.ts - only arg parsing and wiring
  • Calls createRuntimeDeps() once at the top
  • Passes deps down to all modules
  • Handles errors with proper exit codes

Domain Modules

  • Each module defines its own *Deps interface
  • Deps interface only includes contracts the module needs
  • No direct use of globals (Date.now, console, process.env)
  • Factory pattern: createX(deps): X

Types

  • types.ts for shared app-specific types
  • Config types separate from deps types
  • Exit codes as const object

Errors

  • errors.ts with classes extending UserError or RetryableError
  • Actionable error messages with guidance
  • Use getExitCode() for proper exit codes

Common Deps by Use Case

NeedContractPort
Current timecontract-wall-clockport-wall-clock
Loggingcontract-loggerport-logger
Environment varscontract-envport-env
Delays/​timerscontract-ephemeral-schedulerport-ephemeral-scheduler
Writing outputcontract-outchannelport-outchannel
Error handlingcontract-control-flowport-control-flow

Anti-Patterns

Anti-PatternProblemFix
Date.now() in moduleHidden platform dependencyInject WallClock
setTimeout() in moduleUntestable timingInject EphemeralScheduler
process.env.X in moduleHidden env dependencyInject Env
console.log() in moduleHidden output dependencyInject Logger
Deps created inside moduleCan't test with mocksPass deps from composition root
Module imports port directlyBypasses DIImport contract types only

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View