feat(db): move container config from filesystem to DB
Source of truth for container runtime config moves from
groups/<folder>/container.json to a new container_configs table.
The file becomes a materialized view written at spawn time.
- New container_configs table with scalar columns (provider, model,
effort, image_tag, assistant_name, max_messages_per_prompt) and
JSON columns (mcp_servers, packages_apt, packages_npm, skills,
additional_mounts)
- Startup backfill seeds DB from existing container.json files
- materializeContainerJson() replaces readContainerConfig + ensureRuntimeFields
- Self-mod handlers (install_packages, add_mcp_server) write to DB
- Provider cascade simplified: session -> container_configs -> 'claude'
- ncl groups config-{get,update,add-mcp-server,remove-mcp-server,
add-package,remove-package} custom operations
- restartAgentGroupContainers() helper for config change propagation
- Container side unchanged (still reads /workspace/agent/container.json)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
77
src/backfill-container-configs.ts
Normal file
77
src/backfill-container-configs.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* One-time backfill: seed `container_configs` rows from existing
|
||||
* `groups/<folder>/container.json` files and `agent_groups.agent_provider`.
|
||||
*
|
||||
* Runs after migrations, before channel adapters start. Idempotent — skips
|
||||
* groups that already have a config row.
|
||||
*/
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import { GROUPS_DIR } from './config.js';
|
||||
import type { McpServerConfig, AdditionalMountConfig } from './container-config.js';
|
||||
import { getAllAgentGroups } from './db/agent-groups.js';
|
||||
import { getContainerConfig, createContainerConfig } from './db/container-configs.js';
|
||||
import { log } from './log.js';
|
||||
import type { ContainerConfigRow } from './types.js';
|
||||
|
||||
interface LegacyContainerJson {
|
||||
mcpServers?: Record<string, McpServerConfig>;
|
||||
packages?: { apt?: string[]; npm?: string[] };
|
||||
imageTag?: string;
|
||||
additionalMounts?: AdditionalMountConfig[];
|
||||
skills?: string[] | 'all';
|
||||
provider?: string;
|
||||
assistantName?: string;
|
||||
maxMessagesPerPrompt?: number;
|
||||
}
|
||||
|
||||
export function backfillContainerConfigs(): void {
|
||||
const groups = getAllAgentGroups();
|
||||
let backfilled = 0;
|
||||
|
||||
for (const group of groups) {
|
||||
// Skip if already has a config row
|
||||
if (getContainerConfig(group.id)) continue;
|
||||
|
||||
// Read legacy container.json from disk
|
||||
const filePath = path.join(GROUPS_DIR, group.folder, 'container.json');
|
||||
let legacy: LegacyContainerJson = {};
|
||||
if (fs.existsSync(filePath)) {
|
||||
try {
|
||||
legacy = JSON.parse(fs.readFileSync(filePath, 'utf8')) as LegacyContainerJson;
|
||||
} catch (err) {
|
||||
log.warn('Backfill: failed to parse container.json, using defaults', {
|
||||
folder: group.folder,
|
||||
err: String(err),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// DB agent_provider wins over file provider (matches old cascade)
|
||||
const provider = group.agent_provider || legacy.provider || null;
|
||||
|
||||
const row: ContainerConfigRow = {
|
||||
agent_group_id: group.id,
|
||||
provider,
|
||||
model: null,
|
||||
effort: null,
|
||||
image_tag: legacy.imageTag ?? null,
|
||||
assistant_name: legacy.assistantName ?? null,
|
||||
max_messages_per_prompt: legacy.maxMessagesPerPrompt ?? null,
|
||||
skills: JSON.stringify(legacy.skills ?? 'all'),
|
||||
mcp_servers: JSON.stringify(legacy.mcpServers ?? {}),
|
||||
packages_apt: JSON.stringify(legacy.packages?.apt ?? []),
|
||||
packages_npm: JSON.stringify(legacy.packages?.npm ?? []),
|
||||
additional_mounts: JSON.stringify(legacy.additionalMounts ?? []),
|
||||
updated_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
createContainerConfig(row);
|
||||
backfilled++;
|
||||
}
|
||||
|
||||
if (backfilled > 0) {
|
||||
log.info('Backfilled container_configs from disk', { count: backfilled });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user