@indigoai-us/hq-cli 5.345.215-beta.0 → 5.345.215
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.
- package/CHANGELOG.md +65 -1
- package/assets/scaffold/core/scripts/hq-delegate-verify.sh +5 -2
- package/dist/command-catalog.generated.d.ts +46 -6
- package/dist/command-catalog.generated.js +53 -6
- package/dist/commands/bot.js +64 -6
- package/dist/commands/capacity.d.ts +4 -1
- package/dist/commands/capacity.js +46 -10
- package/dist/commands/cloud.js +1 -1
- package/dist/commands/core-delegate-w1b.js +15 -1
- package/dist/commands/core-policy.js +67 -8
- package/dist/commands/daemon.js +5 -0
- package/dist/commands/files-browse.d.ts +11 -4
- package/dist/commands/files-browse.js +21 -4
- package/dist/commands/files.d.ts +1 -1
- package/dist/commands/files.js +25 -11
- package/dist/commands/integrations-auth-rules.d.ts +27 -0
- package/dist/commands/integrations-auth-rules.js +79 -0
- package/dist/commands/integrations-import.js +124 -12
- package/dist/commands/lanes.js +2 -2
- package/dist/commands/mesh.d.ts +2 -0
- package/dist/commands/mesh.js +60 -35
- package/dist/commands/sync-held.js +5 -2
- package/dist/lazy-commands.js +3 -5
- package/dist/lib/bot/fleet-cron.d.ts +8 -0
- package/dist/lib/bot/fleet-cron.js +35 -3
- package/dist/lib/bot/fleet-daemon.d.ts +46 -0
- package/dist/lib/bot/fleet-daemon.js +99 -1
- package/dist/lib/bot/probe.js +7 -1
- package/dist/lib/bot/run.js +20 -1
- package/dist/lib/bot/runtime/codex.js +1 -1
- package/dist/lib/bot/runtime/grok-acp.d.ts +5 -1
- package/dist/lib/bot/runtime/grok-acp.js +6 -3
- package/dist/lib/bot/runtime/index.js +19 -9
- package/dist/lib/bot/runtime/messages-stream.js +2 -2
- package/dist/lib/bot/runtime/types.d.ts +15 -0
- package/dist/lib/bot/runtime/types.js +9 -0
- package/dist/lib/bot/runtime-failure.d.ts +23 -0
- package/dist/lib/bot/runtime-failure.js +43 -1
- package/dist/lib/capacity/accounts.d.ts +8 -0
- package/dist/lib/capacity/accounts.js +19 -0
- package/dist/lib/capacity/available.d.ts +154 -0
- package/dist/lib/capacity/available.js +905 -0
- package/dist/lib/capacity/codex-availability.d.ts +14 -1
- package/dist/lib/capacity/codex-availability.js +26 -1
- package/dist/lib/capacity/collectors/claude-oauth.d.ts +29 -2
- package/dist/lib/capacity/collectors/claude-oauth.js +104 -7
- package/dist/lib/capacity/collectors/codex.js +10 -7
- package/dist/lib/capacity/ledger.d.ts +37 -1
- package/dist/lib/capacity/ledger.js +207 -30
- package/dist/lib/capacity/project.d.ts +26 -0
- package/dist/lib/capacity/project.js +137 -8
- package/dist/lib/capacity/refusal-rows.d.ts +31 -0
- package/dist/lib/capacity/refusal-rows.js +61 -0
- package/dist/lib/capacity/refusal.d.ts +53 -2
- package/dist/lib/capacity/refusal.js +284 -65
- package/dist/lib/capacity/route.js +46 -11
- package/dist/lib/capacity/widget.d.ts +12 -1
- package/dist/lib/capacity/widget.js +91 -3
- package/dist/lib/daemon/resolver.d.ts +2 -0
- package/dist/lib/daemon/resolver.js +3 -0
- package/dist/lib/daemon/run.d.ts +9 -0
- package/dist/lib/daemon/run.js +54 -1
- package/dist/lib/daemon/server.d.ts +2 -0
- package/dist/lib/daemon/server.js +10 -1
- package/dist/lib/daemon/tcc-eperm.d.ts +153 -0
- package/dist/lib/daemon/tcc-eperm.js +310 -0
- package/dist/lib/delivery/admission.d.ts +86 -0
- package/dist/lib/delivery/admission.js +148 -0
- package/dist/lib/delivery/artifacts.d.ts +7 -1
- package/dist/lib/delivery/artifacts.js +29 -10
- package/dist/lib/delivery/attempt-id.d.ts +13 -0
- package/dist/lib/delivery/attempt-id.js +16 -0
- package/dist/lib/delivery/disposable-target.d.ts +42 -0
- package/dist/lib/delivery/disposable-target.js +111 -0
- package/dist/lib/delivery/effects.d.ts +136 -0
- package/dist/lib/delivery/effects.js +541 -0
- package/dist/lib/delivery/enrollment.d.ts +21 -17
- package/dist/lib/delivery/enrollment.js +83 -5
- package/dist/lib/delivery/escalations.d.ts +51 -0
- package/dist/lib/delivery/escalations.js +60 -0
- package/dist/lib/delivery/lease.d.ts +80 -0
- package/dist/lib/delivery/lease.js +134 -0
- package/dist/lib/delivery/migrations.d.ts +120 -4
- package/dist/lib/delivery/migrations.js +215 -15
- package/dist/lib/delivery/mqtt.d.ts +2 -0
- package/dist/lib/delivery/mqtt.js +7 -0
- package/dist/lib/delivery/reconciler.d.ts +65 -1
- package/dist/lib/delivery/reconciler.js +224 -13
- package/dist/lib/delivery/rpc/client.d.ts +22 -0
- package/dist/lib/delivery/rpc/client.js +159 -0
- package/dist/lib/delivery/rpc/port.d.ts +40 -0
- package/dist/lib/delivery/rpc/port.js +16 -0
- package/dist/lib/delivery/rpc/server.d.ts +30 -0
- package/dist/lib/delivery/rpc/server.js +205 -0
- package/dist/lib/delivery/rpc/wire.d.ts +85 -0
- package/dist/lib/delivery/rpc/wire.js +143 -0
- package/dist/lib/delivery/store.d.ts +2 -0
- package/dist/lib/delivery/store.js +4 -0
- package/dist/lib/delivery/windows-service-install.d.ts +262 -0
- package/dist/lib/delivery/windows-service-install.js +892 -0
- package/dist/lib/delivery/workers/macos.d.ts +16 -1
- package/dist/lib/delivery/workers/macos.js +54 -12
- package/dist/lib/delivery/workers/windows-service-live.d.ts +121 -0
- package/dist/lib/delivery/workers/windows-service-live.js +711 -0
- package/dist/lib/delivery/workers/windows-service-runner.d.ts +33 -0
- package/dist/lib/delivery/workers/windows-service-runner.js +82 -0
- package/dist/lib/delivery/workers/windows-service.d.ts +140 -0
- package/dist/lib/delivery/workers/windows-service.js +584 -0
- package/dist/lib/delivery/workers/windows.d.ts +32 -22
- package/dist/lib/delivery/workers/windows.js +50 -64
- package/dist/lib/hot-channels/adapters/meetings.d.ts +15 -0
- package/dist/lib/hot-channels/adapters/meetings.js +36 -3
- package/dist/lib/hot-channels/signals-pass.d.ts +10 -0
- package/dist/lib/hot-channels/signals-pass.js +71 -2
- package/dist/lib/hot-channels/signals-redo.js +10 -2
- package/dist/lib/lanes/watcher.js +35 -0
- package/dist/lib/signals/steps/extract.js +40 -4
- package/dist/schemas/hq-available-ai.v1.schema.json +76 -0
- package/dist/utils/catalog-runtime-paths.d.ts +34 -0
- package/dist/utils/catalog-runtime-paths.js +53 -0
- package/dist/utils/files-browse-acl.js +1 -1
- package/package.json +7 -7
- package/dist/lib/delivery/workers/windows-boundary.d.ts +0 -453
- package/dist/lib/delivery/workers/windows-boundary.js +0 -693
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The controller side of the worker RPC (US-011 slice 4 B). See wire.ts for
|
|
3
|
+
* the format.
|
|
4
|
+
*
|
|
5
|
+
* - Binds loopback only by default: "localhost" (the default) listens on
|
|
6
|
+
* 127.0.0.1 and ::1 on the same port, and works with one family when the
|
|
7
|
+
* other is not available. Any other host is refused with RPC_NOT_LOOPBACK
|
|
8
|
+
* unless allowNonLoopback is true.
|
|
9
|
+
* - One call per connection. A request line over MAX_REQUEST_BYTES is answered
|
|
10
|
+
* RPC_TOO_LARGE; a connection that has not sent a complete line within
|
|
11
|
+
* deadlineMs is closed.
|
|
12
|
+
* - Only the RPC_METHODS allowlist is served. Each call hands the presented
|
|
13
|
+
* identity to DeliveryAccess, which authenticates it again inside the
|
|
14
|
+
* commit, so revocation, tenant and trust checks are exactly those of an
|
|
15
|
+
* in-process call.
|
|
16
|
+
*/
|
|
17
|
+
import { createServer } from "node:net";
|
|
18
|
+
import { DEFAULT_MAX_CONNECTIONS, DEFAULT_SERVER_DEADLINE_MS, MAX_REQUEST_BYTES, RPC_WIRE_VERSION, encodeLine, errorBody, isLoopbackHost, notLoopback, parseRequest, redacted, rpcError, } from "./wire.js";
|
|
19
|
+
function text(v, name) {
|
|
20
|
+
if (typeof v !== "string")
|
|
21
|
+
throw rpcError("RPC_BAD_REQUEST", `${name} that is not a string`, `${name} as a string`, "Nothing was run.");
|
|
22
|
+
return v;
|
|
23
|
+
}
|
|
24
|
+
/** Dispatch's credentials are an object with methods and a redacting toJSON; they cross the wire as refs and values. */
|
|
25
|
+
function wireDispatch(d) {
|
|
26
|
+
const values = {};
|
|
27
|
+
for (const ref of d.credentials.refs)
|
|
28
|
+
values[ref] = d.credentials.get(ref);
|
|
29
|
+
return { claim: d.claim, jobClass: d.jobClass, credentials: { refs: [...d.credentials.refs], values } };
|
|
30
|
+
}
|
|
31
|
+
const HANDLERS = {
|
|
32
|
+
readRun: (a, id, [runId]) => a.readRun(id, text(runId, "runId")),
|
|
33
|
+
readyWork: (a, id, [page]) => a.readyWork(id, page),
|
|
34
|
+
verifyNotification: (a, id, [event]) => a.verifyNotification(id, event),
|
|
35
|
+
claim: (a, id, [jobId, opts]) => a.claim(id, text(jobId, "jobId"), (opts ?? {})),
|
|
36
|
+
dispatch: (a, id, [jobId, request]) => wireDispatch(a.dispatch(id, text(jobId, "jobId"), request)),
|
|
37
|
+
recordOutcome: (a, id, [attemptId, outcome]) => {
|
|
38
|
+
if (outcome !== "passed" && outcome !== "failed")
|
|
39
|
+
throw rpcError("RPC_BAD_REQUEST", "an outcome that is not passed or failed", "passed or failed", "Nothing was run.");
|
|
40
|
+
return a.recordOutcome(id, text(attemptId, "attemptId"), outcome);
|
|
41
|
+
},
|
|
42
|
+
returnAttempt: (a, id, [attemptId]) => a.returnAttempt(id, text(attemptId, "attemptId")),
|
|
43
|
+
renewLease: (a, id, [attemptId]) => a.renewLease(id, text(attemptId, "attemptId")),
|
|
44
|
+
};
|
|
45
|
+
/** Address families that may be missing on a host; binding them is skipped when another family bound. */
|
|
46
|
+
const FAMILY_UNAVAILABLE = new Set(["EADDRNOTAVAIL", "EAFNOSUPPORT", "EINVAL", "ENOTFOUND"]);
|
|
47
|
+
function listen(server, port, host) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const onError = (err) => reject(err);
|
|
50
|
+
server.once("error", onError);
|
|
51
|
+
server.listen({ port, host, exclusive: true }, () => {
|
|
52
|
+
server.off("error", onError);
|
|
53
|
+
const addr = server.address();
|
|
54
|
+
resolve(addr && typeof addr === "object" ? addr.port : port);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function closeServer(server) {
|
|
59
|
+
return new Promise((resolve) => server.close(() => resolve()));
|
|
60
|
+
}
|
|
61
|
+
export async function startWorkerRpcServer(opts) {
|
|
62
|
+
const host = opts.host ?? "localhost";
|
|
63
|
+
if (!isLoopbackHost(host) && opts.allowNonLoopback !== true)
|
|
64
|
+
throw notLoopback(host, "bind");
|
|
65
|
+
const deadlineMs = opts.deadlineMs ?? DEFAULT_SERVER_DEADLINE_MS;
|
|
66
|
+
if (!Number.isSafeInteger(deadlineMs) || deadlineMs < 1)
|
|
67
|
+
throw rpcError("RPC_BAD_REQUEST", `deadlineMs ${String(opts.deadlineMs)}`, "a positive integer of ms", "Nothing was opened.");
|
|
68
|
+
const maxConnections = opts.maxConnections ?? DEFAULT_MAX_CONNECTIONS;
|
|
69
|
+
if (!Number.isSafeInteger(maxConnections) || maxConnections < 1)
|
|
70
|
+
throw rpcError("RPC_BAD_REQUEST", `maxConnections ${String(opts.maxConnections)}`, "a positive integer", "Nothing was opened.");
|
|
71
|
+
const access = opts.access;
|
|
72
|
+
const received = new Map();
|
|
73
|
+
const sockets = new Set();
|
|
74
|
+
const onConnection = (sock) => {
|
|
75
|
+
if (sockets.size >= maxConnections) {
|
|
76
|
+
// Answer and close at once, so a flood of idle connections cannot hold descriptors past the limit.
|
|
77
|
+
sock.on("error", () => sock.destroy());
|
|
78
|
+
const body = errorBody(rpcError("RPC_BUSY", `${maxConnections} connections already open`, "a free connection slot", "Retry shortly. Nothing was run."));
|
|
79
|
+
sock.end(encodeLine({ v: RPC_WIRE_VERSION, id: "", ok: false, error: body }));
|
|
80
|
+
setTimeout(() => sock.destroy(), 250).unref();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
sockets.add(sock);
|
|
84
|
+
sock.setNoDelay(true);
|
|
85
|
+
const deadline = setTimeout(() => sock.destroy(), deadlineMs);
|
|
86
|
+
deadline.unref();
|
|
87
|
+
sock.on("close", () => {
|
|
88
|
+
clearTimeout(deadline);
|
|
89
|
+
sockets.delete(sock);
|
|
90
|
+
});
|
|
91
|
+
sock.on("error", () => sock.destroy());
|
|
92
|
+
const chunks = [];
|
|
93
|
+
let size = 0;
|
|
94
|
+
let answered = false;
|
|
95
|
+
const answer = (line, linger = false) => {
|
|
96
|
+
answered = true;
|
|
97
|
+
sock.removeListener("data", onData);
|
|
98
|
+
if (linger) {
|
|
99
|
+
// Discard the rest of an oversize request, then close, so the caller reads the refusal instead of a reset.
|
|
100
|
+
sock.resume();
|
|
101
|
+
sock.end(line);
|
|
102
|
+
setTimeout(() => sock.destroy(), 250).unref();
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
sock.end(line);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const onData = (b) => {
|
|
109
|
+
if (answered)
|
|
110
|
+
return;
|
|
111
|
+
const nl = b.indexOf(10);
|
|
112
|
+
const lineBytes = size + (nl < 0 ? b.length : nl + 1);
|
|
113
|
+
if (lineBytes > MAX_REQUEST_BYTES) {
|
|
114
|
+
const body = errorBody(rpcError("RPC_TOO_LARGE", `a request over ${MAX_REQUEST_BYTES} bytes`, `at most ${MAX_REQUEST_BYTES} bytes per request`, "Send smaller arguments. Nothing was run."));
|
|
115
|
+
answer(encodeLine({ v: RPC_WIRE_VERSION, id: "", ok: false, error: body }), true);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (nl < 0) {
|
|
119
|
+
chunks.push(b);
|
|
120
|
+
size += b.length;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
chunks.push(b.subarray(0, nl));
|
|
124
|
+
const line = Buffer.concat(chunks).toString("utf8");
|
|
125
|
+
let id = "";
|
|
126
|
+
let reply;
|
|
127
|
+
try {
|
|
128
|
+
const req = parseRequest(line);
|
|
129
|
+
id = req.id;
|
|
130
|
+
received.set(req.method, (received.get(req.method) ?? 0) + 1);
|
|
131
|
+
const value = HANDLERS[req.method](access, req.identity, req.args);
|
|
132
|
+
reply = { v: RPC_WIRE_VERSION, id, ok: true, value: value ?? null };
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
if (redacted(err)) {
|
|
136
|
+
try {
|
|
137
|
+
opts.onError?.(err);
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
// A failing log hook must not change the answer.
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
reply = { v: RPC_WIRE_VERSION, id, ok: false, error: errorBody(err) };
|
|
144
|
+
}
|
|
145
|
+
answer(encodeLine(reply));
|
|
146
|
+
};
|
|
147
|
+
sock.on("data", onData);
|
|
148
|
+
};
|
|
149
|
+
const binds = host === "localhost" ? ["127.0.0.1", "::1"] : [host];
|
|
150
|
+
const servers = [];
|
|
151
|
+
const addresses = [];
|
|
152
|
+
let port = opts.port ?? 0;
|
|
153
|
+
for (let tries = 0;; tries += 1) {
|
|
154
|
+
try {
|
|
155
|
+
for (const h of binds) {
|
|
156
|
+
const server = createServer(onConnection);
|
|
157
|
+
try {
|
|
158
|
+
port = await listen(server, port, h);
|
|
159
|
+
servers.push(server);
|
|
160
|
+
addresses.push(h);
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
const code = err.code ?? "";
|
|
164
|
+
// A family the host lacks is skipped once another family is bound.
|
|
165
|
+
if (servers.length > 0 && FAMILY_UNAVAILABLE.has(code))
|
|
166
|
+
continue;
|
|
167
|
+
throw err;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
catch (err) {
|
|
173
|
+
for (const s of servers.splice(0))
|
|
174
|
+
await closeServer(s);
|
|
175
|
+
addresses.length = 0;
|
|
176
|
+
// With a free port chosen by the OS, the second family can collide with a port in use there: pick again.
|
|
177
|
+
const code = err.code ?? "";
|
|
178
|
+
if (opts.port === undefined && code === "EADDRINUSE" && tries < 5) {
|
|
179
|
+
port = 0;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
throw err;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
let closing = null;
|
|
186
|
+
return {
|
|
187
|
+
host,
|
|
188
|
+
port,
|
|
189
|
+
addresses: [...addresses],
|
|
190
|
+
calls: (method) => (method === undefined ? [...received.values()].reduce((a, b) => a + b, 0) : (received.get(method) ?? 0)),
|
|
191
|
+
unref: () => {
|
|
192
|
+
for (const s of servers)
|
|
193
|
+
s.unref();
|
|
194
|
+
},
|
|
195
|
+
close() {
|
|
196
|
+
closing ??= (async () => {
|
|
197
|
+
for (const s of sockets)
|
|
198
|
+
s.destroy();
|
|
199
|
+
await Promise.all(servers.map(closeServer));
|
|
200
|
+
})();
|
|
201
|
+
return closing;
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire format of the worker-to-controller RPC (US-011 slice 4 B).
|
|
3
|
+
*
|
|
4
|
+
* One TCP connection per call. The worker writes one request line and the
|
|
5
|
+
* controller answers one line, then closes:
|
|
6
|
+
*
|
|
7
|
+
* request {"v":1,"id":"<call id>","method":"<allowlisted>","identity":{...},"args":[...]}
|
|
8
|
+
* answer {"v":1,"id":"<call id>","ok":true,"value":...}
|
|
9
|
+
* {"v":1,"id":"<call id>","ok":false,"error":{"code","found","expected","action"}}
|
|
10
|
+
*
|
|
11
|
+
* The identity travels with every call and the controller hands it to
|
|
12
|
+
* DeliveryAccess, which authenticates it again inside the commit. A refusal
|
|
13
|
+
* keeps the DeliveryStoreError fields and the worker rebuilds it as one.
|
|
14
|
+
* Requests are capped at MAX_REQUEST_BYTES; answers at MAX_ANSWER_BYTES.
|
|
15
|
+
* This is not the MQTT hint channel: the broker is not an authentication
|
|
16
|
+
* boundary, and every call here is answered from the controller's ledger.
|
|
17
|
+
*/
|
|
18
|
+
import { DeliveryStoreError, type DeliveryStoreErrorCode } from "../migrations.js";
|
|
19
|
+
export declare const RPC_WIRE_VERSION = 1;
|
|
20
|
+
/**
|
|
21
|
+
* The only methods a worker may call over the wire. No admin-scoped method
|
|
22
|
+
* (enrollment, revocation, worker listing), no deploy effect (request, publish,
|
|
23
|
+
* reconcile) and nothing on the controller itself.
|
|
24
|
+
*/
|
|
25
|
+
export declare const RPC_METHODS: readonly ["readRun", "readyWork", "verifyNotification", "claim", "dispatch", "recordOutcome", "returnAttempt", "renewLease"];
|
|
26
|
+
export type RpcMethod = (typeof RPC_METHODS)[number];
|
|
27
|
+
export declare function isRpcMethod(m: unknown): m is RpcMethod;
|
|
28
|
+
/** Largest request line, newline included. */
|
|
29
|
+
export declare const MAX_REQUEST_BYTES: number;
|
|
30
|
+
/** Largest answer line the client reads (a readRun of a large run). */
|
|
31
|
+
export declare const MAX_ANSWER_BYTES: number;
|
|
32
|
+
/** The client's deadline for one call, connect to answer. */
|
|
33
|
+
export declare const DEFAULT_RPC_TIMEOUT_MS = 10000;
|
|
34
|
+
/** The server's deadline for receiving a complete request line on a connection. */
|
|
35
|
+
export declare const DEFAULT_SERVER_DEADLINE_MS = 10000;
|
|
36
|
+
/** The longest call id accepted. */
|
|
37
|
+
export declare const MAX_CALL_ID = 128;
|
|
38
|
+
/** Open connections a server holds at once; past this a connection is answered RPC_BUSY and closed. */
|
|
39
|
+
export declare const DEFAULT_MAX_CONNECTIONS = 256;
|
|
40
|
+
export interface RpcRequest {
|
|
41
|
+
v: typeof RPC_WIRE_VERSION;
|
|
42
|
+
id: string;
|
|
43
|
+
method: RpcMethod;
|
|
44
|
+
identity: unknown;
|
|
45
|
+
args: unknown[];
|
|
46
|
+
}
|
|
47
|
+
export interface RpcErrorBody {
|
|
48
|
+
code: string;
|
|
49
|
+
found: string;
|
|
50
|
+
expected: string;
|
|
51
|
+
action: string;
|
|
52
|
+
}
|
|
53
|
+
export type RpcAnswer = {
|
|
54
|
+
v: typeof RPC_WIRE_VERSION;
|
|
55
|
+
id: string;
|
|
56
|
+
ok: true;
|
|
57
|
+
value: unknown;
|
|
58
|
+
} | {
|
|
59
|
+
v: typeof RPC_WIRE_VERSION;
|
|
60
|
+
id: string;
|
|
61
|
+
ok: false;
|
|
62
|
+
error: RpcErrorBody;
|
|
63
|
+
};
|
|
64
|
+
export declare function rpcError(code: DeliveryStoreErrorCode, found: string, expected: string, action: string): DeliveryStoreError;
|
|
65
|
+
/** True when the full error should go to the controller's log because the wire carries less. */
|
|
66
|
+
export declare function redacted(err: unknown): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* The wire body of a refusal. A thrown value that is not a DeliveryStoreError
|
|
69
|
+
* is reported without its message or stack, and a store-internal refusal keeps
|
|
70
|
+
* only its code.
|
|
71
|
+
*/
|
|
72
|
+
export declare function errorBody(err: unknown): RpcErrorBody;
|
|
73
|
+
/** Rebuild a refusal from the wire. A malformed body becomes RPC_BAD_ANSWER. */
|
|
74
|
+
export declare function errorFromBody(body: unknown, method: string): DeliveryStoreError;
|
|
75
|
+
/** Parse and check one request line. Throws RPC_BAD_REQUEST or RPC_UNKNOWN_METHOD. */
|
|
76
|
+
export declare function parseRequest(line: string): RpcRequest;
|
|
77
|
+
export declare function encodeLine(value: unknown): string;
|
|
78
|
+
/** Hosts the server binds without allowNonLoopback. "localhost" binds the literals 127.0.0.1 and ::1, never a name lookup. */
|
|
79
|
+
export declare const LOOPBACK_HOSTS: readonly ["127.0.0.1", "::1", "localhost"];
|
|
80
|
+
/** Hosts the client dials without allowNonLoopback: literals only, since "localhost" goes through name resolution. */
|
|
81
|
+
export declare const LOOPBACK_DIAL_HOSTS: readonly ["127.0.0.1", "::1"];
|
|
82
|
+
export declare function isLoopbackHost(host: string): boolean;
|
|
83
|
+
export declare function isLoopbackDialHost(host: string): boolean;
|
|
84
|
+
export declare function notLoopback(host: string, side: "bind" | "dial"): DeliveryStoreError;
|
|
85
|
+
//# sourceMappingURL=wire.d.ts.map
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire format of the worker-to-controller RPC (US-011 slice 4 B).
|
|
3
|
+
*
|
|
4
|
+
* One TCP connection per call. The worker writes one request line and the
|
|
5
|
+
* controller answers one line, then closes:
|
|
6
|
+
*
|
|
7
|
+
* request {"v":1,"id":"<call id>","method":"<allowlisted>","identity":{...},"args":[...]}
|
|
8
|
+
* answer {"v":1,"id":"<call id>","ok":true,"value":...}
|
|
9
|
+
* {"v":1,"id":"<call id>","ok":false,"error":{"code","found","expected","action"}}
|
|
10
|
+
*
|
|
11
|
+
* The identity travels with every call and the controller hands it to
|
|
12
|
+
* DeliveryAccess, which authenticates it again inside the commit. A refusal
|
|
13
|
+
* keeps the DeliveryStoreError fields and the worker rebuilds it as one.
|
|
14
|
+
* Requests are capped at MAX_REQUEST_BYTES; answers at MAX_ANSWER_BYTES.
|
|
15
|
+
* This is not the MQTT hint channel: the broker is not an authentication
|
|
16
|
+
* boundary, and every call here is answered from the controller's ledger.
|
|
17
|
+
*/
|
|
18
|
+
import { DeliveryStoreError } from "../migrations.js";
|
|
19
|
+
export const RPC_WIRE_VERSION = 1;
|
|
20
|
+
/**
|
|
21
|
+
* The only methods a worker may call over the wire. No admin-scoped method
|
|
22
|
+
* (enrollment, revocation, worker listing), no deploy effect (request, publish,
|
|
23
|
+
* reconcile) and nothing on the controller itself.
|
|
24
|
+
*/
|
|
25
|
+
export const RPC_METHODS = ["readRun", "readyWork", "verifyNotification", "claim", "dispatch", "recordOutcome", "returnAttempt", "renewLease"];
|
|
26
|
+
export function isRpcMethod(m) {
|
|
27
|
+
return typeof m === "string" && RPC_METHODS.includes(m);
|
|
28
|
+
}
|
|
29
|
+
/** Largest request line, newline included. */
|
|
30
|
+
export const MAX_REQUEST_BYTES = 64 * 1024;
|
|
31
|
+
/** Largest answer line the client reads (a readRun of a large run). */
|
|
32
|
+
export const MAX_ANSWER_BYTES = 8 * 1024 * 1024;
|
|
33
|
+
/** The client's deadline for one call, connect to answer. */
|
|
34
|
+
export const DEFAULT_RPC_TIMEOUT_MS = 10_000;
|
|
35
|
+
/** The server's deadline for receiving a complete request line on a connection. */
|
|
36
|
+
export const DEFAULT_SERVER_DEADLINE_MS = 10_000;
|
|
37
|
+
/** The longest call id accepted. */
|
|
38
|
+
export const MAX_CALL_ID = 128;
|
|
39
|
+
/** Open connections a server holds at once; past this a connection is answered RPC_BUSY and closed. */
|
|
40
|
+
export const DEFAULT_MAX_CONNECTIONS = 256;
|
|
41
|
+
export function rpcError(code, found, expected, action) {
|
|
42
|
+
return new DeliveryStoreError(code, { found, expected, action });
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Refusals about the controller's own store. Their text names controller paths
|
|
46
|
+
* and internals, so only the code crosses the wire (the heartbeat and the
|
|
47
|
+
* worker act on the code); the detail stays in the controller's log.
|
|
48
|
+
*/
|
|
49
|
+
const STORE_INTERNAL = new Set([
|
|
50
|
+
"STORE_CORRUPT",
|
|
51
|
+
"STORE_SCHEMA_NEWER",
|
|
52
|
+
"STORE_SCHEMA_UNKNOWN",
|
|
53
|
+
"STORE_INVALID_RECORD",
|
|
54
|
+
"CONTROLLER_OWNED",
|
|
55
|
+
"CONTROLLER_AMBIGUOUS",
|
|
56
|
+
"FENCED",
|
|
57
|
+
"STORE_CLOSED",
|
|
58
|
+
"STORE_IO",
|
|
59
|
+
"BACKUP_INVALID",
|
|
60
|
+
"RESTORE_PENDING",
|
|
61
|
+
"STORE_RETIRED",
|
|
62
|
+
]);
|
|
63
|
+
/** True when the full error should go to the controller's log because the wire carries less. */
|
|
64
|
+
export function redacted(err) {
|
|
65
|
+
return !(err instanceof DeliveryStoreError) || STORE_INTERNAL.has(err.code);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The wire body of a refusal. A thrown value that is not a DeliveryStoreError
|
|
69
|
+
* is reported without its message or stack, and a store-internal refusal keeps
|
|
70
|
+
* only its code.
|
|
71
|
+
*/
|
|
72
|
+
export function errorBody(err) {
|
|
73
|
+
if (err instanceof DeliveryStoreError && STORE_INTERNAL.has(err.code)) {
|
|
74
|
+
return {
|
|
75
|
+
code: err.code,
|
|
76
|
+
found: `the controller could not serve the call (${err.code})`,
|
|
77
|
+
expected: "a controller that owns a working store",
|
|
78
|
+
action: "Retry later. The controller's operator can see the detail in its log.",
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (err instanceof DeliveryStoreError)
|
|
82
|
+
return { code: err.code, found: err.found, expected: err.expected, action: err.action };
|
|
83
|
+
return {
|
|
84
|
+
code: "RPC_SERVER_ERROR",
|
|
85
|
+
found: "the controller failed while handling the call",
|
|
86
|
+
expected: "an answer or a delivery refusal",
|
|
87
|
+
action: "Retry; if it persists, check the controller's log. The call may or may not have been applied.",
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const CODE = /^[A-Z][A-Z0-9_]{1,63}$/;
|
|
91
|
+
/** Rebuild a refusal from the wire. A malformed body becomes RPC_BAD_ANSWER. */
|
|
92
|
+
export function errorFromBody(body, method) {
|
|
93
|
+
const b = body;
|
|
94
|
+
if (b && typeof b === "object" && typeof b.code === "string" && CODE.test(b.code) && typeof b.found === "string" && typeof b.expected === "string" && typeof b.action === "string") {
|
|
95
|
+
return new DeliveryStoreError(b.code, { found: b.found, expected: b.expected, action: b.action });
|
|
96
|
+
}
|
|
97
|
+
return rpcError("RPC_BAD_ANSWER", `a malformed refusal for ${method}`, "code, found, expected and action as strings", "Check that the controller runs a matching HQ version.");
|
|
98
|
+
}
|
|
99
|
+
/** Parse and check one request line. Throws RPC_BAD_REQUEST or RPC_UNKNOWN_METHOD. */
|
|
100
|
+
export function parseRequest(line) {
|
|
101
|
+
let raw;
|
|
102
|
+
try {
|
|
103
|
+
raw = JSON.parse(line);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
throw rpcError("RPC_BAD_REQUEST", "a request line that is not JSON", "one JSON object per line", "Send {v, id, method, identity, args}. Nothing was run.");
|
|
107
|
+
}
|
|
108
|
+
const r = raw;
|
|
109
|
+
if (!r || typeof r !== "object" || Array.isArray(r)) {
|
|
110
|
+
throw rpcError("RPC_BAD_REQUEST", "a request that is not a JSON object", "{v, id, method, identity, args}", "Nothing was run.");
|
|
111
|
+
}
|
|
112
|
+
if (r.v !== RPC_WIRE_VERSION)
|
|
113
|
+
throw rpcError("RPC_BAD_REQUEST", `wire version ${JSON.stringify(r.v)}`, `version ${RPC_WIRE_VERSION}`, "Use a worker and controller of the same HQ version. Nothing was run.");
|
|
114
|
+
if (typeof r.id !== "string" || r.id === "" || r.id.length > MAX_CALL_ID)
|
|
115
|
+
throw rpcError("RPC_BAD_REQUEST", "a missing or overlong call id", `a call id of 1 to ${MAX_CALL_ID} characters`, "Nothing was run.");
|
|
116
|
+
if (!Array.isArray(r.args))
|
|
117
|
+
throw rpcError("RPC_BAD_REQUEST", "args that are not a list", "args as a JSON array", "Nothing was run.");
|
|
118
|
+
if (!r.identity || typeof r.identity !== "object" || Array.isArray(r.identity))
|
|
119
|
+
throw rpcError("RPC_BAD_REQUEST", "a missing identity", "the worker's enrollment identity", "Nothing was run.");
|
|
120
|
+
// Checked against the allowlist only; the name is never looked up on an object.
|
|
121
|
+
if (!isRpcMethod(r.method)) {
|
|
122
|
+
throw rpcError("RPC_UNKNOWN_METHOD", "a method that is not on the worker allowlist", RPC_METHODS.join(", "), "Workers may call only these methods over the wire. Nothing was run.");
|
|
123
|
+
}
|
|
124
|
+
return { v: RPC_WIRE_VERSION, id: r.id, method: r.method, identity: r.identity, args: r.args };
|
|
125
|
+
}
|
|
126
|
+
export function encodeLine(value) {
|
|
127
|
+
return `${JSON.stringify(value)}\n`;
|
|
128
|
+
}
|
|
129
|
+
/** Hosts the server binds without allowNonLoopback. "localhost" binds the literals 127.0.0.1 and ::1, never a name lookup. */
|
|
130
|
+
export const LOOPBACK_HOSTS = ["127.0.0.1", "::1", "localhost"];
|
|
131
|
+
/** Hosts the client dials without allowNonLoopback: literals only, since "localhost" goes through name resolution. */
|
|
132
|
+
export const LOOPBACK_DIAL_HOSTS = ["127.0.0.1", "::1"];
|
|
133
|
+
export function isLoopbackHost(host) {
|
|
134
|
+
return LOOPBACK_HOSTS.includes(host);
|
|
135
|
+
}
|
|
136
|
+
export function isLoopbackDialHost(host) {
|
|
137
|
+
return LOOPBACK_DIAL_HOSTS.includes(host);
|
|
138
|
+
}
|
|
139
|
+
export function notLoopback(host, side) {
|
|
140
|
+
const allowed = side === "bind" ? LOOPBACK_HOSTS : LOOPBACK_DIAL_HOSTS;
|
|
141
|
+
return rpcError("RPC_NOT_LOOPBACK", `a request to ${side} ${JSON.stringify(host)}`, `a loopback host (${allowed.join(", ")}), or allowNonLoopback: true`, "The worker RPC carries the enrollment token in clear. Keep it on loopback (use an SSH-forwarded port to reach another machine), or pass allowNonLoopback explicitly. Nothing was opened.");
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=wire.js.map
|
|
@@ -122,6 +122,8 @@ export declare class DeliveryStore {
|
|
|
122
122
|
static open(opts: StoreOpenOptions): DeliveryStore;
|
|
123
123
|
/** The committed state. Read only: change it through commit(). */
|
|
124
124
|
get state(): StoreState;
|
|
125
|
+
/** Milliseconds from the injected clock: the clock commits are stamped with. */
|
|
126
|
+
nowMs(): number;
|
|
125
127
|
/** ISO timestamp from the injected clock. */
|
|
126
128
|
now(): string;
|
|
127
129
|
/**
|
|
@@ -766,6 +766,10 @@ export class DeliveryStore {
|
|
|
766
766
|
get state() {
|
|
767
767
|
return this.current;
|
|
768
768
|
}
|
|
769
|
+
/** Milliseconds from the injected clock: the clock commits are stamped with. */
|
|
770
|
+
nowMs() {
|
|
771
|
+
return this.clock();
|
|
772
|
+
}
|
|
769
773
|
/** ISO timestamp from the injected clock. */
|
|
770
774
|
now() {
|
|
771
775
|
return new Date(this.clock()).toISOString();
|