Skip to content

Computer use for the Pi coding agent — Windows apps, browsers and desktops through native UI trees, screenshots and real input.

merge: preserve final previews with current develop

alexshpunt-clanker[bot]d0fc7dd1faae
diff --cc src/index.ts
index f9bd646,7d1d68a..f283c8b
--- a/src/index.ts
+++ b/src/index.ts
@@@ -65,10 -67,11 +66,12 @@@ export default async function computerU
if (!active.includes("tool_search") && pi.getAllTools().some(tool => tool.name === "tool_search"))
pi.setActiveTools([...active, "tool_search"]);
});
+ // Copilot's Responses route makes every optional field of a tool with unsaid strictness required (LPT-975).
+ registerToolOmission(pi, computerNamespace().name);
pi.on("session_shutdown", async () => {
if (loading) await loading.catch(() => undefined);
- try { await session?.close(); } finally { await highlights.close(); }
+ visuals.clear();
+ await session?.close();
session = undefined;
});
}
diff --cc src/session.ts
index 73795a8,0136458..81935f0
--- a/src/session.ts
+++ b/src/session.ts
@@@ -1438,20 -1385,17 +1440,21 @@@ export class ComputerSession
// Without an observation it shoots the live page: a locator, or a ref of the page's latest snapshot.
if (id !== undefined && entry.dom?.id !== id) return this.result("refused", "This DOM observation is no longer current. Read the window with dom again.", undefined, this.addressed(entry));
try {
- const shot = await entry.web!.request<{ url: string; title: string; png: string; box?: unknown }>("screenshot",
+ const shot = await entry.web!.request<{ url: string; title: string; png: string; box?: unknown; derive?: Derive }>("screenshot",
{ ...(target ? { target } : {}), highlight: kind === "highlight", ...(css ? { css } : {}) }, signal);
- const bytes = Buffer.from(shot.png, "base64");
+ const plain = Buffer.from(shot.png, "base64");
+ const bytes = shot.derive ? await derivedPng(plain, shot.derive) : plain;
if (bytes.length < 24 || !bytes.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))) throw Error("Playwright did not return a PNG.");
- const image: Content = { type: "image", data: shot.png, mimeType: "image/png" };
++ const image: Content = { type: "image", data: bytes.toString("base64"), mimeType: "image/png" };
+ // CSS boxes do not prove a Windows screen transform. Associate the preview with its window only.
+ await this.activity(entry, { kind: "inspect" }, signal, image);
+ if (!this.allowed(entry) || signal?.aborted) return this.result("refused", "Observation revoked or cancelled; pixels withheld.");
return this.result("observed", `Playwright ${kind === "page" ? "page viewport" : kind === "crop" ? "element" : "highlighted viewport"} screenshot. These are page pixels, not native window coordinates; they grant no input.`,
{ url: shot.url, title: shot.title, ...(shot.box ? { box_css: shot.box } : {}) },
{ ...this.addressed(entry), ...(id !== undefined ? { observation_id: id } : {}), agent_image: { route: "playwright_screenshot", kind, coordinate_space: "page_image_pixels", input_authority: false,
output: { width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20), bytes: bytes.length, mimeType: "image/png",
hash: createHash("sha256").update(bytes).digest("hex") } } },
- [{ type: "image", data: shot.png, mimeType: "image/png" }]);
- [{ type: "image", data: bytes.toString("base64"), mimeType: "image/png" }]);
++ [image]);
} catch (error) { return this.webFailure(entry, error); }
});
}
diff --cc test/attach-browser.test.ts
index 79d2837,9e2f3d3..64da2ea
--- a/test/attach-browser.test.ts
+++ b/test/attach-browser.test.ts
@@@ -1,7 -1,6 +1,8 @@@
import { expect, test, vi } from "vitest";
++import sharp from "sharp";
import { ComputerSession, type ComputerMode, type Driver, type DriverResult } from "../src/session.ts";
import type { WebBrowser, WebLauncher } from "../src/web-browser.ts";
+import type { WindowObserver } from "../src/native-visuals.ts";
// LPT-850: Playwright attaches to a running Chrome or Edge only in YOLO, binds it to the addressed window, and the
// session's close detaches through the worker instead of leaving the browser to anyone else.
@@@ -13,29 -12,14 +14,48 @@@ function fixture(observer?: WindowObser
if (name === "list_windows") return { content: [], structuredContent: { windows: [window] } };
throw new Error(`Unexpected native call ${name}`);
});
-- const browser = { close: vi.fn(async () => {}), page: vi.fn(async () => ({ url: "data:", title: "Attach target", snapshot: "- textbox \"Field\"", tabs: [], events: [] })) };
++ const browser = { close: vi.fn(async () => {}), request: vi.fn(async () => ({})), page: vi.fn(async () => ({ url: "data:", title: "Attach target", snapshot: "- textbox \"Field\"", tabs: [], events: [] })) };
const launch = vi.fn<WebLauncher>(async () => ({ browser: browser as unknown as WebBrowser,
opened: { pid: window.pid, version: "155", url: "data:", title: "Attach target", via: "cdp" } }));
- const session = new ComputerSession({ call, identity: vi.fn(async () => "life"), close: vi.fn(async () => {}) }, () => mode, undefined, undefined, undefined, launch);
+ const session = new ComputerSession({ call, identity: vi.fn(async () => `PiComputerUse.${"a".repeat(32)}:4100:1`), close: vi.fn(async () => {}) }, () => mode, undefined, observer, launch);
const ref = async () => ((await session.windows()).details.state!.windows as { window_ref: string }[])[0].window_ref;
return { session, launch, browser, ref, setMode(value: ComputerMode) { mode = value; } };
}
+test("Playwright activity stays source-associated without exposing text or inventing a desktop point", async () => {
+ const observer = vi.fn<WindowObserver>(async () => ({ status: "queued" })); const f = fixture(observer);
+ const ref = await f.ref(); await f.session.attachBrowser(ref);
+ const read = await f.session.observeDom(ref);
+ expect(observer.mock.calls.at(-1)?.[0]).toMatchObject({ bounds: window.bounds, mark: { kind: "inspect" } });
+ observer.mockClear();
+ const result = await f.session.actDom(ref, read.details.observation_id!, { action: "fill", target: { ref: "e1" }, text: "synthetic-secret" });
+ expect(result.details.status).toBe("delivered");
+ expect(observer).toHaveBeenCalledTimes(1);
+ expect(observer.mock.calls[0][0]).toMatchObject({ mark: { kind: "action" }, action: "fill", outcome: "delivered" });
+ expect(observer.mock.calls[0][0]).not.toHaveProperty("mark.point");
+ expect(observer.mock.calls[0][0]).not.toHaveProperty("mark.region");
+ expect(JSON.stringify(observer.mock.calls)).not.toContain("synthetic-secret");
+ await f.session.close();
+});
++test("browser crop presentation uses the final derived pixels returned to the agent", async () => {
++ const observer = vi.fn<WindowObserver>(async () => ({ status: "queued" })); const f = fixture(observer);
++ const ref = await f.ref(); await f.session.attachBrowser(ref);
++ const png = await sharp({ create: { width: 6, height: 4, channels: 3, background: "blue" } }).png().toBuffer();
++ f.browser.request.mockResolvedValue({ url: "data:", title: "Attach target", png: png.toString("base64"),
++ derive: { geometry: { dpr: 1, scrollX: 0, scrollY: 0, innerWidth: 6, innerHeight: 4 }, box: { x: 2, y: 1, width: 2, height: 2 } } });
++ const result = await f.session.webImage(ref, undefined, "crop", { ref: "e1" });
++ expect(result.details.status).toBe("observed");
++ const image = result.content.find(block => block.type === "image");
++ expect(image?.type).toBe("image");
++ const decoded = await sharp(Buffer.from(image!.data!, "base64")).raw().toBuffer({ resolveWithObject: true });
++ expect(decoded.info).toMatchObject({ width: 2, height: 2, channels: 3 });
++ expect([...decoded.data]).toEqual(Array.from({ length: 4 }, () => [0, 0, 255]).flat());
++ expect(observer).toHaveBeenCalledTimes(1);
++ expect(observer.mock.calls[0][0]).toMatchObject({ mark: { kind: "inspect" }, image });
++ expect(observer.mock.calls[0][0]).not.toHaveProperty("mark.point");
++ expect(observer.mock.calls[0][0]).not.toHaveProperty("mark.region");
++ await f.session.close();
++});
test("Safe mode refuses before any Playwright work", async () => {
const f = fixture();
const ref = await f.ref();