Skip to content

fix(gateway): always inject reply context into skill and chat prompts - #11581

Closed
el-analista wants to merge 1 commit into
NousResearch:mainfrom
el-analista:fix/telegram-reply-context
Closed

fix(gateway): always inject reply context into skill and chat prompts#11581
el-analista wants to merge 1 commit into
NousResearch:mainfrom
el-analista:fix/telegram-reply-context

Conversation

@el-analista

Copy link
Copy Markdown
Contributor

Summary

When a user replied to a previous message in a chat platform (e.g. Telegram), the gateway was suppressing the [Replying to: ...] anchor whenever the replied-to text matched anything already in chat history. In practice, replying to the immediately preceding assistant message always skipped the marker, leaving the LLM to pick the referent from recency bias and causing it to act on the wrong subject.

Concrete failure: replying to a Cronjob Response: Hermes Daily System Audit message with /claude_code improve this skill ... would consistently send the model off to "improve the deep-research skill" (whatever had last been discussed), not the audit skill the user was actually pointing at.

Changes — gateway/run.py

  • Drop the found_in_history gate. Presence in history ≠ being the referent of a reply. The marker is pointer information; always inject it.
  • Widen the snippet from 500 chars to the full reply body (cap 4000 chars, …[truncated] marker appended when over). Telegram's Bot API already provides the complete replied-to text/caption, so no adapter changes are needed; the runner stays platform-agnostic.
  • For skill slash commands, inline the replied-to body directly into user_instruction. The skill payload (full SKILL.md content) otherwise sits between the top-level marker and the user's ask, separating the anchor from the instruction by thousands of tokens.
  • Explicit disambiguation line telling the LLM that "this skill" / "that job" phrases refer to the replied-to message, not to earlier turns.

Both code paths (skill-command dispatcher at ~3229 and general inbound prep at ~3418) now use the same 4000-char cap logic. Mild duplication, intentional — the two sites inject at different points in the final prompt for prompt-structure reasons. Could be factored into a helper in a follow-up.

Test plan

  • Syntax check via python -c "import ast; ast.parse(open('gateway/run.py').read())"
  • Manual end-to-end on Telegram: reply to a Cronjob Response message with /claude_code improve this skill ... — LLM now correctly anchors on the replied-to audit skill.
  • Verified the full reply body lands in the persisted session payload (sessions/session_*.json).
  • Reviewers: confirm non-skill reply flow still works (general chat reply should prepend the [Replying to message N: ...] block verbatim).

The `[Replying to: ...]` marker was suppressed whenever the replied-to
text happened to match something already in chat history. In practice
this meant that replying to the immediately preceding assistant message
(e.g. a cron response) always skipped the marker, leaving the LLM to
guess the referent from recency bias.

- Drop the `found_in_history` gate; always inject the marker.
- Widen the snippet to the full reply body (cap 4000 chars, truncation
  marker appended when over).
- For skill-slash commands, also inline the replied-to body directly
  into `user_instruction` so the anchor sits adjacent to the user's
  ask (the skill payload otherwise pushes the top-level marker far
  from the instruction).
- Include an explicit disambiguation line telling the LLM that
  phrases like "this skill" refer to the replied-to message, not
  earlier conversation turns.

Verified end-to-end on Telegram: replying to a cron response with
`/claude_code improve this skill ...` now correctly anchors on the
replied-to skill instead of drifting to recent chat topics.
@el-analista
el-analista force-pushed the fix/telegram-reply-context branch from 971a14a to bfe519e Compare April 17, 2026 12:29
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery labels Apr 24, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Note: PR #13676 (merged) appears to fix the same root cause — removing the found_in_history guard. This PR may be stale/superseded. Maintainer should check if #13676 already covers this.

@el-analista

Copy link
Copy Markdown
Contributor Author

Closing — the core fix (dropping the found_in_history guard) landed in #13676. The additional changes here (4000-char snippet cap, skill-command context injection) can be revisited as a separate PR if needed.

@el-analista
el-analista deleted the fix/telegram-reply-context branch May 4, 2026 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants