dsh-memu-memory 1.4.3 → 1.4.5

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 (183) hide show
  1. package/AGENTS.md +3 -1
  2. package/CHANGELOG.md +15 -0
  3. package/README.md +34 -15
  4. package/README.zh.md +35 -13
  5. package/assets/screenshots/drawer.jpg +0 -0
  6. package/assets/screenshots/libraries.jpg +0 -0
  7. package/assets/screenshots/mobile.jpg +0 -0
  8. package/assets/screenshots/relations.jpg +0 -0
  9. package/assets/screenshots/settings.jpg +0 -0
  10. package/assets/screenshots/webui-dark.jpg +0 -0
  11. package/assets/screenshots/webui-memory.jpg +0 -0
  12. package/docs/client-conventions.md +19 -3
  13. package/docs/contributing.md +4 -2
  14. package/docs/env-and-commands.md +9 -1
  15. package/docs/graph-scale.md +214 -0
  16. package/docs/lexicon-recall.md +100 -0
  17. package/docs/mcp-porting.md +8 -6
  18. package/docs/memory-semantics.md +35 -3
  19. package/docs/pending-work.md +3 -1
  20. package/docs/retention.md +83 -0
  21. package/docs/rt-index.md +452 -0
  22. package/docs/scale-performance.md +101 -0
  23. package/docs/token-budget.md +92 -0
  24. package/docs/troubleshooting.md +50 -11
  25. package/lib/auto-link/apply.js +7 -2
  26. package/lib/auto-link/candidates.js +32 -10
  27. package/lib/backup.js +67 -32
  28. package/lib/binding-diagnostics.js +1 -1
  29. package/lib/bounded-cache.js +80 -0
  30. package/lib/client.js +2096 -409
  31. package/lib/consolidation-ledger.js +21 -4
  32. package/lib/consolidation-worker.js +14 -1
  33. package/lib/cycle.js +16 -11
  34. package/lib/diagnostic-context.js +372 -0
  35. package/lib/diagnostics.js +147 -61
  36. package/lib/embedding-download.js +7 -8
  37. package/lib/engine-sources.js +286 -0
  38. package/lib/engine.js +23 -10
  39. package/lib/entity-repair.js +43 -13
  40. package/lib/entity-review.js +64 -0
  41. package/lib/environment-check.js +348 -36
  42. package/lib/feedback.js +25 -0
  43. package/lib/global-graph.js +46 -2
  44. package/lib/graph-adjacency-index.js +525 -0
  45. package/lib/graph-overview.js +293 -0
  46. package/lib/graph.js +658 -105
  47. package/lib/inbound.js +44 -15
  48. package/lib/index-pages.js +57 -0
  49. package/lib/index.js +44 -6
  50. package/lib/inject.js +119 -50
  51. package/lib/lexicon-rt-source.js +25 -0
  52. package/lib/lexicon-rt-worker.js +26 -0
  53. package/lib/lexicon-rt.js +224 -0
  54. package/lib/lexicon-scope.js +56 -5
  55. package/lib/lexicon.js +143 -54
  56. package/lib/local-vector-service.js +3 -0
  57. package/lib/maintenance-ledger.js +542 -12
  58. package/lib/managed-runtime.js +260 -80
  59. package/lib/media.js +1 -1
  60. package/lib/memory.js +564 -131
  61. package/lib/port-surface.js +96 -11
  62. package/lib/preset-mount.js +13 -7
  63. package/lib/private.js +47 -21
  64. package/lib/recall-cache.js +20 -34
  65. package/lib/retention.js +173 -0
  66. package/lib/rpc.js +135 -52
  67. package/lib/rt-index/builder.js +209 -0
  68. package/lib/rt-index/format.js +231 -0
  69. package/lib/rt-index/manifest.js +105 -0
  70. package/lib/rt-index/reader.js +673 -0
  71. package/lib/rt-index-retention.js +151 -0
  72. package/lib/rt-index-service.js +283 -0
  73. package/lib/rt-index-worker.js +21 -0
  74. package/lib/salient-wake.js +55 -4
  75. package/lib/state.js +195 -24
  76. package/lib/temporal.js +39 -13
  77. package/lib/token-measurement.js +126 -0
  78. package/lib/tools.js +101 -6
  79. package/lib/turn-log.js +108 -3
  80. package/lib/webui/graph-layout-worker.js +68 -0
  81. package/lib/webui/graph-local-physics.js +390 -0
  82. package/lib/webui/graph-spatial-grid.js +64 -0
  83. package/lib/webui/graph-view.bundle.js +6 -1
  84. package/lib/webui/graph-view.js +478 -135
  85. package/lib/webui/graph-webgl-renderer.js +74 -0
  86. package/lib/webui/standalone.js +1 -1
  87. package/lib/worker-pool.js +38 -7
  88. package/package.json +3 -2
  89. package/scripts/acceptance-local.mjs +4 -10
  90. package/scripts/bench-all-libraries.mjs +47 -0
  91. package/scripts/check-classic-graph-browser.mjs +189 -0
  92. package/scripts/check-client-browser.mjs +929 -0
  93. package/scripts/check-graph-browser.mjs +6 -0
  94. package/scripts/check-graph-elastic-browser.mjs +139 -0
  95. package/scripts/check-memory-review-browser.mjs +124 -0
  96. package/scripts/entity-dedupe.mjs +19 -3
  97. package/scripts/graph-artifact-provenance.mjs +30 -0
  98. package/scripts/graph-freeze-manifest.mjs +129 -0
  99. package/scripts/graph-geometry-fixture.mjs +40 -0
  100. package/scripts/graph-physics-bench.mjs +218 -0
  101. package/scripts/graph-scale-bench.mjs +213 -0
  102. package/scripts/graph-scale-memory.mjs +48 -0
  103. package/scripts/graph-service-bench.mjs +72 -0
  104. package/scripts/make-readme-shots.mjs +17 -5
  105. package/scripts/preview.mjs +1 -0
  106. package/scripts/rebuild-rt.mjs +18 -0
  107. package/scripts/scale-bench.mjs +183 -0
  108. package/scripts/simulate-injection.mjs +47 -8
  109. package/scripts/verify-host-fixtures.mjs +108 -0
  110. package/scripts/verify-host-recovery.mjs +565 -0
  111. package/scripts/verify-vector-recovery.mjs +740 -0
  112. package/tests/auto-link-candidates.test.mjs +20 -0
  113. package/tests/browser.test.mjs +16 -2
  114. package/tests/client-export.test.mjs +16 -1
  115. package/tests/client-pagination.test.mjs +1495 -0
  116. package/tests/client-render.test.mjs +223 -28
  117. package/tests/consolidation-fork.test.mjs +97 -13
  118. package/tests/consolidation-ledger.test.mjs +17 -1
  119. package/tests/diagnostic-entity-candidates.test.mjs +54 -0
  120. package/tests/diagnostic-report.test.mjs +429 -0
  121. package/tests/diagnostic-wiring.test.mjs +88 -0
  122. package/tests/diagnostics.test.mjs +27 -0
  123. package/tests/embedding-download.test.mjs +44 -0
  124. package/tests/engine-source-settings.test.mjs +69 -0
  125. package/tests/engine-sources.test.mjs +145 -0
  126. package/tests/entity-dedupe.test.mjs +143 -0
  127. package/tests/entity-merge-review.test.mjs +363 -0
  128. package/tests/entity-merge.test.mjs +12 -4
  129. package/tests/entity-review.test.mjs +87 -0
  130. package/tests/environment-check.test.mjs +51 -1
  131. package/tests/feedback.test.mjs +95 -0
  132. package/tests/graph-adjacency-index.test.mjs +536 -0
  133. package/tests/graph-cascade.test.mjs +229 -0
  134. package/tests/graph-import-diagnostic.test.mjs +25 -0
  135. package/tests/graph-overview.test.mjs +709 -0
  136. package/tests/graph-session-authorization.test.mjs +419 -0
  137. package/tests/graph-view-geometry.test.mjs +426 -0
  138. package/tests/graph-view-scale.test.mjs +88 -0
  139. package/tests/graph-view.test.mjs +444 -5
  140. package/tests/graph.test.mjs +43 -0
  141. package/tests/help-page.test.mjs +128 -54
  142. package/tests/inbound.test.mjs +19 -0
  143. package/tests/index-pagination.test.mjs +113 -0
  144. package/tests/index-read-view.test.mjs +212 -0
  145. package/tests/inject-guide.test.mjs +9 -0
  146. package/tests/injection-dedupe.test.mjs +213 -0
  147. package/tests/lexicon-recall.test.mjs +641 -0
  148. package/tests/lexicon-rt-lifecycle.test.mjs +196 -0
  149. package/tests/lexicon-rt.test.mjs +468 -0
  150. package/tests/local-embedding-worker.test.mjs +38 -0
  151. package/tests/local-vector-service.test.mjs +32 -3
  152. package/tests/maintenance-compat.test.mjs +128 -0
  153. package/tests/maintenance-settlement.test.mjs +1003 -0
  154. package/tests/managed-memory.test.mjs +39 -11
  155. package/tests/managed-runtime.test.mjs +109 -0
  156. package/tests/memory-fields.test.mjs +24 -12
  157. package/tests/observed-evidence.test.mjs +52 -0
  158. package/tests/port-surface.test.mjs +93 -1
  159. package/tests/preset-mount.test.mjs +12 -4
  160. package/tests/recall-cache.test.mjs +56 -3
  161. package/tests/recall-dedup.test.mjs +63 -2
  162. package/tests/retention.test.mjs +260 -0
  163. package/tests/rt-index-cross-library.test.mjs +230 -0
  164. package/tests/rt-index-integration.test.mjs +194 -0
  165. package/tests/rt-index-retention.test.mjs +552 -0
  166. package/tests/rt-index-scale.test.mjs +204 -0
  167. package/tests/rt-index.test.mjs +705 -0
  168. package/tests/salient-date-boundaries.test.mjs +121 -0
  169. package/tests/salient-wake.test.mjs +29 -0
  170. package/tests/scale-bench.test.mjs +53 -0
  171. package/tests/settings-plumbing.test.mjs +25 -0
  172. package/tests/simulate-injection.test.mjs +15 -1
  173. package/tests/standalone-navigation.test.mjs +44 -0
  174. package/tests/state.test.mjs +51 -0
  175. package/tests/temporal-precision.test.mjs +102 -0
  176. package/tests/temporal.test.mjs +10 -6
  177. package/tests/token-measurement.test.mjs +98 -0
  178. package/tests/tool-schema.test.mjs +41 -5
  179. package/tests/tools.test.mjs +6 -3
  180. package/tests/turn-token-budget.test.mjs +149 -0
  181. package/tests/webui-auth.test.mjs +57 -1
  182. package/tests/webui.test.mjs +40 -15
  183. package/tests/worker-lifecycle.test.mjs +2 -1
@@ -0,0 +1,390 @@
1
+ import { createMutableSpatialGrid } from './graph-spatial-grid.js'
2
+
3
+ const clamp = (n, low, high) => Math.max(low, Math.min(high, n))
4
+ const RELEASE_STEP_LIMIT = 120
5
+ const RELEASE_HARD_LIMIT = 240
6
+ const RELEASE_REPULSION_STEPS = 12
7
+ const RELEASE_STABLE_STEPS = 8
8
+ // While the pointer is held but not moving, the shape must still be allowed to
9
+ // finish travelling. Bounded so a blocked branch cannot spin the loop forever.
10
+ const DRAG_IDLE_STEP_LIMIT = 240
11
+ // A held drag counts as settled only when every connected member is actually at
12
+ // its transported target; velocity/move thresholds alone freeze deep branches
13
+ // mid-flight whenever the user pauses.
14
+ const DRAG_TRANSPORT_TOLERANCE = 1.5
15
+ // The drag transports the cluster on a leash instead of rigidly translating the
16
+ // saved shape. Inside the slack the node is free to relax under its own edge
17
+ // springs and repulsion, so the component flows and re-shapes like a tassel;
18
+ // once it trails past the slack it gets pulled along. Deeper branches get more
19
+ // slack, which is what makes the far end fan out instead of snapping in place.
20
+ const LEASH_RATIO_PER_DEPTH = .04
21
+ const LEASH_RATIO_MAX = .5
22
+ const LEASH_MAX = 64
23
+ // Shape memory stays on after the drop, but only past each node's slack: the
24
+ // cluster keeps the slack it earned while being dragged instead of snapping back
25
+ // into the exact pre-drag arrangement.
26
+ const SHAPE_MEMORY_WHILE_RELEASED = true
27
+ const RELEASE_VELOCITY_LIMIT = .4
28
+ const hash = value => {
29
+ let n = 2166136261
30
+ for (const char of String(value)) n = Math.imul(n ^ char.charCodeAt(0), 16777619)
31
+ return n >>> 0
32
+ }
33
+ const direction = (a, b) => {
34
+ const angle = ((hash(a.id) ^ Math.imul(hash(b.id), 31)) >>> 0) / 4294967296 * Math.PI * 2
35
+ return [Math.cos(angle), Math.sin(angle)]
36
+ }
37
+
38
+ // Follow the complete connected component. The contact budget only limits
39
+ // unrelated obstacles awakened by collisions, never connected branches.
40
+ export function createLocalPhysics({ maxNodes = Infinity, maxEdges = Infinity } = {}) {
41
+ const grid = createMutableSpatialGrid(48)
42
+ let nodes = [], incident = new Map(), byId = new Map(), session = null, gridDirty = false, maxRadius = 6
43
+ let last = { steps: 0, movedNodes: 0, activeNodes: 0, testedPairs: 0, unresolved: 0, hardOverlaps: 0,
44
+ maxPenetration: 0, convergenceLimited: false, saturated: false, capacityLimited: false, retiredNodes: 0, activeEdges: 0, blockedByPins: 0 }
45
+ const refreshGrid = () => { if (gridDirty) { grid.reset(nodes); gridDirty = false } }
46
+ const fixed = node => !!node && (node.fx != null || node.fy != null)
47
+ const collisionRange = node => (node.radius || 6) + maxRadius + 3
48
+ const save = node => {
49
+ if (!session.saved.has(node)) session.saved.set(node, { x: node.x, y: node.y, fx: node.fx, fy: node.fy, locked: node.locked, anchored: node.anchored })
50
+ }
51
+ const addEdges = node => {
52
+ for (const edge of incident.get(node.id) || []) {
53
+ // A retired or refreshed endpoint must not leave a one-sided spring
54
+ // behind. It remains in the obstacle grid, but no longer belongs to the
55
+ // active local solve.
56
+ const from = byId.get(edge.from), to = byId.get(edge.to)
57
+ const fromActive = session.ids.has(edge.from), toActive = session.ids.has(edge.to)
58
+ if (!(fromActive && toActive) && !(fromActive && fixed(to)) && !(toActive && fixed(from))) continue
59
+ if (session.edgeSet.has(edge)) continue
60
+ const topology = session.topology.has(edge.from) && session.topology.has(edge.to)
61
+ if (!topology && session.edgeSet.size >= maxEdges + session.topologyEdges) { session.saturated = true; session.edgeLimited = true; break }
62
+ session.edgeSet.add(edge)
63
+ if (topology) session.topologyEdges++
64
+ }
65
+ }
66
+ const add = (node, depth = Infinity, topology = false) => {
67
+ if (topology) session.topology.add(node.id)
68
+ if (session.ids.has(node.id)) {
69
+ if (depth < (session.depths.get(node.id) ?? Infinity)) session.depths.set(node.id, depth)
70
+ return true
71
+ }
72
+ if (!topology && session.members.length >= maxNodes + session.topology.size - 1) { session.saturated = true; session.capacityBlocked = true; return false }
73
+ save(node); session.ids.add(node.id); session.members.push(node); session.depths.set(node.id, depth); session.version++
74
+ if (node !== session.root && node.anchored && !node.locked) {
75
+ node.fx = null; node.fy = null; node.anchored = false
76
+ }
77
+ addEdges(node)
78
+ return true
79
+ }
80
+ const expandTopology = root => {
81
+ const queue = [{ node: root, depth: session.depths.get(root.id) || 0 }], seen = new Set([root.id])
82
+ for (let i = 0; i < queue.length; i++) {
83
+ const { node, depth } = queue[i]
84
+ for (const edge of incident.get(node.id) || []) {
85
+ const other = edge.source === node ? edge.target : edge.source
86
+ if (seen.has(other.id)) continue
87
+ seen.add(other.id)
88
+ const nextDepth = depth + 1
89
+ // Fixed points are obstacles, not topology boundaries. Continue past
90
+ // them so a free branch on the far side can still be woken later.
91
+ if (add(other, nextDepth, true)) queue.push({ node: other, depth: nextDepth })
92
+ }
93
+ }
94
+ }
95
+ const contact = (a, b, tolerance = 0, padding = 3) => {
96
+ let dx = b.x - a.x, dy = b.y - a.y
97
+ const min = (a.radius || 6) + (b.radius || 6) + padding
98
+ const squared = dx * dx + dy * dy
99
+ if (squared >= (min - tolerance) * (min - tolerance)) return null
100
+ let d = Math.sqrt(squared)
101
+ if (d < .00001) { [dx, dy] = direction(a, b); d = 0 }
102
+ else { dx /= d; dy /= d }
103
+ return { dx, dy, push: min - d }
104
+ }
105
+ const retireSeparated = current => {
106
+ const keep = []
107
+ for (const node of current.members) {
108
+ let touching = current.topology.has(node.id)
109
+ let springing = false
110
+ if (Math.hypot(node.vx || 0, node.vy || 0) > .2) springing = true
111
+ for (const edge of incident.get(node.id) || []) {
112
+ if (springing) break
113
+ if (current.released && current.releaseSteps >= 45 && !current.topology.has(node.id)) continue
114
+ const other = edge.source === node ? edge.target : edge.source
115
+ if (!current.ids.has(other.id)) continue
116
+ const length = Math.hypot(other.x - node.x, other.y - node.y)
117
+ if (Math.abs(length - edge.baseLen) > 12) springing = true
118
+ }
119
+ grid.visit(node.x, node.y, collisionRange(node), other => {
120
+ if (!touching && other !== node && contact(node, other, .001, current.releaseSteps >= 160 ? 0 : current.releaseSteps >= 80 ? .2 : 3)) touching = true
121
+ })
122
+ if (touching || springing) keep.push(node)
123
+ else { node.vx = 0; node.vy = 0; current.ids.delete(node.id); current.retiredNodes++ }
124
+ }
125
+ if (keep.length === current.members.length) return
126
+ // Only contact-free nodes retire, after spring cooldown. They stay in the
127
+ // obstacle grid and cancellation journal; a new contact can wake them again.
128
+ current.members = keep; current.version++
129
+ current.edgeSet.clear(); current.edgeLimited = false; current.topologyEdges = 0
130
+ for (const node of keep) addEdges(node)
131
+ }
132
+ return {
133
+ update(nextNodes, nextEdges) {
134
+ const previous = session
135
+ nodes = nextNodes; byId = new Map(nodes.map(node => [node.id, node])); incident = new Map()
136
+ maxRadius = nodes.reduce((max, node) => Math.max(max, node.radius || 6), 6)
137
+ for (const edge of nextEdges) for (const id of [edge.from, edge.to]) {
138
+ if (!incident.has(id)) incident.set(id, [])
139
+ incident.get(id).push(edge)
140
+ }
141
+ grid.reset(nodes)
142
+ gridDirty = false
143
+ if (previous && byId.get(previous.root.id) === previous.root) {
144
+ previous.members = previous.members.filter(node => byId.get(node.id) === node)
145
+ previous.ids = new Set(previous.members.map(node => node.id))
146
+ previous.depths = new Map([...previous.depths].filter(([id]) => previous.ids.has(id)))
147
+ previous.topology = new Set([...previous.topology].filter(id => previous.ids.has(id)))
148
+ previous.edgeSet.clear(); previous.edgeLimited = false; previous.topologyEdges = 0; previous.version++
149
+ expandTopology(previous.root)
150
+ for (const node of previous.members) addEdges(node)
151
+ } else session = null
152
+ },
153
+ begin(id) {
154
+ const root = byId.get(id)
155
+ if (!root) return null
156
+ refreshGrid()
157
+ session = { root, members: [], ids: new Set(), topology: new Set(), topologyEdges: 0, depths: new Map(), edgeSet: new Set(), saved: new Map(), version: 0,
158
+ steps: 0, testedPairs: 0, moved: new Set(), dragging: true, settled: false, stableSteps: 0, idleSteps: 0,
159
+ released: false, remaining: 0, releaseLimit: RELEASE_STEP_LIMIT, releaseSteps: 0, releaseStableSteps: 0,
160
+ releaseHardLimit: RELEASE_HARD_LIMIT,
161
+ saturated: false, capacityBlocked: false, edgeLimited: false, retiredNodes: 0, blockedByPins: 0 }
162
+ add(root, 0, true); expandTopology(root)
163
+ session.edgeSet.clear(); session.topologyEdges = 0
164
+ for (const node of session.members) addEdges(node)
165
+ root.fx = root.x; root.fy = root.y; root.vx = 0; root.vy = 0
166
+ return session
167
+ },
168
+ move(x, y) {
169
+ if (!session) return
170
+ refreshGrid()
171
+ const root = session.root
172
+ root.x = root.fx = x; root.y = root.fy = y; root.vx = 0; root.vy = 0
173
+ grid.update(root); session.settled = false; session.stableSteps = 0; session.idleSteps = 0
174
+ // A pre-existing fixed point wins over a new drop. Project only the held
175
+ // point, never another user's pin; report the constraint in diagnostics.
176
+ session.blockedByPins = 0
177
+ for (let pass = 0; pass < 4; pass++) {
178
+ const obstacles = []
179
+ grid.visit(root.x, root.y, 80, node => { if (node !== root && fixed(node)) obstacles.push(node) })
180
+ let moved = false
181
+ for (const other of obstacles) {
182
+ const c = contact(other, root)
183
+ if (!c) continue
184
+ root.x += c.dx * (c.push + .01); root.y += c.dy * (c.push + .01)
185
+ session.blockedByPins++; moved = true
186
+ }
187
+ root.fx = root.x; root.fy = root.y; grid.update(root)
188
+ if (!moved) break
189
+ }
190
+ },
191
+ step(options = {}) {
192
+ if (!session || session.dragging && session.settled || session.remaining <= 0 && !session.released && !session.dragging) return false
193
+ refreshGrid()
194
+ const current = session, F = clamp(options.distance || 100, 1, 1000) / 100
195
+ const springScale = current.released ? clamp((160 - current.releaseSteps) / 60, 0, 1) : 1
196
+ const padding = current.released ? current.releaseSteps >= 160 ? 0 : current.releaseSteps >= 80 ? .2 : 3 : 3
197
+ const repulsionScale = current.released
198
+ ? Math.max(0, 1 - current.releaseSteps / RELEASE_REPULSION_STEPS) : 1
199
+ if (current.released) {
200
+ current.releaseSteps++
201
+ // Under capacity pressure, retire only settled members that no longer
202
+ // touch or carry spring energy; live motion keeps its topology intact.
203
+ if (current.members.length > current.topology.size && current.releaseSteps >= RELEASE_REPULSION_STEPS
204
+ && current.releaseSteps % 12 === 0) retireSeparated(current)
205
+ }
206
+ current.capacityBlocked = false
207
+ let maxMove = 0, maxVelocity = 0
208
+ const damping = .78
209
+ for (const node of current.members) { node.vx *= damping; node.vy *= damping }
210
+ const origin = current.saved.get(current.root)
211
+ const pullX = current.root.x - origin.x, pullY = current.root.y - origin.y
212
+ // A leash, not a rigid offset: within the slack the member keeps its own
213
+ // edge-spring equilibrium, so the cluster re-shapes while it is dragged.
214
+ const pullDistance = Math.hypot(pullX, pullY)
215
+ let maxTransportOvershoot = 0
216
+ for (const node of current.members) {
217
+ if (fixed(node) || !current.topology.has(node.id)) continue
218
+ if (current.released && !SHAPE_MEMORY_WHILE_RELEASED) continue
219
+ const start = current.saved.get(node), depth = current.depths.get(node.id) || 1
220
+ const targetX = start.x + pullX, targetY = start.y + pullY
221
+ const dx = targetX - node.x, dy = targetY - node.y
222
+ const distance = Math.hypot(dx, dy)
223
+ // The slack scales with the pull, so a deep branch always trails by a
224
+ // visible fraction of the drag (the tassel fan) yet still follows.
225
+ const leash = Math.min(LEASH_MAX, pullDistance * Math.min(LEASH_RATIO_MAX, depth * LEASH_RATIO_PER_DEPTH))
226
+ const overshoot = Math.max(0, distance - leash)
227
+ maxTransportOvershoot = Math.max(maxTransportOvershoot, overshoot)
228
+ if (overshoot <= 0) continue
229
+ const stiffness = Math.max(.05, .12 / (1 + depth * .12)) * springScale
230
+ node.vx += dx / distance * overshoot * stiffness
231
+ node.vy += dy / distance * overshoot * stiffness
232
+ }
233
+ for (const edge of springScale ? current.edgeSet : []) {
234
+ const a = edge.source, b = edge.target, dx = b.x - a.x, dy = b.y - a.y, d = Math.hypot(dx, dy) || 1
235
+ const degree = Math.max(1, Math.sqrt(Math.max(a.degree || 1, b.degree || 1)))
236
+ const stiffness = .045
237
+ const scale = current.topology.has(a.id) && current.topology.has(b.id) ? springScale
238
+ : current.released ? Math.max(0, 1 - current.releaseSteps / 45) : 1
239
+ const force = clamp((d - edge.baseLen * F) * stiffness / degree, -8, 8) * scale
240
+ if (current.ids.has(a.id) && !fixed(a)) { a.vx += dx / d * force; a.vy += dy / d * force }
241
+ if (current.ids.has(b.id) && !fixed(b)) { b.vx -= dx / d * force; b.vy -= dy / d * force }
242
+ }
243
+ for (const node of current.members) {
244
+ if (fixed(node)) continue
245
+ const beforeX = node.x, beforeY = node.y
246
+ // Bounded repulsion still acts on non-touching neighbors; hard contact
247
+ // below enforces the separate geometric non-overlap constraint.
248
+ if (repulsionScale) grid.visit(node.x, node.y, 54, other => {
249
+ if (other === node) return
250
+ const dx = node.x - other.x, dy = node.y - other.y, d2 = dx * dx + dy * dy
251
+ if (d2 > 1 && d2 < 54 * 54) { const f = repulsionScale * .8 * clamp(options.repulsion || 120, 1, 1000) / 120 / (d2 + 80); node.vx += dx * f; node.vy += dy * f }
252
+ })
253
+ node.x += clamp(node.vx, -48, 48); node.y += clamp(node.vy, -48, 48)
254
+ grid.update(node)
255
+ const move = Math.hypot(node.x - beforeX, node.y - beforeY)
256
+ if (move > .0001) current.moved.add(node.id)
257
+ maxMove = Math.max(maxMove, move)
258
+ }
259
+ // Tethers yield away from the root. Their extension is bounded separately
260
+ // from spring force, so a fast pointer cannot leave kilometre-long links.
261
+ for (let pass = 0; pass < 6; pass++) {
262
+ let stretched = 0
263
+ for (const edge of current.edgeSet) {
264
+ if (!current.topology.has(edge.from) || !current.topology.has(edge.to)) continue
265
+ let a = edge.source, b = edge.target
266
+ if ((current.depths.get(a.id) ?? Infinity) > (current.depths.get(b.id) ?? Infinity)) [a, b] = [b, a]
267
+ const startA = current.saved.get(a), startB = current.saved.get(b)
268
+ if (!startA || !startB) continue
269
+ const radial = current.released && current.releaseSteps >= 80
270
+ const savedX = startB.x - startA.x, savedY = startB.y - startA.y
271
+ // Regrabbing a stretched edge must not turn its old slack into a new
272
+ // rest length. Preserve its direction within the original length.
273
+ const referenceScale = radial ? 0 : F * Math.min(1, edge.baseLen / (Math.hypot(savedX, savedY) || 1))
274
+ const dx = b.x - a.x - savedX * referenceScale
275
+ const dy = b.y - a.y - savedY * referenceScale, d = Math.hypot(dx, dy) || 1
276
+ const rest = edge.baseLen * F, limit = (radial ? rest : 0) + Math.max(12, Math.min(64, rest * .4))
277
+ if (d <= limit + .01) continue
278
+ const freeA = !fixed(a), freeB = !fixed(b)
279
+ if (!freeA && !freeB) continue
280
+ const sameDepth = current.depths.get(a.id) === current.depths.get(b.id)
281
+ const shareA = freeA && (!freeB || sameDepth) ? (freeB ? .5 : 1) : 0
282
+ const shareB = freeB ? 1 - shareA : 0, correction = d - limit
283
+ for (const [node, share, sign] of [[a, shareA, 1], [b, shareB, -1]]) {
284
+ if (!share) continue
285
+ const x = sign * dx / d * correction * share, y = sign * dy / d * correction * share
286
+ node.x += x; node.y += y
287
+ node.vx = clamp(node.vx + x * .12, -48, 48); node.vy = clamp(node.vy + y * .12, -48, 48)
288
+ current.moved.add(node.id); grid.update(node)
289
+ maxMove = Math.max(maxMove, Math.hypot(x, y))
290
+ }
291
+ stretched++
292
+ }
293
+ if (!stretched) break
294
+ }
295
+ const contactPasses = current.released && current.releaseSteps >= 160 ? 12 : 5
296
+ for (let pass = 0; pass < contactPasses; pass++) {
297
+ let contacts = 0
298
+ for (let i = 0; i < current.members.length; i++) {
299
+ const node = current.members[i], others = []
300
+ grid.visit(node.x, node.y, collisionRange(node), other => { if (other !== node) others.push(other) })
301
+ for (const other of others) {
302
+ current.testedPairs++
303
+ const c = contact(node, other, .001, padding)
304
+ if (!c) continue
305
+ const a = !fixed(node), b = !fixed(other) && (current.ids.has(other.id) || add(other, (current.depths.get(node.id) || 0) + 1))
306
+ if (!a && !b) continue
307
+ const share = a && b ? .5 : 1, push = c.push + .005
308
+ if (a) { node.x -= c.dx * push * share; node.y -= c.dy * push * share; node.vx *= .2; node.vy *= .2; current.moved.add(node.id); grid.update(node) }
309
+ if (b) { other.x += c.dx * push * share; other.y += c.dy * push * share; other.vx *= .2; other.vy *= .2; current.moved.add(other.id); grid.update(other) }
310
+ maxMove = Math.max(maxMove, push * share); contacts++
311
+ }
312
+ }
313
+ if (!contacts) break
314
+ }
315
+ for (const node of current.members) maxVelocity = Math.max(maxVelocity, Math.hypot(node.vx || 0, node.vy || 0))
316
+ current.steps++
317
+ if (current.released) current.remaining = Math.max(0, current.releaseLimit - current.releaseSteps)
318
+ let unresolved = 0, movableOverlaps = 0, hardOverlaps = 0, maxPenetration = 0, maxExtension = 0
319
+ for (const edge of current.edgeSet) {
320
+ if (!current.topology.has(edge.from) || !current.topology.has(edge.to)) continue
321
+ const rest = edge.baseLen * F, limit = rest + Math.max(12, Math.min(64, rest * .4))
322
+ maxExtension = Math.max(maxExtension, Math.hypot(edge.source.x - edge.target.x, edge.source.y - edge.target.y) - limit)
323
+ }
324
+ for (const node of current.members) grid.visit(node.x, node.y, collisionRange(node), other => {
325
+ if (other === node || current.ids.has(other.id) && String(node.id) >= String(other.id)) return
326
+ const c = contact(node, other, .05, padding)
327
+ if (!c) return
328
+ unresolved++
329
+ if (!fixed(node) || !fixed(other)) movableOverlaps++
330
+ if (c.push > padding + .05) { hardOverlaps++; maxPenetration = Math.max(maxPenetration, c.push - padding) }
331
+ })
332
+ last = { steps: current.steps, movedNodes: current.moved.size, activeNodes: current.members.length,
333
+ testedPairs: current.testedPairs, unresolved, hardOverlaps, maxPenetration,
334
+ convergenceLimited: current.released && current.remaining <= 0 && (unresolved > 0 || maxVelocity >= RELEASE_VELOCITY_LIMIT || maxExtension > 1),
335
+ maxSpeed: maxVelocity, maxExtension, topologyNodes: current.topology.size,
336
+ dragBlocked: false,
337
+ saturated: current.saturated, capacityLimited: springScale > 0 && current.edgeLimited || unresolved > 0 && current.capacityBlocked,
338
+ retiredNodes: current.retiredNodes, activeEdges: current.edgeSet.size, blockedByPins: current.blockedByPins }
339
+ if (current.dragging) {
340
+ current.idleSteps = (current.idleSteps || 0) + 1
341
+ current.stableSteps = maxVelocity < .18 && maxMove < .18 && maxExtension < 1 && unresolved === 0
342
+ && maxTransportOvershoot < DRAG_TRANSPORT_TOLERANCE ? current.stableSteps + 1 : 0
343
+ if (current.stableSteps >= 4 || current.idleSteps >= DRAG_IDLE_STEP_LIMIT) {
344
+ current.settled = true
345
+ last.dragBlocked = unresolved > 0
346
+ for (const node of current.members) { node.vx = 0; node.vy = 0 }
347
+ }
348
+ } else if (current.released) {
349
+ current.releaseStableSteps = maxVelocity < RELEASE_VELOCITY_LIMIT && maxMove < RELEASE_VELOCITY_LIMIT && maxExtension < 1 && unresolved === 0
350
+ ? current.releaseStableSteps + 1 : 0
351
+ if (current.releaseStableSteps < RELEASE_STABLE_STEPS) {
352
+ if (current.remaining <= 0 && (unresolved === 0 || movableOverlaps > 0) && current.releaseSteps < current.releaseHardLimit) {
353
+ current.releaseLimit = Math.min(current.releaseHardLimit, current.releaseSteps + 30)
354
+ current.remaining = current.releaseLimit - current.releaseSteps
355
+ last.convergenceLimited = false
356
+ }
357
+ if (current.remaining > 0) return true
358
+ }
359
+ for (const node of current.members) { node.vx = 0; node.vy = 0 }
360
+ session = null
361
+ }
362
+ return true
363
+ },
364
+ release() {
365
+ if (!session) return
366
+ session.dragging = false; session.settled = false; session.root.anchored = true
367
+ session.released = true; session.releaseLimit = RELEASE_STEP_LIMIT
368
+ session.remaining = session.releaseLimit; session.releaseSteps = 0; session.releaseStableSteps = 0
369
+ // Preserve the drop exactly. Other free nodes relax around that point.
370
+ session.root.fx = session.root.x; session.root.fy = session.root.y
371
+ },
372
+ cancel() {
373
+ if (!session) return
374
+ for (const [node, saved] of session.saved) {
375
+ Object.assign(node, saved); node.vx = 0; node.vy = 0
376
+ if (byId.get(node.id) === node) grid.update(node)
377
+ }
378
+ session = null
379
+ },
380
+ get session() { return session },
381
+ edgesFor(id) { return incident.get(id) || [] },
382
+ invalidate() { gridDirty = true },
383
+ updatePosition(node) { if (!gridDirty) grid.update(node) },
384
+ get active() { return !!session && (session.dragging ? !session.settled : session.remaining > 0) },
385
+ diagnostics() { return { ...last, running: this.active, released: session?.released || false,
386
+ dragging: session?.dragging || false, settled: session?.settled || false,
387
+ maxNodes: Number.isFinite(maxNodes) ? maxNodes : null, maxEdges: Number.isFinite(maxEdges) ? maxEdges : null } },
388
+ dispose() { grid.dispose(); nodes = []; incident.clear(); byId.clear(); session = null },
389
+ }
390
+ }
@@ -0,0 +1,64 @@
1
+ /** Numeric two-level keys have no stride assumptions or signed-coordinate collisions. */
2
+ export function createSpatialGrid(cellSize = 120) {
3
+ const columns = new Map(), columnPool = [], bucketPool = []
4
+ return {
5
+ reset() {
6
+ for (const column of columns.values()) {
7
+ for (const bucket of column.values()) { bucket.length = 0; bucketPool.push(bucket) }
8
+ column.clear(); columnPool.push(column)
9
+ }
10
+ columns.clear()
11
+ },
12
+ insert(node) {
13
+ const x = Math.floor(node.x / cellSize), y = Math.floor(node.y / cellSize)
14
+ let column = columns.get(x)
15
+ if (!column) columns.set(x, column = columnPool.pop() || new Map())
16
+ let bucket = column.get(y)
17
+ if (!bucket) column.set(y, bucket = bucketPool.pop() || [])
18
+ bucket.push(node)
19
+ },
20
+ get(x, y) { return columns.get(x)?.get(y) },
21
+ dispose() { columns.clear(); columnPool.length = 0; bucketPool.length = 0 },
22
+ }
23
+ }
24
+
25
+ /** A moving local solve updates occupied cells without rebuilding distant rows. */
26
+ export function createMutableSpatialGrid(cellSize = 64) {
27
+ const columns = new Map(), locations = new Map()
28
+ const remove = node => {
29
+ const old = locations.get(node)
30
+ if (!old) return
31
+ const column = columns.get(old.x), bucket = column.get(old.y), last = bucket.pop()
32
+ if (last !== node) { bucket[old.index] = last; locations.get(last).index = old.index }
33
+ if (!bucket.length) column.delete(old.y)
34
+ if (!column.size) columns.delete(old.x)
35
+ locations.delete(node)
36
+ }
37
+ const update = node => {
38
+ const x = Math.floor(node.x / cellSize), y = Math.floor(node.y / cellSize), old = locations.get(node)
39
+ if (old?.x === x && old.y === y) return
40
+ remove(node)
41
+ let column = columns.get(x)
42
+ if (!column) columns.set(x, column = new Map())
43
+ let bucket = column.get(y)
44
+ if (!bucket) column.set(y, bucket = [])
45
+ locations.set(node, { x, y, index: bucket.length }); bucket.push(node)
46
+ }
47
+ return {
48
+ update, remove,
49
+ reset(nodes = []) { columns.clear(); locations.clear(); for (const node of nodes) update(node) },
50
+ visit(x, y, radius, callback) {
51
+ const left = Math.floor((x - radius) / cellSize), right = Math.floor((x + radius) / cellSize)
52
+ const top = Math.floor((y - radius) / cellSize), bottom = Math.floor((y + radius) / cellSize)
53
+ for (let gx = left; gx <= right; gx++) {
54
+ const column = columns.get(gx)
55
+ if (!column) continue
56
+ for (let gy = top; gy <= bottom; gy++) {
57
+ const bucket = column.get(gy)
58
+ if (bucket) for (let i = 0; i < bucket.length; i++) callback(bucket[i])
59
+ }
60
+ }
61
+ },
62
+ dispose() { columns.clear(); locations.clear() },
63
+ }
64
+ }