@oxyhq/core 20.1.0 → 21.0.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.
Files changed (70) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/boot/sessionColdBoot.js +107 -8
  3. package/dist/cjs/i18n/locales/en-US.json +19 -2
  4. package/dist/cjs/i18n/locales/es-ES.json +19 -2
  5. package/dist/cjs/i18n/locales/locales/en-US.json +19 -2
  6. package/dist/cjs/i18n/locales/locales/es-ES.json +19 -2
  7. package/dist/cjs/index.js +50 -16
  8. package/dist/cjs/mixins/OxyServices.auth.js +27 -3
  9. package/dist/cjs/session/SessionClient.js +361 -1
  10. package/dist/cjs/session/accountDialogController.js +121 -147
  11. package/dist/cjs/session/accountSwitchTargets.js +75 -0
  12. package/dist/cjs/session/deviceDirectory.js +143 -0
  13. package/dist/cjs/session/deviceSwitcherRows.js +76 -0
  14. package/dist/cjs/session/projectSessionState.js +8 -1
  15. package/dist/cjs/session/sharedDeviceCredential.js +247 -0
  16. package/dist/esm/.tsbuildinfo +1 -1
  17. package/dist/esm/boot/sessionColdBoot.js +107 -8
  18. package/dist/esm/i18n/locales/en-US.json +19 -2
  19. package/dist/esm/i18n/locales/es-ES.json +19 -2
  20. package/dist/esm/i18n/locales/locales/en-US.json +19 -2
  21. package/dist/esm/i18n/locales/locales/es-ES.json +19 -2
  22. package/dist/esm/index.js +32 -10
  23. package/dist/esm/mixins/OxyServices.auth.js +27 -3
  24. package/dist/esm/session/SessionClient.js +362 -2
  25. package/dist/esm/session/accountDialogController.js +121 -147
  26. package/dist/esm/session/accountSwitchTargets.js +71 -0
  27. package/dist/esm/session/deviceDirectory.js +135 -0
  28. package/dist/esm/session/deviceSwitcherRows.js +72 -0
  29. package/dist/esm/session/projectSessionState.js +8 -2
  30. package/dist/esm/session/sharedDeviceCredential.js +239 -0
  31. package/dist/types/.tsbuildinfo +1 -1
  32. package/dist/types/boot/sessionColdBoot.d.ts +24 -4
  33. package/dist/types/index.d.ts +8 -3
  34. package/dist/types/mixins/OxyServices.auth.d.ts +75 -3
  35. package/dist/types/models/session.d.ts +11 -0
  36. package/dist/types/session/SessionClient.d.ts +202 -1
  37. package/dist/types/session/accountDialogController.d.ts +76 -64
  38. package/dist/types/session/accountSwitchTargets.d.ts +64 -0
  39. package/dist/types/session/deviceDirectory.d.ts +182 -0
  40. package/dist/types/session/deviceSwitcherRows.d.ts +92 -0
  41. package/dist/types/session/projectSessionState.d.ts +29 -0
  42. package/dist/types/session/sharedDeviceCredential.d.ts +202 -0
  43. package/package.json +3 -3
  44. package/src/boot/__tests__/sessionColdBoot.sharedDevice.test.ts +325 -0
  45. package/src/boot/sessionColdBoot.ts +133 -9
  46. package/src/i18n/locales/en-US.json +19 -2
  47. package/src/i18n/locales/es-ES.json +19 -2
  48. package/src/index.ts +75 -18
  49. package/src/mixins/OxyServices.auth.ts +67 -5
  50. package/src/mixins/__tests__/preSessionSkipAuth.test.ts +54 -1
  51. package/src/models/session.ts +11 -0
  52. package/src/session/SessionClient.ts +386 -1
  53. package/src/session/__tests__/SessionClient.directory.test.ts +688 -0
  54. package/src/session/__tests__/accountDialogController.test.ts +411 -278
  55. package/src/session/__tests__/accountSwitchTargets.test.ts +132 -0
  56. package/src/session/__tests__/deviceDirectory.test.ts +422 -0
  57. package/src/session/__tests__/deviceSwitcherRows.test.ts +223 -0
  58. package/src/session/__tests__/projectSessionState.test.ts +17 -0
  59. package/src/session/__tests__/sharedDeviceCredential.test.ts +300 -0
  60. package/src/session/accountDialogController.ts +141 -179
  61. package/src/session/accountSwitchTargets.ts +87 -0
  62. package/src/session/deviceDirectory.ts +269 -0
  63. package/src/session/deviceSwitcherRows.ts +145 -0
  64. package/src/session/projectSessionState.ts +9 -3
  65. package/src/session/sharedDeviceCredential.ts +349 -0
  66. package/dist/cjs/session/accountProjection.js +0 -213
  67. package/dist/esm/session/accountProjection.js +0 -207
  68. package/dist/types/session/accountProjection.d.ts +0 -198
  69. package/src/session/__tests__/accountProjection.test.ts +0 -447
  70. package/src/session/accountProjection.ts +0 -354
@@ -1,13 +1,19 @@
1
1
  import {
2
+ deviceActivateResponseSchema,
3
+ deviceDirectorySchema,
4
+ deviceDirectorySyncSchema,
2
5
  deviceSessionStateSchema,
3
6
  deviceSessionSyncSchema,
4
7
  safeParseContract,
5
8
  SESSION_ACCOUNTS_CHANGED_EVENT,
6
9
  sessionAccountsChangedEventSchema,
10
+ type DeviceDirectory,
7
11
  type DeviceSessionState,
12
+ type DeviceSessionSync,
8
13
  } from '@oxyhq/contracts';
9
14
  import { logger } from '../logger';
10
15
  import { computeIdentityTag } from '../utils/cacheKey';
16
+ import { resolveActiveContext, type DeviceContext } from './deviceDirectory';
11
17
  import { getSocketIO } from './socketLoader';
12
18
  import type { MinimalSocket, SocketIOFactory } from './socketLoader';
13
19
 
@@ -98,6 +104,7 @@ export interface SessionClientOptions {
98
104
  }
99
105
 
100
106
  type StateListener = (state: DeviceSessionState | null) => void;
107
+ type DirectoryListener = (directory: DeviceDirectory | null) => void;
101
108
 
102
109
  /**
103
110
  * Same-origin `BroadcastChannel` name for instant, network-free session-state
@@ -118,7 +125,20 @@ interface SessionBroadcastChannel {
118
125
 
119
126
  export class SessionClient {
120
127
  private state: DeviceSessionState | null = null;
128
+ /**
129
+ * The device DIRECTORY (ADR 0002) — principals, the contexts each may act as,
130
+ * and which context is active. Held BESIDE `state` rather than replacing it:
131
+ * `state` is the flat compatibility projection every app renders until Phase 7
132
+ * moves it, and the two describe the same device at the same `revision`.
133
+ *
134
+ * `null` until something asks for it. A client that never calls
135
+ * {@link refreshDirectory} has no consumer for a directory and never pays for
136
+ * the round trip — which is also what keeps the whole account lane's request
137
+ * count unchanged.
138
+ */
139
+ private directory: DeviceDirectory | null = null;
121
140
  private readonly listeners = new Set<StateListener>();
141
+ private readonly directoryListeners = new Set<DirectoryListener>();
122
142
  protected socket: MinimalSocket | null = null;
123
143
  private tokenUnsub: (() => void) | null = null;
124
144
  private started = false;
@@ -138,6 +158,27 @@ export class SessionClient {
138
158
  return this.state;
139
159
  }
140
160
 
161
+ /**
162
+ * The device directory, or `null` when this client has never read one.
163
+ * Populated by {@link refreshDirectory} / {@link activateContext} and kept
164
+ * fresh from there on.
165
+ */
166
+ getDirectory(): DeviceDirectory | null {
167
+ return this.directory;
168
+ }
169
+
170
+ /**
171
+ * The active `principal acting as account` pair, with the actor and the
172
+ * subject kept apart. `null` when no directory has been read, or when the
173
+ * device genuinely has no active context.
174
+ *
175
+ * This is the answer `getState()` cannot give: `activeAccountId` names the
176
+ * subject and says nothing about whose authentication is behind it.
177
+ */
178
+ getActiveContext(): DeviceContext | null {
179
+ return resolveActiveContext(this.directory);
180
+ }
181
+
141
182
  /**
142
183
  * The account this client's bearer is pinned to, or `null` when it follows the
143
184
  * device's active account (the default). Resolvers are expected to be a plain
@@ -155,6 +196,20 @@ export class SessionClient {
155
196
  };
156
197
  }
157
198
 
199
+ /**
200
+ * Subscribe to the directory half. Fires from the SAME {@link notify} as
201
+ * {@link subscribe}, so the flat state and the directory are never published
202
+ * at two different points of the ordering sequence — a directory subscriber
203
+ * and a state subscriber woken by one transition always see the same device
204
+ * revision under the same bearer.
205
+ */
206
+ subscribeDirectory(listener: DirectoryListener): () => void {
207
+ this.directoryListeners.add(listener);
208
+ return () => {
209
+ this.directoryListeners.delete(listener);
210
+ };
211
+ }
212
+
158
213
  /**
159
214
  * Subscribe to a named server-pushed Socket.IO event (e.g. `civic:attested`).
160
215
  * Listeners survive reconnects and socket re-creation; the returned function
@@ -197,6 +252,13 @@ export class SessionClient {
197
252
  logger.error('[SessionClient] subscriber threw', error);
198
253
  }
199
254
  }
255
+ for (const listener of this.directoryListeners) {
256
+ try {
257
+ listener(this.directory);
258
+ } catch (error) {
259
+ logger.error('[SessionClient] directory subscriber threw', error);
260
+ }
261
+ }
200
262
  }
201
263
 
202
264
  /**
@@ -238,6 +300,13 @@ export class SessionClient {
238
300
  }
239
301
  const previousState = this.state;
240
302
  this.state = next;
303
+ if (next.accounts.length === 0) {
304
+ // A device with nobody signed in has no principals either. The directory
305
+ // is only ever refreshed by a bearer-carrying read and a sign-out leaves
306
+ // no bearer, so `settleDirectory` below cannot correct it — without this,
307
+ // the switcher would go on rendering the people who used to be here.
308
+ this.directory = null;
309
+ }
241
310
  const pinnedAccountId = this.pinnedAccountId();
242
311
  // Plant the sync-supplied active token (it is for `next.activeAccountId`)
243
312
  // now — before the notify below — so the bearer matches the new active
@@ -271,7 +340,7 @@ export class SessionClient {
271
340
  next.accounts.length > 0 &&
272
341
  (activeAccountId === null || computeIdentityTag(this.host.getAccessToken()) !== activeAccountId);
273
342
 
274
- const finishApply = (): void => {
343
+ const publish = (): void => {
275
344
  this.notify();
276
345
  if (next.accounts.length === 0 && this.options.onUnauthenticated) {
277
346
  try {
@@ -282,6 +351,22 @@ export class SessionClient {
282
351
  }
283
352
  };
284
353
 
354
+ // The directory half of the same ordering invariant: when this client holds
355
+ // a directory and the flat state has just moved past it, re-read the
356
+ // directory BEFORE anyone is notified — otherwise a directory-rendering
357
+ // consumer observes the PREVIOUS subject under the new subject's bearer,
358
+ // which is the account-switch race mirrored. `settleDirectory` returns null
359
+ // (and this stays synchronous) for every client that never read a
360
+ // directory, i.e. the whole account lane.
361
+ const finishApply = (): void => {
362
+ const settling = this.settleDirectory(next);
363
+ if (settling === null) {
364
+ publish();
365
+ return;
366
+ }
367
+ void settling.then(publish);
368
+ };
369
+
285
370
  if (needsMintBeforeNotify) {
286
371
  void transport.ensureActiveToken(next).then(finishApply).catch((error) => {
287
372
  logger.warn('[SessionClient] ensureActiveToken failed — reverting session state', { component: 'SessionClient' }, error);
@@ -325,6 +410,20 @@ export class SessionClient {
325
410
  logger.warn('[SessionClient] discarded invalid session sync', { component: 'SessionClient', issues, keys });
326
411
  return;
327
412
  }
413
+ this.commitSync(sync);
414
+ }
415
+
416
+ /**
417
+ * The apply + token-plant half of {@link applySync}, on an ALREADY-VALIDATED
418
+ * sync. Split out so the context-aware removal lane — whose response is a
419
+ * different wire shape and therefore a different parse — reuses this ordering
420
+ * verbatim instead of re-deriving it. Two implementations of
421
+ * "plant before notify" is two chances to get it wrong once.
422
+ *
423
+ * Returns whether `applyState` applied, so a caller holding a second half
424
+ * (the directory) can decide whether anything still needs publishing.
425
+ */
426
+ private commitSync(sync: DeviceSessionSync): boolean {
328
427
  // A `sync` is always the response to a direct REST call this client made
329
428
  // (bootstrap / switch / signOut / add) → a `request`-origin, authoritative
330
429
  // verdict. Hand the active token to `applyState`: in the applied path it is
@@ -349,6 +448,166 @@ export class SessionClient {
349
448
  ) {
350
449
  this.host.setTokens(sync.activeToken.accessToken);
351
450
  }
451
+ return applied;
452
+ }
453
+
454
+ /**
455
+ * Validate + last-writer-wins, `deviceId`-SCOPED exactly as {@link applyState}
456
+ * is for the flat half: a directory belonging to a DIFFERENT device resets the
457
+ * baseline and is accepted at any revision, so a freshly-converged device
458
+ * cannot lose to a retired device's higher number.
459
+ *
460
+ * The comparison itself is deliberately WEAKER than the flat state's. There it
461
+ * is `revision <= current` — correct, because a `DeviceSessionState` arrives
462
+ * out of band over a socket, so a straggler can genuinely land after a newer
463
+ * one. A directory only ever arrives as the response to a request THIS client
464
+ * just made, so the newest response is the freshest answer and only a strictly
465
+ * LOWER revision can be a straggler (two GETs racing).
466
+ *
467
+ * Equal-revision reads are not redundant, and rejecting them was a bug: the
468
+ * directory includes rows projected from the account GRAPH, and the server
469
+ * materializes a context for every account a principal may act as WITHOUT
470
+ * bumping `revision` — deliberately, since `revision` tracks what the device
471
+ * holds and must never advance on a read. So a newly-granted `account:act_as`,
472
+ * and a removed-then-rematerialized context under its NEW id, both appear at
473
+ * an unchanged revision. Under `<=` neither would ever be seen until some
474
+ * unrelated device mutation happened to move the number.
475
+ *
476
+ * Notifies nothing. Every caller decides where in its own ordering sequence
477
+ * the publish belongs.
478
+ */
479
+ private applyDirectory(raw: unknown): boolean {
480
+ const next = safeParseContract(deviceDirectorySchema, raw);
481
+ if (!next) {
482
+ logger.warn('[SessionClient] discarded invalid device directory');
483
+ return false;
484
+ }
485
+ if (
486
+ this.directory &&
487
+ next.deviceId === this.directory.deviceId &&
488
+ next.revision < this.directory.revision
489
+ ) {
490
+ return false;
491
+ }
492
+ this.directory = next;
493
+ return true;
494
+ }
495
+
496
+ /** `GET /session/device/directory` → {@link applyDirectory}. No notify. */
497
+ private async fetchDirectory(): Promise<boolean> {
498
+ const res = await this.host.makeRequest<unknown>('GET', '/session/device/directory', undefined, { cache: false });
499
+ return this.applyDirectory(res);
500
+ }
501
+
502
+ /**
503
+ * Re-read the directory when the flat state has moved past it, returning the
504
+ * in-flight work so the caller can hold its notify until both halves describe
505
+ * the same revision.
506
+ *
507
+ * `null` — meaning "nothing to settle, stay synchronous" — when this client
508
+ * holds no directory (nobody reads one), when the directory is already at or
509
+ * ahead of the state, or when there is no bearer to make the call with.
510
+ * Never rejects: a failed refresh leaves the previous directory in place and
511
+ * the next transition tries again; it must not swallow the flat state's
512
+ * notify.
513
+ */
514
+ private settleDirectory(state: DeviceSessionState): Promise<void> | null {
515
+ const held = this.directory;
516
+ if (held === null) {
517
+ return null;
518
+ }
519
+ if (held.deviceId === state.deviceId && held.revision >= state.revision) {
520
+ return null;
521
+ }
522
+ if (!this.host.getAccessToken()) {
523
+ return null;
524
+ }
525
+ return this.fetchDirectory().then(
526
+ () => undefined,
527
+ (error: unknown) => {
528
+ logger.warn('[SessionClient] directory refresh failed', { component: 'SessionClient' }, error);
529
+ },
530
+ );
531
+ }
532
+
533
+ /**
534
+ * The highest device revision this client currently knows for `deviceId`,
535
+ * across BOTH halves, or `null` when it knows nothing about that device.
536
+ *
537
+ * Used to read the server's `changed` flag, which
538
+ * `POST /session/device/activate` deliberately does not carry: the revision
539
+ * already says whether the device moved, and a second field saying the same
540
+ * thing is a second field that can disagree with the first.
541
+ */
542
+ private knownRevisionFor(deviceId: string): number | null {
543
+ const fromDirectory = this.directory?.deviceId === deviceId ? this.directory.revision : null;
544
+ const fromState = this.state?.deviceId === deviceId ? this.state.revision : null;
545
+ if (fromDirectory === null) return fromState;
546
+ if (fromState === null) return fromDirectory;
547
+ return Math.max(fromDirectory, fromState);
548
+ }
549
+
550
+ /**
551
+ * Plant the bearer `POST /session/device/activate` returned for the newly
552
+ * active context, under the same guards {@link applyState} applies to a
553
+ * sync-supplied `activeToken`: never for a null active context, never a
554
+ * foreign account's token while pinned, and never a redundant re-plant of the
555
+ * token already held.
556
+ *
557
+ * `activeToken: null` is not an error — it is an identity-pinned client, or a
558
+ * caller whose application is not entitled to a bearer for the new context.
559
+ */
560
+ private plantActiveContextToken(directory: DeviceDirectory, accessToken: string | undefined): void {
561
+ if (!accessToken) {
562
+ return;
563
+ }
564
+ const subjectAccountId = resolveActiveContext(directory)?.subject.accountId ?? null;
565
+ if (subjectAccountId === null) {
566
+ return;
567
+ }
568
+ const pinnedAccountId = this.pinnedAccountId();
569
+ if (pinnedAccountId !== null && subjectAccountId !== pinnedAccountId) {
570
+ return;
571
+ }
572
+ if (accessToken === this.host.getAccessToken()) {
573
+ return;
574
+ }
575
+ this.host.setTokens(accessToken);
576
+ }
577
+
578
+ /**
579
+ * Bring the FLAT projection back in step after a context activation.
580
+ *
581
+ * The activation response answers with the directory and a bearer and
582
+ * deliberately not with `DeviceSessionState` (ADR 0002) — but every app that
583
+ * has not moved to the directory still renders from `getState()`, and leaving
584
+ * it a revision behind would show the PREVIOUS subject under the new
585
+ * subject's bearer. So it is settled BEFORE the notify, not after.
586
+ *
587
+ * This is also what converges the bearer when the activation returned no
588
+ * token: `GET /session/device/state` mints one for the active account, and
589
+ * `applyState`'s own mint-before-notify gate holds its notify until it lands.
590
+ *
591
+ * Non-fatal on failure — the directory is applied and (usually) the bearer is
592
+ * planted; the socket push or the next bootstrap catches the flat half up. A
593
+ * network blip must not turn a completed activation into a thrown error.
594
+ */
595
+ private async reconcileFlatState(directory: DeviceDirectory): Promise<void> {
596
+ if (
597
+ this.state &&
598
+ this.state.deviceId === directory.deviceId &&
599
+ this.state.revision >= directory.revision
600
+ ) {
601
+ return;
602
+ }
603
+ if (!this.host.getAccessToken()) {
604
+ return;
605
+ }
606
+ try {
607
+ await this.bootstrap();
608
+ } catch (error) {
609
+ logger.warn('[SessionClient] flat-state reconcile after activation failed', { component: 'SessionClient' }, error);
610
+ }
352
611
  }
353
612
 
354
613
  async bootstrap(): Promise<void> {
@@ -356,18 +615,144 @@ export class SessionClient {
356
615
  this.applySync(res);
357
616
  }
358
617
 
618
+ /**
619
+ * Read `GET /session/device/directory` and publish it.
620
+ *
621
+ * Calling this is what opts a client into the directory: from here on every
622
+ * applied device state re-reads it (see {@link settleDirectory}), so the two
623
+ * halves stay at one revision without the caller polling.
624
+ */
625
+ async refreshDirectory(): Promise<void> {
626
+ if (await this.fetchDirectory()) {
627
+ this.notify();
628
+ }
629
+ }
630
+
631
+ /**
632
+ * `POST /session/device/activate` — make one `principal acting as account`
633
+ * context active (ADR 0002).
634
+ *
635
+ * The body is `{ contextId }` and nothing else: an `accountId` cannot name
636
+ * what to activate on a device where two people can both reach the same
637
+ * organization, and the server refuses a body carrying one rather than
638
+ * guessing inside an authorization path.
639
+ *
640
+ * The sequence is the ADR's ordering invariant, in order — commit the bearer
641
+ * for the new context, publish the new snapshot, notify — with the flat
642
+ * projection reconciled in the middle so no consumer can observe the two
643
+ * halves disagreeing.
644
+ *
645
+ * An IDEMPOTENT activation (the target was already active) moves no revision,
646
+ * so it reconciles nothing and wakes no sibling tab, mirroring the server's
647
+ * "bumps nothing and broadcasts nothing". `switchAccount` remains the
648
+ * compatibility path for callers still keyed on account ids.
649
+ */
650
+ async activateContext(contextId: string): Promise<void> {
651
+ const res = await this.host.makeRequest<unknown>('POST', '/session/device/activate', { contextId }, { cache: false });
652
+ const activation = safeParseContract(deviceActivateResponseSchema, res);
653
+ if (!activation) {
654
+ logger.warn('[SessionClient] discarded invalid activation response');
655
+ return;
656
+ }
657
+ const known = this.knownRevisionFor(activation.directory.deviceId);
658
+ const moved = known === null || activation.directory.revision > known;
659
+ this.plantActiveContextToken(activation.directory, activation.activeToken?.accessToken);
660
+ // Applied BEFORE the reconcile, and only then: the reconcile's own
661
+ // `GET /session/device/state` runs the full apply path, whose
662
+ // `settleDirectory` would otherwise see a stale directory and issue a
663
+ // second, redundant `GET /session/device/directory` for the revision we are
664
+ // already holding in hand.
665
+ const applied = this.applyDirectory(activation.directory);
666
+ if (moved) {
667
+ await this.reconcileFlatState(activation.directory);
668
+ }
669
+ if (applied) {
670
+ this.notify();
671
+ }
672
+ if (moved) {
673
+ this.postCommitPing();
674
+ }
675
+ }
676
+
359
677
  async switchAccount(accountId: string): Promise<void> {
360
678
  const res = await this.host.makeRequest<unknown>('POST', '/session/device/switch', { accountId }, { cache: false });
361
679
  this.applySync(res);
362
680
  this.postCommitPing();
363
681
  }
364
682
 
683
+ /**
684
+ * The FLAT removal meanings, unchanged: `{ accountId }` removes that account
685
+ * however it is reached — plus the operator cascade — and `{ all: true }`
686
+ * removes the whole device including its credentials.
687
+ *
688
+ * `{ accountId }` is deliberately still account-grained. On a device holding
689
+ * two people it removes BOTH of their routes to that account, which is the
690
+ * right meaning for "sign this account out of this device" and the wrong one
691
+ * for "this person is done here" — see {@link signOutContext} and
692
+ * {@link signOutPrincipal} for the two that can tell those apart.
693
+ */
365
694
  async signOut(target: { accountId: string } | { all: true }): Promise<void> {
366
695
  const res = await this.host.makeRequest<unknown>('POST', '/session/device/signout', target, { cache: false });
367
696
  this.applySync(res);
368
697
  this.postCommitPing();
369
698
  }
370
699
 
700
+ /**
701
+ * Remove ONE `principal → account` pair, and only that pair.
702
+ *
703
+ * Never the account across the device: the same organization reached through
704
+ * a second person is a different session, a different audit actor and a
705
+ * different revocation path, and it stays. That distinction is unreachable
706
+ * through {@link signOut}, whose `accountId` cannot name which route to drop.
707
+ *
708
+ * Removal is not permanent while the membership lives — the server offers the
709
+ * pair again on the next directory read, as `onDevice: false` under a NEW id.
710
+ */
711
+ async signOutContext(contextId: string): Promise<void> {
712
+ await this.removeFromDevice({ contextId });
713
+ }
714
+
715
+ /**
716
+ * Remove ONE PERSON and every context they reach — and nobody else's,
717
+ * including when another principal independently operates the same account.
718
+ */
719
+ async signOutPrincipal(principalId: string): Promise<void> {
720
+ await this.removeFromDevice({ principalId });
721
+ }
722
+
723
+ /**
724
+ * The shared apply path for both context-aware removals.
725
+ *
726
+ * The response is `{directory, state, activeToken}` — its own contract, never
727
+ * `deviceSessionSyncSchema`, which would strip the directory silently. Both
728
+ * halves move in one server transition (a removal elects a replacement active
729
+ * context), so both are applied before anything is published: the directory
730
+ * first, so the flat apply's own `settleDirectory` sees a current directory
731
+ * and does not issue a redundant `GET /session/device/directory` for the
732
+ * revision already in hand.
733
+ *
734
+ * Token-before-notify is `commitSync`'s, reused verbatim rather than
735
+ * re-derived — including the equal-revision plant when a socket push already
736
+ * applied this revision.
737
+ */
738
+ private async removeFromDevice(target: { contextId: string } | { principalId: string }): Promise<void> {
739
+ const res = await this.host.makeRequest<unknown>('POST', '/session/device/signout', target, { cache: false });
740
+ const removal = safeParseContract(deviceDirectorySyncSchema, res);
741
+ if (!removal) {
742
+ logger.warn('[SessionClient] discarded invalid device removal response');
743
+ return;
744
+ }
745
+ const directoryApplied = this.applyDirectory(removal.directory);
746
+ const stateApplied = this.commitSync({ state: removal.state, activeToken: removal.activeToken });
747
+ // `commitSync` publishes whenever the flat state moved. When only the
748
+ // directory did — a socket push already applied this revision — the
749
+ // directory half would otherwise never reach a subscriber.
750
+ if (directoryApplied && !stateApplied) {
751
+ this.notify();
752
+ }
753
+ this.postCommitPing();
754
+ }
755
+
371
756
  async addCurrentAccount(): Promise<void> {
372
757
  const res = await this.host.makeRequest<unknown>('POST', '/session/device/add', undefined, { cache: false });
373
758
  this.applySync(res);