pi-better-server 0.4.0 → 0.6.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/package.json +8 -1
- package/scripts/copy-pi-test-providers.mjs +10 -0
- package/scripts/event-publication/README.md +26 -0
- package/scripts/event-publication/commit.mjs +21 -0
- package/scripts/event-publication/measure.mjs +87 -0
- package/scripts/event-publication/writer.mjs +6 -0
- package/scripts/measure-full-runtime/frontend.ts +24 -0
- package/scripts/measure-full-runtime/hosted-extension.ts +28 -0
- package/scripts/measure-full-runtime/probe.mjs +29 -0
- package/scripts/measure-full-runtime/recovery-trace-loader.mjs +51 -0
- package/scripts/measure-full-runtime/recovery-trace.mjs +16 -0
- package/scripts/measure-full-runtime.mjs +146 -0
- package/scripts/measure-shared-reads/probe.mjs +26 -2
- package/scripts/measure-shared-reads.mjs +44 -9
- package/scripts/private-runtime/package-lock.json +66 -4
- package/scripts/private-runtime/package.json +10 -2
- package/scripts/source-provenance.mjs +31 -0
- package/scripts/test-on-pi.sh +3 -9
- package/src/handshake.ts +1 -1
- package/src/hosted-session.ts +32 -4
- package/src/maintenance.ts +35 -7
- package/src/registry.ts +28 -4
- package/src/runtime/attempt-invalidation.ts +54 -0
- package/src/runtime/background-history.ts +151 -45
- package/src/runtime/background-policy.ts +27 -0
- package/src/runtime/background-tools.ts +389 -0
- package/src/runtime/background.ts +410 -48
- package/src/runtime/cancellation.ts +28 -7
- package/src/runtime/catalog-sources.ts +199 -0
- package/src/runtime/catalog.ts +53 -21
- package/src/runtime/client.ts +22 -2
- package/src/runtime/domain/assignment-inbox.mjs +119 -0
- package/src/runtime/domain/assignment-observation.d.mts +1 -1
- package/src/runtime/domain/assignment-observation.mjs +71 -33
- package/src/runtime/domain/background/process-identity.mjs +20 -2
- package/src/runtime/domain/background/process.mjs +3 -3
- package/src/runtime/domain/background/registry.mjs +89 -4
- package/src/runtime/domain/background/types.mjs +1 -0
- package/src/runtime/domain/background-service/adapter-contract.domain.mjs +83 -0
- package/src/runtime/domain/background-service/adapter-contract.ts +70 -0
- package/src/runtime/domain/background-service/batch.domain.mjs +91 -0
- package/src/runtime/domain/background-service/batch.ts +111 -0
- package/src/runtime/domain/background-service/conditions.domain.mjs +94 -0
- package/src/runtime/domain/background-service/conditions.ts +99 -0
- package/src/runtime/domain/background-service/delivery-observer.domain.mjs +37 -0
- package/src/runtime/domain/background-service/delivery-observer.ts +42 -0
- package/src/runtime/domain/background-service/failures.domain.mjs +209 -0
- package/src/runtime/domain/background-service/failures.ts +212 -0
- package/src/runtime/domain/background-service/goal-provider.domain.mjs +193 -0
- package/src/runtime/domain/background-service/goal-provider.ts +209 -0
- package/src/runtime/domain/background-service/index.domain.mjs +54 -0
- package/src/runtime/domain/background-service/index.ts +55 -0
- package/src/runtime/domain/background-service/logs.domain.mjs +129 -0
- package/src/runtime/domain/background-service/logs.ts +134 -0
- package/src/runtime/domain/background-service/maintenance.domain.mjs +95 -0
- package/src/runtime/domain/background-service/maintenance.ts +98 -0
- package/src/runtime/domain/background-service/managed-authority.domain.mjs +32 -0
- package/src/runtime/domain/background-service/managed-authority.ts +30 -0
- package/src/runtime/domain/background-service/managed-client.domain.mjs +79 -0
- package/src/runtime/domain/background-service/managed-client.ts +73 -0
- package/src/runtime/domain/background-service/managed-frontend.domain.mjs +225 -0
- package/src/runtime/domain/background-service/managed-frontend.ts +176 -0
- package/src/runtime/domain/background-service/navigator-provider.domain.mjs +492 -0
- package/src/runtime/domain/background-service/navigator-provider.ts +485 -0
- package/src/runtime/domain/background-service/observed-wait-client.domain.mjs +6 -0
- package/src/runtime/domain/background-service/observed-wait-client.ts +10 -0
- package/src/runtime/domain/background-service/observed-waits.domain.mjs +4080 -0
- package/src/runtime/domain/background-service/observed-waits.ts +4513 -0
- package/src/runtime/domain/background-service/output.domain.mjs +867 -0
- package/src/runtime/domain/background-service/output.ts +952 -0
- package/src/runtime/domain/background-service/process-identity.domain.mjs +129 -0
- package/src/runtime/domain/background-service/process-identity.ts +134 -0
- package/src/runtime/domain/background-service/process.domain.mjs +512 -0
- package/src/runtime/domain/background-service/process.ts +532 -0
- package/src/runtime/domain/background-service/prompt.domain.mjs +58 -0
- package/src/runtime/domain/background-service/prompt.ts +72 -0
- package/src/runtime/domain/background-service/registry.domain.mjs +460 -0
- package/src/runtime/domain/background-service/registry.ts +450 -0
- package/src/runtime/domain/background-service/remote-task-preset.domain.mjs +67 -0
- package/src/runtime/domain/background-service/remote-task-preset.ts +127 -0
- package/src/runtime/domain/background-service/runtime.domain.mjs +1511 -0
- package/src/runtime/domain/background-service/runtime.ts +1636 -0
- package/src/runtime/domain/background-service/sandbox.domain.mjs +735 -0
- package/src/runtime/domain/background-service/sandbox.ts +913 -0
- package/src/runtime/domain/background-service/shared-callback-batcher.domain.mjs +777 -0
- package/src/runtime/domain/background-service/shared-callback-batcher.ts +956 -0
- package/src/runtime/domain/background-service/shared-execution-policy.domain.mjs +50 -0
- package/src/runtime/domain/background-service/shared-execution-policy.ts +61 -0
- package/src/runtime/domain/background-service/shared-failure-observations.domain.mjs +1078 -0
- package/src/runtime/domain/background-service/shared-failure-observations.ts +1166 -0
- package/src/runtime/domain/background-service/shared-harness-settings.domain.mjs +126 -0
- package/src/runtime/domain/background-service/shared-harness-settings.ts +116 -0
- package/src/runtime/domain/background-service/shared-log-utils.domain.mjs +1223 -0
- package/src/runtime/domain/background-service/shared-log-utils.ts +1492 -0
- package/src/runtime/domain/background-service/shared-navigator-panels.domain.mjs +289 -0
- package/src/runtime/domain/background-service/shared-navigator-panels.ts +272 -0
- package/src/runtime/domain/background-service/shared-navigator.domain.mjs +1930 -0
- package/src/runtime/domain/background-service/shared-navigator.ts +1962 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/assignment-inbox.mjs +144 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/assignment-observation.mjs +189 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/index.domain.mjs +51 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/index.ts +122 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-changes.domain.mjs +75 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-changes.ts +73 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-outcomes.domain.mjs +163 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-outcomes.ts +174 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-parent-callbacks.domain.mjs +66 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-parent-callbacks.ts +68 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.domain.mjs +87 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.ts +90 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.domain.mjs +97 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.ts +139 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/errors.domain.mjs +12 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/errors.ts +79 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.domain.mjs +342 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.ts +513 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.domain.mjs +283 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.ts +458 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/recovery.domain.mjs +52 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/recovery.ts +55 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.domain.mjs +114 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.ts +140 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.domain.mjs +280 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.ts +442 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.domain.mjs +420 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.ts +441 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/sqlite.domain.mjs +194 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/sqlite.ts +239 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.domain.mjs +275 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.ts +585 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.domain.mjs +120 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.ts +142 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/task-cancellation.domain.mjs +188 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/task-cancellation.ts +224 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.domain.mjs +25 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.ts +707 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/upgrade.domain.mjs +22 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/upgrade.ts +22 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/validate.domain.mjs +234 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/validate.ts +241 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/waits.domain.mjs +851 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/waits.ts +1424 -0
- package/src/runtime/domain/background-service/shared-permission-blocker.domain.mjs +40 -0
- package/src/runtime/domain/background-service/shared-permission-blocker.ts +48 -0
- package/src/runtime/domain/background-service/shared-render-scheduler.domain.mjs +41 -0
- package/src/runtime/domain/background-service/shared-render-scheduler.ts +47 -0
- package/src/runtime/domain/background-service/shared-sandbox-core.domain.mjs +1531 -0
- package/src/runtime/domain/background-service/shared-sandbox-core.ts +1757 -0
- package/src/runtime/domain/background-service/shared-sandbox-diagnostics.domain.mjs +591 -0
- package/src/runtime/domain/background-service/shared-sandbox-diagnostics.ts +491 -0
- package/src/runtime/domain/background-service/shared-ssh-core/index.domain.mjs +698 -0
- package/src/runtime/domain/background-service/shared-ssh-core/index.ts +979 -0
- package/src/runtime/domain/background-service/shared-stall-detector.domain.mjs +37 -0
- package/src/runtime/domain/background-service/shared-stall-detector.ts +69 -0
- package/src/runtime/domain/background-service/shared-work-client.mjs +303 -0
- package/src/runtime/domain/background-service/stall-attention.domain.mjs +29 -0
- package/src/runtime/domain/background-service/stall-attention.ts +31 -0
- package/src/runtime/domain/background-service/stall.domain.mjs +57 -0
- package/src/runtime/domain/background-service/stall.ts +57 -0
- package/src/runtime/domain/background-service/supervisor/README.md +111 -0
- package/src/runtime/domain/background-service/supervisor/emit-runner.mjs +90 -0
- package/src/runtime/domain/background-service/supervisor/generated/conditions.mjs +96 -0
- package/src/runtime/domain/background-service/supervisor/generated/identity.mjs +95 -0
- package/src/runtime/domain/background-service/supervisor/generated/process-identity.mjs +131 -0
- package/src/runtime/domain/background-service/supervisor/generated/process.mjs +514 -0
- package/src/runtime/domain/background-service/supervisor/generated/runner.mjs +872 -0
- package/src/runtime/domain/background-service/supervisor/generated/store.mjs +609 -0
- package/src/runtime/domain/background-service/supervisor/generated/types.mjs +50 -0
- package/src/runtime/domain/background-service/supervisor/identity.domain.mjs +93 -0
- package/src/runtime/domain/background-service/supervisor/identity.ts +99 -0
- package/src/runtime/domain/background-service/supervisor/index.domain.mjs +22 -0
- package/src/runtime/domain/background-service/supervisor/index.ts +37 -0
- package/src/runtime/domain/background-service/supervisor/invocation.domain.mjs +30 -0
- package/src/runtime/domain/background-service/supervisor/invocation.ts +44 -0
- package/src/runtime/domain/background-service/supervisor/runner.domain.mjs +870 -0
- package/src/runtime/domain/background-service/supervisor/runner.ts +982 -0
- package/src/runtime/domain/background-service/supervisor/store.domain.mjs +605 -0
- package/src/runtime/domain/background-service/supervisor/store.ts +651 -0
- package/src/runtime/domain/background-service/supervisor/supervisor.domain.mjs +721 -0
- package/src/runtime/domain/background-service/supervisor/supervisor.ts +804 -0
- package/src/runtime/domain/background-service/supervisor/types.domain.mjs +48 -0
- package/src/runtime/domain/background-service/supervisor/types.ts +394 -0
- package/src/runtime/domain/background-service/task-diagnostics.domain.mjs +60 -0
- package/src/runtime/domain/background-service/task-diagnostics.ts +71 -0
- package/src/runtime/domain/background-service/tools.domain.mjs +1050 -0
- package/src/runtime/domain/background-service/tools.ts +1142 -0
- package/src/runtime/domain/background-service/types.domain.mjs +5 -0
- package/src/runtime/domain/background-service/types.ts +236 -0
- package/src/runtime/domain/background-service/work-observation.domain.mjs +176 -0
- package/src/runtime/domain/background-service/work-observation.ts +144 -0
- package/src/runtime/domain/catalog/catalog-store.d.mts +9 -1
- package/src/runtime/domain/catalog/catalog-store.mjs +20 -22
- package/src/runtime/domain/coordinator/src/assignment-changes.mjs +75 -0
- package/src/runtime/domain/coordinator/src/assignment-outcomes.mjs +2 -0
- package/src/runtime/domain/coordinator/src/background-parent-callbacks.mjs +65 -0
- package/src/runtime/domain/coordinator/src/background-requests.mjs +86 -0
- package/src/runtime/domain/coordinator/src/discovery.mjs +4 -2
- package/src/runtime/domain/coordinator/src/operations.mjs +44 -4
- package/src/runtime/domain/coordinator/src/result-supervision.mjs +12 -0
- package/src/runtime/domain/coordinator/src/rows.mjs +2 -0
- package/src/runtime/domain/coordinator/src/schema.mjs +73 -21
- package/src/runtime/domain/coordinator/src/store.mjs +32 -3
- package/src/runtime/domain/coordinator/src/subagent-launch.mjs +119 -0
- package/src/runtime/domain/coordinator/src/types.mjs +1 -1
- package/src/runtime/domain/coordinator/src/upgrade.mjs +22 -0
- package/src/runtime/domain/health-observation.d.mts +3 -1
- package/src/runtime/domain/health-observation.mjs +189 -190
- package/src/runtime/domain/log-cursor.d.mts +2 -0
- package/src/runtime/domain/log-cursor.mjs +85 -16
- package/src/runtime/domain/parse.d.mts +4 -1
- package/src/runtime/domain/parse.mjs +50 -5
- package/src/runtime/domain/sandbox-policy/README.md +633 -0
- package/src/runtime/domain/sandbox-policy/background-admission.domain.mjs +83 -0
- package/src/runtime/domain/sandbox-policy/background-admission.ts +75 -0
- package/src/runtime/domain/sandbox-policy/commands.domain.mjs +245 -0
- package/src/runtime/domain/sandbox-policy/commands.ts +336 -0
- package/src/runtime/domain/sandbox-policy/deny-rules.domain.mjs +440 -0
- package/src/runtime/domain/sandbox-policy/deny-rules.ts +620 -0
- package/src/runtime/domain/sandbox-policy/diagnostics-command.domain.mjs +30 -0
- package/src/runtime/domain/sandbox-policy/diagnostics-command.ts +27 -0
- package/src/runtime/domain/sandbox-policy/events.domain.mjs +49 -0
- package/src/runtime/domain/sandbox-policy/events.ts +85 -0
- package/src/runtime/domain/sandbox-policy/files.domain.mjs +153 -0
- package/src/runtime/domain/sandbox-policy/files.ts +224 -0
- package/src/runtime/domain/sandbox-policy/index.domain.mjs +289 -0
- package/src/runtime/domain/sandbox-policy/index.ts +386 -0
- package/src/runtime/domain/sandbox-policy/package.json +66 -0
- package/src/runtime/domain/sandbox-policy/permission-settings.domain.mjs +36 -0
- package/src/runtime/domain/sandbox-policy/permission-settings.ts +46 -0
- package/src/runtime/domain/sandbox-policy/permissions-page.domain.mjs +309 -0
- package/src/runtime/domain/sandbox-policy/permissions-page.ts +295 -0
- package/src/runtime/domain/sandbox-policy/permissions.domain.mjs +98 -0
- package/src/runtime/domain/sandbox-policy/permissions.ts +125 -0
- package/src/runtime/domain/sandbox-policy/policy.domain.mjs +80 -0
- package/src/runtime/domain/sandbox-policy/policy.ts +102 -0
- package/src/runtime/domain/sandbox-policy/preferences.domain.mjs +55 -0
- package/src/runtime/domain/sandbox-policy/preferences.ts +82 -0
- package/src/runtime/domain/sandbox-policy/rules-page.domain.mjs +138 -0
- package/src/runtime/domain/sandbox-policy/rules-page.ts +176 -0
- package/src/runtime/domain/sandbox-policy/session-permissions.domain.mjs +22 -0
- package/src/runtime/domain/sandbox-policy/session-permissions.ts +24 -0
- package/src/runtime/domain/sandbox-policy/shared-harness-settings.domain.mjs +126 -0
- package/src/runtime/domain/sandbox-policy/shared-harness-settings.ts +116 -0
- package/src/runtime/domain/sandbox-policy/shared-permission-blocker.domain.mjs +40 -0
- package/src/runtime/domain/sandbox-policy/shared-permission-blocker.ts +48 -0
- package/src/runtime/domain/sandbox-policy/shared-sandbox-core.domain.mjs +1531 -0
- package/src/runtime/domain/sandbox-policy/shared-sandbox-core.ts +1757 -0
- package/src/runtime/domain/sandbox-policy/shared-sandbox-diagnostics.domain.mjs +591 -0
- package/src/runtime/domain/sandbox-policy/shared-sandbox-diagnostics.ts +491 -0
- package/src/runtime/domain/sandbox-policy/shared-task-application.domain.mjs +185 -0
- package/src/runtime/domain/sandbox-policy/shared-task-application.ts +185 -0
- package/src/runtime/domain/sandbox-policy/shared-task-apply-patch.domain.mjs +416 -0
- package/src/runtime/domain/sandbox-policy/shared-task-apply-patch.ts +409 -0
- package/src/runtime/domain/sandbox-policy/shared-task-exec.domain.mjs +325 -0
- package/src/runtime/domain/sandbox-policy/shared-task-exec.ts +335 -0
- package/src/runtime/domain/sandbox-policy/shared-task-files.domain.mjs +346 -0
- package/src/runtime/domain/sandbox-policy/shared-task-files.ts +342 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-identity.domain.mjs +140 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-identity.ts +126 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-list.domain.mjs +220 -0
- package/src/runtime/domain/sandbox-policy/shared-task-process-list.ts +209 -0
- package/src/runtime/domain/sandbox-policy/shared-task-sandbox.domain.mjs +227 -0
- package/src/runtime/domain/sandbox-policy/shared-task-sandbox.ts +224 -0
- package/src/runtime/domain/sandbox-policy/shared-task-tools.domain.mjs +92 -0
- package/src/runtime/domain/sandbox-policy/shared-task-tools.ts +123 -0
- package/src/runtime/domain/sandbox-policy/shell.domain.mjs +93 -0
- package/src/runtime/domain/sandbox-policy/shell.ts +129 -0
- package/src/runtime/domain/sandbox-policy/state.domain.mjs +307 -0
- package/src/runtime/domain/sandbox-policy/state.ts +408 -0
- package/src/runtime/domain/sandbox-policy/status.domain.mjs +64 -0
- package/src/runtime/domain/sandbox-policy/status.ts +76 -0
- package/src/runtime/domain/sandbox-policy/subagent-admission.domain.mjs +69 -0
- package/src/runtime/domain/sandbox-policy/subagent-admission.ts +60 -0
- package/src/runtime/domain/subagents/CONTEXT.md +202 -0
- package/src/runtime/domain/subagents/PRODUCT.md +49 -0
- package/src/runtime/domain/subagents/README.md +201 -0
- package/src/runtime/domain/subagents/agent-catalog.mjs +64 -0
- package/src/runtime/domain/subagents/agent-commands.domain.mjs +822 -0
- package/src/runtime/domain/subagents/agent-commands.ts +905 -0
- package/src/runtime/domain/subagents/agent-inspection.domain.mjs +398 -0
- package/src/runtime/domain/subagents/agent-inspection.ts +574 -0
- package/src/runtime/domain/subagents/agent-operations.domain.mjs +40 -0
- package/src/runtime/domain/subagents/agent-operations.ts +61 -0
- package/src/runtime/domain/subagents/agent-session-settings.domain.mjs +112 -0
- package/src/runtime/domain/subagents/agent-session-settings.ts +118 -0
- package/src/runtime/domain/subagents/agents-catalog-tool.domain.mjs +84 -0
- package/src/runtime/domain/subagents/agents-catalog-tool.ts +118 -0
- package/src/runtime/domain/subagents/assignment-outcomes.domain.mjs +276 -0
- package/src/runtime/domain/subagents/assignment-outcomes.ts +212 -0
- package/src/runtime/domain/subagents/assignment-projection.mjs +261 -0
- package/src/runtime/domain/subagents/assignment-report.domain.mjs +174 -0
- package/src/runtime/domain/subagents/assignment-report.ts +156 -0
- package/src/runtime/domain/subagents/batch.mjs +275 -0
- package/src/runtime/domain/subagents/callback-fields.domain.mjs +72 -0
- package/src/runtime/domain/subagents/callback-fields.ts +95 -0
- package/src/runtime/domain/subagents/capacity.mjs +108 -0
- package/src/runtime/domain/subagents/catalog-editor.domain.mjs +98 -0
- package/src/runtime/domain/subagents/catalog-editor.ts +125 -0
- package/src/runtime/domain/subagents/catalog-host-admission.domain.mjs +104 -0
- package/src/runtime/domain/subagents/catalog-host-admission.ts +85 -0
- package/src/runtime/domain/subagents/catalog-identity.domain.mjs +222 -0
- package/src/runtime/domain/subagents/catalog-identity.ts +258 -0
- package/src/runtime/domain/subagents/catalog-overlay.domain.mjs +9 -0
- package/src/runtime/domain/subagents/catalog-overlay.ts +10 -0
- package/src/runtime/domain/subagents/catalog-preferences.domain.mjs +164 -0
- package/src/runtime/domain/subagents/catalog-preferences.ts +139 -0
- package/src/runtime/domain/subagents/catalog-resolver.domain.mjs +366 -0
- package/src/runtime/domain/subagents/catalog-resolver.ts +540 -0
- package/src/runtime/domain/subagents/catalog-runtime.domain.mjs +570 -0
- package/src/runtime/domain/subagents/catalog-runtime.ts +680 -0
- package/src/runtime/domain/subagents/catalog-schema.domain.mjs +1331 -0
- package/src/runtime/domain/subagents/catalog-schema.ts +1563 -0
- package/src/runtime/domain/subagents/catalog-skill-edits.domain.mjs +95 -0
- package/src/runtime/domain/subagents/catalog-skill-edits.ts +116 -0
- package/src/runtime/domain/subagents/catalog-store.domain.mjs +808 -0
- package/src/runtime/domain/subagents/catalog-store.ts +1006 -0
- package/src/runtime/domain/subagents/catalog-ui.domain.mjs +651 -0
- package/src/runtime/domain/subagents/catalog-ui.ts +611 -0
- package/src/runtime/domain/subagents/child-apply-patch.domain.mjs +13 -0
- package/src/runtime/domain/subagents/child-apply-patch.ts +13 -0
- package/src/runtime/domain/subagents/child-codemode.domain.mjs +16 -0
- package/src/runtime/domain/subagents/child-codemode.ts +17 -0
- package/src/runtime/domain/subagents/child-dependencies.mjs +193 -0
- package/src/runtime/domain/subagents/child-incidents.domain.mjs +170 -0
- package/src/runtime/domain/subagents/child-incidents.ts +169 -0
- package/src/runtime/domain/subagents/child-operations.domain.mjs +986 -0
- package/src/runtime/domain/subagents/child-operations.ts +1191 -0
- package/src/runtime/domain/subagents/child-ownership.domain.mjs +205 -0
- package/src/runtime/domain/subagents/child-ownership.ts +160 -0
- package/src/runtime/domain/subagents/child-preflight.mjs +38 -0
- package/src/runtime/domain/subagents/child-process-list.domain.mjs +7 -0
- package/src/runtime/domain/subagents/child-process-list.ts +8 -0
- package/src/runtime/domain/subagents/child-runtime.mjs +67 -0
- package/src/runtime/domain/subagents/child-steer.domain.mjs +109 -0
- package/src/runtime/domain/subagents/child-steer.ts +102 -0
- package/src/runtime/domain/subagents/cleanup.domain.mjs +818 -0
- package/src/runtime/domain/subagents/cleanup.ts +1017 -0
- package/src/runtime/domain/subagents/codex-import.domain.mjs +575 -0
- package/src/runtime/domain/subagents/codex-import.ts +717 -0
- package/src/runtime/domain/subagents/completion.domain.mjs +6 -0
- package/src/runtime/domain/subagents/completion.mjs +152 -0
- package/src/runtime/domain/subagents/completion.ts +11 -0
- package/src/runtime/domain/subagents/config.domain.mjs +248 -0
- package/src/runtime/domain/subagents/config.json +15 -0
- package/src/runtime/domain/subagents/config.ts +328 -0
- package/src/runtime/domain/subagents/continuation-authority.domain.mjs +38 -0
- package/src/runtime/domain/subagents/continuation-authority.ts +31 -0
- package/src/runtime/domain/subagents/continuation-runtime.domain.mjs +89 -0
- package/src/runtime/domain/subagents/continuation-runtime.ts +88 -0
- package/src/runtime/domain/subagents/continuation.domain.mjs +942 -0
- package/src/runtime/domain/subagents/continuation.ts +1051 -0
- package/src/runtime/domain/subagents/delegation.domain.mjs +19 -0
- package/src/runtime/domain/subagents/delegation.ts +24 -0
- package/src/runtime/domain/subagents/effective-settings.domain.mjs +130 -0
- package/src/runtime/domain/subagents/effective-settings.ts +184 -0
- package/src/runtime/domain/subagents/extensions.domain.mjs +6 -0
- package/src/runtime/domain/subagents/extensions.mjs +164 -0
- package/src/runtime/domain/subagents/extensions.ts +19 -0
- package/src/runtime/domain/subagents/failures.domain.mjs +362 -0
- package/src/runtime/domain/subagents/failures.ts +319 -0
- package/src/runtime/domain/subagents/finalization.domain.mjs +168 -0
- package/src/runtime/domain/subagents/finalization.ts +218 -0
- package/src/runtime/domain/subagents/git-remotes.domain.mjs +351 -0
- package/src/runtime/domain/subagents/git-remotes.ts +413 -0
- package/src/runtime/domain/subagents/git-workspace.domain.mjs +337 -0
- package/src/runtime/domain/subagents/git-workspace.ts +430 -0
- package/src/runtime/domain/subagents/goal-provider.domain.mjs +125 -0
- package/src/runtime/domain/subagents/goal-provider.ts +132 -0
- package/src/runtime/domain/subagents/health-observation.domain.mjs +617 -0
- package/src/runtime/domain/subagents/health-observation.ts +818 -0
- package/src/runtime/domain/subagents/health-surface.mjs +284 -0
- package/src/runtime/domain/subagents/health.domain.mjs +261 -0
- package/src/runtime/domain/subagents/health.ts +343 -0
- package/src/runtime/domain/subagents/historical-attention.domain.mjs +37 -0
- package/src/runtime/domain/subagents/historical-attention.ts +35 -0
- package/src/runtime/domain/subagents/incident-model.domain.mjs +322 -0
- package/src/runtime/domain/subagents/incident-model.ts +358 -0
- package/src/runtime/domain/subagents/index.domain.mjs +4731 -0
- package/src/runtime/domain/subagents/index.ts +4781 -0
- package/src/runtime/domain/subagents/launch-abort.domain.mjs +13 -0
- package/src/runtime/domain/subagents/launch-abort.ts +21 -0
- package/src/runtime/domain/subagents/launch-context.domain.mjs +24 -0
- package/src/runtime/domain/subagents/launch-context.ts +23 -0
- package/src/runtime/domain/subagents/launch-digest.mjs +6 -0
- package/src/runtime/domain/subagents/lifecycle.domain.mjs +265 -0
- package/src/runtime/domain/subagents/lifecycle.ts +360 -0
- package/src/runtime/domain/subagents/list.domain.mjs +2 -0
- package/src/runtime/domain/subagents/list.mjs +152 -0
- package/src/runtime/domain/subagents/list.ts +19 -0
- package/src/runtime/domain/subagents/log-cursor.domain.mjs +217 -0
- package/src/runtime/domain/subagents/log-cursor.ts +268 -0
- package/src/runtime/domain/subagents/managed-background-capability.domain.mjs +10 -0
- package/src/runtime/domain/subagents/managed-background-capability.ts +9 -0
- package/src/runtime/domain/subagents/managed-failure-worker.mjs +36 -0
- package/src/runtime/domain/subagents/managed-launch-bootstrap.mjs +48 -0
- package/src/runtime/domain/subagents/managed-launch-client.domain.mjs +75 -0
- package/src/runtime/domain/subagents/managed-launch-client.ts +59 -0
- package/src/runtime/domain/subagents/managed-launch-host.domain.mjs +627 -0
- package/src/runtime/domain/subagents/managed-launch-host.ts +504 -0
- package/src/runtime/domain/subagents/managed-launch-journal.mjs +17 -0
- package/src/runtime/domain/subagents/managed-launch-ownership.domain.mjs +29 -0
- package/src/runtime/domain/subagents/managed-launch-ownership.ts +22 -0
- package/src/runtime/domain/subagents/managed-launch-port.domain.mjs +2 -0
- package/src/runtime/domain/subagents/managed-launch-port.ts +25 -0
- package/src/runtime/domain/subagents/managed-launch-recovery.domain.mjs +29 -0
- package/src/runtime/domain/subagents/managed-launch-recovery.ts +45 -0
- package/src/runtime/domain/subagents/managed-worker-bridge.domain.mjs +63 -0
- package/src/runtime/domain/subagents/managed-worker-bridge.ts +48 -0
- package/src/runtime/domain/subagents/managed-worker.mjs +13 -0
- package/src/runtime/domain/subagents/model-resolution-core.domain.mjs +822 -0
- package/src/runtime/domain/subagents/model-resolution-core.ts +1074 -0
- package/src/runtime/domain/subagents/model-resolution.domain.mjs +22 -0
- package/src/runtime/domain/subagents/model-resolution.ts +32 -0
- package/src/runtime/domain/subagents/navigator.domain.mjs +6 -0
- package/src/runtime/domain/subagents/navigator.mjs +1233 -0
- package/src/runtime/domain/subagents/navigator.ts +39 -0
- package/src/runtime/domain/subagents/operation-bridge-types.domain.mjs +1012 -0
- package/src/runtime/domain/subagents/operation-bridge-types.ts +1156 -0
- package/src/runtime/domain/subagents/outage-observations.domain.mjs +84 -0
- package/src/runtime/domain/subagents/outage-observations.ts +73 -0
- package/src/runtime/domain/subagents/output-payload.domain.mjs +767 -0
- package/src/runtime/domain/subagents/output-payload.ts +987 -0
- package/src/runtime/domain/subagents/owned-exit.domain.mjs +12 -0
- package/src/runtime/domain/subagents/owned-exit.ts +12 -0
- package/src/runtime/domain/subagents/package.json +86 -0
- package/src/runtime/domain/subagents/park-checkpoint.domain.mjs +55 -0
- package/src/runtime/domain/subagents/park-checkpoint.ts +61 -0
- package/src/runtime/domain/subagents/parse.domain.mjs +1218 -0
- package/src/runtime/domain/subagents/parse.ts +1451 -0
- package/src/runtime/domain/subagents/permission-policy.domain.mjs +232 -0
- package/src/runtime/domain/subagents/permission-policy.ts +291 -0
- package/src/runtime/domain/subagents/process-identity.mjs +33 -0
- package/src/runtime/domain/subagents/registry.domain.mjs +1584 -0
- package/src/runtime/domain/subagents/registry.ts +1904 -0
- package/src/runtime/domain/subagents/role-assignment.domain.mjs +104 -0
- package/src/runtime/domain/subagents/role-assignment.ts +141 -0
- package/src/runtime/domain/subagents/roles/role.architect.md +13 -0
- package/src/runtime/domain/subagents/roles/role.developer.md +16 -0
- package/src/runtime/domain/subagents/roles/role.explorer.md +13 -0
- package/src/runtime/domain/subagents/roles/role.product-manager.md +13 -0
- package/src/runtime/domain/subagents/roles/role.researcher.md +13 -0
- package/src/runtime/domain/subagents/roles/role.reviewer.md +13 -0
- package/src/runtime/domain/subagents/roles/role.writer.md +12 -0
- package/src/runtime/domain/subagents/sandbox.domain.mjs +64 -0
- package/src/runtime/domain/subagents/sandbox.ts +92 -0
- package/src/runtime/domain/subagents/session-selection.domain.mjs +32 -0
- package/src/runtime/domain/subagents/session-selection.ts +35 -0
- package/src/runtime/domain/subagents/session-store.domain.mjs +489 -0
- package/src/runtime/domain/subagents/session-store.ts +468 -0
- package/src/runtime/domain/subagents/settings-page.domain.mjs +313 -0
- package/src/runtime/domain/subagents/settings-page.ts +363 -0
- package/src/runtime/domain/subagents/shared-callback-batcher.domain.mjs +777 -0
- package/src/runtime/domain/subagents/shared-callback-batcher.ts +956 -0
- package/src/runtime/domain/subagents/shared-catalog.domain.mjs +70 -0
- package/src/runtime/domain/subagents/shared-catalog.ts +61 -0
- package/src/runtime/domain/subagents/shared-execution-policy.domain.mjs +50 -0
- package/src/runtime/domain/subagents/shared-execution-policy.ts +61 -0
- package/src/runtime/domain/subagents/shared-failure-observations.domain.mjs +1078 -0
- package/src/runtime/domain/subagents/shared-failure-observations.ts +1166 -0
- package/src/runtime/domain/subagents/shared-harness-settings.domain.mjs +126 -0
- package/src/runtime/domain/subagents/shared-harness-settings.ts +116 -0
- package/src/runtime/domain/subagents/shared-log-utils.domain.mjs +1223 -0
- package/src/runtime/domain/subagents/shared-log-utils.ts +1492 -0
- package/src/runtime/domain/subagents/shared-navigator-panels.domain.mjs +289 -0
- package/src/runtime/domain/subagents/shared-navigator-panels.ts +272 -0
- package/src/runtime/domain/subagents/shared-navigator.domain.mjs +1930 -0
- package/src/runtime/domain/subagents/shared-navigator.ts +1962 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-inbox.mjs +144 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-observation.mjs +189 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/index.domain.mjs +51 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/index.ts +122 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-changes.domain.mjs +75 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-changes.ts +73 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-outcomes.domain.mjs +163 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-outcomes.ts +174 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-parent-callbacks.domain.mjs +66 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-parent-callbacks.ts +68 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.domain.mjs +87 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.ts +90 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.domain.mjs +97 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.ts +139 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/errors.domain.mjs +12 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/errors.ts +79 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.domain.mjs +342 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.ts +513 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.domain.mjs +283 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.ts +458 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/recovery.domain.mjs +52 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/recovery.ts +55 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.domain.mjs +114 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.ts +140 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.domain.mjs +280 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.ts +442 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.domain.mjs +420 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.ts +441 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/sqlite.domain.mjs +194 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/sqlite.ts +239 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.domain.mjs +275 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.ts +585 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.domain.mjs +120 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.ts +142 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/task-cancellation.domain.mjs +188 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/task-cancellation.ts +224 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.domain.mjs +25 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.ts +707 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/upgrade.domain.mjs +22 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/upgrade.ts +22 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/validate.domain.mjs +234 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/validate.ts +241 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/waits.domain.mjs +851 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/waits.ts +1424 -0
- package/src/runtime/domain/subagents/shared-permission-blocker.domain.mjs +40 -0
- package/src/runtime/domain/subagents/shared-permission-blocker.ts +48 -0
- package/src/runtime/domain/subagents/shared-render-scheduler.domain.mjs +41 -0
- package/src/runtime/domain/subagents/shared-render-scheduler.ts +47 -0
- package/src/runtime/domain/subagents/shared-sandbox-core.domain.mjs +1531 -0
- package/src/runtime/domain/subagents/shared-sandbox-core.ts +1757 -0
- package/src/runtime/domain/subagents/shared-sandbox-diagnostics.domain.mjs +591 -0
- package/src/runtime/domain/subagents/shared-sandbox-diagnostics.ts +491 -0
- package/src/runtime/domain/subagents/shared-stall-detector.domain.mjs +37 -0
- package/src/runtime/domain/subagents/shared-stall-detector.ts +69 -0
- package/src/runtime/domain/subagents/shared-task-application.domain.mjs +185 -0
- package/src/runtime/domain/subagents/shared-task-application.ts +185 -0
- package/src/runtime/domain/subagents/shared-task-apply-patch.domain.mjs +416 -0
- package/src/runtime/domain/subagents/shared-task-apply-patch.ts +409 -0
- package/src/runtime/domain/subagents/shared-task-exec.domain.mjs +325 -0
- package/src/runtime/domain/subagents/shared-task-exec.ts +335 -0
- package/src/runtime/domain/subagents/shared-task-files.domain.mjs +346 -0
- package/src/runtime/domain/subagents/shared-task-files.ts +342 -0
- package/src/runtime/domain/subagents/shared-task-process-identity.domain.mjs +140 -0
- package/src/runtime/domain/subagents/shared-task-process-identity.ts +126 -0
- package/src/runtime/domain/subagents/shared-task-process-list.domain.mjs +220 -0
- package/src/runtime/domain/subagents/shared-task-process-list.ts +209 -0
- package/src/runtime/domain/subagents/shared-task-sandbox.domain.mjs +227 -0
- package/src/runtime/domain/subagents/shared-task-sandbox.ts +224 -0
- package/src/runtime/domain/subagents/shared-task-tools.domain.mjs +92 -0
- package/src/runtime/domain/subagents/shared-task-tools.ts +123 -0
- package/src/runtime/domain/subagents/shared-work-client.mjs +303 -0
- package/src/runtime/domain/subagents/shared-worktree-context.domain.mjs +142 -0
- package/src/runtime/domain/subagents/shared-worktree-context.ts +158 -0
- package/src/runtime/domain/subagents/skill-picker.domain.mjs +316 -0
- package/src/runtime/domain/subagents/skill-picker.ts +353 -0
- package/src/runtime/domain/subagents/skills.domain.mjs +138 -0
- package/src/runtime/domain/subagents/skills.ts +183 -0
- package/src/runtime/domain/subagents/spawn.domain.mjs +72 -0
- package/src/runtime/domain/subagents/spawn.ts +80 -0
- package/src/runtime/domain/subagents/stop.domain.mjs +184 -0
- package/src/runtime/domain/subagents/stop.ts +217 -0
- package/src/runtime/domain/subagents/subagent-tools.domain.mjs +101 -0
- package/src/runtime/domain/subagents/subagent-tools.ts +137 -0
- package/src/runtime/domain/subagents/task-guard.domain.mjs +163 -0
- package/src/runtime/domain/subagents/task-guard.ts +144 -0
- package/src/runtime/domain/subagents/task-policy.domain.mjs +201 -0
- package/src/runtime/domain/subagents/task-policy.ts +236 -0
- package/src/runtime/domain/subagents/task-runtime.mjs +53 -0
- package/src/runtime/domain/subagents/task-tool-owner.domain.mjs +18 -0
- package/src/runtime/domain/subagents/task-tool-owner.ts +21 -0
- package/src/runtime/domain/subagents/thinking.domain.mjs +22 -0
- package/src/runtime/domain/subagents/thinking.ts +28 -0
- package/src/runtime/domain/subagents/tier-policy.domain.mjs +75 -0
- package/src/runtime/domain/subagents/tier-policy.ts +126 -0
- package/src/runtime/domain/subagents/timing-notifications.domain.mjs +51 -0
- package/src/runtime/domain/subagents/timing-notifications.ts +37 -0
- package/src/runtime/domain/subagents/timing.domain.mjs +487 -0
- package/src/runtime/domain/subagents/timing.ts +634 -0
- package/src/runtime/domain/subagents/tools.domain.mjs +558 -0
- package/src/runtime/domain/subagents/tools.ts +629 -0
- package/src/runtime/domain/subagents/widget.domain.mjs +6 -0
- package/src/runtime/domain/subagents/widget.mjs +246 -0
- package/src/runtime/domain/subagents/widget.ts +30 -0
- package/src/runtime/domain/subagents/work-observation.domain.mjs +316 -0
- package/src/runtime/domain/subagents/work-observation.ts +248 -0
- package/src/runtime/domain/subagents/worker-catalog.domain.mjs +120 -0
- package/src/runtime/domain/subagents/worker-catalog.ts +162 -0
- package/src/runtime/domain/subagents/worker-operations-host.domain.mjs +1009 -0
- package/src/runtime/domain/subagents/worker-operations-host.ts +1238 -0
- package/src/runtime/domain/subagents/worker-waits.domain.mjs +2336 -0
- package/src/runtime/domain/subagents/worker-waits.ts +2878 -0
- package/src/runtime/domain/subagents/workspace-scope.domain.mjs +52 -0
- package/src/runtime/domain/subagents/workspace-scope.ts +50 -0
- package/src/runtime/health-config.ts +27 -11
- package/src/runtime/log-checkpoints.ts +117 -0
- package/src/runtime/mutation-store.ts +5 -4
- package/src/runtime/queries.ts +27 -3
- package/src/runtime/query-executor.ts +178 -34
- package/src/runtime/query-worker.mjs +119 -22
- package/src/runtime/read-model.ts +939 -113
- package/src/runtime/read-watchdog-probe.ts +4 -0
- package/src/runtime/result-supervision.ts +188 -16
- package/src/runtime/source-watch.ts +175 -0
- package/src/runtime/subagent-launch.ts +73 -0
- package/src/runtime/work-budget.ts +130 -0
- package/src/runtime/work-client.d.mts +8 -0
- package/src/runtime/work-client.mjs +150 -7
- package/src/server.ts +185 -22
- package/src/session-activity.ts +26 -4
- package/src/version.ts +1 -1
|
@@ -0,0 +1,1531 @@
|
|
|
1
|
+
// Generated fixed launch domain from packages/pi-better-background-tasks/src/shared-sandbox-core.ts. Do not edit directly.
|
|
2
|
+
// Generated from packages/sandbox-core/index.ts. Do not edit directly.
|
|
3
|
+
/**
|
|
4
|
+
* OS-level write sandbox mechanism shared by Pi extensions.
|
|
5
|
+
*
|
|
6
|
+
* Legacy policies are write-only: a sandboxed process may READ anywhere and use
|
|
7
|
+
* the network, but may only WRITE under a canonical root plus runtime paths.
|
|
8
|
+
* Optional permissions add capability restrictions for reads, writes, launches
|
|
9
|
+
* and network access. They cover known credential files, not OS keychains,
|
|
10
|
+
* credential services, or tokens inherited in the child environment.
|
|
11
|
+
*
|
|
12
|
+
* This module owns the mechanism only: backend discovery, canonical path
|
|
13
|
+
* containment, write-deny compilation, macOS SBPL profile construction, Linux
|
|
14
|
+
* Bubblewrap mount construction, ordered executable/argv wrapping, and support
|
|
15
|
+
* diagnostics. It owns no Pi tool, TUI, background-task, or subagent lifecycle
|
|
16
|
+
* policy — callers decide when a sandbox is requested and what it may write.
|
|
17
|
+
*
|
|
18
|
+
* Every platform/filesystem dependency is reachable through the optional
|
|
19
|
+
* `SandboxSeams` argument so callers can plan deterministically in tests.
|
|
20
|
+
*/
|
|
21
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
22
|
+
import { platform as osPlatform } from "node:os";
|
|
23
|
+
import { accessSync, closeSync, constants, existsSync, lstatSync, mkdirSync, openSync, readdirSync, readlinkSync, realpathSync, statSync, writeFileSync, } from "node:fs";
|
|
24
|
+
import { basename, delimiter, dirname, isAbsolute, join, parse, resolve, sep } from "node:path";
|
|
25
|
+
/** Whether a file access level allows creating and overwriting files. */
|
|
26
|
+
export function canWrite(mode) {
|
|
27
|
+
return mode === "write" || mode === "read-write";
|
|
28
|
+
}
|
|
29
|
+
export const SHELL_EXECUTABLE_NAMES = Object.freeze([
|
|
30
|
+
"sh", "bash", "zsh", "dash", "ksh", "ksh93", "mksh", "pdksh", "ash", "csh", "tcsh", "fish",
|
|
31
|
+
"yash", "posh", "rbash", "busybox", "pwsh", "powershell", "cmd",
|
|
32
|
+
]);
|
|
33
|
+
// Fixed runtime code starts after confinement. The launch data is JSON, never
|
|
34
|
+
// env/shell syntax. Native execve never falls back to a command interpreter.
|
|
35
|
+
const APPLICATION_RUNNER = `
|
|
36
|
+
const fs = require("node:fs");
|
|
37
|
+
const path = require("node:path");
|
|
38
|
+
const spec = JSON.parse(process.argv[1]);
|
|
39
|
+
function resolveExecutable(executable) {
|
|
40
|
+
if (executable.includes(path.sep)) return path.resolve(executable);
|
|
41
|
+
const directories = (spec.env.PATH || spec.env.Path || "").split(path.delimiter);
|
|
42
|
+
const resolved = directories.map(directory => path.resolve(directory || ".", executable)).find(candidate => {
|
|
43
|
+
try { fs.accessSync(candidate, fs.constants.X_OK); return fs.statSync(candidate).isFile(); }
|
|
44
|
+
catch { return false; }
|
|
45
|
+
});
|
|
46
|
+
if (!resolved) throw new Error("Executable not found: " + executable);
|
|
47
|
+
return resolved;
|
|
48
|
+
}
|
|
49
|
+
function isShellInterpreter(interpreter) {
|
|
50
|
+
const canonical = fs.realpathSync(interpreter);
|
|
51
|
+
if (spec.shellNames.includes(path.basename(interpreter)) || spec.shellNames.includes(path.basename(canonical))) return true;
|
|
52
|
+
const target = fs.statSync(canonical, { bigint: true });
|
|
53
|
+
const directories = new Set(["/bin", "/usr/bin", ...(spec.env.PATH || spec.env.Path || "").split(path.delimiter)]);
|
|
54
|
+
for (const directory of directories) for (const name of spec.shellNames) {
|
|
55
|
+
try {
|
|
56
|
+
const known = fs.statSync(path.resolve(directory || ".", name), { bigint: true });
|
|
57
|
+
if (target.dev === known.dev && target.ino === known.ino) return true;
|
|
58
|
+
} catch { /* A shell need not exist in every executable directory. */ }
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
function verifyExecutable(executable, depth = 0) {
|
|
63
|
+
if (depth > 4) throw new Error("Too many shebang interpreters.");
|
|
64
|
+
fs.accessSync(executable, fs.constants.X_OK);
|
|
65
|
+
const fd = fs.openSync(executable, "r");
|
|
66
|
+
const header = Buffer.alloc(4096);
|
|
67
|
+
let size;
|
|
68
|
+
try { size = fs.readSync(fd, header, 0, header.length, 0); }
|
|
69
|
+
finally { fs.closeSync(fd); }
|
|
70
|
+
const magic = size >= 4 ? header.readUInt32BE(0) : 0;
|
|
71
|
+
const binary = [0x7f454c46, 0xfeedface, 0xcefaedfe, 0xfeedfacf, 0xcffaedfe,
|
|
72
|
+
0xcafebabe, 0xbebafeca, 0xcafebabf, 0xbfbafeca].includes(magic);
|
|
73
|
+
const script = size >= 2 && header[0] === 35 && header[1] === 33;
|
|
74
|
+
if (!binary && !script) throw new Error("Executable has no supported binary format or shebang; shell fallback is disabled.");
|
|
75
|
+
if (!spec.shell && script) {
|
|
76
|
+
const words = header.subarray(2, size).toString("utf8").split(/\\r?\\n/, 1)[0].trim().split(/\\s+/);
|
|
77
|
+
let interpreter = resolveExecutable(words[0]);
|
|
78
|
+
if (path.basename(fs.realpathSync(interpreter)) === "env") {
|
|
79
|
+
if (words.length !== 2 || words[1].startsWith("-") || words[1].includes("=")) {
|
|
80
|
+
throw new Error("Sandbox: Shell scripts are Off; an env shebang must name one direct interpreter without options.");
|
|
81
|
+
}
|
|
82
|
+
interpreter = resolveExecutable(words[1]);
|
|
83
|
+
}
|
|
84
|
+
if (isShellInterpreter(interpreter)) {
|
|
85
|
+
throw new Error("Sandbox: Shell scripts are Off; the executable uses a shell shebang.");
|
|
86
|
+
}
|
|
87
|
+
verifyExecutable(interpreter, depth + 1);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
if (typeof process.execve !== "function") {
|
|
92
|
+
throw new Error("Direct application execution requires Node.js with process.execve (22.15+ or 24+). Update Node.js; no shell fallback was run.");
|
|
93
|
+
}
|
|
94
|
+
const executable = resolveExecutable(spec.executable);
|
|
95
|
+
verifyExecutable(executable);
|
|
96
|
+
process.execve(executable, [executable, ...spec.args], spec.env);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error(error.code === "ENOEXEC" ? "Executable format is unsupported; shell fallback is disabled." : error.message);
|
|
99
|
+
process.exitCode = 126;
|
|
100
|
+
}
|
|
101
|
+
`;
|
|
102
|
+
/** Fixed post-boundary launcher for literal argv, without env assignment or shell fallback. */
|
|
103
|
+
export function directApplicationTarget(executable, args, env, shell) {
|
|
104
|
+
return { execPath: canonicalizePath(process.execPath), execArgs: ["--eval", APPLICATION_RUNNER,
|
|
105
|
+
JSON.stringify({ executable, args, env, shell, shellNames: SHELL_EXECUTABLE_NAMES })] };
|
|
106
|
+
}
|
|
107
|
+
const MACOS_SANDBOX_EXEC = "/usr/bin/sandbox-exec";
|
|
108
|
+
const CREDENTIAL_LOCATIONS = [
|
|
109
|
+
".ssh", ".aws", ".config/gh", ".config/gcloud", ".azure", ".kube",
|
|
110
|
+
".docker/config.json", ".npmrc", ".netrc", ".git-credentials", ".pi/agent/auth.json",
|
|
111
|
+
];
|
|
112
|
+
/** Credential stores added to the Stored credentials row by the broad-write profile. */
|
|
113
|
+
const BROAD_CREDENTIAL_LOCATIONS = [
|
|
114
|
+
"Library/Keychains", ".claude/.credentials.json", ".claude.json", ".gnupg",
|
|
115
|
+
".codex/auth.json", ".cargo/credentials.toml", ".cargo/credentials", ".pgpass", ".config/rclone",
|
|
116
|
+
];
|
|
117
|
+
/**
|
|
118
|
+
* Code that runs later: shell startup files, agent/harness configuration and
|
|
119
|
+
* login agents. The broad-write profile keeps them readable (skills and
|
|
120
|
+
* extension sources are read by confined tasks) but never writable, removable
|
|
121
|
+
* or renamable. This list is fixed; callers extend it with `denyWrite`.
|
|
122
|
+
*/
|
|
123
|
+
const CODE_LATER_LOCATIONS = [
|
|
124
|
+
".bashrc", ".bash_profile", ".bash_login", ".bash_logout", ".profile",
|
|
125
|
+
".zshrc", ".zshenv", ".zprofile", ".zlogin", ".zlogout",
|
|
126
|
+
".config/fish", ".config/git", ".gitconfig", ".config/systemd/user", ".config/autostart",
|
|
127
|
+
".pi", ".claude", ".agents", "Library/LaunchAgents",
|
|
128
|
+
".local/bin", "bin", ".git-templates", ".oh-my-zsh/custom", ".gradle/init.d",
|
|
129
|
+
".cargo/config.toml", ".cargo/config",
|
|
130
|
+
];
|
|
131
|
+
/** Deny-list entries that are directories when they exist (Linux materializes them as such). */
|
|
132
|
+
const DIRECTORY_LOCATIONS = new Set([
|
|
133
|
+
".ssh", ".aws", ".config/gh", ".config/gcloud", ".azure", ".kube", "Library/Keychains",
|
|
134
|
+
".config/fish", ".config/git", ".config/systemd/user", ".config/autostart",
|
|
135
|
+
".pi", ".claude", ".agents", "Library/LaunchAgents", ".gnupg", ".config/rclone",
|
|
136
|
+
".local/bin", "bin", ".git-templates", ".oh-my-zsh/custom", ".gradle/init.d",
|
|
137
|
+
]);
|
|
138
|
+
const BROAD_TEMP_ROOTS = {
|
|
139
|
+
darwin: ["/private/tmp", "/private/var/folders", "/private/var/tmp"],
|
|
140
|
+
linux: ["/tmp", "/var/tmp", "/dev/shm"],
|
|
141
|
+
};
|
|
142
|
+
// Executables, dynamic libraries and OS frameworks needed to start a child.
|
|
143
|
+
// This deliberately excludes the home directory, /etc, and credential stores.
|
|
144
|
+
// A process relying on /etc or /proc configuration (DNS, certificates, NSS)
|
|
145
|
+
// may not start or function under Linux outsideProject=off; callers must not
|
|
146
|
+
// silently remount these broad host trees to work around that failure.
|
|
147
|
+
const RUNTIME_ROOTS = ["/usr", "/bin", "/sbin", "/lib", "/lib64", "/System/Library", "/System/Cryptexes", "/Library/Apple", "/Library/Developer", "/opt/homebrew"];
|
|
148
|
+
const TEMP_ROOTS = ["/private/var/folders", "/private/tmp", "/tmp", "/dev"];
|
|
149
|
+
const READ_RUNTIME_ROOTS = [...RUNTIME_ROOTS, "/dev"];
|
|
150
|
+
function systemGetconf(name) {
|
|
151
|
+
const result = spawnSync("/usr/bin/getconf", [name], {
|
|
152
|
+
encoding: "utf8", env: { PATH: "/usr/bin:/bin", LANG: "C" }, timeout: 3000, maxBuffer: 4096,
|
|
153
|
+
});
|
|
154
|
+
return result.status === 0 ? result.stdout.trim() : undefined;
|
|
155
|
+
}
|
|
156
|
+
function compatibilityPaths(policy, seams) {
|
|
157
|
+
if (!policy.permissions || !policy.runtimeCompatibility || policy.permissions.outsideProject === "off")
|
|
158
|
+
return [];
|
|
159
|
+
const platform = currentPlatform(seams);
|
|
160
|
+
if (platform === "linux")
|
|
161
|
+
return [canonicalizePath("/tmp", seams)];
|
|
162
|
+
if (platform !== "darwin")
|
|
163
|
+
return [];
|
|
164
|
+
const home = canonicalizePath(policy.home, seams);
|
|
165
|
+
const project = canonicalizePath(policy.writableRoot, seams);
|
|
166
|
+
const paths = [canonicalizePath("/tmp", seams)];
|
|
167
|
+
for (const [key, leaf] of [["DARWIN_USER_TEMP_DIR", ""], ["DARWIN_USER_CACHE_DIR", "mds"]]) {
|
|
168
|
+
const raw = (seams.getconf ?? systemGetconf)(key);
|
|
169
|
+
if (!raw || !raw.startsWith("/") || raw.includes("\0") ||
|
|
170
|
+
raw.split("/").some((part) => part === "." || part === "..")) {
|
|
171
|
+
throw new Error(`Invalid ${key} runtime directory`);
|
|
172
|
+
}
|
|
173
|
+
// getconf returns /var/folders on macOS; /var is a system alias for /private/var.
|
|
174
|
+
const normalized = resolve(raw.replace(/^\/var\/folders\//, "/private/var/folders/"));
|
|
175
|
+
const expected = /^\/private\/var\/folders\/[^/]+\/[^/]+\/[TC]$/.test(normalized) &&
|
|
176
|
+
normalized.endsWith(key === "DARWIN_USER_TEMP_DIR" ? "/T" : "/C");
|
|
177
|
+
const canonical = canonicalizePath(normalized, seams);
|
|
178
|
+
if (!expected || canonical !== normalized ||
|
|
179
|
+
(contains(home, canonical) || contains(canonical, home) || contains(project, canonical))) {
|
|
180
|
+
throw new Error(`Unsafe ${key} runtime directory`);
|
|
181
|
+
}
|
|
182
|
+
paths.push(leaf ? join(canonical, leaf) : canonical);
|
|
183
|
+
}
|
|
184
|
+
// An existing MDS symlink must not redirect the narrow write allowance.
|
|
185
|
+
const mds = paths[2];
|
|
186
|
+
if (!mds || canonicalizePath(mds, seams) !== mds || contains(mds, project) || contains(project, mds)) {
|
|
187
|
+
throw new Error("Unsafe MDS runtime directory");
|
|
188
|
+
}
|
|
189
|
+
return [...new Set(paths)];
|
|
190
|
+
}
|
|
191
|
+
/** Only known on-disk credentials: keychains, services and inherited env tokens are out of scope. */
|
|
192
|
+
export function credentialFilePaths(home, seams = {}) {
|
|
193
|
+
const paths = CREDENTIAL_LOCATIONS.map((name) => join(home, name));
|
|
194
|
+
const configuredAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
195
|
+
if (configuredAgentDir)
|
|
196
|
+
paths.push(join(configuredAgentDir.startsWith("~/")
|
|
197
|
+
? join(home, configuredAgentDir.slice(2)) : configuredAgentDir, "auth.json"));
|
|
198
|
+
return [...new Set(paths.map((path) => canonicalizePath(path, seams)))].sort();
|
|
199
|
+
}
|
|
200
|
+
function currentPlatform(seams) {
|
|
201
|
+
return (seams.platform ?? osPlatform)();
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Resolve `path` to an absolute canonical path. Symlinks are resolved on the
|
|
205
|
+
* longest existing ancestor so a target that does not exist yet still
|
|
206
|
+
* canonicalizes through its real parent chain.
|
|
207
|
+
*/
|
|
208
|
+
export function canonicalizePath(path, seams = {}) {
|
|
209
|
+
const canonicalize = seams.canonicalize ?? realpathSync.native;
|
|
210
|
+
const absolute = resolve(path);
|
|
211
|
+
try {
|
|
212
|
+
const resolved = canonicalize(absolute);
|
|
213
|
+
// APFS firmlinks are not resolved by realpath. Normalize the Data-volume
|
|
214
|
+
// alias only when both names demonstrably refer to the same inode.
|
|
215
|
+
const dataPrefix = "/System/Volumes/Data";
|
|
216
|
+
if (!seams.canonicalize && currentPlatform(seams) === "darwin" && resolved.startsWith(`${dataPrefix}/`)) {
|
|
217
|
+
const candidate = resolved.slice(dataPrefix.length);
|
|
218
|
+
try {
|
|
219
|
+
const source = statSync(resolved);
|
|
220
|
+
const alias = statSync(candidate);
|
|
221
|
+
if (source.dev === alias.dev && source.ino === alias.ino)
|
|
222
|
+
return realpathSync.native(candidate);
|
|
223
|
+
}
|
|
224
|
+
catch { /* An unrelated Data-volume path keeps its original identity. */ }
|
|
225
|
+
}
|
|
226
|
+
return resolved;
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
// Not created yet (or unreadable): canonicalize the parent instead.
|
|
230
|
+
}
|
|
231
|
+
const parent = dirname(absolute);
|
|
232
|
+
if (parent === absolute)
|
|
233
|
+
return absolute;
|
|
234
|
+
return join(canonicalizePath(parent, seams), basename(absolute));
|
|
235
|
+
}
|
|
236
|
+
function compile(policy, seams, strictRoot) {
|
|
237
|
+
// The Linux backend has always required the writable root to exist before it
|
|
238
|
+
// bind-mounts it; the macOS backend has always tolerated a not-yet-created
|
|
239
|
+
// one. Keep both behaviors rather than unifying them here.
|
|
240
|
+
const writableRoot = strictRoot
|
|
241
|
+
? (seams.canonicalize ?? realpathSync.native)(policy.writableRoot)
|
|
242
|
+
: canonicalizePath(policy.writableRoot, seams);
|
|
243
|
+
// The broad profile also keeps each literal entry: its removal grants would
|
|
244
|
+
// otherwise let a task replace a symlinked control path.
|
|
245
|
+
const lexicalDeny = isBroadWritePermissions(policy.permissions);
|
|
246
|
+
const denyWrite = [
|
|
247
|
+
...new Set((policy.denyWrite ?? []).flatMap((entry) => lexicalDeny
|
|
248
|
+
? [resolve(entry), ...symlinkHops(entry), canonicalizePath(entry, seams)] : [canonicalizePath(entry, seams)])),
|
|
249
|
+
].sort();
|
|
250
|
+
const broadMode = isBroadWritePermissions(policy.permissions);
|
|
251
|
+
const compatibilityWrite = broadMode ? [] : compatibilityPaths(policy, seams);
|
|
252
|
+
const runtimeWrite = [...new Set([
|
|
253
|
+
...(policy.runtimeWrite ?? []).map((path) => canonicalizePath(path, seams)),
|
|
254
|
+
...compatibilityWrite,
|
|
255
|
+
])];
|
|
256
|
+
return {
|
|
257
|
+
writableRoot, denyWrite, home: policy.home,
|
|
258
|
+
...(policy.permissions && {
|
|
259
|
+
permissions: { ...policy.permissions },
|
|
260
|
+
credentialPaths: broadMode ? broadCredentialPaths(policy.home, seams) : credentialFilePaths(policy.home, seams),
|
|
261
|
+
compatibilityWrite,
|
|
262
|
+
runtimeWrite,
|
|
263
|
+
}),
|
|
264
|
+
...(broadMode && {
|
|
265
|
+
broad: compileBroadWrite(policy, writableRoot, runtimeWrite, seams),
|
|
266
|
+
}),
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/** True when a permission profile selects the broad-write profile. */
|
|
270
|
+
export function isBroadWritePermissions(permissions) {
|
|
271
|
+
return permissions?.outsideProject === "write";
|
|
272
|
+
}
|
|
273
|
+
function expandHome(path, home) {
|
|
274
|
+
return path === "~" ? home : path.startsWith("~/") ? join(home, path.slice(2)) : path;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* A fixed-list location both as the literal entry under the canonical home and
|
|
278
|
+
* as the path it resolves to. A dotfiles manager (stow, chezmoi) makes
|
|
279
|
+
* `~/.zshrc` a symlink into an ordinary folder: guarding only the target would
|
|
280
|
+
* let a task remove the link and put a real file in its place.
|
|
281
|
+
*/
|
|
282
|
+
function lexicalAndCanonical(home, name, seams) {
|
|
283
|
+
const lexical = join(canonicalizePath(home, seams), name);
|
|
284
|
+
return [lexical, ...symlinkHops(lexical), canonicalizePath(lexical, seams)];
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Every symlink directory entry met while resolving `path`, component by
|
|
288
|
+
* component (as `readlink` would follow them), plus where a dangling chain
|
|
289
|
+
* would land. A chain such as
|
|
290
|
+
* `~/.zshrc -> ~/.dotfiles/zshrc -> ~/dotfiles/zshrc` has an intermediate link
|
|
291
|
+
* in a removable directory; replacing it would retarget the protected entry, so
|
|
292
|
+
* each hop the user could replace is protected too. When a followed link
|
|
293
|
+
* points at something missing, creating it would give the entry content, so
|
|
294
|
+
* the first missing component (which covers every missing directory below it)
|
|
295
|
+
* and the full path it would resolve to are protected as well. Bounded: a loop
|
|
296
|
+
* or more than 40 hops stops the walk (such a path resolves to nothing).
|
|
297
|
+
*/
|
|
298
|
+
function symlinkHops(path) {
|
|
299
|
+
const { hops, unresolved } = walkSymlinks(path);
|
|
300
|
+
return [...hops, ...unresolved];
|
|
301
|
+
}
|
|
302
|
+
function walkSymlinks(path) {
|
|
303
|
+
const hops = [];
|
|
304
|
+
const seen = new Set();
|
|
305
|
+
let followed = 0;
|
|
306
|
+
const absolute = resolve(path);
|
|
307
|
+
let current = parse(absolute).root;
|
|
308
|
+
let pending = absolute.slice(current.length).split(sep);
|
|
309
|
+
while (pending.length) {
|
|
310
|
+
const component = pending.shift();
|
|
311
|
+
if (!component || component === ".")
|
|
312
|
+
continue;
|
|
313
|
+
if (component === "..") {
|
|
314
|
+
current = dirname(current);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
const entry = join(current, component);
|
|
318
|
+
let link;
|
|
319
|
+
try {
|
|
320
|
+
link = lstatSync(entry).isSymbolicLink();
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
// Missing. Only a followed link makes this a dangling target; a
|
|
324
|
+
// plain absent path is guarded as it is.
|
|
325
|
+
return { hops, unresolved: followed ? [...new Set([entry, resolve(entry, ...pending)])] : [] };
|
|
326
|
+
}
|
|
327
|
+
if (!link) {
|
|
328
|
+
current = entry;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (++followed > 40 || seen.has(entry))
|
|
332
|
+
break;
|
|
333
|
+
seen.add(entry);
|
|
334
|
+
// Only a link the user can replace matters (not e.g. macOS's root-owned /tmp -> private/tmp).
|
|
335
|
+
try {
|
|
336
|
+
accessSync(current, constants.W_OK);
|
|
337
|
+
hops.push(entry);
|
|
338
|
+
}
|
|
339
|
+
catch { /* immutable entry */ }
|
|
340
|
+
let target;
|
|
341
|
+
try {
|
|
342
|
+
target = readlinkSync(entry);
|
|
343
|
+
}
|
|
344
|
+
catch {
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
if (isAbsolute(target)) {
|
|
348
|
+
current = parse(target).root;
|
|
349
|
+
pending = [...target.slice(current.length).split(sep), ...pending];
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
pending = [...target.split(sep), ...pending];
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return { hops, unresolved: [] };
|
|
356
|
+
}
|
|
357
|
+
/** Whether a symlink resolves to nothing: dangling (ENOENT), or looping or over 40 hops (ELOOP). */
|
|
358
|
+
function unresolvable(path) {
|
|
359
|
+
try {
|
|
360
|
+
statSync(path);
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
function broadCredentialPaths(home, seams) {
|
|
368
|
+
const configuredAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
369
|
+
return [...new Set([
|
|
370
|
+
...credentialFilePaths(home, seams),
|
|
371
|
+
...[...CREDENTIAL_LOCATIONS, ...BROAD_CREDENTIAL_LOCATIONS].flatMap((name) => lexicalAndCanonical(home, name, seams)),
|
|
372
|
+
...(configuredAgentDir ? [resolve(expandHome(configuredAgentDir, home), "auth.json"),
|
|
373
|
+
...symlinkHops(resolve(expandHome(configuredAgentDir, home), "auth.json"))] : []),
|
|
374
|
+
])].sort();
|
|
375
|
+
}
|
|
376
|
+
function compileBroadWrite(policy, writableRoot, runtimeWrite, seams) {
|
|
377
|
+
const permissions = policy.permissions;
|
|
378
|
+
const home = canonicalizePath(policy.home, seams);
|
|
379
|
+
if (home === sep || RUNTIME_ROOTS.some((root) => contains(canonicalizePath(root, seams), home))) {
|
|
380
|
+
throw new Error("The broad-write sandbox profile needs a real home directory, not / or a system runtime root.");
|
|
381
|
+
}
|
|
382
|
+
if (writableRoot === home || contains(writableRoot, home)) {
|
|
383
|
+
throw new Error("The broad-write sandbox profile needs a project directory inside or beside home, not home itself.");
|
|
384
|
+
}
|
|
385
|
+
const tempRoots = [...new Set((BROAD_TEMP_ROOTS[currentPlatform(seams)] ?? [])
|
|
386
|
+
.map((path) => canonicalizePath(path, seams)))];
|
|
387
|
+
const configuredAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
388
|
+
const codePaths = [...new Set([
|
|
389
|
+
...CODE_LATER_LOCATIONS.flatMap((name) => lexicalAndCanonical(home, name, seams)),
|
|
390
|
+
...(configuredAgentDir ? [resolve(expandHome(configuredAgentDir, home)),
|
|
391
|
+
...symlinkHops(expandHome(configuredAgentDir, home)),
|
|
392
|
+
canonicalizePath(expandHome(configuredAgentDir, home), seams)] : []),
|
|
393
|
+
])].sort();
|
|
394
|
+
return {
|
|
395
|
+
home,
|
|
396
|
+
tempRoots,
|
|
397
|
+
deleteRoots: [...new Set([
|
|
398
|
+
...(permissions.projectFiles === "read-write" ? [writableRoot] : []),
|
|
399
|
+
...runtimeWrite,
|
|
400
|
+
])].sort(),
|
|
401
|
+
codePaths,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
/** Inside a git worktree folder (`.worktrees/`, `*-worktrees/`) below `root`: its contents, not the folder. */
|
|
405
|
+
function insideWorktreeFolder(path, root) {
|
|
406
|
+
if (!contains(root, path) || path === root)
|
|
407
|
+
return false;
|
|
408
|
+
return path.slice(root.length + 1).split(sep).slice(0, -1)
|
|
409
|
+
.some((part) => part === ".worktrees" || /^[^/]+-worktrees$/.test(part));
|
|
410
|
+
}
|
|
411
|
+
/** Hidden entries of home, and git worktree folders anywhere under it. */
|
|
412
|
+
function disposableHomePath(path, home) {
|
|
413
|
+
if (!contains(home, path) || path === home)
|
|
414
|
+
return false;
|
|
415
|
+
return path.slice(home.length + 1).startsWith(".") || insideWorktreeFolder(path, home);
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Canonicalize a write policy once so containment checks and backend rules
|
|
419
|
+
* agree on exactly which paths they are talking about.
|
|
420
|
+
*/
|
|
421
|
+
export function compileWritePolicy(policy, seams = {}) {
|
|
422
|
+
return compile(policy, seams, false);
|
|
423
|
+
}
|
|
424
|
+
function contains(root, target) {
|
|
425
|
+
if (target === root)
|
|
426
|
+
return true;
|
|
427
|
+
return target.startsWith(root.endsWith(sep) ? root : `${root}${sep}`);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Spell `path` the way the policy spells it when they differ only in case, on
|
|
431
|
+
* macOS. `canonicalizePath` fixes the case of every existing component on a
|
|
432
|
+
* case-insensitive volume, but a missing tail (`~/.NPMRC` while `~/.npmrc` is
|
|
433
|
+
* absent) keeps the caller's case, and the kernel still treats it as the
|
|
434
|
+
* protected name. Seatbelt matches paths case-insensitively on every volume,
|
|
435
|
+
* case-sensitive APFS included, so this folds on darwin without probing the
|
|
436
|
+
* volume: the pre-check decides as the kernel does (#354, #358). The longest
|
|
437
|
+
* policy path that matches case-insensitively wins. Other platforms never fold.
|
|
438
|
+
*/
|
|
439
|
+
function alignCaseToPolicy(path, policy, seams) {
|
|
440
|
+
if (currentPlatform(seams) !== "darwin")
|
|
441
|
+
return path;
|
|
442
|
+
const broad = policy.broad;
|
|
443
|
+
const candidates = [policy.writableRoot, ...policy.denyWrite, ...(policy.credentialPaths ?? []),
|
|
444
|
+
...(policy.runtimeWrite ?? []), ...(policy.compatibilityWrite ?? []),
|
|
445
|
+
...(broad ? [broad.home, ...broad.tempRoots, ...broad.deleteRoots, ...broad.codePaths] : [])];
|
|
446
|
+
const lower = path.toLowerCase();
|
|
447
|
+
if (lower.length !== path.length)
|
|
448
|
+
return path;
|
|
449
|
+
let best;
|
|
450
|
+
for (const candidate of candidates) {
|
|
451
|
+
const folded = candidate.toLowerCase();
|
|
452
|
+
if (folded.length !== candidate.length || !contains(folded, lower))
|
|
453
|
+
continue;
|
|
454
|
+
if (!best || candidate.length > best.length)
|
|
455
|
+
best = candidate;
|
|
456
|
+
}
|
|
457
|
+
if (!best || path.startsWith(best))
|
|
458
|
+
return path;
|
|
459
|
+
return `${best}${path.slice(best.length)}`;
|
|
460
|
+
}
|
|
461
|
+
function isCredential(path, policy) {
|
|
462
|
+
return (policy.credentialPaths ?? []).some((credential) => contains(credential, path));
|
|
463
|
+
}
|
|
464
|
+
function runtimeRoots(seams) {
|
|
465
|
+
return [...new Set(READ_RUNTIME_ROOTS.map((path) => canonicalizePath(path, seams)))];
|
|
466
|
+
}
|
|
467
|
+
/** Decide read access using the same canonical path and capability precedence as writes. */
|
|
468
|
+
export function evaluateReadAccess(target, policy, seams = {}) {
|
|
469
|
+
const path = alignCaseToPolicy(canonicalizePath(target, seams), policy, seams);
|
|
470
|
+
const permissions = policy.permissions;
|
|
471
|
+
if (!permissions)
|
|
472
|
+
return { allowed: true, path };
|
|
473
|
+
if (policy.broad) {
|
|
474
|
+
const denied = (isCredential(path, policy) && permissions.storedCredentials === "off") ||
|
|
475
|
+
(permissions.projectFiles === "off" && contains(policy.writableRoot, path));
|
|
476
|
+
return denied ? { allowed: false, path, reason: "read-denied" } : { allowed: true, path };
|
|
477
|
+
}
|
|
478
|
+
const mode = isCredential(path, policy) ? permissions.storedCredentials
|
|
479
|
+
: policy.runtimeWrite?.some((root) => !policy.compatibilityWrite?.includes(root) && contains(root, path)) ? "read-write"
|
|
480
|
+
: contains(policy.writableRoot, path) ? permissions.projectFiles
|
|
481
|
+
: policy.compatibilityWrite?.some((root) => contains(root, path)) ? "read-write"
|
|
482
|
+
: path === sep || runtimeRoots(seams).some((root) => contains(root, path)) ? "read"
|
|
483
|
+
: permissions.outsideProject;
|
|
484
|
+
return mode === "off" ? { allowed: false, path, reason: "read-denied" } : { allowed: true, path };
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Decide whether an in-process write to `target` is permitted by a compiled
|
|
488
|
+
* policy. This is the same containment rule the kernel backends enforce, for
|
|
489
|
+
* callers that mutate files directly instead of spawning a child.
|
|
490
|
+
*/
|
|
491
|
+
export function evaluateWriteAccess(target, policy, seams = {}) {
|
|
492
|
+
const path = alignCaseToPolicy(canonicalizePath(target, seams), policy, seams);
|
|
493
|
+
if (policy.broad)
|
|
494
|
+
return evaluateBroadWrite(path, policy, policy.broad, seams);
|
|
495
|
+
if (!policy.permissions && !contains(policy.writableRoot, path)) {
|
|
496
|
+
return { allowed: false, path, reason: "outside-writable-root" };
|
|
497
|
+
}
|
|
498
|
+
for (const denied of policy.denyWrite) {
|
|
499
|
+
if (contains(denied, path)) {
|
|
500
|
+
return { allowed: false, path, reason: "write-denied", deniedBy: denied };
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (policy.permissions) {
|
|
504
|
+
const mode = isCredential(path, policy) ? policy.permissions.storedCredentials
|
|
505
|
+
: policy.runtimeWrite?.some((root) => !policy.compatibilityWrite?.includes(root) && contains(root, path)) ? "read-write"
|
|
506
|
+
: contains(policy.writableRoot, path) ? policy.permissions.projectFiles
|
|
507
|
+
: policy.compatibilityWrite?.some((root) => contains(root, path)) ? "read-write"
|
|
508
|
+
: contains(canonicalizePath("/dev", seams), path) ? "read-write"
|
|
509
|
+
: policy.permissions.outsideProject;
|
|
510
|
+
if (!canWrite(mode))
|
|
511
|
+
return { allowed: false, path, reason: "permission-denied" };
|
|
512
|
+
}
|
|
513
|
+
return { allowed: true, path };
|
|
514
|
+
}
|
|
515
|
+
function evaluateBroadWrite(path, policy, broad, seams) {
|
|
516
|
+
for (const denied of [...policy.denyWrite, ...broad.codePaths]) {
|
|
517
|
+
if (contains(denied, path))
|
|
518
|
+
return { allowed: false, path, reason: "write-denied", deniedBy: denied };
|
|
519
|
+
}
|
|
520
|
+
if (isCredential(path, policy))
|
|
521
|
+
return canWrite(policy.permissions.storedCredentials)
|
|
522
|
+
? { allowed: true, path } : { allowed: false, path, reason: "permission-denied" };
|
|
523
|
+
if (policy.runtimeWrite?.some((root) => contains(root, path)))
|
|
524
|
+
return { allowed: true, path };
|
|
525
|
+
if (contains(policy.writableRoot, path)) {
|
|
526
|
+
return canWrite(policy.permissions.projectFiles)
|
|
527
|
+
? { allowed: true, path } : { allowed: false, path, reason: "permission-denied" };
|
|
528
|
+
}
|
|
529
|
+
if ([...broad.tempRoots, ...broad.deleteRoots, canonicalizePath("/dev", seams)].some((root) => contains(root, path))) {
|
|
530
|
+
return { allowed: true, path };
|
|
531
|
+
}
|
|
532
|
+
if (contains(broad.home, path)) {
|
|
533
|
+
// Linux cannot separate removal from writing, so its fallback keeps
|
|
534
|
+
// ordinary (non-dot) top-level home folders read-only instead.
|
|
535
|
+
if (currentPlatform(seams) === "linux" && !disposableHomePath(path, broad.home)) {
|
|
536
|
+
return { allowed: false, path, reason: "permission-denied" };
|
|
537
|
+
}
|
|
538
|
+
return { allowed: true, path };
|
|
539
|
+
}
|
|
540
|
+
return { allowed: false, path, reason: "permission-denied" };
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Decide whether `target` may be removed or renamed away (unlink, rmdir,
|
|
544
|
+
* rename of the source, or rename over an existing destination). Outside the
|
|
545
|
+
* broad-write profile removal follows write access.
|
|
546
|
+
*/
|
|
547
|
+
export function evaluateDeleteAccess(target, policy, seams = {}) {
|
|
548
|
+
const write = evaluateWriteAccess(target, policy, seams);
|
|
549
|
+
const broad = policy.broad;
|
|
550
|
+
if (!write.allowed)
|
|
551
|
+
return write;
|
|
552
|
+
const path = write.path;
|
|
553
|
+
if (!broad) {
|
|
554
|
+
// Project files = Write outside the broad profile: nothing in the
|
|
555
|
+
// project is removable except worktree folders' contents.
|
|
556
|
+
return policy.permissions?.projectFiles === "write" && contains(policy.writableRoot, path) &&
|
|
557
|
+
!insideWorktreeFolder(path, policy.writableRoot)
|
|
558
|
+
? { allowed: false, path, reason: "delete-denied" } : write;
|
|
559
|
+
}
|
|
560
|
+
const anchors = broadProtectedPaths(policy, broad);
|
|
561
|
+
if (anchors.some((protectedPath) => protectedPath !== path && contains(path, protectedPath))) {
|
|
562
|
+
return { allowed: false, path, reason: "delete-denied" };
|
|
563
|
+
}
|
|
564
|
+
if (path === policy.writableRoot || contains(path, policy.writableRoot)) {
|
|
565
|
+
return { allowed: false, path, reason: "delete-denied" };
|
|
566
|
+
}
|
|
567
|
+
if ([...broad.tempRoots, ...broad.deleteRoots].some((root) => contains(root, path)) ||
|
|
568
|
+
disposableHomePath(path, broad.home)) {
|
|
569
|
+
return write;
|
|
570
|
+
}
|
|
571
|
+
return { allowed: false, path, reason: "delete-denied" };
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Whether an already-canonical path lies where a Write level still allows
|
|
575
|
+
* removal: temp, hidden home entries, worktree folders, a Write & delete
|
|
576
|
+
* workspace and runtime scratch (broad profile), or a worktree folder inside a
|
|
577
|
+
* Project files = Write workspace. The leaf is not re-resolved, so a caller can
|
|
578
|
+
* ask about a symlink's own directory entry.
|
|
579
|
+
*/
|
|
580
|
+
export function isRemovableUnderWrite(path, policy) {
|
|
581
|
+
const broad = policy.broad;
|
|
582
|
+
if (broad) {
|
|
583
|
+
if (broadProtectedPaths(policy, broad).some((guard) => contains(guard, path) || contains(path, guard)))
|
|
584
|
+
return false;
|
|
585
|
+
return [...broad.tempRoots, ...broad.deleteRoots].some((root) => contains(root, path)) ||
|
|
586
|
+
disposableHomePath(path, broad.home);
|
|
587
|
+
}
|
|
588
|
+
return policy.permissions?.projectFiles === "write" && insideWorktreeFolder(path, policy.writableRoot);
|
|
589
|
+
}
|
|
590
|
+
let lastRecoverySnapshot = 0;
|
|
591
|
+
const RECOVERY_SNAPSHOT_INTERVAL_MS = 15 * 60 * 1000;
|
|
592
|
+
function runLocalSnapshot() {
|
|
593
|
+
return new Promise((resolveOutcome) => {
|
|
594
|
+
let output = "";
|
|
595
|
+
let settled = false;
|
|
596
|
+
const finish = (outcome) => {
|
|
597
|
+
if (settled)
|
|
598
|
+
return;
|
|
599
|
+
settled = true;
|
|
600
|
+
clearTimeout(timer);
|
|
601
|
+
resolveOutcome(outcome);
|
|
602
|
+
};
|
|
603
|
+
const child = spawn("/usr/bin/tmutil", ["localsnapshot"], {
|
|
604
|
+
env: { PATH: "/usr/bin:/bin", LANG: "C" }, stdio: ["ignore", "pipe", "pipe"],
|
|
605
|
+
});
|
|
606
|
+
const timer = setTimeout(() => { child.kill("SIGKILL"); finish({ ok: false, detail: "tmutil timed out after 30 s" }); }, 30_000);
|
|
607
|
+
timer.unref();
|
|
608
|
+
child.unref();
|
|
609
|
+
for (const stream of [child.stdout, child.stderr]) {
|
|
610
|
+
stream?.on("data", (chunk) => { output = (output + chunk.toString()).slice(-2048); });
|
|
611
|
+
stream?.unref?.();
|
|
612
|
+
}
|
|
613
|
+
child.on("error", (error) => finish({ ok: false, detail: error.message }));
|
|
614
|
+
child.on("close", (code) => finish(code === 0
|
|
615
|
+
? { ok: true, detail: output.trim() }
|
|
616
|
+
: { ok: false, detail: output.trim() || `tmutil exited ${code}` }));
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Start an APFS local snapshot (`tmutil localsnapshot`) before a confined run
|
|
621
|
+
* whose Outside project level lets it change files outside the project: Write
|
|
622
|
+
* & delete can remove them, and Write can still truncate, chmod or chflags
|
|
623
|
+
* them in place. macOS only; it needs no elevated privileges. The snapshot
|
|
624
|
+
* runs in the background and never blocks or fails the run; callers log a
|
|
625
|
+
* failed outcome. Snapshots are volume-wide and purgeable, so one per 15
|
|
626
|
+
* minutes per process is enough. `PI_SANDBOX_RECOVERY_SNAPSHOT=off` disables it
|
|
627
|
+
* (test suites set it so they do not create real snapshots).
|
|
628
|
+
*/
|
|
629
|
+
export function takeRecoverySnapshot(permissions, seams = {}) {
|
|
630
|
+
if (!canWrite(permissions?.outsideProject) || process.env.PI_SANDBOX_RECOVERY_SNAPSHOT === "off") {
|
|
631
|
+
return { started: false, reason: "not-needed" };
|
|
632
|
+
}
|
|
633
|
+
if ((seams.platform ?? osPlatform)() !== "darwin")
|
|
634
|
+
return { started: false, reason: "unsupported" };
|
|
635
|
+
const now = (seams.now ?? Date.now)();
|
|
636
|
+
if (lastRecoverySnapshot && now - lastRecoverySnapshot < RECOVERY_SNAPSHOT_INTERVAL_MS) {
|
|
637
|
+
return { started: false, reason: "rate-limited" };
|
|
638
|
+
}
|
|
639
|
+
lastRecoverySnapshot = now;
|
|
640
|
+
let done;
|
|
641
|
+
try {
|
|
642
|
+
done = (seams.run ?? runLocalSnapshot)().catch((error) => ({ ok: false, detail: error instanceof Error ? error.message : String(error) }));
|
|
643
|
+
}
|
|
644
|
+
catch (error) {
|
|
645
|
+
done = Promise.resolve({ ok: false, detail: error instanceof Error ? error.message : String(error) });
|
|
646
|
+
}
|
|
647
|
+
return { started: true, done };
|
|
648
|
+
}
|
|
649
|
+
/** Test hook: forget the last snapshot time. */
|
|
650
|
+
export function resetRecoverySnapshotClock() {
|
|
651
|
+
lastRecoverySnapshot = 0;
|
|
652
|
+
}
|
|
653
|
+
function broadProtectedPaths(policy, broad) {
|
|
654
|
+
return [...policy.denyWrite,
|
|
655
|
+
...(policy.permissions?.storedCredentials === "read-write" ? [] : policy.credentialPaths ?? []), ...broad.codePaths,
|
|
656
|
+
...(!canWrite(policy.permissions?.projectFiles) ? [policy.writableRoot] : [])];
|
|
657
|
+
}
|
|
658
|
+
/** Quote a path as an SBPL string literal. */
|
|
659
|
+
function sbpl(path) {
|
|
660
|
+
return `"${path.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
661
|
+
}
|
|
662
|
+
function validateRuntimeHome(policy, seams) {
|
|
663
|
+
if (policy.permissions?.outsideProject !== "off")
|
|
664
|
+
return;
|
|
665
|
+
const home = canonicalizePath(policy.home, seams);
|
|
666
|
+
if (RUNTIME_ROOTS.some((root) => contains(canonicalizePath(root, seams), home))) {
|
|
667
|
+
throw new Error("outsideProject=off cannot expose a home under a required system runtime root; move the home or use another permission mode.");
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
function protectedAncestors(paths) {
|
|
671
|
+
const parents = new Set();
|
|
672
|
+
for (const path of paths) {
|
|
673
|
+
for (let parent = dirname(path); dirname(parent) !== parent; parent = dirname(parent))
|
|
674
|
+
parents.add(parent);
|
|
675
|
+
}
|
|
676
|
+
return [...parents].sort((a, b) => a.length - b.length);
|
|
677
|
+
}
|
|
678
|
+
function buildPermissionProfile(policy, seams) {
|
|
679
|
+
validateRuntimeHome(policy, seams);
|
|
680
|
+
const permissions = policy.permissions;
|
|
681
|
+
const protectedPaths = [
|
|
682
|
+
...policy.denyWrite,
|
|
683
|
+
...(permissions.projectFiles !== "read-write" ? [policy.writableRoot] : []),
|
|
684
|
+
...(permissions.storedCredentials !== "read-write" ? policy.credentialPaths ?? [] : []),
|
|
685
|
+
...(policy.compatibilityWrite ?? []),
|
|
686
|
+
];
|
|
687
|
+
if (permissions.outsideProject === "write")
|
|
688
|
+
throw new Error("Outside project = Write uses the broad-write profile.");
|
|
689
|
+
const rules = ["(version 1)", "(allow default)", "(deny file-write*)"];
|
|
690
|
+
if (permissions.outsideProject === "off") {
|
|
691
|
+
rules.push("(deny file-read*)", "(allow file-read-metadata)", '(allow file-read* (literal "/"))');
|
|
692
|
+
for (const root of runtimeRoots(seams)) {
|
|
693
|
+
rules.push(`(allow file-read* (subpath ${sbpl(root)}))`);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
if (permissions.outsideProject === "read-write")
|
|
697
|
+
rules.push("(allow file-write*)");
|
|
698
|
+
// Temporary host paths are not granted for off/read: doing so would expose
|
|
699
|
+
// other users' files in temp. /dev remains necessary for basic shell I/O.
|
|
700
|
+
for (const root of TEMP_ROOTS.filter((path) => permissions.outsideProject === "read-write" || path === "/dev")
|
|
701
|
+
.map((path) => canonicalizePath(path, seams))) {
|
|
702
|
+
rules.push(`(allow file-write* (subpath ${sbpl(root)}))`);
|
|
703
|
+
}
|
|
704
|
+
const scoped = (root, mode) => {
|
|
705
|
+
if (mode === "off")
|
|
706
|
+
rules.push(`(deny file-read* (subpath ${sbpl(root)}))`);
|
|
707
|
+
else
|
|
708
|
+
rules.push(`(allow file-read* (subpath ${sbpl(root)}))`);
|
|
709
|
+
if (canWrite(mode))
|
|
710
|
+
rules.push(`(allow file-write* (subpath ${sbpl(root)}))`);
|
|
711
|
+
else
|
|
712
|
+
rules.push(`(deny file-write* (subpath ${sbpl(root)}))`);
|
|
713
|
+
};
|
|
714
|
+
// Last matching SBPL rule wins. Credential rules override project and outside;
|
|
715
|
+
// explicit denyWrite entries always override every write allowance.
|
|
716
|
+
for (const path of policy.compatibilityWrite ?? [])
|
|
717
|
+
scoped(path, "read-write");
|
|
718
|
+
scoped(policy.writableRoot, permissions.projectFiles);
|
|
719
|
+
if (permissions.projectFiles === "write" && !TEMP_ROOTS.filter((root) => root !== "/dev")
|
|
720
|
+
.some((root) => contains(canonicalizePath(root, seams), policy.writableRoot))) {
|
|
721
|
+
// Write without delete: only a worktree folder's contents stay removable.
|
|
722
|
+
const project = sbplRegexPath(policy.writableRoot);
|
|
723
|
+
rules.push(`(deny file-write-unlink (subpath ${sbpl(policy.writableRoot)}))`, `(allow file-write-unlink (regex #"^${project}/(.+/)?[.]worktrees/."))`, `(allow file-write-unlink (regex #"^${project}/(.+/)?[^/]+-worktrees/."))`);
|
|
724
|
+
}
|
|
725
|
+
for (const path of (policy.runtimeWrite ?? []).filter((path) => !policy.compatibilityWrite?.includes(path)))
|
|
726
|
+
scoped(path, "read-write");
|
|
727
|
+
for (const path of policy.credentialPaths ?? [])
|
|
728
|
+
scoped(path, permissions.storedCredentials);
|
|
729
|
+
// Explicit unlink denials too: a `file-write-unlink` allowance above would
|
|
730
|
+
// otherwise outrank a `file-write*` denial.
|
|
731
|
+
for (const path of policy.denyWrite) {
|
|
732
|
+
rules.push(`(deny file-write* (subpath ${sbpl(path)}))`);
|
|
733
|
+
if (permissions.projectFiles === "write")
|
|
734
|
+
rules.push(`(deny file-write-unlink (subpath ${sbpl(path)}))`);
|
|
735
|
+
}
|
|
736
|
+
// Protect the directory entries, not their contents: unrelated children can
|
|
737
|
+
// still be created, while renaming a parent cannot move a denied subtree.
|
|
738
|
+
for (const path of protectedAncestors(protectedPaths))
|
|
739
|
+
rules.push(`(deny file-write-unlink (literal ${sbpl(path)}))`);
|
|
740
|
+
for (const path of policy.compatibilityWrite ?? [])
|
|
741
|
+
rules.push(`(deny file-write-unlink (literal ${sbpl(path)}))`);
|
|
742
|
+
if (!permissions.network)
|
|
743
|
+
rules.push("(deny network*)");
|
|
744
|
+
return [...rules, ""].join("\n");
|
|
745
|
+
}
|
|
746
|
+
/** Escape a canonical path for an SBPL regular expression literal. */
|
|
747
|
+
function sbplRegexPath(path) {
|
|
748
|
+
return path.replace(/[\\.^$|?*+()[\]{}"]/g, (char) => `\\${char}`);
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* The broad-write profile on Seatbelt.
|
|
752
|
+
*
|
|
753
|
+
* Seatbelt has no separate rename operation: `file-write-unlink` guards unlink,
|
|
754
|
+
* rmdir, the source of a rename, and an existing destination a rename replaces.
|
|
755
|
+
* Denying it outside the disposable roots therefore refuses `rm`, `rm -rf` and
|
|
756
|
+
* `mv dir elsewhere`, and also a save that renames a temp file over an existing
|
|
757
|
+
* file there. Writing a file in place (truncate + write) stays allowed.
|
|
758
|
+
*/
|
|
759
|
+
function buildBroadProfile(policy, seams) {
|
|
760
|
+
const permissions = policy.permissions;
|
|
761
|
+
const broad = policy.broad;
|
|
762
|
+
const rules = ["(version 1)", "(allow default)", "(deny file-write*)",
|
|
763
|
+
`(allow file-write* (subpath ${sbpl(broad.home)}))`];
|
|
764
|
+
const project = policy.writableRoot;
|
|
765
|
+
for (const root of [...broad.tempRoots, canonicalizePath("/dev", seams), ...broad.deleteRoots,
|
|
766
|
+
...(policy.runtimeWrite ?? []), ...(canWrite(permissions.projectFiles) ? [project] : [])]) {
|
|
767
|
+
rules.push(`(allow file-write* (subpath ${sbpl(root)}))`);
|
|
768
|
+
}
|
|
769
|
+
if (permissions.projectFiles === "off")
|
|
770
|
+
rules.push(`(deny file-read* (subpath ${sbpl(project)}))`);
|
|
771
|
+
rules.push("(deny file-write-unlink)");
|
|
772
|
+
for (const root of [...broad.tempRoots, ...broad.deleteRoots]) {
|
|
773
|
+
rules.push(`(allow file-write-unlink (subpath ${sbpl(root)}))`);
|
|
774
|
+
}
|
|
775
|
+
const home = sbplRegexPath(broad.home);
|
|
776
|
+
rules.push(`(allow file-write-unlink (regex #"^${home}/[.][^/]+$"))`, `(allow file-write-unlink (regex #"^${home}/[.][^/]+/"))`, `(allow file-write-unlink (regex #"^${home}/(.+/)?[.]worktrees/."))`, `(allow file-write-unlink (regex #"^${home}/(.+/)?[^/]+-worktrees/."))`);
|
|
777
|
+
// Write-denied controls and credentials override every broad grant above. A rule naming
|
|
778
|
+
// `file-write-unlink` outranks a `file-write*` wildcard whatever their
|
|
779
|
+
// order (verified on the real kernel), so each protected path also gets an
|
|
780
|
+
// explicit unlink denial; otherwise the removal grants above would reopen it.
|
|
781
|
+
const denyWrite = (path) => rules.push(`(deny file-write* (subpath ${sbpl(path)}))`, `(deny file-write-unlink (subpath ${sbpl(path)}))`);
|
|
782
|
+
if (!canWrite(permissions.projectFiles))
|
|
783
|
+
denyWrite(project);
|
|
784
|
+
for (const path of broad.codePaths)
|
|
785
|
+
denyWrite(path);
|
|
786
|
+
for (const path of policy.credentialPaths ?? []) {
|
|
787
|
+
if (permissions.storedCredentials === "off")
|
|
788
|
+
rules.push(`(deny file-read* (subpath ${sbpl(path)}))`);
|
|
789
|
+
if (permissions.storedCredentials !== "read-write")
|
|
790
|
+
denyWrite(path);
|
|
791
|
+
}
|
|
792
|
+
for (const path of policy.denyWrite)
|
|
793
|
+
denyWrite(path);
|
|
794
|
+
// Renaming an ancestor would move a protected subtree to an unprotected path,
|
|
795
|
+
// and renaming the workspace's ancestors could redirect the next launch.
|
|
796
|
+
const anchors = [...broadProtectedPaths(policy, broad), project];
|
|
797
|
+
for (const path of [...new Set([...protectedAncestors(anchors), project])]) {
|
|
798
|
+
rules.push(`(deny file-write-unlink (literal ${sbpl(path)}))`);
|
|
799
|
+
}
|
|
800
|
+
if (!permissions.network)
|
|
801
|
+
rules.push("(deny network*)");
|
|
802
|
+
return [...rules, ""].join("\n");
|
|
803
|
+
}
|
|
804
|
+
/** Build the macOS sandbox-exec wrapper and its SBPL profile. */
|
|
805
|
+
function buildMacOSSandboxCommand(args, seams) {
|
|
806
|
+
// Match on the real (symlink-resolved) path — sandbox-exec evaluates the
|
|
807
|
+
// canonical path, so /tmp/x must be written as /private/tmp/x.
|
|
808
|
+
const policy = compile(args.policy, seams, false);
|
|
809
|
+
const profile = policy.broad ? buildBroadProfile(policy, seams)
|
|
810
|
+
: policy.permissions ? buildPermissionProfile(policy, seams) : [
|
|
811
|
+
"(version 1)",
|
|
812
|
+
"(allow default)", // permissive base: reads, exec, network
|
|
813
|
+
"(deny file-write*)", // ...then deny all writes...
|
|
814
|
+
`(allow file-write* (subpath ${sbpl(policy.writableRoot)}))`, // ...except here
|
|
815
|
+
`(allow file-write* (subpath ${sbpl(`${policy.home}/.pi`)}))`, // pi state
|
|
816
|
+
'(allow file-write* (subpath "/private/var/folders"))', // macOS temp / our runtime
|
|
817
|
+
'(allow file-write* (subpath "/private/tmp"))',
|
|
818
|
+
'(allow file-write* (subpath "/dev"))', // /dev/null etc.
|
|
819
|
+
// Deny rules come last: SBPL applies the last matching rule, so these
|
|
820
|
+
// carve holes back out of the allowances above.
|
|
821
|
+
...policy.denyWrite.map((path) => `(deny file-write* (subpath ${sbpl(path)}))`),
|
|
822
|
+
"",
|
|
823
|
+
].join("\n");
|
|
824
|
+
(seams.writeProfile ?? writeFileSync)(args.profilePath, profile);
|
|
825
|
+
return {
|
|
826
|
+
file: MACOS_SANDBOX_EXEC,
|
|
827
|
+
fileArgs: ["-f", args.profilePath, args.execPath, ...args.execArgs],
|
|
828
|
+
};
|
|
829
|
+
}
|
|
830
|
+
const macOSSandboxBackend = {
|
|
831
|
+
id: "macos-seatbelt",
|
|
832
|
+
executable: MACOS_SANDBOX_EXEC,
|
|
833
|
+
buildCommand: buildMacOSSandboxCommand,
|
|
834
|
+
};
|
|
835
|
+
/** Resolve only a root-owned system executable; never inspect task PATH. */
|
|
836
|
+
function systemSandboxExecutable(name) {
|
|
837
|
+
// Never resolve the host-side confinement launcher through task-influenced PATH.
|
|
838
|
+
for (const directory of ["/usr/bin", "/bin"]) {
|
|
839
|
+
const candidate = join(directory, name);
|
|
840
|
+
try {
|
|
841
|
+
const info = statSync(candidate);
|
|
842
|
+
if (!info.isFile() || info.uid !== 0 || (info.mode & 0o022) !== 0)
|
|
843
|
+
continue;
|
|
844
|
+
accessSync(candidate, constants.X_OK);
|
|
845
|
+
return candidate;
|
|
846
|
+
}
|
|
847
|
+
catch { /* Try the next system location. */ }
|
|
848
|
+
}
|
|
849
|
+
return undefined;
|
|
850
|
+
}
|
|
851
|
+
export function executableFromPath(name) {
|
|
852
|
+
const path = process.env.PATH;
|
|
853
|
+
if (!path)
|
|
854
|
+
return undefined;
|
|
855
|
+
for (const entry of path.split(delimiter)) {
|
|
856
|
+
const candidate = resolve(entry || ".", name);
|
|
857
|
+
try {
|
|
858
|
+
if (!statSync(candidate).isFile())
|
|
859
|
+
continue;
|
|
860
|
+
accessSync(candidate, constants.X_OK);
|
|
861
|
+
return candidate;
|
|
862
|
+
}
|
|
863
|
+
catch {
|
|
864
|
+
// A PATH entry may disappear or be inaccessible between lookup and use.
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
return undefined;
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Give an absent denied path something the kernel can hold out.
|
|
871
|
+
*
|
|
872
|
+
* A mount needs a mount point. `--ro-bind-try` skips a source that does not
|
|
873
|
+
* exist, so before this a denied path that had not been created yet was not
|
|
874
|
+
* denied at all: inside the sandbox `echo secret > .env.local` simply created
|
|
875
|
+
* it. `.env` and `.env.local` are absent in most projects, which made the
|
|
876
|
+
* packaged defaults hold on macOS — SBPL denies by resolved path, existing or
|
|
877
|
+
* not — and not on Linux.
|
|
878
|
+
*
|
|
879
|
+
* Nothing bubblewrap offers closes that without a mount point, and every
|
|
880
|
+
* bubblewrap operation that would create one (`--dir`, `--file`, `--tmpfs`)
|
|
881
|
+
* creates it through the read-write bind of the project, which is to say on the
|
|
882
|
+
* real filesystem anyway. So the placeholder is created here, deliberately and
|
|
883
|
+
* visibly, rather than as a side effect of a mount operation.
|
|
884
|
+
*
|
|
885
|
+
* An empty regular file is the least destructive placeholder: a later
|
|
886
|
+
* `cp .env.example .env` overwrites it, where an empty *directory* at that path
|
|
887
|
+
* would fail. Callers whose denied entry has to be a directory create it before
|
|
888
|
+
* building the command, and an entry that already exists — file or directory —
|
|
889
|
+
* is bound as it is. The file is created with O_EXCL, so anything that appears
|
|
890
|
+
* in the meantime is bound rather than clobbered, and it is left in place
|
|
891
|
+
* afterwards because a resumed task re-runs the launch vector it captured and
|
|
892
|
+
* its `--ro-bind` sources have to still be there.
|
|
893
|
+
*
|
|
894
|
+
* Returns false when the placeholder could not be created. Callers must fail
|
|
895
|
+
* closed for writable regions: a task may replace or chmod the blocking parent.
|
|
896
|
+
*/
|
|
897
|
+
function materializeDenyPath(path) {
|
|
898
|
+
if (existsSync(path))
|
|
899
|
+
return true;
|
|
900
|
+
try {
|
|
901
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
902
|
+
closeSync(openSync(path, "wx"));
|
|
903
|
+
return true;
|
|
904
|
+
}
|
|
905
|
+
catch {
|
|
906
|
+
// Re-check rather than trust the errno. EEXIST from the O_EXCL create
|
|
907
|
+
// means the path appeared in between, which is the outcome we wanted and
|
|
908
|
+
// not ours to overwrite; EEXIST from the mkdir means a parent is a
|
|
909
|
+
// regular file, and nothing can exist under it. Only the first leaves a
|
|
910
|
+
// source a bind can use.
|
|
911
|
+
return existsSync(path);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Whether a denied path lies in a region this backend binds read-write.
|
|
916
|
+
*
|
|
917
|
+
* Everywhere else is already covered by the read-only bind of `/`, so a
|
|
918
|
+
* placeholder there would deny nothing that is not denied already — and would
|
|
919
|
+
* scatter empty files across the host for the sake of it. Materializing is
|
|
920
|
+
* confined to the two regions that are genuinely writable inside the sandbox:
|
|
921
|
+
* the writable root, and the `/tmp` rebind that pi's own tooling needs.
|
|
922
|
+
*/
|
|
923
|
+
function writableInsideLinuxSandbox(path, writableRoot) {
|
|
924
|
+
return contains(writableRoot, path) || contains("/tmp", path);
|
|
925
|
+
}
|
|
926
|
+
function buildLinuxSandboxCommand(bwrap, args, seams) {
|
|
927
|
+
// The caller creates the selected work directory before it reaches this
|
|
928
|
+
// boundary. Canonicalizing it before bind-mounting keeps symlink aliases from
|
|
929
|
+
// widening the writable root.
|
|
930
|
+
const policy = compile(args.policy, seams, true);
|
|
931
|
+
if (policy.broad)
|
|
932
|
+
return buildLinuxBroadCommand(bwrap, args, policy, seams);
|
|
933
|
+
if (policy.permissions)
|
|
934
|
+
return buildLinuxPermissionCommand(bwrap, args, policy, seams);
|
|
935
|
+
const materialize = seams.materializeDenyPath ?? materializeDenyPath;
|
|
936
|
+
const denyBinds = policy.denyWrite.flatMap((path) => {
|
|
937
|
+
const writable = writableInsideLinuxSandbox(path, policy.writableRoot);
|
|
938
|
+
if (writable && !materialize(path))
|
|
939
|
+
throw new Error(`Cannot protect denied path: ${path}`);
|
|
940
|
+
return [writable ? "--ro-bind" : "--ro-bind-try", path, path];
|
|
941
|
+
});
|
|
942
|
+
return {
|
|
943
|
+
file: bwrap,
|
|
944
|
+
fileArgs: [
|
|
945
|
+
"--ro-bind", "/", "/",
|
|
946
|
+
"--bind", policy.writableRoot, policy.writableRoot,
|
|
947
|
+
"--bind", "/tmp", "/tmp",
|
|
948
|
+
"--dev", "/dev",
|
|
949
|
+
// Layered last so a denied path wins over every writable bind above.
|
|
950
|
+
// A denied path need not exist yet, so one inside a writable region
|
|
951
|
+
// is materialized first; `-try` remains for the paths that are
|
|
952
|
+
// read-only regardless. A writable region whose guard cannot be
|
|
953
|
+
// materialized must fail before launching the task.
|
|
954
|
+
...denyBinds,
|
|
955
|
+
"--",
|
|
956
|
+
args.execPath, ...args.execArgs,
|
|
957
|
+
],
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
function buildLinuxPermissionCommand(bwrap, args, policy, seams) {
|
|
961
|
+
const permissions = policy.permissions;
|
|
962
|
+
validateRuntimeHome(policy, seams);
|
|
963
|
+
if (permissions.projectFiles === "write" || permissions.outsideProject === "write") {
|
|
964
|
+
throw new Error("Linux bubblewrap cannot separate removal from writing for Project files; use Write & delete or Read.");
|
|
965
|
+
}
|
|
966
|
+
const project = policy.writableRoot;
|
|
967
|
+
const credentials = policy.credentialPaths ?? [];
|
|
968
|
+
const writableProject = permissions.projectFiles === "read-write";
|
|
969
|
+
const overlappingCredentials = credentials.filter((path) => contains(project, path));
|
|
970
|
+
if (credentials.some((path) => contains(path, project)) &&
|
|
971
|
+
permissions.projectFiles !== permissions.storedCredentials) {
|
|
972
|
+
throw new Error("Linux bubblewrap cannot apply differing project and credential permissions when a credential directory contains the project.");
|
|
973
|
+
}
|
|
974
|
+
if (writableProject && policy.denyWrite.some((path) => contains(path, project))) {
|
|
975
|
+
throw new Error("Linux bubblewrap cannot make a project writable inside a write-denied directory.");
|
|
976
|
+
}
|
|
977
|
+
// Protected leaves and their writable ancestors become mount points below.
|
|
978
|
+
// Linux refuses renaming mount points, preventing ancestor replacement.
|
|
979
|
+
if (permissions.outsideProject === "read-write" && (permissions.projectFiles !== "read-write" || permissions.storedCredentials !== "read-write" ||
|
|
980
|
+
policy.denyWrite.length > 0)) {
|
|
981
|
+
throw new Error("Linux bubblewrap cannot enforce restricted project/credential/denyWrite paths under a writable outsideProject mount.");
|
|
982
|
+
}
|
|
983
|
+
if (permissions.outsideProject === "read" && permissions.storedCredentials === "read-write" &&
|
|
984
|
+
credentials.some((path) => !contains(project, path))) {
|
|
985
|
+
throw new Error("Linux bubblewrap cannot write credential stores under a read-only outsideProject root.");
|
|
986
|
+
}
|
|
987
|
+
if (permissions.projectFiles === "read" && permissions.storedCredentials === "read-write" &&
|
|
988
|
+
overlappingCredentials.length > 0) {
|
|
989
|
+
throw new Error("Linux bubblewrap cannot write credential stores under a read-only project mount.");
|
|
990
|
+
}
|
|
991
|
+
if (permissions.outsideProject === "read" && permissions.storedCredentials === "off") {
|
|
992
|
+
throw new Error("Linux bubblewrap cannot hide stored credentials in a read-only whole-root bind.");
|
|
993
|
+
}
|
|
994
|
+
if (permissions.outsideProject === "off" && permissions.storedCredentials === "off" &&
|
|
995
|
+
credentials.some((path) => contains(project, path) && permissions.projectFiles !== "off")) {
|
|
996
|
+
throw new Error("Linux bubblewrap cannot hide credentials inside a visible read-only project.");
|
|
997
|
+
}
|
|
998
|
+
if (permissions.outsideProject === "off" && permissions.projectFiles === "off" &&
|
|
999
|
+
RUNTIME_ROOTS.some((root) => contains(canonicalizePath(root, seams), project))) {
|
|
1000
|
+
throw new Error("Linux bubblewrap cannot hide a project nested under a required system runtime bind.");
|
|
1001
|
+
}
|
|
1002
|
+
if (permissions.outsideProject === "off" && credentials.some((path) => !contains(project, path) &&
|
|
1003
|
+
RUNTIME_ROOTS.some((root) => contains(canonicalizePath(root, seams), path)) &&
|
|
1004
|
+
permissions.storedCredentials === "off")) {
|
|
1005
|
+
throw new Error("Linux bubblewrap cannot hide credentials under a required system runtime bind.");
|
|
1006
|
+
}
|
|
1007
|
+
if (permissions.outsideProject === "off" && permissions.storedCredentials === "read-write" &&
|
|
1008
|
+
credentials.some((path) => !contains(project, path))) {
|
|
1009
|
+
throw new Error("Linux bubblewrap cannot create or safely bind writable credential stores outside a hidden root.");
|
|
1010
|
+
}
|
|
1011
|
+
if (permissions.outsideProject === "off" && permissions.projectFiles === "off" &&
|
|
1012
|
+
permissions.storedCredentials !== "off" && credentials.some((path) => contains(project, path))) {
|
|
1013
|
+
throw new Error("Linux bubblewrap cannot expose credentials inside a hidden project without exposing the project.");
|
|
1014
|
+
}
|
|
1015
|
+
if (permissions.outsideProject !== "off" && permissions.projectFiles === "off") {
|
|
1016
|
+
throw new Error("Linux bubblewrap cannot hide a project inside a visible outsideProject root.");
|
|
1017
|
+
}
|
|
1018
|
+
const mounts = permissions.outsideProject === "off" ? ["--tmpfs", "/"]
|
|
1019
|
+
: [permissions.outsideProject === "read" ? "--ro-bind" : "--bind", "/", "/"];
|
|
1020
|
+
if (permissions.outsideProject === "off") {
|
|
1021
|
+
// Bounded system executable/library roots only. /tmp is private, not a
|
|
1022
|
+
// host bind: otherwise outsideProject=off would expose user temp data.
|
|
1023
|
+
for (const root of RUNTIME_ROOTS) {
|
|
1024
|
+
if (existsSync(root))
|
|
1025
|
+
mounts.push("--ro-bind", root, root);
|
|
1026
|
+
}
|
|
1027
|
+
mounts.push("--tmpfs", "/tmp");
|
|
1028
|
+
if (args.internalHelperExecutable) {
|
|
1029
|
+
const executable = canonicalizePath(args.execPath, seams);
|
|
1030
|
+
if (!RUNTIME_ROOTS.some((root) => contains(canonicalizePath(root, seams), executable))) {
|
|
1031
|
+
mounts.push("--ro-bind", executable, executable);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
else {
|
|
1036
|
+
mounts.push(permissions.outsideProject === "read" && !policy.compatibilityWrite?.includes(canonicalizePath("/tmp", seams)) ? "--ro-bind" : "--bind", "/tmp", "/tmp");
|
|
1037
|
+
}
|
|
1038
|
+
mounts.push("--dev", "/dev");
|
|
1039
|
+
const scopedMounts = [];
|
|
1040
|
+
const writableAncestors = new Set();
|
|
1041
|
+
const readOnlyGuards = new Set();
|
|
1042
|
+
if (permissions.outsideProject === "off" && permissions.storedCredentials === "read") {
|
|
1043
|
+
for (const path of credentials) {
|
|
1044
|
+
if (existsSync(path) && !contains(project, path))
|
|
1045
|
+
readOnlyGuards.add(path);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
// A broad /tmp bind is already present for visible outside roots. Cover
|
|
1049
|
+
// existing leaves and absent leaves via a read-only existing ancestor; do
|
|
1050
|
+
// not create credential or control files in host runtime directories.
|
|
1051
|
+
const compatibility = policy.compatibilityWrite ?? [];
|
|
1052
|
+
if (compatibility.length) {
|
|
1053
|
+
const tmp = canonicalizePath("/tmp", seams);
|
|
1054
|
+
const protectedInTmp = [...policy.denyWrite, ...(permissions.storedCredentials !== "read-write" ? credentials : [])]
|
|
1055
|
+
.filter((path) => contains(tmp, path) && !contains(project, path));
|
|
1056
|
+
if (permissions.storedCredentials === "off" && protectedInTmp.some((path) => credentials.some((credential) => credential === path))) {
|
|
1057
|
+
throw new Error("Linux bubblewrap cannot hide credentials under a writable runtime directory.");
|
|
1058
|
+
}
|
|
1059
|
+
if (policy.denyWrite.some((path) => contains(path, tmp))) {
|
|
1060
|
+
throw new Error("Runtime directory overlaps a write-denied control path.");
|
|
1061
|
+
}
|
|
1062
|
+
const guards = new Set();
|
|
1063
|
+
for (const path of protectedInTmp) {
|
|
1064
|
+
let guard = path;
|
|
1065
|
+
while (!existsSync(guard) && contains(tmp, guard) && guard !== tmp)
|
|
1066
|
+
guard = dirname(guard);
|
|
1067
|
+
if (guard === tmp || !contains(tmp, guard)) {
|
|
1068
|
+
throw new Error("Cannot protect an absent path directly under writable runtime directory.");
|
|
1069
|
+
}
|
|
1070
|
+
guards.add(guard);
|
|
1071
|
+
}
|
|
1072
|
+
for (const guard of [...guards].sort((a, b) => a.length - b.length)) {
|
|
1073
|
+
if ([...guards].some((parent) => parent !== guard && contains(parent, guard)))
|
|
1074
|
+
continue;
|
|
1075
|
+
for (const parent of protectedAncestors([guard]).filter((parent) => contains(tmp, parent) && parent !== tmp)) {
|
|
1076
|
+
writableAncestors.add(parent);
|
|
1077
|
+
}
|
|
1078
|
+
readOnlyGuards.add(guard);
|
|
1079
|
+
}
|
|
1080
|
+
// A writable /tmp can rename any directory above the captured project
|
|
1081
|
+
// root, then substitute a symlink before the next launch. Anchor each
|
|
1082
|
+
// ancestor even when there are no explicit control or credential guards.
|
|
1083
|
+
if (permissions.projectFiles !== "off" && contains(tmp, project)) {
|
|
1084
|
+
for (const parent of protectedAncestors([project]).filter((path) => contains(tmp, path) && path !== tmp)) {
|
|
1085
|
+
writableAncestors.add(parent);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
if (permissions.projectFiles !== "off") {
|
|
1090
|
+
scopedMounts.push({ option: writableProject ? "--bind" : "--ro-bind", path: project });
|
|
1091
|
+
}
|
|
1092
|
+
for (const path of policy.runtimeWrite ?? []) {
|
|
1093
|
+
if (compatibility.includes(path))
|
|
1094
|
+
continue;
|
|
1095
|
+
if (credentials.some((protectedPath) => contains(path, protectedPath) || contains(protectedPath, path)) ||
|
|
1096
|
+
policy.denyWrite.some((protectedPath) => contains(protectedPath, path))) {
|
|
1097
|
+
throw new Error("Runtime directory overlaps protected credentials or control paths.");
|
|
1098
|
+
}
|
|
1099
|
+
scopedMounts.push({ option: "--bind", path });
|
|
1100
|
+
}
|
|
1101
|
+
const protectedPaths = [...policy.denyWrite,
|
|
1102
|
+
...(permissions.storedCredentials !== "read-write" ? overlappingCredentials : [])];
|
|
1103
|
+
for (const writableRoot of [...(writableProject ? [project] : []),
|
|
1104
|
+
...(policy.runtimeWrite ?? []).filter((path) => !compatibility.includes(path))]) {
|
|
1105
|
+
const materialize = seams.materializeDenyPath ?? materializeDenyPath;
|
|
1106
|
+
const leaves = protectedPaths.filter((path) => contains(writableRoot, path));
|
|
1107
|
+
for (const path of leaves) {
|
|
1108
|
+
if (!materialize(path))
|
|
1109
|
+
throw new Error(`Cannot protect denied path: ${path}`);
|
|
1110
|
+
}
|
|
1111
|
+
for (const parent of protectedAncestors(leaves).filter((path) => contains(writableRoot, path) && path !== writableRoot)) {
|
|
1112
|
+
writableAncestors.add(parent);
|
|
1113
|
+
}
|
|
1114
|
+
for (const path of leaves)
|
|
1115
|
+
readOnlyGuards.add(path);
|
|
1116
|
+
}
|
|
1117
|
+
// A nearest-existing guard can be an ancestor of an unrelated writable
|
|
1118
|
+
// project (e.g. absent ~/.npmrc with a project under ~/work). Mount that
|
|
1119
|
+
// broad guard in path order, keeping its intervening anchors read-only;
|
|
1120
|
+
// only the disjoint project subtree may be rebound writable afterwards.
|
|
1121
|
+
const ancestorGuards = [...readOnlyGuards].filter((guard) => scopedMounts.some(({ option, path }) => option === "--bind" && guard !== path && contains(guard, path)));
|
|
1122
|
+
for (const guard of ancestorGuards) {
|
|
1123
|
+
scopedMounts.push({ option: "--ro-bind", path: guard });
|
|
1124
|
+
readOnlyGuards.delete(guard);
|
|
1125
|
+
}
|
|
1126
|
+
for (const path of writableAncestors)
|
|
1127
|
+
scopedMounts.push({
|
|
1128
|
+
option: ancestorGuards.some((guard) => contains(guard, path)) ? "--ro-bind" : "--bind", path,
|
|
1129
|
+
});
|
|
1130
|
+
scopedMounts.sort((a, b) => a.path.length - b.path.length);
|
|
1131
|
+
for (const { option, path } of scopedMounts)
|
|
1132
|
+
mounts.push(option, path, path);
|
|
1133
|
+
for (const path of [...readOnlyGuards].sort((a, b) => a.length - b.length)) {
|
|
1134
|
+
mounts.push("--ro-bind", path, path);
|
|
1135
|
+
}
|
|
1136
|
+
return {
|
|
1137
|
+
file: bwrap,
|
|
1138
|
+
fileArgs: [...mounts, ...(!permissions.network ? ["--unshare-net"] : []),
|
|
1139
|
+
"--", args.execPath, ...args.execArgs],
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
function systemListDirectory(path) {
|
|
1143
|
+
try {
|
|
1144
|
+
return readdirSync(path, { withFileTypes: true }).map((entry) => ({
|
|
1145
|
+
name: entry.name, directory: entry.isDirectory(), symlink: entry.isSymbolicLink(),
|
|
1146
|
+
}));
|
|
1147
|
+
}
|
|
1148
|
+
catch {
|
|
1149
|
+
return [];
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Worktree folders (`.worktrees`, `*-worktrees`) within three levels of home.
|
|
1154
|
+
* Bounded so a large home cannot stall a launch; a folder created after launch
|
|
1155
|
+
* or deeper than that stays read-only on Linux.
|
|
1156
|
+
*/
|
|
1157
|
+
function discoverWorktreeFolders(home, list) {
|
|
1158
|
+
const found = [];
|
|
1159
|
+
let budget = 4000;
|
|
1160
|
+
const walk = (directory, depth) => {
|
|
1161
|
+
for (const entry of list(directory)) {
|
|
1162
|
+
if (--budget < 0 || !entry.directory || entry.symlink)
|
|
1163
|
+
continue;
|
|
1164
|
+
const path = join(directory, entry.name);
|
|
1165
|
+
if (entry.name === ".worktrees" || entry.name.endsWith("-worktrees"))
|
|
1166
|
+
found.push(path);
|
|
1167
|
+
else if (!entry.name.startsWith(".") && depth < 3)
|
|
1168
|
+
walk(path, depth + 1);
|
|
1169
|
+
}
|
|
1170
|
+
};
|
|
1171
|
+
walk(home, 1);
|
|
1172
|
+
return found;
|
|
1173
|
+
}
|
|
1174
|
+
/** Empty, mode-000 mask sources a hidden credential is bound over. */
|
|
1175
|
+
function maskSources(profilePath) {
|
|
1176
|
+
const base = join(dirname(profilePath), ".pi-sandbox-mask");
|
|
1177
|
+
mkdirSync(base, { recursive: true, mode: 0o700 });
|
|
1178
|
+
const directory = join(base, "directory");
|
|
1179
|
+
const file = join(base, "file");
|
|
1180
|
+
if (!existsSync(directory))
|
|
1181
|
+
mkdirSync(directory, { mode: 0o000 });
|
|
1182
|
+
if (!existsSync(file))
|
|
1183
|
+
closeSync(openSync(file, "wx", 0o000));
|
|
1184
|
+
for (const [path, isDirectory] of [[directory, true], [file, false]]) {
|
|
1185
|
+
const info = lstatSync(path);
|
|
1186
|
+
if (info.isDirectory() !== isDirectory || (info.mode & 0o777) !== 0 || (!isDirectory && info.size !== 0)) {
|
|
1187
|
+
throw new Error(`Unsafe sandbox mask source: ${path}`);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
return { directory, file };
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* The broad-write profile on Bubblewrap.
|
|
1194
|
+
*
|
|
1195
|
+
* Mount namespaces cannot separate removal from writing, and Landlock is not
|
|
1196
|
+
* reachable from Node or bwrap here, so Linux uses the documented fallback:
|
|
1197
|
+
* the root is read-only; temp, the workspace, hidden home entries and worktree
|
|
1198
|
+
* folders are writable (removal included); ordinary top-level home folders and
|
|
1199
|
+
* home itself stay read-only, so sibling repositories cannot even be edited in
|
|
1200
|
+
* place there. Off credentials are masked by empty mode-000 mounts, Read
|
|
1201
|
+
* credentials are read-only binds, and Read / write credentials follow the
|
|
1202
|
+
* surrounding broad-write rules. Code that runs later and caller-denied paths
|
|
1203
|
+
* are read-only binds. Every writable bind and protected leaf is a mount point,
|
|
1204
|
+
* which Linux refuses to rename. A protected
|
|
1205
|
+
* symlink inside a writable directory (a stow link in `~/.config`) makes that
|
|
1206
|
+
* directory read-only with each existing entry bound writable again, so only
|
|
1207
|
+
* new top-level entries there are refused.
|
|
1208
|
+
*/
|
|
1209
|
+
function buildLinuxBroadCommand(bwrap, args, policy, seams) {
|
|
1210
|
+
const permissions = policy.permissions;
|
|
1211
|
+
const broad = policy.broad;
|
|
1212
|
+
const project = policy.writableRoot;
|
|
1213
|
+
if (permissions.projectFiles === "off") {
|
|
1214
|
+
throw new Error("Linux bubblewrap cannot hide a project inside the broad-write profile's visible root.");
|
|
1215
|
+
}
|
|
1216
|
+
if (permissions.projectFiles === "write") {
|
|
1217
|
+
throw new Error("Linux bubblewrap cannot separate removal from writing for Project files; use Write & delete or Read.");
|
|
1218
|
+
}
|
|
1219
|
+
const credentials = policy.credentialPaths ?? [];
|
|
1220
|
+
const protectedCredentials = permissions.storedCredentials === "read-write" ? [] : credentials;
|
|
1221
|
+
const writeDenied = [...broad.codePaths, ...policy.denyWrite];
|
|
1222
|
+
if (permissions.projectFiles === "read-write" &&
|
|
1223
|
+
[...protectedCredentials, ...writeDenied].some((path) => path !== project && contains(path, project))) {
|
|
1224
|
+
throw new Error("Linux bubblewrap cannot make a project writable inside a protected directory.");
|
|
1225
|
+
}
|
|
1226
|
+
const list = seams.listDirectory ?? systemListDirectory;
|
|
1227
|
+
const exists = (path) => (seams.pathExists ?? existsSync)(path);
|
|
1228
|
+
const protectedPaths = [...protectedCredentials, ...writeDenied];
|
|
1229
|
+
const insideProtected = (path) => protectedPaths.some((guard) => contains(guard, path));
|
|
1230
|
+
const writable = new Set();
|
|
1231
|
+
for (const root of broad.tempRoots)
|
|
1232
|
+
if (exists(root))
|
|
1233
|
+
writable.add(root);
|
|
1234
|
+
for (const entry of list(broad.home)) {
|
|
1235
|
+
if (entry.symlink)
|
|
1236
|
+
continue;
|
|
1237
|
+
const path = join(broad.home, entry.name);
|
|
1238
|
+
if (entry.name.startsWith(".") && !insideProtected(path))
|
|
1239
|
+
writable.add(path);
|
|
1240
|
+
}
|
|
1241
|
+
for (const path of discoverWorktreeFolders(broad.home, list))
|
|
1242
|
+
if (!insideProtected(path))
|
|
1243
|
+
writable.add(path);
|
|
1244
|
+
for (const path of [...broad.deleteRoots, ...(policy.runtimeWrite ?? [])]) {
|
|
1245
|
+
if (path === project || !exists(path))
|
|
1246
|
+
continue;
|
|
1247
|
+
if (insideProtected(path))
|
|
1248
|
+
throw new Error(`Writable sandbox root overlaps a protected path: ${path}`);
|
|
1249
|
+
writable.add(path);
|
|
1250
|
+
}
|
|
1251
|
+
if (permissions.projectFiles === "read-write")
|
|
1252
|
+
writable.add(project);
|
|
1253
|
+
const writableRoots = [...writable];
|
|
1254
|
+
const writableAt = (path) => writableRoots.some((root) => contains(root, path));
|
|
1255
|
+
const mask = (seams.maskSources ?? maskSources)(args.profilePath);
|
|
1256
|
+
const materialize = seams.materializeDenyPath ?? materializeDenyPath;
|
|
1257
|
+
const hidden = [];
|
|
1258
|
+
const readOnly = new Set();
|
|
1259
|
+
const directoryLocation = (path) => [...DIRECTORY_LOCATIONS]
|
|
1260
|
+
.some((name) => canonicalizePath(join(broad.home, name), seams) === path);
|
|
1261
|
+
// The literal fixed-list entry (a dangling `~/.ssh` link) or its canonical path.
|
|
1262
|
+
const directoryEntry = (path) => directoryLocation(path) ||
|
|
1263
|
+
[...DIRECTORY_LOCATIONS].some((name) => join(broad.home, name) === path);
|
|
1264
|
+
// Directories holding a protected symlink: read-only, so the link cannot be
|
|
1265
|
+
// replaced, with their existing entries bound writable again below.
|
|
1266
|
+
const linkParents = new Set();
|
|
1267
|
+
// Where dangling links would land. Only the first missing component of each
|
|
1268
|
+
// needs a guard: nothing can be created below a read-only mount or a locked
|
|
1269
|
+
// parent. It is a directory when more of the path lies below it, or when the
|
|
1270
|
+
// link is a directory-type entry (`~/.ssh -> ~/work/ssh`).
|
|
1271
|
+
const firstMissing = new Map();
|
|
1272
|
+
const belowFirstMissing = new Set();
|
|
1273
|
+
for (const path of protectedPaths) {
|
|
1274
|
+
const unresolved = walkSymlinks(path).unresolved;
|
|
1275
|
+
if (!unresolved.length)
|
|
1276
|
+
continue;
|
|
1277
|
+
const first = unresolved[0];
|
|
1278
|
+
const full = unresolved.at(-1);
|
|
1279
|
+
firstMissing.set(first, firstMissing.get(first) === true || first !== full || directoryEntry(path));
|
|
1280
|
+
if (full !== first)
|
|
1281
|
+
belowFirstMissing.add(full);
|
|
1282
|
+
}
|
|
1283
|
+
const created = [];
|
|
1284
|
+
const makeDirectory = seams.makeDirectory ?? ((target) => mkdirSync(target, { recursive: true, mode: 0o700 }));
|
|
1285
|
+
const guard = (path, hide) => {
|
|
1286
|
+
let directory;
|
|
1287
|
+
let link = false;
|
|
1288
|
+
try {
|
|
1289
|
+
link = lstatSync(path).isSymbolicLink();
|
|
1290
|
+
}
|
|
1291
|
+
catch { /* absent or intermediate link */ }
|
|
1292
|
+
if (canonicalizePath(path, seams) !== path || (link && unresolvable(path))) {
|
|
1293
|
+
// A literal twin of a canonical deny entry: its target is guarded on
|
|
1294
|
+
// its own. A looping link resolves to nothing (ELOOP), and a dangling
|
|
1295
|
+
// one's missing target is guarded below, so only the link itself
|
|
1296
|
+
// needs guarding here. A symlink leaf inside a writable root could be
|
|
1297
|
+
// replaced, so its directory is protected; home already is.
|
|
1298
|
+
if (link && dirname(path) !== broad.home && writableAt(dirname(path)))
|
|
1299
|
+
linkParents.add(dirname(path));
|
|
1300
|
+
return;
|
|
1301
|
+
}
|
|
1302
|
+
if (belowFirstMissing.has(path) && !firstMissing.has(path))
|
|
1303
|
+
return; // Its first missing component is guarded.
|
|
1304
|
+
if (firstMissing.has(path)) {
|
|
1305
|
+
// Where a dangling link would land: nothing may be created there.
|
|
1306
|
+
// A missing directory gets an empty read-only placeholder directory,
|
|
1307
|
+
// so only that one name is taken, not every new entry beside it
|
|
1308
|
+
// (`~/.cache/<tool>/…` would otherwise lock all of `~/.cache`). A
|
|
1309
|
+
// missing file is not planted in the user's files: its parent is
|
|
1310
|
+
// locked instead (entries stay writable, see below), except in the
|
|
1311
|
+
// workspace root and temp, which must keep accepting new entries,
|
|
1312
|
+
// so the file placeholder is bound there. Each placeholder made is
|
|
1313
|
+
// reported, and the workspace and temp fail closed without one.
|
|
1314
|
+
if (!writableAt(path))
|
|
1315
|
+
return;
|
|
1316
|
+
const ancestor = dirname(path);
|
|
1317
|
+
if (ancestor === broad.home)
|
|
1318
|
+
return; // Home is read-only already.
|
|
1319
|
+
const failClosed = ancestor === project || broad.tempRoots.includes(ancestor);
|
|
1320
|
+
const directory = firstMissing.get(path);
|
|
1321
|
+
if (directory || failClosed) {
|
|
1322
|
+
const existed = exists(path);
|
|
1323
|
+
let made;
|
|
1324
|
+
if (directory) {
|
|
1325
|
+
try {
|
|
1326
|
+
makeDirectory(path);
|
|
1327
|
+
}
|
|
1328
|
+
catch { /* Re-checked below. */ }
|
|
1329
|
+
made = exists(path);
|
|
1330
|
+
}
|
|
1331
|
+
else {
|
|
1332
|
+
made = materialize(path);
|
|
1333
|
+
}
|
|
1334
|
+
if (made) {
|
|
1335
|
+
if (!existed)
|
|
1336
|
+
created.push(`${path}${directory ? "/" : ""}`);
|
|
1337
|
+
readOnly.add(path);
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
if (failClosed)
|
|
1341
|
+
throw new Error(`Cannot protect denied path: ${path}`);
|
|
1342
|
+
}
|
|
1343
|
+
linkParents.add(ancestor);
|
|
1344
|
+
return;
|
|
1345
|
+
}
|
|
1346
|
+
if (!exists(path)) {
|
|
1347
|
+
if (!writableAt(path))
|
|
1348
|
+
return; // Nothing inside the sandbox can create it.
|
|
1349
|
+
let ancestor = dirname(path);
|
|
1350
|
+
while (!exists(ancestor) && dirname(ancestor) !== ancestor)
|
|
1351
|
+
ancestor = dirname(ancestor);
|
|
1352
|
+
if (directoryLocation(path)) {
|
|
1353
|
+
(seams.makeDirectory ?? ((target) => mkdirSync(target, { recursive: true, mode: 0o700 })))(path);
|
|
1354
|
+
directory = true;
|
|
1355
|
+
}
|
|
1356
|
+
else if (writableRoots.includes(ancestor) || contains(project, ancestor) ||
|
|
1357
|
+
broad.tempRoots.some((root) => contains(root, ancestor))) {
|
|
1358
|
+
if (!materialize(path))
|
|
1359
|
+
throw new Error(`Cannot protect denied path: ${path}`);
|
|
1360
|
+
}
|
|
1361
|
+
else {
|
|
1362
|
+
readOnly.add(ancestor);
|
|
1363
|
+
return;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
if (hide) {
|
|
1367
|
+
directory ??= seams.isDirectory ? seams.isDirectory(path) : statSync(path).isDirectory();
|
|
1368
|
+
hidden.push({ path, source: directory ? mask.directory : mask.file });
|
|
1369
|
+
}
|
|
1370
|
+
else {
|
|
1371
|
+
readOnly.add(path);
|
|
1372
|
+
}
|
|
1373
|
+
};
|
|
1374
|
+
for (const path of credentials) {
|
|
1375
|
+
if (permissions.storedCredentials === "off")
|
|
1376
|
+
guard(path, true);
|
|
1377
|
+
else if (permissions.storedCredentials === "read")
|
|
1378
|
+
guard(path, false);
|
|
1379
|
+
}
|
|
1380
|
+
for (const path of writeDenied)
|
|
1381
|
+
guard(path, false);
|
|
1382
|
+
if (permissions.projectFiles === "read")
|
|
1383
|
+
readOnly.add(project);
|
|
1384
|
+
// A read-only link parent keeps its existing entries writable: each one is
|
|
1385
|
+
// bound back read-write (a mount point, so it cannot be renamed away either).
|
|
1386
|
+
// Only new top-level entries in that directory are lost. Protected entries
|
|
1387
|
+
// keep their own mounts, and a symlink entry is never bound: a bind follows
|
|
1388
|
+
// it, and writes through it already meet its target's own mounts.
|
|
1389
|
+
const rebound = new Set();
|
|
1390
|
+
for (const parent of linkParents)
|
|
1391
|
+
readOnly.add(parent);
|
|
1392
|
+
for (const parent of linkParents) {
|
|
1393
|
+
for (const entry of list(parent)) {
|
|
1394
|
+
if (entry.symlink)
|
|
1395
|
+
continue;
|
|
1396
|
+
const path = join(parent, entry.name);
|
|
1397
|
+
if (insideProtected(path) || readOnly.has(path) || writable.has(path))
|
|
1398
|
+
continue;
|
|
1399
|
+
rebound.add(path);
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
// Intermediate directories between a writable root and anything bound
|
|
1403
|
+
// inside it become mount points too, so none of them can be renamed away.
|
|
1404
|
+
const anchors = new Set();
|
|
1405
|
+
for (const target of [...writableRoots, ...readOnly, ...hidden.map((entry) => entry.path)]) {
|
|
1406
|
+
for (const parent of protectedAncestors([target])) {
|
|
1407
|
+
if (writableRoots.some((root) => root !== parent && contains(root, parent)) &&
|
|
1408
|
+
!readOnly.has(parent) && !writable.has(parent) && !rebound.has(parent))
|
|
1409
|
+
anchors.add(parent);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
const mounts = [
|
|
1413
|
+
...[...anchors].map((path) => ({ option: "--bind", source: path, path, rank: 0 })),
|
|
1414
|
+
...writableRoots.map((path) => ({ option: "--bind", source: path, path, rank: 1 })),
|
|
1415
|
+
// `-try`: an entry removed before a resumed launch stays read-only instead of failing it.
|
|
1416
|
+
...[...rebound].map((path) => ({ option: "--bind-try", source: path, path, rank: 1 })),
|
|
1417
|
+
...[...readOnly].map((path) => ({ option: "--ro-bind", source: path, path, rank: 2 })),
|
|
1418
|
+
...hidden.map(({ path, source }) => ({ option: "--ro-bind", source, path, rank: 3 })),
|
|
1419
|
+
].sort((a, b) => a.path.length - b.path.length || a.rank - b.rank);
|
|
1420
|
+
const seen = new Set();
|
|
1421
|
+
const fileArgs = ["--ro-bind", "/", "/", "--dev", "/dev"];
|
|
1422
|
+
for (const mount of mounts) {
|
|
1423
|
+
const key = `${mount.option}\0${mount.source}\0${mount.path}`;
|
|
1424
|
+
if (seen.has(key))
|
|
1425
|
+
continue;
|
|
1426
|
+
seen.add(key);
|
|
1427
|
+
fileArgs.push(mount.option, mount.source, mount.path);
|
|
1428
|
+
}
|
|
1429
|
+
return {
|
|
1430
|
+
file: bwrap,
|
|
1431
|
+
fileArgs: [...fileArgs, ...(!permissions.network ? ["--unshare-net"] : []),
|
|
1432
|
+
"--", args.execPath, ...args.execArgs],
|
|
1433
|
+
...(created.length && { notices: [
|
|
1434
|
+
`Sandbox: created an empty read-only placeholder where a protected symlink points at nothing, and left it in place (remove it before creating the real target): ${created.join(", ")}`,
|
|
1435
|
+
] }),
|
|
1436
|
+
};
|
|
1437
|
+
}
|
|
1438
|
+
function linuxSandboxBackend(seams) {
|
|
1439
|
+
const bwrap = (seams.lookupExecutable ?? systemSandboxExecutable)("bwrap");
|
|
1440
|
+
if (!bwrap)
|
|
1441
|
+
return undefined;
|
|
1442
|
+
return {
|
|
1443
|
+
id: "linux-bubblewrap",
|
|
1444
|
+
executable: bwrap,
|
|
1445
|
+
buildCommand: (args, buildSeams) => buildLinuxSandboxCommand(bwrap, args, buildSeams),
|
|
1446
|
+
};
|
|
1447
|
+
}
|
|
1448
|
+
function selectedSandboxBackend(seams) {
|
|
1449
|
+
const platform = currentPlatform(seams);
|
|
1450
|
+
if (platform === "darwin")
|
|
1451
|
+
return macOSSandboxBackend;
|
|
1452
|
+
if (platform === "linux")
|
|
1453
|
+
return linuxSandboxBackend(seams);
|
|
1454
|
+
return undefined;
|
|
1455
|
+
}
|
|
1456
|
+
/**
|
|
1457
|
+
* Why no backend applies here. The requirement only: what a caller can do
|
|
1458
|
+
* instead is a property of that caller's surface, not of the platform, and is
|
|
1459
|
+
* supplied through `SandboxRequest.remedy`.
|
|
1460
|
+
*/
|
|
1461
|
+
function unavailableMessage(platform) {
|
|
1462
|
+
if (platform === "linux") {
|
|
1463
|
+
return "Linux sandbox requires executable bubblewrap (bwrap) in /usr/bin or /bin. Install bubblewrap to enable it.";
|
|
1464
|
+
}
|
|
1465
|
+
if (platform === "darwin") {
|
|
1466
|
+
return "macOS sandbox requires /usr/bin/sandbox-exec, which is missing here.";
|
|
1467
|
+
}
|
|
1468
|
+
return `sandbox is unsupported on ${platform}.`;
|
|
1469
|
+
}
|
|
1470
|
+
/** Report which backend this platform would select, and why it would not. */
|
|
1471
|
+
export function describeSandboxSupport(seams = {}) {
|
|
1472
|
+
const platform = currentPlatform(seams);
|
|
1473
|
+
const backend = selectedSandboxBackend(seams);
|
|
1474
|
+
if (!backend) {
|
|
1475
|
+
return {
|
|
1476
|
+
supported: false,
|
|
1477
|
+
platform,
|
|
1478
|
+
backend: undefined,
|
|
1479
|
+
executable: undefined,
|
|
1480
|
+
reason: unavailableMessage(platform),
|
|
1481
|
+
};
|
|
1482
|
+
}
|
|
1483
|
+
return { supported: true, platform, backend: backend.id, executable: backend.executable };
|
|
1484
|
+
}
|
|
1485
|
+
/** The message explaining why no backend is available on this platform. */
|
|
1486
|
+
export function sandboxUnavailableMessage(seams = {}) {
|
|
1487
|
+
return unavailableMessage(currentPlatform(seams));
|
|
1488
|
+
}
|
|
1489
|
+
/** True when an OS write-sandbox backend can be applied on this platform. */
|
|
1490
|
+
export function sandboxSupported(seams = {}) {
|
|
1491
|
+
return selectedSandboxBackend(seams) !== undefined;
|
|
1492
|
+
}
|
|
1493
|
+
/**
|
|
1494
|
+
* Resolve the caller's default-on, explicit-request, and opt-out policy before
|
|
1495
|
+
* spawning. A selected backend always returns its wrapper; callers never retry
|
|
1496
|
+
* the child directly when that wrapper exits or cannot initialize.
|
|
1497
|
+
*/
|
|
1498
|
+
export function maybeBuildSandboxCommand(args, request, seams = {}) {
|
|
1499
|
+
if (args.policy.permissions?.commands === false) {
|
|
1500
|
+
throw new Error("Sandbox commands permission is off; enable commands before launching a sandboxed process (including bootstrap).");
|
|
1501
|
+
}
|
|
1502
|
+
if (!request.sandboxEnabled)
|
|
1503
|
+
return undefined;
|
|
1504
|
+
const backend = selectedSandboxBackend(seams);
|
|
1505
|
+
if (!backend) {
|
|
1506
|
+
if (args.policy.permissions) {
|
|
1507
|
+
throw new Error(`Cannot enforce sandbox permissions: ${sandboxUnavailableMessage(seams)}`);
|
|
1508
|
+
}
|
|
1509
|
+
if (request.explicitSandbox) {
|
|
1510
|
+
const reason = sandboxUnavailableMessage(seams);
|
|
1511
|
+
throw new Error(request.remedy ? `${reason} ${request.remedy}` : reason);
|
|
1512
|
+
}
|
|
1513
|
+
return undefined;
|
|
1514
|
+
}
|
|
1515
|
+
return backend.buildCommand(args, seams);
|
|
1516
|
+
}
|
|
1517
|
+
/**
|
|
1518
|
+
* Return the selected backend's executable and ordered argv wrapper around the
|
|
1519
|
+
* target. The fallback preserves the pre-existing direct-call result for callers
|
|
1520
|
+
* that bypass the request-policy helper above.
|
|
1521
|
+
*/
|
|
1522
|
+
export function buildSandboxCommand(args, seams = {}) {
|
|
1523
|
+
if (args.policy.permissions?.commands === false) {
|
|
1524
|
+
throw new Error("Sandbox commands permission is off; enable commands before launching a sandboxed process (including bootstrap).");
|
|
1525
|
+
}
|
|
1526
|
+
const backend = selectedSandboxBackend(seams);
|
|
1527
|
+
if (!backend && args.policy.permissions) {
|
|
1528
|
+
throw new Error(`Cannot enforce sandbox permissions: ${sandboxUnavailableMessage(seams)}`);
|
|
1529
|
+
}
|
|
1530
|
+
return (backend ?? macOSSandboxBackend).buildCommand(args, seams);
|
|
1531
|
+
}
|