Skip to content

Your lightweight open-source personal agent.

fix(session): get_history uses last_consolidated cursor, aligns to user turn

Re-bin2f573e591bce
diff --git a/nanobot/session/manager.py b/nanobot/session/manager.py
index 5f23dc209..d59b7c9a6 100644
--- a/nanobot/session/manager.py
+++ b/nanobot/session/manager.py
@@ -43,9 +43,18 @@ class Session:
self.updated_at = datetime.now()
def get_history(self, max_messages: int = 500) -> list[dict[str, Any]]:
- """Get recent messages in LLM format, preserving tool metadata."""
+ """Return unconsolidated messages for LLM input, aligned to a user turn."""
+ unconsolidated = self.messages[self.last_consolidated:]
+ sliced = unconsolidated[-max_messages:]
+
+ # Drop leading non-user messages to avoid orphaned tool_result blocks
+ for i, m in enumerate(sliced):
+ if m.get("role") == "user":
+ sliced = sliced[i:]
+ break
+
out: list[dict[str, Any]] = []
- for m in self.messages[-max_messages:]:
+ for m in sliced:
entry: dict[str, Any] = {"role": m["role"], "content": m.get("content", "")}
for k in ("tool_calls", "tool_call_id", "name"):
if k in m: