99# - KVM support (for running the Hyperlight micro-VM)
1010#
1111# First-time setup:
12- # just setup # clones + builds hyperlight-js , installs npm deps
12+ # just setup # builds native addons , installs npm deps
1313#
1414# ─────────────────────────────────────────────────────────────────────
1515
16- # Windows: use PowerShell, replace backslashes for clang compatibility
16+ # Windows: use PowerShell
1717set windows-shell := [" pwsh.exe" , " -NoLogo" , " -Command" ]
1818
19- # In windows we need to replace the backslashes with forward slashes
20- # otherwise clang will misinterpret the paths
21- PWD := replace (justfile_dir (), " \\ " , " /" )
22-
2319# On Windows, use Ninja generator for CMake to avoid aws-lc-sys build issues
2420export CMAKE_GENERATOR := if os () == " windows" { " Ninja" } else { " " }
2521
26- # The hyperlight-js repo URL and ref to build against .
27- # Currently using Simon 's fork with in-flight PRs (binary types, call stats).
28- # TODO: Switch to hyperlight-dev/hyperlight-js main once PRs land upstream.
29- hyperlight-repo := " https://github.com/simongdavies/hyperlight-js. git"
30- hyperlight-ref := " hyperagent"
31- hyperlight-dir := justfile_dir () / " deps" / " hyperlight-js "
22+ # The hyperlight-js workspace root, discovered from Cargo's git checkout .
23+ # The runtime 's Cargo.toml uses a git dep on hyperlight-js-runtime, so Cargo
24+ # already clones the full workspace — we reuse that checkout to build the
25+ # NAPI addon (js-host-api) without a separate git clone.
26+ # Resolved lazily by the resolve-hyperlight-dir recipe.
27+ hyperlight-link := justfile_dir () / " deps" / " js-host-api "
3228
3329# Hyperlight analysis guest (secure code validation in micro-VM)
3430analysis-guest-dir := justfile_dir () / " src" / " code-validator" / " guest"
@@ -37,10 +33,10 @@ analysis-guest-dir := justfile_dir() / "src" / "code-validator" / "guest"
3733runtime-dir := justfile_dir () / " src" / " sandbox" / " runtime"
3834
3935# HYPERLIGHT_CFLAGS needed for building guests that link rquickjs/QuickJS:
40- # -I include/ provides stubs for the hyperlight target (no libc)
41- # -D__wasi__=1 disables pthread support in QuickJS
42- # Uses forward slashes (PWD) so clang works on Windows
43- runtime-cflags := " -I" + PWD + " /deps/hyperlight-js/ src/hyperlight-js-runtime/ include -D__wasi__=1"
36+ # The hyperlight target has no libc, so QuickJS needs stub headers plus
37+ # -D__wasi__=1 to disable pthreads. Uses cargo metadata to find the
38+ # include/ dir from the hyperlight-js-runtime dependency.
39+ runtime-cflags := ` node -e " try{var m=JSON.parse(require('child_process').execSync('cargo +1.89 metadata --format-version 1 --manifest-path src/sandbox/runtime/Cargo.toml',{encoding:'utf8',stdio:['pipe','pipe','pipe']}));var p=m.packages.find(function(p){return p.name===' hyperlight-js-runtime'});if(p)console.log('-I'+require('path').join(require('path').dirname(p.manifest_path),' include')+' -D__wasi__=1')}catch(e){} " 2> /dev/null || echo " " `
4440
4541# Export HYPERLIGHT_CFLAGS so cargo-hyperlight picks them up when building runtimes
4642export HYPERLIGHT_CFLAGS := runtime-cflags
@@ -50,12 +46,29 @@ export HYPERLIGHT_CFLAGS := runtime-cflags
5046# Without this, the default runtime (no ha:ziplib) would be embedded.
5147export HYPERLIGHT_JS_RUNTIME_PATH := runtime-dir / " target" / " x86_64-hyperlight-none" / " release" / " hyperagent-runtime"
5248
53- # Clone (or update) the hyperlight-js dependency at the pinned ref.
54- # Cross-platform: - prefix ignores clone failure (dir already exists).
49+ # Resolve the hyperlight-js workspace root from Cargo's git checkout.
50+ # Uses cargo metadata to find where hyperlight-js-runtime lives, then
51+ # derives the workspace src/ dir (js-host-api is a sibling crate).
52+ # Outputs the workspace root path (parent of src/).
5553[private ]
56- fetch-hyperlight :
57- - git clone --branch " {{ hyperlight-ref}} " --single-branch --depth 1 " {{ hyperlight-repo}} " " {{ hyperlight-dir}} "
58- cd " {{ hyperlight-dir}} " && git fetch origin " {{ hyperlight-ref}} " --depth 1 && git checkout FETCH_HEAD
54+ [unix ]
55+ resolve-hyperlight-dir :
56+ #!/usr/bin/env bash
57+ set -euo pipefail
58+ dir=$(node -e " \
59+ var m=JSON.parse(require('child_process').execSync(\
60+ 'cargo +1.89 metadata --format-version 1 --manifest-path src/sandbox/runtime/Cargo.toml',\
61+ {encoding:'utf8',stdio:['pipe','pipe','pipe'],maxBuffer:20*1024*1024}));\
62+ var p=m.packages.find(function(p){return p.name==='hyperlight-js-runtime'});\
63+ if(p)console.log(require('path').resolve(require('path').dirname(p.manifest_path),'..','..'));\
64+ else{process.stderr.write('hyperlight-js-runtime not found in cargo metadata');process.exit(1)}" )
65+ js_host_api=" ${dir}/src/js-host-api"
66+ if [ ! -d " $js_host_api" ]; then
67+ echo " ❌ js-host-api not found at ${js_host_api}"
68+ echo " Run: cargo +1.89 fetch --manifest-path src/sandbox/runtime/Cargo.toml"
69+ exit 1
70+ fi
71+ echo " $dir"
5972
6073# Install required Rust toolchains and cargo subcommands.
6174# Cross-platform (Linux/macOS/Windows) — no bash required.
@@ -65,19 +78,25 @@ ensure-tools:
6578 rustup toolchain install 1.89 --no-self-update
6679 rustup toolchain install nightly --no-self-update
6780
68- # Build the native hyperlight-js NAPI addon (debug — default)
69- # Depends on build-runtime-release so the custom runtime with native modules
70- # is always embedded. cargo clean -p prevents stale cached builds.
81+ # Build the native hyperlight-js NAPI addon.
82+ # 1. Builds the custom runtime (Cargo git dep fetches hyperlight-js automatically)
83+ # 2. Discovers the hyperlight-js workspace from Cargo's checkout
84+ # 3. Builds the NAPI addon with our custom runtime embedded
85+ # 4. Symlinks deps/js-host-api → checkout/src/js-host-api for npm file: dep
7186[private ]
72- build-hyperlight : fetch-hyperlight (build-runtime-release )
73- - cd " {{ hyperlight-dir}} /src/hyperlight-js" && cargo clean -p hyperlight-js
74- cd " {{ hyperlight-dir}} " && just build
75-
76- # Build the native hyperlight-js NAPI addon (release — optimised)
77- [private ]
78- build-hyperlight-release : fetch-hyperlight (build-runtime-release )
79- - cd " {{ hyperlight-dir}} /src/hyperlight-js" && cargo clean -p hyperlight-js
80- cd " {{ hyperlight-dir}} " && just build release
87+ [unix ]
88+ build-hyperlight target = " debug": (build-runtime-release )
89+ #!/usr/bin/env bash
90+ set -euo pipefail
91+ hl_dir=$(just resolve-hyperlight-dir)
92+ # Clean stale hyperlight-js builds so build.rs re-embeds the runtime
93+ cd " ${hl_dir}/src/hyperlight-js" && cargo clean -p hyperlight-js 2 >/ dev/ null || true
94+ # Build the NAPI addon (inherits HYPERLIGHT_JS_RUNTIME_PATH from env)
95+ cd " ${hl_dir}" && just build {{ if target == " debug" { " " } else { target } }}
96+ # Symlink for npm file: dependency resolution
97+ mkdir -p " {{ justfile_dir ()}} /deps"
98+ ln -sfn " ${hl_dir}/src/js-host-api" " {{ hyperlight-link}} "
99+ echo " 🔗 deps/js-host-api → ${hl_dir}/src/js-host-api"
81100
82101# Build the hyperlight-analysis-guest NAPI addon (debug)
83102[private ]
@@ -89,19 +108,19 @@ build-analysis-guest:
89108build-analysis-guest-release :
90109 cd " {{ analysis-guest-dir}} " && just build release && just build-napi release
91110
92- # Install npm deps (links hyperlight-js and analysis-guest from local dirs )
111+ # Install npm deps (builds native addons, symlinks js-host-api )
93112[private ]
94- install : build-hyperlight build-analysis-guest
113+ install : ( build-hyperlight ) build-analysis-guest
95114 npm install
96115
97116# Install npm deps with release-built native addons
98117[private ]
99- install-release : build-hyperlight- release build-analysis-guest-release
118+ install-release : ( build-hyperlight " release" ) build-analysis-guest-release
100119 npm install
101120
102121# ── First-time setup ─────────────────────────────────────────────────
103122
104- # Clone hyperlight-js, build native addon , install npm deps
123+ # First-time setup: build native addons , install npm deps
105124setup : ensure-tools install
106125 @ echo " ✅ Setup complete — run 'just start' to launch the agent"
107126
@@ -187,18 +206,13 @@ test-analysis-guest:
187206# ── HyperAgent Runtime (native modules) ──────────────────────────────
188207
189208# Build the custom runtime for the hyperlight target (debug)
190- build-runtime : fetch-hyperlight
209+ build-runtime :
191210 cd " {{ runtime-dir}} " && cargo + 1.89 hyperlight build --target-dir target
192211
193212# Build the custom runtime for the hyperlight target (release)
194- build-runtime-release : fetch-hyperlight
213+ build-runtime-release :
195214 cd " {{ runtime-dir}} " && cargo + 1.89 hyperlight build --target-dir target --release
196215
197- # Legacy alias — the standard build now always uses the custom runtime.
198- # Kept for backwards compatibility with existing scripts/docs.
199- build-with-runtime : build
200- @ echo " ✅ (build-with-runtime is now a no-op — 'just build' always uses the custom runtime)"
201-
202216# Lint Rust code in the custom runtime
203217lint-runtime :
204218 cd " {{ runtime-dir}} " && cargo + 1.89 clippy --workspace -- -D warnings
@@ -238,7 +252,7 @@ check: lint-all test-all
238252clean :
239253 rm -rf dist node_modules
240254
241- # Clean everything including the hyperlight-js clone
255+ # Clean everything including deps/ symlinks
242256clean -all: clean
243257 rm -rf deps
244258
0 commit comments