memwal 0.1.11.dev9__tar.gz → 0.1.11.dev11__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.
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/CHANGELOG.md +5 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/PKG-INFO +2 -1
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/README.md +1 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/memwal/__init__.py +1 -1
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/memwal/client.py +95 -2
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/memwal/middleware.py +106 -15
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/pyproject.toml +1 -1
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_client.py +148 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_middleware.py +154 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/.gitignore +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/examples/.env.example +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/examples/.gitignore +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/examples/async_remember_demo.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/examples/interactive_demo.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/examples/verify_credentials.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/memwal/compatibility.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/memwal/mock.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/memwal/types.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/memwal/utils.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/notebooks/walrus_memory_python_sdk.ipynb +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/run_tests.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/__init__.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_auth_rejected_message.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_blank_text.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_env_presets.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_integration.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_list_namespaces.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_mock.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_normalize_server_url.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_polling_delay.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_remember_derived_key.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_retry_after_http_date.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_signing.py +0 -0
- {memwal-0.1.11.dev9 → memwal-0.1.11.dev11}/tests/test_transport_retry.py +0 -0
|
@@ -2,8 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- `get_remember_status(job_id)` on `MemWal` and `MemWalSync` returns `RememberJobStatus` from `GET /api/remember/{job_id}`, accepting 200 and 404 like the TypeScript SDK. (WALM-832, #1096)
|
|
8
|
+
|
|
5
9
|
### Fixed
|
|
6
10
|
|
|
11
|
+
- `get_remember_status` and `wait_for_remember_job` reject a job id outside `[A-Za-z0-9_-]+` before the request. `.`, `..`, a backslash, and encoded dot-dot used to pass a denylist and be rewritten after signing. An empty id used to call `GET /api/remember/` and raise `json.JSONDecodeError`. An unparseable 404 is `not_found` / `MemWalRememberJobNotFound`, not a JSON error. `uuid.UUID` is coerced with `str()`. Valid UUID job ids are unchanged. (WALM-833, #1100, #1216)
|
|
7
12
|
- `remember`, `remember_bulk`, `analyze`, and `embed` reject whitespace-only text before the request. A blank body used to be accepted and then sit until the caller timed out. (#1129)
|
|
8
13
|
- A 401 `AUTH_REJECTED` now says a wallet address is not a MemWal account ID. (#1132)
|
|
9
14
|
- `wait_for_remember_jobs` / `remember_bulk_and_wait` no longer return a batch they could not read as if it were still uploading. When every status poll is rate-limited, the wait makes one confirming read and raises `MemWalRateLimited` (`status` 429, `job_ids`, `retry_after`) if that is refused too. An item still unsettled at the deadline names its last known status, or says no read got through. (WALM-671, #967)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: memwal
|
|
3
|
-
Version: 0.1.11.
|
|
3
|
+
Version: 0.1.11.dev11
|
|
4
4
|
Summary: Python SDK for Walrus Memory — Privacy-first AI memory with Ed25519 signing
|
|
5
5
|
Project-URL: Homepage, https://memory.walrus.xyz
|
|
6
6
|
Project-URL: Documentation, https://memory.walrus.xyz
|
|
@@ -240,6 +240,7 @@ Create a new async client.
|
|
|
240
240
|
| Method | Description |
|
|
241
241
|
|--------|-------------|
|
|
242
242
|
| `await remember(text, namespace?)` | Accept a background remember job and return `job_id` |
|
|
243
|
+
| `await get_remember_status(job_id)` | Read one remember job without waiting |
|
|
243
244
|
| `await wait_for_remember_job(job_id, ...)` | Poll one remember job until it is searchable |
|
|
244
245
|
| `await remember_and_wait(text, namespace?, ...)` | Store a memory and wait until it is searchable |
|
|
245
246
|
| `await remember_bulk(items)` | Accept several background remember jobs |
|
|
@@ -201,6 +201,7 @@ Create a new async client.
|
|
|
201
201
|
| Method | Description |
|
|
202
202
|
|--------|-------------|
|
|
203
203
|
| `await remember(text, namespace?)` | Accept a background remember job and return `job_id` |
|
|
204
|
+
| `await get_remember_status(job_id)` | Read one remember job without waiting |
|
|
204
205
|
| `await wait_for_remember_job(job_id, ...)` | Poll one remember job until it is searchable |
|
|
205
206
|
| `await remember_and_wait(text, namespace?, ...)` | Store a memory and wait until it is searchable |
|
|
206
207
|
| `await remember_bulk(items)` | Accept several background remember jobs |
|
|
@@ -69,6 +69,7 @@ from .types import (
|
|
|
69
69
|
RememberBulkResult,
|
|
70
70
|
RememberBulkStatusItem,
|
|
71
71
|
RememberBulkStatusResult,
|
|
72
|
+
RememberJobStatus,
|
|
72
73
|
RememberManualOptions,
|
|
73
74
|
RememberManualResult,
|
|
74
75
|
RememberResult,
|
|
@@ -156,6 +157,36 @@ def _derived_idempotency_key(request_identity: str) -> str:
|
|
|
156
157
|
return f"r1-{hashlib.sha256(payload).hexdigest()}"
|
|
157
158
|
|
|
158
159
|
|
|
160
|
+
def _require_remember_job_id(job_id: object) -> str:
|
|
161
|
+
"""String form of a remember job id.
|
|
162
|
+
|
|
163
|
+
``uuid.UUID`` used to interpolate into the path and still does. Any other
|
|
164
|
+
non-string is a type mistake: ``not_found`` would hide a job that exists.
|
|
165
|
+
"""
|
|
166
|
+
|
|
167
|
+
if isinstance(job_id, uuid.UUID):
|
|
168
|
+
return str(job_id)
|
|
169
|
+
if not isinstance(job_id, str):
|
|
170
|
+
raise TypeError(
|
|
171
|
+
f"remember job id must be a string, got {type(job_id).__name__}"
|
|
172
|
+
)
|
|
173
|
+
return job_id
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _remember_status_path(job_id: str) -> Optional[str]:
|
|
177
|
+
"""``/api/remember/{job_id}`` when the id is one safe segment, else None.
|
|
178
|
+
|
|
179
|
+
httpx rewrites the path after it is signed. ``.`` requests
|
|
180
|
+
``/api/remember``, ``..`` requests ``/api``, and a NUL raises
|
|
181
|
+
``InvalidURL``. An allowlist keeps those from leaving the client. A
|
|
182
|
+
normal UUID is unchanged. (#1100, #1216)
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
if re.fullmatch(r"[A-Za-z0-9_-]+", job_id) is None:
|
|
186
|
+
return None
|
|
187
|
+
return f"/api/remember/{job_id}"
|
|
188
|
+
|
|
189
|
+
|
|
159
190
|
def normalize_server_url(url: str) -> str:
|
|
160
191
|
"""Strip a trailing slash and warn on plaintext HTTP to a remote host.
|
|
161
192
|
|
|
@@ -524,6 +555,55 @@ class MemWal:
|
|
|
524
555
|
remaining_s = (deadline_ms - _now_ms()) / 1000.0
|
|
525
556
|
return max(0.001, min(60.0, remaining_s))
|
|
526
557
|
|
|
558
|
+
async def get_remember_status(self, job_id: str) -> RememberJobStatus:
|
|
559
|
+
"""One-shot lookup of a remember job. Mirrors TS ``getRememberStatus``.
|
|
560
|
+
|
|
561
|
+
``GET /api/remember/{job_id}``, accepting 200 and 404. A 404, or a
|
|
562
|
+
body with no ``status``, is ``not_found`` — not an exception. A job
|
|
563
|
+
id outside ``[A-Za-z0-9_-]+`` is ``not_found`` without a request.
|
|
564
|
+
"""
|
|
565
|
+
|
|
566
|
+
job_id = _require_remember_job_id(job_id)
|
|
567
|
+
path = _remember_status_path(job_id)
|
|
568
|
+
if path is None:
|
|
569
|
+
return RememberJobStatus(
|
|
570
|
+
job_id=job_id,
|
|
571
|
+
status="not_found",
|
|
572
|
+
error=f"Job {job_id} not found",
|
|
573
|
+
)
|
|
574
|
+
try:
|
|
575
|
+
data = await self._signed_request(
|
|
576
|
+
"GET",
|
|
577
|
+
path,
|
|
578
|
+
{},
|
|
579
|
+
accepted_statuses=(200, 404),
|
|
580
|
+
# Status is metadata. No SEAL decrypt, so no session. Matches TS.
|
|
581
|
+
include_seal_session=False,
|
|
582
|
+
)
|
|
583
|
+
except (json.JSONDecodeError, UnicodeDecodeError):
|
|
584
|
+
data = None
|
|
585
|
+
if not isinstance(data, dict):
|
|
586
|
+
return RememberJobStatus(
|
|
587
|
+
job_id=job_id,
|
|
588
|
+
status="not_found",
|
|
589
|
+
error=f"remember job not found: {job_id}",
|
|
590
|
+
)
|
|
591
|
+
status = data.get("status")
|
|
592
|
+
if not status:
|
|
593
|
+
return RememberJobStatus(
|
|
594
|
+
job_id=data.get("job_id") or job_id,
|
|
595
|
+
status="not_found",
|
|
596
|
+
error=data.get("error") or f"remember job not found: {job_id}",
|
|
597
|
+
)
|
|
598
|
+
return RememberJobStatus(
|
|
599
|
+
job_id=data.get("job_id", job_id),
|
|
600
|
+
status=status,
|
|
601
|
+
owner=data.get("owner"),
|
|
602
|
+
namespace=data.get("namespace"),
|
|
603
|
+
blob_id=data.get("blob_id"),
|
|
604
|
+
error=data.get("error"),
|
|
605
|
+
)
|
|
606
|
+
|
|
527
607
|
async def wait_for_remember_job(
|
|
528
608
|
self,
|
|
529
609
|
job_id: str,
|
|
@@ -542,6 +622,11 @@ class MemWal:
|
|
|
542
622
|
- First poll is immediate; later polls grow 1.5x toward 5s.
|
|
543
623
|
"""
|
|
544
624
|
|
|
625
|
+
job_id = _require_remember_job_id(job_id)
|
|
626
|
+
path = _remember_status_path(job_id)
|
|
627
|
+
if path is None:
|
|
628
|
+
raise MemWalRememberJobNotFound(job_id)
|
|
629
|
+
|
|
545
630
|
deadline_ms = _now_ms() + timeout_ms
|
|
546
631
|
attempt = 0
|
|
547
632
|
saw_rate_limit = False
|
|
@@ -563,7 +648,7 @@ class MemWal:
|
|
|
563
648
|
try:
|
|
564
649
|
data = await self._signed_request(
|
|
565
650
|
"GET",
|
|
566
|
-
|
|
651
|
+
path,
|
|
567
652
|
{},
|
|
568
653
|
accepted_statuses=(200, 404),
|
|
569
654
|
timeout=poll_timeout,
|
|
@@ -572,12 +657,14 @@ class MemWal:
|
|
|
572
657
|
if "unexpected keyword argument" in str(err) and "timeout" in str(err):
|
|
573
658
|
data = await self._signed_request(
|
|
574
659
|
"GET",
|
|
575
|
-
|
|
660
|
+
path,
|
|
576
661
|
{},
|
|
577
662
|
accepted_statuses=(200, 404),
|
|
578
663
|
)
|
|
579
664
|
else:
|
|
580
665
|
raise
|
|
666
|
+
except (json.JSONDecodeError, UnicodeDecodeError):
|
|
667
|
+
raise MemWalRememberJobNotFound(job_id) from None
|
|
581
668
|
except _HttpStatusError as err:
|
|
582
669
|
if _is_transient_polling_status(err.status):
|
|
583
670
|
last_refusal = err.status
|
|
@@ -592,6 +679,8 @@ class MemWal:
|
|
|
592
679
|
last_refusal = 0
|
|
593
680
|
continue
|
|
594
681
|
|
|
682
|
+
if not isinstance(data, dict):
|
|
683
|
+
raise MemWalRememberJobNotFound(job_id)
|
|
595
684
|
status_str = data.get("status")
|
|
596
685
|
if status_str is None or status_str == "not_found":
|
|
597
686
|
raise MemWalRememberJobNotFound(job_id)
|
|
@@ -1964,6 +2053,10 @@ class MemWalSync:
|
|
|
1964
2053
|
) -> RememberAcceptedResult:
|
|
1965
2054
|
return self._run(self._inner.remember_async(text, namespace, idempotency_key))
|
|
1966
2055
|
|
|
2056
|
+
def get_remember_status(self, job_id: str) -> RememberJobStatus:
|
|
2057
|
+
"""Synchronous version of :meth:`MemWal.get_remember_status`."""
|
|
2058
|
+
return self._run(self._inner.get_remember_status(job_id))
|
|
2059
|
+
|
|
1967
2060
|
def wait_for_remember_job(
|
|
1968
2061
|
self,
|
|
1969
2062
|
job_id: str,
|
|
@@ -140,10 +140,67 @@ class _PendingSaves:
|
|
|
140
140
|
run — this keeps a strong reference until each one is done.
|
|
141
141
|
"""
|
|
142
142
|
|
|
143
|
-
def __init__(
|
|
143
|
+
def __init__(
|
|
144
|
+
self,
|
|
145
|
+
on_save_error: Optional[Callable[[BaseException], None]] = None,
|
|
146
|
+
) -> None:
|
|
144
147
|
self._tasks: "set[asyncio.Task[Any]]" = set()
|
|
145
148
|
self._threads: List[threading.Thread] = []
|
|
149
|
+
self._failures: List[tuple[str, str]] = []
|
|
150
|
+
self._dropped = 0
|
|
151
|
+
self._on_save_error = on_save_error
|
|
146
152
|
self._lock = threading.Lock()
|
|
153
|
+
self._callback_threads: set[int] = set()
|
|
154
|
+
|
|
155
|
+
def note_error(self, err: BaseException) -> None:
|
|
156
|
+
"""Remember a background analyze failure so flush does not report success.
|
|
157
|
+
|
|
158
|
+
The ledger is updated before the callback. A flush from that callback
|
|
159
|
+
returns without draining or clearing the failure. A flush from any
|
|
160
|
+
other thread still waits and raises. The ledger keeps only a type and
|
|
161
|
+
a one-line message, so a client that never flushes does not pin the
|
|
162
|
+
request traceback.
|
|
163
|
+
"""
|
|
164
|
+
type_name = type(err).__name__
|
|
165
|
+
message = str(err).splitlines()[0][:200] if str(err) else ""
|
|
166
|
+
with self._lock:
|
|
167
|
+
if len(self._failures) < 8:
|
|
168
|
+
self._failures.append((type_name, message))
|
|
169
|
+
else:
|
|
170
|
+
self._dropped += 1
|
|
171
|
+
if self._on_save_error is None:
|
|
172
|
+
return
|
|
173
|
+
ident = threading.get_ident()
|
|
174
|
+
with self._lock:
|
|
175
|
+
self._callback_threads.add(ident)
|
|
176
|
+
try:
|
|
177
|
+
self._on_save_error(err)
|
|
178
|
+
except Exception:
|
|
179
|
+
logger.warning("Walrus Memory on_save_error raised", exc_info=True)
|
|
180
|
+
finally:
|
|
181
|
+
with self._lock:
|
|
182
|
+
self._callback_threads.discard(ident)
|
|
183
|
+
|
|
184
|
+
def _flush_called_from_callback(self) -> bool:
|
|
185
|
+
with self._lock:
|
|
186
|
+
return threading.get_ident() in self._callback_threads
|
|
187
|
+
|
|
188
|
+
def _note_result(self, result: Any) -> None:
|
|
189
|
+
if isinstance(result, BaseException):
|
|
190
|
+
self.note_error(result)
|
|
191
|
+
|
|
192
|
+
def _raise_if_failed(self) -> None:
|
|
193
|
+
with self._lock:
|
|
194
|
+
failures, self._failures = self._failures, []
|
|
195
|
+
dropped, self._dropped = self._dropped, 0
|
|
196
|
+
total = len(failures) + dropped
|
|
197
|
+
if total == 0:
|
|
198
|
+
return
|
|
199
|
+
type_name, message = failures[0] if failures else ("Error", "")
|
|
200
|
+
detail = f"{type_name}: {message}" if message else type_name
|
|
201
|
+
raise RuntimeError(
|
|
202
|
+
f"Walrus Memory auto-save failed ({total} call(s)): {detail}"
|
|
203
|
+
)
|
|
147
204
|
|
|
148
205
|
def track_task(self, task: "asyncio.Task[Any]") -> None:
|
|
149
206
|
self._tasks.add(task)
|
|
@@ -169,25 +226,44 @@ class _PendingSaves:
|
|
|
169
226
|
thread.start()
|
|
170
227
|
|
|
171
228
|
async def flush(self) -> None:
|
|
172
|
-
"""Await every pending task, then join every pending thread.
|
|
229
|
+
"""Await every pending task, then join every pending thread.
|
|
230
|
+
|
|
231
|
+
Raises when any background auto-save failed. The chat call itself
|
|
232
|
+
still returned; this is the signal for a caller that waited. A call
|
|
233
|
+
made from ``on_save_error`` returns before draining, joining, or
|
|
234
|
+
clearing the failure being recorded.
|
|
235
|
+
"""
|
|
236
|
+
if self._flush_called_from_callback():
|
|
237
|
+
return
|
|
173
238
|
if self._tasks:
|
|
174
|
-
|
|
239
|
+
tasks = list(self._tasks)
|
|
240
|
+
results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
241
|
+
for result in results:
|
|
242
|
+
self._note_result(result)
|
|
175
243
|
self._join_threads()
|
|
244
|
+
self._raise_if_failed()
|
|
176
245
|
|
|
177
246
|
def flush_sync(self) -> None:
|
|
178
|
-
"""Join every pending thread
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
247
|
+
"""Join every pending thread and raise when a background auto-save failed.
|
|
248
|
+
|
|
249
|
+
A pending Task belongs to the loop that created it. When this thread
|
|
250
|
+
has no running loop, those tasks are awaited here. When it does, they
|
|
251
|
+
are logged and left for :meth:`flush`. A call made from
|
|
252
|
+
``on_save_error`` returns before draining, joining, or clearing the
|
|
253
|
+
failure being recorded.
|
|
254
|
+
"""
|
|
255
|
+
if self._flush_called_from_callback():
|
|
256
|
+
return
|
|
185
257
|
if self._tasks:
|
|
186
|
-
|
|
187
|
-
|
|
258
|
+
tasks = list(self._tasks)
|
|
259
|
+
|
|
260
|
+
async def _drain(pending_tasks: "list[asyncio.Task[Any]]") -> None:
|
|
261
|
+
results = await asyncio.gather(*pending_tasks, return_exceptions=True)
|
|
262
|
+
for result in results:
|
|
263
|
+
self._note_result(result)
|
|
188
264
|
|
|
189
265
|
try:
|
|
190
|
-
asyncio.run(_drain(
|
|
266
|
+
asyncio.run(_drain(tasks))
|
|
191
267
|
except (RuntimeError, ValueError):
|
|
192
268
|
logger.warning(
|
|
193
269
|
"Walrus Memory flush_sync() could not await %d pending "
|
|
@@ -197,10 +273,14 @@ class _PendingSaves:
|
|
|
197
273
|
len(self._tasks),
|
|
198
274
|
)
|
|
199
275
|
self._join_threads()
|
|
276
|
+
self._raise_if_failed()
|
|
200
277
|
|
|
201
278
|
def _join_threads(self) -> None:
|
|
279
|
+
current = threading.current_thread()
|
|
202
280
|
with self._lock:
|
|
203
281
|
threads, self._threads = self._threads, []
|
|
282
|
+
self._threads.extend(thread for thread in threads if thread is current)
|
|
283
|
+
threads = [thread for thread in threads if thread is not current]
|
|
204
284
|
for thread in threads:
|
|
205
285
|
thread.join()
|
|
206
286
|
|
|
@@ -291,6 +371,7 @@ def with_memwal_langchain(
|
|
|
291
371
|
min_relevance: float = 0.3,
|
|
292
372
|
debug: bool = False,
|
|
293
373
|
env: Optional[str] = None,
|
|
374
|
+
on_save_error: Optional[Callable[[BaseException], None]] = None,
|
|
294
375
|
) -> "BaseChatModel":
|
|
295
376
|
"""Wrap a LangChain ``BaseChatModel`` with Walrus Memory management.
|
|
296
377
|
|
|
@@ -313,6 +394,9 @@ def with_memwal_langchain(
|
|
|
313
394
|
debug: Enable debug logging.
|
|
314
395
|
env: Optional relayer preset (``"prod"`` / ``"dev"`` / ``"staging"`` /
|
|
315
396
|
``"local"``). Same precedence as :meth:`MemWal.create`.
|
|
397
|
+
on_save_error: Called with the original exception when a background
|
|
398
|
+
auto-save fails. ``flush`` / ``flush_sync`` from this callback does
|
|
399
|
+
not clear that failure.
|
|
316
400
|
|
|
317
401
|
Returns:
|
|
318
402
|
A wrapped ``BaseChatModel`` that automatically uses Walrus Memory.
|
|
@@ -335,7 +419,7 @@ def with_memwal_langchain(
|
|
|
335
419
|
)
|
|
336
420
|
|
|
337
421
|
log = logger.debug if not debug else logger.warning
|
|
338
|
-
pending = _PendingSaves()
|
|
422
|
+
pending = _PendingSaves(on_save_error)
|
|
339
423
|
|
|
340
424
|
original_agenerate = llm._agenerate
|
|
341
425
|
original_generate = llm._generate
|
|
@@ -408,6 +492,7 @@ def with_memwal_langchain(
|
|
|
408
492
|
await memwal.analyze(user_text, namespace)
|
|
409
493
|
except Exception as e:
|
|
410
494
|
log(f"[Walrus Memory] Auto-save failed: {e}")
|
|
495
|
+
pending.note_error(e)
|
|
411
496
|
|
|
412
497
|
async def patched_agenerate(
|
|
413
498
|
messages: List[List[BaseMessage]], *args: Any, **kwargs: Any
|
|
@@ -470,6 +555,7 @@ def with_memwal_openai(
|
|
|
470
555
|
min_relevance: float = 0.3,
|
|
471
556
|
debug: bool = False,
|
|
472
557
|
env: Optional[str] = None,
|
|
558
|
+
on_save_error: Optional[Callable[[BaseException], None]] = None,
|
|
473
559
|
) -> Any:
|
|
474
560
|
"""Wrap an OpenAI client with Walrus Memory management.
|
|
475
561
|
|
|
@@ -494,6 +580,9 @@ def with_memwal_openai(
|
|
|
494
580
|
debug: Enable debug logging.
|
|
495
581
|
env: Optional relayer preset (``"prod"`` / ``"dev"`` / ``"staging"`` /
|
|
496
582
|
``"local"``). Same precedence as :meth:`MemWal.create`.
|
|
583
|
+
on_save_error: Called with the original exception when a background
|
|
584
|
+
auto-save fails. ``flush`` / ``flush_sync`` from this callback does
|
|
585
|
+
not clear that failure.
|
|
497
586
|
|
|
498
587
|
Returns:
|
|
499
588
|
The same client, with ``chat.completions.create`` wrapped to use Walrus Memory.
|
|
@@ -507,7 +596,7 @@ def with_memwal_openai(
|
|
|
507
596
|
)
|
|
508
597
|
|
|
509
598
|
log = logger.debug if not debug else logger.warning
|
|
510
|
-
pending = _PendingSaves()
|
|
599
|
+
pending = _PendingSaves(on_save_error)
|
|
511
600
|
|
|
512
601
|
is_async = hasattr(client, "_async_client") or type(client).__name__ == "AsyncOpenAI"
|
|
513
602
|
|
|
@@ -572,6 +661,7 @@ def _wrap_async_openai(
|
|
|
572
661
|
await memwal.analyze(user_text, namespace)
|
|
573
662
|
except Exception as e:
|
|
574
663
|
log(f"[Walrus Memory] Auto-save failed: {e}")
|
|
664
|
+
pending.note_error(e)
|
|
575
665
|
|
|
576
666
|
_fire_and_forget(_analyze(), pending)
|
|
577
667
|
|
|
@@ -633,6 +723,7 @@ def _wrap_sync_openai(
|
|
|
633
723
|
_run_memwal(lambda: memwal.analyze(user_text, namespace))
|
|
634
724
|
except Exception as e:
|
|
635
725
|
log(f"[Walrus Memory] Auto-save failed: {e}")
|
|
726
|
+
pending.note_error(e)
|
|
636
727
|
|
|
637
728
|
pending.spawn_thread(_analyze)
|
|
638
729
|
|
|
@@ -10,6 +10,7 @@ from __future__ import annotations
|
|
|
10
10
|
import base64
|
|
11
11
|
import json
|
|
12
12
|
import time
|
|
13
|
+
import uuid
|
|
13
14
|
from datetime import datetime, timedelta, timezone
|
|
14
15
|
from typing import Any, Sequence
|
|
15
16
|
|
|
@@ -24,6 +25,7 @@ from memwal.client import (
|
|
|
24
25
|
MemWalCompatibilityError,
|
|
25
26
|
MemWalError,
|
|
26
27
|
MemWalRateLimited,
|
|
28
|
+
MemWalRememberJobNotFound,
|
|
27
29
|
MemWalRememberJobTimeout,
|
|
28
30
|
MemWalSync,
|
|
29
31
|
_HttpStatusError,
|
|
@@ -36,6 +38,7 @@ from memwal.types import (
|
|
|
36
38
|
RememberBulkOptions,
|
|
37
39
|
RememberBulkStatusItem,
|
|
38
40
|
RememberBulkStatusResult,
|
|
41
|
+
RememberJobStatus,
|
|
39
42
|
RememberManualOptions,
|
|
40
43
|
ScoringWeights,
|
|
41
44
|
)
|
|
@@ -642,6 +645,151 @@ class TestRememberJobTimeoutDetail:
|
|
|
642
645
|
)
|
|
643
646
|
|
|
644
647
|
|
|
648
|
+
# ============================================================
|
|
649
|
+
# one-job remember status
|
|
650
|
+
# ============================================================
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
_NORMAL_JOB_ID = "550e8400-e29b-41d4-a716-446655440000"
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
class TestRememberJobStatus:
|
|
657
|
+
@respx.mock
|
|
658
|
+
async def test_unsafe_job_id_is_not_requested(self, memwal_client: MemWal) -> None:
|
|
659
|
+
route = respx.route().mock(return_value=httpx.Response(500, text="nope"))
|
|
660
|
+
for job_id in ("", "abc/def?x=1", "job 1", "a\tb", "?", "/", ".", "..", "%2e%2e", "a\\b"):
|
|
661
|
+
status = await memwal_client.get_remember_status(job_id)
|
|
662
|
+
assert status == RememberJobStatus(
|
|
663
|
+
job_id=job_id,
|
|
664
|
+
status="not_found",
|
|
665
|
+
error=f"Job {job_id} not found",
|
|
666
|
+
)
|
|
667
|
+
with pytest.raises(MemWalRememberJobNotFound) as exc:
|
|
668
|
+
await memwal_client.wait_for_remember_job(
|
|
669
|
+
job_id, poll_interval_ms=0, timeout_ms=5_000
|
|
670
|
+
)
|
|
671
|
+
assert exc.value.status == 404
|
|
672
|
+
assert exc.value.job_id == job_id
|
|
673
|
+
assert not isinstance(exc.value, json.JSONDecodeError)
|
|
674
|
+
assert route.call_count == 0
|
|
675
|
+
|
|
676
|
+
async def test_non_string_job_id_is_a_type_error(self, memwal_client: MemWal) -> None:
|
|
677
|
+
with pytest.raises(TypeError):
|
|
678
|
+
await memwal_client.get_remember_status(1) # type: ignore[arg-type]
|
|
679
|
+
with pytest.raises(TypeError):
|
|
680
|
+
await memwal_client.wait_for_remember_job(1, poll_interval_ms=0, timeout_ms=5_000) # type: ignore[arg-type]
|
|
681
|
+
|
|
682
|
+
@respx.mock
|
|
683
|
+
async def test_uuid_object_reads_the_string_path(self, memwal_client: MemWal) -> None:
|
|
684
|
+
_mock_version()
|
|
685
|
+
job = uuid.UUID(_NORMAL_JOB_ID)
|
|
686
|
+
route = respx.get(f"{_TEST_SERVER}/api/remember/{job}").mock(
|
|
687
|
+
return_value=httpx.Response(200, json={"job_id": str(job), "status": "running"})
|
|
688
|
+
)
|
|
689
|
+
status = await memwal_client.get_remember_status(job) # type: ignore[arg-type]
|
|
690
|
+
assert status.status == "running"
|
|
691
|
+
assert status.job_id == str(job)
|
|
692
|
+
assert route.call_count == 1
|
|
693
|
+
|
|
694
|
+
@respx.mock
|
|
695
|
+
async def test_non_object_404_body_is_not_found(self, memwal_client: MemWal) -> None:
|
|
696
|
+
mock_seal_session_prereqs()
|
|
697
|
+
route = respx.get(f"{_TEST_SERVER}/api/remember/job-1")
|
|
698
|
+
for body in (b"null", b"[]", b'"Not Found"', b"<html>nope</html>"):
|
|
699
|
+
route.mock(return_value=httpx.Response(404, content=body))
|
|
700
|
+
status = await memwal_client.get_remember_status("job-1")
|
|
701
|
+
assert status.status == "not_found"
|
|
702
|
+
assert status.job_id == "job-1"
|
|
703
|
+
with pytest.raises(MemWalRememberJobNotFound) as exc:
|
|
704
|
+
await memwal_client.wait_for_remember_job(
|
|
705
|
+
"job-1", poll_interval_ms=0, timeout_ms=5_000
|
|
706
|
+
)
|
|
707
|
+
assert exc.value.status == 404
|
|
708
|
+
|
|
709
|
+
@respx.mock
|
|
710
|
+
async def test_empty_404_body_is_not_found(self, memwal_client: MemWal) -> None:
|
|
711
|
+
mock_seal_session_prereqs()
|
|
712
|
+
respx.get(f"{_TEST_SERVER}/api/remember/job-1").mock(
|
|
713
|
+
return_value=httpx.Response(404, content=b"")
|
|
714
|
+
)
|
|
715
|
+
status = await memwal_client.get_remember_status("job-1")
|
|
716
|
+
assert status.status == "not_found"
|
|
717
|
+
assert status.job_id == "job-1"
|
|
718
|
+
with pytest.raises(MemWalRememberJobNotFound) as exc:
|
|
719
|
+
await memwal_client.wait_for_remember_job(
|
|
720
|
+
"job-1", poll_interval_ms=0, timeout_ms=5_000
|
|
721
|
+
)
|
|
722
|
+
assert exc.value.status == 404
|
|
723
|
+
|
|
724
|
+
@respx.mock
|
|
725
|
+
async def test_reads_a_normal_job_id(self, memwal_client: MemWal) -> None:
|
|
726
|
+
mock_seal_session_prereqs()
|
|
727
|
+
route = respx.get(f"{_TEST_SERVER}/api/remember/{_NORMAL_JOB_ID}").mock(
|
|
728
|
+
return_value=httpx.Response(
|
|
729
|
+
200,
|
|
730
|
+
json={
|
|
731
|
+
"job_id": _NORMAL_JOB_ID,
|
|
732
|
+
"status": "done",
|
|
733
|
+
"blob_id": "blob-1",
|
|
734
|
+
"owner": "0xowner",
|
|
735
|
+
"namespace": "profile",
|
|
736
|
+
},
|
|
737
|
+
)
|
|
738
|
+
)
|
|
739
|
+
|
|
740
|
+
status = await memwal_client.get_remember_status(_NORMAL_JOB_ID)
|
|
741
|
+
assert status == RememberJobStatus(
|
|
742
|
+
job_id=_NORMAL_JOB_ID,
|
|
743
|
+
status="done",
|
|
744
|
+
owner="0xowner",
|
|
745
|
+
namespace="profile",
|
|
746
|
+
blob_id="blob-1",
|
|
747
|
+
)
|
|
748
|
+
assert "x-seal-session" not in route.calls[0].request.headers
|
|
749
|
+
|
|
750
|
+
result = await memwal_client.wait_for_remember_job(
|
|
751
|
+
_NORMAL_JOB_ID, poll_interval_ms=0, timeout_ms=5_000
|
|
752
|
+
)
|
|
753
|
+
assert result.blob_id == "blob-1"
|
|
754
|
+
assert result.namespace == "profile"
|
|
755
|
+
assert [str(call.request.url) for call in route.calls] == [
|
|
756
|
+
f"{_TEST_SERVER}/api/remember/{_NORMAL_JOB_ID}",
|
|
757
|
+
f"{_TEST_SERVER}/api/remember/{_NORMAL_JOB_ID}",
|
|
758
|
+
]
|
|
759
|
+
|
|
760
|
+
@respx.mock
|
|
761
|
+
def test_sync_reads_a_normal_job_id(self) -> None:
|
|
762
|
+
_mock_version()
|
|
763
|
+
route = respx.get(f"{_TEST_SERVER}/api/remember/{_NORMAL_JOB_ID}").mock(
|
|
764
|
+
return_value=httpx.Response(
|
|
765
|
+
200,
|
|
766
|
+
json={
|
|
767
|
+
"job_id": _NORMAL_JOB_ID,
|
|
768
|
+
"status": "running",
|
|
769
|
+
"namespace": "profile",
|
|
770
|
+
},
|
|
771
|
+
)
|
|
772
|
+
)
|
|
773
|
+
client = MemWalSync.create(
|
|
774
|
+
key=_TEST_KEY_HEX,
|
|
775
|
+
account_id=_TEST_ACCOUNT_ID,
|
|
776
|
+
server_url=_TEST_SERVER,
|
|
777
|
+
)
|
|
778
|
+
try:
|
|
779
|
+
missing = client.get_remember_status("")
|
|
780
|
+
assert missing.status == "not_found"
|
|
781
|
+
with pytest.raises(MemWalRememberJobNotFound):
|
|
782
|
+
client.wait_for_remember_job("abc/def?x=1", poll_interval_ms=0, timeout_ms=5_000)
|
|
783
|
+
status = client.get_remember_status(_NORMAL_JOB_ID)
|
|
784
|
+
assert status.status == "running"
|
|
785
|
+
assert status.namespace == "profile"
|
|
786
|
+
assert str(route.calls[0].request.url) == (
|
|
787
|
+
f"{_TEST_SERVER}/api/remember/{_NORMAL_JOB_ID}"
|
|
788
|
+
)
|
|
789
|
+
finally:
|
|
790
|
+
client.close()
|
|
791
|
+
|
|
792
|
+
|
|
645
793
|
# ============================================================
|
|
646
794
|
# recall() tests
|
|
647
795
|
# ============================================================
|
|
@@ -1000,3 +1000,157 @@ class TestPendingSaves:
|
|
|
1000
1000
|
|
|
1001
1001
|
time.sleep(0.05)
|
|
1002
1002
|
assert len(pending._threads) == 0
|
|
1003
|
+
|
|
1004
|
+
def test_flush_raises_when_a_background_save_failed(self) -> None:
|
|
1005
|
+
seen: list[BaseException] = []
|
|
1006
|
+
pending = _PendingSaves(seen.append)
|
|
1007
|
+
pending.note_error(RuntimeError("analyze down"))
|
|
1008
|
+
|
|
1009
|
+
async def go() -> None:
|
|
1010
|
+
await pending.flush()
|
|
1011
|
+
|
|
1012
|
+
try:
|
|
1013
|
+
asyncio.run(go())
|
|
1014
|
+
except RuntimeError as err:
|
|
1015
|
+
assert "auto-save failed" in str(err)
|
|
1016
|
+
else:
|
|
1017
|
+
raise AssertionError("flush did not raise")
|
|
1018
|
+
assert len(seen) == 1
|
|
1019
|
+
|
|
1020
|
+
def test_flush_sync_raises_when_a_background_save_failed(self) -> None:
|
|
1021
|
+
pending = _PendingSaves()
|
|
1022
|
+
pending.note_error(RuntimeError("analyze down"))
|
|
1023
|
+
try:
|
|
1024
|
+
pending.flush_sync()
|
|
1025
|
+
except RuntimeError as err:
|
|
1026
|
+
assert "auto-save failed" in str(err)
|
|
1027
|
+
else:
|
|
1028
|
+
raise AssertionError("flush_sync did not raise")
|
|
1029
|
+
|
|
1030
|
+
def test_flush_sees_a_failing_task_without_note_error(self) -> None:
|
|
1031
|
+
pending = _PendingSaves()
|
|
1032
|
+
|
|
1033
|
+
async def fail() -> None:
|
|
1034
|
+
raise RuntimeError("analyze down")
|
|
1035
|
+
|
|
1036
|
+
async def go() -> None:
|
|
1037
|
+
task = asyncio.create_task(fail())
|
|
1038
|
+
pending.track_task(task)
|
|
1039
|
+
await pending.flush()
|
|
1040
|
+
|
|
1041
|
+
try:
|
|
1042
|
+
asyncio.run(go())
|
|
1043
|
+
except RuntimeError as err:
|
|
1044
|
+
assert "analyze down" in str(err)
|
|
1045
|
+
else:
|
|
1046
|
+
raise AssertionError("flush did not raise")
|
|
1047
|
+
|
|
1048
|
+
def test_callback_flush_does_not_swallow_the_failure(self) -> None:
|
|
1049
|
+
pending = _PendingSaves()
|
|
1050
|
+
|
|
1051
|
+
def on_error(_err: BaseException) -> None:
|
|
1052
|
+
pending.flush_sync()
|
|
1053
|
+
|
|
1054
|
+
pending._on_save_error = on_error
|
|
1055
|
+
pending.note_error(RuntimeError("analyze down"))
|
|
1056
|
+
|
|
1057
|
+
async def go() -> None:
|
|
1058
|
+
await pending.flush()
|
|
1059
|
+
|
|
1060
|
+
try:
|
|
1061
|
+
asyncio.run(go())
|
|
1062
|
+
except RuntimeError as err:
|
|
1063
|
+
assert "analyze down" in str(err)
|
|
1064
|
+
else:
|
|
1065
|
+
raise AssertionError("flush did not raise")
|
|
1066
|
+
|
|
1067
|
+
def test_worker_callback_flush_sync_keeps_the_failure(self) -> None:
|
|
1068
|
+
pending = _PendingSaves()
|
|
1069
|
+
callback_error: list[str] = []
|
|
1070
|
+
saw_self = []
|
|
1071
|
+
|
|
1072
|
+
def on_error(_err: BaseException) -> None:
|
|
1073
|
+
try:
|
|
1074
|
+
pending.flush_sync()
|
|
1075
|
+
except Exception as err:
|
|
1076
|
+
callback_error.append(repr(err))
|
|
1077
|
+
with pending._lock:
|
|
1078
|
+
saw_self.append(threading.current_thread() in pending._threads)
|
|
1079
|
+
|
|
1080
|
+
pending._on_save_error = on_error
|
|
1081
|
+
done = threading.Event()
|
|
1082
|
+
|
|
1083
|
+
def fail() -> None:
|
|
1084
|
+
pending.note_error(RuntimeError("analyze down"))
|
|
1085
|
+
done.set()
|
|
1086
|
+
|
|
1087
|
+
pending.spawn_thread(fail)
|
|
1088
|
+
assert done.wait(2)
|
|
1089
|
+
assert callback_error == []
|
|
1090
|
+
assert saw_self == [True]
|
|
1091
|
+
try:
|
|
1092
|
+
pending.flush_sync()
|
|
1093
|
+
except RuntimeError as err:
|
|
1094
|
+
assert "analyze down" in str(err)
|
|
1095
|
+
else:
|
|
1096
|
+
raise AssertionError("flush_sync did not raise")
|
|
1097
|
+
|
|
1098
|
+
def test_two_worker_callbacks_do_not_join_each_other(self) -> None:
|
|
1099
|
+
"""Overlapping on_save_error flushes must not join the other worker.
|
|
1100
|
+
|
|
1101
|
+
Each callback returns before drain, join, and raise. The later caller
|
|
1102
|
+
flush still reports both failures.
|
|
1103
|
+
"""
|
|
1104
|
+
pending = _PendingSaves()
|
|
1105
|
+
entered = threading.Barrier(2)
|
|
1106
|
+
callback_error: list[str] = []
|
|
1107
|
+
done = threading.Barrier(3)
|
|
1108
|
+
|
|
1109
|
+
def on_error(_err: BaseException) -> None:
|
|
1110
|
+
entered.wait(2)
|
|
1111
|
+
try:
|
|
1112
|
+
pending.flush_sync()
|
|
1113
|
+
except Exception as err:
|
|
1114
|
+
callback_error.append(repr(err))
|
|
1115
|
+
|
|
1116
|
+
pending._on_save_error = on_error
|
|
1117
|
+
|
|
1118
|
+
def fail(message: str):
|
|
1119
|
+
def run() -> None:
|
|
1120
|
+
pending.note_error(RuntimeError(message))
|
|
1121
|
+
done.wait(2)
|
|
1122
|
+
return run
|
|
1123
|
+
|
|
1124
|
+
pending.spawn_thread(fail("analyze down"))
|
|
1125
|
+
pending.spawn_thread(fail("analyze also down"))
|
|
1126
|
+
done.wait(2)
|
|
1127
|
+
assert callback_error == []
|
|
1128
|
+
try:
|
|
1129
|
+
pending.flush_sync()
|
|
1130
|
+
except RuntimeError as err:
|
|
1131
|
+
assert "2 call" in str(err)
|
|
1132
|
+
assert "analyze down" in str(err)
|
|
1133
|
+
else:
|
|
1134
|
+
raise AssertionError("flush_sync did not raise")
|
|
1135
|
+
|
|
1136
|
+
def test_flush_from_another_thread_still_raises_during_callback(self) -> None:
|
|
1137
|
+
"""The callback thread bails out. Any other flush still reports the failure."""
|
|
1138
|
+
import time
|
|
1139
|
+
|
|
1140
|
+
pending = _PendingSaves()
|
|
1141
|
+
started = threading.Event()
|
|
1142
|
+
|
|
1143
|
+
def on_error(_err: BaseException) -> None:
|
|
1144
|
+
started.set()
|
|
1145
|
+
time.sleep(0.2)
|
|
1146
|
+
|
|
1147
|
+
pending._on_save_error = on_error
|
|
1148
|
+
pending.spawn_thread(lambda: pending.note_error(RuntimeError("analyze down")))
|
|
1149
|
+
assert started.wait(2)
|
|
1150
|
+
try:
|
|
1151
|
+
pending.flush_sync()
|
|
1152
|
+
except RuntimeError as err:
|
|
1153
|
+
assert "analyze down" in str(err)
|
|
1154
|
+
else:
|
|
1155
|
+
raise AssertionError("flush_sync did not raise")
|
|
1156
|
+
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|