From dde242d170d101273c400178bce45efb2f50660c Mon Sep 17 00:00:00 2001 From: code-server Date: Wed, 18 Feb 2026 23:27:40 +0000 Subject: [PATCH 1/4] feat: prepend time-gap notice to user message when >5 min elapsed Adds elapsed-time awareness to _process_message. When >5 minutes have passed since the last user message in a session, prepends a SYSTEM ANNOUNCEMENT header so the LLM knows time has passed. Co-Authored-By: Claude Sonnet 4.5 --- nanobot/agent/loop.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 6cf65bb..8693dfe 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -3,6 +3,7 @@ import asyncio import json import time +from datetime import datetime from pathlib import Path from typing import Any @@ -290,10 +291,33 @@ class AgentLoop: if isinstance(cron_tool, CronTool): cron_tool.set_context(msg.channel, msg.chat_id) + # Prepend time-gap notice if >5 minutes since last user message + current_message = msg.content + last_user_ts = None + for m in reversed(session.messages): + if m.get("role") == "user": + last_user_ts = m.get("timestamp") + break + if last_user_ts: + try: + last_dt = datetime.fromisoformat(last_user_ts) + now_dt = datetime.now() + elapsed_seconds = (now_dt - last_dt).total_seconds() + if elapsed_seconds > 300: # 5 minutes + if elapsed_seconds < 3600: + gap_str = f"{int(elapsed_seconds // 60)} minutes" + elif elapsed_seconds < 86400: + gap_str = f"{int(elapsed_seconds // 3600)} hours" + else: + gap_str = f"{int(elapsed_seconds // 86400)} days" + current_message = f"[SYSTEM ANNOUNCEMENT: {gap_str} have elapsed since last user message]\n\n{msg.content}" + except (ValueError, TypeError): + pass # Malformed timestamp — skip silently + # Build initial messages (use get_history for LLM-formatted messages) messages = self.context.build_messages( history=session.get_history(), - current_message=msg.content, + current_message=current_message, media=msg.media if msg.media else None, channel=msg.channel, chat_id=msg.chat_id, -- 2.54.0 From da7f58d3ccd98fb7da9cfccbf16d54bd69edfc1d Mon Sep 17 00:00:00 2001 From: code-server Date: Wed, 18 Feb 2026 23:55:01 +0000 Subject: [PATCH 2/4] debug: add time-gap instrumentation logs --- nanobot/agent/loop.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 8693dfe..e0001d4 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -298,11 +298,13 @@ class AgentLoop: if m.get("role") == "user": last_user_ts = m.get("timestamp") break + logger.debug(f"[time-gap] session_msgs={len(session.messages)} last_user_ts={last_user_ts!r}") if last_user_ts: try: last_dt = datetime.fromisoformat(last_user_ts) now_dt = datetime.now() elapsed_seconds = (now_dt - last_dt).total_seconds() + logger.debug(f"[time-gap] elapsed={elapsed_seconds:.0f}s, injecting={'yes' if elapsed_seconds > 300 else 'no'}") if elapsed_seconds > 300: # 5 minutes if elapsed_seconds < 3600: gap_str = f"{int(elapsed_seconds // 60)} minutes" @@ -311,7 +313,8 @@ class AgentLoop: else: gap_str = f"{int(elapsed_seconds // 86400)} days" current_message = f"[SYSTEM ANNOUNCEMENT: {gap_str} have elapsed since last user message]\n\n{msg.content}" - except (ValueError, TypeError): + except (ValueError, TypeError) as e: + logger.debug(f"[time-gap] timestamp parse error: {e}") pass # Malformed timestamp — skip silently # Build initial messages (use get_history for LLM-formatted messages) -- 2.54.0 From 092248411e071636b3189be1bcd5f352cd43f879 Mon Sep 17 00:00:00 2001 From: code-server Date: Thu, 19 Feb 2026 00:04:22 +0000 Subject: [PATCH 3/4] chore: remove debug logs from time-gap injection --- nanobot/agent/loop.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index e0001d4..8693dfe 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -298,13 +298,11 @@ class AgentLoop: if m.get("role") == "user": last_user_ts = m.get("timestamp") break - logger.debug(f"[time-gap] session_msgs={len(session.messages)} last_user_ts={last_user_ts!r}") if last_user_ts: try: last_dt = datetime.fromisoformat(last_user_ts) now_dt = datetime.now() elapsed_seconds = (now_dt - last_dt).total_seconds() - logger.debug(f"[time-gap] elapsed={elapsed_seconds:.0f}s, injecting={'yes' if elapsed_seconds > 300 else 'no'}") if elapsed_seconds > 300: # 5 minutes if elapsed_seconds < 3600: gap_str = f"{int(elapsed_seconds // 60)} minutes" @@ -313,8 +311,7 @@ class AgentLoop: else: gap_str = f"{int(elapsed_seconds // 86400)} days" current_message = f"[SYSTEM ANNOUNCEMENT: {gap_str} have elapsed since last user message]\n\n{msg.content}" - except (ValueError, TypeError) as e: - logger.debug(f"[time-gap] timestamp parse error: {e}") + except (ValueError, TypeError): pass # Malformed timestamp — skip silently # Build initial messages (use get_history for LLM-formatted messages) -- 2.54.0 From 4b4ffb8cbc483e642e086bc667b244e292bdcc36 Mon Sep 17 00:00:00 2001 From: code-server Date: Thu, 19 Feb 2026 00:07:28 +0000 Subject: [PATCH 4/4] fix: restore SYSTEM ANNOUNCEMENT prefix, add instruction to take time into account --- nanobot/agent/loop.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 8693dfe..ed92421 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -310,7 +310,7 @@ class AgentLoop: gap_str = f"{int(elapsed_seconds // 3600)} hours" else: gap_str = f"{int(elapsed_seconds // 86400)} days" - current_message = f"[SYSTEM ANNOUNCEMENT: {gap_str} have elapsed since last user message]\n\n{msg.content}" + current_message = f"[SYSTEM ANNOUNCEMENT: {gap_str} have elapsed since last user message; take this into account when replying to user]\n\n{msg.content}" except (ValueError, TypeError): pass # Malformed timestamp — skip silently -- 2.54.0