@kya-os/checkpoint-nextjs 1.0.1 → 1.1.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.
@@ -145,6 +145,13 @@ var EdgeRuntimeAgentShield = class {
145
145
  { pattern: /gemini/i, name: "Google Gemini", confidence: 0.85 },
146
146
  { pattern: /perplexity/i, name: "Perplexity", confidence: 0.85 },
147
147
  // Fallback
148
+ // UA-context-only pattern — `\b` is the correct anchor for UA
149
+ // substring matching. PR #2591's tightened `[\s;()]` form
150
+ // dropped `/`, which broke legit UA shapes like `you.com/1.0`
151
+ // and `+https://you.com)` (cursor catch on the same PR — see
152
+ // sibling agents.ts comment for the full rationale + UA shapes).
153
+ // CodeQL js/regex/missing-regexp-anchor speculates about URL
154
+ // misuse; this codebase only applies the pattern to UA strings.
148
155
  { pattern: /\byou\.com\b/i, name: "You.com", confidence: 0.8 },
149
156
  { pattern: /\bphind\b/i, name: "Phind", confidence: 0.8 }
150
157
  ];
@@ -143,6 +143,13 @@ var EdgeRuntimeAgentShield = class {
143
143
  { pattern: /gemini/i, name: "Google Gemini", confidence: 0.85 },
144
144
  { pattern: /perplexity/i, name: "Perplexity", confidence: 0.85 },
145
145
  // Fallback
146
+ // UA-context-only pattern — `\b` is the correct anchor for UA
147
+ // substring matching. PR #2591's tightened `[\s;()]` form
148
+ // dropped `/`, which broke legit UA shapes like `you.com/1.0`
149
+ // and `+https://you.com)` (cursor catch on the same PR — see
150
+ // sibling agents.ts comment for the full rationale + UA shapes).
151
+ // CodeQL js/regex/missing-regexp-anchor speculates about URL
152
+ // misuse; this codebase only applies the pattern to UA strings.
146
153
  { pattern: /\byou\.com\b/i, name: "You.com", confidence: 0.8 },
147
154
  { pattern: /\bphind\b/i, name: "Phind", confidence: 0.8 }
148
155
  ];
package/dist/index.js CHANGED
@@ -56,21 +56,28 @@ function applyHeaders(res, headers) {
56
56
  }
57
57
 
58
58
  // src/translate.ts
59
- function nextRequestToHttpLike(req) {
59
+ async function nextRequestToHttpLike(req, opts = {}) {
60
60
  const url = new URL(req.url);
61
+ const body = await tryDrainJsonBody(req, opts);
61
62
  return {
62
63
  method: req.method,
63
64
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
64
65
  url: url.pathname + url.search,
65
66
  headers: headersToRecord(req.headers),
66
- // NextRequest.body is a ReadableStream; we don't drain it here.
67
- // The orchestrator routes to PlainHttp when body is falsy, which
68
- // is the right call for streaming middlewares that don't want to
69
- // buffer the request body just to detect agents.
70
- body: null,
67
+ body,
71
68
  remoteAddress: extractRemoteAddress(req)
72
69
  };
73
70
  }
71
+ async function tryDrainJsonBody(req, opts) {
72
+ if (opts.drainJsonBody === false) return null;
73
+ const contentType = req.headers.get("content-type") ?? "";
74
+ if (!contentType.toLowerCase().includes("application/json")) return null;
75
+ try {
76
+ return await req.clone().text();
77
+ } catch {
78
+ return null;
79
+ }
80
+ }
74
81
  function headersToRecord(headers) {
75
82
  const out = {};
76
83
  headers.forEach((value, key) => {
@@ -91,8 +98,9 @@ function extractRemoteAddress(req) {
91
98
  // src/middleware-node.ts
92
99
  function withCheckpoint(config) {
93
100
  const opts = buildVerifyOpts(config);
101
+ const translateOpts = { drainJsonBody: config.drainJsonBody };
94
102
  return async function checkpointMiddleware(req) {
95
- const httpLike = nextRequestToHttpLike(req);
103
+ const httpLike = await nextRequestToHttpLike(req, translateOpts);
96
104
  const result = await orchestrator.verifyRequest(httpLike, opts);
97
105
  await dispatchOnResult(config, result, req);
98
106
  const rendered = orchestrator.renderDecisionAsResponse(result);
@@ -110,7 +118,8 @@ function buildVerifyOpts(config) {
110
118
  tenantHost: config.tenantHost,
111
119
  enforcementMode: config.enforcementMode ?? "enforce",
112
120
  reputationBaseline: config.reputationBaseline,
113
- argusUrl: config.argusUrl
121
+ argusUrl: config.argusUrl,
122
+ legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
114
123
  };
115
124
  }
116
125
  async function dispatchOnResult(config, result, req) {
@@ -973,7 +982,13 @@ async function applyPolicy(request, detection, config) {
973
982
  const context = createContextFromDetection(detection, request);
974
983
  const decision = checkpointShared.evaluatePolicy(policy, context);
975
984
  if (config.onPolicyDecision) {
976
- await config.onPolicyDecision(request, decision, context);
985
+ try {
986
+ await config.onPolicyDecision(request, decision, context);
987
+ } catch (error) {
988
+ if (config.debug) {
989
+ console.error("[Checkpoint] onPolicyDecision callback failed:", error);
990
+ }
991
+ }
977
992
  }
978
993
  return await handlePolicyDecision(request, decision, config, detection);
979
994
  } catch (error) {
package/dist/index.mjs CHANGED
@@ -55,21 +55,28 @@ function applyHeaders(res, headers) {
55
55
  }
56
56
 
57
57
  // src/translate.ts
58
- function nextRequestToHttpLike(req) {
58
+ async function nextRequestToHttpLike(req, opts = {}) {
59
59
  const url = new URL(req.url);
60
+ const body = await tryDrainJsonBody(req, opts);
60
61
  return {
61
62
  method: req.method,
62
63
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
63
64
  url: url.pathname + url.search,
64
65
  headers: headersToRecord(req.headers),
65
- // NextRequest.body is a ReadableStream; we don't drain it here.
66
- // The orchestrator routes to PlainHttp when body is falsy, which
67
- // is the right call for streaming middlewares that don't want to
68
- // buffer the request body just to detect agents.
69
- body: null,
66
+ body,
70
67
  remoteAddress: extractRemoteAddress(req)
71
68
  };
72
69
  }
70
+ async function tryDrainJsonBody(req, opts) {
71
+ if (opts.drainJsonBody === false) return null;
72
+ const contentType = req.headers.get("content-type") ?? "";
73
+ if (!contentType.toLowerCase().includes("application/json")) return null;
74
+ try {
75
+ return await req.clone().text();
76
+ } catch {
77
+ return null;
78
+ }
79
+ }
73
80
  function headersToRecord(headers) {
74
81
  const out = {};
75
82
  headers.forEach((value, key) => {
@@ -90,8 +97,9 @@ function extractRemoteAddress(req) {
90
97
  // src/middleware-node.ts
91
98
  function withCheckpoint(config) {
92
99
  const opts = buildVerifyOpts(config);
100
+ const translateOpts = { drainJsonBody: config.drainJsonBody };
93
101
  return async function checkpointMiddleware(req) {
94
- const httpLike = nextRequestToHttpLike(req);
102
+ const httpLike = await nextRequestToHttpLike(req, translateOpts);
95
103
  const result = await verifyRequest(httpLike, opts);
96
104
  await dispatchOnResult(config, result, req);
97
105
  const rendered = renderDecisionAsResponse(result);
@@ -109,7 +117,8 @@ function buildVerifyOpts(config) {
109
117
  tenantHost: config.tenantHost,
110
118
  enforcementMode: config.enforcementMode ?? "enforce",
111
119
  reputationBaseline: config.reputationBaseline,
112
- argusUrl: config.argusUrl
120
+ argusUrl: config.argusUrl,
121
+ legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
113
122
  };
114
123
  }
115
124
  async function dispatchOnResult(config, result, req) {
@@ -972,7 +981,13 @@ async function applyPolicy(request, detection, config) {
972
981
  const context = createContextFromDetection(detection, request);
973
982
  const decision = evaluatePolicy(policy, context);
974
983
  if (config.onPolicyDecision) {
975
- await config.onPolicyDecision(request, decision, context);
984
+ try {
985
+ await config.onPolicyDecision(request, decision, context);
986
+ } catch (error) {
987
+ if (config.debug) {
988
+ console.error("[Checkpoint] onPolicyDecision callback failed:", error);
989
+ }
990
+ }
976
991
  }
977
992
  return await handlePolicyDecision(request, decision, config, detection);
978
993
  } catch (error) {
@@ -57,21 +57,28 @@ function applyHeaders(res, headers) {
57
57
  }
58
58
 
59
59
  // src/translate.ts
60
- function nextRequestToHttpLike(req) {
60
+ async function nextRequestToHttpLike(req, opts = {}) {
61
61
  const url = new URL(req.url);
62
+ const body = await tryDrainJsonBody(req, opts);
62
63
  return {
63
64
  method: req.method,
64
65
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
65
66
  url: url.pathname + url.search,
66
67
  headers: headersToRecord(req.headers),
67
- // NextRequest.body is a ReadableStream; we don't drain it here.
68
- // The orchestrator routes to PlainHttp when body is falsy, which
69
- // is the right call for streaming middlewares that don't want to
70
- // buffer the request body just to detect agents.
71
- body: null,
68
+ body,
72
69
  remoteAddress: extractRemoteAddress(req)
73
70
  };
74
71
  }
72
+ async function tryDrainJsonBody(req, opts) {
73
+ if (opts.drainJsonBody === false) return null;
74
+ const contentType = req.headers.get("content-type") ?? "";
75
+ if (!contentType.toLowerCase().includes("application/json")) return null;
76
+ try {
77
+ return await req.clone().text();
78
+ } catch {
79
+ return null;
80
+ }
81
+ }
75
82
  function headersToRecord(headers) {
76
83
  const out = {};
77
84
  headers.forEach((value, key) => {
@@ -101,7 +108,8 @@ function buildVerifyOpts(config) {
101
108
  tenantHost: config.tenantHost,
102
109
  enforcementMode: config.enforcementMode ?? "enforce",
103
110
  reputationBaseline: config.reputationBaseline,
104
- argusUrl: config.argusUrl
111
+ argusUrl: config.argusUrl,
112
+ legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
105
113
  };
106
114
  }
107
115
 
@@ -109,8 +117,9 @@ function buildVerifyOpts(config) {
109
117
  function withCheckpoint(config) {
110
118
  void edge.initEngineEdge();
111
119
  const opts = buildVerifyOpts(config);
120
+ const translateOpts = { drainJsonBody: config.drainJsonBody };
112
121
  return async function checkpointMiddlewareEdge(req) {
113
- const httpLike = nextRequestToHttpLike(req);
122
+ const httpLike = await nextRequestToHttpLike(req, translateOpts);
114
123
  const result = await edge.verifyRequestEdge(httpLike, opts);
115
124
  await dispatchOnResult(config, result, req);
116
125
  const rendered = edge.renderDecisionAsResponse(result);
@@ -56,21 +56,28 @@ function applyHeaders(res, headers) {
56
56
  }
57
57
 
58
58
  // src/translate.ts
59
- function nextRequestToHttpLike(req) {
59
+ async function nextRequestToHttpLike(req, opts = {}) {
60
60
  const url = new URL(req.url);
61
+ const body = await tryDrainJsonBody(req, opts);
61
62
  return {
62
63
  method: req.method,
63
64
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
64
65
  url: url.pathname + url.search,
65
66
  headers: headersToRecord(req.headers),
66
- // NextRequest.body is a ReadableStream; we don't drain it here.
67
- // The orchestrator routes to PlainHttp when body is falsy, which
68
- // is the right call for streaming middlewares that don't want to
69
- // buffer the request body just to detect agents.
70
- body: null,
67
+ body,
71
68
  remoteAddress: extractRemoteAddress(req)
72
69
  };
73
70
  }
71
+ async function tryDrainJsonBody(req, opts) {
72
+ if (opts.drainJsonBody === false) return null;
73
+ const contentType = req.headers.get("content-type") ?? "";
74
+ if (!contentType.toLowerCase().includes("application/json")) return null;
75
+ try {
76
+ return await req.clone().text();
77
+ } catch {
78
+ return null;
79
+ }
80
+ }
74
81
  function headersToRecord(headers) {
75
82
  const out = {};
76
83
  headers.forEach((value, key) => {
@@ -100,7 +107,8 @@ function buildVerifyOpts(config) {
100
107
  tenantHost: config.tenantHost,
101
108
  enforcementMode: config.enforcementMode ?? "enforce",
102
109
  reputationBaseline: config.reputationBaseline,
103
- argusUrl: config.argusUrl
110
+ argusUrl: config.argusUrl,
111
+ legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
104
112
  };
105
113
  }
106
114
 
@@ -108,8 +116,9 @@ function buildVerifyOpts(config) {
108
116
  function withCheckpoint(config) {
109
117
  void initEngineEdge();
110
118
  const opts = buildVerifyOpts(config);
119
+ const translateOpts = { drainJsonBody: config.drainJsonBody };
111
120
  return async function checkpointMiddlewareEdge(req) {
112
- const httpLike = nextRequestToHttpLike(req);
121
+ const httpLike = await nextRequestToHttpLike(req, translateOpts);
113
122
  const result = await verifyRequestEdge(httpLike, opts);
114
123
  await dispatchOnResult(config, result, req);
115
124
  const rendered = renderDecisionAsResponse(result);
@@ -58,6 +58,41 @@ interface CheckpointConfig {
58
58
  * response.
59
59
  */
60
60
  onResult?: (result: VerifyResult, req: NextRequest) => void | Promise<void>;
61
+ /**
62
+ * Accept legacy `KYA-Delegation`-header envelope form alongside the
63
+ * canonical `_meta.proof.jws` body form. Default `false`.
64
+ *
65
+ * **When to enable** — customers whose agents pre-date Envelope-1
66
+ * (#2537) and ship MCP-I proofs as `{protected,payload,signature}`
67
+ * JSON in a `KYA-Delegation` HTTP header. Post-Envelope-1 agents
68
+ * ship compact JWS in the request body's `_meta.proof.jws` field;
69
+ * those don't need this flag.
70
+ *
71
+ * Forwarded to the orchestrator's `VerifyRequestOpts.legacyEnvelopeFallback`.
72
+ * Both transports (header + body) are honored when this is `true`;
73
+ * the orchestrator's detection order is body first, then header
74
+ * (`packages/checkpoint-wasm-runtime/src/engine/orchestrator/build-agent-request.ts`).
75
+ *
76
+ * SDK-Envelope-Plumbing-1 (#2594). Added in `@kya-os/checkpoint-nextjs@1.1.0`.
77
+ */
78
+ legacyEnvelopeFallback?: boolean;
79
+ /**
80
+ * Read the request body when `content-type` is `application/json` so
81
+ * the orchestrator can extract an MCP-I envelope from
82
+ * `_meta.proof.jws`. Default `true`.
83
+ *
84
+ * **When to disable** — streaming middlewares that can't tolerate
85
+ * the `req.clone()` memory overhead (one full-body copy is buffered
86
+ * during the read). For those, set `false` and route MCP-I
87
+ * envelopes through the `KYA-Delegation` header transport instead
88
+ * (requires `legacyEnvelopeFallback: true`).
89
+ *
90
+ * The clone preserves `req.body` for downstream handlers — disabling
91
+ * is a performance optimization, not a correctness fix.
92
+ *
93
+ * SDK-Envelope-Plumbing-1 (#2594). Added in `@kya-os/checkpoint-nextjs@1.1.0`.
94
+ */
95
+ drainJsonBody?: boolean;
61
96
  }
62
97
  /**
63
98
  * Build the Checkpoint middleware. Returns a function `(req) => NextResponse`
@@ -84,6 +119,7 @@ declare function buildVerifyOpts(config: CheckpointConfig): {
84
119
  enforcementMode: EnforcementMode;
85
120
  reputationBaseline: number | undefined;
86
121
  argusUrl: string | undefined;
122
+ legacyEnvelopeFallback: boolean;
87
123
  };
88
124
 
89
125
  export { type CheckpointConfig, buildVerifyOpts as _buildVerifyOpts, withCheckpoint };
@@ -58,6 +58,41 @@ interface CheckpointConfig {
58
58
  * response.
59
59
  */
60
60
  onResult?: (result: VerifyResult, req: NextRequest) => void | Promise<void>;
61
+ /**
62
+ * Accept legacy `KYA-Delegation`-header envelope form alongside the
63
+ * canonical `_meta.proof.jws` body form. Default `false`.
64
+ *
65
+ * **When to enable** — customers whose agents pre-date Envelope-1
66
+ * (#2537) and ship MCP-I proofs as `{protected,payload,signature}`
67
+ * JSON in a `KYA-Delegation` HTTP header. Post-Envelope-1 agents
68
+ * ship compact JWS in the request body's `_meta.proof.jws` field;
69
+ * those don't need this flag.
70
+ *
71
+ * Forwarded to the orchestrator's `VerifyRequestOpts.legacyEnvelopeFallback`.
72
+ * Both transports (header + body) are honored when this is `true`;
73
+ * the orchestrator's detection order is body first, then header
74
+ * (`packages/checkpoint-wasm-runtime/src/engine/orchestrator/build-agent-request.ts`).
75
+ *
76
+ * SDK-Envelope-Plumbing-1 (#2594). Added in `@kya-os/checkpoint-nextjs@1.1.0`.
77
+ */
78
+ legacyEnvelopeFallback?: boolean;
79
+ /**
80
+ * Read the request body when `content-type` is `application/json` so
81
+ * the orchestrator can extract an MCP-I envelope from
82
+ * `_meta.proof.jws`. Default `true`.
83
+ *
84
+ * **When to disable** — streaming middlewares that can't tolerate
85
+ * the `req.clone()` memory overhead (one full-body copy is buffered
86
+ * during the read). For those, set `false` and route MCP-I
87
+ * envelopes through the `KYA-Delegation` header transport instead
88
+ * (requires `legacyEnvelopeFallback: true`).
89
+ *
90
+ * The clone preserves `req.body` for downstream handlers — disabling
91
+ * is a performance optimization, not a correctness fix.
92
+ *
93
+ * SDK-Envelope-Plumbing-1 (#2594). Added in `@kya-os/checkpoint-nextjs@1.1.0`.
94
+ */
95
+ drainJsonBody?: boolean;
61
96
  }
62
97
  /**
63
98
  * Build the Checkpoint middleware. Returns a function `(req) => NextResponse`
@@ -84,6 +119,7 @@ declare function buildVerifyOpts(config: CheckpointConfig): {
84
119
  enforcementMode: EnforcementMode;
85
120
  reputationBaseline: number | undefined;
86
121
  argusUrl: string | undefined;
122
+ legacyEnvelopeFallback: boolean;
87
123
  };
88
124
 
89
125
  export { type CheckpointConfig, buildVerifyOpts as _buildVerifyOpts, withCheckpoint };
@@ -56,21 +56,28 @@ function applyHeaders(res, headers) {
56
56
  }
57
57
 
58
58
  // src/translate.ts
59
- function nextRequestToHttpLike(req) {
59
+ async function nextRequestToHttpLike(req, opts = {}) {
60
60
  const url = new URL(req.url);
61
+ const body = await tryDrainJsonBody(req, opts);
61
62
  return {
62
63
  method: req.method,
63
64
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
64
65
  url: url.pathname + url.search,
65
66
  headers: headersToRecord(req.headers),
66
- // NextRequest.body is a ReadableStream; we don't drain it here.
67
- // The orchestrator routes to PlainHttp when body is falsy, which
68
- // is the right call for streaming middlewares that don't want to
69
- // buffer the request body just to detect agents.
70
- body: null,
67
+ body,
71
68
  remoteAddress: extractRemoteAddress(req)
72
69
  };
73
70
  }
71
+ async function tryDrainJsonBody(req, opts) {
72
+ if (opts.drainJsonBody === false) return null;
73
+ const contentType = req.headers.get("content-type") ?? "";
74
+ if (!contentType.toLowerCase().includes("application/json")) return null;
75
+ try {
76
+ return await req.clone().text();
77
+ } catch {
78
+ return null;
79
+ }
80
+ }
74
81
  function headersToRecord(headers) {
75
82
  const out = {};
76
83
  headers.forEach((value, key) => {
@@ -91,8 +98,9 @@ function extractRemoteAddress(req) {
91
98
  // src/middleware-node.ts
92
99
  function withCheckpoint(config) {
93
100
  const opts = buildVerifyOpts(config);
101
+ const translateOpts = { drainJsonBody: config.drainJsonBody };
94
102
  return async function checkpointMiddleware(req) {
95
- const httpLike = nextRequestToHttpLike(req);
103
+ const httpLike = await nextRequestToHttpLike(req, translateOpts);
96
104
  const result = await orchestrator.verifyRequest(httpLike, opts);
97
105
  await dispatchOnResult(config, result, req);
98
106
  const rendered = orchestrator.renderDecisionAsResponse(result);
@@ -110,7 +118,8 @@ function buildVerifyOpts(config) {
110
118
  tenantHost: config.tenantHost,
111
119
  enforcementMode: config.enforcementMode ?? "enforce",
112
120
  reputationBaseline: config.reputationBaseline,
113
- argusUrl: config.argusUrl
121
+ argusUrl: config.argusUrl,
122
+ legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
114
123
  };
115
124
  }
116
125
  async function dispatchOnResult(config, result, req) {
@@ -54,21 +54,28 @@ function applyHeaders(res, headers) {
54
54
  }
55
55
 
56
56
  // src/translate.ts
57
- function nextRequestToHttpLike(req) {
57
+ async function nextRequestToHttpLike(req, opts = {}) {
58
58
  const url = new URL(req.url);
59
+ const body = await tryDrainJsonBody(req, opts);
59
60
  return {
60
61
  method: req.method,
61
62
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
62
63
  url: url.pathname + url.search,
63
64
  headers: headersToRecord(req.headers),
64
- // NextRequest.body is a ReadableStream; we don't drain it here.
65
- // The orchestrator routes to PlainHttp when body is falsy, which
66
- // is the right call for streaming middlewares that don't want to
67
- // buffer the request body just to detect agents.
68
- body: null,
65
+ body,
69
66
  remoteAddress: extractRemoteAddress(req)
70
67
  };
71
68
  }
69
+ async function tryDrainJsonBody(req, opts) {
70
+ if (opts.drainJsonBody === false) return null;
71
+ const contentType = req.headers.get("content-type") ?? "";
72
+ if (!contentType.toLowerCase().includes("application/json")) return null;
73
+ try {
74
+ return await req.clone().text();
75
+ } catch {
76
+ return null;
77
+ }
78
+ }
72
79
  function headersToRecord(headers) {
73
80
  const out = {};
74
81
  headers.forEach((value, key) => {
@@ -89,8 +96,9 @@ function extractRemoteAddress(req) {
89
96
  // src/middleware-node.ts
90
97
  function withCheckpoint(config) {
91
98
  const opts = buildVerifyOpts(config);
99
+ const translateOpts = { drainJsonBody: config.drainJsonBody };
92
100
  return async function checkpointMiddleware(req) {
93
- const httpLike = nextRequestToHttpLike(req);
101
+ const httpLike = await nextRequestToHttpLike(req, translateOpts);
94
102
  const result = await verifyRequest(httpLike, opts);
95
103
  await dispatchOnResult(config, result, req);
96
104
  const rendered = renderDecisionAsResponse(result);
@@ -108,7 +116,8 @@ function buildVerifyOpts(config) {
108
116
  tenantHost: config.tenantHost,
109
117
  enforcementMode: config.enforcementMode ?? "enforce",
110
118
  reputationBaseline: config.reputationBaseline,
111
- argusUrl: config.argusUrl
119
+ argusUrl: config.argusUrl,
120
+ legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
112
121
  };
113
122
  }
114
123
  async function dispatchOnResult(config, result, req) {
package/dist/policy.js CHANGED
@@ -144,7 +144,13 @@ async function applyPolicy(request, detection, config) {
144
144
  const context = createContextFromDetection(detection, request);
145
145
  const decision = checkpointShared.evaluatePolicy(policy, context);
146
146
  if (config.onPolicyDecision) {
147
- await config.onPolicyDecision(request, decision, context);
147
+ try {
148
+ await config.onPolicyDecision(request, decision, context);
149
+ } catch (error) {
150
+ if (config.debug) {
151
+ console.error("[Checkpoint] onPolicyDecision callback failed:", error);
152
+ }
153
+ }
148
154
  }
149
155
  return await handlePolicyDecision(request, decision, config, detection);
150
156
  } catch (error) {
package/dist/policy.mjs CHANGED
@@ -143,7 +143,13 @@ async function applyPolicy(request, detection, config) {
143
143
  const context = createContextFromDetection(detection, request);
144
144
  const decision = evaluatePolicy(policy, context);
145
145
  if (config.onPolicyDecision) {
146
- await config.onPolicyDecision(request, decision, context);
146
+ try {
147
+ await config.onPolicyDecision(request, decision, context);
148
+ } catch (error) {
149
+ if (config.debug) {
150
+ console.error("[Checkpoint] onPolicyDecision callback failed:", error);
151
+ }
152
+ }
147
153
  }
148
154
  return await handlePolicyDecision(request, decision, config, detection);
149
155
  } catch (error) {
@@ -16,18 +16,45 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
16
16
  * `x-forwarded-for` first IP).
17
17
  */
18
18
 
19
+ /**
20
+ * Options controlling translator side effects.
21
+ *
22
+ * `drainJsonBody` (default `true`) controls whether the translator
23
+ * reads the request body so the orchestrator can extract an MCP-I
24
+ * envelope from `_meta.proof.jws` (the canonical spec-form transport).
25
+ *
26
+ * Trade-off — body draining consumes the request stream. We use
27
+ * `req.clone().text()` so the original `req.body` stream is preserved
28
+ * for downstream Next.js handlers. Cloning has memory overhead
29
+ * proportional to the body size; for large request bodies (>1 MB) or
30
+ * streaming-sensitive routes, set `drainJsonBody: false` and verify
31
+ * envelopes via the `KYA-Delegation` header transport instead (the
32
+ * legacy form, requires `legacyEnvelopeFallback: true` on
33
+ * `withCheckpoint`).
34
+ */
35
+ interface NextRequestToHttpLikeOpts {
36
+ /**
37
+ * Read the request body when `content-type` is `application/json` so
38
+ * the orchestrator can extract an MCP-I envelope from
39
+ * `_meta.proof.jws`. Default `true`. Set `false` for streaming
40
+ * middlewares that can't tolerate the clone/read overhead.
41
+ */
42
+ drainJsonBody?: boolean;
43
+ }
19
44
  /**
20
45
  * Translate a Next.js `NextRequest` into the orchestrator's
21
46
  * `IncomingHttpLike` shape.
22
47
  *
23
- * The body is passed through as-is the orchestrator's
24
- * `buildAgentRequest` decides whether to parse JSON (looking for an
25
- * MCP-I `_meta.proof.jws` envelope) or treat the request as PlainHttp.
26
- * On Next.js middleware the body is typically not pre-parsed; consumers
27
- * who want to inspect the body for routing decisions should `await
28
- * req.json()` themselves and pass the parsed result via a second
29
- * `verifyRequest` call (not common).
48
+ * When `opts.drainJsonBody !== false` and the request is JSON, the
49
+ * translator clones the request and reads the body into a string so
50
+ * the orchestrator's `buildAgentRequest` can extract a
51
+ * `_meta.proof.jws` envelope. The original `req.body` stream is
52
+ * preserved for downstream handlers via `req.clone()`.
53
+ *
54
+ * When the body can't be read (drain disabled, non-JSON, or a stream
55
+ * error) we pass `body: null` and the orchestrator routes to
56
+ * PlainHttp — same behavior as the pre-1.1.0 default.
30
57
  */
31
- declare function nextRequestToHttpLike(req: NextRequest): IncomingHttpLike;
58
+ declare function nextRequestToHttpLike(req: NextRequest, opts?: NextRequestToHttpLikeOpts): Promise<IncomingHttpLike>;
32
59
 
33
- export { nextRequestToHttpLike };
60
+ export { type NextRequestToHttpLikeOpts, nextRequestToHttpLike };
@@ -16,18 +16,45 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
16
16
  * `x-forwarded-for` first IP).
17
17
  */
18
18
 
19
+ /**
20
+ * Options controlling translator side effects.
21
+ *
22
+ * `drainJsonBody` (default `true`) controls whether the translator
23
+ * reads the request body so the orchestrator can extract an MCP-I
24
+ * envelope from `_meta.proof.jws` (the canonical spec-form transport).
25
+ *
26
+ * Trade-off — body draining consumes the request stream. We use
27
+ * `req.clone().text()` so the original `req.body` stream is preserved
28
+ * for downstream Next.js handlers. Cloning has memory overhead
29
+ * proportional to the body size; for large request bodies (>1 MB) or
30
+ * streaming-sensitive routes, set `drainJsonBody: false` and verify
31
+ * envelopes via the `KYA-Delegation` header transport instead (the
32
+ * legacy form, requires `legacyEnvelopeFallback: true` on
33
+ * `withCheckpoint`).
34
+ */
35
+ interface NextRequestToHttpLikeOpts {
36
+ /**
37
+ * Read the request body when `content-type` is `application/json` so
38
+ * the orchestrator can extract an MCP-I envelope from
39
+ * `_meta.proof.jws`. Default `true`. Set `false` for streaming
40
+ * middlewares that can't tolerate the clone/read overhead.
41
+ */
42
+ drainJsonBody?: boolean;
43
+ }
19
44
  /**
20
45
  * Translate a Next.js `NextRequest` into the orchestrator's
21
46
  * `IncomingHttpLike` shape.
22
47
  *
23
- * The body is passed through as-is the orchestrator's
24
- * `buildAgentRequest` decides whether to parse JSON (looking for an
25
- * MCP-I `_meta.proof.jws` envelope) or treat the request as PlainHttp.
26
- * On Next.js middleware the body is typically not pre-parsed; consumers
27
- * who want to inspect the body for routing decisions should `await
28
- * req.json()` themselves and pass the parsed result via a second
29
- * `verifyRequest` call (not common).
48
+ * When `opts.drainJsonBody !== false` and the request is JSON, the
49
+ * translator clones the request and reads the body into a string so
50
+ * the orchestrator's `buildAgentRequest` can extract a
51
+ * `_meta.proof.jws` envelope. The original `req.body` stream is
52
+ * preserved for downstream handlers via `req.clone()`.
53
+ *
54
+ * When the body can't be read (drain disabled, non-JSON, or a stream
55
+ * error) we pass `body: null` and the orchestrator routes to
56
+ * PlainHttp — same behavior as the pre-1.1.0 default.
30
57
  */
31
- declare function nextRequestToHttpLike(req: NextRequest): IncomingHttpLike;
58
+ declare function nextRequestToHttpLike(req: NextRequest, opts?: NextRequestToHttpLikeOpts): Promise<IncomingHttpLike>;
32
59
 
33
- export { nextRequestToHttpLike };
60
+ export { type NextRequestToHttpLikeOpts, nextRequestToHttpLike };