Plan: ship each extension as one bundled file

Closes: #801. Relates to: #831, #832 (the other start-up costs found on the way), ADR-002 (upstream consumed unmodified). Branch: feat/bundle-extensions. Status: Done (2026-11-16) — a cold start on the Windows box 1.4-1.5 s where it was 11-17 s; not released yet; no seat has worked a day on the bundled build

Context

The operator’s report: pi takes about ten seconds to start on Windows, bare, in a new project. Measured on the Windows box (panther) and recorded on #801:

Question Result

What a slow start is

pi’s own TOTAL 18.9 s and 20.5 s with all extensions (extensions 18.0 s and 19.4 s); the start right after, 1.7-2.0 s; with --no-extensions, 0.09 s.

Where the time goes

Synchronous first reads of files: 74 % of a cold start in the profile. A cold start reads about 1,500 code files; each costs about 10 ms.

Who charges it

Windows Defender. A plain program reading a freshly copied tree of 16,284 files: 125-186 s, Defender using more than one core throughout; the same read of a tree under an exclusion: 4 s; a second read: 1.2-1.7 s.

When it is charged

On the first read after a file is written, and again after Defender’s configuration changes (seen twice). After a signature update: one coincidence, not confirmed.

Per file or per byte

Per file. 4.8 MB as one file: 0.25 s; as 500 files: 7.2-7.5 s. About 10-15 ms per file plus about 50 ms per MB.

Which files

1,512 on the code path: our packages 485 (32 %), the three third-party extensions 672 (44 %), their dependencies 358 (24 %). Every .ts file is read twice cold: the source, and its entry in pi’s transpile cache (552 entries).

How it loads today: packages/pi/package.json lists eleven loaders under pi.extensions; each is a one-line re-export (packages/pi/loaders/). pi loads an extension through jiti (dist/core/extensions/loader.js in pi: createJiti(…, { moduleCache: false, alias })), which transpiles every .ts it meets and serves pi’s own modules (@earendil-works/*, typebox) through its alias table. Those host modules are not installed beside the extensions (pi installs with --omit=peer), so only code that passes through jiti can import them.

The operator’s rulings (2026-11-16): bundle everything, third-party extensions and dependencies included; and the installed tree shrinks in the same change. Today an install is 246 packages, 16,263 files, 153 MB.

Design

D1. Two files per extension. The build writes, for each of the eleven extensions, into packages/pi/dist/:

  • NN-name.ts, a generated shim of about fifteen lines, and the file pi.extensions names. jiti transpiles it, so its static imports of the host modules resolve through pi’s alias table as today.

  • NN-name.<hash>.mjs, the bundle: every module the extension reaches, ours and third-party, as one file whose only export is instantiate(require, root). It imports nothing by name. <hash> is the first 16 hex digits of the sha256 of its content.

The shim’s default export is the extension factory: it imports the bundle, calls instantiate with a require that answers the host module names from the shim’s own imports and everything else from createRequire(import.meta.url), and calls the factory instantiate returns. pi awaits a factory that returns a promise (ExtensionFactory in pi’s types.d.ts).

D2. Why the bundle is a function. Read from jiti 2.7.0’s eval_evalModule, not yet run: a .mjs file imported asynchronously is loaded by Node itself, with no transpile and no cache entry, provided the native import succeeds. A bundle that imported @earendil-works/pi-coding-agent by name would fail natively and fall back to jiti’s transpile, measured here at 11.4 s for the eleven bundles (7.1 s of it pi-web-access). So the bundle takes its host modules as an argument. A function also keeps /reload as it is: each load calls instantiate again and gets fresh module state, as moduleCache: false gives today, and the hash in the file name makes a reload after pi update import the new file rather than Node’s cached old one (the fault #579 fixed for pivot-wire).

D3. Bundler: rolldown, pinned exact as a root devDependency. Compared on this tree, in memory:

rolldown 1.2.18 esbuild 0.28.2 rollup 4

Status

1.x since 2026-05

0.x

stable

Builds all eleven

yes, no plugin; 13.2 MB, under a second

nine; the two @gotgenes entries need a plugin for their #src/* imports

not tried: needs three plugins and the TypeScript compiler

A bundled CommonJS dependency calling require("fs")

createRequire shim

a shim that throws in ESM output (esbuild issue 1921, open)

plugin option

Per-module transform hook with the module id

yes

onLoad

yes

Already in the lockfile

yes (through vite)

yes (through vite)

no

rolldown: the only one that builds all eleven as they stand. Output is CommonJS (format: "cjs", codeSplitting: false), wrapped by a banner and footer into export default function instantiate(require, __root) { … return module.exports; }. Not minified, so a stack trace stays readable.

D4. A module’s own location, and the files beside it. Sixteen of our modules and a few third-party ones find files beside themselves (modes.json, prompts/, guidance/, agents/, models.json, helper.mjs, their own package.json, the permission system’s config/). The build copies those files to packages/pi/dist/assets/<package name>/<path in package>, and a transform plugin replaces import.meta.url, import.meta.dirname, dirname and filename in each module with the place that module would have under dist/assets/, computed from the root the shim passes (its own directory plus assets). So a module finds its neighbours by the same relative path as today. Which files are copied: for a @gadhs package, every file npm pack --dry-run --json lists for it that is not a bundled module; for a third-party package, the paths extensions.json names for it (today: the permission system’s config/, schemas/ and package.json; pi-web-access’s CHANGELOG.md and package.json). The build writes each replaced module and each copied file to dist/locations.json.

D5. What stays out of the bundle. The host modules (D1), and three packages that stay real dependencies of @gadhs/pi because they are loaded as files, not as modules: web-tree-sitter and tree-sitter-bash (their .wasm is found with require.resolve by pi-bash and by the permission system) and @gadhs/pivot-wire (CommonJS and wasm, required fresh on each reload, #579). A createRequire at a rewritten location under dist/assets/ resolves them by walking up to the flat node_modules, as today. Any other require left in a bundle fails the build unless extensions.json lists it as optional (absent today as well: canvas, glimpseui) or inert (text inside a code generator, never called: ajv/dist/runtime/validation_error, ajv-formats/dist/formats, to be confirmed in the spike). A dynamic import() of a host module left in the output (measured: two in pi-web-access) is rewritten by the plugin to a resolved require.

D6. Entries. packages/pi/extensions.json replaces packages/pi/loaders/: an ordered list of { name, package, entry }. The build resolves entry as a file inside the package’s directory, since the @gotgenes packages' exports do not expose their src/index.ts. pi.extensions in the manifest lists ./dist/NN-name.ts in the same order.

D7. When it builds. node tools/task.mjs bundle runs the build. packages/pi gains a prepack script that runs it, so pnpm pack (consumer-sim, the model battery’s stack, the distribution test) and pnpm publish (CI) ship what was just built. validate runs bundle before the tests. dist/ is already ignored by git and Biome.

D8. Dependencies. @gadhs/pi’s `dependencies become the three packages of D5, pinned exactly. The eight @gadhs packages and the three third-party extensions listed there today move to devDependencies, and @gadhs/pi-bash and @gadhs/pi-workspace (reached only through the others today) are added to them, so staleDependents sees all ten: the build needs them and an install does not. An install is then six packages (@gadhs/pi, the three, and tree-sitter-bash’s two) instead of 246. The other `@gadhs packages are still published as they are. A consequence to state in the docs: third-party code inside a bundle is at the version pnpm-lock.yaml held when the release was built, where today npm resolves each range on the day of the install; a dependency’s fix reaches users with our next release. staleDependents in tools/lib/release.mjs reads @gadhs/pi’s `devDependencies as it reads other packages' dependencies, since a bundle goes stale the same way a pin does: a release that moves any @gadhs package must move @gadhs/pi.

D9. Notices. The bundles carry 111 third-party packages (77 MIT, 17 BSD-2-Clause, 8 ISC, 6 Apache-2.0, 2 BSD-3-Clause, 1 Unlicense). The build writes dist/THIRD-PARTY-NOTICES.txt from each bundle’s module list: name, version, licence and licence text per package. It fails on a package with no licence or one outside that list of six.

Scope

In: tools/lib/bundle.mjs (the build, the plugin, the notices), tools/bundle.mjs (the command), tools/lib/test/bundle.test.mjs, tools/task.mjs (the bundle verb; validate runs it), packages/pi/extensions.json, packages/pi/package.json (pi.extensions, files, prepack, dependencies and devDependencies, the //order and //deps notes), packages/pi/loaders/ (removed), packages/pi/test/ (distribution.test.ts, one-bash.test.ts, web-tools.test.ts, shipped-imports.test.ts’s comment, a `globalSetup in vitest.config.ts), packages/pi-agents/test/helper-roles.test.ts, tools/consumer-sim.mjs (what it installs; its loader, flat-root, pi-bash and pi-remote checks), tools/model-battery/lib/stack.mjs (what it installs), tools/lib/release.mjs and its test (staleDependents), the root package.json and pnpm-lock.yaml (rolldown), docs/modules/ROOT/pages/architecture.adoc, docs/modules/ROOT/pages/local-dev.adoc, CHANGELOG.adoc.

Out, each an issue filed at approval:

  • Dropping the last three dependencies by copying their wasm into dist/assets/: the permission system finds its wasm by package name, and its source is not ours to change.

  • Whether the ten @gadhs packages should still be published on their own once nothing installs them.

  • A Defender exclusion as guidance for Windows boxes. Not needed once a start reads few files; an administrator’s choice either way.

  • The same treatment for a user’s own extensions, which is pi’s to do.

  • pi built as a single binary (Bun or Node SEA): by reading, jiti then takes another path for the bundle. Not a way we install pi; not tested.

Risks

Risk Mode Mitigation

jiti does not load the bundle natively as D2 reads it, and transpiles it: a first start after each update costs seconds more than today

silent

Step 1 of Delivery is a spike on two extensions that looks for a transpile-cache entry named after the bundle. One found: stop and report to the operator before anything else is built. The consumer-sim check of the same name holds it afterwards.

A file a module reads beside itself was not copied to dist/assets/

fail-closed

The extension throws at the first read. For our packages the copy is the whole pack list, so nothing can be left out; for the two third-party packages the list is by hand, and consumer-sim’s full run (permission config seeded, web access loaded) and the Windows run meet it. consumer-sim checks every entry of locations.json against the installed tree.

An update from a release that had 246 packages leaves the old ones installed, and a start still reads none of them but the tree does not shrink

silent

consumer-sim installs 0.162.1 from the registry, then the packed branch over it, and counts the packages left: six.

A dependency’s security fix no longer arrives with a fresh install (D8)

silent

Stated in architecture.adoc and the CHANGELOG. The dependency-update routine already in use moves the lockfile; a release carries it.

Someone installs @gadhs/pi-modes or another @gadhs package alone and expects it unchanged

fail-open

They are published as before; nothing in them changes.

The ten @gadhs packages are still published but no longer installed by consumer-sim, so a fault in one’s own packed form goes unseen

silent

consumer-sim still packs all eleven, and shipped-imports.test.ts still checks each one’s packed imports. Whether to go on publishing them is the second Out issue.

A bundled CommonJS dependency relied on sloppy mode; the bundle is a module and so strict

fail-closed

The build parses each bundle as a module. What only fails when run is met by the full consumer-sim, which loads every extension and makes live calls, and by the Windows run.

The permission system is in two bundles (its extension, and the copy pi-modes reaches through import("@gotgenes/pi-permission-system")), and the authorizer registers on the wrong copy: the judge is never asked

fail-open

Both copies already exist today (one jiti instance per extension) and meet through a globalThis registry. consumer-sim’s existing check "the guard chain is live on the installed stack" must pass on the bundled install; it is run before merge and in the release rehearsal.

Load order changes because factories now return promises

fail-open

pi awaits each factory before the next (await factory(load.api) in its loader). The distribution test holds the manifest’s order; consumer-sim’s "chain is seeded" check holds modes before the permission system.

A reload after pi update runs the old bundle from Node’s module cache

silent

The hash in the bundle’s name (D2). Unit test: two builds of different sources give different names, and the shim names the bundle it was built with.

The build is not reproducible, so a bundle’s name differs between two builds of one tree

silent

Measured: two generations in one process are byte-equal and the output holds no absolute path. Test: two builds from clean give equal hashes. The drift comparison in release prepare (tools/lib/ship-run.mjs) covers only packages a release does not move, and @gadhs/pi moves in every release, so an unequal build would refuse no release; it would only make two packs of one tree differ.

A release changes a @gadhs package and ships @gadhs/pi with the old bundle

silent

The bundle is built at pack time from the workspace, never committed; staleDependents reads the devDependencies (D8), with a test.

A third-party licence that forbids redistribution enters through an update

fail-closed

The build fails on a licence outside the list of D9.

The published tarball grows by about 13 MB while the install shrinks from 153 MB

fail-open

Stated in the CHANGELOG. A scan of it costs under a second by the per-MB figure.

A stack trace names a line in a bundle, not a source file

silent

rolldown marks each module’s region with its path in a comment; stated in local-dev.adoc.

Tests

Contract (what pi and an install read):

  • pi.extensions lists ./dist/NN-name.ts for every entry of extensions.json, in its order: modes first, agents before subagents, remote last (the existing order assertions, moved to the new names).

  • The packed tarball holds, for every entry, the shim and exactly one bundle whose name carries the hash of its content, plus locations.json, every file it lists under assets/, and the notices file.

  • The packed manifest’s dependencies are exactly the three of D5.

  • staleDependents names @gadhs/pi when a package among its devDependencies moves and it is not itself named.

  • A bundle’s text has no import of any module, no import.meta, and one export. A shim’s text imports only host modules, node:module, and its own bundle.

Regression (the cases that would have caught the slow start):

  • consumer-sim, on the packed install: after one start of pi with all extensions, the transpile cache holds eleven entries (the shims) and none named after a bundle or a package’s source file.

  • consumer-sim: the number of distinct files under the installed tree that a start opens is at most 60 (today about 960), counted by a --require hook that records paths only.

  • consumer-sim: a fresh install is six packages; an install of 0.162.1 updated to the packed branch is six packages.

Unit (tools/lib/test/bundle.test.mjs, on a fixture package):

  • The location plugin gives a module the path it has in the installed tree for each of the four spellings, and leaves a module that uses none untouched.

  • A host module name required inside the bundle is answered by the object the shim passed; any other name goes to the fallback require.

  • A dynamic import() of a host module is rewritten; of a relative module, inlined.

  • Two builds of the same sources give the same hash; a changed source gives another.

  • A package with no licence, or one outside the list, fails the build and is named.

  • Calling instantiate twice gives two module states.

Existing tests rewritten, not deleted:

  • The six cases of "the loader-shim architecture" in packages/pi/test/distribution.test.ts: one entry per bundled package; the static re-export rule becomes the shim and bundle rules above; the order; each entry naming a real package; each vendored entry’s file existing in its installed dependency (#444); our own entries' shims giving the workspace packages' factories.

  • packages/pi/test/one-bash.test.ts imports the two factories from @gadhs/pi-ssh and @gadhs/pi-workflow, which is what its loaders re-exported; it passes pi-ssh an environment, which a shim does not take. packages/pi/test/web-tools.test.ts loads dist/40-web-access.ts. packages/pi-agents/test/helper-roles.test.ts asserts the same order on the new names.

  • Tests that read packages/pi/dist/ get it from a vitest globalSetup that runs the build, so a hand-started test run does not depend on an earlier bundle.

  • consumer-sim installs the @gadhs/pi tarball alone (it still packs all eleven); tools/model-battery/lib/stack.mjs likewise. Its checks: "each loader RESOLVES from its installed location" becomes the locations.json check; "dependencies land in ONE FLAT root" becomes the six-package check; "every declared resource survives packing" stays. The pi-remote check already drives /remote-control status through pi; its gate changes from the package’s directory to dist/60-remote.ts. The pi-bash check resolves the two wasm files with createRequire from the location the bundle gives parser.ts (dist/assets/@gadhs/pi-bash/parser.ts), as the bundled parser does, and parses a line; its gate changes the same way. Neither may report a skip on the packed branch: a skipped check there fails the run.

Measured before merge, prediction on file first, by the Windows seat on panther and here on luchs, installed 0.162.1 against the packed branch, interleaved:

  • Warm start, pi’s TOTAL, five rounds: the branch is not slower than today by more than 10 % on either box.

  • Cold start on panther (the tree deleted and copied back, then the first start), three rounds: today’s row against the branch. Predicted: under 3 s where today is 18-20 s. Above 5 s is a miss to explain before merge.

  • /reload after replacing the installed package with a second build: the new code runs.

Not tested: pi as a single binary; macOS.

Delivery

  1. Spike, on the branch, two extensions (15-guidance, the smallest, and 50-vertex, the one with CommonJS dependencies and an asset): build, install with consumer-sim’s stage, start pi, check the first Risks row. Report the result on #801 before going on.

  2. The build and its unit tests; then extensions.json, the manifest and the distribution test; then consumer-sim’s checks; then the docs. One branch, a commit for each.

  3. Canonical doc: architecture.adoc, "What is in the box" and a Key decisions line: what a shim and a bundle are, why the bundle takes its host modules as an argument, that upstream source is bundled unmodified apart from the location rewrite, and that bundled dependencies are at the lockfile’s versions (D8). local-dev.adoc gains the bundle verb and how to read a stack trace.

  4. CHANGELOG, under Unreleased: a Changed entry saying a start reads eleven files instead of about 1,500, an install is six packages instead of 246, the measured cold and warm rows, the tarball’s size, and D8’s consequence for dependency fixes.

  5. node tools/task.mjs validate, full consumer-sim, the Windows run above; MR with Closes #801, merge with squash off, the merge SHA on the issue; the out-of-scope issues filed and linked there.

  6. Released as a minor; @gadhs/pi moves, no other package changes. The Windows seat updates from 0.162.1 and reports the package count and a cold start before the release is announced.

Errata

Erratum (2026-11-16): D3’s wrapper is added by the build after generation, not as a rolldown banner and footer: rolldown parses a banner as part of the CommonJS output and refuses its import lines.

Erratum (2026-11-16): D4 copies every packed file of a @gadhs package that is not TypeScript, rather than "not a bundled module": a TypeScript file that is only types is in no bundle and is not needed beside one.

Erratum (2026-11-16): D5’s check of the requires left in a bundle reads the chunk generated without comments, since dependencies' comments are full of example requires; and module paths in a bundle’s comments are anchored to the workspace root, since the bundler wrote them relative to the working directory and a bundle’s name then depended on where the build ran.

Erratum (2026-11-16): the consumer simulation does not start on Windows (it cannot spawn pnpm there, on main as well), so the Windows seat staged the install by hand and ran the checks of "Measured before merge" itself; the reload row was run in RPC mode.

Erratum (2026-11-17): D2 was wrong that each load should call instantiate again "as moduleCache: false gives today". pi caches an extension’s factory for a process, so before the bundles every session in a process shared one module; a fresh instance per factory call gave each helper session its own, and a parent could no longer price its helpers (#845, in 0.163.0). The shim now instantiates once.

Edit this page · latest