trusty-memory 0.9.0

MCP server (stdio + HTTP/SSE) for trusty-memory
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<script>
  /*
   * Why: Issue #97 — give every palace a per-palace visual knowledge graph
   * so operators can spot clusters and inspect the auto-extracted triples
   * coming out of `memory_remember`. The existing `KG.svelte` is a flat
   * subject/triple table that hides topology; this view renders the same
   * data as a force-directed node-link diagram so the structure is visible
   * at a glance.
   * What: Fetches `GET /api/v1/palaces/{id}/kg/graph`, converts the triple
   * list into nodes+links, runs a lightweight d3-style force simulation
   * (implemented inline to avoid a heavy d3 import), and renders the result
   * into an SVG with labelled nodes and directional edges. Clicking a node
   * opens a side panel listing the incident edges and the source drawer ids
   * extracted from `drawer:<uuid>` nodes / triple subjects.
   * Test: open `#/palace/<id>/graph`, confirm nodes render, drag a node to
   * re-settle the layout, click a node and verify the side panel populates.
   */
  import { onDestroy, onMount } from 'svelte';
  import { api } from '../api.js';
  import { getRoute, navigate } from '../router.svelte.js';

  // Selected palace + payload.
  let palaceId = $state('');
  let triples = $state([]);
  let counts = $state({ node_count: 0, edge_count: 0, community_count: 0 });
  let loading = $state(true);
  let error = $state(null);
  let loadStartedAt = 0;
  let loadElapsedMs = $state(0);

  // Layout state — populated by `runLayout`. Both arrays are keyed by index
  // and mutated in-place by the simulation tick.
  let nodes = $state([]); // {id, label, x, y, vx, vy, fx, fy, kind, community}
  let links = $state([]); // {source, target, predicate}
  let selectedId = $state(null);
  let hoverId = $state(null);

  // Viewport sizing — recalculated on mount + window resize.
  let width = $state(900);
  let height = $state(600);
  let svgEl = $state(null);
  let resizeObserver;
  let simHandle = null;

  // Force simulation knobs.
  const LINK_DISTANCE = 90;
  const REPULSION = 1200;
  const CENTER_STRENGTH = 0.04;
  const DAMPING = 0.85;
  const MAX_STEPS = 200; // sim auto-stops after this many ticks
  const TICK_MS = 16; // ~60fps target

  onMount(() => {
    palaceId = palaceFromRoute();
    if (palaceId) loadGraph();
    const onResize = () => sizeFromContainer();
    window.addEventListener('resize', onResize);
    sizeFromContainer();
    return () => {
      window.removeEventListener('resize', onResize);
      stopSimulation();
    };
  });

  onDestroy(() => stopSimulation());

  // React to hash-route changes so a `navigate(...)` from another view
  // re-loads this view automatically.
  $effect(() => {
    const r = getRoute();
    const segs = r?.segments ?? [];
    let next = '';
    if (segs[0] === 'palace' && segs.length >= 2) next = segs[1];
    if (next && next !== palaceId) {
      palaceId = next;
      loadGraph();
    }
  });

  function palaceFromRoute() {
    const r = getRoute();
    const segs = r?.segments ?? [];
    if (segs[0] === 'palace' && segs.length >= 2) return decodeURIComponent(segs[1]);
    return '';
  }

  function sizeFromContainer() {
    if (!svgEl) return;
    const r = svgEl.parentElement?.getBoundingClientRect();
    if (r) {
      width = Math.max(400, Math.floor(r.width));
      // Keep the SVG within a sensible vertical budget so the side panel
      // remains visible. The card body itself can scroll if needed.
      height = Math.max(420, Math.floor(Math.min(800, window.innerHeight - 240)));
    }
  }

  async function loadGraph() {
    loading = true;
    error = null;
    selectedId = null;
    loadStartedAt = performance.now();
    try {
      const payload = await api.kgGraph(palaceId);
      triples = Array.isArray(payload?.triples) ? payload.triples : [];
      counts = {
        node_count: payload?.node_count ?? 0,
        edge_count: payload?.edge_count ?? 0,
        community_count: payload?.community_count ?? 0
      };
      buildLayout();
    } catch (e) {
      error = e.message || String(e);
      triples = [];
      nodes = [];
      links = [];
    } finally {
      loading = false;
      loadElapsedMs = Math.round(performance.now() - loadStartedAt);
    }
  }

  /*
   * Why: Convert the flat `Triple[]` list into the {nodes, links} shape
   * d3-force expects. Each distinct subject and object becomes a node;
   * each triple becomes one directed link.
   * What: Walks `triples`, deduplicating node ids via a Map; produces
   * `{nodes, links}` with stable string ids and assigns a `kind` field
   * (`drawer` / `tag` / `topic` / `room` / `other`) for color coding.
   * Test: visually — labels and edge directionality match the table view.
   */
  function buildLayout() {
    const nodeMap = new Map();
    const nextLinks = [];
    function ensureNode(label) {
      if (!label) return null;
      if (nodeMap.has(label)) return nodeMap.get(label);
      const kind = classify(label);
      const node = {
        id: label,
        label,
        kind,
        community: Math.abs(hashStr(label)) % Math.max(1, counts.community_count || 8),
        x: width / 2 + (Math.random() - 0.5) * 200,
        y: height / 2 + (Math.random() - 0.5) * 200,
        vx: 0,
        vy: 0,
        fx: null,
        fy: null
      };
      nodeMap.set(label, node);
      return node;
    }
    for (const t of triples) {
      const s = ensureNode(t.subject);
      const o = ensureNode(t.object);
      if (s && o && s !== o) {
        nextLinks.push({ source: s.id, target: o.id, predicate: t.predicate });
      }
    }
    nodes = Array.from(nodeMap.values());
    links = nextLinks;
    // Restart the simulation against the new graph.
    stopSimulation();
    runLayout();
  }

  function classify(label) {
    if (typeof label !== 'string') return 'other';
    if (label.startsWith('drawer:')) return 'drawer';
    if (label.startsWith('tag:')) return 'tag';
    if (label.startsWith('topic:')) return 'topic';
    if (label.startsWith('room:')) return 'room';
    return 'other';
  }

  /*
   * Why: Tiny deterministic hash so node colors stay stable across reloads
   * without pulling in an external dep.
   * What: 32-bit djb2 variant returning a signed integer.
   * Test: hashStr('rust') === hashStr('rust'); two distinct strings rarely
   * collide on the same modulus.
   */
  function hashStr(s) {
    let h = 5381;
    for (let i = 0; i < s.length; i++) {
      h = ((h << 5) + h) ^ s.charCodeAt(i);
    }
    return h | 0;
  }

  /*
   * Why: Inline a minimal force-directed layout (link + repulsion + center)
   * so we don't add a 100KB d3-force bundle for what is essentially three
   * loops. Modern browsers run this comfortably for the <500-triple target.
   * What: A `setInterval`-driven tick that updates `nodes[i].x/y` in place
   * and re-assigns `nodes` to trigger Svelte's reactivity. Stops itself
   * after `MAX_STEPS` ticks or when total kinetic energy falls below a
   * threshold.
   * Test: load a palace with at least 5 triples and watch the layout settle
   * within ~3 seconds.
   */
  function runLayout() {
    if (nodes.length === 0) return;
    let step = 0;
    simHandle = setInterval(() => {
      tick();
      step++;
      if (step >= MAX_STEPS) stopSimulation();
    }, TICK_MS);
  }

  function stopSimulation() {
    if (simHandle != null) {
      clearInterval(simHandle);
      simHandle = null;
    }
  }

  function tick() {
    if (nodes.length === 0) return;
    const nodeIndex = new Map();
    for (let i = 0; i < nodes.length; i++) nodeIndex.set(nodes[i].id, i);

    // Repulsion — O(n^2) pairwise. Fine for <500 nodes.
    for (let i = 0; i < nodes.length; i++) {
      const ni = nodes[i];
      for (let j = i + 1; j < nodes.length; j++) {
        const nj = nodes[j];
        const dx = nj.x - ni.x;
        const dy = nj.y - ni.y;
        let dist2 = dx * dx + dy * dy;
        if (dist2 < 1) dist2 = 1;
        const force = REPULSION / dist2;
        const dist = Math.sqrt(dist2);
        const fx = (dx / dist) * force;
        const fy = (dy / dist) * force;
        ni.vx -= fx;
        ni.vy -= fy;
        nj.vx += fx;
        nj.vy += fy;
      }
    }

    // Link spring — pull connected nodes toward LINK_DISTANCE apart.
    for (const lk of links) {
      const si = nodeIndex.get(lk.source);
      const ti = nodeIndex.get(lk.target);
      if (si == null || ti == null) continue;
      const a = nodes[si];
      const b = nodes[ti];
      const dx = b.x - a.x;
      const dy = b.y - a.y;
      const dist = Math.sqrt(dx * dx + dy * dy) || 1;
      const diff = (dist - LINK_DISTANCE) * 0.05;
      const fx = (dx / dist) * diff;
      const fy = (dy / dist) * diff;
      a.vx += fx;
      a.vy += fy;
      b.vx -= fx;
      b.vy -= fy;
    }

    // Centering — pull everything toward the canvas center so the layout
    // doesn't drift off-screen.
    const cx = width / 2;
    const cy = height / 2;
    for (const n of nodes) {
      n.vx += (cx - n.x) * CENTER_STRENGTH;
      n.vy += (cy - n.y) * CENTER_STRENGTH;
      // Apply velocity + damping.
      if (n.fx == null) n.x += n.vx;
      if (n.fy == null) n.y += n.vy;
      n.vx *= DAMPING;
      n.vy *= DAMPING;
    }

    // Trigger Svelte rerender.
    nodes = nodes;
  }

  /*
   * Why: Drag-to-pin lets the operator nudge a cluttered cluster into
   * place. Standard d3 idiom: on `mousedown` pin the node, on `mousemove`
   * move it, on `mouseup` release.
   * What: Pins by setting fx/fy; unpins by setting them back to null. The
   * `tick()` loop respects fx/fy when set, so dragged nodes stay put.
   * Test: drag a node — the rest of the graph reshapes around it.
   */
  let dragId = null;
  function onNodeDown(ev, id) {
    dragId = id;
    selectedId = id;
    const node = nodes.find((n) => n.id === id);
    if (node) {
      node.fx = node.x;
      node.fy = node.y;
    }
    ev.stopPropagation();
  }
  function onSvgMove(ev) {
    if (dragId == null) return;
    const pt = clientToSvg(ev.clientX, ev.clientY);
    const node = nodes.find((n) => n.id === dragId);
    if (node) {
      node.fx = pt.x;
      node.fy = pt.y;
      node.x = pt.x;
      node.y = pt.y;
      nodes = nodes;
    }
  }
  function onSvgUp() {
    if (dragId == null) return;
    const node = nodes.find((n) => n.id === dragId);
    if (node) {
      node.fx = null;
      node.fy = null;
    }
    dragId = null;
  }
  function clientToSvg(cx, cy) {
    if (!svgEl) return { x: cx, y: cy };
    const r = svgEl.getBoundingClientRect();
    return { x: cx - r.left, y: cy - r.top };
  }

  // Side-panel derived view: triples incident on the selected node, split
  // into outgoing and incoming for clarity. Also surfaces source drawer
  // ids extracted from `drawer:<uuid>` references so the operator can jump
  // from a node back to the drawer that produced it.
  let incident = $derived.by(() => {
    if (!selectedId) return { outgoing: [], incoming: [], drawerIds: [] };
    const outgoing = triples.filter((t) => t.subject === selectedId);
    const incoming = triples.filter((t) => t.object === selectedId);
    const drawerIds = new Set();
    for (const t of [...outgoing, ...incoming]) {
      if (typeof t.subject === 'string' && t.subject.startsWith('drawer:')) {
        drawerIds.add(t.subject.slice('drawer:'.length));
      }
      if (typeof t.object === 'string' && t.object.startsWith('drawer:')) {
        drawerIds.add(t.object.slice('drawer:'.length));
      }
    }
    return {
      outgoing,
      incoming,
      drawerIds: Array.from(drawerIds)
    };
  });

  function colorFor(node) {
    const palette = [
      '#6366f1',
      '#ec4899',
      '#10b981',
      '#f59e0b',
      '#0ea5e9',
      '#8b5cf6',
      '#14b8a6',
      '#f43f5e'
    ];
    if (counts.community_count > 0) {
      return palette[node.community % palette.length];
    }
    // Color-by-kind fallback when no Louvain pass has run.
    switch (node.kind) {
      case 'drawer':
        return '#6366f1';
      case 'tag':
        return '#10b981';
      case 'topic':
        return '#f59e0b';
      case 'room':
        return '#ec4899';
      default:
        return '#64748b';
    }
  }
</script>

<div class="page">
  <div class="header">
    <h1 class="page-title">Knowledge Graph</h1>
    <div class="header-meta">
      {#if palaceId}
        <span class="badge badge-info">palace: {palaceId}</span>
      {/if}
      <span class="badge badge-muted">{counts.node_count} nodes</span>
      <span class="badge badge-muted">{counts.edge_count} edges</span>
      {#if loadElapsedMs > 0 && !loading}
        <span class="badge badge-muted" title="API + layout time">
          loaded in {loadElapsedMs}ms
        </span>
      {/if}
      <button
        type="button"
        class="back-link"
        onclick={() => navigate('/palaces')}>
        ← back to palaces
      </button>
    </div>
  </div>

  {#if loading}
    <div class="state">Loading graph…</div>
  {:else if error}
    <div class="state state-error">{error}</div>
  {:else if nodes.length === 0}
    <div class="state">
      This palace has no KG triples yet. Write a memory or run
      <code>trusty-memory kg-rebuild --palace {palaceId}</code> to back-fill.
    </div>
  {:else}
    <div class="layout">
      <div class="canvas">
        <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
        <svg
          bind:this={svgEl}
          width={width}
          height={height}
          role="application"
          aria-label="Per-palace knowledge graph"
          onmousemove={onSvgMove}
          onmouseup={onSvgUp}
          onmouseleave={onSvgUp}>
          <defs>
            <marker
              id="arrow"
              viewBox="0 0 10 10"
              refX="9"
              refY="5"
              markerWidth="6"
              markerHeight="6"
              orient="auto-start-reverse">
              <path d="M0,0 L10,5 L0,10 z" fill="#94a3b8" />
            </marker>
          </defs>
          {#each links as l (l.source + '|' + l.predicate + '|' + l.target)}
            {@const a = nodes.find((n) => n.id === l.source)}
            {@const b = nodes.find((n) => n.id === l.target)}
            {#if a && b}
              <line
                x1={a.x}
                y1={a.y}
                x2={b.x}
                y2={b.y}
                stroke="#94a3b8"
                stroke-width="1"
                stroke-opacity="0.55"
                marker-end="url(#arrow)" />
            {/if}
          {/each}
          {#each nodes as n (n.id)}
            <g
              transform={`translate(${n.x},${n.y})`}
              onmousedown={(ev) => onNodeDown(ev, n.id)}
              onmouseenter={() => (hoverId = n.id)}
              onmouseleave={() => (hoverId = null)}
              role="button"
              tabindex="0"
              class="node">
              <circle
                r={selectedId === n.id ? 9 : 6}
                fill={colorFor(n)}
                stroke={selectedId === n.id ? '#0f172a' : '#fff'}
                stroke-width={selectedId === n.id ? 2 : 1} />
              {#if selectedId === n.id || hoverId === n.id}
                <text
                  x="10"
                  y="4"
                  font-size="11"
                  fill="#0f172a"
                  paint-order="stroke"
                  stroke="#fff"
                  stroke-width="3">
                  {n.label}
                </text>
              {/if}
            </g>
          {/each}
        </svg>
      </div>
      <aside class="side-panel">
        {#if selectedId}
          <div class="side-title">{selectedId}</div>
          <div class="side-sub">
            {incident.outgoing.length} outgoing · {incident.incoming.length} incoming
          </div>
          {#if incident.outgoing.length > 0}
            <div class="side-section">
              <div class="side-section-title">Outgoing</div>
              <ul class="side-list">
                {#each incident.outgoing as t (t.subject + t.predicate + t.object)}
                  <li>
                    <span class="pred">{t.predicate}</span>
                    <span class="arrow">→</span>
                    <span class="obj">{t.object}</span>
                  </li>
                {/each}
              </ul>
            </div>
          {/if}
          {#if incident.incoming.length > 0}
            <div class="side-section">
              <div class="side-section-title">Incoming</div>
              <ul class="side-list">
                {#each incident.incoming as t (t.subject + t.predicate + t.object)}
                  <li>
                    <span class="obj">{t.subject}</span>
                    <span class="arrow">→</span>
                    <span class="pred">{t.predicate}</span>
                  </li>
                {/each}
              </ul>
            </div>
          {/if}
          {#if incident.drawerIds.length > 0}
            <div class="side-section">
              <div class="side-section-title">Source drawers</div>
              <ul class="side-list">
                {#each incident.drawerIds as did}
                  <li>
                    <code>{did}</code>
                  </li>
                {/each}
              </ul>
            </div>
          {/if}
        {:else}
          <div class="side-empty">
            Click a node to inspect its edges and the source drawers that
            produced them.
          </div>
        {/if}
      </aside>
    </div>
  {/if}
</div>

<style>
  .page-title {
    font-size: var(--trusty-fs-xl);
    margin: 0 0 var(--trusty-space-3) 0;
    font-weight: 600;
  }
  .header {
    margin-bottom: var(--trusty-space-4);
  }
  .header-meta {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
  }
  .back-link {
    background: transparent;
    border: 1px solid var(--trusty-border, #e5e7eb);
    border-radius: 4px;
    padding: 2px 8px;
    font-size: var(--trusty-fs-xs);
    cursor: pointer;
    color: var(--trusty-text-secondary, #6b7280);
    font-family: inherit;
  }
  .back-link:hover {
    background: var(--trusty-bg-subtle, #f8fafc);
  }
  .state {
    padding: var(--trusty-space-4);
    background: var(--trusty-bg-subtle, #f8fafc);
    border-radius: var(--trusty-radius, 6px);
    color: var(--trusty-text-secondary, #6b7280);
  }
  .state-error {
    color: var(--trusty-danger, #dc2626);
    background: var(--trusty-danger-soft, #fef2f2);
  }
  .layout {
    display: grid;
    grid-template-columns: 1fr 320px;
    gap: var(--trusty-space-4);
    align-items: start;
  }
  .canvas {
    border: 1px solid var(--trusty-border, #e5e7eb);
    border-radius: var(--trusty-radius, 6px);
    background: #fff;
    overflow: hidden;
    min-height: 420px;
  }
  .canvas svg {
    display: block;
    width: 100%;
    height: auto;
    user-select: none;
  }
  .node {
    cursor: pointer;
  }
  .side-panel {
    border: 1px solid var(--trusty-border, #e5e7eb);
    border-radius: var(--trusty-radius, 6px);
    padding: var(--trusty-space-3);
    background: #fff;
    max-height: 80vh;
    overflow-y: auto;
  }
  .side-title {
    font-weight: 600;
    font-size: var(--trusty-fs-sm);
    word-break: break-all;
    margin-bottom: 2px;
  }
  .side-sub {
    font-size: var(--trusty-fs-xs);
    color: var(--trusty-text-secondary, #6b7280);
    margin-bottom: var(--trusty-space-3);
  }
  .side-section {
    margin-top: var(--trusty-space-3);
  }
  .side-section-title {
    font-size: var(--trusty-fs-xs);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--trusty-text-muted, #94a3b8);
    margin-bottom: 4px;
  }
  .side-list {
    list-style: none;
    padding: 0;
    margin: 0;
    font-size: var(--trusty-fs-xs);
  }
  .side-list li {
    padding: 3px 0;
    border-bottom: 1px dashed var(--trusty-border, #e5e7eb);
    word-break: break-all;
  }
  .side-empty {
    font-size: var(--trusty-fs-xs);
    color: var(--trusty-text-secondary, #6b7280);
  }
  .pred {
    color: var(--trusty-accent, #6366f1);
    font-weight: 500;
  }
  .arrow {
    color: var(--trusty-text-muted, #94a3b8);
    margin: 0 4px;
  }
  .obj {
    color: var(--trusty-text-primary, #0f172a);
  }
</style>