Repository navigation
feat(matrix): add group chat history context for agent triggers - #57022
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d8737b27f5
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Greptile SummaryThis PR adds opt-in group chat history context for Matrix agent triggers. When Key findings:
Confidence Score: 4/5Safe to merge with the watermark regression fix addressed; the room-queue eviction gap and test cleanup are lower-priority follow-ups. Two P2 issues in the new room-history.ts module: the missing monotone watermark advance is a real (if rare) correctness defect, and the unbounded room-queue map contradicts the documented mitigation. Neither blocks primary functionality, but the watermark issue should ideally be fixed before merging. extensions/matrix/src/matrix/monitor/room-history.ts — watermark regression in consumeHistory and missing eviction for roomQueues.
|
| Filename | Overview |
|---|---|
| extensions/matrix/src/matrix/monitor/room-history.ts | New module implementing per-room message queues and per-(agentId, roomId) watermarks. Core logic is sound; two issues flagged: consumeHistory doesn't monotonically advance the watermark (regression risk under concurrent triggers), and roomQueues has no eviction bound unlike agentWatermarks. |
| extensions/matrix/src/matrix/monitor/handler.ts | History wiring is correct: recordPending is called only after all access-control gates, getPendingHistory/recordTrigger are called before dispatch with the snapshot index captured synchronously, and consumeHistory is gated on successful delivery so failed/retried events re-present the same history. |
| extensions/matrix/src/matrix/monitor/handler.group-history.test.ts | Good coverage of multi-agent watermark independence, queue overflow, DM exclusion, and the async race scenario. One test carries dead infrastructure code and only exercises the success path rather than the stated failure scenario. |
| extensions/matrix/src/matrix/monitor/index.ts | Correctly resolves historyLimit with a fallback chain: account-level channels.matrix.historyLimit → global messages.groupChat.historyLimit → 0; the type assertion for the global path is safe due to optional chaining. |
| extensions/matrix/src/config-schema.ts | Adds historyLimit: z.number().int().min(0).optional() — appropriate Zod schema with non-negative integer constraint. |
Comments Outside Diff (1)
-
extensions/matrix/src/matrix/monitor/handler.group-history.test.ts, line 315-378 (link)Test doesn't exercise its stated scenario
The test is titled "watermark does not advance when reply delivery fails (retry should see same history)", and the harness has comments like
// Simulate delivery failure on first call. However,failFirstDeliveryis declared (let failFirstDelivery = true) but never wired to any mock path — it only appears asvoid failFirstDelivery;to silence the lint warning.The test body instead verifies the success path, which is already covered by earlier tests. Consider either renaming the test to reflect what it actually checks, or properly simulating a delivery failure to lock in the stated guarantee.
Prompt To Fix With AI
This is a comment left during a code review. Path: extensions/matrix/src/matrix/monitor/handler.group-history.test.ts Line: 315-378 Comment: **Test doesn't exercise its stated scenario** The test is titled _"watermark does not advance when reply delivery fails (retry should see same history)"_, and the harness has comments like `// Simulate delivery failure on first call`. However, `failFirstDelivery` is declared (`let failFirstDelivery = true`) but never wired to any mock path — it only appears as `void failFirstDelivery;` to silence the lint warning. The test body instead verifies the **success** path, which is already covered by earlier tests. Consider either renaming the test to reflect what it actually checks, or properly simulating a delivery failure to lock in the stated guarantee. How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/room-history.ts
Line: 113-121
Comment:
**Watermark can regress under concurrent triggers**
`agentWatermarks.set(key, snapshotIdx)` unconditionally overwrites the existing watermark. If two triggers for the same `(agentId, roomId)` are in-flight concurrently and the later trigger's reply completes first — advancing the watermark to, say, `7` — the earlier trigger's `consumeHistory(…, 5)` will then silently regress it back to `5`. On the next trigger, the agent would re-see entries `5–6` that were already consumed.
The fix is a monotone write: read the existing watermark first and only overwrite when `snapshotIdx` is strictly larger (or use `Math.max`). While same-agent concurrent triggers in the same room are uncommon, the scenario is possible when a user sends two `@bot` messages in rapid succession, and the fix is a one-liner.
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/room-history.ts
Line: 19-20
Comment:
**`roomQueues` map grows without bound**
`agentWatermarks` is capped at `MAX_WATERMARK_ENTRIES = 5000` with FIFO eviction, but `roomQueues` has no eviction at all — one `RoomQueue` object (up to 200 `HistoryEntry` values) is retained forever per room. In a bot deployed to many rooms over a long uptime, this accumulates indefinitely.
Consider adding a parallel `MAX_ROOM_QUEUES` constant and a FIFO eviction path analogous to the one already written for `agentWatermarks`. The PR description says "hard caps and LRU eviction are already implemented in `room-history.ts`" but currently only `agentWatermarks` has that protection.
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: extensions/matrix/src/matrix/monitor/handler.group-history.test.ts
Line: 315-378
Comment:
**Test doesn't exercise its stated scenario**
The test is titled _"watermark does not advance when reply delivery fails (retry should see same history)"_, and the harness has comments like `// Simulate delivery failure on first call`. However, `failFirstDelivery` is declared (`let failFirstDelivery = true`) but never wired to any mock path — it only appears as `void failFirstDelivery;` to silence the lint warning.
The test body instead verifies the **success** path, which is already covered by earlier tests. Consider either renaming the test to reflect what it actually checks, or properly simulating a delivery failure to lock in the stated guarantee.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "feat(matrix): add group chat history con..." | Re-trigger Greptile
d8737b2 to
4f6ebd4
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4f6ebd4569
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
4f6ebd4 to
92afdc4
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 92afdc4a9e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
92afdc4 to
4826b35
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4826b3518a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (isRoom && shouldRequireMention && !wasMentioned && !shouldBypassMention) { | ||
| // Record in room history so future triggered replies can see this message as context. | ||
| if (historyLimit > 0 && pendingHistoryText) { | ||
| const pendingEntry: HistoryEntry = { |
There was a problem hiding this comment.
Buffer pending history before async mention resolution
Record buffering in the no-mention path happens only after mention detection, but mention detection can await getMemberDisplayName(...) when formatted_body is present. Because room handlers run concurrently, a fast follow-up trigger can call getPendingHistory before this branch executes, so the earlier unmentioned message is omitted from InboundHistory. This makes group history nondeterministically incomplete for rooms where member display-name lookup is slow/cold (for example first formatted messages in a room).
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 52b48eb333
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1ee780437a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0ef1d4913d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 72b49fc75f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 257392d28f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bea2707eb8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 510feb239d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
c9d4a45 to
d937691
Compare
Implement per-room message queue with per-agent watermarks so each agent in a Matrix room independently tracks which messages it has consumed. - Non-trigger messages accumulate in a shared per-room queue - When an agent is triggered, it receives InboundHistory of pending messages since its last reply (capped at historyLimit) - Watermark only advances to the snapshot taken at dispatch time, so messages arriving during async processing are visible on the next trigger (race safety) - Each agent has an independent watermark, so multiple agents in the same room see independent history windows Configure via channels.matrix.historyLimit (or messages.groupChat.historyLimit). Default is 0 (disabled, preserving existing behavior).
d937691 to
b6f88b7
Compare
|
Merged via squash.
Thanks @chain710! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b6f88b72e8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| historyLimit > 0 | ||
| ? await runRoomIngress(roomId, async () => { | ||
| const prefix = await readIngressPrefix(); | ||
| if (!prefix) { | ||
| return; |
There was a problem hiding this comment.
Exclude reaction events from room-history lock
When historyLimit > 0, this path routes all same-room events through runRoomIngress, including m.reaction events that never read or mutate roomHistoryTracker. Those reactions then await handleInboundMatrixReaction(...), which performs a network fetch (client.getEvent(...) in reaction-events.ts), so bursts of reactions can head-of-line block mention triggers in that room and add avoidable reply latency even though reaction handling is history-independent. Restricting the lock to history-mutating message paths avoids this throughput regression.
Useful? React with 👍 / 👎.
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
…claw#57022) Merged via squash. Prepared head SHA: b6f88b7 Co-authored-by: chain710 <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
Summary
InboundHistoryof messages since its last watermark (capped athistoryLimit). The watermark only advances to the snapshot index taken at dispatch time, so messages arriving during async processing remain visible on the next trigger. Multiple agents in the same room each maintain an independent watermark.historyLimitdefaults to0, disabled). DM flow, allowlist logic, existing routing, and reply mechanics are unmodified.Configure via
channels.matrix.historyLimit(ormessages.groupChat.historyLimit). Default:0(disabled).Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
Root Cause / Regression History (if applicable)
N/A — new feature, not a bug fix.
Regression Test Plan (if applicable)
N/A — new feature.
New test file added:
extensions/matrix/src/matrix/monitor/handler.group-history.test.ts(377 lines), covering:Independent watermarks per agent in the same room
Queue overflow trimming and
baseIndexadvancementAsync race safety (dispatch snapshot vs. messages arriving during processing)
historyLimit = 0disabling behaviorHistory entry count capping
Coverage level that should have caught this:
Target test or file:
extensions/matrix/src/matrix/monitor/handler.group-history.test.tsScenario the test should lock in: multi-agent watermark independence, race safety, queue size cap trimming
Why this is the smallest reliable guardrail: core logic is isolated in
room-history.tsas pure functions; unit tests fully cover all boundary conditionsExisting test that already covers this (if any): none (new module)
If no new test is added, why not: tests are added
User-visible / Behavior Changes
channels.matrix.historyLimit(integer, ≥ 0). Default0preserves existing behavior.InboundHistorycontext.Diagram (if applicable)
Security Impact (required)
NoNoNoNoNoRepro + Verification
Environment
channels.matrix.historyLimit: 5Steps
channels.matrix.historyLimit: 5Expected
Actual
Evidence
New unit tests in
handler.group-history.test.tsall pass, covering core boundary scenarios.Human Verification (required)
historyLimit=0preserves original behavior; multiple agents maintain independent watermarks correctly.Review Conversations
Compatibility / Migration
Yes—historyLimitdefaults to0, fully backward compatibleYes— new optional fieldchannels.matrix.historyLimitNoRisks and Mitigations
room-history.ts; feature is opt-in (disabled by default).