legacy-permission-adapter.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import type { ToolCheckResult } from './engine.js';
  2. import type { PermissionDecision } from './types.js';
  3. import {
  4. reducePermissionEvaluation,
  5. type CommandIntent,
  6. type ExecutionPlan,
  7. type PermissionEvidence,
  8. type PermissionEvaluationInput,
  9. type ShellFamily,
  10. } from './permission-core.js';
  11. import { createPermissionExecutionPlan, inferPermissionCommandIntents } from './execution-plan.js';
  12. export type LegacyParserAdapterResult = {
  13. intents: CommandIntent[];
  14. evidence: PermissionEvidence[];
  15. legacyDecision: PermissionDecision;
  16. executionPlan?: ExecutionPlan;
  17. };
  18. export type LegacyAdapterInput = Pick<PermissionEvaluationInput, 'toolName' | 'input'> & {
  19. decision: PermissionDecision;
  20. checkerResult?: ToolCheckResult;
  21. shell?: ShellFamily;
  22. context?: Pick<PermissionEvaluationInput['context'], 'workingDirectory' | 'homeDir'>;
  23. };
  24. export type LegacyEvaluationInput = LegacyAdapterInput &
  25. Pick<PermissionEvaluationInput, 'mode' | 'context'>;
  26. /**
  27. * Shared v1/v2 parity seam. Hosts still own their existing approval and
  28. * execution flow; both can opt into this function without changing the
  29. * legacy checker or moving side effects into Permission Core.
  30. */
  31. export function evaluateLegacyPermission(input: LegacyEvaluationInput) {
  32. const parsed = adaptLegacyPermissionResult(input);
  33. return {
  34. parsed,
  35. evaluation: reducePermissionEvaluation({
  36. ...input,
  37. evidence: parsed.evidence,
  38. executionPlan: parsed.executionPlan,
  39. }),
  40. };
  41. }
  42. export function adaptLegacyPermissionResult(input: LegacyAdapterInput): LegacyParserAdapterResult {
  43. const evidence: PermissionEvidence[] = [decisionEvidence(input.decision)];
  44. const intents = inferPermissionCommandIntents(input);
  45. const executionPlan = buildExecutionPlan(input);
  46. if (input.decision.reason.type === 'safetyCheck') {
  47. evidence.push(
  48. input.decision.reason.classifierApprovable === false
  49. ? { kind: 'classifier-ineligible', reason: input.decision.reason.description }
  50. : { kind: 'classifier-eligible', reason: input.decision.reason.description },
  51. );
  52. }
  53. if (input.decision.reason.type === 'rmRewrite') {
  54. evidence.push({ kind: 'legacy-reason', value: input.decision.reason });
  55. }
  56. return {
  57. intents,
  58. evidence,
  59. legacyDecision: input.decision,
  60. ...(executionPlan ? { executionPlan } : {}),
  61. };
  62. }
  63. function decisionEvidence(decision: PermissionDecision): PermissionEvidence {
  64. if (decision.behavior === 'deny') {
  65. if (decision.reason.type === 'safetyCheck')
  66. return { kind: 'host-deny', reason: decision.reason.description };
  67. return { kind: 'legacy-deny', reason: describeLegacyDeny(decision.reason) };
  68. }
  69. if (decision.behavior === 'allow') {
  70. if (decision.reason.type === 'rule') {
  71. return {
  72. kind: 'user-rule',
  73. behavior: 'allow',
  74. ruleContent: decision.reason.rule.ruleValue.ruleContent,
  75. };
  76. }
  77. return { kind: 'built-in-allow', reason: decision.reason.type };
  78. }
  79. return {
  80. kind: 'user-rule',
  81. behavior: 'ask',
  82. ruleContent: decision.ruleContents?.[0],
  83. };
  84. }
  85. function describeLegacyDeny(reason: PermissionDecision['reason']): string {
  86. if (reason.type === 'safetyCheck') return reason.description;
  87. if (reason.type === 'rule') return `Matched ${reason.rule.source} deny rule.`;
  88. return `Legacy permission decision ${reason.type} denied the tool call.`;
  89. }
  90. function buildExecutionPlan(input: LegacyAdapterInput): ExecutionPlan | undefined {
  91. const rewrittenInput = input.checkerResult?.rewrittenInput ?? input.decision.rewrittenInput;
  92. if (!rewrittenInput) return undefined;
  93. return createPermissionExecutionPlan({
  94. toolName: input.toolName,
  95. input: input.input,
  96. rewrittenInput,
  97. ...(input.shell ? { shell: input.shell } : {}),
  98. ...(input.context ? { context: input.context } : {}),
  99. }) as ExecutionPlan;
  100. }