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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Phase 9 — Episodic memory schema and recency scoring primitives.
//!
//! Provides zero-I/O building blocks for agent memory tables:
//! - `EpisodicMemorySchema` marker + `episodic_columns` constants
//! - `RecencyConfig` / `recency_weight` — exponential time-decay
//! - `hybrid_score` — fuses distance, recency, and importance into one ranking signal
//!
//! These are pure-math functions with no async, no I/O, no external deps.
use ;
// ── Column name constants ─────────────────────────────────────────────────────
/// Canonical column names for episodic memory tables.
///
/// Include these alongside `llm_columns::*` in the Arrow schema of any
/// `EpisodicMemorySchema` table. The AI-Lake SDK reads columns by name.
// ── EpisodicMemorySchema marker ───────────────────────────────────────────────
/// Marker struct for episodic agent memory tables (Phase 9).
/// Actual schema is enforced by column names in `episodic_columns` module.
///
/// An episodic memory table extends `LlmContextSchema` with recency and
/// importance signals, enabling hybrid scoring during recall:
///
/// ```text
/// -- From llm_columns::* (required baseline)
/// chunk_id: Utf8
/// chunk_text: Utf8
/// embedding: FixedSizeBinary(N) -- F16, cosine
///
/// -- From episodic_columns::* (Phase 9 extensions)
/// agent_id: Utf8 -- UUID string
/// session_id: Utf8 -- UUID string
/// created_at: Timestamp(ns, UTC) -- use ailake_core::now_ns()
/// recency_weight: Float32 -- exp(-λ * days_since_access), updated by MemoryDecayJob
/// access_count: UInt32 -- incremented on each recall() hit
/// last_accessed_at: Timestamp(ns, UTC) -- updated on recall, use ailake_core::now_ns()
/// importance_score: Float32 -- agent-assigned [0.0, 1.0]
/// ```
///
/// **Hybrid scoring**: after HNSW retrieval, re-rank results by
/// `hybrid_score(distance, recency_weight, importance_score)`. Memories
/// that are semantically similar AND recently accessed AND flagged important
/// rank highest.
///
/// **Recommended setup**:
/// - One HNSW over `embedding` (text, cosine, dim=1536).
/// - Partition by `agent_id` via `VectorStoragePolicy` hidden partitioning.
/// - Run `MemoryDecayJob` daily to update `recency_weight` via compaction.
;
// ── Recency decay ─────────────────────────────────────────────────────────────
/// Parameters for exponential time-decay of memory recency.
///
/// The decay formula is: `recency_weight = exp(-lambda * days_since_access)`
///
/// Common presets:
/// | Half-life | lambda |
/// |-----------|---------|
/// | 1 day | 0.693 |
/// | 1 week | 0.099 |
/// | 1 month | 0.023 |
/// | 3 months | 0.0077 |
/// Compute the recency weight for a memory chunk.
///
/// Returns a value in (0.0, 1.0]:
/// - 1.0 when `days_since_access == 0` (accessed right now)
/// - 0.5 at the half-life (days = ln(2) / lambda)
/// - Approaches 0 for very old, never-accessed memories
///
/// `days_since_access` may be fractional (e.g. 0.5 = 12 hours).
/// Negative values are clamped to 0 (future timestamps treated as "now").
// ── Hybrid scoring ────────────────────────────────────────────────────────────
/// Compute the hybrid ranking score for a retrieved memory chunk.
///
/// Fuses three signals into a single ascending score (lower = better rank,
/// consistent with AI-Lake's distance-ascending convention):
///
/// ```text
/// hybrid_score = distance / (recency_weight * importance_score)
/// ```
///
/// Rationale:
/// - `distance` is HNSW cosine distance in [0.0, 2.0] (lower = more similar).
/// - Dividing by `recency_weight * importance_score` boosts recent/important
/// memories (high values shrink the score → rise in rank).
/// - Result approaches `distance` as recency and importance → 1.0 (neutral).
/// - Safeguard: denominator clamped to `f32::EPSILON` to avoid division by zero.
///
/// **Usage**: call after HNSW top-k retrieval, before returning results to the agent.
///
/// # Arguments
/// - `distance`: HNSW cosine distance for this result (from `SearchResult.distance`)
/// - `recency_weight`: value from `episodic_columns::RECENCY_WEIGHT` column (or computed via `recency_weight()`)
/// - `importance_score`: value from `episodic_columns::IMPORTANCE_SCORE` column
// ── Tests ─────────────────────────────────────────────────────────────────────