@capybara-hq/kernel 0.1.0-rc.105 → 0.1.0-rc.106
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/agents/agents.schemas.d.ts +9 -9
- package/dist/{schedules/schedules.controller.d.ts → automations/automations.controller.d.ts} +37 -32
- package/dist/{schedules/schedules.controller.js → automations/automations.controller.js} +68 -57
- package/dist/automations/automations.module.d.ts +16 -0
- package/dist/{schedules/schedules.module.js → automations/automations.module.js} +18 -15
- package/dist/{schedules/schedules.schemas.d.ts → automations/automations.schemas.d.ts} +151 -50
- package/dist/automations/automations.schemas.js +253 -0
- package/dist/{schedules/scheduled-tasks.service.d.ts → automations/automations.service.d.ts} +99 -52
- package/dist/{schedules/scheduled-tasks.service.js → automations/automations.service.js} +303 -134
- package/dist/{schedules → automations}/calendar.d.ts +8 -8
- package/dist/{schedules → automations}/calendar.js +2 -2
- package/dist/automations/effective-settings.d.ts +28 -0
- package/dist/automations/effective-settings.js +25 -0
- package/dist/automations/errors.d.ts +19 -0
- package/dist/automations/errors.js +19 -0
- package/dist/automations/events/coalesce.d.ts +28 -0
- package/dist/automations/events/coalesce.js +131 -0
- package/dist/automations/events/deadline.d.ts +10 -0
- package/dist/automations/events/deadline.js +24 -0
- package/dist/automations/events/dispatcher.d.ts +74 -0
- package/dist/automations/events/dispatcher.js +353 -0
- package/dist/automations/events/events.module.d.ts +7 -0
- package/dist/automations/events/events.module.js +26 -0
- package/dist/automations/events/guards.d.ts +32 -0
- package/dist/automations/events/guards.js +87 -0
- package/dist/automations/events/sweep.job.d.ts +49 -0
- package/dist/automations/events/sweep.job.js +202 -0
- package/dist/automations/events/token.d.ts +5 -0
- package/dist/automations/events/token.js +5 -0
- package/dist/automations/events/trigger-events.service.d.ts +47 -0
- package/dist/automations/events/trigger-events.service.js +254 -0
- package/dist/{schedules → automations}/fire.d.ts +57 -17
- package/dist/{schedules → automations}/fire.js +311 -79
- package/dist/{schedules → automations}/grant-rules.d.ts +1 -1
- package/dist/{schedules → automations}/grant-rules.js +5 -5
- package/dist/automations/job-ids.d.ts +113 -0
- package/dist/automations/job-ids.js +117 -0
- package/dist/{schedules → automations}/pause.d.ts +3 -3
- package/dist/{schedules → automations}/pause.js +3 -3
- package/dist/{schedules → automations}/run-tracker.d.ts +21 -10
- package/dist/{schedules → automations}/run-tracker.js +68 -56
- package/dist/{schedules → automations}/scan.job.d.ts +12 -8
- package/dist/{schedules → automations}/scan.job.js +39 -23
- package/dist/{schedules → automations}/schedule-math.d.ts +19 -12
- package/dist/{schedules → automations}/schedule-math.js +17 -7
- package/dist/{schedules → automations}/store.d.ts +57 -27
- package/dist/{schedules → automations}/store.js +102 -52
- package/dist/automations/store.module.d.ts +6 -0
- package/dist/{schedules → automations}/store.module.js +9 -9
- package/dist/automations/types.d.ts +178 -0
- package/dist/{schedules → automations}/unattended.d.ts +22 -11
- package/dist/{schedules → automations}/unattended.js +53 -30
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/channels/channels.module.js +3 -3
- package/dist/channels/outbound.d.ts +5 -5
- package/dist/channels/outbound.js +17 -17
- package/dist/channels/threads.d.ts +1 -1
- package/dist/channels/threads.js +1 -1
- package/dist/config/schema.d.ts +5 -1
- package/dist/config/schema.js +44 -6
- package/dist/conversations/approval-profile.d.ts +2 -2
- package/dist/conversations/approval-profile.js +3 -3
- package/dist/conversations/conversations.controller.d.ts +7 -1
- package/dist/conversations/conversations.controller.js +26 -10
- package/dist/conversations/conversations.service.d.ts +101 -25
- package/dist/conversations/conversations.service.js +132 -32
- package/dist/conversations/events.d.ts +46 -0
- package/dist/conversations/events.js +54 -3
- package/dist/conversations/message-context.d.ts +2 -2
- package/dist/conversations/message-context.js +1 -1
- package/dist/conversations/permission-profile-store.d.ts +2 -1
- package/dist/conversations/permission-profiles.controller.d.ts +3 -0
- package/dist/conversations/permission-profiles.service.js +1 -1
- package/dist/conversations/prompt-suggestion-request.d.ts +1 -1
- package/dist/conversations/prompt-suggestion-request.js +2 -1
- package/dist/conversations/queue.d.ts +7 -4
- package/dist/conversations/queue.js +6 -2
- package/dist/conversations/queue.service.d.ts +5 -5
- package/dist/conversations/queue.service.js +20 -14
- package/dist/ctx/create-ctx.d.ts +20 -1
- package/dist/ctx/create-ctx.js +59 -2
- package/dist/db/kernel-schema.d.ts +104 -25
- package/dist/devices/device-project-files.service.d.ts +88 -0
- package/dist/devices/device-project-files.service.js +249 -0
- package/dist/devices/device-projects.service.d.ts +40 -0
- package/dist/devices/device-projects.service.js +16 -13
- package/dist/devices/devices.controller.d.ts +9 -3
- package/dist/devices/devices.controller.js +27 -5
- package/dist/devices/devices.module.js +9 -1
- package/dist/devices/devices.schemas.js +6 -1
- package/dist/devices/dto.d.ts +15 -0
- package/dist/devices/dto.js +11 -0
- package/dist/devices/project-paths.d.ts +29 -0
- package/dist/devices/project-paths.js +66 -0
- package/dist/devices/project-rules.d.ts +5 -1
- package/dist/devices/project-rules.js +5 -1
- package/dist/errors.d.ts +34 -13
- package/dist/errors.js +34 -13
- package/dist/evals/reconstruct.d.ts +10 -1
- package/dist/evals/reconstruct.js +7 -0
- package/dist/extensions/host.d.ts +6 -1
- package/dist/extensions/host.js +10 -0
- package/dist/extensions/permission-set.d.ts +17 -1
- package/dist/extensions/permission-set.js +13 -1
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +4 -1
- package/dist/git/gateway.js +2 -2
- package/dist/http/api.module.js +5 -5
- package/dist/http/request-signal.d.ts +8 -0
- package/dist/http/request-signal.js +14 -0
- package/dist/inbox/inbox.module.js +2 -2
- package/dist/inbox/inbox.service.d.ts +8 -8
- package/dist/inbox/inbox.service.js +7 -7
- package/dist/infra/config/kernel-config.service.js +10 -5
- package/dist/infra/outbox/registry.d.ts +29 -2
- package/dist/infra/outbox/registry.js +30 -7
- package/dist/kernel.module.js +3 -0
- package/dist/orgs/roles.d.ts +1 -1
- package/dist/orgs/roles.js +3 -3
- package/dist/orgs/settings.controller.d.ts +1 -1
- package/dist/orgs/settings.service.d.ts +27 -11
- package/dist/orgs/settings.service.js +42 -24
- package/dist/platform/operation.js +2 -2
- package/dist/platform/ops/{schedules.ops.d.ts → automations.ops.d.ts} +4 -4
- package/dist/platform/ops/automations.ops.js +560 -0
- package/dist/platform/ops/devices.ops.d.ts +3 -1
- package/dist/platform/ops/devices.ops.js +77 -3
- package/dist/platform/ops/inbox.ops.js +4 -4
- package/dist/platform/ops/orgs.ops.js +1 -1
- package/dist/platform/ops/platform.ops.js +4 -4
- package/dist/platform/platform-ops.module.d.ts +2 -2
- package/dist/platform/platform-ops.module.js +5 -5
- package/dist/platform/route-schemas.js +3 -3
- package/dist/push/jobs.d.ts +3 -3
- package/dist/push/jobs.js +2 -2
- package/dist/push/preferences.service.js +1 -1
- package/dist/push/push-owed.d.ts +3 -3
- package/dist/push/push-owed.js +6 -6
- package/dist/push/push.schemas.d.ts +4 -4
- package/dist/push/push.schemas.js +3 -3
- package/dist/push/recipients.d.ts +4 -4
- package/dist/push/recipients.js +23 -23
- package/dist/push/texts.d.ts +3 -3
- package/dist/push/texts.js +2 -2
- package/dist/push/triggers.d.ts +1 -1
- package/dist/roles/worker.module.js +3 -3
- package/dist/runtime/context/build.d.ts +6 -0
- package/dist/runtime/context/build.js +14 -3
- package/dist/runtime/context/compaction.js +15 -5
- package/dist/runtime/context/run-context.d.ts +38 -0
- package/dist/runtime/context/run-context.js +54 -0
- package/dist/runtime/maintenance.d.ts +8 -4
- package/dist/runtime/maintenance.js +37 -16
- package/dist/runtime/run-finish.d.ts +4 -4
- package/dist/runtime/run-finish.js +3 -3
- package/dist/runtime/run-queue.js +12 -7
- package/dist/runtime/run-worker.js +7 -1
- package/dist/runtime/runtime.module.js +3 -3
- package/dist/runtime/subagents/dispatch.js +11 -3
- package/dist/sandbox/browse-roots.d.ts +8 -0
- package/dist/sandbox/browse-roots.js +22 -0
- package/dist/sandbox/network-grants.d.ts +4 -4
- package/dist/sandbox/network-grants.js +7 -7
- package/dist/sandbox/operation-authority.d.ts +10 -3
- package/dist/sandbox/operation-authority.js +20 -10
- package/dist/sandbox/operation-intents.d.ts +3 -1
- package/dist/sandbox/operation-intents.js +11 -4
- package/dist/sandbox/permission-call-grants.js +2 -2
- package/dist/sandbox/permission-facts.js +1 -1
- package/dist/sandbox/review/classifier.d.ts +2 -2
- package/dist/sandbox/review/classifier.js +22 -11
- package/dist/sandbox/review/permission-candidate.d.ts +1 -1
- package/dist/sandbox/review/permission-candidate.js +1 -1
- package/dist/sandbox/sandbox.service.d.ts +15 -5
- package/dist/sandbox/sandbox.service.js +14 -8
- package/dist/storage/purge.d.ts +1 -1
- package/dist/storage/purge.js +2 -2
- package/dist/tools/approvals.service.d.ts +6 -6
- package/dist/tools/approvals.service.js +19 -19
- package/dist/tools/builtin-names.d.ts +12 -12
- package/dist/tools/builtin-names.js +29 -29
- package/dist/tools/builtins/{schedules.d.ts → automations.d.ts} +35 -33
- package/dist/tools/builtins/{schedules.js → automations.js} +236 -105
- package/dist/tools/builtins/builtin-tools.d.ts +8 -8
- package/dist/tools/builtins/builtin-tools.js +16 -16
- package/dist/tools/call-scope.d.ts +1 -1
- package/dist/tools/git-asks.js +1 -1
- package/dist/tools/git-push-policy.d.ts +3 -3
- package/dist/tools/git-push-policy.js +6 -6
- package/dist/tools/network-asks.d.ts +3 -3
- package/dist/tools/network-asks.js +3 -3
- package/dist/tools/pipeline.d.ts +4 -4
- package/dist/tools/pipeline.js +2 -2
- package/dist/tools/policy.d.ts +3 -3
- package/dist/tools/policy.js +3 -3
- package/dist/tools/tools.module.js +9 -9
- package/package.json +3 -3
- package/src/migrations/0188_automations_rename.sql +309 -0
- package/src/migrations/0189_event_automations.sql +136 -0
- package/dist/platform/ops/schedules.ops.js +0 -509
- package/dist/schedules/effective-settings.d.ts +0 -22
- package/dist/schedules/effective-settings.js +0 -18
- package/dist/schedules/errors.d.ts +0 -8
- package/dist/schedules/errors.js +0 -8
- package/dist/schedules/job-ids.d.ts +0 -81
- package/dist/schedules/job-ids.js +0 -95
- package/dist/schedules/schedules.module.d.ts +0 -16
- package/dist/schedules/schedules.schemas.js +0 -213
- package/dist/schedules/store.module.d.ts +0 -6
- package/dist/schedules/types.d.ts +0 -110
- /package/dist/{schedules → automations}/types.js +0 -0
|
@@ -14,7 +14,7 @@ import { KernelDb } from "../infra/database/kernel-db.js";
|
|
|
14
14
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
15
15
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
16
16
|
import { PushOwed, PushOwedModule } from "../push/push-owed.js";
|
|
17
|
-
import {
|
|
17
|
+
import { readAutomationSettings } from "./effective-settings.js";
|
|
18
18
|
import { pauseTaskIn } from "./pause.js";
|
|
19
19
|
/** The kernel run statuses that are terminal (`runtime/run-finish.ts`, not imported: a cycle). */
|
|
20
20
|
const RUN_TERMINAL = new Set(["completed", "failed", "aborted"]);
|
|
@@ -33,13 +33,13 @@ export const START_GRACE_MS = 5 * 60_000;
|
|
|
33
33
|
* {@link inFlightIn} is the overlap check's view of the task's previous run: it settles a run whose
|
|
34
34
|
* end was missed and releases one whose queued message is gone.
|
|
35
35
|
*/
|
|
36
|
-
let
|
|
36
|
+
let AutomationRunTracker = class AutomationRunTracker {
|
|
37
37
|
kdb;
|
|
38
38
|
txHost;
|
|
39
39
|
config;
|
|
40
40
|
push;
|
|
41
41
|
constructor(kdb, txHost, config,
|
|
42
|
-
/** Owes the task owner's push (`
|
|
42
|
+
/** Owes the task owner's push (`automations`) with every settle. */
|
|
43
43
|
push) {
|
|
44
44
|
this.kdb = kdb;
|
|
45
45
|
this.txHost = txHost;
|
|
@@ -51,30 +51,30 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* The kernel run `runId` ended with `outcome`: the task run it was started for (the run its
|
|
54
|
-
* message triggered, `
|
|
54
|
+
* message triggered, `automation_runs.run_id`) takes the outcome. A sub-agent or wake run
|
|
55
55
|
* that inherited the marker is not the task run's own and changes nothing.
|
|
56
56
|
*/
|
|
57
57
|
async onRunFinished(runId, outcome, error) {
|
|
58
58
|
const run = await this.db
|
|
59
59
|
.selectFrom("runs")
|
|
60
|
-
.select("
|
|
60
|
+
.select("automation_run_id")
|
|
61
61
|
.where("id", "=", runId)
|
|
62
62
|
.executeTakeFirst();
|
|
63
|
-
const taskRunId = run?.
|
|
63
|
+
const taskRunId = run?.automation_run_id;
|
|
64
64
|
if (taskRunId == null)
|
|
65
65
|
return;
|
|
66
66
|
const owner = await this.db
|
|
67
|
-
.selectFrom("
|
|
68
|
-
.select("
|
|
67
|
+
.selectFrom("automation_runs")
|
|
68
|
+
.select("automation_id")
|
|
69
69
|
.where("id", "=", taskRunId)
|
|
70
70
|
.executeTakeFirst();
|
|
71
71
|
if (!owner)
|
|
72
72
|
return;
|
|
73
73
|
// lock order task row → task run, as the fire job's claim takes them (no lock cycle)
|
|
74
|
-
await lockTaskRow(this.db, owner.
|
|
74
|
+
await lockTaskRow(this.db, owner.automation_id);
|
|
75
75
|
const tr = await this.db
|
|
76
|
-
.selectFrom("
|
|
77
|
-
.select(["id", "
|
|
76
|
+
.selectFrom("automation_runs")
|
|
77
|
+
.select(["id", "automation_id", "org_id", "trial", "status", "run_id"])
|
|
78
78
|
.where("id", "=", taskRunId)
|
|
79
79
|
.forUpdate()
|
|
80
80
|
.executeTakeFirst();
|
|
@@ -95,7 +95,7 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
95
95
|
const t = this.db;
|
|
96
96
|
await lockTaskRow(t, prev.taskId);
|
|
97
97
|
const tr = await t
|
|
98
|
-
.selectFrom("
|
|
98
|
+
.selectFrom("automation_runs")
|
|
99
99
|
.selectAll()
|
|
100
100
|
.where("id", "=", prev.id)
|
|
101
101
|
.forUpdate()
|
|
@@ -123,15 +123,15 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
123
123
|
// when the job failed for good before its claim)
|
|
124
124
|
if (tr.fired_at === null)
|
|
125
125
|
return true;
|
|
126
|
-
// only a run with a target conversation is ever queued (`conversation`, `chat`):
|
|
127
|
-
// every queue's markers for the others
|
|
126
|
+
// only a run with a target conversation is ever queued (`conversation`, `chat`, `subject`):
|
|
127
|
+
// no scan of every queue's markers for the others
|
|
128
128
|
const queued = tr.conversation_id === null
|
|
129
129
|
? undefined
|
|
130
130
|
: await t
|
|
131
131
|
.selectFrom("conversation_queue")
|
|
132
132
|
.select("id")
|
|
133
133
|
.where("conversation_id", "=", tr.conversation_id)
|
|
134
|
-
.where(sql `schedule ->> 'taskRunId'`, "=", tr.id)
|
|
134
|
+
.where(sql `coalesce(schedule ->> 'taskRunId', automation_event ->> 'automationRunId')`, "=", tr.id)
|
|
135
135
|
.limit(1)
|
|
136
136
|
.executeTakeFirst();
|
|
137
137
|
if (queued)
|
|
@@ -141,7 +141,7 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
141
141
|
.selectFrom("conversations")
|
|
142
142
|
.select("id")
|
|
143
143
|
.where("org_id", "=", tr.org_id)
|
|
144
|
-
.where("
|
|
144
|
+
.where("automation_id", "=", tr.automation_id)
|
|
145
145
|
.where("create_request_id", "=", tr.id)
|
|
146
146
|
.executeTakeFirst())?.id;
|
|
147
147
|
if (conv !== undefined) {
|
|
@@ -149,13 +149,13 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
149
149
|
.selectFrom("runs")
|
|
150
150
|
.select(["id", "status"])
|
|
151
151
|
.where("conversation_id", "=", conv)
|
|
152
|
-
.where("
|
|
152
|
+
.where("automation_run_id", "=", tr.id)
|
|
153
153
|
.orderBy("created_at")
|
|
154
154
|
.limit(1)
|
|
155
155
|
.executeTakeFirst();
|
|
156
156
|
if (run) {
|
|
157
157
|
await t
|
|
158
|
-
.updateTable("
|
|
158
|
+
.updateTable("automation_runs")
|
|
159
159
|
.set({ status: "running", run_id: run.id, conversation_id: conv })
|
|
160
160
|
.where("id", "=", tr.id)
|
|
161
161
|
.where("org_id", "=", tr.org_id)
|
|
@@ -170,7 +170,7 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
170
170
|
.select("seq")
|
|
171
171
|
.where("conversation_id", "=", conv)
|
|
172
172
|
.where("type", "=", "user_message")
|
|
173
|
-
.where(sql `payload -> 'schedule' ->> 'taskRunId'`, "=", tr.id)
|
|
173
|
+
.where(sql `coalesce(payload -> 'schedule' ->> 'taskRunId', payload -> 'automationEvent' ->> 'automationRunId')`, "=", tr.id)
|
|
174
174
|
.limit(1)
|
|
175
175
|
.executeTakeFirst();
|
|
176
176
|
if (message) {
|
|
@@ -193,7 +193,7 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
193
193
|
if (now.getTime() - firedAt.getTime() < START_GRACE_MS)
|
|
194
194
|
return true;
|
|
195
195
|
await t
|
|
196
|
-
.updateTable("
|
|
196
|
+
.updateTable("automation_runs")
|
|
197
197
|
.set({ status: "skipped", skip_reason: "removed", fired_at: null })
|
|
198
198
|
.where("id", "=", tr.id)
|
|
199
199
|
.where("org_id", "=", tr.org_id)
|
|
@@ -209,8 +209,8 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
209
209
|
*/
|
|
210
210
|
async failStartIn(taskRunId, error) {
|
|
211
211
|
const tr = await this.db
|
|
212
|
-
.selectFrom("
|
|
213
|
-
.select(["id", "
|
|
212
|
+
.selectFrom("automation_runs")
|
|
213
|
+
.select(["id", "automation_id", "org_id", "trial", "status"])
|
|
214
214
|
.where("id", "=", taskRunId)
|
|
215
215
|
.forUpdate()
|
|
216
216
|
.executeTakeFirst();
|
|
@@ -226,7 +226,7 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
226
226
|
const t = this.db;
|
|
227
227
|
const failed = outcome === "failed";
|
|
228
228
|
await t
|
|
229
|
-
.updateTable("
|
|
229
|
+
.updateTable("automation_runs")
|
|
230
230
|
.set({
|
|
231
231
|
status: failed ? "failed" : "completed",
|
|
232
232
|
...(failed && { error: error ?? "The run failed" }),
|
|
@@ -234,66 +234,66 @@ let ScheduledTaskRunTracker = class ScheduledTaskRunTracker {
|
|
|
234
234
|
.where("id", "=", tr.id)
|
|
235
235
|
.where("org_id", "=", tr.org_id)
|
|
236
236
|
.execute();
|
|
237
|
-
await this.push.
|
|
237
|
+
await this.push.oweAutomationRun(tr.id, tr.org_id);
|
|
238
238
|
if (tr.trial)
|
|
239
239
|
return;
|
|
240
240
|
if (!failed) {
|
|
241
241
|
await t
|
|
242
|
-
.updateTable("
|
|
242
|
+
.updateTable("automations")
|
|
243
243
|
.set({ consecutive_failures: 0 })
|
|
244
|
-
.where("id", "=", tr.
|
|
244
|
+
.where("id", "=", tr.automation_id)
|
|
245
245
|
.where("org_id", "=", tr.org_id)
|
|
246
246
|
.where("consecutive_failures", "!=", 0)
|
|
247
247
|
.execute();
|
|
248
248
|
return;
|
|
249
249
|
}
|
|
250
250
|
const row = await t
|
|
251
|
-
.updateTable("
|
|
251
|
+
.updateTable("automations")
|
|
252
252
|
.set({ consecutive_failures: sql `consecutive_failures + 1` })
|
|
253
|
-
.where("id", "=", tr.
|
|
253
|
+
.where("id", "=", tr.automation_id)
|
|
254
254
|
.where("org_id", "=", tr.org_id)
|
|
255
255
|
.where("deleted_at", "is", null)
|
|
256
256
|
.returning("consecutive_failures")
|
|
257
257
|
.executeTakeFirst();
|
|
258
258
|
if (!row)
|
|
259
259
|
return;
|
|
260
|
-
const settings = await
|
|
260
|
+
const settings = await readAutomationSettings(t, this.config, tr.org_id);
|
|
261
261
|
if (row.consecutive_failures >= settings.pauseAfterFailures) {
|
|
262
|
-
await pauseTaskIn(t, tr.
|
|
262
|
+
await pauseTaskIn(t, tr.automation_id, "failures", null);
|
|
263
263
|
}
|
|
264
264
|
}
|
|
265
265
|
};
|
|
266
266
|
__decorate([
|
|
267
267
|
Transactional()
|
|
268
|
-
],
|
|
268
|
+
], AutomationRunTracker.prototype, "onRunFinished", null);
|
|
269
269
|
__decorate([
|
|
270
270
|
Transactional({ propagation: "mandatory" })
|
|
271
|
-
],
|
|
271
|
+
], AutomationRunTracker.prototype, "inFlightIn", null);
|
|
272
272
|
__decorate([
|
|
273
273
|
Transactional({ propagation: "mandatory" })
|
|
274
|
-
],
|
|
275
|
-
|
|
274
|
+
], AutomationRunTracker.prototype, "failStartIn", null);
|
|
275
|
+
AutomationRunTracker = __decorate([
|
|
276
276
|
Injectable()
|
|
277
277
|
// org-filter: kernel-internal (run finish, fire job, settle job): task run ids come from a kernel
|
|
278
|
-
// run's
|
|
278
|
+
// run's automation_run_id or from a task run row the caller already holds, never from input
|
|
279
279
|
,
|
|
280
280
|
__param(0, Inject(KernelDb)),
|
|
281
281
|
__param(1, Inject(TransactionHost)),
|
|
282
282
|
__param(2, Inject(KernelConfigService)),
|
|
283
283
|
__param(3, Inject(PushOwed))
|
|
284
|
-
],
|
|
285
|
-
export {
|
|
284
|
+
], AutomationRunTracker);
|
|
285
|
+
export { AutomationRunTracker };
|
|
286
286
|
/**
|
|
287
287
|
* A kernel run was created for task run `taskRunId` (its message is the run's trigger, or it
|
|
288
288
|
* inherited the marker): a task run still `pending` takes the run (`running`), in the caller's
|
|
289
289
|
* transaction (`RunQueue.ensureRunIn`, which inserts the run). A task run already linked (a
|
|
290
290
|
* sub-agent or wake run inheriting the marker) is left alone.
|
|
291
291
|
*/
|
|
292
|
-
// org-filter: the task run id is the kernel-written schedule marker of the run's trigger
|
|
293
|
-
// (
|
|
292
|
+
// org-filter: the task run id is the kernel-written schedule or event marker of the run's trigger
|
|
293
|
+
// message (automationRunAt), set only through the kernel-internal options, never by a person
|
|
294
294
|
export async function linkStartedRunIn(t, taskRunId, runId, conversationId) {
|
|
295
295
|
await t
|
|
296
|
-
.updateTable("
|
|
296
|
+
.updateTable("automation_runs")
|
|
297
297
|
.set({
|
|
298
298
|
status: "running",
|
|
299
299
|
run_id: runId,
|
|
@@ -305,18 +305,30 @@ export async function linkStartedRunIn(t, taskRunId, runId, conversationId) {
|
|
|
305
305
|
.execute();
|
|
306
306
|
}
|
|
307
307
|
/**
|
|
308
|
-
*
|
|
309
|
-
*
|
|
308
|
+
* The automation run a queued item's stored marker names: its schedule marker's `taskRunId`, or
|
|
309
|
+
* its event marker's `automationRunId` (collaboration spec §4.6); undefined for a person's item.
|
|
310
|
+
*/
|
|
311
|
+
export function queuedAutomationRunId(item) {
|
|
312
|
+
const fromSchedule = item.schedule?.taskRunId;
|
|
313
|
+
if (typeof fromSchedule === "string")
|
|
314
|
+
return fromSchedule;
|
|
315
|
+
const fromEvent = item.automation_event
|
|
316
|
+
?.automationRunId;
|
|
317
|
+
return typeof fromEvent === "string" ? fromEvent : undefined;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* A person removed, edited or sent the queued item that carried a schedule or event marker (the
|
|
321
|
+
* item's stored columns): its task run will never run as the task's, so it becomes `skipped`
|
|
310
322
|
* (`removed`) in the caller's transaction, and the next tick is not held by the overlap check.
|
|
311
323
|
*/
|
|
312
|
-
// org-filter: the marker is the queued item's stored schedule column, written
|
|
313
|
-
// kernel-internal
|
|
314
|
-
export async function releaseQueuedTaskRunIn(t,
|
|
315
|
-
const taskRunId =
|
|
316
|
-
if (
|
|
324
|
+
// org-filter: the marker is the queued item's stored schedule / automation_event column, written
|
|
325
|
+
// only through the kernel-internal options of the queue (never from a request body)
|
|
326
|
+
export async function releaseQueuedTaskRunIn(t, item) {
|
|
327
|
+
const taskRunId = queuedAutomationRunId(item);
|
|
328
|
+
if (taskRunId === undefined)
|
|
317
329
|
return;
|
|
318
330
|
await t
|
|
319
|
-
.updateTable("
|
|
331
|
+
.updateTable("automation_runs")
|
|
320
332
|
.set({ status: "skipped", skip_reason: "removed", fired_at: null })
|
|
321
333
|
.where("id", "=", taskRunId)
|
|
322
334
|
.where("status", "=", "pending")
|
|
@@ -331,20 +343,20 @@ export async function releaseQueuedTaskRunIn(t, schedule) {
|
|
|
331
343
|
// org-filter: a row lock by the task id of a task or task run row the kernel caller already holds
|
|
332
344
|
export async function lockTaskRow(t, taskId) {
|
|
333
345
|
await t
|
|
334
|
-
.selectFrom("
|
|
346
|
+
.selectFrom("automations")
|
|
335
347
|
.select("id")
|
|
336
348
|
.where("id", "=", taskId)
|
|
337
349
|
.forUpdate()
|
|
338
350
|
.executeTakeFirst();
|
|
339
351
|
}
|
|
340
|
-
/** The run tracker alone: the runtime (`RunFinish`) imports it without the
|
|
341
|
-
let
|
|
352
|
+
/** The run tracker alone: the runtime (`RunFinish`) imports it without the automations service. */
|
|
353
|
+
let AutomationTrackerModule = class AutomationTrackerModule {
|
|
342
354
|
};
|
|
343
|
-
|
|
355
|
+
AutomationTrackerModule = __decorate([
|
|
344
356
|
Module({
|
|
345
357
|
imports: [PushOwedModule],
|
|
346
|
-
providers: [
|
|
347
|
-
exports: [
|
|
358
|
+
providers: [AutomationRunTracker],
|
|
359
|
+
exports: [AutomationRunTracker],
|
|
348
360
|
})
|
|
349
|
-
],
|
|
350
|
-
export {
|
|
361
|
+
], AutomationTrackerModule);
|
|
362
|
+
export { AutomationTrackerModule };
|
|
@@ -4,22 +4,24 @@ import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
|
4
4
|
import { LockService } from "../infra/lock/lock.service.js";
|
|
5
5
|
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
6
6
|
import type { KernelLogger } from "../log.js";
|
|
7
|
-
import {
|
|
7
|
+
import { AutomationStore } from "./store.js";
|
|
8
8
|
/**
|
|
9
|
-
* The
|
|
9
|
+
* The automation scanner (spec §6), once a minute across replicas. Each due task in its own
|
|
10
10
|
* transaction, locked with `FOR UPDATE SKIP LOCKED` (replicas share the work and never take the
|
|
11
11
|
* same task): the ticks missed outside the org's `catchUpWindowMinutes` become one `skipped` run
|
|
12
12
|
* (`missed`, at the first missed tick), the latest tick in the window gets its run (the unique
|
|
13
|
-
* `(
|
|
13
|
+
* `(automation_id, scheduled_for)` key makes a second insert a no-op), `next_run_at` moves to the next
|
|
14
14
|
* tick plus the task's jitter (or a `once` task ends, or one past its `expires_at`), and the same
|
|
15
|
-
* transaction owes `
|
|
15
|
+
* transaction owes `automation-fire-<taskRunId>` (transactional outbox spec §8.1), so a written tick is
|
|
16
16
|
* never left unfired and the scan needs no repair of lost fire jobs (a fire delivered too late
|
|
17
|
-
* marks its tick missed itself, `
|
|
17
|
+
* marks its tick missed itself, `AutomationFirer`), and the run's unfired settle check (a fire
|
|
18
18
|
* job that failed for good before its claim: the tick is missed, `job-ids.ts`). A task whose stored
|
|
19
19
|
* trigger the schedule math cannot evaluate is paused (`invalid_schedule`) instead of failing the
|
|
20
|
-
* scan; a database error fails the pass (the next minute retries) and pauses nothing.
|
|
20
|
+
* scan; a database error fails the pass (the next minute retries) and pauses nothing. An event
|
|
21
|
+
* automation past its `expires_at` (it has no tick to be due at) ends the same way, `expired`, in
|
|
22
|
+
* one transaction per scan (the dispatcher already passes it over).
|
|
21
23
|
*/
|
|
22
|
-
export declare class
|
|
24
|
+
export declare class AutomationScanJob {
|
|
23
25
|
private readonly kdb;
|
|
24
26
|
readonly txHost: TransactionHost;
|
|
25
27
|
private readonly config;
|
|
@@ -27,11 +29,13 @@ export declare class ScheduledTaskScanJob {
|
|
|
27
29
|
private readonly outbox;
|
|
28
30
|
readonly lockService: LockService;
|
|
29
31
|
readonly log: KernelLogger;
|
|
30
|
-
constructor(kdb: KernelDb, txHost: TransactionHost, config: Pick<KernelConfigService, "get">, store:
|
|
32
|
+
constructor(kdb: KernelDb, txHost: TransactionHost, config: Pick<KernelConfigService, "get">, store: AutomationStore, outbox: Pick<OutboxService, "add">, lockService: LockService, log: KernelLogger);
|
|
31
33
|
private get db();
|
|
32
34
|
scan(): Promise<void>;
|
|
33
35
|
/** One scan as of `now` (tests pass their own); returns how many due tasks it handled. */
|
|
34
36
|
scanAt(now: Date): Promise<number>;
|
|
37
|
+
/** Ends the event automations past their expiry (see the class); returns how many. */
|
|
38
|
+
endExpiredEvents(now: Date): Promise<number>;
|
|
35
39
|
/**
|
|
36
40
|
* One due task in its own transaction (a task whose trigger cannot be evaluated rolls back alone
|
|
37
41
|
* and is paused, so it cannot block the others): locked with `SKIP LOCKED`, so replicas take
|
|
@@ -17,13 +17,15 @@ import { LockService } from "../infra/lock/lock.service.js";
|
|
|
17
17
|
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
18
18
|
import { LockedCron } from "../infra/schedule/locked-cron.decorator.js";
|
|
19
19
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
20
|
+
import { readAutomationSettings } from "./effective-settings.js";
|
|
21
|
+
import { AUTOMATION_SCAN_JOB, oweAutomationFire, oweAutomationSettleUnfired } from "./job-ids.js";
|
|
22
22
|
import { pauseTaskIn } from "./pause.js";
|
|
23
23
|
import { scanStep } from "./schedule-math.js";
|
|
24
|
-
import {
|
|
24
|
+
import { AutomationStore } from "./store.js";
|
|
25
25
|
/** Due tasks per scan; what is left waits for the next minute. */
|
|
26
26
|
const MAX_TASKS = 2000;
|
|
27
|
+
/** How a task past its `expires_at` ends (a time task when due, an event task in the scan). */
|
|
28
|
+
const EXPIRED = { status: "ended", pauseReason: "expired", nextRunAt: null };
|
|
27
29
|
/**
|
|
28
30
|
* The schedule math could not evaluate a due task's stored trigger (its transaction rolled back);
|
|
29
31
|
* the scan pauses it. Only the math raises it: a database error fails the scan pass instead (the
|
|
@@ -32,25 +34,27 @@ const MAX_TASKS = 2000;
|
|
|
32
34
|
class TaskScanFailed extends Error {
|
|
33
35
|
taskId;
|
|
34
36
|
constructor(taskId, cause) {
|
|
35
|
-
super(`
|
|
37
|
+
super(`automation ${taskId} cannot be advanced`, { cause });
|
|
36
38
|
this.taskId = taskId;
|
|
37
39
|
}
|
|
38
40
|
}
|
|
39
41
|
/**
|
|
40
|
-
* The
|
|
42
|
+
* The automation scanner (spec §6), once a minute across replicas. Each due task in its own
|
|
41
43
|
* transaction, locked with `FOR UPDATE SKIP LOCKED` (replicas share the work and never take the
|
|
42
44
|
* same task): the ticks missed outside the org's `catchUpWindowMinutes` become one `skipped` run
|
|
43
45
|
* (`missed`, at the first missed tick), the latest tick in the window gets its run (the unique
|
|
44
|
-
* `(
|
|
46
|
+
* `(automation_id, scheduled_for)` key makes a second insert a no-op), `next_run_at` moves to the next
|
|
45
47
|
* tick plus the task's jitter (or a `once` task ends, or one past its `expires_at`), and the same
|
|
46
|
-
* transaction owes `
|
|
48
|
+
* transaction owes `automation-fire-<taskRunId>` (transactional outbox spec §8.1), so a written tick is
|
|
47
49
|
* never left unfired and the scan needs no repair of lost fire jobs (a fire delivered too late
|
|
48
|
-
* marks its tick missed itself, `
|
|
50
|
+
* marks its tick missed itself, `AutomationFirer`), and the run's unfired settle check (a fire
|
|
49
51
|
* job that failed for good before its claim: the tick is missed, `job-ids.ts`). A task whose stored
|
|
50
52
|
* trigger the schedule math cannot evaluate is paused (`invalid_schedule`) instead of failing the
|
|
51
|
-
* scan; a database error fails the pass (the next minute retries) and pauses nothing.
|
|
53
|
+
* scan; a database error fails the pass (the next minute retries) and pauses nothing. An event
|
|
54
|
+
* automation past its `expires_at` (it has no tick to be due at) ends the same way, `expired`, in
|
|
55
|
+
* one transaction per scan (the dispatcher already passes it over).
|
|
52
56
|
*/
|
|
53
|
-
let
|
|
57
|
+
let AutomationScanJob = class AutomationScanJob {
|
|
54
58
|
kdb;
|
|
55
59
|
txHost;
|
|
56
60
|
config;
|
|
@@ -86,15 +90,24 @@ let ScheduledTaskScanJob = class ScheduledTaskScanJob {
|
|
|
86
90
|
if (!(err instanceof TaskScanFailed))
|
|
87
91
|
throw err;
|
|
88
92
|
// one task that cannot be advanced must not hold the others back every minute
|
|
89
|
-
this.log.error("
|
|
93
|
+
this.log.error("automation cannot be advanced; paused", {
|
|
90
94
|
taskId: err.taskId,
|
|
91
95
|
err: err.cause,
|
|
92
96
|
});
|
|
93
97
|
await this.quarantine(err.taskId);
|
|
94
98
|
}
|
|
95
99
|
}
|
|
100
|
+
await this.endExpiredEvents(now);
|
|
96
101
|
return total;
|
|
97
102
|
}
|
|
103
|
+
/** Ends the event automations past their expiry (see the class); returns how many. */
|
|
104
|
+
async endExpiredEvents(now) {
|
|
105
|
+
const t = this.db;
|
|
106
|
+
const expired = await this.store.lockExpiredEvent(t, now, MAX_TASKS);
|
|
107
|
+
for (const task of expired)
|
|
108
|
+
await this.store.updateTask(t, task.id, EXPIRED);
|
|
109
|
+
return expired.length;
|
|
110
|
+
}
|
|
98
111
|
/**
|
|
99
112
|
* One due task in its own transaction (a task whose trigger cannot be evaluated rolls back alone
|
|
100
113
|
* and is paused, so it cannot block the others): locked with `SKIP LOCKED`, so replicas take
|
|
@@ -107,13 +120,13 @@ let ScheduledTaskScanJob = class ScheduledTaskScanJob {
|
|
|
107
120
|
return false;
|
|
108
121
|
let s = settings.get(task.orgId);
|
|
109
122
|
if (s === undefined) {
|
|
110
|
-
s = await
|
|
123
|
+
s = await readAutomationSettings(t, this.config, task.orgId);
|
|
111
124
|
settings.set(task.orgId, s);
|
|
112
125
|
}
|
|
113
126
|
const fired = await this.advance(t, task, now, s.catchUpWindowMinutes);
|
|
114
127
|
if (fired !== null) {
|
|
115
|
-
await
|
|
116
|
-
await
|
|
128
|
+
await oweAutomationFire(this.outbox, fired.id, task.orgId);
|
|
129
|
+
await oweAutomationSettleUnfired(this.outbox, fired.id, task.orgId, fired.scheduledFor, s.catchUpWindowMinutes);
|
|
117
130
|
}
|
|
118
131
|
return true;
|
|
119
132
|
}
|
|
@@ -161,29 +174,32 @@ let ScheduledTaskScanJob = class ScheduledTaskScanJob {
|
|
|
161
174
|
const patch = step.end === "done"
|
|
162
175
|
? { status: "ended", nextRunAt: null }
|
|
163
176
|
: step.end === "expired"
|
|
164
|
-
?
|
|
177
|
+
? EXPIRED
|
|
165
178
|
: { nextRunAt: step.nextRunAt };
|
|
166
179
|
await this.store.updateTask(t, task.id, patch);
|
|
167
180
|
return fired;
|
|
168
181
|
}
|
|
169
182
|
};
|
|
170
183
|
__decorate([
|
|
171
|
-
LockedCron("* * * * *", { name:
|
|
172
|
-
],
|
|
184
|
+
LockedCron("* * * * *", { name: AUTOMATION_SCAN_JOB })
|
|
185
|
+
], AutomationScanJob.prototype, "scan", null);
|
|
186
|
+
__decorate([
|
|
187
|
+
Transactional()
|
|
188
|
+
], AutomationScanJob.prototype, "endExpiredEvents", null);
|
|
173
189
|
__decorate([
|
|
174
190
|
Transactional()
|
|
175
|
-
],
|
|
191
|
+
], AutomationScanJob.prototype, "scanOne", null);
|
|
176
192
|
__decorate([
|
|
177
193
|
Transactional()
|
|
178
|
-
],
|
|
179
|
-
|
|
194
|
+
], AutomationScanJob.prototype, "quarantine", null);
|
|
195
|
+
AutomationScanJob = __decorate([
|
|
180
196
|
Injectable(),
|
|
181
197
|
__param(0, Inject(KernelDb)),
|
|
182
198
|
__param(1, Inject(TransactionHost)),
|
|
183
199
|
__param(2, Inject(KernelConfigService)),
|
|
184
|
-
__param(3, Inject(
|
|
200
|
+
__param(3, Inject(AutomationStore)),
|
|
185
201
|
__param(4, Inject(OutboxService)),
|
|
186
202
|
__param(5, Inject(LockService)),
|
|
187
203
|
__param(6, Inject(KERNEL_LOGGER))
|
|
188
|
-
],
|
|
189
|
-
export {
|
|
204
|
+
], AutomationScanJob);
|
|
205
|
+
export { AutomationScanJob };
|
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export type TriggerError = "
|
|
3
|
-
|
|
1
|
+
import type { AutomationTimeTrigger, AutomationTrigger } from "./types.js";
|
|
2
|
+
export type TriggerError = "automation.invalid_cron" | "automation.invalid_time_zone" | "automation.invalid_once_time" | "automation.once_in_past";
|
|
3
|
+
/** A person's time trigger is valid (spec §5); event triggers are validated elsewhere. */
|
|
4
|
+
export declare function validateTrigger(t: AutomationTimeTrigger, now: Date): {
|
|
4
5
|
ok: true;
|
|
5
6
|
} | {
|
|
6
7
|
ok: false;
|
|
7
8
|
code: TriggerError;
|
|
8
9
|
};
|
|
9
|
-
/**
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* The first tick strictly after `after` (before jitter); null for a past once task and for an event
|
|
12
|
+
* trigger (it has no next run time, collaboration spec §4.1).
|
|
13
|
+
*/
|
|
14
|
+
export declare function nextTick(t: AutomationTrigger, after: Date): Date | null;
|
|
11
15
|
/** The first tick strictly after `after` (before jitter) of one trigger; null when none. */
|
|
12
16
|
export type Ticker = (after: Date) => Date | null;
|
|
13
17
|
/**
|
|
14
18
|
* The scanner's ticker for one trigger ({@link nextTick} without rebuilding its `CronTime`s per
|
|
15
19
|
* call), for callers that walk the same trigger many times ({@link ticksIn}).
|
|
16
20
|
*/
|
|
17
|
-
export declare function tickerOf(t:
|
|
21
|
+
export declare function tickerOf(t: AutomationTrigger): Ticker;
|
|
18
22
|
/**
|
|
19
23
|
* The ticks in `(after, until]` (before jitter), at most `max`, from the same ticker
|
|
20
24
|
* {@link nextTick} and {@link dueTicks} use, so a list of planned runs equals what the scanner
|
|
@@ -23,7 +27,7 @@ export declare function tickerOf(t: ScheduleTrigger): Ticker;
|
|
|
23
27
|
* Costs one tick evaluation per tick returned, plus one when the walk reaches `until`. A caller
|
|
24
28
|
* that walks one trigger in many windows passes a {@link tickerOf} it built once.
|
|
25
29
|
*/
|
|
26
|
-
export declare function ticksIn(t:
|
|
30
|
+
export declare function ticksIn(t: AutomationTrigger | Ticker, after: Date, until: Date, max: number): {
|
|
27
31
|
ticks: Date[];
|
|
28
32
|
exhausted: boolean;
|
|
29
33
|
};
|
|
@@ -45,15 +49,18 @@ export interface ScanStep {
|
|
|
45
49
|
* Throws when the trigger cannot be evaluated.
|
|
46
50
|
*/
|
|
47
51
|
export declare function scanStep(task: {
|
|
48
|
-
trigger:
|
|
52
|
+
trigger: AutomationTrigger;
|
|
49
53
|
nextRunAt: Date;
|
|
50
54
|
jitterSeconds: number;
|
|
51
55
|
expiresAt: Date | null;
|
|
52
56
|
}, now: Date, catchUpWindowMinutes: number): ScanStep;
|
|
53
|
-
/**
|
|
54
|
-
|
|
57
|
+
/**
|
|
58
|
+
* The smallest gap, in minutes, among the next 50 fire times after `from`; null for once and for
|
|
59
|
+
* an event trigger (no ticks).
|
|
60
|
+
*/
|
|
61
|
+
export declare function minGapMinutes(t: AutomationTrigger, from: Date): number | null;
|
|
55
62
|
/** A fixed per-task offset: `fnv1a32(id) mod min(floor(10 % of the interval), 300 s)`; once is 0. */
|
|
56
|
-
export declare function jitterSeconds(taskId: string, t:
|
|
63
|
+
export declare function jitterSeconds(taskId: string, t: AutomationTrigger, from: Date): number;
|
|
57
64
|
/**
|
|
58
65
|
* {@link jitterSeconds} from a gap already sampled ({@link minGapMinutes} of the same trigger and
|
|
59
66
|
* time), so a caller that checked the interval does not walk the 50 ticks again; null gap is 0.
|
|
@@ -67,7 +74,7 @@ export declare function jitterForGap(taskId: string, gap: number | null): number
|
|
|
67
74
|
* it holds a tick, then stepping forward inside it, so a month-long outage costs a handful of tick
|
|
68
75
|
* computations, not thousands.
|
|
69
76
|
*/
|
|
70
|
-
export declare function dueTicks(t:
|
|
77
|
+
export declare function dueTicks(t: AutomationTrigger, lastTick: Date, now: Date, catchUpWindowMinutes: number): {
|
|
71
78
|
fire: Date | null;
|
|
72
79
|
firstMissed: Date | null;
|
|
73
80
|
};
|
|
@@ -85,22 +85,26 @@ function onceInstant(at, timeZone) {
|
|
|
85
85
|
}
|
|
86
86
|
return new Date(lo * 1000);
|
|
87
87
|
}
|
|
88
|
+
/** A person's time trigger is valid (spec §5); event triggers are validated elsewhere. */
|
|
88
89
|
export function validateTrigger(t, now) {
|
|
89
90
|
if (!isTimeZone(t.timeZone))
|
|
90
|
-
return { ok: false, code: "
|
|
91
|
+
return { ok: false, code: "automation.invalid_time_zone" };
|
|
91
92
|
if (t.kind === "cron") {
|
|
92
93
|
return cronTime(t.expr, t.timeZone)
|
|
93
94
|
? { ok: true }
|
|
94
|
-
: { ok: false, code: "
|
|
95
|
+
: { ok: false, code: "automation.invalid_cron" };
|
|
95
96
|
}
|
|
96
97
|
const at = onceInstant(t.at, t.timeZone);
|
|
97
98
|
if (!at)
|
|
98
|
-
return { ok: false, code: "
|
|
99
|
+
return { ok: false, code: "automation.invalid_once_time" };
|
|
99
100
|
if (at.getTime() <= now.getTime())
|
|
100
|
-
return { ok: false, code: "
|
|
101
|
+
return { ok: false, code: "automation.once_in_past" };
|
|
101
102
|
return { ok: true };
|
|
102
103
|
}
|
|
103
|
-
/**
|
|
104
|
+
/**
|
|
105
|
+
* The first tick strictly after `after` (before jitter); null for a past once task and for an event
|
|
106
|
+
* trigger (it has no next run time, collaboration spec §4.1).
|
|
107
|
+
*/
|
|
104
108
|
export function nextTick(t, after) {
|
|
105
109
|
return ticker(t)(after);
|
|
106
110
|
}
|
|
@@ -134,6 +138,9 @@ function nextOrNull(search) {
|
|
|
134
138
|
* (the gap sample, the catch-up enumeration) reuse one ticker instead of rebuilding per tick.
|
|
135
139
|
*/
|
|
136
140
|
function ticker(t) {
|
|
141
|
+
// an event automation has no ticks: the time scanner never fires it
|
|
142
|
+
if (t.kind === "event")
|
|
143
|
+
return () => null;
|
|
137
144
|
if (t.kind === "once") {
|
|
138
145
|
const at = onceInstant(t.at, t.timeZone);
|
|
139
146
|
return (after) => (at && at.getTime() > after.getTime() ? at : null);
|
|
@@ -213,9 +220,12 @@ export function scanStep(task, now, catchUpWindowMinutes) {
|
|
|
213
220
|
function localWallClock(at, timeZone) {
|
|
214
221
|
return Math.floor(wallOf(at.getTime(), timeZone) / 60000) * 60000;
|
|
215
222
|
}
|
|
216
|
-
/**
|
|
223
|
+
/**
|
|
224
|
+
* The smallest gap, in minutes, among the next 50 fire times after `from`; null for once and for
|
|
225
|
+
* an event trigger (no ticks).
|
|
226
|
+
*/
|
|
217
227
|
export function minGapMinutes(t, from) {
|
|
218
|
-
if (t.kind
|
|
228
|
+
if (t.kind !== "cron")
|
|
219
229
|
return null;
|
|
220
230
|
const next = ticker(t);
|
|
221
231
|
let prev = next(from);
|