@myrialabs/clopen 0.4.43 → 0.4.45

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.
Files changed (72) hide show
  1. package/backend/artifacts/project/bridge.ts +218 -0
  2. package/backend/artifacts/project/edit.ts +243 -0
  3. package/backend/artifacts/project/index.ts +38 -0
  4. package/backend/artifacts/project/project.test.ts +181 -0
  5. package/backend/artifacts/project/scan.ts +368 -0
  6. package/backend/artifacts/project/sources.ts +100 -0
  7. package/backend/artifacts/sync.ts +1 -1
  8. package/backend/auth/permissions.ts +15 -0
  9. package/backend/chat/stream-manager.ts +26 -8
  10. package/backend/database/migrations/083_create_project_mcp_trust.ts +32 -0
  11. package/backend/database/migrations/index.ts +7 -0
  12. package/backend/database/queries/index.ts +2 -0
  13. package/backend/database/queries/project-mcp-trust-queries.ts +43 -0
  14. package/backend/engine/adapters/claude/message-converter.ts +6 -2
  15. package/backend/engine/adapters/claude/stream.ts +36 -8
  16. package/backend/engine/adapters/cline/mcp-tools.ts +26 -4
  17. package/backend/engine/adapters/cline/stream.ts +20 -6
  18. package/backend/engine/adapters/codex/stream.ts +21 -4
  19. package/backend/engine/adapters/copilot/stream.ts +6 -3
  20. package/backend/engine/adapters/cursor/stream.ts +19 -8
  21. package/backend/engine/adapters/opencode/server.ts +14 -4
  22. package/backend/engine/adapters/opencode/stream.ts +18 -3
  23. package/backend/engine/adapters/pi/mcp-tools.ts +26 -4
  24. package/backend/engine/adapters/pi/stream.ts +35 -3
  25. package/backend/engine/adapters/qwen/message-converter.ts +2 -2
  26. package/backend/engine/adapters/qwen/stream.ts +8 -0
  27. package/backend/engine/artifact-sync.ts +12 -5
  28. package/backend/engine/docs/lessons-learned.md +118 -0
  29. package/backend/files/file-watcher.test.ts +77 -0
  30. package/backend/files/file-watcher.ts +92 -60
  31. package/backend/files/watch-ignore.test.ts +182 -0
  32. package/backend/files/watch-ignore.ts +244 -0
  33. package/backend/git/git-executor.ts +14 -4
  34. package/backend/index.ts +11 -1
  35. package/backend/instructions/service.ts +5 -3
  36. package/backend/instructions/sync.ts +10 -27
  37. package/backend/mcp/external/config.ts +65 -23
  38. package/backend/mcp/external/project.ts +124 -0
  39. package/backend/mcp/external/proxy.ts +65 -11
  40. package/backend/mcp/index.ts +14 -9
  41. package/backend/mcp/internal/remote-server.ts +11 -1
  42. package/backend/skills/marketplace.ts +1 -1
  43. package/backend/snapshot/blob-store.ts +9 -5
  44. package/backend/snapshot/git-attribution.test.ts +42 -0
  45. package/backend/snapshot/git-attribution.ts +306 -0
  46. package/backend/snapshot/gitignore.test.ts +62 -0
  47. package/backend/snapshot/gitignore.ts +158 -78
  48. package/backend/snapshot/snapshot-service.test.ts +270 -0
  49. package/backend/snapshot/snapshot-service.ts +412 -136
  50. package/backend/snapshot/turn-activity.test.ts +52 -0
  51. package/backend/snapshot/turn-activity.ts +83 -0
  52. package/backend/snapshot/turn-changes.test.ts +58 -1
  53. package/backend/snapshot/turn-changes.ts +66 -0
  54. package/backend/worktrees/tree.ts +1 -1
  55. package/backend/ws/chat/composer.ts +504 -0
  56. package/backend/ws/chat/index.ts +3 -1
  57. package/backend/ws/chat/stream.ts +252 -440
  58. package/backend/ws/index.ts +4 -0
  59. package/backend/ws/project-artifacts/index.ts +236 -0
  60. package/backend/ws/snapshot/changes.ts +66 -31
  61. package/dist/assets/{index-BeUKiRiY.js → index-Am-rJRxA.js} +1 -1
  62. package/dist/assets/{index-B6u8cait.js → index-D-Ad1N96.js} +1 -1
  63. package/dist/assets/main--PLCDxSy.css +1 -0
  64. package/dist/assets/main-DelPm2Xt.js +1239 -0
  65. package/dist/index.html +2 -2
  66. package/package.json +5 -5
  67. package/shared/types/stores/settings.ts +5 -0
  68. package/shared/types/unified/index.ts +2 -0
  69. package/shared/types/unified/message.ts +2 -0
  70. package/shared/types/unified/stream.ts +38 -1
  71. package/dist/assets/main-CMRXlv2f.js +0 -1215
  72. package/dist/assets/main-DzCY0n-j.css +0 -1
@@ -0,0 +1,52 @@
1
+ /**
2
+ * When a turn's tools were running.
3
+ *
4
+ * The window must open at the engine's previous output, not at the call: an
5
+ * engine that reports a command only after it finished sends the call and the
6
+ * result together, and the command ran in the silence before them.
7
+ */
8
+
9
+ import { describe, it, expect } from 'bun:test';
10
+ import { TurnActivity } from './turn-activity';
11
+
12
+ const call = (id: string) => ({ type: 'assistant', content: [{ type: 'tool_use', id }] });
13
+ const result = (id: string) => ({ type: 'user', content: [{ type: 'tool_result', toolUseId: id }] });
14
+
15
+ describe('TurnActivity', () => {
16
+ it('covers the span from the previous output to the result', () => {
17
+ const activity = new TurnActivity(0);
18
+ activity.observe({ type: 'stream_event' }, 10_000);
19
+ activity.observe(call('a'), 20_000);
20
+ activity.observe(result('a'), 30_000);
21
+
22
+ expect(activity.wasRunningAt(15_000)).toBe(true);
23
+ expect(activity.wasRunningAt(30_500)).toBe(true);
24
+ expect(activity.wasRunningAt(5_000)).toBe(false);
25
+ expect(activity.wasRunningAt(40_000)).toBe(false);
26
+ });
27
+
28
+ it('covers a command reported only after it finished', () => {
29
+ const activity = new TurnActivity(0);
30
+ activity.observe({ type: 'assistant', content: [{ type: 'text' }] }, 10_000);
31
+ activity.observe(call('cmd'), 50_000);
32
+ activity.observe(result('cmd'), 50_001);
33
+
34
+ expect(activity.wasRunningAt(30_000)).toBe(true);
35
+ });
36
+
37
+ it('treats a call without a result as still running', () => {
38
+ const activity = new TurnActivity(0);
39
+ activity.observe({ type: 'stream_event' }, 10_000);
40
+ activity.observe(call('stuck'), 11_000);
41
+
42
+ expect(activity.wasRunningAt(99_000)).toBe(true);
43
+ });
44
+
45
+ it('does not count model output with no tool as running', () => {
46
+ const activity = new TurnActivity(0);
47
+ activity.observe({ type: 'assistant', content: [{ type: 'text' }] }, 10_000);
48
+ activity.observe({ type: 'stream_event' }, 20_000);
49
+
50
+ expect(activity.wasRunningAt(15_000)).toBe(false);
51
+ });
52
+ });
@@ -0,0 +1,83 @@
1
+ /**
2
+ * When a turn's tools were running.
3
+ *
4
+ * The disk says WHAT changed (turn-changes.ts). It cannot say which chat
5
+ * changed it, and that only matters when two chats run in the same folder at
6
+ * once and both see the same file move. Naming files per tool could never
7
+ * settle it: a shell command, a build script, an MCP tool or a tool that does
8
+ * not exist yet writes files no tool input names.
9
+ *
10
+ * Time can. Every tool call, whatever it is, runs between its call and its
11
+ * result, and a file written by it carries a modification time inside that
12
+ * span. So the question "which chat wrote this" becomes "whose tool was
13
+ * running when it was written" — one rule for every tool.
14
+ *
15
+ * A window opens at the engine's LAST output before the call, not at the call
16
+ * itself. Some engines (Codex) only report a command once it has finished, so
17
+ * the call and its result arrive together; the silence before them is when
18
+ * the command actually ran.
19
+ */
20
+
21
+ /** Filesystem timestamps are coarse on some volumes; a write this close counts. */
22
+ const WINDOW_SLACK_MS = 1000;
23
+
24
+ interface ContentBlock {
25
+ type?: string;
26
+ id?: string;
27
+ toolUseId?: string;
28
+ }
29
+
30
+ /** The part of an engine output this reads: its type and content blocks. */
31
+ export interface ObservedOutput {
32
+ type: string;
33
+ content?: unknown;
34
+ }
35
+
36
+ function blocksOf(output: ObservedOutput): ContentBlock[] {
37
+ return Array.isArray(output.content) ? (output.content as ContentBlock[]) : [];
38
+ }
39
+
40
+ export class TurnActivity {
41
+ private lastOutputAt: number;
42
+ private readonly open = new Map<string, number>();
43
+ private readonly windows: Array<{ start: number; end: number }> = [];
44
+
45
+ constructor(startedAt: number) {
46
+ this.lastOutputAt = startedAt;
47
+ }
48
+
49
+ /** Feed every engine output of the turn, in order. */
50
+ observe(output: ObservedOutput, now = Date.now()): void {
51
+ if (output.type === 'assistant') {
52
+ for (const block of blocksOf(output)) {
53
+ if (block.type === 'tool_use' && block.id && !this.open.has(block.id)) {
54
+ this.open.set(block.id, this.lastOutputAt);
55
+ }
56
+ }
57
+ } else if (output.type === 'user') {
58
+ for (const block of blocksOf(output)) {
59
+ if (block.type !== 'tool_result' || !block.toolUseId) continue;
60
+ const start = this.open.get(block.toolUseId);
61
+ if (start === undefined) continue;
62
+ this.windows.push({ start, end: now });
63
+ this.open.delete(block.toolUseId);
64
+ }
65
+ }
66
+ this.lastOutputAt = now;
67
+ }
68
+
69
+ /**
70
+ * Whether one of this turn's tools was running at `time` (epoch ms). A call
71
+ * still waiting for its result counts as running — an interrupted tool
72
+ * never sends one.
73
+ */
74
+ wasRunningAt(time: number): boolean {
75
+ for (const window of this.windows) {
76
+ if (time >= window.start - WINDOW_SLACK_MS && time <= window.end + WINDOW_SLACK_MS) return true;
77
+ }
78
+ for (const start of this.open.values()) {
79
+ if (time >= start - WINDOW_SLACK_MS) return true;
80
+ }
81
+ return false;
82
+ }
83
+ }
@@ -16,7 +16,9 @@ import {
16
16
  statusOf,
17
17
  isBinaryBuffer,
18
18
  summariseTurnFiles,
19
- excerptPrompt
19
+ excerptPrompt,
20
+ netChangeRuns,
21
+ summariseNetChanges
20
22
  } from './turn-changes';
21
23
 
22
24
  /** Blob reader over an in-memory store, so the tests own their failure modes. */
@@ -161,3 +163,58 @@ describe('excerptPrompt', () => {
161
163
  expect(excerptPrompt(' ', 10)).toBe('');
162
164
  });
163
165
  });
166
+
167
+ describe('netChangeRuns', () => {
168
+ it('folds a file edited in several turns into one change', () => {
169
+ const runs = netChangeRuns([
170
+ { 'a.ts': { oldHash: 'v1', newHash: 'v2' } },
171
+ { 'a.ts': { oldHash: 'v2', newHash: 'v3' } }
172
+ ]);
173
+ expect(runs.get('a.ts')).toEqual([{ oldHash: 'v1', newHash: 'v3' }]);
174
+ });
175
+
176
+ it('drops a file a later turn put back the way it was', () => {
177
+ const runs = netChangeRuns([
178
+ { 'tmp.ts': { oldHash: '', newHash: 'x' }, 'b.ts': { oldHash: 'b1', newHash: 'b2' } },
179
+ { 'tmp.ts': { oldHash: 'x', newHash: '' }, 'b.ts': { oldHash: 'b2', newHash: 'b1' } }
180
+ ]);
181
+ expect(runs.size).toBe(0);
182
+ });
183
+
184
+ it('breaks the run where something else changed the file between turns', () => {
185
+ const runs = netChangeRuns([
186
+ { 'a.ts': { oldHash: 'v1', newHash: 'v2' } },
187
+ { 'a.ts': { oldHash: 'user-edit', newHash: 'v3' } }
188
+ ]);
189
+ expect(runs.get('a.ts')).toEqual([
190
+ { oldHash: 'v1', newHash: 'v2' },
191
+ { oldHash: 'user-edit', newHash: 'v3' }
192
+ ]);
193
+ });
194
+ });
195
+
196
+ describe('summariseNetChanges', () => {
197
+ it('counts lines once for a file touched by many turns', async () => {
198
+ const read = readerFor({ v1: 'a\n', v2: 'a\nb\n', v3: 'a\nb\nc\n' });
199
+ const net = await summariseNetChanges(
200
+ [
201
+ { 'a.ts': { oldHash: 'v1', newHash: 'v2' } },
202
+ { 'a.ts': { oldHash: 'v2', newHash: 'v3' } }
203
+ ],
204
+ read
205
+ );
206
+ expect(net).toEqual({ filesChanged: 1, insertions: 2, deletions: 0 });
207
+ });
208
+
209
+ it('adds up each run of a broken history, without the gap between them', async () => {
210
+ const read = readerFor({ v1: 'a\n', v2: 'a\nb\n', u: 'x\n', v3: 'x\ny\n' });
211
+ const net = await summariseNetChanges(
212
+ [
213
+ { 'a.ts': { oldHash: 'v1', newHash: 'v2' } },
214
+ { 'a.ts': { oldHash: 'u', newHash: 'v3' } }
215
+ ],
216
+ read
217
+ );
218
+ expect(net).toEqual({ filesChanged: 1, insertions: 2, deletions: 0 });
219
+ });
220
+ });
@@ -130,3 +130,69 @@ export function summariseTurnFiles(files: TurnFileChange[]): {
130
130
  }
131
131
  return { filesChanged: files.length, insertions, deletions };
132
132
  }
133
+
134
+ /** One uninterrupted stretch of a file's history across consecutive turns. */
135
+ export interface ChangeRun {
136
+ oldHash: string;
137
+ newHash: string;
138
+ }
139
+
140
+ /**
141
+ * Fold a chat's turns, oldest first, into each file's net change.
142
+ *
143
+ * Adding turns up is what the chat-level counter used to do, and it answered
144
+ * a different question: a file edited in three turns had its lines counted
145
+ * three times, and a file created then deleted again still counted as
146
+ * changed. What a user means by "this chat changed N files" is the net, as
147
+ * `git diff` from before the first turn would show it.
148
+ *
149
+ * A turn continues a run only when it starts where the previous one ended. A
150
+ * gap means something else touched the file between turns (the user, git, a
151
+ * different chat) and that edit is not this chat's to report — so the run
152
+ * breaks there instead of silently spanning it.
153
+ */
154
+ export function netChangeRuns(turns: SessionScopedChanges[]): Map<string, ChangeRun[]> {
155
+ const runs = new Map<string, ChangeRun[]>();
156
+ for (const changes of turns) {
157
+ for (const [path, change] of Object.entries(changes)) {
158
+ const list = runs.get(path) ?? [];
159
+ const last = list[list.length - 1];
160
+ if (last && last.newHash === change.oldHash) {
161
+ last.newHash = change.newHash;
162
+ } else {
163
+ list.push({ oldHash: change.oldHash, newHash: change.newHash });
164
+ }
165
+ runs.set(path, list);
166
+ }
167
+ }
168
+
169
+ for (const [path, list] of runs) {
170
+ const effective = list.filter((run) => run.oldHash !== run.newHash);
171
+ if (effective.length === 0) runs.delete(path);
172
+ else runs.set(path, effective);
173
+ }
174
+ return runs;
175
+ }
176
+
177
+ /** The chat-level numbers: net files changed, and net lines in and out. */
178
+ export async function summariseNetChanges(
179
+ turns: SessionScopedChanges[],
180
+ readBlob: (hash: string) => Promise<Buffer>
181
+ ): Promise<{ filesChanged: number; insertions: number; deletions: number }> {
182
+ let insertions = 0;
183
+ let deletions = 0;
184
+ const runs = netChangeRuns(turns);
185
+
186
+ for (const [path, list] of runs) {
187
+ const changes: SessionScopedChanges = {};
188
+ // Each run is counted on its own; keyed apart so buildTurnFiles keeps them.
189
+ list.forEach((run, index) => {
190
+ changes[`${index}\0${path}`] = run;
191
+ });
192
+ const summary = summariseTurnFiles(await buildTurnFiles(changes, readBlob));
193
+ insertions += summary.insertions;
194
+ deletions += summary.deletions;
195
+ }
196
+
197
+ return { filesChanged: runs.size, insertions, deletions };
198
+ }
@@ -32,7 +32,7 @@ export async function hashTree(root: string, storeBlobs = false): Promise<TreeMa
32
32
  if (stat.size > MAX_TRACKED_FILE_SIZE) continue;
33
33
 
34
34
  const relativePath = path.relative(root, absolute).replace(/\\/g, '/');
35
- const result = await blobStore.hashFile(relativePath, absolute);
35
+ const result = await blobStore.hashFile(absolute);
36
36
  tree[relativePath] = result.hash;
37
37
 
38
38
  if (storeBlobs && !(await blobStore.hasBlob(result.hash))) {