Skip to content

Repository stage stash/local-changes handling is fragile and can leave the tree in unrecoverable dirty state after interrupted updates #46791

Closed as not planned

Description

@apoapostolov

Repository stage stash/local-changes handling is fragile and can leave the tree in unrecoverable dirty state after interrupted updates

Observed failure

The repository stage in the bootstrap installer (and the equivalent path in hermes update) has extensive defensive logic for local changes because a dirty tree aborts git checkout / git pull with "Your local changes would be overwritten by checkout".

When this logic itself fails (or a prior run was interrupted), the tree is left with:

  • Unmerged index entries
  • A pending stash that may or may not be restorable
  • Modified generated files (package-lock.json etc.) that the next update attempt also chokes on

This is exactly the class of dirt that produced the unrecoverable state in #46763 (GUI users hitting "git checkout main failed", manual CLI update also failing until the tree was manually cleaned, and redownload hitting the same state).

Root cause

scripts/install.ps1 (repository stage, ~lines 1162-1266) contains the full logic:

  • Pins core.autocrlf=false to avoid creating dirt on checkout.
  • Runs git status --porcelain; if dirty, does git stash push --include-untracked.
  • Special handling for previously interrupted updates: checks ls-files --unmerged, runs git reset to clear conflict markers before stashing (otherwise git stash and subsequent git checkout both abort).
  • After fetch/checkout/pull, attempts git stash apply + drop.
  • Console detection hack (UserInteractive && !InputRedirected && !OutputRedirected && ConsoleHost) to decide whether to prompt; in desktop handoff / bootstrap runs (no usable TTY) it forces restore.
  • On any failure in the restore block or earlier throw: leaves the stash behind and emits instructions, but the stage has already failed or the tree is in a half-applied state.
  • The finally block only cleans up the stash ref if we never reached the restore section.

The same patterns exist (with less defensive code) in the hermes update path and install.sh.

A single interrupted update, a stash-apply failure, or a non-interactive restore that re-introduces dirt (e.g. the exact package-lock.json that the dirty-tree resolver targets) leaves the managed checkout in the state that makes the next handoff or reinstall also fail.

Impact on casual users

This is the mechanical reason the "dirty tree → broken exe → redownload also fails" loop exists. The stash machinery is intended as a safety net, but its own failure modes (unmerged index, non-interactive restore, partial failures) are exactly what produces the unrecoverable checkout that #46763 describes.

The current resolver proposal in #46763 helps once the dirt is detected, but does not address the source of the dirt coming from the stash/local-changes handling itself.

Proposed direction

  • Make the stash handling a first-class "resolver" step that can be invoked both by the repository stage and by the recovery path in No automatic recovery or repair path after desktop update handoff fails, leaving users unable to launch or reinstall #46782.
  • On detecting unmerged entries or a pending hermes-install-autostash, offer (or auto-apply in the desktop handoff case) a clean git reset --merge + targeted stash pop for generated files only, before the main checkout.
  • After any successful update, always attempt a non-destructive cleanup of known generated dirt (package-lock.json, etc.) even if a stash was involved.
  • Improve the finally-block logic so that an interrupted stash push still leaves a usable tree + clear marker for the recovery UI.
  • Surface the stash ref and the exact commands in the bootstrap progress / desktop error UI instead of just "git checkout failed".

This turns the existing defensive hacks into an explicit, recoverable part of the update contract instead of a source of permanent dirt.

Activity

  1. added
    type/bugSomething isn't working
    comp/cliCLI entry point, hermes_cli/, setup wizard
    area/configConfig system, migrations, profiles
    P2Medium — degraded but workaround exists
    on Jun 15, 2026
  2. baoyu0 commented on Jun 16, 2026

    @baoyu0

    Additional failure pattern: interactive stash-restore loop + stale update cache

    I hit a variant of this today after having 43 locally-modified source files on main. The failure loop and root-cause analysis may be useful for the resolver proposal.

    Reproduction (interactive CLI, not desktop)

    1. Have local tracked modifications in the Hermes checkout (not config — actual source files like agent/auxiliary_client.py, gateway/, hermes_cli/ etc.) that overlap with upstream changes.
    2. Run hermes update — it stashes changes, fetches, pulls successfully.
    3. _restore_stashed_changes (main.py:6102) hits merge conflicts because local changes overlap with new upstream code. It prints the conflicted files and runs git reset --hard HEAD, leaving the working tree clean but leaving the stash in place.
    4. HEAD never advanced — because stash restore failed, but user gets no indicator that HEAD moved or didn't. Next hermes --version re-checks, cache still returns old stale count because VERSION hasn't bumped (same v0.16.0), so the check_for_updates() cache not invalidated after pip upgrade, showing stale "1 commit behind" #34491 guard sees cached["ver"] == VERSION as a match.
    5. User runs hermes update again — same cycle, another autostash piles up.

    After 4 runs we had 4 hermes-update-autostash-* entries, HEAD still at the old commit, and hermes --version stuck on "10 commits behind" due to the stale cache.

    Two gaps not fully covered yet

    1. No interactive "discard local changes and finish update" option

    main.py:8424-8443 — The discard_local_changes path only activates in non-interactive mode (--yes, gateway, non-TTY). In interactive mode, the code always tries _restore_stashed_changes, and if that conflicts, the user is stuck in a loop with no way to say "I give up, just throw away my changes and finish the update."

    It would help to add a prompt in the conflict handler along the lines of:

    "Local changes conflict with the updated codebase. Discard them and continue? [y/N]"

    2. Stale update-check cache survives even after a manual git pull that succeeds

    After we manually fixed the tree (git reset --hard HEAD && git pull --ff-only), hermes --version still showed "10 commits behind" because the .update_check cache had {"behind": 10, "ver": "0.16.0"} and VERSION hadn't changed. The cache was only invalidated once we manually deleted ~/.hermes/.update_check.

    The _invalidate_update_cache() function (main.py:6477) deletes the cache file, but check_for_updates() (banner.py:290) re-creates it on every check. If the cache was written before the update succeeded (HEAD not moved), it keeps returning the old count until it expires (6h TTL).

    Suggestion: when _check_via_local_git() returns 0, write {"behind": 0} to the cache unconditionally so a subsequent check immediately shows "Up to date."

    Environment

    • Hermes v0.16.0 (a68ac0c after manual fix)
    • Windows 10, Python 3.11.15
    • Git-based install (not pip/Docker/nix)
    • Pure CLI hermes update (no desktop app)
  3. teknium1 commented on Jul 14, 2026

    @teknium1
    Collaborator

    This is an automated hermes-sweeper review: the reported recovery paths are implemented on current main.

    • 573b964dc780a9aae91eaf7b5a64497cfdfd2828 added unmerged-index recovery before autostashing in both scripts/install.ps1 and scripts/install.sh; the current installer code clears the index before its stash/checkout path.
    • hermes_cli/main.py:6574–6604 detects a failed/conflicted stash restore, resets the checkout to clean HEAD, preserves the stash with its exact recovery reference, and allows the completed update to continue.
    • hermes_cli/main.py:6929–6953 invalidates stale update-check caches across profiles; hermes_cli/main.py:9271–9305 removes managed package-lock.json churn before autostashing.
    • Regression coverage includes a real unmerged-index installer scenario in tests/test_install_unmerged_index.py:87–127 and stash-conflict cleanup coverage in tests/hermes_cli/test_update_autostash.py:247–304.

    These changes are included in v2026.7.1.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existsarea/configConfig system, migrations, profilescomp/cliCLI entry point, hermes_cli/, setup wizardsweeper:implemented-on-mainSweeper: behavior already present on current maintype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions