@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,21 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { Module } from "@nestjs/common";
|
|
8
|
+
import { OperationAuthorityModule } from "./operation-authority.module.js";
|
|
9
|
+
import { SandboxOperationIntents } from "./operation-intents.js";
|
|
10
|
+
import { OperationPayloads } from "./operation-payloads.js";
|
|
11
|
+
import { PermissionAuthorityModule } from "./permission-authority.module.js";
|
|
12
|
+
let OperationIntentsModule = class OperationIntentsModule {
|
|
13
|
+
};
|
|
14
|
+
OperationIntentsModule = __decorate([
|
|
15
|
+
Module({
|
|
16
|
+
imports: [OperationAuthorityModule, PermissionAuthorityModule],
|
|
17
|
+
providers: [OperationPayloads, SandboxOperationIntents],
|
|
18
|
+
exports: [SandboxOperationIntents],
|
|
19
|
+
})
|
|
20
|
+
], OperationIntentsModule);
|
|
21
|
+
export { OperationIntentsModule };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
2
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
3
|
+
export declare const OPERATION_CHUNK_BYTES = 65536;
|
|
4
|
+
type Slot = "input" | "result";
|
|
5
|
+
export interface PayloadDigest {
|
|
6
|
+
digest: string;
|
|
7
|
+
bytes: number;
|
|
8
|
+
chunks: number;
|
|
9
|
+
inline: string | null;
|
|
10
|
+
}
|
|
11
|
+
interface PayloadRef {
|
|
12
|
+
version: 1;
|
|
13
|
+
slot: Slot;
|
|
14
|
+
digest: string;
|
|
15
|
+
bytes: number;
|
|
16
|
+
chunks: number;
|
|
17
|
+
}
|
|
18
|
+
export declare function operationPayloadChunks(value: unknown): Generator<Buffer>;
|
|
19
|
+
export declare function describeOperationPayload(value: unknown): PayloadDigest;
|
|
20
|
+
export declare function payloadReference(slot: Slot, p: PayloadDigest): string;
|
|
21
|
+
export declare function isPayloadReference(value: unknown): value is {
|
|
22
|
+
$operationPayload: PayloadRef;
|
|
23
|
+
};
|
|
24
|
+
/** PG-only storage: bounded bytea rows/batches share the ledger transaction and retention. */
|
|
25
|
+
export declare class OperationPayloads {
|
|
26
|
+
private readonly db;
|
|
27
|
+
readonly txHost: TransactionHost;
|
|
28
|
+
constructor(db: KernelDb, txHost: TransactionHost);
|
|
29
|
+
save(id: string, slot: Slot, value: unknown, described?: PayloadDigest): Promise<string>;
|
|
30
|
+
load(id: string, slot: Slot, value: unknown): Promise<unknown>;
|
|
31
|
+
expire(id: string): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,332 @@
|
|
|
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 { SandboxOperationOutcomeUnknownError } from "@capybara-hq/plugin-sdk";
|
|
12
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
13
|
+
import { sql } from "kysely";
|
|
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
|
+
export const OPERATION_CHUNK_BYTES = 65536;
|
|
18
|
+
const INLINE_BYTES = 131072;
|
|
19
|
+
const header = (tag, length) => {
|
|
20
|
+
const b = Buffer.alloc(length === undefined ? 1 : 9);
|
|
21
|
+
b[0] = tag;
|
|
22
|
+
if (length !== undefined)
|
|
23
|
+
b.writeDoubleLE(length, 1);
|
|
24
|
+
return b;
|
|
25
|
+
};
|
|
26
|
+
/** Binary framing avoids whole-file base64/JSON allocation; UTF-16 preserves every JS code unit. */
|
|
27
|
+
function* frames(value, parents = new Set()) {
|
|
28
|
+
if (value === null || value === undefined) {
|
|
29
|
+
yield header(0);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (typeof value === "boolean") {
|
|
33
|
+
yield header(value ? 1 : 2);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (typeof value === "number") {
|
|
37
|
+
if (!Number.isFinite(value))
|
|
38
|
+
throw new TypeError("Non-finite operation payload");
|
|
39
|
+
yield header(3, value);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (typeof value === "string") {
|
|
43
|
+
yield header(4, value.length * 2);
|
|
44
|
+
for (let i = 0; i < value.length; i += 16384)
|
|
45
|
+
yield Buffer.from(value.slice(i, i + 16384), "utf16le");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (value instanceof Uint8Array) {
|
|
49
|
+
yield header(5, value.byteLength);
|
|
50
|
+
for (let i = 0; i < value.length; i += OPERATION_CHUNK_BYTES)
|
|
51
|
+
yield value.subarray(i, i + OPERATION_CHUNK_BYTES);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (typeof value !== "object" || parents.has(value))
|
|
55
|
+
throw new TypeError("Operation payload must be an acyclic native value");
|
|
56
|
+
parents.add(value);
|
|
57
|
+
try {
|
|
58
|
+
if (Array.isArray(value)) {
|
|
59
|
+
yield header(6, value.length);
|
|
60
|
+
for (const v of value)
|
|
61
|
+
yield* frames(v, parents);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const proto = Object.getPrototypeOf(value);
|
|
65
|
+
if (proto !== Object.prototype && proto !== null)
|
|
66
|
+
throw new TypeError("Unsupported operation payload object");
|
|
67
|
+
const object = value;
|
|
68
|
+
const keys = Object.keys(object)
|
|
69
|
+
.filter((key) => object[key] !== undefined)
|
|
70
|
+
.sort();
|
|
71
|
+
yield header(7, keys.length);
|
|
72
|
+
for (const key of keys) {
|
|
73
|
+
yield* frames(key, parents);
|
|
74
|
+
yield* frames(object[key], parents);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
parents.delete(value);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export function* operationPayloadChunks(value) {
|
|
83
|
+
let chunk = Buffer.alloc(OPERATION_CHUNK_BYTES), used = 0;
|
|
84
|
+
for (const frame of frames(value))
|
|
85
|
+
for (let offset = 0; offset < frame.byteLength;) {
|
|
86
|
+
const n = Math.min(chunk.length - used, frame.byteLength - offset);
|
|
87
|
+
chunk.set(frame.subarray(offset, offset + n), used);
|
|
88
|
+
used += n;
|
|
89
|
+
offset += n;
|
|
90
|
+
if (used === chunk.length) {
|
|
91
|
+
yield chunk;
|
|
92
|
+
chunk = Buffer.alloc(OPERATION_CHUNK_BYTES);
|
|
93
|
+
used = 0;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (used)
|
|
97
|
+
yield chunk.subarray(0, used);
|
|
98
|
+
}
|
|
99
|
+
function inlineValue(value) {
|
|
100
|
+
if (value instanceof Uint8Array)
|
|
101
|
+
return {
|
|
102
|
+
$bytes: Buffer.from(value.buffer, value.byteOffset, value.byteLength).toString("base64"),
|
|
103
|
+
};
|
|
104
|
+
if (Array.isArray(value))
|
|
105
|
+
return value.map(inlineValue);
|
|
106
|
+
if (value && typeof value === "object")
|
|
107
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, inlineValue(v)]));
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
/** JSON text jsonb refuses: an escaped NUL or an escaped (hence lone) surrogate. */
|
|
111
|
+
const JSONB_UNSAFE = /\\u(0000|d[89a-f][0-9a-f]{2})/i;
|
|
112
|
+
export function describeOperationPayload(value) {
|
|
113
|
+
const hash = createHash("sha256");
|
|
114
|
+
let bytes = 0, chunks = 0;
|
|
115
|
+
for (const chunk of operationPayloadChunks(value)) {
|
|
116
|
+
hash.update(chunk);
|
|
117
|
+
bytes += chunk.length;
|
|
118
|
+
chunks++;
|
|
119
|
+
}
|
|
120
|
+
// Only a proven-small value enters JSON.stringify; binary data never expands without a bound.
|
|
121
|
+
const json = bytes <= INLINE_BYTES ? (JSON.stringify(inlineValue(value)) ?? "null") : null;
|
|
122
|
+
// jsonb holds no NUL and no lone surrogate (`find -print0` output, say): such a value is kept
|
|
123
|
+
// in chunks, whose framing preserves every UTF-16 code unit
|
|
124
|
+
const inline = json !== null && !JSONB_UNSAFE.test(json) ? json : null;
|
|
125
|
+
return { digest: hash.digest("hex"), bytes, chunks, inline };
|
|
126
|
+
}
|
|
127
|
+
export function payloadReference(slot, p) {
|
|
128
|
+
return (p.inline ??
|
|
129
|
+
JSON.stringify({
|
|
130
|
+
$operationPayload: {
|
|
131
|
+
version: 1,
|
|
132
|
+
slot,
|
|
133
|
+
digest: p.digest,
|
|
134
|
+
bytes: p.bytes,
|
|
135
|
+
chunks: p.chunks,
|
|
136
|
+
},
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
export function isPayloadReference(value) {
|
|
140
|
+
return !!value && typeof value === "object" && "$operationPayload" in value;
|
|
141
|
+
}
|
|
142
|
+
function invalidPayload(message) {
|
|
143
|
+
throw new SandboxOperationOutcomeUnknownError(message);
|
|
144
|
+
}
|
|
145
|
+
class PayloadReader {
|
|
146
|
+
chunks;
|
|
147
|
+
remaining;
|
|
148
|
+
current = new Uint8Array();
|
|
149
|
+
offset = 0;
|
|
150
|
+
constructor(chunks, remaining) {
|
|
151
|
+
this.chunks = chunks;
|
|
152
|
+
this.remaining = remaining;
|
|
153
|
+
}
|
|
154
|
+
async bytes(n) {
|
|
155
|
+
if (!Number.isSafeInteger(n) || n < 0 || n > this.remaining)
|
|
156
|
+
invalidPayload("Invalid retained payload length");
|
|
157
|
+
this.remaining -= n;
|
|
158
|
+
const out = Buffer.alloc(n);
|
|
159
|
+
let used = 0;
|
|
160
|
+
while (used < n) {
|
|
161
|
+
if (this.offset === this.current.length) {
|
|
162
|
+
const next = await this.chunks.next();
|
|
163
|
+
if (next.done)
|
|
164
|
+
invalidPayload("Retained payload is incomplete");
|
|
165
|
+
this.current = next.value;
|
|
166
|
+
this.offset = 0;
|
|
167
|
+
}
|
|
168
|
+
const size = Math.min(n - used, this.current.length - this.offset);
|
|
169
|
+
out.set(this.current.subarray(this.offset, this.offset + size), used);
|
|
170
|
+
this.offset += size;
|
|
171
|
+
used += size;
|
|
172
|
+
}
|
|
173
|
+
return out;
|
|
174
|
+
}
|
|
175
|
+
async value() {
|
|
176
|
+
const tag = (await this.bytes(1))[0];
|
|
177
|
+
if (tag === 0)
|
|
178
|
+
return null;
|
|
179
|
+
if (tag === 1)
|
|
180
|
+
return true;
|
|
181
|
+
if (tag === 2)
|
|
182
|
+
return false;
|
|
183
|
+
const count = (await this.bytes(8)).readDoubleLE();
|
|
184
|
+
if (tag === 3)
|
|
185
|
+
return count;
|
|
186
|
+
if (!Number.isSafeInteger(count) || count < 0)
|
|
187
|
+
invalidPayload("Invalid retained payload size");
|
|
188
|
+
if (tag === 4)
|
|
189
|
+
return (await this.bytes(count)).toString("utf16le");
|
|
190
|
+
if (tag === 5) {
|
|
191
|
+
const b = await this.bytes(count);
|
|
192
|
+
return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
|
|
193
|
+
}
|
|
194
|
+
if (tag === 6) {
|
|
195
|
+
const result = [];
|
|
196
|
+
for (let n = 0; n < count; n++)
|
|
197
|
+
result.push(await this.value());
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
if (tag === 7) {
|
|
201
|
+
const result = {};
|
|
202
|
+
for (let n = 0; n < count; n++) {
|
|
203
|
+
const key = await this.value();
|
|
204
|
+
if (typeof key !== "string")
|
|
205
|
+
invalidPayload("Invalid retained payload key");
|
|
206
|
+
Object.defineProperty(result, key, {
|
|
207
|
+
value: await this.value(),
|
|
208
|
+
enumerable: true,
|
|
209
|
+
writable: true,
|
|
210
|
+
configurable: true,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
return invalidPayload("Unknown retained payload encoding");
|
|
216
|
+
}
|
|
217
|
+
async done() {
|
|
218
|
+
return this.offset === this.current.length && (await this.chunks.next()).done === true;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/** PG-only storage: bounded bytea rows/batches share the ledger transaction and retention. */
|
|
222
|
+
let OperationPayloads = class OperationPayloads {
|
|
223
|
+
db;
|
|
224
|
+
txHost;
|
|
225
|
+
constructor(db, txHost) {
|
|
226
|
+
this.db = db;
|
|
227
|
+
this.txHost = txHost;
|
|
228
|
+
}
|
|
229
|
+
async save(id, slot, value, described = describeOperationPayload(value)) {
|
|
230
|
+
await this.db
|
|
231
|
+
.deleteFrom("sandbox_operation_payload_chunks")
|
|
232
|
+
.where("operation_id", "=", id)
|
|
233
|
+
.where("slot", "=", slot)
|
|
234
|
+
.execute();
|
|
235
|
+
if (described.inline !== null)
|
|
236
|
+
return described.inline;
|
|
237
|
+
const hash = createHash("sha256");
|
|
238
|
+
let bytes = 0, sequence = 0, batch = [];
|
|
239
|
+
for (const chunk of operationPayloadChunks(value)) {
|
|
240
|
+
hash.update(chunk);
|
|
241
|
+
bytes += chunk.length;
|
|
242
|
+
batch.push({ operation_id: id, slot, sequence: sequence++, data: chunk });
|
|
243
|
+
if (batch.length === 16) {
|
|
244
|
+
await this.db.insertInto("sandbox_operation_payload_chunks").values(batch).execute();
|
|
245
|
+
batch = [];
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (batch.length)
|
|
249
|
+
await this.db.insertInto("sandbox_operation_payload_chunks").values(batch).execute();
|
|
250
|
+
if (hash.digest("hex") !== described.digest ||
|
|
251
|
+
bytes !== described.bytes ||
|
|
252
|
+
sequence !== described.chunks)
|
|
253
|
+
throw new Error("Operation payload changed during persistence");
|
|
254
|
+
return payloadReference(slot, described);
|
|
255
|
+
}
|
|
256
|
+
async load(id, slot, value) {
|
|
257
|
+
if (!isPayloadReference(value))
|
|
258
|
+
return value;
|
|
259
|
+
const ref = value.$operationPayload;
|
|
260
|
+
if (ref?.version !== 1 ||
|
|
261
|
+
ref.slot !== slot ||
|
|
262
|
+
!/^[a-f0-9]{64}$/.test(ref.digest) ||
|
|
263
|
+
!Number.isSafeInteger(ref.bytes) ||
|
|
264
|
+
ref.bytes < 1 ||
|
|
265
|
+
ref.chunks !== Math.ceil(ref.bytes / OPERATION_CHUNK_BYTES))
|
|
266
|
+
return invalidPayload("Invalid retained payload reference");
|
|
267
|
+
// Validate stored byte counts before decoding lengths can allocate a native result buffer.
|
|
268
|
+
const stored = await this.db
|
|
269
|
+
.selectFrom("sandbox_operation_payload_chunks")
|
|
270
|
+
.select([
|
|
271
|
+
sql `count(*)`.as("chunks"),
|
|
272
|
+
sql `coalesce(sum(octet_length(data)),0)`.as("bytes"),
|
|
273
|
+
])
|
|
274
|
+
.where("operation_id", "=", id)
|
|
275
|
+
.where("slot", "=", slot)
|
|
276
|
+
.executeTakeFirstOrThrow();
|
|
277
|
+
if (Number(stored.chunks) !== ref.chunks || Number(stored.bytes) !== ref.bytes)
|
|
278
|
+
invalidPayload("Retained payload chunks are incomplete");
|
|
279
|
+
const db = this.db;
|
|
280
|
+
const hash = createHash("sha256");
|
|
281
|
+
let bytes = 0, count = 0;
|
|
282
|
+
async function* read() {
|
|
283
|
+
for (let first = 0; first < ref.chunks; first += 16) {
|
|
284
|
+
const rows = await db
|
|
285
|
+
.selectFrom("sandbox_operation_payload_chunks")
|
|
286
|
+
.select(["sequence", "data"])
|
|
287
|
+
.where("operation_id", "=", id)
|
|
288
|
+
.where("slot", "=", slot)
|
|
289
|
+
.where("sequence", ">=", first)
|
|
290
|
+
.where("sequence", "<", Math.min(first + 16, ref.chunks))
|
|
291
|
+
.orderBy("sequence")
|
|
292
|
+
.execute();
|
|
293
|
+
if (rows.length !== Math.min(16, ref.chunks - first))
|
|
294
|
+
invalidPayload("Retained payload chunk is missing");
|
|
295
|
+
for (const row of rows) {
|
|
296
|
+
if (row.sequence !== count || row.data.length > OPERATION_CHUNK_BYTES)
|
|
297
|
+
invalidPayload("Retained payload chunk is missing");
|
|
298
|
+
hash.update(row.data);
|
|
299
|
+
bytes += row.data.length;
|
|
300
|
+
count++;
|
|
301
|
+
yield row.data;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const reader = new PayloadReader(read(), ref.bytes);
|
|
306
|
+
const result = await reader.value();
|
|
307
|
+
if (!(await reader.done()) ||
|
|
308
|
+
bytes !== ref.bytes ||
|
|
309
|
+
count !== ref.chunks ||
|
|
310
|
+
hash.digest("hex") !== ref.digest)
|
|
311
|
+
return invalidPayload("Retained payload digest mismatch");
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
async expire(id) {
|
|
315
|
+
await this.db
|
|
316
|
+
.deleteFrom("sandbox_operation_payload_chunks")
|
|
317
|
+
.where("operation_id", "=", id)
|
|
318
|
+
.execute();
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
__decorate([
|
|
322
|
+
Transactional({ propagation: "mandatory" })
|
|
323
|
+
], OperationPayloads.prototype, "save", null);
|
|
324
|
+
__decorate([
|
|
325
|
+
Transactional({ propagation: "mandatory" })
|
|
326
|
+
], OperationPayloads.prototype, "expire", null);
|
|
327
|
+
OperationPayloads = __decorate([
|
|
328
|
+
Injectable(),
|
|
329
|
+
__param(0, Inject(KernelDb)),
|
|
330
|
+
__param(1, Inject(TransactionHost))
|
|
331
|
+
], OperationPayloads);
|
|
332
|
+
export { OperationPayloads };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { BackgroundTaskStopper } from "../background/stop.js";
|
|
2
|
+
import { BackgroundTaskStore } from "../background/store.js";
|
|
3
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
|
+
import type { KernelLogger } from "../log.js";
|
|
5
|
+
import { SandboxOperationIntents } from "./operation-intents.js";
|
|
6
|
+
import { SandboxProviders } from "./providers.js";
|
|
7
|
+
import { SandboxService } from "./sandbox.service.js";
|
|
8
|
+
/** Called only by the maintenance consumer. It has no native dispatch or lease token API. */
|
|
9
|
+
export declare class SandboxOperationReconcileJob {
|
|
10
|
+
private readonly intents;
|
|
11
|
+
private readonly db;
|
|
12
|
+
private readonly providers;
|
|
13
|
+
private readonly sandbox;
|
|
14
|
+
private readonly tasks;
|
|
15
|
+
private readonly stopper;
|
|
16
|
+
/** Where a recovered start's refusal keeps the provider's own message (never the task's). */
|
|
17
|
+
private readonly log?;
|
|
18
|
+
constructor(intents: SandboxOperationIntents, db: KernelDb, providers: SandboxProviders, sandbox: Pick<SandboxService, "ifRunning">, tasks: Pick<BackgroundTaskStore, "byOperation" | "settleOwedCancel">, stopper: Pick<BackgroundTaskStopper, "stop">,
|
|
19
|
+
/** Where a recovered start's refusal keeps the provider's own message (never the task's). */
|
|
20
|
+
log?: Pick<KernelLogger, "warn"> | undefined);
|
|
21
|
+
reconcile(operationId: string): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* A managed start: past its retention an unresolved launch is given up (terminal task, owed
|
|
24
|
+
* exact revoke); a revoked one owes its revoke; a `dispatching` one still has its dispatcher
|
|
25
|
+
* (or a holder cleanup that revokes it) and is only checked again later; a `started-or-unknown`
|
|
26
|
+
* one is recovered under the same identity.
|
|
27
|
+
*/
|
|
28
|
+
private reconcileBackground;
|
|
29
|
+
/**
|
|
30
|
+
* Recovers a managed start whose acknowledgement was lost, under a task-specific recovery claim:
|
|
31
|
+
* asks the provider about the same operation, recorded scope and recorded input on the exact
|
|
32
|
+
* recorded provider, version and instance generation only (never resumed), after the same
|
|
33
|
+
* artifact and runtime checks as the first dispatch. A launch that happened returns its original
|
|
34
|
+
* ref; one that did not happens once. Never a new operation ID, never refreshed authority.
|
|
35
|
+
*/
|
|
36
|
+
recoverBackground(operationId: string): Promise<void>;
|
|
37
|
+
revoke(operationId: string): Promise<void>;
|
|
38
|
+
private revokeOnce;
|
|
39
|
+
/**
|
|
40
|
+
* The exact cancellation a revoked background start owes (design B3): the task's own stop by its
|
|
41
|
+
* exact ref with the five-second grace, which settles and reports the task; the revocation is
|
|
42
|
+
* acknowledged only on verified settlement (a stop or an exit). A pending cancellation, an
|
|
43
|
+
* unreachable or replaced instance and lost supervision keep it owed. The intent alone carries
|
|
44
|
+
* the obligation: a finished-lost task or a task row removed with its conversation is still
|
|
45
|
+
* stopped by the intent's recorded ref on the recorded instance generation, never by a PID, a
|
|
46
|
+
* tag or a newer instance.
|
|
47
|
+
*/
|
|
48
|
+
private revokeBackground;
|
|
49
|
+
}
|