fix(memory): keep failed Dream batches retryable
This commit is contained in:
+31
-3
@@ -43,6 +43,26 @@ if TYPE_CHECKING:
|
|||||||
# MemoryStore — pure file I/O layer
|
# MemoryStore — pure file I/O layer
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class DreamRunProgress:
|
||||||
|
"""Track tool failures that make a nominally completed Dream run unsafe to advance."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.had_tool_errors = False
|
||||||
|
|
||||||
|
async def __call__(
|
||||||
|
self,
|
||||||
|
*_args: Any,
|
||||||
|
tool_events: list[dict[str, Any]] | None = None,
|
||||||
|
**_kwargs: Any,
|
||||||
|
) -> None:
|
||||||
|
if any(
|
||||||
|
isinstance(event, dict) and event.get("phase") == "error"
|
||||||
|
for event in tool_events or ()
|
||||||
|
):
|
||||||
|
self.had_tool_errors = True
|
||||||
|
|
||||||
|
|
||||||
class MemoryStore:
|
class MemoryStore:
|
||||||
"""Pure file I/O for memory files: MEMORY.md, history.jsonl, SOUL.md, USER.md."""
|
"""Pure file I/O for memory files: MEMORY.md, history.jsonl, SOUL.md, USER.md."""
|
||||||
|
|
||||||
@@ -635,10 +655,18 @@ class MemoryStore:
|
|||||||
return tools
|
return tools
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def dream_run_completed(resp: object | None) -> bool:
|
def dream_run_completed(
|
||||||
"""Return True only when an ephemeral Dream agent turn completed cleanly."""
|
resp: object | None,
|
||||||
|
*,
|
||||||
|
had_tool_errors: bool = False,
|
||||||
|
) -> bool:
|
||||||
|
"""Return True only when a Dream turn completed without tool failures."""
|
||||||
metadata = getattr(resp, "metadata", None)
|
metadata = getattr(resp, "metadata", None)
|
||||||
return isinstance(metadata, dict) and metadata.get("_stop_reason") == "completed"
|
return (
|
||||||
|
not had_tool_errors
|
||||||
|
and isinstance(metadata, dict)
|
||||||
|
and metadata.get("_stop_reason") == "completed"
|
||||||
|
)
|
||||||
|
|
||||||
# -- message formatting utility ------------------------------------------
|
# -- message formatting utility ------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@@ -1824,12 +1824,13 @@ def _run_gateway(
|
|||||||
|
|
||||||
# Dream is an internal job — run directly, not through the agent loop.
|
# Dream is an internal job — run directly, not through the agent loop.
|
||||||
if job.name == "dream":
|
if job.name == "dream":
|
||||||
from nanobot.agent.memory import MemoryStore
|
from nanobot.agent.memory import DreamRunProgress, MemoryStore
|
||||||
|
|
||||||
dream_session_key = MemoryStore.dream_session_key
|
dream_session_key = MemoryStore.dream_session_key
|
||||||
prune_dream_sessions = MemoryStore.prune_dream_sessions
|
prune_dream_sessions = MemoryStore.prune_dream_sessions
|
||||||
|
|
||||||
store = agent.context.memory
|
store = agent.context.memory
|
||||||
|
progress = DreamRunProgress()
|
||||||
resp = None
|
resp = None
|
||||||
diff_body = ""
|
diff_body = ""
|
||||||
try:
|
try:
|
||||||
@@ -1844,12 +1845,15 @@ def _run_gateway(
|
|||||||
session_key=key,
|
session_key=key,
|
||||||
ephemeral=True,
|
ephemeral=True,
|
||||||
tools=store.build_dream_tools(),
|
tools=store.build_dream_tools(),
|
||||||
on_progress=_silent,
|
on_progress=progress,
|
||||||
)
|
)
|
||||||
# The real file delta grounds the audit record; clean completion
|
# The real file delta grounds the audit record; clean completion
|
||||||
# decides whether this history batch has finished processing.
|
# decides whether this history batch has finished processing.
|
||||||
diff_body = store.dream_content_diff()
|
diff_body = store.dream_content_diff()
|
||||||
completed = MemoryStore.dream_run_completed(resp)
|
completed = MemoryStore.dream_run_completed(
|
||||||
|
resp,
|
||||||
|
had_tool_errors=progress.had_tool_errors,
|
||||||
|
)
|
||||||
if completed:
|
if completed:
|
||||||
store.set_last_dream_cursor(last_cursor)
|
store.set_last_dream_cursor(last_cursor)
|
||||||
if diff_body:
|
if diff_body:
|
||||||
|
|||||||
@@ -404,16 +404,14 @@ async def cmd_dream(ctx: CommandContext) -> OutboundMessage:
|
|||||||
msg = ctx.msg
|
msg = ctx.msg
|
||||||
|
|
||||||
async def _run_dream():
|
async def _run_dream():
|
||||||
async def _silent(*_args, **_kwargs):
|
from nanobot.agent.memory import DreamRunProgress, MemoryStore
|
||||||
pass
|
|
||||||
|
|
||||||
from nanobot.agent.memory import MemoryStore
|
|
||||||
|
|
||||||
dream_session_key = MemoryStore.dream_session_key
|
dream_session_key = MemoryStore.dream_session_key
|
||||||
build_dream_commit_message = MemoryStore.build_dream_commit_message
|
build_dream_commit_message = MemoryStore.build_dream_commit_message
|
||||||
prune_dream_sessions = MemoryStore.prune_dream_sessions
|
prune_dream_sessions = MemoryStore.prune_dream_sessions
|
||||||
|
|
||||||
store = loop.context.memory
|
store = loop.context.memory
|
||||||
|
progress = DreamRunProgress()
|
||||||
content = ""
|
content = ""
|
||||||
resp = None
|
resp = None
|
||||||
diff_body = ""
|
diff_body = ""
|
||||||
@@ -434,13 +432,16 @@ async def cmd_dream(ctx: CommandContext) -> OutboundMessage:
|
|||||||
session_key=key,
|
session_key=key,
|
||||||
ephemeral=True,
|
ephemeral=True,
|
||||||
tools=store.build_dream_tools(),
|
tools=store.build_dream_tools(),
|
||||||
on_progress=_silent,
|
on_progress=progress,
|
||||||
)
|
)
|
||||||
elapsed = time.monotonic() - t0
|
elapsed = time.monotonic() - t0
|
||||||
# The real file delta grounds the audit record; clean completion
|
# The real file delta grounds the audit record; clean completion
|
||||||
# decides whether this history batch has finished processing.
|
# decides whether this history batch has finished processing.
|
||||||
diff_body = store.dream_content_diff()
|
diff_body = store.dream_content_diff()
|
||||||
completed = MemoryStore.dream_run_completed(resp)
|
completed = MemoryStore.dream_run_completed(
|
||||||
|
resp,
|
||||||
|
had_tool_errors=progress.had_tool_errors,
|
||||||
|
)
|
||||||
if completed:
|
if completed:
|
||||||
store.set_last_dream_cursor(last_cursor)
|
store.set_last_dream_cursor(last_cursor)
|
||||||
if diff_body:
|
if diff_body:
|
||||||
|
|||||||
@@ -192,6 +192,7 @@ def _build_runnable_dream(
|
|||||||
initialized: bool,
|
initialized: bool,
|
||||||
content_diff: str,
|
content_diff: str,
|
||||||
stop_reason: str = "completed",
|
stop_reason: str = "completed",
|
||||||
|
tool_error: bool = False,
|
||||||
) -> tuple[CommandContext, _FakeStore]:
|
) -> tuple[CommandContext, _FakeStore]:
|
||||||
"""Build a /dream ctx whose run is driven by a canned stop reason + diff."""
|
"""Build a /dream ctx whose run is driven by a canned stop reason + diff."""
|
||||||
msg = InboundMessage(channel="cli", sender_id="u1", chat_id="direct", content="/dream")
|
msg = InboundMessage(channel="cli", sender_id="u1", chat_id="direct", content="/dream")
|
||||||
@@ -203,6 +204,15 @@ def _build_runnable_dream(
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def process_direct(*args, **kwargs):
|
async def process_direct(*args, **kwargs):
|
||||||
|
if tool_error:
|
||||||
|
await kwargs["on_progress"](
|
||||||
|
"",
|
||||||
|
tool_events=[{
|
||||||
|
"phase": "error",
|
||||||
|
"name": "edit_file",
|
||||||
|
"error": "edit failed",
|
||||||
|
}],
|
||||||
|
)
|
||||||
return OutboundMessage(
|
return OutboundMessage(
|
||||||
channel="cli",
|
channel="cli",
|
||||||
chat_id="direct",
|
chat_id="direct",
|
||||||
@@ -257,6 +267,21 @@ async def test_dream_keeps_cursor_when_incomplete_with_diff(tmp_path) -> None:
|
|||||||
assert "did not complete" in ctx.loop.bus.outbound[0].content
|
assert "did not complete" in ctx.loop.bus.outbound[0].content
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_dream_keeps_cursor_when_completed_after_tool_error(tmp_path) -> None:
|
||||||
|
"""A soft tool failure must not masquerade as a verified no-op."""
|
||||||
|
ctx, store = _build_runnable_dream(
|
||||||
|
tmp_path,
|
||||||
|
initialized=True,
|
||||||
|
content_diff="",
|
||||||
|
tool_error=True,
|
||||||
|
)
|
||||||
|
await cmd_dream(ctx)
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
assert store._last_dream_cursor == 5
|
||||||
|
assert "did not complete" in ctx.loop.bus.outbound[0].content
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dream_noop_batch_unlocks_following_history(tmp_path) -> None:
|
async def test_dream_noop_batch_unlocks_following_history(tmp_path) -> None:
|
||||||
"""A no-op first batch must not starve later history entries."""
|
"""A no-op first batch must not starve later history entries."""
|
||||||
|
|||||||
Reference in New Issue
Block a user