UNPKG

@phren/cli

Version:

Knowledge layer for AI agents - CLI, MCP server, and data layer

789 lines (788 loc) • 29.1 kB
import { spawn as spawnChild } from "node:child_process"; import { FrameReader } from "./frame-reader.js"; import { beginMacWindowStream, logMacWindowStreamRefusal } from "./mac-windows.js"; import { verifiedHelper } from "./native-helpers.js"; import { BridgeError } from "./protocol.js"; /** Longest side in pixels, capture fps and encoder/wire target. */ export const MAC_WINDOW_TIERS = [ { size: 320, fps: 5, bitsPerSecond: 96_000 }, { size: 480, fps: 8, bitsPerSecond: 160_000 }, { size: 640, fps: 10, bitsPerSecond: 240_000 }, { size: 960, fps: 12, bitsPerSecond: 384_000 }, ]; export const MAC_WINDOW_MAX_TIER = MAC_WINDOW_TIERS.length - 1; /** One Hook carries one live window at a time. */ export const MAX_MAC_WINDOW_STREAMS = 1; /** One binary WS message: the 24-byte header, parameter sets and the access unit. */ export const MAX_MESSAGE_BYTES = 24_576; /** One helper stdout record: its tag byte plus at most one wire message. */ export const MAX_RECORD_BYTES = MAX_MESSAGE_BYTES + 1; export const HEADER_BYTES = 24; /** Client controls are text and this short. */ export const MAX_CONTROL_BYTES = 256; /** Video bursts may use the whole bucket, then refill at the tier's bytes/s. */ export const BUCKET_CAPACITY_BYTES = 24_576; /** No tier may refill faster than this, which bounds 3 s of video to 168576 B. */ export const BUCKET_CEILING_BYTES_PER_SECOND = 48_000; export const FIRST_SEND_DEADLINE_MS = 12_000; export const PULL_DEADLINE_MS = 5_000; export const ACK_DEADLINE_MS = 4_000; export const STOP_GRACE_MS = 250; export const SIGKILL_AFTER_MS = 2_000; const OPEN = 1; const STDIN_EXIT_GRACE_MS = 1_000; const STREAM_FIELDS = new Set(["id", "codec", "tier"]); /** The stream query, validated before reserving a slot or starting a helper. */ export function streamQuery(query) { for (const field of query.keys()) { if (!STREAM_FIELDS.has(field)) throw new BridgeError(400, "Unknown mac window stream option."); } for (const field of STREAM_FIELDS) { if (query.getAll(field).length > 1) throw new BridgeError(400, `Duplicate ${field}.`); } const rawId = query.get("id"); if (rawId === null || !/^[1-9]\d{0,9}$/.test(rawId) || Number(rawId) > 4_294_967_295) { throw new BridgeError(400, "id must be a window id from 1 to 4294967295."); } const rawCodec = query.get("codec"); if (rawCodec !== null && rawCodec !== "h264" && rawCodec !== "hevc") throw new BridgeError(400, "codec must be h264 or hevc."); const rawTier = query.get("tier"); if (rawTier !== null && !/^[0-3]$/.test(rawTier)) throw new BridgeError(400, "tier must be 0 to 3."); return { id: Number(rawId), codec: rawCodec ?? "h264", tier: rawTier === null ? 1 : Number(rawTier) }; } const defaultDeps = { helper: () => verifiedHelper("wincap"), spawn: (binary, args) => spawnChild(binary, args, { stdio: ["pipe", "pipe", "pipe"] }), now: () => performance.now(), }; /** WebSocket close reasons are at most 123 bytes. */ function reason(text) { return Buffer.from(text).subarray(0, 120).toString("utf8").replace(/�+$/, ""); } function tierRate(tier) { return Math.min(BUCKET_CEILING_BYTES_PER_SECOND, MAC_WINDOW_TIERS[tier].bitsPerSecond / 8); } /** The wire layout of one helper tag-1 record, before the Hook assigns its seq. */ function parseSample(body, codec, maxSide) { if (body.length < HEADER_BYTES + 4) throw new Error("short sample"); if (body.length > MAX_MESSAGE_BYTES) throw new Error("sample over the wire cap"); if (body.toString("latin1", 0, 4) !== "MWV1") throw new Error("bad magic"); const flags = body[4]; if ((flags & 0xfe) !== 0) throw new Error("unknown flags"); const sync = (flags & 1) === 1; const params = body[5]; const width = body.readUInt16BE(6), height = body.readUInt16BE(8); if (width < 2 || width > 960 || height < 2 || height > 960) throw new Error("bad dimensions"); if ((width & 1) !== 0 || (height & 1) !== 0) throw new Error("odd dimensions"); if (Math.max(width, height) > maxSide) throw new Error("longer side than the pulled tier"); if (body.readUInt16BE(10) !== 0) throw new Error("reserved field set"); if (body.readUInt32BE(12) !== 0) throw new Error("helper set the sequence"); const ptsUs = body.readBigUInt64BE(16); if (params !== (sync ? (codec === "h264" ? 2 : 3) : 0)) throw new Error("bad parameter-set count"); // SPS/PPS for H.264, VPS/SPS/PPS for HEVC, in that wire order. const nalTypes = codec === "h264" ? [7, 8] : [32, 33, 34]; let offset = HEADER_BYTES; for (let i = 0; i < params; i++) { if (offset + 2 > body.length) throw new Error("truncated parameter set"); const length = body.readUInt16BE(offset); offset += 2; if (length === 0 || offset + length > body.length) throw new Error("bad parameter set"); const type = codec === "h264" ? body[offset] & 0x1f : (body[offset] >> 1) & 0x3f; if (type !== nalTypes[i]) throw new Error("parameter sets out of order"); offset += length; } let units = 0; while (offset < body.length) { if (offset + 4 > body.length) throw new Error("truncated NAL length"); const length = body.readUInt32BE(offset); offset += 4; if (length === 0 || offset + length > body.length) throw new Error("bad NAL length"); offset += length; units += 1; } if (units === 0) throw new Error("no NAL units"); return { sync, width, height, ptsUs, body }; } /** Close code and reason for an admission refusal (BridgeError with a stable code). */ function admissionClose(error) { // Section 1's 1008 reason tokens only. The slice-1 permission code is the // same refusal as the stream's Screen Recording token. const stable = new Set(["mac-windows-owner-only", "mac-windows-disabled", "mac-windows-stopped", "mac-windows-badge", "mac-windows-screen-recording", "unsupported-platform"]); const details = error instanceof BridgeError ? error.details : undefined; const code = details && typeof details === "object" && !Array.isArray(details) && typeof details.code === "string" ? details.code : undefined; if (code === "shutdown") return { code: 1001, reason: code }; if (code === "mac-windows-permission") return { code: 1008, reason: "mac-windows-screen-recording" }; return code && stable.has(code) ? { code: 1008, reason: code } : { code: 1011, reason: "capture-failed" }; } /** Exit code to close code (contract section 2's helper exits). */ function exitClose(code) { if (code === 3) return { code: 1008, reason: "mac-windows-screen-recording" }; if (code === 5) return { code: 4404, reason: "window-gone" }; if (code === 6) return { code: 4415, reason: "codec-unavailable" }; return { code: 1011, reason: "capture-failed" }; } let active = 0; /** Streams running now, for tests and health. */ export function activeMacWindowStreams() { return active; } const live = new Set(); /** Hook shutdown: stop every live stream (1001) before the sockets are torn down. */ export function closeMacWindowStreams() { for (const shutdown of [...live]) shutdown(1001, "shutdown"); } export async function streamMacWindow(client, query, owner, deps = defaultDeps) { const logRefusal = (why, id) => { void logMacWindowStreamRefusal(why, id).catch(() => undefined); }; const closeNow = (code, why) => { if (client.readyState !== OPEN) return; try { client.close(code, reason(why)); } catch { /* already gone */ } }; // The owner gate comes first: no query parsing, no slot disclosure. if (!owner) { logRefusal("mac-windows-owner-only"); closeNow(1008, "mac-windows-owner-only"); return; } let options; try { options = streamQuery(query); } catch { logRefusal("bad-request"); closeNow(1008, "bad-request"); return; } if (active >= MAX_MAC_WINDOW_STREAMS) { logRefusal("stream-busy", options.id); closeNow(1013, "stream-busy"); return; } active += 1; let starting = true; let childExited = false; let released = false; const release = () => { if (!released) { released = true; active = Math.max(0, active - 1); } }; const releaseIfDone = () => { if (!starting && (!child || childExited)) release(); }; let ended = false; let child; /** The child announced it started; an 'error' after this is not a spawn failure. */ let childStarted = false; /** Its exit status, and whether stdout has finished, in either order. */ let childStatus; let stdoutEnded = false; let lease; let finishedLease = false; const connection = new AbortController(); /** The first terminal outcome, kept for a lease that arrives after it. */ let outcome; const finishLease = () => { if (!ended || finishedLease || !lease) return; finishedLease = true; void lease.finish(outcome).catch(() => undefined); }; let firstSendTimer; let pullTimer; /** The absolute time the pending `pullTimer` was scheduled for. */ let pullDueAt = Infinity; let pullDeadline; let stdinExitTimer; let ackDeadline; let tokenTimer; let termTimer; let killTimer; const later = (ms, task) => { const timer = setTimeout(task, Math.max(0, Math.ceil(ms))); timer.unref?.(); return timer; }; const clear = (timer) => { if (timer) clearTimeout(timer); }; // Frames. let epoch = 1; /** The start sent for the current epoch, or undefined before its first frame. */ let started; /** Advance the epoch before the next frame, once, however many reasons queued. */ let rotate = false; let needsSync = true; let lastPts; // Pacing. let tier = options.tier; let requestedTier = options.tier; let maxTier = MAC_WINDOW_MAX_TIER; let tokens = BUCKET_CAPACITY_BYTES; let rate = tierRate(tier); let refilledAt = deps.now(); let staged; let outstanding; /** A text send (start or stats) is in flight; nothing else may go out yet. */ let sending = false; let lastSeq = 0; let pullOutstanding = false; let lastPullAt = -Infinity; /** The tier of the PULL whose one answer is outstanding. */ let pullTier = options.tier; let oversize = 0; let spawned = false; /** Inside one stdout chunk: a PULL waits until the chunk is fully judged. */ let inChunk = false; let pullWanted = false; const reader = new FrameReader(MAX_RECORD_BYTES); const refill = () => { const now = deps.now(); if (now > refilledAt) { tokens = Math.min(BUCKET_CAPACITY_BYTES, tokens + (now - refilledAt) * rate / 1000); refilledAt = now; } }; /** Resolve the outstanding pull without producing a frame. */ const resolvePull = () => { pullOutstanding = false; clear(pullDeadline); pullDeadline = undefined; }; const sendText = (message, done) => { if (ended) return; if (client.readyState !== OPEN) { sending = false; return; } try { client.send(JSON.stringify(message), { binary: false }, error => { if (error) { sending = false; if (!ended) end(1011, "capture-failed"); return; } if (!ended) done?.(); }); } catch { sending = false; end(1011, "capture-failed"); } }; const sendFrame = (message) => { if (client.readyState !== OPEN) return false; try { client.send(message, { binary: true }, error => { if (error && !ended) end(1011, "capture-failed"); }); } catch { end(1011, "capture-failed"); return false; } return true; }; const end = (code, why) => { if (ended) return; ended = true; outcome = why; connection.abort(why); for (const timer of [firstSendTimer, pullTimer, pullDeadline, stdinExitTimer, ackDeadline, tokenTimer, termTimer, killTimer]) clear(timer); firstSendTimer = pullTimer = pullDeadline = stdinExitTimer = ackDeadline = tokenTimer = termTimer = killTimer = undefined; staged = undefined; outstanding = undefined; pullOutstanding = false; sending = false; live.delete(shutdown); if (child && !childExited) { const running = child; // STOP first; EOF is STOP too, so ending stdin also covers a wedged helper. try { running.stdin?.write("STOP\n"); running.stdin?.end(); } catch { /* pipe already closed */ } termTimer = later(STOP_GRACE_MS, () => { try { running.kill("SIGTERM"); } catch { /* gone */ } }); killTimer = later(SIGKILL_AFTER_MS, () => { try { running.kill("SIGKILL"); } catch { /* gone */ } }); running.once("exit", () => { clear(termTimer); termTimer = undefined; clear(killTimer); killTimer = undefined; }); } releaseIfDone(); finishLease(); if (code !== undefined) closeNow(code, why); }; function shutdown(code, why) { end(code, why); } live.add(shutdown); client.once("close", code => end(undefined, code === 1000 ? "done" : "capture-failed")); client.once("error", () => end(undefined, "capture-failed")); /** The exit code decides the close, but only after stdout was judged. */ const settleExit = () => { if (ended || childStatus === undefined || !stdoutEnded) return; const close = exitClose(childStatus); end(close.code, close.reason); }; /** stdout EOF first: a partial record is bad-helper-frame, whatever the exit. */ const stdoutDone = () => { if (stdoutEnded) return; stdoutEnded = true; if (ended) return; try { reader.finish(); } catch { end(1011, "bad-helper-frame"); return; } settleExit(); }; /** Rotate the epoch before the next frame; several queued reasons collapse. */ const restartEpoch = () => { if (started) rotate = true; needsSync = true; }; const tryPull = () => { if (inChunk) { pullWanted = true; return; } // A child that exited is only draining whatever stdout already holds. if (ended || client.readyState !== OPEN || !spawned || childExited || stdinExitTimer || pullOutstanding || staged || outstanding || sending) return; // Pace off the tier the latest clamped request would use, so an upshift // shortens a pending wait and a superseded downshift never commits. const due = lastPullAt + 1000 / MAC_WINDOW_TIERS[requestedTier].fps; const now = deps.now(); if (now < due) { if (pullTimer && pullDueAt !== due) { clear(pullTimer); pullTimer = undefined; } if (!pullTimer) { pullDueAt = due; pullTimer = later(due - now, () => { pullTimer = undefined; pullDueAt = Infinity; tryPull(); }); } return; } clear(pullTimer); pullTimer = undefined; pullDueAt = Infinity; if (requestedTier !== tier) { // The tier and its rate take effect only here, at the PULL they pace. // Credit earned so far was earned at the old tier's rate. refill(); tier = requestedTier; rate = tierRate(tier); restartEpoch(); } if (!submit(`PULL ${tier} ${needsSync ? 1 : 0}\n`)) return; lastPullAt = now; pullOutstanding = true; pullTier = tier; pullDeadline = later(PULL_DEADLINE_MS, () => { pullDeadline = undefined; if (!ended && pullOutstanding) end(1011, "capture-failed"); }); }; const stdinFailed = () => { if (ended || stdinExitTimer) return; // A failed write can precede a classified exit. Keep any pending answer // eligible for stdout validation while waiting for the child's status. clear(firstSendTimer); firstSendTimer = undefined; clear(pullTimer); pullTimer = undefined; clear(pullDeadline); pullDeadline = undefined; stdinExitTimer = later(STDIN_EXIT_GRACE_MS, () => end(1011, "capture-failed")); }; const submit = (line) => { if (!child?.stdin) { stdinFailed(); return false; } try { child.stdin.write(line); return true; } catch { stdinFailed(); return false; } }; /** Debit the bucket for the whole staged message, then send it. */ const pump = () => { if (ended || !staged || outstanding || sending) return; refill(); if (tokens + 1e-6 < staged.body.length) { if (!tokenTimer) { tokenTimer = later((staged.body.length - tokens) * 1000 / rate, () => { tokenTimer = undefined; pump(); }); } return; } clear(tokenTimer); tokenTimer = undefined; const sample = staged; staged = undefined; if (rotate) { epoch = epoch >= 0xffff_ffff ? 1 : epoch + 1; rotate = false; started = undefined; } // A coded size change is a new epoch's sync frame; a delta may not move it. if (started && (started.width !== sample.width || started.height !== sample.height)) { if (!sample.sync) { end(1011, "bad-helper-frame"); return; } epoch = epoch >= 0xffff_ffff ? 1 : epoch + 1; started = undefined; needsSync = true; } if (needsSync && !sample.sync) { end(1011, "bad-helper-frame"); return; } /** Assign the sequence and put the sample on the wire. */ const deliver = () => { if (ended) return; // Debit at the binary send, not when it was staged: a slow start // callback must not let the bucket refill behind a waiting frame. refill(); tokens -= sample.body.length; const seq = lastSeq + 1; lastSeq = seq; const message = Buffer.from(sample.body); message.writeUInt32BE(seq, 12); outstanding = { seq, tier, sentAt: deps.now() }; if (sample.sync) needsSync = false; oversize = 0; clear(firstSendTimer); firstSendTimer = undefined; ackDeadline = later(ACK_DEADLINE_MS, () => { ackDeadline = undefined; if (!ended && outstanding?.seq === seq) end(1013, "slow-link"); }); sendFrame(message); }; if (!started) { if (!sample.sync) { end(1011, "bad-helper-frame"); return; } started = { width: sample.width, height: sample.height }; // The sample only follows a start the socket accepted. sending = true; sendText({ type: "start", version: 1, codec: options.codec, epoch, width: sample.width, height: sample.height, tier, maxTier }, () => { sending = false; deliver(); }); return; } deliver(); }; const onSample = (body) => { let sample; try { sample = parseSample(body, options.codec, MAC_WINDOW_TIERS[pullTier].size); } catch { end(1011, "bad-helper-frame"); return; } if (lastPts !== undefined && sample.ptsUs <= lastPts) { end(1011, "bad-helper-frame"); return; } lastPts = sample.ptsUs; if (needsSync && !sample.sync) { end(1011, "bad-helper-frame"); return; } staged = sample; pump(); }; const onOversize = () => { // Only oversize responses at the floor count; any video frame resets it. staged = undefined; if (tier === 0) oversize += 1; maxTier = Math.max(0, Math.min(maxTier, tier - 1)); requestedTier = Math.min(requestedTier, maxTier); restartEpoch(); if (tier === 0 && oversize >= 3) { end(1013, "slow-link"); return; } tryPull(); }; const onRecord = (record) => { const tag = record[0]; if (tag !== 0 && tag !== 1 && tag !== 2) { end(1011, "bad-helper-frame"); return; } if (tag !== 1 && record.length !== 1) { end(1011, "bad-helper-frame"); return; } if (!pullOutstanding) { end(1011, "bad-helper-frame"); return; } resolvePull(); if (tag === 0) { tryPull(); return; } if (tag === 2) { onOversize(); return; } onSample(record.subarray(1)); }; const controlSeq = (value) => typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= 4_294_967_295 ? value : undefined; /** Stale below the outstanding seq, a future seq is a protocol error. */ const seqState = (seq) => { if (outstanding) return seq < outstanding.seq ? "stale" : seq > outstanding.seq ? "future" : "current"; return seq <= lastSeq ? "stale" : "future"; }; const onAck = (seq) => { const state = seqState(seq); if (state === "stale") return; if (state === "future" || !outstanding) { end(1008, "bad-control"); return; } const sample = outstanding; outstanding = undefined; clear(ackDeadline); ackDeadline = undefined; const ackMs = Math.max(0, Math.round(deps.now() - sample.sentAt)); // The next pull waits for the stats write to be accepted, not just started. sending = true; sendText({ type: "stats", seq: sample.seq, ackMs, tier: sample.tier }, () => { sending = false; if (ended) return; pump(); tryPull(); }); }; const onKeyframe = (seq) => { const state = seqState(seq); if (state === "stale") return; if (state === "future" || !outstanding) { end(1008, "bad-control"); return; } outstanding = undefined; clear(ackDeadline); ackDeadline = undefined; restartEpoch(); tryPull(); }; const onControl = (data, isBinary) => { if (ended) return; if (isBinary || data.length > MAX_CONTROL_BYTES) { end(1008, "bad-control"); return; } let parsed; try { parsed = JSON.parse(data.toString("utf8")); } catch { end(1008, "bad-control"); return; } if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { end(1008, "bad-control"); return; } const message = parsed; if (message.type === "ack" || message.type === "keyframe") { const seq = controlSeq(message.seq); if (seq === undefined) { end(1008, "bad-control"); return; } if (message.type === "ack") onAck(seq); else onKeyframe(seq); return; } if (message.type === "set") { const set = message.tier; if (typeof set !== "number" || !Number.isInteger(set) || set < 0 || set > MAC_WINDOW_MAX_TIER) { end(1008, "bad-control"); return; } requestedTier = Math.min(set, maxTier); tryPull(); return; } end(1008, "bad-control"); }; client.on("message", onControl); try { if (ended || client.readyState !== OPEN) return; lease = await beginMacWindowStream(owner, options.id, why => end(why === "shutdown" ? 1001 : 1008, why), connection.signal); if (ended || client.readyState !== OPEN) { finishLease(); return; } const binary = await deps.helper(); if (ended || client.readyState !== OPEN) return; await lease.recheckAdmission(); if (ended || client.readyState !== OPEN) return; const spawnedChild = deps.spawn(binary, ["stream", String(options.id), options.codec]); child = spawnedChild; spawnedChild.once("spawn", () => { childStarted = true; }); spawnedChild.once("exit", (code) => { childExited = true; childStatus = code; releaseIfDone(); // stdout can end after this event; settleExit waits for it. settleExit(); }); spawnedChild.once("error", () => { // Only a failure before 'spawn' is a failed start; a later error (a // refused signal, say) leaves a started child running, so its slot stays // taken until it actually exits. if (!childStarted) childExited = true; releaseIfDone(); if (!ended) end(1011, "capture-failed"); }); // A helper that exits mid-write makes Node emit EPIPE here, asynchronously. spawnedChild.stdin?.on("error", stdinFailed); const stdout = spawnedChild.stdout; if (!stdout) stdoutEnded = true; stdout?.on("data", (chunk) => { if (ended) return; // The buffer is empty after every chunk (trailing bytes are rejected // below), so any byte now belongs to an answer to the one PULL. if (!pullOutstanding) { end(1011, "bad-helper-frame"); return; } let records; try { records = reader.push(chunk); } catch { end(1011, "bad-helper-frame"); return; } inChunk = true; pullWanted = false; try { for (const record of records) { if (ended) break; onRecord(record); } } finally { inChunk = false; } if (ended) return; // A second record had no PULL of its own, and extra bytes cannot be the // start of the next answer: both were already on the wire before it. if (records.length > 0 && reader.pending() > 0) { end(1011, "bad-helper-frame"); return; } if (pullWanted) { pullWanted = false; tryPull(); } }); stdout?.on("end", stdoutDone); // Node may close the child before the readable's own close; both mean EOF. stdout?.on("close", stdoutDone); spawnedChild.once("close", stdoutDone); // Bounded, never quoted: the helper's own words never reach a close reason. spawnedChild.stderr?.on("data", () => { }); spawned = true; firstSendTimer = later(FIRST_SEND_DEADLINE_MS, () => { firstSendTimer = undefined; if (!ended) end(1011, "capture-failed"); }); tryPull(); } catch (error) { if (!ended) { const close = admissionClose(error); end(close.code, close.reason); } } finally { starting = false; releaseIfDone(); } }