internal/infrastructure/configuration/setup.Run drives operator identity, LLM provider, models, forge, and Telegram chat channel prompts and returns TOML edits -- not a config.Config. Building a parallel in-memory representation to validate against was rejected during design review: it's a second source of truth about the exact thing (text vs. struct disagreeing) this whole feature exists to stop. Tests instead render the edits against the real embedded template and load the result through a real configuration.Loader, proving the actual defaults/decode/validate path accepts it. Two boot-time landmines in config.example.toml surfaced by that approach, both fixed here: - [providers.openai] ships active with api_key pointed at the bws engine. cmd/archied/provider_secrets.go resolves every entry in cfg.Providers at startup, not just the ones [models] references, and refuses to start the daemon if any fails -- so picking any other provider (or picking openai but skipping the key) left a config that wouldn't boot on a machine without the bws CLI installed (the common case). stepProvider now neutralises providers.openai.api_key to the zero-value secret ref whenever openai wasn't the actual choice. - [[repos]] shipped active with a placeholder owner/name that validates cleanly (owner/name are only checked non-empty) and would have archied silently polling a repo that doesn't exist. Commented out in the template with an explanation; setup does not write this section since tomlwrite cannot target array-of-tables. TestExampleConfigLoads updated to expect zero repos. Also: forge and provider secret env-var names are each defined in one table (forgeToken, cloudProviders) read by both the config edit and the secret sink, so the two can't name a secret differently -- the exact bug class this feature exists to close for the installer, tested directly (TestRun_ForgeTokenKeyMatchesStoredSecretKey compares the decoded key against the key the secret was actually stored under, not a hardcoded literal on both sides). Telegram writes chat.telegram. token_env, not a secret.SecretRef -- TelegramConfig has no such field. Telegram also requires at least one allowed_user_id: AllowedUserIDs is deny-by-default, so a bot token with none configured answers nobody, which reads as broken rather than as the safe default it is. SecretSink is a caller-supplied sink (Put buffers, Commit persists), not a return value: Run never hands back plaintext secrets, and the caller is expected to call Commit only after proving the generated config loads -- so a validation failure never leaves a secret written without the config that references it. Refs archie-core-cbk
110 lines
3.9 KiB
Go
110 lines
3.9 KiB
Go
// Package setup drives archied's interactive first-run configuration flow
|
|
// and produces the TOML edits to persist it. See the [Run] doc comment for
|
|
// why it returns edits rather than a config.Config: the whole point of this
|
|
// feature is to stop the config-writing code and the config-reading code
|
|
// disagreeing about the schema, and building a second, parallel
|
|
// representation to validate against would reintroduce exactly that risk
|
|
// inside setup itself.
|
|
package setup
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite"
|
|
)
|
|
|
|
// SecretSink is where setup sends the secret values a step collects. It is
|
|
// declared here, not implemented: setup edits config.toml keys, it does not
|
|
// write the env file itself. Put is expected to buffer rather than write
|
|
// immediately -- the caller commits only once the config text setup
|
|
// produced has been proven loadable, so a validation failure after some
|
|
// secrets were already prompted for never leaves a secret written without
|
|
// the config that references it, or vice versa.
|
|
type SecretSink interface {
|
|
// Put records that key (as looked up through engine) must resolve to
|
|
// value once Commit is called.
|
|
Put(engine, key, value string) error
|
|
// Commit persists everything Put has recorded so far.
|
|
Commit() error
|
|
}
|
|
|
|
// ModelDiscovery finds locally available models for the self-hosted
|
|
// provider path. A nil ModelDiscovery, or one that returns an error,
|
|
// degrades to a free-text model name -- discovery is a convenience, not a
|
|
// requirement.
|
|
type ModelDiscovery interface {
|
|
ListOllamaModels(ctx context.Context) ([]string, error)
|
|
}
|
|
|
|
// ExistingValues prefills prompts on a re-run. The zero value is correct
|
|
// for a fresh install.
|
|
type ExistingValues struct {
|
|
BotUser string
|
|
Operator string
|
|
ForgeHost string
|
|
}
|
|
|
|
// tableEdits groups pending edits by table before Run flattens them into
|
|
// []tomlwrite.Edit, so a step can build "everything for [forge]" as one
|
|
// value and let Run decide the flattening order.
|
|
type tableEdits = map[string]map[string]string
|
|
|
|
// Run drives the interactive setup flow and returns the TOML edits to
|
|
// apply. It does not read or write any file itself: the caller renders the
|
|
// edits (tomlwrite.Generate against a fresh template, or tomlwrite.Apply
|
|
// against an existing config's bytes for a re-run) and must prove the
|
|
// result loads through a real configuration.Loader before installing it or
|
|
// calling secrets.Commit.
|
|
func Run(ctx context.Context, p Prompter, discovery ModelDiscovery, secrets SecretSink, existing ExistingValues) ([]tomlwrite.Edit, error) {
|
|
var edits []tomlwrite.Edit
|
|
add := func(all tableEdits) {
|
|
for table, kv := range all {
|
|
for k, v := range kv {
|
|
edits = append(edits, tomlwrite.Edit{Table: table, Key: k, Value: v})
|
|
}
|
|
}
|
|
}
|
|
|
|
botUser, err := p.ReadLine(ctx, "Bot user (forge username for archied's commits and API calls): ", existing.BotUser)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("setup: bot user: %w", err)
|
|
}
|
|
if strings.TrimSpace(botUser) == "" {
|
|
return nil, fmt.Errorf("setup: bot user is required")
|
|
}
|
|
add(tableEdits{"": {"bot_user": tomlwrite.String(botUser)}})
|
|
|
|
operator, err := p.ReadLine(ctx, "Operator display name (shown to the chat agent; blank to skip): ", existing.Operator)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("setup: operator: %w", err)
|
|
}
|
|
if strings.TrimSpace(operator) != "" {
|
|
add(tableEdits{"chat": {"operator": tomlwrite.String(operator)}})
|
|
}
|
|
|
|
providerEdits, model, err := stepProvider(ctx, p, discovery, secrets)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
add(providerEdits)
|
|
for _, role := range []string{"triage", "planner", "builder"} {
|
|
add(tableEdits{"models": {role: tomlwrite.String(model)}})
|
|
}
|
|
|
|
forgeEdits, err := stepForge(ctx, p, secrets, existing.ForgeHost)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
add(forgeEdits)
|
|
|
|
chatEdits, err := stepChat(ctx, p, secrets)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
add(chatEdits)
|
|
|
|
return edits, nil
|
|
}
|