@camstack/system 1.2.62 → 1.2.64
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/addon-runner.js +1 -1
- package/dist/addon-runner.mjs +1 -1
- package/dist/addon-utils.js +1 -1
- package/dist/addon-utils.mjs +1 -1
- package/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.js +1 -1
- package/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.mjs +1 -1
- package/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.js +1 -1
- package/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.mjs +1 -1
- package/dist/builtins/alerts/alerts.addon.js +1 -1
- package/dist/builtins/alerts/alerts.addon.mjs +1 -1
- package/dist/builtins/backup-orchestrator/backup-orchestrator.addon.js +3 -3
- package/dist/builtins/backup-orchestrator/backup-orchestrator.addon.mjs +2 -2
- package/dist/builtins/console-logging/index.js +1 -1
- package/dist/builtins/console-logging/index.mjs +1 -1
- package/dist/builtins/core-blocks/block-package.d.ts +9 -27
- package/dist/builtins/core-blocks/block-supervisor.d.ts +15 -0
- package/dist/builtins/core-blocks/core-blocks.addon.js +91 -24
- package/dist/builtins/core-blocks/core-blocks.addon.mjs +91 -25
- package/dist/builtins/device-manager/device-bindings-store.d.ts +28 -5
- package/dist/builtins/device-manager/device-linked-devices.d.ts +2 -2
- package/dist/builtins/device-manager/device-manager.addon.d.ts +3 -45
- package/dist/builtins/device-manager/device-manager.addon.js +123 -884
- package/dist/builtins/device-manager/device-manager.addon.mjs +123 -884
- package/dist/builtins/device-manager/device-meta-actions.d.ts +0 -8
- package/dist/builtins/device-manager/device-meta-store.d.ts +1 -28
- package/dist/builtins/device-manager/device-meta-types.d.ts +2 -6
- package/dist/builtins/device-manager/device-projection.d.ts +1 -2
- package/dist/builtins/device-manager/device-provider-context.d.ts +2 -13
- package/dist/builtins/device-manager/device-queries.d.ts +1 -1
- package/dist/builtins/device-manager/device-state-mirror.d.ts +8 -49
- package/dist/builtins/device-manager/device-sync-resolvers.d.ts +0 -11
- package/dist/builtins/doorbell/virtual-doorbell.addon.js +1 -1
- package/dist/builtins/doorbell/virtual-doorbell.addon.mjs +1 -1
- package/dist/builtins/hub-forwarder/index.js +1 -1
- package/dist/builtins/hub-forwarder/index.mjs +1 -1
- package/dist/builtins/liveness-monitor/liveness-monitor.addon.js +1 -1
- package/dist/builtins/liveness-monitor/liveness-monitor.addon.mjs +1 -1
- package/dist/builtins/local-auth/homeassistant-integration.d.ts +7 -0
- package/dist/builtins/local-auth/index.d.ts +1 -0
- package/dist/builtins/local-auth/index.js +1 -0
- package/dist/builtins/local-auth/index.mjs +2 -2
- package/dist/builtins/local-auth/local-auth.addon.js +561 -497
- package/dist/builtins/local-auth/local-auth.addon.mjs +580 -517
- package/dist/builtins/local-auth/oauth-grants.d.ts +11 -0
- package/dist/builtins/local-auth/oauth-session-manager.d.ts +1 -1
- package/dist/builtins/local-network/local-network.addon.js +1 -1
- package/dist/builtins/local-network/local-network.addon.mjs +1 -1
- package/dist/builtins/loki-logging/index.js +1 -1
- package/dist/builtins/loki-logging/index.mjs +1 -1
- package/dist/builtins/native-metrics/native-metrics.addon.js +2 -2
- package/dist/builtins/native-metrics/native-metrics.addon.mjs +2 -2
- package/dist/builtins/platform-probe/index.js +2 -2
- package/dist/builtins/platform-probe/index.mjs +2 -2
- package/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.js +1 -1
- package/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.mjs +1 -1
- package/dist/builtins/snapshot/index.js +319 -20
- package/dist/builtins/snapshot/index.mjs +319 -20
- package/dist/builtins/snapshot/snapshot-keep-warm.d.ts +86 -0
- package/dist/builtins/snapshot/snapshot-media-handler.d.ts +48 -1
- package/dist/builtins/snapshot/snapshot.addon.d.ts +65 -1
- package/dist/builtins/sqlite-storage/filesystem-storage.addon.js +1 -1
- package/dist/builtins/sqlite-storage/filesystem-storage.addon.mjs +1 -1
- package/dist/builtins/sqlite-storage/retired-settings-keys.d.ts +15 -0
- package/dist/builtins/sqlite-storage/sqlite-settings.addon.js +73 -7
- package/dist/builtins/sqlite-storage/sqlite-settings.addon.mjs +73 -7
- package/dist/builtins/sqlite-storage/vector-index-vec.d.ts +19 -0
- package/dist/builtins/storage-orchestrator/storage-orchestrator.addon.js +1 -1
- package/dist/builtins/storage-orchestrator/storage-orchestrator.addon.mjs +1 -1
- package/dist/builtins/system-config/system-config.addon.js +1 -1
- package/dist/builtins/system-config/system-config.addon.mjs +1 -1
- package/dist/builtins/winston-logging/index.js +1 -1
- package/dist/builtins/winston-logging/index.mjs +1 -1
- package/dist/{dist-DnKpFaP1.mjs → dist-CMb7aKS0.mjs} +1227 -1330
- package/dist/{dist-DgPUarcs.js → dist-DP3nHlNM.js} +1251 -1366
- package/dist/http/asset-compression.d.ts +93 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +64 -9
- package/dist/index.mjs +65 -11
- package/dist/kernel/heap-watch.d.ts +55 -0
- package/dist/kernel/moleculer/device-cap-proxy.d.ts +2 -2
- package/dist/{manifest-python-deps-d4acYXVV.js → manifest-python-deps-BE6l8Y4W.js} +21 -15
- package/dist/{manifest-python-deps-CX5tGRqf.mjs → manifest-python-deps-Dz1ryHIN.mjs} +21 -15
- package/dist/{model-download-service-D8B-4ktF.mjs → model-download-service-CTBHzORJ.mjs} +220 -2
- package/dist/{model-download-service-Cpc6SWzV.js → model-download-service-Caj1kaSi.js} +220 -2
- package/package.json +5 -1
- package/dist/builtins/device-manager/device-link-cycle.d.ts +0 -18
- package/dist/builtins/device-manager/device-link-overlay.d.ts +0 -33
- package/dist/builtins/device-manager/device-link-resolver.d.ts +0 -44
- package/dist/main-CVLq8IzA.js +0 -2356
- package/dist/main-CzH19GkU.mjs +0 -2353
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as deviceStatusCapability, It as sleep, Lt as EventCategory, N as enumerateItemArrayFields, O as deviceManagerCapability, P as enumerateSchemaFields, Q as parseStreamParamsFormPatch, S as buildStreamParamsConfigSchema, Y as normalizeUnit, _t as DeviceType, a as CAP_NAMES_WITH_STATUS, bt as WELL_KNOWN_TAB_MAP, c as DeviceStatusSchema, d as STREAM_PROFILE_META, dt as BaseAddon, gt as DeviceRole, ht as DeviceFeature, k as deviceStateCapability, kt as isDeviceConfigCap, t as ALL_CAPABILITY_DEFINITIONS, ut as errMsg } from "../../dist-CMb7aKS0.mjs";
|
|
2
2
|
import { canonicalDeviceFingerprint } from "@camstack/types/node";
|
|
3
|
-
import { z } from "zod";
|
|
4
3
|
import { randomUUID } from "node:crypto";
|
|
5
4
|
//#region src/builtins/device-manager/day-night-config-schema.ts
|
|
6
5
|
var MODE_LABELS = {
|
|
@@ -532,13 +531,6 @@ function mergeAggregates(parts) {
|
|
|
532
531
|
//#endregion
|
|
533
532
|
//#region src/builtins/device-manager/device-projection.ts
|
|
534
533
|
/**
|
|
535
|
-
* Pure device-projection + small predicate helpers for the device-manager
|
|
536
|
-
* addon. Extracted verbatim from `device-manager.addon.ts`. None of these
|
|
537
|
-
* functions close over the addon instance — they take everything they need
|
|
538
|
-
* as explicit parameters, so they are safe to share across the addon's
|
|
539
|
-
* query / aggregation modules.
|
|
540
|
-
*/
|
|
541
|
-
/**
|
|
542
534
|
* Return true when `err` is a transient Moleculer error that is worth
|
|
543
535
|
* retrying — specifically any `MoleculerRetryableError` subclass
|
|
544
536
|
* (ServiceNotAvailableError, ServiceNotFoundError, BrokerDisconnectedError,
|
|
@@ -567,10 +559,6 @@ function shallowEqual(a, b) {
|
|
|
567
559
|
for (const k of ak) if (a[k] !== b[k]) return false;
|
|
568
560
|
return true;
|
|
569
561
|
}
|
|
570
|
-
/** Returns true when `x` is a non-null, non-array plain object. */
|
|
571
|
-
function isRecord$1(x) {
|
|
572
|
-
return x !== null && typeof x === "object" && !Array.isArray(x);
|
|
573
|
-
}
|
|
574
562
|
function isCameraDevice(device) {
|
|
575
563
|
return "getStreamSources" in device && typeof device.getStreamSources === "function";
|
|
576
564
|
}
|
|
@@ -655,7 +643,6 @@ function toDeviceInfo(addonId, device, metadata = null, metaRow = null) {
|
|
|
655
643
|
...metaRow?.linkDeviceId !== void 0 ? { linkDeviceId: metaRow.linkDeviceId } : {},
|
|
656
644
|
...metaRow?.primaryChildEntityId !== void 0 ? { primaryChildEntityId: metaRow.primaryChildEntityId } : {},
|
|
657
645
|
...metaRow?.childLayout !== void 0 ? { childLayout: metaRow.childLayout } : {},
|
|
658
|
-
...metaRow?.deviceLinks !== void 0 ? { deviceLinks: metaRow.deviceLinks } : {},
|
|
659
646
|
...metaRow?.display !== void 0 ? { display: metaRow.display } : {}
|
|
660
647
|
};
|
|
661
648
|
}
|
|
@@ -671,28 +658,6 @@ function resolveDeviceById(registry, deviceId) {
|
|
|
671
658
|
}
|
|
672
659
|
//#endregion
|
|
673
660
|
//#region src/builtins/device-manager/device-queries.ts
|
|
674
|
-
/**
|
|
675
|
-
* Device query + live-runtime + provider/adoption operations for the
|
|
676
|
-
* device-manager addon — the read/live half of the `IDeviceManagerProvider`
|
|
677
|
-
* surface. Extracted verbatim from the `onInitialize` provider literal as free
|
|
678
|
-
* functions taking a `ProviderContext` (DI). Input/return types are pinned to
|
|
679
|
-
* the cap contract via `Parameters<IDeviceManagerProvider['x']>[0]` /
|
|
680
|
-
* `ReturnType<…>` so the typed surface is identical to the inferred methods.
|
|
681
|
-
*
|
|
682
|
-
* Covers: persisted/live device queries (listPersistedByAddon, listAll,
|
|
683
|
-
* getDevice, getChildren), live runtime methods (getStreamSources,
|
|
684
|
-
* getConfigSchema, getSettingsSchema, updateConfig, enable, disable, remove,
|
|
685
|
-
* removeByIntegration, getStreamProfileMap, setStreamProfileMap, probeStreams),
|
|
686
|
-
* device-provider ops (discoverDevices, adoptDevice, getCreationSchema,
|
|
687
|
-
* createDevice, testCreationField), and device-adoption ops
|
|
688
|
-
* (adoptionListCandidates/Refresh/Adopt/Release/Resync, testField).
|
|
689
|
-
*
|
|
690
|
-
* The hub-live-then-persisted-fallback union, the cross-process device-ops
|
|
691
|
-
* dispatch, the depth-first remove cascade, and the resetToSource resync purge
|
|
692
|
-
* are byte-for-byte preserved. `remove`/`removeByIntegration`/`enable`/`disable`/
|
|
693
|
-
* `probeStreams`/`getStreamProfileMap` self-call sibling cap methods via
|
|
694
|
-
* `pctx.provider`. Pure refactor — no behavior change.
|
|
695
|
-
*/
|
|
696
661
|
async function listPersistedByAddon(pctx, input) {
|
|
697
662
|
const { addonId } = input;
|
|
698
663
|
const [index, meta] = await Promise.all([pctx.metaStore.readIndex(), pctx.metaStore.readMeta()]);
|
|
@@ -769,7 +734,6 @@ async function listAll(pctx, input) {
|
|
|
769
734
|
...m?.linkDeviceId !== void 0 ? { linkDeviceId: m.linkDeviceId } : {},
|
|
770
735
|
...m?.primaryChildEntityId !== void 0 ? { primaryChildEntityId: m.primaryChildEntityId } : {},
|
|
771
736
|
...m?.childLayout !== void 0 ? { childLayout: m.childLayout } : {},
|
|
772
|
-
...m?.deviceLinks !== void 0 ? { deviceLinks: m.deviceLinks } : {},
|
|
773
737
|
...m?.display !== void 0 ? { display: m.display } : {},
|
|
774
738
|
...(() => {
|
|
775
739
|
if (slim) return {};
|
|
@@ -819,7 +783,6 @@ async function getDevice(pctx, input) {
|
|
|
819
783
|
...m.linkDeviceId !== void 0 ? { linkDeviceId: m.linkDeviceId } : {},
|
|
820
784
|
...m.primaryChildEntityId !== void 0 ? { primaryChildEntityId: m.primaryChildEntityId } : {},
|
|
821
785
|
...m.childLayout !== void 0 ? { childLayout: m.childLayout } : {},
|
|
822
|
-
...m.deviceLinks !== void 0 ? { deviceLinks: m.deviceLinks } : {},
|
|
823
786
|
...m.display !== void 0 ? { display: m.display } : {},
|
|
824
787
|
...sourceInfoGetDevice !== void 0 ? { sourceInfo: sourceInfoGetDevice } : {}
|
|
825
788
|
};
|
|
@@ -882,7 +845,6 @@ async function getChildren(pctx, input) {
|
|
|
882
845
|
...m.linkDeviceId !== void 0 ? { linkDeviceId: m.linkDeviceId } : {},
|
|
883
846
|
...m.primaryChildEntityId !== void 0 ? { primaryChildEntityId: m.primaryChildEntityId } : {},
|
|
884
847
|
...m.childLayout !== void 0 ? { childLayout: m.childLayout } : {},
|
|
885
|
-
...m.deviceLinks !== void 0 ? { deviceLinks: m.deviceLinks } : {},
|
|
886
848
|
...m.display !== void 0 ? { display: m.display } : {},
|
|
887
849
|
...sourceInfoChild !== void 0 ? { sourceInfo: sourceInfoChild } : {}
|
|
888
850
|
});
|
|
@@ -1438,18 +1400,6 @@ function splitDeviceStoreKeys(patch) {
|
|
|
1438
1400
|
//#endregion
|
|
1439
1401
|
//#region src/builtins/device-manager/device-bindings-store.ts
|
|
1440
1402
|
/**
|
|
1441
|
-
* Persisted wrapper-activation + per-device binding resolution for the
|
|
1442
|
-
* device-manager addon. Extracted from `device-manager.addon.ts` as free
|
|
1443
|
-
* functions that receive an explicit `BindingsDeps` context (dependency
|
|
1444
|
-
* injection) instead of closing over the addon instance — keeps the routing
|
|
1445
|
-
* logic cohesive and independently testable while preserving the EXACT call
|
|
1446
|
-
* semantics, ordering, and side effects of the original class methods.
|
|
1447
|
-
*
|
|
1448
|
-
* The addon keeps thin delegator methods (`getBindings`, `getAllBindings`, …)
|
|
1449
|
-
* that build `BindingsDeps` from its own state and forward here, so every
|
|
1450
|
-
* existing internal + cap-router call site is unchanged.
|
|
1451
|
-
*/
|
|
1452
|
-
/**
|
|
1453
1403
|
* Wire the push-fed cross-process native-cap cache (`remoteNativeCaps`).
|
|
1454
1404
|
* Workers emit `DeviceBindingsChanged` on `ctx.registerNativeCap` / device
|
|
1455
1405
|
* removal; we mirror those into `remoteNativeCaps` so `getBindings` returns the
|
|
@@ -1551,9 +1501,47 @@ function resolveRemoteNativeCapFromRegistry(deps, capName, deviceId) {
|
|
|
1551
1501
|
};
|
|
1552
1502
|
return null;
|
|
1553
1503
|
}
|
|
1504
|
+
/**
|
|
1505
|
+
* Resolve the device's declared TYPE (`'camera'`, `'event-emitter'`, …), or
|
|
1506
|
+
* `undefined` when it cannot be established.
|
|
1507
|
+
*
|
|
1508
|
+
* The PERSISTED meta is the authority: `ctx.kernel.deviceRegistry` is hub-only
|
|
1509
|
+
* and has been observed empty in the very process that answers `getBindings`
|
|
1510
|
+
* correctly (see the note on `getAllBindings`). The registry is consulted only
|
|
1511
|
+
* as a secondary source, for a device constructed but not yet persisted.
|
|
1512
|
+
*
|
|
1513
|
+
* `undefined` is a first-class answer and callers must treat it as "no
|
|
1514
|
+
* filtering" — an absent row must never be the reason a device loses bindings.
|
|
1515
|
+
*/
|
|
1516
|
+
function resolveDeviceType(deps, rawStore, deviceId) {
|
|
1517
|
+
const persisted = rawStore.deviceMeta?.[String(deviceId)]?.type;
|
|
1518
|
+
if (typeof persisted === "string" && persisted.length > 0) return persisted;
|
|
1519
|
+
const live = deps.ctx.kernel?.deviceRegistry?.getById(deviceId)?.type;
|
|
1520
|
+
return typeof live === "string" && live.length > 0 ? live : void 0;
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* Does a capability apply to a device of type `deviceType`?
|
|
1524
|
+
*
|
|
1525
|
+
* The cap's `deviceTypes` is the ONLY declaration consulted (D4: behavioural
|
|
1526
|
+
* cap metadata lives in the `*.cap.ts`, never in a manifest). Two deliberate
|
|
1527
|
+
* fail-open cases:
|
|
1528
|
+
*
|
|
1529
|
+
* - a cap that declares no `deviceTypes` (or an empty list) applies to every
|
|
1530
|
+
* device — the pre-existing, back-compatible semantics;
|
|
1531
|
+
* - an UNKNOWN `deviceType` never filters, so a missing/failed meta lookup
|
|
1532
|
+
* changes nothing (D49: a read that fails must not destroy work).
|
|
1533
|
+
*/
|
|
1534
|
+
function capAppliesToDeviceType(def, deviceType) {
|
|
1535
|
+
if (deviceType === void 0) return true;
|
|
1536
|
+
const declared = def?.deviceTypes;
|
|
1537
|
+
if (!declared || declared.length === 0) return true;
|
|
1538
|
+
return declared.some((t) => t === deviceType);
|
|
1539
|
+
}
|
|
1554
1540
|
async function getBindings(deps, input) {
|
|
1555
1541
|
const storeKey = String(input.deviceId);
|
|
1556
|
-
const
|
|
1542
|
+
const rawStore = await deps.ctx.settings.readAddonStore();
|
|
1543
|
+
const perDevice = (rawStore.deviceBindings ?? {})[storeKey] ?? {};
|
|
1544
|
+
const deviceType = resolveDeviceType(deps, rawStore, input.deviceId);
|
|
1557
1545
|
const entries = [];
|
|
1558
1546
|
const seenCaps = /* @__PURE__ */ new Set();
|
|
1559
1547
|
const resolveRemote = (capName) => deps.remoteNativeCaps.get(input.deviceId)?.get(capName) ?? resolveRemoteNativeCapFromRegistry(deps, capName, input.deviceId);
|
|
@@ -1575,21 +1563,35 @@ async function getBindings(deps, input) {
|
|
|
1575
1563
|
});
|
|
1576
1564
|
seenCaps.add(capName);
|
|
1577
1565
|
}
|
|
1578
|
-
if (deps.capabilityRegistry)
|
|
1579
|
-
|
|
1580
|
-
const
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
capName
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
nativeAddonId
|
|
1566
|
+
if (deps.capabilityRegistry) {
|
|
1567
|
+
const skippedForType = [];
|
|
1568
|
+
for (const capName of deps.capabilityRegistry.getCapsWithDefaultWrapper()) {
|
|
1569
|
+
if (seenCaps.has(capName)) continue;
|
|
1570
|
+
if (!capAppliesToDeviceType(deps.capabilityRegistry.getDefinition(capName), deviceType)) {
|
|
1571
|
+
skippedForType.push(capName);
|
|
1572
|
+
continue;
|
|
1573
|
+
}
|
|
1574
|
+
const defaultWrapperAddonId = deps.capabilityRegistry.getDefaultWrapperForCap(capName);
|
|
1575
|
+
if (!defaultWrapperAddonId) continue;
|
|
1576
|
+
const hubLocalNative = deps.capabilityRegistry.getNativeAddonId(capName, input.deviceId) ?? null;
|
|
1577
|
+
const remoteNative = resolveRemote(capName);
|
|
1578
|
+
const nativeAddonId = hubLocalNative ?? remoteNative?.addonId ?? "";
|
|
1579
|
+
entries.push({
|
|
1580
|
+
capName,
|
|
1581
|
+
kind: "wrapped",
|
|
1582
|
+
providerAddonId: defaultWrapperAddonId,
|
|
1583
|
+
providerNodeId: resolveWrapperNodeId(defaultWrapperAddonId),
|
|
1584
|
+
nativeAddonId
|
|
1585
|
+
});
|
|
1586
|
+
seenCaps.add(capName);
|
|
1587
|
+
}
|
|
1588
|
+
if (skippedForType.length > 0) deps.ctx.logger.debug("getBindings: default wrappers skipped — deviceTypes mismatch", {
|
|
1589
|
+
tags: { deviceId: input.deviceId },
|
|
1590
|
+
meta: {
|
|
1591
|
+
deviceType,
|
|
1592
|
+
skipped: skippedForType
|
|
1593
|
+
}
|
|
1591
1594
|
});
|
|
1592
|
-
seenCaps.add(capName);
|
|
1593
1595
|
}
|
|
1594
1596
|
if (deps.capabilityRegistry) for (const capName of deps.capabilityRegistry.getNativeCapsForDevice(input.deviceId)) {
|
|
1595
1597
|
if (seenCaps.has(capName)) continue;
|
|
@@ -1631,21 +1633,6 @@ async function getBindings(deps, input) {
|
|
|
1631
1633
|
});
|
|
1632
1634
|
seenCaps.add(entry.capName);
|
|
1633
1635
|
}
|
|
1634
|
-
if (deps.devicesWithLinks?.has(input.deviceId)) {
|
|
1635
|
-
const row = ((await deps.ctx.settings.readAddonStore()).deviceMeta ?? {})[String(input.deviceId)];
|
|
1636
|
-
for (const link of row?.deviceLinks ?? []) {
|
|
1637
|
-
const capName = link.target.cap;
|
|
1638
|
-
if (seenCaps.has(capName)) continue;
|
|
1639
|
-
entries.push({
|
|
1640
|
-
capName,
|
|
1641
|
-
kind: "linked",
|
|
1642
|
-
providerAddonId: bareAddonId(deps.ctx.id),
|
|
1643
|
-
providerNodeId: deps.ctx.kernel.localNodeId ?? "hub",
|
|
1644
|
-
nativeAddonId: ""
|
|
1645
|
-
});
|
|
1646
|
-
seenCaps.add(capName);
|
|
1647
|
-
}
|
|
1648
|
-
}
|
|
1649
1636
|
return {
|
|
1650
1637
|
deviceId: input.deviceId,
|
|
1651
1638
|
entries
|
|
@@ -1663,10 +1650,20 @@ async function getBindings(deps, input) {
|
|
|
1663
1650
|
* payload periodically.
|
|
1664
1651
|
*/
|
|
1665
1652
|
async function getAllBindings(deps) {
|
|
1666
|
-
const
|
|
1667
|
-
|
|
1653
|
+
const store = await deps.ctx.settings.readAddonStore();
|
|
1654
|
+
const ids = /* @__PURE__ */ new Set();
|
|
1655
|
+
for (const key of Object.keys(store.deviceMeta ?? {})) {
|
|
1656
|
+
const id = Number(key);
|
|
1657
|
+
if (Number.isInteger(id)) ids.add(id);
|
|
1658
|
+
}
|
|
1659
|
+
const registered = deps.ctx.kernel?.deviceRegistry?.getAll() ?? [];
|
|
1660
|
+
for (const device of registered) ids.add(device.id);
|
|
1661
|
+
if (ids.size === 0) {
|
|
1662
|
+
deps.ctx.logger.warn("getAllBindings found no devices — warm boot will see nothing", { meta: { hasRegistry: deps.ctx.kernel?.deviceRegistry !== void 0 } });
|
|
1663
|
+
return [];
|
|
1664
|
+
}
|
|
1668
1665
|
const out = [];
|
|
1669
|
-
for (const
|
|
1666
|
+
for (const deviceId of [...ids].sort((a, b) => a - b)) out.push(await getBindings(deps, { deviceId }));
|
|
1670
1667
|
return out;
|
|
1671
1668
|
}
|
|
1672
1669
|
/**
|
|
@@ -2590,120 +2587,6 @@ var DeviceEventPropagator = class {
|
|
|
2590
2587
|
}
|
|
2591
2588
|
};
|
|
2592
2589
|
//#endregion
|
|
2593
|
-
//#region src/builtins/device-manager/device-link-cycle.ts
|
|
2594
|
-
function nodeKey(deviceId, cap) {
|
|
2595
|
-
return `${deviceId}:${cap}`;
|
|
2596
|
-
}
|
|
2597
|
-
/** The target's container stableId — its parent's (falling back to its own for
|
|
2598
|
-
* a top-level target). Sibling FIELD sources resolve relative to this, the same
|
|
2599
|
-
* rule `rebuildLinkDependents` / `resolveLinkedStatus` apply. */
|
|
2600
|
-
function containerStableId(targetRow, rowById) {
|
|
2601
|
-
return targetRow.parentDeviceId !== null ? rowById.get(targetRow.parentDeviceId)?.stableId ?? targetRow.stableId : targetRow.stableId;
|
|
2602
|
-
}
|
|
2603
|
-
/** Resolve ONE field/global/literal binding to its `(deviceId, cap)` node, or
|
|
2604
|
-
* null when it is a literal or resolves to no known device. */
|
|
2605
|
-
function resolveBindingNode(binding, targetRow, rowById, idByStableId) {
|
|
2606
|
-
if (binding.kind === "literal") return null;
|
|
2607
|
-
const wantedStableId = binding.kind === "global" ? binding.sourceStableId : `${containerStableId(targetRow, rowById)}-${binding.sourceKey}`;
|
|
2608
|
-
const srcId = idByStableId.get(wantedStableId);
|
|
2609
|
-
if (srcId === void 0) return null;
|
|
2610
|
-
return nodeKey(srcId, binding.cap);
|
|
2611
|
-
}
|
|
2612
|
-
/**
|
|
2613
|
-
* Resolve a link source to the set of `(deviceId, cap)` nodes it depends on.
|
|
2614
|
-
* A literal source depends on nothing; a field/global source depends on exactly
|
|
2615
|
-
* one node; an EXPRESSION source depends on one node per resolvable non-literal
|
|
2616
|
-
* binding. Unresolvable (dangling) bindings contribute no edge — they cannot
|
|
2617
|
-
* loop while dangling; a cycle that only becomes resolvable later is bounded by
|
|
2618
|
-
* the resolve-time re-entrancy guard.
|
|
2619
|
-
*/
|
|
2620
|
-
function resolveSourceNodes(link, targetRow, rowById, idByStableId) {
|
|
2621
|
-
const src = link.source;
|
|
2622
|
-
if (src.kind === "expression") {
|
|
2623
|
-
const nodes = [];
|
|
2624
|
-
for (const binding of Object.values(src.bindings)) {
|
|
2625
|
-
const node = resolveBindingNode(binding, targetRow, rowById, idByStableId);
|
|
2626
|
-
if (node !== null) nodes.push(node);
|
|
2627
|
-
}
|
|
2628
|
-
return nodes;
|
|
2629
|
-
}
|
|
2630
|
-
const node = resolveBindingNode(src, targetRow, rowById, idByStableId);
|
|
2631
|
-
return node !== null ? [node] : [];
|
|
2632
|
-
}
|
|
2633
|
-
/**
|
|
2634
|
-
* Detect whether replacing `editedDeviceId`'s links with `editedLinks` closes
|
|
2635
|
-
* a dependency cycle. Returns the cycle as an ordered list of node keys
|
|
2636
|
-
* (first node repeated at the end) or null when the set is safe.
|
|
2637
|
-
*/
|
|
2638
|
-
function findDeviceLinkCycle(allMeta, editedDeviceId, editedLinks) {
|
|
2639
|
-
const rows = Object.values(allMeta);
|
|
2640
|
-
const rowById = /* @__PURE__ */ new Map();
|
|
2641
|
-
const idByStableId = /* @__PURE__ */ new Map();
|
|
2642
|
-
for (const r of rows) {
|
|
2643
|
-
rowById.set(r.id, r);
|
|
2644
|
-
if (!idByStableId.has(r.stableId)) idByStableId.set(r.stableId, r.id);
|
|
2645
|
-
}
|
|
2646
|
-
const dependsOn = /* @__PURE__ */ new Map();
|
|
2647
|
-
const addEdges = (row, links) => {
|
|
2648
|
-
for (const link of links) {
|
|
2649
|
-
const srcNodes = resolveSourceNodes(link, row, rowById, idByStableId);
|
|
2650
|
-
if (srcNodes.length === 0) continue;
|
|
2651
|
-
const tNode = nodeKey(row.id, link.target.cap);
|
|
2652
|
-
const list = dependsOn.get(tNode) ?? [];
|
|
2653
|
-
for (const srcNode of srcNodes) list.push(srcNode);
|
|
2654
|
-
dependsOn.set(tNode, list);
|
|
2655
|
-
}
|
|
2656
|
-
};
|
|
2657
|
-
for (const r of rows) {
|
|
2658
|
-
const links = r.id === editedDeviceId ? editedLinks : r.deviceLinks ?? [];
|
|
2659
|
-
if (links.length > 0) addEdges(r, links);
|
|
2660
|
-
}
|
|
2661
|
-
const roots = [...new Set(editedLinks.map((l) => nodeKey(editedDeviceId, l.target.cap)))];
|
|
2662
|
-
for (const root of roots) {
|
|
2663
|
-
const cycle = dfsFindCycle(root, dependsOn);
|
|
2664
|
-
if (cycle) return cycle;
|
|
2665
|
-
}
|
|
2666
|
-
return null;
|
|
2667
|
-
}
|
|
2668
|
-
/** Iterative DFS from `root` over `dependsOn`; returns the first back-edge
|
|
2669
|
-
* cycle path (closed — first node repeated last) or null. */
|
|
2670
|
-
function dfsFindCycle(root, dependsOn) {
|
|
2671
|
-
const onPath = /* @__PURE__ */ new Set();
|
|
2672
|
-
const done = /* @__PURE__ */ new Set();
|
|
2673
|
-
const path = [];
|
|
2674
|
-
const stack = [{
|
|
2675
|
-
node: root,
|
|
2676
|
-
nextChild: 0
|
|
2677
|
-
}];
|
|
2678
|
-
onPath.add(root);
|
|
2679
|
-
path.push(root);
|
|
2680
|
-
while (stack.length > 0) {
|
|
2681
|
-
const frame = stack[stack.length - 1];
|
|
2682
|
-
const children = dependsOn.get(frame.node) ?? [];
|
|
2683
|
-
if (frame.nextChild < children.length) {
|
|
2684
|
-
const child = children[frame.nextChild];
|
|
2685
|
-
frame.nextChild += 1;
|
|
2686
|
-
if (onPath.has(child)) {
|
|
2687
|
-
const start = path.indexOf(child);
|
|
2688
|
-
return [...path.slice(start), child];
|
|
2689
|
-
}
|
|
2690
|
-
if (done.has(child)) continue;
|
|
2691
|
-
stack.push({
|
|
2692
|
-
node: child,
|
|
2693
|
-
nextChild: 0
|
|
2694
|
-
});
|
|
2695
|
-
onPath.add(child);
|
|
2696
|
-
path.push(child);
|
|
2697
|
-
continue;
|
|
2698
|
-
}
|
|
2699
|
-
stack.pop();
|
|
2700
|
-
onPath.delete(frame.node);
|
|
2701
|
-
path.pop();
|
|
2702
|
-
done.add(frame.node);
|
|
2703
|
-
}
|
|
2704
|
-
return null;
|
|
2705
|
-
}
|
|
2706
|
-
//#endregion
|
|
2707
2590
|
//#region src/builtins/device-manager/device-meta-actions.ts
|
|
2708
2591
|
/**
|
|
2709
2592
|
* Device meta-mutation + persistence actions for the device-manager addon —
|
|
@@ -2717,7 +2600,7 @@ function dfsFindCycle(root, dependsOn) {
|
|
|
2717
2600
|
* removeDevice), config persistence (persistConfig, loadConfig), the meta
|
|
2718
2601
|
* surface load (loadMeta, loadRuntimeState), every meta setter (setName,
|
|
2719
2602
|
* setLocation, setType, setIntegrationId, setLinkDeviceId,
|
|
2720
|
-
* setPrimaryChildEntityId, setChildLayout,
|
|
2603
|
+
* setPrimaryChildEntityId, setChildLayout, setRole, setDisplay,
|
|
2721
2604
|
* applyInitialMeta, setMetadata, setDisabled), and the location registry
|
|
2722
2605
|
* (listLocations, addLocation, removeLocation).
|
|
2723
2606
|
*
|
|
@@ -2824,7 +2707,6 @@ async function registerDevice(pctx, input) {
|
|
|
2824
2707
|
...existingMeta?.linkDeviceId !== void 0 ? { linkDeviceId: existingMeta.linkDeviceId } : {},
|
|
2825
2708
|
...existingMeta?.primaryChildEntityId !== void 0 ? { primaryChildEntityId: existingMeta.primaryChildEntityId } : {},
|
|
2826
2709
|
...existingMeta?.childLayout !== void 0 ? { childLayout: existingMeta.childLayout } : {},
|
|
2827
|
-
...existingMeta?.deviceLinks !== void 0 ? { deviceLinks: existingMeta.deviceLinks } : {},
|
|
2828
2710
|
...existingMeta?.role !== void 0 ? { role: existingMeta.role } : {},
|
|
2829
2711
|
...existingMeta?.display !== void 0 ? { display: existingMeta.display } : {},
|
|
2830
2712
|
parentDeviceId,
|
|
@@ -2886,7 +2768,6 @@ async function registerDevice(pctx, input) {
|
|
|
2886
2768
|
generation: Date.now()
|
|
2887
2769
|
}
|
|
2888
2770
|
});
|
|
2889
|
-
if (pctx.host.devicesWithLinks.size > 0 && pctx.host.expectedSourceStableIds.has(stableId)) await pctx.metaStore.rebuildLinkDependents();
|
|
2890
2771
|
}
|
|
2891
2772
|
async function removeDevice(pctx, input) {
|
|
2892
2773
|
const { deviceId } = input;
|
|
@@ -2925,9 +2806,6 @@ async function removeDevice(pctx, input) {
|
|
|
2925
2806
|
pctx.host.remoteNativeCaps.delete(deviceId);
|
|
2926
2807
|
pctx.host.capabilityRegistry?.unregisterAllNativeForDevice(deviceId);
|
|
2927
2808
|
pctx.metaStore.idToAddonId.delete(deviceId);
|
|
2928
|
-
pctx.host.devicesWithLinks.delete(deviceId);
|
|
2929
|
-
pctx.host.dropDeviceOverlays(deviceId);
|
|
2930
|
-
await pctx.metaStore.rebuildLinkDependents();
|
|
2931
2809
|
pctx.host.ctx.logger.info("removed device", { tags: {
|
|
2932
2810
|
deviceId,
|
|
2933
2811
|
deviceName: deviceName.length > 0 ? deviceName : stableId
|
|
@@ -3258,54 +3136,6 @@ async function setChildLayout(pctx, input) {
|
|
|
3258
3136
|
});
|
|
3259
3137
|
}
|
|
3260
3138
|
/**
|
|
3261
|
-
* Set (or replace) the operator-authored cross-device field wirings on
|
|
3262
|
-
* a device's meta row. Mirrors `setChildLayout` but persists the
|
|
3263
|
-
* `deviceLinks` array. Persisted, projected, and preserved across
|
|
3264
|
-
* re-register/restore. Also maintains the in-memory `devicesWithLinks`
|
|
3265
|
-
* Set used as an O(1) gate by `resolveLinkedStatus`.
|
|
3266
|
-
*/
|
|
3267
|
-
async function setDeviceLinks(pctx, input) {
|
|
3268
|
-
const { deviceId, deviceLinks } = input;
|
|
3269
|
-
const cycle = findDeviceLinkCycle(await pctx.metaStore.readMeta(), deviceId, deviceLinks);
|
|
3270
|
-
if (cycle) throw new Error(`[device-manager] setDeviceLinks: cross-device link cycle: ${cycle.join(" → ")}`);
|
|
3271
|
-
for (const link of deviceLinks) {
|
|
3272
|
-
if (link.source.kind !== "expression") continue;
|
|
3273
|
-
const err = validateExpressionSource(link.source);
|
|
3274
|
-
if (err) throw new Error(`[device-manager] setDeviceLinks: invalid expression on link '${link.id}': ${err}`);
|
|
3275
|
-
}
|
|
3276
|
-
await pctx.metaStore.withMetaWriteLock(async () => {
|
|
3277
|
-
const persisted = await pctx.metaStore.resolvePersistedById(deviceId);
|
|
3278
|
-
if (!persisted) throw new Error(`[device-manager] setDeviceLinks: unknown device id=${deviceId}`);
|
|
3279
|
-
const { meta: m } = persisted;
|
|
3280
|
-
const key = String(deviceId);
|
|
3281
|
-
const allMeta = await pctx.metaStore.readMeta();
|
|
3282
|
-
await pctx.settings.writeAddonStore({ deviceMeta: {
|
|
3283
|
-
...allMeta,
|
|
3284
|
-
[key]: {
|
|
3285
|
-
...m,
|
|
3286
|
-
deviceLinks
|
|
3287
|
-
}
|
|
3288
|
-
} });
|
|
3289
|
-
});
|
|
3290
|
-
if (deviceLinks.length > 0) pctx.host.devicesWithLinks.add(deviceId);
|
|
3291
|
-
else pctx.host.devicesWithLinks.delete(deviceId);
|
|
3292
|
-
await pctx.metaStore.rebuildLinkDependents();
|
|
3293
|
-
pctx.host.ctx.eventBus.emit({
|
|
3294
|
-
id: randomUUID(),
|
|
3295
|
-
timestamp: /* @__PURE__ */ new Date(),
|
|
3296
|
-
source: {
|
|
3297
|
-
type: "device",
|
|
3298
|
-
id: deviceId
|
|
3299
|
-
},
|
|
3300
|
-
category: EventCategory.DeviceMetaChanged,
|
|
3301
|
-
data: {
|
|
3302
|
-
deviceId,
|
|
3303
|
-
field: "deviceLinks",
|
|
3304
|
-
value: deviceLinks
|
|
3305
|
-
}
|
|
3306
|
-
});
|
|
3307
|
-
}
|
|
3308
|
-
/**
|
|
3309
3139
|
* Stamp (or update) the semantic role on the device's meta row.
|
|
3310
3140
|
* Called by the kernel's `create()` / `spawnAccessoryChild`
|
|
3311
3141
|
* pre-seed when `initialMeta.role` is set (analogous to
|
|
@@ -3660,69 +3490,10 @@ async function removeLocation(pctx, input) {
|
|
|
3660
3490
|
});
|
|
3661
3491
|
}
|
|
3662
3492
|
//#endregion
|
|
3663
|
-
//#region src/builtins/device-manager/device-link-overlay.ts
|
|
3664
|
-
/** Build both lookup maps from resolved link entries. Pure — order-preserving. */
|
|
3665
|
-
function buildLinkIndexes(entries) {
|
|
3666
|
-
const targets = /* @__PURE__ */ new Map();
|
|
3667
|
-
const dependents = /* @__PURE__ */ new Map();
|
|
3668
|
-
for (const { targetDeviceId, link, sourceDeviceId, bindingSourceIds } of entries) {
|
|
3669
|
-
const tKey = `${targetDeviceId}:${link.target.cap}`;
|
|
3670
|
-
const tList = targets.get(tKey) ?? [];
|
|
3671
|
-
tList.push({
|
|
3672
|
-
link,
|
|
3673
|
-
sourceDeviceId,
|
|
3674
|
-
...bindingSourceIds !== void 0 ? { bindingSourceIds } : {}
|
|
3675
|
-
});
|
|
3676
|
-
targets.set(tKey, tList);
|
|
3677
|
-
const addDependent = (sourceId, sourceCap) => {
|
|
3678
|
-
const sKey = `${sourceId}:${sourceCap}`;
|
|
3679
|
-
const sList = dependents.get(sKey) ?? [];
|
|
3680
|
-
sList.push({
|
|
3681
|
-
targetDeviceId,
|
|
3682
|
-
targetCap: link.target.cap
|
|
3683
|
-
});
|
|
3684
|
-
dependents.set(sKey, sList);
|
|
3685
|
-
};
|
|
3686
|
-
if (link.source.kind === "expression") {
|
|
3687
|
-
if (bindingSourceIds !== void 0) for (const [name, bindingDeviceId] of Object.entries(bindingSourceIds)) {
|
|
3688
|
-
const binding = link.source.bindings[name];
|
|
3689
|
-
if (binding === void 0 || binding.kind === "literal") continue;
|
|
3690
|
-
addDependent(bindingDeviceId, binding.cap);
|
|
3691
|
-
}
|
|
3692
|
-
continue;
|
|
3693
|
-
}
|
|
3694
|
-
if (link.source.kind !== "literal") addDependent(sourceDeviceId, link.source.cap);
|
|
3695
|
-
}
|
|
3696
|
-
return {
|
|
3697
|
-
targets,
|
|
3698
|
-
dependents
|
|
3699
|
-
};
|
|
3700
|
-
}
|
|
3701
|
-
//#endregion
|
|
3702
3493
|
//#region src/builtins/device-manager/device-meta-store.ts
|
|
3703
|
-
/**
|
|
3704
|
-
* Meta persistence layer for the device-manager addon.
|
|
3705
|
-
*
|
|
3706
|
-
* `DeviceMetaStore` encapsulates the read/modify/write helpers that previously
|
|
3707
|
-
* lived as closures inside `onInitialize`: the `ctx.settings`-backed reads
|
|
3708
|
-
* (`readStore` / `readIndex` / `readMeta` / `readMetadataMap`), the
|
|
3709
|
-
* single-writer mutex (`withMetaWriteLock`), id allocation
|
|
3710
|
-
* (`allocateNextDeviceId`), ownership/child resolution, link-source resolution,
|
|
3711
|
-
* and the cross-device link reverse-index rebuild. Extracting it as a class
|
|
3712
|
-
* (constructed once per `onInitialize`) keeps the EXACT semantics — the
|
|
3713
|
-
* `withMetaWriteLock` promise-chain is per-instance state, identical to the
|
|
3714
|
-
* former closure — while letting the meta-action + query modules depend on a
|
|
3715
|
-
* named, injectable surface instead of captured closures.
|
|
3716
|
-
*
|
|
3717
|
-
* Cross-device link state (the `linkTargets` / `linkDependents` /
|
|
3718
|
-
* `expectedSourceStableIds` maps + the `devicesWithLinks` gate) lives on the
|
|
3719
|
-
* addon; the store reaches it through the injected `LinkIndexHost` so the
|
|
3720
|
-
* single owner of that state stays the addon instance.
|
|
3721
|
-
*/
|
|
3722
3494
|
var DeviceMetaStore = class {
|
|
3723
3495
|
settings;
|
|
3724
3496
|
registry;
|
|
3725
|
-
linkHost;
|
|
3726
3497
|
/** Synchronous ownership cache, keyed by NUMERIC deviceId → owning addonId.
|
|
3727
3498
|
* The persisted meta store is authoritative but reads are async; hub-side
|
|
3728
3499
|
* callers (e.g. `CapabilityRegistry.getNativeProvider` fallback) need
|
|
@@ -3733,10 +3504,9 @@ var DeviceMetaStore = class {
|
|
|
3733
3504
|
* through one promise chain (see `withMetaWriteLock`). Per-instance state —
|
|
3734
3505
|
* identical to the former `onInitialize` closure variable. */
|
|
3735
3506
|
metaWriteChain = Promise.resolve();
|
|
3736
|
-
constructor(settings, registry
|
|
3507
|
+
constructor(settings, registry) {
|
|
3737
3508
|
this.settings = settings;
|
|
3738
3509
|
this.registry = registry;
|
|
3739
|
-
this.linkHost = linkHost;
|
|
3740
3510
|
}
|
|
3741
3511
|
readStore = async () => {
|
|
3742
3512
|
return await this.settings.readAddonStore();
|
|
@@ -3798,279 +3568,31 @@ var DeviceMetaStore = class {
|
|
|
3798
3568
|
ids.delete(parentId);
|
|
3799
3569
|
return [...ids];
|
|
3800
3570
|
};
|
|
3801
|
-
/** Resolve a link's `sourceKey` to a live device id: the sibling accessory
|
|
3802
|
-
* whose stableId is `${parentStableId}-${sourceKey}`. Null when absent.
|
|
3803
|
-
* Pure over a pre-read meta map so a multi-link resolve reads the store once. */
|
|
3804
|
-
resolveSourceDeviceId = (parentStableId, sourceKey, meta) => {
|
|
3805
|
-
const wanted = `${parentStableId}-${sourceKey}`;
|
|
3806
|
-
for (const m of Object.values(meta)) if (m.stableId === wanted) return m.id;
|
|
3807
|
-
return null;
|
|
3808
|
-
};
|
|
3809
|
-
/** Resolve a GLOBAL link source (P2e) to a live device id: the device whose
|
|
3810
|
-
* FULL stableId equals `sourceStableId`, regardless of parent container.
|
|
3811
|
-
* First match wins (stableIds are effectively unique cluster-wide — see the
|
|
3812
|
-
* `DeviceLinkGlobalSource` docblock). Null when absent. Pure over a
|
|
3813
|
-
* pre-read meta map so a multi-link resolve reads the store once. */
|
|
3814
|
-
resolveGlobalSourceDeviceId = (sourceStableId, meta) => {
|
|
3815
|
-
for (const m of Object.values(meta)) if (m.stableId === sourceStableId) return m.id;
|
|
3816
|
-
return null;
|
|
3817
|
-
};
|
|
3818
3571
|
allocateNextDeviceId = async () => {
|
|
3819
3572
|
const current = (await this.readStore()).nextDeviceId ?? 1;
|
|
3820
3573
|
await this.settings.writeAddonStore({ nextDeviceId: current + 1 });
|
|
3821
3574
|
return current;
|
|
3822
3575
|
};
|
|
3823
|
-
/** Rebuild the `linkTargets` / `linkDependents` reverse-index maps from the
|
|
3824
|
-
* current persisted meta. Called at boot (once the `devicesWithLinks` seed
|
|
3825
|
-
* has run) and whenever the link topology can change: `setDeviceLinks`,
|
|
3826
|
-
* `registerDevice` (a new sibling source may resolve previously-dangling
|
|
3827
|
-
* links), and `removeDevice` (its entries must be evicted). */
|
|
3828
|
-
rebuildLinkDependents = async () => {
|
|
3829
|
-
if (this.linkHost.devicesWithLinks.size === 0) {
|
|
3830
|
-
this.linkHost.setLinkIndexes(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), /* @__PURE__ */ new Set());
|
|
3831
|
-
return;
|
|
3832
|
-
}
|
|
3833
|
-
const allMeta = await this.readMeta();
|
|
3834
|
-
const stableIdById = /* @__PURE__ */ new Map();
|
|
3835
|
-
const idByStableId = /* @__PURE__ */ new Map();
|
|
3836
|
-
for (const m of Object.values(allMeta)) {
|
|
3837
|
-
stableIdById.set(m.id, m.stableId);
|
|
3838
|
-
idByStableId.set(m.stableId, m.id);
|
|
3839
|
-
}
|
|
3840
|
-
const entries = [];
|
|
3841
|
-
const expectedSources = /* @__PURE__ */ new Set();
|
|
3842
|
-
for (const targetId of this.linkHost.devicesWithLinks) {
|
|
3843
|
-
const targetMeta = Object.values(allMeta).find((m) => m.id === targetId);
|
|
3844
|
-
if (!targetMeta) continue;
|
|
3845
|
-
const container = targetMeta.parentDeviceId !== null ? stableIdById.get(targetMeta.parentDeviceId) ?? stableIdById.get(targetId) : stableIdById.get(targetId);
|
|
3846
|
-
if (container === void 0) continue;
|
|
3847
|
-
for (const link of targetMeta.deviceLinks ?? []) {
|
|
3848
|
-
const src = link.source;
|
|
3849
|
-
if (src.kind === "expression") {
|
|
3850
|
-
const bindingSourceIds = {};
|
|
3851
|
-
for (const [name, b] of Object.entries(src.bindings)) {
|
|
3852
|
-
if (b.kind === "literal") continue;
|
|
3853
|
-
const wanted = b.kind === "global" ? b.sourceStableId : `${container}-${b.sourceKey}`;
|
|
3854
|
-
expectedSources.add(wanted);
|
|
3855
|
-
const bid = idByStableId.get(wanted);
|
|
3856
|
-
if (bid !== void 0) bindingSourceIds[name] = bid;
|
|
3857
|
-
}
|
|
3858
|
-
entries.push({
|
|
3859
|
-
targetDeviceId: targetId,
|
|
3860
|
-
link,
|
|
3861
|
-
sourceDeviceId: -1,
|
|
3862
|
-
bindingSourceIds
|
|
3863
|
-
});
|
|
3864
|
-
continue;
|
|
3865
|
-
}
|
|
3866
|
-
if (src.kind === "literal") {
|
|
3867
|
-
entries.push({
|
|
3868
|
-
targetDeviceId: targetId,
|
|
3869
|
-
link,
|
|
3870
|
-
sourceDeviceId: -1
|
|
3871
|
-
});
|
|
3872
|
-
continue;
|
|
3873
|
-
}
|
|
3874
|
-
const wantedStableId = src.kind === "global" ? src.sourceStableId : `${container}-${src.sourceKey}`;
|
|
3875
|
-
expectedSources.add(wantedStableId);
|
|
3876
|
-
const srcId = idByStableId.get(wantedStableId);
|
|
3877
|
-
if (srcId === void 0) continue;
|
|
3878
|
-
entries.push({
|
|
3879
|
-
targetDeviceId: targetId,
|
|
3880
|
-
link,
|
|
3881
|
-
sourceDeviceId: srcId
|
|
3882
|
-
});
|
|
3883
|
-
}
|
|
3884
|
-
}
|
|
3885
|
-
const { targets, dependents } = buildLinkIndexes(entries);
|
|
3886
|
-
this.linkHost.setLinkIndexes(targets, dependents, expectedSources);
|
|
3887
|
-
};
|
|
3888
3576
|
};
|
|
3889
3577
|
//#endregion
|
|
3890
|
-
//#region src/builtins/device-manager/device-link-resolver.ts
|
|
3891
|
-
/** Returns true when `x` is a non-null, non-array plain object. */
|
|
3892
|
-
function isRecord(x) {
|
|
3893
|
-
return x !== null && typeof x === "object" && !Array.isArray(x);
|
|
3894
|
-
}
|
|
3895
|
-
/**
|
|
3896
|
-
* Resolve one link's transformed value. Field sources read via `readField`
|
|
3897
|
-
* (the caller supplies the sibling lookup — sync, e.g. the in-hub state
|
|
3898
|
-
* mirror); literal sources use their per-device constant and never consult
|
|
3899
|
-
* the reader. Returns `undefined` to skip the overlay (unreadable field).
|
|
3900
|
-
* Pure. Async source reads (`resolveLinkedStatus`'s provider `getStatus`
|
|
3901
|
-
* path) cannot use this helper for field sources — they branch on
|
|
3902
|
-
* `source.kind` directly.
|
|
3903
|
-
*/
|
|
3904
|
-
function resolveLinkValue(link, readField) {
|
|
3905
|
-
const src = link.source;
|
|
3906
|
-
if (src.kind === "expression") return void 0;
|
|
3907
|
-
return applyTransform(src.kind === "literal" ? src.value : readField(src.cap, src.fieldPath), link.transform);
|
|
3908
|
-
}
|
|
3909
|
-
/**
|
|
3910
|
-
* Sync-channel expression resolution: bindings read from the mirror via
|
|
3911
|
-
* `readField(deviceId, cap, fieldPath)` using the pre-resolved per-binding ids
|
|
3912
|
-
* on the `ResolvedTargetLink`. A literal binding uses its constant; an absent
|
|
3913
|
-
* binding id (dangling source) or a non-primitive read resolves to `null`.
|
|
3914
|
-
* Returns `undefined` on parse/eval failure (skip overlay) — silent by design:
|
|
3915
|
-
* the mirror path is hot and churn-free; the async channel logs. Compile
|
|
3916
|
-
* failures cost one parse ever thanks to the negative LRU compile cache. Pure.
|
|
3917
|
-
*/
|
|
3918
|
-
function resolveExpressionLinkValue(rl, readField, now) {
|
|
3919
|
-
const src = rl.link.source;
|
|
3920
|
-
if (src.kind !== "expression") return void 0;
|
|
3921
|
-
const bindingValues = {};
|
|
3922
|
-
for (const [name, b] of Object.entries(src.bindings)) {
|
|
3923
|
-
if (b.kind === "literal") {
|
|
3924
|
-
bindingValues[name] = b.value;
|
|
3925
|
-
continue;
|
|
3926
|
-
}
|
|
3927
|
-
const bId = rl.bindingSourceIds?.[name];
|
|
3928
|
-
if (bId === void 0) {
|
|
3929
|
-
bindingValues[name] = null;
|
|
3930
|
-
continue;
|
|
3931
|
-
}
|
|
3932
|
-
bindingValues[name] = toExpressionValue(readField(bId, b.cap, b.fieldPath)) ?? null;
|
|
3933
|
-
}
|
|
3934
|
-
const result = evaluateLinkExpression(src.expr, bindingValues, now);
|
|
3935
|
-
return result.ok ? applyTransform(result.value, rl.link.transform) : void 0;
|
|
3936
|
-
}
|
|
3937
|
-
/** Narrow Zod v4's structural `$ZodType` (returned by `.unwrap()`) back to the
|
|
3938
|
-
* concrete classic `z.ZodType`. Every runtime schema is a `z.ZodType`, so this
|
|
3939
|
-
* is a true `instanceof` guard rather than a cast. */
|
|
3940
|
-
function asZodType(schema) {
|
|
3941
|
-
return schema instanceof z.ZodType ? schema : null;
|
|
3942
|
-
}
|
|
3943
|
-
/** Unwrap ZodNullable / ZodOptional / ZodDefault wrappers to reach the inner
|
|
3944
|
-
* type. This lets the repair logic recognise a `TankStatus.nullable()` field
|
|
3945
|
-
* as a ZodObject so it can fill in missing nullable keys. */
|
|
3946
|
-
function unwrapSchema(schema) {
|
|
3947
|
-
if (schema instanceof z.ZodNullable || schema instanceof z.ZodOptional || schema instanceof z.ZodDefault) {
|
|
3948
|
-
const inner = asZodType(schema.unwrap());
|
|
3949
|
-
return inner ? unwrapSchema(inner) : schema;
|
|
3950
|
-
}
|
|
3951
|
-
return schema;
|
|
3952
|
-
}
|
|
3953
|
-
/** For a ZodObject schema, ensure every nullable key present under `value` is
|
|
3954
|
-
* filled — a missing key whose field accepts `null` is set to `null`. Generic
|
|
3955
|
-
* repair for the common "set one leaf of a previously-null structured field"
|
|
3956
|
-
* case (e.g. TankStatus.level). Recurses into nested object fields. */
|
|
3957
|
-
function fillNullableDefaults(schema, value) {
|
|
3958
|
-
const inner = unwrapSchema(schema);
|
|
3959
|
-
if (!(inner instanceof z.ZodObject) || !isRecord(value)) return value;
|
|
3960
|
-
const shape = inner.shape;
|
|
3961
|
-
const out = { ...value };
|
|
3962
|
-
for (const [key, field] of Object.entries(shape)) if (out[key] === void 0) {
|
|
3963
|
-
if (field.safeParse(null).success) out[key] = null;
|
|
3964
|
-
} else out[key] = fillNullableDefaults(field, out[key]);
|
|
3965
|
-
return out;
|
|
3966
|
-
}
|
|
3967
|
-
/**
|
|
3968
|
-
* Group item-targeted links (`target.itemKey` set) by itemKey and upsert each
|
|
3969
|
-
* group as ONE item into the status' item array (see
|
|
3970
|
-
* `CapabilityStatusItemArray`). A NEW item is seeded from `emptyItem` with
|
|
3971
|
-
* `keyField` (and `labelField`, when declared) set to the itemKey; an
|
|
3972
|
-
* EXISTING item (matched by `keyField`) is cloned and overlaid in place, so
|
|
3973
|
-
* native fields a link doesn't touch are preserved. Each upserted item is
|
|
3974
|
-
* validated against `itemSchema` — an invalid item is skipped WITHOUT
|
|
3975
|
-
* discarding its valid siblings. Returns the new draft and whether anything
|
|
3976
|
-
* was applied. Pure + immutable (base array/items are never mutated).
|
|
3977
|
-
*/
|
|
3978
|
-
function upsertItemArrayLinks(draft, grouped, itemArray) {
|
|
3979
|
-
const arrRaw = getByPath(draft, itemArray.path);
|
|
3980
|
-
const arr = Array.isArray(arrRaw) ? [...arrRaw] : [];
|
|
3981
|
-
let touched = false;
|
|
3982
|
-
for (const [itemKey, group] of grouped) {
|
|
3983
|
-
const idx = arr.findIndex((el) => isRecord(el) && el[itemArray.keyField] === itemKey);
|
|
3984
|
-
const existing = idx >= 0 ? arr[idx] : null;
|
|
3985
|
-
let item = isRecord(existing) ? { ...existing } : {
|
|
3986
|
-
...itemArray.emptyItem,
|
|
3987
|
-
[itemArray.keyField]: itemKey,
|
|
3988
|
-
...itemArray.labelField !== void 0 ? { [itemArray.labelField]: itemKey } : {}
|
|
3989
|
-
};
|
|
3990
|
-
let itemTouched = false;
|
|
3991
|
-
for (const { link, sourceValue } of group) {
|
|
3992
|
-
if (sourceValue === void 0) continue;
|
|
3993
|
-
item = setByPath(item, link.target.fieldPath, sourceValue);
|
|
3994
|
-
itemTouched = true;
|
|
3995
|
-
}
|
|
3996
|
-
if (!itemTouched) continue;
|
|
3997
|
-
const repaired = fillNullableDefaults(itemArray.itemSchema, item);
|
|
3998
|
-
const parsed = itemArray.itemSchema.safeParse(repaired);
|
|
3999
|
-
if (!parsed.success) continue;
|
|
4000
|
-
if (isRecord(parsed.data)) item = parsed.data;
|
|
4001
|
-
if (idx >= 0) arr[idx] = item;
|
|
4002
|
-
else arr.push(item);
|
|
4003
|
-
touched = true;
|
|
4004
|
-
}
|
|
4005
|
-
if (!touched) return {
|
|
4006
|
-
draft,
|
|
4007
|
-
touched: false
|
|
4008
|
-
};
|
|
4009
|
-
return {
|
|
4010
|
-
draft: setByPath(draft, itemArray.path, arr),
|
|
4011
|
-
touched: true
|
|
4012
|
-
};
|
|
4013
|
-
}
|
|
4014
|
-
/**
|
|
4015
|
-
* Overlay transformed source values onto `base` by dot-path, then validate the
|
|
4016
|
-
* result against the target cap's `statusSchema`. On validation failure the
|
|
4017
|
-
* overlay is discarded and `base` is returned unchanged (a misconfigured link
|
|
4018
|
-
* must never corrupt a cap response). Pure — all I/O happens in the caller.
|
|
4019
|
-
*
|
|
4020
|
-
* Item-array grouping (P2b): when the cap declares `status.itemArray`, links
|
|
4021
|
-
* carrying a `target.itemKey` are grouped per key and upserted as items into
|
|
4022
|
-
* the array (see `upsertItemArrayLinks`); their `fieldPath` is relative to
|
|
4023
|
-
* ONE item ('level', 'status', 'label'). Links WITHOUT an itemKey keep the
|
|
4024
|
-
* scalar dot-path behavior unchanged. A link with an itemKey on a cap that
|
|
4025
|
-
* declares NO `itemArray` is dropped (its item-relative path must never be
|
|
4026
|
-
* scalar-applied to the status root).
|
|
4027
|
-
*/
|
|
4028
|
-
function mergeLinkedStatus(base, resolved, statusSchema, itemArray) {
|
|
4029
|
-
let draft = base;
|
|
4030
|
-
let touched = false;
|
|
4031
|
-
const grouped = /* @__PURE__ */ new Map();
|
|
4032
|
-
for (const entry of resolved) {
|
|
4033
|
-
const itemKey = entry.link.target.itemKey;
|
|
4034
|
-
if (itemKey !== void 0) {
|
|
4035
|
-
if (!itemArray) continue;
|
|
4036
|
-
const group = grouped.get(itemKey);
|
|
4037
|
-
if (group) group.push(entry);
|
|
4038
|
-
else grouped.set(itemKey, [entry]);
|
|
4039
|
-
continue;
|
|
4040
|
-
}
|
|
4041
|
-
if (entry.sourceValue === void 0) continue;
|
|
4042
|
-
draft = setByPath(draft, entry.link.target.fieldPath, entry.sourceValue);
|
|
4043
|
-
touched = true;
|
|
4044
|
-
}
|
|
4045
|
-
if (itemArray && grouped.size > 0) {
|
|
4046
|
-
const upserted = upsertItemArrayLinks(draft, grouped, itemArray);
|
|
4047
|
-
draft = upserted.draft;
|
|
4048
|
-
touched = touched || upserted.touched;
|
|
4049
|
-
}
|
|
4050
|
-
if (!touched) return base;
|
|
4051
|
-
if (!statusSchema) return draft;
|
|
4052
|
-
const repaired = fillNullableDefaults(statusSchema, draft);
|
|
4053
|
-
const parsed = statusSchema.safeParse(repaired);
|
|
4054
|
-
if (!parsed.success) return base;
|
|
4055
|
-
return isRecord(parsed.data) ? parsed.data : base;
|
|
4056
|
-
}
|
|
4057
|
-
//#endregion
|
|
4058
3578
|
//#region src/builtins/device-manager/device-state-mirror.ts
|
|
4059
3579
|
/**
|
|
4060
3580
|
* Hub-side runtime-state mirror for the device-manager addon.
|
|
4061
3581
|
*
|
|
4062
3582
|
* `DeviceStateMirror` owns the per-device cap-keyed slice mirror and the
|
|
4063
|
-
* debounced disk-write coalescer
|
|
4064
|
-
*
|
|
4065
|
-
* `
|
|
4066
|
-
*
|
|
4067
|
-
*
|
|
4068
|
-
*
|
|
4069
|
-
*
|
|
3583
|
+
* debounced disk-write coalescer. It mirrors every `deviceState.setCapSlice`
|
|
3584
|
+
* write, emits `DeviceStateChanged`, and coalesces frequent writes into one
|
|
3585
|
+
* `writeDeviceRuntimeState` per debounce window.
|
|
3586
|
+
*
|
|
3587
|
+
* It used to do one more thing: overlay each emitted slice with cross-device
|
|
3588
|
+
* LINKED values, walk a `linkDependents` reverse-index and re-emit for every
|
|
3589
|
+
* dependent target, guarded by a `lastEmittedOverlay` churn map. Wiring was
|
|
3590
|
+
* deleted on 2026-08-08 (0 links across 302 devices), so the mirror emits what
|
|
3591
|
+
* the provider wrote — one writer per field, which is what D62 asks for
|
|
3592
|
+
* everywhere else.
|
|
4070
3593
|
*/
|
|
4071
3594
|
var DeviceStateMirror = class DeviceStateMirror {
|
|
4072
3595
|
ctx;
|
|
4073
|
-
linkHost;
|
|
4074
3596
|
/**
|
|
4075
3597
|
* Hub-side mirror of every device's cap-keyed runtime state.
|
|
4076
3598
|
* Key: deviceId. Value: per-cap slice map. Empty by default —
|
|
@@ -4084,11 +3606,8 @@ var DeviceStateMirror = class DeviceStateMirror {
|
|
|
4084
3606
|
*/
|
|
4085
3607
|
runtimeStateDebounce = /* @__PURE__ */ new Map();
|
|
4086
3608
|
static RUNTIME_STATE_DEBOUNCE_MS = 1e3;
|
|
4087
|
-
|
|
4088
|
-
lastEmittedOverlay = /* @__PURE__ */ new Map();
|
|
4089
|
-
constructor(ctx, linkHost) {
|
|
3609
|
+
constructor(ctx) {
|
|
4090
3610
|
this.ctx = ctx;
|
|
4091
|
-
this.linkHost = linkHost;
|
|
4092
3611
|
}
|
|
4093
3612
|
/**
|
|
4094
3613
|
* Single-cap mirror update — diff against the current mirror,
|
|
@@ -4105,9 +3624,7 @@ var DeviceStateMirror = class DeviceStateMirror {
|
|
|
4105
3624
|
const prior = perCap.get(capName);
|
|
4106
3625
|
if (prior && shallowEqual(prior, slice)) return;
|
|
4107
3626
|
perCap.set(capName, { ...slice });
|
|
4108
|
-
this.
|
|
4109
|
-
const deps = this.linkHost.linkDependents.get(`${deviceId}:${capName}`);
|
|
4110
|
-
if (deps) for (const d of deps) this.emitOverlayed(d.targetDeviceId, d.targetCap);
|
|
3627
|
+
this.emitStateChanged(deviceId, capName, { ...slice });
|
|
4111
3628
|
}
|
|
4112
3629
|
/**
|
|
4113
3630
|
* Debounced disk writer. Coalesces frequent writes (motion phase
|
|
@@ -4239,68 +3756,17 @@ var DeviceStateMirror = class DeviceStateMirror {
|
|
|
4239
3756
|
for (const [k, v] of perCap) out[k] = { ...v };
|
|
4240
3757
|
return out;
|
|
4241
3758
|
}
|
|
4242
|
-
/**
|
|
4243
|
-
*
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
* target, the slice is SYNTHESIZED: the merge base is seeded from the
|
|
4248
|
-
* cap's declared `status.empty` default; a failed/empty synthesize
|
|
4249
|
-
* returns null (no phantom slices). The disk writer must NOT use this
|
|
4250
|
-
* method; it must persist raw provider truth via snapshotForDevice.
|
|
4251
|
-
*/
|
|
4252
|
-
overlayedSlice(deviceId, cap) {
|
|
4253
|
-
const raw = this.stateMirror.get(deviceId)?.get(cap) ?? null;
|
|
4254
|
-
const links = this.linkHost.linkTargets.get(`${deviceId}:${cap}`);
|
|
4255
|
-
if (!links || links.length === 0) return raw ? { ...raw } : null;
|
|
4256
|
-
const now = Date.now();
|
|
4257
|
-
const resolved = links.map((rl) => ({
|
|
4258
|
-
link: rl.link,
|
|
4259
|
-
sourceValue: rl.link.source.kind === "expression" ? resolveExpressionLinkValue(rl, (srcDeviceId, srcCap, fieldPath) => getByPath(this.stateMirror.get(srcDeviceId)?.get(srcCap), fieldPath), now) : resolveLinkValue(rl.link, (srcCap, fieldPath) => getByPath(this.stateMirror.get(rl.sourceDeviceId)?.get(srcCap), fieldPath))
|
|
4260
|
-
}));
|
|
4261
|
-
const capStatus = this.linkHost.capabilityRegistry?.getDefinition(cap)?.status;
|
|
4262
|
-
const schema = capStatus?.schema;
|
|
4263
|
-
const empty = capStatus?.empty;
|
|
4264
|
-
const base = raw ? { ...raw } : empty !== null && typeof empty === "object" && !Array.isArray(empty) ? { ...empty } : {};
|
|
4265
|
-
const merged = mergeLinkedStatus(base, resolved, schema, capStatus?.itemArray);
|
|
4266
|
-
if (!raw && merged === base) return null;
|
|
4267
|
-
return merged;
|
|
3759
|
+
/** One cap's mirrored slice, cloned. Null when the device has never written
|
|
3760
|
+
* it — callers read that as "the cap has no state", never as an empty slice. */
|
|
3761
|
+
capSlice(deviceId, cap) {
|
|
3762
|
+
const raw = this.stateMirror.get(deviceId)?.get(cap);
|
|
3763
|
+
return raw ? { ...raw } : null;
|
|
4268
3764
|
}
|
|
4269
|
-
/**
|
|
4270
|
-
*
|
|
4271
|
-
|
|
4272
|
-
* getAllSnapshots) so callers see overlayed values. SYNTHESIZE-only
|
|
4273
|
-
* link-target caps (no base mirror slice) are unioned into the iterated
|
|
4274
|
-
* key set so they appear at warm-load — `overlayedSlice` builds them from
|
|
4275
|
-
* the cap's `status.empty` (and drops them when nothing resolves). The
|
|
4276
|
-
* debounced disk writer must continue to call snapshotForDevice (raw truth).
|
|
4277
|
-
*/
|
|
4278
|
-
snapshotForDeviceOverlayed(deviceId) {
|
|
4279
|
-
const perCap = this.stateMirror.get(deviceId);
|
|
4280
|
-
const capNames = new Set(perCap ? perCap.keys() : []);
|
|
4281
|
-
const prefix = `${deviceId}:`;
|
|
4282
|
-
for (const key of this.linkHost.linkTargets.keys()) if (key.startsWith(prefix)) capNames.add(key.slice(prefix.length));
|
|
3765
|
+
/** Whole-system mirror dump. Backs `deviceState.getAllSnapshots` — one
|
|
3766
|
+
* round-trip for a warm boot instead of N per-device calls. */
|
|
3767
|
+
allSnapshots() {
|
|
4283
3768
|
const out = {};
|
|
4284
|
-
for (const
|
|
4285
|
-
const s = this.overlayedSlice(deviceId, capName);
|
|
4286
|
-
if (s) out[capName] = s;
|
|
4287
|
-
}
|
|
4288
|
-
return out;
|
|
4289
|
-
}
|
|
4290
|
-
/** Whole-system mirror dump (overlaid). Backs `deviceState.getAllSnapshots`.
|
|
4291
|
-
* Devices that only exist as link TARGETS (no mirror slice yet) are
|
|
4292
|
-
* included via the link reverse-index so their synthesized caps appear. */
|
|
4293
|
-
allSnapshotsOverlayed() {
|
|
4294
|
-
const deviceIds = new Set(this.stateMirror.keys());
|
|
4295
|
-
for (const key of this.linkHost.linkTargets.keys()) {
|
|
4296
|
-
const id = Number(key.slice(0, key.indexOf(":")));
|
|
4297
|
-
if (Number.isFinite(id)) deviceIds.add(id);
|
|
4298
|
-
}
|
|
4299
|
-
const out = {};
|
|
4300
|
-
for (const deviceId of deviceIds) {
|
|
4301
|
-
const dev = this.snapshotForDeviceOverlayed(deviceId);
|
|
4302
|
-
if (this.stateMirror.has(deviceId) || Object.keys(dev).length > 0) out[String(deviceId)] = dev;
|
|
4303
|
-
}
|
|
3769
|
+
for (const deviceId of this.stateMirror.keys()) out[String(deviceId)] = this.snapshotForDevice(deviceId);
|
|
4304
3770
|
return out;
|
|
4305
3771
|
}
|
|
4306
3772
|
emitStateChanged(deviceId, capName, slice) {
|
|
@@ -4319,24 +3785,6 @@ var DeviceStateMirror = class DeviceStateMirror {
|
|
|
4319
3785
|
}
|
|
4320
3786
|
});
|
|
4321
3787
|
}
|
|
4322
|
-
/** Emit DeviceStateChanged for (deviceId, cap) using the OVERLAID slice,
|
|
4323
|
-
* skipping when the overlay is unchanged since the last emit (loop/churn
|
|
4324
|
-
* guard). Used for the written pair AND its dependent targets. */
|
|
4325
|
-
emitOverlayed(deviceId, cap) {
|
|
4326
|
-
const slice = this.overlayedSlice(deviceId, cap);
|
|
4327
|
-
if (!slice) return;
|
|
4328
|
-
const key = `${deviceId}:${cap}`;
|
|
4329
|
-
const prev = this.lastEmittedOverlay.get(key);
|
|
4330
|
-
if (prev && shallowEqual(prev, slice)) return;
|
|
4331
|
-
this.lastEmittedOverlay.set(key, slice);
|
|
4332
|
-
this.emitStateChanged(deviceId, cap, slice);
|
|
4333
|
-
}
|
|
4334
|
-
/** Drop a removed device's overlay-emit guard entries (keyed
|
|
4335
|
-
* `${deviceId}:${cap}`) so the map doesn't retain rows for a removed device.
|
|
4336
|
-
* Called from `removeDevice`. */
|
|
4337
|
-
dropDeviceOverlays(deviceId) {
|
|
4338
|
-
for (const overlayKey of this.lastEmittedOverlay.keys()) if (overlayKey.startsWith(`${deviceId}:`)) this.lastEmittedOverlay.delete(overlayKey);
|
|
4339
|
-
}
|
|
4340
3788
|
/** Flush every pending debounced disk write (graceful shutdown). Clears the
|
|
4341
3789
|
* debounce slots after awaiting in-flight + scheduled writes so shutdown is
|
|
4342
3790
|
* lossless. */
|
|
@@ -4365,11 +3813,15 @@ var DeviceStateMirror = class DeviceStateMirror {
|
|
|
4365
3813
|
//#endregion
|
|
4366
3814
|
//#region src/builtins/device-manager/device-sync-resolvers.ts
|
|
4367
3815
|
/**
|
|
4368
|
-
* Synchronous ownership / native-cap resolvers
|
|
4369
|
-
*
|
|
4370
|
-
*
|
|
4371
|
-
*
|
|
4372
|
-
* the
|
|
3816
|
+
* Synchronous ownership / native-cap resolvers for the device-manager provider.
|
|
3817
|
+
* Free functions over a `ProviderContext` (DI); these back
|
|
3818
|
+
* `CapabilityRegistry`'s native fallback.
|
|
3819
|
+
*
|
|
3820
|
+
* This file also held `resolveLinkedStatus`, the read-time cross-device link
|
|
3821
|
+
* overlay. Wiring was deleted on 2026-08-08 — 0 links across 302 devices, a
|
|
3822
|
+
* month after the tab shipped — so the overlay, its re-entrancy guard and its
|
|
3823
|
+
* per-call source-read memo went with it. A cross-device derivation now
|
|
3824
|
+
* produces a DEVICE, not a field on somebody else's cap.
|
|
4373
3825
|
*/
|
|
4374
3826
|
/**
|
|
4375
3827
|
* Sync ownership lookup backing persistence fallbacks (e.g. remove()
|
|
@@ -4411,133 +3863,6 @@ function resolveNativeCapOwnerSync(pctx, capName, deviceId) {
|
|
|
4411
3863
|
};
|
|
4412
3864
|
return resolveRemoteNativeCapFromRegistry(pctx.bindingsDeps, capName, deviceId);
|
|
4413
3865
|
}
|
|
4414
|
-
/**
|
|
4415
|
-
* Read-time overlay: merge cross-device linked values onto a target cap's
|
|
4416
|
-
* status. Returns null when the device has no links for `cap` (caller uses
|
|
4417
|
-
* the base status untouched). Reads each source cap via the hub registry's
|
|
4418
|
-
* `getProviderForDevice` (routes cross-process); merge is pure.
|
|
4419
|
-
*/
|
|
4420
|
-
async function resolveLinkedStatus(pctx, input) {
|
|
4421
|
-
const { deviceId, cap } = input;
|
|
4422
|
-
if (!pctx.host.devicesWithLinks.has(deviceId)) return null;
|
|
4423
|
-
const inFlightKey = `${deviceId}:${cap}`;
|
|
4424
|
-
if (pctx.host.linkResolveInFlight.has(inFlightKey)) return null;
|
|
4425
|
-
pctx.host.linkResolveInFlight.add(inFlightKey);
|
|
4426
|
-
try {
|
|
4427
|
-
return await resolveLinkedStatusInner(pctx, input);
|
|
4428
|
-
} finally {
|
|
4429
|
-
pctx.host.linkResolveInFlight.delete(inFlightKey);
|
|
4430
|
-
}
|
|
4431
|
-
}
|
|
4432
|
-
/** Body of `resolveLinkedStatus` — see the wrapper for the re-entrancy guard. */
|
|
4433
|
-
async function resolveLinkedStatusInner(pctx, input) {
|
|
4434
|
-
const { deviceId, cap, baseStatus } = input;
|
|
4435
|
-
const persisted = await pctx.metaStore.resolvePersistedById(deviceId);
|
|
4436
|
-
if (!persisted) return null;
|
|
4437
|
-
const links = (persisted.meta.deviceLinks ?? []).filter((l) => l.target.cap === cap);
|
|
4438
|
-
if (links.length === 0) return null;
|
|
4439
|
-
const capRegistry = pctx.host.capabilityRegistry;
|
|
4440
|
-
const capStatus = capRegistry?.getDefinition(cap)?.status;
|
|
4441
|
-
const schema = capStatus?.schema;
|
|
4442
|
-
if (!schema) return null;
|
|
4443
|
-
const allMeta = await pctx.metaStore.readMeta();
|
|
4444
|
-
let containerStableId = persisted.stableId;
|
|
4445
|
-
const parentId = persisted.meta.parentDeviceId;
|
|
4446
|
-
if (parentId !== null) {
|
|
4447
|
-
const parentMeta = allMeta[String(parentId)];
|
|
4448
|
-
if (parentMeta) containerStableId = parentMeta.stableId;
|
|
4449
|
-
}
|
|
4450
|
-
const now = Date.now();
|
|
4451
|
-
const statusMemo = /* @__PURE__ */ new Map();
|
|
4452
|
-
const readSourceStatus = async (srcId, srcCap) => {
|
|
4453
|
-
const key = `${srcId}:${srcCap}`;
|
|
4454
|
-
const cached = statusMemo.get(key);
|
|
4455
|
-
if (cached !== void 0) return cached;
|
|
4456
|
-
let result;
|
|
4457
|
-
try {
|
|
4458
|
-
const srcProvider = capRegistry?.getProviderForDevice(srcCap, srcId);
|
|
4459
|
-
result = {
|
|
4460
|
-
status: typeof srcProvider?.getStatus === "function" ? await srcProvider.getStatus({ deviceId: srcId }) : void 0,
|
|
4461
|
-
ok: true
|
|
4462
|
-
};
|
|
4463
|
-
} catch (err) {
|
|
4464
|
-
pctx.host.ctx.logger.warn("resolveLinkedStatus: source read failed", {
|
|
4465
|
-
tags: {
|
|
4466
|
-
deviceId,
|
|
4467
|
-
capName: cap
|
|
4468
|
-
},
|
|
4469
|
-
meta: {
|
|
4470
|
-
sourceCap: srcCap,
|
|
4471
|
-
sourceId: srcId,
|
|
4472
|
-
error: err instanceof Error ? err.message : String(err)
|
|
4473
|
-
}
|
|
4474
|
-
});
|
|
4475
|
-
result = {
|
|
4476
|
-
status: void 0,
|
|
4477
|
-
ok: false
|
|
4478
|
-
};
|
|
4479
|
-
}
|
|
4480
|
-
statusMemo.set(key, result);
|
|
4481
|
-
return result;
|
|
4482
|
-
};
|
|
4483
|
-
const resolved = [];
|
|
4484
|
-
for (const link of links) {
|
|
4485
|
-
const src = link.source;
|
|
4486
|
-
if (src.kind === "literal") {
|
|
4487
|
-
resolved.push({
|
|
4488
|
-
link,
|
|
4489
|
-
sourceValue: applyTransform(src.value, link.transform)
|
|
4490
|
-
});
|
|
4491
|
-
continue;
|
|
4492
|
-
}
|
|
4493
|
-
if (src.kind === "expression") {
|
|
4494
|
-
const bindingValues = {};
|
|
4495
|
-
for (const [name, b] of Object.entries(src.bindings)) {
|
|
4496
|
-
if (b.kind === "literal") {
|
|
4497
|
-
bindingValues[name] = b.value;
|
|
4498
|
-
continue;
|
|
4499
|
-
}
|
|
4500
|
-
const bId = b.kind === "global" ? pctx.metaStore.resolveGlobalSourceDeviceId(b.sourceStableId, allMeta) : pctx.metaStore.resolveSourceDeviceId(containerStableId, b.sourceKey, allMeta);
|
|
4501
|
-
if (bId === null) {
|
|
4502
|
-
bindingValues[name] = null;
|
|
4503
|
-
continue;
|
|
4504
|
-
}
|
|
4505
|
-
bindingValues[name] = toExpressionValue(getByPath((await readSourceStatus(bId, b.cap)).status, b.fieldPath)) ?? null;
|
|
4506
|
-
}
|
|
4507
|
-
const result = evaluateLinkExpression(src.expr, bindingValues, now);
|
|
4508
|
-
if (result.ok) resolved.push({
|
|
4509
|
-
link,
|
|
4510
|
-
sourceValue: applyTransform(result.value, link.transform)
|
|
4511
|
-
});
|
|
4512
|
-
else pctx.host.ctx.logger.warn("resolveLinkedStatus: expression skipped", {
|
|
4513
|
-
tags: {
|
|
4514
|
-
deviceId,
|
|
4515
|
-
capName: cap
|
|
4516
|
-
},
|
|
4517
|
-
meta: {
|
|
4518
|
-
linkId: link.id,
|
|
4519
|
-
error: result.error
|
|
4520
|
-
}
|
|
4521
|
-
});
|
|
4522
|
-
continue;
|
|
4523
|
-
}
|
|
4524
|
-
const srcId = src.kind === "global" ? pctx.metaStore.resolveGlobalSourceDeviceId(src.sourceStableId, allMeta) : pctx.metaStore.resolveSourceDeviceId(containerStableId, src.sourceKey, allMeta);
|
|
4525
|
-
if (srcId === null) continue;
|
|
4526
|
-
const read = await readSourceStatus(srcId, src.cap);
|
|
4527
|
-
if (!read.ok) continue;
|
|
4528
|
-
const raw = getByPath(read.status, src.fieldPath);
|
|
4529
|
-
resolved.push({
|
|
4530
|
-
link,
|
|
4531
|
-
sourceValue: applyTransform(raw, link.transform)
|
|
4532
|
-
});
|
|
4533
|
-
}
|
|
4534
|
-
if (resolved.length === 0) return null;
|
|
4535
|
-
const synthesizing = !isRecord$1(baseStatus);
|
|
4536
|
-
const base = isRecord$1(baseStatus) ? baseStatus : isRecord$1(capStatus.empty) ? { ...capStatus.empty } : {};
|
|
4537
|
-
const merged = mergeLinkedStatus(base, resolved, schema, capStatus.itemArray);
|
|
4538
|
-
if (synthesizing && merged === base) return null;
|
|
4539
|
-
return merged;
|
|
4540
|
-
}
|
|
4541
3866
|
//#endregion
|
|
4542
3867
|
//#region src/builtins/device-manager/device-manager.addon.ts
|
|
4543
3868
|
var DeviceManagerAddon = class extends BaseAddon {
|
|
@@ -4590,100 +3915,24 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
4590
3915
|
* the D3 re-handshake after device restore is the reliable replacement.
|
|
4591
3916
|
*/
|
|
4592
3917
|
remoteNativeCaps = /* @__PURE__ */ new Map();
|
|
4593
|
-
/** Device ids that currently have at least one `deviceLinks` entry. O(1) gate
|
|
4594
|
-
* consulted by `resolveLinkedStatus` so the cap-mount overlay is a no-op for
|
|
4595
|
-
* the vast majority of devices that have no links. Rebuilt on restore. */
|
|
4596
|
-
devicesWithLinks = /* @__PURE__ */ new Set();
|
|
4597
|
-
/** Whether a device currently has cross-device links. */
|
|
4598
|
-
devicesWithLinksHas(deviceId) {
|
|
4599
|
-
return this.devicesWithLinks.has(deviceId);
|
|
4600
|
-
}
|
|
4601
|
-
/** `${targetDeviceId}:${targetCap}` → resolved links feeding that cap's slice. */
|
|
4602
|
-
linkTargets = /* @__PURE__ */ new Map();
|
|
4603
|
-
/** `${sourceDeviceId}:${sourceCap}` → targets to recompute when that source changes. */
|
|
4604
|
-
linkDependents = /* @__PURE__ */ new Map();
|
|
4605
|
-
/** Expected source `stableId`s (`${container}-${sourceKey}` for sibling
|
|
4606
|
-
* sources, the full `sourceStableId` for global sources) across all links,
|
|
4607
|
-
* resolved or not — gates the `registerDevice` rebuild so only a registering
|
|
4608
|
-
* device that IS a link source triggers a reindex (not every boot restore). */
|
|
4609
|
-
expectedSourceStableIds = /* @__PURE__ */ new Set();
|
|
4610
|
-
/** In-flight `${deviceId}:${cap}` pairs of `resolveLinkedStatus` — the P2e
|
|
4611
|
-
* defensive re-entrancy guard bounding link cycles at resolve time. Mutated
|
|
4612
|
-
* in place (never reassigned), so the host exposes a direct reference. */
|
|
4613
|
-
linkResolveInFlight = /* @__PURE__ */ new Set();
|
|
4614
|
-
/** Test/diagnostic accessors. */
|
|
4615
|
-
linkTargetKeys() {
|
|
4616
|
-
return [...this.linkTargets.keys()];
|
|
4617
|
-
}
|
|
4618
|
-
linkDependentsKeys() {
|
|
4619
|
-
return [...this.linkDependents.keys()];
|
|
4620
|
-
}
|
|
4621
|
-
/** Replace the cross-device link reverse-index maps in one atomic swap.
|
|
4622
|
-
* Sole writer of `linkTargets` / `linkDependents` / `expectedSourceStableIds`;
|
|
4623
|
-
* called by `DeviceMetaStore.rebuildLinkDependents` via the `LinkIndexHost`. */
|
|
4624
|
-
setLinkIndexes(targets, dependents, expectedSourceStableIds) {
|
|
4625
|
-
this.linkTargets = targets;
|
|
4626
|
-
this.linkDependents = dependents;
|
|
4627
|
-
this.expectedSourceStableIds = expectedSourceStableIds;
|
|
4628
|
-
}
|
|
4629
|
-
/** Build the `LinkIndexHost` the meta store mutates — exposes this addon's
|
|
4630
|
-
* `devicesWithLinks` gate (read) + `setLinkIndexes` (write) without leaking
|
|
4631
|
-
* the private maps. */
|
|
4632
|
-
get linkIndexHost() {
|
|
4633
|
-
return {
|
|
4634
|
-
devicesWithLinks: this.devicesWithLinks,
|
|
4635
|
-
setLinkIndexes: (targets, dependents, expected) => this.setLinkIndexes(targets, dependents, expected)
|
|
4636
|
-
};
|
|
4637
|
-
}
|
|
4638
3918
|
/** The runtime-state mirror, asserted present (constructed in onInitialize). */
|
|
4639
3919
|
get stateMirror() {
|
|
4640
3920
|
if (!this.stateMirrorImpl) throw new Error("DeviceManagerAddon: state mirror accessed before onInitialize");
|
|
4641
3921
|
return this.stateMirrorImpl;
|
|
4642
3922
|
}
|
|
4643
|
-
/** Build the `LinkOverlayHost` the state mirror reads when overlaying linked
|
|
4644
|
-
* values — exposes the link reverse-index maps + registry via LIVE thunks
|
|
4645
|
-
* (the maps are reassigned on every `rebuildLinkDependents`). Arrow thunks
|
|
4646
|
-
* capture `this` lexically so the getters never go stale. */
|
|
4647
|
-
get linkOverlayHost() {
|
|
4648
|
-
const linkTargets = () => this.linkTargets;
|
|
4649
|
-
const linkDependents = () => this.linkDependents;
|
|
4650
|
-
const capabilityRegistry = () => this.capabilityRegistry;
|
|
4651
|
-
return {
|
|
4652
|
-
get linkTargets() {
|
|
4653
|
-
return linkTargets();
|
|
4654
|
-
},
|
|
4655
|
-
get linkDependents() {
|
|
4656
|
-
return linkDependents();
|
|
4657
|
-
},
|
|
4658
|
-
get capabilityRegistry() {
|
|
4659
|
-
return capabilityRegistry();
|
|
4660
|
-
}
|
|
4661
|
-
};
|
|
4662
|
-
}
|
|
4663
3923
|
/** Build the `ProviderHost` the extracted provider-method modules
|
|
4664
3924
|
* (`device-meta-actions`, `device-queries`) reach — exposes the addon's
|
|
4665
3925
|
* private state/methods they need without leaking the class internals.
|
|
4666
3926
|
*
|
|
4667
|
-
* `
|
|
4668
|
-
*
|
|
4669
|
-
* reference would go stale. `devicesWithLinks` / `remoteNativeCaps` are
|
|
4670
|
-
* mutated in place (never reassigned) so a direct reference preserves the
|
|
4671
|
-
* original live semantics. The overlay-emit guard now lives in the state
|
|
4672
|
-
* mirror — pruning is routed through `dropDeviceOverlays`. */
|
|
3927
|
+
* `remoteNativeCaps` is mutated in place (never reassigned) so a direct
|
|
3928
|
+
* reference preserves the live semantics. */
|
|
4673
3929
|
get providerHost() {
|
|
4674
3930
|
const capabilityRegistry = () => this.capabilityRegistry;
|
|
4675
|
-
const expectedSourceStableIds = () => this.expectedSourceStableIds;
|
|
4676
3931
|
return {
|
|
4677
3932
|
ctx: this.ctx,
|
|
4678
3933
|
get capabilityRegistry() {
|
|
4679
3934
|
return capabilityRegistry();
|
|
4680
3935
|
},
|
|
4681
|
-
devicesWithLinks: this.devicesWithLinks,
|
|
4682
|
-
get expectedSourceStableIds() {
|
|
4683
|
-
return expectedSourceStableIds();
|
|
4684
|
-
},
|
|
4685
|
-
linkResolveInFlight: this.linkResolveInFlight,
|
|
4686
|
-
dropDeviceOverlays: (deviceId) => this.stateMirror.dropDeviceOverlays(deviceId),
|
|
4687
3936
|
remoteNativeCaps: this.remoteNativeCaps,
|
|
4688
3937
|
seedMirror: (deviceId, blob) => this.stateMirror.seedMirror(deviceId, blob),
|
|
4689
3938
|
withResetSessionProbe: (blob) => this.stateMirror.withResetSessionProbe(blob),
|
|
@@ -4731,15 +3980,12 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
4731
3980
|
}
|
|
4732
3981
|
}))).filter((id) => id !== null);
|
|
4733
3982
|
}
|
|
4734
|
-
/** Build the dependency context the extracted binding resolvers consume.
|
|
4735
|
-
* `devicesWithLinks` is mutated in place (never reassigned) so the direct
|
|
4736
|
-
* reference stays live — it gates the virtual `linked` binding step. */
|
|
3983
|
+
/** Build the dependency context the extracted binding resolvers consume. */
|
|
4737
3984
|
get bindingsDeps() {
|
|
4738
3985
|
return {
|
|
4739
3986
|
ctx: this.ctx,
|
|
4740
3987
|
capabilityRegistry: this.capabilityRegistry,
|
|
4741
|
-
remoteNativeCaps: this.remoteNativeCaps
|
|
4742
|
-
devicesWithLinks: this.devicesWithLinks
|
|
3988
|
+
remoteNativeCaps: this.remoteNativeCaps
|
|
4743
3989
|
};
|
|
4744
3990
|
}
|
|
4745
3991
|
async getBindings(input) {
|
|
@@ -4795,21 +4041,16 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
4795
4041
|
if (!ops) throw new Error(`[device-manager] device-ops native provider not found for '${deviceId}'`);
|
|
4796
4042
|
return ops;
|
|
4797
4043
|
};
|
|
4798
|
-
const metaStore = new DeviceMetaStore(settings, registry
|
|
4799
|
-
this.stateMirrorImpl = new DeviceStateMirror(this.ctx
|
|
4044
|
+
const metaStore = new DeviceMetaStore(settings, registry);
|
|
4045
|
+
this.stateMirrorImpl = new DeviceStateMirror(this.ctx);
|
|
4800
4046
|
const stateMirror = this.stateMirrorImpl;
|
|
4801
4047
|
const readMeta = metaStore.readMeta;
|
|
4802
4048
|
const resolvePersistedById = metaStore.resolvePersistedById;
|
|
4803
|
-
const rebuildLinkDependents = metaStore.rebuildLinkDependents;
|
|
4804
4049
|
const idToAddonId = metaStore.idToAddonId;
|
|
4805
4050
|
{
|
|
4806
4051
|
const meta = await readMeta();
|
|
4807
|
-
for (const m of Object.values(meta))
|
|
4808
|
-
idToAddonId.set(m.id, m.addonId);
|
|
4809
|
-
if (Array.isArray(m.deviceLinks) && m.deviceLinks.length > 0) this.devicesWithLinks.add(m.id);
|
|
4810
|
-
}
|
|
4052
|
+
for (const m of Object.values(meta)) idToAddonId.set(m.id, m.addonId);
|
|
4811
4053
|
}
|
|
4812
|
-
await rebuildLinkDependents();
|
|
4813
4054
|
const stampIntegrationId$1 = (deviceId, integrationId) => stampIntegrationId(metaStore, settings, this.ctx, deviceId, integrationId);
|
|
4814
4055
|
const pctx = {
|
|
4815
4056
|
host: this.providerHost,
|
|
@@ -4826,7 +4067,6 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
4826
4067
|
const provider = {
|
|
4827
4068
|
resolveDeviceOwnerSync: (deviceId) => resolveDeviceOwnerSync(pctx, deviceId),
|
|
4828
4069
|
resolveNativeCapOwnerSync: (capName, deviceId) => resolveNativeCapOwnerSync(pctx, capName, deviceId),
|
|
4829
|
-
resolveLinkedStatus: (input) => resolveLinkedStatus(pctx, input),
|
|
4830
4070
|
allocateDeviceId: (input) => allocateDeviceId(pctx, input),
|
|
4831
4071
|
registerDevice: (input) => registerDevice(pctx, input),
|
|
4832
4072
|
removeDevice: (input) => removeDevice(pctx, input),
|
|
@@ -4840,7 +4080,6 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
4840
4080
|
setLinkDeviceId: (input) => setLinkDeviceId(pctx, input),
|
|
4841
4081
|
setPrimaryChildEntityId: (input) => setPrimaryChildEntityId(pctx, input),
|
|
4842
4082
|
setChildLayout: (input) => setChildLayout(pctx, input),
|
|
4843
|
-
setDeviceLinks: (input) => setDeviceLinks(pctx, input),
|
|
4844
4083
|
setRole: (input) => setRole(pctx, input),
|
|
4845
4084
|
setDisplay: (input) => setDisplay(pctx, input),
|
|
4846
4085
|
getRoleDisplayDefaults: (input) => getRoleDisplayDefaults(pctx, input),
|
|
@@ -5000,13 +4239,13 @@ var DeviceManagerAddon = class extends BaseAddon {
|
|
|
5000
4239
|
capability: deviceStateCapability,
|
|
5001
4240
|
provider: {
|
|
5002
4241
|
getSnapshot: async (input) => {
|
|
5003
|
-
return stateMirror.
|
|
4242
|
+
return stateMirror.snapshotForDevice(input.deviceId);
|
|
5004
4243
|
},
|
|
5005
4244
|
getCapSlice: async (input) => {
|
|
5006
|
-
return stateMirror.
|
|
4245
|
+
return stateMirror.capSlice(input.deviceId, input.capName);
|
|
5007
4246
|
},
|
|
5008
4247
|
getAllSnapshots: async () => {
|
|
5009
|
-
return stateMirror.
|
|
4248
|
+
return stateMirror.allSnapshots();
|
|
5010
4249
|
},
|
|
5011
4250
|
setCapSlice: async (input) => {
|
|
5012
4251
|
const { deviceId, capName, slice } = input;
|