gabriel / muse public
timing.py python
33 lines 933 B
Raw
sha256:ae1eca169c5efe00a6415971ed0e7259f68df3e3ce23935c4b1b714c2df6a240 Merge branch 'dev' into main Human 22 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 3 commits
sha256:ae1eca169c5efe00a6415971ed0e7259f68df3e3ce23935c4b1b714c2df6a240 Merge branch 'dev' into main Human 22 days ago
sha256:50d413a635f57761c3fce1f70251b957b6423821525bf330747ef4007339222e Merge branch 'dev' into main Human 33 days ago
sha256:fb67fed5a4d3e40de84bdd163de94ef1386570bef1dd1a020a732c8a038962ce Merge branch 'dev' into main Human 52 days ago