@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
|
@@ -162,18 +162,23 @@ interface VideoStreamSession {
|
|
|
162
162
|
scriptInjected: boolean; // Track if persistent script was injected
|
|
163
163
|
scriptsPreInjected: boolean; // Track if scripts were pre-injected during tab creation
|
|
164
164
|
/**
|
|
165
|
-
* When the page last reported frames
|
|
166
|
-
*
|
|
167
|
-
*
|
|
165
|
+
* When the page last reported ANYTHING, frames or not.
|
|
166
|
+
*
|
|
167
|
+
* This is the liveness signal, and the distinction it draws is the whole
|
|
168
|
+
* point: the page ticks a stats report every two seconds for as long as its
|
|
169
|
+
* encoder loop is alive, whether or not there was anything to encode. Reports
|
|
170
|
+
* arriving with zero frames mean a still picture. Reports stopping mean the
|
|
171
|
+
* loop itself is gone — a navigation that took the injected script with it, a
|
|
172
|
+
* context that was torn down — which is the only thing a repair can fix.
|
|
168
173
|
*/
|
|
169
|
-
|
|
174
|
+
lastPageReportAt: number;
|
|
170
175
|
/** Guards the watchdog against re-entering while a repair is in flight. */
|
|
171
176
|
repairing: boolean;
|
|
172
177
|
/**
|
|
173
|
-
* Repairs attempted since the
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
178
|
+
* Repairs attempted since the page last reported in. A repair that did not
|
|
179
|
+
* restore reporting must escalate rather than be repeated: the cheap one asks
|
|
180
|
+
* the page what it thinks, and a page whose own answer is the wrong one will
|
|
181
|
+
* give the same answer every time.
|
|
177
182
|
*/
|
|
178
183
|
stallRounds: number;
|
|
179
184
|
/** Codecs every viewer can decode — one encoder serves them all. */
|
|
@@ -588,11 +593,18 @@ export class BrowserVideoCapture extends EventEmitter {
|
|
|
588
593
|
private static readonly VIEWER_HANDSHAKE_TTL_MS = 45_000;
|
|
589
594
|
|
|
590
595
|
/**
|
|
591
|
-
* Silence tolerated from
|
|
592
|
-
*
|
|
593
|
-
*
|
|
596
|
+
* Silence tolerated from the PAGE before the watchdog repairs the stream,
|
|
597
|
+
* and the interval the watchdog itself runs on.
|
|
598
|
+
*
|
|
599
|
+
* The page reports encoder stats every two seconds for as long as its capture
|
|
600
|
+
* loop is alive, so this is three missed reports: late enough that a slow tick
|
|
601
|
+
* or a busy host is never mistaken for a dead page, early enough that a
|
|
602
|
+
* genuinely broken stream is repaired before anyone reaches for reload.
|
|
603
|
+
*
|
|
604
|
+
* This deliberately measures REPORTS, not frames — a still page produces no
|
|
605
|
+
* frames at all and is perfectly healthy. See `checkForStall`.
|
|
594
606
|
*/
|
|
595
|
-
private static readonly
|
|
607
|
+
private static readonly PAGE_SILENCE_MS = 6_000;
|
|
596
608
|
|
|
597
609
|
/**
|
|
598
610
|
* Cheap stall repairs before the watchdog stops taking the page's word for
|
|
@@ -740,7 +752,7 @@ export class BrowserVideoCapture extends EventEmitter {
|
|
|
740
752
|
viewers: new Map(),
|
|
741
753
|
scriptInjected: false,
|
|
742
754
|
scriptsPreInjected: false,
|
|
743
|
-
|
|
755
|
+
lastPageReportAt: 0,
|
|
744
756
|
repairing: false,
|
|
745
757
|
stallRounds: 0,
|
|
746
758
|
codecSupport: { ...DEFAULT_CODEC_SUPPORT },
|
|
@@ -856,7 +868,10 @@ export class BrowserVideoCapture extends EventEmitter {
|
|
|
856
868
|
// for the first mouse move to see anything. Push one refresh
|
|
857
869
|
// frame as soon as the peer is up.
|
|
858
870
|
if (state === 'connected' && videoSession.tab) {
|
|
859
|
-
|
|
871
|
+
// A peer that just reached `connected` is a page that was talking to
|
|
872
|
+
// us a moment ago — give it a fresh silence window rather than
|
|
873
|
+
// judging it on a clock that predates the handshake.
|
|
874
|
+
videoSession.lastPageReportAt = Date.now();
|
|
860
875
|
void this.requestKeyframe(sessionId, videoSession.tab);
|
|
861
876
|
}
|
|
862
877
|
|
|
@@ -1045,7 +1060,8 @@ export class BrowserVideoCapture extends EventEmitter {
|
|
|
1045
1060
|
this.reconcileViewers(videoSession, health);
|
|
1046
1061
|
}
|
|
1047
1062
|
|
|
1048
|
-
|
|
1063
|
+
// `ensureLive` just heard from the page directly, so this IS a report.
|
|
1064
|
+
videoSession.lastPageReportAt = Date.now();
|
|
1049
1065
|
this.ensureWatchdog();
|
|
1050
1066
|
return true;
|
|
1051
1067
|
}
|
|
@@ -1561,7 +1577,7 @@ export class BrowserVideoCapture extends EventEmitter {
|
|
|
1561
1577
|
if (this.reapStaleViewers(videoSession)) continue;
|
|
1562
1578
|
this.checkForStall(videoSession);
|
|
1563
1579
|
}
|
|
1564
|
-
}, BrowserVideoCapture.
|
|
1580
|
+
}, BrowserVideoCapture.PAGE_SILENCE_MS);
|
|
1565
1581
|
|
|
1566
1582
|
// Nothing here should hold the process open on its own.
|
|
1567
1583
|
this.watchdogTimer.unref?.();
|
|
@@ -1606,36 +1622,71 @@ export class BrowserVideoCapture extends EventEmitter {
|
|
|
1606
1622
|
}
|
|
1607
1623
|
|
|
1608
1624
|
/**
|
|
1609
|
-
* Repair a stream
|
|
1625
|
+
* Repair a stream whose PAGE has stopped reporting while someone is watching.
|
|
1610
1626
|
*
|
|
1611
1627
|
* Recovery used to live entirely in the viewer: it noticed no first frame,
|
|
1612
1628
|
* asked for a screencast refresh, then re-handshook. That only works while
|
|
1613
1629
|
* the *client's* view of the problem is right — and the failures that stuck
|
|
1614
1630
|
* were the ones where the client did everything correctly and the source
|
|
1615
|
-
* was the broken half. Repairing from here closes that gap
|
|
1616
|
-
*
|
|
1631
|
+
* was the broken half. Repairing from here closes that gap.
|
|
1632
|
+
*
|
|
1633
|
+
* WHAT COUNTS AS A STALL, AND WHY IT CHANGED
|
|
1634
|
+
*
|
|
1635
|
+
* This used to key on `lastEncodedAt` — frames reaching the encoder — on the
|
|
1636
|
+
* stated assumption that "even an idle page still tops off". That assumption
|
|
1637
|
+
* was wrong, and the bug it caused was severe.
|
|
1638
|
+
*
|
|
1639
|
+
* Capture is damage-driven: `framesAttempted` is only incremented for motion
|
|
1640
|
+
* frames (see `shouldSkipMotionFrame` in scripts/video-stream.ts), and the
|
|
1641
|
+
* still-page top-off is a one-shot that fires after motion stops, not a
|
|
1642
|
+
* heartbeat. So a page that is simply SITTING THERE — the normal case, a user
|
|
1643
|
+
* looking at their app without touching it — reports zero frames forever, and
|
|
1644
|
+
* every one of those reports was read as "the stream is dead".
|
|
1645
|
+
*
|
|
1646
|
+
* The result was a permanent loop on every idle preview: six seconds of
|
|
1647
|
+
* stillness triggered a repair, twelve seconds triggered a FORCED peer
|
|
1648
|
+
* rebuild, which drops every viewer into a re-handshake — the reload with a
|
|
1649
|
+
* progress bar that users saw arrive every few seconds and could not explain.
|
|
1650
|
+
* The rebuild produced a keyframe, which reset the counters, which started
|
|
1651
|
+
* the same clock again. It also made the failure self-sustaining across the
|
|
1652
|
+
* rest of the app: each rebuild is a burst of CDP work plus handshake round
|
|
1653
|
+
* trips on the shared socket, so the busier the instance, the more of them
|
|
1654
|
+
* were in flight at once.
|
|
1655
|
+
*
|
|
1656
|
+
* The honest signal is whether the PAGE is still talking to us, not whether
|
|
1657
|
+
* it has anything to show. Its stats timer ticks every two seconds for as
|
|
1658
|
+
* long as the encoder loop is alive, so silence there — and only there —
|
|
1659
|
+
* means the loop is genuinely gone. A still picture is not a stall.
|
|
1660
|
+
*
|
|
1661
|
+
* The other real failure, an in-page capture that ends on its own, is already
|
|
1662
|
+
* detected explicitly in `applyEncoderStats` rather than inferred from frame
|
|
1663
|
+
* absence, so nothing is lost by no longer guessing here.
|
|
1617
1664
|
*/
|
|
1618
1665
|
private checkForStall(videoSession: VideoStreamSession): void {
|
|
1619
1666
|
if (videoSession.paused || videoSession.repairing || !videoSession.tab) return;
|
|
1620
1667
|
if (videoSession.viewers.size === 0) return;
|
|
1621
1668
|
if (!Array.from(videoSession.viewers.values()).some((viewer) => viewer.connected)) return;
|
|
1622
1669
|
|
|
1623
|
-
|
|
1624
|
-
|
|
1670
|
+
// Nothing reported yet: the stream is still coming up and its own
|
|
1671
|
+
// handshake path owns that window.
|
|
1672
|
+
if (videoSession.lastPageReportAt === 0) return;
|
|
1673
|
+
|
|
1674
|
+
const silentFor = Date.now() - videoSession.lastPageReportAt;
|
|
1675
|
+
if (silentFor < BrowserVideoCapture.PAGE_SILENCE_MS) return;
|
|
1625
1676
|
|
|
1626
1677
|
const { sessionId, tab } = videoSession;
|
|
1627
1678
|
videoSession.stallRounds++;
|
|
1628
1679
|
|
|
1629
1680
|
// The first round asks the page what state it is in and repairs from
|
|
1630
1681
|
// that answer, which is cheap and keeps every viewer's peer intact. Once
|
|
1631
|
-
// that has been tried and the
|
|
1682
|
+
// that has been tried and the page is still silent, the page's own
|
|
1632
1683
|
// account is the thing that cannot be trusted — so rebuild the peer
|
|
1633
1684
|
// outright. Viewers lose their connection and re-handshake, which is a
|
|
1634
1685
|
// visible hiccup, but they are looking at a frozen picture either way.
|
|
1635
1686
|
const force = videoSession.stallRounds > BrowserVideoCapture.STALL_REPAIR_ROUNDS;
|
|
1636
1687
|
debug.warn(
|
|
1637
1688
|
'webcodecs',
|
|
1638
|
-
`
|
|
1689
|
+
`Page for ${sessionId} stopped reporting for ${silentFor}ms — ` +
|
|
1639
1690
|
`${force ? 'rebuilding the page peer' : 'repairing'} (round ${videoSession.stallRounds})`
|
|
1640
1691
|
);
|
|
1641
1692
|
|
|
@@ -1647,7 +1698,10 @@ export class BrowserVideoCapture extends EventEmitter {
|
|
|
1647
1698
|
.catch((error) => debug.warn('webcodecs', `Stall repair failed for ${sessionId}:`, error))
|
|
1648
1699
|
.finally(() => {
|
|
1649
1700
|
videoSession.repairing = false;
|
|
1650
|
-
|
|
1701
|
+
// Give the repaired page a full silence window to report in before
|
|
1702
|
+
// it can be judged again. Without this the next tick would measure
|
|
1703
|
+
// against a clock that never moved and escalate immediately.
|
|
1704
|
+
videoSession.lastPageReportAt = Date.now();
|
|
1651
1705
|
});
|
|
1652
1706
|
}
|
|
1653
1707
|
|
|
@@ -1661,13 +1715,12 @@ export class BrowserVideoCapture extends EventEmitter {
|
|
|
1661
1715
|
|
|
1662
1716
|
videoSession.captureMode = stats.captureMode || videoSession.captureMode;
|
|
1663
1717
|
|
|
1664
|
-
//
|
|
1665
|
-
//
|
|
1666
|
-
//
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
}
|
|
1718
|
+
// THE report arriving is proof the page's capture loop is alive — whether
|
|
1719
|
+
// or not it had anything to encode. This is what the watchdog measures.
|
|
1720
|
+
// Frames are a property of the CONTENT (a still page has none and is
|
|
1721
|
+
// perfectly healthy); reports are a property of the PIPELINE.
|
|
1722
|
+
videoSession.lastPageReportAt = Date.now();
|
|
1723
|
+
videoSession.stallRounds = 0;
|
|
1671
1724
|
|
|
1672
1725
|
// In-page capture can end on its own — the captured surface goes away
|
|
1673
1726
|
// on some navigations — and the page has no way to start a screencast.
|
|
@@ -123,6 +123,15 @@ export interface BrowserTab {
|
|
|
123
123
|
|
|
124
124
|
// Internal
|
|
125
125
|
isDestroyed?: boolean;
|
|
126
|
+
/**
|
|
127
|
+
* Whether the tab's page is being rebuilt right now.
|
|
128
|
+
*
|
|
129
|
+
* A crashed renderer or a Chrome that went away leaves the tab itself
|
|
130
|
+
* intact — it keeps its id, its slot in the strip and its URL — while
|
|
131
|
+
* `browser`/`context`/`page` are replaced underneath. Anything that would
|
|
132
|
+
* otherwise treat the dead page as a reason to reap the tab checks this.
|
|
133
|
+
*/
|
|
134
|
+
isRecovering?: boolean;
|
|
126
135
|
}
|
|
127
136
|
|
|
128
137
|
/**
|
|
@@ -3,7 +3,7 @@ import { projectCleanupRegistry, runProjectCleanups } from './project-cleanup-re
|
|
|
3
3
|
|
|
4
4
|
import './project-cleanup';
|
|
5
5
|
|
|
6
|
-
const DEFAULT_CLEANUP_HANDLERS = ['engine', 'mcp-context', 'file-watcher', 'presence'] as const;
|
|
6
|
+
const DEFAULT_CLEANUP_HANDLERS = ['engine', 'mcp-context', 'file-watcher', 'presence', 'worktrees'] as const;
|
|
7
7
|
|
|
8
8
|
describe('project cleanup integration', () => {
|
|
9
9
|
test('registers default backend handlers when project-cleanup is loaded', () => {
|
|
@@ -6,6 +6,7 @@ import { projectContextService } from '../mcp';
|
|
|
6
6
|
import { fileWatcher } from '../files/file-watcher';
|
|
7
7
|
import { clearProjectPresence } from '../project/status-manager';
|
|
8
8
|
import { registerProjectCleanup } from './project-cleanup-registry';
|
|
9
|
+
import { removeProjectWorktrees } from '../worktrees';
|
|
9
10
|
|
|
10
11
|
registerProjectCleanup({
|
|
11
12
|
name: 'engine',
|
|
@@ -20,7 +21,7 @@ registerProjectCleanup({
|
|
|
20
21
|
registerProjectCleanup({
|
|
21
22
|
name: 'file-watcher',
|
|
22
23
|
run: (projectId) => {
|
|
23
|
-
fileWatcher.
|
|
24
|
+
fileWatcher.releaseProjectScopes(projectId);
|
|
24
25
|
}
|
|
25
26
|
});
|
|
26
27
|
|
|
@@ -30,3 +31,8 @@ registerProjectCleanup({
|
|
|
30
31
|
clearProjectPresence(projectId);
|
|
31
32
|
}
|
|
32
33
|
});
|
|
34
|
+
|
|
35
|
+
registerProjectCleanup({
|
|
36
|
+
name: 'worktrees',
|
|
37
|
+
run: (projectId) => removeProjectWorktrees(projectId)
|
|
38
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which processes belong to a project.
|
|
3
|
+
*
|
|
4
|
+
* A PtyKit namespace is a project id, so an active PTY session's shell pid is
|
|
5
|
+
* the root of everything that project spawned. Two features ask this and walk
|
|
6
|
+
* it in opposite directions — Ports climbs from a listening socket's pid to
|
|
7
|
+
* find the owning session, Project Info descends from the project to total its
|
|
8
|
+
* usage — so the roots themselves are defined once, here.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { projectQueries } from '$backend/database/queries';
|
|
12
|
+
import { ptyKitManager } from '../terminal/ptykit';
|
|
13
|
+
import { debug } from '$shared/utils/logger';
|
|
14
|
+
|
|
15
|
+
/** An active terminal shell, and the project it belongs to. */
|
|
16
|
+
export interface ProjectShell {
|
|
17
|
+
pid: number;
|
|
18
|
+
sessionId: string;
|
|
19
|
+
projectId: string;
|
|
20
|
+
projectName: string | null;
|
|
21
|
+
cwd: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function shellsOf(projectId: string, projectName: string | null): ProjectShell[] {
|
|
25
|
+
const shells: ProjectShell[] = [];
|
|
26
|
+
for (const session of ptyKitManager.list(projectId)) {
|
|
27
|
+
const info = session.info();
|
|
28
|
+
if (info.status !== 'active' || !Number.isFinite(info.pid) || info.pid <= 0) continue;
|
|
29
|
+
shells.push({
|
|
30
|
+
pid: info.pid,
|
|
31
|
+
sessionId: info.sessionId,
|
|
32
|
+
projectId,
|
|
33
|
+
projectName,
|
|
34
|
+
cwd: info.cwd
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return shells;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Every active shell on this host, keyed by pid. */
|
|
41
|
+
export function collectSessionPids(): Map<number, ProjectShell> {
|
|
42
|
+
const sessions = new Map<number, ProjectShell>();
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
for (const project of projectQueries.getAll()) {
|
|
46
|
+
for (const shell of shellsOf(project.id, project.name ?? null)) {
|
|
47
|
+
sessions.set(shell.pid, shell);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} catch (error) {
|
|
51
|
+
debug.log('project', 'could not read terminal sessions:', error);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return sessions;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Root pids to descend from when totalling one project's usage. */
|
|
58
|
+
export function projectShellPids(projectId: string): number[] {
|
|
59
|
+
try {
|
|
60
|
+
return shellsOf(projectId, null).map((shell) => shell.pid);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
debug.log('project', 'could not read terminal sessions:', error);
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -21,6 +21,7 @@ import { getSnapshotFiles } from './gitignore';
|
|
|
21
21
|
import { fileWatcher } from '../files/file-watcher';
|
|
22
22
|
import type { MessageSnapshot, SessionScopedChanges } from '$shared/types/database/schema';
|
|
23
23
|
import { calculateFileChangeStats } from '$shared/utils/diff-calculator';
|
|
24
|
+
import { makeScopeKey } from '$shared/utils/workspace-scope';
|
|
24
25
|
import { debug } from '$shared/utils/logger';
|
|
25
26
|
|
|
26
27
|
interface SnapshotMetadata {
|
|
@@ -218,8 +219,11 @@ export class SnapshotService {
|
|
|
218
219
|
}
|
|
219
220
|
|
|
220
221
|
// The dirty set is no longer the snapshot source, but clear it so it does
|
|
221
|
-
// not grow unbounded for the Files panel UI.
|
|
222
|
-
|
|
222
|
+
// not grow unbounded for the Files panel UI. Keyed per workspace, so a
|
|
223
|
+
// worktree session must not clear the main tree's set.
|
|
224
|
+
fileWatcher.clearDirtyFiles(
|
|
225
|
+
makeScopeKey(projectId, sessionQueries.getById(sessionId)?.worktree_id ?? null)
|
|
226
|
+
);
|
|
223
227
|
|
|
224
228
|
// No changes vs the baseline → reuse the existing head snapshot.
|
|
225
229
|
if (Object.keys(sessionChanges).length === 0 && previousSnapshot) {
|
|
@@ -7,13 +7,14 @@
|
|
|
7
7
|
* There is no second WebSocket — the server is driven over Clopen's existing
|
|
8
8
|
* app-wide socket via `handleFrame` (see `backend/ws/terminal/tunnel.ts`).
|
|
9
9
|
*
|
|
10
|
-
* - `namespace` =
|
|
10
|
+
* - `namespace` = workspace scope key (project, or one of its worktrees).
|
|
11
11
|
* - `authorize` bridges to Clopen's project-access check.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import { PtyKitManager } from '@myrialabs/ptykit/core';
|
|
15
15
|
import { createPtyKitServer } from '@myrialabs/ptykit/server';
|
|
16
16
|
import { projectQueries } from '$backend/database/queries';
|
|
17
|
+
import { isScopeOfProject, makeScopeKey, scopeProjectId } from '$shared/utils/workspace-scope';
|
|
17
18
|
|
|
18
19
|
/** Terminal-friendly env, matching the previous `createCleanPtyEnv` overrides. */
|
|
19
20
|
const TERMINAL_ENV = {
|
|
@@ -46,19 +47,47 @@ export const ptyKitServer = createPtyKitServer(ptyKitManager, {
|
|
|
46
47
|
authorize: (ctx) => {
|
|
47
48
|
const userId = ctx.conn.data.userId;
|
|
48
49
|
if (typeof userId !== 'string' || !userId) return false;
|
|
49
|
-
|
|
50
|
+
// The namespace separates worktrees; access is still the project's call.
|
|
51
|
+
const allowed = projectQueries.userHasProject(userId, scopeProjectId(ctx.namespace));
|
|
52
|
+
if (allowed) knownNamespaces.add(ctx.namespace);
|
|
53
|
+
return allowed;
|
|
50
54
|
}
|
|
51
55
|
});
|
|
52
56
|
|
|
53
57
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
58
|
+
* Namespaces this process has served. PtyKit lists sessions per namespace only,
|
|
59
|
+
* so reaping a whole project needs the set of its workspace scopes.
|
|
56
60
|
*/
|
|
57
|
-
|
|
61
|
+
const knownNamespaces = new Set<string>();
|
|
62
|
+
|
|
63
|
+
/** Kill every session in one namespace. */
|
|
64
|
+
function killNamespace(namespace: string): number {
|
|
58
65
|
let killed = 0;
|
|
59
|
-
for (const session of ptyKitManager.list(
|
|
66
|
+
for (const session of ptyKitManager.list(namespace)) {
|
|
60
67
|
ptyKitManager.killSession(session.sessionId, 'SIGKILL');
|
|
61
68
|
killed++;
|
|
62
69
|
}
|
|
63
70
|
return killed;
|
|
64
71
|
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Kill every PTY session belonging to a project, across the main tree and all
|
|
75
|
+
* of its worktrees. Used on project delete/remove to reap orphaned shells.
|
|
76
|
+
*/
|
|
77
|
+
export function cleanupProjectSessions(projectId: string): number {
|
|
78
|
+
let killed = 0;
|
|
79
|
+
for (const namespace of knownNamespaces) {
|
|
80
|
+
if (!isScopeOfProject(namespace, projectId)) continue;
|
|
81
|
+
killed += killNamespace(namespace);
|
|
82
|
+
knownNamespaces.delete(namespace);
|
|
83
|
+
}
|
|
84
|
+
return killed;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Kill every PTY session inside one worktree — used when it is deleted. */
|
|
88
|
+
export function cleanupWorktreeSessions(projectId: string, worktreeId: string): number {
|
|
89
|
+
const namespace = makeScopeKey(projectId, worktreeId);
|
|
90
|
+
const killed = killNamespace(namespace);
|
|
91
|
+
knownNamespaces.delete(namespace);
|
|
92
|
+
return killed;
|
|
93
|
+
}
|
package/backend/utils/ws.ts
CHANGED
|
@@ -64,6 +64,8 @@ interface WSMetrics {
|
|
|
64
64
|
interface ConnectionState {
|
|
65
65
|
userId: string | null;
|
|
66
66
|
projectId: string | null;
|
|
67
|
+
/** Worktree this connection is viewing; null = the main project tree */
|
|
68
|
+
worktreeId: string | null;
|
|
67
69
|
/** Chat session IDs this connection is subscribed to */
|
|
68
70
|
chatSessionIds: Set<string>;
|
|
69
71
|
/** Cleanup functions called automatically on unregister (connection close) */
|
|
@@ -149,7 +151,7 @@ class WSServer {
|
|
|
149
151
|
const id = crypto.randomUUID();
|
|
150
152
|
this.rawToId.set(raw, id);
|
|
151
153
|
this.connections.set(id, conn);
|
|
152
|
-
this.connectionState.set(id, { userId: null, projectId: null, chatSessionIds: new Set(), cleanups: new Set(), authenticated: false, role: null, sessionTokenHash: null, lastSessionCheck: 0 });
|
|
154
|
+
this.connectionState.set(id, { userId: null, projectId: null, worktreeId: null, chatSessionIds: new Set(), cleanups: new Set(), authenticated: false, role: null, sessionTokenHash: null, lastSessionCheck: 0 });
|
|
153
155
|
|
|
154
156
|
this.metrics.totalConnections = this.connections.size;
|
|
155
157
|
debug.log('websocket', `Connection registered: ${id} (total: ${this.connections.size})`);
|
|
@@ -366,6 +368,9 @@ class WSServer {
|
|
|
366
368
|
// Update connection state
|
|
367
369
|
if (state) {
|
|
368
370
|
state.projectId = projectId;
|
|
371
|
+
// A worktree belongs to one project; carrying it across would point
|
|
372
|
+
// the connection at a tree the new project does not own.
|
|
373
|
+
if (oldProjectId !== projectId) state.worktreeId = null;
|
|
369
374
|
}
|
|
370
375
|
|
|
371
376
|
// Add to new project room by ID (Map.set replaces if same key → no duplicates)
|
|
@@ -390,6 +395,21 @@ class WSServer {
|
|
|
390
395
|
debug.log('websocket', `Connection ${wsId} set to project: ${projectId}`);
|
|
391
396
|
}
|
|
392
397
|
|
|
398
|
+
/** Set the worktree this connection is viewing (null = main tree). */
|
|
399
|
+
setWorktree(conn: WSConnection, worktreeId: string | null): void {
|
|
400
|
+
const wsId = this.ensureRegistered(conn);
|
|
401
|
+
const state = this.connectionState.get(wsId);
|
|
402
|
+
if (state) state.worktreeId = worktreeId;
|
|
403
|
+
debug.log('websocket', `Connection ${wsId} set to worktree: ${worktreeId ?? 'main'}`);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** Worktree this connection is viewing, or null for the main project tree. */
|
|
407
|
+
getWorktreeId(conn: WSConnection): string | null {
|
|
408
|
+
const id = this.resolveId(conn);
|
|
409
|
+
if (!id) return null;
|
|
410
|
+
return this.connectionState.get(id)?.worktreeId ?? null;
|
|
411
|
+
}
|
|
412
|
+
|
|
393
413
|
/**
|
|
394
414
|
* Get project ID for a connection.
|
|
395
415
|
* Throws if not set - ensures single source of truth.
|