@andresmassello/uscha 1.72.0 → 1.73.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -40,7 +40,7 @@ Requires **Python 3.8+** on the machine (the engine is Python stdlib — no pip
40
40
  runtime dependencies). The npm package is a thin router; the canonical installer is
41
41
  `uscha-kit/install-uscha.py`.
42
42
 
43
- **Kit v1.72.0** <!-- uscha:version --> · [uscha.dev](https://uscha.dev) ·
43
+ **Kit v1.73.0** <!-- uscha:version --> · [uscha.dev](https://uscha.dev) ·
44
44
  [changelog](https://github.com/andresmassello/uscha/blob/main/uscha-kit/CHANGELOG.md)
45
45
  (the per-release changelogs live in the repo, not in the npm tarball)
46
46
 
@@ -76,7 +76,7 @@ and see which file, which test, and when.
76
76
  | `/uscha-mirador` | Bird's-eye HTML dashboard: readiness, trail, acceptance, loops |
77
77
  | `/uscha-status` | One-line progress readout, in chat |
78
78
 
79
- **A measurement engine** (`qa_ledger.py`, 42 subcommands, Python stdlib) that ingests
79
+ **A measurement engine** (`qa_ledger.py`, 44 subcommands, Python stdlib) that ingests
80
80
  evidence from **11 language stacks** — maven, gradle, ant, python, node, go, rust, dotnet,
81
81
  cpp, swift, flutter — and computes a readiness score with hard caps and visible provenance.
82
82
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andresmassello/uscha",
3
- "version": "1.72.0",
3
+ "version": "1.73.0",
4
4
  "description": "Spec-driven development for LLM coding agents: 9 skills + a stdlib evidence engine. Facts block, guesses advise; the human approves.",
5
5
  "author": {
6
6
  "name": "Andres Massello",
@@ -4965,6 +4965,339 @@ def cmd_ir_render(args):
4965
4965
  sys.exit(0)
4966
4966
 
4967
4967
 
4968
+ # --------------------------------------------------------------------------- #
4969
+ # compiler contract (Diamond M3: the LLM is a compiler with a validated output
4970
+ # contract. The engine VALIDATES and INGESTS compilations; it NEVER compiles and
4971
+ # never calls a model. Only mechanical violations gate; every statistic is
4972
+ # advisory (INV-ADVISORY-01). ADR-016.)
4973
+ # --------------------------------------------------------------------------- #
4974
+ COMPILE_SCHEMA = "compile/0.1"
4975
+ COMPILE_REQUIRED = ("schema_version", "canonical_ir", "target_stack", "source",
4976
+ "tests", "trace_manifest", "unresolved_intent",
4977
+ "compilation_report")
4978
+ # The seal covers the load-bearing contract, NOT compilation_report: model, versions and
4979
+ # timestamps legitimately vary and never change WHAT was compiled. A hand edit of the
4980
+ # substance (source/tests/manifest/unresolved_intent) after production must trip the seal.
4981
+ COMPILE_SEALED = ("schema_version", "canonical_ir", "target_stack",
4982
+ "implementation_constraints", "source", "tests",
4983
+ "trace_manifest", "unresolved_intent")
4984
+ # Advisory degeneracy dial: a manifest of >=2 units where EVERY unit claims >= this share
4985
+ # of ALL IR nodes is "everything traces to everything" -- no unit discriminates. ADVISORY
4986
+ # ONLY: printed, never gates. The exact value is a reporting choice, not a contract,
4987
+ # precisely because it can never block (a threshold on a smell would be a judgment).
4988
+ _COMPILE_DEGENERATE_FANOUT = 0.8
4989
+
4990
+
4991
+ def _compile_seal(c):
4992
+ return _integrity_hash({k: c.get(k) for k in COMPILE_SEALED})
4993
+
4994
+
4995
+ def _sha256_file(path):
4996
+ try:
4997
+ with open(path, "rb") as fh:
4998
+ return hashlib.sha256(fh.read()).hexdigest()
4999
+ except OSError:
5000
+ return None
5001
+
5002
+
5003
+ def _contained_unit(base, unit):
5004
+ """A compilation's units must be RELATIVE paths CONTAINED within the compilation
5005
+ directory: the manifest references what was compiled, and what was compiled lives with
5006
+ the compilation. An absolute path or a `..` escape lets a manifest name any file on the
5007
+ filesystem as a "source" unit and pass on that file's hash -- exactly the "manifest
5008
+ cannot lie about what was compiled" guarantee, defeated. Returns the resolved path, or
5009
+ None if the unit is absolute or escapes `base`. Both sides are `realpath`-normalized
5010
+ before comparison -- the Windows 8.3 short-path trap this repo already paid for once
5011
+ (a file INSIDE a tree judged outside it) applies to any containment check."""
5012
+ if os.path.isabs(unit):
5013
+ return None
5014
+ full = os.path.realpath(os.path.join(base, unit.replace("/", os.sep)))
5015
+ root = os.path.realpath(base)
5016
+ if full == root or full.startswith(root + os.sep):
5017
+ return full
5018
+ return None
5019
+
5020
+
5021
+ def _load_ir_at(path):
5022
+ """Strict IR loader for an ARBITRARY IR.json. The repo's own ir/ is gitignored (a
5023
+ regenerable index), so the M3 reference IR is a committed fixture loaded here with the
5024
+ same posture as _load_ir: an unknown schema or a broken/hand-edited seal is refused,
5025
+ never mis-read. Returns (graph, errors); graph None when the file is absent."""
5026
+ if not os.path.isfile(path):
5027
+ return None, ["no reference IR at %s" % path]
5028
+ try:
5029
+ with open(path, encoding="utf-8-sig") as fh:
5030
+ g = json.load(fh)
5031
+ except (OSError, ValueError) as exc:
5032
+ return {}, ["unreadable: %s" % exc]
5033
+ if g.get("schema_version") != IR_SCHEMA:
5034
+ return g, ["schema_version %r != %r (an IR version this engine does not know is "
5035
+ "refused, not read)" % (g.get("schema_version"), IR_SCHEMA)]
5036
+ if not isinstance(g.get("nodes"), list) or not isinstance(g.get("edges"), list):
5037
+ return g, ["nodes/edges missing or not lists"]
5038
+ if g.get("_integrity") != _ir_seal(g):
5039
+ return g, ["reference IR integrity seal does not match -- it was hand-edited "
5040
+ "(regenerate via ir-extract)"]
5041
+ return g, []
5042
+
5043
+
5044
+ def _validate_compilation(comp_path, ir_graph):
5045
+ """The deterministic checker (ADR-016). Returns (blocking_errors, advisory).
5046
+
5047
+ BLOCKS only on FACTS: unknown schema, a missing/mistyped section, a broken seal, an
5048
+ ir_hash that does not match the reference IR, a trace_manifest id that is not an IR
5049
+ node, a unit whose file is absent or whose hash does not match the bytes on disk, a
5050
+ malformed unresolved_intent entry. Every STATISTIC (fan-out degeneracy, empty/generic
5051
+ unresolved_intent, coverage) is advisory and can NEVER change the outcome -- a
5052
+ threshold on a smell is a judgment, and no judgment gates (INV-ADVISORY-01)."""
5053
+ errors, advisory = [], {}
5054
+ base = os.path.dirname(os.path.abspath(comp_path))
5055
+ try:
5056
+ with open(comp_path, encoding="utf-8-sig") as fh:
5057
+ c = json.load(fh)
5058
+ except (OSError, ValueError) as exc:
5059
+ return ["unreadable compilation: %s" % exc], advisory
5060
+ if c.get("schema_version") != COMPILE_SCHEMA:
5061
+ return ["schema_version %r != %r (a contract version this engine does not know is "
5062
+ "refused, not read)" % (c.get("schema_version"), COMPILE_SCHEMA)], advisory
5063
+ for k in COMPILE_REQUIRED:
5064
+ if k not in c:
5065
+ errors.append("required section missing: %s" % k)
5066
+ if errors:
5067
+ return errors, advisory
5068
+ for k, typ in (("source", list), ("tests", list), ("trace_manifest", list),
5069
+ ("unresolved_intent", list), ("canonical_ir", dict),
5070
+ ("compilation_report", dict)):
5071
+ if not isinstance(c.get(k), typ):
5072
+ errors.append("%s must be a %s" % (k, typ.__name__))
5073
+ if errors:
5074
+ return errors, advisory
5075
+ # ELEMENT shapes, checked before any `.get` on them: an adversarial/buggy compiler that
5076
+ # emits a list of strings (or a non-list `implements`) is a mechanical violation -> exit
5077
+ # 2 with a named fault, NEVER an AttributeError traceback (exit 1). This is the trust
5078
+ # boundary the contract exists to defend; the loops below assume dict elements.
5079
+ for k in ("source", "tests"):
5080
+ for i, u in enumerate(c.get(k)):
5081
+ if not isinstance(u, dict):
5082
+ errors.append("%s[%d] must be an object with unit/sha256" % (k, i))
5083
+ for i, e in enumerate(c.get("trace_manifest")):
5084
+ if not isinstance(e, dict):
5085
+ errors.append("trace_manifest[%d] must be an object with unit/implements" % i)
5086
+ elif not isinstance(e.get("implements"), list):
5087
+ errors.append("trace_manifest[%d].implements must be a list of IR node ids" % i)
5088
+ for i, ui in enumerate(c.get("unresolved_intent")):
5089
+ if not isinstance(ui, dict):
5090
+ errors.append("unresolved_intent[%d] must be an object with ir_region/decision"
5091
+ % i)
5092
+ if errors:
5093
+ return errors, advisory
5094
+ if c.get("_integrity") != _compile_seal(c):
5095
+ errors.append("compilation integrity seal does not match -- source/tests/manifest/"
5096
+ "unresolved_intent was hand-edited after production")
5097
+ # the compilation NAMES which IR it compiled; a stale or foreign ir_hash is refused --
5098
+ # a compilation cannot be measured against a graph this repo does not reproduce.
5099
+ ir_seal = ir_graph.get("_integrity")
5100
+ named = (c.get("canonical_ir") or {}).get("ir_hash")
5101
+ if named != ir_seal:
5102
+ errors.append("canonical_ir.ir_hash %s.. does not match the reference IR seal %s.. "
5103
+ "-- a stale or foreign IR is refused, never assumed"
5104
+ % (str(named)[:12], str(ir_seal)[:12]))
5105
+ node_ids = {nd["id"] for nd in ir_graph.get("nodes") or []}
5106
+ # units resolve on disk and their hashes match the bytes: the manifest cannot lie about
5107
+ # what was compiled.
5108
+ units = {}
5109
+ for section in ("source", "tests"):
5110
+ for u in c.get(section) or []:
5111
+ unit, want = u.get("unit"), u.get("sha256")
5112
+ if not unit:
5113
+ errors.append("%s entry without a unit path" % section)
5114
+ continue
5115
+ # source classification is sticky: a unit listed in BOTH source and tests stays
5116
+ # source, so the degeneracy detector (over source units) cannot be dodged by also
5117
+ # listing a source file under tests. Advisory-only, but the detector stays honest.
5118
+ if units.get(unit) != "source":
5119
+ units[unit] = section
5120
+ full = _contained_unit(base, unit)
5121
+ if full is None:
5122
+ errors.append("%s unit escapes the compilation directory -- a relative, "
5123
+ "contained path is required (the manifest references what was "
5124
+ "compiled): %s" % (section, unit))
5125
+ continue
5126
+ got = _sha256_file(full)
5127
+ if got is None:
5128
+ errors.append("%s unit missing on disk: %s" % (section, unit))
5129
+ elif got != want:
5130
+ errors.append("%s unit hash mismatch (manifest lies about the bytes): %s"
5131
+ % (section, unit))
5132
+ # every manifest id is an IR node (THE named mechanical violation); every manifest unit
5133
+ # is a real source/test unit.
5134
+ for entry in c.get("trace_manifest") or []:
5135
+ unit = entry.get("unit")
5136
+ if unit not in units:
5137
+ errors.append("trace_manifest unit is not a declared source/test unit: %s" % unit)
5138
+ for nid in entry.get("implements") or []:
5139
+ if nid not in node_ids:
5140
+ errors.append("trace_manifest implements an id that is not an IR node: %s"
5141
+ % nid)
5142
+ # unresolved_intent SHAPE blocks; its richness (count, specificity) is advisory only.
5143
+ for ui in c.get("unresolved_intent") or []:
5144
+ if not ui.get("ir_region") or not ui.get("decision"):
5145
+ errors.append("unresolved_intent entry missing ir_region or decision")
5146
+ # ---- ADVISORY (computed always, gates never) ----
5147
+ manifest_units = {e.get("unit") for e in c.get("trace_manifest") or []}
5148
+ per_unit = [len(e.get("implements") or []) for e in c.get("trace_manifest") or []]
5149
+ total_nodes = len(node_ids) or 1
5150
+ mean_fanout = (sum(per_unit) / len(per_unit)) if per_unit else 0.0
5151
+ covered = {nid for e in c.get("trace_manifest") or []
5152
+ for nid in (e.get("implements") or []) if nid in node_ids}
5153
+ # Degeneracy is a property of the SOURCE units: >=2 source units that EACH claim >= the
5154
+ # threshold share of all nodes -> the manifest cannot tell you which code implements
5155
+ # which intent (everything traces to everything). Min-based, not mean-based, and over
5156
+ # source only: one comprehensive source file legitimately implements a whole tiny
5157
+ # package, and tests naturally exercise everything, so neither is a degeneracy signal.
5158
+ source_units = {u for u, s in units.items() if s == "source"}
5159
+ src_fanout = [len(e.get("implements") or []) for e in c.get("trace_manifest") or []
5160
+ if e.get("unit") in source_units]
5161
+ advisory = {
5162
+ "trace_units": len(manifest_units),
5163
+ "mean_nodes_per_unit": round(mean_fanout, 3),
5164
+ "node_coverage": round(len(covered) / total_nodes, 3),
5165
+ "unexplained_units": sorted(u for u in units if u not in manifest_units),
5166
+ "unresolved_intent_count": len(c.get("unresolved_intent") or []),
5167
+ "degenerate": len(src_fanout) >= 2 and all(
5168
+ (pu / total_nodes) >= _COMPILE_DEGENERATE_FANOUT for pu in src_fanout),
5169
+ "empty_unresolved": len(c.get("unresolved_intent") or []) == 0,
5170
+ }
5171
+ return errors, advisory
5172
+
5173
+
5174
+ def _print_compile_advisory(advisory):
5175
+ if not advisory:
5176
+ return
5177
+ print(" advisory (never gates): coverage %.2f, mean %.2f nodes/unit, %d unresolved_intent%s"
5178
+ % (advisory.get("node_coverage", 0.0), advisory.get("mean_nodes_per_unit", 0.0),
5179
+ advisory.get("unresolved_intent_count", 0),
5180
+ ", DEGENERATE manifest" if advisory.get("degenerate") else ""))
5181
+ if advisory.get("empty_unresolved"):
5182
+ print(" advisory: unresolved_intent is EMPTY -- suspicious (a compiler that made no "
5183
+ "choices is rare); reported, never blocked")
5184
+ for u in advisory.get("unexplained_units") or []:
5185
+ print(" advisory: %s has no trace_manifest entry -- unexplained by construction" % u)
5186
+
5187
+
5188
+ def cmd_compile_validate(args):
5189
+ ir_graph, ir_errors = _load_ir_at(args.ir)
5190
+ if ir_graph is None or ir_errors:
5191
+ for e in ir_errors:
5192
+ print("[qa_ledger] compile-validate: reference IR: %s" % e, file=sys.stderr)
5193
+ sys.exit(2)
5194
+ errors, advisory = _validate_compilation(args.compilation, ir_graph)
5195
+ if args.json:
5196
+ print(json.dumps({"compilation": args.compilation, "ir": args.ir,
5197
+ "valid": not errors, "errors": errors, "advisory": advisory},
5198
+ indent=2, ensure_ascii=False))
5199
+ elif errors:
5200
+ # diagnostics to stderr (Unix convention, and consistent with compile-ingest); the
5201
+ # machine signal is the exit code. stdout stays clean for the VALID payload path.
5202
+ print("COMPILE-VALIDATE %s: REFUSED (%d mechanical violation(s))"
5203
+ % (args.compilation, len(errors)), file=sys.stderr)
5204
+ for e in errors:
5205
+ print(" x %s" % e, file=sys.stderr)
5206
+ else:
5207
+ print("COMPILE-VALIDATE %s: VALID (contract conformant)" % args.compilation)
5208
+ _print_compile_advisory(advisory)
5209
+ sys.exit(2 if errors else 0)
5210
+
5211
+
5212
+ def cmd_compile_ingest(args):
5213
+ """Record a VALIDATED compilation into the ledger (ADR-016). Ingesting an invalid
5214
+ compilation is a refusal. unresolved_intent becomes append-only, content-addressed
5215
+ UINT objects + an ISSUES-DEFERRED.md mirror (the house convention `fix` uses); the
5216
+ by-construction unexplained_code is the set of units with no trace_manifest entry."""
5217
+ ledger = _load(args.ledger)
5218
+ _repo_node(ledger, args.repo)
5219
+ repo_path = _scope_path(ledger, args.repo)
5220
+ ir_graph, ir_errors = _load_ir_at(args.ir)
5221
+ if ir_graph is None or ir_errors:
5222
+ for e in ir_errors:
5223
+ print("[qa_ledger] compile-ingest: reference IR: %s" % e, file=sys.stderr)
5224
+ sys.exit(2)
5225
+ errors, advisory = _validate_compilation(args.compilation, ir_graph)
5226
+ if errors:
5227
+ print("[qa_ledger] compile-ingest: REFUSED -- ingesting an invalid compilation is a "
5228
+ "refusal (%d mechanical violation(s)); run compile-validate." % len(errors),
5229
+ file=sys.stderr)
5230
+ for e in errors:
5231
+ print(" x %s" % e, file=sys.stderr)
5232
+ sys.exit(2)
5233
+ with open(args.compilation, encoding="utf-8-sig") as fh:
5234
+ c = json.load(fh)
5235
+ comp_seal = c.get("_integrity")
5236
+ uints, seen_uint = [], set()
5237
+ for ui in c.get("unresolved_intent") or []:
5238
+ uid = "UINT-" + hashlib.sha256(
5239
+ (ui.get("ir_region", "") + "\n" + ui.get("decision", "")).encode("utf-8")
5240
+ ).hexdigest()[:12]
5241
+ # content-addressed: two entries that resolve to the same id ARE the same intent gap
5242
+ # (same ir_region + decision), deduped within this ingest just as across ingests --
5243
+ # otherwise ISSUES-DEFERRED and the ledger record carry the id twice.
5244
+ if uid in seen_uint:
5245
+ continue
5246
+ seen_uint.add(uid)
5247
+ uints.append({"id": uid, "ir_region": ui.get("ir_region"),
5248
+ "decision": ui.get("decision"), "rationale": ui.get("rationale", "")})
5249
+ comp_rec = {"id": comp_seal[:12], "repo": args.repo,
5250
+ "ir_hash": (c.get("canonical_ir") or {}).get("ir_hash"),
5251
+ "model": (c.get("compilation_report") or {}).get("model"),
5252
+ "target_stack": c.get("target_stack"), "seal": comp_seal,
5253
+ "unexplained_units": advisory.get("unexplained_units") or [],
5254
+ "node_coverage": advisory.get("node_coverage"),
5255
+ "unresolved_intent": uints, "at": _now()}
5256
+ comps = ledger.setdefault("compilations", [])
5257
+ # re-ingest is idempotent PER REPO: the seal is the identity within a repo, and a
5258
+ # byte-identical compilation has nothing to supersede. The compilations list is flat and
5259
+ # cross-repo, so the supersede check MUST be scoped by repo -- otherwise two repos that
5260
+ # legitimately produce the same compilation (a shared/small canonical package -- exactly
5261
+ # this milestone's own fixtures) collide, and the second repo's first ingest is dropped
5262
+ # as a false "superseded". A CHANGED compilation reseals -> a new record. Never a dup.
5263
+ superseded = any(x.get("seal") == comp_seal and x.get("repo") == args.repo for x in comps)
5264
+ if not superseded:
5265
+ comps.append(comp_rec)
5266
+ _save(args.ledger, ledger)
5267
+ new_items = []
5268
+ if uints:
5269
+ dpath = os.path.join(repo_path, ISSUES_DEFERRED_FILE)
5270
+ existing = ""
5271
+ if os.path.isfile(dpath):
5272
+ with open(dpath, encoding="utf-8-sig", errors="replace") as fh:
5273
+ existing = fh.read()
5274
+ add = [u for u in uints if u["id"] not in existing]
5275
+ if add:
5276
+ with open(dpath, "a", encoding="utf-8", newline="\n") as fh:
5277
+ if existing and not existing.endswith("\n"):
5278
+ fh.write("\n")
5279
+ for u in add:
5280
+ fh.write("- [ ] %s (unresolved_intent): %s -- the compiler decided "
5281
+ "'%s' on its own; a candidate canonical improvement (ADR-016)\n"
5282
+ % (u["id"], u["ir_region"], u["decision"]))
5283
+ new_items = [u["id"] for u in add]
5284
+ out = {"repo": args.repo, "compilation": comp_rec["id"], "superseded": superseded,
5285
+ "unresolved_intent": [u["id"] for u in uints],
5286
+ "issues_deferred_new": new_items,
5287
+ "unexplained_units": comp_rec["unexplained_units"]}
5288
+ if args.json:
5289
+ print(json.dumps(out, indent=2, ensure_ascii=False))
5290
+ else:
5291
+ print("COMPILE-INGEST %s: compilation %s%s"
5292
+ % (args.repo, comp_rec["id"],
5293
+ " (byte-identical to a prior ingest -- nothing superseded)"
5294
+ if superseded else ""))
5295
+ print(" %d unresolved_intent -> %d new %s item(s); %d unexplained unit(s)"
5296
+ % (len(uints), len(new_items), ISSUES_DEFERRED_FILE,
5297
+ len(comp_rec["unexplained_units"])))
5298
+ sys.exit(0)
5299
+
5300
+
4968
5301
  # --------------------------------------------------------------------------- #
4969
5302
  # facts (T0 / SYSTEM-FACTS: public claims become compiled artifacts of repo
4970
5303
  # facts -- Diamond applied to Diamond. ADR-012.)
@@ -9035,6 +9368,28 @@ def build_parser():
9035
9368
  pir.add_argument("--repo", required=True)
9036
9369
  pir.set_defaults(func=cmd_ir_render)
9037
9370
 
9371
+ pcmv = sub.add_parser(
9372
+ "compile-validate",
9373
+ help="validate a COMPILATION.json against a reference IR (ADR-016); only mechanical "
9374
+ "violations gate, degeneracy stats are advisory and NEVER block")
9375
+ pcmv.add_argument("--ir", required=True,
9376
+ help="the reference IR.json the compilation targets")
9377
+ pcmv.add_argument("--compilation", required=True, help="path to COMPILATION.json")
9378
+ pcmv.add_argument("--json", action="store_true")
9379
+ pcmv.set_defaults(func=cmd_compile_validate)
9380
+
9381
+ pcmi = sub.add_parser(
9382
+ "compile-ingest",
9383
+ help="record a VALIDATED compilation into the ledger (ADR-016): by-construction "
9384
+ "unexplained_code + unresolved_intent as append-only UINT objects + backlog")
9385
+ pcmi.add_argument("--ledger", default="QA-LEDGER.json")
9386
+ pcmi.add_argument("--repo", required=True)
9387
+ pcmi.add_argument("--ir", required=True,
9388
+ help="the reference IR.json the compilation targets")
9389
+ pcmi.add_argument("--compilation", required=True, help="path to COMPILATION.json")
9390
+ pcmi.add_argument("--json", action="store_true")
9391
+ pcmi.set_defaults(func=cmd_compile_ingest)
9392
+
9038
9393
  pcr = sub.add_parser("cleanroom",
9039
9394
  help="run a command against a CLEAN checkout of one commit in a throwaway worktree (ADR-008)")
9040
9395
  pcr.add_argument("--ledger", default="QA-LEDGER.json")
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/claude-code-plugin-manifest.json",
3
3
  "name": "uscha",
4
- "version": "1.72.0",
4
+ "version": "1.73.0",
5
5
  "displayName": "Uscha",
6
- "description": "Spec-driven development for LLM coding agents: 9 skills (discovery, adr-refine, reverse-discovery, characterize, devloop, sysdoc, rubric, mirador, status) + a stdlib measurement engine (qa_ledger.py, 42 subcommands + universal installer + npm/npx router). Facts block, guesses advise; the human approves.",
6
+ "description": "Spec-driven development for LLM coding agents: 9 skills (discovery, adr-refine, reverse-discovery, characterize, devloop, sysdoc, rubric, mirador, status) + a stdlib measurement engine (qa_ledger.py, 44 subcommands + universal installer + npm/npx router). Facts block, guesses advise; the human approves.",
7
7
  "author": {
8
8
  "name": "Andres Massello",
9
9
  "url": "https://github.com/andresmassello"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uscha",
3
- "version": "1.72.0",
3
+ "version": "1.73.0",
4
4
  "description": "Uscha spec-driven development methodology for coding agents. Includes npm/npx router.",
5
5
  "author": {
6
6
  "name": "Andres Massello",
@@ -1,6 +1,6 @@
1
1
  # uscha-kit
2
2
 
3
- **Kit version:** v1.72.0 <!-- uscha:version --> · **[uscha.dev](https://uscha.dev)**
3
+ **Kit version:** v1.73.0 <!-- uscha:version --> · **[uscha.dev](https://uscha.dev)**
4
4
 
5
5
  Spec-driven orchestrator + multi-repo QA for Claude Code, with a deterministic ledger.
6
6
  **Nine skills** (`uscha-discovery`, `uscha-adr-refine`, `uscha-devloop`, `uscha-sysdoc`, `uscha-reverse-discovery`,
package/uscha-kit/VERSION CHANGED
@@ -1 +1 @@
1
- uscha-kit 1.72.0
1
+ uscha-kit 1.73.0
@@ -0,0 +1 @@
1
+ {"AC-CC-01": true, "AC-CC-02": true, "AC-CC-03": true, "AC-CC-04": true, "AC-CC-05": true, "AC-CC-06": true, "AC-CC-07": true}
@@ -4965,6 +4965,339 @@ def cmd_ir_render(args):
4965
4965
  sys.exit(0)
4966
4966
 
4967
4967
 
4968
+ # --------------------------------------------------------------------------- #
4969
+ # compiler contract (Diamond M3: the LLM is a compiler with a validated output
4970
+ # contract. The engine VALIDATES and INGESTS compilations; it NEVER compiles and
4971
+ # never calls a model. Only mechanical violations gate; every statistic is
4972
+ # advisory (INV-ADVISORY-01). ADR-016.)
4973
+ # --------------------------------------------------------------------------- #
4974
+ COMPILE_SCHEMA = "compile/0.1"
4975
+ COMPILE_REQUIRED = ("schema_version", "canonical_ir", "target_stack", "source",
4976
+ "tests", "trace_manifest", "unresolved_intent",
4977
+ "compilation_report")
4978
+ # The seal covers the load-bearing contract, NOT compilation_report: model, versions and
4979
+ # timestamps legitimately vary and never change WHAT was compiled. A hand edit of the
4980
+ # substance (source/tests/manifest/unresolved_intent) after production must trip the seal.
4981
+ COMPILE_SEALED = ("schema_version", "canonical_ir", "target_stack",
4982
+ "implementation_constraints", "source", "tests",
4983
+ "trace_manifest", "unresolved_intent")
4984
+ # Advisory degeneracy dial: a manifest of >=2 units where EVERY unit claims >= this share
4985
+ # of ALL IR nodes is "everything traces to everything" -- no unit discriminates. ADVISORY
4986
+ # ONLY: printed, never gates. The exact value is a reporting choice, not a contract,
4987
+ # precisely because it can never block (a threshold on a smell would be a judgment).
4988
+ _COMPILE_DEGENERATE_FANOUT = 0.8
4989
+
4990
+
4991
+ def _compile_seal(c):
4992
+ return _integrity_hash({k: c.get(k) for k in COMPILE_SEALED})
4993
+
4994
+
4995
+ def _sha256_file(path):
4996
+ try:
4997
+ with open(path, "rb") as fh:
4998
+ return hashlib.sha256(fh.read()).hexdigest()
4999
+ except OSError:
5000
+ return None
5001
+
5002
+
5003
+ def _contained_unit(base, unit):
5004
+ """A compilation's units must be RELATIVE paths CONTAINED within the compilation
5005
+ directory: the manifest references what was compiled, and what was compiled lives with
5006
+ the compilation. An absolute path or a `..` escape lets a manifest name any file on the
5007
+ filesystem as a "source" unit and pass on that file's hash -- exactly the "manifest
5008
+ cannot lie about what was compiled" guarantee, defeated. Returns the resolved path, or
5009
+ None if the unit is absolute or escapes `base`. Both sides are `realpath`-normalized
5010
+ before comparison -- the Windows 8.3 short-path trap this repo already paid for once
5011
+ (a file INSIDE a tree judged outside it) applies to any containment check."""
5012
+ if os.path.isabs(unit):
5013
+ return None
5014
+ full = os.path.realpath(os.path.join(base, unit.replace("/", os.sep)))
5015
+ root = os.path.realpath(base)
5016
+ if full == root or full.startswith(root + os.sep):
5017
+ return full
5018
+ return None
5019
+
5020
+
5021
+ def _load_ir_at(path):
5022
+ """Strict IR loader for an ARBITRARY IR.json. The repo's own ir/ is gitignored (a
5023
+ regenerable index), so the M3 reference IR is a committed fixture loaded here with the
5024
+ same posture as _load_ir: an unknown schema or a broken/hand-edited seal is refused,
5025
+ never mis-read. Returns (graph, errors); graph None when the file is absent."""
5026
+ if not os.path.isfile(path):
5027
+ return None, ["no reference IR at %s" % path]
5028
+ try:
5029
+ with open(path, encoding="utf-8-sig") as fh:
5030
+ g = json.load(fh)
5031
+ except (OSError, ValueError) as exc:
5032
+ return {}, ["unreadable: %s" % exc]
5033
+ if g.get("schema_version") != IR_SCHEMA:
5034
+ return g, ["schema_version %r != %r (an IR version this engine does not know is "
5035
+ "refused, not read)" % (g.get("schema_version"), IR_SCHEMA)]
5036
+ if not isinstance(g.get("nodes"), list) or not isinstance(g.get("edges"), list):
5037
+ return g, ["nodes/edges missing or not lists"]
5038
+ if g.get("_integrity") != _ir_seal(g):
5039
+ return g, ["reference IR integrity seal does not match -- it was hand-edited "
5040
+ "(regenerate via ir-extract)"]
5041
+ return g, []
5042
+
5043
+
5044
+ def _validate_compilation(comp_path, ir_graph):
5045
+ """The deterministic checker (ADR-016). Returns (blocking_errors, advisory).
5046
+
5047
+ BLOCKS only on FACTS: unknown schema, a missing/mistyped section, a broken seal, an
5048
+ ir_hash that does not match the reference IR, a trace_manifest id that is not an IR
5049
+ node, a unit whose file is absent or whose hash does not match the bytes on disk, a
5050
+ malformed unresolved_intent entry. Every STATISTIC (fan-out degeneracy, empty/generic
5051
+ unresolved_intent, coverage) is advisory and can NEVER change the outcome -- a
5052
+ threshold on a smell is a judgment, and no judgment gates (INV-ADVISORY-01)."""
5053
+ errors, advisory = [], {}
5054
+ base = os.path.dirname(os.path.abspath(comp_path))
5055
+ try:
5056
+ with open(comp_path, encoding="utf-8-sig") as fh:
5057
+ c = json.load(fh)
5058
+ except (OSError, ValueError) as exc:
5059
+ return ["unreadable compilation: %s" % exc], advisory
5060
+ if c.get("schema_version") != COMPILE_SCHEMA:
5061
+ return ["schema_version %r != %r (a contract version this engine does not know is "
5062
+ "refused, not read)" % (c.get("schema_version"), COMPILE_SCHEMA)], advisory
5063
+ for k in COMPILE_REQUIRED:
5064
+ if k not in c:
5065
+ errors.append("required section missing: %s" % k)
5066
+ if errors:
5067
+ return errors, advisory
5068
+ for k, typ in (("source", list), ("tests", list), ("trace_manifest", list),
5069
+ ("unresolved_intent", list), ("canonical_ir", dict),
5070
+ ("compilation_report", dict)):
5071
+ if not isinstance(c.get(k), typ):
5072
+ errors.append("%s must be a %s" % (k, typ.__name__))
5073
+ if errors:
5074
+ return errors, advisory
5075
+ # ELEMENT shapes, checked before any `.get` on them: an adversarial/buggy compiler that
5076
+ # emits a list of strings (or a non-list `implements`) is a mechanical violation -> exit
5077
+ # 2 with a named fault, NEVER an AttributeError traceback (exit 1). This is the trust
5078
+ # boundary the contract exists to defend; the loops below assume dict elements.
5079
+ for k in ("source", "tests"):
5080
+ for i, u in enumerate(c.get(k)):
5081
+ if not isinstance(u, dict):
5082
+ errors.append("%s[%d] must be an object with unit/sha256" % (k, i))
5083
+ for i, e in enumerate(c.get("trace_manifest")):
5084
+ if not isinstance(e, dict):
5085
+ errors.append("trace_manifest[%d] must be an object with unit/implements" % i)
5086
+ elif not isinstance(e.get("implements"), list):
5087
+ errors.append("trace_manifest[%d].implements must be a list of IR node ids" % i)
5088
+ for i, ui in enumerate(c.get("unresolved_intent")):
5089
+ if not isinstance(ui, dict):
5090
+ errors.append("unresolved_intent[%d] must be an object with ir_region/decision"
5091
+ % i)
5092
+ if errors:
5093
+ return errors, advisory
5094
+ if c.get("_integrity") != _compile_seal(c):
5095
+ errors.append("compilation integrity seal does not match -- source/tests/manifest/"
5096
+ "unresolved_intent was hand-edited after production")
5097
+ # the compilation NAMES which IR it compiled; a stale or foreign ir_hash is refused --
5098
+ # a compilation cannot be measured against a graph this repo does not reproduce.
5099
+ ir_seal = ir_graph.get("_integrity")
5100
+ named = (c.get("canonical_ir") or {}).get("ir_hash")
5101
+ if named != ir_seal:
5102
+ errors.append("canonical_ir.ir_hash %s.. does not match the reference IR seal %s.. "
5103
+ "-- a stale or foreign IR is refused, never assumed"
5104
+ % (str(named)[:12], str(ir_seal)[:12]))
5105
+ node_ids = {nd["id"] for nd in ir_graph.get("nodes") or []}
5106
+ # units resolve on disk and their hashes match the bytes: the manifest cannot lie about
5107
+ # what was compiled.
5108
+ units = {}
5109
+ for section in ("source", "tests"):
5110
+ for u in c.get(section) or []:
5111
+ unit, want = u.get("unit"), u.get("sha256")
5112
+ if not unit:
5113
+ errors.append("%s entry without a unit path" % section)
5114
+ continue
5115
+ # source classification is sticky: a unit listed in BOTH source and tests stays
5116
+ # source, so the degeneracy detector (over source units) cannot be dodged by also
5117
+ # listing a source file under tests. Advisory-only, but the detector stays honest.
5118
+ if units.get(unit) != "source":
5119
+ units[unit] = section
5120
+ full = _contained_unit(base, unit)
5121
+ if full is None:
5122
+ errors.append("%s unit escapes the compilation directory -- a relative, "
5123
+ "contained path is required (the manifest references what was "
5124
+ "compiled): %s" % (section, unit))
5125
+ continue
5126
+ got = _sha256_file(full)
5127
+ if got is None:
5128
+ errors.append("%s unit missing on disk: %s" % (section, unit))
5129
+ elif got != want:
5130
+ errors.append("%s unit hash mismatch (manifest lies about the bytes): %s"
5131
+ % (section, unit))
5132
+ # every manifest id is an IR node (THE named mechanical violation); every manifest unit
5133
+ # is a real source/test unit.
5134
+ for entry in c.get("trace_manifest") or []:
5135
+ unit = entry.get("unit")
5136
+ if unit not in units:
5137
+ errors.append("trace_manifest unit is not a declared source/test unit: %s" % unit)
5138
+ for nid in entry.get("implements") or []:
5139
+ if nid not in node_ids:
5140
+ errors.append("trace_manifest implements an id that is not an IR node: %s"
5141
+ % nid)
5142
+ # unresolved_intent SHAPE blocks; its richness (count, specificity) is advisory only.
5143
+ for ui in c.get("unresolved_intent") or []:
5144
+ if not ui.get("ir_region") or not ui.get("decision"):
5145
+ errors.append("unresolved_intent entry missing ir_region or decision")
5146
+ # ---- ADVISORY (computed always, gates never) ----
5147
+ manifest_units = {e.get("unit") for e in c.get("trace_manifest") or []}
5148
+ per_unit = [len(e.get("implements") or []) for e in c.get("trace_manifest") or []]
5149
+ total_nodes = len(node_ids) or 1
5150
+ mean_fanout = (sum(per_unit) / len(per_unit)) if per_unit else 0.0
5151
+ covered = {nid for e in c.get("trace_manifest") or []
5152
+ for nid in (e.get("implements") or []) if nid in node_ids}
5153
+ # Degeneracy is a property of the SOURCE units: >=2 source units that EACH claim >= the
5154
+ # threshold share of all nodes -> the manifest cannot tell you which code implements
5155
+ # which intent (everything traces to everything). Min-based, not mean-based, and over
5156
+ # source only: one comprehensive source file legitimately implements a whole tiny
5157
+ # package, and tests naturally exercise everything, so neither is a degeneracy signal.
5158
+ source_units = {u for u, s in units.items() if s == "source"}
5159
+ src_fanout = [len(e.get("implements") or []) for e in c.get("trace_manifest") or []
5160
+ if e.get("unit") in source_units]
5161
+ advisory = {
5162
+ "trace_units": len(manifest_units),
5163
+ "mean_nodes_per_unit": round(mean_fanout, 3),
5164
+ "node_coverage": round(len(covered) / total_nodes, 3),
5165
+ "unexplained_units": sorted(u for u in units if u not in manifest_units),
5166
+ "unresolved_intent_count": len(c.get("unresolved_intent") or []),
5167
+ "degenerate": len(src_fanout) >= 2 and all(
5168
+ (pu / total_nodes) >= _COMPILE_DEGENERATE_FANOUT for pu in src_fanout),
5169
+ "empty_unresolved": len(c.get("unresolved_intent") or []) == 0,
5170
+ }
5171
+ return errors, advisory
5172
+
5173
+
5174
+ def _print_compile_advisory(advisory):
5175
+ if not advisory:
5176
+ return
5177
+ print(" advisory (never gates): coverage %.2f, mean %.2f nodes/unit, %d unresolved_intent%s"
5178
+ % (advisory.get("node_coverage", 0.0), advisory.get("mean_nodes_per_unit", 0.0),
5179
+ advisory.get("unresolved_intent_count", 0),
5180
+ ", DEGENERATE manifest" if advisory.get("degenerate") else ""))
5181
+ if advisory.get("empty_unresolved"):
5182
+ print(" advisory: unresolved_intent is EMPTY -- suspicious (a compiler that made no "
5183
+ "choices is rare); reported, never blocked")
5184
+ for u in advisory.get("unexplained_units") or []:
5185
+ print(" advisory: %s has no trace_manifest entry -- unexplained by construction" % u)
5186
+
5187
+
5188
+ def cmd_compile_validate(args):
5189
+ ir_graph, ir_errors = _load_ir_at(args.ir)
5190
+ if ir_graph is None or ir_errors:
5191
+ for e in ir_errors:
5192
+ print("[qa_ledger] compile-validate: reference IR: %s" % e, file=sys.stderr)
5193
+ sys.exit(2)
5194
+ errors, advisory = _validate_compilation(args.compilation, ir_graph)
5195
+ if args.json:
5196
+ print(json.dumps({"compilation": args.compilation, "ir": args.ir,
5197
+ "valid": not errors, "errors": errors, "advisory": advisory},
5198
+ indent=2, ensure_ascii=False))
5199
+ elif errors:
5200
+ # diagnostics to stderr (Unix convention, and consistent with compile-ingest); the
5201
+ # machine signal is the exit code. stdout stays clean for the VALID payload path.
5202
+ print("COMPILE-VALIDATE %s: REFUSED (%d mechanical violation(s))"
5203
+ % (args.compilation, len(errors)), file=sys.stderr)
5204
+ for e in errors:
5205
+ print(" x %s" % e, file=sys.stderr)
5206
+ else:
5207
+ print("COMPILE-VALIDATE %s: VALID (contract conformant)" % args.compilation)
5208
+ _print_compile_advisory(advisory)
5209
+ sys.exit(2 if errors else 0)
5210
+
5211
+
5212
+ def cmd_compile_ingest(args):
5213
+ """Record a VALIDATED compilation into the ledger (ADR-016). Ingesting an invalid
5214
+ compilation is a refusal. unresolved_intent becomes append-only, content-addressed
5215
+ UINT objects + an ISSUES-DEFERRED.md mirror (the house convention `fix` uses); the
5216
+ by-construction unexplained_code is the set of units with no trace_manifest entry."""
5217
+ ledger = _load(args.ledger)
5218
+ _repo_node(ledger, args.repo)
5219
+ repo_path = _scope_path(ledger, args.repo)
5220
+ ir_graph, ir_errors = _load_ir_at(args.ir)
5221
+ if ir_graph is None or ir_errors:
5222
+ for e in ir_errors:
5223
+ print("[qa_ledger] compile-ingest: reference IR: %s" % e, file=sys.stderr)
5224
+ sys.exit(2)
5225
+ errors, advisory = _validate_compilation(args.compilation, ir_graph)
5226
+ if errors:
5227
+ print("[qa_ledger] compile-ingest: REFUSED -- ingesting an invalid compilation is a "
5228
+ "refusal (%d mechanical violation(s)); run compile-validate." % len(errors),
5229
+ file=sys.stderr)
5230
+ for e in errors:
5231
+ print(" x %s" % e, file=sys.stderr)
5232
+ sys.exit(2)
5233
+ with open(args.compilation, encoding="utf-8-sig") as fh:
5234
+ c = json.load(fh)
5235
+ comp_seal = c.get("_integrity")
5236
+ uints, seen_uint = [], set()
5237
+ for ui in c.get("unresolved_intent") or []:
5238
+ uid = "UINT-" + hashlib.sha256(
5239
+ (ui.get("ir_region", "") + "\n" + ui.get("decision", "")).encode("utf-8")
5240
+ ).hexdigest()[:12]
5241
+ # content-addressed: two entries that resolve to the same id ARE the same intent gap
5242
+ # (same ir_region + decision), deduped within this ingest just as across ingests --
5243
+ # otherwise ISSUES-DEFERRED and the ledger record carry the id twice.
5244
+ if uid in seen_uint:
5245
+ continue
5246
+ seen_uint.add(uid)
5247
+ uints.append({"id": uid, "ir_region": ui.get("ir_region"),
5248
+ "decision": ui.get("decision"), "rationale": ui.get("rationale", "")})
5249
+ comp_rec = {"id": comp_seal[:12], "repo": args.repo,
5250
+ "ir_hash": (c.get("canonical_ir") or {}).get("ir_hash"),
5251
+ "model": (c.get("compilation_report") or {}).get("model"),
5252
+ "target_stack": c.get("target_stack"), "seal": comp_seal,
5253
+ "unexplained_units": advisory.get("unexplained_units") or [],
5254
+ "node_coverage": advisory.get("node_coverage"),
5255
+ "unresolved_intent": uints, "at": _now()}
5256
+ comps = ledger.setdefault("compilations", [])
5257
+ # re-ingest is idempotent PER REPO: the seal is the identity within a repo, and a
5258
+ # byte-identical compilation has nothing to supersede. The compilations list is flat and
5259
+ # cross-repo, so the supersede check MUST be scoped by repo -- otherwise two repos that
5260
+ # legitimately produce the same compilation (a shared/small canonical package -- exactly
5261
+ # this milestone's own fixtures) collide, and the second repo's first ingest is dropped
5262
+ # as a false "superseded". A CHANGED compilation reseals -> a new record. Never a dup.
5263
+ superseded = any(x.get("seal") == comp_seal and x.get("repo") == args.repo for x in comps)
5264
+ if not superseded:
5265
+ comps.append(comp_rec)
5266
+ _save(args.ledger, ledger)
5267
+ new_items = []
5268
+ if uints:
5269
+ dpath = os.path.join(repo_path, ISSUES_DEFERRED_FILE)
5270
+ existing = ""
5271
+ if os.path.isfile(dpath):
5272
+ with open(dpath, encoding="utf-8-sig", errors="replace") as fh:
5273
+ existing = fh.read()
5274
+ add = [u for u in uints if u["id"] not in existing]
5275
+ if add:
5276
+ with open(dpath, "a", encoding="utf-8", newline="\n") as fh:
5277
+ if existing and not existing.endswith("\n"):
5278
+ fh.write("\n")
5279
+ for u in add:
5280
+ fh.write("- [ ] %s (unresolved_intent): %s -- the compiler decided "
5281
+ "'%s' on its own; a candidate canonical improvement (ADR-016)\n"
5282
+ % (u["id"], u["ir_region"], u["decision"]))
5283
+ new_items = [u["id"] for u in add]
5284
+ out = {"repo": args.repo, "compilation": comp_rec["id"], "superseded": superseded,
5285
+ "unresolved_intent": [u["id"] for u in uints],
5286
+ "issues_deferred_new": new_items,
5287
+ "unexplained_units": comp_rec["unexplained_units"]}
5288
+ if args.json:
5289
+ print(json.dumps(out, indent=2, ensure_ascii=False))
5290
+ else:
5291
+ print("COMPILE-INGEST %s: compilation %s%s"
5292
+ % (args.repo, comp_rec["id"],
5293
+ " (byte-identical to a prior ingest -- nothing superseded)"
5294
+ if superseded else ""))
5295
+ print(" %d unresolved_intent -> %d new %s item(s); %d unexplained unit(s)"
5296
+ % (len(uints), len(new_items), ISSUES_DEFERRED_FILE,
5297
+ len(comp_rec["unexplained_units"])))
5298
+ sys.exit(0)
5299
+
5300
+
4968
5301
  # --------------------------------------------------------------------------- #
4969
5302
  # facts (T0 / SYSTEM-FACTS: public claims become compiled artifacts of repo
4970
5303
  # facts -- Diamond applied to Diamond. ADR-012.)
@@ -9035,6 +9368,28 @@ def build_parser():
9035
9368
  pir.add_argument("--repo", required=True)
9036
9369
  pir.set_defaults(func=cmd_ir_render)
9037
9370
 
9371
+ pcmv = sub.add_parser(
9372
+ "compile-validate",
9373
+ help="validate a COMPILATION.json against a reference IR (ADR-016); only mechanical "
9374
+ "violations gate, degeneracy stats are advisory and NEVER block")
9375
+ pcmv.add_argument("--ir", required=True,
9376
+ help="the reference IR.json the compilation targets")
9377
+ pcmv.add_argument("--compilation", required=True, help="path to COMPILATION.json")
9378
+ pcmv.add_argument("--json", action="store_true")
9379
+ pcmv.set_defaults(func=cmd_compile_validate)
9380
+
9381
+ pcmi = sub.add_parser(
9382
+ "compile-ingest",
9383
+ help="record a VALIDATED compilation into the ledger (ADR-016): by-construction "
9384
+ "unexplained_code + unresolved_intent as append-only UINT objects + backlog")
9385
+ pcmi.add_argument("--ledger", default="QA-LEDGER.json")
9386
+ pcmi.add_argument("--repo", required=True)
9387
+ pcmi.add_argument("--ir", required=True,
9388
+ help="the reference IR.json the compilation targets")
9389
+ pcmi.add_argument("--compilation", required=True, help="path to COMPILATION.json")
9390
+ pcmi.add_argument("--json", action="store_true")
9391
+ pcmi.set_defaults(func=cmd_compile_ingest)
9392
+
9038
9393
  pcr = sub.add_parser("cleanroom",
9039
9394
  help="run a command against a CLEAN checkout of one commit in a throwaway worktree (ADR-008)")
9040
9395
  pcr.add_argument("--ledger", default="QA-LEDGER.json")
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.72.0",
2
+ "version": "1.73.0",
3
3
  "project": null,
4
4
  "defaults": {
5
5
  "coverage_threshold": 60,