Commit 0bf30ad
authored
fix(memmachein): preserve conversation message order in
## Summary
- Fixes a race condition in \`MemMachineEditor.add_items\` that caused non-deterministic ordering of conversation messages and flaky CI failures on Python 3.13
## Root Cause
\`add_items\` wrapped each conversation message in \`asyncio.to_thread(add_memory)\` and dispatched all tasks concurrently via \`asyncio.gather(*tasks)\`. Thread pool tasks complete in nondeterministic order, so the API spy recorded calls by completion order rather than insertion order — causing \`test_conversation_messages_preserved_in_order\` to flip assertions under CI load.
## Fix
Refactored \`add_items\` to introduce an inner \`add_item\` coroutine per \`MemoryItem\`. Within that coroutine, conversation messages are \`await\`ed sequentially via \`asyncio.to_thread\`, preserving chronological order. Multiple \`MemoryItem\`s are still dispatched concurrently via \`asyncio.gather\` — so there is no performance regression on batch inserts.
\`\`\`python
async def add_item(memory_item: MemoryItem) -> None:
...
for msg in conversation:
await asyncio.to_thread(add_memory) # sequential within one item
if items:
await asyncio.gather(*(add_item(item) for item in items)) # concurrent across items
\`\`\`
The index-based assertions in the test are preserved as-is — they are now correct because the implementation guarantees order.
## Test plan
- [x] \`test_conversation_messages_preserved_in_order\` passes 50 consecutive runs on Python 3.11, 3.12, and 3.13 locally
- [x] Full test suite: 37 passed, 6 skipped (integration) on all three Python versions
Closes #1855
## Summary by CodeRabbit
* **Refactor**
* Updated memory item upload processing to preserve the sequential order of conversation messages while maintaining concurrent processing of multiple memory items.
Authors:
- Federico Kamelhar (https://github.com/fede-kamel)
Approvers:
- Will Killian (https://github.com/willkill07)
URL: #1856add_items (#1856)1 parent 2f82a98 commit 0bf30ad
1 file changed
Lines changed: 13 additions & 11 deletions
Lines changed: 13 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
114 | 118 | | |
115 | | - | |
116 | | - | |
117 | 119 | | |
118 | | - | |
| 120 | + | |
| 121 | + | |
119 | 122 | | |
120 | 123 | | |
121 | 124 | | |
| |||
139 | 142 | | |
140 | 143 | | |
141 | 144 | | |
142 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
143 | 148 | | |
144 | 149 | | |
145 | 150 | | |
| |||
154 | 159 | | |
155 | 160 | | |
156 | 161 | | |
157 | | - | |
158 | 162 | | |
159 | 163 | | |
160 | 164 | | |
| |||
173 | 177 | | |
174 | 178 | | |
175 | 179 | | |
176 | | - | |
177 | | - | |
| 180 | + | |
178 | 181 | | |
179 | 182 | | |
180 | 183 | | |
| |||
195 | 198 | | |
196 | 199 | | |
197 | 200 | | |
198 | | - | |
199 | | - | |
| 201 | + | |
200 | 202 | | |
201 | | - | |
202 | | - | |
| 203 | + | |
| 204 | + | |
203 | 205 | | |
204 | 206 | | |
205 | 207 | | |
| |||
0 commit comments