@cotal-ai/workspace 0.101.0 → 0.101.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/build/Release/darwin-arm64/build.json +5 -4
  2. package/build/Release/darwin-x64/build.json +5 -4
  3. package/build/Release/win32-arm64/build.json +6 -5
  4. package/build/Release/win32-arm64/creation.node +0 -0
  5. package/build/Release/win32-x64/build.json +6 -5
  6. package/build/Release/win32-x64/creation.node +0 -0
  7. package/dist/connect.d.ts +4 -1
  8. package/dist/connect.d.ts.map +1 -1
  9. package/dist/connect.js +4 -1
  10. package/dist/connect.js.map +1 -1
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +2 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/mesh-target.d.ts +3 -2
  16. package/dist/mesh-target.d.ts.map +1 -1
  17. package/dist/mesh-target.js +23 -9
  18. package/dist/mesh-target.js.map +1 -1
  19. package/dist/pid.d.ts.map +1 -1
  20. package/dist/pid.js +7 -1
  21. package/dist/pid.js.map +1 -1
  22. package/dist/render.js +1 -1
  23. package/dist/render.js.map +1 -1
  24. package/dist/service-custody.js +2 -2
  25. package/dist/service-custody.js.map +1 -1
  26. package/dist/service-identity.js +4 -4
  27. package/dist/service-identity.js.map +1 -1
  28. package/dist/service-stop.js +2 -2
  29. package/dist/service-stop.js.map +1 -1
  30. package/dist/win32-custodian.d.ts +2 -0
  31. package/dist/win32-custodian.d.ts.map +1 -0
  32. package/dist/win32-custodian.js +85 -0
  33. package/dist/win32-custodian.js.map +1 -0
  34. package/dist/win32-custody.d.ts +57 -0
  35. package/dist/win32-custody.d.ts.map +1 -0
  36. package/dist/win32-custody.js +220 -0
  37. package/dist/win32-custody.js.map +1 -0
  38. package/dist/win32-process.d.ts +38 -0
  39. package/dist/win32-process.d.ts.map +1 -1
  40. package/dist/win32-process.js +63 -5
  41. package/dist/win32-process.js.map +1 -1
  42. package/native/README.md +3 -3
  43. package/native/win32-process.c +317 -14
  44. package/native/win32-system-id.cpp +32 -0
  45. package/package.json +2 -2
  46. package/scripts/build-native.mjs +10 -4
  47. package/scripts/native-contract.mjs +1 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"win32-custodian.js","sourceRoot":"","sources":["../src/win32-custodian.ts"],"names":[],"mappings":"AAAA;;gFAEgF;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,YAAY,EAA2C,MAAM,oBAAoB,CAAC;AAC5H,OAAO,EAAE,mBAAmB,EAAuB,MAAM,oBAAoB,CAAC;AAE9E,MAAM,WAAW,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AACpC,MAAM,MAAM,GAAG,mBAAmB,EAAE,CAAC;AACrC,8FAA8F;AAC9F,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;AACrC,MAAM,MAAM,GAAG,CAAC,KAAc,EAAE,EAAE,CAAC,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;AACvI,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAEhF,KAAK,UAAU,UAAU;IACvB,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IACxD,OAAO,CAAC,KAAK,CAAC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AAK,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,IAAI,CAAC,OAAO,EAAE,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,QAAQ,EAAE,KAAK,SAAS,CAAC;YAAG,MAAM,KAAK,CAAC,EAAE,CAAC,CAAC;IAClJ,CAAC;IACD,OAAO,MAAM,EAAE,CAAC;AAClB,CAAC;AACD,YAAY,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;AAC1D,MAAM,MAAM,CAAC,EAAE,QAAQ,EAAE,gBAAgB,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;AAC7E,OAAO,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;AAErB,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,EAAE;IAC5B,IAAI,KAAc,CAAC;IACnB,IAAI,CAAC;QAAC,KAAK,GAAG,MAAM,CAAC,QAAQ,EAAE,KAAK,SAAS,IAAI,OAAO,EAAE,CAAC,MAAM,KAAK,CAAC,CAAC;IAAC,CAAC;IAC1E,MAAM,CAAC;QAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAAC,CAAC;IAC1B,IAAI,CAAC,KAAK;QAAE,OAAO;IACnB,aAAa,CAAC,IAAI,CAAC,CAAC;IACpB,+FAA+F;IAC/F,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACtC,CAAC,EAAE,GAAG,CAAC,CAAC"}
@@ -0,0 +1,57 @@
1
+ import { type Server } from "node:net";
2
+ import type { ProcessIdentityRecord } from "./pid.js";
3
+ /**
4
+ * Windows process custody. A detached custodian process per service instance holds the only handle
5
+ * to a kill-on-close job that its program was created in. The custodian's death ends every member,
6
+ * so a lost custodian never leaves an unowned process. A caller reaches the custodian over a named
7
+ * pipe and proves, on every connection, that it holds the instance's 32-byte capability; the
8
+ * custodian proves the same back before the caller trusts any answer. Fresh nonces on both sides
9
+ * bind each answer to its connection.
10
+ */
11
+ export declare const CUSTODY_PROTOCOL = 1;
12
+ export type CustodyRequest = {
13
+ op: "status";
14
+ } | {
15
+ op: "terminate";
16
+ code: number;
17
+ };
18
+ /** What a custodian reports: itself, its program's pin and exit code, the job's members and the
19
+ * program and arguments it launched. Windows has no exec, so that launch is the program's role. */
20
+ export interface CustodyStatus {
21
+ custodian: ProcessIdentityRecord;
22
+ root: ProcessIdentityRecord & {
23
+ exit: number | null;
24
+ };
25
+ members: number[];
26
+ program: string;
27
+ args: string[];
28
+ }
29
+ /** The record of one custodied launch: the custodian's pin and its pipe. The capability itself is
30
+ * kept apart from it, in the caller's protected secret file. */
31
+ export interface Win32Custody {
32
+ custodian: ProcessIdentityRecord;
33
+ pipe: string;
34
+ }
35
+ /** Answer authenticated requests on `server`. A connection that does not prove the capability
36
+ * within the deadline, or sends an oversized or malformed frame, is dropped before `handle` runs. */
37
+ export declare function serveCustody(server: Server, key: Buffer, instanceId: string, handle: (request: CustodyRequest) => Promise<unknown>): void;
38
+ /** Send one request to the custodian at `pipe` and return its authenticated answer. */
39
+ export declare function custodyRequest(pipe: string, key: Buffer, instanceId: string, request: CustodyRequest, timeoutMs?: number): Promise<unknown>;
40
+ /** A custodian's status answer, checked against the custodian the caller recorded. */
41
+ export declare function custodyStatus(value: unknown, custodian: ProcessIdentityRecord): CustodyStatus;
42
+ /** Start a detached custodian that launches `program` in its job. The launch and the capability
43
+ * reach it on stdin, never in its arguments or environment. It answers on stdout once the program
44
+ * runs, and the custodian's own pin is read here before the answer is accepted. `spawned` receives
45
+ * the custodian's pin and pipe as soon as it runs, before its program does. */
46
+ export declare function startWin32Custodian(input: {
47
+ program: string;
48
+ args: readonly string[];
49
+ cwd: string;
50
+ env: NodeJS.ProcessEnv;
51
+ log: string;
52
+ instanceId: string;
53
+ }, key: Buffer, spawned?: (custodian: ProcessIdentityRecord, pipe: string) => void): Promise<Win32Custody & {
54
+ process: ProcessIdentityRecord;
55
+ }>;
56
+ export declare function isCustodyPipe(value: unknown): value is string;
57
+ //# sourceMappingURL=win32-custody.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"win32-custody.d.ts","sourceRoot":"","sources":["../src/win32-custody.ts"],"names":[],"mappings":"AAGA,OAAO,EAAW,KAAK,MAAM,EAAe,MAAM,UAAU,CAAC;AAG7D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGtD;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,IAAI,CAAC;AAOlC,MAAM,MAAM,cAAc,GAAG;IAAE,EAAE,EAAE,QAAQ,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,WAAW,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAClF;oGACoG;AACpG,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,qBAAqB,CAAC;IACjC,IAAI,EAAE,qBAAqB,GAAG;QAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IACtD,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB;AACD;iEACiE;AACjE,MAAM,WAAW,YAAY;IAAG,SAAS,EAAE,qBAAqB,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE;AAoChF;sGACsG;AACtG,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CA0BzI;AAED,uFAAuF;AACvF,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,SAAS,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,CAiC3I;AAOD,sFAAsF;AACtF,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,qBAAqB,GAAG,aAAa,CAO7F;AASD;;;gFAGgF;AAChF,wBAAsB,mBAAmB,CAAC,KAAK,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC,UAAU,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,qBAAqB,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC,YAAY,GAAG;IAAE,OAAO,EAAE,qBAAqB,CAAA;CAAE,CAAC,CAmChT;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE7D"}
@@ -0,0 +1,220 @@
1
+ import { spawn } from "node:child_process";
2
+ import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
3
+ import { existsSync } from "node:fs";
4
+ import { connect } from "node:net";
5
+ import { dirname, join } from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+ import { nativeWin32CreationToken, windowsCommandLine, windowsEnvironmentBlock } from "./win32-process.js";
8
+ /**
9
+ * Windows process custody. A detached custodian process per service instance holds the only handle
10
+ * to a kill-on-close job that its program was created in. The custodian's death ends every member,
11
+ * so a lost custodian never leaves an unowned process. A caller reaches the custodian over a named
12
+ * pipe and proves, on every connection, that it holds the instance's 32-byte capability; the
13
+ * custodian proves the same back before the caller trusts any answer. Fresh nonces on both sides
14
+ * bind each answer to its connection.
15
+ */
16
+ export const CUSTODY_PROTOCOL = 1;
17
+ const FRAME_LIMIT = 64 * 1024;
18
+ const AUTH_DEADLINE_MS = 5_000;
19
+ /** A nonce or a proof: the base64 of 32 bytes, ASCII only. */
20
+ const BASE64_32 = /^[A-Za-z0-9+/]{43}=$/;
21
+ const PIPE = /^\\\\\.\\pipe\\cotal-custody-[0-9a-f]{32}$/;
22
+ const proof = (key, instanceId, label, ...parts) => createHmac("sha256", key).update(["cotal-win32-custody", String(CUSTODY_PROTOCOL), instanceId, label, ...parts].join("\n")).digest("base64");
23
+ // Only a well-formed proof is compared, so both sides are 32 bytes and the comparison cannot throw.
24
+ const sameProof = (value, expected) => typeof value === "string" && BASE64_32.test(value) && timingSafeEqual(Buffer.from(value, "base64"), Buffer.from(expected, "base64"));
25
+ /** Deliver each newline-ended JSON object `socket` sends; anything longer or malformed calls `bad`,
26
+ * as does a frame its handler throws on, so a peer's input ends only its own connection. */
27
+ function frames(socket, frame, bad) {
28
+ let buffer = "";
29
+ socket.setEncoding("utf8");
30
+ socket.on("data", (chunk) => {
31
+ buffer += chunk;
32
+ for (let end = buffer.indexOf("\n"); end >= 0; end = buffer.indexOf("\n")) {
33
+ // A frame over the limit is refused whether or not its newline arrived in the same read.
34
+ if (end > FRAME_LIMIT)
35
+ return bad();
36
+ const line = buffer.slice(0, end);
37
+ buffer = buffer.slice(end + 1);
38
+ let value;
39
+ try {
40
+ value = JSON.parse(line);
41
+ }
42
+ catch {
43
+ return bad();
44
+ }
45
+ if (value === null || typeof value !== "object" || Array.isArray(value))
46
+ return bad();
47
+ try {
48
+ frame(value);
49
+ }
50
+ catch {
51
+ return bad();
52
+ }
53
+ }
54
+ if (buffer.length > FRAME_LIMIT)
55
+ bad();
56
+ });
57
+ }
58
+ function parseRequest(text) {
59
+ const value = JSON.parse(text);
60
+ if (value?.op === "status" && Object.keys(value).length === 1)
61
+ return { op: "status" };
62
+ if (value?.op === "terminate" && Object.keys(value).length === 2 && Number.isInteger(value.code) && Number(value.code) >= 0 && Number(value.code) <= 0xffffffff)
63
+ return { op: "terminate", code: Number(value.code) };
64
+ throw new Error("unknown custody request");
65
+ }
66
+ /** Answer authenticated requests on `server`. A connection that does not prove the capability
67
+ * within the deadline, or sends an oversized or malformed frame, is dropped before `handle` runs. */
68
+ export function serveCustody(server, key, instanceId, handle) {
69
+ server.on("connection", (socket) => {
70
+ let step = 0, client = "", nonce = "";
71
+ const deadline = setTimeout(() => socket.destroy(), AUTH_DEADLINE_MS);
72
+ const drop = () => { clearTimeout(deadline); socket.destroy(); };
73
+ socket.on("error", drop);
74
+ socket.on("close", () => clearTimeout(deadline));
75
+ frames(socket, (frame) => {
76
+ if (step === 0) {
77
+ if (frame.protocol !== CUSTODY_PROTOCOL || typeof frame.nonce !== "string" || !BASE64_32.test(frame.nonce))
78
+ return drop();
79
+ client = frame.nonce;
80
+ nonce = randomBytes(32).toString("base64");
81
+ step = 1;
82
+ socket.write(JSON.stringify({ nonce, proof: proof(key, instanceId, "server", client, nonce) }) + "\n");
83
+ return;
84
+ }
85
+ if (step !== 1)
86
+ return;
87
+ if (typeof frame.request !== "string" || !sameProof(frame.proof, proof(key, instanceId, "request", client, nonce, frame.request)))
88
+ return drop();
89
+ step = 2;
90
+ clearTimeout(deadline);
91
+ let request;
92
+ try {
93
+ request = parseRequest(frame.request);
94
+ }
95
+ catch {
96
+ return drop();
97
+ }
98
+ const reply = (result) => socket.end(JSON.stringify({ result, proof: proof(key, instanceId, "reply", client, nonce, result) }) + "\n");
99
+ handle(request).then((value) => reply(JSON.stringify({ ok: true, value })), (error) => reply(JSON.stringify({ ok: false, error: error instanceof Error ? error.message : String(error) })));
100
+ }, drop);
101
+ });
102
+ }
103
+ /** Send one request to the custodian at `pipe` and return its authenticated answer. */
104
+ export function custodyRequest(pipe, key, instanceId, request, timeoutMs = 20_000) {
105
+ return new Promise((resolve, reject) => {
106
+ const nonce = randomBytes(32).toString("base64");
107
+ const text = JSON.stringify(request);
108
+ let step = 0, server = "", settled = false;
109
+ const socket = connect(pipe);
110
+ const finish = (error, value) => {
111
+ if (settled)
112
+ return;
113
+ settled = true;
114
+ clearTimeout(timer);
115
+ socket.destroy();
116
+ if (error)
117
+ reject(error);
118
+ else
119
+ resolve(value);
120
+ };
121
+ const timer = setTimeout(() => finish(new Error("the process custodian did not answer in time")), timeoutMs);
122
+ socket.on("error", (error) => finish(error));
123
+ socket.on("close", () => finish(new Error("the process custodian closed the connection without an answer")));
124
+ socket.on("connect", () => socket.write(JSON.stringify({ protocol: CUSTODY_PROTOCOL, nonce }) + "\n"));
125
+ frames(socket, (frame) => {
126
+ if (step === 0) {
127
+ if (typeof frame.nonce !== "string" || !BASE64_32.test(frame.nonce) || !sameProof(frame.proof, proof(key, instanceId, "server", nonce, frame.nonce)))
128
+ return finish(new Error("the process custodian did not prove the instance capability"));
129
+ server = frame.nonce;
130
+ step = 1;
131
+ socket.write(JSON.stringify({ request: text, proof: proof(key, instanceId, "request", nonce, server, text) }) + "\n");
132
+ return;
133
+ }
134
+ if (typeof frame.result !== "string" || !sameProof(frame.proof, proof(key, instanceId, "reply", nonce, server, frame.result)))
135
+ return finish(new Error("the process custodian's answer was not authentic"));
136
+ const result = JSON.parse(frame.result);
137
+ if (result.ok === true)
138
+ finish(undefined, result.value);
139
+ else
140
+ finish(new Error(`the process custodian refused the request: ${String(result.error)}`));
141
+ }, () => finish(new Error("the process custodian sent a malformed frame")));
142
+ });
143
+ }
144
+ const pin = (value) => {
145
+ const record = value;
146
+ return typeof record?.pid === "number" && Number.isSafeInteger(record.pid) && record.pid > 0 && typeof record.token === "string" && /^\d{1,20}$/.test(record.token);
147
+ };
148
+ /** A custodian's status answer, checked against the custodian the caller recorded. */
149
+ export function custodyStatus(value, custodian) {
150
+ const status = value;
151
+ if (!status || !pin(status.custodian) || status.custodian.pid !== custodian.pid || status.custodian.token !== custodian.token || !pin(status.root) ||
152
+ !(status.root.exit === null || (Number.isInteger(status.root.exit) && status.root.exit >= 0)) || !Array.isArray(status.members) ||
153
+ !status.members.every((pid) => Number.isSafeInteger(pid) && pid > 0) || typeof status.program !== "string" || !Array.isArray(status.args) || !status.args.every((arg) => typeof arg === "string"))
154
+ throw new Error("the process custodian answered for another custody");
155
+ return status;
156
+ }
157
+ /** The custodian's compiled entry, beside this module in `dist`. */
158
+ function custodianEntry() {
159
+ const path = join(dirname(dirname(fileURLToPath(import.meta.url))), "dist", "win32-custodian.js");
160
+ if (!existsSync(path))
161
+ throw new Error(`the process custodian entry is missing at ${path}; reinstall this version`);
162
+ return path;
163
+ }
164
+ /** Start a detached custodian that launches `program` in its job. The launch and the capability
165
+ * reach it on stdin, never in its arguments or environment. It answers on stdout once the program
166
+ * runs, and the custodian's own pin is read here before the answer is accepted. `spawned` receives
167
+ * the custodian's pin and pipe as soon as it runs, before its program does. */
168
+ export async function startWin32Custodian(input, key, spawned) {
169
+ if (process.platform !== "win32")
170
+ throw new Error("Windows process custody runs only on Windows");
171
+ if (key.length !== 32)
172
+ throw new Error("a custody capability is 32 bytes");
173
+ const pipe = `\\\\.\\pipe\\cotal-custody-${randomBytes(16).toString("hex")}`;
174
+ const launch = { application: input.program, commandLine: windowsCommandLine(input.program, input.args), cwd: input.cwd, environment: windowsEnvironmentBlock(input.env), log: input.log };
175
+ const child = spawn(process.execPath, [custodianEntry()], { detached: true, env: {}, stdio: ["pipe", "pipe", "ignore"], windowsHide: true });
176
+ child.once("spawn", () => {
177
+ const token = nativeWin32CreationToken(child.pid);
178
+ if (token !== undefined)
179
+ spawned?.({ pid: child.pid, token }, pipe);
180
+ });
181
+ try {
182
+ const answer = await new Promise((resolve, reject) => {
183
+ let text = "";
184
+ const timer = setTimeout(() => reject(new Error("the process custodian did not report its launch in time")), 15_000);
185
+ const done = (error) => { clearTimeout(timer); if (error)
186
+ reject(error);
187
+ else
188
+ resolve(text.slice(0, text.indexOf("\n"))); };
189
+ child.once("error", done);
190
+ child.once("exit", () => done(text.includes("\n") ? undefined : new Error("the process custodian exited before it reported a launch")));
191
+ child.stdout.setEncoding("utf8");
192
+ child.stdout.on("data", (chunk) => { text += chunk; if (text.includes("\n"))
193
+ done();
194
+ else if (text.length > FRAME_LIMIT)
195
+ done(new Error("the process custodian's report is oversized")); });
196
+ child.stdin.on("error", () => { });
197
+ child.stdin.end(JSON.stringify({ protocol: CUSTODY_PROTOCOL, capability: key.toString("base64"), instanceId: input.instanceId, program: input.program, args: input.args, pipe, launch }) + "\n");
198
+ });
199
+ const report = JSON.parse(answer);
200
+ if (typeof report.error === "string")
201
+ throw new Error(`the process custodian could not launch the service: ${report.error}`);
202
+ if (!pin(report.custodian) || !pin(report.process) || report.custodian.pid !== child.pid || nativeWin32CreationToken(child.pid) !== report.custodian.token)
203
+ throw new Error("the process custodian's report does not name itself");
204
+ return { custodian: report.custodian, pipe, process: report.process };
205
+ }
206
+ catch (error) {
207
+ // A custodian that did not report a launch holds no running program worth keeping; ending it ends its job.
208
+ if (child.exitCode === null)
209
+ child.kill();
210
+ throw error;
211
+ }
212
+ finally {
213
+ child.stdout?.destroy();
214
+ child.unref();
215
+ }
216
+ }
217
+ export function isCustodyPipe(value) {
218
+ return typeof value === "string" && PIPE.test(value);
219
+ }
220
+ //# sourceMappingURL=win32-custody.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"win32-custody.js","sourceRoot":"","sources":["../src/win32-custody.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,OAAO,EAA4B,MAAM,UAAU,CAAC;AAC7D,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,uBAAuB,E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I,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACvD,CAAC"}
@@ -1,3 +1,41 @@
1
+ /** What one job launch starts: an absolute executable, its full command line (see
2
+ * {@link windowsCommandLine}), its directory, its environment block (see
3
+ * {@link windowsEnvironmentBlock}) and the existing log file its output appends to. */
4
+ export interface Win32JobLaunch {
5
+ application: string;
6
+ commandLine: string;
7
+ cwd: string;
8
+ environment: string;
9
+ log: string;
10
+ }
11
+ /** The shipped Windows binding. `launch` starts one process inside a new job that kills every
12
+ * member when its last handle closes and allows no breakaway; the job and the launched process
13
+ * stay held until this process exits, so a process launches at most once. */
14
+ export interface Win32ProcessBinding {
15
+ creationToken(pid: number): string | undefined;
16
+ userSid(): string;
17
+ /** Windows' system ID for this app's publisher, as hex, and its SystemIdentificationSource:
18
+ * 0 none, 1 TPM, 2 UEFI, 3 registry. */
19
+ systemId(): {
20
+ id: string;
21
+ source: number;
22
+ };
23
+ launch(options: Win32JobLaunch): {
24
+ pid: number;
25
+ token: string;
26
+ };
27
+ rootExit(): number | undefined;
28
+ jobProcesses(): number[];
29
+ inJob(pid: number): boolean | undefined;
30
+ terminate(code: number): void;
31
+ }
32
+ /** Load the shipped Node-API binding once. A broken installation is a loud failure, not an unreadable process. */
33
+ export declare function win32ProcessBinding(): Win32ProcessBinding;
1
34
  /** Read through the shipped Node-API binding. A broken installation is not an unreadable PID. */
2
35
  export declare function nativeWin32CreationToken(pid: number): string | undefined;
36
+ /** The command line for `program` and `args`. A NUL cannot cross the call, so it is refused. */
37
+ export declare function windowsCommandLine(program: string, args: readonly string[]): string;
38
+ /** `env` as a Windows environment block, sorted by name without regard to case, with the variables
39
+ * Windows needs filled from `parent` as libuv does. Two names that differ only in case are refused. */
40
+ export declare function windowsEnvironmentBlock(env: NodeJS.ProcessEnv, parent?: NodeJS.ProcessEnv): string;
3
41
  //# sourceMappingURL=win32-process.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"win32-process.d.ts","sourceRoot":"","sources":["../src/win32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G;AACxG,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,GAAE,MAAM,CAAC,UAAwB,GAAG,MAAM,CAiB/G"}
@@ -2,8 +2,8 @@ import { createRequire } from "node:module";
2
2
  import { fileURLToPath } from "node:url";
3
3
  const require = createRequire(import.meta.url);
4
4
  let binding;
5
- /** Read through the shipped Node-API binding. A broken installation is not an unreadable PID. */
6
- export function nativeWin32CreationToken(pid) {
5
+ /** Load the shipped Node-API binding once. A broken installation is a loud failure, not an unreadable process. */
6
+ export function win32ProcessBinding() {
7
7
  if (process.platform !== "win32" || (process.arch !== "x64" && process.arch !== "arm64"))
8
8
  throw new Error(`Windows creation identity is unsupported on ${process.platform}/${process.arch}`);
9
9
  if (binding === undefined) {
@@ -15,10 +15,68 @@ export function nativeWin32CreationToken(pid) {
15
15
  catch (cause) {
16
16
  throw new Error("could not load the shipped Windows creation identity binding; reinstall this version", { cause });
17
17
  }
18
- if (loaded === null || typeof loaded !== "object" || !("creationToken" in loaded) || typeof loaded.creationToken !== "function")
19
- throw new Error("the shipped Windows creation identity binding has no creationToken function");
18
+ if (loaded === null || typeof loaded !== "object" || !["creationToken", "userSid", "systemId", "launch", "rootExit", "jobProcesses", "inJob", "terminate"].every((name) => typeof loaded[name] === "function"))
19
+ throw new Error("the shipped Windows creation identity binding is missing a reader");
20
20
  binding = loaded;
21
21
  }
22
- return binding.creationToken(pid);
22
+ return binding;
23
+ }
24
+ /** Read through the shipped Node-API binding. A broken installation is not an unreadable PID. */
25
+ export function nativeWin32CreationToken(pid) {
26
+ return win32ProcessBinding().creationToken(pid);
27
+ }
28
+ /** One argument as the Microsoft C runtime parses it back, quoted the way libuv quotes a spawn's
29
+ * arguments, so a job launch passes the same argv a Node spawn did. */
30
+ function quoteArgument(arg) {
31
+ if (arg === "")
32
+ return '""';
33
+ if (!/[ \t"]/.test(arg))
34
+ return arg;
35
+ if (!/["\\]/.test(arg))
36
+ return `"${arg}"`;
37
+ let quoted = '"', backslashes = 0;
38
+ for (const char of arg) {
39
+ if (char === "\\") {
40
+ backslashes++;
41
+ continue;
42
+ }
43
+ quoted += char === '"' ? `${"\\".repeat(backslashes * 2 + 1)}"` : `${"\\".repeat(backslashes)}${char}`;
44
+ backslashes = 0;
45
+ }
46
+ return `${quoted}${"\\".repeat(backslashes * 2)}"`;
47
+ }
48
+ /** The command line for `program` and `args`. A NUL cannot cross the call, so it is refused. */
49
+ export function windowsCommandLine(program, args) {
50
+ if ([program, ...args].some((part) => part.includes("\0")))
51
+ throw new Error("a Windows command line cannot carry a NUL character");
52
+ return [program, ...args].map(quoteArgument).join(" ");
53
+ }
54
+ /** Variables a Windows child needs, which libuv copies from the parent when an environment omits them. */
55
+ const REQUIRED = ["HOMEDRIVE", "HOMEPATH", "LOGONSERVER", "PATH", "SYSTEMDRIVE", "SYSTEMROOT", "TEMP", "USERDOMAIN", "USERNAME", "USERPROFILE", "WINDIR"];
56
+ /** `env` as a Windows environment block, sorted by name without regard to case, with the variables
57
+ * Windows needs filled from `parent` as libuv does. Two names that differ only in case are refused. */
58
+ export function windowsEnvironmentBlock(env, parent = process.env) {
59
+ const entries = new Map();
60
+ const add = (name, value) => {
61
+ if (value === undefined)
62
+ return;
63
+ if (!/^=?[^=\0]+$/.test(name) || value.includes("\0"))
64
+ throw new Error(`invalid environment variable ${JSON.stringify(name)} for a Windows launch`);
65
+ const key = name.toUpperCase();
66
+ if (entries.has(key))
67
+ throw new Error(`the environment names ${key} more than once`);
68
+ entries.set(key, [name, value]);
69
+ };
70
+ for (const [name, value] of Object.entries(env))
71
+ add(name, value);
72
+ for (const name of REQUIRED) {
73
+ if (entries.has(name))
74
+ continue;
75
+ const found = Object.keys(parent).find((key) => key.toUpperCase() === name);
76
+ if (found !== undefined)
77
+ add(found, parent[found]);
78
+ }
79
+ const sorted = [...entries.entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
80
+ return sorted.map(([, [name, value]]) => `${name}=${value}\0`).join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EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC;QAAE,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAClE,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC5B,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,SAAS;QAChC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,IAAI,CAAC,CAAC;QAC5E,IAAI,KAAK,KAAK,SAAS;YAAE,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACrD,CAAC;IACD,MAAM,MAAM,GAAG,CAAC,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACvF,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,IAAI,IAAI,KAAK,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC1E,CAAC"}
package/native/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Native process identity
2
2
 
3
- `win32-process.c` uses the stable Node-API C interface and Windows process-query APIs. It returns the creation FILETIME as a decimal string. It has no runtime dependency or PowerShell fallback.
3
+ `win32-process.c` uses the stable Node-API C interface and documented Windows APIs from `kernel32` and `advapi32`. It returns a process's creation FILETIME as a decimal string and this account's SID from its token. For process custody it starts one program per process inside a new job object. The job kills every member when its last handle closes and allows no breakaway. `CreateProcessW` assigns the job at creation through `PROC_THREAD_ATTRIBUTE_JOB_LIST`, so no child starts outside it. It passes the child only its input and log handles through `PROC_THREAD_ATTRIBUTE_HANDLE_LIST`, so no member can hold the job open. The binding then lists the job's processes, reads the program's exit code and terminates the job. It has no runtime dependency or PowerShell fallback. `win32-system-id.cpp` reads the system ID Windows documents for an unpackaged app's publisher, through C++/WinRT's `SystemIdentification.GetSystemIdForPublisher`, and the module Windows derived it from. It is C++20 and links `windowsapp.lib`; the binding reports the ID and its source, and a caller refuses a source of none.
4
4
 
5
5
  `darwin-process.c` uses the same interface and the documented macOS kernel interfaces. `sysctl` with `KERN_PROC_PID` gives a process's start time in microseconds, its process group and whether it is stopped. `KERN_PROC_PGRP` lists the members of a process group. `gethostuuid` gives the machine's UUID and `kern.bootsessionuuid` gives this boot's UUID. It does not use `libproc`, whose header calls its interfaces private and subject to change, and it never runs `ps`.
6
6
 
7
- Build on native Windows x64 and arm64 in the matching MSVC Developer Command Prompt, or on macOS x64 and arm64 with Xcode or its command line tools, with `pnpm --filter @cotal-ai/workspace build:native`. The script downloads only the checksum-pinned Node headers in `node-sdk.json`, and on Windows the import library. A macOS bundle targets macOS 11.0, the oldest macOS Node 22 supports, and resolves Node-API symbols from the loading `node`. The script records source, SDK, compiler and binary provenance beside the output. An existing binary must verify before reuse; stale output is an error.
7
+ Build on native Windows x64 and arm64 in the matching MSVC Developer Command Prompt, or on macOS x64 and arm64 with Xcode or its command line tools, with `pnpm --filter @cotal-ai/workspace build:native`. The script downloads only the checksum-pinned Node headers in `node-sdk.json`, and on Windows the import library. On Windows it takes the C++/WinRT headers from the Windows SDK the prompt selects. A macOS bundle targets macOS 11.0, the oldest macOS Node 22 supports, and resolves Node-API symbols from the loading `node`. The script records source, SDK, compiler and binary provenance beside the output. An existing binary must verify before reuse; stale output is an error.
8
8
 
9
- Both Windows and both macOS architecture directories and their receipts must be assembled under `build/Release` before packing or publishing workspace; the shared native workflow builds the macOS pair on `macos-15-intel` and `macos-15` when a caller asks for it with `darwin: true`. Customer installation uses these prebuilt files and runs no compiler or native download. The native CI suite runs the same Node-API 8 binaries on Node 22, 24 and 26. Its PowerShell compatibility oracle is test-only and runs after the first cold production read. `smoke/darwin-process-native.smoke.ts` runs the macOS binding on a macOS runner.
9
+ Both Windows and both macOS architecture directories and their receipts must be assembled under `build/Release` before packing or publishing workspace; the shared native workflow builds the macOS pair on `macos-15-intel` and `macos-15` when a caller asks for it with `darwin: true`. Customer installation uses these prebuilt files and runs no compiler or native download. The native CI suite runs the same Node-API 8 binaries on Node 22, 24 and 26. Its PowerShell compatibility oracle is test-only and runs after the first cold production read. `smoke/darwin-process-native.smoke.ts` runs the macOS binding on a macOS runner, and `smoke/win32-job-native.smoke.ts` runs the Windows job custody on a Windows runner. `smoke/win32-custody.smoke.ts` checks the custodian handshake on every platform and runs the custodian itself on Windows.