gabriel / muse public
timing.py python
33 lines 933 B
Raw
sha256:057be43e401106b2b4d877e8028ae67d16a966562d7429707b71a5eaacd5027b docs(mwp-master): tick all ACs green; mark MWP-7 complete w… Sonnet 4.6 20 days ago
1 """Monotonic timing utility for agent-facing JSON output.
2
3 Every ``muse`` command that emits JSON includes ``duration_ms`` and
4 ``exit_code`` so agents can measure latency and check results without
5 inspecting the process exit status separately.
6
7 Usage::
8
9 elapsed = start_timer()
10 # ... do work ...
11 output["duration_ms"] = elapsed()
12 output["exit_code"] = 0
13 """
14
15 import time
16 from collections.abc import Callable
17
18 def start_timer() -> Callable[[], float]:
19 """Capture a monotonic start time and return an elapsed-ms callable.
20
21 Returns:
22 A zero-argument callable that, when called, returns the number of
23 milliseconds elapsed since ``start_timer()`` was called, rounded
24 to three decimal places.
25
26 Example::
27
28 elapsed = start_timer()
29 do_work()
30 ms = elapsed() # e.g. 12.345
31 """
32 t0 = time.monotonic()
33 return lambda: round((time.monotonic() - t0) * 1000, 3)
File History 1 commit
sha256:057be43e401106b2b4d877e8028ae67d16a966562d7429707b71a5eaacd5027b docs(mwp-master): tick all ACs green; mark MWP-7 complete w… Sonnet 4.6 20 days ago