Keep an open editor's pickers in step with the vault

The host editor's four pickers were snapshots taken when it opened, and the
comment on EditorAuthenticationChoices said why: a picker whose contents move
under somebody halfway through a form is worse than a list a minute stale, and
only one editor could be open at a time anyway, so the only way to add a key was
to close this one. The second half of that stopped being true when
AHostEditorIsInTheWay was split from AVaultEditorIsInTheWay. The host editor is
the Hosts screen's business and the keychain's editors are the Vault screen's;
neither refuses the other now, which was the right split — it stopped three
quarters of a screen going inert over an editor the user was not looking at — but
it left the assumption those snapshots rested on false and nothing to notice.

So the ordinary way of using the feature was the broken one. Somebody starts
editing a host, finds there is no key to bind it to, goes to KEYS, makes one, and
comes back to a picker that does not have it — with the fix being to throw the
form away and start again. The same for a password, a tag, a group, and for a
whole vault made on the Teams screen because the host being typed belongs to the
team rather than to the person typing it: the vault they had just made for it was
the one place they could not file it.

RefreshOpenEditors refills whichever editor is open, and it hangs off ReloadAsync
rather than off the twenty-odd commands that write to the vault. That is the
choice worth stating, because it is what makes a sync count as well as a save: a
key pulled from another machine reaches the open editor by the same path a key
typed here does, and a place that wrote to the vault without refreshing the editor
would be a bug nobody would find for months.

What the old comment was protecting against is real, so every picker is put back
onto what it was already showing, by id, and not one typed field is touched. An
editor that reset its own bindings because a background sync landed would be a
worse bug than the stale list this fixes — it would rebind a host as a side effect
of somebody else's work. The placeholder entries go back too, which is the case
3.4 measures: a group deleted on another machine mid-edit still cannot unfile the
host when the form is saved. The group editor gets the same treatment for the same
reasons; it shares the drawer, and its default binding is lent to every host under
it.

The snippet editor's vault picker was the same copy of the same list and went
stale the same way. It watches TargetVaults rather than the reload, because that
screen has always been a wrapper over the vault's collections and has no reload of
its own to hang off — which is how it already follows Snippets.

The move panels are deliberately left alone. A vault arriving from a sync while
one is open still will not appear in it, but a move panel is opened by the act that
fills it and its picker resets its selection to the first entry on every rebuild,
so refreshing it would move a destination somebody had chosen. Same class of bug,
different answer, and not this change.

Five tests, and four of them were checked failing with the RefreshOpenEditors call
commented out: a key reaching the open host editor and binding when chosen, an
item arriving without moving a selection that was already made, a tag arriving as
an unworn chip, a key reaching the group editor, and a vault reaching the host and
snippet editors without moving either. Manual check 7.12 sits beside 7.11, which
is this same bug on the files screen's picker, and says what the worse failure
would look like: a picker that moves rather than one that does not notice.
This commit is contained in:
2026-08-06 12:08:25 +02:00
parent f1d6499bb5
commit 6d6edb02c1
5 changed files with 342 additions and 4 deletions
@@ -72,6 +72,11 @@ internal sealed partial class SnippetsViewModel : ObservableObject
vault.Snippets.CollectionChanged += OnSnippetsChanged;
// The editor's vault picker is a snapshot of this list — see BuildEditorVaultChoices — and a vault
// created on the Teams screen with a half-typed snippet open behind it would otherwise not be
// offered until the editor was closed and opened again.
vault.TargetVaults.CollectionChanged += OnTargetVaultsChanged;
Rebuild();
}
@@ -577,6 +582,21 @@ internal sealed partial class SnippetsViewModel : ObservableObject
private void OnSnippetsChanged(object? sender, NotifyCollectionChangedEventArgs e) => Rebuild();
/// <summary>Refills the open editor's vault picker, landing back on the vault it was already writing to.</summary>
/// <remarks>
/// Only while the editor is open, because that is the only picker built from a copy of the list — the move
/// panel's is built when it opens and folds away with the selection it was opened about. Restored by the
/// latched <see cref="editorVaultId"/> rather than by what the control shows, so a vault arriving mid-edit
/// cannot move a half-typed snippet: the refill is invisible except for the entry it adds.
/// </remarks>
private void OnTargetVaultsChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (IsEditing)
{
BuildEditorVaultChoices(editorVaultId);
}
}
private void Rebuild()
{
// Captured and restored around the refill, for the reason the host sidebar's rebuild is written the
@@ -2629,10 +2629,19 @@ internal sealed partial class VaultViewModel(
/// What the authentication picker offers: a typed password, then every key, then every credential.
/// </summary>
/// <remarks>
/// Rebuilt when the editor opens rather than kept in step with the two lists. A background sync could pull
/// a new key while a host is being edited, and having the picker's contents change under the user mid-edit
/// is worse than the list being a minute stale — only one editor may be open at a time, so the only way to
/// add a key or a credential is to close this one anyway.
/// <para>
/// Filled when the editor opens and kept in step with the two lists from then on, by
/// <see cref="RefreshOpenEditors"/>. It used to be the snapshot alone, on the grounds that only one editor
/// could be open at a time and so the only way to add a key was to close this one — which stopped being
/// true when <see cref="AHostEditorIsInTheWay"/> was split from <see cref="AVaultEditorIsInTheWay"/>. A
/// host editor now sits open on the Hosts screen while a key is added on the Keychain screen, and a
/// picker that did not notice left the user cancelling an editor to see the key they had just made.
/// </para>
/// <para>
/// What the old note was protecting against is real, and is why the refill restores the selection by id
/// rather than rebuilding from the stored host: entries appear and disappear under the user, but what
/// they have chosen does not move.
/// </para>
/// </remarks>
internal ObservableCollection<AuthenticationChoice> EditorAuthenticationChoices { get; } = [];
@@ -3529,9 +3538,86 @@ internal sealed partial class VaultViewModel(
// After all four lists, because the table is a projection of three of them.
RebuildVaultItems();
// Last of the rebuilds, because every picker an open editor holds is drawn from one of the lists
// above and would otherwise be showing the vault as it was when that editor opened.
RefreshOpenEditors();
await LoadConflictsAsync(cancellationToken).ConfigureAwait(true);
}
/// <summary>
/// Refills the pickers of whichever editor is open, so a key or a vault that has just arrived shows in it.
/// </summary>
/// <remarks>
/// <para>
/// The pickers are snapshots — see <see cref="EditorAuthenticationChoices"/> — and taking one when the
/// editor opens was correct while nothing could change a list without closing it first. Nothing about
/// that holds any more: the host editor lives on the Hosts screen and the keychain's editors live on the
/// Vault screen, so a key, a credential, a tag, a group or a whole vault can be added with a host editor
/// standing open behind it. Every one of them then failed to appear in the picker that exists to offer
/// it, and the only way to see it was to abandon the edit and start again.
/// </para>
/// <para>
/// Called from the one reload rather than from each of the twenty-odd places that write to the vault,
/// which is what makes a sync count as well as a save: a key pulled from another machine reaches the
/// open editor by the same path a key typed here does.
/// </para>
/// <para>
/// <b>Selections are carried across by id, and every typed field is left alone.</b> The refill has to be
/// invisible to somebody halfway through a form — an editor that reset its own bindings because a
/// background sync landed would be a worse bug than the stale list it fixes. So each picker is rebuilt
/// and then put back onto what it was already showing, including the placeholder entries that stand for
/// a binding whose target has gone: it is the same restore-by-id the open path does, from the editor's
/// current selection rather than from the stored item, because for as long as the editor is open the two
/// deliberately differ.
/// </para>
/// </remarks>
private void RefreshOpenEditors()
{
if (IsEditing)
{
// Read before anything is cleared. Rebuilding a bound collection makes the control null its own
// selection and write that back, so by the time the last picker is refilled these properties no
// longer say what the user chose.
var authentication = EditorSelectedAuthentication;
var groupId = EditorSelectedGroup?.EntityId;
BuildTagChoices();
// The same order the two commands that open this editor use: the vault picker first because a
// group belongs to one vault, then the groups, then the bindings — which offer "inherit from
// group" only where the group picker has landed on something.
BuildEditorVaultChoices(editingHostVaultId);
// The selection as it stands, not filtered through GroupInEditingVault: a group that has gone
// keeps its placeholder here for the reason it does on the open path, so that a sync arriving
// mid-edit cannot unfile the host when the form is saved.
BuildGroupChoices(groupId);
BuildAuthenticationChoices(
authentication?.Kind == AuthenticationKind.SshKey ? authentication.EntityId : null,
authentication?.Kind == AuthenticationKind.Credential ? authentication.EntityId : null,
asksForPassword: authentication?.Kind == AuthenticationKind.Typed,
grouped: EditorSelectedGroup?.EntityId is not null);
}
if (IsEditingGroup)
{
var authentication = GroupEditorSelectedAuthentication;
var parentId = GroupEditorSelectedParent?.EntityId;
BuildGroupEditorVaultChoices(GroupEditorVaultId);
// Guid.Empty while creating, which is what EditingGroupId being null means and is the same
// stand-in ClearGroupEditor uses: a group that does not exist yet cannot be its own parent.
BuildGroupParentChoices(EditingGroupId ?? Guid.Empty, parentId);
BuildGroupAuthenticationChoices(
authentication?.Kind == AuthenticationKind.SshKey ? authentication.EntityId : null,
authentication?.Kind == AuthenticationKind.Credential ? authentication.EntityId : null);
}
}
/// <summary>Redraws every list from the vault, without saying anything about it.</summary>
/// <remarks>
/// For the two things that change which vaults exist or which are drawn without going through this type