Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/lsp/__serial__/verify.ts

Raw
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import {
	cp,
	mkdir,
	mkdtemp,
	readFile,
	realpath,
	rm,
	symlink,
	writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { delimiter, dirname, join } from "node:path";
import { promisify } from "node:util";
import {
	createPiSession,
	type PiSession,
} from "../../../test/fixture/create-pi-session.ts";
import type { Evidence } from "../src/result.ts";
import { nativeQuery } from "./native.ts";

const evidenceBase = process.env["PI_LSP_EVIDENCE_DIR"] ?? tmpdir();
await mkdir(evidenceBase, { recursive: true });
const evidence = await mkdtemp(join(evidenceBase, "pi-lsp-live-"));
const workspace = join(evidence, "workspace");
const relocated = join(evidence, "package");
await cp(new URL("../../../dist/lsp/", import.meta.url), relocated, {
	recursive: true,
});
await mkdir(workspace);
let pi: PiSession | undefined;
const checks: { name: string; result: Evidence }[] = [];
const otherServers: unknown[] = [];
let startup: unknown;
let goProcesses: unknown;
async function serverPids(): Promise<number[]> {
	const children = (
		await readFile(`/proc/${process.pid}/task/${process.pid}/children`, "utf8")
	)
		.trim()
		.split(/\s+/u)
		.filter(Boolean)
		.map(Number);
	const servers: number[] = [];
	for (const pid of children) {
		try {
			if ((await realpath(`/proc/${pid}/exe`)) === "/usr/bin/gopls")
				servers.push(pid);
		} catch {
			/* Child already exited. */
		}
	}
	return servers;
}
const count = (value: string): number =>
	value.length ? Math.ceil(value.length / 4) : 0;
try {
	const version = (
		await promisify(execFile)("/usr/bin/gopls", ["version"], { timeout: 5000 })
	).stdout.trim();
	assert.match(
		version,
		/v0\.24\./u,
		"live gate requires actual installed /usr/bin/gopls 0.24",
	);
	await writeFile(
		join(workspace, "go.mod"),
		"module example.test/lspfixture\n\ngo 1.24\n",
	);
	await mkdir(join(workspace, "dep"));
	await writeFile(
		join(workspace, "dep/value.go"),
		"package dep\n\nvar Value = 7\n",
	);
	const main = await readFile(
		new URL("./fixtures/main.go", import.meta.url),
		"utf8",
	);
	await writeFile(join(workspace, "main.go"), main);
	const baselineStart = performance.now();
	const baseline = await createPiSession({
		cwd: workspace,
		env: { PI_LSP_DEBUG: "0" },
	});
	const baselineImpact = {
		ms: performance.now() - baselineStart,
		system: count(baseline.session.systemPrompt),
		tools: count(JSON.stringify(baseline.session.getAllTools())),
	};
	await baseline.dispose();
	const start = performance.now();
	pi = await createPiSession({
		cwd: workspace,
		extensionPaths: [join(relocated, "index.ts")],
		env: {
			PI_LSP_DEBUG: "0",
			PATH: `/usr/bin${delimiter}${process.env["PATH"] ?? ""}`,
			GOCACHE: join(workspace, ".gocache"),
			GOPATH: join(workspace, ".gopath"),
			GOTELEMETRY: "off",
		},
	});
	const addedImpact = {
		ms: performance.now() - start,
		system: count(pi.session.systemPrompt),
		tools: count(JSON.stringify(pi.session.getAllTools())),
	};
	startup = {
		baseline: baselineImpact,
		withLsp: addedImpact,
		delta: {
			system: addedImpact.system - baselineImpact.system,
			tools: addedImpact.tools - baselineImpact.tools,
		},
		method:
			"Pi JS-length/4 heuristic; serialized ToolInfo metadata; no provider-token measurement.",
	};
	assert.deepEqual(pi.errors, []);
	assert.deepEqual(
		await serverPids(),
		[],
		"Extension startup must not launch gopls",
	);
	async function query(
		name: string,
		operation:
			| "definition"
			| "references"
			| "hover"
			| "type_definition"
			| "implementations",
		line = 4,
		column = 18,
		file = "main.go",
		workspaceRoot = workspace,
	): Promise<Evidence> {
		assert.ok(pi);
		const result = await nativeQuery(pi, {
			operation,
			file,
			line,
			column,
			workspace: workspaceRoot,
			timeout_ms: 60000,
		});
		checks.push({ name, result });
		return result;
	}
	const definition = await query("definition", "definition");
	assert.equal(definition.status, "ok");
	assert.match(JSON.stringify(definition.items), /"line":3/u);
	assert.match(JSON.stringify(definition.items), /var Local = 1/u);
	const references = await query("references", "references");
	assert.equal(references.status, "ok");
	assert.ok(references.items.length >= 2);
	const hover = await query("hover", "hover");
	assert.equal(hover.status, "ok");
	assert.match(hover.hover ?? "", /Local.*int/su);
	await writeFile(
		join(workspace, "types.go"),
		'package fixture\ntype Speaker interface { Say() string }\ntype Person struct{}\nfunc (Person) Say() string { return "ok" }\nvar Current Speaker = Person{}\nfunc UseSpeaker() { _ = Current }\n',
	);
	const typeDefinition = await query(
		"type definition",
		"type_definition",
		5,
		5,
		"types.go",
	);
	assert.equal(typeDefinition.status, "ok");
	assert.match(JSON.stringify(typeDefinition.items), /type Speaker interface/u);
	assert.match(JSON.stringify(typeDefinition.items), /"line":2/u);
	const implementations = await query(
		"implementations",
		"implementations",
		2,
		6,
		"types.go",
	);
	assert.equal(implementations.status, "ok");
	assert.match(JSON.stringify(implementations.items), /type Person struct/u);
	assert.match(JSON.stringify(implementations.items), /"line":3/u);
	for (const operation of ["document_symbols", "workspace_symbols"] as const) {
		const result = await nativeQuery(pi, {
			operation,
			...(operation === "document_symbols"
				? { file: "types.go" }
				: { server: "gopls", query: "Speaker" }),
			workspace,
			timeout_ms: 60000,
		});
		checks.push({ name: operation, result });
		assert.equal(result.status, "ok");
		assert.match(JSON.stringify(result.items), /Speaker/u);
		assert.equal(result.serverCompleteness, "unknown");
	}
	await writeFile(
		join(workspace, "caller.go"),
		"package fixture\nfunc Caller() { Callee() }\n",
	);
	await writeFile(
		join(workspace, "callee.go"),
		"package fixture\nfunc Callee() {}\n",
	);
	for (const operation of ["incoming_calls", "outgoing_calls"] as const) {
		const result = await nativeQuery(pi, {
			operation,
			file: operation === "incoming_calls" ? "callee.go" : "caller.go",
			line: 2,
			column: 6,
			workspace,
			timeout_ms: 60000,
		});
		checks.push({ name: operation, result });
		assert.equal(result.status, "ok");
		assert.match(
			JSON.stringify(result.items),
			operation === "incoming_calls" ? /Caller/u : /Callee/u,
		);
		assert.match(JSON.stringify(result.items), /caller.go/u);
		const edges = result.items as { callSites: { file: string }[] }[];
		assert.ok(edges.length);
		assert.ok(
			edges.every((edge) =>
				edge.callSites.every((site) => site.file.endsWith("caller.go")),
			),
		);
	}
	await writeFile(
		join(workspace, "limits.go"),
		"package fixture\n" +
			Array.from(
				{ length: 125 },
				(_, i) => "var Limit" + String(i).padStart(3, "0") + " = " + i,
			).join("\n") +
			"\n",
	);
	const silentLimit = await nativeQuery(pi, {
		operation: "workspace_symbols",
		server: "gopls",
		query: "Limit",
		workspace,
		max_results: 100,
		max_bytes: 32768,
		timeout_ms: 60000,
	});
	checks.push({
		name: "gopls silent workspace symbol limit against 125 known declarations",
		result: silentLimit,
	});
	assert.equal(silentLimit.status, "ok");
	assert.ok(silentLimit.items.length < 125);
	assert.equal(
		silentLimit.toolTruncated,
		false,
		"Server-imposed omission must not be mislabeled client truncation",
	);
	assert.equal(silentLimit.serverCompleteness, "unknown");
	const exactLimit = await nativeQuery(pi, {
		operation: "workspace_symbols",
		server: "gopls",
		query: "Limit124",
		workspace,
		timeout_ms: 60000,
	});
	checks.push({
		name: "omitted corpus exact query still resolves known declaration",
		result: exactLimit,
	});
	assert.equal(exactLimit.status, "ok");
	assert.match(JSON.stringify(exactLimit.items), /Limit124/u);
	const empty = await query("negative whitespace", "hover", 4, 39);
	assert.equal(empty.status, "no_result");
	// External managed-file edit changes the valid symbol under the same coordinate.
	const changed = main
		.replace("var Local = 1", 'var Other = "new"')
		.replace("_ = Local;", "_ = Other;");
	await writeFile(join(workspace, "main.go"), changed);
	const edited = await query("external open-file edit", "definition");
	assert.equal(edited.status, "ok");
	assert.match(JSON.stringify(edited.items), /var Other/u);
	// Unopened package affects hover without ever having been a navigation target.
	const depColumn =
		[...(changed.split("\n")[3] ?? "")].join("").indexOf("Value") + 1;
	assert.ok(depColumn > 0);
	const before = await query(
		"unopened dependency before",
		"hover",
		4,
		depColumn,
	);
	assert.match(before.hover ?? "", /int/u);
	await writeFile(
		join(workspace, "dep/value.go"),
		'package dep\n\nvar Value = "changed"\n',
	);
	// Watcher delivery/indexing has no exposed barrier. Query repeatedly within a fixed live assertion deadline, preserving every observation.
	const deadline = Date.now() + 10000;
	let after: Evidence;
	do {
		after = await query("unopened dependency after", "hover", 4, depColumn);
		if (!/string/u.test(after.hover ?? ""))
			await new Promise((resolve) => setTimeout(resolve, 80));
	} while (!/string/u.test(after.hover ?? "") && Date.now() < deadline);
	assert.match(
		after.hover ?? "",
		/string/u,
		"gopls-owned watcher must observe unopened local import edit",
	);
	const dependencyDefinition = await query(
		"observe unopened dependency directory",
		"definition",
		4,
		depColumn,
	);
	assert.equal(dependencyDefinition.status, "ok");
	assert.match(JSON.stringify(dependencyDefinition.items), /dep\/value.go/u);
	// Establish its bounded directory snapshot; returned targets never become open overlays.
	await query("baseline observed unopened dependency", "hover", 4, depColumn);
	await writeFile(
		join(workspace, "dep/value.go"),
		"package dep\n\nvar Value = true\n",
	);
	const observedEdit = await query(
		"synchronized observed unopened dependency edit",
		"hover",
		4,
		depColumn,
	);
	assert.equal(observedEdit.status, "ok");
	assert.match(observedEdit.hover ?? "", /bool/u);
	const unicode = changed.replace("_ = Other;", '_ = "😀"; _ = Other;');
	await writeFile(join(workspace, "main.go"), unicode);
	const scalarColumn = [...(unicode.split("\n")[3] ?? "")]
		.join("")
		.split("Other")[0];
	assert.ok(scalarColumn !== undefined);
	const unicodeResult = await query(
		"Unicode scalar coordinates",
		"definition",
		4,
		[...scalarColumn].length + 1,
	);
	assert.equal(unicodeResult.status, "ok");
	assert.match(JSON.stringify(unicodeResult.items), /var Other/u);
	const nested = join(workspace, "second");
	await mkdir(nested);
	await writeFile(
		join(nested, "go.mod"),
		"module example.test/second\n\ngo 1.24\n",
	);
	await writeFile(
		join(nested, "other.go"),
		"package second\nvar Separate = 9\nvar Use = Separate\n",
	);
	const isolated = await query(
		"root isolation",
		"definition",
		3,
		11,
		join(nested, "other.go"),
		nested,
	);
	assert.equal(isolated.status, "ok");
	assert.equal(isolated.scope, nested);
	for (const { result } of checks) {
		assert.equal(result.readiness, "unknown");
		assert.equal(result.workspaceFreshness, "unknown");
		assert.equal(result.serverCompleteness, "unknown");
	}
	const reloadPids = await serverPids();
	assert.equal(reloadPids.length, 2, "Two roots must own two gopls sessions");
	await pi.session.reload();
	for (const pid of reloadPids)
		assert.throws(
			() => process.kill(pid, 0),
			"Reload must terminate old servers",
		);
	assert.deepEqual(await serverPids(), [], "Reload must remain lazy");
	const reloaded = await query(
		"native reload fresh session",
		"definition",
		4,
		[...scalarColumn].length + 1,
	);
	assert.equal(reloaded.status, "ok");
	assert.match(JSON.stringify(reloaded.items), /var Other/u);
	const pids = await serverPids();
	assert.equal(pids.length, 1);
	await pi.dispose();
	pi = undefined;
	for (const pid of pids)
		assert.throws(
			() => process.kill(pid, 0),
			"gopls exit must be observed after native shutdown",
		);
	goProcesses = {
		reloadPids,
		pids,
		reloadExitVerified: true,
		exitVerified: true,
		noStartupServer: true,
	};
	for (const server of ["clangd", "typescript-language-server"] as const) {
		const root = join(evidence, server);
		await mkdir(root);
		const ts = server === "typescript-language-server";
		const caller = ts ? "caller.ts" : "caller.cpp";
		const callee = ts ? "callee.ts" : "callee.cpp";
		const callerText = ts
			? 'import { Callee } from "./callee.js";\nexport function Caller() { Callee(); }\n'
			: '#include "api.hpp"\nvoid Caller() { Callee(); }\n';
		const calleeText = ts
			? 'export function Callee() {}\nexport interface Speaker { say(): string; }\nexport class Person implements Speaker { say() { return "ok"; } }\nexport const current: Speaker = new Person();\n'
			: '#include "api.hpp"\nvoid Callee() {}\n';
		await writeFile(join(root, caller), callerText);
		await writeFile(join(root, callee), calleeText);
		if (ts) {
			await mkdir(join(root, "node_modules"));
			await symlink(
				process.env["PI_LSP_TYPESCRIPT_SDK"] ??
					join(
						dirname(
							await realpath(
								(
									await promisify(execFile)("which", [server], {
										timeout: 5000,
									})
								).stdout.trim(),
							),
						),
						"../../typescript",
					),
				join(root, "node_modules/typescript"),
				"dir",
			);
			await writeFile(join(root, "package.json"), '{"type":"module"}');
			await writeFile(
				join(root, "tsconfig.json"),
				'{"compilerOptions":{"target":"ES2022","module":"NodeNext","moduleResolution":"NodeNext"},"include":["*.ts"]}',
			);
		} else {
			await writeFile(
				join(root, "api.hpp"),
				"void Callee();\nvoid Caller();\n",
			);
			await writeFile(join(root, "compile_flags.txt"), "-std=c++17\n");
		}
		const versions = (
			await promisify(execFile)(server, ["--version"], { timeout: 5000 })
		).stdout.trim();
		const native = await createPiSession({
			cwd: root,
			extensionPaths: [join(relocated, "index.ts")],
			env: { PI_LSP_DEBUG: "0" },
		});
		let pid: number | undefined;
		try {
			const run = async (
				operation: import("../src/client.ts").Operation,
				file?: string,
				line?: number,
				column?: number,
				query?: string,
			) => {
				const result = await nativeQuery(native, {
					operation,
					server,
					workspace: root,
					...(file ? { file } : {}),
					...(line ? { line, column: column ?? 1 } : {}),
					...(query !== undefined ? { query } : {}),
					timeout_ms: 60000,
				});
				checks.push({ name: server + " " + operation, result });
				return result;
			};
			const usage = (callerText.split("\n")[1] ?? "").indexOf("Callee") + 1;
			const definition = await run("definition", caller, 2, usage);
			assert.equal(definition.status, "ok");
			assert.match(JSON.stringify(definition.items), /Callee/u);
			assert.ok(
				(definition.items as { file: string }[]).some(
					(item) => item.file !== join(root, caller),
				),
				"Definition must resolve a cross-file declaration, not a same-file import alias",
			);
			const hover = await run("hover", caller, 2, usage);
			assert.equal(hover.status, "ok");
			assert.match(hover.hover ?? "", /Callee/u);
			const refs = await run("references", caller, 2, usage);
			assert.equal(refs.status, "ok");
			assert.match(JSON.stringify(refs.items), /caller/u);
			for (const operation of [
				"document_symbols",
				"workspace_symbols",
			] as const) {
				const symbols = await run(
					operation,
					operation === "document_symbols" ? caller : undefined,
					undefined,
					undefined,
					operation === "workspace_symbols" ? "Caller" : undefined,
				);
				assert.equal(symbols.status, "ok");
				assert.match(JSON.stringify(symbols.items), /Caller/u);
			}
			for (const operation of ["incoming_calls", "outgoing_calls"] as const) {
				const result = await run(
					operation,
					operation === "incoming_calls" ? callee : caller,
					ts && operation === "incoming_calls" ? 1 : 2,
					ts ? 17 : 6,
				);
				assert.equal(result.status, "ok");
				assert.ok(
					result.items.length,
					"Actual indexed cross-file calls required",
				);
				const edges = result.items as { callSites: { file: string }[] }[];
				assert.ok(
					edges.every(
						(edge) =>
							edge.callSites.length &&
							edge.callSites.every((site) => site.file.endsWith(caller)),
					),
				);
			}
			for (const operation of ["type_definition", "implementations"] as const) {
				const result = await run(
					operation,
					callee,
					ts ? (operation === "type_definition" ? 4 : 2) : 2,
					ts ? (operation === "type_definition" ? 14 : 18) : 6,
				);
				if (ts) {
					assert.equal(result.status, "ok", JSON.stringify(result));
					assert.match(
						JSON.stringify(result.items),
						operation === "type_definition"
							? /interface Speaker/u
							: /class Person/u,
					);
				} else {
					assert.ok(
						["ok", "no_result", "unsupported"].includes(result.status),
						JSON.stringify(result),
					);
				}
			}
			const changed = callerText.replace("Callee();", "/*😀*/ Callee();");
			await writeFile(join(root, caller), changed);
			const prefix = (changed.split("\n")[1] ?? "").split("Callee")[0] ?? "";
			const edited = await run("definition", caller, 2, [...prefix].length + 1);
			assert.equal(edited.status, "ok");
			assert.match(JSON.stringify(edited.items), /Callee/u);
			const children = (
				await readFile(
					`/proc/${process.pid}/task/${process.pid}/children`,
					"utf8",
				)
			)
				.trim()
				.split(/\s+/u)
				.map(Number);
			for (const child of children) {
				try {
					const exe = await realpath(`/proc/${child}/exe`);
					if (exe.includes(ts ? "node" : "clangd")) pid = child;
				} catch {}
			}
			assert.ok(pid, "Native server child observed");
		} finally {
			await native.dispose();
		}
		assert.throws(
			() => process.kill(pid as number, 0),
			"Native server exit observed",
		);
		otherServers.push({ server, versions, pid, exitVerified: true });
		await rm(root, { recursive: true, force: true });
	}
	await writeFile(
		join(evidence, "report.json"),
		JSON.stringify(
			{
				version,
				otherServers,
				processes: {
					reloadPids,
					pids,
					reloadExitVerified: true,
					exitVerified: true,
					noStartupServer: true,
				},
				checks,
				startup: {
					baseline: baselineImpact,
					withLsp: addedImpact,
					delta: {
						system: addedImpact.system - baselineImpact.system,
						tools: addedImpact.tools - baselineImpact.tools,
					},
					method:
						"Pi JS-length/4 heuristic, serialized ToolInfo metadata; timing single Linux source-fixture SDK startup, built extension. No provider token measurement, Windows/WSL unavailable.",
				},
				native:
					"Real Pi SDK + faux scripted provider, relocated built dist/lsp/index.ts, installed gopls/clangd/TypeScript language server; no external model/network.",
			},
			null,
			2,
		),
	);
	console.log(
		`PASS ${checks.length} native Pi/server observations; evidence: ${evidence}/report.json`,
	);
} catch (error) {
	await writeFile(
		join(evidence, "failure.json"),
		JSON.stringify(
			{ message: String(error), checks, otherServers, goProcesses, startup },
			null,
			2,
		),
	);
	throw error;
} finally {
	await pi?.dispose();
	for (const server of ["clangd", "typescript-language-server"])
		await rm(join(evidence, server), {
			recursive: true,
			force: true,
			maxRetries: 5,
			retryDelay: 100,
		});
	await rm(relocated, { recursive: true, force: true });
	await rm(workspace, {
		recursive: true,
		force: true,
		maxRetries: 5,
		retryDelay: 100,
	});
}