@myrialabs/clopen 0.4.34 → 0.4.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/backend/artifacts/sync.test.ts +63 -0
- package/backend/artifacts/sync.ts +56 -3
- package/backend/auth/permissions.ts +7 -0
- package/backend/containers/actions.ts +316 -0
- package/backend/containers/disk-usage.test.ts +68 -0
- package/backend/containers/disk-usage.ts +170 -0
- package/backend/containers/logs.ts +335 -0
- package/backend/containers/monitor.ts +410 -0
- package/backend/containers/parse.test.ts +599 -0
- package/backend/containers/parse.ts +905 -0
- package/backend/containers/port-index.test.ts +91 -0
- package/backend/containers/port-index.ts +198 -0
- package/backend/containers/prune-job.test.ts +148 -0
- package/backend/containers/prune-job.ts +129 -0
- package/backend/containers/pty-backend.ts +337 -0
- package/backend/containers/pty.ts +114 -0
- package/backend/containers/runtime.ts +218 -0
- package/backend/containers/scan.ts +293 -0
- package/backend/database/migrations/070_create_worktrees_table.ts +67 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/index.ts +1 -0
- package/backend/database/queries/session-queries.ts +50 -11
- package/backend/database/queries/worktree-queries.ts +148 -0
- package/backend/engine/adapters/opencode/server.ts +389 -83
- package/backend/engine/adapters/opencode/stream.ts +14 -14
- package/backend/engine/hot-reload.ts +25 -10
- package/backend/files/file-watcher.test.ts +105 -0
- package/backend/files/file-watcher.ts +80 -10
- package/backend/git/git-service.test.ts +33 -0
- package/backend/git/git-service.ts +7 -3
- package/backend/git/nested-repos.test.ts +292 -1
- package/backend/git/nested-repos.ts +330 -17
- package/backend/host/metrics.test.ts +110 -0
- package/backend/host/metrics.ts +235 -0
- package/backend/{ports → host}/runner.ts +19 -10
- package/backend/index.ts +16 -4
- package/backend/mcp/internal/servers/browser-automation/context.ts +16 -2
- package/backend/ports/attribute.test.ts +3 -2
- package/backend/ports/attribute.ts +6 -45
- package/backend/ports/kill.ts +1 -1
- package/backend/ports/monitor.ts +1 -1
- package/backend/ports/processes.ts +1 -1
- package/backend/ports/scan-host.ts +77 -8
- package/backend/ports/scan.test.ts +1 -1
- package/backend/ports/scan.ts +1 -1
- package/backend/ports/ssh-lineage.ts +1 -1
- package/backend/preview/browser/browser-pool.ts +81 -0
- package/backend/preview/browser/browser-preview-service.ts +142 -32
- package/backend/preview/browser/browser-tab-manager.ts +350 -113
- package/backend/preview/browser/browser-video-capture.ts +84 -31
- package/backend/preview/browser/types.ts +9 -0
- package/backend/projects/project-cleanup.integration.test.ts +1 -1
- package/backend/projects/project-cleanup.ts +7 -1
- package/backend/projects/shell-ownership.ts +65 -0
- package/backend/snapshot/snapshot-service.ts +6 -2
- package/backend/terminal/ptykit.ts +35 -6
- package/backend/utils/ws.ts +21 -1
- package/backend/worktrees/clone.ts +222 -0
- package/backend/worktrees/index.ts +29 -0
- package/backend/worktrees/merge.test.ts +93 -0
- package/backend/worktrees/merge.ts +232 -0
- package/backend/worktrees/paths.test.ts +60 -0
- package/backend/worktrees/paths.ts +60 -0
- package/backend/worktrees/resolve.ts +67 -0
- package/backend/worktrees/service.ts +315 -0
- package/backend/worktrees/tree.ts +59 -0
- package/backend/ws/access.ts +18 -0
- package/backend/ws/artifacts/generate.ts +2 -2
- package/backend/ws/chat/stream.ts +9 -2
- package/backend/ws/containers/actions.ts +156 -0
- package/backend/ws/containers/index.ts +43 -0
- package/backend/ws/containers/list.ts +146 -0
- package/backend/ws/containers/logs.ts +43 -0
- package/backend/ws/containers/tunnel.ts +101 -0
- package/backend/ws/files/panel-state-scope.test.ts +102 -0
- package/backend/ws/files/path-access.test.ts +93 -1
- package/backend/ws/files/path-access.ts +54 -5
- package/backend/ws/files/read.ts +15 -10
- package/backend/ws/files/search.ts +7 -7
- package/backend/ws/files/state.ts +70 -8
- package/backend/ws/files/watch.ts +16 -14
- package/backend/ws/git/branch-name.ts +3 -3
- package/backend/ws/git/branch.ts +15 -15
- package/backend/ws/git/commit-message.ts +3 -3
- package/backend/ws/git/commit.ts +17 -17
- package/backend/ws/git/conflict.ts +10 -10
- package/backend/ws/git/diff.ts +9 -9
- package/backend/ws/git/ignored.ts +9 -9
- package/backend/ws/git/log.ts +6 -4
- package/backend/ws/git/remote.ts +43 -43
- package/backend/ws/git/staging.ts +16 -16
- package/backend/ws/git/status.ts +47 -37
- package/backend/ws/index.ts +6 -0
- package/backend/ws/message-rate-limiter.test.ts +45 -0
- package/backend/ws/message-rate-limiter.ts +34 -3
- package/backend/ws/preview/access.ts +11 -3
- package/backend/ws/projects/index.ts +5 -1
- package/backend/ws/projects/info.test.ts +54 -0
- package/backend/ws/projects/info.ts +398 -0
- package/backend/ws/sessions/crud.ts +21 -6
- package/backend/ws/snapshot/restore.ts +11 -14
- package/backend/ws/ssh/connections.ts +8 -0
- package/backend/ws/system/device-info.ts +70 -96
- package/backend/ws/worktrees/crud.ts +187 -0
- package/backend/ws/worktrees/index.ts +33 -0
- package/backend/ws/worktrees/transfer.ts +88 -0
- package/dist/assets/{index-vW2RpdHu.js → index-Bg-SUCFg.js} +1 -1
- package/dist/assets/{index-gGTMFUa8.js → index-Cz4YpHVi.js} +1 -1
- package/dist/assets/main-9G7IpQpK.css +1 -0
- package/dist/assets/main-BofFsH8E.js +1085 -0
- package/dist/index.html +2 -2
- package/dist/manifest.json +9 -2
- package/package.json +1 -1
- package/shared/types/containers/container.ts +366 -0
- package/shared/types/containers/index.ts +5 -0
- package/shared/types/database/schema.ts +26 -0
- package/shared/types/host/index.ts +18 -0
- package/shared/types/ports/port.ts +47 -9
- package/shared/utils/logger.ts +3 -0
- package/shared/utils/workspace-scope.test.ts +80 -0
- package/shared/utils/workspace-scope.ts +57 -0
- package/shared/utils/ws-client.ts +92 -17
- package/shared/utils/ws-server.ts +83 -16
- package/dist/assets/main-CYCMEbXB.css +0 -1
- package/dist/assets/main-ZCojSzvW.js +0 -1060
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace scope keys.
|
|
3
|
+
*
|
|
4
|
+
* Terminal sessions and preview tabs are keyed per workspace, not per project:
|
|
5
|
+
* a worktree exists so its work stays separate, and sharing those keys with the
|
|
6
|
+
* main tree would leak one task's shells and tabs into another's.
|
|
7
|
+
*
|
|
8
|
+
* Access is always decided on the project id, so the key stays parseable back
|
|
9
|
+
* to it rather than becoming an opaque identifier of its own.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Separator that cannot appear in a UUID, so parsing is unambiguous. */
|
|
13
|
+
const SCOPE_SEPARATOR = '~';
|
|
14
|
+
|
|
15
|
+
/** Key for a workspace: the project itself, or one of its worktrees. */
|
|
16
|
+
export function makeScopeKey(projectId: string, worktreeId?: string | null): string {
|
|
17
|
+
return worktreeId ? `${projectId}${SCOPE_SEPARATOR}${worktreeId}` : projectId;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function parseScopeKey(scopeKey: string): { projectId: string; worktreeId: string | null } {
|
|
21
|
+
const index = scopeKey.indexOf(SCOPE_SEPARATOR);
|
|
22
|
+
if (index === -1) return { projectId: scopeKey, worktreeId: null };
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
projectId: scopeKey.slice(0, index),
|
|
26
|
+
worktreeId: scopeKey.slice(index + SCOPE_SEPARATOR.length) || null
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The project a scope key belongs to — what every access check runs against. */
|
|
31
|
+
export function scopeProjectId(scopeKey: string): string {
|
|
32
|
+
return parseScopeKey(scopeKey).projectId;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Whether `scopeKey` is any workspace of `projectId` (the project or a worktree). */
|
|
36
|
+
export function isScopeOfProject(scopeKey: string, projectId: string): boolean {
|
|
37
|
+
return parseScopeKey(scopeKey).projectId === projectId;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Short id-safe token that distinguishes one workspace from another.
|
|
42
|
+
*
|
|
43
|
+
* Ids generated per workspace (terminal sessions, preview tabs) are numbered
|
|
44
|
+
* from 1 inside their own scope, so two scopes of the same project mint the
|
|
45
|
+
* same id. Embedding this token is what keeps those numbers from colliding —
|
|
46
|
+
* a collision is not cosmetic: PtyKit refuses a session id held by another
|
|
47
|
+
* namespace, and the preview client matches events on tab id alone.
|
|
48
|
+
*/
|
|
49
|
+
export function scopeSlug(scopeKey: string): string {
|
|
50
|
+
const { projectId, worktreeId } = parseScopeKey(scopeKey);
|
|
51
|
+
const alphanumeric = (value: string) => value.replace(/[^a-zA-Z0-9]/g, '');
|
|
52
|
+
|
|
53
|
+
const head = alphanumeric(projectId).slice(0, 8);
|
|
54
|
+
if (!worktreeId) return head;
|
|
55
|
+
// `w` keeps the token alphanumeric, so it survives id sanitisation.
|
|
56
|
+
return `${head}w${alphanumeric(worktreeId).slice(0, 6)}`;
|
|
57
|
+
}
|
|
@@ -278,13 +278,32 @@ export class WSClient<TAPI extends { client: any; server: any }> {
|
|
|
278
278
|
/** Prevents a heal-reconnect stampede when many requests time out at once. */
|
|
279
279
|
private healingReconnect = false;
|
|
280
280
|
|
|
281
|
+
/**
|
|
282
|
+
* When the socket last delivered ANYTHING from the server.
|
|
283
|
+
*
|
|
284
|
+
* This is the evidence that separates the two failures a stalled request can
|
|
285
|
+
* mean, which the recovery path used to conflate. A socket that is still
|
|
286
|
+
* handing us other traffic is demonstrably alive, so a request that has not
|
|
287
|
+
* come back is slow or was lost in transit — re-sending it is the whole fix.
|
|
288
|
+
* A socket that has gone completely silent is the black-holed case reconnect
|
|
289
|
+
* exists for.
|
|
290
|
+
*
|
|
291
|
+
* Treating both as "the connection is dead" is what turned one slow read into
|
|
292
|
+
* a full teardown, and a teardown re-runs every panel's reconnect handler at
|
|
293
|
+
* once — a burst far more expensive than the request that triggered it, and
|
|
294
|
+
* one that made the next timeout more likely rather than less.
|
|
295
|
+
*/
|
|
296
|
+
private lastInboundAt = 0;
|
|
297
|
+
|
|
281
298
|
/** Current context (synced with server) */
|
|
282
299
|
private context: {
|
|
283
300
|
userId: string | null;
|
|
284
301
|
projectId: string | null;
|
|
302
|
+
worktreeId: string | null;
|
|
285
303
|
} = {
|
|
286
304
|
userId: null,
|
|
287
|
-
projectId: null
|
|
305
|
+
projectId: null,
|
|
306
|
+
worktreeId: null
|
|
288
307
|
};
|
|
289
308
|
|
|
290
309
|
/** Session token for re-authentication on reconnection */
|
|
@@ -376,8 +395,11 @@ export class WSClient<TAPI extends { client: any; server: any }> {
|
|
|
376
395
|
}
|
|
377
396
|
|
|
378
397
|
// A fresh socket is live again — clear the heal guard so future
|
|
379
|
-
// dead-connection recoveries can fire
|
|
398
|
+
// dead-connection recoveries can fire, and count the open itself as
|
|
399
|
+
// proof of life so a quiet-but-healthy connection is never mistaken
|
|
400
|
+
// for a black-holed one on its first request.
|
|
380
401
|
this.healingReconnect = false;
|
|
402
|
+
this.lastInboundAt = Date.now();
|
|
381
403
|
|
|
382
404
|
// Flush queued fire-and-forget messages AFTER context + room
|
|
383
405
|
// re-joins are synced. Drain into a local array first so a send
|
|
@@ -407,6 +429,10 @@ export class WSClient<TAPI extends { client: any; server: any }> {
|
|
|
407
429
|
};
|
|
408
430
|
|
|
409
431
|
this.ws.onmessage = (event) => {
|
|
432
|
+
// Proof of life for the recovery path, recorded before anything can
|
|
433
|
+
// throw: what matters is that a frame ARRIVED, not that we managed
|
|
434
|
+
// to handle it.
|
|
435
|
+
this.lastInboundAt = Date.now();
|
|
410
436
|
try {
|
|
411
437
|
if (event.data instanceof ArrayBuffer) {
|
|
412
438
|
// Binary message
|
|
@@ -751,6 +777,8 @@ export class WSClient<TAPI extends { client: any; server: any }> {
|
|
|
751
777
|
if (this.context.projectId === projectId) return;
|
|
752
778
|
|
|
753
779
|
this.context.projectId = projectId;
|
|
780
|
+
// A worktree belongs to one project, so it never survives the switch.
|
|
781
|
+
this.context.worktreeId = null;
|
|
754
782
|
debug.log('websocket', 'Context: project set to', projectId);
|
|
755
783
|
|
|
756
784
|
if (this.isConnected) {
|
|
@@ -758,10 +786,25 @@ export class WSClient<TAPI extends { client: any; server: any }> {
|
|
|
758
786
|
}
|
|
759
787
|
}
|
|
760
788
|
|
|
789
|
+
/**
|
|
790
|
+
* Set the worktree this client is viewing (null = the main project tree).
|
|
791
|
+
* Server-side scoping of terminals and preview tabs reads this.
|
|
792
|
+
*/
|
|
793
|
+
async setWorktree(worktreeId: string | null): Promise<void> {
|
|
794
|
+
if (this.context.worktreeId === worktreeId) return;
|
|
795
|
+
|
|
796
|
+
this.context.worktreeId = worktreeId;
|
|
797
|
+
debug.log('websocket', 'Context: worktree set to', worktreeId);
|
|
798
|
+
|
|
799
|
+
if (this.isConnected) {
|
|
800
|
+
await this.syncContext();
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
761
804
|
/**
|
|
762
805
|
* Get current context
|
|
763
806
|
*/
|
|
764
|
-
getContext(): { userId: string | null; projectId: string | null } {
|
|
807
|
+
getContext(): { userId: string | null; projectId: string | null; worktreeId: string | null } {
|
|
765
808
|
return { ...this.context };
|
|
766
809
|
}
|
|
767
810
|
|
|
@@ -838,15 +881,16 @@ export class WSClient<TAPI extends { client: any; server: any }> {
|
|
|
838
881
|
// Register response listener
|
|
839
882
|
unsubResponse = this.on('ws:set-context:response' as any, handleResponse);
|
|
840
883
|
|
|
841
|
-
// Send context sync request (
|
|
884
|
+
// Send context sync request (userId is set server-side by auth)
|
|
842
885
|
this.emit('ws:set-context' as any, {
|
|
843
886
|
requestId,
|
|
844
887
|
data: {
|
|
845
|
-
projectId: this.context.projectId
|
|
888
|
+
projectId: this.context.projectId,
|
|
889
|
+
worktreeId: this.context.worktreeId
|
|
846
890
|
}
|
|
847
891
|
} as any);
|
|
848
892
|
|
|
849
|
-
debug.log('websocket', 'Context sync sent: projectId=', this.context.projectId);
|
|
893
|
+
debug.log('websocket', 'Context sync sent: projectId=', this.context.projectId, 'worktreeId=', this.context.worktreeId);
|
|
850
894
|
});
|
|
851
895
|
}
|
|
852
896
|
|
|
@@ -889,7 +933,7 @@ export class WSClient<TAPI extends { client: any; server: any }> {
|
|
|
889
933
|
// Reads can be safely re-sent; mutations can only be re-sent if they were
|
|
890
934
|
// never delivered (tracked via `entry.sent`).
|
|
891
935
|
const idempotent = isIdempotentHttpAction(actionStr);
|
|
892
|
-
// Up to this many automatic
|
|
936
|
+
// Up to this many automatic recovery attempts for idempotent reads.
|
|
893
937
|
const MAX_HEAL_ATTEMPTS = 2;
|
|
894
938
|
|
|
895
939
|
return new Promise((resolve, reject) => {
|
|
@@ -920,23 +964,54 @@ export class WSClient<TAPI extends { client: any; server: any }> {
|
|
|
920
964
|
}
|
|
921
965
|
};
|
|
922
966
|
|
|
923
|
-
// Timeout handler — for idempotent reads,
|
|
924
|
-
//
|
|
967
|
+
// Timeout handler — for idempotent reads, recover and retry instead of
|
|
968
|
+
// failing outright.
|
|
969
|
+
//
|
|
970
|
+
// The recovery is now proportionate to the evidence, which it was not
|
|
971
|
+
// before: ANY stalled read forced a full reconnect, so one slow answer
|
|
972
|
+
// from a busy server tore down a connection that was working perfectly
|
|
973
|
+
// and made all fifteen `onWsReconnect` subscribers refetch at once. On a
|
|
974
|
+
// loaded instance that burst is what caused the NEXT timeout, and the
|
|
975
|
+
// loop sustained itself — panels re-showing their skeletons every few
|
|
976
|
+
// seconds for as long as the instance stayed busy.
|
|
977
|
+
//
|
|
978
|
+
// So the socket is only rebuilt when it has actually stopped speaking.
|
|
979
|
+
// While other traffic is still arriving the connection is known-good and
|
|
980
|
+
// re-sending the one request costs a single frame.
|
|
925
981
|
const onTimeout = () => {
|
|
926
982
|
if (idempotent && entry.attempts < MAX_HEAL_ATTEMPTS) {
|
|
927
983
|
entry.attempts++;
|
|
928
|
-
debug.warn(
|
|
929
|
-
'websocket',
|
|
930
|
-
`Request stalled, healing connection and retrying: ${actionStr} (attempt ${entry.attempts})`
|
|
931
|
-
);
|
|
932
984
|
entry.arm();
|
|
933
|
-
|
|
934
|
-
//
|
|
935
|
-
//
|
|
936
|
-
|
|
985
|
+
|
|
986
|
+
// Silent for a whole timeout window means nothing at all is
|
|
987
|
+
// coming through — a stream, an event, another response — which
|
|
988
|
+
// is the black-hole signature reconnect exists for. Anything
|
|
989
|
+
// less is a connection that works and a request that did not.
|
|
990
|
+
const silentFor = Date.now() - this.lastInboundAt;
|
|
991
|
+
const socketIsBlackHoled = this.isSocketOpen() && silentFor >= timeout;
|
|
992
|
+
|
|
993
|
+
if (socketIsBlackHoled && !this.healingReconnect) {
|
|
994
|
+
debug.warn(
|
|
995
|
+
'websocket',
|
|
996
|
+
`Socket silent for ${silentFor}ms — reconnecting and retrying: ${actionStr} (attempt ${entry.attempts})`
|
|
997
|
+
);
|
|
937
998
|
this.healingReconnect = true;
|
|
999
|
+
// The onopen resend loop re-sends this request on the fresh
|
|
1000
|
+
// socket, so nothing extra is needed here.
|
|
938
1001
|
this.reconnect();
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
debug.warn(
|
|
1006
|
+
'websocket',
|
|
1007
|
+
`Request stalled but connection is live — re-sending: ${actionStr} (attempt ${entry.attempts})`
|
|
1008
|
+
);
|
|
1009
|
+
if (this.isSocketOpen()) {
|
|
1010
|
+
entry.sent = true;
|
|
1011
|
+
this.sendRaw(entry.action, entry.payload);
|
|
939
1012
|
}
|
|
1013
|
+
// A socket that is not open needs no action: the request stays in
|
|
1014
|
+
// `pendingHttpRequests` and the onopen resend loop delivers it.
|
|
940
1015
|
return;
|
|
941
1016
|
}
|
|
942
1017
|
entry.cleanup();
|
|
@@ -281,7 +281,12 @@ export class WSRouter<
|
|
|
281
281
|
|
|
282
282
|
/** Optional auth middleware — called before every route handler */
|
|
283
283
|
private authMiddleware: ((conn: WSConnection, action: string) => Promise<{ allowed: boolean; error?: string }>) | null = null;
|
|
284
|
-
|
|
284
|
+
/**
|
|
285
|
+
* Optional rate limiter. `isRequest` tells it whether a caller is blocked
|
|
286
|
+
* waiting on a reply, so throttling can shed fire-and-forget floods without
|
|
287
|
+
* killing the low-volume calls panels depend on.
|
|
288
|
+
*/
|
|
289
|
+
private rateLimiter: ((conn: WSConnection, action: string, isRequest: boolean) => boolean) | null = null;
|
|
285
290
|
|
|
286
291
|
constructor() {
|
|
287
292
|
this.registerContextHandler();
|
|
@@ -291,7 +296,7 @@ export class WSRouter<
|
|
|
291
296
|
this.authMiddleware = fn;
|
|
292
297
|
}
|
|
293
298
|
|
|
294
|
-
setRateLimiter(fn: (conn: WSConnection, action: string) => boolean): void {
|
|
299
|
+
setRateLimiter(fn: (conn: WSConnection, action: string, isRequest: boolean) => boolean): void {
|
|
295
300
|
this.rateLimiter = fn;
|
|
296
301
|
}
|
|
297
302
|
|
|
@@ -303,11 +308,13 @@ export class WSRouter<
|
|
|
303
308
|
this.httpRoutes.set('ws:set-context', {
|
|
304
309
|
action: 'ws:set-context',
|
|
305
310
|
dataSchema: t.Object({
|
|
306
|
-
projectId: t.Optional(t.Union([t.String(), t.Null()]))
|
|
311
|
+
projectId: t.Optional(t.Union([t.String(), t.Null()])),
|
|
312
|
+
worktreeId: t.Optional(t.Union([t.String(), t.Null()]))
|
|
307
313
|
}),
|
|
308
314
|
responseSchema: t.Object({
|
|
309
315
|
userId: t.Union([t.String(), t.Null()]),
|
|
310
|
-
projectId: t.Union([t.String(), t.Null()])
|
|
316
|
+
projectId: t.Union([t.String(), t.Null()]),
|
|
317
|
+
worktreeId: t.Union([t.String(), t.Null()])
|
|
311
318
|
}),
|
|
312
319
|
handler: async ({ conn, data }) => {
|
|
313
320
|
// Import ws server to update context
|
|
@@ -333,11 +340,25 @@ export class WSRouter<
|
|
|
333
340
|
wsServer.setProject(conn, data.projectId);
|
|
334
341
|
}
|
|
335
342
|
|
|
343
|
+
// Which tree this connection is viewing. Validated against the
|
|
344
|
+
// project so a client cannot point itself at another one's worktree.
|
|
345
|
+
if (data.worktreeId !== undefined) {
|
|
346
|
+
let worktreeId = data.worktreeId;
|
|
347
|
+
if (worktreeId !== null) {
|
|
348
|
+
const { worktreeQueries } = await import('$backend/database/queries');
|
|
349
|
+
const activeProjectId = wsServer.getConnectionState(conn)?.projectId ?? null;
|
|
350
|
+
const worktree = worktreeQueries.getById(worktreeId);
|
|
351
|
+
if (!worktree || worktree.project_id !== activeProjectId) worktreeId = null;
|
|
352
|
+
}
|
|
353
|
+
wsServer.setWorktree(conn, worktreeId);
|
|
354
|
+
}
|
|
355
|
+
|
|
336
356
|
// Read back from connectionState (single source of truth)
|
|
337
357
|
const state = wsServer.getConnectionState(conn);
|
|
338
358
|
return {
|
|
339
359
|
userId: state?.userId ?? null,
|
|
340
|
-
projectId: state?.projectId ?? null
|
|
360
|
+
projectId: state?.projectId ?? null,
|
|
361
|
+
worktreeId: state?.worktreeId ?? null
|
|
341
362
|
};
|
|
342
363
|
}
|
|
343
364
|
});
|
|
@@ -531,16 +552,30 @@ export class WSRouter<
|
|
|
531
552
|
data: validatedData
|
|
532
553
|
});
|
|
533
554
|
|
|
534
|
-
//
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
555
|
+
// The response is NOT re-validated.
|
|
556
|
+
//
|
|
557
|
+
// `route.responseSchema` stays declared because it is what gives
|
|
558
|
+
// `ws.http()` its return type on the client — but checking the value
|
|
559
|
+
// against it at runtime bought nothing and cost the whole server.
|
|
560
|
+
// A response is produced by our own handler, not by a peer: it is not
|
|
561
|
+
// untrusted input, so the only thing validation could catch is a bug
|
|
562
|
+
// we would rather surface as a wrong field than as a route that fails
|
|
563
|
+
// outright for the user.
|
|
564
|
+
//
|
|
565
|
+
// The cost was not theoretical. `Value.Decode` walks the entire value
|
|
566
|
+
// synchronously on the main thread, and the worst offender is exactly
|
|
567
|
+
// the biggest payload: `files:list-tree` declares a RECURSIVE UNION
|
|
568
|
+
// (backend/ws/files/read.ts), so TypeBox tries both branches at every
|
|
569
|
+
// node of the tree. On a large project that locked the event loop for
|
|
570
|
+
// hundreds of milliseconds per load — during which every other panel's
|
|
571
|
+
// request, every other project's, and the preview watchdog all waited.
|
|
572
|
+
// That is what made one slow Files load feel like the whole app hanging.
|
|
573
|
+
//
|
|
574
|
+
// Measured against what it protects: `Value.Decode` returns the very
|
|
575
|
+
// same object reference and strips nothing (there is not one
|
|
576
|
+
// `t.Transform` in the codebase), so removing it cannot change a
|
|
577
|
+
// successful response by even one byte.
|
|
578
|
+
const validatedResponse = result;
|
|
544
579
|
|
|
545
580
|
// Check if response contains binary data
|
|
546
581
|
if (isBinaryAction(responseAction, validatedResponse)) {
|
|
@@ -628,7 +663,39 @@ export class WSRouter<
|
|
|
628
663
|
// ═══ END AUTH GATE ═══
|
|
629
664
|
|
|
630
665
|
// ═══ RATE LIMIT GATE ═══
|
|
631
|
-
|
|
666
|
+
// A request-response route has a caller blocked on its reply; an event
|
|
667
|
+
// does not. The limiter needs that distinction to shed the right half
|
|
668
|
+
// of a flood, and the answer below needs it to know whether anyone is
|
|
669
|
+
// listening for one.
|
|
670
|
+
const isRequest = this.httpRoutes.has(action);
|
|
671
|
+
if (this.rateLimiter && !this.rateLimiter(conn, action, isRequest)) {
|
|
672
|
+
// A dropped request must still be ANSWERED. Returning silently here
|
|
673
|
+
// left `ws.http()` waiting on a reply that was never coming, so the
|
|
674
|
+
// caller hung for its full 30s timeout and then tore the whole
|
|
675
|
+
// socket down to "heal" it — which re-ran every panel's reconnect
|
|
676
|
+
// handler at once, producing the very burst that tripped the limiter
|
|
677
|
+
// again. Telling the client no is what breaks that loop: the request
|
|
678
|
+
// fails immediately, the caller retries on its own terms, and the
|
|
679
|
+
// connection survives.
|
|
680
|
+
//
|
|
681
|
+
// The auth gate directly above has always done this; the two gates
|
|
682
|
+
// only differed because this one was written to `return` on a path
|
|
683
|
+
// where nothing was waiting for an answer. Fire-and-forget events
|
|
684
|
+
// have no requestId and still just drop.
|
|
685
|
+
if (isRequest) {
|
|
686
|
+
try {
|
|
687
|
+
conn.send(JSON.stringify({
|
|
688
|
+
action: `${action}:response`,
|
|
689
|
+
payload: {
|
|
690
|
+
success: false,
|
|
691
|
+
error: 'Rate limit exceeded — request dropped',
|
|
692
|
+
requestId: payload?.requestId
|
|
693
|
+
}
|
|
694
|
+
}));
|
|
695
|
+
} catch {
|
|
696
|
+
// Connection may already be closing.
|
|
697
|
+
}
|
|
698
|
+
}
|
|
632
699
|
return;
|
|
633
700
|
}
|
|
634
701
|
// ═══ END RATE LIMIT GATE ═══
|