@kya-os/mcp-i-runtime 1.2.5 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/auth/types.d.ts +2 -2
  3. package/dist/auth/types.d.ts.map +1 -1
  4. package/dist/config/remote-config.d.ts +3 -3
  5. package/dist/config/remote-config.js +6 -6
  6. package/dist/config/remote-config.js.map +1 -1
  7. package/dist/identity/user-did-manager.d.ts +4 -4
  8. package/dist/identity/user-did-manager.js +5 -5
  9. package/dist/identity/user-did-manager.js.map +1 -1
  10. package/dist/index.d.ts +4 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +11 -3
  13. package/dist/index.js.map +1 -1
  14. package/dist/runtime/audit.d.ts.map +1 -1
  15. package/dist/runtime/audit.js +22 -0
  16. package/dist/runtime/audit.js.map +1 -1
  17. package/dist/runtime/base.d.ts +14 -11
  18. package/dist/runtime/base.d.ts.map +1 -1
  19. package/dist/runtime/base.js +51 -29
  20. package/dist/runtime/base.js.map +1 -1
  21. package/dist/runtime/delegation-denial.d.ts +43 -0
  22. package/dist/runtime/delegation-denial.d.ts.map +1 -0
  23. package/dist/runtime/delegation-denial.js +83 -0
  24. package/dist/runtime/delegation-denial.js.map +1 -0
  25. package/dist/runtime/verified-delegation.d.ts +30 -0
  26. package/dist/runtime/verified-delegation.d.ts.map +1 -0
  27. package/dist/runtime/verified-delegation.js +62 -0
  28. package/dist/runtime/verified-delegation.js.map +1 -0
  29. package/dist/services/access-control.service.d.ts +4 -4
  30. package/dist/services/access-control.service.d.ts.map +1 -1
  31. package/dist/services/access-control.service.js +34 -34
  32. package/dist/services/access-control.service.js.map +1 -1
  33. package/dist/services/delegation-error-formatter.d.ts +15 -0
  34. package/dist/services/delegation-error-formatter.d.ts.map +1 -1
  35. package/dist/services/delegation-error-formatter.js +27 -1
  36. package/dist/services/delegation-error-formatter.js.map +1 -1
  37. package/dist/services/oauth-config.service.d.ts +4 -4
  38. package/dist/services/oauth-config.service.d.ts.map +1 -1
  39. package/dist/services/oauth-config.service.js +8 -8
  40. package/dist/services/oauth-config.service.js.map +1 -1
  41. package/dist/services/oauth-provider-registry.d.ts +6 -4
  42. package/dist/services/oauth-provider-registry.d.ts.map +1 -1
  43. package/dist/services/oauth-provider-registry.js +9 -5
  44. package/dist/services/oauth-provider-registry.js.map +1 -1
  45. package/dist/services/oauth-service.d.ts +17 -10
  46. package/dist/services/oauth-service.d.ts.map +1 -1
  47. package/dist/services/oauth-service.js +14 -14
  48. package/dist/services/oauth-service.js.map +1 -1
  49. package/dist/services/oauth-token-retrieval.service.d.ts +6 -6
  50. package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -1
  51. package/dist/services/oauth-token-retrieval.service.js +5 -5
  52. package/dist/services/provider-resolver.d.ts +1 -1
  53. package/dist/services/provider-resolver.js +7 -7
  54. package/dist/services/provider-resolver.js.map +1 -1
  55. package/dist/services/session-registration.service.d.ts +5 -5
  56. package/dist/services/session-registration.service.d.ts.map +1 -1
  57. package/dist/services/session-registration.service.js +9 -9
  58. package/dist/services/session-registration.service.js.map +1 -1
  59. package/dist/services/tool-protection.service.d.ts +15 -15
  60. package/dist/services/tool-protection.service.js +18 -18
  61. package/dist/services/tool-protection.service.js.map +1 -1
  62. package/dist/types/tool-protection.d.ts +11 -4
  63. package/dist/types/tool-protection.d.ts.map +1 -1
  64. package/dist/types/tool-protection.js +8 -1
  65. package/dist/types/tool-protection.js.map +1 -1
  66. package/package.json +3 -3
  67. package/src/__tests__/config/provider-runtime-config.test.ts +1 -1
  68. package/src/__tests__/config.test.ts +3 -3
  69. package/src/__tests__/index.test.ts +12 -0
  70. package/src/__tests__/integration/full-flow.test.ts +4 -4
  71. package/src/__tests__/regression/phase2-regression.test.ts +2 -2
  72. package/src/__tests__/runtime/audit-contracts-compat.test.ts +54 -0
  73. package/src/__tests__/runtime/audit.test.ts +172 -0
  74. package/src/__tests__/runtime/delegation-denial.test.ts +115 -0
  75. package/src/__tests__/runtime/process-tool-call-security.test.ts +326 -9
  76. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +25 -10
  77. package/src/__tests__/runtime/verified-delegation.test.ts +129 -0
  78. package/src/__tests__/services/access-control-retry.test.ts +2 -2
  79. package/src/__tests__/services/cache-busting.test.ts +1 -1
  80. package/src/__tests__/services/{agentshield-integration.test.ts → checkpoint-integration.test.ts} +3 -3
  81. package/src/__tests__/services/former-names.test.ts +114 -0
  82. package/src/__tests__/services/oauth-service-errors.test.ts +5 -5
  83. package/src/__tests__/services/oauth-service-pkce.test.ts +2 -2
  84. package/src/__tests__/services/oauth-service-secret-resolution.test.ts +15 -15
  85. package/src/__tests__/services/provider-resolver-edge-cases.test.ts +11 -11
  86. package/src/__tests__/services/tool-protection.service.test.ts +1 -1
  87. package/src/auth/types.ts +2 -2
  88. package/src/config/remote-config.ts +6 -6
  89. package/src/identity/user-did-manager.ts +5 -5
  90. package/src/index.ts +15 -0
  91. package/src/runtime/audit.ts +26 -0
  92. package/src/runtime/base.ts +63 -32
  93. package/src/runtime/delegation-denial.ts +107 -0
  94. package/src/runtime/verified-delegation.ts +66 -0
  95. package/src/services/__tests__/access-control.integration.test.ts +3 -3
  96. package/src/services/__tests__/access-control.proof-response-validation.test.ts +15 -15
  97. package/src/services/__tests__/access-control.service.test.ts +14 -14
  98. package/src/services/__tests__/delegation-error-formatter.test.ts +156 -0
  99. package/src/services/__tests__/oauth-config.service.test.ts +2 -2
  100. package/src/services/__tests__/oauth-provider-registry.test.ts +11 -11
  101. package/src/services/__tests__/provider-resolution.integration.test.ts +6 -6
  102. package/src/services/__tests__/provider-resolver.test.ts +1 -1
  103. package/src/services/__tests__/session-registration.service.test.ts +1 -1
  104. package/src/services/access-control.service.ts +31 -31
  105. package/src/services/delegation-error-formatter.ts +42 -1
  106. package/src/services/oauth-config.service.d.ts +4 -4
  107. package/src/services/oauth-config.service.ts +9 -9
  108. package/src/services/oauth-provider-registry.d.ts +4 -2
  109. package/src/services/oauth-provider-registry.ts +10 -5
  110. package/src/services/oauth-service.ts +30 -19
  111. package/src/services/oauth-token-retrieval.service.ts +8 -8
  112. package/src/services/provider-resolver.ts +7 -7
  113. package/src/services/session-registration.service.ts +10 -10
  114. package/src/services/tool-protection.service.ts +19 -19
  115. package/src/types/tool-protection.ts +13 -4
  116. package/vitest.config.mts +1 -1
@@ -17,7 +17,7 @@ import {
17
17
  } from "../utils/mock-providers";
18
18
  import type { AccessControlApiService } from "../../services/access-control.service";
19
19
  import type { ToolProtectionService } from "../../services/tool-protection.service";
20
- import { AgentShieldAPIError } from "@kya-os/contracts/agentshield-api";
20
+ import { CheckpointAPIError } from "@kya-os/contracts/checkpoint-api";
21
21
 
22
22
  // Mock AccessControlApiService
23
23
  function createMockAccessControlService(
@@ -165,13 +165,19 @@ describe("processToolCall Security", () => {
165
165
  session
166
166
  );
167
167
 
168
- // Tool handler SHOULD be called
169
- expect(toolHandler).toHaveBeenCalledWith({ arg: "value" });
168
+ // Tool handler SHOULD be called, with the verified delegation second
169
+ expect(toolHandler).toHaveBeenCalledWith(
170
+ { arg: "value" },
171
+ expect.objectContaining({
172
+ id: "test-delegation",
173
+ scopes: ["tool:execute"],
174
+ })
175
+ );
170
176
  expect(result).toEqual({ result: "success" });
171
177
  });
172
178
 
173
- it("should allow delegation when user_identifier is email and AgentShield verifies it", async () => {
174
- // AgentShield may return email as user_identifier but internally link it to DID
179
+ it("should allow delegation when user_identifier is email and Checkpoint verifies it", async () => {
180
+ // Checkpoint may return email as user_identifier but internally link it to DID
175
181
  mockAccessControlService.verifyDelegation = vi.fn().mockResolvedValue({
176
182
  data: {
177
183
  valid: true,
@@ -179,7 +185,7 @@ describe("processToolCall Security", () => {
179
185
  credential: {
180
186
  scopes: ["tool:execute"],
181
187
  user_identifier: "user@example.com", // Email, not DID
182
- // No user_id field - AgentShield verified internally
188
+ // No user_id field - Checkpoint verified internally
183
189
  },
184
190
  },
185
191
  });
@@ -193,7 +199,7 @@ describe("processToolCall Security", () => {
193
199
 
194
200
  const toolHandler = vi.fn().mockResolvedValue({ result: "success" });
195
201
 
196
- // Should allow because email doesn't start with "did:" - trusted AgentShield verification
202
+ // Should allow because email doesn't start with "did:" - trusted Checkpoint verification
197
203
  const result = await runtime.processToolCall(
198
204
  "protected-tool",
199
205
  { arg: "value" },
@@ -413,11 +419,11 @@ describe("processToolCall Security", () => {
413
419
  expect(toolHandler).not.toHaveBeenCalled();
414
420
  });
415
421
 
416
- it("should throw DelegationRequiredError on AgentShieldAPIError", async () => {
422
+ it("should throw DelegationRequiredError on CheckpointAPIError", async () => {
417
423
  mockAccessControlService.verifyDelegation = vi
418
424
  .fn()
419
425
  .mockRejectedValue(
420
- new AgentShieldAPIError("network_error", "Connection failed")
426
+ new CheckpointAPIError("network_error", "Connection failed")
421
427
  );
422
428
 
423
429
  const session = {
@@ -1015,4 +1021,315 @@ describe("processToolCall Security", () => {
1015
1021
  );
1016
1022
  });
1017
1023
  });
1024
+
1025
+ describe("Verified Delegation Handoff", () => {
1026
+ const crisp = {
1027
+ units: "CHF",
1028
+ max_amount: "50.00",
1029
+ product_class: ["https://id.gs1.org/01/09506000134352"],
1030
+ };
1031
+
1032
+ function verifiedResponse(delegationId: string, scopes: string[]) {
1033
+ return {
1034
+ data: {
1035
+ valid: true,
1036
+ delegation_id: delegationId,
1037
+ credential: {
1038
+ agent_did: "did:key:zmock123",
1039
+ user_did: "did:key:zUser123",
1040
+ scopes,
1041
+ constraints: { not_after: 1_900_000_000, crisp },
1042
+ issued_at: 1_800_000_000,
1043
+ created_at: 1_800_000_000,
1044
+ authorization: { type: "none" },
1045
+ },
1046
+ },
1047
+ };
1048
+ }
1049
+
1050
+ const session = {
1051
+ id: "test-session",
1052
+ userDid: "did:key:zUser123",
1053
+ agentDid: "did:key:zmock123",
1054
+ delegationToken: "valid-token",
1055
+ };
1056
+
1057
+ it("passes the verifier's answer to the handler as the second argument", async () => {
1058
+ mockAccessControlService.verifyDelegation = vi
1059
+ .fn()
1060
+ .mockResolvedValue(
1061
+ verifiedResponse("delegation-1", ["tool:execute", "tool:read"])
1062
+ );
1063
+ const toolHandler = vi.fn().mockResolvedValue({ result: "success" });
1064
+
1065
+ await runtime.processToolCall(
1066
+ "protected-tool",
1067
+ { arg: "value" },
1068
+ toolHandler,
1069
+ session
1070
+ );
1071
+
1072
+ expect(toolHandler).toHaveBeenCalledTimes(1);
1073
+ expect(toolHandler).toHaveBeenCalledWith(
1074
+ { arg: "value" },
1075
+ {
1076
+ id: "delegation-1",
1077
+ agentDid: "did:key:zmock123",
1078
+ userDid: "did:key:zUser123",
1079
+ // Scopes come from the verifier, not from the tool's requiredScopes.
1080
+ scopes: ["tool:execute", "tool:read"],
1081
+ constraints: { not_after: 1_900_000_000, crisp },
1082
+ authorizationType: "none",
1083
+ expiresAt: 1_900_000_000,
1084
+ }
1085
+ );
1086
+ });
1087
+
1088
+ it("calls an unprotected tool's handler with exactly one argument", async () => {
1089
+ mockToolProtectionService.checkToolProtection = vi
1090
+ .fn()
1091
+ .mockResolvedValue(null);
1092
+ const toolHandler = vi.fn().mockResolvedValue({ result: "success" });
1093
+
1094
+ await runtime.processToolCall(
1095
+ "public-tool",
1096
+ { arg: "value" },
1097
+ toolHandler,
1098
+ session
1099
+ );
1100
+
1101
+ expect(toolHandler.mock.calls).toEqual([[{ arg: "value" }]]);
1102
+ });
1103
+
1104
+ it("calls the handler with exactly one argument when the verifier returns no credential", async () => {
1105
+ mockAccessControlService.verifyDelegation = vi.fn().mockResolvedValue({
1106
+ data: { valid: true, delegation_id: "kv-delegation" },
1107
+ });
1108
+ const toolHandler = vi.fn().mockResolvedValue({ result: "success" });
1109
+
1110
+ await runtime.processToolCall(
1111
+ "protected-tool",
1112
+ { arg: "value" },
1113
+ toolHandler,
1114
+ session
1115
+ );
1116
+
1117
+ expect(toolHandler.mock.calls).toEqual([[{ arg: "value" }]]);
1118
+ });
1119
+
1120
+ it("gives each of two interleaved calls its own verified delegation", async () => {
1121
+ const releaseFirstVerify: Array<() => void> = [];
1122
+ mockAccessControlService.verifyDelegation = vi
1123
+ .fn()
1124
+ .mockImplementationOnce(
1125
+ () =>
1126
+ new Promise((resolve) => {
1127
+ releaseFirstVerify.push(() =>
1128
+ resolve(verifiedResponse("delegation-first", ["tool:execute"]))
1129
+ );
1130
+ })
1131
+ )
1132
+ .mockResolvedValueOnce(
1133
+ verifiedResponse("delegation-second", ["tool:execute"])
1134
+ );
1135
+ const firstHandler = vi.fn().mockResolvedValue({ result: "first" });
1136
+ const secondHandler = vi.fn().mockResolvedValue({ result: "second" });
1137
+
1138
+ const firstCall = runtime.processToolCall(
1139
+ "protected-tool",
1140
+ { call: 1 },
1141
+ firstHandler,
1142
+ { ...session, delegationToken: "first-token" }
1143
+ );
1144
+ // Let the first call reach its (still pending) verify before starting the second.
1145
+ await vi.waitFor(() => expect(releaseFirstVerify).toHaveLength(1));
1146
+ await runtime.processToolCall(
1147
+ "protected-tool",
1148
+ { call: 2 },
1149
+ secondHandler,
1150
+ { ...session, delegationToken: "second-token" }
1151
+ );
1152
+ releaseFirstVerify[0]();
1153
+ await firstCall;
1154
+
1155
+ expect(firstHandler).toHaveBeenCalledWith(
1156
+ { call: 1 },
1157
+ expect.objectContaining({ id: "delegation-first" })
1158
+ );
1159
+ expect(secondHandler).toHaveBeenCalledWith(
1160
+ { call: 2 },
1161
+ expect.objectContaining({ id: "delegation-second" })
1162
+ );
1163
+ });
1164
+
1165
+ it("does not call the handler when verification fails", async () => {
1166
+ mockAccessControlService.verifyDelegation = vi.fn().mockResolvedValue({
1167
+ data: { valid: false, reason: "revoked" },
1168
+ });
1169
+ const toolHandler = vi.fn().mockResolvedValue({ result: "success" });
1170
+
1171
+ await expect(
1172
+ runtime.processToolCall(
1173
+ "protected-tool",
1174
+ { arg: "value" },
1175
+ toolHandler,
1176
+ session
1177
+ )
1178
+ ).rejects.toThrow(DelegationRequiredError);
1179
+ expect(toolHandler).not.toHaveBeenCalled();
1180
+ });
1181
+ });
1182
+
1183
+ describe("Verifier Denial Codes", () => {
1184
+ const session = {
1185
+ id: "test-session",
1186
+ userDid: "did:key:zUser123",
1187
+ agentDid: "did:key:zmock123",
1188
+ delegationToken: "presented-token",
1189
+ };
1190
+
1191
+ async function denialFor(
1192
+ verifyData: Record<string, unknown>,
1193
+ sessionOverrides: Record<string, unknown> = {}
1194
+ ) {
1195
+ mockAccessControlService.verifyDelegation = vi
1196
+ .fn()
1197
+ .mockResolvedValue({ data: verifyData });
1198
+ const toolHandler = vi.fn().mockResolvedValue({ result: "success" });
1199
+
1200
+ const error = await runtime
1201
+ .processToolCall(
1202
+ "protected-tool",
1203
+ { arg: "value" },
1204
+ toolHandler,
1205
+ { ...session, ...sessionOverrides }
1206
+ )
1207
+ .then(
1208
+ () => undefined,
1209
+ (thrown: unknown) => thrown
1210
+ );
1211
+
1212
+ expect(error).toBeInstanceOf(DelegationRequiredError);
1213
+ expect(toolHandler).not.toHaveBeenCalled();
1214
+ return error as DelegationRequiredError;
1215
+ }
1216
+
1217
+ it("reports a revoked grant without leaking the upstream message or details", async () => {
1218
+ const error = await denialFor({
1219
+ valid: false,
1220
+ error: {
1221
+ code: "delegation_revoked",
1222
+ message: "secret: revoked by admin",
1223
+ details: { reason: "secret fraud case 4411" },
1224
+ },
1225
+ });
1226
+
1227
+ expect(error.denial).toEqual({
1228
+ code: "delegation_revoked",
1229
+ message: "This authorization was revoked. Nothing was executed.",
1230
+ });
1231
+ expect(error.consentUrl).toBeTruthy();
1232
+ expect(error.resumeToken).toBeTruthy();
1233
+ expect(
1234
+ JSON.stringify({ message: error.message, denial: error.denial })
1235
+ ).not.toContain("secret");
1236
+ });
1237
+
1238
+ it("keeps agent_mismatch distinct from user_mismatch", async () => {
1239
+ const agentError = await denialFor({
1240
+ valid: false,
1241
+ error: { code: "agent_mismatch", message: "Agent DID mismatch" },
1242
+ });
1243
+ const userError = await denialFor({
1244
+ valid: false,
1245
+ error: { code: "user_mismatch", message: "User DID mismatch" },
1246
+ });
1247
+
1248
+ expect(agentError.denial?.code).toBe("agent_mismatch");
1249
+ expect(userError.denial?.code).toBe("user_mismatch");
1250
+ });
1251
+
1252
+ it("falls back to a legacy top-level reason only when it is an allowlisted code", async () => {
1253
+ const legacyError = await denialFor({
1254
+ valid: false,
1255
+ reason: "delegation_expired",
1256
+ });
1257
+ const freeTextError = await denialFor({
1258
+ valid: false,
1259
+ reason: "Delegation token invalid or expired",
1260
+ });
1261
+
1262
+ expect(legacyError.denial?.code).toBe("delegation_expired");
1263
+ expect(freeTextError.denial).toBeUndefined();
1264
+ });
1265
+
1266
+ it("reports a native holder binding mismatch from the runtime's own comparison", async () => {
1267
+ const error = await denialFor(
1268
+ {
1269
+ valid: true,
1270
+ delegation_id: "delegation-for-another-holder",
1271
+ credential: {
1272
+ agent_did: "did:key:zSomeoneElse",
1273
+ user_did: "did:key:zUser123",
1274
+ scopes: ["tool:execute"],
1275
+ issued_at: 1,
1276
+ created_at: 1,
1277
+ authorization: { type: "none" },
1278
+ },
1279
+ },
1280
+ { verifiedInvokerDid: "did:key:zAdmittedHolder" }
1281
+ );
1282
+
1283
+ expect(error.denial?.code).toBe("native_binding_mismatch");
1284
+ });
1285
+
1286
+ it("keeps the verifier's own denial code on the native rail", async () => {
1287
+ // A refused grant carries no credential, so a holder comparison against
1288
+ // it is meaningless and must not mask why Checkpoint refused it.
1289
+ const revokedError = await denialFor(
1290
+ {
1291
+ valid: false,
1292
+ error: { code: "delegation_revoked", message: "Revoked" },
1293
+ },
1294
+ { verifiedInvokerDid: "did:key:zAdmittedHolder" }
1295
+ );
1296
+ const unknownCodeError = await denialFor(
1297
+ {
1298
+ valid: false,
1299
+ error: { code: "project_not_found", message: "Project not found" },
1300
+ },
1301
+ { verifiedInvokerDid: "did:key:zAdmittedHolder" }
1302
+ );
1303
+
1304
+ expect(revokedError.denial?.code).toBe("delegation_revoked");
1305
+ expect(unknownCodeError.denial).toBeUndefined();
1306
+ });
1307
+
1308
+ it("leaves the denial undefined for a code outside the allowlist", async () => {
1309
+ const error = await denialFor({
1310
+ valid: false,
1311
+ error: { code: "project_not_found", message: "Project not found" },
1312
+ });
1313
+
1314
+ expect(error.denial).toBeUndefined();
1315
+ expect(error.consentUrl).toBeTruthy();
1316
+ });
1317
+
1318
+ it("leaves the denial undefined when no delegation was presented", async () => {
1319
+ const toolHandler = vi.fn();
1320
+
1321
+ const error = await runtime
1322
+ .processToolCall("protected-tool", {}, toolHandler, {
1323
+ id: "test-session",
1324
+ })
1325
+ .then(
1326
+ () => undefined,
1327
+ (thrown: unknown) => thrown
1328
+ );
1329
+
1330
+ expect(error).toBeInstanceOf(DelegationRequiredError);
1331
+ expect((error as DelegationRequiredError).denial).toBeUndefined();
1332
+ expect(mockAccessControlService.verifyDelegation).not.toHaveBeenCalled();
1333
+ });
1334
+ });
1018
1335
  });
@@ -9,7 +9,7 @@ import { MCPIRuntimeBase } from "../../runtime/base";
9
9
  import { ProviderRuntimeConfig } from "../../config";
10
10
  import { createToolProtectionRuntime } from "../utils/tool-protection-runtime";
11
11
  import { DelegationRequiredError } from "../../types/tool-protection";
12
- import type { VerifyDelegationAPIResponse } from "@kya-os/contracts/agentshield-api";
12
+ import type { VerifyDelegationAPIResponse } from "@kya-os/contracts/checkpoint-api";
13
13
 
14
14
  describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
15
15
  let runtime: MCPIRuntimeBase;
@@ -115,7 +115,10 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
115
115
  })
116
116
  );
117
117
 
118
- expect(mockHandler).toHaveBeenCalledWith({ arg: "value" });
118
+ expect(mockHandler).toHaveBeenCalledWith(
119
+ { arg: "value" },
120
+ expect.objectContaining({ id: "test-delegation-id" })
121
+ );
119
122
  expect(result).toEqual({ result: "success" });
120
123
  });
121
124
 
@@ -164,7 +167,10 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
164
167
  })
165
168
  );
166
169
 
167
- expect(mockHandler).toHaveBeenCalledWith({ arg: "value" });
170
+ expect(mockHandler).toHaveBeenCalledWith(
171
+ { arg: "value" },
172
+ expect.objectContaining({ id: "test-delegation-id" })
173
+ );
168
174
  expect(result).toEqual({ result: "success" });
169
175
  });
170
176
 
@@ -252,11 +258,11 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
252
258
  });
253
259
 
254
260
  // Mock API error
255
- const { AgentShieldAPIError } = await import(
256
- "@kya-os/contracts/agentshield-api"
261
+ const { CheckpointAPIError } = await import(
262
+ "@kya-os/contracts/checkpoint-api"
257
263
  );
258
264
  mockAccessControlService.verifyDelegation.mockRejectedValue(
259
- new AgentShieldAPIError("network_error", "API unavailable", {})
265
+ new CheckpointAPIError("network_error", "API unavailable", {})
260
266
  );
261
267
 
262
268
  const sessionWithDelegation = {
@@ -388,7 +394,10 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
388
394
  );
389
395
 
390
396
  expect(mockAccessControlService.verifyDelegation).toHaveBeenCalled();
391
- expect(mockHandler).toHaveBeenCalledWith({ arg: "value" });
397
+ expect(mockHandler).toHaveBeenCalledWith(
398
+ { arg: "value" },
399
+ expect.objectContaining({ id: "test-delegation-id" })
400
+ );
392
401
  expect(result).toEqual({ result: "success" });
393
402
  });
394
403
 
@@ -428,7 +437,10 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
428
437
  sessionWithUserA
429
438
  );
430
439
 
431
- expect(mockHandler).toHaveBeenCalledWith({ arg: "value" });
440
+ expect(mockHandler).toHaveBeenCalledWith(
441
+ { arg: "value" },
442
+ expect.objectContaining({ id: "test-delegation-id" })
443
+ );
432
444
  expect(result).toEqual({ result: "success" });
433
445
  });
434
446
 
@@ -469,12 +481,15 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
469
481
  sessionWithoutUserDid
470
482
  );
471
483
 
472
- expect(mockHandler).toHaveBeenCalledWith({ arg: "value" });
484
+ expect(mockHandler).toHaveBeenCalledWith(
485
+ { arg: "value" },
486
+ expect.objectContaining({ id: "test-delegation-id" })
487
+ );
473
488
  expect(result).toEqual({ result: "success" });
474
489
  });
475
490
 
476
491
  it("should include user_did in verification request when session.userDid is set", async () => {
477
- // ✅ SECURITY: Verify user_did is sent to AgentShield for user-scoped verification
492
+ // ✅ SECURITY: Verify user_did is sent to Checkpoint for user-scoped verification
478
493
  // This prevents cross-user delegation leakage (Priority 3 fallback issue)
479
494
  mockToolProtectionService.checkToolProtection.mockResolvedValue({
480
495
  requiresDelegation: true,
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Tests for toVerifiedDelegation: the pure mapping from a verify response to
3
+ * the VerifiedDelegation that protected tool handlers receive.
4
+ */
5
+
6
+ import { describe, it, expect } from "vitest";
7
+ import type { VerifyDelegationResponse } from "@kya-os/contracts/checkpoint-api";
8
+ import { toVerifiedDelegation } from "../../runtime/verified-delegation";
9
+
10
+ const NOT_AFTER_SECONDS = 1_900_000_000;
11
+
12
+ function checkpointVerifyResponse(
13
+ overrides: Partial<VerifyDelegationResponse> = {}
14
+ ): VerifyDelegationResponse {
15
+ return {
16
+ valid: true,
17
+ delegation_id: "0d6f4c1e-2b1a-4d8e-9a57-5b2f1c7e9d10",
18
+ credential: {
19
+ agent_did: "did:web:knowthat.ai:agents:kya-commerce",
20
+ user_did: "did:key:zUser",
21
+ scopes: ["place_order:execute"],
22
+ constraints: {
23
+ not_before: 0,
24
+ not_after: NOT_AFTER_SECONDS,
25
+ crisp: {
26
+ units: "CHF",
27
+ max_amount: "50.00",
28
+ product_class: ["https://id.gs1.org/01/09506000134352"],
29
+ },
30
+ },
31
+ issued_at: 1_800_000_000,
32
+ created_at: 1_800_000_000,
33
+ authorization: { type: "none" },
34
+ },
35
+ ...overrides,
36
+ };
37
+ }
38
+
39
+ describe("toVerifiedDelegation", () => {
40
+ it("maps a valid verify response, including nested crisp constraints", () => {
41
+ const verified = toVerifiedDelegation(checkpointVerifyResponse());
42
+
43
+ expect(verified).toEqual({
44
+ id: "0d6f4c1e-2b1a-4d8e-9a57-5b2f1c7e9d10",
45
+ agentDid: "did:web:knowthat.ai:agents:kya-commerce",
46
+ userDid: "did:key:zUser",
47
+ scopes: ["place_order:execute"],
48
+ constraints: {
49
+ not_before: 0,
50
+ not_after: NOT_AFTER_SECONDS,
51
+ crisp: {
52
+ units: "CHF",
53
+ max_amount: "50.00",
54
+ product_class: ["https://id.gs1.org/01/09506000134352"],
55
+ },
56
+ },
57
+ authorizationType: "none",
58
+ expiresAt: NOT_AFTER_SECONDS,
59
+ });
60
+ });
61
+
62
+ it("returns undefined when the verifier did not accept the delegation", () => {
63
+ expect(
64
+ toVerifiedDelegation(checkpointVerifyResponse({ valid: false }))
65
+ ).toBeUndefined();
66
+ });
67
+
68
+ it("returns undefined when the verifier returned no credential (KV and memory verifiers)", () => {
69
+ expect(
70
+ toVerifiedDelegation({ valid: true, delegation_id: "kv-delegation" })
71
+ ).toBeUndefined();
72
+ });
73
+
74
+ it.each([
75
+ ["zero", 0],
76
+ ["negative", -1],
77
+ ["a string", "1900000000"],
78
+ ])("sets no expiresAt when not_after is %s", (_label, notAfter) => {
79
+ const response = checkpointVerifyResponse();
80
+ response.credential!.constraints = { not_after: notAfter };
81
+
82
+ const verified = toVerifiedDelegation(response);
83
+
84
+ expect(verified).toBeDefined();
85
+ expect(verified).not.toHaveProperty("expiresAt");
86
+ });
87
+
88
+ it("sets no expiresAt and no constraints when the credential has none", () => {
89
+ const response = checkpointVerifyResponse();
90
+ delete response.credential!.constraints;
91
+
92
+ const verified = toVerifiedDelegation(response);
93
+
94
+ expect(verified).not.toHaveProperty("expiresAt");
95
+ expect(verified).not.toHaveProperty("constraints");
96
+ });
97
+
98
+ it("omits id when the response carries no delegation_id", () => {
99
+ const verified = toVerifiedDelegation(
100
+ checkpointVerifyResponse({ delegation_id: undefined })
101
+ );
102
+
103
+ expect(verified).not.toHaveProperty("id");
104
+ expect(verified?.scopes).toEqual(["place_order:execute"]);
105
+ });
106
+
107
+ it("copies scopes and constraints, so mutating the output leaves the input unchanged", () => {
108
+ const response = checkpointVerifyResponse();
109
+ const verified = toVerifiedDelegation(response)!;
110
+
111
+ verified.scopes.push("admin:everything");
112
+ (verified.constraints!.crisp as Record<string, unknown>).max_amount =
113
+ "999999.00";
114
+
115
+ expect(response.credential!.scopes).toEqual(["place_order:execute"]);
116
+ expect(
117
+ (response.credential!.constraints!.crisp as Record<string, unknown>)
118
+ .max_amount
119
+ ).toBe("50.00");
120
+ });
121
+
122
+ it("treats a non-array scopes field as no scopes rather than trusting it", () => {
123
+ const response = checkpointVerifyResponse();
124
+ (response.credential as unknown as Record<string, unknown>).scopes =
125
+ "place_order:execute";
126
+
127
+ expect(toVerifiedDelegation(response)?.scopes).toEqual([]);
128
+ });
129
+ });
@@ -250,7 +250,7 @@ describe("AccessControlApiService Retry Logic", () => {
250
250
  expect(mockFetch).toHaveBeenCalledTimes(1);
251
251
  });
252
252
 
253
- it("should throw AgentShieldAPIError on response with success: false", async () => {
253
+ it("should throw CheckpointAPIError on response with success: false", async () => {
254
254
  mockFetch.mockResolvedValue(
255
255
  createMockResponse({
256
256
  ok: true,
@@ -383,7 +383,7 @@ describe("AccessControlApiService Retry Logic", () => {
383
383
  });
384
384
 
385
385
  describe("Retry Behavior (Server Errors)", () => {
386
- it("should retry on 5xx server errors when AgentShieldAPIError thrown", async () => {
386
+ it("should retry on 5xx server errors when CheckpointAPIError thrown", async () => {
387
387
  // First call returns 500, second succeeds
388
388
  mockFetch
389
389
  .mockResolvedValueOnce(
@@ -12,7 +12,7 @@ describe("Cache Busting in clearAndRefresh", () => {
12
12
  cache = new InMemoryToolProtectionCache();
13
13
  service = new ToolProtectionService(
14
14
  {
15
- apiUrl: "https://test.agentshield.com",
15
+ apiUrl: "https://test.checkpoint.example",
16
16
  apiKey: "test-api-key",
17
17
  projectId,
18
18
  cacheTtl: 300000,
@@ -1,7 +1,7 @@
1
1
  /**
2
- * AgentShield Integration Tests
2
+ * Checkpoint Integration Tests
3
3
  *
4
- * Tests for AgentShield API integration including:
4
+ * Tests for Checkpoint API integration including:
5
5
  * - Tool protection config fetching
6
6
  * - API authentication
7
7
  * - Error handling
@@ -47,7 +47,7 @@ function mockFetchWithTools(tools: Record<string, Partial<ToolProtection>>) {
47
47
  });
48
48
  }
49
49
 
50
- describe("AgentShield Integration", () => {
50
+ describe("Checkpoint Integration", () => {
51
51
  let service: ToolProtectionService;
52
52
  let cache: InMemoryToolProtectionCache;
53
53
  let config: ToolProtectionServiceConfig;