From 099b7cb64b40e6bb826a07451136c0b7c5b7e6c8 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Mon, 23 Nov 2026 16:20:09 +0200 Subject: [PATCH] fix(daemon): leave LSP instances free after disconnect Scient-Divergence: SA-LSP-ACQUIRE-DISCONNECT --- SCIENT.md | 1 + .../coding-agent/src/daemon/modules/lsp.ts | 18 ++++++++- .../coding-agent/src/scient/lsp-acquire.ts | 35 +++++++++++++++++ .../scient/lsp-acquire-disconnect.test.ts | 39 +++++++++++++++++++ scripts/scient/divergences.json | 14 +++++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 packages/coding-agent/src/scient/lsp-acquire.ts create mode 100644 packages/coding-agent/test/scient/lsp-acquire-disconnect.test.ts diff --git a/SCIENT.md b/SCIENT.md index b9049f071a..be7434352d 100644 --- a/SCIENT.md +++ b/SCIENT.md @@ -21,6 +21,7 @@ Everything else is upstream's code and behavior. | Symbol | The sign-in page every browser sign-in ends on and the `stats` dashboard show Scient Agent's symbol (`packages/utils/src/scient/scient-agent-symbol.svg.txt`, the file Scient Desktop shows for the agent; copy it over when the symbol changes) in Scient's colors. | | Shared workers | The tiny model worker, the embedding server and word prediction run in Oh My Pi's per-user daemon. The daemon's build carries Scient Agent's release, not the upstream package version, so a new Scient Agent release has an older release's daemon re-exec into it, which replaces its workers. | | Daemon leases | A client that disconnects while Oh My Pi's per-user daemon is still starting a unit for it (a language server, a worker, a background process) no longer leaves that unit running for good: its lease is dropped as on any lost connection, after the usual reconnect grace. Offered to Oh My Pi as [#20263](https://github.com/can1357/oh-my-pi/pull/20263); to be realigned at the next alignment, retired if Oh My Pi merges it or replaced by Oh My Pi's own fix. | +| Language server reservations | A client that disconnects while the daemon resolves its working directory leaves the language server available for the next client. Offered to Oh My Pi as [#20277](https://github.com/can1357/oh-my-pi/pull/20277). At the next alignment, retire this change if Oh My Pi merged it or replace it with Oh My Pi's fix. | | Updates | `scient-agent update` only updates plugins. The startup update check and the startup marketplace refresh are off, and Oh My Pi's background auto-update is never offered or started. A host or installer replaces the executable. | | Reporting | Automatic tool-issue reports to OMP's service are off by default. | | Shell traces | The bash tool records no execution trace unless the user turns `display.shellVisualization` on. Scient Agent's host draws no shell visualization, and every live frame of a running command would carry the whole trace, up to megabytes. | diff --git a/packages/coding-agent/src/daemon/modules/lsp.ts b/packages/coding-agent/src/daemon/modules/lsp.ts index 62f9f43f06..b069379c5c 100644 --- a/packages/coding-agent/src/daemon/modules/lsp.ts +++ b/packages/coding-agent/src/daemon/modules/lsp.ts @@ -32,6 +32,9 @@ import { import type { LspJsonRpcId, LspJsonRpcNotification, LspJsonRpcRequest, LspJsonRpcResponse } from "../../lsp/types"; import { uriToFile } from "../../lsp/utils"; import { advertisesWatchedFiles, WATCHED_FILES_METHOD, WatchedFilesForwarder } from "../../lsp/watched-files"; +// scient(SA-LSP-ACQUIRE-DISCONNECT): begin - Track LSP requests while their working directory resolves. +import { cancelLspAcquires, trackLspAcquire } from "../../scient/lsp-acquire"; +// scient(SA-LSP-ACQUIRE-DISCONNECT): end import type { DaemonModule, ManagedChild, @@ -823,7 +826,17 @@ class LspServerUnit implements ModuleUnit { async function acquire(context: ModuleContext, params: unknown, client: ModuleClient): Promise { const server = ServerParams(params); if (server instanceof type.errors) throw new Error(`invalid lsp acquire params: ${server.summary}`); - const scope = projectScope(await canonicalDir(server.cwd)); + // scient(SA-LSP-ACQUIRE-DISCONNECT): begin - A client that disconnected while its cwd resolved gets no reservation: it would hold the key until it expires. + const pending = trackLspAcquire(client.connectionId); + let dir: string; + try { + dir = await canonicalDir(server.cwd); + } finally { + pending.finish(); + } + pending.assertConnected(); + const scope = projectScope(dir); + // scient(SA-LSP-ACQUIRE-DISCONNECT): end const identity = muxServerKey(server); // Synchronous from here: concurrent acquires each see the previous one's reservation. for (let index = 0; ; index++) { @@ -889,6 +902,9 @@ export const lspModule: DaemonModule = { return new LspServerUnit({ child, slot: restored.slot, cwd: restored.cwd, label: restored.label, restored }); }, disconnected(client) { + // scient(SA-LSP-ACQUIRE-DISCONNECT): begin - Acquires of this client still resolving their cwd reserve nothing. + cancelLspAcquires(client.connectionId); + // scient(SA-LSP-ACQUIRE-DISCONNECT): end for (const [slot, reservation] of reservations) { if (reservation.connectionId === client.connectionId) releaseReservation(slot); } diff --git a/packages/coding-agent/src/scient/lsp-acquire.ts b/packages/coding-agent/src/scient/lsp-acquire.ts new file mode 100644 index 0000000000..dce062690a --- /dev/null +++ b/packages/coding-agent/src/scient/lsp-acquire.ts @@ -0,0 +1,35 @@ +interface AcquireState { + disconnected: boolean; +} + +interface PendingAcquire { + finish(): void; + assertConnected(): void; +} + +const pendingAcquires = new Map>(); + +/** Track a request until its working directory resolves. */ +export function trackLspAcquire(connectionId: string): PendingAcquire { + const pending = { disconnected: false }; + const acquires = pendingAcquires.get(connectionId) ?? new Set(); + acquires.add(pending); + pendingAcquires.set(connectionId, acquires); + + return { + finish() { + acquires.delete(pending); + if (acquires.size === 0 && pendingAcquires.get(connectionId) === acquires) { + pendingAcquires.delete(connectionId); + } + }, + assertConnected() { + if (pending.disconnected) throw new Error("lsp acquire: client disconnected"); + }, + }; +} + +export function cancelLspAcquires(connectionId: string): void { + for (const pending of pendingAcquires.get(connectionId) ?? []) pending.disconnected = true; + pendingAcquires.delete(connectionId); +} diff --git a/packages/coding-agent/test/scient/lsp-acquire-disconnect.test.ts b/packages/coding-agent/test/scient/lsp-acquire-disconnect.test.ts new file mode 100644 index 0000000000..4ab13e4f43 --- /dev/null +++ b/packages/coding-agent/test/scient/lsp-acquire-disconnect.test.ts @@ -0,0 +1,39 @@ +/** + * An `lsp` acquire whose client disconnects while the acquire is still resolving its cwd + * reserves nothing: the instance key stays free for the next client (SA-LSP-ACQUIRE-DISCONNECT). + */ +import { afterEach, describe, expect, it } from "bun:test"; +import * as os from "node:os"; +import type { ModuleClient, ModuleContext } from "../../src/daemon/module"; +import { lspModule } from "../../src/daemon/modules/lsp"; +import { LSP_ACQUIRE_METHOD, type LspAcquireResult, lspInstanceKey, muxServerKey } from "../../src/lsp/mux/protocol"; + +const SERVER = { command: "fake-language-server", args: [], cwd: os.tmpdir() }; + +/** No units yet: every instance key is free unless reserved. */ +const context = { unit: () => undefined } as unknown as ModuleContext; + +function client(): ModuleClient { + return { connectionId: crypto.randomUUID(), pid: 0, build: "" }; +} + +function acquire(from: ModuleClient): Promise { + return lspModule.call!(context, LSP_ACQUIRE_METHOD, SERVER, from, undefined) as Promise; +} + +afterEach(async () => { + await lspModule.dispose?.(); +}); + +describe("lsp acquire after its client disconnected", () => { + it("reserves nothing for a client that disconnected while its acquire resolved the cwd", async () => { + const gone = client(); + const abandoned = acquire(gone); + lspModule.disconnected!(gone); + await expect(abandoned).rejects.toThrow(); + + // The first instance is free for the next client, not held by an abandoned reservation. + const next = await acquire(client()); + expect(next.key).toBe(lspInstanceKey(muxServerKey(SERVER), 0)); + }); +}); diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index e0671a96fe..2b2c855d6f 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -570,6 +570,20 @@ "bun --cwd=packages/coding-agent test test/scient/daemon-open-lease.test.ts" ] }, + "SA-LSP-ACQUIRE-DISCONNECT": { + "status": "active", + "contract": "A client that disconnects while an LSP acquire resolves its working directory leaves the first free instance key available for the next client. Offered to Oh My Pi as https://github.com/can1357/oh-my-pi/pull/20277. At the next alignment, retire this contract as upstreamed if Oh My Pi merged the fix, or replace it with Oh My Pi's own fix if it fixed this differently.", + "owned": [ + "packages/coding-agent/src/scient/lsp-acquire.ts", + "packages/coding-agent/test/scient/lsp-acquire-disconnect.test.ts" + ], + "upstream": [ + "packages/coding-agent/src/daemon/modules/lsp.ts" + ], + "verification": [ + "bun --cwd=packages/coding-agent test test/scient/lsp-acquire-disconnect.test.ts test/daemon/lsp-module.test.ts" + ] + }, "SA-DAEMON-REJOIN": { "status": "active", "contract": "Oh My Pi's daemon test \"a client reconnecting within the grace adopts the same process\" reopens the unit only after describe shows the daemon dropped the closed connection's lease, as the grace test above it waits. The harness's close() settles when the client socket closes, before the daemon handles the close, so the open could be counted beside the stale lease (leases 2), which failed Scient's main on 2026-11-22. Test-only; the daemon's connection-bound lease accounting is unchanged. This inherited flake needs an upstream fix.",