@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
|
@@ -2,7 +2,7 @@ import { type SandboxLimits } from "@capybara-hq/plugin-sdk";
|
|
|
2
2
|
import { type OrgRole } from "../orgs/roles.js";
|
|
3
3
|
import type { ResolvedTool } from "../tools/registry.js";
|
|
4
4
|
/**
|
|
5
|
-
* The one rule for what can be
|
|
5
|
+
* The one rule for what can be an automation's grant (spec §7): the task form and
|
|
6
6
|
* `addGrants`, a trial run's kept approvals and the unattended policy's `missing_grants` all ask
|
|
7
7
|
* it. A plain module (no provider): the tool policy imports it.
|
|
8
8
|
*/
|
|
@@ -26,7 +26,7 @@ export function grantRefusalOf(toolKey, tool, ctx) {
|
|
|
26
26
|
if (changesAgentSkills(toolKey)) {
|
|
27
27
|
return {
|
|
28
28
|
code: "not_grantable",
|
|
29
|
-
message: `${toolKey} cannot be approved for
|
|
29
|
+
message: `${toolKey} cannot be approved for an automation: an unattended run never changes the agent's skills`,
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
32
|
if (!tool) {
|
|
@@ -35,7 +35,7 @@ export function grantRefusalOf(toolKey, tool, ctx) {
|
|
|
35
35
|
if (toolKey !== tool.key) {
|
|
36
36
|
return {
|
|
37
37
|
code: "not_grantable",
|
|
38
|
-
message: `${toolKey} is decided per call and cannot be approved in advance for
|
|
38
|
+
message: `${toolKey} is decided per call and cannot be approved in advance for an automation`,
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
41
|
if (tool.requiredRole !== undefined && !roleAtLeast(ctx.role, tool.requiredRole)) {
|
|
@@ -74,7 +74,7 @@ export function networkGrantRefusal(host, ctx) {
|
|
|
74
74
|
if (host.startsWith("*.") || validateAllowHost(host).length > 0) {
|
|
75
75
|
return {
|
|
76
76
|
code: "not_grantable",
|
|
77
|
-
message: `${host} cannot be approved for
|
|
77
|
+
message: `${host} cannot be approved for an automation: give one exact domain name (no wildcard, no IP address or local name)`,
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
if (!orgAllows(host, ctx.limits)) {
|
|
@@ -137,7 +137,7 @@ export function gitGrantRefusal(key, ctx) {
|
|
|
137
137
|
if (normalizeGitRepo(repo) !== repo) {
|
|
138
138
|
return {
|
|
139
139
|
code: "not_grantable",
|
|
140
|
-
message: `${key} cannot be approved for
|
|
140
|
+
message: `${key} cannot be approved for an automation: name one repository as git:push:<owner>/<repo>`,
|
|
141
141
|
};
|
|
142
142
|
}
|
|
143
143
|
return null;
|
|
@@ -157,7 +157,7 @@ export function tierGrantRefusal(key) {
|
|
|
157
157
|
return null;
|
|
158
158
|
return {
|
|
159
159
|
code: "not_grantable",
|
|
160
|
-
message: `${key} cannot be approved for
|
|
160
|
+
message: `${key} cannot be approved for an automation: sandbox levels are no longer task grants`,
|
|
161
161
|
};
|
|
162
162
|
}
|
|
163
163
|
/**
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
2
|
+
/** The automations' jobs (a leaf module: type imports only). */
|
|
3
|
+
/**
|
|
4
|
+
* The once-a-minute `@LockedCron` that turns due tasks into task runs (spec §6); its name enqueued
|
|
5
|
+
* on the maintenance queue runs one scan (`ScheduledRuns.runNow`).
|
|
6
|
+
*/
|
|
7
|
+
export declare const AUTOMATION_SCAN_JOB = "automation-scan";
|
|
8
|
+
/**
|
|
9
|
+
* The `@LockedCron` that deletes old trigger events (`automations.eventRetentionDays`) and owes
|
|
10
|
+
* the coalesce job of a window whose own job was lost (collaboration spec §4.7).
|
|
11
|
+
*/
|
|
12
|
+
export declare const TRIGGER_EVENT_SWEEP_JOB = "trigger-event-sweep";
|
|
13
|
+
/** The one-off job that checks and starts one task run, on `cap-kernel-maintenance`. */
|
|
14
|
+
export declare const AUTOMATION_FIRE_JOB = "automation-fire";
|
|
15
|
+
/** The data of a {@link AUTOMATION_FIRE_JOB}. */
|
|
16
|
+
export interface AutomationFireJobData {
|
|
17
|
+
taskRunId: string;
|
|
18
|
+
}
|
|
19
|
+
/** The fire job's id: `automation-fire-<taskRunId>` (no `:`, task run ids are UUIDs). */
|
|
20
|
+
export declare const automationFireJobId: (taskRunId: string) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Owes the fire job of task run `taskRunId` in the transaction in context (transactional outbox
|
|
23
|
+
* spec §8.1): the transaction that writes a pending task run (the scan's tick, a person's "run
|
|
24
|
+
* now") owes its fire, so a tick is never left unfired. The job's options are its outbox registry
|
|
25
|
+
* entry's; the fire is idempotent on the task run's status.
|
|
26
|
+
*/
|
|
27
|
+
export declare function oweAutomationFire(outbox: Pick<OutboxService, "add">, taskRunId: string, orgId: string): Promise<boolean>;
|
|
28
|
+
/**
|
|
29
|
+
* The delayed job that settles a task run left pending (ruling R26, fix round 1), on
|
|
30
|
+
* `cap-kernel-maintenance`. Two kinds:
|
|
31
|
+
* - the claimed check, owed by the fire job's claim: the run tracker links, settles or releases a
|
|
32
|
+
* run claimed but never started (`automation-settle-<taskRunId>`, then `-<check>`);
|
|
33
|
+
* - the unfired check, owed with the task run itself (the scan's tick, "run now"): a run its fire
|
|
34
|
+
* job never claimed (the job failed for good before its claim) is skipped, `missed` for a tick,
|
|
35
|
+
* `removed` for a "run now", so it never blocks the task's later ticks as an overlap
|
|
36
|
+
* (`automation-settle-<taskRunId>-unfired`).
|
|
37
|
+
*/
|
|
38
|
+
export declare const AUTOMATION_SETTLE_JOB = "automation-settle";
|
|
39
|
+
/**
|
|
40
|
+
* The data of a {@link AUTOMATION_SETTLE_JOB}: the claimed check (`check` counts the checks before this
|
|
41
|
+
* one) or the unfired check.
|
|
42
|
+
*/
|
|
43
|
+
export type AutomationSettleJobData = {
|
|
44
|
+
taskRunId: string;
|
|
45
|
+
check: number;
|
|
46
|
+
} | {
|
|
47
|
+
taskRunId: string;
|
|
48
|
+
unfired: true;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The first claimed check: 10 minutes after the claim, when the start's retries (seconds) are long
|
|
52
|
+
* over and the run tracker's start grace (`START_GRACE_MS`, 5 minutes) has passed, so a run that
|
|
53
|
+
* left no trace is released at once. Also the unfired check's delay after a "run now".
|
|
54
|
+
*/
|
|
55
|
+
export declare const AUTOMATION_SETTLE_AFTER_MS: number;
|
|
56
|
+
/**
|
|
57
|
+
* A claimed run still in flight at a check (its item queued, its message posted and not yet taken
|
|
58
|
+
* by a run) is checked again hourly. The chain ends with the first check that finds the run
|
|
59
|
+
* started, ended or released: the queued item is consumed or removed, the message taken by a run.
|
|
60
|
+
* A chain past {@link AUTOMATION_SETTLE_LONG_CHECKS} checks (a week) is logged as a warning, once.
|
|
61
|
+
*/
|
|
62
|
+
export declare const AUTOMATION_SETTLE_RECHECK_MS: number;
|
|
63
|
+
/** A week of hourly checks: the claimed check warns at this one (a run in flight that long). */
|
|
64
|
+
export declare const AUTOMATION_SETTLE_LONG_CHECKS = 168;
|
|
65
|
+
/**
|
|
66
|
+
* How long past the org's catch-up window a tick's fire may come before the tick is missed (the
|
|
67
|
+
* fire job's own rule, and when a tick's unfired check runs): the scan fires a tick still inside
|
|
68
|
+
* the window, and its fire job follows within seconds.
|
|
69
|
+
*/
|
|
70
|
+
export declare const AUTOMATION_LATE_FIRE_SLACK_MS: number;
|
|
71
|
+
/** The claimed check's id: `automation-settle-<taskRunId>`, then `-<check>` for the later checks. */
|
|
72
|
+
export declare const automationSettleJobId: (taskRunId: string, check: number) => string;
|
|
73
|
+
/** The unfired check's id: `automation-settle-<taskRunId>-unfired`. */
|
|
74
|
+
export declare const automationSettleUnfiredJobId: (taskRunId: string) => string;
|
|
75
|
+
/**
|
|
76
|
+
* Owes claimed check number `check` of task run `taskRunId` in the transaction in context, due
|
|
77
|
+
* `after` milliseconds from now.
|
|
78
|
+
*/
|
|
79
|
+
export declare function oweAutomationSettle(outbox: Pick<OutboxService, "add">, taskRunId: string, orgId: string, check: number, after: number): Promise<boolean>;
|
|
80
|
+
/**
|
|
81
|
+
* Owes the unfired check of task run `taskRunId` in the transaction that writes the run: for a
|
|
82
|
+
* tick of the schedule (`catchUpWindowMinutes` given) when the fire job would itself call the
|
|
83
|
+
* tick missed (its tick plus the window plus {@link AUTOMATION_LATE_FIRE_SLACK_MS}, at least
|
|
84
|
+
* {@link AUTOMATION_SETTLE_AFTER_MS} on); for a "run now" (null) {@link AUTOMATION_SETTLE_AFTER_MS} on.
|
|
85
|
+
*/
|
|
86
|
+
export declare function oweAutomationSettleUnfired(outbox: Pick<OutboxService, "add">, taskRunId: string, orgId: string,
|
|
87
|
+
/** The run's tick; null for an event run (no tick: settled like a "run now"). */
|
|
88
|
+
scheduledFor: Date | null, catchUpWindowMinutes: number | null): Promise<boolean>;
|
|
89
|
+
/**
|
|
90
|
+
* The one-off job that routes one trigger event to the automations it fires (collaboration spec
|
|
91
|
+
* §4.2), on `cap-kernel-maintenance`, owed by the transaction that stores the event.
|
|
92
|
+
*/
|
|
93
|
+
export declare const TRIGGER_DISPATCH_JOB = "trigger-dispatch";
|
|
94
|
+
/** The data of a {@link TRIGGER_DISPATCH_JOB}. */
|
|
95
|
+
export interface TriggerDispatchJobData {
|
|
96
|
+
orgId: string;
|
|
97
|
+
eventId: string;
|
|
98
|
+
}
|
|
99
|
+
/** The dispatch job's id: `trigger-dispatch-<eventId>` (no `:`, event ids are UUIDs). */
|
|
100
|
+
export declare const triggerDispatchJobId: (eventId: string) => string;
|
|
101
|
+
/**
|
|
102
|
+
* The delayed job that turns one window of an event automation's events for one subject into one
|
|
103
|
+
* run (collaboration spec §4.7), on `cap-kernel-maintenance`, owed by the dispatch that opens the
|
|
104
|
+
* window: the first event delivered to the (automation, subject) pair while none waits for a run.
|
|
105
|
+
*/
|
|
106
|
+
export declare const AUTOMATION_COALESCE_JOB = "automation-coalesce";
|
|
107
|
+
/** The data of a {@link AUTOMATION_COALESCE_JOB}. */
|
|
108
|
+
export interface AutomationCoalesceJobData {
|
|
109
|
+
orgId: string;
|
|
110
|
+
automationId: string;
|
|
111
|
+
/** `subjectKey(subject)` of the events (`automation_event_deliveries.subject_key`). */
|
|
112
|
+
subjectKey: string;
|
|
113
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/** The automations' jobs (a leaf module: type imports only). */
|
|
2
|
+
/**
|
|
3
|
+
* The once-a-minute `@LockedCron` that turns due tasks into task runs (spec §6); its name enqueued
|
|
4
|
+
* on the maintenance queue runs one scan (`ScheduledRuns.runNow`).
|
|
5
|
+
*/
|
|
6
|
+
export const AUTOMATION_SCAN_JOB = "automation-scan";
|
|
7
|
+
/**
|
|
8
|
+
* The `@LockedCron` that deletes old trigger events (`automations.eventRetentionDays`) and owes
|
|
9
|
+
* the coalesce job of a window whose own job was lost (collaboration spec §4.7).
|
|
10
|
+
*/
|
|
11
|
+
export const TRIGGER_EVENT_SWEEP_JOB = "trigger-event-sweep";
|
|
12
|
+
/** The one-off job that checks and starts one task run, on `cap-kernel-maintenance`. */
|
|
13
|
+
export const AUTOMATION_FIRE_JOB = "automation-fire";
|
|
14
|
+
/** The fire job's id: `automation-fire-<taskRunId>` (no `:`, task run ids are UUIDs). */
|
|
15
|
+
export const automationFireJobId = (taskRunId) => `${AUTOMATION_FIRE_JOB}-${taskRunId}`;
|
|
16
|
+
/**
|
|
17
|
+
* Owes the fire job of task run `taskRunId` in the transaction in context (transactional outbox
|
|
18
|
+
* spec §8.1): the transaction that writes a pending task run (the scan's tick, a person's "run
|
|
19
|
+
* now") owes its fire, so a tick is never left unfired. The job's options are its outbox registry
|
|
20
|
+
* entry's; the fire is idempotent on the task run's status.
|
|
21
|
+
*/
|
|
22
|
+
export function oweAutomationFire(outbox, taskRunId, orgId) {
|
|
23
|
+
return outbox.add({
|
|
24
|
+
queue: "cap-kernel-maintenance",
|
|
25
|
+
name: AUTOMATION_FIRE_JOB,
|
|
26
|
+
jobId: automationFireJobId(taskRunId),
|
|
27
|
+
data: { taskRunId },
|
|
28
|
+
orgId,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The delayed job that settles a task run left pending (ruling R26, fix round 1), on
|
|
33
|
+
* `cap-kernel-maintenance`. Two kinds:
|
|
34
|
+
* - the claimed check, owed by the fire job's claim: the run tracker links, settles or releases a
|
|
35
|
+
* run claimed but never started (`automation-settle-<taskRunId>`, then `-<check>`);
|
|
36
|
+
* - the unfired check, owed with the task run itself (the scan's tick, "run now"): a run its fire
|
|
37
|
+
* job never claimed (the job failed for good before its claim) is skipped, `missed` for a tick,
|
|
38
|
+
* `removed` for a "run now", so it never blocks the task's later ticks as an overlap
|
|
39
|
+
* (`automation-settle-<taskRunId>-unfired`).
|
|
40
|
+
*/
|
|
41
|
+
export const AUTOMATION_SETTLE_JOB = "automation-settle";
|
|
42
|
+
/**
|
|
43
|
+
* The first claimed check: 10 minutes after the claim, when the start's retries (seconds) are long
|
|
44
|
+
* over and the run tracker's start grace (`START_GRACE_MS`, 5 minutes) has passed, so a run that
|
|
45
|
+
* left no trace is released at once. Also the unfired check's delay after a "run now".
|
|
46
|
+
*/
|
|
47
|
+
export const AUTOMATION_SETTLE_AFTER_MS = 10 * 60_000;
|
|
48
|
+
/**
|
|
49
|
+
* A claimed run still in flight at a check (its item queued, its message posted and not yet taken
|
|
50
|
+
* by a run) is checked again hourly. The chain ends with the first check that finds the run
|
|
51
|
+
* started, ended or released: the queued item is consumed or removed, the message taken by a run.
|
|
52
|
+
* A chain past {@link AUTOMATION_SETTLE_LONG_CHECKS} checks (a week) is logged as a warning, once.
|
|
53
|
+
*/
|
|
54
|
+
export const AUTOMATION_SETTLE_RECHECK_MS = 60 * 60_000;
|
|
55
|
+
/** A week of hourly checks: the claimed check warns at this one (a run in flight that long). */
|
|
56
|
+
export const AUTOMATION_SETTLE_LONG_CHECKS = 168;
|
|
57
|
+
/**
|
|
58
|
+
* How long past the org's catch-up window a tick's fire may come before the tick is missed (the
|
|
59
|
+
* fire job's own rule, and when a tick's unfired check runs): the scan fires a tick still inside
|
|
60
|
+
* the window, and its fire job follows within seconds.
|
|
61
|
+
*/
|
|
62
|
+
export const AUTOMATION_LATE_FIRE_SLACK_MS = 10 * 60_000;
|
|
63
|
+
/** The claimed check's id: `automation-settle-<taskRunId>`, then `-<check>` for the later checks. */
|
|
64
|
+
export const automationSettleJobId = (taskRunId, check) => check === 0
|
|
65
|
+
? `${AUTOMATION_SETTLE_JOB}-${taskRunId}`
|
|
66
|
+
: `${AUTOMATION_SETTLE_JOB}-${taskRunId}-${check}`;
|
|
67
|
+
/** The unfired check's id: `automation-settle-<taskRunId>-unfired`. */
|
|
68
|
+
export const automationSettleUnfiredJobId = (taskRunId) => `${AUTOMATION_SETTLE_JOB}-${taskRunId}-unfired`;
|
|
69
|
+
/**
|
|
70
|
+
* Owes claimed check number `check` of task run `taskRunId` in the transaction in context, due
|
|
71
|
+
* `after` milliseconds from now.
|
|
72
|
+
*/
|
|
73
|
+
export function oweAutomationSettle(outbox, taskRunId, orgId, check, after) {
|
|
74
|
+
return outbox.add({
|
|
75
|
+
queue: "cap-kernel-maintenance",
|
|
76
|
+
name: AUTOMATION_SETTLE_JOB,
|
|
77
|
+
jobId: automationSettleJobId(taskRunId, check),
|
|
78
|
+
data: { taskRunId, check },
|
|
79
|
+
runAt: new Date(Date.now() + after),
|
|
80
|
+
orgId,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Owes the unfired check of task run `taskRunId` in the transaction that writes the run: for a
|
|
85
|
+
* tick of the schedule (`catchUpWindowMinutes` given) when the fire job would itself call the
|
|
86
|
+
* tick missed (its tick plus the window plus {@link AUTOMATION_LATE_FIRE_SLACK_MS}, at least
|
|
87
|
+
* {@link AUTOMATION_SETTLE_AFTER_MS} on); for a "run now" (null) {@link AUTOMATION_SETTLE_AFTER_MS} on.
|
|
88
|
+
*/
|
|
89
|
+
export function oweAutomationSettleUnfired(outbox, taskRunId, orgId,
|
|
90
|
+
/** The run's tick; null for an event run (no tick: settled like a "run now"). */
|
|
91
|
+
scheduledFor, catchUpWindowMinutes) {
|
|
92
|
+
const soonest = Date.now() + AUTOMATION_SETTLE_AFTER_MS;
|
|
93
|
+
const at = catchUpWindowMinutes === null || scheduledFor === null
|
|
94
|
+
? soonest
|
|
95
|
+
: Math.max(soonest, scheduledFor.getTime() + catchUpWindowMinutes * 60_000 + AUTOMATION_LATE_FIRE_SLACK_MS);
|
|
96
|
+
return outbox.add({
|
|
97
|
+
queue: "cap-kernel-maintenance",
|
|
98
|
+
name: AUTOMATION_SETTLE_JOB,
|
|
99
|
+
jobId: automationSettleUnfiredJobId(taskRunId),
|
|
100
|
+
data: { taskRunId, unfired: true },
|
|
101
|
+
runAt: new Date(at),
|
|
102
|
+
orgId,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The one-off job that routes one trigger event to the automations it fires (collaboration spec
|
|
107
|
+
* §4.2), on `cap-kernel-maintenance`, owed by the transaction that stores the event.
|
|
108
|
+
*/
|
|
109
|
+
export const TRIGGER_DISPATCH_JOB = "trigger-dispatch";
|
|
110
|
+
/** The dispatch job's id: `trigger-dispatch-<eventId>` (no `:`, event ids are UUIDs). */
|
|
111
|
+
export const triggerDispatchJobId = (eventId) => `${TRIGGER_DISPATCH_JOB}-${eventId}`;
|
|
112
|
+
/**
|
|
113
|
+
* The delayed job that turns one window of an event automation's events for one subject into one
|
|
114
|
+
* run (collaboration spec §4.7), on `cap-kernel-maintenance`, owed by the dispatch that opens the
|
|
115
|
+
* window: the first event delivered to the (automation, subject) pair while none waits for a run.
|
|
116
|
+
*/
|
|
117
|
+
export const AUTOMATION_COALESCE_JOB = "automation-coalesce";
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { KernelSchemaDb } from "../db/kernel-schema.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { AutomationPauseReason } from "./types.js";
|
|
3
3
|
/**
|
|
4
4
|
* Pauses an active live task for `reason` in the caller's transaction `t` (the fire job, the run
|
|
5
5
|
* tracker, the delivery path); `by` is the account that caused it, null for the kernel. A task
|
|
6
6
|
* already paused or ended keeps its state and reason. A plain function so the run tracker, which
|
|
7
|
-
* the runtime imports, needs no service of the
|
|
7
|
+
* the runtime imports, needs no service of the automations domain.
|
|
8
8
|
*/
|
|
9
|
-
export declare function pauseTaskIn(t: KernelSchemaDb, taskId: string, reason:
|
|
9
|
+
export declare function pauseTaskIn(t: KernelSchemaDb, taskId: string, reason: AutomationPauseReason, by: string | null): Promise<void>;
|
|
@@ -4,13 +4,13 @@ import { audit } from "../identity/audit.js";
|
|
|
4
4
|
* Pauses an active live task for `reason` in the caller's transaction `t` (the fire job, the run
|
|
5
5
|
* tracker, the delivery path); `by` is the account that caused it, null for the kernel. A task
|
|
6
6
|
* already paused or ended keeps its state and reason. A plain function so the run tracker, which
|
|
7
|
-
* the runtime imports, needs no service of the
|
|
7
|
+
* the runtime imports, needs no service of the automations domain.
|
|
8
8
|
*/
|
|
9
9
|
// org-filter: only kernel paths call it (fire job, run tracker, scan job, channel outbound), each
|
|
10
10
|
// with the task id of a task row or task run row it already holds, never a caller-supplied id
|
|
11
11
|
export async function pauseTaskIn(t, taskId, reason, by) {
|
|
12
12
|
const row = await t
|
|
13
|
-
.updateTable("
|
|
13
|
+
.updateTable("automations")
|
|
14
14
|
.set({
|
|
15
15
|
status: "paused",
|
|
16
16
|
pause_reason: reason,
|
|
@@ -28,7 +28,7 @@ export async function pauseTaskIn(t, taskId, reason, by) {
|
|
|
28
28
|
await audit(t, {
|
|
29
29
|
actorAccountId: by,
|
|
30
30
|
orgId: row.org_id,
|
|
31
|
-
action: "
|
|
31
|
+
action: "automation.paused",
|
|
32
32
|
target: taskId,
|
|
33
33
|
detail: { reason, creator: row.account_id },
|
|
34
34
|
});
|
|
@@ -21,19 +21,19 @@ export declare const START_GRACE_MS: number;
|
|
|
21
21
|
* {@link inFlightIn} is the overlap check's view of the task's previous run: it settles a run whose
|
|
22
22
|
* end was missed and releases one whose queued message is gone.
|
|
23
23
|
*/
|
|
24
|
-
export declare class
|
|
24
|
+
export declare class AutomationRunTracker {
|
|
25
25
|
private readonly kdb;
|
|
26
26
|
readonly txHost: TransactionHost;
|
|
27
27
|
private readonly config;
|
|
28
|
-
/** Owes the task owner's push (`
|
|
28
|
+
/** Owes the task owner's push (`automations`) with every settle. */
|
|
29
29
|
private readonly push;
|
|
30
30
|
constructor(kdb: KernelDb, txHost: TransactionHost, config: Pick<KernelConfigService, "get">,
|
|
31
|
-
/** Owes the task owner's push (`
|
|
32
|
-
push: Pick<PushOwed, "
|
|
31
|
+
/** Owes the task owner's push (`automations`) with every settle. */
|
|
32
|
+
push: Pick<PushOwed, "oweAutomationRun">);
|
|
33
33
|
private get db();
|
|
34
34
|
/**
|
|
35
35
|
* The kernel run `runId` ended with `outcome`: the task run it was started for (the run its
|
|
36
|
-
* message triggered, `
|
|
36
|
+
* message triggered, `automation_runs.run_id`) takes the outcome. A sub-agent or wake run
|
|
37
37
|
* that inherited the marker is not the task run's own and changes nothing.
|
|
38
38
|
*/
|
|
39
39
|
onRunFinished(runId: string, outcome: RunOutcome, error?: string): Promise<void>;
|
|
@@ -68,17 +68,28 @@ export declare class ScheduledTaskRunTracker {
|
|
|
68
68
|
*/
|
|
69
69
|
export declare function linkStartedRunIn(t: KernelSchemaDb, taskRunId: string, runId: string, conversationId: string): Promise<void>;
|
|
70
70
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
71
|
+
* The automation run a queued item's stored marker names: its schedule marker's `taskRunId`, or
|
|
72
|
+
* its event marker's `automationRunId` (collaboration spec §4.6); undefined for a person's item.
|
|
73
|
+
*/
|
|
74
|
+
export declare function queuedAutomationRunId(item: {
|
|
75
|
+
schedule: unknown;
|
|
76
|
+
automation_event: unknown;
|
|
77
|
+
}): string | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* A person removed, edited or sent the queued item that carried a schedule or event marker (the
|
|
80
|
+
* item's stored columns): its task run will never run as the task's, so it becomes `skipped`
|
|
73
81
|
* (`removed`) in the caller's transaction, and the next tick is not held by the overlap check.
|
|
74
82
|
*/
|
|
75
|
-
export declare function releaseQueuedTaskRunIn(t: KernelSchemaDb,
|
|
83
|
+
export declare function releaseQueuedTaskRunIn(t: KernelSchemaDb, item: {
|
|
84
|
+
schedule: unknown;
|
|
85
|
+
automation_event: unknown;
|
|
86
|
+
}): Promise<void>;
|
|
76
87
|
/**
|
|
77
88
|
* Locks a task's row (deleted or not) in the caller's transaction. Every path that locks a task
|
|
78
89
|
* run and the task takes the task row first: the fire job's claim, the run tracker, the scan's
|
|
79
90
|
* repair (a run finishing while the next tick's claim checks overlap must not deadlock).
|
|
80
91
|
*/
|
|
81
92
|
export declare function lockTaskRow(t: KernelSchemaDb, taskId: string): Promise<void>;
|
|
82
|
-
/** The run tracker alone: the runtime (`RunFinish`) imports it without the
|
|
83
|
-
export declare class
|
|
93
|
+
/** The run tracker alone: the runtime (`RunFinish`) imports it without the automations service. */
|
|
94
|
+
export declare class AutomationTrackerModule {
|
|
84
95
|
}
|