diff --git a/docs/PI.md b/docs/PI.md index c7f144b3..6443a0b7 100644 --- a/docs/PI.md +++ b/docs/PI.md @@ -130,6 +130,43 @@ phone needs, shown with the same cards as the other agents: The checklist, questions, sub-agents and background shells need no card of their own beyond what they start. +A file Pi changes with `edit` or `write` shows as a diff under its call: +an edit's lines are the patch Pi applied, with the lines around it; a write +shows the content it wrote. + +## Diagnostics + +After an `edit` or `write` succeeds, the project's language server checks +the file, and its errors and warnings are added to the tool's result — the +model reads a type error it just made before its next step, and the phone +shows it in the result like the rest. Only servers already on the machine +are used; nothing is installed: + +| Files | Server | Found in | +|---|---|---| +| TypeScript, JavaScript | `typescript-language-server` for a TypeScript up to 6.x; TypeScript 7's own `tsc --lsp` | the project's `node_modules`; `typescript-language-server` also on `PATH` | +| Python | `basedpyright-langserver`, else `pyright-langserver` | the project's `.venv`/`venv`, its `node_modules`, then `PATH` | +| Rust | `rust-analyzer` (its own check, then `cargo check`'s) | `PATH` | + +The project is the nearest directory above the file with a `tsconfig.json`, +`jsconfig.json` or `package.json`; a `Cargo.toml`; or a `pyproject.toml`, +`setup.py`, `setup.cfg`, `pyrightconfig.json` or `requirements.txt`. A +server starts with the first change in its project and stays for the +session (stopped after ten idle minutes, and with Pi). The first check +waits for it to load the project — about a second for TypeScript and +Python, ten or more for rust-analyzer, at most 30 s — and later ones take +under a second; a check never waits more than 10 s, and a list that came +in late is marked unfinished. A server that is missing or fails to start +is skipped for that project until the session ends: rustup's +`rust-analyzer` without the component installed +(`rustup component add rust-analyzer`) is one. + +Reported: the changed file only, errors and warnings (not hints), at most +20. A server runs the project's own build tooling (`cargo check` runs +build scripts) with the bridge's environment, without a provider profile's +token. Set `CODEDECK_PI_LSP=off` in the bridge's environment to turn the +checks off. + ## Reasoning and messages Reasoning is Pi's thinking level: Off, Minimal, Low, Medium (Pi's default), @@ -165,8 +202,7 @@ and macOS. The Plugins screen manages Pi's packages, through Pi's own `pi install`, `pi update` and `pi remove`. A package bundles extensions, skills, prompt templates and themes from npm (`npm:`), git (`git:github.com/…` or -a URL) or a path, and is how Pi gains tools neither it nor CodeDeck ships, -such as LSP diagnostics. +a URL) or a path, and is how Pi gains tools neither it nor CodeDeck ships. A package runs code with the bridge's permissions: install only ones you trust. An npm package needs `npm` on the machine (the container image has it, a release archive does not); `CODEDECK_NPM_REGISTRY` is passed on to it. @@ -177,7 +213,8 @@ change load it. - **No subscription usage** to ask for (the context meter still works) and no saved permission rules. -- **No LSP diagnostics** of CodeDeck's own: a Pi package can add them. +- **Diagnostics for three languages**: TypeScript/JavaScript, Python and + Rust (above). Another language's checks can come from a Pi package. - **Accounts need Pi's package**: with `CODEDECK_PI_PATH` naming an executable rather than a `cli.js`, they cannot be listed or changed. diff --git a/packages/agent-host/package.json b/packages/agent-host/package.json index 936a7153..57fb2520 100644 --- a/packages/agent-host/package.json +++ b/packages/agent-host/package.json @@ -27,6 +27,7 @@ "@types/node": "^22.10.0", "esbuild": "^0.25.0", "typescript": "^5.7.0", + "typescript-language-server": "6.1.0", "vitest": "^3.0.0" }, "license": "MIT" diff --git a/packages/agent-host/src/drivers/opencode/adapter.ts b/packages/agent-host/src/drivers/opencode/adapter.ts index 1692cb55..1c27f8f7 100644 --- a/packages/agent-host/src/drivers/opencode/adapter.ts +++ b/packages/agent-host/src/drivers/opencode/adapter.ts @@ -17,8 +17,8 @@ import type { FileDiffInfo } from '@opencode/client'; import { todosOf, toolLocations } from '../../sdk/tools'; import { opencodeTool } from './tools'; import { - MAX_DIFF_LINE_CHARS, - MAX_DIFF_LINES, + boundedDiff, + patchLines, toDiffLines, truncateToolResult, type DiffPayload, @@ -266,42 +266,6 @@ function toolResultEntries(event: Extract MAX_DIFF_LINE_CHARS ? line.slice(0, MAX_DIFF_LINE_CHARS) + '…' : line; -} - -/** A diff payload inside the shared wire caps; `null` when there are no - * lines to show. */ -function boundedDiff(path: string, lines: DiffLine[]): DiffPayload | null { - if (lines.length === 0) return null; - const truncated = lines.length > MAX_DIFF_LINES; - return { - path, - lines: truncated ? lines.slice(0, MAX_DIFF_LINES) : lines, - ...(truncated ? { truncated: true } : {}), - }; -} - /** One file OpenCode reports as changed, as a card: its unified patch turned * into lines. `null` when the patch carries no line content at all (only * counts) — there is nothing to render then. */ diff --git a/packages/agent-host/src/drivers/pi/__tests__/fakeLanguageServer.mjs b/packages/agent-host/src/drivers/pi/__tests__/fakeLanguageServer.mjs new file mode 100644 index 00000000..a5376520 --- /dev/null +++ b/packages/agent-host/src/drivers/pi/__tests__/fakeLanguageServer.mjs @@ -0,0 +1,195 @@ +/** + * A language server for the CodeDeck Pi extension's diagnostics tests, run + * as its own process: `node fakeLanguageServer.mjs `. + * + * Each mode does only what the real server was seen doing, run by hand + * against it (2026-11-18, the frames logged around didOpen/didChange/didSave): + * + * - `push`: typescript-language-server 6.1.0 with TypeScript 5.9 — no pull; + * a `$/progress` begin/end around its first project load; then one + * `publishDiagnostics` per change, WITHOUT a version and with the URI in VS + * Code's spelling (`file:///c%3A/…` for `file:///C:/…`). + * - `push-versioned`: Pyright / basedpyright 1.40 — no pull advertised; a + * progress (created with `window/workDoneProgress/create`) around each + * analysis, then a push WITH the version; `workspace/configuration` + * requests, numbered from 0. + * - `pull`: TypeScript 7.0's `tsc --lsp` — `diagnosticProvider`, answers + * `textDocument/diagnostic` at once, never pushes. + * - `ra`: rust-analyzer 1.99 — answers a pull, but with an empty list while + * its workspace loads under progress; after a save, `cargo check` runs + * under the `rust-analyzer/flycheck/0` progress, its results arrive as a + * versioned push (worded differently, same code), and then it sends + * `workspace/diagnostic/refresh` with no params. + * - `crash`: rustup's `rust-analyzer` proxy with the component not installed + * — prints an error and exits 1. + * - `silent`: a server that never reports, for the deadline. + * + * And faults no real server was seen making, for the client's own guards: + * `null-frame` sends a frame whose body is `null` before each answer; + * `mute` never answers `initialize`; `slow-first` is `push-versioned` with + * no progress and slow pushes: the first after 600 ms, the rest after + * 1000 ms. + * + * Diagnostics come from the document's text: a line holding `ERROR ` is + * an error at that word, `WARN` a warning, `HINT` a hint, and `ENV ` + * an error saying whether NAME is in this process's environment. + */ +const mode = process.argv[2] ?? 'push'; +if (mode === 'crash') { + process.stderr.write("error: Unknown binary 'rust-analyzer' in official toolchain\n"); + process.exit(1); +} + +let buffer = Buffer.alloc(0); +let nextId = 0; +let loaded = mode !== 'ra'; +let firstLoad = true; +let pushes = 0; +// `null-frame` is `pull` with its fault. +const pulls = mode === 'pull' || mode === 'null-frame'; +const docs = new Map(); + +function send(message) { + if (mode === 'null-frame') process.stdout.write('Content-Length: 4\r\n\r\nnull'); + const body = Buffer.from(JSON.stringify({ jsonrpc: '2.0', ...message }), 'utf8'); + process.stdout.write(`Content-Length: ${body.length}\r\n\r\n`); + process.stdout.write(body); +} +const progress = (token, kind, title) => send({ method: '$/progress', params: { token, value: kind === 'begin' ? { kind, title } : { kind } } }); +const later = (ms, fn) => setTimeout(fn, ms); + +/** VS Code's spelling of a file URI: lower-case drive, colon encoded. */ +function vscodeUri(uri) { + return uri.replace(/^file:\/\/\/([A-Za-z]):/, (_, drive) => `file:///${drive.toLowerCase()}%3A`); +} + +function diagnosticsOf(text, source) { + const out = []; + text.split('\n').forEach((line, index) => { + const match = /\b(ERROR|WARN|HINT|ENV)\s+(\S.*)$/.exec(line); + if (!match) return; + const severity = { ERROR: 1, WARN: 2, HINT: 4, ENV: 1 }[match[1]]; + const message = match[1] === 'ENV' ? `${match[2]} is ${process.env[match[2]] === undefined ? 'unset' : 'set'}` : match[2]; + out.push({ + range: { start: { line: index, character: match.index }, end: { line: index, character: line.length } }, + severity, + message, + source, + code: 'E1', + }); + }); + return out; +} + +function changed(uri) { + const doc = docs.get(uri); + if (mode === 'push') { + const token = 'tls-load'; + if (firstLoad) { + firstLoad = false; + progress(token, 'begin', 'Initializing JS/TS language features…'); + later(80, () => progress(token, 'end')); + } + later(120, () => send({ method: 'textDocument/publishDiagnostics', params: { uri: vscodeUri(uri), diagnostics: diagnosticsOf(doc.text, 'typescript') } })); + } else if (mode === 'push-versioned' || mode === 'slow-first') { + const token = `pyright-${++nextId}`; + const reportsProgress = mode !== 'slow-first'; + if (reportsProgress) { + send({ id: nextId, method: 'window/workDoneProgress/create', params: { token } }); + progress(token, 'begin', ''); + } + const version = doc.version; + const text = doc.text; + later(mode === 'slow-first' ? (pushes++ === 0 ? 600 : 1000) : 60, () => { + if (reportsProgress) progress(token, 'end'); + send({ method: 'textDocument/publishDiagnostics', params: { uri: vscodeUri(uri), version, diagnostics: diagnosticsOf(text, 'Pyright') } }); + }); + } +} + +function saved(uri) { + if (mode !== 'ra') return; + const doc = docs.get(uri); + const token = 'rust-analyzer/flycheck/0'; + send({ id: ++nextId, method: 'window/workDoneProgress/create', params: { token } }); + progress(token, 'begin', 'cargo check'); + later(150, () => { + const checked = diagnosticsOf(doc.text, 'rustc').map((d) => ({ ...d, message: `${d.message} (as rustc words it)` })); + send({ method: 'textDocument/publishDiagnostics', params: { uri, version: doc.version, diagnostics: checked } }); + progress(token, 'end'); + send({ id: ++nextId, method: 'workspace/diagnostic/refresh' }); + }); +} + +function handle(message) { + const { id, method, params } = message; + if (method === undefined) return; // a response to one of ours + switch (method) { + case 'initialize': { + if (mode === 'mute') return; + const pull = pulls || mode === 'ra'; + send({ id, result: { capabilities: { textDocumentSync: 2, ...(pull ? { diagnosticProvider: { interFileDependencies: true, workspaceDiagnostics: false } } : {}) } } }); + return; + } + case 'initialized': + if (mode === 'push-versioned') { + send({ id: 0, method: 'workspace/configuration', params: { items: [{ section: 'python' }] } }); + } + return; + case 'textDocument/didOpen': { + const { uri, version, text } = params.textDocument; + docs.set(uri, { version, text }); + if (mode === 'ra' && !loaded) { + // The workspace loads under progress; until it has, a pull is empty. + progress('rustAnalyzer/Fetching', 'begin', 'Fetching'); + later(200, () => { + loaded = true; + progress('rustAnalyzer/Fetching', 'end'); + }); + } + changed(uri); + return; + } + case 'textDocument/didChange': { + const { uri, version } = params.textDocument; + docs.set(uri, { version, text: params.contentChanges.at(-1).text }); + changed(uri); + return; + } + case 'textDocument/didSave': + saved(params.textDocument.uri); + return; + case 'textDocument/diagnostic': { + if (!pulls && mode !== 'ra') { + send({ id, error: { code: -32601, message: 'Unhandled method textDocument/diagnostic' } }); + return; + } + const doc = docs.get(params.textDocument.uri); + const items = doc && loaded ? diagnosticsOf(doc.text, mode === 'ra' ? 'rust-analyzer' : 'ts') : []; + send({ id, result: { kind: 'full', items } }); + return; + } + case 'shutdown': + send({ id, result: null }); + return; + case 'exit': + process.exit(0); + return; + default: + if (id !== undefined) send({ id, error: { code: -32601, message: `Unhandled method ${method}` } }); + } +} + +process.stdin.on('data', (chunk) => { + buffer = Buffer.concat([buffer, chunk]); + for (;;) { + const end = buffer.indexOf('\r\n\r\n'); + if (end < 0) return; + const length = Number(/Content-Length: (\d+)/i.exec(buffer.subarray(0, end).toString())[1]); + if (buffer.length < end + 4 + length) return; + const message = JSON.parse(buffer.subarray(end + 4, end + 4 + length).toString('utf8')); + buffer = buffer.subarray(end + 4 + length); + handle(message); + } +}); +process.stdin.on('end', () => process.exit(0)); diff --git a/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts index 879b0ec5..b4e879e1 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts @@ -517,6 +517,71 @@ describe('PiSession background tasks', () => { }); }); +describe('PiSession file changes', () => { + const end = (toolCallId: string, toolName: string, details: unknown, isError = false) => ({ + type: 'tool_execution_end', + toolCallId, + toolName, + result: { content: [{ type: 'text', text: isError ? 'Could not find the text' : 'Done' }], details }, + isError, + }); + const diffs = (ctx: Parameters[0]) => entries(ctx).filter((entry) => entry.entryType === 'diff'); + + it('shows an edit as the diff of the patch Pi applied, context lines included', async () => { + const h = withDriver(); + const ctx = await ready(h); + h.fake.emit({ type: 'tool_execution_start', toolCallId: 'call_e', toolName: 'edit', args: { path: 'src/a.ts', edits: [{ oldText: 'b', newText: 'c' }] } }); + // The shape Pi's edit-diff.js formats: file headers, then one hunk. + const patch = '--- src/a.ts\n+++ src/a.ts\n@@ -1,3 +1,3 @@\n a\n-b\n+c\n d\n'; + h.fake.emit(end('call_e', 'edit', { diff: '…', patch, firstChangedLine: 2 })); + await ctx.waitFor((event) => event.type === 'entries' && event.entries.some((entry) => entry.entryType === 'diff')); + expect(diffs(ctx)).toEqual([ + expect.objectContaining({ + path: 'src/a.ts', + callId: 'call_e', + lines: [ + { type: 'context', text: 'a' }, + { type: 'del', text: 'b' }, + { type: 'add', text: 'c' }, + { type: 'context', text: 'd' }, + ], + }), + ]); + }); + + it('shows a write as its content added, and no card for a change that failed', async () => { + const h = withDriver(); + const ctx = await ready(h); + h.fake.emit({ type: 'tool_execution_start', toolCallId: 'call_w', toolName: 'write', args: { path: 'b.py', content: 'x = 1\ny = 2' } }); + h.fake.emit(end('call_w', 'write', undefined)); + h.fake.emit({ type: 'tool_execution_start', toolCallId: 'call_f', toolName: 'edit', args: { path: 'c.rs', edits: [] } }); + h.fake.emit(end('call_f', 'edit', undefined, true)); + await ctx.waitFor((event) => event.type === 'entries' && event.entries.some((entry) => entry.entryType === 'tool_result' && entry.callId === 'call_f')); + expect(diffs(ctx)).toEqual([ + expect.objectContaining({ path: 'b.py', callId: 'call_w', lines: [{ type: 'add', text: 'x = 1' }, { type: 'add', text: 'y = 2' }] }), + ]); + }); + + it("shows a sub-agent's edit under its parent's call", async () => { + const h = withDriver(); + const ctx = await ready(h); + const subagent = { label: 'general', parentCallId: 'call_task' }; + const update = (seq: number, event: Record) => ({ + type: 'tool_execution_update', + toolCallId: 'call_task', + toolName: 'task', + partialResult: { content: [], details: { subagent, seq, event } }, + }); + h.fake.emit({ type: 'tool_execution_start', toolCallId: 'call_task', toolName: 'task', args: { description: 'x', prompt: 'y', agent: 'general' } }); + h.fake.emit(update(1, { kind: 'tool_call', callId: 'c1', toolName: 'edit', args: { path: 'a.ts', edits: [] } })); + h.fake.emit(update(2, { kind: 'tool_result', callId: 'c1', text: 'Done', isError: false, patch: '--- a.ts\n+++ a.ts\n@@ -1 +1 @@\n-x\n+y\n' })); + await ctx.waitFor((event) => event.type === 'entries' && event.entries.some((entry) => entry.entryType === 'diff')); + expect(diffs(ctx)).toEqual([ + expect.objectContaining({ path: 'a.ts', callId: 'call_task/c1', subagent, lines: [{ type: 'del', text: 'x' }, { type: 'add', text: 'y' }] }), + ]); + }); +}); + describe('PiSession checklist', () => { it('shows a todo call as the checklist, not as a tool row', async () => { const h = withDriver(); diff --git a/packages/agent-host/src/drivers/pi/__tests__/piFakeModel.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piFakeModel.test.ts index 317d0b2e..34c593eb 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/piFakeModel.test.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/piFakeModel.test.ts @@ -18,7 +18,7 @@ * on purpose — a slow runner must not read as a failure. */ import { spawn, type ChildProcess } from 'node:child_process'; -import { existsSync } from 'node:fs'; +import { existsSync, readFileSync, symlinkSync, writeFileSync } from 'node:fs'; import { mkdtemp, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import * as path from 'node:path'; @@ -35,6 +35,8 @@ import type { StartSession } from '../../../sdk/types'; * this file may not import from outside its folder). */ const MODEL_SERVER = fileURLToPath(new URL('../../../../tools/fake-model.mjs', import.meta.url)); const FAKE_MODEL_ID = 'fake-model-1'; +/** The agent host's node_modules: TypeScript 5 and typescript-language-server. */ +const HOST_NODE_MODULES = fileURLToPath(new URL('../../../../node_modules', import.meta.url)); interface FakeModelServer { url: string; @@ -107,8 +109,10 @@ live('Pi against the fake model', () => { session = undefined; await model?.stop(); model = undefined; - await rm(home, { recursive: true, force: true }); - await rm(cwd, { recursive: true, force: true }); + // A language server Pi started is stopped as Pi exits, a moment after + // the session ends; until then Windows keeps its directory locked. + await rm(home, { recursive: true, force: true, maxRetries: 20, retryDelay: 250 }); + await rm(cwd, { recursive: true, force: true, maxRetries: 20, retryDelay: 250 }); }); /** A session bound to the local server the way the bridge binds one to a @@ -310,6 +314,45 @@ live('Pi against the fake model', () => { TURN_MS, ); + it( + "hands the project's TypeScript errors back with a write, and shows the write as a diff", + async () => { + // A TypeScript project as one is checked out: its node_modules holds + // TypeScript and typescript-language-server — here the agent host's + // own (devDependencies), linked in. Nothing comes from PATH. + writeFileSync(path.join(cwd, 'tsconfig.json'), '{"compilerOptions":{"strict":true,"noEmit":true}}'); + symlinkSync(HOST_NODE_MODULES, path.join(cwd, 'node_modules'), 'junction'); + const ctx = start({ permission: () => ({ outcome: 'selected', optionId: 'allow' }) }); + await turn(ctx, '/write src/a.ts const x: number = "s";\\nexport { x };'); + + expect(readFileSync(path.join(cwd, 'src', 'a.ts'), 'utf8')).toBe('const x: number = "s";\nexport { x };'); + const call = ctx.entries().find((entry) => entry.entryType === 'tool_call' && entry.toolName === 'write'); + const callId = call?.entryType === 'tool_call' ? call.callId : undefined; + expect(callId).toBeDefined(); + expect(ctx.entries()).toContainEqual( + expect.objectContaining({ + entryType: 'tool_result', + callId, + text: expect.stringContaining( + "typescript-language-server reports 1 error in src/a.ts:\nsrc/a.ts:1:7 error 2322: Type 'string' is not assignable to type 'number'.", + ), + }), + ); + expect(ctx.entries()).toContainEqual( + expect.objectContaining({ + entryType: 'diff', + callId, + path: 'src/a.ts', + lines: [ + { type: 'add', text: 'const x: number = "s";' }, + { type: 'add', text: 'export { x };' }, + ], + }), + ); + }, + TURN_MS, + ); + it( "sizes a gateway's model from Pi's own catalog, which the extension reads inside Pi", async () => { diff --git a/packages/agent-host/src/drivers/pi/__tests__/piLsp.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piLsp.test.ts new file mode 100644 index 00000000..d01dd032 --- /dev/null +++ b/packages/agent-host/src/drivers/pi/__tests__/piLsp.test.ts @@ -0,0 +1,342 @@ +/** + * The CodeDeck Pi extension's diagnostics: which server a changed file gets, + * what the model reads after an `edit` or `write`, and how each kind of + * server is waited for — against fakeLanguageServer.mjs, whose modes are the + * real servers' behaviour as observed (its header says which). + */ +import { spawn } from 'node:child_process'; +import { mkdtempSync, writeFileSync } from 'node:fs'; +import { homedir, tmpdir } from 'node:os'; +import * as path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, it } from 'vitest'; +import { mergeDiagnostics, type DiagnosticsResult, type SpawnServer } from '../extension/lsp/client'; +import { changedFile, formatDiagnostics, LSP_ENV, MAX_REPORTED, ProjectServers, registerDiagnostics } from '../extension/lsp/diagnostics'; +import { serverFor, type Machine } from '../extension/lsp/servers'; +import { PROVIDER_TOKEN_ENV } from '../extension/provider'; + +const FAKE_SERVER = fileURLToPath(new URL('./fakeLanguageServer.mjs', import.meta.url)); + +/** A path as the lookup spells it on this host (it uses the host's `path`). */ +const P = (file: string): string => path.resolve(file); + +/** A machine made of the files named, nothing else. `platform` decides only + * how PATH is split and searched. */ +function fakeMachine(files: Record, options: { platform?: NodeJS.Platform; env?: Record } = {}): Machine { + const normal = new Map(Object.entries(files).map(([file, text]) => [P(file), text])); + return { + isFile: (file) => normal.has(P(file)), + readText: (file) => normal.get(P(file)), + env: options.env ?? {}, + platform: options.platform ?? 'linux', + execPath: '/usr/bin/node', + }; +} + +describe('the server a changed file gets', () => { + it("runs typescript-language-server for a TypeScript up to 6, from the project's own node_modules, with Pi's Node", () => { + const m = fakeMachine({ + '/repo/node_modules/typescript/package.json': '{}', + '/repo/node_modules/typescript/lib/tsserver.js': '', + '/repo/node_modules/typescript-language-server/package.json': '{"bin":{"typescript-language-server":"./lib/cli.mjs"}}', + '/repo/node_modules/typescript-language-server/lib/cli.mjs': '', + '/repo/packages/web/tsconfig.json': '{}', + }); + expect(serverFor(P('/repo/packages/web/src/a.ts'), m)).toEqual({ + language: 'typescript', + languageId: 'typescript', + root: P('/repo/packages/web'), + server: { + name: 'typescript-language-server', + command: '/usr/bin/node', + args: [P('/repo/node_modules/typescript-language-server/lib/cli.mjs'), '--stdio'], + }, + }); + }); + + it("runs TypeScript 7's own server, which has no tsserver", () => { + const m = fakeMachine({ + '/p/package.json': '{}', + '/p/node_modules/typescript/package.json': '{"bin":{"tsc":"./bin/tsc"}}', + '/p/node_modules/typescript/bin/tsc': '', + '/usr/local/bin/typescript-language-server': '', + }, { env: { PATH: '/usr/local/bin' } }); + expect(serverFor(P('/p/a.tsx'), m)?.server).toEqual({ name: 'tsc --lsp', command: '/usr/bin/node', args: [P('/p/node_modules/typescript/bin/tsc'), '--lsp', '--stdio'] }); + expect(serverFor(P('/p/a.tsx'), m)?.languageId).toBe('typescriptreact'); + }); + + it("falls back to a typescript-language-server on PATH, an npm .cmd shim on Windows run through a shell", () => { + const m = fakeMachine( + // Keys joined as the lookup joins them, so the case holds on a Linux host too. + { [path.join('C:\\npm', 'typescript-language-server')]: '', [path.join('C:\\npm', 'typescript-language-server.cmd')]: '', 'C:\\w\\package.json': '{}' }, + { platform: 'win32', env: { Path: 'C:\\npm', PATHEXT: '.COM;.EXE;.BAT;.CMD' } }, + ); + expect(serverFor('C:\\w\\a.js', m)?.server).toEqual({ + name: 'typescript-language-server', + command: path.join('C:\\npm', 'typescript-language-server.cmd'), + args: ['--stdio'], + shell: true, + }); + }); + + it("prefers the project's virtualenv for Python, basedpyright before Pyright", () => { + const m = fakeMachine( + { + '/py/pyproject.toml': '', + '/py/.venv/bin/pyright-langserver': '', + '/py/.venv/bin/basedpyright-langserver': '', + '/usr/bin/pyright-langserver': '', + }, + { env: { PATH: '/usr/bin' } }, + ); + expect(serverFor(P('/py/pkg/mod.py'), m)).toMatchObject({ root: P('/py'), server: { name: 'basedpyright-langserver', command: P('/py/.venv/bin/basedpyright-langserver') } }); + }); + + it('checks Rust from the crate that holds the file, and gives no server to a language it has none for', () => { + const m = fakeMachine({ '/r/Cargo.toml': '', '/bin/rust-analyzer': '' }, { env: { PATH: '/bin' } }); + expect(serverFor(P('/r/src/main.rs'), m)).toMatchObject({ root: P('/r'), server: { name: 'rust-analyzer', command: path.join('/bin', 'rust-analyzer') } }); + expect(serverFor(P('/r/README.md'), m)).toBeUndefined(); + // Python here has no server at all: nothing is installed for it. + expect(serverFor(P('/r/tool.py'), m)).toBeUndefined(); + }); +}); + +describe('what the model reads', () => { + const result = (diagnostics: DiagnosticsResult['diagnostics'], settled = true): DiagnosticsResult => ({ diagnostics, settled }); + + it('lists errors and warnings with one-based positions, and says nothing about a clean file or hints', () => { + const text = formatDiagnostics( + 'src/a.ts', + 'typescript-language-server', + result([ + { line: 0, character: 6, severity: 1, message: "Type 'string' is not assignable\n to type 'number'.", code: '2322' }, + { line: 3, character: 0, severity: 2, message: 'Unused import' }, + { line: 4, character: 0, severity: 4, message: "'x' is declared but never read." }, + ]), + ); + expect(text).toBe( + "typescript-language-server reports 1 error, 1 warning in src/a.ts:\n" + + "src/a.ts:1:7 error 2322: Type 'string' is not assignable; to type 'number'.\n" + + 'src/a.ts:4:1 warning: Unused import', + ); + expect(formatDiagnostics('a.ts', 'x', result([{ line: 0, character: 0, severity: 4, message: 'hint' }]))).toBeUndefined(); + expect(formatDiagnostics('a.ts', 'x', result([]))).toBeUndefined(); + }); + + it('caps a long list, and says when the server had not finished', () => { + const many = Array.from({ length: MAX_REPORTED + 5 }, (_, i) => ({ line: i, character: 0, severity: 1, message: `e${i}` })); + const text = formatDiagnostics('a.rs', 'rust-analyzer', result(many, false))!; + expect(text.split('\n')[0]).toBe(`rust-analyzer reports ${MAX_REPORTED + 5} errors in a.rs (it had not finished checking; there may be more):`); + expect(text.split('\n')).toHaveLength(1 + MAX_REPORTED + 1); + expect(text.endsWith('… and 5 more')).toBe(true); + }); + + it("counts one problem once when two checks word it differently, keeping the first's wording", () => { + const own = { line: 1, character: 17, severity: 1, message: "expected u32, found &'static str", code: 'E0308', source: 'rust-analyzer' }; + const cargo = { ...own, message: 'mismatched types', source: 'rustc' }; + expect(mergeDiagnostics([own], [cargo])).toEqual([own]); + }); + + it('opens the file Pi changed: relative to the session, a leading @ dropped, ~ the home directory', () => { + const cwd = path.resolve('/work'); + expect(changedFile({ path: 'src/a.ts' }, cwd)).toBe(path.resolve(cwd, 'src/a.ts')); + expect(changedFile({ path: '@src/a.ts' }, cwd)).toBe(path.resolve(cwd, 'src/a.ts')); + expect(changedFile({ path: '~/x.py' }, cwd)).toBe(path.join(homedir(), 'x.py')); + expect(changedFile({}, cwd)).toBeUndefined(); + }); +}); + +describe('diagnostics after a change, per kind of server', () => { + /** A project whose one language (Rust, by its file name) has a server on + * PATH; whatever is started is the fake in `mode`. */ + function project(mode: string, deps: { env?: NodeJS.ProcessEnv; timeoutMs?: number; coldTimeoutMs?: number; kill?: (pid: number) => void } = {}) { + const dir = mkdtempSync(path.join(tmpdir(), 'codedeck-pi-lsp-')); + writeFileSync(path.join(dir, 'Cargo.toml'), ''); + const m = fakeMachine({ [path.join(dir, 'Cargo.toml')]: '', '/bin/rust-analyzer': '' }, { env: { PATH: '/bin' }, platform: 'linux' }); + // The fake machine's paths are POSIX; the real file lives in `dir`. + m.isFile = (file) => file === path.join(dir, 'Cargo.toml') || file === '/bin/rust-analyzer' || file === path.join('/bin', 'rust-analyzer'); + let spawned = 0; + let env: NodeJS.ProcessEnv | undefined; + const spawnFake: SpawnServer = (_server, cwd, childEnv) => { + spawned++; + env = childEnv; + return spawn(process.execPath, [FAKE_SERVER, mode], { cwd, env: childEnv, stdio: ['pipe', 'pipe', 'ignore'] }); + }; + const servers = new ProjectServers({ + machine: m, + spawn: spawnFake, + env: deps.env ?? {}, + timeoutMs: deps.timeoutMs ?? 5_000, + coldTimeoutMs: deps.coldTimeoutMs ?? deps.timeoutMs ?? 5_000, + ...(deps.kill ? { kill: deps.kill } : {}), + }); + const file = path.join(dir, 'main.rs'); + return { + servers, + spawned: () => spawned, + env: () => env, + async change(text: string, signal?: AbortSignal) { + writeFileSync(file, text); + return servers.check(file, dir, signal); + }, + }; + } + + it("reads a server that only pushes, unversioned, under VS Code's spelling of the file's URI (typescript-language-server)", async () => { + const p = project('push'); + try { + expect(await p.change('ok\nERROR bad type\n')).toBe('rust-analyzer reports 1 error in main.rs:\nmain.rs:2:1 error E1: bad type'); + // The next change is answered by the same process, about the new text. + expect(await p.change('ok\n')).toBeUndefined(); + expect(await p.change('WARN careful\n')).toMatch(/1 warning in main\.rs:\nmain\.rs:1:1 warning E1: careful/); + expect(p.spawned()).toBe(1); + } finally { + p.servers.stopAll(); + } + }); + + it('waits out the progress before a versioned push (Pyright)', async () => { + const p = project('push-versioned'); + try { + expect(await p.change('x\nERROR first\n')).toMatch(/main\.rs:2:1 error E1: first$/); + expect(await p.change('x\nx\nERROR second\n')).toMatch(/main\.rs:3:1 error E1: second$/); + } finally { + p.servers.stopAll(); + } + }); + + it("pulls from a server that only answers pulls (TypeScript 7's tsc --lsp)", async () => { + const p = project('pull'); + try { + expect(await p.change('ERROR pulled\n')).toMatch(/main\.rs:1:1 error E1: pulled$/); + } finally { + p.servers.stopAll(); + } + }); + + it("pulls again once a loading workspace and cargo check are done, each problem once (rust-analyzer)", async () => { + const p = project('ra'); + try { + // The first pull answers empty while the workspace loads. + expect(await p.change('fn main() {}\n ERROR expected u32\n')).toBe('rust-analyzer reports 1 error in main.rs:\nmain.rs:2:3 error E1: expected u32'); + } finally { + p.servers.stopAll(); + } + }); + + it('gives up on a server that cannot start, without trying it again for the project', async () => { + const p = project('crash'); + expect(await p.change('ERROR x\n')).toBeUndefined(); + expect(await p.change('ERROR x\n')).toBeUndefined(); + expect(p.spawned()).toBe(1); + }); + + it('answers by the deadline from a server that never reports', async () => { + const p = project('silent', { timeoutMs: 600 }); + try { + const started = Date.now(); + expect(await p.change('ERROR x\n')).toBeUndefined(); + expect(Date.now() - started).toBeLessThan(5_000); + } finally { + p.servers.stopAll(); + } + }); + + it('reads past a frame whose body is not an object, which must not throw inside Pi', async () => { + const p = project('null-frame'); + try { + expect(await p.change('ERROR still read\n')).toMatch(/main\.rs:1:1 error E1: still read$/); + } finally { + p.servers.stopAll(); + } + }); + + it("waits for this change's push, not a late one for the change before", async () => { + // The first push (600 ms) comes after its check gave up (400 ms), while + // the second check waits for its own (1000 ms): the late one must not + // end that wait, to be dropped then as an older version. + const p = project('slow-first', { coldTimeoutMs: 400, timeoutMs: 5_000 }); + try { + expect(await p.change('ERROR first\n')).toBeUndefined(); + expect(await p.change('x\nERROR second\n')).toMatch(/main\.rs:2:1 error E1: second$/); + } finally { + p.servers.stopAll(); + } + }); + + it('stops waiting when the turn is stopped, also for a server still starting', async () => { + const p = project('mute', { timeoutMs: 20_000 }); + const stop = new AbortController(); + setTimeout(() => stop.abort(), 200); + const started = Date.now(); + try { + expect(await p.change('ERROR x\n', stop.signal)).toBeUndefined(); + expect(Date.now() - started).toBeLessThan(5_000); + } finally { + p.servers.stopAll(); + } + }); + + it('stops a server that is still starting when Pi exits', async () => { + const killed: number[] = []; + const p = project('mute', { + timeoutMs: 20_000, + kill: (pid) => { + killed.push(pid); + process.kill(pid); + }, + }); + const stop = new AbortController(); + const checking = p.change('ERROR x\n', stop.signal); + await new Promise((resolve) => setTimeout(resolve, 300)); + p.servers.stopAll(); + stop.abort(); + await checking; + expect(killed).toHaveLength(1); + }); + + it("starts the server without the provider profile's token", async () => { + const p = project('pull', { env: { [PROVIDER_TOKEN_ENV]: 'sk-secret', HOME_MARK: 'kept' } }); + try { + expect(await p.change(`ENV ${PROVIDER_TOKEN_ENV}\nENV HOME_MARK\n`)).toBe( + `rust-analyzer reports 2 errors in main.rs:\nmain.rs:1:1 error E1: ${PROVIDER_TOKEN_ENV} is unset\nmain.rs:2:1 error E1: HOME_MARK is set`, + ); + expect(p.env()?.[PROVIDER_TOKEN_ENV]).toBeUndefined(); + } finally { + p.servers.stopAll(); + } + }); +}); + +describe('the tool_result hook', () => { + type Handler = (event: Record, ctx: { cwd: string; signal?: AbortSignal }) => Promise; + function hooked(env: NodeJS.ProcessEnv = {}) { + let handler: Handler | undefined; + const pi = { on: (event: string, fn: Handler) => (event === 'tool_result' ? (handler = fn) : undefined) }; + const checked: string[] = []; + const servers = registerDiagnostics(pi as never, { env }); + if (servers) { + servers.check = async (file) => { + checked.push(file); + return 'rust-analyzer reports 1 error in main.rs:\nmain.rs:1:1 error E1: x'; + }; + } + return { handler, checked }; + } + + it("adds the report after the tool's own result, for a successful edit or write only", async () => { + const { handler, checked } = hooked(); + const cwd = path.resolve('/w'); + const content = [{ type: 'text', text: 'Successfully replaced 1 block in src/main.rs.' }]; + expect(await handler!({ toolName: 'edit', isError: false, input: { path: 'src/main.rs' }, content }, { cwd })).toEqual({ + content: [...content, { type: 'text', text: 'rust-analyzer reports 1 error in main.rs:\nmain.rs:1:1 error E1: x' }], + }); + expect(await handler!({ toolName: 'edit', isError: true, input: { path: 'a.rs' }, content }, { cwd })).toBeUndefined(); + expect(await handler!({ toolName: 'bash', isError: false, input: { command: 'x' }, content }, { cwd })).toBeUndefined(); + expect(checked).toEqual([path.resolve(cwd, 'src/main.rs')]); + }); + + it(`registers nothing with ${LSP_ENV}=off`, () => { + expect(hooked({ [LSP_ENV]: 'off' }).handler).toBeUndefined(); + }); +}); diff --git a/packages/agent-host/src/drivers/pi/driver.ts b/packages/agent-host/src/drivers/pi/driver.ts index 84838bc8..68904340 100644 --- a/packages/agent-host/src/drivers/pi/driver.ts +++ b/packages/agent-host/src/drivers/pi/driver.ts @@ -79,6 +79,7 @@ import { CODEDECK_SAFE_TOOLS, PI_EDIT_TOOLS, PI_READ_ONLY_TOOLS, + fileChangeDiff, isReadOnlyShellCall, piExtraTools, piTool, @@ -196,6 +197,9 @@ export class PiSession implements DriverSession { private sessionMode: string; private readonly allowedTools = new Set(); private readonly toolFacts = new Map }>(); + /** The same for a sub-agent's calls, by the id the phone knows them by + * (`/`), for the diff card of a file it changed. */ + private readonly subagentFacts = new Map }>(); /** Calls shown as their own card (CARD_TOOLS) rather than a tool row. */ private readonly cardCalls = new Set(); /** The last progress event shown per sub-agent, by its `task` call. */ @@ -553,8 +557,10 @@ export class PiSession implements DriverSession { private toolEnd(record: PiRecord): void { const callId = str(record.toolCallId); if (callId === undefined) return; + const facts = this.toolFacts.get(callId); this.toolFacts.delete(callId); this.subagentSeq.delete(callId); + for (const id of this.subagentFacts.keys()) if (id.startsWith(`${callId}/`)) this.subagentFacts.delete(id); const text = resultText(record.result); if (this.cardCalls.delete(callId)) { // The card was the call; only a failure is news (the question went @@ -571,6 +577,8 @@ export class PiSession implements DriverSession { timestamp: now(), }, ]); + const diff = record.isError === true || !facts ? null : fileChangeDiff(facts.name, facts.input, recordOf(record.result)?.details); + if (diff) this.emit([{ entryType: 'diff', ...diff, callId, timestamp: now() }]); } /** @@ -599,6 +607,7 @@ export class PiSession implements DriverSession { } else if (event.kind === 'tool_call') { const call = piTool(event.toolName, event.args, this.reportUnknownTool); const locations = toolLocations(event.args); + if (PI_EDIT_TOOLS.has(event.toolName)) this.subagentFacts.set(childId(event.callId), { name: event.toolName, input: event.args }); this.emit([ { entryType: 'tool_call', @@ -623,6 +632,10 @@ export class PiSession implements DriverSession { timestamp, }, ]); + const facts = this.subagentFacts.get(childId(event.callId)); + this.subagentFacts.delete(childId(event.callId)); + const diff = event.isError || !facts ? null : fileChangeDiff(facts.name, facts.input, { patch: event.patch }); + if (diff) this.emit([{ entryType: 'diff', ...diff, callId: childId(event.callId), ...tag, timestamp }]); } } diff --git a/packages/agent-host/src/drivers/pi/extension/index.ts b/packages/agent-host/src/drivers/pi/extension/index.ts index 1c3a4211..2cb3d870 100644 --- a/packages/agent-host/src/drivers/pi/extension/index.ts +++ b/packages/agent-host/src/drivers/pi/extension/index.ts @@ -10,14 +10,16 @@ * Nothing of the host exists there. * * A sub-agent's Pi (task.ts) loads it too, in a smaller form: the gate and - * the provider profile, and `web_fetch` for a general sub-agent — nothing + * the provider profile, and `web_fetch` and the diagnostics of its file + * changes for a general sub-agent — nothing * that talks to the user (questions, plans, the checklist), starts work that * outlives it (background shells), or writes the project's notes. */ import { getBuiltinModels, getBuiltinProviders } from '@earendil-works/pi-ai/providers/all'; import { getPowerShellConfig, getShellConfig, type ExtensionAPI } from '@earendil-works/pi-coding-agent'; import { registerAsk } from './ask'; -import { registerBackground } from './background'; +import { killTree, registerBackground } from './background'; +import { registerDiagnostics } from './lsp/diagnostics'; import type { CatalogModel } from './catalog'; import { installGate } from './gate'; import { registerMemory } from './memory'; @@ -53,10 +55,14 @@ export default function codedeckPi(pi: ExtensionAPI): void { const subagent = process.env[SUBAGENT_ENV]; if (subagent) { - if (subagent === 'general') registerWebFetch(pi); + if (subagent === 'general') { + registerWebFetch(pi); + registerDiagnostics(pi, { kill: killTree }); + } return; } + registerDiagnostics(pi, { kill: killTree }); registerTodo(pi); registerAsk(pi); registerPlanMode(pi); diff --git a/packages/agent-host/src/drivers/pi/extension/lsp/client.ts b/packages/agent-host/src/drivers/pi/extension/lsp/client.ts new file mode 100644 index 00000000..2ded4b7e --- /dev/null +++ b/packages/agent-host/src/drivers/pi/extension/lsp/client.ts @@ -0,0 +1,470 @@ +/** + * One language server process and the diagnostics it gives for a file just + * changed. + * + * Servers do not agree on how they deliver diagnostics, nor on when they are + * done, so the rule below is the one that held for each of them when run + * against the real binary: + * + * - typescript-language-server pushes (`publishDiagnostics`), with no + * version, about 0.4 s after a change (1.1 s cold), and reports its + * project load as `$/progress`. + * - TypeScript 7's `tsc --lsp` only answers a pull (`textDocument/diagnostic`), + * at once. + * - Pyright and basedpyright advertise no pull to this client and push, with + * the version, about 0.25 s after a change (1.3–1.9 s cold), between a + * `$/progress`; their messages follow `initialize`'s `locale`. + * - rust-analyzer answers a pull with its own diagnostics, but with an empty + * list while it still loads the workspace (≈4–10 s cold, under + * `$/progress`); `cargo check`'s come only as a push after its + * `rust-analyzer/flycheck/0` progress ends, and it then asks for a re-pull + * (`workspace/diagnostic/refresh`, a request with no params). + * + * So, after sending the change: pull when the server can; then wait until no + * progress is open and nothing has arrived for QUIET_MS — and, for a server + * that only pushes, until its push for this change came; pull again when + * the server did work meanwhile; the answer is the pulled list together with + * the push, each problem once. A deadline bounds all of it: whatever has + * arrived by then is the answer, marked unsettled. + */ +import { spawn, type ChildProcess } from 'node:child_process'; +import * as path from 'node:path'; +import { fileURLToPath, pathToFileURL } from 'node:url'; +import type { ServerCommand } from './servers'; + +/** How long nothing may arrive, with no progress open, before the server + * counts as done. Longer than the gaps between rust-analyzer's load phases + * (up to ~0.2 s seen), short enough to cost little on every edit. */ +export const QUIET_MS = 300; + +/** One problem in a file, positions zero-based as LSP has them. */ +export interface Diagnostic { + line: number; + character: number; + /** 1 error, 2 warning, 3 information, 4 hint; absent = the server did not say. */ + severity?: number; + message: string; + source?: string; + code?: string; +} + +export interface DiagnosticsResult { + diagnostics: Diagnostic[]; + /** False when the deadline came before the server was done. */ + settled: boolean; +} + +/** What starting a process looks like, so a test can hand in its own. */ +export type SpawnServer = (server: ServerCommand, cwd: string, env: NodeJS.ProcessEnv) => ChildProcess; + +export const spawnServer: SpawnServer = (server, cwd, env) => + server.shell + ? // A `.cmd` shim needs cmd.exe; the arguments are this file's own constants. + spawn(`"${server.command}"`, server.args, { cwd, env, shell: true, stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true }) + : spawn(server.command, server.args, { cwd, env, stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true }); + +interface Pending { + resolve(value: unknown): void; + reject(error: Error): void; +} + +interface Published { + version?: number; + diagnostics: Diagnostic[]; + /** The activity count when it arrived, to tell it from an older push. */ + at: number; +} + +/** LSP error codes after which a pull is worth asking again. */ +const CONTENT_MODIFIED = -32801; +const SERVER_CANCELLED = -32802; + +/** + * The file a document URI names, in one spelling. Servers do not echo the + * URI they were sent: Pyright and typescript-language-server answer + * `file:///C:/x` as `file:///c%3A/x` (VS Code's spelling), so documents are + * keyed by path — and on Windows, whose paths ignore case, by its lower case. + */ +export function documentKey(uri: string): string | undefined { + try { + const file = path.normalize(fileURLToPath(uri)); + return process.platform === 'win32' ? file.toLowerCase() : file; + } catch { + return undefined; + } +} + +function toDiagnostic(raw: unknown): Diagnostic | undefined { + if (typeof raw !== 'object' || raw === null) return undefined; + const d = raw as { range?: { start?: { line?: unknown; character?: unknown } }; severity?: unknown; message?: unknown; source?: unknown; code?: unknown }; + const start = d.range?.start; + if (typeof d.message !== 'string' || typeof start?.line !== 'number') return undefined; + return { + line: start.line, + character: typeof start.character === 'number' ? start.character : 0, + ...(typeof d.severity === 'number' ? { severity: d.severity } : {}), + message: d.message, + ...(typeof d.source === 'string' && d.source ? { source: d.source } : {}), + ...(typeof d.code === 'string' || typeof d.code === 'number' ? { code: String(d.code) } : {}), + }; +} + +function diagnosticsOf(list: unknown): Diagnostic[] { + return Array.isArray(list) ? list.flatMap((item) => toDiagnostic(item) ?? []) : []; +} + +/** Each problem once: a pull and a push often carry the same one, and + * rust-analyzer's own check and `cargo check` word one error two ways + * under the same code at the same place (the first list's wording wins). */ +export function mergeDiagnostics(...lists: Diagnostic[][]): Diagnostic[] { + const seen = new Set(); + const out: Diagnostic[] = []; + for (const d of lists.flat()) { + const key = `${d.line}:${d.character}:${d.code !== undefined ? `code ${d.code}` : d.message}`; + if (seen.has(key)) continue; + seen.add(key); + out.push(d); + } + return out.sort((a, b) => (a.severity ?? 1) - (b.severity ?? 1) || a.line - b.line || a.character - b.character); +} + +export class LspClient { + private child: ChildProcess | undefined; + private buffer = Buffer.alloc(0); + private nextId = 0; + private readonly pending = new Map(); + private readonly versions = new Map(); + private readonly published = new Map(); + private readonly progress = new Set(); + /** Bumped by everything the server sends; `wake` fires with it. */ + private activity = 0; + private lastActivityAt = 0; + private refreshAt = -1; + private wake: (() => void) | undefined; + private pull = false; + private queue: Promise = Promise.resolve(); + /** Set once the process is gone, with why. */ + dead: string | undefined; + lastUsed = Date.now(); + + constructor( + readonly server: ServerCommand, + readonly root: string, + private readonly env: NodeJS.ProcessEnv, + private readonly spawnFn: SpawnServer = spawnServer, + private readonly clock: () => number = Date.now, + /** Stops a process tree: a `.cmd` shim's server is cmd.exe's child, + * which killing cmd.exe leaves running. */ + private readonly kill?: (pid: number) => void, + ) {} + + /** Start the process and initialize it; rejects when either fails. */ + async start(timeoutMs: number): Promise { + const child = this.spawnFn(this.server, this.root, this.env); + this.child = child; + child.on('error', (error) => this.died(`could not start: ${error.message}`)); + child.on('exit', (code) => this.died(`exited (${code ?? 'signal'})`)); + child.stdin?.on('error', () => {}); + child.stdout?.on('data', (chunk: Buffer) => this.read(chunk)); + const rootUri = pathToFileURL(this.root).href; + const result = (await this.withTimeout( + this.request('initialize', { + // Pi, as the parent LSP says a server should exit without. + processId: process.pid, + rootUri, + rootPath: this.root, + workspaceFolders: [{ uri: rootUri, name: 'project' }], + // Pyright otherwise speaks the machine's locale; the model reads it. + locale: 'en', + capabilities: { + window: { workDoneProgress: true }, + workspace: { configuration: true }, + textDocument: { + synchronization: { didSave: true }, + publishDiagnostics: { versionSupport: true }, + }, + }, + }), + timeoutMs, + 'initialize', + )) as { capabilities?: { diagnosticProvider?: unknown } } | null; + this.pull = Boolean(result?.capabilities?.diagnosticProvider); + this.notify('initialized', {}); + } + + /** + * Hand the server the file's new text (open on first sight, a whole-text + * change after) and save it, then collect what it says about it. Calls on + * one server run one at a time: a second edit waits for the first's answer. + */ + diagnose(file: string, languageId: string, text: string, deadline: number, signal?: AbortSignal): Promise { + const run = this.queue.then(() => this.diagnoseNow(file, languageId, text, deadline, signal)); + this.queue = run.catch(() => undefined); + return run; + } + + /** Ask the server to shut down and exit, and kill it a second later if it + * has not (a `.cmd` shim's server only hears the request). */ + close(): void { + const child = this.child; + if (!child || this.dead) { + this.child = undefined; + return; + } + try { + this.request('shutdown', null).catch(() => {}); + this.notify('exit', null); + } catch { + // Its pipe is already gone. + } + this.dead = 'closed'; + this.child = undefined; + setTimeout(() => { + if (child.exitCode !== null || child.signalCode !== null) return; + if (this.kill && child.pid !== undefined) this.kill(child.pid); + else child.kill(); + }, 1_000).unref(); + } + + /** The process, for a hard stop when Pi itself exits. */ + get pid(): number | undefined { + return this.child?.pid; + } + + private async diagnoseNow(file: string, languageId: string, text: string, deadline: number, signal?: AbortSignal): Promise { + if (this.dead) throw new Error(`${this.server.name} ${this.dead}`); + this.lastUsed = this.clock(); + const uri = pathToFileURL(file).href; + const key = documentKey(uri) ?? uri; + const version = (this.versions.get(key) ?? 0) + 1; + this.versions.set(key, version); + const mark = this.activity; + this.touch(); + if (version === 1) { + this.notify('textDocument/didOpen', { textDocument: { uri, languageId, version, text } }); + } else { + this.notify('textDocument/didChange', { textDocument: { uri, version }, contentChanges: [{ text }] }); + } + this.notify('textDocument/didSave', { textDocument: { uri }, text }); + + let pulled = this.pull ? await this.pullDiagnostics(uri, deadline) : []; + const afterPull = this.activity; + const settled = await this.settle(key, mark, version, deadline, signal); + // The server worked after the first pull (a project load, `cargo check`) + // or asked for a re-pull: what it says now is the fresher answer. + if (this.pull && (this.activity > afterPull || this.refreshAt > mark) && this.clock() < deadline) { + pulled = await this.pullDiagnostics(uri, deadline); + } + const push = this.published.get(key); + const pushed = push && this.answers(push, mark, version) ? push.diagnostics : []; + return { diagnostics: mergeDiagnostics(pulled, pushed), settled }; + } + + private async pullDiagnostics(uri: string, deadline: number): Promise { + for (let attempt = 0; attempt < 3; attempt++) { + const left = deadline - this.clock(); + if (left <= 0) return []; + try { + const report = (await this.withTimeout(this.request('textDocument/diagnostic', { textDocument: { uri } }), left, 'pull')) as { + items?: unknown; + } | null; + return diagnosticsOf(report?.items); + } catch (error) { + const code = (error as { code?: unknown }).code; + if (code !== CONTENT_MODIFIED && code !== SERVER_CANCELLED) return []; + } + } + return []; + } + + /** Whether a push is about this change: it came after the change was sent + * and, when the server says which version it read, not an older one (a + * push for the last change can arrive late, after this one was sent). */ + private answers(push: Published, mark: number, version: number): boolean { + return push.at > mark && (push.version === undefined || push.version >= version); + } + + /** Wait for the server to go quiet (see the file comment). */ + private settle(key: string, mark: number, version: number, deadline: number, signal?: AbortSignal): Promise { + return new Promise((resolve) => { + let timer: NodeJS.Timeout | undefined; + const finish = (settled: boolean) => { + if (timer) clearTimeout(timer); + this.wake = undefined; + signal?.removeEventListener('abort', aborted); + resolve(settled); + }; + const aborted = () => finish(false); + const check = () => { + if (timer) clearTimeout(timer); + if (this.dead) return finish(false); + const now = this.clock(); + if (now >= deadline) return finish(false); + const push = this.published.get(key); + const pushed = push !== undefined && this.answers(push, mark, version); + const quietFor = now - this.lastActivityAt; + if (this.progress.size === 0 && quietFor >= QUIET_MS && (this.pull || pushed)) return finish(true); + const until = this.progress.size === 0 && (this.pull || pushed) ? QUIET_MS - quietFor : deadline - now; + timer = setTimeout(check, Math.max(1, Math.min(until, deadline - now))); + }; + if (signal?.aborted) return finish(false); + signal?.addEventListener('abort', aborted, { once: true }); + this.wake = check; + check(); + }); + } + + private touch(): void { + this.activity++; + this.lastActivityAt = this.clock(); + } + + private died(reason: string): void { + if (!this.dead) this.dead = reason; + for (const pending of this.pending.values()) pending.reject(new Error(`${this.server.name} ${reason}`)); + this.pending.clear(); + this.wake?.(); + } + + private send(message: Record): void { + const stdin = this.child?.stdin; + if (!stdin || this.dead) throw new Error(`${this.server.name} ${this.dead ?? 'is not running'}`); + const body = Buffer.from(JSON.stringify({ jsonrpc: '2.0', ...message }), 'utf8'); + stdin.write(`Content-Length: ${body.length}\r\n\r\n`); + stdin.write(body); + } + + private notify(method: string, params: unknown): void { + this.send({ method, params }); + } + + private request(method: string, params: unknown): Promise { + const id = ++this.nextId; + return new Promise((resolve, reject) => { + this.pending.set(id, { resolve, reject }); + try { + this.send({ id, method, params }); + } catch (error) { + this.pending.delete(id); + reject(error as Error); + } + }); + } + + private withTimeout(promise: Promise, ms: number, what: string): Promise { + return new Promise((resolve, reject) => { + const timer = setTimeout(() => reject(new Error(`${this.server.name} did not answer ${what} in time`)), Math.max(1, ms)); + promise.then( + (value) => { + clearTimeout(timer); + resolve(value); + }, + (error: unknown) => { + clearTimeout(timer); + reject(error as Error); + }, + ); + }); + } + + private read(chunk: Buffer): void { + this.buffer = Buffer.concat([this.buffer, chunk]); + for (;;) { + const headerEnd = this.buffer.indexOf('\r\n\r\n'); + if (headerEnd < 0) return; + const length = /content-length:\s*(\d+)/i.exec(this.buffer.subarray(0, headerEnd).toString('ascii')); + if (!length) { + // Not a frame header: drop it and look for the next one. + this.buffer = this.buffer.subarray(headerEnd + 4); + continue; + } + const start = headerEnd + 4; + const end = start + Number(length[1]); + if (this.buffer.length < end) return; + const body = this.buffer.subarray(start, end).toString('utf8'); + this.buffer = this.buffer.subarray(end); + let message: unknown; + try { + message = JSON.parse(body); + } catch { + continue; + } + // This runs in Pi's own process: nothing a server sends may throw here. + if (typeof message !== 'object' || message === null || Array.isArray(message)) continue; + try { + this.handle(message as Record); + } catch { + // A frame this client cannot read says nothing it can use. + } + } + } + + private handle(message: Record): void { + const { id, method } = message; + if (typeof method === 'string' && id !== undefined) { + this.answer(id, method, message.params); + } else if (typeof id === 'number') { + const pending = this.pending.get(id); + this.pending.delete(id); + const error = message.error as { code?: unknown; message?: unknown } | undefined; + if (error) pending?.reject(Object.assign(new Error(String(error.message ?? 'error')), { code: error.code })); + else pending?.resolve(message.result); + return; + } else if (method === 'textDocument/publishDiagnostics') { + const params = message.params as { uri?: unknown; version?: unknown; diagnostics?: unknown } | undefined; + const key = typeof params?.uri === 'string' ? documentKey(params.uri) : undefined; + if (key && params) { + this.touch(); + this.published.set(key, { + ...(typeof params.version === 'number' ? { version: params.version } : {}), + diagnostics: diagnosticsOf(params.diagnostics), + at: this.activity, + }); + } + } else if (method === '$/progress') { + const params = message.params as { token?: unknown; value?: { kind?: unknown } } | undefined; + const token = String(params?.token); + if (params?.value?.kind === 'begin') this.progress.add(token); + else if (params?.value?.kind === 'end') this.progress.delete(token); + this.touch(); + } else { + // Logs and other notices say nothing about this file's state. + return; + } + this.wake?.(); + } + + /** A request from the server. Only what the servers above ask is + * answered with a result; anything else is refused as unknown. */ + private answer(id: unknown, method: string, params: unknown): void { + let result: unknown = null; + switch (method) { + case 'workspace/configuration': { + const items = (params as { items?: unknown } | undefined)?.items; + result = Array.isArray(items) ? items.map(() => null) : []; + break; + } + case 'workspace/diagnostic/refresh': + this.touch(); + this.refreshAt = this.activity; + this.wake?.(); + break; + case 'window/workDoneProgress/create': + case 'client/registerCapability': + case 'client/unregisterCapability': + break; + default: + try { + this.send({ id, error: { code: -32601, message: `${method} is not supported` } }); + } catch { + // Gone. + } + return; + } + try { + this.send({ id, result }); + } catch { + // Gone. + } + } +} diff --git a/packages/agent-host/src/drivers/pi/extension/lsp/diagnostics.ts b/packages/agent-host/src/drivers/pi/extension/lsp/diagnostics.ts new file mode 100644 index 00000000..db663746 --- /dev/null +++ b/packages/agent-host/src/drivers/pi/extension/lsp/diagnostics.ts @@ -0,0 +1,266 @@ +/** + * LSP diagnostics on Pi's file changes: after an `edit` or `write` succeeds, + * the project's language server checks the file, and what it reports is + * added to the tool's result — so the model sees a type error it just made + * in the same turn, and the phone shows it under the call like any result. + * + * One server per project and language, started on the first change to one of + * its files and kept for the session (a cold start costs a second for + * TypeScript or Python, several for rust-analyzer; a warm check a fraction of + * that), stopped after IDLE_MS unused and when Pi exits. A server that is + * missing, fails to start or dies is not tried again for that project this + * session. Only errors and warnings, of the changed file only, are reported: + * hints and the rest of the project would cost the model's context on every + * edit. + */ +import { readFile } from 'node:fs/promises'; +import { homedir } from 'node:os'; +import * as path from 'node:path'; +import type { ExtensionAPI } from '@earendil-works/pi-coding-agent'; +import { PROVIDER_ENV, PROVIDER_TOKEN_ENV } from '../provider'; +import { LspClient, spawnServer, type DiagnosticsResult, type SpawnServer } from './client'; +import { machine, serverFor, type Machine, type ServerChoice } from './servers'; + +/** The longest an edit waits for its diagnostics from a running server. */ +export const DIAGNOSTICS_TIMEOUT_MS = 10_000; +/** The same for the first change that starts a server: its project load + * comes first (rust-analyzer took ~10 s for a one-file crate). */ +export const COLD_TIMEOUT_MS = 30_000; +/** A server unused this long is stopped; the next change starts it again. */ +export const IDLE_MS = 10 * 60_000; +/** The most problems one result lists. */ +export const MAX_REPORTED = 20; +/** A file larger than this is not sent to a server. */ +const MAX_FILE_BYTES = 2 * 1024 * 1024; + +/** Set to `off` in the bridge's environment to turn the checks off. */ +export const LSP_ENV = 'CODEDECK_PI_LSP'; + +export interface DiagnosticsDeps { + machine?: Machine; + spawn?: SpawnServer; + env?: NodeJS.ProcessEnv; + clock?: () => number; + timeoutMs?: number; + coldTimeoutMs?: number; + /** Stops a server's process tree when Pi exits. */ + kill?: (pid: number) => void; +} + +/** The file a Pi file tool changed, resolved the way Pi resolves it + * (`resolveToCwd`): a leading `@` dropped, `~` the home directory. */ +export function changedFile(input: Record, cwd: string): string | undefined { + let file = typeof input.path === 'string' ? input.path.trim() : ''; + if (!file) return undefined; + if (file.startsWith('@')) file = file.slice(1); + if (file === '~' || file.startsWith('~/') || file.startsWith('~\\')) file = path.join(homedir(), file.slice(1)); + return path.resolve(cwd, file); +} + +const SEVERITY: Record = { 1: 'error', 2: 'warning' }; + +/** What the model reads after the tool's own result, or `undefined` when + * there is nothing to report. */ +export function formatDiagnostics(display: string, server: string, result: DiagnosticsResult): string | undefined { + // A diagnostic without a severity is the client's to judge (LSP); an + // error is the safe reading. + const problems = result.diagnostics.filter((d) => (d.severity ?? 1) <= 2); + if (problems.length === 0) return undefined; + const errors = problems.filter((d) => (d.severity ?? 1) === 1).length; + const warnings = problems.length - errors; + const counts = [errors ? `${errors} error${errors === 1 ? '' : 's'}` : '', warnings ? `${warnings} warning${warnings === 1 ? '' : 's'}` : ''] + .filter(Boolean) + .join(', '); + const lines = problems.slice(0, MAX_REPORTED).map((d) => { + const code = d.code ? ` ${d.code}` : ''; + const message = d.message.trim().replace(/\s*\n\s*/g, '; '); + return `${display}:${d.line + 1}:${d.character + 1} ${SEVERITY[d.severity ?? 1]}${code}: ${message}`; + }); + if (problems.length > MAX_REPORTED) lines.push(`… and ${problems.length - MAX_REPORTED} more`); + const unfinished = result.settled ? '' : ` (it had not finished checking; there may be more)`; + return `${server} reports ${counts} in ${display}${unfinished}:\n${lines.join('\n')}`; +} + +/** `promise`, or `undefined` as soon as `signal` aborts: a stopped turn + * does not wait for a server (which goes on starting, for the next one). */ +function untilAborted(promise: Promise, signal: AbortSignal | undefined): Promise { + if (!signal) return promise; + if (signal.aborted) return Promise.resolve(undefined); + return new Promise((resolve, reject) => { + const aborted = () => resolve(undefined); + signal.addEventListener('abort', aborted, { once: true }); + promise.then( + (value) => { + signal.removeEventListener('abort', aborted); + resolve(value); + }, + (error: unknown) => { + signal.removeEventListener('abort', aborted); + reject(error as Error); + }, + ); + }); +} + +export class ProjectServers { + private readonly clients = new Map(); + /** Servers still initializing: stopped with the rest when Pi exits. */ + private readonly launching = new Set(); + private readonly starting = new Map>(); + private readonly broken = new Set(); + private readonly machine: Machine; + private readonly spawn: SpawnServer; + private readonly env: NodeJS.ProcessEnv; + private readonly clock: () => number; + private readonly timeoutMs: number; + private readonly coldTimeoutMs: number; + private readonly kill: ((pid: number) => void) | undefined; + private sweeper: NodeJS.Timeout | undefined; + + constructor(deps: DiagnosticsDeps = {}) { + // A server runs the project's build (`cargo check` runs build scripts): + // it gets Pi's environment without the provider profile's token. + const env = { ...(deps.env ?? process.env) }; + delete env[PROVIDER_ENV]; + delete env[PROVIDER_TOKEN_ENV]; + this.env = env; + this.machine = deps.machine ?? machine(env); + this.spawn = deps.spawn ?? spawnServer; + this.clock = deps.clock ?? Date.now; + this.timeoutMs = deps.timeoutMs ?? DIAGNOSTICS_TIMEOUT_MS; + this.coldTimeoutMs = deps.coldTimeoutMs ?? COLD_TIMEOUT_MS; + this.kill = deps.kill; + } + + /** The diagnostics of a file just changed, as the model reads them, or + * `undefined` when there is no server for it or nothing to report. */ + async check(file: string, cwd: string, signal?: AbortSignal): Promise { + if (signal?.aborted) return undefined; + const choice = serverFor(file, this.machine); + if (!choice) return undefined; + const running = this.clients.get(this.key(choice)); + const warm = running !== undefined && running.dead === undefined; + const deadline = this.clock() + (warm ? this.timeoutMs : this.coldTimeoutMs); + const client = await untilAborted(this.client(choice, deadline), signal); + if (!client) return undefined; + let text: string; + try { + const buffer = await readFile(file); + if (buffer.length > MAX_FILE_BYTES) return undefined; + text = buffer.toString('utf8'); + } catch { + return undefined; + } + let result: DiagnosticsResult | undefined; + try { + result = await untilAborted(client.diagnose(file, choice.languageId, text, deadline, signal), signal); + if (!result) return undefined; + } catch { + this.drop(choice, client); + return undefined; + } + if (client.dead) this.drop(choice, client); + const relative = path.relative(cwd, file); + const parts = relative.split(path.sep); + const display = relative && parts[0] !== '..' && !path.isAbsolute(relative) ? parts.join('/') : file; + return formatDiagnostics(display, client.server.name, result); + } + + /** Every server stopped (Pi is exiting, or the session ended). */ + stopAll(kill: ((pid: number) => void) | undefined = this.kill): void { + if (this.sweeper) clearInterval(this.sweeper); + this.sweeper = undefined; + for (const client of [...this.clients.values(), ...this.launching]) { + const pid = client.pid; + if (kill && pid !== undefined) kill(pid); + else client.close(); + } + this.clients.clear(); + this.launching.clear(); + } + + /** One server per server and project; on Windows `C:\repo` and `c:\repo` are + * one project. */ + private key(choice: ServerChoice): string { + const root = this.machine.platform === 'win32' ? choice.root.toLowerCase() : choice.root; + return `${choice.server.name}\0${root}`; + } + + private drop(choice: ServerChoice, client: LspClient): void { + const key = this.key(choice); + if (this.clients.get(key) === client) this.clients.delete(key); + this.broken.add(key); + client.close(); + } + + private async client(choice: ServerChoice, deadline: number): Promise { + const key = this.key(choice); + if (this.broken.has(key)) return undefined; + const running = this.clients.get(key); + if (running && !running.dead) return running; + // Two parallel edits in one project share one start. + let starting = this.starting.get(key); + if (!starting) { + starting = this.start(choice, key, deadline); + this.starting.set(key, starting); + void starting.finally(() => this.starting.delete(key)); + } + return starting; + } + + private async start(choice: ServerChoice, key: string, deadline: number): Promise { + const client = new LspClient(choice.server, choice.root, this.env, this.spawn, this.clock, this.kill); + this.launching.add(client); + try { + await client.start(Math.max(1, deadline - this.clock())); + } catch { + this.broken.add(key); + client.close(); + return undefined; + } finally { + this.launching.delete(client); + } + this.clients.set(key, client); + this.sweep(); + return client; + } + + /** Stop the servers nobody used for IDLE_MS; runs while any is up. */ + private sweep(): void { + if (this.sweeper) return; + this.sweeper = setInterval(() => { + const now = this.clock(); + for (const [key, client] of this.clients) { + if (now - client.lastUsed >= IDLE_MS) { + this.clients.delete(key); + client.close(); + } + } + if (this.clients.size === 0 && this.sweeper) { + clearInterval(this.sweeper); + this.sweeper = undefined; + } + }, 60_000); + this.sweeper.unref(); + } +} + +/** The file tools whose results carry diagnostics: Pi's own. */ +const FILE_TOOLS = new Set(['edit', 'write']); + +export function registerDiagnostics(pi: Pick, deps: DiagnosticsDeps = {}): ProjectServers | undefined { + const env = deps.env ?? process.env; + if (env[LSP_ENV] === 'off') return undefined; + const servers = new ProjectServers(deps); + // A server must not outlive the Pi that started it. + process.once('exit', () => servers.stopAll()); + pi.on('tool_result', async (event, ctx) => { + if (event.isError || !FILE_TOOLS.has(event.toolName)) return undefined; + const file = changedFile(event.input, ctx.cwd); + if (!file) return undefined; + const report = await servers.check(file, ctx.cwd, ctx.signal); + if (!report) return undefined; + return { content: [...event.content, { type: 'text' as const, text: report }] }; + }); + return servers; +} diff --git a/packages/agent-host/src/drivers/pi/extension/lsp/servers.ts b/packages/agent-host/src/drivers/pi/extension/lsp/servers.ts new file mode 100644 index 00000000..7e9b84f4 --- /dev/null +++ b/packages/agent-host/src/drivers/pi/extension/lsp/servers.ts @@ -0,0 +1,209 @@ +/** + * Which language server checks a file, and from which project root. + * + * Only a server already on the machine is used — the project's own first + * (its `node_modules`, its virtualenv), then `PATH` — and nothing is ever + * installed: a file whose server is not there gets no diagnostics. Each + * entry was run against the real server (see ./client.ts for what each one + * sends): typescript-language-server 6.1 with TypeScript 5.9, TypeScript + * 7.0's own `tsc --lsp`, Pyright and basedpyright, rust-analyzer 1.99. + */ +import { readFileSync, statSync } from 'node:fs'; +import * as path from 'node:path'; + +export type Language = 'typescript' | 'rust' | 'python'; + +/** How to start one server. */ +export interface ServerCommand { + /** The server's own name, which the model reads in front of what it says. */ + name: string; + command: string; + args: string[]; + /** A Windows `.cmd`/`.bat` shim (npm's), which only a shell can start. */ + shell?: boolean; +} + +/** The server for one file: what to start, where, and the file's LSP language. */ +export interface ServerChoice { + language: Language; + languageId: string; + root: string; + server: ServerCommand; +} + +/** What the lookup reads of the machine; the real one is `machine()`. */ +export interface Machine { + isFile(file: string): boolean; + readText(file: string): string | undefined; + env: Record; + platform: NodeJS.Platform; + /** The Node that runs Pi, which also runs a server that is a Node script. */ + execPath: string; +} + +export function machine(env: Record = process.env): Machine { + return { + isFile: (file) => { + try { + return statSync(file).isFile(); + } catch { + return false; + } + }, + readText: (file) => { + try { + return readFileSync(file, 'utf8'); + } catch { + return undefined; + } + }, + env, + platform: process.platform, + execPath: process.execPath, + }; +} + +const LANGUAGES: Record = { + '.ts': ['typescript', 'typescript'], + '.mts': ['typescript', 'typescript'], + '.cts': ['typescript', 'typescript'], + '.tsx': ['typescript', 'typescriptreact'], + '.js': ['typescript', 'javascript'], + '.mjs': ['typescript', 'javascript'], + '.cjs': ['typescript', 'javascript'], + '.jsx': ['typescript', 'javascriptreact'], + '.rs': ['rust', 'rust'], + '.py': ['python', 'python'], + '.pyi': ['python', 'python'], +}; + +/** The files that make a directory a project's root, nearest first wins. */ +const ROOT_MARKERS: Record = { + typescript: ['tsconfig.json', 'jsconfig.json', 'package.json'], + rust: ['Cargo.toml'], + python: ['pyproject.toml', 'setup.py', 'setup.cfg', 'pyrightconfig.json', 'requirements.txt'], +}; + +/** A directory and every one above it, nearest first. */ +function ancestors(dir: string): string[] { + const out: string[] = []; + for (let current = dir; ; ) { + out.push(current); + const parent = path.dirname(current); + if (parent === current) return out; + current = parent; + } +} + +/** The nearest directory holding one of the language's markers; the file's + * own directory when none does (a server still checks a lone file). */ +export function projectRoot(file: string, language: Language, m: Machine): string { + const dir = path.dirname(file); + for (const candidate of ancestors(dir)) { + if (ROOT_MARKERS[language].some((marker) => m.isFile(path.join(candidate, marker)))) return candidate; + } + return dir; +} + +/** An executable on `PATH`. On Windows that means a PATHEXT extension — + * npm's extensionless shim there is a shell script Windows cannot run. */ +export function findOnPath(name: string, m: Machine): ServerCommand | undefined { + // Windows spells it `Path` as often as `PATH`; a copied environment no + // longer looks it up without case. + const pathKey = Object.keys(m.env).find((key) => key.toLowerCase() === 'path'); + const pathValue = (pathKey ? m.env[pathKey] : undefined) ?? ''; + const windows = m.platform === 'win32'; + const extensions = windows ? (m.env.PATHEXT ?? '.COM;.EXE;.BAT;.CMD').split(';').filter(Boolean) : ['']; + for (const dir of pathValue.split(windows ? ';' : ':')) { + if (!dir) continue; + for (const ext of extensions) { + const file = path.join(dir, name + ext.toLowerCase()); + if (m.isFile(file)) return { name, command: file, args: [], ...(/\.(cmd|bat)$/i.test(file) ? { shell: true } : {}) }; + } + } + return undefined; +} + +/** The directory of a Node package as Node would find it from `dir`. */ +function nodePackage(dir: string, name: string, m: Machine): string | undefined { + for (const candidate of ancestors(dir)) { + const pkg = path.join(candidate, 'node_modules', name); + if (m.isFile(path.join(pkg, 'package.json'))) return pkg; + } + return undefined; +} + +/** A Node package's executable, run by Pi's own Node: no shim, so no shell + * on Windows either. */ +function nodeBin(pkg: string, bin: string, m: Machine): string | undefined { + try { + const manifest = JSON.parse(m.readText(path.join(pkg, 'package.json')) ?? 'null') as { bin?: unknown } | null; + const field = manifest?.bin; + const relative = typeof field === 'string' ? field : typeof field === 'object' && field !== null ? (field as Record)[bin] : undefined; + if (typeof relative !== 'string') return undefined; + const script = path.join(pkg, relative); + return m.isFile(script) ? script : undefined; + } catch { + return undefined; + } +} + +function nodeServer(dir: string, pkgName: string, bin: string, args: string[], m: Machine): ServerCommand | undefined { + const pkg = nodePackage(dir, pkgName, m); + const script = pkg ? nodeBin(pkg, bin, m) : undefined; + return script ? { name: bin, command: m.execPath, args: [script, ...args] } : undefined; +} + +function withArgs(found: ServerCommand | undefined, args: string[]): ServerCommand | undefined { + return found ? { ...found, args } : undefined; +} + +/** + * TypeScript and JavaScript. The project's TypeScript decides: up to 6.x it + * ships `tsserver.js`, which typescript-language-server drives (and refuses + * a TypeScript without it); 7.x is a native compiler that is its own server + * (`tsc --lsp --stdio`) and has no tsserver at all. With no TypeScript in the + * project, a typescript-language-server on PATH may still find one of its + * own. + */ +function typescriptServer(root: string, m: Machine): ServerCommand | undefined { + const tls = (): ServerCommand | undefined => + nodeServer(root, 'typescript-language-server', 'typescript-language-server', ['--stdio'], m) ?? + withArgs(findOnPath('typescript-language-server', m), ['--stdio']); + const typescript = nodePackage(root, 'typescript', m); + if (!typescript) return tls(); + if (m.isFile(path.join(typescript, 'lib', 'tsserver.js'))) return tls(); + const tsc = nodeBin(typescript, 'tsc', m); + return tsc ? { name: 'tsc --lsp', command: m.execPath, args: [tsc, '--lsp', '--stdio'] } : undefined; +} + +/** Pyright's two builds, the project's virtualenv first, then an npm install + * in the project, then PATH. */ +function pythonServer(root: string, m: Machine): ServerCommand | undefined { + const names = ['basedpyright-langserver', 'pyright-langserver']; + const windows = m.platform === 'win32'; + for (const venv of ['.venv', 'venv']) { + for (const name of names) { + const file = path.join(root, venv, windows ? 'Scripts' : 'bin', windows ? `${name}.exe` : name); + if (m.isFile(file)) return { name, command: file, args: ['--stdio'] }; + } + } + return ( + nodeServer(root, 'basedpyright', 'basedpyright-langserver', ['--stdio'], m) ?? + nodeServer(root, 'pyright', 'pyright-langserver', ['--stdio'], m) ?? + withArgs(findOnPath(names[0]!, m), ['--stdio']) ?? + withArgs(findOnPath(names[1]!, m), ['--stdio']) + ); +} + +/** The server that checks `file`, or `undefined` when its language has none + * on this machine (or none at all). */ +export function serverFor(file: string, m: Machine): ServerChoice | undefined { + const known = LANGUAGES[path.extname(file).toLowerCase()]; + if (!known) return undefined; + const [language, languageId] = known; + const root = projectRoot(file, language, m); + const server = + language === 'typescript' ? typescriptServer(root, m) : language === 'python' ? pythonServer(root, m) : findOnPath('rust-analyzer', m); + return server ? { language, languageId, root, server } : undefined; +} diff --git a/packages/agent-host/src/drivers/pi/extension/task.ts b/packages/agent-host/src/drivers/pi/extension/task.ts index 9b303e21..187acb47 100644 --- a/packages/agent-host/src/drivers/pi/extension/task.ts +++ b/packages/agent-host/src/drivers/pi/extension/task.ts @@ -57,7 +57,8 @@ const KILL_AFTER_MS = 3_000; export type SubagentEvent = | { kind: 'text'; text: string } | { kind: 'tool_call'; callId: string; toolName: string; args: Record } - | { kind: 'tool_result'; callId: string; text: string; isError: boolean }; + // `patch`: an edit's unified patch (Pi's `details.patch`), for its diff card. + | { kind: 'tool_result'; callId: string; text: string; isError: boolean; patch?: string }; /** The details of one progress update: who, which event, in order. */ export interface SubagentUpdate { @@ -193,7 +194,14 @@ export function runSubagent(options: ChildOptions): Promise<{ text: string; erro const callId = typeof rec.toolCallId === 'string' ? rec.toolCallId : undefined; if (!callId) return; facts.delete(callId); - options.onEvent({ kind: 'tool_result', callId, text: textOf(record(rec.result)?.content), isError: rec.isError === true }); + const patch = record(record(rec.result)?.details)?.patch; + options.onEvent({ + kind: 'tool_result', + callId, + text: textOf(record(rec.result)?.content), + isError: rec.isError === true, + ...(typeof patch === 'string' ? { patch } : {}), + }); return; } case 'extension_ui_request': { diff --git a/packages/agent-host/src/drivers/pi/tools.ts b/packages/agent-host/src/drivers/pi/tools.ts index 39f45e65..2016973f 100644 --- a/packages/agent-host/src/drivers/pi/tools.ts +++ b/packages/agent-host/src/drivers/pi/tools.ts @@ -16,6 +16,7 @@ import * as path from 'node:path'; import { findInDirs, findOnPath } from '../../sdk/executable'; import { oneLine, readToolCall, str, ToolKind, type ToolCallRead, type ToolDialects } from '../../sdk/tools'; +import { boundedDiff, patchLines, toDiffLines, type DiffPayload } from '../../sdk/transcript'; import { PI_TOOL } from './extension/protocol'; type Input = Record; @@ -204,6 +205,27 @@ export const PI_READ_ONLY_TOOLS: ReadonlySet = new Set(['read', 'grep', */ export const PI_EDIT_TOOLS: ReadonlySet = new Set(['edit', 'write']); +/** + * The diff card of a finished file change, or `null` when there is none to + * show. Taken from the result, not the call: only then has the change been + * applied. `edit` returns the unified patch Pi computed against the file + * (`details.patch`), with real context lines; `write` returns no details, so + * its card is the content it wrote, every line an addition. + */ +export function fileChangeDiff(toolName: string, input: Record, details: unknown): DiffPayload | null { + const file = str(input.path); + if (!file) return null; + if (toolName === 'edit') { + const patch = typeof details === 'object' && details !== null ? (details as { patch?: unknown }).patch : undefined; + return typeof patch === 'string' ? boundedDiff(file, patchLines(patch)) : null; + } + if (toolName === 'write') { + const content = str(input.content); + return content ? boundedDiff(file, toDiffLines(content, 'add')) : null; + } + return null; +} + /** * The built-in tools a session turns on beyond Pi's default four, as * `--tools` `+name` entries (which keep whatever else the profile's diff --git a/packages/agent-host/src/sdk/transcript.ts b/packages/agent-host/src/sdk/transcript.ts index 7b101c5a..715b51a3 100644 --- a/packages/agent-host/src/sdk/transcript.ts +++ b/packages/agent-host/src/sdk/transcript.ts @@ -65,3 +65,39 @@ export function toDiffLines(text: string, type: DiffLine['type']): DiffLine[] { text: line.length > MAX_DIFF_LINE_CHARS ? line.slice(0, MAX_DIFF_LINE_CHARS) + '…' : line, })); } + +/** Lines of a unified diff. File headers (`diff`, `index`, `---`, `+++`) are + * only skipped before the first hunk: inside a hunk, `---x` is the deletion + * of the line `--x`, not a header. Hunk headers and "\ No newline" markers + * carry no line content. */ +export function patchLines(patch: string): DiffLine[] { + const out: DiffLine[] = []; + let inHunk = false; + for (const line of patch.split('\n')) { + if (line.startsWith('@@')) { + inHunk = true; + continue; + } + if (!inHunk || line.startsWith('\\')) continue; + if (line.startsWith('+')) out.push({ type: 'add', text: truncateDiffLine(line.slice(1)) }); + else if (line.startsWith('-')) out.push({ type: 'del', text: truncateDiffLine(line.slice(1)) }); + else if (line.startsWith(' ')) out.push({ type: 'context', text: truncateDiffLine(line.slice(1)) }); + } + return out; +} + +function truncateDiffLine(line: string): string { + return line.length > MAX_DIFF_LINE_CHARS ? line.slice(0, MAX_DIFF_LINE_CHARS) + '…' : line; +} + +/** A diff payload inside the shared wire caps; `null` when there are no + * lines to show. */ +export function boundedDiff(path: string, lines: DiffLine[]): DiffPayload | null { + if (lines.length === 0) return null; + const truncated = lines.length > MAX_DIFF_LINES; + return { + path, + lines: truncated ? lines.slice(0, MAX_DIFF_LINES) : lines, + ...(truncated ? { truncated: true } : {}), + }; +} diff --git a/packages/agent-host/tools/__tests__/fakeModel.test.mjs b/packages/agent-host/tools/__tests__/fakeModel.test.mjs index 667b3ce1..15eafa5b 100644 --- a/packages/agent-host/tools/__tests__/fakeModel.test.mjs +++ b/packages/agent-host/tools/__tests__/fakeModel.test.mjs @@ -21,6 +21,20 @@ const toolTurn = (shell) => ({ messages: [{ role: 'user', content: [{ type: 'text', text: '/tool' }] }], }); +describe('/write', () => { + it("writes the named file with Pi's write, a \\n in the content a line break", () => { + const reply = planReply( + { + stream: true, + tools: ['read', 'bash', 'edit', 'write'].map((name) => ({ name, input_schema: { type: 'object' } })), + messages: [{ role: 'user', content: [{ type: 'text', text: '/write src/a.ts const x = 1;\\nexport {};' }] }], + }, + 'anthropic', + ); + expect(reply).toMatchObject({ kind: 'tool', name: 'write', args: { path: 'src/a.ts', content: 'const x = 1;\nexport {};' } }); + }); +}); + describe('/tool', () => { for (const shell of ['bash', 'pwsh']) { it(`calls the Harness's ${shell} with the command and description it requires`, () => { diff --git a/packages/agent-host/tools/fake-model.mjs b/packages/agent-host/tools/fake-model.mjs index 10aa57cf..387abcbc 100644 --- a/packages/agent-host/tools/fake-model.mjs +++ b/packages/agent-host/tools/fake-model.mjs @@ -27,6 +27,11 @@ * hands back * /plan-approval propose a plan for the agent's own plan tool, for the * card the approval travels through + * /write + * write a file with the request's own file-writing tool + * (Pi's `write`, which takes `{path, content}`); `\n` in + * the content is a line break — for what an agent does + * after a file change (Pi's diff card, its diagnostics) * /task [brief] start a sub-agent with the agent's own sub-agent tool * (the CodeDeck Pi extension's `task`); the brief is the * sub-agent's prompt, so `/task /tool` makes the @@ -89,7 +94,7 @@ export const LONG_TEXT = Array.from( ).join('\n'); /** The scenario commands, in the order the vocabulary lists them. */ -export const SCENARIOS = ['tool', 'background', 'question', 'plan-approval', 'task', 'error', 'long']; +export const SCENARIOS = ['tool', 'background', 'question', 'plan-approval', 'task', 'write', 'error', 'long']; /** * How long a streamed reply is held between pieces, and the longest a text can @@ -253,12 +258,17 @@ const QUESTION_TOOLS = ['AskUserQuestion', 'ask_user_question', 'question', 'ask const PLAN_TOOLS = ['ExitPlanMode', 'exit_plan_mode', 'exit_plan']; /** Names that start a sub-agent. */ const TASK_TOOLS = ['task']; +/** Names that write a whole file. The arguments are Pi's (`{path, + * content}`), the only writer this scenario was run against; another + * agent's tool of the same name may want others and refuse the call. */ +const WRITE_TOOLS = ['write']; /** What each scenario's call prefers to be named — the agent's own tool for * it, when the request offers one. */ const SCENARIO_TOOLS = { question: QUESTION_TOOLS, 'plan-approval': PLAN_TOOLS, task: TASK_TOOLS, + write: WRITE_TOOLS, tool: SHELL_TOOLS, // The CodeDeck Pi extension's background shell first, when offered: its // plain `bash` would run the command in the foreground. @@ -322,6 +332,10 @@ function questionArguments(name, question) { * `/tool ` names the command to run instead of the default. */ export function toolArguments(name, scenario, argument = '') { if (scenario === 'plan-approval') return { plan: argument || PLAN_TEXT }; + if (scenario === 'write') { + const [, file = 'codedeck-fake-model.txt', content = ''] = /^(\S+)\s*([\s\S]*)$/.exec(argument) ?? []; + return { path: file, content: content.replace(/\\n/g, '\n') }; + } if (scenario === 'task') return { description: 'Fake sub-agent', prompt: argument || '/tool', agent: 'general' }; if (scenario === 'question') return questionArguments(name, argument || 'Which color?'); if (scenario === 'background') return { command: argument || BACKGROUND_COMMAND, description: `A background ${name} call from the fake model`, ...BACKGROUND_FLAG[name] }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9d17793e..92f760df 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -53,6 +53,9 @@ importers: typescript: specifier: ^5.7.0 version: 5.9.3 + typescript-language-server: + specifier: 6.1.0 + version: 6.1.0 vitest: specifier: ^3.0.0 version: 3.2.7(@types/debug@4.1.13)(@types/node@22.20.1)(jiti@2.7.0)(jsdom@30.0.1(@noble/hashes@2.0.1))(yaml@2.9.1) @@ -5668,6 +5671,11 @@ packages: typed-rest-client@1.8.11: resolution: {integrity: sha512-5UvfMpd1oelmUPRbbaVnq+rHP7ng2cE4qoQkQeAqxRL6PklkxsM0g32/HL0yfvruK6ojQ5x8EE+HF4YV6DtuCA==} + typescript-language-server@6.1.0: + resolution: {integrity: sha512-/O/dr2CbvDS1W9jKJcBQxk4WagUQ97oBb8k/eq9cOslsbtAuC2BDPpkcULxE2SEZiPIJvVNLeNjLZe9hQl1zCQ==} + engines: {node: '>=22.22.2'} + hasBin: true + typescript@5.9.3: resolution: {integrity: sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==} engines: {node: '>=14.17'} @@ -5785,6 +5793,20 @@ packages: jsdom: optional: true + vscode-jsonrpc@10.0.0: + resolution: {integrity: sha512-USSfxg0vDIpTQqlQBGxF3mBARyoS8cU7LHLpbYNh6dOJt32dlskxPWB0Irrh5TllTmyf7oNLAvqDqzklbwwnpA==} + engines: {node: '>=14.17.0'} + + vscode-jsonrpc@5.0.1: + resolution: {integrity: sha512-JvONPptw3GAQGXlVV2utDcHx0BiY34FupW/kI6mZ5x06ER5DdPG/tXWMVHjTNULF5uKPOUUD0SaXg5QaubJL0A==} + engines: {node: '>=8.0.0 || >=10.0.0'} + + vscode-languageserver-protocol@3.18.5: + resolution: {integrity: sha512-FW4fBMzZ99N8xeob4py0l050wgCoRgTpEq5Xcsb9lM1bc29UKGR/wSV+4knaqFxssFHajy7OzNiKP/m3U1ZEuA==} + + vscode-languageserver-types@3.18.5: + resolution: {integrity: sha512-kPd6+JQNz3HMJvjKlqUxERSlZHAntl/aGpsk7ImwIh0ewDKDcjgAEyVSYciPiOJD3ftbK9IrfiqUeTZLSbCizw==} + w3c-xmlserializer@5.0.0: resolution: {integrity: sha512-o8qghlI8NZHU1lLPrpi2+Uq7abh4GGPpYANlalzWxyWteJOCsr/P+oPBA49TOLu5FTZO4d3F9MnWJfiMo4BkmA==} engines: {node: '>=18'} @@ -12441,6 +12463,11 @@ snapshots: tunnel: 0.0.6 underscore: 1.13.8 + typescript-language-server@6.1.0: + dependencies: + vscode-jsonrpc: 5.0.1 + vscode-languageserver-protocol: 3.18.5 + typescript@5.9.3: {} underscore@1.13.8: {} @@ -12549,6 +12576,17 @@ snapshots: - tsx - yaml + vscode-jsonrpc@10.0.0: {} + + vscode-jsonrpc@5.0.1: {} + + vscode-languageserver-protocol@3.18.5: + dependencies: + vscode-jsonrpc: 10.0.0 + vscode-languageserver-types: 3.18.5 + + vscode-languageserver-types@3.18.5: {} + w3c-xmlserializer@5.0.0: dependencies: xml-name-validator: 5.0.0