fix: formatting
This commit is contained in:
parent
6b25cf53ac
commit
6bcf040dee
6 changed files with 944 additions and 299 deletions
43
core/core.ts
43
core/core.ts
|
|
@ -1007,27 +1007,30 @@ export class Core {
|
|||
this.handleToolCall(toolCall),
|
||||
);
|
||||
|
||||
on("tools/evaluatePolicy", async ({ data: { toolName, basePolicy, args } }) => {
|
||||
const { config } = await this.configHandler.loadConfig();
|
||||
if (!config) {
|
||||
throw new Error("Config not loaded");
|
||||
}
|
||||
|
||||
const tool = config.tools.find((t) => t.function.name === toolName);
|
||||
if (!tool) {
|
||||
// Tool not found, return base policy
|
||||
on(
|
||||
"tools/evaluatePolicy",
|
||||
async ({ data: { toolName, basePolicy, args } }) => {
|
||||
const { config } = await this.configHandler.loadConfig();
|
||||
if (!config) {
|
||||
throw new Error("Config not loaded");
|
||||
}
|
||||
|
||||
const tool = config.tools.find((t) => t.function.name === toolName);
|
||||
if (!tool) {
|
||||
// Tool not found, return base policy
|
||||
return { policy: basePolicy };
|
||||
}
|
||||
|
||||
// If tool has evaluateToolCallPolicy function, use it
|
||||
if (tool.evaluateToolCallPolicy) {
|
||||
const evaluatedPolicy = tool.evaluateToolCallPolicy(basePolicy, args);
|
||||
return { policy: evaluatedPolicy };
|
||||
}
|
||||
|
||||
// Otherwise return base policy unchanged
|
||||
return { policy: basePolicy };
|
||||
}
|
||||
|
||||
// If tool has evaluateToolCallPolicy function, use it
|
||||
if (tool.evaluateToolCallPolicy) {
|
||||
const evaluatedPolicy = tool.evaluateToolCallPolicy(basePolicy, args);
|
||||
return { policy: evaluatedPolicy };
|
||||
}
|
||||
|
||||
// Otherwise return base policy unchanged
|
||||
return { policy: basePolicy };
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
on("isItemTooBig", async ({ data: { item } }) => {
|
||||
return this.isItemTooBig(item);
|
||||
|
|
|
|||
|
|
@ -57,8 +57,14 @@ export const runTerminalCommandTool: Tool = {
|
|||
},
|
||||
},
|
||||
defaultToolPolicy: "allowedWithPermission",
|
||||
evaluateToolCallPolicy: (basePolicy: ToolPolicy, parsedArgs: Record<string, unknown>): ToolPolicy => {
|
||||
return evaluateTerminalCommandSecurity(basePolicy, parsedArgs.command as string);
|
||||
evaluateToolCallPolicy: (
|
||||
basePolicy: ToolPolicy,
|
||||
parsedArgs: Record<string, unknown>,
|
||||
): ToolPolicy => {
|
||||
return evaluateTerminalCommandSecurity(
|
||||
basePolicy,
|
||||
parsedArgs.command as string,
|
||||
);
|
||||
},
|
||||
systemMessageDescription: {
|
||||
prefix: `To run a terminal command, use the ${BuiltInToolNames.RunTerminalCommand} tool
|
||||
|
|
|
|||
|
|
@ -1,4 +1,12 @@
|
|||
import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from "vitest";
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
beforeEach,
|
||||
afterEach,
|
||||
afterAll,
|
||||
vi,
|
||||
} from "vitest";
|
||||
import * as childProcess from "node:child_process";
|
||||
import * as fs from "node:fs";
|
||||
import * as os from "node:os";
|
||||
|
|
@ -475,12 +483,14 @@ describe("runTerminalCommandImpl", () => {
|
|||
});
|
||||
|
||||
describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
||||
|
||||
it("should return base policy for safe commands like echo", () => {
|
||||
const basePolicy = "allowedWithoutPermission";
|
||||
const args = { command: "echo hello world" };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("allowedWithoutPermission");
|
||||
});
|
||||
|
|
@ -489,7 +499,10 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
|||
const basePolicy = "disabled";
|
||||
const args = { command: "ls -la" };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("disabled");
|
||||
});
|
||||
|
|
@ -498,7 +511,10 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
|||
const basePolicy = "disabled";
|
||||
const args = { command: "echo test" };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("disabled");
|
||||
});
|
||||
|
|
@ -507,7 +523,10 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
|||
const basePolicy = "allowedWithoutPermission";
|
||||
const args = { command: "ls -la" };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("allowedWithoutPermission");
|
||||
});
|
||||
|
|
@ -516,7 +535,10 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
|||
const basePolicy = "allowedWithPermission";
|
||||
const args = { command: "git status" };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("allowedWithPermission");
|
||||
});
|
||||
|
|
@ -525,7 +547,10 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
|||
const basePolicy = "allowedWithoutPermission";
|
||||
const args = { command: undefined };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("allowedWithoutPermission");
|
||||
});
|
||||
|
|
@ -534,7 +559,10 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
|||
const basePolicy = "allowedWithoutPermission";
|
||||
const args = { command: null };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("allowedWithoutPermission");
|
||||
});
|
||||
|
|
@ -543,7 +571,10 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
|||
const basePolicy = "allowedWithoutPermission";
|
||||
const args = { command: "" };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("allowedWithoutPermission");
|
||||
});
|
||||
|
|
@ -552,7 +583,10 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
|||
const basePolicy = "allowedWithoutPermission";
|
||||
const args = { command: "rm -rf /" };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("disabled");
|
||||
});
|
||||
|
|
@ -561,7 +595,10 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
|
|||
const basePolicy = "allowedWithoutPermission";
|
||||
const args = { command: "curl http://example.com" };
|
||||
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
|
||||
const result = runTerminalCommandTool.evaluateToolCallPolicy!(
|
||||
basePolicy,
|
||||
args,
|
||||
);
|
||||
|
||||
expect(result).toBe("allowedWithPermission");
|
||||
});
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -339,10 +339,7 @@ function evaluateSingleCommand(
|
|||
/**
|
||||
* Checks if a command is critical and should always be disabled
|
||||
*/
|
||||
function isCriticalCommand(
|
||||
baseCommand: string,
|
||||
args: string[],
|
||||
): boolean {
|
||||
function isCriticalCommand(baseCommand: string, args: string[]): boolean {
|
||||
// System destruction commands - check if base command starts with mkfs
|
||||
if (baseCommand.startsWith("mkfs")) {
|
||||
return true;
|
||||
|
|
@ -505,7 +502,10 @@ function isCriticalCommand(
|
|||
/**
|
||||
* Checks if command is a package manager install
|
||||
*/
|
||||
function isHighRiskPackageManager(baseCommand: string, args: string[]): boolean {
|
||||
function isHighRiskPackageManager(
|
||||
baseCommand: string,
|
||||
args: string[],
|
||||
): boolean {
|
||||
const packageManagers = [
|
||||
"npm",
|
||||
"yarn",
|
||||
|
|
@ -562,7 +562,10 @@ function isHighRiskNetworkTool(baseCommand: string): boolean {
|
|||
/**
|
||||
* Checks if command is a script interpreter
|
||||
*/
|
||||
function isHighRiskScriptInterpreter(baseCommand: string, args: string[]): boolean {
|
||||
function isHighRiskScriptInterpreter(
|
||||
baseCommand: string,
|
||||
args: string[],
|
||||
): boolean {
|
||||
const scriptInterpreters = [
|
||||
"sh",
|
||||
"bash",
|
||||
|
|
@ -628,7 +631,10 @@ function isHighRiskDirectScript(baseCommand: string): boolean {
|
|||
/**
|
||||
* Checks if command modifies environment variables
|
||||
*/
|
||||
function isHighRiskEnvironmentModifier(baseCommand: string, args: string[]): boolean {
|
||||
function isHighRiskEnvironmentModifier(
|
||||
baseCommand: string,
|
||||
args: string[],
|
||||
): boolean {
|
||||
if (
|
||||
baseCommand === "export" ||
|
||||
baseCommand === "setx" ||
|
||||
|
|
@ -714,7 +720,10 @@ function isHighRiskRmCommand(baseCommand: string, args: string[]): boolean {
|
|||
/**
|
||||
* Checks if command is archive extraction to system directories
|
||||
*/
|
||||
function isHighRiskArchiveExtraction(baseCommand: string, args: string[]): boolean {
|
||||
function isHighRiskArchiveExtraction(
|
||||
baseCommand: string,
|
||||
args: string[],
|
||||
): boolean {
|
||||
if (
|
||||
baseCommand === "tar" ||
|
||||
baseCommand === "unzip" ||
|
||||
|
|
@ -806,9 +815,7 @@ function isHighRiskWindowsRegistry(baseCommand: string): boolean {
|
|||
*/
|
||||
function isHighRiskWindowsManagement(baseCommand: string): boolean {
|
||||
return (
|
||||
baseCommand === "wmic" ||
|
||||
baseCommand === "net" ||
|
||||
baseCommand === "netsh"
|
||||
baseCommand === "wmic" || baseCommand === "net" || baseCommand === "netsh"
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -829,7 +836,10 @@ function isHighRiskSourceCommand(baseCommand: string): boolean {
|
|||
/**
|
||||
* Checks if command manipulates history
|
||||
*/
|
||||
function isHighRiskHistoryManipulation(baseCommand: string, args: string[]): boolean {
|
||||
function isHighRiskHistoryManipulation(
|
||||
baseCommand: string,
|
||||
args: string[],
|
||||
): boolean {
|
||||
if (baseCommand === "history" && args.includes("-c")) {
|
||||
return true;
|
||||
}
|
||||
|
|
@ -881,7 +891,8 @@ function isHighRiskCommand(
|
|||
if (isHighRiskDirectScript(baseCommand)) return true;
|
||||
if (isHighRiskEnvironmentModifier(baseCommand, args)) return true;
|
||||
if (baseCommand === "alias") return true;
|
||||
if (isFunctionDefinition([baseCommand, ...args], originalCommand)) return true;
|
||||
if (isFunctionDefinition([baseCommand, ...args], originalCommand))
|
||||
return true;
|
||||
if (isHighRiskProcessCommand(baseCommand)) return true;
|
||||
if (isHighRiskSystemService(baseCommand)) return true;
|
||||
if (isHighRiskFileOperation(baseCommand, args)) return true;
|
||||
|
|
@ -905,10 +916,7 @@ function isHighRiskCommand(
|
|||
/**
|
||||
* Checks if a command is safe and can be auto-approved
|
||||
*/
|
||||
function isSafeCommand(
|
||||
baseCommand: string,
|
||||
args: string[],
|
||||
): boolean {
|
||||
function isSafeCommand(baseCommand: string, args: string[]): boolean {
|
||||
// Information commands
|
||||
const infoCommands = [
|
||||
"ls",
|
||||
|
|
|
|||
|
|
@ -578,16 +578,16 @@ describe("streamResponseThunk - tool calls", () => {
|
|||
|
||||
// Verify final state after tool call execution
|
||||
const finalState = mockStoreWithToolSettings.getState();
|
||||
|
||||
|
||||
// Check that the tool was executed (status should be 'done' if auto-approved and executed)
|
||||
const toolCallState = finalState.session.history.find(
|
||||
item => item.toolCallStates && item.toolCallStates.length > 0
|
||||
(item) => item.toolCallStates && item.toolCallStates.length > 0,
|
||||
)?.toolCallStates?.[0];
|
||||
|
||||
|
||||
// With proper mocking, the tool should be auto-executed
|
||||
expect(toolCallState?.status).toBe("done");
|
||||
expect(toolCallState?.output).toBeDefined();
|
||||
|
||||
|
||||
expect(finalState).toEqual({
|
||||
session: {
|
||||
history: [
|
||||
|
|
@ -3054,35 +3054,39 @@ describe("streamResponseThunk - tool calls", () => {
|
|||
const mockIdeMessenger = mockStore.mockIdeMessenger;
|
||||
|
||||
// Simple mock - just return a different policy for testing
|
||||
mockIdeMessenger.request.mockImplementation((endpoint: string, data: any) => {
|
||||
if (endpoint === "tools/evaluatePolicy") {
|
||||
// For this test, simulate that echo commands require permission
|
||||
if (data.args.command?.toLowerCase().startsWith("echo")) {
|
||||
mockIdeMessenger.request.mockImplementation(
|
||||
(endpoint: string, data: any) => {
|
||||
if (endpoint === "tools/evaluatePolicy") {
|
||||
// For this test, simulate that echo commands require permission
|
||||
if (data.args.command?.toLowerCase().startsWith("echo")) {
|
||||
return Promise.resolve({
|
||||
status: "success",
|
||||
content: { policy: "allowedWithPermission" },
|
||||
});
|
||||
}
|
||||
return Promise.resolve({
|
||||
status: "success",
|
||||
content: { policy: "allowedWithPermission" },
|
||||
content: { policy: data.basePolicy },
|
||||
});
|
||||
} else if (endpoint === "llm/compileChat") {
|
||||
return Promise.resolve({
|
||||
status: "success",
|
||||
content: {
|
||||
compiledChatMessages: [
|
||||
{ role: "user", content: "Run echo hello" },
|
||||
],
|
||||
didPrune: false,
|
||||
contextPercentage: 0.5,
|
||||
},
|
||||
});
|
||||
} else if (endpoint === "history/save") {
|
||||
return Promise.resolve({ status: "success" });
|
||||
} else if (endpoint === "history/list") {
|
||||
return Promise.resolve({ status: "success", content: [] });
|
||||
}
|
||||
return Promise.resolve({
|
||||
status: "success",
|
||||
content: { policy: data.basePolicy },
|
||||
});
|
||||
} else if (endpoint === "llm/compileChat") {
|
||||
return Promise.resolve({
|
||||
status: "success",
|
||||
content: {
|
||||
compiledChatMessages: [{ role: "user", content: "Run echo hello" }],
|
||||
didPrune: false,
|
||||
contextPercentage: 0.5,
|
||||
},
|
||||
});
|
||||
} else if (endpoint === "history/save") {
|
||||
return Promise.resolve({ status: "success" });
|
||||
} else if (endpoint === "history/list") {
|
||||
return Promise.resolve({ status: "success", content: [] });
|
||||
}
|
||||
return Promise.resolve({ status: "success", content: {} });
|
||||
});
|
||||
return Promise.resolve({ status: "success", content: {} });
|
||||
},
|
||||
);
|
||||
|
||||
// Setup streaming with echo command (should require permission despite auto-approve base)
|
||||
async function* mockStreamWithEcho() {
|
||||
|
|
@ -3202,29 +3206,31 @@ describe("streamResponseThunk - tool calls", () => {
|
|||
const mockIdeMessenger = mockStore.mockIdeMessenger;
|
||||
|
||||
// Simple mock - just return disabled policy
|
||||
mockIdeMessenger.request.mockImplementation((endpoint: string, data: any) => {
|
||||
if (endpoint === "tools/evaluatePolicy") {
|
||||
// Backend should respect disabled state
|
||||
return Promise.resolve({
|
||||
status: "success",
|
||||
content: { policy: "disabled" },
|
||||
});
|
||||
} else if (endpoint === "llm/compileChat") {
|
||||
return Promise.resolve({
|
||||
status: "success",
|
||||
content: {
|
||||
compiledChatMessages: [{ role: "user", content: "Run ls" }],
|
||||
didPrune: false,
|
||||
contextPercentage: 0.5,
|
||||
},
|
||||
});
|
||||
} else if (endpoint === "history/save") {
|
||||
return Promise.resolve({ status: "success" });
|
||||
} else if (endpoint === "history/list") {
|
||||
return Promise.resolve({ status: "success", content: [] });
|
||||
}
|
||||
return Promise.resolve({ status: "success", content: {} });
|
||||
});
|
||||
mockIdeMessenger.request.mockImplementation(
|
||||
(endpoint: string, data: any) => {
|
||||
if (endpoint === "tools/evaluatePolicy") {
|
||||
// Backend should respect disabled state
|
||||
return Promise.resolve({
|
||||
status: "success",
|
||||
content: { policy: "disabled" },
|
||||
});
|
||||
} else if (endpoint === "llm/compileChat") {
|
||||
return Promise.resolve({
|
||||
status: "success",
|
||||
content: {
|
||||
compiledChatMessages: [{ role: "user", content: "Run ls" }],
|
||||
didPrune: false,
|
||||
contextPercentage: 0.5,
|
||||
},
|
||||
});
|
||||
} else if (endpoint === "history/save") {
|
||||
return Promise.resolve({ status: "success" });
|
||||
} else if (endpoint === "history/list") {
|
||||
return Promise.resolve({ status: "success", content: [] });
|
||||
}
|
||||
return Promise.resolve({ status: "success", content: {} });
|
||||
},
|
||||
);
|
||||
|
||||
// Setup streaming with regular command
|
||||
async function* mockStreamWithLs() {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue