@ijfw/memory-server 1.3.0

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 (106) hide show
  1. package/bin/ijfw +27 -0
  2. package/bin/ijfw-dashboard +180 -0
  3. package/bin/ijfw-dispatch-plan +41 -0
  4. package/bin/ijfw-memorize +273 -0
  5. package/bin/ijfw-memory +51 -0
  6. package/fixtures/demo-target.js +28 -0
  7. package/package.json +53 -0
  8. package/src/api-client.js +190 -0
  9. package/src/audit-roster.js +315 -0
  10. package/src/caps.js +37 -0
  11. package/src/cold-scan-runner.mjs +37 -0
  12. package/src/compute/edges.js +155 -0
  13. package/src/compute/extract.js +560 -0
  14. package/src/compute/fts5.js +420 -0
  15. package/src/compute/graph-auto-index.js +191 -0
  16. package/src/compute/graph-lock.js +114 -0
  17. package/src/compute/index.js +18 -0
  18. package/src/compute/migration-runner.js +116 -0
  19. package/src/compute/migrations/001-initial.js +23 -0
  20. package/src/compute/migrations/002-porter-stemming-source.js +139 -0
  21. package/src/compute/migrations/003-tier-semantic.js +69 -0
  22. package/src/compute/migrations/004-kg-tables.js +83 -0
  23. package/src/compute/migrations/005-stale-candidate.js +72 -0
  24. package/src/compute/python-resolver.js +106 -0
  25. package/src/compute/runner-vm.js +185 -0
  26. package/src/compute/runner.js +416 -0
  27. package/src/compute/sandbox-detect.js +122 -0
  28. package/src/compute/sandbox-linux.js +164 -0
  29. package/src/compute/sandbox-macos.js +167 -0
  30. package/src/compute/sandbox-windows.js +63 -0
  31. package/src/compute/schema.sql +118 -0
  32. package/src/compute/staleness.js +239 -0
  33. package/src/compute/synonyms.js +367 -0
  34. package/src/compute/traverse.js +180 -0
  35. package/src/cost/aggregator.js +229 -0
  36. package/src/cost/pricing.js +134 -0
  37. package/src/cost/readers/claude.js +179 -0
  38. package/src/cost/readers/codex.js +131 -0
  39. package/src/cost/readers/gemini.js +111 -0
  40. package/src/cost/savings.js +243 -0
  41. package/src/cross-dispatcher.js +437 -0
  42. package/src/cross-orchestrator-cli.js +1885 -0
  43. package/src/cross-orchestrator.js +598 -0
  44. package/src/cross-project-search.js +114 -0
  45. package/src/dashboard-client.html +1180 -0
  46. package/src/dashboard-server.js +895 -0
  47. package/src/design-companion.js +81 -0
  48. package/src/dispatch/colon-syntax.js +732 -0
  49. package/src/dispatch-planner.js +235 -0
  50. package/src/dream/cooldown.js +105 -0
  51. package/src/dream/runner.mjs +373 -0
  52. package/src/dream/staleness-wiring.js +195 -0
  53. package/src/feedback-detector.js +57 -0
  54. package/src/hero-line.js +115 -0
  55. package/src/importers/claude-mem.js +152 -0
  56. package/src/importers/cli.js +311 -0
  57. package/src/importers/common.js +84 -0
  58. package/src/importers/discover.js +235 -0
  59. package/src/importers/rtk.js +107 -0
  60. package/src/intent-router.js +221 -0
  61. package/src/lib/atomic-io.js +201 -0
  62. package/src/lib/cache.js +33 -0
  63. package/src/lib/npm-view.js +104 -0
  64. package/src/lib/status-card.js +95 -0
  65. package/src/lib/token.js +85 -0
  66. package/src/memory/fts5.js +349 -0
  67. package/src/memory/migration-runner.js +116 -0
  68. package/src/memory/migrations/001-fts5-init.js +26 -0
  69. package/src/memory/migrations/002-tier-semantic.js +60 -0
  70. package/src/memory/migrations/003-stale-candidate.js +60 -0
  71. package/src/memory/reader.js +300 -0
  72. package/src/memory/recall-counter.js +76 -0
  73. package/src/memory/schema.sql +79 -0
  74. package/src/memory/search.js +431 -0
  75. package/src/memory/staleness.js +237 -0
  76. package/src/memory/tier-promotion.js +377 -0
  77. package/src/memory/tokenize.js +63 -0
  78. package/src/project-type-detector.js +866 -0
  79. package/src/prompt-check.js +171 -0
  80. package/src/ralph-allowlist.js +88 -0
  81. package/src/receipts.js +129 -0
  82. package/src/redactor.js +107 -0
  83. package/src/sandbox.js +275 -0
  84. package/src/sanitizer.js +69 -0
  85. package/src/scan-resume.js +167 -0
  86. package/src/schema.js +82 -0
  87. package/src/search-bm25.js +108 -0
  88. package/src/server.js +1414 -0
  89. package/src/swarm-config.js +80 -0
  90. package/src/trident/dispatch.js +211 -0
  91. package/src/trident/lens-health.js +253 -0
  92. package/src/update-apply.js +79 -0
  93. package/src/update-check.js +136 -0
  94. package/src/vectors.js +178 -0
  95. package/templates/design/bento-grid.md +84 -0
  96. package/templates/design/brutalist-luxe.md +82 -0
  97. package/templates/design/cinematic-dark.md +82 -0
  98. package/templates/design/data-dense-dashboard.md +88 -0
  99. package/templates/design/editorial-warm.md +81 -0
  100. package/templates/design/glassmorphic.md +84 -0
  101. package/templates/design/magazine-editorial.md +84 -0
  102. package/templates/design/maximalist-vibrant.md +85 -0
  103. package/templates/design/neo-swiss-tech.md +85 -0
  104. package/templates/design/swiss-minimal.md +80 -0
  105. package/templates/design/terminal-native.md +83 -0
  106. package/templates/design/warm-organic.md +84 -0
@@ -0,0 +1,108 @@
1
+ // BM25 search over memory files (W3.1 / H4).
2
+ // Pure-JS Okapi BM25 (k1=1.2, b=0.75). FTS5-equivalent ranking + phrase
3
+ // matching without a native SQLite dependency. Linear-scan at query time;
4
+ // scales to ~10k entries before an inverted-index cache would pay off.
5
+
6
+ const STOPWORDS = new Set([
7
+ 'a','an','the','is','are','was','were','be','been','being','do','does','did',
8
+ 'of','in','on','at','to','for','with','by','from','as','that','this','it',
9
+ 'and','or','but','if','so','not','no','yes','we','i','you','they','he','she',
10
+ ]);
11
+
12
+ export function tokenize(s) {
13
+ if (typeof s !== 'string' || !s) return [];
14
+ return s.toLowerCase()
15
+ .replace(/[^a-z0-9_\-.\s]/g, ' ')
16
+ .split(/\s+/)
17
+ .filter(t => t && t.length >= 2 && !STOPWORDS.has(t));
18
+ }
19
+
20
+ function extractPhrases(query) {
21
+ const phrases = [];
22
+ const re = /"([^"]+)"/g;
23
+ let m;
24
+ while ((m = re.exec(query)) !== null) phrases.push(m[1].toLowerCase());
25
+ return phrases;
26
+ }
27
+
28
+ // Okapi BM25 over a document array.
29
+ // docs: [{ id, text, meta? }]
30
+ // returns [{ id, score, meta, snippet }]
31
+ export function searchCorpus(query, docs, opts = {}) {
32
+ const k1 = opts.k1 ?? 1.2;
33
+ const b = opts.b ?? 0.75;
34
+ const limit = opts.limit ?? 10;
35
+ if (!query || !docs || docs.length === 0) return [];
36
+
37
+ const phrases = extractPhrases(query);
38
+ const qBare = query.replace(/"[^"]+"/g, ' ');
39
+ const qTokens = tokenize(qBare);
40
+ if (qTokens.length === 0 && phrases.length === 0) return [];
41
+
42
+ const docTokens = docs.map(d => tokenize(d.text));
43
+ const docLens = docTokens.map(ts => ts.length);
44
+ const avgDl = docLens.reduce((s, n) => s + n, 0) / Math.max(1, docLens.length);
45
+
46
+ const df = new Map();
47
+ for (const t of qTokens) {
48
+ if (df.has(t)) continue;
49
+ let n = 0;
50
+ for (const doc of docTokens) if (doc.includes(t)) n++;
51
+ df.set(t, n);
52
+ }
53
+ const N = docs.length;
54
+
55
+ const scored = [];
56
+ for (let i = 0; i < docs.length; i++) {
57
+ const doc = docs[i];
58
+ const lowerText = doc.text.toLowerCase();
59
+ let phraseOk = true;
60
+ for (const p of phrases) {
61
+ if (!lowerText.includes(p)) { phraseOk = false; break; }
62
+ }
63
+ if (!phraseOk) continue;
64
+
65
+ const dLen = docLens[i];
66
+ let score = 0;
67
+ const tf = new Map();
68
+ for (const t of docTokens[i]) tf.set(t, (tf.get(t) || 0) + 1);
69
+ for (const t of qTokens) {
70
+ const f = tf.get(t);
71
+ if (!f) continue;
72
+ const n = df.get(t) || 0;
73
+ const idf = Math.log(1 + (N - n + 0.5) / (n + 0.5));
74
+ const denom = f + k1 * (1 - b + (b * dLen) / (avgDl || 1));
75
+ score += idf * ((f * (k1 + 1)) / denom);
76
+ }
77
+ for (const p of phrases) {
78
+ const phraseTokens = tokenize(p);
79
+ for (const t of phraseTokens) {
80
+ const n = df.get(t) ?? docTokens.reduce((s, d) => s + (d.includes(t) ? 1 : 0), 0);
81
+ const idf = Math.log(1 + (N - n + 0.5) / (n + 0.5));
82
+ score += idf * 0.5;
83
+ }
84
+ }
85
+ if (score > 0) {
86
+ scored.push({ id: doc.id, score, meta: doc.meta, snippet: snippet(doc.text, qTokens, 160) });
87
+ }
88
+ }
89
+
90
+ scored.sort((a, b) => b.score - a.score);
91
+ return scored.slice(0, limit);
92
+ }
93
+
94
+ function snippet(text, qTokens, width) {
95
+ if (!text) return '';
96
+ const lower = text.toLowerCase();
97
+ let bestPos = -1;
98
+ for (const t of qTokens) {
99
+ const p = lower.indexOf(t);
100
+ if (p >= 0 && (bestPos < 0 || p < bestPos)) bestPos = p;
101
+ }
102
+ if (bestPos < 0) return text.slice(0, width).replace(/\s+/g, ' ');
103
+ const start = Math.max(0, bestPos - 40);
104
+ const end = Math.min(text.length, start + width);
105
+ const prefix = start > 0 ? '…' : '';
106
+ const suffix = end < text.length ? '…' : '';
107
+ return prefix + text.slice(start, end).replace(/\s+/g, ' ') + suffix;
108
+ }