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

fix: remove retired NVIDIA Kimi service instances

[._.]/ Adam Eivyef77fc9ed7de
diff --git a/server/scripts/db-migrations/010-retire-nvidia-kimi-service-instance.js b/server/scripts/db-migrations/010-retire-nvidia-kimi-service-instance.js
new file mode 100644
index 0000000000..b88be78e66
--- /dev/null
+++ b/server/scripts/db-migrations/010-retire-nvidia-kimi-service-instance.js
@@ -0,0 +1,106 @@
+/**
+ * Remove the orphaned NVIDIA Kimi service instance after its shipped preset
+ * was retired by file migration 402. A matching provider in providers.json or
+ * any saved route that still exists in that file keeps the instance intact.
+ *
+ * This is DB-side because `ai_connections` is machine-local Postgres state;
+ * the legacy file migration cannot remove it. The installed provider document
+ * is the input gate, and the migration deliberately ships no data seed.
+ */
+
+import { readFile } from 'node:fs/promises';
+import { join } from 'node:path';
+import { isPlainObject } from '../../lib/objects.js';
+import { PATHS } from '../../lib/paths.js';
+
+const DEFAULT_PROVIDERS_PATH = join(PATHS.data, 'providers.json');
+const RETIRED_SERVICE = Object.freeze({
+ slug: 'nvidia-kimi',
+ definitionId: 'nvidia-nim',
+ kind: 'gateway:nvidia-nim',
+});
+
+async function readProviders(providersPath) {
+ let contents;
+ try {
+ contents = await readFile(providersPath, 'utf8');
+ } catch (err) {
+ if (err.code === 'ENOENT') return null;
+ throw new Error('Cannot read data/providers.json for NVIDIA Kimi service retirement');
+ }
+
+ let document;
+ try {
+ document = JSON.parse(contents);
+ } catch {
+ throw new Error('Cannot parse data/providers.json for NVIDIA Kimi service retirement');
+ }
+ if (!isPlainObject(document) || !isPlainObject(document.providers)) {
+ throw new Error('data/providers.json has no valid providers object for NVIDIA Kimi service retirement');
+ }
+ return document.providers;
+}
+
+const providerStillUsesService = (providers) => (
+ Object.hasOwn(providers, RETIRED_SERVICE.slug)
+ || Object.values(providers).some((provider) => provider?.serviceId === RETIRED_SERVICE.slug)
+);
+
+/**
+ * Retire only an unused instance from the old shipped composition. Other
+ * providers may have been explicitly linked onto this service, so routes
+ * whose ids still exist in the provider document preserve the whole instance.
+ */
+export async function up(client, { providersPath = DEFAULT_PROVIDERS_PATH } = {}) {
+ const providers = await readProviders(providersPath);
+ if (!providers) {
+ console.log('🧹 NVIDIA Kimi service retirement: provider file absent, skipped');
+ return { removed: 0, skipped: 'provider-file-absent' };
+ }
+ if (providerStillUsesService(providers)) {
+ return { removed: 0, skipped: 'provider-still-configured' };
+ }
+
+ const { rows: candidates } = await client.query(
+ `SELECT id
+ FROM ai_connections
+ WHERE slug = $1 AND definition_id = $2 AND kind = $3
+ FOR UPDATE`,
+ [RETIRED_SERVICE.slug, RETIRED_SERVICE.definitionId, RETIRED_SERVICE.kind],
+ );
+
+ let removed = 0;
+ for (const { id } of candidates) {
+ const { rows: routes } = await client.query(
+ `SELECT route.provider_id
+ FROM ai_route_bindings AS route
+ JOIN ai_harness_bindings AS binding ON binding.id = route.binding_id
+ WHERE binding.connection_id = $1
+ FOR UPDATE OF route, binding`,
+ [id],
+ );
+ if (routes.some(({ provider_id }) => Object.hasOwn(providers, provider_id))) continue;
+
+ await client.query(
+ `DELETE FROM ai_route_bindings AS route
+ USING ai_harness_bindings AS binding
+ WHERE route.binding_id = binding.id AND binding.connection_id = $1`,
+ [id],
+ );
+ await client.query('DELETE FROM ai_harness_bindings WHERE connection_id = $1', [id]);
+ const deleted = await client.query(
+ `DELETE FROM ai_connections
+ WHERE id = $1 AND slug = $2 AND definition_id = $3 AND kind = $4
+ RETURNING id`,
+ [id, RETIRED_SERVICE.slug, RETIRED_SERVICE.definitionId, RETIRED_SERVICE.kind],
+ );
+ removed += deleted.rows.length;
+ }
+
+ if (removed > 0) {
+ console.log(`🧹 NVIDIA Kimi service retirement: removed ${removed} unused service instance${removed === 1 ? '' : 's'}`);
+ }
+ return { removed, skipped: null };
+}
+
+export default { up };
diff --git a/server/scripts/db-migrations/010-retire-nvidia-kimi-service-instance.test.js b/server/scripts/db-migrations/010-retire-nvidia-kimi-service-instance.test.js
new file mode 100644
index 0000000000..18792503c6
--- /dev/null
+++ b/server/scripts/db-migrations/010-retire-nvidia-kimi-service-instance.test.js
@@ -0,0 +1,94 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { mkdtemp, rm, writeFile } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+import { up } from './010-retire-nvidia-kimi-service-instance.js';
+
+describe('DB migration 010 — retire NVIDIA Kimi service instance', () => {
+ let tempDir;
+
+ afterEach(async () => {
+ if (tempDir) await rm(tempDir, { recursive: true, force: true });
+ tempDir = null;
+ });
+
+ const writeProviders = async (providers) => {
+ tempDir = await mkdtemp(join(tmpdir(), 'db-migration-nvidia-kimi-'));
+ const providersPath = join(tempDir, 'providers.json');
+ await writeFile(providersPath, `${JSON.stringify({ providers })}\n`);
+ return providersPath;
+ };
+
+ const clientFor = ({ candidates = [{ id: 'connection-example' }], routes = [] } = {}) => {
+ const query = vi.fn(async (sql) => {
+ if (sql.includes('FROM ai_connections')) return { rows: candidates };
+ if (sql.includes('FROM ai_route_bindings')) return { rows: routes };
+ if (sql.includes('DELETE FROM ai_connections')) return { rows: candidates.map(({ id }) => ({ id })) };
+ return { rows: [] };
+ });
+ return { query };
+ };
+
+ it('deletes the retired instance and its orphaned routes and bindings', async () => {
+ const providersPath = await writeProviders({ 'nvidia-nim': { id: 'nvidia-nim' } });
+ const client = clientFor({ routes: [{ provider_id: 'nvidia-kimi' }] });
+
+ await expect(up(client, { providersPath })).resolves.toEqual({ removed: 1, skipped: null });
+
+ expect(client.query).toHaveBeenCalledTimes(5);
+ expect(client.query).toHaveBeenNthCalledWith(1,
+ expect.stringContaining('FROM ai_connections'), ['nvidia-kimi', 'nvidia-nim', 'gateway:nvidia-nim']);
+ expect(client.query).toHaveBeenNthCalledWith(2,
+ expect.stringContaining('FOR UPDATE OF route, binding'), ['connection-example']);
+ expect(client.query).toHaveBeenNthCalledWith(3,
+ expect.stringContaining('DELETE FROM ai_route_bindings'), ['connection-example']);
+ expect(client.query).toHaveBeenNthCalledWith(4,
+ expect.stringContaining('DELETE FROM ai_harness_bindings'), ['connection-example']);
+ expect(client.query).toHaveBeenNthCalledWith(5,
+ expect.stringContaining('DELETE FROM ai_connections'), ['connection-example', 'nvidia-kimi', 'nvidia-nim', 'gateway:nvidia-nim']);
+ });
+
+ it('preserves the service when the retired preset remains configured', async () => {
+ const providersPath = await writeProviders({ 'nvidia-kimi': { id: 'nvidia-kimi' } });
+ const client = clientFor();
+
+ await expect(up(client, { providersPath })).resolves.toEqual({ removed: 0, skipped: 'provider-still-configured' });
+ expect(client.query).not.toHaveBeenCalled();
+ });
+
+ it('preserves a service named by a customized preset even before its route is imported', async () => {
+ const providersPath = await writeProviders({ 'custom-nvidia-route': { serviceId: 'nvidia-kimi' } });
+ const client = clientFor();
+
+ await expect(up(client, { providersPath })).resolves.toEqual({ removed: 0, skipped: 'provider-still-configured' });
+ expect(client.query).not.toHaveBeenCalled();
+ });
+
+ it('preserves a service used by another configured route', async () => {
+ const providersPath = await writeProviders({ 'opencode-nvidia-nim': { id: 'opencode-nvidia-nim' } });
+ const client = clientFor({ routes: [{ provider_id: 'opencode-nvidia-nim' }] });
+
+ await expect(up(client, { providersPath })).resolves.toEqual({ removed: 0, skipped: null });
+ expect(client.query.mock.calls.map(([sql]) => sql)).toHaveLength(2);
+ });
+
+ it('does not retire rows when the installed provider file is absent', async () => {
+ tempDir = await mkdtemp(join(tmpdir(), 'db-migration-nvidia-kimi-'));
+ const client = clientFor();
+
+ await expect(up(client, { providersPath: join(tempDir, 'missing.json') }))
+ .resolves.toEqual({ removed: 0, skipped: 'provider-file-absent' });
+ expect(client.query).not.toHaveBeenCalled();
+ });
+
+ it('fails safely on malformed provider configuration', async () => {
+ tempDir = await mkdtemp(join(tmpdir(), 'db-migration-nvidia-kimi-'));
+ const providersPath = join(tempDir, 'providers.json');
+ await writeFile(providersPath, '{invalid');
+ const client = clientFor();
+
+ await expect(up(client, { providersPath })).rejects.toThrow(/Cannot parse data\/providers\.json/);
+ expect(client.query).not.toHaveBeenCalled();
+ });
+});