fix(indexing): surface index config properly and stop silent degradation

Follow-up to c924281, which introduced two defects of its own.

1. indexing.Config was a private config island: index paths existed nowhere
   in internal/config or config.example.toml, so they were undiscoverable
   and unsettable by anyone reading the real configuration.

   Now [indexing] in config.toml, defined in its own internal/config/
   indexer.go rather than appended to the 21-struct config.go, and its
   defaulting lives with it as IndexingConfig.withDefaults so finalize
   delegates instead of growing. Paths default off work_dir, keeping the
   two archied instances on one host from sharing an index. Documented in
   config.example.toml.

   internal/indexing keeps its own plain Config and does NOT import
   internal/config, so it stays a straight diff against its tau original.

2. A failed index build was invisible. refreshAsync wrote the reason to a
   database column with no reader and degraded to unindexed search in
   silence — the failure mode that costs hours precisely because nothing
   appears to be wrong. Every give-up path now logs, and a successful
   build logs once.

The logger is resolved at the point of use, not in the constructor: a
Manager can be built as a struct literal (the ported tests do), and
constructor-only defaulting panicked on the build goroutine, where a panic
takes the daemon with it. Regression test covers the zero and nil Manager.
This commit is contained in:
sam committed 2026-07-28 01:37:19 +10:00
1 parent 446dc6b3de
commit 05cfc30da3
8 files changed
+145 -13

No files matched your search

+3
View File
@@ -333,6 +333,8 @@ type Config struct {
// Tools holds MCP server and tool policy configuration (from config.tools.yaml).
Tools ToolsConfig `toml:"tools" yaml:"tools"`
// Indexing locates the workspace codesearch index.
Indexing IndexingConfig `toml:"indexing" yaml:"indexing"`
// Identities declares multi-identity configurations. When non-empty,
// each identity runs its own poll loop with its own forge client,
@@ -564,6 +566,7 @@ func finalize(cfg Config) (Config, error) {
if cfg.DBPath == "" {
cfg.DBPath = filepath.Join(dataHome(), "archie", "archie.db")
}
cfg.Indexing = cfg.Indexing.withDefaults(cfg.WorkDir)
if cfg.PollInterval == 0 {
cfg.PollInterval = Duration(60 * time.Second)
}
+49
View File
@@ -833,3 +833,52 @@ func LoadBytes(data []byte) (Config, error) {
_ = tmp.Close()
return Load(tmp.Name())
}
func TestFinalizeIndexingDefaults(t *testing.T) {
tests := []struct {
name string
extra string
wantIndex string
wantDBIsSet bool
}{
{
name: "defaults derive from work_dir",
// Both paths must land under the daemon's own work_dir so two
// archied instances on one host cannot share an index.
extra: "\nwork_dir = \"/srv/archie/work\"\n",
wantIndex: filepath.Join("/srv/archie/work", "indexes"),
wantDBIsSet: true,
},
{
name: "explicit index_dir is preserved",
extra: "\nwork_dir = \"/srv/archie/work\"\n" +
"[indexing]\nindex_dir = \"/mnt/fast/idx\"\n",
wantIndex: "/mnt/fast/idx",
wantDBIsSet: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
// tc.extra precedes the table sections: a top-level key written
// after [[repos]] would be parsed as a member of that table.
contents := "bot_user = \"widget\"\n" + tc.extra +
"[forge]\ntype = \"gitea\"\nhost = \"https://git.example.test\"\ntoken_env = \"T\"\n" +
"[[repos]]\nowner = \"acme\"\nname = \"app\"\n"
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
t.Fatal(err)
}
cfg, err := Load(path)
if err != nil {
t.Fatal(err)
}
if cfg.Indexing.IndexDir != tc.wantIndex {
t.Errorf("Indexing.IndexDir = %q, want %q", cfg.Indexing.IndexDir, tc.wantIndex)
}
if tc.wantDBIsSet && cfg.Indexing.DBPath == "" {
t.Error("Indexing.DBPath is empty; want a default derived from work_dir")
}
})
}
}
+29
View File
@@ -0,0 +1,29 @@
package config
import "path/filepath"
// IndexingConfig locates the workspace codesearch index.
//
// The index is a large, fully rebuildable artifact, so both paths are
// settable to allow putting it on different storage from the task work tree.
type IndexingConfig struct {
// IndexDir holds the codesearch sidecar files.
IndexDir string `toml:"index_dir" yaml:"index_dir" json:"index_dir,omitempty"`
// DBPath is the SQLite database holding index lifecycle metadata.
DBPath string `toml:"db_path" yaml:"db_path" json:"db_path,omitempty"`
}
// withDefaults fills unset paths from the daemon's work directory.
//
// Deriving from workDir rather than a shared data home is deliberate: two
// archied instances run on one host, each with its own work_dir, so this
// keeps their indexes separate without either having to configure anything.
func (c IndexingConfig) withDefaults(workDir string) IndexingConfig {
if c.IndexDir == "" {
c.IndexDir = filepath.Join(workDir, "indexes")
}
if c.DBPath == "" {
c.DBPath = filepath.Join(workDir, "workspace-indexes.db")
}
return c
}
+37 -12
View File
@@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
@@ -58,6 +59,17 @@ type Manager struct {
indexPath string
dbPath string
runner helperRunner
log *slog.Logger
}
// logger is nil-safe: a Manager may be constructed as a struct literal
// (the tests do), so defaulting at the point of use rather than in the
// constructor is what actually holds.
func (m *Manager) logger() *slog.Logger {
if m == nil || m.log == nil {
return slog.Default()
}
return m.log
}
type helperRunner interface {
@@ -69,22 +81,20 @@ type processRunner struct{}
// Config locates the index storage for a Manager.
//
// tau derived these from process-global helpers. archied owns its paths in
// daemon config and passes them down, so they are parameters here: two
// daemons on one host (archie and winter) must not share one index.
// tau derived these from process-global helpers. archied resolves them in
// its own config layer ([indexing] in config.toml, defaulted off work_dir by
// config.finalize) and passes them down. Defaulting deliberately does NOT
// live here: this package stays free of archie-core's config so it remains
// a straight diff against its tau original.
type Config struct {
// IndexDir holds the mmap-friendly codesearch sidecar files.
IndexDir string
// DBPath is the SQLite database holding index lifecycle metadata.
DBPath string
}
// DefaultConfig derives index paths from a daemon work directory.
func DefaultConfig(workDir string) Config {
return Config{
IndexDir: filepath.Join(workDir, "indexes"),
DBPath: filepath.Join(workDir, "workspace-indexes.db"),
}
// Log receives index build failures. Without it a failed build is
// invisible: the manager degrades to unindexed search and the reason
// is only ever written to a database column nobody reads.
Log *slog.Logger
}
// NewManager prepares lifecycle storage and starts an asynchronous atomic
@@ -108,6 +118,7 @@ func NewManager(ctx context.Context, cfg Config, root string) (*Manager, error)
indexPath: filepath.Join(cfg.IndexDir, workspaceKey(absRoot)+".csearch"),
dbPath: cfg.DBPath,
runner: processRunner{},
log: cfg.Log,
}
if err := m.ensureSchema(ctx); err != nil {
return nil, err
@@ -205,7 +216,15 @@ func (m *Manager) refreshAsync(parent context.Context) {
buildID := uuid.NewString()
previous, _ := m.state(ctx)
claimed, err := m.claimBuild(ctx, startedAt, buildID)
if err != nil || !claimed {
if err != nil {
// Degrading to unindexed search is safe but slow, and until
// this is logged the only record of why is a database column
// with no reader.
m.logger().Warn("workspace index build not claimed", "root", m.root, "error", err)
return
}
if !claimed {
// Another process holds the build claim; not an error.
return
}
finalPath := m.indexPath + "." + buildID
@@ -213,18 +232,24 @@ func (m *Manager) refreshAsync(parent context.Context) {
_ = os.Remove(tmp)
if err := m.runner.Build(ctx, m.root, tmp); err != nil {
_ = os.Remove(tmp)
m.logger().Warn("workspace index build failed; falling back to unindexed search",
"root", m.root, "error", err)
m.finishBuild(parent, buildID, "error", time.Time{}, "", err.Error())
return
}
if err := os.Rename(tmp, finalPath); err != nil {
_ = os.Remove(tmp)
m.logger().Warn("workspace index could not be published",
"root", m.root, "error", err)
m.finishBuild(parent, buildID, "error", time.Time{}, "", err.Error())
return
}
if !m.finishBuild(parent, buildID, "ready", startedAt, finalPath, "") {
_ = os.Remove(finalPath)
m.logger().Warn("workspace index build superseded before publication", "root", m.root)
return
}
m.logger().Info("workspace index ready", "root", m.root, "index", finalPath)
if previous.IndexPath != "" && previous.IndexPath != finalPath &&
(previous.IndexPath == m.indexPath || strings.HasPrefix(previous.IndexPath, m.indexPath+".")) {
_ = os.Remove(previous.IndexPath)
+21
View File
@@ -248,3 +248,24 @@ func runGit(t *testing.T, dir string, args ...string) {
t.Fatalf("git %v: %v: %s", args, err, out)
}
}
// A Manager built as a struct literal has no logger. Logging on the build
// path must not panic, since that path runs in a background goroutine where
// a panic takes the whole daemon down.
func TestManagerLoggerIsNilSafe(t *testing.T) {
tests := []struct {
name string
m *Manager
}{
{name: "zero manager", m: &Manager{}},
{name: "nil manager", m: nil},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := tc.m.logger(); got == nil {
t.Fatal("logger() returned nil")
}
tc.m.logger().Warn("must not panic")
})
}
}