Skip to content

Fix: prevent full settings reset on schema mismatch#1263

Merged
shm11C3 merged 4 commits into
developfrom
fix/settings-backward-compatibility
Mar 20, 2026
Merged

Fix: prevent full settings reset on schema mismatch#1263
shm11C3 merged 4 commits into
developfrom
fix/settings-backward-compatibility

Conversation

@shm11C3

@shm11C3 shm11C3 commented Mar 20, 2026

Copy link
Copy Markdown
Owner

This pull request improves the robustness and backward compatibility of settings deserialization and management. It ensures that missing fields in settings.json or related structs are safely filled with default values, and adds logic to recover from partial or invalid settings files. Additionally, it updates the settings version automatically and persists new fields after app upgrades. The most important changes are grouped below:

Settings deserialization and recovery

  • Added #[serde(default)] to Settings, LineGraphColorSettings, BurnInShiftOptions, and HardwareArchiveSettings structs, and implemented their Default trait to ensure missing fields are filled with sensible defaults during deserialization. [1] [2] [3]
  • Improved the read_file method in SettingsService to attempt field-by-field recovery if full deserialization fails, logging errors and preserving valid fields while falling back to defaults for invalid or missing fields.
  • Added a merge_from_json_str method to Settings for granular field recovery from a JSON object, with detailed error logging for each failed field.

Version management and persistence

  • Updated the Settings::new method to automatically update the settings version and persist new fields added by app updates, ensuring that the settings file stays current.

Testing and validation

  • Added comprehensive tests to verify deserialization behavior with missing fields, minimal JSON, invalid values, and field-level recovery, ensuring robust backward compatibility and error handling.

Copilot AI review requested due to automatic review settings March 20, 2026 17:41
@github-actions github-actions Bot added bug Something isn't working rust Pull requests that update Rust code labels Mar 20, 2026
@github-actions

github-actions Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Rust Backend Coverage Report

Coverage Details
Filename                                         Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
_tests/commands/background_image_test.rs              39                 0   100.00%           6                 0   100.00%          21                 0   100.00%           0                 0         -
_tests/commands/settings_test.rs                     220                 0   100.00%          18                 0   100.00%         167                 0   100.00%           0                 0         -
commands/background_image.rs                          22                 7    68.18%          11                 5    54.55%          19                 7    63.16%           0                 0         -
commands/hardware.rs                                  65                65     0.00%          20                20     0.00%          65                65     0.00%           0                 0         -
commands/settings.rs                                 544               544     0.00%          97                97     0.00%         463               463     0.00%           0                 0         -
commands/system.rs                                     6                 6     0.00%           3                 3     0.00%           5                 5     0.00%           0                 0         -
commands/ui.rs                                        17                17     0.00%           2                 2     0.00%          13                13     0.00%           0                 0         -
commands/updater.rs                                   97                97     0.00%          15                15     0.00%          66                66     0.00%           0                 0         -
enums/error.rs                                       105                 0   100.00%           8                 0   100.00%          89                 0   100.00%           0                 0         -
enums/hardware.rs                                    188                 1    99.47%          15                 0   100.00%         114                 0   100.00%           0                 0         -
enums/settings.rs                                    415                 6    98.55%          24                 0   100.00%         279                 0   100.00%           0                 0         -
infrastructure/database/db.rs                         21                21     0.00%           2                 2     0.00%          12                12     0.00%           0                 0         -
infrastructure/database/gpu_archive.rs                49                49     0.00%           4                 4     0.00%          18                18     0.00%           0                 0         -
infrastructure/database/hardware_archive.rs           41                41     0.00%           4                 4     0.00%          21                21     0.00%           0                 0         -
infrastructure/database/migration.rs                   8                 8     0.00%           1                 1     0.00%          37                37     0.00%           0                 0         -
infrastructure/database/process_stats.rs              41                41     0.00%           4                 4     0.00%          29                29     0.00%           0                 0         -
infrastructure/providers/linux/dmidecode.rs          229                15    93.45%          16                 3    81.25%         319                14    95.61%           0                 0         -
infrastructure/providers/linux/drm_sys.rs            143               143     0.00%          13                13     0.00%          87                87     0.00%           0                 0         -
infrastructure/providers/linux/hwmon.rs              119                94    21.01%           8                 6    25.00%          68                56    17.65%           0                 0         -
infrastructure/providers/linux/kernel.rs             165                22    86.67%          19                 2    89.47%         161                 8    95.03%           0                 0         -
infrastructure/providers/linux/lspci.rs               23                23     0.00%           1                 1     0.00%          12                12     0.00%           0                 0         -
infrastructure/providers/linux/net_sys.rs            171               171     0.00%          13                13     0.00%          93                93     0.00%           0                 0         -
infrastructure/providers/linux/procfs.rs             261                24    90.80%          25                 3    88.00%         222                19    91.44%           0                 0         -
infrastructure/providers/sysinfo_provider.rs          54                54     0.00%           2                 2     0.00%          45                45     0.00%           0                 0         -
lib.rs                                               209               209     0.00%           5                 5     0.00%         114               114     0.00%           0                 0         -
main.rs                                                3                 3     0.00%           1                 1     0.00%           3                 3     0.00%           0                 0         -
models/hardware.rs                                   133                 0   100.00%          11                 0   100.00%          83                 0   100.00%           0                 0         -
models/hardware_archive.rs                             3                 0   100.00%           1                 0   100.00%           7                 0   100.00%           0                 0         -
models/settings.rs                                   301                 0   100.00%          17                 0   100.00%         264                 0   100.00%           0                 0         -
platform/factory.rs                                   18                18     0.00%           4                 4     0.00%          15                15     0.00%           0                 0         -
platform/linux/cache.rs                               53                53     0.00%           4                 4     0.00%          38                38     0.00%           0                 0         -
platform/linux/gpu.rs                                139               139     0.00%          13                13     0.00%         102               102     0.00%           0                 0         -
platform/linux/memory.rs                              43                43     0.00%           6                 6     0.00%          41                41     0.00%           0                 0         -
platform/linux/mod.rs                                 34                34     0.00%          11                11     0.00%          70                70     0.00%           0                 0         -
platform/linux/network.rs                              4                 4     0.00%           1                 1     0.00%           4                 4     0.00%           0                 0         -
services/archive_service.rs                         1127               154    86.34%          90                15    83.33%         654               134    79.51%           0                 0         -
services/background_image_service.rs                 165                96    41.82%          16                10    37.50%          93                59    36.56%           0                 0         -
services/cpu_service.rs                               32                32     0.00%           4                 4     0.00%          15                15     0.00%           0                 0         -
services/gpu_service.rs                               37                37     0.00%          10                10     0.00%          31                31     0.00%           0                 0         -
services/hardware_service.rs                          67                67     0.00%           5                 5     0.00%          43                43     0.00%           0                 0         -
services/language_service.rs                         101                 0   100.00%          18                 0   100.00%          57                 0   100.00%           0                 0         -
services/memory_service.rs                            22                22     0.00%           4                 4     0.00%          15                15     0.00%           0                 0         -
services/monitoring_service.rs                       776               138    82.22%          54                15    72.22%         438                91    79.22%           0                 0         -
services/motherboard_service.rs                       10                10     0.00%           3                 3     0.00%           7                 7     0.00%           0                 0         -
services/network_service.rs                            9                 9     0.00%           1                 1     0.00%           7                 7     0.00%           0                 0         -
services/process_service.rs                           86                86     0.00%           5                 5     0.00%          50                50     0.00%           0                 0         -
services/settings_service.rs                         338               158    53.25%          34                16    52.94%         288               148    48.61%           0                 0         -
services/system_service.rs                            22                22     0.00%           2                 2     0.00%          12                12     0.00%           0                 0         -
services/ui_service.rs                                45                45     0.00%           8                 8     0.00%          36                36     0.00%           0                 0         -
utils/color.rs                                        66                 1    98.48%           4                 0   100.00%          26                 0   100.00%           0                 0         -
utils/file.rs                                        224                 5    97.77%          14                 0   100.00%         144                 4    97.22%           0                 0         -
utils/formatter.rs                                   195                 8    95.90%          16                 0   100.00%         160                12    92.50%           0                 0         -
utils/ip.rs                                           65                 0   100.00%           5                 0   100.00%          33                 0   100.00%           0                 0         -
utils/logger.rs                                       71                71     0.00%           1                 1     0.00%          38                38     0.00%           0                 0         -
utils/rounding.rs                                     68                 0   100.00%           7                 0   100.00%          41                 0   100.00%           0                 0         -
utils/tauri.rs                                       138                 0   100.00%          17                 0   100.00%          82                 0   100.00%           0                 0         -
workers/hardware_archive.rs                           52                52     0.00%           6                 6     0.00%          36                36     0.00%           0                 0         -
workers/mod.rs                                        24                24     0.00%           2                 2     0.00%          16                16     0.00%           0                 0         -
workers/system_monitor.rs                            151               151     0.00%          11                11     0.00%         106               106     0.00%           0                 0         -
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                               7874              3140    60.12%         742               363    51.08%        5594              2317    58.58%           0                 0         -

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves robustness of persisted Tauri/Rust settings loading so app updates or schema/value mismatches don’t trigger a full settings reset, while also backfilling new/defaulted fields.

Changes:

  • Adds #[serde(default)] + Default impls to settings-related structs to allow deserialization with missing fields.
  • Implements a fallback “field-level recovery” path when full Settings deserialization fails.
  • Adds unit tests covering missing-field deserialization and fallback recovery behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src-tauri/src/services/settings_service.rs Refactors settings file read, adds fallback merge-based recovery, and persists settings on app version change.
src-tauri/src/models/settings.rs Adds serde defaults + Default impls for nested settings structs and adds tests for compatibility/recovery.
src-tauri/src/models/hardware_archive.rs Adds serde defaults + Default impl for HardwareArchiveSettings to support missing-field deserialization.

Comment thread src-tauri/src/services/settings_service.rs
Comment thread src-tauri/src/services/settings_service.rs Outdated
Comment thread src-tauri/src/services/settings_service.rs
Copilot AI review requested due to automatic review settings March 20, 2026 17:50
@shm11C3 shm11C3 enabled auto-merge (squash) March 20, 2026 17:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

);
Err(format!("Failed to read settings file: {e}"))
// Fall back to field-by-field recovery
self.merge_from_json_str(&input)

Copilot AI Mar 20, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When full deserialization fails but merge_from_json_str succeeds, the recovered settings are not persisted unless settings.version != current_version. If the file contains a permanently invalid value (e.g. invalid enum string) but the version already matches, startup will log an error and fall back on every run. Consider writing the recovered/defaulted settings back to disk after a successful merge (possibly with a best-effort write_file() + log on failure).

Suggested change
self.merge_from_json_str(&input)
match self.merge_from_json_str(&input) {
Ok(()) => {
// Best-effort: try to persist the recovered/defaulted settings so that
// subsequent startups don't repeatedly hit the recovery path.
if let Err(write_err) = self.write_file() {
log_error!(
"Recovered settings but failed to persist updated settings file",
"read_file",
Some(write_err)
);
}
Ok(())
}
Err(recover_err) => Err(recover_err),
}

Copilot uses AI. Check for mistakes.
Comment on lines 112 to 116
log_error!(
"Failed to deserialize settings",
"Failed to deserialize settings, attempting field-level recovery",
"read_file",
Some(e.to_string())
);

Copilot AI Mar 20, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch logs an error even though the code can recover and continue successfully via field-level merge. Consider logging this as warn (and reserving error for cases where recovery also fails) to avoid misleading error-level logs for recoverable schema drift/corruption.

Copilot uses AI. Check for mistakes.
Comment on lines +27 to +32
impl Default for HardwareArchiveSettings {
fn default() -> Self {
Self {
enabled: true,
refresh_interval_days: 30,
scheduled_data_deletion: true,

Copilot AI Mar 20, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that HardwareArchiveSettings implements Default and is used for #[serde(default)] deserialization, there are multiple hard-coded copies of these same default values elsewhere (e.g. Settings::default and settings tests). To avoid future divergence, consider switching those call sites to HardwareArchiveSettings::default() as the single source of truth for defaults.

Suggested change
impl Default for HardwareArchiveSettings {
fn default() -> Self {
Self {
enabled: true,
refresh_interval_days: 30,
scheduled_data_deletion: true,
impl HardwareArchiveSettings {
/// Default value for whether hardware archiving is enabled.
pub const DEFAULT_ENABLED: bool = true;
/// Default value for whether scheduled data deletion is enabled.
pub const DEFAULT_SCHEDULED_DATA_DELETION: bool = true;
/// Default refresh interval in days for hardware archiving.
pub const DEFAULT_REFRESH_INTERVAL_DAYS: u32 = 30;
}
impl Default for HardwareArchiveSettings {
fn default() -> Self {
Self {
enabled: Self::DEFAULT_ENABLED,
scheduled_data_deletion: Self::DEFAULT_SCHEDULED_DATA_DELETION,
refresh_interval_days: Self::DEFAULT_REFRESH_INTERVAL_DAYS,

Copilot uses AI. Check for mistakes.
Comment on lines +16 to +23
impl Default for LineGraphColorSettings {
fn default() -> Self {
Self {
cpu: [75, 192, 192],
memory: [255, 99, 132],
gpu: [255, 206, 86],
}
}

Copilot AI Mar 20, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LineGraphColorSettings now has a Default impl (and #[serde(default)] depends on it), but the same default RGB values are still duplicated in Settings::default and some tests. Consider using LineGraphColorSettings::default() at those call sites to keep defaults consistent and reduce maintenance overhead.

Copilot uses AI. Check for mistakes.
@shm11C3 shm11C3 merged commit 68d0f05 into develop Mar 20, 2026
28 checks passed
@shm11C3 shm11C3 deleted the fix/settings-backward-compatibility branch March 20, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants