Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion agent/context_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,12 @@ def _file_metadata(path: Path) -> str:
return f"{path.stat().st_size} bytes"
try:
line_count = path.read_text(encoding="utf-8").count("\n") + 1
except Exception:
except Exception as exc:
logger.debug(
"Falling back to size metadata for %s after read failure: %s",
path,
exc,
)
return f"{path.stat().st_size} bytes"
return f"{line_count} lines"

Expand Down
12 changes: 12 additions & 0 deletions agent/conversation_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,18 @@ def run_conversation(
agent._last_content_with_tools = None
agent._last_content_tools_all_housekeeping = False
agent._mute_post_response = False
# Fix: update current_turn_user_idx after compression.
# Compression replaces the messages list with [summary, ...tail],
# making the old index stale. Find the current turn's user
# message in the compressed list so memory/plugin context
# injection at line 954 targets the correct message.
for _new_idx in range(len(messages) - 1, -1, -1):
_msg = messages[_new_idx]
if (isinstance(_msg, dict) and _msg.get("role") == "user"
and _msg.get("content") == user_message):
current_turn_user_idx = _new_idx
agent._persist_user_message_idx = _new_idx
break

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the scan finds no matching user message, this leaves both indexes at their pre-compression values. Clear current_turn_user_idx and _persist_user_message_idx in that case so later injection and persistence cannot target a stale row.

# Re-estimate after compression
_preflight_tokens = estimate_request_tokens_rough(
messages,
Expand Down
15 changes: 13 additions & 2 deletions agent/file_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,26 @@ def is_write_denied(path: str) -> bool:
real = os.path.realpath(base)
if real not in hermes_dirs:
hermes_dirs.append(real)
except Exception:
except Exception as exc:
logger.debug(
"Ignoring write-deny hermess-dir resolution error for %s: %s",
base,
exc,
)
continue

for base_real in hermes_dirs:
for name in control_file_names:
try:
if resolved == os.path.realpath(os.path.join(base_real, name)):
return True
except Exception:
except Exception as exc:
logger.debug(
"Ignoring write-deny control-file resolution error for %s/%s: %s",
base_real,
name,
exc,
)
continue
try:
mcp_real = os.path.realpath(os.path.join(base_real, mcp_tokens_dir_name))
Expand Down