@capybara-hq/kernel 0.1.0-rc.112 → 0.1.0-rc.114
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/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/channels/actions.js +2 -0
- package/dist/channels/cards.d.ts +4 -3
- package/dist/channels/cards.js +18 -3
- package/dist/channels/phrases.d.ts +15 -0
- package/dist/channels/phrases.js +9 -0
- package/dist/conversations/conversations.service.js +19 -5
- package/dist/conversations/events.d.ts +13 -0
- package/dist/conversations/events.js +24 -3
- package/dist/ctx/create-ctx.d.ts +1 -1
- package/dist/ctx/create-ctx.js +6 -1
- package/dist/db/kernel-schema.d.ts +26 -0
- package/dist/device-access/device-access.service.d.ts +74 -3
- package/dist/device-access/device-access.service.js +221 -7
- package/dist/devices/devices.service.js +3 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +6 -0
- package/dist/extensions/permission-set.d.ts +11 -1
- package/dist/extensions/permission-set.js +15 -1
- package/dist/extensions/permission-view.d.ts +3 -2
- package/dist/extensions/permission-view.js +15 -2
- package/dist/infra/outbox/registry.d.ts +14 -0
- package/dist/infra/outbox/registry.js +14 -0
- package/dist/orgs/offboarding.js +5 -1
- package/dist/platform/ops/agent-skills.ops.js +6 -2
- package/dist/roles/worker.module.js +3 -0
- package/dist/runtime/maintenance.d.ts +4 -2
- package/dist/runtime/maintenance.js +14 -4
- package/dist/sandbox/device-sandbox.d.ts +12 -0
- package/dist/sandbox/device-sandbox.js +2 -0
- package/dist/sandbox/device-services.d.ts +42 -2
- package/dist/sandbox/device-services.js +74 -14
- package/dist/sandbox/loopback-expiry.job.d.ts +21 -0
- package/dist/sandbox/loopback-expiry.job.js +50 -0
- package/dist/sandbox/loopback-grant-records.d.ts +143 -0
- package/dist/sandbox/loopback-grant-records.js +239 -0
- package/dist/sandbox/loopback-grants.d.ts +119 -0
- package/dist/sandbox/loopback-grants.js +454 -0
- package/dist/sandbox/loopback-live-ports.d.ts +12 -0
- package/dist/sandbox/loopback-live-ports.js +43 -0
- package/dist/sandbox/permission-call-types.d.ts +3 -2
- package/dist/sandbox/permission-ceiling-writes.js +13 -1
- package/dist/sandbox/permission-facts.d.ts +4 -0
- package/dist/sandbox/permission-facts.js +4 -0
- package/dist/sandbox/review/classifier.d.ts +26 -4
- package/dist/sandbox/review/classifier.js +31 -4
- package/dist/sandbox/review/index.d.ts +3 -3
- package/dist/sandbox/review/index.js +2 -2
- package/dist/sandbox/review/permission-candidate.d.ts +5 -4
- package/dist/sandbox/review/permission-candidate.js +6 -5
- package/dist/sandbox/review/review.service.d.ts +69 -0
- package/dist/sandbox/review/review.service.js +117 -0
- package/dist/sandbox/sandbox.module.d.ts +1 -1
- package/dist/sandbox/sandbox.module.js +13 -2
- package/dist/skills/invocable.controller.d.ts +4 -0
- package/dist/skills/invocable.controller.js +5 -1
- package/dist/skills/invoke.d.ts +68 -2
- package/dist/skills/invoke.js +128 -13
- package/dist/skills/skills.schemas.d.ts +7 -1
- package/dist/skills/skills.schemas.js +11 -2
- package/dist/tools/approvals.service.d.ts +82 -6
- package/dist/tools/approvals.service.js +233 -11
- package/dist/tools/call-scope.d.ts +9 -1
- package/dist/tools/loopback-asks.d.ts +52 -0
- package/dist/tools/loopback-asks.js +70 -0
- package/dist/tools/network-asks.d.ts +2 -1
- package/dist/tools/network-asks.js +10 -5
- package/dist/tools/pipeline.d.ts +59 -3
- package/dist/tools/pipeline.js +403 -40
- package/dist/tools/tools.module.js +3 -0
- package/package.json +2 -2
- package/src/migrations/0199_device_loopback_grants.sql +59 -0
|
@@ -35,6 +35,8 @@ import { Offboarding, RUN_STOP_JOB } from "../orgs/offboarding.js";
|
|
|
35
35
|
import { OrgsModule } from "../orgs/orgs.module.js";
|
|
36
36
|
import { CloudPermissionSnapshots } from "../sandbox/cloud-permission-snapshot.js";
|
|
37
37
|
import { runSandboxDestroyFor, runSandboxResetFiles, SANDBOX_DESTROY_FOR_JOB, SANDBOX_RESET_FILES_JOB, } from "../sandbox/jobs.js";
|
|
38
|
+
import { LOOPBACK_REFRESH_JOB, LOOPBACK_REFRESH_TIMEOUT_MS, } from "../sandbox/loopback-grant-records.js";
|
|
39
|
+
import { LoopbackGrants } from "../sandbox/loopback-grants.js";
|
|
38
40
|
import { HOLDER_REVOKE, OPERATION_RECONCILE, OPERATION_REVOKE, } from "../sandbox/operation-intents.js";
|
|
39
41
|
import { SandboxOperationReconcileJob } from "../sandbox/operation-reconcile.job.js";
|
|
40
42
|
import { PERMISSION_APPROVAL_RECHECK } from "../sandbox/permission-transitions.js";
|
|
@@ -68,7 +70,7 @@ export const MAINTENANCE_CONCURRENCY = 5;
|
|
|
68
70
|
* {@link BlobObjects}, `file-library-hash`, `file-library-hash-org` → {@link FileLibraryHashJob},
|
|
69
71
|
* `file-library-backfill` → {@link FileLibraryBackfill}, `platform-icon-delete` →
|
|
70
72
|
* {@link PlatformIconObjects}, `device-conversation-notice` → {@link ConversationNoticeDelivery},
|
|
71
|
-
* `idle-compaction` → {@link IdleCompactionJob}). The name of a periodic job (a `@LockedCron` / `@LockedInterval` job,
|
|
73
|
+
* `idle-compaction` → {@link IdleCompactionJob}, `device-loopback-refresh` → {@link LoopbackGrants}). The name of a periodic job (a `@LockedCron` / `@LockedInterval` job,
|
|
72
74
|
* such as `sandbox-suspend-idle`) runs that job's body once ({@link ScheduledRuns.runNow}), as the
|
|
73
75
|
* maintenance handlers did when those jobs were schedulers on this queue: an operator's or a
|
|
74
76
|
* test's one-off run, not a second schedule (decision R23 of the Nest kernel spec). Any other
|
|
@@ -97,8 +99,9 @@ let MaintenanceProcessor = class MaintenanceProcessor extends WorkerHost {
|
|
|
97
99
|
triggers;
|
|
98
100
|
coalescer;
|
|
99
101
|
idleCompaction;
|
|
102
|
+
loopback;
|
|
100
103
|
snapshots;
|
|
101
|
-
constructor(permissionApprovals, waits, sandbox, drift, directory, deletion, offboarding, runs, ownedCleanup, automations, scheduled, log, operations, shareObjects, blobObjects, hashing, backfill, platformIcons, notices, triggers, coalescer, idleCompaction, snapshots) {
|
|
104
|
+
constructor(permissionApprovals, waits, sandbox, drift, directory, deletion, offboarding, runs, ownedCleanup, automations, scheduled, log, operations, shareObjects, blobObjects, hashing, backfill, platformIcons, notices, triggers, coalescer, idleCompaction, loopback, snapshots) {
|
|
102
105
|
super();
|
|
103
106
|
this.permissionApprovals = permissionApprovals;
|
|
104
107
|
this.waits = waits;
|
|
@@ -122,6 +125,7 @@ let MaintenanceProcessor = class MaintenanceProcessor extends WorkerHost {
|
|
|
122
125
|
this.triggers = triggers;
|
|
123
126
|
this.coalescer = coalescer;
|
|
124
127
|
this.idleCompaction = idleCompaction;
|
|
128
|
+
this.loopback = loopback;
|
|
125
129
|
this.snapshots = snapshots;
|
|
126
130
|
}
|
|
127
131
|
process(job, token) {
|
|
@@ -265,6 +269,11 @@ let MaintenanceProcessor = class MaintenanceProcessor extends WorkerHost {
|
|
|
265
269
|
await this.notices.attempt(orgId, noticeId, { timeoutMs: NOTICE_PROMPT_TIMEOUT_MS });
|
|
266
270
|
return;
|
|
267
271
|
}
|
|
272
|
+
// a service's live loopback port set sent to it, after revoking what the device now
|
|
273
|
+
// refuses (browser loopback port grants design §4)
|
|
274
|
+
case LOOPBACK_REFRESH_JOB:
|
|
275
|
+
await this.loopback.refresh(job.data, AbortSignal.timeout(LOOPBACK_REFRESH_TIMEOUT_MS));
|
|
276
|
+
return;
|
|
268
277
|
// a large idle conversation's compaction before its prompt cache expires (idle compaction §4.3)
|
|
269
278
|
case IDLE_COMPACTION_JOB:
|
|
270
279
|
await this.idleCompaction.fire(job.data);
|
|
@@ -315,8 +324,9 @@ MaintenanceProcessor = __decorate([
|
|
|
315
324
|
__param(19, Inject(TriggerDispatcher)),
|
|
316
325
|
__param(20, Inject(EventCoalescer)),
|
|
317
326
|
__param(21, Inject(IdleCompactionJob)),
|
|
318
|
-
__param(22,
|
|
319
|
-
__param(
|
|
327
|
+
__param(22, Inject(LoopbackGrants)),
|
|
328
|
+
__param(23, Optional()),
|
|
329
|
+
__param(23, Inject(CloudPermissionSnapshots))
|
|
320
330
|
], MaintenanceProcessor);
|
|
321
331
|
export { MaintenanceProcessor };
|
|
322
332
|
/**
|
|
@@ -78,6 +78,11 @@ export interface DeviceSandboxProviderLayer {
|
|
|
78
78
|
openPublic: boolean;
|
|
79
79
|
allowlist: boolean;
|
|
80
80
|
perOperationHosts: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Whether the provider can open one loopback port of the device to one device service (support
|
|
83
|
+
* `network.loopbackPorts`; absent is false).
|
|
84
|
+
*/
|
|
85
|
+
loopbackPorts?: boolean;
|
|
81
86
|
};
|
|
82
87
|
outsideSandbox: boolean;
|
|
83
88
|
/** Who keeps a closed crossing closed when not the provider itself (support `limitedBy`). */
|
|
@@ -102,6 +107,12 @@ export interface DeviceSandbox {
|
|
|
102
107
|
* reports `perOperationHosts` (each operation carries its own network).
|
|
103
108
|
*/
|
|
104
109
|
perCallHosts: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Whether a device service may be granted one loopback port of the device (browser loopback port
|
|
112
|
+
* grants design §4): only where a call may run outside the sandbox and the provider reports
|
|
113
|
+
* `network.loopbackPorts`.
|
|
114
|
+
*/
|
|
115
|
+
loopbackPorts: boolean;
|
|
105
116
|
/** The layer that set each ceiling, or null when nothing narrowed it. */
|
|
106
117
|
layer: {
|
|
107
118
|
network: DeviceSandboxLayer | null;
|
|
@@ -127,6 +138,7 @@ export declare function orgHostsOf(platform: Pick<DeviceSandboxPlatformLayer, "e
|
|
|
127
138
|
* - Allowlist hosts are the org's effective hosts, narrowed by the device's own list: a device list
|
|
128
139
|
* never adds a host, and an empty one adds no narrowing.
|
|
129
140
|
* - `perCallHosts`: only an operation-boundary provider that reports `perOperationHosts`.
|
|
141
|
+
* - `loopbackPorts`: only with `outsideSandbox` and a provider that reports `loopbackPorts`.
|
|
130
142
|
*/
|
|
131
143
|
export declare function effectiveDeviceSandbox(platform: DeviceSandboxPlatformLayer, org: DeviceSandboxOrgLayer, device: DeviceSandboxDeviceLayer, provider: DeviceSandboxProviderLayer): DeviceSandbox;
|
|
132
144
|
/**
|
|
@@ -40,6 +40,7 @@ export function orgHostsOf(platform, org) {
|
|
|
40
40
|
* - Allowlist hosts are the org's effective hosts, narrowed by the device's own list: a device list
|
|
41
41
|
* never adds a host, and an empty one adds no narrowing.
|
|
42
42
|
* - `perCallHosts`: only an operation-boundary provider that reports `perOperationHosts`.
|
|
43
|
+
* - `loopbackPorts`: only with `outsideSandbox` and a provider that reports `loopbackPorts`.
|
|
43
44
|
*/
|
|
44
45
|
export function effectiveDeviceSandbox(platform, org, device, provider) {
|
|
45
46
|
const instance = provider.boundary === "instance";
|
|
@@ -104,6 +105,7 @@ export function effectiveDeviceSandbox(platform, org, device, provider) {
|
|
|
104
105
|
hosts,
|
|
105
106
|
outsideSandbox: outsideLayer === null,
|
|
106
107
|
perCallHosts: !instance && provider.network.perOperationHosts,
|
|
108
|
+
loopbackPorts: outsideLayer === null && provider.network.loopbackPorts === true,
|
|
107
109
|
layer: { network: networkLayer, outsideSandbox: outsideLayer },
|
|
108
110
|
};
|
|
109
111
|
}
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { type DeviceServiceSpec, type DeviceServiceStatus } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
3
|
+
import { LoopbackLivePorts } from "./loopback-live-ports.js";
|
|
4
|
+
import { PermissionFacts } from "./permission-facts.js";
|
|
2
5
|
import { SandboxService } from "./sandbox.service.js";
|
|
3
6
|
import type { SandboxUseContext } from "./use-context.js";
|
|
4
7
|
/** The budget of the `ifRunning` ensure after a refresh applied new limits (a service restart). */
|
|
5
8
|
export declare const POST_APPLY_ENSURE_TIMEOUT_MS = 30000;
|
|
9
|
+
/** The lifetime of the loopback set lock without renewal (renewed while an ensure runs). */
|
|
10
|
+
export declare const LOOPBACK_SET_LOCK_TTL_MS = 30000;
|
|
11
|
+
/** How long an ensure waits for another ensure of the same service to send its set. */
|
|
12
|
+
export declare const LOOPBACK_SET_LOCK_WAIT_MS = 60000;
|
|
6
13
|
/**
|
|
7
14
|
* Device services (browser use spec §5): one long-lived named process per device, started on an
|
|
8
15
|
* extension's behalf in the device's own sandbox with its own state directory, reached by
|
|
@@ -13,10 +20,32 @@ export declare const POST_APPLY_ENSURE_TIMEOUT_MS = 30000;
|
|
|
13
20
|
* launch and the extension's permission are the caller's (`ctx.deviceAccess`). Every name here is
|
|
14
21
|
* a service id (`<extension>.<name>`, `DEVICE_SERVICE_ID_RE`): the caller namespaces the
|
|
15
22
|
* extension's service names, so the provider never sees a bare name two extensions could share.
|
|
23
|
+
*
|
|
24
|
+
* Every ensure sends the provider the service's live loopback port grants (browser loopback port
|
|
25
|
+
* grants design §4: the whole sorted set), but only a non-empty set, and never while the device
|
|
26
|
+
* refuses loopback grants as far as is known ({@link knownLoopbackRefusal}: a ceiling keeps
|
|
27
|
+
* running outside the sandbox off, or a known support lacks `network.loopbackPorts`). Then
|
|
28
|
+
* `loopbackPorts` is left out, which closes every loopback port: a provider known to lack support
|
|
29
|
+
* never sees the key, and a ceiling switched off narrows on the next ensure, before the refresh
|
|
30
|
+
* job revokes the grants. A support not known (as while a device re-binds) keeps sending the set,
|
|
31
|
+
* as it keeps the grants; a provider without support ignores it.
|
|
32
|
+
* Reading the set and sending it are one step per (device, service) across replicas (a
|
|
33
|
+
* `LockService` lock taken outside any transaction): an ensure that read an older set can never
|
|
34
|
+
* reach the device after one that read a newer set.
|
|
16
35
|
*/
|
|
17
36
|
export declare class DeviceServices {
|
|
18
37
|
private readonly sandbox;
|
|
19
|
-
|
|
38
|
+
private readonly loopback;
|
|
39
|
+
private readonly locks;
|
|
40
|
+
private readonly facts;
|
|
41
|
+
constructor(sandbox: SandboxService, loopback: Pick<LoopbackLivePorts, "livePorts">, locks: Pick<LockService, "run">, facts: Pick<PermissionFacts, "deviceSandboxFacts">);
|
|
42
|
+
/**
|
|
43
|
+
* Runs `send` with the service's live loopback ports under the (device, service) set lock: the
|
|
44
|
+
* set is read after the lock is taken and the send finishes before it is released, so the
|
|
45
|
+
* sends reach the device in the order their sets were read. A lost lock aborts the send. The
|
|
46
|
+
* opts carry `loopbackPorts` only for a non-empty set the device sandbox admits (see the class).
|
|
47
|
+
*/
|
|
48
|
+
private withLoopbackSet;
|
|
20
49
|
/**
|
|
21
50
|
* Starts the service in the device, or finds it running (the provider keeps a running one).
|
|
22
51
|
* With `ifRunning` it never starts one: a stopped service answers its `stopped` status, and the
|
|
@@ -39,7 +68,18 @@ export declare class DeviceServices {
|
|
|
39
68
|
* without a running instance.
|
|
40
69
|
*/
|
|
41
70
|
refresh(use: SandboxUseContext, name: string, signal: AbortSignal): Promise<DeviceServiceStatus | null>;
|
|
42
|
-
/**
|
|
71
|
+
/**
|
|
72
|
+
* Sends the service's live loopback port set to the running service (a grant, a revoke, an
|
|
73
|
+
* expiry or a narrowing changed it): an `ifRunning` ensure only, no limits applied (the set is
|
|
74
|
+
* the service's own, not the device's). A provider restarts the service when its set changed and
|
|
75
|
+
* keeps it otherwise; a stopped service or instance needs nothing now (its next ensure sends the
|
|
76
|
+
* set). The status, or null without a running instance.
|
|
77
|
+
*/
|
|
78
|
+
refreshLoopback(orgId: string, deviceId: string, name: string, signal: AbortSignal): Promise<DeviceServiceStatus | null>;
|
|
79
|
+
/**
|
|
80
|
+
* An `ifRunning` ensure on the instance recorded now, with the service's live loopback ports;
|
|
81
|
+
* null without a running instance.
|
|
82
|
+
*/
|
|
43
83
|
private ifRunningEnsure;
|
|
44
84
|
/**
|
|
45
85
|
* One request to the service's socket; resolves its answer (throws when it is not running).
|
|
@@ -9,9 +9,17 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
9
9
|
};
|
|
10
10
|
import { DEVICE_SERVICE_ID_RE, DeviceAccessError, } from "@capybara-hq/plugin-sdk";
|
|
11
11
|
import { Inject, Injectable } from "@nestjs/common";
|
|
12
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
13
|
+
import { knownLoopbackRefusal } from "./loopback-grant-records.js";
|
|
14
|
+
import { LoopbackLivePorts } from "./loopback-live-ports.js";
|
|
15
|
+
import { PermissionFacts } from "./permission-facts.js";
|
|
12
16
|
import { SandboxService } from "./sandbox.service.js";
|
|
13
17
|
/** The budget of the `ifRunning` ensure after a refresh applied new limits (a service restart). */
|
|
14
18
|
export const POST_APPLY_ENSURE_TIMEOUT_MS = 30_000;
|
|
19
|
+
/** The lifetime of the loopback set lock without renewal (renewed while an ensure runs). */
|
|
20
|
+
export const LOOPBACK_SET_LOCK_TTL_MS = 30_000;
|
|
21
|
+
/** How long an ensure waits for another ensure of the same service to send its set. */
|
|
22
|
+
export const LOOPBACK_SET_LOCK_WAIT_MS = 60_000;
|
|
15
23
|
/**
|
|
16
24
|
* Device services (browser use spec §5): one long-lived named process per device, started on an
|
|
17
25
|
* extension's behalf in the device's own sandbox with its own state directory, reached by
|
|
@@ -22,11 +30,43 @@ export const POST_APPLY_ENSURE_TIMEOUT_MS = 30_000;
|
|
|
22
30
|
* launch and the extension's permission are the caller's (`ctx.deviceAccess`). Every name here is
|
|
23
31
|
* a service id (`<extension>.<name>`, `DEVICE_SERVICE_ID_RE`): the caller namespaces the
|
|
24
32
|
* extension's service names, so the provider never sees a bare name two extensions could share.
|
|
33
|
+
*
|
|
34
|
+
* Every ensure sends the provider the service's live loopback port grants (browser loopback port
|
|
35
|
+
* grants design §4: the whole sorted set), but only a non-empty set, and never while the device
|
|
36
|
+
* refuses loopback grants as far as is known ({@link knownLoopbackRefusal}: a ceiling keeps
|
|
37
|
+
* running outside the sandbox off, or a known support lacks `network.loopbackPorts`). Then
|
|
38
|
+
* `loopbackPorts` is left out, which closes every loopback port: a provider known to lack support
|
|
39
|
+
* never sees the key, and a ceiling switched off narrows on the next ensure, before the refresh
|
|
40
|
+
* job revokes the grants. A support not known (as while a device re-binds) keeps sending the set,
|
|
41
|
+
* as it keeps the grants; a provider without support ignores it.
|
|
42
|
+
* Reading the set and sending it are one step per (device, service) across replicas (a
|
|
43
|
+
* `LockService` lock taken outside any transaction): an ensure that read an older set can never
|
|
44
|
+
* reach the device after one that read a newer set.
|
|
25
45
|
*/
|
|
26
46
|
let DeviceServices = class DeviceServices {
|
|
27
47
|
sandbox;
|
|
28
|
-
|
|
48
|
+
loopback;
|
|
49
|
+
locks;
|
|
50
|
+
facts;
|
|
51
|
+
constructor(sandbox, loopback, locks, facts) {
|
|
29
52
|
this.sandbox = sandbox;
|
|
53
|
+
this.loopback = loopback;
|
|
54
|
+
this.locks = locks;
|
|
55
|
+
this.facts = facts;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Runs `send` with the service's live loopback ports under the (device, service) set lock: the
|
|
59
|
+
* set is read after the lock is taken and the send finishes before it is released, so the
|
|
60
|
+
* sends reach the device in the order their sets were read. A lost lock aborts the send. The
|
|
61
|
+
* opts carry `loopbackPorts` only for a non-empty set the device sandbox admits (see the class).
|
|
62
|
+
*/
|
|
63
|
+
withLoopbackSet(orgId, deviceId, name, signal, send) {
|
|
64
|
+
return this.locks.run(`loopback-set-${deviceId}-${name}`, { ttlMs: LOOPBACK_SET_LOCK_TTL_MS, waitMs: LOOPBACK_SET_LOCK_WAIT_MS, signal }, async (held) => {
|
|
65
|
+
const ports = await this.loopback.livePorts(orgId, deviceId, name);
|
|
66
|
+
const admitted = ports.length > 0 &&
|
|
67
|
+
knownLoopbackRefusal(await this.facts.deviceSandboxFacts(orgId, deviceId)) === null;
|
|
68
|
+
return send(admitted ? { loopbackPorts: ports } : {}, AbortSignal.any([signal, held.lost]));
|
|
69
|
+
});
|
|
30
70
|
}
|
|
31
71
|
/**
|
|
32
72
|
* Starts the service in the device, or finds it running (the provider keeps a running one).
|
|
@@ -36,11 +76,13 @@ let DeviceServices = class DeviceServices {
|
|
|
36
76
|
*/
|
|
37
77
|
async ensure(use, spec, signal, opts = {}) {
|
|
38
78
|
assertServiceSpec(spec);
|
|
39
|
-
return this.sandbox.withDeviceServices(use, requireServices, (h, p, sig) =>
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
79
|
+
return this.sandbox.withDeviceServices(use, requireServices, (h, p, sig) => {
|
|
80
|
+
const ensure = p.ensureService;
|
|
81
|
+
// read under the use and the set lock, so a grant committed before it is in the set
|
|
82
|
+
return this.withLoopbackSet(use.orgId, use.deviceId, spec.name, sig, (loopback, s) => opts.ifRunning === true
|
|
83
|
+
? ensure(h, nameOnlySpec(spec.name), s, { ifRunning: true, ...loopback })
|
|
84
|
+
: ensure(h, spec, s, loopback));
|
|
85
|
+
}, signal);
|
|
44
86
|
}
|
|
45
87
|
/**
|
|
46
88
|
* Applies the device's current limits to the service when it runs (the device's allowlist
|
|
@@ -56,23 +98,38 @@ let DeviceServices = class DeviceServices {
|
|
|
56
98
|
*/
|
|
57
99
|
async refresh(use, name, signal) {
|
|
58
100
|
assertServiceName(name);
|
|
59
|
-
const found = await this.ifRunningEnsure(use, name, signal);
|
|
101
|
+
const found = await this.ifRunningEnsure(use.orgId, use.deviceId, name, signal);
|
|
60
102
|
if (found === null || found.state !== "running")
|
|
61
103
|
return found;
|
|
62
104
|
if ((await this.sandbox.applyLimitsIfRunning(use, signal)) !== "applied")
|
|
63
105
|
return found;
|
|
64
106
|
// its own budget, never what the wait left of the caller's: a provider restarts the service
|
|
65
107
|
// on the new network here (device-local), so a spent caller signal must not skip it
|
|
66
|
-
return this.ifRunningEnsure(use, name, AbortSignal.timeout(POST_APPLY_ENSURE_TIMEOUT_MS));
|
|
108
|
+
return this.ifRunningEnsure(use.orgId, use.deviceId, name, AbortSignal.timeout(POST_APPLY_ENSURE_TIMEOUT_MS));
|
|
67
109
|
}
|
|
68
|
-
/**
|
|
69
|
-
|
|
70
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Sends the service's live loopback port set to the running service (a grant, a revoke, an
|
|
112
|
+
* expiry or a narrowing changed it): an `ifRunning` ensure only, no limits applied (the set is
|
|
113
|
+
* the service's own, not the device's). A provider restarts the service when its set changed and
|
|
114
|
+
* keeps it otherwise; a stopped service or instance needs nothing now (its next ensure sends the
|
|
115
|
+
* set). The status, or null without a running instance.
|
|
116
|
+
*/
|
|
117
|
+
async refreshLoopback(orgId, deviceId, name, signal) {
|
|
118
|
+
assertServiceName(name);
|
|
119
|
+
return this.ifRunningEnsure(orgId, deviceId, name, signal);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* An `ifRunning` ensure on the instance recorded now, with the service's live loopback ports;
|
|
123
|
+
* null without a running instance.
|
|
124
|
+
*/
|
|
125
|
+
async ifRunningEnsure(orgId, deviceId, name, signal) {
|
|
126
|
+
const current = await this.sandbox.currentInstance(deviceId);
|
|
71
127
|
if (!current)
|
|
72
128
|
return null;
|
|
73
|
-
const r = await this.sandbox.ifRunning(
|
|
129
|
+
const r = await this.sandbox.ifRunning(deviceId, { createdAt: current.createdAt }, async (p, h, sig) => {
|
|
74
130
|
requireServices(p);
|
|
75
|
-
|
|
131
|
+
const ensure = p.ensureService;
|
|
132
|
+
return this.withLoopbackSet(orgId, deviceId, name, sig, (loopback, s) => ensure(h, nameOnlySpec(name), s, { ifRunning: true, ...loopback }));
|
|
76
133
|
}, signal);
|
|
77
134
|
return r.kind === "ran" ? r.value : null;
|
|
78
135
|
}
|
|
@@ -145,7 +202,10 @@ let DeviceServices = class DeviceServices {
|
|
|
145
202
|
};
|
|
146
203
|
DeviceServices = __decorate([
|
|
147
204
|
Injectable(),
|
|
148
|
-
__param(0, Inject(SandboxService))
|
|
205
|
+
__param(0, Inject(SandboxService)),
|
|
206
|
+
__param(1, Inject(LoopbackLivePorts)),
|
|
207
|
+
__param(2, Inject(LockService)),
|
|
208
|
+
__param(3, Inject(PermissionFacts))
|
|
149
209
|
], DeviceServices);
|
|
150
210
|
export { DeviceServices };
|
|
151
211
|
/**
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
2
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
3
|
+
import type { KernelLogger } from "../log.js";
|
|
4
|
+
import { LoopbackGrants } from "./loopback-grants.js";
|
|
5
|
+
/** The expiry pass of loopback port grants (`@LockedInterval`, every {@link LOOPBACK_EXPIRY_MS}). */
|
|
6
|
+
export declare const LOOPBACK_EXPIRY_JOB = "device-loopback-expiry";
|
|
7
|
+
/** The pass's period. */
|
|
8
|
+
export declare const LOOPBACK_EXPIRY_MS = 60000;
|
|
9
|
+
/**
|
|
10
|
+
* Ends the loopback port grants past their 24 hours (browser loopback port grants design §4) and
|
|
11
|
+
* owes the refresh of each service that held one: once per tick across replicas
|
|
12
|
+
* (`@LockedInterval`), provided only by the worker role (`WorkerModule`).
|
|
13
|
+
*/
|
|
14
|
+
export declare class LoopbackExpiryJob {
|
|
15
|
+
private readonly grants;
|
|
16
|
+
readonly config: KernelConfigService;
|
|
17
|
+
readonly lockService: LockService;
|
|
18
|
+
readonly log: KernelLogger;
|
|
19
|
+
constructor(grants: Pick<LoopbackGrants, "expireDue">, config: KernelConfigService, lockService: LockService, log: KernelLogger);
|
|
20
|
+
expire(): Promise<void>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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 { Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
12
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
13
|
+
import { LockedInterval } from "../infra/schedule/locked-interval.js";
|
|
14
|
+
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
15
|
+
import { LoopbackGrants } from "./loopback-grants.js";
|
|
16
|
+
/** The expiry pass of loopback port grants (`@LockedInterval`, every {@link LOOPBACK_EXPIRY_MS}). */
|
|
17
|
+
export const LOOPBACK_EXPIRY_JOB = "device-loopback-expiry";
|
|
18
|
+
/** The pass's period. */
|
|
19
|
+
export const LOOPBACK_EXPIRY_MS = 60_000;
|
|
20
|
+
/**
|
|
21
|
+
* Ends the loopback port grants past their 24 hours (browser loopback port grants design §4) and
|
|
22
|
+
* owes the refresh of each service that held one: once per tick across replicas
|
|
23
|
+
* (`@LockedInterval`), provided only by the worker role (`WorkerModule`).
|
|
24
|
+
*/
|
|
25
|
+
let LoopbackExpiryJob = class LoopbackExpiryJob {
|
|
26
|
+
grants;
|
|
27
|
+
config;
|
|
28
|
+
lockService;
|
|
29
|
+
log;
|
|
30
|
+
constructor(grants, config, lockService, log) {
|
|
31
|
+
this.grants = grants;
|
|
32
|
+
this.config = config;
|
|
33
|
+
this.lockService = lockService;
|
|
34
|
+
this.log = log;
|
|
35
|
+
}
|
|
36
|
+
async expire() {
|
|
37
|
+
await this.grants.expireDue();
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
__decorate([
|
|
41
|
+
LockedInterval(LOOPBACK_EXPIRY_JOB, () => LOOPBACK_EXPIRY_MS)
|
|
42
|
+
], LoopbackExpiryJob.prototype, "expire", null);
|
|
43
|
+
LoopbackExpiryJob = __decorate([
|
|
44
|
+
Injectable(),
|
|
45
|
+
__param(0, Inject(LoopbackGrants)),
|
|
46
|
+
__param(1, Inject(KernelConfigService)),
|
|
47
|
+
__param(2, Inject(LockService)),
|
|
48
|
+
__param(3, Inject(KERNEL_LOGGER))
|
|
49
|
+
], LoopbackExpiryJob);
|
|
50
|
+
export { LoopbackExpiryJob };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { DeviceAccessError, type LoopbackGrantSource, type SandboxPermissionSupport } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { type Selectable } from "kysely";
|
|
3
|
+
import type { DeviceLoopbackGrantsTable, KernelSchemaDb } from "../db/kernel-schema.js";
|
|
4
|
+
import type { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
5
|
+
import type { DeviceSandbox } from "./device-sandbox.js";
|
|
6
|
+
/**
|
|
7
|
+
* Loopback port grants (browser loopback port grants design §4, §6): one device service of a
|
|
8
|
+
* person's own device may reach one loopback port of that device for one conversation tree. The
|
|
9
|
+
* records in `device_loopback_grants` are the live set; every change to it (a grant, a revoke, an
|
|
10
|
+
* expiry, a conversation's deletion, a narrowing ceiling) owes a `device-loopback-refresh` job per
|
|
11
|
+
* affected (device, service), which sends the whole live set to an `ifRunning` ensure of the
|
|
12
|
+
* service. A ceiling narrows by revocation: the refresh job revokes (`outside-sandbox-off`) when
|
|
13
|
+
* the device refuses loopback grants now.
|
|
14
|
+
*/
|
|
15
|
+
/** A grant's lifetime from issue. */
|
|
16
|
+
export declare const LOOPBACK_GRANT_TTL_HOURS = 24;
|
|
17
|
+
/** The refresh of one (device, service)'s live set (outbox job on the maintenance queue). */
|
|
18
|
+
export declare const LOOPBACK_REFRESH_JOB = "device-loopback-refresh";
|
|
19
|
+
/** The data of {@link LOOPBACK_REFRESH_JOB}: `orgId` gives the job its org scope (`runJob`). */
|
|
20
|
+
export interface LoopbackRefreshJobData {
|
|
21
|
+
orgId: string;
|
|
22
|
+
deviceId: string;
|
|
23
|
+
serviceId: string;
|
|
24
|
+
/** The change that owed it (the job id's last part), so a follow-up names its own id. */
|
|
25
|
+
change?: string;
|
|
26
|
+
/** The follow-up number after failed sends (absent for the first job). */
|
|
27
|
+
followUp?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The most follow-ups one change owes after failed sends, about a day at
|
|
31
|
+
* {@link LOOPBACK_FOLLOW_UP_MAX_DELAY_MS}: a device the kernel records as running keeps getting
|
|
32
|
+
* the live set until one send lands, so a narrowing never stays on the machine because a send
|
|
33
|
+
* failed.
|
|
34
|
+
*/
|
|
35
|
+
export declare const LOOPBACK_MAX_FOLLOW_UPS = 300;
|
|
36
|
+
/** The first follow-up's delay; it doubles up to {@link LOOPBACK_FOLLOW_UP_MAX_DELAY_MS}. */
|
|
37
|
+
export declare const LOOPBACK_FOLLOW_UP_DELAY_MS = 30000;
|
|
38
|
+
export declare const LOOPBACK_FOLLOW_UP_MAX_DELAY_MS = 300000;
|
|
39
|
+
/**
|
|
40
|
+
* Owes the follow-up of a refresh whose send failed (`-t<n>` after its change), delayed with a
|
|
41
|
+
* doubling backoff; false past {@link LOOPBACK_MAX_FOLLOW_UPS}.
|
|
42
|
+
*/
|
|
43
|
+
export declare function oweLoopbackFollowUp(outbox: Pick<OutboxService, "add">, data: LoopbackRefreshJobData): Promise<boolean>;
|
|
44
|
+
/**
|
|
45
|
+
* The deterministic job id of a refresh (no `:`). `change` names what owed it: `g<grantId>` (a
|
|
46
|
+
* grant), `r<grantId>` (a revoke, an expiry or a deletion), `c<level>-<subjectId>-<epoch>` (a
|
|
47
|
+
* ceiling publication, the epoch after its bump).
|
|
48
|
+
*/
|
|
49
|
+
export declare const loopbackRefreshJobId: (deviceId: string, serviceId: string, change: string) => string;
|
|
50
|
+
/** The budget of one refresh job's device calls. */
|
|
51
|
+
export declare const LOOPBACK_REFRESH_TIMEOUT_MS = 30000;
|
|
52
|
+
/** What names one grant: the root conversation holds it, sub-agents use their root's. */
|
|
53
|
+
export interface LoopbackGrantKey {
|
|
54
|
+
orgId: string;
|
|
55
|
+
deviceId: string;
|
|
56
|
+
/** `<extension>.<name>`. */
|
|
57
|
+
serviceId: string;
|
|
58
|
+
rootConversationId: string;
|
|
59
|
+
port: number;
|
|
60
|
+
}
|
|
61
|
+
/** One grant record as the kernel's callers read it. */
|
|
62
|
+
export interface LoopbackGrantRow {
|
|
63
|
+
id: string;
|
|
64
|
+
port: number;
|
|
65
|
+
source: LoopbackGrantSource;
|
|
66
|
+
/** ISO 8601. */
|
|
67
|
+
expiresAt: string;
|
|
68
|
+
runId: string | null;
|
|
69
|
+
callId: string | null;
|
|
70
|
+
attempt: number | null;
|
|
71
|
+
}
|
|
72
|
+
/** Why a grant ended (`device_loopback_grants.revoke_reason`). */
|
|
73
|
+
export type LoopbackRevokeReason = "revoked" | "expired" | "conversation-deleted" | "outside-sandbox-off";
|
|
74
|
+
/** A grant past {@link LOOPBACK_MAX_PORTS_PER_SERVICE} live ports of one (device, service). */
|
|
75
|
+
export declare class LoopbackLimitError extends DeviceAccessError {
|
|
76
|
+
constructor();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Why a loopback grant is refused on this device now, in the note's words; null when grantable.
|
|
80
|
+
* `escalationRefusal` is why a call could not run outside the sandbox here (the ceiling, an
|
|
81
|
+
* unattended run), null or undefined when it could.
|
|
82
|
+
*/
|
|
83
|
+
export declare function loopbackRefusal(box: DeviceSandbox, support: SandboxPermissionSupport | null, escalationRefusal: string | null | undefined): string | null;
|
|
84
|
+
/**
|
|
85
|
+
* Whether the device refuses loopback grants now, as far as that is known: a ceiling layer keeps
|
|
86
|
+
* running outside the sandbox off (`ceiling`), or a known, usable support lacks loopback ports
|
|
87
|
+
* (`provider-support`: no `network.loopbackPorts`, an old capybara-device, or nothing run outside
|
|
88
|
+
* the sandbox). A support not known (none stored, a failed read, or a closed one naming an
|
|
89
|
+
* `unavailableReason`, as while a device re-binds) refuses nothing: grants are consents and are
|
|
90
|
+
* kept until the support is known again. Null when nothing known refuses.
|
|
91
|
+
*/
|
|
92
|
+
export declare function knownLoopbackRefusal(facts: {
|
|
93
|
+
box: DeviceSandbox;
|
|
94
|
+
support: SandboxPermissionSupport | null;
|
|
95
|
+
}): "ceiling" | "provider-support" | null;
|
|
96
|
+
export declare const rowOf: (r: Selectable<DeviceLoopbackGrantsTable>) => LoopbackGrantRow;
|
|
97
|
+
export declare const isDatabasePort: (port: number) => boolean;
|
|
98
|
+
/** Owes the refresh of one (device, service). */
|
|
99
|
+
export declare function oweRefresh(outbox: Pick<OutboxService, "add">, target: LoopbackRefreshJobData, change: string): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Owes one refresh per (device, service) with live grants under the subject, in the caller's
|
|
102
|
+
* transaction (a ceiling publication, `PermissionCeilingWrites.publishLocked`): every org at the
|
|
103
|
+
* platform level, the org's, the device's, or the root conversation's grants. Returns how many it
|
|
104
|
+
* owed. The job decides whether the ceiling now refuses and revokes then.
|
|
105
|
+
*/
|
|
106
|
+
export declare function oweLoopbackRefreshes(db: KernelSchemaDb, outbox: Pick<OutboxService, "add">, under: {
|
|
107
|
+
level: "platform" | "org" | "device" | "root";
|
|
108
|
+
orgId?: string;
|
|
109
|
+
deviceId?: string;
|
|
110
|
+
rootConversationId?: string;
|
|
111
|
+
}, change: string): Promise<number>;
|
|
112
|
+
/**
|
|
113
|
+
* Revokes a deleted device's live grants (reason `revoked`, audited with `cause: "device-deleted"`)
|
|
114
|
+
* and owes their refreshes, in the caller's transaction (the device's deletion or its owner's
|
|
115
|
+
* offboarding).
|
|
116
|
+
*/
|
|
117
|
+
export declare function revokeLoopbackGrantsOfDevice(db: KernelSchemaDb, outbox: Pick<OutboxService, "add">, target: {
|
|
118
|
+
orgId: string;
|
|
119
|
+
deviceId: string;
|
|
120
|
+
}): Promise<void>;
|
|
121
|
+
/**
|
|
122
|
+
* Revokes a deleted root's live grants (reason `conversation-deleted`, audited) and owes their
|
|
123
|
+
* refreshes, in the caller's transaction (the conversation's deletion). A sub-agent's
|
|
124
|
+
* conversation holds no grant, so its id finds none.
|
|
125
|
+
*/
|
|
126
|
+
export declare function revokeLoopbackGrantsOfConversation(db: KernelSchemaDb, outbox: Pick<OutboxService, "add">, target: {
|
|
127
|
+
orgId: string;
|
|
128
|
+
rootConversationId: string;
|
|
129
|
+
}): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Audits ended grants (`device.loopback_revoked`) and owes one refresh per (device, service),
|
|
132
|
+
* named by its first grant.
|
|
133
|
+
*/
|
|
134
|
+
export declare function endedGrants(db: KernelSchemaDb, outbox: Pick<OutboxService, "add">, rows: readonly {
|
|
135
|
+
id: string;
|
|
136
|
+
org_id: string;
|
|
137
|
+
device_id: string;
|
|
138
|
+
service_id: string;
|
|
139
|
+
conversation_id: string;
|
|
140
|
+
port: number;
|
|
141
|
+
}[], reason: LoopbackRevokeReason, actorAccountId: string | null,
|
|
142
|
+
/** What ended them when the reason alone does not say (`device-deleted`, `provider-support`). */
|
|
143
|
+
cause?: string): Promise<void>;
|