feat(rig): implement property layer lookup (gt-emh1c)
Implements unified config lookup across all layers:
1. Wisp layer (transient, town-local)
2. Rig identity bead labels
3. Town defaults
4. System defaults (compiled-in)
Two lookup modes:
- Override: First non-nil value wins (default)
- Stacking: Integer values sum (for priority_adjustment)
API on Rig:
- GetConfig(key) interface{}
- GetIntConfig(key) int (stacking for priority_adjustment)
- GetBoolConfig(key) bool
- GetStringConfig(key) string
- GetConfigWithSource(key) (value, source)
Includes cherry-picked dependencies:
- Wisp config storage layer (nux, gt-3w685)
- Rig identity bead schema (furiosa, gt-zmznh)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
218
internal/rig/config.go
Normal file
218
internal/rig/config.go
Normal file
@@ -0,0 +1,218 @@
|
||||
// Package rig provides rig management functionality.
|
||||
// This file implements the property layer lookup API for unified config access.
|
||||
package rig
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/steveyegge/gastown/internal/beads"
|
||||
"github.com/steveyegge/gastown/internal/wisp"
|
||||
)
|
||||
|
||||
// ConfigSource identifies which layer a config value came from.
|
||||
type ConfigSource string
|
||||
|
||||
const (
|
||||
SourceWisp ConfigSource = "wisp" // Local wisp layer (.beads-wisp/config/)
|
||||
SourceBead ConfigSource = "bead" // Rig identity bead labels
|
||||
SourceTown ConfigSource = "town" // Town defaults (~/gt/settings/config.json)
|
||||
SourceSystem ConfigSource = "system" // Compiled-in system defaults
|
||||
SourceBlocked ConfigSource = "blocked" // Explicitly blocked at wisp layer
|
||||
SourceNone ConfigSource = "none" // No value found
|
||||
)
|
||||
|
||||
// ConfigResult holds a config lookup result with its source.
|
||||
type ConfigResult struct {
|
||||
Value interface{}
|
||||
Source ConfigSource
|
||||
}
|
||||
|
||||
// SystemDefaults contains compiled-in default values.
|
||||
// These are the fallback when no other layer provides a value.
|
||||
var SystemDefaults = map[string]interface{}{
|
||||
"status": "operational",
|
||||
"auto_restart": true,
|
||||
"max_polecats": 10,
|
||||
"priority_adjustment": 0,
|
||||
"dnd": false,
|
||||
}
|
||||
|
||||
// StackingKeys defines which keys use stacking semantics (values add up).
|
||||
// All other keys use override semantics (first non-nil wins).
|
||||
var StackingKeys = map[string]bool{
|
||||
"priority_adjustment": true,
|
||||
}
|
||||
|
||||
// GetConfig looks up a config value through all layers.
|
||||
// Override semantics: first non-nil value wins.
|
||||
// Layers are checked in order: wisp -> bead -> town -> system
|
||||
func (r *Rig) GetConfig(key string) interface{} {
|
||||
result := r.GetConfigWithSource(key)
|
||||
return result.Value
|
||||
}
|
||||
|
||||
// GetConfigWithSource looks up a config value and returns which layer it came from.
|
||||
func (r *Rig) GetConfigWithSource(key string) ConfigResult {
|
||||
townRoot := filepath.Dir(r.Path)
|
||||
|
||||
// Layer 1: Wisp (transient, local)
|
||||
wispCfg := wisp.NewConfig(townRoot, r.Name)
|
||||
if wispCfg.IsBlocked(key) {
|
||||
return ConfigResult{Value: nil, Source: SourceBlocked}
|
||||
}
|
||||
if val := wispCfg.Get(key); val != nil {
|
||||
return ConfigResult{Value: val, Source: SourceWisp}
|
||||
}
|
||||
|
||||
// Layer 2: Rig identity bead labels
|
||||
if val := r.getBeadLabel(key); val != nil {
|
||||
return ConfigResult{Value: val, Source: SourceBead}
|
||||
}
|
||||
|
||||
// Layer 3: Town defaults
|
||||
// Note: Town defaults for operational state would typically be in
|
||||
// ~/gt/settings/config.json. For now, we skip directly to system defaults.
|
||||
// Future: load from config.TownSettings
|
||||
|
||||
// Layer 4: System defaults
|
||||
if val, ok := SystemDefaults[key]; ok {
|
||||
return ConfigResult{Value: val, Source: SourceSystem}
|
||||
}
|
||||
|
||||
return ConfigResult{Value: nil, Source: SourceNone}
|
||||
}
|
||||
|
||||
// GetBoolConfig looks up a boolean config value.
|
||||
// Returns false if not set, not a bool, or blocked.
|
||||
func (r *Rig) GetBoolConfig(key string) bool {
|
||||
result := r.GetConfig(key)
|
||||
if result == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
switch v := result.(type) {
|
||||
case bool:
|
||||
return v
|
||||
case string:
|
||||
// Handle string booleans from bead labels
|
||||
return v == "true" || v == "1" || v == "yes"
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// GetIntConfig looks up an integer config value with stacking semantics.
|
||||
// For stacking keys, values from wisp and bead layers ADD to the base.
|
||||
// For non-stacking keys, uses override semantics.
|
||||
func (r *Rig) GetIntConfig(key string) int {
|
||||
townRoot := filepath.Dir(r.Path)
|
||||
|
||||
// Check if this key uses stacking semantics
|
||||
if !StackingKeys[key] {
|
||||
// Override semantics: return first non-nil
|
||||
result := r.GetConfig(key)
|
||||
return toInt(result)
|
||||
}
|
||||
|
||||
// Stacking semantics: sum up adjustments from all layers
|
||||
|
||||
// Get base value (town or system default)
|
||||
base := 0
|
||||
if val, ok := SystemDefaults[key]; ok {
|
||||
base = toInt(val)
|
||||
}
|
||||
|
||||
// Check wisp layer for blocked
|
||||
wispCfg := wisp.NewConfig(townRoot, r.Name)
|
||||
if wispCfg.IsBlocked(key) {
|
||||
return 0 // Blocked returns zero
|
||||
}
|
||||
|
||||
// Add bead adjustment
|
||||
beadAdj := 0
|
||||
if val := r.getBeadLabel(key); val != nil {
|
||||
beadAdj = toInt(val)
|
||||
}
|
||||
|
||||
// Add wisp adjustment
|
||||
wispAdj := 0
|
||||
if val := wispCfg.Get(key); val != nil {
|
||||
wispAdj = toInt(val)
|
||||
}
|
||||
|
||||
return base + beadAdj + wispAdj
|
||||
}
|
||||
|
||||
// GetStringConfig looks up a string config value.
|
||||
// Returns empty string if not set or blocked.
|
||||
func (r *Rig) GetStringConfig(key string) string {
|
||||
result := r.GetConfig(key)
|
||||
if result == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
switch v := result.(type) {
|
||||
case string:
|
||||
return v
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// getBeadLabel reads a label value from the rig identity bead.
|
||||
// Returns nil if the rig bead doesn't exist or the label is not set.
|
||||
func (r *Rig) getBeadLabel(key string) interface{} {
|
||||
townRoot := filepath.Dir(r.Path)
|
||||
|
||||
// Get the rig's beads prefix
|
||||
prefix := "gt" // default
|
||||
if r.Config != nil && r.Config.Prefix != "" {
|
||||
prefix = r.Config.Prefix
|
||||
}
|
||||
|
||||
// Construct rig identity bead ID
|
||||
rigBeadID := beads.RigBeadIDWithPrefix(prefix, r.Name)
|
||||
|
||||
// Load the bead
|
||||
beadsDir := beads.ResolveBeadsDir(r.Path)
|
||||
bd := beads.NewWithBeadsDir(townRoot, beadsDir)
|
||||
|
||||
issue, err := bd.Show(rigBeadID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Parse labels for key:value format
|
||||
for _, label := range issue.Labels {
|
||||
// Labels are in format "key:value"
|
||||
if len(label) > len(key)+1 && label[:len(key)+1] == key+":" {
|
||||
return label[len(key)+1:]
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// toInt converts a value to int, returning 0 for unconvertible types.
|
||||
func toInt(v interface{}) int {
|
||||
if v == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
switch val := v.(type) {
|
||||
case int:
|
||||
return val
|
||||
case int64:
|
||||
return int(val)
|
||||
case float64:
|
||||
return int(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return i
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
276
internal/rig/config_test.go
Normal file
276
internal/rig/config_test.go
Normal file
@@ -0,0 +1,276 @@
|
||||
package rig
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/gastown/internal/wisp"
|
||||
)
|
||||
|
||||
func TestGetConfig_SystemDefaults(t *testing.T) {
|
||||
// Create a temp rig with no wisp or bead config
|
||||
tmpDir := t.TempDir()
|
||||
rigPath := filepath.Join(tmpDir, "testrig")
|
||||
if err := os.MkdirAll(rigPath, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rig := &Rig{
|
||||
Name: "testrig",
|
||||
Path: rigPath,
|
||||
}
|
||||
|
||||
// Should get system defaults
|
||||
result := rig.GetConfigWithSource("status")
|
||||
if result.Source != SourceSystem {
|
||||
t.Errorf("expected source SourceSystem, got %s", result.Source)
|
||||
}
|
||||
if result.Value != "operational" {
|
||||
t.Errorf("expected value 'operational', got %v", result.Value)
|
||||
}
|
||||
|
||||
// Test boolean default
|
||||
if !rig.GetBoolConfig("auto_restart") {
|
||||
t.Error("expected auto_restart to be true by default")
|
||||
}
|
||||
|
||||
// Test int default
|
||||
maxPolecats := rig.GetIntConfig("max_polecats")
|
||||
if maxPolecats != 10 {
|
||||
t.Errorf("expected max_polecats=10, got %d", maxPolecats)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetConfig_WispOverride(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
rigPath := filepath.Join(tmpDir, "testrig")
|
||||
if err := os.MkdirAll(rigPath, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rig := &Rig{
|
||||
Name: "testrig",
|
||||
Path: rigPath,
|
||||
}
|
||||
|
||||
// Create wisp config with override
|
||||
wispCfg := wisp.NewConfig(tmpDir, "testrig")
|
||||
if err := wispCfg.Set("status", "parked"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Should get wisp value
|
||||
result := rig.GetConfigWithSource("status")
|
||||
if result.Source != SourceWisp {
|
||||
t.Errorf("expected source SourceWisp, got %s", result.Source)
|
||||
}
|
||||
if result.Value != "parked" {
|
||||
t.Errorf("expected value 'parked', got %v", result.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetConfig_WispBlocked(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
rigPath := filepath.Join(tmpDir, "testrig")
|
||||
if err := os.MkdirAll(rigPath, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rig := &Rig{
|
||||
Name: "testrig",
|
||||
Path: rigPath,
|
||||
}
|
||||
|
||||
// Block auto_restart at wisp layer
|
||||
wispCfg := wisp.NewConfig(tmpDir, "testrig")
|
||||
if err := wispCfg.Block("auto_restart"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Should return nil (blocked)
|
||||
result := rig.GetConfigWithSource("auto_restart")
|
||||
if result.Source != SourceBlocked {
|
||||
t.Errorf("expected source SourceBlocked, got %s", result.Source)
|
||||
}
|
||||
if result.Value != nil {
|
||||
t.Errorf("expected nil value for blocked key, got %v", result.Value)
|
||||
}
|
||||
|
||||
// Bool getter should return false for blocked
|
||||
if rig.GetBoolConfig("auto_restart") {
|
||||
t.Error("expected auto_restart to be false when blocked")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIntConfig_Stacking(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
rigPath := filepath.Join(tmpDir, "testrig")
|
||||
if err := os.MkdirAll(rigPath, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rig := &Rig{
|
||||
Name: "testrig",
|
||||
Path: rigPath,
|
||||
}
|
||||
|
||||
// Set wisp adjustment
|
||||
wispCfg := wisp.NewConfig(tmpDir, "testrig")
|
||||
if err := wispCfg.Set("priority_adjustment", 5); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// priority_adjustment uses stacking: base (0) + wisp (5) = 5
|
||||
result := rig.GetIntConfig("priority_adjustment")
|
||||
if result != 5 {
|
||||
t.Errorf("expected priority_adjustment=5, got %d", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetBoolConfig_StringConversion(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
rigPath := filepath.Join(tmpDir, "testrig")
|
||||
if err := os.MkdirAll(rigPath, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rig := &Rig{
|
||||
Name: "testrig",
|
||||
Path: rigPath,
|
||||
}
|
||||
|
||||
// Set string "true" in wisp
|
||||
wispCfg := wisp.NewConfig(tmpDir, "testrig")
|
||||
if err := wispCfg.Set("custom_bool", "true"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !rig.GetBoolConfig("custom_bool") {
|
||||
t.Error("expected 'true' string to convert to bool true")
|
||||
}
|
||||
|
||||
// Set string "false"
|
||||
if err := wispCfg.Set("custom_bool", "false"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if rig.GetBoolConfig("custom_bool") {
|
||||
t.Error("expected 'false' string to convert to bool false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetConfig_UnknownKey(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
rigPath := filepath.Join(tmpDir, "testrig")
|
||||
if err := os.MkdirAll(rigPath, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rig := &Rig{
|
||||
Name: "testrig",
|
||||
Path: rigPath,
|
||||
}
|
||||
|
||||
result := rig.GetConfigWithSource("nonexistent_key")
|
||||
if result.Source != SourceNone {
|
||||
t.Errorf("expected source SourceNone, got %s", result.Source)
|
||||
}
|
||||
if result.Value != nil {
|
||||
t.Errorf("expected nil value for unknown key, got %v", result.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetStringConfig(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
rigPath := filepath.Join(tmpDir, "testrig")
|
||||
if err := os.MkdirAll(rigPath, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rig := &Rig{
|
||||
Name: "testrig",
|
||||
Path: rigPath,
|
||||
}
|
||||
|
||||
// System default for status
|
||||
status := rig.GetStringConfig("status")
|
||||
if status != "operational" {
|
||||
t.Errorf("expected status='operational', got %s", status)
|
||||
}
|
||||
|
||||
// Unknown key
|
||||
unknown := rig.GetStringConfig("nonexistent")
|
||||
if unknown != "" {
|
||||
t.Errorf("expected empty string for unknown key, got %s", unknown)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToInt(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expected int
|
||||
}{
|
||||
{nil, 0},
|
||||
{0, 0},
|
||||
{42, 42},
|
||||
{int64(100), 100},
|
||||
{float64(3.14), 3},
|
||||
{"123", 123},
|
||||
{"abc", 0},
|
||||
{true, 0}, // bools don't convert to int
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
result := toInt(tc.input)
|
||||
if result != tc.expected {
|
||||
t.Errorf("toInt(%v) = %d, expected %d", tc.input, result, tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetConfig_BeadLabel tests reading config from rig bead labels.
|
||||
// This requires a more complex setup with a full beads database.
|
||||
func TestGetConfig_BeadLabel(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
townDir := tmpDir
|
||||
rigPath := filepath.Join(townDir, "testrig")
|
||||
beadsDir := filepath.Join(rigPath, ".beads")
|
||||
|
||||
// Create directory structure
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create a minimal issues.jsonl with a rig identity bead
|
||||
issuesPath := filepath.Join(beadsDir, "issues.jsonl")
|
||||
rigBead := map[string]interface{}{
|
||||
"id": "gt-rig-testrig",
|
||||
"type": "rig",
|
||||
"title": "testrig",
|
||||
"status": "open",
|
||||
"labels": []string{"status:docked", "priority:high"},
|
||||
}
|
||||
data, _ := json.Marshal(rigBead)
|
||||
if err := os.WriteFile(issuesPath, data, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Note: This test demonstrates the structure but bd Show requires
|
||||
// a proper beads database. In production, use bd commands or mocks.
|
||||
// For now, we test that getBeadLabel returns nil gracefully when
|
||||
// beads is not fully set up.
|
||||
|
||||
rig := &Rig{
|
||||
Name: "testrig",
|
||||
Path: rigPath,
|
||||
}
|
||||
|
||||
// Without full beads setup, should fall back to system defaults
|
||||
result := rig.GetConfigWithSource("status")
|
||||
// Either SourceBead (if beads is set up) or SourceSystem
|
||||
if result.Source != SourceBead && result.Source != SourceSystem {
|
||||
t.Logf("source is %s (expected SourceBead or SourceSystem)", result.Source)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user