@hasna/switcher 0.2.13 → 0.2.15

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.
@@ -0,0 +1,179 @@
1
+ # Persistent inference gateway
2
+
3
+ `switcher launch` gives the native harness a loopback base URL and a launch token
4
+ instead of the provider key. Before 0.2.14 that gateway lived inside the
5
+ `switcher launch` process and closed when the command exited. Claude Code starts
6
+ a detached background host (`claude daemon run`) and background sessions that
7
+ outlive the foreground session. They kept the old address and failed with
8
+ `ECONNREFUSED`, retrying until they gave up.
9
+
10
+ Since 0.2.14 every launch registers with one persistent gateway per user and
11
+ Switcher home. It listens on a stable `127.0.0.1` port and keeps a launch's token
12
+ working for as long as any process the launch started is still running.
13
+
14
+ ## What runs where
15
+
16
+ | Piece | Where | Lifetime |
17
+ | --- | --- | --- |
18
+ | Inference listener | `127.0.0.1:<port>`, default `47000 + (uid % 1000)` for `~/.hasna/switcher`, recorded in `gateway/config.json` | The service |
19
+ | Control socket | `~/.hasna/switcher/gateway/control.sock` (mode 0600 in a 0700 directory); long homes use `$XDG_RUNTIME_DIR/switcher/<hash>.sock` on Linux, otherwise `/tmp/switcher-<uid>/<hash>.sock`, in a private directory | The service |
20
+ | Launch records | `gateway/launches/<id>.json` (policy, catalog, token digest) and `<id>.holders.json` (holder PIDs and start times) | Until the launch expires |
21
+ | Service log | launchd: `~/Library/Logs/Hasna/switcher-gateway.log`; systemd: the user journal; detached: `gateway/service.log`. Lifecycle lines only. | Appended |
22
+
23
+ For the default home the service runs under the login supervisor and starts at
24
+ login: a LaunchAgent `com.hasna.switcher.gateway` on macOS, or the systemd user unit
25
+ `switcher-gateway.service` on Linux. Starting at boot without a login on Linux also
26
+ needs `loginctl enable-linger`. The first `switcher launch` of the installed
27
+ `switcher` (the one on `PATH`) installs and starts it. For other homes
28
+ (`HASNA_SWITCHER_HOME`, `HASNA_HOME`, tests), with `SWITCHER_GATEWAY_SUPERVISOR=none`,
29
+ or from another copy of Switcher such as a development checkout, `switcher launch`
30
+ starts a detached service that exits once no launch remains; `switcher gateway install`
31
+ installs the login unit on purpose.
32
+
33
+ The login unit runs its command at every login and after every crash, so Switcher
34
+ installs it only when each file it runs, its real path and all their parent
35
+ directories belong to the user or root and are not writable by group or others, and
36
+ nothing lies under a temporary directory. Otherwise the launch says why and uses a
37
+ detached gateway. The unit's environment is the same allowlist without `TMPDIR`.
38
+
39
+ ## Upgrades
40
+
41
+ A running gateway keeps its version until it stops. A launch that finds a gateway of
42
+ another version uses it and prints a warning naming `switcher gateway install`, which
43
+ stops the outdated gateway and starts the current one on the same port. Launches and
44
+ bound credentials survive that restart; a credential that came only from the launch
45
+ environment does not (see Restarts).
46
+
47
+ ```sh
48
+ switcher gateway status # JSON: running, pid, port, launches, unit; exit 1 when stopped
49
+ switcher gateway install # install or refresh the login unit and start it
50
+ switcher gateway stop # stop the service; the next launch starts it again
51
+ switcher gateway uninstall # stop it and remove the login unit
52
+ SWITCHER_GATEWAY=local switcher launch ... # use the per-launch gateway of 0.2.13
53
+ ```
54
+
55
+ ## Launch lifetime
56
+
57
+ 1. `switcher launch` resolves the provider credential as before and registers the
58
+ launch: the model policy, catalog and provider URL, plus the credential, which is
59
+ sent over the private socket and held only in service memory. The service returns
60
+ an unguessable 256-bit token for this launch and keeps only its SHA-256 digest.
61
+ 2. The service follows the launch by process identity (PID plus start time, so a
62
+ reused PID never counts). It begins with the `switcher launch` process, then adds
63
+ every process the launch starts, read from the process table about once a second
64
+ while a launch is new and every few seconds once it has settled. A detached
65
+ daemon remains a holder after its parent exits and it is reparented.
66
+ 3. When the harness exits, `switcher launch` releases the launch. A daemon started
67
+ by a double fork, or detached in the moment before its parent exited, was
68
+ reparented before any scan saw it under the launch. So at release (or when the
69
+ launcher dies without releasing) the service also checks the processes that were
70
+ reparented to init, launchd or a `systemd --user` subreaper since the launch
71
+ registered. One whose environment carries this launch's token becomes a holder.
72
+ Token-shaped values are compared by SHA-256 digest and never kept or logged.
73
+ 4. If nothing the launch started is still running, the token is revoked at once and
74
+ the launch's state directory is removed as before. Otherwise the service keeps
75
+ the token, the per-launch configuration files and the routing policy while any
76
+ holder runs. If the service cannot be reached at release (for example while
77
+ launchd restarts it), the launcher leaves the state directory to the service,
78
+ which removes it when the launch expires.
79
+ 5. When the last holder exits and the token has not been used for
80
+ `SWITCHER_GATEWAY_GRACE_SECONDS` (default 600), the launch expires: its record,
81
+ in-memory credential and retained state directory are removed.
82
+
83
+ Every request is dispatched by its token to that launch's own handler. One launch
84
+ cannot use another launch's credential, catalog, aliases or fallbacks.
85
+
86
+ ## Restarts
87
+
88
+ The listener port and the launch records survive a service restart, a crash under
89
+ the supervisor, an upgrade and a station restart. Holders are rechecked by PID and
90
+ start time. A surviving holder keeps working on the same address and token:
91
+
92
+ - A credential that came from a `switcher credentials bind` binding is read again
93
+ through that binding the first time it is needed. A binding that changed after the
94
+ launch is refused with `switcher_gateway_credential_changed`.
95
+ - A credential that came only from the launch environment (`SWITCHER_PROVIDER_*`) is
96
+ never written anywhere, so it cannot be recovered. Requests get HTTP 401
97
+ `switcher_gateway_credential_unavailable`, which names the binding command.
98
+
99
+ The grace period restarts when the service starts. After a station restart every
100
+ native process has stopped, so no holder survives, but a session that resumes with
101
+ its launch token within `SWITCHER_GATEWAY_GRACE_SECONDS` still reaches its launch;
102
+ after that it gets `switcher_gateway_launch_unknown`. It restarts once: a launch that
103
+ no process held and no request used since the last restart is not extended again,
104
+ so repeated service restarts never keep an idle token alive. The next `switcher launch`
105
+ registers with the restarted gateway on the same port, and a Claude Code daemon it
106
+ starts serves restored background sessions with that launch.
107
+
108
+ ## Errors instead of refused connections
109
+
110
+ While the service runs, a client always gets an HTTP answer it can show:
111
+
112
+ | Status | Code | Meaning and fix |
113
+ | --- | --- | --- |
114
+ | 401 | `switcher_gateway_launch_unknown` | The launch ended and expired. Start a new session with `switcher launch`. |
115
+ | 401 | `switcher_gateway_credential_unavailable` | The credential came only from the launch environment and the service restarted. Start a new session, or bind the credential. |
116
+ | 503 | `switcher_gateway_credential_unavailable` | The binding could not be read now. Check it with `switcher credentials check REFERENCE`. |
117
+ | 401 | `switcher_gateway_credential_changed` | The binding changed after the launch started. |
118
+ | 403 | `switcher_gateway_host_refused` | The request named a host other than `127.0.0.1:<port>`, `localhost:<port>` or `[::1]:<port>`. Use the base URL `switcher launch` set. |
119
+ | 502 | `provider_network_error`, `provider_request_failed` | The provider could not be reached; Switcher did not replay the request. |
120
+ | 504 | `provider_idle_timeout` | The provider stopped sending data. |
121
+
122
+ ## Security
123
+
124
+ - Only `127.0.0.1`, and only requests whose `Host` names this listener, so a web page
125
+ that rebinds its DNS name to `127.0.0.1` gets nothing, not even `/health`. The
126
+ service runs as the user, never as root.
127
+ - The control socket is owner-only. Before `switcher launch` sends a launch (and its
128
+ credential) or any other control request, it checks that the socket's directory is
129
+ a real directory owned by the user with mode 0700 and that the socket is owned by the
130
+ user. Otherwise it refuses with `gateway_socket_untrusted` and the launch uses a
131
+ per-launch gateway. This matters for the `/tmp` fallback, where another account could
132
+ create the directory first.
133
+ - Provider credentials and launch tokens are never written to disk, logs, unit
134
+ files or command lines. The service environment is an allowlist that excludes
135
+ `SWITCHER_PROVIDER_*` and API keys.
136
+ - Each launch has its own token, its own model policy and its own credential.
137
+ - The detached-process check reads the environment only of this user's processes
138
+ that were reparented after the launch registered, once per launch. It looks only
139
+ for a value whose SHA-256 digest equals the launch token's digest and keeps nothing.
140
+ macOS uses `ps -E`; Linux reads `/proc/<pid>/environ`.
141
+ - Control input is validated strictly. Policy maps use null-prototype objects, and
142
+ the service removes only `launch-XXXXXX` state directories owned by the user.
143
+
144
+ ## Limits
145
+
146
+ - Kilo, Gemini CLI, Grok Build, Hermes and Aider (through its Grok bridge) put a small
147
+ per-launch adapter in front of the gateway, and that adapter lives inside the
148
+ `switcher launch` process. A detached descendant of those harnesses loses it when
149
+ the launch ends, and so does the harness itself if the launcher is killed (for
150
+ example with SIGKILL) while the harness keeps running. Claude Code, Codex and the
151
+ other adapters talk to the gateway directly.
152
+ - Claude Code keeps one background host per configuration directory, and it serves
153
+ background sessions for later launches of the same profile too. Those sessions
154
+ use the launch that started the host: its token, provider, credential and model
155
+ policy, for as long as that host runs. A launch of a different profile normally uses
156
+ a different directory and its own host. If `CLAUDE_CONFIG_DIR` points several
157
+ profiles at one shared directory, they share one host too, so background sessions
158
+ can run with another profile's provider and model policy; give each profile its own
159
+ directory.
160
+ - A launch restored after a gateway restart that happened between its registration
161
+ and its release gets no detached-process check at release, so a double-forked
162
+ daemon no scan saw before the restart is not adopted.
163
+ - The default port is `47000 + uid % 1000`, so two accounts on one host whose uids
164
+ differ by 1000 collide; the second gets `gateway_port_in_use` and its launches use
165
+ per-launch gateways. Set `SWITCHER_GATEWAY_PORT` for one of them and run
166
+ `switcher gateway install`.
167
+ - On a host shared with other accounts, another account can bind the predictable port
168
+ while this user's gateway is down (a crash restart, an upgrade, the moments after
169
+ login). A new launch then detects the conflict and uses a per-launch gateway, but a
170
+ session that survived from before keeps sending its token and prompts to that
171
+ address. Stations with one interactive account are not affected.
172
+ - The detached-process check finds a daemon only if its environment carries the
173
+ launch token (Claude Code passes it in `ANTHROPIC_AUTH_TOKEN`) and it was
174
+ reparented to PID 1 or a `systemd --user` subreaper. Another subreaper hides it,
175
+ and such a daemon keeps the launch only while its parent lives.
176
+ - Claude Desktop and the Ori backend keep their own gateways.
177
+ - A Claude Code daemon started by Switcher 0.2.13 or older still carries its old
178
+ per-launch address. It recovers after that daemon restarts under a 0.2.14 launch.
179
+ - Windows uses the per-launch gateway.
@@ -4,7 +4,7 @@
4
4
  "name": "switcher",
5
5
  "class": "cli-with-store",
6
6
  "contractVersion": "v1",
7
- "kitVersion": "1.3.7",
7
+ "kitVersion": "1.3.8",
8
8
  "description": "Universal provider and model switcher with API-first local and hosted operation. CLI, MCP and SDK resolve one authenticated HTTP authority through @hasna/contracts and fail closed when hosted configuration is missing or invalid. HASNA_SWITCHER_LOCAL=1 deliberately selects an owned loopback API backed by SQLite; hosted https://api.hasna.com/switcher uses PostgreSQL and signed revocable API keys. Provider credentials remain local to the launcher and are never stored or returned by the service.",
9
9
  "bins": [
10
10
  "switcher",
package/openapi.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "openapi": "3.0.3",
3
3
  "info": {
4
4
  "title": "Switcher API",
5
- "version": "0.2.13",
5
+ "version": "0.2.15",
6
6
  "description": "Authenticated provider/profile/catalog control plane. Launches run locally; the API never returns provider credentials."
7
7
  },
8
8
  "security": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/switcher",
3
- "version": "0.2.13",
3
+ "version": "0.2.15",
4
4
  "type": "module",
5
5
  "description": "Universal provider and model switcher for coding harnesses with an API, SDK and self-hosted storage",
6
6
  "license": "Apache-2.0",
@@ -32,7 +32,8 @@
32
32
  "README.md",
33
33
  "LICENSE",
34
34
  "docs/MODEL-POLICY.md",
35
- "docs/CLAUDE-DESKTOP-GATEWAY.md"
35
+ "docs/CLAUDE-DESKTOP-GATEWAY.md",
36
+ "docs/GATEWAY-SERVICE.md"
36
37
  ],
37
38
  "publishConfig": {
38
39
  "registry": "https://registry.npmjs.org",
@@ -77,14 +78,17 @@
77
78
  "@types/pg": "8.23.1"
78
79
  },
79
80
  "dependencies": {
80
- "@hasna/contracts": "1.3.7",
81
- "@modelcontextprotocol/sdk": "^1.29.0",
81
+ "@hasna/contracts": "1.3.8",
82
+ "@modelcontextprotocol/sdk": "1.32.1",
82
83
  "jsonc-parser": "3.3.1",
83
84
  "yaml": "2.9.0",
84
85
  "zod": "3.25.76",
85
86
  "@aws-sdk/client-secrets-manager": "3.1127.0",
86
87
  "pg": "8.23.0"
87
88
  },
89
+ "overrides": {
90
+ "proxy-addr": "2.0.8"
91
+ },
88
92
  "repository": {
89
93
  "type": "git",
90
94
  "url": "https://github.com/hasna/apps.git",