gabriel / muse public
timing.py python
33 lines 933 B
Raw
sha256:61100cca63d948098d334e1b600d8fea514568a1c26ef357bf8b6380fbd8a217 chore(timeline): remove unused RationalRate import in entity.py Human minor ⚠ breaking 8 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:61100cca63d948098d334e1b600d8fea514568a1c26ef357bf8b6380fbd8a217 chore(timeline): remove unused RationalRate import in entity.py Human minor 8 days ago