Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

refactor(acp): the startup recovery decision moves to its own module so core.ts stays under its size ratchet

RGB-loop6966bc9dba66
diff --git a/server/drivers/acp/core.ts b/server/drivers/acp/core.ts
index 509a6465d7..56b5535fdc 100644
--- a/server/drivers/acp/core.ts
+++ b/server/drivers/acp/core.ts
@@ -62,7 +62,7 @@ import { AcpHost } from "./host.ts";
import { trackRunningTool, type AcpAskFinish, type AcpSession, type AcpTurn } from "./turn-state.ts";
import { recoveryPromptFor } from "../../resume-recovery.ts";
import { engineIdleMs, sessionIdlePolicy } from "../session-idle.ts";
-import { classifyError } from "../retry.ts";
+import { startupFailureRecoverable } from "./startup-recovery.ts";
import { engineFailureKind, engineFailureMessage, stopCause } from "../engine-errors.ts";
import { narrowsNativeTools, parseToolScope } from "../../../shared/tool-scope.ts";
import { acpMcpServers, type AcpMcpServer } from "../turn-mcp.ts"; import { acpToolOutput, acpToolProgress } from "../tool-progress.ts";
@@ -1157,7 +1157,6 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
const state: AcpTurn["state"] = { settled: false, promptSent: false, text: "", textMessageId: null, producedItem: false, startupActivity: false, stopped: false, usagePeak: null, usageLast: null, usageCompacted: false, cancelling: false };
let acknowledge = () => {};
let rejectStartup = (_error: TurnNotStartedError) => {};
- const ackAfterActivity = Boolean(turn.startupRecovery && support.promptUnreachedModel);
const startupAck = turn.startupRecovery ? new Promise<{ turnId: string }>((resolve, reject) => {
acknowledge = () => resolve({ turnId });
rejectStartup = reject;
@@ -1481,7 +1480,7 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
}
if (state.settled || state.stopped || session.closing) throw new Error("turn stopped");
state.promptSent = true;
- if (!ackAfterActivity) acknowledge();
+ if (!support.promptUnreachedModel) acknowledge();
const promptIdleMs = promptIdleTimeoutMs();
const quiet = startQuietWatch(threadId, session, current);
const result = await request(
@@ -1571,13 +1570,8 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
const needsAuth = code === "invalid_credentials" || code === "inactive_subscription"
|| code === "insufficient_funds" || message === support.loginNote;
const accountError = isAccountError(code);
- const failure = classifyError({ text: message });
- const transientStartup = failure.transient || (failure.reason === "unknown" && code === "upstream_outage");
- const denied = /\b(?:(?:permission|access) denied|(?:approval|permission) (?:required|denied|rejected)|requires? (?:approval|permission)|policy (?:restriction|violation)|(?:blocked|denied|restricted) by (?:the )?policy)\b/i.test(message);
- const retryableStartup = approvalUnconfirmed || (transientStartup && (!code || code === "upstream_outage")
- && ![-32700, -32600, -32601, -32602].includes((e as any)?.code));
- if (turn.startupRecovery && ((retryableStartup && !state.promptSent) || (ackAfterActivity && (e as any)?.acpPromptStall !== true && support.promptUnreachedModel!(e)))
- && !needsAuth && !accountError && !denied && !state.startupActivity && !state.producedItem && !state.text
+ if (turn.startupRecovery && startupFailureRecoverable(e, message, code, { approvalUnconfirmed, promptSent: state.promptSent, promptUnreachedModel: support.promptUnreachedModel?.(e) === true })
+ && !needsAuth && !accountError && !state.startupActivity && !state.producedItem && !state.text
&& !state.stopped && !session.closing && !asks.size && !current.runningTools.size) {
// Quiesce first: the close must not publish a completion before the harness recovers.
state.settled = true;
diff --git a/server/drivers/acp/opencode-go.test.ts b/server/drivers/acp/opencode-go.test.ts
index 2995414201..a0959f889e 100644
--- a/server/drivers/acp/opencode-go.test.ts
+++ b/server/drivers/acp/opencode-go.test.ts
@@ -336,7 +336,7 @@ describe("OpenCode catalog", () => {
it.each([
apiError("Internal error: Invalid API key."),
apiError("Internal error: socket hang up"),
- // OpenCode issue 27270: a timeout may have reached the model
+ // OpenCode issue 27270: only the refusal wording is proven, other connect failures stay ordinary errors
apiError("Internal error: Cannot connect to API: Connect Timeout Error"),
Object.assign(new Error("Cannot connect to API: ECONNREFUSED"), { code: -32602 }),
new Error("ECONNREFUSED"),
diff --git a/server/drivers/acp/opencode-go.ts b/server/drivers/acp/opencode-go.ts
index 2724d558cb..6b12335746 100644
--- a/server/drivers/acp/opencode-go.ts
+++ b/server/drivers/acp/opencode-go.ts
@@ -923,8 +923,9 @@ export const classifyOpenCodeGoError = classifyOpenCodeError;
/** OpenCode 1.19.4 retries a model server that refuses the connection for about a minute, then answers session/prompt
* `{code: -32603, message: "Internal error: Cannot connect to API: ECONNREFUSED: Unable to connect. Is the computer able
* to access the url?", data: {service: "session", errorName: "APIError"}}`; 1.18.27 drops "ECONNREFUSED: " and other
- * builds say "ConnectionRefused". A "Connect Timeout Error" may have reached the model, so it does not count. It answers
- * the same after a tool already ran, so only the absence of prompt activity proves nothing started. */
+ * builds say "ConnectionRefused". Only the refusal wording is proven; other connect failures such as "Connect Timeout
+ * Error" stay ordinary errors. It answers the same after a tool already ran, so only the absence of prompt activity
+ * proves nothing started. */
export function openCodePromptUnreachedModel(error: unknown): boolean {
const value = error && typeof error === "object" ? error as Record<string, unknown> : {};
if (value.code !== -32603) return false;
diff --git a/server/drivers/acp/startup-recovery.ts b/server/drivers/acp/startup-recovery.ts
new file mode 100644
index 0000000000..5a5ad7094d
--- /dev/null
+++ b/server/drivers/acp/startup-recovery.ts
@@ -0,0 +1,21 @@
+// Whether a turn that failed to start may go to automatic recovery, which closes it and lets the
+// backup engine answer instead (createAcpDriver's session/prompt catch). core.ts still checks that
+// the turn did nothing: no account or sign-in failure, no activity, text, tool or question.
+
+import type { ProviderErrorCode } from "../../contracts.ts";
+import { classifyError } from "../retry.ts";
+
+const DENIED = /\b(?:(?:permission|access) denied|(?:approval|permission) (?:required|denied|rejected)|requires? (?:approval|permission)|policy (?:restriction|violation)|(?:blocked|denied|restricted) by (?:the )?policy)\b/i;
+
+/** Before the prompt went out: a transient failure or an unconfirmed approval. After it: the harness
+ * says the prompt never reached its model (AcpSupport.promptUnreachedModel); a prompt stall does not
+ * count, since the model may still be working. A policy or permission refusal never recovers. */
+export function startupFailureRecoverable(error: unknown, message: string, code: ProviderErrorCode | undefined,
+ turn: { approvalUnconfirmed: boolean; promptSent: boolean; promptUnreachedModel: boolean }): boolean {
+ const failure = classifyError({ text: message });
+ const transientStartup = failure.transient || (failure.reason === "unknown" && code === "upstream_outage");
+ const retryableStartup = turn.approvalUnconfirmed || (transientStartup && (!code || code === "upstream_outage")
+ && ![-32700, -32600, -32601, -32602].includes((error as any)?.code));
+ return ((retryableStartup && !turn.promptSent) || (turn.promptUnreachedModel && (error as any)?.acpPromptStall !== true))
+ && !DENIED.test(message);
+}