fix(dream): filter non-Dream history commits (#4905)
* fix(dream): filter non-Dream history commits * fix(dream): resolve filtered commit diffs
This commit is contained in:
@@ -117,6 +117,17 @@ class TestLog:
|
||||
git_ready.auto_commit(f"c{i}")
|
||||
assert len(git_ready.log(max_entries=3)) == 3
|
||||
|
||||
def test_message_prefix_skips_unrelated_commits_before_counting_limit(self, git_ready):
|
||||
ws = git_ready._workspace
|
||||
messages = ["dream: older", "backup: first", "dream: latest", "backup: newest"]
|
||||
for i, message in enumerate(messages):
|
||||
(ws / "SOUL.md").write_text(f"v{i}", encoding="utf-8")
|
||||
git_ready.auto_commit(message)
|
||||
|
||||
commits = git_ready.log(max_entries=2, message_prefix="dream:")
|
||||
|
||||
assert [commit.message for commit in commits] == ["dream: latest", "dream: older"]
|
||||
|
||||
def test_commit_info_fields(self, git_ready):
|
||||
c = git_ready.log()[0]
|
||||
assert isinstance(c, CommitInfo)
|
||||
@@ -178,6 +189,25 @@ class TestShowCommitDiff:
|
||||
def test_returns_none_for_unknown(self, git_ready):
|
||||
assert git_ready.show_commit_diff("deadbeef") is None
|
||||
|
||||
def test_message_prefix_finds_commit_beyond_unrelated_history_window(self, git_ready):
|
||||
ws = git_ready._workspace
|
||||
(ws / "SOUL.md").write_text("dream content", encoding="utf-8")
|
||||
dream_sha = git_ready.auto_commit("dream: latest")
|
||||
for i in range(20):
|
||||
(ws / "SOUL.md").write_text(f"backup {i}", encoding="utf-8")
|
||||
git_ready.auto_commit(f"backup: {i}")
|
||||
|
||||
result = git_ready.show_commit_diff(
|
||||
dream_sha,
|
||||
max_entries=1,
|
||||
message_prefix="dream:",
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
commit, diff = result
|
||||
assert commit.sha == dream_sha
|
||||
assert "dream content" in diff
|
||||
|
||||
|
||||
class TestCommitInfoFormat:
|
||||
def test_format_with_diff(self):
|
||||
@@ -221,6 +251,19 @@ class TestRevert:
|
||||
def test_invalid_sha_returns_none(self, git_ready):
|
||||
assert git_ready.revert("deadbeef") is None
|
||||
|
||||
def test_message_prefix_rejects_unrelated_commit_without_changing_files(self, git_ready):
|
||||
ws = git_ready._workspace
|
||||
(ws / "SOUL.md").write_text("dream v1", encoding="utf-8")
|
||||
git_ready.auto_commit("dream: v1")
|
||||
(ws / "SOUL.md").write_text("backup state", encoding="utf-8")
|
||||
backup_sha = git_ready.auto_commit("backup: workspace")
|
||||
(ws / "SOUL.md").write_text("dream v2", encoding="utf-8")
|
||||
latest_sha = git_ready.auto_commit("dream: v2")
|
||||
|
||||
assert git_ready.revert(backup_sha, message_prefix="dream:") is None
|
||||
assert (ws / "SOUL.md").read_text(encoding="utf-8") == "dream v2"
|
||||
assert git_ready.log()[0].sha == latest_sha
|
||||
|
||||
|
||||
class TestMemoryStoreGitProperty:
|
||||
def test_git_property_exposes_gitstore(self, tmp_path):
|
||||
|
||||
@@ -65,17 +65,34 @@ class _FakeGit:
|
||||
self._commits = commits or []
|
||||
self._diff_map = diff_map or {}
|
||||
self._revert_result = revert_result
|
||||
self.revert_calls: list[tuple[str, str | None]] = []
|
||||
|
||||
def is_initialized(self) -> bool:
|
||||
return self._initialized
|
||||
|
||||
def log(self, max_entries: int = 20) -> list[CommitInfo]:
|
||||
return self._commits[:max_entries]
|
||||
def log(
|
||||
self,
|
||||
max_entries: int = 20,
|
||||
message_prefix: str | None = None,
|
||||
) -> list[CommitInfo]:
|
||||
commits = self._commits
|
||||
if message_prefix is not None:
|
||||
commits = [c for c in commits if c.message.startswith(message_prefix)]
|
||||
return commits[:max_entries]
|
||||
|
||||
def show_commit_diff(self, sha: str, max_entries: int = 20):
|
||||
return self._diff_map.get(sha)
|
||||
def show_commit_diff(
|
||||
self,
|
||||
sha: str,
|
||||
max_entries: int = 20,
|
||||
message_prefix: str | None = None,
|
||||
):
|
||||
result = self._diff_map.get(sha)
|
||||
if result and message_prefix is not None and not result[0].message.startswith(message_prefix):
|
||||
return None
|
||||
return result
|
||||
|
||||
def revert(self, sha: str) -> str | None:
|
||||
def revert(self, sha: str, *, message_prefix: str | None = None) -> str | None:
|
||||
self.revert_calls.append((sha, message_prefix))
|
||||
return self._revert_result
|
||||
|
||||
def auto_commit(self, message: str) -> str | None:
|
||||
@@ -260,6 +277,26 @@ async def test_dream_log_latest_is_more_user_friendly() -> None:
|
||||
assert "```diff" in out.content
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dream_log_latest_skips_non_dream_commit() -> None:
|
||||
backup = CommitInfo(
|
||||
sha="bbbb2222", message="backup: workspace snapshot", timestamp="2026-04-04 13:00",
|
||||
)
|
||||
dream = CommitInfo(
|
||||
sha="abcd1234", message="dream: latest", timestamp="2026-04-04 12:00",
|
||||
)
|
||||
diff = "diff --git a/SOUL.md b/SOUL.md\n"
|
||||
git = _FakeGit(
|
||||
commits=[backup, dream],
|
||||
diff_map={dream.sha: (dream, diff), backup.sha: (backup, "unrelated diff")},
|
||||
)
|
||||
|
||||
out = await cmd_dream_log(_make_ctx("/dream-log", git))
|
||||
|
||||
assert "`abcd1234`" in out.content
|
||||
assert "`bbbb2222`" not in out.content
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dream_log_missing_commit_guides_user() -> None:
|
||||
git = _FakeGit(diff_map={})
|
||||
@@ -357,6 +394,7 @@ def test_dream_prompt_command_in_help_and_palette() -> None:
|
||||
async def test_dream_restore_lists_versions_with_next_steps() -> None:
|
||||
commits = [
|
||||
CommitInfo(sha="abcd1234", message="dream: latest", timestamp="2026-04-04 12:00"),
|
||||
CommitInfo(sha="cccc3333", message="backup: workspace", timestamp="2026-04-04 10:00"),
|
||||
CommitInfo(sha="bbbb2222", message="dream: older", timestamp="2026-04-04 08:00"),
|
||||
]
|
||||
git = _FakeGit(commits=commits)
|
||||
@@ -366,6 +404,8 @@ async def test_dream_restore_lists_versions_with_next_steps() -> None:
|
||||
assert "## Dream Restore" in out.content
|
||||
assert "Choose a Dream memory version to restore." in out.content
|
||||
assert "`abcd1234` 2026-04-04 12:00 - dream: latest" in out.content
|
||||
assert "`bbbb2222` 2026-04-04 08:00 - dream: older" in out.content
|
||||
assert "backup: workspace" not in out.content
|
||||
assert "Preview a version with `/dream-log <sha>`" in out.content
|
||||
assert "Restore a version with `/dream-restore <sha>`." in out.content
|
||||
|
||||
@@ -398,3 +438,21 @@ async def test_dream_restore_success_mentions_files_and_followup() -> None:
|
||||
assert "- New safety commit: `eeee9999`" in out.content
|
||||
assert "- Restored files: `SOUL.md`, `memory/MEMORY.md`" in out.content
|
||||
assert "Use `/dream-log eeee9999` to inspect the restore diff." in out.content
|
||||
assert git.revert_calls == [("abcd1234", "dream:")]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dream_restore_rejects_non_dream_commit_clearly() -> None:
|
||||
commit = CommitInfo(
|
||||
sha="cccc3333", message="backup: workspace", timestamp="2026-04-04 10:00",
|
||||
)
|
||||
git = _FakeGit(
|
||||
diff_map={commit.sha: (commit, "unrelated diff")},
|
||||
revert_result="eeee9999",
|
||||
)
|
||||
|
||||
out = await cmd_dream_restore(_make_ctx("/dream-restore cccc3333", git, args="cccc3333"))
|
||||
|
||||
assert "Only Dream memory versions can be restored." in out.content
|
||||
assert "Use `/dream-restore` to list recent versions." in out.content
|
||||
assert git.revert_calls == []
|
||||
|
||||
Reference in New Issue
Block a user