@dsh-ray/lark-channel 0.0.1-seed.0 → 0.2.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/LICENSE +21 -0
- package/README.md +206 -1
- package/bin/dsh-lark-setup.js +37 -0
- package/bin/dsh-ray-setup.js +34 -0
- package/bin/dsh-supervised-growth-setup.js +34 -0
- package/cordis.patch.yml +23 -0
- package/docs/operations.md +133 -0
- package/docs/progress-security.md +125 -0
- package/docs/rsi-setup.md +216 -0
- package/docs/setup.md +358 -0
- package/docs/supervised-growth.md +95 -0
- package/lib/adapter.d.ts +185 -0
- package/lib/adapter.d.ts.map +1 -0
- package/lib/adapter.js +2072 -0
- package/lib/adapter.js.map +1 -0
- package/lib/answer-card.d.ts +40 -0
- package/lib/answer-card.d.ts.map +1 -0
- package/lib/answer-card.js +262 -0
- package/lib/answer-card.js.map +1 -0
- package/lib/approval.d.ts +26 -0
- package/lib/approval.d.ts.map +1 -0
- package/lib/approval.js +104 -0
- package/lib/approval.js.map +1 -0
- package/lib/business-cli-install.d.ts +9 -0
- package/lib/business-cli-install.d.ts.map +1 -0
- package/lib/business-cli-install.js +367 -0
- package/lib/business-cli-install.js.map +1 -0
- package/lib/business-environment.d.ts +2 -0
- package/lib/business-environment.d.ts.map +1 -0
- package/lib/business-environment.js +16 -0
- package/lib/business-environment.js.map +1 -0
- package/lib/business-skill.d.ts +32 -0
- package/lib/business-skill.d.ts.map +1 -0
- package/lib/business-skill.js +202 -0
- package/lib/business-skill.js.map +1 -0
- package/lib/command-card.d.ts +3 -0
- package/lib/command-card.d.ts.map +1 -0
- package/lib/command-card.js +480 -0
- package/lib/command-card.js.map +1 -0
- package/lib/config.d.ts +32 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +115 -0
- package/lib/config.js.map +1 -0
- package/lib/group-thread.d.ts +20 -0
- package/lib/group-thread.d.ts.map +1 -0
- package/lib/group-thread.js +36 -0
- package/lib/group-thread.js.map +1 -0
- package/lib/index.d.ts +34 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +53 -0
- package/lib/index.js.map +1 -0
- package/lib/launchd-control.d.ts +24 -0
- package/lib/launchd-control.d.ts.map +1 -0
- package/lib/launchd-control.js +68 -0
- package/lib/launchd-control.js.map +1 -0
- package/lib/launchd-identity.d.ts +34 -0
- package/lib/launchd-identity.d.ts.map +1 -0
- package/lib/launchd-identity.js +184 -0
- package/lib/launchd-identity.js.map +1 -0
- package/lib/launchd-target-lock.d.ts +6 -0
- package/lib/launchd-target-lock.d.ts.map +1 -0
- package/lib/launchd-target-lock.js +48 -0
- package/lib/launchd-target-lock.js.map +1 -0
- package/lib/launchd.d.ts +62 -0
- package/lib/launchd.d.ts.map +1 -0
- package/lib/launchd.js +370 -0
- package/lib/launchd.js.map +1 -0
- package/lib/model-picker.d.ts +24 -0
- package/lib/model-picker.d.ts.map +1 -0
- package/lib/model-picker.js +95 -0
- package/lib/model-picker.js.map +1 -0
- package/lib/normalize.d.ts +12 -0
- package/lib/normalize.d.ts.map +1 -0
- package/lib/normalize.js +85 -0
- package/lib/normalize.js.map +1 -0
- package/lib/permission-picker.d.ts +29 -0
- package/lib/permission-picker.d.ts.map +1 -0
- package/lib/permission-picker.js +145 -0
- package/lib/permission-picker.js.map +1 -0
- package/lib/progress.d.ts +34 -0
- package/lib/progress.d.ts.map +1 -0
- package/lib/progress.js +354 -0
- package/lib/progress.js.map +1 -0
- package/lib/resident.d.ts +13 -0
- package/lib/resident.d.ts.map +1 -0
- package/lib/resident.js +41 -0
- package/lib/resident.js.map +1 -0
- package/lib/rsi-authority-config.d.ts +29 -0
- package/lib/rsi-authority-config.d.ts.map +1 -0
- package/lib/rsi-authority-config.js +233 -0
- package/lib/rsi-authority-config.js.map +1 -0
- package/lib/rsi-authority-resources.d.ts +36 -0
- package/lib/rsi-authority-resources.d.ts.map +1 -0
- package/lib/rsi-authority-resources.js +139 -0
- package/lib/rsi-authority-resources.js.map +1 -0
- package/lib/rsi-authority-runtime.d.ts +51 -0
- package/lib/rsi-authority-runtime.d.ts.map +1 -0
- package/lib/rsi-authority-runtime.js +496 -0
- package/lib/rsi-authority-runtime.js.map +1 -0
- package/lib/rsi-bootstrap-manifest.d.ts +53 -0
- package/lib/rsi-bootstrap-manifest.d.ts.map +1 -0
- package/lib/rsi-bootstrap-manifest.js +507 -0
- package/lib/rsi-bootstrap-manifest.js.map +1 -0
- package/lib/rsi-bootstrap.d.ts +61 -0
- package/lib/rsi-bootstrap.d.ts.map +1 -0
- package/lib/rsi-bootstrap.js +597 -0
- package/lib/rsi-bootstrap.js.map +1 -0
- package/lib/rsi-build.d.ts +76 -0
- package/lib/rsi-build.d.ts.map +1 -0
- package/lib/rsi-build.js +210 -0
- package/lib/rsi-build.js.map +1 -0
- package/lib/rsi-host-authorities.d.ts +18 -0
- package/lib/rsi-host-authorities.d.ts.map +1 -0
- package/lib/rsi-host-authorities.js +129 -0
- package/lib/rsi-host-authorities.js.map +1 -0
- package/lib/rsi-host-update.d.ts +179 -0
- package/lib/rsi-host-update.d.ts.map +1 -0
- package/lib/rsi-host-update.js +568 -0
- package/lib/rsi-host-update.js.map +1 -0
- package/lib/rsi-install-inputs.d.ts +45 -0
- package/lib/rsi-install-inputs.d.ts.map +1 -0
- package/lib/rsi-install-inputs.js +345 -0
- package/lib/rsi-install-inputs.js.map +1 -0
- package/lib/rsi-install.d.ts +117 -0
- package/lib/rsi-install.d.ts.map +1 -0
- package/lib/rsi-install.js +791 -0
- package/lib/rsi-install.js.map +1 -0
- package/lib/rsi-linux-cgroup-roots.d.ts +3 -0
- package/lib/rsi-linux-cgroup-roots.d.ts.map +1 -0
- package/lib/rsi-linux-cgroup-roots.js +3 -0
- package/lib/rsi-linux-cgroup-roots.js.map +1 -0
- package/lib/rsi-linux-creation-build.d.ts +36 -0
- package/lib/rsi-linux-creation-build.d.ts.map +1 -0
- package/lib/rsi-linux-creation-build.js +213 -0
- package/lib/rsi-linux-creation-build.js.map +1 -0
- package/lib/rsi-linux-native-materialize.d.ts +31 -0
- package/lib/rsi-linux-native-materialize.d.ts.map +1 -0
- package/lib/rsi-linux-native-materialize.js +162 -0
- package/lib/rsi-linux-native-materialize.js.map +1 -0
- package/lib/rsi-linux-native-relocation.d.ts +124 -0
- package/lib/rsi-linux-native-relocation.d.ts.map +1 -0
- package/lib/rsi-linux-native-relocation.js +1571 -0
- package/lib/rsi-linux-native-relocation.js.map +1 -0
- package/lib/rsi-linux-native-resources.d.ts +97 -0
- package/lib/rsi-linux-native-resources.d.ts.map +1 -0
- package/lib/rsi-linux-native-resources.js +521 -0
- package/lib/rsi-linux-native-resources.js.map +1 -0
- package/lib/rsi-linux-repository-bootstrap.d.ts +17 -0
- package/lib/rsi-linux-repository-bootstrap.d.ts.map +1 -0
- package/lib/rsi-linux-repository-bootstrap.js +317 -0
- package/lib/rsi-linux-repository-bootstrap.js.map +1 -0
- package/lib/rsi-linux-runner-states.d.ts +20 -0
- package/lib/rsi-linux-runner-states.d.ts.map +1 -0
- package/lib/rsi-linux-runner-states.js +70 -0
- package/lib/rsi-linux-runner-states.js.map +1 -0
- package/lib/rsi-linux-runtime-bootstrap.d.ts +10 -0
- package/lib/rsi-linux-runtime-bootstrap.d.ts.map +1 -0
- package/lib/rsi-linux-runtime-bootstrap.js +38 -0
- package/lib/rsi-linux-runtime-bootstrap.js.map +1 -0
- package/lib/rsi-linux-source-build.d.ts +28 -0
- package/lib/rsi-linux-source-build.d.ts.map +1 -0
- package/lib/rsi-linux-source-build.js +43 -0
- package/lib/rsi-linux-source-build.js.map +1 -0
- package/lib/rsi-local-activation.d.ts +78 -0
- package/lib/rsi-local-activation.d.ts.map +1 -0
- package/lib/rsi-local-activation.js +486 -0
- package/lib/rsi-local-activation.js.map +1 -0
- package/lib/rsi-local-cohort.d.ts +70 -0
- package/lib/rsi-local-cohort.d.ts.map +1 -0
- package/lib/rsi-local-cohort.js +810 -0
- package/lib/rsi-local-cohort.js.map +1 -0
- package/lib/rsi-local-install.d.ts +70 -0
- package/lib/rsi-local-install.d.ts.map +1 -0
- package/lib/rsi-local-install.js +363 -0
- package/lib/rsi-local-install.js.map +1 -0
- package/lib/rsi-local-pair-install.d.ts +53 -0
- package/lib/rsi-local-pair-install.d.ts.map +1 -0
- package/lib/rsi-local-pair-install.js +215 -0
- package/lib/rsi-local-pair-install.js.map +1 -0
- package/lib/rsi-local-profile-update.d.ts +25 -0
- package/lib/rsi-local-profile-update.d.ts.map +1 -0
- package/lib/rsi-local-profile-update.js +226 -0
- package/lib/rsi-local-profile-update.js.map +1 -0
- package/lib/rsi-local-roots-extension.d.ts +11 -0
- package/lib/rsi-local-roots-extension.d.ts.map +1 -0
- package/lib/rsi-local-roots-extension.js +31 -0
- package/lib/rsi-local-roots-extension.js.map +1 -0
- package/lib/rsi-local-update-build.d.ts +37 -0
- package/lib/rsi-local-update-build.d.ts.map +1 -0
- package/lib/rsi-local-update-build.js +75 -0
- package/lib/rsi-local-update-build.js.map +1 -0
- package/lib/rsi-local-update.d.ts +39 -0
- package/lib/rsi-local-update.d.ts.map +1 -0
- package/lib/rsi-local-update.js +205 -0
- package/lib/rsi-local-update.js.map +1 -0
- package/lib/rsi-memory-learning.d.ts +36 -0
- package/lib/rsi-memory-learning.d.ts.map +1 -0
- package/lib/rsi-memory-learning.js +111 -0
- package/lib/rsi-memory-learning.js.map +1 -0
- package/lib/rsi-native-install.d.ts +38 -0
- package/lib/rsi-native-install.d.ts.map +1 -0
- package/lib/rsi-native-install.js +312 -0
- package/lib/rsi-native-install.js.map +1 -0
- package/lib/rsi-native-launchd.d.ts +7 -0
- package/lib/rsi-native-launchd.d.ts.map +1 -0
- package/lib/rsi-native-launchd.js +49 -0
- package/lib/rsi-native-launchd.js.map +1 -0
- package/lib/rsi-native-profile.d.ts +76 -0
- package/lib/rsi-native-profile.d.ts.map +1 -0
- package/lib/rsi-native-profile.js +530 -0
- package/lib/rsi-native-profile.js.map +1 -0
- package/lib/rsi-native-runtime-bootstrap.d.ts +44 -0
- package/lib/rsi-native-runtime-bootstrap.d.ts.map +1 -0
- package/lib/rsi-native-runtime-bootstrap.js +619 -0
- package/lib/rsi-native-runtime-bootstrap.js.map +1 -0
- package/lib/rsi-ordinary-use-learning.d.ts +60 -0
- package/lib/rsi-ordinary-use-learning.d.ts.map +1 -0
- package/lib/rsi-ordinary-use-learning.js +103 -0
- package/lib/rsi-ordinary-use-learning.js.map +1 -0
- package/lib/rsi-owner-profile.d.ts +37 -0
- package/lib/rsi-owner-profile.d.ts.map +1 -0
- package/lib/rsi-owner-profile.js +178 -0
- package/lib/rsi-owner-profile.js.map +1 -0
- package/lib/rsi-plugin-creation.d.ts +46 -0
- package/lib/rsi-plugin-creation.d.ts.map +1 -0
- package/lib/rsi-plugin-creation.js +227 -0
- package/lib/rsi-plugin-creation.js.map +1 -0
- package/lib/rsi-plugin-revision.d.ts +38 -0
- package/lib/rsi-plugin-revision.d.ts.map +1 -0
- package/lib/rsi-plugin-revision.js +221 -0
- package/lib/rsi-plugin-revision.js.map +1 -0
- package/lib/rsi-profile.d.ts +81 -0
- package/lib/rsi-profile.d.ts.map +1 -0
- package/lib/rsi-profile.js +551 -0
- package/lib/rsi-profile.js.map +1 -0
- package/lib/rsi-release-build.d.ts +59 -0
- package/lib/rsi-release-build.d.ts.map +1 -0
- package/lib/rsi-release-build.js +219 -0
- package/lib/rsi-release-build.js.map +1 -0
- package/lib/rsi-scanner-retirement-config.d.ts +34 -0
- package/lib/rsi-scanner-retirement-config.d.ts.map +1 -0
- package/lib/rsi-scanner-retirement-config.js +272 -0
- package/lib/rsi-scanner-retirement-config.js.map +1 -0
- package/lib/rsi-scanner-retirement-runtime.d.ts +23 -0
- package/lib/rsi-scanner-retirement-runtime.d.ts.map +1 -0
- package/lib/rsi-scanner-retirement-runtime.js +98 -0
- package/lib/rsi-scanner-retirement-runtime.js.map +1 -0
- package/lib/rsi-scanner-retirement-stage.d.ts +55 -0
- package/lib/rsi-scanner-retirement-stage.d.ts.map +1 -0
- package/lib/rsi-scanner-retirement-stage.js +181 -0
- package/lib/rsi-scanner-retirement-stage.js.map +1 -0
- package/lib/rsi-scanner-retirement-transition.d.ts +57 -0
- package/lib/rsi-scanner-retirement-transition.d.ts.map +1 -0
- package/lib/rsi-scanner-retirement-transition.js +330 -0
- package/lib/rsi-scanner-retirement-transition.js.map +1 -0
- package/lib/rsi-service-environment.d.ts +6 -0
- package/lib/rsi-service-environment.d.ts.map +1 -0
- package/lib/rsi-service-environment.js +113 -0
- package/lib/rsi-service-environment.js.map +1 -0
- package/lib/rsi-service-setup.d.ts +10 -0
- package/lib/rsi-service-setup.d.ts.map +1 -0
- package/lib/rsi-service-setup.js +34 -0
- package/lib/rsi-service-setup.js.map +1 -0
- package/lib/rsi-setup.d.ts +92 -0
- package/lib/rsi-setup.d.ts.map +1 -0
- package/lib/rsi-setup.js +908 -0
- package/lib/rsi-setup.js.map +1 -0
- package/lib/rsi-source-maintenance.d.ts +33 -0
- package/lib/rsi-source-maintenance.d.ts.map +1 -0
- package/lib/rsi-source-maintenance.js +407 -0
- package/lib/rsi-source-maintenance.js.map +1 -0
- package/lib/rsi-source-update.d.ts +26 -0
- package/lib/rsi-source-update.d.ts.map +1 -0
- package/lib/rsi-source-update.js +276 -0
- package/lib/rsi-source-update.js.map +1 -0
- package/lib/rsi-source.d.ts +34 -0
- package/lib/rsi-source.d.ts.map +1 -0
- package/lib/rsi-source.js +487 -0
- package/lib/rsi-source.js.map +1 -0
- package/lib/rsi-systemd-bootstrap.d.ts +46 -0
- package/lib/rsi-systemd-bootstrap.d.ts.map +1 -0
- package/lib/rsi-systemd-bootstrap.js +271 -0
- package/lib/rsi-systemd-bootstrap.js.map +1 -0
- package/lib/sdk.d.ts +216 -0
- package/lib/sdk.d.ts.map +1 -0
- package/lib/sdk.js +1990 -0
- package/lib/sdk.js.map +1 -0
- package/lib/service.d.ts +63 -0
- package/lib/service.d.ts.map +1 -0
- package/lib/service.js +599 -0
- package/lib/service.js.map +1 -0
- package/lib/setup-business.d.ts +49 -0
- package/lib/setup-business.d.ts.map +1 -0
- package/lib/setup-business.js +335 -0
- package/lib/setup-business.js.map +1 -0
- package/lib/setup-materialization.d.ts +15 -0
- package/lib/setup-materialization.d.ts.map +1 -0
- package/lib/setup-materialization.js +221 -0
- package/lib/setup-materialization.js.map +1 -0
- package/lib/setup-profile.d.ts +23 -0
- package/lib/setup-profile.d.ts.map +1 -0
- package/lib/setup-profile.js +1248 -0
- package/lib/setup-profile.js.map +1 -0
- package/lib/setup.d.ts +298 -0
- package/lib/setup.d.ts.map +1 -0
- package/lib/setup.js +3310 -0
- package/lib/setup.js.map +1 -0
- package/lib/supervised-growth-profile.d.ts +111 -0
- package/lib/supervised-growth-profile.d.ts.map +1 -0
- package/lib/supervised-growth-profile.js +903 -0
- package/lib/supervised-growth-profile.js.map +1 -0
- package/lib/supervised-growth-setup.d.ts +201 -0
- package/lib/supervised-growth-setup.d.ts.map +1 -0
- package/lib/supervised-growth-setup.js +1005 -0
- package/lib/supervised-growth-setup.js.map +1 -0
- package/lib/systemd.d.ts +70 -0
- package/lib/systemd.d.ts.map +1 -0
- package/lib/systemd.js +308 -0
- package/lib/systemd.js.map +1 -0
- package/lib/tool-approval.d.ts +22 -0
- package/lib/tool-approval.d.ts.map +1 -0
- package/lib/tool-approval.js +107 -0
- package/lib/tool-approval.js.map +1 -0
- package/lib/types.d.ts +351 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +11 -0
- package/lib/types.js.map +1 -0
- package/lib/user-question.d.ts +31 -0
- package/lib/user-question.d.ts.map +1 -0
- package/lib/user-question.js +161 -0
- package/lib/user-question.js.map +1 -0
- package/lib/version.d.ts +2 -0
- package/lib/version.d.ts.map +1 -0
- package/lib/version.js +2 -0
- package/lib/version.js.map +1 -0
- package/lib/windows-task.d.ts +42 -0
- package/lib/windows-task.d.ts.map +1 -0
- package/lib/windows-task.js +143 -0
- package/lib/windows-task.js.map +1 -0
- package/lib/ws-card-callback.d.ts +7 -0
- package/lib/ws-card-callback.d.ts.map +1 -0
- package/lib/ws-card-callback.js +51 -0
- package/lib/ws-card-callback.js.map +1 -0
- package/package.json +162 -4
package/lib/rsi-setup.js
ADDED
|
@@ -0,0 +1,908 @@
|
|
|
1
|
+
import { constants, realpathSync } from 'node:fs';
|
|
2
|
+
import { chmod, lstat, mkdir, open, realpath, rename, unlink } from 'node:fs/promises';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import { createHash, createPublicKey, randomUUID } from 'node:crypto';
|
|
5
|
+
import { homedir } from 'node:os';
|
|
6
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
7
|
+
import { createRequire } from 'node:module';
|
|
8
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
9
|
+
import { isAlias, isMap, isScalar, isSeq, parseDocument } from 'yaml';
|
|
10
|
+
import { compileRsiProfiles } from './rsi-profile.js';
|
|
11
|
+
import { withDshHomeLifecycleLock } from './setup.js';
|
|
12
|
+
import { assertRsiSchedulerActivation, captureRsiAutomationInventories, rsiAutomationInventory, rsiOwnerBindingQuery, rsiDatabasePaths } from './rsi-owner-profile.js';
|
|
13
|
+
import { installDshResidentService } from './resident.js';
|
|
14
|
+
import { prepareRsiSourceWorkspace } from './rsi-source.js';
|
|
15
|
+
import { prepareRsiReleaseBuildEnvironment, RsiReleaseBuildUnavailableError } from './rsi-release-build.js';
|
|
16
|
+
import { prepareLinuxRsiRuntime, LinuxRsiRuntimeUnavailableError } from './rsi-linux-runtime-bootstrap.js';
|
|
17
|
+
import { prepareRsiAuthorityRuntime } from './rsi-authority-runtime.js';
|
|
18
|
+
import { prepareRsiAuthorityResources } from './rsi-authority-resources.js';
|
|
19
|
+
import { version } from './version.js';
|
|
20
|
+
import { renderDshSystemdService, systemdServicePaths } from './systemd.js';
|
|
21
|
+
import { rsiServiceEnvironmentPath } from './rsi-service-environment.js';
|
|
22
|
+
import { resolveRsiServiceEnvironments } from './rsi-service-setup.js';
|
|
23
|
+
import { validateRsiHostAuthorities } from './rsi-host-authorities.js';
|
|
24
|
+
import { validateRsiMemoryLearningSetup } from './rsi-memory-learning.js';
|
|
25
|
+
import { resolveInstalledRsiDsh } from './rsi-install-inputs.js';
|
|
26
|
+
const MAX_BYTES = 2_097_152;
|
|
27
|
+
const profilePattern = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/u;
|
|
28
|
+
const digest = (value) => createHash('sha256').update(value).digest('hex');
|
|
29
|
+
function fail(message) { throw new Error(`rsi setup: ${message}`); }
|
|
30
|
+
// Compare semantic YAML nodes, including executable !!js tags, without depending
|
|
31
|
+
// on comments or formatting retained by the profile patch serializer.
|
|
32
|
+
function yamlValue(node, document, ancestors = new Set()) {
|
|
33
|
+
if (isAlias(node)) {
|
|
34
|
+
const resolved = node.resolve(document);
|
|
35
|
+
if (!resolved || ancestors.has(resolved))
|
|
36
|
+
fail('cyclic or unresolved YAML alias');
|
|
37
|
+
return yamlValue(resolved, document, new Set(ancestors).add(resolved));
|
|
38
|
+
}
|
|
39
|
+
if (isScalar(node))
|
|
40
|
+
return { tag: node.tag, value: node.value };
|
|
41
|
+
if (isSeq(node))
|
|
42
|
+
return { tag: node.tag, items: node.items.map(item => yamlValue(item, document, ancestors)) };
|
|
43
|
+
if (isMap(node))
|
|
44
|
+
return { tag: node.tag, entries: node.items.map(item => [yamlValue(item.key, document, ancestors), yamlValue(item.value, document, ancestors)])
|
|
45
|
+
.sort((left, right) => JSON.stringify(left[0]).localeCompare(JSON.stringify(right[0]))) };
|
|
46
|
+
return node;
|
|
47
|
+
}
|
|
48
|
+
function patchValue(source, label) {
|
|
49
|
+
const document = parseDocument(source, { uniqueKeys: true });
|
|
50
|
+
if (document.errors.length || !isSeq(document.contents))
|
|
51
|
+
fail(`invalid ${label} patch`);
|
|
52
|
+
return yamlValue(document.contents, document);
|
|
53
|
+
}
|
|
54
|
+
export function assertRsiEffectivePatch(patch, effective) {
|
|
55
|
+
const expected = parseDocument(patch), actual = parseDocument(effective);
|
|
56
|
+
if (expected.errors.length || actual.errors.length || !isSeq(expected.contents) || !isSeq(actual.contents))
|
|
57
|
+
fail('invalid final profile composition');
|
|
58
|
+
for (const row of expected.contents.items) {
|
|
59
|
+
if (!isMap(row) || !row.has('config'))
|
|
60
|
+
continue;
|
|
61
|
+
const matches = actual.contents.items.filter(item => isMap(item) && item.get('id') === row.get('id'));
|
|
62
|
+
const resolved = matches[0];
|
|
63
|
+
if (matches.length !== 1 || !isMap(resolved) || resolved.get('disabled') === true && row.get('disabled') !== true
|
|
64
|
+
|| !isDeepStrictEqual(yamlValue(row.get('config', true), expected), yamlValue(resolved.get('config', true), actual)))
|
|
65
|
+
fail(`effective profile overrides compiled config for ${String(row.get('id'))}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export function parseRsiSetupArgs(argv) {
|
|
69
|
+
const result = { manifestPath: '', dshHome: process.env.DSH_HOME || join(homedir(), '.dsh'),
|
|
70
|
+
apply: false, rollback: false, start: false, confirmStopped: false, help: false };
|
|
71
|
+
const seen = new Set();
|
|
72
|
+
for (let i = 0; i < argv.length; i++) {
|
|
73
|
+
const key = argv[i];
|
|
74
|
+
if (key !== '--bundle' && seen.has(key))
|
|
75
|
+
fail(`duplicate option ${key}`);
|
|
76
|
+
seen.add(key);
|
|
77
|
+
if (key === '--help' || key === '-h')
|
|
78
|
+
result.help = true;
|
|
79
|
+
else if (key === '--apply')
|
|
80
|
+
result.apply = true;
|
|
81
|
+
else if (key === '--rollback')
|
|
82
|
+
result.rollback = true;
|
|
83
|
+
else if (key === '--start')
|
|
84
|
+
result.start = true;
|
|
85
|
+
else if (key === '--confirm-hosts-stopped')
|
|
86
|
+
result.confirmStopped = true;
|
|
87
|
+
else if (key === '--prepare-source')
|
|
88
|
+
result.prepareSource = true;
|
|
89
|
+
else if (key === '--prepare-build')
|
|
90
|
+
result.prepareBuild = true;
|
|
91
|
+
else if (key === '--prepare-authorities')
|
|
92
|
+
result.prepareAuthorities = true;
|
|
93
|
+
else if (key === '--install-owner')
|
|
94
|
+
result.installOwner = true;
|
|
95
|
+
else if (key === '--upgrade-owner')
|
|
96
|
+
result.upgradeOwner = true;
|
|
97
|
+
else if (key === '--wait-owner-dm')
|
|
98
|
+
result.waitOwnerDm = true;
|
|
99
|
+
else if (key === '--timeout-seconds') {
|
|
100
|
+
const value = argv[++i];
|
|
101
|
+
if (!value || !/^[1-9][0-9]{0,4}$/u.test(value) || Number(value) > 3600)
|
|
102
|
+
fail('--timeout-seconds requires an integer from 1 to 3600');
|
|
103
|
+
result.timeoutSeconds = Number(value);
|
|
104
|
+
}
|
|
105
|
+
else if (key === '--ack-existing-automations')
|
|
106
|
+
result.ackExistingAutomations = true;
|
|
107
|
+
else if (key === '--enable-ordinary-use-learning')
|
|
108
|
+
result.enableOrdinaryUseLearning = true;
|
|
109
|
+
else if (key === '--skip-memory-learning')
|
|
110
|
+
result.skipMemoryLearning = true;
|
|
111
|
+
else if (key === '--install-local-cohort')
|
|
112
|
+
result.installLocalCohort = true;
|
|
113
|
+
else if (key === '--prepare-local-update')
|
|
114
|
+
result.prepareLocalUpdate = true;
|
|
115
|
+
else if (key === '--add-memory-learning')
|
|
116
|
+
result.addMemoryLearning = true;
|
|
117
|
+
else if (key === '--bundle') {
|
|
118
|
+
const value = argv[++i];
|
|
119
|
+
if (!value || !/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/u.test(value))
|
|
120
|
+
fail('--bundle requires a plugin slug');
|
|
121
|
+
(result.bundles ??= []).push(value);
|
|
122
|
+
}
|
|
123
|
+
else if (key === '--optional-build')
|
|
124
|
+
result.optionalBuild = true;
|
|
125
|
+
else if (key === '--manifest' || key === '--dsh-home' || key === '--profile' || key === '--source-repository'
|
|
126
|
+
|| key === '--intake-provider' || key === '--intake-model') {
|
|
127
|
+
const value = argv[++i];
|
|
128
|
+
if (!value || value.startsWith('--'))
|
|
129
|
+
fail(`${key} needs a value`);
|
|
130
|
+
if (key === '--manifest')
|
|
131
|
+
result.manifestPath = resolve(value);
|
|
132
|
+
else if (key === '--profile')
|
|
133
|
+
result.profile = value;
|
|
134
|
+
else if (key === '--source-repository')
|
|
135
|
+
result.sourceRepository = value;
|
|
136
|
+
else if (key === '--intake-provider')
|
|
137
|
+
result.intakeProvider = value;
|
|
138
|
+
else if (key === '--intake-model')
|
|
139
|
+
result.intakeModel = value;
|
|
140
|
+
else
|
|
141
|
+
result.dshHome = value;
|
|
142
|
+
}
|
|
143
|
+
else
|
|
144
|
+
fail(`unknown option ${key}`);
|
|
145
|
+
}
|
|
146
|
+
if (result.help)
|
|
147
|
+
return result;
|
|
148
|
+
if (result.upgradeOwner && !result.installOwner)
|
|
149
|
+
fail('--upgrade-owner requires --install-owner');
|
|
150
|
+
if (result.enableOrdinaryUseLearning && !result.installOwner)
|
|
151
|
+
fail('--enable-ordinary-use-learning requires --install-owner');
|
|
152
|
+
if (result.skipMemoryLearning && !result.installOwner)
|
|
153
|
+
fail('--skip-memory-learning requires --install-owner');
|
|
154
|
+
if (result.skipMemoryLearning && result.enableOrdinaryUseLearning)
|
|
155
|
+
fail('--skip-memory-learning cannot be combined with --enable-ordinary-use-learning');
|
|
156
|
+
if ((result.intakeProvider === undefined) !== (result.intakeModel === undefined))
|
|
157
|
+
fail('--intake-provider and --intake-model must be provided together');
|
|
158
|
+
if ((result.intakeProvider !== undefined || result.intakeModel !== undefined) && result.enableOrdinaryUseLearning !== true) {
|
|
159
|
+
fail('--intake-provider/--intake-model require --enable-ordinary-use-learning');
|
|
160
|
+
}
|
|
161
|
+
if (result.timeoutSeconds !== undefined && !result.waitOwnerDm)
|
|
162
|
+
fail('--timeout-seconds requires --wait-owner-dm');
|
|
163
|
+
if (result.waitOwnerDm) {
|
|
164
|
+
if (result.manifestPath || result.apply || result.rollback || result.start || result.confirmStopped || result.installOwner
|
|
165
|
+
|| result.upgradeOwner
|
|
166
|
+
|| result.prepareSource || result.prepareBuild || result.prepareAuthorities || result.optionalBuild
|
|
167
|
+
|| result.sourceRepository || result.ackExistingAutomations || result.installLocalCohort || result.bundles
|
|
168
|
+
|| result.prepareLocalUpdate || result.addMemoryLearning || result.enableOrdinaryUseLearning
|
|
169
|
+
|| result.skipMemoryLearning || result.intakeProvider || result.intakeModel) {
|
|
170
|
+
fail('owner DM wait cannot be combined with other operations');
|
|
171
|
+
}
|
|
172
|
+
if (!isAbsolute(result.dshHome) || !result.profile || !profilePattern.test(result.profile))
|
|
173
|
+
fail('owner DM wait requires --profile and absolute DSH_HOME');
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
if (result.addMemoryLearning && !result.prepareLocalUpdate)
|
|
177
|
+
fail('--add-memory-learning requires --prepare-local-update');
|
|
178
|
+
if (result.ackExistingAutomations && (!result.installOwner && !result.apply || result.rollback
|
|
179
|
+
|| result.prepareLocalUpdate || result.installLocalCohort || result.prepareSource || result.prepareBuild || result.prepareAuthorities))
|
|
180
|
+
fail('--ack-existing-automations requires --install-owner or --apply');
|
|
181
|
+
if (result.prepareLocalUpdate) {
|
|
182
|
+
if (result.installLocalCohort || result.installOwner || result.prepareSource || result.prepareBuild
|
|
183
|
+
|| result.prepareAuthorities || result.manifestPath || result.apply || result.rollback || result.start
|
|
184
|
+
|| result.confirmStopped || result.optionalBuild || result.bundles)
|
|
185
|
+
fail('local update preparation cannot be combined with other operations');
|
|
186
|
+
if (!isAbsolute(result.dshHome) || !result.profile || !profilePattern.test(result.profile))
|
|
187
|
+
fail('local update preparation requires --profile and absolute DSH_HOME');
|
|
188
|
+
if (result.sourceRepository && !isAbsolute(result.sourceRepository))
|
|
189
|
+
fail('--source-repository must be absolute');
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
if (result.bundles && !result.installLocalCohort)
|
|
193
|
+
fail('--bundle requires --install-local-cohort');
|
|
194
|
+
if (result.installLocalCohort) {
|
|
195
|
+
if (result.installOwner || result.prepareSource || result.prepareBuild || result.prepareAuthorities || result.manifestPath
|
|
196
|
+
|| result.apply || result.rollback || result.start || result.confirmStopped || result.optionalBuild)
|
|
197
|
+
fail('local cohort installation cannot be combined with other operations');
|
|
198
|
+
if (!isAbsolute(result.dshHome) || !result.profile || !profilePattern.test(result.profile)
|
|
199
|
+
|| !result.sourceRepository || !isAbsolute(result.sourceRepository) || !result.bundles?.length
|
|
200
|
+
|| new Set(result.bundles).size !== result.bundles.length)
|
|
201
|
+
fail('local cohort installation requires a profile, absolute source repository and unique bundles');
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
if (result.optionalBuild && !result.prepareBuild)
|
|
205
|
+
fail('--optional-build requires --prepare-build');
|
|
206
|
+
if (result.installOwner) {
|
|
207
|
+
if (result.prepareSource || result.prepareBuild || result.prepareAuthorities || result.manifestPath || result.apply
|
|
208
|
+
|| result.rollback || result.start || result.confirmStopped || result.optionalBuild)
|
|
209
|
+
fail('owner installation cannot be combined with other operations');
|
|
210
|
+
if (!isAbsolute(result.dshHome) || !result.profile || !profilePattern.test(result.profile))
|
|
211
|
+
fail('owner installation requires --profile and absolute DSH_HOME');
|
|
212
|
+
if (result.sourceRepository && !isAbsolute(result.sourceRepository))
|
|
213
|
+
fail('--source-repository must be absolute');
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
if (result.prepareSource || result.prepareBuild || result.prepareAuthorities) {
|
|
217
|
+
if ([result.prepareSource, result.prepareBuild, result.prepareAuthorities].filter(Boolean).length > 1)
|
|
218
|
+
fail('resource preparation modes cannot be combined');
|
|
219
|
+
if (result.manifestPath || result.apply || result.rollback || result.start || result.confirmStopped)
|
|
220
|
+
fail('resource preparation cannot be combined with profile configuration operations');
|
|
221
|
+
if (!isAbsolute(result.dshHome) || !result.profile || !profilePattern.test(result.profile))
|
|
222
|
+
fail('resource preparation requires --profile and absolute DSH_HOME');
|
|
223
|
+
if (result.prepareAuthorities && result.sourceRepository)
|
|
224
|
+
fail('--source-repository requires --prepare-source or --prepare-build');
|
|
225
|
+
if (result.sourceRepository && !isAbsolute(result.sourceRepository))
|
|
226
|
+
fail('--source-repository must be absolute');
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
if (result.profile || result.sourceRepository)
|
|
230
|
+
fail('--profile and --source-repository require --prepare-source, --prepare-build or --prepare-authorities');
|
|
231
|
+
if (!result.manifestPath || !isAbsolute(result.dshHome))
|
|
232
|
+
fail('--manifest and absolute DSH_HOME are required');
|
|
233
|
+
if (result.rollback && (result.apply || result.start))
|
|
234
|
+
fail('--rollback cannot be combined with --apply or --start');
|
|
235
|
+
if (result.start && !result.apply)
|
|
236
|
+
fail('--start requires --apply');
|
|
237
|
+
if ((result.apply || result.rollback) && !result.confirmStopped)
|
|
238
|
+
fail('stop both Hosts and pass --confirm-hosts-stopped');
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
async function readOwnedFile(path, privateFile = true, allowRoot = false) {
|
|
242
|
+
if (!isAbsolute(path) || resolve(path) !== path || await realpath(path) !== path)
|
|
243
|
+
fail(`noncanonical file: ${path}`);
|
|
244
|
+
const descriptor = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
245
|
+
try {
|
|
246
|
+
const entry = await descriptor.stat(), linked = await lstat(path);
|
|
247
|
+
if (!entry.isFile() || (!allowRoot && entry.nlink !== 1) || (entry.uid !== process.getuid?.() && !(allowRoot && entry.uid === 0)) || entry.size > MAX_BYTES
|
|
248
|
+
|| (entry.mode & (privateFile ? 0o077 : 0o022)) !== 0 || linked.ino !== entry.ino || linked.dev !== entry.dev)
|
|
249
|
+
fail(`unsafe file: ${path}`);
|
|
250
|
+
return await descriptor.readFile('utf8');
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
await descriptor.close();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async function safeDirectory(path) {
|
|
257
|
+
const entry = await lstat(path);
|
|
258
|
+
if (!entry.isDirectory() || entry.isSymbolicLink() || entry.uid !== process.getuid?.()
|
|
259
|
+
|| (entry.mode & 0o022) !== 0 || await realpath(path) !== path)
|
|
260
|
+
fail(`unsafe directory: ${path}`);
|
|
261
|
+
}
|
|
262
|
+
async function syncDirectory(path) {
|
|
263
|
+
const descriptor = await open(path, constants.O_RDONLY);
|
|
264
|
+
try {
|
|
265
|
+
await descriptor.sync();
|
|
266
|
+
}
|
|
267
|
+
finally {
|
|
268
|
+
await descriptor.close();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
async function atomicWrite(path, value) {
|
|
272
|
+
const temporary = `${path}.tmp-${randomUUID()}`;
|
|
273
|
+
const descriptor = await open(temporary, 'wx', 0o600);
|
|
274
|
+
try {
|
|
275
|
+
await descriptor.writeFile(value);
|
|
276
|
+
await descriptor.sync();
|
|
277
|
+
}
|
|
278
|
+
finally {
|
|
279
|
+
await descriptor.close();
|
|
280
|
+
}
|
|
281
|
+
try {
|
|
282
|
+
await rename(temporary, path);
|
|
283
|
+
await chmod(path, 0o600);
|
|
284
|
+
await syncDirectory(dirname(path));
|
|
285
|
+
}
|
|
286
|
+
finally {
|
|
287
|
+
await unlink(temporary).catch(error => { if (error.code !== 'ENOENT')
|
|
288
|
+
throw error; });
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function profiles(manifest) {
|
|
292
|
+
const values = [manifest.targetProfile, manifest.coordinatorProfile];
|
|
293
|
+
if (!values.every(value => typeof value === 'string' && profilePattern.test(value)) || values[0] === values[1])
|
|
294
|
+
fail('profiles must be distinct canonical names');
|
|
295
|
+
return values;
|
|
296
|
+
}
|
|
297
|
+
function patchPath(home, profile) { return join(home, 'profiles', profile, 'cordis.patch.yml'); }
|
|
298
|
+
async function readJournal(path, expected) {
|
|
299
|
+
let content;
|
|
300
|
+
try {
|
|
301
|
+
content = await readOwnedFile(path);
|
|
302
|
+
}
|
|
303
|
+
catch (error) {
|
|
304
|
+
if (error.code === 'ENOENT')
|
|
305
|
+
return undefined;
|
|
306
|
+
throw error;
|
|
307
|
+
}
|
|
308
|
+
const value = JSON.parse(content);
|
|
309
|
+
if (![1, 2].includes(value.schemaVersion) || !['prepared', 'applied'].includes(value.stage) || !/^[a-f0-9]{64}$/u.test(value.manifestDigest)
|
|
310
|
+
|| !Array.isArray(value.entries) || value.entries.length !== 2
|
|
311
|
+
|| value.entries.some((entry, index) => entry.profile !== expected[index] || typeof entry.before !== 'string' || typeof entry.after !== 'string'))
|
|
312
|
+
fail('journal does not match this profile pair');
|
|
313
|
+
if (value.schemaVersion === 2) {
|
|
314
|
+
for (const entries of [value.environments, value.units]) {
|
|
315
|
+
if (!Array.isArray(entries) || entries.length !== 2 || entries.some((entry, index) => entry.profile !== expected[index]
|
|
316
|
+
|| (entry.before !== null && typeof entry.before !== 'string') || (entry.after !== null && typeof entry.after !== 'string')))
|
|
317
|
+
fail('invalid service environment journal');
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
else if (value.environments || value.units)
|
|
321
|
+
fail('legacy journal cannot contain service resources');
|
|
322
|
+
return value;
|
|
323
|
+
}
|
|
324
|
+
async function readOptionalFile(path, privateFile = true) {
|
|
325
|
+
try {
|
|
326
|
+
return await readOwnedFile(path, privateFile);
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
if (error.code !== 'ENOENT')
|
|
330
|
+
throw error;
|
|
331
|
+
// realpath also reports ENOENT for dangling links; those are not absent files.
|
|
332
|
+
try {
|
|
333
|
+
await lstat(path);
|
|
334
|
+
}
|
|
335
|
+
catch (linkedError) {
|
|
336
|
+
if (linkedError.code === 'ENOENT')
|
|
337
|
+
return null;
|
|
338
|
+
throw linkedError;
|
|
339
|
+
}
|
|
340
|
+
fail(`unsafe file: ${path}`);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
async function writeOptionalFile(path, value) {
|
|
344
|
+
if (value !== null) {
|
|
345
|
+
await atomicWrite(path, value);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
try {
|
|
349
|
+
await unlink(path);
|
|
350
|
+
await syncDirectory(dirname(path));
|
|
351
|
+
}
|
|
352
|
+
catch (error) {
|
|
353
|
+
if (error.code !== 'ENOENT')
|
|
354
|
+
throw error;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
function auxiliary(home, journal, ports) {
|
|
358
|
+
return [
|
|
359
|
+
...(journal.environments ?? []).map(entry => ({ ...entry, path: rsiServiceEnvironmentPath(home, entry.profile), privateFile: true })),
|
|
360
|
+
...(journal.units ?? []).map(entry => ({ ...entry, path: ports.serviceUnitPath(entry.profile, home), privateFile: false })),
|
|
361
|
+
];
|
|
362
|
+
}
|
|
363
|
+
async function rollback(home, journalPath, journal, ports) {
|
|
364
|
+
// Check every resource before restoring any. Retry accepts a half-restored transaction.
|
|
365
|
+
for (const entry of journal.entries) {
|
|
366
|
+
const current = await readOwnedFile(patchPath(home, entry.profile), false);
|
|
367
|
+
if (current !== entry.before && current !== entry.after)
|
|
368
|
+
fail('profile changed outside setup; retain journal for reconciliation');
|
|
369
|
+
}
|
|
370
|
+
const resources = auxiliary(home, journal, ports);
|
|
371
|
+
for (const entry of resources) {
|
|
372
|
+
const current = await readOptionalFile(entry.path, entry.privateFile);
|
|
373
|
+
if (current !== entry.before && current !== entry.after)
|
|
374
|
+
fail('service resource changed outside setup; retain journal for reconciliation');
|
|
375
|
+
}
|
|
376
|
+
for (const entry of journal.entries)
|
|
377
|
+
await atomicWrite(patchPath(home, entry.profile), entry.before);
|
|
378
|
+
for (const entry of resources)
|
|
379
|
+
await writeOptionalFile(entry.path, entry.before);
|
|
380
|
+
if (journal.units)
|
|
381
|
+
ports.reloadServices();
|
|
382
|
+
await unlink(journalPath);
|
|
383
|
+
await syncDirectory(home);
|
|
384
|
+
}
|
|
385
|
+
export async function readRsiInstalledBase(profile, home, environment = process.env) {
|
|
386
|
+
if (!profilePattern.test(profile))
|
|
387
|
+
fail('invalid profile for installed base');
|
|
388
|
+
await safeDirectory(join(home, 'profiles', profile));
|
|
389
|
+
const dsh = await resolveInstalledRsiDsh(environment);
|
|
390
|
+
const packageName = '@deepseek-ai/dsh-base';
|
|
391
|
+
const installAnchor = join(dsh.root, 'package.json');
|
|
392
|
+
let installRoot = dsh.root;
|
|
393
|
+
for (let directory = dirname(dsh.root); directory !== dirname(directory); directory = dirname(directory)) {
|
|
394
|
+
if (basename(directory) === 'node_modules')
|
|
395
|
+
installRoot = directory;
|
|
396
|
+
}
|
|
397
|
+
for (const searchPath of createRequire(installAnchor).resolve.paths(packageName) ?? []) {
|
|
398
|
+
const packageRoot = join(searchPath, packageName);
|
|
399
|
+
const manifestPath = join(packageRoot, 'package.json');
|
|
400
|
+
let manifestBytes;
|
|
401
|
+
try {
|
|
402
|
+
const physical = await realpath(manifestPath);
|
|
403
|
+
const child = relative(installRoot, physical);
|
|
404
|
+
if (!child || child === '..' || child.startsWith(`..${sep}`) || isAbsolute(child))
|
|
405
|
+
fail('installed DSH base escapes Host installation');
|
|
406
|
+
manifestBytes = await readOwnedFile(physical, false, true);
|
|
407
|
+
}
|
|
408
|
+
catch (error) {
|
|
409
|
+
if (error.code === 'ENOENT')
|
|
410
|
+
continue;
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
413
|
+
let manifest;
|
|
414
|
+
try {
|
|
415
|
+
manifest = JSON.parse(manifestBytes);
|
|
416
|
+
}
|
|
417
|
+
catch {
|
|
418
|
+
fail('installed DSH base manifest is invalid');
|
|
419
|
+
}
|
|
420
|
+
const entry = manifest;
|
|
421
|
+
if (entry?.name !== packageName || entry.version !== dsh.version || entry.dsh?.bundle?.patch !== './cordis.patch.yml') {
|
|
422
|
+
fail('installed DSH base identity or patch declaration differs from Host');
|
|
423
|
+
}
|
|
424
|
+
const patchPath = await realpath(join(packageRoot, 'cordis.patch.yml'));
|
|
425
|
+
const child = relative(installRoot, patchPath);
|
|
426
|
+
if (!child || child === '..' || child.startsWith(`..${sep}`) || isAbsolute(child))
|
|
427
|
+
fail('installed DSH base patch escapes Host installation');
|
|
428
|
+
return readOwnedFile(patchPath, false, true);
|
|
429
|
+
}
|
|
430
|
+
fail('installed DSH base is absent from Host installation');
|
|
431
|
+
}
|
|
432
|
+
export const rsiSetupPorts = {
|
|
433
|
+
async base(profile, home) {
|
|
434
|
+
return readRsiInstalledBase(profile, home);
|
|
435
|
+
},
|
|
436
|
+
dump(profile, home) {
|
|
437
|
+
const result = spawnSync('dsh', ['--profile', profile, '--dump-config'], { encoding: 'utf8', timeout: 60_000,
|
|
438
|
+
maxBuffer: MAX_BYTES, env: { ...process.env, DSH_HOME: home } });
|
|
439
|
+
if (result.status !== 0 || result.error)
|
|
440
|
+
fail(`cannot compose profile ${profile}`);
|
|
441
|
+
return result.stdout;
|
|
442
|
+
},
|
|
443
|
+
async snapshot(effective, home) {
|
|
444
|
+
const { inspectActiveLarkOwnerBindingsLocally } = await import('@dsh-ray/assistant-delivery');
|
|
445
|
+
return inspectActiveLarkOwnerBindingsLocally({ databasePath: rsiDatabasePaths(effective, home).deliveryDatabasePath,
|
|
446
|
+
...rsiOwnerBindingQuery(effective, home) });
|
|
447
|
+
},
|
|
448
|
+
async automationInventory(effective, home) { return rsiAutomationInventory(effective, home); },
|
|
449
|
+
compile: compileRsiProfiles,
|
|
450
|
+
validateAuthorities: validateRsiAuthorities,
|
|
451
|
+
resolveEnvironments: resolveRsiServiceEnvironments,
|
|
452
|
+
serviceUnitPath: (profile, home) => systemdServicePaths({ dshHome: home, profile, home: realpathSync(homedir()) }).unitPath,
|
|
453
|
+
async renderServiceUnit(profile, home, environment) { return (await renderDshSystemdService({ dshHome: home, profile }, { environment })).source; },
|
|
454
|
+
reloadServices() {
|
|
455
|
+
const result = spawnSync('systemctl', ['--user', 'daemon-reload'], { encoding: 'utf8', timeout: 10_000, maxBuffer: 65_536 });
|
|
456
|
+
if (result.status !== 0 || result.error)
|
|
457
|
+
fail('cannot reload service definitions');
|
|
458
|
+
},
|
|
459
|
+
validateServiceUnits: validateRsiServiceUnitEnvironment,
|
|
460
|
+
assertStopped(profile) {
|
|
461
|
+
const result = spawnSync('systemctl', ['--user', 'show', `dsh-profile-${profile}.service`, '--property=ActiveState', '--value'], { encoding: 'utf8', timeout: 10_000, maxBuffer: 65_536 });
|
|
462
|
+
if (result.status !== 0 || result.error || !['inactive', 'failed'].includes(result.stdout.trim()))
|
|
463
|
+
fail(`systemd profile ${profile} must be stopped`);
|
|
464
|
+
},
|
|
465
|
+
async start(profile, home) { await installDshResidentService({ dshHome: home, profile }); },
|
|
466
|
+
};
|
|
467
|
+
/** Read back the loaded unit after daemon-reload, before starting either Host. */
|
|
468
|
+
export async function validateRsiServiceUnitEnvironment(manifest, environment) {
|
|
469
|
+
const path = environment.DSH_SYSTEMD_HOST_ATTESTOR_CONFIG;
|
|
470
|
+
if (!path)
|
|
471
|
+
return;
|
|
472
|
+
const wrapper = JSON.parse(await readOwnedFile(path));
|
|
473
|
+
if (wrapper.schemaVersion !== 4)
|
|
474
|
+
return;
|
|
475
|
+
const result = spawnSync('systemctl', ['--user', 'show', `dsh-profile-${manifest.targetProfile}.service`, '--property=Environment', '--value'], { encoding: 'utf8', timeout: 10_000, maxBuffer: 65_536 });
|
|
476
|
+
if (result.status !== 0 || result.error || result.stdout.trimEnd() !== wrapper.template?.unitProperties?.Environment)
|
|
477
|
+
fail('effective service environment differs from the Host installation grant');
|
|
478
|
+
}
|
|
479
|
+
/** Validate imported finite grants, without calling any signing/authorization method. */
|
|
480
|
+
export async function validateRsiAuthorities(manifest, binding, environment) {
|
|
481
|
+
const cp = await import('@dsh-ray/plugin-control-plane');
|
|
482
|
+
const config = manifest.controlPlane;
|
|
483
|
+
if (!config.sourceJobs || !config.sourceApprovals || !config.sourceReleases || !config.sourceAdoptions || !config.taskObservations)
|
|
484
|
+
fail('source authority chain is incomplete');
|
|
485
|
+
const approvals = JSON.parse(await readOwnedFile(config.sourceApprovals.configPath));
|
|
486
|
+
const releases = JSON.parse(await readOwnedFile(config.sourceReleases.configPath));
|
|
487
|
+
const adoptions = JSON.parse(await readOwnedFile(config.sourceAdoptions.authority.configPath));
|
|
488
|
+
const observations = JSON.parse(await readOwnedFile(config.taskObservations.authority.configPath));
|
|
489
|
+
cp.validateSourceApprovalAuthorityConfig(approvals);
|
|
490
|
+
cp.validateSourceReleaseAuthorityConfig(releases);
|
|
491
|
+
cp.validateSourceAdoptionAuthorityConfig(adoptions);
|
|
492
|
+
cp.validateTaskObservationAuthorityConfig(observations);
|
|
493
|
+
const expectedOwner = manifest.sourceReviews.owner;
|
|
494
|
+
if (expectedOwner.principalRecordId !== binding.owner.id || expectedOwner.principalVersion !== binding.owner.version)
|
|
495
|
+
fail('grant owner has changed');
|
|
496
|
+
// compileRsiProfiles first derives and checks all seven owner fields against
|
|
497
|
+
// effective Delivery. This secondary check binds grants to that owner; it
|
|
498
|
+
// cannot independently resolve a route from the principal receipt alone.
|
|
499
|
+
if (manifest.memoryLearning !== undefined)
|
|
500
|
+
validateRsiMemoryLearningSetup(manifest.memoryLearning, expectedOwner);
|
|
501
|
+
const trust = await cp.loadTrustConfig(config.trustPath);
|
|
502
|
+
if (trust.schemaVersion !== 4 || !trust.hostAttestor || !trust.releaseAdapters
|
|
503
|
+
|| ['pr', 'review', 'merge', 'build', 'sign', 'publish', 'registry-verify', 'catalog-admission'].some(phase => !Object.hasOwn(trust.releaseAdapters, phase)))
|
|
504
|
+
fail('complete release and Host attestation adapters are required');
|
|
505
|
+
const ledgerPath = join(config.statePath, 'control.sqlite');
|
|
506
|
+
const { id, version, path, sha256 } = trust.executor;
|
|
507
|
+
for (const authority of [approvals, releases, adoptions, observations]) {
|
|
508
|
+
if (authority.controlDatabasePath !== ledgerPath || !isDeepStrictEqual(authority.grant.owner, expectedOwner))
|
|
509
|
+
fail('authority owner/ledger mismatch');
|
|
510
|
+
}
|
|
511
|
+
if (trust.ledger.path !== ledgerPath || adoptions.grant.ledger.path !== ledgerPath
|
|
512
|
+
|| !isDeepStrictEqual(adoptions.grant.ledger, trust.ledger) || adoptions.grant.installationId !== trust.installationId
|
|
513
|
+
|| adoptions.grant.target.dshHome !== trust.dshHome || trust.catalog.path !== config.catalogPath
|
|
514
|
+
|| adoptions.grant.target.profile !== manifest.targetProfile || adoptions.grant.target.profilePath !== config.runtimeObserver?.profilePath
|
|
515
|
+
|| adoptions.grant.target.profilePath !== join(trust.dshHome, 'profiles', manifest.targetProfile)
|
|
516
|
+
|| !isDeepStrictEqual(adoptions.grant.executor, { id, version, path, sha256 })
|
|
517
|
+
|| !isDeepStrictEqual(adoptions.grant.liveQualification, config.sourceAdoptions.liveQualification)
|
|
518
|
+
|| !isDeepStrictEqual(adoptions.grant.hostDeploymentInputs, config.sourceAdoptions.hostDeploymentInputs)
|
|
519
|
+
|| adoptions.grant.catalogPath !== config.catalogPath || !isDeepStrictEqual(adoptions.grant.handoff, config.sourceAdoptions.handoff)
|
|
520
|
+
|| approvals.grant.repository !== config.sourceJobs.repository || releases.grant.repository !== config.sourceJobs.repository
|
|
521
|
+
|| approvals.grant.worktreeRoot !== join(config.statePath, 'source-worktrees') || releases.grant.worktreeRoot !== approvals.grant.worktreeRoot
|
|
522
|
+
|| approvals.grant.versioning !== 'patch' || releases.grant.versioning !== 'patch'
|
|
523
|
+
|| !isDeepStrictEqual(observations.grant.policy, config.taskObservations.policy)
|
|
524
|
+
|| observations.grant.profilePath !== config.taskObservations.profilePath
|
|
525
|
+
|| observations.grant.installationId !== trust.installationId || !isDeepStrictEqual(observations.grant.ledger, trust.ledger))
|
|
526
|
+
fail('authority deployment terms mismatch');
|
|
527
|
+
const selected = [...approvals.grant.plugins].sort();
|
|
528
|
+
if (!isDeepStrictEqual([...observations.grant.packages].sort(), selected.map(plugin => `@dsh-ray/${plugin}`)))
|
|
529
|
+
fail('observation package allowlist does not match');
|
|
530
|
+
for (const plugins of [releases.grant.plugins, adoptions.grant.policies.map(policy => policy.candidateId), manifest.sourceReviews.plugins]) {
|
|
531
|
+
if (!isDeepStrictEqual(selected, [...plugins].sort()))
|
|
532
|
+
fail('authority plugin allowlists do not match');
|
|
533
|
+
}
|
|
534
|
+
for (const policy of adoptions.grant.policies) {
|
|
535
|
+
const release = releases.grant.policies.find(item => item.candidateId === policy.candidateId);
|
|
536
|
+
if (!release || release.catalogId !== trust.catalog.id || release.catalogPath !== trust.catalog.path
|
|
537
|
+
|| Object.entries(policy).some(([key, value]) => !isDeepStrictEqual(value, release[key])))
|
|
538
|
+
fail('release and adoption policies do not match');
|
|
539
|
+
}
|
|
540
|
+
for (const [authority, purpose] of [[approvals, 'approval'], [adoptions, 'approval'], [releases, 'release-authorization'], [observations, 'host-attestation']]) {
|
|
541
|
+
const trusted = cp.resolveTrustKey(trust, purpose, authority.authority, authority.keyId);
|
|
542
|
+
const configured = createPublicKey(await readOwnedFile(authority.keyPath)).export({ type: 'spki', format: 'der' });
|
|
543
|
+
const registered = createPublicKey(trusted.publicKeyPem).export({ type: 'spki', format: 'der' });
|
|
544
|
+
if (!configured.equals(registered))
|
|
545
|
+
fail('authority key does not match registered trust');
|
|
546
|
+
}
|
|
547
|
+
if (config.liveQualification) {
|
|
548
|
+
const qualification = JSON.parse(await readOwnedFile(config.liveQualification.authority.configPath));
|
|
549
|
+
cp.validateLiveQualificationAuthorityConfig(qualification);
|
|
550
|
+
if (qualification.controlDatabasePath !== ledgerPath || !isDeepStrictEqual(qualification.grant.owner, expectedOwner)
|
|
551
|
+
|| !isDeepStrictEqual(qualification.grant.terms, config.sourceAdoptions.liveQualification)
|
|
552
|
+
|| qualification.grant.installationId !== trust.installationId || !isDeepStrictEqual(qualification.grant.ledger, trust.ledger)
|
|
553
|
+
|| qualification.grant.profilePath !== config.liveQualification.profilePath
|
|
554
|
+
|| !isDeepStrictEqual([...qualification.grant.packages].sort(), selected.map(plugin => `@dsh-ray/${plugin}`))
|
|
555
|
+
|| qualification.grant.expiresAt <= Date.now())
|
|
556
|
+
fail('live qualification authority terms mismatch');
|
|
557
|
+
const trusted = cp.resolveTrustKey(trust, 'host-attestation', qualification.authority, qualification.keyId);
|
|
558
|
+
const configured = createPublicKey(await readOwnedFile(qualification.keyPath)).export({ type: 'spki', format: 'der' });
|
|
559
|
+
if (!configured.equals(createPublicKey(trusted.publicKeyPem).export({ type: 'spki', format: 'der' })))
|
|
560
|
+
fail('live qualification authority key mismatch');
|
|
561
|
+
}
|
|
562
|
+
if ([approvals.grant.expiresAt, releases.grant.expiresAt, adoptions.grant.expiresAt, observations.grant.policy.expiresAt,
|
|
563
|
+
manifest.sourceReviews.expiresAt].some(value => value <= Date.now()))
|
|
564
|
+
fail('finite authority has expired');
|
|
565
|
+
await validateRsiHostAuthorities({ trust, adoption: adoptions, owner: expectedOwner, ledgerPath, trustPath: config.trustPath,
|
|
566
|
+
targetProfile: manifest.targetProfile,
|
|
567
|
+
liveQualification: config.sourceAdoptions.liveQualification, hostDeploymentInputs: config.sourceAdoptions.hostDeploymentInputs,
|
|
568
|
+
handoff: config.sourceAdoptions.handoff, ...(environment === undefined ? {} : { environment }), readPrivate: path => readOwnedFile(path) });
|
|
569
|
+
}
|
|
570
|
+
export async function configureRsiSetup(args, ports = rsiSetupPorts) {
|
|
571
|
+
return withDshHomeLifecycleLock(args.dshHome, () => configureRsiSetupLocked(args, ports));
|
|
572
|
+
}
|
|
573
|
+
/** Internal installer entry: caller already owns the DSH_HOME lifecycle lock. */
|
|
574
|
+
export async function configureRsiSetupLocked(args, ports = rsiSetupPorts) {
|
|
575
|
+
if (args.prepareLocalUpdate || args.installLocalCohort || args.bundles || args.installOwner || args.prepareSource || args.prepareBuild || args.prepareAuthorities || args.optionalBuild || args.profile || args.sourceRepository)
|
|
576
|
+
fail('resource preparation is a separate setup operation');
|
|
577
|
+
if (args.rollback && (args.apply || args.start) || args.start && !args.apply)
|
|
578
|
+
fail('incompatible setup operations');
|
|
579
|
+
const home = args.dshHome;
|
|
580
|
+
await safeDirectory(home);
|
|
581
|
+
const manifestBytes = await readOwnedFile(args.manifestPath);
|
|
582
|
+
const manifest = JSON.parse(manifestBytes);
|
|
583
|
+
const pair = profiles(manifest);
|
|
584
|
+
return (async () => {
|
|
585
|
+
for (const profile of pair)
|
|
586
|
+
await safeDirectory(join(home, 'profiles', profile));
|
|
587
|
+
const journalPath = join(home, '.rsi-setup-journal.json');
|
|
588
|
+
const previous = await readJournal(journalPath, pair);
|
|
589
|
+
if (args.apply || args.rollback) {
|
|
590
|
+
if (!args.confirmStopped)
|
|
591
|
+
fail('both Hosts must be stopped');
|
|
592
|
+
for (const profile of pair)
|
|
593
|
+
ports.assertStopped(profile);
|
|
594
|
+
}
|
|
595
|
+
if (args.rollback) {
|
|
596
|
+
if (!previous)
|
|
597
|
+
fail('no setup journal to roll back');
|
|
598
|
+
await rollback(home, journalPath, previous, ports);
|
|
599
|
+
return { mode: 'rolled-back', profiles: pair };
|
|
600
|
+
}
|
|
601
|
+
if (previous?.stage === 'prepared')
|
|
602
|
+
fail('interrupted setup; use --rollback before continuing');
|
|
603
|
+
const original = await Promise.all(pair.map(profile => readOwnedFile(patchPath(home, profile), false)));
|
|
604
|
+
const effective = pair.map(profile => ports.dump(profile, home));
|
|
605
|
+
const coordinatorBase = await ports.base(pair[1], home);
|
|
606
|
+
const snapshot = await ports.snapshot(effective[0], home);
|
|
607
|
+
if (snapshot.bindings.length !== 1)
|
|
608
|
+
fail('exactly one active owner DM binding is required');
|
|
609
|
+
const owner = snapshot.bindings[0];
|
|
610
|
+
const input = { manifest, dshHome: home, targetPatch: original[0], coordinatorPatch: original[1],
|
|
611
|
+
targetEffective: effective[0], coordinatorEffective: effective[1], coordinatorBase, owner,
|
|
612
|
+
...(args.upgradeExistingOwner ? { upgradeExistingOwner: true } : {}) };
|
|
613
|
+
const compiled = await ports.compile(input);
|
|
614
|
+
const inventories = await captureRsiAutomationInventories(ports.automationInventory, effective[0], effective[1], home, pair[1]);
|
|
615
|
+
if (args.expectedAutomationInventories && !isDeepStrictEqual(inventories, args.expectedAutomationInventories))
|
|
616
|
+
fail('Automation inventory changed since installer preflight');
|
|
617
|
+
assertRsiSchedulerActivation(inventories, args.ackExistingAutomations === true);
|
|
618
|
+
const originalEnvironments = await Promise.all(pair.map(profile => readOptionalFile(rsiServiceEnvironmentPath(home, profile))));
|
|
619
|
+
const environments = await ports.resolveEnvironments(manifest, home);
|
|
620
|
+
const originalUnits = environments ? await Promise.all(pair.map(profile => readOptionalFile(ports.serviceUnitPath(profile, home), false))) : undefined;
|
|
621
|
+
const nextUnits = environments ? await Promise.all(pair.map((profile, index) => ports.renderServiceUnit(profile, home, index === 0 ? environments.target : environments.coordinator))) : undefined;
|
|
622
|
+
await ports.validateAuthorities(manifest, owner, environments?.target);
|
|
623
|
+
if (!args.apply)
|
|
624
|
+
return { mode: 'checked', profiles: pair };
|
|
625
|
+
if (await readOwnedFile(args.manifestPath) !== manifestBytes || await ports.base(pair[1], home) !== coordinatorBase
|
|
626
|
+
|| !isDeepStrictEqual(snapshot, await ports.snapshot(effective[0], home)))
|
|
627
|
+
fail('manifest or owner snapshot changed before write');
|
|
628
|
+
if (!isDeepStrictEqual(inventories, await captureRsiAutomationInventories(ports.automationInventory, effective[0], effective[1], home, pair[1])))
|
|
629
|
+
fail('Automation inventory changed before write');
|
|
630
|
+
for (let i = 0; i < pair.length; i++) {
|
|
631
|
+
ports.assertStopped(pair[i]);
|
|
632
|
+
if (await readOwnedFile(patchPath(home, pair[i]), false) !== original[i])
|
|
633
|
+
fail('profile changed before write');
|
|
634
|
+
if (environments && (await readOptionalFile(rsiServiceEnvironmentPath(home, pair[i])) !== originalEnvironments[i]
|
|
635
|
+
|| await readOptionalFile(ports.serviceUnitPath(pair[i], home), false) !== originalUnits[i]))
|
|
636
|
+
fail('service resource changed before write');
|
|
637
|
+
}
|
|
638
|
+
const next = [compiled.targetPatch, compiled.coordinatorPatch];
|
|
639
|
+
if (previous && !args.upgradeExistingOwner && previous.entries.some((entry, i) => entry.after !== original[i]))
|
|
640
|
+
fail('profile changed since previous setup; reconcile its journal first');
|
|
641
|
+
const journal = { schemaVersion: environments ? 2 : 1, manifestDigest: digest(manifestBytes), stage: 'prepared',
|
|
642
|
+
entries: pair.map((profile, i) => ({ profile, before: original[i], after: next[i] })),
|
|
643
|
+
...(environments ? {
|
|
644
|
+
environments: pair.map((profile, i) => ({ profile, before: originalEnvironments[i], after: JSON.stringify({ schemaVersion: 1,
|
|
645
|
+
dshHome: home, profile, environment: i === 0 ? environments.target : environments.coordinator }) + '\n' })),
|
|
646
|
+
units: pair.map((profile, i) => ({ profile, before: originalUnits[i], after: nextUnits[i] })),
|
|
647
|
+
} : {}) };
|
|
648
|
+
if (previous && !args.upgradeExistingOwner)
|
|
649
|
+
for (const entry of auxiliary(home, previous, ports)) {
|
|
650
|
+
if (await readOptionalFile(entry.path, entry.privateFile) !== entry.after)
|
|
651
|
+
fail('service resource changed since previous setup; reconcile its journal first');
|
|
652
|
+
}
|
|
653
|
+
if (Buffer.byteLength(JSON.stringify(journal)) > MAX_BYTES)
|
|
654
|
+
fail('combined profile journal exceeds setup size limit');
|
|
655
|
+
const unchanged = next.every((value, i) => value === original[i]) && auxiliary(home, journal, ports).every(entry => entry.before === entry.after);
|
|
656
|
+
if (unchanged)
|
|
657
|
+
for (let i = 0; i < pair.length; i++)
|
|
658
|
+
assertRsiEffectivePatch(next[i], effective[i]);
|
|
659
|
+
if (!unchanged) {
|
|
660
|
+
for (const entry of auxiliary(home, journal, ports)) {
|
|
661
|
+
await mkdir(dirname(entry.path), { recursive: true, mode: 0o700 });
|
|
662
|
+
await safeDirectory(dirname(entry.path));
|
|
663
|
+
if (entry.privateFile && ((await lstat(dirname(entry.path))).mode & 0o077) !== 0)
|
|
664
|
+
fail('service environment directory must be private');
|
|
665
|
+
}
|
|
666
|
+
await atomicWrite(journalPath, JSON.stringify(journal));
|
|
667
|
+
try {
|
|
668
|
+
for (const entry of journal.entries)
|
|
669
|
+
await atomicWrite(patchPath(home, entry.profile), entry.after);
|
|
670
|
+
for (const entry of auxiliary(home, journal, ports))
|
|
671
|
+
await writeOptionalFile(entry.path, entry.after);
|
|
672
|
+
const final = pair.map(profile => ports.dump(profile, home));
|
|
673
|
+
for (let i = 0; i < pair.length; i++)
|
|
674
|
+
assertRsiEffectivePatch(next[i], final[i]);
|
|
675
|
+
const recompiled = await ports.compile({ ...input, targetPatch: next[0], coordinatorPatch: next[1],
|
|
676
|
+
targetEffective: final[0], coordinatorEffective: final[1] });
|
|
677
|
+
if (!isDeepStrictEqual(patchValue(recompiled.targetPatch, 'recompiled target'), patchValue(compiled.targetPatch, 'compiled target'))
|
|
678
|
+
|| !isDeepStrictEqual(patchValue(recompiled.coordinatorPatch, 'recompiled coordinator'), patchValue(compiled.coordinatorPatch, 'compiled coordinator')))
|
|
679
|
+
fail('effective profile differs from the compiled deployment');
|
|
680
|
+
if (environments) {
|
|
681
|
+
ports.reloadServices();
|
|
682
|
+
await ports.validateServiceUnits(manifest, environments.target);
|
|
683
|
+
}
|
|
684
|
+
journal.stage = 'applied';
|
|
685
|
+
await atomicWrite(journalPath, JSON.stringify(journal));
|
|
686
|
+
}
|
|
687
|
+
catch (error) {
|
|
688
|
+
await rollback(home, journalPath, journal, ports);
|
|
689
|
+
if (previous)
|
|
690
|
+
await atomicWrite(journalPath, JSON.stringify(previous));
|
|
691
|
+
throw error;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
if (unchanged && environments) {
|
|
695
|
+
ports.reloadServices();
|
|
696
|
+
await ports.validateServiceUnits(manifest, environments.target);
|
|
697
|
+
}
|
|
698
|
+
// Starting is explicit and follows successful composition of both profiles.
|
|
699
|
+
// A failed start retains the applied journal; it never rewrites a live Host.
|
|
700
|
+
if (args.start) {
|
|
701
|
+
await ports.start(pair[1], home);
|
|
702
|
+
await ports.start(pair[0], home);
|
|
703
|
+
}
|
|
704
|
+
return { mode: args.start ? 'services-started' : 'configured', profiles: pair };
|
|
705
|
+
})();
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Onboarding helper: read-only poll of the delivery database until the owner's
|
|
709
|
+
* first Lark DM has produced exactly one active owner binding. It never creates
|
|
710
|
+
* or widens a binding; owner authorization stays with the Lark DSH-CONNECT flow.
|
|
711
|
+
* A missing database is "not yet" because the Host creates it on first message.
|
|
712
|
+
*/
|
|
713
|
+
export async function waitForOwnerDmBinding(input, ports = rsiSetupPorts, sleep = (ms, signal) => new Promise((resolveSleep, reject) => {
|
|
714
|
+
const timer = setTimeout(resolveSleep, ms);
|
|
715
|
+
signal?.addEventListener('abort', () => { clearTimeout(timer); reject(signal.reason); }, { once: true });
|
|
716
|
+
}), now = Date.now) {
|
|
717
|
+
const effective = ports.dump(input.profile, input.dshHome);
|
|
718
|
+
const deadline = now() + input.timeoutMs, interval = input.intervalMs ?? 3_000;
|
|
719
|
+
let bindings = 0;
|
|
720
|
+
for (;;) {
|
|
721
|
+
input.signal?.throwIfAborted();
|
|
722
|
+
try {
|
|
723
|
+
bindings = (await ports.snapshot(effective, input.dshHome)).bindings.length;
|
|
724
|
+
}
|
|
725
|
+
catch {
|
|
726
|
+
bindings = 0;
|
|
727
|
+
}
|
|
728
|
+
if (bindings === 1)
|
|
729
|
+
return { mode: 'ready', bindings };
|
|
730
|
+
if (bindings > 1)
|
|
731
|
+
return { mode: 'ambiguous', bindings };
|
|
732
|
+
if (now() >= deadline)
|
|
733
|
+
return { mode: 'timeout', bindings };
|
|
734
|
+
await sleep(Math.min(interval, Math.max(0, deadline - now())), input.signal);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
export async function runRsiSetup(argv = process.argv.slice(2)) {
|
|
738
|
+
const args = parseRsiSetupArgs(argv);
|
|
739
|
+
if (args.help) {
|
|
740
|
+
process.stdout.write('Usage: dsh-ray-setup --manifest <private.json> [--dsh-home <absolute>] [--apply --confirm-hosts-stopped [--ack-existing-automations] [--start] | --rollback --confirm-hosts-stopped]\n dsh-ray-setup --prepare-source --profile <name> [--dsh-home <absolute>] [--source-repository <local-absolute>]\n dsh-ray-setup --prepare-build --profile <name> [--dsh-home <absolute>] [--source-repository <local-absolute>] [--optional-build]\n dsh-ray-setup --prepare-authorities --profile <name> [--dsh-home <absolute>]\n dsh-ray-setup --install-owner [--upgrade-owner] --profile <name> [--dsh-home <absolute>] [--source-repository <local-absolute>] [--ack-existing-automations] [--enable-ordinary-use-learning [--intake-provider <id> --intake-model <id>] | --skip-memory-learning]\n dsh-ray-setup --wait-owner-dm --profile <name> [--dsh-home <absolute>] [--timeout-seconds <1-3600>]\n dsh-ray-setup --install-local-cohort --profile <name> --source-repository <local-absolute> --bundle <slug> [--bundle <slug> ...] [--dsh-home <absolute>]\n--ack-existing-automations acknowledges active or paused durable rows when enabling or switching an Automation scheduler; accepted only with --install-owner or --apply, and does not bypass inventory drift checks.\n--upgrade-owner authorizes an in-place upgrade/reconfiguration of an existing owner deployment while preserving owner identity, bindings, and SQLite capability history.\n--enable-ordinary-use-learning is accepted with --install-owner (and requires --upgrade-owner to expand an existing installation): it emits the observe-only intake evaluator (rules plus the Delivery or explicit provider/model) and the flagged memory grants. A bare retry preserves the stored opt-in and never expands an existing installation without --upgrade-owner; no admission, revision key or Growth failure pin is added.\n--skip-memory-learning is the mutually exclusive Linux inverse: a fresh install then issues no ordinary-use, memory-learning, review or adoption grant and writes no learner or personal-memory learning configuration. A retry preserves the recorded decision, and an existing installation that already issued those grants refuses to drop them. macOS native owner deployment always keeps the memory-learning chain, so --skip-memory-learning is accepted only for the Linux supervised owner deployment.\nDefault: validate installed profiles and finite authority configuration without changing profiles.\nSource preparation creates a private checkout and release repository for the installed version. Build preparation also prepares private authority tools, signing identities and local release storage on Linux, prepares a pinned native offline runtime and exports its release toolchain/store/cache; --optional-build reports which build prerequisites are unavailable. Authority preparation alone needs neither Docker nor a source checkout and does not issue grants or start Hosts.\n');
|
|
741
|
+
process.stdout.write('macOS: --prepare-build installs a pinned private Node and frozen manifest-only dependencies for offline Seatbelt checks; --optional-build does not suppress native preparation failure. --install-owner configures a native launchd Host with finite memory learning and verified plugin creation. Source replacement/release remains Linux-only.\n');
|
|
742
|
+
process.stdout.write(' dsh-ray-setup --prepare-local-update --profile <name> [--dsh-home <absolute>] [--source-repository <local-absolute>] [--add-memory-learning]\nPrepare local update source and tarballs outside the active Home; does not install or activate them. Linux builds with the native runtime. --add-memory-learning explicitly prepares the disabled learner bundle for a verified pre-owner installation.\n');
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
if (args.waitOwnerDm) {
|
|
746
|
+
const controller = new AbortController();
|
|
747
|
+
const cancel = () => controller.abort(new Error('owner DM wait interrupted'));
|
|
748
|
+
process.once('SIGINT', cancel);
|
|
749
|
+
process.once('SIGTERM', cancel);
|
|
750
|
+
try {
|
|
751
|
+
const result = await waitForOwnerDmBinding({ profile: args.profile, dshHome: args.dshHome,
|
|
752
|
+
timeoutMs: (args.timeoutSeconds ?? 600) * 1_000, signal: controller.signal });
|
|
753
|
+
process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
754
|
+
if (result.mode !== 'ready')
|
|
755
|
+
process.exitCode = 3;
|
|
756
|
+
}
|
|
757
|
+
finally {
|
|
758
|
+
process.off('SIGINT', cancel);
|
|
759
|
+
process.off('SIGTERM', cancel);
|
|
760
|
+
}
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
if (args.prepareLocalUpdate) {
|
|
764
|
+
const controller = new AbortController();
|
|
765
|
+
const cancel = () => controller.abort(new Error('local update preparation interrupted'));
|
|
766
|
+
process.on('SIGINT', cancel);
|
|
767
|
+
process.on('SIGTERM', cancel);
|
|
768
|
+
try {
|
|
769
|
+
const { prepareRsiLocalUpdate } = await import('./rsi-local-update.js');
|
|
770
|
+
const { readRsiLocalCohort } = await import('./rsi-local-cohort.js');
|
|
771
|
+
const sourceRepository = args.sourceRepository ?? (await readRsiLocalCohort({ dshHome: args.dshHome, profile: args.profile })).sourceRepository;
|
|
772
|
+
const prepared = await prepareRsiLocalUpdate({ dshHome: args.dshHome, profile: args.profile, sourceRepository,
|
|
773
|
+
...(args.addMemoryLearning ? { rootExtension: 'memory-learning' } : {}),
|
|
774
|
+
signal: controller.signal });
|
|
775
|
+
process.stdout.write(`${JSON.stringify({ mode: prepared.mode, root: prepared.root, profile: prepared.profile,
|
|
776
|
+
sourceCommit: prepared.source.sourceCommit, version: prepared.cohort.version,
|
|
777
|
+
packages: prepared.cohort.packages.length, receiptDigest: prepared.receiptDigest,
|
|
778
|
+
...(prepared.extension ? { extension: prepared.extension } : {}) })}\n`);
|
|
779
|
+
}
|
|
780
|
+
finally {
|
|
781
|
+
process.off('SIGINT', cancel);
|
|
782
|
+
process.off('SIGTERM', cancel);
|
|
783
|
+
}
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
if (args.installLocalCohort) {
|
|
787
|
+
const controller = new AbortController();
|
|
788
|
+
const cancel = () => controller.abort(new Error('local cohort installation interrupted'));
|
|
789
|
+
process.once('SIGINT', cancel);
|
|
790
|
+
process.once('SIGTERM', cancel);
|
|
791
|
+
try {
|
|
792
|
+
const { installRsiLocalTarget } = await import('./rsi-local-install.js');
|
|
793
|
+
process.stdout.write(`${JSON.stringify(await installRsiLocalTarget({ dshHome: args.dshHome, profile: args.profile,
|
|
794
|
+
sourceRepository: args.sourceRepository, bundles: args.bundles, signal: controller.signal }))}\n`);
|
|
795
|
+
}
|
|
796
|
+
finally {
|
|
797
|
+
process.off('SIGINT', cancel);
|
|
798
|
+
process.off('SIGTERM', cancel);
|
|
799
|
+
}
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
if (args.installOwner) {
|
|
803
|
+
if (process.platform !== 'linux' && process.platform !== 'darwin') {
|
|
804
|
+
process.stdout.write(`${JSON.stringify({ mode: 'not-ready', reason: 'automatic learning requires Linux systemd or macOS launchd' })}\n`);
|
|
805
|
+
process.exitCode = 3;
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
const controller = new AbortController();
|
|
809
|
+
const cancel = () => controller.abort(new Error('owner installation interrupted'));
|
|
810
|
+
process.once('SIGINT', cancel);
|
|
811
|
+
process.once('SIGTERM', cancel);
|
|
812
|
+
try {
|
|
813
|
+
if (process.platform === 'darwin' && args.skipMemoryLearning) {
|
|
814
|
+
fail('--skip-memory-learning is only supported by the Linux supervised owner deployment; macOS always issues the native memory-learning chain');
|
|
815
|
+
}
|
|
816
|
+
const install = process.platform === 'darwin'
|
|
817
|
+
? (await import('./rsi-native-install.js')).installNativeRsiOwnerDeployment
|
|
818
|
+
: (await import('./rsi-install.js')).installRsiOwnerDeployment;
|
|
819
|
+
const result = await install({ dshHome: args.dshHome, profile: args.profile,
|
|
820
|
+
sourceRepository: args.sourceRepository, ackExistingAutomations: args.ackExistingAutomations, signal: controller.signal,
|
|
821
|
+
enableOrdinaryUseLearning: args.enableOrdinaryUseLearning === true,
|
|
822
|
+
skipMemoryLearning: args.skipMemoryLearning === true,
|
|
823
|
+
upgradeExistingOwner: args.upgradeOwner === true,
|
|
824
|
+
...(args.intakeProvider !== undefined ? { intakeProvider: args.intakeProvider } : {}),
|
|
825
|
+
...(args.intakeModel !== undefined ? { intakeModel: args.intakeModel } : {}) });
|
|
826
|
+
process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
827
|
+
process.stderr.write(result.mode === 'ready' ? (process.platform === 'darwin'
|
|
828
|
+
? '原生 macOS 学习已启动并通过就绪检查:持久记忆、新工具独立验证与动态采用。\n'
|
|
829
|
+
: '自迭代双 Host 已启动并通过就绪检查。\n')
|
|
830
|
+
: `普通 supervised 安装已保留;自迭代未就绪:${result.reason}。\n`);
|
|
831
|
+
if (result.mode === 'not-ready')
|
|
832
|
+
process.exitCode = 3;
|
|
833
|
+
}
|
|
834
|
+
finally {
|
|
835
|
+
process.off('SIGINT', cancel);
|
|
836
|
+
process.off('SIGTERM', cancel);
|
|
837
|
+
}
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
if (args.prepareSource || args.prepareBuild || args.prepareAuthorities) {
|
|
841
|
+
await safeDirectory(args.dshHome);
|
|
842
|
+
const controller = new AbortController();
|
|
843
|
+
const cancel = () => controller.abort(new Error('resource preparation interrupted'));
|
|
844
|
+
process.once('SIGINT', cancel);
|
|
845
|
+
process.once('SIGTERM', cancel);
|
|
846
|
+
try {
|
|
847
|
+
const prepared = await withDshHomeLifecycleLock(args.dshHome, async () => {
|
|
848
|
+
const authorities = async () => ({
|
|
849
|
+
...(process.platform === 'linux' ? { authorityRuntime: await prepareRsiAuthorityRuntime({ dshHome: args.dshHome, profile: args.profile, signal: controller.signal }) } : {}),
|
|
850
|
+
authorityResources: await prepareRsiAuthorityResources({ dshHome: args.dshHome, profile: args.profile, signal: controller.signal }),
|
|
851
|
+
});
|
|
852
|
+
if (args.prepareAuthorities)
|
|
853
|
+
return authorities();
|
|
854
|
+
const source = await prepareRsiSourceWorkspace({
|
|
855
|
+
dshHome: args.dshHome, profile: args.profile, version, sourceRepository: args.sourceRepository, signal: controller.signal,
|
|
856
|
+
});
|
|
857
|
+
if (!args.prepareBuild)
|
|
858
|
+
return source;
|
|
859
|
+
const provisioned = { ...source, ...(['linux', 'darwin'].includes(process.platform) ? await authorities()
|
|
860
|
+
: { authorityResourcesUnavailable: 'private authority runtime currently requires Linux' }) };
|
|
861
|
+
try {
|
|
862
|
+
if (process.platform === 'darwin') {
|
|
863
|
+
const { nativeRsiCreationBuild, prepareNativeRsiRuntime } = await import('./rsi-native-runtime-bootstrap.js');
|
|
864
|
+
const build = await prepareNativeRsiRuntime({ dshHome: args.dshHome, profile: args.profile, source,
|
|
865
|
+
signal: controller.signal });
|
|
866
|
+
return { ...provisioned, sourceBuild: build.sourceBuild, creationBuild: nativeRsiCreationBuild(build),
|
|
867
|
+
capabilities: ['native-memory-learning', 'verified-plugin-creation', 'dynamic-tool-adoption'] };
|
|
868
|
+
}
|
|
869
|
+
// Linux prepares the schema 3 native runtime: no Docker CLI, daemon,
|
|
870
|
+
// socket or image is involved.
|
|
871
|
+
const build = await prepareLinuxRsiRuntime({ dshHome: args.dshHome, profile: args.profile, source,
|
|
872
|
+
signal: controller.signal });
|
|
873
|
+
try {
|
|
874
|
+
const release = await prepareRsiReleaseBuildEnvironment({ dshHome: args.dshHome, profile: args.profile,
|
|
875
|
+
build, signal: controller.signal });
|
|
876
|
+
return { ...provisioned, sourceBuild: build, releaseBuild: release };
|
|
877
|
+
}
|
|
878
|
+
catch (error) {
|
|
879
|
+
controller.signal.throwIfAborted();
|
|
880
|
+
if (!args.optionalBuild || !(error instanceof RsiReleaseBuildUnavailableError))
|
|
881
|
+
throw error;
|
|
882
|
+
process.stderr.write(`自迭代发布构建环境未就绪:${error.message}。已保留源码、验证运行时与原生工具链。\n`);
|
|
883
|
+
return { ...provisioned, sourceBuild: build, releaseBuildUnavailable: error.message };
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
catch (error) {
|
|
887
|
+
controller.signal.throwIfAborted();
|
|
888
|
+
if (!args.optionalBuild || !(error instanceof LinuxRsiRuntimeUnavailableError))
|
|
889
|
+
throw error;
|
|
890
|
+
process.stderr.write(`自迭代构建环境未就绪:${error.message}。已保留源码准备结果。\n`);
|
|
891
|
+
return { ...provisioned, buildUnavailable: error.message };
|
|
892
|
+
}
|
|
893
|
+
});
|
|
894
|
+
process.stdout.write(`${JSON.stringify(prepared)}\n`);
|
|
895
|
+
}
|
|
896
|
+
finally {
|
|
897
|
+
process.off('SIGINT', cancel);
|
|
898
|
+
process.off('SIGTERM', cancel);
|
|
899
|
+
}
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
// 平台门控只属于 CLI 外壳:事务逻辑经注入的 ports 隔离 systemctl/服务安装,
|
|
903
|
+
// 单测须能在任意平台跑;真实 systemd 用户服务只在 Linux 可用。
|
|
904
|
+
if (process.platform !== 'linux')
|
|
905
|
+
fail('dual Host setup currently requires Linux systemd user services');
|
|
906
|
+
process.stdout.write(`${JSON.stringify(await configureRsiSetup(args))}\n`);
|
|
907
|
+
}
|
|
908
|
+
//# sourceMappingURL=rsi-setup.js.map
|