Skip to content

fix: preserve group image context order - #9635

Open
PeiPei233 wants to merge 1 commit into
AstrBotDevs:masterfrom
PeiPei233:fix/group-context-image-order
Open

fix: preserve group image context order#9635
PeiPei233 wants to merge 1 commit into
AstrBotDevs:masterfrom
PeiPei233:fix/group-context-image-order

Conversation

@PeiPei233

@PeiPei233 PeiPei233 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #9634.

Built-in group chat context formatting waits for image captioning before appending a message. Because the event bus processes incoming messages concurrently, a following text message could append first and trigger an LLM request without the preceding image context.

Modifications / 改动点

  • Hold the existing per-session group context lock while formatting and appending each message, preventing later messages in the same group session from overtaking an image caption request.

  • Keep an [Image] placeholder when caption generation fails instead of storing an empty sender/timestamp record.

  • Add deterministic async regression coverage for image/text ordering and caption failure fallback.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

Verification steps:

$ uv run pytest -q tests/unit/test_group_chat_context_wiring.py tests/unit/test_event_bus.py
.......................                                                  [100%]
23 passed, 1 warning in 3.62s
$ uv run ruff check .
All checks passed!

$ uv run ruff format --check .
499 files already formatted

The concurrency regression test starts an image event, pauses its caption request, and then starts a text event for the same group session. It verifies that the text event cannot complete before the image caption and that the text-triggered LLM request receives the image record.


Checklist / 检查清单

Summary by Sourcery

Ensure group chat image messages are formatted and recorded atomically so subsequent text messages cannot overtake them in concurrent processing.

Bug Fixes:

  • Prevent text messages in a group session from triggering LLM requests before preceding image caption context is recorded.
  • Preserve an explicit image placeholder when caption generation fails instead of storing an empty record.

Enhancements:

  • Add deterministic handling of image caption failures to keep group context consistent.

Tests:

  • Add async regression tests verifying image/text ordering under concurrent events and the fallback image placeholder when captioning fails.

Copilot AI lite review requested due to automatic review settings August 12, 2026 04:18
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Aug 12, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a concurrency ordering bug in the built-in GroupChatContext so that image captioning cannot be overtaken by later messages in the same group session, ensuring LLM requests see the correct preceding context.

Changes:

  • Hold the per-session group context lock while formatting and appending records to prevent message reordering during image captioning.
  • Preserve an explicit [Image] placeholder when caption generation fails, instead of storing an empty sender/timestamp-only record.
  • Add async regression tests for image/text ordering and caption-failure placeholder behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
astrbot/builtin_stars/astrbot/group_chat_context.py Makes message formatting+append atomic under the session lock; ensures caption failures still record an [Image] placeholder.
tests/unit/test_group_chat_context_wiring.py Adds deterministic async tests covering ordering under concurrent events and placeholder behavior when captioning fails.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +147 to +149
# Caption and append atomically so later messages cannot overtake images.
async with self._get_lock(umo):
final_message = await self._format_message(event, cfg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Group image caption can be overtaken by a following text message

2 participants