coder(providers): v2.3 provider-lifecycle phase 2 — snapshot lifecycle
provider-snapshot no longer returns null for uninstalled/disabled providers: it emits one entry per registered provider with a lifecycle status (loading|ready|unavailable|error), an enabled flag, and a two-tier probe. Tier-1 is a fast which-style check (command-availability.ts, execFile/no-shell); tier-2 (cold ACP probe) is skipped unless forced, last_probed_at is older than PROVIDER_PROBE_TTL_MS (24h), or DB models are empty — the snapshot-latency win. Cache miss returns status:'loading' synchronously while the build settles via the existing inflight promise. ProviderSnapshotStatus/Entry regain loading/unavailable + gain enabled/description?/fetchedAt? in both coder and web copies, guarded by a runtime parity test (provider-types-parity.test.ts; compile-time cross-project check was blocked by TS6307). Also tracks the data/coder-providers.json seed via a .gitignore exception, completing the Phase 1 config file. No dispatch/route/UI changes (Phase 3+); AgentComposerBar filtering unchanged. 13 snapshot tests (+6) + 6 parity tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import {
|
||||
mergeModels,
|
||||
prefixLlamaSwapModels,
|
||||
clearProviderSnapshotCache,
|
||||
getProviderSnapshot,
|
||||
} from '../provider-snapshot.js';
|
||||
import { loadProviderConfig } from '../provider-config-registry.js';
|
||||
|
||||
vi.mock('../acp-probe.js', () => ({
|
||||
probeAcpProvider: vi.fn(),
|
||||
@@ -14,6 +18,13 @@ import { probeAcpProvider } from '../acp-probe.js';
|
||||
|
||||
const mockProbe = vi.mocked(probeAcpProvider);
|
||||
|
||||
/** Write a temp coder-providers.json and point the resolved registry at it. */
|
||||
function loadConfigFixture(providers: Record<string, unknown>): void {
|
||||
const path = join(tmpdir(), `coder-providers-test-${providers ? Object.keys(providers).join('-') || 'empty' : 'empty'}.json`);
|
||||
writeFileSync(path, JSON.stringify({ providers }), 'utf8');
|
||||
loadProviderConfig(path);
|
||||
}
|
||||
|
||||
function mockSql(agents: Array<{
|
||||
name: string;
|
||||
install_path: string | null;
|
||||
@@ -21,6 +32,7 @@ function mockSql(agents: Array<{
|
||||
models: Array<{ id: string; label: string }> | null;
|
||||
label: string | null;
|
||||
transport: string | null;
|
||||
last_probed_at?: string | null;
|
||||
}>) {
|
||||
return vi.fn((strings: TemplateStringsArray) => {
|
||||
const query = strings.join('');
|
||||
@@ -36,6 +48,7 @@ function mockSql(agents: Array<{
|
||||
|
||||
const config = {
|
||||
LLAMA_SWAP_URL: 'http://llama-swap.test',
|
||||
PROVIDER_PROBE_TTL_MS: 86_400_000,
|
||||
} as import('../config.js').Config;
|
||||
|
||||
describe('prefixLlamaSwapModels', () => {
|
||||
@@ -68,6 +81,8 @@ describe('mergeModels', () => {
|
||||
describe('getProviderSnapshot', () => {
|
||||
beforeEach(() => {
|
||||
clearProviderSnapshotCache();
|
||||
// Reset the resolved registry to built-ins-only (missing path → {} config).
|
||||
loadProviderConfig('/nonexistent-coder-providers.json');
|
||||
vi.restoreAllMocks();
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
@@ -165,4 +180,147 @@ describe('getProviderSnapshot', () => {
|
||||
expect(claude?.modes.length).toBeGreaterThan(0);
|
||||
expect(claude?.commands.some((c) => c.name === 'help')).toBe(true);
|
||||
});
|
||||
|
||||
it('disabled provider → unavailable + enabled:false, WITHOUT spawning a probe', async () => {
|
||||
loadConfigFixture({ goose: { enabled: false } });
|
||||
mockProbe.mockResolvedValue({ ok: true, models: [], modes: [], defaultModeId: null, commands: [] });
|
||||
|
||||
const sql = mockSql([
|
||||
{
|
||||
name: 'goose',
|
||||
install_path: '/usr/bin/goose',
|
||||
supports_acp: true,
|
||||
models: [{ id: 'g1', label: 'G1' }],
|
||||
label: 'Goose',
|
||||
transport: 'acp',
|
||||
last_probed_at: new Date().toISOString(),
|
||||
},
|
||||
]);
|
||||
|
||||
const entries = await getProviderSnapshot(sql, config, '/tmp/project', true);
|
||||
const goose = entries.find((e) => e.name === 'goose');
|
||||
|
||||
expect(goose?.status).toBe('unavailable');
|
||||
expect(goose?.enabled).toBe(false);
|
||||
expect(goose?.installed).toBe(false);
|
||||
expect(mockProbe).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uninstalled provider → unavailable + enabled:true + installed:false', async () => {
|
||||
loadConfigFixture({});
|
||||
mockProbe.mockResolvedValue({ ok: true, models: [], modes: [], defaultModeId: null, commands: [] });
|
||||
|
||||
const sql = mockSql([]); // nothing probed/installed
|
||||
const entries = await getProviderSnapshot(sql, config, '/tmp/project', true);
|
||||
const opencode = entries.find((e) => e.name === 'opencode');
|
||||
|
||||
expect(opencode?.status).toBe('unavailable');
|
||||
expect(opencode?.enabled).toBe(true);
|
||||
expect(opencode?.installed).toBe(false);
|
||||
expect(mockProbe).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('fresh DB within TTL → tier-2 cold probe SKIPPED (serves DB models)', async () => {
|
||||
loadConfigFixture({});
|
||||
// If this were wrongly called, cached-goose would be replaced and the
|
||||
// not.toHaveBeenCalled assertion would fail.
|
||||
mockProbe.mockResolvedValue({
|
||||
ok: true,
|
||||
models: [{ id: 'SHOULD-NOT-APPEAR', label: 'nope' }],
|
||||
modes: [],
|
||||
defaultModeId: null,
|
||||
commands: [],
|
||||
});
|
||||
|
||||
const sql = mockSql([
|
||||
{
|
||||
name: 'goose',
|
||||
install_path: '/usr/bin/goose',
|
||||
supports_acp: true,
|
||||
models: [{ id: 'cached-goose', label: 'Cached Goose' }],
|
||||
label: 'Goose',
|
||||
transport: 'acp',
|
||||
last_probed_at: new Date().toISOString(), // fresh
|
||||
},
|
||||
]);
|
||||
|
||||
// force=false → cache-miss returns loading; second call joins the build / cache.
|
||||
await getProviderSnapshot(sql, config, '/tmp/cwd', false);
|
||||
const entries = await getProviderSnapshot(sql, config, '/tmp/cwd', false);
|
||||
const goose = entries.find((e) => e.name === 'goose');
|
||||
|
||||
expect(goose?.status).toBe('ready');
|
||||
expect(goose?.installed).toBe(true);
|
||||
expect(goose?.models.map((m) => m.id)).toContain('cached-goose');
|
||||
expect(goose?.models.map((m) => m.id)).not.toContain('SHOULD-NOT-APPEAR');
|
||||
expect(mockProbe).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('force refresh → tier-2 cold probe RUNS even when DB is fresh', async () => {
|
||||
loadConfigFixture({});
|
||||
mockProbe.mockResolvedValue({
|
||||
ok: true,
|
||||
models: [{ id: 'fresh-probe', label: 'Fresh' }],
|
||||
modes: [],
|
||||
defaultModeId: null,
|
||||
commands: [],
|
||||
});
|
||||
|
||||
const sql = mockSql([
|
||||
{
|
||||
name: 'goose',
|
||||
install_path: '/usr/bin/goose',
|
||||
supports_acp: true,
|
||||
models: [{ id: 'cached-goose', label: 'Cached' }],
|
||||
label: 'Goose',
|
||||
transport: 'acp',
|
||||
last_probed_at: new Date().toISOString(), // fresh, but force overrides
|
||||
},
|
||||
]);
|
||||
|
||||
await getProviderSnapshot(sql, config, '/tmp/cwd', true);
|
||||
expect(mockProbe).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('native boocode → ready, enabled, installed', async () => {
|
||||
loadConfigFixture({});
|
||||
const sql = mockSql([]);
|
||||
const entries = await getProviderSnapshot(sql, config, '/tmp/project', true);
|
||||
const boocode = entries.find((e) => e.name === 'boocode');
|
||||
|
||||
expect(boocode?.status).toBe('ready');
|
||||
expect(boocode?.enabled).toBe(true);
|
||||
expect(boocode?.installed).toBe(true);
|
||||
});
|
||||
|
||||
it('2.7 warm cache: a second snapshot within the warm window spawns ZERO probes', async () => {
|
||||
loadConfigFixture({});
|
||||
mockProbe.mockResolvedValue({
|
||||
ok: true,
|
||||
models: [{ id: 'm1', label: 'M1' }],
|
||||
modes: [],
|
||||
defaultModeId: null,
|
||||
commands: [],
|
||||
});
|
||||
|
||||
const sql = mockSql([
|
||||
{
|
||||
name: 'goose',
|
||||
install_path: '/usr/bin/goose',
|
||||
supports_acp: true,
|
||||
models: null,
|
||||
label: 'Goose',
|
||||
transport: 'acp',
|
||||
last_probed_at: null,
|
||||
},
|
||||
]);
|
||||
|
||||
await getProviderSnapshot(sql, config, '/tmp/cwd', true); // cold populate
|
||||
const probeCallsAfterFirst = mockProbe.mock.calls.length;
|
||||
await getProviderSnapshot(sql, config, '/tmp/cwd', false); // warm read
|
||||
const probeCallsAfterSecond = mockProbe.mock.calls.length;
|
||||
|
||||
// Success criterion: second snapshot is served from cache with no ACP spawns.
|
||||
expect(probeCallsAfterSecond - probeCallsAfterFirst).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user