fix(tools)!: simplify direct invocation and harden boundary contracts (#555)
Remove REA permission grants, scope ceilings, approval confirmations, and inert settings. Preserve selected local evidence, accept caller-chosen commands and paths, and report actual runtime failures. Harden provider results, schema serialization, snapshot evidence binding, archive paths, source maps, native screenshots, managed EH parsing, and cancellation cleanup. Regenerate tool metadata and document direct invocation. BREAKING CHANGE: Removed permission configuration and policy commands, approval fields, filesystem scope inputs, and fixed confirmation options. Process capture inherits the host environment and accepts command names; callers must refresh tool schemas and migrate removed fields.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { observeNativeUi } from "./NativeUiObservation.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
@@ -13,7 +15,6 @@ const target: BinaryTarget = {
|
||||
const scope = {
|
||||
pid: 123,
|
||||
window_id: 456,
|
||||
observation_approved: true,
|
||||
screenshot: false,
|
||||
};
|
||||
const snapshot = {
|
||||
@@ -29,39 +30,184 @@ const snapshot = {
|
||||
gaps: [],
|
||||
truncated: false,
|
||||
};
|
||||
describe("native UI authority and lifecycle", () => {
|
||||
it("requires observation and independent action approval before invoking a helper", async () => {
|
||||
|
||||
const validPng = Buffer.from(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jv4sAAAAASUVORK5CYII=",
|
||||
"base64",
|
||||
);
|
||||
describe("native UI screenshot validation", () => {
|
||||
it("rejects malformed or mismatched screenshot bytes from the helper", async () => {
|
||||
const screenshot = {
|
||||
mime_type: "image/png",
|
||||
base64: validPng.toString("base64"),
|
||||
sha256: "0".repeat(64),
|
||||
width: 1,
|
||||
height: 1,
|
||||
};
|
||||
const result = await observeNativeUi(
|
||||
target,
|
||||
"observe_native_ui",
|
||||
{ ...scope, screenshot: true },
|
||||
{
|
||||
invoke: async () => ({
|
||||
ok: true,
|
||||
result: { ...snapshot, screenshot },
|
||||
}),
|
||||
},
|
||||
);
|
||||
expect(result.ok).toBe(false);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ base64: "%%%=" },
|
||||
{ base64: Buffer.from("not a PNG").toString("base64") },
|
||||
{ width: 2 },
|
||||
{ sha256: "0".repeat(64) },
|
||||
])(
|
||||
"rejects invalid screenshot field variants from the helper",
|
||||
async (change) => {
|
||||
const screenshot = {
|
||||
mime_type: "image/png",
|
||||
base64: validPng.toString("base64"),
|
||||
sha256: createHash("sha256").update(validPng).digest("hex"),
|
||||
width: 1,
|
||||
height: 1,
|
||||
...change,
|
||||
};
|
||||
const result = await observeNativeUi(
|
||||
target,
|
||||
"observe_native_ui",
|
||||
{ ...scope, screenshot: true },
|
||||
{
|
||||
invoke: async () => ({
|
||||
ok: true,
|
||||
result: { ...snapshot, screenshot },
|
||||
}),
|
||||
},
|
||||
);
|
||||
expect(result.ok).toBe(false);
|
||||
},
|
||||
);
|
||||
|
||||
it("accepts a self-consistent PNG screenshot from the helper", async () => {
|
||||
const bytes = validPng;
|
||||
const screenshot = {
|
||||
mime_type: "image/png",
|
||||
base64: bytes.toString("base64"),
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
width: 1,
|
||||
height: 1,
|
||||
};
|
||||
const result = await observeNativeUi(
|
||||
target,
|
||||
"observe_native_ui",
|
||||
{ ...scope, screenshot: true },
|
||||
{
|
||||
invoke: async () => ({
|
||||
ok: true,
|
||||
result: { ...snapshot, screenshot },
|
||||
}),
|
||||
},
|
||||
);
|
||||
expect(result.ok).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("native UI capture selection and budgets", () => {
|
||||
it("accepts observations and scenarios without compatibility flags", async () => {
|
||||
let calls = 0;
|
||||
const invoke = async () => {
|
||||
calls++;
|
||||
return { ok: true, result: snapshot };
|
||||
};
|
||||
expect(
|
||||
(
|
||||
await observeNativeUi(
|
||||
target,
|
||||
"observe_native_ui",
|
||||
{ ...scope, observation_approved: false },
|
||||
{ invoke },
|
||||
)
|
||||
).ok,
|
||||
).toBe(false);
|
||||
expect(
|
||||
(
|
||||
await observeNativeUi(
|
||||
target,
|
||||
"capture_native_ui_scenario",
|
||||
{
|
||||
...scope,
|
||||
restore: "leave-as-is",
|
||||
steps: [{ kind: "click", path: [] }],
|
||||
},
|
||||
{ invoke },
|
||||
)
|
||||
).ok,
|
||||
).toBe(false);
|
||||
expect(calls).toBe(0);
|
||||
const observed = await observeNativeUi(
|
||||
target,
|
||||
"observe_native_ui",
|
||||
{ pid: 123, window_id: 456, screenshot: false, accessibility: true },
|
||||
{ invoke },
|
||||
);
|
||||
expect(observed.ok).toBe(true);
|
||||
const captured = await observeNativeUi(
|
||||
target,
|
||||
"capture_native_ui_scenario",
|
||||
{
|
||||
pid: 123,
|
||||
window_id: 456,
|
||||
screenshot: false,
|
||||
accessibility: true,
|
||||
steps: [{ kind: "click", path: [] }],
|
||||
},
|
||||
{ invoke },
|
||||
);
|
||||
expect(captured.ok).toBe(true);
|
||||
expect(calls).toBe(3);
|
||||
});
|
||||
it("returns caller-selected large observations and complete action lists", async () => {
|
||||
const nodeLimits: unknown[] = [];
|
||||
const steps = Array.from({ length: 17 }, () => ({
|
||||
kind: "wait" as const,
|
||||
milliseconds: 0,
|
||||
}));
|
||||
const result = await observeNativeUi(
|
||||
target,
|
||||
"capture_native_ui_scenario",
|
||||
{
|
||||
pid: 123,
|
||||
window_id: 456,
|
||||
screenshot: false,
|
||||
accessibility: true,
|
||||
max_nodes: 2_001,
|
||||
steps,
|
||||
},
|
||||
{
|
||||
invoke: async (parameters) => {
|
||||
nodeLimits.push(parameters.max_nodes);
|
||||
return { ok: true, result: snapshot };
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(result.ok).toBe(true);
|
||||
expect(nodeLimits).toEqual(Array.from({ length: 18 }, () => 2_001));
|
||||
if (result.ok) {
|
||||
expect(result.value.steps).toHaveLength(17);
|
||||
expect(
|
||||
result.value.steps.every(({ outcome }) => outcome === "completed"),
|
||||
).toBe(true);
|
||||
}
|
||||
});
|
||||
it("accepts caller-selected waits beyond the old aggregate cutoff", async () => {
|
||||
const controller = new AbortController();
|
||||
const result = await observeNativeUi(
|
||||
target,
|
||||
"capture_native_ui_scenario",
|
||||
{
|
||||
pid: 123,
|
||||
window_id: 456,
|
||||
screenshot: false,
|
||||
accessibility: true,
|
||||
steps: Array.from({ length: 4 }, () => ({
|
||||
kind: "wait" as const,
|
||||
milliseconds: 10_001,
|
||||
})),
|
||||
},
|
||||
{
|
||||
signal: controller.signal,
|
||||
invoke: async () => {
|
||||
controller.abort();
|
||||
return { ok: true, result: snapshot };
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok)
|
||||
expect(result.value.steps[0]).toMatchObject({
|
||||
kind: "wait",
|
||||
outcome: "cancelled",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("native UI target and cancellation failures", () => {
|
||||
it.each([
|
||||
"accessibility-denied",
|
||||
"screen-recording-denied",
|
||||
@@ -84,8 +230,6 @@ describe("native UI authority and lifecycle", () => {
|
||||
"capture_native_ui_scenario",
|
||||
{
|
||||
...scope,
|
||||
actions_approved: true,
|
||||
restore: "leave-as-is",
|
||||
steps: [
|
||||
{ kind: "scroll", path: [1], direction: "increment" },
|
||||
{ kind: "click", path: [2] },
|
||||
@@ -123,8 +267,6 @@ describe("native UI authority and lifecycle", () => {
|
||||
"capture_native_ui_scenario",
|
||||
{
|
||||
...scope,
|
||||
actions_approved: true,
|
||||
restore: "leave-as-is",
|
||||
steps: [
|
||||
{ kind: "wait", milliseconds: 1000 },
|
||||
{ kind: "click", path: [] },
|
||||
|
||||
@@ -28,13 +28,13 @@ const responseSchema = z.discriminatedUnion("ok", [
|
||||
message: z.string(),
|
||||
}),
|
||||
]);
|
||||
/** Narrow process seam preserves the native helper's permission and target failures. */
|
||||
/** Narrow process seam preserves the native helper's OS and target failures. */
|
||||
export type NativeUiHelper = (
|
||||
parameters: Readonly<Record<string, unknown>>,
|
||||
signal?: AbortSignal,
|
||||
) => Promise<unknown>;
|
||||
|
||||
/** Capture a selected existing app window, with separately approved bounded scenarios. */
|
||||
/** Capture a selected existing app window and run explicitly selected scenarios. */
|
||||
export const observeNativeUi = async (
|
||||
target: BinaryTarget,
|
||||
operation: "observe_native_ui" | "capture_native_ui_scenario",
|
||||
@@ -84,16 +84,6 @@ const observeWithHelper = async (
|
||||
operation === "capture_native_ui_scenario"
|
||||
? nativeUiScenarioInputSchema.parse(parameters).steps
|
||||
: [];
|
||||
const waitMs = steps.reduce(
|
||||
(sum, step) => sum + (step.kind === "wait" ? step.milliseconds : 0),
|
||||
0,
|
||||
);
|
||||
if (waitMs > 30_000)
|
||||
return err(
|
||||
new AnalysisInputError(operation, {
|
||||
cause: new Error("Scenario waits may total at most 30000 ms"),
|
||||
}),
|
||||
);
|
||||
const signal =
|
||||
options.signal === undefined
|
||||
? AbortSignal.timeout(180_000)
|
||||
@@ -248,7 +238,7 @@ const observeWithHelper = async (
|
||||
"Click uses AXPress; scroll uses AXIncrement/AXDecrement; key-entry sets the selected element's AXValue. Unsupported elements fail without global event fallback.",
|
||||
"Each completed action is followed immediately by capture; delayed UI changes require explicit wait steps. A failed after-capture can follow a completed action, so failure does not prove absence of application effects.",
|
||||
"Scenarios can change app data, cause app network activity and persist changes. The caller explicitly chooses leave-as-is; automatic restoration is unsupported.",
|
||||
"Window matching uses exact PID and window ID for screenshots and unique accessibility geometry. Accessibility paths can change as the UI changes; stale or ambiguous paths fail. Screenshots are scaled to at most 2048 pixels, each PNG to 8 MiB, total captures to 64 MiB, and scenario duration to 180 seconds.",
|
||||
"Window matching uses exact PID and window ID for screenshots and unique accessibility geometry. Accessibility paths can change as the UI changes; stale or ambiguous paths fail. Screenshots are scaled to at most 2048 pixels; scenario output is budgeted at 64 MiB and execution is cancelled after 180 seconds.",
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user