Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion container/agent-runner/src/agent-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,13 @@ export function buildContainerMcpServers(
};
}

export function getAgentBackendModel(
containerInput: RuntimeContainerInput,
): string | undefined {
const model = containerInput.agentBackend?.model?.trim();
return model || undefined;
}

function tomlString(value: string): string {
return JSON.stringify(value);
}
Expand Down Expand Up @@ -747,7 +754,7 @@ class ClaudeCodeQueryRunner<
),
],
env: sdkEnv,
model: containerInput.agentBackend?.model,
model: getAgentBackendModel(containerInput),
permissionMode: 'bypassPermissions',
allowDangerouslySkipPermissions: true,
settingSources: ['project', 'user'],
Expand Down
210 changes: 210 additions & 0 deletions src/agent/agentlite-impl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import os from 'os';
import path from 'path';

import Database from 'better-sqlite3';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

vi.mock('../box-runtime.js', () => ({
Expand Down Expand Up @@ -105,6 +106,67 @@ describe('AgentLite platform registry', () => {
expect((restored as unknown as { _started: boolean })._started).toBe(false);
});

it('migrates registry rows created before backend_model existed', async () => {
const registryPath = getAgentRegistryDbPath(tmpDir);
fs.mkdirSync(path.dirname(registryPath), { recursive: true });
const raw = new Database(registryPath);
raw.exec(`
CREATE TABLE agents (
name TEXT PRIMARY KEY,
agent_id TEXT NOT NULL UNIQUE,
workdir TEXT NOT NULL,
assistant_name TEXT NOT NULL,
backend_type TEXT NOT NULL DEFAULT 'claudeCode',
mount_allowlist_json TEXT,
instructions TEXT,
skills_sources_json TEXT,
mcp_servers_json TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
INSERT INTO agents (
name,
agent_id,
workdir,
assistant_name,
backend_type,
mount_allowlist_json,
instructions,
skills_sources_json,
mcp_servers_json,
created_at,
updated_at
) VALUES (
'legacy',
'legacy01',
'${path.join(tmpDir, 'legacy-agent').replaceAll("'", "''")}',
'Legacy',
'codex',
NULL,
NULL,
NULL,
NULL,
'2026-01-01T00:00:00.000Z',
'2026-01-01T00:00:00.000Z'
);
`);
raw.close();

const platform = await createAgentLiteImpl({ workdir: tmpDir });
platforms.push(platform);

const restored = platform.agents.get('legacy') as AgentImpl | undefined;
expect(restored).toBeDefined();
expect(restored!.getBackend()).toEqual({ type: 'codex' });

const registry = initAgentRegistryDb(tmpDir);
try {
expect(registry.getAgent('legacy')!.backend).toEqual({ type: 'codex' });
} finally {
registry.close();
}
});

it('getOrCreateAgent merges runtime-only options and rejects conflicting serializable options', async () => {
const firstPlatform = await createAgentLiteImpl({ workdir: tmpDir });
platforms.push(firstPlatform);
Expand Down Expand Up @@ -246,6 +308,154 @@ describe('AgentLite platform registry', () => {
expect(agent.backendRevision).toBe(0);
});

it('same-backend model changes can clear overrides without replacing the native session', async () => {
const platform = await createAgentLiteImpl({ workdir: tmpDir });
platforms.push(platform);

const agent = platform.createAgent('alice', {
backend: { type: 'codex', model: 'gpt-5.4' },
}) as AgentImpl;
agent.db = _initTestDatabase();
agent.registeredGroups['self-chat'] = {
name: 'Main',
folder: 'main',
trigger: 'always',
added_at: new Date().toISOString(),
isMain: true,
};
agent.sessions.main = 'codex-thread';
agent.db.setSession('main', 'codex-thread', 'codex');
const closeSpy = vi.spyOn(agent.queue, 'closeStdin');
(agent as unknown as { _started: boolean })._started = true;

const result = await agent.setBackend({ type: 'codex' });

expect(result).toMatchObject({
previous: { type: 'codex', model: 'gpt-5.4' },
current: { type: 'codex' },
applies: 'nextTurn',
handoff: 'notNeeded',
});
expect(agent.getBackend()).toEqual({ type: 'codex' });
expect(agent.sessions.main).toBe('codex-thread');
expect(agent.db.getSession('main', 'codex')).toBe('codex-thread');
expect(agent.backendRevision).toBe(0);
expect(closeSpy).toHaveBeenCalledWith('self-chat');

const registry = initAgentRegistryDb(tmpDir);
try {
expect(registry.getAgent('alice')!.backend).toEqual({ type: 'codex' });
} finally {
registry.close();
}
});

it('backend switches can request fresh context without creating handoff state', async () => {
const platform = await createAgentLiteImpl({ workdir: tmpDir });
platforms.push(platform);

const agent = platform.createAgent('alice') as AgentImpl;
agent.db = _initTestDatabase();
agent.registeredGroups['self-chat'] = {
name: 'Main',
folder: 'main',
trigger: 'always',
added_at: new Date().toISOString(),
isMain: true,
};
agent.sessions.main = 'claude-session';
agent.db.setSession('main', 'claude-session', 'claudeCode');
agent.db.setSession('main', 'stale-codex-thread', 'codex');
(agent as unknown as { _started: boolean })._started = true;

const result = await agent.setBackend(
{ type: 'codex' },
{ context: 'fresh' },
);

expect(result.handoff).toBe('skipped');
expect(agent.getBackend()).toEqual({ type: 'codex' });
expect(agent.sessions.main).toBeUndefined();
expect(agent.db.getSession('main', 'claudeCode')).toBe('claude-session');
expect(agent.db.getSession('main', 'codex')).toBeUndefined();
expect(agent.db.getBackendHandoff('main', 'codex')).toBeUndefined();
expect(agent.backendRevision).toBe(1);
});

it.each([
{
label: 'before start',
started: false,
groups: {
'self-chat': {
name: 'Main',
folder: 'main',
trigger: 'always',
added_at: '2026-01-01T00:00:00.000Z',
isMain: true,
},
},
},
{
label: 'with no registered groups',
started: true,
groups: {},
},
])('backend switches skip handoff $label', async ({ started, groups }) => {
const platform = await createAgentLiteImpl({ workdir: tmpDir });
platforms.push(platform);

const agent = platform.createAgent('alice') as AgentImpl;
agent.db = _initTestDatabase();
agent.registeredGroups = groups;
const closeSpy = vi.spyOn(agent.queue, 'closeStdin');
(agent as unknown as { _started: boolean })._started = started;

const result = await agent.setBackend({ type: 'codex' });

expect(result).toMatchObject({
previous: { type: 'claudeCode' },
current: { type: 'codex' },
applies: 'nextTurn',
handoff: 'skipped',
});
expect(agent.getBackend()).toEqual({ type: 'codex' });
expect(agent.backendRevision).toBe(1);
expect(closeSpy).not.toHaveBeenCalled();
});

it('rejects invalid backend updates without mutating the current backend', async () => {
const platform = await createAgentLiteImpl({ workdir: tmpDir });
platforms.push(platform);

const agent = platform.createAgent('alice', {
backend: { type: 'claudeCode', model: 'claude-sonnet-4-6' },
}) as AgentImpl;
const closeSpy = vi.spyOn(agent.queue, 'closeStdin');
(agent as unknown as { _started: boolean })._started = true;

await expect(
agent.setBackend({ type: 'unknown' } as never),
).rejects.toThrow('Invalid agent backend');

expect(agent.getBackend()).toEqual({
type: 'claudeCode',
model: 'claude-sonnet-4-6',
});
expect(agent.backendRevision).toBe(0);
expect(closeSpy).not.toHaveBeenCalled();

const registry = initAgentRegistryDb(tmpDir);
try {
expect(registry.getAgent('alice')!.backend).toEqual({
type: 'claudeCode',
model: 'claude-sonnet-4-6',
});
} finally {
registry.close();
}
});

it('setBackend no-ops when backend and model are unchanged', async () => {
const platform = await createAgentLiteImpl({ workdir: tmpDir });
platforms.push(platform);
Expand Down
Loading
Loading