@@ -3,39 +3,51 @@ set -euo pipefail
33
44SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd -P) "
55PACKAGE_JSON=" $SCRIPT_DIR /package.json"
6- OUTPUT_DIR=" $SCRIPT_DIR /output"
76PACKAGE_NAME=" dsh-loopx-plugin"
87PROFILE_NAME=" web"
98
109usage () {
1110 cat << 'EOF '
1211Usage: ./install.sh [--help]
12+ ./install.sh --archive FILE --sha256 DIGEST
1313
1414Build and install the DSH LoopX host plugin into the DSH web profile. This
1515installs the automatic LoopX initializer, `/loopx-init` repair command, the
1616same-session Driver, and the loopback-only GoalBar Host/Client faces. LoopX
1717itself is not a prerequisite; starting DSH installs or verifies the LoopX CLI
1818and its DSH skills before the plugin row becomes ready.
1919
20+ To install or roll back to a verified compatible build, pass its retained
21+ archive and independently recorded SHA-256. The current plugin stays installed
22+ while the installer checks and stages the replacement.
23+
2024Environment:
2125 DSH_BIN=/path/to/dsh Override the package-local DSH CLI.
2226EOF
2327}
2428
29+ archive_source=" "
2530case " ${1:- } " in
2631 -h|--help)
2732 [[ " $# " -eq 1 ]] || { echo ' install: --help does not accept arguments' >&2 ; exit 2; }
2833 usage
2934 exit 0
3035 ;;
31- ' ' ) ;;
36+ --archive)
37+ [[ " $# " -eq 4 && " $3 " == --sha256 && " $4 " =~ ^[0-9a-f]{64}$ && -f " $2 " ]] || {
38+ echo ' install: expected --archive FILE --sha256 lowercase-SHA256' >&2
39+ exit 2
40+ }
41+ archive_source=" $2 "
42+ expected_digest=" $4 "
43+ ;;
44+ ' ' ) [[ " $# " -eq 0 ]] || { usage >&2 ; exit 2; } ;;
3245 * )
3346 echo " install: unsupported argument: $1 " >&2
3447 usage >&2
3548 exit 2
3649 ;;
3750esac
38- [[ " $# " -le 1 ]] || { echo ' install: positional arguments are not supported' >&2 ; exit 2; }
3951
4052need () {
4153 command -v " $1 " > /dev/null 2>&1 || {
@@ -46,6 +58,7 @@ need() {
4658
4759need node
4860need pnpm
61+ need tar
4962
5063node -e '
5164 if (!/^\d+\.\d+\.\d+$/.test(process.versions.node)) process.exit(1)
@@ -62,14 +75,38 @@ pnpm_major="${pnpm_version%%.*}"
6275 exit 2
6376}
6477
78+ archive_digest () {
79+ node -e ' const fs = require("node:fs"); const crypto = require("node:crypto"); process.stdout.write(crypto.createHash("sha256").update(fs.readFileSync(process.argv[1])).digest("hex"))' " $1 "
80+ }
81+
82+ restore_dir=" "
83+ pack_dir=" "
84+ cleanup () {
85+ if [[ -n " $restore_dir " ]]; then rm -rf -- " $restore_dir " ; fi
86+ if [[ -n " $pack_dir " ]]; then rm -rf -- " $pack_dir " ; fi
87+ }
88+ trap cleanup EXIT
89+
90+ target_manifest=" $PACKAGE_JSON "
91+ if [[ -n " $archive_source " ]]; then
92+ restore_dir=" $( mktemp -d) "
93+ cat < " $archive_source " > " $restore_dir /verified.tgz"
94+ [[ " $( archive_digest " $restore_dir /verified.tgz" ) " == " $expected_digest " ]] || {
95+ echo ' install: retained archive SHA-256 mismatch' >&2
96+ exit 2
97+ }
98+ target_manifest=" $restore_dir /package.json"
99+ tar -xzOf - package/package.json < " $restore_dir /verified.tgz" > " $target_manifest "
100+ fi
101+
65102PACKAGE_VERSION=" $(
66103 node -e '
67104 const fs = require("node:fs")
68105 const manifest = JSON.parse(fs.readFileSync(process.argv[1], "utf8"))
69106 if (manifest.name !== "dsh-loopx-plugin") throw new TypeError("unexpected package name")
70107 if (typeof manifest.version !== "string" || !manifest.version) throw new TypeError("missing version")
71108 process.stdout.write(manifest.version)
72- ' " $PACKAGE_JSON "
109+ ' " $target_manifest "
73110) "
74111[[ " $PACKAGE_VERSION " =~ ^[0-9]+\. [0-9]+\. [0-9]+ (-[0-9A-Za-z.-]+)? (\+ [0-9A-Za-z.-]+)? $ ]] || {
75112 echo " install: invalid package version: $PACKAGE_VERSION " >&2
@@ -87,10 +124,10 @@ else
87124fi
88125[[ -x " $dsh_bin " ]] || { echo " install: DSH CLI is unavailable: $dsh_bin " >&2 ; exit 2; }
89126dsh_version=" $( " $dsh_bin " --version) "
90- node - " $PACKAGE_JSON " " $dsh_version " << 'NODE ' || exit 2
127+ node - " $PACKAGE_JSON " " $dsh_version " " $target_manifest " << 'NODE ' || exit 2
91128const fs = require('node:fs')
92129const semver = require('node:module').createRequire(process.argv[2])('semver')
93- const manifest = JSON.parse(fs.readFileSync(process.argv[2 ], 'utf8'))
130+ const manifest = JSON.parse(fs.readFileSync(process.argv[4 ], 'utf8'))
94131const version = process.argv[3].trim()
95132const range = manifest.peerDependencies['@deepseek-ai/dsh']
96133if (!semver.satisfies(version, range)) {
@@ -99,48 +136,54 @@ if (!semver.satisfies(version, range)) {
99136}
100137NODE
101138
102- mkdir -p " $OUTPUT_DIR "
103- tarball=" $OUTPUT_DIR /$PACKAGE_NAME -$PACKAGE_VERSION .tgz"
104- pnpm --dir " $SCRIPT_DIR " pack --out " $tarball "
105- [[ -s " $tarball " ]] || { echo " install: expected tarball was not created" >&2 ; exit 1; }
106-
107- " $dsh_bin " plugin --profile " $PROFILE_NAME " add " $tarball " --ignore-scripts
108- profile_dump=" $( " $dsh_bin " --profile " $PROFILE_NAME " --dump-config) " || {
109- echo " install: DSH profile $PROFILE_NAME could not be read back" >&2
139+ # pnpm 10 includes the profile-relative tarball path in an index filename.
140+ # Keeping the archive beside the profile also lets it outlive the checkout.
141+ # Encode the cwd before it enters DSH's line-oriented command output.
142+ profile_probe=' const cwd = require("node:fs").realpathSync.native(process.cwd()).split(require("node:path").sep).join("/"); console.log("loopx-profile-root=" + Buffer.from(cwd).toString("base64"))'
143+ if ! encoded_root=" $( " $dsh_bin " plugin --profile " $PROFILE_NAME " exec node -e " $profile_probe " | sed -n ' s/^loopx-profile-root=//p' ) " ; then
144+ # pnpm 11 loads the profile's pnpmfile before the probe can run.
145+ echo ' install: unable to query the DSH profile; check the DSH error above and the selected DSH_HOME' >&2
110146 exit 1
111- }
112-
113- printf ' %s' " $profile_dump " | node -e '
114- const fs = require("node:fs")
115- const dump = fs.readFileSync(0, "utf8")
116- const rows = [
117- ["loopx-goalbar", "dsh-loopx-plugin"],
118- ["loopx-init-command", "dsh-loopx-plugin/init-command"],
119- ["loopx-driver", "dsh-loopx-plugin/driver"],
120- ["loopx-shadow-observer", "dsh-loopx-plugin/observer"],
121- ]
122- const packageNames = dump
123- .split(/\r?\n/u)
124- .map(line => line.match(/^\s*name:\s+(\S+)\s*$/u)?.[1])
125- .filter(name => name === "dsh-loopx-plugin" || name?.startsWith("dsh-loopx-plugin/"))
126- const expectedNames = rows.map(([, name]) => name)
127- if (JSON.stringify(packageNames) !== JSON.stringify(expectedNames)) {
128- throw new Error(`unexpected package rows: ${packageNames.join(",")}`)
129- }
130- let previous = -1
131- for (const [id, name] of rows) {
132- const position = dump.indexOf(`id: ${id}`)
133- if (position <= previous) throw new Error(`missing or unordered row ${id}`)
134- if (!dump.includes(`name: ${name}`)) throw new Error(`missing module ${name}`)
135- previous = position
136- }
137- ' || {
138- echo ' install: DSH profile readback did not match the Host/Client plugin contract' >&2
147+ fi
148+ profile_root=" $( node -e ' process.stdout.write(Buffer.from(process.argv[1], "base64"))' " $encoded_root " ) "
149+ [[ -n " $profile_root " && -d " $profile_root " ]] || { echo " install: unable to locate DSH profile" >&2 ; exit 1; }
150+ # Node package exports cannot resolve a POSIX profile containing a literal backslash.
151+ if [[ " $profile_root " == * ' \' * ]]; then
152+ echo ' install: Node cannot load plugin exports from this profile path; select a DSH_HOME without a literal backslash' >&2
153+ exit 2
154+ fi
155+ archive_store=" .loopx-archives"
156+ output_dir=" $profile_root /$archive_store "
157+ mkdir -p " $output_dir "
158+ pack_dir=" $( mktemp -d " $output_dir /.pack.XXXXXXXX" ) "
159+ archive_name=" $PACKAGE_NAME -$PACKAGE_VERSION .tgz"
160+ if [[ -n " $archive_source " ]]; then
161+ cp " $restore_dir /verified.tgz" " $pack_dir /$archive_name "
162+ else
163+ pnpm --dir " $SCRIPT_DIR " pack --out " $pack_dir /$archive_name "
164+ fi
165+ [[ -s " $pack_dir /$archive_name " ]] || { echo " install: expected tarball was not created" >&2 ; exit 1; }
166+
167+ # DSH resolves local packages by path. Keep identical rebuilds reusable and
168+ # give changed bytes a new path even when the public package version is fixed.
169+ digest=" $( archive_digest " $pack_dir /$archive_name " ) "
170+ tar -xzf - -C " $pack_dir " < " $pack_dir /$archive_name "
171+ node " $SCRIPT_DIR /scripts/verify-installed.mjs" " $PACKAGE_VERSION " " $pack_dir /package" --staged
172+ mkdir -p " $output_dir /$digest "
173+ tarball=" $output_dir /$digest /$archive_name "
174+ mv -f " $pack_dir /$archive_name " " $tarball "
175+
176+ # Match the native path pnpm uses to discover the workspace. On Windows a
177+ # short project path and a long lockfile path produce an oversized store key.
178+ " $dsh_bin " plugin --profile " $PROFILE_NAME " add " file:$archive_store /$digest /$archive_name " --ignore-scripts " --dir=$profile_root "
179+ profile_dump=" $pack_dir /profile.yml"
180+ " $dsh_bin " --profile " $PROFILE_NAME " --dump-config > " $profile_dump " || {
181+ echo " install: DSH profile $PROFILE_NAME could not be read back" >&2
139182 exit 1
140183}
141184
142- " $dsh_bin " plugin --profile " $PROFILE_NAME " exec node -e ' const manifest = require("dsh-loopx-plugin /package.json"); if (manifest.name !== "dsh-loopx-plugin" || manifest.version !== process.argv[1]) { throw new Error(`installed plugin version does not match ${process.argv[1]}`); } ' " $PACKAGE_VERSION " || {
143- echo ' install: DSH installed package did not match the built version ' >&2
185+ " $dsh_bin " plugin --profile " $PROFILE_NAME " exec node " $SCRIPT_DIR /scripts/verify-installed.mjs " " $PACKAGE_VERSION " " $pack_dir /package" --profile-dump " $profile_dump " || {
186+ echo ' install: DSH installed package did not match the built archive ' >&2
144187 exit 1
145188}
146189
0 commit comments