@proofoftech/flowsafe 0.11.0 → 0.13.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.
- package/CHANGELOG.md +54 -0
- package/README.md +91 -44
- package/deployment-identity-protocol.d.mts +52 -0
- package/deployment-identity-protocol.mjs +231 -0
- package/dist/agent-host/approval-resumer.d.ts +3 -3
- package/dist/agent-host/approval-resumer.d.ts.map +1 -1
- package/dist/agent-host/approval-resumer.js +6 -7
- package/dist/agent-host/approval-resumer.js.map +1 -1
- package/dist/agent-host/catalog.js +2 -2
- package/dist/agent-host/catalog.js.map +1 -1
- package/dist/agent-host/index.d.ts +2 -2
- package/dist/agent-host/index.d.ts.map +1 -1
- package/dist/agent-host/index.js.map +1 -1
- package/dist/agent-host/router.d.ts +2 -2
- package/dist/agent-host/router.d.ts.map +1 -1
- package/dist/agent-host/router.js +22 -54
- package/dist/agent-host/router.js.map +1 -1
- package/dist/agent-host/thread-host.d.ts +42 -8
- package/dist/agent-host/thread-host.d.ts.map +1 -1
- package/dist/agent-host/thread-host.js +533 -106
- package/dist/agent-host/thread-host.js.map +1 -1
- package/dist/agent-host/thread-topology.d.ts +28 -7
- package/dist/agent-host/thread-topology.d.ts.map +1 -1
- package/dist/agent-host/thread-topology.js +97 -39
- package/dist/agent-host/thread-topology.js.map +1 -1
- package/dist/agent-host/trusted-context.d.ts.map +1 -1
- package/dist/agent-host/trusted-context.js +3 -1
- package/dist/agent-host/trusted-context.js.map +1 -1
- package/dist/agent-host/types.d.ts +2 -0
- package/dist/agent-host/types.d.ts.map +1 -1
- package/dist/agent-runner/agent-run-state.d.ts +9 -8
- package/dist/agent-runner/agent-run-state.d.ts.map +1 -1
- package/dist/agent-runner/agent-run-state.js +78 -40
- package/dist/agent-runner/agent-run-state.js.map +1 -1
- package/dist/agent-runner/durable-agent-runner.d.ts +18 -16
- package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
- package/dist/agent-runner/durable-agent-runner.js +120 -43
- package/dist/agent-runner/durable-agent-runner.js.map +1 -1
- package/dist/agent-runner/index.d.ts +1 -1
- package/dist/agent-runner/index.d.ts.map +1 -1
- package/dist/agent-runner/index.js +1 -1
- package/dist/agent-runner/index.js.map +1 -1
- package/dist/approval-api/actor-context.d.ts +67 -0
- package/dist/approval-api/actor-context.d.ts.map +1 -0
- package/dist/approval-api/actor-context.js +120 -0
- package/dist/approval-api/actor-context.js.map +1 -0
- package/dist/approval-api/contract.d.ts +12 -23
- package/dist/approval-api/contract.d.ts.map +1 -1
- package/dist/approval-api/d1-store.d.ts +7 -15
- package/dist/approval-api/d1-store.d.ts.map +1 -1
- package/dist/approval-api/d1-store.js +83 -159
- package/dist/approval-api/d1-store.js.map +1 -1
- package/dist/approval-api/grants.d.ts +3 -17
- package/dist/approval-api/grants.d.ts.map +1 -1
- package/dist/approval-api/grants.js +19 -39
- package/dist/approval-api/grants.js.map +1 -1
- package/dist/approval-api/index.d.ts +8 -8
- package/dist/approval-api/index.d.ts.map +1 -1
- package/dist/approval-api/index.js +5 -5
- package/dist/approval-api/index.js.map +1 -1
- package/dist/approval-api/principal.d.ts +29 -30
- package/dist/approval-api/principal.d.ts.map +1 -1
- package/dist/approval-api/principal.js +73 -54
- package/dist/approval-api/principal.js.map +1 -1
- package/dist/approval-api/resource-ownership.d.ts +64 -0
- package/dist/approval-api/resource-ownership.d.ts.map +1 -0
- package/dist/approval-api/resource-ownership.js +329 -0
- package/dist/approval-api/resource-ownership.js.map +1 -0
- package/dist/approval-api/retention.d.ts +6 -11
- package/dist/approval-api/retention.d.ts.map +1 -1
- package/dist/approval-api/retention.js +7 -12
- package/dist/approval-api/retention.js.map +1 -1
- package/dist/approval-api/router.d.ts +9 -8
- package/dist/approval-api/router.d.ts.map +1 -1
- package/dist/approval-api/router.js +50 -18
- package/dist/approval-api/router.js.map +1 -1
- package/dist/approval-api/service.d.ts +26 -36
- package/dist/approval-api/service.d.ts.map +1 -1
- package/dist/approval-api/service.js +104 -166
- package/dist/approval-api/service.js.map +1 -1
- package/dist/approval-api/store-factory.d.ts +19 -0
- package/dist/approval-api/store-factory.d.ts.map +1 -0
- package/dist/approval-api/store-factory.js +49 -0
- package/dist/approval-api/store-factory.js.map +1 -0
- package/dist/approval-api/store.d.ts +22 -36
- package/dist/approval-api/store.d.ts.map +1 -1
- package/dist/approval-api/store.js +48 -66
- package/dist/approval-api/store.js.map +1 -1
- package/dist/approval-api/types.d.ts +28 -22
- package/dist/approval-api/types.d.ts.map +1 -1
- package/dist/approval-api/types.js +51 -3
- package/dist/approval-api/types.js.map +1 -1
- package/dist/approval-ui/client.js +3 -3
- package/dist/approval-ui/client.js.map +1 -1
- package/dist/approval-ui/stream.d.ts +4 -4
- package/dist/approval-ui/stream.d.ts.map +1 -1
- package/dist/approval-ui/stream.js +2 -2
- package/dist/approval-ui/tips.d.ts +1 -1
- package/dist/approval-ui/tips.js +1 -1
- package/dist/approval-ui/tips.js.map +1 -1
- package/dist/approval-ui/use-approval-dashboard.d.ts +3 -3
- package/dist/approval-ui/use-approval-dashboard.js +1 -1
- package/dist/artifacts/index.d.ts +2 -3
- package/dist/artifacts/index.d.ts.map +1 -1
- package/dist/artifacts/index.js +9 -8
- package/dist/artifacts/index.js.map +1 -1
- package/dist/audit-export/audit-proxy.d.ts +71 -0
- package/dist/audit-export/audit-proxy.d.ts.map +1 -0
- package/dist/audit-export/audit-proxy.js +142 -0
- package/dist/audit-export/audit-proxy.js.map +1 -0
- package/dist/audit-export/index.d.ts +4 -7
- package/dist/audit-export/index.d.ts.map +1 -1
- package/dist/audit-export/index.js +1 -4
- package/dist/audit-export/index.js.map +1 -1
- package/dist/audit-export/queue-types.d.ts +5 -0
- package/dist/audit-export/queue-types.d.ts.map +1 -0
- package/dist/audit-export/queue-types.js +3 -0
- package/dist/audit-export/queue-types.js.map +1 -0
- package/dist/background-tasks/d1-storage.d.ts +9 -19
- package/dist/background-tasks/d1-storage.d.ts.map +1 -1
- package/dist/background-tasks/d1-storage.js +22 -52
- package/dist/background-tasks/d1-storage.js.map +1 -1
- package/dist/background-tasks/host.d.ts +4 -6
- package/dist/background-tasks/host.d.ts.map +1 -1
- package/dist/background-tasks/host.js +14 -17
- package/dist/background-tasks/host.js.map +1 -1
- package/dist/background-tasks/index.d.ts +1 -1
- package/dist/background-tasks/index.d.ts.map +1 -1
- package/dist/background-tasks/index.js +3 -3
- package/dist/background-tasks/index.js.map +1 -1
- package/dist/background-tasks/routes.d.ts +8 -4
- package/dist/background-tasks/routes.d.ts.map +1 -1
- package/dist/background-tasks/routes.js +33 -31
- package/dist/background-tasks/routes.js.map +1 -1
- package/dist/deployment-identity-protocol.d.ts +3 -0
- package/dist/deployment-identity-protocol.d.ts.map +1 -0
- package/dist/deployment-identity-protocol.js +3 -0
- package/dist/deployment-identity-protocol.js.map +1 -0
- package/dist/do-runner/breakwater-keys.d.ts +7 -5
- package/dist/do-runner/breakwater-keys.d.ts.map +1 -1
- package/dist/do-runner/breakwater-keys.js +7 -5
- package/dist/do-runner/breakwater-keys.js.map +1 -1
- package/dist/do-runner/cf-types.d.ts +18 -12
- package/dist/do-runner/cf-types.d.ts.map +1 -1
- package/dist/do-runner/cf-types.js.map +1 -1
- package/dist/do-runner/d1-storage.d.ts +51 -189
- package/dist/do-runner/d1-storage.d.ts.map +1 -1
- package/dist/do-runner/d1-storage.js +111 -362
- package/dist/do-runner/d1-storage.js.map +1 -1
- package/dist/do-runner/deployment-identity.d.ts +121 -0
- package/dist/do-runner/deployment-identity.d.ts.map +1 -0
- package/dist/do-runner/deployment-identity.js +272 -0
- package/dist/do-runner/deployment-identity.js.map +1 -0
- package/dist/do-runner/do-error-response.d.ts +1 -1
- package/dist/do-runner/do-error-response.d.ts.map +1 -1
- package/dist/do-runner/do-error-response.js +7 -1
- package/dist/do-runner/do-error-response.js.map +1 -1
- package/dist/do-runner/durable-object.d.ts +23 -16
- package/dist/do-runner/durable-object.d.ts.map +1 -1
- package/dist/do-runner/durable-object.js +303 -70
- package/dist/do-runner/durable-object.js.map +1 -1
- package/dist/do-runner/execution-context.d.ts +1 -0
- package/dist/do-runner/execution-context.d.ts.map +1 -1
- package/dist/do-runner/execution-context.js +4 -7
- package/dist/do-runner/execution-context.js.map +1 -1
- package/dist/do-runner/execution-principal-header.d.ts +16 -0
- package/dist/do-runner/execution-principal-header.d.ts.map +1 -0
- package/dist/do-runner/execution-principal-header.js +20 -0
- package/dist/do-runner/execution-principal-header.js.map +1 -0
- package/dist/do-runner/hub-do.d.ts +17 -25
- package/dist/do-runner/hub-do.d.ts.map +1 -1
- package/dist/do-runner/hub-do.js +63 -59
- package/dist/do-runner/hub-do.js.map +1 -1
- package/dist/do-runner/index.d.ts +12 -10
- package/dist/do-runner/index.d.ts.map +1 -1
- package/dist/do-runner/index.js +10 -9
- package/dist/do-runner/index.js.map +1 -1
- package/dist/do-runner/init.d.ts +0 -7
- package/dist/do-runner/init.d.ts.map +1 -1
- package/dist/do-runner/init.js +1 -2
- package/dist/do-runner/init.js.map +1 -1
- package/dist/do-runner/memory-id.d.ts +8 -25
- package/dist/do-runner/memory-id.d.ts.map +1 -1
- package/dist/do-runner/memory-id.js +25 -52
- package/dist/do-runner/memory-id.js.map +1 -1
- package/dist/do-runner/path-safe-id.d.ts +2 -48
- package/dist/do-runner/path-safe-id.d.ts.map +1 -1
- package/dist/do-runner/path-safe-id.js +3 -93
- package/dist/do-runner/path-safe-id.js.map +1 -1
- package/dist/do-runner/pubsub.d.ts +1 -1
- package/dist/do-runner/pubsub.js +1 -1
- package/dist/do-runner/runtime.d.ts +47 -34
- package/dist/do-runner/runtime.d.ts.map +1 -1
- package/dist/do-runner/runtime.js +281 -143
- package/dist/do-runner/runtime.js.map +1 -1
- package/dist/do-runner/schedule-source.d.ts +81 -0
- package/dist/do-runner/schedule-source.d.ts.map +1 -0
- package/dist/do-runner/schedule-source.js +64 -0
- package/dist/do-runner/schedule-source.js.map +1 -0
- package/dist/do-runner/table-prefix.d.ts +3 -0
- package/dist/do-runner/table-prefix.d.ts.map +1 -0
- package/dist/do-runner/table-prefix.js +17 -0
- package/dist/do-runner/table-prefix.js.map +1 -0
- package/dist/do-runner/thread-do.d.ts +18 -32
- package/dist/do-runner/thread-do.d.ts.map +1 -1
- package/dist/do-runner/thread-do.js +52 -74
- package/dist/do-runner/thread-do.js.map +1 -1
- package/dist/goals/objective-routes.d.ts +7 -4
- package/dist/goals/objective-routes.d.ts.map +1 -1
- package/dist/goals/objective-routes.js +58 -43
- package/dist/goals/objective-routes.js.map +1 -1
- package/dist/host-kit/approval-bridge.d.ts +18 -21
- package/dist/host-kit/approval-bridge.d.ts.map +1 -1
- package/dist/host-kit/approval-bridge.js +37 -37
- package/dist/host-kit/approval-bridge.js.map +1 -1
- package/dist/host-kit/bearer-auth.d.ts +3 -4
- package/dist/host-kit/bearer-auth.d.ts.map +1 -1
- package/dist/host-kit/bearer-auth.js +7 -11
- package/dist/host-kit/bearer-auth.js.map +1 -1
- package/dist/host-kit/do-run-topology.d.ts +14 -5
- package/dist/host-kit/do-run-topology.d.ts.map +1 -1
- package/dist/host-kit/do-run-topology.js +47 -15
- package/dist/host-kit/do-run-topology.js.map +1 -1
- package/dist/host-kit/env-vars.d.ts +1 -1
- package/dist/host-kit/env-vars.js +1 -1
- package/dist/host-kit/flowsafe-worker.d.ts +159 -80
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +629 -142
- package/dist/host-kit/flowsafe-worker.js.map +1 -1
- package/dist/host-kit/host-approval-service.d.ts +62 -54
- package/dist/host-kit/host-approval-service.d.ts.map +1 -1
- package/dist/host-kit/host-approval-service.js +90 -61
- package/dist/host-kit/host-approval-service.js.map +1 -1
- package/dist/host-kit/hub-topology.d.ts +6 -7
- package/dist/host-kit/hub-topology.d.ts.map +1 -1
- package/dist/host-kit/hub-topology.js +16 -12
- package/dist/host-kit/hub-topology.js.map +1 -1
- package/dist/host-kit/index.d.ts +13 -11
- package/dist/host-kit/index.d.ts.map +1 -1
- package/dist/host-kit/index.js +9 -12
- package/dist/host-kit/index.js.map +1 -1
- package/dist/host-kit/maintenance-capability.d.ts +58 -0
- package/dist/host-kit/maintenance-capability.d.ts.map +1 -0
- package/dist/host-kit/maintenance-capability.js +268 -0
- package/dist/host-kit/maintenance-capability.js.map +1 -0
- package/dist/host-kit/memory-boundary.d.ts +4 -19
- package/dist/host-kit/memory-boundary.d.ts.map +1 -1
- package/dist/host-kit/memory-boundary.js +12 -27
- package/dist/host-kit/memory-boundary.js.map +1 -1
- package/dist/host-kit/resource-access.d.ts +4 -0
- package/dist/host-kit/resource-access.d.ts.map +1 -0
- package/dist/host-kit/resource-access.js +12 -0
- package/dist/host-kit/resource-access.js.map +1 -0
- package/dist/host-kit/run-router.d.ts +25 -15
- package/dist/host-kit/run-router.d.ts.map +1 -1
- package/dist/host-kit/run-router.js +61 -46
- package/dist/host-kit/run-router.js.map +1 -1
- package/dist/host-kit/state-egress.d.ts +51 -0
- package/dist/host-kit/state-egress.d.ts.map +1 -0
- package/dist/host-kit/state-egress.js +120 -0
- package/dist/host-kit/state-egress.js.map +1 -0
- package/dist/host-kit/stream-router.d.ts +9 -4
- package/dist/host-kit/stream-router.d.ts.map +1 -1
- package/dist/host-kit/stream-router.js +45 -38
- package/dist/host-kit/stream-router.js.map +1 -1
- package/dist/host-kit/stream-ticket.d.ts +10 -10
- package/dist/host-kit/stream-ticket.d.ts.map +1 -1
- package/dist/host-kit/stream-ticket.js +60 -90
- package/dist/host-kit/stream-ticket.js.map +1 -1
- package/dist/host-kit/thread-topology.d.ts +17 -11
- package/dist/host-kit/thread-topology.d.ts.map +1 -1
- package/dist/host-kit/thread-topology.js +33 -59
- package/dist/host-kit/thread-topology.js.map +1 -1
- package/dist/host-kit/verifier.d.ts +6 -6
- package/dist/host-kit/verifier.d.ts.map +1 -1
- package/dist/host-kit/verifier.js +51 -119
- package/dist/host-kit/verifier.js.map +1 -1
- package/dist/http-body.d.ts +20 -0
- package/dist/http-body.d.ts.map +1 -0
- package/dist/http-body.js +67 -0
- package/dist/http-body.js.map +1 -0
- package/dist/schedules/index.d.ts +3 -2
- package/dist/schedules/index.d.ts.map +1 -1
- package/dist/schedules/index.js +5 -4
- package/dist/schedules/index.js.map +1 -1
- package/dist/schedules/router.d.ts +16 -10
- package/dist/schedules/router.d.ts.map +1 -1
- package/dist/schedules/router.js +153 -91
- package/dist/schedules/router.js.map +1 -1
- package/dist/schedules/schedules-d1.d.ts +88 -0
- package/dist/schedules/schedules-d1.d.ts.map +1 -1
- package/dist/schedules/schedules-d1.js +411 -50
- package/dist/schedules/schedules-d1.js.map +1 -1
- package/dist/schedules/target-policy.d.ts +29 -0
- package/dist/schedules/target-policy.d.ts.map +1 -0
- package/dist/schedules/target-policy.js +70 -0
- package/dist/schedules/target-policy.js.map +1 -0
- package/dist/schedules/tick.d.ts +125 -21
- package/dist/schedules/tick.d.ts.map +1 -1
- package/dist/schedules/tick.js +637 -160
- package/dist/schedules/tick.js.map +1 -1
- package/dist/signal-providers/delivery.d.ts +0 -19
- package/dist/signal-providers/delivery.d.ts.map +1 -1
- package/dist/signal-providers/delivery.js +5 -54
- package/dist/signal-providers/delivery.js.map +1 -1
- package/dist/signal-providers/github-provider.d.ts.map +1 -1
- package/dist/signal-providers/github-provider.js +9 -16
- package/dist/signal-providers/github-provider.js.map +1 -1
- package/dist/signal-providers/host-do.d.ts +3 -45
- package/dist/signal-providers/host-do.d.ts.map +1 -1
- package/dist/signal-providers/host-do.js +86 -106
- package/dist/signal-providers/host-do.js.map +1 -1
- package/dist/signal-providers/host-topology.d.ts +5 -14
- package/dist/signal-providers/host-topology.d.ts.map +1 -1
- package/dist/signal-providers/host-topology.js +11 -25
- package/dist/signal-providers/host-topology.js.map +1 -1
- package/dist/signal-providers/index.d.ts +3 -3
- package/dist/signal-providers/index.d.ts.map +1 -1
- package/dist/signal-providers/index.js +3 -3
- package/dist/signal-providers/index.js.map +1 -1
- package/dist/signal-providers/provider.d.ts +4 -5
- package/dist/signal-providers/provider.d.ts.map +1 -1
- package/dist/signal-providers/provider.js +5 -6
- package/dist/signal-providers/provider.js.map +1 -1
- package/dist/signal-providers/subscription-d1.d.ts +10 -57
- package/dist/signal-providers/subscription-d1.d.ts.map +1 -1
- package/dist/signal-providers/subscription-d1.js +208 -155
- package/dist/signal-providers/subscription-d1.js.map +1 -1
- package/dist/signal-providers/webhook-route.d.ts +28 -14
- package/dist/signal-providers/webhook-route.d.ts.map +1 -1
- package/dist/signal-providers/webhook-route.js +117 -88
- package/dist/signal-providers/webhook-route.js.map +1 -1
- package/dist/signals/client.js +8 -8
- package/dist/signals/client.js.map +1 -1
- package/dist/signals/d1-shared.js +1 -1
- package/dist/signals/d1-shared.js.map +1 -1
- package/dist/signals/index.d.ts +1 -1
- package/dist/signals/index.d.ts.map +1 -1
- package/dist/signals/index.js.map +1 -1
- package/dist/signals/notification-dispatch.d.ts +12 -6
- package/dist/signals/notification-dispatch.d.ts.map +1 -1
- package/dist/signals/notification-dispatch.js +37 -21
- package/dist/signals/notification-dispatch.js.map +1 -1
- package/dist/signals/notifications-d1.d.ts +5 -8
- package/dist/signals/notifications-d1.d.ts.map +1 -1
- package/dist/signals/notifications-d1.js +8 -13
- package/dist/signals/notifications-d1.js.map +1 -1
- package/dist/signals/router.d.ts +10 -10
- package/dist/signals/router.d.ts.map +1 -1
- package/dist/signals/router.js +49 -51
- package/dist/signals/router.js.map +1 -1
- package/dist/signals/thread-do-routes.d.ts +74 -15
- package/dist/signals/thread-do-routes.d.ts.map +1 -1
- package/dist/signals/thread-do-routes.js +484 -41
- package/dist/signals/thread-do-routes.js.map +1 -1
- package/dist/signals/thread-state-d1.d.ts.map +1 -1
- package/dist/signals/thread-state-d1.js +2 -3
- package/dist/signals/thread-state-d1.js.map +1 -1
- package/package.json +29 -6
- package/scripts/seed-deployment-identity.mjs +252 -0
- package/dist/approval-api/tenant-brand.d.ts +0 -35
- package/dist/approval-api/tenant-brand.d.ts.map +0 -1
- package/dist/approval-api/tenant-brand.js +0 -19
- package/dist/approval-api/tenant-brand.js.map +0 -1
- package/dist/approval-api/tenant-context.d.ts +0 -84
- package/dist/approval-api/tenant-context.d.ts.map +0 -1
- package/dist/approval-api/tenant-context.js +0 -112
- package/dist/approval-api/tenant-context.js.map +0 -1
- package/dist/approval-api/tenant-store.d.ts +0 -36
- package/dist/approval-api/tenant-store.d.ts.map +0 -1
- package/dist/approval-api/tenant-store.js +0 -129
- package/dist/approval-api/tenant-store.js.map +0 -1
- package/dist/do-runner/resume-ledger.d.ts +0 -48
- package/dist/do-runner/resume-ledger.d.ts.map +0 -1
- package/dist/do-runner/resume-ledger.js +0 -60
- package/dist/do-runner/resume-ledger.js.map +0 -1
- package/dist/do-runner/thread-header.d.ts +0 -31
- package/dist/do-runner/thread-header.d.ts.map +0 -1
- package/dist/do-runner/thread-header.js +0 -39
- package/dist/do-runner/thread-header.js.map +0 -1
- package/dist/host-kit/subdomain-check.d.ts +0 -23
- package/dist/host-kit/subdomain-check.d.ts.map +0 -1
- package/dist/host-kit/subdomain-check.js +0 -61
- package/dist/host-kit/subdomain-check.js.map +0 -1
- package/dist/host-kit/tenant-registry.d.ts +0 -51
- package/dist/host-kit/tenant-registry.d.ts.map +0 -1
- package/dist/host-kit/tenant-registry.js +0 -88
- package/dist/host-kit/tenant-registry.js.map +0 -1
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
import { AGENT_STREAM_TOPIC } from '@mastra/core/agent/durable';
|
|
3
3
|
import { Mastra } from '@mastra/core/mastra';
|
|
4
4
|
import { isPrincipalPermissions } from '@proofoftech/breakwater/rbac';
|
|
5
|
-
import { bindAgentThread, createFlowsafeDurableAgent, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, deleteAgentRunRecord, readAgentRunRecord, readAgentThreadBinding, writeAgentRunRecord, } from '../agent-runner/index.js';
|
|
6
|
-
import { principalActor, principalAuditFields, RUN_START_ROLES, samePrincipal, } from '../approval-api/index.js';
|
|
7
|
-
import {
|
|
5
|
+
import { AGENT_ENTRY_PATHS, AGENT_RUN_STORAGE_KEY_PREFIX, bindAgentThread, createFlowsafeDurableAgent, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, deleteAgentRunRecord, deleteAgentThreadBinding, readAgentRunRecord, readAgentThreadBinding, writeAgentRunRecord, } from '../agent-runner/index.js';
|
|
6
|
+
import { canonicalResourceOwner, principalActor, principalAuditFields, principalOwner, RUN_START_ROLES, samePrincipal, } from '../approval-api/index.js';
|
|
7
|
+
import { isExecutionPrincipalId, } from '../approval-api/principal.js';
|
|
8
|
+
import { DoStatusError, isPathSafeId, resolveScheduleStartOwner, resourceIdFromKey, } from '../do-runner/index.js';
|
|
8
9
|
import { mastraRegistryEntries } from '../do-runner/mastra-registry.js';
|
|
9
10
|
import { reconcileApprovalsForSummary } from '../host-kit/index.js';
|
|
10
11
|
import { createAgentModuleCatalog } from './catalog.js';
|
|
@@ -59,24 +60,14 @@ const TERMINAL_RUN_STATUSES = [
|
|
|
59
60
|
'bailed',
|
|
60
61
|
'skipped',
|
|
61
62
|
];
|
|
63
|
+
const AGENT_OWNER_RECOVERY_PREFIX = 'flowsafe:agent-owner-recovery:v1:';
|
|
64
|
+
const AGENT_OWNER_RECOVERY_DELAY_MS = 60_000;
|
|
62
65
|
function isTerminalRunStatus(status) {
|
|
63
66
|
return TERMINAL_RUN_STATUSES.includes(status);
|
|
64
67
|
}
|
|
65
68
|
function entryPath(value) {
|
|
66
|
-
const allowed = [
|
|
67
|
-
'http.start',
|
|
68
|
-
'approval.resume',
|
|
69
|
-
'signal.message',
|
|
70
|
-
'signal.queue',
|
|
71
|
-
'signal.reactive',
|
|
72
|
-
'signal.state',
|
|
73
|
-
'signal.notification',
|
|
74
|
-
'signal.wake',
|
|
75
|
-
'notification.dispatch',
|
|
76
|
-
'schedule.fire',
|
|
77
|
-
];
|
|
78
69
|
if (typeof value !== 'string' ||
|
|
79
|
-
!
|
|
70
|
+
!AGENT_ENTRY_PATHS.includes(value)) {
|
|
80
71
|
throw new AgentHostRequestError(400, 'entryPath is required');
|
|
81
72
|
}
|
|
82
73
|
return value;
|
|
@@ -88,14 +79,65 @@ function safeContext(value) {
|
|
|
88
79
|
}
|
|
89
80
|
throw new AgentHostRequestError(400, 'safeContext must be an object');
|
|
90
81
|
}
|
|
82
|
+
function providerOptions(value) {
|
|
83
|
+
if (value === undefined)
|
|
84
|
+
return undefined;
|
|
85
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
86
|
+
throw new AgentHostRequestError(400, 'providerOptions must be an object');
|
|
87
|
+
}
|
|
88
|
+
return structuredClone(value);
|
|
89
|
+
}
|
|
90
|
+
function record(value) {
|
|
91
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
92
|
+
? value
|
|
93
|
+
: undefined;
|
|
94
|
+
}
|
|
95
|
+
function scheduleAgentContext(target) {
|
|
96
|
+
const idle = record(target.ifIdle);
|
|
97
|
+
const streamOptions = record(idle?.streamOptions);
|
|
98
|
+
const streamContext = record(streamOptions?.requestContext);
|
|
99
|
+
return {
|
|
100
|
+
...target.requestContext,
|
|
101
|
+
...streamContext,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function resourceOwner(value) {
|
|
105
|
+
try {
|
|
106
|
+
return canonicalResourceOwner(value);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
throw new AgentHostRequestError(400, 'resourceOwner is required');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function requestedBy(value) {
|
|
113
|
+
if (!isExecutionPrincipalId(value)) {
|
|
114
|
+
throw new AgentHostRequestError(400, 'requestedBy is malformed');
|
|
115
|
+
}
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
118
|
+
function createFifoLock() {
|
|
119
|
+
let tail = Promise.resolve();
|
|
120
|
+
return async (operation) => {
|
|
121
|
+
const previous = tail;
|
|
122
|
+
let release = () => undefined;
|
|
123
|
+
tail = new Promise((resolve) => {
|
|
124
|
+
release = resolve;
|
|
125
|
+
});
|
|
126
|
+
await previous;
|
|
127
|
+
try {
|
|
128
|
+
return await operation();
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
release();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
91
135
|
function runRef(scope, input) {
|
|
92
136
|
const { agentId, threadId, resourceId, runId } = input;
|
|
93
|
-
if (
|
|
137
|
+
if (!isPathSafeId(agentId) ||
|
|
94
138
|
threadId !== scope.threadId ||
|
|
95
|
-
|
|
96
|
-
!
|
|
97
|
-
typeof runId !== 'string' ||
|
|
98
|
-
!tenantOwnsSaltedId(scope.tenantId, runId)) {
|
|
139
|
+
!isPathSafeId(resourceId) ||
|
|
140
|
+
!isPathSafeId(runId)) {
|
|
99
141
|
throw new AgentHostRequestError(404, 'run not found');
|
|
100
142
|
}
|
|
101
143
|
return { agentId, threadId: scope.threadId, resourceId, runId };
|
|
@@ -155,11 +197,13 @@ export function createThreadAgentHost(options) {
|
|
|
155
197
|
let runtime;
|
|
156
198
|
const executions = new Map();
|
|
157
199
|
let stableScope;
|
|
158
|
-
|
|
200
|
+
const withBindingLock = createFifoLock();
|
|
201
|
+
const withDispatchLock = createFifoLock();
|
|
202
|
+
const withRecoveryLock = createFifoLock();
|
|
159
203
|
const instanceScopeFor = (scope) => {
|
|
160
204
|
if (stableScope) {
|
|
161
205
|
if (stableScope.threadId !== scope.threadId ||
|
|
162
|
-
stableScope.
|
|
206
|
+
stableScope.deploymentTag !== scope.deploymentTag ||
|
|
163
207
|
stableScope.init !== scope.init) {
|
|
164
208
|
throw new Error('thread agent host cannot be shared across DO instances');
|
|
165
209
|
}
|
|
@@ -167,25 +211,11 @@ export function createThreadAgentHost(options) {
|
|
|
167
211
|
}
|
|
168
212
|
stableScope = Object.freeze({
|
|
169
213
|
threadId: scope.threadId,
|
|
170
|
-
|
|
214
|
+
deploymentTag: scope.deploymentTag,
|
|
171
215
|
init: scope.init,
|
|
172
216
|
});
|
|
173
217
|
return stableScope;
|
|
174
218
|
};
|
|
175
|
-
const withBindingLock = async (operation) => {
|
|
176
|
-
const previous = bindingTail;
|
|
177
|
-
let release = () => undefined;
|
|
178
|
-
bindingTail = new Promise((resolve) => {
|
|
179
|
-
release = resolve;
|
|
180
|
-
});
|
|
181
|
-
await previous;
|
|
182
|
-
try {
|
|
183
|
-
return await operation();
|
|
184
|
-
}
|
|
185
|
-
finally {
|
|
186
|
-
release();
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
219
|
const catalogFor = async (scope) => {
|
|
190
220
|
catalogPromise ??= Promise.resolve(options.buildModules(instanceScopeFor(scope)))
|
|
191
221
|
.then(createAgentModuleCatalog)
|
|
@@ -233,7 +263,8 @@ export function createThreadAgentHost(options) {
|
|
|
233
263
|
* roles at all: it must be declared in the agent's `allowedAutomation` for
|
|
234
264
|
* this precise entry path, AND survive the host's optional authorizer. Absent
|
|
235
265
|
* declaration denies — which is why a scheduled start of an agent that has
|
|
236
|
-
* not opted in fails here rather than executing
|
|
266
|
+
* not opted in fails here rather than executing through a role-bearing
|
|
267
|
+
* human identity.
|
|
237
268
|
*
|
|
238
269
|
* After that kind-specific gate, a configured server-owned resolver runs for
|
|
239
270
|
* EVERY granted entry: an agent that declares required permissions also
|
|
@@ -275,7 +306,7 @@ export function createThreadAgentHost(options) {
|
|
|
275
306
|
principal,
|
|
276
307
|
agentId,
|
|
277
308
|
entryPath: entry,
|
|
278
|
-
|
|
309
|
+
deploymentTag: scope.deploymentTag,
|
|
279
310
|
threadId: scope.threadId,
|
|
280
311
|
})) ?? true;
|
|
281
312
|
granted = hostAllows;
|
|
@@ -307,7 +338,9 @@ export function createThreadAgentHost(options) {
|
|
|
307
338
|
reason: 'permission resolution failed',
|
|
308
339
|
detail: {
|
|
309
340
|
agentId,
|
|
310
|
-
|
|
341
|
+
...(scope.deploymentTag !== undefined
|
|
342
|
+
? { tenantId: scope.deploymentTag }
|
|
343
|
+
: {}),
|
|
311
344
|
threadId: scope.threadId,
|
|
312
345
|
entryPath: entry,
|
|
313
346
|
...principalAuditFields(principal),
|
|
@@ -340,7 +373,9 @@ export function createThreadAgentHost(options) {
|
|
|
340
373
|
...(reason !== undefined ? { reason } : {}),
|
|
341
374
|
detail: {
|
|
342
375
|
agentId,
|
|
343
|
-
|
|
376
|
+
...(scope.deploymentTag !== undefined
|
|
377
|
+
? { tenantId: scope.deploymentTag }
|
|
378
|
+
: {}),
|
|
344
379
|
threadId: scope.threadId,
|
|
345
380
|
entryPath: entry,
|
|
346
381
|
...principalAuditFields(principal),
|
|
@@ -357,6 +392,134 @@ export function createThreadAgentHost(options) {
|
|
|
357
392
|
};
|
|
358
393
|
const readBinding = () => readAgentThreadBinding(options.stateStorage());
|
|
359
394
|
const readRun = (runId) => readAgentRunRecord(options.stateStorage(), runId);
|
|
395
|
+
const findBlockingRun = async (scopeRuntime) => {
|
|
396
|
+
const storage = options.stateStorage();
|
|
397
|
+
const records = await storage.list({
|
|
398
|
+
prefix: AGENT_RUN_STORAGE_KEY_PREFIX,
|
|
399
|
+
});
|
|
400
|
+
for (const key of records.keys()) {
|
|
401
|
+
const runId = key.slice(AGENT_RUN_STORAGE_KEY_PREFIX.length);
|
|
402
|
+
const record = await readRun(runId);
|
|
403
|
+
if (!record)
|
|
404
|
+
continue;
|
|
405
|
+
const summary = await scopeRuntime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, runId);
|
|
406
|
+
if (executions.has(runId) ||
|
|
407
|
+
(summary !== null && !isTerminalRunStatus(summary.status))) {
|
|
408
|
+
return { runId, principal: record.principal };
|
|
409
|
+
}
|
|
410
|
+
const recovery = await storage.get(AGENT_OWNER_RECOVERY_PREFIX + runId);
|
|
411
|
+
if (recovery)
|
|
412
|
+
return { runId, principal: record.principal };
|
|
413
|
+
await deleteAgentRunRecord(storage, runId);
|
|
414
|
+
}
|
|
415
|
+
return undefined;
|
|
416
|
+
};
|
|
417
|
+
const ownerRecoveryKey = (runId) => AGENT_OWNER_RECOVERY_PREFIX + runId;
|
|
418
|
+
const resolveStartSource = async (scope, ref, entry, threaded, scheduleId, dispatchId) => {
|
|
419
|
+
const ownership = options.resourceAccess();
|
|
420
|
+
if (entry !== 'schedule.fire' &&
|
|
421
|
+
(scheduleId !== undefined || dispatchId !== undefined)) {
|
|
422
|
+
throw new AgentHostRequestError(404, 'run not found');
|
|
423
|
+
}
|
|
424
|
+
if (entry === 'schedule.fire') {
|
|
425
|
+
if (!isPathSafeId(scheduleId) || !isPathSafeId(dispatchId)) {
|
|
426
|
+
throw new AgentHostRequestError(404, 'run not found');
|
|
427
|
+
}
|
|
428
|
+
const schedules = options.scheduleSource?.();
|
|
429
|
+
const source = schedules
|
|
430
|
+
? await resolveScheduleStartOwner(schedules, ownership, scheduleId, dispatchId, ref.runId, threaded
|
|
431
|
+
? {
|
|
432
|
+
type: 'agent',
|
|
433
|
+
mode: 'threaded-wake',
|
|
434
|
+
agentId: ref.agentId,
|
|
435
|
+
threadId: ref.threadId,
|
|
436
|
+
resourceId: ref.resourceId,
|
|
437
|
+
}
|
|
438
|
+
: {
|
|
439
|
+
type: 'agent',
|
|
440
|
+
mode: 'threadless-start',
|
|
441
|
+
agentId: ref.agentId,
|
|
442
|
+
})
|
|
443
|
+
: undefined;
|
|
444
|
+
if (!source)
|
|
445
|
+
throw new AgentHostRequestError(404, 'run not found');
|
|
446
|
+
return source;
|
|
447
|
+
}
|
|
448
|
+
if (entry === 'http.start') {
|
|
449
|
+
return { owner: principalOwner(scope.principal) };
|
|
450
|
+
}
|
|
451
|
+
if (!threaded)
|
|
452
|
+
throw new AgentHostRequestError(404, 'run not found');
|
|
453
|
+
const [threadOwner, resourceOwner] = await Promise.all([
|
|
454
|
+
ownership.owner('thread', ref.threadId),
|
|
455
|
+
ownership.owner('resource', ref.resourceId),
|
|
456
|
+
]);
|
|
457
|
+
if (!threadOwner ||
|
|
458
|
+
!resourceOwner ||
|
|
459
|
+
threadOwner.kind !== resourceOwner.kind ||
|
|
460
|
+
threadOwner.id !== resourceOwner.id) {
|
|
461
|
+
throw new AgentHostRequestError(404, 'run not found');
|
|
462
|
+
}
|
|
463
|
+
return { owner: threadOwner };
|
|
464
|
+
};
|
|
465
|
+
const ensureOwnerRecoveryAlarm = async (storage) => {
|
|
466
|
+
const scheduled = Date.now() + AGENT_OWNER_RECOVERY_DELAY_MS;
|
|
467
|
+
const existing = await storage.getAlarm();
|
|
468
|
+
if (existing === null || existing > scheduled) {
|
|
469
|
+
await storage.setAlarm(scheduled);
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
const armOwnerRecovery = async (recovery) => withRecoveryLock(async () => {
|
|
473
|
+
const storage = options.stateStorage();
|
|
474
|
+
await ensureOwnerRecoveryAlarm(storage);
|
|
475
|
+
await storage.put(ownerRecoveryKey(recovery.runId), recovery);
|
|
476
|
+
await ensureOwnerRecoveryAlarm(storage);
|
|
477
|
+
});
|
|
478
|
+
const clearOwnerRecovery = async (recovery) => withRecoveryLock(async () => {
|
|
479
|
+
const storage = options.stateStorage();
|
|
480
|
+
const key = ownerRecoveryKey(recovery.runId);
|
|
481
|
+
const current = await storage.get(key);
|
|
482
|
+
if (current?.token === recovery.token)
|
|
483
|
+
await storage.delete(key);
|
|
484
|
+
});
|
|
485
|
+
const releaseEphemeralOwnerClaims = async (recovery) => {
|
|
486
|
+
await Promise.all([
|
|
487
|
+
options
|
|
488
|
+
.resourceAccess()
|
|
489
|
+
.release('resource', recovery.resourceId, recovery.owner),
|
|
490
|
+
options
|
|
491
|
+
.resourceAccess()
|
|
492
|
+
.release('thread', recovery.threadId, recovery.owner),
|
|
493
|
+
]);
|
|
494
|
+
};
|
|
495
|
+
const finalizeOwnerRecoveryBestEffort = async (recovery, summary) => {
|
|
496
|
+
try {
|
|
497
|
+
await withRecoveryLock(async () => {
|
|
498
|
+
const storage = options.stateStorage();
|
|
499
|
+
await options.resourceAccess().settleReservation(recovery.token, []);
|
|
500
|
+
if (!recovery.threaded && !isTerminalRunStatus(summary.status)) {
|
|
501
|
+
await ensureOwnerRecoveryAlarm(storage);
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (!recovery.threaded) {
|
|
505
|
+
await releaseEphemeralOwnerClaims(recovery);
|
|
506
|
+
}
|
|
507
|
+
const key = ownerRecoveryKey(recovery.runId);
|
|
508
|
+
const current = await storage.get(key);
|
|
509
|
+
if (current?.token === recovery.token)
|
|
510
|
+
await storage.delete(key);
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
catch (error) {
|
|
514
|
+
console.error('agent owner recovery cleanup failed', error);
|
|
515
|
+
try {
|
|
516
|
+
await withRecoveryLock(() => ensureOwnerRecoveryAlarm(options.stateStorage()));
|
|
517
|
+
}
|
|
518
|
+
catch (alarmError) {
|
|
519
|
+
console.error('agent owner recovery rearm failed', alarmError);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
};
|
|
360
523
|
const withExecution = async (execution, operation) => {
|
|
361
524
|
if (executions.has(execution.runId)) {
|
|
362
525
|
throw new AgentHostRequestError(409, `run '${execution.runId}' already has an active operation`);
|
|
@@ -372,8 +535,8 @@ export function createThreadAgentHost(options) {
|
|
|
372
535
|
// Reconciling approvals is trusted platform work with no person behind it.
|
|
373
536
|
// It used to mint role:'operator', which is why an approval bridge looked
|
|
374
537
|
// indistinguishable from a human operator in the audit trail. The bridge
|
|
375
|
-
// mints its own principal from this id
|
|
376
|
-
const
|
|
538
|
+
// mints its own principal from this id.
|
|
539
|
+
const systemPrincipalId = options.systemPrincipalId ?? 'flowsafe-system';
|
|
377
540
|
// Deliberately NOT vouched. Its only consumer projects it to an ApprovalActor
|
|
378
541
|
// for a role-gated READ, which grants nothing an automated principal does not
|
|
379
542
|
// already have — so calling the trust assertion here would assert trust that
|
|
@@ -384,27 +547,29 @@ export function createThreadAgentHost(options) {
|
|
|
384
547
|
// list() emits no audit event, so this string reaches nothing. It is not
|
|
385
548
|
// shared with the bridge's RECONCILE_PURPOSE for that reason — there is no
|
|
386
549
|
// provenance here to drift.
|
|
387
|
-
const systemPrincipal = (
|
|
550
|
+
const systemPrincipal = () => ({
|
|
388
551
|
kind: 'system',
|
|
389
|
-
id:
|
|
390
|
-
tenantId: scope.tenantId,
|
|
552
|
+
id: systemPrincipalId,
|
|
391
553
|
purpose: 'approval-suspension-reconcile',
|
|
392
554
|
});
|
|
393
555
|
const currentApprovals = async (scope, summary, principal, agentId, resourceId) => {
|
|
394
556
|
if (summary.status !== 'suspended')
|
|
395
557
|
return [];
|
|
558
|
+
if (!summary.requestedBy) {
|
|
559
|
+
throw new Error('suspended agent run has no durable requester provenance');
|
|
560
|
+
}
|
|
396
561
|
const service = options.approvalService(instanceScopeFor(scope));
|
|
397
|
-
await reconcileApprovalsForSummary(service, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, summary,
|
|
562
|
+
await reconcileApprovalsForSummary(service, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, summary, systemPrincipalId, {
|
|
398
563
|
kind: 'agent-thread',
|
|
399
564
|
agentId,
|
|
400
565
|
threadId: scope.threadId,
|
|
401
566
|
resourceId,
|
|
402
567
|
principal,
|
|
403
|
-
},
|
|
568
|
+
}, summary.requestedBy);
|
|
404
569
|
const records = await service.list({
|
|
405
570
|
workflowId: DURABLE_AGENTIC_LOOP_WORKFLOW_ID,
|
|
406
571
|
runId: summary.runId,
|
|
407
|
-
}, principalActor(systemPrincipal(
|
|
572
|
+
}, principalActor(systemPrincipal()));
|
|
408
573
|
const keys = new Set((summary.suspended ?? []).map((path) => path.join('.')));
|
|
409
574
|
return records.filter((record) => {
|
|
410
575
|
const key = record.stepPath?.join('.');
|
|
@@ -424,7 +589,18 @@ export function createThreadAgentHost(options) {
|
|
|
424
589
|
};
|
|
425
590
|
if (summary.status !== 'suspended')
|
|
426
591
|
return base;
|
|
427
|
-
|
|
592
|
+
let approvals = [];
|
|
593
|
+
try {
|
|
594
|
+
approvals = await currentApprovals(scope, summary, principal, ref.agentId, ref.resourceId);
|
|
595
|
+
}
|
|
596
|
+
catch (error) {
|
|
597
|
+
console.error(JSON.stringify({
|
|
598
|
+
type: 'agent-approval-filing-error',
|
|
599
|
+
agentId: ref.agentId,
|
|
600
|
+
runId: ref.runId,
|
|
601
|
+
error: error instanceof Error ? error.message : String(error),
|
|
602
|
+
}));
|
|
603
|
+
}
|
|
428
604
|
return {
|
|
429
605
|
...base,
|
|
430
606
|
...(approvals[0] ? { approval: approvals[0] } : {}),
|
|
@@ -461,6 +637,7 @@ export function createThreadAgentHost(options) {
|
|
|
461
637
|
safeContext: sanitizeStoredAgentContext(requestContext),
|
|
462
638
|
};
|
|
463
639
|
};
|
|
640
|
+
let recoverOwner;
|
|
464
641
|
const statusFor = async (scope, ref, knownThreaded) => {
|
|
465
642
|
const binding = await readBinding();
|
|
466
643
|
const current = await runtimeFor(scope);
|
|
@@ -482,18 +659,81 @@ export function createThreadAgentHost(options) {
|
|
|
482
659
|
if (summary.status === 'suspended' && !stored) {
|
|
483
660
|
throw new AgentHostRequestError(409, 'suspended agent run has no recoverable execution principal');
|
|
484
661
|
}
|
|
485
|
-
// Inert today — a suspended run without a stored record already threw 409
|
|
486
|
-
// above, and only a suspended run consults this. Kept as the scope's
|
|
487
|
-
// PRINCIPAL rather than its actor so that if those two conditions are ever
|
|
488
|
-
// decoupled, the fallback still cannot relabel an automated run as whoever
|
|
489
|
-
// happened to poll its status.
|
|
490
662
|
const principal = stored?.principal ?? scope.principal;
|
|
491
663
|
const result = await envelopeFor(scope, ref, principal, summary);
|
|
492
664
|
if (isTerminalRunStatus(summary.status) && stored) {
|
|
493
665
|
await deleteAgentRunRecord(options.stateStorage(), ref.runId);
|
|
494
666
|
}
|
|
667
|
+
if (isTerminalRunStatus(summary.status)) {
|
|
668
|
+
const key = ownerRecoveryKey(ref.runId);
|
|
669
|
+
const recovery = await options
|
|
670
|
+
.stateStorage()
|
|
671
|
+
.get(key);
|
|
672
|
+
if (recovery) {
|
|
673
|
+
await recoverOwner(scope.init.runtime, scope.threadId, key, recovery, true);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
495
676
|
return result;
|
|
496
677
|
};
|
|
678
|
+
recoverOwner = async (scopeRuntime, threadId, key, stored, ignoreActive = false) => withBindingLock(() => withRecoveryLock(async () => {
|
|
679
|
+
if (stored?.version !== 1 ||
|
|
680
|
+
stored.threadId !== threadId ||
|
|
681
|
+
!isPathSafeId(stored.agentId) ||
|
|
682
|
+
!isPathSafeId(stored.resourceId) ||
|
|
683
|
+
!isPathSafeId(stored.runId) ||
|
|
684
|
+
!isPathSafeId(stored.token) ||
|
|
685
|
+
ownerRecoveryKey(stored.runId) !== key ||
|
|
686
|
+
typeof stored.threaded !== 'boolean' ||
|
|
687
|
+
typeof stored.bindingPreexisting !== 'boolean') {
|
|
688
|
+
throw new Error('stored agent owner recovery is malformed');
|
|
689
|
+
}
|
|
690
|
+
resourceOwner(stored.owner);
|
|
691
|
+
const storage = options.stateStorage();
|
|
692
|
+
const currentRecovery = await storage.get(key);
|
|
693
|
+
if (currentRecovery?.token !== stored.token)
|
|
694
|
+
return 'pending';
|
|
695
|
+
if (!ignoreActive && executions.has(stored.runId))
|
|
696
|
+
return 'pending';
|
|
697
|
+
const summary = await scopeRuntime.recoverStartAttempt(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, stored.runId, stored.token);
|
|
698
|
+
const [binding, record] = await Promise.all([
|
|
699
|
+
readBinding(),
|
|
700
|
+
readRun(stored.runId),
|
|
701
|
+
]);
|
|
702
|
+
const bindingMatches = binding?.agentId === stored.agentId &&
|
|
703
|
+
binding.resourceId === stored.resourceId;
|
|
704
|
+
if (record && (!summary || isTerminalRunStatus(summary.status))) {
|
|
705
|
+
await deleteAgentRunRecord(storage, stored.runId);
|
|
706
|
+
}
|
|
707
|
+
if (!summary && !stored.bindingPreexisting && bindingMatches) {
|
|
708
|
+
await deleteAgentThreadBinding(storage, {
|
|
709
|
+
agentId: stored.agentId,
|
|
710
|
+
resourceId: stored.resourceId,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
if (summary) {
|
|
714
|
+
await options.resourceAccess().settleReservation(stored.token, []);
|
|
715
|
+
if (!stored.threaded && !isTerminalRunStatus(summary.status)) {
|
|
716
|
+
await ensureOwnerRecoveryAlarm(storage);
|
|
717
|
+
return 'pending';
|
|
718
|
+
}
|
|
719
|
+
if (!stored.threaded)
|
|
720
|
+
await releaseEphemeralOwnerClaims(stored);
|
|
721
|
+
const current = await storage.get(key);
|
|
722
|
+
if (current?.token === stored.token)
|
|
723
|
+
await storage.delete(key);
|
|
724
|
+
return 'cleared';
|
|
725
|
+
}
|
|
726
|
+
const release = [{ kind: 'run', resourceId: stored.runId }];
|
|
727
|
+
const retainThread = stored.threaded && stored.bindingPreexisting && bindingMatches;
|
|
728
|
+
if (!retainThread) {
|
|
729
|
+
release.push({ kind: 'resource', resourceId: stored.resourceId }, { kind: 'thread', resourceId: stored.threadId });
|
|
730
|
+
}
|
|
731
|
+
await options.resourceAccess().settleReservation(stored.token, release);
|
|
732
|
+
const current = await storage.get(key);
|
|
733
|
+
if (current?.token === stored.token)
|
|
734
|
+
await storage.delete(key);
|
|
735
|
+
return 'cleared';
|
|
736
|
+
}));
|
|
497
737
|
const host = {
|
|
498
738
|
requestContextForRun: (base) => async (workflowId, runId, leg) => {
|
|
499
739
|
const values = base ? await base(workflowId, runId, leg) : undefined;
|
|
@@ -506,75 +746,177 @@ export function createThreadAgentHost(options) {
|
|
|
506
746
|
}
|
|
507
747
|
: values;
|
|
508
748
|
},
|
|
749
|
+
serializeDispatch: withDispatchLock,
|
|
750
|
+
blockingRun: (scope) => findBlockingRun(scope.init.runtime),
|
|
751
|
+
scheduleDispatchStatus: async (scope, input) => {
|
|
752
|
+
const ref = runRef(scope, {
|
|
753
|
+
...input,
|
|
754
|
+
threadId: scope.threadId,
|
|
755
|
+
});
|
|
756
|
+
const key = ownerRecoveryKey(ref.runId);
|
|
757
|
+
const recovery = await options
|
|
758
|
+
.stateStorage()
|
|
759
|
+
.get(key);
|
|
760
|
+
if (recovery) {
|
|
761
|
+
await recoverOwner(scope.init.runtime, scope.threadId, key, recovery, true);
|
|
762
|
+
}
|
|
763
|
+
const summary = await scope.init.runtime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId);
|
|
764
|
+
if (!summary)
|
|
765
|
+
return undefined;
|
|
766
|
+
return (await statusFor(scope, ref)).summary;
|
|
767
|
+
},
|
|
768
|
+
recoverOwnership: (scopeRuntime, threadId) => withDispatchLock(async () => {
|
|
769
|
+
const storage = options.stateStorage();
|
|
770
|
+
try {
|
|
771
|
+
const pending = await storage.list({
|
|
772
|
+
prefix: AGENT_OWNER_RECOVERY_PREFIX,
|
|
773
|
+
});
|
|
774
|
+
for (const [key, stored] of pending) {
|
|
775
|
+
await recoverOwner(scopeRuntime, threadId, key, stored);
|
|
776
|
+
}
|
|
777
|
+
await withRecoveryLock(async () => {
|
|
778
|
+
const remaining = await storage.list({
|
|
779
|
+
prefix: AGENT_OWNER_RECOVERY_PREFIX,
|
|
780
|
+
});
|
|
781
|
+
if (remaining.size > 0) {
|
|
782
|
+
await ensureOwnerRecoveryAlarm(storage);
|
|
783
|
+
}
|
|
784
|
+
else {
|
|
785
|
+
await storage.deleteAlarm();
|
|
786
|
+
}
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
catch (error) {
|
|
790
|
+
await withRecoveryLock(() => ensureOwnerRecoveryAlarm(storage));
|
|
791
|
+
throw error;
|
|
792
|
+
}
|
|
793
|
+
}),
|
|
509
794
|
start: async (scope, input) => {
|
|
510
795
|
const ref = runRef(scope, input);
|
|
511
|
-
const
|
|
512
|
-
const
|
|
513
|
-
|
|
796
|
+
const entry = entryPath(input.entryPath);
|
|
797
|
+
const threaded = input.threaded !== false;
|
|
798
|
+
const source = await resolveStartSource(scope, ref, entry, threaded, input.scheduleId, input.dispatchId);
|
|
799
|
+
const hasPrompt = source.target === undefined && input.prompt !== undefined;
|
|
800
|
+
const hasMessages = source.target === undefined && input.messages !== undefined;
|
|
801
|
+
if ((source.target === undefined && hasPrompt === hasMessages) ||
|
|
514
802
|
(hasPrompt &&
|
|
515
803
|
(typeof input.prompt !== 'string' ||
|
|
516
804
|
input.prompt.trim() === '' ||
|
|
517
805
|
input.prompt.length > 10_000))) {
|
|
518
806
|
throw new AgentHostRequestError(400, 'exactly one valid prompt or messages input is required');
|
|
519
807
|
}
|
|
520
|
-
const messages =
|
|
808
|
+
const messages = source.target
|
|
809
|
+
? threaded && input.messages !== undefined
|
|
810
|
+
? input.messages
|
|
811
|
+
: source.target.prompt
|
|
812
|
+
: (input.messages ?? input.prompt);
|
|
521
813
|
if (messages === undefined) {
|
|
522
814
|
throw new AgentHostRequestError(400, 'agent input is required');
|
|
523
815
|
}
|
|
524
|
-
const
|
|
816
|
+
const resolvedProviderOptions = source.target
|
|
817
|
+
? source.target.providerOptions
|
|
818
|
+
: input.providerOptions;
|
|
819
|
+
const owner = source.owner;
|
|
525
820
|
const { current, module, principalPermissions } = await authorize(scope, ref.agentId, entry, scope.principal);
|
|
526
|
-
if (ref.resourceId !==
|
|
821
|
+
if (ref.resourceId !== resourceIdFromKey(scope.threadId)) {
|
|
527
822
|
throw new AgentHostRequestError(404, 'run not found');
|
|
528
823
|
}
|
|
529
824
|
const execution = {
|
|
530
825
|
agentId: ref.agentId,
|
|
826
|
+
deploymentTag: scope.deploymentTag,
|
|
531
827
|
principal: scope.principal,
|
|
532
828
|
threadId: scope.threadId,
|
|
533
829
|
resourceId: ref.resourceId,
|
|
534
830
|
runId: ref.runId,
|
|
535
831
|
entryPath: entry,
|
|
536
832
|
principalPermissions,
|
|
537
|
-
safeContext: safeContext(
|
|
833
|
+
safeContext: safeContext(source.target
|
|
834
|
+
? scheduleAgentContext(source.target)
|
|
835
|
+
: input.safeContext),
|
|
538
836
|
};
|
|
539
837
|
const durable = current.agents.get(module.meta.id);
|
|
540
838
|
if (!durable)
|
|
541
839
|
throw new Error('guarded agent was not registered');
|
|
542
840
|
return withExecution(execution, async () => {
|
|
841
|
+
const recoveryKey = ownerRecoveryKey(ref.runId);
|
|
842
|
+
const pending = await options
|
|
843
|
+
.stateStorage()
|
|
844
|
+
.get(recoveryKey);
|
|
845
|
+
if (pending) {
|
|
846
|
+
await recoverOwner(scope.init.runtime, scope.threadId, recoveryKey, pending, true);
|
|
847
|
+
}
|
|
543
848
|
const existingRecord = await readRun(ref.runId);
|
|
544
849
|
const existingSummary = await scope.init.runtime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId);
|
|
545
850
|
if (existingRecord || existingSummary) {
|
|
546
851
|
throw new AgentHostRequestError(409, `run '${ref.runId}' already exists`);
|
|
547
852
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
853
|
+
const claims = [
|
|
854
|
+
{ kind: 'thread', resourceId: scope.threadId },
|
|
855
|
+
{ kind: 'resource', resourceId: ref.resourceId },
|
|
856
|
+
{ kind: 'run', resourceId: ref.runId },
|
|
857
|
+
];
|
|
858
|
+
const stored = {
|
|
859
|
+
version: 2,
|
|
860
|
+
agentId: ref.agentId,
|
|
861
|
+
principal: scope.principal,
|
|
862
|
+
originEntryPath: entry,
|
|
863
|
+
};
|
|
864
|
+
const recovery = await withBindingLock(async () => {
|
|
865
|
+
const existing = await readBinding();
|
|
866
|
+
if (!threaded && existing) {
|
|
867
|
+
throw new AgentHostRequestError(409, 'unthreaded starts require an unbound object');
|
|
868
|
+
}
|
|
869
|
+
if (threaded) {
|
|
551
870
|
const bindingMatches = existing?.agentId === ref.agentId &&
|
|
552
871
|
existing.resourceId === ref.resourceId;
|
|
553
872
|
if (entry === 'schedule.fire') {
|
|
554
873
|
if (!bindingMatches) {
|
|
555
874
|
throw new AgentHostRequestError(404, 'run not found');
|
|
556
875
|
}
|
|
557
|
-
return;
|
|
558
876
|
}
|
|
559
|
-
if (existing && !bindingMatches) {
|
|
877
|
+
else if (existing && !bindingMatches) {
|
|
560
878
|
throw new AgentHostRequestError(409, 'thread is bound to another agent');
|
|
561
879
|
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
880
|
+
}
|
|
881
|
+
const blocking = await findBlockingRun(scope.init.runtime);
|
|
882
|
+
if (blocking && blocking.runId !== ref.runId) {
|
|
883
|
+
throw new AgentHostRequestError(409, `thread is blocked by run '${blocking.runId}'`);
|
|
884
|
+
}
|
|
885
|
+
const recovery = {
|
|
886
|
+
version: 1,
|
|
887
|
+
agentId: ref.agentId,
|
|
888
|
+
threadId: scope.threadId,
|
|
889
|
+
resourceId: ref.resourceId,
|
|
890
|
+
runId: ref.runId,
|
|
891
|
+
owner,
|
|
892
|
+
token: crypto.randomUUID(),
|
|
893
|
+
threaded,
|
|
894
|
+
bindingPreexisting: existing !== undefined,
|
|
895
|
+
};
|
|
896
|
+
if (!threaded &&
|
|
897
|
+
(await Promise.all(claims.map((claim) => options.resourceAccess().owner(claim.kind, claim.resourceId)))).some((registered) => registered !== undefined)) {
|
|
898
|
+
throw new AgentHostRequestError(404, 'run not found');
|
|
899
|
+
}
|
|
900
|
+
await armOwnerRecovery(recovery);
|
|
901
|
+
if (!(await options
|
|
902
|
+
.resourceAccess()
|
|
903
|
+
.reserveAll(claims, owner, recovery.token))) {
|
|
904
|
+
await options
|
|
905
|
+
.resourceAccess()
|
|
906
|
+
.settleReservation(recovery.token, claims);
|
|
907
|
+
await clearOwnerRecovery(recovery);
|
|
908
|
+
throw new AgentHostRequestError(404, 'run not found');
|
|
909
|
+
}
|
|
910
|
+
if (threaded && !existing) {
|
|
911
|
+
await bindAgentThread(options.stateStorage(), {
|
|
912
|
+
version: 1,
|
|
913
|
+
agentId: ref.agentId,
|
|
914
|
+
resourceId: ref.resourceId,
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
await writeAgentRunRecord(options.stateStorage(), ref.runId, stored);
|
|
918
|
+
return recovery;
|
|
919
|
+
});
|
|
578
920
|
try {
|
|
579
921
|
await durable.streamUntilPersisted(messages, {
|
|
580
922
|
runId: ref.runId,
|
|
@@ -589,7 +931,10 @@ export function createThreadAgentHost(options) {
|
|
|
589
931
|
: {}),
|
|
590
932
|
maxSteps: module.agent.maxSteps,
|
|
591
933
|
disableBackgroundTasks: true,
|
|
592
|
-
|
|
934
|
+
...(resolvedProviderOptions !== undefined
|
|
935
|
+
? { providerOptions: providerOptions(resolvedProviderOptions) }
|
|
936
|
+
: {}),
|
|
937
|
+
}, scope.principal.id, scope.principal.kind, recovery.token);
|
|
593
938
|
const summary = await scope.init.runtime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId);
|
|
594
939
|
if (!summary)
|
|
595
940
|
throw new Error('agent run did not persist a summary');
|
|
@@ -597,19 +942,41 @@ export function createThreadAgentHost(options) {
|
|
|
597
942
|
if (isTerminalRunStatus(result.summary.status)) {
|
|
598
943
|
await deleteAgentRunRecord(options.stateStorage(), ref.runId);
|
|
599
944
|
}
|
|
945
|
+
await finalizeOwnerRecoveryBestEffort(recovery, summary);
|
|
600
946
|
return result;
|
|
601
947
|
}
|
|
602
948
|
catch (error) {
|
|
603
949
|
let summary;
|
|
604
950
|
try {
|
|
605
|
-
summary = await scope.init.runtime.
|
|
951
|
+
summary = await scope.init.runtime.recoverStartAttempt(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId, recovery.token);
|
|
606
952
|
}
|
|
607
953
|
catch {
|
|
608
954
|
// Unknown authoritative state: retain metadata and fail closed.
|
|
609
955
|
}
|
|
610
|
-
if (
|
|
956
|
+
if (summary === null ||
|
|
957
|
+
(summary && isTerminalRunStatus(summary.status))) {
|
|
611
958
|
await deleteAgentRunRecord(options.stateStorage(), ref.runId);
|
|
612
959
|
}
|
|
960
|
+
if (summary) {
|
|
961
|
+
await finalizeOwnerRecoveryBestEffort(recovery, summary);
|
|
962
|
+
return envelopeFor(scope, ref, scope.principal, summary);
|
|
963
|
+
}
|
|
964
|
+
else if (summary === null) {
|
|
965
|
+
try {
|
|
966
|
+
await recoverOwner(scope.init.runtime, scope.threadId, recoveryKey, recovery, true);
|
|
967
|
+
}
|
|
968
|
+
catch (recoveryError) {
|
|
969
|
+
console.error('agent owner recovery failed', recoveryError);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
else {
|
|
973
|
+
try {
|
|
974
|
+
await withRecoveryLock(() => ensureOwnerRecoveryAlarm(options.stateStorage()));
|
|
975
|
+
}
|
|
976
|
+
catch (alarmError) {
|
|
977
|
+
console.error('agent owner recovery rearm failed', alarmError);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
613
980
|
throw error;
|
|
614
981
|
}
|
|
615
982
|
});
|
|
@@ -618,7 +985,7 @@ export function createThreadAgentHost(options) {
|
|
|
618
985
|
const binding = await readBinding();
|
|
619
986
|
if (!binding)
|
|
620
987
|
throw new AgentHostRequestError(404, 'agent not found');
|
|
621
|
-
if (binding.resourceId !==
|
|
988
|
+
if (binding.resourceId !== resourceIdFromKey(scope.threadId)) {
|
|
622
989
|
throw new AgentHostRequestError(404, 'agent not found');
|
|
623
990
|
}
|
|
624
991
|
if (input.agentId && input.agentId !== binding.agentId) {
|
|
@@ -634,23 +1001,62 @@ export function createThreadAgentHost(options) {
|
|
|
634
1001
|
durableAgent,
|
|
635
1002
|
};
|
|
636
1003
|
},
|
|
637
|
-
route:
|
|
1004
|
+
route: (request, scope) => withDispatchLock(async () => {
|
|
638
1005
|
const url = new URL(request.url);
|
|
639
1006
|
if (!url.pathname.startsWith(AGENT_HOST_ROUTE_PREFIX))
|
|
640
1007
|
return null;
|
|
1008
|
+
if (request.method === 'GET' &&
|
|
1009
|
+
url.pathname === `${AGENT_HOST_ROUTE_PREFIX}/binding`) {
|
|
1010
|
+
const resourceId = url.searchParams.get('resourceId');
|
|
1011
|
+
const agentId = url.searchParams.get('agentId');
|
|
1012
|
+
const binding = await readBinding();
|
|
1013
|
+
if (!resourceId ||
|
|
1014
|
+
resourceId !== resourceIdFromKey(scope.threadId) ||
|
|
1015
|
+
binding?.resourceId !== resourceId ||
|
|
1016
|
+
(agentId !== null && binding.agentId !== agentId)) {
|
|
1017
|
+
throw new AgentHostRequestError(404, 'agent not found');
|
|
1018
|
+
}
|
|
1019
|
+
return json({ bound: true });
|
|
1020
|
+
}
|
|
641
1021
|
if (request.method === 'POST' &&
|
|
642
1022
|
url.pathname === `${AGENT_HOST_ROUTE_PREFIX}/start`) {
|
|
643
1023
|
const body = await objectBody(request);
|
|
1024
|
+
if ('resourceOwner' in body || 'requestedBy' in body) {
|
|
1025
|
+
throw new AgentHostRequestError(400, 'start owner and requester are derived from trusted provenance');
|
|
1026
|
+
}
|
|
644
1027
|
const ref = runRef(scope, body);
|
|
645
|
-
|
|
1028
|
+
const requestedEntry = entryPath(body.entryPath);
|
|
1029
|
+
if (requestedEntry !== 'schedule.fire' &&
|
|
1030
|
+
typeof body.prompt !== 'string') {
|
|
646
1031
|
throw new AgentHostRequestError(400, 'prompt is required');
|
|
647
1032
|
}
|
|
648
1033
|
return json(await host.start(scope, {
|
|
649
1034
|
...ref,
|
|
650
|
-
|
|
651
|
-
|
|
1035
|
+
...(typeof body.prompt === 'string'
|
|
1036
|
+
? { prompt: body.prompt }
|
|
1037
|
+
: {}),
|
|
1038
|
+
entryPath: requestedEntry,
|
|
652
1039
|
threaded: body.threaded !== false,
|
|
653
1040
|
safeContext: safeContext(body.safeContext),
|
|
1041
|
+
providerOptions: providerOptions(body.providerOptions),
|
|
1042
|
+
...(body.scheduleId !== undefined
|
|
1043
|
+
? {
|
|
1044
|
+
scheduleId: isPathSafeId(body.scheduleId)
|
|
1045
|
+
? body.scheduleId
|
|
1046
|
+
: (() => {
|
|
1047
|
+
throw new AgentHostRequestError(404, 'run not found');
|
|
1048
|
+
})(),
|
|
1049
|
+
}
|
|
1050
|
+
: {}),
|
|
1051
|
+
...(body.dispatchId !== undefined
|
|
1052
|
+
? {
|
|
1053
|
+
dispatchId: isPathSafeId(body.dispatchId)
|
|
1054
|
+
? body.dispatchId
|
|
1055
|
+
: (() => {
|
|
1056
|
+
throw new AgentHostRequestError(404, 'run not found');
|
|
1057
|
+
})(),
|
|
1058
|
+
}
|
|
1059
|
+
: {}),
|
|
654
1060
|
}));
|
|
655
1061
|
}
|
|
656
1062
|
if (request.method === 'POST' &&
|
|
@@ -669,6 +1075,7 @@ export function createThreadAgentHost(options) {
|
|
|
669
1075
|
const durable = current.agents.get(module.meta.id);
|
|
670
1076
|
if (!durable)
|
|
671
1077
|
throw new Error('guarded agent was not registered');
|
|
1078
|
+
const requesterId = requestedBy(body.requestedBy);
|
|
672
1079
|
const step = typeof body.step === 'string' ||
|
|
673
1080
|
(Array.isArray(body.step) &&
|
|
674
1081
|
body.step.every((part) => typeof part === 'string'))
|
|
@@ -676,6 +1083,7 @@ export function createThreadAgentHost(options) {
|
|
|
676
1083
|
: undefined;
|
|
677
1084
|
const execution = {
|
|
678
1085
|
agentId: ref.agentId,
|
|
1086
|
+
deploymentTag: scope.deploymentTag,
|
|
679
1087
|
principal: stored.principal,
|
|
680
1088
|
threadId: scope.threadId,
|
|
681
1089
|
resourceId: ref.resourceId,
|
|
@@ -687,22 +1095,31 @@ export function createThreadAgentHost(options) {
|
|
|
687
1095
|
principalPermissions,
|
|
688
1096
|
safeContext: snapshotExecution.safeContext,
|
|
689
1097
|
};
|
|
690
|
-
const summary = await withExecution(execution, () =>
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
? {
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
1098
|
+
const summary = await withExecution(execution, async () => {
|
|
1099
|
+
return durable.resumeViaRuntime({
|
|
1100
|
+
runId: ref.runId,
|
|
1101
|
+
requestedBy: requesterId,
|
|
1102
|
+
...(step !== undefined ? { step } : {}),
|
|
1103
|
+
...('resumeData' in body ? { resumeData: body.resumeData } : {}),
|
|
1104
|
+
...(snapshotExecution.threaded
|
|
1105
|
+
? {
|
|
1106
|
+
memory: {
|
|
1107
|
+
thread: scope.threadId,
|
|
1108
|
+
resource: ref.resourceId,
|
|
1109
|
+
},
|
|
1110
|
+
}
|
|
1111
|
+
: {}),
|
|
1112
|
+
});
|
|
1113
|
+
});
|
|
703
1114
|
const result = await envelopeFor(scope, ref, stored.principal, summary);
|
|
704
1115
|
if (isTerminalRunStatus(summary.status)) {
|
|
705
1116
|
await deleteAgentRunRecord(options.stateStorage(), ref.runId);
|
|
1117
|
+
const recovery = await options
|
|
1118
|
+
.stateStorage()
|
|
1119
|
+
.get(ownerRecoveryKey(ref.runId));
|
|
1120
|
+
if (recovery) {
|
|
1121
|
+
await finalizeOwnerRecoveryBestEffort(recovery, summary);
|
|
1122
|
+
}
|
|
706
1123
|
}
|
|
707
1124
|
return json(result);
|
|
708
1125
|
}
|
|
@@ -723,6 +1140,16 @@ export function createThreadAgentHost(options) {
|
|
|
723
1140
|
runId,
|
|
724
1141
|
});
|
|
725
1142
|
if (segments.length === 3 && request.method === 'GET') {
|
|
1143
|
+
if (url.searchParams.get('dispatch') === '1') {
|
|
1144
|
+
const key = ownerRecoveryKey(ref.runId);
|
|
1145
|
+
const pending = await options
|
|
1146
|
+
.stateStorage()
|
|
1147
|
+
.get(key);
|
|
1148
|
+
if (pending) {
|
|
1149
|
+
await recoverOwner(scope.init.runtime, scope.threadId, key, pending, true);
|
|
1150
|
+
}
|
|
1151
|
+
return json(await statusFor(scope, ref));
|
|
1152
|
+
}
|
|
726
1153
|
return json(await statusFor(scope, ref));
|
|
727
1154
|
}
|
|
728
1155
|
if (segments.length === 4 &&
|
|
@@ -769,7 +1196,7 @@ export function createThreadAgentHost(options) {
|
|
|
769
1196
|
return known
|
|
770
1197
|
? json({ error: 'method not allowed' }, 405)
|
|
771
1198
|
: json({ error: 'not found' }, 404);
|
|
772
|
-
},
|
|
1199
|
+
}),
|
|
773
1200
|
};
|
|
774
1201
|
return host;
|
|
775
1202
|
}
|