Files
reverse-engineering-anything/src/desktop/AgentEnvSolveHelper.ts
T
morluto df1e647266 refactor(desktop): let build-computer and agentenv cleanup errors read their owner's release (#10855)
* refactor(errors): read cleanup completion from dependencies instead of pushing it to dependents

AnalysisError.cleanupIncomplete becomes a getter over the error's own unresolved state and its bound dependencies. The WeakRef dependents graph and resolveCleanup's push propagation are removed, and subclasses mark their initial state through markCleanupIncomplete instead of overriding a public mutable field.

* fix(errors): settle bound dependencies before binding more

A composite whose dependencies had released but whose status was never read
stayed incomplete when a later dependency was bound.

* refactor(errors): let errors read a lease group's release instead of resolving them (#10759)

AnalysisCleanupLeaseGroup no longer calls resolveCleanup() on its error.
The error holds the group as a release claim and reads cleanupReleased when
asked, so completion follows the lease owner without a push.

* chore(verify): count resolveCleanup and bindCleanupDependencies calls so they only shrink (#10763)

* refactor(errors): let errors read a lease group's release instead of resolving them

AnalysisCleanupLeaseGroup no longer calls resolveCleanup() on its error.
The error holds the group as a release claim and reads cleanupReleased when
asked, so completion follows the lease owner without a push.

* chore(verify): count resolveCleanup and bindCleanupDependencies calls so they only shrink

* refactor(process): let worker cleanup errors read their root epoch's release

OwnedWorkerRuntime's root epoch is now an AnalysisCleanupReleaseClaim. Errors bound to it read its release when asked instead of the epoch pushing resolveCleanup() through weak references, and a runtime without a retained root binds an already-released owner. Removes the OwnedWorkerCleanupFailure structural type and four resolveCleanup() pushes; the counted baseline shrinks accordingly.

* refactor(pcap): let pcap cleanup errors read command and snapshot release (#10792)

Retained pcap commands, retained snapshot failures and snapshot completion descriptors now own their release and errors read it: commands and snapshot failures implement AnalysisCleanupReleaseClaim, and a failed completion-descriptor close is retried through an AnalysisCleanupLease bound to its error. Removes four resolveCleanup() pushes; the counted baseline shrinks accordingly.

* refactor(process): let Darwin helper cleanup errors read their owner's release

Darwin helper cache and run-token reader root cleanup now record an AnalysisCleanupRelease that every failure reports; a successful retry confirms it. Replaces the first-failure anchor that later failures depended on and the resolveCleanup() push. Removes two resolveCleanup() and two bindCleanupDependencies() calls; both counted baselines shrink.

* refactor(ghidra): let Ghidra cleanup errors read the client's and checkpoint's release

GhidraClient and GhidraCheckpoint bind each cleanup failure to an AnalysisCleanupRelease and confirm it when a later close or retry releases everything, instead of keeping a set of failures and pushing resolveCleanup() to each. Removes two resolveCleanup() calls; the counted baseline shrinks.

* refactor(process): let debugger session cleanup errors read a confirmed stop

OwnedDebuggerSessionRegistry binds each retained gdb or rizin cleanup failure to the session's AnalysisCleanupRelease and confirms it when a stop is confirmed, keeping the latest failure for retry reporting, instead of pushing resolveCleanup() to a set of failures. Removes one resolveCleanup() call; the counted baseline shrinks.

* refactor(process): drop the debugger registry's unused closeOperation option

Returning the latest retained failure removed the last reader of closeOperation; remove the option and the values gdb and rizin supplied.

* refactor(server): let environment shutdown errors read a successful close

serveEnvironment binds each shutdown failure to an AnalysisCleanupRelease that a later successful close confirms, instead of pushing resolveCleanup() to a set of failures. The state-file retry test now also requires the earlier report to read the release. Removes one resolveCleanup() call; the counted baseline shrinks.

* refactor(desktop): release retained screen-judge cleanup through its lease

Each retained judge or screenshot cleanup owner is wrapped in an AnalysisCleanupLease bound to its failure; close() releases the lease, which records the release the failure reads, instead of closing the owner and pushing resolveCleanup(). Removes one resolveCleanup() call; the counted baseline shrinks.

* refactor(ida): let IDA close failures read the session's later close

IdaSessionClient's close failure binds an owner that is both its resource owner and its release claim, read from the client's closed state, instead of keeping weak references and pushing resolveCleanup() after a successful retry. Removes one resolveCleanup() call; the counted baseline shrinks.

* refactor(javascript): let source-map codec cleanup errors read a later close (#10814)

* refactor(javascript): let source-map codec cleanup errors read a later close

OwnedCodec binds each close failure to an AnalysisCleanupRelease that a later successful close confirms, replacing the first-failure anchor that later failures depended on and the resolveCleanup() push. Removes one resolveCleanup() and one bindCleanupDependencies() call; both counted baselines shrink.

* refactor(cutter): let pending Cutter command errors read the completion receipt (#10815)

* refactor(cutter): let pending Cutter command errors read the completion receipt

Each pending Cutter command keeps an AnalysisCleanupRelease bound to its timeout failure; the producer's terminal completion receipt confirms it instead of pushing resolveCleanup(). Removes one resolveCleanup() call; the counted baseline shrinks.

* refactor(application): let provider candidate cleanup errors read the candidate's close (#10817)

* refactor(application): let provider candidate cleanup errors read the candidate's close

AnalysisProviderRegistry keeps one AnalysisCleanupRelease per candidate whose resource close is unconfirmed; failures bind to it and a later confirmed close confirms it, instead of pushing resolveCleanup() to a set of failures. Removes one resolveCleanup() call; the counted baseline shrinks.

* fix(errors): bind a cleanup release claim to an error at most once

AnalysisError kept release claims in an array, so a provider whose retried close returned the same error bound the same claim again on every retry and each cleanupIncomplete read scanned the duplicates. Keep the claims in a set.

* refactor(application): let reverse-tool cleanup errors read a verified stop (#10819)

* refactor(application): let reverse-tool cleanup errors read a verified stop

Each retained provider process group in ReverseEngineeringService keeps an AnalysisCleanupRelease; its command and retry failures bind to it and a later verified stop confirms it, instead of collecting failures and pushing resolveCleanup(). Removes one resolveCleanup() call; the counted baseline shrinks.

* refactor(sqlite): let SQLite worker and root cleanup errors read their release (#10821)

Each retained SQLite snapshot cleanup records an AnalysisCleanupRelease for its worker and for its runtime root; the resource failures bind to them and a verified stop or root removal confirms them, instead of pushing resolveCleanup(). Composite failures keep reading those resource failures. Removes two resolveCleanup() calls; the counted baseline shrinks.

* refactor(android): let JADX cleanup errors read their retained cleanup's release

Each retained JADX cleanup records an AnalysisCleanupRelease for the admission failure's descriptor owners and one for the whole retained cleanup. The admission failure and every error the cleanup reports bind to them, and a confirmed close confirms them, instead of pushing resolveCleanup() through WeakRefs to reported errors. Removes two resolveCleanup() calls; the counted baseline shrinks.

* refactor(application): let session close failures read the client's later close

A retained session cleanup records an AnalysisCleanupRelease; unowned cleanup failures the client's close reported bind to it and the successful close confirms it, instead of the session collecting them and pushing resolveCleanup(). Failures that already follow their own owner keep reading it. Adds a regression for an unowned close failure. Removes one resolveCleanup() call; the counted baseline shrinks.

* refactor(desktop): let build-computer and agentenv cleanup errors read their owner's release

Build-computer teardown, retained task readers, remote reader folders and agentenv solve sandboxes each record an AnalysisCleanupRelease that their bound failures read; a verified teardown or stop confirms it instead of pushing resolveCleanup() through WeakRef sets. Long-run job failures read whether every job they name has confirmed its stop, so a failure naming two jobs no longer reads complete when only one stops (regression added). WeakRef sets kept only where late facts (completion receipts, reader views) are still retained as diagnostics. Removes six resolveCleanup() calls; the counted baseline shrinks.

* refactor(kuna): let source-map and Kuna cache cleanup errors read their owner's release (#10856)

* refactor(browser): let late source-map fetch errors read their response's release

A pending source-map fetch and a response cancellation each record an AnalysisCleanupRelease that their cancellation or deadline failure reads; releasing the late response or the reader lock confirms it instead of pushing resolveCleanup(). Removes two resolveCleanup() calls; the counted baseline shrinks.

* refactor(kuna): let project-cache cleanup errors read each owner's release

Each retained Kuna resource owner (process, staging, hash descriptor, retired file) records a release that the errors reporting it read; an error completes once every owner it reports confirms, and retirement that moves staging to a retained file forwards the staging owner's release to the file's. Replaces the WeakRef report list, its pruning and the resolveCleanup() push. Removes one resolveCleanup() call; the counted baseline shrinks.

* refactor(process): forbid resolveCleanup() pushes; native markers read their supervisor's release (#10857)

* refactor(providers): let an attributed client failure read its source's completed cleanup

Re-attributing a failure whose source cleanup already completed now binds a claim reading that source's release instead of pushing resolveCleanup() onto the new error; a pending source is still bound as a dependency. Resources stay as history, as before. Removes one resolveCleanup() call; the counted baseline shrinks.

* refactor(process): let native command markers read their supervisor's release

ProviderProcessSupervisor records an AnalysisCleanupRelease that a stop proving the process ended confirms. Native command and LLDB resolver cleanup markers bind to their supervisor's release, so a verified stop by either owner completes them, instead of each owner pushing resolveCleanup() onto the markers it tracks. Removes the last two resolveCleanup() calls outside the error base; the counted baseline is empty.

* chore(verify): forbid resolveCleanup() pushes outside the error base

Every production owner now confirms a release its errors read, so the counted baseline is empty. The guard becomes an outright ban with no baseline entry.

* test(native): complete native command and LLDB markers through a direct supervisor stop

* refactor(errors): complete an error's cleanup from its owner's record and every dependency (#10892)

* refactor(errors): complete an error's cleanup from its owner's record and every dependency

* refactor(errors): complete test errors through an owner release and delete resolveCleanup() (#10898)

* refactor(android): compose JADX and Apktool cleanup failures through one shared owner (#10900)

* refactor(android): compose JADX and Apktool cleanup failures through one shared owner

* refactor(application): compose service cleanup failures through the shared owner (#10902)
2026-11-15 20:06:47 +08:00

360 lines
12 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { fileURLToPath } from "node:url";
import { z } from "zod";
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
import { jsonValueSchema } from "../domain/jsonValue.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { AnalysisCleanupRelease } from "../domain/analysisCleanupRelease.js";
import { readBoundedControlLines } from "../process/BoundedControlLines.js";
import { cleanupOwnedProcessGroup } from "../process/ProcessOwnership.js";
import {
ProviderProcessSupervisor,
spawnOwnedProviderProcess,
} from "../process/ProviderProcess.js";
import { SolveSandboxRoot } from "./SolveSandboxRoot.js";
import {
AGENTENV_PROVIDER,
agentEnvInterpreter,
removeSandbox,
} from "./AgentEnvComputerHelper.js";
const HELPER = fileURLToPath(
new URL("../../bridge/agentenv/solve.py", import.meta.url),
);
const REPORT_BYTES = 256 * 1024;
const STDERR_BYTES = 16 * 1024;
/** The longest one solve may take, its deploy and teardown included, before REA stops it. */
const SOLVE_MS = 45 * 60_000;
/** How long a stopped solve has to tear its computer down. */
const STOP_MS = 120_000;
const POLL_MS = 1_000;
/** How long REA waits for the report once the helper's process group is cleared. */
const REPORT_DRAIN_MS = 5_000;
const cleanupSchema = z.strictObject({
unknown: z.boolean(),
sandboxes: z.array(
z.strictObject({ id: z.string().min(1), type: z.string().nullable() }),
),
});
const reportSchema = z.union([
z.strictObject({
outcome: z.enum([
"passed",
"scored below 1",
"unscored",
"failed",
"cancelled",
]),
instance_id: z.string().nullable(),
seconds: z.number().nonnegative(),
checks: z.array(
z.strictObject({
id: z.string(),
passed: z.boolean(),
description: z.string(),
}),
),
turns: z.int().nonnegative().nullable(),
usd: z.number().nonnegative().nullable(),
agentenv: z.string().nullable(),
error: z.string().nullable(),
cleanup: cleanupSchema.optional(),
}),
z.strictObject({ error: z.string(), cleanup: cleanupSchema.optional() }),
]);
/** What the helper printed: how the solve ended, or why no solve ran. */
export type SolveReport = z.infer<typeof reportSchema>;
export type SolveCleanupOwner = Pick<
ProviderProcessSupervisor,
"stop" | "dispose"
>;
/** Failed cleanup retains both its owner and any report the helper completed. */
export class SolveCleanupError extends ProviderCleanupError {
constructor(
readonly report: SolveReport | undefined,
resources: readonly string[],
diagnostics: Readonly<
Record<string, import("../domain/jsonValue.js").JsonValue>
>,
operation: string,
unknown = false,
) {
super(
AGENTENV_PROVIDER,
resources,
{
...diagnostics,
...(report === undefined
? {}
: { solve_report: jsonValueSchema.parse(report) }),
},
{
operation,
remediationAction: unknown
? "REA could not verify cleanup of every sandbox under this solve's private root. Closing REA retries cleanup of the retained root; inspect its agentenv records and Docker containers for anything still running."
: "Closing REA retries this cleanup; check that no agentenv run of the bundle is still up with docker ps.",
},
);
}
}
/** A completed report remains available when cancellation arrives during teardown. */
export class SolveCancelledError extends AnalysisCancelledError {
constructor(
readonly report: SolveReport,
operation: string,
reason: unknown,
) {
super(operation, {
cause: reason,
diagnostics: { solve_report: jsonValueSchema.parse(report) },
});
}
}
const parseReport = (line: string): SolveReport => {
try {
const parsed = reportSchema.safeParse(JSON.parse(line));
if (parsed.success) return parsed.data;
} catch {
// Reported below as a line that is not a report.
}
return {
error: "REA's solve helper wrote something other than its report",
cleanup: { unknown: true, sandboxes: [] },
};
};
/** How long a solve may run, and how long a stopped one has to exit before its process group is cleared. */
interface SolveTiming {
readonly solveMs: number;
readonly stopMs: number;
}
/**
* One solve of a bundle's task in a fresh computer, through REA's helper run
* with the agentenv runner's Python; the solver's model endpoint is the one
* agentenv reads from the environment or its config. Cancelling it stops the
* helper, which tears its computer down, and a helper still running after
* the stop grace has its process group cleared. A helper whose cleanup could
* not be confirmed goes to `retainCleanup`, so closing REA can retry it.
*/
export const runSolveHelper = async (options: {
readonly bundle: string;
readonly task: string;
readonly model: string;
readonly agent: string;
readonly environment: Readonly<NodeJS.ProcessEnv>;
readonly signal: AbortSignal;
readonly operation: string;
readonly retainCleanup: (owner: SolveCleanupOwner) => void;
readonly spawn?: typeof spawnOwnedProviderProcess;
readonly timing?: SolveTiming;
}): Promise<SolveReport> => {
const { environment, signal } = options;
const timing = options.timing ?? { solveMs: SOLVE_MS, stopMs: STOP_MS };
const interpreter = await agentEnvInterpreter(environment, options.operation);
const sandboxRoot = await SolveSandboxRoot.create(interpreter, environment);
let spawned: Awaited<ReturnType<typeof spawnOwnedProviderProcess>>;
try {
spawned = await (options.spawn ?? spawnOwnedProviderProcess)({
command: interpreter,
arguments: [
HELPER,
options.bundle,
options.task,
options.model,
options.agent,
],
runId: randomUUID(),
expectedCommand: null,
hostEnvironment: environment,
env: {
PYTHONDONTWRITEBYTECODE: "1",
AGENT_ENV_LOCAL_SANDBOX_DIR: sandboxRoot.path,
},
signal,
});
} catch (cause: unknown) {
const cleanup = await sandboxRoot.stop();
if (cleanup.status === "incomplete") {
const release = new AnalysisCleanupRelease();
const failure = new SolveCleanupError(
undefined,
[`sandbox root ${sandboxRoot.path}`],
{
reason: cleanup.reason,
spawn_error: cause instanceof Error ? cause.message : String(cause),
},
options.operation,
);
release.bind(
failure.bindCleanupResourceOwner({
get cleanupResources() {
return sandboxRoot.removed
? []
: [`sandbox root ${sandboxRoot.path}`];
},
}),
);
options.retainCleanup({
async stop() {
const result = await sandboxRoot.stop();
if (result.status === "verified-cleanup") release.confirm();
return result;
},
dispose: () => sandboxRoot.dispose(),
});
throw failure;
}
throw cause;
}
const supervisor = new ProviderProcessSupervisor(
{
...spawned,
ownsProcessLifetime: true,
cleanup:
spawned.cleanup ?? (() => cleanupOwnedProcessGroup(spawned.ownership)),
},
{
captureStdout: false,
maxDiagnosticBytes: { stdout: 0, stderr: STDERR_BYTES },
diagnosticRetention: "tail",
},
);
let report: SolveReport | undefined;
const reading = (async () => {
const stdout = spawned.process.stdout;
if (stdout === null) return;
for await (const line of readBoundedControlLines(stdout, REPORT_BYTES))
report ??= line.ok
? parseReport(line.value)
: {
error: "REA's solve helper's report is too long",
cleanup: { unknown: true, sandboxes: [] },
};
})().catch(() => undefined);
let stopped: Awaited<ReturnType<ProviderProcessSupervisor["stop"]>>;
const begun = Date.now();
try {
const deadline = begun + timing.solveMs;
while (
!signal.aborted &&
Date.now() < deadline &&
!(await supervisor.waitForExit(POLL_MS))
);
if (supervisor.observedExit() === undefined) {
spawned.process.kill("SIGTERM");
await supervisor.waitForExit(timing.stopMs);
}
} finally {
// Clearing the process group closes stdout, so the report is read only after it, and only so long.
stopped = await supervisor.stop();
await Promise.race([
reading,
new Promise((resolve) => setTimeout(resolve, REPORT_DRAIN_MS)),
]);
}
let processPending = stopped.status === "incomplete";
const pending = new Map(
(report?.cleanup?.sandboxes ?? []).map((item) => [item.id, item.type]),
);
let unknown = report?.cleanup?.unknown ?? true;
const resources = () => [
...(processPending
? [`process group ${String(spawned.ownership.processGroupId)}`]
: []),
...[...pending.keys()].map((id) => `agentenv sandbox ${id}`),
...(!sandboxRoot.removed ? [`sandbox root ${sandboxRoot.path}`] : []),
...(unknown ? ["agentenv sandbox identity unknown"] : []),
];
// Confirmed once a stop removes everything and identifies every sandbox.
const release = new AnalysisCleanupRelease();
const cleanupOwner: SolveCleanupOwner = {
async stop() {
if (processPending) {
const result = await supervisor.stop();
if (result.status === "incomplete") return result;
processPending = false;
}
const failures: string[] = [];
for (const [id, type] of pending) {
if (type !== null && type !== "local") {
failures.push(`Sandbox ${id} uses unsupported provider ${type}.`);
continue;
}
const failure = await removeSandbox(environment, id);
if (failure === undefined) pending.delete(id);
else failures.push(failure);
}
const rootCleanup = await sandboxRoot.stop();
if (rootCleanup.status === "incomplete")
failures.push(rootCleanup.reason);
if (failures.length > 0)
return { status: "incomplete", reason: failures.join(" ") };
// The solve's sandboxes are all local under its private root, and agentenv keeps a sandbox's folder until its teardown is confirmed, so an emptied root leaves none up.
if (sandboxRoot.removed) unknown = false;
if (!unknown) release.confirm();
return { status: "verified-cleanup" };
},
dispose: () => {
supervisor.dispose();
sandboxRoot.dispose();
},
};
const cleanup = processPending ? stopped : await cleanupOwner.stop();
if (cleanup.status === "incomplete" || unknown) {
if (cleanup.status === "incomplete") options.retainCleanup(cleanupOwner);
const reason =
cleanup.status === "incomplete"
? cleanup.reason
: "The helper ended before identifying every sandbox, and REA could not empty its sandbox root.";
const cleanupError = new SolveCleanupError(
report,
resources(),
{
reason,
leader_pid: spawned.ownership.leaderPid,
process_group_id: spawned.ownership.processGroupId,
helper_stderr: supervisor.snapshot().stderr.text,
},
options.operation,
unknown,
);
cleanupError.bindCleanupResourceOwner({
get cleanupResources() {
return resources();
},
});
throw release.bind(cleanupError);
}
if (signal.aborted && report !== undefined)
throw new SolveCancelledError(report, options.operation, signal.reason);
signal.throwIfAborted();
if (report !== undefined) return report;
const exit = supervisor.observedExit();
const stderr = supervisor.snapshot().stderr.text.slice(-2000);
// A helper a signal ended, REA's deadline or an outside kill, was stopped mid-solve; one that exited without a report never ran one.
if (exit?.signal != null)
return {
outcome: "failed",
instance_id: null,
seconds: (Date.now() - begun) / 1000,
checks: [],
turns: null,
usd: null,
agentenv: null,
error: `REA's solve helper was stopped (${exit.signal}) without a report: ${stderr}`,
cleanup: { unknown, sandboxes: [] },
};
return {
error: `REA's solve helper ended (exit ${String(exit?.code)}) without a report: ${stderr}`,
cleanup: { unknown, sandboxes: [] },
};
};