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:
chengyongru
2026-07-13 17:04:12 +08:00
committed by GitHub
parent e864fba522
commit 8c9110fee3
4 changed files with 195 additions and 43 deletions
+35 -18
View File
@@ -560,6 +560,9 @@ def _format_changed_files(diff: str) -> str:
return ", ".join(f"`{path}`" for path in files)
_DREAM_COMMIT_PREFIX = "dream:"
def _format_dream_log_content(commit, diff: str, *, requested_sha: str | None = None) -> str:
files_line = _format_changed_files(diff)
lines = [
@@ -608,7 +611,7 @@ def _format_dream_restore_list(commits: list) -> str:
async def cmd_dream_log(ctx: CommandContext) -> OutboundMessage:
"""Show what the last Dream changed.
Default: diff of the latest commit (HEAD~1 vs HEAD).
Default: diff of the latest Dream commit versus its parent.
With /dream-log <sha>: diff of that specific commit.
"""
store = ctx.loop.consolidator.store
@@ -643,9 +646,16 @@ async def cmd_dream_log(ctx: CommandContext) -> OutboundMessage:
commit, diff = result
content = _format_dream_log_content(commit, diff, requested_sha=sha)
else:
# Default: show the latest commit's diff
commits = git.log(max_entries=1)
result = git.show_commit_diff(commits[0].sha) if commits else None
# Default: show the latest Dream commit's diff
commits = git.log(max_entries=1, message_prefix=_DREAM_COMMIT_PREFIX)
result = (
git.show_commit_diff(
commits[0].sha,
max_entries=1,
message_prefix=_DREAM_COMMIT_PREFIX,
)
if commits else None
)
if result:
commit, diff = result
content = _format_dream_log_content(commit, diff)
@@ -678,29 +688,36 @@ async def cmd_dream_restore(ctx: CommandContext) -> OutboundMessage:
args = ctx.args.strip()
if not args:
# Show recent commits for the user to pick
commits = git.log(max_entries=10)
# Show recent Dream commits for the user to pick
commits = git.log(max_entries=10, message_prefix=_DREAM_COMMIT_PREFIX)
if not commits:
content = "Dream memory has no saved versions to restore yet."
else:
content = _format_dream_restore_list(commits)
else:
sha = args.split()[0]
result = git.show_commit_diff(sha)
changed_files = _format_changed_files(result[1]) if result else "the tracked memory files"
new_sha = git.revert(sha)
if new_sha:
content = (
f"Restored Dream memory to the state before `{sha}`.\n\n"
f"- New safety commit: `{new_sha}`\n"
f"- Restored files: {changed_files}\n\n"
f"Use `/dream-log {new_sha}` to inspect the restore diff."
)
else:
result = git.show_commit_diff(sha, message_prefix=_DREAM_COMMIT_PREFIX)
if not result:
content = (
f"Couldn't restore Dream change `{sha}`.\n\n"
"It may not exist, or it may be the first saved version with no earlier state to restore."
"Only Dream memory versions can be restored. "
"Use `/dream-restore` to list recent versions."
)
else:
changed_files = _format_changed_files(result[1])
new_sha = git.revert(sha, message_prefix=_DREAM_COMMIT_PREFIX)
if new_sha:
content = (
f"Restored Dream memory to the state before `{sha}`.\n\n"
f"- New safety commit: `{new_sha}`\n"
f"- Restored files: {changed_files}\n\n"
f"Use `/dream-log {new_sha}` to inspect the restore diff."
)
else:
content = (
f"Couldn't restore Dream change `{sha}`.\n\n"
"It may be the first saved version with no earlier state to restore."
)
return OutboundMessage(
channel=ctx.msg.channel, chat_id=ctx.msg.chat_id,
content=content, metadata={"render_as": "text"},