@camstack/server 1.2.897 → 1.2.899
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/CHANGELOG.md +12 -0
- package/dist/api/trpc/cap-router-runtime.js +1 -0
- package/dist/api/trpc/generated-cap-mounts.js +2 -1
- package/dist/api/trpc/generated-cap-routers.js +1894 -1870
- package/dist/core/addon/hub-addon-api.js +1 -0
- package/dist/core/moleculer/moleculer.service.js +1 -0
- package/package.json +11 -10
- package/system/dist/addon-runner.js +207 -13
- package/system/dist/addon-runner.mjs +207 -13
- package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.js +1 -1
- package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.mjs +1 -1
- package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.js +1 -1
- package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.mjs +1 -1
- package/system/dist/builtins/alerts/alerts.addon.js +1 -1
- package/system/dist/builtins/alerts/alerts.addon.mjs +1 -1
- package/system/dist/builtins/autotrack/index.js +1 -1
- package/system/dist/builtins/autotrack/index.mjs +1 -1
- package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.js +2 -2
- package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.mjs +2 -2
- package/system/dist/builtins/calendars/calendars.addon.js +1 -1
- package/system/dist/builtins/calendars/calendars.addon.mjs +1 -1
- package/system/dist/builtins/camera-grid/index.js +1 -1
- package/system/dist/builtins/camera-grid/index.mjs +1 -1
- package/system/dist/builtins/composer/composer.addon.js +14 -11
- package/system/dist/builtins/composer/composer.addon.mjs +11 -8
- package/system/dist/builtins/console-logging/index.js +1 -1
- package/system/dist/builtins/console-logging/index.mjs +1 -1
- package/system/dist/builtins/core-blocks/core-blocks.addon.js +170 -14
- package/system/dist/builtins/core-blocks/core-blocks.addon.mjs +169 -13
- package/system/dist/builtins/core-blocks/migrations/automation-confirmed-view.d.ts +23 -0
- package/system/dist/builtins/core-blocks/migrations/index.d.ts +1 -0
- package/system/dist/builtins/core-blocks/migrations/pump-cycle-split.d.ts +1 -0
- package/system/dist/builtins/device-history/device-history.addon.js +10 -9
- package/system/dist/builtins/device-history/device-history.addon.mjs +10 -9
- package/system/dist/builtins/device-manager/command-holds-wiring.d.ts +22 -0
- package/system/dist/builtins/device-manager/command-holds.d.ts +77 -0
- package/system/dist/builtins/device-manager/device-manager.addon.d.ts +3 -0
- package/system/dist/builtins/device-manager/device-manager.addon.js +1078 -41
- package/system/dist/builtins/device-manager/device-manager.addon.mjs +1062 -42
- package/system/dist/builtins/device-manager/device-state-mirror.d.ts +42 -9
- package/system/dist/builtins/device-manager/state-refresh-scheduler.d.ts +92 -0
- package/system/dist/builtins/device-manager/state-refresh-wiring.d.ts +23 -0
- package/system/dist/builtins/hub-forwarder/index.js +1 -1
- package/system/dist/builtins/hub-forwarder/index.mjs +1 -1
- package/system/dist/builtins/keychain/index.js +4 -4
- package/system/dist/builtins/keychain/index.mjs +4 -4
- package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.js +1 -1
- package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.mjs +1 -1
- package/system/dist/builtins/local-auth/local-auth.addon.js +2 -2
- package/system/dist/builtins/local-auth/local-auth.addon.mjs +2 -2
- package/system/dist/builtins/local-network/local-network.addon.js +3 -3
- package/system/dist/builtins/local-network/local-network.addon.mjs +3 -3
- package/system/dist/builtins/loki-logging/index.js +1 -1
- package/system/dist/builtins/loki-logging/index.mjs +1 -1
- package/system/dist/builtins/native-metrics/native-metrics.addon.js +1 -1
- package/system/dist/builtins/native-metrics/native-metrics.addon.mjs +1 -1
- package/system/dist/builtins/platform-probe/index.js +1 -1
- package/system/dist/builtins/platform-probe/index.mjs +1 -1
- package/system/dist/builtins/record-entries/index.js +1 -1
- package/system/dist/builtins/record-entries/index.mjs +1 -1
- package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.js +1 -1
- package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.mjs +1 -1
- package/system/dist/builtins/shared/device-state-changed.d.ts +8 -0
- package/system/dist/builtins/snapshot/index.js +1 -1
- package/system/dist/builtins/snapshot/index.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.js +1 -1
- package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/index.js +1 -1
- package/system/dist/builtins/sqlite-storage/index.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.js +3 -3
- package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.mjs +3 -3
- package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.js +1 -1
- package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.mjs +1 -1
- package/system/dist/builtins/system-config/system-config.addon.js +3 -3
- package/system/dist/builtins/system-config/system-config.addon.mjs +284 -284
- package/system/dist/builtins/winston-logging/index.js +1 -1
- package/system/dist/builtins/winston-logging/index.mjs +1 -1
- package/system/dist/{capture-engine-Bjgs__i9.mjs → capture-engine-BcD6iCZM.mjs} +37 -2
- package/system/dist/{capture-engine-D1rjl2dW.js → capture-engine-LHKTf-nM.js} +48 -1
- package/system/dist/{composition-sources-B727nlA0.mjs → composition-sources-C3ARJk2g.mjs} +2 -21
- package/system/dist/{composition-sources-CF2-u58e.js → composition-sources-DNUDbuXy.js} +1 -32
- package/system/dist/{constants-DaR1SUh7.js → constants-dL8fl9aI.js} +1 -1
- package/system/dist/{constants-BF309c_I.mjs → constants-er38mZOb.mjs} +1 -1
- package/system/dist/{device-ca-DT1hYtdW.mjs → device-ca-Ccz5olgH.mjs} +1 -1
- package/system/dist/{device-ca-BqcX5_f2.js → device-ca-KWa6uJRU.js} +1 -1
- package/system/dist/{dist-DorY_sjS.mjs → dist-DBanWvHF.mjs} +819 -54
- package/system/dist/{dist-BoEy9sm9.js → dist-DEAjZE_-.js} +848 -53
- package/system/dist/file-host-worker.js +1 -1
- package/system/dist/file-host-worker.mjs +1 -1
- package/system/dist/{hub-identity-CZ0nE3GK.mjs → hub-identity-CVv79TVO.mjs} +1 -1
- package/system/dist/{hub-identity-DH1rxUMD.js → hub-identity-Dy8ZSUuq.js} +1 -1
- package/system/dist/{hub-mdns-ciao-N0nL34fx.js → hub-mdns-ciao-C_XjO7YI.js} +2 -2
- package/system/dist/{hub-mdns-ciao-7HK57Vtx.mjs → hub-mdns-ciao-DtmIyWDL.mjs} +2 -2
- package/system/dist/index.js +96 -17
- package/system/dist/index.mjs +92 -19
- package/system/dist/kernel/cap-router-builder.d.ts +7 -0
- package/system/dist/kernel/command-follow-up.d.ts +33 -0
- package/system/dist/kernel/command-hold-overrides.d.ts +42 -0
- package/system/dist/kernel/command-observation.d.ts +3 -0
- package/system/dist/kernel/index.d.ts +3 -0
- package/system/dist/kernel/moleculer/child-cap-descriptors.d.ts +5 -0
- package/system/dist/kernel/moleculer/device-cap-proxy.d.ts +3 -1
- package/system/dist/kernel/state-refresh-policies.d.ts +53 -0
- package/system/dist/kernel/transport/child-cap-protocol.d.ts +11 -1
- package/system/dist/kernel/transport/local-child-registry.d.ts +14 -0
- package/system/dist/{legacy-integration-rows-door-CdAQlIvv.mjs → legacy-integration-rows-door-BL8T4lya.mjs} +1 -1
- package/system/dist/{legacy-integration-rows-door-BTfeULYt.js → legacy-integration-rows-door-DPInWtE6.js} +1 -1
- package/system/dist/{rename-composition-block-B1x-yK5U.js → rename-composition-block-BxzETYOf.js} +1 -1
- package/system/dist/{rename-composition-block-CIQjxBUa.mjs → rename-composition-block-Dgo-9-Ts.mjs} +1 -1
- package/system/dist/{restore-archive-Bj_gYmE9.mjs → restore-archive-BUtuNTe1.mjs} +82 -82
- package/system/dist/{restore-archive-B086o8pL.js → restore-archive-Zvx9sHZP.js} +1 -1
- package/system/dist/{wal-maintenance-MVIyxmu7.js → wal-maintenance-DS2IKwtK.js} +0 -0
- package/system/dist/{wal-maintenance-Dgfmi-Ao.mjs → wal-maintenance-cASt_sYy.mjs} +0 -0
- package/system/package.json +1 -1
|
@@ -3,10 +3,10 @@ Object.defineProperties(exports, {
|
|
|
3
3
|
[Symbol.toStringTag]: { value: "Module" }
|
|
4
4
|
});
|
|
5
5
|
const require_chunk = require("../../chunk-Cek0wNdY.js");
|
|
6
|
-
const require_dist = require("../../dist-
|
|
6
|
+
const require_dist = require("../../dist-DEAjZE_-.js");
|
|
7
7
|
const require_read_all_rows$1 = require("../../read-all-rows-CVoO1ONM.js");
|
|
8
8
|
const require_log_redactor = require("../../log-redactor-BhIEMaG9.js");
|
|
9
|
-
const require_rename_composition_block = require("../../rename-composition-block-
|
|
9
|
+
const require_rename_composition_block = require("../../rename-composition-block-BxzETYOf.js");
|
|
10
10
|
const require_prefix_range = require("../../prefix-range-D_KueXbQ.js");
|
|
11
11
|
const require_prefix_walk = require("../../prefix-walk-Dv6MLZlM.js");
|
|
12
12
|
const require_retired_settings_keys = require("../../retired-settings-keys-C8nOOr5H.js");
|
|
@@ -11398,7 +11398,7 @@ function createDeviceStateClaimMethods(deps) {
|
|
|
11398
11398
|
meta: {
|
|
11399
11399
|
cap: capName,
|
|
11400
11400
|
owner,
|
|
11401
|
-
error: errorMessage$
|
|
11401
|
+
error: errorMessage$4(err)
|
|
11402
11402
|
}
|
|
11403
11403
|
});
|
|
11404
11404
|
throw err;
|
|
@@ -11443,13 +11443,13 @@ function createDeviceStateClaimMethods(deps) {
|
|
|
11443
11443
|
meta: {
|
|
11444
11444
|
cap: key.capName,
|
|
11445
11445
|
refusal: code,
|
|
11446
|
-
error: errorMessage$
|
|
11446
|
+
error: errorMessage$4(err)
|
|
11447
11447
|
}
|
|
11448
11448
|
});
|
|
11449
11449
|
}
|
|
11450
11450
|
}
|
|
11451
11451
|
}
|
|
11452
|
-
function errorMessage$
|
|
11452
|
+
function errorMessage$4(err) {
|
|
11453
11453
|
return err instanceof Error ? err.message : String(err);
|
|
11454
11454
|
}
|
|
11455
11455
|
//#endregion
|
|
@@ -11727,7 +11727,7 @@ var RuntimeStateRowUnreadError = class extends Error {
|
|
|
11727
11727
|
};
|
|
11728
11728
|
var ROW_UNREAD_REFUSAL_MESSAGE = "runtime-state row write refused — the row could not be read first, nothing written";
|
|
11729
11729
|
var ROW_DROPS_CAPS_MESSAGE = "runtime-state row write DROPS caps the stored row holds";
|
|
11730
|
-
function errorMessage$
|
|
11730
|
+
function errorMessage$3(err) {
|
|
11731
11731
|
return err instanceof Error ? err.message : String(err);
|
|
11732
11732
|
}
|
|
11733
11733
|
/**
|
|
@@ -11895,7 +11895,7 @@ var MirrorRowWriter = class MirrorRowWriter {
|
|
|
11895
11895
|
tags: { deviceId },
|
|
11896
11896
|
meta: {
|
|
11897
11897
|
path,
|
|
11898
|
-
error: errorMessage$
|
|
11898
|
+
error: errorMessage$3(err)
|
|
11899
11899
|
}
|
|
11900
11900
|
});
|
|
11901
11901
|
}
|
|
@@ -11977,7 +11977,7 @@ var MirrorRowWriter = class MirrorRowWriter {
|
|
|
11977
11977
|
slot.lastWriteFailed = true;
|
|
11978
11978
|
this.deps.logger.warn("writeDeviceRuntimeState failed", {
|
|
11979
11979
|
tags: { deviceId },
|
|
11980
|
-
meta: { error: errorMessage$
|
|
11980
|
+
meta: { error: errorMessage$3(err) }
|
|
11981
11981
|
});
|
|
11982
11982
|
}
|
|
11983
11983
|
}
|
|
@@ -12052,7 +12052,7 @@ var MirrorRowWriter = class MirrorRowWriter {
|
|
|
12052
12052
|
try {
|
|
12053
12053
|
await this.serialiseWrite(deviceId, () => this.writeRow(deviceId, settings, blob, "drain"));
|
|
12054
12054
|
} catch (err) {
|
|
12055
|
-
this.refuseDrain(deviceId, `the pending runtime-state write failed: ${errorMessage$
|
|
12055
|
+
this.refuseDrain(deviceId, `the pending runtime-state write failed: ${errorMessage$3(err)}`, err);
|
|
12056
12056
|
}
|
|
12057
12057
|
}
|
|
12058
12058
|
/** A drain that cannot vouch for the row: still owed (re-armed on release), logged, refused. */
|
|
@@ -12097,6 +12097,206 @@ var MirrorRowWriter = class MirrorRowWriter {
|
|
|
12097
12097
|
}
|
|
12098
12098
|
};
|
|
12099
12099
|
//#endregion
|
|
12100
|
+
//#region src/builtins/device-manager/command-holds.ts
|
|
12101
|
+
function same(a, b) {
|
|
12102
|
+
return Object.is(a, b);
|
|
12103
|
+
}
|
|
12104
|
+
/** Does `reading` confirm `held`: equal, or a number within its tolerance? */
|
|
12105
|
+
function confirms(reading, held, tolerance) {
|
|
12106
|
+
if (same(reading, held)) return true;
|
|
12107
|
+
if (typeof reading !== "number" || typeof held !== "number" || tolerance <= 0) return false;
|
|
12108
|
+
return Math.abs(reading - held) <= tolerance + 1e-9;
|
|
12109
|
+
}
|
|
12110
|
+
function judge(reading, held) {
|
|
12111
|
+
if (confirms(reading, held.value, held.tolerance)) return "confirmed";
|
|
12112
|
+
if (held.stale.some((v) => same(v, reading))) return "stale";
|
|
12113
|
+
return "device-answered";
|
|
12114
|
+
}
|
|
12115
|
+
var CommandHolds = class {
|
|
12116
|
+
deps;
|
|
12117
|
+
/** device → cap → field → held value. */
|
|
12118
|
+
held = /* @__PURE__ */ new Map();
|
|
12119
|
+
armings = /* @__PURE__ */ new Map();
|
|
12120
|
+
seq = 0;
|
|
12121
|
+
constructor(deps) {
|
|
12122
|
+
this.deps = deps;
|
|
12123
|
+
}
|
|
12124
|
+
/**
|
|
12125
|
+
* Hold what a command leads to. A field the slice already holds is not held
|
|
12126
|
+
* (and a standing hold on it is dropped: the newer command wins). Returns the
|
|
12127
|
+
* token that `release` takes, or `null` when nothing is held.
|
|
12128
|
+
*/
|
|
12129
|
+
arm(request) {
|
|
12130
|
+
const base = this.deps.baseSlice(request.deviceId, request.capName);
|
|
12131
|
+
if (base === null) {
|
|
12132
|
+
this.deps.logger.debug("command hold: the cap has no slice yet — nothing held", {
|
|
12133
|
+
tags: { deviceId: request.deviceId },
|
|
12134
|
+
meta: {
|
|
12135
|
+
cap: request.capName,
|
|
12136
|
+
method: request.method
|
|
12137
|
+
}
|
|
12138
|
+
});
|
|
12139
|
+
return null;
|
|
12140
|
+
}
|
|
12141
|
+
this.seq += 1;
|
|
12142
|
+
const token = this.seq;
|
|
12143
|
+
const fields = this.fieldsOf(request.deviceId, request.capName);
|
|
12144
|
+
let changed = false;
|
|
12145
|
+
let holding = false;
|
|
12146
|
+
for (const effect of request.effects) {
|
|
12147
|
+
const standing = fields.get(effect.field);
|
|
12148
|
+
const tolerance = effect.tolerance ?? 0;
|
|
12149
|
+
const reported = Object.hasOwn(base, effect.field) ? base[effect.field] : void 0;
|
|
12150
|
+
if (Object.hasOwn(base, effect.field) && confirms(reported, effect.value, tolerance)) {
|
|
12151
|
+
if (standing !== void 0) {
|
|
12152
|
+
fields.delete(effect.field);
|
|
12153
|
+
changed = true;
|
|
12154
|
+
}
|
|
12155
|
+
continue;
|
|
12156
|
+
}
|
|
12157
|
+
if (standing === void 0 || !same(standing.value, effect.value)) changed = true;
|
|
12158
|
+
const stale = standing === void 0 ? [reported] : [...standing.stale, standing.value];
|
|
12159
|
+
fields.set(effect.field, {
|
|
12160
|
+
value: effect.value,
|
|
12161
|
+
tolerance,
|
|
12162
|
+
stale,
|
|
12163
|
+
token
|
|
12164
|
+
});
|
|
12165
|
+
holding = true;
|
|
12166
|
+
}
|
|
12167
|
+
this.dropEmpty(request.deviceId, request.capName);
|
|
12168
|
+
this.forgetStaleArmings();
|
|
12169
|
+
if (holding) {
|
|
12170
|
+
const timer = setTimeout(() => this.release(token, "unconfirmed"), request.holdMs);
|
|
12171
|
+
timer.unref?.();
|
|
12172
|
+
this.armings.set(token, {
|
|
12173
|
+
token,
|
|
12174
|
+
deviceId: request.deviceId,
|
|
12175
|
+
capName: request.capName,
|
|
12176
|
+
method: request.method,
|
|
12177
|
+
holdMs: request.holdMs,
|
|
12178
|
+
timer
|
|
12179
|
+
});
|
|
12180
|
+
}
|
|
12181
|
+
if (changed) this.deps.reemit(request.deviceId, request.capName);
|
|
12182
|
+
return holding ? token : null;
|
|
12183
|
+
}
|
|
12184
|
+
/** End what `token` still holds — refused at once, or unconfirmed at the limit. */
|
|
12185
|
+
release(token, reason) {
|
|
12186
|
+
const arming = this.armings.get(token);
|
|
12187
|
+
if (arming === void 0) return;
|
|
12188
|
+
this.armings.delete(token);
|
|
12189
|
+
clearTimeout(arming.timer);
|
|
12190
|
+
const fields = this.held.get(arming.deviceId)?.get(arming.capName);
|
|
12191
|
+
if (fields === void 0) return;
|
|
12192
|
+
const dropped = [];
|
|
12193
|
+
for (const [field, held] of fields) {
|
|
12194
|
+
if (held.token !== token) continue;
|
|
12195
|
+
fields.delete(field);
|
|
12196
|
+
dropped.push(field);
|
|
12197
|
+
}
|
|
12198
|
+
if (dropped.length === 0) return;
|
|
12199
|
+
this.dropEmpty(arming.deviceId, arming.capName);
|
|
12200
|
+
const tags = { deviceId: arming.deviceId };
|
|
12201
|
+
const meta = {
|
|
12202
|
+
cap: arming.capName,
|
|
12203
|
+
method: arming.method,
|
|
12204
|
+
fields: dropped
|
|
12205
|
+
};
|
|
12206
|
+
if (reason === "unconfirmed" && arming.holdMs <= 1e4) this.deps.logger.debug("one-shot command hold ended", {
|
|
12207
|
+
tags,
|
|
12208
|
+
meta
|
|
12209
|
+
});
|
|
12210
|
+
else if (reason === "unconfirmed") this.deps.logger.warn("command hold reverted: the device did not confirm in time", {
|
|
12211
|
+
tags,
|
|
12212
|
+
meta
|
|
12213
|
+
});
|
|
12214
|
+
else this.deps.logger.info("command hold rolled back: the command was refused", {
|
|
12215
|
+
tags,
|
|
12216
|
+
meta
|
|
12217
|
+
});
|
|
12218
|
+
this.deps.reemit(arming.deviceId, arming.capName);
|
|
12219
|
+
}
|
|
12220
|
+
/**
|
|
12221
|
+
* The slice under the hold moved: keep each held field it still reports
|
|
12222
|
+
* stale, drop the ones it confirms or answers otherwise.
|
|
12223
|
+
*/
|
|
12224
|
+
settle(deviceId, capName, base) {
|
|
12225
|
+
const fields = this.held.get(deviceId)?.get(capName);
|
|
12226
|
+
if (fields === void 0) return;
|
|
12227
|
+
for (const [field, held] of fields) {
|
|
12228
|
+
if (!Object.hasOwn(base, field)) continue;
|
|
12229
|
+
const reading = judge(base[field], held);
|
|
12230
|
+
if (reading === "stale") continue;
|
|
12231
|
+
fields.delete(field);
|
|
12232
|
+
if (reading === "device-answered") this.deps.logger.debug("command hold released: the device answered otherwise", {
|
|
12233
|
+
tags: { deviceId },
|
|
12234
|
+
meta: {
|
|
12235
|
+
cap: capName,
|
|
12236
|
+
field,
|
|
12237
|
+
reason: reading
|
|
12238
|
+
}
|
|
12239
|
+
});
|
|
12240
|
+
}
|
|
12241
|
+
this.dropEmpty(deviceId, capName);
|
|
12242
|
+
this.forgetStaleArmings();
|
|
12243
|
+
}
|
|
12244
|
+
/** `base` with the held values over it; `base` itself when nothing is held. */
|
|
12245
|
+
overlay(deviceId, capName, base) {
|
|
12246
|
+
const fields = this.held.get(deviceId)?.get(capName);
|
|
12247
|
+
if (fields === void 0 || fields.size === 0) return base;
|
|
12248
|
+
const out = { ...base };
|
|
12249
|
+
for (const [field, held] of fields) out[field] = held.value;
|
|
12250
|
+
return out;
|
|
12251
|
+
}
|
|
12252
|
+
/** Is any field of the cap held? */
|
|
12253
|
+
isHolding(deviceId, capName) {
|
|
12254
|
+
return (this.held.get(deviceId)?.get(capName)?.size ?? 0) > 0;
|
|
12255
|
+
}
|
|
12256
|
+
/** Is any field of any cap of the device still held? (D1062: the command burst stops when this is false.) */
|
|
12257
|
+
isHoldingDevice(deviceId) {
|
|
12258
|
+
for (const fields of this.held.get(deviceId)?.values() ?? []) if (fields.size > 0) return true;
|
|
12259
|
+
return false;
|
|
12260
|
+
}
|
|
12261
|
+
/** Forget a removed device's holds. */
|
|
12262
|
+
clearDevice(deviceId) {
|
|
12263
|
+
this.held.delete(deviceId);
|
|
12264
|
+
for (const [token, arming] of this.armings) {
|
|
12265
|
+
if (arming.deviceId !== deviceId) continue;
|
|
12266
|
+
clearTimeout(arming.timer);
|
|
12267
|
+
this.armings.delete(token);
|
|
12268
|
+
}
|
|
12269
|
+
}
|
|
12270
|
+
/** Stop every timer (shutdown). */
|
|
12271
|
+
dispose() {
|
|
12272
|
+
for (const arming of this.armings.values()) clearTimeout(arming.timer);
|
|
12273
|
+
this.armings.clear();
|
|
12274
|
+
this.held.clear();
|
|
12275
|
+
}
|
|
12276
|
+
fieldsOf(deviceId, capName) {
|
|
12277
|
+
const perCap = this.held.get(deviceId) ?? /* @__PURE__ */ new Map();
|
|
12278
|
+
this.held.set(deviceId, perCap);
|
|
12279
|
+
const fields = perCap.get(capName) ?? /* @__PURE__ */ new Map();
|
|
12280
|
+
perCap.set(capName, fields);
|
|
12281
|
+
return fields;
|
|
12282
|
+
}
|
|
12283
|
+
dropEmpty(deviceId, capName) {
|
|
12284
|
+
const perCap = this.held.get(deviceId);
|
|
12285
|
+
if (perCap === void 0) return;
|
|
12286
|
+
if ((perCap.get(capName)?.size ?? 0) === 0) perCap.delete(capName);
|
|
12287
|
+
if (perCap.size === 0) this.held.delete(deviceId);
|
|
12288
|
+
}
|
|
12289
|
+
/** An arming whose fields were all confirmed or replaced holds nothing: stop its timer. */
|
|
12290
|
+
forgetStaleArmings() {
|
|
12291
|
+
for (const [token, arming] of this.armings) {
|
|
12292
|
+
const fields = this.held.get(arming.deviceId)?.get(arming.capName);
|
|
12293
|
+
if (fields !== void 0 && [...fields.values()].some((f) => f.token === token)) continue;
|
|
12294
|
+
clearTimeout(arming.timer);
|
|
12295
|
+
this.armings.delete(token);
|
|
12296
|
+
}
|
|
12297
|
+
}
|
|
12298
|
+
};
|
|
12299
|
+
//#endregion
|
|
12100
12300
|
//#region src/builtins/device-manager/device-state-mirror.ts
|
|
12101
12301
|
/**
|
|
12102
12302
|
* Hub-side runtime-state mirror for the device-manager addon.
|
|
@@ -12130,13 +12330,14 @@ var MirrorRowWriter = class MirrorRowWriter {
|
|
|
12130
12330
|
*/
|
|
12131
12331
|
var RUNNING_STATE_CAP = "running-state";
|
|
12132
12332
|
var SEED_REFUSAL_MESSAGE = "runtime-state row unreadable — native write refused until it reads";
|
|
12133
|
-
function errorMessage$
|
|
12333
|
+
function errorMessage$2(err) {
|
|
12134
12334
|
return err instanceof Error ? err.message : String(err);
|
|
12135
12335
|
}
|
|
12136
12336
|
var DeviceStateMirror = class {
|
|
12137
12337
|
ctx;
|
|
12138
12338
|
policyFor;
|
|
12139
12339
|
transitionTap = null;
|
|
12340
|
+
reportTap = null;
|
|
12140
12341
|
/**
|
|
12141
12342
|
* Hub-side mirror of every device's cap-keyed NATIVE runtime state — what the
|
|
12142
12343
|
* provider wrote. Key: deviceId. Value: per-cap slice map. Empty by default —
|
|
@@ -12145,6 +12346,8 @@ var DeviceStateMirror = class {
|
|
|
12145
12346
|
stateMirror = /* @__PURE__ */ new Map();
|
|
12146
12347
|
/** The claims and the merged view of every claimed cap (D663). */
|
|
12147
12348
|
claims;
|
|
12349
|
+
/** Commanded values shown before the device confirms them (D1061). Memory only. */
|
|
12350
|
+
holds;
|
|
12148
12351
|
/**
|
|
12149
12352
|
* Devices whose CLAIMS are known: the row was read, or a loaded claims index
|
|
12150
12353
|
* vouched that it names none (D447). This is a READ-path fact — what a
|
|
@@ -12175,6 +12378,14 @@ var DeviceStateMirror = class {
|
|
|
12175
12378
|
schemaFor,
|
|
12176
12379
|
emitSlice: (deviceId, capName, slice) => this.emitStateChanged(deviceId, capName, slice)
|
|
12177
12380
|
});
|
|
12381
|
+
this.holds = new CommandHolds({
|
|
12382
|
+
logger: ctx.logger,
|
|
12383
|
+
baseSlice: (deviceId, capName) => this.baseSlice(deviceId, capName),
|
|
12384
|
+
reemit: (deviceId, capName) => {
|
|
12385
|
+
const base = this.baseSlice(deviceId, capName);
|
|
12386
|
+
if (base !== null) this.emitStateChanged(deviceId, capName, { ...base });
|
|
12387
|
+
}
|
|
12388
|
+
});
|
|
12178
12389
|
this.writer = new MirrorRowWriter({
|
|
12179
12390
|
logger: ctx.logger,
|
|
12180
12391
|
policyFor,
|
|
@@ -12187,6 +12398,30 @@ var DeviceStateMirror = class {
|
|
|
12187
12398
|
setTransitionTap(tap) {
|
|
12188
12399
|
this.transitionTap = tap;
|
|
12189
12400
|
}
|
|
12401
|
+
/** Set once by the device-manager: told of every native slice reported, before the no-change check (D1062). */
|
|
12402
|
+
setReportTap(tap) {
|
|
12403
|
+
this.reportTap = tap;
|
|
12404
|
+
}
|
|
12405
|
+
/**
|
|
12406
|
+
* Hold what a command leads to: the effective slice shows it at once (and
|
|
12407
|
+
* `DeviceStateChanged` carries it), the stored, persisted and recorded slice
|
|
12408
|
+
* does not change. Returns the token {@link releaseCommand} takes, or `null`.
|
|
12409
|
+
*/
|
|
12410
|
+
holdCommand(request) {
|
|
12411
|
+
return this.holds.arm(request);
|
|
12412
|
+
}
|
|
12413
|
+
/** End a command's hold: refused (rolled back at once) or unconfirmed (at the limit). */
|
|
12414
|
+
releaseCommand(token, reason) {
|
|
12415
|
+
this.holds.release(token, reason);
|
|
12416
|
+
}
|
|
12417
|
+
/** Whether any command hold on the device is still unconfirmed (D1062). */
|
|
12418
|
+
isHoldingDevice(deviceId) {
|
|
12419
|
+
return this.holds.isHoldingDevice(deviceId);
|
|
12420
|
+
}
|
|
12421
|
+
/** Stop the hold timers (shutdown). */
|
|
12422
|
+
disposeHolds() {
|
|
12423
|
+
this.holds.dispose();
|
|
12424
|
+
}
|
|
12190
12425
|
/** The native slice the mirror holds for `capName`, as a read-only view. */
|
|
12191
12426
|
currentSlice(deviceId, capName) {
|
|
12192
12427
|
return this.stateMirror.get(deviceId)?.get(capName);
|
|
@@ -12207,6 +12442,7 @@ var DeviceStateMirror = class {
|
|
|
12207
12442
|
* a SQLite commit.
|
|
12208
12443
|
*/
|
|
12209
12444
|
applySingleCapUpdate(deviceId, capName, slice) {
|
|
12445
|
+
this.reportTap?.(deviceId, capName);
|
|
12210
12446
|
let perCap = this.stateMirror.get(deviceId);
|
|
12211
12447
|
if (!perCap) {
|
|
12212
12448
|
perCap = /* @__PURE__ */ new Map();
|
|
@@ -12399,6 +12635,7 @@ var DeviceStateMirror = class {
|
|
|
12399
12635
|
await this.seeding.get(deviceId)?.catch(() => void 0);
|
|
12400
12636
|
await this.writer.forget(deviceId);
|
|
12401
12637
|
this.stateMirror.delete(deviceId);
|
|
12638
|
+
this.holds.clearDevice(deviceId);
|
|
12402
12639
|
this.workloadTransitions.forget(deviceId);
|
|
12403
12640
|
this.claims.clearDevice(deviceId);
|
|
12404
12641
|
this.seeded.delete(deviceId);
|
|
@@ -12471,7 +12708,7 @@ var DeviceStateMirror = class {
|
|
|
12471
12708
|
await this.ensureRowRead(deviceId, settings);
|
|
12472
12709
|
return null;
|
|
12473
12710
|
} catch (err) {
|
|
12474
|
-
return refused("row-unreadable", `device ${deviceId}: ${errorMessage$
|
|
12711
|
+
return refused("row-unreadable", `device ${deviceId}: ${errorMessage$2(err)}`);
|
|
12475
12712
|
}
|
|
12476
12713
|
}
|
|
12477
12714
|
readRowOnce(deviceId, settings) {
|
|
@@ -12485,7 +12722,7 @@ var DeviceStateMirror = class {
|
|
|
12485
12722
|
this.seedRefusalLogged.add(deviceId);
|
|
12486
12723
|
this.ctx.logger.warn(SEED_REFUSAL_MESSAGE, {
|
|
12487
12724
|
tags: { deviceId },
|
|
12488
|
-
meta: { error: errorMessage$
|
|
12725
|
+
meta: { error: errorMessage$2(err) }
|
|
12489
12726
|
});
|
|
12490
12727
|
}
|
|
12491
12728
|
throw err;
|
|
@@ -12519,7 +12756,7 @@ var DeviceStateMirror = class {
|
|
|
12519
12756
|
meta: {
|
|
12520
12757
|
cap: op.cap,
|
|
12521
12758
|
owner: op.owner,
|
|
12522
|
-
error: errorMessage$
|
|
12759
|
+
error: errorMessage$2(err)
|
|
12523
12760
|
}
|
|
12524
12761
|
};
|
|
12525
12762
|
if (rollback()) {
|
|
@@ -12690,25 +12927,33 @@ var DeviceStateMirror = class {
|
|
|
12690
12927
|
if (!raw) return true;
|
|
12691
12928
|
return (typeof raw.lastProbedAt === "number" ? raw.lastProbedAt : 0) > 0;
|
|
12692
12929
|
}
|
|
12693
|
-
/** The
|
|
12694
|
-
*
|
|
12695
|
-
|
|
12930
|
+
/** The slices of a device: native for an unclaimed cap, the merge for a
|
|
12931
|
+
* claimed one (a held cap is not reported) — and, in the `effective` view,
|
|
12932
|
+
* a commanded value over either (D1061). */
|
|
12933
|
+
snapshotForDevice(deviceId, view = "effective") {
|
|
12696
12934
|
const out = {};
|
|
12697
12935
|
for (const [k, v] of this.stateMirror.get(deviceId) ?? []) {
|
|
12698
12936
|
if (this.claims.isClaimed(deviceId, k)) continue;
|
|
12699
|
-
out[k] =
|
|
12937
|
+
out[k] = this.viewOf(deviceId, k, v, view);
|
|
12700
12938
|
}
|
|
12701
|
-
for (const [k, v] of this.claims.visibleCaps(deviceId)) out[k] =
|
|
12939
|
+
for (const [k, v] of this.claims.visibleCaps(deviceId)) out[k] = this.viewOf(deviceId, k, v, view);
|
|
12702
12940
|
return out;
|
|
12703
12941
|
}
|
|
12704
|
-
/** One cap's
|
|
12942
|
+
/** One cap's slice in `view`, cloned. Null when the device has never written
|
|
12705
12943
|
* it — callers read that as "the cap has no state", never as an empty slice —
|
|
12706
12944
|
* and null while a claim holds it. */
|
|
12707
|
-
capSlice(deviceId, cap) {
|
|
12945
|
+
capSlice(deviceId, cap, view = "effective") {
|
|
12946
|
+
const base = this.baseSlice(deviceId, cap);
|
|
12947
|
+
return base === null ? null : this.viewOf(deviceId, cap, base, view);
|
|
12948
|
+
}
|
|
12949
|
+
/** What the device reported for the cap, or the claim's merge — never a hold. Not cloned. */
|
|
12950
|
+
baseSlice(deviceId, cap) {
|
|
12708
12951
|
const visible = this.claims.visible(deviceId, cap);
|
|
12709
|
-
if (visible !== void 0) return visible ?
|
|
12710
|
-
|
|
12711
|
-
|
|
12952
|
+
if (visible !== void 0) return visible ? visible : null;
|
|
12953
|
+
return this.stateMirror.get(deviceId)?.get(cap) ?? null;
|
|
12954
|
+
}
|
|
12955
|
+
viewOf(deviceId, cap, base, view) {
|
|
12956
|
+
return view === "confirmed" ? { ...base } : { ...this.holds.overlay(deviceId, cap, base) };
|
|
12712
12957
|
}
|
|
12713
12958
|
/** The device's NATIVE slices — what the row stores under the cap names. */
|
|
12714
12959
|
nativeSnapshotForDevice(deviceId) {
|
|
@@ -12720,10 +12965,10 @@ var DeviceStateMirror = class {
|
|
|
12720
12965
|
}
|
|
12721
12966
|
/** Whole-system mirror dump. Backs `deviceState.getAllSnapshots` — one
|
|
12722
12967
|
* round-trip for a warm boot instead of N per-device calls. */
|
|
12723
|
-
allSnapshots() {
|
|
12968
|
+
allSnapshots(view = "effective") {
|
|
12724
12969
|
const out = {};
|
|
12725
12970
|
const deviceIds = new Set([...this.stateMirror.keys(), ...this.claims.deviceIds()]);
|
|
12726
|
-
for (const deviceId of deviceIds) out[String(deviceId)] = this.snapshotForDevice(deviceId);
|
|
12971
|
+
for (const deviceId of deviceIds) out[String(deviceId)] = this.snapshotForDevice(deviceId, view);
|
|
12727
12972
|
return out;
|
|
12728
12973
|
}
|
|
12729
12974
|
/**
|
|
@@ -12752,17 +12997,21 @@ var DeviceStateMirror = class {
|
|
|
12752
12997
|
}
|
|
12753
12998
|
/** Remember a restored running bit so the first public merge does not emit a flip. */
|
|
12754
12999
|
seedRunningFlip(deviceId) {
|
|
12755
|
-
const slice = this.capSlice(deviceId, RUNNING_STATE_CAP);
|
|
13000
|
+
const slice = this.capSlice(deviceId, RUNNING_STATE_CAP, "confirmed");
|
|
12756
13001
|
if (slice === null) return;
|
|
12757
13002
|
const running = slice["running"];
|
|
12758
13003
|
this.runningFlip.seed(deviceId, running === true || running === false ? running : null);
|
|
12759
13004
|
}
|
|
12760
13005
|
/**
|
|
12761
|
-
* The PUBLIC event:
|
|
12762
|
-
*
|
|
12763
|
-
* `
|
|
13006
|
+
* The PUBLIC event: `{ deviceId, capName, slice }` for every device, claimed
|
|
13007
|
+
* or not; `slice` is the EFFECTIVE slice. While a command hold changes it
|
|
13008
|
+
* (D1061), `confirmed` carries the slice without the hold — what history,
|
|
13009
|
+
* automations and the edge detectors below read. The native shadow of a
|
|
13010
|
+
* claim never rides here (D224) — see `emitNativeShadow`.
|
|
12764
13011
|
*/
|
|
12765
13012
|
emitStateChanged(deviceId, capName, slice) {
|
|
13013
|
+
this.holds.settle(deviceId, capName, slice);
|
|
13014
|
+
const effective = this.holds.overlay(deviceId, capName, slice);
|
|
12766
13015
|
if (capName === RUNNING_STATE_CAP) {
|
|
12767
13016
|
const flip = this.runningFlip.note(deviceId, slice);
|
|
12768
13017
|
if (flip !== null) this.ctx.eventBus.emit({
|
|
@@ -12789,10 +13038,15 @@ var DeviceStateMirror = class {
|
|
|
12789
13038
|
id: deviceId
|
|
12790
13039
|
},
|
|
12791
13040
|
category: require_dist.EventCategory.DeviceStateChanged,
|
|
12792
|
-
data: {
|
|
13041
|
+
data: effective === slice ? {
|
|
12793
13042
|
deviceId,
|
|
12794
13043
|
capName,
|
|
12795
13044
|
slice
|
|
13045
|
+
} : {
|
|
13046
|
+
deviceId,
|
|
13047
|
+
capName,
|
|
13048
|
+
slice: { ...effective },
|
|
13049
|
+
confirmed: slice
|
|
12796
13050
|
}
|
|
12797
13051
|
});
|
|
12798
13052
|
}
|
|
@@ -12873,6 +13127,93 @@ function pipeReducer(prev, fn) {
|
|
|
12873
13127
|
return fn(prev);
|
|
12874
13128
|
}
|
|
12875
13129
|
//#endregion
|
|
13130
|
+
//#region src/kernel/command-follow-up.ts
|
|
13131
|
+
/**
|
|
13132
|
+
* The command follow-up (D1062): the work a device command waits for before
|
|
13133
|
+
* its caller hears the answer.
|
|
13134
|
+
*
|
|
13135
|
+
* The D1022 seams that observe a command (the hub tRPC mount, the UDS parent,
|
|
13136
|
+
* an in-process hub addon's `ctx.api`) also await this hook once the
|
|
13137
|
+
* provider's call settled — the hub's state-refresh scheduler re-reads the
|
|
13138
|
+
* device there, so the command returns after the real state was pushed.
|
|
13139
|
+
*
|
|
13140
|
+
* Unlike the observation (telemetry, D8) this one is awaited, but it is never
|
|
13141
|
+
* a gate: a follow-up that throws is swallowed here, and the call's own result
|
|
13142
|
+
* or error is what the caller gets. Cost when nothing is registered: one
|
|
13143
|
+
* undefined check (D181).
|
|
13144
|
+
*/
|
|
13145
|
+
/** The one follow-up of this process (the hub's device-manager installs it). */
|
|
13146
|
+
var CommandFollowUpRegistry = class {
|
|
13147
|
+
handler = null;
|
|
13148
|
+
/** Install (or, with `null`, remove) the follow-up. Returns the uninstall. */
|
|
13149
|
+
set(handler) {
|
|
13150
|
+
this.handler = handler;
|
|
13151
|
+
return () => {
|
|
13152
|
+
if (this.handler === handler) this.handler = null;
|
|
13153
|
+
};
|
|
13154
|
+
}
|
|
13155
|
+
/** Run the follow-up for a settled command. Never throws. */
|
|
13156
|
+
async run(target, outcome) {
|
|
13157
|
+
const handler = this.handler;
|
|
13158
|
+
if (handler === null) return;
|
|
13159
|
+
try {
|
|
13160
|
+
await handler(target, outcome);
|
|
13161
|
+
} catch {}
|
|
13162
|
+
}
|
|
13163
|
+
};
|
|
13164
|
+
var registry$3;
|
|
13165
|
+
/** The process-wide registry. */
|
|
13166
|
+
function getCommandFollowUpRegistry() {
|
|
13167
|
+
registry$3 ??= new CommandFollowUpRegistry();
|
|
13168
|
+
return registry$3;
|
|
13169
|
+
}
|
|
13170
|
+
/** Test seam: forget the handler. */
|
|
13171
|
+
function __resetCommandFollowUpRegistryForTests() {
|
|
13172
|
+
registry$3 = void 0;
|
|
13173
|
+
}
|
|
13174
|
+
/** The target of a call when it is a device command naming a device, else `null`. */
|
|
13175
|
+
function commandFollowUpTarget(capName, method, deviceId) {
|
|
13176
|
+
if (deviceId === void 0 || !Number.isInteger(deviceId)) return null;
|
|
13177
|
+
if (!(0, _camstack_types_addon.isCommand)(capName, method)) return null;
|
|
13178
|
+
return {
|
|
13179
|
+
deviceId,
|
|
13180
|
+
capName,
|
|
13181
|
+
method
|
|
13182
|
+
};
|
|
13183
|
+
}
|
|
13184
|
+
/** The `deviceId` an args object names, or `undefined`. */
|
|
13185
|
+
function deviceIdOfArgs(args) {
|
|
13186
|
+
if (args === null || typeof args !== "object" || Array.isArray(args)) return void 0;
|
|
13187
|
+
const deviceId = Reflect.get(args, "deviceId");
|
|
13188
|
+
return typeof deviceId === "number" && Number.isInteger(deviceId) ? deviceId : void 0;
|
|
13189
|
+
}
|
|
13190
|
+
/**
|
|
13191
|
+
* Await `result`, then the follow-up, then answer with `result` — its value, or
|
|
13192
|
+
* its error rethrown. Without a follow-up or a target, `result` is returned
|
|
13193
|
+
* untouched.
|
|
13194
|
+
*/
|
|
13195
|
+
function withCommandFollowUp(followUp, target, result) {
|
|
13196
|
+
if (followUp === void 0 || target === null) return result;
|
|
13197
|
+
return settleThenFollowUp(followUp, target, result);
|
|
13198
|
+
}
|
|
13199
|
+
async function settleThenFollowUp(followUp, target, result) {
|
|
13200
|
+
let value;
|
|
13201
|
+
try {
|
|
13202
|
+
value = await result;
|
|
13203
|
+
} catch (err) {
|
|
13204
|
+
await runCommandFollowUp(followUp, target, "failed");
|
|
13205
|
+
throw err;
|
|
13206
|
+
}
|
|
13207
|
+
await runCommandFollowUp(followUp, target, "ok");
|
|
13208
|
+
return value;
|
|
13209
|
+
}
|
|
13210
|
+
/** Run `followUp` for a settled command, swallowing its failure. */
|
|
13211
|
+
async function runCommandFollowUp(followUp, target, outcome) {
|
|
13212
|
+
try {
|
|
13213
|
+
await followUp(target, outcome);
|
|
13214
|
+
} catch {}
|
|
13215
|
+
}
|
|
13216
|
+
//#endregion
|
|
12876
13217
|
//#region src/kernel/command-observation.ts
|
|
12877
13218
|
/** Fan-out of command observations to the subscribers of this process. */
|
|
12878
13219
|
var CommandObserverRegistry = class {
|
|
@@ -12893,15 +13234,15 @@ var CommandObserverRegistry = class {
|
|
|
12893
13234
|
} catch {}
|
|
12894
13235
|
}
|
|
12895
13236
|
};
|
|
12896
|
-
var registry;
|
|
13237
|
+
var registry$2;
|
|
12897
13238
|
/** The process-wide registry. */
|
|
12898
13239
|
function getCommandObserverRegistry() {
|
|
12899
|
-
registry ??= new CommandObserverRegistry();
|
|
12900
|
-
return registry;
|
|
13240
|
+
registry$2 ??= new CommandObserverRegistry();
|
|
13241
|
+
return registry$2;
|
|
12901
13242
|
}
|
|
12902
13243
|
/** Test seam: forget every subscriber. */
|
|
12903
13244
|
function __resetCommandObserverRegistryForTests() {
|
|
12904
|
-
registry = void 0;
|
|
13245
|
+
registry$2 = void 0;
|
|
12905
13246
|
}
|
|
12906
13247
|
function isRecord(value) {
|
|
12907
13248
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
@@ -13012,18 +13353,45 @@ function commandObserverLink(options) {
|
|
|
13012
13353
|
actor: addonCommandActor(options.callerAddonId, (0, _camstack_types_addon.readCommandOrigin)(op.context)),
|
|
13013
13354
|
outcome
|
|
13014
13355
|
});
|
|
13356
|
+
const target = commandFollowUpTarget(capName, method, deviceIdOfArgs(op.input));
|
|
13357
|
+
const followUp = options.followUp;
|
|
13358
|
+
if (followUp === void 0 || target === null) {
|
|
13359
|
+
const sub = downstream.subscribe({
|
|
13360
|
+
next: (value) => observer.next(value),
|
|
13361
|
+
error: (err) => {
|
|
13362
|
+
settled.reject(err);
|
|
13363
|
+
observer.error(err);
|
|
13364
|
+
},
|
|
13365
|
+
complete: () => {
|
|
13366
|
+
settled.resolve();
|
|
13367
|
+
observer.complete();
|
|
13368
|
+
}
|
|
13369
|
+
});
|
|
13370
|
+
return () => sub.unsubscribe();
|
|
13371
|
+
}
|
|
13372
|
+
const answered = [];
|
|
13373
|
+
let closed = false;
|
|
13015
13374
|
const sub = downstream.subscribe({
|
|
13016
|
-
next: (value) =>
|
|
13375
|
+
next: (value) => void answered.push(value),
|
|
13017
13376
|
error: (err) => {
|
|
13018
13377
|
settled.reject(err);
|
|
13019
|
-
|
|
13378
|
+
runCommandFollowUp(followUp, target, "failed").then(() => {
|
|
13379
|
+
if (!closed) observer.error(err);
|
|
13380
|
+
});
|
|
13020
13381
|
},
|
|
13021
13382
|
complete: () => {
|
|
13022
13383
|
settled.resolve();
|
|
13023
|
-
|
|
13384
|
+
runCommandFollowUp(followUp, target, "ok").then(() => {
|
|
13385
|
+
if (closed) return;
|
|
13386
|
+
for (const value of answered) observer.next(value);
|
|
13387
|
+
observer.complete();
|
|
13388
|
+
});
|
|
13024
13389
|
}
|
|
13025
13390
|
});
|
|
13026
|
-
return () =>
|
|
13391
|
+
return () => {
|
|
13392
|
+
closed = true;
|
|
13393
|
+
sub.unsubscribe();
|
|
13394
|
+
};
|
|
13027
13395
|
});
|
|
13028
13396
|
};
|
|
13029
13397
|
}
|
|
@@ -13081,6 +13449,629 @@ function runObservedCommand(sink, facts, call) {
|
|
|
13081
13449
|
throw err;
|
|
13082
13450
|
}
|
|
13083
13451
|
}
|
|
13452
|
+
//#endregion
|
|
13453
|
+
//#region src/kernel/command-hold-overrides.ts
|
|
13454
|
+
/** The source key of an in-process registration. */
|
|
13455
|
+
var IN_PROCESS_HOLD_SOURCE = "in-process";
|
|
13456
|
+
/** The source key of a forked child's registrations. */
|
|
13457
|
+
function childHoldSource(childId) {
|
|
13458
|
+
return `child:${childId}`;
|
|
13459
|
+
}
|
|
13460
|
+
/** The options a child's cap descriptors carry, one entry per device cap. */
|
|
13461
|
+
function childCommandHoldEntries(caps) {
|
|
13462
|
+
const out = [];
|
|
13463
|
+
for (const cap of caps) {
|
|
13464
|
+
if (cap.deviceId === void 0 || cap.commandHolds === void 0) continue;
|
|
13465
|
+
out.push({
|
|
13466
|
+
deviceId: cap.deviceId,
|
|
13467
|
+
capName: cap.capName,
|
|
13468
|
+
commands: cap.commandHolds
|
|
13469
|
+
});
|
|
13470
|
+
}
|
|
13471
|
+
return out;
|
|
13472
|
+
}
|
|
13473
|
+
function keyOf$1(deviceId, capName) {
|
|
13474
|
+
return `${deviceId}|${capName}`;
|
|
13475
|
+
}
|
|
13476
|
+
var CommandHoldOverrideRegistry = class {
|
|
13477
|
+
bySource = /* @__PURE__ */ new Map();
|
|
13478
|
+
/** Record (or replace) one device cap's options from `source`. */
|
|
13479
|
+
set(source, entry) {
|
|
13480
|
+
const perSource = this.bySource.get(source) ?? /* @__PURE__ */ new Map();
|
|
13481
|
+
perSource.set(keyOf$1(entry.deviceId, entry.capName), entry.commands);
|
|
13482
|
+
this.bySource.set(source, perSource);
|
|
13483
|
+
}
|
|
13484
|
+
/** Forget one device cap's options from `source`. */
|
|
13485
|
+
delete(source, deviceId, capName) {
|
|
13486
|
+
const perSource = this.bySource.get(source);
|
|
13487
|
+
if (perSource === void 0) return;
|
|
13488
|
+
perSource.delete(keyOf$1(deviceId, capName));
|
|
13489
|
+
if (perSource.size === 0) this.bySource.delete(source);
|
|
13490
|
+
}
|
|
13491
|
+
/** Replace everything `source` reported with `entries` (an empty list forgets the source). */
|
|
13492
|
+
replaceSource(source, entries) {
|
|
13493
|
+
if (entries.length === 0) {
|
|
13494
|
+
this.bySource.delete(source);
|
|
13495
|
+
return;
|
|
13496
|
+
}
|
|
13497
|
+
this.bySource.set(source, new Map(entries.map((e) => [keyOf$1(e.deviceId, e.capName), e.commands])));
|
|
13498
|
+
}
|
|
13499
|
+
/** The options for a device cap, from whichever source registered it; `undefined` = default. */
|
|
13500
|
+
get(deviceId, capName) {
|
|
13501
|
+
const key = keyOf$1(deviceId, capName);
|
|
13502
|
+
for (const perSource of this.bySource.values()) {
|
|
13503
|
+
const found = perSource.get(key);
|
|
13504
|
+
if (found !== void 0) return found;
|
|
13505
|
+
}
|
|
13506
|
+
}
|
|
13507
|
+
};
|
|
13508
|
+
var registry$1;
|
|
13509
|
+
/** The process-wide registry. */
|
|
13510
|
+
function getCommandHoldOverrideRegistry() {
|
|
13511
|
+
registry$1 ??= new CommandHoldOverrideRegistry();
|
|
13512
|
+
return registry$1;
|
|
13513
|
+
}
|
|
13514
|
+
//#endregion
|
|
13515
|
+
//#region src/builtins/device-manager/command-holds-wiring.ts
|
|
13516
|
+
/**
|
|
13517
|
+
* Arms the command hold (D1061) from the D1022 command seams.
|
|
13518
|
+
*
|
|
13519
|
+
* Every device command the hub sees — its tRPC mount, the UDS parent for a
|
|
13520
|
+
* forked addon's `ctx.api`, an in-process addon's `ctx.api` — reaches the
|
|
13521
|
+
* process-wide observer registry BEFORE the call runs. Here each one resolves
|
|
13522
|
+
* what it leads to (the vocabulary's effects under the provider's option) on
|
|
13523
|
+
* the slice the device last reported, and the mirror holds it; a refused call
|
|
13524
|
+
* rolls the hold back at once.
|
|
13525
|
+
*
|
|
13526
|
+
* Telemetry in, never a gate (D8): a throw here costs the hold, never the call.
|
|
13527
|
+
*/
|
|
13528
|
+
/** Subscribe the hold to the command observations; returns the unsubscribe. */
|
|
13529
|
+
function startCommandHolds(deps) {
|
|
13530
|
+
const optionsFor = deps.optionsFor ?? ((deviceId, capName) => getCommandHoldOverrideRegistry().get(deviceId, capName));
|
|
13531
|
+
const unsubscribe = getCommandObserverRegistry().subscribe((obs) => {
|
|
13532
|
+
try {
|
|
13533
|
+
const current = deps.mirror.capSlice(obs.deviceId, obs.capName, "confirmed") ?? void 0;
|
|
13534
|
+
const shown = deps.mirror.capSlice(obs.deviceId, obs.capName) ?? void 0;
|
|
13535
|
+
const option = optionsFor(obs.deviceId, obs.capName)?.[obs.method];
|
|
13536
|
+
const hold = require_dist.resolveCommandHold(obs.capName, obs.method, obs.args, current, option, shown);
|
|
13537
|
+
if (hold === null) return;
|
|
13538
|
+
const token = deps.mirror.holdCommand({
|
|
13539
|
+
deviceId: obs.deviceId,
|
|
13540
|
+
capName: obs.capName,
|
|
13541
|
+
method: obs.method,
|
|
13542
|
+
effects: hold.effects,
|
|
13543
|
+
holdMs: hold.holdMs
|
|
13544
|
+
});
|
|
13545
|
+
if (token === null) return;
|
|
13546
|
+
obs.outcome?.catch(() => deps.mirror.releaseCommand(token, "refused"));
|
|
13547
|
+
} catch (err) {
|
|
13548
|
+
deps.logger.error(`command hold: arming ${obs.capName}.${obs.method} failed: ${err instanceof Error ? err.message : String(err)}`, { tags: { deviceId: obs.deviceId } });
|
|
13549
|
+
}
|
|
13550
|
+
});
|
|
13551
|
+
return () => {
|
|
13552
|
+
unsubscribe();
|
|
13553
|
+
deps.mirror.disposeHolds();
|
|
13554
|
+
};
|
|
13555
|
+
}
|
|
13556
|
+
//#endregion
|
|
13557
|
+
//#region src/kernel/state-refresh-policies.ts
|
|
13558
|
+
/** The cap a policy is stated on. */
|
|
13559
|
+
var STATE_REFRESH_CAP = "device-state-refresh";
|
|
13560
|
+
/** The source key of an in-process registration. */
|
|
13561
|
+
var IN_PROCESS_REFRESH_SOURCE = "in-process";
|
|
13562
|
+
/** The source key of a forked child's registrations. */
|
|
13563
|
+
function childRefreshSource(childId) {
|
|
13564
|
+
return `child:${childId}`;
|
|
13565
|
+
}
|
|
13566
|
+
/** The policies a child's cap descriptors carry, one entry per device. */
|
|
13567
|
+
function childStateRefreshEntries(childId, caps) {
|
|
13568
|
+
const out = [];
|
|
13569
|
+
for (const cap of caps) {
|
|
13570
|
+
if (cap.capName !== "device-state-refresh") continue;
|
|
13571
|
+
if (cap.deviceId === void 0 || cap.stateRefresh === void 0) continue;
|
|
13572
|
+
out.push({
|
|
13573
|
+
deviceId: cap.deviceId,
|
|
13574
|
+
addonId: cap.addonId ?? childId,
|
|
13575
|
+
policy: cap.stateRefresh
|
|
13576
|
+
});
|
|
13577
|
+
}
|
|
13578
|
+
return out;
|
|
13579
|
+
}
|
|
13580
|
+
var StateRefreshPolicyRegistry = class {
|
|
13581
|
+
bySource = /* @__PURE__ */ new Map();
|
|
13582
|
+
listeners = /* @__PURE__ */ new Set();
|
|
13583
|
+
/** Record (or replace) one device's policy from `source`. */
|
|
13584
|
+
set(source, entry) {
|
|
13585
|
+
const perSource = new Map(this.bySource.get(source) ?? []);
|
|
13586
|
+
perSource.set(entry.deviceId, entry);
|
|
13587
|
+
this.bySource.set(source, perSource);
|
|
13588
|
+
this.changed();
|
|
13589
|
+
}
|
|
13590
|
+
/** Forget one device's policy from `source`. */
|
|
13591
|
+
delete(source, deviceId) {
|
|
13592
|
+
const perSource = this.bySource.get(source);
|
|
13593
|
+
if (perSource === void 0 || !perSource.has(deviceId)) return;
|
|
13594
|
+
const next = new Map(perSource);
|
|
13595
|
+
next.delete(deviceId);
|
|
13596
|
+
if (next.size === 0) this.bySource.delete(source);
|
|
13597
|
+
else this.bySource.set(source, next);
|
|
13598
|
+
this.changed();
|
|
13599
|
+
}
|
|
13600
|
+
/** Replace everything `source` reported with `entries` (an empty list forgets the source). */
|
|
13601
|
+
replaceSource(source, entries) {
|
|
13602
|
+
const had = this.bySource.has(source);
|
|
13603
|
+
if (entries.length === 0) {
|
|
13604
|
+
this.bySource.delete(source);
|
|
13605
|
+
if (had) this.changed();
|
|
13606
|
+
return;
|
|
13607
|
+
}
|
|
13608
|
+
this.bySource.set(source, new Map(entries.map((e) => [e.deviceId, e])));
|
|
13609
|
+
this.changed();
|
|
13610
|
+
}
|
|
13611
|
+
/** Every device's policy (a device registered by two sources keeps the first). */
|
|
13612
|
+
entries() {
|
|
13613
|
+
const seen = /* @__PURE__ */ new Map();
|
|
13614
|
+
for (const perSource of this.bySource.values()) for (const entry of perSource.values()) if (!seen.has(entry.deviceId)) seen.set(entry.deviceId, entry);
|
|
13615
|
+
return [...seen.values()];
|
|
13616
|
+
}
|
|
13617
|
+
/** One device's policy, or `undefined` when its provider did not migrate. */
|
|
13618
|
+
get(deviceId) {
|
|
13619
|
+
for (const perSource of this.bySource.values()) {
|
|
13620
|
+
const found = perSource.get(deviceId);
|
|
13621
|
+
if (found !== void 0) return found;
|
|
13622
|
+
}
|
|
13623
|
+
}
|
|
13624
|
+
subscribe(listener) {
|
|
13625
|
+
this.listeners.add(listener);
|
|
13626
|
+
return () => {
|
|
13627
|
+
this.listeners.delete(listener);
|
|
13628
|
+
};
|
|
13629
|
+
}
|
|
13630
|
+
changed() {
|
|
13631
|
+
for (const listener of this.listeners) try {
|
|
13632
|
+
listener();
|
|
13633
|
+
} catch {}
|
|
13634
|
+
}
|
|
13635
|
+
};
|
|
13636
|
+
var registry;
|
|
13637
|
+
/** The process-wide registry. */
|
|
13638
|
+
function getStateRefreshPolicyRegistry() {
|
|
13639
|
+
registry ??= new StateRefreshPolicyRegistry();
|
|
13640
|
+
return registry;
|
|
13641
|
+
}
|
|
13642
|
+
/** Test seam: forget every policy. */
|
|
13643
|
+
function __resetStateRefreshPolicyRegistryForTests() {
|
|
13644
|
+
registry = void 0;
|
|
13645
|
+
}
|
|
13646
|
+
//#endregion
|
|
13647
|
+
//#region src/builtins/device-manager/state-refresh-scheduler.ts
|
|
13648
|
+
/**
|
|
13649
|
+
* The state-refresh scheduler (D1062, amending D224): the core decides WHEN a
|
|
13650
|
+
* migrated provider re-reads a device; the provider only knows HOW.
|
|
13651
|
+
*
|
|
13652
|
+
* The policy each provider declared on its `device-state-refresh`
|
|
13653
|
+
* registration (`StateRefreshPolicyRegistry`) drives it. Devices are grouped
|
|
13654
|
+
* per integration instance (`addonId::integrationId`):
|
|
13655
|
+
*
|
|
13656
|
+
* - cadence: `effectiveRefreshIntervalMs` — the declared minimum, slowed (never
|
|
13657
|
+
* sped up) by the integration's own setting, no idle back-off;
|
|
13658
|
+
* `push: 'full'` is never scheduled. A batching integration (`batch: true`)
|
|
13659
|
+
* has ONE schedule for the group — one due time, one read of every device
|
|
13660
|
+
* per interval; a device that registers later joins the group's next tick.
|
|
13661
|
+
* A non-batching integration keeps one schedule per device;
|
|
13662
|
+
* - command (`onCommand`, awaited by the D1022 seams): a polled device is read
|
|
13663
|
+
* at once and the command returns after it, then read again at ~3 s and
|
|
13664
|
+
* ~10 s while a D1061 hold on it is still pending; a push device waits
|
|
13665
|
+
* `PUSH_CONFIRM_WINDOW_MS` for the confirming push and reads once if none
|
|
13666
|
+
* came. These reads are per device and never move a batched group's schedule;
|
|
13667
|
+
* - push health: silence past `heartbeatMs` reads the integration, asks it to
|
|
13668
|
+
* resubscribe and reads again; a new `connectionEpoch` reads everything.
|
|
13669
|
+
*
|
|
13670
|
+
* Reads of one device are serialized: each starts after the previous one on
|
|
13671
|
+
* that device ended. A scheduled tick that finds its last read still running
|
|
13672
|
+
* is skipped (debug line); a command waits for the read in flight at most
|
|
13673
|
+
* `COMMAND_WAIT_BOUND_MS`, then reads its own device (debug line). A refresh
|
|
13674
|
+
* failure is a warn line per device and never fails a command.
|
|
13675
|
+
*/
|
|
13676
|
+
/** The longest a command waits for a read already in flight on its device. */
|
|
13677
|
+
var COMMAND_WAIT_BOUND_MS = 5e3;
|
|
13678
|
+
function groupKeyOf(entry) {
|
|
13679
|
+
return `${entry.addonId}::${entry.policy.integrationId}`;
|
|
13680
|
+
}
|
|
13681
|
+
function errorMessage$1(err) {
|
|
13682
|
+
return err instanceof Error ? err.message : String(err);
|
|
13683
|
+
}
|
|
13684
|
+
var StateRefreshScheduler = class {
|
|
13685
|
+
deps;
|
|
13686
|
+
devices = /* @__PURE__ */ new Map();
|
|
13687
|
+
groups = /* @__PURE__ */ new Map();
|
|
13688
|
+
unsubscribe;
|
|
13689
|
+
disposed = false;
|
|
13690
|
+
constructor(deps) {
|
|
13691
|
+
this.deps = deps;
|
|
13692
|
+
this.unsubscribe = deps.policies.subscribe(() => this.reconcile());
|
|
13693
|
+
this.reconcile();
|
|
13694
|
+
}
|
|
13695
|
+
/** The command follow-up (awaited by the D1022 seams; never throws). */
|
|
13696
|
+
onCommand = async (target, outcome) => {
|
|
13697
|
+
const device = this.devices.get(target.deviceId);
|
|
13698
|
+
if (device === void 0 || this.disposed) return;
|
|
13699
|
+
device.lastCommandAt = Date.now();
|
|
13700
|
+
this.cancelFollowUps(device);
|
|
13701
|
+
const { policy } = device.entry;
|
|
13702
|
+
if (policy.push !== "none") {
|
|
13703
|
+
this.armPushWindow(target.deviceId, device);
|
|
13704
|
+
return;
|
|
13705
|
+
}
|
|
13706
|
+
this.deps.logger.debug("state refresh after command", {
|
|
13707
|
+
tags: { deviceId: target.deviceId },
|
|
13708
|
+
meta: {
|
|
13709
|
+
capName: target.capName,
|
|
13710
|
+
method: target.method,
|
|
13711
|
+
outcome
|
|
13712
|
+
}
|
|
13713
|
+
});
|
|
13714
|
+
await this.runRefresh([target.deviceId], "command", COMMAND_WAIT_BOUND_MS);
|
|
13715
|
+
if (!policy.batch) {
|
|
13716
|
+
device.dueAt = Date.now() + require_dist.effectiveRefreshIntervalMs(policy);
|
|
13717
|
+
this.armSchedule(groupKeyOf(device.entry));
|
|
13718
|
+
}
|
|
13719
|
+
this.armBurst(target.deviceId, device);
|
|
13720
|
+
};
|
|
13721
|
+
/** A state report reached the mirror for `deviceId` (push or read). */
|
|
13722
|
+
onReport(deviceId) {
|
|
13723
|
+
const device = this.devices.get(deviceId);
|
|
13724
|
+
if (device === void 0) return;
|
|
13725
|
+
const now = Date.now();
|
|
13726
|
+
device.lastReportAt = now;
|
|
13727
|
+
const group = this.groups.get(groupKeyOf(device.entry));
|
|
13728
|
+
if (group !== void 0) group.lastReportAt = now;
|
|
13729
|
+
}
|
|
13730
|
+
dispose() {
|
|
13731
|
+
this.disposed = true;
|
|
13732
|
+
this.unsubscribe();
|
|
13733
|
+
for (const device of this.devices.values()) this.cancelFollowUps(device);
|
|
13734
|
+
for (const group of this.groups.values()) this.clearGroupTimers(group);
|
|
13735
|
+
this.devices.clear();
|
|
13736
|
+
this.groups.clear();
|
|
13737
|
+
}
|
|
13738
|
+
reconcile() {
|
|
13739
|
+
if (this.disposed) return;
|
|
13740
|
+
const now = Date.now();
|
|
13741
|
+
const current = new Map(this.deps.policies.entries().map((e) => [e.deviceId, e]));
|
|
13742
|
+
for (const [deviceId, device] of this.devices) {
|
|
13743
|
+
if (current.has(deviceId)) continue;
|
|
13744
|
+
this.cancelFollowUps(device);
|
|
13745
|
+
this.devices.delete(deviceId);
|
|
13746
|
+
}
|
|
13747
|
+
const reconnected = /* @__PURE__ */ new Set();
|
|
13748
|
+
for (const entry of current.values()) {
|
|
13749
|
+
const existing = this.devices.get(entry.deviceId);
|
|
13750
|
+
const interval = require_dist.effectiveRefreshIntervalMs(entry.policy);
|
|
13751
|
+
if (existing === void 0) this.devices.set(entry.deviceId, {
|
|
13752
|
+
entry,
|
|
13753
|
+
dueAt: now + interval,
|
|
13754
|
+
lastReportAt: now,
|
|
13755
|
+
lastCommandAt: 0,
|
|
13756
|
+
inFlight: null,
|
|
13757
|
+
scheduledInFlight: null,
|
|
13758
|
+
burst: [],
|
|
13759
|
+
pushWindow: null
|
|
13760
|
+
});
|
|
13761
|
+
else {
|
|
13762
|
+
if (require_dist.effectiveRefreshIntervalMs(existing.entry.policy) !== interval) existing.dueAt = now + interval;
|
|
13763
|
+
existing.entry = entry;
|
|
13764
|
+
}
|
|
13765
|
+
if (this.noteEpoch(entry)) reconnected.add(groupKeyOf(entry));
|
|
13766
|
+
}
|
|
13767
|
+
this.rebuildGroups(now);
|
|
13768
|
+
for (const key of reconnected) {
|
|
13769
|
+
this.deps.logger.info("state refresh: integration reconnected, reading every device", { meta: { group: key } });
|
|
13770
|
+
this.refreshGroup(key, "schedule");
|
|
13771
|
+
}
|
|
13772
|
+
}
|
|
13773
|
+
/** Track the group's connection epoch; `true` when it moved past a known one. */
|
|
13774
|
+
noteEpoch(entry) {
|
|
13775
|
+
const epoch = entry.policy.connectionEpoch;
|
|
13776
|
+
if (epoch === void 0) return false;
|
|
13777
|
+
const key = groupKeyOf(entry);
|
|
13778
|
+
const group = this.groups.get(key);
|
|
13779
|
+
if (group === void 0) return false;
|
|
13780
|
+
const previous = group.epoch;
|
|
13781
|
+
if (previous === void 0 || epoch > previous) group.epoch = epoch;
|
|
13782
|
+
return previous !== void 0 && epoch > previous;
|
|
13783
|
+
}
|
|
13784
|
+
rebuildGroups(now) {
|
|
13785
|
+
const members = /* @__PURE__ */ new Map();
|
|
13786
|
+
for (const [deviceId, device] of this.devices) {
|
|
13787
|
+
const key = groupKeyOf(device.entry);
|
|
13788
|
+
const set = members.get(key) ?? /* @__PURE__ */ new Set();
|
|
13789
|
+
set.add(deviceId);
|
|
13790
|
+
members.set(key, set);
|
|
13791
|
+
}
|
|
13792
|
+
for (const [key, group] of this.groups) {
|
|
13793
|
+
if (members.has(key)) continue;
|
|
13794
|
+
this.clearGroupTimers(group);
|
|
13795
|
+
this.groups.delete(key);
|
|
13796
|
+
}
|
|
13797
|
+
for (const [key, devices] of members) {
|
|
13798
|
+
const existing = this.groups.get(key);
|
|
13799
|
+
if (existing === void 0) {
|
|
13800
|
+
const first = this.devices.get([...devices][0] ?? -1);
|
|
13801
|
+
const intervalMs = first === void 0 ? 0 : require_dist.effectiveRefreshIntervalMs(first.entry.policy);
|
|
13802
|
+
this.groups.set(key, {
|
|
13803
|
+
key,
|
|
13804
|
+
devices,
|
|
13805
|
+
dueAt: now + intervalMs,
|
|
13806
|
+
intervalMs,
|
|
13807
|
+
scheduleTimer: null,
|
|
13808
|
+
heartbeatTimer: null,
|
|
13809
|
+
lastReportAt: now,
|
|
13810
|
+
epoch: first?.entry.policy.connectionEpoch
|
|
13811
|
+
});
|
|
13812
|
+
} else {
|
|
13813
|
+
existing.devices = devices;
|
|
13814
|
+
const policy = this.groupPolicy(existing);
|
|
13815
|
+
const intervalMs = policy === void 0 ? 0 : require_dist.effectiveRefreshIntervalMs(policy);
|
|
13816
|
+
if (intervalMs !== existing.intervalMs) {
|
|
13817
|
+
existing.intervalMs = intervalMs;
|
|
13818
|
+
existing.dueAt = now + intervalMs;
|
|
13819
|
+
}
|
|
13820
|
+
}
|
|
13821
|
+
this.armSchedule(key);
|
|
13822
|
+
this.armHeartbeat(key);
|
|
13823
|
+
}
|
|
13824
|
+
}
|
|
13825
|
+
groupPolicy(group) {
|
|
13826
|
+
for (const deviceId of group.devices) {
|
|
13827
|
+
const device = this.devices.get(deviceId);
|
|
13828
|
+
if (device !== void 0) return device.entry.policy;
|
|
13829
|
+
}
|
|
13830
|
+
}
|
|
13831
|
+
armSchedule(key) {
|
|
13832
|
+
const group = this.groups.get(key);
|
|
13833
|
+
if (group === void 0 || this.disposed) return;
|
|
13834
|
+
if (group.scheduleTimer !== null) clearTimeout(group.scheduleTimer);
|
|
13835
|
+
group.scheduleTimer = null;
|
|
13836
|
+
const policy = this.groupPolicy(group);
|
|
13837
|
+
if (policy === void 0 || policy.push === "full") return;
|
|
13838
|
+
let earliest = Number.POSITIVE_INFINITY;
|
|
13839
|
+
if (policy.batch) earliest = group.dueAt;
|
|
13840
|
+
else for (const deviceId of group.devices) {
|
|
13841
|
+
const device = this.devices.get(deviceId);
|
|
13842
|
+
if (device !== void 0) earliest = Math.min(earliest, device.dueAt);
|
|
13843
|
+
}
|
|
13844
|
+
if (!Number.isFinite(earliest)) return;
|
|
13845
|
+
group.scheduleTimer = setTimeout(() => this.tick(key), Math.max(0, earliest - Date.now()));
|
|
13846
|
+
}
|
|
13847
|
+
tick(key) {
|
|
13848
|
+
const group = this.groups.get(key);
|
|
13849
|
+
if (group === void 0 || this.disposed) return;
|
|
13850
|
+
group.scheduleTimer = null;
|
|
13851
|
+
const policy = this.groupPolicy(group);
|
|
13852
|
+
if (policy?.batch === true) this.tickBatched(group, policy);
|
|
13853
|
+
else this.tickPerDevice(group);
|
|
13854
|
+
this.armSchedule(key);
|
|
13855
|
+
}
|
|
13856
|
+
/** One read of the whole group per interval, unless the last one still runs. */
|
|
13857
|
+
tickBatched(group, policy) {
|
|
13858
|
+
const now = Date.now();
|
|
13859
|
+
if (group.dueAt > now) return;
|
|
13860
|
+
group.dueAt = now + require_dist.effectiveRefreshIntervalMs(policy);
|
|
13861
|
+
const ids = [...group.devices].filter((id) => this.devices.has(id));
|
|
13862
|
+
const running = ids.filter((id) => (this.devices.get(id)?.scheduledInFlight ?? null) !== null);
|
|
13863
|
+
if (running.length > 0) {
|
|
13864
|
+
for (const deviceId of running) this.logSkippedTick(deviceId);
|
|
13865
|
+
return;
|
|
13866
|
+
}
|
|
13867
|
+
if (ids.length > 0) this.runScheduled(ids);
|
|
13868
|
+
}
|
|
13869
|
+
tickPerDevice(group) {
|
|
13870
|
+
const now = Date.now();
|
|
13871
|
+
const due = [];
|
|
13872
|
+
for (const deviceId of group.devices) {
|
|
13873
|
+
const device = this.devices.get(deviceId);
|
|
13874
|
+
if (device === void 0 || device.dueAt > now) continue;
|
|
13875
|
+
device.dueAt = now + require_dist.effectiveRefreshIntervalMs(device.entry.policy);
|
|
13876
|
+
if (device.inFlight !== null) {
|
|
13877
|
+
this.logSkippedTick(deviceId);
|
|
13878
|
+
continue;
|
|
13879
|
+
}
|
|
13880
|
+
due.push(deviceId);
|
|
13881
|
+
}
|
|
13882
|
+
for (const deviceId of due) this.runScheduled([deviceId]);
|
|
13883
|
+
}
|
|
13884
|
+
logSkippedTick(deviceId) {
|
|
13885
|
+
this.deps.logger.debug("state refresh skipped: a refresh is still in flight", { tags: { deviceId } });
|
|
13886
|
+
}
|
|
13887
|
+
/** A scheduled read, remembered on each device until it settles. */
|
|
13888
|
+
async runScheduled(deviceIds) {
|
|
13889
|
+
const run = this.runRefresh(deviceIds, "schedule");
|
|
13890
|
+
for (const deviceId of deviceIds) {
|
|
13891
|
+
const device = this.devices.get(deviceId);
|
|
13892
|
+
if (device !== void 0) device.scheduledInFlight = run;
|
|
13893
|
+
}
|
|
13894
|
+
await run;
|
|
13895
|
+
for (const deviceId of deviceIds) {
|
|
13896
|
+
const device = this.devices.get(deviceId);
|
|
13897
|
+
if (device !== void 0 && device.scheduledInFlight === run) device.scheduledInFlight = null;
|
|
13898
|
+
}
|
|
13899
|
+
}
|
|
13900
|
+
armBurst(deviceId, device) {
|
|
13901
|
+
if (!this.deps.holdsPending(deviceId)) return;
|
|
13902
|
+
const timers = [];
|
|
13903
|
+
let stopped = false;
|
|
13904
|
+
for (const offset of require_dist.COMMAND_BURST_OFFSETS_MS) timers.push(setTimeout(() => {
|
|
13905
|
+
if (stopped || this.disposed) return;
|
|
13906
|
+
if (!this.deps.holdsPending(deviceId)) {
|
|
13907
|
+
stopped = true;
|
|
13908
|
+
this.deps.logger.debug("state refresh burst stopped: the command is confirmed", { tags: { deviceId } });
|
|
13909
|
+
return;
|
|
13910
|
+
}
|
|
13911
|
+
if (device.inFlight !== null) {
|
|
13912
|
+
this.deps.logger.debug("state refresh burst skipped: a refresh is still in flight", { tags: { deviceId } });
|
|
13913
|
+
return;
|
|
13914
|
+
}
|
|
13915
|
+
this.runRefresh([deviceId], "burst");
|
|
13916
|
+
}, offset));
|
|
13917
|
+
device.burst = timers;
|
|
13918
|
+
}
|
|
13919
|
+
armPushWindow(deviceId, device) {
|
|
13920
|
+
const commandAt = device.lastCommandAt;
|
|
13921
|
+
device.pushWindow = setTimeout(() => {
|
|
13922
|
+
device.pushWindow = null;
|
|
13923
|
+
if (this.disposed) return;
|
|
13924
|
+
if (device.lastReportAt > commandAt && !this.deps.holdsPending(deviceId)) {
|
|
13925
|
+
this.deps.logger.debug("state refresh not needed: the push confirmed the command", { tags: { deviceId } });
|
|
13926
|
+
return;
|
|
13927
|
+
}
|
|
13928
|
+
this.deps.logger.debug("state refresh: no confirming push within the window", {
|
|
13929
|
+
tags: { deviceId },
|
|
13930
|
+
meta: { windowMs: require_dist.PUSH_CONFIRM_WINDOW_MS }
|
|
13931
|
+
});
|
|
13932
|
+
if (device.inFlight !== null) return;
|
|
13933
|
+
this.runRefresh([deviceId], "command");
|
|
13934
|
+
}, require_dist.PUSH_CONFIRM_WINDOW_MS);
|
|
13935
|
+
}
|
|
13936
|
+
cancelFollowUps(device) {
|
|
13937
|
+
for (const timer of device.burst) clearTimeout(timer);
|
|
13938
|
+
device.burst = [];
|
|
13939
|
+
if (device.pushWindow !== null) clearTimeout(device.pushWindow);
|
|
13940
|
+
device.pushWindow = null;
|
|
13941
|
+
}
|
|
13942
|
+
armHeartbeat(key) {
|
|
13943
|
+
const group = this.groups.get(key);
|
|
13944
|
+
if (group === void 0 || this.disposed) return;
|
|
13945
|
+
if (group.heartbeatTimer !== null) clearTimeout(group.heartbeatTimer);
|
|
13946
|
+
group.heartbeatTimer = null;
|
|
13947
|
+
const policy = this.groupPolicy(group);
|
|
13948
|
+
if (policy === void 0 || policy.push === "none" || policy.heartbeatMs === void 0) return;
|
|
13949
|
+
const heartbeatMs = policy.heartbeatMs;
|
|
13950
|
+
const wait = Math.max(0, group.lastReportAt + heartbeatMs - Date.now());
|
|
13951
|
+
group.heartbeatTimer = setTimeout(() => {
|
|
13952
|
+
group.heartbeatTimer = null;
|
|
13953
|
+
if (this.disposed) return;
|
|
13954
|
+
if (Date.now() - group.lastReportAt >= heartbeatMs) this.onSilence(key, heartbeatMs);
|
|
13955
|
+
else this.armHeartbeat(key);
|
|
13956
|
+
}, wait);
|
|
13957
|
+
}
|
|
13958
|
+
async onSilence(key, heartbeatMs) {
|
|
13959
|
+
const group = this.groups.get(key);
|
|
13960
|
+
if (group === void 0) return;
|
|
13961
|
+
group.lastReportAt = Date.now();
|
|
13962
|
+
for (const deviceId of group.devices) this.deps.logger.warn("state refresh: no push within the heartbeat, reading and resubscribing", {
|
|
13963
|
+
tags: { deviceId },
|
|
13964
|
+
meta: { heartbeatMs }
|
|
13965
|
+
});
|
|
13966
|
+
await this.refreshGroup(key, "schedule");
|
|
13967
|
+
const route = [...group.devices][0];
|
|
13968
|
+
if (route !== void 0) try {
|
|
13969
|
+
await this.deps.resubscribe(route);
|
|
13970
|
+
} catch (err) {
|
|
13971
|
+
this.deps.logger.warn("state refresh: resubscribe failed", {
|
|
13972
|
+
tags: { deviceId: route },
|
|
13973
|
+
meta: { error: errorMessage$1(err) }
|
|
13974
|
+
});
|
|
13975
|
+
}
|
|
13976
|
+
await this.refreshGroup(key, "schedule");
|
|
13977
|
+
this.armHeartbeat(key);
|
|
13978
|
+
}
|
|
13979
|
+
async refreshGroup(key, reason) {
|
|
13980
|
+
const group = this.groups.get(key);
|
|
13981
|
+
if (group === void 0) return;
|
|
13982
|
+
const ids = [...group.devices].filter((id) => this.devices.has(id));
|
|
13983
|
+
if (ids.length > 0) await this.refreshDevices(ids, reason);
|
|
13984
|
+
}
|
|
13985
|
+
/** One call for a batching integration, else one per device. */
|
|
13986
|
+
async refreshDevices(deviceIds, reason) {
|
|
13987
|
+
if (this.devices.get(deviceIds[0] ?? -1)?.entry.policy.batch === true) {
|
|
13988
|
+
await this.runRefresh(deviceIds, reason);
|
|
13989
|
+
return;
|
|
13990
|
+
}
|
|
13991
|
+
await Promise.all(deviceIds.map((id) => this.runRefresh([id], reason)));
|
|
13992
|
+
}
|
|
13993
|
+
/**
|
|
13994
|
+
* Read `deviceIds` once every read already queued on them ended — or, with
|
|
13995
|
+
* `waitBoundMs`, once that long passed (a debug line per device that was
|
|
13996
|
+
* still busy). The read becomes the devices' new tail.
|
|
13997
|
+
*/
|
|
13998
|
+
async runRefresh(deviceIds, reason, waitBoundMs) {
|
|
13999
|
+
const route = deviceIds[0];
|
|
14000
|
+
if (route === void 0) return;
|
|
14001
|
+
const busy = [];
|
|
14002
|
+
const prior = [];
|
|
14003
|
+
for (const deviceId of deviceIds) {
|
|
14004
|
+
const tail = this.devices.get(deviceId)?.inFlight;
|
|
14005
|
+
if (tail === null || tail === void 0) continue;
|
|
14006
|
+
busy.push(deviceId);
|
|
14007
|
+
prior.push(tail);
|
|
14008
|
+
}
|
|
14009
|
+
const settled = this.afterPrior(prior, busy, waitBoundMs).then(() => this.deps.refresh(route, deviceIds, reason)).catch((err) => {
|
|
14010
|
+
for (const deviceId of deviceIds) this.deps.logger.warn("state refresh failed", {
|
|
14011
|
+
tags: { deviceId },
|
|
14012
|
+
meta: {
|
|
14013
|
+
reason,
|
|
14014
|
+
error: errorMessage$1(err)
|
|
14015
|
+
}
|
|
14016
|
+
});
|
|
14017
|
+
}).finally(() => {
|
|
14018
|
+
for (const deviceId of deviceIds) {
|
|
14019
|
+
const device = this.devices.get(deviceId);
|
|
14020
|
+
if (device !== void 0 && device.inFlight === settled) device.inFlight = null;
|
|
14021
|
+
}
|
|
14022
|
+
});
|
|
14023
|
+
for (const deviceId of deviceIds) {
|
|
14024
|
+
const device = this.devices.get(deviceId);
|
|
14025
|
+
if (device !== void 0) device.inFlight = settled;
|
|
14026
|
+
}
|
|
14027
|
+
await settled;
|
|
14028
|
+
}
|
|
14029
|
+
/** Resolves when every `prior` read ended, or after `waitBoundMs` (logged per busy device). */
|
|
14030
|
+
async afterPrior(prior, busy, waitBoundMs) {
|
|
14031
|
+
if (prior.length === 0) return;
|
|
14032
|
+
const all = Promise.all(prior).then(() => true);
|
|
14033
|
+
if (waitBoundMs === void 0) {
|
|
14034
|
+
await all;
|
|
14035
|
+
return;
|
|
14036
|
+
}
|
|
14037
|
+
let timer;
|
|
14038
|
+
const bound = new Promise((resolve) => {
|
|
14039
|
+
timer = setTimeout(() => resolve(false), waitBoundMs);
|
|
14040
|
+
});
|
|
14041
|
+
const ended = await Promise.race([all, bound]);
|
|
14042
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
14043
|
+
if (ended) return;
|
|
14044
|
+
for (const deviceId of busy) this.deps.logger.debug("state refresh: stopped waiting for the read in flight", {
|
|
14045
|
+
tags: { deviceId },
|
|
14046
|
+
meta: { waitedMs: waitBoundMs }
|
|
14047
|
+
});
|
|
14048
|
+
}
|
|
14049
|
+
clearGroupTimers(group) {
|
|
14050
|
+
if (group.scheduleTimer !== null) clearTimeout(group.scheduleTimer);
|
|
14051
|
+
if (group.heartbeatTimer !== null) clearTimeout(group.heartbeatTimer);
|
|
14052
|
+
group.scheduleTimer = null;
|
|
14053
|
+
group.heartbeatTimer = null;
|
|
14054
|
+
}
|
|
14055
|
+
};
|
|
14056
|
+
//#endregion
|
|
14057
|
+
//#region src/builtins/device-manager/state-refresh-wiring.ts
|
|
14058
|
+
/** Start the scheduler; returns the stop. */
|
|
14059
|
+
function startStateRefresh(deps) {
|
|
14060
|
+
const scheduler = new StateRefreshScheduler({
|
|
14061
|
+
policies: deps.policies ?? getStateRefreshPolicyRegistry(),
|
|
14062
|
+
logger: deps.logger,
|
|
14063
|
+
holdsPending: (deviceId) => deps.mirror.isHoldingDevice(deviceId),
|
|
14064
|
+
refresh: deps.refresh,
|
|
14065
|
+
resubscribe: deps.resubscribe
|
|
14066
|
+
});
|
|
14067
|
+
deps.mirror.setReportTap((deviceId) => scheduler.onReport(deviceId));
|
|
14068
|
+
const uninstall = getCommandFollowUpRegistry().set(scheduler.onCommand);
|
|
14069
|
+
return () => {
|
|
14070
|
+
uninstall();
|
|
14071
|
+
deps.mirror.setReportTap(null);
|
|
14072
|
+
scheduler.dispose();
|
|
14073
|
+
};
|
|
14074
|
+
}
|
|
13084
14075
|
var REDACTED = "***";
|
|
13085
14076
|
var ROUTING_KEYS = new Set([
|
|
13086
14077
|
"deviceId",
|
|
@@ -13999,6 +14990,9 @@ var DeviceManagerAddon = class extends require_dist.BaseAddon {
|
|
|
13999
14990
|
stateMirrorImpl = null;
|
|
14000
14991
|
/** The transition audit (D1022): command + mirror correlation, shipped to device-history. */
|
|
14001
14992
|
transitions = null;
|
|
14993
|
+
/** Unsubscribes the command hold (D1061). */
|
|
14994
|
+
stopCommandHolds = null;
|
|
14995
|
+
stopStateRefresh = null;
|
|
14002
14996
|
/**
|
|
14003
14997
|
* Removes the claimed-status overlay this addon installed on the capability
|
|
14004
14998
|
* registry (D663, S3): `getStatus` of a claimed cap answers the mirror for
|
|
@@ -14345,6 +15339,28 @@ var DeviceManagerAddon = class extends require_dist.BaseAddon {
|
|
|
14345
15339
|
const metaStore = new DeviceMetaStore(settings, registry, deviceRows, this.addonStoreWriteLock);
|
|
14346
15340
|
this.stateMirrorImpl = new DeviceStateMirror(this.ctx);
|
|
14347
15341
|
const stateMirror = this.stateMirrorImpl;
|
|
15342
|
+
this.stopCommandHolds = startCommandHolds({
|
|
15343
|
+
mirror: stateMirror,
|
|
15344
|
+
logger: this.ctx.logger.child("command-holds")
|
|
15345
|
+
});
|
|
15346
|
+
this.stopStateRefresh = startStateRefresh({
|
|
15347
|
+
mirror: stateMirror,
|
|
15348
|
+
logger: this.ctx.logger.child("state-refresh"),
|
|
15349
|
+
refresh: async (deviceId, deviceIds, reason) => {
|
|
15350
|
+
const api = this.ctx.api;
|
|
15351
|
+
if (api === void 0) throw new Error("ctx.api not ready");
|
|
15352
|
+
await api.deviceStateRefresh.refresh.mutate({
|
|
15353
|
+
deviceId,
|
|
15354
|
+
deviceIds: [...deviceIds],
|
|
15355
|
+
reason
|
|
15356
|
+
});
|
|
15357
|
+
},
|
|
15358
|
+
resubscribe: async (deviceId) => {
|
|
15359
|
+
const api = this.ctx.api;
|
|
15360
|
+
if (api === void 0) throw new Error("ctx.api not ready");
|
|
15361
|
+
await api.deviceStateRefresh.resubscribe.mutate({ deviceId });
|
|
15362
|
+
}
|
|
15363
|
+
});
|
|
14348
15364
|
this.transitions = startDeviceTransitions({
|
|
14349
15365
|
mirror: stateMirror,
|
|
14350
15366
|
logger: this.ctx.logger.child("transitions"),
|
|
@@ -14639,13 +15655,13 @@ var DeviceManagerAddon = class extends require_dist.BaseAddon {
|
|
|
14639
15655
|
return stateMirror.snapshotForDevice(input.deviceId);
|
|
14640
15656
|
},
|
|
14641
15657
|
getCapSlice: async (input) => {
|
|
14642
|
-
return stateMirror.capSlice(input.deviceId, input.capName);
|
|
15658
|
+
return stateMirror.capSlice(input.deviceId, input.capName, input.confirmed === true ? "confirmed" : "effective");
|
|
14643
15659
|
},
|
|
14644
15660
|
getNativeCapSlice: async (input) => {
|
|
14645
15661
|
return stateMirror.nativeSlice(input.deviceId, input.capName);
|
|
14646
15662
|
},
|
|
14647
|
-
getAllSnapshots: async () => {
|
|
14648
|
-
return stateMirror.allSnapshots();
|
|
15663
|
+
getAllSnapshots: async (input) => {
|
|
15664
|
+
return stateMirror.allSnapshots(input?.confirmed === true ? "confirmed" : "effective");
|
|
14649
15665
|
},
|
|
14650
15666
|
setCapSlice: async (input) => {
|
|
14651
15667
|
const { deviceId, capName, slice } = input;
|
|
@@ -14743,6 +15759,10 @@ var DeviceManagerAddon = class extends require_dist.BaseAddon {
|
|
|
14743
15759
|
this.templateActions = null;
|
|
14744
15760
|
await this.transitions?.stop();
|
|
14745
15761
|
this.transitions = null;
|
|
15762
|
+
this.stopStateRefresh?.();
|
|
15763
|
+
this.stopStateRefresh = null;
|
|
15764
|
+
this.stopCommandHolds?.();
|
|
15765
|
+
this.stopCommandHolds = null;
|
|
14746
15766
|
this.removalCleanupImpl?.dispose();
|
|
14747
15767
|
this.removalCleanupImpl = null;
|
|
14748
15768
|
this.removalHooksCleanupImpl?.dispose();
|
|
@@ -14757,18 +15777,35 @@ var DeviceManagerAddon = class extends require_dist.BaseAddon {
|
|
|
14757
15777
|
}
|
|
14758
15778
|
};
|
|
14759
15779
|
//#endregion
|
|
15780
|
+
exports.CommandFollowUpRegistry = CommandFollowUpRegistry;
|
|
14760
15781
|
exports.CommandObserverRegistry = CommandObserverRegistry;
|
|
14761
15782
|
exports.DeviceManagerAddon = DeviceManagerAddon;
|
|
14762
15783
|
exports.default = DeviceManagerAddon;
|
|
15784
|
+
exports.IN_PROCESS_HOLD_SOURCE = IN_PROCESS_HOLD_SOURCE;
|
|
15785
|
+
exports.IN_PROCESS_REFRESH_SOURCE = IN_PROCESS_REFRESH_SOURCE;
|
|
15786
|
+
exports.STATE_REFRESH_CAP = STATE_REFRESH_CAP;
|
|
15787
|
+
exports.StateRefreshPolicyRegistry = StateRefreshPolicyRegistry;
|
|
15788
|
+
exports.__resetCommandFollowUpRegistryForTests = __resetCommandFollowUpRegistryForTests;
|
|
14763
15789
|
exports.__resetCommandObserverRegistryForTests = __resetCommandObserverRegistryForTests;
|
|
15790
|
+
exports.__resetStateRefreshPolicyRegistryForTests = __resetStateRefreshPolicyRegistryForTests;
|
|
14764
15791
|
exports.addonCommandActor = addonCommandActor;
|
|
15792
|
+
exports.childCommandHoldEntries = childCommandHoldEntries;
|
|
15793
|
+
exports.childHoldSource = childHoldSource;
|
|
15794
|
+
exports.childRefreshSource = childRefreshSource;
|
|
15795
|
+
exports.childStateRefreshEntries = childStateRefreshEntries;
|
|
14765
15796
|
exports.commandActorFromContext = commandActorFromContext;
|
|
15797
|
+
exports.commandFollowUpTarget = commandFollowUpTarget;
|
|
14766
15798
|
exports.commandObserverLink = commandObserverLink;
|
|
14767
15799
|
exports.createDeviceSettingsStore = createDeviceSettingsStore;
|
|
15800
|
+
exports.deviceIdOfArgs = deviceIdOfArgs;
|
|
15801
|
+
exports.getCommandFollowUpRegistry = getCommandFollowUpRegistry;
|
|
15802
|
+
exports.getCommandHoldOverrideRegistry = getCommandHoldOverrideRegistry;
|
|
14768
15803
|
exports.getCommandObserverRegistry = getCommandObserverRegistry;
|
|
15804
|
+
exports.getStateRefreshPolicyRegistry = getStateRefreshPolicyRegistry;
|
|
14769
15805
|
exports.mergeAggregates = mergeAggregates;
|
|
14770
15806
|
exports.observable = observable;
|
|
14771
15807
|
exports.readDeviceOrThrow = readDeviceOrThrow;
|
|
14772
15808
|
exports.reportCommand = reportCommand;
|
|
14773
15809
|
exports.runObservedCommand = runObservedCommand;
|
|
14774
15810
|
exports.setWrapperActive = setWrapperActive;
|
|
15811
|
+
exports.withCommandFollowUp = withCommandFollowUp;
|