




















@@ -88,6 +88,8 @@ const GATEWAY_LIVE_SETUP_TIMEOUT_MS = Math.max(
8888);
8989const GATEWAY_LIVE_MODEL_TIMEOUT_MS = resolveGatewayLiveModelTimeoutMs();
9090const GATEWAY_LIVE_TRANSCRIPT_TIMEOUT_MS = resolveGatewayLiveTranscriptTimeoutMs();
91+const GATEWAY_LIVE_AGENT_RUN_TIMEOUT_MS = resolveGatewayLiveAgentRunTimeoutMs();
92+const GATEWAY_LIVE_AGENT_WAIT_TIMEOUT_MS = resolveGatewayLiveAgentWaitTimeoutMs();
9193const GATEWAY_LIVE_HEARTBEAT_MS = Math.max(
92941_000,
9395toInt(process.env.OPENCLAW_LIVE_GATEWAY_HEARTBEAT_MS, 30_000),
@@ -251,6 +253,24 @@ function resolveGatewayLiveTranscriptTimeoutMs(
251253return Math.max(stepTimeoutMs, modelTimeoutMs);
252254}
253255256+function resolveGatewayLiveAgentRunTimeoutMs(
257+modelTimeoutMs = GATEWAY_LIVE_MODEL_TIMEOUT_MS,
258+): number {
259+if (!Number.isFinite(modelTimeoutMs) || modelTimeoutMs <= 1_000) {
260+return Math.max(1_000, Math.floor(modelTimeoutMs));
261+}
262+const terminalGraceMs = Math.min(30_000, Math.max(5_000, Math.floor(modelTimeoutMs / 6)));
263+return Math.max(1_000, Math.floor(modelTimeoutMs - terminalGraceMs));
264+}
265+266+function resolveGatewayLiveAgentWaitTimeoutMs(
267+agentRunTimeoutMs = GATEWAY_LIVE_AGENT_RUN_TIMEOUT_MS,
268+modelTimeoutMs = GATEWAY_LIVE_MODEL_TIMEOUT_MS,
269+): number {
270+const waitGraceMs = Math.min(10_000, Math.max(1_000, Math.floor(modelTimeoutMs / 12)));
271+return Math.max(1_000, Math.min(modelTimeoutMs, Math.floor(agentRunTimeoutMs + waitGraceMs)));
272+}
273+254274function isGatewayLiveProbeTimeout(error: string): boolean {
255275return /probe timeout after \d+ms/i.test(error);
256276}
@@ -680,6 +700,41 @@ describe("resolveGatewayLiveTranscriptTimeoutMs", () => {
680700});
681701});
682702703+describe("resolveGatewayLiveAgentRunTimeoutMs", () => {
704+it("leaves terminal-observation grace inside the model timeout", () => {
705+expect(resolveGatewayLiveAgentRunTimeoutMs(180_000)).toBe(150_000);
706+});
707+708+it("keeps short live probes bounded but positive", () => {
709+expect(resolveGatewayLiveAgentRunTimeoutMs(6_000)).toBe(1_000);
710+});
711+});
712+713+describe("resolveGatewayLiveAgentWaitTimeoutMs", () => {
714+it("waits past the run timeout but before the model timeout", () => {
715+expect(resolveGatewayLiveAgentWaitTimeoutMs(150_000, 180_000)).toBe(160_000);
716+});
717+});
718+719+describe("formatGatewayLiveAgentWaitFailure", () => {
720+it("includes terminal attribution fields without requiring transcript text", () => {
721+expect(
722+formatGatewayLiveAgentWaitFailure({
723+context: "anthropic prompt",
724+runId: "run-1",
725+result: {
726+status: "timeout",
727+timeoutPhase: "provider",
728+providerStarted: true,
729+stopReason: "rpc",
730+},
731+}).message,
732+).toContain(
733+"anthropic prompt: agent.wait timeout for runId=run-1 (timeoutPhase=provider, providerStarted=true, stopReason=rpc)",
734+);
735+});
736+});
737+683738describe("assertGatewayLiveDidNotSkipAllDueToTimeout", () => {
684739it("allows all-skip runs when no timeout skip was involved", () => {
685740expect(() =>
@@ -1582,6 +1637,64 @@ async function waitForSessionAssistantText(params: {
15821637throw new Error(`${timeoutLabel} timeout after ${timeoutMs}ms (${params.context})`);
15831638}
158416391640+function formatGatewayLiveAgentWaitFailure(params: {
1641+context: string;
1642+runId: string;
1643+result: unknown;
1644+}): Error {
1645+const result = params.result as
1646+| {
1647+status?: unknown;
1648+error?: unknown;
1649+stopReason?: unknown;
1650+timeoutPhase?: unknown;
1651+providerStarted?: unknown;
1652+}
1653+| null
1654+| undefined;
1655+const status = typeof result?.status === "string" ? result.status : "unknown";
1656+const details = [
1657+typeof result?.timeoutPhase === "string" ? `timeoutPhase=${result.timeoutPhase}` : undefined,
1658+typeof result?.providerStarted === "boolean"
1659+ ? `providerStarted=${String(result.providerStarted)}`
1660+ : undefined,
1661+typeof result?.stopReason === "string" ? `stopReason=${result.stopReason}` : undefined,
1662+typeof result?.error === "string" ? `error=${result.error}` : undefined,
1663+].filter((value): value is string => Boolean(value));
1664+return new Error(
1665+`${params.context}: agent.wait ${status} for runId=${params.runId}${
1666+ details.length > 0 ? ` (${details.join(", ")})` : ""
1667+ }`,
1668+);
1669+}
1670+1671+async function waitForGatewayAgentRun(params: {
1672+client: GatewayClient;
1673+runId: string;
1674+context: string;
1675+timeoutMs?: number;
1676+}): Promise<void> {
1677+const timeoutMs = params.timeoutMs ?? GATEWAY_LIVE_TRANSCRIPT_TIMEOUT_MS;
1678+const result = await params.client.request(
1679+"agent.wait",
1680+{
1681+runId: params.runId,
1682+ timeoutMs,
1683+},
1684+{
1685+timeoutMs: timeoutMs + 5_000,
1686+},
1687+);
1688+if ((result as { status?: unknown } | undefined)?.status === "ok") {
1689+return;
1690+}
1691+throw formatGatewayLiveAgentWaitFailure({
1692+context: params.context,
1693+runId: params.runId,
1694+ result,
1695+});
1696+}
1697+15851698async function requestGatewayAgentText(params: {
15861699client: GatewayClient;
15871700sessionKey: string;
@@ -1599,27 +1712,55 @@ async function requestGatewayAgentText(params: {
15991712const baselineAssistantCount = (
16001713await readSessionAssistantTexts(params.sessionKey, params.modelKey)
16011714).length;
1715+const runId = params.idempotencyKey;
16021716const accepted = await withGatewayLiveProbeTimeout(
16031717params.client.request("agent", {
16041718sessionKey: params.sessionKey,
1605-idempotencyKey: params.idempotencyKey,
1719+idempotencyKey: runId,
16061720message: params.message,
16071721thinking: params.thinkingLevel,
16081722deliver: false,
1723+timeout: Math.ceil(GATEWAY_LIVE_AGENT_RUN_TIMEOUT_MS / 1_000),
16091724attachments: params.attachments,
16101725}),
16111726`${params.context}: agent-accept`,
16121727);
16131728if (accepted?.status !== "accepted") {
16141729throw new Error(`agent status=${String(accepted?.status)}`);
16151730}
1616-return await waitForSessionAssistantText({
1731+const transcriptPromise = waitForSessionAssistantText({
16171732sessionKey: params.sessionKey,
16181733 baselineAssistantCount,
16191734context: `${params.context}: transcript-final`,
16201735modelKey: params.modelKey,
16211736timeoutLabel: "model",
16221737timeoutMs: GATEWAY_LIVE_TRANSCRIPT_TIMEOUT_MS,
1738+}).then((text) => ({ kind: "transcript" as const, text }));
1739+const agentWaitPromise = waitForGatewayAgentRun({
1740+client: params.client,
1741+ runId,
1742+context: `${params.context}: agent-wait`,
1743+timeoutMs: GATEWAY_LIVE_AGENT_WAIT_TIMEOUT_MS,
1744+}).then(
1745+() => ({ kind: "agent-ok" as const }),
1746+(error: unknown) => ({ kind: "agent-error" as const, error }),
1747+);
1748+const first = await Promise.race([transcriptPromise, agentWaitPromise]);
1749+if (first.kind === "transcript") {
1750+void agentWaitPromise.catch(() => undefined);
1751+return first.text;
1752+}
1753+void transcriptPromise.catch(() => undefined);
1754+if (first.kind === "agent-error") {
1755+throw first.error instanceof Error ? first.error : new Error(String(first.error));
1756+}
1757+return await waitForSessionAssistantText({
1758+sessionKey: params.sessionKey,
1759+ baselineAssistantCount,
1760+context: `${params.context}: transcript-after-agent-wait`,
1761+modelKey: params.modelKey,
1762+timeoutLabel: "probe",
1763+timeoutMs: GATEWAY_LIVE_PROBE_TIMEOUT_MS,
16231764});
16241765}
16251766@@ -2128,7 +2269,7 @@ async function runGatewayModelSuite(params: GatewayModelSuiteParams) {
21282269`[${params.label}] running ${params.candidates.length} models (thinking=${params.thinkingLevel})`,
21292270);
21302271logProgress(
2131-`[${params.label}] heartbeat=${Math.max(1, Math.round(GATEWAY_LIVE_HEARTBEAT_MS / 1_000))}s probe-timeout=${Math.max(1, Math.round(GATEWAY_LIVE_PROBE_TIMEOUT_MS / 1_000))}s model-timeout=${Math.max(1, Math.round(GATEWAY_LIVE_MODEL_TIMEOUT_MS / 1_000))}s transcript-timeout=${Math.max(1, Math.round(GATEWAY_LIVE_TRANSCRIPT_TIMEOUT_MS / 1_000))}s`,
2272+`[${params.label}] heartbeat=${Math.max(1, Math.round(GATEWAY_LIVE_HEARTBEAT_MS / 1_000))}s probe-timeout=${Math.max(1, Math.round(GATEWAY_LIVE_PROBE_TIMEOUT_MS / 1_000))}s agent-timeout=${Math.max(1, Math.round(GATEWAY_LIVE_AGENT_RUN_TIMEOUT_MS / 1_000))}s agent-wait=${Math.max(1, Math.round(GATEWAY_LIVE_AGENT_WAIT_TIMEOUT_MS / 1_000))}s model-timeout=${Math.max(1, Math.round(GATEWAY_LIVE_MODEL_TIMEOUT_MS / 1_000))}s transcript-timeout=${Math.max(1, Math.round(GATEWAY_LIVE_TRANSCRIPT_TIMEOUT_MS / 1_000))}s`,
21322273);
21332274const anthropicKeys = collectAnthropicApiKeys();
21342275if (anthropicKeys.length > 0) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。