@hasna/switcher 0.2.25 → 0.2.26

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
@@ -631,6 +631,26 @@ switcher gateway stop | uninstall
631
631
 
632
632
  Provider credentials stay in gateway memory only. After a gateway restart, a credential from `switcher credentials bind` is read again; an environment-only credential is not recoverable, and its sessions get a 401 naming the fix instead of a refused connection. `SWITCHER_GATEWAY=local` selects the previous per-launch gateway. See [persistent gateway](docs/GATEWAY-SERVICE.md).
633
633
 
634
+ When the persistent gateway cannot be used (it could not start, or it refused the registration), the launch still runs on a per-launch gateway, as before, but prints a `switcher: WARNING:` line with the reason and port, and records it in `~/.hasna/switcher/state/per-launch-gateways/launch-XXXXXX.json` (port, harness, model, working directory, launcher PID, reason; `endedAt` once the port closes; never a token). A session left running or resumed after that launch ends cannot reach the port.
635
+
636
+ ## Resume a Claude session
637
+
638
+ A Claude session that Switcher launched has no provider route of its own. A plain `claude --resume <id>` sends its stored model to Claude's default endpoint and every turn fails. Resume it through Switcher instead:
639
+
640
+ ```sh
641
+ switcher resume <claude-session-id> # relaunch on its profile, cwd and route
642
+ switcher resume <claude-session-id> --check # read-only: {"owned","profile","cwd","recordPath"}
643
+ switcher resume <claude-session-id> --print # prepare the route, print argv and env file, start nothing
644
+ switcher resume <claude-session-id> --dry-run # profile, cwd and argv; nothing registered
645
+ switcher resume <claude-session-id> --cwd DIR -- --fork-session
646
+ ```
647
+
648
+ Each Claude launch's gateway records the sessions it serves: the first request of a session (its `x-claude-code-session-id` header, or `session_id` in the request's `metadata.user_id`) writes `~/.hasna/switcher/state/sessions/claude/<profile>/claude-sessions/<session id>.json` (mode 0600, the launch's working directory and the time inside; no token or content). That covers new, resumed, forked and continued sessions and those `/clear` starts. `switcher resume` reads the newest record across profiles and runs `switcher launch <profile> --cwd <recorded cwd> -- --resume <id>`: a fresh lease on the persistent gateway, the profile's `--settings` route file and model. A session no record owns is refused (exit 3), never resumed plainly. `--state-dir`, `--executable`, `--share-native-state`/`--no-share-native-state` and `--timeout` are passed to that launch; pass the native-state option the session was launched with.
649
+
650
+ `--print` registers the lease and writes the route file but does not start Claude. It prints `{"argv","cwd","envKeys","envFile","routeFile","lease"}`; the values (the loopback gateway token among them, never a provider credential) are only in `envFile`, `~/.hasna/switcher/state/resume/<session id>.env`, mode 0600, as `export NAME='value'` lines. Start the argv in `cwd` after sourcing that file. The lease is released when the command exits; the launch stays current for the gateway's grace period (10 minutes by default) and, because its Claude route is durable, the gateway serves it again on the first request from any process of this user for 30 days after it ends. `--print` needs that durable route and exits 5 when it cannot have it (for example with `SWITCHER_GATEWAY=local` or the gateway down).
651
+
652
+ Exit codes: 0 ok (the harness's own code once it runs), 2 usage, 3 not owned by Switcher, 4 profile missing or no longer a Claude profile, 5 route or lease unavailable, 1 other errors.
653
+
634
654
  ## Run a persistent service
635
655
 
636
656
  For a private local/self-hosted service, inject a random operator token of at least 24 characters as `HASNA_SWITCHER_API_KEY`. For a hosted PostgreSQL service, inject `HASNA_SWITCHER_API_SIGNING_KEY` (or the shared `HASNA_API_SIGNING_KEY` / `API_KEY_SIGNING_SECRET`) and issue revocable, scoped `switcher:read` / `switcher:write` client keys through `@hasna/contracts`. Exactly one authentication mode is accepted. Provider credentials stay in each launcher process; hosted catalog refresh uploads metadata only.
@@ -0,0 +1,29 @@
1
+ export declare const isClaudeSessionId: (value: unknown) => value is string;
2
+ export declare const claudeSessionsDirectory: (sessionDir: string) => string;
3
+ /** The session a Claude Code request belongs to, or undefined when it names none. */
4
+ export declare function claudeRequestSessionId(headers: Headers, body: unknown): string | undefined;
5
+ export type ClaudeSessionRecord = {
6
+ schema: 1;
7
+ sessionId: string;
8
+ cwd: string;
9
+ observedAt: string;
10
+ };
11
+ /** Write (or replace) the record of a session in a profile's session folder, owner-only. */
12
+ export declare function recordClaudeSession(sessionDir: string, session: {
13
+ sessionId: string;
14
+ cwd: string;
15
+ observedAt?: string;
16
+ }): Promise<string>;
17
+ export type ClaudeSessionOwner = {
18
+ sessionId: string;
19
+ profileId: string;
20
+ cwd: string;
21
+ observedAt: string;
22
+ record: string;
23
+ sessionDir: string;
24
+ };
25
+ /**
26
+ * The profile that last served a Claude session in this state root, or undefined. When a session
27
+ * ran under more than one profile, the most recent observation wins: that is the route it used last.
28
+ */
29
+ export declare function findClaudeSession(root: string, sessionId: string): Promise<ClaudeSessionOwner | undefined>;