@yielded/agent 0.1.0-beta.172 → 0.1.0-beta.174
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/dist/{Admin-BX9ns28E.d.mts → Admin-BHmp2V9p.d.mts} +25 -16
- package/dist/{Agent-9aBoIi74.d.mts → Agent-B39jRnFv.d.mts} +2 -2
- package/dist/{Agent-0WvBWWbM.mjs → Agent-BiRbr73L.mjs} +7 -6
- package/dist/Agent-BiRbr73L.mjs.map +1 -0
- package/dist/{AgentRuntime-CQ-qcETl.mjs → AgentRuntime-B6qZlD3f.mjs} +199 -191
- package/dist/AgentRuntime-B6qZlD3f.mjs.map +1 -0
- package/dist/{AgentRuntime-BfP14534.d.mts → AgentRuntime-BD3ZHAMq.d.mts} +10 -10
- package/dist/{Digest-DCuPlhNk.mjs → Digest-EpeqWEdv.mjs} +41 -14
- package/dist/Digest-EpeqWEdv.mjs.map +1 -0
- package/dist/{DurableAgentRuntime-BAmSkWYr.mjs → DurableAgentRuntime-CBBjRNJN.mjs} +1447 -1424
- package/dist/DurableAgentRuntime-CBBjRNJN.mjs.map +1 -0
- package/dist/{EventSource-BwliwQz9.d.mts → EventSource-C9aaJaXT.d.mts} +2 -2
- package/dist/{MessageDelivery-CeSDfHZa.d.mts → MessageDelivery-BGDm-c46.d.mts} +4 -4
- package/dist/{Messaging-C_6OOg_s.d.mts → Messaging-SVu6rWV8.d.mts} +12 -6
- package/dist/{MessagingHost-B8oKM7AC.d.mts → MessagingHost-C6SOAcoG.d.mts} +2 -2
- package/dist/{Output-Dsx6k52t.d.mts → Output-CRamN1Fy.d.mts} +2 -2
- package/dist/{PreparedInputAdmission-CxSJSiTl.d.mts → PreparedInputAdmission-BvDqlgiP.d.mts} +3 -3
- package/dist/{RunContinuation-DHYNAQFi.mjs → RunContinuation-4ZATcVLB.mjs} +113 -56
- package/dist/RunContinuation-4ZATcVLB.mjs.map +1 -0
- package/dist/{RunContinuation-FHM7XPjq.d.mts → RunContinuation-CTJ1jGrO.d.mts} +6 -6
- package/dist/{RunJournal-kfKVxJj7.mjs → RunJournal-CNZqA3Ge.mjs} +33 -5
- package/dist/RunJournal-CNZqA3Ge.mjs.map +1 -0
- package/dist/{RunOptions-D8zxFIrZ.d.mts → RunOptions-Be77lKVZ.d.mts} +2 -2
- package/dist/{RunStorage-uqPxps09.mjs → RunStorage-BHjuuERI.mjs} +9 -6
- package/dist/RunStorage-BHjuuERI.mjs.map +1 -0
- package/dist/{Scheduling-CVyfXlBR.mjs → Scheduling-NG8slKdI.mjs} +11 -11
- package/dist/Scheduling-NG8slKdI.mjs.map +1 -0
- package/dist/{Subagent-vpffemql.mjs → Subagent-CTIpnzlB.mjs} +53 -40
- package/dist/Subagent-CTIpnzlB.mjs.map +1 -0
- package/dist/{Subagent-E6tvd7-k.d.mts → Subagent-DKhZA72u.d.mts} +19 -8
- package/dist/{SubagentHost-B6w2m-SI.d.mts → SubagentHost-D1xBgn9g.d.mts} +2 -2
- package/dist/{Subscription-DONig54Q.d.mts → Subscription-BNg9VCRl.d.mts} +2 -2
- package/dist/{SubscriptionInput-mJIRxWJM.d.mts → SubscriptionInput-BUoXv-_K.d.mts} +2 -2
- package/dist/{ThreadImport-CoQTZywm.d.mts → ThreadImport-CMgzttvr.d.mts} +43 -43
- package/dist/{ThreadImport-DckUMEqJ.mjs → ThreadImport-CodYoJoa.mjs} +7 -7
- package/dist/ThreadImport-CodYoJoa.mjs.map +1 -0
- package/dist/{ThreadStore-CEekRa80.mjs → ThreadStore-59uDGXul.mjs} +163 -84
- package/dist/ThreadStore-59uDGXul.mjs.map +1 -0
- package/dist/{ThreadTransfer-B4gTQLIl.mjs → ThreadTransfer-3jigUl3g.mjs} +3 -3
- package/dist/{ThreadTransfer-B4gTQLIl.mjs.map → ThreadTransfer-3jigUl3g.mjs.map} +1 -1
- package/dist/{WorkerAdmission-DFVbbVh2.mjs → WorkerAdmission-DDYJdOsU.mjs} +4 -4
- package/dist/{WorkerAdmission-DFVbbVh2.mjs.map → WorkerAdmission-DDYJdOsU.mjs.map} +1 -1
- package/dist/{agent-registration-ByMZ_cK5.mjs → agent-registration-DRbyhAu-.mjs} +69 -56
- package/dist/agent-registration-DRbyhAu-.mjs.map +1 -0
- package/dist/capabilities/Approval.mjs +2 -2
- package/dist/capabilities/Approval.mjs.map +1 -1
- package/dist/capabilities/BrowserUse.d.mts +102 -1867
- package/dist/capabilities/BrowserUse.mjs +74 -798
- package/dist/capabilities/BrowserUse.mjs.map +1 -1
- package/dist/capabilities/Budget.mjs +3 -1
- package/dist/capabilities/Budget.mjs.map +1 -1
- package/dist/capabilities/CodeMode.d.mts +1 -1
- package/dist/capabilities/CodeMode.mjs +16 -9
- package/dist/capabilities/CodeMode.mjs.map +1 -1
- package/dist/capabilities/Commands.d.mts +1 -1
- package/dist/capabilities/McpClient.mjs +1 -1
- package/dist/capabilities/McpClient.mjs.map +1 -1
- package/dist/capabilities/Messaging.d.mts +1 -1
- package/dist/capabilities/Messaging.mjs +4 -4
- package/dist/capabilities/Messaging.mjs.map +1 -1
- package/dist/capabilities/ModelContext.mjs +7 -5
- package/dist/capabilities/ModelContext.mjs.map +1 -1
- package/dist/capabilities/Redaction.mjs +49 -19
- package/dist/capabilities/Redaction.mjs.map +1 -1
- package/dist/capabilities/RunHooks.d.mts +1 -1
- package/dist/capabilities/RunHooks.mjs +1 -1
- package/dist/capabilities/RunHooks.mjs.map +1 -1
- package/dist/capabilities/Subagent.d.mts +1 -1
- package/dist/capabilities/Subagent.mjs +1 -1
- package/dist/capabilities/SubagentReservations.mjs +3 -1
- package/dist/capabilities/SubagentReservations.mjs.map +1 -1
- package/dist/capabilities/WebCapture.mjs.map +1 -1
- package/dist/capabilities/WebSearch.mjs +7 -7
- package/dist/capabilities/WebSearch.mjs.map +1 -1
- package/dist/core/Agent.d.mts +1 -1
- package/dist/core/Agent.mjs +1 -1
- package/dist/core/AgentUpdates.mjs +8 -8
- package/dist/core/AgentUpdates.mjs.map +1 -1
- package/dist/core/Thread.mjs +1 -1
- package/dist/core/Thread.mjs.map +1 -1
- package/dist/core/Usage.mjs +2 -2
- package/dist/core/Usage.mjs.map +1 -1
- package/dist/durable/Admin.d.mts +1 -1
- package/dist/durable/AgentRegistration.d.mts +1 -1
- package/dist/durable/AgentRegistration.mjs +1 -1
- package/dist/durable/Certification.d.mts +1 -1
- package/dist/durable/CommittedActivity.d.mts +1 -1
- package/dist/durable/CommittedActivity.mjs +2 -2
- package/dist/durable/Digest.mjs +1 -1
- package/dist/durable/DurableAgentRuntime.d.mts +1 -1
- package/dist/durable/DurableAgentRuntime.mjs +2 -2
- package/dist/durable/EventSource.d.mts +1 -1
- package/dist/durable/EventSource.mjs +6 -6
- package/dist/durable/EventSource.mjs.map +1 -1
- package/dist/durable/GitHubWorkflowSource.d.mts +3 -3
- package/dist/durable/LifecyclePublication.d.mts +1 -1
- package/dist/durable/LifecyclePublication.mjs +1 -1
- package/dist/durable/MessageDelivery.d.mts +1 -1
- package/dist/durable/MessageDelivery.mjs +1 -1
- package/dist/durable/MessageDeliveryStoreConformance.d.mts +2 -2
- package/dist/durable/MessageDeliveryStoreConformance.mjs +1 -1
- package/dist/durable/PersistentHistory.d.mts +1 -1
- package/dist/durable/PersistentHistory.mjs +3 -3
- package/dist/durable/PreparedInputAdmission.d.mts +1 -1
- package/dist/durable/RecordFormat.mjs +2 -2
- package/dist/durable/RecordFormat.mjs.map +1 -1
- package/dist/durable/Recovery.d.mts +1 -1
- package/dist/durable/RunContinuation.d.mts +2 -2
- package/dist/durable/RunContinuation.mjs +2 -2
- package/dist/durable/RunJournal.d.mts +1 -1
- package/dist/durable/RunJournal.mjs +1 -1
- package/dist/durable/RunStorage.d.mts +1 -1
- package/dist/durable/RunStorage.mjs +1 -1
- package/dist/durable/Scheduling.d.mts +1 -1
- package/dist/durable/Scheduling.mjs +1 -1
- package/dist/durable/SettlementPublisher.d.mts +1 -1
- package/dist/durable/SettlementPublisher.mjs +2 -2
- package/dist/durable/SubmissionLedger.d.mts +1 -1
- package/dist/durable/SubmissionLedger.mjs +5 -1
- package/dist/durable/SubmissionLedger.mjs.map +1 -1
- package/dist/durable/SubmissionLedgerConformance.d.mts +1 -1
- package/dist/durable/SubmissionLedgerConformance.mjs +4 -4
- package/dist/durable/SubmissionStatus.d.mts +1 -1
- package/dist/durable/Subscription.d.mts +1 -1
- package/dist/durable/SubscriptionInput.d.mts +1 -1
- package/dist/durable/SubscriptionInput.mjs +3 -3
- package/dist/durable/SubscriptionInput.mjs.map +1 -1
- package/dist/durable/SubscriptionStoreConformance.d.mts +1 -1
- package/dist/durable/SubscriptionStoreConformance.mjs +2 -2
- package/dist/durable/SubscriptionStoreConformance.mjs.map +1 -1
- package/dist/durable/SubscriptionTools.d.mts +2 -2
- package/dist/durable/SubscriptionTransition.d.mts +1 -1
- package/dist/durable/Subscriptions.d.mts +5 -5
- package/dist/durable/Subscriptions.mjs +2 -2
- package/dist/durable/Subscriptions.mjs.map +1 -1
- package/dist/durable/ThreadArchiveRange.d.mts +1 -1
- package/dist/durable/ThreadContextHistory.d.mts +1 -1
- package/dist/durable/ThreadContextHistory.mjs +1 -1
- package/dist/durable/ThreadImport.d.mts +1 -1
- package/dist/durable/ThreadImport.mjs +1 -1
- package/dist/durable/ThreadInvariants.d.mts +3 -3
- package/dist/durable/ThreadInvariants.mjs +4 -4
- package/dist/durable/ThreadInvariants.mjs.map +1 -1
- package/dist/durable/ThreadProjectionMaintenance.d.mts +1 -1
- package/dist/durable/ThreadReader.d.mts +1 -1
- package/dist/durable/ThreadReader.mjs +1 -1
- package/dist/durable/ThreadStore.d.mts +1 -1
- package/dist/durable/ThreadStore.mjs +1 -1
- package/dist/durable/ThreadStoreConformance.d.mts +1 -1
- package/dist/durable/ThreadStoreConformance.mjs +3 -3
- package/dist/durable/ThreadTransfer.d.mts +1 -1
- package/dist/durable/ThreadTransfer.mjs +1 -1
- package/dist/durable/ThreadWork.d.mts +1 -1
- package/dist/durable/ThreadWork.mjs +4 -4
- package/dist/durable/ThreadWork.mjs.map +1 -1
- package/dist/durable/WorkerAdmission.d.mts +1 -1
- package/dist/durable/WorkerAdmission.mjs +1 -1
- package/dist/durable/WorkerHost.d.mts +2 -2
- package/dist/engine/AgentRuntime.d.mts +1 -1
- package/dist/engine/AgentRuntime.mjs +1 -1
- package/dist/engine/MessagingHost.d.mts +1 -1
- package/dist/engine/Output.d.mts +1 -1
- package/dist/engine/RunOptions.d.mts +1 -1
- package/dist/engine/SubagentHost.d.mts +1 -1
- package/dist/index.d.mts +19 -19
- package/dist/index.mjs +14 -14
- package/dist/{run-context-reader-OcsZr4bk.d.mts → run-context-reader-CC7ZD95F.d.mts} +2 -2
- package/package.json +1 -1
- package/src/capabilities/Approval.ts +58 -60
- package/src/capabilities/BrowserUse.ts +89 -944
- package/src/capabilities/Budget.ts +8 -1
- package/src/capabilities/CodeMode.ts +57 -33
- package/src/capabilities/McpClient.ts +2 -2
- package/src/capabilities/Messaging.ts +51 -55
- package/src/capabilities/ModelContext.ts +5 -2
- package/src/capabilities/Redaction.ts +64 -34
- package/src/capabilities/RunHooks.ts +52 -55
- package/src/capabilities/Subagent.ts +33 -33
- package/src/capabilities/SubagentReservations.ts +9 -1
- package/src/capabilities/WebCapture.ts +6 -0
- package/src/capabilities/WebSearch.ts +10 -7
- package/src/capabilities/internal/subagent-background.ts +202 -158
- package/src/core/Agent.ts +11 -6
- package/src/core/AgentUpdates.ts +8 -8
- package/src/core/Thread.ts +9 -10
- package/src/core/Usage.ts +7 -8
- package/src/core/internal/agent-updates.ts +1 -0
- package/src/durable/DurableAgentRuntime.ts +645 -625
- package/src/durable/EventSource.ts +15 -9
- package/src/durable/RecordFormat.ts +5 -3
- package/src/durable/RunContinuation.ts +233 -81
- package/src/durable/RunJournal.ts +10 -9
- package/src/durable/RunStorage.ts +122 -124
- package/src/durable/Scheduling.ts +639 -651
- package/src/durable/SubmissionLedger.ts +4 -0
- package/src/durable/SubscriptionInput.ts +5 -3
- package/src/durable/SubscriptionStoreConformance.ts +23 -23
- package/src/durable/Subscriptions.ts +18 -19
- package/src/durable/ThreadImport.ts +18 -19
- package/src/durable/ThreadInvariants.ts +2 -1
- package/src/durable/ThreadStore.ts +215 -111
- package/src/durable/ThreadWork.ts +1 -2
- package/src/durable/internal/StorageOwner.ts +10 -4
- package/src/durable/internal/agent-registration.ts +96 -89
- package/src/durable/internal/canonical-json.ts +61 -20
- package/src/durable/internal/prompt-history-cache.ts +13 -4
- package/src/durable/internal/record-encoding.ts +120 -64
- package/src/durable/internal/schedule-time.ts +6 -6
- package/src/durable/internal/usage-totals.ts +38 -0
- package/src/durable/internal/worker-host.ts +69 -74
- package/src/engine/internal/agent-runtime.ts +1067 -1018
- package/src/engine/internal/provisional-text.ts +12 -10
- package/src/engine/internal/tool-exposure.ts +22 -18
- package/src/engine/internal/tool-telemetry.ts +12 -7
- package/dist/Agent-0WvBWWbM.mjs.map +0 -1
- package/dist/AgentRuntime-CQ-qcETl.mjs.map +0 -1
- package/dist/Digest-DCuPlhNk.mjs.map +0 -1
- package/dist/DurableAgentRuntime-BAmSkWYr.mjs.map +0 -1
- package/dist/RunContinuation-DHYNAQFi.mjs.map +0 -1
- package/dist/RunJournal-kfKVxJj7.mjs.map +0 -1
- package/dist/RunStorage-uqPxps09.mjs.map +0 -1
- package/dist/Scheduling-CVyfXlBR.mjs.map +0 -1
- package/dist/Subagent-vpffemql.mjs.map +0 -1
- package/dist/ThreadImport-DckUMEqJ.mjs.map +0 -1
- package/dist/ThreadStore-CEekRa80.mjs.map +0 -1
- package/dist/agent-registration-ByMZ_cK5.mjs.map +0 -1
|
@@ -197,766 +197,754 @@ const resolveInput = Effect.fnUntraced(function* <InputSchema extends Schema.Top
|
|
|
197
197
|
return { payload, digest };
|
|
198
198
|
});
|
|
199
199
|
|
|
200
|
-
const makeManagement = (limits: SchedulingLimits)
|
|
201
|
-
|
|
202
|
-
const nowMillis = yield* currentMillis;
|
|
200
|
+
const makeManagement = Effect.fnUntraced(function* (limits: SchedulingLimits) {
|
|
201
|
+
const nowMillis = yield* currentMillis;
|
|
203
202
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
203
|
+
yield* Schema.decodeEffect(ScheduleInstant)(nowMillis + limits.recoveryPollMillis).pipe(
|
|
204
|
+
Effect.mapError(() =>
|
|
205
|
+
ScheduleValidationError.make({
|
|
206
|
+
message: "Scheduling recovery deadline exceeds the supported instant range",
|
|
207
|
+
}),
|
|
208
|
+
),
|
|
209
|
+
);
|
|
210
|
+
const store = yield* ScheduleStore;
|
|
211
|
+
const authorizer = yield* ScheduleAuthorizer;
|
|
212
|
+
const wake = yield* ScheduleWake;
|
|
213
|
+
const crypto = yield* Crypto.Crypto;
|
|
214
|
+
|
|
215
|
+
const withCrypto = <A, E>(effect: Effect.Effect<A, E, Crypto.Crypto>) =>
|
|
216
|
+
Effect.provideService(effect, Crypto.Crypto, crypto);
|
|
217
|
+
|
|
218
|
+
const fingerprint = Effect.fnUntraced(function* (
|
|
219
|
+
agentId: AgentId,
|
|
220
|
+
inputDigest: Digest,
|
|
221
|
+
options: ScheduleCreateOptions,
|
|
222
|
+
) {
|
|
223
|
+
const timing = yield* Schema.encodeEffect(ScheduleTimingRequestSchema)(options.timing).pipe(
|
|
224
|
+
Effect.mapError(() => ScheduleValidationError.make({ message: "Invalid timing request" })),
|
|
210
225
|
);
|
|
211
|
-
const store = yield* ScheduleStore;
|
|
212
|
-
const authorizer = yield* ScheduleAuthorizer;
|
|
213
|
-
const wake = yield* ScheduleWake;
|
|
214
|
-
const crypto = yield* Crypto.Crypto;
|
|
215
226
|
|
|
216
|
-
const
|
|
217
|
-
|
|
227
|
+
const destination = yield* Schema.encodeEffect(ScheduleDestinationSchema)(
|
|
228
|
+
options.destination,
|
|
229
|
+
).pipe(Effect.mapError(() => ScheduleValidationError.make({ message: "Invalid destination" })));
|
|
218
230
|
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
)
|
|
224
|
-
const timing = yield* Schema.encodeEffect(ScheduleTimingRequestSchema)(options.timing).pipe(
|
|
225
|
-
Effect.mapError(() => ScheduleValidationError.make({ message: "Invalid timing request" })),
|
|
226
|
-
);
|
|
227
|
-
|
|
228
|
-
const destination = yield* Schema.encodeEffect(ScheduleDestinationSchema)(
|
|
229
|
-
options.destination,
|
|
230
|
-
).pipe(
|
|
231
|
-
Effect.mapError(() => ScheduleValidationError.make({ message: "Invalid destination" })),
|
|
232
|
-
);
|
|
231
|
+
const definitions = yield* Schema.encodeEffect(DefinitionDigests)(options.definitions).pipe(
|
|
232
|
+
Effect.mapError(() =>
|
|
233
|
+
ScheduleValidationError.make({ message: "Invalid definition digests" }),
|
|
234
|
+
),
|
|
235
|
+
);
|
|
233
236
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
237
|
+
return yield* withCrypto(
|
|
238
|
+
digestJson({
|
|
239
|
+
schemaVersion: 1,
|
|
240
|
+
owner: options.scope.owner,
|
|
241
|
+
scheduleId: options.scheduleId,
|
|
242
|
+
registeringPrincipal: options.scope.principal,
|
|
243
|
+
deliveryPrincipal: options.deliveryPrincipal,
|
|
244
|
+
...(options.admissionGroup === undefined ? {} : { admissionGroup: options.admissionGroup }),
|
|
245
|
+
...(options.admissionFence === undefined ? {} : { admissionFence: options.admissionFence }),
|
|
246
|
+
agentId,
|
|
247
|
+
definitions,
|
|
248
|
+
destination,
|
|
249
|
+
timing,
|
|
250
|
+
inputDigest,
|
|
251
|
+
}),
|
|
252
|
+
).pipe(
|
|
253
|
+
Effect.mapError(() =>
|
|
254
|
+
ScheduleValidationError.make({ message: "Unable to digest Schedule registration" }),
|
|
255
|
+
),
|
|
256
|
+
);
|
|
257
|
+
});
|
|
239
258
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
inputDigest,
|
|
258
|
-
}),
|
|
259
|
-
).pipe(
|
|
260
|
-
Effect.mapError(() =>
|
|
261
|
-
ScheduleValidationError.make({ message: "Unable to digest Schedule registration" }),
|
|
262
|
-
),
|
|
263
|
-
);
|
|
264
|
-
});
|
|
259
|
+
const configuration = (
|
|
260
|
+
agentId: AgentId,
|
|
261
|
+
payload: PersistedJson,
|
|
262
|
+
inputDigest: Digest,
|
|
263
|
+
options: ScheduleCreateOptions,
|
|
264
|
+
timing: ScheduleConfiguration["timing"],
|
|
265
|
+
): ScheduleConfiguration => ({
|
|
266
|
+
timing,
|
|
267
|
+
destination: options.destination,
|
|
268
|
+
deliveryPrincipal: options.deliveryPrincipal,
|
|
269
|
+
...(options.admissionGroup === undefined ? {} : { admissionGroup: options.admissionGroup }),
|
|
270
|
+
...(options.admissionFence === undefined ? {} : { admissionFence: options.admissionFence }),
|
|
271
|
+
agentId,
|
|
272
|
+
definitions: options.definitions,
|
|
273
|
+
input: payload,
|
|
274
|
+
inputDigest,
|
|
275
|
+
});
|
|
265
276
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
...(options.admissionGroup === undefined ? {} : { admissionGroup: options.admissionGroup }),
|
|
277
|
-
...(options.admissionFence === undefined ? {} : { admissionFence: options.admissionFence }),
|
|
278
|
-
agentId,
|
|
279
|
-
definitions: options.definitions,
|
|
280
|
-
input: payload,
|
|
281
|
-
inputDigest,
|
|
282
|
-
});
|
|
277
|
+
const create = Effect.fnUntraced(function* <InputSchema extends Schema.Top>(
|
|
278
|
+
agent: DurableSubmitAgent<InputSchema>,
|
|
279
|
+
input: InputSchema["Type"],
|
|
280
|
+
options: ScheduleCreateOptions,
|
|
281
|
+
): Effect.fn.Return<
|
|
282
|
+
ScheduleSnapshot,
|
|
283
|
+
ScheduleManagementFailure,
|
|
284
|
+
InputSchema["EncodingServices"]
|
|
285
|
+
> {
|
|
286
|
+
const nowMillis = yield* currentMillis;
|
|
283
287
|
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
options: ScheduleCreateOptions,
|
|
288
|
-
): Effect.fn.Return<
|
|
289
|
-
ScheduleSnapshot,
|
|
290
|
-
ScheduleManagementFailure,
|
|
291
|
-
InputSchema["EncodingServices"]
|
|
292
|
-
> {
|
|
293
|
-
const nowMillis = yield* currentMillis;
|
|
288
|
+
const resolvedInput = yield* resolveInput(agent, input, limits.maxInputBytes).pipe(
|
|
289
|
+
Effect.provideService(Crypto.Crypto, crypto),
|
|
290
|
+
);
|
|
294
291
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
292
|
+
const creationFingerprint = yield* fingerprint(
|
|
293
|
+
agent.definition.id,
|
|
294
|
+
resolvedInput.digest,
|
|
295
|
+
options,
|
|
296
|
+
);
|
|
298
297
|
|
|
299
|
-
|
|
300
|
-
agent.definition.id,
|
|
301
|
-
resolvedInput.digest,
|
|
302
|
-
options,
|
|
303
|
-
);
|
|
298
|
+
const key = keyOf(options.scope, options.scheduleId);
|
|
304
299
|
|
|
305
|
-
|
|
300
|
+
yield* authorizer.manage({
|
|
301
|
+
operation: "create",
|
|
302
|
+
scope: options.scope,
|
|
303
|
+
scheduleId: options.scheduleId,
|
|
304
|
+
});
|
|
305
|
+
const existing = yield* store.get(key);
|
|
306
306
|
|
|
307
|
+
if (existing !== null) {
|
|
308
|
+
if (existing.creationFingerprint !== creationFingerprint) {
|
|
309
|
+
return yield* ScheduleConflict.make({ reason: "creation", key });
|
|
310
|
+
}
|
|
307
311
|
yield* authorizer.manage({
|
|
308
312
|
operation: "create",
|
|
309
313
|
scope: options.scope,
|
|
310
314
|
scheduleId: options.scheduleId,
|
|
315
|
+
configuration: existing.configuration,
|
|
311
316
|
});
|
|
312
|
-
const existing = yield* store.get(key);
|
|
313
|
-
|
|
314
|
-
if (existing !== null) {
|
|
315
|
-
if (existing.creationFingerprint !== creationFingerprint) {
|
|
316
|
-
return yield* ScheduleConflict.make({ reason: "creation", key });
|
|
317
|
-
}
|
|
318
|
-
yield* authorizer.manage({
|
|
319
|
-
operation: "create",
|
|
320
|
-
scope: options.scope,
|
|
321
|
-
scheduleId: options.scheduleId,
|
|
322
|
-
configuration: existing.configuration,
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
return asSnapshot(existing, nowMillis);
|
|
326
|
-
}
|
|
327
317
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
);
|
|
318
|
+
return asSnapshot(existing, nowMillis);
|
|
319
|
+
}
|
|
331
320
|
|
|
332
|
-
|
|
321
|
+
const timing = yield* Effect.fromResult(
|
|
322
|
+
normalizeScheduleTiming(options.timing, nowMillis, limits.minIntervalMillis),
|
|
323
|
+
);
|
|
333
324
|
|
|
334
|
-
|
|
335
|
-
return yield* ScheduleValidationError.make({ message: "Timing has no next occurrence" });
|
|
336
|
-
}
|
|
325
|
+
const nextAtMillis = yield* Effect.fromResult(scheduleInitialCursor(timing, nowMillis));
|
|
337
326
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
resolvedInput.digest,
|
|
342
|
-
options,
|
|
343
|
-
timing,
|
|
344
|
-
);
|
|
327
|
+
if (nextAtMillis === null) {
|
|
328
|
+
return yield* ScheduleValidationError.make({ message: "Timing has no next occurrence" });
|
|
329
|
+
}
|
|
345
330
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
331
|
+
const nextConfiguration = configuration(
|
|
332
|
+
agent.definition.id,
|
|
333
|
+
resolvedInput.payload,
|
|
334
|
+
resolvedInput.digest,
|
|
335
|
+
options,
|
|
336
|
+
timing,
|
|
337
|
+
);
|
|
352
338
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
createdAtMillis: nowMillis,
|
|
360
|
-
updatedAtMillis: nowMillis,
|
|
361
|
-
configurationRevision: 1,
|
|
362
|
-
version: 1,
|
|
363
|
-
configuration: nextConfiguration,
|
|
364
|
-
state: "active",
|
|
365
|
-
nextAtMillis,
|
|
366
|
-
pending: null,
|
|
367
|
-
lastReceipt: null,
|
|
368
|
-
lastRefusal: null,
|
|
369
|
-
lastSkippedRange: null,
|
|
370
|
-
};
|
|
339
|
+
yield* authorizer.manage({
|
|
340
|
+
operation: "create",
|
|
341
|
+
scope: options.scope,
|
|
342
|
+
scheduleId: options.scheduleId,
|
|
343
|
+
configuration: nextConfiguration,
|
|
344
|
+
});
|
|
371
345
|
|
|
372
|
-
|
|
346
|
+
const record: ScheduleRecord = {
|
|
347
|
+
schemaVersion: 1,
|
|
348
|
+
owner: options.scope.owner,
|
|
349
|
+
scheduleId: options.scheduleId,
|
|
350
|
+
creationFingerprint,
|
|
351
|
+
createdBy: options.scope.principal,
|
|
352
|
+
createdAtMillis: nowMillis,
|
|
353
|
+
updatedAtMillis: nowMillis,
|
|
354
|
+
configurationRevision: 1,
|
|
355
|
+
version: 1,
|
|
356
|
+
configuration: nextConfiguration,
|
|
357
|
+
state: "active",
|
|
358
|
+
nextAtMillis,
|
|
359
|
+
pending: null,
|
|
360
|
+
lastReceipt: null,
|
|
361
|
+
lastRefusal: null,
|
|
362
|
+
lastSkippedRange: null,
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const inserted = yield* store.insert(record, limits.maxSchedulesPerOwner);
|
|
366
|
+
|
|
367
|
+
yield* wake.notify;
|
|
368
|
+
|
|
369
|
+
return asSnapshot(inserted, nowMillis);
|
|
370
|
+
});
|
|
373
371
|
|
|
374
|
-
|
|
372
|
+
const update = Effect.fnUntraced(function* <InputSchema extends Schema.Top>(
|
|
373
|
+
agent: DurableSubmitAgent<InputSchema>,
|
|
374
|
+
input: InputSchema["Type"],
|
|
375
|
+
options: ScheduleUpdateOptions,
|
|
376
|
+
): Effect.fn.Return<
|
|
377
|
+
ScheduleSnapshot,
|
|
378
|
+
ScheduleManagementFailure,
|
|
379
|
+
InputSchema["EncodingServices"]
|
|
380
|
+
> {
|
|
381
|
+
const nowMillis = yield* currentMillis;
|
|
382
|
+
const key = keyOf(options.scope, options.scheduleId);
|
|
375
383
|
|
|
376
|
-
|
|
384
|
+
yield* authorizer.manage({
|
|
385
|
+
operation: "update",
|
|
386
|
+
scope: options.scope,
|
|
387
|
+
scheduleId: options.scheduleId,
|
|
377
388
|
});
|
|
389
|
+
yield* validateRequestedFields(options);
|
|
390
|
+
const existing = yield* store.get(key);
|
|
378
391
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
): Effect.fn.Return<
|
|
384
|
-
ScheduleSnapshot,
|
|
385
|
-
ScheduleManagementFailure,
|
|
386
|
-
InputSchema["EncodingServices"]
|
|
387
|
-
> {
|
|
388
|
-
const nowMillis = yield* currentMillis;
|
|
389
|
-
const key = keyOf(options.scope, options.scheduleId);
|
|
390
|
-
|
|
391
|
-
yield* authorizer.manage({
|
|
392
|
-
operation: "update",
|
|
393
|
-
scope: options.scope,
|
|
394
|
-
scheduleId: options.scheduleId,
|
|
395
|
-
});
|
|
396
|
-
yield* validateRequestedFields(options);
|
|
397
|
-
const existing = yield* store.get(key);
|
|
392
|
+
if (existing === null) return yield* notFound(key);
|
|
393
|
+
if (existing.configurationRevision !== options.expectedRevision) {
|
|
394
|
+
return yield* ScheduleConflict.make({ reason: "revision", key });
|
|
395
|
+
}
|
|
398
396
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
}
|
|
397
|
+
const resolvedInput = yield* resolveInput(agent, input, limits.maxInputBytes).pipe(
|
|
398
|
+
Effect.provideService(Crypto.Crypto, crypto),
|
|
399
|
+
);
|
|
403
400
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
401
|
+
const timingRequest =
|
|
402
|
+
options.timing._tag === "Interval" &&
|
|
403
|
+
options.timing.anchorMillis === undefined &&
|
|
404
|
+
existing.configuration.timing._tag === "Interval" &&
|
|
405
|
+
existing.configuration.timing.everyMillis === options.timing.everyMillis
|
|
406
|
+
? { ...options.timing, anchorMillis: existing.configuration.timing.anchorMillis }
|
|
407
|
+
: options.timing;
|
|
407
408
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
existing.configuration.timing._tag === "Interval" &&
|
|
412
|
-
existing.configuration.timing.everyMillis === options.timing.everyMillis
|
|
413
|
-
? { ...options.timing, anchorMillis: existing.configuration.timing.anchorMillis }
|
|
414
|
-
: options.timing;
|
|
409
|
+
const timing = yield* Effect.fromResult(
|
|
410
|
+
normalizeScheduleTiming(timingRequest, nowMillis, limits.minIntervalMillis),
|
|
411
|
+
);
|
|
415
412
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
413
|
+
const nextAtMillis =
|
|
414
|
+
timing._tag === "At"
|
|
415
|
+
? timing.atMillis
|
|
416
|
+
: yield* Effect.fromResult(scheduleNextAfter(timing, nowMillis));
|
|
419
417
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
: yield* Effect.fromResult(scheduleNextAfter(timing, nowMillis));
|
|
418
|
+
if (nextAtMillis === null) {
|
|
419
|
+
return yield* ScheduleValidationError.make({ message: "Timing has no next occurrence" });
|
|
420
|
+
}
|
|
424
421
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
422
|
+
const nextConfiguration = configuration(
|
|
423
|
+
agent.definition.id,
|
|
424
|
+
resolvedInput.payload,
|
|
425
|
+
resolvedInput.digest,
|
|
426
|
+
options,
|
|
427
|
+
timing,
|
|
428
|
+
);
|
|
428
429
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
);
|
|
430
|
+
yield* authorizer.manage({
|
|
431
|
+
operation: "update",
|
|
432
|
+
scope: options.scope,
|
|
433
|
+
scheduleId: options.scheduleId,
|
|
434
|
+
configuration: nextConfiguration,
|
|
435
|
+
});
|
|
436
436
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
437
|
+
const changed = yield* store.change(
|
|
438
|
+
key,
|
|
439
|
+
{
|
|
440
|
+
_tag: "Update",
|
|
441
|
+
expectedRevision: options.expectedRevision,
|
|
441
442
|
configuration: nextConfiguration,
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
_tag: "Update",
|
|
448
|
-
expectedRevision: options.expectedRevision,
|
|
449
|
-
configuration: nextConfiguration,
|
|
450
|
-
nextAtMillis,
|
|
451
|
-
nowMillis,
|
|
452
|
-
},
|
|
453
|
-
limits.maxSchedulesPerOwner,
|
|
454
|
-
);
|
|
443
|
+
nextAtMillis,
|
|
444
|
+
nowMillis,
|
|
445
|
+
},
|
|
446
|
+
limits.maxSchedulesPerOwner,
|
|
447
|
+
);
|
|
455
448
|
|
|
456
|
-
|
|
449
|
+
yield* wake.notify;
|
|
457
450
|
|
|
458
|
-
|
|
459
|
-
|
|
451
|
+
return asSnapshot(changed, nowMillis);
|
|
452
|
+
});
|
|
460
453
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
454
|
+
const get = Effect.fnUntraced(function* (scope: ScheduleScope, scheduleId: ScheduleId) {
|
|
455
|
+
yield* authorizer.manage({ operation: "get", scope, scheduleId });
|
|
456
|
+
const key = keyOf(scope, scheduleId);
|
|
457
|
+
const record = yield* store.get(key);
|
|
465
458
|
|
|
466
|
-
|
|
459
|
+
if (record === null) return yield* notFound(key);
|
|
467
460
|
|
|
468
|
-
|
|
469
|
-
|
|
461
|
+
return asSnapshot(record, yield* currentMillis);
|
|
462
|
+
});
|
|
470
463
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
464
|
+
const list = Effect.fnUntraced(function* (
|
|
465
|
+
scope: ScheduleScope,
|
|
466
|
+
options: ScheduleListOptions = {},
|
|
467
|
+
) {
|
|
468
|
+
yield* authorizer.manage({ operation: "list", scope });
|
|
469
|
+
const limit = options.limit ?? 50;
|
|
477
470
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
const page = yield* store.list({
|
|
485
|
-
owner: scope.owner,
|
|
486
|
-
...(options.after === undefined ? {} : { after: options.after }),
|
|
487
|
-
limit,
|
|
471
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 100) {
|
|
472
|
+
return yield* ScheduleValidationError.make({
|
|
473
|
+
message: "Schedule page limit must be an integer from 1 through 100",
|
|
488
474
|
});
|
|
475
|
+
}
|
|
489
476
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
477
|
+
const page = yield* store.list({
|
|
478
|
+
owner: scope.owner,
|
|
479
|
+
...(options.after === undefined ? {} : { after: options.after }),
|
|
480
|
+
limit,
|
|
493
481
|
});
|
|
494
482
|
|
|
495
|
-
const
|
|
496
|
-
operation: "pause" | "resume" | "cancel",
|
|
497
|
-
scope: ScheduleScope,
|
|
498
|
-
scheduleId: ScheduleId,
|
|
499
|
-
expectedRevision: number,
|
|
500
|
-
): Effect.fn.Return<ScheduleSnapshot, ScheduleManagementFailure> {
|
|
501
|
-
yield* authorizer.manage({ operation, scope, scheduleId });
|
|
502
|
-
const key = keyOf(scope, scheduleId);
|
|
503
|
-
|
|
504
|
-
const attempt = (): Effect.Effect<ScheduleRecord, ScheduleManagementFailure> =>
|
|
505
|
-
Effect.suspend(() =>
|
|
506
|
-
Effect.gen(function* () {
|
|
507
|
-
const record = yield* store.get(key);
|
|
508
|
-
|
|
509
|
-
if (record === null) return yield* notFound(key);
|
|
510
|
-
if (record.configurationRevision !== expectedRevision) {
|
|
511
|
-
return yield* ScheduleConflict.make({ reason: "revision", key });
|
|
512
|
-
}
|
|
513
|
-
if (operation === "resume" && record.state === "active") return record;
|
|
514
|
-
const nowMillis = yield* currentMillis;
|
|
515
|
-
|
|
516
|
-
const nextAtMillis =
|
|
517
|
-
operation === "resume"
|
|
518
|
-
? yield* Effect.fromResult(
|
|
519
|
-
scheduleResumeCursor(
|
|
520
|
-
record.configuration.timing,
|
|
521
|
-
record.nextAtMillis,
|
|
522
|
-
nowMillis,
|
|
523
|
-
),
|
|
524
|
-
)
|
|
525
|
-
: operation === "cancel"
|
|
526
|
-
? null
|
|
527
|
-
: record.nextAtMillis;
|
|
528
|
-
|
|
529
|
-
const skippedRange =
|
|
530
|
-
operation === "resume" &&
|
|
531
|
-
record.configuration.timing._tag !== "At" &&
|
|
532
|
-
record.nextAtMillis !== null &&
|
|
533
|
-
nextAtMillis !== null &&
|
|
534
|
-
nextAtMillis > record.nextAtMillis
|
|
535
|
-
? { fromMillis: record.nextAtMillis, toMillis: nextAtMillis }
|
|
536
|
-
: null;
|
|
537
|
-
|
|
538
|
-
return yield* store
|
|
539
|
-
.change(
|
|
540
|
-
key,
|
|
541
|
-
{
|
|
542
|
-
_tag: "Control",
|
|
543
|
-
expectedRevision,
|
|
544
|
-
expectedVersion: record.version,
|
|
545
|
-
action: operation,
|
|
546
|
-
nextAtMillis,
|
|
547
|
-
nowMillis,
|
|
548
|
-
skippedRange,
|
|
549
|
-
},
|
|
550
|
-
limits.maxSchedulesPerOwner,
|
|
551
|
-
)
|
|
552
|
-
.pipe(
|
|
553
|
-
Effect.catchTag("ScheduleConflict", (error) =>
|
|
554
|
-
error.reason === "revision" ? attempt() : Effect.fail(error),
|
|
555
|
-
),
|
|
556
|
-
);
|
|
557
|
-
}),
|
|
558
|
-
);
|
|
483
|
+
const nowMillis = yield* currentMillis;
|
|
559
484
|
|
|
560
|
-
|
|
485
|
+
return { items: page.items.map((record) => asSnapshot(record, nowMillis)), next: page.next };
|
|
486
|
+
});
|
|
561
487
|
|
|
562
|
-
|
|
488
|
+
const control = Effect.fnUntraced(function* (
|
|
489
|
+
operation: "pause" | "resume" | "cancel",
|
|
490
|
+
scope: ScheduleScope,
|
|
491
|
+
scheduleId: ScheduleId,
|
|
492
|
+
expectedRevision: number,
|
|
493
|
+
): Effect.fn.Return<ScheduleSnapshot, ScheduleManagementFailure> {
|
|
494
|
+
yield* authorizer.manage({ operation, scope, scheduleId });
|
|
495
|
+
const key = keyOf(scope, scheduleId);
|
|
496
|
+
|
|
497
|
+
const attempt = (): Effect.Effect<ScheduleRecord, ScheduleManagementFailure> =>
|
|
498
|
+
Effect.suspend(() =>
|
|
499
|
+
Effect.gen(function* () {
|
|
500
|
+
const record = yield* store.get(key);
|
|
501
|
+
|
|
502
|
+
if (record === null) return yield* notFound(key);
|
|
503
|
+
if (record.configurationRevision !== expectedRevision) {
|
|
504
|
+
return yield* ScheduleConflict.make({ reason: "revision", key });
|
|
505
|
+
}
|
|
506
|
+
if (operation === "resume" && record.state === "active") return record;
|
|
507
|
+
const nowMillis = yield* currentMillis;
|
|
508
|
+
|
|
509
|
+
const nextAtMillis =
|
|
510
|
+
operation === "resume"
|
|
511
|
+
? yield* Effect.fromResult(
|
|
512
|
+
scheduleResumeCursor(record.configuration.timing, record.nextAtMillis, nowMillis),
|
|
513
|
+
)
|
|
514
|
+
: operation === "cancel"
|
|
515
|
+
? null
|
|
516
|
+
: record.nextAtMillis;
|
|
517
|
+
|
|
518
|
+
const skippedRange =
|
|
519
|
+
operation === "resume" &&
|
|
520
|
+
record.configuration.timing._tag !== "At" &&
|
|
521
|
+
record.nextAtMillis !== null &&
|
|
522
|
+
nextAtMillis !== null &&
|
|
523
|
+
nextAtMillis > record.nextAtMillis
|
|
524
|
+
? { fromMillis: record.nextAtMillis, toMillis: nextAtMillis }
|
|
525
|
+
: null;
|
|
526
|
+
|
|
527
|
+
return yield* store
|
|
528
|
+
.change(
|
|
529
|
+
key,
|
|
530
|
+
{
|
|
531
|
+
_tag: "Control",
|
|
532
|
+
expectedRevision,
|
|
533
|
+
expectedVersion: record.version,
|
|
534
|
+
action: operation,
|
|
535
|
+
nextAtMillis,
|
|
536
|
+
nowMillis,
|
|
537
|
+
skippedRange,
|
|
538
|
+
},
|
|
539
|
+
limits.maxSchedulesPerOwner,
|
|
540
|
+
)
|
|
541
|
+
.pipe(
|
|
542
|
+
Effect.catchTag("ScheduleConflict", (error) =>
|
|
543
|
+
error.reason === "revision" ? attempt() : Effect.fail(error),
|
|
544
|
+
),
|
|
545
|
+
);
|
|
546
|
+
}),
|
|
547
|
+
);
|
|
563
548
|
|
|
564
|
-
|
|
565
|
-
});
|
|
549
|
+
const changed = yield* attempt();
|
|
566
550
|
|
|
567
|
-
|
|
568
|
-
scope: ScheduleScope,
|
|
569
|
-
scheduleId: ScheduleId,
|
|
570
|
-
expectedRevision: number,
|
|
571
|
-
expectedGeneration: number,
|
|
572
|
-
) {
|
|
573
|
-
yield* authorizer.manage({ operation: "recover", scope, scheduleId });
|
|
574
|
-
const key = keyOf(scope, scheduleId);
|
|
575
|
-
const record = yield* store.get(key);
|
|
576
|
-
|
|
577
|
-
if (record === null) return yield* notFound(key);
|
|
578
|
-
if (record.configurationRevision !== expectedRevision)
|
|
579
|
-
return yield* ScheduleConflict.make({ reason: "revision", key });
|
|
580
|
-
const time = yield* currentMillis;
|
|
581
|
-
|
|
582
|
-
if (record.pending === null) return asSnapshot(record, time);
|
|
583
|
-
|
|
584
|
-
const recovered = yield* store.change(key, {
|
|
585
|
-
_tag: "Recover",
|
|
586
|
-
expectedGeneration,
|
|
587
|
-
expectedRevision,
|
|
588
|
-
occurrenceId: record.pending.envelope.occurrenceId,
|
|
589
|
-
nowMillis: time,
|
|
590
|
-
});
|
|
551
|
+
yield* wake.notify;
|
|
591
552
|
|
|
592
|
-
|
|
553
|
+
return asSnapshot(changed, yield* currentMillis);
|
|
554
|
+
});
|
|
593
555
|
|
|
594
|
-
|
|
556
|
+
const recover = Effect.fn("Scheduling.recover")(function* (
|
|
557
|
+
scope: ScheduleScope,
|
|
558
|
+
scheduleId: ScheduleId,
|
|
559
|
+
expectedRevision: number,
|
|
560
|
+
expectedGeneration: number,
|
|
561
|
+
) {
|
|
562
|
+
yield* authorizer.manage({ operation: "recover", scope, scheduleId });
|
|
563
|
+
const key = keyOf(scope, scheduleId);
|
|
564
|
+
const record = yield* store.get(key);
|
|
565
|
+
|
|
566
|
+
if (record === null) return yield* notFound(key);
|
|
567
|
+
if (record.configurationRevision !== expectedRevision)
|
|
568
|
+
return yield* ScheduleConflict.make({ reason: "revision", key });
|
|
569
|
+
const time = yield* currentMillis;
|
|
570
|
+
|
|
571
|
+
if (record.pending === null) return asSnapshot(record, time);
|
|
572
|
+
|
|
573
|
+
const recovered = yield* store.change(key, {
|
|
574
|
+
_tag: "Recover",
|
|
575
|
+
expectedGeneration,
|
|
576
|
+
expectedRevision,
|
|
577
|
+
occurrenceId: record.pending.envelope.occurrenceId,
|
|
578
|
+
nowMillis: time,
|
|
595
579
|
});
|
|
596
580
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
update,
|
|
601
|
-
get,
|
|
602
|
-
list,
|
|
603
|
-
pause: (scope, id, revision) => control("pause", scope, id, revision),
|
|
604
|
-
resume: (scope, id, revision) => control("resume", scope, id, revision),
|
|
605
|
-
cancel: (scope, id, revision) => control("cancel", scope, id, revision),
|
|
606
|
-
});
|
|
581
|
+
yield* wake.notify;
|
|
582
|
+
|
|
583
|
+
return asSnapshot(recovered, time);
|
|
607
584
|
});
|
|
608
585
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
const retry = Effect.fnUntraced(function* (
|
|
622
|
-
key: ScheduleKey,
|
|
623
|
-
record: ScheduleRecord,
|
|
624
|
-
reason: ScheduleRetryReason,
|
|
625
|
-
nowMillis: number,
|
|
626
|
-
) {
|
|
627
|
-
if (record.pending === null) return record;
|
|
628
|
-
const current = record.pending.retry;
|
|
586
|
+
return Scheduling.of({
|
|
587
|
+
recover,
|
|
588
|
+
create,
|
|
589
|
+
update,
|
|
590
|
+
get,
|
|
591
|
+
list,
|
|
592
|
+
pause: (scope, id, revision) => control("pause", scope, id, revision),
|
|
593
|
+
resume: (scope, id, revision) => control("resume", scope, id, revision),
|
|
594
|
+
cancel: (scope, id, revision) => control("cancel", scope, id, revision),
|
|
595
|
+
});
|
|
596
|
+
});
|
|
629
597
|
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
598
|
+
const makeDriver = Effect.fnUntraced(function* (limits: SchedulingLimits) {
|
|
599
|
+
const store = yield* ScheduleStore;
|
|
600
|
+
const authorizer = yield* ScheduleAuthorizer;
|
|
601
|
+
const admission = yield* ScheduledInputAdmission;
|
|
602
|
+
const failpoint = yield* ScheduleFailpoint;
|
|
603
|
+
const crypto = yield* Crypto.Crypto;
|
|
604
|
+
const admissionSemaphore = yield* Semaphore.make(limits.admissionConcurrency);
|
|
605
|
+
|
|
606
|
+
const withCrypto = <A, E>(effect: Effect.Effect<A, E, Crypto.Crypto>) =>
|
|
607
|
+
Effect.provideService(effect, Crypto.Crypto, crypto);
|
|
608
|
+
|
|
609
|
+
const retry = Effect.fnUntraced(function* (
|
|
610
|
+
key: ScheduleKey,
|
|
611
|
+
record: ScheduleRecord,
|
|
612
|
+
reason: ScheduleRetryReason,
|
|
613
|
+
nowMillis: number,
|
|
614
|
+
) {
|
|
615
|
+
if (record.pending === null) return record;
|
|
616
|
+
const current = record.pending.retry;
|
|
617
|
+
|
|
618
|
+
const delay = Math.min(
|
|
619
|
+
limits.retryBaseMillis * 2 ** Math.min(current.attempts, 52),
|
|
620
|
+
limits.retryMaxMillis,
|
|
621
|
+
);
|
|
634
622
|
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
});
|
|
623
|
+
return yield* store.change(key, {
|
|
624
|
+
_tag: "Retry",
|
|
625
|
+
occurrenceId: record.pending.envelope.occurrenceId,
|
|
626
|
+
retry: {
|
|
627
|
+
generation: current.generation,
|
|
628
|
+
attempts: current.attempts + 1,
|
|
629
|
+
automaticAttempts: current.automaticAttempts + 1,
|
|
630
|
+
parked: current.automaticAttempts + 1 >= (limits.maxAutomaticAttempts ?? 8),
|
|
631
|
+
nextAttemptAtMillis: Math.min(nowMillis + delay, 8_640_000_000_000_000),
|
|
632
|
+
lastAttemptAtMillis: nowMillis,
|
|
633
|
+
lastFailure: reason,
|
|
634
|
+
},
|
|
635
|
+
nowMillis,
|
|
649
636
|
});
|
|
637
|
+
});
|
|
650
638
|
|
|
651
|
-
|
|
652
|
-
|
|
639
|
+
const verifyPending = Effect.fnUntraced(function* (key: ScheduleKey, record: ScheduleRecord) {
|
|
640
|
+
const pending = record.pending;
|
|
653
641
|
|
|
654
|
-
|
|
655
|
-
|
|
642
|
+
if (pending === null) return;
|
|
643
|
+
const envelope = pending.envelope;
|
|
656
644
|
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
const usesCurrentConfiguration =
|
|
668
|
-
envelope.configurationRevision === record.configurationRevision;
|
|
645
|
+
const occurrenceId = yield* withCrypto(
|
|
646
|
+
digestJson({
|
|
647
|
+
schemaVersion: 1,
|
|
648
|
+
owner: envelope.owner,
|
|
649
|
+
scheduleId: envelope.scheduleId,
|
|
650
|
+
configurationRevision: envelope.configurationRevision,
|
|
651
|
+
intendedAtMillis: envelope.intendedAtMillis,
|
|
652
|
+
}),
|
|
653
|
+
).pipe(Effect.mapError(() => corrupt("process")));
|
|
669
654
|
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
? record.configuration.destination.threadId
|
|
673
|
-
: Schema.decodeSync(ThreadId)(`schedule-thread:${envelope.occurrenceId}`)
|
|
674
|
-
: null;
|
|
655
|
+
const usesCurrentConfiguration =
|
|
656
|
+
envelope.configurationRevision === record.configurationRevision;
|
|
675
657
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
658
|
+
const expectedThreadId = usesCurrentConfiguration
|
|
659
|
+
? record.configuration.destination._tag === "ExistingThread"
|
|
660
|
+
? record.configuration.destination.threadId
|
|
661
|
+
: Schema.decodeSync(ThreadId)(`schedule-thread:${envelope.occurrenceId}`)
|
|
662
|
+
: null;
|
|
679
663
|
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
664
|
+
const expectedAdmissionKey = Schema.decodeSync(IdempotencyKey)(
|
|
665
|
+
`schedule-occurrence:${envelope.occurrenceId}`,
|
|
666
|
+
);
|
|
683
667
|
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
envelope.owner.ownerId !== key.owner.ownerId ||
|
|
688
|
-
envelope.scheduleId !== key.scheduleId ||
|
|
689
|
-
envelope.configurationRevision > record.configurationRevision ||
|
|
690
|
-
envelope.occurrenceId !== occurrenceId ||
|
|
691
|
-
(usesCurrentConfiguration &&
|
|
692
|
-
(envelope.deliveryPrincipal !== record.configuration.deliveryPrincipal ||
|
|
693
|
-
envelope.agentId !== record.configuration.agentId ||
|
|
694
|
-
!equivalentDefinitions(envelope.definitions, record.configuration.definitions) ||
|
|
695
|
-
!equivalentInput(envelope.input, record.configuration.input) ||
|
|
696
|
-
envelope.inputDigest !== record.configuration.inputDigest)) ||
|
|
697
|
-
envelope.inputDigest !== digest ||
|
|
698
|
-
(expectedThreadId !== null && envelope.threadId !== expectedThreadId) ||
|
|
699
|
-
envelope.admissionKey !== expectedAdmissionKey
|
|
700
|
-
) {
|
|
701
|
-
return yield* corrupt("process");
|
|
702
|
-
}
|
|
703
|
-
});
|
|
668
|
+
const digest = yield* withCrypto(digestJson(envelope.input)).pipe(
|
|
669
|
+
Effect.mapError(() => corrupt("process")),
|
|
670
|
+
);
|
|
704
671
|
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
672
|
+
if (
|
|
673
|
+
envelope.schemaVersion !== 1 ||
|
|
674
|
+
envelope.owner.tenantId !== key.owner.tenantId ||
|
|
675
|
+
envelope.owner.ownerId !== key.owner.ownerId ||
|
|
676
|
+
envelope.scheduleId !== key.scheduleId ||
|
|
677
|
+
envelope.configurationRevision > record.configurationRevision ||
|
|
678
|
+
envelope.occurrenceId !== occurrenceId ||
|
|
679
|
+
(usesCurrentConfiguration &&
|
|
680
|
+
(envelope.deliveryPrincipal !== record.configuration.deliveryPrincipal ||
|
|
681
|
+
envelope.agentId !== record.configuration.agentId ||
|
|
682
|
+
!equivalentDefinitions(envelope.definitions, record.configuration.definitions) ||
|
|
683
|
+
!equivalentInput(envelope.input, record.configuration.input) ||
|
|
684
|
+
envelope.inputDigest !== record.configuration.inputDigest)) ||
|
|
685
|
+
envelope.inputDigest !== digest ||
|
|
686
|
+
(expectedThreadId !== null && envelope.threadId !== expectedThreadId) ||
|
|
687
|
+
envelope.admissionKey !== expectedAdmissionKey
|
|
709
688
|
) {
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
record.pending.retry.nextAttemptAtMillis > nowMillis
|
|
714
|
-
) {
|
|
715
|
-
return record;
|
|
716
|
-
}
|
|
717
|
-
yield* verifyPending(key, record);
|
|
718
|
-
const envelope = record.pending.envelope;
|
|
689
|
+
return yield* corrupt("process");
|
|
690
|
+
}
|
|
691
|
+
});
|
|
719
692
|
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
693
|
+
const deliverPending = Effect.fnUntraced(function* (
|
|
694
|
+
key: ScheduleKey,
|
|
695
|
+
record: ScheduleRecord,
|
|
696
|
+
nowMillis: number,
|
|
697
|
+
) {
|
|
698
|
+
if (
|
|
699
|
+
record.pending === null ||
|
|
700
|
+
record.pending.retry.parked === true ||
|
|
701
|
+
record.pending.retry.nextAttemptAtMillis > nowMillis
|
|
702
|
+
) {
|
|
703
|
+
return record;
|
|
704
|
+
}
|
|
705
|
+
yield* verifyPending(key, record);
|
|
706
|
+
const envelope = record.pending.envelope;
|
|
707
|
+
|
|
708
|
+
const outcome = yield* admissionSemaphore.withPermit(
|
|
709
|
+
admitPreparedInput(admission.submit(envelope), limits.admissionTimeoutMillis).pipe(
|
|
710
|
+
Effect.tap((outcome) =>
|
|
711
|
+
outcome._tag === "Receipt" ? failpoint.hit("schedule:admission:after") : Effect.void,
|
|
725
712
|
),
|
|
726
|
-
)
|
|
727
|
-
|
|
728
|
-
const completedAtMillis = yield* currentMillis;
|
|
713
|
+
),
|
|
714
|
+
);
|
|
729
715
|
|
|
730
|
-
|
|
731
|
-
if (outcome.receipt.threadId !== envelope.threadId) {
|
|
732
|
-
return yield* corrupt("admission");
|
|
733
|
-
}
|
|
716
|
+
const completedAtMillis = yield* currentMillis;
|
|
734
717
|
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
occurrenceId: envelope.occurrenceId,
|
|
739
|
-
receipt: outcome.receipt,
|
|
740
|
-
nowMillis: completedAtMillis,
|
|
741
|
-
})
|
|
742
|
-
.pipe(
|
|
743
|
-
Effect.catchTag("ScheduleStorageError", (error) =>
|
|
744
|
-
error.reason === "unavailable"
|
|
745
|
-
? retry(key, record, "storage", completedAtMillis)
|
|
746
|
-
: Effect.fail(error),
|
|
747
|
-
),
|
|
748
|
-
);
|
|
718
|
+
if (outcome._tag === "Receipt") {
|
|
719
|
+
if (outcome.receipt.threadId !== envelope.threadId) {
|
|
720
|
+
return yield* corrupt("admission");
|
|
749
721
|
}
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
722
|
+
|
|
723
|
+
return yield* store
|
|
724
|
+
.change(key, {
|
|
725
|
+
_tag: "Complete",
|
|
754
726
|
occurrenceId: envelope.occurrenceId,
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
}
|
|
727
|
+
receipt: outcome.receipt,
|
|
728
|
+
nowMillis: completedAtMillis,
|
|
729
|
+
})
|
|
730
|
+
.pipe(
|
|
731
|
+
Effect.catchTag("ScheduleStorageError", (error) =>
|
|
732
|
+
error.reason === "unavailable"
|
|
733
|
+
? retry(key, record, "storage", completedAtMillis)
|
|
734
|
+
: Effect.fail(error),
|
|
735
|
+
),
|
|
736
|
+
);
|
|
737
|
+
}
|
|
738
|
+
if (outcome._tag === "Refused") {
|
|
739
|
+
const refusal: ScheduleRefusal = {
|
|
740
|
+
atMillis: completedAtMillis,
|
|
741
|
+
intendedAtMillis: envelope.intendedAtMillis,
|
|
742
|
+
occurrenceId: envelope.occurrenceId,
|
|
743
|
+
phase: "admission",
|
|
744
|
+
code: outcome.error.code,
|
|
745
|
+
};
|
|
774
746
|
|
|
775
|
-
return yield*
|
|
776
|
-
|
|
747
|
+
return yield* store
|
|
748
|
+
.change(key, {
|
|
749
|
+
_tag: "Refuse",
|
|
750
|
+
occurrenceId: envelope.occurrenceId,
|
|
751
|
+
refusal,
|
|
752
|
+
nowMillis: completedAtMillis,
|
|
753
|
+
})
|
|
754
|
+
.pipe(
|
|
755
|
+
Effect.catchTag("ScheduleStorageError", (error) =>
|
|
756
|
+
error.reason === "unavailable"
|
|
757
|
+
? retry(key, record, "storage", completedAtMillis)
|
|
758
|
+
: Effect.fail(error),
|
|
759
|
+
),
|
|
760
|
+
);
|
|
761
|
+
}
|
|
777
762
|
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
initial: ScheduleRecord,
|
|
781
|
-
nowMillis: number,
|
|
782
|
-
): Effect.fn.Return<ScheduleRecord, ScheduleProcessFailure> {
|
|
783
|
-
if (initial.pending !== null) return yield* deliverPending(key, initial, nowMillis);
|
|
784
|
-
if (
|
|
785
|
-
initial.state !== "active" ||
|
|
786
|
-
initial.nextAtMillis === null ||
|
|
787
|
-
initial.nextAtMillis > nowMillis
|
|
788
|
-
) {
|
|
789
|
-
return initial;
|
|
790
|
-
}
|
|
763
|
+
return yield* retry(key, record, outcome.reason, completedAtMillis);
|
|
764
|
+
});
|
|
791
765
|
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
766
|
+
const processRecord = Effect.fnUntraced(function* (
|
|
767
|
+
key: ScheduleKey,
|
|
768
|
+
initial: ScheduleRecord,
|
|
769
|
+
nowMillis: number,
|
|
770
|
+
): Effect.fn.Return<ScheduleRecord, ScheduleProcessFailure> {
|
|
771
|
+
if (initial.pending !== null) return yield* deliverPending(key, initial, nowMillis);
|
|
772
|
+
if (
|
|
773
|
+
initial.state !== "active" ||
|
|
774
|
+
initial.nextAtMillis === null ||
|
|
775
|
+
initial.nextAtMillis > nowMillis
|
|
776
|
+
) {
|
|
777
|
+
return initial;
|
|
778
|
+
}
|
|
797
779
|
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
780
|
+
const currentInputDigest = yield* withCrypto(digestJson(initial.configuration.input)).pipe(
|
|
781
|
+
Effect.mapError(() =>
|
|
782
|
+
ScheduleStorageError.make({ operation: "prepare", reason: "unavailable" }),
|
|
783
|
+
),
|
|
784
|
+
);
|
|
801
785
|
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
786
|
+
if (currentInputDigest !== initial.configuration.inputDigest) {
|
|
787
|
+
return yield* corrupt("prepare");
|
|
788
|
+
}
|
|
805
789
|
|
|
806
|
-
|
|
790
|
+
const due = yield* Effect.fromResult(
|
|
791
|
+
scheduleDueOccurrence(initial.configuration.timing, initial.nextAtMillis, nowMillis),
|
|
792
|
+
);
|
|
807
793
|
|
|
808
|
-
|
|
809
|
-
digestJson({
|
|
810
|
-
schemaVersion: 1,
|
|
811
|
-
owner: initial.owner,
|
|
812
|
-
scheduleId: initial.scheduleId,
|
|
813
|
-
configurationRevision: initial.configurationRevision,
|
|
814
|
-
intendedAtMillis: due.intendedAtMillis,
|
|
815
|
-
}),
|
|
816
|
-
).pipe(Effect.mapError(() => corrupt("prepare")));
|
|
794
|
+
if (due === null) return initial;
|
|
817
795
|
|
|
818
|
-
|
|
819
|
-
|
|
796
|
+
const occurrenceId = yield* withCrypto(
|
|
797
|
+
digestJson({
|
|
798
|
+
schemaVersion: 1,
|
|
799
|
+
owner: initial.owner,
|
|
800
|
+
scheduleId: initial.scheduleId,
|
|
820
801
|
configurationRevision: initial.configurationRevision,
|
|
821
|
-
configuration: initial.configuration,
|
|
822
802
|
intendedAtMillis: due.intendedAtMillis,
|
|
823
|
-
|
|
824
|
-
|
|
803
|
+
}),
|
|
804
|
+
).pipe(Effect.mapError(() => corrupt("prepare")));
|
|
825
805
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
return yield* store.change(key, {
|
|
834
|
-
_tag: "DenyPreparation",
|
|
835
|
-
expectedRevision: initial.configurationRevision,
|
|
836
|
-
expectedCursor: initial.nextAtMillis,
|
|
837
|
-
refusal: {
|
|
838
|
-
atMillis: nowMillis,
|
|
839
|
-
intendedAtMillis: due.intendedAtMillis,
|
|
840
|
-
occurrenceId,
|
|
841
|
-
phase: "preparation",
|
|
842
|
-
code: authorization.failure.code,
|
|
843
|
-
},
|
|
844
|
-
nowMillis,
|
|
845
|
-
});
|
|
846
|
-
}
|
|
806
|
+
const occurrence = {
|
|
807
|
+
key,
|
|
808
|
+
configurationRevision: initial.configurationRevision,
|
|
809
|
+
configuration: initial.configuration,
|
|
810
|
+
intendedAtMillis: due.intendedAtMillis,
|
|
811
|
+
occurrenceId,
|
|
812
|
+
};
|
|
847
813
|
|
|
848
|
-
|
|
849
|
-
initial.configuration.destination._tag === "ExistingThread"
|
|
850
|
-
? initial.configuration.destination.threadId
|
|
851
|
-
: Schema.decodeSync(ThreadId)(`schedule-thread:${occurrenceId}`);
|
|
814
|
+
const authorization = yield* authorizer.prepare(occurrence).pipe(Effect.result);
|
|
852
815
|
|
|
853
|
-
|
|
816
|
+
if (Result.isFailure(authorization)) {
|
|
817
|
+
if (authorization.failure._tag === "ScheduleStorageError") {
|
|
818
|
+
return yield* authorization.failure;
|
|
819
|
+
}
|
|
854
820
|
|
|
855
|
-
|
|
856
|
-
_tag: "
|
|
821
|
+
return yield* store.change(key, {
|
|
822
|
+
_tag: "DenyPreparation",
|
|
857
823
|
expectedRevision: initial.configurationRevision,
|
|
858
824
|
expectedCursor: initial.nextAtMillis,
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
owner: initial.owner,
|
|
862
|
-
scheduleId: initial.scheduleId,
|
|
863
|
-
configurationRevision: initial.configurationRevision,
|
|
825
|
+
refusal: {
|
|
826
|
+
atMillis: nowMillis,
|
|
864
827
|
intendedAtMillis: due.intendedAtMillis,
|
|
865
|
-
preparedAtMillis: nowMillis,
|
|
866
828
|
occurrenceId,
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
...(initial.configuration.admissionGroup === undefined
|
|
870
|
-
? {}
|
|
871
|
-
: { admissionGroup: initial.configuration.admissionGroup }),
|
|
872
|
-
...(initial.configuration.admissionFence === undefined
|
|
873
|
-
? {}
|
|
874
|
-
: { admissionFence: initial.configuration.admissionFence }),
|
|
875
|
-
agentId: initial.configuration.agentId,
|
|
876
|
-
definitions: initial.configuration.definitions,
|
|
877
|
-
input: initial.configuration.input,
|
|
878
|
-
inputDigest: initial.configuration.inputDigest,
|
|
879
|
-
admissionKey,
|
|
880
|
-
authorization: authorization.success,
|
|
829
|
+
phase: "preparation",
|
|
830
|
+
code: authorization.failure.code,
|
|
881
831
|
},
|
|
882
|
-
nextAtMillis: due.nextAtMillis,
|
|
883
|
-
skippedRange: due.skippedRange,
|
|
884
832
|
nowMillis,
|
|
885
833
|
});
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
const threadId =
|
|
837
|
+
initial.configuration.destination._tag === "ExistingThread"
|
|
838
|
+
? initial.configuration.destination.threadId
|
|
839
|
+
: Schema.decodeSync(ThreadId)(`schedule-thread:${occurrenceId}`);
|
|
886
840
|
|
|
887
|
-
|
|
841
|
+
const admissionKey = Schema.decodeSync(IdempotencyKey)(`schedule-occurrence:${occurrenceId}`);
|
|
842
|
+
|
|
843
|
+
const prepared = yield* store.change(key, {
|
|
844
|
+
_tag: "Prepare",
|
|
845
|
+
expectedRevision: initial.configurationRevision,
|
|
846
|
+
expectedCursor: initial.nextAtMillis,
|
|
847
|
+
envelope: {
|
|
848
|
+
schemaVersion: 1,
|
|
849
|
+
owner: initial.owner,
|
|
850
|
+
scheduleId: initial.scheduleId,
|
|
851
|
+
configurationRevision: initial.configurationRevision,
|
|
852
|
+
intendedAtMillis: due.intendedAtMillis,
|
|
853
|
+
preparedAtMillis: nowMillis,
|
|
854
|
+
occurrenceId,
|
|
855
|
+
threadId,
|
|
856
|
+
deliveryPrincipal: initial.configuration.deliveryPrincipal,
|
|
857
|
+
...(initial.configuration.admissionGroup === undefined
|
|
858
|
+
? {}
|
|
859
|
+
: { admissionGroup: initial.configuration.admissionGroup }),
|
|
860
|
+
...(initial.configuration.admissionFence === undefined
|
|
861
|
+
? {}
|
|
862
|
+
: { admissionFence: initial.configuration.admissionFence }),
|
|
863
|
+
agentId: initial.configuration.agentId,
|
|
864
|
+
definitions: initial.configuration.definitions,
|
|
865
|
+
input: initial.configuration.input,
|
|
866
|
+
inputDigest: initial.configuration.inputDigest,
|
|
867
|
+
admissionKey,
|
|
868
|
+
authorization: authorization.success,
|
|
869
|
+
},
|
|
870
|
+
nextAtMillis: due.nextAtMillis,
|
|
871
|
+
skippedRange: due.skippedRange,
|
|
872
|
+
nowMillis,
|
|
888
873
|
});
|
|
889
874
|
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
875
|
+
return yield* deliverPending(key, prepared, nowMillis);
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
const process = Effect.fnUntraced(function* (key: ScheduleKey) {
|
|
879
|
+
const nowMillis = yield* currentMillis;
|
|
880
|
+
const record = yield* store.get(key);
|
|
893
881
|
|
|
894
|
-
|
|
882
|
+
if (record === null) return yield* notFound(key);
|
|
895
883
|
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
),
|
|
884
|
+
const processed = yield* processRecord(key, record, nowMillis).pipe(
|
|
885
|
+
Effect.catchTag("ScheduleConflict", () =>
|
|
886
|
+
store
|
|
887
|
+
.get(key)
|
|
888
|
+
.pipe(
|
|
889
|
+
Effect.flatMap((current) =>
|
|
890
|
+
current === null
|
|
891
|
+
? Effect.fail(notFound(key))
|
|
892
|
+
: processRecord(key, current, nowMillis),
|
|
906
893
|
),
|
|
907
|
-
|
|
908
|
-
)
|
|
894
|
+
),
|
|
895
|
+
),
|
|
896
|
+
);
|
|
909
897
|
|
|
910
|
-
|
|
911
|
-
|
|
898
|
+
return processed;
|
|
899
|
+
});
|
|
912
900
|
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
}),
|
|
901
|
+
const runDue = Effect.fnUntraced(function* (owner?: ScheduleOwner) {
|
|
902
|
+
const nowMillis = yield* currentMillis;
|
|
903
|
+
let after: ScheduleDueCursor | undefined;
|
|
904
|
+
let processed = 0;
|
|
905
|
+
let failed = 0;
|
|
906
|
+
|
|
907
|
+
while (true) {
|
|
908
|
+
const due = yield* store.due(nowMillis, limits.dueBatchSize, owner, after);
|
|
909
|
+
|
|
910
|
+
if (due.length === 0) break;
|
|
911
|
+
yield* Effect.forEach(
|
|
912
|
+
due,
|
|
913
|
+
(key) =>
|
|
914
|
+
process(key).pipe(
|
|
915
|
+
Effect.matchCauseEffect({
|
|
916
|
+
onSuccess: () =>
|
|
917
|
+
Effect.sync(() => {
|
|
918
|
+
processed += 1;
|
|
919
|
+
}),
|
|
920
|
+
onFailure: (cause) =>
|
|
921
|
+
Cause.hasInterrupts(cause)
|
|
922
|
+
? Effect.interrupt
|
|
923
|
+
: Effect.gen(function* () {
|
|
924
|
+
failed += 1;
|
|
925
|
+
yield* Effect.logWarning("Schedule processing failed").pipe(
|
|
926
|
+
Effect.annotateLogs({
|
|
927
|
+
failureTag: Option.match(Cause.findErrorOption(cause), {
|
|
928
|
+
onNone: () => "Defect",
|
|
929
|
+
onSome: (error) => error._tag,
|
|
943
930
|
}),
|
|
944
|
-
)
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
),
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
return { processed, failed };
|
|
955
|
-
});
|
|
931
|
+
}),
|
|
932
|
+
);
|
|
933
|
+
}),
|
|
934
|
+
}),
|
|
935
|
+
),
|
|
936
|
+
{ concurrency: limits.admissionConcurrency },
|
|
937
|
+
);
|
|
938
|
+
after = due.at(-1);
|
|
939
|
+
if (due.length < limits.dueBatchSize) break;
|
|
940
|
+
}
|
|
956
941
|
|
|
957
|
-
return
|
|
942
|
+
return { processed, failed };
|
|
958
943
|
});
|
|
959
944
|
|
|
945
|
+
return ScheduleDriver.of({ process, runDue });
|
|
946
|
+
});
|
|
947
|
+
|
|
960
948
|
const corrupt = (operation: string): ScheduleStorageError =>
|
|
961
949
|
ScheduleStorageError.make({ operation, reason: "corrupt" });
|
|
962
950
|
|