@proofoftech/flowsafe 0.20.0 → 0.22.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 (238) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.md +55 -10
  3. package/deployment-identity-protocol.d.mts +28 -3
  4. package/deployment-identity-protocol.mjs +275 -21
  5. package/dist/agent-host/router.d.ts.map +1 -1
  6. package/dist/agent-host/router.js +11 -2
  7. package/dist/agent-host/router.js.map +1 -1
  8. package/dist/agent-host/thread-host.d.ts +3 -1
  9. package/dist/agent-host/thread-host.d.ts.map +1 -1
  10. package/dist/agent-host/thread-host.js +858 -351
  11. package/dist/agent-host/thread-host.js.map +1 -1
  12. package/dist/agent-host/thread-topology.d.ts +8 -0
  13. package/dist/agent-host/thread-topology.d.ts.map +1 -1
  14. package/dist/agent-host/thread-topology.js +243 -64
  15. package/dist/agent-host/thread-topology.js.map +1 -1
  16. package/dist/agent-runner/durable-agent-runner.d.ts +89 -14
  17. package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
  18. package/dist/agent-runner/durable-agent-runner.js +369 -44
  19. package/dist/agent-runner/durable-agent-runner.js.map +1 -1
  20. package/dist/agent-runner/index.d.ts +1 -1
  21. package/dist/agent-runner/index.d.ts.map +1 -1
  22. package/dist/agent-runner/index.js.map +1 -1
  23. package/dist/approval-api/actor-context.d.ts +4 -0
  24. package/dist/approval-api/actor-context.d.ts.map +1 -1
  25. package/dist/approval-api/actor-context.js +53 -5
  26. package/dist/approval-api/actor-context.js.map +1 -1
  27. package/dist/approval-api/principal.d.ts +1 -1
  28. package/dist/approval-api/principal.js +1 -1
  29. package/dist/approval-api/retention.d.ts +1 -2
  30. package/dist/approval-api/retention.d.ts.map +1 -1
  31. package/dist/approval-api/retention.js.map +1 -1
  32. package/dist/approval-api/service.d.ts +13 -20
  33. package/dist/approval-api/service.d.ts.map +1 -1
  34. package/dist/approval-api/service.js +52 -28
  35. package/dist/approval-api/service.js.map +1 -1
  36. package/dist/audit-export/index.d.ts +2 -2
  37. package/dist/audit-export/index.js +2 -2
  38. package/dist/background-tasks/d1-storage.d.ts +4 -3
  39. package/dist/background-tasks/d1-storage.d.ts.map +1 -1
  40. package/dist/background-tasks/d1-storage.js +8 -14
  41. package/dist/background-tasks/d1-storage.js.map +1 -1
  42. package/dist/background-tasks/host.d.ts.map +1 -1
  43. package/dist/background-tasks/host.js +3 -2
  44. package/dist/background-tasks/host.js.map +1 -1
  45. package/dist/do-runner/constants.d.ts +3 -0
  46. package/dist/do-runner/constants.d.ts.map +1 -0
  47. package/dist/do-runner/constants.js +3 -0
  48. package/dist/do-runner/constants.js.map +1 -0
  49. package/dist/do-runner/d1-storage.d.ts +34 -158
  50. package/dist/do-runner/d1-storage.d.ts.map +1 -1
  51. package/dist/do-runner/d1-storage.js +935 -402
  52. package/dist/do-runner/d1-storage.js.map +1 -1
  53. package/dist/do-runner/durable-object.d.ts.map +1 -1
  54. package/dist/do-runner/durable-object.js +472 -185
  55. package/dist/do-runner/durable-object.js.map +1 -1
  56. package/dist/do-runner/execution-admission.d.ts +114 -0
  57. package/dist/do-runner/execution-admission.d.ts.map +1 -0
  58. package/dist/do-runner/execution-admission.js +206 -0
  59. package/dist/do-runner/execution-admission.js.map +1 -0
  60. package/dist/do-runner/execution-context.d.ts +5 -1
  61. package/dist/do-runner/execution-context.d.ts.map +1 -1
  62. package/dist/do-runner/execution-context.js +6 -1
  63. package/dist/do-runner/execution-context.js.map +1 -1
  64. package/dist/do-runner/execution-fence.d.ts +74 -30
  65. package/dist/do-runner/execution-fence.d.ts.map +1 -1
  66. package/dist/do-runner/execution-fence.js +684 -120
  67. package/dist/do-runner/execution-fence.js.map +1 -1
  68. package/dist/do-runner/fenced-workflow-capability.d.ts +64 -0
  69. package/dist/do-runner/fenced-workflow-capability.d.ts.map +1 -0
  70. package/dist/do-runner/fenced-workflow-capability.js +3 -0
  71. package/dist/do-runner/fenced-workflow-capability.js.map +1 -0
  72. package/dist/do-runner/fenced-workflows-d1.d.ts +15 -0
  73. package/dist/do-runner/fenced-workflows-d1.d.ts.map +1 -0
  74. package/dist/do-runner/fenced-workflows-d1.js +894 -0
  75. package/dist/do-runner/fenced-workflows-d1.js.map +1 -0
  76. package/dist/do-runner/index.d.ts +13 -8
  77. package/dist/do-runner/index.d.ts.map +1 -1
  78. package/dist/do-runner/index.js +6 -2
  79. package/dist/do-runner/index.js.map +1 -1
  80. package/dist/do-runner/initial-admission-refusal.d.ts +7 -0
  81. package/dist/do-runner/initial-admission-refusal.d.ts.map +1 -0
  82. package/dist/do-runner/initial-admission-refusal.js +41 -0
  83. package/dist/do-runner/initial-admission-refusal.js.map +1 -0
  84. package/dist/do-runner/inventory.d.ts +2 -1
  85. package/dist/do-runner/inventory.d.ts.map +1 -1
  86. package/dist/do-runner/inventory.js +28 -39
  87. package/dist/do-runner/inventory.js.map +1 -1
  88. package/dist/do-runner/notification-predicate.d.ts +7 -7
  89. package/dist/do-runner/notification-predicate.d.ts.map +1 -1
  90. package/dist/do-runner/notification-predicate.js +134 -7
  91. package/dist/do-runner/notification-predicate.js.map +1 -1
  92. package/dist/do-runner/run-lifecycle.d.ts +20 -1
  93. package/dist/do-runner/run-lifecycle.d.ts.map +1 -1
  94. package/dist/do-runner/run-lifecycle.js +69 -15
  95. package/dist/do-runner/run-lifecycle.js.map +1 -1
  96. package/dist/do-runner/run-provenance.d.ts +38 -0
  97. package/dist/do-runner/run-provenance.d.ts.map +1 -0
  98. package/dist/do-runner/run-provenance.js +152 -0
  99. package/dist/do-runner/run-provenance.js.map +1 -0
  100. package/dist/do-runner/run-storage-tables.d.ts +2 -0
  101. package/dist/do-runner/run-storage-tables.d.ts.map +1 -0
  102. package/dist/do-runner/run-storage-tables.js +4 -0
  103. package/dist/do-runner/run-storage-tables.js.map +1 -0
  104. package/dist/do-runner/run-terminal-state.d.ts +29 -0
  105. package/dist/do-runner/run-terminal-state.d.ts.map +1 -0
  106. package/dist/do-runner/run-terminal-state.js +105 -0
  107. package/dist/do-runner/run-terminal-state.js.map +1 -0
  108. package/dist/do-runner/runtime.d.ts +108 -46
  109. package/dist/do-runner/runtime.d.ts.map +1 -1
  110. package/dist/do-runner/runtime.js +927 -438
  111. package/dist/do-runner/runtime.js.map +1 -1
  112. package/dist/do-runner/start-idempotency.d.ts +35 -258
  113. package/dist/do-runner/start-idempotency.d.ts.map +1 -1
  114. package/dist/do-runner/start-idempotency.js +606 -498
  115. package/dist/do-runner/start-idempotency.js.map +1 -1
  116. package/dist/do-runner/start-reservation-contract.d.ts +103 -0
  117. package/dist/do-runner/start-reservation-contract.d.ts.map +1 -0
  118. package/dist/do-runner/start-reservation-contract.js +338 -0
  119. package/dist/do-runner/start-reservation-contract.js.map +1 -0
  120. package/dist/do-runner/suspension-deadline.d.ts +4 -11
  121. package/dist/do-runner/suspension-deadline.d.ts.map +1 -1
  122. package/dist/do-runner/suspension-deadline.js +8 -12
  123. package/dist/do-runner/suspension-deadline.js.map +1 -1
  124. package/dist/do-runner/table-prefix.d.ts.map +1 -1
  125. package/dist/do-runner/table-prefix.js +2 -1
  126. package/dist/do-runner/table-prefix.js.map +1 -1
  127. package/dist/do-runner/testing.d.ts +3 -0
  128. package/dist/do-runner/testing.d.ts.map +1 -0
  129. package/dist/do-runner/testing.js +3 -0
  130. package/dist/do-runner/testing.js.map +1 -0
  131. package/dist/do-runner/thread-do.d.ts +2 -1
  132. package/dist/do-runner/thread-do.d.ts.map +1 -1
  133. package/dist/do-runner/thread-do.js +14 -10
  134. package/dist/do-runner/thread-do.js.map +1 -1
  135. package/dist/do-runner/workflow-snapshot-row.d.ts +49 -0
  136. package/dist/do-runner/workflow-snapshot-row.d.ts.map +1 -0
  137. package/dist/do-runner/workflow-snapshot-row.js +97 -0
  138. package/dist/do-runner/workflow-snapshot-row.js.map +1 -0
  139. package/dist/goals/objective-routes.d.ts +1 -5
  140. package/dist/goals/objective-routes.d.ts.map +1 -1
  141. package/dist/goals/objective-routes.js +32 -53
  142. package/dist/goals/objective-routes.js.map +1 -1
  143. package/dist/host-kit/approval-bridge.d.ts +8 -8
  144. package/dist/host-kit/approval-bridge.d.ts.map +1 -1
  145. package/dist/host-kit/approval-bridge.js +18 -15
  146. package/dist/host-kit/approval-bridge.js.map +1 -1
  147. package/dist/host-kit/do-response.d.ts +16 -0
  148. package/dist/host-kit/do-response.d.ts.map +1 -1
  149. package/dist/host-kit/do-response.js +181 -0
  150. package/dist/host-kit/do-response.js.map +1 -1
  151. package/dist/host-kit/do-run-topology.d.ts +2 -14
  152. package/dist/host-kit/do-run-topology.d.ts.map +1 -1
  153. package/dist/host-kit/do-run-topology.js +31 -24
  154. package/dist/host-kit/do-run-topology.js.map +1 -1
  155. package/dist/host-kit/env-vars.d.ts.map +1 -1
  156. package/dist/host-kit/env-vars.js +5 -6
  157. package/dist/host-kit/env-vars.js.map +1 -1
  158. package/dist/host-kit/flowsafe-worker.d.ts +38 -6
  159. package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
  160. package/dist/host-kit/flowsafe-worker.js +115 -93
  161. package/dist/host-kit/flowsafe-worker.js.map +1 -1
  162. package/dist/host-kit/host-approval-service.d.ts +4 -14
  163. package/dist/host-kit/host-approval-service.d.ts.map +1 -1
  164. package/dist/host-kit/host-approval-service.js +23 -40
  165. package/dist/host-kit/host-approval-service.js.map +1 -1
  166. package/dist/host-kit/index.d.ts +7 -1
  167. package/dist/host-kit/index.d.ts.map +1 -1
  168. package/dist/host-kit/index.js +4 -0
  169. package/dist/host-kit/index.js.map +1 -1
  170. package/dist/host-kit/run-route-error.d.ts +3 -17
  171. package/dist/host-kit/run-route-error.d.ts.map +1 -1
  172. package/dist/host-kit/run-route-error.js +4 -24
  173. package/dist/host-kit/run-route-error.js.map +1 -1
  174. package/dist/host-kit/run-router.d.ts +6 -2
  175. package/dist/host-kit/run-router.d.ts.map +1 -1
  176. package/dist/host-kit/run-router.js +93 -80
  177. package/dist/host-kit/run-router.js.map +1 -1
  178. package/dist/host-kit/thread-topology.d.ts +1 -1
  179. package/dist/host-kit/thread-topology.d.ts.map +1 -1
  180. package/dist/host-kit/thread-topology.js +7 -3
  181. package/dist/host-kit/thread-topology.js.map +1 -1
  182. package/dist/internal-error-response.d.ts.map +1 -1
  183. package/dist/internal-error-response.js +16 -6
  184. package/dist/internal-error-response.js.map +1 -1
  185. package/dist/schedules/index.d.ts +1 -0
  186. package/dist/schedules/index.d.ts.map +1 -1
  187. package/dist/schedules/index.js +1 -7
  188. package/dist/schedules/index.js.map +1 -1
  189. package/dist/schedules/mutation-contract.d.ts +38 -0
  190. package/dist/schedules/mutation-contract.d.ts.map +1 -0
  191. package/dist/schedules/mutation-contract.js +21 -0
  192. package/dist/schedules/mutation-contract.js.map +1 -0
  193. package/dist/schedules/router.d.ts +11 -19
  194. package/dist/schedules/router.d.ts.map +1 -1
  195. package/dist/schedules/router.js +130 -49
  196. package/dist/schedules/router.js.map +1 -1
  197. package/dist/schedules/schedules-d1.d.ts +11 -5
  198. package/dist/schedules/schedules-d1.d.ts.map +1 -1
  199. package/dist/schedules/schedules-d1.js +571 -144
  200. package/dist/schedules/schedules-d1.js.map +1 -1
  201. package/dist/schedules/storage.d.ts +0 -7
  202. package/dist/schedules/storage.d.ts.map +1 -1
  203. package/dist/schedules/storage.js +0 -27
  204. package/dist/schedules/storage.js.map +1 -1
  205. package/dist/schedules/tick.d.ts +1 -8
  206. package/dist/schedules/tick.d.ts.map +1 -1
  207. package/dist/schedules/tick.js +15 -20
  208. package/dist/schedules/tick.js.map +1 -1
  209. package/dist/signal-providers/webhook-route.d.ts +1 -1
  210. package/dist/signal-providers/webhook-route.d.ts.map +1 -1
  211. package/dist/signal-providers/webhook-route.js +99 -109
  212. package/dist/signal-providers/webhook-route.js.map +1 -1
  213. package/dist/signals/d1-shared.d.ts +11 -8
  214. package/dist/signals/d1-shared.d.ts.map +1 -1
  215. package/dist/signals/d1-shared.js +2 -7
  216. package/dist/signals/d1-shared.js.map +1 -1
  217. package/dist/signals/index.d.ts +1 -1
  218. package/dist/signals/index.d.ts.map +1 -1
  219. package/dist/signals/index.js +1 -1
  220. package/dist/signals/index.js.map +1 -1
  221. package/dist/signals/notification-dispatch.d.ts +84 -4
  222. package/dist/signals/notification-dispatch.d.ts.map +1 -1
  223. package/dist/signals/notification-dispatch.js +365 -53
  224. package/dist/signals/notification-dispatch.js.map +1 -1
  225. package/dist/signals/notifications-d1.d.ts +6 -1
  226. package/dist/signals/notifications-d1.d.ts.map +1 -1
  227. package/dist/signals/notifications-d1.js +215 -10
  228. package/dist/signals/notifications-d1.js.map +1 -1
  229. package/dist/signals/router.d.ts +6 -4
  230. package/dist/signals/router.d.ts.map +1 -1
  231. package/dist/signals/router.js +49 -84
  232. package/dist/signals/router.js.map +1 -1
  233. package/dist/signals/thread-do-routes.d.ts +2 -0
  234. package/dist/signals/thread-do-routes.d.ts.map +1 -1
  235. package/dist/signals/thread-do-routes.js +250 -81
  236. package/dist/signals/thread-do-routes.js.map +1 -1
  237. package/package.json +9 -7
  238. package/scripts/seed-deployment-identity.mjs +27 -6
@@ -28,10 +28,11 @@ import { RequestContext } from '@mastra/core/request-context';
28
28
  import { FLOWSAFE_PERSISTENCE_FORBIDDEN } from '../agent-runner/durable-agent-runner.js';
29
29
  import { isRuntimeDrivenAgent, } from '../agent-runner/index.js';
30
30
  import { samePrincipal, } from '../approval-api/index.js';
31
- import { admitsExistingRun, DoStatusError, executionFencedResponse, isExecutionFenceRefusal, isPathSafeId, readExecutionFence, } from '../do-runner/index.js';
31
+ import { admitsExistingRun, DoStatusError, ExecutionFencedError, executionFencedResponse, isExecutionFenceRefusal, isPathSafeId, readExecutionFence, } from '../do-runner/index.js';
32
32
  import { internalErrorResponse } from '../internal-error-response.js';
33
+ import { positiveSafeInteger } from '../numeric-config.js';
33
34
  import { createScheduleAgentDispatchReceipt, } from '../schedules/schedules-d1.js';
34
- import { deferNotificationAfterFailure, MAX_NOTIFICATION_DISPATCH_IDS, planNotificationDispatch, } from './notification-dispatch.js';
35
+ import { captureNotificationDeliverySelection, captureNotificationDeliveryStorage, DEFAULT_MAX_NOTIFICATION_DELIVERY_ATTEMPTS, MAX_NOTIFICATION_DISPATCH_IDS, planNotificationDispatch, recordNotificationDeliveryFailure, reportNotificationDeliveryError, } from './notification-dispatch.js';
35
36
  /**
36
37
  * The idle-thread delivery behavior a send may ask for. `wake` starts a run
37
38
  * after consulting the run cap; `persist` writes the signal to the durable
@@ -190,6 +191,7 @@ function signalContentPolicyResponse(inspection) {
190
191
  : json({ error: 'signal content policy unavailable' }, 503);
191
192
  }
192
193
  export function createThreadSignalRoutes(options) {
194
+ const maxDeliveryAttempts = positiveSafeInteger(options.maxDeliveryAttempts ?? DEFAULT_MAX_NOTIFICATION_DELIVERY_ATTEMPTS, 'notification maximum delivery attempts');
193
195
  const { resolveAgent, resolveResourceId, consultRunCap, startIdleRun, resolveBlockingRun, serializeDispatch, resolveScheduleRunStatus, resolveScheduleTarget, canPersist, canPersistSchedule, resolveNotificationsStorage, resolveScheduleDispatchStore, contentPolicy, } = options;
194
196
  if (resolveBlockingRun && !serializeDispatch) {
195
197
  throw new Error('signal routes require serializeDispatch with resolveBlockingRun');
@@ -284,11 +286,43 @@ export function createThreadSignalRoutes(options) {
284
286
  // the lanes that never reach it (the persist routes, and a default
285
287
  // non-wake delivery) ungated; handleWake keeps its own check for the
286
288
  // wake path it owns.
289
+ let proof;
287
290
  if (executionFence.state === 'proof-only') {
288
- const activeRunId = activeThreadRunIdOf(agent, threadId, resourceId ?? '');
289
- if (!admitsExistingRun(executionFence, activeRunId)) {
290
- return executionFencedResponse(executionFence.state, entryPath);
291
- }
291
+ const runtime = scope.init.runtime;
292
+ const reader = agent.proofExecutionFor;
293
+ if (!runtimeDriven ||
294
+ typeof reader !== 'function' ||
295
+ runtime.executionFence !== scope.init.executionFence)
296
+ throw new ExecutionFencedError(executionFence.state, entryPath);
297
+ const capture = async (runId) => {
298
+ if (runId === undefined)
299
+ throw new ExecutionFencedError(executionFence.state, entryPath);
300
+ const execution = await Reflect.apply(reader, agent, [
301
+ runtime,
302
+ threadId,
303
+ runId,
304
+ ]);
305
+ if (execution === undefined ||
306
+ !admitsExistingRun(executionFence, execution))
307
+ throw new ExecutionFencedError(executionFence.state, entryPath);
308
+ return Object.freeze({ ...execution });
309
+ };
310
+ const admitted = await capture(activeThreadRunIdOf(agent, threadId, resourceId ?? ''));
311
+ const assertActive = (expected = admitted) => {
312
+ if (activeThreadRunIdOf(agent, threadId, resourceId ?? '') !==
313
+ expected.runId)
314
+ throw new ExecutionFencedError(executionFence.state, entryPath);
315
+ };
316
+ proof = {
317
+ capture,
318
+ async check(expected = admitted) {
319
+ const current = await capture(expected.runId);
320
+ if (!admitsExistingRun({ state: 'proof-only', proofExecution: expected }, current))
321
+ throw new ExecutionFencedError(executionFence.state, entryPath);
322
+ },
323
+ assertActive,
324
+ };
325
+ assertActive();
292
326
  }
293
327
  let memoryResolution;
294
328
  const memoryAvailable = () => {
@@ -298,10 +332,15 @@ export function createThreadSignalRoutes(options) {
298
332
  Boolean(await agent.getMemory()));
299
333
  }
300
334
  catch {
301
- console.error(JSON.stringify({
302
- type: 'signal-memory-resolution-failed',
303
- threadId,
304
- }));
335
+ try {
336
+ console.error(JSON.stringify({
337
+ type: 'signal-memory-resolution-failed',
338
+ threadId,
339
+ }));
340
+ }
341
+ catch {
342
+ // Diagnostics cannot change the memory fallback.
343
+ }
305
344
  return false;
306
345
  }
307
346
  })();
@@ -339,6 +378,7 @@ export function createThreadSignalRoutes(options) {
339
378
  persistenceAllowed,
340
379
  memoryAvailable,
341
380
  inspectContent,
381
+ proof,
342
382
  executionFence,
343
383
  });
344
384
  }
@@ -351,6 +391,7 @@ export function createThreadSignalRoutes(options) {
351
391
  persistenceAllowed,
352
392
  memoryAvailable,
353
393
  inspectContent,
394
+ proof,
354
395
  });
355
396
  }
356
397
  // POST /signal — a system signal (ifActive/ifIdle deliver/persist/discard/wake).
@@ -362,6 +403,7 @@ export function createThreadSignalRoutes(options) {
362
403
  persistenceAllowed,
363
404
  memoryAvailable,
364
405
  inspectContent,
406
+ proof,
365
407
  executionFence,
366
408
  });
367
409
  }
@@ -397,6 +439,7 @@ export function createThreadSignalRoutes(options) {
397
439
  store: await resolveScheduleDispatchStore(scope),
398
440
  completed: completedScheduleDispatches,
399
441
  inspectContent,
442
+ proof,
400
443
  });
401
444
  }
402
445
  // POST /signal/state — a durable thread-state lane (snapshot/delta).
@@ -408,6 +451,7 @@ export function createThreadSignalRoutes(options) {
408
451
  runtimeDriven,
409
452
  memoryAvailable,
410
453
  inspectContent,
454
+ proof,
411
455
  });
412
456
  }
413
457
  if (requestedAgentId !== undefined) {
@@ -431,15 +475,17 @@ export function createThreadSignalRoutes(options) {
431
475
  persistenceAllowed,
432
476
  memoryAvailable,
433
477
  storage: await resolveNotificationsStorage(scope),
478
+ maxDeliveryAttempts,
434
479
  agentId: requestedAgentId,
435
480
  inspectContent,
481
+ proof,
436
482
  }));
437
483
  }
438
484
  // POST /signal/notification — the durable AGENT inbox (mastra_notifications).
439
485
  if (path === '/signal/notification') {
440
486
  return await serializeNotification(async () => handleNotification(agent, body, threadId, resourceId, resolveNotificationsStorage
441
487
  ? () => resolveNotificationsStorage(scope)
442
- : undefined, { persistenceAllowed, runtimeDriven, inspectContent }));
488
+ : undefined, { persistenceAllowed, runtimeDriven, inspectContent, proof }));
443
489
  }
444
490
  return json({ error: 'not found' }, 404);
445
491
  }
@@ -508,6 +554,7 @@ async function handleNotificationDispatch(options) {
508
554
  if (Number.isNaN(now.getTime())) {
509
555
  return json({ error: 'now must be an ISO timestamp' }, 400);
510
556
  }
557
+ const nowMs = now.getTime();
511
558
  const carriesBatchThreadState = Object.hasOwn(options.body, 'batchThreadState');
512
559
  const requestedBatchThreadState = options.body.batchThreadState;
513
560
  if (carriesBatchThreadState &&
@@ -516,30 +563,60 @@ async function handleNotificationDispatch(options) {
516
563
  requestedBatchThreadState !== 'idle') {
517
564
  return json({ error: 'batchThreadState must be null, active, or idle' }, 400);
518
565
  }
519
- const records = [];
566
+ const deliveryStorage = captureNotificationDeliveryStorage(options.storage);
567
+ const selections = [];
568
+ // A record settles once: an already-discarded record must not be counted
569
+ // twice, nor have its content-policy reason overwritten by an unrelated
570
+ // storage error. `recordFailure` returns early for the same reason.
571
+ const settled = new Set();
572
+ let delivered = 0;
573
+ let failed = 0;
574
+ let discarded = 0;
520
575
  let skipped = 0;
576
+ const settle = (id, outcome) => {
577
+ if (settled.has(id))
578
+ return;
579
+ settled.add(id);
580
+ if (outcome === 'delivered')
581
+ delivered += 1;
582
+ else if (outcome === 'failed')
583
+ failed += 1;
584
+ else if (outcome === 'discarded')
585
+ discarded += 1;
586
+ else
587
+ skipped += 1;
588
+ };
521
589
  for (const id of uniqueIds) {
522
- const current = await options.storage.getNotification({
590
+ const current = await deliveryStorage.getNotification({
523
591
  threadId: options.threadId,
524
592
  id,
525
593
  });
526
594
  if (current?.status !== 'pending' || current.deliveredSignalId) {
527
- skipped += 1;
595
+ settle(id, 'skipped');
528
596
  continue;
529
597
  }
530
- if (current.resourceId !== options.resourceId ||
598
+ if (current.id !== id ||
599
+ current.threadId !== options.threadId ||
600
+ current.resourceId !== options.resourceId ||
531
601
  current.agentId !== options.agentId) {
532
602
  return json({ error: 'notification binding does not match' }, 404);
533
603
  }
534
- const due = (current.deliverAt !== undefined &&
535
- current.deliverAt.getTime() <= now.getTime()) ||
536
- (current.summaryAt !== undefined &&
537
- current.summaryAt.getTime() <= now.getTime());
538
- if (!due) {
539
- skipped += 1;
540
- continue;
604
+ try {
605
+ const selection = captureNotificationDeliverySelection(current);
606
+ const { record } = selection;
607
+ const due = (record.deliverAt !== undefined &&
608
+ record.deliverAt.getTime() <= nowMs) ||
609
+ (record.summaryAt !== undefined && record.summaryAt.getTime() <= nowMs);
610
+ if (!due) {
611
+ settle(id, 'skipped');
612
+ continue;
613
+ }
614
+ selections.push(selection);
615
+ }
616
+ catch (error) {
617
+ settle(id, 'failed');
618
+ reportNotificationDeliveryError(error);
541
619
  }
542
- records.push(current);
543
620
  }
544
621
  const batchThreadState = requestedBatchThreadState === 'active' ||
545
622
  requestedBatchThreadState === 'idle'
@@ -548,30 +625,29 @@ async function handleNotificationDispatch(options) {
548
625
  activeThreadRunIdOf(options.agent, options.threadId, resourceId)
549
626
  ? 'active'
550
627
  : 'idle';
551
- let delivered = 0;
552
- let failed = 0;
553
- let discarded = 0;
554
- // Records whose terminal discard is already durable. A later throw in the
555
- // same group funnels the group into `updateFailure`; without this, an
556
- // already-discarded record would be counted twice and have its
557
- // content-policy reason overwritten by an unrelated storage error.
558
- const settledDiscards = new Set();
559
- const updateFailure = async (record, error) => {
560
- if (settledDiscards.has(record.id))
628
+ const recordFailure = async (expected, action) => {
629
+ if (action.type === 'failure' && isExecutionFenceRefusal(action.error))
630
+ throw action.error;
631
+ if (!expected)
632
+ throw new Error('notification observation is unavailable');
633
+ if (settled.has(expected.id))
561
634
  return;
562
- failed += 1;
563
- await deferNotificationAfterFailure(options.storage, record, now, error);
635
+ await options.proof?.check();
636
+ options.proof?.assertActive();
637
+ const outcome = await recordNotificationDeliveryFailure(deliveryStorage, expected, new Date(nowMs), options.maxDeliveryAttempts, action);
638
+ settle(expected.id, outcome === 'discarded' ? 'discarded' : 'failed');
564
639
  };
565
640
  const discardAfterDenial = async (record) => {
641
+ await options.proof?.check();
642
+ options.proof?.assertActive();
566
643
  await options.storage.updateNotification({
567
644
  id: record.id,
568
645
  threadId: record.threadId,
569
646
  status: 'discarded',
570
647
  deliveryReason: 'content-policy-denied',
571
- lastDeliveryAttemptAt: now,
648
+ lastDeliveryAttemptAt: new Date(nowMs),
572
649
  });
573
- settledDiscards.add(record.id);
574
- discarded += 1;
650
+ settle(record.id, 'discarded');
575
651
  };
576
652
  const inspect = async (signal) => {
577
653
  if (!options.inspectContent)
@@ -595,19 +671,20 @@ async function handleNotificationDispatch(options) {
595
671
  startIdleRun: options.startIdleRun,
596
672
  serializeWake: options.serializeWake,
597
673
  executionFence: options.executionFence,
674
+ proof: options.proof,
598
675
  blockingRun: durableBlockingRun
599
676
  ? () => durableBlockingRun
600
677
  : options.blockingRun,
601
678
  persistenceAllowed: options.persistenceAllowed,
602
679
  memoryAvailable: options.memoryAvailable,
603
680
  signal: deliverableSignal,
604
- deliverActive: (runId, memoryAvailable) => options.agent.sendSignal(deliverableSignal, {
681
+ deliverActive: (runId, activeMemoryAvailable) => options.agent.sendSignal(deliverableSignal, {
605
682
  runId,
606
683
  threadId: options.threadId,
607
684
  resourceId,
608
685
  ifActive: { behavior: 'deliver' },
609
686
  ifIdle: {
610
- behavior: memoryAvailable && options.persistenceAllowed
687
+ behavior: activeMemoryAvailable && options.persistenceAllowed
611
688
  ? 'persist'
612
689
  : 'discard',
613
690
  },
@@ -618,8 +695,11 @@ async function handleNotificationDispatch(options) {
618
695
  ifIdle: { behavior: 'persist' },
619
696
  }),
620
697
  });
621
- if (!response.ok)
698
+ if (!response.ok) {
699
+ if (options.executionFence.state === 'proof-only')
700
+ throw new ExecutionFencedError('proof-only', 'notification signal');
622
701
  throw new Error(`notification signal returned ${response.status}`);
702
+ }
623
703
  const result = (await response.json());
624
704
  const action = recordValue(result.decision)?.action;
625
705
  // A stale active id can fall through to idle. Persistence-forbidden and
@@ -640,6 +720,8 @@ async function handleNotificationDispatch(options) {
640
720
  if (!(await options.memoryAvailable())) {
641
721
  throw new Error('signal persistence requires agent memory');
642
722
  }
723
+ await options.proof?.check();
724
+ options.proof?.assertActive();
643
725
  const result = options.agent.sendSignal(signal, {
644
726
  threadId: options.threadId,
645
727
  resourceId,
@@ -651,12 +733,21 @@ async function handleNotificationDispatch(options) {
651
733
  await result.persisted;
652
734
  return result.signal.id;
653
735
  };
736
+ const records = [];
737
+ const observations = new Map();
738
+ for (const { record, expected } of selections) {
739
+ if (expected.deliveryAttempts >= options.maxDeliveryAttempts) {
740
+ await recordFailure(expected, { type: 'exhausted' });
741
+ continue;
742
+ }
743
+ records.push(record);
744
+ observations.set(record.id, expected);
745
+ }
654
746
  for (const item of planNotificationDispatch(records, now)) {
655
747
  if (item.type === 'summary') {
656
- // Everything from rendering onward stays inside this try: a storage
657
- // failure in the discard/failure bookkeeping below must be contained to
658
- // this group, exactly as the individual branch contains its own, rather
659
- // than escaping and abandoning the rest of the plan.
748
+ // A summary group's failure, from rendering through bookkeeping, is
749
+ // contained to this group rather than escaping and abandoning the rest
750
+ // of the plan.
660
751
  try {
661
752
  const signal = createNotificationSummarySignal(summarizeNotifications(item.records));
662
753
  const inspection = await inspect(signal);
@@ -667,7 +758,10 @@ async function handleNotificationDispatch(options) {
667
758
  }
668
759
  if (inspection === 'error') {
669
760
  for (const record of item.records) {
670
- await updateFailure(record, new Error('signal content policy failed'));
761
+ await recordFailure(observations.get(record.id), {
762
+ type: 'failure',
763
+ error: new Error('signal content policy failed'),
764
+ });
671
765
  }
672
766
  continue;
673
767
  }
@@ -676,49 +770,70 @@ async function handleNotificationDispatch(options) {
676
770
  ? await persistWithoutWake(signal)
677
771
  : await send(signal);
678
772
  for (const record of item.records) {
773
+ await options.proof?.check();
774
+ options.proof?.assertActive();
679
775
  await options.storage.updateNotification({
680
776
  id: record.id,
681
777
  threadId: record.threadId,
682
778
  summaryAt: null,
683
779
  summarySignalId: signalId,
684
- lastDeliveryAttemptAt: now,
780
+ lastDeliveryAttemptAt: new Date(nowMs),
685
781
  });
686
- delivered += 1;
782
+ settle(record.id, 'delivered');
687
783
  }
688
784
  }
689
785
  catch (error) {
690
- for (const record of item.records)
691
- await updateFailure(record, error);
786
+ for (const record of item.records) {
787
+ await recordFailure(observations.get(record.id), {
788
+ type: 'failure',
789
+ error,
790
+ });
791
+ }
692
792
  }
693
793
  continue;
694
794
  }
695
795
  const selected = item.record;
796
+ let expected = observations.get(selected.id);
696
797
  try {
697
- const record = await options.storage.getNotification({
798
+ const current = await deliveryStorage.getNotification({
698
799
  threadId: selected.threadId,
699
800
  id: selected.id,
700
801
  });
701
- const summaryDue = Boolean(record?.summaryAt && record.summaryAt.getTime() <= now.getTime());
702
- const deliveryDue = Boolean(record?.deliverAt && record.deliverAt.getTime() <= now.getTime());
703
- if (record?.status !== 'pending' ||
704
- record.deliveredSignalId ||
705
- record.resourceId !== resourceId ||
706
- record.agentId !== options.agentId ||
707
- summaryDue ||
708
- !deliveryDue) {
709
- skipped += 1;
802
+ if (current?.status !== 'pending' ||
803
+ current.deliveredSignalId ||
804
+ current.id !== selected.id ||
805
+ current.threadId !== options.threadId ||
806
+ current.resourceId !== resourceId ||
807
+ current.agentId !== options.agentId) {
808
+ settle(selected.id, 'skipped');
809
+ continue;
810
+ }
811
+ // Clearing the pre-read observation keeps a failure between here and the
812
+ // re-capture from being recorded against stale state.
813
+ expected = undefined;
814
+ const selection = captureNotificationDeliverySelection(current);
815
+ const { record } = selection;
816
+ expected = selection.expected;
817
+ const summaryDue = Boolean(record.summaryAt && record.summaryAt.getTime() <= nowMs);
818
+ const deliveryDue = Boolean(record.deliverAt && record.deliverAt.getTime() <= nowMs);
819
+ if (summaryDue || !deliveryDue) {
820
+ settle(selected.id, 'skipped');
821
+ continue;
822
+ }
823
+ if (expected.deliveryAttempts >= options.maxDeliveryAttempts) {
824
+ await recordFailure(expected, { type: 'exhausted' });
710
825
  continue;
711
826
  }
712
827
  if (record.priority === 'high' &&
713
828
  record.summarySignalId &&
714
829
  batchThreadState === 'active') {
715
- skipped += 1;
830
+ settle(selected.id, 'skipped');
716
831
  continue;
717
832
  }
718
833
  const signal = createNotificationSignal({
719
834
  ...record,
720
835
  status: 'delivered',
721
- deliveredAt: now,
836
+ deliveredAt: new Date(nowMs),
722
837
  });
723
838
  const inspection = await inspect(signal);
724
839
  if (inspection === 'denied') {
@@ -726,21 +841,33 @@ async function handleNotificationDispatch(options) {
726
841
  continue;
727
842
  }
728
843
  if (inspection === 'error') {
729
- await updateFailure(record, new Error('signal content policy failed'));
844
+ await recordFailure(expected, {
845
+ type: 'failure',
846
+ error: new Error('signal content policy failed'),
847
+ });
730
848
  continue;
731
849
  }
732
850
  const signalId = await send(signal);
851
+ await options.proof?.check();
852
+ options.proof?.assertActive();
733
853
  await options.storage.updateNotification({
734
854
  id: record.id,
735
855
  threadId: record.threadId,
736
856
  status: 'delivered',
737
857
  deliveredSignalId: signalId,
738
- lastDeliveryAttemptAt: now,
858
+ lastDeliveryAttemptAt: new Date(nowMs),
739
859
  });
740
- delivered += 1;
860
+ settle(record.id, 'delivered');
741
861
  }
742
862
  catch (error) {
743
- await updateFailure(selected, error);
863
+ if (isExecutionFenceRefusal(error))
864
+ throw error;
865
+ if (expected)
866
+ await recordFailure(expected, { type: 'failure', error });
867
+ else {
868
+ settle(selected.id, 'failed');
869
+ reportNotificationDeliveryError(error);
870
+ }
744
871
  }
745
872
  }
746
873
  return json({
@@ -831,9 +958,11 @@ async function handleWake(options) {
831
958
  // what the drain is waiting for — and in proof-only it is admitted only
832
959
  // for the nominated run. The check sits after the principal gate so a
833
960
  // fenced deployment leaks nothing a permitted caller could not see.
834
- if (activeRunId && !admitsExistingRun(fence, activeRunId)) {
835
- return executionFencedResponse(fence.state, 'signal delivery');
836
- }
961
+ const admitted = fence.state === 'proof-only'
962
+ ? await options.proof?.capture(activeRunId)
963
+ : undefined;
964
+ if (fence.state === 'proof-only' && admitted === undefined)
965
+ throw new ExecutionFencedError(fence.state, 'signal delivery');
837
966
  if (activeRunId) {
838
967
  if (durableBlockingRun && durableBlockingRun.runId !== activeRunId) {
839
968
  return json({
@@ -846,6 +975,8 @@ async function handleWake(options) {
846
975
  });
847
976
  }
848
977
  const memoryAvailable = await options.memoryAvailable();
978
+ await options.proof?.check(admitted);
979
+ options.proof?.assertActive(admitted);
849
980
  const delivered = options.deliverActive(activeRunId, memoryAvailable);
850
981
  const decision = await delivered.accepted;
851
982
  if (delivered.persisted)
@@ -1026,6 +1157,8 @@ async function handleScheduleSignal(options) {
1026
1157
  const receipt = createScheduleAgentDispatchReceipt('discard', {
1027
1158
  signalId: dispatchId,
1028
1159
  });
1160
+ await options.proof?.check();
1161
+ options.proof?.assertActive();
1029
1162
  options.completed.set(dispatchId, receipt);
1030
1163
  await options.store.settle(scheduleId, dispatchId, receipt);
1031
1164
  options.completed.delete(dispatchId);
@@ -1033,10 +1166,14 @@ async function handleScheduleSignal(options) {
1033
1166
  };
1034
1167
  const completed = options.completed.get(dispatchId);
1035
1168
  if (completed) {
1169
+ await options.proof?.check();
1170
+ options.proof?.assertActive();
1036
1171
  await options.store.settle(scheduleId, dispatchId, completed);
1037
1172
  options.completed.delete(dispatchId);
1038
1173
  return json({ receipt: completed });
1039
1174
  }
1175
+ await options.proof?.check();
1176
+ options.proof?.assertActive();
1040
1177
  const state = await options.store.begin(scheduleId, dispatchId);
1041
1178
  if (state.state === 'missing') {
1042
1179
  return json({ error: 'schedule dispatch not found' }, 404);
@@ -1059,6 +1196,8 @@ async function handleScheduleSignal(options) {
1059
1196
  runId: recoveredRun.runId,
1060
1197
  signalId: dispatchId,
1061
1198
  };
1199
+ await options.proof?.check();
1200
+ options.proof?.assertActive();
1062
1201
  options.completed.set(dispatchId, receipt);
1063
1202
  await options.store.settle(scheduleId, dispatchId, receipt);
1064
1203
  options.completed.delete(dispatchId);
@@ -1073,6 +1212,8 @@ async function handleScheduleSignal(options) {
1073
1212
  runId: durableBlockingRun.runId,
1074
1213
  signalId: dispatchId,
1075
1214
  };
1215
+ await options.proof?.check();
1216
+ options.proof?.assertActive();
1076
1217
  options.completed.set(dispatchId, receipt);
1077
1218
  await options.store.settle(scheduleId, dispatchId, receipt);
1078
1219
  options.completed.delete(dispatchId);
@@ -1091,12 +1232,17 @@ async function handleScheduleSignal(options) {
1091
1232
  !renderableAttributes(target.ifIdle?.attributes)) {
1092
1233
  // The operator has to be able to find the broken schedule; the offending
1093
1234
  // name itself stays out of the log.
1094
- console.error(JSON.stringify({
1095
- type: 'schedule-target-unrenderable',
1096
- scheduleId,
1097
- dispatchId,
1098
- agentId: target.agentId,
1099
- }));
1235
+ try {
1236
+ console.error(JSON.stringify({
1237
+ type: 'schedule-target-unrenderable',
1238
+ scheduleId,
1239
+ dispatchId,
1240
+ agentId: target.agentId,
1241
+ }));
1242
+ }
1243
+ catch {
1244
+ // Diagnostics cannot prevent terminal settlement.
1245
+ }
1100
1246
  return await settleDiscard();
1101
1247
  }
1102
1248
  const baseProviderOptions = target.providerOptions ?? {};
@@ -1220,6 +1366,7 @@ async function handleScheduleSignal(options) {
1220
1366
  startIdleRun: options.startIdleRun,
1221
1367
  serializeWake: options.serializeWake,
1222
1368
  executionFence: options.executionFence,
1369
+ proof: options.proof,
1223
1370
  blockingRun: durableBlockingRun
1224
1371
  ? () => durableBlockingRun
1225
1372
  : options.blockingRun,
@@ -1231,13 +1378,15 @@ async function handleScheduleSignal(options) {
1231
1378
  dispatchId,
1232
1379
  safeContext: { ...requestContext, ...idleRequestContext },
1233
1380
  activeDiscardAllowed: ifActive.behavior === 'discard',
1234
- deliverActive: (activeRunId, memoryAvailable) => options.agent.sendSignal(deliverableSignal, {
1381
+ deliverActive: (activeRunId, activeMemoryAvailable) => options.agent.sendSignal(deliverableSignal, {
1235
1382
  runId: activeRunId,
1236
1383
  threadId: options.threadId,
1237
1384
  resourceId,
1238
1385
  ifActive,
1239
1386
  ifIdle: {
1240
- behavior: memoryAvailable && persistenceAllowed ? 'persist' : 'discard',
1387
+ behavior: activeMemoryAvailable && persistenceAllowed
1388
+ ? 'persist'
1389
+ : 'discard',
1241
1390
  },
1242
1391
  }),
1243
1392
  persist: () => options.agent.sendSignal(deliverableSignal, {
@@ -1255,6 +1404,8 @@ async function handleScheduleSignal(options) {
1255
1404
  typeof payload.signalId === 'string' ? payload.signalId : undefined;
1256
1405
  }
1257
1406
  else {
1407
+ await options.proof?.check();
1408
+ options.proof?.assertActive();
1258
1409
  const sent = options.agent.sendSignal(deliverableSignal, signalTarget);
1259
1410
  decision = await sent.accepted;
1260
1411
  const action = recordValue(decision)?.action;
@@ -1274,6 +1425,8 @@ async function handleScheduleSignal(options) {
1274
1425
  if (!receipt) {
1275
1426
  throw new Error('agent schedule returned an invalid signal decision');
1276
1427
  }
1428
+ await options.proof?.check();
1429
+ options.proof?.assertActive();
1277
1430
  options.completed.set(dispatchId, receipt);
1278
1431
  await options.store.settle(scheduleId, dispatchId, receipt);
1279
1432
  options.completed.delete(dispatchId);
@@ -1327,13 +1480,14 @@ async function handleMessage(agent, body, threadId, resourceId, consultRunCap, d
1327
1480
  startIdleRun,
1328
1481
  serializeWake,
1329
1482
  executionFence: options.executionFence,
1483
+ proof: options.proof,
1330
1484
  blockingRun: durableBlockingRun
1331
1485
  ? () => durableBlockingRun
1332
1486
  : options.blockingRun,
1333
1487
  persistenceAllowed: options.persistenceAllowed,
1334
1488
  memoryAvailable: options.memoryAvailable,
1335
1489
  message,
1336
- deliverActive: (runId, memoryAvailable) => agent.sendMessage(message, {
1490
+ deliverActive: (runId, activeMemoryAvailable) => agent.sendMessage(message, {
1337
1491
  runId,
1338
1492
  threadId,
1339
1493
  resourceId,
@@ -1341,7 +1495,7 @@ async function handleMessage(agent, body, threadId, resourceId, consultRunCap, d
1341
1495
  // A stale active id can disappear before core sends. Persist only
1342
1496
  // when both the memory and authorization gates allow the write.
1343
1497
  ifIdle: {
1344
- behavior: memoryAvailable && options.persistenceAllowed
1498
+ behavior: activeMemoryAvailable && options.persistenceAllowed
1345
1499
  ? 'persist'
1346
1500
  : 'discard',
1347
1501
  },
@@ -1357,6 +1511,8 @@ async function handleMessage(agent, body, threadId, resourceId, consultRunCap, d
1357
1511
  return persistenceForbiddenResponse({ capped: false });
1358
1512
  }
1359
1513
  const memoryAvailable = await options.memoryAvailable();
1514
+ await options.proof?.check();
1515
+ options.proof?.assertActive();
1360
1516
  const result = agent.sendMessage(message, {
1361
1517
  threadId,
1362
1518
  resourceId,
@@ -1400,6 +1556,8 @@ async function handleQueue(agent, body, threadId, resourceId, options) {
1400
1556
  return policyRefusal;
1401
1557
  if (!(await options.memoryAvailable()))
1402
1558
  return memoryUnavailableResponse();
1559
+ await options.proof?.check();
1560
+ options.proof?.assertActive();
1403
1561
  const result = agent.sendMessage(message, {
1404
1562
  threadId,
1405
1563
  resourceId,
@@ -1454,6 +1612,8 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
1454
1612
  if (!isPathSafeId(runId)) {
1455
1613
  throw new Error('thread signal generated a non-path-safe run id');
1456
1614
  }
1615
+ await options.proof?.check();
1616
+ options.proof?.assertActive();
1457
1617
  const result = agent.sendSignal(signal, {
1458
1618
  threadId,
1459
1619
  runId,
@@ -1476,6 +1636,7 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
1476
1636
  startIdleRun,
1477
1637
  serializeWake,
1478
1638
  executionFence: options.executionFence,
1639
+ proof: options.proof,
1479
1640
  blockingRun: durableBlockingRun
1480
1641
  ? () => durableBlockingRun
1481
1642
  : options.blockingRun,
@@ -1483,13 +1644,13 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
1483
1644
  memoryAvailable: options.memoryAvailable,
1484
1645
  signal,
1485
1646
  activeDiscardAllowed: activeBehavior === 'discard',
1486
- deliverActive: (runId, memoryAvailable) => agent.sendSignal(signal, {
1647
+ deliverActive: (runId, activeMemoryAvailable) => agent.sendSignal(signal, {
1487
1648
  runId,
1488
1649
  threadId,
1489
1650
  resourceId,
1490
1651
  ifActive: { behavior: deliveredActiveBehavior },
1491
1652
  ifIdle: {
1492
- behavior: memoryAvailable && options.persistenceAllowed
1653
+ behavior: activeMemoryAvailable && options.persistenceAllowed
1493
1654
  ? 'persist'
1494
1655
  : 'discard',
1495
1656
  },
@@ -1507,6 +1668,8 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
1507
1668
  }
1508
1669
  const memoryAvailable = await options.memoryAvailable();
1509
1670
  const wasActive = activeThreadRunIdOf(agent, threadId, resourceId) !== undefined;
1671
+ await options.proof?.check();
1672
+ options.proof?.assertActive();
1510
1673
  const result = agent.sendSignal(signal, {
1511
1674
  threadId,
1512
1675
  resourceId,
@@ -1580,6 +1743,8 @@ async function handleState(agent, body, threadId, resourceId, options) {
1580
1743
  return policyRefusal;
1581
1744
  if (!(await options.memoryAvailable()))
1582
1745
  return memoryUnavailableResponse();
1746
+ await options.proof?.check();
1747
+ options.proof?.assertActive();
1583
1748
  const result = await agent.sendStateSignal(state, {
1584
1749
  threadId,
1585
1750
  resourceId,
@@ -1688,6 +1853,8 @@ async function handleNotification(agent, body, threadId, resourceId, resolveNoti
1688
1853
  if (!storage) {
1689
1854
  return json({ error: 'notifications storage unavailable' }, 409);
1690
1855
  }
1856
+ await options.proof?.check();
1857
+ options.proof?.assertActive();
1691
1858
  const record = await storage.createNotification({
1692
1859
  ...notification,
1693
1860
  threadId,
@@ -1700,6 +1867,8 @@ async function handleNotification(agent, body, threadId, resourceId, resolveNoti
1700
1867
  delivery: { action: 'deferred', reason: 'dispatcher' },
1701
1868
  });
1702
1869
  }
1870
+ await options.proof?.check();
1871
+ options.proof?.assertActive();
1703
1872
  const result = await agent.sendNotificationSignal(notification, {
1704
1873
  threadId,
1705
1874
  resourceId,