workspace-hook-trust.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. // onError 进声明摘要(shared/workspace-hook-digest.ts),改了要重新信任。
  53. process: ["type", "command", "enabled", "args", "timeoutMs", "statusMessage", "onError"],
  54. command: [
  55. "type",
  56. "command",
  57. "enabled",
  58. "async",
  59. "shell",
  60. "timeout",
  61. "timeoutMs",
  62. "statusMessage",
  63. "onError",
  64. ],
  65. });
  66. });
  67. it("固化 state/admission/effective/reason 唯一映射", () => {
  68. expect(WORKSPACE_HOOK_STATE_ADMISSION_MAP).toEqual({
  69. not_applicable: {
  70. admissionClass: "not_applicable",
  71. effectiveRunnable: false,
  72. reasonCode: "workspace_hooks_not_applicable",
  73. },
  74. pending_trust: {
  75. admissionClass: "pending",
  76. effectiveRunnable: false,
  77. reasonCode: "workspace_hooks_pending_trust",
  78. },
  79. trusted_persistent: {
  80. admissionClass: "admitted",
  81. effectiveRunnable: "configured",
  82. reasonCode: "workspace_hooks_trusted_persistent",
  83. },
  84. blocked_untrusted: {
  85. admissionClass: "blocked",
  86. effectiveRunnable: false,
  87. reasonCode: "workspace_hooks_blocked_untrusted",
  88. },
  89. blocked_policy: {
  90. admissionClass: "blocked",
  91. effectiveRunnable: false,
  92. reasonCode: "workspace_hooks_blocked_by_policy",
  93. },
  94. revoked: {
  95. admissionClass: "pending",
  96. effectiveRunnable: false,
  97. reasonCode: "workspace_hooks_revoked",
  98. },
  99. stale_digest: {
  100. admissionClass: "pending",
  101. effectiveRunnable: false,
  102. reasonCode: "workspace_hook_declaration_changed",
  103. },
  104. });
  105. expect(
  106. workspaceHookEffectiveStateSchema.safeParse({
  107. reviewItemId: "review-item-1",
  108. sourceRootEnabled: true,
  109. declarationEnabled: true,
  110. runtimeHooksEnabled: true,
  111. configuredEnabled: true,
  112. editable: true,
  113. trustState: "revoked",
  114. admissionClass: "blocked",
  115. effectiveRunnable: false,
  116. sourcePaths: [".zcode/config.json"],
  117. reasonCode: "workspace_hooks_blocked_untrusted",
  118. }).success,
  119. ).toBe(false);
  120. });
  121. it("固化 immutable snapshot 与两层 digest", () => {
  122. expect(canonicalWorkspaceHookEntrySchema.parse(canonicalEntry())).toMatchObject({
  123. hookDeclarationDigest: DIGEST_A,
  124. configuredEnabled: true,
  125. });
  126. const snapshot = createWorkspaceHookBundleSnapshot({
  127. schemaVersion: 1,
  128. workspaceIdentity: "local:/workspace",
  129. discoveredAt: "2026-08-06T00:00:00.000Z",
  130. sourceFiles: [
  131. {
  132. canonicalPath: "/workspace/.zcode/config.json",
  133. baseDir: "/workspace",
  134. discoveryOrder: 0,
  135. configFileKind: ".zcode/config.json",
  136. explicitProjectConfig: false,
  137. editable: true,
  138. hooksRoot: { enabled: true, timeoutMs: 30_000, maxOutputBytes: 32_768 },
  139. },
  140. ],
  141. hooks: [canonicalEntry()],
  142. digestAlgorithm: "sha256",
  143. bundleDigest: DIGEST_B,
  144. });
  145. expect(snapshot.bundleDigest).toBe(DIGEST_B);
  146. expect(snapshot.hooks[0]?.hookDeclarationDigest).toBe(DIGEST_A);
  147. expect(Object.isFrozen(snapshot)).toBe(true);
  148. expect(Object.isFrozen(snapshot.hooks)).toBe(true);
  149. });
  150. it("固化 generation supersede 状态约束", () => {
  151. expect(
  152. workspaceHookReviewFlowStateSchema.safeParse({
  153. reviewFlowId: "flow-1",
  154. generation: 1,
  155. interactionId: "interaction-1",
  156. sessionId: "session-1",
  157. workspaceIdentity: "local:/workspace",
  158. bundleDigest: DIGEST_B,
  159. state: "superseded",
  160. createdAt: 1,
  161. deadlineAt: 2,
  162. }).success,
  163. ).toBe(false);
  164. expect(
  165. workspaceHookReviewFlowStateSchema.safeParse({
  166. reviewFlowId: "flow-1",
  167. generation: 1,
  168. interactionId: "interaction-1",
  169. sessionId: "session-1",
  170. workspaceIdentity: "local:/workspace",
  171. bundleDigest: DIGEST_B,
  172. state: "superseded",
  173. supersededByInteractionId: "interaction-2",
  174. createdAt: 1,
  175. deadlineAt: 2,
  176. }).success,
  177. ).toBe(true);
  178. });
  179. });
  180. describe("workspace hook trust phase 2 contracts", () => {
  181. it("固化 Trust store record、唯一 key 与私有诊断 metadata", async () => {
  182. const {
  183. WORKSPACE_HOOK_TRUST_STORE_SCHEMA_VERSION,
  184. workspaceHookTrustRecordSchema,
  185. workspaceHookTrustStoreFileSchema,
  186. } = await import("../src/hooks/index.js");
  187. const record = workspaceHookTrustRecordSchema.parse({
  188. workspaceIdentity: "local:/workspace",
  189. hookDeclarationDigest: DIGEST_A,
  190. digestAlgorithm: "sha256",
  191. decision: "trusted",
  192. grantedAt: "2026-08-06T00:00:00.000Z",
  193. bundleDigestAtGrant: DIGEST_B,
  194. eventAtGrant: "SessionStart",
  195. displayCommandAtGrant: "./scripts/start.sh",
  196. sourcePathAtGrant: ".zcode/config.json",
  197. sourceDiscoveryOrderAtGrant: 0,
  198. matcherAtGrant: "startup",
  199. matcherIndexAtGrant: 0,
  200. hookIndexAtGrant: 0,
  201. });
  202. expect(WORKSPACE_HOOK_TRUST_STORE_SCHEMA_VERSION).toBe(1);
  203. expect(record.matcherAtGrant).toBe("startup");
  204. expect(
  205. workspaceHookTrustStoreFileSchema.safeParse({
  206. schemaVersion: 1,
  207. records: [record, record],
  208. }).success,
  209. ).toBe(false);
  210. });
  211. it("固化 managed policy,临时授权不再属于 Workspace Hook 契约", async () => {
  212. const { workspaceHookPolicySchema } = await import("../src/hooks/index.js");
  213. expect(
  214. workspaceHookPolicySchema.parse({
  215. mode: "allow_trusted_only",
  216. reason: "organization pre-approval required",
  217. policyRevision: "managed:7",
  218. }),
  219. ).toMatchObject({ mode: "allow_trusted_only", policyRevision: "managed:7" });
  220. expect(
  221. workspaceHookPolicySchema.safeParse({ mode: "deny", policyRevision: "managed:8" }).success,
  222. ).toBe(false);
  223. expect(
  224. workspaceHookPolicySchema.parse({
  225. mode: "user_decides",
  226. policyRevision: "user:1",
  227. }),
  228. ).toMatchObject({ mode: "user_decides" });
  229. expect(
  230. workspaceHookPolicySchema.parse({
  231. mode: "deny",
  232. reason: "organization disabled workspace hooks",
  233. policyRevision: "managed:9",
  234. }),
  235. ).toMatchObject({ mode: "deny" });
  236. expect(
  237. workspaceHookReviewFlowStateSchema.safeParse({
  238. reviewFlowId: "flow-1",
  239. generation: 1,
  240. interactionId: "interaction-1",
  241. sessionId: "session-1",
  242. sessionFamilyId: "legacy-family",
  243. workspaceIdentity: "local:/workspace",
  244. bundleDigest: DIGEST_B,
  245. state: "pending",
  246. createdAt: 1,
  247. deadlineAt: 2,
  248. }).success,
  249. ).toBe(false);
  250. });
  251. });