-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: knowledge base for long-term memory (#1099) #1115
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
Closed
Closed
Changes from all commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
2c18d7c
feat: knowledge base for long-term memory (issue #1099)
bitloi d07d875
fix: use os.environ in knowledge_base for testability; add unit and A…
bitloi f45ca7c
Merge origin/main into feature/knowledge-base-1099
bitloi ece99c9
fix: resolve chat_service.py conflict with main (keep KB integration)
bitloi 852ab43
Merge upstream/main, resolve chat_service.py (keep KB integration)
bitloi f678309
Merge branch 'main' into feature/knowledge-base-1099
bitloi 3b9c0e4
Merge branch 'main' into feature/knowledge-base-1099
bitloi 3d10e6d
Merge branch 'main' into feature/knowledge-base-1099
bitloi 47f4f10
Merge branch 'main' into feature/knowledge-base-1099
bitloi fbd480b
Merge branch 'main' into feature/knowledge-base-1099
bitloi b7af46a
Merge branch 'main' into feature/knowledge-base-1099
bitloi 7216ce8
fix: resolve merge conflicts with main (router.py, chat_service.py)
bitloi 4e2dcb2
Merge upstream/main into feature/knowledge-base-1099, resolve conflicts
bitloi c363187
Merge branch 'main' into feature/knowledge-base-1099
bitloi 3d2f4b6
Merge branch 'main' into feature/knowledge-base-1099
bitloi d328676
Merge branch 'main' into feature/knowledge-base-1099
bitloi b2fd959
PR feedback: rename to sqlite_toolkit, FTS5/BM25 search, add tool onl…
bitloi 70cd6df
Remove knowledge base from developer agent for now (per review)
bitloi b4bfc4e
Merge branch 'main' into feature/knowledge-base-1099
bitloi e195c47
Merge branch 'main' into feature/knowledge-base-1099
bitloi dd804f2
Merge branch 'main' into feature/knowledge-base-1099
bitloi 606533f
Merge branch 'main' into feature/knowledge-base-1099
bitloi 06d19b1
Merge branch 'main' into feature/knowledge-base-1099
Wendong-Fan b4fba05
Merge branch 'main' into feature/knowledge-base-1099
bitloi 1b0f125
PR feedback: remove KB from chat context, rename tool to store_projec…
bitloi e134793
Merge branch 'main' into feature/knowledge-base-1099
bitloi 52d06b2
Merge branch 'main' into feature/knowledge-base-1099
bitloi 3086710
Merge branch 'main' into feature/knowledge-base-1099
bitloi a275992
Merge upstream/main into feature/knowledge-base-1099
bitloi ba023f5
Merge branch 'feature/knowledge-base-1099' of https://github.com/bitl…
bitloi 8d8b573
Merge branch 'main' into feature/knowledge-base-1099
bitloi 3564835
refactor(knowledge-base): switch from SQLite to markdown file-based m…
bitloi 60367cb
Merge branch 'main' into feature/knowledge-base-1099
nitpicker55555 b2d810a
refactor(memory): index-only prompt, no dedicated tools (reviewer fee…
bitloi 3bd0880
Merge branch 'feature/knowledge-base-1099' of https://github.com/bitl…
bitloi 0971ae6
Address nitpicker55555 review: remove unused memory helpers, wire pro…
bitloi 4cbdaf2
chore(backend): remove ruff from dev dependencies
bitloi 7e7991e
Merge branch 'main' into feature/knowledge-base-1099
bitloi 516caa2
Replace knowledge_base_toolkit with use_project_memory flag
bitloi d000b44
Merge branch 'feature/knowledge-base-1099' of https://github.com/bitl…
bitloi dafac62
Revert linter-only changes in router.py (review feedback)
bitloi f63ba1a
Merge branch 'main' into feature/knowledge-base-1099
bitloi a34927d
refactor(knowledge-base): address PR review – rename to long_term_mem…
bitloi d3c9b98
Merge branch 'main' into feature/knowledge-base-1099
bitloi 2ce99c4
Merge branch 'main' into feature/knowledge-base-1099
bitloi 20ca126
Merge branch 'main' into feature/knowledge-base-1099
bitloi 4d2b0a3
Merge branch 'main' into feature/knowledge-base-1099
bitloi c0cf6af
Merge branch 'main' into feature/knowledge-base-1099
bitloi 9866171
Merge branch 'main' into feature/knowledge-base-1099
bitloi 6b776b2
Merge branch 'main' into feature/knowledge-base-1099
bitloi 1369f7f
Merge branch 'main' into feature/knowledge-base-1099
bitloi c05dc0c
refactor: fix Workspace component naming typos (#1333)
JwCwn 71c0d39
fix: align workspace component export names (#1334)
Wendong-Fan e05d2ce
feat(test): add unit tests for encrypt and time_friendly modules (#1163)
thecaptain789 2395fab
feat(file_utils): robust path handling and safe directory listing (#1…
sharpenteeth a5e4fce
feat: add model type autocomplete with api based suggestions and plat…
statxc fd46400
refactor: standardize workspace casing (#1337)
JwCwn 2b328c8
Merge upstream/main: resolve chat_service.py conflict, keep long_term…
bitloi 37fa80f
Merge branch 'main' into feature/knowledge-base-1099
bitloi f2d74d5
Merge branch 'main' into feature/knowledge-base-1099
bitloi 307249b
Merge branch 'main' into feature/knowledge-base-1099
bitloi 1d62b0c
Merge branch 'main' into feature/knowledge-base-1099
bitloi fa58817
Merge branch 'main' into feature/knowledge-base-1099
bitloi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
bitloi marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| # ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= | ||
|
|
||
| """ | ||
| Markdown-based long-term memory for agents (issue #1099). | ||
|
|
||
| memory.md in the project's .eigent/ directory acts as an index: only a short | ||
| prefix (e.g. first 200 lines) is passed into the system prompt. Topic-specific | ||
| memories live in other .md files under .eigent/; the agent reads and writes | ||
| them on demand via file operations (no dedicated remember/read tools). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from pathlib import Path | ||
| from typing import Final | ||
|
|
||
| logger = logging.getLogger("long_term_memory") | ||
|
|
||
| _MEMORY_FILENAME: Final[str] = "memory.md" | ||
| _EIGENT_DIR: Final[str] = ".eigent" | ||
| _DEFAULT_INDEX_LINES: Final[int] = 200 | ||
| _MAX_INDEX_LINES: Final[int] = 2000 | ||
|
|
||
| _INDEX_HEADER: Final[str] = ( | ||
| "=== Project memory index (.eigent/memory.md) ===\n" | ||
| ) | ||
|
|
||
|
|
||
| class MemoryFileError(Exception): | ||
| """Base exception for memory file operations.""" | ||
|
|
||
|
|
||
| class MemoryReadError(MemoryFileError): | ||
| """Raised when reading the memory file fails.""" | ||
|
|
||
|
|
||
| def _validate_working_directory(working_directory: str) -> Path: | ||
| if not working_directory or not working_directory.strip(): | ||
| raise ValueError("working_directory cannot be empty") | ||
| path = Path(working_directory).expanduser().resolve() | ||
| if not path.exists(): | ||
| raise ValueError(f"working_directory does not exist: {path}") | ||
| if not path.is_dir(): | ||
| raise ValueError(f"working_directory is not a directory: {path}") | ||
| return path | ||
|
|
||
|
|
||
| def get_memory_file_path(working_directory: str) -> Path: | ||
| """Return the path to the project's memory file (.eigent/memory.md).""" | ||
| base_path = _validate_working_directory(working_directory) | ||
| eigent_dir = base_path / _EIGENT_DIR | ||
| eigent_dir.mkdir(parents=True, exist_ok=True) | ||
| return eigent_dir / _MEMORY_FILENAME | ||
|
|
||
|
|
||
| def read_memory(working_directory: str) -> str | None: | ||
| """Read the full content of the memory file, or None if missing/invalid.""" | ||
| try: | ||
| memory_path = get_memory_file_path(working_directory) | ||
| except ValueError as e: | ||
| logger.warning("Invalid working directory: %s", e) | ||
| return None | ||
|
|
||
| if not memory_path.exists(): | ||
| return None | ||
|
|
||
| try: | ||
| content = memory_path.read_text(encoding="utf-8") | ||
| return content if content.strip() else None | ||
| except OSError as e: | ||
| logger.error("Failed to read memory file %s: %s", memory_path, e) | ||
| return None | ||
|
|
||
|
|
||
| MEMORY_ARCHITECTURE_PROMPT: Final[str] = """ | ||
| Project long-term memory lives under .eigent/ in the project directory. | ||
| - .eigent/memory.md is the index: it lists or summarizes memory topics (e.g. user_preferences.md, decisions.md). | ||
| - You can read any .eigent/*.md file when you need topic-specific information. | ||
| - To remember something: create or edit markdown files under .eigent/ (e.g. append to an existing topic file or create one). Use normal file operations (read/write/append) or shell commands; no dedicated memory tool is required. | ||
| """ | ||
|
|
||
|
|
||
| def _continuation_note(remaining_lines: int) -> str: | ||
| """Note shown when index is truncated; includes remaining line count.""" | ||
| return f"\n\n...({remaining_lines} more lines in .eigent/; read files as needed)\n" | ||
|
|
||
|
|
||
| def get_index_for_prompt( | ||
| working_directory: str, | ||
| max_lines: int = _DEFAULT_INDEX_LINES, | ||
| ) -> str | None: | ||
| """ | ||
| Return the first max_lines of memory.md formatted for system-prompt injection. | ||
| Callers should use this instead of dumping the full file; topic-specific | ||
| content is read by the agent via file operations. | ||
| """ | ||
|
bitloi marked this conversation as resolved.
|
||
| if not working_directory or not working_directory.strip(): | ||
| return None | ||
| if max_lines <= 0: | ||
| return None | ||
| effective_max = min(max_lines, _MAX_INDEX_LINES) | ||
|
|
||
| content = read_memory(working_directory) | ||
| if not content: | ||
| return None | ||
|
|
||
| lines = content.splitlines() | ||
| if len(lines) > effective_max: | ||
| remaining = len(lines) - effective_max | ||
| index_content = "\n".join(lines[:effective_max]) + _continuation_note( | ||
| remaining | ||
| ) | ||
| else: | ||
| index_content = content | ||
|
|
||
| return _INDEX_HEADER + index_content + "\n" | ||
|
bitloi marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= | ||
|
|
||
| """Unit tests for the markdown-based long-term memory (issue #1099).""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from app.utils import long_term_memory as ltm | ||
|
|
||
|
|
||
| def _write_memory(working_dir: str, content: str) -> None: | ||
| """Write content to .eigent/memory.md (tests only; production uses file ops).""" | ||
| path = ltm.get_memory_file_path(working_dir) | ||
| path.write_text(content, encoding="utf-8") | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestLongTermMemory: | ||
| """Test long_term_memory read and get_index_for_prompt.""" | ||
|
|
||
| def test_read_nonexistent_memory(self, tmp_path: Path) -> None: | ||
| """Reading memory from a directory without memory.md returns None.""" | ||
| content = ltm.read_memory(str(tmp_path)) | ||
| assert content is None | ||
|
|
||
| def test_read_memory(self, tmp_path: Path) -> None: | ||
| """Read returns content written to memory.md.""" | ||
| working_dir = str(tmp_path) | ||
| _write_memory( | ||
| working_dir, | ||
| "# Project Memory\n\nLong-term memory.\n\nUser prefers dark mode.", | ||
| ) | ||
| content = ltm.read_memory(working_dir) | ||
| assert content is not None | ||
| assert "Project Memory" in content | ||
| assert "dark mode" in content | ||
|
|
||
| def test_read_multiple_sections(self, tmp_path: Path) -> None: | ||
| """Read returns full file content.""" | ||
| working_dir = str(tmp_path) | ||
| _write_memory( | ||
| working_dir, | ||
| "# Project Memory\n\nFirst entry.\n\n## Section 2\n\nSecond entry.\n\nThird entry.", | ||
| ) | ||
| content = ltm.read_memory(working_dir) | ||
| assert content is not None | ||
| assert "First entry" in content | ||
| assert "Second entry" in content | ||
| assert "Third entry" in content | ||
|
|
||
| def test_get_index_for_prompt(self, tmp_path: Path) -> None: | ||
| """get_index_for_prompt returns first portion of memory.md for system prompt.""" | ||
| working_dir = str(tmp_path) | ||
| _write_memory( | ||
| working_dir, "# Project Memory\n\nUser prefers Python 3.10." | ||
| ) | ||
|
|
||
| ctx = ltm.get_index_for_prompt(working_dir) | ||
| assert ctx is not None | ||
| assert "memory index" in ctx.lower() or "memory.md" in ctx | ||
| assert "Python 3.10" in ctx | ||
|
|
||
| def test_get_index_for_prompt_empty(self, tmp_path: Path) -> None: | ||
| """get_index_for_prompt returns None for empty/nonexistent memory.""" | ||
| working_dir = str(tmp_path) | ||
| ctx = ltm.get_index_for_prompt(working_dir) | ||
| assert ctx is None | ||
|
|
||
| def test_get_index_for_prompt_max_lines(self, tmp_path: Path) -> None: | ||
| """get_index_for_prompt limits to first max_lines and adds note with remaining count.""" | ||
| working_dir = str(tmp_path) | ||
| lines = ["# Project Memory", ""] + [ | ||
| f"Line entry {i}." for i in range(300) | ||
| ] | ||
| _write_memory(working_dir, "\n".join(lines)) | ||
|
|
||
| ctx = ltm.get_index_for_prompt(working_dir, max_lines=50) | ||
| assert ctx is not None | ||
| assert "more lines" in ctx or ".eigent" in ctx | ||
| assert len(ctx.splitlines()) <= 55 | ||
|
|
||
| def test_memory_file_path(self, tmp_path: Path) -> None: | ||
| """Memory file path is .eigent/memory.md under working dir.""" | ||
| working_dir = str(tmp_path) | ||
| memory_path = ltm.get_memory_file_path(working_dir) | ||
| assert ".eigent" in str(memory_path) | ||
| assert str(memory_path).endswith("memory.md") | ||
|
|
||
| def test_invalid_working_directory(self) -> None: | ||
| """Invalid working directory returns None for read.""" | ||
| content = ltm.read_memory("/nonexistent/path/that/does/not/exist") | ||
| assert content is None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.