#!/usr/bin/env python3"""The cut's typed speed record, read off the one [speed] line, and the dated gate. record.py check <speed.txt> <speed.json> [--mac] parse, write the record, refuse over budget once it gates record.py lines <speed.json> the record as cut.txt key=value lines"""import datetimeimport jsonfrom pathlib importPathimport reimport sysRECORD_PAGE_BUDGET_MS = 100COLD_START_BUDGET_MS = 1000# Architect ruling, 14 Oct 2026 04:30 IST (direction review, Cotton note 97e11a96;# coordinator todo 8cdbb48f "Before launch"): the speed budget is measured and# reported in every cut now, and gates — refuses the cut — only from 1 Nov 2026.# The cut's own UTC date decides; there is no bypass flag.GATES_FROM = datetime.date(2026, 11, 1)# The exact line scripts/speedgate/report.py prints. A changed wording fails here# loudly; a figure is never read as zero or skipped.LINE = re.compile( r'\[speed\] record page (\d+\.\d) ms \(budget (\d+); first open (\d+\.\d) ms with the warm-up, ' r'(\d+\.\d) ms without\) · cold start (\d+\.\d) ms \(budget (\d+)\) · (\S.* simulator)')# The Mac cut measures the phone app on the simulator; the Mac app itself has no# measurement yet, and its record says so rather than carrying an empty figure.NOT_MEASURED_ON_MAC = 'not measured on Mac'ORDER = ('record_page_ms', 'record_page_budget_ms', 'first_open_warmed_ms', 'first_open_unwarmed_ms','cold_start_ms', 'cold_start_budget_ms', 'measured_on', 'gates', 'gates_from','mac_record_page_ms', 'mac_cold_start_ms', 'mac_measured_on')def parse(text, day, mac=False): lines = [line for line in text.splitlines() if line.startswith('[speed]')]if len(lines) != 1:raiseValueError(f'expected exactly one [speed] line, found {len(lines)}') match = LINE.fullmatch(lines[0])ifnot match:raiseValueError(f'the [speed] line does not parse: {lines[0]!r}') page, page_budget, warmed, unwarmed, cold, cold_budget, measured_on = match.groups()if (int(page_budget), int(cold_budget)) != (RECORD_PAGE_BUDGET_MS, COLD_START_BUDGET_MS):raiseValueError(f'the [speed] line names budgets {page_budget}/{cold_budget} ms, ' f'not the {RECORD_PAGE_BUDGET_MS}/{COLD_START_BUDGET_MS} ms in force') record = {'record_page_ms': float(page), 'record_page_budget_ms': RECORD_PAGE_BUDGET_MS,'first_open_warmed_ms': float(warmed), 'first_open_unwarmed_ms': float(unwarmed),'cold_start_ms': float(cold), 'cold_start_budget_ms': COLD_START_BUDGET_MS,'measured_on': measured_on,'gates': datetime.date.fromisoformat(day) >= GATES_FROM, 'gates_from': GATES_FROM.isoformat(), }if mac: record.update({'mac_record_page_ms': None, 'mac_cold_start_ms': None, 'mac_measured_on': NOT_MEASURED_ON_MAC})return recorddef over_budget(record): over = []if record['record_page_ms'] > record['record_page_budget_ms']: over.append(f"record page {record['record_page_ms']:.1f} ms is over its budget of {record['record_page_budget_ms']} ms")if record['cold_start_ms'] > record['cold_start_budget_ms']: over.append(f"cold start {record['cold_start_ms']:.1f} ms is over its budget of {record['cold_start_budget_ms']} ms")return overdef refusal(record): over = over_budget(record)if record['gates'] and over:return f"[speed] REFUSED: {'; '.join(over)} (the budget gates from {record['gates_from']})"returnNonedef lines(record):def value(v):return''if v isNoneelse str(v).lower() if isinstance(v, bool) else str(v)return [f'{key}={value(record[key])}'for key in ORDER if key in record]def main(argv):if len(argv) >= 3and argv[0] == 'check'and argv[3:] in ([], ['--mac']): day = datetime.datetime.now(datetime.timezone.utc).date().isoformat() record = parse(Path(argv[1]).read_text(), day, mac=argv[3:] == ['--mac'])Path(argv[2]).write_text(json.dumps(record, indent=1) + '\n') said = refusal(record)if said: print(said, file=sys.stderr)return1for over in over_budget(record): print(f"[speed] over budget, reported only until {record['gates_from']}: {over}", file=sys.stderr)return0if len(argv) == 2and argv[0] == 'lines': print('\n'.join(lines(json.loads(Path(argv[1]).read_text()))))return0 print(__doc__, file=sys.stderr)return2if __name__ == '__main__':try: sys.exit(main(sys.argv[1:]))except (ValueError, OSError, KeyError) as error: sys.exit(f'[speed] FAILED: {error}')