Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160/** Cross-platform process birth identities for transaction ownership. @module */import { readFile, } from 'node:fs/promises';import nanoSpawn, { SubprocessError, } from 'nano-spawn';
/** Recorded PID does not currently identify a process. */export const PROCESS_IDENTITY_ABSENT: unique symbol = Symbol('transaction owner process identity absent',);
/** Linux process states of a process that has exited: zombie (exited, not yet reaped by its parent) and dead. */const EXITED_LINUX_STATES: ReadonlySet<string> = new Set([ 'Z', 'X',],);
/** Resolves Linux kernel process start tick.
A zombie keeps its PID and start tick until its parent reaps it, which never happens under an init that does not reap orphans, such as a container's Node PID 1 after a `SIGKILL` of a whole process group. It holds no files and runs no code, so it counts as exited. @param pid - process identifier @returns birth identity or absence after process exit */async function resolveLinuxIdentity(pid: number,): Promise<string | typeof PROCESS_IDENTITY_ABSENT> { try { /** Proc stat containing parenthesized command followed by numbered fields. */ const stat = await readFile( `/proc/${String(pid,)}/stat`, 'utf8', ); /** Final command delimiter before field three. */ const commandEnd = stat.lastIndexOf(')',); if (commandEnd === (-1)) throw new TypeError(`Malformed Linux process stat for PID ${String(pid,)}`,); /** Fields three onward. */ const fields = stat.slice(commandEnd + 2,) .trim() .split(' ',); if (EXITED_LINUX_STATES.has(fields[0] ?? '',)) return PROCESS_IDENTITY_ABSENT; /** Field twenty-two relative to field-three origin. */ const startTick = fields[(2 ** (2 + 2)) + 2 + 1]; if ((startTick === undefined) || (startTick.length === 0)) throw new TypeError(`Linux process start identity is unavailable for PID ${String(pid,)}`,); return `linux:${startTick}`; } catch (error: unknown) { // A process that exits between opening and reading its stat file makes the read fail with ESRCH. if (Error.isError(error,) && ('code' in error) && ((error.code === 'ENOENT') || (error.code === 'ESRCH'))) return PROCESS_IDENTITY_ABSENT; throw error; }}
/** Resolves process identity through bounded platform command. @param command - platform process inspector @param args - exact inspector arguments @param prefix - platform identity prefix @returns birth identity or absence after process exit */async function resolveCommandIdentity({ command, args, prefix,}: Readonly<{ command: string; args: readonly string[]; prefix: string;}>,): Promise<string | typeof PROCESS_IDENTITY_ABSENT> { try { /** Locale-stable process inspector result. */ const result = await nanoSpawn( command, [...args,], { env: { LC_ALL: 'C', }, }, ); /** Trimmed platform birth identity. */ const value = result.stdout .trim(); return value.length === 0 ? PROCESS_IDENTITY_ABSENT : `${prefix}:${value}`; } catch (error: unknown) { if (error instanceof SubprocessError) return PROCESS_IDENTITY_ABSENT; throw error; }}
/** Resolves stable PID plus process-birth identity on supported hosts. @param pid - process identifier @returns platform birth identity or absence after process exit @example ```ts await resolveProcessBirthIdentity(process.pid); ``` */export function resolveProcessBirthIdentity(pid: number,): Promise<string | typeof PROCESS_IDENTITY_ABSENT> { if (process.platform === 'linux') return resolveLinuxIdentity(pid,); if (process.platform === 'darwin') return resolveCommandIdentity({ command: 'ps', args: [ '-o', 'lstart=', '-p', String(pid,), ], prefix: 'darwin', },); if (process.platform === 'win32') return resolveCommandIdentity({ command: 'powershell.exe', args: [ '-NoLogo', '-NoProfile', '-NonInteractive', '-Command', `(Get-Process -Id ${String(pid,)}).StartTime.ToUniversalTime().Ticks`, ], prefix: 'win32', },); return Promise.reject(new TypeError(`Unsupported transaction process identity platform: ${process.platform}`,),);}