---
name: run-ledger
description: >-
  Record every task as a run in .factory/runs/ so the factory can be measured: class, size, model tier, beats completed, evidence, review rounds, human corrections, and token cost. Use when starting any task in a repo that has a .factory/ directory, at the end of each beat, when a human corrects or approves your work, when you finish or get blocked, and when asked how the factory is doing ("factory report", "how many PRs", "what did that cost").
license: MIT
compatibility: >
  Node.js 18+, no dependencies. Works on Linux, macOS, and Windows. Token cost is read from local Claude Code (~/.claude/projects) and Codex (~/.codex/sessions) transcripts when they exist; other harnesses record cost as unknown, never zero.
metadata:
  author: software-factory
  version: "1.2"
allowed-tools: Bash(node:*) Bash(git:*)
title: run-ledger
canonical_url: https://skilld.dev/gh/aditya-deokar/software-factory/run-ledger
last_updated: 2026-10-30T17:45:51.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [scripts/ledger.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/run-ledger/scripts/ledger.mjs), [scripts/lib/factory.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/run-ledger/scripts/lib/factory.mjs), [scripts/lib/schema.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/run-ledger/scripts/lib/schema.mjs), [scripts/lib/transcripts.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/run-ledger/scripts/lib/transcripts.mjs), [scripts/schemas/run.schema.json](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/run-ledger/scripts/schemas/run.schema.json).
>
> If the user asked to install this Skill, run `npx skilld install aditya-deokar/software-factory/run-ledger`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/run-ledger`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/aditya-deokar/software-factory/run-ledger) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/run-ledger`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/run-ledger --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/run-ledger --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Run ledger

A factory you cannot measure is a factory you cannot improve. The ledger is
one JSON file per task under `.factory/runs/`, committed with the PR that did
the work. The outer loop (`skill-feedback-loop`, `model-routing`) reads these
files; nothing else produces the numbers.

Skip this skill only in a repo with no `.factory/` directory. Suggest
`factory-setup` there instead of creating the directory yourself.

## Find the script

`$SKILL_DIR` is the folder this `SKILL.md` lives in. Resolve it once; a bare
`./scripts/` resolves against the user's repo and fails.

```bash
SKILL_DIR="$(dirname "$(find skills .claude/skills .agents/skills ~/.claude/skills ~/.agents/skills ~/.claude/plugins \
  -name SKILL.md -path '*run-ledger*' 2>/dev/null | head -1)")"
LEDGER="$SKILL_DIR/scripts/ledger.mjs"
```

PowerShell:

```powershell
$LEDGER = (Get-ChildItem -Recurse -Filter ledger.mjs -Path skills,.claude\skills,.agents\skills,$HOME\.claude\skills,$HOME\.claude\plugins -ErrorAction SilentlyContinue | Select-Object -First 1).FullName
```

Every command below is `node "$LEDGER" ...`. `current` stands for the open
run on the current branch, so after `start` you rarely need the id.

## When to write

| Moment | Command |
|---|---|
| Task starts, after isolating | `start --class bugfix --size small --title "..." --source github:issue/123 --tier balanced --model <id> --rule <rule>` |
| Triage decided | `beat current triage --set decision=implement` |
| Aim set | `beat current aim --set rung=2 --set rungs=3 --add types=depth --set judge="..." --add follow_ups=github:issue/141` |
| Spec approved | `beat current spec --set path=specs/login.md --set approved=true` |
| Evidence captured | `beat current prove --add evidence=recording --set before_captured=true` |
| PR open, review done | `beat current ship --set pr=45 --set review_rounds=2 --set final_score=5/5` |
| A human corrected you | `touch current --beat build --kind correction --ref <comment URL>` |
| A human approved a gate | `touch current --beat spec --kind approval --ref <URL>` |
| Your part is done | `finish current --outcome shipped` |
| You are blocked | `finish current --outcome blocked` |
| Post-merge check ran | `release-monitoring` sets `monitor`, then `finish <id> --outcome merged` |

`--set` overwrites a field, `--add` appends to a list, and values are coerced
(`2` is a number, `true` a boolean). The script validates every write against
the schema and refuses an invalid record rather than saving a bad one.

`start` takes the tier, model, and rule from `model-routing`. If the repo has
no routing, leave them out.

## Picking class and size

Class: `bugfix`, `feature`, `refactor`, `docs`, `ci-fix`, `migration`,
`perf`, `chore`. Use `triage` for a run that only triaged, `loop` for an
outer-loop run, and `eval` for a routing eval.

Size, by the diff you expect to ship:

- `small`: one concern, a few files, no new interfaces. Most bugfixes.
- `medium`: several files or one new interface. A typical feature.
- `large`: cross-cutting, a migration, or anything that needed a spec.

Pick before you start and do not revise it to match what happened. The gap
between the guess and the result is what routing learns from.

## What counts as a human touch

Record one every time a person had to step in. These are the factory's
defects, and hiding them makes the numbers lie.

| Kind | Example |
|---|---|
| `correction` | "That's still broken." A review comment asking for a change. "Don't touch that file." |
| `approval` | Spec approved, merge approved |
| `rejection` | Spec or PR rejected outright |
| `clarification` | You asked, they answered, and the answer changed the work |
| `manual-fix` | The human edited the code themselves |

Only people count. A comment from a review bot (Greptile, Copilot, CodeRabbit)
is not a human touch; it is already counted in `ship.review_rounds`.
Recording bot findings as touches inflates the number the outer loop acts on.

Link the comment or message in `--ref` whenever there is one. A touch with a
link can be checked; a touch without one is a guess.

## In the PR body

Add one line so reviewers and the outer loop can join the PR to its record:

```markdown
Run: `2026-10-02-fix-login-redirect-3f2a` (.factory/runs/2026-10-02-fix-login-redirect-3f2a.json)
```

Commit the run file on the task branch. It merges with the change it
describes.

## Report

```bash
node "$LEDGER" report --since 14d          # markdown table
node "$LEDGER" report --since all --json   # for scripts
```

The report gives runs by outcome, class, and tier; median review rounds;
human touches per shipped run; corrections by beat; how often evidence and the
before state were captured; median tokens per shipped run; and how often
memory was used. Paste it as evidence when someone asks whether the factory is
working, instead of describing it.

`report` exits 2 when there is no `runs/` directory. That means there is no
data to report on, which is different from a clean report.

## Tokens

`finish` sums token usage from local transcripts for this repo between
`started_at` and now. Claude Code and Codex are supported. Running `finish`
again (for example when a shipped PR later merges) keeps the first count.
Pass `--no-tokens` to skip the scan.

Cost stays `null` when no transcript is found. Never fill it in by hand with
an estimate. A number that looks measured but is not is worse than a blank.

## Do not do these

- **Do not backfill a run after the fact from memory.** Start the record
  when the work starts. A reconstructed record is a guess.
- **Do not leave a run open.** Every run ends `shipped`, `merged`, `closed`,
  `blocked`, or `abandoned`. An open run is invisible to the report.
- **Do not skip touches because they are embarrassing.** The outer loop needs
  them to find which skill failed.
- **Do not edit run files by hand** except to fix a mistake, and then run
  `factory-setup`'s `check-factory.mjs` on the directory.
