diff --git a/apps/yaak-client/components/RequestVersionDropdown.tsx b/apps/yaak-client/components/RequestVersionDropdown.tsx
index 57e04319..4336d511 100644
--- a/apps/yaak-client/components/RequestVersionDropdown.tsx
+++ b/apps/yaak-client/components/RequestVersionDropdown.tsx
@@ -21,8 +21,8 @@ interface Props {
* existed have no version and stay quiet forever.
*/
export function RequestVersionDropdown({ response }: Props) {
- const comparison = useRequestVersion(response.versionId, response.requestId);
- if (comparison.data == null || !comparison.data.differs) {
+ const { data: comparison } = useRequestVersion(response.versionId, response.requestId);
+ if (comparison == null || !comparison.differs) {
return null;
}
@@ -32,12 +32,12 @@ export function RequestVersionDropdown({ response }: Props) {
{
label: "View Diff",
leftSlot: ,
- onSelect: () => showRequestVersionDiff(comparison.data!),
+ onSelect: () => showRequestVersionDiff(comparison),
},
{
label: "Restore This Version",
leftSlot: ,
- onSelect: () => restoreRequestVersion(comparison.data!.version),
+ onSelect: () => restoreRequestVersion(comparison.version),
},
]}
>
diff --git a/crates/yaak-models/src/queries/model_versions.rs b/crates/yaak-models/src/queries/model_versions.rs
index 2b5cd2dc..95edc6d2 100644
--- a/crates/yaak-models/src/queries/model_versions.rs
+++ b/crates/yaak-models/src/queries/model_versions.rs
@@ -50,11 +50,22 @@ impl<'a> ClientDb<'a> {
workspace_id: request.workspace_id().to_string(),
model_type: request.model_type().to_string(),
model_id: request.id().to_string(),
- content_hash,
+ content_hash: content_hash.clone(),
document,
reason,
..Default::default()
- })?;
+ });
+
+ let version = match version {
+ Ok(version) => version,
+ // Two sends of the same request can both miss the lookup above and
+ // race to insert. The unique index settles it, and the loser wants
+ // exactly what the winner wrote.
+ Err(err) => match self.find_version_by_hash(request.id(), &content_hash) {
+ Some(existing) => return Ok(existing),
+ None => return Err(err),
+ },
+ };
self.prune_model_versions(request.id())?;