Files
reverse-engineering-anything/src/application/ClientDeadlines.ts
T
morluto 089dfa3a8b fix(setup): derive client deadlines from one record per client (#15371)
* fix(setup): derive client deadlines from one record per client

Each supported client definition now declares the MCP deadlines it applies
to REA's server: key, unit, built-in default, maximum, coverage and whether
progress resets it. Setup raises a shorter or missing deadline to REA's
recommendation within the client's maximum and keeps a longer one; doctor
reports the same shortfall from the same record. Cline, Zed, Antigravity,
Hermes, Goose and Gemini CLI gain deadlines; the codex/kimi/grok/dsh name
branches, the Goose timeout literal and the per-client startup constants
are removed.

Refs #15337

* fix(setup): keep a raised Goose timeout over a short inherited one and expect Antigravity's deadline
2026-12-01 01:48:59 +00:00

207 lines
8.1 KiB
TypeScript

import {
MCP_STARTUP_POLICY,
mcpProviderDeadlineAllowanceMs,
mcpProviderDeadlineControl,
recommendedMcpToolTimeoutMs,
type McpToolTimeoutInputs,
} from "../config/mcpStartupPolicy.js";
/** A key in REA's own server entry that a client reads for one deadline. */
export interface ClientDeadlineSetting {
readonly key: string;
readonly unit: "seconds" | "milliseconds";
}
/**
* One deadline a client applies to REA's MCP server, as the client's own
* source or documentation states it. Registration and doctor both derive from
* this record; a fact that is not verified stays absent rather than guessed.
*/
export interface ClientDeadline {
readonly covers: "startup" | "tool_call" | "startup_and_tool_call";
/**
* Per-server keys the client reads, in the client's precedence order. Setup
* writes the first one; the others are read-only alternatives.
*/
readonly settings: readonly [
ClientDeadlineSetting,
...ClientDeadlineSetting[],
];
/** The client's built-in value when no key is set; absent when unknown. */
readonly defaultMs?: number;
/** The largest value the client applies; configured values are clamped to it. */
readonly maximumMs?: number;
/** Whether a progress notification restarts the deadline. */
readonly progressResets: boolean | "unknown";
/** Where these facts come from. */
readonly source: string;
}
/** The value a registration holds for one deadline. */
type ClientDeadlineValue =
| {
readonly kind: "configured";
readonly setting: ClientDeadlineSetting;
/** The value the client applies, after its maximum. */
readonly ms: number;
}
| { readonly kind: "default"; readonly ms: number }
/** Absent with no known default, or malformed: setup writes it. */
| { readonly kind: "unset" }
/** A caller-owned expression whose value REA cannot read. */
| { readonly kind: "expression"; readonly setting: ClientDeadlineSetting };
/** One deadline compared with what REA needs, and what setup writes for it. */
export interface ClientDeadlinePlan {
readonly deadline: ClientDeadline;
readonly current: ClientDeadlineValue;
readonly requiredMs: number;
/** The value setup writes to the first setting; absent when it keeps the entry. */
readonly write?: {
readonly setting: ClientDeadlineSetting;
readonly value: number;
};
/** The deadline after setup still falls short of `requiredMs`. */
readonly shortfallMs?: number;
}
const unitMs = (setting: ClientDeadlineSetting): number =>
setting.unit === "seconds" ? 1000 : 1;
/**
* Marks a deadline value the dialect parser found to be a caller-owned
* expression, whose value REA cannot read. A malformed literal is unset.
*/
export const CLIENT_DEADLINE_EXPRESSION = Symbol("client deadline expression");
const readDeadline = (
deadline: ClientDeadline,
entry: Readonly<Record<string, unknown>>,
): ClientDeadlineValue => {
// The client selects the first key that is present, valid or not.
const setting = deadline.settings.find(({ key }) => entry[key] !== undefined);
if (setting === undefined)
return deadline.defaultMs === undefined
? { kind: "unset" }
: { kind: "default", ms: deadline.defaultMs };
const value = entry[setting.key];
if (value === CLIENT_DEADLINE_EXPRESSION)
return { kind: "expression", setting };
// TOML integers arrive as bigint. Values too large for exact Number
// conversion already exceed any requirement.
const number =
typeof value === "bigint" || typeof value === "number"
? Number(value)
: Number.NaN;
if (!(Number.isFinite(number) && number > 0)) return { kind: "unset" };
const ms = number * unitMs(setting);
return {
kind: "configured",
setting,
ms:
deadline.maximumMs === undefined ? ms : Math.min(ms, deadline.maximumMs),
};
};
const requiredDeadlineMs = (
deadline: ClientDeadline,
inputs: McpToolTimeoutInputs,
): number =>
deadline.covers === "startup"
? MCP_STARTUP_POLICY.clientStartupTimeoutMs
: Math.max(
recommendedMcpToolTimeoutMs(inputs),
deadline.covers === "startup_and_tool_call"
? MCP_STARTUP_POLICY.clientStartupTimeoutMs
: 0,
);
/**
* Compare each deadline with REA's requirement. Setup raises a shorter or
* unset value to the requirement, within the client's maximum, and never
* lowers a longer one. A deadline that progress restarts is covered by REA's
* provider heartbeats, and an expression keeps the caller's value.
*/
export const planClientDeadlines = (
deadlines: readonly ClientDeadline[],
entry: Readonly<Record<string, unknown>>,
inputs: McpToolTimeoutInputs,
): readonly ClientDeadlinePlan[] =>
deadlines.map((deadline) => {
const current = readDeadline(deadline, entry);
const requiredMs = requiredDeadlineMs(deadline, inputs);
if (deadline.progressResets === true || current.kind === "expression")
return { deadline, current, requiredMs };
const currentMs = current.kind === "unset" ? undefined : current.ms;
if (currentMs !== undefined && currentMs >= requiredMs)
return { deadline, current, requiredMs };
const [setting] = deadline.settings;
const targetMs = Math.min(requiredMs, deadline.maximumMs ?? requiredMs);
const value = Math.ceil(targetMs / unitMs(setting));
const writtenMs = value * unitMs(setting);
const write =
currentMs === undefined || writtenMs > currentMs
? { setting, value }
: undefined;
const resultMs = write === undefined ? currentMs! : writtenMs;
return {
deadline,
current,
requiredMs,
...(write === undefined ? {} : { write }),
...(resultMs < requiredMs ? { shortfallMs: resultMs } : {}),
};
});
const formatValue = (ms: number, setting: ClientDeadlineSetting): string =>
setting.unit === "seconds"
? `${String(Math.ceil(ms / 1000))} seconds`
: `${String(ms)} ms`;
const coverage = (deadline: ClientDeadline): string =>
deadline.covers === "startup"
? "startup"
: deadline.covers === "tool_call"
? "tool-call"
: "startup and tool-call";
/**
* Explain each deadline that falls short of REA's requirement, with both
* numbers and the one change that fixes it. Setup applies a writable change;
* a client maximum below the requirement leaves REA's provider deadline as
* the setting to lower.
*/
export const clientDeadlineAdvisories = (
clientName: string,
plans: readonly ClientDeadlinePlan[],
inputs: McpToolTimeoutInputs,
): readonly string[] =>
plans.flatMap((plan): readonly string[] => {
const { deadline, current, requiredMs, write, shortfallMs } = plan;
if (current.kind === "expression")
return [
`The effective timeout is unknown because ${current.setting.key} is a caller-owned expression.`,
];
if (write === undefined && shortfallMs === undefined) return [];
const [setting] = deadline.settings;
const observed =
current.kind === "configured"
? `The configured ${current.setting.key} (${formatValue(current.ms, current.setting)})`
: current.kind === "default"
? `${clientName}'s built-in ${coverage(deadline)} timeout (${formatValue(current.ms, setting)}, used while ${setting.key} is unset)`
: `${setting.key} is unset, so the ${coverage(deadline)} timeout`;
const need =
deadline.covers === "startup"
? `REA's ${formatValue(requiredMs, setting)} client startup recommendation`
: `the ${formatValue(requiredMs, setting)} REA recommends: its known provider allowance (${String(mcpProviderDeadlineAllowanceMs(inputs))} ms) plus ${formatValue(MCP_STARTUP_POLICY.toolOperationHeadroomMs, setting)} of operation headroom`;
const repair =
write === undefined
? ""
: ` Setup writes ${setting.key} = ${String(write.value)}; it never lowers a longer value.`;
const limit =
shortfallMs === undefined || deadline.maximumMs === undefined
? ""
: ` ${clientName} caps ${setting.key} at ${formatValue(deadline.maximumMs, setting)}, so lower ${mcpProviderDeadlineControl(inputs)} or split long work into smaller calls.`;
return [`${observed} is shorter than ${need}.${repair}${limit}`];
});