ruicode 0.105.5 → 0.105.6
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/dist/client.js +72 -42
- package/dist/transport.d.ts +6 -0
- package/dist/transport.js +80 -14
- package/package.json +4 -4
package/dist/client.js
CHANGED
|
@@ -13,7 +13,7 @@ import { resolveBinary } from "./binary.js";
|
|
|
13
13
|
import { TRANSPORT_ENVIRONMENT } from "./host-environment.js";
|
|
14
14
|
import { RuiRequestError, RuiStartError, RuiStoreFailedError, RuiUnavailableError } from "./errors.js";
|
|
15
15
|
import { models, reloadTools, Session, Sessions, setPaused } from "./sessions.js";
|
|
16
|
-
import { ConnectionError, post, WIRE_VERSION } from "./transport.js";
|
|
16
|
+
import { ConnectionError, HostFenceReply, post, WIRE_VERSION } from "./transport.js";
|
|
17
17
|
const SOCKET_NAME = "rui.sock";
|
|
18
18
|
const DEFAULT_LOCK_WAIT_MS = 120_000;
|
|
19
19
|
const LOCK_RETRY_MS = 500;
|
|
@@ -40,6 +40,7 @@ export class Rui extends EventEmitter {
|
|
|
40
40
|
#host;
|
|
41
41
|
#child;
|
|
42
42
|
#waiters = [];
|
|
43
|
+
#exchanges = new Set();
|
|
43
44
|
#backoff = FIRST_RESTART_MS;
|
|
44
45
|
#restart;
|
|
45
46
|
#closing;
|
|
@@ -141,48 +142,68 @@ export class Rui extends EventEmitter {
|
|
|
141
142
|
await rm(this.#runtimeDir, { recursive: true, force: true });
|
|
142
143
|
}
|
|
143
144
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
145
|
+
call(path, body, environment, limits) {
|
|
146
|
+
return this.exchange(path, () => body, environment, limits);
|
|
147
|
+
}
|
|
148
|
+
exchange(path, build, environment, limits) {
|
|
149
|
+
if (this.#storeFailed)
|
|
150
|
+
return Promise.reject(this.#storeFailed);
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const cancellation = new AbortController();
|
|
153
|
+
const fail = (error) => {
|
|
154
|
+
if (!this.#exchanges.delete(fail))
|
|
155
|
+
return;
|
|
156
|
+
reject(error);
|
|
157
|
+
cancellation.abort(error);
|
|
158
|
+
};
|
|
159
|
+
this.#exchanges.add(fail);
|
|
160
|
+
void this.#runExchange(path, build, environment, limits, cancellation.signal).then((reply) => {
|
|
161
|
+
if (this.#exchanges.delete(fail))
|
|
162
|
+
resolve(reply);
|
|
163
|
+
}, fail);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
async #runExchange(path, build, environment, limits, signal) {
|
|
167
|
+
let body;
|
|
168
|
+
for (let retried = false;; retried = true) {
|
|
147
169
|
const host = await this.#ready();
|
|
170
|
+
signal.throwIfAborted();
|
|
171
|
+
body ??= build(host.info.store);
|
|
148
172
|
try {
|
|
149
173
|
return await post({
|
|
150
174
|
socketPath: this.#launch.socketPath,
|
|
151
175
|
path,
|
|
152
176
|
body,
|
|
153
177
|
instance: host.instance,
|
|
178
|
+
signal,
|
|
154
179
|
...(environment ? { environment } : {}),
|
|
155
180
|
...(limits ? { limits } : {}),
|
|
156
181
|
});
|
|
157
182
|
}
|
|
158
183
|
catch (error) {
|
|
159
|
-
await this.#retryAfter(host, error, retried);
|
|
160
|
-
retried = true;
|
|
184
|
+
await this.#retryAfter(host, error, retried, signal);
|
|
161
185
|
}
|
|
162
186
|
}
|
|
163
187
|
}
|
|
164
|
-
async #retryAfter(host, error, retried) {
|
|
188
|
+
async #retryAfter(host, error, retried, signal) {
|
|
189
|
+
signal?.throwIfAborted();
|
|
165
190
|
if (this.#storeFailed)
|
|
166
191
|
throw this.#storeFailed;
|
|
167
|
-
if (error instanceof
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
await settle(host);
|
|
192
|
+
if (error instanceof HostFenceReply) {
|
|
193
|
+
if (error.kind === "store")
|
|
194
|
+
throw this.#latchStoreFailed(error);
|
|
195
|
+
await settle(host, signal);
|
|
171
196
|
throw this.#storeFailed ?? error;
|
|
172
197
|
}
|
|
173
198
|
const stopping = error instanceof RuiRequestError && error.code === "host_stopping";
|
|
174
199
|
if (!(error instanceof ConnectionError || stopping))
|
|
175
200
|
throw error;
|
|
176
|
-
const gone = await settle(host);
|
|
201
|
+
const gone = await settle(host, signal);
|
|
177
202
|
if (this.#storeFailed)
|
|
178
203
|
throw this.#storeFailed;
|
|
179
204
|
if (!gone || retried)
|
|
180
205
|
throw new RuiUnavailableError("the Rui Host did not answer", { cause: error });
|
|
181
206
|
}
|
|
182
|
-
async exchange(path, build, environment, limits) {
|
|
183
|
-
const host = await this.#ready();
|
|
184
|
-
return this.call(path, build(host.info.store), environment, limits);
|
|
185
|
-
}
|
|
186
207
|
get pid() {
|
|
187
208
|
return this.#host?.child.pid;
|
|
188
209
|
}
|
|
@@ -190,10 +211,10 @@ export class Rui extends EventEmitter {
|
|
|
190
211
|
return this.#host?.info;
|
|
191
212
|
}
|
|
192
213
|
#ready() {
|
|
193
|
-
if (this.#closing)
|
|
194
|
-
return Promise.reject(new RuiUnavailableError("the Rui client is closed"));
|
|
195
214
|
if (this.#storeFailed)
|
|
196
215
|
return Promise.reject(this.#storeFailed);
|
|
216
|
+
if (this.#closing)
|
|
217
|
+
return Promise.reject(new RuiUnavailableError("the Rui client is closed"));
|
|
197
218
|
if (this.#host)
|
|
198
219
|
return Promise.resolve(this.#host);
|
|
199
220
|
const failed = this.#dead;
|
|
@@ -210,7 +231,7 @@ export class Rui extends EventEmitter {
|
|
|
210
231
|
waiter.resolve(host);
|
|
211
232
|
void host.fenced.then((cause) => this.#latchStoreFailed(cause));
|
|
212
233
|
void host.exited.then(async (exit) => {
|
|
213
|
-
await
|
|
234
|
+
await waitForObservation(host.outputObserved, OUTPUT_GRACE_MS);
|
|
214
235
|
if (this.#closing || this.#host !== host)
|
|
215
236
|
return;
|
|
216
237
|
this.#host = undefined;
|
|
@@ -223,6 +244,8 @@ export class Rui extends EventEmitter {
|
|
|
223
244
|
return this.#storeFailed;
|
|
224
245
|
this.#storeFailed = new RuiStoreFailedError({ cause });
|
|
225
246
|
clearTimeout(this.#restart);
|
|
247
|
+
for (const fail of this.#exchanges)
|
|
248
|
+
fail(this.#storeFailed);
|
|
226
249
|
this.#fail(this.#storeFailed);
|
|
227
250
|
return this.#storeFailed;
|
|
228
251
|
}
|
|
@@ -455,7 +478,10 @@ async function start(launch, spawned) {
|
|
|
455
478
|
child.stdout?.on("data", watch);
|
|
456
479
|
void outputEnded.then(() => fenceIn(pending));
|
|
457
480
|
ready = true;
|
|
458
|
-
return {
|
|
481
|
+
return {
|
|
482
|
+
child, instance: info.instance, info, exited, fenced,
|
|
483
|
+
exitObserved: observe(exited), outputObserved: observe(outputEnded),
|
|
484
|
+
};
|
|
459
485
|
}
|
|
460
486
|
await sleep(READY_POLL_MS);
|
|
461
487
|
}
|
|
@@ -484,32 +510,36 @@ async function start(launch, spawned) {
|
|
|
484
510
|
stdout.length = 0;
|
|
485
511
|
}
|
|
486
512
|
}
|
|
487
|
-
function
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
return error.type === "host_unavailable" && error.code === "dispatch_fenced";
|
|
492
|
-
}
|
|
493
|
-
async function bounded(done, ms, fallback) {
|
|
494
|
-
const cancel = new AbortController();
|
|
495
|
-
try {
|
|
496
|
-
return await Promise.race([done, sleep(ms, fallback, { signal: cancel.signal })]);
|
|
497
|
-
}
|
|
498
|
-
catch {
|
|
499
|
-
return fallback;
|
|
500
|
-
}
|
|
501
|
-
finally {
|
|
502
|
-
cancel.abort();
|
|
503
|
-
}
|
|
513
|
+
function observe(done) {
|
|
514
|
+
const observed = new AbortController();
|
|
515
|
+
void done.then(() => observed.abort());
|
|
516
|
+
return observed.signal;
|
|
504
517
|
}
|
|
505
|
-
async function settle(host) {
|
|
506
|
-
const gone = await
|
|
518
|
+
async function settle(host, signal) {
|
|
519
|
+
const gone = await waitForObservation(host.exitObserved, CLOSE_GRACE_MS, signal);
|
|
507
520
|
if (gone)
|
|
508
|
-
await
|
|
521
|
+
await waitForObservation(host.outputObserved, OUTPUT_GRACE_MS, signal);
|
|
509
522
|
return gone;
|
|
510
523
|
}
|
|
511
|
-
|
|
512
|
-
|
|
524
|
+
function waitForObservation(observed, ms, signal) {
|
|
525
|
+
signal?.throwIfAborted();
|
|
526
|
+
if (observed.aborted)
|
|
527
|
+
return Promise.resolve(true);
|
|
528
|
+
const interruption = signal ? AbortSignal.any([observed, signal]) : observed;
|
|
529
|
+
return new Promise((resolve, reject) => {
|
|
530
|
+
const timer = setTimeout(() => {
|
|
531
|
+
interruption.removeEventListener("abort", wake);
|
|
532
|
+
resolve(false);
|
|
533
|
+
}, ms);
|
|
534
|
+
const wake = () => {
|
|
535
|
+
clearTimeout(timer);
|
|
536
|
+
if (signal?.aborted)
|
|
537
|
+
reject(signal.reason);
|
|
538
|
+
else
|
|
539
|
+
resolve(true);
|
|
540
|
+
};
|
|
541
|
+
interruption.addEventListener("abort", wake, { once: true });
|
|
542
|
+
});
|
|
513
543
|
}
|
|
514
544
|
async function createInterfaceAlias(launch) {
|
|
515
545
|
const dir = launch.interfaceDir;
|
package/dist/transport.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RuiRequestError } from "./errors.ts";
|
|
1
2
|
export declare const WIRE_VERSION = "40";
|
|
2
3
|
export interface Post {
|
|
3
4
|
socketPath: string;
|
|
@@ -6,6 +7,7 @@ export interface Post {
|
|
|
6
7
|
instance?: string;
|
|
7
8
|
environment?: Buffer;
|
|
8
9
|
limits?: ReplyLimits;
|
|
10
|
+
signal?: AbortSignal;
|
|
9
11
|
}
|
|
10
12
|
export interface ReplyLimits {
|
|
11
13
|
prefix?: number;
|
|
@@ -15,5 +17,9 @@ export interface ReplyLimits {
|
|
|
15
17
|
export declare class ConnectionError extends Error {
|
|
16
18
|
readonly name = "ConnectionError";
|
|
17
19
|
}
|
|
20
|
+
export declare class HostFenceReply extends RuiRequestError {
|
|
21
|
+
readonly kind: "store" | "dispatch";
|
|
22
|
+
constructor(kind: HostFenceReply["kind"]);
|
|
23
|
+
}
|
|
18
24
|
export declare function post(call: Post): Promise<Buffer>;
|
|
19
25
|
export declare function refusal(status: number | undefined, reply: Buffer): Error;
|
package/dist/transport.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isUtf8 } from "node:buffer";
|
|
1
2
|
import { request } from "node:http";
|
|
2
3
|
import { RuiRequestError } from "./errors.js";
|
|
3
4
|
export const WIRE_VERSION = "40";
|
|
@@ -6,7 +7,19 @@ const TRANSFER_IDLE_MS = 60_000;
|
|
|
6
7
|
export class ConnectionError extends Error {
|
|
7
8
|
name = "ConnectionError";
|
|
8
9
|
}
|
|
10
|
+
export class HostFenceReply extends RuiRequestError {
|
|
11
|
+
kind;
|
|
12
|
+
constructor(kind) {
|
|
13
|
+
if (kind === "store")
|
|
14
|
+
super("invocation_error", "canonical_store_failure");
|
|
15
|
+
else
|
|
16
|
+
super("host_unavailable", "dispatch_fenced");
|
|
17
|
+
this.kind = kind;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
9
20
|
export function post(call) {
|
|
21
|
+
if (call.signal?.aborted)
|
|
22
|
+
return Promise.reject(call.signal.reason);
|
|
10
23
|
const json = Buffer.from(call.body);
|
|
11
24
|
const payload = call.environment ? Buffer.concat([call.environment, json]) : json;
|
|
12
25
|
const headers = {
|
|
@@ -20,51 +33,70 @@ export function post(call) {
|
|
|
20
33
|
headers["X-Rui-Environment-Bytes"] = String(call.environment.length);
|
|
21
34
|
return new Promise((resolve, reject) => {
|
|
22
35
|
const outgoing = request({ socketPath: call.socketPath, path: call.path, method: "POST", headers, agent: false });
|
|
36
|
+
let incoming;
|
|
37
|
+
const cleanup = () => call.signal?.removeEventListener("abort", abort);
|
|
38
|
+
const succeed = (reply) => { cleanup(); resolve(reply); };
|
|
39
|
+
const fail = (error) => { cleanup(); reject(error); };
|
|
40
|
+
const abort = () => {
|
|
41
|
+
fail(call.signal?.reason);
|
|
42
|
+
incoming?.destroy();
|
|
43
|
+
outgoing.destroy();
|
|
44
|
+
};
|
|
45
|
+
call.signal?.addEventListener("abort", abort, { once: true });
|
|
23
46
|
const idle = call.limits?.idleMs ?? TRANSFER_IDLE_MS;
|
|
24
47
|
outgoing.setTimeout(idle);
|
|
25
48
|
outgoing.on("timeout", () => outgoing.destroy(new Error("the Host made no progress in time")));
|
|
26
|
-
outgoing.on("error", (error) =>
|
|
27
|
-
outgoing.on("response", (
|
|
49
|
+
outgoing.on("error", (error) => fail(new ConnectionError(error.message, { cause: error })));
|
|
50
|
+
outgoing.on("response", (response) => {
|
|
51
|
+
incoming = response;
|
|
28
52
|
outgoing.setTimeout(idle);
|
|
29
53
|
const chunks = [];
|
|
30
54
|
let length = 0;
|
|
31
|
-
|
|
32
|
-
|
|
55
|
+
response.on("data", (chunk) => {
|
|
56
|
+
const prefix = call.limits?.prefix;
|
|
57
|
+
if (prefix !== undefined && response.statusCode === 200) {
|
|
33
58
|
chunks.push(chunk);
|
|
34
59
|
length += chunk.length;
|
|
35
|
-
if (length >=
|
|
36
|
-
|
|
37
|
-
|
|
60
|
+
if (length >= prefix) {
|
|
61
|
+
response.destroy();
|
|
62
|
+
succeed(Buffer.concat(chunks).subarray(0, prefix));
|
|
38
63
|
}
|
|
39
64
|
return;
|
|
40
65
|
}
|
|
41
66
|
length += chunk.length;
|
|
42
67
|
const max = call.limits?.max ?? MAX_REPLY_BYTES;
|
|
43
68
|
if (length > max) {
|
|
44
|
-
|
|
69
|
+
response.destroy(new Error(`the reply exceeds ${max} bytes`));
|
|
45
70
|
return;
|
|
46
71
|
}
|
|
47
72
|
chunks.push(chunk);
|
|
48
73
|
});
|
|
49
|
-
|
|
50
|
-
|
|
74
|
+
response.on("error", (error) => fail(new ConnectionError(error.message, { cause: error })));
|
|
75
|
+
response.on("end", () => {
|
|
51
76
|
const reply = Buffer.concat(chunks);
|
|
52
|
-
if (
|
|
53
|
-
return
|
|
54
|
-
|
|
77
|
+
if (response.statusCode === 200)
|
|
78
|
+
return succeed(reply);
|
|
79
|
+
fail(refusal(response.statusCode, reply));
|
|
55
80
|
});
|
|
56
81
|
});
|
|
57
82
|
outgoing.end(payload);
|
|
58
83
|
});
|
|
59
84
|
}
|
|
60
85
|
export function refusal(status, reply) {
|
|
86
|
+
const text = reply.toString("utf8");
|
|
61
87
|
let parsed;
|
|
62
88
|
try {
|
|
63
|
-
parsed = JSON.parse(
|
|
89
|
+
parsed = JSON.parse(text);
|
|
64
90
|
}
|
|
65
91
|
catch {
|
|
66
92
|
}
|
|
67
93
|
if (parsed && typeof parsed.code === "string" && typeof parsed.type === "string") {
|
|
94
|
+
if (parsed.version === WIRE_VERSION) {
|
|
95
|
+
const store = status === 500 && parsed.type === "invocation_error" && parsed.code === "canonical_store_failure";
|
|
96
|
+
const dispatch = status === 503 && parsed.type === "host_unavailable" && parsed.code === "dispatch_fenced";
|
|
97
|
+
if ((store || dispatch) && isUtf8(reply) && hasUniqueFenceFields(text))
|
|
98
|
+
return new HostFenceReply(store ? "store" : "dispatch");
|
|
99
|
+
}
|
|
68
100
|
return new RuiRequestError(parsed.type, parsed.code);
|
|
69
101
|
}
|
|
70
102
|
if (parsed?.answer && typeof parsed.answer.code === "string" && typeof parsed.answer.status === "string") {
|
|
@@ -72,3 +104,37 @@ export function refusal(status, reply) {
|
|
|
72
104
|
}
|
|
73
105
|
return new RuiRequestError("invocation_error", `http_${status ?? "unknown"}`);
|
|
74
106
|
}
|
|
107
|
+
function hasUniqueFenceFields(json) {
|
|
108
|
+
// JSON.parse validates syntax but has already discarded duplicate object keys.
|
|
109
|
+
const punctuation = /[{}\[\]"\\]/g;
|
|
110
|
+
const colon = /\s*:/y;
|
|
111
|
+
const seen = new Set();
|
|
112
|
+
let depth = 0;
|
|
113
|
+
let quote = -1;
|
|
114
|
+
for (let token; (token = punctuation.exec(json)) !== null;) {
|
|
115
|
+
const char = token[0];
|
|
116
|
+
if (quote !== -1) {
|
|
117
|
+
if (char === "\\")
|
|
118
|
+
punctuation.lastIndex++;
|
|
119
|
+
if (char !== '"')
|
|
120
|
+
continue;
|
|
121
|
+
colon.lastIndex = punctuation.lastIndex;
|
|
122
|
+
if (depth === 1 && colon.test(json)) {
|
|
123
|
+
const key = JSON.parse(json.slice(quote, punctuation.lastIndex));
|
|
124
|
+
if (key === "version" || key === "type" || key === "code") {
|
|
125
|
+
if (seen.has(key))
|
|
126
|
+
return false;
|
|
127
|
+
seen.add(key);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
quote = -1;
|
|
131
|
+
}
|
|
132
|
+
else if (char === '"')
|
|
133
|
+
quote = token.index;
|
|
134
|
+
else if (char === "{" || char === "[")
|
|
135
|
+
depth++;
|
|
136
|
+
else if (char === "}" || char === "]")
|
|
137
|
+
depth--;
|
|
138
|
+
}
|
|
139
|
+
return seen.size === 3;
|
|
140
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ruicode",
|
|
3
|
-
"version": "0.105.
|
|
3
|
+
"version": "0.105.6",
|
|
4
4
|
"description": "Start a Rui Host as a child process and talk to it over its local socket.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"mcp"
|
|
33
33
|
],
|
|
34
34
|
"optionalDependencies": {
|
|
35
|
-
"ruicode-linux-x64": "0.105.
|
|
36
|
-
"ruicode-linux-arm64": "0.105.
|
|
37
|
-
"ruicode-darwin-arm64": "0.105.
|
|
35
|
+
"ruicode-linux-x64": "0.105.6",
|
|
36
|
+
"ruicode-linux-arm64": "0.105.6",
|
|
37
|
+
"ruicode-darwin-arm64": "0.105.6"
|
|
38
38
|
}
|
|
39
39
|
}
|