Files
reverse-engineering-anything/src/desktop/ScreenJudgeProcess.ts
T

233 lines
8.1 KiB
TypeScript

import { AnalysisCleanupLease } from "../resources/index.js";
import { randomUUID } from "node:crypto";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { z } from "zod";
import type { ScreenJudge } from "../domain/desktop/appTaskScreen.js";
import { AnalysisError } from "../domain/analysisErrorBase.js";
import { cancelledBy, carriedReason } from "../domain/analysisErrorCore.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { providerCleanupErrorFromFailures } from "../domain/analysisErrorCleanup.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { RetainedCommandCleanup } from "../process/RetainedCommandCleanup.js";
import {
OwnedCommandFailure,
runOwnedCommand,
} from "../process/OwnedCommand.js";
import { readCompletedCommandAnswer } from "../process/readCompletedCommandAnswer.js";
import { agentEnvInterpreter } from "./AgentEnvComputerHelper.js";
import { AGENTENV_PROVIDER } from "./AgentEnvProviderIdentity.js";
import type { ComputerRelease } from "../domain/desktop/computerRelease.js";
import { errorMessage } from "../kernel/errorMessage.js";
const JUDGE_TIMEOUT_MS = 10 * 60 * 1000;
const answerSchema = z.object({
id: z.string(),
result: z.boolean(),
description: z.string(),
});
const readJudgeAnswer = (stdout: string): Awaited<ReturnType<ScreenJudge>> => {
const answer = answerSchema.parse(
JSON.parse(stdout.trim().split("\n").at(-1) ?? ""),
);
return {
id: answer.id,
passed: answer.result,
description: answer.description,
};
};
/** A failure whose cleanup completes when its owner's lease is released. */
interface Retained {
readonly lease: AnalysisCleanupLease;
readonly failure: ProviderCleanupError;
}
const retained = (
owner: RetainedCommandCleanup,
failure: ProviderCleanupError,
): Retained => {
failure.bindCleanupResourceOwner(owner);
const lease = AnalysisCleanupLease.forCloseOwner(owner, "screen-judge");
lease.bindCleanupError(failure);
return { lease, failure };
};
/** Own the judge, its descendants and the screenshot until cleanup is confirmed. */
export class ScreenJudgeProcess {
readonly #retained = new Set<Retained>();
constructor(readonly environment: Readonly<NodeJS.ProcessEnv>) {}
readonly judge: ScreenJudge = async (verifier, check, image, signal) => {
const cleanup = await this.close();
if (!cleanup.verified) {
const pending = [...this.#retained].map((item) => item.failure);
if (pending.length === 1) throw pending[0];
throw providerCleanupErrorFromFailures(
AGENTENV_PROVIDER,
pending,
{ reason: cleanup.reason },
{
operation: "check_build_task",
remediationAction: "Close the build computer to retry judge cleanup.",
},
);
}
if (signal.aborted) throw cancelledBy("check_build_task", signal);
const python = await agentEnvInterpreter(
this.environment,
"check_build_task",
);
const folder = await mkdtemp(join(tmpdir(), "rea-screen-judge-"));
let retained = false;
let primary: { readonly cause: unknown } | undefined;
let outcome: Awaited<ReturnType<ScreenJudge>> | undefined;
try {
const screen = join(folder, "screen.jpg");
await writeFile(screen, image);
const snapshot = await runOwnedCommand(
{
command: python,
arguments: [verifier, "judge", JSON.stringify(check), screen],
runId: randomUUID(),
expectedCommand: null,
hostEnvironment: { ...this.environment },
env: { PYTHONDONTWRITEBYTECODE: "1" },
sweepTokenOwnedProcesses: true,
},
{ timeoutMs: JUDGE_TIMEOUT_MS, diagnosticBytes: 1024 * 1024 },
{ signal },
);
outcome = readJudgeAnswer(snapshot.stdout.text);
} catch (cause: unknown) {
primary = { cause };
const cleanup = this.#retainProcessFailure(folder, cause);
if (cleanup !== undefined) {
retained = true;
primary = { cause: cleanup };
} else if (cause instanceof OwnedCommandFailure) {
primary = {
cause:
cause.reason === "cancelled"
? cancelledBy(
"check_build_task",
carriedReason(cause.cancellation?.reason),
)
: new ProviderAdapterError(
AGENTENV_PROVIDER,
"check_build_task",
{
cause,
userMessage: `The bundle's screen judge failed: ${cause.snapshot?.stderr.text.trim().split("\n").slice(-3).join(" ") || cause.message}`,
},
),
};
const completed = readCompletedCommandAnswer(cause, readJudgeAnswer);
if (completed !== undefined && primary.cause instanceof AnalysisError)
primary.cause.retainDiagnostics({
completed_judge_result: completed,
});
}
}
if (!retained) await this.#removeScreen(folder, outcome, primary?.cause);
if (primary !== undefined) throw primary.cause;
if (outcome === undefined) throw new Error("Judge produced no outcome.");
return outcome;
};
async #removeScreen(
folder: string,
outcome: Awaited<ReturnType<ScreenJudge>> | undefined,
primary: unknown,
): Promise<void> {
try {
await rm(folder, { recursive: true, force: true });
} catch (cause: unknown) {
const failure = new ProviderCleanupError(
AGENTENV_PROVIDER,
[folder],
{
reason: errorMessage(cause),
...(outcome === undefined ? {} : { completed_judge_result: outcome }),
},
{
operation: "check_build_task",
cause: primary ?? cause,
...(primary instanceof AnalysisError
? { cleanupPrimary: primary }
: {}),
remediationAction:
"Close the build computer to retry screenshot cleanup.",
},
);
this.#retained.add(
retained(new RetainedCommandCleanup(folder, undefined), failure),
);
throw failure;
}
}
#retainProcessFailure(
folder: string,
cause: unknown,
): ProviderCleanupError | undefined {
const owner = new RetainedCommandCleanup(folder, cause);
if (!owner.hasPendingCommandResources) return undefined;
const completed = readCompletedCommandAnswer(cause, readJudgeAnswer);
const original =
cause instanceof OwnedCommandFailure && cause.reason === "cancelled"
? cancelledBy(
"check_build_task",
carriedReason(cause.cancellation?.reason),
)
: new ProviderAdapterError(AGENTENV_PROVIDER, "check_build_task", {
cause,
});
const message = errorMessage(cause);
const failure = new ProviderCleanupError(
AGENTENV_PROVIDER,
owner.cleanupResources,
{
reason:
(cause instanceof OwnedCommandFailure
? cause.cleanupFailure
: null) ?? message,
primary_failure: message,
...(completed === undefined
? {}
: { completed_judge_result: completed }),
},
{
operation: "check_build_task",
cause,
...(completed === undefined ? { cleanupPrimary: original } : {}),
remediationAction: "Close the build computer to retry judge cleanup.",
},
);
this.#retained.add(retained(owner, failure));
return failure;
}
/** Retry unconfirmed process and screenshot cleanup before releasing the computer. */
async close(): Promise<ComputerRelease> {
const failures: string[] = [];
const resources: string[] = [];
for (const item of this.#retained) {
try {
await item.lease.release();
this.#retained.delete(item);
} catch (cause: unknown) {
failures.push(errorMessage(cause));
resources.push(...(item.lease.cleanupResources ?? []));
}
}
return failures.length === 0
? { verified: true, teardown: "agentenv" }
: { verified: false, reason: failures.join("; "), resources };
}
}