memoryking 0.1.0__py3-none-any.whl

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 (179) hide show
  1. memoryking/__init__.py +3 -0
  2. memoryking/__main__.py +1328 -0
  3. memoryking/adapters/__init__.py +1 -0
  4. memoryking/adapters/adapter_state.py +242 -0
  5. memoryking/adapters/anchor_schema.sql +7 -0
  6. memoryking/adapters/atlas.py +441 -0
  7. memoryking/adapters/browser_commands.py +125 -0
  8. memoryking/adapters/dashboard.py +510 -0
  9. memoryking/adapters/dense_search.py +86 -0
  10. memoryking/adapters/egress_policy.py +244 -0
  11. memoryking/adapters/egress_schema.sql +13 -0
  12. memoryking/adapters/embedding.schema.json +302 -0
  13. memoryking/adapters/embedding_store.py +469 -0
  14. memoryking/adapters/execution.py +369 -0
  15. memoryking/adapters/experience.py +423 -0
  16. memoryking/adapters/experience.schema.json +1508 -0
  17. memoryking/adapters/experience_records.py +239 -0
  18. memoryking/adapters/experience_schema.sql +23 -0
  19. memoryking/adapters/handoff.py +494 -0
  20. memoryking/adapters/handoff.schema.json +299 -0
  21. memoryking/adapters/handoff_schema.sql +17 -0
  22. memoryking/adapters/index_schema.sql +11 -0
  23. memoryking/adapters/inference.py +454 -0
  24. memoryking/adapters/inference.schema.json +541 -0
  25. memoryking/adapters/inspection.py +1128 -0
  26. memoryking/adapters/inspection.schema.json +568 -0
  27. memoryking/adapters/knowledge.schema.json +633 -0
  28. memoryking/adapters/knowledge_schema.sql +37 -0
  29. memoryking/adapters/knowledge_store.py +800 -0
  30. memoryking/adapters/legacy_snapshot.py +358 -0
  31. memoryking/adapters/legacy_tables.py +49 -0
  32. memoryking/adapters/local_contract.py +45 -0
  33. memoryking/adapters/local_embeddings.py +189 -0
  34. memoryking/adapters/local_files.py +242 -0
  35. memoryking/adapters/local_ipc.py +258 -0
  36. memoryking/adapters/local_schema.sql +44 -0
  37. memoryking/adapters/local_store.py +632 -0
  38. memoryking/adapters/local_wire.schema.json +7672 -0
  39. memoryking/adapters/models/minilm-l6-v2-onnx.json +57 -0
  40. memoryking/adapters/models/ms-marco-minilm-l6-v2-onnx.json +57 -0
  41. memoryking/adapters/optional_schema.sql +43 -0
  42. memoryking/adapters/plan_dashboard.py +51 -0
  43. memoryking/adapters/plans.py +253 -0
  44. memoryking/adapters/postgres_admin.py +300 -0
  45. memoryking/adapters/postgres_anchor_schema.sql +9 -0
  46. memoryking/adapters/postgres_counter.sql +14 -0
  47. memoryking/adapters/postgres_egress_schema.sql +15 -0
  48. memoryking/adapters/postgres_experience_schema.sql +39 -0
  49. memoryking/adapters/postgres_index_schema.sql +7 -0
  50. memoryking/adapters/postgres_optional_schema.sql +71 -0
  51. memoryking/adapters/postgres_pool.py +77 -0
  52. memoryking/adapters/postgres_recovery.py +525 -0
  53. memoryking/adapters/postgres_schema.sql +365 -0
  54. memoryking/adapters/postgres_sql.py +102 -0
  55. memoryking/adapters/postgres_store.py +428 -0
  56. memoryking/adapters/postgres_summary_schema.sql +12 -0
  57. memoryking/adapters/postgres_trust_schema.sql +20 -0
  58. memoryking/adapters/processing.schema.json +471 -0
  59. memoryking/adapters/processing_store.py +346 -0
  60. memoryking/adapters/projects.py +83 -0
  61. memoryking/adapters/proposals.py +238 -0
  62. memoryking/adapters/ranking.py +315 -0
  63. memoryking/adapters/repository.schema.json +664 -0
  64. memoryking/adapters/repository_git.py +264 -0
  65. memoryking/adapters/repository_schema.sql +25 -0
  66. memoryking/adapters/repository_store.py +960 -0
  67. memoryking/adapters/retrieval.py +1285 -0
  68. memoryking/adapters/retrieval.schema.json +553 -0
  69. memoryking/adapters/retrieval_schema.sql +10 -0
  70. memoryking/adapters/shared_control.py +40 -0
  71. memoryking/adapters/shared_dashboard.py +636 -0
  72. memoryking/adapters/shared_gateway.py +63 -0
  73. memoryking/adapters/shared_http.py +216 -0
  74. memoryking/adapters/shared_mcp.py +159 -0
  75. memoryking/adapters/shared_service.py +124 -0
  76. memoryking/adapters/stdio.py +193 -0
  77. memoryking/adapters/stdio.tools.json +6776 -0
  78. memoryking/adapters/summary_schema.sql +10 -0
  79. memoryking/adapters/team.py +253 -0
  80. memoryking/adapters/timeline.py +80 -0
  81. memoryking/adapters/token_count.py +55 -0
  82. memoryking/adapters/tokenizers/LICENSE +21 -0
  83. memoryking/adapters/tokenizers/cl100k_base.tiktoken +100256 -0
  84. memoryking/adapters/trust.py +123 -0
  85. memoryking/adapters/trust_schema.sql +13 -0
  86. memoryking/adapters/web/app.css +1251 -0
  87. memoryking/adapters/web/app.js +907 -0
  88. memoryking/adapters/web/brand.js +44 -0
  89. memoryking/adapters/web/crown-mono.svg +1 -0
  90. memoryking/adapters/web/crown.svg +8 -0
  91. memoryking/adapters/web/elephant.png +0 -0
  92. memoryking/adapters/web/favicon.svg +8 -0
  93. memoryking/adapters/web/graph-layout.js +52 -0
  94. memoryking/adapters/web/graph-worker.js +10 -0
  95. memoryking/adapters/web/graph.js +570 -0
  96. memoryking/adapters/web/index.html +208 -0
  97. memoryking/adapters/web/observatory.js +1208 -0
  98. memoryking/adapters/web/team.js +151 -0
  99. memoryking/adapters/web/wordmark.svg +1 -0
  100. memoryking/adapters/working.py +971 -0
  101. memoryking/adapters/working.schema.json +1717 -0
  102. memoryking/adapters/working_schema.sql +24 -0
  103. memoryking/application/__init__.py +1 -0
  104. memoryking/application/conformance.py +50 -0
  105. memoryking/application/legacy_import.py +337 -0
  106. memoryking/application/local_port.py +9 -0
  107. memoryking/application/storage.py +117 -0
  108. memoryking/application/store_core.py +1292 -0
  109. memoryking/backups.py +131 -0
  110. memoryking/contracts/__init__.py +6 -0
  111. memoryking/contracts/__main__.py +20 -0
  112. memoryking/contracts/canonical.py +88 -0
  113. memoryking/contracts/embedding.schema.json +302 -0
  114. memoryking/contracts/embedding_generated.py +65 -0
  115. memoryking/contracts/experience.schema.json +1508 -0
  116. memoryking/contracts/experience_generated.py +239 -0
  117. memoryking/contracts/generated.py +122 -0
  118. memoryking/contracts/handoff.schema.json +299 -0
  119. memoryking/contracts/handoff_generated.py +69 -0
  120. memoryking/contracts/inference.schema.json +541 -0
  121. memoryking/contracts/inference_generated.py +106 -0
  122. memoryking/contracts/processing.schema.json +471 -0
  123. memoryking/contracts/processing_generated.py +82 -0
  124. memoryking/contracts/py.typed +0 -0
  125. memoryking/contracts/repository.schema.json +664 -0
  126. memoryking/contracts/repository_generated.py +144 -0
  127. memoryking/contracts/retrieval.schema.json +553 -0
  128. memoryking/contracts/retrieval_generated.py +110 -0
  129. memoryking/contracts/schema.json +696 -0
  130. memoryking/contracts/validation.py +92 -0
  131. memoryking/contracts/working.schema.json +1717 -0
  132. memoryking/contracts/working_generated.py +291 -0
  133. memoryking/domain/__init__.py +1 -0
  134. memoryking/domain/anchors.py +97 -0
  135. memoryking/domain/briefing.py +108 -0
  136. memoryking/domain/briefing_text.py +222 -0
  137. memoryking/domain/checkpoint.py +118 -0
  138. memoryking/domain/compression.py +64 -0
  139. memoryking/domain/delta.py +129 -0
  140. memoryking/domain/embeddings.py +90 -0
  141. memoryking/domain/hints.py +465 -0
  142. memoryking/domain/http_status.py +45 -0
  143. memoryking/domain/learning.py +62 -0
  144. memoryking/domain/local.py +65 -0
  145. memoryking/domain/plans.py +140 -0
  146. memoryking/domain/repository.py +118 -0
  147. memoryking/domain/selectors.py +356 -0
  148. memoryking/domain/summary.py +88 -0
  149. memoryking/domain/usage.py +163 -0
  150. memoryking/embedding_loop.py +98 -0
  151. memoryking/embedding_worker.py +131 -0
  152. memoryking/harness.py +882 -0
  153. memoryking/harness_context.py +376 -0
  154. memoryking/hooks.py +171 -0
  155. memoryking/host_hooks.py +454 -0
  156. memoryking/lean.py +352 -0
  157. memoryking/memory_tool.py +776 -0
  158. memoryking/migration.py +194 -0
  159. memoryking/model_install.py +78 -0
  160. memoryking/observability.py +101 -0
  161. memoryking/observability_metrics.py +100 -0
  162. memoryking/profiles.py +150 -0
  163. memoryking/providers.py +330 -0
  164. memoryking/py.typed +0 -0
  165. memoryking/pytest_plugin.py +69 -0
  166. memoryking/reference_agent.py +152 -0
  167. memoryking/repository_worker.py +57 -0
  168. memoryking/resume_adapter.py +154 -0
  169. memoryking/sdk.py +302 -0
  170. memoryking/shared.py +301 -0
  171. memoryking/shared_sdk.py +101 -0
  172. memoryking/vault.py +176 -0
  173. memoryking-0.1.0.dist-info/METADATA +157 -0
  174. memoryking-0.1.0.dist-info/RECORD +179 -0
  175. memoryking-0.1.0.dist-info/WHEEL +5 -0
  176. memoryking-0.1.0.dist-info/entry_points.txt +7 -0
  177. memoryking-0.1.0.dist-info/licenses/LICENSE +202 -0
  178. memoryking-0.1.0.dist-info/licenses/NOTICE +12 -0
  179. memoryking-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,570 @@
1
+ /** Legend order and wording; colours live in app.css under [data-authority]. */
2
+ export const AUTHORITY_LEGEND = [
3
+ ["observation", "Observation: recorded, not reviewed"],
4
+ ["accepted", "Accepted claim"],
5
+ ["proposed", "Proposed claim: awaiting review"],
6
+ ["quarantined", "Quarantined claim"],
7
+ ["retired", "Rejected or deprecated claim"],
8
+ ["constraint", "Mandatory constraint"],
9
+ ["attestation", "Attestation by an actor"],
10
+ ["procedure", "Procedure"],
11
+ ["plan", "Plan or plan step"],
12
+ ["working", "Checkpoint, task or run"],
13
+ ];
14
+ /** Authority is what the store recorded about the node; layout never changes it. */
15
+ export function authorityOf(node) {
16
+ switch (node.type) {
17
+ case "observation":
18
+ return "observation";
19
+ case "attestation":
20
+ return "attestation";
21
+ case "procedure":
22
+ return "procedure";
23
+ case "plan":
24
+ case "step":
25
+ return "plan";
26
+ case "checkpoint":
27
+ case "task":
28
+ case "run":
29
+ return "working";
30
+ case "claim":
31
+ if (node.quarantined === true)
32
+ return "quarantined";
33
+ if (node.authority === "mandatory_constraint")
34
+ return "constraint";
35
+ if (node.state === "accepted")
36
+ return "accepted";
37
+ if (node.state === "proposed")
38
+ return "proposed";
39
+ return "retired";
40
+ default:
41
+ return "working";
42
+ }
43
+ }
44
+ /** Radius grows with the square root of citations and is capped; selection adds a ring. */
45
+ export function nodeRadius(citations, selected = false) {
46
+ const count = typeof citations === "number" && Number.isFinite(citations) && citations > 0
47
+ ? citations
48
+ : 0;
49
+ return Math.min(16, 8 + 2 * Math.sqrt(count)) + (selected ? 3 : 0);
50
+ }
51
+ const ns = "http://www.w3.org/2000/svg";
52
+ function svg(tag, attrs = {}) {
53
+ const e = document.createElementNS(ns, tag);
54
+ for (const [k, v] of Object.entries(attrs))
55
+ e.setAttribute(k, v);
56
+ return e;
57
+ }
58
+ function html(tag, text = "") {
59
+ const e = document.createElement(tag);
60
+ e.textContent = text;
61
+ return e;
62
+ }
63
+ export class AtlasView {
64
+ root;
65
+ expand;
66
+ receipts;
67
+ worker = null;
68
+ positions = new Map();
69
+ selected = null;
70
+ data = { nodes: [], edges: [] };
71
+ graph = svg("svg", {
72
+ viewBox: "0 0 900 480",
73
+ role: "group",
74
+ "aria-label": "Memory relationships. Use node buttons or the equivalent list.",
75
+ });
76
+ list = html("div");
77
+ inspector = html("section");
78
+ status = html("p");
79
+ search = document.createElement("input");
80
+ type = document.createElement("select");
81
+ ticket = 0;
82
+ neighbors = false;
83
+ constructor(root, expand, receipts = null) {
84
+ this.root = root;
85
+ this.expand = expand;
86
+ this.receipts = receipts;
87
+ this.root.classList.add("atlas-workspace");
88
+ const controls = html("div");
89
+ controls.className = "actions graph-controls";
90
+ this.search.type = "search";
91
+ this.search.placeholder = "Filter loaded labels";
92
+ this.search.setAttribute("aria-label", "Search loaded graph");
93
+ this.type.setAttribute("aria-label", "Node type");
94
+ for (const name of [
95
+ "all",
96
+ "observation",
97
+ "claim",
98
+ "attestation",
99
+ "procedure",
100
+ "checkpoint",
101
+ "plan",
102
+ "step",
103
+ "task",
104
+ "run",
105
+ "file",
106
+ "symbol",
107
+ ]) {
108
+ const o = document.createElement("option");
109
+ o.value = name;
110
+ o.textContent = name;
111
+ this.type.append(o);
112
+ }
113
+ this.search.oninput = () => this.draw();
114
+ this.type.onchange = () => this.draw();
115
+ controls.append(this.search, this.type);
116
+ for (const [name, action] of [
117
+ ["Re-layout", () => this.settle()],
118
+ ["Freeze layout", () => this.freeze()],
119
+ ["Fit all", () => this.fit()],
120
+ ["Zoom in", () => this.zoom(1 / 1.3)],
121
+ ["Zoom out", () => this.zoom(1.3)],
122
+ [
123
+ "Fit selection",
124
+ () => {
125
+ const p = this.positions.get(this.selected ?? "");
126
+ if (p?.x !== undefined && p.y !== undefined)
127
+ this.graph.setAttribute("viewBox", `${p.x - 150} ${p.y - 120} 300 240`);
128
+ },
129
+ ],
130
+ [
131
+ "Focus neighbors",
132
+ () => {
133
+ this.neighbors = !this.neighbors;
134
+ this.draw();
135
+ },
136
+ ],
137
+ ["Pin / unpin selection", () => this.pin()],
138
+ [
139
+ "Expand one hop",
140
+ () => {
141
+ if (this.selected)
142
+ this.expand(this.selected);
143
+ },
144
+ ],
145
+ ]) {
146
+ const b = html("button", name);
147
+ b.onclick = action;
148
+ controls.append(b);
149
+ }
150
+ const drawing = html("button", "Show drawing");
151
+ drawing.className = "graph-drawing-toggle";
152
+ drawing.setAttribute("aria-pressed", "false");
153
+ drawing.onclick = () => {
154
+ const shown = this.root.classList.toggle("graph-shown");
155
+ drawing.textContent = shown ? "List only" : "Show drawing";
156
+ drawing.setAttribute("aria-pressed", String(shown));
157
+ };
158
+ controls.append(drawing);
159
+ const legend = html("ul");
160
+ legend.className = "graph-legend";
161
+ legend.setAttribute("aria-label", "Node colours by authority");
162
+ for (const [authority, text] of AUTHORITY_LEGEND) {
163
+ const item = html("li", text);
164
+ item.dataset.authority = authority;
165
+ legend.append(item);
166
+ }
167
+ this.status.setAttribute("role", "status");
168
+ this.installNavigation();
169
+ const pane = html("div");
170
+ pane.className = "graph-panes";
171
+ const details = html("div");
172
+ details.className = "graph-details";
173
+ this.list.className = "graph-list";
174
+ this.list.setAttribute("aria-label", "Equivalent node list");
175
+ this.inspector.setAttribute("aria-label", "Selected node or edge");
176
+ details.append(this.list, this.inspector);
177
+ pane.append(this.graph, details);
178
+ root.append(controls, legend, this.status, pane, html("p", "Colour is the recorded authority and size is how many of your receipts cited the record. Layout distance is not confidence. Lines describe recorded relations within the loaded, authorized window. Dragging a node, panning the canvas, the wheel and pinching change presentation only. On a narrow screen the list stands in for the drawing."));
179
+ }
180
+ clear() {
181
+ this.freeze();
182
+ this.ticket++;
183
+ this.positions.clear();
184
+ this.data = { nodes: [], edges: [] };
185
+ this.selected = null;
186
+ this.graph.replaceChildren();
187
+ this.list.replaceChildren();
188
+ this.inspector.replaceChildren();
189
+ this.status.textContent = "Graph cleared; current permission required.";
190
+ }
191
+ set(data) {
192
+ this.freeze();
193
+ this.data = {
194
+ ...data,
195
+ nodes: data.nodes.slice(0, 500),
196
+ edges: data.edges.slice(0, 1500),
197
+ };
198
+ const ids = new Set(this.data.nodes.map((n) => n.id));
199
+ for (const key of this.positions.keys())
200
+ if (!ids.has(key))
201
+ this.positions.delete(key);
202
+ this.data.nodes.forEach((n, i) => {
203
+ if (!this.positions.has(n.id))
204
+ this.positions.set(n.id, {
205
+ id: n.id,
206
+ x: 450 + 200 * Math.cos(i * 2.399),
207
+ y: 240 + 170 * Math.sin(i * 2.399),
208
+ });
209
+ });
210
+ if (this.selected &&
211
+ !ids.has(this.selected) &&
212
+ !this.data.edges.some((edge) => edge.id === this.selected))
213
+ this.selected = null;
214
+ // A refreshed view keeps the selection and its inspector when it still exists.
215
+ if (this.selected)
216
+ this.inspect(this.selected);
217
+ else
218
+ this.inspector.replaceChildren();
219
+ this.draw();
220
+ this.settle();
221
+ }
222
+ /** The current viewBox as numbers; the frame falls back to the default. */
223
+ frame() {
224
+ const parts = (this.graph.getAttribute("viewBox") ?? "0 0 900 480")
225
+ .split(/\s+/)
226
+ .map(Number);
227
+ return parts.length === 4 && parts.every(Number.isFinite)
228
+ ? parts
229
+ : [0, 0, 900, 480];
230
+ }
231
+ /** Scale the frame around a point (default: its centre). Presentation only. */
232
+ zoom(factor, around) {
233
+ const [x, y, w, h] = this.frame();
234
+ const width = Math.min(8000, Math.max(120, w * factor)), height = Math.min(8000, Math.max(64, h * factor));
235
+ const cx = around?.x ?? x + w / 2, cy = around?.y ?? y + h / 2;
236
+ const nx = cx - ((cx - x) * width) / w, ny = cy - ((cy - y) * height) / h;
237
+ this.graph.setAttribute("viewBox", `${Math.round(nx)} ${Math.round(ny)} ${Math.round(width)} ${Math.round(height)}`);
238
+ }
239
+ /** Pointer position in graph units, or null without a rendered frame. */
240
+ graphPoint(ev) {
241
+ const matrix = this.graph.getScreenCTM();
242
+ return matrix
243
+ ? new DOMPoint(ev.clientX, ev.clientY).matrixTransform(matrix.inverse())
244
+ : null;
245
+ }
246
+ /** Wheel zooms around the pointer; dragging empty canvas pans; pinch zooms. */
247
+ installNavigation() {
248
+ this.graph.addEventListener("wheel", (ev) => {
249
+ ev.preventDefault();
250
+ const point = this.graphPoint(ev);
251
+ this.zoom(ev.deltaY > 0 ? 1.15 : 1 / 1.15, point ?? undefined);
252
+ }, { passive: false });
253
+ const touches = new Map();
254
+ let pinch = null;
255
+ this.graph.addEventListener("pointerdown", (ev) => {
256
+ if (ev.target !== this.graph)
257
+ return;
258
+ touches.set(ev.pointerId, { x: ev.clientX, y: ev.clientY });
259
+ this.graph.setPointerCapture(ev.pointerId);
260
+ if (touches.size === 2) {
261
+ const [a, b] = [...touches.values()];
262
+ pinch = Math.hypot(a.x - b.x, a.y - b.y);
263
+ }
264
+ });
265
+ this.graph.addEventListener("pointermove", (ev) => {
266
+ const previous = touches.get(ev.pointerId);
267
+ if (!previous)
268
+ return;
269
+ if (touches.size === 2 && pinch) {
270
+ touches.set(ev.pointerId, { x: ev.clientX, y: ev.clientY });
271
+ const [a, b] = [...touches.values()];
272
+ const distance = Math.hypot(a.x - b.x, a.y - b.y);
273
+ if (distance > 0) {
274
+ const centre = this.graphPoint({
275
+ clientX: (a.x + b.x) / 2,
276
+ clientY: (a.y + b.y) / 2,
277
+ });
278
+ this.zoom(pinch / distance, centre ?? undefined);
279
+ pinch = distance;
280
+ }
281
+ return;
282
+ }
283
+ const from = this.graphPoint({
284
+ clientX: previous.x,
285
+ clientY: previous.y,
286
+ }), to = this.graphPoint(ev);
287
+ touches.set(ev.pointerId, { x: ev.clientX, y: ev.clientY });
288
+ if (!from || !to)
289
+ return;
290
+ const [x, y, w, h] = this.frame();
291
+ this.graph.setAttribute("viewBox", `${Math.round(x - (to.x - from.x))} ${Math.round(y - (to.y - from.y))} ${w} ${h}`);
292
+ });
293
+ for (const type of ["pointerup", "pointercancel"])
294
+ this.graph.addEventListener(type, (ev) => {
295
+ touches.delete(ev.pointerId);
296
+ if (touches.size < 2)
297
+ pinch = null;
298
+ });
299
+ }
300
+ freeze() {
301
+ this.worker?.terminate();
302
+ this.worker = null;
303
+ this.ticket++;
304
+ }
305
+ /** Frame the drawn nodes with room for labels; an empty graph keeps the full frame. */
306
+ fit() {
307
+ const shown = this.data.nodes
308
+ .map((n) => this.positions.get(n.id))
309
+ .filter((p) => p?.x !== undefined && p.y !== undefined);
310
+ if (!shown.length) {
311
+ this.graph.setAttribute("viewBox", "0 0 900 480");
312
+ return;
313
+ }
314
+ const xs = shown.map((p) => p.x), ys = shown.map((p) => p.y);
315
+ const minX = Math.min(...xs) - 40, maxX = Math.max(...xs) + 160, minY = Math.min(...ys) - 40, maxY = Math.max(...ys) + 40;
316
+ const width = Math.max(320, maxX - minX), height = Math.max(200, maxY - minY);
317
+ this.graph.setAttribute("viewBox", `${Math.round(minX)} ${Math.round(minY)} ${Math.round(width)} ${Math.round(height)}`);
318
+ }
319
+ settle() {
320
+ this.freeze();
321
+ const ticket = this.ticket;
322
+ this.status.textContent = "Settling a bounded layout…";
323
+ try {
324
+ const worker = new Worker("/graph-worker.js", { type: "module" });
325
+ this.worker = worker;
326
+ const timeout = window.setTimeout(() => {
327
+ if (ticket === this.ticket) {
328
+ this.freeze();
329
+ this.status.textContent =
330
+ "Layout budget reached. Current positions and the list remain usable.";
331
+ }
332
+ }, 1000);
333
+ worker.onmessage = (e) => {
334
+ window.clearTimeout(timeout);
335
+ if (ticket !== this.ticket)
336
+ return;
337
+ worker.terminate();
338
+ this.worker = null;
339
+ if (e.data.ok) {
340
+ for (const p of e.data.nodes)
341
+ this.positions.set(p.id, p);
342
+ this.draw();
343
+ this.fit();
344
+ this.status.textContent += ` · layout ${e.data.elapsed_ms.toFixed(1)} ms / ${e.data.iterations} iterations`;
345
+ }
346
+ };
347
+ worker.onerror = () => {
348
+ window.clearTimeout(timeout);
349
+ this.freeze();
350
+ this.status.textContent =
351
+ "Layout worker unavailable; static positions and list remain usable.";
352
+ };
353
+ worker.postMessage({
354
+ nodes: [...this.positions.values()],
355
+ edges: this.data.edges,
356
+ });
357
+ }
358
+ catch {
359
+ this.status.textContent = "Static graph; worker unavailable.";
360
+ }
361
+ }
362
+ pin() {
363
+ const p = this.positions.get(this.selected ?? "");
364
+ if (p) {
365
+ p.pinned = !p.pinned;
366
+ this.inspect(p.id);
367
+ this.draw();
368
+ }
369
+ }
370
+ inspect(id) {
371
+ this.selected = id;
372
+ const node = this.data.nodes.find((n) => n.id === id), edge = this.data.edges.find((e) => e.id === id);
373
+ const value = node ?? edge;
374
+ const pre = html("pre", JSON.stringify(value, null, 2));
375
+ this.inspector.replaceChildren(html("h3", node?.label.slice(0, 100) ?? edge?.type ?? "Selection"), html("p", node
376
+ ? `${node.type} · ${authorityOf(node).replaceAll("-", " ")} (${node.authority ?? "recorded"}) · ${this.positions.get(id)?.pinned ? "pinned" : "unpinned"}`
377
+ : "Typed directional edge; provenance below."));
378
+ if (node)
379
+ this.inspector.append(this.trail(node));
380
+ this.inspector.append(pre);
381
+ }
382
+ /** Receipts are the evidence that a record was actually used; they open in Retrieval history. */
383
+ trail(node) {
384
+ const box = html("div");
385
+ box.className = "graph-trail";
386
+ const count = typeof node.citations === "number" ? node.citations : 0;
387
+ box.append(html("p", count === 1
388
+ ? "Cited by 1 of your retained receipts."
389
+ : `Cited by ${count} of your retained receipts.`));
390
+ if (!this.receipts || count === 0)
391
+ return box;
392
+ const show = document.createElement("button");
393
+ show.textContent = "Show receipts";
394
+ show.className = "graph-trail-load";
395
+ show.onclick = () => {
396
+ show.disabled = true;
397
+ void this.receipts.load(node.id).then((items) => {
398
+ show.remove();
399
+ const list = html("div");
400
+ list.className = "graph-trail-list";
401
+ for (const item of items) {
402
+ const open = html("button", `${item.status.replaceAll("_", " ")} · ${item.usage?.output_tokens ?? "?"} tokens · ${new Date(item.created_ms).toLocaleString()} · via ${item.dependency_kind.replaceAll("_", " ")}`);
403
+ open.className = "graph-trail-item";
404
+ open.dataset.receipt = item.receipt;
405
+ open.onclick = () => this.receipts.open(item.receipt);
406
+ list.append(open);
407
+ }
408
+ if (!items.length)
409
+ list.append(html("p", "No receipt is currently available: it expired, or its evidence is no longer readable."));
410
+ box.append(list);
411
+ }, () => {
412
+ show.disabled = false;
413
+ box.append(html("p", "Receipts unavailable under the current session."));
414
+ });
415
+ };
416
+ box.append(show);
417
+ return box;
418
+ }
419
+ draw() {
420
+ const drawStarted = performance.now();
421
+ const focused = document.activeElement?.getAttribute("data-focus-key");
422
+ const needle = this.search.value.toLowerCase();
423
+ let nodes = this.data.nodes.filter((n) => (this.type.value === "all" || n.type === this.type.value) &&
424
+ n.label.toLowerCase().includes(needle));
425
+ if (this.neighbors && this.selected) {
426
+ const ids = new Set([this.selected]);
427
+ for (const e of this.data.edges)
428
+ if (e.source === this.selected || e.target === this.selected) {
429
+ ids.add(e.source);
430
+ ids.add(e.target);
431
+ }
432
+ nodes = nodes.filter((n) => ids.has(n.id));
433
+ }
434
+ const ids = new Set(nodes.map((n) => n.id));
435
+ const edges = this.data.edges.filter((e) => ids.has(e.source) && ids.has(e.target));
436
+ this.graph.replaceChildren();
437
+ this.list.replaceChildren();
438
+ const defs = svg("defs"), marker = svg("marker", {
439
+ id: "relation-arrow",
440
+ viewBox: "0 0 10 10",
441
+ refX: "20",
442
+ refY: "5",
443
+ markerWidth: "5",
444
+ markerHeight: "5",
445
+ orient: "auto-start-reverse",
446
+ });
447
+ marker.append(svg("path", { d: "M0 0L10 5L0 10z", fill: "var(--cyan)" }));
448
+ defs.append(marker);
449
+ this.graph.append(defs);
450
+ for (const e of edges) {
451
+ const a = this.positions.get(e.source), b = this.positions.get(e.target);
452
+ const line = svg("line", {
453
+ x1: String(a.x),
454
+ y1: String(a.y),
455
+ x2: String(b.x),
456
+ y2: String(b.y),
457
+ class: "atlas-edge " + e.type,
458
+ "marker-end": "url(#relation-arrow)",
459
+ tabindex: "0",
460
+ role: "button",
461
+ "aria-label": `${e.type}: ${e.source} to ${e.target}`,
462
+ });
463
+ const title = svg("title");
464
+ title.textContent = e.type;
465
+ line.append(title);
466
+ line.setAttribute("data-focus-key", "edge:" + e.id);
467
+ line.onclick = () => this.inspect(e.id);
468
+ line.onkeydown = (ev) => {
469
+ if (ev.key === "Enter" || ev.key === " ") {
470
+ ev.preventDefault();
471
+ this.inspect(e.id);
472
+ }
473
+ };
474
+ this.graph.append(line);
475
+ }
476
+ for (const n of nodes) {
477
+ const p = this.positions.get(n.id);
478
+ const authority = authorityOf(n);
479
+ const g = svg("g", {
480
+ transform: `translate(${p.x},${p.y})`,
481
+ class: "atlas-node " + n.type,
482
+ "data-authority": authority,
483
+ tabindex: "0",
484
+ role: "button",
485
+ "aria-label": `${n.type}, ${authority}: ${n.label}. Cited by ${String(n.citations ?? 0)} receipts. ${p.pinned ? "Pinned." : ""}`,
486
+ });
487
+ g.append(svg("circle", {
488
+ r: String(nodeRadius(n.citations, n.id === this.selected)),
489
+ }));
490
+ const title = svg("title");
491
+ title.textContent = n.label;
492
+ g.append(title);
493
+ if (nodes.length <= 16 || n.id === this.selected) {
494
+ const text = svg("text", { x: "14", y: "5" });
495
+ text.textContent = n.label.slice(0, 28);
496
+ g.append(text);
497
+ }
498
+ g.setAttribute("data-focus-key", "node:" + n.id);
499
+ g.onclick = () => {
500
+ this.inspect(n.id);
501
+ this.draw();
502
+ };
503
+ g.onkeydown = (ev) => {
504
+ if (ev.key === "Enter" || ev.key === " ") {
505
+ ev.preventDefault();
506
+ this.inspect(n.id);
507
+ }
508
+ if (ev.key.toLowerCase() === "p") {
509
+ this.selected = n.id;
510
+ this.pin();
511
+ }
512
+ if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(ev.key)) {
513
+ ev.preventDefault();
514
+ this.freeze();
515
+ p.pinned = true;
516
+ p.x =
517
+ (p.x ?? 450) +
518
+ (ev.key === "ArrowRight" ? 10 : ev.key === "ArrowLeft" ? -10 : 0);
519
+ p.y =
520
+ (p.y ?? 240) +
521
+ (ev.key === "ArrowDown" ? 10 : ev.key === "ArrowUp" ? -10 : 0);
522
+ this.draw();
523
+ this.graph
524
+ .querySelector(`[data-node-index="${nodes.indexOf(n)}"]`)
525
+ ?.focus();
526
+ }
527
+ };
528
+ g.setAttribute("data-node-index", String(nodes.indexOf(n)));
529
+ g.onpointerdown = (ev) => {
530
+ this.freeze();
531
+ this.selected = n.id;
532
+ g.setPointerCapture(ev.pointerId);
533
+ p.pinned = true;
534
+ };
535
+ g.onpointermove = (ev) => {
536
+ if (!g.hasPointerCapture(ev.pointerId))
537
+ return;
538
+ const matrix = this.graph.getScreenCTM();
539
+ if (!matrix)
540
+ return;
541
+ const point = new DOMPoint(ev.clientX, ev.clientY).matrixTransform(matrix.inverse());
542
+ p.x = point.x;
543
+ p.y = point.y;
544
+ g.setAttribute("transform", `translate(${p.x},${p.y})`);
545
+ };
546
+ g.onpointerup = (ev) => {
547
+ if (g.hasPointerCapture(ev.pointerId)) {
548
+ g.releasePointerCapture(ev.pointerId);
549
+ this.inspect(n.id);
550
+ this.draw();
551
+ }
552
+ };
553
+ this.graph.append(g);
554
+ const button = html("button", `${n.type} · ${authority} · ${n.label.slice(0, 100)}`);
555
+ button.dataset.authority = authority;
556
+ button.setAttribute("data-focus-key", "list:" + n.id);
557
+ button.onclick = () => {
558
+ this.inspect(n.id);
559
+ this.draw();
560
+ };
561
+ this.list.append(button);
562
+ }
563
+ if (focused)
564
+ Array.from(this.root.querySelectorAll("[data-focus-key]"))
565
+ .find((n) => n.getAttribute("data-focus-key") === focused)
566
+ ?.focus({ preventScroll: true });
567
+ this.root.dataset.drawMs = String(performance.now() - drawStarted);
568
+ this.status.textContent = `${nodes.length} nodes / ${edges.length} edges shown from ${this.data.nodes.length} / ${this.data.edges.length} loaded. ${this.data.coverage ?? "Bounded authorized data"}. Limits: ${(this.data.truncation ?? []).join(", ") || "loaded window only"}.`;
569
+ }
570
+ }