workspace-hook-trust.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. HookSourceKind,
  4. WORKSPACE_HOOK_SCHEMA_FIELDS,
  5. WORKSPACE_HOOK_STATE_ADMISSION_MAP,
  6. canonicalWorkspaceHookEntrySchema,
  7. createWorkspaceHookBundleSnapshot,
  8. workspaceHookEffectiveStateSchema,
  9. workspaceHookReviewFlowStateSchema,
  10. } from "../src/hooks/index.js";
  11. const DIGEST_A = "a".repeat(64);
  12. const DIGEST_B = "b".repeat(64);
  13. function canonicalEntry() {
  14. return {
  15. reviewItemId: "review-item-1",
  16. event: "SessionStart",
  17. matcherIndex: 0,
  18. hookIndex: 0,
  19. sourceFileIndex: 0,
  20. sourceRelativePath: ".zcode/config.json",
  21. matcher: "startup",
  22. type: "command",
  23. command: "./scripts/start.sh",
  24. async: true,
  25. shell: true,
  26. resolvedTimeoutMs: 30_000,
  27. resolvedMaxOutputBytes: 32_768,
  28. sourceRootEnabled: true,
  29. declarationEnabled: true,
  30. runtimeHooksEnabled: true,
  31. configuredEnabled: true,
  32. editable: true,
  33. declarationDigestAlgorithm: "sha256",
  34. hookDeclarationDigest: DIGEST_A,
  35. } as const;
  36. }
  37. describe("workspace hook trust contracts", () => {
  38. it("固化 project provenance 与真实 Hook schema exhaustive lock", () => {
  39. expect(HookSourceKind.Project).toBe("project");
  40. expect(WORKSPACE_HOOK_SCHEMA_FIELDS).toEqual({
  41. root: ["enabled", "timeoutMs", "maxOutputBytes", "events"],
  42. matcher: ["matcher", "hooks"],
  43. events: [
  44. "SessionStart",
  45. "UserPromptSubmit",
  46. "PreToolUse",
  47. "PermissionRequest",
  48. "PostToolUse",
  49. "PostToolUseFailure",
  50. "Stop",
  51. ],
  52. process: ["type", "command", "enabled", "args", "timeoutMs", "statusMessage"],
  53. command: [
  54. "type",
  55. "command",
  56. "enabled",
  57. "async",
  58. "shell",
  59. "timeout",
  60. "timeoutMs",
  61. "statusMessage",
  62. ],
  63. });
  64. });
  65. it("固化 state/admission/effective/reason 唯一映射", () => {
  66. expect(WORKSPACE_HOOK_STATE_ADMISSION_MAP).toEqual({
  67. not_applicable: {
  68. admissionClass: "not_applicable",
  69. effectiveRunnable: false,
  70. reasonCode: "workspace_hooks_not_applicable",
  71. },
  72. pending_trust: {
  73. admissionClass: "pending",
  74. effectiveRunnable: false,
  75. reasonCode: "workspace_hooks_pending_trust",
  76. },
  77. trusted_persistent: {
  78. admissionClass: "admitted",
  79. effectiveRunnable: "configured",
  80. reasonCode: "workspace_hooks_trusted_persistent",
  81. },
  82. blocked_untrusted: {
  83. admissionClass: "blocked",
  84. effectiveRunnable: false,
  85. reasonCode: "workspace_hooks_blocked_untrusted",
  86. },
  87. blocked_policy: {
  88. admissionClass: "blocked",
  89. effectiveRunnable: false,
  90. reasonCode: "workspace_hooks_blocked_by_policy",
  91. },
  92. revoked: {
  93. admissionClass: "pending",
  94. effectiveRunnable: false,
  95. reasonCode: "workspace_hooks_revoked",
  96. },
  97. stale_digest: {
  98. admissionClass: "pending",
  99. effectiveRunnable: false,
  100. reasonCode: "workspace_hook_declaration_changed",
  101. },
  102. });
  103. expect(
  104. workspaceHookEffectiveStateSchema.safeParse({
  105. reviewItemId: "review-item-1",
  106. sourceRootEnabled: true,
  107. declarationEnabled: true,
  108. runtimeHooksEnabled: true,
  109. configuredEnabled: true,
  110. editable: true,
  111. trustState: "revoked",
  112. admissionClass: "blocked",
  113. effectiveRunnable: false,
  114. sourcePaths: [".zcode/config.json"],
  115. reasonCode: "workspace_hooks_blocked_untrusted",
  116. }).success,
  117. ).toBe(false);
  118. });
  119. it("固化 immutable snapshot 与两层 digest", () => {
  120. expect(canonicalWorkspaceHookEntrySchema.parse(canonicalEntry())).toMatchObject({
  121. hookDeclarationDigest: DIGEST_A,
  122. configuredEnabled: true,
  123. });
  124. const snapshot = createWorkspaceHookBundleSnapshot({
  125. schemaVersion: 1,
  126. workspaceIdentity: "local:/workspace",
  127. discoveredAt: "2026-08-06T00:00:00.000Z",
  128. sourceFiles: [
  129. {
  130. canonicalPath: "/workspace/.zcode/config.json",
  131. baseDir: "/workspace",
  132. discoveryOrder: 0,
  133. configFileKind: ".zcode/config.json",
  134. explicitProjectConfig: false,
  135. editable: true,
  136. hooksRoot: { enabled: true, timeoutMs: 30_000, maxOutputBytes: 32_768 },
  137. },
  138. ],
  139. hooks: [canonicalEntry()],
  140. digestAlgorithm: "sha256",
  141. bundleDigest: DIGEST_B,
  142. });
  143. expect(snapshot.bundleDigest).toBe(DIGEST_B);
  144. expect(snapshot.hooks[0]?.hookDeclarationDigest).toBe(DIGEST_A);
  145. expect(Object.isFrozen(snapshot)).toBe(true);
  146. expect(Object.isFrozen(snapshot.hooks)).toBe(true);
  147. });
  148. it("固化 generation supersede 状态约束", () => {
  149. expect(
  150. workspaceHookReviewFlowStateSchema.safeParse({
  151. reviewFlowId: "flow-1",
  152. generation: 1,
  153. interactionId: "interaction-1",
  154. sessionId: "session-1",
  155. workspaceIdentity: "local:/workspace",
  156. bundleDigest: DIGEST_B,
  157. state: "superseded",
  158. createdAt: 1,
  159. deadlineAt: 2,
  160. }).success,
  161. ).toBe(false);
  162. expect(
  163. workspaceHookReviewFlowStateSchema.safeParse({
  164. reviewFlowId: "flow-1",
  165. generation: 1,
  166. interactionId: "interaction-1",
  167. sessionId: "session-1",
  168. workspaceIdentity: "local:/workspace",
  169. bundleDigest: DIGEST_B,
  170. state: "superseded",
  171. supersededByInteractionId: "interaction-2",
  172. createdAt: 1,
  173. deadlineAt: 2,
  174. }).success,
  175. ).toBe(true);
  176. });
  177. });
  178. describe("workspace hook trust phase 2 contracts", () => {
  179. it("固化 Trust store record、唯一 key 与私有诊断 metadata", async () => {
  180. const {
  181. WORKSPACE_HOOK_TRUST_STORE_SCHEMA_VERSION,
  182. workspaceHookTrustRecordSchema,
  183. workspaceHookTrustStoreFileSchema,
  184. } = await import("../src/hooks/index.js");
  185. const record = workspaceHookTrustRecordSchema.parse({
  186. workspaceIdentity: "local:/workspace",
  187. hookDeclarationDigest: DIGEST_A,
  188. digestAlgorithm: "sha256",
  189. decision: "trusted",
  190. grantedAt: "2026-08-06T00:00:00.000Z",
  191. bundleDigestAtGrant: DIGEST_B,
  192. eventAtGrant: "SessionStart",
  193. displayCommandAtGrant: "./scripts/start.sh",
  194. sourcePathAtGrant: ".zcode/config.json",
  195. sourceDiscoveryOrderAtGrant: 0,
  196. matcherAtGrant: "startup",
  197. matcherIndexAtGrant: 0,
  198. hookIndexAtGrant: 0,
  199. });
  200. expect(WORKSPACE_HOOK_TRUST_STORE_SCHEMA_VERSION).toBe(1);
  201. expect(record.matcherAtGrant).toBe("startup");
  202. expect(
  203. workspaceHookTrustStoreFileSchema.safeParse({
  204. schemaVersion: 1,
  205. records: [record, record],
  206. }).success,
  207. ).toBe(false);
  208. });
  209. it("固化 managed policy,临时授权不再属于 Workspace Hook 契约", async () => {
  210. const { workspaceHookPolicySchema } = await import("../src/hooks/index.js");
  211. expect(
  212. workspaceHookPolicySchema.parse({
  213. mode: "allow_trusted_only",
  214. reason: "organization pre-approval required",
  215. policyRevision: "managed:7",
  216. }),
  217. ).toMatchObject({ mode: "allow_trusted_only", policyRevision: "managed:7" });
  218. expect(
  219. workspaceHookPolicySchema.safeParse({ mode: "deny", policyRevision: "managed:8" }).success,
  220. ).toBe(false);
  221. expect(
  222. workspaceHookPolicySchema.parse({
  223. mode: "user_decides",
  224. policyRevision: "user:1",
  225. }),
  226. ).toMatchObject({ mode: "user_decides" });
  227. expect(
  228. workspaceHookPolicySchema.parse({
  229. mode: "deny",
  230. reason: "organization disabled workspace hooks",
  231. policyRevision: "managed:9",
  232. }),
  233. ).toMatchObject({ mode: "deny" });
  234. expect(
  235. workspaceHookReviewFlowStateSchema.safeParse({
  236. reviewFlowId: "flow-1",
  237. generation: 1,
  238. interactionId: "interaction-1",
  239. sessionId: "session-1",
  240. sessionFamilyId: "legacy-family",
  241. workspaceIdentity: "local:/workspace",
  242. bundleDigest: DIGEST_B,
  243. state: "pending",
  244. createdAt: 1,
  245. deadlineAt: 2,
  246. }).success,
  247. ).toBe(false);
  248. });
  249. });