From 5e9850881d35d3af9ae8a2f99402e02300f77835 Mon Sep 17 00:00:00 2001
From: HsiangNianian <i@jyunko.cn>
Date: Sun, 18 Jan 2026 12:58:22 +0800
Subject: fix: complete Instance/Profile System isolation and state management

## Overview
Fixed critical multi-instance isolation bugs where versions, mod loaders, and
instance state were not properly isolated between instances. These changes ensure
full data isolation and consistent instance metadata.

## Bug Fixes - P0 (Critical Isolation Issues)

### 1. Backend: get_versions() command isolation
- Problem: Used global app_data_dir instead of instance-specific game_dir
- Fix: Added instance_id parameter, now queries instance.game_dir
- Impact: Versions are now properly isolated per instance

### 2. Frontend: delete_version missing instanceId
- Problem: Frontend passed only versionId, not instanceId
- Fix: Updated VersionsView.svelte to pass instanceId parameter
- Impact: Version deletion now targets correct instance

### 3. Frontend: get_version_metadata missing instanceId
- Problem: Metadata queries didn't specify which instance to check
- Fix: Updated VersionsView.svelte to pass instanceId parameter
- Impact: Version info displayed per-instance correctly

### 4. Frontend: Instance switching doesn't refresh versions
- Problem: Switching instances didn't reload version list
- Fix: Added $effect hook in GameState to watch activeInstanceId changes
- Impact: Version list auto-refreshes on instance switch

## Bug Fixes - P1 (State Synchronization)

### 5. Backend: install_fabric doesn't update Instance.mod_loader
- Problem: Instance.mod_loader field wasn't updated after installation
- Fix: Added instance_state.update_instance() call
- Impact: Instance metadata stays in sync

### 6. Backend: install_forge doesn't update Instance.mod_loader
- Problem: Instance.mod_loader field wasn't updated after installation
- Fix: Added instance_state.update_instance() call
- Impact: Instance metadata stays in sync

### 7. Backend: delete_version doesn't clean up Instance state
- Problem: Deleting version didn't clear Instance.version_id or .mod_loader
- Fix: Added cleanup logic to clear stale references
- Impact: Instance state remains valid after deletion

## Testing
- Added comprehensive integration tests in instance_isolation_tests.rs
- Tests document 10 key scenarios for multi-instance isolation
- All code compiles cleanly with no errors
---
 ui/src/components/VersionsView.svelte | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

(limited to 'ui/src/components/VersionsView.svelte')

diff --git a/ui/src/components/VersionsView.svelte b/ui/src/components/VersionsView.svelte
index d4d36d5..f1474d9 100644
--- a/ui/src/components/VersionsView.svelte
+++ b/ui/src/components/VersionsView.svelte
@@ -217,7 +217,10 @@
     if (!versionToDelete) return;
 
     try {
-      await invoke("delete_version", { versionId: versionToDelete });
+      await invoke("delete_version", { 
+        instanceId: instancesState.activeInstanceId,
+        versionId: versionToDelete 
+      });
       // Clear selection if deleted version was selected
       if (gameState.selectedVersion === versionToDelete) {
         gameState.selectedVersion = "";
@@ -253,6 +256,7 @@
     isLoadingMetadata = true;
     try {
       const metadata = await invoke<VersionMetadata>("get_version_metadata", {
+        instanceId: instancesState.activeInstanceId,
         versionId,
       });
       selectedVersionMetadata = metadata;
-- 
cgit v1.2.3-70-g09d2

