Skip to content

Latest commit

 

History

History
101 lines (86 loc) · 4.67 KB

module-contract.md

File metadata and controls

101 lines (86 loc) · 4.67 KB

The module contract (v1)

Everything a module author needs to plug real functionality into typescript-anything. The engine (CLI, pipeline commands) never calls module code directly — modules register capabilities through the typed extension points below, and the pipeline consumes them.

File layout

src/modules/<category>/<module-id>.ts        # one module per file
  • <category> is one of: codecs, record, diff, fuzz, gate, bench, port, runtime, adapters, watch, security, report, ai, mcp, kill, swap, state, observ, interop, profile, pm, fw, ci, docs.
  • <module-id> is kebab-case (codec-typed-arrays), identical to the module's id field and to the file name (without .ts).
  • The file src/modules/index.ts is GENERATED (scripts/gen-module-index.mjs regenerates it). Never edit it by hand.

The module shape (mandatory template)

import type { Module } from '../../types.js';

const m: Module = {
  id: 'codec-typed-arrays',          // kebab-case, unique, == file name
  name: 'Typed Array Codec',
  category: 'codecs',                // must match the parent directory
  description: 'One-sentence, user-facing description of what this does.',
  keywords: ['typedarray', 'serialization'],   // optional
  features: [
    // 3–6 machine-countable features. Every id MUST be `<moduleId>:<kebab>`.
    { id: 'codec-typed-arrays:roundtrip', name: 'Round-trip', description: '…', kind: 'codec' },
    { id: 'codec-typed-arrays:bigi64', name: 'BigInt64 support', description: '…', kind: 'codec' },
  ],
  register(ctx) {
    ctx.registerCodec({
      id: 'codec-typed-arrays',
      name: 'Typed Array Codec',
      detect(value) { return value instanceof Float64Array; },
      encode(value, encodeValue) { return { data: [...(value as Float64Array)] }; },
      decode(data, decodeValue) { return new Float64Array((data as { data: number[] }).data); },
    });
  },
};

export default m;

Hard rules

  1. Relative imports MUST end with .js (NodeNext ESM): import type { Module } from '../../types.js'. Do not import other modules; import shared types from ../../types.js only.
  2. No third-party dependencies. node:* builtins only (node:crypto, node:fs, node:path, node:child_process, …).
  3. Real logic only. No TODO, no placeholders, no throw new Error('not implemented'). Every feature you declare must be backed by code that runs when the module registers (a codec, check, corpus, rule, reporter, transform, runtime, command, or analyzer — or an event handler with side effects).
  4. Feature count: 3–6 per module. Feature ids are globally unique and are machine-verified by ta doctor / ta features count.
  5. register(ctx) must not throw on any reasonable config; wrap risky work in try/catch internally.
  6. Do not modify files outside your module file. No edits to src/types.ts, src/modules/index.ts, commands, or other modules.

Extension points on ModuleContext

Method Registers Consumed by
registerCodec(codec) a lossless value codec (detect/encode/decode) ta record, ta replay, doctor
registerCheck(check) a gate check returning {check,status,severity,summary,details} ta gate, ta ci
registerCorpus(corpus) an adversarial input generator (generate(rng)) ta record, ta fuzz
registerDiffRule(rule) first-match-wins tolerance rule ta diff, ta shadow
registerReporter(section) a markdown report section (order, render(ctx)) ta report
registerPortTransform(t) a JS→TS source transform ta port
registerRuntime(adapter) a process runtime (detect, runFile) runtime matrix commands
registerCommand(cmd) an extra CLI subcommand ta <name>
registerAnalyzer(an) a project analyzer returning findings ta analyze
on(event, handler) subscribe to pipeline events (see EventName) the bus
store per-run scratch storage cross-module state

Codec wire format: the serializer wraps plugin output as { $codec: '<codecId>', v: <encoded> } — you only produce the v part. encode/decode receive an encodeValue/decodeValue callback for nested values.

Self-verification (required for module authors)

cd /home/z/my-project/typescript-anything
node scripts/gen-module-index.mjs     # regenerate the module index
npm run build                         # tsc — must be error-free
node dist/cli.js modules count        # your module must be counted
node dist/cli.js features count       # your features must be counted
node dist/cli.js doctor               # must stay green (no new issues)