prdddDocsOpen viewer ↗

@prddd/contract/js

What a JavaScript runner needs to read and write a prddd workspace without Effect: find the shared .cairn/ directory, decode its files, append events and format commit trailers. The parse functions and appendEvent throw a PrdddContractError when their input does not match the contract; cairnDir throws git's error outside a checkout.

commits

formatTrailers

The Cairn-Task, Cairn-Section and Cairn-Session trailer lines for a commit, one per line. Landing reads these to tell which task and spec sections a commit builds.

Signature

declare const formatTrailers: (trailers: Trailers) => string

Source

trailerArgs

The same trailers as formatTrailers, as --trailer arguments to pass to git commit.

Signature

declare const trailerArgs: (trailers: Trailers) => ReadonlyArray<string>

Source

errors

PrdddContractError

Thrown when an input does not match the contract. path names the refused field, empty when the whole input is refused, and message says why.

Signature

declare const PrdddContractError: typeof PrdddContractError

Source

parsing

migrateManifest

Checks a manifest's version and returns the input unchanged: version 1 is the only version.

Signature

declare const migrateManifest: (input: unknown) => unknown

Source

parseConfig

Decodes .cairn/config.json, filling in the defaults the contract gives. A setting with no default, such as attempts.maxCostUsd, stays absent when the file leaves it out.

Example (Reading the defaults)

import { parseConfig } from "@prddd/contract/js"
import * as assert from "node:assert"

const config = parseConfig({ version: 1 })
assert.strictEqual(config.attempts.maxAttempts, 3)

Signature

declare const parseConfig: (input: unknown) => Config

Source

parseEvent

Decodes one line of events.jsonl: a status event or a usage event. A line without v gets 1, and a status line without actor gets "orchestrator".

Example (Decoding a status line)

import { parseEvent } from "@prddd/contract/js"
import * as assert from "node:assert"

const event = parseEvent('{"taskId":"t12","status":"running","ts":"2026-01-01T00:00:00Z"}')
assert.strictEqual(event.taskId, "t12")
assert.strictEqual(event.v, 1)

Signature

declare const parseEvent: (
  line: string
) =>
  | {
      readonly status: "pending" | "running" | "blocked_on_human" | "failed" | "done" | "landed"
      readonly actor: "orchestrator" | `human:${string}` | `agent:${string}`
      readonly v: 1
      readonly taskId: string & Brand<"TaskId">
      readonly ts: Utc
      readonly model?: string | undefined
      readonly reasoning?: "low" | "medium" | "high" | "xhigh" | "max" | undefined
      readonly session?: (string & Brand<"SessionId">) | undefined
      readonly modelId?: string | undefined
      readonly harness?: { readonly name: string; readonly version: string } | undefined
      readonly route?: "subscription" | "api" | "gateway" | "unified" | undefined
      readonly failure?: "checks" | "agent" | "infra" | "upstream" | undefined
      readonly kind?: "status" | undefined
      readonly usage?:
        | {
            readonly input: number | null
            readonly cachedInput: number | null
            readonly cacheWrite: number | null
            readonly output: number | null
            readonly reasoning: number | null
          }
        | undefined
      readonly cost?: number | null | undefined
      readonly attempt?: "first" | "retry" | "escalation" | "repair" | undefined
      readonly detail?: string | undefined
    }
  | {
      readonly kind: "usage"
      readonly ts: Utc
      readonly taskId: string & Brand<"TaskId">
      readonly v: 1
      readonly session: string & Brand<"SessionId">
      readonly purpose: "review" | "conflict"
      readonly model: string
      readonly reasoning: "low" | "medium" | "high" | "xhigh" | "max"
      readonly harness: { readonly name: string; readonly version: string }
      readonly usage: {
        readonly input: number | null
        readonly cachedInput: number | null
        readonly cacheWrite: number | null
        readonly output: number | null
        readonly reasoning: number | null
      }
      readonly cost: number | null
      readonly route: "subscription" | "api" | "gateway" | "unified"
    }

Source

parseManifest

Decodes .cairn/manifest.json: every task with its branch, parent, dependencies, spec sections and gate.

Example (Refusing a manifest with no tasks)

import { parseManifest, PrdddContractError } from "@prddd/contract/js"
import * as assert from "node:assert"

assert.throws(() => parseManifest({ version: 1, tasks: [] }), PrdddContractError)

Signature

declare const parseManifest: (input: unknown) => Manifest

Source

rendering

formatStatements

Renders the PRD statements a task must satisfy, taken from its trace. order: "review" puts the statements a person has to check first; "document" keeps the PRD's order.

Signature

declare const formatStatements: (
  task: Pick<TraceTask, "statements">,
  trace: Pick<Trace, "prd">,
  { order }: { readonly order: "document" | "review" }
) => string

Source

workspace

appendEvent

Appends one event to events.jsonl as a single O_APPEND write of one line, so concurrent writers never interleave. The event is validated first, as the encoded object a JavaScript caller has to hand (ts a string; v and actor may be left out and are written in); an invalid one throws a PrdddContractError and writes nothing. A file whose last line has no newline gets one first, in the same write (readers skip the blank line two such writers can leave). A short write throws instead of leaving a torn line behind.

Example (Recording that a task started)

import { appendEvent, parseEvent } from "@prddd/contract/js"
import * as assert from "node:assert"
import { mkdtempSync, readFileSync, rmSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"

// A runner writes to join(cairnDir(process.cwd()), "events.jsonl").
const dir = mkdtempSync(join(tmpdir(), "prddd-"))
try {
  const events = join(dir, "events.jsonl")
  appendEvent(events, {
    taskId: "t12",
    status: "running",
    ts: "2026-01-01T00:00:00Z",
    model: "sonnet",
    modelId: "claude-sonnet-5-5"
  })
  assert.strictEqual(parseEvent(readFileSync(events, "utf8").trim()).taskId, "t12")
} finally {
  rmSync(dir, { recursive: true, force: true })
}

Signature

declare const appendEvent: (path: string, event: unknown) => void

Source

cairnDir

The main checkout's .cairn/, found from git's common directory the way the CLI does, so a linked worktree reaches the same directory as everyone else.

Example (Finding the manifest from any worktree)

import { cairnDir } from "@prddd/contract/js"
import * as assert from "node:assert"
import { execFileSync } from "node:child_process"
import { mkdtempSync, rmSync } from "node:fs"
import { tmpdir } from "node:os"
import { basename, dirname, join } from "node:path"

const repo = mkdtempSync(join(tmpdir(), "prddd-"))
try {
  execFileSync("git", ["init", "-q"], { cwd: repo })
  // A runner passes process.cwd(), from the main checkout or any of its worktrees.
  const manifest = join(cairnDir(repo), "manifest.json")
  assert.strictEqual(basename(dirname(manifest)), ".cairn")
} finally {
  rmSync(repo, { recursive: true, force: true })
}

Signature

declare const cairnDir: (cwd: string) => string

Source