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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
import { describe, expect, it } from "vitest";
import type { FileWatcher, FsAccess, SpawnedProcess, SpawnProcess } from "./client.js";
import { encode } from "./framing.js";
import { LspManager } from "./manager.js";
function makeAutoHandshakeSpawn(): SpawnProcess {
return () => {
let messageHandler: ((data: Uint8Array) => void) | null = null;
const proc: SpawnedProcess = {
stdin: {
write: (bytes: Uint8Array) => {
// Parse the message to handle initialize handshake
const decoded = new TextDecoder().decode(bytes);
const headerEnd = decoded.indexOf("\r\n\r\n");
if (headerEnd === -1) return;
const json = decoded.slice(headerEnd + 4);
try {
const msg = JSON.parse(json);
if (msg.method === "initialize") {
// Send back initialize response
setTimeout(() => {
const response = JSON.stringify({
jsonrpc: "2.0",
id: msg.id,
result: { capabilities: {} },
});
messageHandler?.(encode(response));
}, 5);
}
// Ignore other messages
} catch {
// ignore parse errors
}
},
},
stdout: {
on: (_event: string, cb: (data: Uint8Array) => void) => {
messageHandler = cb;
},
},
pid: 12345,
kill: () => {},
};
return proc;
};
}
function noopFileWatcher(): FileWatcher {
return () => ({ close: () => {} });
}
function fakeFs(files: Record<string, string> = {}): FsAccess {
return {
readText: async (path) => files[path] ?? "",
exists: async (path) => path in files,
};
}
describe("manager", () => {
it("status(cwd) lazy-spawns matching servers and reports connected", async () => {
const manager = new LspManager({
spawn: makeAutoHandshakeSpawn(),
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/project/tsconfig.json": "{}",
"/project/.dispatch/lsp.json": JSON.stringify({
servers: {
test: {
command: ["test-lsp", "--stdio"],
extensions: [".ts"],
rootMarkers: ["tsconfig.json"],
},
},
}),
}),
});
const statuses = await manager.status("/project");
expect(statuses).toHaveLength(1);
expect(statuses[0]?.id).toBe("test");
expect(statuses[0]?.state).toBe("connected");
expect(statuses[0]?.root).toBe("/project");
}, 10000);
it("concurrent status for the same root spawns one process", async () => {
let spawnCount = 0;
const countingSpawn: SpawnProcess = () => {
spawnCount++;
return makeAutoHandshakeSpawn()([], { cwd: "/project" });
};
const manager = new LspManager({
spawn: countingSpawn,
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/project/tsconfig.json": "{}",
"/project/.dispatch/lsp.json": JSON.stringify({
servers: {
test: {
command: ["test-lsp"],
extensions: [".ts"],
rootMarkers: ["tsconfig.json"],
},
},
}),
}),
});
const [s1, s2] = await Promise.all([manager.status("/project"), manager.status("/project")]);
expect(s1).toHaveLength(1);
expect(s2).toHaveLength(1);
expect(spawnCount).toBe(1);
}, 10000);
it("a failing spawn reports state:error and is not retried", async () => {
let spawnCount = 0;
const failingSpawn: SpawnProcess = () => {
spawnCount++;
throw new Error("spawn failed");
};
const manager = new LspManager({
spawn: failingSpawn,
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/project/tsconfig.json": "{}",
"/project/.dispatch/lsp.json": JSON.stringify({
servers: {
test: {
command: ["test-lsp"],
extensions: [".ts"],
rootMarkers: ["tsconfig.json"],
},
},
}),
}),
});
const s1 = await manager.status("/project");
expect(s1[0]?.state).toBe("error");
expect(spawnCount).toBe(1);
// Second call should not retry
const s2 = await manager.status("/project");
expect(s2[0]?.state).toBe("error");
expect(spawnCount).toBe(1);
});
it("shutdownAll kills all spawned processes (incl. sidecars)", async () => {
let killed = false;
const trackableSpawn: SpawnProcess = () => {
const proc = makeAutoHandshakeSpawn()([], { cwd: "/project" });
return {
...proc,
kill: () => {
killed = true;
},
};
};
const manager = new LspManager({
spawn: trackableSpawn,
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/project/tsconfig.json": "{}",
"/project/.dispatch/lsp.json": JSON.stringify({
servers: {
test: {
command: ["test-lsp"],
extensions: [".ts"],
rootMarkers: ["tsconfig.json"],
},
},
}),
}),
});
await manager.status("/project");
manager.shutdownAll();
expect(killed).toBe(true);
}, 10000);
it("resolves config per cwd (distinct cwds, opencode.json fallback)", async () => {
const manager = new LspManager({
spawn: makeAutoHandshakeSpawn(),
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/proj-a/.dispatch/lsp.json": JSON.stringify({
servers: { a: { command: ["a-lsp"], extensions: [".a"], rootMarkers: [] } },
}),
"/proj-b/opencode.json": JSON.stringify({
lsp: { b: { command: ["b-lsp"], extensions: [".b"] } },
}),
}),
});
const a = await manager.status("/proj-a");
const b = await manager.status("/proj-b");
expect(a.map((s) => s.id)).toEqual(["a"]);
expect(b.map((s) => s.id)).toEqual(["b"]);
}, 10000);
it("manager: broken server recovers after config is fixed (no shutdownAll)", async () => {
const files: Record<string, string> = {
"/project/tsconfig.json": "{}",
"/project/.dispatch/lsp.json": JSON.stringify({
servers: {
test: {
command: ["bad-lsp"],
extensions: [".ts"],
rootMarkers: ["tsconfig.json"],
},
},
}),
};
const spawn: SpawnProcess = (command, opts) => {
if (command[0] === "bad-lsp") throw new Error("spawn failed");
return makeAutoHandshakeSpawn()(command, opts);
};
const manager = new LspManager({
spawn,
fileWatcher: noopFileWatcher(),
fs: fakeFs(files),
now: () => 0,
});
// Bad config → spawn fails → broken.
const s1 = await manager.status("/project");
expect(s1[0]?.state).toBe("error");
// Fix the config: switch the command to a working one. NO shutdownAll.
files["/project/.dispatch/lsp.json"] = JSON.stringify({
servers: {
test: {
command: ["good-lsp"],
extensions: [".ts"],
rootMarkers: ["tsconfig.json"],
},
},
});
// Next status() re-reads config, detects the change, and re-spawns.
const s2 = await manager.status("/project");
expect(s2[0]?.state).toBe("connected");
}, 10000);
it("manager: no retry storm — repeated status() with no config change does not re-spawn a broken server in a loop", async () => {
let spawnCount = 0;
const failingSpawn: SpawnProcess = () => {
spawnCount++;
throw new Error("spawn failed");
};
const manager = new LspManager({
spawn: failingSpawn,
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/project/.dispatch/lsp.json": JSON.stringify({
servers: { test: { command: ["test-lsp"], extensions: [".ts"] } },
}),
}),
// Frozen clock: the bounded backoff never elapses, so a config-unchanged
// broken server is never retried in a loop.
now: () => 0,
});
await manager.status("/project");
await manager.status("/project");
await manager.status("/project");
await manager.status("/project");
expect(spawnCount).toBe(1);
});
it("manager: configSource reaches status()", async () => {
const manager = new LspManager({
spawn: makeAutoHandshakeSpawn(),
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/d/.dispatch/lsp.json": JSON.stringify({
servers: { d: { command: ["d-lsp"], extensions: [".d"], rootMarkers: [] } },
}),
"/o/opencode.json": JSON.stringify({
lsp: { o: { command: ["o-lsp"], extensions: [".o"] } },
}),
"/b/tsconfig.json": "{}",
}),
now: () => 0,
});
const d = await manager.status("/d");
expect(d[0]?.configSource).toBe(".dispatch/lsp.json");
const o = await manager.status("/o");
expect(o[0]?.configSource).toBe("opencode.json");
const b = await manager.status("/b");
expect(b[0]?.configSource).toBe("built-in");
}, 10000);
it("config: shadow warning logged when .dispatch/lsp.json present and opencode.json also has lsp", async () => {
const warns: Array<{
msg: string;
attrs?: Record<string, string | number | boolean | null>;
}> = [];
const manager = new LspManager({
spawn: makeAutoHandshakeSpawn(),
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/project/.dispatch/lsp.json": JSON.stringify({
servers: { d: { command: ["d-lsp"], extensions: [".d"] } },
}),
"/project/opencode.json": JSON.stringify({
lsp: { o: { command: ["o-lsp"], extensions: [".o"] } },
}),
}),
logger: {
info: () => {},
warn: (msg, attrs) => warns.push({ msg, attrs }),
error: () => {},
},
now: () => 0,
});
await manager.status("/project");
const shadow = warns.find((w) => w.msg.includes("shadowing"));
expect(shadow).toBeDefined();
expect(shadow?.attrs?.cwd).toBe("/project");
// A second status() over the SAME (still-shadowed) cwd does not re-warn.
const before = warns.length;
await manager.status("/project");
expect(warns.length).toBe(before);
}, 10000);
it("status: error string includes config source on spawn failure", async () => {
const failingSpawn: SpawnProcess = () => {
throw new Error("spawn failed");
};
const manager = new LspManager({
spawn: failingSpawn,
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/project/.dispatch/lsp.json": JSON.stringify({
servers: { test: { command: ["test-lsp"], extensions: [".ts"] } },
}),
}),
now: () => 0,
});
const s = await manager.status("/project");
expect(s[0]?.state).toBe("error");
expect(s[0]?.configSource).toBe(".dispatch/lsp.json");
expect(s[0]?.error).toContain("[from .dispatch/lsp.json]");
expect(s[0]?.error).toContain("spawn failed");
});
it("a client that dies after connecting is skipped + re-spawned after backoff (no storm, no eternal hang)", async () => {
// A spawn that completes the initialize handshake AND lets the test
// simulate process death via the captured onExit handler.
const exitHandlers: Array<(info: { code: number | null; signal?: string }) => void> = [];
let spawnCount = 0;
const spawn: SpawnProcess = () => {
spawnCount++;
let messageHandler: ((data: Uint8Array) => void) | null = null;
const proc: SpawnedProcess = {
stdin: {
write: (bytes: Uint8Array) => {
const decoded = new TextDecoder().decode(bytes);
const headerEnd = decoded.indexOf("\r\n\r\n");
if (headerEnd === -1) return;
const json = decoded.slice(headerEnd + 4);
try {
const msg = JSON.parse(json);
if (msg.method === "initialize") {
setTimeout(() => {
const response = JSON.stringify({
jsonrpc: "2.0",
id: msg.id,
result: { capabilities: {} },
});
messageHandler?.(encode(response));
}, 1);
}
} catch {
// ignore
}
},
},
stdout: {
on: (_event: string, cb: (data: Uint8Array) => void) => {
messageHandler = cb;
},
},
pid: 1000 + spawnCount,
kill: () => {},
onExit: (handler) => {
exitHandlers.push(handler);
},
};
return proc;
};
const clock = { now: 0 };
const manager = new LspManager({
spawn,
fileWatcher: noopFileWatcher(),
fs: fakeFs({
"/project/.dispatch/lsp.json": JSON.stringify({
servers: {
steep: {
command: ["steep", "--stdio"],
extensions: [".rb"],
rootMarkers: [],
},
},
}),
}),
now: () => clock.now,
});
// 1) Connects.
const s1 = await manager.status("/project");
expect(s1[0]?.state).toBe("connected");
expect(spawnCount).toBe(1);
// 2) Simulate the process dying (user kill / crash) via onExit.
exitHandlers[0]?.({ code: 1 });
const clientAfterDeath = manager.getClient("steep", "/project");
expect(clientAfterDeath?.getState()).toBe("error");
// 3) status() now reports error (and seeds a broken entry for backoff).
// Backoff not elapsed yet (clock frozen at 0) → NOT re-spawned.
const s2 = await manager.status("/project");
expect(s2[0]?.state).toBe("error");
expect(s2[0]?.error).toMatch(/process exited/);
expect(spawnCount).toBe(1); // no retry storm before backoff
// 4) After the backoff elapses, status() re-spawns a fresh server.
clock.now = 31_000;
const s3 = await manager.status("/project");
expect(s3[0]?.state).toBe("connected");
expect(spawnCount).toBe(2); // re-spawned exactly once
});
});
|