@proofoftech/flowsafe 0.10.0 → 0.12.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 +50 -0
- package/README.md +77 -47
- 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.d.ts +1 -1
- package/dist/agent-host/catalog.d.ts.map +1 -1
- package/dist/agent-host/catalog.js +26 -2
- package/dist/agent-host/catalog.js.map +1 -1
- package/dist/agent-host/index.d.ts +4 -3
- package/dist/agent-host/index.d.ts.map +1 -1
- package/dist/agent-host/index.js +1 -0
- 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 +53 -9
- package/dist/agent-host/thread-host.d.ts.map +1 -1
- package/dist/agent-host/thread-host.js +624 -109
- 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 +9 -2
- package/dist/agent-host/trusted-context.js.map +1 -1
- package/dist/agent-host/types.d.ts +48 -0
- package/dist/agent-host/types.d.ts.map +1 -1
- package/dist/agent-host/types.js +1 -1
- package/dist/agent-host/types.js.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 +13 -24
- package/dist/approval-api/contract.d.ts.map +1 -1
- package/dist/approval-api/contract.js +1 -1
- package/dist/approval-api/contract.js.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 +9 -9
- package/dist/approval-api/index.d.ts.map +1 -1
- package/dist/approval-api/index.js +6 -6
- 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 +16 -5
- package/dist/do-runner/breakwater-keys.d.ts.map +1 -1
- package/dist/do-runner/breakwater-keys.js +16 -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 +44 -182
- package/dist/do-runner/d1-storage.d.ts.map +1 -1
- package/dist/do-runner/d1-storage.js +102 -353
- 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 +6 -8
- 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/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 +149 -73
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +619 -141
- 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 +30 -7
- 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
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
import { AGENT_STREAM_TOPIC } from '@mastra/core/agent/durable';
|
|
3
3
|
import { Mastra } from '@mastra/core/mastra';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { isPrincipalPermissions } from '@proofoftech/breakwater/rbac';
|
|
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';
|
|
7
9
|
import { mastraRegistryEntries } from '../do-runner/mastra-registry.js';
|
|
8
10
|
import { reconcileApprovalsForSummary } from '../host-kit/index.js';
|
|
9
11
|
import { createAgentModuleCatalog } from './catalog.js';
|
|
@@ -58,24 +60,14 @@ const TERMINAL_RUN_STATUSES = [
|
|
|
58
60
|
'bailed',
|
|
59
61
|
'skipped',
|
|
60
62
|
];
|
|
63
|
+
const AGENT_OWNER_RECOVERY_PREFIX = 'flowsafe:agent-owner-recovery:v1:';
|
|
64
|
+
const AGENT_OWNER_RECOVERY_DELAY_MS = 60_000;
|
|
61
65
|
function isTerminalRunStatus(status) {
|
|
62
66
|
return TERMINAL_RUN_STATUSES.includes(status);
|
|
63
67
|
}
|
|
64
68
|
function entryPath(value) {
|
|
65
|
-
const allowed = [
|
|
66
|
-
'http.start',
|
|
67
|
-
'approval.resume',
|
|
68
|
-
'signal.message',
|
|
69
|
-
'signal.queue',
|
|
70
|
-
'signal.reactive',
|
|
71
|
-
'signal.state',
|
|
72
|
-
'signal.notification',
|
|
73
|
-
'signal.wake',
|
|
74
|
-
'notification.dispatch',
|
|
75
|
-
'schedule.fire',
|
|
76
|
-
];
|
|
77
69
|
if (typeof value !== 'string' ||
|
|
78
|
-
!
|
|
70
|
+
!AGENT_ENTRY_PATHS.includes(value)) {
|
|
79
71
|
throw new AgentHostRequestError(400, 'entryPath is required');
|
|
80
72
|
}
|
|
81
73
|
return value;
|
|
@@ -87,14 +79,65 @@ function safeContext(value) {
|
|
|
87
79
|
}
|
|
88
80
|
throw new AgentHostRequestError(400, 'safeContext must be an object');
|
|
89
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
|
+
}
|
|
90
135
|
function runRef(scope, input) {
|
|
91
136
|
const { agentId, threadId, resourceId, runId } = input;
|
|
92
|
-
if (
|
|
137
|
+
if (!isPathSafeId(agentId) ||
|
|
93
138
|
threadId !== scope.threadId ||
|
|
94
|
-
|
|
95
|
-
!
|
|
96
|
-
typeof runId !== 'string' ||
|
|
97
|
-
!tenantOwnsSaltedId(scope.tenantId, runId)) {
|
|
139
|
+
!isPathSafeId(resourceId) ||
|
|
140
|
+
!isPathSafeId(runId)) {
|
|
98
141
|
throw new AgentHostRequestError(404, 'run not found');
|
|
99
142
|
}
|
|
100
143
|
return { agentId, threadId: scope.threadId, resourceId, runId };
|
|
@@ -109,6 +152,21 @@ function audit(sink, event) {
|
|
|
109
152
|
// Authorization is never made unavailable by an audit export failure.
|
|
110
153
|
}
|
|
111
154
|
}
|
|
155
|
+
function normalizedPermissionResolution(value) {
|
|
156
|
+
// Delegate the shape check to breakwater's guard — the SAME predicate its
|
|
157
|
+
// connector required-permissions gate applies to the projection — so this
|
|
158
|
+
// host can never mint a resolution breakwater would reject. Duplicates are
|
|
159
|
+
// tolerated rather than treated as malformed: the all-of check has set
|
|
160
|
+
// semantics, so a host that unions role bundles must not take an
|
|
161
|
+
// availability hit for a repeat that cannot change any decision.
|
|
162
|
+
if (!isPrincipalPermissions(value)) {
|
|
163
|
+
throw new Error('permission resolution is malformed');
|
|
164
|
+
}
|
|
165
|
+
return Object.freeze({
|
|
166
|
+
permissions: Object.freeze([...new Set(value.permissions)]),
|
|
167
|
+
policyVersion: value.policyVersion,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
112
170
|
function ndjson(stream, initialOffset) {
|
|
113
171
|
const reader = stream.getReader();
|
|
114
172
|
const encoder = new TextEncoder();
|
|
@@ -139,11 +197,13 @@ export function createThreadAgentHost(options) {
|
|
|
139
197
|
let runtime;
|
|
140
198
|
const executions = new Map();
|
|
141
199
|
let stableScope;
|
|
142
|
-
|
|
200
|
+
const withBindingLock = createFifoLock();
|
|
201
|
+
const withDispatchLock = createFifoLock();
|
|
202
|
+
const withRecoveryLock = createFifoLock();
|
|
143
203
|
const instanceScopeFor = (scope) => {
|
|
144
204
|
if (stableScope) {
|
|
145
205
|
if (stableScope.threadId !== scope.threadId ||
|
|
146
|
-
stableScope.
|
|
206
|
+
stableScope.deploymentTag !== scope.deploymentTag ||
|
|
147
207
|
stableScope.init !== scope.init) {
|
|
148
208
|
throw new Error('thread agent host cannot be shared across DO instances');
|
|
149
209
|
}
|
|
@@ -151,25 +211,11 @@ export function createThreadAgentHost(options) {
|
|
|
151
211
|
}
|
|
152
212
|
stableScope = Object.freeze({
|
|
153
213
|
threadId: scope.threadId,
|
|
154
|
-
|
|
214
|
+
deploymentTag: scope.deploymentTag,
|
|
155
215
|
init: scope.init,
|
|
156
216
|
});
|
|
157
217
|
return stableScope;
|
|
158
218
|
};
|
|
159
|
-
const withBindingLock = async (operation) => {
|
|
160
|
-
const previous = bindingTail;
|
|
161
|
-
let release = () => undefined;
|
|
162
|
-
bindingTail = new Promise((resolve) => {
|
|
163
|
-
release = resolve;
|
|
164
|
-
});
|
|
165
|
-
await previous;
|
|
166
|
-
try {
|
|
167
|
-
return await operation();
|
|
168
|
-
}
|
|
169
|
-
finally {
|
|
170
|
-
release();
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
219
|
const catalogFor = async (scope) => {
|
|
174
220
|
catalogPromise ??= Promise.resolve(options.buildModules(instanceScopeFor(scope)))
|
|
175
221
|
.then(createAgentModuleCatalog)
|
|
@@ -217,11 +263,26 @@ export function createThreadAgentHost(options) {
|
|
|
217
263
|
* roles at all: it must be declared in the agent's `allowedAutomation` for
|
|
218
264
|
* this precise entry path, AND survive the host's optional authorizer. Absent
|
|
219
265
|
* declaration denies — which is why a scheduled start of an agent that has
|
|
220
|
-
* not opted in fails here rather than executing
|
|
266
|
+
* not opted in fails here rather than executing through a role-bearing
|
|
267
|
+
* human identity.
|
|
268
|
+
*
|
|
269
|
+
* After that kind-specific gate, a configured server-owned resolver runs for
|
|
270
|
+
* EVERY granted entry: an agent that declares required permissions also
|
|
271
|
+
* requires every identifier from the resolution, and the resolution itself
|
|
272
|
+
* is returned so the execution projects it into derived request context —
|
|
273
|
+
* the input to breakwater's connector required-permissions gate. A failed
|
|
274
|
+
* resolution fails closed at the matching scope: it denies a
|
|
275
|
+
* permission-requiring agent, and it costs any other run its projection
|
|
276
|
+
* (audited as `agent.permissions.resolve`), so a permission-declaring
|
|
277
|
+
* connector inside that run denies rather than executing unauthorized.
|
|
221
278
|
*/
|
|
222
279
|
const authorize = async (scope, agentId, entry, principal) => {
|
|
223
280
|
const current = await runtimeFor(scope);
|
|
224
281
|
const module = current.catalog.get(agentId);
|
|
282
|
+
const requiredPermissions = module?.meta.requiredPermissions;
|
|
283
|
+
let permissionPolicyVersion = null;
|
|
284
|
+
let principalPermissions = null;
|
|
285
|
+
let decision = 'denied';
|
|
225
286
|
let reason;
|
|
226
287
|
let granted = false;
|
|
227
288
|
if (module === undefined) {
|
|
@@ -245,35 +306,220 @@ export function createThreadAgentHost(options) {
|
|
|
245
306
|
principal,
|
|
246
307
|
agentId,
|
|
247
308
|
entryPath: entry,
|
|
248
|
-
|
|
309
|
+
deploymentTag: scope.deploymentTag,
|
|
249
310
|
threadId: scope.threadId,
|
|
250
311
|
})) ?? true;
|
|
251
312
|
granted = hostAllows;
|
|
252
313
|
if (!granted)
|
|
253
314
|
reason = 'host denied this automated entry';
|
|
254
315
|
}
|
|
316
|
+
const resolver = options.resolvePrincipalPermissions;
|
|
317
|
+
if (granted && resolver) {
|
|
318
|
+
try {
|
|
319
|
+
principalPermissions = normalizedPermissionResolution(await resolver(principal));
|
|
320
|
+
permissionPolicyVersion = principalPermissions.policyVersion;
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
if (requiredPermissions !== undefined) {
|
|
324
|
+
decision = 'error';
|
|
325
|
+
granted = false;
|
|
326
|
+
reason = 'permission resolution failed';
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
// The entry stays granted — this agent requires no permissions —
|
|
330
|
+
// but the run loses its projection, so a permission-declaring
|
|
331
|
+
// connector inside it fails closed. A dedicated event says so,
|
|
332
|
+
// because the entry event below reports this entry as allowed.
|
|
333
|
+
audit(options.audit, {
|
|
334
|
+
actor: principalActor(principal),
|
|
335
|
+
action: 'agent.permissions.resolve',
|
|
336
|
+
resource: `agent:${agentId}`,
|
|
337
|
+
decision: 'error',
|
|
338
|
+
reason: 'permission resolution failed',
|
|
339
|
+
detail: {
|
|
340
|
+
agentId,
|
|
341
|
+
...(scope.deploymentTag !== undefined
|
|
342
|
+
? { tenantId: scope.deploymentTag }
|
|
343
|
+
: {}),
|
|
344
|
+
threadId: scope.threadId,
|
|
345
|
+
entryPath: entry,
|
|
346
|
+
...principalAuditFields(principal),
|
|
347
|
+
permissionPolicyVersion: null,
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (granted && requiredPermissions !== undefined) {
|
|
354
|
+
if (!resolver) {
|
|
355
|
+
granted = false;
|
|
356
|
+
reason = 'permission resolver is not configured';
|
|
357
|
+
}
|
|
358
|
+
else if (principalPermissions) {
|
|
359
|
+
const effective = new Set(principalPermissions.permissions);
|
|
360
|
+
granted = requiredPermissions.every((permission) => effective.has(permission));
|
|
361
|
+
if (!granted) {
|
|
362
|
+
reason = 'required permissions are not satisfied';
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
if (granted)
|
|
367
|
+
decision = 'allowed';
|
|
255
368
|
audit(options.audit, {
|
|
256
369
|
actor: principalActor(principal),
|
|
257
370
|
action: 'agent.entry.authorize',
|
|
258
371
|
resource: `agent:${agentId}`,
|
|
259
|
-
decision
|
|
372
|
+
decision,
|
|
260
373
|
...(reason !== undefined ? { reason } : {}),
|
|
261
374
|
detail: {
|
|
262
375
|
agentId,
|
|
263
|
-
|
|
376
|
+
...(scope.deploymentTag !== undefined
|
|
377
|
+
? { tenantId: scope.deploymentTag }
|
|
378
|
+
: {}),
|
|
264
379
|
threadId: scope.threadId,
|
|
265
380
|
entryPath: entry,
|
|
266
381
|
...principalAuditFields(principal),
|
|
382
|
+
...(requiredPermissions !== undefined
|
|
383
|
+
? { requiredPermissions, permissionPolicyVersion }
|
|
384
|
+
: {}),
|
|
267
385
|
},
|
|
268
386
|
});
|
|
269
387
|
if (!module)
|
|
270
388
|
throw new AgentHostRequestError(404, 'agent not found');
|
|
271
389
|
if (!granted)
|
|
272
390
|
throw new AgentHostRequestError(403, 'forbidden');
|
|
273
|
-
return { current, module };
|
|
391
|
+
return { current, module, principalPermissions };
|
|
274
392
|
};
|
|
275
393
|
const readBinding = () => readAgentThreadBinding(options.stateStorage());
|
|
276
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
|
+
};
|
|
277
523
|
const withExecution = async (execution, operation) => {
|
|
278
524
|
if (executions.has(execution.runId)) {
|
|
279
525
|
throw new AgentHostRequestError(409, `run '${execution.runId}' already has an active operation`);
|
|
@@ -289,8 +535,8 @@ export function createThreadAgentHost(options) {
|
|
|
289
535
|
// Reconciling approvals is trusted platform work with no person behind it.
|
|
290
536
|
// It used to mint role:'operator', which is why an approval bridge looked
|
|
291
537
|
// indistinguishable from a human operator in the audit trail. The bridge
|
|
292
|
-
// mints its own principal from this id
|
|
293
|
-
const
|
|
538
|
+
// mints its own principal from this id.
|
|
539
|
+
const systemPrincipalId = options.systemPrincipalId ?? 'flowsafe-system';
|
|
294
540
|
// Deliberately NOT vouched. Its only consumer projects it to an ApprovalActor
|
|
295
541
|
// for a role-gated READ, which grants nothing an automated principal does not
|
|
296
542
|
// already have — so calling the trust assertion here would assert trust that
|
|
@@ -301,27 +547,29 @@ export function createThreadAgentHost(options) {
|
|
|
301
547
|
// list() emits no audit event, so this string reaches nothing. It is not
|
|
302
548
|
// shared with the bridge's RECONCILE_PURPOSE for that reason — there is no
|
|
303
549
|
// provenance here to drift.
|
|
304
|
-
const systemPrincipal = (
|
|
550
|
+
const systemPrincipal = () => ({
|
|
305
551
|
kind: 'system',
|
|
306
|
-
id:
|
|
307
|
-
tenantId: scope.tenantId,
|
|
552
|
+
id: systemPrincipalId,
|
|
308
553
|
purpose: 'approval-suspension-reconcile',
|
|
309
554
|
});
|
|
310
555
|
const currentApprovals = async (scope, summary, principal, agentId, resourceId) => {
|
|
311
556
|
if (summary.status !== 'suspended')
|
|
312
557
|
return [];
|
|
558
|
+
if (!summary.requestedBy) {
|
|
559
|
+
throw new Error('suspended agent run has no durable requester provenance');
|
|
560
|
+
}
|
|
313
561
|
const service = options.approvalService(instanceScopeFor(scope));
|
|
314
|
-
await reconcileApprovalsForSummary(service, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, summary,
|
|
562
|
+
await reconcileApprovalsForSummary(service, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, summary, systemPrincipalId, {
|
|
315
563
|
kind: 'agent-thread',
|
|
316
564
|
agentId,
|
|
317
565
|
threadId: scope.threadId,
|
|
318
566
|
resourceId,
|
|
319
567
|
principal,
|
|
320
|
-
},
|
|
568
|
+
}, summary.requestedBy);
|
|
321
569
|
const records = await service.list({
|
|
322
570
|
workflowId: DURABLE_AGENTIC_LOOP_WORKFLOW_ID,
|
|
323
571
|
runId: summary.runId,
|
|
324
|
-
}, principalActor(systemPrincipal(
|
|
572
|
+
}, principalActor(systemPrincipal()));
|
|
325
573
|
const keys = new Set((summary.suspended ?? []).map((path) => path.join('.')));
|
|
326
574
|
return records.filter((record) => {
|
|
327
575
|
const key = record.stepPath?.join('.');
|
|
@@ -341,7 +589,18 @@ export function createThreadAgentHost(options) {
|
|
|
341
589
|
};
|
|
342
590
|
if (summary.status !== 'suspended')
|
|
343
591
|
return base;
|
|
344
|
-
|
|
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
|
+
}
|
|
345
604
|
return {
|
|
346
605
|
...base,
|
|
347
606
|
...(approvals[0] ? { approval: approvals[0] } : {}),
|
|
@@ -378,6 +637,7 @@ export function createThreadAgentHost(options) {
|
|
|
378
637
|
safeContext: sanitizeStoredAgentContext(requestContext),
|
|
379
638
|
};
|
|
380
639
|
};
|
|
640
|
+
let recoverOwner;
|
|
381
641
|
const statusFor = async (scope, ref, knownThreaded) => {
|
|
382
642
|
const binding = await readBinding();
|
|
383
643
|
const current = await runtimeFor(scope);
|
|
@@ -399,18 +659,81 @@ export function createThreadAgentHost(options) {
|
|
|
399
659
|
if (summary.status === 'suspended' && !stored) {
|
|
400
660
|
throw new AgentHostRequestError(409, 'suspended agent run has no recoverable execution principal');
|
|
401
661
|
}
|
|
402
|
-
// Inert today — a suspended run without a stored record already threw 409
|
|
403
|
-
// above, and only a suspended run consults this. Kept as the scope's
|
|
404
|
-
// PRINCIPAL rather than its actor so that if those two conditions are ever
|
|
405
|
-
// decoupled, the fallback still cannot relabel an automated run as whoever
|
|
406
|
-
// happened to poll its status.
|
|
407
662
|
const principal = stored?.principal ?? scope.principal;
|
|
408
663
|
const result = await envelopeFor(scope, ref, principal, summary);
|
|
409
664
|
if (isTerminalRunStatus(summary.status) && stored) {
|
|
410
665
|
await deleteAgentRunRecord(options.stateStorage(), ref.runId);
|
|
411
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
|
+
}
|
|
412
676
|
return result;
|
|
413
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
|
+
}));
|
|
414
737
|
const host = {
|
|
415
738
|
requestContextForRun: (base) => async (workflowId, runId, leg) => {
|
|
416
739
|
const values = base ? await base(workflowId, runId, leg) : undefined;
|
|
@@ -423,74 +746,177 @@ export function createThreadAgentHost(options) {
|
|
|
423
746
|
}
|
|
424
747
|
: values;
|
|
425
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
|
+
}),
|
|
426
794
|
start: async (scope, input) => {
|
|
427
795
|
const ref = runRef(scope, input);
|
|
428
|
-
const
|
|
429
|
-
const
|
|
430
|
-
|
|
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) ||
|
|
431
802
|
(hasPrompt &&
|
|
432
803
|
(typeof input.prompt !== 'string' ||
|
|
433
804
|
input.prompt.trim() === '' ||
|
|
434
805
|
input.prompt.length > 10_000))) {
|
|
435
806
|
throw new AgentHostRequestError(400, 'exactly one valid prompt or messages input is required');
|
|
436
807
|
}
|
|
437
|
-
const messages =
|
|
808
|
+
const messages = source.target
|
|
809
|
+
? threaded && input.messages !== undefined
|
|
810
|
+
? input.messages
|
|
811
|
+
: source.target.prompt
|
|
812
|
+
: (input.messages ?? input.prompt);
|
|
438
813
|
if (messages === undefined) {
|
|
439
814
|
throw new AgentHostRequestError(400, 'agent input is required');
|
|
440
815
|
}
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
|
|
816
|
+
const resolvedProviderOptions = source.target
|
|
817
|
+
? source.target.providerOptions
|
|
818
|
+
: input.providerOptions;
|
|
819
|
+
const owner = source.owner;
|
|
820
|
+
const { current, module, principalPermissions } = await authorize(scope, ref.agentId, entry, scope.principal);
|
|
821
|
+
if (ref.resourceId !== resourceIdFromKey(scope.threadId)) {
|
|
444
822
|
throw new AgentHostRequestError(404, 'run not found');
|
|
445
823
|
}
|
|
446
824
|
const execution = {
|
|
447
825
|
agentId: ref.agentId,
|
|
826
|
+
deploymentTag: scope.deploymentTag,
|
|
448
827
|
principal: scope.principal,
|
|
449
828
|
threadId: scope.threadId,
|
|
450
829
|
resourceId: ref.resourceId,
|
|
451
830
|
runId: ref.runId,
|
|
452
831
|
entryPath: entry,
|
|
453
|
-
|
|
832
|
+
principalPermissions,
|
|
833
|
+
safeContext: safeContext(source.target
|
|
834
|
+
? scheduleAgentContext(source.target)
|
|
835
|
+
: input.safeContext),
|
|
454
836
|
};
|
|
455
837
|
const durable = current.agents.get(module.meta.id);
|
|
456
838
|
if (!durable)
|
|
457
839
|
throw new Error('guarded agent was not registered');
|
|
458
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
|
+
}
|
|
459
848
|
const existingRecord = await readRun(ref.runId);
|
|
460
849
|
const existingSummary = await scope.init.runtime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId);
|
|
461
850
|
if (existingRecord || existingSummary) {
|
|
462
851
|
throw new AgentHostRequestError(409, `run '${ref.runId}' already exists`);
|
|
463
852
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
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) {
|
|
467
870
|
const bindingMatches = existing?.agentId === ref.agentId &&
|
|
468
871
|
existing.resourceId === ref.resourceId;
|
|
469
872
|
if (entry === 'schedule.fire') {
|
|
470
873
|
if (!bindingMatches) {
|
|
471
874
|
throw new AgentHostRequestError(404, 'run not found');
|
|
472
875
|
}
|
|
473
|
-
return;
|
|
474
876
|
}
|
|
475
|
-
if (existing && !bindingMatches) {
|
|
877
|
+
else if (existing && !bindingMatches) {
|
|
476
878
|
throw new AgentHostRequestError(409, 'thread is bound to another agent');
|
|
477
879
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
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
|
+
});
|
|
494
920
|
try {
|
|
495
921
|
await durable.streamUntilPersisted(messages, {
|
|
496
922
|
runId: ref.runId,
|
|
@@ -505,7 +931,10 @@ export function createThreadAgentHost(options) {
|
|
|
505
931
|
: {}),
|
|
506
932
|
maxSteps: module.agent.maxSteps,
|
|
507
933
|
disableBackgroundTasks: true,
|
|
508
|
-
|
|
934
|
+
...(resolvedProviderOptions !== undefined
|
|
935
|
+
? { providerOptions: providerOptions(resolvedProviderOptions) }
|
|
936
|
+
: {}),
|
|
937
|
+
}, scope.principal.id, scope.principal.kind, recovery.token);
|
|
509
938
|
const summary = await scope.init.runtime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId);
|
|
510
939
|
if (!summary)
|
|
511
940
|
throw new Error('agent run did not persist a summary');
|
|
@@ -513,19 +942,41 @@ export function createThreadAgentHost(options) {
|
|
|
513
942
|
if (isTerminalRunStatus(result.summary.status)) {
|
|
514
943
|
await deleteAgentRunRecord(options.stateStorage(), ref.runId);
|
|
515
944
|
}
|
|
945
|
+
await finalizeOwnerRecoveryBestEffort(recovery, summary);
|
|
516
946
|
return result;
|
|
517
947
|
}
|
|
518
948
|
catch (error) {
|
|
519
949
|
let summary;
|
|
520
950
|
try {
|
|
521
|
-
summary = await scope.init.runtime.
|
|
951
|
+
summary = await scope.init.runtime.recoverStartAttempt(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId, recovery.token);
|
|
522
952
|
}
|
|
523
953
|
catch {
|
|
524
954
|
// Unknown authoritative state: retain metadata and fail closed.
|
|
525
955
|
}
|
|
526
|
-
if (
|
|
956
|
+
if (summary === null ||
|
|
957
|
+
(summary && isTerminalRunStatus(summary.status))) {
|
|
527
958
|
await deleteAgentRunRecord(options.stateStorage(), ref.runId);
|
|
528
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
|
+
}
|
|
529
980
|
throw error;
|
|
530
981
|
}
|
|
531
982
|
});
|
|
@@ -534,7 +985,7 @@ export function createThreadAgentHost(options) {
|
|
|
534
985
|
const binding = await readBinding();
|
|
535
986
|
if (!binding)
|
|
536
987
|
throw new AgentHostRequestError(404, 'agent not found');
|
|
537
|
-
if (binding.resourceId !==
|
|
988
|
+
if (binding.resourceId !== resourceIdFromKey(scope.threadId)) {
|
|
538
989
|
throw new AgentHostRequestError(404, 'agent not found');
|
|
539
990
|
}
|
|
540
991
|
if (input.agentId && input.agentId !== binding.agentId) {
|
|
@@ -550,23 +1001,62 @@ export function createThreadAgentHost(options) {
|
|
|
550
1001
|
durableAgent,
|
|
551
1002
|
};
|
|
552
1003
|
},
|
|
553
|
-
route:
|
|
1004
|
+
route: (request, scope) => withDispatchLock(async () => {
|
|
554
1005
|
const url = new URL(request.url);
|
|
555
1006
|
if (!url.pathname.startsWith(AGENT_HOST_ROUTE_PREFIX))
|
|
556
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
|
+
}
|
|
557
1021
|
if (request.method === 'POST' &&
|
|
558
1022
|
url.pathname === `${AGENT_HOST_ROUTE_PREFIX}/start`) {
|
|
559
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
|
+
}
|
|
560
1027
|
const ref = runRef(scope, body);
|
|
561
|
-
|
|
1028
|
+
const requestedEntry = entryPath(body.entryPath);
|
|
1029
|
+
if (requestedEntry !== 'schedule.fire' &&
|
|
1030
|
+
typeof body.prompt !== 'string') {
|
|
562
1031
|
throw new AgentHostRequestError(400, 'prompt is required');
|
|
563
1032
|
}
|
|
564
1033
|
return json(await host.start(scope, {
|
|
565
1034
|
...ref,
|
|
566
|
-
|
|
567
|
-
|
|
1035
|
+
...(typeof body.prompt === 'string'
|
|
1036
|
+
? { prompt: body.prompt }
|
|
1037
|
+
: {}),
|
|
1038
|
+
entryPath: requestedEntry,
|
|
568
1039
|
threaded: body.threaded !== false,
|
|
569
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
|
+
: {}),
|
|
570
1060
|
}));
|
|
571
1061
|
}
|
|
572
1062
|
if (request.method === 'POST' &&
|
|
@@ -581,10 +1071,11 @@ export function createThreadAgentHost(options) {
|
|
|
581
1071
|
!samePrincipal(stored.principal, scope.principal)) {
|
|
582
1072
|
throw new AgentHostRequestError(404, 'run not found');
|
|
583
1073
|
}
|
|
584
|
-
const { current, module } = await authorize(scope, ref.agentId, entryPath(body.entryPath), stored.principal);
|
|
1074
|
+
const { current, module, principalPermissions } = await authorize(scope, ref.agentId, entryPath(body.entryPath), stored.principal);
|
|
585
1075
|
const durable = current.agents.get(module.meta.id);
|
|
586
1076
|
if (!durable)
|
|
587
1077
|
throw new Error('guarded agent was not registered');
|
|
1078
|
+
const requesterId = requestedBy(body.requestedBy);
|
|
588
1079
|
const step = typeof body.step === 'string' ||
|
|
589
1080
|
(Array.isArray(body.step) &&
|
|
590
1081
|
body.step.every((part) => typeof part === 'string'))
|
|
@@ -592,29 +1083,43 @@ export function createThreadAgentHost(options) {
|
|
|
592
1083
|
: undefined;
|
|
593
1084
|
const execution = {
|
|
594
1085
|
agentId: ref.agentId,
|
|
1086
|
+
deploymentTag: scope.deploymentTag,
|
|
595
1087
|
principal: stored.principal,
|
|
596
1088
|
threadId: scope.threadId,
|
|
597
1089
|
resourceId: ref.resourceId,
|
|
598
1090
|
runId: ref.runId,
|
|
599
1091
|
entryPath: 'approval.resume',
|
|
1092
|
+
// The re-derived resolution, not the start leg's: the resume merges
|
|
1093
|
+
// over the persisted context, so this leg's projection retires a
|
|
1094
|
+
// stale one minted under an older policy snapshot.
|
|
1095
|
+
principalPermissions,
|
|
600
1096
|
safeContext: snapshotExecution.safeContext,
|
|
601
1097
|
};
|
|
602
|
-
const summary = await withExecution(execution, () =>
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
? {
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
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
|
+
});
|
|
615
1114
|
const result = await envelopeFor(scope, ref, stored.principal, summary);
|
|
616
1115
|
if (isTerminalRunStatus(summary.status)) {
|
|
617
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
|
+
}
|
|
618
1123
|
}
|
|
619
1124
|
return json(result);
|
|
620
1125
|
}
|
|
@@ -635,6 +1140,16 @@ export function createThreadAgentHost(options) {
|
|
|
635
1140
|
runId,
|
|
636
1141
|
});
|
|
637
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
|
+
}
|
|
638
1153
|
return json(await statusFor(scope, ref));
|
|
639
1154
|
}
|
|
640
1155
|
if (segments.length === 4 &&
|
|
@@ -681,7 +1196,7 @@ export function createThreadAgentHost(options) {
|
|
|
681
1196
|
return known
|
|
682
1197
|
? json({ error: 'method not allowed' }, 405)
|
|
683
1198
|
: json({ error: 'not found' }, 404);
|
|
684
|
-
},
|
|
1199
|
+
}),
|
|
685
1200
|
};
|
|
686
1201
|
return host;
|
|
687
1202
|
}
|