audrey 0.14.0 → 0.16.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.
package/src/decay.js CHANGED
@@ -1,84 +1,84 @@
1
- import { computeConfidence, DEFAULT_HALF_LIVES, salienceModifier } from './confidence.js';
2
- import { interferenceModifier } from './interference.js';
3
- import { daysBetween } from './utils.js';
4
-
5
- /**
6
- * @param {import('better-sqlite3').Database} db
7
- * @param {{ dormantThreshold?: number }} [options]
8
- * @returns {{ totalEvaluated: number, transitionedToDormant: number, timestamp: string }}
9
- */
10
- export function applyDecay(db, { dormantThreshold = 0.1, halfLives } = {}) {
11
- const now = new Date();
12
- let totalEvaluated = 0;
13
- let transitionedToDormant = 0;
14
-
15
- const semantics = db.prepare(`
16
- SELECT id, supporting_count, contradicting_count, created_at,
17
- last_reinforced_at, retrieval_count, interference_count, salience
18
- FROM semantics WHERE state = 'active'
19
- `).all();
20
-
21
- const markDormantSem = db.prepare('UPDATE semantics SET state = ? WHERE id = ?');
22
-
23
- for (const sem of semantics) {
24
- totalEvaluated++;
25
- const ageDays = daysBetween(sem.created_at, now);
26
- const daysSinceRetrieval = sem.last_reinforced_at
27
- ? daysBetween(sem.last_reinforced_at, now)
28
- : ageDays;
29
-
30
- let confidence = computeConfidence({
31
- sourceType: 'tool-result',
32
- supportingCount: sem.supporting_count || 0,
33
- contradictingCount: sem.contradicting_count || 0,
34
- ageDays,
35
- halfLifeDays: halfLives?.semantic ?? DEFAULT_HALF_LIVES.semantic,
36
- retrievalCount: sem.retrieval_count || 0,
37
- daysSinceRetrieval,
38
- });
39
- confidence *= interferenceModifier(sem.interference_count || 0);
40
- confidence *= salienceModifier(sem.salience ?? 0.5);
41
- confidence = Math.max(0, Math.min(1, confidence));
42
-
43
- if (confidence < dormantThreshold) {
44
- markDormantSem.run('dormant', sem.id);
45
- transitionedToDormant++;
46
- }
47
- }
48
-
49
- const procedures = db.prepare(`
50
- SELECT id, success_count, failure_count, created_at,
51
- last_reinforced_at, retrieval_count, interference_count, salience
52
- FROM procedures WHERE state = 'active'
53
- `).all();
54
-
55
- const markDormantProc = db.prepare('UPDATE procedures SET state = ? WHERE id = ?');
56
-
57
- for (const proc of procedures) {
58
- totalEvaluated++;
59
- const ageDays = daysBetween(proc.created_at, now);
60
- const daysSinceRetrieval = proc.last_reinforced_at
61
- ? daysBetween(proc.last_reinforced_at, now)
62
- : ageDays;
63
-
64
- let confidence = computeConfidence({
65
- sourceType: 'tool-result',
66
- supportingCount: proc.success_count || 0,
67
- contradictingCount: proc.failure_count || 0,
68
- ageDays,
69
- halfLifeDays: halfLives?.procedural ?? DEFAULT_HALF_LIVES.procedural,
70
- retrievalCount: proc.retrieval_count || 0,
71
- daysSinceRetrieval,
72
- });
73
- confidence *= interferenceModifier(proc.interference_count || 0);
74
- confidence *= salienceModifier(proc.salience ?? 0.5);
75
- confidence = Math.max(0, Math.min(1, confidence));
76
-
77
- if (confidence < dormantThreshold) {
78
- markDormantProc.run('dormant', proc.id);
79
- transitionedToDormant++;
80
- }
81
- }
82
-
83
- return { totalEvaluated, transitionedToDormant, timestamp: now.toISOString() };
84
- }
1
+ import { computeConfidence, DEFAULT_HALF_LIVES, salienceModifier } from './confidence.js';
2
+ import { interferenceModifier } from './interference.js';
3
+ import { daysBetween } from './utils.js';
4
+
5
+ /**
6
+ * @param {import('better-sqlite3').Database} db
7
+ * @param {{ dormantThreshold?: number }} [options]
8
+ * @returns {{ totalEvaluated: number, transitionedToDormant: number, timestamp: string }}
9
+ */
10
+ export function applyDecay(db, { dormantThreshold = 0.1, halfLives } = {}) {
11
+ const now = new Date();
12
+ let totalEvaluated = 0;
13
+ let transitionedToDormant = 0;
14
+
15
+ const semantics = db.prepare(`
16
+ SELECT id, supporting_count, contradicting_count, created_at,
17
+ last_reinforced_at, retrieval_count, interference_count, salience
18
+ FROM semantics WHERE state = 'active'
19
+ `).all();
20
+
21
+ const markDormantSem = db.prepare('UPDATE semantics SET state = ? WHERE id = ?');
22
+
23
+ for (const sem of semantics) {
24
+ totalEvaluated++;
25
+ const ageDays = daysBetween(sem.created_at, now);
26
+ const daysSinceRetrieval = sem.last_reinforced_at
27
+ ? daysBetween(sem.last_reinforced_at, now)
28
+ : ageDays;
29
+
30
+ let confidence = computeConfidence({
31
+ sourceType: 'tool-result',
32
+ supportingCount: sem.supporting_count || 0,
33
+ contradictingCount: sem.contradicting_count || 0,
34
+ ageDays,
35
+ halfLifeDays: halfLives?.semantic ?? DEFAULT_HALF_LIVES.semantic,
36
+ retrievalCount: sem.retrieval_count || 0,
37
+ daysSinceRetrieval,
38
+ });
39
+ confidence *= interferenceModifier(sem.interference_count || 0);
40
+ confidence *= salienceModifier(sem.salience ?? 0.5);
41
+ confidence = Math.max(0, Math.min(1, confidence));
42
+
43
+ if (confidence < dormantThreshold) {
44
+ markDormantSem.run('dormant', sem.id);
45
+ transitionedToDormant++;
46
+ }
47
+ }
48
+
49
+ const procedures = db.prepare(`
50
+ SELECT id, success_count, failure_count, created_at,
51
+ last_reinforced_at, retrieval_count, interference_count, salience
52
+ FROM procedures WHERE state = 'active'
53
+ `).all();
54
+
55
+ const markDormantProc = db.prepare('UPDATE procedures SET state = ? WHERE id = ?');
56
+
57
+ for (const proc of procedures) {
58
+ totalEvaluated++;
59
+ const ageDays = daysBetween(proc.created_at, now);
60
+ const daysSinceRetrieval = proc.last_reinforced_at
61
+ ? daysBetween(proc.last_reinforced_at, now)
62
+ : ageDays;
63
+
64
+ let confidence = computeConfidence({
65
+ sourceType: 'tool-result',
66
+ supportingCount: proc.success_count || 0,
67
+ contradictingCount: proc.failure_count || 0,
68
+ ageDays,
69
+ halfLifeDays: halfLives?.procedural ?? DEFAULT_HALF_LIVES.procedural,
70
+ retrievalCount: proc.retrieval_count || 0,
71
+ daysSinceRetrieval,
72
+ });
73
+ confidence *= interferenceModifier(proc.interference_count || 0);
74
+ confidence *= salienceModifier(proc.salience ?? 0.5);
75
+ confidence = Math.max(0, Math.min(1, confidence));
76
+
77
+ if (confidence < dormantThreshold) {
78
+ markDormantProc.run('dormant', proc.id);
79
+ transitionedToDormant++;
80
+ }
81
+ }
82
+
83
+ return { totalEvaluated, transitionedToDormant, timestamp: now.toISOString() };
84
+ }