{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/multi-agent-ecosystem/multi-agent-pipeline/pipeline/schemas/learnings-ledger.schema.json",
  "version": "1.0.0",
  "title": "Multi-Agent Pipeline  -  persistent learnings ledger row",
  "description": "One durable knowledge entry, append-only, one file per repo at $HOME/.claude/memory/multi-agent/<repo-slug>/learnings-ledger.jsonl. Distinct from triage-corpus (per-task findings): the ledger holds knowledge replayed into EVERY future run of the same repo - architectural facts, conventions, and explicitly rejected review preferences - so agents stop re-discovering structure and reviewers stop re-flagging rejected feedback.",
  "type": "object",
  "additionalProperties": false,
  "required": ["v", "ts", "repo_slug", "kind", "scope", "statement"],
  "properties": {
    "v": { "type": "string", "description": "Row schema version." },
    "ts": {
      "type": "string",
      "format": "date-time",
      "description": "When the entry was recorded."
    },
    "repo_slug": {
      "type": "string",
      "minLength": 1,
      "description": "Repo the knowledge belongs to."
    },
    "kind": {
      "type": "string",
      "enum": ["fact", "convention", "rejected-preference", "decision", "security-note"],
      "description": "fact = architectural truth ('auth tokens go through KeychainStore'); convention = a project rule; rejected-preference = a review suggestion the team explicitly rejected, so reviewers should not re-raise it; decision = a choice made between alternatives, always stored with its rationale; security-note = a security constraint of the repo (what must never be logged, which input is untrusted)."
    },
    "scope": {
      "type": "string",
      "minLength": 1,
      "description": "Path glob the entry applies to (e.g. 'Auth/*'), or 'global'."
    },
    "statement": {
      "type": "string",
      "minLength": 1,
      "description": "The knowledge itself, in one line."
    },
    "source_task": {
      "type": ["string", "null"],
      "description": "Task id that produced this entry, or null when added manually."
    },
    "source": {
      "type": "string",
      "enum": ["triage", "transcript-mining", "manual", "promotion"],
      "description": "v16.27+ - who found this. `triage` is the Phase 4 distill, `transcript-mining` is learn-from-transcripts.mjs correlating a failure with the retry that worked, `manual` is a human or a model writing it directly, `promotion` is a global row written by learnings-ledger.mjs promote --accept from entries that recur across repositories. Kept separate because the three have different error modes and learning-curve.mjs has to be able to say which kind of learning is actually accumulating: a machine-mined path correction and a model's architectural claim are not the same evidence, and a metric that pools them can rise while the useful half is flat. Absent means the row predates the field."
    },
    "confidence": {
      "type": "string",
      "enum": ["low", "medium", "high"],
      "description": "How strongly to weight the entry when replaying it."
    },
    "rationale": {
      "type": "string",
      "minLength": 1,
      "description": "Why a decision was made and not its alternative. Required when kind is decision (learnings-ledger.mjs refuses a decision without one); rendered as '(rationale: ...)'."
    },
    "supersedes": {
      "type": "array",
      "minItems": 1,
      "maxItems": 5,
      "uniqueItems": true,
      "items": { "type": "string", "pattern": "^L:[0-9a-f]{8}$" },
      "description": "Ids of earlier entries in the same ledger that this entry replaces (learnings-ledger.mjs add --supersedes). The older rows stay in the append-only file; brief and profile leave a superseded entry out, timeline and show name the entry that superseded it. Only a link to an earlier line counts. Absent on rows that replace nothing."
    },
    "promoted_from": {
      "type": "object",
      "additionalProperties": false,
      "required": ["candidate", "similarity", "sources"],
      "description": "Only on a row of the global ledger ($HOME/.claude/memory/multi-agent/_global/learnings-ledger.jsonl): the candidate a person approved with learnings-ledger.mjs promote --accept, its lowest pairwise word-pair similarity, and the per-repository entries it was promoted from. brief and profile leave the row out in every repository named in `sources`.",
      "properties": {
        "candidate": { "type": "string", "pattern": "^P:[0-9a-f]{8}$" },
        "similarity": { "type": "number", "minimum": 0, "maximum": 1 },
        "sources": {
          "type": "array",
          "minItems": 2,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["repo", "id"],
            "properties": {
              "repo": { "type": "string", "minLength": 1 },
              "id": { "type": "string", "pattern": "^L:[0-9a-f]{8}$" }
            }
          }
        }
      }
    },
    "retracts": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "reason"],
      "description": "Only on a row of the global ledger: a person took back the global learning `id` with learnings-ledger.mjs promote --retract, for `reason`. The retraction is appended; the learning's own row is not rewritten. brief and profile leave a retracted learning out in every repository, show reports the retraction, and the learning is not proposed for promotion again. A row with this field is a retraction, never a learning of its own.",
      "properties": {
        "id": { "type": "string", "pattern": "^G:[0-9a-f]{8}$" },
        "reason": { "type": "string", "minLength": 1 }
      }
    },
    "diagnosis": {
      "type": "string",
      "description": "Optional causal 'why' behind the lesson (Reflexion): the root-cause reasoning that turns a detected failure into a prevented one. Rendered in the brief as '(why: ...)'. Absent for plain facts/conventions."
    }
  }
}
