@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.
Files changed (250) hide show
  1. package/dist/agents/agents.schemas.d.ts +9 -9
  2. package/dist/{schedules/schedules.controller.d.ts → automations/automations.controller.d.ts} +37 -32
  3. package/dist/{schedules/schedules.controller.js → automations/automations.controller.js} +68 -57
  4. package/dist/automations/automations.module.d.ts +16 -0
  5. package/dist/{schedules/schedules.module.js → automations/automations.module.js} +18 -15
  6. package/dist/{schedules/schedules.schemas.d.ts → automations/automations.schemas.d.ts} +151 -50
  7. package/dist/automations/automations.schemas.js +253 -0
  8. package/dist/{schedules/scheduled-tasks.service.d.ts → automations/automations.service.d.ts} +99 -52
  9. package/dist/{schedules/scheduled-tasks.service.js → automations/automations.service.js} +303 -134
  10. package/dist/{schedules → automations}/calendar.d.ts +8 -8
  11. package/dist/{schedules → automations}/calendar.js +2 -2
  12. package/dist/automations/effective-settings.d.ts +28 -0
  13. package/dist/automations/effective-settings.js +25 -0
  14. package/dist/automations/errors.d.ts +19 -0
  15. package/dist/automations/errors.js +19 -0
  16. package/dist/automations/events/coalesce.d.ts +28 -0
  17. package/dist/automations/events/coalesce.js +131 -0
  18. package/dist/automations/events/deadline.d.ts +10 -0
  19. package/dist/automations/events/deadline.js +24 -0
  20. package/dist/automations/events/dispatcher.d.ts +74 -0
  21. package/dist/automations/events/dispatcher.js +353 -0
  22. package/dist/automations/events/events.module.d.ts +7 -0
  23. package/dist/automations/events/events.module.js +26 -0
  24. package/dist/automations/events/guards.d.ts +32 -0
  25. package/dist/automations/events/guards.js +87 -0
  26. package/dist/automations/events/sweep.job.d.ts +49 -0
  27. package/dist/automations/events/sweep.job.js +202 -0
  28. package/dist/automations/events/token.d.ts +5 -0
  29. package/dist/automations/events/token.js +5 -0
  30. package/dist/automations/events/trigger-events.service.d.ts +47 -0
  31. package/dist/automations/events/trigger-events.service.js +254 -0
  32. package/dist/{schedules → automations}/fire.d.ts +57 -17
  33. package/dist/{schedules → automations}/fire.js +311 -79
  34. package/dist/{schedules → automations}/grant-rules.d.ts +1 -1
  35. package/dist/{schedules → automations}/grant-rules.js +5 -5
  36. package/dist/automations/job-ids.d.ts +113 -0
  37. package/dist/automations/job-ids.js +117 -0
  38. package/dist/{schedules → automations}/pause.d.ts +3 -3
  39. package/dist/{schedules → automations}/pause.js +3 -3
  40. package/dist/{schedules → automations}/run-tracker.d.ts +21 -10
  41. package/dist/{schedules → automations}/run-tracker.js +68 -56
  42. package/dist/{schedules → automations}/scan.job.d.ts +12 -8
  43. package/dist/{schedules → automations}/scan.job.js +39 -23
  44. package/dist/{schedules → automations}/schedule-math.d.ts +19 -12
  45. package/dist/{schedules → automations}/schedule-math.js +17 -7
  46. package/dist/{schedules → automations}/store.d.ts +57 -27
  47. package/dist/{schedules → automations}/store.js +102 -52
  48. package/dist/automations/store.module.d.ts +6 -0
  49. package/dist/{schedules → automations}/store.module.js +9 -9
  50. package/dist/automations/types.d.ts +178 -0
  51. package/dist/{schedules → automations}/unattended.d.ts +22 -11
  52. package/dist/{schedules → automations}/unattended.js +53 -30
  53. package/dist/catalog/marketplaces.service.d.ts +1 -1
  54. package/dist/channels/channels.module.js +3 -3
  55. package/dist/channels/outbound.d.ts +5 -5
  56. package/dist/channels/outbound.js +17 -17
  57. package/dist/channels/threads.d.ts +1 -1
  58. package/dist/channels/threads.js +1 -1
  59. package/dist/config/schema.d.ts +5 -1
  60. package/dist/config/schema.js +44 -6
  61. package/dist/conversations/approval-profile.d.ts +2 -2
  62. package/dist/conversations/approval-profile.js +3 -3
  63. package/dist/conversations/conversations.controller.d.ts +7 -1
  64. package/dist/conversations/conversations.controller.js +26 -10
  65. package/dist/conversations/conversations.service.d.ts +103 -25
  66. package/dist/conversations/conversations.service.js +138 -33
  67. package/dist/conversations/events.d.ts +48 -1
  68. package/dist/conversations/events.js +61 -9
  69. package/dist/conversations/message-context.d.ts +2 -2
  70. package/dist/conversations/message-context.js +1 -1
  71. package/dist/conversations/permission-profile-store.d.ts +2 -1
  72. package/dist/conversations/permission-profiles.controller.d.ts +3 -0
  73. package/dist/conversations/permission-profiles.service.js +1 -1
  74. package/dist/conversations/prompt-suggestion-request.d.ts +1 -1
  75. package/dist/conversations/prompt-suggestion-request.js +2 -1
  76. package/dist/conversations/queue.d.ts +7 -4
  77. package/dist/conversations/queue.js +6 -2
  78. package/dist/conversations/queue.service.d.ts +5 -5
  79. package/dist/conversations/queue.service.js +20 -14
  80. package/dist/ctx/create-ctx.d.ts +20 -1
  81. package/dist/ctx/create-ctx.js +59 -2
  82. package/dist/db/kernel-schema.d.ts +104 -25
  83. package/dist/devices/device-project-files.service.d.ts +88 -0
  84. package/dist/devices/device-project-files.service.js +249 -0
  85. package/dist/devices/device-projects.service.d.ts +40 -0
  86. package/dist/devices/device-projects.service.js +16 -13
  87. package/dist/devices/devices.controller.d.ts +9 -3
  88. package/dist/devices/devices.controller.js +27 -5
  89. package/dist/devices/devices.module.js +9 -1
  90. package/dist/devices/devices.schemas.js +6 -1
  91. package/dist/devices/dto.d.ts +15 -0
  92. package/dist/devices/dto.js +11 -0
  93. package/dist/devices/project-paths.d.ts +29 -0
  94. package/dist/devices/project-paths.js +66 -0
  95. package/dist/devices/project-rules.d.ts +5 -1
  96. package/dist/devices/project-rules.js +5 -1
  97. package/dist/errors.d.ts +47 -13
  98. package/dist/errors.js +47 -13
  99. package/dist/evals/reconstruct.d.ts +10 -1
  100. package/dist/evals/reconstruct.js +7 -0
  101. package/dist/extensions/host.d.ts +6 -1
  102. package/dist/extensions/host.js +10 -0
  103. package/dist/extensions/permission-set.d.ts +17 -1
  104. package/dist/extensions/permission-set.js +13 -1
  105. package/dist/extensions/permission-view.d.ts +1 -1
  106. package/dist/extensions/permission-view.js +4 -1
  107. package/dist/git/gateway.js +2 -2
  108. package/dist/handoff/app-schemes.d.ts +28 -0
  109. package/dist/handoff/app-schemes.js +41 -0
  110. package/dist/handoff/handoff-paths.d.ts +26 -0
  111. package/dist/handoff/handoff-paths.js +25 -0
  112. package/dist/handoff/handoff-tickets.d.ts +28 -0
  113. package/dist/handoff/handoff-tickets.js +111 -0
  114. package/dist/handoff/handoff.controller.d.ts +24 -0
  115. package/dist/handoff/handoff.controller.js +74 -0
  116. package/dist/handoff/handoff.module.d.ts +7 -0
  117. package/dist/handoff/handoff.module.js +26 -0
  118. package/dist/handoff/handoff.schemas.d.ts +13 -0
  119. package/dist/handoff/handoff.schemas.js +3 -0
  120. package/dist/handoff/handoff.service.d.ts +49 -0
  121. package/dist/handoff/handoff.service.js +96 -0
  122. package/dist/http/api.module.js +13 -5
  123. package/dist/http/meta.controller.d.ts +1 -0
  124. package/dist/http/meta.controller.js +7 -0
  125. package/dist/http/request-signal.d.ts +8 -0
  126. package/dist/http/request-signal.js +14 -0
  127. package/dist/identity/sso.controller.d.ts +30 -6
  128. package/dist/identity/sso.controller.js +51 -17
  129. package/dist/identity/sso.d.ts +10 -2
  130. package/dist/identity/sso.js +15 -3
  131. package/dist/inbox/inbox.module.js +2 -2
  132. package/dist/inbox/inbox.service.d.ts +8 -8
  133. package/dist/inbox/inbox.service.js +7 -7
  134. package/dist/infra/config/kernel-config.service.js +10 -5
  135. package/dist/infra/outbox/registry.d.ts +29 -2
  136. package/dist/infra/outbox/registry.js +30 -7
  137. package/dist/kernel.module.js +3 -0
  138. package/dist/orgs/roles.d.ts +1 -1
  139. package/dist/orgs/roles.js +3 -3
  140. package/dist/orgs/settings.controller.d.ts +1 -1
  141. package/dist/orgs/settings.service.d.ts +27 -11
  142. package/dist/orgs/settings.service.js +42 -24
  143. package/dist/overrides.d.ts +4 -4
  144. package/dist/overrides.js +1 -1
  145. package/dist/platform/exclusions.js +5 -0
  146. package/dist/platform/operation.js +2 -2
  147. package/dist/platform/ops/{schedules.ops.d.ts → automations.ops.d.ts} +4 -4
  148. package/dist/platform/ops/automations.ops.js +560 -0
  149. package/dist/platform/ops/connectors.ops.js +4 -2
  150. package/dist/platform/ops/devices.ops.d.ts +3 -1
  151. package/dist/platform/ops/devices.ops.js +77 -3
  152. package/dist/platform/ops/inbox.ops.js +4 -4
  153. package/dist/platform/ops/orgs.ops.js +1 -1
  154. package/dist/platform/ops/platform.ops.js +4 -4
  155. package/dist/platform/platform-ops.module.d.ts +2 -2
  156. package/dist/platform/platform-ops.module.js +5 -5
  157. package/dist/platform/route-schemas.js +3 -3
  158. package/dist/push/jobs.d.ts +3 -3
  159. package/dist/push/jobs.js +2 -2
  160. package/dist/push/preferences.service.js +1 -1
  161. package/dist/push/push-owed.d.ts +3 -3
  162. package/dist/push/push-owed.js +6 -6
  163. package/dist/push/push.schemas.d.ts +4 -4
  164. package/dist/push/push.schemas.js +3 -3
  165. package/dist/push/recipients.d.ts +4 -4
  166. package/dist/push/recipients.js +23 -23
  167. package/dist/push/texts.d.ts +3 -3
  168. package/dist/push/texts.js +2 -2
  169. package/dist/push/triggers.d.ts +1 -1
  170. package/dist/roles/worker.module.js +3 -3
  171. package/dist/runtime/compaction.service.d.ts +3 -1
  172. package/dist/runtime/compaction.service.js +3 -1
  173. package/dist/runtime/context/build.d.ts +6 -0
  174. package/dist/runtime/context/build.js +14 -3
  175. package/dist/runtime/context/compaction.d.ts +27 -1
  176. package/dist/runtime/context/compaction.js +102 -37
  177. package/dist/runtime/context/run-context.d.ts +38 -0
  178. package/dist/runtime/context/run-context.js +54 -0
  179. package/dist/runtime/maintenance.d.ts +8 -4
  180. package/dist/runtime/maintenance.js +37 -16
  181. package/dist/runtime/model-call.d.ts +6 -4
  182. package/dist/runtime/model-call.js +17 -7
  183. package/dist/runtime/project/knowledge.service.d.ts +16 -7
  184. package/dist/runtime/project/knowledge.service.js +31 -13
  185. package/dist/runtime/run-finish.d.ts +4 -4
  186. package/dist/runtime/run-finish.js +3 -3
  187. package/dist/runtime/run-queue.js +12 -7
  188. package/dist/runtime/run-worker.js +7 -1
  189. package/dist/runtime/runtime.module.js +3 -3
  190. package/dist/runtime/subagents/dispatch.js +11 -3
  191. package/dist/sandbox/browse-roots.d.ts +8 -0
  192. package/dist/sandbox/browse-roots.js +22 -0
  193. package/dist/sandbox/network-grants.d.ts +4 -4
  194. package/dist/sandbox/network-grants.js +7 -7
  195. package/dist/sandbox/operation-authority.d.ts +10 -3
  196. package/dist/sandbox/operation-authority.js +20 -10
  197. package/dist/sandbox/operation-intents.d.ts +3 -1
  198. package/dist/sandbox/operation-intents.js +11 -4
  199. package/dist/sandbox/permission-call-grants.js +2 -2
  200. package/dist/sandbox/permission-facts.js +1 -1
  201. package/dist/sandbox/review/classifier.d.ts +2 -2
  202. package/dist/sandbox/review/classifier.js +22 -11
  203. package/dist/sandbox/review/permission-candidate.d.ts +1 -1
  204. package/dist/sandbox/review/permission-candidate.js +1 -1
  205. package/dist/sandbox/sandbox.service.d.ts +15 -5
  206. package/dist/sandbox/sandbox.service.js +14 -8
  207. package/dist/storage/purge.d.ts +1 -1
  208. package/dist/storage/purge.js +2 -2
  209. package/dist/tools/approvals.service.d.ts +6 -6
  210. package/dist/tools/approvals.service.js +19 -19
  211. package/dist/tools/builtin-names.d.ts +12 -12
  212. package/dist/tools/builtin-names.js +29 -29
  213. package/dist/tools/builtins/{schedules.d.ts → automations.d.ts} +35 -33
  214. package/dist/tools/builtins/{schedules.js → automations.js} +236 -105
  215. package/dist/tools/builtins/builtin-tools.d.ts +8 -8
  216. package/dist/tools/builtins/builtin-tools.js +16 -16
  217. package/dist/tools/call-scope.d.ts +1 -1
  218. package/dist/tools/connectors.service.d.ts +4 -13
  219. package/dist/tools/connectors.service.js +12 -21
  220. package/dist/tools/git-asks.js +1 -1
  221. package/dist/tools/git-push-policy.d.ts +3 -3
  222. package/dist/tools/git-push-policy.js +6 -6
  223. package/dist/tools/network-asks.d.ts +3 -3
  224. package/dist/tools/network-asks.js +3 -3
  225. package/dist/tools/oauth/flow.d.ts +16 -2
  226. package/dist/tools/oauth/flow.js +23 -4
  227. package/dist/tools/oauth/oauth.controller.d.ts +19 -7
  228. package/dist/tools/oauth/oauth.controller.js +42 -12
  229. package/dist/tools/oauth/oauth.schemas.d.ts +14 -1
  230. package/dist/tools/oauth/oauth.schemas.js +4 -1
  231. package/dist/tools/pipeline.d.ts +4 -4
  232. package/dist/tools/pipeline.js +2 -2
  233. package/dist/tools/policy.d.ts +3 -3
  234. package/dist/tools/policy.js +3 -3
  235. package/dist/tools/tools.module.js +9 -9
  236. package/package.json +3 -3
  237. package/src/migrations/0188_automations_rename.sql +309 -0
  238. package/src/migrations/0189_event_automations.sql +136 -0
  239. package/dist/platform/ops/schedules.ops.js +0 -509
  240. package/dist/schedules/effective-settings.d.ts +0 -22
  241. package/dist/schedules/effective-settings.js +0 -18
  242. package/dist/schedules/errors.d.ts +0 -8
  243. package/dist/schedules/errors.js +0 -8
  244. package/dist/schedules/job-ids.d.ts +0 -81
  245. package/dist/schedules/job-ids.js +0 -95
  246. package/dist/schedules/schedules.module.d.ts +0 -16
  247. package/dist/schedules/schedules.schemas.js +0 -213
  248. package/dist/schedules/store.module.d.ts +0 -6
  249. package/dist/schedules/types.d.ts +0 -110
  250. /package/dist/{schedules → automations}/types.js +0 -0
@@ -1,15 +1,16 @@
1
1
  import type { KernelSchemaDb } from "../db/kernel-schema.js";
2
- import type { ScheduledTask, ScheduledTaskGrant, TaskRun, TaskRunStatus } from "./types.js";
2
+ import type { Automation, AutomationGrant, TaskRun, TaskRunStatus, TimedTaskRun } from "./types.js";
3
3
  /** What a caller supplies to create a task; the counters, pause fields and timestamps default. */
4
- export type ScheduledTaskInsert = Omit<ScheduledTask, "pauseReason" | "pausedBy" | "consecutiveFailures" | "createdAt" | "updatedAt">;
4
+ export type AutomationInsert = Omit<Automation, "pauseReason" | "pausedBy" | "consecutiveFailures" | "createdAt" | "updatedAt" | "unattendedAsk"> & {
5
+ unattendedAsk?: Automation["unattendedAsk"];
6
+ };
5
7
  /** The fields a task update may change (`updated_at` is always refreshed). */
6
- export type ScheduledTaskPatch = Partial<Omit<ScheduledTask, "id" | "orgId" | "accountId" | "createdAt" | "updatedAt">>;
7
- /** What a caller supplies to create a task run; it starts `pending`. */
8
- export interface TaskRunInsert {
8
+ export type AutomationPatch = Partial<Omit<Automation, "id" | "orgId" | "accountId" | "createdAt" | "updatedAt">>;
9
+ /** The fields of a task run insert either kind of run has; it starts `pending`. */
10
+ interface TaskRunInsertBase {
9
11
  id: string;
10
12
  taskId: string;
11
13
  orgId: string;
12
- scheduledFor: Date;
13
14
  trial: boolean;
14
15
  /** A person's "run now" (default false: a tick of the schedule). */
15
16
  manual?: boolean;
@@ -18,29 +19,47 @@ export interface TaskRunInsert {
18
19
  firedAt?: Date | null;
19
20
  conversationId?: string | null;
20
21
  }
21
- export type TaskRunPatch = Partial<Omit<TaskRun, "id" | "taskId" | "orgId" | "trial" | "manual">>;
22
+ /** A time run: a tick of the schedule (or a person's "run now" at a moment). */
23
+ export interface TimedTaskRunInsert extends TaskRunInsertBase {
24
+ scheduledFor: Date;
25
+ /** Default: the tick as `Date.toISOString()`. */
26
+ triggerKey?: string;
27
+ }
28
+ /** An event run (collaboration spec §4.7): no tick; keyed by the first event it serves. */
29
+ export interface EventTaskRunInsert extends TaskRunInsertBase {
30
+ scheduledFor: null;
31
+ triggerKey: string;
32
+ }
33
+ /** What a caller supplies to create a task run; it starts `pending`. */
34
+ export type TaskRunInsert = TimedTaskRunInsert | EventTaskRunInsert;
35
+ /**
36
+ * The moment a run is ordered by in the run lists: its tick, else its claim, else when it was
37
+ * recorded (an event run has no tick and no claim before its fire job takes it).
38
+ */
39
+ export declare const runOrderMoment: import("kysely").RawBuilder<Date>;
40
+ export type TaskRunPatch = Partial<Omit<TaskRun, "id" | "taskId" | "orgId" | "trial" | "manual" | "triggerKey">>;
22
41
  /**
23
- * Scheduled tasks, their runs and grants (spec §4): reads and writes rows only. Every method takes
42
+ * Automations, their runs and grants (spec §4): reads and writes rows only. Every method takes
24
43
  * the database handle first so the caller decides the transaction (the scanner's `lockDue` and
25
44
  * `insertRun` run in one, under the rows' locks).
26
45
  */
27
- export declare class ScheduledTaskStore {
28
- insertTask(t: KernelSchemaDb, task: ScheduledTaskInsert): Promise<ScheduledTask>;
46
+ export declare class AutomationStore {
47
+ insertTask(t: KernelSchemaDb, task: AutomationInsert): Promise<Automation>;
29
48
  /** A live (not deleted) task of the org; null when missing, deleted or in another org. */
30
- getTask(t: KernelSchemaDb, orgId: string, id: string): Promise<ScheduledTask | null>;
49
+ getTask(t: KernelSchemaDb, orgId: string, id: string): Promise<Automation | null>;
31
50
  /**
32
51
  * {@link getTask} under `FOR UPDATE` (call it inside a transaction): a person's write holds the
33
52
  * row, so the scanner's `lockDue` skips it until the write commits.
34
53
  */
35
- lockTask(t: KernelSchemaDb, orgId: string, id: string): Promise<ScheduledTask | null>;
54
+ lockTask(t: KernelSchemaDb, orgId: string, id: string): Promise<Automation | null>;
36
55
  /** Patches a live task; null when it is missing or deleted. */
37
- updateTask(t: KernelSchemaDb, id: string, patch: ScheduledTaskPatch): Promise<ScheduledTask | null>;
56
+ updateTask(t: KernelSchemaDb, id: string, patch: AutomationPatch): Promise<Automation | null>;
38
57
  /** Deletes a task softly: hidden from every read and never due again. */
39
58
  softDelete(t: KernelSchemaDb, id: string): Promise<boolean>;
40
59
  /** A creator's live tasks, newest first. */
41
- listForAccount(t: KernelSchemaDb, orgId: string, accountId: string): Promise<ScheduledTask[]>;
60
+ listForAccount(t: KernelSchemaDb, orgId: string, accountId: string): Promise<Automation[]>;
42
61
  /** Every live task of the org, newest first. */
43
- listForOrg(t: KernelSchemaDb, orgId: string): Promise<ScheduledTask[]>;
62
+ listForOrg(t: KernelSchemaDb, orgId: string): Promise<Automation[]>;
44
63
  /** Live tasks that count against the caps (`active` and `paused`), of the org or one creator. */
45
64
  countActive(t: KernelSchemaDb, orgId: string, accountId?: string): Promise<number>;
46
65
  /**
@@ -48,12 +67,20 @@ export declare class ScheduledTaskStore {
48
67
  * rows another transaction holds (`FOR UPDATE SKIP LOCKED`). Call it inside a transaction: the
49
68
  * locks last until it ends.
50
69
  */
51
- lockDue(t: KernelSchemaDb, now: Date, limit: number): Promise<ScheduledTask[]>;
70
+ lockDue(t: KernelSchemaDb, now: Date, limit: number): Promise<Automation[]>;
71
+ /**
72
+ * Locks up to `limit` active live event automations past their `expires_at` (an event
73
+ * automation has no `next_run_at`, so {@link lockDue} never selects it), skipping rows another
74
+ * transaction holds. Call it inside a transaction: the scan ends each as it ends an expired time
75
+ * task.
76
+ */
77
+ lockExpiredEvent(t: KernelSchemaDb, now: Date, limit: number): Promise<Automation[]>;
52
78
  /**
53
- * Inserts a task run; null when a non-trial run for the same tick already exists (the
54
- * exactly-once guard, `UNIQUE (task_id, scheduled_for) WHERE NOT trial`).
79
+ * Inserts a task run; null when a non-trial run for the same trigger key (a time run's tick)
80
+ * already exists (the exactly-once guard, `UNIQUE (automation_id, trigger_key) WHERE NOT trial`).
55
81
  */
56
- insertRun(t: KernelSchemaDb, run: TaskRunInsert): Promise<TaskRun | null>;
82
+ insertRun(t: KernelSchemaDb, run: TimedTaskRunInsert): Promise<TimedTaskRun | null>;
83
+ insertRun(t: KernelSchemaDb, run: EventTaskRunInsert): Promise<TaskRun | null>;
57
84
  /** Patches a task run; null when it does not exist. */
58
85
  updateRun(t: KernelSchemaDb, id: string, patch: TaskRunPatch): Promise<TaskRun | null>;
59
86
  /**
@@ -62,20 +89,22 @@ export declare class ScheduledTaskStore {
62
89
  */
63
90
  latestNonTerminalRun(t: KernelSchemaDb, taskId: string, exceptId?: string): Promise<TaskRun | null>;
64
91
  /**
65
- * Runs of the org fired at or after `since` (the `maxRunsPerHour` count; trial runs included,
66
- * skipped runs not: a run whose queued message a person removed was never run).
92
+ * Runs of the org's automations owned by `accountId` fired at or after `since` (the
93
+ * `maxRunsPerHour` count, per person: collaboration spec §4.7; trial runs included, skipped runs
94
+ * not: a run whose queued message a person removed was never run). Deleted automations count:
95
+ * their runs were made.
67
96
  */
68
- countFiredSince(t: KernelSchemaDb, orgId: string, since: Date): Promise<number>;
69
- /** A task's runs, latest tick first. */
97
+ countFiredSince(t: KernelSchemaDb, orgId: string, accountId: string, since: Date): Promise<number>;
98
+ /** A task's runs, latest first (by tick, else claim, else record: {@link runOrderMoment}). */
70
99
  listRuns(t: KernelSchemaDb, taskId: string, limit: number): Promise<TaskRun[]>;
71
100
  /**
72
101
  * The runs of these tasks of the org with `scheduled_for` in `[from, to)`, trial included, by tick, at most
73
- * `limit` (the calendar; `scheduled_task_runs_task_idx` per task).
102
+ * `limit` (the calendar; `automation_runs_automation_idx` per task).
74
103
  */
75
- listRunsBetween(t: KernelSchemaDb, orgId: string, taskIds: readonly string[], from: Date, to: Date, limit: number): Promise<TaskRun[]>;
76
- listGrants(t: KernelSchemaDb, taskId: string): Promise<ScheduledTaskGrant[]>;
104
+ listRunsBetween(t: KernelSchemaDb, orgId: string, taskIds: readonly string[], from: Date, to: Date, limit: number): Promise<TimedTaskRun[]>;
105
+ listGrants(t: KernelSchemaDb, taskId: string): Promise<AutomationGrant[]>;
77
106
  /** Adds a grant, replacing the task's grant of the same tool key. */
78
- addGrant(t: KernelSchemaDb, taskId: string, grant: ScheduledTaskGrant): Promise<void>;
107
+ addGrant(t: KernelSchemaDb, taskId: string, grant: AutomationGrant): Promise<void>;
79
108
  removeGrant(t: KernelSchemaDb, taskId: string, toolKey: string): Promise<boolean>;
80
109
  }
81
110
  /**
@@ -84,3 +113,4 @@ export declare class ScheduledTaskStore {
84
113
  * A plain function too, for the tool policy (`createDecidePolicy`), which is not a provider.
85
114
  */
86
115
  export declare function findTaskGrant(t: KernelSchemaDb, taskId: string, toolKey: string, specHash: string, connectorId: string | null): Promise<boolean>;
116
+ export {};
@@ -7,6 +7,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  import { Injectable } from "@nestjs/common";
8
8
  import { sql } from "kysely";
9
9
  import { asPlatform } from "../infra/database/org-scope.js";
10
+ /**
11
+ * The moment a run is ordered by in the run lists: its tick, else its claim, else when it was
12
+ * recorded (an event run has no tick and no claim before its fire job takes it).
13
+ */
14
+ export const runOrderMoment = sql `coalesce(scheduled_for, fired_at, created_at)`;
10
15
  const NON_TERMINAL = ["pending", "running"];
11
16
  const date = (v) => (v instanceof Date ? v : new Date(v));
12
17
  const dateOrNull = (v) => (v === null ? null : date(v));
@@ -24,6 +29,7 @@ const toTask = (r) => ({
24
29
  model: r.model,
25
30
  effort: r.effort,
26
31
  deviceId: r.device_id,
32
+ unattendedAsk: r.unattended_ask,
27
33
  status: r.status,
28
34
  pauseReason: r.pause_reason,
29
35
  pausedBy: r.paused_by,
@@ -36,9 +42,10 @@ const toTask = (r) => ({
36
42
  });
37
43
  const toRun = (r) => ({
38
44
  id: r.id,
39
- taskId: r.task_id,
45
+ taskId: r.automation_id,
40
46
  orgId: r.org_id,
41
- scheduledFor: date(r.scheduled_for),
47
+ scheduledFor: dateOrNull(r.scheduled_for),
48
+ triggerKey: r.trigger_key,
42
49
  firedAt: dateOrNull(r.fired_at),
43
50
  trial: r.trial,
44
51
  manual: r.manual,
@@ -49,6 +56,13 @@ const toRun = (r) => ({
49
56
  error: r.error,
50
57
  missingGrants: r.missing_grants,
51
58
  });
59
+ /** A run read back from a row written with a tick (a time run). */
60
+ const toTimedRun = (r) => {
61
+ const run = toRun(r);
62
+ if (run.scheduledFor === null)
63
+ throw new Error(`automation run ${r.id} has no tick`);
64
+ return { ...run, scheduledFor: run.scheduledFor };
65
+ };
52
66
  const json = (v) => (v === null ? null : JSON.stringify(v));
53
67
  /** Maps a task patch to its columns; fields left undefined are not written. */
54
68
  function taskColumns(p) {
@@ -73,6 +87,8 @@ function taskColumns(p) {
73
87
  c.effort = p.effort;
74
88
  if (p.deviceId !== undefined)
75
89
  c.device_id = p.deviceId;
90
+ if (p.unattendedAsk !== undefined)
91
+ c.unattended_ask = p.unattendedAsk;
76
92
  if (p.status !== undefined)
77
93
  c.status = p.status;
78
94
  if (p.pauseReason !== undefined)
@@ -90,14 +106,14 @@ function taskColumns(p) {
90
106
  return c;
91
107
  }
92
108
  /**
93
- * Scheduled tasks, their runs and grants (spec §4): reads and writes rows only. Every method takes
109
+ * Automations, their runs and grants (spec §4): reads and writes rows only. Every method takes
94
110
  * the database handle first so the caller decides the transaction (the scanner's `lockDue` and
95
111
  * `insertRun` run in one, under the rows' locks).
96
112
  */
97
- let ScheduledTaskStore = class ScheduledTaskStore {
113
+ let AutomationStore = class AutomationStore {
98
114
  async insertTask(t, task) {
99
115
  const row = await t
100
- .insertInto("scheduled_tasks")
116
+ .insertInto("automations")
101
117
  .values({
102
118
  id: task.id,
103
119
  org_id: task.orgId,
@@ -112,6 +128,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
112
128
  model: json(task.model),
113
129
  effort: task.effort,
114
130
  device_id: task.deviceId,
131
+ unattended_ask: task.unattendedAsk ?? null,
115
132
  status: task.status,
116
133
  next_run_at: task.nextRunAt,
117
134
  jitter_seconds: task.jitterSeconds,
@@ -124,7 +141,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
124
141
  /** A live (not deleted) task of the org; null when missing, deleted or in another org. */
125
142
  async getTask(t, orgId, id) {
126
143
  const row = await t
127
- .selectFrom("scheduled_tasks")
144
+ .selectFrom("automations")
128
145
  .selectAll()
129
146
  .where("org_id", "=", orgId)
130
147
  .where("id", "=", id)
@@ -138,7 +155,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
138
155
  */
139
156
  async lockTask(t, orgId, id) {
140
157
  const row = await t
141
- .selectFrom("scheduled_tasks")
158
+ .selectFrom("automations")
142
159
  .selectAll()
143
160
  .where("org_id", "=", orgId)
144
161
  .where("id", "=", id)
@@ -152,7 +169,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
152
169
  // or locked in the scan's due sweep (which runs in the platform scope)
153
170
  async updateTask(t, id, patch) {
154
171
  const row = await t
155
- .updateTable("scheduled_tasks")
172
+ .updateTable("automations")
156
173
  .set({ ...taskColumns(patch), updated_at: sql `now()` })
157
174
  .where("id", "=", id)
158
175
  .where("deleted_at", "is", null)
@@ -164,7 +181,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
164
181
  // org-filter: by the id of a task its caller locked under an org filter (lockManaged)
165
182
  async softDelete(t, id) {
166
183
  const r = await t
167
- .updateTable("scheduled_tasks")
184
+ .updateTable("automations")
168
185
  .set({ deleted_at: sql `now()`, updated_at: sql `now()`, next_run_at: null })
169
186
  .where("id", "=", id)
170
187
  .where("deleted_at", "is", null)
@@ -174,7 +191,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
174
191
  /** A creator's live tasks, newest first. */
175
192
  async listForAccount(t, orgId, accountId) {
176
193
  const rows = await t
177
- .selectFrom("scheduled_tasks")
194
+ .selectFrom("automations")
178
195
  .selectAll()
179
196
  .where("org_id", "=", orgId)
180
197
  .where("account_id", "=", accountId)
@@ -187,7 +204,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
187
204
  /** Every live task of the org, newest first. */
188
205
  async listForOrg(t, orgId) {
189
206
  const rows = await t
190
- .selectFrom("scheduled_tasks")
207
+ .selectFrom("automations")
191
208
  .selectAll()
192
209
  .where("org_id", "=", orgId)
193
210
  .where("deleted_at", "is", null)
@@ -199,7 +216,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
199
216
  /** Live tasks that count against the caps (`active` and `paused`), of the org or one creator. */
200
217
  async countActive(t, orgId, accountId) {
201
218
  let q = t
202
- .selectFrom("scheduled_tasks")
219
+ .selectFrom("automations")
203
220
  .select((eb) => eb.fn.countAll().as("n"))
204
221
  .where("org_id", "=", orgId)
205
222
  .where("deleted_at", "is", null)
@@ -218,7 +235,7 @@ let ScheduledTaskStore = class ScheduledTaskStore {
218
235
  async lockDue(t, now, limit) {
219
236
  return asPlatform("deployment-wide due-task sweep", async () => {
220
237
  const rows = await t
221
- .selectFrom("scheduled_tasks")
238
+ .selectFrom("automations")
222
239
  .selectAll()
223
240
  .where("status", "=", "active")
224
241
  .where("deleted_at", "is", null)
@@ -233,17 +250,42 @@ let ScheduledTaskStore = class ScheduledTaskStore {
233
250
  });
234
251
  }
235
252
  /**
236
- * Inserts a task run; null when a non-trial run for the same tick already exists (the
237
- * exactly-once guard, `UNIQUE (task_id, scheduled_for) WHERE NOT trial`).
253
+ * Locks up to `limit` active live event automations past their `expires_at` (an event
254
+ * automation has no `next_run_at`, so {@link lockDue} never selects it), skipping rows another
255
+ * transaction holds. Call it inside a transaction: the scan ends each as it ends an expired time
256
+ * task.
238
257
  */
258
+ // org-filter: the scan's deployment-wide expired event automation sweep (LockedCron), each task
259
+ // ended by its own id
260
+ async lockExpiredEvent(t, now, limit) {
261
+ return asPlatform("deployment-wide expired event automation sweep", async () => {
262
+ const rows = await t
263
+ .selectFrom("automations")
264
+ .selectAll()
265
+ .where("status", "=", "active")
266
+ .where("deleted_at", "is", null)
267
+ .where(sql `trigger->>'kind' = 'event'`)
268
+ .where("expires_at", "<=", now)
269
+ .orderBy("expires_at")
270
+ .orderBy("id")
271
+ .limit(limit)
272
+ .forUpdate()
273
+ .skipLocked()
274
+ .execute();
275
+ return rows.map(toTask);
276
+ });
277
+ }
239
278
  async insertRun(t, run) {
240
279
  const row = await t
241
- .insertInto("scheduled_task_runs")
280
+ .insertInto("automation_runs")
242
281
  .values({
243
282
  id: run.id,
244
- task_id: run.taskId,
283
+ automation_id: run.taskId,
245
284
  org_id: run.orgId,
246
285
  scheduled_for: run.scheduledFor,
286
+ trigger_key: run.scheduledFor === null
287
+ ? run.triggerKey
288
+ : (run.triggerKey ?? run.scheduledFor.toISOString()),
247
289
  trial: run.trial,
248
290
  manual: run.manual ?? false,
249
291
  status: run.status ?? "pending",
@@ -251,10 +293,12 @@ let ScheduledTaskStore = class ScheduledTaskStore {
251
293
  fired_at: run.firedAt ?? null,
252
294
  conversation_id: run.conversationId ?? null,
253
295
  })
254
- .onConflict((oc) => oc.columns(["task_id", "scheduled_for"]).where("trial", "=", false).doNothing())
296
+ .onConflict((oc) => oc.columns(["automation_id", "trigger_key"]).where("trial", "=", false).doNothing())
255
297
  .returningAll()
256
298
  .executeTakeFirst();
257
- return row ? toRun(row) : null;
299
+ if (!row)
300
+ return null;
301
+ return run.scheduledFor === null ? toRun(row) : toTimedRun(row);
258
302
  }
259
303
  /** Patches a task run; null when it does not exist. */
260
304
  // org-filter: by task run id for the fire / settle jobs only; the id is the kernel-written job
@@ -279,14 +323,14 @@ let ScheduledTaskStore = class ScheduledTaskStore {
279
323
  c.missing_grants = JSON.stringify(patch.missingGrants);
280
324
  if (Object.keys(c).length === 0) {
281
325
  const row = await t
282
- .selectFrom("scheduled_task_runs")
326
+ .selectFrom("automation_runs")
283
327
  .selectAll()
284
328
  .where("id", "=", id)
285
329
  .executeTakeFirst();
286
330
  return row ? toRun(row) : null;
287
331
  }
288
332
  const row = await t
289
- .updateTable("scheduled_task_runs")
333
+ .updateTable("automation_runs")
290
334
  .set(c)
291
335
  .where("id", "=", id)
292
336
  .returningAll()
@@ -300,41 +344,46 @@ let ScheduledTaskStore = class ScheduledTaskStore {
300
344
  // org-filter: the fire job's overlap check, by the task id of the run it claimed
301
345
  async latestNonTerminalRun(t, taskId, exceptId) {
302
346
  let q = t
303
- .selectFrom("scheduled_task_runs")
347
+ .selectFrom("automation_runs")
304
348
  .selectAll()
305
- .where("task_id", "=", taskId)
349
+ .where("automation_id", "=", taskId)
306
350
  .where("status", "in", NON_TERMINAL);
307
351
  if (exceptId !== undefined)
308
352
  q = q.where("id", "!=", exceptId);
309
353
  const row = await q
310
- .orderBy("scheduled_for", "desc")
354
+ .orderBy(runOrderMoment, "desc")
311
355
  .orderBy("id", "desc")
312
356
  .limit(1)
313
357
  .executeTakeFirst();
314
358
  return row ? toRun(row) : null;
315
359
  }
316
360
  /**
317
- * Runs of the org fired at or after `since` (the `maxRunsPerHour` count; trial runs included,
318
- * skipped runs not: a run whose queued message a person removed was never run).
361
+ * Runs of the org's automations owned by `accountId` fired at or after `since` (the
362
+ * `maxRunsPerHour` count, per person: collaboration spec §4.7; trial runs included, skipped runs
363
+ * not: a run whose queued message a person removed was never run). Deleted automations count:
364
+ * their runs were made.
319
365
  */
320
- async countFiredSince(t, orgId, since) {
366
+ async countFiredSince(t, orgId, accountId, since) {
321
367
  const row = await t
322
- .selectFrom("scheduled_task_runs")
368
+ .selectFrom("automation_runs as r")
369
+ .innerJoin("automations as a", "a.id", "r.automation_id")
323
370
  .select((eb) => eb.fn.countAll().as("n"))
324
- .where("org_id", "=", orgId)
325
- .where("fired_at", ">=", since)
326
- .where("status", "!=", "skipped")
371
+ .where("r.org_id", "=", orgId)
372
+ .where("a.org_id", "=", orgId)
373
+ .where("a.account_id", "=", accountId)
374
+ .where("r.fired_at", ">=", since)
375
+ .where("r.status", "!=", "skipped")
327
376
  .executeTakeFirstOrThrow();
328
377
  return Number(row.n);
329
378
  }
330
- /** A task's runs, latest tick first. */
331
- // org-filter: by the id of a task its caller read under an org filter (ScheduledTasksService.get)
379
+ /** A task's runs, latest first (by tick, else claim, else record: {@link runOrderMoment}). */
380
+ // org-filter: by the id of a task its caller read under an org filter (AutomationsService.get)
332
381
  async listRuns(t, taskId, limit) {
333
382
  const rows = await t
334
- .selectFrom("scheduled_task_runs")
383
+ .selectFrom("automation_runs")
335
384
  .selectAll()
336
- .where("task_id", "=", taskId)
337
- .orderBy("scheduled_for", "desc")
385
+ .where("automation_id", "=", taskId)
386
+ .orderBy(runOrderMoment, "desc")
338
387
  .orderBy("id", "desc")
339
388
  .limit(limit)
340
389
  .execute();
@@ -342,29 +391,30 @@ let ScheduledTaskStore = class ScheduledTaskStore {
342
391
  }
343
392
  /**
344
393
  * The runs of these tasks of the org with `scheduled_for` in `[from, to)`, trial included, by tick, at most
345
- * `limit` (the calendar; `scheduled_task_runs_task_idx` per task).
394
+ * `limit` (the calendar; `automation_runs_automation_idx` per task).
346
395
  */
347
396
  async listRunsBetween(t, orgId, taskIds, from, to, limit) {
348
397
  if (taskIds.length === 0)
349
398
  return [];
350
399
  const rows = await t
351
- .selectFrom("scheduled_task_runs")
400
+ .selectFrom("automation_runs")
352
401
  .selectAll()
353
402
  .where("org_id", "=", orgId)
354
- .where("task_id", "in", [...taskIds])
403
+ .where("automation_id", "in", [...taskIds])
355
404
  .where("scheduled_for", ">=", from)
356
405
  .where("scheduled_for", "<", to)
357
406
  .orderBy("scheduled_for")
358
407
  .orderBy("id")
359
408
  .limit(limit)
360
409
  .execute();
361
- return rows.map(toRun);
410
+ // the range admits only runs with a tick
411
+ return rows.map(toRun).filter((r) => r.scheduledFor !== null);
362
412
  }
363
413
  async listGrants(t, taskId) {
364
414
  const rows = await t
365
- .selectFrom("scheduled_task_grants")
415
+ .selectFrom("automation_grants")
366
416
  .select(["tool_key", "spec_hash", "connector_id"])
367
- .where("task_id", "=", taskId)
417
+ .where("automation_id", "=", taskId)
368
418
  .orderBy("tool_key")
369
419
  .execute();
370
420
  return rows.map((r) => ({
@@ -376,14 +426,14 @@ let ScheduledTaskStore = class ScheduledTaskStore {
376
426
  /** Adds a grant, replacing the task's grant of the same tool key. */
377
427
  async addGrant(t, taskId, grant) {
378
428
  await t
379
- .insertInto("scheduled_task_grants")
429
+ .insertInto("automation_grants")
380
430
  .values({
381
- task_id: taskId,
431
+ automation_id: taskId,
382
432
  tool_key: grant.toolKey,
383
433
  spec_hash: grant.specHash,
384
434
  connector_id: grant.connectorId,
385
435
  })
386
- .onConflict((oc) => oc.columns(["task_id", "tool_key"]).doUpdateSet({
436
+ .onConflict((oc) => oc.columns(["automation_id", "tool_key"]).doUpdateSet({
387
437
  spec_hash: grant.specHash,
388
438
  connector_id: grant.connectorId,
389
439
  created_at: sql `now()`,
@@ -392,17 +442,17 @@ let ScheduledTaskStore = class ScheduledTaskStore {
392
442
  }
393
443
  async removeGrant(t, taskId, toolKey) {
394
444
  const r = await t
395
- .deleteFrom("scheduled_task_grants")
396
- .where("task_id", "=", taskId)
445
+ .deleteFrom("automation_grants")
446
+ .where("automation_id", "=", taskId)
397
447
  .where("tool_key", "=", toolKey)
398
448
  .executeTakeFirst();
399
449
  return Number(r.numDeletedRows) > 0;
400
450
  }
401
451
  };
402
- ScheduledTaskStore = __decorate([
452
+ AutomationStore = __decorate([
403
453
  Injectable()
404
- ], ScheduledTaskStore);
405
- export { ScheduledTaskStore };
454
+ ], AutomationStore);
455
+ export { AutomationStore };
406
456
  /**
407
457
  * Whether the task holds a grant for this tool as defined now: same key, same spec hash, same
408
458
  * connector (null for plugin and builtin tools). A grant of an older definition does not count.
@@ -410,9 +460,9 @@ export { ScheduledTaskStore };
410
460
  */
411
461
  export async function findTaskGrant(t, taskId, toolKey, specHash, connectorId) {
412
462
  const row = await t
413
- .selectFrom("scheduled_task_grants")
463
+ .selectFrom("automation_grants")
414
464
  .select("tool_key")
415
- .where("task_id", "=", taskId)
465
+ .where("automation_id", "=", taskId)
416
466
  .where("tool_key", "=", toolKey)
417
467
  .where("spec_hash", "=", specHash)
418
468
  .where(sql `connector_id IS NOT DISTINCT FROM ${connectorId}`)
@@ -0,0 +1,6 @@
1
+ /**
2
+ * The automation store alone, so the scanner (worker), the fire job, the HTTP API and the
3
+ * automation tools can each import it without importing one another.
4
+ */
5
+ export declare class AutomationStoreModule {
6
+ }
@@ -5,17 +5,17 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
7
  import { Module } from "@nestjs/common";
8
- import { ScheduledTaskStore } from "./store.js";
8
+ import { AutomationStore } from "./store.js";
9
9
  /**
10
- * The scheduled task store alone, so the scanner (worker), the fire job, the HTTP API and the
11
- * schedule tools can each import it without importing one another.
10
+ * The automation store alone, so the scanner (worker), the fire job, the HTTP API and the
11
+ * automation tools can each import it without importing one another.
12
12
  */
13
- let ScheduledTaskStoreModule = class ScheduledTaskStoreModule {
13
+ let AutomationStoreModule = class AutomationStoreModule {
14
14
  };
15
- ScheduledTaskStoreModule = __decorate([
15
+ AutomationStoreModule = __decorate([
16
16
  Module({
17
- providers: [ScheduledTaskStore],
18
- exports: [ScheduledTaskStore],
17
+ providers: [AutomationStore],
18
+ exports: [AutomationStore],
19
19
  })
20
- ], ScheduledTaskStoreModule);
21
- export { ScheduledTaskStoreModule };
20
+ ], AutomationStoreModule);
21
+ export { AutomationStoreModule };