@myrialabs/clopen 0.4.48 → 0.4.50
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/background-wait.test.ts +28 -0
- package/backend/chat/message-pages.test.ts +180 -0
- package/backend/chat/message-pages.ts +237 -0
- package/backend/chat/stream-manager.ts +101 -18
- package/backend/database/migrations/087_create_blob_line_stats.ts +37 -0
- package/backend/database/migrations/088_create_messages_fts_map.ts +52 -0
- package/backend/database/migrations/089_add_memory_layout_tiles.ts +58 -0
- package/backend/database/migrations/index.ts +21 -0
- package/backend/database/queries/blob-line-stats-queries.ts +48 -0
- package/backend/database/queries/graph-queries.ts +392 -135
- package/backend/database/queries/index.ts +2 -0
- package/backend/database/queries/message-fts.test.ts +120 -0
- package/backend/database/queries/message-queries.ts +45 -9
- package/backend/database/queries/session-queries.ts +3 -2
- package/backend/database/queries/utils-queries.ts +3 -0
- package/backend/db-client/env/diff.ts +9 -33
- package/backend/db-client/env/env.test.ts +8 -8
- package/backend/db-client/env/service.ts +1 -1
- package/backend/engine/adapters/claude/environment.ts +4 -0
- package/backend/engine/adapters/claude/input-gate.test.ts +59 -0
- package/backend/engine/adapters/claude/input-gate.ts +74 -0
- package/backend/engine/adapters/claude/message-converter.test.ts +16 -0
- package/backend/engine/adapters/claude/message-converter.ts +8 -0
- package/backend/engine/adapters/claude/stream.ts +15 -0
- package/backend/files/file-watcher.ts +19 -0
- package/backend/git/contributors.test.ts +35 -0
- package/backend/git/contributors.ts +46 -0
- package/backend/git/identity/scope-config.test.ts +7 -4
- package/backend/http/static-assets.test.ts +78 -0
- package/backend/http/static-assets.ts +104 -0
- package/backend/index.ts +46 -17
- package/backend/memory/README.md +7 -4
- package/backend/memory/layout.test.ts +377 -0
- package/backend/memory/layout.ts +1029 -265
- package/backend/memory/view.ts +257 -386
- package/backend/preview/browser/browser-preview-service.ts +24 -6
- package/backend/project/status-manager.ts +4 -2
- package/backend/snapshot/blob-store.ts +9 -1
- package/backend/snapshot/helpers.ts +3 -10
- package/backend/snapshot/line-stats.test.ts +80 -0
- package/backend/snapshot/line-stats.ts +64 -0
- package/backend/snapshot/snapshot-service.ts +14 -27
- package/backend/snapshot/turn-changes.test.ts +10 -5
- package/backend/snapshot/turn-changes.ts +78 -40
- package/backend/terminal/ptykit.ts +6 -1
- package/backend/utils/ws-wire.test.ts +87 -0
- package/backend/utils/ws-wire.ts +189 -0
- package/backend/utils/ws.ts +40 -38
- package/backend/ws/chat/stream.ts +60 -20
- package/backend/ws/files/read.ts +30 -0
- package/backend/ws/git/log.ts +52 -0
- package/backend/ws/memory/crud.ts +118 -81
- package/backend/ws/messages/crud.ts +51 -0
- package/backend/ws/preview/index.ts +2 -0
- package/backend/ws/preview/watchers.ts +84 -0
- package/backend/ws/sessions/crud.ts +8 -12
- package/backend/ws/snapshot/changes.ts +5 -6
- package/backend/ws/terminal/tunnel.ts +2 -2
- package/bin/clopen.ts +37 -30
- package/dist/assets/addon-clipboard-wHJhZAA4.js.br +0 -0
- package/dist/assets/addon-clipboard-wHJhZAA4.js.gz +0 -0
- package/dist/assets/addon-fit-DX4qG4td.js.br +1 -0
- package/dist/assets/addon-fit-DX4qG4td.js.gz +0 -0
- package/dist/assets/addon-ligatures-DpDY8N_X.js.br +0 -0
- package/dist/assets/addon-ligatures-DpDY8N_X.js.gz +0 -0
- package/dist/assets/addon-unicode11-Bt8F3D7-.js.br +0 -0
- package/dist/assets/addon-unicode11-Bt8F3D7-.js.gz +0 -0
- package/dist/assets/addon-web-links-DIbG5aQx.js.br +0 -0
- package/dist/assets/addon-web-links-DIbG5aQx.js.gz +0 -0
- package/dist/assets/diff.worker-BoHLxmr2.js +7 -0
- package/dist/assets/diff.worker-BoHLxmr2.js.br +0 -0
- package/dist/assets/diff.worker-BoHLxmr2.js.gz +0 -0
- package/dist/assets/{index-DJzwt-hq.js → index-By6KdSLT.js} +1 -1
- package/dist/assets/index-By6KdSLT.js.br +0 -0
- package/dist/assets/index-By6KdSLT.js.gz +0 -0
- package/dist/assets/{index-CONlwAXZ.js → index-DV4TiDn1.js} +1 -1
- package/dist/assets/index-DV4TiDn1.js.br +0 -0
- package/dist/assets/index-DV4TiDn1.js.gz +0 -0
- package/dist/assets/main-DIESTGTD.css +1 -0
- package/dist/assets/main-DIESTGTD.css.br +0 -0
- package/dist/assets/main-DIESTGTD.css.gz +0 -0
- package/dist/assets/{main-nBOb9YOA.js → main-Da3JKoua.js} +623 -623
- package/dist/assets/main-Da3JKoua.js.br +0 -0
- package/dist/assets/main-Da3JKoua.js.gz +0 -0
- package/dist/assets/preview-video.worker-DcpDhCWL.js.br +0 -0
- package/dist/assets/preview-video.worker-DcpDhCWL.js.gz +0 -0
- package/dist/assets/xterm-B-qIQCd3.js.br +0 -0
- package/dist/assets/xterm-B-qIQCd3.js.gz +0 -0
- package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.br +0 -0
- package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.gz +0 -0
- package/dist/fonts/dm-sans.css.br +0 -0
- package/dist/fonts/dm-sans.css.gz +0 -0
- package/dist/index.html +2 -2
- package/dist/index.html.br +0 -0
- package/dist/index.html.gz +0 -0
- package/dist/sw.js.br +0 -0
- package/dist/sw.js.gz +0 -0
- package/package.json +3 -4
- package/shared/types/memory.ts +96 -73
- package/shared/types/unified/index.ts +1 -0
- package/shared/types/unified/stream.ts +33 -2
- package/shared/utils/diff-calculator.test.ts +178 -41
- package/shared/utils/diff-calculator.ts +383 -100
- package/shared/utils/process-supervisor.test.ts +242 -0
- package/shared/utils/process-supervisor.ts +356 -0
- package/shared/utils/process-tree.ts +99 -0
- package/shared/utils/ws-chunk.test.ts +33 -0
- package/shared/utils/ws-chunk.ts +73 -0
- package/shared/utils/ws-client-chunks.test.ts +45 -0
- package/shared/utils/ws-client.ts +43 -0
- package/shared/utils/ws-server.ts +17 -14
- package/backend/database/migrations/086_single_owner.test.ts +0 -170
- package/dist/assets/graph-layout.worker-CRFqN7ZQ.js +0 -1
- package/dist/assets/main-BUiUL3eZ.css +0 -1
|
@@ -16,6 +16,7 @@ import { ws } from '$backend/utils/ws';
|
|
|
16
16
|
import { defaultRotation, type DeviceRequest } from '$shared/constants/preview.js';
|
|
17
17
|
import { applyViewport, type EmulationPatch } from './viewport';
|
|
18
18
|
import { debug } from '$shared/utils/logger';
|
|
19
|
+
import { emitToPreviewWatchers, forgetAgentCursor, rememberAgentCursor } from '../../ws/preview/watchers';
|
|
19
20
|
import { sleep } from '$shared/utils/async';
|
|
20
21
|
import { scopeProjectId } from '$shared/utils/workspace-scope';
|
|
21
22
|
import type {
|
|
@@ -1786,12 +1787,15 @@ class BrowserPreviewServiceManager {
|
|
|
1786
1787
|
});
|
|
1787
1788
|
|
|
1788
1789
|
// Forward console events
|
|
1790
|
+
// Console lines are a panel's live detail, as frequent as the page is
|
|
1791
|
+
// chatty: only connections with a Preview open receive them. A panel that
|
|
1792
|
+
// opens later seeds itself from the console history.
|
|
1789
1793
|
service.on('preview:browser-console-message', (data) => {
|
|
1790
|
-
|
|
1794
|
+
emitToPreviewWatchers(roomId, 'preview:browser-console-message', data);
|
|
1791
1795
|
});
|
|
1792
1796
|
|
|
1793
1797
|
service.on('preview:browser-console-clear', (data) => {
|
|
1794
|
-
|
|
1798
|
+
emitToPreviewWatchers(roomId, 'preview:browser-console-clear', data);
|
|
1795
1799
|
});
|
|
1796
1800
|
|
|
1797
1801
|
// Forward dialog events
|
|
@@ -1910,6 +1914,7 @@ class BrowserPreviewServiceManager {
|
|
|
1910
1914
|
// No longer pinned: an unwatched tab the agent has let go of is free
|
|
1911
1915
|
// to fall asleep on the usual timer.
|
|
1912
1916
|
this.services.get(data.projectId)?.noteTabActivity(data.browserTabId);
|
|
1917
|
+
forgetAgentCursor(data.projectId, data.browserTabId);
|
|
1913
1918
|
ws.emit.project(data.projectId, 'preview:browser-mcp-control-end', {
|
|
1914
1919
|
browserTabId: data.browserTabId,
|
|
1915
1920
|
projectId: data.projectId,
|
|
@@ -1926,21 +1931,34 @@ class BrowserPreviewServiceManager {
|
|
|
1926
1931
|
});
|
|
1927
1932
|
});
|
|
1928
1933
|
|
|
1934
|
+
// The agent's cursor moves many times a second while it works and is only
|
|
1935
|
+
// drawn inside a Preview, so its steps go to connections with one open;
|
|
1936
|
+
// where it came to rest is remembered for a Preview that opens later.
|
|
1929
1937
|
browserMcpControl.on('cursor-position', (data) => {
|
|
1930
1938
|
if (!data.projectId) return;
|
|
1931
|
-
|
|
1939
|
+
const cursor = {
|
|
1932
1940
|
sessionId: data.tabId,
|
|
1933
1941
|
x: data.x,
|
|
1934
1942
|
y: data.y,
|
|
1935
1943
|
pressed: data.pressed ?? false,
|
|
1936
1944
|
timestamp: data.timestamp,
|
|
1937
1945
|
source: 'mcp' as const
|
|
1938
|
-
}
|
|
1946
|
+
};
|
|
1947
|
+
rememberAgentCursor(data.projectId, cursor);
|
|
1948
|
+
emitToPreviewWatchers(data.projectId, 'preview:browser-mcp-cursor-position', cursor);
|
|
1939
1949
|
});
|
|
1940
1950
|
|
|
1941
1951
|
browserMcpControl.on('cursor-glide', (data) => {
|
|
1942
1952
|
if (!data.projectId) return;
|
|
1943
|
-
|
|
1953
|
+
rememberAgentCursor(data.projectId, {
|
|
1954
|
+
sessionId: data.tabId,
|
|
1955
|
+
x: data.to.x,
|
|
1956
|
+
y: data.to.y,
|
|
1957
|
+
pressed: data.pressed ?? false,
|
|
1958
|
+
timestamp: data.timestamp,
|
|
1959
|
+
source: 'mcp' as const
|
|
1960
|
+
});
|
|
1961
|
+
emitToPreviewWatchers(data.projectId, 'preview:browser-mcp-cursor-glide', {
|
|
1944
1962
|
sessionId: data.tabId,
|
|
1945
1963
|
from: data.from,
|
|
1946
1964
|
control: data.control,
|
|
@@ -1955,7 +1973,7 @@ class BrowserPreviewServiceManager {
|
|
|
1955
1973
|
|
|
1956
1974
|
browserMcpControl.on('cursor-click', (data) => {
|
|
1957
1975
|
if (!data.projectId) return;
|
|
1958
|
-
|
|
1976
|
+
emitToPreviewWatchers(data.projectId, 'preview:browser-mcp-cursor-click', {
|
|
1959
1977
|
sessionId: data.tabId,
|
|
1960
1978
|
x: data.x,
|
|
1961
1979
|
y: data.y,
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Shared logic for getting project status data
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { streamManager, type StreamState } from '../chat/stream-manager.js';
|
|
6
|
+
import { streamManager, waitingOnBackground, type StreamState } from '../chat/stream-manager.js';
|
|
7
7
|
import { sessionQueries } from '../database/queries/session-queries.js';
|
|
8
8
|
import type { UnifiedMessage } from '$shared/types/unified';
|
|
9
9
|
import { isWaitingForInteractiveInput } from '$shared/utils/interactive-input';
|
|
@@ -48,7 +48,9 @@ function toStreamStatus(stream: StreamState, worktreeMemo: Map<string, string |
|
|
|
48
48
|
status: stream.status,
|
|
49
49
|
startedAt: stream.startedAt,
|
|
50
50
|
messagesCount: stream.messages.length,
|
|
51
|
-
isWaitingInput: detectStreamWaitingInput(stream)
|
|
51
|
+
isWaitingInput: detectStreamWaitingInput(stream),
|
|
52
|
+
/** Background tasks the stream waits on between turns (0 while generating). */
|
|
53
|
+
waitingOnBackground: waitingOnBackground(stream)
|
|
52
54
|
};
|
|
53
55
|
}
|
|
54
56
|
|
|
@@ -14,6 +14,7 @@ import fs from 'fs/promises';
|
|
|
14
14
|
import { gzipSync, gunzipSync } from 'zlib';
|
|
15
15
|
import { debug } from '$shared/utils/logger';
|
|
16
16
|
import { getClopenDir } from '../utils/index.js';
|
|
17
|
+
import { blobLineStatsQueries } from '../database/queries/blob-line-stats-queries';
|
|
17
18
|
|
|
18
19
|
const SNAPSHOTS_DIR = join(getClopenDir(), 'snapshots');
|
|
19
20
|
const BLOBS_DIR = join(SNAPSHOTS_DIR, 'blobs');
|
|
@@ -156,7 +157,8 @@ class BlobStore {
|
|
|
156
157
|
|
|
157
158
|
/**
|
|
158
159
|
* Delete multiple blobs by hash.
|
|
159
|
-
* Also invalidates fileHashCache entries whose hash matches a deleted blob
|
|
160
|
+
* Also invalidates fileHashCache entries whose hash matches a deleted blob,
|
|
161
|
+
* and the cached line counts of every pair that names one.
|
|
160
162
|
*/
|
|
161
163
|
async deleteBlobs(hashes: string[]): Promise<number> {
|
|
162
164
|
const hashSet = new Set(hashes);
|
|
@@ -177,6 +179,12 @@ class BlobStore {
|
|
|
177
179
|
}
|
|
178
180
|
}
|
|
179
181
|
|
|
182
|
+
try {
|
|
183
|
+
blobLineStatsQueries.deleteByHashes(hashes);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
debug.warn('snapshot', 'Failed to prune cached line stats:', error);
|
|
186
|
+
}
|
|
187
|
+
|
|
180
188
|
return deleted;
|
|
181
189
|
}
|
|
182
190
|
|
|
@@ -3,6 +3,7 @@ import { blobStore } from './blob-store';
|
|
|
3
3
|
import { debug } from '$shared/utils/logger';
|
|
4
4
|
import { loadMessage } from '$shared/utils/message-formatter';
|
|
5
5
|
import { buildTurnFiles, summariseTurnFiles } from './turn-changes';
|
|
6
|
+
import { blobLineStats } from './line-stats';
|
|
6
7
|
import type { DatabaseMessage, SessionScopedChanges } from '$shared/types/database/schema';
|
|
7
8
|
import type { UnifiedMessage, UserMessage } from '$shared/types/unified';
|
|
8
9
|
|
|
@@ -348,16 +349,8 @@ export async function getCheckpointFileStats(
|
|
|
348
349
|
const changes = JSON.parse(snapshot.session_changes as string) as SessionScopedChanges;
|
|
349
350
|
if (Object.keys(changes).length === 0) return stored;
|
|
350
351
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
try {
|
|
354
|
-
return await blobStore.readBlob(hash);
|
|
355
|
-
} catch (err) {
|
|
356
|
-
blobMissing = true;
|
|
357
|
-
throw err;
|
|
358
|
-
}
|
|
359
|
-
});
|
|
360
|
-
return blobMissing ? stored : summariseTurnFiles(files);
|
|
352
|
+
const files = await buildTurnFiles(changes, blobLineStats);
|
|
353
|
+
return files.some((file) => !file.contentAvailable) ? stored : summariseTurnFiles(files);
|
|
361
354
|
} catch {
|
|
362
355
|
return stored;
|
|
363
356
|
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Counting each pair of blobs once.
|
|
3
|
+
*
|
|
4
|
+
* A chat open used to re-diff every changed file of every turn, and a single
|
|
5
|
+
* large file made that block the server for minutes. The pair's count is a
|
|
6
|
+
* fact about two content hashes, so it is computed on first use and read back
|
|
7
|
+
* from then on — and dropped when the blobs it describes are deleted.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect, beforeAll } from 'bun:test';
|
|
11
|
+
import { randomUUID } from 'node:crypto';
|
|
12
|
+
|
|
13
|
+
import { initializeDatabase } from '../database';
|
|
14
|
+
import { blobLineStatsQueries } from '../database/queries/blob-line-stats-queries';
|
|
15
|
+
import { blobStore } from './blob-store';
|
|
16
|
+
import { blobLineStats } from './line-stats';
|
|
17
|
+
|
|
18
|
+
/** Fresh content per test, so its hashes are not shared with any other run. */
|
|
19
|
+
async function storePair(oldText: string, newText: string) {
|
|
20
|
+
const salt = randomUUID();
|
|
21
|
+
const oldHash = await blobStore.storeBlob(Buffer.from(`${salt}\n${oldText}`, 'utf8'));
|
|
22
|
+
const newHash = await blobStore.storeBlob(Buffer.from(`${salt}\n${newText}`, 'utf8'));
|
|
23
|
+
return { oldHash, newHash };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
beforeAll(async () => {
|
|
27
|
+
await initializeDatabase();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('blobLineStats', () => {
|
|
31
|
+
it('counts a pair on first use and remembers it', async () => {
|
|
32
|
+
const { oldHash, newHash } = await storePair('a\nb\nc\n', 'a\nB\nc\nd\n');
|
|
33
|
+
|
|
34
|
+
expect(blobLineStatsQueries.get(oldHash, newHash)).toBeNull();
|
|
35
|
+
|
|
36
|
+
const first = await blobLineStats(oldHash, newHash);
|
|
37
|
+
expect(first).toEqual({ insertions: 2, deletions: 1, isBinary: false, contentAvailable: true });
|
|
38
|
+
expect(blobLineStatsQueries.get(oldHash, newHash)).toEqual({ insertions: 2, deletions: 1, isBinary: false });
|
|
39
|
+
|
|
40
|
+
expect(await blobLineStats(oldHash, newHash)).toEqual(first);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('counts a created file against an empty side', async () => {
|
|
44
|
+
const { newHash } = await storePair('', 'one\ntwo\n');
|
|
45
|
+
|
|
46
|
+
// The salt line makes it three.
|
|
47
|
+
expect(await blobLineStats('', newHash)).toEqual({
|
|
48
|
+
insertions: 3,
|
|
49
|
+
deletions: 0,
|
|
50
|
+
isBinary: false,
|
|
51
|
+
contentAvailable: true
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('shares one computation between concurrent callers', async () => {
|
|
56
|
+
const { oldHash, newHash } = await storePair('x\n', 'y\n');
|
|
57
|
+
|
|
58
|
+
const [a, b] = await Promise.all([blobLineStats(oldHash, newHash), blobLineStats(oldHash, newHash)]);
|
|
59
|
+
expect(a).toBe(b);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('never stores a pair whose blob is missing', async () => {
|
|
63
|
+
const { newHash } = await storePair('', 'x\n');
|
|
64
|
+
const missing = 'f'.repeat(64);
|
|
65
|
+
|
|
66
|
+
const stats = await blobLineStats(missing, newHash);
|
|
67
|
+
expect(stats.contentAvailable).toBe(false);
|
|
68
|
+
expect(blobLineStatsQueries.get(missing, newHash)).toBeNull();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('drops the remembered count when its blobs are deleted', async () => {
|
|
72
|
+
const { oldHash, newHash } = await storePair('a\n', 'b\n');
|
|
73
|
+
await blobLineStats(oldHash, newHash);
|
|
74
|
+
|
|
75
|
+
await blobStore.deleteBlobs([oldHash, newHash]);
|
|
76
|
+
|
|
77
|
+
expect(blobLineStatsQueries.get(oldHash, newHash)).toBeNull();
|
|
78
|
+
expect((await blobLineStats(oldHash, newHash)).contentAvailable).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The production `LineStatsSource`: every pair of blobs is counted once.
|
|
3
|
+
*
|
|
4
|
+
* Blobs are content-addressed, so the +/- count of (oldHash, newHash) is a
|
|
5
|
+
* fact that never changes. It used to be recomputed on every chat open, every
|
|
6
|
+
* reconnect and every pending-changes poll — twice per request, once for the
|
|
7
|
+
* turn list and once for the net total — and a single large file made each of
|
|
8
|
+
* those block the server. Now the first caller computes it and every later
|
|
9
|
+
* one, on any surface, reads the same row (migration 087).
|
|
10
|
+
*
|
|
11
|
+
* The count itself is `countPairFromBlobs`; this module only remembers it.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { blobLineStatsQueries } from '../database/queries/blob-line-stats-queries';
|
|
15
|
+
import { blobStore } from './blob-store';
|
|
16
|
+
import { countPairFromBlobs } from './turn-changes';
|
|
17
|
+
import type { LineStatsSource, PairLineStats } from './turn-changes';
|
|
18
|
+
import { debug } from '$shared/utils/logger';
|
|
19
|
+
|
|
20
|
+
/** Pairs being counted right now, so concurrent callers share one computation. */
|
|
21
|
+
const inFlight = new Map<string, Promise<PairLineStats>>();
|
|
22
|
+
|
|
23
|
+
const readBlob = (hash: string) => blobStore.readBlob(hash);
|
|
24
|
+
|
|
25
|
+
/** Whether both sides of a pair can still be read — the diff view needs them. */
|
|
26
|
+
async function contentAvailable(oldHash: string, newHash: string): Promise<boolean> {
|
|
27
|
+
const [hasOld, hasNew] = await Promise.all([
|
|
28
|
+
oldHash ? blobStore.hasBlob(oldHash) : true,
|
|
29
|
+
newHash ? blobStore.hasBlob(newHash) : true
|
|
30
|
+
]);
|
|
31
|
+
return hasOld && hasNew;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function computeAndRemember(oldHash: string, newHash: string): Promise<PairLineStats> {
|
|
35
|
+
const stats = await countPairFromBlobs(oldHash, newHash, readBlob);
|
|
36
|
+
// A missing blob yields a non-answer, not a count — never store it.
|
|
37
|
+
if (stats.contentAvailable) {
|
|
38
|
+
try {
|
|
39
|
+
blobLineStatsQueries.save(oldHash, newHash, stats);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
debug.warn('snapshot', 'Failed to cache line stats:', error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return stats;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const blobLineStats: LineStatsSource = async (oldHash, newHash) => {
|
|
48
|
+
try {
|
|
49
|
+
const cached = blobLineStatsQueries.get(oldHash, newHash);
|
|
50
|
+
if (cached) {
|
|
51
|
+
return { ...cached, contentAvailable: await contentAvailable(oldHash, newHash) };
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
debug.warn('snapshot', 'Failed to read cached line stats:', error);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const key = `${oldHash}:${newHash}`;
|
|
58
|
+
const pending = inFlight.get(key);
|
|
59
|
+
if (pending) return pending;
|
|
60
|
+
|
|
61
|
+
const computation = computeAndRemember(oldHash, newHash).finally(() => inFlight.delete(key));
|
|
62
|
+
inFlight.set(key, computation);
|
|
63
|
+
return computation;
|
|
64
|
+
};
|
|
@@ -23,7 +23,8 @@ import { attributeGitChanges, readRepoHeads, type RepoHeads } from './git-attrib
|
|
|
23
23
|
import { TurnActivity, type ObservedOutput } from './turn-activity';
|
|
24
24
|
import { fileWatcher } from '../files/file-watcher';
|
|
25
25
|
import type { MessageSnapshot, SessionScopedChanges } from '$shared/types/database/schema';
|
|
26
|
-
import {
|
|
26
|
+
import { buildTurnFiles, summariseTurnFiles } from './turn-changes';
|
|
27
|
+
import { blobLineStats } from './line-stats';
|
|
27
28
|
import { makeScopeKey } from '$shared/utils/workspace-scope';
|
|
28
29
|
import { debug } from '$shared/utils/logger';
|
|
29
30
|
|
|
@@ -180,14 +181,13 @@ export class SnapshotService {
|
|
|
180
181
|
private async scanTree(
|
|
181
182
|
root: string,
|
|
182
183
|
previous: TreeMap
|
|
183
|
-
): Promise<{ tree: TreeMap
|
|
184
|
+
): Promise<{ tree: TreeMap }> {
|
|
184
185
|
const scan = await scanSnapshotFiles(root, { pruneGenerated: true });
|
|
185
186
|
if (scan.incomplete.includes('')) {
|
|
186
187
|
throw new Error(`Could not list the files of ${root}`);
|
|
187
188
|
}
|
|
188
189
|
|
|
189
190
|
const tree: TreeMap = {};
|
|
190
|
-
const contents = new Map<string, Buffer>();
|
|
191
191
|
|
|
192
192
|
if (scan.incomplete.length > 0) {
|
|
193
193
|
for (const [filepath, hash] of Object.entries(previous)) {
|
|
@@ -207,16 +207,13 @@ export class SnapshotService {
|
|
|
207
207
|
|
|
208
208
|
const result = await blobStore.hashFile(filepath);
|
|
209
209
|
tree[relativePath] = result.hash;
|
|
210
|
-
if (result.content !== null && result.hash !== previous[relativePath]) {
|
|
211
|
-
contents.set(relativePath, result.content);
|
|
212
|
-
}
|
|
213
210
|
} catch (error) {
|
|
214
211
|
if (isMissingFile(error)) return;
|
|
215
212
|
if (previous[relativePath]) tree[relativePath] = previous[relativePath];
|
|
216
213
|
}
|
|
217
214
|
});
|
|
218
215
|
|
|
219
|
-
return { tree
|
|
216
|
+
return { tree };
|
|
220
217
|
}
|
|
221
218
|
|
|
222
219
|
// ========================================================================
|
|
@@ -556,7 +553,7 @@ export class SnapshotService {
|
|
|
556
553
|
// closed. A gitignore-aware scan + hash diff against the baseline is
|
|
557
554
|
// always correct, and blobStore.hashFile's mtime+size cache keeps it
|
|
558
555
|
// cheap — only files that actually changed are re-read.
|
|
559
|
-
const { tree: currentTree
|
|
556
|
+
const { tree: currentTree } = await this.scanTree(projectPath, previousTree);
|
|
560
557
|
const sessionChanges = diffTrees(previousTree, currentTree);
|
|
561
558
|
|
|
562
559
|
// A disk diff sees every writer. Take out what is not this turn's.
|
|
@@ -589,7 +586,7 @@ export class SnapshotService {
|
|
|
589
586
|
}
|
|
590
587
|
|
|
591
588
|
// Calculate line-level file change stats
|
|
592
|
-
const fileStats = await this.calculateChangeStats(sessionChanges
|
|
589
|
+
const fileStats = await this.calculateChangeStats(sessionChanges);
|
|
593
590
|
|
|
594
591
|
const metadata: SnapshotMetadata = {
|
|
595
592
|
totalFiles: Object.keys(currentTree).length,
|
|
@@ -1069,27 +1066,17 @@ export class SnapshotService {
|
|
|
1069
1066
|
}
|
|
1070
1067
|
|
|
1071
1068
|
/**
|
|
1072
|
-
*
|
|
1069
|
+
* Line-level change stats for a turn's changed files.
|
|
1070
|
+
*
|
|
1071
|
+
* Counted through the same `buildTurnFiles` + `blobLineStats` every other
|
|
1072
|
+
* surface reads, so the numbers stored on the row are the ones the Changes
|
|
1073
|
+
* tab shows — and counting here, at capture, is what puts each pair in the
|
|
1074
|
+
* cache before anyone opens the chat.
|
|
1073
1075
|
*/
|
|
1074
1076
|
private async calculateChangeStats(
|
|
1075
|
-
sessionChanges: SessionScopedChanges
|
|
1076
|
-
readContents: Map<string, Buffer>
|
|
1077
|
+
sessionChanges: SessionScopedChanges
|
|
1077
1078
|
): Promise<{ filesChanged: number; insertions: number; deletions: number }> {
|
|
1078
|
-
|
|
1079
|
-
const currentSnapshot: Record<string, Buffer> = {};
|
|
1080
|
-
|
|
1081
|
-
for (const [filepath, change] of Object.entries(sessionChanges)) {
|
|
1082
|
-
try {
|
|
1083
|
-
if (change.oldHash) {
|
|
1084
|
-
previousSnapshot[filepath] = await blobStore.readBlob(change.oldHash);
|
|
1085
|
-
}
|
|
1086
|
-
if (change.newHash) {
|
|
1087
|
-
currentSnapshot[filepath] = readContents.get(filepath) ?? await blobStore.readBlob(change.newHash);
|
|
1088
|
-
}
|
|
1089
|
-
} catch { /* skip */ }
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
return calculateFileChangeStats(previousSnapshot, currentSnapshot);
|
|
1079
|
+
return summariseTurnFiles(await buildTurnFiles(sessionChanges, blobLineStats));
|
|
1093
1080
|
}
|
|
1094
1081
|
|
|
1095
1082
|
/**
|
|
@@ -18,16 +18,21 @@ import {
|
|
|
18
18
|
summariseTurnFiles,
|
|
19
19
|
excerptPrompt,
|
|
20
20
|
netChangeRuns,
|
|
21
|
-
summariseNetChanges
|
|
21
|
+
summariseNetChanges,
|
|
22
|
+
statsFromBlobs
|
|
22
23
|
} from './turn-changes';
|
|
24
|
+
import type { LineStatsSource } from './turn-changes';
|
|
23
25
|
|
|
24
|
-
/**
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Line stats over an in-memory blob store, so the tests own their failure
|
|
28
|
+
* modes. Uncached on purpose: the hashes here are labels, not content hashes.
|
|
29
|
+
*/
|
|
30
|
+
function readerFor(blobs: Record<string, string | Buffer>): LineStatsSource {
|
|
31
|
+
return statsFromBlobs(async (hash: string): Promise<Buffer> => {
|
|
27
32
|
const value = blobs[hash];
|
|
28
33
|
if (value === undefined) throw new Error(`missing blob ${hash}`);
|
|
29
34
|
return Buffer.isBuffer(value) ? value : Buffer.from(value, 'utf8');
|
|
30
|
-
};
|
|
35
|
+
});
|
|
31
36
|
}
|
|
32
37
|
|
|
33
38
|
describe('statusOf', () => {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
import type { SessionScopedChanges } from '$shared/types/database/schema';
|
|
20
20
|
import { naturalCompare } from '$shared/utils/compare';
|
|
21
|
-
import {
|
|
21
|
+
import { countLineChangesAsync, isBinaryBuffer } from '$shared/utils/diff-calculator';
|
|
22
22
|
|
|
23
23
|
export { isBinaryBuffer };
|
|
24
24
|
|
|
@@ -46,54 +46,92 @@ export function statusOf(oldHash: string, newHash: string): TurnFileStatus {
|
|
|
46
46
|
return 'modified';
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
/** What one (old, new) pair of blobs amounts to as a +/- badge. */
|
|
50
|
+
export interface PairLineStats {
|
|
51
|
+
insertions: number;
|
|
52
|
+
deletions: number;
|
|
53
|
+
isBinary: boolean;
|
|
54
|
+
/** False when a blob this pair needs is no longer readable. */
|
|
55
|
+
contentAvailable: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Where a pair's numbers come from. Production passes `blobLineStats`
|
|
60
|
+
* (backend/snapshot/line-stats.ts), which counts each pair once and remembers
|
|
61
|
+
* it; tests pass `statsFromBlobs` over an in-memory store.
|
|
62
|
+
*/
|
|
63
|
+
export type LineStatsSource = (oldHash: string, newHash: string) => Promise<PairLineStats>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Count one pair straight from its blobs. This is the single definition of
|
|
67
|
+
* "how many lines did this change" — the cache only stores what it returns.
|
|
68
|
+
*/
|
|
69
|
+
export async function countPairFromBlobs(
|
|
70
|
+
oldHash: string,
|
|
71
|
+
newHash: string,
|
|
72
|
+
readBlob: (hash: string) => Promise<Buffer>
|
|
73
|
+
): Promise<PairLineStats> {
|
|
74
|
+
let oldBuf: Buffer | null = null;
|
|
75
|
+
let newBuf: Buffer | null = null;
|
|
76
|
+
let contentAvailable = true;
|
|
77
|
+
|
|
78
|
+
if (oldHash) {
|
|
79
|
+
try {
|
|
80
|
+
oldBuf = await readBlob(oldHash);
|
|
81
|
+
} catch {
|
|
82
|
+
contentAvailable = false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (newHash) {
|
|
86
|
+
try {
|
|
87
|
+
newBuf = await readBlob(newHash);
|
|
88
|
+
} catch {
|
|
89
|
+
contentAvailable = false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const isBinary =
|
|
94
|
+
(oldBuf !== null && isBinaryBuffer(oldBuf)) || (newBuf !== null && isBinaryBuffer(newBuf));
|
|
95
|
+
|
|
96
|
+
let insertions = 0;
|
|
97
|
+
let deletions = 0;
|
|
98
|
+
if (contentAvailable && !isBinary) {
|
|
99
|
+
// Cooperative: a pathological pair takes long, but never holds the
|
|
100
|
+
// server's thread for more than a few milliseconds at a time.
|
|
101
|
+
const counts = await countLineChangesAsync(
|
|
102
|
+
oldBuf ? oldBuf.toString('utf8') : '',
|
|
103
|
+
newBuf ? newBuf.toString('utf8') : ''
|
|
104
|
+
);
|
|
105
|
+
insertions = counts.additions;
|
|
106
|
+
deletions = counts.deletions;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return { insertions, deletions, isBinary, contentAvailable };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** An uncached source over any blob reader. */
|
|
113
|
+
export function statsFromBlobs(readBlob: (hash: string) => Promise<Buffer>): LineStatsSource {
|
|
114
|
+
return (oldHash, newHash) => countPairFromBlobs(oldHash, newHash, readBlob);
|
|
115
|
+
}
|
|
116
|
+
|
|
49
117
|
/**
|
|
50
118
|
* Turn one checkpoint's `session_changes` into a displayable file list.
|
|
51
119
|
*
|
|
52
|
-
* `
|
|
53
|
-
* only I/O is the
|
|
120
|
+
* `statsFor` is injected so this stays a pure function over its inputs — the
|
|
121
|
+
* only I/O is the source the caller hands in.
|
|
54
122
|
*/
|
|
55
123
|
export async function buildTurnFiles(
|
|
56
124
|
changes: SessionScopedChanges,
|
|
57
|
-
|
|
125
|
+
statsFor: LineStatsSource
|
|
58
126
|
): Promise<TurnFileChange[]> {
|
|
59
127
|
const files: TurnFileChange[] = [];
|
|
60
128
|
|
|
61
129
|
for (const [path, change] of Object.entries(changes)) {
|
|
62
130
|
const status = statusOf(change.oldHash, change.newHash);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (change.oldHash) {
|
|
69
|
-
try {
|
|
70
|
-
oldBuf = await readBlob(change.oldHash);
|
|
71
|
-
} catch {
|
|
72
|
-
contentAvailable = false;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
if (change.newHash) {
|
|
76
|
-
try {
|
|
77
|
-
newBuf = await readBlob(change.newHash);
|
|
78
|
-
} catch {
|
|
79
|
-
contentAvailable = false;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const isBinary =
|
|
84
|
-
(oldBuf !== null && isBinaryBuffer(oldBuf)) || (newBuf !== null && isBinaryBuffer(newBuf));
|
|
85
|
-
|
|
86
|
-
let insertions = 0;
|
|
87
|
-
let deletions = 0;
|
|
88
|
-
if (contentAvailable && !isBinary) {
|
|
89
|
-
const counts = countLineChanges(
|
|
90
|
-
oldBuf ? oldBuf.toString('utf8') : '',
|
|
91
|
-
newBuf ? newBuf.toString('utf8') : ''
|
|
92
|
-
);
|
|
93
|
-
insertions = counts.additions;
|
|
94
|
-
deletions = counts.deletions;
|
|
95
|
-
}
|
|
96
|
-
|
|
131
|
+
const { insertions, deletions, isBinary, contentAvailable } = await statsFor(
|
|
132
|
+
change.oldHash,
|
|
133
|
+
change.newHash
|
|
134
|
+
);
|
|
97
135
|
files.push({ path, status, insertions, deletions, isBinary, contentAvailable });
|
|
98
136
|
}
|
|
99
137
|
|
|
@@ -177,7 +215,7 @@ export function netChangeRuns(turns: SessionScopedChanges[]): Map<string, Change
|
|
|
177
215
|
/** The chat-level numbers: net files changed, and net lines in and out. */
|
|
178
216
|
export async function summariseNetChanges(
|
|
179
217
|
turns: SessionScopedChanges[],
|
|
180
|
-
|
|
218
|
+
statsFor: LineStatsSource
|
|
181
219
|
): Promise<{ filesChanged: number; insertions: number; deletions: number }> {
|
|
182
220
|
let insertions = 0;
|
|
183
221
|
let deletions = 0;
|
|
@@ -189,7 +227,7 @@ export async function summariseNetChanges(
|
|
|
189
227
|
list.forEach((run, index) => {
|
|
190
228
|
changes[`${index}\0${path}`] = run;
|
|
191
229
|
});
|
|
192
|
-
const summary = summariseTurnFiles(await buildTurnFiles(changes,
|
|
230
|
+
const summary = summariseTurnFiles(await buildTurnFiles(changes, statsFor));
|
|
193
231
|
insertions += summary.insertions;
|
|
194
232
|
deletions += summary.deletions;
|
|
195
233
|
}
|
|
@@ -35,13 +35,18 @@ export const ptyKitManager = new PtyKitManager({
|
|
|
35
35
|
// refresh/reconnect can still replay their final screen (matches old 5-min window).
|
|
36
36
|
idleTtl: null,
|
|
37
37
|
retainExitedMs: 5 * 60_000,
|
|
38
|
+
// A flood (a build, a dev server's log) is held to one frame per window;
|
|
39
|
+
// the first chunk after a pause still goes out at once, so typing is not.
|
|
40
|
+
outputCoalesceMs: 16,
|
|
38
41
|
env: { sanitize: true, inject: TERMINAL_ENV }
|
|
39
42
|
});
|
|
40
43
|
|
|
41
44
|
/**
|
|
42
45
|
* Embedded WebSocket server over the manager. No HTTP/Bun mounting — Clopen's
|
|
43
46
|
* tunnel feeds it frames and supplies per-connection `send`. `authorize` enforces
|
|
44
|
-
* project access
|
|
47
|
+
* project access. The room is the workspace scope, which carries awareness (tabs
|
|
48
|
+
* opened or closed on any device); a session's output goes only to the devices
|
|
49
|
+
* that have that tab mounted (PtyKit 0.1.0).
|
|
45
50
|
*/
|
|
46
51
|
export const ptyKitServer = createPtyKitServer(ptyKitManager, {
|
|
47
52
|
room: (ctx) => ctx.namespace,
|