Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fan every connection-owned key out to its CLI/TUI mode sibling (#7500)

[._.]/ Adam Eivycef6630be4a2
diff --git a/scripts/migrations/391-credential-bootstrap-mode-siblings.test.js b/scripts/migrations/391-credential-bootstrap-mode-siblings.test.js
index a01cfa4778..1d0dd2ab57 100644
--- a/scripts/migrations/391-credential-bootstrap-mode-siblings.test.js
+++ b/scripts/migrations/391-credential-bootstrap-mode-siblings.test.js
@@ -40,13 +40,16 @@ describe('migration 391 — credential bootstrap on mode siblings', () => {
expect(readJson(providersPath)).toEqual(out);
});
- it('leaves a pair alone when both modes name a bootstrap, and when they are not the same connection', async () => {
+ it('leaves a pair alone when the two modes CONTRADICT each other', async () => {
const other = { command: 'other-cli' };
const providers = {
...pair({ cli: { credentialBootstrap: BOOTSTRAP }, tui: { credentialBootstrap: other } }),
- // Same harness, different backend: two connections, not one card.
- split: { id: 'split', type: 'cli', command: 'split', models: [], envVars: { ANTHROPIC_BASE_URL: 'http://127.0.0.1:11434' }, credentialBootstrap: BOOTSTRAP },
- 'split-tui': { id: 'split-tui', type: 'tui', command: 'split', models: [], envVars: {} },
+ // Two backends named outright: two connections, not one card. The
+ // contradiction disqualifies the whole group, so the bootstrap only the
+ // CLI names must not reach the TUI either — a credential entered for one
+ // backend never follows the pair to another (#7500).
+ split: { id: 'split', type: 'cli', command: 'split', models: [], envVars: { EXAMPLE_BASE_URL: 'http://127.0.0.1:11434' }, credentialBootstrap: BOOTSTRAP },
+ 'split-tui': { id: 'split-tui', type: 'tui', command: 'split', models: [], envVars: { EXAMPLE_BASE_URL: 'https://api.example.com' } },
};
writeJson(providersPath, { providers });
@@ -56,6 +59,24 @@ describe('migration 391 — credential bootstrap on mode siblings', () => {
expect(out['split-tui']).not.toHaveProperty('credentialBootstrap');
});
+ it('converges a pair that carries the whole connection on only ONE mode', async () => {
+ // The reserved `<stem>` / `<stem>-tui` id pair IS the declaration that two
+ // records are one harness in two modes, so a sibling carrying nothing is an
+ // INCOMPLETE pair, not a second connection — a second connection gets its
+ // own id. Deciding that for endpoint/apiKey/envVars as well as the
+ // bootstrap is what #7500 settled; a contradiction still blocks the fill.
+ writeJson(providersPath, { providers: pair({ cli: {
+ endpoint: 'https://api.example.com', apiKey: 'k', envVars: { EXAMPLE_BASE_URL: 'https://api.example.com' },
+ } }) });
+
+ expect(await migration.up({ rootDir })).toMatchObject({ ok: true, updated: 1 });
+ const out = readJson(providersPath).providers;
+ expect(out['example-tui']).toMatchObject({
+ endpoint: 'https://api.example.com', apiKey: 'k', envVars: { EXAMPLE_BASE_URL: 'https://api.example.com' },
+ });
+ expect(out['example-tui'].args).toEqual([]);
+ });
+
it('skips an install with no providers file', async () => {
expect(await migration.up({ rootDir })).toMatchObject({ ok: false, reason: 'no-file', updated: 0 });
});
diff --git a/server/lib/aiToolkit/internal/providerModes.js b/server/lib/aiToolkit/internal/providerModes.js
index 9e0a8e71b3..893f6b4295 100644
--- a/server/lib/aiToolkit/internal/providerModes.js
+++ b/server/lib/aiToolkit/internal/providerModes.js
@@ -13,8 +13,27 @@ const MODE_KEY_DEFAULTS = { endpoint: '', apiKey: '', envVars: {}, credentialBoo
* this list, or it mints a pair this function then refuses to group — the exact
* failure a dual-mode create exists to prevent. `command` is checked separately
* (it has no empty-value default).
+ *
+ * DERIVED from `MODE_KEY_DEFAULTS` rather than restated, because the pairing
+ * test and `sharedModeUpdates`' fan-out list are one list seen from two sides:
+ * a key that says "these two describe the same connection" but is NOT fanned
+ * out when it changes splits the card on the very edit that changed it, and
+ * leaves the mode the editor did not open pointed at the old backend. Adding a
+ * key to the defaults above therefore opts it into both.
*/
-export const MODE_GROUPED_KEYS = Object.freeze(['endpoint', 'apiKey', 'envVars', 'credentialBootstrap']);
+export const MODE_GROUPED_KEYS = Object.freeze(Object.keys(MODE_KEY_DEFAULTS));
+
+/** True when a record NAMES a connection-identity value rather than leaving it at the empty default. */
+function namesModeValue(provider, key) {
+ if (key === 'credentialBootstrap') return hasCredentialBootstrap(provider);
+ const value = provider?.[key];
+ if (typeof value === 'string') return value.length > 0;
+ if (value && typeof value === 'object') return Object.keys(value).length > 0;
+ return false;
+}
+
+/** A fanned-out value must not ALIAS one object across two stored records. */
+const detachModeValue = value => (value && typeof value === 'object' ? structuredClone(value) : value);
export function providerModeGroups(providers) {
const byId = new Map(providers.map(provider => [provider.id, provider]));
@@ -24,10 +43,10 @@ export function providerModeGroups(providers) {
const stem = tui.id.replace(/-tui(?=-|$)/, '');
const cli = [byId.get(stem), byId.get(`${stem}-cli`)].find(provider => provider?.type === 'cli');
if (!cli || paired.has(cli.id) || !cli.command || cli.command !== tui.command) continue;
- // `credentialBootstrap` is the connection's auth when there is no apiKey,
- // so siblings naming DIFFERENT ones are two connections, not one. A pair
- // where only one names a bootstrap is the asymmetry `unifyProviderModes`
- // converges, so it does not reach here as a lasting split.
+ // Backend, credential and environment: siblings naming DIFFERENT ones are
+ // two connections, not one. A pair where only one names a value is the
+ // asymmetry `unifyProviderModes` converges, so it does not reach here as a
+ // lasting split.
if (!MODE_GROUPED_KEYS.every(key =>
isDeepStrictEqual(cli[key] || MODE_KEY_DEFAULTS[key], tui[key] || MODE_KEY_DEFAULTS[key]))) continue;
groups.push([cli, tui]);
@@ -40,14 +59,17 @@ export function providerModeGroups(providers) {
export function sharedModeUpdates(updates, sibling) {
// Arguments, timeouts, routing consent and model pins remain mode-specific.
//
- // `credentialBootstrap` is shared for the same reason the pairing test above
- // keys on it: it is the connection's AUTH, not a mode's argv. The editor opens
- // the group's representative (the CLI mode), so a bootstrap saved there and
- // not fanned out leaves the TUI mode — the one "Launch in Shell" launches —
- // spawning a bare harness that falls through to ambient vendor auth, and
- // splits the one card in two. `unifyProviderModes` converges a pair that
- // arrived asymmetric by some other route.
- const shared = Object.fromEntries(['enabled', 'models', 'modelContextWindows', 'credentialBootstrap'].filter(key => Object.hasOwn(updates, key)).map(key => [key, updates[key]]));
+ // Every {@link MODE_GROUPED_KEYS} field is shared for the reason the pairing
+ // test above keys on it: endpoint, API key, env vars and bootstrap describe
+ // the CONNECTION, not a mode's argv. The editor opens the group's
+ // representative (the CLI mode), so any of them saved there and not fanned
+ // out leaves the TUI mode — the one "Launch in Shell" resolves its command
+ // line and env from — pointed at the old backend with the old credential,
+ // and splits the one card in two on the next load. `unifyProviderModes`
+ // converges a pair that arrived asymmetric by some other route.
+ const shared = Object.fromEntries(['enabled', 'models', 'modelContextWindows', ...MODE_GROUPED_KEYS]
+ .filter(key => Object.hasOwn(updates, key))
+ .map(key => [key, detachModeValue(updates[key])]));
// A caller deliberately repicking a default with a new catalog (the editor
// or harness discovery) must repair a removed sibling default too. Ordinary
// catalog probes omit defaultModel and retain their existing pin semantics.
@@ -58,29 +80,38 @@ export function sharedModeUpdates(updates, sibling) {
}
/**
- * Give a mode sibling the credential bootstrap its pair already carries.
+ * Give a mode sibling the connection-identity values its pair already carries.
*
- * Grouped over a projection with `credentialBootstrap` ERASED, because an
- * asymmetric pair is invisible to the real rule — the bootstrap is exactly the
- * value it disagrees on. Only this fill uses the loose grouping; the
- * enablement/model convergence below keeps the strict one.
+ * Grouped over a projection with every {@link MODE_GROUPED_KEYS} field ERASED,
+ * because an asymmetric pair is invisible to the real rule — the unfanned key
+ * is exactly the value it disagrees on, and an edit that changed two of them at
+ * once (an endpoint AND its API key) disagrees on two. Only this fill uses the
+ * loose grouping; the enablement/model convergence below keeps the strict one.
*
- * Exactly one named bootstrap is repaired. Two different ones are a deliberate
- * configuration (two connections), and picking a winner between two credentials
- * is nobody's call to make silently.
+ * Only a pair that is INCOMPLETE is repaired, never one that CONTRADICTS: a key
+ * two modes both name with different values is a deliberate configuration (two
+ * connections that happen to share a command), and picking a winner between two
+ * endpoints or two credentials is nobody's call to make silently. One
+ * contradiction disqualifies the whole group rather than just that key —
+ * filling an API key across a pair that already points at two different
+ * backends would send the credential somewhere it was never entered for.
*/
-function fillModeSiblingBootstrap(providers) {
+function fillModeSiblingIdentity(providers) {
const byId = new Map(providers.map(provider => [provider.id, provider]));
+ const erased = Object.fromEntries(MODE_GROUPED_KEYS.map(key => [key, undefined]));
let changed = false;
- for (const group of providerModeGroups(providers.map(provider => ({ ...provider, credentialBootstrap: undefined })))) {
+ for (const group of providerModeGroups(providers.map(provider => ({ ...provider, ...erased })))) {
if (group.length < 2) continue;
const modes = group.map(({ id }) => byId.get(id)).filter(Boolean);
- const named = modes.filter(hasCredentialBootstrap);
- if (named.length !== 1) continue;
- for (const mode of modes) {
- if (hasCredentialBootstrap(mode)) continue;
- mode.credentialBootstrap = structuredClone(named[0].credentialBootstrap);
- changed = true;
+ const namers = MODE_GROUPED_KEYS.map(key => [key, modes.filter(mode => namesModeValue(mode, key))]);
+ if (namers.some(([key, named]) => named.some(mode => !isDeepStrictEqual(mode[key], named[0][key])))) continue;
+ for (const [key, named] of namers) {
+ if (named.length !== 1) continue;
+ for (const mode of modes) {
+ if (namesModeValue(mode, key)) continue;
+ mode[key] = detachModeValue(named[0][key]);
+ changed = true;
+ }
}
}
return changed;
@@ -88,9 +119,9 @@ function fillModeSiblingBootstrap(providers) {
export function unifyProviderModes(data) {
const providers = Object.values(data.providers || {});
- // Bootstrap FIRST: a pair split only by an unfanned bootstrap is rejoined
- // here, so the convergence below sees the one connection it actually is.
- let changed = fillModeSiblingBootstrap(providers);
+ // Identity FIRST: a pair split only by an unfanned connection field is
+ // rejoined here, so the convergence below sees the one connection it is.
+ let changed = fillModeSiblingIdentity(providers);
for (const group of providerModeGroups(providers)) {
if (group.length < 2) continue;
const enabled = group.some(provider => provider.enabled === true);
diff --git a/server/lib/aiToolkit/providers.js b/server/lib/aiToolkit/providers.js
index 2a61db9d66..6ed482c8a0 100644
--- a/server/lib/aiToolkit/providers.js
+++ b/server/lib/aiToolkit/providers.js
@@ -847,10 +847,11 @@ export function createProviderService(config = {}) {
};
dropClearedBootstrap(provider);
- // Grouped BEFORE the edit lands: a bootstrap set on one mode is exactly
- // the value `providerModeGroups` pairs on, so reading the group after
- // would find the siblings already split and skip the fan-out that keeps
- // them together.
+ // Grouped BEFORE the edit lands: every connection-identity value the
+ // fan-out shares (endpoint, API key, env vars, bootstrap) is exactly what
+ // `providerModeGroups` pairs on, so reading the group after would find
+ // the siblings already split and skip the fan-out that keeps them
+ // together.
const group = providerModeGroups(Object.values(data.providers)).find(modes => modes.some(mode => mode.id === id));
data.providers[id] = provider;
for (const sibling of group || []) {
diff --git a/server/lib/aiToolkit/providers.test.js b/server/lib/aiToolkit/providers.test.js
index 5cebf906f8..00e33b9b14 100644
--- a/server/lib/aiToolkit/providers.test.js
+++ b/server/lib/aiToolkit/providers.test.js
@@ -142,6 +142,46 @@ describe('Provider Service', () => {
expect(await providerService.getProviderById('example-tui')).not.toHaveProperty('credentialBootstrap');
});
+ it('fans the whole connection — endpoint, key and env — out to the mode sibling', async () => {
+ // The editor opens the group's REPRESENTATIVE (the CLI mode) and edits the
+ // connection there. Before the fan-out covered these three, the save left
+ // the TUI record — the one "Launch in Shell" resolves its command line and
+ // env from — on the old backend with the old key, and the disagreement
+ // split the one card in two on the next load.
+ await writeFile(join(TEST_DATA_DIR, 'providers.json'), JSON.stringify({ activeProvider: 'example', providers: {
+ example: { id: 'example', name: 'Example CLI', type: 'cli', command: 'example', enabled: true, models: ['a'], args: ['--print'], endpoint: 'https://old.example.com', apiKey: 'old-key', envVars: { BASE: 'old' } },
+ 'example-tui': { id: 'example-tui', name: 'Example TUI', type: 'tui', command: 'example', enabled: true, models: ['a'], args: [], endpoint: 'https://old.example.com', apiKey: 'old-key', envVars: { BASE: 'old' } },
+ } }));
+ await providerService.updateProvider('example', {
+ endpoint: 'https://new.example.com', apiKey: 'new-key', envVars: { BASE: 'new' },
+ });
+ const sibling = await providerService.getProviderById('example-tui');
+ expect(sibling).toMatchObject({ endpoint: 'https://new.example.com', apiKey: 'new-key', envVars: { BASE: 'new' } });
+ // Mode-specific argv still never crosses over.
+ expect(sibling.args).toEqual([]);
+ // Still ONE card after the edit: a fan-out that missed a pairing key would
+ // leave the two disagreeing, and this catalog change would stop crossing.
+ await providerService.updateProvider('example-tui', { models: ['b'] });
+ expect((await providerService.getProviderById('example')).models).toEqual(['b']);
+ });
+
+ it('converges a pair whose connection landed on only ONE mode, and refuses when the two CONTRADICT', async () => {
+ // An install that edited a unified card before the fan-out covered these
+ // keys still carries the split; so does a hand-edited file or a restored
+ // backup. The load-time repair fills what only one mode names — including
+ // the two-key case a single-key projection could never see.
+ await writeFile(join(TEST_DATA_DIR, 'providers.json'), JSON.stringify({ activeProvider: 'example', providers: {
+ example: { id: 'example', name: 'Example CLI', type: 'cli', command: 'example', enabled: true, models: ['a'], endpoint: 'https://api.example.com', apiKey: 'k' },
+ 'example-tui': { id: 'example-tui', name: 'Example TUI', type: 'tui', command: 'example', enabled: true, models: ['a'] },
+ // Two connections that happen to share a command: they CONTRADICT on the
+ // endpoint, so the key one of them names must not be handed to the other.
+ other: { id: 'other', name: 'Other CLI', type: 'cli', command: 'other', enabled: true, models: ['a'], endpoint: 'https://one.example.com', apiKey: 'secret' },
+ 'other-tui': { id: 'other-tui', name: 'Other TUI', type: 'tui', command: 'other', enabled: true, models: ['a'], endpoint: 'https://two.example.com' },
+ } }));
+ expect(await providerService.getProviderById('example-tui')).toMatchObject({ endpoint: 'https://api.example.com', apiKey: 'k' });
+ expect(await providerService.getProviderById('other-tui')).not.toHaveProperty('apiKey');
+ });
+
it.skipIf(process.platform === 'win32')('refreshes Pi models and distinguishes authentication from probe failure', async () => {
const command = join(TEST_DATA_DIR, 'pi');
const emit = async (text, code = 0) => {