@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.
- package/bootstrap.json +9 -1
- package/dist/admin/admin.module.js +2 -0
- package/dist/admin/health.service.d.ts +11 -4
- package/dist/admin/health.service.js +33 -17
- package/dist/background/poll.job.js +2 -2
- package/dist/background/tasks.service.d.ts +5 -5
- package/dist/background/tasks.service.js +9 -8
- package/dist/catalog/credential-key.d.ts +10 -0
- package/dist/catalog/credential-key.js +28 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/plugin-upgrade.js +1 -19
- package/dist/catalog/requests.service.js +1 -1
- package/dist/config/schema.d.ts +1 -1
- package/dist/config/schema.js +9 -9
- package/dist/conversations/conversations.controller.d.ts +8 -3
- package/dist/conversations/conversations.controller.js +8 -3
- package/dist/conversations/conversations.service.d.ts +33 -15
- package/dist/conversations/conversations.service.js +60 -10
- package/dist/conversations/events.d.ts +21 -41
- package/dist/conversations/events.js +57 -31
- package/dist/conversations/permission-profiles.controller.d.ts +6 -4
- package/dist/conversations/permission-profiles.service.d.ts +6 -4
- package/dist/ctx/create-ctx.d.ts +20 -1
- package/dist/ctx/create-ctx.js +77 -6
- package/dist/ctx/device-provider-check.d.ts +6 -2
- package/dist/ctx/device-provider-check.js +17 -2
- package/dist/db/kernel-schema.d.ts +10 -48
- package/dist/devices/access.d.ts +3 -4
- package/dist/devices/access.js +3 -4
- package/dist/devices/conversation-devices.d.ts +1 -3
- package/dist/devices/conversation-devices.js +2 -7
- package/dist/devices/device-projects.service.js +24 -29
- package/dist/devices/device-provider-selection.d.ts +5 -6
- package/dist/devices/device-provider-selection.js +8 -9
- package/dist/devices/devices.controller.d.ts +8 -21
- package/dist/devices/devices.controller.js +9 -34
- package/dist/devices/devices.module.js +6 -4
- package/dist/devices/devices.schemas.d.ts +1 -5
- package/dist/devices/devices.schemas.js +1 -5
- package/dist/devices/devices.service.d.ts +84 -147
- package/dist/devices/devices.service.js +134 -229
- package/dist/devices/dto.d.ts +5 -32
- package/dist/devices/dto.js +5 -37
- package/dist/devices/errors.d.ts +6 -5
- package/dist/devices/errors.js +7 -8
- package/dist/devices/project-rules.d.ts +9 -11
- package/dist/devices/project-rules.js +7 -7
- package/dist/devices/working-root.d.ts +3 -6
- package/dist/devices/working-root.js +3 -8
- package/dist/dial-in/access.d.ts +23 -0
- package/dist/dial-in/access.js +64 -0
- package/dist/{local-devices/device-auth.controller.d.ts → dial-in/auth.controller.d.ts} +12 -6
- package/dist/{local-devices/device-auth.controller.js → dial-in/auth.controller.js} +30 -15
- package/dist/dial-in/auth.module.d.ts +7 -0
- package/dist/dial-in/auth.module.js +27 -0
- package/dist/{local-devices/device-auth.d.ts → dial-in/auth.service.d.ts} +42 -13
- package/dist/{local-devices/device-auth.js → dial-in/auth.service.js} +83 -32
- package/dist/dial-in/dial-in.module.d.ts +9 -0
- package/dist/dial-in/dial-in.module.js +37 -0
- package/dist/{local-devices → dial-in}/dto.d.ts +17 -2
- package/dist/{local-devices → dial-in}/dto.js +14 -2
- package/dist/dial-in/envelope.d.ts +95 -0
- package/dist/dial-in/envelope.js +89 -0
- package/dist/dial-in/gateway.d.ts +214 -0
- package/dist/{local-devices/local-device-gateway.js → dial-in/gateway.js} +337 -181
- package/dist/dial-in/handlers.d.ts +47 -0
- package/dist/dial-in/handlers.js +84 -0
- package/dist/dial-in/machines.d.ts +155 -0
- package/dist/dial-in/machines.js +383 -0
- package/dist/dial-in/relay.d.ts +135 -0
- package/dist/dial-in/relay.js +444 -0
- package/dist/dial-in/relay.module.d.ts +7 -0
- package/dist/dial-in/relay.module.js +24 -0
- package/dist/{local-devices/device-routes.d.ts → dial-in/routes.d.ts} +19 -11
- package/dist/{local-devices/device-routes.js → dial-in/routes.js} +45 -25
- package/dist/{local-devices → dial-in}/targets.d.ts +3 -18
- package/dist/{local-devices → dial-in}/targets.js +3 -48
- package/dist/errors.d.ts +8 -27
- package/dist/errors.js +8 -27
- package/dist/extensions/bootstrap.d.ts +5 -0
- package/dist/extensions/bootstrap.js +10 -1
- package/dist/extensions/extensions.module.js +6 -0
- package/dist/extensions/host.d.ts +6 -0
- package/dist/extensions/host.js +12 -2
- package/dist/extensions/permission-set.d.ts +15 -1
- package/dist/extensions/permission-set.js +11 -1
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +4 -1
- package/dist/http/api.module.js +4 -4
- package/dist/http/gateway-routes.d.ts +1 -1
- package/dist/http/gateway-routes.js +1 -1
- package/dist/http/meta.controller.js +1 -1
- package/dist/identity/settings.d.ts +1 -1
- package/dist/identity/settings.js +6 -8
- package/dist/infra/config/kernel-config.service.js +2 -2
- package/dist/infra/outbox/registry.d.ts +0 -14
- package/dist/infra/outbox/registry.js +0 -15
- package/dist/infra/throttler/named-throttlers.d.ts +1 -1
- package/dist/infra/throttler/named-throttlers.js +1 -1
- package/dist/kernel.module.js +0 -3
- package/dist/orgs/settings.service.js +5 -10
- package/dist/overrides.d.ts +0 -4
- package/dist/overrides.js +0 -2
- package/dist/platform/exclusions.js +5 -5
- package/dist/platform/ops/admin.ops.js +1 -1
- package/dist/platform/ops/conversations.ops.js +5 -2
- package/dist/platform/ops/devices.ops.js +11 -24
- package/dist/platform/ops/inbox.ops.js +1 -1
- package/dist/roles/gateway-role.d.ts +3 -3
- package/dist/roles/gateway-role.js +4 -4
- package/dist/roles/gateway.module.d.ts +1 -1
- package/dist/roles/gateway.module.js +5 -5
- package/dist/runtime/context/device-paths.d.ts +4 -1
- package/dist/runtime/context/permission-profile.js +5 -5
- package/dist/runtime/maintenance.d.ts +2 -5
- package/dist/runtime/maintenance.js +9 -19
- package/dist/runtime/project/knowledge.js +4 -5
- package/dist/runtime/project/knowledge.service.js +2 -1
- package/dist/sandbox/cloud-permission-snapshot.d.ts +1 -1
- package/dist/sandbox/cloud-permission-snapshot.js +1 -3
- package/dist/sandbox/device-reports.d.ts +74 -14
- package/dist/sandbox/device-reports.js +190 -33
- package/dist/sandbox/device-reports.module.d.ts +7 -0
- package/dist/sandbox/device-reports.module.js +25 -0
- package/dist/sandbox/device-sandbox.d.ts +39 -41
- package/dist/sandbox/device-sandbox.js +44 -83
- package/dist/sandbox/kernel-read.d.ts +2 -2
- package/dist/sandbox/legacy-retirement.d.ts +11 -59
- package/dist/sandbox/legacy-retirement.js +34 -186
- package/dist/sandbox/legacy-retirement.module.d.ts +2 -2
- package/dist/sandbox/legacy-retirement.module.js +2 -2
- package/dist/sandbox/managed-device-context.d.ts +16 -4
- package/dist/sandbox/managed-device-context.js +25 -8
- package/dist/sandbox/managed-device-context.module.js +4 -0
- package/dist/sandbox/modes.d.ts +3 -12
- package/dist/sandbox/modes.js +2 -14
- package/dist/{local-devices → sandbox}/network-grants.d.ts +2 -21
- package/dist/{local-devices → sandbox}/network-grants.js +4 -49
- package/dist/sandbox/operation-authority-types.d.ts +2 -1
- package/dist/sandbox/operation-authority.d.ts +6 -20
- package/dist/sandbox/operation-authority.js +43 -104
- package/dist/sandbox/operation-authority.module.js +1 -1
- package/dist/sandbox/operation-intents.d.ts +4 -1
- package/dist/sandbox/operation-intents.js +11 -2
- package/dist/sandbox/operation-reconcile.job.js +2 -4
- package/dist/sandbox/permission-availability.d.ts +6 -7
- package/dist/sandbox/permission-availability.js +7 -10
- package/dist/sandbox/permission-call-grants.d.ts +2 -1
- package/dist/sandbox/permission-call-grants.js +1 -7
- package/dist/sandbox/permission-call-types.d.ts +4 -4
- package/dist/sandbox/permission-ceiling-writes.d.ts +7 -23
- package/dist/sandbox/permission-ceiling-writes.js +8 -35
- package/dist/sandbox/permission-config-publication.d.ts +9 -1
- package/dist/sandbox/permission-config-publication.js +29 -21
- package/dist/sandbox/permission-context-types.d.ts +17 -10
- package/dist/sandbox/permission-facts.d.ts +98 -95
- package/dist/sandbox/permission-facts.js +160 -314
- package/dist/sandbox/permission-instance-writes.d.ts +23 -37
- package/dist/sandbox/permission-instance-writes.js +44 -121
- package/dist/sandbox/permission-runtime-reports.d.ts +38 -30
- package/dist/sandbox/permission-runtime-reports.js +105 -122
- package/dist/sandbox/providers.d.ts +26 -38
- package/dist/sandbox/providers.js +36 -18
- package/dist/sandbox/request-not-sent.d.ts +2 -13
- package/dist/sandbox/request-not-sent.js +2 -13
- package/dist/sandbox/review/permission-candidate.d.ts +13 -8
- package/dist/sandbox/review/permission-candidate.js +38 -48
- package/dist/sandbox/sandbox.module.js +7 -3
- package/dist/sandbox/sandbox.service.d.ts +49 -59
- package/dist/sandbox/sandbox.service.js +139 -259
- package/dist/schedules/fire.d.ts +15 -6
- package/dist/schedules/fire.js +27 -13
- package/dist/schedules/grant-rules.js +1 -1
- package/dist/schedules/scheduled-tasks.service.d.ts +0 -7
- package/dist/schedules/scheduled-tasks.service.js +1 -4
- package/dist/schedules/schedules.module.js +5 -3
- package/dist/skills/invoke.d.ts +1 -1
- package/dist/skills/invoke.js +2 -3
- package/dist/tools/approvals.service.d.ts +8 -5
- package/dist/tools/approvals.service.js +5 -5
- package/dist/tools/builtins/sandbox/bash.js +1 -2
- package/dist/tools/builtins/sandbox/common.d.ts +15 -9
- package/dist/tools/builtins/sandbox/common.js +37 -38
- package/dist/tools/connectors.controller.d.ts +2 -0
- package/dist/tools/connectors.schemas.d.ts +2 -0
- package/dist/tools/credentials.d.ts +2 -0
- package/dist/tools/network-asks.d.ts +19 -22
- package/dist/tools/network-asks.js +15 -21
- package/dist/tools/oauth/authorization-params.d.ts +7 -0
- package/dist/tools/oauth/authorization-params.js +19 -0
- package/dist/tools/oauth/flow.js +7 -0
- package/dist/tools/oauth/tokens.js +2 -1
- package/dist/tools/pipeline.d.ts +6 -7
- package/dist/tools/pipeline.js +17 -21
- package/dist/tools/tools.module.js +3 -7
- package/package.json +3 -4
- package/src/migrations/0177_dial_in_contract.sql +32 -0
- package/dist/local-devices/device-auth.module.d.ts +0 -7
- package/dist/local-devices/device-auth.module.js +0 -25
- package/dist/local-devices/device-policy.service.d.ts +0 -213
- package/dist/local-devices/device-policy.service.js +0 -605
- package/dist/local-devices/instance-bindings.d.ts +0 -82
- package/dist/local-devices/instance-bindings.js +0 -307
- package/dist/local-devices/local-device-gateway.d.ts +0 -153
- package/dist/local-devices/local-device-provider.d.ts +0 -122
- package/dist/local-devices/local-device-provider.js +0 -326
- package/dist/local-devices/local-devices.module.d.ts +0 -31
- package/dist/local-devices/local-devices.module.js +0 -100
- package/dist/local-devices/machines.d.ts +0 -211
- package/dist/local-devices/machines.js +0 -766
- package/dist/local-devices/owner-report.d.ts +0 -81
- package/dist/local-devices/owner-report.js +0 -150
- package/dist/local-devices/owner-reports.d.ts +0 -48
- package/dist/local-devices/owner-reports.js +0 -208
- package/dist/local-devices/policy-op-jobs.d.ts +0 -15
- package/dist/local-devices/policy-op-jobs.js +0 -12
- package/dist/local-devices/policy-summary.d.ts +0 -102
- package/dist/local-devices/policy-summary.js +0 -155
- package/dist/local-devices/relay.d.ts +0 -166
- package/dist/local-devices/relay.js +0 -559
- package/dist/tools/device-op-resolver.d.ts +0 -34
- package/dist/tools/device-op-resolver.js +0 -120
- /package/dist/{local-devices → dial-in}/keys.d.ts +0 -0
- /package/dist/{local-devices → dial-in}/keys.js +0 -0
- /package/dist/{local-devices → sandbox}/network-grants.module.d.ts +0 -0
- /package/dist/{local-devices → sandbox}/network-grants.module.js +0 -0
|
@@ -1,559 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
-
};
|
|
10
|
-
import { randomUUID } from "node:crypto";
|
|
11
|
-
import { DeviceProtocolError, FileSystemGoneError, parseDeviceFrame, parseLegacyDeviceFrame, parseOperationResult, parseOpResult, SandboxGoneError, SandboxOperationOutcomeUnknownError, SandboxPermissionUnsupportedError, SandboxScopeDeniedError, SandboxScopeValidationError, SandboxWorkUnsettledError, ToolError, } from "@capybara-hq/plugin-sdk";
|
|
12
|
-
import { Inject, Injectable } from "@nestjs/common";
|
|
13
|
-
import { machineRevoked } from "../devices/errors.js";
|
|
14
|
-
import { ApiError } from "../errors.js";
|
|
15
|
-
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
16
|
-
import { LockService } from "../infra/lock/lock.service.js";
|
|
17
|
-
import { KERNEL_BUS, KERNEL_LOGGER, REDIS } from "../infra/tokens.js";
|
|
18
|
-
import { DeviceProgramRestartedError, DeviceRequestNotSentError, isDeviceRequestNotSent, } from "../sandbox/request-not-sent.js";
|
|
19
|
-
import { deviceNotFound } from "../sandbox/sandbox.service.js";
|
|
20
|
-
import { deviceLeaseKey, deviceReqKey, deviceReqTopic, deviceResKey, deviceResTopic, } from "./keys.js";
|
|
21
|
-
import { DeviceTargets } from "./targets.js";
|
|
22
|
-
/** The device's connected machine is not connected (no replica holds its lease). */
|
|
23
|
-
export const deviceOffline = () => new ApiError("device.offline", "The machine of this local device is offline. Start capybara-device on that machine, or pick another device.");
|
|
24
|
-
export { machineRevoked } from "../devices/errors.js";
|
|
25
|
-
/** The text a machine's `policy` refusal reaches the model with. */
|
|
26
|
-
export const POLICY_REFUSAL_PREFIX = "Refused by the machine's local policy: ";
|
|
27
|
-
/** The text a machine's `error` result reaches the model with (ruling R28). */
|
|
28
|
-
export const ON_MACHINE_PREFIX = "On the connected machine: ";
|
|
29
|
-
/**
|
|
30
|
-
* The text of a staged (protocol 4) request refused because the machine's connection changed.
|
|
31
|
-
* Only an output read has an offset to retry from; every other operation is simply retried.
|
|
32
|
-
*/
|
|
33
|
-
export const connectionChangedMessage = (op) => op === "backgroundReadOutput"
|
|
34
|
-
? "The machine's connection changed during the read; retry the read from the same offset"
|
|
35
|
-
: `The machine's connection changed before ${op} was sent; retry the operation`;
|
|
36
|
-
export { DeviceRequestNotSentError, isDeviceRequestNotSent };
|
|
37
|
-
/**
|
|
38
|
-
* The operations held under a stable operation identity (native, preparation and browsing
|
|
39
|
-
* operations, execution, a background start and an operation revoke): their not-sent refusal is
|
|
40
|
-
* typed ({@link DeviceRequestNotSentError}) so the operation path records it as definite. Exact
|
|
41
|
-
* status, tail, stop and reads, the policy controls and binding keep their plain errors.
|
|
42
|
-
*/
|
|
43
|
-
const heldOperation = (op) => op === "backgroundStart" ||
|
|
44
|
-
op === "revokeOperation" ||
|
|
45
|
-
/^(?:exec|readFile|writeFile|stat|list|scan|prepare(?:ReadFile|WriteFile|Stat)|browse(?:Stat|List|Scan))$/.test(op);
|
|
46
|
-
/**
|
|
47
|
-
* Whether a staged operation's result is fenced to the connection its request was sent on.
|
|
48
|
-
* Protocol 4 (spec B5) fences every request to its connection, so an old capacity is never used on
|
|
49
|
-
* a replacement socket. For results, only an output read is fenced: its range was sized for the
|
|
50
|
-
* old connection's capacity and an in-flight read that meets a reconnect fails so the reader
|
|
51
|
-
* retries from the same offset. Every other operation keeps protocol 2's grace semantics: the
|
|
52
|
-
* machine keeps it running across a socket blip and its result, validated by the gateway against
|
|
53
|
-
* the capacity of the connection it arrived on, is accepted after a reconnect within the grace.
|
|
54
|
-
*/
|
|
55
|
-
const connectionFenced = (op) => op === "backgroundReadOutput";
|
|
56
|
-
/**
|
|
57
|
-
* The kernel-internal field of a queued `stop` naming the machine it is for (stripped by the holder
|
|
58
|
-
* before the frame goes out; a frame for another machine is dropped).
|
|
59
|
-
*/
|
|
60
|
-
export const STOP_MACHINE = "machineId";
|
|
61
|
-
/** The longest one lease check of a waiting caller may take before it counts as "unknown". */
|
|
62
|
-
export const LEASE_CHECK_TIMEOUT_MS = 2000;
|
|
63
|
-
/** How often a waiting caller checks that a replica still holds the machine's connection. */
|
|
64
|
-
export const HOLDER_POLL_MS = 5000;
|
|
65
|
-
/**
|
|
66
|
-
* How much longer than the caller's wait a queued request lives: giving up must still find an
|
|
67
|
-
* untaken request on the list (an expired one would look taken and get a needless cancel).
|
|
68
|
-
*/
|
|
69
|
-
export const LIST_TTL_MARGIN_MS = 30_000;
|
|
70
|
-
/**
|
|
71
|
-
* Appends one frame to the request list and makes sure the list lives at least `ARGV[2]` ms
|
|
72
|
-
* (never shortens the TTL a request with a longer deadline set). Atomic, so the list never exists
|
|
73
|
-
* without a TTL.
|
|
74
|
-
*/
|
|
75
|
-
const PUSH_WITH_TTL = `
|
|
76
|
-
redis.call('RPUSH', KEYS[1], ARGV[1])
|
|
77
|
-
if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[2]) then
|
|
78
|
-
redis.call('PEXPIRE', KEYS[1], ARGV[2])
|
|
79
|
-
end
|
|
80
|
-
return 1
|
|
81
|
-
`;
|
|
82
|
-
/**
|
|
83
|
-
* Gives up on a request (ruling R15): removes the exact request frame `ARGV[1]` from the list; only
|
|
84
|
-
* when it is no longer queued (the machine already took it) appends the cancel `ARGV[2]`, with the
|
|
85
|
-
* TTL rule of {@link PUSH_WITH_TTL}. Returns 1 when a cancel was queued.
|
|
86
|
-
*/
|
|
87
|
-
const GIVE_UP = `
|
|
88
|
-
if redis.call('LREM', KEYS[1], 1, ARGV[1]) > 0 then
|
|
89
|
-
return 0
|
|
90
|
-
end
|
|
91
|
-
redis.call('RPUSH', KEYS[1], ARGV[2])
|
|
92
|
-
if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[3]) then
|
|
93
|
-
redis.call('PEXPIRE', KEYS[1], ARGV[3])
|
|
94
|
-
end
|
|
95
|
-
return 1
|
|
96
|
-
`;
|
|
97
|
-
/** Why a call stopped waiting without a result. */
|
|
98
|
-
const ABORTED = Symbol("aborted");
|
|
99
|
-
const GRACE_EXPIRED = Symbol("grace expired");
|
|
100
|
-
/**
|
|
101
|
-
* The kernel-internal fields the gateway adds to what it stores for a staged result, next to the
|
|
102
|
-
* device's frame: the connection it arrived on, that connection's capacity and, only on the
|
|
103
|
-
* gateway's own refusal of a request queued for another connection, {@link GATEWAY_NOT_SENT}. The
|
|
104
|
-
* device's frame is parsed strictly (unknown keys refused), so a device can never set them.
|
|
105
|
-
*/
|
|
106
|
-
export const GATEWAY_NOT_SENT = "notSent";
|
|
107
|
-
/**
|
|
108
|
-
* A staged request refused because it was not sent, outside the operations held under an operation
|
|
109
|
-
* identity (a policy control, a status read): a protocol error that also says nothing ran, so a
|
|
110
|
-
* caller can record it as never received instead of unknown ({@link wasDeviceRequestNotSent}).
|
|
111
|
-
*/
|
|
112
|
-
export class DeviceRequestNotSentProtocolError extends DeviceProtocolError {
|
|
113
|
-
notSent = true;
|
|
114
|
-
}
|
|
115
|
-
/** Any staged request the kernel knows was never sent to the machine (held or not). */
|
|
116
|
-
export const wasDeviceRequestNotSent = (e) => e instanceof Error && e.notSent === true;
|
|
117
|
-
/** The not-sent refusal of `op`: typed for held operations, a not-sent protocol error otherwise. */
|
|
118
|
-
const notSentError = (op) => heldOperation(op)
|
|
119
|
-
? new DeviceRequestNotSentError(connectionChangedMessage(op))
|
|
120
|
-
: new DeviceRequestNotSentProtocolError(connectionChangedMessage(op));
|
|
121
|
-
const errMsg = (e) => (e instanceof Error ? e.message : String(e));
|
|
122
|
-
/**
|
|
123
|
-
* The caller side of the device relay (environments spec §13.4). A request goes onto the
|
|
124
|
-
* device's Redis list (`capk:device-req:<deviceId>`) and is announced on
|
|
125
|
-
* `device.req.<deviceId>`; whichever gateway replica holds the machine's lease drains the
|
|
126
|
-
* list and forwards the frames to the machine.
|
|
127
|
-
*
|
|
128
|
-
* **The holder's half of the wire** (Task 6 follows it): items on the list are `KernelReq` or
|
|
129
|
-
* `KernelCancel` frames as JSON text, to be parsed with `parseLegacyDeviceFrame(item, "kernel")`; the
|
|
130
|
-
* nudge's payload is `{ id }`. For each `res` frame the machine sends, the holder writes the frame
|
|
131
|
-
* (the JSON text as validated by `parseLegacyDeviceFrame(…, "device")`) under
|
|
132
|
-
* `capk:device-res:<requestId>` with a TTL and publishes the same frame object on
|
|
133
|
-
* `device.res.<requestId>`.
|
|
134
|
-
*
|
|
135
|
-
* The caller subscribes to its result topic before pushing and reads the result key once after,
|
|
136
|
-
* and once more before giving up (the run-abort pattern; a publish lost while the subscriber
|
|
137
|
-
* reconnects is still found). It waits within its own call for at most `deadlineMs` plus
|
|
138
|
-
* `devices.local.reconnectGraceSeconds`; when the lease disappears meanwhile (checked every
|
|
139
|
-
* {@link HOLDER_POLL_MS}) the wait ends at most one grace later, and when the machine is connected
|
|
140
|
-
* again as another run of its program (`programRunId`) it ends at that poll with
|
|
141
|
-
* {@link DeviceProgramRestartedError}: the old run's kept results died with it. A result produced
|
|
142
|
-
* while the machine was disconnected arrives after it reconnects. Giving up (grace, abort, a bad result)
|
|
143
|
-
* removes the request from the list, or queues a `cancel` when the machine already took it
|
|
144
|
-
* (ruling R15). Nothing is kept in process memory beyond the call.
|
|
145
|
-
*/
|
|
146
|
-
let LocalDeviceRelay = class LocalDeviceRelay {
|
|
147
|
-
redis;
|
|
148
|
-
bus;
|
|
149
|
-
locks;
|
|
150
|
-
config;
|
|
151
|
-
log;
|
|
152
|
-
targets;
|
|
153
|
-
constructor(redis, bus, locks, config, log, targets) {
|
|
154
|
-
this.redis = redis;
|
|
155
|
-
this.bus = bus;
|
|
156
|
-
this.locks = locks;
|
|
157
|
-
this.config = config;
|
|
158
|
-
this.log = log;
|
|
159
|
-
this.targets = targets;
|
|
160
|
-
}
|
|
161
|
-
/** Whether a gateway replica holds the machine's connection now. */
|
|
162
|
-
async holderLive(deviceId) {
|
|
163
|
-
return (await this.locks.holderOf(deviceLeaseKey(deviceId))) !== null;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* The negotiated transport of the device's connection for the platform's own controls (policy
|
|
167
|
-
* ops): every connection speaks protocol 4, so this is {@link connection} (a missing or unready
|
|
168
|
-
* one throws its typed error).
|
|
169
|
-
*/
|
|
170
|
-
async stagedTransport(deviceId) {
|
|
171
|
-
return this.connection(deviceId);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* The negotiated transport of the machine's current connection, for a staged call. Liveness is
|
|
175
|
-
* checked first (ruling R27): no holder throws `device.not_found` for a deleted device,
|
|
176
|
-
* `device.forbidden` for a revoked machine and `device.offline` otherwise. It carries the program
|
|
177
|
-
* run the connection's hello named (`programRunId`, when reported): kernel-internal evidence a
|
|
178
|
-
* staged call keeps, never put into a frame. Every staged call reads its connection here.
|
|
179
|
-
*/
|
|
180
|
-
async connection(deviceId) {
|
|
181
|
-
if (await this.holderLive(deviceId)) {
|
|
182
|
-
const current = await this.targets.current(deviceId);
|
|
183
|
-
if (current)
|
|
184
|
-
return current;
|
|
185
|
-
}
|
|
186
|
-
throw this.goneError(await this.targetState(deviceId)) ?? deviceOffline();
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Queues `stop` for the machine without waiting for an answer (ruling R14). The caller checks
|
|
190
|
-
* {@link holderLive} first; the frame waits on the list for the grace like any request. It is
|
|
191
|
-
* fenced to the machine `machineId` ({@link STOP_MACHINE}): a holder hands it only to that
|
|
192
|
-
* machine's connection (on a staged connection as a protocol 4 `stop`, across a socket blip
|
|
193
|
-
* too), never to a newer machine of the device. Only `stop` is ever queued this way: a broad
|
|
194
|
-
* kill (`killAll` / `killTagged`) has no frame on protocol 4.
|
|
195
|
-
*/
|
|
196
|
-
async send(deviceId, op, args, deadlineMs, machineId) {
|
|
197
|
-
const id = randomUUID();
|
|
198
|
-
const d = Math.max(1, Math.ceil(deadlineMs));
|
|
199
|
-
const req = { type: "req", id, op, args, deadlineMs: d };
|
|
200
|
-
const tagged = { ...req, [STOP_MACHINE]: machineId };
|
|
201
|
-
await this.push(deviceId, JSON.stringify(tagged), this.graceMs() + d + LIST_TTL_MARGIN_MS);
|
|
202
|
-
await this.bus.publish(deviceReqTopic(deviceId), { id });
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Runs `op` on the device's machine and returns its validated result. Throws
|
|
206
|
-
* `device.offline` when no replica holds the machine at send time or when the wait ran
|
|
207
|
-
* out, but `device.forbidden` ({@link machineRevoked}) when the machine was revoked and
|
|
208
|
-
* `device.not_found` when the device was deleted (checked whenever the connection is
|
|
209
|
-
* missing, ruling R27); the machine's failures map to `SandboxGoneError` (`gone`),
|
|
210
|
-
* `FileSystemGoneError` (`fs_gone`) or a `ToolError` (`policy`, and `error` prefixed
|
|
211
|
-
* {@link ON_MACHINE_PREFIX}); an invalid answer throws a `DeviceProtocolError`. On abort the
|
|
212
|
-
* abort reason is thrown.
|
|
213
|
-
*/
|
|
214
|
-
async call(deviceId, op, args, opts) {
|
|
215
|
-
return this.callWithCodec(deviceId, op, args, opts, false);
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Runs a protocol 4 operation on the device's machine over its current staged connection and
|
|
219
|
-
* returns its validated result; the request is checked against the protocol 4 codec first.
|
|
220
|
-
*/
|
|
221
|
-
async callV3(deviceId, op, args, opts) {
|
|
222
|
-
parseDeviceFrame(JSON.stringify({ type: "req", id: "validation", op, args, deadlineMs: opts.deadlineMs }), "kernel");
|
|
223
|
-
return this.callWithCodec(deviceId, op, args, opts, true);
|
|
224
|
-
}
|
|
225
|
-
async callWithCodec(deviceId, op, args, opts, staged) {
|
|
226
|
-
const { signal } = opts;
|
|
227
|
-
signal.throwIfAborted();
|
|
228
|
-
// liveness first: a deleted device or a revoked machine keeps its typed error (R27)
|
|
229
|
-
const current = staged ? await this.connection(deviceId) : undefined;
|
|
230
|
-
const connection = current && {
|
|
231
|
-
connectionId: current.connectionId,
|
|
232
|
-
maxFrameBytes: current.maxFrameBytes,
|
|
233
|
-
};
|
|
234
|
-
// the program run the request goes to: the drain sends it only on this connection (a newer
|
|
235
|
-
// one refuses it as not sent), so a later connection of another run proves its result lost
|
|
236
|
-
const sentToRun = current?.programRunId;
|
|
237
|
-
if (!staged && !(await this.holderLive(deviceId))) {
|
|
238
|
-
throw this.goneError(await this.targetState(deviceId)) ?? deviceOffline();
|
|
239
|
-
}
|
|
240
|
-
if (opts.connection &&
|
|
241
|
-
(connection?.connectionId !== opts.connection.connectionId ||
|
|
242
|
-
connection?.maxFrameBytes !== opts.connection.maxFrameBytes))
|
|
243
|
-
// nothing was queued: the request was definitely not sent
|
|
244
|
-
throw notSentError(op);
|
|
245
|
-
const fenced = staged && connectionFenced(op);
|
|
246
|
-
signal.throwIfAborted();
|
|
247
|
-
const id = randomUUID();
|
|
248
|
-
const deadlineMs = Math.max(1, Math.ceil(opts.deadlineMs));
|
|
249
|
-
const graceMs = this.graceMs();
|
|
250
|
-
const listTtlMs = graceMs + deadlineMs + LIST_TTL_MARGIN_MS;
|
|
251
|
-
const frame = { type: "req", id, op, args, deadlineMs };
|
|
252
|
-
if (connection)
|
|
253
|
-
parseDeviceFrame(JSON.stringify(frame), "kernel", connection.maxFrameBytes);
|
|
254
|
-
const reqJson = JSON.stringify({ ...frame, ...connection });
|
|
255
|
-
let settle;
|
|
256
|
-
const outcome = new Promise((resolve) => {
|
|
257
|
-
let done = false;
|
|
258
|
-
settle = (r) => {
|
|
259
|
-
if (done)
|
|
260
|
-
return;
|
|
261
|
-
done = true;
|
|
262
|
-
resolve(r);
|
|
263
|
-
};
|
|
264
|
-
});
|
|
265
|
-
const onStored = (raw) => {
|
|
266
|
-
if (raw !== null)
|
|
267
|
-
settle(this.parseRes(raw, id, op, connection, fenced));
|
|
268
|
-
};
|
|
269
|
-
const onAbort = () => settle({ error: ABORTED });
|
|
270
|
-
const unsubscribe = await this.bus.subscribe(deviceResTopic(id), (payload) => {
|
|
271
|
-
onStored(JSON.stringify(payload));
|
|
272
|
-
});
|
|
273
|
-
let timer;
|
|
274
|
-
let stopped = false;
|
|
275
|
-
try {
|
|
276
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
277
|
-
// aborted while subscribing: nothing was sent yet
|
|
278
|
-
signal.throwIfAborted();
|
|
279
|
-
await this.push(deviceId, reqJson, listTtlMs);
|
|
280
|
-
this.nudge(deviceId, id);
|
|
281
|
-
onStored(await this.redis.get(deviceResKey(id)));
|
|
282
|
-
// wait until the result, the abort, or `giveUpAt`. The lease is checked every poll: its loss
|
|
283
|
-
// caps `giveUpAt` at one grace from then (never extending it); a reconnect within that grace
|
|
284
|
-
// restores the full wait, so a long command whose machine blipped keeps its deadline
|
|
285
|
-
const fullGiveUpAt = Date.now() + deadlineMs + graceMs;
|
|
286
|
-
let giveUpAt = fullGiveUpAt;
|
|
287
|
-
let capped = false;
|
|
288
|
-
const tick = async () => {
|
|
289
|
-
if (stopped)
|
|
290
|
-
return;
|
|
291
|
-
try {
|
|
292
|
-
// a stalled Redis must not hold the give-up back: no answer in time means "unknown"
|
|
293
|
-
const live = await this.leaseCheck(deviceId);
|
|
294
|
-
if (fenced && connection && live === true) {
|
|
295
|
-
// an output read never waits across a reconnect: its range was sized for this socket
|
|
296
|
-
const current = await this.targets.current(deviceId);
|
|
297
|
-
if (current &&
|
|
298
|
-
(current.connectionId !== connection.connectionId ||
|
|
299
|
-
current.maxFrameBytes !== connection.maxFrameBytes)) {
|
|
300
|
-
settle({ error: new DeviceProtocolError(connectionChangedMessage(op)) });
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
if (!fenced && sentToRun !== undefined && live === true) {
|
|
305
|
-
// results a program keeps for a reconnect live in its memory: once the machine is
|
|
306
|
-
// connected as another run, this request's result can never arrive (unless it was
|
|
307
|
-
// stored before the old run went away, which the last look below still finds)
|
|
308
|
-
const now = await this.targets.current(deviceId).catch(() => null);
|
|
309
|
-
if (now?.programRunId !== undefined && now.programRunId !== sentToRun) {
|
|
310
|
-
this.redis.get(deviceResKey(id)).then((raw) => raw !== null
|
|
311
|
-
? onStored(raw)
|
|
312
|
-
: settle({ error: new DeviceProgramRestartedError(op) }), () => settle({ error: new DeviceProgramRestartedError(op) }));
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (live === true && capped) {
|
|
317
|
-
capped = false;
|
|
318
|
-
giveUpAt = fullGiveUpAt;
|
|
319
|
-
}
|
|
320
|
-
else if (live === false && !capped) {
|
|
321
|
-
capped = true;
|
|
322
|
-
giveUpAt = Math.min(giveUpAt, Date.now() + graceMs);
|
|
323
|
-
}
|
|
324
|
-
if (live === false) {
|
|
325
|
-
// a revoked machine or a deleted device never comes back: fail at once
|
|
326
|
-
const gone = this.goneError(await this.targetState(deviceId));
|
|
327
|
-
if (gone) {
|
|
328
|
-
settle({ error: gone });
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
catch (e) {
|
|
334
|
-
this.log.warn("device relay lease check failed", { deviceId, error: errMsg(e) });
|
|
335
|
-
}
|
|
336
|
-
if (stopped)
|
|
337
|
-
return;
|
|
338
|
-
if (Date.now() >= giveUpAt) {
|
|
339
|
-
// a last look at the key: the publish may have been lost while the subscriber reconnected
|
|
340
|
-
this.redis.get(deviceResKey(id)).then((raw) => (raw !== null ? onStored(raw) : settle({ error: GRACE_EXPIRED })), () => settle({ error: GRACE_EXPIRED }));
|
|
341
|
-
return;
|
|
342
|
-
}
|
|
343
|
-
schedule();
|
|
344
|
-
};
|
|
345
|
-
const schedule = () => {
|
|
346
|
-
if (stopped)
|
|
347
|
-
return;
|
|
348
|
-
const wait = Math.max(0, Math.min(HOLDER_POLL_MS, giveUpAt - Date.now()));
|
|
349
|
-
timer = setTimeout(() => void tick(), wait);
|
|
350
|
-
};
|
|
351
|
-
schedule();
|
|
352
|
-
const r = await outcome;
|
|
353
|
-
stopped = true;
|
|
354
|
-
clearTimeout(timer);
|
|
355
|
-
if ("res" in r) {
|
|
356
|
-
// the result's connection tag was checked in parseRes: a later reconnect changes nothing
|
|
357
|
-
await this.quietly("delete the result", deviceId, () => this.redis.del(deviceResKey(id)));
|
|
358
|
-
// only the gateway's marker says a request never reached the machine: never its text
|
|
359
|
-
if (connection && r.notSent)
|
|
360
|
-
throw notSentError(op);
|
|
361
|
-
return staged && r.res.ok
|
|
362
|
-
? parseOperationResult(op, r.res.result, op === "backgroundReadOutput"
|
|
363
|
-
? args
|
|
364
|
-
: op === "bindInstance"
|
|
365
|
-
? args
|
|
366
|
-
: undefined)
|
|
367
|
-
: this.resultOf(op, r.res);
|
|
368
|
-
}
|
|
369
|
-
if (r.error instanceof Error) {
|
|
370
|
-
// a bad answer: drop it, give the request up, report the protocol error
|
|
371
|
-
await this.quietly("delete the result", deviceId, () => this.redis.del(deviceResKey(id)));
|
|
372
|
-
}
|
|
373
|
-
await this.giveUp(deviceId, id, reqJson, listTtlMs, connection);
|
|
374
|
-
if (r.error === ABORTED)
|
|
375
|
-
throw signal.reason;
|
|
376
|
-
if (r.error === GRACE_EXPIRED)
|
|
377
|
-
throw deviceOffline();
|
|
378
|
-
throw r.error;
|
|
379
|
-
}
|
|
380
|
-
finally {
|
|
381
|
-
stopped = true;
|
|
382
|
-
clearTimeout(timer);
|
|
383
|
-
signal.removeEventListener("abort", onAbort);
|
|
384
|
-
await this.quietly("unsubscribe", deviceId, unsubscribe);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
/** {@link holderLive}, or undefined when it takes longer than {@link LEASE_CHECK_TIMEOUT_MS}. */
|
|
388
|
-
async leaseCheck(deviceId) {
|
|
389
|
-
let timer;
|
|
390
|
-
const check = this.holderLive(deviceId);
|
|
391
|
-
check.catch(() => { }); // a late failure after the timeout is not unhandled
|
|
392
|
-
try {
|
|
393
|
-
return await Promise.race([
|
|
394
|
-
check,
|
|
395
|
-
new Promise((resolve) => {
|
|
396
|
-
timer = setTimeout(() => resolve(undefined), LEASE_CHECK_TIMEOUT_MS);
|
|
397
|
-
}),
|
|
398
|
-
]);
|
|
399
|
-
}
|
|
400
|
-
finally {
|
|
401
|
-
clearTimeout(timer);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
/**
|
|
405
|
-
* The device's state ({@link DeviceTargets}), or undefined when it cannot be read within
|
|
406
|
-
* {@link LEASE_CHECK_TIMEOUT_MS} (a failure only logs: the caller keeps its other rules).
|
|
407
|
-
*/
|
|
408
|
-
async targetState(deviceId) {
|
|
409
|
-
let timer;
|
|
410
|
-
const read = this.targets.state(deviceId);
|
|
411
|
-
read.catch(() => { });
|
|
412
|
-
try {
|
|
413
|
-
return await Promise.race([
|
|
414
|
-
read,
|
|
415
|
-
new Promise((resolve) => {
|
|
416
|
-
timer = setTimeout(() => resolve(undefined), LEASE_CHECK_TIMEOUT_MS);
|
|
417
|
-
}),
|
|
418
|
-
]);
|
|
419
|
-
}
|
|
420
|
-
catch (e) {
|
|
421
|
-
this.log.warn("device relay could not read the device state", {
|
|
422
|
-
deviceId,
|
|
423
|
-
error: errMsg(e),
|
|
424
|
-
});
|
|
425
|
-
return undefined;
|
|
426
|
-
}
|
|
427
|
-
finally {
|
|
428
|
-
clearTimeout(timer);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
/** The error of a state that never comes back; undefined for `active` or unknown. */
|
|
432
|
-
goneError(state) {
|
|
433
|
-
if (state === "deleted")
|
|
434
|
-
return deviceNotFound();
|
|
435
|
-
if (state === "revoked")
|
|
436
|
-
return machineRevoked();
|
|
437
|
-
return undefined;
|
|
438
|
-
}
|
|
439
|
-
graceMs() {
|
|
440
|
-
return this.config.get().devices.local.reconnectGraceSeconds * 1000;
|
|
441
|
-
}
|
|
442
|
-
async push(deviceId, frame, ttlMs) {
|
|
443
|
-
await this.redis.eval(PUSH_WITH_TTL, 1, deviceReqKey(deviceId), frame, String(ttlMs));
|
|
444
|
-
}
|
|
445
|
-
/** Announces new frames to the holder; a lost nudge is picked up on the holder's next drain. */
|
|
446
|
-
nudge(deviceId, id) {
|
|
447
|
-
this.bus.publish(deviceReqTopic(deviceId), { id }).catch((e) => {
|
|
448
|
-
this.log.warn("device relay nudge failed", { deviceId, error: errMsg(e) });
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
/** Removes the request from the list, or queues a cancel when the machine took it (R15). */
|
|
452
|
-
async giveUp(deviceId, id, reqJson, ttlMs, connection) {
|
|
453
|
-
const cancel = JSON.stringify({ type: "cancel", id, ...connection });
|
|
454
|
-
await this.quietly("give the request up", deviceId, async () => {
|
|
455
|
-
const queued = await this.redis.eval(GIVE_UP, 1, deviceReqKey(deviceId), reqJson, cancel, String(ttlMs));
|
|
456
|
-
if (queued === 1)
|
|
457
|
-
this.nudge(deviceId, id);
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
/** Runs a side step whose failure must not replace the call's outcome: logged, swallowed. */
|
|
461
|
-
async quietly(what, deviceId, fn) {
|
|
462
|
-
try {
|
|
463
|
-
await fn();
|
|
464
|
-
}
|
|
465
|
-
catch (e) {
|
|
466
|
-
this.log.warn(`device relay could not ${what}`, { deviceId, error: errMsg(e) });
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
/**
|
|
470
|
-
* A stored / published `res` frame for request `id`, or the protocol error it carries. A staged
|
|
471
|
-
* frame carries the tag of the connection it arrived on (added by the gateway); a fenced
|
|
472
|
-
* operation ({@link connectionFenced}) accepts only its request's connection, any other staged
|
|
473
|
-
* operation a well-formed tag, and the frame is parsed against that connection's capacity.
|
|
474
|
-
*/
|
|
475
|
-
parseRes(raw, id, op, connection, fenced = false) {
|
|
476
|
-
try {
|
|
477
|
-
let wire = raw;
|
|
478
|
-
let cap;
|
|
479
|
-
let arrivedOn;
|
|
480
|
-
let notSent = false;
|
|
481
|
-
if (connection) {
|
|
482
|
-
const { connectionId, maxFrameBytes, [GATEWAY_NOT_SENT]: marker, ...frame } = JSON.parse(raw);
|
|
483
|
-
if (typeof connectionId !== "string" ||
|
|
484
|
-
connectionId === "" ||
|
|
485
|
-
typeof maxFrameBytes !== "number" ||
|
|
486
|
-
!Number.isSafeInteger(maxFrameBytes) ||
|
|
487
|
-
maxFrameBytes < 1024)
|
|
488
|
-
throw new DeviceProtocolError("the gateway stored a result without its connection");
|
|
489
|
-
if (fenced &&
|
|
490
|
-
(connectionId !== connection.connectionId || maxFrameBytes !== connection.maxFrameBytes))
|
|
491
|
-
throw new DeviceProtocolError(connectionChangedMessage(op));
|
|
492
|
-
cap = maxFrameBytes;
|
|
493
|
-
arrivedOn = connectionId;
|
|
494
|
-
notSent = marker === true;
|
|
495
|
-
wire = JSON.stringify(frame);
|
|
496
|
-
}
|
|
497
|
-
const f = connection
|
|
498
|
-
? parseDeviceFrame(wire, "device", cap)
|
|
499
|
-
: parseLegacyDeviceFrame(wire, "device");
|
|
500
|
-
if (f.type !== "res" || f.id !== id) {
|
|
501
|
-
return { error: new DeviceProtocolError("the connected machine sent an unexpected frame") };
|
|
502
|
-
}
|
|
503
|
-
if (arrivedOn === undefined)
|
|
504
|
-
return { res: f };
|
|
505
|
-
// a refusal only: a successful result never carries the marker
|
|
506
|
-
return notSent && !f.ok
|
|
507
|
-
? { res: f, connectionId: arrivedOn, notSent: true }
|
|
508
|
-
: { res: f, connectionId: arrivedOn };
|
|
509
|
-
}
|
|
510
|
-
catch (e) {
|
|
511
|
-
return {
|
|
512
|
-
error: e instanceof DeviceProtocolError
|
|
513
|
-
? e
|
|
514
|
-
: new DeviceProtocolError(`the connected machine sent an invalid frame: ${errMsg(e)}`),
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
resultOf(op, res) {
|
|
519
|
-
if (res.ok) {
|
|
520
|
-
try {
|
|
521
|
-
return parseOpResult(op, res.result);
|
|
522
|
-
}
|
|
523
|
-
catch (e) {
|
|
524
|
-
throw new DeviceProtocolError(`The connected machine sent an invalid ${op} result: ${errMsg(e)}`);
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
const { kind, message } = res.error;
|
|
528
|
-
switch (kind) {
|
|
529
|
-
case "denied":
|
|
530
|
-
throw new SandboxScopeDeniedError(message);
|
|
531
|
-
case "unsupported":
|
|
532
|
-
throw new SandboxPermissionUnsupportedError(message);
|
|
533
|
-
case "unsettled":
|
|
534
|
-
throw new SandboxWorkUnsettledError(message);
|
|
535
|
-
case "unknown":
|
|
536
|
-
throw new SandboxOperationOutcomeUnknownError(message);
|
|
537
|
-
case "invalid":
|
|
538
|
-
throw new SandboxScopeValidationError(message);
|
|
539
|
-
case "gone":
|
|
540
|
-
throw new SandboxGoneError(message);
|
|
541
|
-
case "fs_gone":
|
|
542
|
-
throw new FileSystemGoneError(message);
|
|
543
|
-
case "policy":
|
|
544
|
-
throw new ToolError(`${POLICY_REFUSAL_PREFIX}${message}`);
|
|
545
|
-
default:
|
|
546
|
-
throw new ToolError(`${ON_MACHINE_PREFIX}${message}`);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
};
|
|
550
|
-
LocalDeviceRelay = __decorate([
|
|
551
|
-
Injectable(),
|
|
552
|
-
__param(0, Inject(REDIS)),
|
|
553
|
-
__param(1, Inject(KERNEL_BUS)),
|
|
554
|
-
__param(2, Inject(LockService)),
|
|
555
|
-
__param(3, Inject(KernelConfigService)),
|
|
556
|
-
__param(4, Inject(KERNEL_LOGGER)),
|
|
557
|
-
__param(5, Inject(DeviceTargets))
|
|
558
|
-
], LocalDeviceRelay);
|
|
559
|
-
export { LocalDeviceRelay };
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
2
|
-
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
3
|
-
import { DevicePolicyService } from "../local-devices/device-policy.service.js";
|
|
4
|
-
import type { DeviceOpResolveData } from "../local-devices/policy-op-jobs.js";
|
|
5
|
-
/**
|
|
6
|
-
* The `device-op-resolve` job (transactional outbox spec §7): records what became of a device
|
|
7
|
-
* policy op whose outcome its caller did not record (a crash between the call and the resolve
|
|
8
|
-
* transaction, an answer that never came). Owed by the op's intent for when the op can no longer
|
|
9
|
-
* reach the machine, and by a hello that reported the op (`device-op-reported-<opId>`).
|
|
10
|
-
*
|
|
11
|
-
* An op without an entry any more is resolved already: a no-op. Otherwise the outcome is the one
|
|
12
|
-
* the hello reported, else the machine's answer to a read-only `policyStatus`: `applied` →
|
|
13
|
-
* recorded applied; `refused`, never received (`unknown` to the machine, or `absent` from its
|
|
14
|
-
* hello, once the op can no longer reach it) → failed; `started` (the machine stopped between
|
|
15
|
-
* its log and its answer), once the op can no longer reach it → applied when the policy it
|
|
16
|
-
* reports holds the change, else failed (`interrupted`); no answer (`offline`) → `unknown`, left
|
|
17
|
-
* for the machine's next hello. It never sends the change
|
|
18
|
-
* again. No op answers a network ask any more: the deployment never adds a host to a local device
|
|
19
|
-
* (device protocol 4), so a network ask is only ever a run grant and an op only records its outcome.
|
|
20
|
-
*/
|
|
21
|
-
export declare class DeviceOpResolver {
|
|
22
|
-
private readonly db;
|
|
23
|
-
readonly txHost: TransactionHost;
|
|
24
|
-
private readonly policies;
|
|
25
|
-
constructor(db: KernelDb, txHost: TransactionHost, policies: Pick<DevicePolicyService, "outcomeOf" | "settleIn" | "lockMachineDevice">);
|
|
26
|
-
resolve(data: DeviceOpResolveData): Promise<void>;
|
|
27
|
-
/**
|
|
28
|
-
* The outcome a hello reported; a refusal's message (a hello carries none) is the machine's
|
|
29
|
-
* answer to `policyStatus` when it is connected.
|
|
30
|
-
*/
|
|
31
|
-
private reportedResult;
|
|
32
|
-
/** The resolve transaction: the op's outcome. */
|
|
33
|
-
private settle;
|
|
34
|
-
}
|