Files
oh-my-pi/packages/coding-agent/src/mnemopi/embed-protocol.ts
T
roboomp 65782a75e6 fix(mnemopi): carry (model, cacheDir) on every embed for respawn safety
After `shutdownMnemopiEmbedClient()` clears `#worker` (session dispose, or
worker crash), mnemopi still holds the cached `LocalEmbeddingModel` wrapper.
The next embed re-spawned a fresh subprocess that had never received
`init`, and the bare `embed` request tripped the "embed before init" guard,
breaking local embeddings for the rest of the process.

- Carry `model` + `cacheDir` in every `embed` IPC; bind both into the
  `MnemopiSubprocessEmbeddingModel` closure when the wrapper is created.
- Worker calls `ensureLoaded(model, cacheDir)` in `handleEmbed` (idempotent
  for the same key, so steady-state embeds pay nothing). Drops the
  "embed before init" guard — `embed` self-initializes.
- Promotes the internal `WorkerHandle` interface to `MnemopiEmbedWorkerHandle`
  so the test seam can construct a fake worker.
- New regression test drives a fake worker, terminates + re-embeds the SAME
  cached wrapper, and asserts every embed message still carries the bound
  `(model, cacheDir)`.

Per review on #3034.
2026-06-19 08:20:14 +00:00

36 lines
1.7 KiB
TypeScript

/**
* Wire types between the parent (`MnemopiEmbedClient`) and the local
* embeddings subprocess. The parent owns the subprocess lifecycle (graceful
* work, hard `SIGKILL` on shutdown); the protocol carries no explicit close
* handshake — once the parent decides to terminate, it signals the OS to reap
* the child so `onnxruntime-node`'s NAPI finalizer never runs in the main
* agent address space (it crashes Bun on Windows shutdown — issue #3031, the
* mnemopi sibling of the tiny-model fix from #1606/#1607). See
* `embed-client.ts` for the spawn/kill glue.
*/
/** Identifier of the fastembed model the worker should load (e.g. `fast-bge-base-en-v1.5`). */
export type MnemopiEmbedModelId = string;
export type MnemopiEmbedWorkerInbound =
| { type: "ping"; id: string }
| { type: "init"; id: string; model: MnemopiEmbedModelId; cacheDir?: string }
// `embed` always carries the same `model` / `cacheDir` the wrapper was
// initialized with so a fresh subprocess (after the parent SIGKILLed the
// previous one but mnemopi still holds the cached `LocalEmbeddingModel`)
// can lazily reload the model on demand instead of returning
// "embed before init".
| { type: "embed"; id: string; model: MnemopiEmbedModelId; cacheDir?: string; texts: string[]; batchSize?: number };
export type MnemopiEmbedWorkerOutbound =
| { type: "pong"; id: string }
| { type: "ready"; id: string }
| { type: "vectors"; id: string; vectors: number[][] }
| { type: "error"; id: string; error: string }
| { type: "log"; level: "debug" | "warn" | "error"; msg: string; meta?: Record<string, unknown> };
export interface MnemopiEmbedTransport {
send(message: MnemopiEmbedWorkerOutbound): void;
onMessage(handler: (message: MnemopiEmbedWorkerInbound) => void): () => void;
}