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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
refactor: share preset re-derivation and narrowing between the service fold and its callers
[._.]/ Adam Eivy64caee8e196a
diff --git a/server/lib/providerPresets.js b/server/lib/providerPresets.jsindex 677a392809..8463ae3d5a 100644--- a/server/lib/providerPresets.js+++ b/server/lib/providerPresets.js@@ -1,7 +1,7 @@import { isDeepStrictEqual } from 'node:util';import { isDerivedPreset } from './providerGraphRecords.js';import { providerConnectionProfile, withoutConnectionOwnedFields } from './providerConnections.js';-import { harnessForProvider } from './providerHarnesses.js';+import { harnessById, harnessForProvider } from './providerHarnesses.js';import { parseOpencodeConfigContent } from './providerModels.js';import { BACKEND_MARKER_KEYS, materializeRouteOutcome } from './providerRouteRecipes.js';import { applyServicePlanFilter, instanceForConnection } from './providerServiceInstances.js';@@ -356,6 +356,39 @@ export function derivedPresetPatch(record, derived) {return patch;}+/**+ * The `catalogNarrowing` that keeps a record on the model list it already+ * runs once it is derived from a service listing `listed`: its own list, when+ * that is a strict, fully listed subset; `null` when no narrowing is needed or+ * none would hold. Shared by the conversion backfill and the service fold.+ */+export function impliedCatalogNarrowing(record, listed) {+ const models = Array.isArray(record.models) ? record.models : [];+ return listed.length > 0 && !isDeepStrictEqual(listed, models) && models.every((model) => listed.includes(model))+ ? [...models]+ : null;+}++/**+ * Re-derive one DERIVED preset from `instance`: the materialized record and the+ * patch that writes it, or `null` when it cannot be derived there (an unknown+ * harness, a missing bootstrap app, a refused composition).+ *+ * @param {object} record - a derived preset, possibly with its structural keys already retargeted+ * @param {{instance: object, catalog: object|null, bootstraps: Record<string, object>, stored?: object}} context+ * `stored` is the record as persisted, which the patch is taken against (defaults to `record`)+ * @returns {{derived: object, patch: object}|null}+ */+export function rederivePreset(record, { instance, catalog, bootstraps, stored = record }) {+ const harness = harnessById(record.harnessId);+ const app = record.credentialBootstrapId ? bootstraps[record.credentialBootstrapId] : null;+ if (!harness || (record.credentialBootstrapId && !app)) return null;+ const { record: derived } = materializeDerivedPreset({+ record, harness, instance, catalog, bootstrap: app ? bootstrapInputFor(record.credentialBootstrapId, app) : null,+ });+ return derived ? { derived, patch: derivedPresetPatch(stored, derived) } : null;+}+/*** The backfill verdict for ONE legacy record already resolved onto the graph* — the additive structural patch when re-deriving it from `connection` is a@@ -397,11 +430,8 @@ export function presetBackfillVerdict(record, { route, connection, instance, booserviceId: connection.slug,...(match ? { credentialBootstrapId: match.slug } : {}),};- const listed = listedModels(instance, connection.catalog);- const models = Array.isArray(record.models) ? record.models : [];- if (listed.length > 0 && !isDeepStrictEqual(listed, models) && models.every((model) => listed.includes(model))) {- structural.catalogNarrowing = [...models];- }+ const narrowing = impliedCatalogNarrowing(record, listedModels(instance, connection.catalog));+ if (narrowing) structural.catalogNarrowing = narrowing;const { record: derived, error } = materializeDerivedPreset({record: { ...record, ...structural },diff --git a/server/lib/providerServiceMerge.js b/server/lib/providerServiceMerge.jsindex 6b3db785dc..d78f799204 100644--- a/server/lib/providerServiceMerge.js+++ b/server/lib/providerServiceMerge.js@@ -1,8 +1,7 @@import { isDeepStrictEqual } from 'node:util';import { providerConnectionProfile } from './providerConnections.js';import { isDerivedPreset, routeBelongsOnConnection } from './providerGraphRecords.js';-import { harnessById } from './providerHarnesses.js';-import { bootstrapInputFor, derivedPresetPatch, listedModels, materializeDerivedPreset } from './providerPresets.js';+import { impliedCatalogNarrowing, listedModels, rederivePreset } from './providerPresets.js';import { instanceApiKeyFor, instanceForConnection } from './providerServiceInstances.js';import { SERVICE_DEFINITIONS, serviceDefinitionById } from './serviceDefinitions.js';@@ -118,55 +117,54 @@ export function planServiceInstanceMerges(graph, providers, { bootstraps = {}, econst records = new Map((Array.isArray(providers) ? providers : []).filter((record) => record && typeof record === 'object' && record.id).map((record) => [record.id, record]));- const bindingsOf = (connectionId) => graph.bindings.filter((binding) => binding.connectionId === connectionId);- const routesOf = (connectionId) => {- const ids = new Set(bindingsOf(connectionId).map((binding) => binding.id));- return graph.routes.filter((route) => ids.has(route.bindingId));- };-const groups = new Map();for (const connection of graph.connections) {if (!connection.slug || !connection.definitionId || Object.keys(connection.transports || {}).length === 0) continue;- const routes = routesOf(connection.id);+ const bindings = graph.bindings.filter((binding) => binding.connectionId === connection.id);+ const bindingIds = new Set(bindings.map((binding) => binding.id));+ const routes = graph.routes.filter((route) => bindingIds.has(route.bindingId));if (routes.some((route) => route.pending || !records.has(route.providerId))) continue;- const definitionId = effectiveDefinitionId(connection);- if (!serviceDefinitionById(definitionId)) continue;- groups.set(definitionId, [...(groups.get(definitionId) || []), { connection, routes }]);+ const definition = serviceDefinitionById(effectiveDefinitionId(connection));+ if (!definition) continue;+ const group = groups.get(definition.id) || { definition, members: [] };+ group.members.push({ connection, bindings, routes });+ groups.set(definition.id, group);}const merges = [];- for (const [definitionId, members] of groups) {+ for (const { definition, members } of groups.values()) {if (members.length < 2) continue;- const definition = serviceDefinitionById(definitionId);+ const definitionId = definition.id;// The survivor keeps its id, kind and slug, so prefer the row that already// IS the named definition, then the one addressed by the definition's own// id, then the one labelled as the service; ties keep table order.const rank = ({ connection }) => (connection.definitionId === definitionId ? 0 : 4)+ (connection.slug === definitionId ? 0 : 2)+ (connection.label === definition.label ? 0 : 1);- const ordered = [...members].sort((a, b) => rank(a) - rank(b));- const remaining = [...ordered];+ const remaining = [...members].sort((a, b) => rank(a) - rank(b));while (remaining.length > 1) {const lead = remaining.shift();let merged = { ...lead.connection };+ let presetPatches = {};const absorbed = [];for (const member of [...remaining]) {- if (bindingsOf(member.connection.id).length === 0) continue;+ if (member.bindings.length === 0) continue;const next = foldInto(merged, member.connection, definition);if (!next) continue;const legacy = member.routes.filter((route) => !isDerivedPreset(records.get(route.providerId)));if (!legacy.every((route) => routeBelongsOnConnection(providerConnectionProfile(records.get(route.providerId)), next))) continue;+ // The label is settled below; nothing a preset derives reads it.const onRow = [lead, ...absorbed, member].flatMap(({ routes }) => routes).map((route) => records.get(route.providerId));- if (!presetPatchesForMerge(next, onRow, bootstraps, env)) continue;+ const patches = presetPatchesForMerge(next, onRow, bootstraps, env);+ if (!patches) continue;merged = next;+ presetPatches = patches;absorbed.push(member);remaining.splice(remaining.indexOf(member), 1);}if (absorbed.length === 0) continue;- merged.label = serviceLabel([lead, ...absorbed].map(({ connection }) => connection), definition, graph.bindings);- const onKeeper = [lead, ...absorbed].flatMap(({ routes }) => routes).map((route) => records.get(route.providerId));- const presetPatches = presetPatchesForMerge(merged, onKeeper, bootstraps, env);- if (presetPatches) merges.push({ ...planMoves(graph, merged, absorbed), keeper: merged, presetPatches });+ merged.label = serviceLabel([lead, ...absorbed], definition);+ merges.push({ ...planMoves(lead, absorbed), keeper: merged, presetPatches });}}return merges;@@ -178,21 +176,19 @@ export function planServiceInstanceMerges(graph, providers, { bootstraps = {}, e* NIM") — wrong once the row serves several. Prefer a member already named as* the service, then a direct-API member's name, then the definition's own.*/-function serviceLabel(rows, definition, bindings) {- const named = rows.find((row) => row.label === definition.label);- if (named) return named.label;- const direct = rows.find((row) => bindings.some((binding) => binding.connectionId === row.id && binding.harnessId == null));- return direct?.label || definition.label;+function serviceLabel(members, definition) {+ if (members.some(({ connection }) => connection.label === definition.label)) return definition.label;+ const direct = members.find(({ bindings }) => bindings.some((binding) => binding.harnessId == null));+ return direct?.connection.label || definition.label;}/** Binding moves onto the survivor, each on a variant key free on it: `UNIQUE(connection, harness, variant)`. */-function planMoves(graph, keeper, absorbed) {+function planMoves(lead, absorbed) {const key = (harnessId, variantKey) => JSON.stringify([harnessId ?? null, variantKey]);- const taken = new Set(graph.bindings.filter((binding) => binding.connectionId === keeper.id)- .map((binding) => key(binding.harnessId, binding.variantKey)));+ const taken = new Set(lead.bindings.map((binding) => key(binding.harnessId, binding.variantKey)));const bindingMoves = [];- for (const { connection } of absorbed) {- for (const binding of graph.bindings.filter((candidate) => candidate.connectionId === connection.id)) {+ for (const { bindings } of absorbed) {+ for (const binding of bindings) {const variantKey = taken.has(key(binding.harnessId, binding.variantKey)) ? `variant:${binding.id}` : binding.variantKey;taken.add(key(binding.harnessId, variantKey));bindingMoves.push({ bindingId: binding.id, variantKey });@@ -205,7 +201,6 @@ function planMoves(graph, keeper, absorbed) {};}-/*** The `providers.json` patches that put every DERIVED preset among `records`* on the fold's survivor: its `serviceId` becomes the survivor's slug and@@ -223,10 +218,10 @@ function planMoves(graph, keeper, absorbed) {* @param {object} keeper - the survivor as `planServiceInstanceMerges` returned it* @param {object[]} records - the executable records on it (legacy ones are skipped)* @param {Record<string, object>} bootstraps - the configured bootstrap apps- * @param {Record<string, string|undefined>} [env]+ * @param {Record<string, string|undefined>} env* @returns {Record<string, object>|null}*/-function presetPatchesForMerge(keeper, records, bootstraps, env = process.env) {+function presetPatchesForMerge(keeper, records, bootstraps, env) {const derived = records.filter(isDerivedPreset);if (derived.length === 0) return {};const instance = instanceForConnection(keeper, env);@@ -234,23 +229,14 @@ function presetPatchesForMerge(keeper, records, bootstraps, env = process.env) {const listed = listedModels(instance, keeper.catalog);const patches = {};for (const record of derived) {- const harness = harnessById(record.harnessId);- const app = record.credentialBootstrapId ? bootstraps[record.credentialBootstrapId] : null;- if (!harness || (record.credentialBootstrapId && !app)) return null;const next = { ...record, serviceId: keeper.slug };- const models = Array.isArray(record.models) ? record.models : [];- if (!Array.isArray(record.catalogNarrowing) && listed.length > 0 && !isDeepStrictEqual(listed, models)- && models.every((model) => listed.includes(model))) {- next.catalogNarrowing = [...models];- }- const { record: materialized, error } = materializeDerivedPreset({- record: next, harness, instance, catalog: keeper.catalog, bootstrap: app ? bootstrapInputFor(record.credentialBootstrapId, app) : null,- });+ const narrowing = Array.isArray(record.catalogNarrowing) ? null : impliedCatalogNarrowing(record, listed);+ if (narrowing) next.catalogNarrowing = narrowing;+ const result = rederivePreset(next, { instance, catalog: keeper.catalog, bootstraps, stored: record });// A fold must not change what a preset runs: a list the survivor's plan// would cut (a paid-model preset onto a free-plan instance) refuses it.- if (error || !isDeepStrictEqual(materialized.models, models)) return null;- const patch = derivedPresetPatch(record, materialized);- if (Object.keys(patch).length > 0) patches[record.id] = patch;+ if (!result || !isDeepStrictEqual(result.derived.models, Array.isArray(record.models) ? record.models : [])) return null;+ if (Object.keys(result.patch).length > 0) patches[record.id] = result.patch;}return patches;}diff --git a/server/services/providerGraph.js b/server/services/providerGraph.jsindex 0bcead2b2a..e38c10ff01 100644--- a/server/services/providerGraph.js+++ b/server/services/providerGraph.js@@ -28,11 +28,9 @@ import {import { harnessById } from '../lib/providerHarnesses.js';import { modeSiblingName } from '../lib/aiToolkit/internal/providerModes.js';import {- bootstrapInputFor,- derivedPresetPatch,- materializeDerivedPreset,planPresetBackfill,presetBackfillVerdict,+ rederivePreset,} from '../lib/providerPresets.js';import { effortLevelsForProvider } from '../lib/providerModels.js';import {@@ -325,16 +323,8 @@ export async function rematerializeDerivedPresets(connection, { providerIds = nuconst patches = {};for (const record of providers) {if (!isDerivedPreset(record) || !onRow(record)) continue;- const harness = harnessById(record.harnessId);- if (!harness) continue;- const app = record.credentialBootstrapId ? bootstraps[record.credentialBootstrapId] : null;- if (record.credentialBootstrapId && !app) continue;- const { record: derived } = materializeDerivedPreset({- record, harness, instance, catalog: connection.catalog, bootstrap: app ? bootstrapInputFor(record.credentialBootstrapId, app) : null,- });- if (!derived) continue;- const patch = derivedPresetPatch(record, derived);- if (Object.keys(patch).length > 0) patches[record.id] = patch;+ const patch = rederivePreset(record, { instance, catalog: connection.catalog, bootstraps })?.patch;+ if (patch && Object.keys(patch).length > 0) patches[record.id] = patch;}if (Object.keys(patches).length === 0) return [];const written = await writeProviderPatches(patches);