diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f3a3277..bd12d75 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -62,6 +62,10 @@ jobs: env: CHROME_NO_SANDBOX: '1' timeout-minutes: 2 + - run: node tests/browser/hermes-reporting.mjs + env: + CHROME_NO_SANDBOX: '1' + timeout-minutes: 2 - run: node tests/browser/proxy.mjs env: CHROME_NO_SANDBOX: '1' @@ -245,8 +249,11 @@ jobs: path: bin/native merge-multiple: true - run: chmod +x bin/native/*/agent-guild-report + # A release also names the commit it is built from, whose Hermes plugin it installs. - if: inputs.version != '' - run: npm version "$VERSION" --no-git-tag-version + run: | + npm version "$VERSION" --no-git-tag-version + npm pkg set agentGuild.commit="$GITHUB_SHA" env: VERSION: ${{ inputs.version }} # A release packs the Windows modules that sign.yml signed, keeping node-pty's copies to compare. @@ -273,9 +280,9 @@ jobs: version=$(node -p "require('./package.json').version") node scripts/yard-pack.mjs --out "$RUNNER_TEMP/package" --url-base "https://github.com/$GITHUB_REPOSITORY/releases/download/v$version/" npm pack --pack-destination "$RUNNER_TEMP/package" - upstream=() - if [ "$SIGNED" = true ]; then upstream=(--upstream "$RUNNER_TEMP/upstream"); fi - node tests/package/check-tarball.mjs "$RUNNER_TEMP"/package/*.tgz --version "$version" "${upstream[@]}" + release=() + if [ "$SIGNED" = true ]; then release=(--upstream "$RUNNER_TEMP/upstream" --commit "$GITHUB_SHA"); fi + node tests/package/check-tarball.mjs "$RUNNER_TEMP"/package/*.tgz --version "$version" "${release[@]}" cd "$RUNNER_TEMP/package" sha256sum -- *.tgz yard-art-*.tar.gz > SHA256SUMS echo "version=$version" >> "$GITHUB_OUTPUT" diff --git a/.github/workflows/hermes-compat.yml b/.github/workflows/hermes-compat.yml new file mode 100644 index 0000000..1f1ee19 --- /dev/null +++ b/.github/workflows/hermes-compat.yml @@ -0,0 +1,31 @@ +# Checks the Hermes plugin against a real Hermes Agent with Hermes's own catalog gates. It downloads +# Hermes, so it is not part of CI: pull requests test the plugin with local fixtures only, and a +# failure here never blocks a merge. +name: Hermes compatibility + +on: + push: + branches: [main] + paths: [plugins/hermes/**, .github/workflows/hermes-compat.yml] + schedule: + - cron: '17 6 * * 1' + workflow_dispatch: + +permissions: + contents: read + +jobs: + hermes: + name: Hermes plugins (catalog gates) + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + sparse-checkout: plugins/hermes + # Hermes Agent v0.23.1. + - run: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash -s -- --commit 3b421578588243f920886e6fe79a6faa87ea026e --non-interactive --skip-browser --skip-computer-use + - run: | + "$HOME/.local/bin/hermes" plugins validate --install-deps plugins/hermes/agent-guild + "$HOME/.local/bin/hermes" plugins doctor plugins/hermes/agent-guild --ci diff --git a/config/providers.optional.json b/config/providers.optional.json index 787c620..f29613f 100644 --- a/config/providers.optional.json +++ b/config/providers.optional.json @@ -22,6 +22,28 @@ "winget": { "id": "OddEssentials.TerminalAIChat" } } } + }, + { + "id": "hermes", + "vendor": "Nous Research", + "tool": "Hermes Agent", + "command": "hermes", + "versionArgs": ["--version"], + "reporting": "hermes", + "mcp": "hermes", + "homeVar": "HERMES_HOME", + "profiles": "hermes", + "resumeArgs": ["--resume", "{id}"], + "color": "#8B5CF6", + "monogram": "H", + "install": "curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash", + "docs": "https://hermes-agent.nousresearch.com/docs", + "channels": { + "native": { "paths": ["~/.local/bin/hermes", "~/.hermes/hermes-agent"], "update": ["update"] } + }, + "win32": { + "install": "iex (irm https://hermes-agent.nousresearch.com/install.ps1)" + } } ] } diff --git a/docs/agent-reporting.md b/docs/agent-reporting.md index 5b5e20a..c49b5d1 100644 --- a/docs/agent-reporting.md +++ b/docs/agent-reporting.md @@ -10,6 +10,7 @@ report these through hooks or the `agent-guild-report` command. | Claude Code, Codex CLI | Automatic when the installed version can load hooks for one session. Otherwise the card says so; add the hooks yourself below. | | Grok Build | Turn on **Agent reporting** on its card. It adds Agent Guild's hooks to the `hooks` folder of each Grok Build home (`~/.grok/hooks` by default). | | Antigravity CLI | Turn on **Agent reporting** on its card. Reports the model, its conversation (for Resume) and each turn's answer, not helper agents. | +| Hermes Agent | Turn on **Agent reporting** on its card (an optional card). For each account (the default is Hermes's base home, the others are its profiles) it keeps a report token and its own address in a file of its own, saves that file's path with `hermes config set AGENT_GUILD_HERMES_TOKEN_FILE`, and installs the [Agent Guild plugin](../plugins/hermes/agent-guild) with `hermes plugins install --ref`, at the commit this release of Agent Guild was built from; a running gateway loads it at once. A plugin of another commit, as after Agent Guild is upgraded, keeps reporting, and the card offers **Update plugin**, which installs this release's; nothing is reinstalled when Agent Guild starts. A build that is not a release installs no plugin unless `AGENT_GUILD_HERMES_PLUGIN_COMMIT` names a commit to install. The plugin reports every session the gateway runs, Telegram, cron and the CLI included, with its subagents, shell commands and approvals, on the Hermes Agent tool card, per profile. It shows the sessions active since reporting started: one already running appears at its next turn or tool call. The plugin sends events in order and, while anything is open, a heartbeat every 30 seconds that closes whatever a lost event left open and rebuilds what one never delivered, also after a manager restart. A profile reports only with a token issued for its own home: one cloned from a profile that reports, with its settings, sends nothing until reporting is turned on for it. Commands and subagent goals pass through Hermes's own secret redaction first, and are left out when it fails; a reporting process silent for 90 seconds is dropped. | | Docker Agent | Automatic from 1.55.0. Reports from the first prompt: its sub-agents (`transfer_task`, background agents; from 1.81.2, which names the agent on each event), shell commands in its approval modes, and turns. For the model, turn on **Model reporting** on its card (1.100.0 and later). A shell command Docker Agent runs without asking (every command in autonomous mode, and the ones its balanced and restricted modes classify as safe, or that you allowed for the session) fires no `pre_tool_use` for Agent Guild's hooks, so it shows only with the hook below. Its matching `post_tool_use` fires, and the card ignores it. Resume opens the first session that runs a prompt: the one Agent Guild started or resumed (from 1.81.2 it names that session, so Resume works before the first prompt), unless a tab opened with `/new` before the first prompt ran it. A sub-agent is a session first heard from under another agent's name while another session's run is in progress (from its `session_start` to its `session_end`); the launch agent is the one named on the first event. Later tabs (`/new`, `/fork`, `/sessions`) start with the agent Docker Agent was launched with, so they are not shown as sub-agents and do not move Resume; a skill that agent runs, or a sub-agent with its name, counts as that agent's own work, as does an agent switched to before the first prompt. A tab prompted under another agent's name while another tab's run is in progress shows as a sub-agent until it ends. Docker Agent run inside a Shell card, with the hooks below configured in its own settings, reports the same way from 1.81.2, and **Model reporting** shows its model there too. From 1.81.2, Docker Agent run inside another tool's card reports nothing to that card. | The end of each main turn also brings the agent's answer, which the terminal's diff --git a/docs/api.md b/docs/api.md index f4656ca..0b37f26 100644 --- a/docs/api.md +++ b/docs/api.md @@ -79,7 +79,7 @@ on; any route can answer others. "usageSource": "claude", "historySource": "claude", "memorySource": "claude", - "accounts": [{ "id": "default", "label": "Default" }, { "id": "work", "label": "Work" }], + "accounts": [{ "id": "default", "label": "Default", "live": null }, { "id": "work", "label": "Work", "live": null }], "color": "#D97757", "monogram": "A", "iconUrl": null, @@ -112,7 +112,7 @@ or uninstall session's result, are lines the card shows about the copies, such as several being installed. `npmNote` qualifies an npm install, or is null. `modelPattern` is the provider's, or null. `reporting` is the tool's reporting mode: `claude`, -`codex`, `antigravity`, `grok`, `docker`, or null. +`codex`, `antigravity`, `grok`, `docker`, `hermes`, or null. `installs` lists every copy of the tool that was found, each `{ path, displayPath, channel, version, versionStatus, active, newer, onPath, uninstall, uninstallGuidance }`. @@ -139,7 +139,12 @@ can read saved messages: for every built-in history source, not for a under: `default` is the tool's own, and each further one has its own home folder, so it keeps its own sign-in and usage. `POST /sessions` takes an account id. `accountsEditable` is true when accounts can be added, renamed -and removed through `/providers/:id/accounts`. +and removed through `/providers/:id/accounts`; for Hermes Agent the accounts are its own +profiles instead: `default` is Hermes's base home (`HERMES_HOME`, else Hermes's own default), and each +other one a profile folder in it, found when the cards load. An account's `live` is null +except for a Hermes Agent profile whose plugin reports: then it is +`{ working, waiting, agents, shells }`: the counts of its Hermes sessions and subagents working +and waiting, the first 64 of them as `{ id, name, status, detail }`, and their shell commands as `{ id }`. `shells` is null except for the `@shell` provider, where it lists the installed shells, each `{ id, label, path, multiplexer }`, and `defaultShell` @@ -153,12 +158,19 @@ leaves the multiplexer's own session running, and For a tool whose agent or model reporting has to be turned on, `reportingEnabled` says whether it is, and `reportingNote` says why that switch -cannot be used, or is null. +cannot be used, or for Hermes Agent why reporting is off until it is turned on +again, or is null. `reportingUpdate` is true for Hermes Agent while a profile +that reports has an Agent Guild plugin of another commit than the one this +Agent Guild installs; that plugin keeps reporting until it is updated. For +Hermes Agent, `reportingEnabled` also stays true while a profile's report token +is still accepted after a revocation that failed, with a `reportingNote` that +says so, so turning it off can be tried again. `mcp` is null unless the tool is installed and Agent Guild can add its own MCP server (`agent-guild mcp`, see [mcp.md](mcp.md)) to the tool's settings: -Claude Code, Codex CLI, Antigravity CLI and Grok Build. Otherwise it is -`{ mode, accounts }`: `mode` is `claude`, `codex`, `antigravity` or `grok`, and +Claude Code, Codex CLI, Antigravity CLI, Grok Build and Hermes Agent, where it +is `mcp_servers.agent-guild` in the profile's `config.yaml`, marked untrusted. Otherwise it is +`{ mode, accounts }`: `mode` is `claude`, `codex`, `antigravity`, `grok` or `hermes`, and `accounts` has one `{ id, label, state, managed, from, command, allowInput, detail, fingerprint }` per account, read from that account's settings. A settings file the tool keeps as JSON is read again when it changes; a tool @@ -663,7 +675,7 @@ All paths are under `/api/v1`. | POST | `/providers/:id/accounts` | `{ label, revision }` | `201 { account, dir, providers, revision }`: adds an account to `providers.json`. Its id is made from `label` and does not change. | | PATCH | `/providers/:id/accounts/:account` | `{ label, revision }` | `{ account, dir, providers, revision }`: renames an account, including `default`. | | DELETE | `/providers/:id/accounts/:account?revision=` | | `{ account, dir, providers, revision }`: removes an account from `providers.json`. Its home folder, `dir`, stays on disk. 409 `default_account` for `default`; 409 `account_in_use` while a running session uses it. | -| POST | `/providers/:id/reporting` | `{ enabled }` | `{ provider }`: turns agent or model reporting on or off for a tool that has `reportingEnabled`. | +| POST | `/providers/:id/reporting` | `{ enabled }` or `{ update: true }` | `{ provider }`: turns agent or model reporting on or off for a tool that has `reportingEnabled`. `{ update: true }` installs this Agent Guild's Hermes plugin in every profile that reports with another commit, and no other; a profile whose update fails keeps the plugin it had, and the error says so. 400 `not_applicable` for a tool other than Hermes Agent; 409 `reporting_unavailable` when this build names no plugin commit; 502 `reporting_setup_failed` when Hermes fails or does not record the new commit. | | POST | `/providers/:id/mcp` | `{ account, action, expect }` | `{ provider, result }`: changes Agent Guild's MCP server in one account's settings. `action` is `on` (add it, or turn it on in the tool), `off` (remove Agent Guild's entry, or turn off one that comes from another tool's settings) or `replace` (put Agent Guild's own entry in place of the one there). `expect` is the account's `fingerprint`: the account is read again first, and nothing changes unless the entry is still that one. `result` is `{ outcome, state, detail? }`, `outcome` `changed`, `unchanged` or `failed`. 400 `not_applicable` for a tool without `mcp`; 404 `unknown_account`; 409 `provider_unavailable` when it is not installed, `busy` while a change for that tool is running, `changed` when the entry is no longer `expect`, `unreadable` when the settings cannot be read, `conflict` for `on` or `off` on another server of that name, `needs_replace` for `on` on a Codex CLI entry Agent Guild did not add. | | POST | `/providers/:id/install` | `{ force? }` | `201 { session }`: a session that installs or updates the tool. 409 `provider_in_use` (with `running`, the session count) while the provider's sessions are running; retry with `force: true` after the user confirms. | | POST | `/providers/:id/uninstall` | `{ path, force? }` | `201 { session }`: a session that removes the copy at `path`, one of the provider's `installs`. `force` as for `install`. | @@ -726,6 +738,7 @@ All paths are under `/api/v1`. | POST | `/sessions/:id/docker` | `{ event, sessionId, agentName?, toolName?, toolUseId?, command?, model?, reply? }` | `{ ok }`. One Docker Agent hook event. | | POST | `/sessions/:id/shells` | `{ shell, key \| task, match?, agentId?, persist?, endsWithAgent?, kind?, tasks? }` | `{ ok }`. A shell command the tool started, ended or put in the background. | | POST | `/sessions/:id/hook?event=&reporting=` | A coding tool's hook event, as the tool gives it to its hooks | `{ ok }`, or `{ ok, errors }` when reports the event makes are refused, each `{ status, code?, message }` as its own route would answer. The manager turns the event into the session's reports as `agent-guild-report --hook` does. `event` names an event that does not name itself; `reporting` is the session's `AGENT_GUILD_REPORTING` where the hook ran, and defaults to the session's own. A transcript the event names is read only if it is a regular file. | +| POST | `/hermes/hook` | `{ instance, messages }` | `{ ok }`. What the Hermes Agent plugin of one profile reports, for every Hermes Agent card of that account. `instance` names the reporting process; `messages` are its events, `{ seq, event, data }`, and heartbeats, `{ seq, open }`, applied in `seq` order, each `seq` once. A heartbeat lists what is still open: `sessions` (each `{ platform }`), `working` (session ids), `subagents` (each `{ parent_session_id, child_role, child_goal }`), `tools` (each `{ session_id, tool_name }`) and `waits` (each `{ session_id }`), keyed by id. It closes the rest and rebuilds what is missing. An instance that sends nothing for 90 seconds is dropped. Needs the profile's Hermes report token in `X-Agent-Guild-Report-Token`, which turning on its reporting sets; 401 `unauthorized` otherwise. | | POST | `/shutdown` | `{ force?, restart? }` | `202 { ok, running, restart }`: stops the manager and every session, detaching tmux and herdr sessions. 409 `sessions_running` (with `running`) while other sessions run; retry with `force: true` after the user confirms. With `restart`, a new manager takes over on the same port and token, running the version on disk. | `cwd` defaults to the user's home folder, and a leading `~` is expanded. @@ -744,7 +757,7 @@ header. The manager gives that token only to the processes inside that session. Request bodies are limited to 64 KiB, except `PUT /notes`, -`POST /sessions/:id/reply` and `POST /sessions/:id/docker`, which accept +`POST /sessions/:id/reply`, `POST /sessions/:id/docker` and `POST /hermes/hook`, which accept 1 MiB, and `POST /sessions/:id/hook`, which accepts 32 MiB and checks the token before reading the body. Messages a client sends on a WebSocket are limited to 1 MiB. diff --git a/docs/configuration.md b/docs/configuration.md index 895b146..0c7b612 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -68,6 +68,7 @@ Invalid values are ignored; a file that cannot be read is reported in `manager.l | `modelPattern` | Regular expression that finds the model name on screen | | `homeVar` | Variable that moves the tool's home folder; required for `accounts` | | `accounts` | Extra sign-ins; see [Accounts](#accounts) | +| `profiles` | `hermes`: the accounts are Hermes's home, as `default`, and each of its profiles, found as Hermes finds them when the cards load, in place of `accounts`; they are created in the tool | | `color`, `monogram`, `icon` | Card appearance; `icon` is a URL path | | `install`, `docs` | Shown when the tool is not installed; for a `plugin`, or with `channels.native.reinstall`, `install` is the command **Install** and **Update** run | | `channels` | Where each kind of copy lives, so the card can update and remove it: `native` `{ paths, remove, reinstall, versionEnv }`, `brew` `{ names }`, `winget` `{ id }`. A copy inside a `native` path is native; **Remove** deletes its `remove` paths, which must be inside your home folder. With `reinstall`, running `install` again updates a native copy, and `versionEnv` names the variable that pins `install` to the version `releases` reports | @@ -124,6 +125,7 @@ Codex CLI and Grok Build support this. | `AGENT_GUILD_HOME` | Moves the data folder. Turns off **Settings → Startup**. | | `AGENT_GUILD_NPM_REGISTRY` | npm registry for version checks and installs | | `AGENT_GUILD_NO_UPDATE_CHECK` | `1` turns off online version checks and self-upgrade | +| `AGENT_GUILD_HERMES_PLUGIN_COMMIT` | For a build that is not a release: the commit of `oddessentials/agent-guild` whose Hermes plugin turning on Hermes reporting installs. A release always installs its own commit's. | | `AGENT_GUILD_SKIP_SHELL_ENV` | `1` stops reading PATH from your login shell, or from the registry on Windows | | `AGENT_GUILD_ALLOWED_HOSTS`, `AGENT_GUILD_ALLOWED_ORIGINS` | Legacy proxy allowlists, used until remote access settings are saved. An invalid value stops the manager from starting. | diff --git a/docs/mcp.md b/docs/mcp.md index d07e5fa..82cd27b 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -21,6 +21,7 @@ With Agent Guild installed (`npm install -g @oddessentials/agent-guild`): | Codex CLI | `codex mcp add agent-guild -- agent-guild mcp` | | Antigravity CLI | `agy mcp add agent-guild agent-guild mcp` | | Gemini CLI | `gemini mcp add -s user agent-guild agent-guild mcp` | +| Hermes Agent | `hermes config set mcp_servers.agent-guild '{"command": "agent-guild", "args": ["mcp"], "trust": "untrusted"}'` | | VS Code | `code --add-mcp '{"name":"agent-guild","type":"stdio","command":"agent-guild","args":["mcp"]}'` | For one VS Code workspace, `.vscode/mcp.json`: diff --git a/plugins/hermes/agent-guild/LICENSE b/plugins/hermes/agent-guild/LICENSE new file mode 100644 index 0000000..fe6b903 --- /dev/null +++ b/plugins/hermes/agent-guild/LICENSE @@ -0,0 +1,662 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. + diff --git a/plugins/hermes/agent-guild/README.md b/plugins/hermes/agent-guild/README.md new file mode 100644 index 0000000..95b76ed --- /dev/null +++ b/plugins/hermes/agent-guild/README.md @@ -0,0 +1,13 @@ +# Agent Guild for Hermes Agent + +Shows every Hermes session live in [Agent Guild](https://github.com/oddessentials/agent-guild): Telegram, Discord, cron, CLI, the terminal app and Desktop sessions, their subagents, the shell commands they run and the approvals they wait for. + +## Install + +Add the Hermes Agent card in Agent Guild and turn on **Agent reporting** on it. For each profile it saves where Agent Guild keeps that profile's report token and its own address (`AGENT_GUILD_HERMES_TOKEN_FILE`, in the profile's `.env`) and installs this plugin with `hermes plugins install`. A running gateway loads it straight away. + +## What it sends + +Session, tool, subagent and approval events, including shell commands and subagent goals after Hermes's secret redaction (left out when it fails), only to the Agent Guild manager on this computer (default `http://127.0.0.1:47821`). It runs one background thread per profile that reports, reads one file, where Agent Guild keeps the profile's report token and its own address, and installs no packages. + +Agent Guild shows the Hermes sessions that have been active since reporting started. A session that was already running appears at its next turn or tool call. diff --git a/plugins/hermes/agent-guild/__init__.py b/plugins/hermes/agent-guild/__init__.py new file mode 100644 index 0000000..f19cd76 --- /dev/null +++ b/plugins/hermes/agent-guild/__init__.py @@ -0,0 +1,250 @@ +import json +import logging +import os +import sys +import threading +import urllib.request +import uuid +from collections import deque + +logger = logging.getLogger(__name__) + +HEARTBEAT_SECONDS = 30 +QUEUE_LIMIT = 1000 +COMMAND_LIMIT = 500 +DEFAULT_URL = "http://127.0.0.1:47821" +HOOKS = ( + "on_session_start", "on_session_end", "on_session_finalize", "on_session_delete", + "pre_llm_call", "pre_tool_call", "post_tool_call", "subagent_start", "subagent_stop", + "pre_approval_request", "post_approval_response", + "on_human_input_request", "on_human_input_resolved", +) +FIELDS = ( + "session_id", "platform", "tool_name", "tool_call_id", "turn_id", + "completed", "failed", "interrupted", "parent_session_id", "child_session_id", + "child_role", "child_status", "request_id", "kind", "choice", "outcome", "reason", +) +# Reports go to the manager on this machine only: never through a proxy from the environment, which would get the +# report token and what is reported and could not reach the manager. Hermes's own requests keep their proxy settings. +LOCAL = urllib.request.build_opener(urllib.request.ProxyHandler({})) + + +def _secret(name): + try: + from agent.secret_scope import get_secret + return get_secret(name) + except Exception: + return None + + +def _home_key(path): + """A Hermes home as Agent Guild names it in a token file: its real path, lowercase on Windows.""" + real = os.path.realpath(path) + return real.lower() if sys.platform == "win32" else real + + +def _home(): + """This profile's Hermes home, or None when Hermes cannot say. Read in a hook, where Hermes has set the profile.""" + try: + from hermes_constants import get_hermes_home + return _home_key(str(get_hermes_home())) + except Exception: + return None + + +def _settings(path): + """The token, the home it was issued for, and the manager's address.""" + try: + with open(path, encoding="utf-8") as file: + lines = file.read().split("\n") + except Exception: + return None, None, None + home = lines[1].strip() if len(lines) > 1 else "" + url = lines[2].strip() if len(lines) > 2 else "" + return lines[0].strip() or None, home or None, (url or DEFAULT_URL).rstrip("/") + + +def _redact(text, limit=None): + try: + from agent.redact import redact_sensitive_text + redacted = redact_sensitive_text(text, force=True) + except Exception: + return None + if not isinstance(redacted, str): + return None + return redacted[:limit] if limit else redacted + + +class Reporter: + def __init__(self): + self.lock = threading.Lock() + self.wake = threading.Event() + self.events = deque(maxlen=QUEUE_LIMIT) + self.seq = 0 + self.instance = uuid.uuid4().hex + self.token_file = None + self.home = None + self.refused = None + self.open ={"sessions": {}, "working": set(), "subagents": {}, "tools": {}, "waits": {}} + self.turns = {} + self.settled = True + self.thread = None + self.down = False + self.stopped = False + + def _track(self, name, kw, data): + sid, tool, child, request = kw.get("session_id"), kw.get("tool_call_id"), kw.get("child_session_id"), kw.get("request_id") + o = self.open + turn = kw.get("turn_id") + if not sid and turn in self.turns: + sid = data["session_id"] = self.turns[turn] + elif sid and turn and name in ("pre_llm_call", "pre_tool_call"): + self.turns[turn] = sid + ended = child if name == "subagent_stop" else sid if name in ("on_session_end", "on_session_finalize", "on_session_delete") else None + if ended: + self.turns = {t: s for t, s in self.turns.items() if s != ended} + call = f"{sid or ''}:{tool}" if tool else None + if sid and name not in ("on_session_finalize", "on_session_delete") and sid not in o["subagents"]: + session = o["sessions"].setdefault(sid, {}) + if name in ("on_session_start", "pre_llm_call") and isinstance(kw.get("platform"), str): + session["platform"] = kw["platform"] + if name == "on_session_end" and sid: + o["working"].discard(sid) + elif name in ("on_session_finalize", "on_session_delete") and sid: + o["sessions"].pop(sid, None) + o["working"].discard(sid) + elif name == "pre_llm_call" and sid: + o["working"].add(sid) + elif name == "pre_tool_call": + if sid: + o["working"].add(sid) + if call: + o["tools"][call] = {"session_id": sid, "tool_name": kw.get("tool_name")} + elif name == "post_tool_call" and call: + o["tools"].pop(call, None) + elif name == "subagent_start" and child: + o["subagents"][child] = {k: data[k] for k in ("parent_session_id", "child_role", "child_goal") if k in data} + elif name == "subagent_stop" and child: + o["subagents"].pop(child, None) + o["working"].discard(child) + elif name == "pre_approval_request" and call: + o["waits"][f"approval:{call}"] = {"session_id": sid} + elif name == "post_approval_response" and call: + o["waits"].pop(f"approval:{call}", None) + elif name == "on_human_input_request" and request: + o["waits"][f"input:{request}"] = {"session_id": sid} + elif name == "on_human_input_resolved" and request: + o["waits"].pop(f"input:{request}", None) + + def _data(self, name, kw): + data = {k: kw[k] for k in FIELDS if isinstance(kw.get(k), (str, int, float, bool))} + args = kw.get("args") + texts = ( + ("command", args.get("command") if isinstance(args, dict) else None, COMMAND_LIMIT), + ("child_goal", kw.get("child_goal"), None), + ) + for key, text, limit in texts: + redacted = _redact(text, limit) if isinstance(text, str) else None + if redacted is not None: + data[key] = redacted + return data + + def handle(self, name, kw): + data = self._data(name, kw) + with self.lock: + if self.stopped: + return + self.token_file = _secret("AGENT_GUILD_HERMES_TOKEN_FILE") or self.token_file + # Taken here, in the profile's context: the reporting thread starts with none. + self.home = _home() or self.home + self._track(name, kw, data) + self.seq += 1 + self.events.append({"seq": self.seq, "event": name, "data": data}) + self.settled = False + if self.thread is None: + self.thread = threading.Thread(target=self._run, name="agent-guild-reporter", daemon=True) + self.thread.start() + self.wake.set() + + def _take(self): + with self.lock: + messages = list(self.events) + self.events.clear() + o = self.open + snapshot = { + "sessions": {k: dict(v) for k, v in o["sessions"].items()}, + "working": sorted(o["working"]), + "subagents": {k: dict(v) for k, v in o["subagents"].items()}, + "tools": {k: dict(v) for k, v in o["tools"].items()}, + "waits": {k: dict(v) for k, v in o["waits"].items()}, + } + busy = any(snapshot.values()) + if busy or not self.settled: + self.seq += 1 + messages.append({"seq": self.seq, "open": snapshot}) + self.settled = not busy + return messages, self.token_file, self.home, self.stopped + + def _allowed(self, recorded, home): + """Whether a token issued for `recorded` may report for this profile's `home`; says once why it may not.""" + if home and recorded == home: + self.refused = None + return True + if self.refused != (recorded, home): + self.refused = (recorded, home) + logger.warning("Agent Guild reporting is off for this profile: its report token was issued for %s, not for %s. " + "Turn on agent reporting for this profile in Agent Guild.", recorded or "no home", home or "a home Hermes did not name") + return False + + def _run(self): + while True: + self.wake.wait(None if self.settled else HEARTBEAT_SECONDS) + self.wake.clear() + messages, path, home, final = self._take() + token, recorded, url = _settings(path) if messages else (None, None, None) + # A token copied with a cloned profile belongs to the profile it was issued for: it never reports this one. + if token and self._allowed(recorded, home): + self._post(url, token, messages) + if final: + return + + def stop(self): + with self.lock: + self.stopped = True + self.events.clear() + for items in self.open.values(): + items.clear() + self.turns.clear() + self.settled = False + self.wake.set() + + def _post(self, url, token, messages): + body = json.dumps({"instance": self.instance, "messages": messages}).encode("utf-8") + request = urllib.request.Request(f"{url}/api/v1/hermes/hook", data=body, method="POST", headers={ + "content-type": "application/json", "x-agent-guild-report-token": token, + }) + try: + with LOCAL.open(request, timeout=10) as response: + response.read() + self.down = False + except Exception as exc: + if not self.down: + logger.warning("agent-guild: cannot report to Agent Guild at %s (%s)", url, exc) + self.down = True + + +def register(ctx): + reporter = Reporter() + + def callback(name): + def hook(**kw): + try: + reporter.handle(name, kw) + except Exception: + pass + return None + return hook + + for name in HOOKS: + ctx.register_hook(name, callback(name)) + ctx.on_unload(reporter.stop) diff --git a/plugins/hermes/agent-guild/plugin.yaml b/plugins/hermes/agent-guild/plugin.yaml new file mode 100644 index 0000000..c657ea5 --- /dev/null +++ b/plugins/hermes/agent-guild/plugin.yaml @@ -0,0 +1,39 @@ +name: agent-guild +version: 1.0.0 +manifest_version: 2 +description: >- + Shows every Hermes session, subagent, shell command and approval live in Agent + Guild. Disclosure — sends session, tool, subagent, + and approval events, including shell commands and subagent goals after + Hermes's secret redaction (left out when it fails), only to + the Agent Guild manager on this computer (default http://127.0.0.1:47821); + runs one background thread per profile that reports; reads one file, where + Agent Guild keeps the profile's report token and its own address, and + installs no packages. +author: Odd Essentials +license: AGPL-3.0-only +homepage: https://github.com/oddessentials/agent-guild +tags: + - observability + - agents + - dashboard +requires_hermes: ">=0.23.0" +requires_env: + - name: AGENT_GUILD_HERMES_TOKEN_FILE + description: "The file Agent Guild keeps this profile's report token and its own address in. Agent Guild sets it when you turn on agent reporting on its Hermes Agent card." + url: "https://github.com/oddessentials/agent-guild/blob/main/docs/agent-reporting.md" + secret: false +provides_hooks: + - on_session_start + - on_session_end + - on_session_finalize + - on_session_delete + - pre_llm_call + - pre_tool_call + - post_tool_call + - subagent_start + - subagent_stop + - pre_approval_request + - post_approval_response + - on_human_input_request + - on_human_input_resolved diff --git a/src/manager/accounts.mjs b/src/manager/accounts.mjs index 3844548..cf21e66 100644 --- a/src/manager/accounts.mjs +++ b/src/manager/accounts.mjs @@ -115,6 +115,7 @@ export function createAccountEditor({ registry, inUse }) { const provider = registry.get(providerId); if (!provider) throw refusal(404, 'unknown_provider', `unknown provider "${providerId}"`); if (!provider.homeVar) throw refusal(409, 'accounts_unsupported', `${provider.tool} keeps a single sign-in.`); + if (provider.profiles) throw refusal(409, 'accounts_unsupported', `${provider.tool}'s accounts are its profiles. Create one in ${provider.tool}, then reload the cards.`); if (revision !== null && typeof revision !== 'string') throw refusal(400, 'bad_revision', 'revision must be a string or null'); const file = registry.userFile; if (!file) throw refusal(409, 'accounts_unsupported', 'This manager has no providers.json to keep accounts in.'); diff --git a/src/manager/config.mjs b/src/manager/config.mjs index 2a5abe5..f5544c3 100644 --- a/src/manager/config.mjs +++ b/src/manager/config.mjs @@ -16,6 +16,8 @@ export const DEFAULT_HOST = '127.0.0.1'; export const PACKAGE_FILE = fileURLToPath(new URL('../../package.json', import.meta.url)); const packageJson = JSON.parse(fs.readFileSync(PACKAGE_FILE, 'utf8')); export const VERSION = packageJson.version; +/** The commit a release was built from, which the release stamps; null in any other build. */ +export const COMMIT = packageJson.agentGuild?.commit ?? null; export const PACKAGE_NAME = packageJson.name; /** Per-user data directory. Override with AGENT_GUILD_HOME (used by tests). */ @@ -52,6 +54,7 @@ export const paths = { /** Launchers for agent-guild-report, put first on every session's PATH. */ get shims() { return path.join(dataDir(), 'bin'); }, get reporting() { return path.join(dataDir(), 'reporting'); }, + get hermesTokens() { return path.join(dataDir(), 'hermes-tokens'); }, /** The tmux and herdr cards a restarted manager brings back, report tokens included. */ get multiplexers() { return path.join(dataDir(), 'multiplexers.json'); }, /** The Yard's art packs, one folder per content id. */ diff --git a/src/manager/hermes.mjs b/src/manager/hermes.mjs new file mode 100644 index 0000000..11a0457 --- /dev/null +++ b/src/manager/hermes.mjs @@ -0,0 +1,519 @@ +import crypto from 'node:crypto'; +import fs from 'node:fs'; +import path from 'node:path'; +import { EventEmitter } from 'node:events'; +import { expandHome, homeDir } from './install-channels.mjs'; +import { envValue } from './multiplexer-paths.mjs'; +import { renameRetrying } from './yard-art.mjs'; + +export const HERMES_EXPIRY_MS = 90 * 1000; +const SWEEP_MS = 15 * 1000; +const MAX_HELPERS = 64; +const SHELL_TOOLS = new Set(['terminal', 'process']); +const PREFIX = 'hermes:'; + +const PROFILE_ID_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/; +const PROFILE_MARKERS = ['config.yaml', '.env', 'SOUL.md', 'profile.yaml', 'auth.json', 'state.db']; + +const text = (value) => (typeof value === 'string' && value ? value : null); +const pathFor = (platform) => (platform === 'win32' ? path.win32 : path.posix); + +/** One name for a Hermes home, however its path is spelled. */ +export function hermesHomeKey(dir, platform = process.platform) { + let real; + try { real = fs.realpathSync.native(dir); } catch { real = pathFor(platform).resolve(dir); } + return platform === 'win32' ? real.toLowerCase() : real; +} + +/** The root `hermes -p` resolves profiles in, for a process started with `env`. */ +export function hermesRoot(env, platform) { + const p = pathFor(platform); + const get = (name) => (platform === 'win32' ? envValue(env, name) : env[name]); + const raw = (get('HERMES_HOME') ?? '').trim(); + if (!raw) { + const suffix = get('HERMES_DATA_DIR_SUFFIX') ?? ''; + const home = homeDir(env, platform); + if (platform !== 'win32') return p.join(home, `.hermes${suffix}`); + return p.join((get('LOCALAPPDATA') ?? '').trim() || p.join(home, 'AppData', 'Local'), `hermes${suffix}`); + } + const reference = platform === 'win32' ? /%([^%]+)%|\$\{([^}]+)\}|\$(\w+)/g : /\$\{([^}]+)\}|\$(\w+)/g; + const expanded = raw.replace(reference, (whole, ...names) => get(names.find((name) => typeof name === 'string')) ?? whole); + const dir = p.resolve(expandHome(expanded, env, platform)); + return p.basename(p.dirname(dir)) === 'profiles' ? p.dirname(p.dirname(dir)) : dir; +} + +/** Hermes's root as the default account, then each profile Hermes itself lists there. */ +export function hermesProfiles(env, platform) { + const p = pathFor(platform); + const root = hermesRoot(env, platform); + const profiles = p.join(root, 'profiles'); + const marked = (file) => { + try { + const stat = fs.lstatSync(file); + return stat.isFile() || stat.isSymbolicLink(); + } catch { + return false; + } + }; + const isDir = (dir) => { + try { return fs.statSync(dir).isDirectory(); } catch { return false; } + }; + let names = []; + try { names = fs.readdirSync(profiles).sort(); } catch { /* no profiles yet */ } + names = names.filter((name) => name !== 'default' && PROFILE_ID_RE.test(name) && isDir(p.join(profiles, name)) + && PROFILE_MARKERS.some((file) => marked(p.join(profiles, name, file))) && !fs.existsSync(p.join(profiles, '.deleted', name))); + // `root` is the absolute HERMES_HOME every command of the account runs with, so none resolves a relative one elsewhere. + const account = (id, label, dir) => ({ id, label, dir, profile: id, root, home: hermesHomeKey(dir, platform) }); + return [account('default', 'Default', root), ...names.map((name) => account(name, name, p.join(profiles, name)))]; +} + +/** + * Which report token each Hermes home has. `valid.json`, read only by the manager, is the one record of the tokens it + * accepts; every change to it is made synchronously and saved before it counts. Each home's plugin reads a copy, one file + * per home that only the user can read, holding the token, the home and the manager's address, one per line. + */ +export class HermesTokens { + constructor(dir) { + this.dir = dir; + /** The homes whose file still names an address the manager has left. */ + this.stale = new Set(); + /** The homes the cards have, once told; a token of any other home is refused. */ + this.homes = null; + this.queues = new Map(); + this.setups = new Map(); + /** Why the saved record could not be read, until a change replaces it; no token is accepted meanwhile. */ + this.unreadable = null; + /** True while a file whose token was revoked could not be deleted yet, so the next prune tries again. */ + this.leftover = false; + this.record = this._load(); + } + + get recordFile() { + return path.join(this.dir, 'valid.json'); + } + + /** The saved record. None saved yet is an empty record; one that cannot be read accepts nothing and says why. */ + _load() { + let text; + try { + text = fs.readFileSync(this.recordFile, 'utf8'); + } catch (err) { + if (err.code !== 'ENOENT') this._unreadable(err.code || err.message); + return new Map(); + } + let saved; + try { saved = JSON.parse(text); } catch { /* reported below */ } + if (!saved || typeof saved !== 'object' || Array.isArray(saved)) { + this._unreadable('it is not a valid record'); + return new Map(); + } + return new Map(Object.entries(saved).filter(([home, token]) => home && typeof token === 'string' && token)); + } + + _unreadable(reason) { + this.unreadable = reason; + console.error(`[hermes] no report token is accepted: ${this.recordFile} could not be read (${reason})`); + } + + /** The only way the record changes: the change counts once it is saved, and not at all when saving fails. */ + _commit(change) { + const next = new Map(this.record); + change(next); + fs.mkdirSync(this.dir, { recursive: true, mode: 0o700 }); + const tmp = `${this.recordFile}.${crypto.randomBytes(6).toString('hex')}.tmp`; + try { + fs.writeFileSync(tmp, JSON.stringify(Object.fromEntries(next)), { mode: 0o600 }); + fs.renameSync(tmp, this.recordFile); + } catch (err) { + fs.rmSync(tmp, { force: true }); + throw err; + } + this.record = next; + this.unreadable = null; + } + + _carded(home) { + return !this.homes || this.homes.has(home); + } + + /** Runs one home's file work whole, after whatever was asked for that home before. The record never waits on it. */ + _serial(home, work, queues = this.queues) { + const run = (queues.get(home) ?? Promise.resolve()).then(work); + const settled = run.catch(() => {}); + queues.set(home, settled); + settled.then(() => { if (queues.get(home) === settled) queues.delete(home); }); + return run; + } + + /** + * Runs one whole turning on or off of a home's reporting, its failure handling included, after any asked for that home + * before, so a request never acts on what another changed meanwhile. It may use the token methods, which never wait on it. + */ + setup(home, work) { + return this._serial(home, work, this.setups); + } + + /** The file a home's plugin reads its token from. */ + file(home) { + return path.join(this.dir, `${crypto.createHash('sha256').update(home).digest('hex').slice(0, 32)}.token`); + } + + _read(file) { + try { + const [token, home, url = ''] = fs.readFileSync(file, 'utf8').split('\n'); + return token && home ? { file, token, home, url } : null; + } catch { + return null; + } + } + + _files() { + let names = []; + try { names = fs.readdirSync(this.dir).filter((name) => name.endsWith('.token')); } catch { /* none yet */ } + return names.map((name) => this._read(path.join(this.dir, name))).filter(Boolean); + } + + /** The home's accepted token, or null. */ + get(home) { + return this._carded(home) ? this.record.get(home) ?? null : null; + } + + /** Deletes the file of a home whose token is revoked, waiting out a reader that holds it. False when it is still there. */ + async _delete(home) { + if (this.record.has(home)) return true; + const file = this.file(home); + if (fs.existsSync(file)) { + const gone = `${file}.${crypto.randomBytes(6).toString('hex')}.tmp`; + try { + await renameRetrying(file, gone); + } catch { + return false; + } + fs.rmSync(gone, { force: true }); + } + this.stale.delete(home); + return true; + } + + async _write(home, token, url) { + fs.mkdirSync(this.dir, { recursive: true, mode: 0o700 }); + const file = this.file(home); + const next = `${file}.${crypto.randomBytes(6).toString('hex')}.tmp`; + fs.writeFileSync(next, `${token}\n${home}\n${url ?? ''}\n`, { mode: 0o600 }); + try { + await renameRetrying(next, file); + } catch (err) { + fs.rmSync(next, { force: true }); + throw err; + } + } + + /** + * The home's token, saved in the record first when it has none, then copied to the home's file with the manager's address. + * A copy that cannot be written leaves the home stale, so it does not count as reporting. + */ + ensure(home, url = '') { + let token; + try { + if (!this._carded(home)) throw new Error('No card has this Hermes home any more'); + token = this.record.get(home); + if (!token) { + token = crypto.randomBytes(24).toString('hex'); + this._commit((record) => record.set(home, token)); + } + } catch (err) { + return Promise.reject(err); + } + return this._serial(home, async () => { + if (this.record.get(home) !== token) throw new Error('Reporting for this Hermes home was turned off meanwhile'); + const known = this._read(this.file(home)); + if (known?.token !== token || known.url !== (url ?? '')) { + try { + await this._write(home, token, url); + } catch (err) { + this.stale.add(home); + throw err; + } + } + this.stale.delete(home); + return token; + }); + } + + /** Gives every home the manager's address, as after a start on another port. A file that cannot be replaced keeps its contents and its home is stale. */ + async refresh(url) { + for (const home of [...this.record.keys()]) { + await this._serial(home, async () => { + const token = this.get(home); + const known = this._read(this.file(home)); + if (!token || (known?.token === token && known.url === url)) return; + try { + await this._write(home, token, url); + this.stale.delete(home); + } catch (err) { + this.stale.add(home); + console.error(`[hermes] could not give ${home} the manager's address: ${this.file(home)} could not be replaced (${err.code || err.message})`); + } + }); + } + } + + /** + * Revokes the home's token: rejects, changing nothing, when the record cannot be saved. Once saved the token is refused, + * then its file is deleted; resolves false when the file is still there, and a later prune deletes it. + */ + remove(home) { + try { + if (this.record.has(home)) this._commit((record) => record.delete(home)); + } catch (err) { + return Promise.reject(err); + } + return this._serial(home, () => this._delete(home)).then((done) => { + if (!done) this.leftover = true; + return done; + }); + } + + /** + * Takes the homes the cards have now and revokes the token of every other home, saved before anything else. Then it deletes + * every file whose token is not accepted; resolves true when none is left. + */ + prune(homes) { + const same = this.homes && homes.size === this.homes.size && [...homes].every((home) => this.homes.has(home)); + this.homes = homes; + const gone = [...this.record.keys()].filter((home) => !homes.has(home)); + try { + if (gone.length) this._commit((record) => { for (const home of gone) record.delete(home); }); + } catch (err) { + return Promise.reject(err); + } + if (same && !gone.length && !this.leftover) return Promise.resolve(true); + this.leftover = false; + const orphans = new Set(this._files().map((entry) => entry.home).filter((home) => !this.record.has(home))); + return Promise.all([...orphans].map((home) => this._serial(home, () => this._delete(home)))).then((done) => { + const clean = done.every(Boolean); + if (!clean) this.leftover = true; + return clean; + }); + } + + account(token) { + if (typeof token !== 'string' || !token) return null; + const given = Buffer.from(token); + for (const [home, known] of this.record) { + if (!this._carded(home)) continue; + const expected = Buffer.from(known); + if (expected.length === given.length && crypto.timingSafeEqual(expected, given)) return home; + } + return null; + } +} + +function newInstance(now) { + return { seq: 0, at: now, sessions: new Map(), subagents: new Map(), tools: new Map(), waits: new Map() }; +} + +/** + * What the Hermes plugin reports, per account and per reporting process (instance). Emits `changed` with an account id. + */ +export class HermesLive extends EventEmitter { + constructor({ now = Date.now, expiryMs = HERMES_EXPIRY_MS, sweepMs = SWEEP_MS } = {}) { + super(); + this.now = now; + this.expiryMs = expiryMs; + this.accounts = new Map(); + this.muted = new Set(); + this.timer = sweepMs ? setInterval(() => this.sweep(), sweepMs) : null; + this.timer?.unref?.(); + } + + close() { + clearInterval(this.timer); + } + + /** Clears what an account reports and refuses its reports, while its reporting is being turned off. */ + mute(accountId) { + this.muted.add(accountId); + this.accounts.delete(accountId); + this.emit('changed', accountId); + } + + unmute(accountId) { + this.muted.delete(accountId); + } + + receive(accountId, instanceId, messages) { + if (this.muted.has(accountId)) return; + if (typeof instanceId !== 'string' || !/^[A-Za-z0-9_-]{1,64}$/.test(instanceId)) throw Object.assign(new Error('instance must be an id'), { status: 400, code: 'bad_request' }); + if (!Array.isArray(messages)) throw Object.assign(new Error('messages must be an array'), { status: 400, code: 'bad_request' }); + let instances = this.accounts.get(accountId); + if (!instances) this.accounts.set(accountId, instances = new Map()); + let inst = instances.get(instanceId); + if (!inst) instances.set(instanceId, inst = newInstance(this.now())); + inst.at = this.now(); + for (const message of messages) { + if (!message || typeof message !== 'object' || !Number.isSafeInteger(message.seq) || message.seq <= inst.seq) continue; + inst.seq = message.seq; + if (message.open && typeof message.open === 'object') this._reconcile(inst, message.open); + else if (typeof message.event === 'string') this._event(accountId, inst, message.event, message.data && typeof message.data === 'object' ? message.data : {}); + } + this.emit('changed', accountId); + } + + _entity(inst, sid) { + return inst.subagents.get(sid) ?? inst.sessions.get(sid) ?? null; + } + + _session(inst, sid, data) { + if (inst.subagents.has(sid)) return inst.subagents.get(sid); + const session = inst.sessions.get(sid) ?? { platform: null, working: false }; + inst.sessions.set(sid, session); + session.platform = text(data.platform) ?? session.platform; + return session; + } + + _forget(inst, sid) { + inst.sessions.delete(sid); + for (const [id, tool] of inst.tools) if (tool.sid === sid) inst.tools.delete(id); + for (const [id, owner] of inst.waits) if (owner === sid) inst.waits.delete(id); + } + + _event(accountId, inst, event, data) { + const sid = text(data.session_id); + const call = text(data.tool_call_id) && `${sid ?? ''}:${data.tool_call_id}`; + const child = text(data.child_session_id); + switch (event) { + case 'on_session_start': + if (sid) this._session(inst, sid, data); + break; + case 'on_session_end': { + const entity = sid && this._entity(inst, sid); + if (entity) entity.working = false; + break; + } + case 'on_session_finalize': + case 'on_session_delete': + if (sid) this._forget(inst, sid); + break; + case 'pre_llm_call': + case 'pre_tool_call': + if (sid) this._session(inst, sid, data).working = true; + if (event === 'pre_tool_call' && call) inst.tools.set(call, { sid, shell: SHELL_TOOLS.has(data.tool_name) }); + break; + case 'post_tool_call': + if (call) inst.tools.delete(call); + break; + case 'subagent_start': + if (child) inst.subagents.set(child, { parent: text(data.parent_session_id), role: text(data.child_role), goal: text(data.child_goal), working: true }); + break; + case 'subagent_stop': + if (child) { + inst.subagents.delete(child); + this._forget(inst, child); + } + break; + case 'pre_approval_request': + if (call) inst.waits.set(`approval:${call}`, sid); + break; + case 'post_approval_response': + if (call) inst.waits.delete(`approval:${call}`); + break; + case 'on_human_input_request': + if (text(data.request_id)) inst.waits.set(`input:${data.request_id}`, sid); + break; + case 'on_human_input_resolved': + if (text(data.request_id)) inst.waits.delete(`input:${data.request_id}`); + break; + default: + } + } + + _reconcile(inst, open) { + const items = (kind) => new Map(Object.entries(open[kind] && typeof open[kind] === 'object' && !Array.isArray(open[kind]) ? open[kind] : {}) + .map(([id, facts]) => [id, facts && typeof facts === 'object' ? facts : {}])); + const sessions = items('sessions'); + const subagents = items('subagents'); + const tools = items('tools'); + const waits = items('waits'); + const working = new Set(Array.isArray(open.working) ? open.working.filter((id) => typeof id === 'string') : []); + for (const sid of [...inst.sessions.keys()]) { + if (subagents.has(sid)) inst.sessions.delete(sid); + else if (!sessions.has(sid)) this._forget(inst, sid); + } + for (const [sid, facts] of sessions) { + if (subagents.has(sid)) continue; + const session = inst.sessions.get(sid) ?? { platform: null, working: false }; + session.platform = text(facts.platform) ?? session.platform; + inst.sessions.set(sid, session); + } + for (const child of [...inst.subagents.keys()]) if (!subagents.has(child)) inst.subagents.delete(child); + for (const [child, facts] of subagents) { + const agent = inst.subagents.get(child) ?? { parent: null, role: null, goal: null, working: true }; + agent.parent = text(facts.parent_session_id) ?? agent.parent; + agent.role = text(facts.child_role) ?? agent.role; + agent.goal = text(facts.child_goal) ?? agent.goal; + inst.subagents.set(child, agent); + } + for (const session of inst.sessions.values()) session.working = false; + for (const sid of working) { + const entity = this._entity(inst, sid); + if (entity) entity.working = true; + } + for (const call of [...inst.tools.keys()]) if (!tools.has(call)) inst.tools.delete(call); + for (const [call, facts] of tools) if (!inst.tools.has(call)) inst.tools.set(call, { sid: text(facts.session_id), shell: SHELL_TOOLS.has(facts.tool_name) }); + for (const key of [...inst.waits.keys()]) if (!waits.has(key)) inst.waits.delete(key); + for (const [key, facts] of waits) if (!inst.waits.has(key)) inst.waits.set(key, text(facts.session_id)); + } + + /** Drops the instances that have not reported for the expiry time. */ + sweep() { + const now = this.now(); + for (const [accountId, instances] of this.accounts) { + let dropped = false; + for (const [id, inst] of instances) { + if (now - inst.at >= this.expiryMs) { + instances.delete(id); + dropped = true; + } + } + if (!instances.size) this.accounts.delete(accountId); + if (dropped) this.emit('changed', accountId); + } + } + + _shown(accountId) { + const agents = new Map(); + const shells = new Map(); + const add = (agent) => { + if (agents.get(agent.agentId)?.status !== 'waiting') agents.set(agent.agentId, agent); + }; + for (const inst of this.accounts.get(accountId)?.values() ?? []) { + const waiting = new Set(inst.waits.values()); + for (const [sid, session] of inst.sessions) { + if (waiting.has(sid) || session.working) add({ agentId: `${PREFIX}${sid}`, name: session.platform ?? 'hermes', kind: 'session', status: waiting.has(sid) ? 'waiting' : 'working' }); + } + for (const [child, agent] of inst.subagents) { + add({ agentId: `${PREFIX}${child}`, name: agent.goal ?? agent.role ?? 'subagent', kind: agent.role ?? 'subagent', status: waiting.has(child) ? 'waiting' : 'working', detail: agent.goal ?? '' }); + } + for (const [call, tool] of inst.tools) if (tool.shell && tool.sid) shells.set(`${PREFIX}${call}`, { key: `${PREFIX}${call}`, agentId: `${PREFIX}${tool.sid}` }); + } + const rank = { waiting: 0, working: 1 }; + return { agents: [...agents.values()].sort((a, b) => rank[a.status] - rank[b.status]), shells: [...shells.values()] }; + } + + view(accountId) { + const { agents, shells } = this._shown(accountId); + return { agents: agents.slice(0, MAX_HELPERS), shells }; + } + + live(accountId) { + if (!this.accounts.has(accountId)) return null; + const { agents, shells } = this._shown(accountId); + return { + working: agents.filter((a) => a.status === 'working').length, + waiting: agents.filter((a) => a.status === 'waiting').length, + agents: agents.slice(0, MAX_HELPERS).map((a) => ({ id: a.agentId, name: a.name, status: a.status, detail: a.detail ?? '' })), + shells: shells.map((s) => ({ id: s.key })), + }; + } +} diff --git a/src/manager/main.mjs b/src/manager/main.mjs index 0853dfc..0fd4e0e 100644 --- a/src/manager/main.mjs +++ b/src/manager/main.mjs @@ -26,7 +26,8 @@ import { RemoteAccess, loadRemoteAccess } from './remote-access.mjs'; import { SelfUpdate } from './self-update.mjs'; import { addRuntimePaths, resolveBaseEnv, pathReader } from './shell-env.mjs'; import { nativeReporter, writeReportShims } from './report-shims.mjs'; -import { SessionHooks } from './session-hooks.mjs'; +import { HERMES_PLUGIN_COMMIT_VAR, SessionHooks } from './session-hooks.mjs'; +import { HermesLive, HermesTokens } from './hermes.mjs'; import { McpConnect, ownServer } from './mcp-connect.mjs'; import { launcherPath, nextManager } from './launch.mjs'; import { createAutostart } from './autostart.mjs'; @@ -36,6 +37,7 @@ import { PACKAGE_FILE, PACKAGE_NAME, VERSION, + COMMIT, dataDir, ensureDataDir, loadOrCreateToken, @@ -55,7 +57,7 @@ const VERSION_REFRESH_MS = 60 * 60 * 1000; * `version`, `packageFile`, `github` (GitHub's URLs and client id), `autostart`, `boot` and `execPath` (the Node.js whose folders join PATH) stand in for the real ones in tests. * `supervised`: started by the Linux boot service, so systemd starts the next manager after a restart. */ -export async function startManager({ port = resolvePort(), host = DEFAULT_HOST, sessionDefaults, version = VERSION, packageFile = PACKAGE_FILE, github: githubOptions = {}, remoteAccess: remoteOptions = {}, autostart = null, boot, supervised = false, execPath = process.execPath } = {}) { +export async function startManager({ port = resolvePort(), host = DEFAULT_HOST, sessionDefaults, version = VERSION, packageFile = PACKAGE_FILE, hermesPluginCommit = COMMIT || process.env[HERMES_PLUGIN_COMMIT_VAR] || null, github: githubOptions = {}, remoteAccess: remoteOptions = {}, autostart = null, boot, supervised = false, execPath = process.execPath } = {}) { // Validate before creating files, processes or timers, so a typo, or a // system node-pty cannot run on, fails startup cleanly. loadPty(); @@ -87,13 +89,31 @@ export async function startManager({ port = resolvePort(), host = DEFAULT_HOST, let api; const selfUpdate = new SelfUpdate({ pkg: PACKAGE_NAME, version, packageFile, registry, execPath }); const github = new GitHub({ dir: paths.github, registry, ...githubOptions }); - const sessionHooks = new SessionHooks({ registry, dir: paths.reporting, version }); + const hermesTokens = new HermesTokens(paths.hermesTokens); + const hermesLive = new HermesLive(); + const sessionHooks = new SessionHooks({ registry, dir: paths.reporting, version, hermesTokens, hermesPluginCommit, getApiUrl: () => api.url }); registry.reportingEnabled = (provider) => sessionHooks.enabled(provider); registry.reportingNote = (provider) => sessionHooks.note(provider); + registry.reportingUpdate = (provider) => sessionHooks.updatable(provider); + registry.liveFor = (provider, account) => (provider.reporting === 'hermes' && account.home ? hermesLive.live(account.home) : null); + const pruneHermesTokens = () => { + const homes = registry.providers.filter((p) => p.reporting === 'hermes').flatMap((p) => p.accounts.map((a) => a.home).filter(Boolean)); + hermesTokens.prune(new Set(homes)).catch((err) => console.error(`[hermes] could not update the report tokens: ${err.code || err.message}`)); + }; + pruneHermesTokens(); + registry.on('updated', pruneHermesTokens); const cardNames = createCardNames(paths.cardNames); const manager = new SessionManager({ registry, baseEnv, getApiUrl: () => api.url, sessionDefaults, shimDir, selfUpdate, github, sessionHooks, store: multiplexerStore, cardNames, }); + const shownLive = new Map(); + hermesLive.on('changed', (home) => { + const live = JSON.stringify(hermesLive.live(home)); + if (shownLive.get(home) !== live) { + shownLive.set(home, live); + registry.emit('updated'); + } + }); const mcpConnect = new McpConnect({ registry, own: ownServer(), prepareAccount: (provider, account) => manager.prepareAccount(provider, account), dir: dataDir() }); registry.mcpState = (provider) => mcpConnect.describe(provider); environment.setSessionLookup((id) => { @@ -141,6 +161,7 @@ export async function startManager({ port = resolvePort(), host = DEFAULT_HOST, if (closing) return closing; console.log(`[manager] ${restart ? 'restarting' : 'stopping'} (${reason}); ending ${manager.sessions.size} session(s)`); clearInterval(versionTimer); + hermesLive.close(); environment.close(); remoteAccess.close(); github.close(); @@ -191,6 +212,7 @@ export async function startManager({ port = resolvePort(), host = DEFAULT_HOST, yardWorlds: new YardWorlds({ dir: paths.yardWorlds }), yardPieces: new YardPieces({ dir: paths.yardPieces }), mcpConnect, + hermes: { tokens: hermesTokens, live: hermesLive }, onShutdownRequest: ({ restart = false } = {}) => shutdown('requested via API', { restart }).then((code) => process.exit(code)), }); remoteAccess.attach({ apply: (access) => api.setAccessPolicy(access), getTarget: () => api.url }); @@ -204,6 +226,7 @@ export async function startManager({ port = resolvePort(), host = DEFAULT_HOST, } throw err; } + await hermesTokens.refresh(api.url); // Only now: a manager that cannot take its port never runs its shutdown, which is what ends these probes. sessionHooks.warm(); // tmux and herdr cards come back closed, ready to reattach, before the diff --git a/src/manager/mcp-connect.mjs b/src/manager/mcp-connect.mjs index 9f63756..d862f19 100644 --- a/src/manager/mcp-connect.mjs +++ b/src/manager/mcp-connect.mjs @@ -10,7 +10,8 @@ import { PACKAGE_SCRIPT } from './launch.mjs'; import { dataDir, dataDirFor } from './config.mjs'; export const SERVER_NAME = 'agent-guild'; -export const MCP_MODES = ['claude', 'codex', 'antigravity', 'grok']; +export const MCP_MODES = ['claude', 'codex', 'antigravity', 'grok', 'hermes']; +const HERMES_KEY = `mcp_servers.${SERVER_NAME}`; export const MCP_ACTIONS = ['on', 'off', 'replace']; const STATE_TTL_MS = 60 * 1000; const RUN_TIMEOUT_MS = 30000; @@ -87,6 +88,29 @@ function lastLine(err) { const grokConfigError = (stderr) => String(stderr || '').split('\n').map((l) => l.trim()).find((l) => /\bERROR\b.*\bconfig\b/.test(l)) ?? null; const TOOLS = { + hermes: { + async read(ctx) { + let stdout; + try { + ({ stdout } = await ctx.run(['config', 'get', HERMES_KEY, '--json'])); + } catch (err) { + if (/Config key not set/.test(`${err.stdout || ''}${err.stderr || ''}`)) return { entry: null }; + throw err; + } + let raw; + try { + raw = JSON.parse(stdout); + } catch { + return { error: 'its MCP settings were not in a form Agent Guild recognizes' }; + } + return { entry: fromJson(raw, raw?.enabled !== false) }; + }, + add: (own) => [['config', 'set', HERMES_KEY, JSON.stringify({ command: own.command, args: own.args, trust: 'untrusted', enabled: true })]], + enable: () => [['config', 'set', `${HERMES_KEY}.enabled`, 'true']], + disable: () => [['config', 'set', `${HERMES_KEY}.enabled`, 'false']], + remove: () => [['config', 'unset', HERMES_KEY]], + replace: (own) => TOOLS.hermes.add(own), + }, claude: { // Claude Code starts MCP servers from Node, which keeps the first of Windows variables that differ only in case. firstKeyWins: true, @@ -369,7 +393,7 @@ export class McpConnect { // A managed home that does not exist yet holds no settings, and some tools create it when asked. if (account.dir && !fs.existsSync(account.dir)) return { state: 'off', fingerprint: fingerprint(null) }; const platform = this.registry.platform; - const ctx = { env: account.env, platform, run: (args) => this._run(provider, resolved, account.env, args, { signal: this.ending.signal }) }; + const ctx = { env: account.env, platform, run: (args) => this._run(provider, resolved, account.env, [...(account.account.args ?? []), ...args], { signal: this.ending.signal }) }; let read; try { read = await tool.read(ctx); @@ -478,7 +502,7 @@ export class McpConnect { } try { if (want === 'on' && account.dir) this.prepareAccount(provider, account.account); - for (const args of steps) await this._run(provider, resolved, account.env, args); + for (const args of steps) await this._run(provider, resolved, account.env, [...(account.account.args ?? []), ...args]); } catch (err) { const after = await this.readAccount(provider, resolved, account); return { outcome: 'failed', state: after.state, detail: `${provider.command} mcp failed: ${lastLine(err)}` }; @@ -487,7 +511,7 @@ export class McpConnect { // Removing its own entry can leave the tool loading one from another tool's settings. if (action === 'off' && after.state === 'on' && after.from && tool.disable) { try { - for (const args of tool.disable()) await this._run(provider, resolved, account.env, args); + for (const args of tool.disable()) await this._run(provider, resolved, account.env, [...(account.account.args ?? []), ...args]); } catch (err) { return { outcome: 'failed', state: after.state, detail: `${provider.command} mcp failed: ${lastLine(err)}` }; } diff --git a/src/manager/providers.mjs b/src/manager/providers.mjs index bc84d93..d01e1ca 100644 --- a/src/manager/providers.mjs +++ b/src/manager/providers.mjs @@ -23,12 +23,13 @@ import { reconcileHerdrPath } from './multiplexer-paths.mjs'; import { refreshLaunchers } from './launchers.mjs'; import { LaunchFlags, normalizeLaunchFlags } from './launch-flags.mjs'; import { MCP_MODES } from './mcp-connect.mjs'; +import { hermesProfiles } from './hermes.mjs'; const here = path.dirname(fileURLToPath(import.meta.url)); export const DEFAULTS_FILE = path.resolve(here, '../../config/providers.default.json'); export const ID_RE = /^[a-z0-9][a-z0-9_-]{0,31}$/; const PLATFORM_KEYS = ['win32', 'darwin', 'linux']; -const REPORTING_MODES = new Set(['claude', 'codex', 'antigravity', 'grok', 'docker']); +const REPORTING_MODES = new Set(['claude', 'codex', 'antigravity', 'grok', 'docker', 'hermes']); const VERSION_TTL_MS = 60 * 60 * 1000; const FAILED_PROBE_TTL_MS = 5 * 60 * 1000; const PATH_REFRESH_MS = 60 * 1000; @@ -185,6 +186,7 @@ function normalize(raw, platform, warnings) { accountEnv: normalizeEnv(merged.accountEnv), hooks: normalizeHooks(merged.hooks), accounts: normalizeAccounts(merged.accounts, merged.id, homeVar, warnings), + profiles: merged.profiles === 'hermes' ? 'hermes' : null, color: String(merged.color || '#64748B'), monogram: String(merged.monogram || String(merged.vendor || merged.id).charAt(0)).slice(0, 2), icon: merged.icon ? String(merged.icon) : null, @@ -314,6 +316,8 @@ export class ProviderRegistry extends EventEmitter { this._npmRegistry = null; this.reportingEnabled = null; this.reportingNote = null; + this.reportingUpdate = null; + this.liveFor = null; this.mcpState = null; this.multiplexers = new MultiplexerRegistry(this, multiplexerOptions); this.multiplexerState = null; @@ -323,6 +327,12 @@ export class ProviderRegistry extends EventEmitter { /** `accountsOnly` keeps what was discovered about each tool, for a change to nothing but its accounts. */ reload({ accountsOnly = false } = {}) { const { providers, warnings, revision } = loadProviders({ userFile: this.userFile, platform: this.platform }); + for (const provider of providers) { + if (!provider.profiles) continue; + provider.accounts = hermesProfiles({ ...this.env, ...provider.env }, this.platform); + // Hermes's Windows installer puts its launcher in the home it installs to. + if (this.platform === 'win32' && provider.channels.native) provider.channels.native.paths.push(path.win32.join(provider.accounts[0].dir, 'bin')); + } this.providers = providers; this.warnings = warnings; this.revision = revision; @@ -755,7 +765,8 @@ export class ProviderRegistry extends EventEmitter { } accountFor(provider, account) { - if (account.id === DEFAULT_ACCOUNT || !provider.homeVar) return { id: account.id, label: account.label, dir: null, env: {} }; + if (provider.profiles) return { id: account.id, label: account.label, dir: account.dir, home: account.home, env: { HERMES_HOME: account.root }, args: ['-p', account.profile] }; + if (!provider.homeVar || account.id === DEFAULT_ACCOUNT) return { id: account.id, label: account.label, dir: null, env: {} }; const p = this.platform === 'win32' ? path.win32 : path.posix; const own = account.dir ? expandHome(account.dir, this.env, this.platform) : null; const dir = own && p.isAbsolute(own) ? p.normalize(own) : p.join(this.accountsDir, provider.id, own || account.id); @@ -893,9 +904,10 @@ export class ProviderRegistry extends EventEmitter { reporting: provider.reporting, reportingEnabled: this.reportingEnabled?.(provider) ?? null, reportingNote: this.reportingNote?.(provider) ?? null, + reportingUpdate: this.reportingUpdate?.(provider) ?? false, mcp: this.mcpState?.(provider) ?? null, - accounts: provider.accounts.map((account) => ({ id: account.id, label: account.label })), - accountsEditable: Boolean(provider.homeVar), + accounts: provider.accounts.map((account) => ({ id: account.id, label: account.label, live: this.liveFor?.(provider, account) ?? null })), + accountsEditable: Boolean(provider.homeVar) && !provider.profiles, shells: shells?.shells.map((shell) => ({ id: shell.id, label: shell.label, path: shell.path, multiplexer: Boolean(shell.multiplexer) })) ?? null, defaultShell: shells?.defaultId ?? null, multiplexers: this.multiplexers.describe(provider).map((m) => ({ ...m, ...this.multiplexerState?.(provider.id, m.id) })), diff --git a/src/manager/server.mjs b/src/manager/server.mjs index b14125b..454bae1 100644 --- a/src/manager/server.mjs +++ b/src/manager/server.mjs @@ -191,6 +191,7 @@ export function createManagerServer({ yardWorlds = new YardWorlds(), yardPieces = new YardPieces(), mcpConnect = null, + hermes = null, heartbeatMs = 30000, }) { let listedProviders = null; @@ -368,6 +369,17 @@ export function createManagerServer({ return sendJson(res, 200, { toolSessionId: manager.reportToolSession(id, body, auth) }); } + if (route === '/hermes/hook' && method === 'POST' && hermes) { + const reportToken = req.headers['x-agent-guild-report-token']; + const home = hermes.tokens.account(reportToken); + if (!home) throw new HttpError(401, 'invalid report token', 'unauthorized'); + const body = await readJsonBody(req, REPLY_BODY_LIMIT); + // Checked again as it is applied: a token revoked while the body arrived restores nothing. + if (hermes.tokens.account(reportToken) !== home) throw new HttpError(401, 'invalid report token', 'unauthorized'); + hermes.live.receive(home, body.instance, body.messages); + return sendJson(res, 200, { ok: true }); + } + if (route === '/sign-in' && method === 'POST') return sendJson(res, 201, { signIn: deviceSignIn.request() }); if (route === '/sign-in/poll' && method === 'POST') { const { device } = await readJsonBody(req); @@ -551,9 +563,23 @@ export function createManagerServer({ const provider = registry.get(reportingMatch[1]); if (!provider) throw new HttpError(404, `unknown provider "${reportingMatch[1]}"`, 'unknown_provider'); const body = await readJsonBody(req); + if (body.update === true) { + try { + await manager.sessionHooks.update(provider); + } finally { + registry.emit('updated'); + } + return sendJson(res, 200, { provider: registry.describe(provider) }); + } if (typeof body.enabled !== 'boolean') throw new HttpError(400, 'enabled must be true or false', 'bad_request'); - await manager.sessionHooks.setEnabled(provider, body.enabled); - registry.emit('updated'); + const muted = hermes && provider.reporting === 'hermes' && !body.enabled ? registry.accountsFor(provider).map((account) => account.home) : []; + for (const home of muted) hermes.live.mute(home); + try { + await manager.sessionHooks.setEnabled(provider, body.enabled); + } finally { + for (const home of muted) hermes.live.unmute(home); + registry.emit('updated'); + } return sendJson(res, 200, { provider: registry.describe(provider) }); } const mcpMatch = route.match(/^\/providers\/([a-z0-9][a-z0-9_-]{0,31})\/mcp$/); diff --git a/src/manager/session-hooks.mjs b/src/manager/session-hooks.mjs index c497555..8d8ea56 100644 --- a/src/manager/session-hooks.mjs +++ b/src/manager/session-hooks.mjs @@ -6,12 +6,20 @@ import { spawn } from 'node:child_process'; import { buildSpawnSpec, endWithProcess, killGroup, runSpec, killWindowsTree } from './command-resolver.mjs'; import { compareVersions, probeVersion } from './versions.mjs'; import { commandMtime } from './install-channels.mjs'; +import { hermesHomeKey } from './hermes.mjs'; export const REPORT_COMMAND = 'agent-guild-report --hook'; export const PLUGIN_NAME = 'agent-guild'; const MANIFEST_DESCRIPTION = 'Reports to the Agent Guild session it runs in.'; const PROBE_TIMEOUT_MS = 20000; const PROBE_RETRY_MS = 5 * 60 * 1000; +const HERMES_PLUGIN = 'agent-guild'; +const HERMES_TOKEN_FILE_VAR = 'AGENT_GUILD_HERMES_TOKEN_FILE'; +const HERMES_PLUGIN_SOURCE = 'oddessentials/agent-guild/plugins/hermes/agent-guild'; +const HERMES_PLUGIN_COMMIT = /^[0-9a-f]{40}$/; +const HERMES_RUN_TIMEOUT_MS = 120000; +/** Names the plugin commit a build without a released one installs, for development. */ +export const HERMES_PLUGIN_COMMIT_VAR = 'AGENT_GUILD_HERMES_PLUGIN_COMMIT'; const handler = (extra = {}) => ({ type: 'command', command: REPORT_COMMAND, ...extra }); @@ -330,13 +338,21 @@ export function antigravityInstalled(pluginDir, bundle) { const pending = (tool, when) => ({ state: 'pending', reason: `Agent Guild added its reporting hooks to this ${tool} session. They report once ${tool} ${when}.` }); export class SessionHooks { - constructor({ registry, dir, version, probeTimeoutMs = PROBE_TIMEOUT_MS, probeRetryMs = PROBE_RETRY_MS }) { + constructor({ registry, dir, version, probeTimeoutMs = PROBE_TIMEOUT_MS, probeRetryMs = PROBE_RETRY_MS, hermesTokens = null, hermesPluginCommit = null, getApiUrl = () => null }) { this.registry = registry; + this.hermesTokens = hermesTokens; + /** The commit of oddessentials/agent-guild whose Hermes plugin this Agent Guild installs, or null: then it installs none. */ + this.hermesPluginCommit = HERMES_PLUGIN_COMMIT.test(hermesPluginCommit ?? '') ? hermesPluginCommit : null; + this.getApiUrl = getApiUrl; + this.hermesPlugins = new Map(); + /** How many reporting changes each Hermes home has had: a read that a change overtook is dropped. */ + this.hermesChanges = new Map(); this.probeTimeoutMs = probeTimeoutMs; this.probeRetryMs = probeRetryMs; this.dir = dir; this.bundles = null; this.probes = new Map(); + this.hermesRuns = new Set(); this.ending = new AbortController(); if (dir) { try { @@ -350,12 +366,13 @@ export class SessionHooks { /** Ends the probes still running, and resolves once they have and their folders are gone. */ close() { this.ending.abort(); - return Promise.allSettled([...this.probes.values()].map((entry) => entry.promise)); + return Promise.allSettled([...[...this.probes.values()].map((entry) => entry.promise), ...this.hermesRuns]); } warm() { for (const provider of this.registry.providers) { if (['claude', 'codex', 'docker'].includes(provider.reporting)) this._probe(provider).catch(() => {}); + if (provider.reporting === 'hermes') this._readHermes(provider).catch(() => {}); } } @@ -429,6 +446,7 @@ export class SessionHooks { if (!mode) return { args: [], reporting: null }; const tool = provider.tool; const off = { args: [], reporting: { state: 'setup_required', reason: `Agent reporting is off for ${tool}. Turn it on from the ${tool} card; it applies to new sessions.` } }; + if (mode === 'hermes') return { args: [], reporting: null }; if (mode === 'grok') { const dir = this._grokDir(provider, account); const state = grokHooksState(dir, this._grokFiles()); @@ -497,6 +515,9 @@ export class SessionHooks { const state = this._dropinState(provider); return state === 'other' ? null : Boolean(state); } + if (provider.reporting === 'hermes') { + return this._hermesReporting(provider).some((account) => account.on || account.held); + } if (provider.reporting === 'grok') { const states = this._grokDirs(provider).map((dir) => grokHooksState(dir, this._grokFiles())); return states.includes('other') ? null : states.every(Boolean); @@ -511,6 +532,30 @@ export class SessionHooks { const other = this._grokDirs(provider).find((dir) => grokHooksState(dir, this._grokFiles()) === 'other'); return other ? `Agent reporting is off: ${path.join(other, GROK_HOOKS_FILE)} was not written by Agent Guild. Remove it to turn agent reporting on here.` : null; } + if (provider.reporting === 'hermes') { + const accounts = this._hermesReporting(provider); + const on = accounts.filter((account) => account.on); + if (this.hermesTokens?.unreadable) { + return `Agent reporting is off: Agent Guild could not read its saved report tokens in ${this.hermesTokens.recordFile} (${this.hermesTokens.unreadable}). Turn it on to make new ones.`; + } + const held = accounts.filter((account) => account.held); + if (held.length) { + return `Agent reporting is not fully off: the report token of ${held.map((a) => a.label).join(', ')} is still accepted, because Agent Guild could not revoke it. Turn it off to try again.`; + } + if (on.length && on.length < accounts.length) { + return `Agent reporting is on for ${on.map((a) => a.label).join(', ')}, not for ${accounts.filter((a) => !a.on).map((a) => a.label).join(', ')}. Turn it off, then on, to try again.`; + } + const stale = this.hermesTokens && accounts.some((account) => this.hermesTokens.stale.has(account.home)); + if (stale) return `Agent reporting is off: Agent Guild could not give ${provider.tool} its new address. Turn it on to try again.`; + const outdated = on.filter((account) => account.outdated); + if (outdated.length) { + return `Agent reporting is on. The Agent Guild plugin of ${outdated.map((a) => a.label).join(', ')} is not the one this Agent Guild installs; it keeps reporting until you update it.`; + } + if (!this.hermesPluginCommit && !on.length && accounts.some((account) => account.plugin === 'missing')) { + return `Agent reporting is off: this build of Agent Guild names no commit of its Hermes plugin to install. Set ${HERMES_PLUGIN_COMMIT_VAR} to one to install that plugin.`; + } + return null; + } if (provider.reporting !== 'docker') return null; if (!this.probes.get(provider.id)?.version) return null; if (!this._dockerDropins(provider)) return `Model reporting needs ${provider.tool} ${DOCKER_DROPIN_VERSION} or later.`; @@ -615,7 +660,217 @@ export class SessionHooks { return antigravityPluginEnabled(antigravityConfigFile({ ...this.registry.env, ...provider.env }, this.registry.platform)); } + /** Runs a Hermes command; every failure, Hermes not found included, arrives as a rejection, so each caller's handling sees it. */ + async _hermesRun(provider, account, args, what) { + const resolved = this.registry.resolve(provider); + if (!resolved) throw Object.assign(new Error(`${provider.tool} is not installed`), { status: 409, code: 'provider_unavailable' }); + const env = { ...this.registry.env, ...provider.env, ...account.env }; + const run = runSpec(buildSpawnSpec(resolved, [...(account.args ?? []), ...args], env, this.registry.platform), { env, timeoutMs: HERMES_RUN_TIMEOUT_MS, cwd: this.dir, killTree: true, signal: this.ending.signal }); + this.hermesRuns.add(run); + return run.finally(() => this.hermesRuns.delete(run)).catch((err) => { + const detail = `${err.stderr || ''}\n${err.stdout || ''}`.trim().split('\n').filter(Boolean).pop() + || (err.killed ? 'it did not finish in time' : typeof err.code === 'number' ? `it exited with code ${err.code}` : 'it could not be run'); + throw Object.assign(new Error(`${provider.tool} could not ${what}: ${detail}`), { status: 502, code: 'reporting_setup_failed', detail }); + }); + } + + /** Refuses an account whose home Hermes itself resolves elsewhere: its reports would reach another card. */ + async _hermesHomeChecked(provider, account) { + const { stdout } = await this._hermesRun(provider, account, ['config', 'env-path'], 'say where it keeps its settings'); + const file = stdout.trim().split('\n').pop()?.trim() || ''; + if (!file || hermesHomeKey(path.dirname(file), this.registry.platform) !== account.home) { + throw Object.assign(new Error(`${provider.tool} keeps the "${account.label}" profile in ${file ? path.dirname(file) : 'a folder it did not name'}, not in ${account.dir}. Reload the cards.`), { status: 409, code: 'reporting_setup_failed' }); + } + } + + /** The Agent Guild plugin in each account's Hermes home: its status, and whether it is the commit this Agent Guild installs. */ + async _readHermes(provider) { + let changed = false; + for (const account of this.registry.accountsFor(provider)) { + const seen = this.hermesChanges.get(account.home) ?? 0; + const plugin = await this._hermesPlugin(provider, account); + if ((this.hermesChanges.get(account.home) ?? 0) !== seen) continue; + if (JSON.stringify(this.hermesPlugins.get(account.home)) !== JSON.stringify(plugin)) changed = true; + this.hermesPlugins.set(account.home, plugin); + } + // Every card of a changed home is described again, not only this one. + if (changed) this.registry.emit?.('updated'); + } + + async _hermesPlugin(provider, account) { + const { stdout } = await this._hermesRun(provider, account, ['plugins', 'list', '--json', '--user'], 'list its plugins'); + let list = []; + try { list = JSON.parse(stdout); } catch { /* read as none */ } + const entry = Array.isArray(list) ? list.find((p) => p?.name === HERMES_PLUGIN) : null; + if (!entry) return { status: 'missing', current: false, source: null }; + // Hermes names a plugin installed with --ref by the commit its install record keeps. + const current = Boolean(this.hermesPluginCommit) && entry.source === `git pinned@${this.hermesPluginCommit.slice(0, 8)}`; + return { status: entry.status === 'enabled' ? 'enabled' : 'disabled', current, source: entry.source ?? null }; + } + + /** Installs the plugin at this Agent Guild's commit, over the one there when `force`, and checks Hermes recorded that commit. */ + async _hermesInstall(provider, account, force) { + const commit = this.hermesPluginCommit; + await this._hermesRun(provider, account, ['plugins', 'install', HERMES_PLUGIN_SOURCE, '--ref', commit, ...(force ? ['--force'] : []), '--enable'], + `${force ? 'update' : 'install'} the Agent Guild plugin`); + const plugin = await this._hermesPlugin(provider, account); + if (plugin.status !== 'enabled' || !plugin.current) { + throw Object.assign(new Error(`${provider.tool} reported success, but it does not record the Agent Guild plugin at commit ${commit.slice(0, 12)} as on`), { status: 502, code: 'reporting_setup_failed' }); + } + } + + /** Runs one home's reporting change in its setup turn, and makes any read that started before it obsolete. */ + _hermesChange(account, work) { + return this.hermesTokens.setup(account.home, async () => { + try { + return await work(); + } finally { + this.hermesChanges.set(account.home, (this.hermesChanges.get(account.home) ?? 0) + 1); + } + }); + } + + _noPluginCommit() { + return Object.assign(new Error(`This build of Agent Guild names no commit of its Hermes plugin to install. Set ${HERMES_PLUGIN_COMMIT_VAR} to one to install that plugin`), { status: 409, code: 'reporting_unavailable' }); + } + + /** + * Updates the plugin of every profile that reports with another commit to this Agent Guild's, one profile at a time. Hermes + * replaces a plugin only once the new one is installed, so a profile whose update fails keeps reporting with the plugin it had. + */ + async update(provider) { + if (provider.reporting !== 'hermes') throw Object.assign(new Error(`${provider.tool} has no plugin to update`), { status: 400, code: 'not_applicable' }); + if (!this.hermesTokens) throw Object.assign(new Error('Agent Guild keeps no Hermes report tokens here'), { status: 409, code: 'reporting_unavailable' }); + if (!this.hermesPluginCommit) throw this._noPluginCommit(); + await this._readHermes(provider); + for (const account of this._hermesReporting(provider).filter((a) => a.on && a.outdated)) { + let had = null; + try { + await this._hermesChange(account, async () => { + const plugin = await this._hermesPlugin(provider, account); + had = plugin.source; + if (plugin.status === 'enabled' && !plugin.current) await this._hermesInstall(provider, account, true); + }); + } catch (err) { + // What Hermes has now is said only as a fresh query answers it; a cached state is never taken for it. + let unread = null; + const now = await this._hermesPlugin(provider, account).catch((cause) => { unread = cause; return null; }); + await this._readHermes(provider).catch(() => {}); + const after = !now + ? `Agent Guild could not check which plugin Hermes has now, so whether it still has the one it had is unknown: ${unread.message.replace(/\.$/, '')}.` + : now.status === 'missing' ? 'Hermes no longer has the Agent Guild plugin installed.' + : `Hermes ${now.source === had ? 'still has the plugin it had' : 'now has another plugin'}, ${now.source ?? 'from no recorded source'}, and it is ${now.status === 'enabled' ? 'on' : 'off'}.`; + err.message = `The ${account.label} profile was not updated: ${err.message.replace(/\.$/, '')}. ${after}`; + throw err; + } + } + await this._readHermes(provider); + return this.enabled(provider); + } + + /** Whether a profile that reports has a plugin of another commit, so the card offers to update it. */ + updatable(provider) { + return provider.reporting === 'hermes' && this._hermesReporting(provider).some((account) => account.on && account.outdated); + } + + async _setHermes(provider, enabled) { + if (!this.hermesTokens) throw Object.assign(new Error('Agent Guild keeps no Hermes report tokens here'), { status: 409, code: 'reporting_unavailable' }); + // Turning off revokes every token even when Hermes cannot say what it has; each profile reports that itself. + await (enabled ? this._readHermes(provider) : this._readHermes(provider).catch(() => {})); + const failed = []; + for (const account of this.registry.accountsFor(provider)) { + try { + await this._hermesChange(account, () => this._setHermesAccount(provider, account, enabled)); + } catch (err) { + failed.push([account, err]); + // Turning on stops at the first profile that fails; turning off still turns off every other one. + if (enabled) break; + } + } + if (failed.length) { + await this._readHermes(provider).catch(() => {}); + const state = this._hermesReporting(provider); + const on = state.filter((a) => a.on).map((a) => a.label); + const held = state.filter((a) => a.held).map((a) => a.label); + const err = failed[0][1]; + err.message = [ + ...failed.map(([account, cause]) => `The ${account.label} profile failed: ${cause.message.replace(/\.$/, '')}.`), + `Reporting is on for ${on.length ? on.join(', ') : 'no profile'}.`, + ...(held.length ? [`The report token of ${held.join(', ')} is still accepted; turn it off to try again.`] : []), + ].join(' '); + throw err; + } + await this._readHermes(provider); + const now = enabled ? this._hermesReporting(provider).every((account) => account.on) : this.enabled(provider); + if (now !== enabled) { + throw Object.assign(new Error(`${provider.tool} reported success, but the Agent Guild plugin is ${now ? 'still on' : 'not on'}`), { status: 502, code: 'reporting_setup_failed' }); + } + return now; + } + + _hermesReporting(provider) { + return this.registry.accountsFor(provider).map((account) => { + const plugin = this.hermesPlugins.get(account.home); + const accepted = Boolean(this.hermesTokens?.get(account.home)); + return { + ...account, + plugin: plugin?.status ?? null, + // Reporting and the plugin's commit are apart: a plugin of another commit that is on still reports. + on: Boolean(plugin && accepted && plugin.status === 'enabled' && !this.hermesTokens.stale.has(account.home)), + // A token still accepted for a plugin that is not on: a revocation that failed, so reporting is not off. + held: Boolean(plugin && accepted && plugin.status !== 'enabled'), + outdated: Boolean(this.hermesPluginCommit && plugin && plugin.status !== 'missing' && !plugin.current), + }; + }); + } + + /** + * Turns one account's reporting on or off, in its home's setup turn: what it finds there is what no other request changes + * meanwhile. A profile that fails to turn on keeps no accepted token; one that already reported keeps reporting. + */ + async _setHermesAccount(provider, account, enabled) { + const failures = []; + // A teardown goes on without knowing the plugin's state: it asks Hermes to turn it off all the same. + const plugin = enabled ? await this._hermesPlugin(provider, account) : await this._hermesPlugin(provider, account).catch((err) => { failures.push(err); return null; }); + const state = plugin?.status ?? null; + if (enabled) { + const wasOn = state === 'enabled' && Boolean(this.hermesTokens.get(account.home)) && !this.hermesTokens.stale.has(account.home); + // A profile that turns on gets this Agent Guild's plugin; one that already reports keeps its own until updated. + const install = state === 'missing' || (!wasOn && Boolean(this.hermesPluginCommit) && !plugin.current); + if (install && !this.hermesPluginCommit) throw this._noPluginCommit(); + try { + await this._hermesHomeChecked(provider, account); + await this.hermesTokens.ensure(account.home, this.getApiUrl()).catch((err) => { + throw Object.assign(new Error(`Agent Guild could not save its reporting settings (${err.code || err.message})`), { status: 500, code: 'reporting_setup_failed' }); + }); + await this._hermesRun(provider, account, ['config', 'set', HERMES_TOKEN_FILE_VAR, this.hermesTokens.file(account.home)], "save Agent Guild's reporting settings"); + if (install) await this._hermesInstall(provider, account, state !== 'missing'); + else if (state === 'disabled') await this._hermesRun(provider, account, ['plugins', 'enable', HERMES_PLUGIN], 'turn on the Agent Guild plugin'); + } catch (err) { + if (!wasOn) await this.hermesTokens.remove(account.home).catch((cause) => console.error(`[hermes] could not revoke the report token of ${account.home}: ${cause.code || cause.message}`)); + throw err; + } + return; + } + if (state === 'enabled' || !plugin) { + await this._hermesRun(provider, account, ['plugins', 'disable', HERMES_PLUGIN], 'turn off the Agent Guild plugin').catch((err) => failures.push(err)); + } + await this._hermesRun(provider, account, ['config', 'unset', HERMES_TOKEN_FILE_VAR], "remove Agent Guild's reporting settings").catch((err) => { + // Hermes's answer for a setting that is not there: removed by hand, or by a teardown before. + if (err.detail !== `Config key not set: ${HERMES_TOKEN_FILE_VAR}`) failures.push(err); + }); + await this.hermesTokens.remove(account.home).catch((err) => { + failures.push(Object.assign(new Error(`Agent Guild could not revoke its report token (${err.code || err.message})`), { status: 500, code: 'reporting_setup_failed' })); + }); + if (failures.length) { + const err = failures[0]; + err.message = [...new Set(failures.map((cause) => cause.message.replace(/\.$/, '')))].join('; '); + throw err; + } + } + async setEnabled(provider, enabled) { + if (provider.reporting === 'hermes') return this._setHermes(provider, enabled); if (provider.reporting === 'docker') return this._setDockerModel(provider, enabled); if (provider.reporting === 'grok') return this._setGrok(provider, enabled); if (provider.reporting !== 'antigravity') throw Object.assign(new Error(`${provider.tool} needs no setup for agent reporting`), { status: 400, code: 'not_applicable' }); diff --git a/src/manager/session-manager.mjs b/src/manager/session-manager.mjs index 4432874..376ed9e 100644 --- a/src/manager/session-manager.mjs +++ b/src/manager/session-manager.mjs @@ -163,7 +163,7 @@ export class SessionManager extends EventEmitter { const extraEnv = hooks.env ? { ...runShell?.env, ...hooks.env } : runShell?.env; const options = { provider, cwd: workDir, cols, rows, name: given ?? defaultName, defaultName, named: given !== null, resume: resumeId, account: signIn, reporting: hooks.reporting, extraEnv }; if (runShell?.multiplexer) return this._withMultiplexerStart(provider.id, runShell.id, () => this._startMultiplexer(options, runShell, args || [])); - const spawnSpec = this.registry.spawnSpec(provider, args || [], resumeId, hooks.args, runShell); + const spawnSpec = this.registry.spawnSpec(provider, [...(signIn.args ?? []), ...(args || [])], resumeId, hooks.args, runShell); const session = this._spawn({ ...options, spawnSpec }); // A session id Agent Guild chose is known before the tool reports it, so the card can resume it at once. if (hooks.toolSessionId) session.reportToolSession({ toolSessionId: hooks.toolSessionId }, 'launch'); diff --git a/tests/browser/hermes-reporting.mjs b/tests/browser/hermes-reporting.mjs new file mode 100644 index 0000000..1683dfe --- /dev/null +++ b/tests/browser/hermes-reporting.mjs @@ -0,0 +1,150 @@ +// Headless Chrome: the Hermes Agent card's reporting switch, partial failure, and live activity kept apart from terminals. +// Run: node tests/browser/hermes-reporting.mjs (CHROME_PATH may name Chrome or Edge). +import assert from 'node:assert/strict'; +import { until, withPage } from './chrome.mjs'; + +const instrumentation = ``; + +const FAILED = 'The work profile failed: Hermes Agent could not install the Agent Guild plugin: it exited with code 1. Reporting is on for Default.'; + +const checks = await withPage({ name: 'hermes-reporting', instrumentation }, async ({ origin, evaluate, pass, errors, navigate }) => { + const card = '.provider[data-id=hermes]'; + const read = (selector) => evaluate(`document.querySelector(${JSON.stringify(selector)})?.textContent ?? null`); + const shown = (selector) => evaluate(`(() => { const el = document.querySelector(${JSON.stringify(selector)}); return Boolean(el) && !el.hidden && el.getClientRects().length > 0; })()`); + + await navigate(origin); + await until('the Hermes Agent card', () => shown(`${card} .reporting-row`)); + assert.equal(await read(`${card} .reporting-text`), 'Agent reporting off'); + assert.equal(await evaluate(`document.querySelector('${card} .reporting-open')`), null); + assert.equal(await shown(`${card} .reporting-live`), false); + assert.doesNotMatch(await evaluate(`document.querySelector('${card} .reporting-toggle').title`), /token/i); + pass('reporting off: no Open action, no live list, no token wording'); + + await evaluate(`document.querySelector('${card} .reporting-toggle').click()`); + await until('the switch request held', () => evaluate('Boolean(window.__hermes.held)')); + assert.equal(await read(`${card} .reporting-text`), 'Turning on…'); + assert.equal(await evaluate(`document.querySelector('${card} .reporting-toggle').disabled`), true); + pass('turning on shows progress and locks the switch'); + + await evaluate(`(async () => { + window.__hermes.enabled = true; + window.__hermes.note = 'Agent reporting is on for Default, not for work. Turn it off, then on, to try again.'; + window.__hermes.live.default = { working: 1, waiting: 0, agents: [{ id: 'hermes:s1', name: 'telegram', status: 'working', detail: '' }], shells: [] }; + await window.__pushProviders(); + const answer = window.__hermes.held; window.__hermes.held = null; + answer({ status: 502, body: { error: { message: ${JSON.stringify(FAILED)}, code: 'reporting_setup_failed' } } }); + })()`); + await until('the failure toast', async () => (await read('#toast')) === FAILED); + await until('the switch released', () => evaluate(`!document.querySelector('${card} .reporting-toggle').disabled`)); + assert.equal(await read(`${card} .reporting-text`), 'Agent reporting is on for Default, not for work. Turn it off, then on, to try again.'); + assert.equal(await shown(`${card} .reporting-toggle`), true, 'the switch is shown'); + assert.equal(await read(`${card} .reporting-toggle`), 'Turn off', 'and offers Turn off'); + assert.equal(await shown(`${card} .reporting-live`), true); + assert.equal(await read(`${card} .reporting-live-head`), 'Running in Hermes Agent, not in Agent Guild terminals. Shows sessions active since reporting started.'); + assert.deepEqual(await evaluate(`[...document.querySelectorAll('${card} .reporting-live-profile')].map((el) => [el.querySelector('.reporting-live-label').textContent, [...el.querySelectorAll('.agent')].map((a) => a.title)])`), + [['Default profile: 1 working, 0 waiting', ['telegram (working)']]]); + pass('a failed profile names itself, the card offers Turn off, and the profile that reports keeps its live list'); + + await evaluate(`(async () => { + const { session: base } = await (await fetch('/api/v1/sessions', { method: 'POST', body: JSON.stringify({ providerId: 'xai', cwd: 'demo' }) })).json(); + window.__push({ type: 'session.created', session: { ...base, id: 'he12e501', name: 'Hermes terminal', provider: { ...base.provider, id: 'hermes', tool: 'Hermes Agent' }, agents: [], shells: [], reporting: null, reply: null } }); + })()`); + await until('the Hermes terminal card', () => shown('.session-card[data-id=he12e501]')); + assert.equal(await evaluate(`document.querySelectorAll('.session-card[data-id=he12e501] .agents .agent').length`), 0); + assert.equal(await evaluate(`document.querySelectorAll('${card} .reporting-live .agent').length`), 1); + pass('Hermes activity stays on the tool card, never on a Hermes terminal'); + + await evaluate(`(async () => { + window.__hermes.enabled = true; + window.__hermes.note = null; + window.__hermes.live.work = { working: 0, waiting: 1, agents: [{ id: 'hermes:c1', name: 'Map the code', status: 'waiting', detail: 'Map the code' }], shells: [] }; + await window.__pushProviders(); + })()`); + await until('both profiles listed', () => evaluate(`document.querySelectorAll('${card} .reporting-live-profile').length === 2`)); + assert.equal(await read(`${card} .reporting-text`), 'Agent reporting on'); + assert.deepEqual(await evaluate(`[...document.querySelectorAll('${card} .reporting-live-label:not(.reporting-live-head)')].map((el) => el.textContent)`), + ['Default profile: 1 working, 0 waiting', 'work profile: 0 working, 1 waiting']); + pass('with reporting on, every reporting profile is listed by name'); + + assert.equal(await shown(`${card} .reporting-update`), false, 'nothing to update'); + const OUTDATED = 'Agent reporting is on. The Agent Guild plugin of Default, work is not the one this Agent Guild installs; it keeps reporting until you update it.'; + await evaluate(`(async () => { window.__hermes.update = true; window.__hermes.note = ${JSON.stringify(OUTDATED)}; await window.__pushProviders(); })()`); + await until('Update plugin offered', () => shown(`${card} .reporting-update`)); + assert.equal(await read(`${card} .reporting-text`), OUTDATED); + assert.equal(await read(`${card} .reporting-toggle`), 'Turn off', 'the outdated plugin still reports'); + assert.equal(await evaluate(`document.querySelectorAll('${card} .reporting-live-profile').length`), 2, 'and its activity still shows'); + await evaluate(`document.querySelector('${card} .reporting-update').click()`); + await until('the update request held', () => evaluate('Boolean(window.__hermes.held)')); + assert.deepEqual(await evaluate('window.__hermes.asked'), { update: true }); + assert.equal(await read(`${card} .reporting-text`), 'Updating the Agent Guild plugin…'); + assert.equal(await evaluate(`document.querySelector('${card} .reporting-toggle').disabled`), true); + await evaluate(`(async () => { + window.__hermes.update = false; + window.__hermes.note = null; + await window.__pushProviders(); + const answer = window.__hermes.held; window.__hermes.held = null; + answer({ status: 200, body: { provider: null } }); + })()`); + await until('the update toast', async () => (await read('#toast')) === 'The Agent Guild plugin is updated for Hermes Agent.'); + await until('Update plugin gone', async () => !(await shown(`${card} .reporting-update`))); + assert.equal(await read(`${card} .reporting-text`), 'Agent reporting on'); + pass('a plugin of another version keeps reporting and offers Update plugin, which updates it'); + + assert.doesNotMatch(await evaluate(`document.querySelector('${card} .reporting-toggle').title`), /token/i); + await evaluate(`document.querySelector('${card} .reporting-toggle').click()`); + await until('the off request held', () => evaluate('Boolean(window.__hermes.held)')); + assert.equal(await read(`${card} .reporting-text`), 'Turning off…'); + await evaluate(`(async () => { + window.__hermes.enabled = false; + window.__hermes.live = { default: null, work: null }; + await window.__pushProviders(); + const answer = window.__hermes.held; window.__hermes.held = null; + answer({ status: 200, body: { provider: null } }); + })()`); + await until('the off toast', async () => (await read('#toast')) === 'Agent reporting is off for Hermes Agent.'); + await until('the live list gone', async () => !(await shown(`${card} .reporting-live`))); + assert.equal(await read(`${card} .reporting-text`), 'Agent reporting off'); + assert.deepEqual(errors, []); + pass('turning off confirms it and clears the live list'); +}); + +console.log(`ok ${checks} checks`); diff --git a/tests/browser/yard.mjs b/tests/browser/yard.mjs index 418e398..7c18b8f 100644 --- a/tests/browser/yard.mjs +++ b/tests/browser/yard.mjs @@ -658,7 +658,7 @@ test('Cards and Yard use the same actions, accounts, dialogs and terminal',optio await b.click(provider('google'));await b.click(inspector+' .reporting-toggle'); await until(()=>f.calls.some(c=>c[0]==='reporting')); assert.deepEqual(f.calls.find(c=>c[0]==='reporting'),['reporting','google',true]); - await b.wait(`document.querySelector('${inspector} .reporting-text').textContent.includes('on')`); + await b.wait(`document.querySelector('${inspector} .reporting-text').textContent==='Agent reporting on'`); f.providers[3].available=false;f.registry.emit('updated'); await b.click(provider('xai'));await b.wait(`document.querySelector('${inspector} .install')?.hidden===false`); await b.click(inspector+' .install');await terminalReady(b);await closeTerminal(b); diff --git a/tests/fixtures/fake-coding-tool.mjs b/tests/fixtures/fake-coding-tool.mjs index 0da64c4..fc9d10d 100644 --- a/tests/fixtures/fake-coding-tool.mjs +++ b/tests/fixtures/fake-coding-tool.mjs @@ -59,7 +59,7 @@ const [tool, ...argv] = process.argv.slice(2); const win = process.platform === 'win32'; const out = (text) => process.stdout.write(`${text}\r\n`); const readJson = (file) => { try { return JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return null; } }; -if ((argv[0] === 'mcp' && tool !== 'docker') || (argv[0] === 'inspect' && tool === 'grok')) (await import('./fake-mcp.mjs')).runMcp(tool, argv); +if ((argv[0] === 'mcp' && tool !== 'docker') || (argv[0] === 'inspect' && tool === 'grok') || (argv[0] === 'config' && tool === 'hermes')) (await import('./fake-mcp.mjs')).runMcp(tool, argv); // The docker fixture's agent plugin: absent with FAKE_DOCKER_NO_PLUGIN=1, or when FAKE_DOCKER_PLUGIN_PATH names a // file that is not there (so an install command that writes it, `docker-install`, makes the plugin appear). diff --git a/tests/fixtures/fake-hermes.mjs b/tests/fixtures/fake-hermes.mjs new file mode 100644 index 0000000..1a3a771 --- /dev/null +++ b/tests/fixtures/fake-hermes.mjs @@ -0,0 +1,64 @@ +// `hermes -p ...` for the commands Agent Guild's reporting setup runs, answered as Hermes Agent 0.23.1 answers them. +// The profile's home follows $HERMES_HOME as `hermes -p` does. Calls are logged to $FAKE_HERMES_LOG, one argv per line. +// $FAKE_HERMES_FAIL names a command, as in "plugins disable", that exits 1 with no output, for the profile +// $FAKE_HERMES_FAIL_PROFILE only when that is set; $FAKE_HERMES_HANG names one +// that writes its process id to $FAKE_HERMES_PID and never ends. Each call's profile and the home it resolves are logged to +// $FAKE_HERMES_HOMES, one JSON object per line. `config get` answers as for a key that is not set. `plugins install` keeps +// the plugin's source as Hermes's install record names it, `git pinned@` with --ref, and refuses an installed plugin +// without --force; enable and disable keep that source. $FAKE_HERMES_UNPINNED makes install ignore --ref. `config set` keeps +// the key, and `config unset` of a key that is not set fails with Hermes's "Config key not set: ". + +import fs from 'node:fs'; +import path from 'node:path'; + +const argv = process.argv.slice(2); +if (process.env.FAKE_HERMES_LOG) fs.appendFileSync(process.env.FAKE_HERMES_LOG, `${JSON.stringify(argv)}\n`); +const [profile, rest] = argv[0] === '-p' ? [argv[1], argv.slice(2)] : ['default', argv]; +const set = path.resolve(process.env.HERMES_HOME); +const root = path.basename(path.dirname(set)) === 'profiles' ? path.dirname(path.dirname(set)) : set; +const home = profile === 'default' ? root : path.join(root, 'profiles', profile); +const stateFile = path.join(home, 'fake-plugins.json'); +const configFile = path.join(home, 'fake-config.json'); +const command = rest.slice(0, 2).join(' '); +if (process.env.FAKE_HERMES_HOMES) fs.appendFileSync(process.env.FAKE_HERMES_HOMES, `${JSON.stringify({ profile, home, command })}\n`); +const installed = () => { try { return JSON.parse(fs.readFileSync(stateFile, 'utf8'))[0] ?? null; } catch { return null; } }; +const plugin = (status, source = installed()?.source ?? null) => fs.writeFileSync(stateFile, JSON.stringify([{ name: 'agent-guild', status, source }])); + +if (process.env.FAKE_HERMES_FAIL === command && (process.env.FAKE_HERMES_FAIL_PROFILE ?? profile) === profile) process.exit(1); +if (process.env.FAKE_HERMES_HANG === command) { + fs.writeFileSync(process.env.FAKE_HERMES_PID, String(process.pid)); + await new Promise(() => setInterval(() => {}, 1000)); +} +if (command === 'config env-path') { + process.stdout.write(`${path.join(home, '.env')}\n`); +} else if (command === 'config get') { + process.stdout.write(`Config key not set: ${rest[2]}\n`); + process.exit(1); +} else if (command === 'plugins list') { + let list = '[]'; + try { list = fs.readFileSync(stateFile, 'utf8'); } catch { /* none installed */ } + process.stdout.write(`${list}\n`); +} else if (command === 'plugins install') { + if (installed() && !rest.includes('--force')) { + process.stderr.write("Error: Plugin 'agent-guild' is already installed. Use --force to reinstall it.\n"); + process.exit(1); + } + const ref = rest.includes('--ref') && !process.env.FAKE_HERMES_UNPINNED ? rest[rest.indexOf('--ref') + 1] : null; + plugin('enabled', ref ? `git pinned@${ref.slice(0, 8)}` : rest[2]); +} else if (command === 'plugins enable') { + plugin('enabled'); +} else if (command === 'plugins disable') { + plugin('disabled'); +} else if (command === 'config set' || command === 'config unset') { + const keys = (() => { try { return JSON.parse(fs.readFileSync(configFile, 'utf8')); } catch { return {}; } })(); + if (command === 'config unset' && !(rest[2] in keys)) { + process.stderr.write(`Config key not set: ${rest[2]}\n`); + process.exit(1); + } + if (command === 'config set') keys[rest[2]] = rest[3]; + else delete keys[rest[2]]; + fs.writeFileSync(configFile, JSON.stringify(keys)); +} else { + process.stderr.write(`hermes: unexpected arguments: ${argv.join(' ')}\n`); + process.exit(2); +} diff --git a/tests/fixtures/fake-mcp.mjs b/tests/fixtures/fake-mcp.mjs index 69cba3f..8ba44cd 100644 --- a/tests/fixtures/fake-mcp.mjs +++ b/tests/fixtures/fake-mcp.mjs @@ -1,4 +1,4 @@ -// ` mcp ...` for claude, codex, agy and grok, stored as each real tool stores it. Calls are logged to $FAKE_MCP_LOG. +// ` mcp ...` for claude, codex, agy and grok, and `hermes config get|set|unset mcp_servers.`, stored as each real tool stores it. Calls are logged to $FAKE_MCP_LOG. // fake-mcp.json { "fail": true } fails every call, { "garbage": true } prints a list that is not JSON; a home named *broken* refuses writes. // As the real tools do, codex fails when $CODEX_HOME does not exist, and grok creates its home on any call. Grok also // loads ~/.claude.json's servers, as `inspect` and `mcp doctor` show and `mcp list` does not, and only says on stderr that its @@ -16,12 +16,14 @@ const homes = { codex: () => process.env.CODEX_HOME || path.join(home(), '.codex'), agy: () => home(), grok: () => process.env.GROK_HOME || path.join(home(), '.grok'), + hermes: () => process.env.HERMES_HOME || path.join(home(), '.hermes'), }; const files = { claude: (dir) => path.join(dir, '.claude.json'), codex: (dir) => path.join(dir, 'fake-mcp.json'), agy: (dir) => path.join(dir, '.gemini', 'config', 'mcp_config.json'), grok: (dir) => path.join(dir, 'fake-mcp.json'), + hermes: (dir) => path.join(dir, 'fake-mcp.json'), }; function load(file) { @@ -57,6 +59,34 @@ export function runMcp(tool, argv) { store(file, tool === 'claude' || tool === 'agy' ? { ...config, mcpServers: servers } : { servers }); if (tool === 'grok') fs.writeFileSync(offFile, stringifyToml({ ...grokConfig, disabled_mcp_servers: disabled })); }; + if (tool === 'hermes') { + const [, verb, key, value] = argv; + const name = key?.startsWith('mcp_servers.') ? key.slice('mcp_servers.'.length).split('.')[0] : null; + const field = name ? key.slice('mcp_servers.'.length + name.length + 1) || null : null; + const notSet = () => { process.stdout.write(`Config key not set: ${key}\n`); process.exit(1); }; + if (argv[0] !== 'config' || !name) fail(`hermes: unexpected arguments: ${argv.join(' ')}`, 2); + if (verb === 'get') { + if (argv.slice(3).join(' ') !== '--json') fail(`hermes: unexpected arguments: ${argv.join(' ')}`, 2); + const found = field ? servers[name]?.[field] : servers[name]; + if (found === undefined) notSet(); + process.stdout.write(`${JSON.stringify(found)}\n`); + process.exit(0); + } + if (path.basename(dir).includes('broken')) fail('cannot write the settings'); + if (verb === 'set' && argv.length === 4) { + if (field) servers[name] = { ...servers[name], [field]: value === 'true' ? true : value === 'false' ? false : value }; + else servers[name] = JSON.parse(value); + save(); + process.exit(0); + } + if (verb === 'unset' && argv.length === 3 && !field) { + if (!servers[name]) notSet(); + delete servers[name]; + save(); + process.exit(0); + } + fail(`hermes: unexpected arguments: ${argv.join(' ')}`, 2); + } const claudeFile = path.join(home(), '.claude.json'); const claudeServers = () => load(claudeFile).mcpServers ?? {}; if (tool === 'grok' && argv[0] === 'inspect') { diff --git a/tests/hermes.test.mjs b/tests/hermes.test.mjs new file mode 100644 index 0000000..86ecc10 --- /dev/null +++ b/tests/hermes.test.mjs @@ -0,0 +1,1433 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import http from 'node:http'; +import os from 'node:os'; +import path from 'node:path'; +import readline from 'node:readline'; +import { spawn } from 'node:child_process'; +import { EventEmitter } from 'node:events'; +import { fileURLToPath } from 'node:url'; +import { HermesLive, HermesTokens, HERMES_EXPIRY_MS, hermesHomeKey, hermesRoot } from '../src/manager/hermes.mjs'; +import { SessionHooks } from '../src/manager/session-hooks.mjs'; +import { ProviderRegistry } from '../src/manager/providers.mjs'; +import { createAccountEditor } from '../src/manager/accounts.mjs'; +import { createManagerServer } from '../src/manager/server.mjs'; +import { SessionManager } from '../src/manager/session-manager.mjs'; +import { McpConnect, ownServer } from '../src/manager/mcp-connect.mjs'; + +const here = path.dirname(fileURLToPath(import.meta.url)); +const event = (seq, name, data) => ({ seq, event: name, data }); +const heartbeat = (seq, open = {}) => ({ seq, open: { sessions: {}, working: [], subagents: {}, tools: {}, waits: {}, ...open } }); +const statuses = (live, account = 'default') => Object.fromEntries(live.view(account).agents.map((a) => [a.agentId, a.status])); +const counts = (l) => l && { working: l.working, waiting: l.waiting }; +const SECRET = 'sk-live-0123456789abcdef'; +const MASKED = 'sk-liv...cdef'; + +async function until(what, read, done) { + const deadline = Date.now() + 15000; + for (;;) { + const value = await read(); + if (done(value)) return value; + assert.ok(Date.now() < deadline, `${what} never came:\n${JSON.stringify(value, null, 2)}`); + await new Promise((resolve) => setTimeout(resolve, 50)); + } +} + +// The real plugin under Python, with Hermes 0.23.1's plugin context and modules stood in by their real names and +// signatures. Redaction masks only under force=True, as Hermes does with security.redact_secrets off; 'broken' raises. +// `home` is the profile's Hermes home, which get_hermes_home() answers ('broken' makes it raise); a token file made here +// records `recordedHome`, by default that home, as the manager names it. +async function startPlugin(t, { url, token, redact = 'mask', heartbeatSeconds = null, tokenFile = null, proxy = null, home = null, recordedHome = null }) { + const stub = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-plugin-')); + t.after(() => fs.rmSync(stub, { recursive: true, force: true })); + const ownHome = home && home !== 'broken' ? home : path.join(stub, 'home'); + fs.mkdirSync(ownHome, { recursive: true }); + const homeKey = hermesHomeKey(ownHome); + fs.writeFileSync(path.join(stub, 'hermes_constants.py'), 'import os\nfrom pathlib import Path\n\ndef get_hermes_home():\n return Path(os.environ["TEST_HERMES_HOME"])\n'); + fs.mkdirSync(path.join(stub, 'agent')); + fs.writeFileSync(path.join(stub, 'agent', '__init__.py'), ''); + fs.writeFileSync(path.join(stub, 'agent', 'secret_scope.py'), 'import os\n\ndef get_secret(name):\n return os.environ.get(name)\n'); + fs.writeFileSync(path.join(stub, 'agent', 'redact.py'), [ + 'def redact_sensitive_text(text, *, force=False, code_file=False, file_read=False, secret_file=False, redact_url_credentials=False):', + redact === 'broken' ? ' raise RuntimeError("redaction failed")' : ` return text.replace(${JSON.stringify(SECRET)}, ${JSON.stringify(MASKED)}) if force else text`, + ].join('\n')); + if (!tokenFile) { + tokenFile = path.join(stub, 'report.token'); + fs.writeFileSync(tokenFile, `${token}\n${recordedHome ?? homeKey}\n${url}\n`); + } + fs.writeFileSync(path.join(stub, 'drive.py'), [ + 'import gc, importlib.util, json, os, sys, threading', + 'hooks = {}', + 'unloads = []', + 'loads = [0]', + 'class Ctx:', + ' profile_name = "default"', + ' def register_hook(self, name, callback):', + ' hooks[name] = callback', + ' def on_unload(self, callback):', + ' unloads.append(callback)', + 'def load():', + ' loads[0] += 1', + ' hooks.clear()', + ' spec = importlib.util.spec_from_file_location(f"agent_guild_plugin_{loads[0]}", sys.argv[1])', + ' plugin = importlib.util.module_from_spec(spec)', + ' spec.loader.exec_module(plugin)', + ' plugin.HEARTBEAT_SECONDS = float(os.environ.get("TEST_HEARTBEAT_SECONDS", plugin.HEARTBEAT_SECONDS))', + ' plugin.register(Ctx())', + 'load()', + 'print("ready", flush=True)', + 'for line in sys.stdin:', + ' message = json.loads(line)', + ' if message.get("cmd") == "reload":', + ' for callback in unloads:', + ' callback()', + ' unloads.clear()', + ' load()', + ' print("fired", flush=True)', + ' elif message.get("cmd") == "state":', + ' reporters = [o for o in gc.get_objects() if type(o).__name__ == "Reporter" and not getattr(o, "stopped", False)]', + ' threads = sum(1 for t in threading.enumerate() if t.name == "agent-guild-reporter" and t.is_alive())', + ' print(json.dumps({"open": [{k: sorted(v) for k, v in r.open.items()} for r in reporters], "threads": threads}), flush=True)', + ' else:', + ' callback = hooks.get(message["hook"])', + ' if callback:', + ' callback(**message["kw"])', + ' print("fired", flush=True)', + ].join('\n')); + const python = spawn(process.platform === 'win32' ? 'python' : 'python3', [path.join(stub, 'drive.py'), path.join(here, '..', 'plugins', 'hermes', 'agent-guild', '__init__.py')], { + env: { + // With a proxy, the plugin runs with only HTTP_PROXY set: no other proxy setting and no NO_PROXY exception. + ...(proxy ? Object.fromEntries(Object.entries(process.env).filter(([name]) => !/^(https?|all|no)_proxy$/i.test(name))) : process.env), + ...(proxy && { HTTP_PROXY: proxy, http_proxy: proxy }), + PYTHONPATH: stub, PYTHONDONTWRITEBYTECODE: '1', AGENT_GUILD_HERMES_TOKEN_FILE: tokenFile, ...(home !== 'broken' && { TEST_HERMES_HOME: ownHome }), ...(heartbeatSeconds && { TEST_HEARTBEAT_SECONDS: String(heartbeatSeconds) }), + }, + stdio: ['pipe', 'pipe', 'inherit'], + }); + t.after(() => python.kill()); + const lines = readline.createInterface({ input: python.stdout })[Symbol.asyncIterator](); + assert.equal((await lines.next()).value, 'ready'); + const send = async (message) => { + python.stdin.write(`${JSON.stringify(message)}\n`); + return (await lines.next()).value; + }; + return { + fire: async (hook, kw) => assert.equal(await send({ hook, kw }), 'fired'), + reload: async () => assert.equal(await send({ cmd: 'reload' }), 'fired'), + state: async () => JSON.parse(await send({ cmd: 'state' })), + tokenFile, + homeKey, + }; +} + +test('a turn works from its start and waits on its own session, in Hermes 0.23.1\'s hook order', () => { + const live = new HermesLive({ sweepMs: 0 }); + let seq = 0; + const send = (name, data) => live.receive('default', 'gateway', [event(++seq, name, data)]); + const turn = 'local-s1:local-s1:b2401558'; + send('on_session_start', { session_id: 'local-s1', platform: 'cli' }); + assert.deepEqual(statuses(live), {}, 'an idle session is no helper'); + send('pre_llm_call', { session_id: 'local-s1', turn_id: turn, platform: 'cli' }); + assert.deepEqual(statuses(live), { 'hermes:local-s1': 'working' }, 'a turn works from its start'); + send('on_session_start', { session_id: 'local-s2', platform: 'telegram' }); + send('pre_llm_call', { session_id: 'local-s2', turn_id: 'local-s2:local-s2:0c4f12aa', platform: 'telegram' }); + send('pre_tool_call', { session_id: 'local-s1', turn_id: turn, tool_call_id: 'call_rig_1', tool_name: 'terminal' }); + send('on_human_input_request', { session_id: 'local-s1', request_id: 'r1', kind: 'approval' }); + send('pre_approval_request', { session_id: 'local-s1', turn_id: turn, tool_call_id: 'call_rig_1' }); + assert.deepEqual(statuses(live), { 'hermes:local-s1': 'waiting', 'hermes:local-s2': 'working' }); + send('pre_approval_request', { session_id: 'local-s2', turn_id: 'local-s2:local-s2:0c4f12aa', tool_call_id: 'call_rig_2' }); + assert.deepEqual(statuses(live), { 'hermes:local-s1': 'waiting', 'hermes:local-s2': 'waiting' }); + send('post_approval_response', { session_id: 'local-s1', turn_id: turn, tool_call_id: 'call_rig_1', choice: 'once' }); + send('on_human_input_resolved', { session_id: 'local-s1', request_id: 'r1', kind: 'approval', outcome: 'once' }); + send('post_tool_call', { session_id: 'local-s1', turn_id: turn, tool_call_id: 'call_rig_1', tool_name: 'terminal' }); + send('post_llm_call', { session_id: 'local-s1', turn_id: turn, reply: 'done' }); + send('on_session_end', { session_id: 'local-s1', turn_id: turn, platform: 'cli' }); + assert.deepEqual(statuses(live), { 'hermes:local-s2': 'waiting' }); +}); + +test('a heartbeat closes what a lost event left open, the last empty one included', () => { + const live = new HermesLive({ sweepMs: 0 }); + live.receive('default', 'a', [ + event(1, 'on_session_start', { session_id: 's1', platform: 'telegram' }), + event(2, 'pre_llm_call', { session_id: 's1', turn_id: 'u1' }), + event(3, 'pre_tool_call', { session_id: 's1', turn_id: 'u1', tool_name: 'terminal', tool_call_id: 't1' }), + event(4, 'pre_approval_request', { session_id: 's1', turn_id: 'u1', tool_call_id: 't1' }), + ]); + assert.deepEqual(statuses(live), { 'hermes:s1': 'waiting' }); + assert.equal(live.view('default').shells.length, 1); + live.receive('default', 'a', [heartbeat(7, { sessions: { s1: { platform: 'telegram' } }, working: ['s1'] })]); + assert.deepEqual(statuses(live), { 'hermes:s1': 'working' }); + assert.equal(live.view('default').shells.length, 0); + live.receive('default', 'a', [heartbeat(9)]); + assert.deepEqual(live.view('default').agents, []); + assert.deepEqual(counts(live.live('default')), { working: 0, waiting: 0 }); +}); + +test('an event older than the last applied heartbeat is ignored', () => { + const live = new HermesLive({ sweepMs: 0 }); + live.receive('default', 'a', [event(1, 'on_session_start', { session_id: 's1' }), heartbeat(5, { sessions: { s1: {} } })]); + live.receive('default', 'a', [event(4, 'pre_llm_call', { session_id: 's1', turn_id: 'u1' }), event(3, 'pre_tool_call', { session_id: 's1', turn_id: 'u1', tool_call_id: 't1' })]); + assert.deepEqual(statuses(live), {}); +}); + +test('two reporting processes of one profile keep their own helpers', () => { + const live = new HermesLive({ sweepMs: 0 }); + live.receive('default', 'gateway', [event(1, 'pre_llm_call', { session_id: 's1', turn_id: 'u1' })]); + live.receive('default', 'cli', [event(1, 'pre_llm_call', { session_id: 's2', turn_id: 'u2' }), heartbeat(2, { sessions: { s2: {} }, working: ['s2'] })]); + assert.deepEqual(Object.keys(statuses(live)).sort(), ['hermes:s1', 'hermes:s2']); + live.receive('default', 'cli', [heartbeat(3)]); + assert.deepEqual(Object.keys(statuses(live)), ['hermes:s1']); +}); + +test('a heartbeat that lists a subagent as a session too counts it once', () => { + const live = new HermesLive({ sweepMs: 0 }); + live.receive('default', 'a', [ + event(1, 'subagent_start', { parent_session_id: 'p1', child_session_id: 'c1', child_role: 'leaf', child_goal: 'Map the code' }), + event(2, 'pre_approval_request', { session_id: 'c1', turn_id: 'c1:t', tool_call_id: 'k1' }), + heartbeat(3, { sessions: { p1: {}, c1: {} }, subagents: { c1: {} }, working: ['c1'], waits: { 'approval:k1': { session_id: 'c1' } } }), + ]); + assert.deepEqual(live.view('default').agents.map((a) => `${a.agentId}:${a.name}:${a.status}`), ['hermes:c1:Map the code:waiting']); +}); + +test('a session two reporting processes both report, as after an unload whose last heartbeat was lost, counts once', () => { + const live = new HermesLive({ sweepMs: 0 }); + for (const instance of ['unloaded', 'reloaded']) live.receive('default', instance, [event(1, 'pre_llm_call', { session_id: 's1', turn_id: 'u1', platform: 'telegram' })]); + assert.deepEqual(counts(live.live('default')), { working: 1, waiting: 0 }); +}); + +test('a heartbeat alone rebuilds what it lists, as after a manager restart or a lost batch', () => { + const live = new HermesLive({ sweepMs: 0 }); + live.receive('default', 'a', [heartbeat(4, { + sessions: { s1: { platform: 'telegram' } }, + working: ['s1', 'c1'], + subagents: { c1: { parent_session_id: 's1', child_role: 'leaf', child_goal: 'Map the code' } }, + tools: { 's1:t1': { session_id: 's1', tool_name: 'terminal' } }, + waits: { 'approval:s1:t1': { session_id: 's1' } }, + })]); + assert.deepEqual(live.view('default'), { + agents: [ + { agentId: 'hermes:s1', name: 'telegram', kind: 'session', status: 'waiting' }, + { agentId: 'hermes:c1', name: 'Map the code', kind: 'leaf', status: 'working', detail: 'Map the code' }, + ], + shells: [{ key: 'hermes:s1:t1', agentId: 'hermes:s1' }], + }); +}); + +test('a reporting process silent for the expiry time is dropped', () => { + let now = 1000; + const live = new HermesLive({ sweepMs: 0, now: () => now }); + live.receive('default', 'a', [event(1, 'subagent_start', { child_session_id: 'c1', child_goal: 'Map the code' })]); + now += HERMES_EXPIRY_MS - 1; + live.sweep(); + assert.deepEqual(statuses(live), { 'hermes:c1': 'working' }); + now += 1; + live.sweep(); + assert.equal(live.live('default'), null); +}); + +test("the Hermes plugin reports to a real manager: the tool card's live helpers, shells and counts per profile, and nothing on a Hermes terminal's card", { timeout: 60000 }, async (t) => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-')); + const apiToken = 'c7d1e0b2a4f6983e5d7c1b0a2f4e6d8c9b1a3e5f7d9c2b4a'; + fs.writeFileSync(path.join(home, 'auth-token'), `${apiToken}\n`); + const hermesHome = path.join(home, 'hermes'); + fs.mkdirSync(hermesHome); + const stored = new HermesTokens(path.join(home, 'hermes-tokens')); + await stored.ensure(hermesHomeKey(hermesHome), 'http://127.0.0.1:1'); + Object.assign(process.env, { AGENT_GUILD_HOME: home, AGENT_GUILD_PORT: '0', AGENT_GUILD_NO_UPDATE_CHECK: '1', AGENT_GUILD_SKIP_SHELL_ENV: '1' }); + const fixture = path.join(here, 'fixtures', 'fake-tool.mjs'); + fs.writeFileSync(path.join(home, 'providers.json'), JSON.stringify({ providers: [ + ...['anthropic', 'openai', 'google', 'xai', 'docker', 'shell'].map((id) => ({ id, enabled: false })), + { id: 'hermes', vendor: 'Nous Research', tool: 'Hermes Agent', command: process.execPath, args: [fixture], versionArgs: [fixture, '--version'], reporting: 'hermes', homeVar: 'HERMES_HOME', profiles: 'hermes', env: { HERMES_HOME: hermesHome } }, + ] })); + const { startManager } = await import('../src/manager/main.mjs'); + const ctx = await startManager({ sessionDefaults: { killGraceMs: 100 } }); + t.after(() => ctx.shutdown()); + const api = async (method, route, body) => { + const res = await fetch(`${ctx.api.url}/api/v1${route}`, { method, headers: { authorization: `Bearer ${apiToken}`, 'content-type': 'application/json' }, body: body && JSON.stringify(body) }); + return res.json(); + }; + const tokenFile = stored.file(hermesHomeKey(hermesHome)); + assert.equal(fs.readFileSync(tokenFile, 'utf8').split('\n')[2], ctx.api.url, 'a start gives every token file the address the manager now has'); + const { fire, state, reload } = await startPlugin(t, { tokenFile, home: hermesHome }); + + const { session } = await api('POST', '/sessions', { providerId: 'hermes', cwd: home }); + const card = () => api('GET', `/sessions/${session.id}`).then((r) => r.session); + const live = () => api('GET', '/providers').then((r) => r.providers.find((p) => p.id === 'hermes').accounts[0].live); + const helpers = (l) => Object.fromEntries((l?.agents ?? []).map((a) => [a.id, `${a.name}:${a.status}`])); + const untouched = async () => { + const own = await card(); + assert.deepEqual({ agents: own.agents, shells: own.shells, reporting: own.reporting, reply: own.reply, model: own.model, compactions: own.context.compactions }, + { agents: [], shells: [], reporting: null, reply: null, model: null, compactions: 0 }); + }; + + const turn = 's1:s1:b2401558'; + await untouched(); + await fire('on_session_start', { session_id: 's1', platform: 'telegram' }); + await fire('pre_llm_call', { session_id: 's1', turn_id: turn, platform: 'telegram', user_message: 'clean the build' }); + await until('the turn and reporting heard', live, (s) => helpers(s)['hermes:s1'] === 'telegram:working'); + await fire('pre_tool_call', { session_id: 's1', turn_id: turn, tool_name: 'terminal', tool_call_id: 't1', args: { command: 'rm -rf build' } }); + await until('its shell command', live, (s) => s?.shells.length === 1); + await fire('on_human_input_request', { session_id: 's1', request_id: 'r1', kind: 'approval' }); + await fire('pre_approval_request', { session_id: 's1', turn_id: turn, tool_call_id: 't1', command: 'rm -rf build' }); + await until('the approval wait', live, (s) => helpers(s)['hermes:s1'] === 'telegram:waiting'); + assert.deepEqual(counts(await live()), { working: 0, waiting: 1 }); + await untouched(); + await fire('post_approval_response', { session_id: 's1', turn_id: turn, tool_call_id: 't1', choice: 'once' }); + await fire('on_human_input_resolved', { session_id: 's1', request_id: 'r1', kind: 'approval', outcome: 'once' }); + await fire('post_tool_call', { session_id: 's1', turn_id: turn, tool_call_id: 't1', tool_name: 'terminal', status: 'ok' }); + await fire('subagent_start', { parent_session_id: 's1', child_session_id: 'c1', child_role: 'leaf', child_goal: 'Map the code' }); + await until('the subagent', live, (s) => helpers(s)['hermes:c1'] === 'Map the code:working' && helpers(s)['hermes:s1'] === 'telegram:working' && s.shells.length === 0); + await fire('subagent_stop', { child_session_id: 'c1' }); + await fire('post_llm_call', { session_id: 's1', turn_id: turn, assistant_response: 'All done.', model: 'hermes-model' }); + await fire('post_compaction', { session_id: 's1' }); + await fire('on_session_end', { session_id: 's1', turn_id: turn }); + await until('the idle session leaving the card', live, (s) => Object.keys(helpers(s)).length === 0); + await fire('on_session_start', { session_id: 's3', platform: 'discord', model: 'hermes-model' }); + await fire('pre_llm_call', { session_id: 's3', turn_id: 's3:s3:41d0', platform: 'discord' }); + await until('the Discord session working', live, (s) => helpers(s)['hermes:s3'] === 'discord:working'); + await untouched(); + assert.deepEqual((await api('GET', `/sessions/${session.id}/reply`)).reply, null, 'Read has nothing of theirs to read'); + await fire('on_session_end', { session_id: 's3', turn_id: 's3:s3:41d0' }); + await fire('on_session_finalize', { session_id: 's1' }); + await fire('on_session_finalize', { session_id: 's3' }); + await until('the session leaving', live, (s) => Object.keys(helpers(s)).length === 0); + + const resumed = 'r1:r1:c0f3a9'; + await fire('on_session_open', { session_id: 'r1', platform: 'cli', reason: 'resume' }); + await fire('pre_llm_call', { session_id: 'r1', turn_id: resumed, platform: 'cli', parent_session_id: '' }); + await until('a session resumed with no on_session_start, working', live, (s) => helpers(s)['hermes:r1'] === 'cli:working'); + await fire('post_llm_call', { session_id: 'r1', turn_id: resumed, platform: 'cli', assistant_response: 'again' }); + await fire('on_session_end', { session_id: 'r1', turn_id: resumed, platform: 'cli' }); + await fire('on_session_finalize', { session_id: 'r1', platform: 'oneshot', reason: 'cli_close' }); + await until('the resumed session leaving', live, (s) => !helpers(s)['hermes:r1']); + + const parentTurn = 'p1:p1:9be0a8'; + const childTurn = 'c2:c2:3ed17f'; + await fire('on_session_open', { session_id: 'p1', platform: 'cli', reason: 'create' }); + await fire('on_session_start', { session_id: 'p1', platform: 'cli' }); + await fire('pre_llm_call', { session_id: 'p1', turn_id: parentTurn, platform: 'cli', parent_session_id: '' }); + await fire('pre_tool_call', { session_id: 'p1', turn_id: parentTurn, tool_call_id: 'call_delegate_1', tool_name: 'delegate_task' }); + await fire('subagent_start', { parent_session_id: 'p1', child_session_id: 'c2', child_subagent_id: 'sa-0-fa75f0b8', child_role: 'leaf', child_goal: 'Map the code' }); + await fire('on_session_start', { session_id: 'c2', platform: 'subagent' }); + await fire('pre_llm_call', { session_id: 'c2', turn_id: childTurn, platform: 'subagent', parent_session_id: 'p1' }); + await until('the parent and its subagent, each once', live, (s) => helpers(s)['hermes:p1'] === 'cli:working' && helpers(s)['hermes:c2'] === 'Map the code:working'); + assert.deepEqual(counts(await live()), { working: 2, waiting: 0 }); + await fire('on_human_input_request', { session_id: 'c2', request_id: 'r2', kind: 'approval' }); + await fire('pre_approval_request', { session_id: 'c2', turn_id: childTurn, tool_call_id: 'call_c2' }); + await until('the subagent waiting, counted once', live, (counts) => counts?.working === 1 && counts.waiting === 1); + await fire('post_approval_response', { session_id: 'c2', turn_id: childTurn, tool_call_id: 'call_c2', choice: 'once' }); + await fire('on_human_input_resolved', { session_id: 'c2', request_id: 'r2', kind: 'approval', outcome: 'once' }); + await fire('post_llm_call', { session_id: 'c2', turn_id: childTurn, platform: 'subagent', assistant_response: 'mapped' }); + await fire('on_session_end', { session_id: 'c2', turn_id: childTurn, platform: 'subagent' }); + await fire('subagent_stop', { parent_session_id: 'p1', child_session_id: 'c2', child_role: 'leaf', child_status: 'completed' }); + await fire('post_tool_call', { session_id: 'p1', turn_id: parentTurn, tool_call_id: 'call_delegate_1', tool_name: 'delegate_task' }); + await fire('post_llm_call', { session_id: 'p1', turn_id: parentTurn, platform: 'cli', assistant_response: 'delegated' }); + await fire('on_session_end', { session_id: 'p1', turn_id: parentTurn, platform: 'cli' }); + await fire('on_session_finalize', { session_id: 'p1', platform: 'oneshot', reason: 'cli_close' }); + assert.deepEqual((await state()).open, [{ sessions: [], working: [], subagents: [], tools: [], waits: [] }], 'a subagent, which Hermes never finalizes, is not kept open'); + + const slowTurn = 'u1:u1:b7260b'; + await fire('on_session_open', { session_id: 'u1', platform: 'cli', reason: 'create' }); + await fire('on_session_start', { session_id: 'u1', platform: 'cli' }); + await fire('pre_llm_call', { session_id: 'u1', turn_id: slowTurn, platform: 'cli', parent_session_id: '' }); + await until('the slow turn working', live, (s) => helpers(s)['hermes:u1'] === 'cli:working'); + await reload(); + await until('the unloaded load cleared at once', live, (s) => !helpers(s)['hermes:u1']); + await until('its reporter stopped', state, (s) => s.threads === 0); + await fire('post_llm_call', { session_id: 'u1', turn_id: slowTurn, platform: 'cli', assistant_response: 'slow done' }); + await fire('on_session_end', { session_id: 'u1', turn_id: slowTurn, platform: 'cli' }); + await fire('on_session_finalize', { session_id: 'u1', platform: 'oneshot', reason: 'cli_close' }); + await until('nothing stale after the reload', live, (counts) => counts?.working === 0 && counts.waiting === 0); + + await api('POST', `/sessions/${session.id}/stop`); + await until('the card to close', card, (s) => s.status === 'exited'); + await fire('on_session_start', { session_id: 's2', platform: 'cron' }); + await fire('pre_llm_call', { session_id: 's2', turn_id: 's2:s2:7a1e', platform: 'cron' }); + await until('live counts with no card open', live, (counts) => counts?.working === 1 && counts.waiting === 0); +}); + +async function captureServer(t) { + const received = []; + const held = []; + let holding = false; + const server = http.createServer((req, res) => { + let body = ''; + req.on('data', (chunk) => { body += chunk; }); + req.on('end', () => { + received.push(JSON.parse(body)); + const answer = () => res.end('{"ok":true}'); + if (holding) held.push(answer); + else answer(); + }); + }); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + t.after(() => server.close()); + return { + url: `http://127.0.0.1:${server.address().port}`, + received, + held, + hold: () => { holding = true; }, + release: () => { holding = false; for (const answer of held.splice(0)) answer(); }, + }; +} + +test('the plugin reports straight to the local manager, never through an HTTP proxy, even with no NO_PROXY exception', { timeout: 30000 }, async (t) => { + const manager = await captureServer(t); + const proxied = []; + const proxy = http.createServer((req, res) => { + let body = ''; + req.on('data', (chunk) => { body += chunk; }); + req.on('end', () => { proxied.push({ url: req.url, headers: req.headers, body }); res.statusCode = 502; res.end(); }); + }); + await new Promise((resolve) => proxy.listen(0, '127.0.0.1', resolve)); + t.after(() => proxy.close()); + + const { fire } = await startPlugin(t, { url: manager.url, token: 'report-token', proxy: `http://127.0.0.1:${proxy.address().port}` }); + await fire('on_session_start', { session_id: 'proxied-session', platform: 'telegram' }); + await fire('pre_llm_call', { session_id: 'proxied-session', turn_id: 'u1', platform: 'telegram' }); + await until('the manager has the turn', () => sentEvents(manager.received), (events) => events.some((e) => e.event === 'pre_llm_call')); + assert.deepEqual(sentEvents(manager.received).map((e) => [e.event, e.data.session_id]), [['on_session_start', 'proxied-session'], ['pre_llm_call', 'proxied-session']]); + const leaked = JSON.stringify(proxied); + assert.equal(leaked.includes('report-token'), false, 'the proxy never sees the token'); + assert.equal(leaked.includes('proxied-session'), false, 'nor what is reported'); + assert.deepEqual(proxied, [], 'the proxy gets no request at all'); +}); + +const sentEvents = (received) => received.flatMap((body) => body.messages.filter((m) => m.event)); +const lastOpen = (received, instance) => received.filter((b) => !instance || b.instance === instance).flatMap((b) => b.messages.filter((m) => m.open)).at(-1)?.open; + +test('the plugin redacts with Hermes\'s redactor, sends what rebuilds open items, and clears its instance when unloaded mid-send', { timeout: 30000 }, async (t) => { + const manager = await captureServer(t); + const { fire, reload } = await startPlugin(t, { url: manager.url, token: 'report-token' }); + await fire('on_session_start', { session_id: 's1', platform: 'telegram' }); + await fire('pre_llm_call', { session_id: 's1', turn_id: 'u1', platform: 'telegram' }); + await fire('pre_tool_call', { session_id: 's1', turn_id: 'u1', tool_call_id: 't1', tool_name: 'terminal', args: { command: `export KEY=${SECRET}` } }); + await fire('pre_approval_request', { session_id: 's1', turn_id: 'u1', tool_call_id: 't1' }); + await fire('subagent_start', { parent_session_id: 's1', child_session_id: 'c1', child_role: 'leaf', child_goal: `use ${SECRET}` }); + await until('the subagent sent', () => sentEvents(manager.received), (events) => events.some((e) => e.event === 'subagent_start')); + const sent = JSON.stringify(manager.received); + assert.equal(sent.includes(SECRET), false, 'no secret leaves'); + const byName = Object.fromEntries(sentEvents(manager.received).map((e) => [e.event, e.data])); + assert.equal(byName.pre_tool_call.command, `export KEY=${MASKED}`); + assert.equal(byName.subagent_start.child_goal, `use ${MASKED}`); + + const open = lastOpen(manager.received); + assert.deepEqual(open, { + sessions: { s1: { platform: 'telegram' } }, + working: ['s1'], + subagents: { c1: { parent_session_id: 's1', child_role: 'leaf', child_goal: `use ${MASKED}` } }, + tools: { 's1:t1': { session_id: 's1', tool_name: 'terminal' } }, + waits: { 'approval:s1:t1': { session_id: 's1' } }, + }); + const restarted = new HermesLive({ sweepMs: 0 }); + restarted.receive('default', 'gateway', [{ seq: 1, open }]); + assert.deepEqual(counts(restarted.live('default')), { working: 1, waiting: 1 }, 'a restarted manager rebuilds the wait from one heartbeat'); + assert.equal(restarted.view('default').shells.length, 1); + + const unloaded = manager.received[0].instance; + manager.hold(); + await fire('post_tool_call', { session_id: 's1', turn_id: 'u1', tool_call_id: 't1', tool_name: 'terminal' }); + await until('a send in flight', () => manager.held.length, (n) => n === 1); + await reload(); + manager.release(); + await until('the unloaded instance\'s last heartbeat, empty', () => lastOpen(manager.received, unloaded), (last) => last && Object.values(last).every((v) => Object.keys(v).length === 0)); +}); + +test('when Hermes\'s redactor fails, the command and subagent goal are left out, never sent raw', { timeout: 30000 }, async (t) => { + const manager = await captureServer(t); + const { fire } = await startPlugin(t, { url: manager.url, token: 'report-token', redact: 'broken' }); + await fire('pre_tool_call', { session_id: 's1', turn_id: 'u1', tool_call_id: 't1', tool_name: 'terminal', args: { command: `export KEY=${SECRET}` } }); + await fire('subagent_start', { parent_session_id: 's1', child_session_id: 'c1', child_role: 'leaf', child_goal: `use ${SECRET}` }); + await until('the subagent event sent', () => sentEvents(manager.received), (events) => events.some((e) => e.event === 'subagent_start')); + assert.equal(JSON.stringify(manager.received).includes(SECRET), false); + const byName = Object.fromEntries(sentEvents(manager.received).map((e) => [e.event, e.data])); + assert.equal('command' in byName.pre_tool_call, false); + assert.equal('child_goal' in byName.subagent_start, false); + assert.equal('child_goal' in lastOpen(manager.received).subagents.c1, false); +}); + +function makeHermesHome(base, names) { + for (const [name, file] of names) { + fs.mkdirSync(path.join(base, 'profiles', name), { recursive: true }); + fs.writeFileSync(path.join(base, 'profiles', name, file), ''); + } +} + +function hermesRegistry(t, { env = {}, providerEnv = null } = {}) { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-profiles-')); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + const base = hermesRoot({ HOME: root, USERPROFILE: root }, process.platform); + makeHermesHome(base, [['work', 'config.yaml'], ['coder', '.env'], ['Bad Name', 'config.yaml']]); + fs.mkdirSync(path.join(base, 'profiles', 'logs-only')); + const userFile = path.join(root, 'providers.json'); + fs.writeFileSync(userFile, JSON.stringify({ providers: [ + { id: 'hermes', vendor: 'Nous Research', tool: 'Hermes Agent', command: 'hermes', reporting: 'hermes', homeVar: 'HERMES_HOME', profiles: 'hermes', accounts: [{ id: 'mine', label: 'Mine' }], ...(providerEnv && { env: providerEnv }) }, + ] })); + const registry = new ProviderRegistry({ userFile, env: { PATH: '', HOME: root, USERPROFILE: root, ...env }, platform: process.platform, checkUpdates: false, accountsDir: path.join(root, 'accounts') }); + return { registry, base, root, userFile }; +} + +test("a Hermes card's accounts are one home each: Hermes's base home, then each profile folder Hermes recognizes", (t) => { + const { registry, base, userFile } = hermesRegistry(t); + const hermes = registry.get('hermes'); + assert.deepEqual(registry.describe(hermes).accounts.map((a) => a.id), ['default', 'coder', 'work']); + assert.equal(registry.describe(hermes).accountsEditable, false); + const pick = (id) => { const a = registry.account(hermes, id); return { env: a.env, args: a.args, dir: a.dir }; }; + assert.deepEqual(pick('default'), { env: { HERMES_HOME: base }, args: ['-p', 'default'], dir: base }, 'the default selects the base home explicitly, whatever profile is active'); + assert.deepEqual(pick('work'), { env: { HERMES_HOME: base }, args: ['-p', 'work'], dir: path.join(base, 'profiles', 'work') }); + const selections = registry.accountsFor(hermes).map((a) => a.args.join(' ')); + assert.equal(new Set(selections).size, selections.length, 'no two accounts select the same profile'); + const before = fs.readFileSync(userFile, 'utf8'); + assert.throws(() => createAccountEditor({ registry, inUse: () => false }).add('hermes', { label: 'New' }), { code: 'accounts_unsupported' }); + assert.equal(fs.readFileSync(userFile, 'utf8'), before, 'no Hermes home is made'); + assert.equal(fs.existsSync(path.join(path.dirname(userFile), 'accounts')), false); +}); + +test("with HERMES_HOME set where the manager runs, the Hermes card's accounts come from that home", (t) => { + const custom = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-custom-')); + t.after(() => fs.rmSync(custom, { recursive: true, force: true })); + makeHermesHome(custom, [['team', 'config.yaml']]); + const { registry } = hermesRegistry(t, { env: { HERMES_HOME: custom } }); + const hermes = registry.get('hermes'); + assert.deepEqual(registry.accountsFor(hermes).map((a) => [a.id, a.dir, a.args, a.env]), [['default', custom, ['-p', 'default'], { HERMES_HOME: custom }], ['team', path.join(custom, 'profiles', 'team'), ['-p', 'team'], { HERMES_HOME: custom }]], "the manager's own HERMES_HOME stays in force"); + + const fromCard = hermesRegistry(t, { providerEnv: { HERMES_HOME: custom } }).registry; + assert.deepEqual(fromCard.accountsFor(fromCard.get('hermes')).map((a) => [a.id, a.dir]), [['default', custom], ['team', path.join(custom, 'profiles', 'team')]], 'the card env HERMES_HOME Hermes is started with'); +}); + +test('with HERMES_HOME set to a custom home inside the native home, the accounts are the profiles hermes -p reaches there', (t) => { + const { root, base, userFile } = hermesRegistry(t); + const isolated = path.join(base, 'isolated'); + makeHermesHome(isolated, [['solo', 'config.yaml']]); + const reg = new ProviderRegistry({ userFile, env: { PATH: '', HOME: root, USERPROFILE: root, HERMES_HOME: isolated }, platform: process.platform, checkUpdates: false, accountsDir: path.join(root, 'accounts') }); + assert.deepEqual(reg.accountsFor(reg.get('hermes')).map((a) => [a.id, a.dir]), [['default', isolated], ['solo', path.join(isolated, 'profiles', 'solo')]]); +}); + +test('with HERMES_HOME set to a profile inside the native home, the accounts come from the native home, as Hermes resolves it', (t) => { + const inside = hermesRegistry(t); + const reg = new ProviderRegistry({ userFile: inside.userFile, env: { PATH: '', HOME: inside.root, USERPROFILE: inside.root, HERMES_HOME: path.join(inside.base, 'profiles', 'work') }, platform: process.platform, checkUpdates: false, accountsDir: path.join(inside.root, 'accounts') }); + assert.deepEqual(reg.accountsFor(reg.get('hermes')).map((a) => [a.id, a.dir]), [['default', inside.base], ['coder', path.join(inside.base, 'profiles', 'coder')], ['work', path.join(inside.base, 'profiles', 'work')]]); +}); + +test('turning Hermes reporting off clears what the profile reports at once, refuses its reports meanwhile, and restores them when it fails', { timeout: 30000 }, async (t) => { + const { registry } = hermesRegistry(t); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-off-')); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const tokens = new HermesTokens(path.join(dir, 'hermes-tokens')); + const reportToken = await tokens.ensure(registry.account(registry.get('hermes'), 'work').home); + const live = new HermesLive({ sweepMs: 0 }); + registry.liveFor = (provider, account) => live.live(account.home); + let finish; + const sessionHooks = { setEnabled: () => new Promise((resolve, reject) => { finish = { resolve, reject }; }) }; + const manager = Object.assign(new EventEmitter(), { list: () => [], refreshMultiplexers() {}, sessions: new Map(), sessionHooks }); + const token = 'f'.repeat(48); + const api = createManagerServer({ manager, registry, token, port: 0, webDir: path.join(here, '..', 'web'), hermes: { tokens, live } }); + await api.listen(); + t.after(() => api.close()); + const report = (seq) => fetch(`${api.url}/api/v1/hermes/hook`, { method: 'POST', headers: { 'content-type': 'application/json', 'x-agent-guild-report-token': reportToken }, + body: JSON.stringify({ instance: 'gateway', messages: [{ seq, event: 'pre_llm_call', data: { session_id: 's1', turn_id: `u${seq}`, platform: 'telegram' } }] }) }); + const turnOff = () => fetch(`${api.url}/api/v1/providers/hermes/reporting`, { method: 'POST', headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' }, body: JSON.stringify({ enabled: false }) }); + const shown = async () => (await (await fetch(`${api.url}/api/v1/providers`, { headers: { authorization: `Bearer ${token}` } })).json()).providers.find((p) => p.id === 'hermes').accounts.find((a) => a.id === 'work').live; + + await report(1); + assert.deepEqual(counts(await shown()), { working: 1, waiting: 0 }); + const failing = turnOff(); + await until('the off request under way', () => finish, Boolean); + assert.equal(await shown(), null, 'cleared as soon as Off is asked'); + await report(2); + assert.equal(await shown(), null, 'a report during the switch-off is refused'); + finish.reject(Object.assign(new Error('Hermes Agent could not turn off the Agent Guild plugin: locked'), { status: 502, code: 'reporting_setup_failed' })); + let updates = 0; + registry.on('updated', () => updates++); + const failed = await failing; + assert.equal(failed.status, 502); + assert.equal(updates, 1, 'a failed switch still sends the cards what is now true'); + assert.match((await failed.json()).error.message, /could not turn off/); + await report(3); + assert.deepEqual(counts(await shown()), { working: 1, waiting: 0 }, 'reports count again after the switch-off failed'); + + finish = null; + const succeeding = turnOff(); + await until('the second off request under way', () => finish, Boolean); + assert.equal(await shown(), null); + finish.resolve(false); + assert.equal((await succeeding).status, 200); + assert.equal(await shown(), null); +}); + +test('a card with more sessions than it shows still counts them all', () => { + const live = new HermesLive({ sweepMs: 0 }); + const ids = Array.from({ length: 65 }, (_, i) => `s${i}`); + live.receive('default', 'a', [heartbeat(1, { sessions: Object.fromEntries(ids.map((id) => [id, {}])), working: ids })]); + assert.equal(live.view('default').agents.length, 64); + assert.deepEqual(counts(live.live('default')), { working: 65, waiting: 0 }); +}); + +test('an approval Hermes reports with its turn and no session is rebuilt as that session waiting', { timeout: 30000 }, async (t) => { + const manager = await captureServer(t); + const { fire } = await startPlugin(t, { url: manager.url, token: 'report-token' }); + await fire('pre_llm_call', { session_id: 's1', turn_id: 'u1', platform: 'cli' }); + await fire('pre_approval_request', { turn_id: 'u1', tool_call_id: 't1' }); + const open = await until('the wait in a heartbeat', () => lastOpen(manager.received), (last) => last && Object.keys(last.waits).length === 1); + assert.equal(sentEvents(manager.received).find((e) => e.event === 'pre_approval_request').data.session_id, 's1'); + const restarted = new HermesLive({ sweepMs: 0 }); + restarted.receive('default', 'gateway', [{ seq: 1, open }]); + assert.deepEqual(counts(restarted.live('default')), { working: 0, waiting: 1 }); +}); + +test("Hermes's default home on Windows follows LOCALAPPDATA and the data folder suffix", () => { + const user = { USERPROFILE: 'C:\\Users\\ada' }; + assert.equal(hermesRoot({ ...user, LOCALAPPDATA: 'D:\\Local' }, 'win32'), 'D:\\Local\\hermes'); + assert.equal(hermesRoot(user, 'win32'), 'C:\\Users\\ada\\AppData\\Local\\hermes'); + assert.equal(hermesRoot({ ...user, LocalAppData: 'D:\\Local', HERMES_DATA_DIR_SUFFIX: '-dev' }, 'win32'), 'D:\\Local\\hermes-dev'); + assert.equal(hermesRoot({ ...user, LOCALAPPDATA: 'D:\\Local', HERMES_HOME: '%LOCALAPPDATA%\\other\\profiles\\work' }, 'win32'), 'D:\\Local\\other'); + assert.equal(hermesRoot({ HOME: '/home/ada', HERMES_DATA_DIR_SUFFIX: '-dev' }, 'linux'), '/home/ada/.hermes-dev'); + assert.equal(hermesRoot({ HOME: '/home/ada', DATA: '/srv', HERMES_HOME: '${DATA}/hermes' }, 'linux'), '/srv/hermes'); +}); + +test('a Hermes profile is listed by Hermes\'s own rules: names up to 64 characters, never one it deleted', (t) => { + const { registry, base, userFile, root } = hermesRegistry(t); + const long = 'a'.repeat(64); + makeHermesHome(base, [[long, 'config.yaml'], ['a'.repeat(65), 'config.yaml'], ['gone', 'config.yaml']]); + fs.mkdirSync(path.join(base, 'profiles', '.deleted', 'gone'), { recursive: true }); + fs.mkdirSync(path.join(base, 'profiles', 'folder-marker', 'config.yaml'), { recursive: true }); + const reg = new ProviderRegistry({ userFile, env: registry.env, platform: process.platform, checkUpdates: false, accountsDir: path.join(root, 'accounts') }); + assert.deepEqual(reg.accountsFor(reg.get('hermes')).map((a) => a.id), ['default', long, 'coder', 'work']); +}); + +const win = process.platform === 'win32'; + +/** Starts Python holding `file` open until it reads a line, added to `readers` first so it is stopped even when this fails. */ +async function holdOpen(file, readers) { + const reader = spawn('python', ['-c', 'import sys\nf = open(sys.argv[1], encoding="utf-8")\nprint("open", flush=True)\nsys.stdin.readline()\nf.close()\nprint("closed", flush=True)', file], { stdio: ['pipe', 'pipe', 'inherit'] }); + readers.push(reader); + const lines = readline.createInterface({ input: reader.stdout })[Symbol.asyncIterator](); + assert.equal((await lines.next()).value, 'open'); + return { reader, lines }; +} + +/** Stops the readers and waits until each has exited, so the files they held can be deleted. */ +function stopReaders(readers) { + return Promise.all(readers.map((reader) => (reader.exitCode !== null || reader.signalCode !== null ? null : new Promise((resolve) => { + reader.once('exit', resolve); + reader.once('error', resolve); + reader.kill(); + })))); +} + +/** A deletion or replacement another process blocks fails with EPERM, or EBUSY from Node 22's rmSync. */ +const blocked = (err) => ['EPERM', 'EBUSY'].includes(err.code); + +/** The plugin commit the Hermes setup tests install, as a release stamps it. */ +const PLUGIN_COMMIT = '0123456789abcdef0123456789abcdef01234567'; + +/** With `relative`, each card's HERMES_HOME is relative and the manager runs in `root`, where it resolves. */ +function hermesSetup(t, cards, { relative = false, pluginCommit = PLUGIN_COMMIT } = {}) { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-setup-')); + const before = process.cwd(); + if (relative) process.chdir(root); + t.after(() => { + process.chdir(before); + fs.rmSync(root, { recursive: true, force: true }); + }); + const bin = path.join(root, 'bin'); + fs.mkdirSync(bin); + const fake = path.join(here, 'fixtures', 'fake-hermes.mjs'); + if (win) fs.writeFileSync(path.join(bin, 'hermes.cmd'), `@echo off\r\n"${process.execPath}" "${fake}" %*\r\n`); + else fs.writeFileSync(path.join(bin, 'hermes'), `#!/bin/sh\nexec "${process.execPath}" "${fake}" "$@"\n`, { mode: 0o755 }); + const log = path.join(root, 'hermes.log'); + const systemDirs = win ? ['System32', ''].map((dir) => path.join(process.env.SystemRoot || 'C:\\Windows', dir)) : ['/usr/bin', '/bin']; + const env = { + PATH: [bin, ...systemDirs].join(path.delimiter), HOME: root, USERPROFILE: root, FAKE_HERMES_LOG: log, FAKE_HERMES_HOMES: path.join(root, 'homes.log'), + ...(win && { SystemRoot: process.env.SystemRoot, ComSpec: process.env.ComSpec, PATHEXT: '.COM;.EXE;.BAT;.CMD' }), + }; + const userFile = path.join(root, 'providers.json'); + fs.writeFileSync(userFile, JSON.stringify({ providers: Object.entries(cards).map(([id, home]) => { + fs.mkdirSync(path.join(root, home), { recursive: true }); + return { id, vendor: 'Nous Research', tool: 'Hermes Agent', command: 'hermes', reporting: 'hermes', homeVar: 'HERMES_HOME', profiles: 'hermes', env: { HERMES_HOME: relative ? home : path.join(root, home) }, ...(relative && { mcp: 'hermes' }) }; + }) })); + const registry = new ProviderRegistry({ userFile, env, platform: process.platform, checkUpdates: false, accountsDir: path.join(root, 'accounts') }); + const tokens = new HermesTokens(path.join(root, 'hermes-tokens')); + const hooks = new SessionHooks({ registry, dir: path.join(root, 'reporting'), version: '1', hermesTokens: tokens, hermesPluginCommit: pluginCommit, getApiUrl: () => env.TEST_API_URL ?? 'http://127.0.0.1:47821' }); + const home = (id) => registry.accountsFor(registry.get(id))[0]; + const token = (id) => tokens.get(home(id).home); + const homes = () => (fs.existsSync(env.FAKE_HERMES_HOMES) ? fs.readFileSync(env.FAKE_HERMES_HOMES, 'utf8').trim().split('\n').map((line) => JSON.parse(line)) : []); + return { registry, tokens, hooks, env, log, home, token, homes, root }; +} + +test('a relative HERMES_HOME is pinned where discovery found it: reporting setup, MCP commands and terminals, run elsewhere, all target that home', { timeout: 30000 }, async (t) => { + const { registry, hooks, env, home, homes, root } = hermesSetup(t, { hermes: 'relative-home' }, { relative: true }); + makeHermesHome(home('hermes').dir, [['work', 'config.yaml']]); + registry.reload(); + const hermes = registry.get('hermes'); + const base = path.join(process.cwd(), 'relative-home'); + assert.deepEqual(registry.accountsFor(hermes).map((a) => [a.id, a.dir, a.env.HERMES_HOME, a.args]), + [['default', base, base, ['-p', 'default']], ['work', path.join(base, 'profiles', 'work'), base, ['-p', 'work']]], + "discovered against the manager's folder, each profile still chosen with -p"); + const discovered = Object.fromEntries(registry.accountsFor(hermes).map((a) => [a.id, a.home])); + const targeted = (from) => homes().slice(from).map((call) => [call.profile, hermesHomeKey(call.home)]); + const onlyDiscovered = (calls) => calls.every(([profile, key]) => discovered[profile] === key); + + // Reporting setup runs in Agent Guild's reporting folder. + assert.equal(await hooks.setEnabled(hermes, true), true); + const setup = targeted(0); + assert.deepEqual([...new Set(setup.map(([profile]) => profile))], ['default', 'work']); + assert.ok(onlyDiscovered(setup), JSON.stringify(setup)); + + // MCP commands run in the MCP folder. + const mcpCwd = path.join(root, 'mcp-folder'); + fs.mkdirSync(mcpCwd); + const mcp = new McpConnect({ registry, own: ownServer(process.execPath, path.join(root, 'guild')), home: path.join(root, 'guild'), prepareAccount: () => {}, dir: mcpCwd }); + let from = homes().length; + assert.deepEqual((await mcp.refresh(hermes, { force: true })).map((a) => [a.id, a.state]), [['default', 'off'], ['work', 'off']]); + const mcpCalls = targeted(from); + assert.deepEqual(mcpCalls.map(([profile]) => profile).sort(), ['default', 'work'], 'one probe per profile, in whatever order they finish'); + assert.ok(onlyDiscovered(mcpCalls), JSON.stringify(mcpCalls)); + + // A terminal runs in its own folder. + const project = path.join(root, 'project'); + fs.mkdirSync(project); + const manager = new SessionManager({ registry, baseEnv: env, getApiUrl: () => '' }); + try { + from = homes().length; + await manager.create({ providerId: 'hermes', cwd: project, account: 'work' }); + const terminal = await until('the terminal ran Hermes', () => targeted(from), (calls) => calls.length === 1); + assert.deepEqual(terminal, [['work', discovered.work]]); + } finally { + await manager.shutdown(); // before the folder it runs in is deleted + } +}); + +test('the Hermes report token stays in a file of Agent Guild\'s own: Hermes is given its path, and no command line or error carries the token', { timeout: 30000 }, async (t) => { + const { registry, tokens, hooks, env, log, home } = hermesSetup(t, { hermes: 'home' }); + const hermes = registry.get('hermes'); + const dotEnv = path.join(home('hermes').dir, '.env'); + fs.writeFileSync(dotEnv, 'OPENAI_API_KEY=keep\n'); + const commands = () => fs.readFileSync(log, 'utf8').trim().split('\n').map((line) => JSON.parse(line).slice(2).join(' ')); + + assert.equal(await hooks.setEnabled(hermes, true), true); + const token = tokens.get(home('hermes').home); + const file = tokens.file(home('hermes').home); + assert.match(token, /^[0-9a-f]{48}$/); + assert.deepEqual(fs.readFileSync(file, 'utf8').split('\n').slice(0, 3), [token, home('hermes').home, 'http://127.0.0.1:47821']); + if (!win) assert.equal(fs.statSync(file).mode & 0o777, 0o600); + assert.ok(commands().includes(`config set AGENT_GUILD_HERMES_TOKEN_FILE ${file}`), 'Hermes saves the path itself'); + assert.equal(fs.readFileSync(log, 'utf8').includes(token), false, 'no command carries the token'); + assert.equal(fs.readFileSync(dotEnv, 'utf8'), 'OPENAI_API_KEY=keep\n', 'Agent Guild writes no Hermes file'); + + env.FAKE_HERMES_FAIL = 'plugins disable'; + await assert.rejects(hooks.setEnabled(hermes, false), (err) => { + assert.equal(err.message, 'The Default profile failed: Hermes Agent could not turn off the Agent Guild plugin: it exited with code 1. Reporting is on for no profile.'); + return true; + }); + assert.equal(tokens.account(token), null, 'the token is revoked though Hermes could not turn its plugin off'); + assert.ok(commands().includes('config unset AGENT_GUILD_HERMES_TOKEN_FILE'), 'and the rest of the teardown ran'); + + delete env.FAKE_HERMES_FAIL; + assert.equal(await hooks.setEnabled(hermes, false), false, 'turning off again finishes, though the setting is already gone'); + assert.equal(commands().filter((c) => c === 'plugins disable agent-guild').length, 2); + assert.equal(tokens.get(home('hermes').home), null); + assert.equal(fs.existsSync(file), false); +}); + +test('when one Hermes profile fails to turn on, the error names it and the profiles that report, and their activity still shows', { timeout: 30000 }, async (t) => { + const { registry, hooks, env, home, tokens } = hermesSetup(t, { hermes: 'home' }); + makeHermesHome(home('hermes').dir, [['work', 'config.yaml']]); + registry.reload(); + const hermes = registry.get('hermes'); + assert.deepEqual(registry.accountsFor(hermes).map((a) => a.id), ['default', 'work']); + const live = new HermesLive({ sweepMs: 0 }); + registry.liveFor = (provider, account) => live.live(account.home); + + Object.assign(env, { FAKE_HERMES_FAIL: 'plugins install', FAKE_HERMES_FAIL_PROFILE: 'work' }); + await assert.rejects(hooks.setEnabled(hermes, true), (err) => { + assert.equal(err.message, 'The work profile failed: Hermes Agent could not install the Agent Guild plugin: it exited with code 1. Reporting is on for Default.'); + return true; + }); + const reporting = registry.accountsFor(hermes)[0].home; + live.receive(tokens.account(tokens.get(reporting)), 'gateway', [event(1, 'pre_llm_call', { session_id: 's1', turn_id: 'u1', platform: 'telegram' })]); + const described = registry.describe(hermes); + assert.equal(hooks.enabled(hermes), true, 'one profile reports, so the card offers Turn off'); + assert.equal(hooks.note(hermes), 'Agent reporting is on for Default, not for work. Turn it off, then on, to try again.'); + assert.equal(tokens.get(registry.accountsFor(hermes)[1].home), null, 'the profile that failed keeps no accepted token'); + assert.notEqual(tokens.get(reporting), null, 'the profile that turned on keeps its token'); + assert.deepEqual(described.accounts.map((a) => [a.id, counts(a.live)]), [['default', { working: 1, waiting: 0 }], ['work', null]]); +}); + +test('each Hermes home has its own token and live state, two cards on one home share them, and a home no card has loses its token', { timeout: 30000 }, async (t) => { + const { registry, tokens, hooks, home, token } = hermesSetup(t, { hermes: 'one', second: 'two', twin: 'one' }); + const live = new HermesLive({ sweepMs: 0 }); + registry.liveFor = (provider, account) => live.live(account.home); + for (const id of ['hermes', 'second', 'twin']) assert.equal(await hooks.setEnabled(registry.get(id), true), true); + const shown = (id) => registry.describe(registry.get(id)).accounts[0].live; + + assert.notEqual(token('hermes'), token('second')); + assert.equal(token('twin'), token('hermes')); + assert.equal(tokens.account(token('hermes')), home('hermes').home); + assert.equal(tokens.account(token('second')), home('second').home); + + live.receive(tokens.account(token('hermes')), 'gateway', [event(1, 'pre_llm_call', { session_id: 's1', platform: 'cli' })]); + assert.deepEqual([shown('hermes'), shown('twin'), shown('second')].map(counts), [{ working: 1, waiting: 0 }, { working: 1, waiting: 0 }, null]); + + assert.equal(await hooks.setEnabled(registry.get('hermes'), false), false); + assert.equal(hooks.enabled(registry.get('second')), true, 'the other home keeps reporting'); + assert.equal(tokens.account(token('second')), home('second').home); + + const kept = token('second'); + await tokens.ensure('/a/home/no/card/has'); + assert.equal(await tokens.prune(new Set(['hermes', 'second', 'twin'].map((id) => home(id).home))), true); + assert.equal(tokens.get('/a/home/no/card/has'), null); + assert.equal(tokens.account(kept), home('second').home); +}); + +test("on Windows the Hermes card owns the launcher in Hermes's own home, wherever LOCALAPPDATA puts it", (t) => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-install-')); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + const card = JSON.parse(fs.readFileSync(path.join(here, '..', 'config', 'providers.optional.json'), 'utf8')).providers.find((p) => p.id === 'hermes'); + const userFile = path.join(root, 'providers.json'); + fs.writeFileSync(userFile, JSON.stringify({ providers: [card] })); + const registry = new ProviderRegistry({ userFile, env: { PATH: '', USERPROFILE: 'C:\\Users\\ada', LOCALAPPDATA: 'D:\\Local' }, platform: 'win32', checkUpdates: false, accountsDir: path.join(root, 'accounts') }); + const hermes = registry.get('hermes'); + assert.equal(hermes.accounts[0].dir, 'D:\\Local\\hermes'); + assert.ok(hermes.channels.native.paths.includes('D:\\Local\\hermes\\bin')); +}); + +test('two sessions whose backend gives their tool calls the same id keep their own shells and approvals', { timeout: 30000 }, async (t) => { + const manager = await captureServer(t); + const { fire } = await startPlugin(t, { url: manager.url, token: 'report-token' }); + for (const sid of ['s1', 's2']) { + await fire('pre_llm_call', { session_id: sid, turn_id: `turn-${sid}`, platform: 'cli' }); + await fire('pre_tool_call', { session_id: sid, turn_id: `turn-${sid}`, tool_call_id: 'call_0', tool_name: 'terminal' }); + } + await fire('pre_approval_request', { session_id: 's2', turn_id: 'turn-s2', tool_call_id: 'call_0' }); + await fire('post_tool_call', { session_id: 's1', turn_id: 'turn-s1', tool_call_id: 'call_0', tool_name: 'terminal' }); + await until('the first session\'s call ended', () => sentEvents(manager.received), (events) => events.some((e) => e.event === 'post_tool_call')); + const open = await until('a heartbeat after it', () => lastOpen(manager.received), (last) => last && Object.keys(last.tools).length === 1); + const shown = (live) => ({ ...statuses(live), shells: live.view('default').shells.map((shell) => shell.agentId) }); + const expected = { 'hermes:s1': 'working', 'hermes:s2': 'waiting', shells: ['hermes:s2'] }; + + const fromEvents = new HermesLive({ sweepMs: 0 }); + fromEvents.receive('default', 'gateway', sentEvents(manager.received)); + assert.deepEqual(shown(fromEvents), expected); + const fromHeartbeat = new HermesLive({ sweepMs: 0 }); + fromHeartbeat.receive('default', 'gateway', [{ seq: 1, open }]); + assert.deepEqual(shown(fromHeartbeat), expected); +}); + +test('a session waiting on an approval comes back after its token and the manager\'s address change, from heartbeats alone', { timeout: 30000 }, async (t) => { + const first = await captureServer(t); + const moved = []; + const server = http.createServer((req, res) => { + let body = ''; + req.on('data', (chunk) => { body += chunk; }); + req.on('end', () => { + const ok = req.headers['x-agent-guild-report-token'] === 'token-b'; + if (ok) moved.push(JSON.parse(body)); + res.statusCode = ok ? 200 : 401; + res.end('{}'); + }); + }); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + t.after(() => server.close()); + const { fire, tokenFile, homeKey } = await startPlugin(t, { url: first.url, token: 'token-a', heartbeatSeconds: 0.2 }); + await fire('pre_llm_call', { session_id: 's1', turn_id: 'u1', platform: 'cli' }); + await fire('pre_approval_request', { session_id: 's1', turn_id: 'u1', tool_call_id: 't1' }); + await until('the wait reported to the first address', () => lastOpen(first.received), (open) => open && Object.keys(open.waits).length === 1); + + fs.writeFileSync(`${tokenFile}.next`, `token-b\n${homeKey}\nhttp://127.0.0.1:${server.address().port}\n`); + fs.renameSync(`${tokenFile}.next`, tokenFile); + const open = await until('a heartbeat at the new address with the new token', () => lastOpen(moved), Boolean); + const live = new HermesLive({ sweepMs: 0 }); + live.receive('default', 'gateway', [{ seq: 1, open }]); + assert.deepEqual(statuses(live), { 'hermes:s1': 'waiting' }); +}); + +test('stopping the manager ends a Hermes command still running, and waits for it', { timeout: 30000 }, async (t) => { + const { registry, hooks, env } = hermesSetup(t, { hermes: 'home' }); + const pidFile = path.join(registry.accountsFor(registry.get('hermes'))[0].dir, 'hang.pid'); + Object.assign(env, { FAKE_HERMES_HANG: 'plugins list', FAKE_HERMES_PID: pidFile }); + hooks.warm(); + const pid = Number(await until('the Hermes command running', () => (fs.existsSync(pidFile) ? fs.readFileSync(pidFile, 'utf8') : ''), Boolean)); + await hooks.close(); + assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }, 'the command is gone once close resolves'); +}); + +test("a token file Hermes is reading when the manager starts on another port: the manager starts, the card says reporting is off and why, and turning it on recovers", { skip: !win, timeout: 60000 }, async (t) => { + const { registry, tokens, hooks, home, env } = hermesSetup(t, { hermes: 'home' }); + const hermes = registry.get('hermes'); + assert.equal(await hooks.setEnabled(hermes, true), true); + const file = tokens.file(home('hermes').home); + const before = fs.readFileSync(file, 'utf8'); + + const readers = []; + try { + const { reader, lines } = await holdOpen(file, readers); + fs.writeFileSync(`${file}.probe`, 'x'); + assert.throws(() => fs.renameSync(`${file}.probe`, file), blocked, 'the reader really blocks the replacement'); + fs.rmSync(`${file}.probe`); + + env.TEST_API_URL = 'http://127.0.0.1:47999'; + await tokens.refresh(env.TEST_API_URL); + assert.equal(fs.readFileSync(file, 'utf8'), before, 'the file it could not replace is whole and unchanged'); + assert.equal(hooks.enabled(hermes), false); + assert.match(hooks.note(hermes), /could not give Hermes Agent its new address\. Turn it on to try again\./); + + reader.stdin.write('\n'); + assert.equal((await lines.next()).value, 'closed'); + } finally { + await stopReaders(readers); + } + assert.equal(await hooks.setEnabled(hermes, true), true); + assert.equal(fs.readFileSync(file, 'utf8').split('\n')[2], 'http://127.0.0.1:47999'); + assert.equal(hooks.note(hermes), null); +}); + +test('overlapping requests for one Hermes home all get the token that is stored, and a removal never takes a token made after it', async (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-tokens-')); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const tokens = new HermesTokens(dir); + const stored = () => fs.readFileSync(tokens.file('/home/a'), 'utf8').split('\n').slice(0, 3); + + const given = await Promise.all([tokens.ensure('/home/a', 'http://one'), tokens.ensure('/home/a', 'http://one'), tokens.ensure('/home/a', 'http://two')]); + assert.equal(new Set(given).size, 1, 'one token for the home'); + assert.deepEqual(stored(), [given[0], '/home/a', 'http://two']); + + const [, , remade] = await Promise.all([tokens.refresh('http://three'), tokens.remove('/home/a'), tokens.ensure('/home/a', 'http://four')]); + assert.notEqual(remade, given[0]); + assert.deepEqual(stored(), [remade, '/home/a', 'http://four'], 'the token made after the removal stays'); + assert.equal(tokens.get('/home/a'), remade); + assert.deepEqual(fs.readdirSync(dir).filter((name) => !name.endsWith('.token')), ['valid.json'], 'no temporary file is left'); +}); + +test('a Hermes home whose last card is removed while its reporting is being set up gets no token', async (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-prune-')); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const tokens = new HermesTokens(dir); + assert.equal(await tokens.prune(new Set(['/home/a'])), true); + + const setup = tokens.ensure('/home/a', 'http://one'); + const removed = tokens.prune(new Set()); + await assert.rejects(setup, /turned off meanwhile/); + assert.equal(await removed, true); + assert.equal(tokens.get('/home/a'), null); + assert.deepEqual(fs.readdirSync(dir), ['valid.json'], 'no token is left for the home'); + + const before = await (async () => { await tokens.prune(new Set(['/home/a'])); return tokens.ensure('/home/a', 'http://one'); })(); + assert.equal(tokens.account(before), '/home/a'); + const pruned = tokens.prune(new Set()); + assert.equal(tokens.account(before), null, 'refused at once, before the file is deleted'); + await assert.rejects(tokens.ensure('/home/a', 'http://one')); + assert.equal(await pruned, true); + assert.deepEqual(fs.readdirSync(dir), ['valid.json']); +}); + +test('a revoked Hermes token is refused at once even while its file cannot be deleted, and the file goes once it can', { skip: !win, timeout: 60000 }, async (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-revoke-')); + const readers = []; + t.after(async () => { + await stopReaders(readers); + fs.rmSync(dir, { recursive: true, force: true, maxRetries: 20, retryDelay: 50 }); + }); + const tokens = new HermesTokens(dir); + await tokens.prune(new Set(['/home/a', '/home/b'])); + const a = await tokens.ensure('/home/a', 'http://one'); + const b = await tokens.ensure('/home/b', 'http://one'); + const hold = async (file) => { + const { reader, lines } = await holdOpen(file, readers); + assert.throws(() => fs.rmSync(file), blocked, 'the reader really blocks the deletion'); + return async () => { reader.stdin.write('\n'); assert.equal((await lines.next()).value, 'closed'); }; + }; + + const releaseA = await hold(tokens.file('/home/a')); + const pruned = tokens.prune(new Set(['/home/b'])); + assert.equal(tokens.account(a), null, 'the last card gone: refused at once'); + assert.equal(await pruned, false, 'the cleanup is not done while the file is held'); + assert.equal(fs.existsSync(tokens.file('/home/a')), true); + assert.equal(tokens.account(a), null); + assert.equal(tokens.account(b), '/home/b'); + await releaseA(); + assert.equal(await tokens.prune(new Set(['/home/b'])), true, 'the next change deletes it'); + assert.equal(fs.existsSync(tokens.file('/home/a')), false); + + const releaseB = await hold(tokens.file('/home/b')); + await tokens.remove('/home/b'); + assert.equal(tokens.account(b), null, 'reporting turned off: refused though the file is held'); + assert.equal(tokens.get('/home/b'), null); + assert.equal(fs.existsSync(tokens.file('/home/b')), true, 'the file is still held'); + assert.equal(new HermesTokens(dir).account(b), null, 'and stays refused after a restart'); + await releaseB(); + const again = await tokens.ensure('/home/b', 'http://one'); + assert.notEqual(again, b, 'turning it on again makes a new token'); + assert.equal(tokens.account(again), '/home/b'); + assert.equal(tokens.account(b), null); +}); + +test('a revoked Hermes token stays refused after a restart, even when its file is left behind', async (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-restart-')); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const tokens = new HermesTokens(dir); + const kept = await tokens.ensure('/home/a', 'http://one'); + const revoked = await tokens.ensure('/home/b', 'http://one'); + const leftBehind = fs.readFileSync(tokens.file('/home/b'), 'utf8'); + assert.equal(await tokens.remove('/home/b'), true); + fs.writeFileSync(tokens.file('/home/b'), leftBehind, { mode: 0o600 }); + + const restarted = new HermesTokens(dir); + assert.equal(restarted.unreadable, null); + assert.equal(restarted.account(kept), '/home/a'); + assert.equal(restarted.account(revoked), null, 'a token file alone is never accepted'); + assert.equal(await restarted.prune(new Set(['/home/a', '/home/b'])), true); + assert.equal(fs.existsSync(restarted.file('/home/b')), false, 'the file left behind is deleted'); +}); + +test('an unreadable Hermes token record accepts no token and says so, unlike one with no token in it', async (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-corrupt-')); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const tokens = new HermesTokens(dir); + const token = await tokens.ensure('/home/a', 'http://one'); + assert.equal(new HermesTokens(path.join(dir, 'none-yet')).unreadable, null, 'no record yet is an empty one'); + + fs.writeFileSync(tokens.recordFile, '{}'); + assert.equal(new HermesTokens(dir).unreadable, null, 'an empty record is not reported'); + assert.equal(new HermesTokens(dir).account(token), null); + + for (const text of ['{"/home/a": "', '["/home/a"]', 'null']) { + fs.writeFileSync(tokens.recordFile, text); + const restarted = new HermesTokens(dir); + assert.equal(restarted.unreadable, 'it is not a valid record', text); + assert.equal(restarted.account(token), null, `${text}: the token file is not trusted instead`); + assert.equal(restarted.get('/home/a'), null); + } + + const restarted = new HermesTokens(dir); + const made = await restarted.ensure('/home/a', 'http://one'); + assert.equal(restarted.unreadable, null, 'a saved change replaces the unreadable record'); + assert.notEqual(made, token); + assert.equal(new HermesTokens(dir).account(made), '/home/a'); +}); + +test('a Hermes removal asked after an enable wins, whichever finishes its file work last', async (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-order-')); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const tokens = new HermesTokens(dir); + const enable = tokens.ensure('/home/a', 'http://one'); + const token = tokens.get('/home/a'); + assert.ok(token, 'the record has the token at once'); + const removal = tokens.remove('/home/a'); + assert.equal(tokens.account(token), null, 'refused as soon as the removal is asked'); + await assert.rejects(enable, /turned off meanwhile/); + assert.equal(await removal, true); + assert.equal(tokens.account(token), null); + assert.equal(fs.existsSync(tokens.file('/home/a')), false); + assert.equal(new HermesTokens(dir).account(token), null, 'and after a restart'); + + // Refreshes and enables queued around a removal never bring its token back. + const again = await tokens.ensure('/home/a', 'http://one'); + await Promise.all([tokens.ensure('/home/a', 'http://two'), tokens.remove('/home/a'), tokens.refresh('http://three')].map((p) => p.catch(() => {}))); + assert.equal(tokens.account(again), null); + assert.equal(new HermesTokens(dir).account(again), null); +}); + +test('when the Hermes token record cannot be saved, no change counts and nothing reports success', async (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-unsaved-')); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const tokens = new HermesTokens(dir); + await tokens.prune(new Set(['/home/a', '/home/b'])); + const a = await tokens.ensure('/home/a', 'http://one'); + fs.rmSync(tokens.recordFile); + fs.mkdirSync(tokens.recordFile); + + await assert.rejects(tokens.ensure('/home/b', 'http://one')); + assert.equal(tokens.get('/home/b'), null, 'no token is made'); + assert.equal(fs.existsSync(tokens.file('/home/b')), false); + await assert.rejects(tokens.remove('/home/a'), 'a removal that is not saved fails'); + assert.equal(tokens.get('/home/a'), a, 'and changes nothing'); + await assert.rejects(tokens.prune(new Set(['/home/b']))); + assert.equal(tokens.account(a), null, 'a home no card has is refused all the same'); + assert.deepEqual(fs.readdirSync(dir).filter((name) => name.endsWith('.tmp')), [], 'no temporary file is left'); +}); + +test('a Hermes home added back while its cleanup is still running can be turned on at once', async (t) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-readd-')); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const tokens = new HermesTokens(dir); + await tokens.prune(new Set(['/home/a'])); + const old = await tokens.ensure('/home/a', 'http://one'); + const cleanup = tokens.prune(new Set()); + const readded = tokens.prune(new Set(['/home/a'])); + const token = await tokens.ensure('/home/a', 'http://one'); + await Promise.all([cleanup, readded]); + assert.notEqual(token, old); + assert.equal(tokens.account(token), '/home/a'); + assert.equal(tokens.account(old), null); + assert.equal(fs.readFileSync(tokens.file('/home/a'), 'utf8').split('\n')[0], token, 'the new token file is not deleted by the cleanup'); +}); + +test('a failed Hermes setup never revokes the token that a request for the same home made after it set up', { timeout: 30000 }, async (t) => { + const { registry, tokens, hooks, home } = hermesSetup(t, { hermes: 'one', twin: 'one' }); + const failing = registry.get('hermes'); + const twin = registry.get('twin'); + const shared = home('hermes').home; + assert.equal(home('twin').home, shared, 'two cards on one home'); + failing.env.FAKE_HERMES_FAIL = 'plugins install'; + + // A asks for the home first and starts B as it does; A's failing install then waits until B has asked for the home too. + let later; + let release; + const bothAsked = new Promise((resolve) => { release = resolve; }); + const setup = tokens.setup.bind(tokens); + let asks = 0; + tokens.setup = (asked, work) => { + if (asked === shared && ++asks === 1) later = hooks.setEnabled(twin, true); + if (asked === shared && asks === 2) release(); + return setup(asked, work); + }; + const run = hooks._hermesRun.bind(hooks); + hooks._hermesRun = async (provider, account, args, what) => { + if (provider === failing && args[0] === 'plugins' && args[1] === 'install') await bothAsked; + return run(provider, account, args, what); + }; + + await assert.rejects(hooks.setEnabled(failing, true), /^Error: The Default profile failed: Hermes Agent could not install the Agent Guild plugin/); + assert.equal(await later, true, 'the later request turns reporting on'); + const token = tokens.get(shared); + assert.ok(token, 'and its token is kept'); + assert.equal(tokens.account(token), shared); + assert.equal(fs.readFileSync(tokens.file(shared), 'utf8').split('\n')[0], token); + assert.equal(hooks.enabled(twin), true); + assert.equal(new HermesTokens(tokens.dir).account(token), shared, 'also after a restart'); +}); + +const NEXT_COMMIT = 'fedcba9876543210fedcba9876543210fedcba98'; +/** The plugin each profile's Hermes records, as `plugins list --json` gives it. */ +const recorded = (registry, provider) => registry.accountsFor(provider).map((a) => { + try { return [a.id, JSON.parse(fs.readFileSync(path.join(a.dir, 'fake-plugins.json'), 'utf8'))[0]]; } catch { return [a.id, null]; } +}); +const installs = (log) => fs.readFileSync(log, 'utf8').trim().split('\n').map((line) => JSON.parse(line)).filter((argv) => argv[2] === 'plugins' && argv[3] === 'install'); + +test('turning on Hermes reporting installs the plugin at the commit this Agent Guild was released from, and nothing is left to update', { timeout: 30000 }, async (t) => { + const { registry, hooks, log } = hermesSetup(t, { hermes: 'home' }); + const hermes = registry.get('hermes'); + assert.equal(await hooks.setEnabled(hermes, true), true); + assert.deepEqual(installs(log), [['-p', 'default', 'plugins', 'install', 'oddessentials/agent-guild/plugins/hermes/agent-guild', '--ref', PLUGIN_COMMIT, '--enable']]); + assert.deepEqual(recorded(registry, hermes), [['default', { name: 'agent-guild', status: 'enabled', source: `git pinned@${PLUGIN_COMMIT.slice(0, 8)}` }]]); + assert.equal(hooks.updatable(hermes), false); + assert.equal(hooks.note(hermes), null); +}); + +test('a Hermes plugin of another commit keeps reporting and offers Update plugin; a failed update keeps it, and an update changes only the profiles that report', { timeout: 60000 }, async (t) => { + const { registry, tokens, env, log, root } = hermesSetup(t, { hermes: 'home' }); + makeHermesHome(registry.accountsFor(registry.get('hermes'))[0].dir, [['work', 'config.yaml']]); + registry.reload(); + const hermes = registry.get('hermes'); + const before = new SessionHooks({ registry, dir: path.join(root, 'reporting'), version: '1', hermesTokens: tokens, hermesPluginCommit: PLUGIN_COMMIT, getApiUrl: () => 'http://127.0.0.1:47821' }); + assert.equal(await before.setEnabled(hermes, true), true); + const [base, work] = registry.accountsFor(hermes); + const kept = tokens.get(base.home); + + // The next Agent Guild, released from another commit. + const hooks = new SessionHooks({ registry, dir: path.join(root, 'reporting'), version: '2', hermesTokens: tokens, hermesPluginCommit: NEXT_COMMIT, getApiUrl: () => 'http://127.0.0.1:47821' }); + t.after(() => hooks.close()); + await hooks._readHermes(hermes); + assert.equal(hooks.enabled(hermes), true, 'the old plugin still reports'); + assert.equal(hooks.updatable(hermes), true); + assert.equal(hooks.note(hermes), 'Agent reporting is on. The Agent Guild plugin of Default, work is not the one this Agent Guild installs; it keeps reporting until you update it.'); + assert.equal(installs(log).length, 2, 'starting changes no plugin'); + + // Reporting turned off for work by hand in Hermes: its plugin, of the old commit too, stays off. + fs.writeFileSync(path.join(work.dir, 'fake-plugins.json'), JSON.stringify([{ name: 'agent-guild', status: 'disabled', source: `git pinned@${PLUGIN_COMMIT.slice(0, 8)}` }])); + await hooks._readHermes(hermes); + assert.equal(hooks.updatable(hermes), true); + + env.FAKE_HERMES_FAIL = 'plugins install'; + await assert.rejects(hooks.update(hermes), (err) => { + assert.equal(err.message, `The Default profile was not updated: Hermes Agent could not update the Agent Guild plugin: it exited with code 1. Hermes still has the plugin it had, git pinned@${PLUGIN_COMMIT.slice(0, 8)}, and it is on.`); + return true; + }); + delete env.FAKE_HERMES_FAIL; + assert.equal(recorded(registry, hermes)[0][1].source, `git pinned@${PLUGIN_COMMIT.slice(0, 8)}`, 'Hermes kept the plugin it had'); + assert.equal(hooks.enabled(hermes), true); + assert.equal(tokens.get(base.home), kept, 'and its token'); + assert.equal(hooks.updatable(hermes), true); + + env.FAKE_HERMES_UNPINNED = '1'; + await assert.rejects(hooks.update(hermes), /does not record the Agent Guild plugin at commit fedcba987654 as on\. Hermes now has another plugin, oddessentials\/agent-guild\/plugins\/hermes\/agent-guild, and it is on\.$/); + delete env.FAKE_HERMES_UNPINNED; + assert.equal(hooks.updatable(hermes), true, 'an update Hermes does not record is not taken as done'); + + assert.equal(await hooks.update(hermes), true); + assert.deepEqual(installs(log).slice(-1), [['-p', 'default', 'plugins', 'install', 'oddessentials/agent-guild/plugins/hermes/agent-guild', '--ref', NEXT_COMMIT, '--force', '--enable']]); + assert.deepEqual(recorded(registry, hermes).map(([id, p]) => [id, p.status, p.source]), + [['default', 'enabled', `git pinned@${NEXT_COMMIT.slice(0, 8)}`], ['work', 'disabled', `git pinned@${PLUGIN_COMMIT.slice(0, 8)}`]], 'the profile that does not report is left alone'); + assert.equal(tokens.get(base.home), kept, 'updating keeps the token'); + assert.equal(hooks.updatable(hermes), false); + + assert.equal(await hooks.setEnabled(hermes, true), true); + assert.deepEqual(installs(log).slice(-1), [['-p', 'work', 'plugins', 'install', 'oddessentials/agent-guild/plugins/hermes/agent-guild', '--ref', NEXT_COMMIT, '--force', '--enable']], 'turning a profile on installs this commit over the old one'); + assert.equal(hooks.note(hermes), null); +}); + +test('a build that names no Hermes plugin commit installs none and says so, and still turns on a plugin Hermes already has', { timeout: 30000 }, async (t) => { + const { registry, hooks, tokens, log, home } = hermesSetup(t, { hermes: 'home' }, { pluginCommit: null }); + const hermes = registry.get('hermes'); + await hooks._readHermes(hermes); + assert.equal(hooks.note(hermes), 'Agent reporting is off: this build of Agent Guild names no commit of its Hermes plugin to install. Set AGENT_GUILD_HERMES_PLUGIN_COMMIT to one to install that plugin.'); + await assert.rejects(hooks.setEnabled(hermes, true), (err) => { + assert.match(err.message, /^The Default profile failed: This build of Agent Guild names no commit of its Hermes plugin to install\./); + assert.equal(err.code, 'reporting_unavailable'); + return true; + }); + assert.equal(fs.existsSync(log) ? installs(log).length : 0, 0, 'nothing is installed from GitHub'); + assert.equal(tokens.get(home('hermes').home), null); + await assert.rejects(hooks.update(hermes), { code: 'reporting_unavailable' }); + + fs.writeFileSync(path.join(home('hermes').dir, 'fake-plugins.json'), JSON.stringify([{ name: 'agent-guild', status: 'disabled', source: 'git pinned@00000000' }])); + assert.equal(await hooks.setEnabled(hermes, true), true, 'the plugin there is turned on'); + assert.equal(installs(log).length, 0); + assert.equal(hooks.updatable(hermes), false); + assert.equal(hooks.note(hermes), null); +}); + +/** The keys a profile's Hermes keeps with `config set`. */ +const configKeys = (account) => { try { return JSON.parse(fs.readFileSync(path.join(account.dir, 'fake-config.json'), 'utf8')); } catch { return {}; } }; + +test('turning off Hermes reporting whose setting was removed by hand still finishes and revokes the token', { timeout: 30000 }, async (t) => { + const { registry, hooks, tokens, home } = hermesSetup(t, { hermes: 'home' }); + const hermes = registry.get('hermes'); + assert.equal(await hooks.setEnabled(hermes, true), true); + const token = tokens.get(home('hermes').home); + assert.ok('AGENT_GUILD_HERMES_TOKEN_FILE' in configKeys(home('hermes'))); + fs.writeFileSync(path.join(home('hermes').dir, 'fake-config.json'), '{}'); + + assert.equal(await hooks.setEnabled(hermes, false), false, 'Hermes saying the setting is not set counts as removed'); + assert.equal(tokens.account(token), null); + assert.equal(hooks.note(hermes), null); +}); + +test('turning off Hermes reporting goes through every profile: each one is revoked though Hermes fails a step, and the error names each failure', { timeout: 30000 }, async (t) => { + const { registry, hooks, tokens, env, home } = hermesSetup(t, { hermes: 'home' }); + makeHermesHome(home('hermes').dir, [['work', 'config.yaml']]); + registry.reload(); + const hermes = registry.get('hermes'); + assert.equal(await hooks.setEnabled(hermes, true), true); + const [base, work] = registry.accountsFor(hermes); + const given = [tokens.get(base.home), tokens.get(work.home)]; + + // Default cannot turn its plugin off; work cannot remove its setting, for another reason than its being absent. + Object.assign(env, { FAKE_HERMES_FAIL: 'plugins disable', FAKE_HERMES_FAIL_PROFILE: 'default' }); + const disable = hooks.setEnabled(hermes, false); + await assert.rejects(disable, (err) => { + assert.equal(err.message, 'The Default profile failed: Hermes Agent could not turn off the Agent Guild plugin: it exited with code 1. Reporting is on for no profile.'); + return true; + }); + assert.deepEqual(given.map((token) => tokens.account(token)), [null, null], 'both tokens are revoked'); + assert.equal(recorded(registry, hermes)[1][1].status, 'disabled', 'the profile after the one that failed is turned off too'); + + assert.equal(await hooks.setEnabled(hermes, true), true); + Object.assign(env, { FAKE_HERMES_FAIL: 'config unset', FAKE_HERMES_FAIL_PROFILE: 'work' }); + await assert.rejects(hooks.setEnabled(hermes, false), (err) => { + assert.equal(err.message, "The work profile failed: Hermes Agent could not remove Agent Guild's reporting settings: it exited with code 1. Reporting is on for no profile."); + return true; + }); + assert.equal(tokens.get(work.home), null, 'a failed setting removal does not keep the token'); + assert.equal(tokens.get(base.home), null); + delete env.FAKE_HERMES_FAIL; + delete env.FAKE_HERMES_FAIL_PROFILE; + assert.equal(await hooks.setEnabled(hermes, false), false, 'and turning off again finishes'); +}); + +test('a Hermes token that cannot be revoked keeps the card from showing reporting off: Turn off stays, the note says why, and trying again revokes it', { timeout: 30000 }, async (t) => { + const { registry, hooks, tokens, home } = hermesSetup(t, { hermes: 'home' }); + makeHermesHome(home('hermes').dir, [['work', 'config.yaml']]); + registry.reload(); + const hermes = registry.get('hermes'); + assert.equal(await hooks.setEnabled(hermes, true), true); + const [base, work] = registry.accountsFor(hermes); + const given = [tokens.get(base.home), tokens.get(work.home)]; + const saved = fs.readFileSync(tokens.recordFile, 'utf8'); + fs.rmSync(tokens.recordFile); + fs.mkdirSync(tokens.recordFile); + + await assert.rejects(hooks.setEnabled(hermes, false), (err) => { + assert.match(err.message, /^The Default profile failed: Agent Guild could not revoke its report token \(\w+\)\. The work profile failed: Agent Guild could not revoke its report token \(\w+\)\. Reporting is on for no profile\. The report token of Default, work is still accepted; turn it off to try again\.$/); + return true; + }); + assert.deepEqual(given.map((token) => tokens.account(token)), [base.home, work.home], 'still accepted'); + assert.deepEqual(recorded(registry, hermes).map(([, p]) => p.status), ['disabled', 'disabled'], 'though Hermes turned the plugins off'); + assert.equal(hooks.enabled(hermes), true, 'so the card offers Turn off'); + assert.equal(hooks.note(hermes), 'Agent reporting is not fully off: the report token of Default, work is still accepted, because Agent Guild could not revoke it. Turn it off to try again.'); + + fs.rmdirSync(tokens.recordFile); + fs.writeFileSync(tokens.recordFile, saved); + assert.equal(await hooks.setEnabled(hermes, false), false, 'trying again finishes, the settings already removed'); + assert.deepEqual(given.map((token) => tokens.account(token)), [null, null]); + assert.equal(hooks.note(hermes), null); +}); + +test('two cards of one Hermes home show the same reporting and update state, whichever one changes it', { timeout: 30000 }, async (t) => { + const { registry, hooks, home } = hermesSetup(t, { hermes: 'one', twin: 'one' }); + const [hermes, twin] = [registry.get('hermes'), registry.get('twin')]; + await hooks._readHermes(hermes); + await hooks._readHermes(twin); + let updated = 0; + registry.on('updated', () => updated++); + + assert.equal(await hooks.setEnabled(hermes, true), true); + assert.equal(hooks.enabled(twin), true, 'the other card shows reporting on'); + assert.ok(updated > 0, 'and is described again'); + + fs.writeFileSync(path.join(home('hermes').dir, 'fake-plugins.json'), JSON.stringify([{ name: 'agent-guild', status: 'enabled', source: 'git pinned@00000000' }])); + await hooks._readHermes(twin); + assert.deepEqual([hooks.updatable(hermes), hooks.updatable(twin)], [true, true]); + assert.equal(await hooks.update(twin), true); + assert.deepEqual([hooks.updatable(hermes), hooks.updatable(twin)], [false, false]); + + assert.equal(await hooks.setEnabled(twin, false), false); + assert.deepEqual([hooks.enabled(hermes), hooks.enabled(twin)], [false, false]); +}); + +test('turning off Hermes reporting revokes every token even when Hermes cannot list a profile\'s plugins, and names that failure', { timeout: 30000 }, async (t) => { + const { registry, hooks, tokens, env, home } = hermesSetup(t, { hermes: 'home' }); + makeHermesHome(home('hermes').dir, [['work', 'config.yaml']]); + registry.reload(); + const hermes = registry.get('hermes'); + assert.equal(await hooks.setEnabled(hermes, true), true); + const [base, work] = registry.accountsFor(hermes); + const given = [tokens.get(base.home), tokens.get(work.home)]; + + Object.assign(env, { FAKE_HERMES_FAIL: 'plugins list', FAKE_HERMES_FAIL_PROFILE: 'work' }); + await assert.rejects(hooks.setEnabled(hermes, false), (err) => { + assert.equal(err.message, 'The work profile failed: Hermes Agent could not list its plugins: it exited with code 1. Reporting is on for no profile.'); + return true; + }); + assert.deepEqual(given.map((token) => tokens.account(token)), [null, null], 'both tokens are revoked'); + assert.deepEqual(recorded(registry, hermes).map(([, p]) => p.status), ['disabled', 'disabled'], 'and Hermes was still asked to turn the unlisted plugin off'); +}); + +test('a Hermes report whose token is revoked while its body is still arriving is refused, and restores no activity', { timeout: 30000 }, async (t) => { + const { registry } = hermesRegistry(t); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-inflight-')); + t.after(() => fs.rmSync(dir, { recursive: true, force: true })); + const tokens = new HermesTokens(path.join(dir, 'hermes-tokens')); + const work = registry.account(registry.get('hermes'), 'work').home; + const reportToken = await tokens.ensure(work); + const live = new HermesLive({ sweepMs: 0 }); + registry.liveFor = (provider, account) => live.live(account.home); + const manager = Object.assign(new EventEmitter(), { list: () => [], refreshMultiplexers() {}, sessions: new Map(), sessionHooks: {} }); + const api = createManagerServer({ manager, registry, token: 'f'.repeat(48), port: 0, webDir: path.join(here, '..', 'web'), hermes: { tokens, live } }); + await api.listen(); + t.after(() => api.close()); + + // The token is checked as the request arrives: hold the body until then, revoke, then send the rest. + let checked; + const atCheck = new Promise((resolve) => { checked = resolve; }); + const account = tokens.account.bind(tokens); + tokens.account = (token) => { const home = account(token); checked(); return home; }; + const body = JSON.stringify({ instance: 'gateway', messages: [{ seq: 1, event: 'pre_llm_call', data: { session_id: 's1', turn_id: 'u1', platform: 'telegram' } }] }); + const { port } = new URL(api.url); + const answered = new Promise((resolve, reject) => { + const req = http.request({ host: '127.0.0.1', port, path: '/api/v1/hermes/hook', method: 'POST', + headers: { 'content-type': 'application/json', 'content-length': Buffer.byteLength(body), 'x-agent-guild-report-token': reportToken } }, (res) => { res.resume(); res.on('end', () => resolve(res.statusCode)); }); + req.on('error', reject); + req.write(body.slice(0, 10)); + atCheck.then(() => tokens.remove(work)).then(() => req.end(body.slice(10)), reject); + }); + assert.equal(await answered, 401, 'refused: its token was revoked before it was applied'); + assert.equal(live.live(work), null, 'and no activity came back'); +}); + +test('a Hermes plugin read that finishes after a newer change is dropped, so it cannot show an enabled plugin as off', { timeout: 30000 }, async (t) => { + const { registry, hooks } = hermesSetup(t, { hermes: 'home' }); + const hermes = registry.get('hermes'); + // A background read takes Hermes's answer before reporting is turned on, and is delivered only after. + let release, taken; + const gate = new Promise((resolve) => { release = resolve; }); + const tookIt = new Promise((resolve) => { taken = resolve; }); + const run = hooks._hermesRun.bind(hooks); + let held = false; + hooks._hermesRun = async (provider, account, args, what) => { + const result = await run(provider, account, args, what); + if (!held && args[0] === 'plugins' && args[1] === 'list') { + held = true; + taken(); + await gate; + } + return result; + }; + const reading = hooks._readHermes(hermes); + await tookIt; + assert.equal(await hooks.setEnabled(hermes, true), true); + release(); + await reading; + assert.equal(hooks._hermesReporting(hermes)[0].on, true, 'the read from before is dropped'); + assert.equal(hooks.note(hermes), null, 'no false warning about a token still accepted'); +}); + +test('turning off Hermes reporting revokes every token even when Hermes is no longer on PATH, since a gateway still running may hold one', { timeout: 30000 }, async (t) => { + const { registry, hooks, tokens, home, root } = hermesSetup(t, { hermes: 'home' }); + makeHermesHome(home('hermes').dir, [['work', 'config.yaml']]); + registry.reload(); + const hermes = registry.get('hermes'); + assert.equal(await hooks.setEnabled(hermes, true), true); + const given = registry.accountsFor(hermes).map((account) => tokens.get(account.home)); + fs.rmSync(path.join(root, 'bin', win ? 'hermes.cmd' : 'hermes')); + + await assert.rejects(hooks.setEnabled(hermes, false), (err) => { + assert.equal(err.message, 'The Default profile failed: Hermes Agent is not installed. The work profile failed: Hermes Agent is not installed. Reporting is on for no profile.'); + return true; + }); + assert.deepEqual(given.map((token) => tokens.account(token)), [null, null], 'both tokens are revoked'); +}); + +test('when a Hermes plugin update fails and Hermes then cannot be asked what it has, the error says that is unknown and claims no plugin survived', { timeout: 30000 }, async (t) => { + const { registry, tokens, root } = hermesSetup(t, { hermes: 'home' }); + const hermes = registry.get('hermes'); + const before = new SessionHooks({ registry, dir: path.join(root, 'reporting'), version: '1', hermesTokens: tokens, hermesPluginCommit: PLUGIN_COMMIT, getApiUrl: () => 'http://127.0.0.1:47821' }); + assert.equal(await before.setEnabled(hermes, true), true); + const hooks = new SessionHooks({ registry, dir: path.join(root, 'reporting'), version: '2', hermesTokens: tokens, hermesPluginCommit: NEXT_COMMIT, getApiUrl: () => 'http://127.0.0.1:47821' }); + t.after(() => hooks.close()); + await hooks._readHermes(hermes); + assert.equal(hooks.updatable(hermes), true); + + // The install fails, and from then on Hermes cannot list its plugins either, as when its own cleanup leaves it stuck. + const run = hooks._hermesRun.bind(hooks); + let installed = false; + hooks._hermesRun = async (provider, account, args, what) => { + if (args[0] === 'plugins' && args[1] === 'install') { + installed = true; + throw Object.assign(new Error('Hermes Agent could not update the Agent Guild plugin: open() missing required argument \'flags\' (pos 2)'), { status: 502, code: 'reporting_setup_failed' }); + } + if (installed && args[0] === 'plugins' && args[1] === 'list') { + throw Object.assign(new Error('Hermes Agent could not list its plugins: cannot recover dependency publication'), { status: 502, code: 'reporting_setup_failed' }); + } + return run(provider, account, args, what); + }; + await assert.rejects(hooks.update(hermes), (err) => { + assert.equal(err.message, "The Default profile was not updated: Hermes Agent could not update the Agent Guild plugin: open() missing required argument 'flags' (pos 2). Agent Guild could not check which plugin Hermes has now, so whether it still has the one it had is unknown: Hermes Agent could not list its plugins: cannot recover dependency publication."); + assert.doesNotMatch(err.message, /still has the plugin it had|keeps reporting/); + return true; + }); +}); + +test('a Hermes profile whose token file was issued for another home, as a cloned profile has, reports nothing, nor does one whose home Hermes cannot name', { timeout: 30000 }, async (t) => { + const manager = await captureServer(t); + const source = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-guild-hermes-source-')); + t.after(() => fs.rmSync(source, { recursive: true, force: true })); + const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + + // A clone copies the source profile's setting, so its plugin reads the source's token file. + const clone = await startPlugin(t, { url: manager.url, token: 'source-token', recordedHome: hermesHomeKey(source), heartbeatSeconds: 0.2 }); + await clone.fire('on_session_start', { session_id: 's1', platform: 'cli' }); + await clone.fire('pre_llm_call', { session_id: 's1', turn_id: 'u1', platform: 'cli' }); + await pause(1500); + assert.deepEqual(manager.received, [], 'nothing is sent with the token of the profile it was cloned from'); + fs.writeFileSync(clone.tokenFile, `own-token\n${clone.homeKey}\n${manager.url}\n`); + await until('reports once its token was issued for its own home', () => manager.received.length, (n) => n > 0); + + const other = await captureServer(t); + const unnamed = await startPlugin(t, { url: other.url, token: 'some-token', home: 'broken', heartbeatSeconds: 0.2 }); + await unnamed.fire('on_session_start', { session_id: 's1', platform: 'cli' }); + await unnamed.fire('pre_llm_call', { session_id: 's1', turn_id: 'u1', platform: 'cli' }); + await pause(1500); + assert.deepEqual(other.received, [], 'a home Hermes cannot name reports nothing'); +}); diff --git a/tests/manager.test.mjs b/tests/manager.test.mjs index 408f92c..0766c19 100644 --- a/tests/manager.test.mjs +++ b/tests/manager.test.mjs @@ -1310,8 +1310,8 @@ test('stopping a tmux or herdr card ends only its client, so the multiplexer\'s test('a session runs under the account picked, in that account\'s own home folder', async () => { const { body: listed } = await call('GET', '/providers'); - assert.deepEqual(listed.providers.find((p) => p.id === 'multi').accounts, [{ id: 'default', label: 'Default' }, { id: 'work', label: 'Work' }, { id: 'kept', label: 'Kept' }]); - assert.deepEqual(listed.providers.find((p) => p.id === 'fake').accounts, [{ id: 'default', label: 'Default' }]); + assert.deepEqual(listed.providers.find((p) => p.id === 'multi').accounts, [{ id: 'default', label: 'Default', live: null }, { id: 'work', label: 'Work', live: null }, { id: 'kept', label: 'Kept', live: null }]); + assert.deepEqual(listed.providers.find((p) => p.id === 'fake').accounts, [{ id: 'default', label: 'Default', live: null }]); const workHome = path.join(home, 'accounts', 'multi', 'work'); assert.ok(!fs.existsSync(workHome), 'nothing is created before the first session'); @@ -1386,7 +1386,7 @@ test('accounts are added, renamed and removed through the API without touching t const renamed = await call('PATCH', '/providers/multi/accounts/side-project', { label: 'Side', revision: added.body.revision }); assert.equal(renamed.status, 200, JSON.stringify(renamed.body)); - assert.deepEqual(multi(renamed.body).accounts.at(-1), { id: 'side-project', label: 'Side' }); + assert.deepEqual(multi(renamed.body).accounts.at(-1), { id: 'side-project', label: 'Side', live: null }); const reused = await call('PATCH', '/providers/multi/accounts/side-project', { label: 'Again', revision: added.body.revision }); assert.deepEqual([reused.status, reused.body.error.code], [409, 'stale_accounts']); @@ -1670,6 +1670,8 @@ test('Antigravity CLI reports its model and conversation once its plugin is turn assert.equal(off.body.provider.reportingEnabled, false); assert.ok(!fs.existsSync(installed)); assert.equal((await call('POST', '/providers/anthropic/reporting', { enabled: true })).status, 400, 'nothing to turn on where hooks come with each session'); + const update = await call('POST', '/providers/google/reporting', { update: true }); + assert.deepEqual([update.status, update.body.error.code], [400, 'not_applicable'], 'only Hermes Agent has a plugin to update'); }); test('the Antigravity hook answers with an empty object and reports only into an Antigravity session', async () => { diff --git a/tests/mcp-connect.test.mjs b/tests/mcp-connect.test.mjs index 44fb6c8..6807285 100644 --- a/tests/mcp-connect.test.mjs +++ b/tests/mcp-connect.test.mjs @@ -22,7 +22,8 @@ const codingTool = path.join(here, 'fixtures', 'fake-coding-tool.mjs'); const systemDirs = win ? ['System32', ''].map((dir) => path.join(process.env.SystemRoot || 'C:\\Windows', dir)) : ['/usr/bin', '/bin']; -const TOOLS = { anthropic: 'claude', openai: 'codex', google: 'agy', xai: 'grok' }; +const TOOLS = { anthropic: 'claude', openai: 'codex', google: 'agy', xai: 'grok', hermes: 'hermes' }; +const HERMES = { vendor: 'Nous Research', tool: 'Hermes Agent', command: 'hermes', mcp: 'hermes', homeVar: 'HERMES_HOME' }; function writeScript(file, name) { if (win) fs.writeFileSync(`${file}.cmd`, `@echo off\r\n"${process.execPath}" "${codingTool}" ${name} %*\r\n`); @@ -39,15 +40,15 @@ function setup(t, { accounts = {}, extraEnv = {} } = {}) { const log = path.join(root, 'mcp.log'); const env = { PATH: [bin, ...systemDirs].join(path.delimiter), HOME: home, USERPROFILE: home, FAKE_MCP_LOG: log, - CLAUDE_CONFIG_DIR: path.join(root, 'claude'), CODEX_HOME: path.join(root, 'codex'), GROK_HOME: path.join(root, 'grok'), + CLAUDE_CONFIG_DIR: path.join(root, 'claude'), CODEX_HOME: path.join(root, 'codex'), GROK_HOME: path.join(root, 'grok'), HERMES_HOME: path.join(root, 'hermes'), ...(win && { SystemRoot: process.env.SystemRoot, ComSpec: process.env.ComSpec, PATHEXT: '.COM;.EXE;.BAT;.CMD' }), ...extraEnv, }; const userFile = path.join(root, 'providers.json'); fs.writeFileSync(userFile, JSON.stringify({ - providers: Object.keys(TOOLS).map((id) => ({ id, usage: null, history: null, accounts: (accounts[id] ?? []).map((a) => ({ id: a, label: a })) })), + providers: Object.keys(TOOLS).map((id) => ({ id, ...(id === 'hermes' && HERMES), usage: null, history: null, accounts: (accounts[id] ?? []).map((a) => ({ id: a, label: a })) })), })); - for (const dir of [home, env.CLAUDE_CONFIG_DIR, env.CODEX_HOME, env.GROK_HOME]) fs.mkdirSync(dir, { recursive: true }); + for (const dir of [home, env.CLAUDE_CONFIG_DIR, env.CODEX_HOME, env.GROK_HOME, env.HERMES_HOME]) fs.mkdirSync(dir, { recursive: true }); const registry = new ProviderRegistry({ userFile, env, platform: process.platform, checkUpdates: false, accountsDir: path.join(root, 'accounts') }); const manager = new SessionManager({ registry, baseEnv: {}, getApiUrl: () => '' }); // The folder an unpinned `agent-guild mcp` reaches from these tools' environment. @@ -57,7 +58,7 @@ function setup(t, { accounts = {}, extraEnv = {} } = {}) { const provider = (id) => registry.get(id); const homeOf = (id, account = 'default') => { if (account !== 'default') return path.join(root, 'accounts', id, account); - return { anthropic: env.CLAUDE_CONFIG_DIR, openai: env.CODEX_HOME, google: home, xai: env.GROK_HOME }[id]; + return { anthropic: env.CLAUDE_CONFIG_DIR, openai: env.CODEX_HOME, google: home, xai: env.GROK_HOME, hermes: env.HERMES_HOME }[id]; }; const calls = () => (fs.existsSync(log) ? fs.readFileSync(log, 'utf8').trim().split('\n').filter(Boolean).map((line) => JSON.parse(line)) : []); const clearCalls = () => fs.rmSync(log, { force: true }); @@ -93,7 +94,7 @@ function writeRaw(id, dir, text) { fs.writeFileSync(storeFile(id, dir), text); } -const changes = (ctx) => ctx.calls().filter((c) => c.argv[1] !== 'list' && c.argv[0] !== 'inspect'); +const changes = (ctx) => ctx.calls().filter((c) => c.argv[1] !== 'list' && c.argv[1] !== 'get' && c.argv[0] !== 'inspect'); test('every tool reads Agent Guild\'s server, one the person added, another server, and broken settings alike', async (t) => { const ctx = setup(t); @@ -161,6 +162,8 @@ test('missing, empty and broken settings read as each tool reads them', async (t await expect(id, '{"garbage":true}', 'error', `${id}, a list that is not JSON`); await expect(id, '{"fail":true}', 'error', `${id}, failing`); } + await expect('hermes', null, 'off', 'Hermes, missing'); + await expect('hermes', '{"fail":true}', 'error', 'Hermes, failing'); await expect('xai', '{"configError":true,"servers":{}}', 'error', 'Grok, a config.toml that does not parse'); assert.match((await ctx.read('xai')).detail, /ERROR config toml has syntax errors/); await expect('xai', '{"stderrNoise":true,"servers":{}}', 'off', 'Grok, an unrelated stderr line'); @@ -204,9 +207,11 @@ test('on Windows the entry\'s variable wins in any case, except under Claude Cod }); test('turning on and off runs each tool\'s own commands in the chosen account only', async (t) => { - const ctx = setup(t, { accounts: { anthropic: ['work'], openai: ['work'], xai: ['work'] } }); - const add = (tool) => (tool === 'claude' || tool === 'grok' ? ['mcp', 'add', '-s', 'user', SERVER_NAME, '--'] : ['mcp', 'add', SERVER_NAME, '--']).concat(ctx.own.command, ctx.own.args); - const remove = (tool) => (tool === 'claude' || tool === 'grok' ? ['mcp', 'remove', '-s', 'user', SERVER_NAME] : ['mcp', 'remove', SERVER_NAME]); + const ctx = setup(t, { accounts: { anthropic: ['work'], openai: ['work'], xai: ['work'], hermes: ['work'] } }); + const add = (tool) => (tool === 'hermes' ? ['config', 'set', `mcp_servers.${SERVER_NAME}`, JSON.stringify({ command: ctx.own.command, args: ctx.own.args, trust: 'untrusted', enabled: true })] + : (tool === 'claude' || tool === 'grok' ? ['mcp', 'add', '-s', 'user', SERVER_NAME, '--'] : ['mcp', 'add', SERVER_NAME, '--']).concat(ctx.own.command, ctx.own.args)); + const remove = (tool) => (tool === 'hermes' ? ['config', 'unset', `mcp_servers.${SERVER_NAME}`] + : tool === 'claude' || tool === 'grok' ? ['mcp', 'remove', '-s', 'user', SERVER_NAME] : ['mcp', 'remove', SERVER_NAME]); for (const [id, tool] of Object.entries(TOOLS)) { const account = id === 'google' ? 'default' : 'work'; const other = id === 'google' ? null : storeFile(id, ctx.homeOf(id)); @@ -223,7 +228,7 @@ test('turning on and off runs each tool\'s own commands in the chosen account on assert.deepEqual(changes(ctx), [{ tool, home: ctx.homeOf(id, account), argv: remove(tool) }]); if (other) assert.equal(fs.readFileSync(other, 'utf8'), before, `${tool}: the other account is untouched`); } - for (const [id, argv] of [['openai', add('codex')], ['google', ['mcp', 'enable', SERVER_NAME]], ['xai', ['mcp', 'enable', SERVER_NAME]]]) { + for (const [id, argv] of [['openai', add('codex')], ['google', ['mcp', 'enable', SERVER_NAME]], ['xai', ['mcp', 'enable', SERVER_NAME]], ['hermes', ['config', 'set', `mcp_servers.${SERVER_NAME}.enabled`, 'true']]]) { seed(id, ctx.homeOf(id), { ...ctx.own, enabled: false }); ctx.clearCalls(); assert.deepEqual(await ctx.change(id, 'on'), { outcome: 'changed', state: 'on' }, id); @@ -450,7 +455,7 @@ test('a manager with its own data folder registers that folder, so a tool starte test('an open page hears an MCP server removed outside the card, from the heartbeat', async (t) => { const ctx = setup(t); - for (const name of ['codex', 'agy', 'grok']) fs.rmSync(path.join(ctx.root, 'bin', win ? `${name}.cmd` : name)); + for (const name of ['codex', 'agy', 'grok', 'hermes']) fs.rmSync(path.join(ctx.root, 'bin', win ? `${name}.cmd` : name)); seed('anthropic', ctx.homeOf('anthropic'), ctx.own); assert.equal((await ctx.read('anthropic')).state, 'on'); diff --git a/tests/optional-cards.test.mjs b/tests/optional-cards.test.mjs index 26d9cf3..cbfca24 100644 --- a/tests/optional-cards.test.mjs +++ b/tests/optional-cards.test.mjs @@ -25,7 +25,7 @@ const refused = (fn, code) => assert.throws(fn, (err) => err.code === code); test('an optional card is added to providers.json and removed from it, keeping the other entries', (t) => { const fresh = setup(t); - assert.deepEqual(fresh.cards.list(), [{ id: shipped.id, vendor: shipped.vendor, tool: shipped.tool, added: false }]); + assert.deepEqual(fresh.cards.list(), JSON.parse(fs.readFileSync(OPTIONAL_FILE, 'utf8')).providers.map(({ id, vendor, tool }) => ({ id, vendor, tool, added: false }))); fresh.cards.add(shipped.id, { revision: null }); assert.deepEqual(JSON.parse(fresh.read()), { providers: [shipped] }); assert.equal(fresh.cards.list()[0].added, true); diff --git a/tests/package.test.mjs b/tests/package.test.mjs index 7d6845e..24d9862 100644 --- a/tests/package.test.mjs +++ b/tests/package.test.mjs @@ -117,6 +117,13 @@ test('a package with the listed files, the locked dependencies and an executable assert.deepEqual(checkPackage({ entries: goodPackage(), lock }), []); }); +test('a release package names the commit it was built from, whose Hermes plugin it installs', () => { + const commit = '0123456789abcdef0123456789abcdef01234567'; + assert.deepEqual(checkPackage({ entries: goodPackage({ manifest: { agentGuild: { commit } } }), lock, version: '1.2.3', commit }), []); + assert.deepEqual(checkPackage({ entries: goodPackage(), lock, version: '1.2.3', commit }), [`the package names commit none, not ${commit}`]); + assert.deepEqual(checkPackage({ entries: goodPackage({ manifest: { agentGuild: { commit: 'f'.repeat(40) } } }), lock, version: '1.2.3', commit }), [`the package names commit ${'f'.repeat(40)}, not ${commit}`]); +}); + test('the package check names every way a tarball can be wrong', () => { const problems = (options, version = '1.2.3') => checkPackage({ entries: goodPackage(options), lock, version }); diff --git a/tests/package/check-tarball.mjs b/tests/package/check-tarball.mjs index 0287fe9..251d787 100644 --- a/tests/package/check-tarball.mjs +++ b/tests/package/check-tarball.mjs @@ -108,7 +108,7 @@ export function bundledPackages(files) { return bundled; } -export function checkPackage({ entries, lock, version = null, upstream = null }) { +export function checkPackage({ entries, lock, version = null, commit = null, upstream = null }) { const problems = []; for (const entry of entries) { if (!entry.path.startsWith('package/')) problems.push(`${entry.path} is outside package/`); @@ -136,6 +136,8 @@ export function checkPackage({ entries, lock, version = null, upstream = null }) if (manifest.name !== name) problems.push(`the package is named ${manifest.name}, not ${name}`); if (manifest.private) problems.push('the package is marked private'); if (version !== null && manifest.version !== version) problems.push(`the package is version ${manifest.version}, not ${version}`); + // The commit a release was built from: the Hermes plugin it installs is that commit's. + if (commit !== null && manifest.agentGuild?.commit !== commit) problems.push(`the package names commit ${manifest.agentGuild?.commit ?? 'none'}, not ${commit}`); } const expected = new Map(Object.entries(lock.packages).filter(([key, pkg]) => key && !pkg.dev).map(([key, pkg]) => [key, pkg.version])); @@ -179,9 +181,11 @@ export function checkPackage({ entries, lock, version = null, upstream = null }) function main(argv) { let tarball = null; let version = null; + let commit = null; let upstreamDir = null; for (let i = 0; i < argv.length; i++) { if (argv[i] === '--version') version = argv[++i] ?? null; + else if (argv[i] === '--commit') commit = argv[++i] ?? null; else if (argv[i] === '--upstream') upstreamDir = argv[++i] ?? null; else if (argv[i] === '--windows-modules') { console.log(WINDOWS_MODULES.join('\n')); @@ -189,7 +193,7 @@ function main(argv) { } else tarball = argv[i]; } if (!tarball) { - console.error('Usage: node tests/package/check-tarball.mjs [--version X.Y.Z] [--upstream ] | --windows-modules'); + console.error('Usage: node tests/package/check-tarball.mjs [--version X.Y.Z] [--commit ] [--upstream ] | --windows-modules'); return 2; } const here = path.dirname(fileURLToPath(import.meta.url)); @@ -199,7 +203,7 @@ function main(argv) { const upstream = upstreamDir && new Map(WINDOWS_MODULES .filter((rel) => fs.existsSync(path.join(upstreamDir, rel))) .map((rel) => [rel, fs.readFileSync(path.join(upstreamDir, rel))])); - const problems = checkPackage({ entries, lock, version, upstream }); + const problems = checkPackage({ entries, lock, version, commit, upstream }); const mb = (bytes) => (bytes / 1024 / 1024).toFixed(1); if (packed > MAX_PACKED_BYTES) problems.push(`the package is ${mb(packed)} MB packed; the limit is ${mb(MAX_PACKED_BYTES)} MB`); for (const problem of problems) console.error(`not ok ${problem}`); diff --git a/tests/unit.test.mjs b/tests/unit.test.mjs index 9bdccbb..780102b 100644 --- a/tests/unit.test.mjs +++ b/tests/unit.test.mjs @@ -2075,8 +2075,8 @@ test('accounts get their own home folder and environment, the default keeps the assert.deepEqual(registry.account(anthropic), registry.accountsFor(anthropic)[0]); assert.deepEqual(registry.account(anthropic, 'work'), registry.accountsFor(anthropic)[1]); assert.throws(() => registry.account(anthropic, 'nope'), (err) => err.status === 404 && err.code === 'unknown_account'); - assert.deepEqual(registry.describe(anthropic).accounts, [{ id: 'default', label: 'Main' }, { id: 'work', label: 'Work' }, { id: 'personal', label: 'Personal' }, { id: 'shared', label: 'Shared' }]); - assert.deepEqual(registry.describe(registry.get('openai')).accounts, [{ id: 'default', label: 'Default' }]); + assert.deepEqual(registry.describe(anthropic).accounts, [{ id: 'default', label: 'Main', live: null }, { id: 'work', label: 'Work', live: null }, { id: 'personal', label: 'Personal', live: null }, { id: 'shared', label: 'Shared', live: null }]); + assert.deepEqual(registry.describe(registry.get('openai')).accounts, [{ id: 'default', label: 'Default', live: null }]); const relativeFile = path.join(dir, 'relative.json'); fs.writeFileSync(relativeFile, JSON.stringify({ providers: [{ id: 'anthropic', accounts: [{ id: 'two', dir: 'claude-two' }] }] })); diff --git a/web/app.js b/web/app.js index 75e5843..5d7215a 100644 --- a/web/app.js +++ b/web/app.js @@ -2294,6 +2294,12 @@ const REPORTING_SWITCH = { off: (p) => `Remove Agent Guild's hooks from ${p.tool}'s hooks folder. New sessions stop reporting their agents and commands.`, on: (p) => `Add Agent Guild's hooks to ${p.tool}'s hooks folder, so new sessions show their agents and commands and can be resumed. ${p.tool} runs them in every session; outside Agent Guild they do nothing.`, }, + hermes: { + what: 'Agent reporting', + off: (p) => `Turn off the Agent Guild plugin in every ${p.tool} profile. ${p.tool} sessions stop showing here.`, + on: (p) => `Install the Agent Guild plugin into every ${p.tool} profile with "${p.command} plugins install", so its sessions, subagents and approvals show on this card.`, + applies: 'Sessions already running show from their next turn.', + }, antigravity: { what: 'Agent reporting', off: (p) => `Remove the Agent Guild plugin from ${p.tool}. New sessions stop reporting their model.`, @@ -2301,28 +2307,83 @@ const REPORTING_SWITCH = { }, }; +const reportingBusy = new Map(); + +function repaintReporting(id) { + const provider = state.providers.find((p) => p.id === id); + if (!provider) return; + for (const card of document.querySelectorAll('.provider[data-id]')) if (card.dataset.id === id) renderReportingSetup(card, provider); +} + +function renderReportingLive(card, provider) { + const box = card.querySelector('.reporting-live'); + const shown = provider.available && provider.reporting === 'hermes' ? (provider.accounts || []).filter((a) => a.live) : []; + box.hidden = !shown.length; + if (box.hidden) return box.replaceChildren(); + const head = box.querySelector('.reporting-live-head') ?? Object.assign(document.createElement('span'), { className: 'reporting-live-label reporting-live-head' }); + head.textContent = `Running in ${provider.tool}, not in Agent Guild terminals. Shows sessions active since reporting started.`; + const groups = new Map([...box.querySelectorAll('.reporting-live-profile')].map((el) => [el.dataset.account, el])); + box.replaceChildren(head, ...shown.map((account) => { + let group = groups.get(account.id); + if (!group) { + group = Object.assign(document.createElement('div'), { className: 'reporting-live-profile' }); + group.dataset.account = account.id; + const label = Object.assign(document.createElement('span'), { className: 'reporting-live-label' }); + const agents = Object.assign(document.createElement('div'), { className: 'agents' }); + agents.dataset.empty = 'Nothing running now'; + group.append(label, agents); + } + group.querySelector('.reporting-live-label').textContent = `${account.label} profile: ${account.live.working} working, ${account.live.waiting} waiting`; + renderAgents(group.querySelector('.agents'), account.live.agents, account.live.shells); + return group; + })); +} + function renderReportingSetup(card, provider) { + renderReportingLive(card, provider); const row = card.querySelector('.reporting-row'); const note = provider.reportingNote; row.hidden = !provider.available || (typeof provider.reportingEnabled !== 'boolean' && !note); if (row.hidden) return; const words = REPORTING_SWITCH[provider.reporting] ?? REPORTING_SWITCH.antigravity; const on = provider.reportingEnabled; + const busy = reportingBusy.get(provider.id); const button = row.querySelector('.reporting-toggle'); - row.querySelector('.reporting-text').textContent = note || `${words.what} ${on ? 'on' : 'off'}`; - button.hidden = Boolean(note); + row.querySelector('.reporting-text').textContent = busy === 'update' ? 'Updating the Agent Guild plugin…' : busy ? `Turning ${busy}…` : note || `${words.what} ${on ? 'on' : 'off'}`; + button.hidden = Boolean(note) && provider.reporting !== 'hermes'; + button.disabled = Boolean(busy); button.textContent = on ? 'Turn off' : 'Turn on'; button.title = on ? words.off(provider) : words.on(provider); + const update = row.querySelector('.reporting-update'); + update.hidden = !provider.reportingUpdate; + update.disabled = Boolean(busy); + update.title = `Install the Agent Guild plugin of this version in ${provider.tool}; until it is installed the one there keeps reporting`; + update.onclick = async () => { + reportingBusy.set(provider.id, 'update'); + repaintReporting(provider.id); + try { + await api('POST', `/providers/${provider.id}/reporting`, { update: true }); + toast(`The Agent Guild plugin is updated for ${provider.tool}.`); + } catch (err) { + if (err instanceof AuthError) return showAuth(err.message); + toast(err.message, 10000); + } finally { + reportingBusy.delete(provider.id); + repaintReporting(provider.id); + } + }; button.onclick = async () => { - button.disabled = true; + reportingBusy.set(provider.id, on ? 'off' : 'on'); + repaintReporting(provider.id); try { await api('POST', `/providers/${provider.id}/reporting`, { enabled: !on }); - toast(on ? `${words.what} is off for ${provider.tool}.` : `${words.what} is on for ${provider.tool}. It applies to new sessions.`); + toast(on ? `${words.what} is off for ${provider.tool}.` : `${words.what} is on for ${provider.tool}. ${words.applies ?? 'It applies to new sessions.'}`); } catch (err) { if (err instanceof AuthError) return showAuth(err.message); toast(err.message, 10000); } finally { - button.disabled = false; + reportingBusy.delete(provider.id); + repaintReporting(provider.id); } }; } diff --git a/web/icons/hermes.svg b/web/icons/hermes.svg new file mode 100644 index 0000000..36f00c2 --- /dev/null +++ b/web/icons/hermes.svg @@ -0,0 +1 @@ + diff --git a/web/index.html b/web/index.html index ed1ee94..fe2f124 100644 --- a/web/index.html +++ b/web/index.html @@ -844,7 +844,9 @@

Choose working folder

+