diff --git a/modules/claude-code/cmd/claude-code/insteadof.go b/modules/claude-code/cmd/claude-code/insteadof.go index 7f4728c28..98b9de7f4 100644 --- a/modules/claude-code/cmd/claude-code/insteadof.go +++ b/modules/claude-code/cmd/claude-code/insteadof.go @@ -92,11 +92,3 @@ func InsteadOf(m *MeshTools) string { " need, none is worked around: say so, and the tool is created in the module that owns it, on its seat.\n") return b.String() } - -func cut(s string, n int) string { - s = strings.Join(strings.Fields(s), " ") - if r := []rune(s); len(r) > n { - return string(r[:n-1]) + "…" - } - return s -} diff --git a/modules/claude-code/cmd/claude-code/insteadof_test.go b/modules/claude-code/cmd/claude-code/insteadof_test.go index 87f461432..2396f230e 100644 --- a/modules/claude-code/cmd/claude-code/insteadof_test.go +++ b/modules/claude-code/cmd/claude-code/insteadof_test.go @@ -99,16 +99,10 @@ func aCatalog() fakeCatalog { return f } -func resolving(name string) []string { - return map[string][]string{"anchor": {"10.10.0.1"}, "anchor.internal": {"10.10.0.1"}, "anchor.example": {"192.0.2.10"}, - "laptop": {"127.0.0.1", "10.10.0.4"}}[name] -} - // Issue 1126 B13: what the mesh says of its tools and machines is read from the bus whole, never asked of the -// controller: the seats' verbs, the placed modules' own tools, every machine with its domains and addresses — a -// loopback address never one of them. +// controller: the seats' verbs and the placed modules' own tools. func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) { - m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats(), resolving) + m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats()) if err != nil { t.Fatal(err) } @@ -154,7 +148,7 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) { case <-time.After(10 * time.Second): t.Fatalf("the seat list was not read: %+v", seatList.Status()) } - got, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), definedSeats(), resolving) + got, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), definedSeats()) var addrs []string for _, r := range got.Replacements { addrs = append(addrs, r.Address) @@ -164,10 +158,6 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) { t.Fatalf("replacements from the bus: %v, %v", addrs, err) } }) - if len(m.Machines) != 2 || strings.Join(m.Machines[0].Domains, ",") != "anchor.internal,anchor.example" || - strings.Join(m.Machines[0].Addresses, ",") != "10.10.0.1,192.0.2.10" || strings.Join(m.Machines[1].Addresses, ",") != "10.10.0.4" { - t.Errorf("machines: %+v", m.Machines) - } if m.Replacements[3].Does != "logs" || !m.Replacements[2].NodeScoped || m.Replacements[0].NodeScoped { t.Errorf("the rows: %+v", m.Replacements) } @@ -176,7 +166,7 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) { // **The table is generated, not written** (novox/hq ADR 0245): what the records say a verb replaces is a row, // the machines' seats first, and nothing is rendered where the records say nothing. func TestTheInsteadOfTableIsGeneratedFromTheRecords(t *testing.T) { - m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats(), resolving) + m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats()) table := InsteadOf(&m) for _, want := range []string{ "| `/node-service-manager.` | `status` — `systemctl status`, `systemctl is-active` · `journal` — `journalctl` |", @@ -223,15 +213,15 @@ func TestTheInsteadOfTableIsGeneratedFromTheRecords(t *testing.T) { // Asked again with the same answer, nothing is written and nothing renders. func TestTheMeshsToolsAreKeptOnlyWhenTheyChange(t *testing.T) { p := Paths{State: t.TempDir()} - m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats(), resolving) + m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats()) if !KeepMeshTools(p, m) { t.Fatal("the first answer was not kept") } if KeepMeshTools(p, m) { t.Fatal("the same answer was kept again") } - m.Machines = m.Machines[:1] - if !KeepMeshTools(p, m) || len(ReadMeshTools(p).Machines) != 1 { + m.Replacements = m.Replacements[:1] + if !KeepMeshTools(p, m) || len(ReadMeshTools(p).Replacements) != 1 { t.Fatal("a changed answer was not kept") } } @@ -270,20 +260,20 @@ func TestTheMeshIsReadFromTheOwnersFirstViews(t *testing.T) { t.Fatal(err) } } - want, err := ReadMeshToolsFrom(bus, aCatalog(), someSeats(), resolving) + want, err := ReadMeshToolsFrom(bus, aCatalog(), someSeats()) if err != nil { t.Fatal(err) } - got, err := ReadMeshToolsFrom(viewStates{r}, aCatalog(), someSeats(), resolving) + got, err := ReadMeshToolsFrom(viewStates{r}, aCatalog(), someSeats()) if err != nil || !reflect.DeepEqual(got, want) || len(got.Replacements) == 0 { t.Fatalf("from the views: %+v, %v\nfrom the state: %+v", got, err, want) } // A reader not yet answered says so, and reads nothing. // A catalog half with no first view says so, and reads no module as absent. - if _, err := ReadMeshToolsFrom(bus, fakeCatalog{notYet: errors.New("the catalog half has no first view of main yet")}, someSeats(), resolving); err == nil || !strings.Contains(err.Error(), "no first view") { + if _, err := ReadMeshToolsFrom(bus, fakeCatalog{notYet: errors.New("the catalog half has no first view of main yet")}, someSeats()); err == nil || !strings.Contains(err.Error(), "no first view") { t.Fatalf("an unanswered catalog half: %v", err) } - if _, err := ReadMeshToolsFrom(viewStates{newMeshViews()}, aCatalog(), someSeats(), resolving); err == nil || !strings.Contains(err.Error(), "no first view") { + if _, err := ReadMeshToolsFrom(viewStates{newMeshViews()}, aCatalog(), someSeats()); err == nil || !strings.Contains(err.Error(), "no first view") { t.Fatalf("an unanswered first view: %v", err) } } @@ -291,7 +281,7 @@ func TestTheMeshIsReadFromTheOwnersFirstViews(t *testing.T) { // Issue 1126 B13 AC1 (ADR 0590): over the seats the mesh defines, the rendered instructions, their table among them, // name no address of the controller, which is not placed: each job is named by the seat that owns it now. func TestTheRenderedInstructionsNameNoControllerAddress(t *testing.T) { - m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats(), resolving) + m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats()) if err != nil { t.Fatal(err) } diff --git a/modules/claude-code/cmd/claude-code/main.go b/modules/claude-code/cmd/claude-code/main.go index 3ed701bb5..ab5c5a101 100644 --- a/modules/claude-code/cmd/claude-code/main.go +++ b/modules/claude-code/cmd/claude-code/main.go @@ -339,10 +339,10 @@ func tools(p Paths, servers ServerState, view *ServerView, config ConfigState, c Description: "Claude Code on this machine as the mesh configured it: the licence it holds and when its token expires, what it reports holding, the managed files, the MCP servers registered here. Fingerprints only, never a token.", Run: func(map[string]any) (any, error) { return status(p), nil }}, {Name: "claude_code_render", - Description: "Write Claude Code's managed directory now, from the mesh's facts, this module's settings and the servers registered here — after reading the bus again for what each tool replaces and which machines the mesh has (the instructions' table).", + Description: "Write Claude Code's managed directory now, from the mesh's facts, this module's settings and the servers registered here — after reading the bus again for what each tool replaces (the instructions' table).", Run: func(map[string]any) (any, error) { answer := map[string]any{} - if m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats(), lookup); err != nil { + if m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats()); err != nil { answer["asked"] = "the bus could not be read in full (" + err.Error() + "); what was kept is rendered" } else { KeepMeshTools(p, m) diff --git a/modules/claude-code/cmd/claude-code/mesh.go b/modules/claude-code/cmd/claude-code/mesh.go index 639ccb75a..1fac6c010 100644 --- a/modules/claude-code/cmd/claude-code/mesh.go +++ b/modules/claude-code/cmd/claude-code/mesh.go @@ -1,21 +1,19 @@ package main -// What the mesh says about its own tools and machines, as the agent's instructions need it (novox/hq ADR 0245): every seat verb and module tool that says what shell command it -// replaces, and the mesh's machines by every name and address a session could reach one by. +// What the mesh says about its own tools, as the agent's instructions need it (novox/hq ADR 0245): every seat verb +// and module tool that says what shell command it replaces. // -// Read from the bus at start and every few minutes (issue 1126 B13; the controller that answered this before is +// Read from the bus at start and whenever a followed state changes (issue 1126 B13; the controller that answered this before is // not placed, novox/hq ADR 0590): the seats' verbs from the seats' contract (mesh-contracts), the modules' -// own tools from the manifests the indexer keeps (`mesh-delivery-indexer.modules` and `.manifests`), and the -// machines and where each module runs from the seat mesh-module-assigner's `assignments`. Kept in the module's state so +// own tools from the manifests the indexer keeps (`mesh-delivery-indexer.modules` and `.manifests`), and where +// each module runs from the seat mesh-module-assigner's `assignments`. Kept in the module's state so // a render never waits for the bus, and rendered whenever it changes. Nothing here is written by hand: a verb // that gains a `replaces` is in the next render's table. import ( - "context" "encoding/json" "errors" "fmt" - "net" "os" "path/filepath" "sort" @@ -44,25 +42,13 @@ type Replacement struct { On []string `json:"on,omitempty"` } -// Machine is one of the mesh's machines and the names and addresses it is reached by. -type Machine struct { - Name string `json:"name"` - Domains []string `json:"domains,omitempty"` - Addresses []string `json:"addresses,omitempty"` -} - // MeshTools is what the bus said, as kept in the module's state. type MeshTools struct { Replacements []Replacement `json:"replacements"` - Machines []Machine `json:"machines"` // Asked is when the bus was last read whole. Asked string `json:"asked,omitempty"` } -// InternalSuffix is the mesh's own names' domain: every machine and service is `.internal` -// (novox/hq ADR 0194), and a session reaches none of them by ssh. -const InternalSuffix = ".internal" - func (p Paths) meshTools() string { return filepath.Join(p.State, "mesh-tools.json") } // ReadMeshTools is what was kept, or nil. @@ -219,17 +205,8 @@ func ReplacementsOf(defined []seatdef.Definition, modules []ModuleTools) []Repla return out } -// lookup resolves one name, briefly: a name that does not resolve here is no address. -func lookup(name string) []string { - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() - addrs, _ := net.DefaultResolver.LookupHost(ctx, name) - return addrs -} - -// ReadMeshToolsFrom reads the bus now: the seats' verbs, the placed modules' own tools, and every node assignments names -// with its domains and addresses. A part that cannot be read is left out and said in the error; what was read is kept. -func ReadMeshToolsFrom(st States, cat CatalogHalf, defined []seatdef.Definition, resolve func(string) []string) (MeshTools, error) { +// ReadMeshToolsFrom reads the bus now: the seats' verbs and the placed modules' own tools. A part that cannot be read is left out and said in the error; what was read is kept. +func ReadMeshToolsFrom(st States, cat CatalogHalf, defined []seatdef.Definition) (MeshTools, error) { var m MeshTools placed, err := assignments(st) if err != nil { @@ -238,24 +215,6 @@ func ReadMeshToolsFrom(st States, cat CatalogHalf, defined []seatdef.Definition, } modules, merr := modulesTools(cat, runsOn(placed)) m.Replacements = ReplacementsOf(defined, modules) - for node, p := range placed { - machine := Machine{Name: node, Domains: []string{node + InternalSuffix}} - if d := strings.ToLower(strings.TrimSpace(p.Decided.PublicDomain)); d != "" { - machine.Domains = append(machine.Domains, d) - } - seen := map[string]bool{} - for _, name := range append([]string{node}, machine.Domains...) { - for _, a := range resolve(name) { - if !seen[a] && !net.ParseIP(a).IsLoopback() { - seen[a] = true - machine.Addresses = append(machine.Addresses, a) - } - } - } - sort.Strings(machine.Addresses) - m.Machines = append(m.Machines, machine) - } - sort.Slice(m.Machines, func(i, j int) bool { return m.Machines[i].Name < m.Machines[j].Name }) return m, merr } @@ -293,15 +252,10 @@ func hookFiles(p Paths) *HookFiles { return &HookFiles{Exe: exe, WaitsDir: WaitsDirPath(p)} } -// MeshToolsEvery is how often the machines' addresses are resolved again; the tools and the assignments are read again -// when their followed states change (a views event), not on this timer. -const MeshToolsEvery = 10 * time.Minute - // MeshToolsFirst is how long the first read waits for the first views to be answered. const MeshToolsFirst = 2 * time.Minute -// refreshMeshTools reads the pictures once the first views are answered, and again whenever one changes (and the -// machines' addresses every few minutes), and renders when what it said changed. +// refreshMeshTools reads the pictures once the first views are answered, and again whenever one changes, and renders when what it said changed. func refreshMeshTools(p Paths) { select { case <-meshViews.Ready(): @@ -310,25 +264,22 @@ func refreshMeshTools(p Paths) { } startSeatList(seatlist.StdioBus()) for { - m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats(), lookup) + m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats()) if err != nil { - say("reading what each tool replaces and which machines the mesh has: %v", err) + say("reading what each tool replaces: %v", err) } if (err == nil || len(m.Replacements) > 0) && KeepMeshTools(p, m) { if _, err := RenderNow(p, writeManaged); err != nil { say("rendering what the bus said of the tools: %v", err) } else { - say("the mesh's tools changed: %d replace a command, %d machines; rendered", len(m.Replacements), len(m.Machines)) + say("the mesh's tools changed: %d replace a command; rendered", len(m.Replacements)) } } + <-meshChanged + time.Sleep(2 * time.Second) // a burst of events is one read select { case <-meshChanged: - time.Sleep(2 * time.Second) // a burst of events is one read - select { - case <-meshChanged: - default: - } - case <-time.After(MeshToolsEvery): + default: } } }