Skip to content

Commit 5bef3cd

Browse files
authored
Merge pull request #10118 from loopx-project/codex/dsh-content-addressed-local-install
Verify rebuilt DSH plugin contents after local installation
2 parents 6f32b2b + 0c2debb commit 5bef3cd

4 files changed

Lines changed: 359 additions & 76 deletions

File tree

‎packages/dsh-loopx-plugin/README.md‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,13 @@ cd packages/dsh-loopx-plugin
740740
DSH_BIN="$(command -v dsh)" ./install.sh
741741
dsh --profile web --dump-config
742742
```
743+
On macOS and Linux, choose a `DSH_HOME` without a literal backslash. Node cannot resolve the installed package exports from such a path.
743744
The installer rejects an incompatible host before changing its profile. A
744745
successful profile contains `loopx-goalbar`, `loopx-init-command`, `loopx-driver`,
745-
and the passive `loopx-shadow-observer` in that order. The installer also reads
746-
the installed package version back. Restart DSH after upgrading a running host; then
746+
and the passive `loopx-shadow-observer` in that order. The installer retains the local
747+
archive under its SHA-256 digest in the profile's `.loopx-archives/` directory
748+
and verifies the installed files against it,
749+
including when you rebuild the same version. Restart DSH after upgrading a running host, then
747750
check the native GoalBar and `/loopx-init` in that same Session. Profile
748751
registration alone does not prove that a Client loaded or that work continued.
749752

@@ -752,7 +755,7 @@ compatible release asset on a network that can reach GitHub, verify its SHA256
752755
against the GitHub release asset's digest, then transfer and install that file:
753756

754757
```bash
755-
dsh plugin --profile web add ./dsh-loopx-plugin-VERSION.tgz --ignore-scripts
758+
dsh plugin --profile web add ./dsh-loopx-plugin-VERSION.tgz --ignore-scripts --dir=.
756759
dsh --profile web --dump-config
757760
```
758761

@@ -764,19 +767,26 @@ retain the previous compatible tarball for the rollback below. Marketplace
764767
catalog correction, release publication, browser qualification, and native
765768
Windows verification remain distinct delivery steps.
766769

770+
DSH runs pnpm inside the selected profile. For local tarball installs and
771+
rollbacks, `--dir=.` makes pnpm resolve that directory explicitly, including
772+
when Windows exposes both short and long names for the same profile.
773+
767774
On Windows, the same `dsh plugin ... add` command accepts a local `.tgz` file;
768-
the Bash source installer is optional. Use `--profile desktop` for a desktop
769-
profile and `--profile web` for a web profile throughout install, readback and
770-
removal. The old beta.5 asset is not a 0.2 compatibility claim. To build the
775+
the Bash source installer is optional. Use `--profile web` throughout CLI
776+
installation, readback and removal for the web profile. DSH 0.2.2-rc.3
777+
reserves the `desktop` profile for the Electron application. The old beta.5 asset is not a 0.2 compatibility claim. To build the
771778
current source using native PowerShell, run `pnpm install --frozen-lockfile
772779
--ignore-scripts` and `npm pack` in this package, then add the resulting `.tgz`
773780
with the native DSH command.
774781

775782
For release qualification, run `pnpm smoke:install` with `DSH_BIN` pointing to
776783
each supported host executable. To qualify an upgrade rather than a fresh
777784
installation, run `pnpm smoke:install --previous-tarball /path/to/retained.tgz`.
778-
It invokes the actual source installer in an isolated profile, verifies the
779-
installed version, rejects an incompatible host before profile operations,
785+
For a same-version upgrade, the smoke reads the installed Driver before and
786+
after running the source installer and requires its bytes to change to those
787+
in the new build. The default run also exercises this path using two Driver
788+
builds with the same version in an isolated profile. It verifies the installed
789+
files, rejects an incompatible host before profile operations,
780790
and removes the plugin. It makes no model calls and does not qualify a browser
781791
or the user's existing profile.
782792

@@ -806,14 +816,22 @@ DSH_LOOPX_RUNTIME="${DSH_AGENTS_HOME:-$HOME/.agents}/runtime/dsh-loopx-plugin"
806816
test -f "$DSH_LOOPX_RUNTIME/loopx_cli.py" && rm -rf -- "$DSH_LOOPX_RUNTIME"
807817
```
808818

809-
To roll back the plugin while preserving LoopX state, remove it and install a
810-
previously retained package tarball, then read the profile back. Replace the
811-
placeholder value with the exact path of the old tarball retained before the
812-
upgrade:
819+
To roll back the web profile, keep the current plugin installed and run the
820+
installer from this source checkout with the retained, compatible package.
821+
Supply the SHA-256 you recorded when verifying the release asset or retaining
822+
the build. Set `DSH_BIN` to the host used for this profile. The installer
823+
checks the digest, package identity, and every public runtime entry point declared
824+
by this source checkout for the target version, plus any additional exports
825+
advertised by the archive. The observer export and profile row are required
826+
from beta.5 onward. Both optional native operation modules are required from beta.7.
827+
It resolves their local JavaScript imports before replacing the current plugin. It retains the
828+
archive in the profile, resolves the installed entry points and compares the
829+
installed files with the archive.
830+
The backup can reside outside the profile, including in a deep directory.
813831

814832
```bash
815-
RETAINED_PREVIOUS_DSH_LOOPX_TARBALL=/absolute/path/to/retained/previous-dsh-loopx-plugin.tgz
816-
dsh plugin --profile web remove dsh-loopx-plugin
817-
dsh plugin --profile web add "$RETAINED_PREVIOUS_DSH_LOOPX_TARBALL" --ignore-scripts
833+
cd packages/dsh-loopx-plugin
834+
DSH_BIN="$(command -v dsh)" ./install.sh --archive "$RETAINED_PREVIOUS_DSH_LOOPX_TARBALL" \
835+
--sha256 "$VERIFIED_PREVIOUS_SHA256"
818836
dsh --profile web --dump-config
819837
```

‎packages/dsh-loopx-plugin/install.sh‎

Lines changed: 87 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,51 @@ set -euo pipefail
33

44
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
55
PACKAGE_JSON="$SCRIPT_DIR/package.json"
6-
OUTPUT_DIR="$SCRIPT_DIR/output"
76
PACKAGE_NAME="dsh-loopx-plugin"
87
PROFILE_NAME="web"
98

109
usage() {
1110
cat <<'EOF'
1211
Usage: ./install.sh [--help]
12+
./install.sh --archive FILE --sha256 DIGEST
1313
1414
Build and install the DSH LoopX host plugin into the DSH web profile. This
1515
installs the automatic LoopX initializer, `/loopx-init` repair command, the
1616
same-session Driver, and the loopback-only GoalBar Host/Client faces. LoopX
1717
itself is not a prerequisite; starting DSH installs or verifies the LoopX CLI
1818
and 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+
2024
Environment:
2125
DSH_BIN=/path/to/dsh Override the package-local DSH CLI.
2226
EOF
2327
}
2428

29+
archive_source=""
2530
case "${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
;;
3750
esac
38-
[[ "$#" -le 1 ]] || { echo 'install: positional arguments are not supported' >&2; exit 2; }
3951

4052
need() {
4153
command -v "$1" >/dev/null 2>&1 || {
@@ -46,6 +58,7 @@ need() {
4658

4759
need node
4860
need pnpm
61+
need tar
4962

5063
node -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+
65102
PACKAGE_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
87124
fi
88125
[[ -x "$dsh_bin" ]] || { echo "install: DSH CLI is unavailable: $dsh_bin" >&2; exit 2; }
89126
dsh_version="$("$dsh_bin" --version)"
90-
node - "$PACKAGE_JSON" "$dsh_version" <<'NODE' || exit 2
127+
node - "$PACKAGE_JSON" "$dsh_version" "$target_manifest" <<'NODE' || exit 2
91128
const fs = require('node:fs')
92129
const 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'))
94131
const version = process.argv[3].trim()
95132
const range = manifest.peerDependencies['@deepseek-ai/dsh']
96133
if (!semver.satisfies(version, range)) {
@@ -99,48 +136,54 @@ if (!semver.satisfies(version, range)) {
99136
}
100137
NODE
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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { readFileSync, readdirSync, realpathSync, statSync } from 'node:fs'
2+
import { createRequire } from 'node:module'
3+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
4+
5+
const packageName = 'dsh-loopx-plugin'
6+
const [version, expected, mode, profileDump] = process.argv.slice(2)
7+
const staged = mode === '--staged'
8+
// Resolve from the staged package itself or from the selected DSH profile.
9+
const owner = createRequire(join(staged ? resolve(expected) : process.cwd(), 'package.json'))
10+
const manifestPath = owner.resolve(packageName + '/package.json')
11+
const root = realpathSync(dirname(manifestPath))
12+
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
13+
if (manifest.name !== packageName || manifest.version !== version) {
14+
throw new Error(`installed plugin version does not match ${version}`)
15+
}
16+
if (staged && root !== realpathSync(expected)) {
17+
throw new Error('staged package manifest resolved outside the archive')
18+
}
19+
20+
// Rollback archives can omit an export declaration as well as its file.
21+
const sourceManifest = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
22+
const sourceRequire = createRequire(import.meta.url)
23+
const semver = sourceRequire('semver')
24+
const hasObserver = semver.gte(version, '0.1.1-beta.5')
25+
const requiredEntries = new Set(Object.keys(sourceManifest.exports))
26+
if (!hasObserver) requiredEntries.delete('./observer')
27+
// These independently loaded tools first shipped in beta.7. Older compatible
28+
// releases still need to resolve every export they actually advertise.
29+
if (semver.lt(version, '0.1.1-beta.7')) {
30+
requiredEntries.delete('./native-operation')
31+
requiredEntries.delete('./native-operation-context')
32+
}
33+
const entries = new Set([...requiredEntries, ...Object.keys(manifest.exports)])
34+
const files = [...entries].map(entry =>
35+
owner.resolve(entry === '.' ? packageName : packageName + entry.slice(1)))
36+
const ts = staged ? sourceRequire('typescript') : undefined
37+
const checked = new Set()
38+
for (const file of files) {
39+
const path = realpathSync(file)
40+
const local = relative(root, path)
41+
if (isAbsolute(local) || local === '..' || local.startsWith('..' + sep) || !statSync(path).isFile()) {
42+
throw new Error(`expected a runtime file inside ${packageName}: ${file}`)
43+
}
44+
if (staged && path.endsWith('.js') && !checked.has(path)) {
45+
checked.add(path)
46+
// Inspect relative imports in the build without executing archive code.
47+
const imports = ts.preProcessFile(readFileSync(path, 'utf8'), true, true).importedFiles
48+
for (const { fileName } of imports) {
49+
if (fileName.startsWith('./') || fileName.startsWith('../')) {
50+
files.push(resolve(dirname(path), fileName))
51+
}
52+
}
53+
}
54+
}
55+
56+
function verifyDirectory(source, target) {
57+
for (const entry of readdirSync(source, { withFileTypes: true })) {
58+
const from = join(source, entry.name)
59+
const to = join(target, entry.name)
60+
if (entry.isDirectory()) verifyDirectory(from, to)
61+
else if (!readFileSync(from).equals(readFileSync(to))) {
62+
throw new Error(`installed package content differs: ${relative(root, to)}`)
63+
}
64+
}
65+
}
66+
67+
if (mode === '--profile-dump') {
68+
const dump = readFileSync(profileDump, 'utf8')
69+
const rows = [
70+
['loopx-goalbar', packageName],
71+
['loopx-init-command', packageName + '/init-command'],
72+
['loopx-driver', packageName + '/driver'],
73+
]
74+
if (hasObserver) rows.push(['loopx-shadow-observer', packageName + '/observer'])
75+
const names = dump.split(/\r?\n/u)
76+
.map(line => line.match(/^\s*name:\s+(\S+)\s*$/u)?.[1])
77+
.filter(name => name === packageName || name?.startsWith(packageName + '/'))
78+
if (JSON.stringify(names) !== JSON.stringify(rows.map(([, name]) => name))) {
79+
throw new Error(`unexpected package rows: ${names.join(',')}`)
80+
}
81+
let previous = -1
82+
for (const [id] of rows) {
83+
const position = dump.indexOf(`id: ${id}`)
84+
if (position <= previous) throw new Error(`missing or unordered row ${id}`)
85+
previous = position
86+
}
87+
}
88+
89+
if (!staged) verifyDirectory(expected, root)

0 commit comments

Comments
 (0)