fix: restore go compatibility for runtime models

This commit is contained in:
Yoilun
2026-05-25 18:32:10 +08:00
parent d573bde194
commit bb8b8fe732
11 changed files with 262 additions and 60 deletions

View File

@@ -5,8 +5,11 @@ type Snapshot struct {
SpawnEdges []SpawnEdge `json:"spawnEdges"`
Goals []Goal `json:"goals"`
Source SourceEvidence `json:"source"`
Sources SourceMap `json:"sources"`
}
type SourceMap map[string]SourceEvidence
type Thread struct {
ID string `json:"id"`
Role string `json:"role"`

View File

@@ -0,0 +1,45 @@
package runtime
import (
"os"
"path/filepath"
"runtime"
"testing"
)
func TestModuleKeepsGo122Compatibility(t *testing.T) {
if runtime.Version() == "" {
t.Fatal("runtime version unavailable")
}
data, err := os.ReadFile(filepath.Join("..", "..", "go.mod"))
if err != nil {
t.Fatal(err)
}
if !containsLine(string(data), "go 1.22") {
t.Fatalf("go.mod must keep Go 1.22 compatibility, got:\n%s", data)
}
}
func containsLine(input string, want string) bool {
for _, line := range splitLines(input) {
if line == want {
return true
}
}
return false
}
func splitLines(input string) []string {
var lines []string
start := 0
for i, char := range input {
if char == '\n' {
lines = append(lines, input[start:i])
start = i + 1
}
}
if start <= len(input) {
lines = append(lines, input[start:])
}
return lines
}

View File

@@ -27,24 +27,16 @@ func (s Store) Snapshot() (Snapshot, error) {
}
stateExists := fileExists(statePath)
goalsExists := fileExists(goalsPath)
if !stateExists && !goalsExists {
return Snapshot{
Threads: []Thread{},
SpawnEdges: []SpawnEdge{},
Goals: []Goal{},
Source: SourceEvidence{
Kind: "sqlite_missing",
Confidence: "low",
Message: "Codex SQLite files were not found; returning an empty read-only snapshot.",
},
}, nil
sources := SourceMap{
"state": sqliteSource("state", statePath, stateExists),
"goals": sqliteSource("goals", goalsPath, goalsExists),
}
snapshot := Snapshot{
Threads: []Thread{},
SpawnEdges: []SpawnEdge{},
Goals: []Goal{},
Source: SourceEvidence{Kind: "sqlite_readonly", Path: statePath, Confidence: "high"},
Source: aggregateSource(sources),
Sources: sources,
}
if stateExists {
db, err := openReadonlySQLite(statePath)
@@ -75,6 +67,38 @@ func (s Store) Snapshot() (Snapshot, error) {
return snapshot, nil
}
func sqliteSource(name string, path string, exists bool) SourceEvidence {
if exists {
return SourceEvidence{Kind: "sqlite_readonly", Path: path, Confidence: "high"}
}
return SourceEvidence{
Kind: "sqlite_missing",
Path: path,
Confidence: "low",
Message: name + " SQLite file was not found; returning empty data for that source.",
}
}
func aggregateSource(sources SourceMap) SourceEvidence {
state := sources["state"]
goals := sources["goals"]
if state.Kind == "sqlite_readonly" && goals.Kind == "sqlite_readonly" {
return SourceEvidence{Kind: "sqlite_readonly", Confidence: "high"}
}
if state.Kind == "sqlite_missing" && goals.Kind == "sqlite_missing" {
return SourceEvidence{
Kind: "sqlite_missing",
Confidence: "low",
Message: "Codex SQLite files were not found; returning an empty read-only snapshot.",
}
}
return SourceEvidence{
Kind: "sqlite_partial",
Confidence: "partial",
Message: "One Codex SQLite source is missing; available data was read from existing sources only.",
}
}
func openReadonlySQLite(path string) (*sql.DB, error) {
uri := url.URL{Scheme: "file", Path: path}
query := uri.Query()

View File

@@ -2,6 +2,7 @@ package runtime
import (
"database/sql"
"os"
"path/filepath"
"testing"
@@ -19,6 +20,12 @@ func TestStoreMissingSQLiteReturnsEmptySnapshot(t *testing.T) {
if snapshot.Source.Confidence != "low" || snapshot.Source.Kind != "sqlite_missing" {
t.Fatalf("unexpected source evidence: %#v", snapshot.Source)
}
if source := snapshot.Sources["state"]; source.Kind != "sqlite_missing" || source.Confidence != "low" {
t.Fatalf("unexpected state source evidence: %#v", source)
}
if source := snapshot.Sources["goals"]; source.Kind != "sqlite_missing" || source.Confidence != "low" {
t.Fatalf("unexpected goals source evidence: %#v", source)
}
}
func TestStoreReadsThreadsEdgesAndGoalsFromReadonlySQLite(t *testing.T) {
@@ -44,6 +51,68 @@ func TestStoreReadsThreadsEdgesAndGoalsFromReadonlySQLite(t *testing.T) {
if snapshot.Source.Confidence != "high" || snapshot.Source.Kind != "sqlite_readonly" {
t.Fatalf("unexpected source evidence: %#v", snapshot.Source)
}
if source := snapshot.Sources["state"]; source.Kind != "sqlite_readonly" || source.Confidence != "high" {
t.Fatalf("unexpected state source evidence: %#v", source)
}
if source := snapshot.Sources["goals"]; source.Kind != "sqlite_readonly" || source.Confidence != "high" {
t.Fatalf("unexpected goals source evidence: %#v", source)
}
}
func TestStoreMarksStateMissingWhenOnlyGoalsSQLiteExists(t *testing.T) {
root := t.TempDir()
createRuntimeSQLite(t, root)
if err := os.Remove(filepath.Join(root, "state_5.sqlite")); err != nil {
t.Fatal(err)
}
snapshot, err := Store{CodexHome: root}.Snapshot()
if err != nil {
t.Fatalf("Snapshot returned error: %v", err)
}
if len(snapshot.Threads) != 0 || len(snapshot.SpawnEdges) != 0 {
t.Fatalf("expected no state-backed data, got threads=%#v edges=%#v", snapshot.Threads, snapshot.SpawnEdges)
}
if len(snapshot.Goals) != 1 {
t.Fatalf("goals = %#v, want one goal from goals DB", snapshot.Goals)
}
if source := snapshot.Sources["state"]; source.Kind != "sqlite_missing" || source.Confidence != "low" {
t.Fatalf("unexpected state source evidence: %#v", source)
}
if source := snapshot.Sources["goals"]; source.Kind != "sqlite_readonly" || source.Confidence != "high" {
t.Fatalf("unexpected goals source evidence: %#v", source)
}
if snapshot.Source.Confidence != "partial" || snapshot.Source.Kind != "sqlite_partial" {
t.Fatalf("unexpected aggregate source evidence: %#v", snapshot.Source)
}
}
func TestStoreMarksGoalsMissingWhenOnlyStateSQLiteExists(t *testing.T) {
root := t.TempDir()
createRuntimeSQLite(t, root)
if err := os.Remove(filepath.Join(root, "goals_1.sqlite")); err != nil {
t.Fatal(err)
}
snapshot, err := Store{CodexHome: root}.Snapshot()
if err != nil {
t.Fatalf("Snapshot returned error: %v", err)
}
if len(snapshot.Threads) != 2 || len(snapshot.SpawnEdges) != 1 {
t.Fatalf("expected state-backed data, got threads=%#v edges=%#v", snapshot.Threads, snapshot.SpawnEdges)
}
if len(snapshot.Goals) != 0 {
t.Fatalf("goals = %#v, want no goals", snapshot.Goals)
}
if source := snapshot.Sources["state"]; source.Kind != "sqlite_readonly" || source.Confidence != "high" {
t.Fatalf("unexpected state source evidence: %#v", source)
}
if source := snapshot.Sources["goals"]; source.Kind != "sqlite_missing" || source.Confidence != "low" {
t.Fatalf("unexpected goals source evidence: %#v", source)
}
if snapshot.Source.Confidence != "partial" || snapshot.Source.Kind != "sqlite_partial" {
t.Fatalf("unexpected aggregate source evidence: %#v", snapshot.Source)
}
}
func createRuntimeSQLite(t *testing.T, root string) {