@proofoftech/flowsafe 0.22.0 → 0.24.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 +160 -0
- package/README.md +77 -12
- package/dist/agent-host/catalog.d.ts.map +1 -1
- package/dist/agent-host/catalog.js +5 -2
- package/dist/agent-host/catalog.js.map +1 -1
- package/dist/agent-host/thread-host.d.ts +16 -0
- package/dist/agent-host/thread-host.d.ts.map +1 -1
- package/dist/agent-host/thread-host.js +81 -27
- package/dist/agent-host/thread-host.js.map +1 -1
- package/dist/agent-runner/durable-agent-runner.d.ts +121 -80
- package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
- package/dist/agent-runner/durable-agent-runner.js +747 -468
- package/dist/agent-runner/durable-agent-runner.js.map +1 -1
- package/dist/approval-ui/use-approval-dashboard.js +6 -6
- package/dist/approval-ui/use-approval-dashboard.js.map +1 -1
- package/dist/background-tasks/d1-storage.d.ts.map +1 -1
- package/dist/background-tasks/d1-storage.js +2 -3
- package/dist/background-tasks/d1-storage.js.map +1 -1
- package/dist/background-tasks/host.d.ts.map +1 -1
- package/dist/background-tasks/host.js +27 -38
- package/dist/background-tasks/host.js.map +1 -1
- package/dist/do-runner/cause-chain.d.ts +5 -0
- package/dist/do-runner/cause-chain.d.ts.map +1 -1
- package/dist/do-runner/cause-chain.js +12 -0
- package/dist/do-runner/cause-chain.js.map +1 -1
- package/dist/do-runner/cf-types.d.ts +3 -0
- 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/constants.d.ts +1 -1
- package/dist/do-runner/constants.d.ts.map +1 -1
- package/dist/do-runner/constants.js +1 -1
- package/dist/do-runner/constants.js.map +1 -1
- package/dist/do-runner/core-events-nonce.d.ts +2 -0
- package/dist/do-runner/core-events-nonce.d.ts.map +1 -0
- package/dist/do-runner/core-events-nonce.js +16 -0
- package/dist/do-runner/core-events-nonce.js.map +1 -0
- package/dist/do-runner/d1-storage.js +1 -9
- package/dist/do-runner/d1-storage.js.map +1 -1
- package/dist/do-runner/do-error-response.d.ts.map +1 -1
- package/dist/do-runner/do-error-response.js +2 -1
- package/dist/do-runner/do-error-response.js.map +1 -1
- package/dist/do-runner/durable-object.d.ts +17 -2
- package/dist/do-runner/durable-object.d.ts.map +1 -1
- package/dist/do-runner/durable-object.js +442 -55
- package/dist/do-runner/durable-object.js.map +1 -1
- package/dist/do-runner/fenced-workflow-capability.d.ts +24 -0
- package/dist/do-runner/fenced-workflow-capability.d.ts.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.d.ts +5 -1
- package/dist/do-runner/fenced-workflows-d1.d.ts.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.js +149 -48
- package/dist/do-runner/fenced-workflows-d1.js.map +1 -1
- package/dist/do-runner/index.d.ts +2 -2
- package/dist/do-runner/index.d.ts.map +1 -1
- package/dist/do-runner/index.js +2 -2
- package/dist/do-runner/index.js.map +1 -1
- package/dist/do-runner/init.d.ts +2 -3
- package/dist/do-runner/init.d.ts.map +1 -1
- package/dist/do-runner/init.js.map +1 -1
- package/dist/do-runner/memory-id.js +1 -1
- package/dist/do-runner/memory-id.js.map +1 -1
- package/dist/do-runner/pubsub.d.ts +6 -2
- package/dist/do-runner/pubsub.d.ts.map +1 -1
- package/dist/do-runner/pubsub.js +12 -22
- package/dist/do-runner/pubsub.js.map +1 -1
- package/dist/do-runner/run-lifecycle.d.ts +42 -2
- package/dist/do-runner/run-lifecycle.d.ts.map +1 -1
- package/dist/do-runner/run-lifecycle.js +57 -1
- package/dist/do-runner/run-lifecycle.js.map +1 -1
- package/dist/do-runner/run-terminal-state.d.ts +2 -0
- package/dist/do-runner/run-terminal-state.d.ts.map +1 -1
- package/dist/do-runner/run-terminal-state.js +4 -0
- package/dist/do-runner/run-terminal-state.js.map +1 -1
- package/dist/do-runner/runtime.d.ts +91 -58
- package/dist/do-runner/runtime.d.ts.map +1 -1
- package/dist/do-runner/runtime.js +351 -191
- package/dist/do-runner/runtime.js.map +1 -1
- package/dist/do-runner/suspension-deadline.d.ts +25 -17
- package/dist/do-runner/suspension-deadline.d.ts.map +1 -1
- package/dist/do-runner/suspension-deadline.js +44 -21
- package/dist/do-runner/suspension-deadline.js.map +1 -1
- package/dist/do-runner/workflow-snapshot-row.d.ts +7 -0
- package/dist/do-runner/workflow-snapshot-row.d.ts.map +1 -1
- package/dist/do-runner/workflow-snapshot-row.js +21 -0
- package/dist/do-runner/workflow-snapshot-row.js.map +1 -1
- package/dist/host-kit/approval-bridge.d.ts +11 -8
- package/dist/host-kit/approval-bridge.d.ts.map +1 -1
- package/dist/host-kit/approval-bridge.js +15 -9
- package/dist/host-kit/approval-bridge.js.map +1 -1
- package/dist/host-kit/do-response.d.ts.map +1 -1
- package/dist/host-kit/do-response.js +7 -1
- package/dist/host-kit/do-response.js.map +1 -1
- package/dist/host-kit/flowsafe-worker.d.ts +2 -1
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +4 -2
- package/dist/host-kit/flowsafe-worker.js.map +1 -1
- package/dist/host-kit/run-router.d.ts.map +1 -1
- package/dist/host-kit/run-router.js +3 -2
- package/dist/host-kit/run-router.js.map +1 -1
- package/dist/schedules/schedules-d1.d.ts +1 -0
- package/dist/schedules/schedules-d1.d.ts.map +1 -1
- package/dist/schedules/schedules-d1.js +35 -22
- package/dist/schedules/schedules-d1.js.map +1 -1
- package/dist/schedules/tick.d.ts +17 -2
- package/dist/schedules/tick.d.ts.map +1 -1
- package/dist/schedules/tick.js +63 -44
- package/dist/schedules/tick.js.map +1 -1
- package/dist/signals/client.d.ts +7 -4
- package/dist/signals/client.d.ts.map +1 -1
- package/dist/signals/client.js +9 -7
- 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/thread-do-routes.d.ts +56 -10
- package/dist/signals/thread-do-routes.d.ts.map +1 -1
- package/dist/signals/thread-do-routes.js +434 -247
- package/dist/signals/thread-do-routes.js.map +1 -1
- package/package.json +18 -9
|
@@ -4,20 +4,15 @@
|
|
|
4
4
|
// onto ONE isolate. The DO IS the serialization lease Mastra otherwise wants
|
|
5
5
|
// Redis for. These routes run AFTER ThreadDurableObject.fetch verifies the
|
|
6
6
|
// deployment identity and decodes the server-stamped principal. The scope's
|
|
7
|
-
// threadId and principal are trusted here; the ingestion gate
|
|
8
|
-
//
|
|
9
|
-
//
|
|
7
|
+
// threadId and principal are trusted here; the ingestion gate is the
|
|
8
|
+
// Worker-side createSignalRouter's job, the same split createRunRouter (Worker
|
|
9
|
+
// gate) → DurableObjectRunner (execution) uses.
|
|
10
10
|
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
// stamp the DO's ONE identity (`scope.init.pubsub`) onto the agent before every
|
|
17
|
-
// call — the exact reason durable-agent execution threads that identity into
|
|
18
|
-
// createRun. Absent (host opted out) ⇒ core's module default, still one per
|
|
19
|
-
// isolate, so affinity holds either way; a wired pubsub additionally makes
|
|
20
|
-
// observe()/replay align (pubsub.ts).
|
|
11
|
+
// Core keys its in-process signal registry by pub/sub identity. A runtime-driven
|
|
12
|
+
// wrapper carries the thread Durable Object's pub/sub from construction; these
|
|
13
|
+
// routes refuse a configured identity mismatch. Other agents receive the
|
|
14
|
+
// thread's pub/sub before each call. Without a host pub/sub, other agents fall
|
|
15
|
+
// back to core's module default and runtime-driven wrappers use their own stream bus.
|
|
21
16
|
//
|
|
22
17
|
// core's `agentThreadStreamRuntime` is NOT on the package exports map, so these
|
|
23
18
|
// routes drive only the PUBLIC Agent methods, never a deep dist import across
|
|
@@ -33,6 +28,15 @@ import { internalErrorResponse } from '../internal-error-response.js';
|
|
|
33
28
|
import { positiveSafeInteger } from '../numeric-config.js';
|
|
34
29
|
import { createScheduleAgentDispatchReceipt, } from '../schedules/schedules-d1.js';
|
|
35
30
|
import { captureNotificationDeliverySelection, captureNotificationDeliveryStorage, DEFAULT_MAX_NOTIFICATION_DELIVERY_ATTEMPTS, MAX_NOTIFICATION_DISPATCH_IDS, planNotificationDispatch, recordNotificationDeliveryFailure, reportNotificationDeliveryError, } from './notification-dispatch.js';
|
|
31
|
+
// The schedule tick settles a threaded fire as failed when this route answers
|
|
32
|
+
// one of the permanent statuses `ScheduleTickSignalAgent` documents, so a
|
|
33
|
+
// transient condition must not answer with one of them.
|
|
34
|
+
const SIGNAL_ROUTE_REFUSAL_MESSAGES = {
|
|
35
|
+
400: 'bad request',
|
|
36
|
+
403: 'forbidden',
|
|
37
|
+
404: 'not found',
|
|
38
|
+
409: 'conflict',
|
|
39
|
+
};
|
|
36
40
|
/**
|
|
37
41
|
* The idle-thread delivery behavior a send may ask for. `wake` starts a run
|
|
38
42
|
* after consulting the run cap; `persist` writes the signal to the durable
|
|
@@ -104,7 +108,7 @@ async function readJson(request) {
|
|
|
104
108
|
* The wire surface accepts STRING contents only — the common case, and the one
|
|
105
109
|
* the ingestion gate can size-cap and escape uniformly as defense in depth.
|
|
106
110
|
* Core's multimodal `AgentSignalContents` array form (TextPart/FilePart) is a
|
|
107
|
-
* documented residual, not exposed over this untrusted channel
|
|
111
|
+
* documented residual, not exposed over this untrusted channel.
|
|
108
112
|
*/
|
|
109
113
|
function isContents(value) {
|
|
110
114
|
return typeof value === 'string';
|
|
@@ -117,9 +121,7 @@ function isContents(value) {
|
|
|
117
121
|
* into a 400 at the route, and an invalid attribute name into a dropped
|
|
118
122
|
* attributes object (`isAttributes`), rather than a
|
|
119
123
|
* throw at render time inside the agent turn. Kept byte-identical to core
|
|
120
|
-
* (chunk `signalToXmlMarkup`: `/^[A-Za-z_][A-Za-z0-9_.-]*$/`)
|
|
121
|
-
* pins core's own neutralization of contents/attribute values so this
|
|
122
|
-
* mirror and that layer are checked together.
|
|
124
|
+
* (chunk `signalToXmlMarkup`: `/^[A-Za-z_][A-Za-z0-9_.-]*$/`).
|
|
123
125
|
*/
|
|
124
126
|
const XML_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_.-]*$/;
|
|
125
127
|
/**
|
|
@@ -152,6 +154,21 @@ function renderableAttributes(attributes) {
|
|
|
152
154
|
return true;
|
|
153
155
|
return Object.entries(attributes).every(([key, value]) => value === null || value === undefined || XML_NAME_PATTERN.test(key));
|
|
154
156
|
}
|
|
157
|
+
async function inspectPolicyResult(invoke) {
|
|
158
|
+
try {
|
|
159
|
+
const result = await invoke();
|
|
160
|
+
if (result?.allowed === true)
|
|
161
|
+
return 'allowed';
|
|
162
|
+
if (result?.allowed === false &&
|
|
163
|
+
(result.outcome === 'denied' || result.outcome === 'error')) {
|
|
164
|
+
return result.outcome;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
return 'error';
|
|
169
|
+
}
|
|
170
|
+
return 'error';
|
|
171
|
+
}
|
|
155
172
|
async function inspectSignalContent(policy, input, signal, runId) {
|
|
156
173
|
if (typeof signal.contents !== 'string')
|
|
157
174
|
return 'error';
|
|
@@ -167,23 +184,11 @@ async function inspectSignalContent(policy, input, signal, runId) {
|
|
|
167
184
|
catch {
|
|
168
185
|
return 'error';
|
|
169
186
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
});
|
|
176
|
-
if (result?.allowed === true)
|
|
177
|
-
return 'allowed';
|
|
178
|
-
if (result?.allowed === false &&
|
|
179
|
-
(result.outcome === 'denied' || result.outcome === 'error')) {
|
|
180
|
-
return result.outcome;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
catch {
|
|
184
|
-
return 'error';
|
|
185
|
-
}
|
|
186
|
-
return 'error';
|
|
187
|
+
return inspectPolicyResult(() => policy({
|
|
188
|
+
...input,
|
|
189
|
+
text,
|
|
190
|
+
...(runId !== undefined ? { runId } : {}),
|
|
191
|
+
}));
|
|
187
192
|
}
|
|
188
193
|
function signalContentPolicyResponse(inspection) {
|
|
189
194
|
return inspection === 'denied'
|
|
@@ -192,7 +197,7 @@ function signalContentPolicyResponse(inspection) {
|
|
|
192
197
|
}
|
|
193
198
|
export function createThreadSignalRoutes(options) {
|
|
194
199
|
const maxDeliveryAttempts = positiveSafeInteger(options.maxDeliveryAttempts ?? DEFAULT_MAX_NOTIFICATION_DELIVERY_ATTEMPTS, 'notification maximum delivery attempts');
|
|
195
|
-
const { resolveAgent, resolveResourceId, consultRunCap, startIdleRun, resolveBlockingRun, serializeDispatch, resolveScheduleRunStatus, resolveScheduleTarget, canPersist, canPersistSchedule, resolveNotificationsStorage, resolveScheduleDispatchStore, contentPolicy, } = options;
|
|
200
|
+
const { resolveAgent, resolveResourceId, consultRunCap, startIdleRun, resolveBlockingRun, serializeDispatch, resolveScheduleRunStatus, resolveScheduleTarget, canPersist, canPersistSchedule, resolveNotificationsStorage, notificationDispatchAllowed, resolveScheduleDispatchStore, contentPolicy, scheduleProviderOptionsPolicy, } = options;
|
|
196
201
|
if (resolveBlockingRun && !serializeDispatch) {
|
|
197
202
|
throw new Error('signal routes require serializeDispatch with resolveBlockingRun');
|
|
198
203
|
}
|
|
@@ -209,19 +214,8 @@ export function createThreadSignalRoutes(options) {
|
|
|
209
214
|
notificationTail = next.then(() => undefined, () => undefined);
|
|
210
215
|
return next;
|
|
211
216
|
};
|
|
212
|
-
const route = async (
|
|
213
|
-
if (request.method !== 'POST')
|
|
214
|
-
return null;
|
|
215
|
-
const url = new URL(request.url);
|
|
217
|
+
const route = async (url, scope, entryPath, body) => {
|
|
216
218
|
const path = url.pathname;
|
|
217
|
-
if (path !== '/signal' && !path.startsWith('/signal/'))
|
|
218
|
-
return null;
|
|
219
|
-
const entryPath = entryPathForSignalRoute(path);
|
|
220
|
-
if (entryPath === undefined)
|
|
221
|
-
return json({ error: 'not found' }, 404);
|
|
222
|
-
const body = await readJson(request);
|
|
223
|
-
if (!body)
|
|
224
|
-
return json({ error: 'a JSON body is required' }, 400);
|
|
225
219
|
try {
|
|
226
220
|
// The execution fence, read ONCE for this request and before any store
|
|
227
221
|
// lookup. `migration-locked` refuses every signal route outright — the
|
|
@@ -230,7 +224,7 @@ export function createThreadSignalRoutes(options) {
|
|
|
230
224
|
// once the thread's active run is knowable.
|
|
231
225
|
//
|
|
232
226
|
// A read that fails throws ExecutionFenceUnreadableError, which the
|
|
233
|
-
// catch below answers
|
|
227
|
+
// catch below answers — degrade closed, never a silent open.
|
|
234
228
|
const executionFence = await readExecutionFence(scope.init.executionFence);
|
|
235
229
|
if (executionFence.state === 'migration-locked') {
|
|
236
230
|
return executionFencedResponse(executionFence.state, entryPath);
|
|
@@ -266,26 +260,28 @@ export function createThreadSignalRoutes(options) {
|
|
|
266
260
|
requestedAgentId === undefined) {
|
|
267
261
|
return json({ error: 'agentId is required for notification dispatch' }, 400);
|
|
268
262
|
}
|
|
269
|
-
//
|
|
270
|
-
//
|
|
271
|
-
// resolution inside this catch-all: construction/storage failures are
|
|
272
|
-
// internal and must not escape through the outer DO error response.
|
|
263
|
+
// Host resolution stays inside this catch-all so construction and storage
|
|
264
|
+
// failures use the route's internal error response.
|
|
273
265
|
const agent = await resolveAgent(scope, requestedAgentId, entryPath);
|
|
274
266
|
if (requestedAgentId !== undefined && agent.id !== requestedAgentId) {
|
|
275
267
|
return json({ error: 'agent binding does not match' }, 404);
|
|
276
268
|
}
|
|
277
269
|
const runtimeDriven = isRuntimeDrivenAgent(agent);
|
|
278
270
|
const pubsub = scope.init.pubsub;
|
|
279
|
-
if (pubsub)
|
|
271
|
+
if (pubsub && !runtimeDriven)
|
|
280
272
|
agent.__setPubSub(pubsub);
|
|
273
|
+
if (runtimeDriven &&
|
|
274
|
+
pubsub !== undefined &&
|
|
275
|
+
(typeof agent.getPubSub !== 'function' || agent.getPubSub() !== pubsub)) {
|
|
276
|
+
return json({ error: "agent pub/sub does not match this thread's" }, 503);
|
|
277
|
+
}
|
|
281
278
|
const resourceId = resolveResourceId?.(scope);
|
|
282
279
|
const threadId = scope.threadId;
|
|
283
280
|
// proof-only admits work on ONE run, so the gate lives here — the first
|
|
284
281
|
// point where the run a signal would reach is knowable, and the only one
|
|
285
282
|
// every route passes through. Deciding it inside handleWake would leave
|
|
286
|
-
// the lanes that never reach it
|
|
287
|
-
//
|
|
288
|
-
// wake path it owns.
|
|
283
|
+
// the lanes that never reach it ungated; handleWake keeps its own check
|
|
284
|
+
// for the wake path it owns.
|
|
289
285
|
let proof;
|
|
290
286
|
if (executionFence.state === 'proof-only') {
|
|
291
287
|
const runtime = scope.init.runtime;
|
|
@@ -355,7 +351,13 @@ export function createThreadSignalRoutes(options) {
|
|
|
355
351
|
const blockingRun = resolveBlockingRun
|
|
356
352
|
? () => resolveBlockingRun(scope)
|
|
357
353
|
: undefined;
|
|
358
|
-
const
|
|
354
|
+
const schedulePersistenceAllowed = path === '/signal/schedule' && canPersistSchedule
|
|
355
|
+
? (input) => canPersistSchedule(scope, input)
|
|
356
|
+
: undefined;
|
|
357
|
+
// Schedule-owner authorization makes the request-principal check unnecessary.
|
|
358
|
+
const persistenceAllowed = canPersist && !schedulePersistenceAllowed
|
|
359
|
+
? await canPersist(scope)
|
|
360
|
+
: true;
|
|
359
361
|
const inspectContent = contentPolicy
|
|
360
362
|
? (signal, runId) => inspectSignalContent(contentPolicy, {
|
|
361
363
|
agentId: agent.id,
|
|
@@ -394,7 +396,7 @@ export function createThreadSignalRoutes(options) {
|
|
|
394
396
|
proof,
|
|
395
397
|
});
|
|
396
398
|
}
|
|
397
|
-
// POST /signal — a system signal
|
|
399
|
+
// POST /signal — a system signal with its ifActive/ifIdle behaviors.
|
|
398
400
|
if (path === '/signal') {
|
|
399
401
|
return await handleSignal(agent, body, threadId, resourceId, consultRunCap, scope.deploymentTag, entryPath, startIdleRun, serializeWake, {
|
|
400
402
|
runtimeDriven,
|
|
@@ -428,17 +430,15 @@ export function createThreadSignalRoutes(options) {
|
|
|
428
430
|
startIdleRun,
|
|
429
431
|
serializeWake,
|
|
430
432
|
blockingRun,
|
|
431
|
-
persistenceAllowed,
|
|
432
433
|
memoryAvailable,
|
|
433
|
-
schedulePersistenceAllowed:
|
|
434
|
-
? (input) => canPersistSchedule(scope, input)
|
|
435
|
-
: undefined,
|
|
434
|
+
schedulePersistenceAllowed: schedulePersistenceAllowed ?? (() => persistenceAllowed),
|
|
436
435
|
resolveRunStatus: resolveScheduleRunStatus
|
|
437
436
|
? (input) => resolveScheduleRunStatus(scope, input)
|
|
438
437
|
: undefined,
|
|
439
438
|
store: await resolveScheduleDispatchStore(scope),
|
|
440
439
|
completed: completedScheduleDispatches,
|
|
441
440
|
inspectContent,
|
|
441
|
+
scheduleProviderOptionsPolicy,
|
|
442
442
|
proof,
|
|
443
443
|
});
|
|
444
444
|
}
|
|
@@ -481,30 +481,38 @@ export function createThreadSignalRoutes(options) {
|
|
|
481
481
|
proof,
|
|
482
482
|
}));
|
|
483
483
|
}
|
|
484
|
-
// POST /signal/notification — the durable AGENT inbox
|
|
484
|
+
// POST /signal/notification — the durable AGENT inbox.
|
|
485
485
|
if (path === '/signal/notification') {
|
|
486
486
|
return await serializeNotification(async () => handleNotification(agent, body, threadId, resourceId, resolveNotificationsStorage
|
|
487
487
|
? () => resolveNotificationsStorage(scope)
|
|
488
|
-
: undefined, {
|
|
488
|
+
: undefined, {
|
|
489
|
+
principal: scope.principal,
|
|
490
|
+
blockingRun,
|
|
491
|
+
persistenceAllowed,
|
|
492
|
+
runtimeDriven,
|
|
493
|
+
memoryAvailable,
|
|
494
|
+
inspectContent,
|
|
495
|
+
notificationDispatchAllowed: notificationDispatchAllowed
|
|
496
|
+
? () => notificationDispatchAllowed(scope, agent.id)
|
|
497
|
+
: undefined,
|
|
498
|
+
proof,
|
|
499
|
+
}));
|
|
489
500
|
}
|
|
490
501
|
return json({ error: 'not found' }, 404);
|
|
491
502
|
}
|
|
492
503
|
catch (error) {
|
|
493
|
-
// The fence refusing, or failing to answer. Surfaced with its own
|
|
494
|
-
// reason rather than the 502 below: a caller must be able to tell "this
|
|
504
|
+
// The fence refusing, or failing to answer. Surfaced with its own status
|
|
505
|
+
// and reason rather than the 502 below: a caller must be able to tell "this
|
|
495
506
|
// deployment is deliberately not executing" (retry after the migration)
|
|
496
507
|
// from "the model or a route is broken".
|
|
497
508
|
if (isExecutionFenceRefusal(error)) {
|
|
498
509
|
return json({ error: error.message, reason: error.reason }, error.status);
|
|
499
510
|
}
|
|
500
|
-
if (error instanceof DoStatusError
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
? 'not found'
|
|
506
|
-
: 'conflict';
|
|
507
|
-
return json({ error: message }, error.status);
|
|
511
|
+
if (error instanceof DoStatusError) {
|
|
512
|
+
const message = SIGNAL_ROUTE_REFUSAL_MESSAGES[error.status];
|
|
513
|
+
if (message !== undefined) {
|
|
514
|
+
return json({ error: message }, error.status);
|
|
515
|
+
}
|
|
508
516
|
}
|
|
509
517
|
// A send that cannot be routed at all (e.g. an idle wake whose stream setup
|
|
510
518
|
// throws — no model) rejects `accepted`; surface it as a 502 rather than a
|
|
@@ -512,16 +520,23 @@ export function createThreadSignalRoutes(options) {
|
|
|
512
520
|
return internalErrorResponse('signals.thread', error, 502);
|
|
513
521
|
}
|
|
514
522
|
};
|
|
515
|
-
return (request, scope) => {
|
|
523
|
+
return async (request, scope) => {
|
|
516
524
|
if (request.method !== 'POST')
|
|
517
|
-
return
|
|
518
|
-
const
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
525
|
+
return null;
|
|
526
|
+
const url = new URL(request.url);
|
|
527
|
+
const path = url.pathname;
|
|
528
|
+
if (path !== '/signal' && !path.startsWith('/signal/'))
|
|
529
|
+
return null;
|
|
530
|
+
const entryPath = entryPathForSignalRoute(path);
|
|
531
|
+
if (entryPath === undefined)
|
|
532
|
+
return json({ error: 'not found' }, 404);
|
|
533
|
+
// A queued request keeps its parsed body if the sender disconnects.
|
|
534
|
+
const body = await readJson(request);
|
|
535
|
+
if (!body)
|
|
536
|
+
return json({ error: 'a JSON body is required' }, 400);
|
|
522
537
|
return serializeDispatch
|
|
523
|
-
? serializeDispatch(scope, () => route(
|
|
524
|
-
: route(
|
|
538
|
+
? serializeDispatch(scope, () => route(url, scope, entryPath, body))
|
|
539
|
+
: route(url, scope, entryPath, body);
|
|
525
540
|
};
|
|
526
541
|
}
|
|
527
542
|
async function handleNotificationDispatch(options) {
|
|
@@ -530,7 +545,9 @@ async function handleNotificationDispatch(options) {
|
|
|
530
545
|
ids.length === 0 ||
|
|
531
546
|
ids.length > MAX_NOTIFICATION_DISPATCH_IDS ||
|
|
532
547
|
!ids.every((id) => typeof id === 'string' && id.length > 0)) {
|
|
533
|
-
return json({
|
|
548
|
+
return json({
|
|
549
|
+
error: `notificationIds must contain 1-${MAX_NOTIFICATION_DISPATCH_IDS} strings`,
|
|
550
|
+
}, 400);
|
|
534
551
|
}
|
|
535
552
|
const uniqueIds = [...new Set(ids)];
|
|
536
553
|
if (options.resourceId === undefined ||
|
|
@@ -541,12 +558,7 @@ async function handleNotificationDispatch(options) {
|
|
|
541
558
|
const durableBlockingRun = await options.blockingRun?.();
|
|
542
559
|
if (durableBlockingRun &&
|
|
543
560
|
!samePrincipal(durableBlockingRun.principal, options.principal)) {
|
|
544
|
-
return
|
|
545
|
-
error: 'notification principal does not match the active run',
|
|
546
|
-
reason: 'principal-mismatch',
|
|
547
|
-
runId: durableBlockingRun.runId,
|
|
548
|
-
retry: true,
|
|
549
|
-
}, 409);
|
|
561
|
+
return notificationPrincipalMismatchResponse(durableBlockingRun.runId);
|
|
550
562
|
}
|
|
551
563
|
const now = typeof options.body.now === 'string'
|
|
552
564
|
? new Date(options.body.now)
|
|
@@ -672,17 +684,15 @@ async function handleNotificationDispatch(options) {
|
|
|
672
684
|
serializeWake: options.serializeWake,
|
|
673
685
|
executionFence: options.executionFence,
|
|
674
686
|
proof: options.proof,
|
|
675
|
-
blockingRun:
|
|
676
|
-
? () => durableBlockingRun
|
|
677
|
-
: options.blockingRun,
|
|
687
|
+
blockingRun: options.blockingRun,
|
|
678
688
|
persistenceAllowed: options.persistenceAllowed,
|
|
679
689
|
memoryAvailable: options.memoryAvailable,
|
|
680
690
|
signal: deliverableSignal,
|
|
681
|
-
deliverActive: (runId, activeMemoryAvailable) => options.agent.sendSignal(deliverableSignal, {
|
|
691
|
+
deliverActive: (runId, activeMemoryAvailable, resolvedActiveBehavior) => options.agent.sendSignal(deliverableSignal, {
|
|
682
692
|
runId,
|
|
683
693
|
threadId: options.threadId,
|
|
684
694
|
resourceId,
|
|
685
|
-
ifActive: { behavior:
|
|
695
|
+
ifActive: { behavior: resolvedActiveBehavior },
|
|
686
696
|
ifIdle: {
|
|
687
697
|
behavior: activeMemoryAvailable && options.persistenceAllowed
|
|
688
698
|
? 'persist'
|
|
@@ -725,7 +735,9 @@ async function handleNotificationDispatch(options) {
|
|
|
725
735
|
const result = options.agent.sendSignal(signal, {
|
|
726
736
|
threadId: options.threadId,
|
|
727
737
|
resourceId,
|
|
728
|
-
ifActive: {
|
|
738
|
+
ifActive: {
|
|
739
|
+
behavior: activeBehaviorForRun('deliver', durableBlockingRun),
|
|
740
|
+
},
|
|
729
741
|
ifIdle: { behavior: 'persist' },
|
|
730
742
|
});
|
|
731
743
|
await result.accepted;
|
|
@@ -879,14 +891,8 @@ async function handleNotificationDispatch(options) {
|
|
|
879
891
|
});
|
|
880
892
|
}
|
|
881
893
|
/**
|
|
882
|
-
* Resolve the idle behavior a body asked for. A `wake` STARTS a run, so
|
|
883
|
-
*
|
|
884
|
-
* principal may persist and the agent has memory; otherwise the route answers
|
|
885
|
-
* `persistence-forbidden` or `memory-unavailable` instead of silently dropping
|
|
886
|
-
* the signal:
|
|
887
|
-
* - the agent must be RUNTIME-DRIVEN (its stream re-enters RunnerRuntime, not
|
|
888
|
-
* the default engine) — else `wakeRefused:'not-runtime-driven'`;
|
|
889
|
-
* - the deployment run cap must allow it — otherwise `capped:true`.
|
|
894
|
+
* Resolve the idle behavior a body asked for. A `wake` STARTS a run, so
|
|
895
|
+
* handleWake gates it.
|
|
890
896
|
*/
|
|
891
897
|
function requestedIdle(body) {
|
|
892
898
|
const requested = body.ifIdle;
|
|
@@ -895,6 +901,34 @@ function requestedIdle(body) {
|
|
|
895
901
|
? requested
|
|
896
902
|
: 'persist';
|
|
897
903
|
}
|
|
904
|
+
/** See resolveBlockingRun for why a run that is not executing gets persist. */
|
|
905
|
+
function activeBehaviorForRun(requested, blockingRun) {
|
|
906
|
+
return requested === 'deliver' &&
|
|
907
|
+
blockingRun?.status !== undefined &&
|
|
908
|
+
blockingRun.status !== 'running'
|
|
909
|
+
? 'persist'
|
|
910
|
+
: requested;
|
|
911
|
+
}
|
|
912
|
+
function deliverableActiveBehavior(behavior, persistenceAllowed) {
|
|
913
|
+
return behavior === 'persist' && !persistenceAllowed ? 'discard' : behavior;
|
|
914
|
+
}
|
|
915
|
+
function notificationPrincipalMismatchResponse(runId) {
|
|
916
|
+
return json({
|
|
917
|
+
error: 'notification principal does not match the active run',
|
|
918
|
+
reason: 'principal-mismatch',
|
|
919
|
+
runId,
|
|
920
|
+
retry: true,
|
|
921
|
+
}, 409);
|
|
922
|
+
}
|
|
923
|
+
const OWNER_NOTIFICATION_REFUSALS = {
|
|
924
|
+
'memory-unavailable': 'notification delivery requires agent memory',
|
|
925
|
+
'notification-pending': 'a matching notification is already pending',
|
|
926
|
+
'notification-dispatch-forbidden': 'the target agent does not accept notification dispatch',
|
|
927
|
+
'thread-blocked': 'the thread is blocked by another run',
|
|
928
|
+
};
|
|
929
|
+
function ownerNotificationRefusal(reason) {
|
|
930
|
+
return json({ error: OWNER_NOTIFICATION_REFUSALS[reason], reason }, 409);
|
|
931
|
+
}
|
|
898
932
|
function principalMismatchResponse(runId) {
|
|
899
933
|
return json({
|
|
900
934
|
decision: {
|
|
@@ -907,34 +941,14 @@ function principalMismatchResponse(runId) {
|
|
|
907
941
|
}
|
|
908
942
|
/**
|
|
909
943
|
* Report a lazily computed missing-memory gate after content inspection, only
|
|
910
|
-
* where a memory write would otherwise disappear.
|
|
911
|
-
* inbox recording never use this response. On an active thread it is returned
|
|
912
|
-
* for a persist outcome that would otherwise be silently dropped — an explicit
|
|
913
|
-
* `ifActive: 'persist'`, or the stale-active-id idle fall-through — and by
|
|
914
|
-
* `/state`, whose pre-send gate replaces core's hard memory requirement rather
|
|
915
|
-
* than covering a dropped write. A default or `ifIdle: 'persist'` request to
|
|
916
|
-
* `/signal/message` or `/signal` still delivers into an active run without
|
|
917
|
-
* memory; the memory gate responds only when core's outcome for the request
|
|
918
|
-
* replaced a persist (an idle discard substituted for a requested persist, or
|
|
919
|
-
* an active persist that no memory could write).
|
|
944
|
+
* where a memory write would otherwise disappear.
|
|
920
945
|
*/
|
|
921
946
|
function memoryUnavailableResponse() {
|
|
922
947
|
return json({
|
|
923
948
|
decision: { action: 'discard', reason: 'memory-unavailable' },
|
|
924
949
|
});
|
|
925
950
|
}
|
|
926
|
-
/**
|
|
927
|
-
* Report route-specific persistence authorization failures.
|
|
928
|
-
* `/queue` and `/state` refuse every non-owner request before sending.
|
|
929
|
-
* `/signal/message` and `/signal` refuse a default or requested `ifIdle: 'persist'`;
|
|
930
|
-
* `/signal` also degrades a requested `ifActive: 'persist'`.
|
|
931
|
-
* `/notification` never refuses because a non-owner is record-only for the
|
|
932
|
-
* dispatcher.
|
|
933
|
-
* Wake handling for `/signal/message`, `/signal`, `/signal/schedule`, and the
|
|
934
|
-
* notification dispatch lane returns this response from `handleWake` when a
|
|
935
|
-
* refused or capped wake, or a stale-active-id fall-through, would otherwise
|
|
936
|
-
* persist for a non-owner.
|
|
937
|
-
*/
|
|
951
|
+
/** Report route-specific persistence authorization failures. */
|
|
938
952
|
function persistenceForbiddenResponse(options) {
|
|
939
953
|
return json({
|
|
940
954
|
decision: { action: 'discard', reason: 'persistence-forbidden' },
|
|
@@ -977,12 +991,14 @@ async function handleWake(options) {
|
|
|
977
991
|
const memoryAvailable = await options.memoryAvailable();
|
|
978
992
|
await options.proof?.check(admitted);
|
|
979
993
|
options.proof?.assertActive(admitted);
|
|
980
|
-
const
|
|
994
|
+
const activeBehavior = activeBehaviorForRun(options.activeBehavior ?? 'deliver', durableBlockingRun);
|
|
995
|
+
const deliveredActiveBehavior = deliverableActiveBehavior(activeBehavior, options.persistenceAllowed);
|
|
996
|
+
const delivered = options.deliverActive(activeRunId, memoryAvailable, deliveredActiveBehavior);
|
|
981
997
|
const decision = await delivered.accepted;
|
|
982
998
|
if (delivered.persisted)
|
|
983
999
|
await delivered.persisted;
|
|
984
1000
|
const action = recordValue(decision)?.action;
|
|
985
|
-
if (action === 'discard' &&
|
|
1001
|
+
if (action === 'discard' && options.activeBehavior !== 'discard') {
|
|
986
1002
|
return options.persistenceAllowed
|
|
987
1003
|
? memoryUnavailableResponse()
|
|
988
1004
|
: persistenceForbiddenResponse({ capped: false });
|
|
@@ -1030,13 +1046,13 @@ async function handleWake(options) {
|
|
|
1030
1046
|
? !(await options.consultRunCap())
|
|
1031
1047
|
: false;
|
|
1032
1048
|
if (refusal || capped) {
|
|
1033
|
-
//
|
|
1034
|
-
//
|
|
1049
|
+
// A drain can lose a signal here, and it is the CALLER'S choice, not the
|
|
1050
|
+
// fence's. `execution-draining` degrades a wake into a persist so
|
|
1035
1051
|
// nothing is lost while a deployment finishes its work — but a caller who
|
|
1036
1052
|
// said persistence is forbidden has already declared it does not want its
|
|
1037
1053
|
// signal parked, and honouring the fence by parking it anyway would
|
|
1038
1054
|
// override an authorization decision with an operational one. So it is
|
|
1039
|
-
// discarded,
|
|
1055
|
+
// discarded, and the response SAYS SO: `wakeRefused:
|
|
1040
1056
|
// 'execution-draining'` alongside `action: 'discard'` tells the caller
|
|
1041
1057
|
// exactly which condition dropped it, so a sender that would rather wait
|
|
1042
1058
|
// out the migration can retry instead of assuming delivery.
|
|
@@ -1149,9 +1165,9 @@ async function handleScheduleSignal(options) {
|
|
|
1149
1165
|
return json({ error: 'invalid schedule signal dispatch' }, 400);
|
|
1150
1166
|
}
|
|
1151
1167
|
const resourceId = options.resourceId;
|
|
1152
|
-
//
|
|
1153
|
-
//
|
|
1154
|
-
//
|
|
1168
|
+
// A terminal "this fire will not be delivered" outcome settles a canonical
|
|
1169
|
+
// receipt, so a replay of the dispatch returns it instead of re-deciding.
|
|
1170
|
+
// Settling is what lets the schedule advance; leaving the lease
|
|
1155
1171
|
// unsettled is reserved for outcomes a later tick could still resolve.
|
|
1156
1172
|
const settleDiscard = async () => {
|
|
1157
1173
|
const receipt = createScheduleAgentDispatchReceipt('discard', {
|
|
@@ -1245,7 +1261,24 @@ async function handleScheduleSignal(options) {
|
|
|
1245
1261
|
}
|
|
1246
1262
|
return await settleDiscard();
|
|
1247
1263
|
}
|
|
1248
|
-
const
|
|
1264
|
+
const storedProviderOptions = target.providerOptions;
|
|
1265
|
+
const scheduleProviderOptionsPolicy = options.scheduleProviderOptionsPolicy;
|
|
1266
|
+
if (scheduleProviderOptionsPolicy) {
|
|
1267
|
+
const inspection = await inspectPolicyResult(() => scheduleProviderOptionsPolicy({
|
|
1268
|
+
// The policy cannot change the options later delivered in the signal.
|
|
1269
|
+
providerOptions: structuredClone(storedProviderOptions),
|
|
1270
|
+
agentId: target.agentId,
|
|
1271
|
+
threadId: options.threadId,
|
|
1272
|
+
resourceId,
|
|
1273
|
+
}));
|
|
1274
|
+
if (inspection === 'denied') {
|
|
1275
|
+
return await settleDiscard();
|
|
1276
|
+
}
|
|
1277
|
+
if (inspection === 'error') {
|
|
1278
|
+
return json({ error: 'schedule provider options policy unavailable' }, 503);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
const baseProviderOptions = storedProviderOptions ?? {};
|
|
1249
1282
|
const baseMastra = recordValue(baseProviderOptions.mastra) ?? {};
|
|
1250
1283
|
const signal = {
|
|
1251
1284
|
id: dispatchId,
|
|
@@ -1267,21 +1300,26 @@ async function handleScheduleSignal(options) {
|
|
|
1267
1300
|
},
|
|
1268
1301
|
};
|
|
1269
1302
|
const ifActive = target.ifActive ?? { behavior: 'deliver' };
|
|
1303
|
+
const requestedActiveBehavior = ifActive.behavior ?? 'deliver';
|
|
1304
|
+
const effectiveIfActive = {
|
|
1305
|
+
...ifActive,
|
|
1306
|
+
behavior: activeBehaviorForRun(requestedActiveBehavior, durableBlockingRun),
|
|
1307
|
+
};
|
|
1270
1308
|
const ifIdle = target.ifIdle ?? { behavior: 'wake' };
|
|
1271
1309
|
const localActiveRunId = activeThreadRunIdOf(options.agent, options.threadId, resourceId);
|
|
1272
1310
|
const persistenceRequested = localActiveRunId
|
|
1273
|
-
?
|
|
1311
|
+
? effectiveIfActive.behavior === 'persist'
|
|
1274
1312
|
: (ifIdle.behavior ?? 'wake') === 'persist';
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1313
|
+
// A wake or a delivery can still end in persistence, so the schedule's
|
|
1314
|
+
// authorization governs every fire.
|
|
1315
|
+
const persistenceAllowed = await options.schedulePersistenceAllowed({
|
|
1316
|
+
scheduleId,
|
|
1317
|
+
dispatchId,
|
|
1318
|
+
runId,
|
|
1319
|
+
agentId: target.agentId,
|
|
1320
|
+
threadId: options.threadId,
|
|
1321
|
+
resourceId,
|
|
1322
|
+
});
|
|
1285
1323
|
if (persistenceRequested && !persistenceAllowed) {
|
|
1286
1324
|
return await settleDiscard();
|
|
1287
1325
|
}
|
|
@@ -1337,8 +1375,16 @@ async function handleScheduleSignal(options) {
|
|
|
1337
1375
|
const signalTarget = {
|
|
1338
1376
|
threadId: options.threadId,
|
|
1339
1377
|
resourceId,
|
|
1340
|
-
ifActive
|
|
1341
|
-
|
|
1378
|
+
ifActive: {
|
|
1379
|
+
...effectiveIfActive,
|
|
1380
|
+
behavior: deliverableActiveBehavior(effectiveIfActive.behavior, persistenceAllowed),
|
|
1381
|
+
},
|
|
1382
|
+
ifIdle: {
|
|
1383
|
+
...signalIfIdle,
|
|
1384
|
+
behavior: signalIfIdle.behavior === 'persist' && !persistenceAllowed
|
|
1385
|
+
? 'discard'
|
|
1386
|
+
: signalIfIdle.behavior,
|
|
1387
|
+
},
|
|
1342
1388
|
};
|
|
1343
1389
|
let decision;
|
|
1344
1390
|
let signalId;
|
|
@@ -1367,22 +1413,20 @@ async function handleScheduleSignal(options) {
|
|
|
1367
1413
|
serializeWake: options.serializeWake,
|
|
1368
1414
|
executionFence: options.executionFence,
|
|
1369
1415
|
proof: options.proof,
|
|
1370
|
-
blockingRun:
|
|
1371
|
-
? () => durableBlockingRun
|
|
1372
|
-
: options.blockingRun,
|
|
1416
|
+
blockingRun: options.blockingRun,
|
|
1373
1417
|
persistenceAllowed,
|
|
1374
1418
|
memoryAvailable: options.memoryAvailable,
|
|
1375
1419
|
signal: idleSignal,
|
|
1420
|
+
activeBehavior: requestedActiveBehavior,
|
|
1376
1421
|
runId,
|
|
1377
1422
|
scheduleId,
|
|
1378
1423
|
dispatchId,
|
|
1379
1424
|
safeContext: { ...requestContext, ...idleRequestContext },
|
|
1380
|
-
|
|
1381
|
-
deliverActive: (activeRunId, activeMemoryAvailable) => options.agent.sendSignal(deliverableSignal, {
|
|
1425
|
+
deliverActive: (activeRunId, activeMemoryAvailable, resolvedActiveBehavior) => options.agent.sendSignal(deliverableSignal, {
|
|
1382
1426
|
runId: activeRunId,
|
|
1383
1427
|
threadId: options.threadId,
|
|
1384
1428
|
resourceId,
|
|
1385
|
-
ifActive,
|
|
1429
|
+
ifActive: { ...ifActive, behavior: resolvedActiveBehavior },
|
|
1386
1430
|
ifIdle: {
|
|
1387
1431
|
behavior: activeMemoryAvailable && persistenceAllowed
|
|
1388
1432
|
? 'persist'
|
|
@@ -1451,8 +1495,7 @@ async function handleMessage(agent, body, threadId, resourceId, consultRunCap, d
|
|
|
1451
1495
|
const message = options.persistenceAllowed
|
|
1452
1496
|
? baseMessage
|
|
1453
1497
|
: markPersistenceForbidden(baseMessage);
|
|
1454
|
-
// sendMessage requires a resourceId+threadId target for its idle branch
|
|
1455
|
-
// host has not wired a resourceId, only the active/queue path is reachable.
|
|
1498
|
+
// sendMessage requires a resourceId+threadId target for its idle branch.
|
|
1456
1499
|
if (resourceId === undefined) {
|
|
1457
1500
|
return json({
|
|
1458
1501
|
error: 'this thread has no resourceId wired; message delivery needs one',
|
|
@@ -1481,17 +1524,15 @@ async function handleMessage(agent, body, threadId, resourceId, consultRunCap, d
|
|
|
1481
1524
|
serializeWake,
|
|
1482
1525
|
executionFence: options.executionFence,
|
|
1483
1526
|
proof: options.proof,
|
|
1484
|
-
blockingRun:
|
|
1485
|
-
? () => durableBlockingRun
|
|
1486
|
-
: options.blockingRun,
|
|
1527
|
+
blockingRun: options.blockingRun,
|
|
1487
1528
|
persistenceAllowed: options.persistenceAllowed,
|
|
1488
1529
|
memoryAvailable: options.memoryAvailable,
|
|
1489
1530
|
message,
|
|
1490
|
-
deliverActive: (runId, activeMemoryAvailable) => agent.sendMessage(message, {
|
|
1531
|
+
deliverActive: (runId, activeMemoryAvailable, resolvedActiveBehavior) => agent.sendMessage(message, {
|
|
1491
1532
|
runId,
|
|
1492
1533
|
threadId,
|
|
1493
1534
|
resourceId,
|
|
1494
|
-
ifActive: { behavior:
|
|
1535
|
+
ifActive: { behavior: resolvedActiveBehavior },
|
|
1495
1536
|
// A stale active id can disappear before core sends. Persist only
|
|
1496
1537
|
// when both the memory and authorization gates allow the write.
|
|
1497
1538
|
ifIdle: {
|
|
@@ -1510,27 +1551,16 @@ async function handleMessage(agent, body, threadId, resourceId, consultRunCap, d
|
|
|
1510
1551
|
if (behavior === 'persist' && !options.persistenceAllowed) {
|
|
1511
1552
|
return persistenceForbiddenResponse({ capped: false });
|
|
1512
1553
|
}
|
|
1513
|
-
const
|
|
1514
|
-
|
|
1515
|
-
options.
|
|
1516
|
-
const
|
|
1554
|
+
const activeBehavior = activeBehaviorForRun('deliver', durableBlockingRun);
|
|
1555
|
+
const activePersistenceForbidden = activeBehavior === 'persist' && !options.persistenceAllowed;
|
|
1556
|
+
const deliveredActiveBehavior = deliverableActiveBehavior(activeBehavior, options.persistenceAllowed);
|
|
1557
|
+
const sent = await sendWithoutWake(agent, (sendOptions) => agent.sendMessage(message, sendOptions), {
|
|
1517
1558
|
threadId,
|
|
1518
1559
|
resourceId,
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
await result.persisted;
|
|
1524
|
-
if (behavior === 'persist' &&
|
|
1525
|
-
!memoryAvailable &&
|
|
1526
|
-
recordValue(decision)?.action === 'discard') {
|
|
1527
|
-
return memoryUnavailableResponse();
|
|
1528
|
-
}
|
|
1529
|
-
return json({
|
|
1530
|
-
decision,
|
|
1531
|
-
capped: false,
|
|
1532
|
-
signalId: result.signal.id,
|
|
1533
|
-
});
|
|
1560
|
+
ifActive: deliveredActiveBehavior,
|
|
1561
|
+
ifIdle: behavior,
|
|
1562
|
+
}, options);
|
|
1563
|
+
return noWakeResponse(sent, behavior, deliveredActiveBehavior, activePersistenceForbidden);
|
|
1534
1564
|
}
|
|
1535
1565
|
async function handleQueue(agent, body, threadId, resourceId, options) {
|
|
1536
1566
|
if (!isContents(body.contents)) {
|
|
@@ -1576,11 +1606,16 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
|
|
|
1576
1606
|
// Route-level tagName defense: reject a non-XML-name tagName HERE with a
|
|
1577
1607
|
// 400, rather than letting core's signalToXmlMarkup throw at render time inside
|
|
1578
1608
|
// the agent turn. Core still escapes contents/attribute values and re-validates
|
|
1579
|
-
// names
|
|
1609
|
+
// names.
|
|
1580
1610
|
if (typeof body.tagName === 'string' &&
|
|
1581
1611
|
!XML_NAME_PATTERN.test(body.tagName)) {
|
|
1582
1612
|
return json({ error: 'tagName is not a valid XML name' }, 400);
|
|
1583
1613
|
}
|
|
1614
|
+
if (resourceId === undefined) {
|
|
1615
|
+
return json({
|
|
1616
|
+
error: 'this thread has no resourceId wired; signal delivery needs one',
|
|
1617
|
+
}, 409);
|
|
1618
|
+
}
|
|
1584
1619
|
const baseSignal = {
|
|
1585
1620
|
type: 'reactive',
|
|
1586
1621
|
contents: body.contents,
|
|
@@ -1594,10 +1629,6 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
|
|
|
1594
1629
|
ACTIVE_BEHAVIORS.includes(body.ifActive)
|
|
1595
1630
|
? body.ifActive
|
|
1596
1631
|
: 'deliver';
|
|
1597
|
-
const activePersistenceForbidden = activeBehavior === 'persist' && !options.persistenceAllowed;
|
|
1598
|
-
const deliveredActiveBehavior = activePersistenceForbidden
|
|
1599
|
-
? 'discard'
|
|
1600
|
-
: activeBehavior;
|
|
1601
1632
|
const durableBlockingRun = await options.blockingRun?.();
|
|
1602
1633
|
if (durableBlockingRun &&
|
|
1603
1634
|
!samePrincipal(durableBlockingRun.principal, options.principal)) {
|
|
@@ -1606,22 +1637,9 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
|
|
|
1606
1637
|
const policyRefusal = await contentPolicyRefusal(options.inspectContent, createSignal(signal), durableBlockingRun?.runId);
|
|
1607
1638
|
if (policyRefusal)
|
|
1608
1639
|
return policyRefusal;
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
if (!isPathSafeId(runId)) {
|
|
1613
|
-
throw new Error('thread signal generated a non-path-safe run id');
|
|
1614
|
-
}
|
|
1615
|
-
await options.proof?.check();
|
|
1616
|
-
options.proof?.assertActive();
|
|
1617
|
-
const result = agent.sendSignal(signal, {
|
|
1618
|
-
threadId,
|
|
1619
|
-
runId,
|
|
1620
|
-
ifActive: { behavior: deliveredActiveBehavior },
|
|
1621
|
-
});
|
|
1622
|
-
const decision = await result.accepted;
|
|
1623
|
-
return json({ decision, signalId: result.signal.id });
|
|
1624
|
-
}
|
|
1640
|
+
const effectiveActiveBehavior = activeBehaviorForRun(activeBehavior, durableBlockingRun);
|
|
1641
|
+
const activePersistenceForbidden = effectiveActiveBehavior === 'persist' && !options.persistenceAllowed;
|
|
1642
|
+
const deliveredActiveBehavior = deliverableActiveBehavior(effectiveActiveBehavior, options.persistenceAllowed);
|
|
1625
1643
|
const behavior = requestedIdle(body);
|
|
1626
1644
|
if (behavior === 'wake') {
|
|
1627
1645
|
return handleWake({
|
|
@@ -1637,18 +1655,16 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
|
|
|
1637
1655
|
serializeWake,
|
|
1638
1656
|
executionFence: options.executionFence,
|
|
1639
1657
|
proof: options.proof,
|
|
1640
|
-
blockingRun:
|
|
1641
|
-
? () => durableBlockingRun
|
|
1642
|
-
: options.blockingRun,
|
|
1658
|
+
blockingRun: options.blockingRun,
|
|
1643
1659
|
persistenceAllowed: options.persistenceAllowed,
|
|
1644
1660
|
memoryAvailable: options.memoryAvailable,
|
|
1645
1661
|
signal,
|
|
1646
|
-
|
|
1647
|
-
deliverActive: (runId, activeMemoryAvailable) => agent.sendSignal(signal, {
|
|
1662
|
+
activeBehavior,
|
|
1663
|
+
deliverActive: (runId, activeMemoryAvailable, resolvedActiveBehavior) => agent.sendSignal(signal, {
|
|
1648
1664
|
runId,
|
|
1649
1665
|
threadId,
|
|
1650
1666
|
resourceId,
|
|
1651
|
-
ifActive: { behavior:
|
|
1667
|
+
ifActive: { behavior: resolvedActiveBehavior },
|
|
1652
1668
|
ifIdle: {
|
|
1653
1669
|
behavior: activeMemoryAvailable && options.persistenceAllowed
|
|
1654
1670
|
? 'persist'
|
|
@@ -1666,19 +1682,16 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
|
|
|
1666
1682
|
if (behavior === 'persist' && !options.persistenceAllowed) {
|
|
1667
1683
|
return persistenceForbiddenResponse({ capped: false });
|
|
1668
1684
|
}
|
|
1669
|
-
const
|
|
1670
|
-
const wasActive = activeThreadRunIdOf(agent, threadId, resourceId) !== undefined;
|
|
1671
|
-
await options.proof?.check();
|
|
1672
|
-
options.proof?.assertActive();
|
|
1673
|
-
const result = agent.sendSignal(signal, {
|
|
1685
|
+
const sent = await sendWithoutWake(agent, (sendOptions) => agent.sendSignal(signal, sendOptions), {
|
|
1674
1686
|
threadId,
|
|
1675
1687
|
resourceId,
|
|
1676
|
-
ifActive:
|
|
1677
|
-
ifIdle:
|
|
1678
|
-
});
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1688
|
+
ifActive: deliveredActiveBehavior,
|
|
1689
|
+
ifIdle: behavior,
|
|
1690
|
+
}, options);
|
|
1691
|
+
return noWakeResponse(sent, behavior, deliveredActiveBehavior, activePersistenceForbidden);
|
|
1692
|
+
}
|
|
1693
|
+
function noWakeResponse(sent, behavior, deliveredActiveBehavior, activePersistenceForbidden) {
|
|
1694
|
+
const { decision, signal, wasActive, memoryAvailable } = sent;
|
|
1682
1695
|
const action = recordValue(decision)?.action;
|
|
1683
1696
|
if (action === 'persist' && !memoryAvailable) {
|
|
1684
1697
|
return memoryUnavailableResponse();
|
|
@@ -1697,8 +1710,30 @@ async function handleSignal(agent, body, threadId, resourceId, consultRunCap, de
|
|
|
1697
1710
|
return json({
|
|
1698
1711
|
decision,
|
|
1699
1712
|
capped: false,
|
|
1700
|
-
signalId:
|
|
1713
|
+
signalId: signal.id,
|
|
1714
|
+
});
|
|
1715
|
+
}
|
|
1716
|
+
/**
|
|
1717
|
+
* Send a signal or message that never wakes a run, behind the proof gate.
|
|
1718
|
+
* Without agent memory the idle behavior becomes `discard`, because core's
|
|
1719
|
+
* idle persist reports `persist` even when no memory wrote the signal.
|
|
1720
|
+
*/
|
|
1721
|
+
async function sendWithoutWake(agent, send, target, options) {
|
|
1722
|
+
const memoryAvailable = await options.memoryAvailable();
|
|
1723
|
+
const wasActive = activeThreadRunIdOf(agent, target.threadId, target.resourceId) !==
|
|
1724
|
+
undefined;
|
|
1725
|
+
await options.proof?.check();
|
|
1726
|
+
options.proof?.assertActive();
|
|
1727
|
+
const result = send({
|
|
1728
|
+
threadId: target.threadId,
|
|
1729
|
+
resourceId: target.resourceId,
|
|
1730
|
+
ifActive: { behavior: target.ifActive },
|
|
1731
|
+
ifIdle: { behavior: memoryAvailable ? target.ifIdle : 'discard' },
|
|
1701
1732
|
});
|
|
1733
|
+
const decision = await result.accepted;
|
|
1734
|
+
if (result.persisted)
|
|
1735
|
+
await result.persisted;
|
|
1736
|
+
return { decision, signal: result.signal, wasActive, memoryAvailable };
|
|
1702
1737
|
}
|
|
1703
1738
|
async function handleState(agent, body, threadId, resourceId, options) {
|
|
1704
1739
|
if (typeof body.id !== 'string' || typeof body.cacheKey !== 'string') {
|
|
@@ -1769,6 +1804,27 @@ async function handleState(agent, body, threadId, resourceId, options) {
|
|
|
1769
1804
|
...(!options.runtimeDriven ? { degraded } : {}),
|
|
1770
1805
|
});
|
|
1771
1806
|
}
|
|
1807
|
+
function hasDueTime(row) {
|
|
1808
|
+
return row.deliverAt !== undefined || row.summaryAt !== undefined;
|
|
1809
|
+
}
|
|
1810
|
+
async function matchingPendingNotifications(storage, notification, threadId, resourceId, agentId) {
|
|
1811
|
+
if (!notification.dedupeKey && !notification.coalesceKey)
|
|
1812
|
+
return [];
|
|
1813
|
+
const pending = await storage.listNotifications({
|
|
1814
|
+
threadId,
|
|
1815
|
+
status: 'pending',
|
|
1816
|
+
source: notification.source,
|
|
1817
|
+
agentId,
|
|
1818
|
+
resourceId,
|
|
1819
|
+
});
|
|
1820
|
+
// Storage coalesces into a pending row with the same source, kind, agent,
|
|
1821
|
+
// resource and key. D1 can match an empty key when the other key is truthy.
|
|
1822
|
+
return pending.filter((row) => row.kind === notification.kind &&
|
|
1823
|
+
((notification.dedupeKey !== undefined &&
|
|
1824
|
+
row.dedupeKey === notification.dedupeKey) ||
|
|
1825
|
+
(notification.coalesceKey !== undefined &&
|
|
1826
|
+
row.coalesceKey === notification.coalesceKey)));
|
|
1827
|
+
}
|
|
1772
1828
|
async function handleNotification(agent, body, threadId, resourceId, resolveNotificationsStorage, options) {
|
|
1773
1829
|
if (typeof body.source !== 'string' ||
|
|
1774
1830
|
typeof body.kind !== 'string' ||
|
|
@@ -1792,30 +1848,30 @@ async function handleNotification(agent, body, threadId, resourceId, resolveNoti
|
|
|
1792
1848
|
: {}),
|
|
1793
1849
|
...(isAttributes(body.attributes) ? { attributes: body.attributes } : {}),
|
|
1794
1850
|
};
|
|
1795
|
-
// Owners use core's delivery policy. The inbox row is the durable artifact,
|
|
1796
|
-
// so this route has no memory gate: without agent memory, a model-visible
|
|
1797
|
-
// persist is best-effort and the row stays pending. Non-owner notifications
|
|
1798
|
-
// are record-only here: the host's createNotificationDispatchTick delivers
|
|
1799
|
-
// them later (agent-starter runs it every 60 seconds). A host without that
|
|
1800
|
-
// tick records but never delivers them; the spike intentionally has no tick
|
|
1801
|
-
// and its provider probes assert only the inbox row. This branch bypasses an
|
|
1802
|
-
// agent-level notifications.deliveryPolicy and
|
|
1803
|
-
// __ensureNotificationDispatchReady; branded hosts cannot reach either seam
|
|
1804
|
-
// through the wrapped agent anyway.
|
|
1805
1851
|
// The target requires a resourceId because the inbox is keyed by its owner.
|
|
1806
|
-
// Core's default policy does not summarize an idle thread immediately. A
|
|
1807
|
-
// medium-priority delivery can sample active, then fall idle across its
|
|
1808
|
-
// awaits. For a branded runner the resulting forced wake reaches the terminal
|
|
1809
|
-
// refusal path while the created record stays pending with its summary signal
|
|
1810
|
-
// id. An unbranded agent keeps core's own below-boundary run start; no shipped
|
|
1811
|
-
// host uses that degraded configuration.
|
|
1812
1852
|
if (resourceId === undefined) {
|
|
1813
1853
|
return json({ error: 'this thread has no resourceId wired; notifications need one' }, 409);
|
|
1814
1854
|
}
|
|
1815
|
-
//
|
|
1816
|
-
//
|
|
1817
|
-
//
|
|
1818
|
-
//
|
|
1855
|
+
// An owner's notification to a runtime-driven agent is recorded, then
|
|
1856
|
+
// delivered at ingestion under the owner's principal: into the owner's
|
|
1857
|
+
// running run, otherwise persisted to memory. It never wakes a run, and its
|
|
1858
|
+
// row is never due, so the dispatcher, whose deliveries and wakes belong to
|
|
1859
|
+
// the dispatch principal, never selects it. Core's sendNotificationSignal
|
|
1860
|
+
// cannot serve it: its in-process delivery reads the notifications store
|
|
1861
|
+
// from the agent's own Mastra, and the FlowSafe wrapper is registered on
|
|
1862
|
+
// none. The branch keys on the brand rather than on Mastra registration
|
|
1863
|
+
// because a registered wrapper still carries no delivery policy (see
|
|
1864
|
+
// "DurableAgent does not forward the wrapped Agent's `notifications`" in
|
|
1865
|
+
// agent-runner/durable-agent-runner.ts).
|
|
1866
|
+
if (options.persistenceAllowed && options.runtimeDriven) {
|
|
1867
|
+
return handleOwnerNotification(agent, notification, threadId, resourceId, resolveNotificationsStorage, options);
|
|
1868
|
+
}
|
|
1869
|
+
// On core's delivery path this gate is AUTHORITATIVE, not a preview: core can
|
|
1870
|
+
// send an individual or summary signal before the record reaches the
|
|
1871
|
+
// dispatcher's second gate. On the record-only branch it refuses the row
|
|
1872
|
+
// before it is written. Storage owns the id, timestamps, and coalescing, so
|
|
1873
|
+
// inspect a prospective record carrying every untrusted model-visible field
|
|
1874
|
+
// instead.
|
|
1819
1875
|
if (options.inspectContent) {
|
|
1820
1876
|
const prospectiveRecord = {
|
|
1821
1877
|
id: 'prospective',
|
|
@@ -1834,11 +1890,11 @@ async function handleNotification(agent, body, threadId, resourceId, resolveNoti
|
|
|
1834
1890
|
createdAt: new Date(0),
|
|
1835
1891
|
updatedAt: new Date(0),
|
|
1836
1892
|
};
|
|
1837
|
-
// Core
|
|
1838
|
-
//
|
|
1839
|
-
//
|
|
1840
|
-
//
|
|
1841
|
-
//
|
|
1893
|
+
// Core's delivery policy, or the dispatcher on a later tick, picks between
|
|
1894
|
+
// two renderings, so inspect both. The individual signal renders
|
|
1895
|
+
// `status="delivered"` because that is the status its sender stamps on it;
|
|
1896
|
+
// the summary renders from the row as stored, which is why it keeps
|
|
1897
|
+
// `status:'pending'`.
|
|
1842
1898
|
for (const candidate of [
|
|
1843
1899
|
createNotificationSignal({ ...prospectiveRecord, status: 'delivered' }),
|
|
1844
1900
|
createNotificationSummarySignal(summarizeNotifications([prospectiveRecord])),
|
|
@@ -1848,11 +1904,24 @@ async function handleNotification(agent, body, threadId, resourceId, resolveNoti
|
|
|
1848
1904
|
return policyRefusal;
|
|
1849
1905
|
}
|
|
1850
1906
|
}
|
|
1907
|
+
// A non-owner's notification is record-only for any agent: the row is due
|
|
1908
|
+
// now, and the host's createNotificationDispatchTick delivers it as the
|
|
1909
|
+
// dispatch principal. A host without that tick records it but never
|
|
1910
|
+
// delivers it. The record-only branch bypasses an agent-level
|
|
1911
|
+
// notifications.deliveryPolicy and __ensureNotificationDispatchReady.
|
|
1851
1912
|
if (!options.persistenceAllowed) {
|
|
1852
1913
|
const storage = await resolveNotificationsStorage?.();
|
|
1853
1914
|
if (!storage) {
|
|
1854
1915
|
return json({ error: 'notifications storage unavailable' }, 409);
|
|
1855
1916
|
}
|
|
1917
|
+
if (options.notificationDispatchAllowed &&
|
|
1918
|
+
!(await options.notificationDispatchAllowed())) {
|
|
1919
|
+
return ownerNotificationRefusal('notification-dispatch-forbidden');
|
|
1920
|
+
}
|
|
1921
|
+
// Owner residue cannot become due under the dispatch principal.
|
|
1922
|
+
if ((await matchingPendingNotifications(storage, notification, threadId, resourceId, agent.id)).some((row) => !hasDueTime(row))) {
|
|
1923
|
+
return ownerNotificationRefusal('notification-pending');
|
|
1924
|
+
}
|
|
1856
1925
|
await options.proof?.check();
|
|
1857
1926
|
options.proof?.assertActive();
|
|
1858
1927
|
const record = await storage.createNotification({
|
|
@@ -1867,24 +1936,142 @@ async function handleNotification(agent, body, threadId, resourceId, resolveNoti
|
|
|
1867
1936
|
delivery: { action: 'deferred', reason: 'dispatcher' },
|
|
1868
1937
|
});
|
|
1869
1938
|
}
|
|
1939
|
+
// Agent.sendNotificationSignal can wake a notification summary through the
|
|
1940
|
+
// thread runtime outside RunnerRuntime, the host entry gate and the run cap.
|
|
1941
|
+
// The inbox row is durable without agent memory; model-visible persistence
|
|
1942
|
+
// is best-effort and the row stays pending when memory is unavailable.
|
|
1943
|
+
const notificationsStore = await agent
|
|
1944
|
+
.getMastraInstance?.()
|
|
1945
|
+
?.getStorage()
|
|
1946
|
+
?.getStore('notifications');
|
|
1947
|
+
if (!notificationsStore) {
|
|
1948
|
+
return json({ error: 'notifications storage unavailable' }, 409);
|
|
1949
|
+
}
|
|
1870
1950
|
await options.proof?.check();
|
|
1871
1951
|
options.proof?.assertActive();
|
|
1872
1952
|
const result = await agent.sendNotificationSignal(notification, {
|
|
1873
1953
|
threadId,
|
|
1874
1954
|
resourceId,
|
|
1875
|
-
|
|
1876
|
-
? { ifActive: { behavior: 'persist' } }
|
|
1877
|
-
: {}),
|
|
1955
|
+
ifActive: { behavior: 'persist' },
|
|
1878
1956
|
ifIdle: { behavior: 'persist' },
|
|
1879
1957
|
});
|
|
1880
1958
|
if (result.persisted)
|
|
1881
1959
|
await result.persisted;
|
|
1882
1960
|
const response = {
|
|
1883
1961
|
record: result,
|
|
1884
|
-
|
|
1962
|
+
degraded: 'not-runtime-driven',
|
|
1885
1963
|
};
|
|
1886
1964
|
if (result.accepted)
|
|
1887
1965
|
response.delivery = await result.accepted;
|
|
1888
1966
|
return json(response);
|
|
1889
1967
|
}
|
|
1968
|
+
async function handleOwnerNotification(agent, notification, threadId, resourceId, resolveNotificationsStorage, options) {
|
|
1969
|
+
const durableBlockingRun = await options.blockingRun?.();
|
|
1970
|
+
if (durableBlockingRun &&
|
|
1971
|
+
!samePrincipal(durableBlockingRun.principal, options.principal)) {
|
|
1972
|
+
return notificationPrincipalMismatchResponse(durableBlockingRun.runId);
|
|
1973
|
+
}
|
|
1974
|
+
const ifActive = activeBehaviorForRun('deliver', durableBlockingRun);
|
|
1975
|
+
const memoryAvailable = await options.memoryAvailable();
|
|
1976
|
+
if (!memoryAvailable &&
|
|
1977
|
+
(ifActive === 'persist' ||
|
|
1978
|
+
activeThreadRunIdOf(agent, threadId, resourceId) === undefined)) {
|
|
1979
|
+
return ownerNotificationRefusal('memory-unavailable');
|
|
1980
|
+
}
|
|
1981
|
+
const storage = await resolveNotificationsStorage?.();
|
|
1982
|
+
if (!storage) {
|
|
1983
|
+
return json({ error: 'notifications storage unavailable' }, 409);
|
|
1984
|
+
}
|
|
1985
|
+
if ((await matchingPendingNotifications(storage, notification, threadId, resourceId, agent.id)).some(hasDueTime)) {
|
|
1986
|
+
return ownerNotificationRefusal('notification-pending');
|
|
1987
|
+
}
|
|
1988
|
+
await options.proof?.check();
|
|
1989
|
+
options.proof?.assertActive();
|
|
1990
|
+
// No deliverAt or summaryAt, so no dispatch pass ever selects the row.
|
|
1991
|
+
const stored = await storage.createNotification({
|
|
1992
|
+
...notification,
|
|
1993
|
+
id: crypto.randomUUID(),
|
|
1994
|
+
threadId,
|
|
1995
|
+
resourceId,
|
|
1996
|
+
agentId: agent.id,
|
|
1997
|
+
});
|
|
1998
|
+
const now = new Date();
|
|
1999
|
+
let settled = false;
|
|
2000
|
+
let accepted;
|
|
2001
|
+
// Every settle write passes the proof gate first. When the gate refuses, the
|
|
2002
|
+
// row stays pending with no due time rather than bypass it.
|
|
2003
|
+
const settle = async (patch) => {
|
|
2004
|
+
await options.proof?.check();
|
|
2005
|
+
options.proof?.assertActive();
|
|
2006
|
+
const record = await storage.updateNotification({
|
|
2007
|
+
...patch,
|
|
2008
|
+
id: stored.id,
|
|
2009
|
+
threadId: stored.threadId,
|
|
2010
|
+
lastDeliveryAttemptAt: now,
|
|
2011
|
+
});
|
|
2012
|
+
settled = true;
|
|
2013
|
+
return record;
|
|
2014
|
+
};
|
|
2015
|
+
try {
|
|
2016
|
+
// Built from the stored row, which a coalesce may have merged, so the
|
|
2017
|
+
// policy inspects exactly the signal that is sent.
|
|
2018
|
+
const signal = createNotificationSignal({
|
|
2019
|
+
...stored,
|
|
2020
|
+
status: 'delivered',
|
|
2021
|
+
deliveredAt: now,
|
|
2022
|
+
});
|
|
2023
|
+
const inspection = options.inspectContent
|
|
2024
|
+
? await options.inspectContent(signal, durableBlockingRun?.runId)
|
|
2025
|
+
: 'allowed';
|
|
2026
|
+
if (inspection !== 'allowed') {
|
|
2027
|
+
await settle({
|
|
2028
|
+
status: 'discarded',
|
|
2029
|
+
deliveryReason: inspection === 'denied'
|
|
2030
|
+
? 'content-policy-denied'
|
|
2031
|
+
: 'content-policy-error',
|
|
2032
|
+
});
|
|
2033
|
+
return signalContentPolicyResponse(inspection);
|
|
2034
|
+
}
|
|
2035
|
+
const sent = await sendWithoutWake(agent, (sendOptions) => agent.sendSignal(signal, sendOptions), { threadId, resourceId, ifActive, ifIdle: 'persist' }, options);
|
|
2036
|
+
const decision = recordValue(sent.decision);
|
|
2037
|
+
const action = decision?.action;
|
|
2038
|
+
if (action === 'deliver' ||
|
|
2039
|
+
(action === 'persist' && sent.memoryAvailable)) {
|
|
2040
|
+
accepted = {
|
|
2041
|
+
signalId: sent.signal.id,
|
|
2042
|
+
delivery: { ...decision, signalId: sent.signal.id },
|
|
2043
|
+
};
|
|
2044
|
+
const record = await settle({
|
|
2045
|
+
status: 'delivered',
|
|
2046
|
+
deliveredSignalId: accepted.signalId,
|
|
2047
|
+
});
|
|
2048
|
+
return json({ record, delivery: accepted.delivery });
|
|
2049
|
+
}
|
|
2050
|
+
const cause = (action === 'persist' || action === 'discard') && !sent.memoryAvailable
|
|
2051
|
+
? 'memory-unavailable'
|
|
2052
|
+
: action === 'blocked'
|
|
2053
|
+
? 'thread-blocked'
|
|
2054
|
+
: 'delivery-failed';
|
|
2055
|
+
await settle({ status: 'discarded', deliveryReason: cause });
|
|
2056
|
+
if (cause === 'delivery-failed') {
|
|
2057
|
+
throw new Error(`owner notification was not delivered (${String(action ?? 'unknown')})`);
|
|
2058
|
+
}
|
|
2059
|
+
return ownerNotificationRefusal(cause);
|
|
2060
|
+
}
|
|
2061
|
+
catch (error) {
|
|
2062
|
+
if (isExecutionFenceRefusal(error))
|
|
2063
|
+
throw error;
|
|
2064
|
+
if (accepted !== undefined) {
|
|
2065
|
+
const record = await settle({
|
|
2066
|
+
status: 'delivered',
|
|
2067
|
+
deliveredSignalId: accepted.signalId,
|
|
2068
|
+
});
|
|
2069
|
+
return json({ record, delivery: accepted.delivery });
|
|
2070
|
+
}
|
|
2071
|
+
if (!settled) {
|
|
2072
|
+
await settle({ status: 'discarded', deliveryReason: 'delivery-failed' });
|
|
2073
|
+
}
|
|
2074
|
+
throw error;
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
1890
2077
|
//# sourceMappingURL=thread-do-routes.js.map
|