-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathffiRuntimeHost.ts
More file actions
338 lines (308 loc) · 12.7 KB
/
Copy pathffiRuntimeHost.ts
File metadata and controls
338 lines (308 loc) · 12.7 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
/*---------------------------------------------------------------------------------------------
* 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 spawns the CLI worker itself
* (`node <entrypoint> --embedded-host` for a `.js` entrypoint, or `<entrypoint>
* --embedded-host` for a packaged binary), so the SDK never launches the worker
* directly. 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 { dirname, join, 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;
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): Buffer {
// A `.js` entrypoint is launched via node; the packaged single-file CLI binary
// embeds its own Node and is invoked directly. `--no-auto-update` pins the worker
// to the bundled pkg matching the loaded cdylib, instead of drifting to a newer
// version installed under the user's `~/.copilot/pkg` (which would cause ABI skew).
const argv = cliEntrypoint.toLowerCase().endsWith(".js")
? ["node", cliEntrypoint, "--embedded-host", "--no-auto-update"]
: [cliEntrypoint, "--embedded-host", "--no-auto-update"];
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 readonly lib: FfiLibrary;
private serverId = 0;
private connectionId = 0;
private disposed = false;
private outboundCallback: KoffiRegisteredCallback | undefined;
private keepAliveTimer: ReturnType<typeof setInterval> | undefined;
/** 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,
private readonly environment?: Record<string, string | undefined>
) {
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);
}
},
});
}
/**
* Resolves the cdylib next to the given CLI entrypoint and prepares the FFI host.
* The cdylib is resolved as `prebuilds/<prebuildsFolder>/runtime.node` relative to
* the entrypoint directory (the napi-rs `<node-platform>-<arch>` layout, e.g.
* `linux-x64`). Throws if it cannot be found.
*/
static create(
cliEntrypoint: string,
prebuildsFolder: string,
environment?: Record<string, string | undefined>
): FfiRuntimeHost {
const fullEntrypoint = resolve(cliEntrypoint);
const distDir = dirname(fullEntrypoint);
const libraryPath = join(distDir, "prebuilds", prebuildsFolder, "runtime.node");
if (!existsSync(libraryPath)) {
throw new Error(`FFI runtime library not found. Looked for '${libraryPath}'.`);
}
return new FfiRuntimeHost(libraryPath, fullEntrypoint, environment);
}
/**
* Starts the in-process runtime: spawns the CLI worker via the native host,
* waits for readiness, and opens the FFI JSON-RPC connection.
*/
async start(): Promise<void> {
const argvJson = buildArgvJson(this.cliEntrypoint);
const envJson = buildEnvJson(this.environment);
// The native host spawns the CLI worker itself and has no cwd parameter, so the
// worker inherits 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 blocks until the worker connects back and signals readiness
// (up to ~30s); 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}', entrypoint '${this.cliEntrypoint}').`
);
}
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);
}
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(): void {
if (this.outboundCallback === undefined) {
return;
}
const callback = this.outboundCallback;
this.outboundCallback = undefined;
try {
koffi.unregister(callback);
} catch {
// Ignore teardown failures.
}
}
/** Closes the FFI connection, shuts down the native host, and releases resources. */
dispose(): void {
if (this.disposed) {
return;
}
this.disposed = true;
if (this.keepAliveTimer !== undefined) {
clearInterval(this.keepAliveTimer);
this.keepAliveTimer = undefined;
}
try {
if (this.connectionId) {
this.lib.connectionClose(this.connectionId);
this.connectionId = 0;
}
} catch {
// Ignore teardown failures.
}
try {
if (this.serverId) {
this.lib.hostShutdown(this.serverId);
this.serverId = 0;
}
} catch {
// Ignore teardown failures.
}
this.receiveStream.end();
this.unregisterCallback();
}
}