@cortexkit/pi-magic-context 0.47.0 → 0.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/auto-search-worker.js +27 -22
  2. package/dist/embedding-activity-ew6dshfd.js +10 -0
  3. package/dist/embedding-backfill-b71dvfs8.js +20 -0
  4. package/dist/embedding-cache-7rp4gqzr.js +16 -0
  5. package/dist/embedding-worker.js +11 -8
  6. package/dist/harness-9872e0ym.js +10 -0
  7. package/dist/historian-calibration-extension.js +30 -4
  8. package/dist/index-041ex0cj.js +519 -0
  9. package/dist/index-0h2mkab2.js +17 -0
  10. package/dist/{index-2p1e8706.js → index-0vrkp1jq.js} +3 -3
  11. package/dist/index-135e8hvg.js +43 -0
  12. package/dist/{index-vgcemg0n.js → index-1evzceyw.js} +151 -2452
  13. package/dist/index-21v1kfs7.js +2309 -0
  14. package/dist/{index-n3g5mwa8.js → index-290tzwp1.js} +3 -3
  15. package/dist/{index-0qejsd7f.js → index-3cgbv47m.js} +289 -45
  16. package/dist/{index-xpbh1ypk.js → index-3mgf68pn.js} +38 -741
  17. package/dist/index-3rea6zd9.js +2396 -0
  18. package/dist/index-674f6qs2.js +16 -0
  19. package/dist/index-8976mezv.js +209 -0
  20. package/dist/index-904reqtg.js +152 -0
  21. package/dist/index-9p40dgkm.js +22 -0
  22. package/dist/index-9x1f96r8.js +11 -0
  23. package/dist/index-bejpsj10.js +1060 -0
  24. package/dist/{index-spk5vrhy.js → index-bgmv2kaf.js} +1 -16
  25. package/dist/{index-k3ezsjng.js → index-ehybv5nr.js} +14657 -13951
  26. package/dist/index-fapenpch.js +13 -0
  27. package/dist/index-hwqb3s7f.js +3804 -0
  28. package/dist/index-kjd1fj55.js +3106 -0
  29. package/dist/{index-td885aah.js → index-mcgehvgw.js} +181 -230
  30. package/dist/index-r024r8da.js +171 -0
  31. package/dist/{index-266qrgss.js → index-r56xgha8.js} +301 -27
  32. package/dist/{index-kztsbkqq.js → index-t7xfqtg6.js} +171 -168
  33. package/dist/index-vakawmbc.js +1262 -0
  34. package/dist/{index-7n8gb6j3.js → index-x3dcnxh0.js} +34 -33
  35. package/dist/index-xjqheg7a.js +931 -0
  36. package/dist/index-yarjn32j.js +3026 -0
  37. package/dist/index-yhzhdp3m.js +143 -0
  38. package/dist/index-yzcjxnt2.js +787 -0
  39. package/dist/{index-w76x3af7.js → index-zh1jw8qp.js} +107 -4
  40. package/dist/index.js +4926 -2437
  41. package/dist/logger-j6tecd1h.js +19 -0
  42. package/dist/message-index-bounded-qz5dz2se.js +120 -0
  43. package/dist/{message-time-backfill-d3db2etr.js → message-time-backfill-05ppypkz.js} +7 -6
  44. package/dist/migration-worker.js +20 -14
  45. package/dist/project-embedding-registry-ky7wrkr1.js +85 -0
  46. package/dist/raw-ordinal-warmup-worker.js +60 -0
  47. package/dist/{read-session-chunk-atxsyfqv.js → read-session-chunk-24sd89tf.js} +14 -6
  48. package/dist/read-session-raw-npx3920c.js +57 -0
  49. package/dist/{safe-notification-target-6cn3z99t.js → safe-notification-target-vn9kpmv4.js} +3 -2
  50. package/dist/search-jbh28nwy.js +40 -0
  51. package/dist/sqlite-3yye76ht.js +44 -0
  52. package/dist/sqlite-context-pragmas-gcgch6qz.js +6 -0
  53. package/dist/storage-memory-1z20y55x.js +71 -0
  54. package/dist/storage-tags-kjnsqbdb.js +127 -0
  55. package/dist/subagent-entry.js +35 -17
  56. package/dist/transformers-node-wasm.js +18415 -18415
  57. package/package.json +3 -3
  58. package/dist/index-7s8vw2y0.js +0 -5207
  59. package/dist/index-qaa5j277.js +0 -8544
  60. /package/dist/{index-en1k8h8m.js → index-5prbx026.js} +0 -0
@@ -1,18 +1,21 @@
1
1
  import {
2
2
  modelRefLookupOrder,
3
- toolTemplateError,
4
- DEFAULT_PROTECTED_TOOLS,
5
- mergeProtectedTools
6
- } from "./index-spk5vrhy.js";
3
+ toolTemplateError
4
+ } from "./index-bgmv2kaf.js";
5
+ import {
6
+ getSynapseLaneIdentity
7
+ } from "./index-21v1kfs7.js";
8
+ import {
9
+ getHarness2
10
+ } from "./index-0h2mkab2.js";
7
11
  import {
8
- getHarness,
9
12
  getMagicContextStorageDir,
10
13
  importPluginModule,
11
- log
12
- } from "./index-7n8gb6j3.js";
14
+ log2
15
+ } from "./index-x3dcnxh0.js";
13
16
  import {
14
- estimateTokens
15
- } from "./index-266qrgss.js";
17
+ computeNormalizedHash
18
+ } from "./index-9x1f96r8.js";
16
19
  import {
17
20
  shouldEnforcePrivateStoragePermissions,
18
21
  ensureStorageDirectorySync,
@@ -20,13 +23,10 @@ import {
20
23
  writeStorageFileHandleAsync,
21
24
  createStorageFileHandleAsync
22
25
  } from "./index-vf38qhee.js";
23
-
24
- // ../plugin/src/features/magic-context/memory/embedding-local.ts
25
- import { readdirSync, statSync } from "node:fs";
26
- import { stat, unlink } from "node:fs/promises";
27
- import { dirname, join } from "node:path";
28
- import { pathToFileURL } from "node:url";
29
- import { workerData } from "node:worker_threads";
26
+ import {
27
+ DEFAULT_PROTECTED_TOOLS,
28
+ mergeProtectedTools
29
+ } from "./index-674f6qs2.js";
30
30
 
31
31
  // ../plugin/src/config/schema/magic-context.ts
32
32
  import { homedir } from "node:os";
@@ -5430,7 +5430,7 @@ var MagicContextConfigSchema = object({
5430
5430
  allow_home_project: boolean2().default(false).describe("Allow Magic Context sessions launched from the exact canonical home directory. A non-git home uses its deterministic dir: identity; a home repository uses its git: identity. USER-LEVEL ONLY: project config is ignored. The home identity is excluded from registry seed exports, never resolves descendants by containment, and cannot join a workspace."),
5431
5431
  mural: object({
5432
5432
  enabled: boolean2().default(false),
5433
- model: string2().trim().min(1).optional().describe("Model for the compress-cues task that compresses each memory into a mural cue. The mural image itself is rendered deterministically (no author model).")
5433
+ model: unknown().optional().describe("Deprecated: ignored. compress-cues now uses the dreamer model for each harness, like every other dreamer task; override it with dreamer.opencode.tasks.compress-cues.model (or the pi/omp equivalent). Remove this key; it no longer does anything.").meta({ deprecated: true })
5434
5434
  }).default({ enabled: false }).describe("Experimental mural: a single deterministically-rendered image of project memories that did not fit the context budget. Cues are compressed per-memory by the compress-cues dreamer task."),
5435
5435
  transform_mode: _enum(["ts", "rust"]).default("ts").describe('Experimental: routes the entire Magic Context runtime for the project through the ck-mc Rust module over subc (requires user-level `subc` config); "ts" is the current TypeScript pipeline.'),
5436
5436
  auto_update: boolean2().optional().describe("Enable automatic npm self-update checks for the OpenCode plugin. Security: USER-only in config loader, so hostile project configs cannot suppress updates."),
@@ -5440,8 +5440,9 @@ var MagicContextConfigSchema = object({
5440
5440
  historian: HistorianConfigSchema.describe("Historian metadata plus independent strict OpenCode, Pi, and OMP execution blocks. Retained metadata stays at historian; model, fallback_models, variant, and thinking_level belong only in historian.opencode, historian.pi, or historian.omp."),
5441
5441
  dreamer: DreamerConfigSchema.optional().describe("Dreamer metadata and scheduling plus independent strict OpenCode, Pi, and OMP execution blocks. schedule and promotion_threshold stay at dreamer.tasks; model, fallback_models, variant, thinking_level, and timeout_minutes belong only in the matching harness block."),
5442
5442
  smart_notes: object({
5443
+ enabled: boolean2().default(true).describe("Enable smart-note authoring and condition evaluation. User-level only: tool definitions are process-wide. Changes require a host restart and rebuild each session's cached prompt once. Pending notes remain stored and unchecked; already-ready notes still surface."),
5443
5444
  retina_handoff: boolean2().default(false).describe("When true, dreamer skips smart notes whose surface conditions compiled to retina provider configs at authoring time. Default false keeps both paths active until the retina consumer is deployed.")
5444
- }).default({ retina_handoff: false }).describe("Smart-note ownership transition controls."),
5445
+ }).default({ enabled: true, retina_handoff: false }).describe("Smart-note ownership transition controls."),
5445
5446
  cache_ttl: union([string2(), object({ default: string2() }).catchall(string2())]).default("5m").describe(`How long Magic Context assumes the provider's cached prefix stays valid. This is MC's own deferral gate — it does not change the provider's actual cache lifetime. String (e.g. "5m", "1h", "30s") or per-model object ({ default: "5m", "provider/model": "1h", "provider/*": "never" }); keys resolve most-specific first (exact provider/model, bare model ID, shorter dash-prefixes, then the provider/* wildcard, then default). User and project settings, including an explicit "5m" or object default, apply on the next pass. Only built-in defaults are frozen per session, including across restarts: when unset, GPT-5.6 and later (including gpt-6*, through any provider prefix) use 30m, other models use 5m. /ctx-status shows the effective TTL and whether it comes from your config or a frozen built-in default. Editing the TTL does not itself rewrite prompt bytes; a lowered TTL rebuilds only after normal idle expiry. OpenAI documents at least 30 minutes since the latest write or reuse: https://developers.openai.com/api/docs/guides/prompt-caching (Cache lifetime and Summary of model differences). Set to "never" to mean MC never assumes expiry (for lanes kept warm externally by a cache-keep tool) — disables the idle-TTL heuristic so MC never initiates a rebuild based on elapsed time. Provider-side extended TTL is a separate request-level concern (cache_control: { ttl } in the request body).`),
5446
5447
  prompt_surface: PromptSurfaceConfigSchema.default({ default: "full" }).describe("Prompt-surface presets: default is full; models use bare model IDs, provider/model, or provider/* routing keys. Guidance and tool-description overrides are user-level only. OpenCode 1.x, Pi, and OMP register tool descriptions once per process (they follow the default preset). OpenCode 2 rewrites the five ctx_* descriptions per request from the draft model."),
5447
5448
  output_reserve: union([
@@ -5546,7 +5547,6 @@ var MagicContextConfigSchema = object({
5546
5547
  });
5547
5548
  var LIVE_RELOAD_CONFIG_PATHS = [
5548
5549
  "cache_ttl",
5549
- "mural.model",
5550
5550
  "toast_duration_ms",
5551
5551
  "historian.opencode.model",
5552
5552
  "historian.opencode.fallback_models",
@@ -5617,6 +5617,125 @@ function deriveDefaultProtectedTokens(usableSoft) {
5617
5617
  return Math.min(64000, Math.max(lowerBound, target));
5618
5618
  }
5619
5619
 
5620
+ // ../plugin/src/features/magic-context/memory/embedding-model-match.ts
5621
+ function parseEmbeddingModel(model) {
5622
+ const lastColon = model.lastIndexOf(":");
5623
+ if (lastColon > model.lastIndexOf("/")) {
5624
+ return { base: model.slice(0, lastColon), tag: model.slice(lastColon + 1) };
5625
+ }
5626
+ return { base: model };
5627
+ }
5628
+ function normalizeEmbeddingModelId(model) {
5629
+ return parseEmbeddingModel(model.trim().toLowerCase()).base;
5630
+ }
5631
+ function matchNormalizedEmbeddingModels(a, b) {
5632
+ if (a.length === 0 || b.length === 0)
5633
+ return true;
5634
+ if (a === b)
5635
+ return true;
5636
+ const longer = a.length >= b.length ? a : b;
5637
+ const shorter = a.length >= b.length ? b : a;
5638
+ const isBoundary = (ch) => ch === "-" || ch === "/";
5639
+ if (longer.startsWith(shorter) && isBoundary(longer.charAt(shorter.length)))
5640
+ return true;
5641
+ if (longer.endsWith(shorter) && isBoundary(longer.charAt(longer.length - shorter.length - 1)))
5642
+ return true;
5643
+ return false;
5644
+ }
5645
+ function embeddingModelsMatch(served, requested) {
5646
+ const servedNormalized = served.trim().toLowerCase();
5647
+ const requestedNormalized = requested.trim().toLowerCase();
5648
+ const servedModel = parseEmbeddingModel(servedNormalized);
5649
+ const requestedModel = parseEmbeddingModel(requestedNormalized);
5650
+ if (servedModel.tag !== undefined && requestedModel.tag !== undefined && servedModel.tag !== requestedModel.tag) {
5651
+ return false;
5652
+ }
5653
+ return matchNormalizedEmbeddingModels(servedModel.base, requestedModel.base);
5654
+ }
5655
+ var QWEN3_QUERY_INSTRUCTION = `Instruct: Given a web search query, retrieve relevant passages that answer the query
5656
+ Query: `;
5657
+ var INSTRUCT_WEB_SEARCH_QUERY_PREFIX = QWEN3_QUERY_INSTRUCTION;
5658
+ var EMBEDDING_MODEL_PREFIX_FAMILIES = [
5659
+ {
5660
+ family: "qwen3-embedding",
5661
+ basenamePattern: /^qwen3-embedding(?:-|$)/,
5662
+ prefixes: { queryPrefix: QWEN3_QUERY_INSTRUCTION, documentPrefix: "" }
5663
+ },
5664
+ {
5665
+ family: "gte-qwen-instruct",
5666
+ basenamePattern: /^gte-qwen.*-instruct(?:-|$)/,
5667
+ prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
5668
+ },
5669
+ {
5670
+ family: "e5-instruct",
5671
+ basenamePattern: /^(?:multilingual-)?e5-.*-instruct(?:-|$)/,
5672
+ prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
5673
+ },
5674
+ {
5675
+ family: "nomic-embed-text",
5676
+ basenamePattern: /^nomic-embed-text(?:-|$)/,
5677
+ prefixes: { queryPrefix: "search_query: ", documentPrefix: "search_document: " }
5678
+ }
5679
+ ];
5680
+ function modelBasename(model) {
5681
+ const normalized = normalizeEmbeddingModelId(model);
5682
+ return normalized.slice(normalized.lastIndexOf("/") + 1);
5683
+ }
5684
+ function resolveEmbeddingTextPrefixes(model, queryInstruction, documentPrefix) {
5685
+ const family = EMBEDDING_MODEL_PREFIX_FAMILIES.find(({ basenamePattern }) => basenamePattern.test(modelBasename(model)));
5686
+ return {
5687
+ queryPrefix: queryInstruction === false ? "" : queryInstruction !== undefined ? queryInstruction : family?.prefixes.queryPrefix ?? "",
5688
+ documentPrefix: documentPrefix ?? family?.prefixes.documentPrefix ?? ""
5689
+ };
5690
+ }
5691
+
5692
+ // ../plugin/src/features/magic-context/memory/embedding-identity.ts
5693
+ function normalizeEndpoint(endpoint) {
5694
+ return endpoint?.trim().replace(/\/+$/, "") ?? "";
5695
+ }
5696
+ var LOCAL_EMBEDDING_RUNTIME_FINGERPRINT = "transformers@4.3.0;onnxruntime-node@1.30.0;onnxruntime-web@1.26.0-dev.20260416-b7804b056c";
5697
+ function getEmbeddingProviderIdentity(config) {
5698
+ if (config.provider === "off") {
5699
+ return "embedding-provider:off";
5700
+ }
5701
+ if (config.provider === "synapse") {
5702
+ const resolved = config;
5703
+ if (!resolved.model || !resolved.synapse_fingerprint)
5704
+ return "synapse:v1:pending";
5705
+ return getSynapseLaneIdentity(resolved.model, resolved.synapse_fingerprint);
5706
+ }
5707
+ if (config.provider !== "local" && config.provider !== "openai-compatible") {
5708
+ throw new Error("Unknown embedding provider");
5709
+ }
5710
+ const truncate = config.provider === "openai-compatible" ? config.truncate?.trim() : undefined;
5711
+ const documentPrefix = config.provider === "openai-compatible" ? resolveEmbeddingTextPrefixes(config.model, config.query_instruction, config.document_prefix).documentPrefix : "";
5712
+ const localDtype = config.provider === "local" && config.local_dtype && config.local_dtype !== "fp32" ? config.local_dtype : undefined;
5713
+ const identityInput = config.provider === "openai-compatible" ? {
5714
+ provider: "openai-compatible",
5715
+ model: config.model.trim(),
5716
+ endpoint: normalizeEndpoint(config.endpoint),
5717
+ apiKeyPresent: Boolean(config.api_key?.trim()),
5718
+ inputType: config.input_type?.trim() || "",
5719
+ ...documentPrefix ? { documentPrefix } : {},
5720
+ ...truncate ? { truncate } : {}
5721
+ } : {
5722
+ provider: "local",
5723
+ model: config.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
5724
+ endpoint: "",
5725
+ apiKeyPresent: false,
5726
+ runtimeFingerprint: LOCAL_EMBEDDING_RUNTIME_FINGERPRINT,
5727
+ ...localDtype ? { localDtype } : {}
5728
+ };
5729
+ return `embedding-provider:${computeNormalizedHash(JSON.stringify(identityInput))}`;
5730
+ }
5731
+
5732
+ // ../plugin/src/features/magic-context/memory/embedding-local.ts
5733
+ import { readdirSync, statSync } from "node:fs";
5734
+ import { stat, unlink } from "node:fs/promises";
5735
+ import { dirname, join } from "node:path";
5736
+ import { pathToFileURL } from "node:url";
5737
+ import { workerData } from "node:worker_threads";
5738
+
5620
5739
  // ../plugin/src/features/magic-context/memory/embedding-failure.ts
5621
5740
  function errorMessage(error) {
5622
5741
  return error instanceof Error ? error.message : String(error ?? "unknown error");
@@ -5712,2426 +5831,6 @@ function dominantEmbeddingFailure(failures) {
5712
5831
  return dominant?.failure;
5713
5832
  }
5714
5833
 
5715
- // ../plugin/src/features/magic-context/memory/embedding-model-match.ts
5716
- function parseEmbeddingModel(model) {
5717
- const lastColon = model.lastIndexOf(":");
5718
- if (lastColon > model.lastIndexOf("/")) {
5719
- return { base: model.slice(0, lastColon), tag: model.slice(lastColon + 1) };
5720
- }
5721
- return { base: model };
5722
- }
5723
- function normalizeEmbeddingModelId(model) {
5724
- return parseEmbeddingModel(model.trim().toLowerCase()).base;
5725
- }
5726
- function matchNormalizedEmbeddingModels(a, b) {
5727
- if (a.length === 0 || b.length === 0)
5728
- return true;
5729
- if (a === b)
5730
- return true;
5731
- const longer = a.length >= b.length ? a : b;
5732
- const shorter = a.length >= b.length ? b : a;
5733
- const isBoundary = (ch) => ch === "-" || ch === "/";
5734
- if (longer.startsWith(shorter) && isBoundary(longer.charAt(shorter.length)))
5735
- return true;
5736
- if (longer.endsWith(shorter) && isBoundary(longer.charAt(longer.length - shorter.length - 1)))
5737
- return true;
5738
- return false;
5739
- }
5740
- function embeddingModelsMatch(served, requested) {
5741
- const servedNormalized = served.trim().toLowerCase();
5742
- const requestedNormalized = requested.trim().toLowerCase();
5743
- const servedModel = parseEmbeddingModel(servedNormalized);
5744
- const requestedModel = parseEmbeddingModel(requestedNormalized);
5745
- if (servedModel.tag !== undefined && requestedModel.tag !== undefined && servedModel.tag !== requestedModel.tag) {
5746
- return false;
5747
- }
5748
- return matchNormalizedEmbeddingModels(servedModel.base, requestedModel.base);
5749
- }
5750
- var QWEN3_QUERY_INSTRUCTION = `Instruct: Given a web search query, retrieve relevant passages that answer the query
5751
- Query: `;
5752
- var INSTRUCT_WEB_SEARCH_QUERY_PREFIX = QWEN3_QUERY_INSTRUCTION;
5753
- var EMBEDDING_MODEL_PREFIX_FAMILIES = [
5754
- {
5755
- family: "qwen3-embedding",
5756
- basenamePattern: /^qwen3-embedding(?:-|$)/,
5757
- prefixes: { queryPrefix: QWEN3_QUERY_INSTRUCTION, documentPrefix: "" }
5758
- },
5759
- {
5760
- family: "gte-qwen-instruct",
5761
- basenamePattern: /^gte-qwen.*-instruct(?:-|$)/,
5762
- prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
5763
- },
5764
- {
5765
- family: "e5-instruct",
5766
- basenamePattern: /^(?:multilingual-)?e5-.*-instruct(?:-|$)/,
5767
- prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
5768
- },
5769
- {
5770
- family: "nomic-embed-text",
5771
- basenamePattern: /^nomic-embed-text(?:-|$)/,
5772
- prefixes: { queryPrefix: "search_query: ", documentPrefix: "search_document: " }
5773
- }
5774
- ];
5775
- function modelBasename(model) {
5776
- const normalized = normalizeEmbeddingModelId(model);
5777
- return normalized.slice(normalized.lastIndexOf("/") + 1);
5778
- }
5779
- function resolveEmbeddingTextPrefixes(model, queryInstruction, documentPrefix) {
5780
- const family = EMBEDDING_MODEL_PREFIX_FAMILIES.find(({ basenamePattern }) => basenamePattern.test(modelBasename(model)));
5781
- return {
5782
- queryPrefix: queryInstruction === false ? "" : queryInstruction !== undefined ? queryInstruction : family?.prefixes.queryPrefix ?? "",
5783
- documentPrefix: documentPrefix ?? family?.prefixes.documentPrefix ?? ""
5784
- };
5785
- }
5786
-
5787
- // ../plugin/src/features/magic-context/memory/embedding-synapse.ts
5788
- import { createHash } from "node:crypto";
5789
-
5790
- // ../../node_modules/.bun/@cortexkit+subc-client@0.11.1/node_modules/@cortexkit/subc-client/dist/client.js
5791
- import { promises as fs2 } from "node:fs";
5792
- import { debuglog } from "node:util";
5793
-
5794
- // ../../node_modules/.bun/@cortexkit+subc-client@0.11.1/node_modules/@cortexkit/subc-client/dist/auth.js
5795
- import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
5796
-
5797
- // ../../node_modules/.bun/@cortexkit+subc-client@0.11.1/node_modules/@cortexkit/subc-client/dist/socket.js
5798
- import net from "node:net";
5799
-
5800
- // ../../node_modules/.bun/@cortexkit+subc-client@0.11.1/node_modules/@cortexkit/subc-client/dist/envelope.js
5801
- var PROTOCOL_VERSION = 2;
5802
- var HEADER_LEN = 21;
5803
- var FROZEN_PREFIX_LEN = 5;
5804
- var MAX_FRAME_BODY_LEN = 64 * 1024 * 1024;
5805
- var FrameType;
5806
- (function(FrameType) {
5807
- FrameType[FrameType["Request"] = 0] = "Request";
5808
- FrameType[FrameType["Response"] = 1] = "Response";
5809
- FrameType[FrameType["Push"] = 2] = "Push";
5810
- FrameType[FrameType["StreamData"] = 3] = "StreamData";
5811
- FrameType[FrameType["StreamEnd"] = 4] = "StreamEnd";
5812
- FrameType[FrameType["Error"] = 5] = "Error";
5813
- FrameType[FrameType["Cancel"] = 6] = "Cancel";
5814
- FrameType[FrameType["Ping"] = 7] = "Ping";
5815
- FrameType[FrameType["Pong"] = 8] = "Pong";
5816
- FrameType[FrameType["Hello"] = 9] = "Hello";
5817
- FrameType[FrameType["HelloAck"] = 10] = "HelloAck";
5818
- FrameType[FrameType["Goodbye"] = 11] = "Goodbye";
5819
- })(FrameType || (FrameType = {}));
5820
- var FRAME_TYPE_MAX = FrameType.Goodbye;
5821
- function isPureHeader(ty) {
5822
- return ty === FrameType.Cancel || ty === FrameType.Ping || ty === FrameType.Pong || ty === FrameType.Goodbye;
5823
- }
5824
- var Priority;
5825
- (function(Priority) {
5826
- Priority[Priority["Passive"] = 0] = "Passive";
5827
- Priority[Priority["Interactive"] = 1] = "Interactive";
5828
- Priority[Priority["Background"] = 2] = "Background";
5829
- })(Priority || (Priority = {}));
5830
- var AdmissionClass;
5831
- (function(AdmissionClass) {
5832
- AdmissionClass[AdmissionClass["Normal"] = 0] = "Normal";
5833
- AdmissionClass[AdmissionClass["Expedite"] = 1] = "Expedite";
5834
- AdmissionClass[AdmissionClass["Sheddable"] = 2] = "Sheddable";
5835
- })(AdmissionClass || (AdmissionClass = {}));
5836
- var FLAG_BINARY = 1;
5837
- var FLAG_PRIORITY_MASK = 6;
5838
- var FLAG_PRIORITY_SHIFT = 1;
5839
- var FLAG_LAST = 8;
5840
- var FLAG_ADMISSION_MASK = 48;
5841
- var FLAG_ADMISSION_SHIFT = 4;
5842
- var FLAG_RESERVED_MASK = 128;
5843
- function buildFlags(binary, priority, last, admissionClass = AdmissionClass.Normal) {
5844
- let flags = 0;
5845
- if (binary)
5846
- flags |= FLAG_BINARY;
5847
- flags |= priority << FLAG_PRIORITY_SHIFT;
5848
- if (last)
5849
- flags |= FLAG_LAST;
5850
- flags |= admissionClass << FLAG_ADMISSION_SHIFT;
5851
- return flags;
5852
- }
5853
- function hasBinary(flags) {
5854
- return (flags & FLAG_BINARY) !== 0;
5855
- }
5856
- function encodeHeader(header) {
5857
- const buffer = new Uint8Array(HEADER_LEN);
5858
- const view = new DataView(buffer.buffer);
5859
- view.setUint32(0, header.len, true);
5860
- buffer[4] = header.ver;
5861
- buffer[5] = header.ty;
5862
- buffer[6] = header.flags;
5863
- view.setUint16(7, header.channel, true);
5864
- view.setUint32(9, header.epoch, true);
5865
- view.setBigUint64(13, header.corr, true);
5866
- return buffer;
5867
- }
5868
-
5869
- class DecodeError extends Error {
5870
- code;
5871
- constructor(message, code) {
5872
- super(message);
5873
- this.code = code;
5874
- this.name = "DecodeError";
5875
- }
5876
- }
5877
- function validateHeaderFields(header) {
5878
- const len = header.len >>> 0;
5879
- const ver = header.ver >>> 0 & 255;
5880
- const typeByte = header.ty >>> 0 & 255;
5881
- const flags = header.flags >>> 0 & 255;
5882
- const channel = header.channel >>> 0 & 65535;
5883
- const epoch = header.epoch >>> 0;
5884
- BigInt.asUintN(64, header.corr);
5885
- if (ver !== PROTOCOL_VERSION)
5886
- throw new DecodeError(`unsupported envelope version ${ver}`, "unsupported_version");
5887
- if (typeByte > FRAME_TYPE_MAX)
5888
- throw new DecodeError(`unknown frame type byte ${typeByte}`, "unknown_frame_type");
5889
- const ty = typeByte;
5890
- if ((flags & FLAG_RESERVED_MASK) !== 0) {
5891
- throw new DecodeError(`reserved flag bits set in flags 0b${flags.toString(2).padStart(8, "0")}`, "reserved_flag_bits");
5892
- }
5893
- if ((flags & FLAG_PRIORITY_MASK) >> FLAG_PRIORITY_SHIFT === 3) {
5894
- throw new DecodeError(`reserved priority bits set in flags 0b${flags.toString(2).padStart(8, "0")}`, "reserved_priority_bits");
5895
- }
5896
- const admission = (flags & FLAG_ADMISSION_MASK) >> FLAG_ADMISSION_SHIFT;
5897
- if (admission === 3) {
5898
- throw new DecodeError(`reserved admission class set in flags 0b${flags.toString(2).padStart(8, "0")}`, "reserved_admission_class");
5899
- }
5900
- if (admission === AdmissionClass.Sheddable && ty !== FrameType.Push && ty !== FrameType.StreamData) {
5901
- throw new DecodeError(`SHEDDABLE admission class is illegal on ${FrameType[ty]} in flags 0b${flags.toString(2).padStart(8, "0")}`, "sheddable_illegal_frame_type");
5902
- }
5903
- if (channel === 0 && epoch !== 0) {
5904
- throw new DecodeError(`control channel carried nonzero epoch ${epoch}`, "nonzero_epoch_on_control_channel");
5905
- }
5906
- if (isPureHeader(ty) && len !== 0) {
5907
- throw new DecodeError(`pure-header frame ${FrameType[ty]} declared non-zero body length ${len}`, "pure_header_frame_with_body");
5908
- }
5909
- }
5910
- function decodeHeader(bytes) {
5911
- if (bytes.length < FROZEN_PREFIX_LEN) {
5912
- throw new DecodeError(`header shorter than frozen prefix: have ${bytes.length} bytes`, "too_short_for_prefix");
5913
- }
5914
- const ver = bytes[4];
5915
- if (ver !== PROTOCOL_VERSION)
5916
- throw new DecodeError(`unsupported envelope version ${ver}`, "unsupported_version");
5917
- if (bytes.length < HEADER_LEN) {
5918
- throw new DecodeError(`header too short for version: have ${bytes.length} bytes, need ${HEADER_LEN}`, "too_short_for_header");
5919
- }
5920
- const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
5921
- const len = view.getUint32(0, true);
5922
- const typeByte = bytes[5];
5923
- if (typeByte > FRAME_TYPE_MAX)
5924
- throw new DecodeError(`unknown frame type byte ${typeByte}`, "unknown_frame_type");
5925
- const ty = typeByte;
5926
- const flags = bytes[6];
5927
- if ((flags & FLAG_RESERVED_MASK) !== 0) {
5928
- throw new DecodeError(`reserved flag bits set in flags 0b${flags.toString(2).padStart(8, "0")}`, "reserved_flag_bits");
5929
- }
5930
- if ((flags & FLAG_PRIORITY_MASK) >> FLAG_PRIORITY_SHIFT === 3) {
5931
- throw new DecodeError(`reserved priority bits set in flags 0b${flags.toString(2).padStart(8, "0")}`, "reserved_priority_bits");
5932
- }
5933
- const admission = (flags & FLAG_ADMISSION_MASK) >> FLAG_ADMISSION_SHIFT;
5934
- if (admission === 3) {
5935
- throw new DecodeError(`reserved admission class set in flags 0b${flags.toString(2).padStart(8, "0")}`, "reserved_admission_class");
5936
- }
5937
- if (admission === AdmissionClass.Sheddable && ty !== FrameType.Push && ty !== FrameType.StreamData) {
5938
- throw new DecodeError(`SHEDDABLE admission class is illegal on ${FrameType[ty]} in flags 0b${flags.toString(2).padStart(8, "0")}`, "sheddable_illegal_frame_type");
5939
- }
5940
- const channel = view.getUint16(7, true);
5941
- const epoch = view.getUint32(9, true);
5942
- if (channel === 0 && epoch !== 0) {
5943
- throw new DecodeError(`control channel carried nonzero epoch ${epoch}`, "nonzero_epoch_on_control_channel");
5944
- }
5945
- if (isPureHeader(ty) && len !== 0) {
5946
- throw new DecodeError(`pure-header frame ${FrameType[ty]} declared non-zero body length ${len}`, "pure_header_frame_with_body");
5947
- }
5948
- return { len, ver, ty, flags, channel, epoch, corr: view.getBigUint64(13, true) };
5949
- }
5950
- function buildFrame(ty, flags, channel, epoch, corr, body) {
5951
- return buildFrameWithVersion(PROTOCOL_VERSION, ty, flags, channel, epoch, corr, body);
5952
- }
5953
- function buildFrameWithVersion(ver, ty, flags, channel, epoch, corr, body) {
5954
- if (body.length > MAX_FRAME_BODY_LEN) {
5955
- throw new DecodeError(`frame body ${body.length} exceeds max ${MAX_FRAME_BODY_LEN}`, "frame_body_too_large");
5956
- }
5957
- const header = { len: body.length, ver, ty, flags, channel, epoch, corr };
5958
- validateHeaderFields(header);
5959
- return { header, body };
5960
- }
5961
- function encodeFrame(frame) {
5962
- if (frame.header.len !== frame.body.length) {
5963
- throw new DecodeError(`frame header length ${frame.header.len} does not match body length ${frame.body.length}`, "frame_length_mismatch");
5964
- }
5965
- const header = encodeHeader(frame.header);
5966
- const output = new Uint8Array(header.length + frame.body.length);
5967
- output.set(header, 0);
5968
- output.set(frame.body, header.length);
5969
- return output;
5970
- }
5971
-
5972
- // ../../node_modules/.bun/@cortexkit+subc-client@0.11.1/node_modules/@cortexkit/subc-client/dist/socket.js
5973
- class SocketClosedError extends Error {
5974
- }
5975
-
5976
- class SocketTimeoutError extends Error {
5977
- }
5978
-
5979
- class SocketWriteNotQueuedError extends Error {
5980
- cause;
5981
- constructor(message, cause) {
5982
- super(message);
5983
- this.cause = cause;
5984
- }
5985
- }
5986
-
5987
- class SocketWriteQueuedError extends Error {
5988
- cause;
5989
- constructor(message, cause) {
5990
- super(message);
5991
- this.cause = cause;
5992
- }
5993
- }
5994
- function toWriteBuffer(bytes) {
5995
- return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
5996
- }
5997
- var WRITE_BORROWED = Symbol("subc.socket.writeBorrowed");
5998
- var WRITE_TRACKED_BORROWED = Symbol("subc.socket.writeTrackedBorrowed");
5999
- function writeBorrowed(socket, bytes, deadlineMs) {
6000
- const borrowed = socket[WRITE_BORROWED];
6001
- return borrowed ? borrowed.call(socket, bytes, deadlineMs) : socket.write(bytes, deadlineMs);
6002
- }
6003
- function writeTrackedBorrowed(socket, bytes, deadlineMs) {
6004
- const borrowed = socket[WRITE_TRACKED_BORROWED];
6005
- return borrowed ? borrowed.call(socket, bytes, deadlineMs) : socket.writeTracked(bytes, deadlineMs);
6006
- }
6007
-
6008
- class SubcSocket {
6009
- sock;
6010
- chunks = [];
6011
- buffered = 0;
6012
- waiter = null;
6013
- closedErr = null;
6014
- bufferedBytes() {
6015
- return this.buffered;
6016
- }
6017
- constructor(sock) {
6018
- this.sock = sock;
6019
- sock.on("data", (chunk) => {
6020
- this.chunks.push(chunk);
6021
- this.buffered += chunk.length;
6022
- this.tryServe();
6023
- });
6024
- const fail = (err) => {
6025
- if (!this.closedErr)
6026
- this.closedErr = err;
6027
- this.tryServe();
6028
- };
6029
- sock.on("error", (err) => fail(err instanceof Error ? err : new Error(String(err))));
6030
- sock.on("end", () => fail(new SocketClosedError("subc closed the connection")));
6031
- sock.on("close", () => fail(new SocketClosedError("subc connection closed")));
6032
- }
6033
- localPort() {
6034
- return this.sock.localPort ?? null;
6035
- }
6036
- static connect(host, port, deadlineMs) {
6037
- return new Promise((resolve, reject) => {
6038
- const sock = net.connect({ host, port });
6039
- sock.setNoDelay(true);
6040
- const timer = setTimeout(() => {
6041
- sock.destroy();
6042
- reject(new SocketTimeoutError(`timed out connecting to ${host}:${port}`));
6043
- }, Math.max(0, deadlineMs - Date.now()));
6044
- sock.once("connect", () => {
6045
- clearTimeout(timer);
6046
- resolve(new SubcSocket(sock));
6047
- });
6048
- sock.once("error", (err) => {
6049
- clearTimeout(timer);
6050
- reject(err);
6051
- });
6052
- });
6053
- }
6054
- async readFrame(headerDeadlineMs, bodyDeadline, onHeader) {
6055
- const prefix = await this.readExact(FROZEN_PREFIX_LEN, headerDeadlineMs);
6056
- const version = prefix[4];
6057
- if (version !== PROTOCOL_VERSION)
6058
- throw new DecodeError(`unsupported envelope version ${version}`, "unsupported_version");
6059
- const remainder = await this.readExact(HEADER_LEN - FROZEN_PREFIX_LEN, headerDeadlineMs);
6060
- const headerBytes = new Uint8Array(HEADER_LEN);
6061
- headerBytes.set(prefix);
6062
- headerBytes.set(remainder, FROZEN_PREFIX_LEN);
6063
- const header = decodeHeader(headerBytes);
6064
- if (header.len > MAX_FRAME_BODY_LEN) {
6065
- throw new DecodeError(`frame body ${header.len} exceeds max ${MAX_FRAME_BODY_LEN}`, "frame_body_too_large");
6066
- }
6067
- onHeader?.();
6068
- const bodyDeadlineMs = typeof bodyDeadline === "number" ? bodyDeadline : Date.now() + bodyDeadline.afterHeaderMs;
6069
- const body = header.len === 0 ? new Uint8Array(0) : await this.readExact(header.len, bodyDeadlineMs);
6070
- return { header, body };
6071
- }
6072
- readExact(n, deadlineMs) {
6073
- if (this.waiter) {
6074
- return Promise.reject(new Error("concurrent readExact is not supported"));
6075
- }
6076
- if (n === 0)
6077
- return Promise.resolve(new Uint8Array(0));
6078
- return new Promise((resolve, reject) => {
6079
- let timer = null;
6080
- if (Number.isFinite(deadlineMs)) {
6081
- const remaining = deadlineMs - Date.now();
6082
- if (remaining <= 0) {
6083
- reject(new SocketTimeoutError(`timed out waiting for ${n} bytes`));
6084
- return;
6085
- }
6086
- timer = setTimeout(() => {
6087
- this.waiter = null;
6088
- reject(new SocketTimeoutError(`timed out waiting for ${n} bytes`));
6089
- }, remaining);
6090
- }
6091
- this.waiter = { need: n, resolve, reject, timer };
6092
- this.tryServe();
6093
- });
6094
- }
6095
- async write(bytes, deadlineMs) {
6096
- await this.writeBuffer(Buffer.from(bytes), deadlineMs);
6097
- }
6098
- writeTracked(bytes, deadlineMs) {
6099
- return this.writeTrackedBuffer(Buffer.from(bytes), deadlineMs);
6100
- }
6101
- async[WRITE_BORROWED](bytes, deadlineMs) {
6102
- await this.writeBuffer(toWriteBuffer(bytes), deadlineMs);
6103
- }
6104
- [WRITE_TRACKED_BORROWED](bytes, deadlineMs) {
6105
- return this.writeTrackedBuffer(toWriteBuffer(bytes), deadlineMs);
6106
- }
6107
- async writeBuffer(buffer, deadlineMs) {
6108
- try {
6109
- await this.writeTrackedBuffer(buffer, deadlineMs).completed;
6110
- } catch (err) {
6111
- if (err instanceof SocketWriteNotQueuedError || err instanceof SocketWriteQueuedError) {
6112
- throw err.cause ?? err;
6113
- }
6114
- throw err;
6115
- }
6116
- }
6117
- writeTrackedBuffer(buffer, deadlineMs) {
6118
- if (this.closedErr) {
6119
- return {
6120
- queued: false,
6121
- completed: Promise.reject(new SocketWriteNotQueuedError("subc socket was closed before bytes could be queued", this.closedErr))
6122
- };
6123
- }
6124
- let queued = false;
6125
- let settled = false;
6126
- let timer = null;
6127
- const completed = new Promise((resolve, reject) => {
6128
- const settle = (run) => {
6129
- if (settled)
6130
- return;
6131
- settled = true;
6132
- if (timer)
6133
- clearTimeout(timer);
6134
- run();
6135
- };
6136
- const remaining = deadlineMs - Date.now();
6137
- if (remaining <= 0) {
6138
- settle(() => reject(new SocketWriteNotQueuedError("timed out before bytes could be queued to subc", new SocketTimeoutError("timed out writing to subc"))));
6139
- return;
6140
- }
6141
- timer = setTimeout(() => {
6142
- const timeout = new SocketTimeoutError("timed out writing to subc");
6143
- settle(() => reject(queued ? new SocketWriteQueuedError("timed out after bytes were handed to the subc socket", timeout) : new SocketWriteNotQueuedError("timed out before bytes could be queued to subc", timeout)));
6144
- }, remaining);
6145
- try {
6146
- this.sock.write(buffer, (err) => {
6147
- settle(() => {
6148
- if (err) {
6149
- reject(new SocketWriteQueuedError("subc socket reported a write error after bytes were handed to the socket", err instanceof Error ? err : new Error(String(err))));
6150
- } else {
6151
- resolve();
6152
- }
6153
- });
6154
- });
6155
- queued = true;
6156
- } catch (err) {
6157
- settle(() => reject(new SocketWriteNotQueuedError("subc socket write threw before bytes could be queued", err instanceof Error ? err : new Error(String(err)))));
6158
- }
6159
- });
6160
- return { queued, completed };
6161
- }
6162
- close() {
6163
- this.sock.destroy();
6164
- }
6165
- tryServe() {
6166
- const w = this.waiter;
6167
- if (!w)
6168
- return;
6169
- if (this.buffered >= w.need) {
6170
- const out = this.take(w.need);
6171
- this.waiter = null;
6172
- if (w.timer)
6173
- clearTimeout(w.timer);
6174
- w.resolve(out);
6175
- return;
6176
- }
6177
- if (this.closedErr) {
6178
- this.waiter = null;
6179
- if (w.timer)
6180
- clearTimeout(w.timer);
6181
- w.reject(this.closedErr);
6182
- }
6183
- }
6184
- take(n) {
6185
- const out = Buffer.allocUnsafe(n);
6186
- let off = 0;
6187
- while (off < n) {
6188
- const head = this.chunks[0];
6189
- const want = n - off;
6190
- if (head.length <= want) {
6191
- head.copy(out, off);
6192
- off += head.length;
6193
- this.chunks.shift();
6194
- } else {
6195
- head.copy(out, off, 0, want);
6196
- this.chunks[0] = head.subarray(want);
6197
- off += want;
6198
- }
6199
- }
6200
- this.buffered -= n;
6201
- return out;
6202
- }
6203
- }
6204
-
6205
- // ../../node_modules/.bun/@cortexkit+subc-client@0.11.1/node_modules/@cortexkit/subc-client/dist/auth.js
6206
- var NONCE_LEN = 32;
6207
- var MAX_AUTH_MESSAGE_LEN = 4096;
6208
- var SERVER_PROOF_DOMAIN = "subc-server-v1";
6209
- var CLIENT_AUTH_DOMAIN = "subc-client-v1";
6210
- var DEFAULT_CLIENT_ROLE = "client";
6211
-
6212
- class AuthError extends Error {
6213
- }
6214
- function computeProof(key, domain, clientNonce, serverNonce, daemonId) {
6215
- const mac = createHmac("sha256", Buffer.from(key));
6216
- mac.update(Buffer.from(domain, "utf8"));
6217
- mac.update(Buffer.from(clientNonce));
6218
- mac.update(Buffer.from(serverNonce));
6219
- mac.update(Buffer.from(daemonId));
6220
- return new Uint8Array(mac.digest());
6221
- }
6222
- function constantTimeEq(a, b) {
6223
- if (a.length !== b.length)
6224
- return false;
6225
- return timingSafeEqual(Buffer.from(a), Buffer.from(b));
6226
- }
6227
- async function writeMessage(sock, value, deadlineMs) {
6228
- const json = Buffer.from(JSON.stringify(value), "utf8");
6229
- if (json.length > MAX_AUTH_MESSAGE_LEN) {
6230
- throw new AuthError(`auth message too large: ${json.length} > ${MAX_AUTH_MESSAGE_LEN}`);
6231
- }
6232
- const lenPrefix = new Uint8Array(4);
6233
- new DataView(lenPrefix.buffer).setUint32(0, json.length, true);
6234
- await writeBorrowed(sock, lenPrefix, deadlineMs);
6235
- await writeBorrowed(sock, json, deadlineMs);
6236
- }
6237
- async function readMessage(sock, deadlineMs) {
6238
- const lenBytes = await sock.readExact(4, deadlineMs);
6239
- const len = new DataView(lenBytes.buffer, lenBytes.byteOffset, 4).getUint32(0, true);
6240
- if (len > MAX_AUTH_MESSAGE_LEN) {
6241
- throw new AuthError(`auth message too large: ${len} > ${MAX_AUTH_MESSAGE_LEN}`);
6242
- }
6243
- const body = len === 0 ? new Uint8Array(0) : await sock.readExact(len, deadlineMs);
6244
- try {
6245
- return JSON.parse(Buffer.from(body).toString("utf8"));
6246
- } catch (err) {
6247
- throw new AuthError(`auth message JSON decode failed: ${String(err)}`);
6248
- }
6249
- }
6250
- function authBytes(value, field) {
6251
- if (!Array.isArray(value)) {
6252
- throw new AuthError(`auth field '${field}' must be a byte array`);
6253
- }
6254
- for (const byte of value) {
6255
- if (typeof byte !== "number" || !Number.isInteger(byte) || byte < 0 || byte > 255) {
6256
- throw new AuthError(`auth field '${field}' has invalid byte ${String(byte)}`);
6257
- }
6258
- }
6259
- return Uint8Array.from(value);
6260
- }
6261
- async function authenticateClient(sock, conn, deadlineMs) {
6262
- const clientNonce = new Uint8Array(randomBytes(NONCE_LEN));
6263
- await writeMessage(sock, { client_nonce: Array.from(clientNonce), role: DEFAULT_CLIENT_ROLE }, deadlineMs);
6264
- const proof = await readMessage(sock, deadlineMs);
6265
- const serverNonce = authBytes(proof.server_nonce, "server_nonce");
6266
- const daemonId = authBytes(proof.daemon_id, "daemon_id");
6267
- const serverProof = authBytes(proof.server_proof, "server_proof");
6268
- const expected = computeProof(conn.key, SERVER_PROOF_DOMAIN, clientNonce, serverNonce, daemonId);
6269
- if (!constantTimeEq(expected, serverProof)) {
6270
- throw new AuthError("server proof mismatch — wrong key or impostor daemon");
6271
- }
6272
- if (!constantTimeEq(daemonId, conn.daemonId)) {
6273
- throw new AuthError("daemon id mismatch — connection file points at a different daemon");
6274
- }
6275
- const clientAuth = computeProof(conn.key, CLIENT_AUTH_DOMAIN, clientNonce, serverNonce, daemonId);
6276
- await writeMessage(sock, { client_auth: Array.from(clientAuth) }, deadlineMs);
6277
- }
6278
-
6279
- // ../../node_modules/.bun/@cortexkit+subc-client@0.11.1/node_modules/@cortexkit/subc-client/dist/connection-file.js
6280
- import { promises as fs } from "node:fs";
6281
- var SCHEMA_VERSION = 1;
6282
- var MIN_KEY_LEN = 32;
6283
- var DAEMON_ID_LEN = 16;
6284
-
6285
- class ConnectionFileError extends Error {
6286
- }
6287
- function toBytes(value, field) {
6288
- if (!Array.isArray(value)) {
6289
- throw new ConnectionFileError(`connection file field '${field}' must be a JSON array of bytes`);
6290
- }
6291
- for (const byte of value) {
6292
- if (typeof byte !== "number" || !Number.isInteger(byte) || byte < 0 || byte > 255) {
6293
- throw new ConnectionFileError(`connection file field '${field}' has invalid byte ${String(byte)}`);
6294
- }
6295
- }
6296
- return Uint8Array.from(value);
6297
- }
6298
- function validate(info) {
6299
- if (info.schema !== SCHEMA_VERSION) {
6300
- throw new ConnectionFileError(`unsupported connection file schema ${info.schema}; expected ${SCHEMA_VERSION}`);
6301
- }
6302
- if (info.endpoints.length === 0) {
6303
- throw new ConnectionFileError("connection file must include at least one endpoint");
6304
- }
6305
- if (info.key.length < MIN_KEY_LEN) {
6306
- throw new ConnectionFileError(`connection file key is too short: ${info.key.length} bytes, need at least ${MIN_KEY_LEN}`);
6307
- }
6308
- if (info.daemonId.length !== DAEMON_ID_LEN) {
6309
- throw new ConnectionFileError(`connection file daemon_id must be ${DAEMON_ID_LEN} bytes, got ${info.daemonId.length}`);
6310
- }
6311
- }
6312
- async function verifyOwnerOnly(path) {
6313
- if (process.platform === "win32")
6314
- return;
6315
- const stat = await fs.stat(path);
6316
- const mode = stat.mode & 511;
6317
- if ((mode & 63) !== 0) {
6318
- throw new ConnectionFileError(`connection file ${path} has insecure permissions 0o${mode.toString(8)}; expected owner-only 0600`);
6319
- }
6320
- }
6321
- async function readConnectionFile(path) {
6322
- await verifyOwnerOnly(path);
6323
- const raw = await fs.readFile(path, "utf8");
6324
- let parsed;
6325
- try {
6326
- parsed = JSON.parse(raw);
6327
- } catch (err) {
6328
- throw new ConnectionFileError(`connection file JSON read failed for ${path}: ${String(err)}`);
6329
- }
6330
- const wireVersion = parsed.wire_version;
6331
- if (wireVersion !== undefined && wireVersion !== PROTOCOL_VERSION) {
6332
- throw new ConnectionFileError(`connection file wire_version ${String(wireVersion)} but this client speaks ${PROTOCOL_VERSION}; the client library must be upgraded`);
6333
- }
6334
- const endpointsRaw = parsed.endpoints;
6335
- if (!Array.isArray(endpointsRaw)) {
6336
- throw new ConnectionFileError("connection file 'endpoints' must be an array");
6337
- }
6338
- const endpoints = endpointsRaw.map((e) => {
6339
- const ep = e;
6340
- if (typeof ep.host !== "string" || typeof ep.port !== "number") {
6341
- throw new ConnectionFileError("connection file endpoint must be { host: string, port: number }");
6342
- }
6343
- return { host: ep.host, port: ep.port };
6344
- });
6345
- const info = {
6346
- schema: parsed.schema,
6347
- endpoints,
6348
- key: toBytes(parsed.key, "key"),
6349
- daemonId: toBytes(parsed.daemon_id, "daemon_id"),
6350
- pid: parsed.pid,
6351
- daemonVer: parsed.daemon_ver ?? ""
6352
- };
6353
- validate(info);
6354
- return info;
6355
- }
6356
-
6357
- // ../../node_modules/.bun/@cortexkit+subc-client@0.11.1/node_modules/@cortexkit/subc-client/dist/route-handle.js
6358
- var connectionToken = new WeakMap;
6359
-
6360
- class RouteHandle {
6361
- channel;
6362
- epoch;
6363
- constructor(channel, epoch, token) {
6364
- if (!Number.isInteger(channel) || channel <= 0 || channel > 65535) {
6365
- throw new RangeError(`route channel must be an integer in 1..65535, got ${channel}`);
6366
- }
6367
- if (!Number.isInteger(epoch) || epoch <= 0 || epoch > 4294967295) {
6368
- throw new RangeError(`route epoch must be an integer in 1..4294967295, got ${epoch}`);
6369
- }
6370
- this.channel = channel;
6371
- this.epoch = epoch;
6372
- connectionToken.set(this, token);
6373
- Object.freeze(this);
6374
- }
6375
- static create(channel, epoch, token) {
6376
- return new RouteHandle(channel, epoch, token);
6377
- }
6378
- }
6379
- function createRouteHandle(channel, epoch, token) {
6380
- const factory = RouteHandle;
6381
- return factory.create(channel, epoch, token);
6382
- }
6383
-
6384
- class StaleRouteHandleError extends Error {
6385
- handle;
6386
- code = "stale_route_handle";
6387
- constructor(handle) {
6388
- super(`route handle (${handle.channel}, ${handle.epoch}) is not live on the current connection`);
6389
- this.handle = handle;
6390
- this.name = "StaleRouteHandleError";
6391
- }
6392
- }
6393
- function newConnectionToken() {
6394
- return Object.freeze({});
6395
- }
6396
- function belongsToConnection(handle, token) {
6397
- return connectionToken.get(handle) === token;
6398
- }
6399
- function sameRouteHandle(left, right) {
6400
- return left === right;
6401
- }
6402
-
6403
- // ../../node_modules/.bun/@cortexkit+subc-client@0.11.1/node_modules/@cortexkit/subc-client/dist/client.js
6404
- var debug = debuglog("subc-client");
6405
- var DEFAULT_HANDSHAKE_TIMEOUT_MS = 1e4;
6406
- var DEFAULT_REQUEST_TIMEOUT_MS = 30000;
6407
- var TIMEOUT_ARBITRATION_GRACE_MS = 50;
6408
- var LIVENESS_PROBE_WINDOW_MS = 2000;
6409
- var REQUEST_DEADLINE_MARKER = "request_deadline";
6410
- var DEADLINE_NO_DROP_CODE = "deadline_exceeded_no_drop_observed";
6411
- var ROUTE_OPEN_RETRY_DEADLINE_MS = 30000;
6412
- var BODY_READ_TIMEOUT_MS = 30000;
6413
- var EMPTY_BODY = new Uint8Array(0);
6414
- var DEFAULT_MANAGED_TARGET_KIND = "management_surface";
6415
- var SUBC_MODULE_ID_ENV = "SUBC_MODULE_ID";
6416
- var SUBC_LAUNCH_NONCE_ENV = "SUBC_LAUNCH_NONCE";
6417
- var DEFAULT_RECONNECT_BACKOFF = {
6418
- baseMs: 100,
6419
- capMs: 2000,
6420
- maxAttempts: 6
6421
- };
6422
-
6423
- class SubcCallError extends Error {
6424
- kind;
6425
- code;
6426
- cause;
6427
- constructor(kind, message, code, cause) {
6428
- super(message);
6429
- this.kind = kind;
6430
- this.code = code;
6431
- this.cause = cause;
6432
- this.name = "SubcCallError";
6433
- }
6434
- get detail() {
6435
- return this.cause instanceof SubcError ? this.cause.detail : undefined;
6436
- }
6437
- }
6438
-
6439
- class SubcError extends Error {
6440
- code;
6441
- detail;
6442
- constructor(message, code, detail) {
6443
- super(message);
6444
- this.code = code;
6445
- this.detail = detail;
6446
- }
6447
- }
6448
- function requireBinaryBody(body) {
6449
- if (body instanceof Uint8Array)
6450
- return body;
6451
- const type = body === null ? "null" : Array.isArray(body) ? "array" : typeof body;
6452
- throw new SubcError(`binary request body must be a Uint8Array; got ${type}`, "binary_body_required");
6453
- }
6454
- class SubcClient {
6455
- sock;
6456
- currentConn;
6457
- opts;
6458
- nextCorr = 1n;
6459
- pending = new Map;
6460
- lateResponses = new Map;
6461
- routes = new Map;
6462
- liveRoutes = new Map;
6463
- connectionToken = newConnectionToken();
6464
- ingressEpochDropCount = 0;
6465
- closedErr = null;
6466
- closeStarted = false;
6467
- reconnecting = null;
6468
- generation = 1;
6469
- readerActive = false;
6470
- constructor(sock, currentConn, opts) {
6471
- this.sock = sock;
6472
- this.currentConn = currentConn;
6473
- this.opts = opts;
6474
- this.readLoop(sock, this.generation);
6475
- }
6476
- get conn() {
6477
- return this.currentConn;
6478
- }
6479
- static async connect(opts) {
6480
- const normalized = normalizeConnectOptions(opts);
6481
- const opened = await SubcClient.openConnection(normalized);
6482
- return new SubcClient(opened.sock, opened.conn, normalized);
6483
- }
6484
- async catalogList(moduleId) {
6485
- const body = this.encode(moduleId === undefined ? { op: "catalog.list" } : { op: "catalog.list", module_id: moduleId });
6486
- const reply = await this.controlRpc(body);
6487
- const parsed = this.parseJson(reply);
6488
- return parsed.modules ?? [];
6489
- }
6490
- async resolveProvider(capability) {
6491
- const claimants = await this.resolveProviders(capability);
6492
- if (claimants.length === 0) {
6493
- throw new SubcError(`no catalog claimant for capability ${capability}`, "capability_unprovided");
6494
- }
6495
- if (claimants.length > 1) {
6496
- throw new SubcError(`multiple catalog claimants for capability ${capability}: ${claimants.join(", ")}`, "capability_ambiguous");
6497
- }
6498
- return claimants[0];
6499
- }
6500
- async resolveProviders(capability) {
6501
- if (!isValidCapabilityIdentifier(capability)) {
6502
- throw new SubcError(`malformed capability identifier ${JSON.stringify(capability)}`, "invalid_capability_identifier");
6503
- }
6504
- const modules = await this.catalogList();
6505
- return modules.filter((module) => module.capabilities?.provides.includes(capability) ?? false).map((module) => module.module_id).sort();
6506
- }
6507
- async routeOpen(target, identity, opts = {}) {
6508
- const consumerIdentity = routeOpenConsumerIdentity(opts);
6509
- const consumerCapabilities = opts.consumerCapabilities;
6510
- const body = this.encode({
6511
- op: "route.open",
6512
- target,
6513
- identity,
6514
- ...consumerIdentity ? { consumer_identity: consumerIdentity } : {},
6515
- ...consumerCapabilities !== undefined ? { consumer_capabilities: consumerCapabilities } : {}
6516
- });
6517
- let installed = null;
6518
- const install = (frame) => {
6519
- if (frame.header.ty !== FrameType.Response)
6520
- return true;
6521
- const parsed = this.parseJson(frame);
6522
- if (typeof parsed.route_channel !== "number" || typeof parsed.route_epoch !== "number") {
6523
- throw new SubcError(`route.open returned no route handle: ${JSON.stringify(parsed)}`);
6524
- }
6525
- installed = this.installRoute(parsed.route_channel, parsed.route_epoch);
6526
- return true;
6527
- };
6528
- const closeLateRoute = (frame) => {
6529
- if (frame.header.ty !== FrameType.Response)
6530
- return;
6531
- try {
6532
- const parsed = this.parseJson(frame);
6533
- if (typeof parsed.route_channel !== "number" || typeof parsed.route_epoch !== "number")
6534
- return;
6535
- const lateHandle = this.installRoute(parsed.route_channel, parsed.route_epoch);
6536
- this.failHandle(lateHandle, new SubcError("late route.open was closed", "route_closed"));
6537
- this.liveRoutes.delete(lateHandle.channel);
6538
- this.sendRouteGoodbye(lateHandle, true);
6539
- } catch {
6540
- this.closeConnectionAfterCleanupFailure();
6541
- }
6542
- };
6543
- await this.controlRpc(body, install, closeLateRoute);
6544
- if (!installed)
6545
- throw new SubcError("route.open response was not installed");
6546
- return installed;
6547
- }
6548
- async request(handle, body, opts = {}) {
6549
- this.assertLiveHandle(handle);
6550
- const binary = opts.binary ?? false;
6551
- const bytes = binary ? requireBinaryBody(body) : body instanceof Uint8Array ? body : this.encode(body);
6552
- const priority = opts.priority ?? Priority.Interactive;
6553
- const admission = opts.admissionClass ?? AdmissionClass.Normal;
6554
- const reply = await this.send(handle, bytes, priority, admission, opts.timeoutMs, opts.onProgress, undefined, undefined, binary);
6555
- return this.decodeReply(reply);
6556
- }
6557
- async call(moduleId, method, params, opts = {}) {
6558
- if (opts.binary) {
6559
- throw new SubcError("call() builds a JSON body and cannot send a binary request; use callBinary(moduleId, body, opts) with a Uint8Array", "binary_call_requires_call_binary");
6560
- }
6561
- const body = params === undefined ? { method } : { method, params };
6562
- return await this.managedCall(moduleId, body, opts);
6563
- }
6564
- async callBinary(moduleId, body, opts = {}) {
6565
- const bytes = requireBinaryBody(body);
6566
- return this.managedCall(moduleId, bytes, { ...opts, binary: true });
6567
- }
6568
- async managedCall(moduleId, body, opts) {
6569
- let retriedUnknownChannel = false;
6570
- for (;; ) {
6571
- const routeHandle = await this.cachedRouteHandle(moduleId, opts);
6572
- try {
6573
- return await this.managedRequest(routeHandle, body, opts);
6574
- } catch (err) {
6575
- if (!(err instanceof SubcCallError))
6576
- throw this.terminalCallError("managed call failed", err);
6577
- const deadBindCode = err.code === "unknown_channel" || err.code === "stale_route_epoch";
6578
- if (deadBindCode && !retriedUnknownChannel && !this.closeStarted) {
6579
- retriedUnknownChannel = true;
6580
- this.evictRouteHandle(routeHandle);
6581
- continue;
6582
- }
6583
- if (deadBindCode && retriedUnknownChannel) {
6584
- this.evictRouteHandle(routeHandle);
6585
- }
6586
- if (err.kind === "not_sent") {
6587
- try {
6588
- await this.reconnectAfterDrop(err);
6589
- } catch (reconnectErr) {
6590
- throw this.notSentRecoveryError("managed call was not sent", reconnectErr);
6591
- }
6592
- continue;
6593
- }
6594
- if (err.kind === "outcome_unknown" && err.code !== DEADLINE_NO_DROP_CODE) {
6595
- this.scheduleReconnectAfterDrop(err);
6596
- } else if (err.kind === "outcome_unknown" && err.code === DEADLINE_NO_DROP_CODE) {
6597
- this.probeLivenessAfterDeadline();
6598
- }
6599
- throw err;
6600
- }
6601
- }
6602
- }
6603
- subscribe(handle, body, onEvent, opts = {}) {
6604
- this.assertLiveHandle(handle);
6605
- const bytes = body instanceof Uint8Array ? body : this.encode(body);
6606
- const priority = opts.priority ?? Priority.Interactive;
6607
- const admission = opts.admissionClass ?? AdmissionClass.Normal;
6608
- const corr = this.allocateCorr();
6609
- const key = pendingKey(handle, corr);
6610
- let subscriptionPending = null;
6611
- let resolveClosed = null;
6612
- const closed = new Promise((resolve, reject) => {
6613
- if (this.closedErr) {
6614
- reject(this.closedErr);
6615
- return;
6616
- }
6617
- resolveClosed = resolve;
6618
- subscriptionPending = {
6619
- handle,
6620
- resolve: () => resolve(),
6621
- reject,
6622
- onProgress: onEvent,
6623
- timer: null,
6624
- subscription: true
6625
- };
6626
- this.pending.set(key, subscriptionPending);
6627
- const frame = buildFrame(FrameType.Request, buildFlags(false, priority, false, admission), handle.channel, handle.epoch, corr, bytes);
6628
- writeBorrowed(this.sock, encodeFrame(frame), Date.now() + DEFAULT_REQUEST_TIMEOUT_MS).catch((err) => {
6629
- const pending = this.pending.get(key);
6630
- if (pending)
6631
- this.rejectPending(key, pending, err instanceof Error ? err : new SubcError(String(err)));
6632
- });
6633
- });
6634
- let cancelled = false;
6635
- const unsubscribe = () => {
6636
- if (cancelled)
6637
- return;
6638
- cancelled = true;
6639
- if (subscriptionPending && resolveClosed)
6640
- this.settle(key, subscriptionPending, resolveClosed);
6641
- if (this.isLiveHandle(handle))
6642
- this.cancel(handle, corr, priority);
6643
- };
6644
- return { unsubscribe, closed };
6645
- }
6646
- cancel(handle, corr, priority = Priority.Interactive) {
6647
- this.assertLiveHandle(handle);
6648
- const cancel = buildFrame(FrameType.Cancel, buildFlags(false, priority, false), handle.channel, handle.epoch, corr, EMPTY_BODY);
6649
- writeBorrowed(this.sock, encodeFrame(cancel), Date.now() + DEFAULT_REQUEST_TIMEOUT_MS).catch(() => {
6650
- return;
6651
- });
6652
- }
6653
- async routePoll(handle, kind) {
6654
- this.assertLiveHandle(handle);
6655
- const body = this.encode({
6656
- op: "route.poll",
6657
- route_channel: handle.channel,
6658
- route_epoch: handle.epoch,
6659
- kind
6660
- });
6661
- const reply = await this.controlRpc(body, (frame) => {
6662
- if (frame.header.ty !== FrameType.Response)
6663
- return true;
6664
- const parsed = this.parseJson(frame);
6665
- return parsed.route_channel === handle.channel && parsed.route_epoch === handle.epoch;
6666
- });
6667
- return this.parseJson(reply);
6668
- }
6669
- async closeRoute(handle, opts = {}) {
6670
- this.assertLiveHandle(handle);
6671
- for (const [key, cached] of this.routes) {
6672
- if (cached.handle && sameRouteHandle(cached.handle, handle)) {
6673
- cached.closed = true;
6674
- cached.handle = null;
6675
- this.routes.delete(key);
6676
- }
6677
- }
6678
- if (opts.drain)
6679
- await this.drainUnaryOnHandle(handle);
6680
- this.failHandle(handle, new SubcError("route closed by closeRoute", "route_closed"));
6681
- if (this.liveRoutes.get(handle.channel) === handle)
6682
- this.liveRoutes.delete(handle.channel);
6683
- this.sendRouteGoodbye(handle);
6684
- }
6685
- async closeManagedRoute(target, identity, opts = {}) {
6686
- const key = routeCacheKey(target, identity, routeOpenConsumerIdentity(opts));
6687
- const cached = this.routes.get(key);
6688
- if (!cached)
6689
- return;
6690
- cached.closed = true;
6691
- this.routes.delete(key);
6692
- const handle = cached.handle;
6693
- cached.handle = null;
6694
- if (handle)
6695
- await this.closeRoute(handle, opts);
6696
- }
6697
- async closeRouteChannel(handle, opts = {}) {
6698
- await this.closeRoute(handle, opts);
6699
- }
6700
- close() {
6701
- this.closeStarted = true;
6702
- this.fail(new SubcError("client closed"));
6703
- this.sock.close();
6704
- }
6705
- drainUnaryOnHandle(handle) {
6706
- const waiters = [];
6707
- for (const pending of this.pending.values()) {
6708
- if (pending.handle === handle && !pending.subscription) {
6709
- waiters.push(new Promise((resolve) => {
6710
- const previous = pending.onSettle;
6711
- pending.onSettle = () => {
6712
- previous?.();
6713
- resolve();
6714
- };
6715
- }));
6716
- }
6717
- }
6718
- return Promise.all(waiters).then(() => {
6719
- return;
6720
- });
6721
- }
6722
- sendRouteGoodbye(handle, closeOnQueueFailure = false) {
6723
- this.assertLiveConnection(handle);
6724
- if (this.closedErr) {
6725
- if (closeOnQueueFailure)
6726
- this.closeConnectionAfterCleanupFailure();
6727
- return;
6728
- }
6729
- const goodbye = buildFrame(FrameType.Goodbye, buildFlags(false, Priority.Interactive, false), handle.channel, handle.epoch, 0n, EMPTY_BODY);
6730
- const write = writeTrackedBorrowed(this.sock, encodeFrame(goodbye), Date.now() + DEFAULT_REQUEST_TIMEOUT_MS);
6731
- if (!write.queued && closeOnQueueFailure)
6732
- this.closeConnectionAfterCleanupFailure();
6733
- write.completed.catch(() => {
6734
- if (closeOnQueueFailure && !write.queued)
6735
- this.closeConnectionAfterCleanupFailure();
6736
- });
6737
- }
6738
- static async openConnection(opts) {
6739
- const conn = await readConnectionFile(opts.connectionFile);
6740
- const deadline = Date.now() + (opts.handshakeTimeoutMs ?? DEFAULT_HANDSHAKE_TIMEOUT_MS);
6741
- const endpoint = conn.endpoints[0];
6742
- const sock = await SubcSocket.connect(endpoint.host, endpoint.port, deadline);
6743
- try {
6744
- await authenticateClient(sock, conn, deadline);
6745
- } catch (err) {
6746
- sock.close();
6747
- throw err;
6748
- }
6749
- return { sock, conn };
6750
- }
6751
- async controlRpc(body, acceptFrame, onLateResponse) {
6752
- return this.send(null, body, Priority.Interactive, AdmissionClass.Normal, undefined, undefined, acceptFrame, onLateResponse);
6753
- }
6754
- send(handle, body, priority, admission, timeoutMs, onProgress, acceptFrame, onLateResponse, binary = false) {
6755
- if (handle)
6756
- this.assertLiveHandle(handle);
6757
- if (this.closedErr)
6758
- return Promise.reject(this.closedErr);
6759
- let corr;
6760
- try {
6761
- corr = this.allocateCorr();
6762
- } catch (error) {
6763
- return Promise.reject(error);
6764
- }
6765
- const key = pendingKey(handle, corr);
6766
- const channel = handle?.channel ?? 0;
6767
- const epoch = handle?.epoch ?? 0;
6768
- const frame = buildFrame(FrameType.Request, buildFlags(binary, priority, false, admission), channel, epoch, corr, body);
6769
- return new Promise((resolve, reject) => {
6770
- const ms = timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
6771
- const pending = {
6772
- handle,
6773
- resolve,
6774
- reject,
6775
- onProgress,
6776
- timer: null,
6777
- acceptFrame,
6778
- onLateResponse
6779
- };
6780
- pending.timer = setTimeout(() => this.arbitrateTimeout(key, pending, channel, corr, ms), ms);
6781
- this.pending.set(key, pending);
6782
- writeBorrowed(this.sock, encodeFrame(frame), Date.now() + ms).catch((error) => {
6783
- const current = this.pending.get(key);
6784
- if (current)
6785
- this.rejectPending(key, current, error instanceof Error ? error : new SubcError(String(error)));
6786
- });
6787
- });
6788
- }
6789
- arbitrateTimeout(key, pending, channel, corr, ms) {
6790
- const settleAsTimeout = () => {
6791
- if (pending.onLateResponse)
6792
- this.lateResponses.set(key, pending.onLateResponse);
6793
- this.rejectPending(key, pending, new SubcError(this.timeoutMessage(channel, corr, ms), REQUEST_DEADLINE_MARKER));
6794
- };
6795
- const graceDeadline = Date.now() + this.opts.timeoutArbitrationGraceMs;
6796
- const arbitrate = () => {
6797
- if (this.pending.get(key) !== pending)
6798
- return;
6799
- const readerDraining = this.readerActive || this.sock.bufferedBytes() > 0;
6800
- if (readerDraining && Date.now() < graceDeadline) {
6801
- setImmediate(arbitrate);
6802
- return;
6803
- }
6804
- settleAsTimeout();
6805
- };
6806
- setImmediate(arbitrate);
6807
- }
6808
- async managedRequest(handle, body, opts) {
6809
- const binary = opts.binary ?? false;
6810
- const bytes = binary ? requireBinaryBody(body) : body instanceof Uint8Array ? body : this.encode(body);
6811
- const priority = opts.priority ?? Priority.Interactive;
6812
- const admission = opts.admissionClass ?? AdmissionClass.Normal;
6813
- try {
6814
- const reply = await this.sendManaged(handle, bytes, priority, admission, opts.timeoutMs, opts.onProgress, binary);
6815
- return this.decodeReply(reply);
6816
- } catch (error) {
6817
- if (error instanceof SubcCallError)
6818
- throw error;
6819
- throw this.terminalCallError("managed call failed", error);
6820
- }
6821
- }
6822
- sendManaged(handle, body, priority, admission, timeoutMs, onProgress, binary = false) {
6823
- try {
6824
- this.assertLiveHandle(handle);
6825
- } catch (error) {
6826
- return Promise.reject(this.notSentCallError("request used a stale route handle", error));
6827
- }
6828
- if (this.closedErr) {
6829
- return Promise.reject(this.notSentCallError("request was not sent because the subc connection was already closed", this.closedErr));
6830
- }
6831
- let corr;
6832
- try {
6833
- corr = this.allocateCorr();
6834
- } catch (error) {
6835
- return Promise.reject(this.notSentCallError("request correlation allocator was exhausted", error));
6836
- }
6837
- const key = pendingKey(handle, corr);
6838
- const frame = buildFrame(FrameType.Request, buildFlags(binary, priority, false, admission), handle.channel, handle.epoch, corr, body);
6839
- let handedToSocket = false;
6840
- const classifyFailure = (error) => {
6841
- if (!handedToSocket)
6842
- return this.notSentCallError("request bytes were not queued to the subc socket", error);
6843
- if (error instanceof SubcError && error.code === REQUEST_DEADLINE_MARKER) {
6844
- return new SubcCallError("outcome_unknown", `managed call deadline exceeded after request bytes were queued to the local socket; no terminal response was observed; outcome unknown${causeMessage(error)}`, DEADLINE_NO_DROP_CODE, error);
6845
- }
6846
- return this.outcomeUnknownCallError("connection dropped before the managed call returned a response", error);
6847
- };
6848
- return new Promise((resolve, reject) => {
6849
- const ms = timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
6850
- const pending = {
6851
- handle,
6852
- resolve,
6853
- reject,
6854
- onProgress,
6855
- timer: null,
6856
- classifyFailure
6857
- };
6858
- pending.timer = setTimeout(() => this.arbitrateTimeout(key, pending, handle.channel, corr, ms), ms);
6859
- this.pending.set(key, pending);
6860
- const write = writeTrackedBorrowed(this.sock, encodeFrame(frame), Date.now() + ms);
6861
- handedToSocket = write.queued;
6862
- write.completed.catch((error) => {
6863
- const current = this.pending.get(key);
6864
- if (current)
6865
- this.rejectPending(key, current, error instanceof Error ? error : new SubcError(String(error)));
6866
- });
6867
- });
6868
- }
6869
- async cachedRouteHandle(moduleId, opts) {
6870
- const identity = opts.identity ?? this.opts.identity;
6871
- if (!identity) {
6872
- throw new SubcCallError("terminal", "managed call requires a BindIdentity in SubcClient.connect({ identity }) or call(..., { identity })", "missing_identity");
6873
- }
6874
- const target = { kind: opts.targetKind ?? this.opts.targetKind, module_id: moduleId };
6875
- const consumerIdentity = routeOpenConsumerIdentity(opts);
6876
- const key = routeCacheKey(target, identity, consumerIdentity);
6877
- let cached = this.routes.get(key);
6878
- if (!cached) {
6879
- cached = {
6880
- key,
6881
- moduleId,
6882
- target,
6883
- identity,
6884
- consumerIdentity,
6885
- handle: null,
6886
- opening: null
6887
- };
6888
- this.routes.set(key, cached);
6889
- }
6890
- if (cached.handle && this.isLiveHandle(cached.handle))
6891
- return cached.handle;
6892
- if (!cached.opening) {
6893
- cached.opening = this.openCachedRoute(cached).finally(() => {
6894
- cached.opening = null;
6895
- });
6896
- }
6897
- return cached.opening;
6898
- }
6899
- async openCachedRoute(cached) {
6900
- const routeRetryDeadline = Date.now() + this.opts.routeOpenRetryDeadlineMs;
6901
- let routeRetryDelay = this.opts.reconnectBackoff.baseMs;
6902
- let routeRetryAttempt = 0;
6903
- for (;; ) {
6904
- if (cached.closed)
6905
- throw this.routeClosedDuringOpen();
6906
- try {
6907
- await this.ensureConnectedForManaged();
6908
- } catch (error) {
6909
- throw this.notSentRecoveryError("route.open could not run because reconnect failed", error);
6910
- }
6911
- if (cached.handle && this.isLiveHandle(cached.handle))
6912
- return cached.handle;
6913
- try {
6914
- const handle = await this.routeOpen(cached.target, cached.identity, {
6915
- consumerIdentity: cached.consumerIdentity ?? null
6916
- });
6917
- if (cached.closed) {
6918
- this.liveRoutes.delete(handle.channel);
6919
- this.sendRouteGoodbye(handle);
6920
- throw this.routeClosedDuringOpen();
6921
- }
6922
- cached.handle = handle;
6923
- return handle;
6924
- } catch (error) {
6925
- if (error instanceof SubcCallError && error.code === "route_closed")
6926
- throw error;
6927
- if (!this.closeStarted && isConsumerReconnectTransient(error)) {
6928
- try {
6929
- await this.reconnectAfterDrop(error);
6930
- } catch (reconnectError) {
6931
- throw this.notSentRecoveryError("route.open was not sent and reconnect failed", reconnectError);
6932
- }
6933
- continue;
6934
- }
6935
- if (!this.closeStarted && error instanceof SubcError && isRetryableRouteOpenCode(error.code)) {
6936
- routeRetryAttempt += 1;
6937
- if (Date.now() < routeRetryDeadline) {
6938
- await this.opts.sleep(routeRetryDelay);
6939
- routeRetryDelay = Math.min(routeRetryDelay * 2, this.opts.reconnectBackoff.capMs);
6940
- continue;
6941
- }
6942
- throw this.notSentCallError(`route.open failed for module ${cached.moduleId}: ${error.code} (retry deadline exhausted after ${routeRetryAttempt} attempts)`, error);
6943
- }
6944
- throw this.terminalCallError(`route.open failed for module ${cached.moduleId}`, error);
6945
- }
6946
- }
6947
- }
6948
- lastInboundAtMs = 0;
6949
- livenessProbe = null;
6950
- hasControlPending() {
6951
- for (const pending of this.pending.values()) {
6952
- if (pending.handle === null)
6953
- return true;
6954
- }
6955
- return false;
6956
- }
6957
- probeLivenessAfterDeadline() {
6958
- if (this.livenessProbe || this.closeStarted || this.closedErr)
6959
- return;
6960
- if (this.hasControlPending())
6961
- return;
6962
- const sock = this.sock;
6963
- const generation = this.generation;
6964
- let corr;
6965
- try {
6966
- corr = this.allocateCorr();
6967
- } catch {
6968
- return;
6969
- }
6970
- const t0 = Date.now();
6971
- const ping = buildFrame(FrameType.Ping, buildFlags(false, Priority.Interactive, false, AdmissionClass.Normal), 0, 0, corr, new Uint8Array);
6972
- const probe = (async () => {
6973
- await writeBorrowed(sock, encodeFrame(ping), t0 + this.opts.livenessProbeWindowMs).catch(() => {});
6974
- await this.opts.sleep(this.opts.livenessProbeWindowMs);
6975
- if (this.sock !== sock || this.generation !== generation || this.closeStarted)
6976
- return;
6977
- if (this.lastInboundAtMs >= t0)
6978
- return;
6979
- if (this.hasControlPending())
6980
- return;
6981
- this.fail(new SocketClosedError(`liveness probe convicted a half-open socket: no inbound frame for ${this.opts.livenessProbeWindowMs}ms after a channel-0 Ping (deadline-no-drop settles preceded this); closing so the next call reconnects`));
6982
- sock.close();
6983
- })().finally(() => {
6984
- this.livenessProbe = null;
6985
- });
6986
- this.livenessProbe = probe;
6987
- }
6988
- async ensureConnectedForManaged() {
6989
- if (this.closeStarted)
6990
- throw new SubcError("client closed");
6991
- if (this.reconnecting)
6992
- await this.reconnecting;
6993
- if (this.closedErr)
6994
- await this.reconnectAfterDrop(this.closedErr);
6995
- }
6996
- scheduleReconnectAfterDrop(err) {
6997
- if (this.closeStarted || this.reconnecting)
6998
- return;
6999
- this.reconnectAfterDrop(err).catch(() => {});
7000
- }
7001
- reconnectAfterDrop(trigger) {
7002
- if (this.closeStarted)
7003
- return Promise.reject(new SubcError("client closed"));
7004
- if (this.reconnecting)
7005
- return this.reconnecting;
7006
- const promise = this.reconnectWithRetry(trigger).finally(() => {
7007
- if (this.reconnecting === promise)
7008
- this.reconnecting = null;
7009
- });
7010
- this.reconnecting = promise;
7011
- return promise;
7012
- }
7013
- async reconnectWithRetry(_trigger) {
7014
- let attempt = 0;
7015
- let delay = this.opts.reconnectBackoff.baseMs;
7016
- for (;; ) {
7017
- if (this.closeStarted)
7018
- throw new SubcError("client closed");
7019
- attempt += 1;
7020
- try {
7021
- const opened = await SubcClient.openConnection(this.opts);
7022
- if (this.closeStarted) {
7023
- opened.sock.close();
7024
- throw new SubcError("client closed");
7025
- }
7026
- this.replaceConnection(opened);
7027
- await this.reopenCachedRoutes();
7028
- return;
7029
- } catch (err) {
7030
- if (!isConsumerReconnectTransient(err) || attempt >= this.opts.reconnectBackoff.maxAttempts) {
7031
- if (err instanceof AuthError && attempt > 1) {
7032
- throw new AuthError(`reconnect gave up after ${attempt} attempts: ${err.message} — the connection file and the daemon's key disagree persistently ` + `(daemon restarting in a loop, split connection-file paths, or a genuinely foreign daemon on this port); ` + `check the daemon, then restart this host app`);
7033
- }
7034
- throw err;
7035
- }
7036
- await this.opts.sleep(delay);
7037
- delay = Math.min(delay * 2, this.opts.reconnectBackoff.capMs);
7038
- }
7039
- }
7040
- }
7041
- replaceConnection(opened) {
7042
- this.sock.close();
7043
- this.sock = opened.sock;
7044
- this.currentConn = opened.conn;
7045
- this.closedErr = null;
7046
- this.generation += 1;
7047
- this.connectionToken = newConnectionToken();
7048
- this.liveRoutes.clear();
7049
- this.lateResponses.clear();
7050
- this.nextCorr = 1n;
7051
- this.readLoop(opened.sock, this.generation);
7052
- }
7053
- async reopenCachedRoutes() {
7054
- const routeKeys = [...this.routes.keys()];
7055
- for (const key of routeKeys) {
7056
- const cached = this.routes.get(key);
7057
- if (cached)
7058
- cached.handle = null;
7059
- }
7060
- for (const key of routeKeys) {
7061
- const cached = this.routes.get(key);
7062
- if (!cached || cached.closed)
7063
- continue;
7064
- try {
7065
- const handle = await this.routeOpen(cached.target, cached.identity, {
7066
- consumerIdentity: cached.consumerIdentity ?? null
7067
- });
7068
- if (cached.closed) {
7069
- this.liveRoutes.delete(handle.channel);
7070
- this.sendRouteGoodbye(handle);
7071
- continue;
7072
- }
7073
- cached.handle = handle;
7074
- } catch (error) {
7075
- if (isRouteOpenRefusal(error)) {
7076
- this.routes.delete(key);
7077
- continue;
7078
- }
7079
- throw error;
7080
- }
7081
- }
7082
- }
7083
- timeoutMessage(channel, corr, ms) {
7084
- const port = this.sock.localPort();
7085
- const where = port === null ? "channel" : `local_port=${port} channel`;
7086
- return `request on ${where} ${channel} corr ${corr} timed out after ${ms}ms`;
7087
- }
7088
- routeClosedDuringOpen() {
7089
- return new SubcCallError("not_sent", "route was closed before route.open completed", "route_closed");
7090
- }
7091
- async readLoop(sock, generation) {
7092
- try {
7093
- for (;; ) {
7094
- this.readerActive = false;
7095
- const frame = await sock.readFrame(Number.POSITIVE_INFINITY, { afterHeaderMs: BODY_READ_TIMEOUT_MS }, () => {
7096
- this.readerActive = true;
7097
- });
7098
- try {
7099
- if (this.sock === sock && this.generation === generation)
7100
- this.dispatch(frame);
7101
- } finally {
7102
- this.readerActive = false;
7103
- }
7104
- }
7105
- } catch (error) {
7106
- if (this.sock === sock && this.generation === generation) {
7107
- this.fail(error instanceof Error ? error : new SubcError(String(error)));
7108
- }
7109
- }
7110
- }
7111
- dispatch(frame) {
7112
- this.lastInboundAtMs = Date.now();
7113
- if (frame.header.channel === 0 && frame.header.ty === FrameType.Push) {
7114
- const observer = this.opts.onControlPush;
7115
- if (observer) {
7116
- let parsed = null;
7117
- try {
7118
- const body = this.parseJson(frame);
7119
- if (body && typeof body.op === "string")
7120
- parsed = { op: body.op, body };
7121
- } catch {}
7122
- if (parsed) {
7123
- try {
7124
- observer(parsed);
7125
- } catch {}
7126
- }
7127
- }
7128
- return;
7129
- }
7130
- let handle = null;
7131
- if (frame.header.channel !== 0) {
7132
- handle = this.liveRoutes.get(frame.header.channel) ?? null;
7133
- if (!handle || handle.epoch !== frame.header.epoch) {
7134
- this.ingressEpochDropCount += 1;
7135
- return;
7136
- }
7137
- }
7138
- const key = pendingKey(handle, frame.header.corr);
7139
- const pending = this.pending.get(key);
7140
- if (pending) {
7141
- if (pending.acceptFrame && !pending.acceptFrame(frame))
7142
- return;
7143
- switch (frame.header.ty) {
7144
- case FrameType.Push:
7145
- case FrameType.StreamData:
7146
- try {
7147
- pending.onProgress?.(frame.body);
7148
- } catch {}
7149
- return;
7150
- case FrameType.Response:
7151
- case FrameType.StreamEnd:
7152
- this.settle(key, pending, () => pending.resolve(frame));
7153
- return;
7154
- case FrameType.Error:
7155
- this.settle(key, pending, () => pending.reject(this.errorFromFrame(frame)));
7156
- return;
7157
- default:
7158
- return;
7159
- }
7160
- }
7161
- const late = this.lateResponses.get(key);
7162
- if (late && (frame.header.ty === FrameType.Response || frame.header.ty === FrameType.Error)) {
7163
- this.lateResponses.delete(key);
7164
- late(frame);
7165
- return;
7166
- }
7167
- if (frame.header.ty === FrameType.Goodbye && handle) {
7168
- this.failHandle(handle, new SubcError("route closed by subc (GOODBYE)", "route_closed"));
7169
- if (this.liveRoutes.get(handle.channel) === handle)
7170
- this.liveRoutes.delete(handle.channel);
7171
- this.evictRouteHandle(handle);
7172
- return;
7173
- }
7174
- if (frame.header.ty === FrameType.Response || frame.header.ty === FrameType.Error || frame.header.ty === FrameType.StreamEnd) {
7175
- debug("dropped terminal frame with no waiter: type=%d channel=%d epoch=%d corr=%s port=%s", frame.header.ty, frame.header.channel, frame.header.epoch, frame.header.corr, this.sock.localPort() ?? "?");
7176
- }
7177
- }
7178
- settle(key, pending, run) {
7179
- if (this.pending.get(key) !== pending)
7180
- return false;
7181
- this.pending.delete(key);
7182
- if (pending.timer)
7183
- clearTimeout(pending.timer);
7184
- run();
7185
- pending.onSettle?.();
7186
- return true;
7187
- }
7188
- rejectPending(key, pending, err) {
7189
- this.settle(key, pending, () => pending.reject(pending.classifyFailure?.(err) ?? err));
7190
- }
7191
- errorFromFrame(frame) {
7192
- try {
7193
- const parsed = JSON.parse(Buffer.from(frame.body).toString("utf8"));
7194
- return new SubcError(parsed.message ?? "subc error", parsed.code, parsed.detail);
7195
- } catch {
7196
- return new SubcError(Buffer.from(frame.body).toString("utf8") || "subc error");
7197
- }
7198
- }
7199
- evictRouteHandle(handle) {
7200
- for (const cached of this.routes.values()) {
7201
- if (cached.handle && sameRouteHandle(cached.handle, handle))
7202
- cached.handle = null;
7203
- }
7204
- }
7205
- failHandle(handle, error) {
7206
- for (const [key, pending] of this.pending) {
7207
- if (pending.handle && sameRouteHandle(pending.handle, handle))
7208
- this.rejectPending(key, pending, error);
7209
- }
7210
- }
7211
- fail(err) {
7212
- if (!this.closedErr)
7213
- this.closedErr = err;
7214
- for (const [key, pending] of this.pending) {
7215
- this.rejectPending(key, pending, err);
7216
- }
7217
- }
7218
- notSentCallError(message, cause) {
7219
- return new SubcCallError("not_sent", `${message}${causeMessage(cause)}`, errorCode(cause), cause);
7220
- }
7221
- outcomeUnknownCallError(message, cause) {
7222
- return new SubcCallError("outcome_unknown", `${message}${causeMessage(cause)}`, errorCode(cause), cause);
7223
- }
7224
- terminalCallError(message, cause) {
7225
- if (cause instanceof SubcCallError)
7226
- return cause;
7227
- return new SubcCallError("terminal", `${message}${causeMessage(cause)}`, errorCode(cause), cause);
7228
- }
7229
- notSentRecoveryError(message, cause) {
7230
- if (cause instanceof SubcCallError)
7231
- return cause;
7232
- if (isConsumerReconnectTransient(cause))
7233
- return this.notSentCallError(message, cause);
7234
- return this.terminalCallError(message, cause);
7235
- }
7236
- get droppedIngressFrames() {
7237
- return this.ingressEpochDropCount;
7238
- }
7239
- installRoute(channel, epoch) {
7240
- const handle = createRouteHandle(channel, epoch, this.connectionToken);
7241
- this.liveRoutes.set(channel, handle);
7242
- return handle;
7243
- }
7244
- isLiveHandle(handle) {
7245
- return belongsToConnection(handle, this.connectionToken) && this.liveRoutes.get(handle.channel) === handle;
7246
- }
7247
- assertLiveConnection(handle) {
7248
- if (!belongsToConnection(handle, this.connectionToken))
7249
- throw new StaleRouteHandleError(handle);
7250
- }
7251
- assertLiveHandle(handle) {
7252
- if (!this.isLiveHandle(handle))
7253
- throw new StaleRouteHandleError(handle);
7254
- }
7255
- allocateCorr() {
7256
- const maximum = 0xffffffffffffffffn;
7257
- if (this.nextCorr > maximum) {
7258
- const error = new SubcError("channel-0 correlation id allocator exhausted", "corr_exhausted");
7259
- this.fail(error);
7260
- this.sock.close();
7261
- this.scheduleReconnectAfterDrop(error);
7262
- throw error;
7263
- }
7264
- const corr = this.nextCorr;
7265
- this.nextCorr += 1n;
7266
- return corr;
7267
- }
7268
- closeConnectionAfterCleanupFailure() {
7269
- const error = new SubcError("late route cleanup could not be queued", "late_route_cleanup_failed");
7270
- this.fail(error);
7271
- this.sock.close();
7272
- this.scheduleReconnectAfterDrop(error);
7273
- }
7274
- encode(value) {
7275
- return Buffer.from(JSON.stringify(value), "utf8");
7276
- }
7277
- decodeReply(frame) {
7278
- return hasBinary(frame.header.flags) ? frame.body : this.parseJson(frame);
7279
- }
7280
- parseJson(frame) {
7281
- const b = frame.body;
7282
- return JSON.parse(Buffer.from(b.buffer, b.byteOffset, b.byteLength).toString("utf8"));
7283
- }
7284
- }
7285
- function isConsumerReconnectTransient(err) {
7286
- if (err instanceof SocketClosedError || err instanceof SocketTimeoutError)
7287
- return true;
7288
- if (err instanceof SocketWriteNotQueuedError || err instanceof SocketWriteQueuedError)
7289
- return true;
7290
- if (err instanceof SubcCallError)
7291
- return err.kind === "not_sent" || err.kind === "outcome_unknown";
7292
- if (err instanceof AuthError)
7293
- return true;
7294
- if (err instanceof SubcError || err instanceof ConnectionFileError)
7295
- return false;
7296
- const code = errorCode(err);
7297
- return code === "ECONNREFUSED" || code === "ECONNRESET" || code === "EPIPE" || code === "ETIMEDOUT" || code === "ENOENT";
7298
- }
7299
- function isRouteOpenRefusal(err) {
7300
- return err instanceof SubcError && typeof err.code === "string";
7301
- }
7302
- function isRetryableRouteOpenCode(code) {
7303
- if (code === "capability_forbidden")
7304
- return false;
7305
- return code === "unknown_module" || code === "module_reloading" || code === "module_warming" || code === "target_unavailable" || code === "module_timeout";
7306
- }
7307
- async function connectionFileExists(path) {
7308
- try {
7309
- await fs2.access(path);
7310
- return true;
7311
- } catch {
7312
- return false;
7313
- }
7314
- }
7315
- function normalizeConnectOptions(opts) {
7316
- return {
7317
- connectionFile: opts.connectionFile,
7318
- handshakeTimeoutMs: opts.handshakeTimeoutMs,
7319
- identity: opts.identity,
7320
- targetKind: opts.targetKind ?? DEFAULT_MANAGED_TARGET_KIND,
7321
- reconnectBackoff: opts.reconnectBackoff ?? DEFAULT_RECONNECT_BACKOFF,
7322
- sleep: opts.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms))),
7323
- routeOpenRetryDeadlineMs: opts.routeOpenRetryDeadlineMs ?? ROUTE_OPEN_RETRY_DEADLINE_MS,
7324
- timeoutArbitrationGraceMs: opts.timeoutArbitrationGraceMs ?? TIMEOUT_ARBITRATION_GRACE_MS,
7325
- livenessProbeWindowMs: opts.livenessProbeWindowMs ?? LIVENESS_PROBE_WINDOW_MS,
7326
- onControlPush: opts.onControlPush
7327
- };
7328
- }
7329
- function isValidCapabilityIdentifier(identifier) {
7330
- if (/\s/u.test(identifier))
7331
- return false;
7332
- const separator = identifier.indexOf("/v");
7333
- if (separator <= 0 || identifier.indexOf("/v", separator + 1) !== -1)
7334
- return false;
7335
- const name = identifier.slice(0, separator);
7336
- const version = identifier.slice(separator + 2);
7337
- if (new TextEncoder().encode(name).byteLength > 64 || version.length === 0)
7338
- return false;
7339
- if (!/^[a-z][a-z0-9-]*[a-z0-9]$/u.test(name) && !/^[a-z]$/u.test(name))
7340
- return false;
7341
- if (name.includes("--"))
7342
- return false;
7343
- if (!/^\d+$/u.test(version) || version.length > 1 && version.startsWith("0"))
7344
- return false;
7345
- const numericVersion = Number(version);
7346
- return Number.isSafeInteger(numericVersion) && numericVersion >= 1 && numericVersion <= 4294967295;
7347
- }
7348
- function routeCacheKey(target, identity, consumerIdentity) {
7349
- const consumerPart = consumerIdentity ? `${consumerIdentity.module_id}\x00${consumerIdentity.launch_nonce}` : "";
7350
- return `${target.kind}\x00${target.module_id}\x00${identity.project_root}\x00${identity.harness}\x00${identity.session}\x00${consumerPart}`;
7351
- }
7352
- function routeOpenConsumerIdentity(opts = {}) {
7353
- if (opts.consumerIdentity !== undefined)
7354
- return opts.consumerIdentity ?? undefined;
7355
- const moduleId = process.env[SUBC_MODULE_ID_ENV];
7356
- const launchNonce = process.env[SUBC_LAUNCH_NONCE_ENV];
7357
- if (!moduleId || !launchNonce)
7358
- return;
7359
- return { module_id: moduleId, launch_nonce: launchNonce };
7360
- }
7361
- function errorCode(err) {
7362
- if (typeof err === "object" && err !== null && "code" in err) {
7363
- const code = err.code;
7364
- if (typeof code === "string")
7365
- return code;
7366
- }
7367
- return;
7368
- }
7369
- function causeMessage(cause) {
7370
- if (cause === undefined)
7371
- return "";
7372
- return `: ${cause instanceof Error ? cause.message : String(cause)}`;
7373
- }
7374
- function pendingKey(handle, corr) {
7375
- return handle ? `${handle.channel}:${handle.epoch}:${corr}` : `0:0:${corr}`;
7376
- }
7377
- // ../plugin/src/features/magic-context/memory/embedding-synapse.ts
7378
- var SYNAPSE_DEFAULT_MODEL = "gte-modernbert-base-f16";
7379
- var SYNAPSE_DEFAULT_QUERY_TIMEOUT_MS = 3000;
7380
- var SYNAPSE_DEFAULT_BATCH_TIMEOUT_MS = 120000;
7381
- var SYNAPSE_CERTIFICATION_REFUSAL_REASONS = new Set([
7382
- "not_certified",
7383
- "probe_required",
7384
- "migration_required"
7385
- ]);
7386
- var embeddingRowMetadata = new WeakMap;
7387
- function isSynapseEmbeddingTruncated(vector) {
7388
- return embeddingRowMetadata.get(vector)?.truncated === true;
7389
- }
7390
- function toSynapseLaneDescriptor(metadata) {
7391
- const tokenBudget = metadata.recommended_token_budget;
7392
- return {
7393
- lane: metadata.model,
7394
- ...metadata.device_class ? { device_class: metadata.device_class } : {},
7395
- max_tokens: metadata.max_tokens,
7396
- max_tokens_source: metadata.max_tokens_source,
7397
- ...metadata.bucket_ladder ? { bucket_ladder: [...metadata.bucket_ladder] } : {},
7398
- ...metadata.dims ? { dims: metadata.dims } : {},
7399
- ...metadata.dtype ? { dtype: metadata.dtype } : {},
7400
- ...typeof metadata.certified === "boolean" ? { certified: metadata.certified } : {},
7401
- ...metadata.warm_load_cost_hint_ms !== undefined ? { warm_load_cost_hint_ms: metadata.warm_load_cost_hint_ms } : {},
7402
- ...metadata.recommended_batch ? {
7403
- recommended_batch: {
7404
- rows: metadata.recommended_batch,
7405
- ...tokenBudget !== undefined ? { token_budget: tokenBudget } : {}
7406
- }
7407
- } : {},
7408
- warm: metadata.max_tokens_source === "runtime_bucket" || metadata.max_tokens_source === "worker_bucket"
7409
- };
7410
- }
7411
- function formatSynapseLaneDescriptor(descriptor) {
7412
- const certified = descriptor.certified === undefined ? "unknown" : String(descriptor.certified);
7413
- return `lane=${descriptor.lane}; device_class=${descriptor.device_class ?? "unknown"}; ` + `max_tokens=${descriptor.max_tokens} (${descriptor.max_tokens_source}); ` + `certified=${certified}; warm=${descriptor.warm ? "yes" : "no"}; ` + `warm_load_cost_hint_ms=${descriptor.warm_load_cost_hint_ms ?? "unknown"}`;
7414
- }
7415
-
7416
- class SynapseEmbeddingError extends Error {
7417
- code;
7418
- retryAfterMs;
7419
- permanent;
7420
- refusalReason;
7421
- constructor(code, message, options) {
7422
- super(message, options?.cause === undefined ? undefined : { cause: options.cause });
7423
- this.name = "SynapseEmbeddingError";
7424
- this.code = code;
7425
- this.permanent = options?.permanent ?? isPermanentSynapseCode(code);
7426
- this.retryAfterMs = options?.retryAfterMs ?? (this.permanent ? undefined : 100);
7427
- this.refusalReason = options?.refusalReason;
7428
- }
7429
- }
7430
- function isPermanentSynapseCode(code) {
7431
- return code === "artifact_invalid" || code === "substitution_rejected" || code === "not_certified" || code === "probe_required" || code === "idempotency_conflict" || code === "schema_violation" || code === "certification_refused" || code === "needs_reauth";
7432
- }
7433
- function asRecord(value) {
7434
- return value !== null && typeof value === "object" ? value : null;
7435
- }
7436
- function responseBody(value) {
7437
- const record = asRecord(value);
7438
- const result = asRecord(record?.result);
7439
- const payload = asRecord(result?.payload ?? record?.payload);
7440
- return {
7441
- ...record ?? {},
7442
- ...result ?? {},
7443
- ...payload ?? {}
7444
- };
7445
- }
7446
- function readRetryAfter(value) {
7447
- const record = asRecord(value);
7448
- const candidate = record?.retry_after_ms ?? record?.retryAfterMs;
7449
- if (typeof candidate !== "number" || !Number.isFinite(candidate) || candidate < 0)
7450
- return;
7451
- return Math.ceil(candidate);
7452
- }
7453
- function readErrorCode(value) {
7454
- const record = asRecord(value);
7455
- if (typeof record?.code === "string")
7456
- return record.code;
7457
- if (value instanceof Error && "code" in value && typeof value.code === "string") {
7458
- return value.code;
7459
- }
7460
- return;
7461
- }
7462
- function readCertificationRefusalReason(value) {
7463
- if (!(value instanceof SubcCallError))
7464
- return;
7465
- const detail = value.detail;
7466
- if (typeof detail === "string")
7467
- return detail;
7468
- const record = asRecord(detail);
7469
- return typeof record?.reason === "string" ? record.reason : undefined;
7470
- }
7471
- function classifyError(value) {
7472
- if (value instanceof SynapseEmbeddingError)
7473
- return value;
7474
- const code = readErrorCode(value) ?? (value instanceof Error ? value.name : "transport");
7475
- const normalized = code.toLowerCase();
7476
- const refusalReason = readCertificationRefusalReason(value);
7477
- let mapped = "transport";
7478
- if (normalized.includes("queue_full"))
7479
- mapped = "queue_full";
7480
- else if (normalized.includes("model_loading"))
7481
- mapped = "model_loading";
7482
- else if (normalized.includes("timeout") || normalized.includes("deadline"))
7483
- mapped = "timeout";
7484
- else if (normalized.includes("artifact_invalid"))
7485
- mapped = "artifact_invalid";
7486
- else if (normalized.includes("substitution"))
7487
- mapped = "substitution_rejected";
7488
- else if (normalized === "certification_refused")
7489
- mapped = "certification_refused";
7490
- else if (normalized.includes("not_certified"))
7491
- mapped = "not_certified";
7492
- else if (normalized.includes("probe_required"))
7493
- mapped = "probe_required";
7494
- else if (normalized.includes("idempotency_conflict"))
7495
- mapped = "idempotency_conflict";
7496
- else if (normalized.includes("needs_reauth"))
7497
- mapped = "needs_reauth";
7498
- else if (normalized.includes("schema"))
7499
- mapped = "schema_violation";
7500
- else if (normalized.includes("module_restarted") || normalized.includes("module restarted"))
7501
- mapped = "module_restarted";
7502
- const message = value instanceof Error ? value.message : String(value);
7503
- return new SynapseEmbeddingError(mapped, message, {
7504
- retryAfterMs: readRetryAfter(value) ?? (isPermanentSynapseCode(mapped) ? undefined : 100),
7505
- cause: value,
7506
- refusalReason
7507
- });
7508
- }
7509
- function embeddingFailureFor(error) {
7510
- const typedReason = error.refusalReason;
7511
- if (typedReason !== undefined && SYNAPSE_CERTIFICATION_REFUSAL_REASONS.has(typedReason) || error.code === "not_certified" || error.code === "probe_required") {
7512
- return {
7513
- class: "certification_refusal",
7514
- reason: `SYNAPSE certification refused embedding: ${typedReason ?? error.code}`,
7515
- retryable: false
7516
- };
7517
- }
7518
- if (error.code === "certification_refused") {
7519
- return {
7520
- class: "certification_refusal",
7521
- reason: `SYNAPSE certification refused embedding: ${typedReason ?? "unknown reason"}`,
7522
- retryable: false
7523
- };
7524
- }
7525
- if (typedReason === "needs_reauth" || typedReason === "needs_reauth_expired") {
7526
- return {
7527
- class: "credential_required",
7528
- reason: "SYNAPSE requires reauthentication",
7529
- retryable: false
7530
- };
7531
- }
7532
- if (error.code === "substitution_rejected") {
7533
- return { class: "substitution_rejected", reason: error.message, retryable: false };
7534
- }
7535
- if (error.code === "schema_violation" || error.code === "artifact_invalid") {
7536
- return { class: "invalid_envelope", reason: error.message, retryable: false };
7537
- }
7538
- return { class: "transport_error", reason: error.message, retryable: !error.permanent };
7539
- }
7540
- function wait(ms) {
7541
- return new Promise((resolve) => setTimeout(resolve, ms));
7542
- }
7543
- function sha256(value) {
7544
- return createHash("sha256").update(value).digest("hex");
7545
- }
7546
- function stableJson(value) {
7547
- if (Array.isArray(value))
7548
- return `[${value.map(stableJson).join(",")}]`;
7549
- if (value && typeof value === "object") {
7550
- return `{${Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, entry]) => `${JSON.stringify(key)}:${stableJson(entry)}`).join(",")}}`;
7551
- }
7552
- return JSON.stringify(value);
7553
- }
7554
- function getSynapseLaneIdentity(model, fingerprint) {
7555
- return `synapse:v1:${sha256(stableJson({ model, fingerprint }))}`;
7556
- }
7557
- function getSynapseBatchRequestKey(args) {
7558
- return sha256(stableJson({
7559
- op: "embed.batch",
7560
- model: args.model,
7561
- required_fingerprint: args.fingerprint,
7562
- required_epoch: args.tableEpoch,
7563
- allow_equivalent: false,
7564
- accept_declared: false,
7565
- ids: args.items.map((item) => item.id),
7566
- content_sha256: args.items.map((item) => item.contentSha256),
7567
- ...args.purpose === "query" ? { purpose: args.purpose } : {}
7568
- }));
7569
- }
7570
- function hashContent(text) {
7571
- return sha256(text);
7572
- }
7573
- function extractCatalogEntries(value) {
7574
- const body = responseBody(value);
7575
- const raw = Array.isArray(body.models) ? body.models : Array.isArray(body.entries) ? body.entries : Array.isArray(value) ? value : [];
7576
- const envelopeEpoch = typeof body.table_epoch === "number" && Number.isInteger(body.table_epoch) ? body.table_epoch : undefined;
7577
- const maxTokenSources = new Set([
7578
- "runtime_bucket",
7579
- "worker_bucket",
7580
- "catalog",
7581
- "catalog_unloaded"
7582
- ]);
7583
- return raw.flatMap((entry) => {
7584
- const record = asRecord(entry);
7585
- if (!record)
7586
- return [];
7587
- const model = typeof record.model === "string" && record.model.length > 0 ? record.model : typeof record.model_id === "string" ? record.model_id : "";
7588
- const fingerprint = typeof record.fingerprint === "string" && record.fingerprint.length > 0 ? record.fingerprint : Array.isArray(record.fingerprints) && typeof record.fingerprints[0] === "string" ? record.fingerprints[0] : "";
7589
- const entryEpoch = record.table_epoch ?? record.tableEpoch;
7590
- const tableEpoch = typeof entryEpoch === "number" && Number.isInteger(entryEpoch) ? entryEpoch : envelopeEpoch;
7591
- const maxTokens = record.max_tokens;
7592
- const maxTokensSource = record.max_tokens_source;
7593
- if (model.length === 0 || fingerprint.length === 0 || typeof tableEpoch !== "number" || !Number.isInteger(tableEpoch) || typeof maxTokens !== "number" || !Number.isInteger(maxTokens) || maxTokens <= 0 || typeof maxTokensSource !== "string" || !maxTokenSources.has(maxTokensSource)) {
7594
- return [];
7595
- }
7596
- const dims = record.dims ?? record.dimensions;
7597
- const rawBatch = record.recommended_batch ?? record.recommendedBatch;
7598
- const batchRecord = asRecord(rawBatch);
7599
- const recommendedBatch = typeof rawBatch === "number" ? rawBatch : batchRecord ? batchRecord.rows : undefined;
7600
- const recommendedTokenBudget = batchRecord ? batchRecord.token_budget : undefined;
7601
- const bucketLadder = Array.isArray(record.bucket_ladder) ? record.bucket_ladder.filter((bucket) => typeof bucket === "number" && Number.isInteger(bucket) && bucket > 0) : undefined;
7602
- const state = typeof record.state === "string" ? record.state : undefined;
7603
- const warmLoadCostHint = record.warm_load_cost_hint_ms;
7604
- return [
7605
- {
7606
- model,
7607
- fingerprint,
7608
- table_epoch: tableEpoch,
7609
- max_tokens: maxTokens,
7610
- max_tokens_source: maxTokensSource,
7611
- ...bucketLadder && bucketLadder.length > 0 ? { bucket_ladder: [...new Set(bucketLadder)].sort((a, b) => a - b) } : {},
7612
- ...typeof dims === "number" && Number.isInteger(dims) && dims > 0 ? { dims } : {},
7613
- ...typeof record.dtype === "string" && record.dtype.length > 0 ? { dtype: record.dtype } : {},
7614
- ...typeof record.device_class === "string" && record.device_class.length > 0 ? { device_class: record.device_class } : {},
7615
- ...typeof recommendedBatch === "number" && recommendedBatch > 0 ? { recommended_batch: Math.floor(recommendedBatch) } : {},
7616
- ...typeof recommendedTokenBudget === "number" && recommendedTokenBudget > 0 ? { recommended_token_budget: Math.floor(recommendedTokenBudget) } : {},
7617
- ...record.provenance !== undefined ? { provenance: record.provenance } : {},
7618
- ...typeof record.certified === "boolean" ? { certified: record.certified } : {},
7619
- ...typeof warmLoadCostHint === "number" && Number.isFinite(warmLoadCostHint) && warmLoadCostHint >= 0 ? { warm_load_cost_hint_ms: warmLoadCostHint } : {},
7620
- ...typeof record.status === "string" ? { status: record.status } : state ? { status: state } : {}
7621
- }
7622
- ];
7623
- });
7624
- }
7625
- function extractVector(value) {
7626
- const body = responseBody(value);
7627
- const item = Array.isArray(body.vectors) ? asRecord(body.vectors[0]) : null;
7628
- const raw = item?.vector;
7629
- if (!item || !Array.isArray(raw) || raw.some((component) => typeof component !== "number" || !Number.isFinite(component))) {
7630
- return null;
7631
- }
7632
- const disclosure = Array.isArray(body.truncation_disclosures) ? asRecord(body.truncation_disclosures[0]) : null;
7633
- return { vector: Float32Array.from(raw), item, disclosure, metadata: body };
7634
- }
7635
- function extractBatchItems(value) {
7636
- const body = responseBody(value);
7637
- const raw = Array.isArray(body.vectors) ? body.vectors : Array.isArray(body.items) ? body.items : Array.isArray(body.results) ? body.results : [];
7638
- return raw.flatMap((item) => {
7639
- const record = asRecord(item);
7640
- return record ? [record] : [];
7641
- });
7642
- }
7643
- function extractTruncationDisclosures(value) {
7644
- const body = responseBody(value);
7645
- if (!Array.isArray(body.truncation_disclosures))
7646
- return [];
7647
- return body.truncation_disclosures.map((disclosure) => asRecord(disclosure));
7648
- }
7649
- function validateWireRow(item, disclosure, expected) {
7650
- const submittedSha256 = item.submitted_sha256;
7651
- const contentSha256 = item.content_sha256;
7652
- const expectedSubmittedSha256 = hashContent(expected.text);
7653
- if (typeof submittedSha256 !== "string") {
7654
- throw new SynapseEmbeddingError("schema_violation", `Synapse item ${expected.id} omitted submitted_sha256`);
7655
- }
7656
- if (submittedSha256 !== expectedSubmittedSha256) {
7657
- throw new SynapseEmbeddingError("schema_violation", `Synapse submitted hash mismatch for item ${expected.id}`);
7658
- }
7659
- if (typeof contentSha256 !== "string") {
7660
- throw new SynapseEmbeddingError("schema_violation", `Synapse item ${expected.id} omitted content_sha256`);
7661
- }
7662
- const hashesDiffer = contentSha256 !== submittedSha256;
7663
- const disclosedTruncation = disclosure?.truncated === true;
7664
- const effectiveTokens = disclosure?.effective_tokens;
7665
- if (disclosure && (typeof disclosure.truncated !== "boolean" || typeof effectiveTokens !== "number" || !Number.isInteger(effectiveTokens) || effectiveTokens < 0)) {
7666
- throw new SynapseEmbeddingError("schema_violation", `Synapse item ${expected.id} returned an invalid truncation disclosure`);
7667
- }
7668
- if (hashesDiffer) {
7669
- if (!disclosedTruncation || typeof effectiveTokens !== "number" || !Number.isInteger(effectiveTokens) || effectiveTokens < 0) {
7670
- throw new SynapseEmbeddingError("schema_violation", `Synapse item ${expected.id} changed content without a valid truncation disclosure`);
7671
- }
7672
- } else if (disclosedTruncation) {
7673
- throw new SynapseEmbeddingError("schema_violation", `Synapse item ${expected.id} disclosed truncation but returned matching hashes`);
7674
- }
7675
- return {
7676
- truncated: hashesDiffer,
7677
- submittedSha256,
7678
- contentSha256,
7679
- ...typeof effectiveTokens === "number" && Number.isInteger(effectiveTokens) && effectiveTokens >= 0 ? { effectiveTokens } : {}
7680
- };
7681
- }
7682
- var sharedClient = null;
7683
- var sharedClientFile = null;
7684
- var sharedClientPromise = null;
7685
- var factoryClients = new WeakMap;
7686
- async function getSharedClient(options) {
7687
- if (options.clientFactory) {
7688
- let promise = factoryClients.get(options.clientFactory);
7689
- if (!promise) {
7690
- promise = options.clientFactory();
7691
- factoryClients.set(options.clientFactory, promise);
7692
- }
7693
- return promise;
7694
- }
7695
- if (sharedClient && sharedClientFile === options.connectionFile)
7696
- return sharedClient;
7697
- if (sharedClientPromise && sharedClientFile === options.connectionFile)
7698
- return sharedClientPromise;
7699
- sharedClientFile = options.connectionFile;
7700
- sharedClientPromise = SubcClient.connect({ connectionFile: options.connectionFile }).then((client) => {
7701
- sharedClient = client;
7702
- return client;
7703
- });
7704
- return sharedClientPromise;
7705
- }
7706
-
7707
- class SynapseEmbeddingProvider {
7708
- modelId;
7709
- metadata;
7710
- get maxInputTokens() {
7711
- return this.metadata?.max_tokens;
7712
- }
7713
- options;
7714
- client = null;
7715
- initialized = false;
7716
- initializing = null;
7717
- permanentFailure = false;
7718
- lastFailureReason = null;
7719
- batchLimit = 16;
7720
- tokenBudget = null;
7721
- constructor(options) {
7722
- this.options = options;
7723
- const model = options.model || SYNAPSE_DEFAULT_MODEL;
7724
- const fingerprint = options.fingerprint ?? "";
7725
- const descriptor = options.descriptor;
7726
- this.metadata = options.metadata ? {
7727
- ...options.metadata,
7728
- bucket_ladder: options.metadata.bucket_ladder ? [...options.metadata.bucket_ladder] : undefined
7729
- } : fingerprint && Number.isInteger(options.tableEpoch) && descriptor ? {
7730
- model,
7731
- fingerprint,
7732
- table_epoch: options.tableEpoch,
7733
- max_tokens: descriptor.max_tokens,
7734
- max_tokens_source: descriptor.max_tokens_source,
7735
- ...descriptor.bucket_ladder ? { bucket_ladder: [...descriptor.bucket_ladder] } : {},
7736
- ...options.dims ?? descriptor.dims ? { dims: options.dims ?? descriptor.dims } : {},
7737
- ...descriptor.dtype ? { dtype: descriptor.dtype } : {},
7738
- ...descriptor.device_class ? { device_class: descriptor.device_class } : {},
7739
- ...options.recommendedBatch ?? descriptor.recommended_batch?.rows ? {
7740
- recommended_batch: Math.max(1, Math.floor(options.recommendedBatch ?? descriptor.recommended_batch?.rows))
7741
- } : {},
7742
- ...options.recommendedTokenBudget ?? descriptor.recommended_batch?.token_budget ? {
7743
- recommended_token_budget: Math.max(1, Math.floor(options.recommendedTokenBudget ?? descriptor.recommended_batch?.token_budget))
7744
- } : {},
7745
- ...options.provenance !== undefined ? { provenance: options.provenance } : {},
7746
- ...typeof descriptor.certified === "boolean" ? { certified: descriptor.certified } : {},
7747
- ...descriptor.warm_load_cost_hint_ms !== undefined ? { warm_load_cost_hint_ms: descriptor.warm_load_cost_hint_ms } : {},
7748
- laneIdentity: getSynapseLaneIdentity(model, fingerprint)
7749
- } : null;
7750
- this.modelId = this.metadata?.laneIdentity ?? "synapse:v1:pending";
7751
- this.batchLimit = this.metadata?.recommended_batch ?? 16;
7752
- this.tokenBudget = this.metadata?.recommended_token_budget ?? null;
7753
- }
7754
- nextPage(items, start) {
7755
- const hardEnd = Math.min(items.length, start + this.batchLimit);
7756
- if (this.tokenBudget === null)
7757
- return items.slice(start, hardEnd);
7758
- let end = start;
7759
- let tokens = 0;
7760
- while (end < hardEnd) {
7761
- tokens += estimateTokens(items[end].text);
7762
- if (tokens > this.tokenBudget && end > start)
7763
- break;
7764
- end += 1;
7765
- }
7766
- return items.slice(start, Math.max(end, start + 1));
7767
- }
7768
- static async discover(options) {
7769
- const provider = new SynapseEmbeddingProvider(options);
7770
- if (!await provider.initialize() || !provider.metadata) {
7771
- throw new SynapseEmbeddingError("not_certified", "Synapse lane is not ready");
7772
- }
7773
- return provider.metadata;
7774
- }
7775
- async initialize() {
7776
- if (this.initialized)
7777
- return true;
7778
- if (this.permanentFailure)
7779
- return false;
7780
- if (this.initializing)
7781
- return this.initializing;
7782
- this.initializing = (async () => {
7783
- try {
7784
- if (!this.options.clientFactory && !await connectionFileExists(this.options.connectionFile)) {
7785
- throw new SynapseEmbeddingError("transport", `Synapse connection file is unavailable: ${this.options.connectionFile}`);
7786
- }
7787
- this.client = await getSharedClient(this.options);
7788
- if (!this.metadata) {
7789
- const discovered = await this.callWithRetry("models.list", {}, this.options.queryTimeoutMs ?? SYNAPSE_DEFAULT_QUERY_TIMEOUT_MS, false);
7790
- const entries = extractCatalogEntries(discovered);
7791
- const requested = this.options.model?.trim() || SYNAPSE_DEFAULT_MODEL;
7792
- const entry = entries.find((candidate) => candidate.model === requested);
7793
- if (!entry) {
7794
- throw new SynapseEmbeddingError("artifact_invalid", `Synapse models.list did not return requested model ${requested}`);
7795
- }
7796
- if (entry.certified === false || entry.status === "not_certified") {
7797
- throw new SynapseEmbeddingError("not_certified", `Synapse model ${entry.model} is not certified`);
7798
- }
7799
- const metadata = {
7800
- ...entry,
7801
- laneIdentity: getSynapseLaneIdentity(entry.model, entry.fingerprint)
7802
- };
7803
- this.metadata = metadata;
7804
- this.modelId = metadata.laneIdentity;
7805
- this.batchLimit = metadata.recommended_batch ?? this.batchLimit;
7806
- this.tokenBudget = metadata.recommended_token_budget ?? this.tokenBudget;
7807
- }
7808
- this.initialized = true;
7809
- this.recordSuccess();
7810
- return true;
7811
- } catch (error) {
7812
- const classified = classifyError(error);
7813
- this.recordFailure(classified);
7814
- if (classified.permanent) {
7815
- this.permanentFailure = true;
7816
- log(`[magic-context] Synapse lane disabled: ${classified.code}: ${classified.message}`);
7817
- } else {
7818
- log(`[magic-context] Synapse lane unavailable: ${classified.message}`);
7819
- }
7820
- this.initialized = false;
7821
- return false;
7822
- } finally {
7823
- this.initializing = null;
7824
- }
7825
- })();
7826
- return this.initializing;
7827
- }
7828
- async embed(text, signal, purpose = "passage") {
7829
- if (!await this.initialize() || signal?.aborted || !this.metadata)
7830
- return null;
7831
- if (estimateTokens(text) > this.metadata.max_tokens)
7832
- return null;
7833
- try {
7834
- const id = "query";
7835
- const value = await this.callWithRetry("embed.query", this.requestConstraints({
7836
- id,
7837
- text,
7838
- purpose,
7839
- deadline_ms: this.options.queryTimeoutMs ?? SYNAPSE_DEFAULT_QUERY_TIMEOUT_MS
7840
- }), this.options.queryTimeoutMs ?? SYNAPSE_DEFAULT_QUERY_TIMEOUT_MS, true, signal);
7841
- const extracted = extractVector(value);
7842
- if (!extracted) {
7843
- throw new SynapseEmbeddingError("schema_violation", "Synapse query returned no vector row");
7844
- }
7845
- if (extracted.item.id !== id) {
7846
- throw new SynapseEmbeddingError("schema_violation", `Synapse query returned unexpected item ${String(extracted.item.id)}`);
7847
- }
7848
- const rowMetadata = validateWireRow(extracted.item, extracted.disclosure, { id, text });
7849
- this.validateResponse(extracted.metadata, extracted.vector.length);
7850
- embeddingRowMetadata.set(extracted.vector, rowMetadata);
7851
- this.recordSuccess();
7852
- return extracted.vector;
7853
- } catch (error) {
7854
- this.logCallFailure(error, "embed.query");
7855
- return null;
7856
- }
7857
- }
7858
- async embedBatch(texts, signal, purpose = "passage") {
7859
- if (texts.length === 0)
7860
- return [];
7861
- const items = texts.map((text, index) => ({
7862
- id: `item:${index}`,
7863
- text,
7864
- contentSha256: hashContent(text)
7865
- }));
7866
- const map = await this.embedItems(items, signal, purpose);
7867
- return items.map((item) => map.get(item.id) ?? null);
7868
- }
7869
- async embedItems(items, signal, purpose = "passage") {
7870
- const output = new Map;
7871
- if (items.length === 0 || !await this.initialize() || !this.metadata || signal?.aborted) {
7872
- return output;
7873
- }
7874
- const maxTokens = this.metadata.max_tokens;
7875
- const eligibleItems = items.filter((item) => estimateTokens(item.text) <= maxTokens).map((item) => ({ ...item, contentSha256: hashContent(item.text) }));
7876
- for (let start = 0;start < eligibleItems.length; ) {
7877
- if (signal?.aborted || this.permanentFailure)
7878
- break;
7879
- const page = this.nextPage(eligibleItems, start);
7880
- start += page.length;
7881
- try {
7882
- const requestKey = this.requestKey(page, purpose);
7883
- let body = {};
7884
- let restarted = false;
7885
- for (;; ) {
7886
- try {
7887
- body = await this.callWithRetry("embed.batch", this.batchRequest(page, requestKey, purpose), this.options.batchTimeoutMs ?? SYNAPSE_DEFAULT_BATCH_TIMEOUT_MS, true, signal);
7888
- const first = responseBody(body);
7889
- const jobId = typeof first.job_id === "string" ? first.job_id : null;
7890
- if (jobId)
7891
- body = await this.pollBatch(jobId, requestKey, signal);
7892
- break;
7893
- } catch (error) {
7894
- const classified = classifyError(error);
7895
- if (classified.code !== "module_restarted" || restarted)
7896
- throw classified;
7897
- restarted = true;
7898
- }
7899
- }
7900
- const batchEnvelope = responseBody(body);
7901
- const disclosures = extractTruncationDisclosures(body);
7902
- for (const [index, item] of extractBatchItems(body).entries()) {
7903
- const id = typeof item.id === "string" ? item.id : "";
7904
- const vector = item.vector ?? item.embedding;
7905
- if (!id || !Array.isArray(vector) || vector.some((component) => typeof component !== "number" || !Number.isFinite(component))) {
7906
- throw new SynapseEmbeddingError("schema_violation", "Synapse batch item is malformed");
7907
- }
7908
- const expected = page.find((candidate) => candidate.id === id);
7909
- if (!expected) {
7910
- throw new SynapseEmbeddingError("schema_violation", `Synapse returned unknown item ${id}`);
7911
- }
7912
- const rowMetadata = validateWireRow(item, disclosures[index] ?? null, expected);
7913
- const vectorArray = Float32Array.from(vector);
7914
- this.validateResponse({ ...batchEnvelope, ...item }, vectorArray.length);
7915
- embeddingRowMetadata.set(vectorArray, rowMetadata);
7916
- output.set(id, vectorArray);
7917
- this.recordSuccess();
7918
- }
7919
- } catch (error) {
7920
- const classified = classifyError(error);
7921
- this.logCallFailure(classified, "embed.batch");
7922
- if (classified.code === "idempotency_conflict" || classified.code === "schema_violation") {
7923
- throw classified;
7924
- }
7925
- if (classified.permanent) {
7926
- this.permanentFailure = true;
7927
- this.initialized = false;
7928
- break;
7929
- }
7930
- }
7931
- }
7932
- return output;
7933
- }
7934
- async dispose() {
7935
- this.initialized = false;
7936
- this.client = null;
7937
- }
7938
- isLoaded() {
7939
- return this.initialized;
7940
- }
7941
- getLastFailureReason() {
7942
- return this.lastFailureReason;
7943
- }
7944
- requestConstraints(extra) {
7945
- const metadata = this.metadata;
7946
- if (!metadata)
7947
- return extra;
7948
- return {
7949
- ...extra,
7950
- model: metadata.model,
7951
- required_fingerprint: metadata.fingerprint,
7952
- required_epoch: metadata.table_epoch,
7953
- allow_equivalent: false,
7954
- accept_declared: false
7955
- };
7956
- }
7957
- batchRequest(items, requestKey, purpose) {
7958
- return this.requestConstraints({
7959
- items: items.map((item) => ({
7960
- id: item.id,
7961
- text: item.text,
7962
- content_sha256: item.contentSha256
7963
- })),
7964
- request_key: requestKey,
7965
- purpose
7966
- });
7967
- }
7968
- requestKey(items, purpose) {
7969
- if (!this.metadata)
7970
- throw new SynapseEmbeddingError("transport", "Synapse metadata is unavailable");
7971
- return getSynapseBatchRequestKey({
7972
- model: this.metadata.model,
7973
- fingerprint: this.metadata.fingerprint,
7974
- tableEpoch: this.metadata.table_epoch,
7975
- items,
7976
- purpose
7977
- });
7978
- }
7979
- async pollBatch(jobId, requestKey, signal) {
7980
- let cursor = null;
7981
- const allItems = [];
7982
- const allDisclosures = [];
7983
- for (;; ) {
7984
- if (signal?.aborted)
7985
- return {};
7986
- const body = await this.callWithRetry("embed.result", this.requestConstraints({
7987
- job_id: jobId,
7988
- cursor,
7989
- request_key: requestKey
7990
- }), this.options.batchTimeoutMs ?? SYNAPSE_DEFAULT_BATCH_TIMEOUT_MS, true, signal);
7991
- const parsed = responseBody(body);
7992
- const items = extractBatchItems(body);
7993
- const disclosures = extractTruncationDisclosures(body);
7994
- allItems.push(...items);
7995
- allDisclosures.push(...items.map((_, index) => disclosures[index] ?? null));
7996
- const nextCursor = parsed.next_cursor ?? parsed.cursor;
7997
- const done = parsed.done === true || parsed.complete === true || nextCursor === undefined || nextCursor === null;
7998
- if (done) {
7999
- return {
8000
- ...parsed,
8001
- vectors: allItems,
8002
- truncation_disclosures: allDisclosures
8003
- };
8004
- }
8005
- cursor = nextCursor;
8006
- }
8007
- }
8008
- async callWithRetry(method, params, timeoutMs, retryEmbeddings, signal) {
8009
- let attempt = 0;
8010
- for (;; ) {
8011
- if (signal?.aborted)
8012
- throw new SynapseEmbeddingError("transport", "Synapse request aborted");
8013
- try {
8014
- if (!this.client)
8015
- throw new SynapseEmbeddingError("transport", "Synapse client is unavailable");
8016
- return await this.client.call(this.options.moduleId ?? "synapse", method, params, {
8017
- timeoutMs,
8018
- targetKind: "management_surface",
8019
- identity: {
8020
- project_root: this.options.projectRoot,
8021
- harness: getHarness(),
8022
- session: this.options.session
8023
- }
8024
- });
8025
- } catch (error) {
8026
- const classified = classifyError(error);
8027
- if (classified.code === "idempotency_conflict")
8028
- throw classified;
8029
- const outcomeUnknown = error instanceof SubcCallError && error.kind === "outcome_unknown";
8030
- const retryable = !classified.permanent && (retryEmbeddings || !outcomeUnknown);
8031
- if (!retryable || attempt >= 3)
8032
- throw classified;
8033
- const delay = classified.retryAfterMs ?? Math.min(2000, 100 * 2 ** attempt);
8034
- attempt += 1;
8035
- await wait(delay);
8036
- }
8037
- }
8038
- }
8039
- validateResponse(body, dims) {
8040
- const metadata = this.metadata;
8041
- if (!metadata) {
8042
- throw new SynapseEmbeddingError("artifact_invalid", "Synapse lane metadata missing");
8043
- }
8044
- if (metadata.dims === undefined) {
8045
- const envelopeDims = body.dims;
8046
- if (typeof envelopeDims === "number" && envelopeDims !== dims) {
8047
- throw new SynapseEmbeddingError("artifact_invalid", `Synapse envelope declares ${envelopeDims} dimensions but the vector has ${dims}`);
8048
- }
8049
- metadata.dims = dims;
8050
- }
8051
- if (dims !== metadata.dims) {
8052
- throw new SynapseEmbeddingError("artifact_invalid", `Synapse returned ${dims} dimensions, expected ${metadata.dims}`);
8053
- }
8054
- const fingerprint = body.fingerprint ?? body.served_fingerprint;
8055
- if (typeof fingerprint !== "string") {
8056
- throw new SynapseEmbeddingError("artifact_invalid", "Synapse response omitted the served fingerprint");
8057
- }
8058
- if (fingerprint !== metadata.fingerprint) {
8059
- throw new SynapseEmbeddingError("substitution_rejected", `Synapse fingerprint changed from ${metadata.fingerprint} to ${fingerprint}`);
8060
- }
8061
- const epoch = body.table_epoch ?? body.tableEpoch;
8062
- if (typeof epoch !== "number") {
8063
- throw new SynapseEmbeddingError("artifact_invalid", "Synapse response omitted the served table epoch");
8064
- }
8065
- if (epoch !== metadata.table_epoch) {
8066
- throw new SynapseEmbeddingError("substitution_rejected", `Synapse table epoch changed from ${metadata.table_epoch} to ${epoch}`);
8067
- }
8068
- }
8069
- logCallFailure(error, operation) {
8070
- const classified = classifyError(error);
8071
- this.recordFailure(classified);
8072
- if (classified.permanent)
8073
- this.permanentFailure = true;
8074
- const suffix = classified.retryAfterMs === undefined ? "" : ` retry_after_ms=${classified.retryAfterMs}`;
8075
- log(`[magic-context] Synapse ${operation} failed: ${classified.code}${suffix}: ${classified.message}`);
8076
- }
8077
- recordFailure(error) {
8078
- this.lastFailureReason = embeddingFailureFor(error);
8079
- }
8080
- recordSuccess() {
8081
- this.lastFailureReason = null;
8082
- }
8083
- }
8084
-
8085
- // ../plugin/src/features/magic-context/memory/normalize-hash.ts
8086
- import { createHash as createHash2 } from "node:crypto";
8087
- function normalizeMemoryContent(content) {
8088
- return content.toLowerCase().replace(/\s+/g, " ").trim();
8089
- }
8090
- function computeNormalizedHash(content) {
8091
- const normalized = normalizeMemoryContent(content);
8092
- return createHash2("md5").update(normalized).digest("hex");
8093
- }
8094
-
8095
- // ../plugin/src/features/magic-context/memory/embedding-identity.ts
8096
- function normalizeEndpoint(endpoint) {
8097
- return endpoint?.trim().replace(/\/+$/, "") ?? "";
8098
- }
8099
- var LOCAL_EMBEDDING_RUNTIME_FINGERPRINT = "transformers@4.3.0;onnxruntime-node@1.30.0;onnxruntime-web@1.26.0-dev.20260416-b7804b056c";
8100
- function getEmbeddingProviderIdentity(config) {
8101
- if (config.provider === "off") {
8102
- return "embedding-provider:off";
8103
- }
8104
- if (config.provider === "synapse") {
8105
- const resolved = config;
8106
- if (!resolved.model || !resolved.synapse_fingerprint)
8107
- return "synapse:v1:pending";
8108
- return getSynapseLaneIdentity(resolved.model, resolved.synapse_fingerprint);
8109
- }
8110
- if (config.provider !== "local" && config.provider !== "openai-compatible") {
8111
- throw new Error("Unknown embedding provider");
8112
- }
8113
- const truncate = config.provider === "openai-compatible" ? config.truncate?.trim() : undefined;
8114
- const documentPrefix = config.provider === "openai-compatible" ? resolveEmbeddingTextPrefixes(config.model, config.query_instruction, config.document_prefix).documentPrefix : "";
8115
- const localDtype = config.provider === "local" && config.local_dtype && config.local_dtype !== "fp32" ? config.local_dtype : undefined;
8116
- const identityInput = config.provider === "openai-compatible" ? {
8117
- provider: "openai-compatible",
8118
- model: config.model.trim(),
8119
- endpoint: normalizeEndpoint(config.endpoint),
8120
- apiKeyPresent: Boolean(config.api_key?.trim()),
8121
- inputType: config.input_type?.trim() || "",
8122
- ...documentPrefix ? { documentPrefix } : {},
8123
- ...truncate ? { truncate } : {}
8124
- } : {
8125
- provider: "local",
8126
- model: config.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
8127
- endpoint: "",
8128
- apiKeyPresent: false,
8129
- runtimeFingerprint: LOCAL_EMBEDDING_RUNTIME_FINGERPRINT,
8130
- ...localDtype ? { localDtype } : {}
8131
- };
8132
- return `embedding-provider:${computeNormalizedHash(JSON.stringify(identityInput))}`;
8133
- }
8134
-
8135
5834
  // ../plugin/src/features/magic-context/memory/embedding-worker-client.ts
8136
5835
  import { Worker } from "node:worker_threads";
8137
5836
  class EmbeddingWorkerClient {
@@ -8179,7 +5878,7 @@ class EmbeddingWorkerClient {
8179
5878
  request.reject(error);
8180
5879
  }
8181
5880
  this.pending.clear();
8182
- log("[magic-context] embedding worker exited unexpectedly:", error);
5881
+ log2("[magic-context] embedding worker exited unexpectedly:", error);
8183
5882
  worker.terminate();
8184
5883
  };
8185
5884
  worker.on("error", fail);
@@ -8313,7 +6012,7 @@ async function acquireModelLoadLock(lockPath) {
8313
6012
  try {
8314
6013
  const info = await stat(lockPath);
8315
6014
  if (Date.now() - info.mtimeMs > STALE_LOCK_MS) {
8316
- log(`[magic-context] embedding-load lock stale (>${STALE_LOCK_MS}ms), taking over`);
6015
+ log2(`[magic-context] embedding-load lock stale (>${STALE_LOCK_MS}ms), taking over`);
8317
6016
  try {
8318
6017
  await unlink(lockPath);
8319
6018
  } catch {}
@@ -8376,7 +6075,7 @@ var importTransformersNodeWasmFallbackForRuntime = async () => {
8376
6075
  return await importPluginModule(() => import(nodeWasmEntry));
8377
6076
  };
8378
6077
  var modelCacheDirForRuntime = () => workerData?.magicContextEmbeddingWorker && typeof workerData.modelCacheDir === "string" ? workerData.modelCacheDir : join(getMagicContextStorageDir(), "models");
8379
- var logForRuntime = log;
6078
+ var logForRuntime = log2;
8380
6079
  var injectWasmOrtForRuntime = injectWasmOrt;
8381
6080
  var loadedLocalEmbeddingRuntimes = new Map;
8382
6081
  var nextLocalEmbeddingRuntimeId = 1;
@@ -8433,7 +6132,7 @@ async function injectWasmOrt() {
8433
6132
  wasmRuntimeInjected = true;
8434
6133
  return true;
8435
6134
  } catch (error) {
8436
- log("[magic-context] failed to inject onnxruntime-web:", error instanceof Error ? error.message : String(error));
6135
+ log2("[magic-context] failed to inject onnxruntime-web:", error instanceof Error ? error.message : String(error));
8437
6136
  return false;
8438
6137
  }
8439
6138
  }
@@ -8541,7 +6240,7 @@ async function withQuietConsole(fn) {
8541
6240
  const origError = console.error;
8542
6241
  const redirect = (...args) => {
8543
6242
  const message = args.map((a) => typeof a === "string" ? a : String(a)).join(" ");
8544
- log(`[transformers] ${message}`);
6243
+ log2(`[transformers] ${message}`);
8545
6244
  };
8546
6245
  console.warn = redirect;
8547
6246
  console.error = redirect;
@@ -8593,13 +6292,13 @@ function extractBatchEmbeddings(result, expectedCount) {
8593
6292
  return data.map((entry) => toFloat32Array(entry));
8594
6293
  }
8595
6294
  if (!isArrayLikeNumber(data)) {
8596
- log("[magic-context] embedding batch returned unexpected data shape");
6295
+ log2("[magic-context] embedding batch returned unexpected data shape");
8597
6296
  return Array.from({ length: expectedCount }, () => null);
8598
6297
  }
8599
6298
  const flatData = toFloat32Array(data);
8600
6299
  const dimension = result.dims?.at(-1) ?? flatData.length / expectedCount;
8601
6300
  if (!Number.isInteger(dimension) || dimension <= 0 || flatData.length !== expectedCount * dimension) {
8602
- log("[magic-context] embedding batch returned invalid dimensions");
6301
+ log2("[magic-context] embedding batch returned invalid dimensions");
8603
6302
  return Array.from({ length: expectedCount }, () => null);
8604
6303
  }
8605
6304
  const embeddings = [];
@@ -8635,7 +6334,7 @@ class LocalEmbeddingProvider {
8635
6334
  magicContextEmbeddingWorker: true,
8636
6335
  modelCacheDir: modelCacheDirForRuntime(),
8637
6336
  enforcePrivateStoragePermissions: shouldEnforcePrivateStoragePermissions(),
8638
- harness: getHarness(),
6337
+ harness: getHarness2(),
8639
6338
  model,
8640
6339
  maxInputTokens,
8641
6340
  dtype: this.dtype,
@@ -8698,7 +6397,7 @@ class LocalEmbeddingProvider {
8698
6397
  ensureStorageDirectorySync(modelCacheDir);
8699
6398
  env.cacheDir = modelCacheDir;
8700
6399
  } catch {
8701
- log("[magic-context] could not create model cache dir, using library default");
6400
+ log2("[magic-context] could not create model cache dir, using library default");
8702
6401
  }
8703
6402
  const createPipeline = transformersModule.pipeline;
8704
6403
  const lockPath = join(modelCacheDir, ".load.lock");
@@ -8727,7 +6426,7 @@ class LocalEmbeddingProvider {
8727
6426
  break;
8728
6427
  }
8729
6428
  const delayMs = 300 * attempt + Math.floor(Math.random() * 200);
8730
- log(`[magic-context] embedding model load attempt ${attempt}/${MAX_ATTEMPTS} failed transiently, retrying in ${delayMs}ms`);
6429
+ log2(`[magic-context] embedding model load attempt ${attempt}/${MAX_ATTEMPTS} failed transiently, retrying in ${delayMs}ms`);
8731
6430
  await new Promise((resolve) => setTimeout(resolve, delayMs));
8732
6431
  }
8733
6432
  }
@@ -8742,7 +6441,7 @@ class LocalEmbeddingProvider {
8742
6441
  externalDeltaAtLoad: memoryAfterLoad.external - memoryBeforeLoad.external,
8743
6442
  arrayBuffersDeltaAtLoad: memoryAfterLoad.arrayBuffers - memoryBeforeLoad.arrayBuffers
8744
6443
  });
8745
- log(`[magic-context] embedding model loaded: ${this.model}`);
6444
+ log2(`[magic-context] embedding model loaded: ${this.model}`);
8746
6445
  } else if (this.disposing) {
8747
6446
  return;
8748
6447
  } else {
@@ -8927,7 +6626,7 @@ class LocalEmbeddingProvider {
8927
6626
  try {
8928
6627
  await pipelineToDispose.dispose?.();
8929
6628
  } catch (error) {
8930
- log("[magic-context] embedding model dispose failed:", error);
6629
+ log2("[magic-context] embedding model dispose failed:", error);
8931
6630
  }
8932
6631
  })();
8933
6632
  return this.disposePromise;
@@ -8940,4 +6639,4 @@ class LocalEmbeddingProvider {
8940
6639
  }
8941
6640
  }
8942
6641
 
8943
- export { isValidLanguageCode, withContentLanguageDirective, buildPrimaryLanguageDirective, parseCron, nextOccurrence, nextDueAtMs, resolveModelConfigValue, resolvePromptSurface, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, DEFAULT_HISTORIAN_TIMEOUT_MS, PROTECTED_TOKENS_MIN, DEFAULT_LOCAL_EMBEDDING_MODEL, PER_HARNESS_MIGRATION_INVENTORY, PER_HARNESS_MODEL_KEYS, ConfigProfilesSchema, DREAM_TASK_PROMOTION_DEFAULTS, MagicContextConfigSchema, LIVE_RELOAD_CONFIG_PATHS, deriveDefaultProtectedTokens, SubcClient, connectionFileExists, SYNAPSE_DEFAULT_MODEL, isSynapseEmbeddingTruncated, toSynapseLaneDescriptor, formatSynapseLaneDescriptor, getSynapseBatchRequestKey, SynapseEmbeddingProvider, computeNormalizedHash, embeddingModelsMatch, resolveEmbeddingTextPrefixes, getEmbeddingProviderIdentity, dominantEmbeddingFailure, resolveLocalEmbeddingWorkerRuntime, getLocalEmbeddingNativeMemoryStats, LocalEmbeddingProvider };
6642
+ export { isValidLanguageCode, withContentLanguageDirective, buildPrimaryLanguageDirective, parseCron, nextOccurrence, nextDueAtMs, resolveModelConfigValue, resolvePromptSurface, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, DEFAULT_HISTORIAN_TIMEOUT_MS, PROTECTED_TOKENS_MIN, DEFAULT_LOCAL_EMBEDDING_MODEL, PER_HARNESS_MIGRATION_INVENTORY, PER_HARNESS_MODEL_KEYS, ConfigProfilesSchema, DREAM_TASK_PROMOTION_DEFAULTS, MagicContextConfigSchema, LIVE_RELOAD_CONFIG_PATHS, deriveDefaultProtectedTokens, embeddingModelsMatch, resolveEmbeddingTextPrefixes, getEmbeddingProviderIdentity, dominantEmbeddingFailure, resolveLocalEmbeddingWorkerRuntime, getLocalEmbeddingNativeMemoryStats, LocalEmbeddingProvider };