Building an "Edit Message" or "Regenerate" feature shouldn't require complex database migrations. In the ADK, time travel is built-in.

https://youtu.be/9FQh-Pw2sfE?si=Dumtw7ITtE1zZCp3

Google Advent of Agents:第 9 天

你的 Agent 执行了步骤 1、2、3、4……但如果你突然意识到步骤 2 是个错误,想要“时光倒流”并从那里重新开始怎么办?

现在,ADK 支持时光倒流 (Time Travel) 与检查点 (Checkpointing) 功能了!

不同于直接删除历史记录的破坏性做法,运行器(Runner)会计算“现在”与“当时”之间的差异(即状态与工件的增量数据 State & Artifact Deltas),并在日志中追加一个“倒回事件”。这让你可以将应用状态恢复到特定的时间戳或调用 ID (Invocation ID),同时保留完整的审计轨迹以供查证。

化繁为简的修复方案

你不仅是“回到过去”——而是将整个世界还原到当时完全一致的状态,并保留被倒回的那条路径以备不时之需。

亲自动手尝试:

你可以通过这个示例来亲自体验:adk-python 示例仓库

作为开发者,你可以使用“倒回 (Rewind)”功能来撤销对话中的某个步骤,或恢复到应用之前的状态。如果你配置得当,Agent 甚至可以将此操作作为一项“工具”自主调用。

参考资料连结

Code

import asyncio
from adk.api.agents.in_memory_runner import InMemoryRunner

# 1. Initialize the runner
runner = InMemoryRunner(...)

# 2. Something goes wrong? (e.g., hallucination or error at 'invocation_456')
# Instead of clearing the session, we request a rewind.

# 3. Rewind (Time Travel)
# This asynchronously restores session state and artifacts to the target moment.
asyncio.run(
    runner.rewind_async(
        session_id='session_123',
        before_invocation_id='invocation_456'
    )
)

# 4. Resume the conversation from that exact point
asyncio.run(runner.run(query="Let's try that request again with these constraints..."))