@myrialabs/clopen 0.4.37 → 0.4.39

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 (193) hide show
  1. package/README.md +2 -2
  2. package/backend/auth/permissions.ts +39 -0
  3. package/backend/bootstrap.ts +16 -0
  4. package/backend/chat/stream-manager.ts +1 -1
  5. package/backend/database/crypto/envelope.test.ts +93 -0
  6. package/backend/database/crypto/envelope.ts +127 -0
  7. package/backend/database/crypto/index.ts +22 -0
  8. package/backend/database/crypto/master-key.ts +129 -0
  9. package/backend/database/crypto/secret-columns.ts +151 -0
  10. package/backend/database/migrations/071_create_notes_tables.ts +90 -0
  11. package/backend/database/migrations/072_encrypt_stored_secrets.ts +107 -0
  12. package/backend/database/migrations/073_create_integration_tables.ts +119 -0
  13. package/backend/database/migrations/074_create_work_tables.ts +94 -0
  14. package/backend/database/migrations/075_create_deploy_bindings.ts +54 -0
  15. package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
  16. package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
  17. package/backend/database/migrations/index.ts +49 -0
  18. package/backend/database/queries/db-client-connection-queries.ts +42 -12
  19. package/backend/database/queries/deploy-queries.ts +124 -0
  20. package/backend/database/queries/engine-queries.ts +23 -10
  21. package/backend/database/queries/graph-queries.ts +291 -247
  22. package/backend/database/queries/index.ts +10 -1
  23. package/backend/database/queries/integration-db-link-queries.ts +168 -0
  24. package/backend/database/queries/integration-queries.ts +325 -0
  25. package/backend/database/queries/mcp-server-queries.ts +38 -11
  26. package/backend/database/queries/note-queries.test.ts +190 -0
  27. package/backend/database/queries/note-queries.ts +269 -0
  28. package/backend/database/queries/profile-queries.ts +1 -1
  29. package/backend/database/queries/ssh-connection-queries.ts +19 -8
  30. package/backend/database/queries/work-queries.ts +210 -0
  31. package/backend/db-client/integrations/index.ts +37 -0
  32. package/backend/db-client/integrations/links.ts +533 -0
  33. package/backend/db-client/integrations/projector.test.ts +280 -0
  34. package/backend/db-client/integrations/projector.ts +256 -0
  35. package/backend/db-client/integrations/supabase-service.ts +266 -0
  36. package/backend/db-client/providers/registry.ts +27 -0
  37. package/backend/db-client/providers/supabase/adapter.ts +500 -0
  38. package/backend/db-client/providers/supabase/api-types.ts +92 -0
  39. package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
  40. package/backend/db-client/providers/supabase/client.ts +259 -0
  41. package/backend/db-client/providers/supabase/features.ts +502 -0
  42. package/backend/db-client/providers/supabase/local.test.ts +78 -0
  43. package/backend/db-client/providers/supabase/local.ts +137 -0
  44. package/backend/db-client/providers/types.ts +137 -0
  45. package/backend/deployments/bindings.test.ts +43 -0
  46. package/backend/deployments/bindings.ts +216 -0
  47. package/backend/deployments/index.ts +53 -0
  48. package/backend/deployments/log-streams.ts +214 -0
  49. package/backend/deployments/prompts.ts +70 -0
  50. package/backend/deployments/providers/vercel/adapter.ts +757 -0
  51. package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
  52. package/backend/deployments/providers/vercel/build-logs.ts +211 -0
  53. package/backend/deployments/providers/vercel/client.ts +203 -0
  54. package/backend/deployments/providers/vercel/list.test.ts +624 -0
  55. package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
  56. package/backend/deployments/providers/vercel/normalise.ts +113 -0
  57. package/backend/deployments/providers/vercel/types.ts +104 -0
  58. package/backend/deployments/registry.ts +27 -0
  59. package/backend/deployments/service.ts +458 -0
  60. package/backend/deployments/types.ts +278 -0
  61. package/backend/engine/docs/artifacts.md +22 -9
  62. package/backend/files/os-clipboard.test.ts +209 -0
  63. package/backend/files/os-clipboard.ts +567 -0
  64. package/backend/http/integration-hooks.ts +42 -0
  65. package/backend/http/notes-images.test.ts +228 -0
  66. package/backend/http/notes-images.ts +196 -0
  67. package/backend/index.ts +13 -0
  68. package/backend/integrations/accounts.ts +229 -0
  69. package/backend/integrations/health.ts +146 -0
  70. package/backend/integrations/index.ts +36 -0
  71. package/backend/integrations/projections/index.ts +0 -0
  72. package/backend/integrations/projections/mcp-projector.test.ts +204 -0
  73. package/backend/integrations/projections/mcp-projector.ts +163 -0
  74. package/backend/integrations/projections/types.ts +63 -0
  75. package/backend/integrations/registry/define.ts +38 -0
  76. package/backend/integrations/registry/index.ts +60 -0
  77. package/backend/integrations/registry/providers/context7.ts +44 -0
  78. package/backend/integrations/registry/providers/firecrawl.ts +41 -0
  79. package/backend/integrations/registry/providers/github.ts +78 -0
  80. package/backend/integrations/registry/providers/supabase.ts +61 -0
  81. package/backend/integrations/registry/providers/vercel.ts +46 -0
  82. package/backend/integrations/registry/types.ts +96 -0
  83. package/backend/integrations/webhooks/gateway.ts +160 -0
  84. package/backend/integrations/webhooks/verify.ts +65 -0
  85. package/backend/mcp/README.md +25 -3
  86. package/backend/mcp/external/config.ts +35 -1
  87. package/backend/mcp/index.ts +1 -1
  88. package/backend/mcp/internal/config.ts +1 -1
  89. package/backend/mcp/internal/servers/memory-graph/description.ts +3 -3
  90. package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
  91. package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
  92. package/backend/memory/README.md +57 -34
  93. package/backend/memory/compose.test.ts +3 -3
  94. package/backend/memory/compose.ts +6 -9
  95. package/backend/memory/config.ts +0 -5
  96. package/backend/memory/consolidate.ts +1 -2
  97. package/backend/memory/context.test.ts +8 -54
  98. package/backend/memory/context.ts +19 -25
  99. package/backend/memory/extract/episodic.ts +10 -12
  100. package/backend/memory/extract/index.ts +23 -52
  101. package/backend/memory/extract/scheduler.ts +3 -6
  102. package/backend/memory/index.ts +6 -5
  103. package/backend/memory/invalidate.ts +36 -48
  104. package/backend/memory/layout.ts +319 -130
  105. package/backend/memory/retention.ts +12 -42
  106. package/backend/memory/retrieval.eval.test.ts +2 -2
  107. package/backend/memory/retrieval.test.ts +228 -183
  108. package/backend/memory/retrieval.ts +22 -21
  109. package/backend/memory/revise.test.ts +62 -116
  110. package/backend/memory/revise.ts +0 -1
  111. package/backend/memory/view.ts +8 -5
  112. package/backend/notes/access.test.ts +82 -0
  113. package/backend/notes/access.ts +27 -0
  114. package/backend/notes/storage.test.ts +77 -0
  115. package/backend/notes/storage.ts +114 -0
  116. package/backend/permissions/service.ts +2 -2
  117. package/backend/profiles/service.ts +1 -1
  118. package/backend/projects/project-cleanup.ts +6 -0
  119. package/backend/snapshot/helpers.ts +35 -41
  120. package/backend/snapshot/snapshot-service.ts +90 -0
  121. package/backend/snapshot/turn-changes.test.ts +163 -0
  122. package/backend/snapshot/turn-changes.ts +131 -0
  123. package/backend/work/bindings.ts +124 -0
  124. package/backend/work/index.ts +42 -0
  125. package/backend/work/prompts.ts +150 -0
  126. package/backend/work/providers/github/actions-logs.ts +180 -0
  127. package/backend/work/providers/github/adapter.test.ts +88 -0
  128. package/backend/work/providers/github/adapter.ts +1255 -0
  129. package/backend/work/providers/github/client.ts +280 -0
  130. package/backend/work/providers/github/list-range.test.ts +158 -0
  131. package/backend/work/providers/github/token-kind.test.ts +42 -0
  132. package/backend/work/pull-requests.ts +242 -0
  133. package/backend/work/registry.ts +27 -0
  134. package/backend/work/service.ts +436 -0
  135. package/backend/work/start-work.test.ts +71 -0
  136. package/backend/work/start-work.ts +232 -0
  137. package/backend/work/types.ts +180 -0
  138. package/backend/ws/db-client/accounts.ts +225 -0
  139. package/backend/ws/db-client/connections.ts +34 -3
  140. package/backend/ws/db-client/context.ts +37 -0
  141. package/backend/ws/db-client/index.ts +20 -1
  142. package/backend/ws/db-client/supabase.ts +188 -0
  143. package/backend/ws/deployments/actions.ts +360 -0
  144. package/backend/ws/deployments/context.ts +43 -0
  145. package/backend/ws/deployments/crud.ts +152 -0
  146. package/backend/ws/deployments/index.ts +15 -0
  147. package/backend/ws/deployments/schemas.ts +183 -0
  148. package/backend/ws/files/clipboard.ts +78 -0
  149. package/backend/ws/files/index.ts +5 -0
  150. package/backend/ws/git/diff-budget.ts +70 -11
  151. package/backend/ws/index.ts +16 -2
  152. package/backend/ws/integrations/crud.ts +219 -0
  153. package/backend/ws/integrations/index.ts +22 -0
  154. package/backend/ws/mcp/crud.ts +25 -1
  155. package/backend/ws/memory/crud.ts +4 -16
  156. package/backend/ws/notes/index.ts +278 -0
  157. package/backend/ws/profiles/crud.ts +1 -1
  158. package/backend/ws/snapshot/changes.ts +279 -0
  159. package/backend/ws/snapshot/index.ts +4 -3
  160. package/backend/ws/work/actions.ts +206 -0
  161. package/backend/ws/work/context.ts +51 -0
  162. package/backend/ws/work/crud.ts +347 -0
  163. package/backend/ws/work/index.ts +17 -0
  164. package/backend/ws/work/pull-requests.ts +177 -0
  165. package/backend/ws/work/schemas.ts +226 -0
  166. package/dist/assets/{index-sJmS_CCm.js → index-BrF6nqld.js} +1 -1
  167. package/dist/assets/{index-Bik9TCBi.js → index-C8Gi-b6U.js} +1 -1
  168. package/dist/assets/main-Bd3gb79F.css +1 -0
  169. package/dist/assets/main-Bo1hD7mt.js +1191 -0
  170. package/dist/index.html +2 -2
  171. package/package.json +1 -1
  172. package/shared/constants/engine-tools.ts +1 -1
  173. package/shared/constants/tool-icons.ts +42 -0
  174. package/shared/types/database/schema.ts +41 -0
  175. package/shared/types/db-client/connection.ts +6 -0
  176. package/shared/types/db-client/index.ts +2 -0
  177. package/shared/types/db-client/integrations.ts +174 -0
  178. package/shared/types/db-client/supabase.ts +131 -0
  179. package/shared/types/deployments.ts +382 -0
  180. package/shared/types/integrations.ts +140 -0
  181. package/shared/types/memory.ts +34 -51
  182. package/shared/types/stores/settings.ts +8 -0
  183. package/shared/types/ui/components.ts +1 -1
  184. package/shared/types/work.ts +426 -0
  185. package/shared/utils/diff-calculator.test.ts +89 -0
  186. package/shared/utils/diff-calculator.ts +30 -65
  187. package/shared/utils/logger.ts +4 -0
  188. package/backend/memory/extract/languages.ts +0 -176
  189. package/backend/memory/extract/structural.test.ts +0 -215
  190. package/backend/memory/extract/structural.ts +0 -364
  191. package/backend/ws/snapshot/content.ts +0 -85
  192. package/dist/assets/main-DLqolBI2.css +0 -1
  193. package/dist/assets/main-Df1I3hnw.js +0 -1096
@@ -36,9 +36,30 @@ import Graph from 'graphology';
36
36
  import louvain from 'graphology-communities-louvain';
37
37
  import forceAtlas2 from 'graphology-layout-forceatlas2';
38
38
  import { graphQueries, graphLayoutQueries, type GraphLayoutRow } from '$backend/database/queries/graph-queries';
39
+ import { settingsQueries } from '$backend/database/queries/settings-queries';
39
40
  import { broadcastGraphChanged } from './notify';
40
41
  import { debug } from '$shared/utils/logger';
41
42
 
43
+ /**
44
+ * What produced the stored arrangement. BUMP IT whenever that changes — either
45
+ * the edges this pass lays out, or how it places them.
46
+ *
47
+ * A pass is warm by default: every node is seeded at the position it already had
48
+ * and only lightly re-settled, which is what stops one new memory rearranging a
49
+ * map somebody has learned. That is exactly wrong after the pass itself changes,
50
+ * because the arrangement it would preserve was computed by different rules —
51
+ * when removing the code half took the store from 1,351 components to one, the
52
+ * surviving memories stayed in the shell those nine thousand nodes had shaped.
53
+ *
54
+ * So the marker is stored alongside the arrangement, and a mismatch drops the
55
+ * arrangement once. It costs one cold pass, which the store pays for anyway the
56
+ * first time it is laid out.
57
+ */
58
+ const LAYOUT_VERSION = '6';
59
+ const LAYOUT_VERSION_KEY = 'memory_layout_version';
60
+ /** The key this used before it covered placement as well as derivation. */
61
+ const LEGACY_LAYOUT_KEY = 'memory_layout_derivation';
62
+
42
63
  /**
43
64
  * Two caps, because the two halves of a pass scale differently.
44
65
  *
@@ -138,6 +159,16 @@ export async function runGraphLayout(): Promise<void> {
138
159
  return;
139
160
  }
140
161
 
162
+ // Before anything reads the stored arrangement: if a different version of
163
+ // this pass produced it, it is not an arrangement of this graph.
164
+ if (settingsQueries.get(LAYOUT_VERSION_KEY)?.value !== LAYOUT_VERSION) {
165
+ graphLayoutQueries.clear();
166
+ settingsQueries.set(LAYOUT_VERSION_KEY, LAYOUT_VERSION);
167
+ settingsQueries.delete(LEGACY_LAYOUT_KEY);
168
+ lastMembership = '';
169
+ debug.log('memory', 'Memory layout: the pass changed, arranging from scratch');
170
+ }
171
+
141
172
  const membership = membershipOf(ids);
142
173
  // Same nodes as last time and nothing waiting to be placed: the answer is
143
174
  // the one already in the table.
@@ -337,8 +368,9 @@ async function arrange(
337
368
  const anchor = existingCentroid(member, existing);
338
369
  seedComponent(part, communities, existing, anchor);
339
370
  await settle(part, cold || !anchor ? iterationsFor(part.order) : warmIterationsFor(part.order));
371
+ const lobes = spreadCommunities(part, communities);
340
372
 
341
- placed.push(measure(part, anchor));
373
+ placed.push(measure(part, anchor, lobes));
342
374
  }
343
375
 
344
376
  packComponents(placed);
@@ -356,10 +388,170 @@ async function arrange(
356
388
  }
357
389
  }
358
390
 
359
- for (const row of scatterSingletons(singletons, placed, communities, existing)) rows.push(row);
391
+ // The field is where the LOBES are. Lone nodes are dropped into it rather than
392
+ // onto a disc of their own, which is what stopped them deciding how far the
393
+ // map reaches — see `placeLoneNodes`.
394
+ const field: Disc[] = [];
395
+ for (const component of placed) {
396
+ for (const lobe of component.lobes) {
397
+ field.push({ x: lobe.x + component.offsetX, y: lobe.y + component.offsetY, radius: lobe.radius });
398
+ }
399
+ }
400
+
401
+ for (const row of placeLoneNodes(singletons, field, communities, existing)) rows.push(row);
360
402
  return rows;
361
403
  }
362
404
 
405
+ /**
406
+ * Push the lobes apart from each other, leaving each one's interior alone.
407
+ *
408
+ * The forces cannot do this on their own. Repulsion is between NODES, so it is
409
+ * strongest exactly where nodes are dense — inside a lobe — while the edges
410
+ * between two lobes pull them together with nothing pushing back at the same
411
+ * scale. A well-clustered graph therefore settles as one field of colour with
412
+ * its groups readable only by hue.
413
+ *
414
+ * Raising `scalingRatio` would not fix it: global repulsion inflates the lobes
415
+ * and the gaps together, so the picture gets bigger and reads the same.
416
+ *
417
+ * SEPARATION IS SOLVED, NOT SCALED, and the difference matters. Multiplying each
418
+ * lobe's distance from the centre — by one constant, or by a draw per lobe —
419
+ * cannot promise any particular lobe clear space: the constant preserves the
420
+ * silhouette and only inflates it, and the draw flings a few lobes far out while
421
+ * the rest stay exactly as crowded as they were. Measured, that second version
422
+ * put the whole store's extent in the hands of two outliers and left the other
423
+ * fifty-six in a clump.
424
+ *
425
+ * So each lobe is treated as a disc, and the discs are relaxed apart until none
426
+ * of them overlap — the same collision pass `packComponents` runs over
427
+ * components, one level down. Every lobe ends up with room, the field grows only
428
+ * as far as it has to, and `GAP` is the dial: clear space between two lobes as a
429
+ * fraction of the smaller one's radius.
430
+ *
431
+ * Each lobe moves RIGIDLY. Every node keeps its offset from its own lobe's
432
+ * centroid, so the interior the forces worked out is preserved exactly and only
433
+ * the distance between lobes changes. Deterministic, and it cannot destabilise a
434
+ * simulation that has already finished.
435
+ *
436
+ * Returns the discs, because they are the FIELD: what the map covers is where
437
+ * the lobes are, and the nodes that connect to nothing are placed inside it
438
+ * rather than on a disc of their own. See `placeLoneNodes`.
439
+ */
440
+ function spreadCommunities(part: Graph, communities: Record<string, number>): Disc[] {
441
+ /**
442
+ * Clear space between two lobes, as a fraction of the smaller one's radius.
443
+ * This is the "multiverse rather than one universe" dial. `packComponents`
444
+ * uses 0.15 between whole components; lobes want far more, because unlike
445
+ * components they are joined by edges that read as bridges.
446
+ */
447
+ const GAP = 0.9;
448
+ const PASSES = 240;
449
+ /**
450
+ * The percentile of member distances taken as a lobe's radius.
451
+ *
452
+ * Not the maximum: Louvain leaves the occasional member stretched toward a
453
+ * neighbouring lobe by one edge, and sizing the disc to that straggler would
454
+ * separate on a distance almost none of the lobe occupies.
455
+ */
456
+ const RADIUS_PERCENTILE = 0.9;
457
+
458
+ const members = new Map<number, string[]>();
459
+ part.forEachNode(id => {
460
+ const community = communities[id] ?? 0;
461
+ const list = members.get(community);
462
+ if (list) list.push(id);
463
+ else members.set(community, [id]);
464
+ });
465
+
466
+ interface Lobe extends Disc {
467
+ ids: string[];
468
+ originX: number;
469
+ originY: number;
470
+ }
471
+
472
+ const lobes: Lobe[] = [];
473
+ for (const [, ids] of [...members.entries()].sort((a, b) => a[0] - b[0])) {
474
+ let sumX = 0;
475
+ let sumY = 0;
476
+ for (const id of ids) {
477
+ sumX += part.getNodeAttribute(id, 'x') as number;
478
+ sumY += part.getNodeAttribute(id, 'y') as number;
479
+ }
480
+ const x = sumX / ids.length;
481
+ const y = sumY / ids.length;
482
+
483
+ const distances = ids
484
+ .map(id =>
485
+ Math.hypot((part.getNodeAttribute(id, 'x') as number) - x, (part.getNodeAttribute(id, 'y') as number) - y)
486
+ )
487
+ .sort((a, b) => a - b);
488
+ const radius = Math.max(distances[Math.floor(distances.length * RADIUS_PERCENTILE)] ?? 0, 20);
489
+
490
+ lobes.push({ ids, x, y, radius, originX: x, originY: y });
491
+ }
492
+
493
+ // One lobe has nothing to be spread away from, but it is still the field.
494
+ if (lobes.length < 2) return lobes.map(lobe => ({ x: lobe.x, y: lobe.y, radius: lobe.radius }));
495
+
496
+ for (let pass = 0; pass < PASSES; pass++) {
497
+ // Over-relaxed early and settling toward an exact correction, for the same
498
+ // reason `packComponents` does it: separating one pair nudges both into
499
+ // their other neighbours.
500
+ const relax = 1.4 - 0.4 * (pass / PASSES);
501
+ let collided = false;
502
+
503
+ for (let i = 0; i < lobes.length; i++) {
504
+ for (let j = i + 1; j < lobes.length; j++) {
505
+ const a = lobes[i];
506
+ const b = lobes[j];
507
+ const minimum = a.radius + b.radius + GAP * Math.min(a.radius, b.radius);
508
+ let dx = b.x - a.x;
509
+ let dy = b.y - a.y;
510
+ const squared = dx * dx + dy * dy;
511
+ if (squared >= minimum * minimum) continue;
512
+
513
+ let distance = Math.sqrt(squared);
514
+ if (distance < 1e-9) {
515
+ // Two centroids on the same point have no direction to separate
516
+ // along; take one from the ids, so it is still the same every pass.
517
+ const angle = jitterOf(a.ids[0] + b.ids[0]).angle;
518
+ dx = Math.cos(angle);
519
+ dy = Math.sin(angle);
520
+ distance = 1;
521
+ }
522
+
523
+ const push = (relax * (minimum - distance)) / distance / 2;
524
+ a.x -= dx * push;
525
+ a.y -= dy * push;
526
+ b.x += dx * push;
527
+ b.y += dy * push;
528
+ collided = true;
529
+ }
530
+ }
531
+
532
+ if (!collided) break;
533
+ }
534
+
535
+ for (const lobe of lobes) {
536
+ const shiftX = lobe.x - lobe.originX;
537
+ const shiftY = lobe.y - lobe.originY;
538
+ if (shiftX === 0 && shiftY === 0) continue;
539
+ for (const id of lobe.ids) {
540
+ part.setNodeAttribute(id, 'x', (part.getNodeAttribute(id, 'x') as number) + shiftX);
541
+ part.setNodeAttribute(id, 'y', (part.getNodeAttribute(id, 'y') as number) + shiftY);
542
+ }
543
+ }
544
+
545
+ return lobes.map(lobe => ({ x: lobe.x, y: lobe.y, radius: lobe.radius }));
546
+ }
547
+
548
+ /** A circle something occupies — a lobe, or a whole component. */
549
+ interface Disc {
550
+ x: number;
551
+ y: number;
552
+ radius: number;
553
+ }
554
+
363
555
  /** One component, laid out in local space and waiting to be given a place. */
364
556
  interface PlacedComponent {
365
557
  ids: string[];
@@ -371,6 +563,8 @@ interface PlacedComponent {
371
563
  offsetY: number;
372
564
  /** Where it already sat, when it was on the map before this pass. */
373
565
  anchored: boolean;
566
+ /** Its lobes, in the same local space as `xs`/`ys`. See `placeLoneNodes`. */
567
+ lobes: Disc[];
374
568
  }
375
569
 
376
570
  /**
@@ -567,7 +761,7 @@ function communityMetaSeed(
567
761
  }
568
762
 
569
763
  /** Centre a settled component on its own centroid and measure what it needs. */
570
- function measure(part: Graph, anchor: { x: number; y: number } | null): PlacedComponent {
764
+ function measure(part: Graph, anchor: { x: number; y: number } | null, lobes: Disc[]): PlacedComponent {
571
765
  const ids = part.nodes();
572
766
  const xs = new Float64Array(ids.length);
573
767
  const ys = new Float64Array(ids.length);
@@ -594,6 +788,9 @@ function measure(part: Graph, anchor: { x: number; y: number } | null): PlacedCo
594
788
  ids,
595
789
  xs,
596
790
  ys,
791
+ // Re-centred with the nodes, so a lobe keeps describing where its members
792
+ // actually are once the component is given a place.
793
+ lobes: lobes.map(lobe => ({ x: lobe.x - centreX, y: lobe.y - centreY, radius: lobe.radius })),
597
794
  radius: Math.max(radius, 20),
598
795
  // An anchored component keeps its old centroid; the drift the settle
599
796
  // introduced is absorbed by re-centring rather than accumulating.
@@ -606,32 +803,51 @@ function measure(part: Graph, anchor: { x: number; y: number } | null): PlacedCo
606
803
  /**
607
804
  * Give every unanchored component a place, and separate any that overlap.
608
805
  *
609
- * The outline this produces is the point. Components are scattered into a box
610
- * whose AREA follows the total they need and whose aspect is deliberately not
611
- * square, then pushed apart until they no longer collide — so the silhouette is
612
- * decided by how many components there are and how big each one is, both of which
613
- * are properties of the data. Every spiral, ring or fill-outward-from-a-point
614
- * strategy converges on a disc no matter how its regions are sized; that lesson
615
- * cost two attempts.
806
+ * A store this feature has been running in for any length of time is ONE mass
807
+ * and a handful of stragglers, so the placement is built around that rather than
808
+ * around components being comparable: the largest holds the origin and does not
809
+ * move, the others are put just outside it, and the relaxation pushes apart
810
+ * whatever still collides. The silhouette is then decided by how big the mass is
811
+ * and how many stragglers there are, both of which are properties of the data.
616
812
  *
617
813
  * Anchored components do not move: they are where the user last saw them.
618
814
  */
619
815
  function packComponents(placed: PlacedComponent[]): void {
620
816
  if (placed.length === 0) return;
621
817
 
622
- const total = placed.reduce((sum, component) => sum + Math.PI * component.radius ** 2, 0);
623
- /** Air, as a multiple of the area the components themselves occupy. */
624
- const BREATHING = 2.6;
625
- /** Wider than tall, so the default outline is not a square either. */
626
- const ASPECT = 1.45;
627
- const width = Math.sqrt(total * BREATHING * ASPECT);
628
- const height = width / ASPECT;
629
-
630
- for (const component of placed) {
631
- if (component.anchored) continue;
818
+ // THE LARGEST COMPONENT HOLDS THE ORIGIN, and the rest are placed around it.
819
+ //
820
+ // Both halves of that are corrections, and both came from the same measurement.
821
+ // Once a store has grown its components are not comparable: the measured graph
822
+ // is one of 2,036 memories, one of three, one of two, and twenty-six lone
823
+ // nodes. Scattering all of them into a box sized by TOTAL area — which is what
824
+ // this did — flung the giant half a box width, so it settled at (6960, 5095)
825
+ // and took the whole map off the origin with it, while every other placement
826
+ // in this file measures from the middle. It also threw the two tiny components
827
+ // tens of thousands of units out, which then set how far the map reached.
828
+ //
829
+ // So the biggest mass is pinned at the origin and marked as fixed, and the
830
+ // others are placed in a ring just outside it — near the thing they are small
831
+ // against, rather than lost in a box scaled to it. The relaxation below then
832
+ // resolves whatever still overlaps. `componentsOf` returns them largest first.
833
+ const movable = placed.filter(component => !component.anchored);
834
+ const anchorRadius = movable.length > 0 ? movable[0].radius : 0;
835
+ for (let i = 0; i < movable.length; i++) {
836
+ const component = movable[i];
837
+ if (i === 0) {
838
+ component.offsetX = 0;
839
+ component.offsetY = 0;
840
+ // Fixed for the relaxation too. Without this a three-node component
841
+ // landing on it pushes the mass — and the map — aside to make room.
842
+ component.anchored = true;
843
+ continue;
844
+ }
632
845
  const jitter = jitterOf(component.ids[0]);
633
- component.offsetX = (jitter.x * width) / 2;
634
- component.offsetY = (jitter.y * height) / 2;
846
+ // Deterministic angle, and a radius just clear of the mass with enough
847
+ // variation that the satellites do not draw a circle around it.
848
+ const distance = anchorRadius + component.radius * (1.4 + 0.9 * ((jitter.y + 1) / 2));
849
+ component.offsetX = Math.cos(jitter.angle) * distance;
850
+ component.offsetY = Math.sin(jitter.angle) * distance;
635
851
  }
636
852
 
637
853
  /** Clear space between two components, as a fraction of the smaller radius. */
@@ -683,20 +899,38 @@ function packComponents(placed: PlacedComponent[]): void {
683
899
  }
684
900
 
685
901
  /**
686
- * Place the nodes that connect to nothing, in the gaps between what does.
902
+ * Place the nodes that connect to nothing, inside the field the lobes occupy.
903
+ *
904
+ * These were the ring. With no edges there is no force to arrange them, so under
905
+ * the old global gravity they all settled at one radius and drew a circle around
906
+ * everything else. Two attempts to fix that each failed in their own way, and
907
+ * both failures are why this is shaped as it is.
908
+ *
909
+ * A GRID OF FREE CELLS over the bounding RECTANGLE of the components, filled
910
+ * from the middle out, worked while the graph was many small components with
911
+ * gaps between them. It stopped working the moment the store became one
912
+ * connected mass: there is no interior gap left, so every free cell was a corner
913
+ * or an edge of the rectangle, and the lone nodes lined up along two sides of a
914
+ * box.
915
+ *
916
+ * A DISC OF THEIR OWN, sized from the component's reach, put them off the box —
917
+ * and made them the largest thing on the map. Measured on a real store their
918
+ * span was 40,998 against the clusters' 26,065, so the view fitted to THEM and
919
+ * zoomed the clusters, which are the thing worth looking at, down into a clump
920
+ * in one corner. Nodes that connect to nothing were setting the scale of
921
+ * everything that does.
687
922
  *
688
- * These were the ring. A third of the marks on the measured store are lone
689
- * nodes, and with no edges there is no force to arrange them — so under the old
690
- * global gravity they all settled at one radius and drew a circle around
691
- * everything else. Filling the gaps instead removes the ring and puts them where
692
- * there is room, which is also where the eye is not already busy.
923
+ * So there is no second field. The lobes ARE the field: a lone node is drawn
924
+ * somewhere inside it — angle and radius from its own id, the radius
925
+ * square-rooted so the draws are uniform over the AREA rather than bunched at
926
+ * the centre — and pushed clear if it lands on a lobe. It fills the gaps between
927
+ * the islands, and it cannot make the map larger than the islands already do.
693
928
  *
694
- * The grid is coarse on purpose: it only has to answer "is anything here", and a
695
- * fine one would cost more than the question is worth.
929
+ * Deterministic: an id does not change, so neither does where its memory sits.
696
930
  */
697
- function scatterSingletons(
931
+ function placeLoneNodes(
698
932
  singletons: string[],
699
- placed: PlacedComponent[],
933
+ field: Disc[],
700
934
  communities: Record<string, number>,
701
935
  existing: Map<string, GraphLayoutRow>
702
936
  ): GraphLayoutRow[] {
@@ -708,10 +942,9 @@ function scatterSingletons(
708
942
  * One that was already on the map stays exactly where it was.
709
943
  *
710
944
  * Anchoring these matters more than anchoring components, and measurement is
711
- * what showed it: the free-cell grid is derived from where the components
712
- * ended up, so a single memory arriving anywhere changed one component's
713
- * radius, which changed the grid, which reassigned EVERY lone node. A warm
714
- * pass moved a hundred of them clear across the map — the one thing a
945
+ * what showed it: the old free-cell grid was derived from where the components
946
+ * ended up, so a single memory arriving anywhere reassigned EVERY lone node. A
947
+ * warm pass moved a hundred of them clear across the map — the one thing a
715
948
  * background pass must never do to a view somebody is reading.
716
949
  */
717
950
  const ordered: string[] = [];
@@ -731,117 +964,73 @@ function scatterSingletons(
731
964
  }
732
965
  if (ordered.length === 0) return rows;
733
966
 
734
- // Nothing else on the map — spread them over a box of their own rather than
735
- // piling them on the origin.
736
- if (placed.length === 0) {
967
+ // Nothing else on the map — a field of their own, which is the only case where
968
+ // they set the scale, because there is nothing else to set it.
969
+ if (field.length === 0) {
737
970
  const span = 120 * Math.sqrt(ordered.length);
738
971
  for (const id of ordered) {
739
- const jitter = jitterOf(id);
972
+ const draw = jitterOf(id);
973
+ const radius = span * Math.sqrt((draw.x + 1) / 2);
740
974
  rows.push({
741
975
  nodeId: id,
742
976
  community: communities[id] ?? 0,
743
- x: jitter.x * span,
744
- y: (jitter.y * span) / 1.45,
977
+ x: Math.cos(draw.angle) * radius,
978
+ y: Math.sin(draw.angle) * radius,
745
979
  placed: 1
746
980
  });
747
981
  }
748
982
  return rows;
749
983
  }
750
984
 
751
- let minX = Infinity;
752
- let maxX = -Infinity;
753
- let minY = Infinity;
754
- let maxY = -Infinity;
755
- for (const component of placed) {
756
- minX = Math.min(minX, component.offsetX - component.radius);
757
- maxX = Math.max(maxX, component.offsetX + component.radius);
758
- minY = Math.min(minY, component.offsetY - component.radius);
759
- maxY = Math.max(maxY, component.offsetY + component.radius);
985
+ // The field's own centre and reach, taken from the lobes rather than from the
986
+ // origin: a component is re-centred on its own centroid and then given a
987
+ // place, so the origin is not where the map is.
988
+ let centreX = 0;
989
+ let centreY = 0;
990
+ for (const lobe of field) {
991
+ centreX += lobe.x;
992
+ centreY += lobe.y;
760
993
  }
994
+ centreX /= field.length;
995
+ centreY /= field.length;
761
996
 
762
- // A thin margin, not a ring. At 12% this added a whole outer band of candidate
763
- // cells, and combined with spreading across every free cell it put the lone
764
- // nodes OUTSIDE the components — measured, they reached 9,790 wide where the
765
- // components reached 7,996, which is the ring reappearing one layer out. Just
766
- // enough air that a tightly packed store still has somewhere to put them.
767
- const margin = 0.04 * Math.max(maxX - minX, maxY - minY);
768
- minX -= margin;
769
- maxX += margin;
770
- minY -= margin;
771
- maxY += margin;
772
-
773
- // Roughly four candidate cells per node, which leaves enough free ones to
774
- // spread across after the occupied ones are struck out.
775
- const columns = Math.max(4, Math.round(Math.sqrt(ordered.length * 4 * 1.45)));
776
- const rowsCount = Math.max(4, Math.round(columns / 1.45));
777
- const cellWidth = (maxX - minX) / columns;
778
- const cellHeight = (maxY - minY) / rowsCount;
779
-
780
- const free: { x: number; y: number; distance: number }[] = [];
781
- for (let column = 0; column < columns; column++) {
782
- for (let row = 0; row < rowsCount; row++) {
783
- const x = minX + (column + 0.5) * cellWidth;
784
- const y = minY + (row + 0.5) * cellHeight;
785
-
786
- let occupied = false;
787
- for (const component of placed) {
788
- const dx = x - component.offsetX;
789
- const dy = y - component.offsetY;
790
- if (dx * dx + dy * dy < component.radius * component.radius) {
791
- occupied = true;
792
- break;
793
- }
794
- }
795
- // And the lone nodes already anchored here, so an arrival does not land
796
- // on top of one that has been sitting there since a previous pass.
797
- if (!occupied) {
798
- const reach = Math.min(cellWidth, cellHeight) * 0.5;
799
- for (const row of rows) {
800
- if (Math.abs(row.x - x) < reach && Math.abs(row.y - y) < reach) {
801
- occupied = true;
802
- break;
803
- }
804
- }
805
- }
806
- if (occupied) continue;
807
- free.push({ x, y, distance: Math.hypot(x, y) });
808
- }
997
+ let reach = 0;
998
+ for (const lobe of field) {
999
+ reach = Math.max(reach, Math.hypot(lobe.x - centreX, lobe.y - centreY) + lobe.radius);
809
1000
  }
810
1001
 
811
- // Nearest the middle first, so the gaps between the big components fill before
812
- // the empty edges do — which is what stops them re-forming a ring.
813
- free.sort((a, b) => a.distance - b.distance || a.x - b.x || a.y - b.y);
814
-
815
- for (let i = 0; i < ordered.length; i++) {
816
- const id = ordered[i];
817
- const jitter = jitterOf(id);
818
-
819
- if (free.length === 0) {
820
- rows.push({
821
- nodeId: id,
822
- community: communities[id] ?? 0,
823
- x: jitter.x * (maxX - minX) * 0.5,
824
- y: jitter.y * (maxY - minY) * 0.5,
825
- placed: 1
826
- });
827
- continue;
1002
+ /** Clear space kept around a lobe a lone node was drawn on top of. */
1003
+ const CLEARANCE = 0.12;
1004
+
1005
+ for (const id of ordered) {
1006
+ const draw = jitterOf(id);
1007
+ const radius = reach * Math.sqrt((draw.x + 1) / 2);
1008
+ let x = centreX + Math.cos(draw.angle) * radius;
1009
+ let y = centreY + Math.sin(draw.angle) * radius;
1010
+
1011
+ // Nudged off any lobe it landed on, along the line out of that lobe's
1012
+ // centre. A lone memory drawn on top of a cluster reads as a member of it,
1013
+ // which is the one thing it is not. Bounded, because pushing off one lobe
1014
+ // can land it on the next.
1015
+ for (let attempt = 0; attempt < 8; attempt++) {
1016
+ const hit = field.find(lobe => Math.hypot(x - lobe.x, y - lobe.y) < lobe.radius);
1017
+ if (!hit) break;
1018
+ let dx = x - hit.x;
1019
+ let dy = y - hit.y;
1020
+ let distance = Math.hypot(dx, dy);
1021
+ if (distance < 1e-9) {
1022
+ // Dead centre of a lobe has no direction to leave by; take one from the
1023
+ // id, so it is still the same on every pass.
1024
+ dx = Math.cos(draw.angle);
1025
+ dy = Math.sin(draw.angle);
1026
+ distance = 1;
1027
+ }
1028
+ const target = hit.radius * (1 + CLEARANCE);
1029
+ x = hit.x + (dx / distance) * target;
1030
+ y = hit.y + (dy / distance) * target;
828
1031
  }
829
1032
 
830
- // NEAREST free cells first while there are enough of them, so the lone nodes
831
- // fill the gaps between the components rather than reaching for the edge.
832
- // Spreading across the whole list unconditionally was what sent them to the
833
- // outermost cells even when the interior had room to spare.
834
- const cell =
835
- ordered.length <= free.length
836
- ? free[i]
837
- : free[Math.floor((i * free.length) / ordered.length)];
838
- rows.push({
839
- nodeId: id,
840
- community: communities[id] ?? 0,
841
- x: cell.x + jitter.x * cellWidth * 0.4,
842
- y: cell.y + jitter.y * cellHeight * 0.4,
843
- placed: 1
844
- });
1033
+ rows.push({ nodeId: id, community: communities[id] ?? 0, x, y, placed: 1 });
845
1034
  }
846
1035
 
847
1036
  return rows;
@@ -1,10 +1,9 @@
1
1
  /**
2
2
  * Retention — the bound on how large the graph may grow.
3
3
  *
4
- * The store has no natural bound. Every turn writes structural nodes for the
5
- * files it touched and episodic nodes for what it concluded, and without this
6
- * nothing would ever remove either, so the graph would grow linearly with the
7
- * number of turns the instance had ever served. That is fine for a week and
4
+ * The store has no natural bound. Every turn writes what it concluded, and
5
+ * without this nothing would ever remove any of it, so the graph would grow
6
+ * linearly with the number of turns the instance had ever served. That is fine for a week and
8
7
  * untenable for a year: the vector scan, the FTS index and the graph view all
9
8
  * degrade together, and the user's only recourse would be "Clear All Data".
10
9
  *
@@ -43,30 +42,15 @@ const EVICT_BELOW_CONFIDENCE = 0.45;
43
42
  /** How long an archived node is kept so it can still be restored. */
44
43
  const PURGE_ARCHIVED_AFTER_DAYS = 60;
45
44
 
46
- /**
47
- * How long an unreferenced symbol or module node survives without being touched.
48
- *
49
- * Longer than the episodic window on purpose, because a structural node costs
50
- * far less to be wrong about: re-observing a file re-creates its symbols on the
51
- * next turn that touches it, at microseconds and with no model involved. What is
52
- * being bounded here is not error, it is COUNT.
53
- */
54
- const PRUNE_STRUCTURAL_AFTER_DAYS = 120;
55
-
56
45
  /** Work done per pass, so maintenance never becomes a long stall. */
57
46
  const BATCH = 200;
58
47
 
59
- /** Structural rows removed per pass — a bigger batch, because it is a plain delete. */
60
- const STRUCTURAL_BATCH = 1_000;
61
-
62
48
  /** Queue entries that outlived any chance of being summarised usefully. */
63
49
  const QUEUE_MAX_AGE_DAYS = 14;
64
50
 
65
51
  export interface RetentionResult {
66
52
  evicted: number;
67
53
  purged: number;
68
- /** Symbol/module nodes no memory referred to, removed to bound growth. */
69
- structural: number;
70
54
  /** Extraction queue rows dropped as orphaned or stale. */
71
55
  queue: number;
72
56
  }
@@ -80,7 +64,7 @@ export interface RetentionResult {
80
64
  * separated by sixty days, before it is actually gone.
81
65
  */
82
66
  export function applyRetention(): RetentionResult {
83
- const result: RetentionResult = { evicted: 0, purged: 0, structural: 0, queue: 0 };
67
+ const result: RetentionResult = { evicted: 0, purged: 0, queue: 0 };
84
68
 
85
69
  try {
86
70
  const candidates = graphQueries.evictionCandidates({
@@ -92,31 +76,17 @@ export function applyRetention(): RetentionResult {
92
76
  result.evicted = graphQueries.archiveNodes(candidates.map(node => node.id));
93
77
  result.purged = graphQueries.purgeArchived(PURGE_ARCHIVED_AFTER_DAYS, BATCH);
94
78
 
95
- // The structural half is what actually grows without bound. Every turn
96
- // writes a node per changed file, per directory and up to twenty-five per
97
- // file's symbols, so on a repository under development it outgrows the
98
- // episodic half by an order of magnitude — and none of the queries above
99
- // look at `kind = 'structural'` at all. What is removed here is narrow by
100
- // construction: symbols and modules that nothing is `about`, untouched for
101
- // four months. A node any memory hangs off is never eligible, because
102
- // severing that edge would break the join both halves exist for.
103
- //
104
- // The vector cache is deliberately NOT reset here. Structural nodes are never
105
- // vector-indexed, and even a stale entry left by an older build can only
106
- // occupy a slot — candidates come from SQL, so nothing the cache no longer
107
- // has a row for is ever asked about. Reloading a quarter of a million vectors
108
- // on every retention tick to tidy that would be the expensive half of a
109
- // problem that does not exist.
110
- result.structural = graphQueries.pruneStructural({
111
- maxAgeDays: PRUNE_STRUCTURAL_AFTER_DAYS,
112
- limit: STRUCTURAL_BATCH
113
- });
114
- if (result.evicted > 0 || result.purged > 0 || result.structural > 0) {
79
+ // A pass that removed the codebase half used to run here too, and it was by
80
+ // far the largest term: a node per changed file, per directory and up to
81
+ // twenty-five per file's symbols, every turn. Migration 076 removed the half
82
+ // rather than the growth, so what is left to bound is the memories — which
83
+ // grow with what was CONCLUDED rather than with what was touched, and are
84
+ // therefore bounded by the work itself.
85
+ if (result.evicted > 0 || result.purged > 0) {
115
86
  notifyGraphChanged('retention');
116
87
  debug.log(
117
88
  'memory',
118
- `Retention: archived ${result.evicted}, removed ${result.purged} archived and ` +
119
- `${result.structural} unreferenced code node(s)`
89
+ `Retention: archived ${result.evicted}, removed ${result.purged} archived node(s)`
120
90
  );
121
91
  }
122
92
  } catch (error) {