@capybara-hq/kernel 0.1.0-rc.104 → 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 +103 -25
- package/dist/conversations/conversations.service.js +138 -33
- package/dist/conversations/events.d.ts +48 -1
- package/dist/conversations/events.js +61 -9
- 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 +47 -13
- package/dist/errors.js +47 -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/handoff/app-schemes.d.ts +28 -0
- package/dist/handoff/app-schemes.js +41 -0
- package/dist/handoff/handoff-paths.d.ts +26 -0
- package/dist/handoff/handoff-paths.js +25 -0
- package/dist/handoff/handoff-tickets.d.ts +28 -0
- package/dist/handoff/handoff-tickets.js +111 -0
- package/dist/handoff/handoff.controller.d.ts +24 -0
- package/dist/handoff/handoff.controller.js +74 -0
- package/dist/handoff/handoff.module.d.ts +7 -0
- package/dist/handoff/handoff.module.js +26 -0
- package/dist/handoff/handoff.schemas.d.ts +13 -0
- package/dist/handoff/handoff.schemas.js +3 -0
- package/dist/handoff/handoff.service.d.ts +49 -0
- package/dist/handoff/handoff.service.js +96 -0
- package/dist/http/api.module.js +13 -5
- package/dist/http/meta.controller.d.ts +1 -0
- package/dist/http/meta.controller.js +7 -0
- package/dist/http/request-signal.d.ts +8 -0
- package/dist/http/request-signal.js +14 -0
- package/dist/identity/sso.controller.d.ts +30 -6
- package/dist/identity/sso.controller.js +51 -17
- package/dist/identity/sso.d.ts +10 -2
- package/dist/identity/sso.js +15 -3
- 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/overrides.d.ts +4 -4
- package/dist/overrides.js +1 -1
- package/dist/platform/exclusions.js +5 -0
- 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/connectors.ops.js +4 -2
- 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/compaction.service.d.ts +3 -1
- package/dist/runtime/compaction.service.js +3 -1
- package/dist/runtime/context/build.d.ts +6 -0
- package/dist/runtime/context/build.js +14 -3
- package/dist/runtime/context/compaction.d.ts +27 -1
- package/dist/runtime/context/compaction.js +102 -37
- 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/model-call.d.ts +6 -4
- package/dist/runtime/model-call.js +17 -7
- package/dist/runtime/project/knowledge.service.d.ts +16 -7
- package/dist/runtime/project/knowledge.service.js +31 -13
- 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/connectors.service.d.ts +4 -13
- package/dist/tools/connectors.service.js +12 -21
- 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/oauth/flow.d.ts +16 -2
- package/dist/tools/oauth/flow.js +23 -4
- package/dist/tools/oauth/oauth.controller.d.ts +19 -7
- package/dist/tools/oauth/oauth.controller.js +42 -12
- package/dist/tools/oauth/oauth.schemas.d.ts +14 -1
- package/dist/tools/oauth/oauth.schemas.js +4 -1
- 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
|
@@ -8,13 +8,14 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
8
8
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
9
|
};
|
|
10
10
|
import { Inject, Injectable, Module } from "@nestjs/common";
|
|
11
|
+
import { sql } from "kysely";
|
|
11
12
|
import { AgentLookup, AgentLookupModule } from "../agents/agent-lookup.js";
|
|
12
13
|
import { ChannelsModule } from "../channels/channels.module.js";
|
|
13
14
|
import { ChannelConnections } from "../channels/connections.service.js";
|
|
14
15
|
import { findOrBindThread, findThread } from "../channels/threads.js";
|
|
15
16
|
import { ConversationAccess, ConversationAccessModule } from "../conversations/access.js";
|
|
16
17
|
import { ConversationsModule } from "../conversations/conversations.module.js";
|
|
17
|
-
import { ConversationsService } from "../conversations/conversations.service.js";
|
|
18
|
+
import { ConversationsService, } from "../conversations/conversations.service.js";
|
|
18
19
|
import { ConversationQueue } from "../conversations/queue.service.js";
|
|
19
20
|
import { channelSource } from "../conversations/source.js";
|
|
20
21
|
import { DialInRelay } from "../dial-in/relay.js";
|
|
@@ -28,18 +29,19 @@ import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
|
28
29
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
29
30
|
import { OrgLock, OrgLockModule } from "../orgs/org-lock.js";
|
|
30
31
|
import { FORBIDDEN_TOOLS_UNREADABLE_ERROR } from "../orgs/settings.service.js";
|
|
32
|
+
import { AUTOMATION_EVENT_HEAD, AUTOMATION_EVENT_TAIL } from "../runtime/context/run-context.js";
|
|
31
33
|
import { RuntimeModule } from "../runtime/runtime.module.js";
|
|
32
34
|
import { DeviceProviders, servesDialIn } from "../sandbox/providers.js";
|
|
33
35
|
import { SandboxProviderRegistryModule } from "../sandbox/providers.module.js";
|
|
34
|
-
import {
|
|
35
|
-
import {
|
|
36
|
+
import { readAutomationSettings } from "./effective-settings.js";
|
|
37
|
+
import { AUTOMATION_LATE_FIRE_SLACK_MS, AUTOMATION_SETTLE_AFTER_MS, AUTOMATION_SETTLE_LONG_CHECKS, AUTOMATION_SETTLE_RECHECK_MS, oweAutomationSettle, } from "./job-ids.js";
|
|
36
38
|
import { pauseTaskIn } from "./pause.js";
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
39
|
-
import {
|
|
40
|
-
import {
|
|
39
|
+
import { AutomationRunTracker, AutomationTrackerModule, lockTaskRow } from "./run-tracker.js";
|
|
40
|
+
import { AutomationScanJob } from "./scan.job.js";
|
|
41
|
+
import { AutomationStore } from "./store.js";
|
|
42
|
+
import { AutomationStoreModule } from "./store.module.js";
|
|
41
43
|
/** A task run's message client id in a conversation's queue (never read back as "the task's"). */
|
|
42
|
-
export const
|
|
44
|
+
export const automationClientMessageId = (taskRunId) => `automation-${taskRunId}`;
|
|
43
45
|
const HOUR_MS = 3_600_000;
|
|
44
46
|
/** How many stale previous runs the overlap check releases before it gives up. */
|
|
45
47
|
const OVERLAP_SETTLE_MAX = 5;
|
|
@@ -68,7 +70,7 @@ export function localTimeOf(at, timeZone) {
|
|
|
68
70
|
}
|
|
69
71
|
const DEVICE_GONE = new Set(["device.not_found", "device.unavailable", "sandbox.unavailable"]);
|
|
70
72
|
/**
|
|
71
|
-
* Checks and starts one task run (scheduled tasks spec §6), the body of the `
|
|
73
|
+
* Checks and starts one task run (scheduled tasks spec §6), the body of the `automation-fire-<id>` job.
|
|
72
74
|
* Idempotent on the task run: only a `pending` one is fired. Two steps:
|
|
73
75
|
*
|
|
74
76
|
* 1. The claim, one transaction under the org row lock (which serializes the org's rate limit
|
|
@@ -78,19 +80,25 @@ const DEVICE_GONE = new Set(["device.not_found", "device.unavailable", "sandbox.
|
|
|
78
80
|
* gone or not visible, conversation gone, connection inactive or its extension disabled in the
|
|
79
81
|
* org, overlap, rate limit, device offline, queue paused), each a `skipped` run with its reason,
|
|
80
82
|
* the lasting ones also pausing the task; then `fired_at` is written, after the rate limit count,
|
|
81
|
-
* with the run's delayed settle check owed (`
|
|
83
|
+
* with the run's delayed settle check owed (`automation-settle`, {@link settle}).
|
|
82
84
|
* A run whose claim committed before (a retried job) goes straight to the start.
|
|
83
85
|
* 2. The start, idempotent: `new` creates the conversation with its first message keyed by the
|
|
84
86
|
* task run id (`create_request_id`); `conversation` queues the prompt in the target
|
|
85
87
|
* conversation's queue (never absorbed into a run in progress); `chat` finds the DM's live
|
|
86
88
|
* thread or creates and binds a conversation (one transaction), then queues the prompt there;
|
|
87
89
|
* a group chat gets a new conversation per run whose thread outbound binds at the run's first
|
|
88
|
-
* post (`openGroup`)
|
|
90
|
+
* post (`openGroup`); `subject` finds the subject's conversation (`automation_conversations`)
|
|
91
|
+
* or creates and maps one (one transaction, `bindSubject`), then queues the prompt there. A
|
|
92
|
+
* start that finds the target gone takes `fired_at` back and skips the run.
|
|
89
93
|
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
94
|
+
* A time run's message carries the schedule marker and the task's zone; an event run's carries
|
|
95
|
+
* the event marker (its events, from `automation_event_deliveries`; collaboration spec §4.6). The
|
|
96
|
+
* kernel run its message triggers takes the task run (`linkStartedRunIn`), and the run tracker
|
|
97
|
+
* follows it to its end. An event run skips the overlap check (its events would be lost): a `new`
|
|
98
|
+
* run gets its own conversation, and a `conversation` or `subject` run is queued into its
|
|
99
|
+
* conversation, whose queue serializes the runs. The rate limit counts the owner's runs.
|
|
92
100
|
*/
|
|
93
|
-
let
|
|
101
|
+
let AutomationFirer = class AutomationFirer {
|
|
94
102
|
kdb;
|
|
95
103
|
txHost;
|
|
96
104
|
config;
|
|
@@ -135,14 +143,14 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
135
143
|
/** Fires task run `taskRunId` (see the class); a run not `pending` is left as it is. */
|
|
136
144
|
async fire(taskRunId) {
|
|
137
145
|
const pre = await this.db
|
|
138
|
-
.selectFrom("
|
|
139
|
-
.innerJoin("
|
|
146
|
+
.selectFrom("automation_runs as r")
|
|
147
|
+
.innerJoin("automations as t", "t.id", "r.automation_id")
|
|
140
148
|
.select(["r.status", "r.org_id", "r.fired_at", "t.device_id", "t.mode"])
|
|
141
149
|
.where("r.id", "=", taskRunId)
|
|
142
150
|
.executeTakeFirst();
|
|
143
151
|
if (pre?.status !== "pending")
|
|
144
152
|
return;
|
|
145
|
-
// a Redis read: outside the claim's transaction
|
|
153
|
+
// a Redis read: outside the claim's transaction; only a `new` task takes a device
|
|
146
154
|
const offline = pre.fired_at === null && pre.device_id !== null && pre.mode === "new"
|
|
147
155
|
? await this.deviceOffline(pre.org_id, pre.device_id)
|
|
148
156
|
: false;
|
|
@@ -152,7 +160,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
152
160
|
await this.start(claimed.plan);
|
|
153
161
|
}
|
|
154
162
|
/**
|
|
155
|
-
* The `
|
|
163
|
+
* The `automation-settle` job (ruling R26; `job-ids.ts`). The claimed check: a task run claimed
|
|
156
164
|
* (`fired_at`) and still `pending` goes through the run tracker's check, which links it to its
|
|
157
165
|
* run, settles it, or releases a run that left no trace (`skipped`, `removed`), so a `once`
|
|
158
166
|
* task's last run never stays pending; one still in flight (its item queued, its message posted)
|
|
@@ -177,52 +185,52 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
177
185
|
if (now?.status !== "pending" || now.firedAt === null)
|
|
178
186
|
return;
|
|
179
187
|
const next = data.check + 1;
|
|
180
|
-
if (next ===
|
|
181
|
-
this.log.warn("
|
|
188
|
+
if (next === AUTOMATION_SETTLE_LONG_CHECKS) {
|
|
189
|
+
this.log.warn("automation run still in flight a week after its claim", {
|
|
182
190
|
taskId: run.taskId,
|
|
183
191
|
taskRunId,
|
|
184
192
|
});
|
|
185
193
|
}
|
|
186
|
-
await
|
|
194
|
+
await oweAutomationSettle(this.outbox, taskRunId, run.orgId, next, AUTOMATION_SETTLE_RECHECK_MS);
|
|
187
195
|
}
|
|
188
196
|
/** The unfired check (see {@link settle}), task row first (the one lock order). */
|
|
189
197
|
async settleUnfired(taskRunId) {
|
|
190
198
|
const t = this.db;
|
|
191
199
|
const owner = await t
|
|
192
|
-
.selectFrom("
|
|
193
|
-
.select("
|
|
200
|
+
.selectFrom("automation_runs")
|
|
201
|
+
.select("automation_id")
|
|
194
202
|
.where("id", "=", taskRunId)
|
|
195
203
|
.executeTakeFirst();
|
|
196
204
|
if (!owner)
|
|
197
205
|
return;
|
|
198
|
-
await lockTaskRow(t, owner.
|
|
206
|
+
await lockTaskRow(t, owner.automation_id);
|
|
199
207
|
const run = await t
|
|
200
|
-
.selectFrom("
|
|
208
|
+
.selectFrom("automation_runs")
|
|
201
209
|
.select(["status", "fired_at", "manual"])
|
|
202
210
|
.where("id", "=", taskRunId)
|
|
203
211
|
.forUpdate()
|
|
204
212
|
.executeTakeFirst();
|
|
205
213
|
if (run?.status !== "pending" || run.fired_at !== null)
|
|
206
214
|
return;
|
|
207
|
-
await this.skipIn(t, taskRunId, owner.
|
|
215
|
+
await this.skipIn(t, taskRunId, owner.automation_id, run.manual ? "removed" : "missed");
|
|
208
216
|
}
|
|
209
217
|
/** Step 1 (see the class): a skip, the plan, or null when there is nothing to fire. */
|
|
210
218
|
async claim(taskRunId, orgId, deviceOffline) {
|
|
211
219
|
const t = this.db;
|
|
212
|
-
// the org row first, as every task write does (
|
|
220
|
+
// the org row first, as every task write does (AutomationsService): no lock cycle
|
|
213
221
|
if ((await this.orgLock.tryLock(orgId)) === undefined)
|
|
214
222
|
return null;
|
|
215
223
|
const owner = await t
|
|
216
|
-
.selectFrom("
|
|
217
|
-
.select("
|
|
224
|
+
.selectFrom("automation_runs")
|
|
225
|
+
.select("automation_id")
|
|
218
226
|
.where("id", "=", taskRunId)
|
|
219
227
|
.executeTakeFirst();
|
|
220
228
|
if (!owner)
|
|
221
229
|
return null;
|
|
222
230
|
// lock order org → task row → task runs, as the run tracker and the service take them
|
|
223
|
-
await lockTaskRow(t, owner.
|
|
231
|
+
await lockTaskRow(t, owner.automation_id);
|
|
224
232
|
const row = await t
|
|
225
|
-
.selectFrom("
|
|
233
|
+
.selectFrom("automation_runs")
|
|
226
234
|
.select("id")
|
|
227
235
|
.where("id", "=", taskRunId)
|
|
228
236
|
.forUpdate()
|
|
@@ -247,18 +255,22 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
247
255
|
// paused after the scan recorded the tick: only a person's "run now" still runs
|
|
248
256
|
if (!resumed && !run.manual && task.status === "paused")
|
|
249
257
|
return skip("task_paused");
|
|
250
|
-
const settings = await
|
|
258
|
+
const settings = await readAutomationSettings(t, this.config, orgId);
|
|
251
259
|
// a tick whose fire comes this late (workers down for long) is missed, as the scan records a
|
|
252
260
|
// tick it finds outside the window; a person's "run now" still runs
|
|
253
|
-
const lateAfter = settings.catchUpWindowMinutes * 60_000 +
|
|
254
|
-
|
|
261
|
+
const lateAfter = settings.catchUpWindowMinutes * 60_000 + AUTOMATION_LATE_FIRE_SLACK_MS;
|
|
262
|
+
// an event run has no tick and is never missed (collaboration spec §4.1)
|
|
263
|
+
if (!resumed &&
|
|
264
|
+
!run.manual &&
|
|
265
|
+
run.scheduledFor !== null &&
|
|
266
|
+
Date.now() - run.scheduledFor.getTime() > lateAfter) {
|
|
255
267
|
return skip("missed");
|
|
256
268
|
}
|
|
257
269
|
if (!resumed && !settings.enabled)
|
|
258
270
|
return skip("org_disabled");
|
|
259
271
|
// an unattended run must not run with nothing forbidden (a trial run is attended)
|
|
260
272
|
if (!resumed && !run.trial && settings.forbiddenToolsUnreadable) {
|
|
261
|
-
this.log.warn("
|
|
273
|
+
this.log.warn("automation run failed: the org's forbidden tool list is unreadable", {
|
|
262
274
|
orgId,
|
|
263
275
|
taskId: task.id,
|
|
264
276
|
taskRunId: run.id,
|
|
@@ -279,9 +291,28 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
279
291
|
throw e;
|
|
280
292
|
}
|
|
281
293
|
}
|
|
294
|
+
let event;
|
|
295
|
+
if (task.trigger.kind === "event") {
|
|
296
|
+
event = await this.eventMarkerOf(task, run.id, task.trigger.source);
|
|
297
|
+
if (event === undefined) {
|
|
298
|
+
// the coalescer links a run's deliveries in the transaction that inserts it
|
|
299
|
+
this.log.warn("event automation run has no events", { taskId: task.id, taskRunId: run.id });
|
|
300
|
+
await this.tracker.failStartIn(run.id, "The run's events are gone");
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
282
304
|
let plan;
|
|
283
305
|
let queuePausedOn;
|
|
284
|
-
if (task.mode === "
|
|
306
|
+
if (task.mode === "subject") {
|
|
307
|
+
const first = event?.events[0];
|
|
308
|
+
if (first === undefined)
|
|
309
|
+
return skip("unsupported_target");
|
|
310
|
+
plan = { mode: "subject", task, run, subject: first.subject };
|
|
311
|
+
const mapped = await this.subjectConversation(task, first.subject);
|
|
312
|
+
if (mapped?.queuePaused)
|
|
313
|
+
queuePausedOn = mapped.id;
|
|
314
|
+
}
|
|
315
|
+
else if (task.mode === "conversation") {
|
|
285
316
|
const conv = await this.targetConversation(task);
|
|
286
317
|
if (conv === undefined)
|
|
287
318
|
return skip("conversation_gone");
|
|
@@ -333,11 +364,17 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
333
364
|
else {
|
|
334
365
|
plan = { mode: "new", task, run };
|
|
335
366
|
}
|
|
367
|
+
if (event !== undefined)
|
|
368
|
+
plan = { ...plan, event };
|
|
336
369
|
if (resumed)
|
|
337
370
|
return { plan };
|
|
338
|
-
|
|
371
|
+
// an event run never waits for another run of its automation (collaboration spec §4.7): its
|
|
372
|
+
// events would be lost; `new` gives each run its own conversation, and `conversation` and
|
|
373
|
+
// `subject` queue it into the conversation, whose queue serializes the runs
|
|
374
|
+
if (event === undefined && (await this.overlaps(task.id, run.id)))
|
|
339
375
|
return skip("overlap");
|
|
340
|
-
|
|
376
|
+
// the owner's runs across their automations (collaboration spec §4.7: per person)
|
|
377
|
+
const fired = await this.store.countFiredSince(t, orgId, task.accountId, new Date(Date.now() - HOUR_MS));
|
|
341
378
|
if (fired >= settings.maxRunsPerHour)
|
|
342
379
|
return skip("rate_limited");
|
|
343
380
|
if (deviceOffline)
|
|
@@ -349,25 +386,37 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
349
386
|
...(plan.mode === "conversation" && { conversationId: plan.conversationId }),
|
|
350
387
|
});
|
|
351
388
|
// a start that fails for good leaves the run claimed: its settle check releases or ends it
|
|
352
|
-
await
|
|
389
|
+
await oweAutomationSettle(this.outbox, run.id, orgId, 0, AUTOMATION_SETTLE_AFTER_MS);
|
|
353
390
|
return { plan: { ...plan, run: claimed ?? run } };
|
|
354
391
|
}
|
|
355
|
-
/**
|
|
356
|
-
|
|
392
|
+
/** The marker a run's message carries: the event marker, or the schedule marker and zone. */
|
|
393
|
+
markerOf(plan) {
|
|
357
394
|
const { task, run } = plan;
|
|
395
|
+
if (plan.event !== undefined)
|
|
396
|
+
return { automationEvent: plan.event };
|
|
397
|
+
const trigger = task.trigger;
|
|
398
|
+
if (trigger.kind === "event" || run.scheduledFor === null) {
|
|
399
|
+
throw new Error(`automation run ${run.id}: a time run without a tick`);
|
|
400
|
+
}
|
|
358
401
|
const marker = {
|
|
359
402
|
taskId: task.id,
|
|
360
403
|
taskRunId: run.id,
|
|
361
404
|
scheduledFor: run.scheduledFor.toISOString(),
|
|
362
405
|
trial: run.trial,
|
|
363
406
|
taskName: task.name,
|
|
364
|
-
localTime: localTimeOf(run.scheduledFor,
|
|
407
|
+
localTime: localTimeOf(run.scheduledFor, trigger.timeZone),
|
|
365
408
|
};
|
|
366
|
-
|
|
409
|
+
return { schedule: marker, timeZoneOverride: trigger.timeZone };
|
|
410
|
+
}
|
|
411
|
+
/** Step 2 (see the class). */
|
|
412
|
+
async start(plan) {
|
|
413
|
+
const { task, run } = plan;
|
|
414
|
+
const internal = this.markerOf(plan);
|
|
367
415
|
try {
|
|
368
416
|
if (plan.mode === "new") {
|
|
369
|
-
// model and effort are a new-conversation task's only (spec §4
|
|
370
|
-
// task runs with that conversation's selection, never
|
|
417
|
+
// model and effort are a new-conversation or subject task's only (spec §4, collaboration
|
|
418
|
+
// §4.5): a conversation or chat task runs with that conversation's selection, never
|
|
419
|
+
// switching it for a person
|
|
371
420
|
const pick = {
|
|
372
421
|
...(task.model !== null && { model: task.model }),
|
|
373
422
|
...(task.effort !== null && { effort: task.effort }),
|
|
@@ -379,16 +428,31 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
379
428
|
return;
|
|
380
429
|
}
|
|
381
430
|
let conversationId;
|
|
431
|
+
// a subject conversation with no message yet takes the task's model and effort with its
|
|
432
|
+
// first message, as a new-conversation task's does; a later run switches nothing
|
|
433
|
+
let pick = {};
|
|
382
434
|
if (plan.mode === "chat" || plan.mode === "group") {
|
|
383
435
|
const bound = plan.mode === "chat" ? await this.bindChat(plan) : await this.openGroup(plan);
|
|
384
436
|
if (bound === null)
|
|
385
437
|
return;
|
|
386
438
|
conversationId = bound;
|
|
387
439
|
}
|
|
440
|
+
else if (plan.mode === "subject") {
|
|
441
|
+
const bound = await this.bindSubject(plan);
|
|
442
|
+
if (bound === null)
|
|
443
|
+
return;
|
|
444
|
+
conversationId = bound.conversationId;
|
|
445
|
+
if (bound.fresh) {
|
|
446
|
+
pick = {
|
|
447
|
+
...(task.model !== null && { model: task.model }),
|
|
448
|
+
...(task.effort !== null && { effort: task.effort }),
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
}
|
|
388
452
|
else {
|
|
389
453
|
conversationId = plan.conversationId;
|
|
390
454
|
}
|
|
391
|
-
await this.queue.add(task.orgId, conversationId, task.accountId, { text: task.prompt, clientMessageId:
|
|
455
|
+
await this.queue.add(task.orgId, conversationId, task.accountId, { text: task.prompt, clientMessageId: automationClientMessageId(run.id), ...pick }, internal);
|
|
392
456
|
}
|
|
393
457
|
catch (e) {
|
|
394
458
|
if (!(e instanceof ApiError))
|
|
@@ -396,7 +460,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
396
460
|
const reason = e.code === "agent.not_found"
|
|
397
461
|
? "agent_gone"
|
|
398
462
|
: e.code === "conversation.not_found"
|
|
399
|
-
? plan.mode === "chat" || plan.mode === "group"
|
|
463
|
+
? plan.mode === "chat" || plan.mode === "group" || plan.mode === "subject"
|
|
400
464
|
? "target_gone"
|
|
401
465
|
: "conversation_gone"
|
|
402
466
|
: DEVICE_GONE.has(e.code)
|
|
@@ -406,7 +470,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
406
470
|
await this.unstart(run.id, task.id, reason);
|
|
407
471
|
return;
|
|
408
472
|
}
|
|
409
|
-
this.log.warn("
|
|
473
|
+
this.log.warn("automation run could not start", {
|
|
410
474
|
taskId: task.id,
|
|
411
475
|
taskRunId: run.id,
|
|
412
476
|
code: e.code,
|
|
@@ -416,7 +480,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
416
480
|
}
|
|
417
481
|
/**
|
|
418
482
|
* The DM's live conversation, or a new one bound to it (`findOrBindThread`), in one transaction:
|
|
419
|
-
* a conversation it creates is the task's (`conversations.
|
|
483
|
+
* a conversation it creates is the task's (`conversations.automation_id`). Null when the
|
|
420
484
|
* live conversation's queue is paused (the run is skipped, `queue_paused`).
|
|
421
485
|
*/
|
|
422
486
|
async bindChat(plan) {
|
|
@@ -425,7 +489,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
425
489
|
// the one lock order: task row, then the task run
|
|
426
490
|
await lockTaskRow(t, task.id);
|
|
427
491
|
const locked = await t
|
|
428
|
-
.selectFrom("
|
|
492
|
+
.selectFrom("automation_runs")
|
|
429
493
|
.select("status")
|
|
430
494
|
.where("id", "=", run.id)
|
|
431
495
|
.forUpdate()
|
|
@@ -438,7 +502,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
438
502
|
{ im: true }, { source: channelSource(plan.target.chatType) });
|
|
439
503
|
await t
|
|
440
504
|
.updateTable("conversations")
|
|
441
|
-
.set({
|
|
505
|
+
.set({ automation_id: task.id })
|
|
442
506
|
.where("id", "=", conv.id)
|
|
443
507
|
.where("org_id", "=", task.orgId)
|
|
444
508
|
.execute();
|
|
@@ -453,7 +517,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
453
517
|
}
|
|
454
518
|
/**
|
|
455
519
|
* A group run's conversation (spec §8): a new one per run, the task's
|
|
456
|
-
* (`conversations.
|
|
520
|
+
* (`conversations.automation_id`), whose delivery goes to the group with its thread still to
|
|
457
521
|
* bind (`channel_deliveries.pending_thread_account_id` = the creator): outbound binds it at the
|
|
458
522
|
* run's first post, thread key = that message's id, so replies in that thread continue the
|
|
459
523
|
* conversation. One transaction; a retried start reuses the conversation it made before.
|
|
@@ -464,7 +528,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
464
528
|
// the one lock order: task row, then the task run
|
|
465
529
|
await lockTaskRow(t, task.id);
|
|
466
530
|
const locked = await t
|
|
467
|
-
.selectFrom("
|
|
531
|
+
.selectFrom("automation_runs")
|
|
468
532
|
.select(["status", "conversation_id"])
|
|
469
533
|
.where("id", "=", run.id)
|
|
470
534
|
.forUpdate()
|
|
@@ -481,7 +545,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
481
545
|
{ source: channelSource(target.chatType) });
|
|
482
546
|
await t
|
|
483
547
|
.updateTable("conversations")
|
|
484
|
-
.set({
|
|
548
|
+
.set({ automation_id: task.id })
|
|
485
549
|
.where("id", "=", conv.id)
|
|
486
550
|
.where("org_id", "=", task.orgId)
|
|
487
551
|
.execute();
|
|
@@ -498,6 +562,171 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
498
562
|
await this.store.updateRun(t, run.id, { conversationId: conv.id });
|
|
499
563
|
return conv.id;
|
|
500
564
|
}
|
|
565
|
+
/**
|
|
566
|
+
* A subject run's conversation (collaboration spec §4.5), in one transaction under the task row
|
|
567
|
+
* lock (so two runs of one subject never create two): the conversation `automation_conversations`
|
|
568
|
+
* maps the subject to, when it is still live, the owner's chat with the task's agent and
|
|
569
|
+
* accessible; else a new one, the task's (`conversations.automation_id`) with
|
|
570
|
+
* `conversations.subject` set, whose mapping replaces the stale one (the mapping has no foreign
|
|
571
|
+
* key: a deleted conversation leaves it behind). Null when the run is no longer pending or the
|
|
572
|
+
* mapped conversation's queue is paused (the run is skipped, `queue_paused`). A retried start
|
|
573
|
+
* reuses the conversation the run already recorded. `fresh`: the conversation has no message
|
|
574
|
+
* yet, logged or queued by another run (just created, or created by a start that stopped
|
|
575
|
+
* before its message), so this run's message carries the task's model and effort.
|
|
576
|
+
*/
|
|
577
|
+
async bindSubject(plan) {
|
|
578
|
+
const { task, run, subject } = plan;
|
|
579
|
+
const t = this.db;
|
|
580
|
+
// the one lock order: task row, then the task run
|
|
581
|
+
await lockTaskRow(t, task.id);
|
|
582
|
+
const locked = await t
|
|
583
|
+
.selectFrom("automation_runs")
|
|
584
|
+
.select(["status", "conversation_id"])
|
|
585
|
+
.where("id", "=", run.id)
|
|
586
|
+
.forUpdate()
|
|
587
|
+
.executeTakeFirst();
|
|
588
|
+
if (locked?.status !== "pending")
|
|
589
|
+
return null;
|
|
590
|
+
if (locked.conversation_id !== null) {
|
|
591
|
+
const id = locked.conversation_id;
|
|
592
|
+
return { conversationId: id, fresh: await this.noMessageYet(id, run.id) };
|
|
593
|
+
}
|
|
594
|
+
const mapped = await this.subjectConversation(task, subject);
|
|
595
|
+
if (mapped !== undefined) {
|
|
596
|
+
if (mapped.queuePaused) {
|
|
597
|
+
await this.skipIn(t, run.id, task.id, "queue_paused");
|
|
598
|
+
return null;
|
|
599
|
+
}
|
|
600
|
+
await this.store.updateRun(t, run.id, { conversationId: mapped.id });
|
|
601
|
+
return { conversationId: mapped.id, fresh: await this.noMessageYet(mapped.id, run.id) };
|
|
602
|
+
}
|
|
603
|
+
const trigger = task.trigger;
|
|
604
|
+
if (trigger.kind !== "event")
|
|
605
|
+
throw new Error(`automation ${task.id}: subject mode without events`);
|
|
606
|
+
const conv = await this.conversations.create(task.orgId, task.accountId, task.agentId,
|
|
607
|
+
// a subject task takes no device (new mode only): the default pick for new conversations
|
|
608
|
+
{}, {
|
|
609
|
+
source: "web",
|
|
610
|
+
subject: {
|
|
611
|
+
source: trigger.source,
|
|
612
|
+
kind: subject.kind,
|
|
613
|
+
id: subject.id,
|
|
614
|
+
title: subject.title,
|
|
615
|
+
},
|
|
616
|
+
});
|
|
617
|
+
await t
|
|
618
|
+
.updateTable("conversations")
|
|
619
|
+
.set({ automation_id: task.id })
|
|
620
|
+
.where("id", "=", conv.id)
|
|
621
|
+
.where("org_id", "=", task.orgId)
|
|
622
|
+
.execute();
|
|
623
|
+
await t
|
|
624
|
+
.insertInto("automation_conversations")
|
|
625
|
+
.values({
|
|
626
|
+
org_id: task.orgId,
|
|
627
|
+
automation_id: task.id,
|
|
628
|
+
subject_kind: subject.kind,
|
|
629
|
+
subject_id: subject.id,
|
|
630
|
+
conversation_id: conv.id,
|
|
631
|
+
})
|
|
632
|
+
.onConflict((oc) => oc
|
|
633
|
+
.columns(["automation_id", "subject_kind", "subject_id"])
|
|
634
|
+
.doUpdateSet({ conversation_id: conv.id, created_at: sql `now()` }))
|
|
635
|
+
.execute();
|
|
636
|
+
await this.store.updateRun(t, run.id, { conversationId: conv.id });
|
|
637
|
+
return { conversationId: conv.id, fresh: true };
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Conversation `conversationId` has no event yet (`last_seq` 0) and no queued message other
|
|
641
|
+
* than task run `runId`'s own (see {@link bindSubject}).
|
|
642
|
+
*/
|
|
643
|
+
async noMessageYet(conversationId, runId) {
|
|
644
|
+
const conv = await this.db
|
|
645
|
+
.selectFrom("conversations")
|
|
646
|
+
.select("last_seq")
|
|
647
|
+
.where("id", "=", conversationId)
|
|
648
|
+
.executeTakeFirst();
|
|
649
|
+
if (conv === undefined || Number(conv.last_seq) !== 0)
|
|
650
|
+
return false;
|
|
651
|
+
const queued = await this.db
|
|
652
|
+
.selectFrom("conversation_queue")
|
|
653
|
+
.select("id")
|
|
654
|
+
.where("conversation_id", "=", conversationId)
|
|
655
|
+
.where("client_message_id", "!=", automationClientMessageId(runId))
|
|
656
|
+
.limit(1)
|
|
657
|
+
.executeTakeFirst();
|
|
658
|
+
return queued === undefined;
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* The live conversation `automation_conversations` maps a subject of the task to (see
|
|
662
|
+
* {@link bindSubject}), or undefined: no mapping, or its conversation is gone, deleted, no
|
|
663
|
+
* longer the owner's chat with the task's agent, or not accessible.
|
|
664
|
+
*/
|
|
665
|
+
async subjectConversation(task, subject) {
|
|
666
|
+
const conv = await this.db
|
|
667
|
+
.selectFrom("automation_conversations as m")
|
|
668
|
+
.innerJoin("conversations as c", "c.id", "m.conversation_id")
|
|
669
|
+
.select(["c.id", "c.queue_paused"])
|
|
670
|
+
.where("m.org_id", "=", task.orgId)
|
|
671
|
+
.where("m.automation_id", "=", task.id)
|
|
672
|
+
.where("m.subject_kind", "=", subject.kind)
|
|
673
|
+
.where("m.subject_id", "=", subject.id)
|
|
674
|
+
.where("c.org_id", "=", task.orgId)
|
|
675
|
+
.where("c.account_id", "=", task.accountId)
|
|
676
|
+
.where("c.agent_id", "=", task.agentId)
|
|
677
|
+
.where("c.kind", "=", "chat")
|
|
678
|
+
.where("c.deleted_at", "is", null)
|
|
679
|
+
.executeTakeFirst();
|
|
680
|
+
if (!conv)
|
|
681
|
+
return undefined;
|
|
682
|
+
if (!(await this.access.isAccessible(task.orgId, conv.id, task.accountId)))
|
|
683
|
+
return undefined;
|
|
684
|
+
return { id: conv.id, queuePaused: conv.queue_paused };
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* An event run's marker (collaboration spec §4.6): the events its deliveries name, in the order
|
|
688
|
+
* they were stored (the coalescer's order), each with its type, subject and summary; past
|
|
689
|
+
* {@link AUTOMATION_EVENT_HEAD} + {@link AUTOMATION_EVENT_TAIL} events only the first and the
|
|
690
|
+
* latest, the rest counted (`omittedEvents`; every delivery stays bound to the run); undefined
|
|
691
|
+
* when it has none.
|
|
692
|
+
*/
|
|
693
|
+
async eventMarkerOf(task, runId, source) {
|
|
694
|
+
const rows = await this.db
|
|
695
|
+
.selectFrom("automation_event_deliveries as d")
|
|
696
|
+
.innerJoin("trigger_events as e", "e.id", "d.event_id")
|
|
697
|
+
.select(["e.id", "e.type", "e.subject", "e.summary"])
|
|
698
|
+
.where("d.org_id", "=", task.orgId)
|
|
699
|
+
.where("d.automation_id", "=", task.id)
|
|
700
|
+
.where("d.automation_run_id", "=", runId)
|
|
701
|
+
.orderBy("e.created_at")
|
|
702
|
+
.orderBy("e.id")
|
|
703
|
+
.execute();
|
|
704
|
+
if (rows.length === 0)
|
|
705
|
+
return undefined;
|
|
706
|
+
const cap = AUTOMATION_EVENT_HEAD + AUTOMATION_EVENT_TAIL;
|
|
707
|
+
const omitted = Math.max(0, rows.length - cap);
|
|
708
|
+
const shown = omitted === 0
|
|
709
|
+
? rows
|
|
710
|
+
: [...rows.slice(0, AUTOMATION_EVENT_HEAD), ...rows.slice(-AUTOMATION_EVENT_TAIL)];
|
|
711
|
+
return {
|
|
712
|
+
automationId: task.id,
|
|
713
|
+
automationRunId: runId,
|
|
714
|
+
automationName: task.name,
|
|
715
|
+
source,
|
|
716
|
+
...(omitted > 0 && { omittedEvents: omitted }),
|
|
717
|
+
events: shown.map((r) => ({
|
|
718
|
+
id: r.id,
|
|
719
|
+
type: r.type,
|
|
720
|
+
subject: {
|
|
721
|
+
kind: r.subject.kind,
|
|
722
|
+
id: r.subject.id,
|
|
723
|
+
title: r.subject.title,
|
|
724
|
+
...(r.subject.url !== undefined && { url: r.subject.url }),
|
|
725
|
+
},
|
|
726
|
+
summary: r.summary,
|
|
727
|
+
})),
|
|
728
|
+
};
|
|
729
|
+
}
|
|
501
730
|
/** The start found the target gone: `fired_at` back to null, skipped with `reason`. */
|
|
502
731
|
async unstart(runId, taskId, reason) {
|
|
503
732
|
// the one lock order: task row (paused for a lasting reason), then the task run
|
|
@@ -515,7 +744,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
515
744
|
}
|
|
516
745
|
async setConversation(runId, conversationId) {
|
|
517
746
|
await this.db
|
|
518
|
-
.updateTable("
|
|
747
|
+
.updateTable("automation_runs")
|
|
519
748
|
.set({ conversation_id: conversationId })
|
|
520
749
|
.where("id", "=", runId)
|
|
521
750
|
.where("conversation_id", "is", null)
|
|
@@ -524,7 +753,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
524
753
|
/** Marks a pending run skipped (never fired) and pauses the task for a lasting reason. */
|
|
525
754
|
async skipIn(t, runId, taskId, reason) {
|
|
526
755
|
const r = await t
|
|
527
|
-
.updateTable("
|
|
756
|
+
.updateTable("automation_runs")
|
|
528
757
|
.set({ status: "skipped", skip_reason: reason, fired_at: null })
|
|
529
758
|
.where("id", "=", runId)
|
|
530
759
|
.where("status", "=", "pending")
|
|
@@ -533,7 +762,7 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
533
762
|
return;
|
|
534
763
|
if (LASTING.has(reason))
|
|
535
764
|
await pauseTaskIn(t, taskId, reason, null);
|
|
536
|
-
this.log.info("
|
|
765
|
+
this.log.info("automation run skipped", { taskId, taskRunId: runId, reason });
|
|
537
766
|
}
|
|
538
767
|
/** Another run of the task is still in flight (stale ones are settled on the way). */
|
|
539
768
|
async overlaps(taskId, runId) {
|
|
@@ -660,35 +889,38 @@ let ScheduledTaskFirer = class ScheduledTaskFirer {
|
|
|
660
889
|
};
|
|
661
890
|
__decorate([
|
|
662
891
|
Transactional()
|
|
663
|
-
],
|
|
892
|
+
], AutomationFirer.prototype, "settle", null);
|
|
893
|
+
__decorate([
|
|
894
|
+
Transactional()
|
|
895
|
+
], AutomationFirer.prototype, "claim", null);
|
|
664
896
|
__decorate([
|
|
665
897
|
Transactional()
|
|
666
|
-
],
|
|
898
|
+
], AutomationFirer.prototype, "bindChat", null);
|
|
667
899
|
__decorate([
|
|
668
900
|
Transactional()
|
|
669
|
-
],
|
|
901
|
+
], AutomationFirer.prototype, "openGroup", null);
|
|
670
902
|
__decorate([
|
|
671
903
|
Transactional()
|
|
672
|
-
],
|
|
904
|
+
], AutomationFirer.prototype, "bindSubject", null);
|
|
673
905
|
__decorate([
|
|
674
906
|
Transactional()
|
|
675
|
-
],
|
|
907
|
+
], AutomationFirer.prototype, "unstart", null);
|
|
676
908
|
__decorate([
|
|
677
909
|
Transactional()
|
|
678
|
-
],
|
|
910
|
+
], AutomationFirer.prototype, "failStart", null);
|
|
679
911
|
__decorate([
|
|
680
912
|
Transactional()
|
|
681
|
-
],
|
|
682
|
-
|
|
913
|
+
], AutomationFirer.prototype, "setConversation", null);
|
|
914
|
+
AutomationFirer = __decorate([
|
|
683
915
|
Injectable()
|
|
684
|
-
// org-filter: the
|
|
916
|
+
// org-filter: the automation-fire / automation-settle job handler (worker only): the task run id is the
|
|
685
917
|
// kernel-written job payload, and every other id is read from that run's own task row
|
|
686
918
|
,
|
|
687
919
|
__param(0, Inject(KernelDb)),
|
|
688
920
|
__param(1, Inject(TransactionHost)),
|
|
689
921
|
__param(2, Inject(KernelConfigService)),
|
|
690
|
-
__param(3, Inject(
|
|
691
|
-
__param(4, Inject(
|
|
922
|
+
__param(3, Inject(AutomationStore)),
|
|
923
|
+
__param(4, Inject(AutomationRunTracker)),
|
|
692
924
|
__param(5, Inject(OrgLock)),
|
|
693
925
|
__param(6, Inject(AgentLookup)),
|
|
694
926
|
__param(7, Inject(ConversationAccess)),
|
|
@@ -699,15 +931,15 @@ ScheduledTaskFirer = __decorate([
|
|
|
699
931
|
__param(12, Inject(DeviceProviders)),
|
|
700
932
|
__param(13, Inject(KERNEL_LOGGER)),
|
|
701
933
|
__param(14, Inject(OutboxService))
|
|
702
|
-
],
|
|
703
|
-
export {
|
|
934
|
+
], AutomationFirer);
|
|
935
|
+
export { AutomationFirer };
|
|
704
936
|
/**
|
|
705
937
|
* The fire job and the scanner, worker only: imported by `MaintenanceModule` (which dispatches
|
|
706
|
-
* `
|
|
938
|
+
* `automation-fire`) and `WorkerModule` (the scan's `@LockedCron`), never by another role.
|
|
707
939
|
*/
|
|
708
|
-
let
|
|
940
|
+
let AutomationFireModule = class AutomationFireModule {
|
|
709
941
|
};
|
|
710
|
-
|
|
942
|
+
AutomationFireModule = __decorate([
|
|
711
943
|
Module({
|
|
712
944
|
imports: [
|
|
713
945
|
ConversationsModule,
|
|
@@ -716,13 +948,13 @@ ScheduledTaskFireModule = __decorate([
|
|
|
716
948
|
AgentLookupModule,
|
|
717
949
|
ConversationAccessModule,
|
|
718
950
|
OrgLockModule,
|
|
719
|
-
|
|
720
|
-
|
|
951
|
+
AutomationStoreModule,
|
|
952
|
+
AutomationTrackerModule,
|
|
721
953
|
DialInRelayModule,
|
|
722
954
|
SandboxProviderRegistryModule,
|
|
723
955
|
],
|
|
724
|
-
providers: [
|
|
725
|
-
exports: [
|
|
956
|
+
providers: [AutomationFirer, AutomationScanJob],
|
|
957
|
+
exports: [AutomationFirer],
|
|
726
958
|
})
|
|
727
|
-
],
|
|
728
|
-
export {
|
|
959
|
+
], AutomationFireModule);
|
|
960
|
+
export { AutomationFireModule };
|