Skip to content

fix(web): Remove saved environments atomically#2917

Open
mwolson wants to merge 1 commit into
pingdotgg:mainfrom
mwolson:fix/saved-environment-persistence-cleanup
Open

fix(web): Remove saved environments atomically#2917
mwolson wants to merge 1 commit into
pingdotgg:mainfrom
mwolson:fix/saved-environment-persistence-cleanup

Conversation

@mwolson

@mwolson mwolson commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #2914.

This makes saved-environment removal use a single persistence operation that removes metadata and embedded credentials together, and tightens rollback behavior when replacing stale desktop SSH records.

Problem and Fix

Problem and Why it Happened Fix
Removing a saved environment updated persisted metadata and bearer-token state separately, so SSH cleanup could race with deletion and stale records could reappear. Add removeSavedEnvironment to the local persistence API and desktop IPC bridge so metadata and embedded secrets are removed atomically.
The UI cleared saved-environment state before durable removal finished. Persist removal first, then clear registry/runtime/UI state, while still awaiting SSH cleanup before returning.
Browser fallback persistence did not have an equivalent atomic saved-environment removal path. Add browser-storage removal alongside the desktop bridge implementation.
Replacing a stale desktop SSH record only snapshotted the new environment id. Snapshot both the new id and stale SSH record so rollback can restore the previous saved record if credential persistence fails.

Defensive Fixes

Problem and Why it Happened Fix
Rollback errors could hide the original credential persistence error. Preserve and rethrow the primary credential persistence failure after best-effort rollback.
SSH cleanup errors during removal could block state cleanup in the wrong order. Remove persisted saved-environment state first, then await SSH cleanup and log cleanup failures without restoring stale persistence.

Validation

  • bun fmt
  • bun lint
  • bun typecheck
  • bun run --filter @t3tools/desktop test -- DesktopSavedEnvironments
  • bun run --filter @t3tools/web test -- localApi catalog service.addSavedEnvironment service.savedEnvironments

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes: N/A, persistence behavior only
  • I included a video for animation/interaction changes: N/A

Note

Medium Risk
Changes ordering of credential/registry removal and SSH teardown plus rollback paths for add/replace flows; mistakes could leave orphaned or resurrected saved environments, though behavior is heavily covered by tests.

Overview
Adds removeSavedEnvironment end-to-end so saved environments are deleted in one persistence step (registry metadata plus embedded secrets on desktop/browser), wired through IPC, localApi, and removePersistedSavedEnvironment.

removeSavedEnvironment now tears down connections, persists removal first, then clears in-memory registry/runtime/UI state, and only then runs desktop SSH cleanup (failures are logged, not rolled back). disconnectSavedEnvironment no longer deletes saved records; it only disconnects and, for SSH, clears the bearer token via the existing secret-removal path.

Rollback when adding/replacing environments is tightened: bearer-token writes go through persistSavedEnvironmentBearerTokenOrRollback, registry rollback can keep the original error when rollback itself fails, and replacing a stale desktop SSH target snapshots both environment ids and uses atomic removal for the old record with rollback if credential persistence or stale removal fails.

Reviewed by Cursor Bugbot for commit 33193f4. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Remove saved environments atomically with rollback on credential persistence failure

  • Adds removePersistedSavedEnvironment across desktop and browser persistence layers, wiring it through IPC (desktop:remove-saved-environment), DesktopBridge, and LocalApi.
  • Refactors removeSavedEnvironment in service.ts to delete the persisted record and update in-memory stores before running SSH cleanup; SSH cleanup errors are logged and ignored.
  • Introduces removeStaleSavedEnvironmentRecord for atomic replacement flows in addSavedEnvironment, so stale records are removed from persistence without triggering bearer token deletion or disconnect.
  • Centralizes bearer token persistence and rollback in persistSavedEnvironmentBearerTokenOrRollback; rollback now preserves the primary error and attaches rollback failure context.
  • Behavioral Change: removeSavedEnvironment no longer calls the separate bearer token deletion step — token cleanup is handled implicitly by the desktop persistence layer when removing the record.

Macroscope summarized 33193f4.

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b695884c-f0c7-46c3-9d11-4c3a296d0c89

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list. labels Jun 2, 2026
@macroscopeapp

macroscopeapp Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Needs human review

This PR introduces significant runtime behavior changes to saved environment persistence, including new IPC methods and atomic operation ordering with rollback handling. While well-tested, the changes affect critical data persistence flows and should be reviewed by someone familiar with this subsystem.

You can customize Macroscope's approvability policy. Learn more.

macroscopeapp[bot]
macroscopeapp Bot previously approved these changes Jun 2, 2026
@macroscopeapp macroscopeapp Bot dismissed their stale review June 2, 2026 23:50

Dismissing prior approval to re-evaluate 933a157

Comment thread apps/web/src/environments/runtime/service.ts
Comment thread apps/web/src/environments/runtime/service.ts
macroscopeapp[bot]
macroscopeapp Bot previously approved these changes Jun 3, 2026
@mwolson mwolson force-pushed the fix/saved-environment-persistence-cleanup branch from 57451d3 to 61426fd Compare June 3, 2026 12:43
@macroscopeapp macroscopeapp Bot dismissed their stale review June 3, 2026 12:44

Dismissing prior approval to re-evaluate 61426fd

Comment thread apps/web/src/environments/runtime/service.ts Outdated
@mwolson mwolson force-pushed the fix/saved-environment-persistence-cleanup branch from 61426fd to 177b89b Compare June 3, 2026 13:40
@github-actions github-actions Bot added size:XL 500-999 changed lines (additions + deletions). and removed size:L 100-499 changed lines (additions + deletions). labels Jun 3, 2026

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 177b89b. Configure here.

Comment thread apps/desktop/src/app/DesktopApp.ts Outdated
@mwolson mwolson force-pushed the fix/saved-environment-persistence-cleanup branch 2 times, most recently from 1e011dc to cfc3112 Compare June 3, 2026 13:46
@github-actions github-actions Bot added size:L 100-499 changed lines (additions + deletions). and removed size:XL 500-999 changed lines (additions + deletions). labels Jun 3, 2026
Comment thread apps/web/src/environments/runtime/service.ts
@mwolson mwolson force-pushed the fix/saved-environment-persistence-cleanup branch from cfc3112 to c3497b0 Compare June 3, 2026 14:02
@juliusmarminge juliusmarminge force-pushed the fix/saved-environment-persistence-cleanup branch from c3497b0 to cc9d672 Compare June 4, 2026 00:58
@mwolson mwolson force-pushed the fix/saved-environment-persistence-cleanup branch 2 times, most recently from 6033220 to 66e84a7 Compare June 10, 2026 22:21
@mwolson mwolson force-pushed the fix/saved-environment-persistence-cleanup branch from 66e84a7 to 33193f4 Compare June 10, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Saved SSH environments can reappear or lose rollback state during removal

1 participant