tool-guard.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import type { PiBeforeToolCallHook } from '@mavis/agent-core/pi-turn-runner';
  2. import type { ReviewTurnState } from './turn-state.js';
  3. // Bash retains ordinary permission checks; Review adds no argument policy.
  4. const REVIEW_ALLOWED_TOOLS = new Set(['read', 'grep', 'glob', 'skill', 'bash']);
  5. // Native controls enforce session ownership in the task service. Review must
  6. // retain them when an allowed Bash call runs in the background.
  7. const REVIEW_TASK_CONTROLS = new Set(['task_query', 'task_output', 'task_stop']);
  8. export function createReviewToolGuard(state: ReviewTurnState): {
  9. beforeToolCall: PiBeforeToolCallHook;
  10. } {
  11. return {
  12. beforeToolCall(toolContext) {
  13. if (!state.isReviewActivated()) return undefined;
  14. const toolName = toolContext.toolCall.name;
  15. if (toolName === 'code_review') {
  16. return {
  17. block: true,
  18. reason: buildRepeatedCodeReviewReason(state.prepared),
  19. };
  20. }
  21. if (toolName === 'skill' && readSkillName(toolContext.args) === 'code-review') {
  22. return {
  23. block: true,
  24. reason: buildRepeatedCodeReviewSkillReason(state.prepared),
  25. };
  26. }
  27. const source = readToolSource(toolContext.toolCall);
  28. if (source !== 'configured' && REVIEW_ALLOWED_TOOLS.has(toolName)) return undefined;
  29. if ((source === undefined || source === 'builtin') && REVIEW_TASK_CONTROLS.has(toolName)) {
  30. return undefined;
  31. }
  32. return {
  33. block: true,
  34. reason: `Code Review tool policy denied "${toolName}".`,
  35. };
  36. },
  37. };
  38. }
  39. function readSkillName(input: unknown): string | undefined {
  40. if (!input || typeof input !== 'object') return undefined;
  41. const name = (input as { name?: unknown }).name;
  42. return typeof name === 'string' ? name.trim().toLowerCase() : undefined;
  43. }
  44. function buildRepeatedCodeReviewSkillReason(prepared: ReviewTurnState['prepared']): string {
  45. if (prepared?.responseLanguage === 'zh-CN') {
  46. return '当前结构化 Review 已经激活,不要再读取 code-review Skill;请使用允许的只读工具继续审查。';
  47. }
  48. return 'Structured Review is already active. Do not load the code-review Skill; continue with the allowed read-only tools.';
  49. }
  50. function buildRepeatedCodeReviewReason(prepared: ReviewTurnState['prepared']): string {
  51. const isSlash = prepared?.trigger === 'slash';
  52. if (prepared?.responseLanguage === 'zh-CN') {
  53. return isSlash
  54. ? '当前 Review 已由 Slash 请求激活,不能再次调用 code_review。请直接使用允许的只读工具检查当前改动,并返回 Review 结果。'
  55. : '当前 Turn 的 Review 已经激活,不能再次调用 code_review。请直接使用允许的只读工具继续检查,并返回 Review 结果。';
  56. }
  57. return isSlash
  58. ? 'Code Review is already active for this Slash request. Do not call code_review again. Inspect the current changes with the allowed read-only tools and return the Review result directly.'
  59. : 'Code Review is already active for this turn. Do not call code_review again. Continue with the allowed read-only tools and return the Review result directly.';
  60. }
  61. function readToolSource(toolCall: unknown): unknown {
  62. if (!toolCall || typeof toolCall !== 'object') return undefined;
  63. return (toolCall as { source?: unknown }).source;
  64. }