refactor(process): own deadlines as one monotonic type with a finalize budget (#10723)

* refactor(process): own deadlines as one monotonic type with a finalize budget

Deadline (src/domain/deadline) holds a monotonic work budget and a separate finalize budget that starts when finalization begins. ProviderStartupDeadline is built on it and exposes finalize(), so a failed Ghidra startup drains launcher output under its own budget and the caller's signal instead of the expired startup signal. The six wall-clock deadline sums in src move onto Deadline; a counted guard keeps the one page-clock conversion and rejects new copies.

* refactor(process): share one finalize window across repeated beginFinalize calls
This commit is contained in:
morluto authored and GitHub committed 2026-11-15 13:45:27 +08:00
1 parent 52d6725d63
commit a67dbe07a4
15 files changed
+292 -56

No files matched your search

+3 -2
View File
@@ -6,6 +6,7 @@ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
import { z } from "zod";
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
import { Deadline } from "../domain/deadline.js";
import { jsonObjectSchema, type JsonValue } from "../domain/jsonValue.js";
import { StructuredContentMcpClient } from "../providers/StructuredContentMcpClient.js";
import { validateMcpHttpResponse } from "../providers/Utf8McpResponse.js";
@@ -640,7 +641,7 @@ export class AgentEnvComputer {
}
async #waitReady(signal: AbortSignal): Promise<void> {
const deadline = Date.now() + READY_TIMEOUT_MS;
const deadline = Deadline.after(READY_TIMEOUT_MS);
for (;;) {
const status = readinessSchema.parse(
await this.#request(
@@ -652,7 +653,7 @@ export class AgentEnvComputer {
if (status.ready) return;
const waiting = status.checks.filter((check) => !check.ok);
const failed = waiting.find((check) => check.final === true);
if (failed !== undefined || Date.now() > deadline)
if (failed !== undefined || deadline.expired())
throw new ComputerStartFailure(
this.redacted(
failed === undefined
+4 -3
View File
@@ -2,6 +2,7 @@ import { setTimeout as delay } from "node:timers/promises";
import { isOneColourPng } from "../browser/PngVisualDiff.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { Deadline } from "../domain/deadline.js";
/** How often REA retakes a first screenshot the desktop has not drawn on, and the time the retries share. */
const DRAWN_POLL_MS = 500;
@@ -31,15 +32,15 @@ export const drawnScreenshot = async <Shot extends Screenshot>(
take: (signal: AbortSignal) => Promise<Shot>,
signal: AbortSignal,
): Promise<Shot> => {
const deadline = Date.now() + DRAWN_WAIT_MS;
const deadline = Deadline.after(DRAWN_WAIT_MS);
let shot = await take(signal);
while (!shot.isError && undrawn(shot)) {
const left = deadline - Date.now();
const left = deadline.remainingMs();
if (left <= 0) break;
await delay(Math.min(DRAWN_POLL_MS, left), undefined, { signal });
const retry = AbortSignal.any([
signal,
AbortSignal.timeout(Math.max(1, deadline - Date.now())),
AbortSignal.timeout(Math.max(1, deadline.remainingMs())),
]);
try {
shot = await take(retry);