Multi-Account and Session Isolation for Desktop Clients

    This article explains why single-instance desktop clients break down when you need to switch between personal and organizational identities, and how to design proper account boundaries. It covers why single-instance locking should be scoped per profile rather than per machine, how to layer login-state storage across the system keystore and per-profile encrypted directories, why swapping local token files is an unsafe substitute for real account switching, and a recommended multi-profile model with atomic writes and first-login confirmation. It also lists the server-side constraints and a pre-release checklist for verifying that profiles stay isolated.

    Where the Problem Comes From

    Many AI coding tools and collaboration clients allow only one process per system user. A second instance launches and immediately exits, which prevents two UIs from writing to the same local state at once. For someone using a single account, this is fine. But once you need to move back and forth between a personal identity and an organizational identity, the single-instance rule becomes a hard limit.

    QR-code login amplifies this limit. The client often has no memorable password; the login result is a time-limited access token plus refresh material, written into the application support directory. After the process exits, the next launch reads that material directly to restore the session. So “keeping one identity logged in” means the other identity’s messages, tasks, and permissions are all invisible; forcing another login overwrites the local login state you just had.

    Treating this as “just install another shortcut” does not solve it. What actually needs to be separated is three things: whether the process is a singleton, whether on-disk account data is split into separate directories, and whether the keys protecting that data are isolated per account or per profile.

    Single Instance Is Not the Same as Single Account

    Desktop apps use a single instance typically to:

    • Avoid two processes writing logs, caches, indexes, and the local database at the same time and corrupting them;
    • Ensure external protocol links and file associations always go to the already-open window;
    • Reduce memory usage and simplify file replacement during auto-updates.

    These reasons are valid, but they constrain “the same profile,” not “one identity per computer.” The mature approach is to have the single-instance manager lock only a specific profile name. For example, the default profile holds one lock, and a profile named work holds another. Both profiles can run at the same time because their directories, socket names, and window identifiers all differ. If a product explicitly does not support running in parallel, it should at least provide an official path to “exit the current session and enter another profile,” rather than making users move files by hand.

    Where Should Login State Live

    Local login state generally contains the access token, refresh material, account identifier, tenant or organization identifier, expiration time, and sometimes a device identifier. All of this is sensitive data and should not sit in plaintext in the application support directory.

    A more robust layering is:

    1. Keys go into the system keychain or an equivalent local keystore. The app obtains only the unlocked short-lived key in memory and does not store a portable symmetric key as an ordinary file in the user’s home directory.
    2. Ciphertext is stored in per-profile directories. Each identity gets its own directory holding that identity’s token bundle, UI state, and cache. Directories do not share writable files.
    3. Keys are also derived or generated per profile. Even with the same encryption algorithm, profile A’s key must not unlock profile B. This way, copying files into another directory does not become a silent account switch.
    4. The server binds tokens to account, device, and app-version policy. When the machine, profile, or an anomalous replay changes, the server can require a fresh QR login instead of trusting only that “it looks like the same machine.”

    A single machine-level key shared across all accounts, plus one shared ciphertext path, is the classic structure of a failed isolation. Its direct consequence: whoever can read and write that directory can replace the login material; if the client only verifies that “the file decrypts and the token is not expired,” it treats the on-disk identity as the current user. This is not a convenience feature but a missing session boundary. When discussing such designs, treat it as a defect to fix, not a switch mechanism to productize.

    Do Not Use Local File Replacement as Account Switching

    Some people discover that the login material lives in the app data directory and the material that unlocks it sits in a fixed location, so exiting the process, swapping the files, and relaunching turns the UI into another logged-in identity. This looks like “just my two accounts,” but the problems are:

    • The token is still a credential issued by the server. Replaying or swapping it when the client design does not allow it may violate the terms of use and may trigger risk controls, remote logout, or device unbinding.
    • Once the machine-level key can be read by any local process, other programs under the same system user can also read or replace the login state. This is a credential-exposure surface, not a private trick.
    • Switching files before first login has completed device registration, key rotation, or server confirmation can cause the newly written session to be overwritten by an old file, showing up as a mysterious return to the old identity. This shows that the client’s login state machine and the on-disk files are not a one-to-one transaction.
    • Scripts that automate “exit, swap files, reopen” harden these dangers into a daily routine. A later upgrade that changes the directory or encryption format can then corrupt the profile or even send the wrong token to the wrong environment.

    So engineering-wise, you should not write, distribute, or rely on such switchers. When multiple identities are needed, change the product model or use the isolation boundaries the operating system provides.

    You can define a “profile” as a complete local identity, not just an extra remembered username.

    Directory convention

    • The base directory holds only version-independent launcher config, such as the current default profile name.
    • Each profile owns a subdirectory: account metadata, the encrypted token bundle, UI layout, local index, and logs.
    • The lock file or local socket name includes the profile name, so two profiles do not each decide the other is “already running” and exit instantly.

    Startup flow

    1. Resolve the profile name to open; fall back to the default profile if none is given.
    2. If a healthy process already exists for that profile, hand it focus instead of exiting the new process.
    3. If no process exists, load only that profile’s directory. Read the key from the system keychain by profile name; if it does not exist, create it at first login and bind it to this profile only.
    4. Use the unlocked material to exchange for a short-lived access token from the identity service. On failure, enter QR login; on success, atomically write the new token bundle: write a temp file first, validate it, then replace, so a half-written file is not read by the next launch.
    5. Treat other profiles’ directories as non-writable for this process, and do not even add them to the search path.

    First login and switching

    After a new identity’s first successful QR login, confirm the server has accepted this profile on this device before allowing a switch away. In implementation, mark the profile as “pending confirmation”: until the first valid identity refresh is received or the user explicitly completes onboarding, do not include that profile in the rotatable list. This prevents a login transaction that has not finished from being overwritten by another launch.

    Once confirmation is done, switching means “select another profile and launch or wake its process,” not swapping two token files. The old profile’s token stays in its own directory, and its expiration is handled by its own refresh logic.

    How to Isolate Process and Data

    If the product cannot be changed in the short term, users should adopt only legitimate isolation, not edit login files:

    • Use different operating-system users, or the officially provided multi-instance/multi-profile capability, so each identity has its own home directory and keychain entries.
    • Do not use the same browser profile to complete QR login for both work and personal identities, reducing the chaining of third-party login state.
    • Do not sync the application support directory to cloud storage. Syncing carries copies of the encrypted material to other machines and produces conflicting copies across two devices.
    • Before leaving a job, handing over, or sending a machine for repair, log out inside the client and clear that profile’s directory and its keychain entries. Deleting a UI shortcut alone does not revoke the token.

    On the development side, do not log tokens, refresh material, keychain handles, or identity identifiers within full user paths. Diagnostic bundles should be redacted by default. Crash recovery should restore only UI drafts, not automatically restore a token file of unknown origin.

    Constraints the Server Must Enforce

    Per-profile directories on the client alone are not enough. The identity service should at least:

    • Keep access tokens short-lived, make refresh material revocable, and distinguish client profile or device identifiers.
    • Show an explainable prompt when the same account logs in from a new client profile, instead of silently kicking out all sessions, unless a security policy explicitly requires a single session.
    • Require re-authentication on anomalous replay, a burst of refreshes in a short time, or a device identifier inconsistent with history.
    • Provide official identity switching or delegated login for enterprise tenants, rather than encouraging employees to keep a second local credential.

    These are all general identity-system designs and do not depend on any single cloud vendor’s proprietary interfaces. Object storage, message queues, or logging services have nothing to do with “whether to swap the token file locally”; do not treat session files as syncable business data.

    Pre-Release Checklist

    Before shipping multi-profile support, you can self-check with these questions:

    • Can two profiles run at the same time without fighting over locks or writing each other’s databases?
    • After deleting one profile’s directory, can the other profile still log in?
    • If you copy A’s token bundle into B’s directory, does B reject it instead of becoming A?
    • Are keychain entries separated per profile, and does uninstalling the app offer a clear option?
    • After an interrupted first QR login, a network failure, or a killed process, is no half identity left behind to be misused by the next launch?
    • When auto-update replaces program files, does it avoid merging or migrating the wrong profile directory?

    The illustrative startup selection below describes only the control flow and involves no real product paths or key formats:

    1. profile = args.profile or default_profile()
    2. acquire_lock(profile) # lock is scoped per profile
    3. key = keystore.open(profile) # create it only after first login if missing
    4. state_dir = app_support(profile)
    5. session = load_session(state_dir, key)
    6. if session is missing or rejected:
    7. session = interactive_login(profile)
    8. save_session_atomically(state_dir, key, session)
    9. run_ui(profile, session)

    The key points of this flow: the profile name runs through the lock, the key, and the directory; a login failure triggers re-authentication; and saving is atomic. It deliberately contains no branch that “replaces the current login state with another file.”

    Summary

    Single instance, QR login, and local encrypted storage can coexist, but the account boundary must be designed separately. A shared machine-level key plus a single login file degrades identity switching into file replacement and exposes the token to other programs under the same system user. The correct direction is to split directories, locks, and keys per profile, protect keys with the system keychain, constrain refresh and replay with server-side policy, and use atomic writes plus “first-login pending confirmation” to prevent cross-account mix-ups. For users, the right move is to wait for or push for official multi-profile support, or use operating-system user isolation; do not switch identities by moving login material when the client does not support it.