@myrialabs/clopen 0.4.41 → 0.4.42
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/chat/stream-manager.ts +8 -0
- package/backend/database/crypto/secret-columns.ts +5 -1
- package/backend/database/index.ts +18 -0
- package/backend/database/migrations/082_create_git_identities.test.ts +186 -0
- package/backend/database/migrations/082_create_git_identities.ts +122 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/git-identity-queries.ts +290 -0
- package/backend/database/queries/index.ts +2 -0
- package/backend/engine/adapters/claude/environment.ts +11 -1
- package/backend/engine/adapters/claude/stream.ts +1 -1
- package/backend/engine/adapters/codex/stream.ts +35 -3
- package/backend/engine/adapters/opencode/server.ts +26 -2
- package/backend/engine/adapters/opencode/stream.ts +4 -1
- package/backend/engine/adapters/qwen/stream.ts +3 -1
- package/backend/engine/types.ts +10 -0
- package/backend/git/git-executor.ts +28 -2
- package/backend/git/git-service.ts +59 -40
- package/backend/git/identity/cli.test.ts +58 -0
- package/backend/git/identity/cli.ts +201 -0
- package/backend/git/identity/credential-helper.test.ts +242 -0
- package/backend/git/identity/env.test.ts +214 -0
- package/backend/git/identity/env.ts +193 -0
- package/backend/git/identity/import-local.test.ts +199 -0
- package/backend/git/identity/import-local.ts +141 -0
- package/backend/git/identity/index.ts +78 -0
- package/backend/git/identity/keys.ts +183 -0
- package/backend/git/identity/resolve.ts +140 -0
- package/backend/git/identity/scope-config.test.ts +145 -0
- package/backend/git/identity/scope-config.ts +175 -0
- package/backend/git/identity/service.ts +297 -0
- package/backend/index.ts +16 -0
- package/backend/preview/browser/browser-console-manager.ts +214 -34
- package/backend/preview/browser/browser-pool.ts +526 -123
- package/backend/preview/browser/browser-preview-service.ts +192 -8
- package/backend/preview/browser/browser-tab-lifecycle.test.ts +240 -0
- package/backend/preview/browser/browser-tab-lifecycle.ts +495 -0
- package/backend/preview/browser/browser-tab-manager.ts +291 -65
- package/backend/preview/browser/capture-profile.ts +15 -1
- package/backend/preview/browser/types.ts +26 -1
- package/backend/terminal/ptykit.ts +24 -0
- package/backend/ws/git/branch.ts +17 -6
- package/backend/ws/git/commit.ts +6 -5
- package/backend/ws/git/conflict.ts +3 -2
- package/backend/ws/git/identity-context.ts +91 -0
- package/backend/ws/git/identity.ts +200 -0
- package/backend/ws/git/index.ts +3 -1
- package/backend/ws/git/remote.ts +57 -11
- package/backend/ws/preview/browser/data.ts +60 -0
- package/backend/ws/preview/browser/interact.ts +6 -0
- package/backend/ws/preview/browser/tab-info.ts +9 -1
- package/backend/ws/preview/browser/tab.ts +64 -8
- package/backend/ws/preview/index.ts +21 -0
- package/bin/clopen.ts +9 -0
- package/dist/assets/{index-CeNKKSDR.js → index-BynqyvF4.js} +1 -1
- package/dist/assets/{index-DW89566K.js → index-DLoJsmNQ.js} +1 -1
- package/dist/assets/{main-Cy5DXWPT.js → main-CUyZAmqt.js} +591 -584
- package/dist/assets/main-DVCW3cfr.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +2 -2
- package/shared/types/git-identity.ts +112 -0
- package/dist/assets/main-DJKj_4By.css +0 -1
|
@@ -8,6 +8,7 @@ import { BrowserVideoCapture } from './browser-video-capture.js';
|
|
|
8
8
|
import { BrowserDialogHandler } from './browser-dialog-handler.js';
|
|
9
9
|
import { BrowserNativeUIHandler } from './browser-native-ui-handler.js';
|
|
10
10
|
import { BrowserHostBridge, type HostResponse } from './browser-host-bridge.js';
|
|
11
|
+
import { BrowserTabLifecycle } from './browser-tab-lifecycle.js';
|
|
11
12
|
import { browserMcpControl } from './browser-mcp-control.js';
|
|
12
13
|
import { browserPool } from './browser-pool.js';
|
|
13
14
|
import { ws } from '$backend/utils/ws';
|
|
@@ -15,6 +16,7 @@ import { getViewportDimensions } from '$shared/constants/preview.js';
|
|
|
15
16
|
import { debug } from '$shared/utils/logger';
|
|
16
17
|
import { scopeProjectId } from '$shared/utils/workspace-scope';
|
|
17
18
|
import type {
|
|
19
|
+
BrowserSiteData,
|
|
18
20
|
BrowserTab,
|
|
19
21
|
BrowserTabInfo,
|
|
20
22
|
BrowserConsoleMessage,
|
|
@@ -40,7 +42,7 @@ import type {
|
|
|
40
42
|
*
|
|
41
43
|
* Architecture:
|
|
42
44
|
* - Tabs are the primary unit (no separate session concept)
|
|
43
|
-
* - Each tab =
|
|
45
|
+
* - Each tab = a page in its workspace's shared browser context
|
|
44
46
|
* - Event-driven communication with frontend
|
|
45
47
|
* - Manages all browser operations: streaming, interaction, console, etc.
|
|
46
48
|
* - **PROJECT ISOLATION**: Each instance is isolated per project
|
|
@@ -54,6 +56,7 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
54
56
|
private dialogHandler: BrowserDialogHandler;
|
|
55
57
|
private nativeUIHandler: BrowserNativeUIHandler;
|
|
56
58
|
private hostBridge: BrowserHostBridge;
|
|
59
|
+
private lifecycle: BrowserTabLifecycle;
|
|
57
60
|
|
|
58
61
|
// Store context menu info for later action execution
|
|
59
62
|
private contextMenus = new Map<string, BrowserContextMenuInfo>();
|
|
@@ -84,7 +87,14 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
84
87
|
|
|
85
88
|
// Initialize managers with projectId for isolation
|
|
86
89
|
this.tabManager = new BrowserTabManager(projectId);
|
|
87
|
-
|
|
90
|
+
// Built before the console manager, which asks it who is watching.
|
|
91
|
+
this.lifecycle = new BrowserTabLifecycle({
|
|
92
|
+
getPage: (tabId) => this.tabManager.peekTab(tabId)?.page ?? null,
|
|
93
|
+
isPinned: (tabId) => this.isTabPinnedAwake(tabId)
|
|
94
|
+
});
|
|
95
|
+
this.consoleManager = new BrowserConsoleManager({
|
|
96
|
+
isWatched: (tabId) => this.lifecycle.hasViewers(tabId)
|
|
97
|
+
});
|
|
88
98
|
this.interactionHandler = new BrowserInteractionHandler();
|
|
89
99
|
this.navigationTracker = new BrowserNavigationTracker();
|
|
90
100
|
this.videoCapture = new BrowserVideoCapture();
|
|
@@ -96,7 +106,48 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
96
106
|
this.setupEventForwarding();
|
|
97
107
|
}
|
|
98
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Tabs that must keep running even though nobody is watching them.
|
|
111
|
+
*
|
|
112
|
+
* An agent's tab, because the run continues whether or not a panel is open
|
|
113
|
+
* on it; and a tab still loading or being rebuilt, because freezing one
|
|
114
|
+
* mid-navigation would strand it half rendered with no event left to wake
|
|
115
|
+
* it.
|
|
116
|
+
*/
|
|
117
|
+
private isTabPinnedAwake(tabId: string): boolean {
|
|
118
|
+
if (browserMcpControl.isTabControlled(tabId, this.projectId)) return true;
|
|
119
|
+
|
|
120
|
+
const tab = this.tabManager.peekTab(tabId);
|
|
121
|
+
if (!tab) return false;
|
|
122
|
+
|
|
123
|
+
return !!tab.isLoading || !!tab.isRecovering;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Wake a tab and wait for it — required before anything runs script in it. */
|
|
127
|
+
async ensureTabAwake(tabId: string): Promise<void> {
|
|
128
|
+
await this.lifecycle.ensureAwake(tabId);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Push back a tab's freeze without waiting for it to wake. */
|
|
132
|
+
noteTabActivity(tabId: string): void {
|
|
133
|
+
this.lifecycle.touch(tabId);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
isTabSleeping(tabId: string): boolean {
|
|
137
|
+
return this.lifecycle.isFrozen(tabId);
|
|
138
|
+
}
|
|
139
|
+
|
|
99
140
|
private setupEventForwarding() {
|
|
141
|
+
// A page put to sleep (or woken) is visible in the tab strip, so the
|
|
142
|
+
// viewer is told the same way it is told about a lock.
|
|
143
|
+
this.lifecycle.on('state', ({ tabId, state }: { tabId: string; state: string }) => {
|
|
144
|
+
this.emit('preview:browser-tab-lifecycle', {
|
|
145
|
+
tabId,
|
|
146
|
+
sleeping: state === 'frozen',
|
|
147
|
+
timestamp: Date.now()
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
100
151
|
// Forward console events
|
|
101
152
|
this.consoleManager.on('console-message', (data) => {
|
|
102
153
|
this.emit('preview:browser-console-message', data);
|
|
@@ -296,7 +347,12 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
296
347
|
* - Desktop/laptop: landscape
|
|
297
348
|
* - Tablet/mobile: portrait
|
|
298
349
|
*/
|
|
299
|
-
async createTab(
|
|
350
|
+
async createTab(
|
|
351
|
+
url?: string,
|
|
352
|
+
deviceSize: DeviceSize = 'laptop',
|
|
353
|
+
rotation?: Rotation,
|
|
354
|
+
launchId?: string
|
|
355
|
+
): Promise<BrowserTab> {
|
|
300
356
|
// Use device-appropriate default rotation if not specified
|
|
301
357
|
const actualRotation = rotation || ((deviceSize === 'desktop' || deviceSize === 'laptop') ? 'landscape' : 'portrait');
|
|
302
358
|
|
|
@@ -312,7 +368,8 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
312
368
|
// Create tab
|
|
313
369
|
const tab = await this.tabManager.createTab(url, deviceSize, actualRotation, {
|
|
314
370
|
setActive: true,
|
|
315
|
-
preNavigationSetup
|
|
371
|
+
preNavigationSetup,
|
|
372
|
+
launchId
|
|
316
373
|
});
|
|
317
374
|
|
|
318
375
|
// Setup console, navigation tracking, and pre-inject streaming scripts in parallel
|
|
@@ -325,6 +382,8 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
325
382
|
// Fire-and-forget: failure here is non-fatal, startStreaming() will retry injection
|
|
326
383
|
this.videoCapture.preInjectScripts(tab.id, tab).catch(() => {});
|
|
327
384
|
|
|
385
|
+
this.lifecycle.register(tab.id);
|
|
386
|
+
|
|
328
387
|
await this.captureHistoryBase(tab.id);
|
|
329
388
|
void this.refreshTabMeta(tab.id);
|
|
330
389
|
|
|
@@ -407,15 +466,24 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
407
466
|
timestamp: Date.now()
|
|
408
467
|
});
|
|
409
468
|
|
|
469
|
+
// The page it was frozen with is gone; the replacement is running.
|
|
470
|
+
this.lifecycle.rebind(tabId);
|
|
471
|
+
|
|
410
472
|
debug.log('preview', `✅ Tab ${tabId} rebuilt at ${tab.url}`);
|
|
411
473
|
|
|
412
474
|
return true;
|
|
413
475
|
}
|
|
414
476
|
|
|
477
|
+
/** Stop a launch the user abandoned before it produced a tab. */
|
|
478
|
+
async cancelLaunch(launchId: string): Promise<void> {
|
|
479
|
+
await this.tabManager.cancelLaunch(launchId);
|
|
480
|
+
}
|
|
481
|
+
|
|
415
482
|
/**
|
|
416
483
|
* Navigate tab to a new URL
|
|
417
484
|
*/
|
|
418
485
|
async navigateTab(tabId: string, url: string): Promise<string> {
|
|
486
|
+
await this.ensureTabAwake(tabId);
|
|
419
487
|
const wasBlank = this.getTab(tabId)?.url === 'about:blank';
|
|
420
488
|
|
|
421
489
|
const actualUrl = await this.tabManager.navigateTab(tabId, url);
|
|
@@ -450,6 +518,7 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
450
518
|
async getHistoryState(tabId: string): Promise<BrowserHistoryState | null> {
|
|
451
519
|
const tab = this.getTab(tabId);
|
|
452
520
|
if (!tab) return null;
|
|
521
|
+
await this.ensureTabAwake(tabId);
|
|
453
522
|
|
|
454
523
|
const history = await this.navigationTracker.getNavigationHistory(tabId, tab.page);
|
|
455
524
|
if (!history) return null;
|
|
@@ -594,6 +663,9 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
594
663
|
// Close the tab (this will cleanup context, page, etc.)
|
|
595
664
|
const result = await this.tabManager.closeTab(tabId);
|
|
596
665
|
|
|
666
|
+
this.lifecycle.dispose(tabId);
|
|
667
|
+
this.consoleManager.forgetSession(tabId);
|
|
668
|
+
|
|
597
669
|
// Emit tab closed event (for MCP control manager and other listeners)
|
|
598
670
|
this.emit('preview:browser-tab-destroyed', { tabId });
|
|
599
671
|
|
|
@@ -610,7 +682,9 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
610
682
|
* act of looking reassign this made each of them move the other's target.
|
|
611
683
|
*/
|
|
612
684
|
switchTab(tabId: string): boolean {
|
|
613
|
-
|
|
685
|
+
const switched = this.tabManager.setActiveTab(tabId);
|
|
686
|
+
if (switched) this.lifecycle.touch(tabId);
|
|
687
|
+
return switched;
|
|
614
688
|
}
|
|
615
689
|
|
|
616
690
|
/**
|
|
@@ -623,6 +697,11 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
623
697
|
noteTabViewed(tabId: string): boolean {
|
|
624
698
|
if (!this.tabManager.getTab(tabId)) return false;
|
|
625
699
|
this.tabManager.markTabActivity(tabId);
|
|
700
|
+
|
|
701
|
+
// Start the thaw here rather than waiting for the stream to ask for it:
|
|
702
|
+
// the viewer has already committed to this tab, and a wake in flight by
|
|
703
|
+
// the time the first frame is requested is a wake nobody waits for.
|
|
704
|
+
this.lifecycle.touch(tabId);
|
|
626
705
|
return true;
|
|
627
706
|
}
|
|
628
707
|
|
|
@@ -651,9 +730,60 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
651
730
|
* Change viewport settings (device size and rotation) for an existing tab
|
|
652
731
|
*/
|
|
653
732
|
async setViewport(tabId: string, deviceSize: DeviceSize, rotation: Rotation): Promise<boolean> {
|
|
733
|
+
await this.ensureTabAwake(tabId);
|
|
654
734
|
return await this.tabManager.setViewport(tabId, deviceSize, rotation);
|
|
655
735
|
}
|
|
656
736
|
|
|
737
|
+
/**
|
|
738
|
+
* Sites this workspace's browser profile holds data for.
|
|
739
|
+
*
|
|
740
|
+
* The cookie jar is the backbone of the list — every login leaves one —
|
|
741
|
+
* but a site can hold storage without a cookie, so the origins currently
|
|
742
|
+
* open are folded in too. Anything the user could recognise as "I am
|
|
743
|
+
* signed into this" is therefore listed, with something to clear.
|
|
744
|
+
*/
|
|
745
|
+
async listBrowsingData(): Promise<BrowserSiteData[]> {
|
|
746
|
+
const sites = new Map<string, BrowserSiteData>();
|
|
747
|
+
|
|
748
|
+
for (const entry of await browserPool.listBrowsingOrigins(this.projectId)) {
|
|
749
|
+
sites.set(entry.domain, {
|
|
750
|
+
domain: entry.domain,
|
|
751
|
+
origin: `${entry.secure ? 'https' : 'http'}://${entry.domain}`,
|
|
752
|
+
cookies: entry.cookies,
|
|
753
|
+
open: false
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
for (const tab of this.tabManager.getAllTabs()) {
|
|
758
|
+
let url: URL;
|
|
759
|
+
try {
|
|
760
|
+
url = new URL(tab.url);
|
|
761
|
+
} catch {
|
|
762
|
+
continue; // about:blank and friends store nothing.
|
|
763
|
+
}
|
|
764
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') continue;
|
|
765
|
+
|
|
766
|
+
const existing = sites.get(url.hostname);
|
|
767
|
+
// The open tab knows the real origin (scheme and port); a cookie
|
|
768
|
+
// only ever knew the domain, so its guess is replaced here.
|
|
769
|
+
sites.set(url.hostname, {
|
|
770
|
+
domain: url.hostname,
|
|
771
|
+
origin: url.origin,
|
|
772
|
+
cookies: existing?.cookies ?? 0,
|
|
773
|
+
open: true
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
return Array.from(sites.values()).sort(
|
|
778
|
+
(a, b) => Number(b.open) - Number(a.open) || b.cookies - a.cookies || a.domain.localeCompare(b.domain)
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/** Forget one site, or everything this workspace's browser has stored. */
|
|
783
|
+
async clearBrowsingData(origin?: string): Promise<boolean> {
|
|
784
|
+
return browserPool.clearBrowsingData(this.projectId, origin);
|
|
785
|
+
}
|
|
786
|
+
|
|
657
787
|
/**
|
|
658
788
|
* Get tab count
|
|
659
789
|
*/
|
|
@@ -665,14 +795,18 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
665
795
|
* Get tab info
|
|
666
796
|
*/
|
|
667
797
|
getTabInfo(tabId: string): BrowserTabInfo | null {
|
|
668
|
-
|
|
798
|
+
const info = this.tabManager.getTabInfo(tabId);
|
|
799
|
+
if (!info) return null;
|
|
800
|
+
return { ...info, isSleeping: this.lifecycle.isFrozen(tabId) };
|
|
669
801
|
}
|
|
670
802
|
|
|
671
803
|
/**
|
|
672
804
|
* Get all tabs info
|
|
673
805
|
*/
|
|
674
806
|
getAllTabsInfo(): BrowserTabInfo[] {
|
|
675
|
-
return this.tabManager
|
|
807
|
+
return this.tabManager
|
|
808
|
+
.getAllTabsInfo()
|
|
809
|
+
.map((info) => ({ ...info, isSleeping: this.lifecycle.isFrozen(info.id) }));
|
|
676
810
|
}
|
|
677
811
|
|
|
678
812
|
/**
|
|
@@ -715,12 +849,22 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
715
849
|
if (!tab) {
|
|
716
850
|
return false;
|
|
717
851
|
}
|
|
718
|
-
|
|
852
|
+
|
|
853
|
+
// Before the capture, not after: the encoder is injected by evaluating
|
|
854
|
+
// in the page, which a frozen one would never get round to running.
|
|
855
|
+
await this.ensureTabAwake(tabId);
|
|
856
|
+
this.lifecycle.attachViewer(tabId, options.viewerId);
|
|
857
|
+
|
|
858
|
+
const started = await this.videoCapture.startStreaming(
|
|
719
859
|
tabId,
|
|
720
860
|
tab,
|
|
721
861
|
() => this.isValidTab(tabId),
|
|
722
862
|
options
|
|
723
863
|
);
|
|
864
|
+
|
|
865
|
+
if (!started) this.lifecycle.detachViewer(tabId, options.viewerId);
|
|
866
|
+
|
|
867
|
+
return started;
|
|
724
868
|
}
|
|
725
869
|
|
|
726
870
|
/**
|
|
@@ -748,6 +892,16 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
748
892
|
if (!tab) {
|
|
749
893
|
return false;
|
|
750
894
|
}
|
|
895
|
+
|
|
896
|
+
// A viewer with the preview off screen is not watching: capture is
|
|
897
|
+
// already suspended for it, so the page has nothing left to run for.
|
|
898
|
+
if (paused) {
|
|
899
|
+
this.lifecycle.setViewerVisible(tabId, viewerId, false);
|
|
900
|
+
} else {
|
|
901
|
+
await this.ensureTabAwake(tabId);
|
|
902
|
+
this.lifecycle.setViewerVisible(tabId, viewerId, true);
|
|
903
|
+
}
|
|
904
|
+
|
|
751
905
|
return await this.videoCapture.setViewerVisibility(tabId, tab, viewerId, !paused);
|
|
752
906
|
}
|
|
753
907
|
|
|
@@ -758,6 +912,9 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
758
912
|
async stopWebCodecsStreaming(tabId: string, viewerId?: string): Promise<void> {
|
|
759
913
|
const tab = this.getTab(tabId);
|
|
760
914
|
|
|
915
|
+
if (viewerId) this.lifecycle.detachViewer(tabId, viewerId);
|
|
916
|
+
else this.lifecycle.dropViewers(tabId);
|
|
917
|
+
|
|
761
918
|
if (viewerId) {
|
|
762
919
|
await this.videoCapture.detachViewer(tabId, tab ?? undefined, viewerId);
|
|
763
920
|
return;
|
|
@@ -938,6 +1095,7 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
938
1095
|
|
|
939
1096
|
markUserInteraction(tabId: string): void {
|
|
940
1097
|
this.tabManager.markTabActivity(tabId);
|
|
1098
|
+
this.lifecycle.touch(tabId);
|
|
941
1099
|
}
|
|
942
1100
|
|
|
943
1101
|
// Public method to mark tab activity (called from WS handlers)
|
|
@@ -974,6 +1132,7 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
974
1132
|
async executeConsoleCommand(tabId: string, command: string): Promise<any> {
|
|
975
1133
|
const tab = this.getTab(tabId);
|
|
976
1134
|
if (!tab) throw new Error('Tab not found or invalid');
|
|
1135
|
+
await this.ensureTabAwake(tabId);
|
|
977
1136
|
return this.consoleManager.executeConsoleCommand(tab, command);
|
|
978
1137
|
}
|
|
979
1138
|
|
|
@@ -1000,6 +1159,10 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
1000
1159
|
const tab = this.getTab(tabId);
|
|
1001
1160
|
if (!tab) throw new Error('Tab not found or invalid');
|
|
1002
1161
|
|
|
1162
|
+
// An agent drives tabs nobody is looking at, which is exactly the state
|
|
1163
|
+
// this wakes them out of.
|
|
1164
|
+
await this.ensureTabAwake(tabId);
|
|
1165
|
+
|
|
1003
1166
|
return this.interactionHandler.performAutonomousActions(
|
|
1004
1167
|
tabId,
|
|
1005
1168
|
tab,
|
|
@@ -1136,6 +1299,10 @@ export class BrowserPreviewService extends EventEmitter {
|
|
|
1136
1299
|
}
|
|
1137
1300
|
|
|
1138
1301
|
async forceCleanupAll() {
|
|
1302
|
+
// Before the tabs go: a frozen page with its lifecycle listener still
|
|
1303
|
+
// attached would emit a wake nobody is left to hear.
|
|
1304
|
+
await this.lifecycle.cleanup();
|
|
1305
|
+
|
|
1139
1306
|
// First try normal cleanup
|
|
1140
1307
|
await this.cleanup();
|
|
1141
1308
|
|
|
@@ -1264,6 +1431,13 @@ class BrowserPreviewServiceManager {
|
|
|
1264
1431
|
ws.emit.project(roomId, 'preview:browser-tab-switched', { ...data, projectId });
|
|
1265
1432
|
});
|
|
1266
1433
|
|
|
1434
|
+
// A page frozen because nobody was watching, or woken because someone
|
|
1435
|
+
// is. Carries the scope like every other tab event so a client that has
|
|
1436
|
+
// switched workspace drops it.
|
|
1437
|
+
service.on('preview:browser-tab-lifecycle', (data) => {
|
|
1438
|
+
ws.emit.project(roomId, 'preview:browser-tab-lifecycle', { ...data, projectId });
|
|
1439
|
+
});
|
|
1440
|
+
|
|
1267
1441
|
service.on('preview:browser-tab-navigated', (data) => {
|
|
1268
1442
|
ws.emit.project(roomId, 'preview:browser-tab-navigated', { ...data, projectId });
|
|
1269
1443
|
});
|
|
@@ -1394,6 +1568,12 @@ class BrowserPreviewServiceManager {
|
|
|
1394
1568
|
browserMcpControl.on('control-start', (data) => {
|
|
1395
1569
|
debug.log('preview', '🚀 Forwarding mcp-control-start:', data);
|
|
1396
1570
|
if (!data.projectId) return;
|
|
1571
|
+
|
|
1572
|
+
// The tab is now pinned awake; if it was asleep, start the thaw
|
|
1573
|
+
// before the agent's first action asks for it.
|
|
1574
|
+
if (this.services.get(data.projectId)?.isTabSleeping(data.browserTabId)) {
|
|
1575
|
+
void this.services.get(data.projectId)?.ensureTabAwake(data.browserTabId);
|
|
1576
|
+
}
|
|
1397
1577
|
ws.emit.project(data.projectId, 'preview:browser-mcp-control-start', {
|
|
1398
1578
|
browserTabId: data.browserTabId,
|
|
1399
1579
|
chatSessionId: data.chatSessionId,
|
|
@@ -1405,6 +1585,10 @@ class BrowserPreviewServiceManager {
|
|
|
1405
1585
|
browserMcpControl.on('control-end', (data) => {
|
|
1406
1586
|
debug.log('preview', '🚀 Forwarding mcp-control-end:', data);
|
|
1407
1587
|
if (!data.projectId) return;
|
|
1588
|
+
|
|
1589
|
+
// No longer pinned: an unwatched tab the agent has let go of is free
|
|
1590
|
+
// to fall asleep on the usual timer.
|
|
1591
|
+
this.services.get(data.projectId)?.noteTabActivity(data.browserTabId);
|
|
1408
1592
|
ws.emit.project(data.projectId, 'preview:browser-mcp-control-end', {
|
|
1409
1593
|
browserTabId: data.browserTabId,
|
|
1410
1594
|
projectId: data.projectId,
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What may run, and what may not.
|
|
3
|
+
*
|
|
4
|
+
* The rules under test are the ones whose failure is invisible until it is
|
|
5
|
+
* expensive: a tab nobody is watching that keeps a renderer busy, or — far
|
|
6
|
+
* worse — a tab someone *is* watching that gets frozen underneath them.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// Read before the profile is first asked for, so the budget under test is the
|
|
10
|
+
// low-spec one rather than whatever machine happens to run the suite.
|
|
11
|
+
process.env.CLOPEN_PREVIEW_QUALITY = 'low';
|
|
12
|
+
|
|
13
|
+
import { afterEach, describe, expect, test } from 'bun:test';
|
|
14
|
+
import type { Page } from 'puppeteer';
|
|
15
|
+
import { BrowserTabLifecycle } from './browser-tab-lifecycle';
|
|
16
|
+
|
|
17
|
+
const GRACE_MS = 20;
|
|
18
|
+
|
|
19
|
+
/** Records every lifecycle command the page was sent. */
|
|
20
|
+
interface FakePage {
|
|
21
|
+
page: Page;
|
|
22
|
+
states: string[];
|
|
23
|
+
closed: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function fakePage(options: { failFreeze?: boolean } = {}): FakePage {
|
|
27
|
+
const record: FakePage = { page: null as unknown as Page, states: [], closed: false };
|
|
28
|
+
|
|
29
|
+
const cdp = {
|
|
30
|
+
send: async (method: string, params?: { state?: string }) => {
|
|
31
|
+
if (method !== 'Page.setWebLifecycleState') return {};
|
|
32
|
+
const state = params?.state ?? '';
|
|
33
|
+
if (options.failFreeze && state === 'frozen') {
|
|
34
|
+
throw new Error('cannot freeze');
|
|
35
|
+
}
|
|
36
|
+
record.states.push(state);
|
|
37
|
+
return {};
|
|
38
|
+
},
|
|
39
|
+
detach: async () => {}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
record.page = {
|
|
43
|
+
isClosed: () => record.closed,
|
|
44
|
+
createCDPSession: async () => cdp
|
|
45
|
+
} as unknown as Page;
|
|
46
|
+
|
|
47
|
+
return record;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function settle(ms = GRACE_MS * 3): Promise<void> {
|
|
51
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Lifecycles built by a test, torn down after it.
|
|
56
|
+
*
|
|
57
|
+
* Their timers outlive the test that made them, and a stray freeze landing
|
|
58
|
+
* during the next one is how a suite starts reporting on the wrong tabs.
|
|
59
|
+
*/
|
|
60
|
+
const built: BrowserTabLifecycle[] = [];
|
|
61
|
+
|
|
62
|
+
afterEach(async () => {
|
|
63
|
+
await Promise.all(built.splice(0).map((lifecycle) => lifecycle.cleanup()));
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
function lifecycleOver(
|
|
67
|
+
pages: Record<string, FakePage>,
|
|
68
|
+
pinned: Set<string> = new Set()
|
|
69
|
+
): BrowserTabLifecycle {
|
|
70
|
+
const lifecycle = new BrowserTabLifecycle({
|
|
71
|
+
getPage: (tabId) => pages[tabId]?.page ?? null,
|
|
72
|
+
isPinned: (tabId) => pinned.has(tabId),
|
|
73
|
+
freezeAfterMs: GRACE_MS
|
|
74
|
+
});
|
|
75
|
+
built.push(lifecycle);
|
|
76
|
+
return lifecycle;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
describe('a preview tab nobody is watching', () => {
|
|
80
|
+
test('is frozen once the grace has passed', async () => {
|
|
81
|
+
const pages = { a: fakePage() };
|
|
82
|
+
const lifecycle = lifecycleOver(pages);
|
|
83
|
+
|
|
84
|
+
lifecycle.register('a');
|
|
85
|
+
expect(pages.a.states).toEqual([]);
|
|
86
|
+
|
|
87
|
+
await settle();
|
|
88
|
+
|
|
89
|
+
expect(pages.a.states).toEqual(['frozen']);
|
|
90
|
+
expect(lifecycle.isFrozen('a')).toBe(true);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('wakes on demand, and says so', async () => {
|
|
94
|
+
const pages = { a: fakePage() };
|
|
95
|
+
const lifecycle = lifecycleOver(pages);
|
|
96
|
+
const announced: boolean[] = [];
|
|
97
|
+
lifecycle.on('state', ({ state }: { state: string }) => announced.push(state === 'frozen'));
|
|
98
|
+
|
|
99
|
+
lifecycle.register('a');
|
|
100
|
+
await settle();
|
|
101
|
+
await lifecycle.ensureAwake('a');
|
|
102
|
+
|
|
103
|
+
expect(pages.a.states).toEqual(['frozen', 'active']);
|
|
104
|
+
expect(lifecycle.isFrozen('a')).toBe(false);
|
|
105
|
+
expect(announced).toEqual([true, false]);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('stops being asked once the page has refused enough times', async () => {
|
|
109
|
+
const pages = { a: fakePage({ failFreeze: true }) };
|
|
110
|
+
const lifecycle = lifecycleOver(pages);
|
|
111
|
+
|
|
112
|
+
lifecycle.register('a');
|
|
113
|
+
await settle(GRACE_MS * 12);
|
|
114
|
+
|
|
115
|
+
// Three attempts, all refused, and then left alone — never reported as
|
|
116
|
+
// asleep, because it is still running.
|
|
117
|
+
expect(pages.a.states).toEqual([]);
|
|
118
|
+
expect(lifecycle.isFrozen('a')).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('a preview tab someone is using', () => {
|
|
123
|
+
test('is never frozen while a viewer is attached', async () => {
|
|
124
|
+
const pages = { a: fakePage() };
|
|
125
|
+
const lifecycle = lifecycleOver(pages);
|
|
126
|
+
|
|
127
|
+
lifecycle.register('a');
|
|
128
|
+
lifecycle.attachViewer('a', 'viewer-1');
|
|
129
|
+
await settle();
|
|
130
|
+
|
|
131
|
+
expect(pages.a.states).toEqual([]);
|
|
132
|
+
expect(lifecycle.hasViewers('a')).toBe(true);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('freezes only after the last viewer leaves', async () => {
|
|
136
|
+
const pages = { a: fakePage() };
|
|
137
|
+
const lifecycle = lifecycleOver(pages);
|
|
138
|
+
|
|
139
|
+
lifecycle.register('a');
|
|
140
|
+
lifecycle.attachViewer('a', 'laptop');
|
|
141
|
+
lifecycle.attachViewer('a', 'phone');
|
|
142
|
+
lifecycle.detachViewer('a', 'laptop');
|
|
143
|
+
await settle();
|
|
144
|
+
|
|
145
|
+
expect(pages.a.states).toEqual([]);
|
|
146
|
+
|
|
147
|
+
lifecycle.detachViewer('a', 'phone');
|
|
148
|
+
await settle();
|
|
149
|
+
|
|
150
|
+
expect(pages.a.states).toEqual(['frozen']);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('is never frozen while an agent holds it', async () => {
|
|
154
|
+
const pages = { a: fakePage() };
|
|
155
|
+
const lifecycle = lifecycleOver(pages, new Set(['a']));
|
|
156
|
+
|
|
157
|
+
lifecycle.register('a');
|
|
158
|
+
await settle();
|
|
159
|
+
|
|
160
|
+
expect(pages.a.states).toEqual([]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('counts a viewer with the preview off screen as gone', async () => {
|
|
164
|
+
const pages = { a: fakePage() };
|
|
165
|
+
const lifecycle = lifecycleOver(pages);
|
|
166
|
+
|
|
167
|
+
lifecycle.register('a');
|
|
168
|
+
lifecycle.attachViewer('a', 'viewer-1');
|
|
169
|
+
lifecycle.setViewerVisible('a', 'viewer-1', false);
|
|
170
|
+
await settle();
|
|
171
|
+
|
|
172
|
+
expect(pages.a.states).toEqual(['frozen']);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe('the host budget', () => {
|
|
177
|
+
test('reclaims idle tabs beyond it, oldest first', async () => {
|
|
178
|
+
const pages = { a: fakePage(), b: fakePage(), c: fakePage() };
|
|
179
|
+
const lifecycle = lifecycleOver(pages);
|
|
180
|
+
|
|
181
|
+
// Registered in order, so `a` is the least recently awake of the three.
|
|
182
|
+
lifecycle.register('a');
|
|
183
|
+
lifecycle.register('b');
|
|
184
|
+
lifecycle.register('c');
|
|
185
|
+
|
|
186
|
+
// A viewer arriving is what makes the budget count the room. The low
|
|
187
|
+
// tier allows two awake pages; `a` is the one that has to go.
|
|
188
|
+
lifecycle.attachViewer('c', 'viewer-1');
|
|
189
|
+
await settle();
|
|
190
|
+
|
|
191
|
+
expect(pages.a.states).toEqual(['frozen']);
|
|
192
|
+
expect(pages.c.states).toEqual([]);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('never counts a watched tab out of existence', async () => {
|
|
196
|
+
const pages = { a: fakePage(), b: fakePage(), c: fakePage() };
|
|
197
|
+
const lifecycle = lifecycleOver(pages);
|
|
198
|
+
|
|
199
|
+
lifecycle.register('a');
|
|
200
|
+
lifecycle.register('b');
|
|
201
|
+
lifecycle.register('c');
|
|
202
|
+
lifecycle.attachViewer('a', 'one');
|
|
203
|
+
lifecycle.attachViewer('b', 'two');
|
|
204
|
+
lifecycle.attachViewer('c', 'three');
|
|
205
|
+
|
|
206
|
+
await settle();
|
|
207
|
+
|
|
208
|
+
// Three watched tabs on a two-tab budget: every one of them is still
|
|
209
|
+
// running, because freezing one would blank a preview on screen.
|
|
210
|
+
expect(pages.a.states).toEqual([]);
|
|
211
|
+
expect(pages.b.states).toEqual([]);
|
|
212
|
+
expect(pages.c.states).toEqual([]);
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe('a tab whose page went away', () => {
|
|
217
|
+
test('is treated as awake rather than frozen', async () => {
|
|
218
|
+
const pages = { a: fakePage() };
|
|
219
|
+
const lifecycle = lifecycleOver(pages);
|
|
220
|
+
|
|
221
|
+
lifecycle.register('a');
|
|
222
|
+
pages.a.closed = true;
|
|
223
|
+
await settle();
|
|
224
|
+
|
|
225
|
+
expect(pages.a.states).toEqual([]);
|
|
226
|
+
expect(lifecycle.isFrozen('a')).toBe(false);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test('starts over awake once its page is rebuilt', async () => {
|
|
230
|
+
const pages = { a: fakePage() };
|
|
231
|
+
const lifecycle = lifecycleOver(pages);
|
|
232
|
+
|
|
233
|
+
lifecycle.register('a');
|
|
234
|
+
await settle();
|
|
235
|
+
expect(lifecycle.isFrozen('a')).toBe(true);
|
|
236
|
+
|
|
237
|
+
lifecycle.rebind('a');
|
|
238
|
+
expect(lifecycle.isFrozen('a')).toBe(false);
|
|
239
|
+
});
|
|
240
|
+
});
|