package pi import ( "context" "crypto/sha256" "encoding/json" "errors" "fmt" "io" "strings" "github.com/aritmolab/thothii/tools/thothctl/internal/compose" ) // Runner is the narrow, shell-free command boundary shared with thothctl. type Runner interface { Run(context.Context, []string, io.Reader) (compose.Result, error) } // Status reports the image-bundled Pi version without using a host Pi executable. func Status(ctx context.Context, runner Runner) (string, error) { result, err := runCompose(ctx, runner, "exec", "-T", "core", "pi", "--version") if err != nil { return "", commandError("Pi version check", result, err) } version := strings.TrimSpace(result.Stdout) if version == "" { return "", errors.New("Pi version check returned no version") } return version, nil } // Doctor verifies the installation-side invariants Pi needs before an update. func Doctor(ctx context.Context, runner Runner) error { if _, err := renderedCore(ctx, runner); err != nil { return err } if _, err := Status(ctx, runner); err != nil { return err } for _, check := range [][]string{ {"exec", "-T", "core", "sh", "-ceu", "test -w /home/thoth/.pi"}, {"exec", "-T", "core", "sh", "-ceu", "test -r /home/thoth/.pi/agent/auth.json"}, {"exec", "-T", "core", "curl", "-fsS", "http://127.0.0.1:8787/health"}, } { result, err := runCompose(ctx, runner, check...) if err != nil { return commandError("Pi preflight check", result, err) } } return nil } // Test performs the pre-Task-8 composite smoke through core's private loopback endpoint. func Test(ctx context.Context, runner Runner) error { if _, err := Status(ctx, runner); err != nil { return err } for _, path := range []string{"health", "models", "settings"} { result, err := runCompose(ctx, runner, "exec", "-T", "core", "curl", "-fsS", "http://127.0.0.1:8787/"+path) if err != nil { return commandError("Pi smoke check", result, err) } var payload any if err := json.Unmarshal([]byte(result.Stdout), &payload); err != nil { return fmt.Errorf("Pi smoke check returned invalid %s response", path) } if _, ok := payload.(map[string]any); !ok { return fmt.Errorf("Pi smoke check returned invalid %s response", path) } } return nil } func renderedCore(ctx context.Context, runner Runner) (Image, error) { result, err := runCompose(ctx, runner, "config", "--format", "json") if err != nil { return Image{}, commandError("Compose configuration check", result, err) } var document struct { Services map[string]struct { Image string `json:"image"` Environment map[string]any `json:"environment"` } `json:"services"` } if err := json.Unmarshal([]byte(result.Stdout), &document); err != nil { return Image{}, errors.New("Compose returned invalid rendered configuration") } core, exists := document.Services["core"] if !exists || core.Image == "" { return Image{}, errors.New("rendered Compose configuration has no core image") } endpoint, exists := core.Environment["THT_LLM_URL"].(string) if !exists || strings.TrimSpace(endpoint) == "" { return Image{}, errors.New("THT_LLM_URL must be configured before Pi lifecycle operations") } digest := sha256.Sum256([]byte(result.Stdout)) return Image{Reference: core.Image, ConfigurationSHA: fmt.Sprintf("%x", digest[:])}, nil } func runCompose(ctx context.Context, runner Runner, args ...string) (compose.Result, error) { return runner.Run(ctx, append([]string{"compose"}, args...), nil) } func commandError(label string, result compose.Result, err error) error { if result.ExitCode != 0 { return commandFailure{message: fmt.Sprintf("%s failed (exit %d)", label, result.ExitCode), exitCode: result.ExitCode} } return commandFailure{message: fmt.Sprintf("%s failed", label)} } type commandFailure struct { message string exitCode int } func (e commandFailure) Error() string { return e.message } // ExitCode exposes a Docker child exit code without exposing its output. func (e commandFailure) ExitCode() int { return e.exitCode }