Skip to content

Commit 2d80bbc

Browse files
committed
feat(agents): allow disabling PI harness fallback
1 parent 6e4d78c commit 2d80bbc

12 files changed

Lines changed: 273 additions & 31 deletions
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
a962c1d7ddffa15f2333854f77b03da4f6db07fada16f288377ee1daf50afc08 config-baseline.json
2-
3c8455d44a63d495ad295d2c9d76fed7a190b80344dabaa0e78ba433bf2d253b config-baseline.core.json
3-
df55c673a1cdbebc4fe68baaaf9d0d4289313be5034be92f0d510726a086b1d6 config-baseline.channel.json
4-
3f6fccab66a9abe7e1dd412fb01b13b944ed24edbe09df55ada3323acc7f76fe config-baseline.plugin.json
1+
c2705b6fbb297a6f06aefa6036db71aa5dbfea5a21ec3dafd53ed631cdc558f9 config-baseline.json
2+
b8e245d02a00b696af2b4f0447553dd3b5bb98ca805aca650fb2ce5c0487eacb config-baseline.core.json
3+
e1f94346a8507ce3dec763b598e79f3bb89ff2e33189ce977cc87d3b05e71c1d config-baseline.channel.json
4+
9153501720ea74f9356432a011fa9b41c9b700084bfe0d156feb5647624b35ad config-baseline.plugin.json

‎src/agents/harness/registry.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { selectAgentHarness } from "./selection.js";
1313
import type { AgentHarness } from "./types.js";
1414

1515
const originalRuntime = process.env.OPENCLAW_AGENT_RUNTIME;
16+
const originalHarnessFallback = process.env.OPENCLAW_AGENT_HARNESS_FALLBACK;
1617

1718
afterEach(() => {
1819
clearAgentHarnesses();
@@ -21,6 +22,11 @@ afterEach(() => {
2122
} else {
2223
process.env.OPENCLAW_AGENT_RUNTIME = originalRuntime;
2324
}
25+
if (originalHarnessFallback == null) {
26+
delete process.env.OPENCLAW_AGENT_HARNESS_FALLBACK;
27+
} else {
28+
process.env.OPENCLAW_AGENT_HARNESS_FALLBACK = originalHarnessFallback;
29+
}
2430
});
2531

2632
function makeHarness(

‎src/agents/harness/selection.test.ts‎

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { Api, Model } from "@mariozechner/pi-ai";
22
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import type { OpenClawConfig } from "../../config/config.js";
34
import type {
45
EmbeddedRunAttemptParams,
56
EmbeddedRunAttemptResult,
67
} from "../pi-embedded-runner/run/types.js";
78
import { clearAgentHarnesses, registerAgentHarness } from "./registry.js";
8-
import { runAgentHarnessAttemptWithFallback } from "./selection.js";
9+
import { runAgentHarnessAttemptWithFallback, selectAgentHarness } from "./selection.js";
910
import type { AgentHarness } from "./types.js";
1011

1112
const piRunAttempt = vi.fn(async () => createAttemptResult("pi"));
@@ -20,6 +21,7 @@ vi.mock("./builtin-pi.js", () => ({
2021
}));
2122

2223
const originalRuntime = process.env.OPENCLAW_AGENT_RUNTIME;
24+
const originalHarnessFallback = process.env.OPENCLAW_AGENT_HARNESS_FALLBACK;
2325

2426
afterEach(() => {
2527
clearAgentHarnesses();
@@ -29,9 +31,14 @@ afterEach(() => {
2931
} else {
3032
process.env.OPENCLAW_AGENT_RUNTIME = originalRuntime;
3133
}
34+
if (originalHarnessFallback == null) {
35+
delete process.env.OPENCLAW_AGENT_HARNESS_FALLBACK;
36+
} else {
37+
process.env.OPENCLAW_AGENT_HARNESS_FALLBACK = originalHarnessFallback;
38+
}
3239
});
3340

34-
function createAttemptParams(): EmbeddedRunAttemptParams {
41+
function createAttemptParams(config?: OpenClawConfig): EmbeddedRunAttemptParams {
3542
return {
3643
prompt: "hello",
3744
sessionId: "session-1",
@@ -45,6 +52,7 @@ function createAttemptParams(): EmbeddedRunAttemptParams {
4552
authStorage: {} as never,
4653
modelRegistry: {} as never,
4754
thinkLevel: "low",
55+
config,
4856
} as EmbeddedRunAttemptParams;
4957
}
5058

@@ -115,4 +123,64 @@ describe("runAgentHarnessAttemptWithFallback", () => {
115123
);
116124
expect(piRunAttempt).not.toHaveBeenCalled();
117125
});
126+
127+
it("disables PI retry fallback when auto-selected harness fails and fallback is none", async () => {
128+
process.env.OPENCLAW_AGENT_RUNTIME = "auto";
129+
registerFailingCodexHarness();
130+
131+
await expect(
132+
runAgentHarnessAttemptWithFallback(
133+
createAttemptParams({ agents: { defaults: { embeddedHarness: { fallback: "none" } } } }),
134+
),
135+
).rejects.toThrow("codex startup failed");
136+
expect(piRunAttempt).not.toHaveBeenCalled();
137+
});
138+
139+
it("honors env fallback override over config fallback", async () => {
140+
process.env.OPENCLAW_AGENT_RUNTIME = "auto";
141+
process.env.OPENCLAW_AGENT_HARNESS_FALLBACK = "none";
142+
registerFailingCodexHarness();
143+
144+
await expect(runAgentHarnessAttemptWithFallback(createAttemptParams())).rejects.toThrow(
145+
"codex startup failed",
146+
);
147+
expect(piRunAttempt).not.toHaveBeenCalled();
148+
});
149+
});
150+
151+
describe("selectAgentHarness", () => {
152+
it("fails instead of choosing PI when no plugin harness matches and fallback is none", () => {
153+
expect(() =>
154+
selectAgentHarness({
155+
provider: "anthropic",
156+
modelId: "sonnet-4.6",
157+
config: { agents: { defaults: { embeddedHarness: { fallback: "none" } } } },
158+
}),
159+
).toThrow("PI fallback is disabled");
160+
expect(piRunAttempt).not.toHaveBeenCalled();
161+
});
162+
163+
it("allows per-agent embedded harness policy overrides", () => {
164+
const config: OpenClawConfig = {
165+
agents: {
166+
defaults: { embeddedHarness: { fallback: "pi" } },
167+
list: [
168+
{ id: "main", default: true },
169+
{ id: "strict", embeddedHarness: { fallback: "none" } },
170+
],
171+
},
172+
};
173+
174+
expect(() =>
175+
selectAgentHarness({
176+
provider: "anthropic",
177+
modelId: "sonnet-4.6",
178+
config,
179+
sessionKey: "agent:strict:session-1",
180+
}),
181+
).toThrow("PI fallback is disabled");
182+
expect(selectAgentHarness({ provider: "anthropic", modelId: "sonnet-4.6", config }).id).toBe(
183+
"pi",
184+
);
185+
});
118186
});

‎src/agents/harness/selection.ts‎

Lines changed: 118 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1+
import type { OpenClawConfig } from "../../config/config.js";
2+
import type { AgentEmbeddedHarnessConfig } from "../../config/types.agents-shared.js";
13
import { createSubsystemLogger } from "../../logging/subsystem.js";
4+
import { normalizeAgentId } from "../../routing/session-key.js";
5+
import { listAgentEntries, resolveSessionAgentIds } from "../agent-scope.js";
26
import type { CompactEmbeddedPiSessionParams } from "../pi-embedded-runner/compact.js";
37
import type {
48
EmbeddedRunAttemptParams,
59
EmbeddedRunAttemptResult,
610
} from "../pi-embedded-runner/run/types.js";
7-
import { resolveEmbeddedAgentRuntime } from "../pi-embedded-runner/runtime.js";
11+
import {
12+
normalizeEmbeddedAgentRuntime,
13+
resolveEmbeddedAgentHarnessFallback,
14+
resolveEmbeddedAgentRuntime,
15+
type EmbeddedAgentHarnessFallback,
16+
type EmbeddedAgentRuntime,
17+
} from "../pi-embedded-runner/runtime.js";
818
import type { EmbeddedPiCompactResult } from "../pi-embedded-runner/types.js";
919
import { createPiAgentHarness } from "./builtin-pi.js";
1020
import { listRegisteredAgentHarnesses } from "./registry.js";
1121
import type { AgentHarness, AgentHarnessSupport } from "./types.js";
1222

1323
const log = createSubsystemLogger("agents/harness");
1424

15-
function listAvailableAgentHarnesses(): AgentHarness[] {
16-
return [...listRegisteredAgentHarnesses().map((entry) => entry.harness), createPiAgentHarness()];
25+
type AgentHarnessPolicy = {
26+
runtime: EmbeddedAgentRuntime;
27+
fallback: EmbeddedAgentHarnessFallback;
28+
};
29+
30+
function listPluginAgentHarnesses(): AgentHarness[] {
31+
return listRegisteredAgentHarnesses().map((entry) => entry.harness);
1732
}
1833

1934
function compareHarnessSupport(
@@ -27,21 +42,39 @@ function compareHarnessSupport(
2742
return left.harness.id.localeCompare(right.harness.id);
2843
}
2944

30-
export function selectAgentHarness(params: { provider: string; modelId?: string }): AgentHarness {
31-
const runtime = resolveEmbeddedAgentRuntime();
32-
const harnesses = listAvailableAgentHarnesses();
45+
export function selectAgentHarness(params: {
46+
provider: string;
47+
modelId?: string;
48+
config?: OpenClawConfig;
49+
agentId?: string;
50+
sessionKey?: string;
51+
}): AgentHarness {
52+
const policy = resolveAgentHarnessPolicy(params);
53+
// PI is intentionally not part of the plugin candidate list. It is the legacy
54+
// fallback path, so `fallback: "none"` can prove that only plugin harnesses run.
55+
const pluginHarnesses = listPluginAgentHarnesses();
56+
const piHarness = createPiAgentHarness();
57+
const runtime = policy.runtime;
58+
if (runtime === "pi") {
59+
return piHarness;
60+
}
3361
if (runtime !== "auto") {
34-
const forced = harnesses.find((entry) => entry.id === runtime);
62+
const forced = pluginHarnesses.find((entry) => entry.id === runtime);
3563
if (forced) {
3664
return forced;
3765
}
66+
if (policy.fallback === "none") {
67+
throw new Error(
68+
`Requested agent harness "${runtime}" is not registered and PI fallback is disabled.`,
69+
);
70+
}
3871
log.warn("requested agent harness is not registered; falling back to embedded PI backend", {
3972
requestedRuntime: runtime,
4073
});
41-
return createPiAgentHarness();
74+
return piHarness;
4275
}
4376

44-
const supported = harnesses
77+
const supported = pluginHarnesses
4578
.map((harness) => ({
4679
harness,
4780
support: harness.supports({
@@ -60,16 +93,34 @@ export function selectAgentHarness(params: { provider: string; modelId?: string
6093
)
6194
.toSorted(compareHarnessSupport);
6295

63-
return supported[0]?.harness ?? createPiAgentHarness();
96+
const selected = supported[0]?.harness;
97+
if (selected) {
98+
return selected;
99+
}
100+
if (policy.fallback === "none") {
101+
throw new Error(
102+
`No registered agent harness supports ${formatProviderModel(params)} and PI fallback is disabled.`,
103+
);
104+
}
105+
return piHarness;
64106
}
65107

66108
export async function runAgentHarnessAttemptWithFallback(
67109
params: EmbeddedRunAttemptParams,
68110
): Promise<EmbeddedRunAttemptResult> {
69-
const runtime = resolveEmbeddedAgentRuntime();
111+
const policy = resolveAgentHarnessPolicy({
112+
provider: params.provider,
113+
modelId: params.modelId,
114+
config: params.config,
115+
agentId: params.agentId,
116+
sessionKey: params.sessionKey,
117+
});
70118
const harness = selectAgentHarness({
71119
provider: params.provider,
72120
modelId: params.modelId,
121+
config: params.config,
122+
agentId: params.agentId,
123+
sessionKey: params.sessionKey,
73124
});
74125
if (harness.id === "pi") {
75126
return harness.runAttempt(params);
@@ -78,7 +129,7 @@ export async function runAgentHarnessAttemptWithFallback(
78129
try {
79130
return await harness.runAttempt(params);
80131
} catch (error) {
81-
if (runtime !== "auto") {
132+
if (policy.runtime !== "auto" || policy.fallback === "none") {
82133
throw error;
83134
}
84135
log.warn(`${harness.label} failed; falling back to embedded PI backend`, { error });
@@ -92,9 +143,64 @@ export async function maybeCompactAgentHarnessSession(
92143
const harness = selectAgentHarness({
93144
provider: params.provider ?? "",
94145
modelId: params.model,
146+
config: params.config,
147+
sessionKey: params.sessionKey,
95148
});
96149
if (!harness.compact) {
97150
return undefined;
98151
}
99152
return harness.compact(params);
100153
}
154+
155+
export function resolveAgentHarnessPolicy(params: {
156+
provider?: string;
157+
modelId?: string;
158+
config?: OpenClawConfig;
159+
agentId?: string;
160+
sessionKey?: string;
161+
env?: NodeJS.ProcessEnv;
162+
}): AgentHarnessPolicy {
163+
const env = params.env ?? process.env;
164+
// Harness policy can be session-scoped because users may switch between agents
165+
// with different strictness requirements inside the same gateway process.
166+
const agentPolicy = resolveAgentEmbeddedHarnessConfig(params.config, {
167+
agentId: params.agentId,
168+
sessionKey: params.sessionKey,
169+
});
170+
const defaultsPolicy = params.config?.agents?.defaults?.embeddedHarness;
171+
const runtime = env.OPENCLAW_AGENT_RUNTIME?.trim()
172+
? resolveEmbeddedAgentRuntime(env)
173+
: normalizeEmbeddedAgentRuntime(agentPolicy?.runtime ?? defaultsPolicy?.runtime);
174+
return {
175+
runtime,
176+
fallback:
177+
resolveEmbeddedAgentHarnessFallback(env) ??
178+
normalizeAgentHarnessFallback(agentPolicy?.fallback ?? defaultsPolicy?.fallback),
179+
};
180+
}
181+
182+
function resolveAgentEmbeddedHarnessConfig(
183+
config: OpenClawConfig | undefined,
184+
params: { agentId?: string; sessionKey?: string },
185+
): AgentEmbeddedHarnessConfig | undefined {
186+
if (!config) {
187+
return undefined;
188+
}
189+
const { sessionAgentId } = resolveSessionAgentIds({
190+
config,
191+
agentId: params.agentId,
192+
sessionKey: params.sessionKey,
193+
});
194+
return listAgentEntries(config).find((entry) => normalizeAgentId(entry.id) === sessionAgentId)
195+
?.embeddedHarness;
196+
}
197+
198+
function normalizeAgentHarnessFallback(
199+
value: AgentEmbeddedHarnessConfig["fallback"] | undefined,
200+
): EmbeddedAgentHarnessFallback {
201+
return value === "none" ? "none" : "pi";
202+
}
203+
204+
function formatProviderModel(params: { provider: string; modelId?: string }): string {
205+
return params.modelId ? `${params.provider}/${params.modelId}` : params.provider;
206+
}

‎src/agents/pi-embedded-runner/run/backend.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { resolveEmbeddedAgentRuntime } from "../runtime.js";
2+
import { resolveEmbeddedAgentHarnessFallback, resolveEmbeddedAgentRuntime } from "../runtime.js";
33

44
describe("resolveEmbeddedAgentRuntime", () => {
55
it("uses auto mode by default", () => {
@@ -28,3 +28,20 @@ describe("resolveEmbeddedAgentRuntime", () => {
2828
);
2929
});
3030
});
31+
32+
describe("resolveEmbeddedAgentHarnessFallback", () => {
33+
it("accepts the PI fallback kill switch", () => {
34+
expect(resolveEmbeddedAgentHarnessFallback({ OPENCLAW_AGENT_HARNESS_FALLBACK: "none" })).toBe(
35+
"none",
36+
);
37+
expect(resolveEmbeddedAgentHarnessFallback({ OPENCLAW_AGENT_HARNESS_FALLBACK: "pi" })).toBe(
38+
"pi",
39+
);
40+
});
41+
42+
it("ignores unknown fallback values", () => {
43+
expect(
44+
resolveEmbeddedAgentHarnessFallback({ OPENCLAW_AGENT_HARNESS_FALLBACK: "custom" }),
45+
).toBeUndefined();
46+
});
47+
});

0 commit comments

Comments
 (0)