fix: tolerate runtime schema drift

This commit is contained in:
Yoilun
2026-05-25 18:43:46 +08:00
parent bb8b8fe732
commit 69e1af7a17
8 changed files with 313 additions and 25 deletions

View File

@@ -44,11 +44,11 @@ func (s Store) Snapshot() (Snapshot, error) {
return Snapshot{}, err
}
defer db.Close()
snapshot.Threads, err = readThreads(db, statePath)
snapshot.Threads, err = readThreads(db, statePath, snapshot.Sources)
if err != nil {
return Snapshot{}, err
}
snapshot.SpawnEdges, err = readSpawnEdges(db, statePath)
snapshot.SpawnEdges, err = readSpawnEdges(db, statePath, snapshot.Sources)
if err != nil {
return Snapshot{}, err
}
@@ -59,7 +59,7 @@ func (s Store) Snapshot() (Snapshot, error) {
return Snapshot{}, err
}
defer db.Close()
snapshot.Goals, err = readGoals(db, goalsPath)
snapshot.Goals, err = readGoals(db, goalsPath, snapshot.Sources)
if err != nil {
return Snapshot{}, err
}
@@ -94,7 +94,7 @@ func aggregateSource(sources SourceMap) SourceEvidence {
}
return SourceEvidence{
Kind: "sqlite_partial",
Confidence: "partial",
Confidence: "medium",
Message: "One Codex SQLite source is missing; available data was read from existing sources only.",
}
}
@@ -108,8 +108,21 @@ func openReadonlySQLite(path string) (*sql.DB, error) {
return sql.Open("sqlite", uri.String())
}
func readThreads(db *sql.DB, sourcePath string) ([]Thread, error) {
rows, err := db.Query(`SELECT id, role, status, created_at, updated_at FROM threads ORDER BY created_at, id`)
func readThreads(db *sql.DB, sourcePath string, sources SourceMap) ([]Thread, error) {
columns, err := tableColumns(db, "threads")
if err != nil {
return nil, err
}
if len(columns) == 0 {
sources["threads"] = tableSource("sqlite_missing_table", sourcePath, "threads table was not found.")
return []Thread{}, nil
}
if !columns["id"] {
sources["threads"] = tableSource("sqlite_schema_drift", sourcePath, "threads table is missing required id column.")
return []Thread{}, nil
}
query := `SELECT ` + textColumn(columns, "id") + `, ` + textColumn(columns, "role") + `, ` + textColumn(columns, "status") + `, ` + textColumn(columns, "created_at") + `, ` + textColumn(columns, "updated_at") + ` FROM threads ORDER BY ` + orderBy(columns, "created_at", "id")
rows, err := db.Query(query)
if err != nil {
if isMissingTable(err) {
return []Thread{}, nil
@@ -126,11 +139,25 @@ func readThreads(db *sql.DB, sourcePath string) ([]Thread, error) {
item.Source = SourceEvidence{Kind: "sqlite_table", Path: sourcePath, Confidence: "high"}
threads = append(threads, item)
}
sources["threads"] = SourceEvidence{Kind: "sqlite_table", Path: sourcePath, Confidence: "high"}
return threads, rows.Err()
}
func readSpawnEdges(db *sql.DB, sourcePath string) ([]SpawnEdge, error) {
rows, err := db.Query(`SELECT from_thread_id, to_thread_id, reason, created_at FROM thread_spawn_edges ORDER BY created_at, from_thread_id, to_thread_id`)
func readSpawnEdges(db *sql.DB, sourcePath string, sources SourceMap) ([]SpawnEdge, error) {
columns, err := tableColumns(db, "thread_spawn_edges")
if err != nil {
return nil, err
}
if len(columns) == 0 {
sources["thread_spawn_edges"] = tableSource("sqlite_missing_table", sourcePath, "thread_spawn_edges table was not found.")
return []SpawnEdge{}, nil
}
if !columns["from_thread_id"] || !columns["to_thread_id"] {
sources["thread_spawn_edges"] = tableSource("sqlite_schema_drift", sourcePath, "thread_spawn_edges table is missing required endpoint columns.")
return []SpawnEdge{}, nil
}
query := `SELECT ` + textColumn(columns, "from_thread_id") + `, ` + textColumn(columns, "to_thread_id") + `, ` + textColumn(columns, "reason") + `, ` + textColumn(columns, "created_at") + ` FROM thread_spawn_edges ORDER BY ` + orderBy(columns, "created_at", "from_thread_id", "to_thread_id")
rows, err := db.Query(query)
if err != nil {
if isMissingTable(err) {
return []SpawnEdge{}, nil
@@ -147,11 +174,25 @@ func readSpawnEdges(db *sql.DB, sourcePath string) ([]SpawnEdge, error) {
item.Source = SourceEvidence{Kind: "sqlite_table", Path: sourcePath, Confidence: "high"}
edges = append(edges, item)
}
sources["thread_spawn_edges"] = SourceEvidence{Kind: "sqlite_table", Path: sourcePath, Confidence: "high"}
return edges, rows.Err()
}
func readGoals(db *sql.DB, sourcePath string) ([]Goal, error) {
rows, err := db.Query(`SELECT thread_id, goal, status, updated_at FROM thread_goals ORDER BY updated_at, thread_id`)
func readGoals(db *sql.DB, sourcePath string, sources SourceMap) ([]Goal, error) {
columns, err := tableColumns(db, "thread_goals")
if err != nil {
return nil, err
}
if len(columns) == 0 {
sources["thread_goals"] = tableSource("sqlite_missing_table", sourcePath, "thread_goals table was not found.")
return []Goal{}, nil
}
if !columns["thread_id"] {
sources["thread_goals"] = tableSource("sqlite_schema_drift", sourcePath, "thread_goals table is missing required thread_id column.")
return []Goal{}, nil
}
query := `SELECT ` + textColumn(columns, "thread_id") + `, ` + textColumn(columns, "goal") + `, ` + textColumn(columns, "status") + `, ` + textColumn(columns, "updated_at") + ` FROM thread_goals ORDER BY ` + orderBy(columns, "updated_at", "thread_id")
rows, err := db.Query(query)
if err != nil {
if isMissingTable(err) {
return []Goal{}, nil
@@ -168,9 +209,58 @@ func readGoals(db *sql.DB, sourcePath string) ([]Goal, error) {
item.Source = SourceEvidence{Kind: "sqlite_table", Path: sourcePath, Confidence: "high"}
goals = append(goals, item)
}
sources["thread_goals"] = SourceEvidence{Kind: "sqlite_table", Path: sourcePath, Confidence: "high"}
return goals, rows.Err()
}
func tableColumns(db *sql.DB, table string) (map[string]bool, error) {
rows, err := db.Query(`PRAGMA table_info(` + table + `)`)
if err != nil {
return nil, err
}
defer rows.Close()
columns := map[string]bool{}
for rows.Next() {
var cid sql.NullInt64
var name sql.NullString
var typ sql.NullString
var notNull sql.NullInt64
var defaultValue sql.NullString
var pk sql.NullInt64
if err := rows.Scan(&cid, &name, &typ, &notNull, &defaultValue, &pk); err != nil {
return nil, err
}
if name.Valid {
columns[name.String] = true
}
}
return columns, rows.Err()
}
func textColumn(columns map[string]bool, name string) string {
if !columns[name] {
return "''"
}
return "COALESCE(CAST(" + name + " AS TEXT), '')"
}
func orderBy(columns map[string]bool, names ...string) string {
var order []string
for _, name := range names {
if columns[name] {
order = append(order, name)
}
}
if len(order) == 0 {
return "rowid"
}
return strings.Join(order, ", ")
}
func tableSource(kind string, sourcePath string, message string) SourceEvidence {
return SourceEvidence{Kind: kind, Path: sourcePath, Confidence: "low", Message: message}
}
func fileExists(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()