@seaveyon/harness-switch 0.6.1 → 0.7.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
@@ -137,36 +137,36 @@ The UI is a React SPA. Authentication uses an HttpOnly `hsw_session` cookie.
137
137
  | `POST` | `/api/transfer/preview` | Decrypt and report profile counts and conflicts without writing |
138
138
  | `POST` | `/api/transfer/import` | Import with `skip` or `overwrite` conflict handling |
139
139
 
140
- ## systemd
140
+ ## Background daemon (bunx / npx)
141
141
 
142
- Create `/etc/systemd/system/harness-switch.service`:
142
+ The published CLI runs as a background daemon by default: the command returns
143
+ immediately, the server keeps running after the terminal is closed, and
144
+ re-running after a release restarts the daemon on the newest version.
143
145
 
144
- ```ini
145
- [Unit]
146
- Description=Harness Switch Web Control Plane
147
- After=network.target
146
+ ```bash
147
+ bunx @seaveyon/harness-switch@latest # start, or update + restart the daemon
148
+ bunx @seaveyon/harness-switch@latest status # pid, url, log path
149
+ bunx @seaveyon/harness-switch@latest stop # stop the daemon
150
+ bunx @seaveyon/harness-switch@latest server # run in the foreground instead
151
+ ```
152
+
153
+ `npx -y @seaveyon/harness-switch@latest` works the same way. Append `@latest`
154
+ so `bunx`/`npx` fetch the newest release before running.
155
+
156
+ The daemon writes its pid to `~/.harness-switch/daemon.pid` and its log to
157
+ `~/.harness-switch/daemon.log` (a fresh log per start). The first run creates
158
+ the web password in `~/.harness-switch/web_password` and prints it to the log.
159
+ When a daemon is already running, a new invocation stops it before starting the
160
+ new process, so the port never conflicts and the newest code wins.
161
+
162
+ Under systemd (or any supervisor), run the CLI in the foreground instead:
148
163
 
164
+ ```ini
149
165
  [Service]
150
166
  Type=simple
151
- User=ubuntu
152
- Environment=HOST=127.0.0.1
153
- Environment=PORT=8787
154
- ExecStart=/usr/bin/env harness-switch
167
+ ExecStart=/usr/local/bin/harness-switch server
155
168
  Restart=on-failure
156
169
  RestartSec=3
157
- NoNewPrivileges=true
158
- PrivateTmp=true
159
-
160
- [Install]
161
- WantedBy=multi-user.target
162
- ```
163
-
164
- Then run:
165
-
166
- ```bash
167
- sudo systemctl daemon-reload
168
- sudo systemctl enable --now harness-switch
169
- sudo journalctl -u harness-switch -f
170
170
  ```
171
171
 
172
172
  ## Development
@@ -206,7 +206,7 @@ bun run check
206
206
  bun run precommit
207
207
  ```
208
208
 
209
- Production build (frontend assets are copied into `apps/server/public`, then the CLI is bundled):
209
+ Production build (frontend assets are copied into `apps/server/public`, then the CLI is bundled). `bun run start` starts the daemon; use `bun run start:foreground` for the foreground server:
210
210
 
211
211
  ```bash
212
212
  bun run build
@@ -12791,18 +12791,159 @@ function createServices() {
12791
12791
  return new InstantiationService(collection);
12792
12792
  }
12793
12793
 
12794
+ // src/daemon.ts
12795
+ import { spawn } from "child_process";
12796
+ import { mkdirSync as mkdirSync3, openSync, readFileSync as readFileSync2, unlinkSync, writeFileSync as writeFileSync2 } from "fs";
12797
+ import { homedir as homedir2 } from "os";
12798
+ import { join as join10, resolve } from "path";
12799
+ var DEFAULT_HOST = "127.0.0.1";
12800
+ var DEFAULT_PORT = 8787;
12801
+ var STOP_TIMEOUT_MS = 5000;
12802
+ var START_GRACE_MS = 500;
12803
+ function daemonDataDir() {
12804
+ return process.env.HSW_DATA_DIR || join10(homedir2(), ".harness-switch");
12805
+ }
12806
+ function daemonPidFile() {
12807
+ return join10(daemonDataDir(), "daemon.pid");
12808
+ }
12809
+ function daemonLogFile() {
12810
+ return join10(daemonDataDir(), "daemon.log");
12811
+ }
12812
+ function readPid() {
12813
+ try {
12814
+ const raw2 = readFileSync2(daemonPidFile(), "utf8").trim();
12815
+ const pid = Number(raw2);
12816
+ return Number.isInteger(pid) && pid > 0 ? pid : null;
12817
+ } catch {
12818
+ return null;
12819
+ }
12820
+ }
12821
+ function isAlive(pid) {
12822
+ try {
12823
+ process.kill(pid, 0);
12824
+ return true;
12825
+ } catch {
12826
+ return false;
12827
+ }
12828
+ }
12829
+ async function waitForExit(pid, timeoutMs = STOP_TIMEOUT_MS) {
12830
+ const deadline = Date.now() + timeoutMs;
12831
+ while (Date.now() < deadline) {
12832
+ if (!isAlive(pid))
12833
+ return true;
12834
+ await Bun.sleep(100);
12835
+ }
12836
+ return !isAlive(pid);
12837
+ }
12838
+ async function killProcess(pid) {
12839
+ if (!isAlive(pid))
12840
+ return true;
12841
+ process.kill(pid, "SIGTERM");
12842
+ if (await waitForExit(pid))
12843
+ return true;
12844
+ process.kill(pid, "SIGKILL");
12845
+ return waitForExit(pid, 3000);
12846
+ }
12847
+ async function stopDaemon() {
12848
+ const pid = readPid();
12849
+ if (pid === null) {
12850
+ console.log("no harness-switch daemon pid file found");
12851
+ return;
12852
+ }
12853
+ if (!isAlive(pid)) {
12854
+ console.log(`daemon pid ${pid} is not running; removing stale pid file`);
12855
+ try {
12856
+ unlinkSync(daemonPidFile());
12857
+ } catch {}
12858
+ return;
12859
+ }
12860
+ console.log(`stopping harness-switch daemon (pid ${pid})...`);
12861
+ await killProcess(pid);
12862
+ try {
12863
+ unlinkSync(daemonPidFile());
12864
+ } catch {}
12865
+ console.log("stopped");
12866
+ }
12867
+ function printStatus() {
12868
+ const pid = readPid();
12869
+ const host = process.env.HOST || DEFAULT_HOST;
12870
+ const port = Number(process.env.PORT || DEFAULT_PORT);
12871
+ const running = pid !== null && isAlive(pid);
12872
+ console.log(`harness-switch daemon: ${running ? `running (pid ${pid})` : "not running"}`);
12873
+ console.log(` url: http://${host}:${port}`);
12874
+ console.log(` log: ${daemonLogFile()}`);
12875
+ console.log(` password: ${join10(daemonDataDir(), "web_password")}`);
12876
+ console.log(` data: ${daemonDataDir()}`);
12877
+ }
12878
+ async function daemonize() {
12879
+ const previous = readPid();
12880
+ if (previous !== null && isAlive(previous)) {
12881
+ console.log(`restarting harness-switch daemon (previous pid ${previous})...`);
12882
+ await killProcess(previous);
12883
+ }
12884
+ mkdirSync3(daemonDataDir(), { recursive: true, mode: 448 });
12885
+ const entry = resolve(process.argv[1] ?? "harness-switch.js");
12886
+ const logPath = daemonLogFile();
12887
+ const logFd = openSync(logPath, "w");
12888
+ const child = spawn(process.execPath, [entry, "server"], {
12889
+ detached: true,
12890
+ stdio: ["ignore", logFd, logFd],
12891
+ env: process.env
12892
+ });
12893
+ child.unref();
12894
+ await Bun.sleep(START_GRACE_MS);
12895
+ if (child.exitCode !== null || child.signalCode !== null) {
12896
+ console.error(`daemon exited immediately (code ${child.exitCode}); tail of ${logPath}:`);
12897
+ try {
12898
+ const tail = (await Bun.file(logPath).text()).split(`
12899
+ `).slice(-15).join(`
12900
+ `);
12901
+ console.error(tail);
12902
+ } catch {}
12903
+ process.exit(1);
12904
+ }
12905
+ writeFileSync2(daemonPidFile(), `${child.pid}
12906
+ `, { mode: 384 });
12907
+ const host = process.env.HOST || DEFAULT_HOST;
12908
+ const port = Number(process.env.PORT || DEFAULT_PORT);
12909
+ console.log(`harness-switch daemon started (pid ${child.pid})`);
12910
+ console.log(` url: http://${host}:${port}`);
12911
+ console.log(` log: ${logPath}`);
12912
+ console.log(" manage: harness-switch status | harness-switch stop");
12913
+ console.log(` password (first run): ${join10(daemonDataDir(), "web_password")}`);
12914
+ }
12915
+ function usage() {
12916
+ return "usage: harness-switch [server|daemon|status|stop]";
12917
+ }
12918
+
12794
12919
  // src/main.ts
12795
- var services = createServices();
12796
- var environment = services.get(IEnvironmentService);
12797
- var log = services.get(ILogService);
12798
- var auth = services.get(IAuthService);
12799
- environment.ensureDataDir();
12800
- auth.ensurePassword();
12801
- var app = createApp(services);
12802
- var server = Bun.serve({
12803
- hostname: environment.host,
12804
- port: environment.port,
12805
- fetch: app.fetch
12806
- });
12807
- log.info(`data directory: ${environment.dataDir}`);
12808
- log.info(`listening on http://${server.hostname}:${server.port}`);
12920
+ async function runServer() {
12921
+ const services = createServices();
12922
+ const environment = services.get(IEnvironmentService);
12923
+ const log = services.get(ILogService);
12924
+ const auth = services.get(IAuthService);
12925
+ environment.ensureDataDir();
12926
+ auth.ensurePassword();
12927
+ const app = createApp(services);
12928
+ const server = Bun.serve({
12929
+ hostname: environment.host,
12930
+ port: environment.port,
12931
+ fetch: app.fetch
12932
+ });
12933
+ log.info(`data directory: ${environment.dataDir}`);
12934
+ log.info(`listening on http://${server.hostname}:${server.port}`);
12935
+ }
12936
+ var command = process.argv[2];
12937
+ if (command === "server") {
12938
+ await runServer();
12939
+ } else if (command === "stop") {
12940
+ await stopDaemon();
12941
+ } else if (command === "status") {
12942
+ printStatus();
12943
+ } else if (command === undefined || command === "daemon") {
12944
+ await daemonize();
12945
+ } else {
12946
+ console.error(`unknown command: ${command}`);
12947
+ console.error(usage());
12948
+ process.exit(1);
12949
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seaveyon/harness-switch",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "A secure web control plane that switches API providers for Claude Code, Codex, Kimi Code, Pi and DeepSeek Harness by writing their native config files on SSH servers.",
5
5
  "keywords": [
6
6
  "claude-code",
@@ -29,8 +29,9 @@
29
29
  "LICENSE"
30
30
  ],
31
31
  "scripts": {
32
- "dev": "bun --hot src/main.ts",
32
+ "dev": "bun --hot src/main.ts server",
33
33
  "start": "bun dist/harness-switch.js",
34
+ "start:foreground": "bun dist/harness-switch.js server",
34
35
  "build": "bun build src/main.ts --outfile dist/harness-switch.js --target bun && bun ../../scripts/postbuild.ts",
35
36
  "test": "bun test test"
36
37
  },