claude-code: register the agent's configuration at three scopes, served as the nox-mesh plugin
Skills, subagents, commands and hooks had no machine-wide place, so they were copied into homes by hand and drifted. Each is now registered once through the module's tools, kept in its config state, and written per node: the plugin in the managed directory, settings and instructions in the managed files, or the account's own directory, touching only what the module placed. hq ADR 0216.
This commit is contained in:
@@ -30,6 +30,9 @@ func say(format string, args ...any) {
|
||||
// writeManaged writes one managed file as root, only when its content changed. From a staged file, never
|
||||
// /dev/stdin: a child's input may be a socket, which /dev/stdin cannot open (found on the first assignment).
|
||||
func writeManaged(name, content string) (string, error) {
|
||||
if strings.HasSuffix(name, "/") {
|
||||
return writeManagedTree(strings.TrimSuffix(name, "/"), content)
|
||||
}
|
||||
path := filepath.Join(ManagedDir, name)
|
||||
if was, err := os.ReadFile(path); err == nil && string(was) == content {
|
||||
return name + ": unchanged", nil
|
||||
@@ -54,6 +57,74 @@ func writeManaged(name, content string) (string, error) {
|
||||
return name + ": written", nil
|
||||
}
|
||||
|
||||
// writeManagedTree replaces one directory of the managed directory whole — the plugin's marketplace (ADR
|
||||
// 0216) — when what it holds differs from the files given (a JSON map of path to PluginFile). Staged
|
||||
// beside, then swapped in as root, so a session never reads half of it.
|
||||
func writeManagedTree(name, content string) (string, error) {
|
||||
var files map[string]PluginFile
|
||||
if err := json.Unmarshal([]byte(content), &files); err != nil {
|
||||
return "", err
|
||||
}
|
||||
target := filepath.Join(ManagedDir, name)
|
||||
if sameTree(target, files) {
|
||||
return name + "/: unchanged", nil
|
||||
}
|
||||
staged, err := os.MkdirTemp("", "claude-code-tree-")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer os.RemoveAll(staged)
|
||||
for rel, f := range files {
|
||||
full := filepath.Join(staged, filepath.FromSlash(rel))
|
||||
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
mode := os.FileMode(0o644)
|
||||
if f.Executable {
|
||||
mode = 0o755
|
||||
}
|
||||
if err := os.WriteFile(full, []byte(f.Content), mode); err != nil {
|
||||
return "", err
|
||||
}
|
||||
_ = os.Chmod(full, mode)
|
||||
}
|
||||
_ = os.Chmod(staged, 0o755)
|
||||
script := `set -e; rm -rf "$2.next" "$2.old"; cp -r "$1" "$2.next"; chown -R root:root "$2.next"; chmod -R go-w,a+rX "$2.next";
|
||||
if [ -d "$2" ]; then mv "$2" "$2.old"; fi; mv "$2.next" "$2"; rm -rf "$2.old"`
|
||||
args := []string{"sh", "-c", script, "sh", staged, target}
|
||||
if os.Geteuid() != 0 {
|
||||
args = append([]string{"sudo", "-n"}, args...)
|
||||
}
|
||||
if out, err := exec.Command(args[0], args[1:]...).CombinedOutput(); err != nil {
|
||||
return "", fmt.Errorf("%s/: could not be written to %s (%s); the module writes there through the operator account's passwordless sudo",
|
||||
name, ManagedDir, strings.TrimSpace(string(out)))
|
||||
}
|
||||
return fmt.Sprintf("%s/: written, %d file(s)", name, len(files)), nil
|
||||
}
|
||||
|
||||
// sameTree says whether a directory holds exactly these files, with these contents and executable bits.
|
||||
func sameTree(dir string, files map[string]PluginFile) bool {
|
||||
found := 0
|
||||
err := filepath.WalkDir(dir, func(full string, d os.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() {
|
||||
return err
|
||||
}
|
||||
rel, _ := filepath.Rel(dir, full)
|
||||
f, ok := files[filepath.ToSlash(rel)]
|
||||
if !ok {
|
||||
return errors.New("not wanted")
|
||||
}
|
||||
raw, err := os.ReadFile(full)
|
||||
info, ierr := d.Info()
|
||||
if err != nil || ierr != nil || string(raw) != f.Content || (info.Mode()&0o111 != 0) != f.Executable {
|
||||
return errors.New("differs")
|
||||
}
|
||||
found++
|
||||
return nil
|
||||
})
|
||||
return err == nil && found == len(files)
|
||||
}
|
||||
|
||||
// ask is a tool on the bus, through the runtime: its answer is the tool's value.
|
||||
func ask(address string, args any) (json.RawMessage, error) { return stdio.Ask(address, args) }
|
||||
|
||||
@@ -63,6 +134,13 @@ type stateOf struct{ s stdio.KeptState }
|
||||
func (s stateOf) Put(key string, value any) error { _, err := s.s.Put(key, value); return err }
|
||||
func (s stateOf) Delete(key string) error { return s.s.Delete(key) }
|
||||
func (s stateOf) Keys() ([]string, error) { return s.s.Keys() }
|
||||
func (s stateOf) Get(key string) (json.RawMessage, bool, error) {
|
||||
e, err := s.s.Get(key)
|
||||
if err != nil || e == nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return e.Value, true, nil
|
||||
}
|
||||
|
||||
// nodesRunningMe is the nodes claude-code runs on, from the controller's list of modules — for the register
|
||||
// tool's question.
|
||||
@@ -152,9 +230,9 @@ func nodesOf(v any) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
func tools(p Paths, servers ServerState, view *ServerView) []stdio.Tool {
|
||||
func tools(p Paths, servers ServerState, view *ServerView, config ConfigState, configView *ConfigView) []stdio.Tool {
|
||||
nodesArg := str(`more nodes: "all" for every node running claude-code, or a comma-separated list; absent is this node only`)
|
||||
return []stdio.Tool{
|
||||
out := []stdio.Tool{
|
||||
{Name: "claude_code_status",
|
||||
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 }},
|
||||
@@ -239,6 +317,7 @@ func tools(p Paths, servers ServerState, view *ServerView) []stdio.Tool {
|
||||
return RegisterServer(p, Registration{Name: name, Nodes: nodesOf(a["nodes"])}, servers, view, writeManaged, nodesRunningMe)
|
||||
}},
|
||||
}
|
||||
return append(out, configTools(p, config, configView)...)
|
||||
}
|
||||
|
||||
// persist asks the state again until it answers: its bucket or the bus's grant may arrive after the module.
|
||||
@@ -283,15 +362,44 @@ func main() {
|
||||
}
|
||||
servers := stateOf{stdio.State("servers")}
|
||||
view := NewServerView(p)
|
||||
go run(p, view)
|
||||
if err := stdio.Serve("", tools(p, servers, view)); err != nil {
|
||||
config := stateOf{stdio.State("config")}
|
||||
configView := NewConfigView(p)
|
||||
go run(p, view, configView)
|
||||
if err := stdio.Serve("", tools(p, servers, view, config, configView)); err != nil {
|
||||
say("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// run is the module's long-running half, beside the tools (ADR 0198).
|
||||
func run(p Paths, view *ServerView) {
|
||||
func run(p Paths, view *ServerView, configView *ConfigView) {
|
||||
// The agent's configuration, at every scope (ADR 0216): the whole current set first, then each change.
|
||||
go persist("watching the agent's configuration", func() error {
|
||||
return stdio.State("config").Watch("", func(c stdio.StateChange) error {
|
||||
var item *Item
|
||||
if c.Op == "put" {
|
||||
item = &Item{}
|
||||
if json.Unmarshal(c.Value, item) != nil {
|
||||
item = nil
|
||||
}
|
||||
}
|
||||
if !configView.Take(c.Key, c.Op, item) {
|
||||
return nil
|
||||
}
|
||||
if out, err := RenderNow(p, writeManaged); err != nil {
|
||||
say("taking %s %s: %v", c.Op, c.Key, err)
|
||||
} else {
|
||||
say("took %s %s", c.Op, c.Key)
|
||||
for _, line := range out {
|
||||
if !strings.HasSuffix(line, "unchanged") {
|
||||
say("%s", line)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}, func(n int) { say("watching the agent's configuration%s", refusals(n)) })
|
||||
|
||||
// Every node's MCP servers: the whole current set first, then each change (ADR 0201).
|
||||
go persist("watching the MCP servers", func() error {
|
||||
return stdio.State("servers").Watch("", func(c stdio.StateChange) error {
|
||||
|
||||
Reference in New Issue
Block a user