@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,286 @@
|
|
|
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 { SandboxScopeDeniedError } from "@capybara-hq/plugin-sdk";
|
|
11
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
12
|
+
import { EventLog } from "../conversations/event-log.js";
|
|
13
|
+
import { PermissionContextV1Schema } from "../conversations/events.js";
|
|
14
|
+
import { PermissionProfileStore } from "../conversations/permission-profile-store.js";
|
|
15
|
+
import { ApiError } from "../errors.js";
|
|
16
|
+
import { afterCommit } from "../infra/database/after-commit.js";
|
|
17
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
18
|
+
import { hasTransactionFence, TransactionHost, withTransactionFencePrelude, } from "../infra/database/transaction-host.js";
|
|
19
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
20
|
+
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
21
|
+
import { SandboxOperationAuthority } from "../sandbox/operation-authority.js";
|
|
22
|
+
import { PermissionAuthorityLock } from "../sandbox/permission-authority-lock.js";
|
|
23
|
+
import { PermissionConfigPublication } from "../sandbox/permission-config-publication.js";
|
|
24
|
+
import { PermissionContextSubjectChanged, } from "../sandbox/permission-context-types.js";
|
|
25
|
+
import { permissionContextHash, permissionContextIdemKey, RecordedPermissionContextError, } from "./context/permission-profile.js";
|
|
26
|
+
import { RunError } from "./model-call.js";
|
|
27
|
+
import { PERMISSION_CONTEXT_RUN_ERRORS, PublicationStale, permissionContextFailureKind, } from "./permission-context-errors.js";
|
|
28
|
+
import { currentExecutionClaim, ExecutionRevoked } from "./run-execution.js";
|
|
29
|
+
export { PERMISSION_CONTEXT_RUN_ERRORS };
|
|
30
|
+
const runError = (kind) => new RunError(PERMISSION_CONTEXT_RUN_ERRORS[kind].code, PERMISSION_CONTEXT_RUN_ERRORS[kind].message);
|
|
31
|
+
/** A live recorder reached a conversation it must never write (an eval replay, a wrong claim). */
|
|
32
|
+
class RecorderRefused extends Error {
|
|
33
|
+
name = "RecorderRefused";
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Maps a failure of a permission context read or append (and of the complete-log refresh after it)
|
|
37
|
+
* to what the run sees, before any request is sent: a revoked or replaced execution keeps its
|
|
38
|
+
* identical `ExecutionRevoked` object, an abort keeps its reason, a `RunError` passes, and every
|
|
39
|
+
* other failure becomes one of the named run errors. A recorded `permission_context` (or marked
|
|
40
|
+
* path) the strict v1 schema rejects is `run.permission_context_invalid`, never a provider error
|
|
41
|
+
* and never rendered as historical text.
|
|
42
|
+
*/
|
|
43
|
+
export function permissionContextFailure(error, signal) {
|
|
44
|
+
if (error instanceof ExecutionRevoked || error instanceof RunError)
|
|
45
|
+
return error;
|
|
46
|
+
if (signal?.aborted && (error === signal.reason || error?.name === "AbortError"))
|
|
47
|
+
return error;
|
|
48
|
+
return runError(permissionContextFailureKind(error));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The live recorder of the model-facing permission context (spec "Live facts and writer
|
|
52
|
+
* contract"). For one request construction it discovers the conversation's root and device
|
|
53
|
+
* without locks, then opens exactly one transaction whose fence prelude takes the complete
|
|
54
|
+
* authority subject (platform, org, device when selected, root) before the inherited run fence
|
|
55
|
+
* (org, root, calling conversation, execution id). Inside it re-validates the claim, the run and
|
|
56
|
+
* the discovered subject, reads the authority projection shared with admission, compares it with
|
|
57
|
+
* the latest recorded `permission_context` of the calling conversation and appends a new snapshot
|
|
58
|
+
* only when the payload differs (A to B to A appends three). One changed subject is rediscovered
|
|
59
|
+
* outside the transaction; a second fails the run. An abort, a replaced claim or a deleted
|
|
60
|
+
* conversation appends nothing. The bus hint follows the commit.
|
|
61
|
+
*
|
|
62
|
+
* Dormant: nothing in production calls it until the request boundary and the managed tool path
|
|
63
|
+
* activate together (Gate 9.4 with Task 8 phase 8.6).
|
|
64
|
+
*/
|
|
65
|
+
let PermissionContextRecorder = class PermissionContextRecorder {
|
|
66
|
+
db;
|
|
67
|
+
txHost;
|
|
68
|
+
events;
|
|
69
|
+
authority;
|
|
70
|
+
locks;
|
|
71
|
+
publication;
|
|
72
|
+
profiles;
|
|
73
|
+
log;
|
|
74
|
+
constructor(db, txHost, events, authority, locks, publication, profiles, log) {
|
|
75
|
+
this.db = db;
|
|
76
|
+
this.txHost = txHost;
|
|
77
|
+
this.events = events;
|
|
78
|
+
this.authority = authority;
|
|
79
|
+
this.locks = locks;
|
|
80
|
+
this.publication = publication;
|
|
81
|
+
this.profiles = profiles;
|
|
82
|
+
this.log = log;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Records the current permission context into the calling conversation's log, or nothing when
|
|
86
|
+
* the latest recorded snapshot already equals it. Requires the real execution claim of this run
|
|
87
|
+
* (`RunExecution.withClaim`) and no open transaction.
|
|
88
|
+
*/
|
|
89
|
+
async record(input, signal) {
|
|
90
|
+
try {
|
|
91
|
+
const claim = currentExecutionClaim();
|
|
92
|
+
if (!claim ||
|
|
93
|
+
claim.orgId !== input.orgId ||
|
|
94
|
+
claim.runId !== input.runId ||
|
|
95
|
+
claim.conversationId !== input.conversationId)
|
|
96
|
+
throw new RecorderRefused("A permission context requires this run's execution claim");
|
|
97
|
+
if (this.txHost.current())
|
|
98
|
+
throw new RecorderRefused("A permission context is recorded outside any transaction");
|
|
99
|
+
// the run fence takes the org row before the prelude's authority locks; without it the
|
|
100
|
+
// append's org row would follow them (inverting the global lock order)
|
|
101
|
+
if (!hasTransactionFence())
|
|
102
|
+
throw new RecorderRefused("A permission context requires the run's inherited fence");
|
|
103
|
+
for (let attempt = 0;; attempt++) {
|
|
104
|
+
signal.throwIfAborted();
|
|
105
|
+
try {
|
|
106
|
+
const subject = await this.discover(input, claim);
|
|
107
|
+
await withTransactionFencePrelude(() => this.locks.acquire(subject, { kind: "admit" }), () => this.appendLocked(input, subject, signal));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
// one rediscovery outside the transaction; never a lock added under held ones
|
|
112
|
+
if (attempt === 0 && changedSubject(error))
|
|
113
|
+
continue;
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
const mapped = permissionContextFailure(error instanceof ApiError && error.code === "device.not_found"
|
|
120
|
+
? new PermissionContextSubjectChanged()
|
|
121
|
+
: error, signal);
|
|
122
|
+
if (mapped instanceof RunError &&
|
|
123
|
+
(mapped.code === PERMISSION_CONTEXT_RUN_ERRORS.invalid.code ||
|
|
124
|
+
mapped.code === PERMISSION_CONTEXT_RUN_ERRORS.unavailable.code))
|
|
125
|
+
this.log.warn("permission context not recorded", {
|
|
126
|
+
conversationId: input.conversationId,
|
|
127
|
+
runId: input.runId,
|
|
128
|
+
code: mapped.code,
|
|
129
|
+
cause: error instanceof Error ? `${error.name}: ${error.message}`.slice(0, 500) : "",
|
|
130
|
+
});
|
|
131
|
+
throw mapped;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** The complete authority subject, read without locks; validated again under them. */
|
|
135
|
+
async discover(input, claim) {
|
|
136
|
+
const conv = await this.db
|
|
137
|
+
.selectFrom("conversations")
|
|
138
|
+
.select(["org_id", "kind", "eval_run_id"])
|
|
139
|
+
.where("id", "=", input.conversationId)
|
|
140
|
+
.executeTakeFirst();
|
|
141
|
+
if (!conv || conv.org_id !== input.orgId)
|
|
142
|
+
throw new ApiError("conversation.not_found", "Conversation not found");
|
|
143
|
+
if (conv.kind === "eval" || conv.eval_run_id !== null)
|
|
144
|
+
throw new RecorderRefused("A replay conversation never records live permission context");
|
|
145
|
+
const root = await this.profiles.readRoot(input.orgId, input.conversationId);
|
|
146
|
+
if (root.id !== claim.rootId)
|
|
147
|
+
throw new RecorderRefused("The execution claim belongs to another conversation tree");
|
|
148
|
+
// the whole tree, not only the calling conversation: a replay's children never record either
|
|
149
|
+
if (root.id !== input.conversationId)
|
|
150
|
+
await this.refuseReplayRoot(input.orgId, root.id);
|
|
151
|
+
return {
|
|
152
|
+
orgId: input.orgId,
|
|
153
|
+
rootConversationId: root.id,
|
|
154
|
+
...(root.device_id ? { deviceId: root.device_id } : {}),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
async refuseReplayRoot(orgId, rootId) {
|
|
158
|
+
const row = await this.db
|
|
159
|
+
.selectFrom("conversations")
|
|
160
|
+
.select(["kind", "eval_run_id"])
|
|
161
|
+
.where("id", "=", rootId)
|
|
162
|
+
.where("org_id", "=", orgId)
|
|
163
|
+
.executeTakeFirst();
|
|
164
|
+
if (row && (row.kind === "eval" || row.eval_run_id !== null))
|
|
165
|
+
throw new RecorderRefused("A replay conversation never records live permission context");
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The one transaction of a record attempt: its fence already holds the complete authority
|
|
169
|
+
* subject and the inherited run fence. Returns the appended seq, or null when deduplicated.
|
|
170
|
+
*/
|
|
171
|
+
async appendLocked(input, subject, signal) {
|
|
172
|
+
signal.throwIfAborted();
|
|
173
|
+
try {
|
|
174
|
+
await this.publication.assertCurrent();
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
if (error instanceof SandboxScopeDeniedError)
|
|
178
|
+
throw new PublicationStale(error.message);
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
const claim = currentExecutionClaim();
|
|
182
|
+
if (!claim || claim.runId !== input.runId || claim.conversationId !== input.conversationId)
|
|
183
|
+
throw new RecorderRefused("A permission context requires this run's execution claim");
|
|
184
|
+
// The same order as the run fence (org, root, calling conversation), then the explicit claim
|
|
185
|
+
// comparison: without the inherited wrapper this transaction still cannot write unfenced.
|
|
186
|
+
const org = await this.db
|
|
187
|
+
.selectFrom("orgs")
|
|
188
|
+
.select("deleted_at")
|
|
189
|
+
.where("id", "=", input.orgId)
|
|
190
|
+
.forNoKeyUpdate()
|
|
191
|
+
.executeTakeFirst();
|
|
192
|
+
if (!org || org.deleted_at !== null)
|
|
193
|
+
throw new ExecutionRevoked();
|
|
194
|
+
const rootRow = await this.db
|
|
195
|
+
.selectFrom("conversations")
|
|
196
|
+
.select(["deleted_at", "kind", "eval_run_id"])
|
|
197
|
+
.where("id", "=", subject.rootConversationId)
|
|
198
|
+
.where("org_id", "=", input.orgId)
|
|
199
|
+
.forNoKeyUpdate()
|
|
200
|
+
.executeTakeFirst();
|
|
201
|
+
const conv = await this.db
|
|
202
|
+
.selectFrom("conversations")
|
|
203
|
+
.select(["deleted_at", "kind", "eval_run_id"])
|
|
204
|
+
.where("id", "=", input.conversationId)
|
|
205
|
+
.where("org_id", "=", input.orgId)
|
|
206
|
+
.forUpdate()
|
|
207
|
+
.executeTakeFirst();
|
|
208
|
+
if (!rootRow || rootRow.deleted_at !== null || !conv || conv.deleted_at !== null)
|
|
209
|
+
throw new ExecutionRevoked();
|
|
210
|
+
if (conv.kind === "eval" ||
|
|
211
|
+
conv.eval_run_id !== null ||
|
|
212
|
+
rootRow.kind === "eval" ||
|
|
213
|
+
rootRow.eval_run_id !== null)
|
|
214
|
+
throw new RecorderRefused("A replay conversation never records live permission context");
|
|
215
|
+
const run = await this.db
|
|
216
|
+
.selectFrom("runs")
|
|
217
|
+
.select(["conversation_id", "status", "execution_id", "trigger_account_id"])
|
|
218
|
+
.where("id", "=", input.runId)
|
|
219
|
+
.executeTakeFirst();
|
|
220
|
+
if (!run ||
|
|
221
|
+
run.conversation_id !== input.conversationId ||
|
|
222
|
+
run.status !== "running" ||
|
|
223
|
+
run.execution_id !== claim.executionId)
|
|
224
|
+
throw new ExecutionRevoked();
|
|
225
|
+
if (run.trigger_account_id !== input.accountId)
|
|
226
|
+
throw new ApiError("forbidden", "This run's actor is no longer authorized");
|
|
227
|
+
const root = await this.profiles.readRoot(input.orgId, input.conversationId);
|
|
228
|
+
if (root.id !== subject.rootConversationId ||
|
|
229
|
+
(root.device_id ?? undefined) !== subject.deviceId)
|
|
230
|
+
throw new PermissionContextSubjectChanged();
|
|
231
|
+
const payload = await this.authority.readContextLocked(input, signal);
|
|
232
|
+
if (payload.rootConversationId !== subject.rootConversationId)
|
|
233
|
+
throw new PermissionContextSubjectChanged();
|
|
234
|
+
// the persisted log decides: the latest snapshot of this conversation, under its row lock
|
|
235
|
+
const latest = await this.db
|
|
236
|
+
.selectFrom("conversation_events")
|
|
237
|
+
.select(["seq", "payload"])
|
|
238
|
+
.where("conversation_id", "=", input.conversationId)
|
|
239
|
+
.where("type", "=", "permission_context")
|
|
240
|
+
.orderBy("seq", "desc")
|
|
241
|
+
.limit(1)
|
|
242
|
+
.executeTakeFirst();
|
|
243
|
+
const previousSeq = latest ? Number(latest.seq) : 0;
|
|
244
|
+
if (latest) {
|
|
245
|
+
const prior = PermissionContextV1Schema.safeParse(latest.payload);
|
|
246
|
+
if (!prior.success)
|
|
247
|
+
throw new RecordedPermissionContextError(previousSeq, "permission_context");
|
|
248
|
+
if (permissionContextHash(prior.data) === permissionContextHash(payload))
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
signal.throwIfAborted();
|
|
252
|
+
const [event] = await this.events.appendIn(input.conversationId, [
|
|
253
|
+
{
|
|
254
|
+
type: "permission_context",
|
|
255
|
+
runId: input.runId,
|
|
256
|
+
idemKey: permissionContextIdemKey(payload, previousSeq),
|
|
257
|
+
payload,
|
|
258
|
+
},
|
|
259
|
+
]);
|
|
260
|
+
if (!event)
|
|
261
|
+
throw new Error("The permission context append returned no event");
|
|
262
|
+
const conversationId = input.conversationId;
|
|
263
|
+
// a lossy hint after the commit; a rollback drops it
|
|
264
|
+
afterCommit(() => this.events.notify(conversationId, event.seq));
|
|
265
|
+
return event.seq;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
__decorate([
|
|
269
|
+
Transactional()
|
|
270
|
+
], PermissionContextRecorder.prototype, "appendLocked", null);
|
|
271
|
+
PermissionContextRecorder = __decorate([
|
|
272
|
+
Injectable(),
|
|
273
|
+
__param(0, Inject(KernelDb)),
|
|
274
|
+
__param(1, Inject(TransactionHost)),
|
|
275
|
+
__param(2, Inject(EventLog)),
|
|
276
|
+
__param(3, Inject(SandboxOperationAuthority)),
|
|
277
|
+
__param(4, Inject(PermissionAuthorityLock)),
|
|
278
|
+
__param(5, Inject(PermissionConfigPublication)),
|
|
279
|
+
__param(6, Inject(PermissionProfileStore)),
|
|
280
|
+
__param(7, Inject(KERNEL_LOGGER))
|
|
281
|
+
], PermissionContextRecorder);
|
|
282
|
+
export { PermissionContextRecorder };
|
|
283
|
+
function changedSubject(error) {
|
|
284
|
+
return (error instanceof PermissionContextSubjectChanged ||
|
|
285
|
+
(error instanceof ApiError && error.code === "device.not_found"));
|
|
286
|
+
}
|
|
@@ -12,8 +12,8 @@ export type ProjectRootPayload = EventPayload<"project_root">;
|
|
|
12
12
|
export declare const AGENTS_MD_MAX_BYTES: number;
|
|
13
13
|
/** Appended to an `AGENTS.md` cut at {@link AGENTS_MD_MAX_BYTES} (model-visible, so English). */
|
|
14
14
|
export declare const AGENTS_MD_TRUNCATED_MARKER = "\n\n[AGENTS.md is longer than 64 KiB; the rest was cut here]";
|
|
15
|
-
/** The directory of a project's skills, relative to its root. */
|
|
16
|
-
export
|
|
15
|
+
/** The directory of a project's skills, relative to its root (the kernel read's own constant). */
|
|
16
|
+
export { PROJECT_SKILLS_DIR } from "../../sandbox/kernel-read.js";
|
|
17
17
|
/**
|
|
18
18
|
* The most skill directories a run start reads, the first by name; a project with more says so
|
|
19
19
|
* (`skillsTruncated`).
|
|
@@ -92,6 +92,11 @@ export declare const projectUnavailable: {
|
|
|
92
92
|
reason: "gone" | "outside_mode" | "removed" | "unreadable" | "unsupported";
|
|
93
93
|
message: string;
|
|
94
94
|
};
|
|
95
|
+
/** A host machine whose runtime cannot enforce the kernel's read-only read (gate on). */
|
|
96
|
+
readonly notEnforceable: (root: string) => {
|
|
97
|
+
reason: "gone" | "outside_mode" | "removed" | "unreadable" | "unsupported";
|
|
98
|
+
message: string;
|
|
99
|
+
};
|
|
95
100
|
readonly unsupported: () => {
|
|
96
101
|
reason: "gone" | "outside_mode" | "removed" | "unreadable" | "unsupported";
|
|
97
102
|
message: string;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { isFileSystemGoneError, isSandboxGoneError, MAX_DEVICE_LIST_ENTRIES, parseSkillMd, SKILL_ENTRY_FILE, } from "@capybara-hq/plugin-sdk";
|
|
3
3
|
import { POLICY_REFUSAL_PREFIX } from "../../local-devices/relay.js";
|
|
4
|
+
import { PROJECT_SKILLS_DIR } from "../../sandbox/kernel-read.js";
|
|
4
5
|
/** The most of a project's `AGENTS.md` a `project_root` event carries, in UTF-8 bytes (marker included). */
|
|
5
6
|
export const AGENTS_MD_MAX_BYTES = 64 * 1024;
|
|
6
7
|
/** Appended to an `AGENTS.md` cut at {@link AGENTS_MD_MAX_BYTES} (model-visible, so English). */
|
|
7
8
|
export const AGENTS_MD_TRUNCATED_MARKER = "\n\n[AGENTS.md is longer than 64 KiB; the rest was cut here]";
|
|
8
|
-
/** The directory of a project's skills, relative to its root. */
|
|
9
|
-
export
|
|
9
|
+
/** The directory of a project's skills, relative to its root (the kernel read's own constant). */
|
|
10
|
+
export { PROJECT_SKILLS_DIR } from "../../sandbox/kernel-read.js";
|
|
10
11
|
/** How many `SKILL.md` files one run start reads at once. */
|
|
11
12
|
const SKILL_READ_CONCURRENCY = 8;
|
|
12
13
|
/**
|
|
@@ -42,6 +43,8 @@ export const projectUnavailable = {
|
|
|
42
43
|
unreadable: (root) => unavailable("unreadable", `The project's root ${clipped(root)} could not be read.`),
|
|
43
44
|
widerMode: (root) => unavailable("outside_mode", `The project's root ${clipped(root)} was not read: the device runs a wider mode than the organization allows.`),
|
|
44
45
|
removed: (project) => unavailable("removed", `The project "${clipped(project, 80)}" was removed from the device's projects.`),
|
|
46
|
+
/** A host machine whose runtime cannot enforce the kernel's read-only read (gate on). */
|
|
47
|
+
notEnforceable: (root) => unavailable("unreadable", `The project's root ${clipped(root)} was not read: this device's runtime cannot enforce a read-only read on its host; turn on its sandbox enforcement or use another device.`),
|
|
45
48
|
unsupported: () => unavailable("unsupported", "The device does not offer projects; capybara-device on that machine needs an update."),
|
|
46
49
|
};
|
|
47
50
|
/** The snapshot of a project the run cannot use: the reason alone, no AGENTS.md, no skills. */
|
|
@@ -43,7 +43,11 @@ export interface ProjectKnowledgeInput {
|
|
|
43
43
|
*
|
|
44
44
|
* Reads in the device's effective mode (`DeviceProjectsService.scopeFor`, the same scope the
|
|
45
45
|
* binding was checked against) and through `SandboxService.withSandbox`, as a tool call does, so
|
|
46
|
-
* a bound conversation starts its device eagerly (the accepted cost, spec §4.4).
|
|
46
|
+
* a bound conversation starts its device eagerly (the accepted cost, spec §4.4). Once the
|
|
47
|
+
* activation gate is on the read goes through `SandboxService.withKernelRead` instead: every
|
|
48
|
+
* stat / readFile / list a kernel read under the run's current execution claim while the run is
|
|
49
|
+
* still queued, acting for the run's trigger, its scope narrowed to the root, `AGENTS.md` and
|
|
50
|
+
* `.agents/skills` (permission activation L2). A project the
|
|
47
51
|
* run cannot use is recorded as `unavailable` with its reason: a project the device no longer
|
|
48
52
|
* keeps under the bound name and path (`removed`, not asked), a root that is gone, out of the mode
|
|
49
53
|
* (or above the owner's home) or unreadable, or a machine that does not announce projects
|
|
@@ -60,7 +64,11 @@ export declare class ProjectKnowledgeService {
|
|
|
60
64
|
private readonly sandbox;
|
|
61
65
|
private readonly log;
|
|
62
66
|
private readonly timeoutMs;
|
|
63
|
-
|
|
67
|
+
/** The paired activation gate: once on, the read is a set of kernel reads. */
|
|
68
|
+
private readonly activation?;
|
|
69
|
+
constructor(db: KernelDb, eventLog: Pick<EventLog, "appendIn" | "notify">, txHost: TransactionHost, devices: Pick<ConversationDevices, "forTool">, projects: Pick<DeviceProjectsService, "scopeFor" | "keeps">, sandbox: Pick<SandboxService, "withSandbox" | "withKernelRead">, log: KernelLogger, timeoutMs?: () => number,
|
|
70
|
+
/** The paired activation gate: once on, the read is a set of kernel reads. */
|
|
71
|
+
activation?: boolean | undefined);
|
|
64
72
|
/**
|
|
65
73
|
* Reads and records (see the class); notifies after the commit. False: the run had ended under
|
|
66
74
|
* the locks (a stop landed while the device was read), so nothing was appended and the run must
|
|
@@ -75,6 +83,13 @@ export declare class ProjectKnowledgeService {
|
|
|
75
83
|
private copyFromRoot;
|
|
76
84
|
/** The snapshot read on the device, or null when the device could not be asked (logged). */
|
|
77
85
|
private read;
|
|
86
|
+
/**
|
|
87
|
+
* How the read enters the device. Gate off: the raw `withSandbox`, acting for the conversation's
|
|
88
|
+
* owner (unchanged). Gate on: `withKernelRead` under the run's current execution claim (the run
|
|
89
|
+
* start runs inside it; another run's claim or none admits nothing), acting for the run's
|
|
90
|
+
* trigger, reading the bound root only.
|
|
91
|
+
*/
|
|
92
|
+
private entry;
|
|
78
93
|
/**
|
|
79
94
|
* One transaction under the conversation row lock (as `PluginContextService`): compares the
|
|
80
95
|
* snapshot with the conversation's latest `project_root` and appends it when it differs, with
|
|
@@ -11,17 +11,20 @@ import { DeviceProtocolError } from "@capybara-hq/plugin-sdk";
|
|
|
11
11
|
import { Inject, Injectable, Optional } from "@nestjs/common";
|
|
12
12
|
import { EventLog } from "../../conversations/event-log.js";
|
|
13
13
|
import { ConversationDevices } from "../../devices/conversation-devices.js";
|
|
14
|
-
import { DeviceProjectsService, withinMode } from "../../devices/device-projects.service.js";
|
|
14
|
+
import { DeviceProjectsService, readModeOf, withinMode, } from "../../devices/device-projects.service.js";
|
|
15
15
|
import { aboveHome } from "../../devices/working-root.js";
|
|
16
16
|
import { ApiError } from "../../errors.js";
|
|
17
17
|
import { KernelDb } from "../../infra/database/kernel-db.js";
|
|
18
18
|
import { TransactionHost } from "../../infra/database/transaction-host.js";
|
|
19
19
|
import { Transactional } from "../../infra/database/transactional.decorator.js";
|
|
20
20
|
import { KERNEL_LOGGER } from "../../infra/tokens.js";
|
|
21
|
+
import { KernelReadNotAdmitted, KernelReadUnsupported } from "../../sandbox/kernel-read.js";
|
|
21
22
|
import { SandboxLockLostError, SandboxPathBusyError } from "../../sandbox/leases.js";
|
|
23
|
+
import { PERMISSION_CONTEXT_ACTIVATION, permissionContextActive, } from "../../sandbox/permission-context-types.js";
|
|
22
24
|
import { homeOf } from "../../sandbox/review/rules.js";
|
|
23
25
|
import { SandboxService } from "../../sandbox/sandbox.service.js";
|
|
24
26
|
import { providerConnectionFailure } from "../../sandbox/unreachable.js";
|
|
27
|
+
import { currentExecutionClaim } from "../run-execution.js";
|
|
25
28
|
import { runEndedIn } from "../run-status.js";
|
|
26
29
|
import { projectRootIdemKey, projectUnavailable, readProjectKnowledge, sameProjectRoot, unavailableProject, } from "./knowledge.js";
|
|
27
30
|
/**
|
|
@@ -34,13 +37,23 @@ export const PROJECT_KNOWLEDGE_TIMEOUT_MS = 30_000;
|
|
|
34
37
|
* {@link PROJECT_KNOWLEDGE_TIMEOUT_MS}.
|
|
35
38
|
*/
|
|
36
39
|
export const PROJECT_KNOWLEDGE_TIMEOUT = Symbol("PROJECT_KNOWLEDGE_TIMEOUT");
|
|
40
|
+
/** The run start has no current execution claim of its own run: nothing is admitted. */
|
|
41
|
+
class KnowledgeClaimMissing extends Error {
|
|
42
|
+
constructor() {
|
|
43
|
+
super("The run start has no current execution claim of its run");
|
|
44
|
+
this.name = "KnowledgeClaimMissing";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
37
47
|
/**
|
|
38
48
|
* An error that says the device could not be asked, not that the project is unreadable: the
|
|
39
49
|
* provider unreachable, a kernel refusal (the device offline, gone or busy, a relay given up), a
|
|
40
|
-
* malformed device answer, a lost lease
|
|
50
|
+
* malformed device answer, a lost lease, a kernel read that was not admitted (gate on: nothing
|
|
51
|
+
* was sent to the device).
|
|
41
52
|
*/
|
|
42
53
|
function isDeviceFailure(e) {
|
|
43
54
|
return (providerConnectionFailure(e) !== undefined ||
|
|
55
|
+
e instanceof KernelReadNotAdmitted ||
|
|
56
|
+
e instanceof KernelReadUnsupported ||
|
|
44
57
|
e instanceof ApiError ||
|
|
45
58
|
e instanceof DeviceProtocolError ||
|
|
46
59
|
e instanceof SandboxLockLostError ||
|
|
@@ -59,7 +72,11 @@ function isDeviceFailure(e) {
|
|
|
59
72
|
*
|
|
60
73
|
* Reads in the device's effective mode (`DeviceProjectsService.scopeFor`, the same scope the
|
|
61
74
|
* binding was checked against) and through `SandboxService.withSandbox`, as a tool call does, so
|
|
62
|
-
* a bound conversation starts its device eagerly (the accepted cost, spec §4.4).
|
|
75
|
+
* a bound conversation starts its device eagerly (the accepted cost, spec §4.4). Once the
|
|
76
|
+
* activation gate is on the read goes through `SandboxService.withKernelRead` instead: every
|
|
77
|
+
* stat / readFile / list a kernel read under the run's current execution claim while the run is
|
|
78
|
+
* still queued, acting for the run's trigger, its scope narrowed to the root, `AGENTS.md` and
|
|
79
|
+
* `.agents/skills` (permission activation L2). A project the
|
|
63
80
|
* run cannot use is recorded as `unavailable` with its reason: a project the device no longer
|
|
64
81
|
* keeps under the bound name and path (`removed`, not asked), a root that is gone, out of the mode
|
|
65
82
|
* (or above the owner's home) or unreadable, or a machine that does not announce projects
|
|
@@ -76,7 +93,10 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
76
93
|
sandbox;
|
|
77
94
|
log;
|
|
78
95
|
timeoutMs;
|
|
79
|
-
|
|
96
|
+
activation;
|
|
97
|
+
constructor(db, eventLog, txHost, devices, projects, sandbox, log, timeoutMs = () => PROJECT_KNOWLEDGE_TIMEOUT_MS,
|
|
98
|
+
/** The paired activation gate: once on, the read is a set of kernel reads. */
|
|
99
|
+
activation) {
|
|
80
100
|
this.db = db;
|
|
81
101
|
this.eventLog = eventLog;
|
|
82
102
|
this.txHost = txHost;
|
|
@@ -85,6 +105,7 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
85
105
|
this.sandbox = sandbox;
|
|
86
106
|
this.log = log;
|
|
87
107
|
this.timeoutMs = timeoutMs;
|
|
108
|
+
this.activation = activation;
|
|
88
109
|
}
|
|
89
110
|
/**
|
|
90
111
|
* Reads and records (see the class); notifies after the commit. False: the run had ended under
|
|
@@ -182,7 +203,8 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
182
203
|
});
|
|
183
204
|
return unavailableProject(bound.project, bound.root, projectUnavailable.widerMode(bound.root));
|
|
184
205
|
}
|
|
185
|
-
|
|
206
|
+
const enter = await this.entry(input, bound.accountId, bound.root);
|
|
207
|
+
return await enter({
|
|
186
208
|
orgId: input.orgId,
|
|
187
209
|
deviceId,
|
|
188
210
|
holderConversationId: input.conversationId,
|
|
@@ -190,7 +212,7 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
190
212
|
// started, restarted or had its files reset
|
|
191
213
|
silent: true,
|
|
192
214
|
actor: {
|
|
193
|
-
accountId:
|
|
215
|
+
accountId: enter.accountId,
|
|
194
216
|
conversationId: input.conversationId,
|
|
195
217
|
runId: input.runId,
|
|
196
218
|
},
|
|
@@ -209,13 +231,26 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
209
231
|
stat: (path) => p.stat(h, path, sig),
|
|
210
232
|
readFile: (path, maxBytes) => p.readFile(h, path, { maxBytes, signal: sig }),
|
|
211
233
|
...(list !== undefined && {
|
|
212
|
-
list: (path, limit) => list.call(p, h, path, { limit, limits: { mode: scope
|
|
234
|
+
list: (path, limit) => list.call(p, h, path, { limit, limits: { mode: readModeOf(scope) }, signal: sig }),
|
|
213
235
|
}),
|
|
214
236
|
};
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
237
|
+
try {
|
|
238
|
+
return await readProjectKnowledge(files, target, {
|
|
239
|
+
log: (msg, detail) => this.log.info(msg, { ...where, ...detail }),
|
|
240
|
+
isDeviceFailure: (e) => sig.aborted || isDeviceFailure(e),
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
catch (e) {
|
|
244
|
+
// gate on: a host runtime without restricted enforcement gets no kernel read; the
|
|
245
|
+
// project is recorded unreadable with the reason and the run goes on
|
|
246
|
+
if (!(e instanceof KernelReadUnsupported))
|
|
247
|
+
throw e;
|
|
248
|
+
this.log.warn("project knowledge not read: the device cannot enforce a read-only read", {
|
|
249
|
+
...where,
|
|
250
|
+
deviceId,
|
|
251
|
+
});
|
|
252
|
+
return unavailableProject(bound.project, bound.root, projectUnavailable.notEnforceable(bound.root));
|
|
253
|
+
}
|
|
219
254
|
}, signal);
|
|
220
255
|
}
|
|
221
256
|
catch (err) {
|
|
@@ -227,6 +262,33 @@ let ProjectKnowledgeService = class ProjectKnowledgeService {
|
|
|
227
262
|
return null;
|
|
228
263
|
}
|
|
229
264
|
}
|
|
265
|
+
/**
|
|
266
|
+
* How the read enters the device. Gate off: the raw `withSandbox`, acting for the conversation's
|
|
267
|
+
* owner (unchanged). Gate on: `withKernelRead` under the run's current execution claim (the run
|
|
268
|
+
* start runs inside it; another run's claim or none admits nothing), acting for the run's
|
|
269
|
+
* trigger, reading the bound root only.
|
|
270
|
+
*/
|
|
271
|
+
async entry(input, owner, root) {
|
|
272
|
+
if (!permissionContextActive(this.activation))
|
|
273
|
+
return Object.assign(this.sandbox.withSandbox.bind(this.sandbox), { accountId: owner });
|
|
274
|
+
const claim = currentExecutionClaim();
|
|
275
|
+
if (!claim ||
|
|
276
|
+
claim.runId !== input.runId ||
|
|
277
|
+
claim.conversationId !== input.conversationId ||
|
|
278
|
+
claim.orgId !== input.orgId)
|
|
279
|
+
throw new KnowledgeClaimMissing();
|
|
280
|
+
const run = await this.db.db
|
|
281
|
+
.selectFrom("runs")
|
|
282
|
+
.select("trigger_account_id")
|
|
283
|
+
.where("id", "=", input.runId)
|
|
284
|
+
.where("conversation_id", "=", input.conversationId)
|
|
285
|
+
.executeTakeFirst();
|
|
286
|
+
if (!run)
|
|
287
|
+
throw new KnowledgeClaimMissing();
|
|
288
|
+
const read = { claim, purpose: "project_root", root };
|
|
289
|
+
const enter = (ctx, fn, signal) => this.sandbox.withKernelRead(ctx, read, fn, signal);
|
|
290
|
+
return Object.assign(enter, { accountId: run.trigger_account_id });
|
|
291
|
+
}
|
|
230
292
|
/**
|
|
231
293
|
* One transaction under the conversation row lock (as `PluginContextService`): compares the
|
|
232
294
|
* snapshot with the conversation's latest `project_root` and appends it when it differs, with
|
|
@@ -283,6 +345,8 @@ ProjectKnowledgeService = __decorate([
|
|
|
283
345
|
__param(5, Inject(SandboxService)),
|
|
284
346
|
__param(6, Inject(KERNEL_LOGGER)),
|
|
285
347
|
__param(7, Optional()),
|
|
286
|
-
__param(7, Inject(PROJECT_KNOWLEDGE_TIMEOUT))
|
|
348
|
+
__param(7, Inject(PROJECT_KNOWLEDGE_TIMEOUT)),
|
|
349
|
+
__param(8, Optional()),
|
|
350
|
+
__param(8, Inject(PERMISSION_CONTEXT_ACTIVATION))
|
|
287
351
|
], ProjectKnowledgeService);
|
|
288
352
|
export { ProjectKnowledgeService };
|
|
@@ -8,6 +8,7 @@ export interface ExecutionClaim {
|
|
|
8
8
|
rootId: string;
|
|
9
9
|
executionId: string;
|
|
10
10
|
}
|
|
11
|
+
export declare const currentExecutionClaim: () => Readonly<ExecutionClaim> | undefined;
|
|
11
12
|
/**
|
|
12
13
|
* Runs `fn` (a tool step, a sub-agent dispatch) with every fenced transaction it opens admitted
|
|
13
14
|
* exclusively, even one that asks for shared admission (an event append made by a tool). Tool code
|
|
@@ -50,7 +51,9 @@ export declare class ExecutionRevoked extends Error {
|
|
|
50
51
|
* (`OrgLock.lockForRemoval`): they wait for the admitted writes, keep new ones out (a later one
|
|
51
52
|
* then sees the org deleted), and are not starved by a stream of them (the lock queues fairly).
|
|
52
53
|
*
|
|
53
|
-
* Global order: org admission lock → org row →
|
|
54
|
+
* Global order (`OrgLock`): org admission lock → org row → membership → device row → super admin
|
|
55
|
+
* lock → permission authority locks (an ordered prelude, `withTransactionFencePrelude`, runs here
|
|
56
|
+
* and takes them with the locks before them) → root conversation → conversation; a writer that
|
|
54
57
|
* locks several conversation or run rows of one tree locks the tree's root first (the fence
|
|
55
58
|
* does, so every fenced write of a tree serializes on its root). A claim never
|
|
56
59
|
* opens a nested fenced `requires_new` transaction (shared or not) and waits for it while its
|
|
@@ -15,6 +15,9 @@ import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
|
15
15
|
import { XactLock } from "../infra/database/xact-lock.js";
|
|
16
16
|
import { isLive } from "./run-liveness.js";
|
|
17
17
|
import { rootConversationOf } from "./subagents/child.js";
|
|
18
|
+
// This carries invocation identity only. Consumers must revalidate it in PostgreSQL.
|
|
19
|
+
const invocationClaim = new AsyncLocalStorage();
|
|
20
|
+
export const currentExecutionClaim = () => invocationClaim.getStore();
|
|
18
21
|
/** Set inside a tool step: every fenced transaction there is admitted exclusively. */
|
|
19
22
|
const exclusiveScope = new AsyncLocalStorage();
|
|
20
23
|
/**
|
|
@@ -65,7 +68,9 @@ export class ExecutionRevoked extends Error {
|
|
|
65
68
|
* (`OrgLock.lockForRemoval`): they wait for the admitted writes, keep new ones out (a later one
|
|
66
69
|
* then sees the org deleted), and are not starved by a stream of them (the lock queues fairly).
|
|
67
70
|
*
|
|
68
|
-
* Global order: org admission lock → org row →
|
|
71
|
+
* Global order (`OrgLock`): org admission lock → org row → membership → device row → super admin
|
|
72
|
+
* lock → permission authority locks (an ordered prelude, `withTransactionFencePrelude`, runs here
|
|
73
|
+
* and takes them with the locks before them) → root conversation → conversation; a writer that
|
|
69
74
|
* locks several conversation or run rows of one tree locks the tree's root first (the fence
|
|
70
75
|
* does, so every fenced write of a tree serializes on its root). A claim never
|
|
71
76
|
* opens a nested fenced `requires_new` transaction (shared or not) and waits for it while its
|
|
@@ -107,9 +112,11 @@ let RunExecution = class RunExecution {
|
|
|
107
112
|
return run ? claim : null;
|
|
108
113
|
}
|
|
109
114
|
withClaim(claim, live, fn) {
|
|
110
|
-
return withTransactionFence(async ({ shared }) => {
|
|
115
|
+
return invocationClaim.run(Object.freeze({ ...claim }), () => withTransactionFence(async ({ shared, prelude }) => {
|
|
111
116
|
const admitShared = shared && !exclusiveScope.getStore();
|
|
112
|
-
if (
|
|
117
|
+
if (prelude && admitShared)
|
|
118
|
+
throw new Error("An ordered prelude needs exclusive admission");
|
|
119
|
+
if (!live() || !(await this.lockLiveConversation(claim, admitShared, prelude))) {
|
|
113
120
|
throw new ExecutionRevoked();
|
|
114
121
|
}
|
|
115
122
|
const run = await this.db
|
|
@@ -121,7 +128,7 @@ let RunExecution = class RunExecution {
|
|
|
121
128
|
if (run?.execution_id !== claim.executionId || !live())
|
|
122
129
|
throw new ExecutionRevoked();
|
|
123
130
|
return admitShared ? "shared" : "exclusive";
|
|
124
|
-
}, fn);
|
|
131
|
+
}, fn));
|
|
125
132
|
}
|
|
126
133
|
/**
|
|
127
134
|
* The admission (see the class), then the root and the current conversation; NO KEY UPDATE
|
|
@@ -130,7 +137,7 @@ let RunExecution = class RunExecution {
|
|
|
130
137
|
* depth 1) under `FOR UPDATE`, so two siblings holding the root only shared would deadlock
|
|
131
138
|
* upgrading it.
|
|
132
139
|
*/
|
|
133
|
-
async lockLiveConversation(claim, shared = false) {
|
|
140
|
+
async lockLiveConversation(claim, shared = false, prelude) {
|
|
134
141
|
// shared: after the admission lock, a plain read sees a committed deletion (a new statement
|
|
135
142
|
// under READ COMMITTED), and the deletion cannot commit before this transaction ends
|
|
136
143
|
if (shared)
|
|
@@ -143,6 +150,8 @@ let RunExecution = class RunExecution {
|
|
|
143
150
|
.executeTakeFirst();
|
|
144
151
|
if (!org || !isLive(org.deleted_at))
|
|
145
152
|
return false;
|
|
153
|
+
// the permission authority locks: after the org row, before the conversation rows
|
|
154
|
+
await prelude?.();
|
|
146
155
|
const root = await this.db
|
|
147
156
|
.selectFrom("conversations")
|
|
148
157
|
.select("deleted_at")
|