132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300 | async def check_thread_store[TContext](
store: ThreadStore[TContext],
context: TContext,
*,
other_context: TContext,
peer: ThreadStore[TContext] | None = None,
) -> None:
"""Check core thread storage, including form claims, retries, and hidden items.
Run against a disposable database with contexts for two distinct users.
Supply an independent store instance sharing that database to check
concurrent operations across connections. The check writes and deletes
a synthetic thread. A failed condition raises an informative AssertionError.
"""
peer = peer if peer is not None else store
async with _disposable_thread(store, context) as thread:
created_at = thread.created_at
try:
await peer.load_thread(thread.id, other_context)
except NotFoundError:
pass
else:
raise AssertionError("Another user's context can load this thread")
async with asyncio.TaskGroup() as patches:
patches.create_task(
store.patch_thread(thread.id, {"title": "Renamed"}, context)
)
patches.create_task(
peer.patch_thread(
thread.id,
{"metadata_set": {"saved": True}, "metadata_delete": ("remove",)},
context,
)
)
patched = await store.load_thread(thread.id, context)
_require(
patched.title == "Renamed"
and patched.metadata == {"kept": True, "saved": True},
"Concurrent field patches must preserve unrelated state",
)
older_turn, newer_turn = str(uuid4()), str(uuid4())
await store.begin_turn(thread.id, older_turn, context)
await store.start_turn(thread.id, older_turn, context)
await peer.begin_turn(thread.id, newer_turn, context)
try:
await store.patch_thread(
thread.id, {"title": "Stale title"}, context, turn_id=older_turn
)
except ConflictError:
pass
else:
raise AssertionError("A superseded turn can still patch the thread")
_require(
await peer.load_turn_state(thread.id, older_turn, context)
== TurnState.SUPERSEDED,
"The older turn must retain its superseded state",
)
_require(
await store.finish_turn(
thread.id, newer_turn, TurnOutcome.CANCELLED, context
)
== TurnState.ABANDONED,
"Closing an accepted turn must record that it never started",
)
item_ids = set()
for text in ("First message", "Second message", "Third message"):
message = UserMessageItem(
id=store.generate_item_id("message", thread, context),
thread_id=thread.id,
created_at=created_at,
content=[UserMessageTextContent(text=text)],
inference_options=InferenceOptions(),
)
await store.add_thread_item(thread.id, message, context)
item_ids.add(message.id)
_require(len(item_ids) == 3, "Item ID generation returned a duplicate")
ascending = await peer.load_thread_items(thread.id, None, 3, "asc", context)
ascending_ids = [item.id for item in ascending.data]
_require(
len(ascending_ids) == len(item_ids) and set(ascending_ids) == item_ids,
"Pagination must return each item with a tied timestamp once",
)
form = StructuredInputItem(
id=store.generate_item_id("task", thread, context),
thread_id=thread.id,
created_at=created_at + timedelta(seconds=2),
inputs=[],
)
await store.add_thread_item(thread.id, form, context)
active_turn = str(uuid4())
await store.begin_turn(thread.id, active_turn, context)
answered = form.model_copy(update={"status": "answered"})
for active_state in (TurnState.ACCEPTED, TurnState.RUNNING):
if active_state == TurnState.RUNNING:
await store.start_turn(thread.id, active_turn, context)
try:
await peer.claim_pending_item(
thread.id, form, answered, context, next_turn_id=str(uuid4())
)
except ConflictError:
pass
else:
raise AssertionError(f"A form claim replaced the {active_state} turn")
_require(
await peer.load_turn_state(thread.id, active_turn, context)
== active_state
and await peer.load_item(thread.id, form.id, context) == form,
"A rejected form claim must preserve the active turn and pending form",
)
await store.finish_turn(thread.id, active_turn, TurnOutcome.COMPLETED, context)
_, result = await _race_claims(
store,
peer,
context,
form,
answered,
form.model_copy(update={"status": "skipped"}),
)
_require(result is None, "A form claim must return no private invocation")
app_note = HiddenContextItem(
id=str(uuid4()),
thread_id=thread.id,
created_at=created_at + timedelta(seconds=1),
content="App context",
)
sdk_note = SDKHiddenContextItem(
id=store.generate_item_id("sdk_hidden_context", thread, context),
thread_id=thread.id,
created_at=created_at + timedelta(seconds=3),
content="SDK context",
)
last_message = UserMessageItem(
id=store.generate_item_id("message", thread, context),
thread_id=thread.id,
created_at=created_at + timedelta(seconds=4),
content=[UserMessageTextContent(text="Last message")],
inference_options=InferenceOptions(),
)
for item in (app_note, sdk_note, last_message):
await store.add_thread_item(thread.id, item, context)
stored_ids = [
*ascending_ids,
app_note.id,
form.id,
sdk_note.id,
last_message.id,
]
await _check_item_pagination(peer, thread.id, context, stored_ids)
await _check_item_pagination(
peer,
thread.id,
context,
stored_ids,
exclude_ids=frozenset({app_note.id, sdk_note.id}),
)
await _check_retry(
store,
peer,
context,
thread.id,
ascending_ids[0],
stored_ids[1:],
)
|