@myrialabs/clopen 0.4.49 → 0.4.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/backend/chat/background-wait.test.ts +28 -0
- package/backend/chat/message-pages.test.ts +17 -0
- package/backend/chat/message-pages.ts +6 -2
- package/backend/chat/stream-manager.ts +73 -14
- package/backend/database/migrations/088_create_messages_fts_map.ts +52 -0
- package/backend/database/migrations/089_add_memory_layout_tiles.ts +58 -0
- package/backend/database/migrations/index.ts +14 -0
- package/backend/database/queries/graph-queries.ts +392 -135
- package/backend/database/queries/message-fts.test.ts +120 -0
- package/backend/database/queries/message-queries.ts +45 -9
- package/backend/database/queries/session-queries.ts +3 -2
- package/backend/database/queries/utils-queries.ts +3 -0
- package/backend/engine/adapters/claude/environment.ts +4 -0
- package/backend/engine/adapters/claude/input-gate.test.ts +59 -0
- package/backend/engine/adapters/claude/input-gate.ts +74 -0
- package/backend/engine/adapters/claude/message-converter.test.ts +16 -0
- package/backend/engine/adapters/claude/message-converter.ts +8 -0
- package/backend/engine/adapters/claude/stream.ts +15 -0
- package/backend/git/identity/scope-config.test.ts +7 -4
- package/backend/index.ts +35 -8
- package/backend/memory/README.md +7 -4
- package/backend/memory/layout.test.ts +377 -0
- package/backend/memory/layout.ts +1029 -265
- package/backend/memory/view.ts +257 -386
- package/backend/project/status-manager.ts +4 -2
- package/backend/ws/chat/stream.ts +19 -10
- package/backend/ws/memory/crud.ts +118 -81
- package/backend/ws/messages/crud.ts +3 -1
- package/bin/clopen.ts +3 -2
- package/dist/assets/{index-DD0-3EWg.js → index-By6KdSLT.js} +1 -1
- package/dist/assets/index-By6KdSLT.js.br +0 -0
- package/dist/assets/index-By6KdSLT.js.gz +0 -0
- package/dist/assets/{index-CrVWZH_D.js → index-DV4TiDn1.js} +1 -1
- package/dist/assets/index-DV4TiDn1.js.br +0 -0
- package/dist/assets/index-DV4TiDn1.js.gz +0 -0
- package/dist/assets/main-DIESTGTD.css +1 -0
- package/dist/assets/main-DIESTGTD.css.br +0 -0
- package/dist/assets/main-DIESTGTD.css.gz +0 -0
- package/dist/assets/{main-D2_TmcwQ.js → main-Da3JKoua.js} +612 -612
- package/dist/assets/main-Da3JKoua.js.br +0 -0
- package/dist/assets/main-Da3JKoua.js.gz +0 -0
- package/dist/index.html +2 -2
- package/dist/index.html.br +0 -0
- package/dist/index.html.gz +0 -0
- package/package.json +3 -3
- package/shared/types/memory.ts +96 -73
- package/shared/types/unified/index.ts +1 -0
- package/shared/types/unified/stream.ts +22 -1
- package/shared/utils/process-supervisor.test.ts +71 -1
- package/shared/utils/process-supervisor.ts +134 -29
- package/shared/utils/process-tree.ts +99 -0
- package/dist/assets/graph-layout.worker-CRFqN7ZQ.js +0 -1
- package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.br +0 -0
- package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.gz +0 -0
- package/dist/assets/index-CrVWZH_D.js.br +0 -0
- package/dist/assets/index-CrVWZH_D.js.gz +0 -0
- package/dist/assets/index-DD0-3EWg.js.br +0 -0
- package/dist/assets/index-DD0-3EWg.js.gz +0 -0
- package/dist/assets/main-D2_TmcwQ.js.br +0 -0
- package/dist/assets/main-D2_TmcwQ.js.gz +0 -0
- package/dist/assets/main-Yucigcmf.css +0 -1
- package/dist/assets/main-Yucigcmf.css.br +0 -0
- package/dist/assets/main-Yucigcmf.css.gz +0 -0
package/backend/memory/view.ts
CHANGED
|
@@ -1,71 +1,73 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The Memory graph, served the way a map is.
|
|
3
3
|
*
|
|
4
|
-
* ──
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* and this file only reads it.
|
|
4
|
+
* ── Why tiles ────────────────────────────────────────────────────────────────
|
|
5
|
+
* The view used to answer "show me the graph" with the graph: every memory up
|
|
6
|
+
* to a render budget, and past it a single grid laid over the whole extent that
|
|
7
|
+
* merged whatever shared a cell into one mark — which then had to be clicked to
|
|
8
|
+
* see inside. The cost of a look grew with the store, and past the budget the
|
|
9
|
+
* picture was mostly marks standing in for memories.
|
|
11
10
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
11
|
+
* Now the client asks for the squares of the map that are on screen, at the
|
|
12
|
+
* resolution the camera is at, exactly as a map viewer does. The world (see
|
|
13
|
+
* `layout.ts`) is cut into 2^z × 2^z tiles at zoom level z; the client picks the
|
|
14
|
+
* level from its zoom, so a tile is always a few hundred pixels across whatever
|
|
15
|
+
* the zoom. What is on screen therefore costs about the same at any zoom and
|
|
16
|
+
* any store size, and zooming in is how you see more — never clicking.
|
|
17
17
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
18
|
+
* ── What a tile holds ────────────────────────────────────────────────────────
|
|
19
|
+
* The budget is the SCREEN's, not a tile's: how many marks the client can draw
|
|
20
|
+
* at once. When everything in the requested tiles fits it, every memory arrives
|
|
21
|
+
* individually — so a store of a few thousand memories is drawn in full at every
|
|
22
|
+
* zoom, and zooming in can never fold something the overview showed. Only when
|
|
23
|
+
* the screen holds more than the budget is it shared out: sparse tiles keep
|
|
24
|
+
* every memory, and what is left is split between the crowded ones, which are
|
|
25
|
+
* folded into a grid of cells (a cell holding one memory still arrives as that
|
|
26
|
+
* memory). Zooming in shows less of the map, which fits, so every cluster
|
|
27
|
+
* resolves by zooming alone.
|
|
27
28
|
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* the display had none to give.
|
|
32
|
-
*
|
|
33
|
-
* Zooming into a mark asks for its cell as a rectangle, which costs that
|
|
34
|
-
* rectangle rather than the store.
|
|
29
|
+
* Edges travel with the tile they leave from, carrying where their far end is,
|
|
30
|
+
* so the client can attach them to whichever mark stands for that end at the
|
|
31
|
+
* moment — the memory itself, or the cluster it is folded into.
|
|
35
32
|
*/
|
|
36
33
|
|
|
37
|
-
import {
|
|
34
|
+
import { graphLayoutQueries, graphQueries, type GraphListFilter } from '$backend/database/queries/graph-queries';
|
|
35
|
+
import { getLayoutState, scheduleGraphLayout } from './layout';
|
|
38
36
|
import type {
|
|
39
|
-
GraphNode,
|
|
40
|
-
GraphRegion,
|
|
41
37
|
GraphScope,
|
|
42
38
|
GraphSource,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
39
|
+
GraphTile,
|
|
40
|
+
GraphTileCellEdge,
|
|
41
|
+
GraphTileCluster,
|
|
42
|
+
GraphTileNode,
|
|
43
|
+
GraphTilesResponse,
|
|
44
|
+
GraphViewMeta
|
|
48
45
|
} from '$shared/types/memory';
|
|
49
46
|
|
|
47
|
+
/** The deepest zoom level a client may ask for — far past one memory per tile. */
|
|
48
|
+
export const MAX_TILE_ZOOM = 20;
|
|
49
|
+
|
|
50
|
+
/** Tiles served per request. A screen needs a few dozen at most. */
|
|
51
|
+
const MAX_TILES_PER_REQUEST = 64;
|
|
52
|
+
|
|
53
|
+
/** The range a client may ask the screen's budget to be in. */
|
|
54
|
+
const MIN_SCREEN_BUDGET = 256;
|
|
55
|
+
const MAX_SCREEN_BUDGET = 40_000;
|
|
56
|
+
|
|
50
57
|
/**
|
|
51
|
-
*
|
|
58
|
+
* Cells per side when a crowded tile is folded.
|
|
52
59
|
*
|
|
53
|
-
* A
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
60
|
+
* A tile is drawn a few hundred pixels across, so 32 makes a cell 8–16 pixels —
|
|
61
|
+
* about the size a cluster is drawn, which is what stops neighbouring clusters
|
|
62
|
+
* overlapping. Smaller when the budget is smaller, so a folded tile never holds
|
|
63
|
+
* more marks than its budget.
|
|
57
64
|
*/
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
/** Bin edges drawn at once, heaviest first. Lines are cheaper than marks, not free. */
|
|
61
|
-
const MAX_BIN_EDGES = 6_000;
|
|
65
|
+
const MAX_CELLS_PER_SIDE = 32;
|
|
62
66
|
|
|
63
|
-
/**
|
|
64
|
-
const
|
|
67
|
+
/** Edges served per tile, as a multiple of the marks it holds, heaviest first. */
|
|
68
|
+
const EDGES_PER_MARK = 5;
|
|
65
69
|
|
|
66
70
|
export interface GraphViewFilter {
|
|
67
|
-
/** `undefined` = every project (cross-project view); `null` = global only. */
|
|
68
|
-
projectId?: string | null;
|
|
69
71
|
/** The multi-select's answer: absent = every project, empty = global only. */
|
|
70
72
|
projectIds?: string[];
|
|
71
73
|
/** Narrow to particular subkinds — decisions only, failures only, and so on. */
|
|
@@ -76,385 +78,254 @@ export interface GraphViewFilter {
|
|
|
76
78
|
includeArchived?: boolean;
|
|
77
79
|
/** Show memories that have been replaced by a newer belief. Off by default. */
|
|
78
80
|
includeSuperseded?: boolean;
|
|
79
|
-
/**
|
|
80
|
-
* A rectangle of the layout to restrict to — how a mark is opened.
|
|
81
|
-
*
|
|
82
|
-
* Navigation, not filtering: it narrows what is DRAWN without changing what
|
|
83
|
-
* the graph is, which is why the totals it reports are the region's.
|
|
84
|
-
*/
|
|
85
|
-
region?: GraphRegion;
|
|
86
|
-
limit?: number;
|
|
87
81
|
}
|
|
88
82
|
|
|
89
|
-
export
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
return flat(nodes, region, nodes.length, false);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const extent = graphLayoutQueries.extent(filter);
|
|
101
|
-
|
|
102
|
-
// Either it fits, or nothing has been laid out yet — a fresh install, or the
|
|
103
|
-
// first fetch after the upgrade added the table. Both draw the flat view: the
|
|
104
|
-
// second because binning needs positions, and falling back to what the feature
|
|
105
|
-
// always did means the graph is never empty while the background pass catches
|
|
106
|
-
// up.
|
|
107
|
-
const laidOut = extent.total - extent.unplaced;
|
|
108
|
-
if (extent.total <= RENDER_BUDGET || laidOut === 0) {
|
|
109
|
-
const nodes = graphQueries.list({ ...filter, limit });
|
|
110
|
-
return flat(nodes, null, extent.total, extent.total > nodes.length);
|
|
111
|
-
}
|
|
83
|
+
export interface GraphTileRequest extends GraphViewFilter {
|
|
84
|
+
/** The epoch the client's tile addresses were computed against. */
|
|
85
|
+
epoch: number;
|
|
86
|
+
z: number;
|
|
87
|
+
tiles: { x: number; y: number }[];
|
|
88
|
+
/** Marks the client can draw at once, shared out across `tiles`. */
|
|
89
|
+
budget: number;
|
|
90
|
+
}
|
|
112
91
|
|
|
113
|
-
|
|
92
|
+
function listFilter(filter: GraphViewFilter): GraphListFilter {
|
|
93
|
+
return {
|
|
94
|
+
projectIds: filter.projectIds,
|
|
95
|
+
subkinds: filter.subkinds,
|
|
96
|
+
scopes: filter.scopes,
|
|
97
|
+
sources: filter.sources,
|
|
98
|
+
includeArchived: filter.includeArchived,
|
|
99
|
+
includeSuperseded: filter.includeSuperseded
|
|
100
|
+
};
|
|
114
101
|
}
|
|
115
102
|
|
|
116
103
|
/**
|
|
117
|
-
*
|
|
104
|
+
* What the client needs before it can ask for tiles: the world, its epoch, and
|
|
105
|
+
* the filter's counts and extent.
|
|
118
106
|
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
107
|
+
* Also the nudge for anything still waiting to be placed. A memory with no
|
|
108
|
+
* position is counted but not drawn, and the pass that places it is normally
|
|
109
|
+
* already scheduled by the write — this covers a store that was written while
|
|
110
|
+
* the server was not running a pass at all (an upgrade, a clear-data).
|
|
121
111
|
*/
|
|
122
|
-
function
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
): GraphView {
|
|
128
|
-
if (nodes.length === 0) {
|
|
129
|
-
return {
|
|
130
|
-
level: 'flat',
|
|
131
|
-
nodes: [],
|
|
132
|
-
edges: [],
|
|
133
|
-
bins: [],
|
|
134
|
-
binEdges: [],
|
|
135
|
-
region,
|
|
136
|
-
totalNodes,
|
|
137
|
-
truncated: false
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const ids = nodes.map(node => node.id);
|
|
142
|
-
const stored = graphQueries.edgesWithin(ids);
|
|
143
|
-
|
|
144
|
-
// Two memories about the same subject ARE connected, and that connection is
|
|
145
|
-
// stated by extraction rather than inferred from how they are worded. It is
|
|
146
|
-
// derived here rather than stored, so it can never outlive the claim it came
|
|
147
|
-
// from — see `derivedEdges`.
|
|
148
|
-
//
|
|
149
|
-
// Without it the episodic half has almost no structure at all. Similarity
|
|
150
|
-
// linking used to supply it, badly: on a real graph 144 of 371 edges were
|
|
151
|
-
// fabricated by cosine over a corpus with no variance. Removing that was right
|
|
152
|
-
// and removing it ALONE was not — the view became ninety-three disconnected
|
|
153
|
-
// dots, which is a less honest picture than the wrong one, because these
|
|
154
|
-
// memories genuinely do belong together.
|
|
155
|
-
const derived: GraphViewEdge[] = graphQueries.derivedEdges(ids).map((edge, index) => ({
|
|
156
|
-
// Negative ids, so nothing can mistake a derived edge for a stored row and
|
|
157
|
-
// try to delete or re-weight it.
|
|
158
|
-
id: -(index + 1),
|
|
159
|
-
source: edge.srcId,
|
|
160
|
-
target: edge.dstId,
|
|
161
|
-
rel: 'relates_to' as const,
|
|
162
|
-
weight: edge.weight
|
|
163
|
-
}));
|
|
164
|
-
|
|
165
|
-
// Degree is counted over what is being DRAWN, so a node's size answers "how
|
|
166
|
-
// connected is this within what I am looking at" — which is the question the
|
|
167
|
-
// eye is actually asking.
|
|
168
|
-
const degree = new Map<string, number>();
|
|
169
|
-
const bump = (srcId: string, dstId: string): void => {
|
|
170
|
-
degree.set(srcId, (degree.get(srcId) ?? 0) + 1);
|
|
171
|
-
degree.set(dstId, (degree.get(dstId) ?? 0) + 1);
|
|
172
|
-
};
|
|
173
|
-
for (const edge of stored) bump(edge.srcId, edge.dstId);
|
|
174
|
-
for (const edge of derived) bump(edge.source, edge.target);
|
|
175
|
-
|
|
176
|
-
const layout = graphLayoutQueries.read(ids);
|
|
177
|
-
const viewNodes = nodes.map(node => toViewNode(node, degree.get(node.id) ?? 0, layout.get(node.id)));
|
|
178
|
-
placeArrivals(viewNodes, [...stored.map(edge => ({ source: edge.srcId, target: edge.dstId })), ...derived]);
|
|
112
|
+
export function buildGraphMeta(filter: GraphViewFilter): GraphViewMeta {
|
|
113
|
+
const narrowing = listFilter(filter);
|
|
114
|
+
const state = getLayoutState();
|
|
115
|
+
const extent = graphLayoutQueries.extent(narrowing);
|
|
116
|
+
if (extent.unplaced > 0) scheduleGraphLayout();
|
|
179
117
|
|
|
118
|
+
const placed = extent.total - extent.unplaced;
|
|
180
119
|
return {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
})),
|
|
191
|
-
...derived
|
|
192
|
-
],
|
|
193
|
-
bins: [],
|
|
194
|
-
binEdges: [],
|
|
195
|
-
region,
|
|
196
|
-
totalNodes,
|
|
197
|
-
truncated
|
|
120
|
+
epoch: state.epoch,
|
|
121
|
+
stamp: state.stamp,
|
|
122
|
+
world: state.world,
|
|
123
|
+
spacing: state.spacing,
|
|
124
|
+
total: extent.total,
|
|
125
|
+
unplaced: extent.unplaced,
|
|
126
|
+
extent:
|
|
127
|
+
placed > 0 ? { minX: extent.minX, maxX: extent.maxX, minY: extent.minY, maxY: extent.maxY } : null,
|
|
128
|
+
degreeScale: placed > 0 ? graphLayoutQueries.degreeScale(narrowing) : 1
|
|
198
129
|
};
|
|
199
130
|
}
|
|
200
131
|
|
|
201
132
|
/**
|
|
202
|
-
* The
|
|
133
|
+
* The requested tiles of the map at one zoom level.
|
|
203
134
|
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
* was going to overlap.
|
|
135
|
+
* Answers `stale` instead of tiles when the client's epoch is not the current
|
|
136
|
+
* one: its addresses were computed against a world that no longer exists, and
|
|
137
|
+
* serving them would draw the right memories in the wrong places.
|
|
208
138
|
*/
|
|
209
|
-
function
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
139
|
+
export function buildGraphTiles(request: GraphTileRequest): GraphTilesResponse {
|
|
140
|
+
const state = getLayoutState();
|
|
141
|
+
const world = state.world;
|
|
142
|
+
if (!world || request.epoch !== state.epoch) return { epoch: state.epoch, stale: true, tiles: [] };
|
|
143
|
+
|
|
144
|
+
const z = Math.max(0, Math.min(MAX_TILE_ZOOM, Math.floor(request.z)));
|
|
145
|
+
const span = 2 ** z;
|
|
146
|
+
const tileSize = world.size / span;
|
|
147
|
+
const budget = Math.max(MIN_SCREEN_BUDGET, Math.min(MAX_SCREEN_BUDGET, Math.floor(request.budget)));
|
|
148
|
+
const filter = listFilter(request);
|
|
149
|
+
|
|
150
|
+
const wanted: { x: number; y: number }[] = [];
|
|
151
|
+
const seen = new Set<string>();
|
|
152
|
+
for (const tile of request.tiles) {
|
|
153
|
+
const x = Math.floor(tile.x);
|
|
154
|
+
const y = Math.floor(tile.y);
|
|
155
|
+
if (x < 0 || y < 0 || x >= span || y >= span) continue;
|
|
156
|
+
const key = `${x}:${y}`;
|
|
157
|
+
if (seen.has(key)) continue;
|
|
158
|
+
seen.add(key);
|
|
159
|
+
wanted.push({ x, y });
|
|
160
|
+
if (wanted.length >= MAX_TILES_PER_REQUEST) break;
|
|
161
|
+
}
|
|
162
|
+
if (wanted.length === 0) return { epoch: state.epoch, stale: false, tiles: [] };
|
|
163
|
+
|
|
164
|
+
const rectOf = (x: number, y: number) => ({
|
|
165
|
+
minX: world.minX + x * tileSize,
|
|
166
|
+
maxX: world.minX + (x + 1) * tileSize,
|
|
167
|
+
minY: world.minY + y * tileSize,
|
|
168
|
+
maxY: world.minY + (y + 1) * tileSize
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// One counting query over the union of what was asked, so each tile's
|
|
172
|
+
// resolution is decided before a single label is read.
|
|
173
|
+
let minTileX = Infinity;
|
|
174
|
+
let maxTileX = -Infinity;
|
|
175
|
+
let minTileY = Infinity;
|
|
176
|
+
let maxTileY = -Infinity;
|
|
177
|
+
for (const tile of wanted) {
|
|
178
|
+
minTileX = Math.min(minTileX, tile.x);
|
|
179
|
+
maxTileX = Math.max(maxTileX, tile.x);
|
|
180
|
+
minTileY = Math.min(minTileY, tile.y);
|
|
181
|
+
maxTileY = Math.max(maxTileY, tile.y);
|
|
182
|
+
}
|
|
183
|
+
const union = {
|
|
184
|
+
minX: world.minX + minTileX * tileSize,
|
|
185
|
+
maxX: world.minX + (maxTileX + 1) * tileSize,
|
|
186
|
+
minY: world.minY + minTileY * tileSize,
|
|
187
|
+
maxY: world.minY + (maxTileY + 1) * tileSize
|
|
188
|
+
};
|
|
189
|
+
const counts = new Map<string, number>();
|
|
190
|
+
for (const row of graphLayoutQueries.tileCounts(filter, { originX: world.minX, originY: world.minY, size: tileSize }, union)) {
|
|
191
|
+
counts.set(`${row.tileX}:${row.tileY}`, row.count);
|
|
232
192
|
}
|
|
233
193
|
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
194
|
+
const allowance = shareBudget(
|
|
195
|
+
wanted.map(({ x, y }) => counts.get(`${x}:${y}`) ?? 0),
|
|
196
|
+
budget
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
// An edge between two tiles of the same request would otherwise travel twice,
|
|
200
|
+
// once from each end — measured, that was most of the payload at overview.
|
|
201
|
+
const sentPairs = new Set<string>();
|
|
202
|
+
const firstTime = (a: string, b: string): boolean => {
|
|
203
|
+
const key = a < b ? `${a}|${b}` : `${b}|${a}`;
|
|
204
|
+
if (sentPairs.has(key)) return false;
|
|
205
|
+
sentPairs.add(key);
|
|
206
|
+
return true;
|
|
207
|
+
};
|
|
237
208
|
|
|
238
|
-
|
|
239
|
-
const
|
|
240
|
-
const
|
|
241
|
-
|
|
209
|
+
const tiles: GraphTile[] = wanted.map(({ x, y }, index) => {
|
|
210
|
+
const count = counts.get(`${x}:${y}`) ?? 0;
|
|
211
|
+
const allowed = allowance[index];
|
|
212
|
+
const rect = rectOf(x, y);
|
|
213
|
+
const empty: GraphTile = { z, x, y, cells: 0, nodes: [], clusters: [], edges: [], cellEdges: [] };
|
|
214
|
+
if (count === 0) return empty;
|
|
215
|
+
const edgeLimit = Math.max(1, Math.min(count, allowed)) * EDGES_PER_MARK;
|
|
216
|
+
|
|
217
|
+
if (count <= allowed) {
|
|
218
|
+
const nodes = graphLayoutQueries.tileNodes(filter, rect, count).map(round);
|
|
219
|
+
const edges = graphLayoutQueries
|
|
220
|
+
.edgesTouching(
|
|
221
|
+
filter,
|
|
222
|
+
nodes.map(node => node.id),
|
|
223
|
+
edgeLimit
|
|
224
|
+
)
|
|
225
|
+
.filter(edge => firstTime(edge.source, edge.target))
|
|
226
|
+
.map(edge => ({ ...edge, targetX: tidy(edge.targetX), targetY: tidy(edge.targetY) }));
|
|
227
|
+
return { ...empty, nodes, edges };
|
|
228
|
+
}
|
|
242
229
|
|
|
243
|
-
//
|
|
244
|
-
//
|
|
245
|
-
//
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
230
|
+
// Folded, into no more cells than its share of the budget. Cell coordinates
|
|
231
|
+
// are global at this zoom, so an edge from any other tile can name the cell
|
|
232
|
+
// it lands in without knowing this tile.
|
|
233
|
+
const cellsPerSide = Math.max(4, Math.min(MAX_CELLS_PER_SIDE, Math.floor(Math.sqrt(allowed))));
|
|
234
|
+
const cellGrid = { originX: world.minX, originY: world.minY, size: tileSize / cellsPerSide };
|
|
235
|
+
const nodes: GraphTileNode[] = [];
|
|
236
|
+
const clusters: GraphTileCluster[] = [];
|
|
237
|
+
const markOfCell = new Map<string, string>();
|
|
238
|
+
|
|
239
|
+
for (const cell of graphLayoutQueries.tileCells(filter, rect, cellGrid)) {
|
|
240
|
+
const key = `${cell.cellX}:${cell.cellY}`;
|
|
241
|
+
if (cell.members === 1) {
|
|
242
|
+
nodes.push(round(cell));
|
|
243
|
+
markOfCell.set(key, cell.id);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const id = `cluster:${z}:${cell.cellX}:${cell.cellY}`;
|
|
247
|
+
clusters.push({
|
|
248
|
+
id,
|
|
249
|
+
members: cell.members,
|
|
250
|
+
label: cell.label,
|
|
251
|
+
community: cell.community,
|
|
252
|
+
x: tidy(cell.x),
|
|
253
|
+
y: tidy(cell.y),
|
|
254
|
+
cellX: cell.cellX,
|
|
255
|
+
cellY: cell.cellY
|
|
256
|
+
});
|
|
257
|
+
markOfCell.set(key, id);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const cellEdges: GraphTileCellEdge[] = [];
|
|
261
|
+
for (const edge of graphLayoutQueries.cellEdges(filter, rect, cellGrid, edgeLimit)) {
|
|
262
|
+
const source = markOfCell.get(`${edge.sourceCellX}:${edge.sourceCellY}`);
|
|
263
|
+
if (!source) continue;
|
|
264
|
+
if (!firstTime(`${z}:${edge.sourceCellX}:${edge.sourceCellY}`, `${z}:${edge.targetCellX}:${edge.targetCellY}`)) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
cellEdges.push({
|
|
250
268
|
source,
|
|
251
|
-
|
|
252
|
-
|
|
269
|
+
targetCellX: edge.targetCellX,
|
|
270
|
+
targetCellY: edge.targetCellY,
|
|
253
271
|
weight: edge.weight
|
|
254
272
|
});
|
|
255
|
-
} else {
|
|
256
|
-
binEdges.push({ source, target, weight: edge.weight });
|
|
257
273
|
}
|
|
258
|
-
}
|
|
259
274
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
// not the thing it connects to had to be merged.
|
|
263
|
-
const degree = new Map<string, number>();
|
|
264
|
-
const bump = (id: string): void => {
|
|
265
|
-
degree.set(id, (degree.get(id) ?? 0) + 1);
|
|
266
|
-
};
|
|
267
|
-
for (const edge of nodeEdges) {
|
|
268
|
-
bump(edge.source);
|
|
269
|
-
bump(edge.target);
|
|
270
|
-
}
|
|
271
|
-
for (const edge of binEdges) {
|
|
272
|
-
bump(edge.source);
|
|
273
|
-
bump(edge.target);
|
|
274
|
-
}
|
|
275
|
+
return { ...empty, cells: cellsPerSide, nodes, clusters, cellEdges };
|
|
276
|
+
});
|
|
275
277
|
|
|
276
|
-
|
|
277
|
-
id: binId(cell.cellX, cell.cellY),
|
|
278
|
-
members: cell.members,
|
|
279
|
-
label: truncate(cell.label ?? ''),
|
|
280
|
-
community: cell.community,
|
|
281
|
-
x: cell.x,
|
|
282
|
-
y: cell.y,
|
|
283
|
-
region: {
|
|
284
|
-
minX: grid.originX + cell.cellX * grid.cellWidth,
|
|
285
|
-
maxX: grid.originX + (cell.cellX + 1) * grid.cellWidth,
|
|
286
|
-
minY: grid.originY + cell.cellY * grid.cellHeight,
|
|
287
|
-
maxY: grid.originY + (cell.cellY + 1) * grid.cellHeight
|
|
288
|
-
}
|
|
289
|
-
}));
|
|
290
|
-
|
|
291
|
-
return {
|
|
292
|
-
level: 'binned',
|
|
293
|
-
nodes: singles
|
|
294
|
-
.map(cell => byId.get(cell.id))
|
|
295
|
-
.filter((node): node is GraphNode => node !== undefined)
|
|
296
|
-
.map(node => toViewNode(node, degree.get(node.id) ?? 0, layout.get(node.id))),
|
|
297
|
-
edges: nodeEdges,
|
|
298
|
-
bins,
|
|
299
|
-
binEdges,
|
|
300
|
-
region: null,
|
|
301
|
-
totalNodes: extent.total,
|
|
302
|
-
// Nothing was left out — every memory is inside one of these marks. The
|
|
303
|
-
// only thing a binned view withholds is which memory is which inside a
|
|
304
|
-
// crowd, and opening the mark answers that.
|
|
305
|
-
truncated: nodes.length + bins.length > limit
|
|
306
|
-
};
|
|
278
|
+
return { epoch: state.epoch, stale: false, tiles };
|
|
307
279
|
}
|
|
308
280
|
|
|
309
281
|
/**
|
|
310
|
-
*
|
|
282
|
+
* Share the screen's budget between tiles, sparse ones first.
|
|
311
283
|
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
284
|
+
* Water-filling: walking the tiles from least to most crowded, each takes what
|
|
285
|
+
* it holds if that is within an even split of what is left, and the crowded
|
|
286
|
+
* remainder share the rest equally. So a tile is only ever folded when the
|
|
287
|
+
* screen as a whole could not afford it, and then only the crowded ones are.
|
|
316
288
|
*/
|
|
317
|
-
function
|
|
318
|
-
const
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
originY: extent.minY,
|
|
329
|
-
cellWidth: cell,
|
|
330
|
-
cellHeight: cell
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
function toViewNode(
|
|
335
|
-
node: GraphNode,
|
|
336
|
-
degree: number,
|
|
337
|
-
placed?: { community: number; x: number; y: number; placed: number }
|
|
338
|
-
): GraphViewNode {
|
|
339
|
-
return {
|
|
340
|
-
id: node.id,
|
|
341
|
-
subkind: node.subkind,
|
|
342
|
-
scope: node.scope,
|
|
343
|
-
label: node.label,
|
|
344
|
-
projectId: node.projectId,
|
|
345
|
-
degree,
|
|
346
|
-
weight: node.weight,
|
|
347
|
-
pinned: node.pinned,
|
|
348
|
-
community: placed?.community ?? 0,
|
|
349
|
-
createdAt: node.createdAt,
|
|
350
|
-
// Sent ONLY when the simulation computed it. A memory written since the
|
|
351
|
-
// last pass has no row at all, and one below the simulation cap has a row
|
|
352
|
-
// holding a guess — in both cases the client seeds it from its neighbours,
|
|
353
|
-
// which is a far better answer than drawing a whole lobe on one point.
|
|
354
|
-
...(placed?.placed === 1 && { x: placed.x, y: placed.y })
|
|
355
|
-
};
|
|
289
|
+
function shareBudget(counts: number[], budget: number): number[] {
|
|
290
|
+
const allowance = new Array<number>(counts.length).fill(0);
|
|
291
|
+
const order = counts.map((count, index) => ({ count, index })).sort((a, b) => a.count - b.count);
|
|
292
|
+
let remaining = budget;
|
|
293
|
+
for (let i = 0; i < order.length; i++) {
|
|
294
|
+
const share = Math.floor(remaining / (order.length - i));
|
|
295
|
+
const take = Math.min(order[i].count, share);
|
|
296
|
+
allowance[order[i].index] = order[i].count <= share ? order[i].count : Math.max(16, share);
|
|
297
|
+
remaining -= take;
|
|
298
|
+
}
|
|
299
|
+
return allowance;
|
|
356
300
|
}
|
|
357
301
|
|
|
358
302
|
/**
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
* A memory recorded while the modal is open has no layout row: the background
|
|
362
|
-
* pass is debounced behind the notification that announces the memory, so the
|
|
363
|
-
* first fetch after it lands is always a few seconds early. Sending it without
|
|
364
|
-
* coordinates left the client to invent some, and what it invented was the
|
|
365
|
-
* middle of the map — a new memory appearing nowhere near the subject it is
|
|
366
|
-
* about.
|
|
303
|
+
* Where particular memories are on the map, whatever the client has loaded.
|
|
367
304
|
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
* the client stops having an opinion about placement at all.
|
|
372
|
-
*
|
|
373
|
-
* Read-time only — nothing is written. A position that has not been through the
|
|
374
|
-
* simulation is a guess, and guesses do not belong in the table the simulation
|
|
375
|
-
* owns.
|
|
376
|
-
*
|
|
377
|
-
* A node with no placed neighbour is deliberately LEFT without a position. That
|
|
378
|
-
* is the honest signal for "the arrangement does not cover this yet", and it is
|
|
379
|
-
* what the client's cold fallback keys on — inventing a centroid for a store that
|
|
380
|
-
* has never been laid out would replace a considered scatter with a meaningless
|
|
381
|
-
* one.
|
|
305
|
+
* What lets the view go TO a memory it has not drawn: a search result, a
|
|
306
|
+
* connection listed in the inspector, a step back through history. A memory the
|
|
307
|
+
* layout has not placed yet is simply absent from the answer.
|
|
382
308
|
*/
|
|
383
|
-
function
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
const byId = new Map(nodes.map(node => [node.id, node]));
|
|
388
|
-
const adjacency = new Map<string, string[]>();
|
|
389
|
-
const link = (from: string, to: string): void => {
|
|
390
|
-
const neighbours = adjacency.get(from);
|
|
391
|
-
if (neighbours) neighbours.push(to);
|
|
392
|
-
else adjacency.set(from, [to]);
|
|
393
|
-
};
|
|
394
|
-
for (const edge of edges) {
|
|
395
|
-
link(edge.source, edge.target);
|
|
396
|
-
link(edge.target, edge.source);
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// Three passes, so a small cluster of arrivals resolves outward from whichever
|
|
400
|
-
// end touches the existing map rather than all of them giving up at once.
|
|
401
|
-
for (let pass = 0; pass < 3; pass++) {
|
|
402
|
-
let settled = 0;
|
|
403
|
-
|
|
404
|
-
for (const node of arrivals) {
|
|
405
|
-
if (node.x !== undefined) continue;
|
|
406
|
-
|
|
407
|
-
let sumX = 0;
|
|
408
|
-
let sumY = 0;
|
|
409
|
-
let count = 0;
|
|
410
|
-
for (const neighbour of adjacency.get(node.id) ?? []) {
|
|
411
|
-
const other = byId.get(neighbour);
|
|
412
|
-
if (!other || other.x === undefined || other.y === undefined) continue;
|
|
413
|
-
sumX += other.x;
|
|
414
|
-
sumY += other.y;
|
|
415
|
-
count++;
|
|
416
|
-
}
|
|
417
|
-
if (count === 0) continue;
|
|
418
|
-
|
|
419
|
-
// Nudged off the exact centroid, or several arrivals about one subject
|
|
420
|
-
// land on the same point and read as a single node.
|
|
421
|
-
const jitter = jitterOf(node.id);
|
|
422
|
-
node.x = sumX / count + jitter.x * 24;
|
|
423
|
-
node.y = sumY / count + jitter.y * 24;
|
|
424
|
-
settled++;
|
|
425
|
-
}
|
|
309
|
+
export function locateNodes(nodeIds: string[]): GraphTileNode[] {
|
|
310
|
+
return graphLayoutQueries.locate(nodeIds.slice(0, 500)).map(round);
|
|
311
|
+
}
|
|
426
312
|
|
|
427
|
-
|
|
428
|
-
|
|
313
|
+
/** Positions are drawn on a screen; a tenth of a unit is far below a pixel. */
|
|
314
|
+
function tidy(value: number): number {
|
|
315
|
+
return Math.round(value * 10) / 10;
|
|
429
316
|
}
|
|
430
317
|
|
|
431
|
-
|
|
432
|
-
function jitterOf(id: string): { x: number; y: number } {
|
|
433
|
-
let hash = 2166136261;
|
|
434
|
-
for (let i = 0; i < id.length; i++) {
|
|
435
|
-
hash ^= id.charCodeAt(i);
|
|
436
|
-
hash = Math.imul(hash, 16777619);
|
|
437
|
-
}
|
|
438
|
-
hash = hash >>> 0;
|
|
318
|
+
function round(node: GraphTileNode): GraphTileNode {
|
|
439
319
|
return {
|
|
440
|
-
|
|
441
|
-
|
|
320
|
+
id: node.id,
|
|
321
|
+
label: node.label,
|
|
322
|
+
community: node.community,
|
|
323
|
+
degree: node.degree,
|
|
324
|
+
x: tidy(node.x),
|
|
325
|
+
y: tidy(node.y)
|
|
442
326
|
};
|
|
443
327
|
}
|
|
444
328
|
|
|
445
|
-
/** Namespaced so a bin's id can never collide with a node's. */
|
|
446
|
-
function binId(cellX: number, cellY: number): string {
|
|
447
|
-
return `bin:${cellX}:${cellY}`;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
function cellKey(cellX: number, cellY: number): string {
|
|
451
|
-
return `${cellX}:${cellY}`;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
function truncate(label: string): string {
|
|
455
|
-
return label.length > BIN_LABEL_MAX ? `${label.slice(0, BIN_LABEL_MAX - 1)}…` : label;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
329
|
/**
|
|
459
330
|
* A node plus the nodes it reaches — what the inspector renders.
|
|
460
331
|
*
|