@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,22 +0,0 @@
1
- import type { KernelSchemaDb } from "../db/kernel-schema.js";
2
- import type { KernelConfigService } from "../infra/config/kernel-config.service.js";
3
- import { type ScheduledTasksSettings } from "../orgs/settings.service.js";
4
- /** The platform's bounds for the org settings (kernel config `schedules.*`). */
5
- export interface ScheduleBounds {
6
- minIntervalFloorMinutes: number;
7
- maxTasksPerMemberCap: number;
8
- maxTasksPerOrgCap: number;
9
- maxRunsPerHourCap: number;
10
- }
11
- /**
12
- * The org's scheduled-task settings after clamping to the platform bounds. `forbiddenToolsUnreadable`:
13
- * the stored list cannot be read (`invalidStoredForbiddenTools`); `forbiddenTools` is then `[]`
14
- * and unattended runs of the org must fail instead of running with nothing forbidden.
15
- */
16
- export type EffectiveScheduleSettings = ScheduledTasksSettings & {
17
- forbiddenToolsUnreadable: boolean;
18
- };
19
- /** An org value outside the platform bounds is clamped: the interval up, the counts down. */
20
- export declare function effectiveScheduleSettings(org: ScheduledTasksSettings, bounds: ScheduleBounds, forbiddenToolsUnreadable?: boolean): EffectiveScheduleSettings;
21
- /** Read the org's effective settings live (PG and the current kernel config). */
22
- export declare function readScheduleSettings(db: KernelSchemaDb, config: Pick<KernelConfigService, "get">, orgId: string): Promise<EffectiveScheduleSettings>;
@@ -1,18 +0,0 @@
1
- import { invalidStoredForbiddenTools, parseOrgSettings, readStoredOrgSettings, } from "../orgs/settings.service.js";
2
- /** An org value outside the platform bounds is clamped: the interval up, the counts down. */
3
- export function effectiveScheduleSettings(org, bounds, forbiddenToolsUnreadable = false) {
4
- return {
5
- ...org,
6
- forbiddenTools: [...org.forbiddenTools],
7
- forbiddenToolsUnreadable,
8
- minIntervalMinutes: Math.max(org.minIntervalMinutes, bounds.minIntervalFloorMinutes),
9
- maxTasksPerMember: Math.min(org.maxTasksPerMember, bounds.maxTasksPerMemberCap),
10
- maxTasksPerOrg: Math.min(org.maxTasksPerOrg, bounds.maxTasksPerOrgCap),
11
- maxRunsPerHour: Math.min(org.maxRunsPerHour, bounds.maxRunsPerHourCap),
12
- };
13
- }
14
- /** Read the org's effective settings live (PG and the current kernel config). */
15
- export async function readScheduleSettings(db, config, orgId) {
16
- const raw = await readStoredOrgSettings(db, orgId);
17
- return effectiveScheduleSettings(parseOrgSettings(raw).scheduledTasks, config.get().schedules, invalidStoredForbiddenTools(raw));
18
- }
@@ -1,8 +0,0 @@
1
- import { ApiError } from "../errors.js";
2
- /** The task is missing, deleted, in another org or not the caller's to see (never 403). */
3
- export declare const scheduleNotFound: () => ApiError;
4
- /** An org admin acting on another person's task in a way only its creator may (spec §10). */
5
- export declare const scheduleForbidden: () => ApiError;
6
- export declare const scheduleDisabled: () => ApiError;
7
- export declare const targetInvalid: (message: string) => ApiError;
8
- export declare const grantNotAllowed: (message: string) => ApiError;
@@ -1,8 +0,0 @@
1
- import { ApiError } from "../errors.js";
2
- /** The task is missing, deleted, in another org or not the caller's to see (never 403). */
3
- export const scheduleNotFound = () => new ApiError("schedule.not_found", "Scheduled task not found");
4
- /** An org admin acting on another person's task in a way only its creator may (spec §10). */
5
- export const scheduleForbidden = () => new ApiError("schedule.forbidden", "Only the task's creator can change its prompt, schedule, target or approved tools");
6
- export const scheduleDisabled = () => new ApiError("schedule.disabled", "Scheduled tasks are turned off in this organization");
7
- export const targetInvalid = (message) => new ApiError("schedule.target_invalid", message);
8
- export const grantNotAllowed = (message) => new ApiError("schedule.grant_not_allowed", message);
@@ -1,81 +0,0 @@
1
- import type { OutboxService } from "../infra/outbox/outbox.service.js";
2
- /** The scheduled tasks' 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 SCHED_SCAN_JOB = "scheduled-task-scan";
8
- /** The one-off job that checks and starts one task run, on `cap-kernel-maintenance`. */
9
- export declare const SCHED_FIRE_JOB = "sched-fire";
10
- /** The data of a {@link SCHED_FIRE_JOB}. */
11
- export interface SchedFireJobData {
12
- taskRunId: string;
13
- }
14
- /** The fire job's id: `sched-fire-<taskRunId>` (no `:`, task run ids are UUIDs). */
15
- export declare const schedFireJobId: (taskRunId: string) => string;
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 declare function oweSchedFire(outbox: Pick<OutboxService, "add">, taskRunId: string, orgId: string): Promise<boolean>;
23
- /**
24
- * The delayed job that settles a task run left pending (ruling R26, fix round 1), on
25
- * `cap-kernel-maintenance`. Two kinds:
26
- * - the claimed check, owed by the fire job's claim: the run tracker links, settles or releases a
27
- * run claimed but never started (`sched-settle-<taskRunId>`, then `-<check>`);
28
- * - the unfired check, owed with the task run itself (the scan's tick, "run now"): a run its fire
29
- * job never claimed (the job failed for good before its claim) is skipped, `missed` for a tick,
30
- * `removed` for a "run now", so it never blocks the task's later ticks as an overlap
31
- * (`sched-settle-<taskRunId>-unfired`).
32
- */
33
- export declare const SCHED_SETTLE_JOB = "sched-settle";
34
- /**
35
- * The data of a {@link SCHED_SETTLE_JOB}: the claimed check (`check` counts the checks before this
36
- * one) or the unfired check.
37
- */
38
- export type SchedSettleJobData = {
39
- taskRunId: string;
40
- check: number;
41
- } | {
42
- taskRunId: string;
43
- unfired: true;
44
- };
45
- /**
46
- * The first claimed check: 10 minutes after the claim, when the start's retries (seconds) are long
47
- * over and the run tracker's start grace (`START_GRACE_MS`, 5 minutes) has passed, so a run that
48
- * left no trace is released at once. Also the unfired check's delay after a "run now".
49
- */
50
- export declare const SCHED_SETTLE_AFTER_MS: number;
51
- /**
52
- * A claimed run still in flight at a check (its item queued, its message posted and not yet taken
53
- * by a run) is checked again hourly. The chain ends with the first check that finds the run
54
- * started, ended or released: the queued item is consumed or removed, the message taken by a run.
55
- * A chain past {@link SCHED_SETTLE_LONG_CHECKS} checks (a week) is logged as a warning, once.
56
- */
57
- export declare const SCHED_SETTLE_RECHECK_MS: number;
58
- /** A week of hourly checks: the claimed check warns at this one (a run in flight that long). */
59
- export declare const SCHED_SETTLE_LONG_CHECKS = 168;
60
- /**
61
- * How long past the org's catch-up window a tick's fire may come before the tick is missed (the
62
- * fire job's own rule, and when a tick's unfired check runs): the scan fires a tick still inside
63
- * the window, and its fire job follows within seconds.
64
- */
65
- export declare const SCHED_LATE_FIRE_SLACK_MS: number;
66
- /** The claimed check's id: `sched-settle-<taskRunId>`, then `-<check>` for the later checks. */
67
- export declare const schedSettleJobId: (taskRunId: string, check: number) => string;
68
- /** The unfired check's id: `sched-settle-<taskRunId>-unfired`. */
69
- export declare const schedSettleUnfiredJobId: (taskRunId: string) => string;
70
- /**
71
- * Owes claimed check number `check` of task run `taskRunId` in the transaction in context, due
72
- * `after` milliseconds from now.
73
- */
74
- export declare function oweSchedSettle(outbox: Pick<OutboxService, "add">, taskRunId: string, orgId: string, check: number, after: number): Promise<boolean>;
75
- /**
76
- * Owes the unfired check of task run `taskRunId` in the transaction that writes the run: for a
77
- * tick of the schedule (`catchUpWindowMinutes` given) when the fire job would itself call the
78
- * tick missed (its tick plus the window plus {@link SCHED_LATE_FIRE_SLACK_MS}, at least
79
- * {@link SCHED_SETTLE_AFTER_MS} on); for a "run now" (null) {@link SCHED_SETTLE_AFTER_MS} on.
80
- */
81
- export declare function oweSchedSettleUnfired(outbox: Pick<OutboxService, "add">, taskRunId: string, orgId: string, scheduledFor: Date, catchUpWindowMinutes: number | null): Promise<boolean>;
@@ -1,95 +0,0 @@
1
- /** The scheduled tasks' 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 SCHED_SCAN_JOB = "scheduled-task-scan";
7
- /** The one-off job that checks and starts one task run, on `cap-kernel-maintenance`. */
8
- export const SCHED_FIRE_JOB = "sched-fire";
9
- /** The fire job's id: `sched-fire-<taskRunId>` (no `:`, task run ids are UUIDs). */
10
- export const schedFireJobId = (taskRunId) => `${SCHED_FIRE_JOB}-${taskRunId}`;
11
- /**
12
- * Owes the fire job of task run `taskRunId` in the transaction in context (transactional outbox
13
- * spec §8.1): the transaction that writes a pending task run (the scan's tick, a person's "run
14
- * now") owes its fire, so a tick is never left unfired. The job's options are its outbox registry
15
- * entry's; the fire is idempotent on the task run's status.
16
- */
17
- export function oweSchedFire(outbox, taskRunId, orgId) {
18
- return outbox.add({
19
- queue: "cap-kernel-maintenance",
20
- name: SCHED_FIRE_JOB,
21
- jobId: schedFireJobId(taskRunId),
22
- data: { taskRunId },
23
- orgId,
24
- });
25
- }
26
- /**
27
- * The delayed job that settles a task run left pending (ruling R26, fix round 1), on
28
- * `cap-kernel-maintenance`. Two kinds:
29
- * - the claimed check, owed by the fire job's claim: the run tracker links, settles or releases a
30
- * run claimed but never started (`sched-settle-<taskRunId>`, then `-<check>`);
31
- * - the unfired check, owed with the task run itself (the scan's tick, "run now"): a run its fire
32
- * job never claimed (the job failed for good before its claim) is skipped, `missed` for a tick,
33
- * `removed` for a "run now", so it never blocks the task's later ticks as an overlap
34
- * (`sched-settle-<taskRunId>-unfired`).
35
- */
36
- export const SCHED_SETTLE_JOB = "sched-settle";
37
- /**
38
- * The first claimed check: 10 minutes after the claim, when the start's retries (seconds) are long
39
- * over and the run tracker's start grace (`START_GRACE_MS`, 5 minutes) has passed, so a run that
40
- * left no trace is released at once. Also the unfired check's delay after a "run now".
41
- */
42
- export const SCHED_SETTLE_AFTER_MS = 10 * 60_000;
43
- /**
44
- * A claimed run still in flight at a check (its item queued, its message posted and not yet taken
45
- * by a run) is checked again hourly. The chain ends with the first check that finds the run
46
- * started, ended or released: the queued item is consumed or removed, the message taken by a run.
47
- * A chain past {@link SCHED_SETTLE_LONG_CHECKS} checks (a week) is logged as a warning, once.
48
- */
49
- export const SCHED_SETTLE_RECHECK_MS = 60 * 60_000;
50
- /** A week of hourly checks: the claimed check warns at this one (a run in flight that long). */
51
- export const SCHED_SETTLE_LONG_CHECKS = 168;
52
- /**
53
- * How long past the org's catch-up window a tick's fire may come before the tick is missed (the
54
- * fire job's own rule, and when a tick's unfired check runs): the scan fires a tick still inside
55
- * the window, and its fire job follows within seconds.
56
- */
57
- export const SCHED_LATE_FIRE_SLACK_MS = 10 * 60_000;
58
- /** The claimed check's id: `sched-settle-<taskRunId>`, then `-<check>` for the later checks. */
59
- export const schedSettleJobId = (taskRunId, check) => check === 0 ? `${SCHED_SETTLE_JOB}-${taskRunId}` : `${SCHED_SETTLE_JOB}-${taskRunId}-${check}`;
60
- /** The unfired check's id: `sched-settle-<taskRunId>-unfired`. */
61
- export const schedSettleUnfiredJobId = (taskRunId) => `${SCHED_SETTLE_JOB}-${taskRunId}-unfired`;
62
- /**
63
- * Owes claimed check number `check` of task run `taskRunId` in the transaction in context, due
64
- * `after` milliseconds from now.
65
- */
66
- export function oweSchedSettle(outbox, taskRunId, orgId, check, after) {
67
- return outbox.add({
68
- queue: "cap-kernel-maintenance",
69
- name: SCHED_SETTLE_JOB,
70
- jobId: schedSettleJobId(taskRunId, check),
71
- data: { taskRunId, check },
72
- runAt: new Date(Date.now() + after),
73
- orgId,
74
- });
75
- }
76
- /**
77
- * Owes the unfired check of task run `taskRunId` in the transaction that writes the run: for a
78
- * tick of the schedule (`catchUpWindowMinutes` given) when the fire job would itself call the
79
- * tick missed (its tick plus the window plus {@link SCHED_LATE_FIRE_SLACK_MS}, at least
80
- * {@link SCHED_SETTLE_AFTER_MS} on); for a "run now" (null) {@link SCHED_SETTLE_AFTER_MS} on.
81
- */
82
- export function oweSchedSettleUnfired(outbox, taskRunId, orgId, scheduledFor, catchUpWindowMinutes) {
83
- const soonest = Date.now() + SCHED_SETTLE_AFTER_MS;
84
- const at = catchUpWindowMinutes === null
85
- ? soonest
86
- : Math.max(soonest, scheduledFor.getTime() + catchUpWindowMinutes * 60_000 + SCHED_LATE_FIRE_SLACK_MS);
87
- return outbox.add({
88
- queue: "cap-kernel-maintenance",
89
- name: SCHED_SETTLE_JOB,
90
- jobId: schedSettleUnfiredJobId(taskRunId),
91
- data: { taskRunId, unfired: true },
92
- runAt: new Date(at),
93
- orgId,
94
- });
95
- }
@@ -1,16 +0,0 @@
1
- import { type OnModuleInit } from "@nestjs/common";
2
- import { ScheduleToolsPort } from "../tools/builtins/schedules.js";
3
- import { ScheduledTasksService } from "./scheduled-tasks.service.js";
4
- /**
5
- * Scheduled tasks for people (spec 2026-11-11): the service the HTTP API, the Platform operations
6
- * and the schedule tools share. ToolsModule: grants resolve the agent's tools (`ToolRegistry`);
7
- * ExtensionRegistryModule: a group chat target needs its channel's `features.threads`. At start
8
- * it binds the service into the schedule tools' port (`ScheduleToolsPort`): the tools cannot
9
- * inject it, since this module imports theirs.
10
- */
11
- export declare class SchedulesModule implements OnModuleInit {
12
- private readonly port;
13
- private readonly service;
14
- constructor(port: ScheduleToolsPort, service: ScheduledTasksService);
15
- onModuleInit(): void;
16
- }
@@ -1,213 +0,0 @@
1
- import { z } from "zod";
2
- import { createZodDto } from "../infra/validation/create-zod-dto.js";
3
- import { IanaTimeZone } from "../time-zone.js";
4
- /** Longest task name and prompt, in characters (the same bounds as the service). */
5
- export const SCHEDULE_NAME_MAX = 200;
6
- export const SCHEDULE_PROMPT_MAX = 20_000;
7
- const Id = z.string().min(1).max(100);
8
- /**
9
- * When a task fires, before the zone is fixed: a 5-field cron, or a local date-time
10
- * (`2026-12-01T09:00`, no offset) for a one-off run.
11
- */
12
- export const ScheduleTriggerInput = z.discriminatedUnion("kind", [
13
- z.object({ kind: z.literal("cron"), expr: z.string().trim().min(1).max(200) }).strict(),
14
- z.object({ kind: z.literal("once"), at: z.string().trim().min(1).max(40) }).strict(),
15
- ]);
16
- /** The IM chat a `chat`-mode task posts into. */
17
- export const ScheduleChatInput = z
18
- .object({
19
- connectionId: Id,
20
- chatId: z.string().min(1).max(500),
21
- chatType: z.enum(["dm", "group"]),
22
- })
23
- .strict();
24
- /**
25
- * A model pick: the same `{ provider, model }` an agent's definition uses, its strings bounded on
26
- * the fields (so the JSON schema shows the bounds). `new` mode only.
27
- */
28
- export const ScheduleModel = z
29
- .object({ provider: z.string().min(1).max(100), model: z.string().min(1).max(200) })
30
- .strict();
31
- export const ScheduleEffort = z.enum(["low", "medium", "high", "xhigh", "max"]);
32
- /** An end time as an ISO 8601 timestamp with an offset (`2026-12-31T00:00:00Z`). */
33
- const ExpiresAt = z.string().datetime({ offset: true });
34
- /**
35
- * A tool the creator pre-approves for the task, or a domain as `network:<host>` (network
36
- * ask-to-allow spec §8.1: one exact host; a host name is at most 253 characters).
37
- */
38
- export const ScheduleGrantInput = z
39
- .object({ toolKey: z.string().trim().min(1).max(270), connectorId: Id.optional() })
40
- .strict();
41
- const Name = z.string().trim().min(1).max(SCHEDULE_NAME_MAX);
42
- const Prompt = z.string().trim().min(1).max(SCHEDULE_PROMPT_MAX);
43
- const Mode = z.enum(["new", "conversation", "chat"]);
44
- export const ScheduleCreateBody = z
45
- .object({
46
- agentId: Id,
47
- name: Name,
48
- prompt: Prompt,
49
- trigger: ScheduleTriggerInput,
50
- /** The browser's zone: used only when the account has no zone of its own. */
51
- timeZone: IanaTimeZone.optional(),
52
- mode: Mode,
53
- conversationId: Id.optional(),
54
- chat: ScheduleChatInput.optional(),
55
- /** `new` mode only, as `effort` and `deviceId` (a conversation or chat task uses its own). */
56
- model: ScheduleModel.optional(),
57
- effort: ScheduleEffort.optional(),
58
- deviceId: Id.optional(),
59
- expiresAt: ExpiresAt.nullable().optional(),
60
- grants: z.array(ScheduleGrantInput).max(50).optional(),
61
- })
62
- .strict();
63
- export const ScheduleUpdateBody = z
64
- .object({
65
- agentId: Id.optional(),
66
- name: Name.optional(),
67
- prompt: Prompt.optional(),
68
- trigger: ScheduleTriggerInput.optional(),
69
- /** The task's own zone (an explicit change). */
70
- timeZone: IanaTimeZone.optional(),
71
- mode: Mode.optional(),
72
- conversationId: Id.nullable().optional(),
73
- chat: ScheduleChatInput.nullable().optional(),
74
- /** `new` mode only; switching to another mode clears them. */
75
- model: ScheduleModel.nullable().optional(),
76
- effort: ScheduleEffort.nullable().optional(),
77
- deviceId: Id.nullable().optional(),
78
- /** Null: the org's default expiry for a cron task (as on create), else never. */
79
- expiresAt: ExpiresAt.nullable().optional(),
80
- })
81
- .strict();
82
- export const ScheduleListQuery = z
83
- .object({
84
- all: z.enum(["true", "false"]).optional(),
85
- /** Only the tasks that post into this conversation (the conversation's side panel). */
86
- conversationId: Id.optional(),
87
- })
88
- .strict();
89
- /** An instant with an offset (`2026-11-09T00:00:00+08:00`). */
90
- const Instant = z.string().datetime({ offset: true });
91
- /**
92
- * The calendar's range (`from < to`, at most 43 days, 2 with `taskId`: checked by the service),
93
- * the viewer's zone (local days for the collapse and the cut), `all` (org admins) and `taskId`
94
- * (one task, never collapsed).
95
- */
96
- export const ScheduleCalendarQuery = z
97
- .object({
98
- from: Instant,
99
- to: Instant,
100
- timeZone: IanaTimeZone,
101
- all: z.enum(["true", "false"]).optional(),
102
- taskId: Id.optional(),
103
- })
104
- .strict();
105
- export const ScheduleChatTargetsQuery = z.object({}).strict();
106
- export const ScheduleGrantableQuery = z.object({ agentId: Id }).strict();
107
- export const ScheduleParams = z.object({ id: Id });
108
- export const ScheduleRunBody = z.object({ trial: z.boolean().optional() }).strict();
109
- export const ScheduleRunsQuery = z
110
- .object({ limit: z.coerce.number().int().min(1).max(200).optional() })
111
- .strict();
112
- export const ScheduleGrantsBody = z
113
- .object({ grants: z.array(ScheduleGrantInput).min(1).max(50) })
114
- .strict();
115
- export const ScheduleGrantParams = z.object({ id: Id, toolKey: z.string().min(1).max(270) });
116
- export class ScheduleCreateBodyDto extends createZodDto(ScheduleCreateBody, "ScheduleCreateBody") {
117
- }
118
- export class ScheduleUpdateBodyDto extends createZodDto(ScheduleUpdateBody, "ScheduleUpdateBody") {
119
- }
120
- export class ScheduleListQueryDto extends createZodDto(ScheduleListQuery, "ScheduleListQuery") {
121
- }
122
- export class ScheduleCalendarQueryDto extends createZodDto(ScheduleCalendarQuery, "ScheduleCalendarQuery") {
123
- }
124
- export class ScheduleChatTargetsQueryDto extends createZodDto(ScheduleChatTargetsQuery, "ScheduleChatTargetsQuery") {
125
- }
126
- export class ScheduleGrantableQueryDto extends createZodDto(ScheduleGrantableQuery, "ScheduleGrantableQuery") {
127
- }
128
- export class ScheduleParamsDto extends createZodDto(ScheduleParams, "ScheduleParams") {
129
- }
130
- export class ScheduleRunBodyDto extends createZodDto(ScheduleRunBody, "ScheduleRunBody") {
131
- }
132
- export class ScheduleRunsQueryDto extends createZodDto(ScheduleRunsQuery, "ScheduleRunsQuery") {
133
- }
134
- export class ScheduleGrantsBodyDto extends createZodDto(ScheduleGrantsBody, "ScheduleGrantsBody") {
135
- }
136
- export class ScheduleGrantParamsDto extends createZodDto(ScheduleGrantParams, "ScheduleGrantParams") {
137
- }
138
- /** The schedule routes the Platform operations mirror (`check:platform-ops` keys). */
139
- export const SCHEDULE_ROUTES = [
140
- { route: "GET /api/v1/orgs/:orgId/schedules", query: ScheduleListQuery },
141
- { route: "GET /api/v1/orgs/:orgId/schedules/calendar", query: ScheduleCalendarQuery },
142
- { route: "GET /api/v1/orgs/:orgId/schedules/chat-targets", query: ScheduleChatTargetsQuery },
143
- { route: "GET /api/v1/orgs/:orgId/schedules/grantable-tools", query: ScheduleGrantableQuery },
144
- { route: "POST /api/v1/orgs/:orgId/schedules", body: ScheduleCreateBody },
145
- { route: "GET /api/v1/orgs/:orgId/schedules/:id", params: ScheduleParams },
146
- {
147
- route: "PATCH /api/v1/orgs/:orgId/schedules/:id",
148
- params: ScheduleParams,
149
- body: ScheduleUpdateBody,
150
- },
151
- { route: "DELETE /api/v1/orgs/:orgId/schedules/:id", params: ScheduleParams },
152
- { route: "POST /api/v1/orgs/:orgId/schedules/:id/pause", params: ScheduleParams },
153
- { route: "POST /api/v1/orgs/:orgId/schedules/:id/resume", params: ScheduleParams },
154
- {
155
- route: "POST /api/v1/orgs/:orgId/schedules/:id/run",
156
- params: ScheduleParams,
157
- body: ScheduleRunBody,
158
- },
159
- {
160
- route: "GET /api/v1/orgs/:orgId/schedules/:id/runs",
161
- params: ScheduleParams,
162
- query: ScheduleRunsQuery,
163
- },
164
- { route: "GET /api/v1/orgs/:orgId/schedules/:id/grants", params: ScheduleParams },
165
- {
166
- route: "POST /api/v1/orgs/:orgId/schedules/:id/grants",
167
- params: ScheduleParams,
168
- body: ScheduleGrantsBody,
169
- },
170
- {
171
- route: "DELETE /api/v1/orgs/:orgId/schedules/:id/grants/:toolKey",
172
- params: ScheduleGrantParams,
173
- },
174
- ];
175
- /** The object without its undefined properties (the service's inputs take absent, not undefined). */
176
- export function defined(o) {
177
- return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
178
- }
179
- /** A grant list as the service's input. */
180
- export const grantsOf = (grants) => grants.map((g) => defined(g));
181
- /**
182
- * A create body as the service's input. `timeZone` is the browser zone and is not part of it: the
183
- * service uses it only when the account has no zone (spec §5), so it is returned separately.
184
- */
185
- export function createInputOf(body) {
186
- const { timeZone, expiresAt, grants, ...rest } = body;
187
- return {
188
- input: {
189
- ...defined(rest),
190
- ...(expiresAt !== undefined && {
191
- expiresAt: expiresAt === null ? null : new Date(expiresAt),
192
- }),
193
- ...(grants !== undefined && { grants: grantsOf(grants) }),
194
- },
195
- browserTimeZone: timeZone,
196
- };
197
- }
198
- /** A patch body as the service's input (an explicit `timeZone` changes the task's zone). */
199
- export function updateInputOf(body) {
200
- const { expiresAt, ...rest } = body;
201
- return {
202
- ...defined(rest),
203
- ...(expiresAt !== undefined && { expiresAt: expiresAt === null ? null : new Date(expiresAt) }),
204
- };
205
- }
206
- /** A calendar query as the service's input. */
207
- export const calendarInputOf = (q) => ({
208
- from: new Date(q.from),
209
- to: new Date(q.to),
210
- timeZone: q.timeZone,
211
- all: q.all === "true",
212
- ...(q.taskId !== undefined && { taskId: q.taskId }),
213
- });
@@ -1,6 +0,0 @@
1
- /**
2
- * The scheduled task store alone, so the scanner (worker), the fire job, the HTTP API and the
3
- * schedule tools can each import it without importing one another.
4
- */
5
- export declare class ScheduledTaskStoreModule {
6
- }
@@ -1,110 +0,0 @@
1
- import type { ModelRef } from "../agents/definition.js";
2
- /**
3
- * Scheduled tasks (spec 2026-11-11-scheduled-tasks-design.md §4): the shapes the kernel reads and
4
- * writes. The rows live in `capybara.scheduled_tasks`, `scheduled_task_runs` and
5
- * `scheduled_task_grants` (migration 0113).
6
- */
7
- /** When a task fires: a 5-field cron, or once at a local date-time, both in an IANA zone (§5). */
8
- export type ScheduleTrigger = {
9
- kind: "cron";
10
- expr: string;
11
- timeZone: string;
12
- } | {
13
- kind: "once";
14
- at: string;
15
- timeZone: string;
16
- };
17
- /** Where a run posts: a new conversation, one existing conversation, or an IM chat (§3). */
18
- export type ScheduleMode = "new" | "conversation" | "chat";
19
- /** The IM chat a `chat`-mode task posts into. */
20
- export interface ScheduleChat {
21
- connectionId: string;
22
- chatId: string;
23
- chatType: "dm" | "group";
24
- }
25
- export type ScheduleStatus = "active" | "paused" | "ended";
26
- export type SchedulePauseReason = "creator" | "admin" | "failures" | "creator_inactive" | "agent_gone" | "target_gone" | "conversation_gone" | "org_disabled" | "expired"
27
- /** The stored trigger cannot be evaluated (the scanner stops selecting the task). */
28
- | "invalid_schedule";
29
- export type TaskRunStatus = "pending" | "running" | "completed" | "failed" | "skipped";
30
- export type TaskRunSkipReason = "missed" | "overlap" | "rate_limited" | "queue_paused" | "removed" | "device_offline" | "creator_inactive" | "agent_gone" | "target_gone" | "conversation_gone" | "org_disabled"
31
- /** The channel's extension is disabled in the org: the task stays active. */
32
- | "plugin_disabled"
33
- /** A group chat on a channel without threads (a group run needs its own thread). */
34
- | "unsupported_target"
35
- /** The task was deleted after the run was recorded. */
36
- | "task_deleted"
37
- /** The task was paused after the scan recorded the tick (a "run now" still runs). */
38
- | "task_paused";
39
- /** A task as the kernel reads it: the columns of `capybara.scheduled_tasks`, camel-cased. */
40
- export interface ScheduledTask {
41
- id: string;
42
- orgId: string;
43
- /** The creator: every run acts as this account. */
44
- accountId: string;
45
- agentId: string;
46
- name: string;
47
- prompt: string;
48
- trigger: ScheduleTrigger;
49
- mode: ScheduleMode;
50
- /** The target conversation (`conversation` mode only). */
51
- conversationId: string | null;
52
- /** The target chat (`chat` mode only). */
53
- chat: ScheduleChat | null;
54
- /** Null = the agent's default model. */
55
- model: ModelRef | null;
56
- /** Null = the agent's default effort. */
57
- effort: string | null;
58
- /** Null = the default pick for new conversations. */
59
- deviceId: string | null;
60
- status: ScheduleStatus;
61
- pauseReason: SchedulePauseReason | null;
62
- /** The account that paused the task (creator or admin); null when not paused by a person. */
63
- pausedBy: string | null;
64
- /** The next fire time, jitter included; null when the task is not active. */
65
- nextRunAt: Date | null;
66
- jitterSeconds: number;
67
- expiresAt: Date | null;
68
- consecutiveFailures: number;
69
- createdAt: Date;
70
- updatedAt: Date;
71
- }
72
- /** One tick of a task (or a trial run): the columns of `capybara.scheduled_task_runs`. */
73
- export interface TaskRun {
74
- id: string;
75
- taskId: string;
76
- orgId: string;
77
- /** The tick, before jitter (for a trial run: when it was requested). */
78
- scheduledFor: Date;
79
- firedAt: Date | null;
80
- trial: boolean;
81
- /** A person's "run now" (trial or not), outside the schedule. */
82
- manual: boolean;
83
- conversationId: string | null;
84
- runId: string | null;
85
- status: TaskRunStatus;
86
- skipReason: TaskRunSkipReason | null;
87
- error: string | null;
88
- /** Tool keys refused for lack of a grant (§7). */
89
- missingGrants: string[];
90
- }
91
- /**
92
- * The marker a task's message carries (`conversation_queue.schedule`, then `user_message`): not
93
- * model-visible as such; `taskName` and `localTime` feed the English header the model sees.
94
- */
95
- export interface ScheduleMarker {
96
- taskId: string;
97
- taskRunId: string;
98
- /** ISO timestamp of the tick. */
99
- scheduledFor: string;
100
- trial: boolean;
101
- taskName: string;
102
- /** The tick in the task's zone, as the header shows it. */
103
- localTime: string;
104
- }
105
- /** A remembered "always allow" of a task (same meaning as `conversation_tool_grants`). */
106
- export interface ScheduledTaskGrant {
107
- toolKey: string;
108
- specHash: string;
109
- connectorId: string | null;
110
- }
File without changes