@capybara-hq/kernel 0.1.0-rc.67 → 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 (226) hide show
  1. package/bootstrap.json +9 -1
  2. package/dist/admin/admin.module.js +2 -0
  3. package/dist/admin/health.service.d.ts +11 -4
  4. package/dist/admin/health.service.js +33 -17
  5. package/dist/background/poll.job.js +2 -2
  6. package/dist/background/tasks.service.d.ts +5 -5
  7. package/dist/background/tasks.service.js +9 -8
  8. package/dist/catalog/credential-key.d.ts +10 -0
  9. package/dist/catalog/credential-key.js +28 -0
  10. package/dist/catalog/marketplaces.service.d.ts +1 -1
  11. package/dist/catalog/plugin-upgrade.js +1 -19
  12. package/dist/catalog/requests.service.js +1 -1
  13. package/dist/config/schema.d.ts +1 -1
  14. package/dist/config/schema.js +9 -9
  15. package/dist/conversations/conversations.controller.d.ts +8 -3
  16. package/dist/conversations/conversations.controller.js +8 -3
  17. package/dist/conversations/conversations.service.d.ts +33 -15
  18. package/dist/conversations/conversations.service.js +60 -10
  19. package/dist/conversations/events.d.ts +21 -41
  20. package/dist/conversations/events.js +57 -31
  21. package/dist/conversations/permission-profiles.controller.d.ts +6 -4
  22. package/dist/conversations/permission-profiles.service.d.ts +6 -4
  23. package/dist/ctx/create-ctx.d.ts +20 -1
  24. package/dist/ctx/create-ctx.js +77 -6
  25. package/dist/ctx/device-provider-check.d.ts +6 -2
  26. package/dist/ctx/device-provider-check.js +17 -2
  27. package/dist/db/kernel-schema.d.ts +10 -48
  28. package/dist/devices/access.d.ts +3 -4
  29. package/dist/devices/access.js +3 -4
  30. package/dist/devices/conversation-devices.d.ts +1 -3
  31. package/dist/devices/conversation-devices.js +2 -7
  32. package/dist/devices/device-projects.service.js +24 -29
  33. package/dist/devices/device-provider-selection.d.ts +5 -6
  34. package/dist/devices/device-provider-selection.js +8 -9
  35. package/dist/devices/devices.controller.d.ts +8 -21
  36. package/dist/devices/devices.controller.js +9 -34
  37. package/dist/devices/devices.module.js +6 -4
  38. package/dist/devices/devices.schemas.d.ts +1 -5
  39. package/dist/devices/devices.schemas.js +1 -5
  40. package/dist/devices/devices.service.d.ts +84 -147
  41. package/dist/devices/devices.service.js +134 -229
  42. package/dist/devices/dto.d.ts +5 -32
  43. package/dist/devices/dto.js +5 -37
  44. package/dist/devices/errors.d.ts +6 -5
  45. package/dist/devices/errors.js +7 -8
  46. package/dist/devices/project-rules.d.ts +9 -11
  47. package/dist/devices/project-rules.js +7 -7
  48. package/dist/devices/working-root.d.ts +3 -6
  49. package/dist/devices/working-root.js +3 -8
  50. package/dist/dial-in/access.d.ts +23 -0
  51. package/dist/dial-in/access.js +64 -0
  52. package/dist/{local-devices/device-auth.controller.d.ts → dial-in/auth.controller.d.ts} +12 -6
  53. package/dist/{local-devices/device-auth.controller.js → dial-in/auth.controller.js} +30 -15
  54. package/dist/dial-in/auth.module.d.ts +7 -0
  55. package/dist/dial-in/auth.module.js +27 -0
  56. package/dist/{local-devices/device-auth.d.ts → dial-in/auth.service.d.ts} +42 -13
  57. package/dist/{local-devices/device-auth.js → dial-in/auth.service.js} +83 -32
  58. package/dist/dial-in/dial-in.module.d.ts +9 -0
  59. package/dist/dial-in/dial-in.module.js +37 -0
  60. package/dist/{local-devices → dial-in}/dto.d.ts +17 -2
  61. package/dist/{local-devices → dial-in}/dto.js +14 -2
  62. package/dist/dial-in/envelope.d.ts +95 -0
  63. package/dist/dial-in/envelope.js +89 -0
  64. package/dist/dial-in/gateway.d.ts +214 -0
  65. package/dist/{local-devices/local-device-gateway.js → dial-in/gateway.js} +337 -181
  66. package/dist/dial-in/handlers.d.ts +47 -0
  67. package/dist/dial-in/handlers.js +84 -0
  68. package/dist/dial-in/machines.d.ts +155 -0
  69. package/dist/dial-in/machines.js +383 -0
  70. package/dist/dial-in/relay.d.ts +135 -0
  71. package/dist/dial-in/relay.js +444 -0
  72. package/dist/dial-in/relay.module.d.ts +7 -0
  73. package/dist/dial-in/relay.module.js +24 -0
  74. package/dist/{local-devices/device-routes.d.ts → dial-in/routes.d.ts} +19 -11
  75. package/dist/{local-devices/device-routes.js → dial-in/routes.js} +45 -25
  76. package/dist/{local-devices → dial-in}/targets.d.ts +3 -18
  77. package/dist/{local-devices → dial-in}/targets.js +3 -48
  78. package/dist/errors.d.ts +8 -27
  79. package/dist/errors.js +8 -27
  80. package/dist/extensions/bootstrap.d.ts +5 -0
  81. package/dist/extensions/bootstrap.js +10 -1
  82. package/dist/extensions/extensions.module.js +6 -0
  83. package/dist/extensions/host.d.ts +6 -0
  84. package/dist/extensions/host.js +12 -2
  85. package/dist/extensions/permission-set.d.ts +15 -1
  86. package/dist/extensions/permission-set.js +11 -1
  87. package/dist/extensions/permission-view.d.ts +1 -1
  88. package/dist/extensions/permission-view.js +4 -1
  89. package/dist/http/api.module.js +4 -4
  90. package/dist/http/gateway-routes.d.ts +1 -1
  91. package/dist/http/gateway-routes.js +1 -1
  92. package/dist/http/meta.controller.js +1 -1
  93. package/dist/identity/settings.d.ts +1 -1
  94. package/dist/identity/settings.js +6 -8
  95. package/dist/infra/config/kernel-config.service.js +2 -2
  96. package/dist/infra/outbox/registry.d.ts +0 -14
  97. package/dist/infra/outbox/registry.js +0 -15
  98. package/dist/infra/throttler/named-throttlers.d.ts +1 -1
  99. package/dist/infra/throttler/named-throttlers.js +1 -1
  100. package/dist/kernel.module.js +0 -3
  101. package/dist/orgs/settings.service.js +5 -10
  102. package/dist/overrides.d.ts +0 -4
  103. package/dist/overrides.js +0 -2
  104. package/dist/platform/exclusions.js +5 -5
  105. package/dist/platform/ops/admin.ops.js +1 -1
  106. package/dist/platform/ops/conversations.ops.js +5 -2
  107. package/dist/platform/ops/devices.ops.js +11 -24
  108. package/dist/platform/ops/inbox.ops.js +1 -1
  109. package/dist/roles/gateway-role.d.ts +3 -3
  110. package/dist/roles/gateway-role.js +4 -4
  111. package/dist/roles/gateway.module.d.ts +1 -1
  112. package/dist/roles/gateway.module.js +5 -5
  113. package/dist/runtime/context/device-paths.d.ts +4 -1
  114. package/dist/runtime/context/permission-profile.js +5 -5
  115. package/dist/runtime/maintenance.d.ts +2 -5
  116. package/dist/runtime/maintenance.js +9 -19
  117. package/dist/runtime/project/knowledge.js +4 -5
  118. package/dist/runtime/project/knowledge.service.js +2 -1
  119. package/dist/sandbox/cloud-permission-snapshot.d.ts +1 -1
  120. package/dist/sandbox/cloud-permission-snapshot.js +1 -3
  121. package/dist/sandbox/device-reports.d.ts +74 -14
  122. package/dist/sandbox/device-reports.js +190 -33
  123. package/dist/sandbox/device-reports.module.d.ts +7 -0
  124. package/dist/sandbox/device-reports.module.js +25 -0
  125. package/dist/sandbox/device-sandbox.d.ts +39 -41
  126. package/dist/sandbox/device-sandbox.js +44 -83
  127. package/dist/sandbox/kernel-read.d.ts +2 -2
  128. package/dist/sandbox/legacy-retirement.d.ts +11 -59
  129. package/dist/sandbox/legacy-retirement.js +34 -186
  130. package/dist/sandbox/legacy-retirement.module.d.ts +2 -2
  131. package/dist/sandbox/legacy-retirement.module.js +2 -2
  132. package/dist/sandbox/managed-device-context.d.ts +16 -4
  133. package/dist/sandbox/managed-device-context.js +25 -8
  134. package/dist/sandbox/managed-device-context.module.js +4 -0
  135. package/dist/sandbox/modes.d.ts +3 -12
  136. package/dist/sandbox/modes.js +2 -14
  137. package/dist/{local-devices → sandbox}/network-grants.d.ts +2 -21
  138. package/dist/{local-devices → sandbox}/network-grants.js +4 -49
  139. package/dist/sandbox/operation-authority-types.d.ts +2 -1
  140. package/dist/sandbox/operation-authority.d.ts +6 -20
  141. package/dist/sandbox/operation-authority.js +43 -104
  142. package/dist/sandbox/operation-authority.module.js +1 -1
  143. package/dist/sandbox/operation-intents.d.ts +4 -1
  144. package/dist/sandbox/operation-intents.js +11 -2
  145. package/dist/sandbox/operation-reconcile.job.js +2 -4
  146. package/dist/sandbox/permission-availability.d.ts +6 -7
  147. package/dist/sandbox/permission-availability.js +7 -10
  148. package/dist/sandbox/permission-call-grants.d.ts +2 -1
  149. package/dist/sandbox/permission-call-grants.js +1 -7
  150. package/dist/sandbox/permission-call-types.d.ts +4 -4
  151. package/dist/sandbox/permission-ceiling-writes.d.ts +7 -23
  152. package/dist/sandbox/permission-ceiling-writes.js +8 -35
  153. package/dist/sandbox/permission-config-publication.d.ts +9 -1
  154. package/dist/sandbox/permission-config-publication.js +29 -21
  155. package/dist/sandbox/permission-context-types.d.ts +17 -10
  156. package/dist/sandbox/permission-facts.d.ts +98 -95
  157. package/dist/sandbox/permission-facts.js +160 -314
  158. package/dist/sandbox/permission-instance-writes.d.ts +23 -37
  159. package/dist/sandbox/permission-instance-writes.js +44 -121
  160. package/dist/sandbox/permission-runtime-reports.d.ts +38 -30
  161. package/dist/sandbox/permission-runtime-reports.js +105 -122
  162. package/dist/sandbox/providers.d.ts +26 -38
  163. package/dist/sandbox/providers.js +36 -18
  164. package/dist/sandbox/request-not-sent.d.ts +2 -13
  165. package/dist/sandbox/request-not-sent.js +2 -13
  166. package/dist/sandbox/review/permission-candidate.d.ts +13 -8
  167. package/dist/sandbox/review/permission-candidate.js +38 -48
  168. package/dist/sandbox/sandbox.module.js +7 -3
  169. package/dist/sandbox/sandbox.service.d.ts +49 -59
  170. package/dist/sandbox/sandbox.service.js +139 -259
  171. package/dist/schedules/fire.d.ts +15 -6
  172. package/dist/schedules/fire.js +27 -13
  173. package/dist/schedules/grant-rules.js +1 -1
  174. package/dist/schedules/scheduled-tasks.service.d.ts +0 -7
  175. package/dist/schedules/scheduled-tasks.service.js +1 -4
  176. package/dist/schedules/schedules.module.js +5 -3
  177. package/dist/skills/invoke.d.ts +1 -1
  178. package/dist/skills/invoke.js +2 -3
  179. package/dist/tools/approvals.service.d.ts +8 -5
  180. package/dist/tools/approvals.service.js +5 -5
  181. package/dist/tools/builtins/sandbox/bash.js +1 -2
  182. package/dist/tools/builtins/sandbox/common.d.ts +15 -9
  183. package/dist/tools/builtins/sandbox/common.js +37 -38
  184. package/dist/tools/connectors.controller.d.ts +2 -0
  185. package/dist/tools/connectors.schemas.d.ts +2 -0
  186. package/dist/tools/credentials.d.ts +2 -0
  187. package/dist/tools/network-asks.d.ts +19 -22
  188. package/dist/tools/network-asks.js +15 -21
  189. package/dist/tools/oauth/authorization-params.d.ts +7 -0
  190. package/dist/tools/oauth/authorization-params.js +19 -0
  191. package/dist/tools/oauth/flow.js +7 -0
  192. package/dist/tools/oauth/tokens.js +2 -1
  193. package/dist/tools/pipeline.d.ts +6 -7
  194. package/dist/tools/pipeline.js +17 -21
  195. package/dist/tools/tools.module.js +3 -7
  196. package/package.json +3 -4
  197. package/src/migrations/0177_dial_in_contract.sql +32 -0
  198. package/dist/local-devices/device-auth.module.d.ts +0 -7
  199. package/dist/local-devices/device-auth.module.js +0 -25
  200. package/dist/local-devices/device-policy.service.d.ts +0 -213
  201. package/dist/local-devices/device-policy.service.js +0 -605
  202. package/dist/local-devices/instance-bindings.d.ts +0 -82
  203. package/dist/local-devices/instance-bindings.js +0 -307
  204. package/dist/local-devices/local-device-gateway.d.ts +0 -153
  205. package/dist/local-devices/local-device-provider.d.ts +0 -122
  206. package/dist/local-devices/local-device-provider.js +0 -326
  207. package/dist/local-devices/local-devices.module.d.ts +0 -31
  208. package/dist/local-devices/local-devices.module.js +0 -100
  209. package/dist/local-devices/machines.d.ts +0 -211
  210. package/dist/local-devices/machines.js +0 -766
  211. package/dist/local-devices/owner-report.d.ts +0 -81
  212. package/dist/local-devices/owner-report.js +0 -150
  213. package/dist/local-devices/owner-reports.d.ts +0 -48
  214. package/dist/local-devices/owner-reports.js +0 -208
  215. package/dist/local-devices/policy-op-jobs.d.ts +0 -15
  216. package/dist/local-devices/policy-op-jobs.js +0 -12
  217. package/dist/local-devices/policy-summary.d.ts +0 -102
  218. package/dist/local-devices/policy-summary.js +0 -155
  219. package/dist/local-devices/relay.d.ts +0 -166
  220. package/dist/local-devices/relay.js +0 -559
  221. package/dist/tools/device-op-resolver.d.ts +0 -34
  222. package/dist/tools/device-op-resolver.js +0 -120
  223. /package/dist/{local-devices → dial-in}/keys.d.ts +0 -0
  224. /package/dist/{local-devices → dial-in}/keys.js +0 -0
  225. /package/dist/{local-devices → sandbox}/network-grants.module.d.ts +0 -0
  226. /package/dist/{local-devices → sandbox}/network-grants.module.js +0 -0
@@ -7,15 +7,18 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __param = (this && this.__param) || function (paramIndex, decorator) {
8
8
  return function (target, key) { decorator(target, key, paramIndex); }
9
9
  };
10
- import { parseDeviceReport } from "@capybara-hq/plugin-sdk";
10
+ import { parseDeviceReport, } from "@capybara-hq/plugin-sdk";
11
11
  import { Inject, Injectable, Optional } from "@nestjs/common";
12
12
  import { sql } from "kysely";
13
13
  import { KernelDb } from "../infra/database/kernel-db.js";
14
- import { TransactionHost } from "../infra/database/transaction-host.js";
14
+ import { runOutsideTransaction, TransactionHost } from "../infra/database/transaction-host.js";
15
15
  import { Transactional } from "../infra/database/transactional.decorator.js";
16
16
  import { LockService } from "../infra/lock/lock.service.js";
17
17
  import { KERNEL_LOGGER } from "../infra/tokens.js";
18
18
  import { untilAborted } from "../until-aborted.js";
19
+ import { PermissionCeilingWrites, requiresFullExecution } from "./permission-ceiling-writes.js";
20
+ import { supportOf } from "./permission-runtime-reports.js";
21
+ import { DeviceProviders } from "./providers.js";
19
22
  /** How long a provider's `report` may take before the refresh gives up (keeps the stored one). */
20
23
  export const DEVICE_REPORT_TIMEOUT_MS = 10_000;
21
24
  /** Overrides {@link DEVICE_REPORT_TIMEOUT_MS} (ms; tests only). */
@@ -24,46 +27,96 @@ export const DEVICE_REPORT_TIMEOUT = Symbol("DEVICE_REPORT_TIMEOUT");
24
27
  const FAILURE_LOG_WINDOW_MS = 60 * 60_000;
25
28
  const failureLogKey = (deviceId) => `capk:device-report-failure:${deviceId}`;
26
29
  /** The revision of a stored report, or null when none (or not a readable one) is stored. */
27
- function storedRevision(stored) {
30
+ export function storedRevision(stored) {
28
31
  if (typeof stored !== "object" || stored === null)
29
32
  return null;
30
33
  const revision = stored.revision;
31
34
  return typeof revision === "string" ? revision : null;
32
35
  }
36
+ /** The permission support stored with a report (its `support` key), or null when none passes. */
37
+ export function storedSupport(stored) {
38
+ if (typeof stored !== "object" || stored === null)
39
+ return null;
40
+ return supportOf(stored.support);
41
+ }
42
+ /**
43
+ * A stored report (`devices.report`) checked again ({@link parseDeviceReport}) without its
44
+ * `support` key; null when none is stored or it no longer passes.
45
+ */
46
+ export function storedReport(stored) {
47
+ if (typeof stored !== "object" || stored === null)
48
+ return null;
49
+ const { support: _support, ...report } = stored;
50
+ try {
51
+ return parseDeviceReport(report);
52
+ }
53
+ catch {
54
+ return null;
55
+ }
56
+ }
57
+ /**
58
+ * The crossings a device's provider can enforce now, from its permission support: none when the
59
+ * support is unknown (not implemented, no instance, a failed read) or names why it is unavailable.
60
+ */
61
+ function crossingsOf(support) {
62
+ const usable = support !== null && support.unavailableReason === undefined;
63
+ return {
64
+ outsideSandbox: usable && support.outsideSandbox,
65
+ perOperationHosts: usable && support.network.perOperationHosts,
66
+ };
67
+ }
33
68
  /**
34
- * The stored device report (`devices.report`, `report_at`; device-provider spec §6.1): what a
35
- * device's provider last told about it (`device-provider@1` `report()`), shown to people and never
36
- * used for enforcement. {@link refresh} asks the provider and stores the answer; request paths
37
- * {@link read} the stored copy from PG and never call the provider.
69
+ * The stored device report (`devices.report`, `report_at`; device-provider spec §6.1, §6.7): what a
70
+ * device's provider last told about it (`device-provider@1` `report()`, with the instance's
71
+ * `permissionSupport()` read at the last {@link changed} that had an instance, kept under the key
72
+ * `support`). The report's text is shown to people; its revision and the stored support drive
73
+ * the device authority epoch below (what an operation may do is still decided by the authority
74
+ * and the provider's own enforcement, never read from here). {@link refresh} (the kernel, on a create or resume) and {@link changed} (the
75
+ * provider, through `ctx.devices.reportChanged`) ask the provider and store the answer; request
76
+ * paths {@link read} the stored copy from PG and never call the provider.
77
+ *
78
+ * The report's revision drives the provider-neutral device authority epoch (§6.7): a stored
79
+ * revision replaced by another one publishes a device ceiling change in the same transaction
80
+ * (`PermissionCeilingWrites.publishLocked`), so approvals, grants and pending admissions bound to
81
+ * the old epoch are stale. Running work is revoked only for the crossings the new support cannot
82
+ * enforce (outside the sandbox, per-call hosts); Full consent is reset when the outside-sandbox
83
+ * crossing changes either way or per-call hosts become possible. A first report replaces no
84
+ * revision and publishes nothing. No in-process state: every comparison reads PG under the device
85
+ * authority writer lock and the row lock.
38
86
  */
39
87
  let DeviceReports = class DeviceReports {
40
88
  db;
41
89
  txHost;
42
90
  locks;
43
91
  log;
92
+ ceilings;
93
+ providers;
44
94
  timeoutMs;
45
- constructor(db, txHost, locks, log, timeoutMs = DEVICE_REPORT_TIMEOUT_MS) {
95
+ constructor(db, txHost, locks, log, ceilings, providers, timeoutMs = DEVICE_REPORT_TIMEOUT_MS) {
46
96
  this.db = db;
47
97
  this.txHost = txHost;
48
98
  this.locks = locks;
49
99
  this.log = log;
100
+ this.ceilings = ceilings;
101
+ this.providers = providers;
50
102
  this.timeoutMs = timeoutMs;
51
103
  }
52
104
  /**
53
105
  * Asks `entry` for the report of `ref`'s device (given up after {@link DEVICE_REPORT_TIMEOUT_MS}
54
- * or on `signal`, even when the provider ignores its signal), checks it ({@link parseDeviceReport}) and stores it with `report_at`, only
55
- * while the device is live and still bound to `entry` (`devices.provider = entry.plugin`).
106
+ * or on `signal`, even when the provider ignores its signal), checks it ({@link parseDeviceReport})
107
+ * and stores it with `report_at`, keeping the permission support stored before (the lifecycle
108
+ * reads the instance's support itself; a refresh adds no provider probe),
109
+ * only while the device is live and still bound to `entry` (`devices.provider = entry.plugin`);
110
+ * a replaced revision publishes the device authority change (see the class).
56
111
  * `changed`: the stored revision differs from the one before (true for a first report). Never
57
- * throws: a failing or invalid answer leaves the stored report as it was and is logged at most
112
+ * throws: a failing or invalid report leaves the stored one as it was and is logged at most
58
113
  * once per device per hour; an abort of `signal` is not logged. Call it outside any transaction
59
- * (the provider call must not hold one open).
114
+ * (the provider calls must not hold one open).
60
115
  */
61
116
  async refresh(entry, ref, signal) {
62
- let report;
117
+ let answer;
63
118
  try {
64
- const bounded = AbortSignal.any([signal, AbortSignal.timeout(this.timeoutMs)]);
65
- // raced, not only signalled: a provider that ignores the signal never holds the lease past it
66
- report = parseDeviceReport(await untilAborted(entry.impl.report(ref, bounded), bounded));
119
+ answer = await this.ask(entry, ref, signal, false);
67
120
  }
68
121
  catch (err) {
69
122
  if (!signal.aborted)
@@ -71,13 +124,51 @@ let DeviceReports = class DeviceReports {
71
124
  return { changed: false };
72
125
  }
73
126
  try {
74
- return { changed: await this.store(entry.plugin, ref, report) };
127
+ return { changed: (await this.store(entry.plugin, ref, answer)).changed };
75
128
  }
76
129
  catch (err) {
77
130
  await this.noteFailure(entry, ref.deviceId, err);
78
131
  return { changed: false };
79
132
  }
80
133
  }
134
+ /**
135
+ * `ctx.devices.reportChanged(deviceId)` of the extension `plugin`: `not-served` unless the
136
+ * device is live and bound to `plugin` (the caller refuses it); otherwise reads the device's
137
+ * report and permission support from `plugin` (its current instance's handle, if any) outside
138
+ * any transaction and stores them as {@link refresh} does. Throws when the report cannot be read
139
+ * or does not pass, so the provider may tell again; a failed support read stores no support
140
+ * (no crossing enforceable: revocation fails closed).
141
+ */
142
+ async changed(plugin, deviceId, signal = new AbortController().signal) {
143
+ return runOutsideTransaction(async () => {
144
+ const device = await this.db
145
+ .selectFrom("devices as d")
146
+ .innerJoin("orgs as o", "o.id", "d.org_id")
147
+ .select(["d.org_id", "d.provider"])
148
+ .where("d.id", "=", deviceId)
149
+ .where("d.deleted_at", "is", null)
150
+ .where("o.deleted_at", "is", null)
151
+ .executeTakeFirst();
152
+ if (!device || device.provider !== plugin)
153
+ return "not-served";
154
+ const entry = this.providers.forDevice(plugin);
155
+ if (!entry)
156
+ throw new Error(`Device provider ${plugin} is not loaded on this replica`);
157
+ const instance = await this.db
158
+ .selectFrom("device_instances")
159
+ .select("handle")
160
+ .where("device_id", "=", deviceId)
161
+ .where("provider", "=", plugin)
162
+ .executeTakeFirst();
163
+ const ref = {
164
+ deviceId,
165
+ orgId: device.org_id,
166
+ handle: (instance?.handle ?? null),
167
+ };
168
+ const answer = await this.ask(entry, ref, signal, true);
169
+ return (await this.store(plugin, ref, answer)).published ? "bumped" : "same";
170
+ });
171
+ }
81
172
  /**
82
173
  * The stored report of a live device of `orgId`, checked again ({@link parseDeviceReport});
83
174
  * null when none is stored, the stored one no longer passes, or there is no such device.
@@ -91,21 +182,63 @@ let DeviceReports = class DeviceReports {
91
182
  .where("org_id", "=", orgId)
92
183
  .where("deleted_at", "is", null)
93
184
  .executeTakeFirst();
94
- if (row?.report == null)
95
- return null;
96
- try {
97
- return parseDeviceReport(row.report);
98
- }
99
- catch {
185
+ return storedReport(row?.report);
186
+ }
187
+ /**
188
+ * The stored revision of a live device and the permission support stored with it (null when
189
+ * none was read); null when no report is stored. Reads PG only.
190
+ */
191
+ async current(deviceId) {
192
+ const row = await this.db
193
+ .selectFrom("devices")
194
+ .select("report")
195
+ .where("id", "=", deviceId)
196
+ .where("deleted_at", "is", null)
197
+ .executeTakeFirst();
198
+ const revision = storedRevision(row?.report);
199
+ return revision === null ? null : { revision, support: storedSupport(row?.report) };
200
+ }
201
+ /**
202
+ * The provider's report (checked; throws when it fails or does not pass) and, with
203
+ * `withSupport`, the instance's permission support (null when the provider has none or the read
204
+ * fails or does not pass; absent without `withSupport` or without an instance: the stored one
205
+ * is kept), each bounded and raced so a provider ignoring the signal holds nothing.
206
+ */
207
+ async ask(entry, ref, signal, withSupport) {
208
+ const bounded = () => AbortSignal.any([signal, AbortSignal.timeout(this.timeoutMs)]);
209
+ const s1 = bounded();
210
+ // raced, not only signalled: a provider that ignores the signal never holds a lease past it
211
+ const report = parseDeviceReport(await untilAborted(entry.impl.report(ref, s1), s1));
212
+ if (!withSupport)
213
+ return { report };
214
+ const { handle } = ref;
215
+ const read = entry.impl.permissionSupport?.bind(entry.impl);
216
+ // no instance to ask: what the last instance-backed read said still stands
217
+ if (handle === null)
218
+ return { report };
219
+ if (!read)
220
+ return { report, support: null };
221
+ const s2 = bounded();
222
+ const support = await untilAborted(read(handle, s2), s2).then(supportOf, (err) => {
223
+ if (!signal.aborted)
224
+ this.log.warn("cannot read the device's permission support with its report", {
225
+ deviceId: ref.deviceId,
226
+ provider: entry.plugin,
227
+ err,
228
+ });
100
229
  return null;
101
- }
230
+ });
231
+ return { report, support };
102
232
  }
103
233
  /**
104
- * Writes `report` on the device if it is live and bound to `plugin`; whether its revision
105
- * differs from the stored one. The row is locked first, so two refreshes of one device compare
106
- * against each other's writes, never against the same old value.
234
+ * Writes the answer on the device if it is live and bound to `plugin`, under the row lock and
235
+ * then the device authority writer lock, so two stores of one device compare against each
236
+ * other's writes, never against the same old value.
237
+ * `changed`: the revision differs from the stored one; `published`: a stored revision was
238
+ * replaced, so the device authority change was published in this transaction.
107
239
  */
108
- async store(plugin, ref, report) {
240
+ async store(plugin, ref, answer) {
241
+ const subject = { orgId: ref.orgId, deviceId: ref.deviceId };
109
242
  const row = await this.db
110
243
  .selectFrom("devices")
111
244
  .select("report")
@@ -117,13 +250,35 @@ let DeviceReports = class DeviceReports {
117
250
  .executeTakeFirst();
118
251
  // deleted, or bound to another provider than the one that answered: nothing is written
119
252
  if (!row)
120
- return false;
253
+ return { changed: false, published: false };
254
+ // the device row first, then the device authority writer: the order of the device writers
255
+ // (`DevicesService` removal and network)
256
+ await this.ceilings.lock(subject, "device");
257
+ const { report } = answer;
258
+ const was = crossingsOf(storedSupport(row.report));
259
+ const support = answer.support === undefined ? storedSupport(row.report) : answer.support;
121
260
  await this.db
122
261
  .updateTable("devices")
123
- .set({ report: JSON.stringify(report), report_at: sql `now()` })
262
+ .set({ report: JSON.stringify({ ...report, support }), report_at: sql `now()` })
124
263
  .where("id", "=", ref.deviceId)
125
264
  .execute();
126
- return storedRevision(row.report) !== report.revision;
265
+ const before = storedRevision(row.report);
266
+ const changed = before !== report.revision;
267
+ if (!changed || before === null)
268
+ return { changed, published: false };
269
+ const now = crossingsOf(support);
270
+ await this.ceilings.publishLocked(subject, "device",
271
+ // running work keeps only the crossings the new support can still enforce
272
+ (scope, intent) => (!now.outsideSandbox && requiresFullExecution(scope)) ||
273
+ (!now.perOperationHosts && intent.granted_hosts.length > 0),
274
+ // Full access approves crossings by itself: a changed outside-sandbox crossing, or per-call
275
+ // hosts becoming possible, needs the person's consent again. This reset is a backstop: the
276
+ // stored support usually already follows the runtime (`recordSupport` stores it), and the
277
+ // guard is the consent check itself (`consentHolds` / `reachWithin` against the reach the
278
+ // ceilings' support gives), which fails on any widening whether or not this flag is set
279
+ was.outsideSandbox !== now.outsideSandbox ||
280
+ (!was.perOperationHosts && now.perOperationHosts));
281
+ return { changed, published: true };
127
282
  }
128
283
  /** Logs a refresh failure, at most once per device per hour (a Redis key with a TTL). */
129
284
  async noteFailure(entry, deviceId, err) {
@@ -151,7 +306,9 @@ DeviceReports = __decorate([
151
306
  __param(1, Inject(TransactionHost)),
152
307
  __param(2, Inject(LockService)),
153
308
  __param(3, Inject(KERNEL_LOGGER)),
154
- __param(4, Optional()),
155
- __param(4, Inject(DEVICE_REPORT_TIMEOUT))
309
+ __param(4, Inject(PermissionCeilingWrites)),
310
+ __param(5, Inject(DeviceProviders)),
311
+ __param(6, Optional()),
312
+ __param(6, Inject(DEVICE_REPORT_TIMEOUT))
156
313
  ], DeviceReports);
157
314
  export { DeviceReports };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * {@link DeviceReports}: the stored device reports and the device authority epoch their revisions
3
+ * drive. Below the extension host (registry, ceiling writes and infrastructure only), so the host
4
+ * builds `ctx.devices` from it and the sandbox and device domains share the one instance.
5
+ */
6
+ export declare class DeviceReportsModule {
7
+ }
@@ -0,0 +1,25 @@
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
+ import { Module } from "@nestjs/common";
8
+ import { DeviceReports } from "./device-reports.js";
9
+ import { PermissionCeilingWritesModule } from "./permission-authority.module.js";
10
+ import { SandboxProviderRegistryModule } from "./providers.module.js";
11
+ /**
12
+ * {@link DeviceReports}: the stored device reports and the device authority epoch their revisions
13
+ * drive. Below the extension host (registry, ceiling writes and infrastructure only), so the host
14
+ * builds `ctx.devices` from it and the sandbox and device domains share the one instance.
15
+ */
16
+ let DeviceReportsModule = class DeviceReportsModule {
17
+ };
18
+ DeviceReportsModule = __decorate([
19
+ Module({
20
+ imports: [SandboxProviderRegistryModule, PermissionCeilingWritesModule],
21
+ providers: [DeviceReports],
22
+ exports: [DeviceReports],
23
+ })
24
+ ], DeviceReportsModule);
25
+ export { DeviceReportsModule };
@@ -48,25 +48,21 @@ export interface DeviceSandboxOrgLayer {
48
48
  };
49
49
  }
50
50
  /**
51
- * The device layer: a local device's owner ceiling (on-disk, reported over the protocol), or a cloud
52
- * device's stored network choice (`devices.network_mode`, `devices.network_hosts`).
51
+ * The device layer: the device's stored network choice (`devices.network_mode`,
52
+ * `devices.network_hosts`), the same for every provider (device-provider spec §6.3).
53
53
  */
54
- export type DeviceSandboxDeviceLayer = {
55
- location: "local";
56
- outsideSandbox: boolean;
57
- network: DeviceNetworkMode;
58
- allowedDomains: readonly string[];
59
- restrictedProjectReach: "workspace" | "os-user";
60
- } | {
61
- location: "cloud";
54
+ export interface DeviceSandboxDeviceLayer {
62
55
  /**
63
56
  * Null follows the org; `none` is a closed network (a snapshot that ran on an empty or
64
57
  * unreadable allowlist). An `allowlist` with no hosts of its own follows its org's hosts.
65
58
  */
66
59
  networkMode: DeviceNetworkMode | "none" | null;
67
60
  networkHosts: readonly string[];
68
- };
69
- /** The provider layer: what the runtime reports (support v2) and, for a cloud device, its outer network. */
61
+ }
62
+ /**
63
+ * The provider layer: what the provider's permission support says it can enforce (support v2):
64
+ * its boundary, the networks it enforces and whether a call may run outside its sandbox now.
65
+ */
70
66
  export interface DeviceSandboxProviderLayer {
71
67
  boundary: SandboxBoundary;
72
68
  network: {
@@ -87,7 +83,11 @@ export interface DeviceSandbox {
87
83
  hosts: readonly string[];
88
84
  /** Whether a call may be granted escalation outside the sandbox. */
89
85
  outsideSandbox: boolean;
90
- restrictedProjectReach: "workspace" | "os-user";
86
+ /**
87
+ * Whether a call may be granted a host per call: only on an operation boundary whose provider
88
+ * reports `perOperationHosts` (each operation carries its own network).
89
+ */
90
+ perCallHosts: boolean;
91
91
  /** The layer that set each ceiling, or null when nothing narrowed it. */
92
92
  layer: {
93
93
  network: DeviceSandboxLayer | null;
@@ -104,37 +104,40 @@ export declare function orgHostsOf(platform: Pick<DeviceSandboxPlatformLayer, "e
104
104
  /**
105
105
  * Computes the effective device sandbox from its four layers. Pure; no I/O.
106
106
  *
107
- * - `outsideSandbox`: only when the platform, the org, the device owner and the provider all allow
108
- * it; a cloud device and an instance-boundary provider never do.
107
+ * - `outsideSandbox`: only when the platform, the org and the provider's support all allow it; an
108
+ * instance-boundary provider never does.
109
109
  * - The network ceiling is the narrower of the platform's and the org's (and what the provider can
110
110
  * enforce); the kill switch closes it (`none`).
111
- * - The mode is the device's choice (a local owner's network, a cloud device's `network_mode`),
112
- * else the org default, else the platform default, clamped to the ceiling.
111
+ * - The mode is the device's choice (`network_mode`), else the org default, else the platform
112
+ * default, clamped to the ceiling.
113
113
  * - Allowlist hosts are the org's effective hosts, narrowed by the device's own list: a device list
114
- * never adds a host. A cloud device's empty list adds no narrowing; a local owner's allowlist is
115
- * the machine's ceiling, so an empty one allows no host (as the device enforces it).
114
+ * never adds a host, and an empty one adds no narrowing.
115
+ * - `perCallHosts`: only an operation-boundary provider that reports `perOperationHosts`.
116
116
  */
117
117
  export declare function effectiveDeviceSandbox(platform: DeviceSandboxPlatformLayer, org: DeviceSandboxOrgLayer, device: DeviceSandboxDeviceLayer, provider: DeviceSandboxProviderLayer): DeviceSandbox;
118
118
  /**
119
- * The provider layer of a device: its recorded runtime report (support v2) when one is current,
120
- * else what the kernel assumes before the device starts (the device and its provider still
121
- * enforce, and admission refuses until a report is recorded): a local device runs per operation
122
- * and may run outside; a cloud device's instance is its sandbox.
119
+ * The provider layer of a device from its provider's permission support (support v2): the current
120
+ * runtime report, else the support stored with the device's report, the one layer the crossing
121
+ * ceilings and the Full access consent reach both read. With no support known yet, the kernel
122
+ * assumes no crossing for every provider: the networks open to the device's own choice (the
123
+ * provider still enforces its own, and admission refuses until a report is recorded), no per-call
124
+ * host (as an instance boundary) and nothing outside the sandbox. A support that later allows a
125
+ * crossing therefore widens the reach, so a Full access consent given before it no longer holds
126
+ * and the person confirms again with that crossing shown.
123
127
  */
124
- export declare function providerLayerOf(report: SandboxPermissionSupport | null, location: "local" | "cloud"): DeviceSandboxProviderLayer;
128
+ export declare function providerLayerOf(support: SandboxPermissionSupport | null): DeviceSandboxProviderLayer;
125
129
  /**
126
130
  * The ceiling of a per-call host grant (approval-only profiles design, "Per-call host grant"):
127
131
  * `any` (bounded by the kernel only, minus the org's exclusions) or exactly the `hosts` a grant may
128
132
  * name.
129
- * - Nothing on an instance-boundary device (its provider's network covers the whole instance) or
130
- * on a closed network.
133
+ * - Nothing on an instance-boundary device (its provider's network covers the whole instance), on
134
+ * an operation-boundary device whose provider does not report `perOperationHosts` (it cannot give
135
+ * one operation a host the others lack), or on a closed network.
131
136
  * - An allowlist network ceiling (platform or org) is a hard ceiling: only the org's effective
132
137
  * hosts.
133
138
  * - Under an open ceiling, `orgHosts: within` keeps grants within the org's effective hosts (the
134
139
  * platform list minus exclusions, or only the extras the platform list covers under `base:
135
140
  * none`); `extend` allows any host the org does not exclude.
136
- * - A local owner's allowlist is the machine's ceiling: intersected with the owner's list, and
137
- * under an open ceiling still minus the org's exclusions (`excluded`).
138
141
  *
139
142
  * An excluded entry is a pattern like an allowlist entry (`*.example.com` excludes every
140
143
  * subdomain, never the apex): a host is grantable only when no excluded entry covers it.
@@ -148,36 +151,31 @@ export type HostGrantCeiling = {
148
151
  excluded?: readonly string[];
149
152
  };
150
153
  export declare function hostGrantCeiling(box: Pick<DeviceSandbox, "boundary" | "networkMode" | "networkCeiling"> & {
154
+ /** Absent for an org- or platform-level ceiling with no device: the boundary decides. */
155
+ perCallHosts?: boolean;
151
156
  layer?: Pick<DeviceSandbox["layer"], "network">;
152
- }, platform: Pick<DeviceSandboxPlatformLayer, "egress" | "allowHosts" | "orgHosts">, org: DeviceSandboxOrgLayer, owner: {
153
- network: DeviceNetworkMode;
154
- allowedDomains: readonly string[];
155
- } | null): HostGrantCeiling;
157
+ }, platform: Pick<DeviceSandboxPlatformLayer, "egress" | "allowHosts" | "orgHosts">, org: DeviceSandboxOrgLayer): HostGrantCeiling;
156
158
  /** Whether a per-call grant may name `host` under `ceiling`. */
157
159
  export declare function hostGrantable(ceiling: HostGrantCeiling, host: string): boolean;
158
160
  /** A canonical form of a ceiling (sorted, deduplicated, lowercased) for the consent fingerprint. */
159
161
  export declare function canonicalHostCeiling(ceiling: HostGrantCeiling): HostGrantCeiling;
160
162
  /**
161
163
  * Why a per-call grant for `host` is unavailable on this device, naming who can add it, or null
162
- * when the grant ceiling covers it ({@link hostGrantCeiling}). The org's and platform's layers are
163
- * named first: the machine's owner can widen only within them.
164
+ * when the grant ceiling covers it ({@link hostGrantCeiling}).
164
165
  */
165
166
  export declare function hostGrantRefusal(host: string, box: Pick<DeviceSandbox, "boundary" | "networkMode" | "networkCeiling"> & {
167
+ /** Absent for an org- or platform-level ceiling with no device: the boundary decides. */
168
+ perCallHosts?: boolean;
166
169
  layer?: Pick<DeviceSandbox["layer"], "network">;
167
- }, platform: Pick<DeviceSandboxPlatformLayer, "egress" | "allowHosts" | "orgHosts">, org: DeviceSandboxOrgLayer, owner: {
168
- network: DeviceNetworkMode;
169
- allowedDomains: readonly string[];
170
- } | null): string | null;
170
+ }, platform: Pick<DeviceSandboxPlatformLayer, "egress" | "allowHosts" | "orgHosts">, org: DeviceSandboxOrgLayer): string | null;
171
171
  /**
172
172
  * The crossing ceilings a Full access consent covers (approval-only profiles design, "Consent
173
- * fingerprint"): running outside, the network ceiling and mode, the project reach and the per-call
174
- * host ceiling.
173
+ * fingerprint"): running outside, the network ceiling and mode and the per-call host ceiling.
175
174
  */
176
175
  export interface ConsentReach {
177
176
  outsideSandbox: boolean;
178
177
  networkCeiling: DeviceNetworkMode | "none";
179
178
  networkMode: DeviceNetworkMode | "none";
180
- restrictedProjectReach: "workspace" | "os-user";
181
179
  hostCeiling: HostGrantCeiling;
182
180
  }
183
181
  /**