@capybara-hq/kernel 0.1.0-rc.68 → 0.1.0-rc.69

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 (215) hide show
  1. package/dist/admin/admin.module.js +2 -0
  2. package/dist/admin/health.service.d.ts +11 -4
  3. package/dist/admin/health.service.js +33 -17
  4. package/dist/background/poll.job.js +2 -2
  5. package/dist/background/tasks.service.d.ts +5 -5
  6. package/dist/background/tasks.service.js +9 -8
  7. package/dist/catalog/marketplaces.service.d.ts +1 -1
  8. package/dist/catalog/requests.service.js +1 -1
  9. package/dist/config/schema.d.ts +1 -1
  10. package/dist/config/schema.js +9 -9
  11. package/dist/conversations/conversations.controller.d.ts +8 -3
  12. package/dist/conversations/conversations.controller.js +8 -3
  13. package/dist/conversations/conversations.service.d.ts +33 -15
  14. package/dist/conversations/conversations.service.js +60 -10
  15. package/dist/conversations/events.d.ts +21 -41
  16. package/dist/conversations/events.js +57 -31
  17. package/dist/conversations/permission-profiles.controller.d.ts +6 -4
  18. package/dist/conversations/permission-profiles.service.d.ts +6 -4
  19. package/dist/ctx/create-ctx.d.ts +20 -1
  20. package/dist/ctx/create-ctx.js +77 -6
  21. package/dist/ctx/device-provider-check.d.ts +6 -2
  22. package/dist/ctx/device-provider-check.js +17 -2
  23. package/dist/db/kernel-schema.d.ts +10 -48
  24. package/dist/devices/access.d.ts +3 -4
  25. package/dist/devices/access.js +3 -4
  26. package/dist/devices/conversation-devices.d.ts +1 -3
  27. package/dist/devices/conversation-devices.js +2 -7
  28. package/dist/devices/device-projects.service.js +24 -29
  29. package/dist/devices/device-provider-selection.d.ts +5 -6
  30. package/dist/devices/device-provider-selection.js +8 -9
  31. package/dist/devices/devices.controller.d.ts +8 -21
  32. package/dist/devices/devices.controller.js +9 -34
  33. package/dist/devices/devices.module.js +6 -4
  34. package/dist/devices/devices.schemas.d.ts +1 -5
  35. package/dist/devices/devices.schemas.js +1 -5
  36. package/dist/devices/devices.service.d.ts +84 -147
  37. package/dist/devices/devices.service.js +134 -229
  38. package/dist/devices/dto.d.ts +5 -32
  39. package/dist/devices/dto.js +5 -37
  40. package/dist/devices/errors.d.ts +6 -5
  41. package/dist/devices/errors.js +7 -8
  42. package/dist/devices/project-rules.d.ts +9 -11
  43. package/dist/devices/project-rules.js +7 -7
  44. package/dist/devices/working-root.d.ts +3 -6
  45. package/dist/devices/working-root.js +3 -8
  46. package/dist/dial-in/access.d.ts +23 -0
  47. package/dist/dial-in/access.js +64 -0
  48. package/dist/{local-devices/device-auth.controller.d.ts → dial-in/auth.controller.d.ts} +12 -6
  49. package/dist/{local-devices/device-auth.controller.js → dial-in/auth.controller.js} +30 -15
  50. package/dist/dial-in/auth.module.d.ts +7 -0
  51. package/dist/dial-in/auth.module.js +27 -0
  52. package/dist/{local-devices/device-auth.d.ts → dial-in/auth.service.d.ts} +42 -13
  53. package/dist/{local-devices/device-auth.js → dial-in/auth.service.js} +83 -32
  54. package/dist/dial-in/dial-in.module.d.ts +9 -0
  55. package/dist/dial-in/dial-in.module.js +37 -0
  56. package/dist/{local-devices → dial-in}/dto.d.ts +17 -2
  57. package/dist/{local-devices → dial-in}/dto.js +14 -2
  58. package/dist/dial-in/envelope.d.ts +95 -0
  59. package/dist/dial-in/envelope.js +89 -0
  60. package/dist/dial-in/gateway.d.ts +214 -0
  61. package/dist/{local-devices/local-device-gateway.js → dial-in/gateway.js} +337 -181
  62. package/dist/dial-in/handlers.d.ts +47 -0
  63. package/dist/dial-in/handlers.js +84 -0
  64. package/dist/dial-in/machines.d.ts +155 -0
  65. package/dist/dial-in/machines.js +383 -0
  66. package/dist/dial-in/relay.d.ts +135 -0
  67. package/dist/dial-in/relay.js +444 -0
  68. package/dist/dial-in/relay.module.d.ts +7 -0
  69. package/dist/dial-in/relay.module.js +24 -0
  70. package/dist/{local-devices/device-routes.d.ts → dial-in/routes.d.ts} +19 -11
  71. package/dist/{local-devices/device-routes.js → dial-in/routes.js} +45 -25
  72. package/dist/{local-devices → dial-in}/targets.d.ts +3 -18
  73. package/dist/{local-devices → dial-in}/targets.js +3 -48
  74. package/dist/errors.d.ts +8 -27
  75. package/dist/errors.js +8 -27
  76. package/dist/extensions/bootstrap.d.ts +5 -0
  77. package/dist/extensions/bootstrap.js +10 -1
  78. package/dist/extensions/extensions.module.js +6 -0
  79. package/dist/extensions/host.d.ts +6 -0
  80. package/dist/extensions/host.js +12 -2
  81. package/dist/extensions/permission-set.d.ts +15 -1
  82. package/dist/extensions/permission-set.js +11 -1
  83. package/dist/extensions/permission-view.d.ts +1 -1
  84. package/dist/extensions/permission-view.js +4 -1
  85. package/dist/http/api.module.js +4 -4
  86. package/dist/http/gateway-routes.d.ts +1 -1
  87. package/dist/http/gateway-routes.js +1 -1
  88. package/dist/http/meta.controller.js +1 -1
  89. package/dist/identity/settings.d.ts +1 -1
  90. package/dist/identity/settings.js +6 -8
  91. package/dist/infra/config/kernel-config.service.js +2 -2
  92. package/dist/infra/outbox/registry.d.ts +0 -14
  93. package/dist/infra/outbox/registry.js +0 -15
  94. package/dist/infra/throttler/named-throttlers.d.ts +1 -1
  95. package/dist/infra/throttler/named-throttlers.js +1 -1
  96. package/dist/kernel.module.js +0 -3
  97. package/dist/orgs/settings.service.js +5 -10
  98. package/dist/overrides.d.ts +0 -4
  99. package/dist/overrides.js +0 -2
  100. package/dist/platform/exclusions.js +5 -5
  101. package/dist/platform/ops/admin.ops.js +1 -1
  102. package/dist/platform/ops/conversations.ops.js +5 -2
  103. package/dist/platform/ops/devices.ops.js +11 -24
  104. package/dist/platform/ops/inbox.ops.js +1 -1
  105. package/dist/roles/gateway-role.d.ts +3 -3
  106. package/dist/roles/gateway-role.js +4 -4
  107. package/dist/roles/gateway.module.d.ts +1 -1
  108. package/dist/roles/gateway.module.js +5 -5
  109. package/dist/runtime/context/device-paths.d.ts +4 -1
  110. package/dist/runtime/context/permission-profile.js +5 -5
  111. package/dist/runtime/maintenance.d.ts +2 -5
  112. package/dist/runtime/maintenance.js +9 -19
  113. package/dist/runtime/project/knowledge.js +4 -5
  114. package/dist/runtime/project/knowledge.service.js +2 -1
  115. package/dist/sandbox/cloud-permission-snapshot.d.ts +1 -1
  116. package/dist/sandbox/cloud-permission-snapshot.js +1 -3
  117. package/dist/sandbox/device-reports.d.ts +74 -14
  118. package/dist/sandbox/device-reports.js +190 -33
  119. package/dist/sandbox/device-reports.module.d.ts +7 -0
  120. package/dist/sandbox/device-reports.module.js +25 -0
  121. package/dist/sandbox/device-sandbox.d.ts +39 -41
  122. package/dist/sandbox/device-sandbox.js +44 -83
  123. package/dist/sandbox/kernel-read.d.ts +2 -2
  124. package/dist/sandbox/legacy-retirement.d.ts +11 -59
  125. package/dist/sandbox/legacy-retirement.js +34 -186
  126. package/dist/sandbox/legacy-retirement.module.d.ts +2 -2
  127. package/dist/sandbox/legacy-retirement.module.js +2 -2
  128. package/dist/sandbox/managed-device-context.d.ts +16 -4
  129. package/dist/sandbox/managed-device-context.js +25 -8
  130. package/dist/sandbox/managed-device-context.module.js +4 -0
  131. package/dist/sandbox/modes.d.ts +3 -12
  132. package/dist/sandbox/modes.js +2 -14
  133. package/dist/{local-devices → sandbox}/network-grants.d.ts +2 -21
  134. package/dist/{local-devices → sandbox}/network-grants.js +4 -49
  135. package/dist/sandbox/operation-authority-types.d.ts +2 -1
  136. package/dist/sandbox/operation-authority.d.ts +6 -20
  137. package/dist/sandbox/operation-authority.js +43 -104
  138. package/dist/sandbox/operation-authority.module.js +1 -1
  139. package/dist/sandbox/operation-intents.d.ts +4 -1
  140. package/dist/sandbox/operation-intents.js +11 -2
  141. package/dist/sandbox/operation-reconcile.job.js +2 -4
  142. package/dist/sandbox/permission-availability.d.ts +6 -7
  143. package/dist/sandbox/permission-availability.js +7 -10
  144. package/dist/sandbox/permission-call-grants.d.ts +2 -1
  145. package/dist/sandbox/permission-call-grants.js +1 -7
  146. package/dist/sandbox/permission-call-types.d.ts +4 -4
  147. package/dist/sandbox/permission-ceiling-writes.d.ts +7 -23
  148. package/dist/sandbox/permission-ceiling-writes.js +8 -35
  149. package/dist/sandbox/permission-config-publication.d.ts +9 -1
  150. package/dist/sandbox/permission-config-publication.js +29 -21
  151. package/dist/sandbox/permission-context-types.d.ts +17 -10
  152. package/dist/sandbox/permission-facts.d.ts +98 -95
  153. package/dist/sandbox/permission-facts.js +160 -314
  154. package/dist/sandbox/permission-instance-writes.d.ts +23 -37
  155. package/dist/sandbox/permission-instance-writes.js +44 -121
  156. package/dist/sandbox/permission-runtime-reports.d.ts +38 -30
  157. package/dist/sandbox/permission-runtime-reports.js +105 -122
  158. package/dist/sandbox/providers.d.ts +26 -38
  159. package/dist/sandbox/providers.js +36 -18
  160. package/dist/sandbox/request-not-sent.d.ts +2 -13
  161. package/dist/sandbox/request-not-sent.js +2 -13
  162. package/dist/sandbox/review/permission-candidate.d.ts +13 -8
  163. package/dist/sandbox/review/permission-candidate.js +38 -48
  164. package/dist/sandbox/sandbox.module.js +7 -3
  165. package/dist/sandbox/sandbox.service.d.ts +49 -59
  166. package/dist/sandbox/sandbox.service.js +139 -259
  167. package/dist/schedules/fire.d.ts +15 -6
  168. package/dist/schedules/fire.js +27 -13
  169. package/dist/schedules/grant-rules.js +1 -1
  170. package/dist/schedules/scheduled-tasks.service.d.ts +0 -7
  171. package/dist/schedules/scheduled-tasks.service.js +1 -4
  172. package/dist/schedules/schedules.module.js +5 -3
  173. package/dist/skills/invoke.d.ts +1 -1
  174. package/dist/skills/invoke.js +2 -3
  175. package/dist/tools/approvals.service.d.ts +8 -5
  176. package/dist/tools/approvals.service.js +5 -5
  177. package/dist/tools/builtins/sandbox/bash.js +1 -2
  178. package/dist/tools/builtins/sandbox/common.d.ts +15 -9
  179. package/dist/tools/builtins/sandbox/common.js +37 -38
  180. package/dist/tools/network-asks.d.ts +19 -22
  181. package/dist/tools/network-asks.js +15 -21
  182. package/dist/tools/pipeline.d.ts +6 -7
  183. package/dist/tools/pipeline.js +17 -21
  184. package/dist/tools/tools.module.js +3 -7
  185. package/package.json +3 -4
  186. package/src/migrations/0177_dial_in_contract.sql +32 -0
  187. package/dist/local-devices/device-auth.module.d.ts +0 -7
  188. package/dist/local-devices/device-auth.module.js +0 -25
  189. package/dist/local-devices/device-policy.service.d.ts +0 -213
  190. package/dist/local-devices/device-policy.service.js +0 -605
  191. package/dist/local-devices/instance-bindings.d.ts +0 -82
  192. package/dist/local-devices/instance-bindings.js +0 -307
  193. package/dist/local-devices/local-device-gateway.d.ts +0 -153
  194. package/dist/local-devices/local-device-provider.d.ts +0 -122
  195. package/dist/local-devices/local-device-provider.js +0 -326
  196. package/dist/local-devices/local-devices.module.d.ts +0 -31
  197. package/dist/local-devices/local-devices.module.js +0 -100
  198. package/dist/local-devices/machines.d.ts +0 -211
  199. package/dist/local-devices/machines.js +0 -766
  200. package/dist/local-devices/owner-report.d.ts +0 -81
  201. package/dist/local-devices/owner-report.js +0 -150
  202. package/dist/local-devices/owner-reports.d.ts +0 -48
  203. package/dist/local-devices/owner-reports.js +0 -208
  204. package/dist/local-devices/policy-op-jobs.d.ts +0 -15
  205. package/dist/local-devices/policy-op-jobs.js +0 -12
  206. package/dist/local-devices/policy-summary.d.ts +0 -102
  207. package/dist/local-devices/policy-summary.js +0 -155
  208. package/dist/local-devices/relay.d.ts +0 -166
  209. package/dist/local-devices/relay.js +0 -559
  210. package/dist/tools/device-op-resolver.d.ts +0 -34
  211. package/dist/tools/device-op-resolver.js +0 -120
  212. /package/dist/{local-devices → dial-in}/keys.d.ts +0 -0
  213. /package/dist/{local-devices → dial-in}/keys.js +0 -0
  214. /package/dist/{local-devices → sandbox}/network-grants.module.d.ts +0 -0
  215. /package/dist/{local-devices → sandbox}/network-grants.module.js +0 -0
@@ -8,8 +8,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
8
  return function (target, key) { decorator(target, key, paramIndex); }
9
9
  };
10
10
  import { createHash } from "node:crypto";
11
- import { instanceSupported, parseSandboxPermissionSupport, } from "@capybara-hq/plugin-sdk";
11
+ import { instanceSupported, } from "@capybara-hq/plugin-sdk";
12
+ import { parseSandboxPermissionSupport } from "@capybara-hq/plugin-sdk/device-protocol";
12
13
  import { Inject, Injectable } from "@nestjs/common";
14
+ import { sql } from "kysely";
13
15
  import { KernelDb } from "../infra/database/kernel-db.js";
14
16
  import { TransactionHost } from "../infra/database/transaction-host.js";
15
17
  import { Transactional } from "../infra/database/transactional.decorator.js";
@@ -17,7 +19,6 @@ import { canonicalJson } from "../tools/canonical-json.js";
17
19
  import { readInstanceLimits } from "./modes.js";
18
20
  import { PermissionAuthorityLock } from "./permission-authority-lock.js";
19
21
  import { unprobedPermissionAvailability, } from "./permission-availability.js";
20
- import { LOCAL_PROVIDER } from "./providers.js";
21
22
  export const sameRuntimeIdentity = (a, b) => permissionScopeHash(a) === permissionScopeHash(b);
22
23
  const permissionScopeHash = (identity) => createHash("sha256")
23
24
  .update(JSON.stringify([
@@ -45,23 +46,24 @@ export function runtimeIdentity(value) {
45
46
  }
46
47
  const sha256 = (value) => createHash("sha256").update(canonicalJson(value)).digest("hex");
47
48
  /**
48
- * The kernel-built runtime identity of a cloud instance (B-15, Q1 option A). A cloud provider is
49
- * an in-process extension (full trust), so its own permission support report, bound to these
50
- * kernel facts, is the strongest evidence available without an SDK change: the provider plugin
51
- * and version (`runtimeHash`), the committed instance handle (`helperHash`), the recorded applied
52
- * limits with no provider-attested image digest (`imageFingerprint`) and the instance generation.
53
- * Any of them changing makes a recorded report unprobed.
49
+ * The kernel-built runtime identity of a device's instance (B-15, Q1 option A; device-provider
50
+ * spec §6.7), one rule for every provider. A provider is an in-process extension (full trust), so
51
+ * its own permission support report, bound to these kernel facts, is the evidence: the provider
52
+ * plugin and version (`runtimeHash`), the committed instance handle (`helperHash`), the recorded
53
+ * applied limits with no provider-attested image digest and the device's stored report revision
54
+ * (`imageFingerprint`), and the instance generation. Any of them changing (a new report revision
55
+ * too) makes a recorded report unprobed until the lifecycle records it again.
54
56
  */
55
- export function cloudRuntimeIdentity(input) {
57
+ export function runtimeIdentityOf(input) {
56
58
  return {
57
59
  runtimeHash: sha256({
58
- kind: "cloud-provider",
60
+ kind: "device-provider",
59
61
  provider: input.provider,
60
62
  version: input.version,
61
63
  }),
62
- helperHash: sha256({ kind: "cloud-instance", handle: input.handle }),
64
+ helperHash: sha256({ kind: "device-instance", handle: input.handle }),
63
65
  imageFingerprint: sha256({
64
- kind: "cloud-environment",
66
+ kind: "device-environment",
65
67
  applied: {
66
68
  mode: input.applied.mode,
67
69
  network: {
@@ -69,6 +71,7 @@ export function cloudRuntimeIdentity(input) {
69
71
  hosts: [...new Set(input.applied.network.hosts)].sort(),
70
72
  },
71
73
  },
74
+ reportRevision: input.reportRevision,
72
75
  imageDigest: null,
73
76
  }),
74
77
  instanceGeneration: input.instanceGeneration,
@@ -76,11 +79,11 @@ export function cloudRuntimeIdentity(input) {
76
79
  }
77
80
  /**
78
81
  * A permission support report strictly parsed (`parseSandboxPermissionSupport`, version 2), or null
79
- * when it is not one: a cloud provider's answer (an in-process extension is trusted, but its answer
80
- * is stored and read back later, so nothing beyond the contract's fields is kept) and a stored
81
- * report read back (one recorded before version 2 is no report).
82
+ * when it is not one: a provider's answer (an in-process extension is trusted, but its answer is
83
+ * stored and read back later, so nothing beyond the contract's fields is kept) and a stored report
84
+ * read back (one recorded before version 2 is no report).
82
85
  */
83
- export function cloudSupportOf(value) {
86
+ export function supportOf(value) {
84
87
  try {
85
88
  return parseSandboxPermissionSupport(value);
86
89
  }
@@ -88,38 +91,49 @@ export function cloudSupportOf(value) {
88
91
  return null;
89
92
  }
90
93
  }
94
+ /** The revision of the device's stored report (`devices.report`), or null when none is stored. */
95
+ async function storedReportRevision(db, deviceId) {
96
+ const row = await db
97
+ .selectFrom("devices")
98
+ .select(sql `report->>'revision'`.as("revision"))
99
+ .where("id", "=", deviceId)
100
+ .executeTakeFirst();
101
+ return row?.revision ?? null;
102
+ }
91
103
  /**
92
- * The cloud identity the instance row proves now, or null when it proves none: no live instance
93
- * of this provider, no handle or no recorded applied limits.
104
+ * The identity the instance row proves now, or null when it proves none: no live instance of this
105
+ * provider, no handle or no recorded applied limits.
94
106
  */
95
- function currentCloudIdentity(instance, provider, version) {
107
+ function currentIdentity(instance, provider, version, reportRevision) {
96
108
  const applied = readInstanceLimits(instance.instance_limits).applied;
97
109
  if (instance.provider !== provider ||
98
110
  !instance.handle ||
99
111
  applied === null ||
100
112
  (instance.status !== "running" && instance.status !== "suspended"))
101
113
  return null;
102
- return cloudRuntimeIdentity({
114
+ return runtimeIdentityOf({
103
115
  provider,
104
116
  version,
105
117
  handle: instance.handle,
106
118
  instanceGeneration: String(instance.created_at.getTime()),
107
119
  applied,
120
+ reportRevision,
108
121
  });
109
122
  }
110
123
  /**
111
- * What a cloud instance can enforce of its provider's report (B-12, spec §124): Full access needs
112
- * the instance itself to hold the outer boundary. An instance applied in the OS sandbox mode
113
- * holds its outer network only through the restricted child filters that Full access leaves, so
114
- * Full reads as unsupported there whatever the provider reports; restricted support stands.
124
+ * What an instance can enforce of its provider's report (B-12, spec §124): running outside the
125
+ * sandbox needs the instance applied in the `auto` tier (the platform and the org allowing it when
126
+ * it was made). An instance applied in the OS sandbox tier holds its outer network only through
127
+ * the restricted child filters that an escalated call leaves, so outside-sandbox reads as
128
+ * unsupported there whatever the provider reports; restricted support stands.
115
129
  */
116
- export function cloudEnforceable(report, instanceLimits) {
130
+ export function enforceable(report, instanceLimits) {
117
131
  if (!report.outsideSandbox || readInstanceLimits(instanceLimits).applied?.mode === "auto")
118
132
  return report;
119
133
  return {
120
134
  ...report,
121
135
  outsideSandbox: false,
122
- unavailableReason: "This cloud instance enforces its outer network only inside the OS sandbox, so it cannot run Full access",
136
+ unavailableReason: "This instance enforces its outer network only inside the OS sandbox, so it cannot run a command outside it",
123
137
  };
124
138
  }
125
139
  /** No probe, provisioning or provider IO. A changed identity makes the previous report unprobed. */
@@ -128,18 +142,20 @@ export async function readRuntimeReport(db, deviceId, provider, version) {
128
142
  }
129
143
  /**
130
144
  * {@link readRuntimeReport} plus the identity hash of the current report (null unless it is
131
- * current): a cloud device's installed evidence names its report by it.
145
+ * current, which names the installed evidence) and the support it recorded (null unless it is
146
+ * current), whatever availability that support gives.
132
147
  */
133
148
  export async function readCurrentReport(db, deviceId, provider, version) {
134
149
  const unprobed = {
135
150
  availability: unprobedPermissionAvailability(),
136
151
  identityHash: null,
137
152
  instanceGeneration: null,
153
+ support: null,
138
154
  };
139
155
  if (!version)
140
156
  return unprobed;
141
- const [device, instance] = await Promise.all([
142
- db.selectFrom("devices").select("source").where("id", "=", deviceId).executeTakeFirst(),
157
+ const [revision, instance] = await Promise.all([
158
+ storedReportRevision(db, deviceId),
143
159
  db
144
160
  .selectFrom("device_instances")
145
161
  .select([
@@ -155,27 +171,11 @@ export async function readCurrentReport(db, deviceId, provider, version) {
155
171
  ]);
156
172
  if (!instance || instance.provider !== provider || !instance.permission_runtime_identity)
157
173
  return unprobed;
158
- // A cloud instance attests only through the kernel-built identity its current row proves (B-15):
159
- // a changed provider version, handle, generation or applied limits is unprobed.
160
- if (device?.source === "cloud") {
161
- const current = currentCloudIdentity(instance, provider, version);
162
- if (!current || !sameRuntimeIdentity(current, instance.permission_runtime_identity))
163
- return unprobed;
164
- }
165
- // A local instance attests only through its installed post-commit binding (B4): an unbound or
166
- // still awaiting generation, or a report differing from the bound identity, is unprobed.
167
- if (provider === LOCAL_PROVIDER) {
168
- const binding = await db
169
- .selectFrom("device_instance_bindings")
170
- .select(["status", "runtime_identity"])
171
- .where("device_id", "=", deviceId)
172
- .where("instance_generation", "=", String(instance.created_at.getTime()))
173
- .executeTakeFirst();
174
- if (binding?.status !== "installed" ||
175
- !binding.runtime_identity ||
176
- !sameRuntimeIdentity(binding.runtime_identity, instance.permission_runtime_identity))
177
- return unprobed;
178
- }
174
+ // an instance attests only through the kernel-built identity its current row proves (B-15): a
175
+ // changed provider version, handle, generation, applied limits or report revision is unprobed
176
+ const current = currentIdentity(instance, provider, version, revision);
177
+ if (!current || !sameRuntimeIdentity(current, instance.permission_runtime_identity))
178
+ return unprobed;
179
179
  const row = await db
180
180
  .selectFrom("device_permission_reports")
181
181
  .selectAll()
@@ -187,10 +187,15 @@ export async function readCurrentReport(db, deviceId, provider, version) {
187
187
  row.instance_generation !== String(instance.created_at.getTime()) ||
188
188
  row.identity_hash !== permissionScopeHash(instance.permission_runtime_identity))
189
189
  return unprobed;
190
- const stored = cloudSupportOf(row.report);
190
+ const stored = supportOf(row.report);
191
191
  if (stored === null)
192
192
  return unprobed;
193
- const report = device?.source === "cloud" ? cloudEnforceable(stored, instance.instance_limits) : stored;
193
+ const report = enforceable(stored, instance.instance_limits);
194
+ const evidence = {
195
+ identityHash: row.identity_hash,
196
+ instanceGeneration: row.instance_generation,
197
+ support: report,
198
+ };
194
199
  // an instance-boundary provider is a supported device whose scope is the whole instance,
195
200
  // unless its report names why it is unavailable (a stale image, a digest mismatch)
196
201
  if (!report.restricted && !instanceSupported(report) && !report.outsideSandbox)
@@ -200,34 +205,30 @@ export async function readCurrentReport(db, deviceId, provider, version) {
200
205
  reason: report.unavailableReason ?? "This runtime cannot enforce permission operations",
201
206
  cause: { kind: "runtime" },
202
207
  },
203
- identityHash: row.identity_hash,
204
- instanceGeneration: row.instance_generation,
208
+ ...evidence,
205
209
  };
206
- return {
207
- availability: { state: "supported", report },
208
- identityHash: row.identity_hash,
209
- instanceGeneration: row.instance_generation,
210
- };
210
+ return { availability: { state: "supported", report }, ...evidence };
211
211
  }
212
212
  /**
213
- * Whether a cloud device's current instance owes a new runtime report (B-15): it can prove a
213
+ * Whether a device's current instance owes a new runtime report (B-15): it can prove a
214
214
  * kernel-built identity (a live instance of this provider version with a handle and recorded
215
- * applied limits) but no current report is recorded for it. Database reads only.
215
+ * applied limits) but no current report is recorded for it (none yet, or one recorded under an
216
+ * earlier report revision). Database reads only.
216
217
  */
217
- export async function cloudReportOwed(db, deviceId, provider, version) {
218
+ export async function reportOwed(db, deviceId, provider, version) {
218
219
  if (!version)
219
220
  return false;
220
- const [device, instance] = await Promise.all([
221
- db.selectFrom("devices").select("source").where("id", "=", deviceId).executeTakeFirst(),
221
+ const [revision, instance] = await Promise.all([
222
+ storedReportRevision(db, deviceId),
222
223
  db
223
224
  .selectFrom("device_instances")
224
225
  .select(["provider", "status", "handle", "created_at", "instance_limits"])
225
226
  .where("device_id", "=", deviceId)
226
227
  .executeTakeFirst(),
227
228
  ]);
228
- if (device?.source !== "cloud" || !instance)
229
+ if (!instance)
229
230
  return false;
230
- if (!currentCloudIdentity(instance, provider, version))
231
+ if (!currentIdentity(instance, provider, version, revision))
231
232
  return false;
232
233
  return (await readCurrentReport(db, deviceId, provider, version)).identityHash === null;
233
234
  }
@@ -240,16 +241,30 @@ let PermissionRuntimeReports = class PermissionRuntimeReports {
240
241
  this.txHost = txHost;
241
242
  this.locks = locks;
242
243
  }
243
- /** Caller holds the device authority writer lock and validates authenticated hello/source identity. */
244
- async record(deviceId, provider, version, identity, report) {
244
+ /**
245
+ * Records an instance's permission support with its kernel-built identity
246
+ * ({@link runtimeIdentityOf}, B-15), read from the committed row under the caller's device
247
+ * authority writer lock: the lifecycle holder calls it after create (in the handle commit's
248
+ * transaction) or after resume and limits changes, with the support it read from that exact
249
+ * instance under its lease. An instance of another provider records nothing; one without a
250
+ * handle or without recorded applied limits records nothing new and clears a stale identity.
251
+ * The support recorded is also stored with the device's report (`devices.report.support`), so
252
+ * the stored support never lags what the runtime last said: the caller holds the device row
253
+ * lock (taken before the authority writer lock, the device writers' order).
254
+ */
255
+ async recordSupport(deviceId, provider, version, report) {
245
256
  this.locks.assertHeld("device", deviceId, true);
246
- const instance = await this.db
247
- .selectFrom("device_instances")
248
- .select(["created_at", "provider"])
249
- .where("device_id", "=", deviceId)
250
- .executeTakeFirst();
257
+ const [revision, instance] = await Promise.all([
258
+ storedReportRevision(this.db.db, deviceId),
259
+ this.db
260
+ .selectFrom("device_instances")
261
+ .select(["provider", "status", "handle", "created_at", "instance_limits"])
262
+ .where("device_id", "=", deviceId)
263
+ .executeTakeFirst(),
264
+ ]);
251
265
  if (!instance || instance.provider !== provider)
252
266
  return;
267
+ const identity = currentIdentity(instance, provider, version, revision);
253
268
  if (!identity) {
254
269
  await this.db
255
270
  .updateTable("device_instances")
@@ -259,65 +274,36 @@ let PermissionRuntimeReports = class PermissionRuntimeReports {
259
274
  return;
260
275
  }
261
276
  const checked = runtimeIdentity(identity);
262
- if (checked.instanceGeneration !== String(instance.created_at.getTime()))
263
- throw new Error("Runtime report belongs to another instance generation");
264
277
  await this.db
265
278
  .updateTable("device_instances")
266
279
  .set({ permission_runtime_identity: JSON.stringify(checked) })
267
280
  .where("device_id", "=", deviceId)
268
281
  .execute();
269
- await this.db
270
- .insertInto("device_permission_reports")
271
- .values({
272
- device_id: deviceId,
282
+ const values = {
273
283
  provider,
274
284
  provider_version: version,
275
285
  instance_generation: checked.instanceGeneration,
276
286
  identity_hash: permissionScopeHash(checked),
277
287
  report: JSON.stringify(report),
278
- })
279
- .onConflict((oc) => oc.column("device_id").doUpdateSet({
280
- provider,
281
- provider_version: version,
282
- instance_generation: checked.instanceGeneration,
283
- identity_hash: permissionScopeHash(checked),
284
- report: JSON.stringify(report),
285
- observed_at: new Date(),
286
- }))
288
+ };
289
+ await this.db
290
+ .insertInto("device_permission_reports")
291
+ .values({ device_id: deviceId, ...values })
292
+ .onConflict((oc) => oc.column("device_id").doUpdateSet({ ...values, observed_at: new Date() }))
287
293
  .execute();
288
- }
289
- /**
290
- * Records a cloud instance's permission support with its kernel-built identity
291
- * ({@link cloudRuntimeIdentity}, B-15), read from the committed row under the caller's device
292
- * authority writer lock: the lifecycle holder calls it after create (in the handle commit's
293
- * transaction) or after resume and limits changes, with the support it read from that exact
294
- * instance under its lease. A local device, an instance of another provider, one without a
295
- * handle or without recorded applied limits records nothing new (and clears a stale identity).
296
- */
297
- async recordCloud(deviceId, provider, version, report) {
298
- this.locks.assertHeld("device", deviceId, true);
299
- const device = await this.db
300
- .selectFrom("devices")
301
- .select("source")
294
+ await this.db
295
+ .updateTable("devices")
296
+ .set({
297
+ report: sql `COALESCE(report, '{}'::jsonb) || jsonb_build_object('support', ${JSON.stringify(report)}::jsonb)`,
298
+ })
302
299
  .where("id", "=", deviceId)
303
- .executeTakeFirst();
304
- if (device?.source !== "cloud")
305
- return;
306
- const instance = await this.db
307
- .selectFrom("device_instances")
308
- .select(["provider", "status", "handle", "created_at", "instance_limits"])
309
- .where("device_id", "=", deviceId)
310
- .executeTakeFirst();
311
- if (!instance || instance.provider !== provider)
312
- return;
313
- const identity = currentCloudIdentity(instance, provider, version);
314
- await this.record(deviceId, provider, version, identity ?? undefined, report);
300
+ .execute();
315
301
  }
316
302
  /**
317
- * Clears a cloud instance's recorded identity (its report then reads unprobed). Caller holds
318
- * the device authority writer lock and has checked the instance it probed.
303
+ * Clears an instance's recorded identity (its report then reads unprobed). Caller holds the
304
+ * device authority writer lock and has checked the instance it probed.
319
305
  */
320
- async clearCloud(deviceId, provider) {
306
+ async clearSupport(deviceId, provider) {
321
307
  this.locks.assertHeld("device", deviceId, true);
322
308
  await this.db
323
309
  .updateTable("device_instances")
@@ -329,13 +315,10 @@ let PermissionRuntimeReports = class PermissionRuntimeReports {
329
315
  };
330
316
  __decorate([
331
317
  Transactional({ propagation: "mandatory" })
332
- ], PermissionRuntimeReports.prototype, "record", null);
333
- __decorate([
334
- Transactional({ propagation: "mandatory" })
335
- ], PermissionRuntimeReports.prototype, "recordCloud", null);
318
+ ], PermissionRuntimeReports.prototype, "recordSupport", null);
336
319
  __decorate([
337
320
  Transactional({ propagation: "mandatory" })
338
- ], PermissionRuntimeReports.prototype, "clearCloud", null);
321
+ ], PermissionRuntimeReports.prototype, "clearSupport", null);
339
322
  PermissionRuntimeReports = __decorate([
340
323
  Injectable(),
341
324
  __param(0, Inject(KernelDb)),
@@ -1,67 +1,55 @@
1
1
  import { type DeviceProvider } from "@capybara-hq/plugin-sdk";
2
2
  import { ExtensionRegistry } from "../extensions/registry.js";
3
- /** The kernel-internal provider of connected machines (local devices). */
4
- export declare const LOCAL_PROVIDER = "@local";
5
3
  /**
6
- * A kernel-internal provider name: it starts with `@`, which no extension name can
7
- * (`EXTENSION_NAME_RE`), so it never collides with a loaded `device-provider@1` plugin.
4
+ * Whether a provider serves its devices over the dial-in channel: it implements
5
+ * `DeviceProvider.dialIn` (an extension may only when it declares `permissions.dialIn`, checked
6
+ * when it provides the point). Its devices are online while a gateway replica holds their machine's
7
+ * connection (`DialInRelay.online`).
8
8
  */
9
- export declare const isInternalProvider: (name: string) => boolean;
10
- /**
11
- * Post-commit binding of a committed instance generation (permission runtime cutover B4/B-8).
12
- * Present only on the staged v3 registration of a kernel-internal provider; protocol 2 has none.
13
- */
14
- export interface SandboxInstanceBinder {
15
- /**
16
- * The binding committed for the device's current instance generation: installed, still awaiting
17
- * the device, or null when none was committed (an instance from before the staged path).
18
- */
19
- status(deviceId: string): Promise<"installed" | "awaiting-device" | null>;
20
- /**
21
- * Sends the committed binding (the same binding id on every retry) and installs the verified
22
- * answer; refuses while a legacy retirement obligation is open. Never inside a transaction.
23
- */
24
- bind(deviceId: string, signal: AbortSignal): Promise<unknown>;
25
- }
26
- /** A device provider: its name (plugin name, or an internal `@…` name) and implementation. */
9
+ export declare const servesDialIn: (entry: Pick<DeviceProviderEntry, "impl">) => boolean;
10
+ /** A device provider: its name (plugin name, or an in-process `@…` name) and implementation. */
27
11
  export interface DeviceProviderEntry {
28
12
  plugin: string;
29
- /** The loaded extension's version; a kernel-internal provider reports the kernel version. */
13
+ /** The loaded extension's version; an in-process provider reports its own. */
30
14
  version: string;
31
15
  impl: DeviceProvider;
32
- /**
33
- * The staged path's instance binder: the exclusive lifecycle holder binds the generation before
34
- * it downgrades, and shared use waits for an installed binding. Absent on protocol 2.
35
- */
36
- binder?: SandboxInstanceBinder;
37
16
  }
38
17
  /**
39
18
  * The device providers of this process (a registry: which of them serve an org is
40
- * `DeviceProviderSelection`). A cloud provider is a loaded `device-provider@1` extension,
41
- * read from the {@link ExtensionRegistry} at every call (providers are ready only after the
42
- * extension load, and change with installs). Kernel-internal providers (`@local`) are registered by their
43
- * module on init, in every replica: they are code, not shared state.
19
+ * `DeviceProviderSelection`). A provider is a loaded `device-provider@1` extension, read from the
20
+ * {@link ExtensionRegistry} at every call (providers are ready only after the extension load, and
21
+ * change with installs). An in-process provider (an `@…` name, {@link register}) is code
22
+ * registered in every replica, never shared state; the kernel ships none (tests register stubs).
44
23
  *
45
24
  * Every `device-provider@1` extension is handed out guarded: a network-level failure to reach it is
46
25
  * rethrown as `SandboxProviderUnreachableError` (`./unreachable.ts`), so the tools can tell it
47
- * apart. Internal providers are not: their failures (the device relay's Redis, the machine's
48
- * connection) are the kernel's own and have their own errors.
26
+ * apart. In-process providers are not.
49
27
  */
50
28
  export declare class DeviceProviders {
51
29
  private readonly registry;
52
- private readonly internal;
30
+ private readonly inProcess;
53
31
  constructor(registry: Pick<ExtensionRegistry, "list">);
54
- /** Registers a kernel-internal provider (an `@…` name); a later registration replaces it. */
32
+ /** Registers an in-process provider (an `@…` name); a later registration replaces it. */
55
33
  register(entry: DeviceProviderEntry): void;
34
+ /**
35
+ * Whether `plugin` names a registered in-process provider: no catalog item and no org switch,
36
+ * so it serves every org of this process (`DeviceProviderSelection.isOnInOrg`).
37
+ */
38
+ registered(plugin: string): boolean;
56
39
  /**
57
40
  * The provider of a device, from its `devices.provider` (fixed at creation): a registered
58
- * internal one (`@local`), else the loaded `device-provider@1` extension of that plugin name,
41
+ * in-process one, else the loaded `device-provider@1` extension of that plugin name,
59
42
  * on in no org in particular (whether it serves the device's org is
60
43
  * `DeviceProviderSelection.isOnInOrg`). Undefined when neither exists, or for a device without
61
44
  * a provider (`null`). Never another provider in its place.
62
45
  */
63
46
  forDevice(provider: string | null): DeviceProviderEntry | undefined;
64
- /** Every loaded `device-provider@1` extension (no internal ones), in registry order: the sweeps. */
47
+ /** Every loaded `device-provider@1` extension (no in-process ones), in registry order. */
65
48
  loaded(): DeviceProviderEntry[];
49
+ /**
50
+ * Every provider of this process: the loaded `device-provider@1` extensions in registry order,
51
+ * then the registered in-process ones (the sweeps go over each provider's own rows).
52
+ */
53
+ all(): DeviceProviderEntry[];
66
54
  private guarded;
67
55
  }
@@ -11,41 +11,52 @@ import { DEVICE_PROVIDER_EXTENSION_POINT } from "@capybara-hq/plugin-sdk";
11
11
  import { Inject, Injectable } from "@nestjs/common";
12
12
  import { ExtensionRegistry } from "../extensions/registry.js";
13
13
  import { guardProviderConnection } from "./unreachable.js";
14
- /** The kernel-internal provider of connected machines (local devices). */
15
- export const LOCAL_PROVIDER = "@local";
16
14
  /**
17
- * A kernel-internal provider name: it starts with `@`, which no extension name can
15
+ * An in-process provider name: it starts with `@`, which no extension name can
18
16
  * (`EXTENSION_NAME_RE`), so it never collides with a loaded `device-provider@1` plugin.
19
17
  */
20
- export const isInternalProvider = (name) => name.startsWith("@");
18
+ const isInProcessName = (name) => name.startsWith("@");
19
+ /**
20
+ * Whether a provider serves its devices over the dial-in channel: it implements
21
+ * `DeviceProvider.dialIn` (an extension may only when it declares `permissions.dialIn`, checked
22
+ * when it provides the point). Its devices are online while a gateway replica holds their machine's
23
+ * connection (`DialInRelay.online`).
24
+ */
25
+ export const servesDialIn = (entry) => entry.impl.dialIn !== undefined;
21
26
  /**
22
27
  * The device providers of this process (a registry: which of them serve an org is
23
- * `DeviceProviderSelection`). A cloud provider is a loaded `device-provider@1` extension,
24
- * read from the {@link ExtensionRegistry} at every call (providers are ready only after the
25
- * extension load, and change with installs). Kernel-internal providers (`@local`) are registered by their
26
- * module on init, in every replica: they are code, not shared state.
28
+ * `DeviceProviderSelection`). A provider is a loaded `device-provider@1` extension, read from the
29
+ * {@link ExtensionRegistry} at every call (providers are ready only after the extension load, and
30
+ * change with installs). An in-process provider (an `@…` name, {@link register}) is code
31
+ * registered in every replica, never shared state; the kernel ships none (tests register stubs).
27
32
  *
28
33
  * Every `device-provider@1` extension is handed out guarded: a network-level failure to reach it is
29
34
  * rethrown as `SandboxProviderUnreachableError` (`./unreachable.ts`), so the tools can tell it
30
- * apart. Internal providers are not: their failures (the device relay's Redis, the machine's
31
- * connection) are the kernel's own and have their own errors.
35
+ * apart. In-process providers are not.
32
36
  */
33
37
  let DeviceProviders = class DeviceProviders {
34
38
  registry;
35
- internal = new Map();
39
+ inProcess = new Map();
36
40
  constructor(registry) {
37
41
  this.registry = registry;
38
42
  }
39
- /** Registers a kernel-internal provider (an `@…` name); a later registration replaces it. */
43
+ /** Registers an in-process provider (an `@…` name); a later registration replaces it. */
40
44
  register(entry) {
41
- if (!isInternalProvider(entry.plugin)) {
42
- throw new Error(`an internal device provider name starts with @: ${entry.plugin}`);
45
+ if (!isInProcessName(entry.plugin)) {
46
+ throw new Error(`an in-process device provider name starts with @: ${entry.plugin}`);
43
47
  }
44
- this.internal.set(entry.plugin, entry);
48
+ this.inProcess.set(entry.plugin, entry);
49
+ }
50
+ /**
51
+ * Whether `plugin` names a registered in-process provider: no catalog item and no org switch,
52
+ * so it serves every org of this process (`DeviceProviderSelection.isOnInOrg`).
53
+ */
54
+ registered(plugin) {
55
+ return this.inProcess.has(plugin);
45
56
  }
46
57
  /**
47
58
  * The provider of a device, from its `devices.provider` (fixed at creation): a registered
48
- * internal one (`@local`), else the loaded `device-provider@1` extension of that plugin name,
59
+ * in-process one, else the loaded `device-provider@1` extension of that plugin name,
49
60
  * on in no org in particular (whether it serves the device's org is
50
61
  * `DeviceProviderSelection.isOnInOrg`). Undefined when neither exists, or for a device without
51
62
  * a provider (`null`). Never another provider in its place.
@@ -53,7 +64,7 @@ let DeviceProviders = class DeviceProviders {
53
64
  forDevice(provider) {
54
65
  if (provider === null)
55
66
  return undefined;
56
- const own = this.internal.get(provider);
67
+ const own = this.inProcess.get(provider);
57
68
  if (own)
58
69
  return own;
59
70
  const e = this.registry
@@ -61,13 +72,20 @@ let DeviceProviders = class DeviceProviders {
61
72
  .find((x) => x.capability === DEVICE_PROVIDER_EXTENSION_POINT && x.plugin === provider);
62
73
  return e ? this.guarded(e) : undefined;
63
74
  }
64
- /** Every loaded `device-provider@1` extension (no internal ones), in registry order: the sweeps. */
75
+ /** Every loaded `device-provider@1` extension (no in-process ones), in registry order. */
65
76
  loaded() {
66
77
  return this.registry
67
78
  .list("device-provider")
68
79
  .filter((x) => x.capability === DEVICE_PROVIDER_EXTENSION_POINT)
69
80
  .map((e) => this.guarded(e));
70
81
  }
82
+ /**
83
+ * Every provider of this process: the loaded `device-provider@1` extensions in registry order,
84
+ * then the registered in-process ones (the sweeps go over each provider's own rows).
85
+ */
86
+ all() {
87
+ return [...this.loaded(), ...this.inProcess.values()];
88
+ }
71
89
  guarded(e) {
72
90
  return {
73
91
  plugin: e.plugin,
@@ -1,16 +1,5 @@
1
- import { SandboxScopeDeniedError } from "@capybara-hq/plugin-sdk";
2
- /**
3
- * A staged request refused because the machine's connection changed before it was sent: the frame
4
- * never reached the machine, so this request ran nothing. Typed as a scope refusal, a pre-effect
5
- * failure the operation path records as definite (`not_sent`), never as an unknown outcome. It
6
- * says nothing about an earlier request of the same operation, so a recovery tells it apart
7
- * ({@link isDeviceRequestNotSent}). The relay throws it (`local-devices/relay.ts` re-exports it).
8
- */
9
- export declare class DeviceRequestNotSentError extends SandboxScopeDeniedError {
10
- readonly notSent: true;
11
- }
12
- /** Recognizes {@link DeviceRequestNotSentError} by its tag. */
13
- export declare const isDeviceRequestNotSent: (e: unknown) => e is DeviceRequestNotSentError;
1
+ /** `DeviceRequestNotSentError` lives in the SDK (the dial-in relay throws it to providers). */
2
+ export { DeviceRequestNotSentError, isDeviceRequestNotSent } from "@capybara-hq/plugin-sdk";
14
3
  /**
15
4
  * A staged request whose machine reconnected as a new run of the program (a different
16
5
  * `programRunId` in its hello) before answering: the results a program keeps for a reconnect live
@@ -1,16 +1,5 @@
1
- import { SandboxScopeDeniedError } from "@capybara-hq/plugin-sdk";
2
- /**
3
- * A staged request refused because the machine's connection changed before it was sent: the frame
4
- * never reached the machine, so this request ran nothing. Typed as a scope refusal, a pre-effect
5
- * failure the operation path records as definite (`not_sent`), never as an unknown outcome. It
6
- * says nothing about an earlier request of the same operation, so a recovery tells it apart
7
- * ({@link isDeviceRequestNotSent}). The relay throws it (`local-devices/relay.ts` re-exports it).
8
- */
9
- export class DeviceRequestNotSentError extends SandboxScopeDeniedError {
10
- notSent = true;
11
- }
12
- /** Recognizes {@link DeviceRequestNotSentError} by its tag. */
13
- export const isDeviceRequestNotSent = (e) => e instanceof SandboxScopeDeniedError && e.notSent === true;
1
+ /** `DeviceRequestNotSentError` lives in the SDK (the dial-in relay throws it to providers). */
2
+ export { DeviceRequestNotSentError, isDeviceRequestNotSent } from "@capybara-hq/plugin-sdk";
14
3
  /**
15
4
  * A staged request whose machine reconnected as a new run of the program (a different
16
5
  * `programRunId` in its hello) before answering: the results a program keeps for a reconnect live