From e7eada4802ceebd86c83bcd6e3eca70152e7f331 Mon Sep 17 00:00:00 2001 From: Adam Malczewski Date: Thu, 11 Jun 2026 21:12:03 +0900 Subject: feat(lsp,cwd): LSP integration + per-conversation cwd; fix cache-warming cache bust MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LSP + per-conversation CWD feature: - new bundled `lsp` extension: hand-rolled JSON-RPC codec (framing/rpc), lazy one-server-per-(serverID,root), per-cwd config resolution, on-demand `lsp` tool - `conversation-store`: getCwd/setCwd (cwdKey); `session-orchestrator` defaults a turn's cwd from the store - `transport-http`: cwd + lsp status endpoints; wire types in transport-contract - host-bin: register lsp; config wiring Cache-warming fix (the warm read 0% on the first reheat after a message): - warm assembled tools under a different cwd than the real turn (a reheat sends no cwd, and the warm service had no store fallback). The skills filter rewrites the cwd-sensitive `load_skill` description, so the tools block — the first bytes of the prompt-cache prefix — diverged and the cache missed entirely. Warm now resolves cwd as opts.cwd ?? conversationStore.getCwd(), mirroring handleMessage. - capture warm sends as `provider.request` spans flagged `warm:true` (thread a child logger into providerOpts) so warm vs real bodies are diffable (obs §3.1). - kernel logger: span-close now merges child-bound attrs like span-open, so a `warm:true` query finds the closed span (with usage/status), not just the open. Tests: warm forwards a warm-flagged logger; warm falls back to stored cwd; logger open/close attr consistency. Full suite green (873). --- packages/lsp/src/rpc.test.ts | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 packages/lsp/src/rpc.test.ts (limited to 'packages/lsp/src/rpc.test.ts') diff --git a/packages/lsp/src/rpc.test.ts b/packages/lsp/src/rpc.test.ts new file mode 100644 index 0000000..a03870f --- /dev/null +++ b/packages/lsp/src/rpc.test.ts @@ -0,0 +1,78 @@ +import { describe, expect, it } from "vitest"; +import { JsonRpcConnection } from "./rpc.js"; + +function makeConnection(): { conn: JsonRpcConnection; messages: string[] } { + const messages: string[] = []; + const conn = new JsonRpcConnection((bytes) => { + const decoded = new TextDecoder().decode(bytes); + // Extract JSON from the LSP-framed message + const headerEnd = decoded.indexOf("\r\n\r\n"); + if (headerEnd !== -1) { + messages.push(decoded.slice(headerEnd + 4)); + } + }); + return { conn, messages }; +} + +function frameResponse(id: number, result: unknown): string { + return JSON.stringify({ jsonrpc: "2.0", id, result }); +} + +describe("rpc", () => { + it("sendRequest resolves by matching id", async () => { + const { conn, messages } = makeConnection(); + + const promise = conn.sendRequest("test/method", { key: "value" }); + expect(messages).toHaveLength(1); + + const rawSent = messages[0]; + if (rawSent === undefined) throw new Error("expected a sent message"); + const sent = JSON.parse(rawSent); + expect(sent.method).toBe("test/method"); + expect(sent.params).toEqual({ key: "value" }); + expect(sent.id).toBe(1); + + conn.handleMessage(frameResponse(1, { ok: true })); + const result = await promise; + expect(result).toEqual({ ok: true }); + }); + + it("onNotification dispatches by method", () => { + const { conn } = makeConnection(); + let received: unknown = null; + conn.onNotification("test/notify", (params) => { + received = params; + }); + + conn.handleMessage( + JSON.stringify({ jsonrpc: "2.0", method: "test/notify", params: { data: 42 } }), + ); + expect(received).toEqual({ data: 42 }); + }); + + it("onRequest replies to a server-to-client request", async () => { + const { conn, messages } = makeConnection(); + + conn.onRequest("workspace/configuration", (params) => { + const { items } = params as { readonly items: readonly { readonly section?: string }[] }; + return items.map(() => ({ setting: true })); + }); + + await conn.handleMessage( + JSON.stringify({ + jsonrpc: "2.0", + id: 100, + method: "workspace/configuration", + params: { items: [{ section: "test" }] }, + }), + ); + + // The response should be sent back + expect(messages).toHaveLength(1); + const rawResponse = messages[0]; + if (rawResponse === undefined) throw new Error("expected a response message"); + const response = JSON.parse(rawResponse); + expect(response.id).toBe(100); + expect(response.result).toEqual([{ setting: true }]); + }); +}); -- cgit v1.2.3