# hermenix ctl — drive Hermes inside the guest. # # Identity, config, secrets, gateway service, and a passthrough to the # hermes CLI itself. For controlling the VM around it, see vm/vm.nu. # # Depends on: lib/common.nu, lib/guest.nu (ssh-args). def home-dir [] { $env.HERMES_HOME_DIR? | default "/var/lib/hermes-vm/.hermes" } def gateway-unit [] { $env.HERMES_GATEWAY_UNIT? | default "hermes-agent" } def ctl-gssh [cmd: string] { run-argv ((ssh-args) ++ [$cmd]) } # Pull a file out of the guest, open it in $EDITOR, push it back. # Skips the write if nothing changed (hash comparison). def edit-remote [remote: string, mode: string = "0644"] { let tmp = (mktemp) let fetched = (do -i { ctl-gssh $"cat ($remote)" } | complete) $fetched.stdout | save -f $tmp let before = (open --raw $tmp | hash sha256) ^($env.EDITOR? | default "vim") $tmp if (open --raw $tmp | hash sha256) == $before { rm $tmp say "unchanged" return } (open --raw $tmp) | ctl-gssh $"install -m ($mode) /dev/stdin ($remote)" rm $tmp say $"written to ($remote)" if (confirm "restart the gateway now?") { ctl-gssh $"systemctl --user restart (gateway-unit)" } } # Everything not matched by a more-specific `main ctl *` goes straight to the # hermes CLI in the guest, with a pty for interactive subcommands like `chat`. # # A single string, not rest args: nu's parser tries to match any bare # --flag-shaped token against this command's own flags before it ever # reaches a rest parameter, so `hermenix ctl chat --model x` would fail to # parse. Quote the whole thing instead — e.g. `hermenix ctl "chat --model x"`. def "main ctl" [remainder: string = ""] { if $remainder == "" { print "usage: hermenix ctl " print "" print " soul [edit] show or edit SOUL.md" print " memory [edit] show or edit memories/USER.md" print " conf [edit] show or edit config.yaml" print " secrets [edit|show] manage .env (keys masked by default)" print " status gateway service status" print " logs follow gateway journal (-f)" print " restart restart the gateway" print " units list all hermes* user units" print " reseed overwrite identity from /etc/nixos/hermes" print " pull [dir] copy live identity back to the host" print "" print "Anything else is passed straight to the hermes CLI in the guest:" print " hermenix ctl \"chat\"" print " hermenix ctl \"update\"" print " hermenix ctl \"sessions list\"" exit 1 } let words = ($remainder | split row " " | where {|w| $w != "" }) run-argv ((ssh-args) ++ ["-t" "hermes"] ++ $words) } def "main ctl soul" [action?: string] { if $action == "edit" { edit-remote $"(home-dir)/SOUL.md" } else { ctl-gssh $"cat (home-dir)/SOUL.md" } } def "main ctl memory" [action?: string] { if $action == "edit" { edit-remote $"(home-dir)/memories/USER.md" } else { ctl-gssh $"cat (home-dir)/memories/USER.md" } } def "main ctl conf" [action?: string] { if $action == "edit" { edit-remote $"(home-dir)/config.yaml" } else { ctl-gssh $"cat (home-dir)/config.yaml" } } # API keys. Mode 0600, never enters the Nix store, and not printed unless # you ask for it explicitly. def "main ctl secrets" [action?: string] { if $action == "edit" { edit-remote $"(home-dir)/.env" "0600" } else if $action == "show" { ctl-gssh $"cat (home-dir)/.env" } else { ctl-gssh $"sed 's/=.*/=/' (home-dir)/.env" } } def "main ctl status" [] { ctl-gssh $"systemctl --user status (gateway-unit) --no-pager" } def "main ctl logs" [] { run-argv ((ssh-args) ++ ["-t" $"journalctl --user -u (gateway-unit) -f"]) } def "main ctl restart" [] { ctl-gssh $"systemctl --user restart (gateway-unit)" } def "main ctl units" [] { ctl-gssh "systemctl --user list-units 'hermes*' --all --no-pager" } # Overwrite the agent's identity from the seed. Destructive — this is how # you undo whatever it has decided about itself. def "main ctl reseed" [] { warn "This overwrites SOUL.md, memories and config.yaml in the guest" warn "with the seed from /etc/nixos/hermes. Anything Hermes has" warn "learned and written there is lost." if not (confirm-word "reseed" "type 'reseed' to confirm:") { die "aborted" } ctl-gssh $"cp -rT /etc/nixos/hermes/home (home-dir) && cp -rT /etc/nixos/hermes/workspace /var/lib/hermes/workspace && systemctl --user restart (gateway-unit)" ok "reseeded" } # Copy the agent's live identity back here, so you can fold what it has # learned into the seed in guest/hermes/. def "main ctl pull" [dest: string = "./hermes-home"] { mkdir $dest (ctl-gssh $"tar -C (home-dir) --exclude=.env --exclude=sessions --exclude=state.db -cf - .") | ^tar -C $dest -xf - say $"pulled to ($dest) (secrets and sessions excluded)" }