mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat: add multi-run fusion and session change walkthroughs
Fuse two to five completed multi-run entries into one synthesized session, and explain a session's diff as ordered walkthrough stops cached by diff hash. - Multi-run fusion service, persistence, routes and FuseRunDialog - Change walkthrough service, persistence, routes and ChangesWalkthroughDialog - Shared unified-diff parser plus bounded text generation, json-extract, text-truncate and service-error helpers
This commit is contained in:
1 parent
90c6ccf236
commit
b36d412ca2
66 files changed
+6228
-468
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import { ChangeWalkthroughSchema, type ChangeWalkthrough } from '@opencode-manager/shared/schemas'
|
||||
|
||||
interface ChangeWalkthroughRow {
|
||||
session_id: string
|
||||
diff_hash: string
|
||||
payload: string
|
||||
created_at: number
|
||||
}
|
||||
|
||||
export function ensureChangeWalkthroughTable(db: Database): void {
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS change_walkthroughs (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
diff_hash TEXT NOT NULL,
|
||||
payload TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL
|
||||
)
|
||||
`)
|
||||
}
|
||||
|
||||
export function getChangeWalkthrough(db: Database, sessionId: string): ChangeWalkthrough | null {
|
||||
const row = db
|
||||
.prepare('SELECT session_id, diff_hash, payload, created_at FROM change_walkthroughs WHERE session_id = ?')
|
||||
.get(sessionId) as ChangeWalkthroughRow | undefined
|
||||
if (!row) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
return ChangeWalkthroughSchema.parse(JSON.parse(row.payload))
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function saveChangeWalkthrough(db: Database, walkthrough: ChangeWalkthrough): void {
|
||||
db.prepare(`
|
||||
INSERT INTO change_walkthroughs(session_id, diff_hash, payload, created_at)
|
||||
VALUES(?,?,?,?)
|
||||
ON CONFLICT(session_id) DO UPDATE SET
|
||||
diff_hash = excluded.diff_hash,
|
||||
payload = excluded.payload,
|
||||
created_at = excluded.created_at
|
||||
`).run(walkthrough.sessionId, walkthrough.diffHash, JSON.stringify(walkthrough), walkthrough.createdAt)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { Migration } from '../migration-runner'
|
||||
import { ensureMultiRunFusionTable } from '../multi-runs'
|
||||
|
||||
const migration: Migration = {
|
||||
id: '202610061700-multi-run-fusions',
|
||||
up(db) {
|
||||
ensureMultiRunFusionTable(db)
|
||||
},
|
||||
down(db) {
|
||||
db.run('DROP TABLE IF EXISTS multi_run_fusions')
|
||||
},
|
||||
}
|
||||
|
||||
export default migration
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { Migration } from '../migration-runner'
|
||||
import { ensureChangeWalkthroughTable } from '../change-walkthroughs'
|
||||
|
||||
const migration: Migration = {
|
||||
id: '202610061701-change-walkthroughs',
|
||||
up(db) {
|
||||
ensureChangeWalkthroughTable(db)
|
||||
},
|
||||
down(db) {
|
||||
db.run('DROP TABLE IF EXISTS change_walkthroughs')
|
||||
},
|
||||
}
|
||||
|
||||
export default migration
|
||||
@@ -27,6 +27,8 @@ 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'
|
||||
import migration202610061700 from './202610061700-multi-run-fusions'
|
||||
import migration202610061701 from './202610061701-change-walkthroughs'
|
||||
|
||||
export const allMigrations: Migration[] = [
|
||||
migration001,
|
||||
@@ -57,4 +59,6 @@ export const allMigrations: Migration[] = [
|
||||
migration026,
|
||||
migration027,
|
||||
migration202610061345,
|
||||
migration202610061700,
|
||||
migration202610061701,
|
||||
]
|
||||
+241
-17
@@ -1,5 +1,10 @@
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { MultiRunEntryStatus } from '@opencode-manager/shared/schemas'
|
||||
import {
|
||||
MultiRunFusionSourceSchema,
|
||||
type MultiRunEntryStatus,
|
||||
type MultiRunFusionSource,
|
||||
type MultiRunFusionStatus,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
|
||||
export interface MultiRunEntryRecord {
|
||||
id: number
|
||||
@@ -13,6 +18,23 @@ export interface MultiRunEntryRecord {
|
||||
updatedAt: number
|
||||
}
|
||||
|
||||
export interface MultiRunFusionRecord {
|
||||
id: number
|
||||
multiRunId: number
|
||||
requestId: string
|
||||
model: string
|
||||
instructions: string | null
|
||||
isolated: boolean
|
||||
baseRef: string | null
|
||||
sources: MultiRunFusionSource[]
|
||||
status: MultiRunFusionStatus
|
||||
sessionId: string | null
|
||||
directory: string | null
|
||||
error: string | null
|
||||
createdAt: number
|
||||
updatedAt: number
|
||||
}
|
||||
|
||||
export interface MultiRunRecord {
|
||||
id: number
|
||||
repoId: number
|
||||
@@ -22,6 +44,7 @@ export interface MultiRunRecord {
|
||||
baseRef: string | null
|
||||
createdAt: number
|
||||
entries: MultiRunEntryRecord[]
|
||||
fusions: MultiRunFusionRecord[]
|
||||
}
|
||||
|
||||
export interface CreateMultiRunGroup {
|
||||
@@ -39,6 +62,23 @@ export interface MultiRunEntryPatch {
|
||||
error?: string | null
|
||||
}
|
||||
|
||||
export interface CreateMultiRunFusionInput {
|
||||
multiRunId: number
|
||||
requestId: string
|
||||
model: string
|
||||
instructions: string | null
|
||||
isolated: boolean
|
||||
baseRef: string | null
|
||||
sources: MultiRunFusionSource[]
|
||||
}
|
||||
|
||||
export interface MultiRunFusionPatch {
|
||||
status?: MultiRunFusionStatus
|
||||
sessionId?: string | null
|
||||
directory?: string | null
|
||||
error?: string | null
|
||||
}
|
||||
|
||||
interface MultiRunRow {
|
||||
id: number
|
||||
repo_id: number
|
||||
@@ -61,10 +101,30 @@ interface MultiRunEntryRow {
|
||||
updated_at: number
|
||||
}
|
||||
|
||||
interface MultiRunFusionRow {
|
||||
id: number
|
||||
multi_run_id: number
|
||||
request_id: string
|
||||
model: string
|
||||
instructions: string | null
|
||||
isolated: number
|
||||
base_ref: string | null
|
||||
sources: string
|
||||
status: MultiRunFusionStatus
|
||||
session_id: string | null
|
||||
directory: string | null
|
||||
error: string | null
|
||||
created_at: number
|
||||
updated_at: number
|
||||
}
|
||||
|
||||
const MULTI_RUN_COLUMNS = 'id, repo_id, name, prompt, isolated, base_ref, created_at'
|
||||
|
||||
const MULTI_RUN_ENTRY_COLUMNS = 'id, multi_run_id, model, status, session_id, directory, error, created_at, updated_at'
|
||||
|
||||
const MULTI_RUN_FUSION_COLUMNS =
|
||||
'id, multi_run_id, request_id, model, instructions, isolated, base_ref, sources, status, session_id, directory, error, created_at, updated_at'
|
||||
|
||||
export function ensureMultiRunTables(db: Database): void {
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS multi_runs (
|
||||
@@ -100,6 +160,32 @@ export function ensureMultiRunTables(db: Database): void {
|
||||
`)
|
||||
}
|
||||
|
||||
export function ensureMultiRunFusionTable(db: Database): void {
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS multi_run_fusions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
multi_run_id INTEGER NOT NULL,
|
||||
request_id TEXT NOT NULL,
|
||||
model TEXT NOT NULL,
|
||||
instructions TEXT,
|
||||
isolated INTEGER NOT NULL,
|
||||
base_ref TEXT,
|
||||
sources TEXT NOT NULL,
|
||||
status TEXT NOT NULL CHECK(status IN ('starting','started','failed')),
|
||||
session_id TEXT,
|
||||
directory TEXT,
|
||||
error TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL,
|
||||
UNIQUE(multi_run_id, request_id)
|
||||
)
|
||||
`)
|
||||
db.run(`
|
||||
CREATE INDEX IF NOT EXISTS idx_multi_run_fusions_group
|
||||
ON multi_run_fusions(multi_run_id)
|
||||
`)
|
||||
}
|
||||
|
||||
export function createMultiRunWithEntries(
|
||||
db: Database,
|
||||
group: CreateMultiRunGroup,
|
||||
@@ -137,7 +223,7 @@ export function getMultiRun(db: Database, id: number): MultiRunRecord | null {
|
||||
if (!row) {
|
||||
return null
|
||||
}
|
||||
return toMultiRunRecord(row, loadEntries(db, row.id))
|
||||
return toMultiRunRecord(row, loadEntries(db, row.id), loadFusions(db, row.id))
|
||||
}
|
||||
|
||||
export function listMultiRuns(db: Database, repoId: number, limit: number): MultiRunRecord[] {
|
||||
@@ -170,7 +256,25 @@ export function listMultiRuns(db: Database, repoId: number, limit: number): Mult
|
||||
entriesByRun.get(entryRow.multi_run_id)?.push(entryRow)
|
||||
}
|
||||
|
||||
return rows.map((row) => toMultiRunRecord(row, entriesByRun.get(row.id) ?? []))
|
||||
const fusionRows = db
|
||||
.prepare(`
|
||||
SELECT ${MULTI_RUN_FUSION_COLUMNS} FROM multi_run_fusions
|
||||
WHERE multi_run_id IN (${placeholders})
|
||||
ORDER BY multi_run_id ASC, id ASC
|
||||
`)
|
||||
.all(...rows.map((row) => row.id)) as MultiRunFusionRow[]
|
||||
|
||||
const fusionsByRun = new Map<number, MultiRunFusionRow[]>()
|
||||
for (const row of rows) {
|
||||
fusionsByRun.set(row.id, [])
|
||||
}
|
||||
for (const fusionRow of fusionRows) {
|
||||
fusionsByRun.get(fusionRow.multi_run_id)?.push(fusionRow)
|
||||
}
|
||||
|
||||
return rows.map((row) =>
|
||||
toMultiRunRecord(row, entriesByRun.get(row.id) ?? [], fusionsByRun.get(row.id) ?? []),
|
||||
)
|
||||
}
|
||||
|
||||
export function getMultiRunEntry(db: Database, multiRunId: number, entryId: number): MultiRunEntryRecord | null {
|
||||
@@ -193,6 +297,106 @@ export function updateMultiRunEntry(
|
||||
fromStatuses: MultiRunEntryStatus[],
|
||||
patch: MultiRunEntryPatch,
|
||||
): MultiRunEntryRecord | null {
|
||||
if (!runGuardedStatusUpdate(db, 'multi_run_entries', entryId, fromStatuses, patch)) {
|
||||
return null
|
||||
}
|
||||
|
||||
const row = db.prepare('SELECT multi_run_id FROM multi_run_entries WHERE id = ?').get(entryId) as
|
||||
| { multi_run_id: number }
|
||||
| undefined
|
||||
if (!row) {
|
||||
return null
|
||||
}
|
||||
return getMultiRunEntry(db, row.multi_run_id, entryId)
|
||||
}
|
||||
|
||||
export function getMultiRunFusionByRequest(
|
||||
db: Database,
|
||||
multiRunId: number,
|
||||
requestId: string,
|
||||
): MultiRunFusionRecord | null {
|
||||
const row = db
|
||||
.prepare(`SELECT ${MULTI_RUN_FUSION_COLUMNS} FROM multi_run_fusions WHERE multi_run_id = ? AND request_id = ?`)
|
||||
.get(multiRunId, requestId) as MultiRunFusionRow | undefined
|
||||
if (!row) {
|
||||
return null
|
||||
}
|
||||
return mapFusionRow(row)
|
||||
}
|
||||
|
||||
export function insertMultiRunFusion(
|
||||
db: Database,
|
||||
input: CreateMultiRunFusionInput,
|
||||
): { fusion: MultiRunFusionRecord; created: boolean } {
|
||||
const now = Date.now()
|
||||
return db.transaction(() => {
|
||||
const result = db
|
||||
.prepare(`
|
||||
INSERT INTO multi_run_fusions(
|
||||
multi_run_id, request_id, model, instructions, isolated, base_ref, sources,
|
||||
status, session_id, directory, error, created_at, updated_at
|
||||
)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
ON CONFLICT(multi_run_id, request_id) DO NOTHING
|
||||
`)
|
||||
.run(
|
||||
input.multiRunId,
|
||||
input.requestId,
|
||||
input.model,
|
||||
input.instructions,
|
||||
input.isolated ? 1 : 0,
|
||||
input.baseRef,
|
||||
JSON.stringify(input.sources),
|
||||
'starting',
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
now,
|
||||
now,
|
||||
)
|
||||
const fusion = getMultiRunFusionByRequest(db, input.multiRunId, input.requestId)
|
||||
if (!fusion) {
|
||||
throw new Error('Failed to create multi-run fusion')
|
||||
}
|
||||
return { fusion, created: result.changes > 0 }
|
||||
})()
|
||||
}
|
||||
|
||||
export function updateMultiRunFusion(
|
||||
db: Database,
|
||||
fusionId: number,
|
||||
fromStatuses: MultiRunFusionStatus[],
|
||||
patch: MultiRunFusionPatch,
|
||||
): MultiRunFusionRecord | null {
|
||||
if (!runGuardedStatusUpdate(db, 'multi_run_fusions', fusionId, fromStatuses, patch)) {
|
||||
return null
|
||||
}
|
||||
|
||||
const row = db.prepare('SELECT multi_run_id, request_id FROM multi_run_fusions WHERE id = ?').get(fusionId) as
|
||||
| { multi_run_id: number; request_id: string }
|
||||
| undefined
|
||||
if (!row) {
|
||||
return null
|
||||
}
|
||||
return getMultiRunFusionByRequest(db, row.multi_run_id, row.request_id)
|
||||
}
|
||||
|
||||
type GuardedUpdateTable = 'multi_run_entries' | 'multi_run_fusions'
|
||||
|
||||
interface GuardedUpdatePatch {
|
||||
status?: string
|
||||
sessionId?: string | null
|
||||
directory?: string | null
|
||||
error?: string | null
|
||||
}
|
||||
|
||||
function runGuardedStatusUpdate(
|
||||
db: Database,
|
||||
table: GuardedUpdateTable,
|
||||
id: number,
|
||||
fromStatuses: readonly string[],
|
||||
patch: GuardedUpdatePatch,
|
||||
): boolean {
|
||||
const assignments: string[] = ['updated_at = ?']
|
||||
const values: (string | number | null)[] = [Date.now()]
|
||||
|
||||
@@ -215,20 +419,10 @@ export function updateMultiRunEntry(
|
||||
|
||||
const placeholders = fromStatuses.map(() => '?').join(', ')
|
||||
const result = db
|
||||
.prepare(`UPDATE multi_run_entries SET ${assignments.join(', ')} WHERE id = ? AND status IN (${placeholders})`)
|
||||
.run(...values, entryId, ...fromStatuses)
|
||||
.prepare(`UPDATE ${table} SET ${assignments.join(', ')} WHERE id = ? AND status IN (${placeholders})`)
|
||||
.run(...values, id, ...fromStatuses)
|
||||
|
||||
if (result.changes === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
const row = db.prepare('SELECT multi_run_id FROM multi_run_entries WHERE id = ?').get(entryId) as
|
||||
| { multi_run_id: number }
|
||||
| undefined
|
||||
if (!row) {
|
||||
return null
|
||||
}
|
||||
return getMultiRunEntry(db, row.multi_run_id, entryId)
|
||||
return result.changes > 0
|
||||
}
|
||||
|
||||
function loadEntries(db: Database, multiRunId: number): MultiRunEntryRow[] {
|
||||
@@ -237,7 +431,17 @@ function loadEntries(db: Database, multiRunId: number): MultiRunEntryRow[] {
|
||||
.all(multiRunId) as MultiRunEntryRow[]
|
||||
}
|
||||
|
||||
function toMultiRunRecord(row: MultiRunRow, entries: MultiRunEntryRow[]): MultiRunRecord {
|
||||
function loadFusions(db: Database, multiRunId: number): MultiRunFusionRow[] {
|
||||
return db
|
||||
.prepare(`SELECT ${MULTI_RUN_FUSION_COLUMNS} FROM multi_run_fusions WHERE multi_run_id = ? ORDER BY id ASC`)
|
||||
.all(multiRunId) as MultiRunFusionRow[]
|
||||
}
|
||||
|
||||
function toMultiRunRecord(
|
||||
row: MultiRunRow,
|
||||
entries: MultiRunEntryRow[],
|
||||
fusions: MultiRunFusionRow[],
|
||||
): MultiRunRecord {
|
||||
return {
|
||||
id: row.id,
|
||||
repoId: row.repo_id,
|
||||
@@ -247,6 +451,26 @@ function toMultiRunRecord(row: MultiRunRow, entries: MultiRunEntryRow[]): MultiR
|
||||
baseRef: row.base_ref,
|
||||
createdAt: row.created_at,
|
||||
entries: entries.map((entry) => mapEntryRow(entry, row.isolated === 1)),
|
||||
fusions: fusions.map(mapFusionRow),
|
||||
}
|
||||
}
|
||||
|
||||
function mapFusionRow(row: MultiRunFusionRow): MultiRunFusionRecord {
|
||||
return {
|
||||
id: row.id,
|
||||
multiRunId: row.multi_run_id,
|
||||
requestId: row.request_id,
|
||||
model: row.model,
|
||||
instructions: row.instructions,
|
||||
isolated: row.isolated === 1,
|
||||
baseRef: row.base_ref,
|
||||
sources: MultiRunFusionSourceSchema.array().parse(JSON.parse(row.sources)),
|
||||
status: row.status,
|
||||
sessionId: row.session_id,
|
||||
directory: row.directory,
|
||||
error: row.error,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -348,6 +348,7 @@ export function deleteRepo(db: Database, id: number): void {
|
||||
}
|
||||
|
||||
const remove = db.transaction(() => {
|
||||
db.prepare('DELETE FROM multi_run_fusions WHERE multi_run_id IN (SELECT id FROM multi_runs WHERE repo_id = ?)').run(id)
|
||||
db.prepare('DELETE FROM multi_run_entries WHERE multi_run_id IN (SELECT id FROM multi_runs WHERE repo_id = ?)').run(id)
|
||||
db.prepare('DELETE FROM multi_runs WHERE repo_id = ?').run(id)
|
||||
for (const table of TABLES_WITH_REPO_ID) {
|
||||
|
||||
Reference in new issue
Block a user