@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
64 lines • 2.46 kB
JavaScript
import { execFile } from "node:child_process";
import { readFile } from "node:fs/promises";
import { processStartTimeMs } from "../../../../utils/process-identity.js";
const LINUX_CLOCK_TICKS_PER_SECOND = 100;
async function linuxProcessStartMs(pid) {
const [stat, systemStat] = await Promise.all([
readFile(`/proc/${pid}/stat`, "utf8"),
readFile("/proc/stat", "utf8"),
]);
const afterComm = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
const startTicks = Number(afterComm[19]);
const bootSeconds = Number(/^btime (\d+)/m.exec(systemStat)?.[1]);
if (!Number.isFinite(startTicks) || !Number.isFinite(bootSeconds))
return undefined;
return Math.round((bootSeconds + startTicks / LINUX_CLOCK_TICKS_PER_SECOND) * 1000);
}
function execText(command, args, env) {
return new Promise((resolve, reject) => {
execFile(command, [...args], {
encoding: "utf8",
timeout: 5_000,
windowsHide: true,
...(env === undefined ? {} : { env }),
}, (error, stdout) => {
if (error)
reject(error);
else
resolve(stdout);
});
});
}
async function darwinProcessStartMs(pid) {
const text = (await execText("ps", ["-o", "lstart=", "-p", String(pid)], { ...process.env, LC_ALL: "C", LANG: "C" })).trim();
if (text.length === 0)
return undefined;
return processStartTimeMs(text);
}
async function windowsProcessStartMs(pid) {
const script = `(Get-Process -Id ${pid} -ErrorAction Stop).StartTime.ToUniversalTime().ToString('o')`;
const text = (await execText("powershell", ["-NoProfile", "-NonInteractive", "-Command", script])).trim();
return processStartTimeMs(text);
}
/**
* When the process currently wearing `pid` started, as the OS reports it; `undefined` when the
* platform cannot say. Spawns on darwin/win32, so callers keep it off every hot path.
*/
export async function readProcessStartMs(pid) {
try {
switch (process.platform) {
case "linux":
return await linuxProcessStartMs(pid);
case "darwin":
return await darwinProcessStartMs(pid);
case "win32":
return await windowsProcessStartMs(pid);
default:
return undefined;
}
}
catch {
return undefined;
}
}
//# sourceMappingURL=process-start-probe.js.map