-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Expand file tree
/
Copy pathcollab-presence.ts
More file actions
984 lines (939 loc) · 33.3 KB
/
Copy pathcollab-presence.ts
File metadata and controls
984 lines (939 loc) · 33.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
import type { Express, Request, Response } from 'express';
import type {
CollabPresenceMember,
WorkspaceCollabContext,
} from '@open-design/contracts';
import type { CollabRuntime } from '../collab/runtime.js';
import type { PresenceMember } from '../collab/presence-tracker.js';
import type {
VelaCliPresenceHeartbeatInput,
VelaCliPresenceLeaveInput,
} from '../collab/vela-cli-collab-client.js';
import type {
VerifiedWorkspaceRequestContextResult,
} from '../collab/request-workspace-context.js';
import {
classifyCollabCloudError,
CollabCloudUpstreamBlockedError,
type ClassifiedCollabCloudError,
} from '../collab/collab-cloud-error.js';
type PresenceActivity = Exclude<PresenceMember['activity'], undefined>;
export interface CollabPresenceCloudClient {
heartbeatPresence(
projectId: string,
input: VelaCliPresenceHeartbeatInput,
context?: WorkspaceCollabContext | null,
): Promise<CollabPresenceMember[]>;
listPresence(
projectId: string,
context?: WorkspaceCollabContext | null,
): Promise<CollabPresenceMember[]>;
leavePresence(
projectId: string,
input: VelaCliPresenceLeaveInput,
context?: WorkspaceCollabContext | null,
): Promise<CollabPresenceMember[]>;
}
export interface RegisterCollabPresenceRoutesDeps {
collab: Pick<CollabRuntime, 'presence'>;
cloud?: CollabPresenceCloudClient | null;
isProjectShared?: (
projectId: string,
context?: WorkspaceCollabContext | null,
) => Promise<boolean>;
verifyWorkspaceRequest?: (
req: Request,
projectId: string,
) => Promise<
| VerifiedWorkspaceRequestContextResult
| WorkspaceCollabContext
| null
>;
/**
* Leave must bypass any background authority outage lease so closing a tab
* still gets one chance to release its remote presence lease. Heartbeats use
* `verifyWorkspaceRequest`; leave falls back to it when this seam is absent.
*/
verifyWorkspaceLeaveRequest?: RegisterCollabPresenceRoutesDeps['verifyWorkspaceRequest'];
/**
* Bounded successful authority lease for the idempotent GET surface.
* Heartbeat deliberately keeps using `verifyWorkspaceRequest`.
*/
verifyWorkspaceReadRequest?: RegisterCollabPresenceRoutesDeps['verifyWorkspaceRequest'];
/** Test/operations seam for the short-lived cloud list cache. */
presenceListCacheFreshMs?: number;
/** Virtual clock seam for deterministic TTL coverage. */
presenceListCacheNow?: () => number;
/**
* The configured cloud route authoritatively rejects projects outside the
* requested workspace, so a separate remote team-project lookup is redundant.
*/
cloudAuthorizesProjectPresence?: (projectId: string) => boolean;
/**
* Cooldown for the upstream negative cache: after repeated consecutive
* cloud-transport failures for one project, presence stops spawning the
* transport for this long and replays the classified failure instead.
*/
presenceUpstreamCooldownMs?: number;
/** Virtual clock seam for deterministic negative-cache coverage. */
presenceUpstreamNow?: () => number;
}
export interface CollabPresenceRoutesControl {
/**
* Preserve the last authorized roster but make it due for background
* refresh. Presence-change events use this path so their own relay echo
* cannot turn the next UI read into a cold, blocking Vela request.
*/
markPresenceStale(projectId: string, workspaceId?: string): void;
/**
* Drop cached list results for one project after a share-authority change.
* `workspaceId` keeps invalidation scoped when it is known.
*/
invalidatePresence(projectId: string, workspaceId?: string): void;
}
const DEFAULT_PRESENCE_LIST_CACHE_FRESH_MS = 1_000;
const MAX_PRESENCE_LIST_CACHE_ENTRIES = 256;
interface PresenceListCacheEntry {
projectId: string;
workspaceId: string;
value: CollabPresenceMember[] | null;
settledAt: number;
inflight: Promise<CollabPresenceMember[]> | null;
}
function presenceListCacheKey(
projectId: string,
context: WorkspaceCollabContext | null,
): string {
const permissions = context?.permissions;
return JSON.stringify([
projectId,
context?.workspaceId?.trim() ?? '',
context?.workspaceMemberId?.trim() ?? '',
context?.workspaceType ?? '',
context?.role ?? '',
context?.memberStatus ?? '',
context?.lifecycleState ?? '',
permissions?.canManageMembers ?? false,
permissions?.canManageBilling ?? false,
permissions?.canInviteMembers ?? false,
permissions?.canManageAutoRecharge ?? false,
permissions?.canShareProjects ?? false,
permissions?.canWriteSyncedFiles ?? false,
permissions?.canViewWorkspaceSettings ?? false,
permissions?.canManageSharedResources ?? false,
]);
}
function createPresenceListCache(options: {
freshMs: number;
now: () => number;
}) {
const entries = new Map<string, PresenceListCacheEntry>();
const store = (key: string, entry: PresenceListCacheEntry) => {
entries.delete(key);
entries.set(key, entry);
while (entries.size > MAX_PRESENCE_LIST_CACHE_ENTRIES) {
const oldestKey = entries.keys().next().value;
if (oldestKey === undefined) break;
entries.delete(oldestKey);
}
};
const refresh = (
key: string,
entry: PresenceListCacheEntry,
fetcher: () => Promise<CollabPresenceMember[]>,
): Promise<CollabPresenceMember[]> => {
const request = Promise.resolve().then(fetcher);
entry.inflight = request;
request.then(
(value) => {
if (entries.get(key) !== entry) return;
entry.value = value;
entry.settledAt = options.now();
entry.inflight = null;
},
() => {
if (entries.get(key) !== entry) return;
// A failed read is not presence evidence. Preserve a previously
// authorized roster so a transient relay/CLI outage cannot make every
// open project flash empty; keep it stale so the next read retries.
// Cold failures still remove the empty shell and retry normally.
if (entry.value !== null) {
entry.inflight = null;
} else {
entries.delete(key);
}
},
);
return request;
};
return {
read(
projectId: string,
context: WorkspaceCollabContext | null,
fetcher: () => Promise<CollabPresenceMember[]>,
readOptions?: { waitForFresh?: boolean },
): Promise<CollabPresenceMember[]> {
const key = presenceListCacheKey(projectId, context);
let entry = entries.get(key);
if (!entry) {
entry = {
projectId,
workspaceId: context?.workspaceId?.trim() ?? '',
value: null,
settledAt: 0,
inflight: null,
};
store(key, entry);
return refresh(key, entry, fetcher);
}
// Bounded LRU: a daemon that opens many historical projects must not
// retain every short-lived roster forever.
store(key, entry);
if (entry.value !== null) {
const value = entry.value;
let refreshRequest = entry.inflight;
if (
!refreshRequest
&& options.now() - entry.settledAt >= options.freshMs
) {
// Polls never wait on a fresh Vela process once this exact viewer has
// a value. Refresh in the background and keep concurrent callers on
// the same process.
refreshRequest = refresh(key, entry, fetcher);
void refreshRequest.catch(() => undefined);
}
if (readOptions?.waitForFresh && refreshRequest) return refreshRequest;
return Promise.resolve(value);
}
return entry.inflight ?? refresh(key, entry, fetcher);
},
publish(
projectId: string,
context: WorkspaceCollabContext | null,
value: CollabPresenceMember[],
): void {
const key = presenceListCacheKey(projectId, context);
store(key, {
projectId,
workspaceId: context?.workspaceId?.trim() ?? '',
value,
settledAt: options.now(),
inflight: null,
});
},
markStale(projectId: string, workspaceId?: string): void {
const exactWorkspaceId = workspaceId?.trim();
for (const entry of entries.values()) {
if (entry.projectId !== projectId) continue;
if (exactWorkspaceId && entry.workspaceId !== exactWorkspaceId) continue;
// Keep the last authorized roster for the immediate caller. The next
// read starts one background refresh and returns without waiting on
// the Vela process.
entry.settledAt = Number.NEGATIVE_INFINITY;
}
},
invalidate(projectId: string, workspaceId?: string): void {
const exactWorkspaceId = workspaceId?.trim();
for (const [key, entry] of entries) {
if (entry.projectId !== projectId) continue;
if (exactWorkspaceId && entry.workspaceId !== exactWorkspaceId) continue;
entries.delete(key);
}
},
};
}
/**
* The workspace-scoped presence surface of the collab transport, as this route
* module needs it. `VelaCliCollabClient` satisfies it structurally.
*/
export interface CollabPresenceCloudTransport {
heartbeatPresence(
projectId: string,
input: VelaCliPresenceHeartbeatInput,
workspaceId: string,
): Promise<CollabPresenceMember[]>;
listPresence(
projectId: string,
workspaceId: string,
): Promise<CollabPresenceMember[]>;
leavePresence(
projectId: string,
input: VelaCliPresenceLeaveInput,
workspaceId: string,
): Promise<CollabPresenceMember[]>;
}
/**
* Bind a collab transport to a per-project workspace scope, producing the
* `cloud` dependency below — or nothing when this run has no cloud transport.
*
* The invariant: **a `cloud` dependency exists if and only if a transport
* exists.** Every endpoint below reads a present `cloud` as "the cloud owns
* presence for this run" and stops consulting the process-local tracker
* entirely. So a relay built over an absent transport is not merely useless —
* it turns the in-process fallback into dead code and every gated presence
* request into a 502. Returning `null` is what keeps that fallback reachable
* for the runs that have no transport, which is every build that has not
* opted into the vela-cli collab transport: all stable/prod packaged builds
* and every plain `tools-dev` run.
*
* Callers must route through this rather than assembling an object literal of
* arrow functions, because a literal is unconditionally truthy no matter what
* the transport turned out to be.
*/
export function createCollabPresenceCloudClient(
transport: CollabPresenceCloudTransport | null | undefined,
workspaceScopeFor: (projectId: string) => string | undefined,
): CollabPresenceCloudClient | null {
if (!transport) return null;
const exactWorkspaceId = (
projectId: string,
context?: WorkspaceCollabContext | null,
): string => {
const workspaceId =
context?.workspaceId?.trim() || workspaceScopeFor(projectId)?.trim() || '';
if (!workspaceId) {
throw Object.assign(new Error('workspace scope is unavailable'), {
code: 'workspace_scope_unavailable',
});
}
return workspaceId;
};
return {
heartbeatPresence: (projectId, input, context) =>
transport.heartbeatPresence(
projectId,
input,
exactWorkspaceId(projectId, context),
),
listPresence: (projectId, context) =>
transport.listPresence(
projectId,
exactWorkspaceId(projectId, context),
),
leavePresence: (projectId, input, context) =>
transport.leavePresence(
projectId,
input,
exactWorkspaceId(projectId, context),
),
};
}
function readHeartbeat(body: unknown): {
member: PresenceMember;
clientId: string;
sequence?: number;
filePath?: string | null;
activity?: PresenceMember['activity'];
} | null {
const raw = (body ?? {}) as Record<string, unknown>;
const memberId = typeof raw.memberId === 'string' ? raw.memberId.trim() : '';
if (!memberId) return null;
const sequence = readPresenceSequence(raw);
if (sequence === null) return null;
const member: PresenceMember = { memberId };
if (typeof raw.name === 'string' && raw.name.trim()) member.name = raw.name.trim();
if (raw.role === 'owner' || raw.role === 'admin' || raw.role === 'member') member.role = raw.role;
if (typeof raw.avatarUrl === 'string' || raw.avatarUrl === null) member.avatarUrl = raw.avatarUrl;
if (typeof raw.filePath === 'string' || raw.filePath === null) member.filePath = raw.filePath;
if (raw.activity !== undefined) member.activity = raw.activity as PresenceActivity;
const explicitClientId = typeof raw.clientId === 'string' && raw.clientId.trim()
? raw.clientId.trim()
: '';
// A sequence only has meaning inside a stable session identity. Legacy
// requests may still omit both fields and retain the member-id fallback.
if (sequence !== undefined && !explicitClientId) return null;
const clientId = explicitClientId || memberId;
const filePath = typeof raw.filePath === 'string' || raw.filePath === null
? raw.filePath
: undefined;
return {
member,
clientId,
...(sequence !== undefined ? { sequence } : {}),
...(filePath !== undefined ? { filePath } : {}),
...(member.activity !== undefined ? { activity: member.activity } : {}),
};
}
function readLeave(body: unknown): {
memberId: string;
clientId: string;
sequence?: number;
} | null {
const raw = (body ?? {}) as Record<string, unknown>;
const memberId = typeof raw.memberId === 'string' ? raw.memberId.trim() : '';
if (!memberId) return null;
const sequence = readPresenceSequence(raw);
if (sequence === null) return null;
const explicitClientId = typeof raw.clientId === 'string' && raw.clientId.trim()
? raw.clientId.trim()
: '';
if (sequence !== undefined && !explicitClientId) return null;
return {
memberId,
clientId: explicitClientId || memberId,
...(sequence !== undefined ? { sequence } : {}),
};
}
function readPresenceSequence(
raw: Record<string, unknown>,
): number | undefined | null {
if (!Object.prototype.hasOwnProperty.call(raw, 'sequence')) return undefined;
return Number.isSafeInteger(raw.sequence) && Number(raw.sequence) > 0
? Number(raw.sequence)
: null;
}
/**
* Answer a classified cloud-transport failure. Authoritative upstream
* rejections keep their meaning (403/404) so the web client's existing
* forbidden-request handling applies; only genuine infrastructure failures
* stay 502/503. The response body is intentionally free of transport
* internals — the spawned command line and stderr stay in the daemon log
* (see {@link cloudError}).
*/
function sendClassifiedCloudError(
res: Response,
classified: ClassifiedCollabCloudError,
) {
const upstreamSuffix =
classified.upstreamStatus == null
? ''
: ` (upstream status ${classified.upstreamStatus})`;
const responseByKind = {
denied: {
error: 'collab_presence_denied',
message: `the collab cloud rejected this presence request${upstreamSuffix}`,
},
not_found: {
error: 'collab_presence_not_found',
message: `the collab cloud has no record of this project${upstreamSuffix}`,
},
timeout: {
error: 'collab_presence_unavailable',
message: 'the collab cloud transport timed out',
},
infrastructure: {
error: 'collab_presence_unavailable',
message: `the collab cloud transport failed${upstreamSuffix}`,
},
} as const;
return res.status(classified.status).json({
...responseByKind[classified.kind],
...(classified.retryable ? { retryable: true } : {}),
});
}
function cloudError(res: Response, error: unknown) {
if (error instanceof CollabCloudUpstreamBlockedError) {
return sendClassifiedCloudError(res, error.classified);
}
const classified = classifyCollabCloudError(error);
// The full failure — command line, stderr — is diagnostic gold but must not
// reach the renderer; keep it daemon-side.
console.warn(
`[od] collab_presence_upstream_failure kind=${classified.kind} status=${classified.status} `
+ `detail=${JSON.stringify(error instanceof Error ? error.message : String(error))}`,
);
return sendClassifiedCloudError(res, classified);
}
const DEFAULT_PRESENCE_UPSTREAM_COOLDOWN_MS = 20_000;
/** Failures must be consecutive — one transient blip never opens the cache. */
const PRESENCE_UPSTREAM_FAILURE_THRESHOLD = 2;
const MAX_PRESENCE_UPSTREAM_FAILURE_ENTRIES = 256;
interface PresenceUpstreamFailureState {
consecutiveFailures: number;
blockedUntil: number;
classified: ClassifiedCollabCloudError;
}
/**
* Negative cache for a persistently failing collab cloud upstream.
*
* Every presence heartbeat/list over the vela-cli transport spawns a child
* process. When the upstream keeps rejecting the same project (the packaged
* 502-storm incident: hours of one CLI spawn per 10s beat against a dead
* upstream), repeated consecutive failures open a cooldown during which the
* stored classification is replayed without spawning; the first attempt after
* the cooldown probes the upstream again. Any success closes the entry.
*/
function createPresenceUpstreamFailureCache(options: {
cooldownMs: number;
now: () => number;
}) {
const states = new Map<string, PresenceUpstreamFailureState>();
const keyFor = (
projectId: string,
context: WorkspaceCollabContext | null,
): string => `${context?.workspaceId?.trim() ?? ''}\0${projectId}`;
const touch = (key: string, state: PresenceUpstreamFailureState) => {
states.delete(key);
states.set(key, state);
while (states.size > MAX_PRESENCE_UPSTREAM_FAILURE_ENTRIES) {
const oldestKey = states.keys().next().value;
if (oldestKey === undefined) break;
states.delete(oldestKey);
}
};
return {
/** The classification to replay while the cooldown is active, else null. */
active(
projectId: string,
context: WorkspaceCollabContext | null,
): ClassifiedCollabCloudError | null {
const state = states.get(keyFor(projectId, context));
if (!state) return null;
return options.now() < state.blockedUntil ? state.classified : null;
},
recordFailure(
projectId: string,
context: WorkspaceCollabContext | null,
classified: ClassifiedCollabCloudError,
): void {
const key = keyFor(projectId, context);
const state = states.get(key) ?? {
consecutiveFailures: 0,
blockedUntil: 0,
classified,
};
state.consecutiveFailures += 1;
state.classified = classified;
if (state.consecutiveFailures >= PRESENCE_UPSTREAM_FAILURE_THRESHOLD) {
state.blockedUntil = options.now() + options.cooldownMs;
}
touch(key, state);
},
recordSuccess(
projectId: string,
context: WorkspaceCollabContext | null,
): void {
states.delete(keyFor(projectId, context));
},
};
}
type PresenceWorkspaceVerification =
| { ok: true; context: WorkspaceCollabContext | null }
| Exclude<VerifiedWorkspaceRequestContextResult, { ok: true }>;
function normalizeWorkspaceVerification(
value:
| VerifiedWorkspaceRequestContextResult
| WorkspaceCollabContext
| null,
): PresenceWorkspaceVerification {
if (value && 'ok' in value) return value;
if (value) return { ok: true, context: value };
// Legacy injected test adapters used null as a route-specific denial.
// Production supplies the structured verifier result above.
return { ok: true, context: null };
}
function sendWorkspaceVerificationFailure(
res: Response,
verification: Exclude<PresenceWorkspaceVerification, { ok: true }>,
) {
return res.status(verification.status).json({
error: verification.code,
message: verification.message,
...(verification.retryable ? { retryable: true } : {}),
});
}
interface PresenceSessionFenceState {
latestSequence: number;
closed: boolean;
inflightHeartbeats: number;
}
interface PresenceHeartbeatFenceToken {
state: PresenceSessionFenceState;
accepted: boolean;
}
const MAX_PRESENCE_SESSION_FENCES = 4_096;
/**
* Process-local ordering for the Web → daemon presence protocol.
*
* A close tombstone rejects heartbeats that arrive after leave, while an
* already-running heartbeat observes the same state when it settles and sends
* one compensating leave. Legacy requests without a sequence retain their old
* behavior; new sessions use random client ids, so a closed old mount cannot
* affect its replacement.
*/
function createPresenceSessionFence() {
const states = new Map<string, PresenceSessionFenceState>();
const keyFor = (
projectId: string,
context: WorkspaceCollabContext | null,
memberId: string,
clientId: string,
) => JSON.stringify([
context?.workspaceId?.trim() ?? '',
projectId,
memberId,
clientId,
]);
const touch = (key: string, state: PresenceSessionFenceState) => {
states.delete(key);
states.set(key, state);
while (states.size > MAX_PRESENCE_SESSION_FENCES) {
let removed = false;
for (const [candidateKey, candidate] of states) {
if (candidate.inflightHeartbeats > 0) continue;
states.delete(candidateKey);
removed = true;
break;
}
if (!removed) break;
}
};
const stateFor = (key: string): PresenceSessionFenceState => {
const existing = states.get(key);
if (existing) return existing;
const created: PresenceSessionFenceState = {
latestSequence: 0,
closed: false,
inflightHeartbeats: 0,
};
touch(key, created);
return created;
};
return {
beginHeartbeat(input: {
projectId: string;
context: WorkspaceCollabContext | null;
memberId: string;
clientId: string;
sequence?: number;
}): PresenceHeartbeatFenceToken | null {
if (input.sequence === undefined) return null;
const key = keyFor(
input.projectId,
input.context,
input.memberId,
input.clientId,
);
const state = stateFor(key);
const accepted = !state.closed && input.sequence > state.latestSequence;
if (accepted) {
state.latestSequence = input.sequence;
state.inflightHeartbeats += 1;
}
touch(key, state);
return { state, accepted };
},
close(input: {
projectId: string;
context: WorkspaceCollabContext | null;
memberId: string;
clientId: string;
sequence?: number;
}): boolean {
if (input.sequence === undefined) return true;
const key = keyFor(
input.projectId,
input.context,
input.memberId,
input.clientId,
);
const state = stateFor(key);
if (input.sequence < state.latestSequence) {
touch(key, state);
return false;
}
// Repeating the same close is a valid retry. The first upstream leave
// may have failed after the local tombstone was recorded, and deleting
// the same client lease again is idempotent.
if (input.sequence === state.latestSequence) {
touch(key, state);
return state.closed;
}
state.latestSequence = input.sequence;
state.closed = true;
touch(key, state);
return true;
},
isClosed(token: PresenceHeartbeatFenceToken | null): boolean {
return token?.state.closed === true;
},
finishHeartbeat(token: PresenceHeartbeatFenceToken | null): void {
if (!token?.accepted) return;
token.state.inflightHeartbeats = Math.max(
0,
token.state.inflightHeartbeats - 1,
);
},
};
}
/**
* Team collaboration presence (presence) capability. Members heartbeat while viewing a
* shared project; clients poll the present set (live cursors were cut, content
* is polled — the spec). The set is process-local in {@link CollabRuntime}.
*/
export function registerCollabPresenceRoutes(
app: Express,
deps: RegisterCollabPresenceRoutesDeps,
): CollabPresenceRoutesControl {
const { presence } = deps.collab;
const cloud = deps.cloud ?? null;
const presenceLists = createPresenceListCache({
freshMs: Math.max(
0,
deps.presenceListCacheFreshMs ?? DEFAULT_PRESENCE_LIST_CACHE_FRESH_MS,
),
now: deps.presenceListCacheNow ?? Date.now,
});
const presenceSessions = createPresenceSessionFence();
const upstreamFailures = createPresenceUpstreamFailureCache({
cooldownMs: Math.max(
0,
deps.presenceUpstreamCooldownMs ?? DEFAULT_PRESENCE_UPSTREAM_COOLDOWN_MS,
),
now: deps.presenceUpstreamNow ?? Date.now,
});
async function projectIsShared(
projectId: string,
context: WorkspaceCollabContext | null,
): Promise<boolean> {
if (!deps.isProjectShared) return true;
try {
return await deps.isProjectShared(projectId, context);
} catch (error) {
return false;
}
}
function cloudAuthorizesProject(projectId: string): boolean {
if (!cloud || !deps.cloudAuthorizesProjectPresence) return false;
try {
return deps.cloudAuthorizesProjectPresence(projectId);
} catch {
return false;
}
}
async function verifiedContext(
req: Request,
projectId: string,
mode: 'read' | 'heartbeat' | 'leave',
): Promise<PresenceWorkspaceVerification> {
let verify = deps.verifyWorkspaceRequest;
if (mode === 'read') {
verify = deps.verifyWorkspaceReadRequest ?? verify;
} else if (mode === 'leave') {
verify = deps.verifyWorkspaceLeaveRequest ?? verify;
}
if (!verify) {
return { ok: true, context: null };
}
try {
return normalizeWorkspaceVerification(
await verify(req, projectId),
);
} catch {
return {
ok: false,
status: 503,
code: 'WORKSPACE_AUTHORITY_UNAVAILABLE',
message: 'workspace membership authority is temporarily unavailable',
retryable: true,
};
}
}
app.get('/api/projects/:id/presence', async (req, res) => {
const verification = await verifiedContext(req, req.params.id, 'read');
if (cloud && !verification.ok) {
return sendWorkspaceVerificationFailure(res, verification);
}
const context = verification.ok ? verification.context : null;
if (cloud) {
try {
return res.json({
present: await presenceLists.read(
req.params.id,
context,
async () => {
// When the upstream hub subscription cannot authoritatively
// vouch for this Workspace, resolving the project owner is part
// of the SAME expensive Vela read as the roster. Keeping this
// check outside `presenceLists` made every hot GET spawn a
// second CLI/catalog read even while the roster itself was
// cached. Explicit share/unshare and hub invalidation drop this
// entry, while the exact authority verification above still
// runs before every read and fails closed on membership loss.
if (
!cloudAuthorizesProject(req.params.id)
&& !(await projectIsShared(req.params.id, context))
) {
return [];
}
const blocked = upstreamFailures.active(req.params.id, context);
if (blocked) throw new CollabCloudUpstreamBlockedError(blocked);
try {
const present = await cloud.listPresence(req.params.id, context);
upstreamFailures.recordSuccess(req.params.id, context);
return present;
} catch (error) {
upstreamFailures.recordFailure(
req.params.id,
context,
classifyCollabCloudError(error),
);
throw error;
}
},
{ waitForFresh: req.query.fresh === '1' },
),
});
} catch (error) {
return cloudError(res, error);
}
}
if (!(await projectIsShared(req.params.id, context))) {
return res.json({ present: [] });
}
res.json({ present: presence.present(req.params.id) });
});
app.post('/api/projects/:id/presence/heartbeat', async (req, res) => {
const heartbeat = readHeartbeat(req.body);
if (!heartbeat) return res.status(400).json({ error: 'memberId required' });
const verification = await verifiedContext(req, req.params.id, 'heartbeat');
if (cloud && !verification.ok) {
return sendWorkspaceVerificationFailure(res, verification);
}
const context = verification.ok ? verification.context : null;
if (
cloud
&& deps.verifyWorkspaceRequest
&& (
!verification.ok
|| !context
|| heartbeat.member.memberId !== context.workspaceMemberId
)
) {
return res.status(403).json({ error: 'WORKSPACE_PROJECT_PRESENCE_DENIED' });
}
const heartbeatFence = presenceSessions.beginHeartbeat({
projectId: req.params.id,
context,
memberId: heartbeat.member.memberId,
clientId: heartbeat.clientId,
...(heartbeat.sequence !== undefined
? { sequence: heartbeat.sequence }
: {}),
});
if (heartbeatFence && !heartbeatFence.accepted) {
return res.json({ present: [] });
}
try {
if (
!cloudAuthorizesProject(req.params.id) &&
!(await projectIsShared(req.params.id, context))
) {
if (cloud) presenceLists.publish(req.params.id, context, []);
return res.json({ present: [] });
}
if (presenceSessions.isClosed(heartbeatFence)) {
return res.json({ present: [] });
}
if (cloud) {
// A project whose upstream keeps failing must not spawn another
// transport process on every 10s beat — replay the classified failure
// until the cooldown lets the next probe through.
const blocked = upstreamFailures.active(req.params.id, context);
if (blocked && !presenceSessions.isClosed(heartbeatFence)) {
return sendClassifiedCloudError(res, blocked);
}
let present: CollabPresenceMember[] | null = null;
let heartbeatError: unknown = null;
try {
present = await cloud.heartbeatPresence(
req.params.id,
heartbeat,
context,
);
upstreamFailures.recordSuccess(req.params.id, context);
} catch (error) {
upstreamFailures.recordFailure(
req.params.id,
context,
classifyCollabCloudError(error),
);
heartbeatError = error;
}
if (presenceSessions.isClosed(heartbeatFence)) {
// leave may already have completed while this heartbeat was still in
// flight. Close the same client lease once more after the old write
// settles so its late side effect cannot resurrect the session.
present = await cloud.leavePresence(
req.params.id,
{
memberId: heartbeat.member.memberId,
clientId: heartbeat.clientId,
},
context,
);
heartbeatError = null;
}
if (heartbeatError) throw heartbeatError;
const settledPresent = present ?? [];
presenceLists.publish(req.params.id, context, settledPresent);
return res.json({ present: settledPresent });
}
if (!presenceSessions.isClosed(heartbeatFence)) {
presence.heartbeat(
req.params.id,
heartbeat.member,
heartbeat.clientId,
);
}
return res.json({ present: presence.present(req.params.id) });
} catch (error) {
return cloudError(res, error);
} finally {
presenceSessions.finishHeartbeat(heartbeatFence);
}
});
app.post('/api/projects/:id/presence/leave', async (req, res) => {
const leave = readLeave(req.body);
if (!leave) return res.status(400).json({ error: 'memberId required' });
const verification = await verifiedContext(req, req.params.id, 'leave');
if (cloud && !verification.ok) {
return sendWorkspaceVerificationFailure(res, verification);
}
const context = verification.ok ? verification.context : null;
if (
cloud
&& (deps.verifyWorkspaceLeaveRequest ?? deps.verifyWorkspaceRequest)
&& (!verification.ok || !context || leave.memberId !== context.workspaceMemberId)
) {
return res.status(403).json({ error: 'WORKSPACE_PROJECT_PRESENCE_DENIED' });
}
const leaveAccepted = presenceSessions.close({
projectId: req.params.id,
context,
memberId: leave.memberId,
clientId: leave.clientId,
...(leave.sequence !== undefined ? { sequence: leave.sequence } : {}),
});
if (!leaveAccepted) {
return res.json({ ok: true, present: [] });
}
if (cloud) {
try {
// Leave is deliberately never gated by the upstream negative cache: a
// closing session must always try to release its remote lease.
const present = await cloud.leavePresence(
req.params.id,
leave,
context,
);
upstreamFailures.recordSuccess(req.params.id, context);
presenceLists.publish(req.params.id, context, present);
return res.json({
ok: true,
present,
});
} catch (error) {
return cloudError(res, error);
}
}
presence.leave(req.params.id, leave.memberId, leave.clientId);
res.json({ ok: true, present: presence.present(req.params.id) });
});
return {
markPresenceStale: (projectId, workspaceId) =>
presenceLists.markStale(projectId, workspaceId),
invalidatePresence: (projectId, workspaceId) =>
presenceLists.invalidate(projectId, workspaceId),
};
}