|
| 1 | +"""Size discipline for concepts and for the files billed on every turn. |
| 2 | +
|
| 3 | +The transferable rule from `virgiliojr94/book-to-skill` is not its numbers, it is |
| 4 | +the sentence beside them: depth is earned with content, not with a bigger number. |
| 5 | +A chapter that genuinely has less to say should land below the floor and be |
| 6 | +reported thin. Padding it to hit a target makes the artifact worse while making |
| 7 | +the metric better, so this report says so in its own output — a number printed |
| 8 | +without that sentence invites exactly the wrong fix. |
| 9 | +
|
| 10 | +OKFy required sections and never sizes. More to the point, nothing in the tool |
| 11 | +distinguished the two economies it actually has: `AGENTS.md` and `index.md` are |
| 12 | +resident and re-read on EVERY turn, while concepts are fetched on demand. Those |
| 13 | +are different costs and this module reports them separately. |
| 14 | +
|
| 15 | +Advisory by the owner's decision. `okfy budget` exits 0 always, |
| 16 | +`W_BUDGET_RESIDENT` is a warning at every strictness level, and `release_check` |
| 17 | +never composes any of it. |
| 18 | +""" |
| 19 | +from okfy.bundle import Bundle |
| 20 | +from okfy.tokens import count_path, token_method |
| 21 | + |
| 22 | +RESIDENT_FILES = ("AGENTS.md", "index.md") |
| 23 | + |
| 24 | +ANTI_PADDING = ( |
| 25 | + "depth is earned with content, not with a bigger number: a thin concept " |
| 26 | + "should stay thin and be reported, never padded to reach a target" |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +def resident_core(bundle: Bundle) -> dict: |
| 31 | + """The files an agent keeps loaded for a whole session. |
| 32 | +
|
| 33 | + One predicate, two callers: `okfy cost` bills this into its retrieval |
| 34 | + strategy and `okfy budget` reports it against a target. Two modules with |
| 35 | + two definitions of "resident" is the drift shape the audits kept finding. |
| 36 | + """ |
| 37 | + tokens, present, missing = 0, [], [] |
| 38 | + for name in RESIDENT_FILES: |
| 39 | + p = bundle.root / name |
| 40 | + if p.is_file(): |
| 41 | + tokens += count_path(p) |
| 42 | + present.append(name) |
| 43 | + else: |
| 44 | + missing.append(name) |
| 45 | + return {"tokens": tokens, "files": present, "missing": missing} |
| 46 | + |
| 47 | + |
| 48 | +def _percentile(sorted_vals: list[int], q: float) -> int: |
| 49 | + if not sorted_vals: |
| 50 | + return 0 |
| 51 | + i = min(len(sorted_vals) - 1, int(round(q * (len(sorted_vals) - 1)))) |
| 52 | + return sorted_vals[i] |
| 53 | + |
| 54 | + |
| 55 | +def _median(sorted_vals: list[int]) -> int: |
| 56 | + n = len(sorted_vals) |
| 57 | + if not n: |
| 58 | + return 0 |
| 59 | + mid = n // 2 |
| 60 | + return sorted_vals[mid] if n % 2 else (sorted_vals[mid - 1] + sorted_vals[mid]) // 2 |
| 61 | + |
| 62 | + |
| 63 | +def budget_report(bundle: Bundle, archetype=None) -> dict: |
| 64 | + """Read-only. An archetype with no `budgets:` block is not a defect: every |
| 65 | + target reads `None` and no warning is produced.""" |
| 66 | + budgets = (getattr(archetype, "budgets", None) or {}) |
| 67 | + type_targets = budgets.get("types") or {} |
| 68 | + resident_max = budgets.get("resident_max") |
| 69 | + |
| 70 | + sizes: dict[str, list[int]] = {} |
| 71 | + ids: dict[str, list[tuple[str, int]]] = {} |
| 72 | + unreadable: list[dict] = [] |
| 73 | + for c in bundle.concepts(): |
| 74 | + if c.id.startswith("meta/"): |
| 75 | + continue |
| 76 | + t = str(c.meta.get("type")) |
| 77 | + try: |
| 78 | + n = count_path(c.path) |
| 79 | + except OSError as e: |
| 80 | + unreadable.append({"id": c.id, "reason": type(e).__name__}) |
| 81 | + continue |
| 82 | + sizes.setdefault(t, []).append(n) |
| 83 | + ids.setdefault(t, []).append((c.id, n)) |
| 84 | + |
| 85 | + types = [] |
| 86 | + for t in sorted(sizes): |
| 87 | + v = sorted(sizes[t]) |
| 88 | + target = type_targets.get(t) or {} |
| 89 | + tmin, tmax = target.get("target_min"), target.get("target_max") |
| 90 | + thin = sorted(i for i, n in ids[t] if tmin is not None and n < tmin) |
| 91 | + over = sorted(i for i, n in ids[t] if tmax is not None and n > tmax) |
| 92 | + types.append({"type": t, "count": len(v), "median": _median(v), |
| 93 | + "p90": _percentile(v, 0.9), "min": v[0], "max": v[-1], |
| 94 | + "target_min": tmin, "target_max": tmax, |
| 95 | + "thin": thin, "over": over}) |
| 96 | + |
| 97 | + resident = resident_core(bundle) |
| 98 | + resident["target_max"] = resident_max |
| 99 | + resident["over"] = bool(resident_max and resident["tokens"] > resident_max) |
| 100 | + |
| 101 | + notes = [ANTI_PADDING] |
| 102 | + if not budgets: |
| 103 | + notes.append( |
| 104 | + f"archetype {getattr(archetype, 'name', '(none)')!r} declares no " |
| 105 | + "budgets: block — targets read '-' and nothing is out of range. " |
| 106 | + "The block is optional; its absence is not a defect") |
| 107 | + if resident["missing"]: |
| 108 | + notes.append("not packaged: " + ", ".join(resident["missing"]) |
| 109 | + + " absent, so the resident total is undercounted") |
| 110 | + if unreadable: |
| 111 | + notes.append(f"{len(unreadable)} concept(s) could not be read and are " |
| 112 | + "absent from every figure below") |
| 113 | + return {"bundle": str(bundle.root), |
| 114 | + "archetype": getattr(archetype, "name", None), |
| 115 | + "token_method": token_method(), |
| 116 | + "resident": resident, "types": types, |
| 117 | + "unreadable": unreadable, "notes": notes, |
| 118 | + "anti_padding": ANTI_PADDING} |
0 commit comments