-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: Ideal State Purification (Mypy Strict & Ruff Clean) #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||||||||||||||||
| import json | ||||||||||||||||||||
| import time | ||||||||||||||||||||
| from dataclasses import dataclass | ||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||
|
|
||||||||||||||||||||
| from shared.connection import AsyncConnectionManager, connection_manager | ||||||||||||||||||||
| from shared.constants import DB_NAME | ||||||||||||||||||||
|
|
@@ -26,7 +27,7 @@ class EpisodicMemory: | |||||||||||||||||||
| def __init__(self, cm: AsyncConnectionManager | None = None): | ||||||||||||||||||||
| self._cm = cm or connection_manager | ||||||||||||||||||||
|
|
||||||||||||||||||||
| async def _init_db(self): | ||||||||||||||||||||
| async def _init_db(self) -> None: | ||||||||||||||||||||
| await self._cm.execute_script( | ||||||||||||||||||||
| DB_NAME, | ||||||||||||||||||||
| """ | ||||||||||||||||||||
|
|
@@ -47,10 +48,10 @@ async def save(self, user_id: str, summary: str, emotional_weight: float = 0.5, | |||||||||||||||||||
| conn = await self._cm.get(DB_NAME) | ||||||||||||||||||||
| cursor = await conn.execute( | ||||||||||||||||||||
| "INSERT INTO episodes (user_id, summary, emotional_weight, tags, created_at) VALUES (?, ?, ?, ?, ?)", | ||||||||||||||||||||
| (user_id, summary, emotional_weight, json.dumps(tags or []), time.time()), | ||||||||||||||||||||
| (user_id, summary, emotional_weight, json.dumps(tags or [])), | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When any episode is saved, this four-value tuple is bound to an
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: core/episodic.py
Line: 51
Comment:
**Episode INSERT binding mismatch**
When any episode is saved, this four-value tuple is bound to an `INSERT` with five placeholders, causing SQLite to raise a binding-count error before the episode is persisted.
```suggestion
(user_id, summary, emotional_weight, json.dumps(tags or []), time.time()),
```
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||||||||||||||||||||
| ) | ||||||||||||||||||||
| await conn.commit() | ||||||||||||||||||||
| return cursor.lastrowid | ||||||||||||||||||||
| return int(cursor.lastrowid or 0) | ||||||||||||||||||||
|
Comment on lines
+51
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win Restore the The SQL statement has five placeholders, but the tuple has four values. Every call to Proposed fix- (user_id, summary, emotional_weight, json.dumps(tags or [])),
+ (user_id, summary, emotional_weight, json.dumps(tags or []), time.time()),📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| async def get_episodes(self, user_id: str, limit: int = 20, offset: int = 0) -> list[Episode]: | ||||||||||||||||||||
| conn = await self._cm.get(DB_NAME) | ||||||||||||||||||||
|
|
@@ -70,7 +71,7 @@ async def search_by_tag(self, user_id: str, tag: str, limit: int = 10) -> list[E | |||||||||||||||||||
| rows = await cursor.fetchall() | ||||||||||||||||||||
| return [self._row_to_episode(r) for r in rows] | ||||||||||||||||||||
|
|
||||||||||||||||||||
| async def search(self, user_id: str, query: str, limit: int = 10) -> list: | ||||||||||||||||||||
| async def search(self, user_id: str, query: str, limit: int = 10) -> list[Episode]: | ||||||||||||||||||||
| conn = await self._cm.get(DB_NAME) | ||||||||||||||||||||
| cursor = await conn.execute( | ||||||||||||||||||||
| "SELECT * FROM episodes WHERE user_id=? AND summary LIKE ? ORDER BY created_at DESC LIMIT ?", | ||||||||||||||||||||
|
|
@@ -98,21 +99,21 @@ async def archive_old(self, user_id: str, days: int = 90) -> int: | |||||||||||||||||||
| for row in rows: | ||||||||||||||||||||
| await am.archive( | ||||||||||||||||||||
| user_id=user_id, | ||||||||||||||||||||
| content=row["summary"], | ||||||||||||||||||||
| content=str(row["summary"]), | ||||||||||||||||||||
| memory_type="episode", | ||||||||||||||||||||
| importance=row["emotional_weight"], | ||||||||||||||||||||
| original_id=row["episode_id"], | ||||||||||||||||||||
| importance=float(row["emotional_weight"]), | ||||||||||||||||||||
| original_id=int(row["episode_id"]), | ||||||||||||||||||||
| reason=f"inactive_{days}d", | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| archived_count += 1 | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ids = [row["episode_id"] for row in rows] | ||||||||||||||||||||
| ids = [int(row["episode_id"]) for row in rows] | ||||||||||||||||||||
| if not ids: | ||||||||||||||||||||
| return archived_count | ||||||||||||||||||||
|
|
||||||||||||||||||||
| placeholders = ",".join(["?"] * len(ids)) | ||||||||||||||||||||
| sql = f"DELETE FROM episodes WHERE episode_id IN ({placeholders})" | ||||||||||||||||||||
| await conn.execute(sql, ids) | ||||||||||||||||||||
| await conn.execute(sql, tuple(ids)) | ||||||||||||||||||||
| await conn.commit() | ||||||||||||||||||||
| return archived_count | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -124,14 +125,14 @@ async def count(self, user_id: str) -> int: | |||||||||||||||||||
| (user_id,), | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| row = await cursor.fetchone() | ||||||||||||||||||||
| return row["cnt"] if row else 0 | ||||||||||||||||||||
| return int(row["cnt"]) if row and row[0] is not None else 0 | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def _row_to_episode(self, row) -> Episode: | ||||||||||||||||||||
| def _row_to_episode(self, row: dict[str, Any] | Any) -> Episode: | ||||||||||||||||||||
| return Episode( | ||||||||||||||||||||
| episode_id=row["episode_id"], | ||||||||||||||||||||
| user_id=row["user_id"], | ||||||||||||||||||||
| summary=row["summary"], | ||||||||||||||||||||
| emotional_weight=row["emotional_weight"], | ||||||||||||||||||||
| tags=json.loads(row["tags"]) if row["tags"] else [], | ||||||||||||||||||||
| created_at=row["created_at"], | ||||||||||||||||||||
| episode_id=int(row["episode_id"]), | ||||||||||||||||||||
| user_id=str(row["user_id"]), | ||||||||||||||||||||
| summary=str(row["summary"]), | ||||||||||||||||||||
| emotional_weight=float(row["emotional_weight"]), | ||||||||||||||||||||
| tags=list(json.loads(row["tags"])) if row["tags"] else [], | ||||||||||||||||||||
| created_at=float(row["created_at"]), | ||||||||||||||||||||
| ) | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Treat invalid cache values as cache misses.
At Line 39, a non-list cache value returns
[]and skips both L4 and episodic searches. A malformed or legacy cache entry can therefore hide valid memories. Fall through to the storage search and overwrite the invalid cache value.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents