companion-resource-probe.mjs
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠ breaking
1 day ago
| 1 | /** |
| 2 | * Companion App — Phase 5 runtime resource probe. |
| 3 | * |
| 4 | * Probes only the supervised runtime PID. VRAM is reported as aggregate-safe zero |
| 5 | * in this phase; this module never enumerates GPU process tables or escalates |
| 6 | * privilege for telemetry. |
| 7 | */ |
| 8 | |
| 9 | import { execFile as nodeExecFile } from 'node:child_process'; |
| 10 | import { readFile as nodeReadFile } from 'node:fs/promises'; |
| 11 | import os from 'node:os'; |
| 12 | import { promisify } from 'node:util'; |
| 13 | |
| 14 | const execFileAsync = promisify(nodeExecFile); |
| 15 | |
| 16 | export const RESOURCE_PROBE_REASONS = Object.freeze({ |
| 17 | INVALID_PID: 'invalid_pid', |
| 18 | PROBE_FAILED: 'probe_failed', |
| 19 | }); |
| 20 | |
| 21 | /** |
| 22 | * @param {unknown} pid |
| 23 | * @returns {number} |
| 24 | */ |
| 25 | export function requireRuntimePid(pid) { |
| 26 | if (!Number.isInteger(pid) || pid <= 0) { |
| 27 | throw new TypeError(RESOURCE_PROBE_REASONS.INVALID_PID); |
| 28 | } |
| 29 | return pid; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Create a PID-scoped resource probe with a <=500ms cache. |
| 34 | * @param {{ |
| 35 | * pid: number, |
| 36 | * platform?: string, |
| 37 | * now?: () => number, |
| 38 | * execFile?: Function, |
| 39 | * readFile?: Function, |
| 40 | * pageSizeBytes?: number, |
| 41 | * cacheMs?: number, |
| 42 | * }} opts |
| 43 | */ |
| 44 | export function createCompanionResourceProbe(opts) { |
| 45 | const pid = requireRuntimePid(opts?.pid); |
| 46 | const platform = opts?.platform ?? process.platform; |
| 47 | const now = opts?.now ?? (() => Date.now()); |
| 48 | const execFile = opts?.execFile ?? execFileAsync; |
| 49 | const readFile = opts?.readFile ?? nodeReadFile; |
| 50 | const pageSizeBytes = opts?.pageSizeBytes ?? 4096; |
| 51 | const cacheMs = Math.min(opts?.cacheMs ?? 500, 500); |
| 52 | let cachedAt = -Infinity; |
| 53 | let cached = null; |
| 54 | |
| 55 | return { |
| 56 | async statResources() { |
| 57 | const current = now(); |
| 58 | if (cached && current - cachedAt <= cacheMs) return cached; |
| 59 | cached = await probeByPlatform({ pid, platform, execFile, readFile, pageSizeBytes }); |
| 60 | cachedAt = current; |
| 61 | return cached; |
| 62 | }, |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param {{ pid: number, platform: string, execFile: Function, readFile: Function, pageSizeBytes: number }} opts |
| 68 | */ |
| 69 | export async function probeByPlatform(opts) { |
| 70 | if (opts.platform === 'linux') return probeLinuxProc(opts); |
| 71 | if (opts.platform === 'darwin' || opts.platform === 'freebsd') return probeWithPs(opts); |
| 72 | if (opts.platform === 'win32') return probeWindows(opts); |
| 73 | return probeWithPs(opts); |
| 74 | } |
| 75 | |
| 76 | async function probeLinuxProc({ pid, readFile, pageSizeBytes }) { |
| 77 | try { |
| 78 | const statm = await readFile(`/proc/${pid}/statm`, 'utf8'); |
| 79 | const parts = String(statm).trim().split(/\s+/); |
| 80 | const rssPages = Number(parts[1]); |
| 81 | if (!Number.isFinite(rssPages) || rssPages < 0) throw new Error('bad_statm'); |
| 82 | return { ramBytes: rssPages * pageSizeBytes, vramBytes: 0, cpuPercent: 0 }; |
| 83 | } catch { |
| 84 | throw new Error(RESOURCE_PROBE_REASONS.PROBE_FAILED); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | async function probeWithPs({ pid, execFile }) { |
| 89 | try { |
| 90 | const result = await execFile('/bin/ps', ['-o', 'rss=', '-o', 'pcpu=', '-p', String(pid)], { |
| 91 | encoding: 'utf8', |
| 92 | maxBuffer: 4096, |
| 93 | }); |
| 94 | const stdout = typeof result === 'string' ? result : result.stdout; |
| 95 | const line = String(stdout).trim().split(/\n/).find(Boolean); |
| 96 | if (!line) throw new Error('missing_ps'); |
| 97 | const [rssKb, cpuPercent] = line.trim().split(/\s+/).map(Number); |
| 98 | if (!Number.isFinite(rssKb) || rssKb < 0 || !Number.isFinite(cpuPercent) || cpuPercent < 0) { |
| 99 | throw new Error('bad_ps'); |
| 100 | } |
| 101 | return { ramBytes: rssKb * 1024, vramBytes: 0, cpuPercent }; |
| 102 | } catch { |
| 103 | throw new Error(RESOURCE_PROBE_REASONS.PROBE_FAILED); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | async function probeWindows({ pid, execFile }) { |
| 108 | try { |
| 109 | const ps = [ |
| 110 | '-NoProfile', |
| 111 | '-NonInteractive', |
| 112 | '-Command', |
| 113 | `"$p = Get-Process -Id ${pid}; Write-Output ($p.WorkingSet64.ToString() + ' ' + $p.CPU.ToString())"`, |
| 114 | ]; |
| 115 | const result = await execFile('powershell.exe', ps, { encoding: 'utf8', maxBuffer: 4096 }); |
| 116 | const stdout = typeof result === 'string' ? result : result.stdout; |
| 117 | const [ramBytes, cpuSeconds] = String(stdout).trim().split(/\s+/).map(Number); |
| 118 | if (!Number.isFinite(ramBytes) || ramBytes < 0 || !Number.isFinite(cpuSeconds) || cpuSeconds < 0) { |
| 119 | throw new Error('bad_windows_probe'); |
| 120 | } |
| 121 | const cpuPercent = Math.min(100, cpuSeconds / Math.max(1, os.cpus().length)); |
| 122 | return { ramBytes, vramBytes: 0, cpuPercent }; |
| 123 | } catch { |
| 124 | throw new Error(RESOURCE_PROBE_REASONS.PROBE_FAILED); |
| 125 | } |
| 126 | } |
File History
2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠
1 day ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6
docs: accept Calendar Events v0 spec with Phase 0 security …
Human
1 day ago