@arki/clock
For @arki/[email protected] · source AGENTS.md
AGENTS.md = laws, docs/agent-guide.md = tasks, llms.txt = map.
@arki/clock
The one place code asks "what time is it?" and "wake me later". Read docs/agent-guide.md for tasks. When a doc and the code disagree, src/index.ts and src/clock.ts win.
Laws
- Library and app code takes a
Clockas an argument. It never callsnew Date(),Date.now()orsetTimeoutfor logic that tests need to control. - The default is
SystemClock. A test passesMockClock. There is no global clock and no way to install one. - Time moves forward on purpose only.
MockClock.advanceandadvanceAsyncreject negative and non-finite steps withARKI_CLOCK_E001.setNowis the only way back, and it fires no timers. - A delay must be finite, zero or more, and at most
MAX_TIMER_MS. Anything else throwsARKI_CLOCK_E002at once, on every clock. A mock never accepts what a real clock refuses. now()returns a freshDateeach call. No caller can change the clock by changing the value it got.- An aborted
sleepreturnsSleepCancelledwith privatecauseset tosignal.reason. The timer is cleared first. - The package depends only on portable
@arki/failureand nonode:*import. It usesDateandsetTimeoutonly at call time, so it runs on every engine. - Static setup misuse throws
ClockError; operational sleep cancellation returns the canonicalSLEEP_FAILURESoutcome. Match definitions or codes, never messages. - Never write benchmark numbers by hand.
bun run bench --write-readmewrites them from an idle machine.
Changing this package
- Change
src/clock.tsorsrc/errors.ts. Keep the JSDoc on every error code: summary,@fix,@verify. - Add the case to
src/clock.test.ts. Pin the boundary that decides the behaviour, such as a delay of exactlyMAX_TIMER_MS. - Keep
MockClockandSystemClockequal on every rule above. A rule that only the mock enforces hides bugs until production. - Add a changeset in
.changeset/. Documentation-only changes usepatchand the lineno demo impact: documentation and doc-example changes only, runtime behaviour unchanged.
Verify
bun run build
bun run typecheck
bun run test
bun run test:docs
bun run test:runtimes
bun ../../tooling/library/src/llms.ts --check