Skip to content

Commit 56b24ef

Browse files
author
Yamcha
committed
fix(hooks): keep soft block ownership across approval merges
1 parent 605095e commit 56b24ef

3 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎src/plugins/hooks.before-tool-call.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,34 @@ describe("before_tool_call hook merger — requireApproval", () => {
236236
params: { source: "approver", safe: true },
237237
},
238238
},
239+
{
240+
name: "does not let a lower-priority approver convert another plugin's soft block into approval",
241+
hooks: [
242+
{
243+
pluginId: "soft-blocker",
244+
result: {
245+
block: true,
246+
blockMode: "soft",
247+
blockReason: "needs explicit clear",
248+
},
249+
priority: 100,
250+
},
251+
{
252+
pluginId: "approver",
253+
result: {
254+
requireApproval: {
255+
title: "Needs approval",
256+
description: "Approval needed",
257+
},
258+
},
259+
priority: 50,
260+
},
261+
],
262+
expected: {
263+
block: true,
264+
blockReason: "needs explicit clear",
265+
},
266+
},
239267
] as const)("$name", async ({ hooks, expected }) => {
240268
const result = await runBeforeToolCallWithHooks(registry, hooks);
241269
expectRequireApprovalResult(result, expected);

‎src/plugins/hooks.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,20 +713,28 @@ export function createHookRunner(registry: PluginRegistry, options: HookRunnerOp
713713
let block = acc?.block;
714714
let blockMode = acc?.blockMode;
715715
let blockReason = acc?.blockReason;
716+
let blockOwnerPluginId = acc?.blockOwnerPluginId;
716717

717718
if (next.block === true) {
718719
block = true;
719720
blockMode = normalizeBlockMode(next.blockMode);
720721
blockReason = lastDefined(blockReason, next.blockReason);
722+
blockOwnerPluginId = reg.pluginId;
721723
} else if (next.block === false) {
722724
if (block === true && normalizeBlockMode(blockMode) === "soft") {
723725
block = undefined;
724726
blockMode = undefined;
725727
blockReason = undefined;
728+
blockOwnerPluginId = undefined;
726729
}
727730
} else {
728731
blockReason = lastDefined(blockReason, next.blockReason);
729732
}
733+
const softBlockOwnedByAnotherPlugin =
734+
block === true &&
735+
normalizeBlockMode(blockMode) === "soft" &&
736+
Boolean(blockOwnerPluginId) &&
737+
blockOwnerPluginId !== reg.pluginId;
730738

731739
return {
732740
params: freezeParamsForDifferentPlugin
@@ -735,14 +743,21 @@ export function createHookRunner(registry: PluginRegistry, options: HookRunnerOp
735743
block,
736744
blockMode,
737745
blockReason,
746+
blockOwnerPluginId,
738747
requireApproval:
739748
acc?.requireApproval ??
740-
(next.requireApproval
741-
? { ...next.requireApproval, pluginId: reg.pluginId }
742-
: undefined),
749+
(softBlockOwnedByAnotherPlugin
750+
? undefined
751+
: next.requireApproval
752+
? {
753+
...next.requireApproval,
754+
pluginId: reg.pluginId,
755+
}
756+
: undefined),
743757
};
744758
},
745-
shouldStop: (result) => result.block === true && normalizeBlockMode(result.blockMode) === "hard",
759+
shouldStop: (result) =>
760+
result.block === true && normalizeBlockMode(result.blockMode) === "hard",
746761
terminalLabel: "block=true(hard)",
747762
},
748763
);

‎src/plugins/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,8 @@ export type PluginHookBeforeToolCallResult = {
21512151
*/
21522152
blockMode?: "hard" | "soft";
21532153
blockReason?: string;
2154+
/** Internal provenance used by the hook runner — plugins should not set this. */
2155+
blockOwnerPluginId?: string;
21542156
requireApproval?: {
21552157
title: string;
21562158
description: string;

0 commit comments

Comments
 (0)