@capybara-hq/kernel 0.1.0-rc.41 → 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 +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 +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,354 @@
|
|
|
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 { Inject, Injectable } from "@nestjs/common";
|
|
12
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
13
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
14
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
15
|
+
import { canonicalJson } from "../tools/canonical-json.js";
|
|
16
|
+
import { readInstanceLimits } from "./modes.js";
|
|
17
|
+
import { PermissionAuthorityLock } from "./permission-authority-lock.js";
|
|
18
|
+
import { unprobedPermissionAvailability, } from "./permission-availability.js";
|
|
19
|
+
import { LOCAL_PROVIDER } from "./providers.js";
|
|
20
|
+
export const sameRuntimeIdentity = (a, b) => permissionScopeHash(a) === permissionScopeHash(b);
|
|
21
|
+
const permissionScopeHash = (identity) => createHash("sha256")
|
|
22
|
+
.update(JSON.stringify([
|
|
23
|
+
identity.runtimeHash,
|
|
24
|
+
identity.helperHash,
|
|
25
|
+
identity.imageFingerprint,
|
|
26
|
+
identity.instanceGeneration,
|
|
27
|
+
]))
|
|
28
|
+
.digest("hex");
|
|
29
|
+
export function runtimeIdentity(value) {
|
|
30
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
31
|
+
throw new Error("Runtime identity is missing");
|
|
32
|
+
const v = value;
|
|
33
|
+
if (Object.keys(v).sort().join() !==
|
|
34
|
+
["helperHash", "imageFingerprint", "instanceGeneration", "runtimeHash"].join())
|
|
35
|
+
throw new Error("Incomplete runtime identity");
|
|
36
|
+
for (const key of ["runtimeHash", "helperHash", "imageFingerprint"])
|
|
37
|
+
if (typeof v[key] !== "string" || !/^[a-f0-9]{64}$/.test(v[key]))
|
|
38
|
+
throw new Error("Runtime identity requires verified hashes");
|
|
39
|
+
if (typeof v.instanceGeneration !== "string" ||
|
|
40
|
+
!v.instanceGeneration.length ||
|
|
41
|
+
v.instanceGeneration.length > 256)
|
|
42
|
+
throw new Error("Invalid runtime generation");
|
|
43
|
+
return { ...v };
|
|
44
|
+
}
|
|
45
|
+
const sha256 = (value) => createHash("sha256").update(canonicalJson(value)).digest("hex");
|
|
46
|
+
/**
|
|
47
|
+
* The kernel-built runtime identity of a cloud instance (B-15, Q1 option A). A cloud provider is
|
|
48
|
+
* an in-process extension (full trust), so its own permission support report, bound to these
|
|
49
|
+
* kernel facts, is the strongest evidence available without an SDK change: the provider plugin
|
|
50
|
+
* and version (`runtimeHash`), the committed instance handle (`helperHash`), the recorded applied
|
|
51
|
+
* limits with no provider-attested image digest (`imageFingerprint`) and the instance generation.
|
|
52
|
+
* Any of them changing makes a recorded report unprobed.
|
|
53
|
+
*/
|
|
54
|
+
export function cloudRuntimeIdentity(input) {
|
|
55
|
+
return {
|
|
56
|
+
runtimeHash: sha256({
|
|
57
|
+
kind: "cloud-provider",
|
|
58
|
+
provider: input.provider,
|
|
59
|
+
version: input.version,
|
|
60
|
+
}),
|
|
61
|
+
helperHash: sha256({ kind: "cloud-instance", handle: input.handle }),
|
|
62
|
+
imageFingerprint: sha256({
|
|
63
|
+
kind: "cloud-environment",
|
|
64
|
+
applied: {
|
|
65
|
+
mode: input.applied.mode,
|
|
66
|
+
network: {
|
|
67
|
+
kind: input.applied.network.kind,
|
|
68
|
+
hosts: [...new Set(input.applied.network.hosts)].sort(),
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
imageDigest: null,
|
|
72
|
+
}),
|
|
73
|
+
instanceGeneration: input.instanceGeneration,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A cloud provider's permission support copied field by field, or null when it is not a version 1
|
|
78
|
+
* report (an in-process extension is trusted, but its answer is stored and read back later, so
|
|
79
|
+
* nothing beyond the contract's fields is kept).
|
|
80
|
+
*/
|
|
81
|
+
export function cloudSupportOf(value) {
|
|
82
|
+
if (!value || typeof value !== "object")
|
|
83
|
+
return null;
|
|
84
|
+
const o = value;
|
|
85
|
+
if (o.version !== 1 || typeof o.fullAccess !== "boolean" || typeof o.background !== "boolean")
|
|
86
|
+
return null;
|
|
87
|
+
const r = o.restricted;
|
|
88
|
+
if (r !== null && (typeof r !== "object" || r === undefined))
|
|
89
|
+
return null;
|
|
90
|
+
if (r && !["filesystem", "network", "native", "concurrentScopes"].every((k) => r[k] === true))
|
|
91
|
+
return null;
|
|
92
|
+
return {
|
|
93
|
+
version: 1,
|
|
94
|
+
restricted: r
|
|
95
|
+
? { filesystem: true, network: true, native: true, concurrentScopes: true }
|
|
96
|
+
: null,
|
|
97
|
+
fullAccess: o.fullAccess,
|
|
98
|
+
background: o.background,
|
|
99
|
+
...(typeof o.unavailableReason === "string" && {
|
|
100
|
+
unavailableReason: o.unavailableReason.slice(0, 1000),
|
|
101
|
+
}),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The cloud identity the instance row proves now, or null when it proves none: no live instance
|
|
106
|
+
* of this provider, no handle or no recorded applied limits.
|
|
107
|
+
*/
|
|
108
|
+
function currentCloudIdentity(instance, provider, version) {
|
|
109
|
+
const applied = readInstanceLimits(instance.instance_limits).applied;
|
|
110
|
+
if (instance.provider !== provider ||
|
|
111
|
+
!instance.handle ||
|
|
112
|
+
applied === null ||
|
|
113
|
+
(instance.status !== "running" && instance.status !== "suspended"))
|
|
114
|
+
return null;
|
|
115
|
+
return cloudRuntimeIdentity({
|
|
116
|
+
provider,
|
|
117
|
+
version,
|
|
118
|
+
handle: instance.handle,
|
|
119
|
+
instanceGeneration: String(instance.created_at.getTime()),
|
|
120
|
+
applied,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* What a cloud instance can enforce of its provider's report (B-12, spec §124): Full access needs
|
|
125
|
+
* the instance itself to hold the outer boundary. An instance applied in the OS sandbox mode
|
|
126
|
+
* holds its outer network only through the restricted child filters that Full access leaves, so
|
|
127
|
+
* Full reads as unsupported there whatever the provider reports; restricted support stands.
|
|
128
|
+
*/
|
|
129
|
+
export function cloudEnforceable(report, instanceLimits) {
|
|
130
|
+
if (!report.fullAccess || readInstanceLimits(instanceLimits).applied?.mode === "auto")
|
|
131
|
+
return report;
|
|
132
|
+
return {
|
|
133
|
+
...report,
|
|
134
|
+
fullAccess: false,
|
|
135
|
+
unavailableReason: "This cloud instance enforces its outer network only inside the OS sandbox, so it cannot run Full access",
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/** No probe, provisioning or provider IO. A changed identity makes the previous report unprobed. */
|
|
139
|
+
export async function readRuntimeReport(db, deviceId, provider, version) {
|
|
140
|
+
return (await readCurrentReport(db, deviceId, provider, version)).availability;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* {@link readRuntimeReport} plus the identity hash of the current report (null unless it is
|
|
144
|
+
* current): a cloud device's installed evidence names its report by it.
|
|
145
|
+
*/
|
|
146
|
+
export async function readCurrentReport(db, deviceId, provider, version) {
|
|
147
|
+
const unprobed = {
|
|
148
|
+
availability: unprobedPermissionAvailability(),
|
|
149
|
+
identityHash: null,
|
|
150
|
+
instanceGeneration: null,
|
|
151
|
+
};
|
|
152
|
+
if (!version)
|
|
153
|
+
return unprobed;
|
|
154
|
+
const [device, instance] = await Promise.all([
|
|
155
|
+
db.selectFrom("devices").select("source").where("id", "=", deviceId).executeTakeFirst(),
|
|
156
|
+
db
|
|
157
|
+
.selectFrom("device_instances")
|
|
158
|
+
.select([
|
|
159
|
+
"provider",
|
|
160
|
+
"status",
|
|
161
|
+
"handle",
|
|
162
|
+
"created_at",
|
|
163
|
+
"instance_limits",
|
|
164
|
+
"permission_runtime_identity",
|
|
165
|
+
])
|
|
166
|
+
.where("device_id", "=", deviceId)
|
|
167
|
+
.executeTakeFirst(),
|
|
168
|
+
]);
|
|
169
|
+
if (!instance || instance.provider !== provider || !instance.permission_runtime_identity)
|
|
170
|
+
return unprobed;
|
|
171
|
+
// A cloud instance attests only through the kernel-built identity its current row proves (B-15):
|
|
172
|
+
// a changed provider version, handle, generation or applied limits is unprobed.
|
|
173
|
+
if (device?.source === "cloud") {
|
|
174
|
+
const current = currentCloudIdentity(instance, provider, version);
|
|
175
|
+
if (!current || !sameRuntimeIdentity(current, instance.permission_runtime_identity))
|
|
176
|
+
return unprobed;
|
|
177
|
+
}
|
|
178
|
+
// A local instance attests only through its installed post-commit binding (B4): an unbound or
|
|
179
|
+
// still awaiting generation, or a report differing from the bound identity, is unprobed.
|
|
180
|
+
if (provider === LOCAL_PROVIDER) {
|
|
181
|
+
const binding = await db
|
|
182
|
+
.selectFrom("device_instance_bindings")
|
|
183
|
+
.select(["status", "runtime_identity"])
|
|
184
|
+
.where("device_id", "=", deviceId)
|
|
185
|
+
.where("instance_generation", "=", String(instance.created_at.getTime()))
|
|
186
|
+
.executeTakeFirst();
|
|
187
|
+
if (binding?.status !== "installed" ||
|
|
188
|
+
!binding.runtime_identity ||
|
|
189
|
+
!sameRuntimeIdentity(binding.runtime_identity, instance.permission_runtime_identity))
|
|
190
|
+
return unprobed;
|
|
191
|
+
}
|
|
192
|
+
const row = await db
|
|
193
|
+
.selectFrom("device_permission_reports")
|
|
194
|
+
.selectAll()
|
|
195
|
+
.where("device_id", "=", deviceId)
|
|
196
|
+
.executeTakeFirst();
|
|
197
|
+
if (!row ||
|
|
198
|
+
row.provider !== provider ||
|
|
199
|
+
row.provider_version !== version ||
|
|
200
|
+
row.instance_generation !== String(instance.created_at.getTime()) ||
|
|
201
|
+
row.identity_hash !== permissionScopeHash(instance.permission_runtime_identity))
|
|
202
|
+
return unprobed;
|
|
203
|
+
const report = device?.source === "cloud"
|
|
204
|
+
? cloudEnforceable(row.report, instance.instance_limits)
|
|
205
|
+
: row.report;
|
|
206
|
+
if (!report.restricted && !report.fullAccess)
|
|
207
|
+
return {
|
|
208
|
+
availability: {
|
|
209
|
+
state: "unsupported",
|
|
210
|
+
reason: report.unavailableReason ?? "This runtime cannot enforce permission operations",
|
|
211
|
+
},
|
|
212
|
+
identityHash: row.identity_hash,
|
|
213
|
+
instanceGeneration: row.instance_generation,
|
|
214
|
+
};
|
|
215
|
+
return {
|
|
216
|
+
availability: { state: "supported", report },
|
|
217
|
+
identityHash: row.identity_hash,
|
|
218
|
+
instanceGeneration: row.instance_generation,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Whether a cloud device's current instance owes a new runtime report (B-15): it can prove a
|
|
223
|
+
* kernel-built identity (a live instance of this provider version with a handle and recorded
|
|
224
|
+
* applied limits) but no current report is recorded for it. Database reads only.
|
|
225
|
+
*/
|
|
226
|
+
export async function cloudReportOwed(db, deviceId, provider, version) {
|
|
227
|
+
if (!version)
|
|
228
|
+
return false;
|
|
229
|
+
const [device, instance] = await Promise.all([
|
|
230
|
+
db.selectFrom("devices").select("source").where("id", "=", deviceId).executeTakeFirst(),
|
|
231
|
+
db
|
|
232
|
+
.selectFrom("device_instances")
|
|
233
|
+
.select(["provider", "status", "handle", "created_at", "instance_limits"])
|
|
234
|
+
.where("device_id", "=", deviceId)
|
|
235
|
+
.executeTakeFirst(),
|
|
236
|
+
]);
|
|
237
|
+
if (device?.source !== "cloud" || !instance)
|
|
238
|
+
return false;
|
|
239
|
+
if (!currentCloudIdentity(instance, provider, version))
|
|
240
|
+
return false;
|
|
241
|
+
return (await readCurrentReport(db, deviceId, provider, version)).identityHash === null;
|
|
242
|
+
}
|
|
243
|
+
let PermissionRuntimeReports = class PermissionRuntimeReports {
|
|
244
|
+
db;
|
|
245
|
+
txHost;
|
|
246
|
+
locks;
|
|
247
|
+
constructor(db, txHost, locks) {
|
|
248
|
+
this.db = db;
|
|
249
|
+
this.txHost = txHost;
|
|
250
|
+
this.locks = locks;
|
|
251
|
+
}
|
|
252
|
+
/** Caller holds the device authority writer lock and validates authenticated hello/source identity. */
|
|
253
|
+
async record(deviceId, provider, version, identity, report) {
|
|
254
|
+
this.locks.assertHeld("device", deviceId, true);
|
|
255
|
+
const instance = await this.db
|
|
256
|
+
.selectFrom("device_instances")
|
|
257
|
+
.select(["created_at", "provider"])
|
|
258
|
+
.where("device_id", "=", deviceId)
|
|
259
|
+
.executeTakeFirst();
|
|
260
|
+
if (!instance || instance.provider !== provider)
|
|
261
|
+
return;
|
|
262
|
+
if (!identity) {
|
|
263
|
+
await this.db
|
|
264
|
+
.updateTable("device_instances")
|
|
265
|
+
.set({ permission_runtime_identity: null })
|
|
266
|
+
.where("device_id", "=", deviceId)
|
|
267
|
+
.execute();
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const checked = runtimeIdentity(identity);
|
|
271
|
+
if (checked.instanceGeneration !== String(instance.created_at.getTime()))
|
|
272
|
+
throw new Error("Runtime report belongs to another instance generation");
|
|
273
|
+
await this.db
|
|
274
|
+
.updateTable("device_instances")
|
|
275
|
+
.set({ permission_runtime_identity: JSON.stringify(checked) })
|
|
276
|
+
.where("device_id", "=", deviceId)
|
|
277
|
+
.execute();
|
|
278
|
+
await this.db
|
|
279
|
+
.insertInto("device_permission_reports")
|
|
280
|
+
.values({
|
|
281
|
+
device_id: deviceId,
|
|
282
|
+
provider,
|
|
283
|
+
provider_version: version,
|
|
284
|
+
instance_generation: checked.instanceGeneration,
|
|
285
|
+
identity_hash: permissionScopeHash(checked),
|
|
286
|
+
report: JSON.stringify(report),
|
|
287
|
+
})
|
|
288
|
+
.onConflict((oc) => oc.column("device_id").doUpdateSet({
|
|
289
|
+
provider,
|
|
290
|
+
provider_version: version,
|
|
291
|
+
instance_generation: checked.instanceGeneration,
|
|
292
|
+
identity_hash: permissionScopeHash(checked),
|
|
293
|
+
report: JSON.stringify(report),
|
|
294
|
+
observed_at: new Date(),
|
|
295
|
+
}))
|
|
296
|
+
.execute();
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Records a cloud instance's permission support with its kernel-built identity
|
|
300
|
+
* ({@link cloudRuntimeIdentity}, B-15), read from the committed row under the caller's device
|
|
301
|
+
* authority writer lock: the lifecycle holder calls it after create (in the handle commit's
|
|
302
|
+
* transaction) or after resume and limits changes, with the support it read from that exact
|
|
303
|
+
* instance under its lease. A local device, an instance of another provider, one without a
|
|
304
|
+
* handle or without recorded applied limits records nothing new (and clears a stale identity).
|
|
305
|
+
*/
|
|
306
|
+
async recordCloud(deviceId, provider, version, report) {
|
|
307
|
+
this.locks.assertHeld("device", deviceId, true);
|
|
308
|
+
const device = await this.db
|
|
309
|
+
.selectFrom("devices")
|
|
310
|
+
.select("source")
|
|
311
|
+
.where("id", "=", deviceId)
|
|
312
|
+
.executeTakeFirst();
|
|
313
|
+
if (device?.source !== "cloud")
|
|
314
|
+
return;
|
|
315
|
+
const instance = await this.db
|
|
316
|
+
.selectFrom("device_instances")
|
|
317
|
+
.select(["provider", "status", "handle", "created_at", "instance_limits"])
|
|
318
|
+
.where("device_id", "=", deviceId)
|
|
319
|
+
.executeTakeFirst();
|
|
320
|
+
if (!instance || instance.provider !== provider)
|
|
321
|
+
return;
|
|
322
|
+
const identity = currentCloudIdentity(instance, provider, version);
|
|
323
|
+
await this.record(deviceId, provider, version, identity ?? undefined, report);
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Clears a cloud instance's recorded identity (its report then reads unprobed). Caller holds
|
|
327
|
+
* the device authority writer lock and has checked the instance it probed.
|
|
328
|
+
*/
|
|
329
|
+
async clearCloud(deviceId, provider) {
|
|
330
|
+
this.locks.assertHeld("device", deviceId, true);
|
|
331
|
+
await this.db
|
|
332
|
+
.updateTable("device_instances")
|
|
333
|
+
.set({ permission_runtime_identity: null })
|
|
334
|
+
.where("device_id", "=", deviceId)
|
|
335
|
+
.where("provider", "=", provider)
|
|
336
|
+
.execute();
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
__decorate([
|
|
340
|
+
Transactional({ propagation: "mandatory" })
|
|
341
|
+
], PermissionRuntimeReports.prototype, "record", null);
|
|
342
|
+
__decorate([
|
|
343
|
+
Transactional({ propagation: "mandatory" })
|
|
344
|
+
], PermissionRuntimeReports.prototype, "recordCloud", null);
|
|
345
|
+
__decorate([
|
|
346
|
+
Transactional({ propagation: "mandatory" })
|
|
347
|
+
], PermissionRuntimeReports.prototype, "clearCloud", null);
|
|
348
|
+
PermissionRuntimeReports = __decorate([
|
|
349
|
+
Injectable(),
|
|
350
|
+
__param(0, Inject(KernelDb)),
|
|
351
|
+
__param(1, Inject(TransactionHost)),
|
|
352
|
+
__param(2, Inject(PermissionAuthorityLock))
|
|
353
|
+
], PermissionRuntimeReports);
|
|
354
|
+
export { PermissionRuntimeReports };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { PermissionProfile } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { EventLog } from "../conversations/event-log.js";
|
|
3
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
5
|
+
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
6
|
+
import { type AuthorityLevel, type AuthoritySubject, PermissionAuthorityLock } from "./permission-authority-lock.js";
|
|
7
|
+
export declare const PERMISSION_APPROVAL_RECHECK = "permission-approval-recheck";
|
|
8
|
+
export type PermissionTransition = {
|
|
9
|
+
orgId: string;
|
|
10
|
+
rootConversationId: string;
|
|
11
|
+
deviceId: string | null;
|
|
12
|
+
oldProfile: PermissionProfile;
|
|
13
|
+
newProfile: PermissionProfile;
|
|
14
|
+
revision: number;
|
|
15
|
+
scopeRevision: string | null;
|
|
16
|
+
actorAccountId: string | null;
|
|
17
|
+
reason: "user" | "authority-changed";
|
|
18
|
+
};
|
|
19
|
+
/** Canonical lower write: never takes descendant authority or imports the tools domain. */
|
|
20
|
+
export declare class PermissionTransitions {
|
|
21
|
+
private readonly db;
|
|
22
|
+
readonly txHost: TransactionHost;
|
|
23
|
+
private readonly locks;
|
|
24
|
+
private readonly events;
|
|
25
|
+
private readonly outbox;
|
|
26
|
+
constructor(db: KernelDb, txHost: TransactionHost, locks: PermissionAuthorityLock, events: EventLog, outbox: OutboxService);
|
|
27
|
+
recordLocked(change: PermissionTransition, writer: {
|
|
28
|
+
subject: AuthoritySubject;
|
|
29
|
+
level: AuthorityLevel;
|
|
30
|
+
}): Promise<void>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
import { EventLog } from "../conversations/event-log.js";
|
|
12
|
+
import { audit } from "../identity/audit.js";
|
|
13
|
+
import { afterCommit } from "../infra/database/after-commit.js";
|
|
14
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
15
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
16
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
17
|
+
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
18
|
+
import { PermissionAuthorityLock, } from "./permission-authority-lock.js";
|
|
19
|
+
export const PERMISSION_APPROVAL_RECHECK = "permission-approval-recheck";
|
|
20
|
+
/** Canonical lower write: never takes descendant authority or imports the tools domain. */
|
|
21
|
+
let PermissionTransitions = class PermissionTransitions {
|
|
22
|
+
db;
|
|
23
|
+
txHost;
|
|
24
|
+
locks;
|
|
25
|
+
events;
|
|
26
|
+
outbox;
|
|
27
|
+
constructor(db, txHost, locks, events, outbox) {
|
|
28
|
+
this.db = db;
|
|
29
|
+
this.txHost = txHost;
|
|
30
|
+
this.locks = locks;
|
|
31
|
+
this.events = events;
|
|
32
|
+
this.outbox = outbox;
|
|
33
|
+
}
|
|
34
|
+
async recordLocked(change, writer) {
|
|
35
|
+
const id = writer.level === "platform"
|
|
36
|
+
? "platform"
|
|
37
|
+
: writer.level === "org"
|
|
38
|
+
? writer.subject.orgId
|
|
39
|
+
: writer.level === "device"
|
|
40
|
+
? writer.subject.deviceId
|
|
41
|
+
: writer.subject.rootConversationId;
|
|
42
|
+
if (!id)
|
|
43
|
+
throw new Error("Permission transition requires the actual writer subject");
|
|
44
|
+
this.locks.assertHeld(writer.level, id, true);
|
|
45
|
+
if ((writer.level !== "platform" && writer.subject.orgId !== change.orgId) ||
|
|
46
|
+
(writer.level === "device" && writer.subject.deviceId !== change.deviceId) ||
|
|
47
|
+
(writer.level === "root" && writer.subject.rootConversationId !== change.rootConversationId))
|
|
48
|
+
throw new Error("Permission transition does not belong to this writer");
|
|
49
|
+
const root = await this.db
|
|
50
|
+
.selectFrom("conversations")
|
|
51
|
+
.selectAll()
|
|
52
|
+
.where("id", "=", change.rootConversationId)
|
|
53
|
+
.where("org_id", "=", change.orgId)
|
|
54
|
+
.executeTakeFirstOrThrow();
|
|
55
|
+
if (root.parent_conversation_id ||
|
|
56
|
+
root.permission_revision !== change.revision ||
|
|
57
|
+
root.permission_profile !== change.newProfile ||
|
|
58
|
+
root.device_id !== change.deviceId)
|
|
59
|
+
throw new Error("Permission transition does not match the persisted root");
|
|
60
|
+
const tree = await this.db
|
|
61
|
+
.withRecursive("tree(id)", (db) => db
|
|
62
|
+
.selectFrom("conversations")
|
|
63
|
+
.select("id")
|
|
64
|
+
.where("id", "=", change.rootConversationId)
|
|
65
|
+
.unionAll((q) => q
|
|
66
|
+
.selectFrom("conversations as c")
|
|
67
|
+
.innerJoin("tree", "tree.id", "c.parent_conversation_id")
|
|
68
|
+
.select("c.id")
|
|
69
|
+
.where("c.org_id", "=", change.orgId)))
|
|
70
|
+
.selectFrom("conversations")
|
|
71
|
+
.select("id")
|
|
72
|
+
.where("id", "in", (q) => q.selectFrom("tree").select("id"))
|
|
73
|
+
.orderBy("id")
|
|
74
|
+
.forUpdate()
|
|
75
|
+
.execute();
|
|
76
|
+
const { orgId, rootConversationId, ...detail } = change;
|
|
77
|
+
const toSeq = new Map();
|
|
78
|
+
const rootEvents = await this.events.appendIn(rootConversationId, [
|
|
79
|
+
{
|
|
80
|
+
type: "permission_profile_changed",
|
|
81
|
+
idemKey: `permission-profile-${rootConversationId}-${change.revision}`,
|
|
82
|
+
payload: detail,
|
|
83
|
+
},
|
|
84
|
+
]);
|
|
85
|
+
for (const event of rootEvents)
|
|
86
|
+
toSeq.set(rootConversationId, Math.max(toSeq.get(rootConversationId) ?? 0, event.seq));
|
|
87
|
+
await audit(this.db.db, {
|
|
88
|
+
orgId,
|
|
89
|
+
actorAccountId: change.actorAccountId,
|
|
90
|
+
action: "conversation.permission_profile_changed",
|
|
91
|
+
target: rootConversationId,
|
|
92
|
+
detail: { ...detail, writer },
|
|
93
|
+
});
|
|
94
|
+
const cards = await this.db
|
|
95
|
+
.selectFrom("approvals")
|
|
96
|
+
.selectAll()
|
|
97
|
+
.where("org_id", "=", orgId)
|
|
98
|
+
.where("conversation_id", "in", tree.map((c) => c.id))
|
|
99
|
+
.where("kind", "=", "command")
|
|
100
|
+
.where("status", "=", "pending")
|
|
101
|
+
.orderBy("conversation_id")
|
|
102
|
+
.orderBy("id")
|
|
103
|
+
.forUpdate()
|
|
104
|
+
.execute();
|
|
105
|
+
for (const card of cards) {
|
|
106
|
+
await this.db
|
|
107
|
+
.updateTable("approvals")
|
|
108
|
+
.set({
|
|
109
|
+
status: "superseded",
|
|
110
|
+
superseded_revision: change.revision,
|
|
111
|
+
superseded_reason: change.reason,
|
|
112
|
+
})
|
|
113
|
+
.where("id", "=", card.id)
|
|
114
|
+
.execute();
|
|
115
|
+
const cardEvents = await this.events.appendIn(card.conversation_id, [
|
|
116
|
+
{
|
|
117
|
+
type: "approval_decided",
|
|
118
|
+
runId: card.run_id,
|
|
119
|
+
idemKey: `approval_decided:${card.id}`,
|
|
120
|
+
payload: {
|
|
121
|
+
approvalId: card.id,
|
|
122
|
+
callId: card.call_id,
|
|
123
|
+
decision: "superseded",
|
|
124
|
+
byAccountId: null,
|
|
125
|
+
reason: change.reason,
|
|
126
|
+
revision: change.revision,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
]);
|
|
130
|
+
for (const event of cardEvents)
|
|
131
|
+
toSeq.set(card.conversation_id, Math.max(toSeq.get(card.conversation_id) ?? 0, event.seq));
|
|
132
|
+
}
|
|
133
|
+
await this.outbox.add({
|
|
134
|
+
queue: "cap-kernel-maintenance",
|
|
135
|
+
name: PERMISSION_APPROVAL_RECHECK,
|
|
136
|
+
jobId: `permission-recheck-${rootConversationId}-${change.revision}`,
|
|
137
|
+
data: { orgId, rootConversationId, revision: change.revision },
|
|
138
|
+
orgId,
|
|
139
|
+
});
|
|
140
|
+
for (const [conversationId, seq] of toSeq)
|
|
141
|
+
afterCommit(() => this.events.notify(conversationId, seq));
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
__decorate([
|
|
145
|
+
Transactional({ propagation: "mandatory" })
|
|
146
|
+
], PermissionTransitions.prototype, "recordLocked", null);
|
|
147
|
+
PermissionTransitions = __decorate([
|
|
148
|
+
Injectable(),
|
|
149
|
+
__param(0, Inject(KernelDb)),
|
|
150
|
+
__param(1, Inject(TransactionHost)),
|
|
151
|
+
__param(2, Inject(PermissionAuthorityLock)),
|
|
152
|
+
__param(3, Inject(EventLog)),
|
|
153
|
+
__param(4, Inject(OutboxService))
|
|
154
|
+
], PermissionTransitions);
|
|
155
|
+
export { PermissionTransitions };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { KernelSchemaDb } from "../db/kernel-schema.js";
|
|
3
|
+
import type { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
4
|
+
import type { KernelLogger } from "../log.js";
|
|
5
|
+
/**
|
|
6
|
+
* The platform's Full permission switch (permission runtime cutover B1, replacing the auto-mode
|
|
7
|
+
* switch): whether a device's owner ceiling may allow Full permission. It is both a startup config
|
|
8
|
+
* field (`devices.fullAccess`) and a deployment setting ({@link DEVICES_FULL_ACCESS_SETTING}), read
|
|
9
|
+
* live at every use: environment > config file / Nacos > the super admin's choice in the admin
|
|
10
|
+
* console > {@link DEVICES_FULL_ACCESS_DEFAULT}. A field set in the startup config is read-only in
|
|
11
|
+
* the admin console. An invalid stored value reads as `off` with a warning (fail closed): a value
|
|
12
|
+
* nobody could set must never widen a device.
|
|
13
|
+
*/
|
|
14
|
+
export type FullAccessPermission = "allowed" | "off";
|
|
15
|
+
/** The `deployment_settings` key of the super admin's choice. */
|
|
16
|
+
export declare const DEVICES_FULL_ACCESS_SETTING = "devicesFullAccess";
|
|
17
|
+
/**
|
|
18
|
+
* The key the auto-mode switch was stored under. Only the one-time conversion in the permission
|
|
19
|
+
* configuration publication reads it ({@link convertStoredPermissionKeys}).
|
|
20
|
+
*/
|
|
21
|
+
export declare const LEGACY_DEVICES_AUTO_MODE_SETTING = "devicesAutoMode";
|
|
22
|
+
/** What a deployment with neither the startup config nor the stored choice says: allowed. */
|
|
23
|
+
export declare const DEVICES_FULL_ACCESS_DEFAULT: FullAccessPermission;
|
|
24
|
+
/** The stored choice: `permission` absent follows the default. */
|
|
25
|
+
export declare const DevicesFullAccessStoredSchema: z.ZodObject<{
|
|
26
|
+
permission: z.ZodOptional<z.ZodEnum<{
|
|
27
|
+
allowed: "allowed";
|
|
28
|
+
off: "off";
|
|
29
|
+
}>>;
|
|
30
|
+
}, z.core.$strict>;
|
|
31
|
+
export type DevicesFullAccessStored = z.infer<typeof DevicesFullAccessStoredSchema>;
|
|
32
|
+
/**
|
|
33
|
+
* A change of the stored choice (`PUT /api/v1/admin/settings` `devicesFullAccess`): `permission:
|
|
34
|
+
* null` clears it, back to {@link DEVICES_FULL_ACCESS_DEFAULT}.
|
|
35
|
+
*/
|
|
36
|
+
export declare const DEVICES_FULL_ACCESS_PATCH: z.ZodObject<{
|
|
37
|
+
permission: z.ZodNullable<z.ZodEnum<{
|
|
38
|
+
allowed: "allowed";
|
|
39
|
+
off: "off";
|
|
40
|
+
}>>;
|
|
41
|
+
}, z.core.$strict>;
|
|
42
|
+
export type DevicesFullAccessPatch = z.infer<typeof DEVICES_FULL_ACCESS_PATCH>;
|
|
43
|
+
/**
|
|
44
|
+
* The old public patch (`devicesAutoMode: { mode }`), still accepted until the old public inputs
|
|
45
|
+
* are removed; {@link fullAccessPatchOfLegacy} maps it exactly.
|
|
46
|
+
*/
|
|
47
|
+
export declare const DEVICES_AUTO_MODE_PATCH: z.ZodObject<{
|
|
48
|
+
mode: z.ZodNullable<z.ZodEnum<{
|
|
49
|
+
allowed: "allowed";
|
|
50
|
+
off: "off";
|
|
51
|
+
}>>;
|
|
52
|
+
}, z.core.$strict>;
|
|
53
|
+
export type DevicesAutoModePatch = z.infer<typeof DEVICES_AUTO_MODE_PATCH>;
|
|
54
|
+
export declare const fullAccessPatchOfLegacy: (patch: DevicesAutoModePatch) => DevicesFullAccessPatch;
|
|
55
|
+
/** Where the effective switch comes from. */
|
|
56
|
+
export type DevicesFullAccessSource = "env" | "file" | "nacos" | "db" | "default";
|
|
57
|
+
/** The admin console's view: the effective permission and where it comes from. */
|
|
58
|
+
export interface DevicesFullAccessView {
|
|
59
|
+
permission: FullAccessPermission;
|
|
60
|
+
source: DevicesFullAccessSource;
|
|
61
|
+
}
|
|
62
|
+
/** The old public view (`devicesAutoMode`, `deployment.autoMode`), kept until it is removed. */
|
|
63
|
+
export interface DevicesAutoModeView {
|
|
64
|
+
mode: FullAccessPermission;
|
|
65
|
+
source: DevicesFullAccessSource;
|
|
66
|
+
}
|
|
67
|
+
export declare const legacyAutoModeView: (view: DevicesFullAccessView) => DevicesAutoModeView;
|
|
68
|
+
/**
|
|
69
|
+
* Parses a stored choice: missing -> `{}`; invalid -> `{ permission: "off" }` with a warning,
|
|
70
|
+
* narrower, never wider.
|
|
71
|
+
*/
|
|
72
|
+
export declare function parseStoredDevicesFullAccess(value: unknown, log?: Pick<KernelLogger, "warn">): DevicesFullAccessStored;
|
|
73
|
+
/**
|
|
74
|
+
* Parses a stored old auto-mode choice into the new shape: `{ mode }` maps to `{ permission }`, an
|
|
75
|
+
* empty choice stays empty, an invalid one becomes an explicit `off` with a warning, never absent.
|
|
76
|
+
*/
|
|
77
|
+
export declare function parseLegacyStoredDevicesAutoMode(value: unknown, log?: Pick<KernelLogger, "warn">): DevicesFullAccessStored;
|
|
78
|
+
/** The stored choice with `patch` applied (`permission: null` clears it). */
|
|
79
|
+
export declare function applyDevicesFullAccessPatch(_stored: DevicesFullAccessStored, patch: DevicesFullAccessPatch): DevicesFullAccessStored;
|
|
80
|
+
/**
|
|
81
|
+
* The effective switch and its source, pure (the reader below adds the database read). The startup
|
|
82
|
+
* config field is optional and has no schema default, so a defined `configured` value means the
|
|
83
|
+
* config source (environment, file or Nacos) sets it: it wins over the stored choice.
|
|
84
|
+
*/
|
|
85
|
+
export declare function resolveDevicesFullAccess(configured: FullAccessPermission | undefined, configSource: Exclude<DevicesFullAccessSource, "db" | "default"> | undefined, stored: DevicesFullAccessStored): DevicesFullAccessView;
|
|
86
|
+
/**
|
|
87
|
+
* The narrower of two stored choices: `off` when either says off; otherwise an explicit value
|
|
88
|
+
* beats an absent one only when it is narrower, so two non-off choices read as `a`.
|
|
89
|
+
*/
|
|
90
|
+
export declare const narrowerStoredFullAccess: (a: DevicesFullAccessStored, b: DevicesFullAccessStored) => DevicesFullAccessStored;
|
|
91
|
+
/**
|
|
92
|
+
* The stored choice, read live (in the caller's transaction when there is one). Until the old
|
|
93
|
+
* public inputs are removed (8.6) it reads both keys and takes the narrower (fail closed): before
|
|
94
|
+
* the publication converts them, or after an old replica wrote its own key again, a stored
|
|
95
|
+
* `devicesAutoMode` off is never lost.
|
|
96
|
+
*/
|
|
97
|
+
export declare function readStoredDevicesFullAccess(db: KernelSchemaDb, log?: Pick<KernelLogger, "warn">): Promise<DevicesFullAccessStored>;
|
|
98
|
+
/** Whether the old key is still stored (a write then keeps it in step for old replicas). */
|
|
99
|
+
export declare function legacyDevicesAutoModeStored(db: KernelSchemaDb): Promise<boolean>;
|
|
100
|
+
/**
|
|
101
|
+
* The org's stored Full switch read over both keys until 8.6 (fail closed): `fullAccess` and the
|
|
102
|
+
* old `autoMode` (an invalid old value reads as off); the narrower wins, an absent old key leaves
|
|
103
|
+
* `fullAccess` as it is.
|
|
104
|
+
*/
|
|
105
|
+
export declare function narrowedOrgFullAccess(fullAccess: FullAccessPermission | undefined, rawLegacy: unknown): FullAccessPermission | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* The platform's effective Full switch, read live from PG at every use (so a change applies on
|
|
108
|
+
* every replica at once): see {@link resolveDevicesFullAccess}.
|
|
109
|
+
*/
|
|
110
|
+
export declare function readDevicesFullAccess(db: KernelSchemaDb, config: Pick<KernelConfigService, "get" | "fieldSource">, log?: Pick<KernelLogger, "warn">): Promise<DevicesFullAccessView>;
|
|
111
|
+
/**
|
|
112
|
+
* Locks the org rows {@link convertStoredPermissionKeys} would convert, in id order, before the
|
|
113
|
+
* caller takes the platform authority write lock: the global lock order puts the org row before
|
|
114
|
+
* the permission authority locks (a fenced prelude holds an org row while it waits for its shared
|
|
115
|
+
* platform lock). The conversion itself then finds its rows already held.
|
|
116
|
+
*/
|
|
117
|
+
export declare function lockOrgsWithStoredPermissionKeys(db: KernelSchemaDb): Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* The one-time conversion of the stored permission keys (B1), run only inside the permission
|
|
120
|
+
* configuration publication transaction under the platform authority write lock (which excludes
|
|
121
|
+
* every org and device writer; the caller locked the org rows to convert before it,
|
|
122
|
+
* {@link lockOrgsWithStoredPermissionKeys}): the deployment setting `devicesAutoMode` becomes
|
|
123
|
+
* `devicesFullAccess`, and every org's `autoMode` becomes `fullAccess` (absent stays absent:
|
|
124
|
+
* the org follows the platform). Values convert exactly; an invalid old value converts to an
|
|
125
|
+
* explicit `off` with a warning. An old and a new value that differ stop the conversion (the
|
|
126
|
+
* caller's transaction rolls back): never pick the wider one. Idempotent.
|
|
127
|
+
*/
|
|
128
|
+
export declare function convertStoredPermissionKeys(db: KernelSchemaDb, log?: Pick<KernelLogger, "warn">): Promise<void>;
|