Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(console): a session with a huge model catalog no longer dies on its next message (#812)
Louis Amaudruz1ce291f7902a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e27caf9ed..be2f1f704 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1486,6 +1486,14 @@ version of their own to them without also giving them a release of their own.provider signs in with it on the machine.#### Fixed+- **A session whose provider offers hundreds of models no longer dies on its+ next message.** OpenCode signed in to many providers lists more models than+ a session event can hold, so every update to the session was refused and+ the host stopped when the next message arrived, leaving the room+ unanswered. The catalog is now cut to fit (without the models' options+ first, then without the models past what fits, the selected one kept), and+ the session says so. A host that fails also records its failure without the+ catalog, rather than failing again on it.- **A machine whose controller Switch refuses for good says so instead ofrestarting it forever.** When the server needs a newer agents controller thanConsole carries, "This computer as a machine" shows that Console needs andiff --git a/console/packages/agent-providers/src/host/model-catalog.test.ts b/console/packages/agent-providers/src/host/model-catalog.test.tsnew file mode 100644index 000000000..f5ec6b8fa--- /dev/null+++ b/console/packages/agent-providers/src/host/model-catalog.test.ts@@ -0,0 +1,40 @@+import { eventBytes } from '@switch-console/shared/session-v1';+import { describe, expect, it } from 'vitest';+import { fitModelCatalog, type SessionModel } from './model-catalog';++function catalog(count: number): SessionModel[] {+ return Array.from({ length: count }, (_, i) => ({+ id: `model-${i}`,+ label: `Model ${i} with a descriptive label`,+ options: { effort: ['low', 'medium', 'high'] },+ }));+}++describe('fitModelCatalog', () => {+ it('keeps a catalog that fits as it is', () => {+ const models = catalog(3);+ expect(fitModelCatalog(models, null, 10_000)).toEqual({+ models,+ omitted: 0,+ optionsDropped: false,+ });+ });++ it('drops the options first, but not the selected model’s', () => {+ const models = catalog(20);+ const fitted = fitModelCatalog(models, 'model-4', eventBytes(models) - 1);+ expect(fitted.omitted).toBe(0);+ expect(fitted.optionsDropped).toBe(true);+ expect(fitted.models.find((m) => m.id === 'model-4')?.options).toEqual(models[4]!.options);+ expect(fitted.models.find((m) => m.id === 'model-5')?.options).toEqual({});+ });++ it('then lists what fits, the selected model first, within the budget', () => {+ const models = catalog(500);+ const fitted = fitModelCatalog(models, 'model-499', 4_000);+ expect(fitted.models[0]!.id).toBe('model-499');+ expect(fitted.omitted).toBe(500 - fitted.models.length);+ expect(fitted.omitted).toBeGreaterThan(0);+ expect(eventBytes(fitted.models)).toBeLessThanOrEqual(4_000);+ });+});diff --git a/console/packages/agent-providers/src/host/model-catalog.ts b/console/packages/agent-providers/src/host/model-catalog.tsnew file mode 100644index 000000000..97b9fbd91--- /dev/null+++ b/console/packages/agent-providers/src/host/model-catalog.ts@@ -0,0 +1,48 @@+import { eventBytes } from '@switch-console/shared/session-v1';+import type { Session } from '@switch-console/shared/session-v1';++export type SessionModel = NonNullable<Session['models']>[number];++/**+ * How much of a session event the model catalog may take. Every event is held+ * to 64 KiB, and the catalog rides on each `session.upsert` beside the rest of+ * the session record, which is small.+ */+export const MODEL_CATALOG_BYTES = 48 * 1024;++export type FittedCatalog = {+ models: SessionModel[];+ /** How many of the provider's models were left out to fit. */+ omitted: number;+ /** Whether the models' options were left out to fit (the selected one keeps its own). */+ optionsDropped: boolean;+};++/**+ * The provider's model catalog cut to fit `budget` bytes: whole if it fits;+ * else without the models' options (the selected model keeps its own); else+ * also without the models past what fits, the selected one always kept and+ * first. A provider with hundreds of models (OpenCode with many providers+ * signed in) would otherwise make every `session.upsert` too large to record.+ */+export function fitModelCatalog(+ models: SessionModel[],+ selectedId: string | null,+ budget = MODEL_CATALOG_BYTES+): FittedCatalog {+ if (eventBytes(models) <= budget) return { models, omitted: 0, optionsDropped: false };+ const bare = models.map((model) => (model.id === selectedId ? model : { ...model, options: {} }));+ if (eventBytes(bare) <= budget) return { models: bare, omitted: 0, optionsDropped: true };+ const selected = bare.filter((model) => model.id === selectedId);+ const rest = bare.filter((model) => model.id !== selectedId);+ const kept = [...selected];+ // The array's brackets, then each model and the comma before it.+ let used = 2 + kept.reduce((total, model) => total + eventBytes(model) + 1, 0);+ for (const model of rest) {+ const size = eventBytes(model) + 1;+ if (used + size > budget) break;+ kept.push(model);+ used += size;+ }+ return { models: kept, omitted: models.length - kept.length, optionsDropped: true };+}diff --git a/console/packages/agent-providers/src/host/session-host.test.ts b/console/packages/agent-providers/src/host/session-host.test.tsindex 84b31ade3..861175d93 100644--- a/console/packages/agent-providers/src/host/session-host.test.ts+++ b/console/packages/agent-providers/src/host/session-host.test.ts@@ -602,6 +602,34 @@ it('validates native model choices and persists a confirmed choice across restar);});+it('lists a catalog too large for a session event without its overflow, and keeps working', async () => {+ const root = await mkdtemp(join(tmpdir(), 'sdk-model-test-'));+ roots.push(root);+ const fixture = setup('opencode');+ const catalog = Array.from({ length: 600 }, (_, i) => ({+ id: `provider-${i}/model-with-a-long-identifier-${i}`,+ label: `Provider ${i} · A model with a long, descriptive label ${i}`,+ options: { variant: ['low', 'medium', 'high', 'max'] },+ }));+ fixture.adapter.listModels = vi.fn(async () => catalog);+ fixture.adapter.setModel = vi.fn(async () => {});+ const host = await HostedSession.start(root, fixture.config, fixture.adapter);+ hosts.push(host);+ await waitFor(() => expect(host.snapshot().session.models?.length).toBeGreaterThan(0));+ const listed = host.snapshot().session.models!;+ expect(listed.length).toBeLessThan(catalog.length);+ expect(listed.every((model) => Object.keys(model.options).length === 0)).toBe(true);+ expect(host.replay(0).events.map((e) => e.body)).toContainEqual(+ expect.objectContaining({ code: 'MODEL_CATALOG_TRUNCATED' })+ );+ expect(host.replay(0).events.map((e) => e.body)).not.toContainEqual(+ expect.objectContaining({ code: 'MODEL_CATALOG_UNAVAILABLE' })+ );+ // The session record is published again on every turn; it still fits.+ expect((await host.command(message('after-catalog'))).status).not.toBe('rejected');+ expect(host.snapshot().session.status).not.toBe('error');+});+it('leaves a lost model acknowledgement unknown and does not retry it', async () => {const root = await mkdtemp(join(tmpdir(), 'sdk-model-test-'));roots.push(root);diff --git a/console/packages/agent-providers/src/host/session-host.ts b/console/packages/agent-providers/src/host/session-host.tsindex 346752121..77dbf4ccf 100644--- a/console/packages/agent-providers/src/host/session-host.ts+++ b/console/packages/agent-providers/src/host/session-host.ts@@ -26,6 +26,7 @@ import type { ProviderRuntimeEvent } from '../events';import { ChatProjector } from '../session-v1/chat-projector';import { ATTACHMENT_MIME_TYPES } from './attachments';import { Journal } from './journal';+import { fitModelCatalog } from './model-catalog';export const hostInboxRecordSchema = z.discriminatedUnion('type', [z.object({ type: z.literal('accepted'), command: commandSchema }),@@ -420,13 +421,26 @@ export class HostedSession {'The provider did not return a model catalog on resume. Showing the last known choices; the provider will validate each change.',});}- this.config.session.models = models;- this.config.session.model = this.config.input.model+ const selected = this.config.input.model? { id: this.config.input.model.id, options: this.config.input.model.options ?? {} }: null;- this.config.session.capabilities.modelChange = Boolean(models.length && this.adapter.setModel);+ const fitted = fitModelCatalog(models, selected?.id ?? null);+ this.config.session.models = fitted.models;+ this.config.session.model = selected;+ this.config.session.capabilities.modelChange = Boolean(+ fitted.models.length && this.adapter.setModel+ );this.updateAttachmentCapabilities();await this.publish({ type: 'session.upsert', session: structuredClone(this.config.session) });+ if (fitted.omitted || fitted.optionsDropped)+ await this.publish({+ type: 'notice',+ level: 'warning',+ code: 'MODEL_CATALOG_TRUNCATED',+ message: fitted.omitted+ ? `The provider offers ${models.length} models, more than a session can list: only ${fitted.models.length} are offered here, without their options. Narrow the providers it is signed in to, or pick a model in its own settings.`+ : `The provider offers ${models.length} models: they are listed without their options, which would not fit. The selected model keeps its own.`,+ });}private updateAttachmentCapabilities(): void {@@ -1230,7 +1244,18 @@ export class HostedSession {this.fault = error instanceof Error ? error : new Error(String(error));this.config.session.status = 'error';try {- await this.publish({ type: 'session.upsert', session: structuredClone(this.config.session) });+ try {+ await this.publish({+ type: 'session.upsert',+ session: structuredClone(this.config.session),+ });+ } catch (upsertError) {+ // The failure is recorded even when the session record is what could+ // not be: without its model catalog it is a few hundred bytes.+ console.error('SDK host could not record its session:', String(upsertError));+ const { models: _models, ...session } = structuredClone(this.config.session);+ await this.publish({ type: 'session.upsert', session });+ }await this.publish({type: 'notice',level: 'error',