zcodeProtocolV4Permission.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { describe, expect, it } from "vitest";
  2. import { z } from "zod";
  3. import { zcodePermissionRequestedEventPayloadSchema } from "../src/zcode-protocol/index.js";
  4. import {
  5. pendingInteractionSchema,
  6. permissionRequestPayloadSchema,
  7. } from "../src/zcode-protocol-v4/index.js";
  8. describe("v4 permission payload compatibility", () => {
  9. const preCapabilityGroupStrictRuleSchema = z
  10. .object({
  11. toolName: z.string().min(1),
  12. ruleContent: z.string().optional(),
  13. })
  14. .strict();
  15. const preCapabilityGroupStrictResponseSchema = z
  16. .object({
  17. decision: z.enum(["allow", "deny", "escalate", "modify"]),
  18. reason: z.string().optional(),
  19. permissionUpdates: z
  20. .array(
  21. z
  22. .object({
  23. type: z.literal("addRules"),
  24. behavior: z.enum(["allow", "deny", "ask"]),
  25. rules: z.array(preCapabilityGroupStrictRuleSchema).min(1),
  26. })
  27. .strict(),
  28. )
  29. .optional(),
  30. })
  31. .strict();
  32. it("parses legacy options without an embedded response", () => {
  33. expect(
  34. permissionRequestPayloadSchema.parse({
  35. kind: "permission",
  36. toolCallId: "tc-legacy",
  37. toolName: "Bash",
  38. summary: "Run command",
  39. detail: { command: "pnpm run lint --fix" },
  40. options: [
  41. { optionId: "allowOnce", label: "Allow once", kind: "allowOnce" },
  42. { optionId: "deny", label: "Deny", kind: "deny" },
  43. ],
  44. }).options[0]?.response,
  45. ).toBeUndefined();
  46. });
  47. it("accepts the additive freeText capability in missing, false and true forms", () => {
  48. const base = {
  49. kind: "permission" as const,
  50. toolCallId: "tc-capability",
  51. toolName: "Bash",
  52. summary: "Run command",
  53. detail: { command: "ls" },
  54. options: [{ optionId: "deny", label: "Deny", kind: "deny" }],
  55. };
  56. expect(permissionRequestPayloadSchema.parse(base).freeText).toBeUndefined();
  57. expect(permissionRequestPayloadSchema.parse({ ...base, freeText: false }).freeText).toBe(false);
  58. expect(permissionRequestPayloadSchema.parse({ ...base, freeText: true }).freeText).toBe(true);
  59. });
  60. it("preserves a project permission response with prefix suggestions", () => {
  61. const response = {
  62. decision: "allow" as const,
  63. permissionUpdates: [
  64. {
  65. behavior: "allow" as const,
  66. rules: [{ ruleContent: "pnpm run lint:*", toolName: "Bash" }],
  67. type: "addRules" as const,
  68. },
  69. ],
  70. };
  71. const parsed = permissionRequestPayloadSchema.parse({
  72. kind: "permission",
  73. toolCallId: "tc-prefix",
  74. toolName: "Bash",
  75. summary: "Run command",
  76. detail: { command: "pnpm run lint --fix" },
  77. options: [
  78. {
  79. optionId: "allowAlways",
  80. label: "Always allow",
  81. kind: "allowAlways",
  82. response,
  83. },
  84. ],
  85. });
  86. expect(parsed.options[0]?.response).toEqual(response);
  87. });
  88. it("round-trips the trusted official CUA project rule in the legacy wire shape", () => {
  89. const response = {
  90. decision: "allow" as const,
  91. permissionUpdates: [
  92. {
  93. behavior: "allow" as const,
  94. rules: [{ toolName: "zcode:permission-capability:official_cua" }],
  95. type: "addRules" as const,
  96. },
  97. ],
  98. };
  99. const parsed = permissionRequestPayloadSchema.parse({
  100. detail: {},
  101. kind: "permission",
  102. options: [
  103. {
  104. kind: "allowAlways",
  105. label: "Always allow Computer Use in this project",
  106. optionId: "allowAlways",
  107. response,
  108. },
  109. ],
  110. summary: "Use Computer Use",
  111. toolCallId: "tc-official-cua",
  112. toolName: "mcp__computer-use__get_app_state",
  113. });
  114. expect(parsed.options[0]?.response).toEqual(response);
  115. expect(
  116. preCapabilityGroupStrictResponseSchema.safeParse(parsed.options[0]?.response).success,
  117. ).toBe(true);
  118. });
  119. it("keeps a pending official CUA permission snapshot readable by the pre-MR schema", () => {
  120. const pending = pendingInteractionSchema.parse({
  121. interactionId: "permission-official-cua",
  122. kind: "permission",
  123. anchorRowId: 7,
  124. createdAt: 1_785_848_000_000,
  125. payload: {
  126. detail: {},
  127. kind: "permission",
  128. options: [
  129. {
  130. kind: "allowAlways",
  131. label: "Always allow Computer Use in this project",
  132. optionId: "allowAlways",
  133. response: {
  134. decision: "allow",
  135. permissionUpdates: [
  136. {
  137. behavior: "allow",
  138. rules: [{ toolName: "zcode:permission-capability:official_cua" }],
  139. type: "addRules",
  140. },
  141. ],
  142. },
  143. },
  144. {
  145. kind: "deny",
  146. label: "Deny",
  147. optionId: "deny",
  148. response: { decision: "deny" },
  149. },
  150. ],
  151. summary: "Use Computer Use",
  152. toolCallId: "tc-official-cua",
  153. toolName: "mcp__computer-use__left_click",
  154. },
  155. });
  156. expect(
  157. preCapabilityGroupStrictResponseSchema.safeParse(
  158. pending.payload.kind === "permission" ? pending.payload.options[0]?.response : undefined,
  159. ).success,
  160. ).toBe(true);
  161. expect(
  162. preCapabilityGroupStrictResponseSchema.safeParse(
  163. pending.payload.kind === "permission" ? pending.payload.options[1]?.response : undefined,
  164. ).success,
  165. ).toBe(true);
  166. });
  167. it("keeps the legacy permission.requested event inside the pre-MR strict rule shape", () => {
  168. const event = zcodePermissionRequestedEventPayloadSchema.parse({
  169. input: {},
  170. options: [
  171. {
  172. kind: "allow_always",
  173. name: "Always allow Computer Use in this project",
  174. optionId: "allow_project",
  175. response: {
  176. decision: "allow",
  177. permissionUpdates: [
  178. {
  179. behavior: "allow",
  180. rules: [{ toolName: "zcode:permission-capability:official_cua" }],
  181. type: "addRules",
  182. },
  183. ],
  184. },
  185. },
  186. ],
  187. reason: "Use Computer Use",
  188. riskLevel: "medium",
  189. suggestedPermissionUpdates: [
  190. {
  191. behavior: "allow",
  192. rules: [{ toolName: "zcode:permission-capability:official_cua" }],
  193. type: "addRules",
  194. },
  195. ],
  196. toolCallId: "tc-official-cua",
  197. toolName: "mcp__computer-use__get_app_state",
  198. });
  199. expect(
  200. preCapabilityGroupStrictRuleSchema.safeParse(event.suggestedPermissionUpdates?.[0]?.rules[0])
  201. .success,
  202. ).toBe(true);
  203. expect(
  204. preCapabilityGroupStrictResponseSchema.safeParse(event.options[0]?.response).success,
  205. ).toBe(true);
  206. });
  207. it("rejects an added capabilityGroup field so wire compatibility cannot regress", () => {
  208. const parsed = permissionRequestPayloadSchema.safeParse({
  209. detail: {},
  210. kind: "permission",
  211. options: [
  212. {
  213. kind: "allowAlways",
  214. label: "Always allow",
  215. optionId: "allowAlways",
  216. response: {
  217. decision: "allow",
  218. permissionUpdates: [
  219. {
  220. behavior: "allow",
  221. rules: [
  222. {
  223. capabilityGroup: "third_party_mcp",
  224. toolName: "mcp__fake__left_click",
  225. },
  226. ],
  227. type: "addRules",
  228. },
  229. ],
  230. },
  231. },
  232. ],
  233. summary: "Use fake tool",
  234. toolCallId: "tc-fake-group",
  235. toolName: "mcp__fake__left_click",
  236. });
  237. expect(parsed.success).toBe(false);
  238. });
  239. });