feat(schedules): add workspace modes and worktree management

This commit is contained in:
Chris Scott committed 2026-10-06 10:16:34 -04:00
1 parent c06075651d
commit 6ec3f3bd91
90 files changed
+2084 -801

No files matched your search

+67 -51
View File
@@ -1,65 +1,38 @@
import { Database } from 'bun:sqlite'
import { logger } from '../utils/logger'
/**
* A schema change applied once per database. Migrations run in the order they appear in
* `allMigrations`, and each is recorded by its `id`, which must never change once released.
*
* New migrations use a UTC timestamp id, `YYYYMMDDHHmm-short-name` (for example
* `202610061345-schedule-workspace-mode`), in a file of the same name, so migrations written on
* different branches cannot claim the same identity. Make `up` safe to run on a database that
* already has the change, because merged branches may apply migrations in a different order.
*
* `legacy` only exists on the numbered migrations written before ids; it lets databases that
* recorded them by version number in `schema_migrations` recognise them as applied.
*/
export interface Migration {
version: number
name: string
id: string
legacy?: { version: number; name: string }
up(db: Database): void
down(db: Database): void
}
interface MigrationRecord {
interface LegacyMigrationRecord {
version: number
name: string
applied_at: number
}
function ensureMigrationsTable(db: Database): void {
db.run(`
CREATE TABLE IF NOT EXISTS schema_migrations (
version INTEGER PRIMARY KEY,
name TEXT NOT NULL,
applied_at INTEGER NOT NULL
)
`)
}
function getAppliedMigrations(db: Database): Map<number, string> {
const rows = db.prepare('SELECT version, name FROM schema_migrations ORDER BY version').all() as MigrationRecord[]
return new Map(rows.map(r => [r.version, r.name]))
}
/**
* Surfaces version-number collisions where a recorded migration's name differs
* from the code migration registered under the same version. The runner keys on
* version number alone, so a reused version silently skips the real migration
* (e.g. an ADD COLUMN), producing later runtime errors that are hard to trace.
* This converts that silent skip into a loud, actionable warning.
*/
function warnOnVersionNameMismatch(applied: Map<number, string>, migrations: Migration[]): void {
for (const migration of migrations) {
const recordedName = applied.get(migration.version)
if (recordedName !== undefined && recordedName !== migration.name) {
logger.warn(
`Migration version ${migration.version} is recorded as "${recordedName}" but the code defines "${migration.name}". ` +
`This migration was skipped; its schema changes may be missing. Verify the database schema and apply the changes manually if needed.`,
)
}
}
}
function markApplied(db: Database, migration: Migration): void {
db.prepare('INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)')
.run(migration.version, migration.name, Date.now())
}
export function migrate(db: Database, migrations: Migration[]): void {
ensureMigrationsTable(db)
assertUniqueIds(migrations)
db.run('CREATE TABLE IF NOT EXISTS applied_migrations (id TEXT PRIMARY KEY, applied_at INTEGER NOT NULL)')
adoptLegacyMigrations(db, migrations)
const applied = getAppliedMigrations(db)
warnOnVersionNameMismatch(applied, migrations)
const sorted = [...migrations].sort((a, b) => a.version - b.version)
const pending = sorted.filter(m => !applied.has(m.version))
const applied = new Set((db.prepare('SELECT id FROM applied_migrations').all() as Array<{ id: string }>).map((row) => row.id))
const pending = migrations.filter((migration) => !applied.has(migration.id))
if (pending.length === 0) {
logger.info('Database schema is up to date')
@@ -69,16 +42,16 @@ export function migrate(db: Database, migrations: Migration[]): void {
logger.info(`Running ${pending.length} pending migration(s)`)
for (const migration of pending) {
logger.info(`Applying migration ${migration.version}: ${migration.name}`)
logger.info(`Applying migration ${migration.id}`)
db.run('BEGIN TRANSACTION')
try {
migration.up(db)
markApplied(db, migration)
db.prepare('INSERT INTO applied_migrations (id, applied_at) VALUES (?, ?)').run(migration.id, Date.now())
db.run('COMMIT')
logger.info(`Migration ${migration.version} applied successfully`)
logger.info(`Migration ${migration.id} applied successfully`)
} catch (error) {
db.run('ROLLBACK')
logger.error(`Migration ${migration.version} failed:`, error)
logger.error(`Migration ${migration.id} failed:`, error)
throw error
}
}
@@ -86,4 +59,47 @@ export function migrate(db: Database, migrations: Migration[]): void {
logger.info('All migrations applied successfully')
}
function assertUniqueIds(migrations: Migration[]): void {
const seen = new Set<string>()
for (const migration of migrations) {
if (seen.has(migration.id)) throw new Error(`Duplicate migration id "${migration.id}"`)
seen.add(migration.id)
}
}
/**
* Records numbered migrations that a database applied under the old version-keyed table.
* A version recorded under a different name is still treated as applied, as before, but warned
* about because its schema change may be missing.
*/
function adoptLegacyMigrations(db: Database, migrations: Migration[]): void {
const hasLegacyTable = db.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'schema_migrations'").get()
if (!hasLegacyTable) return
const records = new Map(
(db.prepare('SELECT version, name, applied_at FROM schema_migrations').all() as LegacyMigrationRecord[])
.map((record) => [record.version, record] as const),
)
const adopted = migrations.flatMap((migration) => {
const record = migration.legacy ? records.get(migration.legacy.version) : undefined
if (!migration.legacy || !record) return []
if (record.name !== migration.legacy.name) {
logger.warn(
`Migration version ${migration.legacy.version} is recorded as "${record.name}" but the code defines "${migration.legacy.name}". ` +
`This migration was skipped; its schema changes may be missing. Verify the database schema and apply the changes manually if needed.`,
)
}
return [{ id: migration.id, appliedAt: record.applied_at }]
})
if (adopted.length === 0) return
const insert = db.prepare('INSERT OR IGNORE INTO applied_migrations (id, applied_at) VALUES (?, ?)')
db.run('BEGIN TRANSACTION')
try {
adopted.forEach((entry) => insert.run(entry.id, entry.appliedAt))
db.run('COMMIT')
} catch (error) {
db.run('ROLLBACK')
throw error
}
}
+2 -2
View File
@@ -1,8 +1,8 @@
import type { Migration } from '../migration-runner'
const migration: Migration = {
version: 1,
name: 'base-schema',
id: '001-base-schema',
legacy: { version: 1, name: 'base-schema' },
up(db) {
db.run(`
@@ -6,8 +6,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 2,
name: 'repos-nullable-url',
id: '002-repos-nullable-url',
legacy: { version: 2, name: 'repos-nullable-url' },
up(db) {
const tableInfo = db.prepare('PRAGMA table_info(repos)').all() as ColumnInfo[]
@@ -16,8 +16,8 @@ const COLUMNS = [
]
const migration: Migration = {
version: 3,
name: 'repos-add-columns',
id: '003-repos-add-columns',
legacy: { version: 3, name: 'repos-add-columns' },
up(db) {
const tableInfo = db.prepare('PRAGMA table_info(repos)').all() as ColumnInfo[]
@@ -1,8 +1,8 @@
import type { Migration } from '../migration-runner'
const migration: Migration = {
version: 4,
name: 'repos-indexes',
id: '004-repos-indexes',
legacy: { version: 4, name: 'repos-indexes' },
up(db) {
db.run(`
@@ -2,8 +2,8 @@ import type { Migration } from '../migration-runner'
import { logger } from '../../utils/logger'
const migration: Migration = {
version: 5,
name: 'repos-local-path-prefix',
id: '005-repos-local-path-prefix',
legacy: { version: 5, name: 'repos-local-path-prefix' },
up(db) {
const repos = db.prepare("SELECT id, local_path FROM repos WHERE local_path LIKE 'repos/%'").all() as Array<{
@@ -2,8 +2,8 @@ import type { Migration } from '../migration-runner'
import { logger } from '../../utils/logger'
const migration: Migration = {
version: 6,
name: 'git-token-to-credentials',
id: '006-git-token-to-credentials',
legacy: { version: 6, name: 'git-token-to-credentials' },
up(db) {
const rows = db.prepare('SELECT user_id, preferences FROM user_preferences').all() as Array<{
+2 -2
View File
@@ -1,8 +1,8 @@
import type { Migration } from '../migration-runner'
const migration: Migration = {
version: 7,
name: 'schedules',
id: '007-schedules',
legacy: { version: 7, name: 'schedules' },
up(db) {
db.run(`
@@ -7,8 +7,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 8,
name: 'schedule-cron-support',
id: '008-schedule-cron-support',
legacy: { version: 8, name: 'schedule-cron-support' },
up(db) {
const tableExists = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='schedule_jobs'").get()
@@ -1,8 +1,8 @@
import type { Migration } from '../migration-runner'
const migration: Migration = {
version: 10,
name: '010-prompt-templates',
id: '009-prompt-templates',
legacy: { version: 10, name: '010-prompt-templates' },
up(db) {
db.run(`
@@ -5,8 +5,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 9,
name: 'repo-source-path',
id: '009-repo-source-path',
legacy: { version: 9, name: 'repo-source-path' },
up(db) {
const tableInfo = db.prepare('PRAGMA table_info(repos)').all() as ColumnInfo[]
@@ -5,8 +5,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 11,
name: 'repo-last-accessed',
id: '011-repo-last-accessed',
legacy: { version: 11, name: 'repo-last-accessed' },
up(db) {
const tableInfo = db.prepare('PRAGMA table_info(repos)').all() as ColumnInfo[]
@@ -1,8 +1,8 @@
import type { Migration } from '../migration-runner'
const migration: Migration = {
version: 12,
name: 'opencode-model-state',
id: '012-opencode-model-state',
legacy: { version: 12, name: 'opencode-model-state' },
up(db) {
db.run(`
CREATE TABLE IF NOT EXISTS opencode_model_state (
+2 -2
View File
@@ -1,8 +1,8 @@
import type { Migration } from '../migration-runner'
const migration: Migration = {
version: 13,
name: 'app-secrets',
id: '013-app-secrets',
legacy: { version: 13, name: 'app-secrets' },
up(db) {
db.run(`
CREATE TABLE IF NOT EXISTS app_secrets (
@@ -5,8 +5,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 14,
name: 'repos-add-name',
id: '014-repos-add-name',
legacy: { version: 14, name: 'repos-add-name' },
up(db) {
const tableInfo = db.prepare('PRAGMA table_info(repos)').all() as ColumnInfo[]
@@ -7,8 +7,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 15,
name: 'schedule-worktree-isolation',
id: '015-schedule-worktree-isolation',
legacy: { version: 15, name: 'schedule-worktree-isolation' },
up(db) {
const jobColumns = db.prepare('PRAGMA table_info(schedule_jobs)').all() as ColumnInfo[]
@@ -7,8 +7,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 16,
name: 'schedule-permission-config',
id: '016-schedule-permission-config',
legacy: { version: 16, name: 'schedule-permission-config' },
up(db) {
const cols = db.prepare('PRAGMA table_info(schedule_jobs)').all() as ColumnInfo[]
@@ -7,8 +7,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 17,
name: 'schedule-run-workspace-id',
id: '017-schedule-run-workspace-id',
legacy: { version: 17, name: 'schedule-run-workspace-id' },
up(db) {
const cols = db.prepare('PRAGMA table_info(schedule_runs)').all() as ColumnInfo[]
@@ -2,8 +2,8 @@ import type { Migration } from '../migration-runner'
import { ensureSessionPinsTable } from '../session-pins'
const migration: Migration = {
version: 18,
name: 'session-pins',
id: '018-session-pins',
legacy: { version: 18, name: 'session-pins' },
up(db) {
ensureSessionPinsTable(db)
},
@@ -45,8 +45,8 @@ function archiveConfigs(rows: Array<{ config_name: string; config_content: strin
}
const migration: Migration = {
version: 19,
name: 'drop-opencode-configs',
id: '019-drop-opencode-configs',
legacy: { version: 19, name: 'drop-opencode-configs' },
up(db) {
const rows = db.prepare('SELECT config_name, config_content, is_default FROM opencode_configs').all() as Array<{ config_name: string; config_content: string; is_default: number | null }>
@@ -49,8 +49,8 @@ function restoreModelStateFile(row: ModelStateRow): void {
}
const migration: Migration = {
version: 20,
name: 'drop-opencode-model-state',
id: '020-drop-opencode-model-state',
legacy: { version: 20, name: 'drop-opencode-model-state' },
up(db) {
const row = readDefaultRow(db)
@@ -5,8 +5,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 21,
name: 'drop-schedule-run-workspace-id',
id: '021-drop-schedule-run-workspace-id',
legacy: { version: 21, name: 'drop-schedule-run-workspace-id' },
up(db) {
const cols = db.prepare('PRAGMA table_info(schedule_runs)').all() as ColumnInfo[]
@@ -1,8 +1,8 @@
import type { Migration } from '../migration-runner'
const migration: Migration = {
version: 22,
name: 'schedule-runs-session-index',
id: '022-schedule-runs-session-index',
legacy: { version: 22, name: 'schedule-runs-session-index' },
up(db) {
db.run(`
@@ -5,8 +5,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 23,
name: 'schedule-mcp-servers',
id: '023-schedule-mcp-servers',
legacy: { version: 23, name: 'schedule-mcp-servers' },
up(db) {
const columns = db.prepare('PRAGMA table_info(schedule_jobs)').all() as ColumnInfo[]
@@ -5,8 +5,8 @@ interface ColumnInfo {
}
const migration: Migration = {
version: 24,
name: 'schedule-runs-viewed-at',
id: '024-schedule-runs-viewed-at',
legacy: { version: 24, name: 'schedule-runs-viewed-at' },
up(db) {
const columns = db.prepare('PRAGMA table_info(schedule_runs)').all() as ColumnInfo[]
@@ -2,8 +2,8 @@ import type { Migration } from '../migration-runner'
import { ensureSessionPermissionModesTable } from '../session-permission-modes'
const migration: Migration = {
version: 25,
name: 'session-permission-modes',
id: '025-session-permission-modes',
legacy: { version: 25, name: 'session-permission-modes' },
up(db) {
ensureSessionPermissionModesTable(db)
},
@@ -2,8 +2,8 @@ import type { Migration } from '../migration-runner'
import { ensureSessionGoalsTable } from '../session-goals'
const migration: Migration = {
version: 26,
name: 'session-goals',
id: '026-session-goals',
legacy: { version: 26, name: 'session-goals' },
up(db) {
ensureSessionGoalsTable(db)
},
+2 -2
View File
@@ -2,8 +2,8 @@ import type { Migration } from '../migration-runner'
import { ensureMultiRunTables } from '../multi-runs'
const migration: Migration = {
version: 27,
name: 'multi-runs',
id: '027-multi-runs',
legacy: { version: 27, name: 'multi-runs' },
up(db) {
ensureMultiRunTables(db)
},
@@ -0,0 +1,22 @@
import type { Migration } from '../migration-runner'
interface ColumnInfo {
name: string
}
const migration: Migration = {
id: '202610061345-schedule-workspace-mode',
up(db) {
const columns = db.prepare('PRAGMA table_info(schedule_jobs)').all() as ColumnInfo[]
if (!columns.some((column) => column.name === 'workspace_mode')) {
db.run("ALTER TABLE schedule_jobs ADD COLUMN workspace_mode TEXT NOT NULL DEFAULT 'worktree'")
}
},
down(db) {
db.run('ALTER TABLE schedule_jobs DROP COLUMN workspace_mode')
},
}
export default migration
+2
View File
@@ -26,6 +26,7 @@ import migration024 from './024-schedule-runs-viewed-at'
import migration025 from './025-session-permission-modes'
import migration026 from './026-session-goals'
import migration027 from './027-multi-runs'
import migration202610061345 from './202610061345-schedule-workspace-mode'
export const allMigrations: Migration[] = [
migration001,
@@ -55,4 +56,5 @@ export const allMigrations: Migration[] = [
migration025,
migration026,
migration027,
migration202610061345,
]
+17 -7
View File
@@ -12,6 +12,7 @@ import {
type ScheduleRun,
type ScheduleRunStatus,
type ScheduleRunTriggerSource,
type ScheduleWorkspaceMode,
} from '@opencode-manager/shared/schemas'
import { ASSISTANT_REPO_ID, ASSISTANT_REPO_NAME, ASSISTANT_REPO_PATH, getRepoDisplayName } from '@opencode-manager/shared/utils'
import type { ScheduleJobPersistenceInput } from '../services/schedule-config'
@@ -33,6 +34,7 @@ interface ScheduleJobRow {
permission_config: string | null
mcp_servers: string | null
branch: string | null
workspace_mode: ScheduleWorkspaceMode | null
created_at: number
updated_at: number
last_run_at: number | null
@@ -118,6 +120,7 @@ function rowToScheduleJob(row: ScheduleJobRow): ScheduleJob {
permissionConfig: parsePermissionConfig(row.permission_config),
mcpServers: parseMcpServers(row.mcp_servers),
branch: row.branch,
workspaceMode: row.workspace_mode ?? 'worktree',
createdAt: row.created_at,
updatedAt: row.updated_at,
lastRunAt: row.last_run_at,
@@ -199,9 +202,10 @@ export function createScheduleJob(db: Database, repoId: number, input: ScheduleJ
permission_config,
mcp_servers,
branch,
workspace_mode,
created_at, updated_at, last_run_at, next_run_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`)
const result = stmt.run(
@@ -220,6 +224,7 @@ export function createScheduleJob(db: Database, repoId: number, input: ScheduleJ
serializePermissionConfig(input.permissionConfig),
serializeMcpServers(input.mcpServers),
input.branch,
input.workspaceMode,
now,
now,
null,
@@ -244,7 +249,7 @@ export function updateScheduleJob(db: Database, repoId: number, jobId: number, i
const stmt = db.prepare(`
UPDATE schedule_jobs
SET name = ?, description = ?, enabled = ?, schedule_mode = ?, interval_minutes = ?, cron_expression = ?, timezone = ?,
agent_slug = ?, prompt = ?, model = ?, skill_metadata = ?, permission_config = ?, mcp_servers = ?, branch = ?, updated_at = ?, next_run_at = ?
agent_slug = ?, prompt = ?, model = ?, skill_metadata = ?, permission_config = ?, mcp_servers = ?, branch = ?, workspace_mode = ?, updated_at = ?, next_run_at = ?
WHERE repo_id = ? AND id = ?
`)
@@ -263,6 +268,7 @@ export function updateScheduleJob(db: Database, repoId: number, jobId: number, i
serializePermissionConfig(input.permissionConfig),
serializeMcpServers(input.mcpServers),
input.branch,
input.workspaceMode,
now,
input.nextRunAt,
repoId,
@@ -298,11 +304,15 @@ export function listScheduleRunArtifactsByJob(db: Database, repoId: number, jobI
}))
}
export function listActiveScheduleRunWorktreePaths(db: Database): string[] {
const rows = db
.prepare('SELECT worktree_path FROM schedule_runs WHERE worktree_path IS NOT NULL')
.all() as { worktree_path: string }[]
return rows.map((row) => row.worktree_path)
export function getScheduleJobRepoId(db: Database, jobId: number): number | null {
const row = db.prepare('SELECT repo_id FROM schedule_jobs WHERE id = ?').get(jobId) as { repo_id: number } | undefined
return row?.repo_id ?? null
}
export function clearScheduleRunWorktreePath(db: Database, repoId: number, jobId: number, worktreePath: string): number {
return db
.prepare('UPDATE schedule_runs SET worktree_path = NULL WHERE repo_id = ? AND job_id = ? AND worktree_path = ?')
.run(repoId, jobId, worktreePath).changes
}
export function deleteScheduleRunById(db: Database, repoId: number, jobId: number, runId: number): boolean {