Skip to content

perry: experimental patches for context-mode native compilation - #1

Open
bradmccormack wants to merge 3 commits into
mainfrom
perry-native-patches
Open

perry: experimental patches for context-mode native compilation#1
bradmccormack wants to merge 3 commits into
mainfrom
perry-native-patches

Conversation

@bradmccormack

Copy link
Copy Markdown
Owner

Experimental patches for context-mode native compilation

This PR is targeted at the personal fork bradmccormack/perry, not upstream PerryTS/perry. It's the same experimental work previously raised (inadvertently) against upstream as PR PerryTS#7225, which has been closed with an apology comment.

Experimental, opt-in. This is a set of small forward-compat patches to perry-hir that let the auto-optimizer's dyn-eval interpreter ride along for KnownLibraryCodegen (ajv, fast-json-stringify, find-my-way) call sites, and that track const F = Function-style aliases of the global Function constructor through the globalThis.Function path. Together they unblock compiling context-mode (a Node.js MCP server) into a native 16 MB binary with node:sqlite + FTS5 + HTTP MCP transport working end-to-end.

Full write-up — including the 7 upstream Perry issues worked around and the file-level diff inventory — is in CONTEXT-MODE-COMPAT.md at the repo root.

Why these patches exist

A real-world downstream consumer (context-mode, the MCP server that lives next to this checkout) compiles fine into Node.js but runs into three concrete gaps in the current perry-hir:

  1. The auto-optimizer refuses to link the dyn-eval interpreter when only KnownLibraryCodegen (bucket-2) sites are present. It only consults has_deferred_dynamic_code_sites(). ajv's new Function() then throws at the first call.
  2. Zod v4 (and any user code) does const F = Function to keep the global Function constructor reachable. perry-hir did not recognise that as a Function alias, so new F(...) degraded to a user-class new and lost the dyn-eval route.
  3. globalThis.Function = <local> style aliases (also a real downstream pattern) hit the same gap.

What changes

File Diff Purpose
crates/perry-hir/src/eval_classifier.rs +21 / -0 Add KNOWN_CODEGEN_SITE_COUNT + has_known_codegen_sites() so the auto-optimizer can opt into dyn-eval when only bucket-2 sites are present.
crates/perry-hir/src/destructuring/var_decl/alias_tracking.rs +2 / -1 Add a 'Function' arm to is_global_this_value so globalThis.Function aliases are recognised.
crates/perry-hir/src/lower/expr_new.rs +9 / -11 function_alias resolution via resolve_class_alias — recognise const F = Function as the global Function constructor.
crates/perry-hir/src/lib.rs +3 / -2 Re-export has_known_codegen_sites from the crate root.
crates/perry/src/commands/compile/optimized_libs/freshness.rs +5 / -2 knowngen= key in the auto-opt cache + has_dyn_eval || has_known gate so the dyn-eval interpreter is included in the cache key when the new bucket-2 site detector fires.
CONTEXT-MODE-COMPAT.md new Compatibility report — 7 upstream issues found, the 5 patches above, all 11 tools verified end-to-end, and the Perry GC bug investigation that was deferred in favour of the incremental-port approach.
changelog.d/<this-pr>-context-mode-patches.md new Per-PR changelog fragment. Filename will be set to the assigned PR number once gh pr create returns.

Verification

context-mode is currently built and shipped at:

  • Binary: context-mode 16 MB, optimized release (lto=true, opt-level=3, strip=true)
  • Tools (all 11 verified): ctx_index, ctx_search, ctx_execute, ctx_execute_file, ctx_batch_execute, ctx_fetch_and_index, ctx_stats, ctx_doctor, ctx_purge, ctx_upgrade, ctx_insight
  • Transport: HTTP on 127.0.0.1:9999 (configurable via CONTEXT_MODE_PORT)
  • DB: node:sqlite per-pid FTS5 at /tmp/context-mode-<pid>.db

The 11-tool verified end-to-end trace and the build commands are in CONTEXT-MODE-COMPAT.md.

What is unchanged

  • No new perry runtime features; only the HIR + the auto-opt freshness key.
  • All 5 patches are surgical — no refactor, no rename, no API break.
  • KNOWN_CODEGEN_SITE_COUNT is pub(crate) — no public-API change.
  • Upstream CHANGELOG.md is untouched (per perry's freeze policy; fragment goes in changelog.d/).

Scope note

These are local, fork-only patches. The earlier PR PerryTS#7225 against PerryTS/perry was opened by mistake and has been closed. This PR is the corrected target.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant