pendra 0.12.0 → 0.12.2

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.
package/dist/index.mjs CHANGED
@@ -622,6 +622,10 @@ var Rerank = class {
622
622
  }
623
623
  };
624
624
 
625
+ // src/version.ts
626
+ var VERSION = "0.12.2";
627
+ var USER_AGENT = `pendra-node/${VERSION}`;
628
+
625
629
  // src/_hpke.ts
626
630
  import { x25519 } from "@noble/curves/ed25519.js";
627
631
  import { chacha20poly1305 } from "@noble/ciphers/chacha.js";
@@ -1015,6 +1019,27 @@ var PrivateInferenceEngine = class {
1015
1019
  const digest = sha256Hex(new TextEncoder().encode(this.apiKey)).slice(0, 16);
1016
1020
  return `${this.baseUrl}|${digest}`;
1017
1021
  }
1022
+ /**
1023
+ * Drop the cached key set so the next call re-fetches it.
1024
+ *
1025
+ * The cache is otherwise held for the whole process lifetime, which is
1026
+ * correct while a fleet keeps one key but fatal when it re-keys: a
1027
+ * long-running server would go on sealing to a fingerprint no worker holds
1028
+ * and fail EVERY request until someone restarted it. The API signals a re-key
1029
+ * with `X-Pendra-Error-Code: no_private_inference_worker`, and this is what
1030
+ * lets the client recover on its own.
1031
+ *
1032
+ * Pinned clients are deliberately NOT re-fetched on that signal — a pin is an
1033
+ * allowlist, so a fetch could only return fingerprints the pin already
1034
+ * rejects. They surface the error instead, which is what pinning means.
1035
+ */
1036
+ invalidateOrgKey() {
1037
+ this.orgPubs = null;
1038
+ }
1039
+ /** Whether this client seals only to an explicit fingerprint allowlist. */
1040
+ get isPinned() {
1041
+ return this.pinnedFingerprints !== null;
1042
+ }
1018
1043
  /**
1019
1044
  * Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
1020
1045
  * allowlist (or auto-trust TOFU), cache the accepted public keys, and return
@@ -1027,7 +1052,7 @@ var PrivateInferenceEngine = class {
1027
1052
  if (this.orgPubs !== null) return this.orgPubs;
1028
1053
  if (!fetchedKeys || fetchedKeys.length === 0) {
1029
1054
  throw new PrivateInferenceError(
1030
- "No private-inference workload key is registered for this organisation. Run `pendra keygen --save` on your worker(s), then reconnect them."
1055
+ "No encryption key is registered for this organisation. Start a worker and it will be issued one automatically."
1031
1056
  );
1032
1057
  }
1033
1058
  const pubs = [];
@@ -1053,7 +1078,7 @@ var PrivateInferenceEngine = class {
1053
1078
  if (accepted.length === 0) {
1054
1079
  const pinned = Array.from(this.pinnedFingerprints).sort().join(", ");
1055
1080
  throw new PrivateInferenceError(
1056
- `None of the org's workload key fingerprints (${fps.join(", ")}) match your pinned fingerprint(s) (${pinned}). Refusing to seal (possible man-in-the-middle). Verify the fingerprint(s) from \`pendra keygen\` and update the pin if you rotated keys.`
1081
+ `None of the org's workload key fingerprints (${fps.join(", ")}) match your pinned fingerprint(s) (${pinned}). Refusing to seal (possible man-in-the-middle). If your fleet re-keyed \u2014 which happens when every worker goes offline at once \u2014 update the pin to a current fingerprint from the console.`
1057
1082
  );
1058
1083
  }
1059
1084
  } else {
@@ -1227,7 +1252,7 @@ var Pendra = class {
1227
1252
  try {
1228
1253
  response = await fetch(url, {
1229
1254
  method: "GET",
1230
- headers: { Authorization: `Bearer ${this.apiKey}` },
1255
+ headers: { Authorization: `Bearer ${this.apiKey}`, "User-Agent": USER_AGENT },
1231
1256
  signal: controller.signal
1232
1257
  });
1233
1258
  } catch (err) {
@@ -1242,7 +1267,7 @@ var Pendra = class {
1242
1267
  }
1243
1268
  if (response.status === 404) {
1244
1269
  throw new PrivateInferenceError(
1245
- "No private-inference workload key is registered for this organisation. Run `pendra keygen --save` on your worker(s), then reconnect them."
1270
+ "No encryption key is registered for this organisation. Start a worker and it will be issued one automatically."
1246
1271
  );
1247
1272
  }
1248
1273
  if (!response.ok) {
@@ -1267,6 +1292,7 @@ var Pendra = class {
1267
1292
  const headers = {
1268
1293
  Authorization: `Bearer ${this.apiKey}`,
1269
1294
  "Content-Type": "application/json",
1295
+ "User-Agent": USER_AGENT,
1270
1296
  ...options.headers ?? {}
1271
1297
  };
1272
1298
  let response;
@@ -1293,6 +1319,7 @@ var Pendra = class {
1293
1319
  }
1294
1320
  if (!response.ok) {
1295
1321
  clearTimeout(timeoutId);
1322
+ this.invalidateKeyCacheIfRekeyed(response);
1296
1323
  let message;
1297
1324
  try {
1298
1325
  const body = await response.json();
@@ -1306,6 +1333,26 @@ var Pendra = class {
1306
1333
  }
1307
1334
  return { response, controller };
1308
1335
  }
1336
+ /**
1337
+ * Drop the cached encryption keys when the API reports that no worker holds
1338
+ * the one this request was sealed to — the signal that the org's fleet has
1339
+ * re-keyed. Returns true if the cache was dropped.
1340
+ *
1341
+ * A PINNED client is left alone on purpose: a pin is an allowlist, so
1342
+ * re-fetching could only return fingerprints the pin already rejects and
1343
+ * would loop rather than recover. The error surfaces instead, which is
1344
+ * exactly what pinning is for.
1345
+ */
1346
+ invalidateKeyCacheIfRekeyed(response) {
1347
+ const engine = this._piEngine;
1348
+ if (!engine || engine.isPinned) return false;
1349
+ if (response.headers?.get("X-Pendra-Error-Code") !== "no_private_inference_worker") {
1350
+ return false;
1351
+ }
1352
+ engine.invalidateOrgKey();
1353
+ this._piKeyFetch = null;
1354
+ return true;
1355
+ }
1309
1356
  };
1310
1357
 
1311
1358
  // src/index.ts