memlife 0.1.0b0__tar.gz
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.
- memlife-0.1.0b0/LICENSE +21 -0
- memlife-0.1.0b0/PKG-INFO +163 -0
- memlife-0.1.0b0/README.md +131 -0
- memlife-0.1.0b0/pyproject.toml +39 -0
- memlife-0.1.0b0/setup.cfg +4 -0
- memlife-0.1.0b0/src/memlife/__init__.py +51 -0
- memlife-0.1.0b0/src/memlife/adapters/__init__.py +5 -0
- memlife-0.1.0b0/src/memlife/config.py +98 -0
- memlife-0.1.0b0/src/memlife/embedders.py +26 -0
- memlife-0.1.0b0/src/memlife/gc.py +28 -0
- memlife-0.1.0b0/src/memlife/io.py +126 -0
- memlife-0.1.0b0/src/memlife/llm.py +38 -0
- memlife-0.1.0b0/src/memlife/models.py +133 -0
- memlife-0.1.0b0/src/memlife/protocols.py +32 -0
- memlife-0.1.0b0/src/memlife/reflection.py +773 -0
- memlife-0.1.0b0/src/memlife/retrieval.py +265 -0
- memlife-0.1.0b0/src/memlife/store.py +1769 -0
- memlife-0.1.0b0/src/memlife/vectors.py +24 -0
- memlife-0.1.0b0/src/memlife.egg-info/PKG-INFO +163 -0
- memlife-0.1.0b0/src/memlife.egg-info/SOURCES.txt +25 -0
- memlife-0.1.0b0/src/memlife.egg-info/dependency_links.txt +1 -0
- memlife-0.1.0b0/src/memlife.egg-info/requires.txt +17 -0
- memlife-0.1.0b0/src/memlife.egg-info/top_level.txt +1 -0
- memlife-0.1.0b0/tests/test_lifecycle.py +101 -0
- memlife-0.1.0b0/tests/test_reflection.py +105 -0
- memlife-0.1.0b0/tests/test_retrieval.py +50 -0
- memlife-0.1.0b0/tests/test_store.py +142 -0
memlife-0.1.0b0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 EzyRider
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
memlife-0.1.0b0/PKG-INFO
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: memlife
|
|
3
|
+
Version: 0.1.0b0
|
|
4
|
+
Summary: Memory that degrades gracefully — four-tier lifecycle memory for AI agents
|
|
5
|
+
Author-email: EzyRider <ezyrider70@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: memory,ai,agent,lifecycle,decay,reflection,mcp
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Provides-Extra: ollama
|
|
20
|
+
Requires-Dist: aiohttp; extra == "ollama"
|
|
21
|
+
Provides-Extra: openai
|
|
22
|
+
Requires-Dist: openai; extra == "openai"
|
|
23
|
+
Provides-Extra: sentence-transformers
|
|
24
|
+
Requires-Dist: sentence-transformers; extra == "sentence-transformers"
|
|
25
|
+
Provides-Extra: mcp
|
|
26
|
+
Requires-Dist: mcp; extra == "mcp"
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-asyncio; extra == "dev"
|
|
30
|
+
Requires-Dist: ruff; extra == "dev"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# memlife
|
|
34
|
+
|
|
35
|
+
Memory that degrades gracefully. Not another pile that grows forever.
|
|
36
|
+
|
|
37
|
+
## What
|
|
38
|
+
|
|
39
|
+
memlife is a four-tier lifecycle memory system for AI agents:
|
|
40
|
+
|
|
41
|
+
- **Episodes** — raw events (what happened)
|
|
42
|
+
- **Facts** — durable truths (what I know)
|
|
43
|
+
- **Journal** — reflected beliefs (what I believe)
|
|
44
|
+
- **Decay/Prune** — confidence fades, stale entries retire, GC cleans up
|
|
45
|
+
|
|
46
|
+
Every memory has a lifecycle. Facts decay through confidence erosion. Journal entries retire when they fall below the floor. Superseded data is pruned after a retention period. Nothing accumulates forever.
|
|
47
|
+
|
|
48
|
+
## Why
|
|
49
|
+
|
|
50
|
+
Every other memory system accumulates. Facts never expire. Confidence never decays. Stale conventions become unquestioned truths. Recall quality degrades over time.
|
|
51
|
+
|
|
52
|
+
memlife solves this. Memory should be like human memory — it fades, it gets revised, it gets pruned. Not a database that grows until it breaks.
|
|
53
|
+
|
|
54
|
+
## Quickstart
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install memlife
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
import asyncio
|
|
62
|
+
from memlife import MemoryStore, MemoryConfig, DummyEmbedder
|
|
63
|
+
|
|
64
|
+
async def main():
|
|
65
|
+
store = MemoryStore(
|
|
66
|
+
config=MemoryConfig(db_path="./mem.db"),
|
|
67
|
+
embedder=DummyEmbedder(), # zero external dependencies
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# Store an episode (something happened)
|
|
71
|
+
store.remember(task="User asked about deployment", outcome="success")
|
|
72
|
+
|
|
73
|
+
# Store a fact (durable truth)
|
|
74
|
+
await store.store_fact("User deploys via GitHub Actions", confidence=0.8)
|
|
75
|
+
|
|
76
|
+
# Retrieve relevant memories (unified scoring across all layers)
|
|
77
|
+
context = await store.retrieve("deployment")
|
|
78
|
+
|
|
79
|
+
print(context)
|
|
80
|
+
store.close()
|
|
81
|
+
|
|
82
|
+
asyncio.run(main())
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
No Ollama, no OpenAI, no API key. The DummyEmbedder uses hash-based vectors. The full lifecycle — store, retrieve, decay, GC — works without any LLM.
|
|
86
|
+
|
|
87
|
+
## The Lifecycle
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
┌───────────┐ reflection ┌───────────┐
|
|
91
|
+
│ EPISODE │ ──────────────────▶ │ JOURNAL │
|
|
92
|
+
│ (event) │ LLM synthesises │ (belief) │
|
|
93
|
+
└─────┬─────┘ observations & └─────┬─────┘
|
|
94
|
+
│ hypotheses │
|
|
95
|
+
│ │
|
|
96
|
+
│ store_fact() │ confidence decay
|
|
97
|
+
▼ │ (30d halflife)
|
|
98
|
+
┌───────────┐ recall bumps ┌─────▼─────┐
|
|
99
|
+
│ FACT │ ◀──────────────── │ RETIRE │
|
|
100
|
+
│ (truth) │ confidence +0.05 │ (floor) │
|
|
101
|
+
└─────┬─────┘ └─────┬─────┘
|
|
102
|
+
│ │
|
|
103
|
+
│ revise / supersede │ GC prunes
|
|
104
|
+
▼ ▼
|
|
105
|
+
┌───────────┐ ┌───────────┐
|
|
106
|
+
│ SUPERSEDED│ 90 days retention │ PRUNED │
|
|
107
|
+
│ (replaced)│ ───────────────────▶ │ (deleted) │
|
|
108
|
+
└───────────┘ └───────────┘
|
|
109
|
+
|
|
110
|
+
UNIFIED SCORE = relevance × confidence × recency
|
|
111
|
+
Applied across ALL layers before every response.
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## No-LLM Mode
|
|
115
|
+
|
|
116
|
+
The store, retrieval, decay, GC, and embedding versioning all work without any LLM. Only the reflection loop needs a model. If you just want durable, decaying memory:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
store = MemoryStore(config=MemoryConfig(db_path="./mem.db"))
|
|
120
|
+
store.remember(task="something happened", outcome="success")
|
|
121
|
+
context = await store.retrieve("something")
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## With Reflection
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
from memlife import MemoryStore, MemoryConfig, Reflector, DummyEmbedder, DummyChat
|
|
128
|
+
|
|
129
|
+
store = MemoryStore(
|
|
130
|
+
config=MemoryConfig(db_path="./mem.db"),
|
|
131
|
+
embedder=DummyEmbedder(),
|
|
132
|
+
)
|
|
133
|
+
reflector = Reflector(
|
|
134
|
+
memory=store,
|
|
135
|
+
model_chat=DummyChat(),
|
|
136
|
+
critic=False,
|
|
137
|
+
)
|
|
138
|
+
result = await reflector.reflect()
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
For real LLMs, implement the `Embedder` and `ChatCallable` protocols, or use an adapter (Phase 2).
|
|
142
|
+
|
|
143
|
+
## Features
|
|
144
|
+
|
|
145
|
+
- Four-tier lifecycle: Episode → Fact → Journal → Decay/Prune
|
|
146
|
+
- Unified scoring: relevance × confidence × recency across all layers
|
|
147
|
+
- Confidence ceiling (0.99) — facts are never immutable
|
|
148
|
+
- Confidence decay with 30-day halflife — journal entries fade
|
|
149
|
+
- GC with configurable retention (90 days for superseded facts, etc.)
|
|
150
|
+
- Embedding versioning — detect stale vectors when the model changes
|
|
151
|
+
- Episode tool index — search "have I used this tool before?"
|
|
152
|
+
- Incremental contradiction detection — O(new × n), not O(n²)
|
|
153
|
+
- JSONL import/export for backup and migration
|
|
154
|
+
- SQLite-backed, single file, zero external services
|
|
155
|
+
- Works with zero dependencies (DummyEmbedder + DummyChat)
|
|
156
|
+
|
|
157
|
+
## Status
|
|
158
|
+
|
|
159
|
+
**v0.1.0-beta.** The API may change before v1.0.
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
MIT
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# memlife
|
|
2
|
+
|
|
3
|
+
Memory that degrades gracefully. Not another pile that grows forever.
|
|
4
|
+
|
|
5
|
+
## What
|
|
6
|
+
|
|
7
|
+
memlife is a four-tier lifecycle memory system for AI agents:
|
|
8
|
+
|
|
9
|
+
- **Episodes** — raw events (what happened)
|
|
10
|
+
- **Facts** — durable truths (what I know)
|
|
11
|
+
- **Journal** — reflected beliefs (what I believe)
|
|
12
|
+
- **Decay/Prune** — confidence fades, stale entries retire, GC cleans up
|
|
13
|
+
|
|
14
|
+
Every memory has a lifecycle. Facts decay through confidence erosion. Journal entries retire when they fall below the floor. Superseded data is pruned after a retention period. Nothing accumulates forever.
|
|
15
|
+
|
|
16
|
+
## Why
|
|
17
|
+
|
|
18
|
+
Every other memory system accumulates. Facts never expire. Confidence never decays. Stale conventions become unquestioned truths. Recall quality degrades over time.
|
|
19
|
+
|
|
20
|
+
memlife solves this. Memory should be like human memory — it fades, it gets revised, it gets pruned. Not a database that grows until it breaks.
|
|
21
|
+
|
|
22
|
+
## Quickstart
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install memlife
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import asyncio
|
|
30
|
+
from memlife import MemoryStore, MemoryConfig, DummyEmbedder
|
|
31
|
+
|
|
32
|
+
async def main():
|
|
33
|
+
store = MemoryStore(
|
|
34
|
+
config=MemoryConfig(db_path="./mem.db"),
|
|
35
|
+
embedder=DummyEmbedder(), # zero external dependencies
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# Store an episode (something happened)
|
|
39
|
+
store.remember(task="User asked about deployment", outcome="success")
|
|
40
|
+
|
|
41
|
+
# Store a fact (durable truth)
|
|
42
|
+
await store.store_fact("User deploys via GitHub Actions", confidence=0.8)
|
|
43
|
+
|
|
44
|
+
# Retrieve relevant memories (unified scoring across all layers)
|
|
45
|
+
context = await store.retrieve("deployment")
|
|
46
|
+
|
|
47
|
+
print(context)
|
|
48
|
+
store.close()
|
|
49
|
+
|
|
50
|
+
asyncio.run(main())
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
No Ollama, no OpenAI, no API key. The DummyEmbedder uses hash-based vectors. The full lifecycle — store, retrieve, decay, GC — works without any LLM.
|
|
54
|
+
|
|
55
|
+
## The Lifecycle
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
┌───────────┐ reflection ┌───────────┐
|
|
59
|
+
│ EPISODE │ ──────────────────▶ │ JOURNAL │
|
|
60
|
+
│ (event) │ LLM synthesises │ (belief) │
|
|
61
|
+
└─────┬─────┘ observations & └─────┬─────┘
|
|
62
|
+
│ hypotheses │
|
|
63
|
+
│ │
|
|
64
|
+
│ store_fact() │ confidence decay
|
|
65
|
+
▼ │ (30d halflife)
|
|
66
|
+
┌───────────┐ recall bumps ┌─────▼─────┐
|
|
67
|
+
│ FACT │ ◀──────────────── │ RETIRE │
|
|
68
|
+
│ (truth) │ confidence +0.05 │ (floor) │
|
|
69
|
+
└─────┬─────┘ └─────┬─────┘
|
|
70
|
+
│ │
|
|
71
|
+
│ revise / supersede │ GC prunes
|
|
72
|
+
▼ ▼
|
|
73
|
+
┌───────────┐ ┌───────────┐
|
|
74
|
+
│ SUPERSEDED│ 90 days retention │ PRUNED │
|
|
75
|
+
│ (replaced)│ ───────────────────▶ │ (deleted) │
|
|
76
|
+
└───────────┘ └───────────┘
|
|
77
|
+
|
|
78
|
+
UNIFIED SCORE = relevance × confidence × recency
|
|
79
|
+
Applied across ALL layers before every response.
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## No-LLM Mode
|
|
83
|
+
|
|
84
|
+
The store, retrieval, decay, GC, and embedding versioning all work without any LLM. Only the reflection loop needs a model. If you just want durable, decaying memory:
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
store = MemoryStore(config=MemoryConfig(db_path="./mem.db"))
|
|
88
|
+
store.remember(task="something happened", outcome="success")
|
|
89
|
+
context = await store.retrieve("something")
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## With Reflection
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from memlife import MemoryStore, MemoryConfig, Reflector, DummyEmbedder, DummyChat
|
|
96
|
+
|
|
97
|
+
store = MemoryStore(
|
|
98
|
+
config=MemoryConfig(db_path="./mem.db"),
|
|
99
|
+
embedder=DummyEmbedder(),
|
|
100
|
+
)
|
|
101
|
+
reflector = Reflector(
|
|
102
|
+
memory=store,
|
|
103
|
+
model_chat=DummyChat(),
|
|
104
|
+
critic=False,
|
|
105
|
+
)
|
|
106
|
+
result = await reflector.reflect()
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
For real LLMs, implement the `Embedder` and `ChatCallable` protocols, or use an adapter (Phase 2).
|
|
110
|
+
|
|
111
|
+
## Features
|
|
112
|
+
|
|
113
|
+
- Four-tier lifecycle: Episode → Fact → Journal → Decay/Prune
|
|
114
|
+
- Unified scoring: relevance × confidence × recency across all layers
|
|
115
|
+
- Confidence ceiling (0.99) — facts are never immutable
|
|
116
|
+
- Confidence decay with 30-day halflife — journal entries fade
|
|
117
|
+
- GC with configurable retention (90 days for superseded facts, etc.)
|
|
118
|
+
- Embedding versioning — detect stale vectors when the model changes
|
|
119
|
+
- Episode tool index — search "have I used this tool before?"
|
|
120
|
+
- Incremental contradiction detection — O(new × n), not O(n²)
|
|
121
|
+
- JSONL import/export for backup and migration
|
|
122
|
+
- SQLite-backed, single file, zero external services
|
|
123
|
+
- Works with zero dependencies (DummyEmbedder + DummyChat)
|
|
124
|
+
|
|
125
|
+
## Status
|
|
126
|
+
|
|
127
|
+
**v0.1.0-beta.** The API may change before v1.0.
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
MIT
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "memlife"
|
|
7
|
+
version = "0.1.0b0"
|
|
8
|
+
description = "Memory that degrades gracefully — four-tier lifecycle memory for AI agents"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "MIT"}
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{name = "EzyRider", email = "ezyrider70@gmail.com"}]
|
|
13
|
+
keywords = ["memory", "ai", "agent", "lifecycle", "decay", "reflection", "mcp"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
ollama = ["aiohttp"]
|
|
27
|
+
openai = ["openai"]
|
|
28
|
+
sentence-transformers = ["sentence-transformers"]
|
|
29
|
+
mcp = ["mcp"]
|
|
30
|
+
dev = ["pytest", "pytest-asyncio", "ruff"]
|
|
31
|
+
|
|
32
|
+
# [project.scripts]
|
|
33
|
+
# memlife-mcp-server = "memlife.mcp_server:main" # Phase 3: MCP server not yet implemented
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.packages.find]
|
|
36
|
+
where = ["src"]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
asyncio_mode = "auto"
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""memlife — memory that degrades gracefully.
|
|
2
|
+
|
|
3
|
+
Four-tier lifecycle memory for AI agents:
|
|
4
|
+
Episode → Fact → Journal → Decay/Prune
|
|
5
|
+
|
|
6
|
+
Unified scoring: relevance × confidence × recency.
|
|
7
|
+
No-LLM mode: store, retrieve, decay, and GC work without any model.
|
|
8
|
+
Only reflection needs an LLM.
|
|
9
|
+
|
|
10
|
+
Quickstart:
|
|
11
|
+
from memlife import MemoryStore, MemoryConfig
|
|
12
|
+
store = MemoryStore(MemoryConfig(db_path="./mem.db"))
|
|
13
|
+
store.remember(task="hello", outcome="success")
|
|
14
|
+
context = await store.retrieve("hello", config=MemoryConfig())
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from memlife.config import MemoryConfig
|
|
18
|
+
from memlife.models import Episode, Fact, JournalEntry
|
|
19
|
+
from memlife.protocols import ChatCallable, Embedder
|
|
20
|
+
from memlife.embedders import DummyEmbedder
|
|
21
|
+
from memlife.llm import DummyChat
|
|
22
|
+
from memlife.reflection import Reflector, ReflectionResult
|
|
23
|
+
from memlife.store import MemoryStore
|
|
24
|
+
from memlife.vectors import cosine, recency_weight
|
|
25
|
+
|
|
26
|
+
__version__ = "0.1.0b0"
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"MemoryStore",
|
|
30
|
+
"MemoryConfig",
|
|
31
|
+
"Reflector",
|
|
32
|
+
"ReflectionResult",
|
|
33
|
+
"Episode",
|
|
34
|
+
"Fact",
|
|
35
|
+
"JournalEntry",
|
|
36
|
+
"Embedder",
|
|
37
|
+
"ChatCallable",
|
|
38
|
+
"DummyEmbedder",
|
|
39
|
+
"DummyChat",
|
|
40
|
+
"cosine",
|
|
41
|
+
"recency_weight",
|
|
42
|
+
"retrieve",
|
|
43
|
+
"run_gc",
|
|
44
|
+
"export_jsonl",
|
|
45
|
+
"import_jsonl",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
# Convenience imports
|
|
49
|
+
from memlife.retrieval import retrieve
|
|
50
|
+
from memlife.gc import run_gc
|
|
51
|
+
from memlife.io import export_jsonl, import_jsonl
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Memory configuration — memory fields only, no agent config."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class MemoryConfig:
|
|
10
|
+
"""Configuration for the memory system.
|
|
11
|
+
|
|
12
|
+
All fields have sensible defaults. The store works with zero
|
|
13
|
+
configuration — just ``MemoryConfig()``.
|
|
14
|
+
|
|
15
|
+
Env vars (optional, override defaults):
|
|
16
|
+
MEMLIFE_DB_PATH, MEMLIFE_EMBEDDING_MODEL, etc.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
# Database
|
|
20
|
+
db_path: str = "./memlife.db"
|
|
21
|
+
|
|
22
|
+
# Embedding model name (stored with each vector for versioning)
|
|
23
|
+
embedding_model: str = ""
|
|
24
|
+
|
|
25
|
+
# Retrieval — how much context to inject before responding
|
|
26
|
+
recall_episodes: int = 5
|
|
27
|
+
recall_facts: int = 5
|
|
28
|
+
recall_journal: int = 3
|
|
29
|
+
working_window: int = 20
|
|
30
|
+
max_context_chars: int = 4000
|
|
31
|
+
recency_halflife_days: float = 14.0
|
|
32
|
+
|
|
33
|
+
# Strict recall cut-off + density dedup
|
|
34
|
+
recall_min_score: float = 0.0
|
|
35
|
+
recall_score_cutoff_ratio: float = 0.0
|
|
36
|
+
recall_dedup_threshold: float = 0.75
|
|
37
|
+
recall_dedup_method: str = "jaccard"
|
|
38
|
+
|
|
39
|
+
# Fact memory — cosine bands for merge and conflict detection
|
|
40
|
+
fact_merge_threshold: float = 0.90
|
|
41
|
+
fact_conflict_threshold: float = 0.75
|
|
42
|
+
|
|
43
|
+
# Reflection quality
|
|
44
|
+
reflect_critic: bool = True
|
|
45
|
+
critic_model: str = "" # empty = use primary model; set to a cheap model for the critic pass
|
|
46
|
+
significance_model: str = ""
|
|
47
|
+
journal_decay_halflife_days: float = 30.0
|
|
48
|
+
journal_decay_floor: float = 0.15
|
|
49
|
+
|
|
50
|
+
# Reflection timeouts
|
|
51
|
+
reflection_timeout: float = 120.0
|
|
52
|
+
reflection_total_timeout: float = 300.0
|
|
53
|
+
|
|
54
|
+
# GC retention (days)
|
|
55
|
+
gc_superseded_facts_days: int = 90
|
|
56
|
+
gc_superseded_journal_days: int = 90
|
|
57
|
+
gc_completed_runs_days: int = 60
|
|
58
|
+
gc_metrics_days: int = 30
|
|
59
|
+
gc_reflected_queue_days: int = 30
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
def from_env(cls) -> "MemoryConfig":
|
|
63
|
+
"""Load from environment variables with MEMLIFE_ prefix."""
|
|
64
|
+
import os
|
|
65
|
+
|
|
66
|
+
def _bool(name: str, default: bool) -> bool:
|
|
67
|
+
val = os.getenv(name)
|
|
68
|
+
if val is None:
|
|
69
|
+
return default
|
|
70
|
+
return val.strip().lower() in ("1", "true", "yes", "on")
|
|
71
|
+
|
|
72
|
+
return cls(
|
|
73
|
+
db_path=os.getenv("MEMLIFE_DB_PATH", "./memlife.db"),
|
|
74
|
+
embedding_model=os.getenv("MEMLIFE_EMBEDDING_MODEL", ""),
|
|
75
|
+
recall_episodes=int(os.getenv("MEMLIFE_RECALL_EPISODES", "5")),
|
|
76
|
+
recall_facts=int(os.getenv("MEMLIFE_RECALL_FACTS", "5")),
|
|
77
|
+
recall_journal=int(os.getenv("MEMLIFE_RECALL_JOURNAL", "3")),
|
|
78
|
+
working_window=int(os.getenv("MEMLIFE_WORKING_WINDOW", "20")),
|
|
79
|
+
max_context_chars=int(os.getenv("MEMLIFE_MAX_CONTEXT_CHARS", "4000")),
|
|
80
|
+
recency_halflife_days=float(os.getenv("MEMLIFE_RECENCY_HALFLIFE_DAYS", "14")),
|
|
81
|
+
recall_min_score=float(os.getenv("MEMLIFE_RECALL_MIN_SCORE", "0")),
|
|
82
|
+
recall_score_cutoff_ratio=float(os.getenv("MEMLIFE_RECALL_SCORE_CUTOFF_RATIO", "0")),
|
|
83
|
+
recall_dedup_threshold=float(os.getenv("MEMLIFE_RECALL_DEDUP_THRESHOLD", "0.75")),
|
|
84
|
+
recall_dedup_method=os.getenv("MEMLIFE_RECALL_DEDUP_METHOD", "jaccard"),
|
|
85
|
+
fact_merge_threshold=float(os.getenv("MEMLIFE_FACT_MERGE_THRESHOLD", "0.90")),
|
|
86
|
+
fact_conflict_threshold=float(os.getenv("MEMLIFE_FACT_CONFLICT_THRESHOLD", "0.75")),
|
|
87
|
+
reflect_critic=_bool("MEMLIFE_REFLECT_CRITIC", True),
|
|
88
|
+
critic_model=os.getenv("MEMLIFE_CRITIC_MODEL", ""),
|
|
89
|
+
journal_decay_halflife_days=float(os.getenv("MEMLIFE_JOURNAL_HALFLIFE_DAYS", "30")),
|
|
90
|
+
journal_decay_floor=float(os.getenv("MEMLIFE_JOURNAL_DECAY_FLOOR", "0.15")),
|
|
91
|
+
reflection_timeout=float(os.getenv("MEMLIFE_REFLECTION_TIMEOUT", "120")),
|
|
92
|
+
reflection_total_timeout=float(os.getenv("MEMLIFE_REFLECTION_TOTAL_TIMEOUT", "300")),
|
|
93
|
+
gc_superseded_facts_days=int(os.getenv("MEMLIFE_GC_SUPERSEDED_FACTS_DAYS", "90")),
|
|
94
|
+
gc_superseded_journal_days=int(os.getenv("MEMLIFE_GC_SUPERSEDED_JOURNAL_DAYS", "90")),
|
|
95
|
+
gc_completed_runs_days=int(os.getenv("MEMLIFE_GC_COMPLETED_RUNS_DAYS", "60")),
|
|
96
|
+
gc_metrics_days=int(os.getenv("MEMLIFE_GC_METRICS_DAYS", "30")),
|
|
97
|
+
gc_reflected_queue_days=int(os.getenv("MEMLIFE_GC_REFLECTED_QUEUE_DAYS", "30")),
|
|
98
|
+
)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""DummyEmbedder — hash-based embeddings for testing and quickstart.
|
|
2
|
+
|
|
3
|
+
Zero external dependencies, no API calls, deterministic.
|
|
4
|
+
Produces 128-dimensional vectors from SHA-256 hashes.
|
|
5
|
+
Good enough for keyword-like similarity, not for real semantic recall.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import hashlib
|
|
11
|
+
from typing import Sequence
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class DummyEmbedder:
|
|
15
|
+
"""Hash-based embeddings. No external dependencies.
|
|
16
|
+
|
|
17
|
+
Implements the Embedder protocol: ``await embedder.embed(texts)``.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
async def embed(self, texts: Sequence[str]) -> list[list[float]] | None:
|
|
21
|
+
return [self._hash_vector(t) for t in texts]
|
|
22
|
+
|
|
23
|
+
@staticmethod
|
|
24
|
+
def _hash_vector(text: str, dim: int = 128) -> list[float]:
|
|
25
|
+
h = hashlib.sha256(text.encode("utf-8")).digest()
|
|
26
|
+
return [((h[i % len(h)] / 255.0) - 0.5) * 2.0 for i in range(dim)]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Garbage collection for the memory store."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from memlife.store import MemoryStore
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def run_gc(
|
|
9
|
+
store: MemoryStore,
|
|
10
|
+
*,
|
|
11
|
+
superseded_facts_days: int = 90,
|
|
12
|
+
superseded_journal_days: int = 90,
|
|
13
|
+
completed_runs_days: int = 60,
|
|
14
|
+
metrics_days: int = 30,
|
|
15
|
+
reflected_queue_days: int = 30,
|
|
16
|
+
) -> dict:
|
|
17
|
+
"""Run garbage collection on old/superseded data.
|
|
18
|
+
|
|
19
|
+
Delegates to MemoryStore.run_gc() — this wrapper is the public entry
|
|
20
|
+
point. Most callers should use ``store.run_gc()`` directly.
|
|
21
|
+
"""
|
|
22
|
+
return store.run_gc(
|
|
23
|
+
superseded_facts_days=superseded_facts_days,
|
|
24
|
+
superseded_journal_days=superseded_journal_days,
|
|
25
|
+
completed_runs_days=completed_runs_days,
|
|
26
|
+
metrics_days=metrics_days,
|
|
27
|
+
reflected_queue_days=reflected_queue_days,
|
|
28
|
+
)
|