@seaveyon/harness-switch 0.16.1 → 0.18.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/README.md CHANGED
@@ -6,24 +6,32 @@ English | [简体中文](https://github.com/B1anker/harness-switch/blob/main/REA
6
6
 
7
7
  **harness-switch** is a Bun-powered web control plane for managing API profiles on an SSH or headless server. It switches API Base URL, API key, and model profiles for **Claude Code**, **Codex**, **Kimi Code**, **Pi**, and **DeepSeek Harness (DSH)**.
8
8
 
9
- It is a configuration manager, not an API proxy: it does not route or inspect model traffic.
9
+ It writes each tool's native configuration; it is not an API proxy and never routes model traffic. Profiles are encrypted at rest, writes are transactional, and every activation has a backup and an undo receipt.
10
10
 
11
11
  ## Install and run
12
12
 
13
- Requires [Bun](https://bun.sh) >= 1.2.
13
+ Requires [Bun](https://bun.sh) >= 1.2 as the runtime. Start it with your preferred package runner:
14
14
 
15
15
  ```bash
16
- bunx @seaveyon/harness-switch
16
+ bunx @seaveyon/harness-switch@latest
17
+ npx -y @seaveyon/harness-switch@latest
18
+ pnpm dlx @seaveyon/harness-switch@latest
17
19
  ```
18
20
 
19
- Or install it globally:
21
+ `pnpx @seaveyon/harness-switch@latest` is also supported where `pnpx` is available. Or install it globally:
20
22
 
21
23
  ```bash
22
24
  bun add -g @seaveyon/harness-switch
25
+ # npm install -g @seaveyon/harness-switch
26
+ # pnpm add -g @seaveyon/harness-switch
23
27
  harness-switch
24
28
  ```
25
29
 
26
- The server listens on `127.0.0.1:8787` by default. The first startup prints a randomly generated Web password in the terminal.
30
+ The command starts a background daemon on `127.0.0.1:8787`. Read the generated Web password with:
31
+
32
+ ```bash
33
+ cat ~/.harness-switch/web_password
34
+ ```
27
35
 
28
36
  For a remote SSH server, keep the server bound to loopback and use an SSH tunnel:
29
37
 
@@ -31,7 +39,7 @@ For a remote SSH server, keep the server bound to loopback and use an SSH tunnel
31
39
  ssh -N -L 8787:127.0.0.1:8787 user@your-server
32
40
  ```
33
41
 
34
- Then open <http://127.0.0.1:8787> in your local browser.
42
+ Then open <http://127.0.0.1:8787> locally. Use `harness-switch status`, `stop`, or `server` to inspect, stop, or run the service in the foreground.
35
43
 
36
44
  ### Interface language
37
45
 
@@ -80,10 +88,39 @@ Any field the form does not expose can be set by editing the raw file content in
80
88
 
81
89
  Before every write, the previous content of each target file is snapshotted into `~/.harness-switch/backups/`, keeping the most recent 10. The backup panel restores a snapshot verbatim, comments included. Snapshots never land next to the live file, because tools like Claude Code scan their own config directory.
82
90
 
83
- A write either lands completely or not at all: content is validated first, and a failure part way through restores the previous state, deleting files that did not exist before rather than leaving them empty.
91
+ A write either lands completely or not at all: content is validated first, and a failure part way through restores the previous state, deleting files that did not exist before rather than leaving them empty. A restore carries the same guarantee and snapshots the live files it is about to overwrite, so the restore itself can be undone.
92
+
93
+ Backup directories live in the managed user's own home, so on a root-run service that account can rewrite its own manifests. Nothing in a manifest is therefore treated as a destination: it records the harness and the target key, the path is re-derived from that harness's adapter at restore time, and a payload name has to be a plain regular file inside the backup directory. Any path that resolves outside the user's manageable directories — a `~/.claude` symlinked elsewhere, for instance — is refused for both reads and writes.
84
94
 
85
95
  Switching away from a profile first reads the live file back into that profile's record, so edits you made directly in the CLI tool are not lost on the next switch.
86
96
 
97
+ ### Operation receipts and undo
98
+
99
+ An in-process `try/catch` only covers a thrown error. A power cut, SIGKILL, OOM or an upgrade restart never gives it the chance to run, and the native config, the profile store and the recorded active profile can be left describing three different states.
100
+
101
+ So before any native file is written, a record lands in `~/.harness-switch/journal/` and advances with the operation:
102
+
103
+ ```
104
+ PREPARED → APPLYING → METADATA_COMMITTED → COMMITTED
105
+ ↘ ROLLED_BACK / DEGRADED
106
+ ```
107
+
108
+ On startup the service settles whatever it finds, for every account it may manage. The boundary that matters is `METADATA_COMMITTED`: by then everything the operation set out to change is already on disk, so it rolls forward to `COMMITTED` rather than undoing a switch the user saw succeed. Anything still at `APPLYING` may be half written and is rolled back whole. A rollback that itself fails is recorded as `DEGRADED` and surfaced in the UI for a human.
109
+
110
+ Writing `active.json` now happens inside the same transaction as the native files, so the config on disk and the record of what is live can no longer disagree. Each record doubles as a receipt: the files it changed, the backup id, the target user and where it got to. The **Operation log** in the top bar lists them and offers a one-click undo that puts the native files *and* `active.json` back together, rather than just restoring a few files. The undo takes its own snapshot first. Once a backup has been rotated away, its receipt is automatically marked as no longer undoable.
111
+
112
+ `HSW_JOURNAL_RETAIN` controls how many receipts are kept; the default is 50.
113
+
114
+ ### Importing configuration you already have
115
+
116
+ If you configured these tools by hand before installing harness-switch, **Import existing config** in the top bar adopts that setup instead of making you retype it.
117
+
118
+ The scan reads what the five tools currently have on disk and lists every provider it finds. The additive tools (Codex, Kimi Code, Pi, DeepSeek Harness) report one candidate per provider entry, with the one currently in use flagged; Claude Code holds a single routing, so it yields at most one. Credentials are not returned with the scan — the UI only ever sees a mask — and an import re-reads them from disk server-side, so the plaintext never passes through the browser.
119
+
120
+ Each candidate is decided on its own: store it as a standalone profile, or extract the credential into the Provider Vault and reference it. When the vault already holds the identical credential, the wizard offers to reuse that entry instead of creating a duplicate. A name collision is skipped unless you explicitly ask to overwrite. Some providers read their key from the environment instead of a file (Codex's `env_key` mode, for example); those have nothing on disk to import, so the wizard asks you to supply one.
121
+
122
+ **Neither the scan nor the import touches the tools' own config files.** An import writes only to harness-switch's own profile store and vault; making it take effect still requires an explicit activation. A config file that exists but cannot be parsed causes that tool to be skipped with a reason, rather than guessed at.
123
+
87
124
  ### Moving every profile to another machine
88
125
 
89
126
  Use **Import / Export** in the top bar to create one `.hsw-backup` file containing every Harness profile, API key, raw-file override, and current activation choice. The bundle is encrypted with a migration password you choose, so it does not depend on the source machine's `aes-256-gcm.key`.
@@ -136,24 +173,38 @@ Cross-user writes require access to the destination home. Managing both `root` a
136
173
 
137
174
  ### CLI automation
138
175
 
139
- The same business logic is available from the terminal without opening the browser or starting any listener: the CLI builds the same service graph in-process and reads/writes the same data directory, so it works even when the daemon is not running. Every command supports `--json` for scripting:
176
+ The CLI talks to the running local daemon and uses the same authenticated HTTP API as the Web UI. Start the daemon once before using API-backed commands. `help` and `version` work offline; data commands support `--json` (or `-j`) for scripting:
140
177
 
141
178
  ```bash
142
179
  harness-switch list # harnesses, active profile, profile counts
180
+ harness-switch profiles [claude] # secret-free profile list, optionally filtered
181
+ harness-switch create claude main \
182
+ --base-url https://api.example.com/v1 \
183
+ --model claude-sonnet-4-5 \
184
+ --api-key-env ANTHROPIC_TOKEN # safer than putting a key in shell history
143
185
  harness-switch providers # Provider Vault entries
144
186
  harness-switch doctor # read-only diagnostics
145
- harness-switch doctor --probe --harness claude
146
- harness-switch plan claude # drift inspection for a harness
187
+ harness-switch doctor --strict --harness claude # exit 1 if a check has error status
188
+ harness-switch plan claude main # exact native files activation would write
147
189
  harness-switch activate claude main --yes # activate a profile
190
+ harness-switch official claude --yes # return to the tool's built-in login
191
+ harness-switch delete claude old --yes # inactive profiles only
148
192
  harness-switch users # manageable local Unix users
149
193
  harness-switch list --user alice # inspect alice's independent store
150
194
  harness-switch activate codex main --user alice --yes
151
195
  harness-switch sync --from root --to alice # one-time copy; skips conflicts
152
196
  harness-switch sync --from root --to alice --overwrite
153
197
  harness-switch sync --from root --to alice --copy-codex-auth # explicitly copy Codex auth.json
198
+ harness-switch scan # providers the five tools already have on disk
199
+ harness-switch import codex:alpha --name main # save as a profile; tools untouched
200
+ harness-switch import claude:claude --vault # extract the credential into the vault
201
+ harness-switch operations # operation receipts, newest first
202
+ harness-switch undo <operation-id> # revert one complete operation
154
203
  ```
155
204
 
156
- `plan <harness>` prints the drift inspection of the active profile (expected vs. current content per file); without an active profile it reports `status: unknown`. `activate` prompts for confirmation on a TTY and requires `--yes` in non-interactive terminals (CI). JSON output mirrors the HTTP API response shapes (`HarnessesResponse`, `ProvidersResponse`, `DoctorResponse`, `DriftSummary`, `ActivateResponse`), so scripts can reuse the same field names. `HSW_DATA_DIR` and `HSW_HOME_DIR` override where it reads state (defaults: `~/.harness-switch` and `$HOME`).
205
+ `plan <harness> <profile>` includes exact rendered content and therefore may include an API key. Mutating commands prompt on a TTY and require `--yes` in non-interactive terminals (CI). Prefer `--api-key-env VAR` to `--api-key VALUE` so credentials do not enter shell history or the process list. Run `harness-switch help` for the complete command reference. `HSW_URL` selects a non-default local endpoint; `HSW_DATA_DIR` tells the CLI where to find `web_password`.
206
+
207
+ JSON failures retain the HTTP status and stable server error code when available. Each CLI invocation logs out its temporary API session before exiting, so repeated automation does not grow the persisted Web session table.
157
208
 
158
209
  ## Security
159
210
 
@@ -173,12 +224,14 @@ This protects against accidental plaintext disclosure in profile storage, but do
173
224
  |---|---:|---|
174
225
  | `HOST` | `127.0.0.1` | Bind address. Keep the default when using SSH tunnelling. |
175
226
  | `PORT` | `8787` | Listening port. |
227
+ | `HSW_URL` | `http://127.0.0.1:$PORT` | Base URL used by API-backed CLI commands. |
176
228
  | `HSW_DATA_DIR` | `~/.harness-switch` | Control-plane and service-owner data directory; other users use `.harness-switch` in their own home. |
177
229
  | `HSW_HOME_DIR` | `$HOME` | Service owner's home override, mainly for tests and containers. |
178
230
  | `HSW_USERS` | auto-discovered | Comma-separated allowlist such as `root,alice`. The service owner is always manageable. |
179
231
  | `HSW_UPDATE_CHECK` | `1` | Set to `0` to skip npm registry update checks. The local development command sets this automatically. |
180
232
  | `HSW_SESSION_TTL_HOURS` | `24` | How long a Web login stays valid. Sessions survive a service restart. |
181
233
  | `HSW_BACKUP_RETAIN` | `10` | Number of snapshots to keep. |
234
+ | `HSW_JOURNAL_RETAIN` | `50` | Number of operation receipts to keep. |
182
235
  | `HSW_PUBLIC_DIR` | auto | Optional override for the built frontend directory. |
183
236
 
184
237
  The tools' own overrides are honoured when locating their config: `CODEX_HOME`, `KIMI_CODE_HOME`, and `PI_CODING_AGENT_DIR`.
@@ -204,6 +257,7 @@ The UI is a React SPA. Authentication uses an HttpOnly `hsw_session` cookie.
204
257
  | `DELETE` | `/api/harnesses/:id/profiles/:name` | Delete; `409` for the active profile |
205
258
  | `GET` | `/api/harnesses/:id/profiles/:name/preview` | The exact content that would be written |
206
259
  | `POST` | `/api/harnesses/:id/profiles/:name/activate` | Write the native config, then commit the switch |
260
+ | `POST` | `/api/harnesses/:id/official/activate` | Return a supported harness to its built-in account login |
207
261
  | `GET` | `/api/backups` | Snapshots, newest first |
208
262
  | `POST` | `/api/backups/:id/restore` | Restore a snapshot verbatim |
209
263
  | `POST` | `/api/transfer/export` | Create a passphrase-encrypted portable bundle |
@@ -217,8 +271,15 @@ The UI is a React SPA. Authentication uses an HttpOnly `hsw_session` cookie.
217
271
  | `POST` | `/api/drift/:harnessId/reapply` | Rewrite live files from the active profile |
218
272
  | `POST` | `/api/drift/:harnessId/adopt` | Read live files back into the profile record |
219
273
  | `GET` | `/api/doctor` | Read-only diagnostics (`?probe=1` includes the MVP-disabled, non-network probe check) |
274
+ | `GET` | `/api/scan` | Providers the five tools already have on disk; read-only, credentials masked |
275
+ | `POST` | `/api/scan/import` | Save selected candidates as profiles or vault entries; the tools' own config is untouched |
276
+ | `GET` | `/api/operations` | Operation receipts, newest first |
277
+ | `GET` | `/api/operations/:id` | One receipt |
278
+ | `POST` | `/api/operations/:id/undo` | Put the native files and the active profile back to before that operation |
220
279
 
221
- ## Background daemon (bunx / npx)
280
+ Every POST/PATCH body is validated against a shared Zod schema (`packages/shared/src/schemas.ts`) that the server and the web client both build on. A malformed shape is rejected with a `400` naming the field *before* it reaches storage, instead of being persisted and only surfacing as a `500` on the next activation. Unknown fields are dropped rather than rejected, so an older client keeps working while nothing unrecognised enters the store.
281
+
282
+ ## Background daemon (bunx / npx / pnpm)
222
283
 
223
284
  The published CLI runs as a background daemon by default: the command returns
224
285
  immediately, the server keeps running after the terminal is closed, and
@@ -232,15 +293,17 @@ bunx @seaveyon/harness-switch@latest server # run in the foreground instead
232
293
  bunx @seaveyon/harness-switch@latest list # CLI automation (see above)
233
294
  ```
234
295
 
235
- `npx -y @seaveyon/harness-switch@latest` works the same way. Append `@latest`
236
- so `bunx`/`npx` fetch the newest release before running.
296
+ Replace `bunx` with `npx -y`, `pnpm dlx`, or `pnpx` as preferred. Append `@latest`
297
+ so the package runner fetches the newest release before running. Bun remains the runtime used by the installed executable.
237
298
 
238
- The daemon writes its pid to `~/.harness-switch/daemon.pid` and its log to
299
+ The daemon writes its PID, instance identity, and listening address to `~/.harness-switch/daemon.pid` and its log to
239
300
  `~/.harness-switch/daemon.log` (a fresh log per start). The first run creates
240
301
  the web password in `~/.harness-switch/web_password` and prints it to the log.
241
302
  When a daemon is already running, a new invocation stops it before starting the
242
303
  new process, so the port never conflicts and the newest code wins.
243
304
 
305
+ Startup reports success only after the new process answers `/healthz`. `status` returns a non-zero exit code when the recorded daemon is not healthy. Before `stop` or an update sends a signal, the recorded instance identity is verified so a stale, reused PID cannot terminate an unrelated process.
306
+
244
307
  The dashboard polls the npm registry and shows a one-click **update button**
245
308
  next to the version badge when a newer release exists; it runs the same
246
309
  `bunx <package>@latest` restart under the hood and reloads the page once the