Skip to content

Commit 08c9ad3

Browse files
fix(agents): preserve prompt-hook failure fallback
1 parent 9686806 commit 08c9ad3

4 files changed

Lines changed: 7 additions & 37 deletions

File tree

‎src/agents/cli-runner/prepare.test.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,7 @@ describe("prepareCliRunContext", () => {
20062006
expect(promptContext?.senderId).toBe("user-789");
20072007
});
20082008

2009-
it("fails closed when prompt-build hooks fail", async () => {
2009+
it("preserves the base prompt when prompt-build hooks fail", async () => {
20102010
const hookRunner = {
20112011
hasHooks: vi.fn((hookName: string) => hookName === "before_prompt_build"),
20122012
runBeforePromptBuild: vi.fn(async () => {
@@ -2015,9 +2015,12 @@ describe("prepareCliRunContext", () => {
20152015
};
20162016
mockGetGlobalHookRunner.mockReturnValue(hookRunner as never);
20172017

2018-
await expect(fixture.prepare({})).rejects.toThrow(
2019-
"CLI before_prompt_build hook failed before tool restrictions could be resolved",
2020-
);
2018+
const context = await fixture.prepare({});
2019+
2020+
expect(context.params.prompt).toBe("latest ask");
2021+
expect(context.systemPrompt).toContain("You are a personal assistant running inside OpenClaw.");
2022+
expect(context.systemPrompt).toContain("Current model identity: test-cli/test-model.");
2023+
expect(context.systemPrompt).not.toContain("hook exploded");
20212024
expect(hookRunner.runBeforePromptBuild).toHaveBeenCalledOnce();
20222025
});
20232026

‎src/agents/cli-runner/prepare.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,6 @@ export async function prepareCliRunContext(
733733
return undefined;
734734
}
735735
const hookRunner = getGlobalHookRunner();
736-
const promptBuildToolPolicyActive = hookRunner?.hasHooks("before_prompt_build") === true;
737736
try {
738737
return await resolvePromptBuildHookResult({
739738
config: params.config ?? getRuntimeConfig(),
@@ -752,15 +751,8 @@ export async function prepareCliRunContext(
752751
},
753752
hookRunner,
754753
bootstrapContextRunKind: params.bootstrapContextRunKind,
755-
failClosedBeforePromptBuild: promptBuildToolPolicyActive,
756754
});
757755
} catch (error) {
758-
if (promptBuildToolPolicyActive) {
759-
throw new Error(
760-
`CLI before_prompt_build hook failed before tool restrictions could be resolved: ${String(error)}`,
761-
{ cause: error },
762-
);
763-
}
764756
cliBackendLog.warn(`cli prompt-build hook preparation failed: ${String(error)}`);
765757
return undefined;
766758
}

‎src/agents/embedded-agent-runner/run/attempt.prompt-helpers.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ export async function resolvePromptBuildHookResult(params: {
102102
hookCtx: PluginHookAgentContext;
103103
hookRunner?: PromptBuildHookRunner | null;
104104
bootstrapContextRunKind?: EmbeddedRunAttemptParams["bootstrapContextRunKind"];
105-
/** Tool-policy owners must not turn a failed restriction hook into unrestricted execution. */
106-
failClosedBeforePromptBuild?: boolean;
107105
}): Promise<PluginHookBeforePromptBuildResult> {
108106
const runId = params.hookCtx.runId;
109107
const cachedInjections = runId ? promptBuildDrainCache.get(runId) : undefined;
@@ -175,9 +173,6 @@ export async function resolvePromptBuildHookResult(params: {
175173
)
176174
.catch((hookErr: unknown) => {
177175
log.warn(`before_prompt_build hook failed: ${String(hookErr)}`);
178-
if (params.failClosedBeforePromptBuild) {
179-
throw hookErr;
180-
}
181176
return undefined;
182177
})
183178
: undefined;

‎src/agents/embedded-agent-runner/run/attempt.test.ts‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,6 @@ describe("resolvePromptBuildHookResult", () => {
164164
expect(result.appendSystemContext).toBe(wrappedPluginSystemContext("prompt append"));
165165
});
166166

167-
it("fails closed on before_prompt_build errors when tool-policy resolution requires it", async () => {
168-
const hookRunner = {
169-
hasHooks: vi.fn((hookName: string) => hookName === "before_prompt_build"),
170-
runBeforePromptBuild: vi.fn(async () => {
171-
throw new Error("policy unavailable");
172-
}),
173-
};
174-
175-
await expect(
176-
resolvePromptBuildHookResult({
177-
config: {},
178-
prompt: "hello",
179-
messages: [],
180-
hookCtx: {},
181-
hookRunner,
182-
failClosedBeforePromptBuild: true,
183-
}),
184-
).rejects.toThrow("policy unavailable");
185-
});
186-
187167
it("applies heartbeat prompt contributions only during heartbeat turns", async () => {
188168
const hookRunner = {
189169
hasHooks: vi.fn((hookName: string) => hookName === "heartbeat_prompt_contribution"),

0 commit comments

Comments
 (0)