wavemind 2.0.0__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.
- wavemind-2.0.0/LICENSE +21 -0
- wavemind-2.0.0/PKG-INFO +134 -0
- wavemind-2.0.0/README.md +107 -0
- wavemind-2.0.0/pyproject.toml +45 -0
- wavemind-2.0.0/setup.cfg +4 -0
- wavemind-2.0.0/tests/test_api.py +52 -0
- wavemind-2.0.0/tests/test_api_process_persistence.py +112 -0
- wavemind-2.0.0/tests/test_cli_smoke.py +77 -0
- wavemind-2.0.0/tests/test_core_persistence.py +73 -0
- wavemind-2.0.0/tests/test_examples.py +53 -0
- wavemind-2.0.0/tests/test_import_benchmark.py +70 -0
- wavemind-2.0.0/tests/test_indexes_encoders.py +92 -0
- wavemind-2.0.0/tests/test_packaging_files.py +30 -0
- wavemind-2.0.0/tests/test_semantic_and_latency.py +149 -0
- wavemind-2.0.0/wavemind/__init__.py +22 -0
- wavemind-2.0.0/wavemind/__main__.py +5 -0
- wavemind-2.0.0/wavemind/api.py +177 -0
- wavemind-2.0.0/wavemind/benchmark.py +67 -0
- wavemind-2.0.0/wavemind/cli.py +245 -0
- wavemind-2.0.0/wavemind/core.py +428 -0
- wavemind-2.0.0/wavemind/encoders.py +127 -0
- wavemind-2.0.0/wavemind/importers.py +136 -0
- wavemind-2.0.0/wavemind/indexes.py +214 -0
- wavemind-2.0.0/wavemind/storage.py +222 -0
- wavemind-2.0.0/wavemind.egg-info/PKG-INFO +134 -0
- wavemind-2.0.0/wavemind.egg-info/SOURCES.txt +28 -0
- wavemind-2.0.0/wavemind.egg-info/dependency_links.txt +1 -0
- wavemind-2.0.0/wavemind.egg-info/entry_points.txt +2 -0
- wavemind-2.0.0/wavemind.egg-info/requires.txt +21 -0
- wavemind-2.0.0/wavemind.egg-info/top_level.txt +1 -0
wavemind-2.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 WaveMind contributors
|
|
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.
|
wavemind-2.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wavemind
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Persistent dynamic memory engine with vector search and wave-field re-ranking
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/CaspianG/wavemind
|
|
7
|
+
Project-URL: Repository, https://github.com/CaspianG/wavemind
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: numpy>=1.24
|
|
12
|
+
Requires-Dist: fastapi>=0.110
|
|
13
|
+
Requires-Dist: uvicorn[standard]>=0.27
|
|
14
|
+
Requires-Dist: pydantic>=2
|
|
15
|
+
Requires-Dist: pypdf>=4
|
|
16
|
+
Provides-Extra: sentence
|
|
17
|
+
Requires-Dist: sentence-transformers>=3; extra == "sentence"
|
|
18
|
+
Provides-Extra: ml
|
|
19
|
+
Requires-Dist: sentence-transformers>=3; extra == "ml"
|
|
20
|
+
Provides-Extra: indexes
|
|
21
|
+
Requires-Dist: annoy>=1.17; extra == "indexes"
|
|
22
|
+
Requires-Dist: faiss-cpu>=1.8; platform_system != "Windows" and extra == "indexes"
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
25
|
+
Requires-Dist: httpx>=0.27; extra == "dev"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# WaveMind is persistent dynamic memory for AI agents: vector search first, wave-field priority second, SQLite as the source of truth.
|
|
29
|
+
|
|
30
|
+

|
|
31
|
+
[](https://github.com/CaspianG/wavemind/actions/workflows/tests.yml)
|
|
32
|
+

|
|
33
|
+
|
|
34
|
+
## Terminal Demo
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
$ python examples/demo.py
|
|
38
|
+
✓ Remembered: "Andrey is a trader who tracks market breakouts."
|
|
39
|
+
✓ Remembered: "Andrey prefers short practical answers about AI agents."
|
|
40
|
+
|
|
41
|
+
Query: "Andrey trader agent"
|
|
42
|
+
→ Result 1 (0.54): "Andrey is a trader who tracks market breakouts."
|
|
43
|
+
→ Result 2 (0.30): "Andrey prefers short practical answers about AI agents."
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The demo is offline, keyless, and uses the built-in hash encoder.
|
|
47
|
+
|
|
48
|
+
## Quick Start
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
python -m pip install -e .
|
|
52
|
+
wavemind remember "Andrey is a trader" --namespace demo
|
|
53
|
+
wavemind query "trader" --namespace demo
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
This creates `wavemind.sqlite3` in your current working directory.
|
|
57
|
+
|
|
58
|
+
For sentence-transformer embeddings:
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
python -m pip install -e ".[sentence]"
|
|
62
|
+
wavemind --encoder sentence remember "Andrey is a trader" --namespace demo
|
|
63
|
+
wavemind --encoder sentence query "What does Andrey do?" --namespace demo
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
One-file setup scripts are also included:
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
sh install.sh
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```bat
|
|
73
|
+
install.bat
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Benchmark
|
|
77
|
+
|
|
78
|
+
Real Russian sentences from Tatoeba, 50 one-word queries, NumPy exact index.
|
|
79
|
+
|
|
80
|
+
| metric | hash | sentence-transformers |
|
|
81
|
+
|---|---:|---:|
|
|
82
|
+
| precision@1 | 1.00 | 1.00 |
|
|
83
|
+
| precision@3 | 1.00 | 1.00 |
|
|
84
|
+
| avg query | 0.49 ms | 52.84 ms |
|
|
85
|
+
|
|
86
|
+
Capacity check with the hash encoder:
|
|
87
|
+
|
|
88
|
+
| memories | precision@1 | precision@3 | avg query |
|
|
89
|
+
|---:|---:|---:|---:|
|
|
90
|
+
| 200 | 1.00 | 1.00 | 0.49 ms |
|
|
91
|
+
| 1000 | 0.88 | 1.00 | 1.50 ms |
|
|
92
|
+
| 5000 | 0.72 | 0.88 | 5.68 ms |
|
|
93
|
+
|
|
94
|
+
Run locally:
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
python benchmarks/ru_sentences_benchmark.py --sentences 200 --queries 50 --encoder hash --index numpy
|
|
98
|
+
python benchmarks/ru_sentences_benchmark.py --sentences 200 --queries 50 --encoder sentence --index numpy
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Comparison
|
|
102
|
+
|
|
103
|
+
| feature | WaveMind | Chroma | Qdrant |
|
|
104
|
+
|---|---|---|---|
|
|
105
|
+
| Primary role | Agent memory engine | Embedding database | Production vector database |
|
|
106
|
+
| Local SQLite persistence | Yes | Yes | No, separate service/storage |
|
|
107
|
+
| HTTP API | FastAPI included | Included | Included |
|
|
108
|
+
| Dynamic memory priority | Wave-field hotness, TTL, priority | Metadata/filter driven | Payload/filter driven |
|
|
109
|
+
| Built-in forgetting | TTL and explicit forget | Manual delete/filtering | Manual delete/filtering |
|
|
110
|
+
| Best fit | Small to medium agent memory with dynamic recall | Local RAG apps and prototypes | Large-scale vector search |
|
|
111
|
+
| Scale target today | Up to 1000 optimal on NumPy, FAISS recommended beyond 5000 | Larger than WaveMind local mode | Production scale |
|
|
112
|
+
|
|
113
|
+
WaveMind is not trying to replace dedicated vector databases at scale. Its difference is dynamic priority: frequently used memories can become hotter while old or low-priority memories fade.
|
|
114
|
+
|
|
115
|
+
## Known Limitations
|
|
116
|
+
|
|
117
|
+
- Optimal capacity on the current NumPy exact index is up to 1000 records.
|
|
118
|
+
- At 5000 records, one-word `precision@1` is currently 0.72 with the hash encoder; many misses are ambiguous queries where another sentence containing the same word ranks first.
|
|
119
|
+
- For `N > 5000`, use the FAISS backend with `--index faiss` or another production vector index.
|
|
120
|
+
- `sentence-transformers/paraphrase-multilingual-mpnet-base-v2` requires about 420 MB of model files and measured about 53 ms per query on the benchmark machine.
|
|
121
|
+
- The bundled benchmark is a retrieval sanity check, not a full agent-memory benchmark against Chroma or Qdrant yet.
|
|
122
|
+
|
|
123
|
+
## Roadmap
|
|
124
|
+
|
|
125
|
+
- FAISS-first production index path with persisted index rebuilds.
|
|
126
|
+
- Larger public benchmark against Chroma and Qdrant on agent-memory tasks.
|
|
127
|
+
- Better semantic query expansion for short and ambiguous queries.
|
|
128
|
+
- Namespace quotas, backups, and daemon hardening for SaaS use.
|
|
129
|
+
- Webhook on recall for agent runtimes.
|
|
130
|
+
- OHLCV pattern-memory experiments for market research and backtests.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT. See [LICENSE](LICENSE).
|
wavemind-2.0.0/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# WaveMind is persistent dynamic memory for AI agents: vector search first, wave-field priority second, SQLite as the source of truth.
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://github.com/CaspianG/wavemind/actions/workflows/tests.yml)
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
## Terminal Demo
|
|
8
|
+
|
|
9
|
+
```text
|
|
10
|
+
$ python examples/demo.py
|
|
11
|
+
✓ Remembered: "Andrey is a trader who tracks market breakouts."
|
|
12
|
+
✓ Remembered: "Andrey prefers short practical answers about AI agents."
|
|
13
|
+
|
|
14
|
+
Query: "Andrey trader agent"
|
|
15
|
+
→ Result 1 (0.54): "Andrey is a trader who tracks market breakouts."
|
|
16
|
+
→ Result 2 (0.30): "Andrey prefers short practical answers about AI agents."
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The demo is offline, keyless, and uses the built-in hash encoder.
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
python -m pip install -e .
|
|
25
|
+
wavemind remember "Andrey is a trader" --namespace demo
|
|
26
|
+
wavemind query "trader" --namespace demo
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
This creates `wavemind.sqlite3` in your current working directory.
|
|
30
|
+
|
|
31
|
+
For sentence-transformer embeddings:
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
python -m pip install -e ".[sentence]"
|
|
35
|
+
wavemind --encoder sentence remember "Andrey is a trader" --namespace demo
|
|
36
|
+
wavemind --encoder sentence query "What does Andrey do?" --namespace demo
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
One-file setup scripts are also included:
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
sh install.sh
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```bat
|
|
46
|
+
install.bat
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Benchmark
|
|
50
|
+
|
|
51
|
+
Real Russian sentences from Tatoeba, 50 one-word queries, NumPy exact index.
|
|
52
|
+
|
|
53
|
+
| metric | hash | sentence-transformers |
|
|
54
|
+
|---|---:|---:|
|
|
55
|
+
| precision@1 | 1.00 | 1.00 |
|
|
56
|
+
| precision@3 | 1.00 | 1.00 |
|
|
57
|
+
| avg query | 0.49 ms | 52.84 ms |
|
|
58
|
+
|
|
59
|
+
Capacity check with the hash encoder:
|
|
60
|
+
|
|
61
|
+
| memories | precision@1 | precision@3 | avg query |
|
|
62
|
+
|---:|---:|---:|---:|
|
|
63
|
+
| 200 | 1.00 | 1.00 | 0.49 ms |
|
|
64
|
+
| 1000 | 0.88 | 1.00 | 1.50 ms |
|
|
65
|
+
| 5000 | 0.72 | 0.88 | 5.68 ms |
|
|
66
|
+
|
|
67
|
+
Run locally:
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
python benchmarks/ru_sentences_benchmark.py --sentences 200 --queries 50 --encoder hash --index numpy
|
|
71
|
+
python benchmarks/ru_sentences_benchmark.py --sentences 200 --queries 50 --encoder sentence --index numpy
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Comparison
|
|
75
|
+
|
|
76
|
+
| feature | WaveMind | Chroma | Qdrant |
|
|
77
|
+
|---|---|---|---|
|
|
78
|
+
| Primary role | Agent memory engine | Embedding database | Production vector database |
|
|
79
|
+
| Local SQLite persistence | Yes | Yes | No, separate service/storage |
|
|
80
|
+
| HTTP API | FastAPI included | Included | Included |
|
|
81
|
+
| Dynamic memory priority | Wave-field hotness, TTL, priority | Metadata/filter driven | Payload/filter driven |
|
|
82
|
+
| Built-in forgetting | TTL and explicit forget | Manual delete/filtering | Manual delete/filtering |
|
|
83
|
+
| Best fit | Small to medium agent memory with dynamic recall | Local RAG apps and prototypes | Large-scale vector search |
|
|
84
|
+
| Scale target today | Up to 1000 optimal on NumPy, FAISS recommended beyond 5000 | Larger than WaveMind local mode | Production scale |
|
|
85
|
+
|
|
86
|
+
WaveMind is not trying to replace dedicated vector databases at scale. Its difference is dynamic priority: frequently used memories can become hotter while old or low-priority memories fade.
|
|
87
|
+
|
|
88
|
+
## Known Limitations
|
|
89
|
+
|
|
90
|
+
- Optimal capacity on the current NumPy exact index is up to 1000 records.
|
|
91
|
+
- At 5000 records, one-word `precision@1` is currently 0.72 with the hash encoder; many misses are ambiguous queries where another sentence containing the same word ranks first.
|
|
92
|
+
- For `N > 5000`, use the FAISS backend with `--index faiss` or another production vector index.
|
|
93
|
+
- `sentence-transformers/paraphrase-multilingual-mpnet-base-v2` requires about 420 MB of model files and measured about 53 ms per query on the benchmark machine.
|
|
94
|
+
- The bundled benchmark is a retrieval sanity check, not a full agent-memory benchmark against Chroma or Qdrant yet.
|
|
95
|
+
|
|
96
|
+
## Roadmap
|
|
97
|
+
|
|
98
|
+
- FAISS-first production index path with persisted index rebuilds.
|
|
99
|
+
- Larger public benchmark against Chroma and Qdrant on agent-memory tasks.
|
|
100
|
+
- Better semantic query expansion for short and ambiguous queries.
|
|
101
|
+
- Namespace quotas, backups, and daemon hardening for SaaS use.
|
|
102
|
+
- Webhook on recall for agent runtimes.
|
|
103
|
+
- OHLCV pattern-memory experiments for market research and backtests.
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "wavemind"
|
|
7
|
+
version = "2.0.0"
|
|
8
|
+
description = "Persistent dynamic memory engine with vector search and wave-field re-ranking"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"numpy>=1.24",
|
|
14
|
+
"fastapi>=0.110",
|
|
15
|
+
"uvicorn[standard]>=0.27",
|
|
16
|
+
"pydantic>=2",
|
|
17
|
+
"pypdf>=4",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
sentence = [
|
|
22
|
+
"sentence-transformers>=3",
|
|
23
|
+
]
|
|
24
|
+
ml = [
|
|
25
|
+
"sentence-transformers>=3",
|
|
26
|
+
]
|
|
27
|
+
indexes = [
|
|
28
|
+
"annoy>=1.17",
|
|
29
|
+
"faiss-cpu>=1.8; platform_system != 'Windows'",
|
|
30
|
+
]
|
|
31
|
+
dev = [
|
|
32
|
+
"pytest>=8",
|
|
33
|
+
"httpx>=0.27",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[project.scripts]
|
|
37
|
+
wavemind = "wavemind.cli:main"
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
Homepage = "https://github.com/CaspianG/wavemind"
|
|
41
|
+
Repository = "https://github.com/CaspianG/wavemind"
|
|
42
|
+
|
|
43
|
+
[tool.setuptools.packages.find]
|
|
44
|
+
where = ["."]
|
|
45
|
+
include = ["wavemind*"]
|
wavemind-2.0.0/setup.cfg
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from fastapi.testclient import TestClient
|
|
2
|
+
|
|
3
|
+
from wavemind import HashingTextEncoder, WaveMind
|
|
4
|
+
from wavemind.api import create_app
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_fastapi_remember_query_forget_and_stats(tmp_path):
|
|
8
|
+
mind = WaveMind(
|
|
9
|
+
db_path=tmp_path / "api.sqlite3",
|
|
10
|
+
width=32,
|
|
11
|
+
height=32,
|
|
12
|
+
layers=2,
|
|
13
|
+
encoder=HashingTextEncoder(vector_dim=64),
|
|
14
|
+
score_threshold=0.05,
|
|
15
|
+
)
|
|
16
|
+
client = TestClient(create_app(mind=mind))
|
|
17
|
+
|
|
18
|
+
remember = client.post(
|
|
19
|
+
"/remember",
|
|
20
|
+
json={
|
|
21
|
+
"text": "кошка сидит на подоконнике",
|
|
22
|
+
"namespace": "pets",
|
|
23
|
+
"tags": ["animal"],
|
|
24
|
+
"ttl_seconds": 3600,
|
|
25
|
+
},
|
|
26
|
+
)
|
|
27
|
+
assert remember.status_code == 200
|
|
28
|
+
memory_id = remember.json()["id"]
|
|
29
|
+
|
|
30
|
+
query = client.post(
|
|
31
|
+
"/query",
|
|
32
|
+
json={"text": "кошка", "namespace": "pets", "top_k": 3, "tags": ["animal"]},
|
|
33
|
+
)
|
|
34
|
+
assert query.status_code == 200
|
|
35
|
+
assert query.json()["results"][0]["id"] == memory_id
|
|
36
|
+
assert query.json()["results"][0]["text"] == "кошка сидит на подоконнике"
|
|
37
|
+
|
|
38
|
+
stats = client.get("/stats", params={"namespace": "pets"})
|
|
39
|
+
assert stats.status_code == 200
|
|
40
|
+
assert stats.json()["active_memories"] == 1
|
|
41
|
+
|
|
42
|
+
deleted = client.request(
|
|
43
|
+
"DELETE",
|
|
44
|
+
"/forget",
|
|
45
|
+
json={"id": memory_id, "namespace": "pets"},
|
|
46
|
+
)
|
|
47
|
+
assert deleted.status_code == 200
|
|
48
|
+
assert deleted.json()["deleted"] == 1
|
|
49
|
+
|
|
50
|
+
empty = client.post("/query", json={"text": "кошка", "namespace": "pets"})
|
|
51
|
+
assert empty.json()["results"] == []
|
|
52
|
+
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import socket
|
|
2
|
+
import subprocess
|
|
3
|
+
import sys
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def free_port() -> int:
|
|
10
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
11
|
+
sock.bind(("127.0.0.1", 0))
|
|
12
|
+
return int(sock.getsockname()[1])
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def start_api(db_path, port: int) -> subprocess.Popen:
|
|
16
|
+
return subprocess.Popen(
|
|
17
|
+
[
|
|
18
|
+
sys.executable,
|
|
19
|
+
"-m",
|
|
20
|
+
"wavemind",
|
|
21
|
+
"--db",
|
|
22
|
+
str(db_path),
|
|
23
|
+
"--score-threshold",
|
|
24
|
+
"0.05",
|
|
25
|
+
"serve",
|
|
26
|
+
"--host",
|
|
27
|
+
"127.0.0.1",
|
|
28
|
+
"--port",
|
|
29
|
+
str(port),
|
|
30
|
+
],
|
|
31
|
+
stdout=subprocess.PIPE,
|
|
32
|
+
stderr=subprocess.PIPE,
|
|
33
|
+
text=True,
|
|
34
|
+
encoding="utf-8",
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def wait_until_ready(base_url: str, process: subprocess.Popen) -> None:
|
|
39
|
+
deadline = time.time() + 20
|
|
40
|
+
last_error = None
|
|
41
|
+
with httpx.Client(trust_env=False) as client:
|
|
42
|
+
while time.time() < deadline:
|
|
43
|
+
if process.poll() is not None:
|
|
44
|
+
stdout, stderr = process.communicate(timeout=1)
|
|
45
|
+
raise AssertionError(
|
|
46
|
+
f"API process exited early with {process.returncode}\nSTDOUT:\n{stdout}\nSTDERR:\n{stderr}"
|
|
47
|
+
)
|
|
48
|
+
try:
|
|
49
|
+
response = client.get(f"{base_url}/stats", timeout=1)
|
|
50
|
+
if response.status_code == 200:
|
|
51
|
+
return
|
|
52
|
+
except (httpx.NetworkError, httpx.TimeoutException) as exc:
|
|
53
|
+
last_error = exc
|
|
54
|
+
time.sleep(0.25)
|
|
55
|
+
raise AssertionError(f"API did not become ready: {last_error}")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def kill_process(process: subprocess.Popen) -> None:
|
|
59
|
+
if process.poll() is not None:
|
|
60
|
+
return
|
|
61
|
+
process.kill()
|
|
62
|
+
try:
|
|
63
|
+
process.communicate(timeout=5)
|
|
64
|
+
except subprocess.TimeoutExpired:
|
|
65
|
+
process.terminate()
|
|
66
|
+
process.communicate(timeout=5)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def test_api_persists_10_memories_across_process_restart(tmp_path):
|
|
70
|
+
db_path = tmp_path / "persistent.sqlite3"
|
|
71
|
+
port = free_port()
|
|
72
|
+
base_url = f"http://127.0.0.1:{port}"
|
|
73
|
+
memories = [
|
|
74
|
+
f"wmrestart{i:02d} контрольная память номер {i} сохраняется в sqlite"
|
|
75
|
+
for i in range(10)
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
first = start_api(db_path, port)
|
|
79
|
+
try:
|
|
80
|
+
wait_until_ready(base_url, first)
|
|
81
|
+
with httpx.Client(trust_env=False) as client:
|
|
82
|
+
for text in memories:
|
|
83
|
+
response = client.post(
|
|
84
|
+
f"{base_url}/remember",
|
|
85
|
+
json={"text": text, "namespace": "restart", "tags": ["persistence"]},
|
|
86
|
+
timeout=5,
|
|
87
|
+
)
|
|
88
|
+
assert response.status_code == 200
|
|
89
|
+
assert response.json()["id"] > 0
|
|
90
|
+
finally:
|
|
91
|
+
kill_process(first)
|
|
92
|
+
|
|
93
|
+
second = start_api(db_path, port)
|
|
94
|
+
try:
|
|
95
|
+
wait_until_ready(base_url, second)
|
|
96
|
+
with httpx.Client(trust_env=False) as client:
|
|
97
|
+
for i, expected in enumerate(memories):
|
|
98
|
+
response = client.post(
|
|
99
|
+
f"{base_url}/query",
|
|
100
|
+
json={
|
|
101
|
+
"text": f"wmrestart{i:02d}",
|
|
102
|
+
"namespace": "restart",
|
|
103
|
+
"tags": ["persistence"],
|
|
104
|
+
"top_k": 3,
|
|
105
|
+
},
|
|
106
|
+
timeout=5,
|
|
107
|
+
)
|
|
108
|
+
assert response.status_code == 200
|
|
109
|
+
texts = [item["text"] for item in response.json()["results"]]
|
|
110
|
+
assert expected in texts
|
|
111
|
+
finally:
|
|
112
|
+
kill_process(second)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import subprocess
|
|
2
|
+
import sys
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def run_cli(*args, cwd=None):
|
|
9
|
+
env = os.environ.copy()
|
|
10
|
+
project_root = Path(__file__).resolve().parents[1]
|
|
11
|
+
env["PYTHONPATH"] = str(project_root) + os.pathsep + env.get("PYTHONPATH", "")
|
|
12
|
+
return subprocess.run(
|
|
13
|
+
[sys.executable, "-m", "wavemind", *args],
|
|
14
|
+
cwd=cwd,
|
|
15
|
+
env=env,
|
|
16
|
+
text=True,
|
|
17
|
+
encoding="utf-8",
|
|
18
|
+
capture_output=True,
|
|
19
|
+
check=True,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_module_cli_remember_query_stats_and_backup(tmp_path):
|
|
24
|
+
db_path = tmp_path / "cli.sqlite3"
|
|
25
|
+
backup_path = tmp_path / "backup.sqlite3"
|
|
26
|
+
|
|
27
|
+
remembered = run_cli(
|
|
28
|
+
"--db",
|
|
29
|
+
str(db_path),
|
|
30
|
+
"remember",
|
|
31
|
+
"кошка спит на окне",
|
|
32
|
+
"--namespace",
|
|
33
|
+
"cli",
|
|
34
|
+
"--tag",
|
|
35
|
+
"animal",
|
|
36
|
+
)
|
|
37
|
+
assert "remembered id=" in remembered.stdout
|
|
38
|
+
|
|
39
|
+
queried = run_cli("--db", str(db_path), "query", "кошка", "--namespace", "cli")
|
|
40
|
+
assert "кошка спит на окне" in queried.stdout
|
|
41
|
+
|
|
42
|
+
stats = run_cli("--db", str(db_path), "stats", "--namespace", "cli")
|
|
43
|
+
assert "active_memories: 1" in stats.stdout
|
|
44
|
+
|
|
45
|
+
backup = run_cli("--db", str(db_path), "backup", "--out", str(backup_path))
|
|
46
|
+
assert "backup:" in backup.stdout
|
|
47
|
+
assert backup_path.exists()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_legacy_script_delegates_to_new_cli(tmp_path):
|
|
51
|
+
result = subprocess.run(
|
|
52
|
+
[sys.executable, "wavemind_v2.py", "--help"],
|
|
53
|
+
cwd=".",
|
|
54
|
+
text=True,
|
|
55
|
+
encoding="utf-8",
|
|
56
|
+
capture_output=True,
|
|
57
|
+
check=True,
|
|
58
|
+
)
|
|
59
|
+
assert "WaveMind" in result.stdout
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_cli_benchmark_seeds_all_synthetic_cases(tmp_path):
|
|
63
|
+
db_path = tmp_path / "bench.sqlite3"
|
|
64
|
+
|
|
65
|
+
result = run_cli("--db", str(db_path), "benchmark")
|
|
66
|
+
report = json.loads(result.stdout)
|
|
67
|
+
|
|
68
|
+
assert report["capacity"] == 4
|
|
69
|
+
assert report["recall_at_k"] == 1.0
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_cli_default_database_is_created_in_working_directory(tmp_path):
|
|
73
|
+
result = run_cli("remember", "portable default database memory", cwd=tmp_path)
|
|
74
|
+
|
|
75
|
+
assert "remembered id=" in result.stdout
|
|
76
|
+
assert (tmp_path / "wavemind.sqlite3").exists()
|
|
77
|
+
assert not (tmp_path / "data").exists()
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from wavemind import HashingTextEncoder, QueryResult, WaveMind
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def make_mind(db_path: Path, **kwargs) -> WaveMind:
|
|
7
|
+
params = {
|
|
8
|
+
"db_path": db_path,
|
|
9
|
+
"width": 32,
|
|
10
|
+
"height": 32,
|
|
11
|
+
"layers": 2,
|
|
12
|
+
"encoder": HashingTextEncoder(vector_dim=64),
|
|
13
|
+
"score_threshold": 0.05,
|
|
14
|
+
}
|
|
15
|
+
params.update(kwargs)
|
|
16
|
+
return WaveMind(**params)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_remember_query_persist_and_load(tmp_path):
|
|
20
|
+
db_path = tmp_path / "memory.sqlite3"
|
|
21
|
+
mind = make_mind(db_path)
|
|
22
|
+
|
|
23
|
+
first_id = mind.remember(
|
|
24
|
+
"кошка сидит на подоконнике",
|
|
25
|
+
namespace="pets",
|
|
26
|
+
tags=["animal", "home"],
|
|
27
|
+
metadata={"source": "unit"},
|
|
28
|
+
)
|
|
29
|
+
mind.remember("собака лает во дворе", namespace="pets", tags=["animal"])
|
|
30
|
+
mind.remember("market signal breaks resistance", namespace="markets", tags=["trading"])
|
|
31
|
+
mind.save()
|
|
32
|
+
|
|
33
|
+
results = mind.query("кошка", namespace="pets", top_k=2)
|
|
34
|
+
assert isinstance(results[0], QueryResult)
|
|
35
|
+
assert results[0].id == first_id
|
|
36
|
+
assert results[0].text == "кошка сидит на подоконнике"
|
|
37
|
+
assert results[0].namespace == "pets"
|
|
38
|
+
assert set(results[0].tags) == {"animal", "home"}
|
|
39
|
+
|
|
40
|
+
reloaded = make_mind(db_path)
|
|
41
|
+
reloaded.load()
|
|
42
|
+
reloaded_results = reloaded.query("кошка", namespace="pets", top_k=1)
|
|
43
|
+
assert reloaded_results[0].text == "кошка сидит на подоконнике"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_namespace_tags_threshold_ttl_and_forget(tmp_path):
|
|
47
|
+
db_path = tmp_path / "memory.sqlite3"
|
|
48
|
+
mind = make_mind(db_path, score_threshold=0.20)
|
|
49
|
+
|
|
50
|
+
keep_id = mind.remember("alpha project launch checklist", namespace="work", tags=["project"])
|
|
51
|
+
mind.remember("alpha private journal note", namespace="personal", tags=["journal"])
|
|
52
|
+
expired_id = mind.remember(
|
|
53
|
+
"alpha obsolete task",
|
|
54
|
+
namespace="work",
|
|
55
|
+
tags=["project"],
|
|
56
|
+
ttl_seconds=-1,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
work_results = mind.query("alpha", namespace="work", tags=["project"], top_k=5)
|
|
60
|
+
assert [result.id for result in work_results] == [keep_id]
|
|
61
|
+
assert all(result.score >= 0.20 for result in work_results)
|
|
62
|
+
|
|
63
|
+
stats = mind.stats(namespace="work")
|
|
64
|
+
assert stats["active_memories"] == 1
|
|
65
|
+
assert stats["expired_memories"] == 1
|
|
66
|
+
|
|
67
|
+
removed = mind.forget(id=keep_id, namespace="work")
|
|
68
|
+
assert removed == 1
|
|
69
|
+
assert mind.query("alpha", namespace="work", top_k=5) == []
|
|
70
|
+
|
|
71
|
+
purged = mind.purge_expired()
|
|
72
|
+
assert purged == 1
|
|
73
|
+
assert mind.store.get(expired_id) is None
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import importlib.util
|
|
2
|
+
import os
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def load_example():
|
|
9
|
+
path = Path("examples/agent_with_memory.py")
|
|
10
|
+
spec = importlib.util.spec_from_file_location("agent_with_memory", path)
|
|
11
|
+
module = importlib.util.module_from_spec(spec)
|
|
12
|
+
spec.loader.exec_module(module)
|
|
13
|
+
return module
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_agent_example_uses_environment_key_not_hardcoded_secret():
|
|
17
|
+
text = Path("examples/agent_with_memory.py").read_text(encoding="utf-8")
|
|
18
|
+
|
|
19
|
+
assert "sk-or-v1-" not in text
|
|
20
|
+
assert "OPENROUTER_API_KEY" in text
|
|
21
|
+
assert "https://openrouter.ai/api/v1/chat/completions" in text
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_offline_demo_prints_recall_flow():
|
|
25
|
+
env = os.environ.copy()
|
|
26
|
+
project_root = Path(__file__).resolve().parents[1]
|
|
27
|
+
env["PYTHONPATH"] = str(project_root) + os.pathsep + env.get("PYTHONPATH", "")
|
|
28
|
+
|
|
29
|
+
result = subprocess.run(
|
|
30
|
+
[sys.executable, "examples/demo.py"],
|
|
31
|
+
cwd=project_root,
|
|
32
|
+
env=env,
|
|
33
|
+
text=True,
|
|
34
|
+
encoding="utf-8",
|
|
35
|
+
capture_output=True,
|
|
36
|
+
check=True,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
assert '✓ Remembered: "' in result.stdout
|
|
40
|
+
assert 'Query: "' in result.stdout
|
|
41
|
+
assert "→ Result 1" in result.stdout
|
|
42
|
+
assert "→ Result 2" in result.stdout
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_agent_example_remembers_and_recalls_user_profile(tmp_path):
|
|
46
|
+
module = load_example()
|
|
47
|
+
memory = module.build_memory(tmp_path / "agent.sqlite3")
|
|
48
|
+
|
|
49
|
+
module.observe_user_message(memory, "меня зовут Андрей, я трейдер")
|
|
50
|
+
context = module.recall_memory(memory, "как меня зовут?")
|
|
51
|
+
|
|
52
|
+
assert "Андрей" in context
|
|
53
|
+
assert "трейдер" in context
|