Files
hermes-agent/apps
kshitijk4poor fbf6c6c91d fix(desktop): a failed gateway pick keeps the draft route the user chose
When a gateway pick reached its turn and the activation then failed,
the new-chat draft was reset to the current profile with no route. A
draft the user had pinned before the click (an explicit agent route,
for example to a local work profile, or a legacy profile pin) was lost,
and the next message went through a different owner. The failure path
also skipped the new-chat intent check the success path has, so it
could overwrite a draft the user started after the pick reached its
turn.

Before #147681 the re-home ran only after a successful activation, so
a failure touched nothing. That PR moved the re-home into
beforeActivate and answered a failure with a second re-home.

A pick now snapshots the draft before its re-home (profile, explicit
route, captured source, legacy profile pin). If its target never
becomes active, it gives that draft back, but only while the new-chat
intent is still the one its re-home produced, so a draft the user
started since is left alone. This also runs when a newer pick has
superseded the failed one. The restore reuses the profile store's own
setters (captureNewChatSource, pinLegacyNewChatProfile), so it is a
normal new-chat intent.

Overlapping picks need two more rules, because activations are
serialized and a queued pick takes its turn as soon as the one ahead
settles, before that pick's caller sees its failure. If the draft is
still the re-home of a pick ahead that did not land, the queued pick
inherits that pick's snapshot instead of snapshotting the tentative
draft. Otherwise two failed picks (A, then B) left the foreground on
the original gateway while the next message went to A. And if the
pick ahead already gave its draft back, that give-back does not count
as a newer intent, so a queued pick that succeeds still re-homes.

Tests (connections.test.ts), red with the pre-fix connections.ts:
- "a failed activation keeps the agent route the user pinned before
  the click": AssertionError: expected 'default' to be 'work'
- "a failed activation leaves alone a draft the user started after the
  pick reached its turn": AssertionError: expected 'default' to be 'scout'
- "a failed pick restores a legacy profile pin the draft had before
  the click": AssertionError: expected "vi.fn()" to be called with
  arguments: [ 'ops' ]
- "two overlapping picks ... both fail: the draft is the one from
  before the first click": AssertionError: expected 'default' to be
  null. Without the inherited snapshot (each pick undoing only its own
  re-home): AssertionError: expected 'homelab' to be null
- The existing failed-activation case now expects the draft back as it
  was before the click (null profile) instead of 'default':
  AssertionError: expected 'default' to be null
- "two overlapping picks ... the first lands and the second fails: the
  draft stays on the first": AssertionError: expected null to be
  'homelab'. Also red, with the same assertion, when a queued pick
  inherits the snapshot of a pick ahead that landed.
- "two overlapping picks ... the newer pick fails while opening, then
  the first fails: the pre-click draft is back": AssertionError:
  expected 'default' to be 'work'. Also red, with the same assertion,
  when a superseded failed pick skips its give-back.
"two overlapping picks ... the first fails and settles before the
second takes its turn: the second still re-homes the draft" passes on
the pre-fix code; it guards the give-back from being read as a newer
intent (red when that rule is dropped: expected null to be 'work-vps').
2026-11-02 19:05:47 +05:30
..