oct
Loading...
Searching...
No Matches
tiers.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# oct/timing/tiers.py
4
5"""
6Purpose
7-------
8Declare each timed stage's cost-of-quality tier and that tier's budget.
9
10Responsibilities
11----------------
12- Keep local timing data bounded, private and advisory.
13
14Diagnostics
15-----------
16Domain: GENERAL
17Levels:
18 L1 -- instrumentation failures
19 L2 -- lifecycle
20 L3 -- semantic details
21 L4 -- deep tracing
22
23Contracts
24---------
25- Timing instrumentation never changes the wrapped operation's outcome.
26- A project may lower a shipped tier budget and never raise one.
27
28Dependencies
29------------
30- oc_diagnostics (structured timing diagnostics)
31"""
32from __future__ import annotations
33
34from collections.abc import Mapping
35from types import MappingProxyType
36from oc_diagnostics import _dbg
37
38INNER_LOOP = "inner_loop"
39PER_COMMIT = "per_commit"
40PER_LANDING = "per_landing"
41PERIODIC = "periodic"
42
43#: The Tooling spec's "Cost of quality: tier budgets" table. Aims, never
44#: limits: the unattended tier ships no budget, so it has none to lower.
45TIER_BUDGETS_MS: Mapping[str, int | None] = MappingProxyType({
46 INNER_LOOP: 10_000,
47 PER_COMMIT: 30_000,
48 PER_LANDING: 900_000,
49 PERIODIC: None,
50})
51
52#: Every stage the ledger can record: each command path `command_stage`
53#: builds from the CLI group, plus the three engine operations. A stage
54#: whose moment is unclear takes the slower tier rather than a tight one.
55_STAGES_BY_TIER: Mapping[str, tuple[str, ...]] = {
56 INNER_LOOP: ("format", "health", "lint", "new", "oct"),
57 PER_COMMIT: ("git", "git.add", "git.branch", "git.check", "git.commit",
58 "git.commit-msg", "git.ignore", "git.restore", "git.status"),
59 PER_LANDING: ("agent", "audit", "clean", "complexity", "deps", "docs",
60 "engine.dispatch", "engine.prepare", "engine.teardown",
61 "export-skeleton", "export-source", "git.changelog", "test",
62 "typecheck"),
63 PERIODIC: ("diag", "diag.list-domains", "diag.migrate-config",
64 "diag.restore", "diag.set-level", "diag.validate-config",
65 "git.hooks", "git.hooks.install", "git.hooks.remove",
66 "git.hooks.status", "git.init", "install", "install-hooks",
67 "install-mcp", "migrate", "migrate.option-c", "mutate",
68 "scaffold"),
69}
70
71STAGE_TIERS: Mapping[str, str] = MappingProxyType(
72 {stage: tier for tier, stages in _STAGES_BY_TIER.items() for stage in stages})
73
74def tier_for(stage: object) -> str | None:
75 """Return the declared tier, never inventing one for an unknown stage."""
76 return STAGE_TIERS.get(stage) if isinstance(stage, str) else None
77
78def budgets_ms(overrides: object = None) -> dict[str, int | None]:
79 """Apply project overrides, which may only lower a shipped budget."""
80 func = "budgets_ms"
81 budgets = dict(TIER_BUDGETS_MS)
82 if not isinstance(overrides, Mapping):
83 return budgets
84 for tier, value in overrides.items():
85 shipped = TIER_BUDGETS_MS.get(tier) if isinstance(tier, str) else None
86 if shipped is None or type(value) is not int or value <= 0 or value > shipped:
87 _dbg("GENERAL", func, "VALIDATION_ERROR: invalid timing budget", 3)
88 continue
89 budgets[tier] = value
90 return budgets
91
92def budget_for(stage: object, budgets: Mapping[str, int | None] | None = None) -> int | None:
93 """Return the effective budget, or None when the stage declares no tier."""
94 tier = tier_for(stage)
95 return None if tier is None else (TIER_BUDGETS_MS if budgets is None else budgets).get(tier)
dict[str, int|None] budgets_ms(object overrides=None)
Definition tiers.py:78
int|None budget_for(object stage, Mapping[str, int|None]|None budgets=None)
Definition tiers.py:92
str|None tier_for(object stage)
Definition tiers.py:74