@ulrichc1/sparn 1.0.1 → 1.1.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.
@@ -0,0 +1,882 @@
1
+ "use strict";
2
+
3
+ // src/daemon/index.ts
4
+ var import_node_fs3 = require("fs");
5
+
6
+ // src/daemon/session-watcher.ts
7
+ var import_node_fs2 = require("fs");
8
+ var import_node_os = require("os");
9
+ var import_node_path = require("path");
10
+
11
+ // src/utils/context-parser.ts
12
+ var import_node_crypto2 = require("crypto");
13
+
14
+ // src/utils/hash.ts
15
+ var import_node_crypto = require("crypto");
16
+ function hashContent(content) {
17
+ return (0, import_node_crypto.createHash)("sha256").update(content, "utf8").digest("hex");
18
+ }
19
+
20
+ // src/utils/context-parser.ts
21
+ function parseClaudeCodeContext(context) {
22
+ const entries = [];
23
+ const now = Date.now();
24
+ const lines = context.split("\n");
25
+ let currentBlock = [];
26
+ let blockType = "other";
27
+ for (const line of lines) {
28
+ const trimmed = line.trim();
29
+ if (trimmed.startsWith("User:") || trimmed.startsWith("Assistant:")) {
30
+ if (currentBlock.length > 0) {
31
+ entries.push(createEntry(currentBlock.join("\n"), blockType, now));
32
+ currentBlock = [];
33
+ }
34
+ blockType = "conversation";
35
+ currentBlock.push(line);
36
+ } else if (trimmed.includes("<function_calls>") || trimmed.includes("<invoke>") || trimmed.includes("<tool_use>")) {
37
+ if (currentBlock.length > 0) {
38
+ entries.push(createEntry(currentBlock.join("\n"), blockType, now));
39
+ currentBlock = [];
40
+ }
41
+ blockType = "tool";
42
+ currentBlock.push(line);
43
+ } else if (trimmed.includes("<function_results>") || trimmed.includes("</function_results>")) {
44
+ if (currentBlock.length > 0 && blockType !== "result") {
45
+ entries.push(createEntry(currentBlock.join("\n"), blockType, now));
46
+ currentBlock = [];
47
+ }
48
+ blockType = "result";
49
+ currentBlock.push(line);
50
+ } else if (currentBlock.length > 0) {
51
+ currentBlock.push(line);
52
+ } else if (trimmed.length > 0) {
53
+ currentBlock.push(line);
54
+ blockType = "other";
55
+ }
56
+ }
57
+ if (currentBlock.length > 0) {
58
+ entries.push(createEntry(currentBlock.join("\n"), blockType, now));
59
+ }
60
+ return entries.filter((e) => e.content.trim().length > 0);
61
+ }
62
+ function createEntry(content, type, baseTime) {
63
+ const tags = [type];
64
+ let initialScore = 0.5;
65
+ if (type === "conversation") initialScore = 0.8;
66
+ if (type === "tool") initialScore = 0.7;
67
+ if (type === "result") initialScore = 0.4;
68
+ return {
69
+ id: (0, import_node_crypto2.randomUUID)(),
70
+ content,
71
+ hash: hashContent(content),
72
+ timestamp: baseTime,
73
+ score: initialScore,
74
+ state: initialScore > 0.7 ? "active" : initialScore > 0.3 ? "ready" : "silent",
75
+ ttl: 24 * 3600,
76
+ // 24 hours default
77
+ accessCount: 0,
78
+ tags,
79
+ metadata: { type },
80
+ isBTSP: false
81
+ };
82
+ }
83
+
84
+ // src/utils/tokenizer.ts
85
+ function estimateTokens(text) {
86
+ if (!text || text.length === 0) {
87
+ return 0;
88
+ }
89
+ const words = text.split(/\s+/).filter((w) => w.length > 0);
90
+ const wordCount = words.length;
91
+ const charCount = text.length;
92
+ const charEstimate = Math.ceil(charCount / 4);
93
+ const wordEstimate = Math.ceil(wordCount * 0.75);
94
+ return Math.max(wordEstimate, charEstimate);
95
+ }
96
+
97
+ // src/core/engram-scorer.ts
98
+ function createEngramScorer(config2) {
99
+ const { defaultTTL } = config2;
100
+ function calculateDecay(ageInSeconds, ttlInSeconds) {
101
+ if (ttlInSeconds === 0) return 1;
102
+ if (ageInSeconds <= 0) return 0;
103
+ const ratio = ageInSeconds / ttlInSeconds;
104
+ const decay = 1 - Math.exp(-ratio);
105
+ return Math.max(0, Math.min(1, decay));
106
+ }
107
+ function calculateScore(entry, currentTime = Date.now()) {
108
+ const ageInMilliseconds = currentTime - entry.timestamp;
109
+ const ageInSeconds = Math.max(0, ageInMilliseconds / 1e3);
110
+ const decay = calculateDecay(ageInSeconds, entry.ttl);
111
+ let score = entry.score * (1 - decay);
112
+ if (entry.accessCount > 0) {
113
+ const accessBonus = Math.log(entry.accessCount + 1) * 0.1;
114
+ score = Math.min(1, score + accessBonus);
115
+ }
116
+ if (entry.isBTSP) {
117
+ score = Math.max(score, 0.9);
118
+ }
119
+ return Math.max(0, Math.min(1, score));
120
+ }
121
+ function refreshTTL(entry) {
122
+ return {
123
+ ...entry,
124
+ ttl: defaultTTL * 3600,
125
+ // Convert hours to seconds
126
+ timestamp: Date.now()
127
+ };
128
+ }
129
+ return {
130
+ calculateScore,
131
+ refreshTTL,
132
+ calculateDecay
133
+ };
134
+ }
135
+
136
+ // src/core/budget-pruner.ts
137
+ function createBudgetPruner(config2) {
138
+ const { tokenBudget, decay } = config2;
139
+ const engramScorer = createEngramScorer(decay);
140
+ function tokenize(text) {
141
+ return text.toLowerCase().split(/\s+/).filter((word) => word.length > 0);
142
+ }
143
+ function calculateTF(term, tokens) {
144
+ const count = tokens.filter((t) => t === term).length;
145
+ return Math.sqrt(count);
146
+ }
147
+ function calculateIDF(term, allEntries) {
148
+ const totalDocs = allEntries.length;
149
+ const docsWithTerm = allEntries.filter((entry) => {
150
+ const tokens = tokenize(entry.content);
151
+ return tokens.includes(term);
152
+ }).length;
153
+ if (docsWithTerm === 0) return 0;
154
+ return Math.log(totalDocs / docsWithTerm);
155
+ }
156
+ function calculateTFIDF(entry, allEntries) {
157
+ const tokens = tokenize(entry.content);
158
+ if (tokens.length === 0) return 0;
159
+ const uniqueTerms = [...new Set(tokens)];
160
+ let totalScore = 0;
161
+ for (const term of uniqueTerms) {
162
+ const tf = calculateTF(term, tokens);
163
+ const idf = calculateIDF(term, allEntries);
164
+ totalScore += tf * idf;
165
+ }
166
+ return totalScore / tokens.length;
167
+ }
168
+ function getStateMultiplier(entry) {
169
+ if (entry.isBTSP) return 2;
170
+ switch (entry.state) {
171
+ case "active":
172
+ return 2;
173
+ case "ready":
174
+ return 1;
175
+ case "silent":
176
+ return 0.5;
177
+ default:
178
+ return 1;
179
+ }
180
+ }
181
+ function priorityScore(entry, allEntries) {
182
+ const tfidf = calculateTFIDF(entry, allEntries);
183
+ const currentScore = engramScorer.calculateScore(entry);
184
+ const engramDecay = 1 - currentScore;
185
+ const stateMultiplier = getStateMultiplier(entry);
186
+ return tfidf * (1 - engramDecay) * stateMultiplier;
187
+ }
188
+ function pruneToFit(entries, budget = tokenBudget) {
189
+ if (entries.length === 0) {
190
+ return {
191
+ kept: [],
192
+ removed: [],
193
+ originalTokens: 0,
194
+ prunedTokens: 0,
195
+ budgetUtilization: 0
196
+ };
197
+ }
198
+ const originalTokens = entries.reduce((sum, e) => sum + estimateTokens(e.content), 0);
199
+ const btspEntries = entries.filter((e) => e.isBTSP);
200
+ const regularEntries = entries.filter((e) => !e.isBTSP);
201
+ const btspTokens = btspEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);
202
+ const scored = regularEntries.map((entry) => ({
203
+ entry,
204
+ score: priorityScore(entry, entries),
205
+ tokens: estimateTokens(entry.content)
206
+ }));
207
+ scored.sort((a, b) => b.score - a.score);
208
+ const kept = [...btspEntries];
209
+ const removed = [];
210
+ let currentTokens = btspTokens;
211
+ for (const item of scored) {
212
+ if (currentTokens + item.tokens <= budget) {
213
+ kept.push(item.entry);
214
+ currentTokens += item.tokens;
215
+ } else {
216
+ removed.push(item.entry);
217
+ }
218
+ }
219
+ const budgetUtilization = budget > 0 ? currentTokens / budget : 0;
220
+ return {
221
+ kept,
222
+ removed,
223
+ originalTokens,
224
+ prunedTokens: currentTokens,
225
+ budgetUtilization
226
+ };
227
+ }
228
+ return {
229
+ pruneToFit,
230
+ priorityScore
231
+ };
232
+ }
233
+
234
+ // src/core/metrics.ts
235
+ function createMetricsCollector() {
236
+ const optimizations = [];
237
+ let daemonMetrics = {
238
+ startTime: Date.now(),
239
+ sessionsWatched: 0,
240
+ totalOptimizations: 0,
241
+ totalTokensSaved: 0,
242
+ averageLatency: 0,
243
+ memoryUsage: 0
244
+ };
245
+ let cacheHits = 0;
246
+ let cacheMisses = 0;
247
+ function recordOptimization(metric) {
248
+ optimizations.push(metric);
249
+ daemonMetrics.totalOptimizations++;
250
+ daemonMetrics.totalTokensSaved += metric.tokensBefore - metric.tokensAfter;
251
+ if (metric.cacheHitRate > 0) {
252
+ const hits = Math.round(metric.entriesProcessed * metric.cacheHitRate);
253
+ cacheHits += hits;
254
+ cacheMisses += metric.entriesProcessed - hits;
255
+ }
256
+ daemonMetrics.averageLatency = (daemonMetrics.averageLatency * (daemonMetrics.totalOptimizations - 1) + metric.duration) / daemonMetrics.totalOptimizations;
257
+ if (optimizations.length > 1e3) {
258
+ optimizations.shift();
259
+ }
260
+ }
261
+ function updateDaemon(metric) {
262
+ daemonMetrics = {
263
+ ...daemonMetrics,
264
+ ...metric
265
+ };
266
+ }
267
+ function calculatePercentile(values, percentile) {
268
+ if (values.length === 0) return 0;
269
+ const sorted = [...values].sort((a, b) => a - b);
270
+ const index = Math.ceil(percentile / 100 * sorted.length) - 1;
271
+ return sorted[index] || 0;
272
+ }
273
+ function getSnapshot() {
274
+ const totalRuns = optimizations.length;
275
+ const totalDuration = optimizations.reduce((sum, m) => sum + m.duration, 0);
276
+ const totalTokensSaved = optimizations.reduce(
277
+ (sum, m) => sum + (m.tokensBefore - m.tokensAfter),
278
+ 0
279
+ );
280
+ const totalTokensBefore = optimizations.reduce((sum, m) => sum + m.tokensBefore, 0);
281
+ const averageReduction = totalTokensBefore > 0 ? totalTokensSaved / totalTokensBefore : 0;
282
+ const durations = optimizations.map((m) => m.duration);
283
+ const totalCacheQueries = cacheHits + cacheMisses;
284
+ const hitRate = totalCacheQueries > 0 ? cacheHits / totalCacheQueries : 0;
285
+ return {
286
+ timestamp: Date.now(),
287
+ optimization: {
288
+ totalRuns,
289
+ totalDuration,
290
+ totalTokensSaved,
291
+ averageReduction,
292
+ p50Latency: calculatePercentile(durations, 50),
293
+ p95Latency: calculatePercentile(durations, 95),
294
+ p99Latency: calculatePercentile(durations, 99)
295
+ },
296
+ cache: {
297
+ hitRate,
298
+ totalHits: cacheHits,
299
+ totalMisses: cacheMisses,
300
+ size: optimizations.reduce((sum, m) => sum + m.entriesKept, 0)
301
+ },
302
+ daemon: {
303
+ uptime: Date.now() - daemonMetrics.startTime,
304
+ sessionsWatched: daemonMetrics.sessionsWatched,
305
+ memoryUsage: daemonMetrics.memoryUsage
306
+ }
307
+ };
308
+ }
309
+ function exportMetrics() {
310
+ return JSON.stringify(getSnapshot(), null, 2);
311
+ }
312
+ function reset() {
313
+ optimizations.length = 0;
314
+ cacheHits = 0;
315
+ cacheMisses = 0;
316
+ daemonMetrics = {
317
+ startTime: Date.now(),
318
+ sessionsWatched: 0,
319
+ totalOptimizations: 0,
320
+ totalTokensSaved: 0,
321
+ averageLatency: 0,
322
+ memoryUsage: 0
323
+ };
324
+ }
325
+ return {
326
+ recordOptimization,
327
+ updateDaemon,
328
+ getSnapshot,
329
+ export: exportMetrics,
330
+ reset
331
+ };
332
+ }
333
+ var globalMetrics = null;
334
+ function getMetrics() {
335
+ if (!globalMetrics) {
336
+ globalMetrics = createMetricsCollector();
337
+ }
338
+ return globalMetrics;
339
+ }
340
+
341
+ // src/core/incremental-optimizer.ts
342
+ function createIncrementalOptimizer(config2) {
343
+ const pruner = createBudgetPruner(config2);
344
+ const { fullOptimizationInterval } = config2;
345
+ let state = {
346
+ entryCache: /* @__PURE__ */ new Map(),
347
+ documentFrequency: /* @__PURE__ */ new Map(),
348
+ totalDocuments: 0,
349
+ updateCount: 0,
350
+ lastFullOptimization: Date.now()
351
+ };
352
+ function tokenize(text) {
353
+ return text.toLowerCase().split(/\s+/).filter((word) => word.length > 0);
354
+ }
355
+ function updateDocumentFrequency(entries, remove = false) {
356
+ for (const entry of entries) {
357
+ const tokens = tokenize(entry.content);
358
+ const uniqueTerms = [...new Set(tokens)];
359
+ for (const term of uniqueTerms) {
360
+ const current = state.documentFrequency.get(term) || 0;
361
+ const updated = remove ? Math.max(0, current - 1) : current + 1;
362
+ if (updated === 0) {
363
+ state.documentFrequency.delete(term);
364
+ } else {
365
+ state.documentFrequency.set(term, updated);
366
+ }
367
+ }
368
+ }
369
+ state.totalDocuments += remove ? -entries.length : entries.length;
370
+ state.totalDocuments = Math.max(0, state.totalDocuments);
371
+ }
372
+ function getCachedEntry(hash) {
373
+ const cached = state.entryCache.get(hash);
374
+ if (!cached) return null;
375
+ return cached.entry;
376
+ }
377
+ function cacheEntry(entry, score) {
378
+ state.entryCache.set(entry.hash, {
379
+ entry,
380
+ score,
381
+ timestamp: Date.now()
382
+ });
383
+ }
384
+ function optimizeIncremental(newEntries, budget) {
385
+ const startTime = Date.now();
386
+ state.updateCount++;
387
+ if (state.updateCount >= fullOptimizationInterval) {
388
+ const allEntries2 = Array.from(state.entryCache.values()).map((c) => c.entry);
389
+ return optimizeFull([...allEntries2, ...newEntries], budget);
390
+ }
391
+ const uncachedEntries = [];
392
+ const cachedEntries = [];
393
+ for (const entry of newEntries) {
394
+ const cached = getCachedEntry(entry.hash);
395
+ if (cached) {
396
+ cachedEntries.push(cached);
397
+ } else {
398
+ uncachedEntries.push(entry);
399
+ }
400
+ }
401
+ if (uncachedEntries.length > 0) {
402
+ updateDocumentFrequency(uncachedEntries, false);
403
+ }
404
+ const allEntries = [...cachedEntries, ...uncachedEntries];
405
+ for (const entry of uncachedEntries) {
406
+ const score = pruner.priorityScore(entry, allEntries);
407
+ cacheEntry(entry, score);
408
+ }
409
+ const currentEntries = Array.from(state.entryCache.values()).map((c) => c.entry);
410
+ const tokensBefore = currentEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);
411
+ const result = pruner.pruneToFit(currentEntries, budget);
412
+ const tokensAfter = result.kept.reduce((sum, e) => sum + estimateTokens(e.content), 0);
413
+ for (const removed of result.removed) {
414
+ state.entryCache.delete(removed.hash);
415
+ }
416
+ if (result.removed.length > 0) {
417
+ updateDocumentFrequency(result.removed, true);
418
+ }
419
+ const duration = Date.now() - startTime;
420
+ const cacheHitRate = newEntries.length > 0 ? cachedEntries.length / newEntries.length : 0;
421
+ getMetrics().recordOptimization({
422
+ timestamp: Date.now(),
423
+ duration,
424
+ tokensBefore,
425
+ tokensAfter,
426
+ entriesProcessed: newEntries.length,
427
+ entriesKept: result.kept.length,
428
+ cacheHitRate,
429
+ memoryUsage: process.memoryUsage().heapUsed
430
+ });
431
+ return result;
432
+ }
433
+ function optimizeFull(allEntries, budget) {
434
+ const startTime = Date.now();
435
+ const tokensBefore = allEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);
436
+ state.entryCache.clear();
437
+ state.documentFrequency.clear();
438
+ state.totalDocuments = 0;
439
+ state.updateCount = 0;
440
+ state.lastFullOptimization = Date.now();
441
+ updateDocumentFrequency(allEntries, false);
442
+ for (const entry of allEntries) {
443
+ const score = pruner.priorityScore(entry, allEntries);
444
+ cacheEntry(entry, score);
445
+ }
446
+ const result = pruner.pruneToFit(allEntries, budget);
447
+ const tokensAfter = result.kept.reduce((sum, e) => sum + estimateTokens(e.content), 0);
448
+ for (const removed of result.removed) {
449
+ state.entryCache.delete(removed.hash);
450
+ }
451
+ if (result.removed.length > 0) {
452
+ updateDocumentFrequency(result.removed, true);
453
+ }
454
+ const duration = Date.now() - startTime;
455
+ getMetrics().recordOptimization({
456
+ timestamp: Date.now(),
457
+ duration,
458
+ tokensBefore,
459
+ tokensAfter,
460
+ entriesProcessed: allEntries.length,
461
+ entriesKept: result.kept.length,
462
+ cacheHitRate: 0,
463
+ // Full optimization has no cache hits
464
+ memoryUsage: process.memoryUsage().heapUsed
465
+ });
466
+ return result;
467
+ }
468
+ function getState() {
469
+ return {
470
+ entryCache: new Map(state.entryCache),
471
+ documentFrequency: new Map(state.documentFrequency),
472
+ totalDocuments: state.totalDocuments,
473
+ updateCount: state.updateCount,
474
+ lastFullOptimization: state.lastFullOptimization
475
+ };
476
+ }
477
+ function restoreState(restoredState) {
478
+ state = {
479
+ entryCache: new Map(restoredState.entryCache),
480
+ documentFrequency: new Map(restoredState.documentFrequency),
481
+ totalDocuments: restoredState.totalDocuments,
482
+ updateCount: restoredState.updateCount,
483
+ lastFullOptimization: restoredState.lastFullOptimization
484
+ };
485
+ }
486
+ function reset() {
487
+ state = {
488
+ entryCache: /* @__PURE__ */ new Map(),
489
+ documentFrequency: /* @__PURE__ */ new Map(),
490
+ totalDocuments: 0,
491
+ updateCount: 0,
492
+ lastFullOptimization: Date.now()
493
+ };
494
+ }
495
+ function getStats() {
496
+ return {
497
+ cachedEntries: state.entryCache.size,
498
+ uniqueTerms: state.documentFrequency.size,
499
+ totalDocuments: state.totalDocuments,
500
+ updateCount: state.updateCount,
501
+ lastFullOptimization: state.lastFullOptimization
502
+ };
503
+ }
504
+ return {
505
+ optimizeIncremental,
506
+ optimizeFull,
507
+ getState,
508
+ restoreState,
509
+ reset,
510
+ getStats
511
+ };
512
+ }
513
+
514
+ // src/core/context-pipeline.ts
515
+ function createContextPipeline(config2) {
516
+ const optimizer = createIncrementalOptimizer(config2);
517
+ const { windowSize, tokenBudget } = config2;
518
+ let totalIngested = 0;
519
+ let evictedEntries = 0;
520
+ let currentEntries = [];
521
+ let budgetUtilization = 0;
522
+ function ingest(content, metadata = {}) {
523
+ const newEntries = parseClaudeCodeContext(content);
524
+ if (newEntries.length === 0) return 0;
525
+ const entriesWithMetadata = newEntries.map((entry) => ({
526
+ ...entry,
527
+ metadata: { ...entry.metadata, ...metadata }
528
+ }));
529
+ const result = optimizer.optimizeIncremental(entriesWithMetadata, tokenBudget);
530
+ totalIngested += newEntries.length;
531
+ evictedEntries += result.removed.length;
532
+ currentEntries = result.kept;
533
+ budgetUtilization = result.budgetUtilization;
534
+ if (currentEntries.length > windowSize) {
535
+ const sorted = [...currentEntries].sort((a, b) => b.timestamp - a.timestamp);
536
+ const toKeep = sorted.slice(0, windowSize);
537
+ const toRemove = sorted.slice(windowSize);
538
+ currentEntries = toKeep;
539
+ evictedEntries += toRemove.length;
540
+ }
541
+ return newEntries.length;
542
+ }
543
+ function getContext() {
544
+ const sorted = [...currentEntries].sort((a, b) => a.timestamp - b.timestamp);
545
+ return sorted.map((e) => e.content).join("\n\n");
546
+ }
547
+ function getEntries() {
548
+ return [...currentEntries].sort((a, b) => a.timestamp - b.timestamp);
549
+ }
550
+ function getStats() {
551
+ const optimizerStats = optimizer.getStats();
552
+ const currentTokens = currentEntries.reduce((sum, e) => sum + estimateTokens(e.content), 0);
553
+ return {
554
+ totalIngested,
555
+ currentEntries: currentEntries.length,
556
+ currentTokens,
557
+ budgetUtilization,
558
+ evictedEntries,
559
+ optimizer: {
560
+ cachedEntries: optimizerStats.cachedEntries,
561
+ uniqueTerms: optimizerStats.uniqueTerms,
562
+ updateCount: optimizerStats.updateCount
563
+ }
564
+ };
565
+ }
566
+ function clear() {
567
+ totalIngested = 0;
568
+ evictedEntries = 0;
569
+ currentEntries = [];
570
+ budgetUtilization = 0;
571
+ optimizer.reset();
572
+ }
573
+ return {
574
+ ingest,
575
+ getContext,
576
+ getEntries,
577
+ getStats,
578
+ clear
579
+ };
580
+ }
581
+
582
+ // src/daemon/file-tracker.ts
583
+ var import_node_fs = require("fs");
584
+ function createFileTracker() {
585
+ const positions = /* @__PURE__ */ new Map();
586
+ function readNewLines(filePath) {
587
+ try {
588
+ const stats = (0, import_node_fs.statSync)(filePath);
589
+ const currentSize = stats.size;
590
+ const currentModified = stats.mtimeMs;
591
+ let pos = positions.get(filePath);
592
+ if (!pos) {
593
+ pos = {
594
+ path: filePath,
595
+ position: 0,
596
+ partialLine: "",
597
+ lastModified: currentModified,
598
+ lastSize: 0
599
+ };
600
+ positions.set(filePath, pos);
601
+ }
602
+ if (currentSize < pos.lastSize || currentSize === pos.position) {
603
+ if (currentSize < pos.lastSize) {
604
+ pos.position = 0;
605
+ pos.partialLine = "";
606
+ }
607
+ return [];
608
+ }
609
+ const buffer = Buffer.alloc(currentSize - pos.position);
610
+ const fd = (0, import_node_fs.readFileSync)(filePath);
611
+ fd.copy(buffer, 0, pos.position, currentSize);
612
+ const newContent = (pos.partialLine + buffer.toString("utf-8")).split("\n");
613
+ const partialLine = newContent.pop() || "";
614
+ pos.position = currentSize;
615
+ pos.partialLine = partialLine;
616
+ pos.lastModified = currentModified;
617
+ pos.lastSize = currentSize;
618
+ return newContent.filter((line) => line.trim().length > 0);
619
+ } catch (_error) {
620
+ return [];
621
+ }
622
+ }
623
+ function getPosition(filePath) {
624
+ return positions.get(filePath) || null;
625
+ }
626
+ function resetPosition(filePath) {
627
+ positions.delete(filePath);
628
+ }
629
+ function clearAll() {
630
+ positions.clear();
631
+ }
632
+ function getTrackedFiles() {
633
+ return Array.from(positions.keys());
634
+ }
635
+ return {
636
+ readNewLines,
637
+ getPosition,
638
+ resetPosition,
639
+ clearAll,
640
+ getTrackedFiles
641
+ };
642
+ }
643
+
644
+ // src/daemon/session-watcher.ts
645
+ function createSessionWatcher(config2) {
646
+ const { config: sparnConfig, onOptimize, onError } = config2;
647
+ const { realtime, decay, states } = sparnConfig;
648
+ const pipelines = /* @__PURE__ */ new Map();
649
+ const fileTracker = createFileTracker();
650
+ const watchers = [];
651
+ const debounceTimers = /* @__PURE__ */ new Map();
652
+ function getProjectsDir() {
653
+ return (0, import_node_path.join)((0, import_node_os.homedir)(), ".claude", "projects");
654
+ }
655
+ function getSessionId(filePath) {
656
+ const filename = filePath.split(/[/\\]/).pop() || "";
657
+ return filename.replace(/\.jsonl$/, "");
658
+ }
659
+ function getPipeline(sessionId) {
660
+ let pipeline = pipelines.get(sessionId);
661
+ if (!pipeline) {
662
+ pipeline = createContextPipeline({
663
+ tokenBudget: realtime.tokenBudget,
664
+ decay,
665
+ states,
666
+ windowSize: realtime.windowSize,
667
+ fullOptimizationInterval: 50
668
+ // Full re-optimization every 50 incremental updates
669
+ });
670
+ pipelines.set(sessionId, pipeline);
671
+ }
672
+ return pipeline;
673
+ }
674
+ function handleFileChange(filePath) {
675
+ const existingTimer = debounceTimers.get(filePath);
676
+ if (existingTimer) {
677
+ clearTimeout(existingTimer);
678
+ }
679
+ const timer = setTimeout(() => {
680
+ try {
681
+ const newLines = fileTracker.readNewLines(filePath);
682
+ if (newLines.length === 0) return;
683
+ const content = newLines.join("\n");
684
+ const sessionId = getSessionId(filePath);
685
+ const pipeline = getPipeline(sessionId);
686
+ pipeline.ingest(content, { sessionId, filePath });
687
+ const stats = pipeline.getStats();
688
+ if (stats.currentTokens >= realtime.autoOptimizeThreshold) {
689
+ getMetrics().updateDaemon({
690
+ sessionsWatched: pipelines.size,
691
+ memoryUsage: process.memoryUsage().heapUsed
692
+ });
693
+ if (onOptimize) {
694
+ const sessionStats = computeSessionStats(sessionId, pipeline);
695
+ onOptimize(sessionId, sessionStats);
696
+ }
697
+ }
698
+ } catch (error) {
699
+ if (onError) {
700
+ onError(error instanceof Error ? error : new Error(String(error)));
701
+ }
702
+ } finally {
703
+ debounceTimers.delete(filePath);
704
+ }
705
+ }, realtime.debounceMs);
706
+ debounceTimers.set(filePath, timer);
707
+ }
708
+ function findJsonlFiles(dir) {
709
+ const files = [];
710
+ try {
711
+ const entries = (0, import_node_fs2.readdirSync)(dir);
712
+ for (const entry of entries) {
713
+ const fullPath = (0, import_node_path.join)(dir, entry);
714
+ const stat = (0, import_node_fs2.statSync)(fullPath);
715
+ if (stat.isDirectory()) {
716
+ files.push(...findJsonlFiles(fullPath));
717
+ } else if (entry.endsWith(".jsonl")) {
718
+ const matches = realtime.watchPatterns.some((pattern) => {
719
+ const regex = new RegExp(
720
+ pattern.replace(/\*\*/g, ".*").replace(/\*/g, "[^/\\\\]*").replace(/\./g, "\\.")
721
+ );
722
+ return regex.test(fullPath);
723
+ });
724
+ if (matches) {
725
+ files.push(fullPath);
726
+ }
727
+ }
728
+ }
729
+ } catch (_error) {
730
+ }
731
+ return files;
732
+ }
733
+ function computeSessionStats(sessionId, pipeline) {
734
+ const stats = pipeline.getStats();
735
+ const entries = pipeline.getEntries();
736
+ const totalTokens = entries.reduce((sum, e) => sum + estimateTokens(e.content), 0);
737
+ return {
738
+ sessionId,
739
+ totalTokens: stats.totalIngested,
740
+ optimizedTokens: stats.currentTokens,
741
+ reduction: totalTokens > 0 ? (totalTokens - stats.currentTokens) / totalTokens : 0,
742
+ entryCount: stats.currentEntries,
743
+ budgetUtilization: stats.budgetUtilization
744
+ };
745
+ }
746
+ async function start() {
747
+ const projectsDir = getProjectsDir();
748
+ const jsonlFiles = findJsonlFiles(projectsDir);
749
+ const watchedDirs = /* @__PURE__ */ new Set();
750
+ for (const file of jsonlFiles) {
751
+ const dir = (0, import_node_path.dirname)(file);
752
+ if (!watchedDirs.has(dir)) {
753
+ const watcher2 = (0, import_node_fs2.watch)(dir, { recursive: false }, (_eventType, filename) => {
754
+ if (filename?.endsWith(".jsonl")) {
755
+ const fullPath = (0, import_node_path.join)(dir, filename);
756
+ handleFileChange(fullPath);
757
+ }
758
+ });
759
+ watchers.push(watcher2);
760
+ watchedDirs.add(dir);
761
+ }
762
+ }
763
+ const projectsWatcher = (0, import_node_fs2.watch)(projectsDir, { recursive: true }, (_eventType, filename) => {
764
+ if (filename?.endsWith(".jsonl")) {
765
+ const fullPath = (0, import_node_path.join)(projectsDir, filename);
766
+ handleFileChange(fullPath);
767
+ }
768
+ });
769
+ watchers.push(projectsWatcher);
770
+ getMetrics().updateDaemon({
771
+ startTime: Date.now(),
772
+ sessionsWatched: jsonlFiles.length,
773
+ memoryUsage: process.memoryUsage().heapUsed
774
+ });
775
+ }
776
+ function stop() {
777
+ for (const watcher2 of watchers) {
778
+ watcher2.close();
779
+ }
780
+ watchers.length = 0;
781
+ for (const timer of debounceTimers.values()) {
782
+ clearTimeout(timer);
783
+ }
784
+ debounceTimers.clear();
785
+ pipelines.clear();
786
+ fileTracker.clearAll();
787
+ }
788
+ function getStats() {
789
+ const stats = [];
790
+ for (const [sessionId, pipeline] of pipelines.entries()) {
791
+ stats.push(computeSessionStats(sessionId, pipeline));
792
+ }
793
+ return stats;
794
+ }
795
+ function getSessionStats(sessionId) {
796
+ const pipeline = pipelines.get(sessionId);
797
+ if (!pipeline) return null;
798
+ return computeSessionStats(sessionId, pipeline);
799
+ }
800
+ function optimizeSession(sessionId) {
801
+ const pipeline = pipelines.get(sessionId);
802
+ if (!pipeline) return;
803
+ const entries = pipeline.getEntries();
804
+ pipeline.clear();
805
+ pipeline.ingest(entries.map((e) => e.content).join("\n\n"));
806
+ if (onOptimize) {
807
+ const stats = computeSessionStats(sessionId, pipeline);
808
+ onOptimize(sessionId, stats);
809
+ }
810
+ }
811
+ return {
812
+ start,
813
+ stop,
814
+ getStats,
815
+ getSessionStats,
816
+ optimizeSession
817
+ };
818
+ }
819
+
820
+ // src/daemon/index.ts
821
+ var configJson = process.env["SPARN_CONFIG"];
822
+ var pidFile = process.env["SPARN_PID_FILE"];
823
+ var logFile = process.env["SPARN_LOG_FILE"];
824
+ if (!configJson || !pidFile || !logFile) {
825
+ console.error("Daemon: Missing required environment variables");
826
+ process.exit(1);
827
+ }
828
+ var config = JSON.parse(configJson);
829
+ function log(message) {
830
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
831
+ const logMessage = `[${timestamp}] ${message}
832
+ `;
833
+ if (logFile) {
834
+ try {
835
+ (0, import_node_fs3.appendFileSync)(logFile, logMessage, "utf-8");
836
+ } catch {
837
+ }
838
+ }
839
+ }
840
+ function cleanup() {
841
+ log("Daemon shutting down");
842
+ if (pidFile && (0, import_node_fs3.existsSync)(pidFile)) {
843
+ try {
844
+ (0, import_node_fs3.unlinkSync)(pidFile);
845
+ } catch {
846
+ }
847
+ }
848
+ watcher.stop();
849
+ process.exit(0);
850
+ }
851
+ process.on("SIGTERM", cleanup);
852
+ process.on("SIGINT", cleanup);
853
+ process.on("SIGHUP", cleanup);
854
+ process.on("uncaughtException", (error) => {
855
+ log(`Uncaught exception: ${error.message}`);
856
+ cleanup();
857
+ });
858
+ process.on("unhandledRejection", (reason) => {
859
+ log(`Unhandled rejection: ${reason}`);
860
+ cleanup();
861
+ });
862
+ log("Daemon starting");
863
+ var watcher = createSessionWatcher({
864
+ config,
865
+ onOptimize: (sessionId, stats) => {
866
+ log(
867
+ `Optimized session ${sessionId}: ${stats.optimizedTokens} tokens (${Math.round(stats.reduction * 100)}% reduction)`
868
+ );
869
+ },
870
+ onError: (error) => {
871
+ log(`Error: ${error.message}`);
872
+ }
873
+ });
874
+ watcher.start().then(() => {
875
+ log("Daemon ready - watching Claude Code sessions");
876
+ }).catch((error) => {
877
+ log(`Failed to start: ${error.message}`);
878
+ cleanup();
879
+ });
880
+ setInterval(() => {
881
+ }, 6e4);
882
+ //# sourceMappingURL=index.cjs.map