package main import ( "context" "encoding/json" "fmt" "github.com/jackc/pgx/v5/pgxpool" "knowledge-governance/internal/appconfig" "knowledge-governance/internal/version" "os" "time" ) // Operator-only read-back in the existing API binary; no new public endpoint, // no session/credential creation and no migrations or remote forge access. func versionRecordStatus(ctx context.Context, getenv func(string) string) int { info := version.Snapshot() if !info.IdentityAvailable || info.CommitSHA == nil { return printVersionStatus(false, "build_identity_missing", "") } config, e := appconfig.Load(getenv) if e != nil { return printVersionStatus(false, "configuration_invalid", *info.CommitSHA) } ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() pool, e := pgxpool.New(ctx, config.DatabaseURL) if e != nil { return printVersionStatus(false, "database_unavailable", *info.CommitSHA) } defer pool.Close() tag := "snapshot/v" + info.DisplayVersion kind := "development" if info.BuildKind == "release" { tag = "v" + info.DisplayVersion kind = "release" } evidence, _ := json.Marshal([]map[string]any{{"name": tag, "annotated": true}}) var ready bool e = pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM version_history_commits c WHERE repo_key=$1 AND commit_sha=$2 AND version_label=$3 AND record_kind=$4 AND btrim(release_notes)<>'' AND release_tags @> $5::jsonb AND snapshot_id=(SELECT max(id) FROM version_history_sync_runs WHERE repo_key=$1 AND status='completed' AND complete))`, config.VersionHistoryRepo, *info.CommitSHA, info.DisplayVersion, kind, evidence).Scan(&ready) if e != nil { return printVersionStatus(false, "database_unavailable", *info.CommitSHA) } if !ready { return printVersionStatus(false, "version_record_pending", *info.CommitSHA) } return printVersionStatus(true, "verified", *info.CommitSHA) } func printVersionStatus(ready bool, code, sha string) int { raw, _ := json.Marshal(map[string]any{"ready": ready, "code": code, "commit_sha": sha}) fmt.Fprintln(os.Stdout, string(raw)) if ready { return 0 } return 1 }