File size: 14,908 Bytes
5cb63c1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | import fs from "node:fs";
import http from "node:http";
import path from "node:path";
import { afterEach, expect, it, onTestFinished } from "vitest";
import { withTestTimeout } from "../../../test/helpers/promise.js";
import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js";
import {
claimAgentRunApprovalAuthority,
claimAgentRunDelegatedAuthority,
releaseAgentRunDelegatedAuthority,
validateAgentRunDelegatedAuthority,
} from "../../infra/agent-run-registry.js";
import { createCompiledSdkHost } from "../../plugins/compiled-sdk-host.test-support.js";
import { registerComputerUseProvider } from "../../plugins/computer-use-contract.js";
import { createPluginRecord } from "../../plugins/loader-records.js";
import { computerUseSdkEntrypoint } from "../../plugins/loader-sdk-bridge-artifacts.test-support.js";
import { createEmptyPluginRegistry } from "../../plugins/registry-empty.js";
import {
createColdPluginConfig,
createColdPluginFixture,
} from "../../plugins/test-helpers/cold-plugin-fixtures.js";
import { createDeferredCore } from "../../shared/deferred.js";
import { createOpenClawTestState } from "../../test-utils/openclaw-test-state.js";
import { createAgentRuntimeApprovalAuthorityValidator } from "../agent-runtime-identity-token.js";
import { createGatewayAuxHandlers } from "../server-aux-handlers.js";
import { createDirectChatContext } from "../server-chat.agent-events.test-helpers.js";
import { computerHandlers } from "../server-methods/computer.js";
import type { RespondFn } from "../server-methods/types.js";
import { createGatewayComputerService } from "./computer-service.js";
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
it.each(["direct-run", "parent-closes-scope", "scope-closes"] as const)(
"cancels %s authority after native input is queued in the real child and before its final effect",
async (mode) => {
const state = await createOpenClawTestState({ label: "computer-authority" });
const entered = createDeferredCore<http.ServerResponse>();
const blockerEntered = createDeferredCore();
const revokedQueued = createDeferredCore();
const revokedCancelled = createDeferredCore();
const gate = http.createServer((request, response) => {
if (request.url === "/gate/allowed") {
entered.resolve(response);
return;
}
if (request.url === "/gate/blocker") {
blockerEntered.resolve();
return;
}
if (request.url === "/queued/revoked") {
revokedQueued.resolve();
}
if (request.url === "/cancelled/revoked") {
revokedCancelled.resolve();
}
response.writeHead(204).end();
});
onTestFinished(async () => {
gate.closeAllConnections();
if (gate.listening) {
await new Promise<void>((resolve, reject) => {
gate.close((error) => (error ? reject(error) : resolve()));
});
}
await state.cleanup();
});
await new Promise<void>((resolve, reject) => {
gate.once("error", reject);
gate.listen(0, "127.0.0.1", resolve);
});
const address = gate.address();
if (!address || typeof address === "string") {
throw new Error("Native input gate did not acquire a loopback port");
}
const pluginId = "fixture-authority-computer";
const pluginRoot = state.path("plugin");
fs.mkdirSync(pluginRoot);
const fixture = createColdPluginFixture({
rootDir: pluginRoot,
pluginId,
manifest: { providers: [], channels: [], channelConfigs: {}, providerAuthChoices: [] },
});
const events = state.path("native-events.txt");
const allowedEffect = state.path("allowed-effect.txt");
const revokedEffect = state.path("revoked-effect.txt");
const blockerEffect = state.path("blocker-effect.txt");
fs.writeFileSync(
fixture.runtimeSource,
`
const fs = require("node:fs");
const http = require("node:http");
const { registerComputerUseProvider } = require("openclaw/plugin-sdk/computer-use");
const record = (event) => fs.appendFileSync(${JSON.stringify(events)}, event + "\\n");
const effects = ${JSON.stringify({ allowed: allowedEffect, revoked: revokedEffect, blocker: blockerEffect })};
const request = (route, signal) => new Promise((resolve, reject) => {
const req = http.get("http://127.0.0.1:${address.port}/" + route, { signal }, (response) => {
response.resume();
response.once("end", resolve);
response.once("error", reject);
});
req.once("error", reject);
});
process.once("exit", (code) => record("exit:" + code));
module.exports = {
id: ${JSON.stringify(pluginId)},
register(api) {
registerComputerUseProvider(api, {
id: ${JSON.stringify(pluginId)}, label: "Synthetic authority computer", isAvailable: () => true,
capabilities: () => ({
contractVersion: 2,
provider: { id: ${JSON.stringify(pluginId)}, label: "Synthetic authority computer", generation: "authority-generation" },
actions: ["screenshot", "type"], targets: ["screen"], deliveryModes: ["foreground"], observations: ["image"],
features: { recording: false, agentCursor: false, multiDisplay: false },
}),
async openExecution() {
let tail = Promise.resolve();
return {
async snapshot() { return JSON.stringify({ format: "png", base64: "c3ludGhldGlj" }); },
async act(paramsJSON, signal) {
if (!signal) throw new Error("Native input did not receive its cancellation lifetime");
const label = JSON.parse(paramsJSON).text;
if (!Object.hasOwn(effects, label)) throw new Error("Unexpected synthetic input");
const prior = tail;
const result = prior.then(async () => {
try {
signal.throwIfAborted();
await request("gate/" + label, signal);
signal.throwIfAborted();
fs.writeFileSync(effects[label], "applied");
record("effect:" + label);
return JSON.stringify({ ok: true });
} catch (error) {
if (signal.aborted) {
record("cancelled:" + label);
await request("cancelled/" + label);
}
throw error;
}
});
tail = result.catch(() => {});
await request("queued/" + label);
return await result;
},
async close(reason) { await tail; record("close:" + reason); },
};
},
});
},
};
`,
);
const config = createColdPluginConfig(pluginRoot, pluginId);
config.plugins!.allow = [pluginId];
config.desktop = { host: { enabled: true, managed: true } };
await state.writeConfig(config);
const sdkHost = createCompiledSdkHost(computerUseSdkEntrypoint, (prefix) =>
tempDirs.make(prefix),
);
const env = {
PATH: path.dirname(process.execPath),
HOME: state.home,
USERPROFILE: state.home,
OPENCLAW_HOME: state.home,
OPENCLAW_STATE_DIR: state.stateDir,
OPENCLAW_CONFIG_PATH: state.configPath,
OPENCLAW_DISABLE_BUNDLED_PLUGINS: "1",
...(sdkHost ? { OPENCLAW_DEV_SOURCE_ROOT: sdkHost } : {}),
NODE_ENV: "test",
};
const registry = createEmptyPluginRegistry();
registry.plugins.push(
createPluginRecord({
id: pluginId,
source: fixture.runtimeSource,
origin: "config",
enabled: true,
configSchema: true,
}),
);
registerComputerUseProvider(
{
registerNodeHostCommand: (command) =>
registry.nodeHostCommands.push({
pluginId,
pluginName: "Synthetic authority computer",
command,
source: fixture.runtimeSource,
}),
},
{
id: pluginId,
label: "Synthetic authority computer",
isAvailable: () => true,
capabilities: () => ({
contractVersion: 2,
provider: {
id: pluginId,
label: "Synthetic authority computer",
generation: "authority-generation",
},
actions: ["screenshot", "type"],
targets: ["screen"],
deliveryModes: ["foreground"],
observations: ["image"],
features: { recording: false, agentCursor: false, multiDisplay: false },
}),
openExecution: async () => {
throw new Error("Native execution must run in the child");
},
},
);
let desktopCurrent = true;
const service = createGatewayComputerService({
getConfig: () => config,
getPluginRegistry: () => registry,
hostDesktopService: {
observe: async () => {
throw new Error("Unexpected desktop observer");
},
status: async () => {
throw new Error("Unexpected desktop status");
},
acquireComputer: async () => ({
env,
isCurrent: () => desktopCurrent,
release: () => {
desktopCurrent = false;
},
}),
},
});
const parentAuthority = claimAgentRunDelegatedAuthority({
instanceId: "computer-live-instance",
runId: "computer-live-run",
});
const scopeLifetime = new AbortController();
const authority =
mode === "direct-run"
? parentAuthority
: claimAgentRunApprovalAuthority(parentAuthority, [scopeLifetime.signal]);
const unrelated = claimAgentRunApprovalAuthority(parentAuthority, [
new AbortController().signal,
]);
const aux = createGatewayAuxHandlers({
log: {},
getNativeApprovalRouteCoordinator: () => undefined,
activateRuntimeSecrets: async () => {
throw new Error("Unexpected secrets reload");
},
sharedGatewaySessionGenerationState: { current: undefined, required: null },
resolveSharedGatewaySessionGenerationForConfig: () => undefined,
clients: [],
channelManager: {
startChannel: async () => new Map(),
stopChannel: async () => {},
isManuallyStopped: () => false,
resolveRuntimeAccountId: (_channel, accountId) => accountId,
},
logChannels: { info: () => {} },
onAgentRunAuthorityClosed: (closed) => service.revokeRunAuthority(closed),
});
const validateAuthority = createAgentRuntimeApprovalAuthorityValidator();
const requestLifetime = new AbortController();
const pending: Promise<unknown>[] = [];
const invoke = async (text: string) => {
const params = {
command: "computer.act",
generation: "authority-generation",
params: { action: "type", text, executionId: "123e4567-e89b-42d3-a456-426614174000" },
idempotencyKey: text,
};
let response: Parameters<RespondFn> | undefined;
await computerHandlers["computer.invoke"]!({
req: { type: "req", id: text, method: "computer.invoke", params },
params,
signal: requestLifetime.signal,
respond: (...args) => {
response = args;
},
isWebchatConnect: () => false,
client: {
connId: "computer-agent-rpc",
connect: {
minProtocol: 1,
maxProtocol: 1,
client: { id: "gateway-client", version: "test", platform: "test", mode: "backend" },
},
internal: {
syntheticClient: true,
agentRuntimeIdentity: {
kind: "agentRuntime",
agentId: "main",
sessionKey: "agent:main:computer-proof",
operationalRunInstance: authority.operationalRunInstance,
delegatedAuthority: { ...authority, kind: "local" },
},
},
},
context: createDirectChatContext({
gatewayComputerService: service,
validateAgentRuntimeApprovalAuthority: validateAuthority,
}),
});
expect(response).toBeDefined();
return response!;
};
try {
expect(await service.status()).toMatchObject({ configured: true, available: true });
const allowed = invoke("allowed");
pending.push(allowed);
const releaseAllowed = await Promise.race([
entered.promise,
allowed.then(() => {
throw new Error("Allowed native input never reached its gate");
}),
]);
releaseAgentRunDelegatedAuthority(unrelated);
expect(validateAgentRunDelegatedAuthority(authority)).toBe(true);
releaseAllowed.writeHead(204).end();
expect((await allowed)[0]).toBe(true);
expect(fs.readFileSync(allowedEffect, "utf8")).toBe("applied");
const blocker = invoke("blocker");
pending.push(blocker);
await Promise.race([
blockerEntered.promise,
blocker.then(() => {
throw new Error("Blocking native input never reached its gate");
}),
]);
const revoked = invoke("revoked");
pending.push(revoked);
await Promise.race([
revokedQueued.promise,
revoked.then(() => {
throw new Error("Revoked native input never entered the child queue");
}),
]);
if (mode === "scope-closes") {
scopeLifetime.abort();
expect(validateAgentRunDelegatedAuthority(parentAuthority)).toBe(true);
} else {
expect(releaseAgentRunDelegatedAuthority(parentAuthority)).toBe(true);
}
expect(requestLifetime.signal.aborted).toBe(false);
// Observe native cancellation before explicit cleanup or the 60-second RPC deadline.
await withTestTimeout(
revokedCancelled.promise,
10_000,
"Run revocation did not cancel queued native input",
);
await service.close();
expect((await blocker)[0]).toBe(false);
expect((await revoked)[0]).toBe(false);
expect(fs.existsSync(blockerEffect)).toBe(false);
expect(fs.existsSync(revokedEffect)).toBe(false);
const nativeEvents = fs.readFileSync(events, "utf8");
expect(nativeEvents).toContain("cancelled:blocker\n");
expect(nativeEvents).toContain("cancelled:revoked\n");
expect(nativeEvents).toContain("close:gateway-disconnect\n");
expect(nativeEvents).toContain("exit:0\n");
expect(desktopCurrent).toBe(false);
console.log(
`[computer-authority-proof] ${mode}: live input applied; sibling claim closure preserved owner; revoked queued input cancelled before effect; native child exit joined`,
);
} finally {
releaseAgentRunDelegatedAuthority(authority);
releaseAgentRunDelegatedAuthority(parentAuthority);
releaseAgentRunDelegatedAuthority(unrelated);
requestLifetime.abort();
try {
await service.close();
await Promise.allSettled(pending);
} finally {
await aux.stopOperatorInteractions();
}
}
},
90_000,
);
|