-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathffiRuntimeHost.ts
More file actions
421 lines (389 loc) · 15.9 KB
/
Copy pathffiRuntimeHost.ts
File metadata and controls
421 lines (389 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
/**
* Hosts the Copilot runtime in-process by loading the native `runtime.node` cdylib
* and speaking JSON-RPC over its C ABI (FFI) instead of spawning a CLI child process
* and communicating over stdio/TCP.
*
* The native `host_start` export constructs the Rust server synchronously in this
* process. LSP `Content-Length:`-framed JSON-RPC bytes are pumped across the ABI:
* writes go to `connection_write`; inbound frames arrive on a native callback that
* feeds {@link FfiRuntimeHost.receiveStream}. The existing `vscode-jsonrpc`
* `StreamMessageReader`/`StreamMessageWriter` handle framing unchanged — this is a
* transport swap, not a new protocol.
*/
import { existsSync } from "node:fs";
import koffi from "koffi";
import { resolve } from "node:path";
import { PassThrough, Writable } from "node:stream";
const SYMBOL_PREFIX = "copilot_runtime_";
// A long, referenced no-op timer keeps the Node event loop alive while the in-process
// connection is open (see start()); the exact interval is irrelevant.
const KEEP_ALIVE_INTERVAL_MS = 1 << 30;
const CLEANUP_RETRY_INTERVAL_MS = 100;
type KoffiFunction = ReturnType<ReturnType<typeof koffi.load>["func"]>;
type KoffiType = ReturnType<typeof koffi.pointer>;
type KoffiRegisteredCallback = ReturnType<typeof koffi.register>;
interface FfiLibrary {
hostStart: KoffiFunction;
hostShutdown: KoffiFunction;
connectionOpen: KoffiFunction;
connectionWrite: KoffiFunction;
connectionClose: KoffiFunction;
outboundCallbackType: KoffiType;
}
let loadedLibraryPath: string | undefined;
let loadedLibrary: FfiLibrary | undefined;
/**
* Loads the cdylib once per process and binds the C ABI exports. Loading a
* different library path in the same process is unsupported.
*/
function loadLibrary(libraryPath: string): FfiLibrary {
if (loadedLibrary) {
if (loadedLibraryPath !== libraryPath) {
throw new Error(
`An in-process FFI runtime library is already loaded from '${loadedLibraryPath}'; ` +
`loading a different library from '${libraryPath}' in the same process is not supported.`
);
}
return loadedLibrary;
}
const lib = koffi.load(libraryPath);
const outboundCallbackType = koffi.pointer(
koffi.proto(
`void ${SYMBOL_PREFIX}outbound(void *userData, uint8 *bytesPtr, size_t bytesLen)`
)
);
loadedLibrary = {
hostStart: lib.func(`${SYMBOL_PREFIX}host_start`, "uint32", [
"uint8*",
"size_t",
"uint8*",
"size_t",
]),
hostShutdown: lib.func(`${SYMBOL_PREFIX}host_shutdown`, "bool", ["uint32"]),
connectionOpen: lib.func(`${SYMBOL_PREFIX}connection_open`, "uint32", [
"uint32",
outboundCallbackType,
"void*",
"uint8*",
"size_t",
"uint8*",
"size_t",
"uint8*",
"size_t",
]),
connectionWrite: lib.func(`${SYMBOL_PREFIX}connection_write`, "bool", [
"uint32",
"uint8*",
"size_t",
]),
connectionClose: lib.func(`${SYMBOL_PREFIX}connection_close`, "bool", ["uint32"]),
outboundCallbackType,
};
loadedLibraryPath = libraryPath;
return loadedLibrary;
}
function buildArgvJson(cliEntrypoint: string | undefined, args: readonly string[]): Buffer {
const argv = cliEntrypoint
? cliEntrypoint.toLowerCase().endsWith(".js")
? ["node", cliEntrypoint, "--embedded-host", "--no-auto-update"]
: [cliEntrypoint, "--embedded-host", "--no-auto-update"]
: [];
argv.push(...args);
return Buffer.from(JSON.stringify(argv), "utf8");
}
function buildEnvJson(environment?: Record<string, string | undefined>): Buffer | null {
if (!environment) {
return null;
}
const obj: Record<string, string> = {};
for (const [key, value] of Object.entries(environment)) {
if (value !== undefined) {
obj[key] = value;
}
}
if (Object.keys(obj).length === 0) {
return null;
}
return Buffer.from(JSON.stringify(obj), "utf8");
}
export class FfiRuntimeHost {
private static readonly quarantinedHosts = new Set<FfiRuntimeHost>();
private readonly lib: FfiLibrary;
private serverId = 0;
private connectionId = 0;
private disposed = false;
private starting = false;
private outboundCallback: KoffiRegisteredCallback | undefined;
private keepAliveTimer: ReturnType<typeof setInterval> | undefined;
private cleanupRetryTimer: ReturnType<typeof setTimeout> | undefined;
private cleanupInProgress = false;
/** The stream JSON-RPC reads server→client frames from. */
readonly receiveStream: PassThrough;
/** The stream JSON-RPC writes client→server frames to. */
readonly sendStream: Writable;
private constructor(
private readonly libraryPath: string,
private readonly cliEntrypoint: string | undefined,
private readonly environment: Record<string, string | undefined> | undefined,
private readonly args: readonly string[]
) {
this.lib = loadLibrary(libraryPath);
this.receiveStream = new PassThrough();
this.sendStream = new Writable({
// connection_write enqueues the frame into the runtime's inbound channel and
// returns immediately, so a synchronous FFI call is sufficient here.
write: (chunk: Buffer, _encoding, callback) => {
try {
this.writeFrame(chunk);
callback();
} catch (error) {
callback(error as Error);
}
},
});
}
/**
* Loads the runtime cdylib at the given path and prepares the FFI host.
*/
static create(
libraryPath: string,
cliEntrypoint: string | undefined,
environment: Record<string, string | undefined> | undefined,
args: readonly string[]
): FfiRuntimeHost {
const fullLibraryPath = resolve(libraryPath);
if (!existsSync(fullLibraryPath)) {
throw new Error(`FFI runtime library not found at '${fullLibraryPath}'.`);
}
return new FfiRuntimeHost(
fullLibraryPath,
cliEntrypoint ? resolve(cliEntrypoint) : undefined,
environment,
args
);
}
/** Starts the in-process Rust runtime and opens the FFI JSON-RPC connection. */
async start(): Promise<void> {
if (this.disposed) {
throw new Error("The in-process runtime host is disposed.");
}
this.starting = true;
const argvJson = buildArgvJson(this.cliEntrypoint, this.args);
const envJson = buildEnvJson(this.environment);
try {
// The native host has no cwd parameter, so it uses this process's cwd. A custom
// working directory is intentionally
// unsupported for the in-process transport (rejected by the client constructor)
// rather than mutating the shared process-global cwd here.
// host_start constructs the native engine synchronously; run it as an async FFI
// call so the Node event loop isn't blocked.
this.serverId = await new Promise<number>((resolvePromise, rejectPromise) => {
this.lib.hostStart.async(
argvJson,
argvJson.length,
envJson,
envJson ? envJson.length : 0,
(error: Error | null, result: number) => {
if (error) {
rejectPromise(error);
} else {
resolvePromise(result);
}
}
);
});
if (!this.serverId) {
throw new Error(
`copilot_runtime_host_start failed (library '${this.libraryPath}').`
);
}
if (this.disposed) {
throw new Error("The in-process runtime host was disposed during startup.");
}
this.outboundCallback = koffi.register(
(_userData: unknown, bytesPtr: unknown, bytesLen: number | bigint) =>
this.feedInbound(bytesPtr, bytesLen),
this.lib.outboundCallbackType
);
this.connectionId = this.lib.connectionOpen(
this.serverId,
this.outboundCallback,
null,
null,
0,
null,
0,
null,
0
);
if (!this.connectionId) {
this.unregisterCallback();
this.lib.hostShutdown(this.serverId);
this.serverId = 0;
throw new Error("copilot_runtime_connection_open failed.");
}
// The in-process transport has no socket/pipe handle to keep the Node event loop
// alive while the SDK is idle awaiting a server→client frame. koffi delivers the
// outbound callback on the loop but does not reference it, so hold one referenced
// timer for the lifetime of the connection.
this.keepAliveTimer = setInterval(() => {}, KEEP_ALIVE_INTERVAL_MS);
} finally {
this.starting = false;
if (this.disposed) {
void this.tryFinalizeCleanup();
}
}
}
private writeFrame(frame: Buffer): void {
if (this.disposed || !this.connectionId) {
throw new Error("The in-process runtime connection is closed.");
}
const ok = this.lib.connectionWrite(this.connectionId, frame, frame.length);
if (!ok) {
throw new Error("Failed to write a frame to the in-process runtime connection.");
}
}
/**
* Native outbound (server→client) callback. koffi delivers it on the JS event loop
* via a threadsafe function, so the frame is decoded and written straight to
* {@link receiveStream}. The native pointer is only valid for this call, so the
* bytes are copied out before returning.
*/
private feedInbound(bytesPtr: unknown, bytesLen: number | bigint): void {
// An exception thrown across the native→JS (Node-API) boundary cannot propagate
// and would surface only as a DEP0168 "uncaught Node-API callback exception"
// warning, so catch and log it here instead of letting it escape.
try {
// A native outbound callback can still be delivered on the event loop after
// dispose() has ended receiveStream; writing then would throw
// ERR_STREAM_WRITE_AFTER_END. Drop late frames instead — the connection is
// gone and nothing is reading them.
if (this.disposed || this.receiveStream.writableEnded) {
return;
}
const length = Number(bytesLen);
if (!bytesPtr || length <= 0) {
return;
}
const bytes = koffi.decode(
bytesPtr,
koffi.array("uint8", length, "Typed")
) as Uint8Array;
this.receiveStream.write(Buffer.from(bytes));
} catch (error) {
console.error(
`In-process FFI inbound callback failed: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`
);
}
}
private unregisterCallback(): boolean {
if (this.outboundCallback === undefined) {
return true;
}
const callback = this.outboundCallback;
try {
koffi.unregister(callback);
this.outboundCallback = undefined;
return true;
} catch (error) {
console.error(
`Failed to unregister in-process FFI callback: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`
);
FfiRuntimeHost.quarantinedHosts.add(this);
return false;
}
}
private scheduleCleanupRetry(): void {
if (this.cleanupRetryTimer !== undefined) {
return;
}
this.cleanupRetryTimer = setTimeout(() => {
this.cleanupRetryTimer = undefined;
void this.tryFinalizeCleanup();
}, CLEANUP_RETRY_INTERVAL_MS);
}
private async tryFinalizeCleanup(): Promise<void> {
if (this.cleanupInProgress) {
this.scheduleCleanupRetry();
return;
}
this.cleanupInProgress = true;
try {
if (this.connectionId) {
let closed = false;
try {
// Close waits for outbound callbacks, which need the JS event loop
// to run and return before native code can report quiescence.
closed = await new Promise<boolean>((resolvePromise, rejectPromise) => {
this.lib.connectionClose.async(
this.connectionId,
(error: Error | null, result: boolean) => {
if (error) {
rejectPromise(error);
} else {
resolvePromise(result);
}
}
);
});
} catch (error) {
console.error(
`Failed to close in-process FFI connection: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`
);
FfiRuntimeHost.quarantinedHosts.add(this);
if (this.keepAliveTimer !== undefined) {
clearInterval(this.keepAliveTimer);
this.keepAliveTimer = undefined;
}
return;
}
if (!closed) {
this.scheduleCleanupRetry();
return;
}
this.connectionId = 0;
}
const callbackUnregistered = this.unregisterCallback();
// The referenced timer is part of the callback lifetime. Clearing it
// before connection_close reports quiescence can let the process exit
// while native code still owns the Koffi registration.
if (this.keepAliveTimer !== undefined) {
clearInterval(this.keepAliveTimer);
this.keepAliveTimer = undefined;
}
if (this.serverId) {
try {
if (!this.lib.hostShutdown(this.serverId)) {
console.error(
`In-process FFI host shutdown did not recognize server ${this.serverId}.`
);
}
} catch (error) {
console.error(
`Failed to shut down in-process FFI host: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`
);
}
this.serverId = 0;
}
if (callbackUnregistered) {
FfiRuntimeHost.quarantinedHosts.delete(this);
}
} finally {
this.cleanupInProgress = false;
}
}
/** Awaits the initial cleanup attempt; a non-quiescent close is retried in the background. */
async dispose(): Promise<void> {
if (this.disposed) {
return;
}
this.disposed = true;
this.receiveStream.end();
if (!this.starting) {
await this.tryFinalizeCleanup();
}
}
}