Cotton code
102 lines · 5.0 KB · PythonRaw
#!/usr/bin/env python3"""The fan's growth budget over many runs (architect's rulings, 15 Oct 2026,the method fixed before any number was read): one run's growth is afootprint sample minus another, and samples seconds apart swing 4-10 MB onthe simulator, so one run cannot tell a leak from noise. A leak scales withturns; noise does not.For each runtime (iOS 26.5, the owner's phone, and the newest installed) andeach value set (the shipped values, and the owner's fan_tile_w 72 /fan_step_deg 8 / fan_visible_deg 76), the 300-turn run is the leak test: itpasses when it grows less than three times GROWTH_MB and no more than themedian of the five runs at 100 turns — nothing scales with turns. Thatmedian is reported as the noise floor, not a gate (second ruling, on fan3'siOS 27 runs: 300 turns 1 / 4 MB against 100-turn medians of 5 / 9 MB).usage: growth.py <root>  <root>/<values>/<run>/ — each <run> a scripts/perf/fan.sh --out folder  (fan.sh --turns 300 for the long run), <values> `shipped` or `owner`.Writes growth.json and growth.md in <root>; exits 1 when a runtime and valueset fails, or the runs do not cover both runtimes and both value sets."""import jsonimport osimport statisticsimport sysfrom pathlib import Pathsys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))from report import GROWTH_MB, KNOWN, growth, turns_made  # noqa: E402from simulator import RUNTIME  # noqa: E402NAME = 'growth over 100 turns'VALUE_SETS = ('shipped', 'owner')RUNS_AT_100 = 5LONG_TURNS = 300LONG_TIMES = 3def version(runtime):    return tuple(int(part) for part in runtime.split('.') if part.isdigit())def judge(root):    groups = {}    for values in sorted(p for p in root.iterdir() if p.is_dir()):        for run in sorted(p for p in values.iterdir() if (p / 'footprint.txt').exists()):            runtime = (run / 'runtime.txt').read_text().strip() if (run / 'runtime.txt').exists() else ''            grown = growth(run)            group = groups.setdefault((runtime, values.name), {'100': [], 'long': [], 'runs': []})            group['runs'].append(str(run))            if grown is None:                continue            turns = turns_made(run)            if turns == 100:                group['100'].append(grown[0])            elif turns == LONG_TURNS:                group['long'].append(grown[0])    runtimes = sorted({runtime for runtime, _ in groups if runtime}, key=version)    owners = next((r for r in runtimes if (r + '.').startswith(RUNTIME + '.')), RUNTIME)    newer = [r for r in runtimes if version(r) > version(owners)]    # A runtime with no runs still has its rows: they fail, saying what is missing.    wanted = [(runtime, values) for runtime in (owners, newer[-1] if newer else 'newest') for values in VALUE_SETS]    rows = []    for runtime, values in wanted:        group = groups.get((runtime, values), {'100': [], 'long': [], 'runs': []})        median = statistics.median(group['100']) if group['100'] else None        longest = max(group['long']) if group['long'] else None        enough = len(group['100']) >= RUNS_AT_100 and longest is not None        # The leak test: three times the turns grow less than three times the        # budget and no more than 100 turns do.        ok = enough and longest < LONG_TIMES * GROWTH_MB and longest <= median        rows.append({'runtime': runtime, 'values': values, 'median_100': median, 'runs_100': group['100'],                     'growth_300': longest, 'ok': ok,                     'note': '' if enough else f'needs {RUNS_AT_100} runs at 100 turns and one at {LONG_TURNS}'})    ok = all(row['ok'] for row in rows)    return {'name': NAME, 'budget_mb': GROWTH_MB, 'long_budget_mb': LONG_TIMES * GROWTH_MB, 'rows': rows,            'ok': ok, 'known': '' if ok else KNOWN.get(NAME, '')}def main(root):    result = judge(root)    (root / 'growth.json').write_text(json.dumps(result, indent=2) + '\n')    lines = [f"fan growth · the run at {LONG_TURNS} turns grows < {LONG_TIMES * GROWTH_MB} MB and no more than the "             f"median of {RUNS_AT_100} runs at 100 turns (the noise floor, reported), iOS {RUNTIME} and the newest, "             "shipped and owner values", '',             '| iOS | values | noise floor: median, 100 turns | runs | 300 turns | ok |', '|---|---|---|---|---|---|']    for row in result['rows']:        median = '—' if row['median_100'] is None else f"{row['median_100']:.1f} MB"        long = '—' if row['growth_300'] is None else f"{row['growth_300']:.1f} MB"        runs = ', '.join(f'{mb:.0f}' for mb in row['runs_100'])        lines.append(f"| {row['runtime'] or '?'} | {row['values']} | {median} | {runs} | {long} | "                     f"{'✓' if row['ok'] else '✗'} {row['note']} |")    if result['known']:        lines.append('')        lines.append(result['known'])    text = '\n'.join(lines) + '\n'    (root / 'growth.md').write_text(text)    print(text, end='')    return 0 if result['ok'] else 1if __name__ == '__main__':    sys.exit(main(Path(sys.argv[1])))