Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/agent/agent-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { stringTool } from "@intx/agent";
import type { AgentTool } from "@intx/agent";
import type { ToolDefinition } from "@intx/types/runtime";
import { type } from "arktype";
import { scrubSecretShapedToolResultContent } from "../plugins/tool-result-secret-scrub.js";
import { scrubSecretShapedContent } from "../plugins/tool-result-secret-scrub.js";
import type { AgentProfile } from "./profiles.js";

function tokenize(text: string): string[] {
Expand Down Expand Up @@ -86,7 +86,7 @@ export function formatAgentSearchResults(profiles: readonly AgentProfile[]): str
// SCRUBBABLE_TOOLS in tool-result-secret-scrub-plugin cannot reach it. Scrub here
// before the formatted string becomes a tool result (marketplace/plugin bodies may
// contain secret-shaped substrings).
return scrubSecretShapedToolResultContent(
return scrubSecretShapedContent(
[
"Matching agent profiles (pass id to spawn_agent(agent=...)). Full system prompt / body is included so you do not need read_file on plugin roots outside the workspace:",
"",
Expand Down
22 changes: 17 additions & 5 deletions src/agent/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
createSessionCostAccumulator,
type TurnBillingIdentity,
} from "../cost/session-cost.js";
import { inferenceErrorMessage } from "../inference-error-message.js";
import {
inferenceErrorMessage,
terminalProviderFailureMessage,
} from "../inference-error-message.js";

export interface Renderer {
render(event: ReactorEmittedEvent): void;
Expand Down Expand Up @@ -258,16 +261,25 @@ export function createRenderer(
case "inference.error": {
const err = e.data?.error as Record<string, unknown> | undefined;
const rawMessage = String(err?.message ?? e.data?.error ?? "inference error");
const message =
const classifiedError =
typeof err?.category === "string"
? inferenceErrorMessage({
? {
category: err.category,
message: rawMessage,
...(typeof err.statusCode === "number" ? { statusCode: err.statusCode } : {}),
...(err.raw !== undefined ? { raw: err.raw } : {}),
...(typeof err.providerId === "string" ? { providerId: err.providerId } : {}),
})
: rawMessage;
}
: undefined;
const message =
classifiedError === undefined
? rawMessage
: classifiedError.category === "quota_exhausted"
? inferenceErrorMessage(classifiedError)
: terminalProviderFailureMessage(
classifiedError.providerId ?? "Unknown",
classifiedError,
);
writeErrorBlock(message);
break;
}
Expand Down
27 changes: 27 additions & 0 deletions src/agent/retry-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ describe("createCorbitsRetryPolicy", () => {
expect(decision).toEqual({ kind: "retry", delayMs: 500 });
});

test("aborts an OpenCode Go malformed streamed SSE schema response", async () => {
const policy = createCorbitsRetryPolicy({ providerId: "opencode-go/corbits" });
const decision = await policy({
attempt: 1,
elapsedMs: 0,
error: {
category: "protocol_mismatch",
message:
"openai parseResponse: SSE chunk failed schema validation: choices0.delta.role must be a string (was null)",
},
});
expect(decision).toEqual({ kind: "abort" });
});

test("aborts a generic non-overload protocol mismatch", async () => {
const policy = createCorbitsRetryPolicy();
const decision = await policy({
attempt: 1,
elapsedMs: 0,
error: {
category: "protocol_mismatch",
message: "response did not match the provider protocol",
},
});
expect(decision).toEqual({ kind: "abort" });
});

test("aborts long-window quota exhaustion", async () => {
const policy = createCorbitsRetryPolicy();
const decision = await policy({
Expand Down
14 changes: 14 additions & 0 deletions src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { join, resolve } from "node:path";

import {
buildBifrostSource,
buildGoSource,
buildOpenAISource,
buildXaiSource,
buildProviderCatalog,
Expand Down Expand Up @@ -1178,6 +1179,19 @@ describe("loadConfig", () => {
});
});

describe("buildGoSource", () => {
test("routes chat-completions models through the OpenCode Go adapter", () => {
const source = buildGoSource({
id: "opencode-go",
apiKey: "sk-go",
model: "kimi-k2.7-code",
});

expect(source.provider).toBe("opencode-go");
expect(source.quirks).toBeUndefined();
});
});

describe("buildOpenAISource", () => {
test("normalizes the runtime source baseURL", () => {
const source = buildOpenAISource({
Expand Down
18 changes: 11 additions & 7 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
import { xaiUserIdFromAccessToken } from "../auth/xai/session.js";
import {
OPENCODE_GO_BASE_URL,
OPENCODE_GO_PROVIDER_ID,
isOpenCodeGoProvider,
resolveGoEndpoint,
} from "../../packages/opencode-go/src/index.js";
Expand Down Expand Up @@ -372,13 +373,16 @@ export function buildGoSource(fields: {
};
}
// chat-completions (default)
return buildOpenAISource({
id: fields.id,
baseURL: endpoint.baseURL.length > 0 ? endpoint.baseURL : OPENCODE_GO_BASE_URL,
apiKey,
model: fields.model,
...(fields.reasoningEffort !== undefined ? { reasoningEffort: fields.reasoningEffort } : {}),
});
return {
...buildOpenAISource({
id: fields.id,
baseURL: endpoint.baseURL.length > 0 ? endpoint.baseURL : OPENCODE_GO_BASE_URL,
apiKey,
model: fields.model,
...(fields.reasoningEffort !== undefined ? { reasoningEffort: fields.reasoningEffort } : {}),
}),
provider: OPENCODE_GO_PROVIDER_ID,
};
}

export interface Config {
Expand Down
43 changes: 35 additions & 8 deletions src/exec/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
isResolvedProviderFailureError,
terminalProviderFailureMessage,
} from "../inference-error-message.js";
import type { InferenceErrorLike } from "../inference-gateway-error.js";
import { collectToolPlugins, resolveToolPlugins } from "../plugins/tool-plugins.js";
import {
expandExistingPluginMembers,
Expand Down Expand Up @@ -132,19 +133,34 @@ export async function refreshSelectedProviderCredential<T>(refresh: () => Promis
}
}

function execTerminalProviderFailureMessage(
config: Config,
diagnostic: InferenceErrorLike,
): string {
const providerId = diagnostic.providerId ?? config.providerName;
const displayLabel =
providerId === config.providerName
? config.settings?.providers[config.providerName]?.name
: undefined;
return terminalProviderFailureMessage(providerId, diagnostic, displayLabel);
}

export function execUserFailureMessage(
config: Config,
err: unknown,
providerFailureObserved: boolean,
providerError?: InferenceErrorLike,
): string {
if (err instanceof Error && err.name === SELECTED_PROVIDER_FAILURE) {
return CREDENTIAL_FAILURE_USER_MESSAGE;
}
if (providerError === undefined && isResolvedProviderFailureError(err)) return err.message;
if (providerFailureObserved || isResolvedProviderFailureError(err)) {
return terminalProviderFailureMessage(
config.providerName,
config.settings?.providers[config.providerName]?.name,
);
const diagnostic = providerError ?? {
category: "fatal",
message: err instanceof Error ? err.message : String(err),
};
return execTerminalProviderFailureMessage(config, diagnostic);
}
return formatCaughtError(err);
}
Expand Down Expand Up @@ -327,6 +343,7 @@ export async function runExec(config: Config): Promise<ExecResult> {
let turnsUsed = 0;
let runSink: RunSink | null = null;
let providerFailureObserved = false;
let providerError: InferenceErrorLike | undefined;

const persist = async (
status: "running" | "done" | "failed" | "cancelled",
Expand Down Expand Up @@ -756,8 +773,18 @@ export async function runExec(config: Config): Promise<ExecResult> {
const sink = (event: ReactorEmittedEvent): void => {
if (event.type === "inference.start" || event.type === "inference.done") {
providerFailureObserved = false;
providerError = undefined;
} else if (event.type === "inference.error") {
providerFailureObserved = true;
const error = event.data.error;
providerError = {
category: error.category,
...(error.message !== undefined ? { message: error.message } : {}),
...(error.statusCode !== undefined ? { statusCode: error.statusCode } : {}),
...("providerId" in error && typeof error.providerId === "string"
? { providerId: error.providerId }
: {}),
};
}
liveSink.sink(event);
cycleRecorder.handleEvent(event);
Expand Down Expand Up @@ -883,9 +910,9 @@ export async function runExec(config: Config): Promise<ExecResult> {
(summaryStatus === "cancelled" ? "run cancelled before completion" : "run failed");
const userMessage =
summaryStatus === "failed"
? terminalProviderFailureMessage(
config.providerName,
config.settings?.providers[config.providerName]?.name,
? execTerminalProviderFailureMessage(
config,
providerError ?? { category: "unknown", message: diagnosticMessage },
)
: diagnosticMessage;
stderr.write(`Error: ${userMessage}\n`);
Expand Down Expand Up @@ -922,7 +949,7 @@ export async function runExec(config: Config): Promise<ExecResult> {
} catch (err) {
const diagnosticMessage = formatCaughtError(err);
logger.error("exec failed: {error}", { error: diagnosticMessage });
const userMessage = execUserFailureMessage(config, err, providerFailureObserved);
const userMessage = execUserFailureMessage(config, err, providerFailureObserved, providerError);
stderr.write(`Error: ${userMessage}\n`);
await persist("failed", { error: diagnosticMessage });
return {
Expand Down
Loading
Loading