indigo-matter-bridge 0.10.1 → 0.12.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.
@@ -67,7 +67,7 @@
67
67
  * tested without a Matter stack. Callers hand in `{uniqueId, endpointNumber}`
68
68
  * pairs; deriving `uniqueId` from an Indigo device id is `endpoints.ts`'s job.
69
69
  */
70
- import { type DriftEntry } from "./protocol.js";
70
+ import { type DriftEntry, type OrphanRecord } from "./protocol.js";
71
71
  /** What the PRD §7 refuse-to-start decision is taken from. */
72
72
  export interface EndpointIdentityState {
73
73
  /** Does the Matter stack hold at least one fabric right now? */
@@ -166,8 +166,8 @@ export interface EndpointRecord {
166
166
  */
167
167
  numberVoid?: true;
168
168
  /**
169
- * Set by {@link EndpointMapStore.forget} (issue #219, this half only —
170
- * the re-adopt UI itself lands separately): the device was un-exported,
169
+ * Set by {@link EndpointMapStore.forget} (issue #219): the device was
170
+ * un-exported,
171
171
  * so the entry must not be rebuilt by {@link EndpointMapStore.restorable}
172
172
  * — but unlike the pre-#219 behaviour, `role`/`label` are KEPT rather
173
173
  * than deleted, because they are the only evidence a future re-adopt UI
@@ -190,6 +190,50 @@ export interface EndpointRecord {
190
190
  * tolerance as the other two, and the same reason the schema stays 2.
191
191
  */
192
192
  battery?: true;
193
+ /**
194
+ * Set by {@link recordFor}/{@link noteRestorable} (issue #219 re-adopt):
195
+ * the Indigo device CURRENTLY driving this published identity. Absent
196
+ * means "the key's own derivation" — every pre-PR5 record, and every
197
+ * un-re-adopted one, for which `parsePublishedId(uniqueId)?.deviceId` is
198
+ * still the right answer (see {@link RestorableEndpoint.indigoDeviceId}).
199
+ *
200
+ * **Replace-on-change, unlike {@link numberVoid}/{@link orphaned}/
201
+ * {@link battery}'s add-only convention.** A re-adopt IS a change of
202
+ * driving device — pinning the FIRST (now-deleted) device id forever
203
+ * would make restore-on-start rebuild the accessory bound to a device
204
+ * that no longer exists, which is the whole failure #219 exists to fix.
205
+ */
206
+ deviceId?: number;
207
+ /**
208
+ * Set by {@link EndpointMapStore.forget} (issue #219) the moment an entry
209
+ * is FIRST orphaned — an ISO-8601 stamp from the store's injected clock,
210
+ * purely for the re-adopt picker's "un-exported on …" column. Absent on
211
+ * every pre-PR5 orphan; the picker renders that as "date unknown" rather
212
+ * than treating it as a fault (§4.2).
213
+ */
214
+ orphanedAt?: string;
215
+ /**
216
+ * Set by {@link EndpointMapStore.forget} when the caller has decided this
217
+ * identity was RETIRED, not merely un-exported (issue #240): the published
218
+ * identity that replaced it.
219
+ *
220
+ * **The narrow rule, and it is narrow on purpose.** Only a LATER
221
+ * GENERATION of the same identity — `indigo-7` → `indigo-7~2`, which is
222
+ * what `protocol.ts`'s `supersedes()` tests — counts. A **re-adopt** is
223
+ * the same shape at a glance (one removal plus one create, for one device)
224
+ * and is deliberately NOT one: the identity a re-adopt leaves behind is an
225
+ * ordinary orphan the picker must go on offering, PR5 design E5 in as many
226
+ * words. Both node-side writers apply `supersedes()` before setting this,
227
+ * whether the pair lands in one mutation (`node.ts`'s `forgetRemoved`) or
228
+ * as two commands (`noteSupersessionIfPaired`).
229
+ *
230
+ * A superseded entry is {@link orphaned} too (it is not live), but it is
231
+ * NOT a re-adopt candidate — {@link EndpointMapStore.restorable} and
232
+ * {@link EndpointMapStore.orphans} both exclude it, because re-adopting it
233
+ * would resurrect the OLD-role accessory under a number every paired
234
+ * ecosystem has already processed a removal for.
235
+ */
236
+ supersededBy?: string;
193
237
  }
194
238
  /** The on-disk shape (v2). */
195
239
  export interface EndpointMapFile {
@@ -222,6 +266,15 @@ export interface LiveEndpointNumber {
222
266
  * disk — see {@link noteRestorable}'s add-only handling.
223
267
  */
224
268
  battery?: boolean;
269
+ /**
270
+ * Issues #219/#240 — the Indigo device driving this live endpoint right
271
+ * now. Optional for the same reason `role`/`label`/`battery` are: a
272
+ * caller with no live set to report (the seed path) must not be forced
273
+ * to assert one. When present, {@link EndpointMapStore.check} records it
274
+ * REPLACING, not adding to, whatever device id the entry already
275
+ * carried — see {@link EndpointRecord.deviceId}.
276
+ */
277
+ deviceId?: number;
225
278
  }
226
279
  /** An entry complete enough for `node.ts` to reconstruct before going online. */
227
280
  export interface RestorableEndpoint {
@@ -231,6 +284,16 @@ export interface RestorableEndpoint {
231
284
  label: string;
232
285
  /** Issue #220 — `true` only when the persisted entry has ever seen one. */
233
286
  battery?: true;
287
+ /**
288
+ * Issue #219 — the device `node.ts`'s restore-on-start builds the spec
289
+ * for. Resolved by {@link EndpointMapStore.restorable} from
290
+ * {@link EndpointRecord.deviceId} where a re-adopt has set it, falling
291
+ * back to the published identity's own derivation
292
+ * (`parsePublishedId(uniqueId)?.deviceId`) for every pre-PR5 and
293
+ * un-re-adopted entry — an entry that resolves to neither is not
294
+ * restorable at all, so this is never `undefined` here.
295
+ */
296
+ indigoDeviceId: number;
234
297
  }
235
298
  /** The outcome of reading the file: absent, usable, or present-but-broken. */
236
299
  export interface EndpointMapLoad {
@@ -239,6 +302,18 @@ export interface EndpointMapLoad {
239
302
  present: boolean;
240
303
  /** Set only when a file was present and could not be used. */
241
304
  problem?: string;
305
+ /**
306
+ * Entry-level oddities tolerated during the read, for the caller to put on
307
+ * the §4.3 channel (issues #219/#240).
308
+ *
309
+ * `readRecord` is deliberately forgiving — a bad field costs the field, not
310
+ * the file (see its own comment) — but "tolerated" is not the same as
311
+ * "unremarkable", and a DISCARDED `supersededBy` re-arms a retired identity
312
+ * as an ordinary re-adopt candidate. That is a decision about the user's
313
+ * accessories, taken from a value we could not read, and it must not be
314
+ * taken in silence.
315
+ */
316
+ notes?: string[];
242
317
  }
243
318
  /**
244
319
  * Read `endpoint-map.json`. Never throws — the caller decides what a fault means.
@@ -283,15 +358,26 @@ export declare class EndpointMapStore {
283
358
  */
284
359
  get checked(): boolean;
285
360
  /**
286
- * §4.3 `warnings` — the persistence failures a client has to be told about.
361
+ * §4.3 `warnings` — what a client has to be told about: the persistence
362
+ * failures, plus the one-shot {@link #notices}.
287
363
  *
288
- * The node's log is stdout, and in this milestone the node is started **by
289
- * hand**, so stdout is a terminal somebody closed. A map that cannot be
290
- * written is exactly the fault this file exists to make visible, and it
291
- * would otherwise be visible only there. `get_status` is polled, so this is
292
- * the channel that actually reaches a user.
364
+ * The node's log is stdout, and the node is started by launchd, so stdout
365
+ * is a terminal nobody is watching. A map that cannot be written is exactly
366
+ * the fault this file exists to make visible, and it would otherwise be
367
+ * visible only there. `get_status` is polled and `attach` answers with one,
368
+ * so this is the channel that actually reaches a user's Indigo event log
369
+ * (`export_bridge.py`'s `_report_node_warnings`).
293
370
  */
294
371
  get warnings(): string[];
372
+ /**
373
+ * Log `message` AND put it on the §4.3 channel, once per `key` — see
374
+ * {@link #notices} for why stdout alone is not enough.
375
+ *
376
+ * The first caller for a key wins: these are statements about a specific
377
+ * accessory identity, and a second one for the same identity would say
378
+ * the same thing again.
379
+ */
380
+ private notice;
295
381
  /** The recorded number for a `UniqueID`, if there is one. */
296
382
  numberFor(uniqueId: string): number | undefined;
297
383
  get size(): number;
@@ -319,8 +405,60 @@ export declare class EndpointMapStore {
319
405
  * losing them, so the orphan marker is what still keeps it out of the
320
406
  * pre-attach rebuild — exactly the churn {@link forget}'s own doc comment
321
407
  * exists to prevent, unaffected by role/label now surviving alongside it.
408
+ *
409
+ * **So is `supersededBy` (issue #240), checked in its own right rather
410
+ * than left to ride on `orphaned`.** The two are written together, but a
411
+ * hand-edited or partially-rolled-back map can carry one without the
412
+ * other, and a RETIRED identity must never be rebuilt whichever way it got
413
+ * that way — it would put an old-role accessory back under a number every
414
+ * paired ecosystem has already processed a removal for. So the filter is
415
+ * four conditions, not three: no `role`, no `label`, `orphaned`, or
416
+ * `supersededBy`.
417
+ *
418
+ * **Resolves `indigoDeviceId` — and skips what it cannot (issue #219).**
419
+ * `record.deviceId` wins where a re-adopt has set it; otherwise it falls
420
+ * back to the published identity's own derivation
421
+ * (`parsePublishedId(uniqueId)?.deviceId`), which is every pre-PR5 and
422
+ * un-re-adopted entry. An entry with a `role`/`label` but neither source
423
+ * of a device id — a map written by a NEWER node, or hand-edited — is
424
+ * skipped and named on the §4.3 {@link warnings} channel (so it reaches
425
+ * the user's Indigo log, not just stdout), in the same "cannot be rebuilt
426
+ * by this bridge version" words `node.ts`'s own loop uses for an
427
+ * unsupported `role`: this is the only place that ever sees such an entry,
428
+ * since a skipped one never reaches that loop at all.
322
429
  */
323
430
  restorable(): RestorableEndpoint[];
431
+ /**
432
+ * §3.12's answer (issue #219) — every left-behind accessory identity the
433
+ * re-adopt picker could plausibly offer, in map order (the order numbers
434
+ * were first recorded).
435
+ *
436
+ * **Excludes `supersededBy` records (issue #240).** Those are orphaned
437
+ * too, but re-adopting one would resurrect the OLD-role accessory under a
438
+ * number every paired ecosystem has already processed a removal for —
439
+ * see {@link EndpointRecord.supersededBy}.
440
+ *
441
+ * **Includes entries with no `role`/`label` (PR5 design E4, §4.2), unlike
442
+ * {@link restorable}'s filter.** A pre-2026.16.2 bare `{number}` orphan —
443
+ * from a plugin old enough to have deleted them on un-export — carries
444
+ * nothing a replacement device could be matched against, so it can never
445
+ * be re-adopted, but the ruling (PR5 design §5 E4) is to show it anyway: "the number
446
+ * is reserved and the user is entitled to see why", as the picker's
447
+ * "no role recorded, cannot be re-adopted" row (§4.2's third row). Callers
448
+ * that need to tell the two shapes apart check `role`/`label` for
449
+ * `undefined`, which {@link restorable}'s own skip-and-log already treats
450
+ * as unrebuildable for the SAME entries.
451
+ *
452
+ * **Skips a key `parsePublishedId` refuses — PR5 design E11's `restorable()`
453
+ * treatment, applied here for a sharper reason.** This list feeds the
454
+ * re-adopt picker, and picking a row writes its key into the plugin's
455
+ * `ExportStore` as a `publishedAs`, which the node then refuses at the
456
+ * next attach with `malformed_args` — and an attach refusal takes EVERY
457
+ * export offline, not just that one. So a hand-edited or newer-node key is
458
+ * named once on the §4.3 channel and left out, rather than offered as a
459
+ * route into a whole-bridge outage.
460
+ */
461
+ orphans(): OrphanRecord[];
324
462
  /**
325
463
  * True when the map is non-empty and EVERY entry in it is
326
464
  * {@link EndpointRecord.orphaned} — issue #222's distinction for
@@ -367,11 +505,32 @@ export declare class EndpointMapStore {
367
505
  * caller: `erase()` wipes matter.js's endpoints, but the user un-exported
368
506
  * nothing and the plugin's re-attach re-creates the same set.
369
507
  *
370
- * Returns how many entries were newly marked orphaned, so a no-op — no
371
- * record at all, already orphaned, or nothing worth protecting because the
372
- * entry never carried a `role`/`label` to begin with — costs no disk write.
508
+ * **`options.supersededBy` marks a role-change supersession (issue
509
+ * #240) instead of an ordinary un-export**, and `orphanedAt` is stamped
510
+ * from the store's injected `now()` the moment an entry is FIRST
511
+ * orphaned — never re-stamped on a later call, so it always reads the
512
+ * time the accessory actually left, not the time a supersession was
513
+ * confirmed.
514
+ *
515
+ * **This is also how a TWO-COMMAND supersede gets its `supersededBy`
516
+ * (§3 of the design: `replace()` sends `remove_endpoint` then
517
+ * `upsert_endpoint` as separate commands).** The first call orphans the
518
+ * entry with no `supersededBy` yet — the create has not happened. The
519
+ * SECOND call, once the create lands, passes the SAME `uniqueId` again
520
+ * with `{supersededBy}` — the guard below therefore does not stop at
521
+ * "already orphaned" the way it does for a caller with nothing new to
522
+ * say; a second call that has a `supersededBy` this record does not yet
523
+ * carry still counts as a change, and only `orphanedAt` is protected
524
+ * from being re-stamped.
525
+ *
526
+ * Returns how many entries CHANGED — newly orphaned, newly given a
527
+ * `supersededBy`, or both — so a no-op (no record at all, nothing worth
528
+ * protecting because the entry never carried a `role`/`label`, or a call
529
+ * that repeats a fact the record already has) costs no disk write.
373
530
  */
374
- forget(uniqueIds: readonly string[]): number;
531
+ forget(uniqueIds: readonly string[], options?: {
532
+ supersededBy?: string;
533
+ }): number;
375
534
  /**
376
535
  * Compare the live endpoint numbers against the map (PRD §4.3).
377
536
  *
@@ -391,8 +550,43 @@ export declare class EndpointMapStore {
391
550
  * happens to already equal the stale one, because the marker itself is
392
551
  * what has to stop being true, not just the mismatch it was guarding
393
552
  * against.
553
+ *
554
+ * **A changed `deviceId` on an EXISTING record is logged as a re-adopt
555
+ * (issue #219).** The record's device id is read before
556
+ * {@link noteRestorable} overwrites it, so a real change (both sides
557
+ * defined, and different) can still be told apart from a first sighting
558
+ * (`added`, above — nothing to compare against) and from this build
559
+ * simply filling in a field a pre-PR5 record never had (`record.deviceId`
560
+ * was `undefined`, not merely different). Reported directly here, the same
561
+ * place the `numberVoid` adoption above logs itself, because — like that
562
+ * one — only `check` ever sees both the old and the new value at once; and
563
+ * on the §4.3 {@link warnings} channel rather than stdout, because it is
564
+ * the confirmation that the user's re-adopt actually landed.
394
565
  */
395
566
  check(live: readonly LiveEndpointNumber[]): DriftEntry[];
567
+ /**
568
+ * Owner ruling 4 (issue #219) — a cheap nudge, not a decision. Called
569
+ * from {@link check}'s `added` branch, before the brand-new record is
570
+ * inserted: when the identity `check` is about to record for the FIRST
571
+ * time has a `role` AND `label` that exactly match an existing
572
+ * ORPHANED, non-superseded record, say so once — on the §4.3
573
+ * {@link warnings} channel, because a nudge nobody reads is not a nudge —
574
+ * naming the `Re-adopt a Matter accessory…` menu action. This is exactly the
575
+ * motivating case for #219 — a device deleted, recreated and
576
+ * re-exported under a brand-new identity, with the room now empty in
577
+ * every ecosystem and no obvious reason why — offered before the user
578
+ * has to notice and go looking for it.
579
+ *
580
+ * Deliberately does nothing else: it changes no state, blocks nothing,
581
+ * and never fires for an update to an EXISTING identity (an ordinary
582
+ * re-export, or a role-change supersede) — those are not the
583
+ * "orphaned accessory nobody re-adopted yet" case this exists to
584
+ * surface. An ambiguous match (more than one orphan with the same
585
+ * role/label) logs against the first one found, in map order — good
586
+ * enough for a nudge that only ever points a user at the menu, never
587
+ * acts on their behalf.
588
+ */
589
+ private noteReadoptableMatch;
396
590
  /**
397
591
  * §3.10's preserving reset and the last-fabric self-reset (issue #140) —
398
592
  * mark every entry's number as no longer trustworthy, WITHOUT discarding