@capybara-hq/kernel 0.1.0-rc.40 → 0.1.0-rc.42
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 +10 -1
- 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 +1 -1
- package/dist/config/schema.js +11 -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 +304 -4
- package/dist/conversations/events.js +279 -29
- 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/db/kernel-schema.d.ts +238 -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/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 +31 -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/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/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/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/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/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 +129 -0
- package/dist/sandbox/review/review.service.d.ts +4 -1
- package/dist/sandbox/review/review.service.js +19 -5
- 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/builtins/builtin-tools.d.ts +16 -2
- package/dist/tools/builtins/builtin-tools.js +38 -10
- 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 +2 -2
- 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 +300 -19
- package/dist/tools/registry.d.ts +9 -0
- package/dist/tools/tools.module.js +12 -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/dist/sandbox/platform-auto-mode.d.ts +0 -63
- package/dist/sandbox/platform-auto-mode.js +0 -63
|
@@ -0,0 +1,783 @@
|
|
|
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 { parseSandboxBrowseScope, parseSandboxOperationScope, } from "@capybara-hq/plugin-sdk";
|
|
12
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
13
|
+
import { sql } from "kysely";
|
|
14
|
+
import { PermissionContextV1Schema, permissionCodePointCompare, } from "../conversations/events.js";
|
|
15
|
+
import { PermissionProfileStore, } from "../conversations/permission-profile-store.js";
|
|
16
|
+
import { canonicalRoot, workingRootOf as reviewWorkingRootOf } from "../devices/working-root.js";
|
|
17
|
+
import { ApiError } from "../errors.js";
|
|
18
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
19
|
+
import { TransactionHost, withTransactionFencePrelude, } from "../infra/database/transaction-host.js";
|
|
20
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
21
|
+
import { NetworkGrants } from "../local-devices/network-grants.js";
|
|
22
|
+
import { KernelReadUnsupported, kernelReadRoot, kernelReadRoute, } from "./kernel-read.js";
|
|
23
|
+
import { coversHost, projectReachOf } from "./modes.js";
|
|
24
|
+
import { isExecutionMethod, parseStartingExecutionGrants } from "./operation-authority-types.js";
|
|
25
|
+
import { PermissionAuthorityEpochStore } from "./permission-authority-epochs.js";
|
|
26
|
+
import { assertAuthorityHeld, PermissionAuthorityLock } from "./permission-authority-lock.js";
|
|
27
|
+
import { PermissionCallGrants } from "./permission-call-grants.js";
|
|
28
|
+
import { PermissionConfigPublication } from "./permission-config-publication.js";
|
|
29
|
+
import { PermissionContextSubjectChanged, } from "./permission-context-types.js";
|
|
30
|
+
import { hostsWithinOuter, PermissionFacts, permissionScopeHash } from "./permission-facts.js";
|
|
31
|
+
import { normalizePermissionExpansion } from "./review/permission-candidate.js";
|
|
32
|
+
const UNPROVED_ENVIRONMENT = "The device's installed environment is not verified yet; it is checked when the device starts";
|
|
33
|
+
/**
|
|
34
|
+
* The one verdict over a device's runtime report and its applied environment (B1, B-16): the
|
|
35
|
+
* recorded permission context withholds its scope exactly when this returns a verdict, and
|
|
36
|
+
* admission refuses with the same reason, so the model is never told it can do what admission
|
|
37
|
+
* refuses, nor the reverse (local and cloud devices alike, B-15). Unknown applied facts precede a
|
|
38
|
+
* positive capability report; a working root that is not the installed workspace (no project) is
|
|
39
|
+
* never installed scope.
|
|
40
|
+
*/
|
|
41
|
+
function withheldScope(input) {
|
|
42
|
+
const { availability, applied } = input;
|
|
43
|
+
if (availability.state !== "supported")
|
|
44
|
+
return { state: availability.state, reason: availability.reason };
|
|
45
|
+
if (applied === null ||
|
|
46
|
+
applied.location !== input.location ||
|
|
47
|
+
(input.projectPath === null && input.workingRoot !== applied.roots.workspace))
|
|
48
|
+
return { state: "unprobed", reason: UNPROVED_ENVIRONMENT };
|
|
49
|
+
if (input.effectiveProfile === "full-access") {
|
|
50
|
+
if (!availability.report.fullAccess)
|
|
51
|
+
return { state: "unsupported", reason: "This device's runtime cannot run Full access" };
|
|
52
|
+
}
|
|
53
|
+
else if (availability.report.restricted === null)
|
|
54
|
+
return {
|
|
55
|
+
state: "unsupported",
|
|
56
|
+
reason: "This device's runtime cannot enforce the restricted permission profiles",
|
|
57
|
+
};
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The profile every kernel read is sent with: a restricted one, so the runtime enforces its single
|
|
62
|
+
* read root (a symlink out of the project is refused) whatever the conversation selected.
|
|
63
|
+
*/
|
|
64
|
+
const KERNEL_READ_PROFILE = "request-approval";
|
|
65
|
+
/** Only server-resolved facts enter this constructor; callers never submit roots or profile. */
|
|
66
|
+
export function intersectPermissionScope(f) {
|
|
67
|
+
if (f.profile === "full-access" && (!f.fullConsent || !f.fullAccessCeiling))
|
|
68
|
+
throw new ApiError("forbidden", "Full access requires current device authority and matching consent");
|
|
69
|
+
return parseSandboxOperationScope({
|
|
70
|
+
operationId: f.operationId,
|
|
71
|
+
admitBeforeMs: f.admitBeforeMs,
|
|
72
|
+
deviceId: f.deviceId,
|
|
73
|
+
rootConversationId: f.rootConversationId,
|
|
74
|
+
profileRevision: f.profileRevision,
|
|
75
|
+
ceilingRevision: f.ceilingRevision,
|
|
76
|
+
profile: f.profile,
|
|
77
|
+
actor: f.actor,
|
|
78
|
+
tag: f.tag,
|
|
79
|
+
filesystem: f.profile === "full-access"
|
|
80
|
+
? { kind: "device" }
|
|
81
|
+
: {
|
|
82
|
+
kind: "scoped",
|
|
83
|
+
readRoots: [...new Set([...f.readRoots, ...f.writeRoots])].sort(),
|
|
84
|
+
writeRoots: [...new Set(f.writeRoots)].sort(),
|
|
85
|
+
},
|
|
86
|
+
network: f.profile === "full-access"
|
|
87
|
+
? { kind: "environment" }
|
|
88
|
+
: f.allowedHosts.length
|
|
89
|
+
? { kind: "allowlist", hosts: [...new Set(f.allowedHosts)].sort() }
|
|
90
|
+
: { kind: "none" },
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/** Fresh authority shape only. Provider dispatch must add durable admission fencing before use. */
|
|
94
|
+
let SandboxOperationAuthority = class SandboxOperationAuthority {
|
|
95
|
+
callGrants;
|
|
96
|
+
publication;
|
|
97
|
+
db;
|
|
98
|
+
txHost;
|
|
99
|
+
profiles;
|
|
100
|
+
facts;
|
|
101
|
+
grants;
|
|
102
|
+
locks;
|
|
103
|
+
epochs;
|
|
104
|
+
constructor(callGrants, publication, db, txHost, profiles, facts, grants, locks, epochs) {
|
|
105
|
+
this.callGrants = callGrants;
|
|
106
|
+
this.publication = publication;
|
|
107
|
+
this.db = db;
|
|
108
|
+
this.txHost = txHost;
|
|
109
|
+
this.profiles = profiles;
|
|
110
|
+
this.facts = facts;
|
|
111
|
+
this.grants = grants;
|
|
112
|
+
this.locks = locks;
|
|
113
|
+
this.epochs = epochs;
|
|
114
|
+
}
|
|
115
|
+
/** Owner project browsing has no conversation, profile, or fabricated root identity. */
|
|
116
|
+
async resolveBrowse(ctx, signal) {
|
|
117
|
+
signal.throwIfAborted();
|
|
118
|
+
await this.locks.acquire({ orgId: ctx.orgId, deviceId: ctx.deviceId }, { kind: "admit" });
|
|
119
|
+
await this.publication.assertCurrent();
|
|
120
|
+
await this.facts.assertCloudReconciled(ctx.orgId, ctx.deviceId);
|
|
121
|
+
await this.facts.assertOwnerReportUsable(ctx.orgId, ctx.deviceId);
|
|
122
|
+
if (!(await this.isActiveMember(ctx.orgId, ctx.accountId)))
|
|
123
|
+
throw new ApiError("device.not_found", "Device not found");
|
|
124
|
+
await this.facts.preview(ctx.orgId, ctx.accountId, ctx.deviceId);
|
|
125
|
+
const scope = await this.facts.projectScope(ctx.orgId, ctx.accountId, ctx.deviceId);
|
|
126
|
+
if (!scope.supported || scope.widerThanAllowed)
|
|
127
|
+
throw new ApiError("device.projects_unsupported", "Current owner and organization ceilings do not permit browsing");
|
|
128
|
+
const operationId = randomUUID();
|
|
129
|
+
const vector = await this.epochs.readLocked({ orgId: ctx.orgId, deviceId: ctx.deviceId });
|
|
130
|
+
return parseSandboxBrowseScope({
|
|
131
|
+
operationId,
|
|
132
|
+
deviceId: ctx.deviceId,
|
|
133
|
+
accountId: ctx.accountId,
|
|
134
|
+
admitBeforeMs: Date.now() + 30000,
|
|
135
|
+
ceilingRevision: permissionScopeHash(vector),
|
|
136
|
+
readRoots: scope.reach === "os-user" ? ["/"] : [scope.workspace],
|
|
137
|
+
tag: operationId,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The one actor liveness predicate of owner browsing, the recorded permission context and the
|
|
142
|
+
* managed device context writer: an active account that is still a member of a live org.
|
|
143
|
+
*/
|
|
144
|
+
async isActiveMember(orgId, accountId) {
|
|
145
|
+
const member = await this.db
|
|
146
|
+
.selectFrom("memberships as m")
|
|
147
|
+
.innerJoin("accounts as a", "a.id", "m.account_id")
|
|
148
|
+
.innerJoin("orgs as o", "o.id", "m.org_id")
|
|
149
|
+
.select("m.account_id")
|
|
150
|
+
.where("m.org_id", "=", orgId)
|
|
151
|
+
.where("m.account_id", "=", accountId)
|
|
152
|
+
.where("a.status", "=", "active")
|
|
153
|
+
.where("o.deleted_at", "is", null)
|
|
154
|
+
.executeTakeFirst();
|
|
155
|
+
return member !== undefined;
|
|
156
|
+
}
|
|
157
|
+
async subjectFor(ctx) {
|
|
158
|
+
const root = await this.profiles.readOwnedRoot(ctx.orgId, ctx.holderConversationId, ctx.actor.accountId);
|
|
159
|
+
return { orgId: ctx.orgId, deviceId: ctx.deviceId, rootConversationId: root.id };
|
|
160
|
+
}
|
|
161
|
+
async previewCall(ctx, call, signal) {
|
|
162
|
+
const subject = await this.subjectFor(ctx);
|
|
163
|
+
return withTransactionFencePrelude(() => this.locks.acquire(subject, { kind: "admit" }), () => this.previewLocked(ctx, call, signal));
|
|
164
|
+
}
|
|
165
|
+
async previewLocked(ctx, call, signal) {
|
|
166
|
+
const snapshot = await this.snapshot(ctx, call, signal);
|
|
167
|
+
const messages = await this.db
|
|
168
|
+
.selectFrom("conversation_events")
|
|
169
|
+
.select("payload")
|
|
170
|
+
.where("conversation_id", "=", ctx.holderConversationId)
|
|
171
|
+
.where("run_id", "=", call.runId)
|
|
172
|
+
.where("type", "=", "assistant_message")
|
|
173
|
+
.execute();
|
|
174
|
+
if (!messages.some((row) => Array.isArray(row.payload.content) &&
|
|
175
|
+
row.payload.content.some((v) => v !== null &&
|
|
176
|
+
typeof v === "object" &&
|
|
177
|
+
"type" in v &&
|
|
178
|
+
v.type === "tool_call" &&
|
|
179
|
+
"id" in v &&
|
|
180
|
+
v.id === call.callId)))
|
|
181
|
+
throw new ApiError("forbidden", "The permission review has no recorded tool call");
|
|
182
|
+
return snapshot;
|
|
183
|
+
}
|
|
184
|
+
async resolve(ctx, operation, signal) {
|
|
185
|
+
const f = await this.snapshot(ctx, { runId: ctx.actor.runId ?? "", callId: "" }, signal);
|
|
186
|
+
return this.scopeOf(ctx, operation, f);
|
|
187
|
+
}
|
|
188
|
+
async resolveInvocation(ctx, operation, call, signal) {
|
|
189
|
+
let networkGrant;
|
|
190
|
+
const f = await this.snapshot(ctx, call, signal, (grant) => {
|
|
191
|
+
networkGrant = grant;
|
|
192
|
+
});
|
|
193
|
+
const started = await this.db
|
|
194
|
+
.selectFrom("conversation_events")
|
|
195
|
+
.select("payload")
|
|
196
|
+
.where("conversation_id", "=", ctx.holderConversationId)
|
|
197
|
+
.where("run_id", "=", call.runId)
|
|
198
|
+
.where("type", "=", "tool_call_started")
|
|
199
|
+
.where(sql `payload->>'callId'`, "=", call.callId)
|
|
200
|
+
.executeTakeFirst();
|
|
201
|
+
if (!started)
|
|
202
|
+
throw new ApiError("forbidden", "The operation has no canonical started call");
|
|
203
|
+
const payload = started.payload;
|
|
204
|
+
const { orgId, rootConversationId, conversationId, deviceId, accountId, runId, callId, profile, profileRevision, scopeRevision, authorityRevision, } = f;
|
|
205
|
+
const grant = payload.toolKey && payload.specHash && payload.permissionInput
|
|
206
|
+
? await this.callGrants.findForAdmission({
|
|
207
|
+
orgId,
|
|
208
|
+
rootConversationId,
|
|
209
|
+
conversationId,
|
|
210
|
+
deviceId,
|
|
211
|
+
accountId,
|
|
212
|
+
runId,
|
|
213
|
+
callId,
|
|
214
|
+
profile,
|
|
215
|
+
profileRevision,
|
|
216
|
+
scopeRevision,
|
|
217
|
+
authorityRevision,
|
|
218
|
+
toolKey: payload.toolKey,
|
|
219
|
+
specHash: payload.specHash,
|
|
220
|
+
...payload.permissionInput,
|
|
221
|
+
})
|
|
222
|
+
: null;
|
|
223
|
+
if (grant) {
|
|
224
|
+
normalizePermissionExpansion(grant.expansion, f);
|
|
225
|
+
if ((!f.broadProjectReach &&
|
|
226
|
+
(grant.expansion.readDirectories.length || grant.expansion.writeDirectories.length)) ||
|
|
227
|
+
grant.expansion.hosts.some((host) => f.policy.network.kind !== "open-public" &&
|
|
228
|
+
!f.policy.network.hosts.some((pattern) => coversHost(pattern, host))))
|
|
229
|
+
throw new ApiError("forbidden", "Permission expansion exceeds the current ceiling");
|
|
230
|
+
}
|
|
231
|
+
// an accepted host addition the applied cloud outer boundary does not cover cannot be
|
|
232
|
+
// reached: cut like the base hosts, so admission never names more than the context (B-15)
|
|
233
|
+
const expansionHosts = hostsWithinOuter(grant?.expansion.hosts ?? [], f.outerNetwork ?? undefined);
|
|
234
|
+
const scope = this.scopeOf(ctx, operation, {
|
|
235
|
+
...f,
|
|
236
|
+
baseReadDirectories: [...f.baseReadDirectories, ...(grant?.expansion.readDirectories ?? [])],
|
|
237
|
+
baseWriteDirectories: [
|
|
238
|
+
...f.baseWriteDirectories,
|
|
239
|
+
...(grant?.expansion.writeDirectories ?? []),
|
|
240
|
+
],
|
|
241
|
+
baseHosts: [...f.baseHosts, ...expansionHosts],
|
|
242
|
+
});
|
|
243
|
+
const hosts = isExecutionMethod(operation)
|
|
244
|
+
? [...new Set([...(networkGrant?.hosts ?? []), ...expansionHosts])].sort()
|
|
245
|
+
: [];
|
|
246
|
+
const executionGrants = isExecutionMethod(operation)
|
|
247
|
+
? parseStartingExecutionGrants({
|
|
248
|
+
version: 1,
|
|
249
|
+
grants: hosts.length
|
|
250
|
+
? {
|
|
251
|
+
hosts,
|
|
252
|
+
// The accepted per-call addition takes source precedence over the run/task envelope.
|
|
253
|
+
source: expansionHosts.length
|
|
254
|
+
? { kind: "run", id: call.runId, accountId: ctx.actor.accountId }
|
|
255
|
+
: networkGrant?.source,
|
|
256
|
+
}
|
|
257
|
+
: null,
|
|
258
|
+
})
|
|
259
|
+
: null;
|
|
260
|
+
return { scope, permissionGrantId: grant?.id ?? null, grantedHosts: hosts, executionGrants };
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* The scope of one kernel read (permission activation L2, Hook 1): the conversation's profile
|
|
264
|
+
* scope from the same authority snapshot admission reads, with the bound project as the working
|
|
265
|
+
* root it becomes once read (the review's fallbacks apply: a root outside the mode or above the
|
|
266
|
+
* home is refused), intersected with the one path the read may reach
|
|
267
|
+
* ({@link kernelReadRoot}: the root's `stat`, `AGENTS.md`, `.agents/skills/**`). The restricted
|
|
268
|
+
* {@link KERNEL_READ_PROFILE} whenever the runtime enforces it (the runtime then enforces the
|
|
269
|
+
* read root, symlinks included); the conversation's admitted Full access only on an isolated
|
|
270
|
+
* Full-only runtime; a host machine without restricted enforcement throws
|
|
271
|
+
* {@link KernelReadUnsupported} ({@link kernelReadRoute}). No write roots, no hosts, never a
|
|
272
|
+
* per-call grant. Called under the run start's claim only (the intents
|
|
273
|
+
* check that its run is still `queued`).
|
|
274
|
+
*/
|
|
275
|
+
async resolveKernelRead(ctx, read, method, path, signal) {
|
|
276
|
+
if (read.purpose !== "project_root")
|
|
277
|
+
throw new ApiError("forbidden", "Unknown kernel read purpose");
|
|
278
|
+
const covering = kernelReadRoot(read.root, method, path);
|
|
279
|
+
if (covering === null)
|
|
280
|
+
throw new ApiError("forbidden", "A kernel read reaches only the project's root, its AGENTS.md and its .agents/skills");
|
|
281
|
+
const f = await this.snapshot(ctx, { runId: ctx.actor.runId ?? "", callId: "" }, signal, undefined, { projectUsable: true });
|
|
282
|
+
const bound = await this.profiles.readOwnedRoot(ctx.orgId, ctx.holderConversationId, ctx.actor.accountId);
|
|
283
|
+
if (bound.project_path === null ||
|
|
284
|
+
canonicalRoot(bound.project_path) !== canonicalRoot(read.root) ||
|
|
285
|
+
canonicalRoot(f.workingRoot) !== canonicalRoot(read.root))
|
|
286
|
+
throw new ApiError("forbidden", "The kernel read's root is not the conversation's usable project root");
|
|
287
|
+
// the recorded runtime support decides the route: restricted when enforced, Full access only
|
|
288
|
+
// on an isolated Full-only runtime, nothing on a host machine without restricted enforcement
|
|
289
|
+
const availability = f.availability;
|
|
290
|
+
let route = "restricted";
|
|
291
|
+
if (availability.state === "supported") {
|
|
292
|
+
route = kernelReadRoute({
|
|
293
|
+
restricted: availability.report.restricted !== null,
|
|
294
|
+
fullAccess: availability.report.fullAccess,
|
|
295
|
+
isolated: await this.isolatedRuntime(ctx.orgId, ctx.deviceId),
|
|
296
|
+
});
|
|
297
|
+
if (route === "refused")
|
|
298
|
+
throw new KernelReadUnsupported();
|
|
299
|
+
}
|
|
300
|
+
const profile = this.scopeOf(ctx, method, f);
|
|
301
|
+
if (profile.filesystem.kind === "scoped" &&
|
|
302
|
+
!profile.filesystem.readRoots.some((r) => covering === r || covering.startsWith(r === "/" ? "/" : `${r}/`)))
|
|
303
|
+
throw new ApiError("forbidden", "The kernel read is outside the conversation's profile");
|
|
304
|
+
// a restricted read-only scope whenever the runtime enforces it, whatever the conversation's
|
|
305
|
+
// profile: a runtime keyed on Full access may follow a symlinked AGENTS.md or SKILL.md out of
|
|
306
|
+
// the project, and a kernel read has no tool call or review to show for it
|
|
307
|
+
return parseSandboxOperationScope({
|
|
308
|
+
...profile,
|
|
309
|
+
profile: route === "full" ? profile.profile : KERNEL_READ_PROFILE,
|
|
310
|
+
filesystem: { kind: "scoped", readRoots: [covering], writeRoots: [] },
|
|
311
|
+
network: { kind: "none" },
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Whether the device's runtime is isolated from its host's files (a kernel read may then go
|
|
316
|
+
* under the conversation's Full access on a Full-only runtime): a cloud device, or a local
|
|
317
|
+
* machine whose authenticated connection recorded its Docker mode (`device_machines.mode`,
|
|
318
|
+
* written at hello; a mode change resets the device's files).
|
|
319
|
+
*/
|
|
320
|
+
async isolatedRuntime(orgId, deviceId) {
|
|
321
|
+
const row = await this.db
|
|
322
|
+
.selectFrom("devices as d")
|
|
323
|
+
.leftJoin("device_machines as m", (j) => j.onRef("m.device_id", "=", "d.id").on("m.revoked_at", "is", null))
|
|
324
|
+
.select(["d.source", "m.mode"])
|
|
325
|
+
.where("d.id", "=", deviceId)
|
|
326
|
+
.where("d.org_id", "=", orgId)
|
|
327
|
+
.executeTakeFirst();
|
|
328
|
+
return row !== undefined && (row.source !== "local" || row.mode === "docker");
|
|
329
|
+
}
|
|
330
|
+
scopeOf(ctx, operation, f) {
|
|
331
|
+
if (!["exec", "readFile", "writeFile", "stat", "list", "scan", "backgroundStart"].includes(operation))
|
|
332
|
+
throw new ApiError("forbidden", "Unknown sandbox operation");
|
|
333
|
+
// the recorded context withholds its scope for exactly these states (B-16)
|
|
334
|
+
if (f.admissionRefusal)
|
|
335
|
+
throw new ApiError("forbidden", f.admissionRefusal);
|
|
336
|
+
const operationId = randomUUID();
|
|
337
|
+
return intersectPermissionScope({
|
|
338
|
+
operationId,
|
|
339
|
+
admitBeforeMs: Date.now() + 30000,
|
|
340
|
+
deviceId: ctx.deviceId,
|
|
341
|
+
rootConversationId: f.rootConversationId,
|
|
342
|
+
profileRevision: f.profileRevision,
|
|
343
|
+
ceilingRevision: f.authorityRevision,
|
|
344
|
+
profile: f.profile,
|
|
345
|
+
actor: ctx.actor,
|
|
346
|
+
tag: operationId,
|
|
347
|
+
workingRoot: f.workingRoot,
|
|
348
|
+
readRoots: f.baseReadDirectories,
|
|
349
|
+
writeRoots: f.baseWriteDirectories,
|
|
350
|
+
allowedHosts: isExecutionMethod(operation) ? f.baseHosts : [],
|
|
351
|
+
fullAccessCeiling: f.fullAccessCeiling,
|
|
352
|
+
fullConsent: f.fullConsent,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
async snapshot(ctx, call, signal, captureGrant, opts = {}) {
|
|
356
|
+
signal.throwIfAborted();
|
|
357
|
+
if (ctx.actor.runId && call.runId !== ctx.actor.runId)
|
|
358
|
+
throw new ApiError("forbidden", "Permission call does not match its run");
|
|
359
|
+
if (ctx.actor.conversationId !== ctx.holderConversationId)
|
|
360
|
+
throw new ApiError("forbidden", "Operation actor does not match its conversation");
|
|
361
|
+
const initial = await this.profiles.readOwnedRoot(ctx.orgId, ctx.holderConversationId, ctx.actor.accountId);
|
|
362
|
+
await this.facts.view(ctx.orgId, ctx.actor.accountId, initial);
|
|
363
|
+
const subject = {
|
|
364
|
+
orgId: ctx.orgId,
|
|
365
|
+
deviceId: ctx.deviceId,
|
|
366
|
+
rootConversationId: initial.id,
|
|
367
|
+
};
|
|
368
|
+
await this.locks.acquire(subject, { kind: "admit" });
|
|
369
|
+
// the recorded context's actor predicate: a disabled or removed actor is refused alike (B-19).
|
|
370
|
+
// Both readers refuse in the same two steps with the same errors: the unlocked owned-root read
|
|
371
|
+
// (`conversation.not_found`), then this backstop under the locks for an actor removed in
|
|
372
|
+
// between (`forbidden`, the text `readContextLocked` and the managed writer throw). The
|
|
373
|
+
// recorder maps both codes to its one `actor` run error: the model-facing refusal is the same
|
|
374
|
+
if (!(await this.isActiveMember(ctx.orgId, ctx.actor.accountId)))
|
|
375
|
+
throw new ApiError("forbidden", "This run's actor is no longer authorized");
|
|
376
|
+
await this.publication.assertCurrent();
|
|
377
|
+
await this.facts.assertCloudReconciled(ctx.orgId, ctx.deviceId);
|
|
378
|
+
await this.facts.assertOwnerReportUsable(ctx.orgId, ctx.deviceId);
|
|
379
|
+
const root = await this.profiles.readOwnedRoot(ctx.orgId, ctx.holderConversationId, ctx.actor.accountId);
|
|
380
|
+
const view = await this.facts.view(ctx.orgId, ctx.actor.accountId, root);
|
|
381
|
+
if (view.deviceId !== ctx.deviceId)
|
|
382
|
+
throw new ApiError("device.not_found", "The conversation is not bound to this device");
|
|
383
|
+
await this.facts.preview(ctx.orgId, ctx.actor.accountId, ctx.deviceId, root.project_name ?? undefined);
|
|
384
|
+
let unattended = root.scheduled_task_id !== null;
|
|
385
|
+
if (ctx.actor.runId)
|
|
386
|
+
unattended ||= await this.runScheduledLocked(ctx.holderConversationId, ctx.actor.accountId, ctx.actor.runId);
|
|
387
|
+
const base = await this.baseScopeLocked({
|
|
388
|
+
orgId: ctx.orgId,
|
|
389
|
+
deviceId: ctx.deviceId,
|
|
390
|
+
root,
|
|
391
|
+
holderConversationId: ctx.holderConversationId,
|
|
392
|
+
runId: ctx.actor.runId,
|
|
393
|
+
...opts,
|
|
394
|
+
});
|
|
395
|
+
const { policy, device, workingRoot, projectRoot, grant } = base;
|
|
396
|
+
const applied = await this.facts.readAppliedEnvironmentLocked({
|
|
397
|
+
orgId: ctx.orgId,
|
|
398
|
+
deviceId: ctx.deviceId,
|
|
399
|
+
});
|
|
400
|
+
// admission's hosts, as the recorded context names them: within a cloud instance's applied
|
|
401
|
+
// outer boundary
|
|
402
|
+
const allowedHosts = hostsWithinOuter(base.allowedHosts, applied?.outerNetwork);
|
|
403
|
+
captureGrant?.(grant
|
|
404
|
+
? {
|
|
405
|
+
hosts: grant.hosts.filter((host) => allowedHosts.includes(host)).sort(),
|
|
406
|
+
source: { ...grant.source },
|
|
407
|
+
}
|
|
408
|
+
: undefined);
|
|
409
|
+
const effectiveProfile = unattended ? "request-approval" : view.profile;
|
|
410
|
+
// one verdict for local and cloud devices alike (B-16, B-15): a cloud device without an
|
|
411
|
+
// instance yet is refused here, but provisioning is not an admitted operation: the lifecycle
|
|
412
|
+
// lease creates the instance and records its evidence before the first operation is admitted
|
|
413
|
+
const withheld = withheldScope({
|
|
414
|
+
availability: view.availability,
|
|
415
|
+
effectiveProfile,
|
|
416
|
+
location: device.source === "local" ? "local" : "cloud",
|
|
417
|
+
workingRoot,
|
|
418
|
+
projectPath: projectRoot,
|
|
419
|
+
applied,
|
|
420
|
+
});
|
|
421
|
+
const vector = await this.epochs.readLocked(subject);
|
|
422
|
+
signal.throwIfAborted();
|
|
423
|
+
if (!view.scopeRevision)
|
|
424
|
+
throw new ApiError("forbidden", "Device permission scope is unavailable");
|
|
425
|
+
return {
|
|
426
|
+
orgId: ctx.orgId,
|
|
427
|
+
rootConversationId: view.rootConversationId,
|
|
428
|
+
conversationId: ctx.holderConversationId,
|
|
429
|
+
deviceId: ctx.deviceId,
|
|
430
|
+
accountId: ctx.actor.accountId,
|
|
431
|
+
runId: call.runId,
|
|
432
|
+
callId: call.callId,
|
|
433
|
+
profile: effectiveProfile,
|
|
434
|
+
profileRevision: view.revision,
|
|
435
|
+
scopeRevision: view.scopeRevision,
|
|
436
|
+
authorityRevision: permissionScopeHash({ scopeRevision: view.scopeRevision, vector }),
|
|
437
|
+
workingRoot,
|
|
438
|
+
baseReadDirectories: [workingRoot],
|
|
439
|
+
baseWriteDirectories: [workingRoot],
|
|
440
|
+
baseHosts: allowedHosts,
|
|
441
|
+
fullAccessCeiling: view.fullAccessCeiling,
|
|
442
|
+
broadProjectReach: view.broadProjectReach,
|
|
443
|
+
fullConsent: view.profile === "full-access" && view.consentScope?.scopeRevision === view.scopeRevision,
|
|
444
|
+
unattended,
|
|
445
|
+
// the review and admission see the recorded context's verdict, never a wider one
|
|
446
|
+
availability: withheld ?? view.availability,
|
|
447
|
+
admissionRefusal: withheld?.reason ?? null,
|
|
448
|
+
outerNetwork: applied?.outerNetwork ?? null,
|
|
449
|
+
policy: device.source === "local"
|
|
450
|
+
? { ...policy, network: { kind: "allowlist", hosts: policy.localAllowedDomains } }
|
|
451
|
+
: policy,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* The actual run of an operation or context read: still this conversation's, triggered by this
|
|
456
|
+
* actor and not ended; true when it serves a scheduled task run (an unattended child or wake
|
|
457
|
+
* included). Shared by admission and the recorded permission context.
|
|
458
|
+
*/
|
|
459
|
+
async runScheduledLocked(conversationId, accountId, runId) {
|
|
460
|
+
const run = await this.db
|
|
461
|
+
.selectFrom("runs")
|
|
462
|
+
.select(["conversation_id", "trigger_account_id", "scheduled_task_run_id", "status"])
|
|
463
|
+
.where("id", "=", runId)
|
|
464
|
+
.executeTakeFirst();
|
|
465
|
+
if (!run ||
|
|
466
|
+
run.conversation_id !== conversationId ||
|
|
467
|
+
run.trigger_account_id !== accountId ||
|
|
468
|
+
!["running", "queued", "waiting"].includes(run.status))
|
|
469
|
+
throw new ApiError("forbidden", "The operation run is no longer authorized");
|
|
470
|
+
return run.scheduled_task_run_id !== null;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* The call-independent base authority of a device for one root (B1): the device's policy and
|
|
474
|
+
* owner facts, its working root (the validated project binding, else the recorded or reported
|
|
475
|
+
* workspace) and the restricted base hosts after the owner and organization ceilings, including
|
|
476
|
+
* the run's network grants and an unattended run's explicit task grants. No call expansion, no
|
|
477
|
+
* operation, no provider call. Admission and the recorded permission context both read it here.
|
|
478
|
+
*/
|
|
479
|
+
async baseScopeLocked(input) {
|
|
480
|
+
const { orgId, deviceId } = input;
|
|
481
|
+
const { policy, owner } = await this.facts.readDevice(orgId, deviceId);
|
|
482
|
+
const device = await this.db
|
|
483
|
+
.selectFrom("devices")
|
|
484
|
+
.select("source")
|
|
485
|
+
.where("id", "=", deviceId)
|
|
486
|
+
.executeTakeFirstOrThrow();
|
|
487
|
+
const { workingRoot, projectRoot } = await this.workingRootOf({
|
|
488
|
+
...input,
|
|
489
|
+
reach: projectReachOf(device.source, policy, owner?.restrictedProjectReach),
|
|
490
|
+
mode: policy.mode,
|
|
491
|
+
});
|
|
492
|
+
const grant = input.runId
|
|
493
|
+
? await this.grants.forExec({ orgId, deviceId, runId: input.runId })
|
|
494
|
+
: undefined;
|
|
495
|
+
const baseHosts = owner ? owner.allowedDomains : policy.localAllowedDomains;
|
|
496
|
+
const requested = [...baseHosts, ...(grant?.hosts ?? [])];
|
|
497
|
+
const allowedHosts = requested.filter((host) => (policy.localAllowedDomains.some((pattern) => coversHost(pattern, host)) ||
|
|
498
|
+
grant?.hosts.includes(host)) &&
|
|
499
|
+
(policy.network.kind === "open-public" ||
|
|
500
|
+
policy.network.hosts.some((pattern) => coversHost(pattern, host))));
|
|
501
|
+
return { policy, device, workingRoot, projectRoot, grant, allowedHosts };
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* The working root of the base authority (B1), decided exactly as the command review decides it
|
|
505
|
+
* (`devices/working-root.ts` `workingRootOf`, permission activation L2): the bound project's
|
|
506
|
+
* root when the root conversation's latest `project_root` snapshot is usable and names that
|
|
507
|
+
* root, else the recorded or reported workspace. A bound project before its first snapshot, an
|
|
508
|
+
* unavailable one, one outside the workspace without the os-user reach and one above the
|
|
509
|
+
* owner's home all work in the workspace, so a call the review judged against the workspace is
|
|
510
|
+
* not stale at admission. Narrowing only: no root other than the binding's or the workspace is
|
|
511
|
+
* ever named. `projectUsable` (a kernel read of the bound project) takes the binding as its
|
|
512
|
+
* snapshot. `projectRoot` is the bound project path when it is the working root, else null.
|
|
513
|
+
* Admission, the recorded permission context and the managed device context evidence all
|
|
514
|
+
* resolve it here, so none of them can name a wider root than admission.
|
|
515
|
+
*/
|
|
516
|
+
async workingRootOf(input) {
|
|
517
|
+
const { orgId, deviceId, root } = input;
|
|
518
|
+
const working = await this.facts.readWorkingFacts({
|
|
519
|
+
orgId,
|
|
520
|
+
rootConversationId: root.id,
|
|
521
|
+
holderConversationId: input.holderConversationId,
|
|
522
|
+
deviceId,
|
|
523
|
+
});
|
|
524
|
+
if (root.project_name &&
|
|
525
|
+
root.project_path &&
|
|
526
|
+
!(await this.facts.projectMatches(deviceId, root.project_name, root.project_path)))
|
|
527
|
+
throw new ApiError("conflict", "The conversation project binding changed");
|
|
528
|
+
if (!root.project_path)
|
|
529
|
+
return { workingRoot: working.workspace, projectRoot: null };
|
|
530
|
+
const bound = root.project_path;
|
|
531
|
+
const snapshot = input.projectUsable ? { root: bound, unavailable: false } : working.project;
|
|
532
|
+
// a snapshot of another root than the binding (an older binding's) never names the root
|
|
533
|
+
const usable = snapshot != null &&
|
|
534
|
+
!snapshot.unavailable &&
|
|
535
|
+
canonicalRoot(snapshot.root) === canonicalRoot(bound)
|
|
536
|
+
? { root: bound, unavailable: false }
|
|
537
|
+
: null;
|
|
538
|
+
const workingRoot = reviewWorkingRootOf(usable, {
|
|
539
|
+
mode: input.mode,
|
|
540
|
+
reach: input.reach,
|
|
541
|
+
workspace: working.workspace,
|
|
542
|
+
home: working.home,
|
|
543
|
+
});
|
|
544
|
+
const projectRoot = usable !== null && canonicalRoot(workingRoot) === canonicalRoot(bound) ? bound : null;
|
|
545
|
+
return { workingRoot, projectRoot };
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* The base working root of the calling conversation's tree on `deviceId` (see
|
|
549
|
+
* {@link workingRootOf}): read without locks as a managed operation's held evidence, and again
|
|
550
|
+
* under the complete authority subject by the managed device context writer, which compares the
|
|
551
|
+
* two. Throws `conflict` when the project binding no longer matches the device.
|
|
552
|
+
*/
|
|
553
|
+
async workingRootFor(input) {
|
|
554
|
+
const root = await this.profiles.readRoot(input.orgId, input.conversationId);
|
|
555
|
+
const { policy, owner } = await this.facts.readDevice(input.orgId, input.deviceId);
|
|
556
|
+
const device = await this.db
|
|
557
|
+
.selectFrom("devices")
|
|
558
|
+
.select("source")
|
|
559
|
+
.where("id", "=", input.deviceId)
|
|
560
|
+
.executeTakeFirstOrThrow();
|
|
561
|
+
return (await this.workingRootOf({
|
|
562
|
+
orgId: input.orgId,
|
|
563
|
+
deviceId: input.deviceId,
|
|
564
|
+
root,
|
|
565
|
+
holderConversationId: input.conversationId,
|
|
566
|
+
reach: projectReachOf(device.source, policy, owner?.restrictedProjectReach),
|
|
567
|
+
mode: policy.mode,
|
|
568
|
+
})).workingRoot;
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* The recorded permission context of a run's next request (B1): the same base facts admission
|
|
572
|
+
* reads, projected without any call, operation, grant expansion or provider effect. The caller
|
|
573
|
+
* already holds the complete authority subject (platform, org, device when selected, root) in
|
|
574
|
+
* this transaction; a subject those locks do not cover throws
|
|
575
|
+
* {@link PermissionContextSubjectChanged}. A missing or inaccessible actor, root or run throws
|
|
576
|
+
* (no request is sent); a device this actor cannot use is the logged access refusal; device
|
|
577
|
+
* states admission refuses are unsupported or unprobed contexts without a scope.
|
|
578
|
+
*/
|
|
579
|
+
async readContextLocked(input, signal) {
|
|
580
|
+
signal.throwIfAborted();
|
|
581
|
+
const root = await this.profiles.readOwnedRoot(input.orgId, input.conversationId, input.accountId);
|
|
582
|
+
const held = (kind, id) => {
|
|
583
|
+
try {
|
|
584
|
+
assertAuthorityHeld(kind, id);
|
|
585
|
+
return true;
|
|
586
|
+
}
|
|
587
|
+
catch {
|
|
588
|
+
return false;
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
if (!held("platform", "platform") ||
|
|
592
|
+
!held("org", input.orgId) ||
|
|
593
|
+
!held("root", root.id) ||
|
|
594
|
+
(root.device_id !== null && !held("device", root.device_id)))
|
|
595
|
+
throw new PermissionContextSubjectChanged();
|
|
596
|
+
if (!(await this.isActiveMember(input.orgId, input.accountId)))
|
|
597
|
+
throw new ApiError("forbidden", "This run's actor is no longer authorized");
|
|
598
|
+
await this.publication.assertCurrent();
|
|
599
|
+
const scheduled = (await this.runScheduledLocked(input.conversationId, input.accountId, input.runId)) ||
|
|
600
|
+
root.scheduled_task_id !== null;
|
|
601
|
+
const header = {
|
|
602
|
+
version: 1,
|
|
603
|
+
rootConversationId: root.id,
|
|
604
|
+
execution: scheduled ? "scheduled" : "interactive",
|
|
605
|
+
inherited: input.conversationId !== root.id,
|
|
606
|
+
};
|
|
607
|
+
const parse = (payload) => {
|
|
608
|
+
signal.throwIfAborted();
|
|
609
|
+
return PermissionContextV1Schema.parse(payload);
|
|
610
|
+
};
|
|
611
|
+
const { view, access } = await this.facts.viewWithAccess(input.orgId, input.accountId, root);
|
|
612
|
+
if (root.device_id === null || access === "no-device")
|
|
613
|
+
return parse({
|
|
614
|
+
...header,
|
|
615
|
+
profileRevision: root.permission_revision,
|
|
616
|
+
selectedProfile: root.permission_profile,
|
|
617
|
+
effectiveProfile: null,
|
|
618
|
+
deviceId: null,
|
|
619
|
+
scopeRevision: null,
|
|
620
|
+
authorityRevision: null,
|
|
621
|
+
availability: {
|
|
622
|
+
state: "unavailable",
|
|
623
|
+
cause: "no-device",
|
|
624
|
+
reason: "Choose a device to run device operations",
|
|
625
|
+
},
|
|
626
|
+
scope: null,
|
|
627
|
+
});
|
|
628
|
+
if (access === "actor-device-access")
|
|
629
|
+
return parse({
|
|
630
|
+
...header,
|
|
631
|
+
profileRevision: null,
|
|
632
|
+
selectedProfile: null,
|
|
633
|
+
effectiveProfile: null,
|
|
634
|
+
deviceId: null,
|
|
635
|
+
scopeRevision: null,
|
|
636
|
+
authorityRevision: null,
|
|
637
|
+
availability: {
|
|
638
|
+
state: "unavailable",
|
|
639
|
+
cause: "actor-device-access",
|
|
640
|
+
reason: "This run's actor cannot use the selected device",
|
|
641
|
+
},
|
|
642
|
+
scope: null,
|
|
643
|
+
});
|
|
644
|
+
const deviceId = root.device_id;
|
|
645
|
+
const scopeRevision = access === "provider-unavailable"
|
|
646
|
+
? (await this.facts.scope(input.orgId, deviceId, root.project_path, root.project_name))
|
|
647
|
+
.scopeRevision
|
|
648
|
+
: view.scopeRevision;
|
|
649
|
+
if (!scopeRevision)
|
|
650
|
+
throw new Error("Device permission scope revision is missing");
|
|
651
|
+
const vector = await this.epochs.readLocked({
|
|
652
|
+
orgId: input.orgId,
|
|
653
|
+
deviceId,
|
|
654
|
+
rootConversationId: root.id,
|
|
655
|
+
});
|
|
656
|
+
const effectiveProfile = scheduled ? "request-approval" : view.profile;
|
|
657
|
+
const known = {
|
|
658
|
+
...header,
|
|
659
|
+
profileRevision: root.permission_revision,
|
|
660
|
+
selectedProfile: root.permission_profile,
|
|
661
|
+
effectiveProfile,
|
|
662
|
+
deviceId,
|
|
663
|
+
scopeRevision,
|
|
664
|
+
authorityRevision: permissionScopeHash({ scopeRevision, vector }),
|
|
665
|
+
};
|
|
666
|
+
const without = (state, reason) => parse({ ...known, availability: { state, reason }, scope: null });
|
|
667
|
+
const availability = view.availability;
|
|
668
|
+
if (access === "provider-unavailable")
|
|
669
|
+
return without("unsupported", availability.state === "supported"
|
|
670
|
+
? "This device's provider is not available"
|
|
671
|
+
: availability.reason);
|
|
672
|
+
if (availability.state !== "supported")
|
|
673
|
+
return without(availability.state, availability.reason);
|
|
674
|
+
// states admission refuses before any operation: the device is not verified for new work
|
|
675
|
+
try {
|
|
676
|
+
await this.facts.assertCloudReconciled(input.orgId, deviceId);
|
|
677
|
+
await this.facts.assertOwnerReportUsable(input.orgId, deviceId);
|
|
678
|
+
}
|
|
679
|
+
catch (error) {
|
|
680
|
+
if (!(error instanceof ApiError))
|
|
681
|
+
throw error;
|
|
682
|
+
return without("unprobed", error.message);
|
|
683
|
+
}
|
|
684
|
+
let base;
|
|
685
|
+
try {
|
|
686
|
+
await this.facts.preview(input.orgId, input.accountId, deviceId, root.project_name ?? undefined);
|
|
687
|
+
base = await this.baseScopeLocked({
|
|
688
|
+
orgId: input.orgId,
|
|
689
|
+
deviceId,
|
|
690
|
+
root,
|
|
691
|
+
holderConversationId: input.conversationId,
|
|
692
|
+
runId: input.runId,
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
catch (error) {
|
|
696
|
+
if (!(error instanceof ApiError))
|
|
697
|
+
throw error;
|
|
698
|
+
return without("unsupported", error.message);
|
|
699
|
+
}
|
|
700
|
+
const applied = await this.facts.readAppliedEnvironmentLocked({
|
|
701
|
+
orgId: input.orgId,
|
|
702
|
+
deviceId,
|
|
703
|
+
});
|
|
704
|
+
const location = base.device.source === "local" ? "local" : "cloud";
|
|
705
|
+
// the hosts admission uses: within a cloud instance's applied outer boundary (B-15)
|
|
706
|
+
const allowedHosts = hostsWithinOuter(base.allowedHosts, applied?.outerNetwork);
|
|
707
|
+
// the verdict admission refuses with (B-16)
|
|
708
|
+
const withheld = withheldScope({
|
|
709
|
+
availability,
|
|
710
|
+
effectiveProfile,
|
|
711
|
+
location,
|
|
712
|
+
workingRoot: base.workingRoot,
|
|
713
|
+
projectPath: base.projectRoot,
|
|
714
|
+
applied,
|
|
715
|
+
});
|
|
716
|
+
if (withheld)
|
|
717
|
+
return without(withheld.state, withheld.reason);
|
|
718
|
+
if (applied === null)
|
|
719
|
+
throw new Error("A supported context needs its applied environment");
|
|
720
|
+
const full = effectiveProfile === "full-access";
|
|
721
|
+
if (full &&
|
|
722
|
+
(!view.fullAccessCeiling ||
|
|
723
|
+
view.consentScope?.deviceId !== deviceId ||
|
|
724
|
+
view.consentScope.scopeRevision !== scopeRevision))
|
|
725
|
+
throw new Error("Full access lost its ceiling or consent inside one authority read");
|
|
726
|
+
const hosts = [...new Set(allowedHosts)].sort(permissionCodePointCompare);
|
|
727
|
+
return parse({
|
|
728
|
+
...known,
|
|
729
|
+
availability: { state: "supported" },
|
|
730
|
+
scope: {
|
|
731
|
+
location,
|
|
732
|
+
workingRoot: base.workingRoot,
|
|
733
|
+
filesystem: full
|
|
734
|
+
? { kind: "device" }
|
|
735
|
+
: {
|
|
736
|
+
kind: "scoped",
|
|
737
|
+
readDirectories: [base.workingRoot],
|
|
738
|
+
writeDirectories: [base.workingRoot],
|
|
739
|
+
},
|
|
740
|
+
network: full
|
|
741
|
+
? { kind: "environment" }
|
|
742
|
+
: hosts.length
|
|
743
|
+
? { kind: "allowlist", hosts }
|
|
744
|
+
: { kind: "none" },
|
|
745
|
+
outerNetwork: applied.outerNetwork,
|
|
746
|
+
},
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
};
|
|
750
|
+
__decorate([
|
|
751
|
+
Transactional()
|
|
752
|
+
], SandboxOperationAuthority.prototype, "resolveBrowse", null);
|
|
753
|
+
__decorate([
|
|
754
|
+
Transactional()
|
|
755
|
+
], SandboxOperationAuthority.prototype, "previewLocked", null);
|
|
756
|
+
__decorate([
|
|
757
|
+
Transactional()
|
|
758
|
+
], SandboxOperationAuthority.prototype, "resolve", null);
|
|
759
|
+
__decorate([
|
|
760
|
+
Transactional()
|
|
761
|
+
], SandboxOperationAuthority.prototype, "resolveInvocation", null);
|
|
762
|
+
__decorate([
|
|
763
|
+
Transactional()
|
|
764
|
+
], SandboxOperationAuthority.prototype, "resolveKernelRead", null);
|
|
765
|
+
__decorate([
|
|
766
|
+
Transactional({ propagation: "mandatory" })
|
|
767
|
+
], SandboxOperationAuthority.prototype, "snapshot", null);
|
|
768
|
+
__decorate([
|
|
769
|
+
Transactional({ propagation: "mandatory" })
|
|
770
|
+
], SandboxOperationAuthority.prototype, "readContextLocked", null);
|
|
771
|
+
SandboxOperationAuthority = __decorate([
|
|
772
|
+
Injectable(),
|
|
773
|
+
__param(0, Inject(PermissionCallGrants)),
|
|
774
|
+
__param(1, Inject(PermissionConfigPublication)),
|
|
775
|
+
__param(2, Inject(KernelDb)),
|
|
776
|
+
__param(3, Inject(TransactionHost)),
|
|
777
|
+
__param(4, Inject(PermissionProfileStore)),
|
|
778
|
+
__param(5, Inject(PermissionFacts)),
|
|
779
|
+
__param(6, Inject(NetworkGrants)),
|
|
780
|
+
__param(7, Inject(PermissionAuthorityLock)),
|
|
781
|
+
__param(8, Inject(PermissionAuthorityEpochStore))
|
|
782
|
+
], SandboxOperationAuthority);
|
|
783
|
+
export { SandboxOperationAuthority };
|