@myrialabs/clopen 0.4.31 → 0.4.32

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 (52) hide show
  1. package/backend/auth/permissions.test.ts +0 -1
  2. package/backend/auth/permissions.ts +0 -4
  3. package/backend/chat/stream-manager.ts +27 -11
  4. package/backend/database/migrations/068_create_engine_config_revision.test.ts +121 -0
  5. package/backend/database/migrations/068_create_engine_config_revision.ts +117 -0
  6. package/backend/database/migrations/index.ts +7 -0
  7. package/backend/engine/README.md +16 -0
  8. package/backend/engine/adapters/claude/stream.ts +89 -49
  9. package/backend/engine/adapters/cline/stream.ts +62 -24
  10. package/backend/engine/adapters/codex/stream.ts +45 -18
  11. package/backend/engine/adapters/copilot/stream.ts +96 -52
  12. package/backend/engine/adapters/cursor/stream.ts +74 -34
  13. package/backend/engine/adapters/opencode/server.ts +511 -134
  14. package/backend/engine/adapters/opencode/stream.ts +131 -79
  15. package/backend/engine/adapters/pi/stream.ts +63 -24
  16. package/backend/engine/adapters/qwen/stream.ts +91 -58
  17. package/backend/engine/adapters/run-registry.test.ts +75 -0
  18. package/backend/engine/adapters/run-registry.ts +63 -0
  19. package/backend/engine/config-revision.ts +150 -0
  20. package/backend/engine/docs/adapter-contract.md +40 -9
  21. package/backend/engine/docs/adding-an-engine.md +8 -0
  22. package/backend/engine/docs/frontend-and-chat.md +39 -0
  23. package/backend/engine/docs/lessons-learned.md +70 -0
  24. package/backend/engine/docs/quick-reference.md +3 -1
  25. package/backend/engine/hot-reload.ts +99 -0
  26. package/backend/engine/index.ts +96 -24
  27. package/backend/engine/types.ts +17 -5
  28. package/backend/git/git-executor.ts +22 -3
  29. package/backend/git/git-service.ts +1 -1
  30. package/backend/git/nested-repos.test.ts +121 -0
  31. package/backend/git/nested-repos.ts +301 -0
  32. package/backend/index.ts +7 -0
  33. package/backend/snapshot/gitignore.ts +10 -174
  34. package/backend/ws/chat/stream.ts +24 -0
  35. package/backend/ws/engine/codex/accounts.ts +0 -12
  36. package/backend/ws/engine/copilot/accounts.ts +0 -10
  37. package/backend/ws/engine/cursor/accounts.ts +0 -10
  38. package/backend/ws/engine/index.ts +17 -2
  39. package/backend/ws/engine/opencode/providers.ts +0 -52
  40. package/backend/ws/files/read.ts +1 -1
  41. package/backend/ws/git/conflict.ts +1 -1
  42. package/backend/ws/git/diff.ts +1 -1
  43. package/backend/ws/git/staging.ts +1 -1
  44. package/backend/ws/git/status.ts +1 -1
  45. package/dist/assets/{index-DYwQ5bP8.js → index-DSvGoxp2.js} +1 -1
  46. package/dist/assets/{index-DUc5T21e.js → index-hu17G6tX.js} +1 -1
  47. package/dist/assets/main-BccclZsC.js +1031 -0
  48. package/dist/assets/{main-VgdjTBLR.css → main-Ciuw0wF0.css} +1 -1
  49. package/dist/index.html +2 -2
  50. package/package.json +1 -1
  51. package/backend/ws/engine/restart.ts +0 -54
  52. package/dist/assets/main-r0iv2PDz.js +0 -1044
@@ -35,6 +35,7 @@ import { getCursorMcpConfig } from '../../../mcp';
35
35
  import { subagentQueries } from '$backend/database/queries';
36
36
  import { readSubagentMd } from '$backend/subagents/store';
37
37
  import { buildJsonPrompt, extractJson } from '../../structured-helpers';
38
+ import { EngineRuns } from '../run-registry';
38
39
  import { getActiveCursorAccount, resolveCursorApiKey } from './credential';
39
40
  import { getCursorStore } from './environment';
40
41
  import { forkCursorAgent } from './session-fork';
@@ -97,16 +98,28 @@ function buildCursorModelSelection(modelId: string, reasoningEffort?: string): M
97
98
  return { id: modelId, params: [{ id: paramId, value }] };
98
99
  }
99
100
 
101
+ /**
102
+ * One stream in flight on this instance. This engine instance is shared by every
103
+ * chat session of a project, so the run and agent belong here, not in instance
104
+ * fields — those would hold only the most recently started stream, and
105
+ * cancelling any chat would then cancel another chat's Cursor run.
106
+ */
107
+ interface CursorRunState {
108
+ /** Identity of the run — the controller the caller passed to streamQuery. */
109
+ controller: AbortController;
110
+ run: Run | null;
111
+ agent: SDKAgent | null;
112
+ }
113
+
100
114
  export class CursorEngine implements AIEngine {
101
115
  readonly name = 'cursor' as const;
102
116
  private _isInitialized = false;
103
- private activeController: AbortController | null = null;
104
- private activeRun: Run | null = null;
105
- private activeAgent: SDKAgent | null = null;
106
- private pendingAsks = new Map<string, PendingAsk>();
117
+ private runs = new EngineRuns<CursorRunState>();
118
+ /** Parked AskUserQuestion callbacks → the run that asked. */
119
+ private pendingAsks = new Map<string, PendingAsk & { owner: CursorRunState }>();
107
120
 
108
121
  get isInitialized(): boolean { return this._isInitialized; }
109
- get isActive(): boolean { return this.activeController !== null; }
122
+ get isActive(): boolean { return this.runs.isActive; }
110
123
 
111
124
  async initialize(): Promise<void> {
112
125
  if (this._isInitialized) return;
@@ -115,7 +128,7 @@ export class CursorEngine implements AIEngine {
115
128
  }
116
129
 
117
130
  async dispose(): Promise<void> {
118
- await this.cancel();
131
+ await this.stopRuns(this.runs.all());
119
132
  this.pendingAsks.clear();
120
133
  this._isInitialized = false;
121
134
  }
@@ -153,7 +166,9 @@ export class CursorEngine implements AIEngine {
153
166
  }
154
167
  const apiKey = resolveCursorApiKey(account);
155
168
 
156
- this.activeController = abortController || new AbortController();
169
+ const controller = abortController || new AbortController();
170
+ const runState: CursorRunState = { controller, run: null, agent: null };
171
+ this.runs.add(runState);
157
172
  const resolvedProjectPath = resolveOsPath(projectPath);
158
173
 
159
174
  // ── Active Profile scoping + artifact materialization ──
@@ -168,7 +183,7 @@ export class CursorEngine implements AIEngine {
168
183
 
169
184
  // ── Tools: AskUserQuestion (in-process custom tool) + MCP (native config) ──
170
185
  const askTool: SDKCustomTool = createAskUserQuestionTool({
171
- register: (id, entry) => this.pendingAsks.set(id, entry),
186
+ register: (id, entry) => this.pendingAsks.set(id, { ...entry, owner: runState }),
172
187
  unregister: (id) => this.pendingAsks.delete(id),
173
188
  // Emit the tool_use with the COMPLETE questions from execute's args.
174
189
  emit: (id, questions) => {
@@ -220,11 +235,11 @@ export class CursorEngine implements AIEngine {
220
235
  ...(agents ? { agents } : {}),
221
236
  });
222
237
  } catch (error) {
223
- this.activeController = null;
238
+ this.runs.remove(runState);
224
239
  handleStreamError(error);
225
240
  return;
226
241
  }
227
- this.activeAgent = agent;
242
+ runState.agent = agent;
228
243
  const sessionId = agent.agentId;
229
244
 
230
245
  const converter = createCursorMessageConverter({ engine: engineMeta, sessionId });
@@ -260,11 +275,11 @@ export class CursorEngine implements AIEngine {
260
275
  }
261
276
  },
262
277
  });
263
- this.activeRun = run;
278
+ runState.run = run;
264
279
 
265
280
  // Abort wiring: cancelling the stream cancels the Cursor run.
266
281
  onAbort = () => { run.cancel().catch(() => { /* already terminal */ }); };
267
- this.activeController.signal.addEventListener('abort', onAbort, { once: true });
282
+ controller.signal.addEventListener('abort', onAbort, { once: true });
268
283
 
269
284
  // Drive the pull stream + result in the background, funnelling into the queue.
270
285
  const settle = (async () => {
@@ -287,38 +302,63 @@ export class CursorEngine implements AIEngine {
287
302
  } catch (error) {
288
303
  handleStreamError(error);
289
304
  } finally {
290
- if (onAbort) this.activeController?.signal.removeEventListener('abort', onAbort);
305
+ if (onAbort) controller.signal.removeEventListener('abort', onAbort);
291
306
  try { agent.close(); } catch { /* fire-and-forget */ }
292
- this.activeRun = null;
293
- this.activeAgent = null;
294
- this.activeController = null;
295
- this.pendingAsks.clear();
307
+ // Retire THIS run only — another chat session of the same project may
308
+ // still be streaming on this instance.
309
+ this.forgetRun(runState);
296
310
  }
297
311
  }
298
312
 
299
- async cancel(): Promise<void> {
300
- // Release any parked AskUserQuestion callbacks so a blocked run can settle.
301
- for (const [, pending] of this.pendingAsks) {
302
- pending.resolve('User did not answer the question.');
313
+ /** Drop a finished run and the asks only it could answer. */
314
+ private forgetRun(runState: CursorRunState): void {
315
+ this.runs.remove(runState);
316
+ for (const [id, pending] of this.pendingAsks) {
317
+ if (pending.owner === runState) this.pendingAsks.delete(id);
303
318
  }
304
- this.pendingAsks.clear();
319
+ runState.run = null;
320
+ runState.agent = null;
321
+ }
305
322
 
306
- // Abort the local controller first (cuts the for-await loop), then RPC.
307
- if (this.activeController && !this.activeController.signal.aborted) {
308
- this.activeController.abort();
309
- }
310
- this.activeController = null;
323
+ /**
324
+ * Cancel the run whose AbortController is `owner`, and only that run.
325
+ */
326
+ async cancel(owner: AbortController): Promise<void> {
327
+ await this.stopRuns(this.runs.select(owner));
328
+ }
329
+
330
+ /**
331
+ * Tear down the given runs. `cancel` passes the one run it was asked to
332
+ * stop; `dispose` passes them all. Nothing else may reach this.
333
+ */
334
+ private async stopRuns(targets: CursorRunState[]): Promise<void> {
335
+ for (const runState of targets) {
336
+ // Release the parked AskUserQuestion callbacks of THIS run so it can
337
+ // settle. Another run's parked ask is left alone — it is still live and
338
+ // still waiting for a real answer.
339
+ for (const [id, pending] of this.pendingAsks) {
340
+ if (pending.owner !== runState) continue;
341
+ pending.resolve('User did not answer the question.');
342
+ this.pendingAsks.delete(id);
343
+ }
344
+
345
+ // Abort the local controller first (cuts the for-await loop), then RPC.
346
+ if (!runState.controller.signal.aborted) runState.controller.abort();
311
347
 
312
- if (this.activeRun) {
313
- try { await this.activeRun.cancel(); } catch { /* may already be terminal */ }
348
+ const run = runState.run;
349
+ this.forgetRun(runState);
350
+ if (run) {
351
+ try { await run.cancel(); } catch { /* may already be terminal */ }
352
+ }
314
353
  }
315
- this.activeRun = null;
316
- this.activeAgent = null;
317
354
  }
318
355
 
319
- async interrupt(): Promise<void> {
320
- if (this.activeRun) {
321
- try { await this.activeRun.cancel(); } catch { /* ignore */ }
356
+ async interrupt(owner: AbortController): Promise<void> {
357
+ const targets = this.runs.select(owner);
358
+ for (const runState of targets) {
359
+ if (runState.run) {
360
+ try { await runState.run.cancel(); } catch { /* ignore */ }
361
+ }
322
362
  }
323
363
  }
324
364