Skip to content

[Pi] Cold start: pi-magic-context module import ~3.8s (7.7MB bundle dominates startup) #242

Description

@WAY29

Short description

[Pi] @cortexkit/pi-magic-context cold-starts ~3.8s on module import (dominates Pi startup)

What happened?

On macOS arm64, starting Pi with npm:@cortexkit/pi-magic-context installed makes CLI startup ~4s+ slower. Pi's built-in PI_TIMING=1 attributes almost all of that to the Magic Context package's module import, not factory init and not other local extensions.

Expected: Plugin load should not dominate interactive CLI cold start (ideally well under a few hundred ms, or defer heavy deps).

Observed: Magic Context alone accounts for ~89% of extension load time in a typical Pi install with many other extensions present.

Repro

# Full startup timings
PI_TIMING=1 PI_OFFLINE=1 pi -p "ok" --no-session

# Baseline without any discovered extensions
PI_TIMING=1 PI_OFFLINE=1 pi --no-extensions -p "ok" --no-session

Measured (Pi 0.82.0, plugin 0.32.4, Node v24.8.0, macOS 15.3.1 arm64)

With extensions (createAgentSessionRuntime ≈ total extension load):

--- Startup Timings: main ---
  createAgentSessionRuntime: 4330ms
  TOTAL: 4336ms

--- Startup Timings: extensions ---
  ...other extensions: mostly <30ms each; a few in the ~100ms range...
  ~/.pi/agent/npm/node_modules/@cortexkit/pi-magic-context/dist/index.js module import: 3787ms
  ~/.pi/agent/npm/node_modules/@cortexkit/pi-magic-context/dist/index.js factory: 41ms
  TOTAL: 4294ms

Without discovered extensions:

--- Startup Timings: main ---
  createAgentSessionRuntime: 43ms
  TOTAL: 49ms

Summary from the same run:

item time
@cortexkit/pi-magic-context module import ~3787ms
@cortexkit/pi-magic-context factory ~41ms
all other extensions combined ~466ms
extensions TOTAL ~4294ms
Pi startup without discovered extensions ~49ms

Likely cause

Installed package contents:

  • @cortexkit/pi-magic-context@0.32.4
  • dist/index.js7.7MB single ESM bundle
  • dist/subagent-entry.js ≈ 4.2MB
  • Build externalizes @huggingface/transformers, but the main bundle still eagerly evaluates a large inlined quickjs payload (many quickjs symbols in the bundle)
  • Declared deps also pull a heavy local-embedding stack (@huggingface/transformers, onnxruntime-node ~210MB on disk, etc.)

This matches the timing profile: cost is almost entirely module import / evaluate, not the factory body (~40ms).

Note: this report is about cold-start import time, not runtime transform lag (#224) or idle TUI CPU (#200).

Suggested directions (not prescriptions)

Any of these would help Pi users a lot:

  1. Split/lazy-load quickjs and other heavy subtrees so the Pi extension factory path doesn't parse a 7–8MB bundle on every process start
  2. Defer embedding/transformers/onnx work until first actual embedding use (or when embedding.provider !== "off")
  3. Ship a thinner Pi entrypoint that only registers hooks/tools, with optional heavy features behind dynamic import()
  4. Document a "lightweight / no local embedding" install path if one already exists

Happy to re-run timings against a canary build.

Diagnostics

## Environment
- Pi plugin: @cortexkit/pi-magic-context@0.32.4
- Pi: 0.82.0
- OS: darwin arm64 (macOS 15.3.1)
- Node: v24.8.0

## Configuration
Config from `~/.config/cortexkit/magic-context.jsonc`:

{
  "$schema": "https://raw.githubusercontent.com/cortexkit/magic-context/master/assets/magic-context.schema.json",
  "historian": {
    "model": "<redacted>"
  },
  "dreamer": {
    "model": "<redacted>"
  },
  "sidekick": {
    "disable": true
  },
  "embedding": {
    "provider": "local",
    "model": "Xenova/all-MiniLM-L6-v2"
  }
}

## Package install
- settings packages includes: `npm:@cortexkit/pi-magic-context`
- resolved path: `~/.pi/agent/npm/node_modules/@cortexkit/pi-magic-context`
- dist/index.js size: ~7.7MB

Plugin version

0.32.4

OpenCode version

N/A (Pi only)

Platform

macOS arm64

Client

Pi

Log output (optional)

# Command
PI_TIMING=1 PI_OFFLINE=1 pi -p "ok" --no-session

# Key lines
createAgentSessionRuntime: 4330ms
@cortexkit/pi-magic-context/dist/index.js module import: 3787ms
@cortexkit/pi-magic-context/dist/index.js factory: 41ms
extensions TOTAL: 4294ms

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions