@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
38 lines • 2.04 kB
JavaScript
import { uptime as osUptimeSeconds } from "node:os";
import { workerData } from "node:worker_threads";
export { PROCESS_START_TOLERANCE_MS, sameProcessStartMs as sameProcessStart, } from "../../../../utils/process-identity.js";
/**
* Two boot instants further apart than this belong to different boots. `os.uptime()` is
* integral seconds on every platform, and a laptop's sleep is accounted in uptime, so the
* only drift left is sub-second rounding on either side.
*/
export const BOOT_INSTANT_TOLERANCE_MS = 120_000;
const wholeSeconds = (ms) => Math.round(ms / 1000) * 1000;
export function processBootAtMs(now = Date.now) {
return wholeSeconds(now() - osUptimeSeconds() * 1000);
}
const floorToSecond = (ms) => Math.floor(ms / 1000) * 1000;
/**
* Captured once at load: `process.uptime()` runs on a monotonic clock that stops during a Linux
* suspend, so recomputing later would drift by every suspend and read our own live lease as reused.
*/
// Bun's worker uptime starts at isolate creation, not process creation. A session
// worker receives the host's start instant so its lease names the same OS process.
const hostIdentity = workerData;
const OWN_START_AT_LOAD = typeof hostIdentity === "object" &&
hostIdentity !== null &&
"hostProcessStartedAtMs" in hostIdentity &&
typeof hostIdentity.hostProcessStartedAtMs === "number" &&
Number.isFinite(hostIdentity.hostProcessStartedAtMs)
? hostIdentity.hostProcessStartedAtMs
: floorToSecond(Date.now() - process.uptime() * 1000);
/** Floored like `ps -o lstart`, which truncates to the second: rounding up could land after now. */
export function ownProcessStartedAtMs(now) {
return now === undefined ? OWN_START_AT_LOAD : floorToSecond(now() - process.uptime() * 1000);
}
export function sameBoot(bootAtMs, otherBootAtMs) {
if (otherBootAtMs === undefined || !Number.isFinite(otherBootAtMs))
return false;
return Math.abs(bootAtMs - otherBootAtMs) <= BOOT_INSTANT_TOLERANCE_MS;
}
//# sourceMappingURL=process-identity.js.map