Skip to content

Commit 908fc35

Browse files
committed
fix: preserve trusted policy checks for skill workshop
1 parent d6d1cc2 commit 908fc35

4 files changed

Lines changed: 98 additions & 18 deletions

File tree

‎src/agents/agent-tools.before-tool-call.embedded-mode.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,52 @@ describe("runBeforeToolCallHook — embedded mode approvals", () => {
355355
expect(runBeforeToolCallMock).not.toHaveBeenCalled();
356356
});
357357

358+
it("runs trusted policies before skill_workshop lifecycle approval", async () => {
359+
const registry = createEmptyPluginRegistry();
360+
registry.trustedToolPolicies = [
361+
{
362+
pluginId: "trusted-policy",
363+
pluginName: "Trusted Policy",
364+
source: "test",
365+
policy: {
366+
id: "block-skill-workshop",
367+
description: "Block skill workshop lifecycle",
368+
evaluate: () => ({
369+
block: true,
370+
blockReason: "trusted policy blocked skill workshop",
371+
}),
372+
},
373+
},
374+
];
375+
setActivePluginRegistry(registry);
376+
(hookRunner.hasHooks as ReturnType<typeof vi.fn>).mockReturnValue(false);
377+
378+
const result = await runBeforeToolCallHook({
379+
toolName: "skill_workshop",
380+
params: { action: "apply", proposal_id: "weather-20260530-a1b2c3d4e5" },
381+
toolCallId: "call-skill-apply",
382+
ctx: {
383+
config: {
384+
skills: {
385+
workshop: {
386+
approvalPolicy: "pending",
387+
},
388+
},
389+
},
390+
},
391+
});
392+
393+
expect(result).toEqual({
394+
blocked: true,
395+
kind: "veto",
396+
deniedReason: "plugin-before-tool-call",
397+
reason: "trusted policy blocked skill workshop",
398+
params: { action: "apply", proposal_id: "weather-20260530-a1b2c3d4e5" },
399+
});
400+
expect(mockCallGatewayTool).not.toHaveBeenCalled();
401+
expect(runBeforeToolCallMock).not.toHaveBeenCalled();
402+
});
403+
358404
it("does not require skill_workshop lifecycle approval in auto mode", async () => {
359405
(hookRunner.hasHooks as ReturnType<typeof vi.fn>).mockReturnValue(false);
360406

‎src/agents/agent-tools.before-tool-call.ts‎

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -839,12 +839,12 @@ export async function runBeforeToolCallHook(args: {
839839
const hasBeforeToolCallHooks = hookRunner?.hasHooks("before_tool_call") === true;
840840
const shouldRunTrustedPolicies = hasTrustedToolPolicies();
841841
const normalizedParams = isPlainObject(params) ? params : {};
842-
const corePolicyResult = resolveSkillWorkshopToolApproval({
842+
const initialCorePolicyResult = resolveSkillWorkshopToolApproval({
843843
toolName,
844844
toolParams: normalizedParams,
845845
...(args.ctx?.config ? { config: args.ctx.config } : {}),
846846
});
847-
if (!corePolicyResult && !shouldRunTrustedPolicies && !hasBeforeToolCallHooks) {
847+
if (!initialCorePolicyResult && !shouldRunTrustedPolicies && !hasBeforeToolCallHooks) {
848848
return { blocked: false, params };
849849
}
850850
const deriveOptions =
@@ -875,18 +875,6 @@ export async function runBeforeToolCallHook(args: {
875875
...(args.ctx?.channelId && { channelId: args.ctx.channelId }),
876876
});
877877
const toolContext = buildToolContext(toolIdentity);
878-
const coreApprovalOutcome = await resolveBeforeToolCallApprovalOutcome({
879-
result: corePolicyResult,
880-
approvalMode: args.approvalMode,
881-
toolName,
882-
...(args.toolCallId ? { toolCallId: args.toolCallId } : {}),
883-
...(args.ctx ? { ctx: args.ctx } : {}),
884-
signal: args.signal,
885-
baseParams: params,
886-
});
887-
if (coreApprovalOutcome) {
888-
return coreApprovalOutcome;
889-
}
890878
const trustedPolicyResult = shouldRunTrustedPolicies
891879
? await runTrustedToolPolicies(
892880
{
@@ -934,6 +922,8 @@ export async function runBeforeToolCallHook(args: {
934922
params,
935923
};
936924
}
925+
let trustedApprovalParams: unknown | undefined;
926+
let trustedApprovalResolution: PluginApprovalResolution | undefined;
937927
if (trustedPolicyResult?.requireApproval) {
938928
const approvalOutcome = await resolveBeforeToolCallApprovalOutcome({
939929
result: trustedPolicyResult,
@@ -945,10 +935,17 @@ export async function runBeforeToolCallHook(args: {
945935
baseParams: params,
946936
});
947937
if (approvalOutcome) {
948-
return approvalOutcome;
938+
if (approvalOutcome.blocked) {
939+
return approvalOutcome;
940+
}
941+
if (approvalOutcome.deferredApproval) {
942+
return approvalOutcome;
943+
}
944+
trustedApprovalParams = approvalOutcome.params;
945+
trustedApprovalResolution = approvalOutcome.approvalResolution;
949946
}
950947
}
951-
const rawPolicyAdjustedParams = trustedPolicyResult?.params ?? params;
948+
const rawPolicyAdjustedParams = trustedApprovalParams ?? trustedPolicyResult?.params ?? params;
952949
const policyAdjustedParams = normalizeCodeModeExecBeforeHookParamsForToolKind({
953950
toolKind: args.toolKind,
954951
params: rawPolicyAdjustedParams,
@@ -963,8 +960,29 @@ export async function runBeforeToolCallHook(args: {
963960
trustedPolicyResult?.params && isPlainObject(policyAdjustedParams)
964961
? deriveToolParams(toolName, policyAdjustedParams, deriveOptions)
965962
: derivedToolParams;
963+
const corePolicyResult = resolveSkillWorkshopToolApproval({
964+
toolName,
965+
toolParams: isPlainObject(policyAdjustedParams) ? policyAdjustedParams : {},
966+
...(args.ctx?.config ? { config: args.ctx.config } : {}),
967+
});
968+
const coreApprovalOutcome = await resolveBeforeToolCallApprovalOutcome({
969+
result: corePolicyResult,
970+
approvalMode: args.approvalMode,
971+
toolName,
972+
...(args.toolCallId ? { toolCallId: args.toolCallId } : {}),
973+
...(args.ctx ? { ctx: args.ctx } : {}),
974+
signal: args.signal,
975+
baseParams: policyAdjustedParams,
976+
});
977+
if (coreApprovalOutcome) {
978+
return coreApprovalOutcome;
979+
}
966980
if (!hasBeforeToolCallHooks) {
967-
return { blocked: false, params: policyAdjustedParams };
981+
return {
982+
blocked: false,
983+
params: policyAdjustedParams,
984+
...(trustedApprovalResolution ? { approvalResolution: trustedApprovalResolution } : {}),
985+
};
968986
}
969987
const hookEventParams = isPlainObject(policyAdjustedParams) ? policyAdjustedParams : {};
970988
const hookResult = await hookRunner.runBeforeToolCall(

‎src/skills/workshop/service.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ describe("skill workshop proposals", () => {
179179
await expect(fs.readFile(skillFile, "utf8")).resolves.toContain("user-invocable: false");
180180
});
181181

182+
it("rejects create proposals when the target skill file already exists", async () => {
183+
const workspaceDir = await makeWorkspace();
184+
const skillFile = path.join(workspaceDir, "skills", "empty-skill", "SKILL.md");
185+
await fs.mkdir(path.dirname(skillFile), { recursive: true });
186+
await fs.writeFile(skillFile, "", "utf8");
187+
188+
await expect(
189+
proposeCreateSkill({
190+
workspaceDir,
191+
name: "Empty Skill",
192+
description: "Existing empty skill file",
193+
content: "# Empty Skill\n",
194+
}),
195+
).rejects.toThrow("Skill already exists");
196+
});
197+
182198
it("revises pending proposals in place before approval", async () => {
183199
const workspaceDir = await makeWorkspace();
184200
const proposal = await proposeCreateSkill({

‎src/skills/workshop/service.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export async function proposeCreateSkill(
227227
assertProposalDescriptionWithinLimit(description);
228228
assertProposalContentWithinLimit(input.content, config.maxSkillBytes);
229229
const target = resolveSkillProposalTarget({ workspaceDir: input.workspaceDir, skillName: name });
230-
if (await readWorkspaceSkillFile(target.skillFile)) {
230+
if ((await readWorkspaceSkillFile(target.skillFile)) !== null) {
231231
throw new Error(`Skill already exists at ${target.skillFile}.`);
232232
}
233233

0 commit comments

Comments
 (0)