@staix/agent-hosts 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 STAIx Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,124 @@
1
- # Temporary Holding Version
1
+ # agent-hosts
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ `@staix/agent-hosts` resolves MCP configuration paths and previews, applies, or undoes one named server entry. It requires Bun 1.4.4 or later and has zero runtime dependencies.
4
+
5
+ Install an exact version after it is published.
6
+
7
+ ```sh
8
+ bun add --exact @staix/agent-hosts@0.2.0
9
+ ```
10
+
11
+ ## Supported hosts
12
+
13
+ | Host ID | Default MCP files | Format | Verified versions |
14
+ | --- | --- | --- | --- |
15
+ | `claude` | user `~/.claude.json` | JSON `mcpServers` | `2.1.332` |
16
+ | `codex` | user `~/.codex/config.toml` | TOML `mcp_servers` | `0.165.0`, `0.172.0` |
17
+ | `kimi` | user `~/.kimi-code/mcp.json` / project `.kimi-code/mcp.json` | JSON `mcpServers` | `2.7.2` |
18
+ | `pi` | user `~/.pi/agent/mcp.json` | JSON `mcpServers` | `1.6.0`, `1.6.1` |
19
+ | `qwen` | user `~/.qwen/settings.json` | JSON `mcpServers` | `0.26.1`, `0.27.1` |
20
+ | `jcode` | user `~/.jcode/mcp.json` | JSON `servers` (alias `mcpServers`) | `0.99.1`, `1.0.3` |
21
+ | `opencode` | user `~/.config/opencode/opencode.json` | JSON `mcp` | `1.18.35`, `1.19.5` |
22
+ | `gemini` | user `~/.gemini/settings.json` | JSON `mcpServers` | `0.61.0`, `0.67.0` |
23
+ | `agy` | project `.agents/mcp_config.json` | JSON `mcpServers` | `1.5.18` |
24
+
25
+ `HOSTS` records the file scopes, environment overrides, format, entry fields, and verified versions. `HostDefinition.entry` lists the minimum fields read by the host. Extra fields, including `env`, pass through. `CLAUDE_CONFIG_DIR` moves the Claude file into that directory as `.claude.json`. `QWEN_HOME` expands `~` against the injected home. A set but empty `JCODE_HOME` is refused. OpenCode and Gemini honor absolute `XDG_CONFIG_HOME` and `GEMINI_CLI_HOME` respectively, with an empty value treated as unset. Version evidence is available in [PR #5](https://github.com/STAIxBWLB/agent-hosts/pull/5), the [six-host verification receipt](docs/verification/2026-11-28-host-versions.md), and the [Antigravity 1.5.18 receipt](docs/verification/2026-11-27-agy-1.5.18.md).
26
+
27
+ Version 0.2.0 adds Antigravity support for the direct workspace file. On the verified version, `agy mcp list` listed the user-scope control and omitted both project files. Pass `scopes: ["project"]` and `workspaceRoot` to resolve the project file.
28
+
29
+ ## API
30
+
31
+ ### Resolve and edit an MCP entry
32
+
33
+ ```ts
34
+ import { applyEntry, planEntry, realPath, resolveHostPaths } from "@staix/agent-hosts";
35
+
36
+ const home = realPath("/path/to/home");
37
+ const paths = resolveHostPaths({ home, env: {}, hosts: ["codex"] });
38
+ const plan = planEntry({
39
+ host: "codex",
40
+ paths,
41
+ name: "example",
42
+ entry: { command: "/path/to/server", args: ["mcp"] },
43
+ });
44
+
45
+ if (plan.change === "refused") throw new Error(plan.reason);
46
+
47
+ // Inspect the plan before an explicit apply.
48
+ // applyEntry(plan, { receiptDir: `${home}/.example/setup` });
49
+ ```
50
+
51
+ - `realPath(path)` resolves an existing physical path and throws for a missing path or dangling link. It handles Bun's backslash and symlink-parent edge cases.
52
+ - `resolveHostPaths({ env, home, workspaceRoot?, hosts?, scopes?, redirectManagedProfiles? })` returns `home`, per-host `paths`, pre-redirect `legacyPaths`, `redirects`, per-host `refusals`, and per-host `roots.current` and `roots.legacy` for locating sibling files such as hooks. Pass the environment and home explicitly. It selects user files by default. Supply `scopes: ["project"]` and `workspaceRoot` for Kimi or Antigravity project files. Select exactly one file per host when calling the entry API. Managed Claude and Codex profiles redirect to the personal source by default. Set `redirectManagedProfiles: false` to keep the configured path. The resolver trims Claude, Codex, Kimi, and Pi overrides and omits files whose location override is invalid. Other hosts remain available.
53
+ - `planEntry({ host, paths, name, entry })` returns `add`, `replace`, `remove`, `unchanged`, or `refused` with a stable `code` and a human-readable `reason`. A successful preview contains the decoded `current` entry (or `undefined`), the before and after images, and an intent digest. Supply a plain JSON-compatible entry object or use `entry: null` to remove an entry with an undo receipt. Removing an absent entry is unchanged. Preview writes nothing.
54
+ - `applyEntry(plan, { receiptDir, env? })` rechecks file state and host policy under an exclusive lock, publishes the change, and records a versioned receipt. Use an absolute receipt directory. Generate a fresh plan if the file changed since preview. A matching entry with no receipt stays unclaimed. OpenCode requires fresh injected `env` on every apply. The plan carries routing-variable names without storing their values.
55
+ - `undoEntry({ host, paths, name, receiptDir })` restores the prior owned entry or removes the one it installed. A conflicting edit to the entry is left in place and the error names the file. The result identifies the config and receipt paths. If `legacy: true` is returned, the legacy receipt still exists and the consumer manages its cleanup. An unchanged undo carries a code from `UNDO_NOOP_CODES`. If someone deleted an installed entry, undo consumes its package receipt without restoring the prior entry. An explicit removal remains undoable.
56
+ - `readEntryReceipt({ host, paths, name, receiptDir })` reads the current or historical receipt without writing. It returns `file`, `receipt`, `intent`, `before`, `after`, and optional `pending` or `legacy`. Entry-only legacy formats yield synthesized MCP images because they store values rather than file snapshots.
57
+ - `REFUSAL_CODES` lists stable refusal identifiers used by both path and entry operations. Apply and undo throw `EntryRefusal` with a `code`, a human-readable `reason`, and `file` when a path is known. Refused plans also include `file` when known.
58
+ - `UNDO_NOOP_CODES` lists `no-receipt`, `entry-already-absent`, and `entry-already-restored` for unchanged undo results.
59
+
60
+ OpenCode preview and apply refuse nonblank `OPENCODE_CONFIG`, `OPENCODE_CONFIG_DIR`, or `OPENCODE_CONFIG_CONTENT` and sibling `opencode.jsonc` or `config.json` files. Undo remains available for the recorded global file. Jcode preview refuses `${` interpolation in the command or arguments.
61
+
62
+ Writes use bounded reads, refuse linked paths, and publish through a temporary file while preserving the config's mode. Configuration is capped at 8 MiB. The `<config>.agent-hosts.lock` file serializes package writers. Take any consumer-specific lock around package calls. If a stale lock blocks an operation, confirm that its writer has stopped before removing the named lock.
63
+
64
+ Package receipts use `entry-<digest([file, name])>.json` and carry version 1. A pending replacement can temporarily store its previous receipt at `<receipt>.previous-<digest>`. Consumers scanning the receipt directory should ignore these recovery files. Apply and undo return the receipt intent when one is known. Retry the same plan after interrupted publication. Undo can cancel a pending apply while the config still matches its start image. After publication, finish the apply before undoing.
65
+
66
+ ### Use the codecs directly
67
+
68
+ | Export | Behavior |
69
+ | --- | --- |
70
+ | `createJsonMcpCodec(options)` | Returns synchronous `read(image)`, `put(image, value, options?)`, and `restore(before, after, current, options?)`. Supply `canonicalKey`, `entryName`, `label`, and optional `aliases` or `preferCanonical`. The latter selects the canonical container when a legacy file holds both. Use `undefined` to remove the value. Pass `keepEmpty: true` to retain an empty container or `ownedOnly: true` to restore lexically even when the entire current image equals the installed image. |
71
+ | `SETUP_CONFIG_BYTES` | Configuration limit in bytes, 8 MiB. |
72
+ | `setTomlPath(text, path, value, options?)` | Edits a string-keyed TOML subtree. `undefined` removes it. `refuseInlineParent: true` refuses edits that would rewrite a containing inline value. |
73
+ | `updateTomlArray(text, path, expectedArray)` | Appends or removes elements while retaining surviving syntax. Unsupported layouts, reordering, replacements, or mixed removal and append are refused. |
74
+ | `insertTomlArrayItems(text, path, expectedArray)` | Inserts restored rows without deleting, reordering, or reserializing surviving elements. |
75
+ | `restoreRemovedItems(before, after, current, key)` | Computes the array with rows removed by the installer reinserted beside surviving anchors. |
76
+
77
+ The JSON codec accepts strict JSON and keeps unrelated bytes. The TOML codecs use Bun's TOML parser and refuse a layout they cannot edit safely. Callers that use the raw-text TOML API can add the filename when surfacing errors.
78
+
79
+ ### Exported types
80
+
81
+ | Type | Use |
82
+ | --- | --- |
83
+ | `HostId`, `HostScope` | Keys of `HOSTS` and the `user` or `project` scope. Pass a hub-owned path through the host's environment override. |
84
+ | `HostDefinition`, `HostConfig`, `HostFormat`, `HomeOverride` | Host metadata, per-file scope and path, JSON key or TOML table, and environment override rules. |
85
+ | `ResolveHostPathsOptions`, `HostPaths`, `HostRedirect`, `HostRefusal` | Resolver inputs, its path maps, managed-profile redirects, and path refusals. |
86
+ | `EntryTarget`, `PlanEntryOptions`, `ApplyEntryOptions` | Host, resolved paths, and name. Planning adds the desired `entry`. Apply takes a receipt directory and optional refreshed environment. |
87
+ | `EntryPlan`, `EntryResult`, `EntryReceipt` | Preview result, apply or undo result, and read-only receipt projection. |
88
+ | `RefusalCode`, `UndoNoopCode` | Machine-readable identifiers from `REFUSAL_CODES` and `UNDO_NOOP_CODES`. |
89
+ | `ConfigImage`, `JsonMcpCodecOptions` | Base64 `bytes` or `null` for an absent file, plus numeric `mode`, and the JSON codec's key and entry options. |
90
+
91
+ ## Consumer migration
92
+
93
+ For each migration, use one PR per consumer and pin `@staix/agent-hosts` to the exact release. Keep the existing entry name and pass the consumer's existing setup directory as `receiptDir` so undo can find earlier receipts. Replay the [copy fixtures](fixtures/copies/) and [legacy receipts](fixtures/receipts/), and list behavior changes in the consumer PR. [Fixture divergences](fixtures/DIVERGENCES.md)
94
+
95
+ | Consumer | Migration |
96
+ | --- | --- |
97
+ | vault-mem | Pass entry name `vmem` and `<vault-mem home>/setup`. Remove the migrated host table and writers in the same PR. Keep the CLI output and console manifest local. Move the Antigravity writer to this package in 0.2.0 using the workspace root guidance below. After a successful undo returning `legacy: true`, the consumer deletes the retained receipt. |
98
+ | agent-research | Pass `ares` and `<family home>/research/setup`. Remove the migrated host table and writers in the same PR. Keep the CLI output and console manifest local. Move the Antigravity writer to this package in 0.2.0 using the workspace root guidance below. After a successful undo returning `legacy: true`, the consumer deletes the retained receipt. |
99
+ | agent-mem | Migration deferred. Agent-mem keeps its own resolver, codecs, and `realPath`. Revisit when this package can be pinned under its writer attestation. [agent-mem #648](https://github.com/STAIxBWLB/agent-mem/issues/648) |
100
+ | agent-hub | Optionally adopt the path and format table after the other migrations if it removes duplicate code. Keep its project-scoped writers and adapter policy local. |
101
+
102
+ Vault-mem and agent-research gain Qwen tilde expansion and refuse an explicitly empty Jcode override. Both can disable managed-profile redirects to preserve their literal-path behavior. The package validates host overrides. When apply returns `legacy: true` and `supersedes`, the package receipt now owns the original baseline. Retire the superseded receipt after success. Agent-mem preserves any hook records it still needs before retiring a journal.
103
+
104
+ With 0.2.0, vault-mem and agent-research can drop their local Antigravity project writers. Select `hosts: ["agy"]` and `scopes: ["project"]` and supply the same `workspaceRoot` the previous writer used.
105
+
106
+ - When agent-mem revisits migration, pass the Git top-level directory normalized with `realPath`. Its legacy journal filename uses a digest of that root.
107
+ - Vault-mem and agent-research pass the same working directory their previous release used as the root. Their legacy receipt filenames hash the literal configuration file path.
108
+
109
+ A different root targets a different file. With respect to the original receipt, apply returns no `supersedes` and undo returns `no-receipt`.
110
+
111
+ Treat export and entry result shapes, host metadata, and the version 1 receipt layout and location as the shared 0.1.0 protocol. Branch on refusal codes. Human-readable reasons may change in a patch. A new host or scope widens a closed union and requires a minor release with a changelog note. Newly verified versions and optional fields can be added in a patch. Later 0.x releases continue to read earlier 0.x receipts.
112
+
113
+ ## Development and release
114
+
115
+ ```sh
116
+ bun install --frozen-lockfile
117
+ bun run typecheck
118
+ bun test
119
+ bun scripts/check-pack.ts
120
+ ```
121
+
122
+ CI runs tests with a temporary home. Tests inject fixture host paths and run offline. The npm tarball allowlist covers `src`, `fixtures`, `README.md`, `LICENSE`, and npm's required `package.json`.
123
+
124
+ The npm package owner registers `STAIxBWLB/agent-hosts` with workflow filename `release.yml` and enables direct publishing. Each tag requires the person's consent. The workflow publishes via npm OIDC after checking that the tag matches the manifest version and its commit belongs to `main`. [npm Trusted Publishing](https://docs.npmjs.com/trusted-publishers/)
@@ -0,0 +1,21 @@
1
+ # Fixture divergences
2
+
3
+ | Copy | Case | Consumer result | Package result | Reason |
4
+ | --- | --- | --- | --- | --- |
5
+ | agent-mem | Antigravity Git and directory ownership | Binds setup to a Git root, refuses tracked config, and tracks a created directory for cleanup | Resolves the supplied project root and owns only the MCP entry | Git checks and directory ownership stay with the consumer |
6
+ | vault-mem | Legacy receipt undo | Deletes the consumed v1 receipt | Retains the legacy file | Legacy receipts remain with the consumer |
7
+ | agent-research | Legacy receipt undo | Deletes the consumed v1 receipt | Retains the legacy file | Legacy receipts remain with the consumer |
8
+ | agent-mem | Legacy receipt undo | Restores MCP and hook changes, then removes the journal | Restores the MCP entry and retains the journal | The consumer owns hook state and history |
9
+ | vault-mem and agent-research | Prior entry included as structured data or TOML fragment | Can reconstruct syntax using the old writer, which may re-render the document | Restores the prior entry lexically, preserving unrelated live bytes. Prior entry formatting can differ | The v1 receipt does not contain the original configuration image |
10
+ | vault-mem | `qwen-tilde-home` | Refuses a non-absolute `QWEN_HOME` | Expands `~/custom-qwen` under the injected home and installs there | Adopt agent-mem's Qwen override rule |
11
+ | agent-research | `qwen-tilde-home` | Refuses a non-absolute `QWEN_HOME` | Expands `~/custom-qwen` under the injected home and installs there | Adopt agent-mem's Qwen override rule |
12
+ | vault-mem | `jcode-empty-home` | Treats the empty override as unset and installs in `$HOME/.jcode/mcp.json` | Refuses the explicitly set empty override with `empty-override` | Match the Jcode host and agent-mem setup guard |
13
+ | agent-research | `jcode-empty-home` | Treats the empty override as unset and installs in `$HOME/.jcode/mcp.json` | Refuses the explicitly set empty override with `empty-override` | Match the Jcode host and agent-mem setup guard |
14
+ | vault-mem | All standard hosts, applied rendering | Pretty-prints JSON entries or emits a TOML table for the owned entry | Emits the owned JSON value compactly or uses the TOML codec's editable entry form | Keep unrelated bytes and use the shared lexical codecs |
15
+ | agent-research | All standard hosts, applied rendering | Pretty-prints JSON entries or emits a TOML table for the owned entry | Emits the owned JSON value compactly or uses the TOML codec's editable entry form | Keep unrelated bytes and use the shared lexical codecs |
16
+ | agent-mem | `claude-standard`, `kimi-standard`, `pi-standard`, `qwen-standard`, `gemini-standard`, applied rendering | Re-renders the JSON document | Emits the owned JSON value compactly | Keep unrelated bytes and use the shared lexical codec |
17
+ | agent-mem | `claude-standard`, `codex-standard`, `kimi-standard`, `pi-standard`, `jcode-standard` | Adds a separate hook or extension file and restores it on undo | Leaves the separate file untouched | Hook and extension ownership stays with the consumer |
18
+ | agent-mem | `qwen-standard` | Adds a hook in the MCP settings file and restores it on undo | Changes only the MCP entry in the shared file | Hook ownership stays with the consumer |
19
+ | agent-mem | Relative Claude, Codex, Kimi, Pi, or Qwen overrides after applicable trimming or tilde expansion | Resolves against its working directory | Refuses with `relative-override` | The shared resolver requires an absolute directory for each host |
20
+ | agent-mem | OpenCode sibling config during undo | Refuses a merged sibling configuration | Undoes the owned entry in the recorded `opencode.json` and preserves the sibling | The spec permits undo after a routing or layout change |
21
+ | agent-mem | Padded absolute Claude, Codex, Kimi, or Pi override | Checks trimmed presence and resolves the literal value | Resolves the trimmed absolute directory | Surrounding whitespace is ignored for these host overrides |