# Groundcheck Ledger Specification (v1)

Plain, git-diff-friendly YAML files stored under `.groundcheck/` at the workspace root. Shared identically between the coding-agent prompt path (`https://nicia.ai/groundcheck.md`) and the `@nicia-ai/groundcheck` CLI.

## Directory Layout

```text
.groundcheck/
  .gitignore               # ignores cache.db, cache.db-*, *.db, inbox.yaml
  ledger.yaml              # metadata: schema_version, run_count, last_run_at, tool_version
  claims.yaml              # accepted, proposed, superseded, and rejected claims
  findings.yaml            # findings with stable IDs and lifecycle status
  inbox.yaml               # optional local-only queue of SessionEnd hook corrections (gitignored)
  runs/
    2026-12-02T21-30Z.md   # human-readable Markdown report per run
```

Commit `.groundcheck/` to git so the whole team shares accepted decisions and scope resolutions. Keep `.groundcheck/inbox.yaml` local/gitignored until a human promotes an item into `claims.yaml` so unreviewed chat transcripts never leak into git. Always keep `.groundcheck/.gitignore` containing:

```gitignore
cache.db
cache.db-*
*.db
inbox.yaml
```

## Schema Compatibility

`ledger.yaml` carries `schema_version: 1`. Tools MUST refuse to write to a ledger whose `schema_version` is newer than the version they support (stay strictly read-only and warn the user).

## `ledger.yaml`

```yaml
schema_version: 1
run_count: 3
last_run_at: "2026-12-09T18:00:00Z"
tool_version: "0.1.0"
```

## `claims.yaml`

```yaml
- id: clm_a1b2c3d4e5f6
  kind: rule # rule | decision | fact | bet
  text: "Pro annual plans are eligible for a full refund only within 14 calendar days."
  scope: "Pro tier annual subscriptions"
  source:
    path: docs/pricing-policy.md
    line: 32
    commit: "1d75521" # git HEAD at time of capture, if available
    quote: "Pro annual plans are eligible for a full refund only within 14 calendar days of the initial purchase date."
    # For MCP or local session sources instead of path/line:
    # uri: "slack://#pricing/1733160000.1234"
  status: active # active | proposed | superseded | rejected
  supersedes: [clm_0987654321ab] # optional
  origin: extracted # extracted | resolution | promoted_correction
  accepted_by: "Paul Dlug" # set only when a human accepted it (or "unknown")
  accepted_at: "2026-12-02T21:30:00Z"
```

## `findings.yaml`

```yaml
- id: fnd_9f8e7d6c5b4a
  type: contradiction # contradiction | stale_rule | broken_chain | dead_path | died_in_chat
  claims: [clm_a1b2c3d4e5f6, clm_0987654321ab]
  first_seen: "2026-12-02T21:30:00Z"
  last_seen: "2026-12-09T18:00:00Z"
  seen_count: 2
  status: open # open | resolved | deferred | regressed
  resolution: # present once a human acts on the finding
    choice: a_wins # a_wins | b_wins | both_scoped | defer
    decision: clm_112233445566
    note: "Pricing Policy (2026-03-01) replaced the 30-day SOP."
  blast_radius:
    - path: docs/billing-runbook.md
      line: 16
```

## `inbox.yaml` (Optional, Local-Only Hook Queue)

Written by `SessionEnd` hooks (`groundcheck init --hooks`) when a human corrects an agent in chat. Gitignored by default so raw session snippets stay local until promoted via `groundcheck resolve` or Loop mode:

```yaml
- id: inb_4c7d8e9f0a1b
  captured_at: "2026-12-02T21:15:00Z"
  harness: claude-code # claude-code | cursor | codex
  session_uri: "session://claude-code/2026-12-02/abc123"
  user_quote: "No, Pro annual refunds are 14 calendar days, not 30."
  candidate_claim: "Pro annual plans are eligible for a full refund only within 14 calendar days."
  suggested_target: "AGENTS.md"
  status: pending # pending | promoted | dismissed
```

## Stable ID Rules (Offset- and Order-Independent)

Stable IDs keep `new`, `still open`, `resolved`, and `regressed` diffs accurate even when line numbers shift or an LLM paraphrases a statement:

1. **Normalize text (`norm(s)`):** Unicode NFKC → lowercase → strip Markdown emphasis/punctuation (keeping letters, digits, `%`, `$`, `.`, `-`, `/`) → collapse runs of whitespace to a single space → trim.
2. **Claim ID (`clm_<12hex>`):**
   - Canonical key uses **`norm(source.path || source.uri) + "\0" + norm(source.quote || text) + "\0" + norm(scope || "")`** — deliberately omitting `line` and `commit`, and preferring the verbatim `quote` over paraphrased `text`.
   - CLI computes `clm_${sha256(key).slice(0, 12)}`.
   - Near-duplicate match on rerun: if an incoming claim has no exact ID match in `claims.yaml`, check existing claims from the same `source.path` (or `source.uri`) with token Jaccard `>= 0.80` on `norm(quote || text)` and reuse that existing ID.
3. **Finding ID (`fnd_<12hex>`):**
   - Canonical key uses **`type + "\0" + sortedQuoteKeys.join("\0")`** where each claim key is `norm(source.quote || text)` (deliberately omitting LLM-paraphrased `scope`, `line`, and `path`), sorted lexicographically so pair order `(A, B)` vs. `(B, A)` produces the identical ID and reintroducing a resolved rule in another file still triggers `regressed`.
   - CLI computes `fnd_${sha256(key).slice(0, 12)}`.
4. **Prompt-only path (when CLI is unavailable):** Read existing `.groundcheck/claims.yaml` and `.groundcheck/findings.yaml` first and always reuse existing IDs for matching claims/findings. For brand-new entries without shell access, use deterministic readable slugs (`clm_<doc-stem>_<short-slug>`, `fnd_<type>_<short-slug>`), or run a one-line `node -e` / `shasum` SHA-256 helper if shell access is available.

## Lifecycle Transitions

- **Not in `findings.yaml` → detected:** `new` (`status: open`, `seen_count: 1`).
- **In `findings.yaml` (`open` or `deferred`) → detected again:** `still open` (increment `seen_count`, update `last_seen`; `deferred` stays `deferred` and is omitted from `new`).
- **In `findings.yaml` (`open`) → not detected:** `resolved` (or explicitly `resolved` via `a_wins` / `b_wins` / `both_scoped`).
- **In `findings.yaml` (`resolved`, except valid `both_scoped`) → detected again:** **`regressed`** (`status: regressed`, increment `seen_count`, update `last_seen`, ranked first in output).
