Skip to content

feat(dashboard): keep config backups before save (#956) - #959

Open
iceteaSA wants to merge 3 commits into
cortexkit:masterfrom
iceteaSA:fix/956-dashboard-config-backups
Open

iceteaSA wants to merge 3 commits into
cortexkit:masterfrom
iceteaSA:fix/956-dashboard-config-backups

Conversation

@iceteaSA

@iceteaSA iceteaSA commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Approved issue: #956 (implemented in the shape proposed there).

Behaviour

  • Before a save replaces a config, the dashboard copies the current file into <store>/config-backups/. The store is resolved as the plugin resolves it: MAGIC_CONTEXT_STORAGE_DIR, else XDG_DATA_HOME, else the platform's default data folder, then cortexkit/magic-context.
  • Copy names: magic-context.user.<UTC stamp with nanoseconds>-NN.jsonc for the user config, and magic-context.project-<12 hex of the sha256 of the canonical project path>.<stamp>-NN.jsonc for a project config. create_new and a counter keep two saves in the same second apart.
  • The newest 5 copies of each config are kept. Pruning counts only regular files with the same prefix, extension and stamp format. Pruning is best effort: if it fails, the save still completes, because the new copy is already on disk.
  • Owner-only: the save is refused if the backup folder is a symlink or not a directory. The folder is narrowed to 0700 through an O_NOFOLLOW handle, and each copy is written with O_NOFOLLOW and mode 0600.
  • If the copy cannot be written, the save is refused with the reason, and the config is not changed.
  • The first save of a config that does not exist yet makes no copy.

Points to review

  • A relative MAGIC_CONTEXT_STORAGE_DIR refuses the save, the same as the plugin refuses a relative override.
  • Copy names sort by time, so a backward clock jump can make pruning remove the wrong copy.
  • A symlinked parent above config-backups is still followed, because the plugin store follows it too.
  • Pi config saves use the user config path, so they share the user copy set.

Tests

The three requested tests (a save keeps the previous bytes; a sixth save drops the oldest copy; a copy failure leaves the config untouched), plus: copies go to the data folder and to the storage override; a project copy carries the project hash; owner-only modes; a symlinked backup folder refuses the save. cargo test --lib config: 24 pass. With the backup call removed, the 7 new behaviour tests fail.

RetriggerView in GreptileConfidence Score: 4/5

Separate Windows dashboard processes can still lose an intermediate config version, so the PR needs another fix before merging.

Findings

  1. P1 Overlapping saves miss a backup ▶

Summary

Adds config backups before dashboard saves.

  • Dashboard saves keep up to five previous config versions.
  • Overlapping saves keep a copy of each replaced config version.

Reviews (2) · Last reviewed commit: "fix(dashboard): name backups by the logi..." · Reviewed by Greptile

Before a save replaces the user or project config, copy the current file
into config-backups/ in the Magic Context store (same resolution as the
plugin: MAGIC_CONTEXT_STORAGE_DIR, XDG_DATA_HOME, ~/.local/share). The
folder is 0700 and each copy 0600, written with O_NOFOLLOW and create_new.
Copies carry a UTC timestamp with nanoseconds and a counter; project copies
carry a short hash of the canonical project path. The newest 5 copies per
config stay. If the copy fails, the save is refused and the config is not
changed.
@github-actions

Copy link
Copy Markdown

Waiting for design-approved on #956.

This PR needs a linked issue with the design-approved label before it can be reviewed or merged. Link the approved issue with Approved issue: #<issue> (or Refs #<issue>) in the description. A maintainer will apply the design-approved label on the issue, or trivial on the pull request when there is genuinely no design to agree. The issue stays open until the change ships; maintainers take care of it then.

Comment on lines +261 to +264
if let Err(e) = back_up_current_config(path, &backup_dir, &backup_scope) {
let _ = std::fs::remove_file(&temp_path);
return Err(e);
}

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.

P1 Overlapping saves miss a backup

Two save requests can both read version A, then save B and C in turn. Both backups contain A, so B disappears without a backup even though both saves succeed. Serialize the read, backup, prune, and replace steps for each config, including across dashboard processes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed: read, copy, prune and replace were not serialized, so two overlapping saves could both copy the same old version. Fixed in 6b58b23. Each save now holds an in-process mutex for its backup set and an exclusive flock on .<scope>.lock in the owner-only backup folder (opened with O_NOFOLLOW, mode 0600) from the read until after the replace. If the lock is still held after 10 s, the save is refused, the same as a failed copy. Saves of different configs do not wait for each other; on non-unix only the in-process mutex applies. Tests force the overlap with a read hook and with a lock held from another handle; with both locks removed, 3 tests fail.

Two overlapping saves could both copy the same old version, so the
version between them had no copy. Hold a per-config in-process mutex
and an exclusive flock on a lock file in the owner-only backup folder
from the read of the current config until after the replace. A lock
that stays held past 10 s refuses the save.
A symlinked user config named its copies after the link target, so a
link that changed to a different file started a new set of copies that
was never pruned. Name and prune the copies by the config path before
symlinks resolve; use the resolved path only to read and write.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant