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) {

View File

@@ -60,10 +60,11 @@ func New(cfg app.Config) http.Handler {
return
}
writeJSON(w, http.StatusOK, map[string]any{
"items": snapshot.Threads,
"edges": snapshot.SpawnEdges,
"goals": snapshot.Goals,
"source": snapshot.Source,
"items": snapshot.Threads,
"edges": snapshot.SpawnEdges,
"goals": snapshot.Goals,
"source": snapshot.Source,
"sources": snapshot.Sources,
})
})
mux.HandleFunc("/api/workflow/events", func(w http.ResponseWriter, r *http.Request) {

View File

@@ -1,6 +1,7 @@
package server
import (
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -9,6 +10,8 @@ import (
"testing"
"codex-agent-manager/internal/app"
_ "modernc.org/sqlite"
)
func TestAgentsEndpointReturnsAgentItems(t *testing.T) {
@@ -117,6 +120,48 @@ func TestRuntimeThreadsEndpointReturnsEmptyWhenSQLiteMissing(t *testing.T) {
}
}
func TestRuntimeThreadsEndpointReturnsPartialSourceEvidence(t *testing.T) {
root := t.TempDir()
db, err := sql.Open("sqlite", filepath.Join(root, "goals_1.sqlite"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE TABLE thread_goals (thread_id TEXT, goal TEXT, status TEXT, updated_at TEXT)`); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/api/runtime/threads", nil)
rec := httptest.NewRecorder()
New(app.Config{CodexHome: root, HTTPAddr: "127.0.0.1:0"}).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
}
var body struct {
Source struct {
Kind string `json:"kind"`
Confidence string `json:"confidence"`
} `json:"source"`
Sources map[string]struct {
Kind string `json:"kind"`
Confidence string `json:"confidence"`
} `json:"sources"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("invalid json: %v", err)
}
if body.Source.Kind != "sqlite_partial" || body.Source.Confidence != "partial" {
t.Fatalf("unexpected aggregate source: %#v", body.Source)
}
if body.Sources["state"].Kind != "sqlite_missing" || body.Sources["state"].Confidence != "low" {
t.Fatalf("unexpected state source: %#v", body.Sources["state"])
}
if body.Sources["goals"].Kind != "sqlite_readonly" || body.Sources["goals"].Confidence != "high" {
t.Fatalf("unexpected goals source: %#v", body.Sources["goals"])
}
}
func TestWorkflowEventsEndpointReturnsEvents(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "task_plan.md"), []byte("| 3 | in_progress | Runtime model |\n"), 0o644); err != nil {