@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
@@ -0,0 +1,89 @@
1
+ /**
2
+ * The marker of a result the gateway stored itself for a request it never sent (the request was
3
+ * fenced to another connection, or did not fit): never a machine's field, since the strict
4
+ * envelope parse refuses unknown keys.
5
+ */
6
+ export const GATEWAY_NOT_SENT = "notSent";
7
+ const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
8
+ /**
9
+ * What the holder sends for one queued item (`raw`, as popped from the list) on its connection:
10
+ * the kernel frame without the kernel-internal fields, or why it does not send it. A request goes
11
+ * out only on the connection it is fenced to; a cancel always; a stop only to the machine it names
12
+ * (when it names one).
13
+ */
14
+ export function wireFrameOf(raw, conn) {
15
+ let item;
16
+ try {
17
+ item = JSON.parse(raw);
18
+ }
19
+ catch {
20
+ return { drop: "a queued item is not JSON" };
21
+ }
22
+ if (!isRecord(item))
23
+ return { drop: "a queued item is not an object" };
24
+ if (item.type === "req") {
25
+ const { id, deadlineMs, body } = item;
26
+ if (typeof id !== "string" || typeof deadlineMs !== "number" || !(deadlineMs > 0))
27
+ return { drop: "a queued request is malformed" };
28
+ if (item.connectionId !== conn.connectionId || item.maxFrameBytes !== conn.maxFrameBytes)
29
+ return {
30
+ refuse: "The machine's connection changed before the request was sent; retry it",
31
+ id,
32
+ };
33
+ const send = { type: "req", id, deadlineMs, body };
34
+ return { send, text: JSON.stringify(send) };
35
+ }
36
+ if (item.type === "cancel") {
37
+ if (typeof item.id !== "string")
38
+ return { drop: "a queued cancel is malformed" };
39
+ const send = { type: "cancel", id: item.id };
40
+ return { send, text: JSON.stringify(send) };
41
+ }
42
+ if (item.type === "stop") {
43
+ if (item.reason !== "drain" && item.reason !== "stop")
44
+ return { drop: "a queued stop is malformed" };
45
+ // fenced to the machine it was queued for: a newer machine of the device never gets it
46
+ if (item.machineId !== undefined && item.machineId !== conn.machineId)
47
+ return { drop: "a stop queued for another machine of the device" };
48
+ const send = { type: "stop", reason: item.reason };
49
+ return { send, text: JSON.stringify(send) };
50
+ }
51
+ return { drop: "a queued item has an unknown type" };
52
+ }
53
+ /**
54
+ * The result the holder stores for a machine's `res` (`body` given) or for its own refusal of a
55
+ * request it never sent (`notSent` given, no body), tagged with the connection it belongs to.
56
+ */
57
+ export function storedResult(res, conn, notSent) {
58
+ return {
59
+ type: "res",
60
+ id: res.id,
61
+ ...(notSent === undefined ? { body: res.body } : { [GATEWAY_NOT_SENT]: notSent }),
62
+ connectionId: conn.connectionId,
63
+ maxFrameBytes: conn.maxFrameBytes,
64
+ };
65
+ }
66
+ /**
67
+ * Reads a stored result (as stored, or as published on the bus): the machine's body with every
68
+ * kernel-internal field stripped, the connection it arrived on, and the gateway's not-sent refusal.
69
+ * Throws on anything else (no connection tag, a body missing without a refusal).
70
+ */
71
+ export function readStoredResult(stored) {
72
+ const r = typeof stored === "string" ? JSON.parse(stored) : stored;
73
+ if (!isRecord(r) || r.type !== "res" || typeof r.id !== "string")
74
+ throw new Error("the gateway stored a malformed result");
75
+ const { connectionId, maxFrameBytes } = r;
76
+ if (typeof connectionId !== "string" ||
77
+ connectionId === "" ||
78
+ typeof maxFrameBytes !== "number" ||
79
+ !Number.isSafeInteger(maxFrameBytes) ||
80
+ maxFrameBytes < 1024)
81
+ throw new Error("the gateway stored a result without its connection");
82
+ const connection = { connectionId, maxFrameBytes };
83
+ const notSent = r[GATEWAY_NOT_SENT];
84
+ if (typeof notSent === "string")
85
+ return { id: r.id, body: undefined, connection, notSent };
86
+ if (!("body" in r) || r.body === undefined)
87
+ throw new Error("the gateway stored a result without a body");
88
+ return { id: r.id, body: r.body, connection };
89
+ }
@@ -0,0 +1,214 @@
1
+ import { type ExtensionContext } from "@capybara-hq/plugin-sdk";
2
+ import type { WebSocket } from "@fastify/websocket";
3
+ import type { Redis } from "ioredis";
4
+ import { KernelConfigService } from "../infra/config/kernel-config.service.js";
5
+ import { LockService } from "../infra/lock/lock.service.js";
6
+ import type { KernelLogger } from "../log.js";
7
+ import { DialInHandlers, type DialInServing, type DialInUnavailable } from "./handlers.js";
8
+ import { DialInMachines, type MachineIdentity } from "./machines.js";
9
+ /** WebSocket close codes the gateway uses (the program reads 4002 and 4003). */
10
+ export declare const DEVICE_CLOSE: {
11
+ /** The kernel is shutting down: reconnect (another gateway replica takes over). */
12
+ readonly shutdown: 1001;
13
+ /** A frame larger than `devices.dialIn.maxFrameBytes` (sent by `ws` itself). */
14
+ readonly tooBig: 1009;
15
+ /** The kernel failed (e.g. the database): reconnect. */
16
+ readonly internal: 1011;
17
+ /** The lease was lost (renewal kept failing or the key was taken): reconnect. */
18
+ readonly leaseLost: 4001;
19
+ /** A newer connection of the device took over: the program exits. */
20
+ readonly replaced: 4002;
21
+ /** The machine's credential was revoked: the program exits. */
22
+ readonly revoked: 4003;
23
+ /**
24
+ * No valid `hello` first, a frame that is no valid envelope or does not fit the negotiated
25
+ * capacity, or the provider refused the hello (the reason carries its code).
26
+ */
27
+ readonly protocol: 4400;
28
+ /** The lease could not be claimed in time: reconnect. */
29
+ readonly claimTimeout: 4409;
30
+ };
31
+ /** How long the program has to send `hello` after the upgrade, and the provider to answer it. */
32
+ export declare const HELLO_TIMEOUT_MS = 10000;
33
+ /** How often a connection is pinged; two pings without a pong close it. */
34
+ export declare const PING_INTERVAL_MS = 20000;
35
+ /** How long a new connection retries the lease after asking the holder to let go. */
36
+ export declare const CLAIM_TIMEOUT_MS = 5000;
37
+ /** The pause between two tries at the lease while claiming. */
38
+ export declare const CLAIM_RETRY_MS = 250;
39
+ /**
40
+ * The most `event` frames a connection may have waiting for the provider (received, not yet
41
+ * handled): a machine sending faster than its provider handles them is closed (4400) instead of
42
+ * growing the gateway's memory.
43
+ */
44
+ export declare const MAX_EVENTS_IN_FLIGHT = 32;
45
+ type State = "hello" | "claiming" | "holding" | "closed";
46
+ /**
47
+ * One machine connection on this replica. Its state (the socket, the lease handle, the requests
48
+ * it forwarded) is the socket's own and lives exactly as long as the socket.
49
+ */
50
+ declare class DeviceConnection {
51
+ private readonly gw;
52
+ private readonly socket;
53
+ readonly identity: MachineIdentity;
54
+ private readonly credential;
55
+ /** The protocol the route admitted (the serving provider's `permissions.dialIn.protocol`). */
56
+ private readonly protocol;
57
+ readonly token: `${string}-${string}-${string}-${string}-${string}`;
58
+ state: State;
59
+ private lease;
60
+ private readonly unsubscribes;
61
+ private helloTimer;
62
+ private pingTimer;
63
+ private missedPongs;
64
+ private lastTouch;
65
+ private lastAuth;
66
+ /** The drains (and the close after a revocation) run one after another. */
67
+ private chain;
68
+ /** Resolves once the lease is handed back (or was never held). */
69
+ private released;
70
+ /** The hello's handling, then each event's, one after another (never rejects). */
71
+ private handled;
72
+ /** Aborted when the connection closes: the provider's `onHello` stops waiting. */
73
+ private readonly ended;
74
+ /** The provider whose `onHello` accepted this connection: it gets `onClose`. */
75
+ private accepted;
76
+ /** `event` frames received and not yet handled ({@link MAX_EVENTS_IN_FLIGHT}). */
77
+ private eventsInFlight;
78
+ /** Why the connection closed, for the provider's `onClose` (offline unless set). */
79
+ private closeReason;
80
+ constructor(gw: DialInGateway, socket: WebSocket, identity: MachineIdentity, credential: string,
81
+ /** The protocol the route admitted (the serving provider's `permissions.dialIn.protocol`). */
82
+ protocol: string);
83
+ /**
84
+ * The id a connection fences requests and results with (sent to the machine in `ready`).
85
+ * Random and separate from the lease token, which stays internal to the kernel.
86
+ */
87
+ readonly connectionId: `${string}-${string}-${string}-${string}-${string}`;
88
+ /** The negotiated capacity: the smaller of the machine's and the kernel's frame limits. */
89
+ private effectiveCap;
90
+ /** The program run the hello named (`programRunId`), published with the capacity. */
91
+ private programRunId;
92
+ private ready;
93
+ /** Consecutive failed capacity publications on ping (this socket's own state). */
94
+ private publishFailures;
95
+ private publishCapacity;
96
+ get deviceId(): string;
97
+ start(): void;
98
+ private onFrame;
99
+ /**
100
+ * The accepted hello: claims the lease, records the program facts, then asks the provider
101
+ * (`onHello`, within {@link HELLO_TIMEOUT_MS}); only an accepted hello gets `ready`. A refusal, a
102
+ * failure or the timeout releases the lease and closes 4400 with the provider's code.
103
+ */
104
+ private welcome;
105
+ /** The provider's answer to the hello body; a throw or the timeout is a refusal. */
106
+ private askProvider;
107
+ /** Hands one event body to the provider that accepted the hello (dropped otherwise). */
108
+ private dispatchEvent;
109
+ /**
110
+ * Stores one result and announces it (the relay's wire, envelope.ts), only for a request taken
111
+ * for this device (ruling R20); anything else is dropped. `notSent`: the gateway's own refusal
112
+ * of a request it never sent (never a machine's frame).
113
+ */
114
+ private store;
115
+ /**
116
+ * Claims the device's lease; true when this connection holds it. The newest connection
117
+ * wins (ruling R21): when another connection holds the lease, a takeover is published (the
118
+ * holder yields to any takeover) and the lease retried every {@link CLAIM_RETRY_MS} for
119
+ * {@link CLAIM_TIMEOUT_MS}, then 4409 (the program reconnects). A claimer never yields.
120
+ */
121
+ private claim;
122
+ /** Publishes the capacity, sends `ready` and drains what was queued meanwhile. */
123
+ private sendReady;
124
+ /** Subscribes for this connection; a subscription that completes after the close is ended. */
125
+ private subscribe;
126
+ /** Queues one drain of the request list after the ones already queued. */
127
+ private drain;
128
+ private drainOnce;
129
+ /** The next ping (a chain of timeouts; the lease renewal is LockService's, not this timer's). */
130
+ private schedulePing;
131
+ private ping;
132
+ /**
133
+ * The credential still stands (membership, org and device are read live); a deleted device
134
+ * closes as deleted, like its revocation does.
135
+ */
136
+ private reauthenticate;
137
+ private traffic;
138
+ /** Closes the socket with `code` and hands the lease back; idempotent. */
139
+ close(code: number, reason: string): Promise<void>;
140
+ /**
141
+ * Stops timers and subscriptions, releases the lease (a lost one is abandoned) and tells the
142
+ * provider that accepted the hello why the connection ended (`onClose`).
143
+ */
144
+ shutdown(): Promise<void>;
145
+ /** The provider's `onClose`, after the lease is handed back; its failure is only logged. */
146
+ private tellClosed;
147
+ }
148
+ /** What the device connection route learns before the upgrade: who connects and what it speaks. */
149
+ export type DialInAdmission = {
150
+ identity: MachineIdentity;
151
+ serving: DialInServing;
152
+ } | {
153
+ identity: MachineIdentity;
154
+ unavailable: DialInUnavailable["unavailable"];
155
+ } | null;
156
+ /**
157
+ * The gateway role's half of the dial-in relay (environments spec §13.4): holds the WebSocket
158
+ * connections of connected machines (`GET /device/connect`, routes.ts) and speaks the dial-in
159
+ * envelope with them. One connection per device across replicas, under the lease
160
+ * `capk:device-lease:<deviceId>` (the newest connection wins: it publishes
161
+ * `device.takeover.<deviceId>` and the holder lets go). A hello is answered by the device's
162
+ * provider (`DialInHandler.onHello`, {@link DialInHandlers}) once the lease is held; only an
163
+ * accepted hello gets `ready`. The holder drains `capk:device-req:<deviceId>` on every
164
+ * `device.req.<deviceId>` nudge, once after `ready` and on every ping, marking each forwarded
165
+ * request taken for its device (`capk:device-taken:<id>`, TTL the reconnect grace plus the
166
+ * request's deadline plus a margin). A `res` is accepted only for a request taken for the socket's
167
+ * device: it is stored under `capk:device-res:<id>` for the rest of that TTL and published on
168
+ * `device.res.<id>`. An `event` goes to the provider's `onFrame`; the close to its `onClose`.
169
+ * `device.revoked.<deviceId>` closes the connection of that machine only (ruling R16). The
170
+ * sockets are this process's own; everything other replicas need is in Redis.
171
+ */
172
+ export declare class DialInGateway {
173
+ readonly machines: Pick<DialInMachines, "authenticate" | "isDeviceDeleted" | "recordConnect" | "touch">;
174
+ readonly handlers: Pick<DialInHandlers, "forDevice">;
175
+ readonly locks: Pick<LockService, "hold">;
176
+ readonly bus: ExtensionContext["bus"];
177
+ readonly redis: Redis;
178
+ private readonly config;
179
+ readonly log: Pick<KernelLogger, "info" | "warn">;
180
+ /** This replica's open connections (the sockets cannot live anywhere else). */
181
+ private readonly connections;
182
+ private stopping;
183
+ /** How often connections are pinged ({@link PING_INTERVAL_MS}; a test seam). */
184
+ pingIntervalMs: number;
185
+ /** How often a held connection re-checks its credential ({@link REAUTH_EVERY_MS}; test seam). */
186
+ reauthEveryMs: number;
187
+ constructor(machines: Pick<DialInMachines, "authenticate" | "isDeviceDeleted" | "recordConnect" | "touch">, handlers: Pick<DialInHandlers, "forDevice">, locks: Pick<LockService, "hold">, bus: ExtensionContext["bus"], redis: Redis, config: KernelConfigService, log: Pick<KernelLogger, "info" | "warn">);
188
+ graceMs(): number;
189
+ leaseTtlMs(): number;
190
+ maxFrameBytes(): number;
191
+ /**
192
+ * Who a `Bearer` machine credential stands for and the provider serving its device (checked
193
+ * before the upgrade): null for an unknown or revoked credential.
194
+ */
195
+ admit(credential: string): Promise<DialInAdmission>;
196
+ /**
197
+ * Serves one upgraded socket of a machine until it closes, speaking `protocol` (the route
198
+ * admitted it). The credential and the provider are checked again here, with the socket paused,
199
+ * so a credential revoked or a provider switched off since the upgrade check never gets a
200
+ * connection.
201
+ */
202
+ accept(socket: WebSocket, credential: string, protocol: string): Promise<void>;
203
+ /**
204
+ * How many devices this process holds the connection of (replica presence reports it): only
205
+ * sockets past the hello that hold their device's lease, so a socket still claiming or being
206
+ * replaced by a newer one does not count a device twice.
207
+ */
208
+ connectionCount(): number;
209
+ /** @internal a connection that ended. */
210
+ forget(conn: DeviceConnection): void;
211
+ /** Closes every connection (1001) and hands their leases back. */
212
+ stop(): Promise<void>;
213
+ }
214
+ export {};