@capybara-hq/kernel 0.1.0-rc.41 → 0.1.0-rc.43

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 (359) hide show
  1. package/bootstrap.json +3 -3
  2. package/dist/admin/admin.schemas.d.ts +6 -0
  3. package/dist/admin/org-network.service.d.ts +3 -1
  4. package/dist/admin/org-network.service.js +10 -2
  5. package/dist/app.d.ts +4 -0
  6. package/dist/app.js +2 -0
  7. package/dist/background/finish.d.ts +9 -1
  8. package/dist/background/finish.js +57 -4
  9. package/dist/background/launch.d.ts +23 -0
  10. package/dist/background/launch.js +55 -1
  11. package/dist/background/managed-output.d.ts +110 -0
  12. package/dist/background/managed-output.js +219 -0
  13. package/dist/background/managed.d.ts +52 -0
  14. package/dist/background/managed.js +128 -0
  15. package/dist/background/poll.job.d.ts +35 -1
  16. package/dist/background/poll.job.js +174 -9
  17. package/dist/background/render.js +1 -0
  18. package/dist/background/retire-legacy-tasks.job.d.ts +57 -0
  19. package/dist/background/retire-legacy-tasks.job.js +149 -0
  20. package/dist/background/session.d.ts +15 -5
  21. package/dist/background/session.js +22 -6
  22. package/dist/background/stop.d.ts +35 -5
  23. package/dist/background/stop.js +94 -8
  24. package/dist/background/store.d.ts +110 -3
  25. package/dist/background/store.js +205 -3
  26. package/dist/background/tasks.service.d.ts +18 -1
  27. package/dist/background/tasks.service.js +99 -9
  28. package/dist/catalog/marketplaces.service.d.ts +1 -1
  29. package/dist/channels/cards.js +1 -1
  30. package/dist/channels/inbound.js +2 -0
  31. package/dist/channels/phrases.d.ts +4 -0
  32. package/dist/channels/phrases.js +4 -0
  33. package/dist/config/load.d.ts +18 -0
  34. package/dist/config/load.js +39 -1
  35. package/dist/config/permission-migration.d.ts +26 -0
  36. package/dist/config/permission-migration.js +64 -0
  37. package/dist/config/schema.d.ts +4 -1
  38. package/dist/config/schema.js +41 -10
  39. package/dist/conversations/conversations.controller.d.ts +1 -1
  40. package/dist/conversations/conversations.controller.js +1 -1
  41. package/dist/conversations/conversations.module.js +12 -0
  42. package/dist/conversations/conversations.service.d.ts +11 -2
  43. package/dist/conversations/conversations.service.js +97 -27
  44. package/dist/conversations/event-log.d.ts +1 -1
  45. package/dist/conversations/event-log.js +10 -2
  46. package/dist/conversations/events.d.ts +307 -4
  47. package/dist/conversations/events.js +286 -32
  48. package/dist/conversations/permission-profile-store.d.ts +50 -0
  49. package/dist/conversations/permission-profile-store.js +67 -0
  50. package/dist/conversations/permission-profile-store.module.d.ts +2 -0
  51. package/dist/conversations/permission-profile-store.module.js +19 -0
  52. package/dist/conversations/permission-profiles.controller.d.ts +84 -0
  53. package/dist/conversations/permission-profiles.controller.js +105 -0
  54. package/dist/conversations/permission-profiles.schemas.d.ts +31 -0
  55. package/dist/conversations/permission-profiles.schemas.js +23 -0
  56. package/dist/conversations/permission-profiles.service.d.ts +56 -0
  57. package/dist/conversations/permission-profiles.service.js +179 -0
  58. package/dist/ctx/create-ctx.d.ts +12 -5
  59. package/dist/ctx/create-ctx.js +21 -5
  60. package/dist/db/kernel-schema.d.ts +248 -3
  61. package/dist/devices/access.js +1 -1
  62. package/dist/devices/access.module.d.ts +2 -0
  63. package/dist/devices/access.module.js +19 -0
  64. package/dist/devices/conversation-devices.d.ts +8 -29
  65. package/dist/devices/conversation-devices.js +37 -73
  66. package/dist/devices/device-projects.service.d.ts +26 -28
  67. package/dist/devices/device-projects.service.js +123 -127
  68. package/dist/devices/devices.controller.js +3 -1
  69. package/dist/devices/devices.module.js +11 -9
  70. package/dist/devices/devices.service.d.ts +116 -8
  71. package/dist/devices/devices.service.js +252 -20
  72. package/dist/devices/dto.d.ts +16 -0
  73. package/dist/devices/dto.js +34 -6
  74. package/dist/devices/errors.d.ts +8 -0
  75. package/dist/devices/errors.js +10 -0
  76. package/dist/devices/project-rules.d.ts +44 -0
  77. package/dist/devices/project-rules.js +27 -0
  78. package/dist/devices/working-root.d.ts +6 -0
  79. package/dist/devices/working-root.js +12 -1
  80. package/dist/errors.d.ts +31 -0
  81. package/dist/errors.js +31 -0
  82. package/dist/evals/reconstruct.d.ts +19 -2
  83. package/dist/evals/reconstruct.js +40 -2
  84. package/dist/evals/recorded-permission-context.d.ts +77 -0
  85. package/dist/evals/recorded-permission-context.js +151 -0
  86. package/dist/evals/recorded-tools.d.ts +10 -1
  87. package/dist/evals/recorded-tools.js +27 -3
  88. package/dist/evals/run-replay.d.ts +8 -2
  89. package/dist/evals/run-replay.js +18 -2
  90. package/dist/evals/trajectory.d.ts +1 -1
  91. package/dist/evals/worker.d.ts +10 -1
  92. package/dist/evals/worker.js +23 -14
  93. package/dist/extensions/host.d.ts +4 -1
  94. package/dist/extensions/host.js +10 -6
  95. package/dist/http/api.module.js +2 -0
  96. package/dist/identity/accounts.d.ts +14 -1
  97. package/dist/identity/accounts.js +61 -5
  98. package/dist/identity/identity.module.js +2 -0
  99. package/dist/identity/settings.d.ts +28 -12
  100. package/dist/identity/settings.js +89 -33
  101. package/dist/index.d.ts +6 -0
  102. package/dist/index.js +5 -0
  103. package/dist/infra/cli/kernel-cli.module.d.ts +2 -2
  104. package/dist/infra/cli/kernel-cli.module.js +5 -2
  105. package/dist/infra/cli/permission-cutover.d.ts +59 -0
  106. package/dist/infra/cli/permission-cutover.js +107 -0
  107. package/dist/infra/config/kernel-config.service.d.ts +2 -2
  108. package/dist/infra/config/kernel-config.service.js +35 -11
  109. package/dist/infra/database/startup-activation-transaction.d.ts +36 -0
  110. package/dist/infra/database/startup-activation-transaction.js +242 -0
  111. package/dist/infra/database/transaction-host.d.ts +47 -0
  112. package/dist/infra/database/transaction-host.js +119 -0
  113. package/dist/infra/database/xact-lock.d.ts +1 -0
  114. package/dist/infra/database/xact-lock.js +6 -0
  115. package/dist/infra/outbox/registry.d.ts +39 -0
  116. package/dist/infra/outbox/registry.js +18 -0
  117. package/dist/kernel.module.js +2 -0
  118. package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
  119. package/dist/lifecycle/kernel-lifecycle.js +55 -28
  120. package/dist/local-devices/device-auth.js +1 -1
  121. package/dist/local-devices/device-policy.service.d.ts +35 -5
  122. package/dist/local-devices/device-policy.service.js +188 -13
  123. package/dist/local-devices/instance-bindings.d.ts +82 -0
  124. package/dist/local-devices/instance-bindings.js +304 -0
  125. package/dist/local-devices/keys.d.ts +5 -0
  126. package/dist/local-devices/keys.js +5 -0
  127. package/dist/local-devices/local-device-gateway.d.ts +19 -4
  128. package/dist/local-devices/local-device-gateway.js +185 -17
  129. package/dist/local-devices/local-device-provider.d.ts +42 -7
  130. package/dist/local-devices/local-device-provider.js +185 -10
  131. package/dist/local-devices/local-devices.module.d.ts +17 -2
  132. package/dist/local-devices/local-devices.module.js +49 -8
  133. package/dist/local-devices/machines.d.ts +35 -12
  134. package/dist/local-devices/machines.js +203 -8
  135. package/dist/local-devices/network-grants.js +3 -3
  136. package/dist/local-devices/network-grants.module.d.ts +2 -0
  137. package/dist/local-devices/network-grants.module.js +14 -0
  138. package/dist/local-devices/owner-report.d.ts +79 -0
  139. package/dist/local-devices/owner-report.js +155 -0
  140. package/dist/local-devices/owner-reports.d.ts +48 -0
  141. package/dist/local-devices/owner-reports.js +190 -0
  142. package/dist/local-devices/policy-summary.d.ts +24 -1
  143. package/dist/local-devices/policy-summary.js +48 -0
  144. package/dist/local-devices/relay.d.ts +82 -6
  145. package/dist/local-devices/relay.js +187 -16
  146. package/dist/local-devices/targets.d.ts +16 -1
  147. package/dist/local-devices/targets.js +35 -2
  148. package/dist/models/image-usable.d.ts +22 -0
  149. package/dist/models/image-usable.js +29 -0
  150. package/dist/models/images.d.ts +125 -0
  151. package/dist/models/images.js +440 -0
  152. package/dist/models/models.module.d.ts +3 -2
  153. package/dist/models/models.module.js +16 -5
  154. package/dist/models/org-models.schemas.js +1 -0
  155. package/dist/models/purposes.controller.d.ts +7 -0
  156. package/dist/models/purposes.controller.js +9 -0
  157. package/dist/models/purposes.d.ts +28 -3
  158. package/dist/models/purposes.js +40 -13
  159. package/dist/orgs/cloud-network-setting.d.ts +33 -0
  160. package/dist/orgs/cloud-network-setting.js +47 -0
  161. package/dist/orgs/offboarding.d.ts +3 -1
  162. package/dist/orgs/offboarding.js +8 -2
  163. package/dist/orgs/org-lock.d.ts +10 -4
  164. package/dist/orgs/org-lock.js +10 -4
  165. package/dist/orgs/orgs.service.d.ts +3 -1
  166. package/dist/orgs/orgs.service.js +8 -2
  167. package/dist/orgs/settings.controller.d.ts +4 -1
  168. package/dist/orgs/settings.controller.js +3 -3
  169. package/dist/orgs/settings.service.d.ts +48 -56
  170. package/dist/orgs/settings.service.js +144 -101
  171. package/dist/overrides.d.ts +6 -1
  172. package/dist/overrides.js +2 -0
  173. package/dist/platform/exclusions.js +20 -0
  174. package/dist/platform/ops/admin.ops.js +14 -11
  175. package/dist/platform/ops/agent-skills.ops.js +2 -0
  176. package/dist/platform/ops/devices.ops.js +10 -2
  177. package/dist/platform/ops/models.ops.js +10 -0
  178. package/dist/platform/ops/org-settings.ops.js +6 -6
  179. package/dist/platform/ops/orgs.ops.js +1 -0
  180. package/dist/roles/worker.module.js +4 -0
  181. package/dist/runtime/context/build.d.ts +3 -1
  182. package/dist/runtime/context/build.js +19 -3
  183. package/dist/runtime/context/compaction.js +5 -0
  184. package/dist/runtime/context/device-paths.d.ts +9 -2
  185. package/dist/runtime/context/device-paths.js +20 -0
  186. package/dist/runtime/context/permission-profile.d.ts +46 -0
  187. package/dist/runtime/context/permission-profile.js +194 -0
  188. package/dist/runtime/hook-calls.d.ts +11 -1
  189. package/dist/runtime/hook-calls.js +24 -1
  190. package/dist/runtime/maintenance.d.ts +7 -1
  191. package/dist/runtime/maintenance.js +43 -15
  192. package/dist/runtime/model-call.d.ts +32 -0
  193. package/dist/runtime/model-call.js +67 -0
  194. package/dist/runtime/permission-context-errors.d.ts +39 -0
  195. package/dist/runtime/permission-context-errors.js +67 -0
  196. package/dist/runtime/permission-context.service.d.ts +60 -0
  197. package/dist/runtime/permission-context.service.js +286 -0
  198. package/dist/runtime/project/knowledge.d.ts +7 -2
  199. package/dist/runtime/project/knowledge.js +5 -2
  200. package/dist/runtime/project/knowledge.service.d.ts +17 -2
  201. package/dist/runtime/project/knowledge.service.js +76 -12
  202. package/dist/runtime/providers.d.ts +9 -5
  203. package/dist/runtime/providers.js +22 -3
  204. package/dist/runtime/run-execution.d.ts +4 -1
  205. package/dist/runtime/run-execution.js +14 -5
  206. package/dist/runtime/run-steps.d.ts +11 -3
  207. package/dist/runtime/run-steps.js +27 -4
  208. package/dist/runtime/runtime.module.js +11 -0
  209. package/dist/runtime/usage-report.d.ts +10 -6
  210. package/dist/runtime/usage-report.js +31 -11
  211. package/dist/sandbox/attachments-path.d.ts +15 -0
  212. package/dist/sandbox/attachments-path.js +15 -0
  213. package/dist/sandbox/cloud-permission-snapshot.d.ts +159 -0
  214. package/dist/sandbox/cloud-permission-snapshot.js +407 -0
  215. package/dist/sandbox/cloud-permission-snapshot.module.d.ts +8 -0
  216. package/dist/sandbox/cloud-permission-snapshot.module.js +34 -0
  217. package/dist/sandbox/held-operations.d.ts +39 -0
  218. package/dist/sandbox/held-operations.js +119 -0
  219. package/dist/sandbox/jobs.d.ts +14 -3
  220. package/dist/sandbox/jobs.js +20 -2
  221. package/dist/sandbox/kernel-read.d.ts +55 -0
  222. package/dist/sandbox/kernel-read.js +57 -0
  223. package/dist/sandbox/legacy-retirement.d.ts +336 -0
  224. package/dist/sandbox/legacy-retirement.js +1063 -0
  225. package/dist/sandbox/legacy-retirement.module.d.ts +8 -0
  226. package/dist/sandbox/legacy-retirement.module.js +25 -0
  227. package/dist/sandbox/managed-device-context.d.ts +94 -0
  228. package/dist/sandbox/managed-device-context.js +308 -0
  229. package/dist/sandbox/managed-device-context.module.d.ts +6 -0
  230. package/dist/sandbox/managed-device-context.module.js +33 -0
  231. package/dist/sandbox/modes.d.ts +56 -8
  232. package/dist/sandbox/modes.js +92 -8
  233. package/dist/sandbox/operation-authority-types.d.ts +8 -0
  234. package/dist/sandbox/operation-authority-types.js +32 -0
  235. package/dist/sandbox/operation-authority.d.ts +145 -0
  236. package/dist/sandbox/operation-authority.js +783 -0
  237. package/dist/sandbox/operation-authority.module.d.ts +2 -0
  238. package/dist/sandbox/operation-authority.module.js +29 -0
  239. package/dist/sandbox/operation-intents.d.ts +206 -0
  240. package/dist/sandbox/operation-intents.js +1252 -0
  241. package/dist/sandbox/operation-intents.module.d.ts +2 -0
  242. package/dist/sandbox/operation-intents.module.js +21 -0
  243. package/dist/sandbox/operation-payloads.d.ts +33 -0
  244. package/dist/sandbox/operation-payloads.js +332 -0
  245. package/dist/sandbox/operation-reconcile.job.d.ts +49 -0
  246. package/dist/sandbox/operation-reconcile.job.js +247 -0
  247. package/dist/sandbox/permission-authority-epochs.d.ts +18 -0
  248. package/dist/sandbox/permission-authority-epochs.js +83 -0
  249. package/dist/sandbox/permission-authority-lock.d.ts +38 -0
  250. package/dist/sandbox/permission-authority-lock.js +147 -0
  251. package/dist/sandbox/permission-authority.module.d.ts +9 -0
  252. package/dist/sandbox/permission-authority.module.js +55 -0
  253. package/dist/sandbox/permission-availability.d.ts +18 -0
  254. package/dist/sandbox/permission-availability.js +19 -0
  255. package/dist/sandbox/permission-call-grants.d.ts +34 -0
  256. package/dist/sandbox/permission-call-grants.js +349 -0
  257. package/dist/sandbox/permission-call-grants.module.d.ts +2 -0
  258. package/dist/sandbox/permission-call-grants.module.js +21 -0
  259. package/dist/sandbox/permission-call-types.d.ts +82 -0
  260. package/dist/sandbox/permission-call-types.js +1 -0
  261. package/dist/sandbox/permission-ceiling-writes.d.ts +47 -0
  262. package/dist/sandbox/permission-ceiling-writes.js +223 -0
  263. package/dist/sandbox/permission-config-publication.d.ts +76 -0
  264. package/dist/sandbox/permission-config-publication.js +339 -0
  265. package/dist/sandbox/permission-context-types.d.ts +64 -0
  266. package/dist/sandbox/permission-context-types.js +24 -0
  267. package/dist/sandbox/permission-facts.d.ts +206 -0
  268. package/dist/sandbox/permission-facts.js +627 -0
  269. package/dist/sandbox/permission-facts.module.d.ts +2 -0
  270. package/dist/sandbox/permission-facts.module.js +20 -0
  271. package/dist/sandbox/permission-instance-writes.d.ts +90 -0
  272. package/dist/sandbox/permission-instance-writes.js +305 -0
  273. package/dist/sandbox/permission-runtime-reports.d.ts +83 -0
  274. package/dist/sandbox/permission-runtime-reports.js +354 -0
  275. package/dist/sandbox/permission-transitions.d.ts +31 -0
  276. package/dist/sandbox/permission-transitions.js +155 -0
  277. package/dist/sandbox/platform-full-access.d.ts +128 -0
  278. package/dist/sandbox/platform-full-access.js +210 -0
  279. package/dist/sandbox/providers.d.ts +22 -0
  280. package/dist/sandbox/providers.js +10 -2
  281. package/dist/sandbox/providers.module.d.ts +2 -0
  282. package/dist/sandbox/providers.module.js +19 -0
  283. package/dist/sandbox/request-not-sent.d.ts +13 -0
  284. package/dist/sandbox/request-not-sent.js +13 -0
  285. package/dist/sandbox/review/classifier.d.ts +2 -0
  286. package/dist/sandbox/review/classifier.js +8 -0
  287. package/dist/sandbox/review/permission-candidate.d.ts +12 -0
  288. package/dist/sandbox/review/permission-candidate.js +145 -0
  289. package/dist/sandbox/review/review.service.d.ts +5 -2
  290. package/dist/sandbox/review/review.service.js +60 -15
  291. package/dist/sandbox/sandbox-file-reader.d.ts +18 -1
  292. package/dist/sandbox/sandbox-file-reader.js +51 -7
  293. package/dist/sandbox/sandbox.module.js +26 -6
  294. package/dist/sandbox/sandbox.service.d.ts +230 -34
  295. package/dist/sandbox/sandbox.service.js +1368 -199
  296. package/dist/sandbox/unreachable.js +17 -3
  297. package/dist/sandbox/use-context.d.ts +17 -0
  298. package/dist/sandbox/use-context.js +1 -0
  299. package/dist/skills/invoke.d.ts +27 -4
  300. package/dist/skills/invoke.js +72 -7
  301. package/dist/tools/approvals.controller.d.ts +1 -0
  302. package/dist/tools/approvals.schemas.d.ts +1 -0
  303. package/dist/tools/approvals.schemas.js +3 -1
  304. package/dist/tools/approvals.service.d.ts +23 -10
  305. package/dist/tools/approvals.service.js +104 -26
  306. package/dist/tools/builtin-names.d.ts +3 -1
  307. package/dist/tools/builtin-names.js +5 -1
  308. package/dist/tools/builtins/artifacts.d.ts +43 -0
  309. package/dist/tools/builtins/artifacts.js +67 -0
  310. package/dist/tools/builtins/builtin-tools.d.ts +20 -2
  311. package/dist/tools/builtins/builtin-tools.js +61 -10
  312. package/dist/tools/builtins/image/generate.d.ts +59 -0
  313. package/dist/tools/builtins/image/generate.js +535 -0
  314. package/dist/tools/builtins/image/index.d.ts +2 -0
  315. package/dist/tools/builtins/image/index.js +2 -0
  316. package/dist/tools/builtins/image/paths.d.ts +12 -0
  317. package/dist/tools/builtins/image/paths.js +26 -0
  318. package/dist/tools/builtins/sandbox/bash.d.ts +25 -1
  319. package/dist/tools/builtins/sandbox/bash.js +201 -5
  320. package/dist/tools/builtins/sandbox/common.d.ts +92 -20
  321. package/dist/tools/builtins/sandbox/common.js +201 -17
  322. package/dist/tools/builtins/sandbox/deliver.js +9 -51
  323. package/dist/tools/builtins/sandbox/edit.js +2 -2
  324. package/dist/tools/builtins/sandbox/fetch-attachment.js +11 -4
  325. package/dist/tools/builtins/sandbox/glob.js +2 -2
  326. package/dist/tools/builtins/sandbox/grep.js +2 -2
  327. package/dist/tools/builtins/sandbox/read.js +2 -2
  328. package/dist/tools/builtins/sandbox/task-list.js +7 -1
  329. package/dist/tools/builtins/sandbox/task-output.d.ts +1 -1
  330. package/dist/tools/builtins/sandbox/task-output.js +172 -0
  331. package/dist/tools/builtins/sandbox/task-stop.js +25 -3
  332. package/dist/tools/builtins/sandbox/write.js +2 -2
  333. package/dist/tools/builtins/skill-load.js +9 -6
  334. package/dist/tools/device-op-resolver.d.ts +1 -1
  335. package/dist/tools/device-op-resolver.js +13 -3
  336. package/dist/tools/permission-approval-jobs.d.ts +18 -0
  337. package/dist/tools/permission-approval-jobs.js +92 -0
  338. package/dist/tools/pipeline.d.ts +17 -2
  339. package/dist/tools/pipeline.js +315 -26
  340. package/dist/tools/registry.d.ts +9 -0
  341. package/dist/tools/tools.module.js +15 -0
  342. package/package.json +4 -3
  343. package/src/migrations/0146_permission_authority_epochs.sql +6 -0
  344. package/src/migrations/0147_conversation_permission_profiles.sql +4 -0
  345. package/src/migrations/0148_first_message_request_hash.sql +2 -0
  346. package/src/migrations/0149_sandbox_operation_intents.sql +66 -0
  347. package/src/migrations/0150_permission_call_grants_and_approvals.sql +36 -0
  348. package/src/migrations/0151_starting_execution_grants.sql +8 -0
  349. package/src/migrations/0152_background_launch_identity.sql +66 -0
  350. package/src/migrations/0153_background_cancel_requester.sql +13 -0
  351. package/src/migrations/0154_cloud_owner_ceiling.sql +14 -0
  352. package/src/migrations/0155_device_instance_generations.sql +30 -0
  353. package/src/migrations/0156_device_owner_report.sql +7 -0
  354. package/src/migrations/0157_legacy_retirement.sql +116 -0
  355. package/src/migrations/0158_legacy_freeze_enumerated.sql +5 -0
  356. package/src/migrations/0159_retirement_exact_cancel.sql +9 -0
  357. package/src/migrations/0160_model_usage_image_kind.sql +10 -0
  358. package/dist/sandbox/platform-auto-mode.d.ts +0 -63
  359. package/dist/sandbox/platform-auto-mode.js +0 -63
@@ -0,0 +1,304 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { DeviceProtocolError, parseOperationResultV3, } from "@capybara-hq/plugin-sdk";
11
+ import { Inject, Injectable } from "@nestjs/common";
12
+ import { sql } from "kysely";
13
+ import { KernelDb } from "../infra/database/kernel-db.js";
14
+ import { runOutsideTransaction, TransactionHost } from "../infra/database/transaction-host.js";
15
+ import { Transactional } from "../infra/database/transactional.decorator.js";
16
+ import { LegacyRetirementStore } from "../sandbox/legacy-retirement.js";
17
+ import { PermissionCeilingWrites } from "../sandbox/permission-ceiling-writes.js";
18
+ import { PermissionRuntimeReports, readCurrentReport, runtimeIdentity, } from "../sandbox/permission-runtime-reports.js";
19
+ import { LOCAL_PROVIDER } from "../sandbox/providers.js";
20
+ import { OP_DEADLINE_MS } from "./local-device-provider.js";
21
+ import { OwnerReports } from "./owner-reports.js";
22
+ import { LocalDeviceRelay } from "./relay.js";
23
+ /** The binding cannot be completed; the instance stays unprobed and admits nothing. */
24
+ export class InstanceBindingError extends Error {
25
+ name = "InstanceBindingError";
26
+ }
27
+ /**
28
+ * Post-commit binding of a local instance generation (permission runtime cutover B4, staged on the
29
+ * v3 path; protocol 2 stays active). `commitHandle` allocated the generation and committed an
30
+ * `awaiting-device` binding; before the lifecycle holder downgrades to shared use it calls
31
+ * {@link bind}, which sends the private `bindInstance` RPC to the authenticated machine outside
32
+ * any transaction and then, under the device authority writer, verifies the exact instance row,
33
+ * generation, binding id and machine, records the runtime report and marks the binding
34
+ * `installed`. A retry (reconnect, lost reply, another replica) resends the identical binding; it
35
+ * never allocates a new generation. A delayed answer for an old generation cannot populate a
36
+ * replacement row.
37
+ */
38
+ let LocalInstanceBindings = class LocalInstanceBindings {
39
+ db;
40
+ txHost;
41
+ relay;
42
+ ceilings;
43
+ reports;
44
+ owners;
45
+ retirement;
46
+ constructor(db, txHost, relay, ceilings, reports, owners,
47
+ /** The durable legacy gate checked before any bind is sent (8.5). */
48
+ retirement) {
49
+ this.db = db;
50
+ this.txHost = txHost;
51
+ this.relay = relay;
52
+ this.ceilings = ceilings;
53
+ this.reports = reports;
54
+ this.owners = owners;
55
+ this.retirement = retirement;
56
+ }
57
+ /** The binding committed for the device's current local instance, or null (no allocation). */
58
+ async committed(deviceId) {
59
+ const instance = await this.db
60
+ .selectFrom("device_instances")
61
+ .select(["org_id", "handle", "provider", "created_at"])
62
+ .where("device_id", "=", deviceId)
63
+ .where("status", "in", ["running", "suspended"])
64
+ .executeTakeFirst();
65
+ if (!instance || instance.provider !== LOCAL_PROVIDER || !instance.handle)
66
+ return null;
67
+ const binding = await this.db
68
+ .selectFrom("device_instance_bindings")
69
+ .select(["instance_generation", "binding_id", "previous_generation", "status", "machine_id"])
70
+ .where("device_id", "=", deviceId)
71
+ .where("instance_generation", "=", String(instance.created_at.getTime()))
72
+ .executeTakeFirst();
73
+ if (!binding)
74
+ return null;
75
+ const row = { ...instance, ...binding };
76
+ return {
77
+ orgId: row.org_id,
78
+ status: row.status,
79
+ machineId: row.machine_id,
80
+ request: {
81
+ instanceId: instance.handle.id,
82
+ instanceGeneration: row.instance_generation,
83
+ bindingId: row.binding_id,
84
+ expectedPreviousGeneration: row.previous_generation,
85
+ },
86
+ };
87
+ }
88
+ /**
89
+ * Whether the device's current local instance generation has an installed binding (the staged
90
+ * acquire path's shared-use condition). No committed binding reads as not installed.
91
+ */
92
+ async installed(deviceId) {
93
+ return (await this.status(deviceId)) === "installed";
94
+ }
95
+ /** The current generation's committed binding status, or null when none was committed. */
96
+ async status(deviceId) {
97
+ return (await this.committed(deviceId))?.status ?? null;
98
+ }
99
+ /**
100
+ * The permission support the device attested for its current instance generation (the staged
101
+ * provider's `permissionSupport`): only the report recorded with an installed binding of that
102
+ * generation, for the active machine's program version. Anything else (unbound, awaiting, a
103
+ * changed identity) reads as no support with its reason: nothing is probed, and a missing
104
+ * report never admits an operation.
105
+ */
106
+ async permissionSupport(deviceId) {
107
+ const machine = await this.db
108
+ .selectFrom("device_machines")
109
+ .select("program_version")
110
+ .where("device_id", "=", deviceId)
111
+ .where("revoked_at", "is", null)
112
+ .executeTakeFirst();
113
+ const { availability } = await readCurrentReport(this.db.db, deviceId, LOCAL_PROVIDER, machine?.program_version ?? undefined);
114
+ if (availability.state === "supported")
115
+ return availability.report;
116
+ return {
117
+ version: 1,
118
+ restricted: null,
119
+ fullAccess: false,
120
+ background: false,
121
+ unavailableReason: availability.state === "unsupported"
122
+ ? availability.reason
123
+ : "The device has not attested an installed runtime for this instance yet; reconnect capybara-device and retry",
124
+ };
125
+ }
126
+ /**
127
+ * Sends the committed binding and installs the verified answer. The caller holds the device's
128
+ * lifecycle lease; no transaction is open across the RPC.
129
+ */
130
+ async bind(deviceId, signal) {
131
+ // the device round trip may take the whole operation deadline: never inside a transaction
132
+ if (this.txHost.current())
133
+ throw new InstanceBindingError("bindInstance must not run inside a transaction");
134
+ const found = await this.committed(deviceId);
135
+ if (!found)
136
+ throw new InstanceBindingError("No committed local instance to bind");
137
+ if (found.status === "installed")
138
+ return null;
139
+ // the kernel's durable legacy gate (8.5): an old incarnation of this device that may still
140
+ // run legacy work blocks every new binding; a new generation never clears it
141
+ if (await this.retirement.hasUnresolved(deviceId))
142
+ throw new InstanceBindingError("The device has an unresolved legacy retirement obligation; no instance can be bound");
143
+ const machineId = await this.activeMachine(deviceId);
144
+ let committed = found;
145
+ // a re-login since the commit: the binding follows the machine it is sent to, before the
146
+ // send, so a lost answer leaves the chain naming what that machine's device may have stored
147
+ if (committed.machineId !== machineId) {
148
+ await runOutsideTransaction(() => this.retarget(deviceId, committed, machineId));
149
+ const again = await this.committed(deviceId);
150
+ if (!again)
151
+ throw new InstanceBindingError("No committed local instance to bind");
152
+ committed = again;
153
+ }
154
+ const connection = await this.relay.connection(deviceId);
155
+ const answer = parseOperationResultV3("bindInstance", await this.relay.callV3(deviceId, "bindInstance", { ...committed.request }, {
156
+ deadlineMs: OP_DEADLINE_MS,
157
+ signal,
158
+ connection,
159
+ }), committed.request);
160
+ await runOutsideTransaction(() => this.install(deviceId, machineId, committed, answer, connection.connectionId));
161
+ return answer;
162
+ }
163
+ /**
164
+ * Moves a still awaiting binding to the active machine: its previous becomes the last binding
165
+ * committed for that machine. Under the device writer, against the exact generation and id.
166
+ */
167
+ async retarget(deviceId, committed, machineId) {
168
+ await this.ceilings.lock({ orgId: committed.orgId, deviceId }, "device");
169
+ const { instanceGeneration, bindingId } = committed.request;
170
+ const previous = await this.db
171
+ .selectFrom("device_instance_bindings")
172
+ .select("instance_generation")
173
+ .where("device_id", "=", deviceId)
174
+ .where("machine_id", "=", machineId)
175
+ .where(sql `instance_generation::bigint < ${instanceGeneration}::bigint`)
176
+ .orderBy(sql `instance_generation::bigint`, "desc")
177
+ .limit(1)
178
+ .executeTakeFirst();
179
+ const moved = await this.db
180
+ .updateTable("device_instance_bindings")
181
+ .set({ machine_id: machineId, previous_generation: previous?.instance_generation ?? null })
182
+ .where("device_id", "=", deviceId)
183
+ .where("instance_generation", "=", instanceGeneration)
184
+ .where("binding_id", "=", bindingId)
185
+ .where("status", "=", "awaiting-device")
186
+ .executeTakeFirst();
187
+ if (!moved.numUpdatedRows)
188
+ throw new InstanceBindingError("The binding changed before it could follow the new machine");
189
+ }
190
+ async activeMachine(deviceId) {
191
+ const machine = await this.db
192
+ .selectFrom("device_machines")
193
+ .select("id")
194
+ .where("device_id", "=", deviceId)
195
+ .where("revoked_at", "is", null)
196
+ .executeTakeFirst();
197
+ if (!machine)
198
+ throw new InstanceBindingError("The device has no active machine to bind");
199
+ return machine.id;
200
+ }
201
+ /**
202
+ * Under the device authority writer: the answer must be for the exact committed generation and
203
+ * binding of the current row, from the machine the request went to. A different environment
204
+ * fingerprint than the previous installed generation resets Full consent; the first verified
205
+ * proof of an unprobed device does not (its semantic scope was what the person consented to).
206
+ */
207
+ async install(deviceId, machineId, committed, answer,
208
+ /** The authenticated connection the answer came on (recorded with its owner report). */
209
+ connectionId = null) {
210
+ const subject = { orgId: committed.orgId, deviceId };
211
+ await this.ceilings.lock(subject, "device");
212
+ const { request } = committed;
213
+ const identity = runtimeIdentity(answer.runtimeIdentity);
214
+ if (answer.bindingId !== request.bindingId ||
215
+ answer.instanceGeneration !== request.instanceGeneration ||
216
+ answer.instanceId !== request.instanceId ||
217
+ identity.instanceGeneration !== request.instanceGeneration)
218
+ throw new DeviceProtocolError("Binding answer belongs to another instance generation");
219
+ const instance = await this.db
220
+ .selectFrom("device_instances")
221
+ .select(["provider", "created_at", "handle", "status"])
222
+ .where("device_id", "=", deviceId)
223
+ .forUpdate()
224
+ .executeTakeFirst();
225
+ if (!instance ||
226
+ instance.provider !== LOCAL_PROVIDER ||
227
+ String(instance.created_at.getTime()) !== request.instanceGeneration ||
228
+ instance.handle?.id !== request.instanceId ||
229
+ !["running", "suspended"].includes(instance.status))
230
+ throw new InstanceBindingError("The instance was replaced before its binding completed");
231
+ const binding = await this.db
232
+ .selectFrom("device_instance_bindings")
233
+ .select(["binding_id", "status", "runtime_identity"])
234
+ .where("device_id", "=", deviceId)
235
+ .where("instance_generation", "=", request.instanceGeneration)
236
+ .forUpdate()
237
+ .executeTakeFirst();
238
+ if (!binding || binding.binding_id !== request.bindingId)
239
+ throw new InstanceBindingError("The binding id does not match the committed binding");
240
+ const machine = await this.db
241
+ .selectFrom("device_machines")
242
+ .select(["id", "program_version"])
243
+ .where("device_id", "=", deviceId)
244
+ .where("revoked_at", "is", null)
245
+ .executeTakeFirst();
246
+ if (machine?.id !== machineId)
247
+ throw new InstanceBindingError("The device's machine changed before its binding completed");
248
+ if (!machine.program_version)
249
+ throw new InstanceBindingError("The machine has not reported its program version");
250
+ if (binding.status === "installed")
251
+ return;
252
+ // B2: the answer's authenticated owner report enters the common owner writer; a lower
253
+ // sequence is ignored there and a conflicting one blocks new authority, neither failing the
254
+ // installation
255
+ await this.owners.recordLocked(machineId, answer.ownerCeiling, {
256
+ connectionId,
257
+ via: "bind",
258
+ });
259
+ // the environment this device last proved, whichever generation that was: the chain's
260
+ // previous may be a binding whose answer never arrived (never installed)
261
+ const previous = await this.db
262
+ .selectFrom("device_instance_bindings")
263
+ .select("runtime_identity")
264
+ .where("device_id", "=", deviceId)
265
+ .where("status", "=", "installed")
266
+ .where(sql `instance_generation::bigint < ${request.instanceGeneration}::bigint`)
267
+ .orderBy(sql `instance_generation::bigint`, "desc")
268
+ .limit(1)
269
+ .executeTakeFirst();
270
+ if (previous?.runtime_identity &&
271
+ previous.runtime_identity.imageFingerprint !== identity.imageFingerprint)
272
+ await this.ceilings.publishLocked(subject, "device", true, true);
273
+ await this.db
274
+ .updateTable("device_instance_bindings")
275
+ .set({
276
+ status: "installed",
277
+ machine_id: machineId,
278
+ runtime_identity: JSON.stringify(identity),
279
+ installed_at: sql `now()`,
280
+ })
281
+ .where("device_id", "=", deviceId)
282
+ .where("instance_generation", "=", request.instanceGeneration)
283
+ .where("binding_id", "=", request.bindingId)
284
+ .execute();
285
+ await this.reports.record(deviceId, LOCAL_PROVIDER, machine.program_version, identity, answer.permissionSupport);
286
+ }
287
+ };
288
+ __decorate([
289
+ Transactional()
290
+ ], LocalInstanceBindings.prototype, "retarget", null);
291
+ __decorate([
292
+ Transactional()
293
+ ], LocalInstanceBindings.prototype, "install", null);
294
+ LocalInstanceBindings = __decorate([
295
+ Injectable(),
296
+ __param(0, Inject(KernelDb)),
297
+ __param(1, Inject(TransactionHost)),
298
+ __param(2, Inject(LocalDeviceRelay)),
299
+ __param(3, Inject(PermissionCeilingWrites)),
300
+ __param(4, Inject(PermissionRuntimeReports)),
301
+ __param(5, Inject(OwnerReports)),
302
+ __param(6, Inject(LegacyRetirementStore))
303
+ ], LocalInstanceBindings);
304
+ export { LocalInstanceBindings };
@@ -23,3 +23,8 @@ export declare const deviceResTopic: (requestId: string) => string;
23
23
  export declare const deviceTakeoverTopic: (deviceId: string) => string;
24
24
  /** The machine's credential was revoked: its connection is closed. */
25
25
  export declare const deviceRevokedTopic: (deviceId: string) => string;
26
+ /**
27
+ * Negotiated transport (`{ connectionId, maxFrameBytes }`) of the connection holding one lease
28
+ * token; expires with the connection.
29
+ */
30
+ export declare const deviceTransportKey: (deviceId: string, connectionId: string) => string;
@@ -23,3 +23,8 @@ export const deviceResTopic = (requestId) => `device.res.${requestId}`;
23
23
  export const deviceTakeoverTopic = (deviceId) => `device.takeover.${deviceId}`;
24
24
  /** The machine's credential was revoked: its connection is closed. */
25
25
  export const deviceRevokedTopic = (deviceId) => `device.revoked.${deviceId}`;
26
+ /**
27
+ * Negotiated transport (`{ connectionId, maxFrameBytes }`) of the connection holding one lease
28
+ * token; expires with the connection.
29
+ */
30
+ export const deviceTransportKey = (deviceId, connectionId) => `capk:device-transport:${deviceId}:${connectionId}`;
@@ -1,4 +1,4 @@
1
- import { type ExtensionContext } from "@capybara-hq/plugin-sdk";
1
+ import { type DeviceFrameV3, type ExtensionContext } from "@capybara-hq/plugin-sdk";
2
2
  import type { WebSocket } from "@fastify/websocket";
3
3
  import type { Redis } from "ioredis";
4
4
  import { KernelConfigService } from "../infra/config/kernel-config.service.js";
@@ -42,6 +42,7 @@ declare class DeviceConnection {
42
42
  private readonly socket;
43
43
  readonly identity: MachineIdentity;
44
44
  private readonly credential;
45
+ private readonly staged;
45
46
  readonly token: `${string}-${string}-${string}-${string}-${string}`;
46
47
  state: State;
47
48
  private lease;
@@ -57,7 +58,17 @@ declare class DeviceConnection {
57
58
  private released;
58
59
  /** The hello's record, then each policy frame's, one after another (never rejects). */
59
60
  private recorded;
60
- constructor(gw: LocalDeviceGateway, socket: WebSocket, identity: MachineIdentity, credential: string);
61
+ constructor(gw: LocalDeviceGateway, socket: WebSocket, identity: MachineIdentity, credential: string, staged?: boolean);
62
+ /**
63
+ * The id a staged connection fences requests and results with (sent to the machine in `ready`).
64
+ * Random and separate from the lease token, which stays internal to the kernel.
65
+ */
66
+ readonly connectionId: `${string}-${string}-${string}-${string}-${string}`;
67
+ private effectiveCap;
68
+ private ready;
69
+ /** Consecutive failed capacity publications on ping (this socket's own state). */
70
+ private publishFailures;
71
+ private publishCapacity;
61
72
  get deviceId(): string;
62
73
  start(): void;
63
74
  private onFrame;
@@ -103,7 +114,7 @@ declare class DeviceConnection {
103
114
  * sockets are this process's own; everything other replicas need is in Redis.
104
115
  */
105
116
  export declare class LocalDeviceGateway {
106
- readonly machines: Pick<MachinesService, "authenticate" | "recordHello" | "recordPolicy" | "touch">;
117
+ readonly machines: Pick<MachinesService, "authenticate" | "recordHello" | "recordPolicy" | "recordHelloV3" | "recordPolicyV3" | "touch">;
107
118
  readonly locks: Pick<LockService, "hold">;
108
119
  readonly bus: ExtensionContext["bus"];
109
120
  readonly redis: Redis;
@@ -114,7 +125,7 @@ export declare class LocalDeviceGateway {
114
125
  private stopping;
115
126
  /** How often connections are pinged ({@link PING_INTERVAL_MS}; a test seam). */
116
127
  pingIntervalMs: number;
117
- constructor(machines: Pick<MachinesService, "authenticate" | "recordHello" | "recordPolicy" | "touch">, locks: Pick<LockService, "hold">, bus: ExtensionContext["bus"], redis: Redis, config: KernelConfigService, log: Pick<KernelLogger, "info" | "warn">);
128
+ constructor(machines: Pick<MachinesService, "authenticate" | "recordHello" | "recordPolicy" | "recordHelloV3" | "recordPolicyV3" | "touch">, locks: Pick<LockService, "hold">, bus: ExtensionContext["bus"], redis: Redis, config: KernelConfigService, log: Pick<KernelLogger, "info" | "warn">);
118
129
  graceMs(): number;
119
130
  leaseTtlMs(): number;
120
131
  maxFrameBytes(): number;
@@ -122,6 +133,8 @@ export declare class LocalDeviceGateway {
122
133
  authenticate(credential: string): Promise<MachineIdentity | null>;
123
134
  /** Serves one upgraded socket of an authenticated machine until it closes. */
124
135
  accept(socket: WebSocket, identity: MachineIdentity, credential: string): void;
136
+ /** Dormant authenticated seam; the production HTTP route never calls this until full cutover. */
137
+ acceptStaged(socket: WebSocket, credential: string): Promise<void>;
125
138
  /**
126
139
  * How many devices this process holds the connection of (replica presence reports it): only
127
140
  * sockets past the hello that hold their device's lease, so a socket still claiming or being
@@ -133,4 +146,6 @@ export declare class LocalDeviceGateway {
133
146
  /** Closes every connection (1001) and hands their leases back. */
134
147
  stop(): Promise<void>;
135
148
  }
149
+ /** Staged validation only; active connection acceptance above remains protocol 2. */
150
+ export declare function validateDeviceOperationFrameV3(raw: string, from: "device" | "kernel"): DeviceFrameV3;
136
151
  export {};