indigo-matter-bridge 0.14.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Controller subscription-churn detection (issue #286).
3
+ *
4
+ * Alexa opens a CASE session, subscribes, then some minutes later reports the
5
+ * subscription invalid; matter.js terminates and deletes it, Alexa re-subscribes
6
+ * over a NEW session, and the old sessions are never reaped. Measured on the
7
+ * reference server (issue #283, 2026-08-23): three session generations for one
8
+ * Echo peer inside 30 minutes, a ~30-minute re-subscribe cycle, 24
9
+ * SubscribeRequests a day. Nothing recovers it but a bridge restart, and until
10
+ * this detector existed the only evidence was a log line nobody reads.
11
+ *
12
+ * **Detection is read-only.** Sessions are matter.js's to manage; this module
13
+ * counts and reports, and never closes anything.
14
+ *
15
+ * Deliberately pure — no matter.js import, no timers, injected clock — for the
16
+ * same reason `refuseReasonFor` is (ADR/ARCHITECTURE): the interesting states
17
+ * take three session generations and half an hour of real controller
18
+ * misbehaviour to reach, so they are unreachable in a test that needs hardware.
19
+ * {@link BridgeNode} owns the thin wiring from the matter.js observables.
20
+ *
21
+ * The signature is deliberately NOT the "Subscription reported invalid by peer"
22
+ * log line: that string belongs to a dependency. But nor is `isTerminated` a
23
+ * synonym for it. In matter.js 0.17.8 three distinct things set that flag, and
24
+ * only the last is the fault:
25
+ *
26
+ * 1. `handlePeerCancel()` (`ServerSubscription.ts:249`), which
27
+ * `InteractionServer.ts:620` calls on EVERY existing subscription of a peer
28
+ * whenever that peer re-subscribes with `keepSubscriptions: false` — the
29
+ * routine re-subscribe path for Apple, Alexa and Google alike;
30
+ * 2. giving up after three failed updates on a transient network error
31
+ * (`ServerSubscription.ts:557` — NoResponseTimeout/NetworkError/
32
+ * SessionClosedError), i.e. a Wi-Fi blip;
33
+ * 3. the peer reporting the subscription invalid (`ServerSubscription.ts:870`).
34
+ *
35
+ * A deletion count alone therefore cannot separate #283 from a healthy
36
+ * controller having a bad afternoon: three ordinary re-subscribes or three
37
+ * Wi-Fi blips inside the window would tell the user to restart a working
38
+ * bridge. What distinguishes the fault is that the deletions land on a peer
39
+ * whose sessions are ALSO piling up — a healthy controller re-subscribes over
40
+ * the session it already has, or closes the old one; the #283 peer does
41
+ * neither. Hence {@link ChurnDetector.verdict}'s conjunction.
42
+ */
43
+ import type { SubscriptionChurn } from "./protocol.js";
44
+ /**
45
+ * Rolling window for the deletion count.
46
+ *
47
+ * 30 minutes because that is the observed re-subscribe cycle (#283): a window
48
+ * shorter than one cycle could never see two deletions, and a much longer one
49
+ * would keep reporting a fault that has already stopped.
50
+ */
51
+ export declare const CHURN_WINDOW_MINUTES = 30;
52
+ /**
53
+ * Terminated-subscription deletions for ONE peer inside the window that, TOGETHER
54
+ * with {@link CHURN_MIN_PILED_SESSIONS}, mean churn. Three, because the observed
55
+ * fault produced exactly three generations in 30 minutes.
56
+ *
57
+ * Never sufficient on its own — see the module header for the three unrelated
58
+ * causes that set `isTerminated`, two of which a healthy controller hits.
59
+ */
60
+ export declare const CHURN_DELETION_THRESHOLD = 3;
61
+ /**
62
+ * Live sessions a peer must ALSO be holding before its deletions count as churn.
63
+ *
64
+ * Two, because that is the smallest number that is evidence of a pile: a
65
+ * controller re-subscribing normally does so over the session it already has,
66
+ * or closes the old one as it opens the new. The #283 peer does neither, which
67
+ * is why its sessions accumulate while it deletes subscriptions. One session
68
+ * plus deletions is a controller reconnecting, and must not be reported.
69
+ */
70
+ export declare const CHURN_MIN_PILED_SESSIONS = 2;
71
+ /**
72
+ * Live CASE sessions for ONE peer that mean churn on their own.
73
+ *
74
+ * A controller normally holds one or two (Alexa uses a second briefly while
75
+ * re-subscribing). Four is the pile that only accumulates when the old ones are
76
+ * never reaped, and it catches the case where the deletions happened before the
77
+ * bridge started counting.
78
+ */
79
+ export declare const CHURN_SESSION_THRESHOLD = 4;
80
+ export interface ChurnDetectorOptions {
81
+ /** Injected for tests; milliseconds since the epoch. */
82
+ now?: () => number;
83
+ windowMinutes?: number;
84
+ deletionThreshold?: number;
85
+ sessionThreshold?: number;
86
+ minPiledSessions?: number;
87
+ }
88
+ /** What one poll saw, and whether it is news. */
89
+ export interface ChurnPoll {
90
+ verdict: SubscriptionChurn;
91
+ /**
92
+ * True only when this verdict differs from the previous poll's in a way a
93
+ * user needs telling about — `checked` flipped, `active` flipped, or the
94
+ * set of over-threshold peers changed. Counts alone do not make it true:
95
+ * `get_status` is the plugin's 15s watchdog tick, and a caller that logged
96
+ * on every change would re-log one standing fault four times a minute.
97
+ */
98
+ changed: boolean;
99
+ }
100
+ /**
101
+ * Per-peer churn bookkeeping.
102
+ *
103
+ * Fed plain events by {@link BridgeNode}; answers {@link verdict} on demand.
104
+ * The rolling window is pruned lazily at read time (and on insert, so an
105
+ * unpolled detector cannot grow without bound) rather than by a timer — there
106
+ * is nothing to do when it drains except answer differently next time.
107
+ */
108
+ export declare class ChurnDetector {
109
+ #private;
110
+ constructor(options?: ChurnDetectorOptions);
111
+ /**
112
+ * The detector can no longer observe session state, so every verdict from
113
+ * here on is `checked: false`.
114
+ *
115
+ * One-way on purpose. A detector that had its wiring fail, or a handler
116
+ * that threw, has an unknown number of unseen events behind it — and the
117
+ * whole point of `checked` is that a bridge which cannot look must not
118
+ * report the healthy answer. Later events are ignored rather than counted
119
+ * into a total that is already missing some.
120
+ */
121
+ markBroken(): void;
122
+ get isBroken(): boolean;
123
+ sessionOpened(sessionId: number, peerNodeId: string, fabricIndex: number): void;
124
+ sessionClosed(sessionId: number): void;
125
+ /**
126
+ * A subscription left a peer's session.
127
+ *
128
+ * `wasTerminated` is the caller's read of `Subscription.isTerminated`, which
129
+ * is NOT "the peer reported it invalid" — see the module header for all
130
+ * three causes. It is recorded because it is the cheapest available marker
131
+ * that the subscription did not end cleanly; the conjunction in
132
+ * {@link verdict} is what stops the two innocent causes reaching a user. A
133
+ * clean unsubscribe (`wasTerminated: false`) is never counted at all.
134
+ */
135
+ subscriptionRemoved(peerNodeId: string, fabricIndex: number, wasTerminated: boolean): void;
136
+ /** The verdict now, with the rolling window pruned to `nowMs`. */
137
+ verdict(nowMs?: number): SubscriptionChurn;
138
+ /** {@link verdict}, plus whether it is a transition worth acting on. */
139
+ poll(nowMs?: number): ChurnPoll;
140
+ }
141
+ /**
142
+ * The §4.3 notice text for an active verdict.
143
+ *
144
+ * Names the peer because the two Echoes on one fabric are not interchangeable:
145
+ * "an Alexa controller is churning" leaves the user nothing to act on, and the
146
+ * only recovery is a bridge restart.
147
+ */
148
+ export declare function churnWarning(churn: SubscriptionChurn): string;
package/dist/churn.js ADDED
@@ -0,0 +1,257 @@
1
+ /**
2
+ * Controller subscription-churn detection (issue #286).
3
+ *
4
+ * Alexa opens a CASE session, subscribes, then some minutes later reports the
5
+ * subscription invalid; matter.js terminates and deletes it, Alexa re-subscribes
6
+ * over a NEW session, and the old sessions are never reaped. Measured on the
7
+ * reference server (issue #283, 2026-08-23): three session generations for one
8
+ * Echo peer inside 30 minutes, a ~30-minute re-subscribe cycle, 24
9
+ * SubscribeRequests a day. Nothing recovers it but a bridge restart, and until
10
+ * this detector existed the only evidence was a log line nobody reads.
11
+ *
12
+ * **Detection is read-only.** Sessions are matter.js's to manage; this module
13
+ * counts and reports, and never closes anything.
14
+ *
15
+ * Deliberately pure — no matter.js import, no timers, injected clock — for the
16
+ * same reason `refuseReasonFor` is (ADR/ARCHITECTURE): the interesting states
17
+ * take three session generations and half an hour of real controller
18
+ * misbehaviour to reach, so they are unreachable in a test that needs hardware.
19
+ * {@link BridgeNode} owns the thin wiring from the matter.js observables.
20
+ *
21
+ * The signature is deliberately NOT the "Subscription reported invalid by peer"
22
+ * log line: that string belongs to a dependency. But nor is `isTerminated` a
23
+ * synonym for it. In matter.js 0.17.8 three distinct things set that flag, and
24
+ * only the last is the fault:
25
+ *
26
+ * 1. `handlePeerCancel()` (`ServerSubscription.ts:249`), which
27
+ * `InteractionServer.ts:620` calls on EVERY existing subscription of a peer
28
+ * whenever that peer re-subscribes with `keepSubscriptions: false` — the
29
+ * routine re-subscribe path for Apple, Alexa and Google alike;
30
+ * 2. giving up after three failed updates on a transient network error
31
+ * (`ServerSubscription.ts:557` — NoResponseTimeout/NetworkError/
32
+ * SessionClosedError), i.e. a Wi-Fi blip;
33
+ * 3. the peer reporting the subscription invalid (`ServerSubscription.ts:870`).
34
+ *
35
+ * A deletion count alone therefore cannot separate #283 from a healthy
36
+ * controller having a bad afternoon: three ordinary re-subscribes or three
37
+ * Wi-Fi blips inside the window would tell the user to restart a working
38
+ * bridge. What distinguishes the fault is that the deletions land on a peer
39
+ * whose sessions are ALSO piling up — a healthy controller re-subscribes over
40
+ * the session it already has, or closes the old one; the #283 peer does
41
+ * neither. Hence {@link ChurnDetector.verdict}'s conjunction.
42
+ */
43
+ /**
44
+ * Rolling window for the deletion count.
45
+ *
46
+ * 30 minutes because that is the observed re-subscribe cycle (#283): a window
47
+ * shorter than one cycle could never see two deletions, and a much longer one
48
+ * would keep reporting a fault that has already stopped.
49
+ */
50
+ export const CHURN_WINDOW_MINUTES = 30;
51
+ /**
52
+ * Terminated-subscription deletions for ONE peer inside the window that, TOGETHER
53
+ * with {@link CHURN_MIN_PILED_SESSIONS}, mean churn. Three, because the observed
54
+ * fault produced exactly three generations in 30 minutes.
55
+ *
56
+ * Never sufficient on its own — see the module header for the three unrelated
57
+ * causes that set `isTerminated`, two of which a healthy controller hits.
58
+ */
59
+ export const CHURN_DELETION_THRESHOLD = 3;
60
+ /**
61
+ * Live sessions a peer must ALSO be holding before its deletions count as churn.
62
+ *
63
+ * Two, because that is the smallest number that is evidence of a pile: a
64
+ * controller re-subscribing normally does so over the session it already has,
65
+ * or closes the old one as it opens the new. The #283 peer does neither, which
66
+ * is why its sessions accumulate while it deletes subscriptions. One session
67
+ * plus deletions is a controller reconnecting, and must not be reported.
68
+ */
69
+ export const CHURN_MIN_PILED_SESSIONS = 2;
70
+ /**
71
+ * Live CASE sessions for ONE peer that mean churn on their own.
72
+ *
73
+ * A controller normally holds one or two (Alexa uses a second briefly while
74
+ * re-subscribing). Four is the pile that only accumulates when the old ones are
75
+ * never reaped, and it catches the case where the deletions happened before the
76
+ * bridge started counting.
77
+ */
78
+ export const CHURN_SESSION_THRESHOLD = 4;
79
+ /**
80
+ * Per-peer churn bookkeeping.
81
+ *
82
+ * Fed plain events by {@link BridgeNode}; answers {@link verdict} on demand.
83
+ * The rolling window is pruned lazily at read time (and on insert, so an
84
+ * unpolled detector cannot grow without bound) rather than by a timer — there
85
+ * is nothing to do when it drains except answer differently next time.
86
+ */
87
+ export class ChurnDetector {
88
+ #now;
89
+ #windowMs;
90
+ #deletionThreshold;
91
+ #sessionThreshold;
92
+ #minPiledSessions;
93
+ #peers = new Map();
94
+ /** `sessionId → peer key`, so a close needs only the id matter.js gives it. */
95
+ #sessionPeers = new Map();
96
+ #broken = false;
97
+ /**
98
+ * The last {@link poll} signature, for the transition test. Seeded with the
99
+ * healthy one a fresh detector is genuinely in, so the first poll of a
100
+ * quiet bridge is not itself a transition.
101
+ */
102
+ #signature = signatureOf({ checked: true, active: false, peers: [] });
103
+ constructor(options = {}) {
104
+ this.#now = options.now ?? Date.now;
105
+ this.#windowMs = (options.windowMinutes ?? CHURN_WINDOW_MINUTES) * 60_000;
106
+ this.#deletionThreshold = options.deletionThreshold ?? CHURN_DELETION_THRESHOLD;
107
+ this.#sessionThreshold = options.sessionThreshold ?? CHURN_SESSION_THRESHOLD;
108
+ this.#minPiledSessions = options.minPiledSessions ?? CHURN_MIN_PILED_SESSIONS;
109
+ }
110
+ /**
111
+ * The detector can no longer observe session state, so every verdict from
112
+ * here on is `checked: false`.
113
+ *
114
+ * One-way on purpose. A detector that had its wiring fail, or a handler
115
+ * that threw, has an unknown number of unseen events behind it — and the
116
+ * whole point of `checked` is that a bridge which cannot look must not
117
+ * report the healthy answer. Later events are ignored rather than counted
118
+ * into a total that is already missing some.
119
+ */
120
+ markBroken() {
121
+ this.#broken = true;
122
+ this.#peers.clear();
123
+ this.#sessionPeers.clear();
124
+ }
125
+ get isBroken() {
126
+ return this.#broken;
127
+ }
128
+ sessionOpened(sessionId, peerNodeId, fabricIndex) {
129
+ if (this.#broken) {
130
+ return;
131
+ }
132
+ const key = peerKey(peerNodeId, fabricIndex);
133
+ this.#sessionPeers.set(sessionId, key);
134
+ this.#peer(key, peerNodeId, fabricIndex).sessions.add(sessionId);
135
+ }
136
+ sessionClosed(sessionId) {
137
+ if (this.#broken) {
138
+ return;
139
+ }
140
+ const key = this.#sessionPeers.get(sessionId);
141
+ if (key === undefined) {
142
+ return;
143
+ }
144
+ this.#sessionPeers.delete(sessionId);
145
+ this.#peers.get(key)?.sessions.delete(sessionId);
146
+ }
147
+ /**
148
+ * A subscription left a peer's session.
149
+ *
150
+ * `wasTerminated` is the caller's read of `Subscription.isTerminated`, which
151
+ * is NOT "the peer reported it invalid" — see the module header for all
152
+ * three causes. It is recorded because it is the cheapest available marker
153
+ * that the subscription did not end cleanly; the conjunction in
154
+ * {@link verdict} is what stops the two innocent causes reaching a user. A
155
+ * clean unsubscribe (`wasTerminated: false`) is never counted at all.
156
+ */
157
+ subscriptionRemoved(peerNodeId, fabricIndex, wasTerminated) {
158
+ if (this.#broken || !wasTerminated) {
159
+ return;
160
+ }
161
+ const now = this.#now();
162
+ const peer = this.#peer(peerKey(peerNodeId, fabricIndex), peerNodeId, fabricIndex);
163
+ peer.deletions.push(now);
164
+ prune(peer, now - this.#windowMs);
165
+ }
166
+ /** The verdict now, with the rolling window pruned to `nowMs`. */
167
+ verdict(nowMs = this.#now()) {
168
+ if (this.#broken) {
169
+ return { checked: false, active: false, peers: [] };
170
+ }
171
+ const cutoff = nowMs - this.#windowMs;
172
+ const peers = [];
173
+ for (const [key, peer] of this.#peers) {
174
+ prune(peer, cutoff);
175
+ // Two ways in, and the deletion arm is deliberately conjunctive: on
176
+ // its own it fires on ordinary re-subscribes and Wi-Fi blips
177
+ // (module header). The session arm stands alone because a pile that
178
+ // large is already the fault, however it got there.
179
+ const over = peer.sessions.size >= this.#sessionThreshold
180
+ || (peer.deletions.length >= this.#deletionThreshold
181
+ && peer.sessions.size >= this.#minPiledSessions);
182
+ if (over) {
183
+ peer.since ??= nowMs;
184
+ peers.push({
185
+ peerNodeId: peer.peerNodeId,
186
+ fabricIndex: peer.fabricIndex,
187
+ liveSessions: peer.sessions.size,
188
+ invalidDeletions: peer.deletions.length,
189
+ windowMinutes: this.#windowMs / 60_000,
190
+ since: new Date(peer.since).toISOString(),
191
+ });
192
+ continue;
193
+ }
194
+ peer.since = undefined;
195
+ if (peer.sessions.size === 0 && peer.deletions.length === 0) {
196
+ this.#peers.delete(key);
197
+ }
198
+ }
199
+ peers.sort((a, b) => a.peerNodeId.localeCompare(b.peerNodeId));
200
+ return { checked: true, active: peers.length > 0, peers };
201
+ }
202
+ /** {@link verdict}, plus whether it is a transition worth acting on. */
203
+ poll(nowMs = this.#now()) {
204
+ const verdict = this.verdict(nowMs);
205
+ const signature = signatureOf(verdict);
206
+ const changed = signature !== this.#signature;
207
+ this.#signature = signature;
208
+ return { verdict, changed };
209
+ }
210
+ #peer(key, peerNodeId, fabricIndex) {
211
+ let peer = this.#peers.get(key);
212
+ if (peer === undefined) {
213
+ peer = { peerNodeId, fabricIndex, sessions: new Set(), deletions: [] };
214
+ this.#peers.set(key, peer);
215
+ }
216
+ return peer;
217
+ }
218
+ }
219
+ /**
220
+ * The §4.3 notice text for an active verdict.
221
+ *
222
+ * Names the peer because the two Echoes on one fabric are not interchangeable:
223
+ * "an Alexa controller is churning" leaves the user nothing to act on, and the
224
+ * only recovery is a bridge restart.
225
+ */
226
+ export function churnWarning(churn) {
227
+ const peers = churn.peers.map(peer => `controller peer ${peer.peerNodeId} (fabric ${peer.fabricIndex}): ${peer.invalidDeletions} invalid `
228
+ + `subscription deletion(s) in ${peer.windowMinutes} min, ${peer.liveSessions} live session(s) `
229
+ + `since ${peer.since}`);
230
+ return `Subscription churn detected for ${peers.join("; ")} — restart the Matter bridge to recover.`;
231
+ }
232
+ /**
233
+ * What makes one verdict a different *situation* from another. Deliberately
234
+ * excludes the counts: those move on every deletion, and a caller that treated
235
+ * that as news would re-report one standing fault forever.
236
+ */
237
+ function signatureOf(churn) {
238
+ return [
239
+ churn.checked,
240
+ churn.active,
241
+ ...churn.peers.map(peer => `${peer.fabricIndex}/${peer.peerNodeId}`),
242
+ ].join("|");
243
+ }
244
+ function peerKey(peerNodeId, fabricIndex) {
245
+ return `${fabricIndex}/${peerNodeId}`;
246
+ }
247
+ /** Drop deletions older than `cutoff`; the array is kept oldest-first. */
248
+ function prune(peer, cutoff) {
249
+ let keep = 0;
250
+ while (keep < peer.deletions.length && peer.deletions[keep] <= cutoff) {
251
+ keep++;
252
+ }
253
+ if (keep > 0) {
254
+ peer.deletions.splice(0, keep);
255
+ }
256
+ }
257
+ //# sourceMappingURL=churn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"churn.js","sourceRoot":"","sources":["../src/churn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAIH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAEvC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,CAAC;AAE1C;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,CAAC;AAE1C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,CAAC;AAmCzC;;;;;;;GAOG;AACH,MAAM,OAAO,aAAa;IACb,IAAI,CAAe;IACnB,SAAS,CAAS;IAClB,kBAAkB,CAAS;IAC3B,iBAAiB,CAAS;IAC1B,iBAAiB,CAAS;IAC1B,MAAM,GAAG,IAAI,GAAG,EAAqB,CAAC;IAC/C,+EAA+E;IACtE,aAAa,GAAG,IAAI,GAAG,EAAkB,CAAC;IACnD,OAAO,GAAG,KAAK,CAAC;IAChB;;;;OAIG;IACH,UAAU,GA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AIG;AACH,SAAS,WAAW,CAAC,KAAwB;IACzC,OAAO;QACH,KAAK,CAAC,OAAO;QACb,KAAK,CAAC,MAAM;QACZ,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;KACvE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAChB,CAAC;AAED,SAAS,OAAO,CAAC,UAAkB,EAAE,WAAmB;IACpD,OAAO,GAAG,WAAW,IAAI,UAAU,EAAE,CAAC;AAC1C,CAAC;AAED,0EAA0E;AAC1E,SAAS,KAAK,CAAC,IAAe,EAAE,MAAc;IAC1C,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,OAAO,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,IAAK,IAAI,CAAC,SAAS,CAAC,IAAI,CAAY,IAAI,MAAM,EAAE,CAAC;QAChF,IAAI,EAAE,CAAC;IACX,CAAC;IACD,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;QACX,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;AACL,CAAC"}
@@ -299,6 +299,29 @@ export declare function percent100thsToPosition(percent100ths: number): number;
299
299
  export declare function systemModeToMatter(mode: unknown): Thermostat.SystemMode | undefined;
300
300
  /** Matter `SystemModeEnum` → §4.2 `systemMode`, or `undefined` outside the domain. */
301
301
  export declare function matterToSystemMode(value: unknown): string | undefined;
302
+ /** Per-device ColorControl physical bounds, once validated (issue #293). */
303
+ export interface CtBounds {
304
+ minMireds: number;
305
+ maxMireds: number;
306
+ }
307
+ /**
308
+ * §4.1's `ctMinMireds`/`ctMaxMireds` validity rule (issue #293), read off an
309
+ * `EndpointSpec.options` struct: usable iff BOTH keys are present, BOTH are
310
+ * integers, and `MIREDS_MIN <= min < max <= MIREDS_MAX`. A boolean already
311
+ * fails the `typeof === "number"` half of that on its own — `JSON.parse`
312
+ * turns `true`/`false` into JS booleans, not numbers — so there is no separate
313
+ * "and not a boolean" branch to write here the way the Python side needs one.
314
+ *
315
+ * Neither key present is the ordinary case (no seed, no learned bound yet on
316
+ * this device) and returns `undefined` silently. Any OTHER shape — one key
317
+ * missing, a float, an inverted or out-of-range pair — is unusable and ALSO
318
+ * returns `undefined`, but is worth a warn naming the device: refusing the
319
+ * attach over a malformed seed would take the WHOLE export offline, so
320
+ * falling back to the generic bounds is the only lawful response, and the
321
+ * warn is the only trace that it happened. One line for the pair, not one per
322
+ * malformed key — the two keys describe a single value.
323
+ */
324
+ export declare function extractCtBounds(options: Record<string, unknown>, indigoDeviceId: number): CtBounds | undefined;
302
325
  /**
303
326
  * Why a lawful §4.2 role can still be refused.
304
327
  *
@@ -381,6 +404,56 @@ export declare function applyStates(endpoint: Endpoint, role: RoleValue, states:
381
404
  export declare function applyReachable(endpoint: Endpoint, reachable: boolean): Promise<void>;
382
405
  /** §4.1 — `NodeLabel` follows the export's display name. */
383
406
  export declare function applyLabel(endpoint: Endpoint, label: string): Promise<void>;
407
+ /**
408
+ * Issue #293 — apply a live CT-bounds change. Called from `registry.ts#update`,
409
+ * which is itself the tail of `rekeyOne`, so this one function is what both
410
+ * the reconcile UPDATE bucket and the driving-device REKEY path get it from —
411
+ * there is no second copy to keep in step with this one.
412
+ *
413
+ * A silent no-op whenever the desired bounds are absent/unusable (leaves live
414
+ * bounds untouched — an unusable pair on an UPDATE must never reset a working
415
+ * per-device pair back to generic) OR already match what the endpoint
416
+ * advertises: an ordinary reconcile touches every CT-role export on every
417
+ * pass, and logging "nothing changed" on each one would drown the log for the
418
+ * overwhelmingly common case. `extractCtBounds` already owns the one warn
419
+ * that a genuinely malformed pair deserves.
420
+ *
421
+ * When the bounds DO differ, the new physical bounds and the live
422
+ * `colorTemperatureMireds` reclamped into them are written in the SAME
423
+ * `endpoint.set()` — a fabric holding a colour temperature the new bounds no
424
+ * longer cover, even for one intermediate report, is the same shape of bug
425
+ * {@link clampSetpoint}'s doc names for a thermostat write.
426
+ *
427
+ * **`colorTemperatureMireds` is keyed before `coupleColorTempToLevelMinMireds`
428
+ * in the patch object, and that ordering is load-bearing, not cosmetic
429
+ * (measured against 0.17.8, "orders colorTemperatureMireds before
430
+ * coupleColorTempToLevelMinMireds" in registry.test.ts).** Unlike
431
+ * `new Endpoint(...)` — which validates the FINAL merged state as one unit,
432
+ * so {@link ctBoundsOverride}'s own key order is free — `endpoint.set()`
433
+ * applies a patch's keys one at a time and validates each against whatever
434
+ * the OTHER referenced fields hold at THAT point in the sequence.
435
+ * `coupleColorTempToLevelMinMireds`'s own constraint is "physical min to
436
+ * colorTemperatureMireds", so writing it before `colorTemperatureMireds` has
437
+ * moved checks the new couple value against the STALE colour temperature and
438
+ * throws a `CONSTRAINT_ERROR` — even though the two keys sit in the very same
439
+ * `set()` call and the state is perfectly consistent once both land.
440
+ * `colorTempPhysicalMinMireds`/`MaxMireds` were probed too and are NOT
441
+ * order-sensitive against each other or against `colorTemperatureMireds`, so
442
+ * they are left first for readability; only the couple/CT pair's order is a
443
+ * real constraint.
444
+ *
445
+ * The bridged accessory's OWN `ConfigurationVersion` is then bumped so
446
+ * capability-caching ecosystems re-read this one accessory's clusters rather
447
+ * than trusting a physical-bounds pair cached at commissioning. That is
448
+ * deliberately NOT the ROOT bump `node.ts#bumpConfigurationVersion` drives
449
+ * for a changed bridged-node SET (children added/removed) — a different
450
+ * signal for a different change shape — though `increaseConfigurationVersion`
451
+ * called standalone (no `change` callback) bumps the root too as a documented
452
+ * side effect (confirmed live against 0.17.8 by the existing "matter.js
453
+ * RESTORES a bridged accessory's ConfigurationVersion" test), which is
454
+ * harmless here: an extra hint on top of a real one costs nothing.
455
+ */
456
+ export declare function applyCtBoundsUpdate(endpoint: Endpoint, role: RoleValue, options: Record<string, unknown>, indigoDeviceId: number): Promise<void>;
384
457
  /**
385
458
  * True when a cluster change came from our own `set_state` rather than from an
386
459
  * ecosystem (§6.4). matter.js runs local agent writes in a `LocalActorContext`,