@zerogpu/browser-extension-sdk 0.2.0-dev.202607211328.gc9a2a62 → 0.2.0-dev.202607221142.g1c6b982

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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "sdkVersion": "0.2.0-dev.202607211328.gc9a2a62+c9a2a62",
2
+ "sdkVersion": "0.2.0-dev.202607221142.g1c6b982+1c6b982",
3
3
  "channel": "development",
4
4
  "transformersVersion": "2.17.2",
5
5
  "wllamaVersion": "2.3.5",
@@ -14,5 +14,5 @@
14
14
  "wllama/single-thread/wllama.wasm",
15
15
  "wllama/multi-thread/wllama.wasm"
16
16
  ],
17
- "builtAt": "2026-07-21T13:30:10.105Z"
17
+ "builtAt": "2026-07-22T11:44:17.730Z"
18
18
  }
@@ -1,5 +1,5 @@
1
- const SDK_VERSION = typeof "0.2.0-dev.202607211328.gc9a2a62+c9a2a62" === 'string' && "0.2.0-dev.202607211328.gc9a2a62+c9a2a62".length > 0
2
- ? "0.2.0-dev.202607211328.gc9a2a62+c9a2a62"
1
+ const SDK_VERSION = typeof "0.2.0-dev.202607221142.g1c6b982+1c6b982" === 'string' && "0.2.0-dev.202607221142.g1c6b982+1c6b982".length > 0
2
+ ? "0.2.0-dev.202607221142.g1c6b982+1c6b982"
3
3
  : '0.0.0-dev';
4
4
 
5
5
  const ORCHESTRATOR_BASE_URL = typeof "https://devices.zerogpu.ai" === 'string' && "https://devices.zerogpu.ai".length > 0
@@ -1587,6 +1587,65 @@ const onAllocationFailure = (message) => {
1587
1587
  getLogger().error('[Watchdog] Allocation failure — emergency shutdown', { message });
1588
1588
  };
1589
1589
 
1590
+ /**
1591
+ * Fires ad-measurement pixels from the device.
1592
+ *
1593
+ * The point of running these here rather than server-side is that the ad server
1594
+ * sees the end user's real IP, User-Agent and cookies; fired from a datacenter
1595
+ * they are filtered or flagged as fraud.
1596
+ */
1597
+ const DEFAULT_TIMEOUT_MS = 3000;
1598
+ // Holds in-flight images so the GC cannot collect one mid-request, which would
1599
+ // cancel the beacon before it reaches the ad server.
1600
+ const inFlight = new Set();
1601
+ /**
1602
+ * Loads `url` as an image and resolves once the request has left the device.
1603
+ *
1604
+ * Both `load` and `error` resolve to 'fired', by design. Measurement endpoints
1605
+ * answer with 204s, redirect chains and non-image bodies, so `error` is the
1606
+ * normal outcome for a perfectly successful pixel — treating it as a failure
1607
+ * would report a ~100% failure rate. Only the timeout is genuinely unknown.
1608
+ *
1609
+ * Never rejects: a pixel is best-effort and must not affect inference.
1610
+ */
1611
+ const fireTrackingPixel = (url, timeoutMs = DEFAULT_TIMEOUT_MS) => {
1612
+ // Non-DOM hosts (e.g. a worker) cannot fire a beacon.
1613
+ if (typeof Image === 'undefined')
1614
+ return Promise.resolve('skipped');
1615
+ return new Promise((resolve) => {
1616
+ let settled = false;
1617
+ let img;
1618
+ let timer;
1619
+ const settle = (status) => {
1620
+ if (settled)
1621
+ return;
1622
+ settled = true;
1623
+ if (timer !== undefined)
1624
+ clearTimeout(timer);
1625
+ if (img) {
1626
+ img.onload = null;
1627
+ img.onerror = null;
1628
+ inFlight.delete(img);
1629
+ }
1630
+ resolve(status);
1631
+ };
1632
+ // Everything here is best-effort: a host that blocks image construction or
1633
+ // src assignment must degrade to 'skipped', never throw into the caller and
1634
+ // never take inference down with it.
1635
+ try {
1636
+ timer = setTimeout(() => settle('timeout'), timeoutMs);
1637
+ img = new Image();
1638
+ img.onload = () => settle('fired');
1639
+ img.onerror = () => settle('fired');
1640
+ inFlight.add(img);
1641
+ img.src = url;
1642
+ }
1643
+ catch {
1644
+ settle('skipped');
1645
+ }
1646
+ });
1647
+ };
1648
+
1590
1649
  let executing = false;
1591
1650
  const buildInferenceInput = (request) => {
1592
1651
  const input = request.input ?? {};
@@ -1657,6 +1716,29 @@ const executeSingleRequest = async (request) => {
1657
1716
  }
1658
1717
  };
1659
1718
  }
1719
+ // Kicked off only once the adapter is confirmed: a model_not_ready return
1720
+ // above sends the request to cloud instead, and firing then would count an
1721
+ // impression this device never served. Deliberately not awaited so a slow ad
1722
+ // server cannot add latency to inference; whatever has settled by the time
1723
+ // inference finishes is what gets reported.
1724
+ let pixelStatus = 'pending';
1725
+ if (request.trackingPixel) {
1726
+ void fireTrackingPixel(request.trackingPixel).then((status) => {
1727
+ pixelStatus = status;
1728
+ });
1729
+ }
1730
+ const pixelMetadata = async () => {
1731
+ if (!request.trackingPixel)
1732
+ return {};
1733
+ // Yield one turn before snapshotting. The beacon's load/error is a
1734
+ // macrotask, and it cannot run while synchronous inference holds the
1735
+ // thread — so by the time inference resolves, a request that finished long
1736
+ // ago is still sitting unprocessed in the queue and would be reported as
1737
+ // 'pending'. This waits on nothing (a genuinely slow pixel still reports
1738
+ // 'pending'), it just stops discarding a result that already arrived.
1739
+ await new Promise((resolve) => setTimeout(resolve, 0));
1740
+ return { trackingPixel: pixelStatus };
1741
+ };
1660
1742
  try {
1661
1743
  const input = buildInferenceInput(request);
1662
1744
  const result = await adapter.runInference(input);
@@ -1668,7 +1750,8 @@ const executeSingleRequest = async (request) => {
1668
1750
  error: false,
1669
1751
  errorMessage: '',
1670
1752
  metadata: {
1671
- processingTime: Math.round(performance.now() - start)
1753
+ processingTime: Math.round(performance.now() - start),
1754
+ ...(await pixelMetadata())
1672
1755
  }
1673
1756
  };
1674
1757
  }
@@ -1695,7 +1778,11 @@ const executeSingleRequest = async (request) => {
1695
1778
  error: true,
1696
1779
  errorMessage: error.message,
1697
1780
  metadata: {
1698
- processingTime: Math.round(performance.now() - start)
1781
+ processingTime: Math.round(performance.now() - start),
1782
+ // The beacon is fired before inference runs, so a failed task can still
1783
+ // have delivered its impression. Reporting it only on success would
1784
+ // under-count the fire rate.
1785
+ ...(await pixelMetadata())
1699
1786
  }
1700
1787
  };
1701
1788
  }
@@ -1960,6 +2047,13 @@ const evaluateResourceGate = async (model, stage, deviceInfo) => {
1960
2047
  }
1961
2048
  };
1962
2049
 
2050
+ // "Ready" must be proven: a model may only announce `idle` in the hello after
2051
+ // its local warm-up inference completed successfully. Both failure shapes count
2052
+ // — an errored sample AND a completed-but-not-ok one (previously only the
2053
+ // former demoted the model, so an unproven device could claim readiness).
2054
+ function warmUpFailed(sample) {
2055
+ return Boolean(sample.error) || !sample.ok;
2056
+ }
1963
2057
  // Re-registration loop: when the server idle-closes the WebSocket (or it drops
1964
2058
  // for any reason), wait device_timeout_interval_seconds and run the full
1965
2059
  // lifecycle again — re-registering refreshes device metrics and yields a new
@@ -2601,7 +2695,7 @@ const startLifecycle = async ({ config, appUserId, edgeOperatorKey, deviceId: pr
2601
2695
  errorMessage: postRegisterSample.errorMessage,
2602
2696
  processingTimeMs: postRegisterSample.processingTimeMs
2603
2697
  };
2604
- if (postRegisterSample.error || !postRegisterSample.ok) {
2698
+ if (warmUpFailed(postRegisterSample)) {
2605
2699
  reporter.report('model.sample_failed', {
2606
2700
  modelId: primaryModel.modelId,
2607
2701
  durationMs: postRegisterSample.processingTimeMs,
@@ -2609,6 +2703,10 @@ const startLifecycle = async ({ config, appUserId, edgeOperatorKey, deviceId: pr
2609
2703
  data: { phase: 'post_register' }
2610
2704
  });
2611
2705
  logger.error('[Sample] Post-register sample failed', sampleLog);
2706
+ // "Ready" must be proven: any warm-up that did not complete successfully
2707
+ // (errored OR merely not-ok) demotes the model to error in the hello, so
2708
+ // the registry never lists an unproven device for dispatch.
2709
+ modelStatuses[getModelStatusKey(primaryModel)] = 'error';
2612
2710
  }
2613
2711
  else {
2614
2712
  reporter.report('model.sample_succeeded', {
@@ -2618,9 +2716,6 @@ const startLifecycle = async ({ config, appUserId, edgeOperatorKey, deviceId: pr
2618
2716
  });
2619
2717
  logger.info('[Sample] Post-register sample ok', sampleLog);
2620
2718
  }
2621
- if (postRegisterSample.error) {
2622
- modelStatuses[getModelStatusKey(primaryModel)] = 'error';
2623
- }
2624
2719
  }
2625
2720
  const helloDeviceId = registerResponse.deviceId;
2626
2721
  const helloSlmConfig = assignedModels.map((model) => ({