@capybara-hq/kernel 0.1.0-rc.23 → 0.1.0-rc.25

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 (293) hide show
  1. package/bootstrap.json +25 -25
  2. package/dist/admin/admin.module.d.ts +2 -1
  3. package/dist/admin/admin.module.js +9 -3
  4. package/dist/admin/health.service.d.ts +2 -2
  5. package/dist/admin/health.service.js +6 -2
  6. package/dist/admin/outbox-dead.controller.d.ts +23 -0
  7. package/dist/admin/outbox-dead.controller.js +62 -0
  8. package/dist/admin/outbox-dead.schemas.d.ts +8 -0
  9. package/dist/admin/outbox-dead.schemas.js +18 -0
  10. package/dist/admin/outbox-dead.service.d.ts +66 -0
  11. package/dist/admin/outbox-dead.service.js +179 -0
  12. package/dist/admin/overview.service.d.ts +40 -5
  13. package/dist/admin/overview.service.js +41 -7
  14. package/dist/agents/agents.module.js +3 -0
  15. package/dist/agents/agents.service.d.ts +6 -1
  16. package/dist/agents/agents.service.js +11 -2
  17. package/dist/agents/from-template.js +1 -1
  18. package/dist/background/finish.d.ts +5 -2
  19. package/dist/background/finish.js +14 -3
  20. package/dist/background/launch.d.ts +2 -1
  21. package/dist/background/launch.js +5 -2
  22. package/dist/background/store.d.ts +8 -0
  23. package/dist/background/store.js +24 -0
  24. package/dist/catalog/admin-catalog.controller.d.ts +21 -2
  25. package/dist/catalog/admin-catalog.controller.js +45 -6
  26. package/dist/catalog/admin-catalog.schemas.d.ts +5 -0
  27. package/dist/catalog/admin-catalog.schemas.js +8 -0
  28. package/dist/catalog/catalog-import.module.js +0 -4
  29. package/dist/catalog/catalog-scope.service.d.ts +63 -26
  30. package/dist/catalog/catalog-scope.service.js +146 -71
  31. package/dist/catalog/catalog.module.d.ts +2 -1
  32. package/dist/catalog/catalog.module.js +9 -4
  33. package/dist/catalog/catalog.service.d.ts +11 -4
  34. package/dist/catalog/catalog.service.js +21 -5
  35. package/dist/catalog/cli.d.ts +4 -3
  36. package/dist/catalog/cli.js +8 -13
  37. package/dist/catalog/job-ids.d.ts +8 -20
  38. package/dist/catalog/job-ids.js +11 -16
  39. package/dist/catalog/marketplaces.service.d.ts +1 -1
  40. package/dist/catalog/org-plugin-uploads.d.ts +11 -4
  41. package/dist/catalog/org-plugin-uploads.js +21 -6
  42. package/dist/catalog/owned-items.d.ts +6 -13
  43. package/dist/catalog/owned-items.js +11 -29
  44. package/dist/catalog/owned-plugin-cleanup.d.ts +8 -17
  45. package/dist/catalog/owned-plugin-cleanup.js +12 -66
  46. package/dist/catalog/platform-components.d.ts +56 -34
  47. package/dist/catalog/platform-components.js +68 -78
  48. package/dist/catalog/platform-import-update.d.ts +50 -0
  49. package/dist/catalog/platform-import-update.js +116 -0
  50. package/dist/catalog/plugin-import.d.ts +64 -26
  51. package/dist/catalog/plugin-import.js +113 -77
  52. package/dist/catalog/plugin-upgrade.d.ts +22 -4
  53. package/dist/catalog/plugin-upgrade.js +26 -19
  54. package/dist/catalog/scope-jobs.d.ts +59 -0
  55. package/dist/catalog/scope-jobs.js +65 -0
  56. package/dist/catalog/scope-sync-workers.d.ts +22 -0
  57. package/dist/catalog/scope-sync-workers.js +58 -2
  58. package/dist/catalog/scope-sync.d.ts +40 -65
  59. package/dist/catalog/scope-sync.js +114 -122
  60. package/dist/channels/channels.module.d.ts +5 -2
  61. package/dist/channels/channels.module.js +9 -2
  62. package/dist/channels/deliver-job.d.ts +25 -0
  63. package/dist/channels/deliver-job.js +25 -0
  64. package/dist/channels/inbound.d.ts +1 -1
  65. package/dist/channels/inbound.js +4 -17
  66. package/dist/channels/jobs.d.ts +28 -18
  67. package/dist/channels/jobs.js +63 -26
  68. package/dist/channels/threads.d.ts +4 -3
  69. package/dist/channels/threads.js +4 -3
  70. package/dist/config/schema.d.ts +0 -3
  71. package/dist/config/schema.js +11 -20
  72. package/dist/conversations/agent-tree.d.ts +16 -0
  73. package/dist/conversations/agent-tree.js +36 -0
  74. package/dist/conversations/conversations.controller.js +4 -2
  75. package/dist/conversations/conversations.service.d.ts +61 -19
  76. package/dist/conversations/conversations.service.js +69 -43
  77. package/dist/conversations/event-log.d.ts +7 -10
  78. package/dist/conversations/event-log.js +24 -15
  79. package/dist/conversations/events.d.ts +21 -0
  80. package/dist/conversations/events.js +26 -2
  81. package/dist/conversations/queue.service.d.ts +8 -3
  82. package/dist/conversations/queue.service.js +37 -13
  83. package/dist/conversations/source.d.ts +10 -0
  84. package/dist/conversations/source.js +17 -0
  85. package/dist/conversations/titles.d.ts +21 -8
  86. package/dist/conversations/titles.js +42 -15
  87. package/dist/conversations/tree.d.ts +10 -0
  88. package/dist/conversations/tree.js +35 -0
  89. package/dist/db/kernel-schema.d.ts +35 -22
  90. package/dist/devices/devices.service.d.ts +23 -11
  91. package/dist/devices/devices.service.js +34 -26
  92. package/dist/errors.d.ts +11 -0
  93. package/dist/errors.js +11 -0
  94. package/dist/evals/run-replay.d.ts +13 -2
  95. package/dist/evals/run-replay.js +42 -9
  96. package/dist/evals/snapshot.d.ts +7 -1
  97. package/dist/evals/snapshot.js +2 -1
  98. package/dist/extensions/extensions.module.js +3 -0
  99. package/dist/extensions/hooks.d.ts +22 -3
  100. package/dist/extensions/hooks.js +15 -1
  101. package/dist/extensions/lifecycle-hooks.d.ts +99 -0
  102. package/dist/extensions/lifecycle-hooks.js +273 -0
  103. package/dist/extensions/lifecycle.d.ts +6 -1
  104. package/dist/extensions/lifecycle.js +9 -2
  105. package/dist/extensions/org-config.d.ts +13 -1
  106. package/dist/extensions/org-config.js +19 -2
  107. package/dist/extensions/permission-view.d.ts +9 -5
  108. package/dist/extensions/permission-view.js +9 -4
  109. package/dist/extensions/point-defaults.d.ts +28 -1
  110. package/dist/extensions/point-defaults.js +44 -13
  111. package/dist/extensions/usage-subscribers.d.ts +43 -0
  112. package/dist/extensions/usage-subscribers.js +108 -0
  113. package/dist/http/api.module.js +2 -0
  114. package/dist/inbox/inbox.service.d.ts +6 -20
  115. package/dist/inbox/inbox.service.js +0 -3
  116. package/dist/inbox/org-notices.d.ts +1 -1
  117. package/dist/inbox/platform-inbox.service.d.ts +3 -32
  118. package/dist/inbox/platform-inbox.service.js +5 -54
  119. package/dist/index.d.ts +1 -0
  120. package/dist/index.js +1 -0
  121. package/dist/infra/cli/kernel-cli.module.js +5 -0
  122. package/dist/infra/events/kernel-events.d.ts +9 -35
  123. package/dist/infra/outbox/outbox-relay.module.d.ts +6 -0
  124. package/dist/infra/outbox/outbox-relay.module.js +18 -0
  125. package/dist/infra/outbox/outbox.dispatcher.d.ts +103 -0
  126. package/dist/infra/outbox/outbox.dispatcher.js +310 -0
  127. package/dist/infra/outbox/outbox.module.d.ts +24 -0
  128. package/dist/infra/outbox/outbox.module.js +50 -0
  129. package/dist/infra/outbox/outbox.relay.d.ts +31 -0
  130. package/dist/infra/outbox/outbox.relay.js +76 -0
  131. package/dist/infra/outbox/outbox.service.d.ts +51 -0
  132. package/dist/infra/outbox/outbox.service.js +131 -0
  133. package/dist/infra/outbox/registry.d.ts +244 -0
  134. package/dist/infra/outbox/registry.js +234 -0
  135. package/dist/infra/queue/queue-names.d.ts +8 -0
  136. package/dist/infra/queue/queue-names.js +17 -0
  137. package/dist/kernel.module.js +3 -0
  138. package/dist/local-devices/device-policy.service.d.ts +4 -0
  139. package/dist/local-devices/device-policy.service.js +61 -3
  140. package/dist/local-devices/machines.d.ts +13 -5
  141. package/dist/local-devices/machines.js +45 -7
  142. package/dist/local-devices/policy-summary.d.ts +44 -1
  143. package/dist/local-devices/policy-summary.js +69 -0
  144. package/dist/models/embeddings.d.ts +9 -9
  145. package/dist/models/embeddings.js +11 -18
  146. package/dist/models/models.module.js +3 -0
  147. package/dist/orgs/directory-sync.js +1 -1
  148. package/dist/orgs/members.service.js +1 -1
  149. package/dist/orgs/offboarding.d.ts +50 -57
  150. package/dist/orgs/offboarding.js +117 -162
  151. package/dist/orgs/orgs.module.js +3 -0
  152. package/dist/orgs/orgs.service.d.ts +11 -5
  153. package/dist/orgs/orgs.service.js +22 -6
  154. package/dist/overrides.d.ts +23 -3
  155. package/dist/overrides.js +12 -2
  156. package/dist/platform/exclusions.js +5 -0
  157. package/dist/platform/ops/admin.ops.js +18 -2
  158. package/dist/platform/ops/catalog-admin.ops.d.ts +3 -1
  159. package/dist/platform/ops/catalog-admin.ops.js +32 -4
  160. package/dist/platform/ops/inbox.ops.js +4 -20
  161. package/dist/platform/ops/outbox.ops.d.ts +21 -0
  162. package/dist/platform/ops/outbox.ops.js +125 -0
  163. package/dist/platform/ops/platform.ops.d.ts +1 -0
  164. package/dist/platform/ops/platform.ops.js +6 -3
  165. package/dist/platform/ops/schedules.ops.js +40 -1
  166. package/dist/platform/platform-ops.module.d.ts +2 -1
  167. package/dist/platform/platform-ops.module.js +3 -0
  168. package/dist/platform/route-schemas.js +3 -0
  169. package/dist/roles/worker-role.d.ts +10 -5
  170. package/dist/roles/worker-role.js +19 -9
  171. package/dist/roles/worker.module.js +11 -8
  172. package/dist/runtime/context/build.d.ts +3 -0
  173. package/dist/runtime/context/build.js +15 -0
  174. package/dist/runtime/context/plugin-context.d.ts +20 -0
  175. package/dist/runtime/context/plugin-context.js +30 -0
  176. package/dist/runtime/langgraph-loop.js +17 -5
  177. package/dist/runtime/limits.d.ts +15 -0
  178. package/dist/runtime/limits.js +25 -0
  179. package/dist/runtime/loop.d.ts +16 -3
  180. package/dist/runtime/loop.js +2 -1
  181. package/dist/runtime/maintenance.d.ts +7 -3
  182. package/dist/runtime/maintenance.js +52 -12
  183. package/dist/runtime/model-call.d.ts +20 -17
  184. package/dist/runtime/model-call.js +15 -24
  185. package/dist/runtime/plugin-context.service.d.ts +69 -0
  186. package/dist/runtime/plugin-context.service.js +208 -0
  187. package/dist/runtime/queue-lost-jobs.d.ts +24 -0
  188. package/dist/runtime/queue-lost-jobs.js +51 -0
  189. package/dist/runtime/run-control.d.ts +47 -5
  190. package/dist/runtime/run-control.js +79 -12
  191. package/dist/runtime/run-finish.d.ts +39 -12
  192. package/dist/runtime/run-finish.js +97 -14
  193. package/dist/runtime/run-jobs.d.ts +95 -13
  194. package/dist/runtime/run-jobs.js +199 -28
  195. package/dist/runtime/run-orphan-check.job.d.ts +142 -0
  196. package/dist/runtime/run-orphan-check.job.js +339 -0
  197. package/dist/runtime/run-processors.d.ts +16 -9
  198. package/dist/runtime/run-processors.js +1 -1
  199. package/dist/runtime/run-queue.d.ts +75 -53
  200. package/dist/runtime/run-queue.js +189 -263
  201. package/dist/runtime/run-steps.d.ts +1 -2
  202. package/dist/runtime/run-steps.js +8 -11
  203. package/dist/runtime/run-worker.d.ts +26 -16
  204. package/dist/runtime/run-worker.js +132 -37
  205. package/dist/runtime/runtime.module.d.ts +2 -3
  206. package/dist/runtime/runtime.module.js +7 -6
  207. package/dist/runtime/step-log.d.ts +16 -4
  208. package/dist/runtime/step-log.js +26 -6
  209. package/dist/runtime/stream.controller.d.ts +2 -4
  210. package/dist/runtime/stream.controller.js +5 -11
  211. package/dist/runtime/subagents/child.js +1 -1
  212. package/dist/runtime/subagents/definition.d.ts +2 -1
  213. package/dist/runtime/subagents/definition.js +4 -2
  214. package/dist/runtime/subagents/deliveries.d.ts +6 -3
  215. package/dist/runtime/subagents/deliveries.js +16 -7
  216. package/dist/runtime/subagents/dispatch.d.ts +12 -12
  217. package/dist/runtime/subagents/dispatch.js +14 -38
  218. package/dist/runtime/subagents/messages.d.ts +5 -1
  219. package/dist/runtime/subagents/messages.js +11 -3
  220. package/dist/runtime/subagents/skill-fork.d.ts +3 -3
  221. package/dist/runtime/subagents/skill-fork.js +8 -5
  222. package/dist/runtime/subagents/subagents.service.js +1 -1
  223. package/dist/runtime/subagents/tree-jobs.d.ts +61 -0
  224. package/dist/runtime/subagents/tree-jobs.js +86 -0
  225. package/dist/runtime/usage-report-jobs.d.ts +23 -48
  226. package/dist/runtime/usage-report-jobs.js +67 -119
  227. package/dist/runtime/usage-report.d.ts +28 -77
  228. package/dist/runtime/usage-report.js +35 -213
  229. package/dist/runtime/waits.d.ts +48 -198
  230. package/dist/runtime/waits.js +103 -805
  231. package/dist/sandbox/jobs.d.ts +20 -10
  232. package/dist/sandbox/jobs.js +23 -17
  233. package/dist/sandbox/sandbox.service.d.ts +17 -4
  234. package/dist/sandbox/sandbox.service.js +55 -14
  235. package/dist/schedules/calendar.d.ts +134 -0
  236. package/dist/schedules/calendar.js +410 -0
  237. package/dist/schedules/fire.d.ts +26 -7
  238. package/dist/schedules/fire.js +91 -22
  239. package/dist/schedules/job-ids.d.ts +66 -17
  240. package/dist/schedules/job-ids.js +81 -13
  241. package/dist/schedules/run-tracker.d.ts +0 -5
  242. package/dist/schedules/run-tracker.js +2 -14
  243. package/dist/schedules/scan.job.d.ts +13 -32
  244. package/dist/schedules/scan.job.js +50 -164
  245. package/dist/schedules/schedule-math.d.ts +42 -0
  246. package/dist/schedules/schedule-math.js +52 -0
  247. package/dist/schedules/scheduled-tasks.service.d.ts +22 -6
  248. package/dist/schedules/scheduled-tasks.service.js +70 -14
  249. package/dist/schedules/schedules.controller.d.ts +5 -1
  250. package/dist/schedules/schedules.controller.js +15 -1
  251. package/dist/schedules/schedules.module.js +0 -4
  252. package/dist/schedules/schedules.schemas.d.ts +35 -0
  253. package/dist/schedules/schedules.schemas.js +27 -0
  254. package/dist/schedules/store.d.ts +5 -0
  255. package/dist/schedules/store.js +20 -0
  256. package/dist/skills/changes.controller.d.ts +1 -1
  257. package/dist/skills/changes.controller.js +1 -1
  258. package/dist/tools/approvals.service.d.ts +41 -11
  259. package/dist/tools/approvals.service.js +108 -14
  260. package/dist/tools/builtin-names.d.ts +5 -3
  261. package/dist/tools/builtin-names.js +12 -4
  262. package/dist/tools/builtins/builtin-tools.d.ts +1 -2
  263. package/dist/tools/builtins/builtin-tools.js +21 -17
  264. package/dist/tools/builtins/sandbox/bash.js +9 -3
  265. package/dist/tools/builtins/sandbox/common.d.ts +18 -1
  266. package/dist/tools/builtins/sandbox/common.js +14 -1
  267. package/dist/tools/builtins/sandbox/index.d.ts +2 -1
  268. package/dist/tools/builtins/sandbox/index.js +4 -1
  269. package/dist/tools/builtins/sandbox/task-list.d.ts +12 -0
  270. package/dist/tools/builtins/sandbox/task-list.js +130 -0
  271. package/dist/tools/builtins/sandbox/task-output.d.ts +8 -1
  272. package/dist/tools/builtins/sandbox/task-output.js +21 -3
  273. package/dist/tools/builtins/web/extract.d.ts +2 -4
  274. package/dist/tools/builtins/web/extract.js +2 -5
  275. package/dist/tools/pipeline.d.ts +12 -3
  276. package/dist/tools/pipeline.js +6 -1
  277. package/dist/tools/questions.service.d.ts +30 -10
  278. package/dist/tools/questions.service.js +41 -27
  279. package/dist/tools/registry.d.ts +12 -3
  280. package/dist/tools/registry.js +13 -3
  281. package/dist/tools/tools.module.js +4 -3
  282. package/package.json +3 -3
  283. package/src/migrations/0118_device_policy_ops.sql +6 -0
  284. package/src/migrations/0119_outbox.sql +22 -0
  285. package/src/migrations/0120_drop_usage_report_sweep.sql +7 -0
  286. package/src/migrations/0121_drop_platform_migration_runs.sql +12 -0
  287. package/src/migrations/0122_drop_scheduled_task_runs_rechecked_at.sql +5 -0
  288. package/src/migrations/0123_pgvector.sql +27 -0
  289. package/src/migrations/0124_conversation_source.sql +36 -0
  290. package/dist/catalog/platform-migration.d.ts +0 -115
  291. package/dist/catalog/platform-migration.js +0 -583
  292. package/dist/infra/queue/add-job-once.d.ts +0 -14
  293. package/dist/infra/queue/add-job-once.js +0 -16
@@ -1,14 +1,12 @@
1
1
  import type { Logger } from "@capybara-hq/plugin-sdk";
2
2
  import { type OnApplicationShutdown } from "@nestjs/common";
3
- import { type Job, type Queue } from "bullmq";
3
+ import { type Job } from "bullmq";
4
4
  import { BackgroundTaskStopper } from "../background/stop.js";
5
5
  import { EventLog } from "../conversations/event-log.js";
6
- import type { KernelSchemaDb } from "../db/kernel-schema.js";
7
6
  import { KernelDb } from "../infra/database/kernel-db.js";
8
7
  import { TransactionHost } from "../infra/database/transaction-host.js";
9
8
  import { KernelEvents } from "../infra/events/kernel-events.js";
10
- import { LockService } from "../infra/lock/lock.service.js";
11
- import type { KernelLogger } from "../log.js";
9
+ import { OutboxService } from "../infra/outbox/outbox.service.js";
12
10
  import { ApprovalsService } from "../tools/approvals.service.js";
13
11
  import { ToolPipeline } from "../tools/pipeline.js";
14
12
  import { QuestionsService } from "../tools/questions.service.js";
@@ -17,6 +15,7 @@ import { KyselyCheckpointSaver } from "./checkpointer.js";
17
15
  import { type WaitingFor } from "./loop.js";
18
16
  import { MAINTENANCE_QUEUE_NAME } from "./maintenance-queue.js";
19
17
  import { RunFinish } from "./run-finish.js";
18
+ import { RunResume, resumeJobId } from "./run-jobs.js";
20
19
  import { RunQueue } from "./run-queue.js";
21
20
  import { SubagentDeliveries } from "./subagents/deliveries.js";
22
21
  /** 等待超时任务所在的内核维护队列(见 maintenance.ts)。 */
@@ -24,50 +23,8 @@ export { MAINTENANCE_QUEUE_NAME };
24
23
  /** 等待超时任务的名字与 jobId(每个审批 / 输入请求一个,重复投递被 BullMQ 忽略)。 */
25
24
  export declare const WAIT_TIMEOUT_JOB = "wait-timeout";
26
25
  export declare const waitTimeoutJobId: (waitId: string) => string;
27
- /** 续跑任务的 jobId:与按消息生成的 `run-<conversationId>-<seq>` 不冲突,同一等待只会续跑一次。 */
28
- export declare const resumeJobId: (conversationId: string, waitId: string) => string;
29
- /**
30
- * 等待兜底扫描(Task 22):任务名、job scheduler id(不含 `:`)、周期、宽限期与每类每次最多处理的条数。
31
- * 宽限期:waiting 超过它、等待却已全部处理的运行才视为「续跑丢失」,避免与正常续跑(暂停后的复查等)竞争。
32
- */
33
- export declare const WAIT_SWEEP_JOB = "wait-sweep";
34
- /** The maintenance scheduler the sweep ran on before it became a `@LockedCron` (retired). */
35
- export { WAIT_SWEEP_SCHEDULER_ID } from "./job-ids.js";
36
- export declare const WAIT_SWEEP_GRACE_MS = 60000;
37
- export declare const WAIT_SWEEP_BATCH = 500;
38
- /** 一次扫描(本副本)实际生效的修复数。 */
39
- export interface WaitSweepResult {
40
- /** 过期仍 pending、被本次扫描按超时处理的审批 / 输入请求。 */
41
- expired: number;
42
- /** 等待已全部处理、被本次扫描续跑的 waiting 运行。 */
43
- resumed: number;
44
- /** 运行已终态、被本次扫描取消的审批 / 输入请求。 */
45
- cancelled: number;
46
- /**
47
- * Finished runs whose fork invocation had no report, repaired (`pairStoppedForks`; the next run
48
- * would build on a fork lead without its report).
49
- */
50
- repaired: number;
51
- /** Queued sub-agent runs, and chat runs queued by a resume, whose job was lost, enqueued again. */
52
- requeued: number;
53
- /** Running sub-agent runs without job or lock, failed `run.orphaned` and reported to the parent. */
54
- orphaned: number;
55
- /**
56
- * Active fork sub-agent runs of a finished parent run, and active sub-agent runs of a deleted
57
- * conversation's tree, asked to stop.
58
- */
59
- stopped: number;
60
- /**
61
- * Finished background sub-agent runs whose `finished` delivery was never written (the finisher
62
- * died after the child's terminal commit), delivered (agent map spec §10).
63
- */
64
- delivered: number;
65
- /**
66
- * Idle sub-agents with a parked message no run took (a `SendMessage` that reached a run after
67
- * its last model call, whose replica died before the post-run `ensureRun`), started again.
68
- */
69
- restarted: number;
70
- }
26
+ /** A resumed run's job id (`run-<conversationId>-resume-<waitId>`; `RunResume` owes it). */
27
+ export { resumeJobId };
71
28
  /** The data of a wait timeout job; `subagents` waits also carry their run and step. */
72
29
  export interface WaitTimeoutData {
73
30
  kind: WaitingFor["kind"];
@@ -76,18 +33,22 @@ export interface WaitTimeoutData {
76
33
  step?: number;
77
34
  }
78
35
  /**
79
- * 运行等待人处理(spec 4.4 / Task 9):暂停、续跑、超时与中止。状态全部在 PG(`runs.status = 'waiting'`、
80
- * `approvals` / `questions` 行),延迟与重试走 BullMQ(维护队列的 `wait-timeout` 任务),没有进程内状态。
36
+ * Runs waiting for a person (spec 4.4 / Task 9): suspend, resume, timeout and stop. All state is in
37
+ * PG (`runs.status = 'waiting'`, the `approvals` / `questions` rows); delays and retries are BullMQ
38
+ * jobs (the maintenance queue's `wait-timeout`) owed through the outbox; no in-process state.
81
39
  *
82
- * 只续跑一次:`resume` 是一条条件更新 `waiting → queued`(且该运行已没有 pending 的等待),并发调用只有一个成功,
83
- * 成功者投递 jobId 为 `run-<conversationId>-resume-<waitId>` 的运行任务。人工决定、超时任务、
84
- * 暂停后的复查都可能调用它,谁先谁后都只有一次续跑。
40
+ * One resume only: a resume locks the run row and moves it `waiting → queued` only when it waits on
41
+ * nothing else, owing the run job `run-<conversationId>-resume-<waitId>` in the same transaction
42
+ * (transactional outbox spec §6). A decision does it in its own transaction
43
+ * (`RunResume.resumeIn`); the timeout job, the suspend's re-check and a fork child's finish
44
+ * through {@link resume}. Whichever comes first, the run resumes once.
85
45
  */
86
46
  export declare class Waits implements OnApplicationShutdown {
87
47
  private readonly kdb;
88
48
  private readonly eventLog;
89
49
  private readonly approvals;
90
50
  private readonly questions;
51
+ /** Stops a queued or running sub-agent no worker holds directly ({@link stopRun}). */
91
52
  private readonly runQueue;
92
53
  /**
93
54
  * Writes, in the caller's transaction, the results of a stopped run's open `Agent` calls;
@@ -103,9 +64,10 @@ export declare class Waits implements OnApplicationShutdown {
103
64
  * `waiting` is set. Children are stopped through `request`.
104
65
  */
105
66
  private readonly abort;
106
- /** The maintenance queue: wait timeout jobs (`MaintenanceProcessor` consumes them). */
107
- private readonly timeouts;
108
- private readonly events;
67
+ /** Owes the wait timeout jobs (`MaintenanceProcessor` consumes them) with the suspend. */
68
+ private readonly outbox;
69
+ /** The resume of a waiting run, in the caller's transaction ({@link resume}). */
70
+ private readonly resumes;
109
71
  private readonly log;
110
72
  /** Stops a finished sub-agent run's background tasks ({@link childFinished}). */
111
73
  private readonly background;
@@ -114,7 +76,9 @@ export declare class Waits implements OnApplicationShutdown {
114
76
  readonly txHost: TransactionHost;
115
77
  /** Drops the `run.orphaned` subscription (the events may outlive this instance). */
116
78
  private readonly unsubscribe;
117
- constructor(kdb: KernelDb, eventLog: EventLog, approvals: Pick<ApprovalsService, "expire" | "cancelPendingIn">, questions: Pick<QuestionsService, "expire" | "cancelPendingIn">, runQueue: Pick<RunQueue, "enqueue" | "repairOrphan" | "abortIfOrphaned" | "ensureRun">,
79
+ constructor(kdb: KernelDb, eventLog: EventLog, approvals: Pick<ApprovalsService, "expire" | "cancelPendingIn">, questions: Pick<QuestionsService, "expire" | "cancelPendingIn">,
80
+ /** Stops a queued or running sub-agent no worker holds directly ({@link stopRun}). */
81
+ runQueue: Pick<RunQueue, "abortIfOrphaned">,
118
82
  /**
119
83
  * Writes, in the caller's transaction, the results of a stopped run's open `Agent` calls;
120
84
  * `fire` runs their observe hooks after the commit.
@@ -128,8 +92,10 @@ export declare class Waits implements OnApplicationShutdown {
128
92
  * `waiting` is set. Children are stopped through `request`.
129
93
  */
130
94
  abort: Pick<RunAbort, "requested" | "request">,
131
- /** The maintenance queue: wait timeout jobs (`MaintenanceProcessor` consumes them). */
132
- timeouts: Pick<Queue, "add">, events: KernelEvents, log: Pick<Logger, "error" | "warn">,
95
+ /** Owes the wait timeout jobs (`MaintenanceProcessor` consumes them) with the suspend. */
96
+ outbox: Pick<OutboxService, "add">,
97
+ /** The resume of a waiting run, in the caller's transaction ({@link resume}). */
98
+ resumes: Pick<RunResume, "resumeIn">, events: KernelEvents, log: Pick<Logger, "error" | "warn">,
133
99
  /** Stops a finished sub-agent run's background tasks ({@link childFinished}). */
134
100
  background: Pick<BackgroundTaskStopper, "stopSubagentTasks">,
135
101
  /** Delivers a background child's finish to its parent ({@link childFinished}). */
@@ -139,21 +105,22 @@ export declare class Waits implements OnApplicationShutdown {
139
105
  /** The context's handle (the transaction of a `@Transactional` method, else the pool). */
140
106
  private get db();
141
107
  /**
142
- * 兜底扫描(维护队列每 10 分钟一次,防延迟任务 / 续跑丢失)。每项修复都复用常规路径,与并发的超时任务、
143
- * 人工决定、其他副本的扫描竞争时由行锁与条件更新保证只生效一次;每项生效的修复记一行 warn(只含 id)。
144
- * 顺序:先取消终态运行的等待(这些不该再按超时处理),再处理过期的等待,最后续跑「续跑丢失」的运行。
145
- * 单项失败只记日志,不影响其他项。
108
+ * After the loop returned `suspended` (the worker released the conversation lock and the quota):
109
+ * in one transaction the conditional `running → waiting` (recording `waiting_since`) and the
110
+ * wait's timeout job (`runAt` = `expiresAt`); then a re-check whether the wait is already
111
+ * settled: a decision that came while the run was still `running` did not resume it, so the
112
+ * re-check does.
113
+ * A run no longer running (ended by another path) → nothing.
114
+ * A stop received while suspending (`abort`), or found in the stop marker after `waiting` is set
115
+ * → the wait is cancelled and the run ended as {@link abortWaiting} does (a stop arriving after
116
+ * `waiting` is set is handled by `POST .../abort`, which sees `waiting`; neither side misses it).
146
117
  */
147
- sweep(): Promise<WaitSweepResult>;
118
+ suspend(runId: string, conversationId: string, waitingFor: WaitingFor, abort?: RunAbortBy): Promise<void>;
148
119
  /**
149
- * loop 返回 `suspended` 之后(worker 已释放会话锁与配额):条件更新 `running → waiting`(记 `waiting_since`),
150
- * 投递延迟到 `expiresAt` 的超时任务,再复查一次等待是否已被处理——处理者的 `resume` 可能发生在本次置 waiting
151
- * 之前(那时条件更新不成立),复查保证这种情况下仍会续跑。
152
- * 运行已不是 running(例如已被其他路径结束)→ 什么都不做。
153
- * 暂停时已收到用户中止(`abort`),或置 waiting 之后补查到中止标记 → 直接按 {@link abortWaiting} 取消等待并结束
154
- * 运行(置 waiting 之后到达的中止由 `POST .../abort` 看到 waiting 直接处理,两边不会都漏掉)。
120
+ * {@link suspend}'s transaction: the conditional `running → waiting` and the wait's timeout job
121
+ * owed with it ({@link oweTimeout}). False when the run was no longer running (nothing written).
155
122
  */
156
- suspend(runId: string, conversationId: string, waitingFor: WaitingFor, abort?: RunAbortBy): Promise<void>;
123
+ private suspendIn;
157
124
  /**
158
125
  * A sub-agent's run waits for an approval or an input (sub-agents spec §9): appends
159
126
  * `subagent_waiting` to the **root** conversation of its tree, whatever its depth (agent map spec
@@ -165,9 +132,9 @@ export declare class Waits implements OnApplicationShutdown {
165
132
  */
166
133
  private surfaceInRoot;
167
134
  /**
168
- * 续跑:一条条件更新 `waiting → queued`(且该运行没有 pending 的审批 / 输入请求),把本次等待时长累加进
169
- * `waited_ms`,记下续跑任务的 jobId;成功(只有一个调用方)则投递运行任务并返回 true。
170
- * 投递失败 → 撤回为 waiting(等待时长已计入,重新从现在起计),之后超时任务到点会再次续跑,并抛出。
135
+ * Resumes the run in its own transaction (`RunResume.resumeIn`: locks the run row and, when it
136
+ * is `waiting` on nothing else, sets it `queued` and owes its resume job); true when this call
137
+ * queued it. The wait timeout, the suspend's re-check and a fork child's finish use it; the decisions call `RunResume.resumeIn` in their own transactions.
171
138
  */
172
139
  resume(runId: string, waitId: string): Promise<boolean>;
173
140
  /**
@@ -191,103 +158,16 @@ export declare class Waits implements OnApplicationShutdown {
191
158
  abortWaiting(conversationId: string, runId: string, byAccountId: string | null, detail?: RunAbortDetail): Promise<boolean>;
192
159
  /** {@link abortWaiting}'s transaction; the open `Agent` calls it wrote go back through `st`. */
193
160
  private abortWaitingIn;
194
- /** 扫描 3:运行已终态(如暂停途中失败)仍 pending 的审批 / 输入请求 → 取消(写决定 / 回答事件)。 */
195
- private sweepFinishedRuns;
196
- /**
197
- * 同一事务里锁会话行与运行行(与 {@link abortWaiting} 同序),运行仍是终态时取消其 pending 等待;
198
- * 提交后发布事件。返回取消的条数(并发的另一副本拿到锁时已无 pending → 0)。
199
- */
200
- private cancelWaitsOfFinished;
201
- private cancelWaitsOfFinishedIn;
202
- /** 扫描 1:`expires_at < now()` 仍 pending 的审批 / 输入请求 → 与超时任务同一路径(置 expired、写事件、续跑)。 */
203
- private sweepOverdue;
204
- /**
205
- * Sub-agents waits past their `runs.wait_expires_at` (their timeout job was lost): expired the
206
- * way the timeout job would ({@link expire}: the fork invocation's active child stops as
207
- * `timed out`, the parent resumes once it is done). Only fork invocations wait (agent map spec
208
- * §5.4); their step is the run's fork dispatch step. A run stays here until its children have
209
- * stopped; a pass that neither stopped a child nor resumed the run is not counted.
210
- */
211
- private sweepOverdueSubagents;
212
- /**
213
- * Sub-agent runs nobody will run (spec §7.5; `RunQueue.repairOrphan`, the same orphan test as
214
- * `ensureRun`, which never runs for a child): `queued` past the grace period without a job → enqueued
215
- * again (the dispatch committed, its enqueue was lost); `running` without job and lock → failed
216
- * `run.orphaned` and reported to the parent, which then resumes. Also a chat run left `queued`
217
- * by a resume whose enqueue was lost (final review m8): enqueued again under its resume job id
218
- * (`ensureRun` would only repair it at the conversation's next message).
219
- */
220
- private sweepSubagentOrphans;
221
- /**
222
- * Idle sub-agent conversations (no active run, not deleted) with a message newer than what they
223
- * consumed, written past the grace period and within the last day: a `SendMessage` that reached a
224
- * run after its last model call is started by that run's post-job `ensureRun`
225
- * (`RunQueue.childWakeIn`), which a replica dying after the terminal commit skips. Re-derived with
226
- * the same `ensureRun` (its rules decide: a paused root starts nothing). A sub-agent whose last
227
- * run was stopped (`aborted`) can never start from a message, so it is left out here, and the
228
- * newest conversations come first (active within the last day), so parked messages that cannot
229
- * start never fill the batch. Returns how many runs started.
230
- */
231
- private sweepParkedMessages;
232
- /**
233
- * Finished runs (past the grace period, within the last day) triggered by a fork invocation that
234
- * has no report ({@link unreportedForkRuns}): a terminal path that skipped
235
- * `stopOpenAgentCalls`, such as an old replica's abort, would otherwise leave a fork lead without
236
- * its report. Each is repaired under the conversation row lock with the normal
237
- * `stopOpenAgentCalls` (an ended child gives its result, an active one the stopped text;
238
- * idempotency keys make it safe against the regular paths); an active fork child is then stopped
239
- * by {@link sweepForkChildrenOfFinished}. An `Agent` call has its result from its dispatch (agent
240
- * map spec §4.1).
241
- */
242
- private sweepOpenAgentCalls;
243
- /** One run's repair of {@link sweepOpenAgentCalls}; its observe hooks go back through `st`. */
244
- private repairOpenAgentCallsIn;
245
- /**
246
- * Active fork sub-agent runs (phase D, `for_skill`) of a parent run that finished more than the
247
- * grace period ago (§12: the run's terminal state stops its fork child and reports it stopped;
248
- * this is the backstop) → stopped. Background `Agent` children of a finished run are the normal
249
- * case and keep running (agent map spec §5.4).
250
- */
251
- private sweepForkChildrenOfFinished;
252
- /**
253
- * Active sub-agent runs with a deleted ancestor (agent map spec §10: deleting the root stops the
254
- * whole tree; the delete's own {@link stopTree} is the normal path, this the backstop for a crash
255
- * between marking the root deleted and stopping its tree, or a dispatch that raced it) → stopped
256
- * on the deleter's behalf (no account). No grace period: nothing in a deleted tree should run.
257
- */
258
- private sweepTreesOfDeletedRoots;
259
- /**
260
- * Background sub-agent runs that finished more than the grace period ago (within the last day)
261
- * whose parent has no `subagent_finished` for them (agent map spec §10: the finisher died after
262
- * the child's terminal commit; the finish path writes `subagent_finished` and the delivery in one
263
- * transaction) → {@link childFinished}, idempotent against a late finisher. Keyed on the child
264
- * run, so a run nobody dispatched (a wake run, §5.3) is repaired too, and on
265
- * `subagent_finished`, not on the delivery, so a child that finished before background
266
- * dispatch (its result was the `Agent` call's, its `subagent_finished` written then) is never
267
- * delivered a second time. Fork invocations' runs, a run without a terminal event in its log
268
- * (nothing to deliver), a conversation never dispatched and a child whose parent conversation
269
- * was deleted are skipped; only a written delivery counts.
270
- */
271
- private sweepUndelivered;
272
- /**
273
- * 扫描 2:waiting 超过宽限期、却已没有 pending 等待的运行(决定提交后进程崩溃、续跑投递失败回滚等)→
274
- * {@link resume}(条件更新 `waiting → queued` + 投递)。续跑任务的 jobId 用该运行最近一次等待的 id
275
- * (与人工决定 / 超时任务一致,已投递过的同一续跑被 BullMQ 去重)。
276
- */
277
- private sweepStuckRuns;
278
- /**
279
- * The id of the run's latest wait (by creation time): an approval, an input request or a fork
280
- * invocation's dispatch, whose wait id is its child run id (the id the child's finisher resumes
281
- * with, so a sweep resume and a late finisher resume share one job id).
282
- */
283
- private lastWaitId;
284
161
  /**
285
162
  * A `wait-timeout` job (dispatched by `MaintenanceProcessor`): expires the wait,
286
163
  * or delays the job again until the wait's expiry when it ran early.
287
164
  */
288
165
  processTimeout(job: Job<WaitTimeoutData>, token: string | undefined): Promise<void>;
289
- /** 投递超时任务(延迟到 expiresAt)。失败只记日志:人工处理仍可续跑,只是不会自动超时。 */
290
- private scheduleTimeout;
166
+ /**
167
+ * Owes the wait's timeout job (`wait-timeout-<waitId>`, `runAt` = the wait's deadline) in the
168
+ * suspend's transaction (transactional outbox spec §6 "Resumes and timeouts").
169
+ */
170
+ private oweTimeout;
291
171
  /**
292
172
  * Whether the wait is over: an approval or input request is no longer pending (decided, expired
293
173
  * or cancelled); `subagents`: the run's fork invocation child is no longer active.
@@ -309,7 +189,8 @@ export declare class Waits implements OnApplicationShutdown {
309
189
  * `subagent_finished` (idempotent; not model-visible) and tries
310
190
  * {@link resume}`(parentRunId, childRunId)`, which succeeds once its parent run waits on nothing
311
191
  * else. A run of a conversation never dispatched, or not terminal yet, is left alone. A crash in
312
- * between is repaired by the wait sweep (the orphan repair calls this again). A fork child's
192
+ * between is repaired by the `deliver-to-parent` job its terminal transaction owed (the
193
+ * `run-orphan-check` calls this too). A fork child's
313
194
  * running background tasks the terminal run started are then stopped (not those of a later run);
314
195
  * a stopped background child's are all stopped, whichever of its runs started them; a background
315
196
  * child that ended on its own keeps them, and their finish wakes it (agent map spec §5.3). True
@@ -377,34 +258,3 @@ export declare class Waits implements OnApplicationShutdown {
377
258
  */
378
259
  private stopRun;
379
260
  }
380
- /**
381
- * Phase D: finished chat runs (past the grace period, within the last day) triggered by a fork
382
- * invocation that has no report (dispatched or not); the wait sweep writes it
383
- * (`pairStoppedForks`).
384
- */
385
- export declare function unreportedForkRuns(db: KernelSchemaDb): Promise<{
386
- parent_run_id: string;
387
- parent_conversation_id: string;
388
- }[]>;
389
- /**
390
- * A decided approval / answered question resumes the waiting run on this replica (domain events,
391
- * spec §3b; the wait sweep resumes a run whose resume was lost). Its IM delivery follows the same
392
- * events (`ChannelJobs`).
393
- */
394
- export declare class DecisionResumer implements OnApplicationShutdown {
395
- private readonly unsubscribe;
396
- constructor(waits: Pick<Waits, "resume">, events: Pick<KernelEvents, "on">);
397
- /** Unsubscribes after the stop phases: events emitted while they drain are still handled. */
398
- onApplicationShutdown(): void;
399
- }
400
- /**
401
- * The wait sweep, every 10 minutes, once across replicas: {@link Waits.sweep}. Replaces
402
- * the maintenance scheduler `wait-sweep-every-10m` (retired at worker start).
403
- */
404
- export declare class WaitSweepJob {
405
- private readonly waits;
406
- readonly lockService: LockService;
407
- readonly log: KernelLogger;
408
- constructor(waits: Pick<Waits, "sweep">, lockService: LockService, log: KernelLogger);
409
- run(): Promise<void>;
410
- }