From 819ddca9701d189b44c538ac9f990de9eb3edae5 Mon Sep 17 00:00:00 2001 From: bliatun-code <270035675+bliatun-code@users.noreply.github.com> Date: Fri, 27 Nov 2026 03:45:24 +0100 Subject: [PATCH] Pin paired spool registration to its reviewed Host generation --- src-tauri/src/library_sync_models.rs | 3 ++- src-tauri/src/library_sync_resilience_tests.rs | 3 ++- .../src/library_sync_spool_write_commands.rs | 18 +++++++++++++++--- ui/src/lib/spool_writes.test.ts | 4 ++-- ui/src/lib/spool_writes.ts | 12 +++++++----- ui/src/lib/tauri_inventory_client.test.ts | 7 ++++--- ui/src/lib/tauri_inventory_client.ts | 11 +++++++---- ui/src/lib/use_inventory_create_actions.ts | 10 +++++----- ui/src/pages/use_printer_slot_interactions.ts | 2 ++ 9 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src-tauri/src/library_sync_models.rs b/src-tauri/src/library_sync_models.rs index 4ab0edad..9d0112e5 100644 --- a/src-tauri/src/library_sync_models.rs +++ b/src-tauri/src/library_sync_models.rs @@ -257,7 +257,8 @@ pub(crate) struct LibrarySyncDomainRevisionsResponse { #[derive(Deserialize)] pub(crate) struct LibrarySyncCreateSpoolInput { pub(crate) base_url: String, - pub(crate) expected_library_id: Option, + pub(crate) expected_library_id: String, + pub(crate) expected_target_generation: u64, pub(crate) master_id: Option, pub(crate) material: Option, pub(crate) filament_name: Option, diff --git a/src-tauri/src/library_sync_resilience_tests.rs b/src-tauri/src/library_sync_resilience_tests.rs index fefa07af..c7f07d86 100644 --- a/src-tauri/src/library_sync_resilience_tests.rs +++ b/src-tauri/src/library_sync_resilience_tests.rs @@ -943,7 +943,8 @@ async fn real_tcp_client_completes_the_five_fixed_workflows_on_the_host() { &state, LibrarySyncCreateSpoolInput { base_url, - expected_library_id: Some(HOST_LIBRARY_ID.to_string()), + expected_library_id: HOST_LIBRARY_ID.to_string(), + expected_target_generation: target_generation, master_id: None, material: Some("PLA".to_string()), filament_name: Some("Fixed Workflow".to_string()), diff --git a/src-tauri/src/library_sync_spool_write_commands.rs b/src-tauri/src/library_sync_spool_write_commands.rs index c52c3637..630d594d 100644 --- a/src-tauri/src/library_sync_spool_write_commands.rs +++ b/src-tauri/src/library_sync_spool_write_commands.rs @@ -429,8 +429,17 @@ pub(crate) fn create_library_sync_host_spool_blocking( state: &AppState, input: LibrarySyncCreateSpoolInput, ) -> Result { - let host_input = library_sync_host_input(&input.base_url, input.expected_library_id.as_deref()); - let (_, _, target) = prepare_library_sync_host_write(state, &host_input)?; + if input.expected_library_id.trim().is_empty() { + return Err(crate::app_error::coded_command_error( + "common.invalid_request", + )); + } + let host_input = library_sync_host_input(&input.base_url, Some(&input.expected_library_id)); + let (_, _, target) = prepare_library_sync_host_write_for_generation( + state, + &host_input, + Some(input.expected_target_generation), + )?; let path = library_sync_create_spool_path( input.ownership_type.as_deref(), @@ -460,8 +469,11 @@ pub(crate) fn create_library_sync_host_spool_blocking( return Err(response.message); } + if response.spool_id.trim().is_empty() { + return Err(crate::app_error::coded_command_error("common.internal")); + } refresh_library_sync_spool_cache(state, &target); - save_library_sync_success(state, &target, &response.message, None)?; + let _ = save_library_sync_success(state, &target, &response.message, None); Ok(response.spool_id) } diff --git a/ui/src/lib/spool_writes.test.ts b/ui/src/lib/spool_writes.test.ts index bb913ab4..7d74bd4b 100644 --- a/ui/src/lib/spool_writes.test.ts +++ b/ui/src/lib/spool_writes.test.ts @@ -62,7 +62,7 @@ test("createInventorySpoolFromMaster routes client writes to the host and return const createdId = await createInventorySpoolFromMaster( masterSpoolInput(), - { clientReadOnly: true, clientHostBaseUrl: "http://host", clientLibraryId: "library-1" }, + { clientReadOnly: true, clientHostBaseUrl: "http://host", clientLibraryId: "library-1", clientTargetGeneration: 3 }, { createHostSpool: async (baseUrl, libraryId, input) => { calls.push({ baseUrl, libraryId, input }); @@ -100,7 +100,7 @@ test("createManualInventorySpool routes manual client writes to the host", async const createdId = await createManualInventorySpool( manualSpoolInput(), - { clientReadOnly: true, clientHostBaseUrl: "http://host", clientLibraryId: "library-1" }, + { clientReadOnly: true, clientHostBaseUrl: "http://host", clientLibraryId: "library-1", clientTargetGeneration: 3 }, { createHostSpool: async (baseUrl, _libraryId, input) => { calls.push({ baseUrl, input }); diff --git a/ui/src/lib/spool_writes.ts b/ui/src/lib/spool_writes.ts index 9b46a36a..69557ac6 100644 --- a/ui/src/lib/spool_writes.ts +++ b/ui/src/lib/spool_writes.ts @@ -25,7 +25,7 @@ import { type UpdateSpoolRfidTagInput, } from "./tauri_client"; import { updateActiveLibrarySpoolDetails, type ReviewedLibraryAuthority } from "./tauri_active_library_gateway_client"; -import { requireClientHostWriteTarget } from "./host_write_target"; +import { requireClientHostWriteTarget, resolveClientHostCacheTarget } from "./host_write_target"; export type SpoolWriteTarget = { reviewedAuthority?: ReviewedLibraryAuthority; @@ -68,8 +68,9 @@ export async function createInventorySpoolFromMaster( const createLocalSpool = dependencies.createLocalSpool ?? createSpool; if (target.clientReadOnly) { - const hostTarget = requireClientHostWriteTarget(target, missingSpoolHostTargetMessage); - return createHostSpool(hostTarget.baseUrl, hostTarget.libraryId, input); + const hostTarget = resolveClientHostCacheTarget(target); + if (!hostTarget) throw new Error(missingSpoolHostTargetMessage); + return createHostSpool(hostTarget.baseUrl, hostTarget.libraryId, input, hostTarget.targetGeneration); } await createLocalSpool(input); @@ -86,8 +87,9 @@ export async function createManualInventorySpool( dependencies.createLocalManualSpool ?? createManualSpool; if (target.clientReadOnly) { - const hostTarget = requireClientHostWriteTarget(target, missingSpoolHostTargetMessage); - return createHostSpool(hostTarget.baseUrl, hostTarget.libraryId, input); + const hostTarget = resolveClientHostCacheTarget(target); + if (!hostTarget) throw new Error(missingSpoolHostTargetMessage); + return createHostSpool(hostTarget.baseUrl, hostTarget.libraryId, input, hostTarget.targetGeneration); } await createLocalManualSpool(input); diff --git a/ui/src/lib/tauri_inventory_client.test.ts b/ui/src/lib/tauri_inventory_client.test.ts index 9fb77c0d..d978d0a7 100644 --- a/ui/src/lib/tauri_inventory_client.test.ts +++ b/ui/src/lib/tauri_inventory_client.test.ts @@ -92,11 +92,12 @@ test("buildLibrarySyncHostSpoolCreatePayload forwards catalog ownership to host }; assert.deepEqual( - buildLibrarySyncHostSpoolCreatePayload("http://host", "library-1", input), + buildLibrarySyncHostSpoolCreatePayload("http://host", "library-1", input, 3), { input: { base_url: "http://host", expected_library_id: "library-1", + expected_target_generation: 3, master_id: "master-1", material: null, filament_name: null, @@ -135,12 +136,12 @@ test("buildLibrarySyncHostSpoolCreatePayload forwards manual ownership to host c }; assert.equal( - buildLibrarySyncHostSpoolCreatePayload("http://host", null, input).input + buildLibrarySyncHostSpoolCreatePayload("http://host", "library-1", input, 3).input .ownership_type, "OWNED", ); assert.equal( - buildLibrarySyncHostSpoolCreatePayload("http://host", null, input).input.location, + buildLibrarySyncHostSpoolCreatePayload("http://host", "library-1", input, 3).input.location, "Shelf A", ); }); diff --git a/ui/src/lib/tauri_inventory_client.ts b/ui/src/lib/tauri_inventory_client.ts index 03bbf149..aef25dd2 100644 --- a/ui/src/lib/tauri_inventory_client.ts +++ b/ui/src/lib/tauri_inventory_client.ts @@ -267,8 +267,9 @@ export async function createSpool(input: CreateSpoolInput) { export function buildLibrarySyncHostSpoolCreatePayload( baseUrl: string, - expectedLibraryId: string | null | undefined, + expectedLibraryId: string, input: CreateSpoolInput | CreateManualSpoolInput, + expectedGeneration: number, ) { const normalizedLocation = ("location" in input ? input.location : undefined) ?? @@ -277,7 +278,8 @@ export function buildLibrarySyncHostSpoolCreatePayload( return { input: { base_url: baseUrl, - expected_library_id: expectedLibraryId ?? null, + expected_library_id: expectedLibraryId, + expected_target_generation: expectedGeneration, master_id: "master_id" in input ? input.master_id ?? null : null, material: "material" in input ? input.material : null, filament_name: "filament_name" in input ? input.filament_name : null, @@ -296,12 +298,13 @@ export function buildLibrarySyncHostSpoolCreatePayload( export async function createLibrarySyncHostSpool( baseUrl: string, - expectedLibraryId: string | null | undefined, + expectedLibraryId: string, input: CreateSpoolInput | CreateManualSpoolInput, + expectedGeneration: number, ) { return invoke( "create_library_sync_host_spool", - buildLibrarySyncHostSpoolCreatePayload(baseUrl, expectedLibraryId, input), + buildLibrarySyncHostSpoolCreatePayload(baseUrl, expectedLibraryId, input, expectedGeneration), ); } diff --git a/ui/src/lib/use_inventory_create_actions.ts b/ui/src/lib/use_inventory_create_actions.ts index 91dcd32e..567ae4aa 100644 --- a/ui/src/lib/use_inventory_create_actions.ts +++ b/ui/src/lib/use_inventory_create_actions.ts @@ -153,7 +153,7 @@ export function useInventoryCreateActions({ tauriAvailable, t, }: InventoryCreateActionsInput) { - const hostWriteTarget = { clientReadOnly, clientHostBaseUrl, clientLibraryId }; + const hostWriteTarget = { clientReadOnly, clientHostBaseUrl, clientLibraryId, clientTargetGeneration }; const batchController = useMemo(() => batchControllerFor({ clientReadOnly, clientHostBaseUrl, clientLibraryId, clientTargetGeneration, }), [clientReadOnly, clientHostBaseUrl, clientLibraryId, clientTargetGeneration]); @@ -521,7 +521,7 @@ export function useInventoryCreateActions({ quantity: 1, note: null, }, - { ...hostWriteTarget, clientTargetGeneration, libraryMode }, + { ...hostWriteTarget, libraryMode }, ); if (!isCurrent()) return; await reloadWishlist(); @@ -561,7 +561,7 @@ export function useInventoryCreateActions({ item_id: itemId, status, }, - { ...hostWriteTarget, clientTargetGeneration, libraryMode }, + { ...hostWriteTarget, libraryMode }, ); if (!isCurrent()) return; await reloadWishlist(); @@ -590,7 +590,7 @@ export function useInventoryCreateActions({ setConfirmWishlistRemoveId(null); setInfoMessage(null); try { - await deleteWishlistEntry(itemId, { ...hostWriteTarget, clientTargetGeneration, libraryMode }); + await deleteWishlistEntry(itemId, { ...hostWriteTarget, libraryMode }); if (!isCurrent()) return; await reloadWishlist(); } catch (deleteError) { @@ -684,7 +684,7 @@ export function useInventoryCreateActions({ attempt = pending; const receipt = await receiveWishlistEntry( { ...input, request_id: attempt.requestId }, - { ...hostWriteTarget, clientTargetGeneration, libraryMode, retry: attempt.retry }, + { ...hostWriteTarget, libraryMode, retry: attempt.retry }, ); if (!isConfirmedWishlistReceipt(receipt, quantity)) { throw createAppError(clientReadOnly diff --git a/ui/src/pages/use_printer_slot_interactions.ts b/ui/src/pages/use_printer_slot_interactions.ts index c1c568f8..ecdbcd09 100644 --- a/ui/src/pages/use_printer_slot_interactions.ts +++ b/ui/src/pages/use_printer_slot_interactions.ts @@ -1027,6 +1027,7 @@ export function usePrinterSlotInteractions({ clientReadOnly, clientHostBaseUrl, clientLibraryId, + clientTargetGeneration, }); const postCreateWrites = buildSlotCatalogOnboardingPostCreateWrites({ printerId, @@ -1076,6 +1077,7 @@ export function usePrinterSlotInteractions({ clientHostBaseUrl, clientLibraryId, clientReadOnly, + clientTargetGeneration, ensureLocalWriteAllowed, findLiveTrayForSlot, printers,