diff --git a/internal/app/archied/readiness.go b/internal/app/archied/readiness.go index 2fc90663a..507efaada 100644 --- a/internal/app/archied/readiness.go +++ b/internal/app/archied/readiness.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "net/http" + "os" "path/filepath" "strings" "time" @@ -33,7 +34,7 @@ func (b *boot) setupReadinessProbes() { probes := []health.Probe{ readiness.NewStoreProbe(b.stateStore), readiness.NewConfigProbe(b.cfgHolder.Get, configuration.Validate), - readiness.NewDiskProbe(diskProbePath(cfg)), + readiness.NewDiskProbeTargets(diskProbeTargets(cfg)), readiness.NewModelProbe(b.chatModels.ActiveModel, b.chatModels.Models, modelReachProbe(cfg, b.chatModels.ActiveModel)), readiness.NewGatewayProbe( func() []readiness.ChannelState { return channelStates(b.channelManager) }, @@ -75,9 +76,20 @@ func sessionCount(ctx context.Context, chat *webui.ChatService) int { return len(snapshot.Sessions) } -// diskProbePath resolves the filesystem the readiness disk probe inspects: -// the store database directory first (the data that must fit on disk), then -// the working directory, then the process cwd. +// diskProbeTargets returns the filesystems whose capacity matters to the +// daemon. Root is always required; /var is useful on conventional Linux +// installations but optional so it does not make other environments fail. +func diskProbeTargets(cfg config.Config) []readiness.DiskTarget { + targets := []readiness.DiskTarget{{Name: "root", Path: "/"}} + if info, err := os.Stat("/var"); err == nil && info.IsDir() { + targets = append(targets, readiness.DiskTarget{Name: "var", Path: "/var", Optional: true}) + } + targets = append(targets, readiness.DiskTarget{Name: "data", Path: diskProbePath(cfg)}) + return targets +} + +// diskProbePath preserves the database/work/cwd fallback used by the daemon's +// disk target: the store database directory first, then work directory, then cwd. func diskProbePath(cfg config.Config) string { switch { case cfg.DBPath != "": diff --git a/internal/infrastructure/readiness/probes.go b/internal/infrastructure/readiness/probes.go index 0104da2f6..c0a6527ae 100644 --- a/internal/infrastructure/readiness/probes.go +++ b/internal/infrastructure/readiness/probes.go @@ -12,7 +12,10 @@ package readiness import ( "context" + "errors" "fmt" + "os" + "strings" "syscall" "time" @@ -130,42 +133,95 @@ func (p *ConfigProbe) Check(ctx context.Context) health.Result { // requirement of the readiness epic. const DefaultDiskThreshold = 0.90 -// DiskProbe checks the filesystem holding Path is below the used threshold. -type DiskProbe struct { - Path string - Threshold float64 +// DiskTarget is one filesystem location to measure. Optional targets are +// ignored when they disappear (for example, /var on a non-Linux installation). +type DiskTarget struct { + Name string + Path string + Optional bool } -// NewDiskProbe returns a disk probe for the directory Path, degraded when -// used usage exceeds the default 90% threshold. +// DiskProbe checks each configured filesystem independently and aggregates the +// results. Statfs is injectable so callers can test disk boundaries without +// filling a filesystem. +type DiskProbe struct { + // Path is retained for source compatibility with the original single-path + // probe. Targets takes precedence when non-empty. + Path string + Targets []DiskTarget + Threshold float64 + Statfs func(string, *syscall.Statfs_t) error +} + +// NewDiskProbe returns a single-path disk probe. func NewDiskProbe(path string) *DiskProbe { - return &DiskProbe{Path: path, Threshold: DefaultDiskThreshold} + return NewDiskProbeTargets([]DiskTarget{{Name: "work", Path: path}}) +} + +// NewDiskProbeTargets returns a disk probe for the supplied, stable-ordered targets. +func NewDiskProbeTargets(targets []DiskTarget) *DiskProbe { + return &DiskProbe{Targets: append([]DiskTarget(nil), targets...), Threshold: DefaultDiskThreshold, Statfs: syscall.Statfs} } func (p *DiskProbe) Name() string { return "disk" } func (p *DiskProbe) Check(ctx context.Context) health.Result { - if p.Path == "" { - return health.Result{Status: health.StatusDegraded, Detail: "no disk path configured"} + _ = ctx + targets := p.Targets + if len(targets) == 0 && p.Path != "" { + targets = []DiskTarget{{Name: "work", Path: p.Path}} } - var stat syscall.Statfs_t - if err := syscall.Statfs(p.Path, &stat); err != nil { - return health.Result{Status: health.StatusDegraded, Detail: "statfs failed: " + err.Error()} + if len(targets) == 0 { + return health.Result{Status: health.StatusDegraded, Detail: "no disk targets configured"} } - if stat.Blocks == 0 { - return health.Result{Status: health.StatusDegraded, Detail: "cannot size filesystem"} + statfs := p.Statfs + if statfs == nil { + statfs = syscall.Statfs } - // Used fraction uses available (bavail) rather than free (bfree) so a - // filesystem with reserved blocks is not reported as safer than it is. - used := stat.Blocks - stat.Bavail - pct := float64(used) / float64(stat.Blocks) - if pct >= p.Threshold { - return health.Result{ - Status: health.StatusDegraded, - Detail: fmt.Sprintf("%.0f%% used (limit %.0f%%)", pct*100, p.Threshold*100), + var details []string + degraded := false + seen := make(map[string]bool) + for _, target := range targets { + if target.Path == "" || seen[target.Path] { + continue } + seen[target.Path] = true + var stat syscall.Statfs_t + if err := statfs(target.Path, &stat); err != nil { + if target.Optional && errors.Is(err, os.ErrNotExist) { + continue + } + degraded = true + details = append(details, fmt.Sprintf("%s (%s): statfs failed: %v", target.Name, target.Path, err)) + continue + } + if stat.Blocks == 0 { + degraded = true + details = append(details, fmt.Sprintf("%s (%s): cannot size filesystem", target.Name, target.Path)) + continue + } + used := stat.Blocks - stat.Bavail + pct := float64(used) / float64(stat.Blocks) + status := "ok" + if pct >= p.Threshold { + status = "degraded" + degraded = true + } + details = append(details, fmt.Sprintf("%s (%s): %.0f%% used (limit %.0f%%), %s", target.Name, target.Path, pct*100, p.Threshold*100, status)) } - return health.Result{Status: health.StatusOK} + if len(details) == 0 { + for _, target := range targets { + if !target.Optional { + return health.Result{Status: health.StatusDegraded, Detail: "no disk path configured"} + } + } + return health.Result{Status: health.StatusOK, Detail: "no optional disk targets present"} + } + result := health.Result{Status: health.StatusOK, Detail: strings.Join(details, "; ")} + if degraded { + result.Status = health.StatusDegraded + } + return result } // --- model ---