@sema-agent/core 7.71.1 → 7.73.0
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/CHANGELOG.md +500 -0
- package/dist/agents/cross-session-ref.d.ts +2 -2
- package/dist/agents/cross-session-ref.js +7 -2
- package/dist/agents/inherited-tool-pool.d.ts +30 -2
- package/dist/agents/inherited-tool-pool.js +53 -7
- package/dist/agents/parked-resume-drive.d.ts +10 -0
- package/dist/agents/parked-resume-drive.js +9 -0
- package/dist/agents/retain-ledger.d.ts +3 -0
- package/dist/agents/send-message-tool.js +7 -2
- package/dist/agents/send-user-message-tool.d.ts +12 -0
- package/dist/agents/send-user-message-tool.js +67 -0
- package/dist/agents/subagent-steps.d.ts +9 -0
- package/dist/agents/subagent-steps.js +38 -0
- package/dist/agents/subagent.d.ts +2 -0
- package/dist/agents/subagent.js +47 -12
- package/dist/agents/verify.js +23 -7
- package/dist/agents/yield-adoption.js +3 -1
- package/dist/brain/anthropic.js +14 -9
- package/dist/brain/open-responses.js +4 -5
- package/dist/brain/openai.js +30 -23
- package/dist/brain/stream-engine.js +159 -151
- package/dist/core/a2a.d.ts +24 -3
- package/dist/core/a2a.js +27 -8
- package/dist/core/actions-digest.d.ts +43 -0
- package/dist/core/actions-digest.js +22 -0
- package/dist/core/agent-state.d.ts +28 -7
- package/dist/core/agent-state.js +19 -6
- package/dist/core/allow-entry.js +3 -3
- package/dist/core/approval-object.d.ts +7 -3
- package/dist/core/approval-object.js +30 -9
- package/dist/core/auto-mode.d.ts +7 -9
- package/dist/core/auto-mode.js +114 -8
- package/dist/core/checkpoint-features.d.ts +16 -5
- package/dist/core/checkpoint-features.js +35 -6
- package/dist/core/checkpoint-rekey.d.ts +1 -1
- package/dist/core/checkpoint-rekey.js +6 -1
- package/dist/core/checkpoint-requirements.d.ts +23 -3
- package/dist/core/checkpoint-requirements.js +5 -1
- package/dist/core/checkpoint-row-verdict.d.ts +1 -1
- package/dist/core/checkpoint-row-verdict.js +10 -4
- package/dist/core/checkpoint-store.d.ts +24 -2
- package/dist/core/checkpoint-store.js +1 -0
- package/dist/core/child-clamp-seats.d.ts +15 -0
- package/dist/core/child-clamp-seats.js +15 -0
- package/dist/core/delegation-frames.d.ts +1 -0
- package/dist/core/delegation-frames.js +1 -1
- package/dist/core/effective-path-target.d.ts +19 -0
- package/dist/core/effective-path-target.js +95 -38
- package/dist/core/engine-capabilities.d.ts +5 -1
- package/dist/core/engine-capabilities.js +1 -1
- package/dist/core/engine-notice.d.ts +34 -23
- package/dist/core/engine-version.js +1 -1
- package/dist/core/entries-not-applied.d.ts +19 -7
- package/dist/core/entries-not-applied.js +33 -1
- package/dist/core/file-history-store.d.ts +15 -4
- package/dist/core/file-history-store.js +53 -23
- package/dist/core/gate-fold.js +3 -1
- package/dist/core/governance-codes.d.ts +10 -1
- package/dist/core/governance-codes.js +17 -5
- package/dist/core/hooks.d.ts +3 -0
- package/dist/core/hooks.js +1 -0
- package/dist/core/host-instance.d.ts +23 -0
- package/dist/core/host-instance.js +69 -0
- package/dist/core/instruction-preview.d.ts +63 -0
- package/dist/core/instruction-preview.js +406 -0
- package/dist/core/judged-read.d.ts +57 -0
- package/dist/core/judged-read.js +1 -0
- package/dist/core/leg-budget.d.ts +26 -0
- package/dist/core/leg-budget.js +9 -0
- package/dist/core/leg-identity.d.ts +51 -1
- package/dist/core/leg-identity.js +45 -2
- package/dist/core/manager-launch-check.d.ts +48 -0
- package/dist/core/manager-launch-check.js +64 -0
- package/dist/core/mcp-call-meta.d.ts +2 -2
- package/dist/core/mcp-host-seat.d.ts +5 -0
- package/dist/core/mcp-host-seat.js +7 -1
- package/dist/core/mcp-injection-drop.d.ts +20 -10
- package/dist/core/mcp-injection-drop.js +9 -3
- package/dist/core/mcp-server-spec.d.ts +2 -1
- package/dist/core/mcp.js +3 -1
- package/dist/core/member-outcome.d.ts +3 -2
- package/dist/core/member-outcome.js +1 -1
- package/dist/core/memory-engine/deletion-record.d.ts +46 -0
- package/dist/core/memory-engine/deletion-record.js +96 -0
- package/dist/core/memory-engine/engine.js +24 -16
- package/dist/core/memory-engine/file-backend.js +7 -5
- package/dist/core/memory-engine/index.d.ts +1 -1
- package/dist/core/memory-engine/index.js +1 -1
- package/dist/core/memory-engine/layout.js +6 -4
- package/dist/core/memory-engine/projection-ledger.d.ts +1 -1
- package/dist/core/memory-engine/projection-ledger.js +1 -1
- package/dist/core/memory-engine/scope-contract.d.ts +69 -11
- package/dist/core/memory-engine/scope-contract.js +99 -34
- package/dist/core/memory-engine/sync-client.js +26 -6
- package/dist/core/memory-engine/sync.d.ts +20 -2
- package/dist/core/memory-engine/sync.js +18 -17
- package/dist/core/mode-policy.d.ts +21 -6
- package/dist/core/mode-policy.js +15 -6
- package/dist/core/mounted-mcp.d.ts +7 -2
- package/dist/core/mounted-mcp.js +10 -1
- package/dist/core/own-property-receipt.js +11 -4
- package/dist/core/param-rule-reach.d.ts +8 -3
- package/dist/core/param-rule-reach.js +9 -1
- package/dist/core/parent-spec-seat.d.ts +13 -1
- package/dist/core/parent-spec-seat.js +18 -0
- package/dist/core/peer-registration.d.ts +52 -0
- package/dist/core/peer-registration.js +37 -0
- package/dist/core/peer-seat.d.ts +3 -1
- package/dist/core/peer-seat.js +3 -3
- package/dist/core/peer-task-store.d.ts +365 -0
- package/dist/core/peer-task-store.js +682 -0
- package/dist/core/permission-rule-consent.js +6 -11
- package/dist/core/permission-rule-model.d.ts +27 -0
- package/dist/core/permission-rule-model.js +34 -6
- package/dist/core/permission-rule-provider.d.ts +35 -33
- package/dist/core/permission-rule-provider.js +38 -295
- package/dist/core/permission-rule-store.d.ts +181 -8
- package/dist/core/permission-rule-store.js +420 -17
- package/dist/core/permission-rule-sync.js +15 -1
- package/dist/core/project-seat.d.ts +55 -0
- package/dist/core/project-seat.js +96 -0
- package/dist/core/protocol-http-auth.d.ts +3 -2
- package/dist/core/protocol-http-auth.js +26 -18
- package/dist/core/remote-env.d.ts +4 -4
- package/dist/core/retired-keys.js +1 -0
- package/dist/core/rule-offer-read.d.ts +1 -1
- package/dist/core/rule-offer-read.js +32 -4
- package/dist/core/run-ledger-seat.d.ts +66 -0
- package/dist/core/run-ledger-seat.js +70 -0
- package/dist/core/run-ledger.d.ts +332 -0
- package/dist/core/run-ledger.js +550 -0
- package/dist/core/runner/chain-mismatch.d.ts +32 -0
- package/dist/core/runner/chain-mismatch.js +29 -0
- package/dist/core/runner/changes-history.d.ts +1 -0
- package/dist/core/runner/changes-history.js +2 -2
- package/dist/core/runner/checkpoint-scope.d.ts +13 -3
- package/dist/core/runner/checkpoint-scope.js +12 -4
- package/dist/core/runner/construction-doors.js +6 -0
- package/dist/core/runner/contracts.d.ts +22 -3
- package/dist/core/runner/delegation-owner.d.ts +30 -0
- package/dist/core/runner/delegation-owner.js +20 -4
- package/dist/core/runner/file-history-restore.d.ts +3 -1
- package/dist/core/runner/file-history-restore.js +9 -6
- package/dist/core/runner/finish-reason-seat.js +4 -2
- package/dist/core/runner/leg-frame.js +3 -0
- package/dist/core/runner/loaded-skills.d.ts +2 -0
- package/dist/core/runner/loaded-skills.js +10 -0
- package/dist/core/runner/nested-memory-lane.d.ts +22 -5
- package/dist/core/runner/nested-memory-lane.js +91 -30
- package/dist/core/runner/person-path-rows.d.ts +23 -20
- package/dist/core/runner/person-path-rows.js +82 -18
- package/dist/core/runner/prepare-caps-and-workflow.d.ts +4 -0
- package/dist/core/runner/prepare-caps-and-workflow.js +105 -90
- package/dist/core/runner/prepare-config-doors.d.ts +1 -0
- package/dist/core/runner/prepare-config-doors.js +5 -3
- package/dist/core/runner/prepare-context-lane.js +1 -0
- package/dist/core/runner/prepare-delegation-surface.d.ts +3 -0
- package/dist/core/runner/prepare-delegation-surface.js +6 -0
- package/dist/core/runner/prepare-execution-env.d.ts +3 -3
- package/dist/core/runner/prepare-file-history.js +1 -1
- package/dist/core/runner/prepare-hands-readface.d.ts +10 -2
- package/dist/core/runner/prepare-hands-readface.js +36 -22
- package/dist/core/runner/prepare-inherited-gate.d.ts +8 -5
- package/dist/core/runner/prepare-inherited-gate.js +14 -6
- package/dist/core/runner/prepare-memory-engine-session.d.ts +2 -2
- package/dist/core/runner/prepare-memory-engine-session.js +2 -2
- package/dist/core/runner/prepare-memory.js +3 -15
- package/dist/core/runner/prepare-project-context.d.ts +1 -1
- package/dist/core/runner/prepare-project-context.js +3 -7
- package/dist/core/runner/prepare-protocol-tools.d.ts +5 -0
- package/dist/core/runner/prepare-protocol-tools.js +2 -2
- package/dist/core/runner/prepare-run-refs.d.ts +1 -1
- package/dist/core/runner/prepare-run-refs.js +3 -2
- package/dist/core/runner/prepare-suspend-saga.d.ts +6 -2
- package/dist/core/runner/prepare-suspend-saga.js +4 -2
- package/dist/core/runner/prepare-task.js +50 -37
- package/dist/core/runner/prepare-turn-wiring.d.ts +4 -3
- package/dist/core/runner/prepare-turn-wiring.js +2 -1
- package/dist/core/runner/prepare-wiring-manifest.d.ts +5 -1
- package/dist/core/runner/prepare-wiring-manifest.js +8 -4
- package/dist/core/runner/read-session-changes.js +1 -1
- package/dist/core/runner/resume-apply.js +11 -5
- package/dist/core/runner/resume-checkpoint-screen.d.ts +8 -7
- package/dist/core/runner/resume-checkpoint-screen.js +7 -6
- package/dist/core/runner/resume-review-outcome.d.ts +3 -0
- package/dist/core/runner/resume-review-outcome.js +24 -1
- package/dist/core/runner/rewind-files.js +3 -2
- package/dist/core/runner/run-harness-handlers.js +3 -0
- package/dist/core/runner/run-identity-wiring.js +3 -2
- package/dist/core/runner/run-leg.js +9 -2
- package/dist/core/runner/run-notification-lane.js +20 -1
- package/dist/core/runner/run-settle-and-teardown.js +21 -12
- package/dist/core/runner/run-stop-and-final-verify.js +6 -0
- package/dist/core/runner/run-telemetry-and-budget-seats.js +1 -1
- package/dist/core/runner/runtask.d.ts +6 -2
- package/dist/core/runner/runtask.js +21 -9
- package/dist/core/runner/shell-env-withheld.d.ts +3 -2
- package/dist/core/runner/shell-env-withheld.js +1 -2
- package/dist/core/runner/stop-tail-notice.d.ts +10 -2
- package/dist/core/runner/stop-tail-notice.js +9 -2
- package/dist/core/runner/stream-settle-backstop.d.ts +1 -0
- package/dist/core/runner/stream-settle-backstop.js +1 -0
- package/dist/core/runner/task-start-doors.js +6 -3
- package/dist/core/runner/terminal-account.d.ts +10 -1
- package/dist/core/runner/terminal-account.js +23 -3
- package/dist/core/runner/tool-face-overlay.d.ts +2 -11
- package/dist/core/runner/tool-face-overlay.js +39 -0
- package/dist/core/runner/trim-stretch.js +7 -2
- package/dist/core/runner/unreadable-person-rows.d.ts +2 -1
- package/dist/core/runner-deps.d.ts +13 -10
- package/dist/core/scheduler.d.ts +47 -7
- package/dist/core/scheduler.js +19 -7
- package/dist/core/secret-env.d.ts +60 -37
- package/dist/core/secret-env.js +45 -48
- package/dist/core/sensitive-path-policy.js +14 -7
- package/dist/core/session-checkout.d.ts +3 -0
- package/dist/core/session-checkout.js +1 -0
- package/dist/core/session-store.d.ts +12 -6
- package/dist/core/session-store.js +26 -6
- package/dist/core/session.d.ts +31 -8
- package/dist/core/shell-read-silence.js +1 -0
- package/dist/core/store-contracts/checkpoint-store-contract.js +17 -0
- package/dist/core/store-contracts/peer-task-store-contract.d.ts +21 -0
- package/dist/core/store-contracts/peer-task-store-contract.js +516 -0
- package/dist/core/store-contracts/session-store-contract.d.ts +15 -2
- package/dist/core/store-contracts/session-store-contract.js +87 -3
- package/dist/core/store-contracts/workflow-journal-store-contract.d.ts +19 -1
- package/dist/core/store-contracts/workflow-journal-store-contract.js +89 -1
- package/dist/core/task-event.d.ts +11 -3
- package/dist/core/task-notification.d.ts +97 -10
- package/dist/core/task-notification.js +129 -5
- package/dist/core/task-registry-shared.d.ts +12 -1
- package/dist/core/task-registry-shared.js +3 -1
- package/dist/core/task-registry-workflow.d.ts +1 -1
- package/dist/core/task-registry-workflow.js +9 -1
- package/dist/core/task-registry.d.ts +39 -2
- package/dist/core/task-registry.js +76 -12
- package/dist/core/task-result.d.ts +13 -0
- package/dist/core/task-spec-members.d.ts +3 -1
- package/dist/core/task-spec-members.js +11 -3
- package/dist/core/task-spec.d.ts +57 -23
- package/dist/core/tool-catalog-entries.js +4 -0
- package/dist/core/tool-face-reader.js +1 -1
- package/dist/core/tool-face.d.ts +7 -0
- package/dist/core/tool-face.js +1 -0
- package/dist/core/tool-mount-facts.d.ts +5 -3
- package/dist/core/tool-mount-facts.js +4 -3
- package/dist/core/tool-mount-plan.d.ts +7 -3
- package/dist/core/tool-mount-plan.js +11 -5
- package/dist/core/tool-mount-static.d.ts +2 -2
- package/dist/core/tool-mount-static.js +28 -14
- package/dist/core/tool-registry.d.ts +33 -1
- package/dist/core/tool-registry.js +33 -0
- package/dist/core/tool-result-store.d.ts +19 -2
- package/dist/core/tool-result-store.js +51 -2
- package/dist/core/tool-roster.d.ts +16 -0
- package/dist/core/tool-roster.js +6 -0
- package/dist/core/tool-spec.d.ts +13 -4
- package/dist/core/tools.d.ts +6 -0
- package/dist/core/tools.js +1 -1
- package/dist/core/user-attachment.d.ts +5 -1
- package/dist/core/user-attachment.js +14 -7
- package/dist/core/wiring-manifest.d.ts +89 -29
- package/dist/core/wiring-manifest.js +44 -4
- package/dist/core/workflow-journal-store.d.ts +53 -10
- package/dist/core/workflow-journal-store.js +27 -4
- package/dist/core/workflow-run-store-contract.js +16 -0
- package/dist/core/workflow-run-store.d.ts +9 -7
- package/dist/core/workflow-run-store.js +4 -1
- package/dist/engine/execution-env/node-execution-env.js +3 -1
- package/dist/engine/harness/messages.d.ts +1 -1
- package/dist/engine/harness/messages.js +2 -1
- package/dist/engine/harness/types.d.ts +24 -1
- package/dist/engine/harness/types.js +10 -0
- package/dist/engine/llm/types.d.ts +5 -7
- package/dist/engine/loop/types.d.ts +2 -0
- package/dist/engine/lsp/node-lsp-manager.js +2 -1
- package/dist/engine/session/import-validate.js +6 -1
- package/dist/engine/session/session.d.ts +15 -2
- package/dist/engine/session/session.js +35 -5
- package/dist/index.d.ts +27 -12
- package/dist/index.js +20 -8
- package/dist/orchestration/run-workflow-tool.js +40 -32
- package/dist/orchestration/workflow-park-adoption.d.ts +51 -12
- package/dist/orchestration/workflow-park-adoption.js +47 -12
- package/dist/orchestration/workflow-stop.d.ts +128 -0
- package/dist/orchestration/workflow-stop.js +156 -0
- package/dist/orchestration/workflow.d.ts +30 -6
- package/dist/orchestration/workflow.js +117 -54
- package/dist/prompts/manager-recipe.d.ts +18 -0
- package/dist/prompts/manager-recipe.js +24 -0
- package/dist/stores/file/adoption/marker.d.ts +2 -27
- package/dist/stores/file/adoption/marker.js +12 -0
- package/dist/stores/file/index.d.ts +1 -1
- package/dist/stores/file/index.js +1 -1
- package/dist/stores/file/permission-rule-store.d.ts +3 -1
- package/dist/stores/file/permission-rule-store.js +0 -1
- package/dist/stores/file/workflow-journal-store.d.ts +16 -3
- package/dist/stores/file/workflow-journal-store.js +93 -5
- package/dist/stores/file/workflow-run-store.d.ts +1 -1
- package/dist/tools/artifact/artifact-tool.js +11 -6
- package/dist/tools/fs/file-read-check.d.ts +45 -11
- package/dist/tools/fs/file-read-check.js +200 -57
- package/dist/tools/fs/file-write-check.js +9 -3
- package/dist/tools/fs/index.d.ts +8 -7
- package/dist/tools/fs/index.js +4 -4
- package/dist/tools/fs/judged-reads.d.ts +51 -0
- package/dist/tools/fs/judged-reads.js +68 -0
- package/dist/tools/fs/read-deny-home.d.ts +1 -1
- package/dist/tools/fs/read-deny-home.js +5 -5
- package/dist/tools/fs/read-deny.d.ts +33 -8
- package/dist/tools/fs/read-deny.js +114 -25
- package/dist/tools/fs/safety.d.ts +12 -1
- package/dist/tools/fs/safety.js +16 -3
- package/dist/tools/fs/shell-read-check.d.ts +25 -5
- package/dist/tools/fs/shell-read-check.js +100 -15
- package/dist/tools/fs/shell-read-establish.d.ts +9 -0
- package/dist/tools/fs/shell-read-establish.js +34 -14
- package/dist/tools/fs/shell-read-judge.d.ts +13 -1
- package/dist/tools/fs/shell-read-judge.js +26 -7
- package/dist/tools/fs/shell-read-plan.d.ts +5 -0
- package/dist/tools/fs/shell-read-write-targets.js +17 -9
- package/dist/tools/scheduler-store-move.d.ts +74 -0
- package/dist/tools/scheduler-store-move.js +103 -0
- package/dist/tools/scheduler-tools.d.ts +43 -29
- package/dist/tools/scheduler-tools.js +161 -118
- package/package.json +2 -2
- package/test/export-surface.snapshot.json +173 -3
- package/wire-changes/answers/core-stores-sql@7.72.0.json +12 -0
- package/wire-changes/answers/core-stores-sql@7.73.0.json +28 -0
- package/wire-changes/answers/core@7.72.0.json +84 -0
- package/wire-changes/answers/core@7.73.0.json +216 -0
- package/wire-changes/answers/mods-kernel@7.72.0.json +7 -0
- package/wire-changes/answers/mods-kernel@7.73.0.json +7 -0
- package/wire-changes/answers/primitives@7.72.0.json +6 -0
- package/wire-changes/answers/primitives@7.73.0.json +12 -0
- package/wire-changes/answers/wiki-gate@7.72.0.json +7 -0
- package/wire-changes/answers/wiki-gate@7.73.0.json +7 -0
- package/wire-changes/core-stores-sql@7.72.0.json +215 -0
- package/wire-changes/core-stores-sql@7.73.0.json +326 -0
- package/wire-changes/core@7.72.0.json +2426 -0
- package/wire-changes/core@7.73.0.json +6480 -0
- package/wire-changes/mods-kernel@7.72.0.json +147 -0
- package/wire-changes/mods-kernel@7.73.0.json +147 -0
- package/wire-changes/primitives@7.72.0.json +143 -0
- package/wire-changes/primitives@7.73.0.json +227 -0
- package/wire-changes/wiki-gate@7.72.0.json +145 -0
- package/wire-changes/wiki-gate@7.73.0.json +145 -0
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { types } from "node:util";
|
|
3
|
+
import { isNameableToolName } from "@sema-agent/primitives";
|
|
2
4
|
import { canonicalize, textContentHashOf } from "./canonical-json.js";
|
|
3
|
-
import { escapeForDisclosure, isRuleBehavior, isRuleLive, parseRuleText } from "./permission-rule-model.js";
|
|
5
|
+
import { PATH_RULE_FACES, escapeForDisclosure, formatRuleText, isRuleBehavior, isRuleLive, parseRuleText } from "./permission-rule-model.js";
|
|
4
6
|
import { readRuleOwner } from "./rule-owner.js";
|
|
5
7
|
import { callGrantRowFault } from "./session-call-grant.js";
|
|
6
8
|
import { PRINCIPAL_NAME_SPELLING } from "./principal-name.js";
|
|
@@ -105,27 +107,387 @@ export function sameScope(a, b) {
|
|
|
105
107
|
return b.kind === "project" && a.root === b.root;
|
|
106
108
|
}
|
|
107
109
|
function sameDot(a, b) {
|
|
108
|
-
return a.actor === b.actor && a.counter === b.counter;
|
|
110
|
+
return a.actor === b.actor && (a.counter === b.counter || Object.is(a.counter, b.counter));
|
|
111
|
+
}
|
|
112
|
+
export const UNREADABLE_RULE_TEXT = "(unreadable)";
|
|
113
|
+
export function storedRuleTextOf(read) {
|
|
114
|
+
const { tool, match } = read;
|
|
115
|
+
const command = match === "tool" ? "" : read.command;
|
|
116
|
+
if (typeof tool !== "string" || typeof match !== "string" || typeof command !== "string")
|
|
117
|
+
return UNREADABLE_RULE_TEXT;
|
|
118
|
+
try {
|
|
119
|
+
const rule = formatRuleText(command, match, tool);
|
|
120
|
+
return "reject" in normalizePersistedRule({ ...read, command, rule }) ? UNREADABLE_RULE_TEXT : rule;
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return UNREADABLE_RULE_TEXT;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const GLOBAL_SCOPE = Object.freeze({ kind: "global" });
|
|
127
|
+
function scopeReads(scope) {
|
|
128
|
+
if (scope === null || typeof scope !== "object")
|
|
129
|
+
return false;
|
|
130
|
+
const primitive = (k) => {
|
|
131
|
+
try {
|
|
132
|
+
const v = Reflect.get(scope, k);
|
|
133
|
+
return v === null || (typeof v !== "object" && typeof v !== "function");
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
if (!primitive("kind"))
|
|
140
|
+
return false;
|
|
141
|
+
const kind = scope.kind;
|
|
142
|
+
return kind === "project" ? primitive("root") : kind === "session" ? primitive("sessionId") : true;
|
|
143
|
+
}
|
|
144
|
+
export function storedRuleIdentityOf(row) {
|
|
145
|
+
if (row === null || typeof row !== "object")
|
|
146
|
+
return { behavior: "ask", rule: UNREADABLE_RULE_TEXT, scope: GLOBAL_SCOPE };
|
|
147
|
+
const read = (k) => {
|
|
148
|
+
try {
|
|
149
|
+
return Reflect.get(row, k);
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const behavior = read("behavior");
|
|
156
|
+
const rule = read("rule");
|
|
157
|
+
const scope = read("scope");
|
|
158
|
+
if (!isRuleBehavior(behavior)) {
|
|
159
|
+
return {
|
|
160
|
+
behavior: "ask",
|
|
161
|
+
rule: typeof rule === "string" ? rule : UNREADABLE_RULE_TEXT,
|
|
162
|
+
scope: isValidDurableScope(scope) ? scope : GLOBAL_SCOPE,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
behavior,
|
|
167
|
+
rule: typeof rule === "string" ? rule : storedRuleTextOf({ behavior, tool: read("tool"), match: read("match"), command: read("command"), scope }),
|
|
168
|
+
scope: scopeReads(scope) ? scope : GLOBAL_SCOPE,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function storedExactIdentityOf(row) {
|
|
172
|
+
if (row === null || typeof row !== "object")
|
|
173
|
+
return undefined;
|
|
174
|
+
const read = (k) => {
|
|
175
|
+
try {
|
|
176
|
+
return Reflect.get(row, k);
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
const behavior = read("behavior");
|
|
183
|
+
const rule = read("rule");
|
|
184
|
+
const scope = read("scope");
|
|
185
|
+
return isRuleBehavior(behavior) && typeof rule === "string" && scopeReads(scope) ? { behavior, rule, scope } : undefined;
|
|
186
|
+
}
|
|
187
|
+
function sameDisplayedIdentity(row, identity) {
|
|
188
|
+
const x = storedRuleIdentityOf(row);
|
|
189
|
+
return x.behavior === identity.behavior && x.rule === identity.rule && sameScope(x.scope, identity.scope);
|
|
109
190
|
}
|
|
110
191
|
export function sameRuleIdentity(a, b) {
|
|
111
|
-
|
|
192
|
+
const x = storedExactIdentityOf(a);
|
|
193
|
+
const y = storedExactIdentityOf(b);
|
|
194
|
+
return x !== undefined && y !== undefined && x.behavior === y.behavior && x.rule === y.rule && sameScope(x.scope, y.scope);
|
|
195
|
+
}
|
|
196
|
+
function heldAddsOf(row) {
|
|
197
|
+
if (row === null || typeof row !== "object")
|
|
198
|
+
return undefined;
|
|
199
|
+
try {
|
|
200
|
+
const adds = Reflect.get(row, "adds");
|
|
201
|
+
return Array.isArray(adds) ? adds : undefined;
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const DOT_SHAPE = { members: { actor: "primitive", counter: "primitive" } };
|
|
208
|
+
const SCOPE_SHAPE = { members: { kind: "primitive" }, byKind: { project: { root: "primitive" }, session: { sessionId: "primitive" } }, held: true };
|
|
209
|
+
const ADD_DOT_SHAPE = { members: { actor: "value", counter: "value" }, held: true };
|
|
210
|
+
const ADD_SHAPE = { members: { dot: ADD_DOT_SHAPE, origin: "primitive", createdAt: "time", peerFingerprint: "primitive" }, own: ["peerFingerprint"], held: true };
|
|
211
|
+
const RULE_ROW_MEMBERS = { behavior: "text", rule: "text", tool: "text", match: "text", command: "text", scope: SCOPE_SHAPE, adds: { listOf: ADD_SHAPE, held: true } };
|
|
212
|
+
export const TOMBSTONE_ROW_MEMBERS = { behavior: "primitive", rule: "primitive", scope: SCOPE_SHAPE, removedDots: { listOf: DOT_SHAPE }, deletedBy: DOT_SHAPE };
|
|
213
|
+
const TIME_OF_DATE = Date.prototype.getTime;
|
|
214
|
+
const ISO_OF_DATE = Date.prototype.toISOString;
|
|
215
|
+
const unreadValue = (why, kept) => ({ ok: false, why, ...(kept !== undefined ? { kept } : {}) });
|
|
216
|
+
export function servedValueOf(v, shape) {
|
|
217
|
+
const held = heldKindOf(shape);
|
|
218
|
+
if (v === null || (typeof v !== "object" && typeof v !== "function")) {
|
|
219
|
+
if (held === undefined || (held === "a text" && typeof v === "string") || (held === "a value" && v !== null && v !== undefined))
|
|
220
|
+
return { ok: true, v };
|
|
221
|
+
return unreadValue(`${v === null || v === undefined ? String(v) : `a ${typeof v}`} where ${held} belongs`);
|
|
222
|
+
}
|
|
223
|
+
if (shape === "time" && typeof v === "object" && !types.isProxy(v) && types.isDate(v) && Number.isFinite(TIME_OF_DATE.call(v)))
|
|
224
|
+
return { ok: true, v: ISO_OF_DATE.call(v) };
|
|
225
|
+
if (typeof shape === "string" || typeof v === "function")
|
|
226
|
+
return unreadValue(`${typeof v === "function" ? "a function" : shape === "time" ? "an object that is no valid date" : "an object"} where a ${typeof shape === "string" ? shape : "primitive"} belongs`);
|
|
227
|
+
if ("listOf" in shape) {
|
|
228
|
+
const length = servedLengthOf(v);
|
|
229
|
+
if ("why" in length)
|
|
230
|
+
return unreadValue(length.why);
|
|
231
|
+
const out = new Array(length.n);
|
|
232
|
+
const kept = [];
|
|
233
|
+
let why;
|
|
234
|
+
for (let i = 0; i < length.n; i++) {
|
|
235
|
+
const el = memberOf(v, i);
|
|
236
|
+
const got = "why" in el ? el : el.held ? servedValueOf(el.v, shape.listOf) : heldKindOf(shape.listOf) !== undefined ? unreadValue(`a hole where ${heldKindOf(shape.listOf)} belongs`) : undefined;
|
|
237
|
+
if (got === undefined)
|
|
238
|
+
continue;
|
|
239
|
+
if ("why" in got)
|
|
240
|
+
why ??= `element #${i}: ${got.why}`;
|
|
241
|
+
else
|
|
242
|
+
kept.push((out[i] = got.v));
|
|
243
|
+
}
|
|
244
|
+
return why === undefined ? { ok: true, v: out } : unreadValue(why, kept);
|
|
245
|
+
}
|
|
246
|
+
const out = {};
|
|
247
|
+
const readMembers = (members) => {
|
|
248
|
+
for (const [k, member] of Object.entries(members)) {
|
|
249
|
+
const got = memberOf(v, k);
|
|
250
|
+
if ("why" in got)
|
|
251
|
+
return unreadValue(got.why);
|
|
252
|
+
if (!got.held) {
|
|
253
|
+
if (heldKindOf(member) !== undefined)
|
|
254
|
+
return unreadValue(`nothing where ${heldKindOf(member)} belongs`);
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (got.v !== undefined && got.v !== null && "members" in shape && shape.own?.includes(k) === true && !ownMemberOf(v, k))
|
|
258
|
+
return unreadValue(`${k}: a value its prototype supplies, not the object's own`);
|
|
259
|
+
const value = servedValueOf(got.v, member);
|
|
260
|
+
if (!value.ok)
|
|
261
|
+
return unreadValue(value.why);
|
|
262
|
+
out[k] = value.v;
|
|
263
|
+
}
|
|
264
|
+
return undefined;
|
|
265
|
+
};
|
|
266
|
+
const failed = readMembers(shape.members);
|
|
267
|
+
if (failed !== undefined)
|
|
268
|
+
return failed;
|
|
269
|
+
const kind = out.kind;
|
|
270
|
+
const byKind = shape.byKind !== undefined && typeof kind === "string" && Object.hasOwn(shape.byKind, kind) ? readMembers(shape.byKind[kind]) : undefined;
|
|
271
|
+
return byKind ?? { ok: true, v: out };
|
|
272
|
+
}
|
|
273
|
+
function memberOf(o, k) {
|
|
274
|
+
try {
|
|
275
|
+
const v = Reflect.get(o, k);
|
|
276
|
+
return v === undefined && !(k in o) ? { held: false } : { held: true, v };
|
|
277
|
+
}
|
|
278
|
+
catch (e) {
|
|
279
|
+
return { why: errText(e), thrown: e };
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function ownMemberOf(o, k) {
|
|
283
|
+
try {
|
|
284
|
+
return Object.hasOwn(o, k);
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
export function memberValueOf(o, k) {
|
|
291
|
+
const got = memberOf(o, k);
|
|
292
|
+
return "why" in got ? { ok: false, why: got.why, thrown: got.thrown } : { ok: true, v: got.held ? got.v : undefined };
|
|
293
|
+
}
|
|
294
|
+
const MAX_LIST_LENGTH = 2 ** 32 - 1;
|
|
295
|
+
export function servedLengthOf(v) {
|
|
296
|
+
let isList;
|
|
297
|
+
try {
|
|
298
|
+
isList = Array.isArray(v);
|
|
299
|
+
}
|
|
300
|
+
catch (e) {
|
|
301
|
+
return { why: errText(e), thrown: e };
|
|
302
|
+
}
|
|
303
|
+
if (!isList)
|
|
304
|
+
return { why: "an object where a list belongs" };
|
|
305
|
+
const got = memberValueOf(v, "length");
|
|
306
|
+
if (!got.ok)
|
|
307
|
+
return { why: got.why, ...("thrown" in got ? { thrown: got.thrown } : {}) };
|
|
308
|
+
const n = got.v;
|
|
309
|
+
return typeof n === "number" && Number.isInteger(n) && n >= 0 && n <= MAX_LIST_LENGTH ? { n } : { why: "a list whose length is no count" };
|
|
310
|
+
}
|
|
311
|
+
function heldKindOf(shape) {
|
|
312
|
+
if (shape === "text")
|
|
313
|
+
return "a text";
|
|
314
|
+
if (shape === "value")
|
|
315
|
+
return "a value";
|
|
316
|
+
if (typeof shape !== "object" || shape.held !== true)
|
|
317
|
+
return undefined;
|
|
318
|
+
return "listOf" in shape ? "a list" : "an object";
|
|
319
|
+
}
|
|
320
|
+
export function servedRowOf(row, members) {
|
|
321
|
+
if (row === null || typeof row !== "object") {
|
|
322
|
+
const why = `${row === null ? "null" : row === undefined ? "nothing" : `a ${typeof row}`} where a row belongs`;
|
|
323
|
+
return { failed: Object.keys(members).map((member) => ({ member, why })), read: {}, notObject: why };
|
|
324
|
+
}
|
|
325
|
+
const read = {};
|
|
326
|
+
const failed = [];
|
|
327
|
+
for (const [k, shape] of Object.entries(members)) {
|
|
328
|
+
const got = servedMemberOf(row, k, shape);
|
|
329
|
+
if ("why" in got)
|
|
330
|
+
failed.push({ member: k, why: got.why, ...("thrown" in got ? { threw: true } : {}), ...("kept" in got && got.kept !== undefined ? { kept: got.kept } : {}) });
|
|
331
|
+
else if (got.held)
|
|
332
|
+
read[k] = got.v;
|
|
333
|
+
}
|
|
334
|
+
return failed.length > 0 ? { failed: failed, read } : { row: read };
|
|
335
|
+
}
|
|
336
|
+
function servedMemberOf(row, k, shape) {
|
|
337
|
+
const got = memberOf(row, k);
|
|
338
|
+
if ("why" in got)
|
|
339
|
+
return got;
|
|
340
|
+
if (!got.held)
|
|
341
|
+
return heldKindOf(shape) !== undefined ? { why: `nothing where ${heldKindOf(shape)} belongs` } : got;
|
|
342
|
+
const value = servedValueOf(got.v, shape);
|
|
343
|
+
return value.ok ? { held: true, v: value.v } : { why: value.why, ...(value.kept !== undefined ? { kept: value.kept } : {}) };
|
|
344
|
+
}
|
|
345
|
+
function behaviorJudged(r) {
|
|
346
|
+
const read = ("row" in r ? r.row : r.read);
|
|
347
|
+
if (!Object.hasOwn(read, "behavior") || isRuleBehavior(read.behavior))
|
|
348
|
+
return r;
|
|
349
|
+
const { behavior, ...rest } = read;
|
|
350
|
+
const fault = { member: "behavior", why: `a word outside deny/ask/allow (${JSON.stringify(escapeForDisclosure(behavior))})` };
|
|
351
|
+
return { failed: [fault, ...("failed" in r ? r.failed : [])], read: rest };
|
|
352
|
+
}
|
|
353
|
+
function commandIsIdentity(match) {
|
|
354
|
+
return match !== "tool";
|
|
355
|
+
}
|
|
356
|
+
const UNNAMED_ADD = Object.freeze({ dot: Object.freeze({ actor: "", counter: 0 }), origin: "user", createdAt: "" });
|
|
357
|
+
function identityOf(read, failed) {
|
|
358
|
+
if (!failed.has("tool") && !failed.has("match") && !(failed.has("command") && commandIsIdentity(read.match)))
|
|
359
|
+
return {};
|
|
360
|
+
const parsed = failed.has("rule") || typeof read.rule !== "string" ? undefined : parseRuleText(read.rule, read.behavior);
|
|
361
|
+
return parsed === undefined || "reject" in parsed ? undefined : { tool: parsed.rule.tool, match: parsed.rule.match, command: parsed.rule.command };
|
|
362
|
+
}
|
|
363
|
+
function standingRowOf(read, failed, seat) {
|
|
364
|
+
const adds = failed.find((f) => f.member === "adds");
|
|
365
|
+
const stood = {
|
|
366
|
+
...read,
|
|
367
|
+
...(failed.some((f) => f.member === "scope") ? { scope: widestScopeOf(seat) } : {}),
|
|
368
|
+
...(read.match === "tool" && typeof read.command !== "string" ? { command: "" } : {}),
|
|
369
|
+
...(adds !== undefined ? { adds: adds.kept !== undefined && adds.kept.length > 0 ? [...adds.kept] : [UNNAMED_ADD] } : {}),
|
|
370
|
+
};
|
|
371
|
+
return failed.some((f) => f.member === "rule") ? { ...stood, rule: storedRuleTextOf(stood) } : stood;
|
|
372
|
+
}
|
|
373
|
+
function unreadableRowOf(read, failed, first, seat) {
|
|
374
|
+
const behavior = !failed.has("behavior") && (read.behavior === "deny" || read.behavior === "ask") ? read.behavior : "ask";
|
|
375
|
+
const tool = failed.has("tool") ? undefined : read.tool;
|
|
376
|
+
const pathDeny = (behavior === "deny" || failed.has("behavior")) && typeof tool === "string" && Object.hasOwn(PATH_RULE_FACES, tool) && PATH_RULE_FACES[tool]?.deny !== undefined;
|
|
377
|
+
return {
|
|
378
|
+
reason: failed.has("rule") ? "invalid_rule_text" : "metadata_mismatch",
|
|
379
|
+
rule: failed.has("rule") ? storedRuleTextOf(read) : String(read.rule),
|
|
380
|
+
behavior,
|
|
381
|
+
member: first,
|
|
382
|
+
reach: typeof tool === "string" && isNameableToolName(tool) && !pathDeny ? { tool } : "every",
|
|
383
|
+
...namedScopeOf(failed.has("scope") ? undefined : read.scope, seat),
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
export const DURABLE_SEAT = {};
|
|
387
|
+
function widestScopeOf(seat) {
|
|
388
|
+
return seat.session !== undefined ? { kind: "session", sessionId: seat.session } : { kind: "global" };
|
|
389
|
+
}
|
|
390
|
+
function namedScopeOf(scope, seat) {
|
|
391
|
+
if (seat.session === undefined)
|
|
392
|
+
return isValidDurableScope(scope) ? { scope: scope } : {};
|
|
393
|
+
if (scope === undefined)
|
|
394
|
+
return { scope: widestScopeOf(seat) };
|
|
395
|
+
const session = typeof scope === "object" && scope !== null && scope.kind === "session" && typeof scope.sessionId === "string";
|
|
396
|
+
return session ? { scope: scope } : {};
|
|
397
|
+
}
|
|
398
|
+
export function unreadableRowsLine(partition, rows) {
|
|
399
|
+
return (`the ${partition} rule partition served ${rows.length} deny or ask row(s) a member of which could not be read as the type it holds — one whose tool, match and command read, or whose text says the ones that did not, stands as written (its text, when that is the member, spelled from them; its scope, when that is the member, ${partition === "session" ? "the session's" : "everywhere"}); one whose neither do stands at its strictest reading over the calls it may reach (no allow clears them; a readable deny still refuses, a readable ask still asks); every other row stands: ` +
|
|
400
|
+
rows
|
|
401
|
+
.slice(0, 3)
|
|
402
|
+
.map(({ row, why }) => `${escapeForDisclosure(row.rule)} (${escapeForDisclosure(why)}; ${row.reach === "as_read" ? "as written" : row.reach === "every" ? "every call" : `the ${escapeForDisclosure(row.reach.tool)} calls`})`)
|
|
403
|
+
.join(", "));
|
|
404
|
+
}
|
|
405
|
+
export function ruleRowReadOf(served, seat) {
|
|
406
|
+
const r = behaviorJudged(servedRowOf(served, RULE_ROW_MEMBERS));
|
|
407
|
+
if ("row" in r)
|
|
408
|
+
return { row: r.row };
|
|
409
|
+
const failed = new Set(r.failed.map((f) => f.member));
|
|
410
|
+
const [first] = r.failed;
|
|
411
|
+
const why = `${first.member}: ${first.why}`;
|
|
412
|
+
const reason = failed.has("rule") ? "invalid_rule_text" : "metadata_mismatch";
|
|
413
|
+
const rule = failed.has("rule") ? storedRuleTextOf(r.read) : String(r.read.rule);
|
|
414
|
+
const scope = failed.has("scope") ? widestScopeOf(seat) : r.read.scope;
|
|
415
|
+
const dead = !failed.has("adds") && Array.isArray(r.read.adds) && r.read.adds.length === 0;
|
|
416
|
+
if (dead)
|
|
417
|
+
return { dead };
|
|
418
|
+
if (failed.has("behavior"))
|
|
419
|
+
return { named: { row: unreadableRowOf(r.read, failed, first.member, seat), why: r.notObject ?? `behavior: ${first.threw === true ? "a read that threw" : first.why}` }, scope };
|
|
420
|
+
if (r.read.behavior === "allow")
|
|
421
|
+
return { left: { reason, rule, member: first.member }, scope };
|
|
422
|
+
const identity = identityOf(r.read, failed);
|
|
423
|
+
if (identity === undefined)
|
|
424
|
+
return { named: { row: unreadableRowOf(r.read, failed, first.member, seat), why } };
|
|
425
|
+
const stood = standingRowOf({ ...r.read, ...identity }, r.failed, seat);
|
|
426
|
+
return { row: stood, named: { row: { reason, rule, behavior: r.read.behavior, member: first.member, reach: "as_read", ...namedScopeOf(failed.has("scope") ? undefined : r.read.scope, seat) }, why, stood } };
|
|
427
|
+
}
|
|
428
|
+
function scopeRootAsServed(root) {
|
|
429
|
+
return typeof root === "string" ? root : `(${root === null ? "null" : typeof root})`;
|
|
430
|
+
}
|
|
431
|
+
export function droppedRowOf(r) {
|
|
432
|
+
if (!isValidDurableScope(r.scope))
|
|
433
|
+
return { reason: "scope_root_not_absolute", rule: String(r.rule), scopeRoot: scopeRootAsServed(r.scope.root) };
|
|
434
|
+
if (r.behavior !== "allow")
|
|
435
|
+
return undefined;
|
|
436
|
+
const shape = normalizePersistedRule(r);
|
|
437
|
+
return "reject" in shape ? { reason: shape.reject, rule: r.rule } : undefined;
|
|
438
|
+
}
|
|
439
|
+
export function readDurableRuleRow(served) {
|
|
440
|
+
const r = ruleRowReadOf(served, DURABLE_SEAT);
|
|
441
|
+
if (r.dead === true)
|
|
442
|
+
return { kind: "inert", dead: true };
|
|
443
|
+
if (r.left !== undefined)
|
|
444
|
+
return { kind: "inert", dropped: r.left, unread: true };
|
|
445
|
+
if ("row" in r) {
|
|
446
|
+
const row = r.row;
|
|
447
|
+
const dropped = droppedRowOf(row);
|
|
448
|
+
if (dropped !== undefined)
|
|
449
|
+
return { kind: "inert", row, dropped };
|
|
450
|
+
if (!Array.isArray(row.adds) || row.adds.length === 0)
|
|
451
|
+
return { kind: "inert", row, dead: true };
|
|
452
|
+
return r.named !== undefined ? { kind: "acts", row, named: r.named } : { kind: "acts", row };
|
|
453
|
+
}
|
|
454
|
+
if (r.named !== undefined)
|
|
455
|
+
return { kind: "acts", named: r.named };
|
|
456
|
+
return { kind: "inert" };
|
|
457
|
+
}
|
|
458
|
+
function heldDotOf(add) {
|
|
459
|
+
if (add === null || typeof add !== "object")
|
|
460
|
+
return undefined;
|
|
461
|
+
const got = memberValueOf(add, "dot");
|
|
462
|
+
if (!got.ok)
|
|
463
|
+
return undefined;
|
|
464
|
+
const dot = servedValueOf(got.v, ADD_DOT_SHAPE);
|
|
465
|
+
return dot.ok ? dot.v : undefined;
|
|
466
|
+
}
|
|
467
|
+
function heldRowLive(row) {
|
|
468
|
+
return readDurableRuleRow(row).kind === "acts";
|
|
112
469
|
}
|
|
113
470
|
export function applyTombstones(rules, tombstones) {
|
|
114
471
|
return rules
|
|
115
472
|
.map((r) => {
|
|
473
|
+
const adds = heldAddsOf(r);
|
|
474
|
+
if (adds === undefined)
|
|
475
|
+
return r;
|
|
116
476
|
const removed = tombstones.filter((t) => sameRuleIdentity(t, r)).flatMap((t) => t.removedDots);
|
|
117
|
-
|
|
477
|
+
if (removed.length === 0)
|
|
478
|
+
return r;
|
|
479
|
+
return { ...r, adds: adds.filter((a) => { const dot = heldDotOf(a); return dot === undefined || !removed.some((d) => sameDot(d, dot)); }) };
|
|
118
480
|
})
|
|
119
|
-
.filter(
|
|
481
|
+
.filter((r) => heldAddsOf(r)?.length !== 0);
|
|
120
482
|
}
|
|
121
483
|
export function foldDelta(rules, delta) {
|
|
122
|
-
const next = rules.map((r) => ({ ...r, adds: [...r.adds] }));
|
|
123
|
-
const existing = next.find((r) => sameRuleIdentity(r, delta));
|
|
484
|
+
const next = rules.map((r) => (heldAddsOf(r) !== undefined ? { ...r, adds: [...r.adds] } : r));
|
|
485
|
+
const existing = next.find((r) => heldAddsOf(r) !== undefined && sameRuleIdentity(r, delta));
|
|
124
486
|
if (existing === undefined) {
|
|
125
487
|
next.push({ behavior: delta.behavior, rule: delta.rule, tool: delta.tool, match: delta.match, command: delta.command, scope: delta.scope, adds: [delta.add] });
|
|
126
488
|
return next;
|
|
127
489
|
}
|
|
128
|
-
if (!existing
|
|
490
|
+
if (!holdsDot(existing, delta.add.dot))
|
|
129
491
|
existing.adds.push(delta.add);
|
|
130
492
|
return next;
|
|
131
493
|
}
|
|
@@ -337,10 +699,24 @@ export function collectBelowFrontier(state, frontier) {
|
|
|
337
699
|
.filter((r) => r.adds.length > 0);
|
|
338
700
|
return { state: { rules, tombstones: keep }, collectedTombstones: collectable.length };
|
|
339
701
|
}
|
|
702
|
+
export function unreadableLocalRuleIndices(rules) {
|
|
703
|
+
const out = [];
|
|
704
|
+
for (let i = 0; i < rules.length; i++) {
|
|
705
|
+
const r = rules[i];
|
|
706
|
+
const adds = heldAddsOf(r);
|
|
707
|
+
if (storedExactIdentityOf(r) === undefined || adds === undefined || adds.some((a) => { const d = heldDotOf(a); return d === undefined || !isValidDot(d); }))
|
|
708
|
+
out.push(i);
|
|
709
|
+
}
|
|
710
|
+
return out;
|
|
711
|
+
}
|
|
340
712
|
export function applySyncJoin(cur, delta, nowMs) {
|
|
341
713
|
if (delta.kind !== "sync-join" || delta.inbound === null || typeof delta.inbound !== "object" || !Array.isArray(delta.inbound.rules) || !Array.isArray(delta.inbound.tombstones)) {
|
|
342
714
|
throw new Error("a sync-join delta must carry an inbound state with rules and tombstones arrays");
|
|
343
715
|
}
|
|
716
|
+
const unreadableLocal = unreadableLocalRuleIndices(cur.rules);
|
|
717
|
+
if (unreadableLocal.length > 0) {
|
|
718
|
+
throw new Error(`local rule row (#${unreadableLocal[0]}) cannot be read as a stored rule row — refusing the sync-join rather than writing it away; repair or remove it in the store`);
|
|
719
|
+
}
|
|
344
720
|
for (const t of delta.inbound.tombstones) {
|
|
345
721
|
if (t?.scope?.kind === "session")
|
|
346
722
|
continue;
|
|
@@ -525,13 +901,13 @@ function resolveRuleWriter(store) {
|
|
|
525
901
|
return w === undefined ? { unavailable: "the resolved permission-rule store has no write face", committed: "no" } : { writer: w };
|
|
526
902
|
}
|
|
527
903
|
function ruleIdentityStands(rules, identity) {
|
|
528
|
-
return rules.some((r) => sameRuleIdentity(r, identity) &&
|
|
904
|
+
return rules.some((r) => (sameRuleIdentity(r, identity) || sameDisplayedIdentity(r, identity)) && heldRowLive(r));
|
|
529
905
|
}
|
|
530
|
-
function
|
|
531
|
-
return
|
|
906
|
+
function holdsDot(row, dot) {
|
|
907
|
+
return heldAddsOf(row)?.some((a) => { const d = heldDotOf(a); return d !== undefined && sameDot(d, dot); }) === true;
|
|
532
908
|
}
|
|
533
909
|
function rowsStand(raw, identity) {
|
|
534
|
-
return ruleIdentityStands(
|
|
910
|
+
return ruleIdentityStands(applyTombstones(raw.rules, raw.tombstones), identity);
|
|
535
911
|
}
|
|
536
912
|
async function witnessRuleAdd(store, writer, delta) {
|
|
537
913
|
const identity = { behavior: delta.behavior, rule: delta.rule, scope: delta.scope };
|
|
@@ -541,10 +917,10 @@ async function witnessRuleAdd(store, writer, delta) {
|
|
|
541
917
|
const view = await store.effective({ sessionId: delta.scope.sessionId });
|
|
542
918
|
if (view.unreadable !== undefined)
|
|
543
919
|
return { unreadable: view.unreadable };
|
|
544
|
-
return { seen: view.rules.some((r) => sameRuleIdentity(r, identity) && r
|
|
920
|
+
return { seen: view.rules.some((r) => sameRuleIdentity(r, identity) && holdsDot(r, dot)), rev: view.rev };
|
|
545
921
|
}
|
|
546
922
|
const raw = await writer.readRaw();
|
|
547
|
-
const inRows = raw.rules.some((r) => sameRuleIdentity(r, identity) && r
|
|
923
|
+
const inRows = raw.rules.some((r) => sameRuleIdentity(r, identity) && holdsDot(r, dot));
|
|
548
924
|
const inQuarantine = (raw.quarantined ?? []).some((q) => sameDot(q.add.dot, dot));
|
|
549
925
|
return { seen: inRows || inQuarantine, rev: raw.rev };
|
|
550
926
|
}
|
|
@@ -613,10 +989,37 @@ export async function removePersistedRule(opts) {
|
|
|
613
989
|
}
|
|
614
990
|
const writer = face.writer;
|
|
615
991
|
let snapshot;
|
|
616
|
-
let removedDots;
|
|
992
|
+
let removedDots = [];
|
|
617
993
|
try {
|
|
618
994
|
snapshot = await writer.readRaw();
|
|
619
|
-
|
|
995
|
+
let unretireableInertError;
|
|
996
|
+
for (const r of applyTombstones(snapshot.rules, snapshot.tombstones)) {
|
|
997
|
+
const exact = sameRuleIdentity(r, identity);
|
|
998
|
+
if (!exact && !sameDisplayedIdentity(r, identity))
|
|
999
|
+
continue;
|
|
1000
|
+
const inert = readDurableRuleRow(r).kind === "inert";
|
|
1001
|
+
if (!exact) {
|
|
1002
|
+
const msg = `removePersistedRule: a stored row displayed as ${behavior} ${escapeForDisclosure(rule)} is not stored whole, so no tombstone can name it — nothing was written; remove it in the store`;
|
|
1003
|
+
if (inert) {
|
|
1004
|
+
unretireableInertError ??= msg;
|
|
1005
|
+
continue;
|
|
1006
|
+
}
|
|
1007
|
+
return { status: "failed", error: msg, committed: "no" };
|
|
1008
|
+
}
|
|
1009
|
+
const dots = heldAddsOf(r)?.map(heldDotOf);
|
|
1010
|
+
if (dots === undefined || dots.some((d) => d === undefined)) {
|
|
1011
|
+
const msg = `removePersistedRule: a stored ${behavior} row named ${escapeForDisclosure(rule)} holds a list of adds this engine cannot read, so no tombstone can name them — nothing was written; remove it in the store`;
|
|
1012
|
+
if (inert) {
|
|
1013
|
+
unretireableInertError ??= msg;
|
|
1014
|
+
continue;
|
|
1015
|
+
}
|
|
1016
|
+
return { status: "failed", error: msg, committed: "no" };
|
|
1017
|
+
}
|
|
1018
|
+
removedDots.push(...dots);
|
|
1019
|
+
}
|
|
1020
|
+
if (removedDots.length === 0 && unretireableInertError !== undefined) {
|
|
1021
|
+
return { status: "failed", error: unretireableInertError, committed: "no" };
|
|
1022
|
+
}
|
|
620
1023
|
}
|
|
621
1024
|
catch (err) {
|
|
622
1025
|
return { status: "failed", error: `could not read the permission-rule store: ${errText(err)}`, committed: "no" };
|
|
@@ -829,7 +1232,7 @@ export async function addPersistedRule(opts) {
|
|
|
829
1232
|
({ rev, rules: rows } = raw);
|
|
830
1233
|
quarantinedRows = raw.quarantined ?? [];
|
|
831
1234
|
}
|
|
832
|
-
const stood = rows.some((r) => sameRuleIdentity(r, identity) && r
|
|
1235
|
+
const stood = rows.some((r) => sameRuleIdentity(r, identity) && holdsDot(r, dot)) ||
|
|
833
1236
|
quarantinedRows.some((q) => sameDot(q.add.dot, dot));
|
|
834
1237
|
return stood
|
|
835
1238
|
? { status: "added", rev, dot }
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { escapeForDisclosure, isAddStamp, isRuleBehavior, parseRuleText, RULE_ADD_ORIGINS } from "./permission-rule-model.js";
|
|
2
2
|
import { principalNameOf } from "./principal-name.js";
|
|
3
|
-
import { PERMISSION_RULES_LATER_GRAMMAR_CODE, applyTombstones, errText, isProjectScopeRoot, joinFrontiers, joinRuleStates, normalizePersistedRule, readRuleWriteOutcome, ruleSyncVector, sameRuleIdentity, screenRuleSyncState, writerOf, } from "./permission-rule-store.js";
|
|
3
|
+
import { PERMISSION_RULES_LATER_GRAMMAR_CODE, applyTombstones, errText, isProjectScopeRoot, joinFrontiers, joinRuleStates, normalizePersistedRule, readRuleWriteOutcome, ruleSyncVector, sameRuleIdentity, screenRuleSyncState, unreadableLocalRuleIndices, writerOf, } from "./permission-rule-store.js";
|
|
4
4
|
import { emitTrace } from "./trace.js";
|
|
5
5
|
import { SESSION_RULES_GRAMMAR, readRuleGrammarStamp } from "./session-policy-store.js";
|
|
6
6
|
export const PERMISSION_RULE_SYNC_PATH = "/v1/permission-rules/sync";
|
|
@@ -61,6 +61,20 @@ export async function syncPermissionRules(opts) {
|
|
|
61
61
|
throw new Error("the resolved permission-rule store has no write face — a sync round cannot land through it");
|
|
62
62
|
}
|
|
63
63
|
const raw = await writer.readRaw();
|
|
64
|
+
const unreadableLocal = unreadableLocalRuleIndices(raw.rules);
|
|
65
|
+
if (unreadableLocal.length > 0) {
|
|
66
|
+
return disclose({
|
|
67
|
+
ok: false,
|
|
68
|
+
pushed: { addDots: 0, tombstones: 0 },
|
|
69
|
+
landed: { newAdds: 0, newTombstones: 0 },
|
|
70
|
+
resurrected: [],
|
|
71
|
+
dropped: [],
|
|
72
|
+
rev: raw.rev,
|
|
73
|
+
warnings: [
|
|
74
|
+
`held: ${unreadableLocal.length} unreadable row(s), nothing sent — local rule row(s) (${unreadableLocal.map((i) => `#${i}`).join(", ")}) cannot be read as stored rule rows; repair or remove them in the store`,
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
}
|
|
64
78
|
const request = {
|
|
65
79
|
principal,
|
|
66
80
|
replica: raw.actor,
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** A project id's one format: a UUID (the version nibble not narrowed — read wide, minted strict). The marker file, a declared seat
|
|
2
|
+
* and a v2 memory write key are all judged against it. */
|
|
3
|
+
export declare const PROJECT_ID_REGEX: RegExp;
|
|
4
|
+
/**
|
|
5
|
+
* A leg's project. `id` is the leg's DECLARATION of its project (judged once per leg against its checkout's marker); the four text
|
|
6
|
+
* members are written by people and vouched for by the host, never by a program (a model's write-back is data, never this seat). Each
|
|
7
|
+
* text member is present only when it says something — "none" is the member left out.
|
|
8
|
+
*/
|
|
9
|
+
export interface ProjectSeat {
|
|
10
|
+
/** The project's UUID (the id a `.sema/project` marker carries). */
|
|
11
|
+
readonly id: string;
|
|
12
|
+
/** A one-line label: no line break, at most {@link PROJECT_SEAT_NAME_MAX} characters. */
|
|
13
|
+
readonly name?: string;
|
|
14
|
+
/** What the project is. */
|
|
15
|
+
readonly description?: string;
|
|
16
|
+
/** The project's standing instructions. */
|
|
17
|
+
readonly instructions?: string;
|
|
18
|
+
/** Host paths the project names (folders or files), absolute, at most {@link PROJECT_SEAT_FILES_MAX}: names only — the engine reads
|
|
19
|
+
* none of them and binds none. */
|
|
20
|
+
readonly files?: readonly string[];
|
|
21
|
+
}
|
|
22
|
+
/** The longest `name`, in characters. */
|
|
23
|
+
export declare const PROJECT_SEAT_NAME_MAX = 256;
|
|
24
|
+
/** The most `files` entries. */
|
|
25
|
+
export declare const PROJECT_SEAT_FILES_MAX = 100;
|
|
26
|
+
/** The screen's answer: the seat as the engine reads it (a frozen copy; absent ⇔ none was given), or the refusal — its code, the field
|
|
27
|
+
* path it is about, and the sentence (opening with that path). Never thrown: a caller decides what a refusal does. */
|
|
28
|
+
export type ProjectSeatScreen = {
|
|
29
|
+
readonly ok: true;
|
|
30
|
+
readonly seat?: ProjectSeat;
|
|
31
|
+
} | {
|
|
32
|
+
readonly ok: false;
|
|
33
|
+
readonly code: "config.invalid_argument" | "config.project_id_invalid";
|
|
34
|
+
readonly field: string;
|
|
35
|
+
readonly message: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Judge a project seat. `undefined` is "no seat". Refused, each with `config.invalid_argument` and its field path, unless noted: a value
|
|
39
|
+
* that is no plain object (`null`, a primitive, an array, a class instance); a member that is no seat member; a
|
|
40
|
+
* member that is not an own data property; a missing `id`; an `id` that is not a UUID (`config.project_id_invalid`); a text member that
|
|
41
|
+
* is no string, or is empty or only whitespace; a `name` with a line break or over {@link PROJECT_SEAT_NAME_MAX} characters; `files`
|
|
42
|
+
* that is no array, over {@link PROJECT_SEAT_FILES_MAX} entries, or holds an entry that is not an absolute host path; and a value whose read
|
|
43
|
+
* throws (an index getter, a Proxy trap) — answered, never thrown. A plain object is judged by its shape, not by which realm made it: its
|
|
44
|
+
* prototype is `null` or a root object (another VM context's `Object.prototype` included). Each member is read once; the answer's seat is
|
|
45
|
+
* a copy of what was read, so the caller's object is never read again. `at` is the field path of the seat for the sentences (`TaskSpec.project`).
|
|
46
|
+
*/
|
|
47
|
+
export declare function screenProjectSeat(value: unknown, at?: string): ProjectSeatScreen;
|
|
48
|
+
/**
|
|
49
|
+
* Capture a seat at the moment it is handed in (a leg's internals, at the call's door): the screened copy, or — when the screen refuses —
|
|
50
|
+
* a frozen stand-in that carries the refusal, so the leg gives it as its failed terminal ({@link handedProjectSeat}) and the caller keeps
|
|
51
|
+
* its stream and result. Either way the caller's object is never read again.
|
|
52
|
+
*/
|
|
53
|
+
export declare function captureProjectSeat(value: unknown, at: string): ProjectSeat | undefined;
|
|
54
|
+
/** A captured seat as the leg reads it: the seat, or the refusal its capture recorded ({@link captureProjectSeat}). */
|
|
55
|
+
export declare function handedProjectSeat(captured: ProjectSeat | undefined): ProjectSeatScreen;
|