@capybara-hq/kernel 0.1.0-rc.41 → 0.1.0-rc.43
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 +3 -3
- package/dist/admin/admin.schemas.d.ts +6 -0
- package/dist/admin/org-network.service.d.ts +3 -1
- package/dist/admin/org-network.service.js +10 -2
- package/dist/app.d.ts +4 -0
- package/dist/app.js +2 -0
- package/dist/background/finish.d.ts +9 -1
- package/dist/background/finish.js +57 -4
- package/dist/background/launch.d.ts +23 -0
- package/dist/background/launch.js +55 -1
- package/dist/background/managed-output.d.ts +110 -0
- package/dist/background/managed-output.js +219 -0
- package/dist/background/managed.d.ts +52 -0
- package/dist/background/managed.js +128 -0
- package/dist/background/poll.job.d.ts +35 -1
- package/dist/background/poll.job.js +174 -9
- package/dist/background/render.js +1 -0
- package/dist/background/retire-legacy-tasks.job.d.ts +57 -0
- package/dist/background/retire-legacy-tasks.job.js +149 -0
- package/dist/background/session.d.ts +15 -5
- package/dist/background/session.js +22 -6
- package/dist/background/stop.d.ts +35 -5
- package/dist/background/stop.js +94 -8
- package/dist/background/store.d.ts +110 -3
- package/dist/background/store.js +205 -3
- package/dist/background/tasks.service.d.ts +18 -1
- package/dist/background/tasks.service.js +99 -9
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/channels/cards.js +1 -1
- package/dist/channels/inbound.js +2 -0
- package/dist/channels/phrases.d.ts +4 -0
- package/dist/channels/phrases.js +4 -0
- package/dist/config/load.d.ts +18 -0
- package/dist/config/load.js +39 -1
- package/dist/config/permission-migration.d.ts +26 -0
- package/dist/config/permission-migration.js +64 -0
- package/dist/config/schema.d.ts +4 -1
- package/dist/config/schema.js +41 -10
- package/dist/conversations/conversations.controller.d.ts +1 -1
- package/dist/conversations/conversations.controller.js +1 -1
- package/dist/conversations/conversations.module.js +12 -0
- package/dist/conversations/conversations.service.d.ts +11 -2
- package/dist/conversations/conversations.service.js +97 -27
- package/dist/conversations/event-log.d.ts +1 -1
- package/dist/conversations/event-log.js +10 -2
- package/dist/conversations/events.d.ts +307 -4
- package/dist/conversations/events.js +286 -32
- package/dist/conversations/permission-profile-store.d.ts +50 -0
- package/dist/conversations/permission-profile-store.js +67 -0
- package/dist/conversations/permission-profile-store.module.d.ts +2 -0
- package/dist/conversations/permission-profile-store.module.js +19 -0
- package/dist/conversations/permission-profiles.controller.d.ts +84 -0
- package/dist/conversations/permission-profiles.controller.js +105 -0
- package/dist/conversations/permission-profiles.schemas.d.ts +31 -0
- package/dist/conversations/permission-profiles.schemas.js +23 -0
- package/dist/conversations/permission-profiles.service.d.ts +56 -0
- package/dist/conversations/permission-profiles.service.js +179 -0
- package/dist/ctx/create-ctx.d.ts +12 -5
- package/dist/ctx/create-ctx.js +21 -5
- package/dist/db/kernel-schema.d.ts +248 -3
- package/dist/devices/access.js +1 -1
- package/dist/devices/access.module.d.ts +2 -0
- package/dist/devices/access.module.js +19 -0
- package/dist/devices/conversation-devices.d.ts +8 -29
- package/dist/devices/conversation-devices.js +37 -73
- package/dist/devices/device-projects.service.d.ts +26 -28
- package/dist/devices/device-projects.service.js +123 -127
- package/dist/devices/devices.controller.js +3 -1
- package/dist/devices/devices.module.js +11 -9
- package/dist/devices/devices.service.d.ts +116 -8
- package/dist/devices/devices.service.js +252 -20
- package/dist/devices/dto.d.ts +16 -0
- package/dist/devices/dto.js +34 -6
- package/dist/devices/errors.d.ts +8 -0
- package/dist/devices/errors.js +10 -0
- package/dist/devices/project-rules.d.ts +44 -0
- package/dist/devices/project-rules.js +27 -0
- package/dist/devices/working-root.d.ts +6 -0
- package/dist/devices/working-root.js +12 -1
- package/dist/errors.d.ts +31 -0
- package/dist/errors.js +31 -0
- package/dist/evals/reconstruct.d.ts +19 -2
- package/dist/evals/reconstruct.js +40 -2
- package/dist/evals/recorded-permission-context.d.ts +77 -0
- package/dist/evals/recorded-permission-context.js +151 -0
- package/dist/evals/recorded-tools.d.ts +10 -1
- package/dist/evals/recorded-tools.js +27 -3
- package/dist/evals/run-replay.d.ts +8 -2
- package/dist/evals/run-replay.js +18 -2
- package/dist/evals/trajectory.d.ts +1 -1
- package/dist/evals/worker.d.ts +10 -1
- package/dist/evals/worker.js +23 -14
- package/dist/extensions/host.d.ts +4 -1
- package/dist/extensions/host.js +10 -6
- package/dist/http/api.module.js +2 -0
- package/dist/identity/accounts.d.ts +14 -1
- package/dist/identity/accounts.js +61 -5
- package/dist/identity/identity.module.js +2 -0
- package/dist/identity/settings.d.ts +28 -12
- package/dist/identity/settings.js +89 -33
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/infra/cli/kernel-cli.module.d.ts +2 -2
- package/dist/infra/cli/kernel-cli.module.js +5 -2
- package/dist/infra/cli/permission-cutover.d.ts +59 -0
- package/dist/infra/cli/permission-cutover.js +107 -0
- package/dist/infra/config/kernel-config.service.d.ts +2 -2
- package/dist/infra/config/kernel-config.service.js +35 -11
- package/dist/infra/database/startup-activation-transaction.d.ts +36 -0
- package/dist/infra/database/startup-activation-transaction.js +242 -0
- package/dist/infra/database/transaction-host.d.ts +47 -0
- package/dist/infra/database/transaction-host.js +119 -0
- package/dist/infra/database/xact-lock.d.ts +1 -0
- package/dist/infra/database/xact-lock.js +6 -0
- package/dist/infra/outbox/registry.d.ts +39 -0
- package/dist/infra/outbox/registry.js +18 -0
- package/dist/kernel.module.js +2 -0
- package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
- package/dist/lifecycle/kernel-lifecycle.js +55 -28
- package/dist/local-devices/device-auth.js +1 -1
- package/dist/local-devices/device-policy.service.d.ts +35 -5
- package/dist/local-devices/device-policy.service.js +188 -13
- package/dist/local-devices/instance-bindings.d.ts +82 -0
- package/dist/local-devices/instance-bindings.js +304 -0
- package/dist/local-devices/keys.d.ts +5 -0
- package/dist/local-devices/keys.js +5 -0
- package/dist/local-devices/local-device-gateway.d.ts +19 -4
- package/dist/local-devices/local-device-gateway.js +185 -17
- package/dist/local-devices/local-device-provider.d.ts +42 -7
- package/dist/local-devices/local-device-provider.js +185 -10
- package/dist/local-devices/local-devices.module.d.ts +17 -2
- package/dist/local-devices/local-devices.module.js +49 -8
- package/dist/local-devices/machines.d.ts +35 -12
- package/dist/local-devices/machines.js +203 -8
- package/dist/local-devices/network-grants.js +3 -3
- package/dist/local-devices/network-grants.module.d.ts +2 -0
- package/dist/local-devices/network-grants.module.js +14 -0
- package/dist/local-devices/owner-report.d.ts +79 -0
- package/dist/local-devices/owner-report.js +155 -0
- package/dist/local-devices/owner-reports.d.ts +48 -0
- package/dist/local-devices/owner-reports.js +190 -0
- package/dist/local-devices/policy-summary.d.ts +24 -1
- package/dist/local-devices/policy-summary.js +48 -0
- package/dist/local-devices/relay.d.ts +82 -6
- package/dist/local-devices/relay.js +187 -16
- package/dist/local-devices/targets.d.ts +16 -1
- package/dist/local-devices/targets.js +35 -2
- package/dist/models/image-usable.d.ts +22 -0
- package/dist/models/image-usable.js +29 -0
- package/dist/models/images.d.ts +125 -0
- package/dist/models/images.js +440 -0
- package/dist/models/models.module.d.ts +3 -2
- package/dist/models/models.module.js +16 -5
- package/dist/models/org-models.schemas.js +1 -0
- package/dist/models/purposes.controller.d.ts +7 -0
- package/dist/models/purposes.controller.js +9 -0
- package/dist/models/purposes.d.ts +28 -3
- package/dist/models/purposes.js +40 -13
- package/dist/orgs/cloud-network-setting.d.ts +33 -0
- package/dist/orgs/cloud-network-setting.js +47 -0
- package/dist/orgs/offboarding.d.ts +3 -1
- package/dist/orgs/offboarding.js +8 -2
- package/dist/orgs/org-lock.d.ts +10 -4
- package/dist/orgs/org-lock.js +10 -4
- package/dist/orgs/orgs.service.d.ts +3 -1
- package/dist/orgs/orgs.service.js +8 -2
- package/dist/orgs/settings.controller.d.ts +4 -1
- package/dist/orgs/settings.controller.js +3 -3
- package/dist/orgs/settings.service.d.ts +48 -56
- package/dist/orgs/settings.service.js +144 -101
- package/dist/overrides.d.ts +6 -1
- package/dist/overrides.js +2 -0
- package/dist/platform/exclusions.js +20 -0
- package/dist/platform/ops/admin.ops.js +14 -11
- package/dist/platform/ops/agent-skills.ops.js +2 -0
- package/dist/platform/ops/devices.ops.js +10 -2
- package/dist/platform/ops/models.ops.js +10 -0
- package/dist/platform/ops/org-settings.ops.js +6 -6
- package/dist/platform/ops/orgs.ops.js +1 -0
- package/dist/roles/worker.module.js +4 -0
- package/dist/runtime/context/build.d.ts +3 -1
- package/dist/runtime/context/build.js +19 -3
- package/dist/runtime/context/compaction.js +5 -0
- package/dist/runtime/context/device-paths.d.ts +9 -2
- package/dist/runtime/context/device-paths.js +20 -0
- package/dist/runtime/context/permission-profile.d.ts +46 -0
- package/dist/runtime/context/permission-profile.js +194 -0
- package/dist/runtime/hook-calls.d.ts +11 -1
- package/dist/runtime/hook-calls.js +24 -1
- package/dist/runtime/maintenance.d.ts +7 -1
- package/dist/runtime/maintenance.js +43 -15
- package/dist/runtime/model-call.d.ts +32 -0
- package/dist/runtime/model-call.js +67 -0
- package/dist/runtime/permission-context-errors.d.ts +39 -0
- package/dist/runtime/permission-context-errors.js +67 -0
- package/dist/runtime/permission-context.service.d.ts +60 -0
- package/dist/runtime/permission-context.service.js +286 -0
- package/dist/runtime/project/knowledge.d.ts +7 -2
- package/dist/runtime/project/knowledge.js +5 -2
- package/dist/runtime/project/knowledge.service.d.ts +17 -2
- package/dist/runtime/project/knowledge.service.js +76 -12
- package/dist/runtime/providers.d.ts +9 -5
- package/dist/runtime/providers.js +22 -3
- package/dist/runtime/run-execution.d.ts +4 -1
- package/dist/runtime/run-execution.js +14 -5
- package/dist/runtime/run-steps.d.ts +11 -3
- package/dist/runtime/run-steps.js +27 -4
- package/dist/runtime/runtime.module.js +11 -0
- package/dist/runtime/usage-report.d.ts +10 -6
- package/dist/runtime/usage-report.js +31 -11
- package/dist/sandbox/attachments-path.d.ts +15 -0
- package/dist/sandbox/attachments-path.js +15 -0
- package/dist/sandbox/cloud-permission-snapshot.d.ts +159 -0
- package/dist/sandbox/cloud-permission-snapshot.js +407 -0
- package/dist/sandbox/cloud-permission-snapshot.module.d.ts +8 -0
- package/dist/sandbox/cloud-permission-snapshot.module.js +34 -0
- package/dist/sandbox/held-operations.d.ts +39 -0
- package/dist/sandbox/held-operations.js +119 -0
- package/dist/sandbox/jobs.d.ts +14 -3
- package/dist/sandbox/jobs.js +20 -2
- package/dist/sandbox/kernel-read.d.ts +55 -0
- package/dist/sandbox/kernel-read.js +57 -0
- package/dist/sandbox/legacy-retirement.d.ts +336 -0
- package/dist/sandbox/legacy-retirement.js +1063 -0
- package/dist/sandbox/legacy-retirement.module.d.ts +8 -0
- package/dist/sandbox/legacy-retirement.module.js +25 -0
- package/dist/sandbox/managed-device-context.d.ts +94 -0
- package/dist/sandbox/managed-device-context.js +308 -0
- package/dist/sandbox/managed-device-context.module.d.ts +6 -0
- package/dist/sandbox/managed-device-context.module.js +33 -0
- package/dist/sandbox/modes.d.ts +56 -8
- package/dist/sandbox/modes.js +92 -8
- package/dist/sandbox/operation-authority-types.d.ts +8 -0
- package/dist/sandbox/operation-authority-types.js +32 -0
- package/dist/sandbox/operation-authority.d.ts +145 -0
- package/dist/sandbox/operation-authority.js +783 -0
- package/dist/sandbox/operation-authority.module.d.ts +2 -0
- package/dist/sandbox/operation-authority.module.js +29 -0
- package/dist/sandbox/operation-intents.d.ts +206 -0
- package/dist/sandbox/operation-intents.js +1252 -0
- package/dist/sandbox/operation-intents.module.d.ts +2 -0
- package/dist/sandbox/operation-intents.module.js +21 -0
- package/dist/sandbox/operation-payloads.d.ts +33 -0
- package/dist/sandbox/operation-payloads.js +332 -0
- package/dist/sandbox/operation-reconcile.job.d.ts +49 -0
- package/dist/sandbox/operation-reconcile.job.js +247 -0
- package/dist/sandbox/permission-authority-epochs.d.ts +18 -0
- package/dist/sandbox/permission-authority-epochs.js +83 -0
- package/dist/sandbox/permission-authority-lock.d.ts +38 -0
- package/dist/sandbox/permission-authority-lock.js +147 -0
- package/dist/sandbox/permission-authority.module.d.ts +9 -0
- package/dist/sandbox/permission-authority.module.js +55 -0
- package/dist/sandbox/permission-availability.d.ts +18 -0
- package/dist/sandbox/permission-availability.js +19 -0
- package/dist/sandbox/permission-call-grants.d.ts +34 -0
- package/dist/sandbox/permission-call-grants.js +349 -0
- package/dist/sandbox/permission-call-grants.module.d.ts +2 -0
- package/dist/sandbox/permission-call-grants.module.js +21 -0
- package/dist/sandbox/permission-call-types.d.ts +82 -0
- package/dist/sandbox/permission-call-types.js +1 -0
- package/dist/sandbox/permission-ceiling-writes.d.ts +47 -0
- package/dist/sandbox/permission-ceiling-writes.js +223 -0
- package/dist/sandbox/permission-config-publication.d.ts +76 -0
- package/dist/sandbox/permission-config-publication.js +339 -0
- package/dist/sandbox/permission-context-types.d.ts +64 -0
- package/dist/sandbox/permission-context-types.js +24 -0
- package/dist/sandbox/permission-facts.d.ts +206 -0
- package/dist/sandbox/permission-facts.js +627 -0
- package/dist/sandbox/permission-facts.module.d.ts +2 -0
- package/dist/sandbox/permission-facts.module.js +20 -0
- package/dist/sandbox/permission-instance-writes.d.ts +90 -0
- package/dist/sandbox/permission-instance-writes.js +305 -0
- package/dist/sandbox/permission-runtime-reports.d.ts +83 -0
- package/dist/sandbox/permission-runtime-reports.js +354 -0
- package/dist/sandbox/permission-transitions.d.ts +31 -0
- package/dist/sandbox/permission-transitions.js +155 -0
- package/dist/sandbox/platform-full-access.d.ts +128 -0
- package/dist/sandbox/platform-full-access.js +210 -0
- package/dist/sandbox/providers.d.ts +22 -0
- package/dist/sandbox/providers.js +10 -2
- package/dist/sandbox/providers.module.d.ts +2 -0
- package/dist/sandbox/providers.module.js +19 -0
- package/dist/sandbox/request-not-sent.d.ts +13 -0
- package/dist/sandbox/request-not-sent.js +13 -0
- package/dist/sandbox/review/classifier.d.ts +2 -0
- package/dist/sandbox/review/classifier.js +8 -0
- package/dist/sandbox/review/permission-candidate.d.ts +12 -0
- package/dist/sandbox/review/permission-candidate.js +145 -0
- package/dist/sandbox/review/review.service.d.ts +5 -2
- package/dist/sandbox/review/review.service.js +60 -15
- package/dist/sandbox/sandbox-file-reader.d.ts +18 -1
- package/dist/sandbox/sandbox-file-reader.js +51 -7
- package/dist/sandbox/sandbox.module.js +26 -6
- package/dist/sandbox/sandbox.service.d.ts +230 -34
- package/dist/sandbox/sandbox.service.js +1368 -199
- package/dist/sandbox/unreachable.js +17 -3
- package/dist/sandbox/use-context.d.ts +17 -0
- package/dist/sandbox/use-context.js +1 -0
- package/dist/skills/invoke.d.ts +27 -4
- package/dist/skills/invoke.js +72 -7
- package/dist/tools/approvals.controller.d.ts +1 -0
- package/dist/tools/approvals.schemas.d.ts +1 -0
- package/dist/tools/approvals.schemas.js +3 -1
- package/dist/tools/approvals.service.d.ts +23 -10
- package/dist/tools/approvals.service.js +104 -26
- package/dist/tools/builtin-names.d.ts +3 -1
- package/dist/tools/builtin-names.js +5 -1
- package/dist/tools/builtins/artifacts.d.ts +43 -0
- package/dist/tools/builtins/artifacts.js +67 -0
- package/dist/tools/builtins/builtin-tools.d.ts +20 -2
- package/dist/tools/builtins/builtin-tools.js +61 -10
- package/dist/tools/builtins/image/generate.d.ts +59 -0
- package/dist/tools/builtins/image/generate.js +535 -0
- package/dist/tools/builtins/image/index.d.ts +2 -0
- package/dist/tools/builtins/image/index.js +2 -0
- package/dist/tools/builtins/image/paths.d.ts +12 -0
- package/dist/tools/builtins/image/paths.js +26 -0
- package/dist/tools/builtins/sandbox/bash.d.ts +25 -1
- package/dist/tools/builtins/sandbox/bash.js +201 -5
- package/dist/tools/builtins/sandbox/common.d.ts +92 -20
- package/dist/tools/builtins/sandbox/common.js +201 -17
- package/dist/tools/builtins/sandbox/deliver.js +9 -51
- package/dist/tools/builtins/sandbox/edit.js +2 -2
- package/dist/tools/builtins/sandbox/fetch-attachment.js +11 -4
- package/dist/tools/builtins/sandbox/glob.js +2 -2
- package/dist/tools/builtins/sandbox/grep.js +2 -2
- package/dist/tools/builtins/sandbox/read.js +2 -2
- package/dist/tools/builtins/sandbox/task-list.js +7 -1
- package/dist/tools/builtins/sandbox/task-output.d.ts +1 -1
- package/dist/tools/builtins/sandbox/task-output.js +172 -0
- package/dist/tools/builtins/sandbox/task-stop.js +25 -3
- package/dist/tools/builtins/sandbox/write.js +2 -2
- package/dist/tools/builtins/skill-load.js +9 -6
- package/dist/tools/device-op-resolver.d.ts +1 -1
- package/dist/tools/device-op-resolver.js +13 -3
- package/dist/tools/permission-approval-jobs.d.ts +18 -0
- package/dist/tools/permission-approval-jobs.js +92 -0
- package/dist/tools/pipeline.d.ts +17 -2
- package/dist/tools/pipeline.js +315 -26
- package/dist/tools/registry.d.ts +9 -0
- package/dist/tools/tools.module.js +15 -0
- package/package.json +4 -3
- package/src/migrations/0146_permission_authority_epochs.sql +6 -0
- package/src/migrations/0147_conversation_permission_profiles.sql +4 -0
- package/src/migrations/0148_first_message_request_hash.sql +2 -0
- package/src/migrations/0149_sandbox_operation_intents.sql +66 -0
- package/src/migrations/0150_permission_call_grants_and_approvals.sql +36 -0
- package/src/migrations/0151_starting_execution_grants.sql +8 -0
- package/src/migrations/0152_background_launch_identity.sql +66 -0
- package/src/migrations/0153_background_cancel_requester.sql +13 -0
- package/src/migrations/0154_cloud_owner_ceiling.sql +14 -0
- package/src/migrations/0155_device_instance_generations.sql +30 -0
- package/src/migrations/0156_device_owner_report.sql +7 -0
- package/src/migrations/0157_legacy_retirement.sql +116 -0
- package/src/migrations/0158_legacy_freeze_enumerated.sql +5 -0
- package/src/migrations/0159_retirement_exact_cancel.sql +9 -0
- package/src/migrations/0160_model_usage_image_kind.sql +10 -0
- package/dist/sandbox/platform-auto-mode.d.ts +0 -63
- package/dist/sandbox/platform-auto-mode.js +0 -63
|
@@ -0,0 +1,627 @@
|
|
|
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 { createHash } from "node:crypto";
|
|
11
|
+
import { DEFAULT_SANDBOX_ROOTS, rootsOf, validateAllowHost, } from "@capybara-hq/plugin-sdk";
|
|
12
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
13
|
+
import { sql } from "kysely";
|
|
14
|
+
import { permissionCodePointCompare } from "../conversations/events.js";
|
|
15
|
+
import { DeviceAccess } from "../devices/access.js";
|
|
16
|
+
import { deviceNotFound, projectsUnsupported } from "../devices/errors.js";
|
|
17
|
+
import { outsideMode, projectNotFound, tooBroad, withinMode, } from "../devices/project-rules.js";
|
|
18
|
+
import { aboveHome } from "../devices/working-root.js";
|
|
19
|
+
import { ApiError } from "../errors.js";
|
|
20
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
21
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
22
|
+
import { ownerFactsOf, ownerModeOf } from "../local-devices/owner-report.js";
|
|
23
|
+
import { deviceFeaturesOf } from "../local-devices/policy-summary.js";
|
|
24
|
+
import { readOrgSettings } from "../orgs/settings.service.js";
|
|
25
|
+
import { appliedOf, appliedWider, cloudChosenMode, coversHost, effectiveDevicePolicy, readDeploymentDevicePolicy, readInstanceLimits, } from "./modes.js";
|
|
26
|
+
import { assertAuthorityHeld } from "./permission-authority-lock.js";
|
|
27
|
+
import { permissionAvailabilityFor } from "./permission-availability.js";
|
|
28
|
+
import { readCurrentReport, readRuntimeReport, sameRuntimeIdentity, } from "./permission-runtime-reports.js";
|
|
29
|
+
import { providerForDevice, SandboxProviders } from "./providers.js";
|
|
30
|
+
import { homeOf } from "./review/rules.js";
|
|
31
|
+
export const permissionScopeHash = (value) => createHash("sha256").update(canonical(value)).digest("hex");
|
|
32
|
+
function canonical(value) {
|
|
33
|
+
if (Array.isArray(value))
|
|
34
|
+
return `[${value.map(canonical).join(",")}]`;
|
|
35
|
+
if (value !== null && typeof value === "object")
|
|
36
|
+
return `{${Object.entries(value)
|
|
37
|
+
.filter(([, v]) => v !== undefined)
|
|
38
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
39
|
+
.map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`)
|
|
40
|
+
.join(",")}}`;
|
|
41
|
+
return JSON.stringify(value) ?? "null";
|
|
42
|
+
}
|
|
43
|
+
const notice = "Changes apply to new operations. Work already started keeps its starting permissions.";
|
|
44
|
+
/** Database and registered metadata reads only; never provisions or probes a device. */
|
|
45
|
+
let PermissionFacts = class PermissionFacts {
|
|
46
|
+
db;
|
|
47
|
+
config;
|
|
48
|
+
providers;
|
|
49
|
+
devices;
|
|
50
|
+
constructor(db, config, providers, devices) {
|
|
51
|
+
this.db = db;
|
|
52
|
+
this.config = config;
|
|
53
|
+
this.providers = providers;
|
|
54
|
+
this.devices = devices;
|
|
55
|
+
}
|
|
56
|
+
/** Current policy and persisted owner roots; access is checked by the caller. */
|
|
57
|
+
async readDevice(orgId, deviceId) {
|
|
58
|
+
const [policy, machine] = await Promise.all([
|
|
59
|
+
this.devicePolicy({ orgId, deviceId }),
|
|
60
|
+
this.db
|
|
61
|
+
.selectFrom("device_machines")
|
|
62
|
+
.select(["local_policy", "owner_report", "roots", "home"])
|
|
63
|
+
.where("device_id", "=", deviceId)
|
|
64
|
+
.where("org_id", "=", orgId)
|
|
65
|
+
.where("revoked_at", "is", null)
|
|
66
|
+
.executeTakeFirst(),
|
|
67
|
+
]);
|
|
68
|
+
const facts = machine ? ownerFactsOf(machine) : null;
|
|
69
|
+
return {
|
|
70
|
+
policy,
|
|
71
|
+
// the owner's authenticated facts (B2), or its protocol 2 summary translated exactly; null
|
|
72
|
+
// when the machine reported nothing readable
|
|
73
|
+
owner: facts === null || facts.state === "missing"
|
|
74
|
+
? null
|
|
75
|
+
: {
|
|
76
|
+
state: facts.state,
|
|
77
|
+
fullAccess: facts.fullAccess,
|
|
78
|
+
restrictedProjectReach: facts.restrictedProjectReach,
|
|
79
|
+
allowedDomains: facts.restrictedAllowedDomains,
|
|
80
|
+
},
|
|
81
|
+
roots: machine?.roots ?? null,
|
|
82
|
+
home: machine?.home ?? null,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Stored binding only; projectScope supplies the live owner and deletion checks. */
|
|
86
|
+
async readProject(orgId, deviceId, name) {
|
|
87
|
+
return ((await this.db
|
|
88
|
+
.selectFrom("device_projects as p")
|
|
89
|
+
.innerJoin("devices as d", "d.id", "p.device_id")
|
|
90
|
+
.select(["p.name", "p.path"])
|
|
91
|
+
.where("d.org_id", "=", orgId)
|
|
92
|
+
.where("p.device_id", "=", deviceId)
|
|
93
|
+
.where("p.name", "=", name)
|
|
94
|
+
.executeTakeFirst()) ?? null);
|
|
95
|
+
}
|
|
96
|
+
async devicePolicy(device) {
|
|
97
|
+
const [deployment, settings, chosen] = await Promise.all([
|
|
98
|
+
readDeploymentDevicePolicy(this.db.db, this.config),
|
|
99
|
+
readOrgSettings(this.db.db, device.orgId),
|
|
100
|
+
device.deviceId === undefined
|
|
101
|
+
? { mode: null, outer: null }
|
|
102
|
+
: this.deviceChosenMode(device.orgId, device.deviceId),
|
|
103
|
+
]);
|
|
104
|
+
return effectiveDevicePolicy(deployment, settings, chosen.mode, chosen.outer);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Refuses admission on a cloud device whose B1 snapshot found applied limits it could not
|
|
108
|
+
* establish exactly (unknown, stale, wider or pending a reset): only a lifecycle action that
|
|
109
|
+
* establishes them clears the marker (`CloudPermissionSnapshots.reconcile`). Read under the
|
|
110
|
+
* caller's device authority lock, which the snapshot writer takes for writing.
|
|
111
|
+
*/
|
|
112
|
+
async assertCloudReconciled(orgId, deviceId) {
|
|
113
|
+
const row = await this.db
|
|
114
|
+
.selectFrom("devices")
|
|
115
|
+
.select("permission_reconcile_required")
|
|
116
|
+
.where("id", "=", deviceId)
|
|
117
|
+
.where("org_id", "=", orgId)
|
|
118
|
+
.executeTakeFirst();
|
|
119
|
+
if (row?.permission_reconcile_required)
|
|
120
|
+
throw new ApiError("device.permission_reconcile_required", "This device's sandbox limits must be established again before it runs anything; reset the device's files to apply them");
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Refuses new authority on a local device whose machine regressed its owner report sequence on
|
|
124
|
+
* a new connection or sent conflicting reports (B2): only a valid current report or a new
|
|
125
|
+
* login clears it. Late deliveries of older reports are ignored, never blocking.
|
|
126
|
+
*/
|
|
127
|
+
async assertOwnerReportUsable(orgId, deviceId) {
|
|
128
|
+
const row = await this.db
|
|
129
|
+
.selectFrom("device_machines")
|
|
130
|
+
.select(["owner_report", "local_policy"])
|
|
131
|
+
.where("device_id", "=", deviceId)
|
|
132
|
+
.where("org_id", "=", orgId)
|
|
133
|
+
.where("revoked_at", "is", null)
|
|
134
|
+
.executeTakeFirst();
|
|
135
|
+
if (row && ownerFactsOf(row).state === "blocked")
|
|
136
|
+
throw new ApiError("device.owner_report_stale", "This device's machine reported an owner ceiling older than the one it reported before (its owner report sequence was lost or reset); run capybara-device login again on that machine to reset it");
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The mode a device chose: a local device's derived one (`localModeOf`, which never returns null
|
|
140
|
+
* — a machine with no summary or no mode feature is sandbox); a cloud device's narrowing switch
|
|
141
|
+
* (`devices.mode`, `sandbox` only), or null for a cloud device that follows the platform and the
|
|
142
|
+
* org.
|
|
143
|
+
*/
|
|
144
|
+
async deviceChosenMode(orgId, deviceId) {
|
|
145
|
+
const row = await this.db
|
|
146
|
+
.selectFrom("devices as e")
|
|
147
|
+
.leftJoin("device_machines as m", (j) => j.onRef("m.device_id", "=", "e.id").on("m.revoked_at", "is", null))
|
|
148
|
+
.select([
|
|
149
|
+
"e.source",
|
|
150
|
+
"e.mode",
|
|
151
|
+
"e.owner_full_access",
|
|
152
|
+
"e.outer_network",
|
|
153
|
+
"m.features",
|
|
154
|
+
"m.local_policy",
|
|
155
|
+
"m.owner_report",
|
|
156
|
+
])
|
|
157
|
+
.where("e.id", "=", deviceId)
|
|
158
|
+
.where("e.org_id", "=", orgId)
|
|
159
|
+
.where("e.deleted_at", "is", null)
|
|
160
|
+
.executeTakeFirst();
|
|
161
|
+
if (row === undefined)
|
|
162
|
+
return { mode: null, outer: null };
|
|
163
|
+
if (row.source === "local")
|
|
164
|
+
return { mode: ownerModeOf(row), outer: null };
|
|
165
|
+
// a cloud device: its B1 owner ceiling and outer network once snapshotted
|
|
166
|
+
return { mode: cloudChosenMode(row), outer: row.outer_network };
|
|
167
|
+
}
|
|
168
|
+
async readWorkingFacts({ orgId, rootConversationId, holderConversationId: conversationId, deviceId, }) {
|
|
169
|
+
const [recorded, recordedRoot, machine, project] = await Promise.all([
|
|
170
|
+
this.recordedWorkspace(conversationId),
|
|
171
|
+
conversationId === rootConversationId ? null : this.recordedWorkspace(rootConversationId),
|
|
172
|
+
this.machineRoots(deviceId),
|
|
173
|
+
this.workingProject(orgId, rootConversationId),
|
|
174
|
+
]);
|
|
175
|
+
const workspace = recorded ?? recordedRoot ?? machine?.workspace ?? DEFAULT_SANDBOX_ROOTS.workspace;
|
|
176
|
+
// never from a project root: the machine's home, else the device workspace's home prefix
|
|
177
|
+
const reported = machine?.home;
|
|
178
|
+
const home = reported?.startsWith("/") ? reported : homeOf(workspace);
|
|
179
|
+
return { workspace, home, project };
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* The project snapshot that decides the working root of the conversation tree rooted at
|
|
183
|
+
* `rootConversationId` (project root spec §4.3): its latest `project_root` event, read only when
|
|
184
|
+
* the conversation is bound to a project (a free conversation reads one row by its key). Null:
|
|
185
|
+
* free, or bound with no snapshot recorded yet.
|
|
186
|
+
*/
|
|
187
|
+
async workingProject(orgId, rootConversationId) {
|
|
188
|
+
const conv = await this.db
|
|
189
|
+
.selectFrom("conversations")
|
|
190
|
+
.select("project_path")
|
|
191
|
+
.where("id", "=", rootConversationId)
|
|
192
|
+
.where("org_id", "=", orgId)
|
|
193
|
+
.executeTakeFirst();
|
|
194
|
+
if (conv?.project_path == null)
|
|
195
|
+
return null;
|
|
196
|
+
const row = await this.db
|
|
197
|
+
.selectFrom("conversation_events")
|
|
198
|
+
.select([
|
|
199
|
+
sql `payload->>'root'`.as("root"),
|
|
200
|
+
sql `payload ? 'unavailable'`.as("unavailable"),
|
|
201
|
+
])
|
|
202
|
+
.where("conversation_id", "=", rootConversationId)
|
|
203
|
+
.where("type", "=", "project_root")
|
|
204
|
+
.orderBy("seq", "desc")
|
|
205
|
+
.limit(1)
|
|
206
|
+
.executeTakeFirst();
|
|
207
|
+
if (row === undefined || typeof row.root !== "string")
|
|
208
|
+
return null;
|
|
209
|
+
return { root: row.root, unavailable: row.unavailable !== false };
|
|
210
|
+
}
|
|
211
|
+
/** The workspace root of a conversation's latest `device_paths` event, or null. */
|
|
212
|
+
async recordedWorkspace(conversationId) {
|
|
213
|
+
const row = await this.db
|
|
214
|
+
.selectFrom("conversation_events")
|
|
215
|
+
.select(sql `payload->'roots'->>'workspace'`.as("workspace"))
|
|
216
|
+
.where("conversation_id", "=", conversationId)
|
|
217
|
+
.where("type", "=", "device_paths")
|
|
218
|
+
.orderBy("seq", "desc")
|
|
219
|
+
.limit(1)
|
|
220
|
+
.executeTakeFirst();
|
|
221
|
+
return row?.workspace ?? null;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* The workspace root and the owner's home the device's active machine last reported, or null
|
|
225
|
+
* (cloud: no machine).
|
|
226
|
+
*/
|
|
227
|
+
async machineRoots(deviceId) {
|
|
228
|
+
const row = await this.db
|
|
229
|
+
.selectFrom("device_machines")
|
|
230
|
+
.select([sql `roots->>'workspace'`.as("workspace"), "home"])
|
|
231
|
+
.where("device_id", "=", deviceId)
|
|
232
|
+
.where("revoked_at", "is", null)
|
|
233
|
+
.executeTakeFirst();
|
|
234
|
+
return row === undefined ? null : { workspace: row.workspace ?? null, home: row.home ?? null };
|
|
235
|
+
}
|
|
236
|
+
async projectScope(orgId, accountId, deviceId) {
|
|
237
|
+
const row = await this.db
|
|
238
|
+
.selectFrom("devices as e")
|
|
239
|
+
.leftJoin("device_machines as m", (j) => j.onRef("m.device_id", "=", "e.id").on("m.revoked_at", "is", null))
|
|
240
|
+
.leftJoin("device_instances as i", "i.device_id", "e.id")
|
|
241
|
+
.select([
|
|
242
|
+
"e.source",
|
|
243
|
+
"e.provider",
|
|
244
|
+
"m.features",
|
|
245
|
+
"m.local_policy",
|
|
246
|
+
"m.owner_report",
|
|
247
|
+
"m.home",
|
|
248
|
+
sql `m.roots->>'workspace'`.as("machine_workspace"),
|
|
249
|
+
"i.handle",
|
|
250
|
+
])
|
|
251
|
+
.where("e.id", "=", deviceId)
|
|
252
|
+
.where("e.org_id", "=", orgId)
|
|
253
|
+
.where("e.owner_kind", "=", "personal")
|
|
254
|
+
.where("e.owner_account_id", "=", accountId)
|
|
255
|
+
.where("e.deleted_at", "is", null)
|
|
256
|
+
.executeTakeFirst();
|
|
257
|
+
if (row === undefined)
|
|
258
|
+
throw deviceNotFound();
|
|
259
|
+
// `devicePolicy`, not `modeLevel` as the command review reads: a cloud device with a pending
|
|
260
|
+
// files reset is scoped by the narrower mode it will run at, never the wider one its instance
|
|
261
|
+
// still has, on purpose (projects are offered for what the device will read; no effect today)
|
|
262
|
+
const { mode, allowedMode } = await this.devicePolicy({ orgId, deviceId });
|
|
263
|
+
if (row.source === "local") {
|
|
264
|
+
const owner = ownerFactsOf(row);
|
|
265
|
+
return {
|
|
266
|
+
source: "local",
|
|
267
|
+
mode,
|
|
268
|
+
// B2: restricted reach follows the owner's reach, independent of its Full permission, and
|
|
269
|
+
// only within a platform and org that allow the wider device ceiling
|
|
270
|
+
reach: allowedMode === "auto" && owner.restrictedProjectReach === "os-user"
|
|
271
|
+
? "os-user"
|
|
272
|
+
: "workspace",
|
|
273
|
+
// protocol 2 only: an owner auto machine under an org that forbids it is not read unasked
|
|
274
|
+
// (its wire reads unsandboxed); an authenticated report bounds browsing by scope instead
|
|
275
|
+
widerThanAllowed: owner.state !== "report" && allowedMode === "sandbox" && ownerModeOf(row) === "auto",
|
|
276
|
+
workspace: row.machine_workspace ?? DEFAULT_SANDBOX_ROOTS.workspace,
|
|
277
|
+
home: row.home,
|
|
278
|
+
supported: deviceFeaturesOf(row.features).includes("projects"),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
const impl = providerForDevice(this.providers, row.provider)?.impl;
|
|
282
|
+
const handle = row.handle;
|
|
283
|
+
return {
|
|
284
|
+
source: "cloud",
|
|
285
|
+
mode,
|
|
286
|
+
// cloud projects stay within their provisioned files
|
|
287
|
+
reach: "workspace",
|
|
288
|
+
widerThanAllowed: false,
|
|
289
|
+
workspace: handle === null ? DEFAULT_SANDBOX_ROOTS.workspace : rootsOf(handle).workspace,
|
|
290
|
+
home: null,
|
|
291
|
+
supported: typeof impl?.list === "function" && typeof impl.scan === "function",
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
async resolveProjectForBinding(orgId, deviceId, accountId, name) {
|
|
295
|
+
const scope = await this.projectScope(orgId, accountId, deviceId);
|
|
296
|
+
if (!scope.supported)
|
|
297
|
+
throw projectsUnsupported(scope.source === "local");
|
|
298
|
+
const row = await this.readProject(orgId, deviceId, name);
|
|
299
|
+
if (row === null)
|
|
300
|
+
throw projectNotFound(name);
|
|
301
|
+
if (!withinMode(scope, row.path))
|
|
302
|
+
throw outsideMode(row.path, scope.mode);
|
|
303
|
+
// a home reported after the project was added can put it above the home: the working root
|
|
304
|
+
// would fall back for it, so it is not bound (the add rule, project root §4.3)
|
|
305
|
+
if (aboveHome(row.path, scope.home))
|
|
306
|
+
throw tooBroad(row.path);
|
|
307
|
+
return { name: row.name, path: row.path };
|
|
308
|
+
}
|
|
309
|
+
async projectMatches(deviceId, name, path) {
|
|
310
|
+
const row = await this.db
|
|
311
|
+
.selectFrom("device_projects")
|
|
312
|
+
.select("name")
|
|
313
|
+
.where("device_id", "=", deviceId)
|
|
314
|
+
.where("name", "=", name)
|
|
315
|
+
.where("path", "=", path)
|
|
316
|
+
.executeTakeFirst();
|
|
317
|
+
return row !== undefined;
|
|
318
|
+
}
|
|
319
|
+
async scope(orgId, deviceId, projectPath, project) {
|
|
320
|
+
const { policy, owner, roots: ownerRoots } = await this.readDevice(orgId, deviceId);
|
|
321
|
+
const device = await this.db
|
|
322
|
+
.selectFrom("devices")
|
|
323
|
+
.select(["provider", "source"])
|
|
324
|
+
.where("id", "=", deviceId)
|
|
325
|
+
.where("org_id", "=", orgId)
|
|
326
|
+
.executeTakeFirst();
|
|
327
|
+
const provider = device ? providerForDevice(this.providers, device.provider) : undefined;
|
|
328
|
+
let availability = permissionAvailabilityFor(provider?.impl);
|
|
329
|
+
if (availability.state === "unprobed" && provider) {
|
|
330
|
+
const machine = provider.plugin === "@local"
|
|
331
|
+
? await this.db
|
|
332
|
+
.selectFrom("device_machines")
|
|
333
|
+
.select("program_version")
|
|
334
|
+
.where("device_id", "=", deviceId)
|
|
335
|
+
.where("revoked_at", "is", null)
|
|
336
|
+
.executeTakeFirst()
|
|
337
|
+
: undefined;
|
|
338
|
+
availability = await readRuntimeReport(this.db.db, deviceId, provider.plugin, machine?.program_version ?? provider.version);
|
|
339
|
+
}
|
|
340
|
+
// A ceiling restored to the same values is still a new authorization decision. Reads do
|
|
341
|
+
// not initialize epoch rows; absent rows denote the pre-change revision zero.
|
|
342
|
+
const epochRows = await this.db
|
|
343
|
+
.selectFrom("permission_authority_epochs")
|
|
344
|
+
.select(["kind", "id", "full_revision"])
|
|
345
|
+
.where((eb) => eb.or([
|
|
346
|
+
eb.and([eb("kind", "=", "platform"), eb("id", "=", "platform")]),
|
|
347
|
+
eb.and([eb("kind", "=", "org"), eb("id", "=", orgId)]),
|
|
348
|
+
eb.and([eb("kind", "=", "device"), eb("id", "=", deviceId)]),
|
|
349
|
+
]))
|
|
350
|
+
.execute();
|
|
351
|
+
const authority = { platform: 0, org: 0, device: 0 };
|
|
352
|
+
for (const row of epochRows)
|
|
353
|
+
if (row.kind === "platform" || row.kind === "org" || row.kind === "device")
|
|
354
|
+
authority[row.kind] = row.full_revision;
|
|
355
|
+
const instanceRoots = await this.db
|
|
356
|
+
.selectFrom("device_instances")
|
|
357
|
+
.select("handle")
|
|
358
|
+
.where("device_id", "=", deviceId)
|
|
359
|
+
.executeTakeFirst();
|
|
360
|
+
const scopeRevision = permissionScopeHash({
|
|
361
|
+
authority,
|
|
362
|
+
deviceId,
|
|
363
|
+
projectPath,
|
|
364
|
+
workingRoot: projectPath ??
|
|
365
|
+
ownerRoots?.workspace ??
|
|
366
|
+
rootsOf(instanceRoots?.handle ?? { id: deviceId }).workspace,
|
|
367
|
+
mode: policy.mode,
|
|
368
|
+
allowedMode: policy.allowedMode,
|
|
369
|
+
network: device?.source === "local"
|
|
370
|
+
? { kind: "environment" }
|
|
371
|
+
: { ...policy.network, hosts: [...policy.network.hosts].sort() },
|
|
372
|
+
});
|
|
373
|
+
return {
|
|
374
|
+
deviceId,
|
|
375
|
+
project,
|
|
376
|
+
scopeRevision,
|
|
377
|
+
availability,
|
|
378
|
+
fullAccessCeiling: policy.mode === "auto" && policy.allowedMode === "auto",
|
|
379
|
+
// B2: broad restricted reach (directories outside the working root) is the owner's reach
|
|
380
|
+
// within the platform and org device ceiling, never the owner's Full permission; a cloud
|
|
381
|
+
// device keeps its previous rule (its Full ceiling over its own files)
|
|
382
|
+
broadProjectReach: device?.source === "local"
|
|
383
|
+
? policy.allowedMode === "auto" && owner?.restrictedProjectReach === "os-user"
|
|
384
|
+
: policy.mode === "auto" && policy.allowedMode === "auto",
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
async preview(orgId, accountId, deviceId, project) {
|
|
388
|
+
await this.devices.usable(orgId, accountId, deviceId);
|
|
389
|
+
const binding = project === undefined
|
|
390
|
+
? null
|
|
391
|
+
: await this.resolveProjectForBinding(orgId, deviceId, accountId, project);
|
|
392
|
+
return this.scope(orgId, deviceId, binding?.path ?? null, project ?? null);
|
|
393
|
+
}
|
|
394
|
+
async view(orgId, accountId, row) {
|
|
395
|
+
return (await this.viewWithAccess(orgId, accountId, row)).view;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* {@link view} plus how the actor reached the root's device: none selected, usable, refused for
|
|
399
|
+
* this actor (`device.not_found`: not its device), or its provider is not the current one. The
|
|
400
|
+
* recorded permission context needs the distinction; the view itself stays unchanged.
|
|
401
|
+
*/
|
|
402
|
+
async viewWithAccess(orgId, accountId, row) {
|
|
403
|
+
let preview = null;
|
|
404
|
+
let unavailableReason = "Choose a device to run device operations";
|
|
405
|
+
let access = "no-device";
|
|
406
|
+
if (row.device_id) {
|
|
407
|
+
try {
|
|
408
|
+
await this.devices.usable(orgId, accountId, row.device_id);
|
|
409
|
+
preview = await this.scope(orgId, row.device_id, row.project_path, row.project_name);
|
|
410
|
+
access = "device";
|
|
411
|
+
}
|
|
412
|
+
catch (error) {
|
|
413
|
+
if (!(error instanceof ApiError) ||
|
|
414
|
+
!["device.not_found", "device.unavailable"].includes(error.code))
|
|
415
|
+
throw error;
|
|
416
|
+
unavailableReason = error.message;
|
|
417
|
+
access = error.code === "device.not_found" ? "actor-device-access" : "provider-unavailable";
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
const consent = row.permission_consent;
|
|
421
|
+
const profile = row.permission_profile === "full-access" &&
|
|
422
|
+
(!preview?.fullAccessCeiling ||
|
|
423
|
+
preview.availability.state === "unsupported" ||
|
|
424
|
+
consent?.deviceId !== row.device_id ||
|
|
425
|
+
consent?.scopeRevision !== preview.scopeRevision)
|
|
426
|
+
? "approve-for-me"
|
|
427
|
+
: row.permission_profile;
|
|
428
|
+
return {
|
|
429
|
+
access,
|
|
430
|
+
view: {
|
|
431
|
+
profile,
|
|
432
|
+
revision: row.permission_revision,
|
|
433
|
+
rootConversationId: row.id,
|
|
434
|
+
deviceId: row.device_id,
|
|
435
|
+
consentScope: profile === "full-access" ? consent : null,
|
|
436
|
+
scopeRevision: preview?.scopeRevision ?? null,
|
|
437
|
+
availability: preview?.availability ?? {
|
|
438
|
+
state: "unsupported",
|
|
439
|
+
reason: unavailableReason,
|
|
440
|
+
},
|
|
441
|
+
fullAccessCeiling: preview?.fullAccessCeiling ?? false,
|
|
442
|
+
broadProjectReach: preview?.broadProjectReach ?? false,
|
|
443
|
+
startingWorkNotice: notice,
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* The installed environment of the device's current instance (B1 adapter over the Task 8 B4
|
|
449
|
+
* installed binding and its current runtime attestation), read under the caller's device
|
|
450
|
+
* authority lock. Null whenever it cannot be proved: no live instance (none, creating,
|
|
451
|
+
* destroyed), no installed binding of exactly this generation, a binding of another provider or
|
|
452
|
+
* machine, a runtime identity differing from the bound one, roots differing from the committed
|
|
453
|
+
* ones. A cloud device proves it by its kernel-recorded cloud evidence instead (B-15,
|
|
454
|
+
* {@link cloudAppliedLocked}). Never desired policy, never a provider call, probe, resume or
|
|
455
|
+
* provisioning; a suspended instance is read as recorded.
|
|
456
|
+
*/
|
|
457
|
+
async readAppliedEnvironmentLocked(input) {
|
|
458
|
+
assertAuthorityHeld("device", input.deviceId);
|
|
459
|
+
return this.readAppliedEnvironment(input);
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* {@link readAppliedEnvironmentLocked} without the authority locks: a managed operation's held
|
|
463
|
+
* evidence only (read under its lifecycle lease), which the managed writer validates again
|
|
464
|
+
* under the locks. Never a basis for admission or a recorded context.
|
|
465
|
+
*/
|
|
466
|
+
async readHeldEnvironment(input) {
|
|
467
|
+
return this.readAppliedEnvironment(input);
|
|
468
|
+
}
|
|
469
|
+
async readAppliedEnvironment(input) {
|
|
470
|
+
const device = await this.db
|
|
471
|
+
.selectFrom("devices")
|
|
472
|
+
.select(["source", "provider"])
|
|
473
|
+
.where("id", "=", input.deviceId)
|
|
474
|
+
.where("org_id", "=", input.orgId)
|
|
475
|
+
.where("deleted_at", "is", null)
|
|
476
|
+
.executeTakeFirst();
|
|
477
|
+
if (!device)
|
|
478
|
+
return null;
|
|
479
|
+
const instance = await this.db
|
|
480
|
+
.selectFrom("device_instances")
|
|
481
|
+
.select([
|
|
482
|
+
"provider",
|
|
483
|
+
"status",
|
|
484
|
+
"handle",
|
|
485
|
+
"created_at",
|
|
486
|
+
"permission_roots",
|
|
487
|
+
"permission_runtime_identity",
|
|
488
|
+
"instance_limits",
|
|
489
|
+
])
|
|
490
|
+
.where("device_id", "=", input.deviceId)
|
|
491
|
+
.where("org_id", "=", input.orgId)
|
|
492
|
+
.executeTakeFirst();
|
|
493
|
+
if (!instance?.provider ||
|
|
494
|
+
!instance.handle ||
|
|
495
|
+
!instance.permission_roots ||
|
|
496
|
+
!instance.permission_runtime_identity ||
|
|
497
|
+
(instance.status !== "running" && instance.status !== "suspended") ||
|
|
498
|
+
(device.provider !== null && device.provider !== instance.provider))
|
|
499
|
+
return null;
|
|
500
|
+
const roots = rootsOf(instance.handle);
|
|
501
|
+
const committed = instance.permission_roots;
|
|
502
|
+
if (committed.workspace !== roots.workspace ||
|
|
503
|
+
committed.skills !== roots.skills ||
|
|
504
|
+
committed.tmp !== roots.tmp)
|
|
505
|
+
return null;
|
|
506
|
+
if (device.source !== "local")
|
|
507
|
+
return this.cloudAppliedLocked(input, {
|
|
508
|
+
...instance,
|
|
509
|
+
provider: instance.provider,
|
|
510
|
+
handle: instance.handle,
|
|
511
|
+
});
|
|
512
|
+
const instanceGeneration = String(instance.created_at.getTime());
|
|
513
|
+
const [binding, machine] = await Promise.all([
|
|
514
|
+
this.db
|
|
515
|
+
.selectFrom("device_instance_bindings")
|
|
516
|
+
.select(["binding_id", "provider", "status", "machine_id", "runtime_identity"])
|
|
517
|
+
.where("device_id", "=", input.deviceId)
|
|
518
|
+
.where("instance_generation", "=", instanceGeneration)
|
|
519
|
+
.executeTakeFirst(),
|
|
520
|
+
this.db
|
|
521
|
+
.selectFrom("device_machines")
|
|
522
|
+
.select("id")
|
|
523
|
+
.where("device_id", "=", input.deviceId)
|
|
524
|
+
.where("org_id", "=", input.orgId)
|
|
525
|
+
.where("revoked_at", "is", null)
|
|
526
|
+
.executeTakeFirst(),
|
|
527
|
+
]);
|
|
528
|
+
if (binding?.status !== "installed" ||
|
|
529
|
+
binding.provider !== instance.provider ||
|
|
530
|
+
!binding.runtime_identity ||
|
|
531
|
+
!machine ||
|
|
532
|
+
binding.machine_id !== machine.id ||
|
|
533
|
+
!sameRuntimeIdentity(binding.runtime_identity, instance.permission_runtime_identity))
|
|
534
|
+
return null;
|
|
535
|
+
return {
|
|
536
|
+
location: "local",
|
|
537
|
+
provider: instance.provider,
|
|
538
|
+
instanceId: instance.handle.id,
|
|
539
|
+
instanceGeneration,
|
|
540
|
+
bindingId: binding.binding_id,
|
|
541
|
+
roots: { workspace: roots.workspace, skills: roots.skills, tmp: roots.tmp },
|
|
542
|
+
// a local device's outer boundary is its host environment; restricted rules are the
|
|
543
|
+
// operation's own network field, never an outer network-off
|
|
544
|
+
outerNetwork: { kind: "host-environment" },
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* A cloud instance's installed environment (B-15, Q1 option A): its current runtime report
|
|
549
|
+
* recorded with the kernel-built cloud identity (provider plugin and version, handle,
|
|
550
|
+
* generation, applied limits; `readCurrentReport`), named by that report's identity hash in
|
|
551
|
+
* place of a binding id. Null while the evidence is missing or stale, while desired limits wait
|
|
552
|
+
* for a file reset (`unapplied`) and while the applied limits are more permissive than the
|
|
553
|
+
* effective desired ones in any dimension (mode, network kind, a host the desired allowlist
|
|
554
|
+
* does not cover; spec §124): a pending desired change is never projected as installed. Applied
|
|
555
|
+
* limits narrower than desired stand (the outer boundary is the applied network, and restricted
|
|
556
|
+
* hosts are cut to it); the next use's lifecycle lease applies any change before admitting an
|
|
557
|
+
* operation, and its new limits change the identity, so that use records the evidence again.
|
|
558
|
+
*/
|
|
559
|
+
async cloudAppliedLocked(input, instance) {
|
|
560
|
+
const device = await this.db
|
|
561
|
+
.selectFrom("devices")
|
|
562
|
+
.select("provider")
|
|
563
|
+
.where("id", "=", input.deviceId)
|
|
564
|
+
.executeTakeFirst();
|
|
565
|
+
const entry = providerForDevice(this.providers, device?.provider ?? null);
|
|
566
|
+
if (!entry || entry.plugin !== instance.provider)
|
|
567
|
+
return null;
|
|
568
|
+
const evidence = await readCurrentReport(this.db.db, input.deviceId, entry.plugin, entry.version);
|
|
569
|
+
if (evidence.identityHash === null)
|
|
570
|
+
return null;
|
|
571
|
+
const limits = readInstanceLimits(instance.instance_limits);
|
|
572
|
+
if (limits.applied === null || limits.unapplied !== null)
|
|
573
|
+
return null;
|
|
574
|
+
// applied limits more permissive than the effective desired ones (spec §124) are not the
|
|
575
|
+
// installed authority: unproved until the next use's lifecycle lease applies the narrowing
|
|
576
|
+
if (appliedWider(limits.applied, appliedOf(await this.devicePolicy(input))))
|
|
577
|
+
return null;
|
|
578
|
+
const outerNetwork = cloudOuterNetwork(limits.applied.network);
|
|
579
|
+
if (outerNetwork === null)
|
|
580
|
+
return null;
|
|
581
|
+
const roots = rootsOf(instance.handle);
|
|
582
|
+
return {
|
|
583
|
+
location: "cloud",
|
|
584
|
+
provider: instance.provider,
|
|
585
|
+
instanceId: instance.handle.id,
|
|
586
|
+
instanceGeneration: String(instance.created_at.getTime()),
|
|
587
|
+
bindingId: evidence.identityHash,
|
|
588
|
+
roots: { workspace: roots.workspace, skills: roots.skills, tmp: roots.tmp },
|
|
589
|
+
outerNetwork,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
PermissionFacts = __decorate([
|
|
594
|
+
Injectable(),
|
|
595
|
+
__param(0, Inject(KernelDb)),
|
|
596
|
+
__param(1, Inject(KernelConfigService)),
|
|
597
|
+
__param(2, Inject(SandboxProviders)),
|
|
598
|
+
__param(3, Inject(DeviceAccess))
|
|
599
|
+
], PermissionFacts);
|
|
600
|
+
export { PermissionFacts };
|
|
601
|
+
/**
|
|
602
|
+
* The outer boundary a cloud instance's applied network proves: public addresses, or its exact
|
|
603
|
+
* allowlist (empty: network disabled). Null when the allowlist cannot be stated as recorded
|
|
604
|
+
* context hosts (a host that is not a normalized authority host, or more than the cap).
|
|
605
|
+
*/
|
|
606
|
+
export function cloudOuterNetwork(network) {
|
|
607
|
+
if (network.kind === "open-public")
|
|
608
|
+
return { kind: "open-public" };
|
|
609
|
+
const hosts = [...new Set(network.hosts)].sort(permissionCodePointCompare);
|
|
610
|
+
if (hosts.length === 0)
|
|
611
|
+
return { kind: "none" };
|
|
612
|
+
if (hosts.length > 100 || hosts.some((host) => validateAllowHost(host).length > 0))
|
|
613
|
+
return null;
|
|
614
|
+
return { kind: "allowlist", hosts };
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Restricted hosts within a cloud instance's applied outer boundary (narrowing only): a host the
|
|
618
|
+
* outer allowlist does not cover cannot be reached, so neither admission nor the recorded context
|
|
619
|
+
* names it. A local device's host environment keeps every host.
|
|
620
|
+
*/
|
|
621
|
+
export function hostsWithinOuter(hosts, outer) {
|
|
622
|
+
if (!outer || outer.kind === "host-environment" || outer.kind === "open-public")
|
|
623
|
+
return [...hosts];
|
|
624
|
+
if (outer.kind === "none")
|
|
625
|
+
return [];
|
|
626
|
+
return hosts.filter((host) => outer.hosts.some((pattern) => coversHost(pattern, host)));
|
|
627
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { Module } from "@nestjs/common";
|
|
8
|
+
import { DeviceAccessModule } from "../devices/access.module.js";
|
|
9
|
+
import { PermissionFacts } from "./permission-facts.js";
|
|
10
|
+
import { SandboxProviderRegistryModule } from "./providers.module.js";
|
|
11
|
+
let PermissionFactsModule = class PermissionFactsModule {
|
|
12
|
+
};
|
|
13
|
+
PermissionFactsModule = __decorate([
|
|
14
|
+
Module({
|
|
15
|
+
imports: [SandboxProviderRegistryModule, DeviceAccessModule],
|
|
16
|
+
providers: [PermissionFacts],
|
|
17
|
+
exports: [PermissionFacts],
|
|
18
|
+
})
|
|
19
|
+
], PermissionFactsModule);
|
|
20
|
+
export { PermissionFactsModule };
|